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