class-wp-customize-setting.php 28 KB

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