database
5 months ago
importers
2 weeks ago
metabox
6 days ago
notifications
5 months ago
views
4 months ago
watcher
2 weeks ago
wizard
2 weeks ago
class-admin-bar-menu.php
2 weeks ago
class-admin-breadcrumbs.php
2 weeks ago
class-admin-dashboard-nav.php
2 weeks ago
class-admin-header.php
2 weeks ago
class-admin-helper.php
2 weeks ago
class-admin-init.php
8 months ago
class-admin-menu.php
2 weeks ago
class-admin.php
6 days ago
class-api.php
1 year ago
class-ask-review.php
2 weeks ago
class-assets.php
2 weeks ago
class-bulk-actions.php
2 weeks ago
class-cmb2-fields.php
2 weeks ago
class-cmb2-options.php
2 weeks ago
class-dashboard-widget.php
2 weeks ago
class-import-export.php
2 weeks ago
class-list-table.php
2 weeks ago
class-lock-modified-date.php
1 year ago
class-notices.php
2 weeks ago
class-option-center.php
2 weeks ago
class-options.php
2 weeks ago
class-page.php
2 weeks ago
class-post-columns.php
2 weeks ago
class-post-filters.php
2 weeks ago
class-pro-notice.php
2 weeks ago
class-register-options-page.php
9 months ago
class-registration.php
2 weeks ago
class-sanitize-settings.php
2 weeks ago
class-setup-wizard.php
2 weeks ago
index.php
7 years ago
class-page.php
381 lines
| 1 | <?php |
| 2 | /** |
| 3 | * The admin-page functionality. |
| 4 | * |
| 5 | * @since 1.0.0 |
| 6 | * @package RankMath |
| 7 | * @subpackage RankMath\Admin |
| 8 | * @author RankMath <support@rankmath.com> |
| 9 | */ |
| 10 | |
| 11 | namespace RankMath\Admin; |
| 12 | |
| 13 | use RankMath\Helpers\Param; |
| 14 | use RankMath\Helper; |
| 15 | |
| 16 | /** |
| 17 | * Page class. |
| 18 | */ |
| 19 | class Page { |
| 20 | |
| 21 | /** |
| 22 | * Unique ID used for menu_slug. |
| 23 | * |
| 24 | * @var string |
| 25 | */ |
| 26 | public $id = null; |
| 27 | |
| 28 | /** |
| 29 | * The text to be displayed in the title tags of the page. |
| 30 | * |
| 31 | * @var string |
| 32 | */ |
| 33 | public $title = null; |
| 34 | |
| 35 | /** |
| 36 | * The slug name for the parent menu. |
| 37 | * |
| 38 | * @var string |
| 39 | */ |
| 40 | public $parent = null; |
| 41 | |
| 42 | /** |
| 43 | * The The on-screen name text for the menu. |
| 44 | * |
| 45 | * @var string |
| 46 | */ |
| 47 | public $menu_title = null; |
| 48 | |
| 49 | /** |
| 50 | * The capability required for this menu to be displayed to the user. |
| 51 | * |
| 52 | * @var string |
| 53 | */ |
| 54 | public $capability = 'manage_options'; |
| 55 | |
| 56 | /** |
| 57 | * The icon for this menu. |
| 58 | * |
| 59 | * @var string |
| 60 | */ |
| 61 | public $icon = 'dashicons-art'; |
| 62 | |
| 63 | /** |
| 64 | * The position in the menu order this menu should appear. |
| 65 | * |
| 66 | * @var int |
| 67 | */ |
| 68 | public $position = -1; |
| 69 | |
| 70 | /** |
| 71 | * The init hook priority. |
| 72 | * |
| 73 | * @var int |
| 74 | */ |
| 75 | public $priority = 25; |
| 76 | |
| 77 | /** |
| 78 | * The function/file that displays the page content for the menu page. |
| 79 | * |
| 80 | * @var string|callable |
| 81 | */ |
| 82 | public $render = null; |
| 83 | |
| 84 | /** |
| 85 | * The function that run on page POST to save data. |
| 86 | * |
| 87 | * @var callable |
| 88 | */ |
| 89 | public $onsave = null; |
| 90 | |
| 91 | /** |
| 92 | * Hold contextual help tabs. |
| 93 | * |
| 94 | * @var array |
| 95 | */ |
| 96 | public $help = null; |
| 97 | |
| 98 | /** |
| 99 | * Hold scripts and styles. |
| 100 | * |
| 101 | * @var array |
| 102 | */ |
| 103 | public $assets = null; |
| 104 | |
| 105 | /** |
| 106 | * Check if plugin is network active. |
| 107 | * |
| 108 | * @var array |
| 109 | */ |
| 110 | public $is_network = false; |
| 111 | |
| 112 | /** |
| 113 | * Hold classes for body tag. |
| 114 | * |
| 115 | * @var array |
| 116 | */ |
| 117 | public $classes = null; |
| 118 | |
| 119 | /** |
| 120 | * Hold localized data. |
| 121 | * |
| 122 | * @var array |
| 123 | */ |
| 124 | public $json = null; |
| 125 | |
| 126 | /** |
| 127 | * The Constructor. |
| 128 | * |
| 129 | * @param string $id Admin page unique id. |
| 130 | * @param string $title Title of the admin page. |
| 131 | * @param array $config Optional. Override page settings. |
| 132 | */ |
| 133 | public function __construct( $id, $title, $config = [] ) { |
| 134 | |
| 135 | // Early bail! |
| 136 | if ( ! $id ) { |
| 137 | wp_die( esc_html__( '$id variable required', 'seo-by-rank-math' ), esc_html__( 'Variable Required', 'seo-by-rank-math' ) ); |
| 138 | } |
| 139 | |
| 140 | if ( ! $title ) { |
| 141 | wp_die( esc_html__( '$title variable required', 'seo-by-rank-math' ), esc_html__( 'Variable Required', 'seo-by-rank-math' ) ); |
| 142 | } |
| 143 | |
| 144 | $this->id = $id; |
| 145 | $this->title = $title; |
| 146 | foreach ( $config as $key => $value ) { |
| 147 | $this->$key = $value; |
| 148 | } |
| 149 | |
| 150 | if ( ! $this->menu_title ) { |
| 151 | $this->menu_title = $title; |
| 152 | } |
| 153 | |
| 154 | add_action( 'init', [ $this, 'init' ], $this->priority ?? 25 ); |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Init admin page when WordPress Initialises. |
| 159 | * |
| 160 | * @codeCoverageIgnore |
| 161 | */ |
| 162 | public function init() { |
| 163 | $priority = $this->parent ? intval( $this->position ) : -1; |
| 164 | add_action( $this->is_network ? 'network_admin_menu' : 'admin_menu', [ $this, 'register_menu' ], $priority ); |
| 165 | |
| 166 | // If not the page is not this page stop here. |
| 167 | if ( ! $this->is_current_page() ) { |
| 168 | return; |
| 169 | } |
| 170 | |
| 171 | $hooks = [ |
| 172 | 'admin_init' => [ |
| 173 | 'callback' => 'save', |
| 174 | 'condition' => ! is_null( $this->onsave ) && is_callable( $this->onsave ), |
| 175 | ], |
| 176 | 'admin_enqueue_scripts' => [ |
| 177 | 'callback' => 'enqueue', |
| 178 | 'condition' => ! empty( $this->assets ), |
| 179 | ], |
| 180 | 'admin_head' => [ |
| 181 | 'callback' => 'contextual_help', |
| 182 | 'condition' => ! empty( $this->help ), |
| 183 | ], |
| 184 | 'admin_body_class' => [ |
| 185 | 'callback' => 'body_class', |
| 186 | 'condition' => ! empty( $this->classes ), |
| 187 | ], |
| 188 | ]; |
| 189 | |
| 190 | foreach ( $hooks as $hook => $data ) { |
| 191 | if ( true === $data['condition'] ) { |
| 192 | add_action( $hook, [ $this, $data['callback'] ] ); |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Register Admin Menu. |
| 199 | * |
| 200 | * @codeCoverageIgnore |
| 201 | */ |
| 202 | public function register_menu() { |
| 203 | if ( ! $this->parent ) { |
| 204 | add_menu_page( $this->title, $this->menu_title, $this->capability, $this->id, [ $this, 'display' ], $this->icon, $this->position ); |
| 205 | return; |
| 206 | } |
| 207 | |
| 208 | add_submenu_page( $this->parent, $this->title, $this->menu_title, $this->capability, $this->id, [ $this, 'display' ] ); |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Enqueue styles and scripts. |
| 213 | * |
| 214 | * @codeCoverageIgnore |
| 215 | */ |
| 216 | public function enqueue() { |
| 217 | $this->enqueue_styles(); |
| 218 | $this->enqueue_scripts(); |
| 219 | $this->add_localized_data(); |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Add classes to <body> of WordPress admin. |
| 224 | * |
| 225 | * @codeCoverageIgnore |
| 226 | * |
| 227 | * @param string $classes Space-separated list of CSS classes. |
| 228 | * |
| 229 | * @return string |
| 230 | */ |
| 231 | public function body_class( $classes = '' ) { |
| 232 | return $classes . ' ' . join( ' ', $this->classes ); |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Save anything you want using onsave function. |
| 237 | * |
| 238 | * @codeCoverageIgnore |
| 239 | */ |
| 240 | public function save() { |
| 241 | call_user_func( $this->onsave, $this ); |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Contextual Help. |
| 246 | * |
| 247 | * @codeCoverageIgnore |
| 248 | */ |
| 249 | public function contextual_help() { |
| 250 | $screen = get_current_screen(); |
| 251 | |
| 252 | foreach ( $this->help as $tab_id => $tab ) { |
| 253 | $tab['id'] = $tab_id; |
| 254 | $tab['content'] = $this->get_help_content( $tab ); |
| 255 | $screen->add_help_tab( $tab ); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * Render admin page content using render function you passed in config. |
| 261 | * |
| 262 | * @codeCoverageIgnore |
| 263 | */ |
| 264 | public function display() { |
| 265 | if ( is_null( $this->render ) ) { |
| 266 | return; |
| 267 | } |
| 268 | |
| 269 | if ( 'settings' === $this->render ) { |
| 270 | return $this->display_settings(); |
| 271 | } |
| 272 | |
| 273 | if ( is_callable( $this->render ) ) { |
| 274 | call_user_func( $this->render, $this ); |
| 275 | return; |
| 276 | } |
| 277 | |
| 278 | if ( is_string( $this->render ) ) { |
| 279 | include_once $this->render; |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * Is the page is current page. |
| 285 | * |
| 286 | * @return bool |
| 287 | */ |
| 288 | public function is_current_page() { |
| 289 | return Param::get( 'page' ) === $this->id; |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * Enqueue styles |
| 294 | * |
| 295 | * @codeCoverageIgnore |
| 296 | */ |
| 297 | private function enqueue_styles() { |
| 298 | if ( ! isset( $this->assets['styles'] ) || empty( $this->assets['styles'] ) ) { |
| 299 | return; |
| 300 | } |
| 301 | |
| 302 | foreach ( $this->assets['styles'] as $handle => $src ) { |
| 303 | wp_enqueue_style( $handle, $src, null, rank_math()->version ); |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * Enqueue scripts. |
| 309 | * |
| 310 | * @codeCoverageIgnore |
| 311 | */ |
| 312 | private function enqueue_scripts() { |
| 313 | if ( ! isset( $this->assets['scripts'] ) || empty( $this->assets['scripts'] ) ) { |
| 314 | return; |
| 315 | } |
| 316 | |
| 317 | foreach ( $this->assets['scripts'] as $handle => $src ) { |
| 318 | if ( $handle === 'media-editor' ) { |
| 319 | wp_enqueue_media(); |
| 320 | } |
| 321 | wp_enqueue_script( $handle, $src, null, rank_math()->version, true ); |
| 322 | } |
| 323 | |
| 324 | do_action( 'rank-math/admin_enqueue_scripts' ); |
| 325 | } |
| 326 | |
| 327 | /** |
| 328 | * Get tab content |
| 329 | * |
| 330 | * @codeCoverageIgnore |
| 331 | * |
| 332 | * @param array $tab Tab to get content for. |
| 333 | * |
| 334 | * @return string |
| 335 | */ |
| 336 | private function get_help_content( $tab ) { |
| 337 | ob_start(); |
| 338 | |
| 339 | // If it is a function. |
| 340 | if ( isset( $tab['content'] ) && is_callable( $tab['content'] ) ) { |
| 341 | call_user_func( $tab['content'] ); |
| 342 | } |
| 343 | |
| 344 | // If it is a file. |
| 345 | if ( isset( $tab['view'] ) && $tab['view'] ) { |
| 346 | require $tab['view']; |
| 347 | } |
| 348 | |
| 349 | return ob_get_clean(); |
| 350 | } |
| 351 | |
| 352 | /** |
| 353 | * Localized data. |
| 354 | */ |
| 355 | private function add_localized_data() { |
| 356 | if ( empty( $this->assets['json'] ) ) { |
| 357 | return; |
| 358 | } |
| 359 | |
| 360 | foreach ( $this->assets['json'] as $key => $value ) { |
| 361 | Helper::add_json( $key, $value ); |
| 362 | } |
| 363 | |
| 364 | Helper::add_json( |
| 365 | 'settings', |
| 366 | [ |
| 367 | 'general' => Helper::get_settings( 'general' ), |
| 368 | 'titles' => Helper::get_settings( 'titles' ), |
| 369 | 'sitemap' => Helper::get_settings( 'sitemap' ), |
| 370 | ] |
| 371 | ); |
| 372 | } |
| 373 | |
| 374 | /** |
| 375 | * Display settings. |
| 376 | */ |
| 377 | private function display_settings() { |
| 378 | echo '<div id="rank-math-settings" class="' . esc_attr( $this->id ) . '"></div>'; |
| 379 | } |
| 380 | } |
| 381 |