PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/class-wp-customize-control.php

https://github.com/shaiquddin/WordPress
PHP | 814 lines | 395 code | 91 blank | 328 comment | 26 complexity | efa3e3a5ec49c8abc2141484c44bb03f MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, LGPL-2.1
  1. <?php
  2. /**
  3. * Customize Control Class
  4. *
  5. * @package WordPress
  6. * @subpackage Customize
  7. * @since 3.4.0
  8. */
  9. class WP_Customize_Control {
  10. /**
  11. * @access public
  12. * @var WP_Customize_Manager
  13. */
  14. public $manager;
  15. /**
  16. * @access public
  17. * @var string
  18. */
  19. public $id;
  20. /**
  21. * All settings tied to the control.
  22. *
  23. * @access public
  24. * @var array
  25. */
  26. public $settings;
  27. /**
  28. * The primary setting for the control (if there is one).
  29. *
  30. * @access public
  31. * @var string
  32. */
  33. public $setting = 'default';
  34. /**
  35. * @access public
  36. * @var int
  37. */
  38. public $priority = 10;
  39. /**
  40. * @access public
  41. * @var string
  42. */
  43. public $section = '';
  44. /**
  45. * @access public
  46. * @var string
  47. */
  48. public $label = '';
  49. /**
  50. * @todo: Remove choices
  51. *
  52. * @access public
  53. * @var array
  54. */
  55. public $choices = array();
  56. /**
  57. * @access public
  58. * @var array
  59. */
  60. public $json = array();
  61. /**
  62. * @access public
  63. * @var string
  64. */
  65. public $type = 'text';
  66. /**
  67. * Constructor.
  68. *
  69. * If $args['settings'] is not defined, use the $id as the setting ID.
  70. *
  71. * @since 3.4.0
  72. *
  73. * @param WP_Customize_Manager $manager
  74. * @param string $id
  75. * @param array $args
  76. */
  77. function __construct( $manager, $id, $args = array() ) {
  78. $keys = array_keys( get_object_vars( $this ) );
  79. foreach ( $keys as $key ) {
  80. if ( isset( $args[ $key ] ) )
  81. $this->$key = $args[ $key ];
  82. }
  83. $this->manager = $manager;
  84. $this->id = $id;
  85. // Process settings.
  86. if ( empty( $this->settings ) )
  87. $this->settings = $id;
  88. $settings = array();
  89. if ( is_array( $this->settings ) ) {
  90. foreach ( $this->settings as $key => $setting ) {
  91. $settings[ $key ] = $this->manager->get_setting( $setting );
  92. }
  93. } else {
  94. $this->setting = $this->manager->get_setting( $this->settings );
  95. $settings['default'] = $this->setting;
  96. }
  97. $this->settings = $settings;
  98. }
  99. /**
  100. * Enqueue control related scripts/styles.
  101. *
  102. * @since 3.4.0
  103. */
  104. public function enqueue() {}
  105. /**
  106. * Fetch a setting's value.
  107. * Grabs the main setting by default.
  108. *
  109. * @since 3.4.0
  110. *
  111. * @param string $setting_key
  112. * @return mixed The requested setting's value, if the setting exists.
  113. */
  114. public final function value( $setting_key = 'default' ) {
  115. if ( isset( $this->settings[ $setting_key ] ) )
  116. return $this->settings[ $setting_key ]->value();
  117. }
  118. /**
  119. * Refresh the parameters passed to the JavaScript via JSON.
  120. *
  121. * @since 3.4.0
  122. */
  123. public function to_json() {
  124. $this->json['settings'] = array();
  125. foreach ( $this->settings as $key => $setting ) {
  126. $this->json['settings'][ $key ] = $setting->id;
  127. }
  128. $this->json['type'] = $this->type;
  129. }
  130. /**
  131. * Check if the theme supports the control and check user capabilities.
  132. *
  133. * @since 3.4.0
  134. *
  135. * @return bool False if theme doesn't support the control or user doesn't have the required permissions, otherwise true.
  136. */
  137. public final function check_capabilities() {
  138. foreach ( $this->settings as $setting ) {
  139. if ( ! $setting->check_capabilities() )
  140. return false;
  141. }
  142. $section = $this->manager->get_section( $this->section );
  143. if ( isset( $section ) && ! $section->check_capabilities() )
  144. return false;
  145. return true;
  146. }
  147. /**
  148. * Check capabilities and render the control.
  149. *
  150. * @since 3.4.0
  151. * @uses WP_Customize_Control::render()
  152. */
  153. public final function maybe_render() {
  154. if ( ! $this->check_capabilities() )
  155. return;
  156. do_action( 'customize_render_control', $this );
  157. do_action( 'customize_render_control_' . $this->id, $this );
  158. $this->render();
  159. }
  160. /**
  161. * Render the control. Renders the control wrapper, then calls $this->render_content().
  162. *
  163. * @since 3.4.0
  164. */
  165. protected function render() {
  166. $id = 'customize-control-' . str_replace( '[', '-', str_replace( ']', '', $this->id ) );
  167. $class = 'customize-control customize-control-' . $this->type;
  168. ?><li id="<?php echo esc_attr( $id ); ?>" class="<?php echo esc_attr( $class ); ?>">
  169. <?php $this->render_content(); ?>
  170. </li><?php
  171. }
  172. /**
  173. * Get the data link parameter for a setting.
  174. *
  175. * @since 3.4.0
  176. *
  177. * @param string $setting_key
  178. * @return string Data link parameter, if $setting_key is a valid setting, empty string otherwise.
  179. */
  180. public function get_link( $setting_key = 'default' ) {
  181. if ( ! isset( $this->settings[ $setting_key ] ) )
  182. return '';
  183. return 'data-customize-setting-link="' . esc_attr( $this->settings[ $setting_key ]->id ) . '"';
  184. }
  185. /**
  186. * Render the data link parameter for a setting
  187. *
  188. * @since 3.4.0
  189. * @uses WP_Customize_Control::get_link()
  190. *
  191. * @param string $setting_key
  192. */
  193. public function link( $setting_key = 'default' ) {
  194. echo $this->get_link( $setting_key );
  195. }
  196. /**
  197. * Render the control's content.
  198. *
  199. * Allows the content to be overriden without having to rewrite the wrapper.
  200. *
  201. * @since 3.4.0
  202. */
  203. protected function render_content() {
  204. switch( $this->type ) {
  205. case 'text':
  206. ?>
  207. <label>
  208. <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
  209. <input type="text" value="<?php echo esc_attr( $this->value() ); ?>" <?php $this->link(); ?> />
  210. </label>
  211. <?php
  212. break;
  213. case 'checkbox':
  214. ?>
  215. <label>
  216. <input type="checkbox" value="<?php echo esc_attr( $this->value() ); ?>" <?php $this->link(); checked( $this->value() ); ?> />
  217. <?php echo esc_html( $this->label ); ?>
  218. </label>
  219. <?php
  220. break;
  221. case 'radio':
  222. if ( empty( $this->choices ) )
  223. return;
  224. $name = '_customize-radio-' . $this->id;
  225. ?>
  226. <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
  227. <?php
  228. foreach ( $this->choices as $value => $label ) :
  229. ?>
  230. <label>
  231. <input type="radio" value="<?php echo esc_attr( $value ); ?>" name="<?php echo esc_attr( $name ); ?>" <?php $this->link(); checked( $this->value(), $value ); ?> />
  232. <?php echo esc_html( $label ); ?><br/>
  233. </label>
  234. <?php
  235. endforeach;
  236. break;
  237. case 'select':
  238. if ( empty( $this->choices ) )
  239. return;
  240. ?>
  241. <label>
  242. <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
  243. <select <?php $this->link(); ?>>
  244. <?php
  245. foreach ( $this->choices as $value => $label )
  246. echo '<option value="' . esc_attr( $value ) . '"' . selected( $this->value(), $value, false ) . '>' . $label . '</option>';
  247. ?>
  248. </select>
  249. </label>
  250. <?php
  251. break;
  252. case 'dropdown-pages':
  253. $dropdown = wp_dropdown_pages(
  254. array(
  255. 'name' => '_customize-dropdown-pages-' . $this->id,
  256. 'echo' => 0,
  257. 'show_option_none' => __( '&mdash; Select &mdash;' ),
  258. 'option_none_value' => '0',
  259. 'selected' => $this->value(),
  260. )
  261. );
  262. // Hackily add in the data link parameter.
  263. $dropdown = str_replace( '<select', '<select ' . $this->get_link(), $dropdown );
  264. printf(
  265. '<label class="customize-control-select"><span class="customize-control-title">%s</span> %s</label>',
  266. $this->label,
  267. $dropdown
  268. );
  269. break;
  270. }
  271. }
  272. }
  273. /**
  274. * Customize Color Control Class
  275. *
  276. * @package WordPress
  277. * @subpackage Customize
  278. * @since 3.4.0
  279. */
  280. class WP_Customize_Color_Control extends WP_Customize_Control {
  281. /**
  282. * @access public
  283. * @var string
  284. */
  285. public $type = 'color';
  286. /**
  287. * @access public
  288. * @var array
  289. */
  290. public $statuses;
  291. /**
  292. * Constructor.
  293. *
  294. * If $args['settings'] is not defined, use the $id as the setting ID.
  295. *
  296. * @since 3.4.0
  297. * @uses WP_Customize_Control::__construct()
  298. *
  299. * @param WP_Customize_Manager $manager
  300. * @param string $id
  301. * @param array $args
  302. */
  303. public function __construct( $manager, $id, $args = array() ) {
  304. $this->statuses = array( '' => __('Default') );
  305. parent::__construct( $manager, $id, $args );
  306. }
  307. /**
  308. * Enqueue control related scripts/styles.
  309. *
  310. * @since 3.4.0
  311. */
  312. public function enqueue() {
  313. wp_enqueue_script( 'wp-color-picker' );
  314. wp_enqueue_style( 'wp-color-picker' );
  315. }
  316. /**
  317. * Refresh the parameters passed to the JavaScript via JSON.
  318. *
  319. * @since 3.4.0
  320. * @uses WP_Customize_Control::to_json()
  321. */
  322. public function to_json() {
  323. parent::to_json();
  324. $this->json['statuses'] = $this->statuses;
  325. }
  326. /**
  327. * Render the control's content.
  328. *
  329. * @since 3.4.0
  330. */
  331. public function render_content() {
  332. $this_default = $this->setting->default;
  333. $default_attr = '';
  334. if ( $this_default ) {
  335. if ( false === strpos( $this_default, '#' ) )
  336. $this_default = '#' . $this_default;
  337. $default_attr = ' data-default-color="' . esc_attr( $this_default ) . '"';
  338. }
  339. // The input's value gets set by JS. Don't fill it.
  340. ?>
  341. <label>
  342. <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
  343. <div class="customize-control-content">
  344. <input class="color-picker-hex" type="text" maxlength="7" placeholder="<?php esc_attr_e( 'Hex Value' ); ?>"<?php echo $default_attr ?> />
  345. </div>
  346. </label>
  347. <?php
  348. }
  349. }
  350. /**
  351. * Customize Upload Control Class
  352. *
  353. * @package WordPress
  354. * @subpackage Customize
  355. * @since 3.4.0
  356. */
  357. class WP_Customize_Upload_Control extends WP_Customize_Control {
  358. public $type = 'upload';
  359. public $removed = '';
  360. public $context;
  361. /**
  362. * Enqueue control related scripts/styles.
  363. *
  364. * @since 3.4.0
  365. */
  366. public function enqueue() {
  367. wp_enqueue_script( 'wp-plupload' );
  368. }
  369. /**
  370. * Refresh the parameters passed to the JavaScript via JSON.
  371. *
  372. * @since 3.4.0
  373. * @uses WP_Customize_Control::to_json()
  374. */
  375. public function to_json() {
  376. parent::to_json();
  377. $this->json['removed'] = $this->removed;
  378. if ( $this->context )
  379. $this->json['context'] = $this->context;
  380. }
  381. /**
  382. * Render the control's content.
  383. *
  384. * @since 3.4.0
  385. */
  386. public function render_content() {
  387. ?>
  388. <label>
  389. <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
  390. <div>
  391. <a href="#" class="button-secondary upload"><?php _e( 'Upload' ); ?></a>
  392. <a href="#" class="remove"><?php _e( 'Remove' ); ?></a>
  393. </div>
  394. </label>
  395. <?php
  396. }
  397. }
  398. /**
  399. * Customize Image Control Class
  400. *
  401. * @package WordPress
  402. * @subpackage Customize
  403. * @since 3.4.0
  404. */
  405. class WP_Customize_Image_Control extends WP_Customize_Upload_Control {
  406. public $type = 'image';
  407. public $get_url;
  408. public $statuses;
  409. protected $tabs = array();
  410. /**
  411. * Constructor.
  412. *
  413. * If $args['settings'] is not defined, use the $id as the setting ID.
  414. *
  415. * @since 3.4.0
  416. * @uses WP_Customize_Upload_Control::__construct()
  417. *
  418. * @param WP_Customize_Manager $manager
  419. * @param string $id
  420. * @param array $args
  421. */
  422. public function __construct( $manager, $id, $args ) {
  423. $this->statuses = array( '' => __('No Image') );
  424. parent::__construct( $manager, $id, $args );
  425. $this->add_tab( 'upload-new', __('Upload New'), array( $this, 'tab_upload_new' ) );
  426. $this->add_tab( 'uploaded', __('Uploaded'), array( $this, 'tab_uploaded' ) );
  427. // Early priority to occur before $this->manager->prepare_controls();
  428. add_action( 'customize_controls_init', array( $this, 'prepare_control' ), 5 );
  429. }
  430. /**
  431. * Prepares the control.
  432. *
  433. * If no tabs exist, removes the control from the manager.
  434. *
  435. * @since 3.4.2
  436. */
  437. public function prepare_control() {
  438. if ( ! $this->tabs )
  439. $this->manager->remove_control( $this->id );
  440. }
  441. /**
  442. * Refresh the parameters passed to the JavaScript via JSON.
  443. *
  444. * @since 3.4.0
  445. * @uses WP_Customize_Upload_Control::to_json()
  446. */
  447. public function to_json() {
  448. parent::to_json();
  449. $this->json['statuses'] = $this->statuses;
  450. }
  451. /**
  452. * Render the control's content.
  453. *
  454. * @since 3.4.0
  455. */
  456. public function render_content() {
  457. $src = $this->value();
  458. if ( isset( $this->get_url ) )
  459. $src = call_user_func( $this->get_url, $src );
  460. ?>
  461. <div class="customize-image-picker">
  462. <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
  463. <div class="customize-control-content">
  464. <div class="dropdown preview-thumbnail">
  465. <div class="dropdown-content">
  466. <?php if ( empty( $src ) ): ?>
  467. <img style="display:none;" />
  468. <?php else: ?>
  469. <img src="<?php echo esc_url( set_url_scheme( $src ) ); ?>" />
  470. <?php endif; ?>
  471. <div class="dropdown-status"></div>
  472. </div>
  473. <div class="dropdown-arrow"></div>
  474. </div>
  475. </div>
  476. <div class="library">
  477. <ul>
  478. <?php foreach ( $this->tabs as $id => $tab ): ?>
  479. <li data-customize-tab='<?php echo esc_attr( $id ); ?>'>
  480. <?php echo esc_html( $tab['label'] ); ?>
  481. </li>
  482. <?php endforeach; ?>
  483. </ul>
  484. <?php foreach ( $this->tabs as $id => $tab ): ?>
  485. <div class="library-content" data-customize-tab='<?php echo esc_attr( $id ); ?>'>
  486. <?php call_user_func( $tab['callback'] ); ?>
  487. </div>
  488. <?php endforeach; ?>
  489. </div>
  490. <div class="actions">
  491. <a href="#" class="remove"><?php _e( 'Remove Image' ); ?></a>
  492. </div>
  493. </div>
  494. <?php
  495. }
  496. /**
  497. * Add a tab to the control.
  498. *
  499. * @since 3.4.0
  500. *
  501. * @param string $id
  502. * @param string $label
  503. * @param mixed $callback
  504. */
  505. public function add_tab( $id, $label, $callback ) {
  506. $this->tabs[ $id ] = array(
  507. 'label' => $label,
  508. 'callback' => $callback,
  509. );
  510. }
  511. /**
  512. * Remove a tab from the control.
  513. *
  514. * @since 3.4.0
  515. *
  516. * @param string $id
  517. */
  518. public function remove_tab( $id ) {
  519. unset( $this->tabs[ $id ] );
  520. }
  521. /**
  522. * @since 3.4.0
  523. */
  524. public function tab_upload_new() {
  525. if ( ! _device_can_upload() ) {
  526. ?>
  527. <p><?php _e('The web browser on your device cannot be used to upload files. You may be able to use the <a href="http://wordpress.org/extend/mobile/">native app for your device</a> instead.'); ?></p>
  528. <?php
  529. } else {
  530. ?>
  531. <div class="upload-dropzone">
  532. <?php _e('Drop a file here or <a href="#" class="upload">select a file</a>.'); ?>
  533. </div>
  534. <div class="upload-fallback">
  535. <span class="button-secondary"><?php _e('Select File'); ?></span>
  536. </div>
  537. <?php
  538. }
  539. }
  540. /**
  541. * @since 3.4.0
  542. */
  543. public function tab_uploaded() {
  544. ?>
  545. <div class="uploaded-target"></div>
  546. <?php
  547. }
  548. /**
  549. * @since 3.4.0
  550. *
  551. * @param string $url
  552. * @param string $thumbnail_url
  553. */
  554. public function print_tab_image( $url, $thumbnail_url = null ) {
  555. $url = set_url_scheme( $url );
  556. $thumbnail_url = ( $thumbnail_url ) ? set_url_scheme( $thumbnail_url ) : $url;
  557. ?>
  558. <a href="#" class="thumbnail" data-customize-image-value="<?php echo esc_url( $url ); ?>">
  559. <img src="<?php echo esc_url( $thumbnail_url ); ?>" />
  560. </a>
  561. <?php
  562. }
  563. }
  564. /**
  565. * Customize Background Image Control Class
  566. *
  567. * @package WordPress
  568. * @subpackage Customize
  569. * @since 3.4.0
  570. */
  571. class WP_Customize_Background_Image_Control extends WP_Customize_Image_Control {
  572. /**
  573. * Constructor.
  574. *
  575. * @since 3.4.0
  576. * @uses WP_Customize_Image_Control::__construct()
  577. *
  578. * @param WP_Customize_Manager $manager
  579. */
  580. public function __construct( $manager ) {
  581. parent::__construct( $manager, 'background_image', array(
  582. 'label' => __( 'Background Image' ),
  583. 'section' => 'background_image',
  584. 'context' => 'custom-background',
  585. 'get_url' => 'get_background_image',
  586. ) );
  587. if ( $this->setting->default )
  588. $this->add_tab( 'default', __('Default'), array( $this, 'tab_default_background' ) );
  589. }
  590. /**
  591. * @since 3.4.0
  592. */
  593. public function tab_uploaded() {
  594. $backgrounds = get_posts( array(
  595. 'post_type' => 'attachment',
  596. 'meta_key' => '_wp_attachment_is_custom_background',
  597. 'meta_value' => $this->manager->get_stylesheet(),
  598. 'orderby' => 'none',
  599. 'nopaging' => true,
  600. ) );
  601. ?><div class="uploaded-target"></div><?php
  602. if ( empty( $backgrounds ) )
  603. return;
  604. foreach ( (array) $backgrounds as $background )
  605. $this->print_tab_image( esc_url_raw( $background->guid ) );
  606. }
  607. /**
  608. * @since 3.4.0
  609. * @uses WP_Customize_Image_Control::print_tab_image()
  610. */
  611. public function tab_default_background() {
  612. $this->print_tab_image( $this->setting->default );
  613. }
  614. }
  615. /**
  616. * Customize Header Image Control Class
  617. *
  618. * @package WordPress
  619. * @subpackage Customize
  620. * @since 3.4.0
  621. */
  622. class WP_Customize_Header_Image_Control extends WP_Customize_Image_Control {
  623. /**
  624. * The processed default headers.
  625. * @since 3.4.2
  626. * @var array
  627. */
  628. protected $default_headers;
  629. /**
  630. * The uploaded headers.
  631. * @since 3.4.2
  632. * @var array
  633. */
  634. protected $uploaded_headers;
  635. /**
  636. * Constructor.
  637. *
  638. * @since 3.4.0
  639. * @uses WP_Customize_Image_Control::__construct()
  640. * @uses WP_Customize_Image_Control::add_tab()
  641. *
  642. * @param WP_Customize_Manager $manager
  643. */
  644. public function __construct( $manager ) {
  645. parent::__construct( $manager, 'header_image', array(
  646. 'label' => __( 'Header Image' ),
  647. 'settings' => array(
  648. 'default' => 'header_image',
  649. 'data' => 'header_image_data',
  650. ),
  651. 'section' => 'header_image',
  652. 'context' => 'custom-header',
  653. 'removed' => 'remove-header',
  654. 'get_url' => 'get_header_image',
  655. 'statuses' => array(
  656. '' => __('Default'),
  657. 'remove-header' => __('No Image'),
  658. 'random-default-image' => __('Random Default Image'),
  659. 'random-uploaded-image' => __('Random Uploaded Image'),
  660. )
  661. ) );
  662. // Remove the upload tab.
  663. $this->remove_tab( 'upload-new' );
  664. }
  665. /**
  666. * Prepares the control.
  667. *
  668. * If no tabs exist, removes the control from the manager.
  669. *
  670. * @since 3.4.2
  671. */
  672. public function prepare_control() {
  673. global $custom_image_header;
  674. if ( empty( $custom_image_header ) )
  675. return parent::prepare_control();
  676. // Process default headers and uploaded headers.
  677. $custom_image_header->process_default_headers();
  678. $this->default_headers = $custom_image_header->default_headers;
  679. $this->uploaded_headers = get_uploaded_header_images();
  680. if ( $this->default_headers )
  681. $this->add_tab( 'default', __('Default'), array( $this, 'tab_default_headers' ) );
  682. if ( ! $this->uploaded_headers )
  683. $this->remove_tab( 'uploaded' );
  684. return parent::prepare_control();
  685. }
  686. /**
  687. * @since 3.4.0
  688. *
  689. * @param mixed $choice Which header image to select. (@see Custom_Image_Header::get_header_image() )
  690. * @param array $header
  691. */
  692. public function print_header_image( $choice, $header ) {
  693. $header['url'] = set_url_scheme( $header['url'] );
  694. $header['thumbnail_url'] = set_url_scheme( $header['thumbnail_url'] );
  695. $header_image_data = array( 'choice' => $choice );
  696. foreach ( array( 'attachment_id', 'width', 'height', 'url', 'thumbnail_url' ) as $key ) {
  697. if ( isset( $header[ $key ] ) )
  698. $header_image_data[ $key ] = $header[ $key ];
  699. }
  700. ?>
  701. <a href="#" class="thumbnail"
  702. data-customize-image-value="<?php echo esc_url( $header['url'] ); ?>"
  703. data-customize-header-image-data="<?php echo esc_attr( json_encode( $header_image_data ) ); ?>">
  704. <img src="<?php echo esc_url( $header['thumbnail_url'] ); ?>" />
  705. </a>
  706. <?php
  707. }
  708. /**
  709. * @since 3.4.0
  710. */
  711. public function tab_uploaded() {
  712. ?><div class="uploaded-target"></div><?php
  713. foreach ( $this->uploaded_headers as $choice => $header )
  714. $this->print_header_image( $choice, $header );
  715. }
  716. /**
  717. * @since 3.4.0
  718. */
  719. public function tab_default_headers() {
  720. foreach ( $this->default_headers as $choice => $header )
  721. $this->print_header_image( $choice, $header );
  722. }
  723. }