| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPAdminify\Inc\Modules\AdminPages; |
| 4 |
|
| 5 |
use WPAdminify\Inc\Utils ; |
| 6 |
use WPAdminify\Inc\Classes\Multisite_Helper ; |
| 7 |
use WPAdminify\Inc\Modules\AdminPages\AdminPages_Render ; |
| 8 |
// no direct access allowed |
| 9 |
if ( !defined( 'ABSPATH' ) ) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
/** |
| 13 |
* WPAdminify |
| 14 |
* @package Admin Pages |
| 15 |
* |
| 16 |
* @author Jewel Theme <support@jeweltheme.com> |
| 17 |
*/ |
| 18 |
class AdminPages_Output extends AdminPagesModel |
| 19 |
{ |
| 20 |
public function __construct() |
| 21 |
{ |
| 22 |
$this->adminpages_init(); |
| 23 |
} |
| 24 |
|
| 25 |
public function adminpages_init() |
| 26 |
{ |
| 27 |
add_action( 'admin_menu', [ $this, 'render_menu' ] ); |
| 28 |
add_action( 'wp', [ $this, 'render_front' ] ); |
| 29 |
add_filter( 'adminify_admin_page_user_roles', [ $this, 'add_super_admin' ] ); |
| 30 |
} |
| 31 |
|
| 32 |
public function render_menu() |
| 33 |
{ |
| 34 |
} |
| 35 |
|
| 36 |
public function get_posts( $menu_type ) |
| 37 |
{ |
| 38 |
$posts = get_posts( array( |
| 39 |
'post_type' => 'adminify_admin_page', |
| 40 |
'post_status' => 'publish', |
| 41 |
'posts_per_page' => -1, |
| 42 |
'meta_query' => array( array( |
| 43 |
'key' => $this->prefix . 'menu_type', |
| 44 |
'compare' => 'IN', |
| 45 |
'value' => $menu_type, |
| 46 |
) ), |
| 47 |
) ); |
| 48 |
$posts = ( $posts ? $posts : array() ); |
| 49 |
foreach ( $posts as $post ) { |
| 50 |
$post_id = $post->ID; |
| 51 |
$post->menu_type = get_post_meta( $post_id, '_wp_adminify_menu_type', true ); |
| 52 |
$post->menu_parent = get_post_meta( $post_id, '_wp_adminify_sub_menu_item', true ); |
| 53 |
$post->menu_order = get_post_meta( $post_id, '_wp_adminify_menu_order', true ); |
| 54 |
$post->menu_order = ( $post->menu_order ? absint( $post->menu_order ) : 10 ); |
| 55 |
$post->icon_class = get_post_meta( $post_id, $this->prefix . 'menu_icon', true ); |
| 56 |
$post->allowed_roles = get_post_meta( $post_id, $this->prefix . 'user_roles', true ); |
| 57 |
$post->allowed_roles = ( empty($post->allowed_roles) ? array( 'all' ) : $post->allowed_roles ); |
| 58 |
$post->allowed_roles = ( is_string( $post->allowed_roles ) ? unserialize( $post->allowed_roles ) : $post->allowed_roles ); |
| 59 |
$post->custom_css = get_post_meta( $post_id, '_wp_adminify_custom_css', true ); |
| 60 |
$post->custom_js = get_post_meta( $post_id, '_wp_adminify_custom_js', true ); |
| 61 |
$post->remove_page_title = (int) get_post_meta( $post_id, $this->prefix . 'page_title', true ); |
| 62 |
$post->remove_page_margin = (int) get_post_meta( $post_id, $this->prefix . 'remove_margin', true ); |
| 63 |
$post->remove_admin_notices = get_post_meta( $post_id, $this->prefix . 'remove_notice', true ); |
| 64 |
} |
| 65 |
wp_reset_postdata(); |
| 66 |
return $posts; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Register Menu and Submenu Admin Pages |
| 71 |
* |
| 72 |
* @param array $posts |
| 73 |
* @param boolean $from_multisite |
| 74 |
* |
| 75 |
* @return void |
| 76 |
*/ |
| 77 |
public function prepare_menu( $posts, $from_multisite = false ) |
| 78 |
{ |
| 79 |
$user_roles = wp_get_current_user()->roles; |
| 80 |
$user_roles = apply_filters( 'adminify_admin_page_user_roles', $user_roles ); |
| 81 |
foreach ( $posts as $post ) { |
| 82 |
$is_allowed = false; |
| 83 |
|
| 84 |
if ( in_array( 'all', $post->allowed_roles, true ) ) { |
| 85 |
$is_allowed = true; |
| 86 |
} else { |
| 87 |
foreach ( $user_roles as $user_role ) { |
| 88 |
|
| 89 |
if ( in_array( $user_role, $post->allowed_roles, true ) ) { |
| 90 |
$is_allowed = true; |
| 91 |
break; |
| 92 |
} |
| 93 |
|
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
if ( $is_allowed ) { |
| 98 |
$this->add_menu( $post, $from_multisite ); |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Register Admin Menu or Submenu |
| 105 |
*/ |
| 106 |
public function add_menu( $post, $from_multisite = false ) |
| 107 |
{ |
| 108 |
$menu_title = $post->post_title; |
| 109 |
$menu_slug = $post->post_name; |
| 110 |
$menu_type = $post->menu_type; |
| 111 |
$menu_parent = $post->menu_parent; |
| 112 |
$menu_order = $post->menu_order; |
| 113 |
$icon_class = $post->icon_class; |
| 114 |
|
| 115 |
if ( !empty($icon_class) ) { |
| 116 |
$menu_icon = str_ireplace( 'dashicons ', '', $icon_class ); |
| 117 |
} else { |
| 118 |
$menu_icon = 'none'; |
| 119 |
} |
| 120 |
|
| 121 |
$screen_id = 'adminify_admin_page_' . $menu_slug; |
| 122 |
|
| 123 |
if ( 'top_level' === $menu_type ) { |
| 124 |
add_menu_page( |
| 125 |
$menu_title, |
| 126 |
$menu_title, |
| 127 |
'manage_options', |
| 128 |
$screen_id, |
| 129 |
function () use( $post, $from_multisite ) { |
| 130 |
$this->render_admin_page( $post, $from_multisite ); |
| 131 |
}, |
| 132 |
$menu_icon, |
| 133 |
$menu_order |
| 134 |
); |
| 135 |
} else { |
| 136 |
add_submenu_page( |
| 137 |
$menu_parent, |
| 138 |
$menu_title, |
| 139 |
$menu_title, |
| 140 |
'read', |
| 141 |
$screen_id, |
| 142 |
function () use( $post, $from_multisite ) { |
| 143 |
$this->render_admin_page( $post, $from_multisite ); |
| 144 |
}, |
| 145 |
$menu_order |
| 146 |
); |
| 147 |
} |
| 148 |
|
| 149 |
add_action( 'current_screen', function () use( $post, $screen_id ) { |
| 150 |
global $current_screen ; |
| 151 |
if ( !is_object( $current_screen ) || !property_exists( $current_screen, 'id' ) ) { |
| 152 |
return; |
| 153 |
} |
| 154 |
if ( false === stripos( $current_screen->id, '_' . $screen_id ) ) { |
| 155 |
return; |
| 156 |
} |
| 157 |
if ( $post->remove_admin_notices ) { |
| 158 |
remove_all_actions( 'admin_notices' ); |
| 159 |
} |
| 160 |
add_action( 'admin_print_footer_scripts', function () use( $post ) { |
| 161 |
echo '<script>' ; |
| 162 |
echo $post->custom_js ; |
| 163 |
echo '</script>' ; |
| 164 |
} ); |
| 165 |
} ); |
| 166 |
} |
| 167 |
|
| 168 |
public function render_admin_page( $post, $from_multisite = false ) |
| 169 |
{ |
| 170 |
new AdminPages_Render( $post, $from_multisite = false ); |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* Add Super Admin to existing roles |
| 175 |
* |
| 176 |
* @return void |
| 177 |
*/ |
| 178 |
public function add_super_admin( $roles ) |
| 179 |
{ |
| 180 |
$ms_helper = new Multisite_Helper(); |
| 181 |
if ( $ms_helper->is_multisite_supported() ) { |
| 182 |
if ( is_super_admin() ) { |
| 183 |
array_push( $roles, 'super_admin' ); |
| 184 |
} |
| 185 |
} |
| 186 |
return $roles; |
| 187 |
} |
| 188 |
|
| 189 |
// Accessing from frontend not allowed |
| 190 |
public function render_front() |
| 191 |
{ |
| 192 |
if ( is_user_logged_in() || !is_singular( 'adminify_admin_page' ) ) { |
| 193 |
return; |
| 194 |
} |
| 195 |
wp_safe_redirect( home_url() ); |
| 196 |
} |
| 197 |
|
| 198 |
} |