PageRenderTime 55ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/wp-includes/class-wp-customize-setting.php

https://gitlab.com/VTTE/sitios-vtte
PHP | 982 lines | 362 code | 102 blank | 518 comment | 76 complexity | 5e9535a7b5ce4585fa648315a7dd2508 MD5 | raw file
  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. * @link https://developer.wordpress.org/themes/customize-api
  16. *
  17. * @see WP_Customize_Manager
  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 string
  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 False if cap check fails or value isn't set or is invalid.
  467. */
  468. final public function save() {
  469. $value = $this->post_value();
  470. if ( ! $this->check_capabilities() || ! isset( $value ) ) {
  471. return false;
  472. }
  473. $id_base = $this->id_data['base'];
  474. /**
  475. * Fires when the WP_Customize_Setting::save() method is called.
  476. *
  477. * The dynamic portion of the hook name, `$id_base` refers to
  478. * the base slug of the setting name.
  479. *
  480. * @since 3.4.0
  481. *
  482. * @param WP_Customize_Setting $this WP_Customize_Setting instance.
  483. */
  484. do_action( "customize_save_{$id_base}", $this );
  485. $this->update( $value );
  486. }
  487. /**
  488. * Fetch and sanitize the $_POST value for the setting.
  489. *
  490. * During a save request prior to save, post_value() provides the new value while value() does not.
  491. *
  492. * @since 3.4.0
  493. *
  494. * @param mixed $default A default value which is used as a fallback. Default is null.
  495. * @return mixed The default value on failure, otherwise the sanitized and validated value.
  496. */
  497. final public function post_value( $default = null ) {
  498. return $this->manager->post_value( $this, $default );
  499. }
  500. /**
  501. * Sanitize an input.
  502. *
  503. * @since 3.4.0
  504. *
  505. * @param string|array $value The value to sanitize.
  506. * @return string|array|null|WP_Error Sanitized value, or `null`/`WP_Error` if invalid.
  507. */
  508. public function sanitize( $value ) {
  509. /**
  510. * Filters a Customize setting value in un-slashed form.
  511. *
  512. * @since 3.4.0
  513. *
  514. * @param mixed $value Value of the setting.
  515. * @param WP_Customize_Setting $this WP_Customize_Setting instance.
  516. */
  517. return apply_filters( "customize_sanitize_{$this->id}", $value, $this );
  518. }
  519. /**
  520. * Validates an input.
  521. *
  522. * @since 4.6.0
  523. *
  524. * @see WP_REST_Request::has_valid_params()
  525. *
  526. * @param mixed $value Value to validate.
  527. * @return true|WP_Error True if the input was validated, otherwise WP_Error.
  528. */
  529. public function validate( $value ) {
  530. if ( is_wp_error( $value ) ) {
  531. return $value;
  532. }
  533. if ( is_null( $value ) ) {
  534. return new WP_Error( 'invalid_value', __( 'Invalid value.' ) );
  535. }
  536. $validity = new WP_Error();
  537. /**
  538. * Validates a Customize setting value.
  539. *
  540. * Plugins should amend the `$validity` object via its `WP_Error::add()` method.
  541. *
  542. * The dynamic portion of the hook name, `$this->ID`, refers to the setting ID.
  543. *
  544. * @since 4.6.0
  545. *
  546. * @param WP_Error $validity Filtered from `true` to `WP_Error` when invalid.
  547. * @param mixed $value Value of the setting.
  548. * @param WP_Customize_Setting $this WP_Customize_Setting instance.
  549. */
  550. $validity = apply_filters( "customize_validate_{$this->id}", $validity, $value, $this );
  551. if ( is_wp_error( $validity ) && ! $validity->has_errors() ) {
  552. $validity = true;
  553. }
  554. return $validity;
  555. }
  556. /**
  557. * Get the root value for a setting, especially for multidimensional ones.
  558. *
  559. * @since 4.4.0
  560. *
  561. * @param mixed $default Value to return if root does not exist.
  562. * @return mixed
  563. */
  564. protected function get_root_value( $default = null ) {
  565. $id_base = $this->id_data['base'];
  566. if ( 'option' === $this->type ) {
  567. return get_option( $id_base, $default );
  568. } elseif ( 'theme_mod' === $this->type ) {
  569. return get_theme_mod( $id_base, $default );
  570. } else {
  571. /*
  572. * Any WP_Customize_Setting subclass implementing aggregate multidimensional
  573. * will need to override this method to obtain the data from the appropriate
  574. * location.
  575. */
  576. return $default;
  577. }
  578. }
  579. /**
  580. * Set the root value for a setting, especially for multidimensional ones.
  581. *
  582. * @since 4.4.0
  583. *
  584. * @param mixed $value Value to set as root of multidimensional setting.
  585. * @return bool Whether the multidimensional root was updated successfully.
  586. */
  587. protected function set_root_value( $value ) {
  588. $id_base = $this->id_data['base'];
  589. if ( 'option' === $this->type ) {
  590. $autoload = true;
  591. if ( isset( self::$aggregated_multidimensionals[ $this->type ][ $this->id_data['base'] ]['autoload'] ) ) {
  592. $autoload = self::$aggregated_multidimensionals[ $this->type ][ $this->id_data['base'] ]['autoload'];
  593. }
  594. return update_option( $id_base, $value, $autoload );
  595. } elseif ( 'theme_mod' === $this->type ) {
  596. set_theme_mod( $id_base, $value );
  597. return true;
  598. } else {
  599. /*
  600. * Any WP_Customize_Setting subclass implementing aggregate multidimensional
  601. * will need to override this method to obtain the data from the appropriate
  602. * location.
  603. */
  604. return false;
  605. }
  606. }
  607. /**
  608. * Save the value of the setting, using the related API.
  609. *
  610. * @since 3.4.0
  611. *
  612. * @param mixed $value The value to update.
  613. * @return bool The result of saving the value.
  614. */
  615. protected function update( $value ) {
  616. $id_base = $this->id_data['base'];
  617. if ( 'option' === $this->type || 'theme_mod' === $this->type ) {
  618. if ( ! $this->is_multidimensional_aggregated ) {
  619. return $this->set_root_value( $value );
  620. } else {
  621. $root = self::$aggregated_multidimensionals[ $this->type ][ $id_base ]['root_value'];
  622. $root = $this->multidimensional_replace( $root, $this->id_data['keys'], $value );
  623. self::$aggregated_multidimensionals[ $this->type ][ $id_base ]['root_value'] = $root;
  624. return $this->set_root_value( $root );
  625. }
  626. } else {
  627. /**
  628. * Fires when the WP_Customize_Setting::update() method is called for settings
  629. * not handled as theme_mods or options.
  630. *
  631. * The dynamic portion of the hook name, `$this->type`, refers to the type of setting.
  632. *
  633. * @since 3.4.0
  634. *
  635. * @param mixed $value Value of the setting.
  636. * @param WP_Customize_Setting $this WP_Customize_Setting instance.
  637. */
  638. do_action( "customize_update_{$this->type}", $value, $this );
  639. return has_action( "customize_update_{$this->type}" );
  640. }
  641. }
  642. /**
  643. * Deprecated method.
  644. *
  645. * @since 3.4.0
  646. * @deprecated 4.4.0 Deprecated in favor of update() method.
  647. */
  648. protected function _update_theme_mod() {
  649. _deprecated_function( __METHOD__, '4.4.0', __CLASS__ . '::update()' );
  650. }
  651. /**
  652. * Deprecated method.
  653. *
  654. * @since 3.4.0
  655. * @deprecated 4.4.0 Deprecated in favor of update() method.
  656. */
  657. protected function _update_option() {
  658. _deprecated_function( __METHOD__, '4.4.0', __CLASS__ . '::update()' );
  659. }
  660. /**
  661. * Fetch the value of the setting.
  662. *
  663. * @since 3.4.0
  664. *
  665. * @return mixed The value.
  666. */
  667. public function value() {
  668. $id_base = $this->id_data['base'];
  669. $is_core_type = ( 'option' === $this->type || 'theme_mod' === $this->type );
  670. if ( ! $is_core_type && ! $this->is_multidimensional_aggregated ) {
  671. // Use post value if previewed and a post value is present.
  672. if ( $this->is_previewed ) {
  673. $value = $this->post_value( null );
  674. if ( null !== $value ) {
  675. return $value;
  676. }
  677. }
  678. $value = $this->get_root_value( $this->default );
  679. /**
  680. * Filters a Customize setting value not handled as a theme_mod or option.
  681. *
  682. * The dynamic portion of the hook name, `$id_base`, refers to
  683. * the base slug of the setting name, initialized from `$this->id_data['base']`.
  684. *
  685. * For settings handled as theme_mods or options, see those corresponding
  686. * functions for available hooks.
  687. *
  688. * @since 3.4.0
  689. * @since 4.6.0 Added the `$this` setting instance as the second parameter.
  690. *
  691. * @param mixed $default The setting default value. Default empty.
  692. * @param WP_Customize_Setting $this The setting instance.
  693. */
  694. $value = apply_filters( "customize_value_{$id_base}", $value, $this );
  695. } elseif ( $this->is_multidimensional_aggregated ) {
  696. $root_value = self::$aggregated_multidimensionals[ $this->type ][ $id_base ]['root_value'];
  697. $value = $this->multidimensional_get( $root_value, $this->id_data['keys'], $this->default );
  698. // Ensure that the post value is used if the setting is previewed, since preview filters aren't applying on cached $root_value.
  699. if ( $this->is_previewed ) {
  700. $value = $this->post_value( $value );
  701. }
  702. } else {
  703. $value = $this->get_root_value( $this->default );
  704. }
  705. return $value;
  706. }
  707. /**
  708. * Sanitize the setting's value for use in JavaScript.
  709. *
  710. * @since 3.4.0
  711. *
  712. * @return mixed The requested escaped value.
  713. */
  714. public function js_value() {
  715. /**
  716. * Filters a Customize setting value for use in JavaScript.
  717. *
  718. * The dynamic portion of the hook name, `$this->id`, refers to the setting ID.
  719. *
  720. * @since 3.4.0
  721. *
  722. * @param mixed $value The setting value.
  723. * @param WP_Customize_Setting $this WP_Customize_Setting instance.
  724. */
  725. $value = apply_filters( "customize_sanitize_js_{$this->id}", $this->value(), $this );
  726. if ( is_string( $value ) ) {
  727. return html_entity_decode( $value, ENT_QUOTES, 'UTF-8' );
  728. }
  729. return $value;
  730. }
  731. /**
  732. * Retrieves the data to export to the client via JSON.
  733. *
  734. * @since 4.6.0
  735. *
  736. * @return array Array of parameters passed to JavaScript.
  737. */
  738. public function json() {
  739. return array(
  740. 'value' => $this->js_value(),
  741. 'transport' => $this->transport,
  742. 'dirty' => $this->dirty,
  743. 'type' => $this->type,
  744. );
  745. }
  746. /**
  747. * Validate user capabilities whether the theme supports the setting.
  748. *
  749. * @since 3.4.0
  750. *
  751. * @return bool False if theme doesn't support the setting or user can't change setting, otherwise true.
  752. */
  753. final public function check_capabilities() {
  754. if ( $this->capability && ! current_user_can( $this->capability ) ) {
  755. return false;
  756. }
  757. if ( $this->theme_supports && ! current_theme_supports( ... (array) $this->theme_supports ) ) {
  758. return false;
  759. }
  760. return true;
  761. }
  762. /**
  763. * Multidimensional helper function.
  764. *
  765. * @since 3.4.0
  766. *
  767. * @param $root
  768. * @param $keys
  769. * @param bool $create Default is false.
  770. * @return array|void Keys are 'root', 'node', and 'key'.
  771. */
  772. final protected function multidimensional( &$root, $keys, $create = false ) {
  773. if ( $create && empty( $root ) ) {
  774. $root = array();
  775. }
  776. if ( ! isset( $root ) || empty( $keys ) ) {
  777. return;
  778. }
  779. $last = array_pop( $keys );
  780. $node = &$root;
  781. foreach ( $keys as $key ) {
  782. if ( $create && ! isset( $node[ $key ] ) ) {
  783. $node[ $key ] = array();
  784. }
  785. if ( ! is_array( $node ) || ! isset( $node[ $key ] ) ) {
  786. return;
  787. }
  788. $node = &$node[ $key ];
  789. }
  790. if ( $create ) {
  791. if ( ! is_array( $node ) ) {
  792. // Account for an array overriding a string or object value.
  793. $node = array();
  794. }
  795. if ( ! isset( $node[ $last ] ) ) {
  796. $node[ $last ] = array();
  797. }
  798. }
  799. if ( ! isset( $node[ $last ] ) ) {
  800. return;
  801. }
  802. return array(
  803. 'root' => &$root,
  804. 'node' => &$node,
  805. 'key' => $last,
  806. );
  807. }
  808. /**
  809. * Will attempt to replace a specific value in a multidimensional array.
  810. *
  811. * @since 3.4.0
  812. *
  813. * @param $root
  814. * @param $keys
  815. * @param mixed $value The value to update.
  816. * @return mixed
  817. */
  818. final protected function multidimensional_replace( $root, $keys, $value ) {
  819. if ( ! isset( $value ) ) {
  820. return $root;
  821. } elseif ( empty( $keys ) ) { // If there are no keys, we're replacing the root.
  822. return $value;
  823. }
  824. $result = $this->multidimensional( $root, $keys, true );
  825. if ( isset( $result ) ) {
  826. $result['node'][ $result['key'] ] = $value;
  827. }
  828. return $root;
  829. }
  830. /**
  831. * Will attempt to fetch a specific value from a multidimensional array.
  832. *
  833. * @since 3.4.0
  834. *
  835. * @param $root
  836. * @param $keys
  837. * @param mixed $default A default value which is used as a fallback. Default is null.
  838. * @return mixed The requested value or the default value.
  839. */
  840. final protected function multidimensional_get( $root, $keys, $default = null ) {
  841. if ( empty( $keys ) ) { // If there are no keys, test the root.
  842. return isset( $root ) ? $root : $default;
  843. }
  844. $result = $this->multidimensional( $root, $keys );
  845. return isset( $result ) ? $result['node'][ $result['key'] ] : $default;
  846. }
  847. /**
  848. * Will attempt to check if a specific value in a multidimensional array is set.
  849. *
  850. * @since 3.4.0
  851. *
  852. * @param $root
  853. * @param $keys
  854. * @return bool True if value is set, false if not.
  855. */
  856. final protected function multidimensional_isset( $root, $keys ) {
  857. $result = $this->multidimensional_get( $root, $keys );
  858. return isset( $result );
  859. }
  860. }
  861. /**
  862. * WP_Customize_Filter_Setting class.
  863. */
  864. require_once ABSPATH . WPINC . '/customize/class-wp-customize-filter-setting.php';
  865. /**
  866. * WP_Customize_Header_Image_Setting class.
  867. */
  868. require_once ABSPATH . WPINC . '/customize/class-wp-customize-header-image-setting.php';
  869. /**
  870. * WP_Customize_Background_Image_Setting class.
  871. */
  872. require_once ABSPATH . WPINC . '/customize/class-wp-customize-background-image-setting.php';
  873. /**
  874. * WP_Customize_Nav_Menu_Item_Setting class.
  875. */
  876. require_once ABSPATH . WPINC . '/customize/class-wp-customize-nav-menu-item-setting.php';
  877. /**
  878. * WP_Customize_Nav_Menu_Setting class.
  879. */
  880. require_once ABSPATH . WPINC . '/customize/class-wp-customize-nav-menu-setting.php';