No Description

class-wp-customize-setting.php 29KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984
  1. <?php
  2. /**
  3. * WordPress Customize Setting classes
  4. *
  5. * @package WordPress
  6. * @subpackage Customize
  7. * @since 3.4.0
  8. */
  9. /**
  10. * Customize Setting class.
  11. *
  12. * Handles saving and sanitizing of settings.
  13. *
  14. * @since 3.4.0
  15. *
  16. * @see WP_Customize_Manager
  17. * @link https://developer.wordpress.org/themes/customize-api
  18. */
  19. class WP_Customize_Setting {
  20. /**
  21. * Customizer bootstrap instance.
  22. *
  23. * @since 3.4.0
  24. * @var WP_Customize_Manager
  25. */
  26. public $manager;
  27. /**
  28. * Unique string identifier for the setting.
  29. *
  30. * @since 3.4.0
  31. * @var string
  32. */
  33. public $id;
  34. /**
  35. * Type of customize settings.
  36. *
  37. * @since 3.4.0
  38. * @var string
  39. */
  40. public $type = 'theme_mod';
  41. /**
  42. * Capability required to edit this setting.
  43. *
  44. * @since 3.4.0
  45. * @var string|array
  46. */
  47. public $capability = 'edit_theme_options';
  48. /**
  49. * Theme features required to support the setting.
  50. *
  51. * @since 3.4.0
  52. * @var string|string[]
  53. */
  54. public $theme_supports = '';
  55. /**
  56. * The default value for the setting.
  57. *
  58. * @since 3.4.0
  59. * @var string
  60. */
  61. public $default = '';
  62. /**
  63. * Options for rendering the live preview of changes in Customizer.
  64. *
  65. * Set this value to 'postMessage' to enable a custom JavaScript handler to render changes to this setting
  66. * as opposed to reloading the whole page.
  67. *
  68. * @since 3.4.0
  69. * @var string
  70. */
  71. public $transport = 'refresh';
  72. /**
  73. * Server-side validation callback for the setting's value.
  74. *
  75. * @since 4.6.0
  76. * @var callable
  77. */
  78. public $validate_callback = '';
  79. /**
  80. * Callback to filter a Customize setting value in un-slashed form.
  81. *
  82. * @since 3.4.0
  83. * @var callable
  84. */
  85. public $sanitize_callback = '';
  86. /**
  87. * Callback to convert a Customize PHP setting value to a value that is JSON serializable.
  88. *
  89. * @since 3.4.0
  90. * @var callable
  91. */
  92. public $sanitize_js_callback = '';
  93. /**
  94. * Whether or not the setting is initially dirty when created.
  95. *
  96. * This is used to ensure that a setting will be sent from the pane to the
  97. * preview when loading the Customizer. Normally a setting only is synced to
  98. * the preview if it has been changed. This allows the setting to be sent
  99. * from the start.
  100. *
  101. * @since 4.2.0
  102. * @var bool
  103. */
  104. public $dirty = false;
  105. /**
  106. * ID Data.
  107. *
  108. * @since 3.4.0
  109. * @var array
  110. */
  111. protected $id_data = array();
  112. /**
  113. * Whether or not preview() was called.
  114. *
  115. * @since 4.4.0
  116. * @var bool
  117. */
  118. protected $is_previewed = false;
  119. /**
  120. * Cache of multidimensional values to improve performance.
  121. *
  122. * @since 4.4.0
  123. * @var array
  124. */
  125. protected static $aggregated_multidimensionals = array();
  126. /**
  127. * Whether the multidimensional setting is aggregated.
  128. *
  129. * @since 4.4.0
  130. * @var bool
  131. */
  132. protected $is_multidimensional_aggregated = false;
  133. /**
  134. * Constructor.
  135. *
  136. * Any supplied $args override class property defaults.
  137. *
  138. * @since 3.4.0
  139. *
  140. * @param WP_Customize_Manager $manager Customizer bootstrap instance.
  141. * @param string $id A specific ID of the setting.
  142. * Can be a theme mod or option name.
  143. * @param array $args {
  144. * Optional. Array of properties for the new Setting object. Default empty array.
  145. *
  146. * @type string $type Type of the setting. Default 'theme_mod'.
  147. * @type string $capability Capability required for the setting. Default 'edit_theme_options'
  148. * @type string|string[] $theme_supports Theme features required to support the panel. Default is none.
  149. * @type string $default Default value for the setting. Default is empty string.
  150. * @type string $transport Options for rendering the live preview of changes in Customizer.
  151. * Using 'refresh' makes the change visible by reloading the whole preview.
  152. * Using 'postMessage' allows a custom JavaScript to handle live changes.
  153. * Default is 'refresh'.
  154. * @type callable $validate_callback Server-side validation callback for the setting's value.
  155. * @type callable $sanitize_callback Callback to filter a Customize setting value in un-slashed form.
  156. * @type callable $sanitize_js_callback Callback to convert a Customize PHP setting value to a value that is
  157. * JSON serializable.
  158. * @type bool $dirty Whether or not the setting is initially dirty when created.
  159. * }
  160. */
  161. public function __construct( $manager, $id, $args = array() ) {
  162. $keys = array_keys( get_object_vars( $this ) );
  163. foreach ( $keys as $key ) {
  164. if ( isset( $args[ $key ] ) ) {
  165. $this->$key = $args[ $key ];
  166. }
  167. }
  168. $this->manager = $manager;
  169. $this->id = $id;
  170. // Parse the ID for array keys.
  171. $this->id_data['keys'] = preg_split( '/\[/', str_replace( ']', '', $this->id ) );
  172. $this->id_data['base'] = array_shift( $this->id_data['keys'] );
  173. // Rebuild the ID.
  174. $this->id = $this->id_data['base'];
  175. if ( ! empty( $this->id_data['keys'] ) ) {
  176. $this->id .= '[' . implode( '][', $this->id_data['keys'] ) . ']';
  177. }
  178. if ( $this->validate_callback ) {
  179. add_filter( "customize_validate_{$this->id}", $this->validate_callback, 10, 3 );
  180. }
  181. if ( $this->sanitize_callback ) {
  182. add_filter( "customize_sanitize_{$this->id}", $this->sanitize_callback, 10, 2 );
  183. }
  184. if ( $this->sanitize_js_callback ) {
  185. add_filter( "customize_sanitize_js_{$this->id}", $this->sanitize_js_callback, 10, 2 );
  186. }
  187. if ( 'option' === $this->type || 'theme_mod' === $this->type ) {
  188. // Other setting types can opt-in to aggregate multidimensional explicitly.
  189. $this->aggregate_multidimensional();
  190. // Allow option settings to indicate whether they should be autoloaded.
  191. if ( 'option' === $this->type && isset( $args['autoload'] ) ) {
  192. self::$aggregated_multidimensionals[ $this->type ][ $this->id_data['base'] ]['autoload'] = $args['autoload'];
  193. }
  194. }
  195. }
  196. /**
  197. * Get parsed ID data for multidimensional setting.
  198. *
  199. * @since 4.4.0
  200. *
  201. * @return array {
  202. * ID data for multidimensional setting.
  203. *
  204. * @type string $base ID base
  205. * @type array $keys Keys for multidimensional array.
  206. * }
  207. */
  208. final public function id_data() {
  209. return $this->id_data;
  210. }
  211. /**
  212. * Set up the setting for aggregated multidimensional values.
  213. *
  214. * When a multidimensional setting gets aggregated, all of its preview and update
  215. * calls get combined into one call, greatly improving performance.
  216. *
  217. * @since 4.4.0
  218. */
  219. protected function aggregate_multidimensional() {
  220. $id_base = $this->id_data['base'];
  221. if ( ! isset( self::$aggregated_multidimensionals[ $this->type ] ) ) {
  222. self::$aggregated_multidimensionals[ $this->type ] = array();
  223. }
  224. if ( ! isset( self::$aggregated_multidimensionals[ $this->type ][ $id_base ] ) ) {
  225. self::$aggregated_multidimensionals[ $this->type ][ $id_base ] = array(
  226. 'previewed_instances' => array(), // Calling preview() will add the $setting to the array.
  227. 'preview_applied_instances' => array(), // Flags for which settings have had their values applied.
  228. 'root_value' => $this->get_root_value( array() ), // Root value for initial state, manipulated by preview and update calls.
  229. );
  230. }
  231. if ( ! empty( $this->id_data['keys'] ) ) {
  232. // Note the preview-applied flag is cleared at priority 9 to ensure it is cleared before a deferred-preview runs.
  233. add_action( "customize_post_value_set_{$this->id}", array( $this, '_clear_aggregated_multidimensional_preview_applied_flag' ), 9 );
  234. $this->is_multidimensional_aggregated = true;
  235. }
  236. }
  237. /**
  238. * Reset `$aggregated_multidimensionals` static variable.
  239. *
  240. * This is intended only for use by unit tests.
  241. *
  242. * @since 4.5.0
  243. * @ignore
  244. */
  245. static public function reset_aggregated_multidimensionals() {
  246. self::$aggregated_multidimensionals = array();
  247. }
  248. /**
  249. * The ID for the current site when the preview() method was called.
  250. *
  251. * @since 4.2.0
  252. * @var int
  253. */
  254. protected $_previewed_blog_id;
  255. /**
  256. * Return true if the current site is not the same as the previewed site.
  257. *
  258. * @since 4.2.0
  259. *
  260. * @return bool If preview() has been called.
  261. */
  262. public function is_current_blog_previewed() {
  263. if ( ! isset( $this->_previewed_blog_id ) ) {
  264. return false;
  265. }
  266. return ( get_current_blog_id() === $this->_previewed_blog_id );
  267. }
  268. /**
  269. * Original non-previewed value stored by the preview method.
  270. *
  271. * @see WP_Customize_Setting::preview()
  272. * @since 4.1.1
  273. * @var mixed
  274. */
  275. protected $_original_value;
  276. /**
  277. * Add filters to supply the setting's value when accessed.
  278. *
  279. * If the setting already has a pre-existing value and there is no incoming
  280. * post value for the setting, then this method will short-circuit since
  281. * there is no change to preview.
  282. *
  283. * @since 3.4.0
  284. * @since 4.4.0 Added boolean return value.
  285. *
  286. * @return bool False when preview short-circuits due no change needing to be previewed.
  287. */
  288. public function preview() {
  289. if ( ! isset( $this->_previewed_blog_id ) ) {
  290. $this->_previewed_blog_id = get_current_blog_id();
  291. }
  292. // Prevent re-previewing an already-previewed setting.
  293. if ( $this->is_previewed ) {
  294. return true;
  295. }
  296. $id_base = $this->id_data['base'];
  297. $is_multidimensional = ! empty( $this->id_data['keys'] );
  298. $multidimensional_filter = array( $this, '_multidimensional_preview_filter' );
  299. /*
  300. * Check if the setting has a pre-existing value (an isset check),
  301. * and if doesn't have any incoming post value. If both checks are true,
  302. * then the preview short-circuits because there is nothing that needs
  303. * to be previewed.
  304. */
  305. $undefined = new stdClass();
  306. $needs_preview = ( $undefined !== $this->post_value( $undefined ) );
  307. $value = null;
  308. // Since no post value was defined, check if we have an initial value set.
  309. if ( ! $needs_preview ) {
  310. if ( $this->is_multidimensional_aggregated ) {
  311. $root = self::$aggregated_multidimensionals[ $this->type ][ $id_base ]['root_value'];
  312. $value = $this->multidimensional_get( $root, $this->id_data['keys'], $undefined );
  313. } else {
  314. $default = $this->default;
  315. $this->default = $undefined; // Temporarily set default to undefined so we can detect if existing value is set.
  316. $value = $this->value();
  317. $this->default = $default;
  318. }
  319. $needs_preview = ( $undefined === $value ); // Because the default needs to be supplied.
  320. }
  321. // If the setting does not need previewing now, defer to when it has a value to preview.
  322. if ( ! $needs_preview ) {
  323. if ( ! has_action( "customize_post_value_set_{$this->id}", array( $this, 'preview' ) ) ) {
  324. add_action( "customize_post_value_set_{$this->id}", array( $this, 'preview' ) );
  325. }
  326. return false;
  327. }
  328. switch ( $this->type ) {
  329. case 'theme_mod':
  330. if ( ! $is_multidimensional ) {
  331. add_filter( "theme_mod_{$id_base}", array( $this, '_preview_filter' ) );
  332. } else {
  333. if ( empty( self::$aggregated_multidimensionals[ $this->type ][ $id_base ]['previewed_instances'] ) ) {
  334. // Only add this filter once for this ID base.
  335. add_filter( "theme_mod_{$id_base}", $multidimensional_filter );
  336. }
  337. self::$aggregated_multidimensionals[ $this->type ][ $id_base ]['previewed_instances'][ $this->id ] = $this;
  338. }
  339. break;
  340. case 'option':
  341. if ( ! $is_multidimensional ) {
  342. add_filter( "pre_option_{$id_base}", array( $this, '_preview_filter' ) );
  343. } else {
  344. if ( empty( self::$aggregated_multidimensionals[ $this->type ][ $id_base ]['previewed_instances'] ) ) {
  345. // Only add these filters once for this ID base.
  346. add_filter( "option_{$id_base}", $multidimensional_filter );
  347. add_filter( "default_option_{$id_base}", $multidimensional_filter );
  348. }
  349. self::$aggregated_multidimensionals[ $this->type ][ $id_base ]['previewed_instances'][ $this->id ] = $this;
  350. }
  351. break;
  352. default:
  353. /**
  354. * Fires when the WP_Customize_Setting::preview() method is called for settings
  355. * not handled as theme_mods or options.
  356. *
  357. * The dynamic portion of the hook name, `$this->id`, refers to the setting ID.
  358. *
  359. * @since 3.4.0
  360. *
  361. * @param WP_Customize_Setting $this WP_Customize_Setting instance.
  362. */
  363. do_action( "customize_preview_{$this->id}", $this );
  364. /**
  365. * Fires when the WP_Customize_Setting::preview() method is called for settings
  366. * not handled as theme_mods or options.
  367. *
  368. * The dynamic portion of the hook name, `$this->type`, refers to the setting type.
  369. *
  370. * @since 4.1.0
  371. *
  372. * @param WP_Customize_Setting $this WP_Customize_Setting instance.
  373. */
  374. do_action( "customize_preview_{$this->type}", $this );
  375. }
  376. $this->is_previewed = true;
  377. return true;
  378. }
  379. /**
  380. * Clear out the previewed-applied flag for a multidimensional-aggregated value whenever its post value is updated.
  381. *
  382. * This ensures that the new value will get sanitized and used the next time
  383. * that `WP_Customize_Setting::_multidimensional_preview_filter()`
  384. * is called for this setting.
  385. *
  386. * @since 4.4.0
  387. *
  388. * @see WP_Customize_Manager::set_post_value()
  389. * @see WP_Customize_Setting::_multidimensional_preview_filter()
  390. */
  391. final public function _clear_aggregated_multidimensional_preview_applied_flag() {
  392. unset( self::$aggregated_multidimensionals[ $this->type ][ $this->id_data['base'] ]['preview_applied_instances'][ $this->id ] );
  393. }
  394. /**
  395. * Callback function to filter non-multidimensional theme mods and options.
  396. *
  397. * If switch_to_blog() was called after the preview() method, and the current
  398. * site is now not the same site, then this method does a no-op and returns
  399. * the original value.
  400. *
  401. * @since 3.4.0
  402. *
  403. * @param mixed $original Old value.
  404. * @return mixed New or old value.
  405. */
  406. public function _preview_filter( $original ) {
  407. if ( ! $this->is_current_blog_previewed() ) {
  408. return $original;
  409. }
  410. $undefined = new stdClass(); // Symbol hack.
  411. $post_value = $this->post_value( $undefined );
  412. if ( $undefined !== $post_value ) {
  413. $value = $post_value;
  414. } else {
  415. /*
  416. * Note that we don't use $original here because preview() will
  417. * not add the filter in the first place if it has an initial value
  418. * and there is no post value.
  419. */
  420. $value = $this->default;
  421. }
  422. return $value;
  423. }
  424. /**
  425. * Callback function to filter multidimensional theme mods and options.
  426. *
  427. * For all multidimensional settings of a given type, the preview filter for
  428. * the first setting previewed will be used to apply the values for the others.
  429. *
  430. * @since 4.4.0
  431. *
  432. * @see WP_Customize_Setting::$aggregated_multidimensionals
  433. * @param mixed $original Original root value.
  434. * @return mixed New or old value.
  435. */
  436. final public function _multidimensional_preview_filter( $original ) {
  437. if ( ! $this->is_current_blog_previewed() ) {
  438. return $original;
  439. }
  440. $id_base = $this->id_data['base'];
  441. // If no settings have been previewed yet (which should not be the case, since $this is), just pass through the original value.
  442. if ( empty( self::$aggregated_multidimensionals[ $this->type ][ $id_base ]['previewed_instances'] ) ) {
  443. return $original;
  444. }
  445. foreach ( self::$aggregated_multidimensionals[ $this->type ][ $id_base ]['previewed_instances'] as $previewed_setting ) {
  446. // Skip applying previewed value for any settings that have already been applied.
  447. if ( ! empty( self::$aggregated_multidimensionals[ $this->type ][ $id_base ]['preview_applied_instances'][ $previewed_setting->id ] ) ) {
  448. continue;
  449. }
  450. // Do the replacements of the posted/default sub value into the root value.
  451. $value = $previewed_setting->post_value( $previewed_setting->default );
  452. $root = self::$aggregated_multidimensionals[ $previewed_setting->type ][ $id_base ]['root_value'];
  453. $root = $previewed_setting->multidimensional_replace( $root, $previewed_setting->id_data['keys'], $value );
  454. self::$aggregated_multidimensionals[ $previewed_setting->type ][ $id_base ]['root_value'] = $root;
  455. // Mark this setting having been applied so that it will be skipped when the filter is called again.
  456. self::$aggregated_multidimensionals[ $previewed_setting->type ][ $id_base ]['preview_applied_instances'][ $previewed_setting->id ] = true;
  457. }
  458. return self::$aggregated_multidimensionals[ $this->type ][ $id_base ]['root_value'];
  459. }
  460. /**
  461. * Checks user capabilities and theme supports, and then saves
  462. * the value of the setting.
  463. *
  464. * @since 3.4.0
  465. *
  466. * @return void|false Void on success, false if cap check fails
  467. * or value isn't set or is invalid.
  468. */
  469. final public function save() {
  470. $value = $this->post_value();
  471. if ( ! $this->check_capabilities() || ! isset( $value ) ) {
  472. return false;
  473. }
  474. $id_base = $this->id_data['base'];
  475. /**
  476. * Fires when the WP_Customize_Setting::save() method is called.
  477. *
  478. * The dynamic portion of the hook name, `$id_base` refers to
  479. * the base slug of the setting name.
  480. *
  481. * @since 3.4.0
  482. *
  483. * @param WP_Customize_Setting $this WP_Customize_Setting instance.
  484. */
  485. do_action( "customize_save_{$id_base}", $this );
  486. $this->update( $value );
  487. }
  488. /**
  489. * Fetch and sanitize the $_POST value for the setting.
  490. *
  491. * During a save request prior to save, post_value() provides the new value while value() does not.
  492. *
  493. * @since 3.4.0
  494. *
  495. * @param mixed $default A default value which is used as a fallback. Default null.
  496. * @return mixed The default value on failure, otherwise the sanitized and validated value.
  497. */
  498. final public function post_value( $default = null ) {
  499. return $this->manager->post_value( $this, $default );
  500. }
  501. /**
  502. * Sanitize an input.
  503. *
  504. * @since 3.4.0
  505. *
  506. * @param string|array $value The value to sanitize.
  507. * @return string|array|null|WP_Error Sanitized value, or `null`/`WP_Error` if invalid.
  508. */
  509. public function sanitize( $value ) {
  510. /**
  511. * Filters a Customize setting value in un-slashed form.
  512. *
  513. * @since 3.4.0
  514. *
  515. * @param mixed $value Value of the setting.
  516. * @param WP_Customize_Setting $this WP_Customize_Setting instance.
  517. */
  518. return apply_filters( "customize_sanitize_{$this->id}", $value, $this );
  519. }
  520. /**
  521. * Validates an input.
  522. *
  523. * @since 4.6.0
  524. *
  525. * @see WP_REST_Request::has_valid_params()
  526. *
  527. * @param mixed $value Value to validate.
  528. * @return true|WP_Error True if the input was validated, otherwise WP_Error.
  529. */
  530. public function validate( $value ) {
  531. if ( is_wp_error( $value ) ) {
  532. return $value;
  533. }
  534. if ( is_null( $value ) ) {
  535. return new WP_Error( 'invalid_value', __( 'Invalid value.' ) );
  536. }
  537. $validity = new WP_Error();
  538. /**
  539. * Validates a Customize setting value.
  540. *
  541. * Plugins should amend the `$validity` object via its `WP_Error::add()` method.
  542. *
  543. * The dynamic portion of the hook name, `$this->ID`, refers to the setting ID.
  544. *
  545. * @since 4.6.0
  546. *
  547. * @param WP_Error $validity Filtered from `true` to `WP_Error` when invalid.
  548. * @param mixed $value Value of the setting.
  549. * @param WP_Customize_Setting $setting WP_Customize_Setting instance.
  550. */
  551. $validity = apply_filters( "customize_validate_{$this->id}", $validity, $value, $this );
  552. if ( is_wp_error( $validity ) && ! $validity->has_errors() ) {
  553. $validity = true;
  554. }
  555. return $validity;
  556. }
  557. /**
  558. * Get the root value for a setting, especially for multidimensional ones.
  559. *
  560. * @since 4.4.0
  561. *
  562. * @param mixed $default Value to return if root does not exist.
  563. * @return mixed
  564. */
  565. protected function get_root_value( $default = null ) {
  566. $id_base = $this->id_data['base'];
  567. if ( 'option' === $this->type ) {
  568. return get_option( $id_base, $default );
  569. } elseif ( 'theme_mod' === $this->type ) {
  570. return get_theme_mod( $id_base, $default );
  571. } else {
  572. /*
  573. * Any WP_Customize_Setting subclass implementing aggregate multidimensional
  574. * will need to override this method to obtain the data from the appropriate
  575. * location.
  576. */
  577. return $default;
  578. }
  579. }
  580. /**
  581. * Set the root value for a setting, especially for multidimensional ones.
  582. *
  583. * @since 4.4.0
  584. *
  585. * @param mixed $value Value to set as root of multidimensional setting.
  586. * @return bool Whether the multidimensional root was updated successfully.
  587. */
  588. protected function set_root_value( $value ) {
  589. $id_base = $this->id_data['base'];
  590. if ( 'option' === $this->type ) {
  591. $autoload = true;
  592. if ( isset( self::$aggregated_multidimensionals[ $this->type ][ $this->id_data['base'] ]['autoload'] ) ) {
  593. $autoload = self::$aggregated_multidimensionals[ $this->type ][ $this->id_data['base'] ]['autoload'];
  594. }
  595. return update_option( $id_base, $value, $autoload );
  596. } elseif ( 'theme_mod' === $this->type ) {
  597. set_theme_mod( $id_base, $value );
  598. return true;
  599. } else {
  600. /*
  601. * Any WP_Customize_Setting subclass implementing aggregate multidimensional
  602. * will need to override this method to obtain the data from the appropriate
  603. * location.
  604. */
  605. return false;
  606. }
  607. }
  608. /**
  609. * Save the value of the setting, using the related API.
  610. *
  611. * @since 3.4.0
  612. *
  613. * @param mixed $value The value to update.
  614. * @return bool The result of saving the value.
  615. */
  616. protected function update( $value ) {
  617. $id_base = $this->id_data['base'];
  618. if ( 'option' === $this->type || 'theme_mod' === $this->type ) {
  619. if ( ! $this->is_multidimensional_aggregated ) {
  620. return $this->set_root_value( $value );
  621. } else {
  622. $root = self::$aggregated_multidimensionals[ $this->type ][ $id_base ]['root_value'];
  623. $root = $this->multidimensional_replace( $root, $this->id_data['keys'], $value );
  624. self::$aggregated_multidimensionals[ $this->type ][ $id_base ]['root_value'] = $root;
  625. return $this->set_root_value( $root );
  626. }
  627. } else {
  628. /**
  629. * Fires when the WP_Customize_Setting::update() method is called for settings
  630. * not handled as theme_mods or options.
  631. *
  632. * The dynamic portion of the hook name, `$this->type`, refers to the type of setting.
  633. *
  634. * @since 3.4.0
  635. *
  636. * @param mixed $value Value of the setting.
  637. * @param WP_Customize_Setting $this WP_Customize_Setting instance.
  638. */
  639. do_action( "customize_update_{$this->type}", $value, $this );
  640. return has_action( "customize_update_{$this->type}" );
  641. }
  642. }
  643. /**
  644. * Deprecated method.
  645. *
  646. * @since 3.4.0
  647. * @deprecated 4.4.0 Deprecated in favor of update() method.
  648. */
  649. protected function _update_theme_mod() {
  650. _deprecated_function( __METHOD__, '4.4.0', __CLASS__ . '::update()' );
  651. }
  652. /**
  653. * Deprecated method.
  654. *
  655. * @since 3.4.0
  656. * @deprecated 4.4.0 Deprecated in favor of update() method.
  657. */
  658. protected function _update_option() {
  659. _deprecated_function( __METHOD__, '4.4.0', __CLASS__ . '::update()' );
  660. }
  661. /**
  662. * Fetch the value of the setting.
  663. *
  664. * @since 3.4.0
  665. *
  666. * @return mixed The value.
  667. */
  668. public function value() {
  669. $id_base = $this->id_data['base'];
  670. $is_core_type = ( 'option' === $this->type || 'theme_mod' === $this->type );
  671. if ( ! $is_core_type && ! $this->is_multidimensional_aggregated ) {
  672. // Use post value if previewed and a post value is present.
  673. if ( $this->is_previewed ) {
  674. $value = $this->post_value( null );
  675. if ( null !== $value ) {
  676. return $value;
  677. }
  678. }
  679. $value = $this->get_root_value( $this->default );
  680. /**
  681. * Filters a Customize setting value not handled as a theme_mod or option.
  682. *
  683. * The dynamic portion of the hook name, `$id_base`, refers to
  684. * the base slug of the setting name, initialized from `$this->id_data['base']`.
  685. *
  686. * For settings handled as theme_mods or options, see those corresponding
  687. * functions for available hooks.
  688. *
  689. * @since 3.4.0
  690. * @since 4.6.0 Added the `$this` setting instance as the second parameter.
  691. *
  692. * @param mixed $default The setting default value. Default empty.
  693. * @param WP_Customize_Setting $setting The setting instance.
  694. */
  695. $value = apply_filters( "customize_value_{$id_base}", $value, $this );
  696. } elseif ( $this->is_multidimensional_aggregated ) {
  697. $root_value = self::$aggregated_multidimensionals[ $this->type ][ $id_base ]['root_value'];
  698. $value = $this->multidimensional_get( $root_value, $this->id_data['keys'], $this->default );
  699. // Ensure that the post value is used if the setting is previewed, since preview filters aren't applying on cached $root_value.
  700. if ( $this->is_previewed ) {
  701. $value = $this->post_value( $value );
  702. }
  703. } else {
  704. $value = $this->get_root_value( $this->default );
  705. }
  706. return $value;
  707. }
  708. /**
  709. * Sanitize the setting's value for use in JavaScript.
  710. *
  711. * @since 3.4.0
  712. *
  713. * @return mixed The requested escaped value.
  714. */
  715. public function js_value() {
  716. /**
  717. * Filters a Customize setting value for use in JavaScript.
  718. *
  719. * The dynamic portion of the hook name, `$this->id`, refers to the setting ID.
  720. *
  721. * @since 3.4.0
  722. *
  723. * @param mixed $value The setting value.
  724. * @param WP_Customize_Setting $setting WP_Customize_Setting instance.
  725. */
  726. $value = apply_filters( "customize_sanitize_js_{$this->id}", $this->value(), $this );
  727. if ( is_string( $value ) ) {
  728. return html_entity_decode( $value, ENT_QUOTES, 'UTF-8' );
  729. }
  730. return $value;
  731. }
  732. /**
  733. * Retrieves the data to export to the client via JSON.
  734. *
  735. * @since 4.6.0
  736. *
  737. * @return array Array of parameters passed to JavaScript.
  738. */
  739. public function json() {
  740. return array(
  741. 'value' => $this->js_value(),
  742. 'transport' => $this->transport,
  743. 'dirty' => $this->dirty,
  744. 'type' => $this->type,
  745. );
  746. }
  747. /**
  748. * Validate user capabilities whether the theme supports the setting.
  749. *
  750. * @since 3.4.0
  751. *
  752. * @return bool False if theme doesn't support the setting or user can't change setting, otherwise true.
  753. */
  754. final public function check_capabilities() {
  755. if ( $this->capability && ! current_user_can( $this->capability ) ) {
  756. return false;
  757. }
  758. if ( $this->theme_supports && ! current_theme_supports( ... (array) $this->theme_supports ) ) {
  759. return false;
  760. }
  761. return true;
  762. }
  763. /**
  764. * Multidimensional helper function.
  765. *
  766. * @since 3.4.0
  767. *
  768. * @param array $root
  769. * @param array $keys
  770. * @param bool $create Default false.
  771. * @return array|void Keys are 'root', 'node', and 'key'.
  772. */
  773. final protected function multidimensional( &$root, $keys, $create = false ) {
  774. if ( $create && empty( $root ) ) {
  775. $root = array();
  776. }
  777. if ( ! isset( $root ) || empty( $keys ) ) {
  778. return;
  779. }
  780. $last = array_pop( $keys );
  781. $node = &$root;
  782. foreach ( $keys as $key ) {
  783. if ( $create && ! isset( $node[ $key ] ) ) {
  784. $node[ $key ] = array();
  785. }
  786. if ( ! is_array( $node ) || ! isset( $node[ $key ] ) ) {
  787. return;
  788. }
  789. $node = &$node[ $key ];
  790. }
  791. if ( $create ) {
  792. if ( ! is_array( $node ) ) {
  793. // Account for an array overriding a string or object value.
  794. $node = array();
  795. }
  796. if ( ! isset( $node[ $last ] ) ) {
  797. $node[ $last ] = array();
  798. }
  799. }
  800. if ( ! isset( $node[ $last ] ) ) {
  801. return;
  802. }
  803. return array(
  804. 'root' => &$root,
  805. 'node' => &$node,
  806. 'key' => $last,
  807. );
  808. }
  809. /**
  810. * Will attempt to replace a specific value in a multidimensional array.
  811. *
  812. * @since 3.4.0
  813. *
  814. * @param array $root
  815. * @param array $keys
  816. * @param mixed $value The value to update.
  817. * @return mixed
  818. */
  819. final protected function multidimensional_replace( $root, $keys, $value ) {
  820. if ( ! isset( $value ) ) {
  821. return $root;
  822. } elseif ( empty( $keys ) ) { // If there are no keys, we're replacing the root.
  823. return $value;
  824. }
  825. $result = $this->multidimensional( $root, $keys, true );
  826. if ( isset( $result ) ) {
  827. $result['node'][ $result['key'] ] = $value;
  828. }
  829. return $root;
  830. }
  831. /**
  832. * Will attempt to fetch a specific value from a multidimensional array.
  833. *
  834. * @since 3.4.0
  835. *
  836. * @param array $root
  837. * @param array $keys
  838. * @param mixed $default A default value which is used as a fallback. Default null.
  839. * @return mixed The requested value or the default value.
  840. */
  841. final protected function multidimensional_get( $root, $keys, $default = null ) {
  842. if ( empty( $keys ) ) { // If there are no keys, test the root.
  843. return isset( $root ) ? $root : $default;
  844. }
  845. $result = $this->multidimensional( $root, $keys );
  846. return isset( $result ) ? $result['node'][ $result['key'] ] : $default;
  847. }
  848. /**
  849. * Will attempt to check if a specific value in a multidimensional array is set.
  850. *
  851. * @since 3.4.0
  852. *
  853. * @param array $root
  854. * @param array $keys
  855. * @return bool True if value is set, false if not.
  856. */
  857. final protected function multidimensional_isset( $root, $keys ) {
  858. $result = $this->multidimensional_get( $root, $keys );
  859. return isset( $result );
  860. }
  861. }
  862. /**
  863. * WP_Customize_Filter_Setting class.
  864. */
  865. require_once ABSPATH . WPINC . '/customize/class-wp-customize-filter-setting.php';
  866. /**
  867. * WP_Customize_Header_Image_Setting class.
  868. */
  869. require_once ABSPATH . WPINC . '/customize/class-wp-customize-header-image-setting.php';
  870. /**
  871. * WP_Customize_Background_Image_Setting class.
  872. */
  873. require_once ABSPATH . WPINC . '/customize/class-wp-customize-background-image-setting.php';
  874. /**
  875. * WP_Customize_Nav_Menu_Item_Setting class.
  876. */
  877. require_once ABSPATH . WPINC . '/customize/class-wp-customize-nav-menu-item-setting.php';
  878. /**
  879. * WP_Customize_Nav_Menu_Setting class.
  880. */
  881. require_once ABSPATH . WPINC . '/customize/class-wp-customize-nav-menu-setting.php';