您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 

922 行
27 KiB

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