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-options.php
306 lines
| 1 | <?php |
| 2 | /** |
| 3 | * The option page functionality of the plugin. |
| 4 | * |
| 5 | * @since 0.9.0 |
| 6 | * @package RankMath |
| 7 | * @subpackage RankMath\Admin |
| 8 | * @author Rank Math <support@rankmath.com> |
| 9 | */ |
| 10 | |
| 11 | namespace RankMath\Admin; |
| 12 | |
| 13 | use WP_Http; |
| 14 | use RankMath\KB; |
| 15 | use RankMath\CMB2; |
| 16 | use RankMath\Helper; |
| 17 | use RankMath\Traits\Hooker; |
| 18 | use RankMath\Helpers\Str; |
| 19 | use RankMath\Helpers\Param; |
| 20 | use RankMath\Robots_Txt; |
| 21 | use RankMath\Sitemap\Router; |
| 22 | use RankMath\Sitemap\Sitemap; |
| 23 | use RankMath\Admin\Page; |
| 24 | |
| 25 | defined( 'ABSPATH' ) || exit; |
| 26 | |
| 27 | /** |
| 28 | * Options class. |
| 29 | */ |
| 30 | class Options { |
| 31 | |
| 32 | use Hooker; |
| 33 | |
| 34 | /** |
| 35 | * Page title. |
| 36 | * |
| 37 | * @var string |
| 38 | */ |
| 39 | public $title = 'Settings'; |
| 40 | |
| 41 | /** |
| 42 | * Menu title. |
| 43 | * |
| 44 | * @var string |
| 45 | */ |
| 46 | public $menu_title = 'Settings'; |
| 47 | |
| 48 | /** |
| 49 | * Hold tabs for page. |
| 50 | * |
| 51 | * @var array |
| 52 | */ |
| 53 | public $tabs = []; |
| 54 | |
| 55 | /** |
| 56 | * Hold folder name for tab files. |
| 57 | * |
| 58 | * @var string |
| 59 | */ |
| 60 | public $folder = ''; |
| 61 | |
| 62 | /** |
| 63 | * Menu Position. |
| 64 | * |
| 65 | * @var int |
| 66 | */ |
| 67 | public $position = 10; |
| 68 | |
| 69 | /** |
| 70 | * The capability required for this menu to be displayed to the user. |
| 71 | * |
| 72 | * @var string |
| 73 | */ |
| 74 | public $capability = 'manage_options'; |
| 75 | |
| 76 | /** |
| 77 | * CMB2 option page id. |
| 78 | * |
| 79 | * @var string |
| 80 | */ |
| 81 | private $cmb_id = null; |
| 82 | |
| 83 | /** |
| 84 | * Options key. |
| 85 | * |
| 86 | * @var string |
| 87 | */ |
| 88 | public $key = ''; |
| 89 | |
| 90 | /** |
| 91 | * The Constructor |
| 92 | * |
| 93 | * @param array $config Array of configuration. |
| 94 | */ |
| 95 | public function __construct( $config ) { |
| 96 | $this->config( $config ); |
| 97 | $this->cmb_id = $this->key . '_options'; |
| 98 | |
| 99 | $this->action( 'admin_post_' . $this->key, 'reset_options', 2 ); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Create option object and add settings. |
| 104 | */ |
| 105 | public function register_option_page() { |
| 106 | $current_page = str_replace( 'rank-math-options-', '', $this->key ); |
| 107 | |
| 108 | new Page( |
| 109 | $this->key, |
| 110 | $this->title, |
| 111 | [ |
| 112 | 'position' => $this->position, |
| 113 | 'priority' => 9999, |
| 114 | 'parent' => 'rank-math', |
| 115 | 'capability' => $this->capability, |
| 116 | 'menu_title' => $this->menu_title, |
| 117 | 'render' => [ $this, 'display' ], |
| 118 | 'classes' => $this->get_body_class(), |
| 119 | 'assets' => [ |
| 120 | 'styles' => [ |
| 121 | 'select2-rm' => '', |
| 122 | 'rank-math-common' => '', |
| 123 | 'rank-math-cmb2' => '', |
| 124 | 'wp-components' => '', |
| 125 | 'rank-math-options' => rank_math()->plugin_url() . 'assets/admin/css/option-panel.css', |
| 126 | ], |
| 127 | 'scripts' => [ |
| 128 | 'media-editor' => '', |
| 129 | 'underscore' => '', |
| 130 | 'select2-rm' => '', |
| 131 | 'lodash' => '', |
| 132 | 'rank-math-common' => '', |
| 133 | 'wp-api-fetch' => '', |
| 134 | 'wp-data' => '', |
| 135 | 'rank-math-components' => '', |
| 136 | 'rank-math-options' => rank_math()->plugin_url() . 'assets/admin/js/settings.js', |
| 137 | ], |
| 138 | 'json' => $this->get_json_data( $current_page ), |
| 139 | ], |
| 140 | ] |
| 141 | ); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Set the default values if not set. |
| 146 | * |
| 147 | * @param CMB2 $cmb The CMB2 object to hookup. |
| 148 | */ |
| 149 | public function set_defaults( $cmb ) { |
| 150 | foreach ( $cmb->prop( 'fields' ) as $id => $field_args ) { |
| 151 | $field = $cmb->get_field( $id ); |
| 152 | if ( isset( $field_args['default'] ) || isset( $field_args['default_cb'] ) ) { |
| 153 | $defaults[ $id ] = $field->get_default(); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | // Save Defaults if any. |
| 158 | if ( ! empty( $defaults ) ) { |
| 159 | add_option( $this->key, $defaults ); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Reset options. |
| 165 | */ |
| 166 | public function reset_options() { |
| 167 | |
| 168 | if ( ! check_admin_referer( 'rank-math-reset-options' ) || ! current_user_can( 'manage_options' ) ) { |
| 169 | return false; |
| 170 | } |
| 171 | |
| 172 | $url = wp_get_referer(); |
| 173 | if ( ! $url ) { |
| 174 | $url = admin_url(); |
| 175 | } |
| 176 | |
| 177 | if ( filter_has_var( INPUT_POST, 'reset-cmb' ) && Param::post( 'action' ) === $this->key ) { |
| 178 | delete_option( $this->key ); |
| 179 | Helper::redirect( esc_url_raw( $url ), WP_Http::SEE_OTHER ); |
| 180 | exit; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Add classes to <body> of WordPress admin. |
| 186 | * |
| 187 | * @return string |
| 188 | */ |
| 189 | public function get_body_class() { |
| 190 | $mode = Helper::is_advanced_mode() ? 'advanced' : 'basic'; |
| 191 | return [ |
| 192 | 'rank-math-page ', |
| 193 | 'rank-math-mode-' . $mode, |
| 194 | ]; |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Display Setting on a page. |
| 199 | */ |
| 200 | public function display() { |
| 201 | ?> |
| 202 | <div id="rank-math-options" class="<?php echo esc_attr( $this->cmb_id ); ?>"></div> |
| 203 | <?php |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Get setting tabs. |
| 208 | * |
| 209 | * @return array |
| 210 | */ |
| 211 | private function get_tabs() { |
| 212 | |
| 213 | $filter = str_replace( '-', '_', str_replace( 'rank-math-', '', $this->key ) ); |
| 214 | /** |
| 215 | * Allow developers to add new tabs into option panel. |
| 216 | * |
| 217 | * The dynamic part of hook is, page name without 'rank-math-' prefix. |
| 218 | * |
| 219 | * @param array $tabs |
| 220 | */ |
| 221 | return $this->do_filter( "admin/options/{$filter}_tabs", $this->tabs ); |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Get localized data for the current settings page. |
| 226 | * |
| 227 | * @param string $current_page Current Settings page. |
| 228 | * |
| 229 | * @return array |
| 230 | */ |
| 231 | private function get_json_data( $current_page ) { |
| 232 | if ( Param::get( 'page' ) !== $this->key ) { |
| 233 | return []; |
| 234 | } |
| 235 | |
| 236 | if ( is_admin() ) { |
| 237 | rank_math()->variables->setup(); |
| 238 | rank_math()->variables->setup_json(); |
| 239 | } |
| 240 | |
| 241 | $tabs = $this->get_tabs(); |
| 242 | $data = $this->do_filter( |
| 243 | "admin/options/{$current_page}_data", |
| 244 | [ |
| 245 | 'isPro' => defined( 'RANK_MATH_PRO_FILE' ), |
| 246 | 'tabs' => array_keys( $tabs ), |
| 247 | 'optionPage' => $current_page, |
| 248 | 'homeUrl' => get_home_url(), |
| 249 | 'data' => $current_page === 'instant-indexing' ? get_option( 'rank-math-options-instant-indexing' ) : Helper::get_settings( $current_page ), |
| 250 | 'isSiteConnected' => Helper::is_site_connected(), |
| 251 | 'choices' => [ |
| 252 | 'postTypes' => Helper::choices_post_types(), |
| 253 | 'accessiblePostTypes' => Helper::get_accessible_post_types(), |
| 254 | 'accessibleTaxonomies' => Helper::get_accessible_taxonomies(), |
| 255 | 'choicesPostTypeIcons' => Helper::choices_post_type_icons(), |
| 256 | 'choicesTaxonomyIcons' => Helper::choices_taxonomy_icons(), |
| 257 | ], |
| 258 | ] |
| 259 | ); |
| 260 | foreach ( $tabs as $tab ) { |
| 261 | if ( empty( $tab['json'] ) ) { |
| 262 | continue; |
| 263 | } |
| 264 | |
| 265 | $data = array_merge( $data, $tab['json'] ); |
| 266 | } |
| 267 | |
| 268 | $method = "get_{$current_page}_data"; |
| 269 | if ( ! method_exists( $this, $method ) ) { |
| 270 | return $data; |
| 271 | } |
| 272 | |
| 273 | return array_merge( $data, $this->$method() ); |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * Get General Settings page data. |
| 278 | * |
| 279 | * @return array |
| 280 | */ |
| 281 | private function get_general_data() { |
| 282 | return [ |
| 283 | 'activateUrl' => Admin_Helper::get_activate_url( admin_url( 'admin.php??page=rank-math-options-general&tab=content-ai' ) ), |
| 284 | 'hasBreadcrumbSupport' => current_theme_supports( 'rank-math-breadcrumbs' ), |
| 285 | 'showBlogPage' => 'page' === get_option( 'show_on_front' ) && get_option( 'page_for_posts' ) > 0, |
| 286 | 'isEditAllowed' => Helper::is_edit_allowed(), |
| 287 | 'defaultLanguage' => Helper::content_ai_default_language(), |
| 288 | ]; |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * Get General Settings page data. |
| 293 | * |
| 294 | * @return array |
| 295 | */ |
| 296 | private function get_titles_data() { |
| 297 | $data = [ |
| 298 | 'choicesRobots' => Helper::choices_robots(), |
| 299 | 'supportsTitleTag' => current_theme_supports( 'title-tag' ) || wp_is_block_theme(), |
| 300 | 'schemaTypes' => Helper::choices_rich_snippet_types( esc_html__( 'None (Click here to set one)', 'seo-by-rank-math' ) ), |
| 301 | 'isRedirectAttachments' => Helper::get_settings( 'general.attachment_redirect_urls' ), |
| 302 | ]; |
| 303 | return $data; |
| 304 | } |
| 305 | } |
| 306 |