PageRenderTime 54ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/crafttheweb/wordpress-fold
PHP | 813 lines | 395 code | 91 blank | 327 comment | 24 complexity | 68e0d5a4d16b4f62361c1920366457f5 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( 'farbtastic' );
  314. wp_enqueue_style( 'farbtastic' );
  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. ?>
  333. <label>
  334. <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
  335. <div class="customize-control-content">
  336. <div class="dropdown">
  337. <div class="dropdown-content">
  338. <div class="dropdown-status"></div>
  339. </div>
  340. <div class="dropdown-arrow"></div>
  341. </div>
  342. <input class="color-picker-hex" type="text" maxlength="7" placeholder="<?php esc_attr_e('Hex Value'); ?>" />
  343. </div>
  344. <div class="farbtastic-placeholder"></div>
  345. </label>
  346. <?php
  347. }
  348. }
  349. /**
  350. * Customize Upload Control Class
  351. *
  352. * @package WordPress
  353. * @subpackage Customize
  354. * @since 3.4.0
  355. */
  356. class WP_Customize_Upload_Control extends WP_Customize_Control {
  357. public $type = 'upload';
  358. public $removed = '';
  359. public $context;
  360. /**
  361. * Enqueue control related scripts/styles.
  362. *
  363. * @since 3.4.0
  364. */
  365. public function enqueue() {
  366. wp_enqueue_script( 'wp-plupload' );
  367. }
  368. /**
  369. * Refresh the parameters passed to the JavaScript via JSON.
  370. *
  371. * @since 3.4.0
  372. * @uses WP_Customize_Control::to_json()
  373. */
  374. public function to_json() {
  375. parent::to_json();
  376. $this->json['removed'] = $this->removed;
  377. if ( $this->context )
  378. $this->json['context'] = $this->context;
  379. }
  380. /**
  381. * Render the control's content.
  382. *
  383. * @since 3.4.0
  384. */
  385. public function render_content() {
  386. ?>
  387. <label>
  388. <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
  389. <div>
  390. <a href="#" class="button-secondary upload"><?php _e( 'Upload' ); ?></a>
  391. <a href="#" class="remove"><?php _e( 'Remove' ); ?></a>
  392. </div>
  393. </label>
  394. <?php
  395. }
  396. }
  397. /**
  398. * Customize Image Control Class
  399. *
  400. * @package WordPress
  401. * @subpackage Customize
  402. * @since 3.4.0
  403. */
  404. class WP_Customize_Image_Control extends WP_Customize_Upload_Control {
  405. public $type = 'image';
  406. public $get_url;
  407. public $statuses;
  408. protected $tabs = array();
  409. /**
  410. * Constructor.
  411. *
  412. * If $args['settings'] is not defined, use the $id as the setting ID.
  413. *
  414. * @since 3.4.0
  415. * @uses WP_Customize_Upload_Control::__construct()
  416. *
  417. * @param WP_Customize_Manager $manager
  418. * @param string $id
  419. * @param array $args
  420. */
  421. public function __construct( $manager, $id, $args ) {
  422. $this->statuses = array( '' => __('No Image') );
  423. parent::__construct( $manager, $id, $args );
  424. $this->add_tab( 'upload-new', __('Upload New'), array( $this, 'tab_upload_new' ) );
  425. $this->add_tab( 'uploaded', __('Uploaded'), array( $this, 'tab_uploaded' ) );
  426. // Early priority to occur before $this->manager->prepare_controls();
  427. add_action( 'customize_controls_init', array( $this, 'prepare_control' ), 5 );
  428. }
  429. /**
  430. * Prepares the control.
  431. *
  432. * If no tabs exist, removes the control from the manager.
  433. *
  434. * @since 3.4.2
  435. */
  436. public function prepare_control() {
  437. if ( ! $this->tabs )
  438. $this->manager->remove_control( $this->id );
  439. }
  440. /**
  441. * Refresh the parameters passed to the JavaScript via JSON.
  442. *
  443. * @since 3.4.0
  444. * @uses WP_Customize_Upload_Control::to_json()
  445. */
  446. public function to_json() {
  447. parent::to_json();
  448. $this->json['statuses'] = $this->statuses;
  449. }
  450. /**
  451. * Render the control's content.
  452. *
  453. * @since 3.4.0
  454. */
  455. public function render_content() {
  456. $src = $this->value();
  457. if ( isset( $this->get_url ) )
  458. $src = call_user_func( $this->get_url, $src );
  459. ?>
  460. <div class="customize-image-picker">
  461. <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
  462. <div class="customize-control-content">
  463. <div class="dropdown preview-thumbnail">
  464. <div class="dropdown-content">
  465. <?php if ( empty( $src ) ): ?>
  466. <img style="display:none;" />
  467. <?php else: ?>
  468. <img src="<?php echo esc_url( set_url_scheme( $src ) ); ?>" />
  469. <?php endif; ?>
  470. <div class="dropdown-status"></div>
  471. </div>
  472. <div class="dropdown-arrow"></div>
  473. </div>
  474. </div>
  475. <div class="library">
  476. <ul>
  477. <?php foreach ( $this->tabs as $id => $tab ): ?>
  478. <li data-customize-tab='<?php echo esc_attr( $id ); ?>'>
  479. <?php echo esc_html( $tab['label'] ); ?>
  480. </li>
  481. <?php endforeach; ?>
  482. </ul>
  483. <?php foreach ( $this->tabs as $id => $tab ): ?>
  484. <div class="library-content" data-customize-tab='<?php echo esc_attr( $id ); ?>'>
  485. <?php call_user_func( $tab['callback'] ); ?>
  486. </div>
  487. <?php endforeach; ?>
  488. </div>
  489. <div class="actions">
  490. <a href="#" class="remove"><?php _e( 'Remove Image' ); ?></a>
  491. </div>
  492. </div>
  493. <?php
  494. }
  495. /**
  496. * Add a tab to the control.
  497. *
  498. * @since 3.4.0
  499. *
  500. * @param string $id
  501. * @param string $label
  502. * @param mixed $callback
  503. */
  504. public function add_tab( $id, $label, $callback ) {
  505. $this->tabs[ $id ] = array(
  506. 'label' => $label,
  507. 'callback' => $callback,
  508. );
  509. }
  510. /**
  511. * Remove a tab from the control.
  512. *
  513. * @since 3.4.0
  514. *
  515. * @param string $id
  516. */
  517. public function remove_tab( $id ) {
  518. unset( $this->tabs[ $id ] );
  519. }
  520. /**
  521. * @since 3.4.0
  522. */
  523. public function tab_upload_new() {
  524. if ( ! _device_can_upload() ) {
  525. ?>
  526. <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>
  527. <?php
  528. } else {
  529. ?>
  530. <div class="upload-dropzone">
  531. <?php _e('Drop a file here or <a href="#" class="upload">select a file</a>.'); ?>
  532. </div>
  533. <div class="upload-fallback">
  534. <span class="button-secondary"><?php _e('Select File'); ?></span>
  535. </div>
  536. <?php
  537. }
  538. }
  539. /**
  540. * @since 3.4.0
  541. */
  542. public function tab_uploaded() {
  543. ?>
  544. <div class="uploaded-target"></div>
  545. <?php
  546. }
  547. /**
  548. * @since 3.4.0
  549. *
  550. * @param string $url
  551. * @param string $thumbnail_url
  552. */
  553. public function print_tab_image( $url, $thumbnail_url = null ) {
  554. $url = set_url_scheme( $url );
  555. $thumbnail_url = ( $thumbnail_url ) ? set_url_scheme( $thumbnail_url ) : $url;
  556. ?>
  557. <a href="#" class="thumbnail" data-customize-image-value="<?php echo esc_url( $url ); ?>">
  558. <img src="<?php echo esc_url( $thumbnail_url ); ?>" />
  559. </a>
  560. <?php
  561. }
  562. }
  563. /**
  564. * Customize Background Image Control Class
  565. *
  566. * @package WordPress
  567. * @subpackage Customize
  568. * @since 3.4.0
  569. */
  570. class WP_Customize_Background_Image_Control extends WP_Customize_Image_Control {
  571. /**
  572. * Constructor.
  573. *
  574. * @since 3.4.0
  575. * @uses WP_Customize_Image_Control::__construct()
  576. *
  577. * @param WP_Customize_Manager $manager
  578. */
  579. public function __construct( $manager ) {
  580. parent::__construct( $manager, 'background_image', array(
  581. 'label' => __( 'Background Image' ),
  582. 'section' => 'background_image',
  583. 'context' => 'custom-background',
  584. 'get_url' => 'get_background_image',
  585. ) );
  586. if ( $this->setting->default )
  587. $this->add_tab( 'default', __('Default'), array( $this, 'tab_default_background' ) );
  588. }
  589. /**
  590. * @since 3.4.0
  591. */
  592. public function tab_uploaded() {
  593. $backgrounds = get_posts( array(
  594. 'post_type' => 'attachment',
  595. 'meta_key' => '_wp_attachment_is_custom_background',
  596. 'meta_value' => $this->manager->get_stylesheet(),
  597. 'orderby' => 'none',
  598. 'nopaging' => true,
  599. ) );
  600. ?><div class="uploaded-target"></div><?php
  601. if ( empty( $backgrounds ) )
  602. return;
  603. foreach ( (array) $backgrounds as $background )
  604. $this->print_tab_image( esc_url_raw( $background->guid ) );
  605. }
  606. /**
  607. * @since 3.4.0
  608. * @uses WP_Customize_Image_Control::print_tab_image()
  609. */
  610. public function tab_default_background() {
  611. $this->print_tab_image( $this->setting->default );
  612. }
  613. }
  614. /**
  615. * Customize Header Image Control Class
  616. *
  617. * @package WordPress
  618. * @subpackage Customize
  619. * @since 3.4.0
  620. */
  621. class WP_Customize_Header_Image_Control extends WP_Customize_Image_Control {
  622. /**
  623. * The processed default headers.
  624. * @since 3.4.2
  625. * @var array
  626. */
  627. protected $default_headers;
  628. /**
  629. * The uploaded headers.
  630. * @since 3.4.2
  631. * @var array
  632. */
  633. protected $uploaded_headers;
  634. /**
  635. * Constructor.
  636. *
  637. * @since 3.4.0
  638. * @uses WP_Customize_Image_Control::__construct()
  639. * @uses WP_Customize_Image_Control::add_tab()
  640. *
  641. * @param WP_Customize_Manager $manager
  642. */
  643. public function __construct( $manager ) {
  644. parent::__construct( $manager, 'header_image', array(
  645. 'label' => __( 'Header Image' ),
  646. 'settings' => array(
  647. 'default' => 'header_image',
  648. 'data' => 'header_image_data',
  649. ),
  650. 'section' => 'header_image',
  651. 'context' => 'custom-header',
  652. 'removed' => 'remove-header',
  653. 'get_url' => 'get_header_image',
  654. 'statuses' => array(
  655. '' => __('Default'),
  656. 'remove-header' => __('No Image'),
  657. 'random-default-image' => __('Random Default Image'),
  658. 'random-uploaded-image' => __('Random Uploaded Image'),
  659. )
  660. ) );
  661. // Remove the upload tab.
  662. $this->remove_tab( 'upload-new' );
  663. }
  664. /**
  665. * Prepares the control.
  666. *
  667. * If no tabs exist, removes the control from the manager.
  668. *
  669. * @since 3.4.2
  670. */
  671. public function prepare_control() {
  672. global $custom_image_header;
  673. if ( empty( $custom_image_header ) )
  674. return parent::prepare_control();
  675. // Process default headers and uploaded headers.
  676. $custom_image_header->process_default_headers();
  677. $this->default_headers = $custom_image_header->default_headers;
  678. $this->uploaded_headers = get_uploaded_header_images();
  679. if ( $this->default_headers )
  680. $this->add_tab( 'default', __('Default'), array( $this, 'tab_default_headers' ) );
  681. if ( ! $this->uploaded_headers )
  682. $this->remove_tab( 'uploaded' );
  683. return parent::prepare_control();
  684. }
  685. /**
  686. * @since 3.4.0
  687. *
  688. * @param mixed $choice Which header image to select. (@see Custom_Image_Header::get_header_image() )
  689. * @param array $header
  690. */
  691. public function print_header_image( $choice, $header ) {
  692. $header['url'] = set_url_scheme( $header['url'] );
  693. $header['thumbnail_url'] = set_url_scheme( $header['thumbnail_url'] );
  694. $header_image_data = array( 'choice' => $choice );
  695. foreach ( array( 'attachment_id', 'width', 'height', 'url', 'thumbnail_url' ) as $key ) {
  696. if ( isset( $header[ $key ] ) )
  697. $header_image_data[ $key ] = $header[ $key ];
  698. }
  699. ?>
  700. <a href="#" class="thumbnail"
  701. data-customize-image-value="<?php echo esc_url( $header['url'] ); ?>"
  702. data-customize-header-image-data="<?php echo esc_attr( json_encode( $header_image_data ) ); ?>">
  703. <img src="<?php echo esc_url( $header['thumbnail_url'] ); ?>" />
  704. </a>
  705. <?php
  706. }
  707. /**
  708. * @since 3.4.0
  709. */
  710. public function tab_uploaded() {
  711. ?><div class="uploaded-target"></div><?php
  712. foreach ( $this->uploaded_headers as $choice => $header )
  713. $this->print_header_image( $choice, $header );
  714. }
  715. /**
  716. * @since 3.4.0
  717. */
  718. public function tab_default_headers() {
  719. foreach ( $this->default_headers as $choice => $header )
  720. $this->print_header_image( $choice, $header );
  721. }
  722. }