PageRenderTime 58ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

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

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