PageRenderTime 41ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/shop quần áo starloveshop.com/wp-includes/class-wp-customize-setting.php

https://gitlab.com/phamngsinh/baitaplon_sinhvien
PHP | 565 lines | 194 code | 70 blank | 301 comment | 37 complexity | ba405b162e4309a263d8f248a05861fb MD5 | raw file
  1. <?php
  2. /**
  3. * Customize Setting Class.
  4. *
  5. * Handles saving and sanitizing of settings.
  6. *
  7. * @package WordPress
  8. * @subpackage Customize
  9. * @since 3.4.0
  10. */
  11. class WP_Customize_Setting {
  12. /**
  13. * @access public
  14. * @var WP_Customize_Manager
  15. */
  16. public $manager;
  17. /**
  18. * @access public
  19. * @var string
  20. */
  21. public $id;
  22. /**
  23. * @access public
  24. * @var string
  25. */
  26. public $type = 'theme_mod';
  27. /**
  28. * Capability required to edit this setting.
  29. *
  30. * @var string
  31. */
  32. public $capability = 'edit_theme_options';
  33. /**
  34. * Feature a theme is required to support to enable this setting.
  35. *
  36. * @access public
  37. * @var string
  38. */
  39. public $theme_supports = '';
  40. public $default = '';
  41. public $transport = 'refresh';
  42. /**
  43. * Server-side sanitization callback for the setting's value.
  44. *
  45. * @var callback
  46. */
  47. public $sanitize_callback = '';
  48. public $sanitize_js_callback = '';
  49. protected $id_data = array();
  50. /**
  51. * Cached and sanitized $_POST value for the setting.
  52. *
  53. * @access private
  54. * @var mixed
  55. */
  56. private $_post_value;
  57. /**
  58. * Constructor.
  59. *
  60. * Any supplied $args override class property defaults.
  61. *
  62. * @since 3.4.0
  63. *
  64. * @param WP_Customize_Manager $manager
  65. * @param string $id An specific ID of the setting. Can be a
  66. * theme mod or option name.
  67. * @param array $args Setting arguments.
  68. * @return WP_Customize_Setting $setting
  69. */
  70. public function __construct( $manager, $id, $args = array() ) {
  71. $keys = array_keys( get_object_vars( $this ) );
  72. foreach ( $keys as $key ) {
  73. if ( isset( $args[ $key ] ) )
  74. $this->$key = $args[ $key ];
  75. }
  76. $this->manager = $manager;
  77. $this->id = $id;
  78. // Parse the ID for array keys.
  79. $this->id_data[ 'keys' ] = preg_split( '/\[/', str_replace( ']', '', $this->id ) );
  80. $this->id_data[ 'base' ] = array_shift( $this->id_data[ 'keys' ] );
  81. // Rebuild the ID.
  82. $this->id = $this->id_data[ 'base' ];
  83. if ( ! empty( $this->id_data[ 'keys' ] ) )
  84. $this->id .= '[' . implode( '][', $this->id_data[ 'keys' ] ) . ']';
  85. if ( $this->sanitize_callback )
  86. add_filter( "customize_sanitize_{$this->id}", $this->sanitize_callback, 10, 2 );
  87. if ( $this->sanitize_js_callback )
  88. add_filter( "customize_sanitize_js_{$this->id}", $this->sanitize_js_callback, 10, 2 );
  89. return $this;
  90. }
  91. /**
  92. * Handle previewing the setting.
  93. *
  94. * @since 3.4.0
  95. */
  96. public function preview() {
  97. switch( $this->type ) {
  98. case 'theme_mod' :
  99. add_filter( 'theme_mod_' . $this->id_data[ 'base' ], array( $this, '_preview_filter' ) );
  100. break;
  101. case 'option' :
  102. if ( empty( $this->id_data[ 'keys' ] ) )
  103. add_filter( 'pre_option_' . $this->id_data[ 'base' ], array( $this, '_preview_filter' ) );
  104. else {
  105. add_filter( 'option_' . $this->id_data[ 'base' ], array( $this, '_preview_filter' ) );
  106. add_filter( 'default_option_' . $this->id_data[ 'base' ], array( $this, '_preview_filter' ) );
  107. }
  108. break;
  109. default :
  110. /**
  111. * Fires when the {@see WP_Customize_Setting::preview()} method is called for settings
  112. * not handled as theme_mods or options.
  113. *
  114. * The dynamic portion of the hook name, `$this->id`, refers to the setting ID.
  115. *
  116. * @since 3.4.0
  117. *
  118. * @param WP_Customize_Setting $this {@see WP_Customize_Setting} instance.
  119. */
  120. do_action( "customize_preview_{$this->id}", $this );
  121. /**
  122. * Fires when the {@see WP_Customize_Setting::preview()} method is called for settings
  123. * not handled as theme_mods or options.
  124. *
  125. * The dynamic portion of the hook name, `$this->type`, refers to the setting type.
  126. *
  127. * @since 4.1.0
  128. *
  129. * @param WP_Customize_Setting $this {@see WP_Customize_Setting} instance.
  130. */
  131. do_action( "customize_preview_{$this->type}", $this );
  132. }
  133. }
  134. /**
  135. * Callback function to filter the theme mods and options.
  136. *
  137. * @since 3.4.0
  138. * @uses WP_Customize_Setting::multidimensional_replace()
  139. *
  140. * @param mixed $original Old value.
  141. * @return mixed New or old value.
  142. */
  143. public function _preview_filter( $original ) {
  144. return $this->multidimensional_replace( $original, $this->id_data[ 'keys' ], $this->post_value() );
  145. }
  146. /**
  147. * Check user capabilities and theme supports, and then save
  148. * the value of the setting.
  149. *
  150. * @since 3.4.0
  151. *
  152. * @return false|null False if cap check fails or value isn't set.
  153. */
  154. public final function save() {
  155. $value = $this->post_value();
  156. if ( ! $this->check_capabilities() || ! isset( $value ) )
  157. return false;
  158. /**
  159. * Fires when the WP_Customize_Setting::save() method is called.
  160. *
  161. * The dynamic portion of the hook name, `$this->id_data['base']` refers to
  162. * the base slug of the setting name.
  163. *
  164. * @since 3.4.0
  165. *
  166. * @param WP_Customize_Setting $this {@see WP_Customize_Setting} instance.
  167. */
  168. do_action( 'customize_save_' . $this->id_data[ 'base' ], $this );
  169. $this->update( $value );
  170. }
  171. /**
  172. * Fetch and sanitize the $_POST value for the setting.
  173. *
  174. * @since 3.4.0
  175. *
  176. * @param mixed $default A default value which is used as a fallback. Default is null.
  177. * @return mixed The default value on failure, otherwise the sanitized value.
  178. */
  179. public final function post_value( $default = null ) {
  180. // Check for a cached value
  181. if ( isset( $this->_post_value ) )
  182. return $this->_post_value;
  183. // Call the manager for the post value
  184. $result = $this->manager->post_value( $this );
  185. if ( isset( $result ) )
  186. return $this->_post_value = $result;
  187. else
  188. return $default;
  189. }
  190. /**
  191. * Sanitize an input.
  192. *
  193. * @since 3.4.0
  194. *
  195. * @param mixed $value The value to sanitize.
  196. * @return mixed Null if an input isn't valid, otherwise the sanitized value.
  197. */
  198. public function sanitize( $value ) {
  199. $value = wp_unslash( $value );
  200. /**
  201. * Filter a Customize setting value in un-slashed form.
  202. *
  203. * @since 3.4.0
  204. *
  205. * @param mixed $value Value of the setting.
  206. * @param WP_Customize_Setting $this WP_Customize_Setting instance.
  207. */
  208. return apply_filters( "customize_sanitize_{$this->id}", $value, $this );
  209. }
  210. /**
  211. * Save the value of the setting, using the related API.
  212. *
  213. * @since 3.4.0
  214. *
  215. * @param mixed $value The value to update.
  216. * @return mixed The result of saving the value.
  217. */
  218. protected function update( $value ) {
  219. switch( $this->type ) {
  220. case 'theme_mod' :
  221. return $this->_update_theme_mod( $value );
  222. case 'option' :
  223. return $this->_update_option( $value );
  224. default :
  225. /**
  226. * Fires when the {@see WP_Customize_Setting::update()} method is called for settings
  227. * not handled as theme_mods or options.
  228. *
  229. * The dynamic portion of the hook name, `$this->type`, refers to the type of setting.
  230. *
  231. * @since 3.4.0
  232. *
  233. * @param mixed $value Value of the setting.
  234. * @param WP_Customize_Setting $this WP_Customize_Setting instance.
  235. */
  236. return do_action( 'customize_update_' . $this->type, $value, $this );
  237. }
  238. }
  239. /**
  240. * Update the theme mod from the value of the parameter.
  241. *
  242. * @since 3.4.0
  243. *
  244. * @param mixed $value The value to update.
  245. * @return mixed The result of saving the value.
  246. */
  247. protected function _update_theme_mod( $value ) {
  248. // Handle non-array theme mod.
  249. if ( empty( $this->id_data[ 'keys' ] ) )
  250. return set_theme_mod( $this->id_data[ 'base' ], $value );
  251. // Handle array-based theme mod.
  252. $mods = get_theme_mod( $this->id_data[ 'base' ] );
  253. $mods = $this->multidimensional_replace( $mods, $this->id_data[ 'keys' ], $value );
  254. if ( isset( $mods ) )
  255. return set_theme_mod( $this->id_data[ 'base' ], $mods );
  256. }
  257. /**
  258. * Update the option from the value of the setting.
  259. *
  260. * @since 3.4.0
  261. *
  262. * @param mixed $value The value to update.
  263. * @return bool|null The result of saving the value.
  264. */
  265. protected function _update_option( $value ) {
  266. // Handle non-array option.
  267. if ( empty( $this->id_data[ 'keys' ] ) )
  268. return update_option( $this->id_data[ 'base' ], $value );
  269. // Handle array-based options.
  270. $options = get_option( $this->id_data[ 'base' ] );
  271. $options = $this->multidimensional_replace( $options, $this->id_data[ 'keys' ], $value );
  272. if ( isset( $options ) )
  273. return update_option( $this->id_data[ 'base' ], $options );
  274. }
  275. /**
  276. * Fetch the value of the setting.
  277. *
  278. * @since 3.4.0
  279. *
  280. * @return mixed The value.
  281. */
  282. public function value() {
  283. // Get the callback that corresponds to the setting type.
  284. switch( $this->type ) {
  285. case 'theme_mod' :
  286. $function = 'get_theme_mod';
  287. break;
  288. case 'option' :
  289. $function = 'get_option';
  290. break;
  291. default :
  292. /**
  293. * Filter a Customize setting value not handled as a theme_mod or option.
  294. *
  295. * The dynamic portion of the hook name, `$this->id_date['base']`, refers to
  296. * the base slug of the setting name.
  297. *
  298. * For settings handled as theme_mods or options, see those corresponding
  299. * functions for available hooks.
  300. *
  301. * @since 3.4.0
  302. *
  303. * @param mixed $default The setting default value. Default empty.
  304. */
  305. return apply_filters( 'customize_value_' . $this->id_data[ 'base' ], $this->default );
  306. }
  307. // Handle non-array value
  308. if ( empty( $this->id_data[ 'keys' ] ) )
  309. return $function( $this->id_data[ 'base' ], $this->default );
  310. // Handle array-based value
  311. $values = $function( $this->id_data[ 'base' ] );
  312. return $this->multidimensional_get( $values, $this->id_data[ 'keys' ], $this->default );
  313. }
  314. /**
  315. * Sanitize the setting's value for use in JavaScript.
  316. *
  317. * @since 3.4.0
  318. *
  319. * @return mixed The requested escaped value.
  320. */
  321. public function js_value() {
  322. /**
  323. * Filter a Customize setting value for use in JavaScript.
  324. *
  325. * The dynamic portion of the hook name, `$this->id`, refers to the setting ID.
  326. *
  327. * @since 3.4.0
  328. *
  329. * @param mixed $value The setting value.
  330. * @param WP_Customize_Setting $this {@see WP_Customize_Setting} instance.
  331. */
  332. $value = apply_filters( "customize_sanitize_js_{$this->id}", $this->value(), $this );
  333. if ( is_string( $value ) )
  334. return html_entity_decode( $value, ENT_QUOTES, 'UTF-8');
  335. return $value;
  336. }
  337. /**
  338. * Validate user capabilities whether the theme supports the setting.
  339. *
  340. * @since 3.4.0
  341. *
  342. * @return bool False if theme doesn't support the setting or user can't change setting, otherwise true.
  343. */
  344. public final function check_capabilities() {
  345. if ( $this->capability && ! call_user_func_array( 'current_user_can', (array) $this->capability ) )
  346. return false;
  347. if ( $this->theme_supports && ! call_user_func_array( 'current_theme_supports', (array) $this->theme_supports ) )
  348. return false;
  349. return true;
  350. }
  351. /**
  352. * Multidimensional helper function.
  353. *
  354. * @since 3.4.0
  355. *
  356. * @param $root
  357. * @param $keys
  358. * @param bool $create Default is false.
  359. * @return null|array Keys are 'root', 'node', and 'key'.
  360. */
  361. final protected function multidimensional( &$root, $keys, $create = false ) {
  362. if ( $create && empty( $root ) )
  363. $root = array();
  364. if ( ! isset( $root ) || empty( $keys ) )
  365. return;
  366. $last = array_pop( $keys );
  367. $node = &$root;
  368. foreach ( $keys as $key ) {
  369. if ( $create && ! isset( $node[ $key ] ) )
  370. $node[ $key ] = array();
  371. if ( ! is_array( $node ) || ! isset( $node[ $key ] ) )
  372. return;
  373. $node = &$node[ $key ];
  374. }
  375. if ( $create && ! isset( $node[ $last ] ) )
  376. $node[ $last ] = array();
  377. if ( ! isset( $node[ $last ] ) )
  378. return;
  379. return array(
  380. 'root' => &$root,
  381. 'node' => &$node,
  382. 'key' => $last,
  383. );
  384. }
  385. /**
  386. * Will attempt to replace a specific value in a multidimensional array.
  387. *
  388. * @since 3.4.0
  389. *
  390. * @param $root
  391. * @param $keys
  392. * @param mixed $value The value to update.
  393. * @return
  394. */
  395. final protected function multidimensional_replace( $root, $keys, $value ) {
  396. if ( ! isset( $value ) )
  397. return $root;
  398. elseif ( empty( $keys ) ) // If there are no keys, we're replacing the root.
  399. return $value;
  400. $result = $this->multidimensional( $root, $keys, true );
  401. if ( isset( $result ) )
  402. $result['node'][ $result['key'] ] = $value;
  403. return $root;
  404. }
  405. /**
  406. * Will attempt to fetch a specific value from a multidimensional array.
  407. *
  408. * @since 3.4.0
  409. *
  410. * @param $root
  411. * @param $keys
  412. * @param mixed $default A default value which is used as a fallback. Default is null.
  413. * @return mixed The requested value or the default value.
  414. */
  415. final protected function multidimensional_get( $root, $keys, $default = null ) {
  416. if ( empty( $keys ) ) // If there are no keys, test the root.
  417. return isset( $root ) ? $root : $default;
  418. $result = $this->multidimensional( $root, $keys );
  419. return isset( $result ) ? $result['node'][ $result['key'] ] : $default;
  420. }
  421. /**
  422. * Will attempt to check if a specific value in a multidimensional array is set.
  423. *
  424. * @since 3.4.0
  425. *
  426. * @param $root
  427. * @param $keys
  428. * @return bool True if value is set, false if not.
  429. */
  430. final protected function multidimensional_isset( $root, $keys ) {
  431. $result = $this->multidimensional_get( $root, $keys );
  432. return isset( $result );
  433. }
  434. }
  435. /**
  436. * A setting that is used to filter a value, but will not save the results.
  437. *
  438. * Results should be properly handled using another setting or callback.
  439. *
  440. * @package WordPress
  441. * @subpackage Customize
  442. * @since 3.4.0
  443. */
  444. class WP_Customize_Filter_Setting extends WP_Customize_Setting {
  445. /**
  446. * @since 3.4.0
  447. */
  448. public function update( $value ) {}
  449. }
  450. /**
  451. * A setting that is used to filter a value, but will not save the results.
  452. *
  453. * Results should be properly handled using another setting or callback.
  454. *
  455. * @package WordPress
  456. * @subpackage Customize
  457. * @since 3.4.0
  458. */
  459. final class WP_Customize_Header_Image_Setting extends WP_Customize_Setting {
  460. public $id = 'header_image_data';
  461. /**
  462. * @since 3.4.0
  463. *
  464. * @param $value
  465. */
  466. public function update( $value ) {
  467. global $custom_image_header;
  468. // If the value doesn't exist (removed or random),
  469. // use the header_image value.
  470. if ( ! $value )
  471. $value = $this->manager->get_setting('header_image')->post_value();
  472. if ( is_array( $value ) && isset( $value['choice'] ) )
  473. $custom_image_header->set_header_image( $value['choice'] );
  474. else
  475. $custom_image_header->set_header_image( $value );
  476. }
  477. }
  478. /**
  479. * Class WP_Customize_Background_Image_Setting
  480. *
  481. * @package WordPress
  482. * @subpackage Customize
  483. * @since 3.4.0
  484. */
  485. final class WP_Customize_Background_Image_Setting extends WP_Customize_Setting {
  486. public $id = 'background_image_thumb';
  487. /**
  488. * @since 3.4.0
  489. *
  490. * @param $value
  491. */
  492. public function update( $value ) {
  493. remove_theme_mod( 'background_image_thumb' );
  494. }
  495. }