3rdparty
7 months ago
admin
7 months ago
cli
5 years ago
frontend
8 months ago
helpers
7 months ago
metaboxes
2 years ago
module
7 months ago
modules
7 months ago
opengraph
9 months ago
replace-variables
7 months ago
rest
8 months ago
settings
9 months ago
traits
8 months ago
updates
8 months ago
class-auto-updater.php
5 years ago
class-cmb2.php
2 years ago
class-common.php
1 year ago
class-compatibility.php
1 year ago
class-data-encryption.php
1 year ago
class-defaults.php
6 years ago
class-frontend-seo-score.php
1 year ago
class-helper.php
10 months ago
class-installer.php
7 months ago
class-json-manager.php
1 year ago
class-kb.php
7 months ago
class-metadata.php
1 year ago
class-post.php
1 year ago
class-rewrite.php
10 months ago
class-settings.php
1 year ago
class-term.php
1 year ago
class-thumbnail-overlay.php
1 year ago
class-tracking.php
7 months ago
class-update-email.php
3 years ago
class-updates.php
8 months ago
class-user.php
8 months ago
index.php
7 years ago
interface-runner.php
7 years ago
template-tags.php
1 year ago
class-settings.php
191 lines
| 1 | <?php |
| 2 | /** |
| 3 | * The settings functionality of the plugin. |
| 4 | * |
| 5 | * @since 0.9.0 |
| 6 | * @package RankMath |
| 7 | * @subpackage RankMath\Core |
| 8 | * @author Rank Math <support@rankmath.com> |
| 9 | */ |
| 10 | |
| 11 | namespace RankMath; |
| 12 | |
| 13 | use RankMath\Helper; |
| 14 | use RankMath\Helpers\Sitepress; |
| 15 | |
| 16 | defined( 'ABSPATH' ) || exit; |
| 17 | |
| 18 | /** |
| 19 | * Settings class. |
| 20 | */ |
| 21 | class Settings { |
| 22 | |
| 23 | /** |
| 24 | * Hold option data. |
| 25 | * |
| 26 | * @var array |
| 27 | */ |
| 28 | private $keys = []; |
| 29 | |
| 30 | /** |
| 31 | * Options holder. |
| 32 | * |
| 33 | * @var array |
| 34 | */ |
| 35 | private $options = null; |
| 36 | |
| 37 | /** |
| 38 | * The Constructor. |
| 39 | */ |
| 40 | public function __construct() { |
| 41 | add_action( 'init', [ $this, 'init' ] ); |
| 42 | |
| 43 | $this->add_options( 'titles', 'rank-math-options-titles' ); |
| 44 | $this->add_options( 'general', 'rank-math-options-general' ); |
| 45 | $this->add_options( 'sitemap', 'rank-math-options-sitemap' ); |
| 46 | $this->add_options( 'instant_indexing', 'rank-math-options-instant-indexing' ); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Init. |
| 51 | */ |
| 52 | public function init() { |
| 53 | if ( Sitepress::get()->is_active() ) { |
| 54 | $this->reset(); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Add new option data. |
| 60 | * |
| 61 | * @param string $id Unique id. |
| 62 | * @param string $key Option key. |
| 63 | */ |
| 64 | public function add_options( $id, $key ) { |
| 65 | if ( ! empty( $id ) && ! empty( $key ) ) { |
| 66 | |
| 67 | $this->keys[ $id ] = $key; |
| 68 | |
| 69 | // Lazy-Load options. |
| 70 | if ( ! is_null( $this->options ) ) { |
| 71 | $options = get_option( $key, [] ); |
| 72 | $options = $this->normalize_it( $options ); |
| 73 | |
| 74 | $this->options[ $id ] = $options; |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Get settings keys. |
| 81 | * |
| 82 | * @return array |
| 83 | */ |
| 84 | public function get_keys() { |
| 85 | return $this->keys; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Get Setting. |
| 90 | * |
| 91 | * @param string $field_id ID of field to get. |
| 92 | * @param mixed $default_value (Optional) Default value. |
| 93 | * @return mixed |
| 94 | */ |
| 95 | public function get( $field_id = '', $default_value = false ) { |
| 96 | $opts = $this->get_options(); |
| 97 | $ids = explode( '.', $field_id ); |
| 98 | |
| 99 | foreach ( $ids as $id ) { |
| 100 | if ( is_null( $opts ) ) { |
| 101 | break; |
| 102 | } |
| 103 | $opts = isset( $opts[ $id ] ) ? $opts[ $id ] : null; |
| 104 | } |
| 105 | |
| 106 | if ( is_null( $opts ) ) { |
| 107 | return $default_value; |
| 108 | } |
| 109 | |
| 110 | return $opts; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Set value. |
| 115 | * |
| 116 | * @param string $group Setting group. |
| 117 | * @param string $id Setting id. |
| 118 | * @param string $value Setting value. |
| 119 | */ |
| 120 | public function set( $group, $id, $value ) { |
| 121 | $this->options[ $group ][ $id ] = $value; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Get all settings. |
| 126 | * |
| 127 | * @return array |
| 128 | */ |
| 129 | public function all() { |
| 130 | return $this->get_options(); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Get all settings. |
| 135 | * |
| 136 | * @return array |
| 137 | */ |
| 138 | public function all_raw() { |
| 139 | $options = []; |
| 140 | if ( ! empty( $this->keys ) ) { |
| 141 | foreach ( $this->keys as $id => $key ) { |
| 142 | $options[ $id ] = get_option( $key, [] ); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | return $options; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Reset options |
| 151 | */ |
| 152 | public function reset() { |
| 153 | $this->options = null; |
| 154 | $this->get_options(); |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Get options once for use throughout the plugin cycle. |
| 159 | * |
| 160 | * @return array |
| 161 | */ |
| 162 | private function get_options() { |
| 163 | |
| 164 | if ( is_null( $this->options ) && ! empty( $this->keys ) ) { |
| 165 | |
| 166 | $options = []; |
| 167 | foreach ( $this->keys as $id => $key ) { |
| 168 | $options[ $id ] = get_option( $key, [] ); |
| 169 | } |
| 170 | |
| 171 | $this->options = $this->normalize_it( $options ); |
| 172 | } |
| 173 | |
| 174 | return (array) $this->options; |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Normalize option data |
| 179 | * |
| 180 | * @param mixed $options Array to normalize. |
| 181 | * @return mixed |
| 182 | */ |
| 183 | private function normalize_it( $options ) { |
| 184 | foreach ( (array) $options as $key => $value ) { |
| 185 | $options[ $key ] = is_array( $value ) ? $this->normalize_it( $value ) : Helper::normalize_data( $value ); |
| 186 | } |
| 187 | |
| 188 | return $options; |
| 189 | } |
| 190 | } |
| 191 |