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

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

https://bitbucket.org/floppyxyz/musical
PHP | 567 lines | 395 code | 86 blank | 86 comment | 24 complexity | ff7dfbe9921ad14ea051b42f64c20bbf 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. public $manager;
  11. public $id;
  12. // All settings tied to the control.
  13. public $settings;
  14. // The primary setting for the control (if there is one).
  15. public $setting = 'default';
  16. public $priority = 10;
  17. public $section = '';
  18. public $label = '';
  19. // @todo: remove choices
  20. public $choices = array();
  21. public $json = array();
  22. public $type = 'text';
  23. /**
  24. * Constructor.
  25. *
  26. * If $args['settings'] is not defined, use the $id as the setting ID.
  27. *
  28. * @since 3.4.0
  29. */
  30. function __construct( $manager, $id, $args = array() ) {
  31. $keys = array_keys( get_object_vars( $this ) );
  32. foreach ( $keys as $key ) {
  33. if ( isset( $args[ $key ] ) )
  34. $this->$key = $args[ $key ];
  35. }
  36. $this->manager = $manager;
  37. $this->id = $id;
  38. // Process settings.
  39. if ( empty( $this->settings ) )
  40. $this->settings = $id;
  41. $settings = array();
  42. if ( is_array( $this->settings ) ) {
  43. foreach ( $this->settings as $key => $setting ) {
  44. $settings[ $key ] = $this->manager->get_setting( $setting );
  45. }
  46. } else {
  47. $this->setting = $this->manager->get_setting( $this->settings );
  48. $settings['default'] = $this->setting;
  49. }
  50. $this->settings = $settings;
  51. }
  52. /**
  53. * Enqueue control related scripts/styles.
  54. *
  55. * @since 3.4.0
  56. */
  57. public function enqueue() {}
  58. /**
  59. * Fetch a setting's value.
  60. * Grabs the main setting by default.
  61. *
  62. * @since 3.4.0
  63. */
  64. public final function value( $setting_key = 'default' ) {
  65. if ( isset( $this->settings[ $setting_key ] ) )
  66. return $this->settings[ $setting_key ]->value();
  67. }
  68. /**
  69. * Refresh the parameters passed to the JavaScript via JSON.
  70. *
  71. * @since 3.4.0
  72. */
  73. public function to_json() {
  74. $this->json['settings'] = array();
  75. foreach ( $this->settings as $key => $setting ) {
  76. $this->json['settings'][ $key ] = $setting->id;
  77. }
  78. $this->json['type'] = $this->type;
  79. }
  80. /**
  81. * Check if the theme supports the control and check user capabilities.
  82. *
  83. * @since 3.4.0
  84. *
  85. * @return bool False if theme doesn't support the control or user doesn't have the required permissions, otherwise true.
  86. */
  87. public final function check_capabilities() {
  88. foreach ( $this->settings as $setting ) {
  89. if ( ! $setting->check_capabilities() )
  90. return false;
  91. }
  92. $section = $this->manager->get_section( $this->section );
  93. if ( isset( $section ) && ! $section->check_capabilities() )
  94. return false;
  95. return true;
  96. }
  97. /**
  98. * Check capabilities and render the control.
  99. *
  100. * @since 3.4.0
  101. */
  102. public final function maybe_render() {
  103. if ( ! $this->check_capabilities() )
  104. return;
  105. do_action( 'customize_render_control', $this );
  106. do_action( 'customize_render_control_' . $this->id, $this );
  107. $this->render();
  108. }
  109. /**
  110. * Render the control. Renders the control wrapper, then calls $this->render_content().
  111. *
  112. * @since 3.4.0
  113. */
  114. protected function render() {
  115. $id = 'customize-control-' . str_replace( '[', '-', str_replace( ']', '', $this->id ) );
  116. $class = 'customize-control customize-control-' . $this->type;
  117. ?><li id="<?php echo esc_attr( $id ); ?>" class="<?php echo esc_attr( $class ); ?>">
  118. <?php $this->render_content(); ?>
  119. </li><?php
  120. }
  121. public function get_link( $setting_key = 'default' ) {
  122. if ( ! isset( $this->settings[ $setting_key ] ) )
  123. return '';
  124. return 'data-customize-setting-link="' . esc_attr( $this->settings[ $setting_key ]->id ) . '"';
  125. }
  126. public function link( $setting_key = 'default' ) {
  127. echo $this->get_link( $setting_key );
  128. }
  129. /**
  130. * Render the control's content.
  131. *
  132. * Allows the content to be overriden without having to rewrite the wrapper.
  133. *
  134. * @since 3.4.0
  135. */
  136. protected function render_content() {
  137. switch( $this->type ) {
  138. case 'text':
  139. ?>
  140. <label>
  141. <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
  142. <input type="text" value="<?php echo esc_attr( $this->value() ); ?>" <?php $this->link(); ?> />
  143. </label>
  144. <?php
  145. break;
  146. case 'checkbox':
  147. ?>
  148. <label>
  149. <input type="checkbox" value="<?php echo esc_attr( $this->value() ); ?>" <?php $this->link(); checked( $this->value() ); ?> />
  150. <?php echo esc_html( $this->label ); ?>
  151. </label>
  152. <?php
  153. break;
  154. case 'radio':
  155. if ( empty( $this->choices ) )
  156. return;
  157. $name = '_customize-radio-' . $this->id;
  158. ?>
  159. <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
  160. <?php
  161. foreach ( $this->choices as $value => $label ) :
  162. ?>
  163. <label>
  164. <input type="radio" value="<?php echo esc_attr( $value ); ?>" name="<?php echo esc_attr( $name ); ?>" <?php $this->link(); checked( $this->value(), $value ); ?> />
  165. <?php echo esc_html( $label ); ?><br/>
  166. </label>
  167. <?php
  168. endforeach;
  169. break;
  170. case 'select':
  171. if ( empty( $this->choices ) )
  172. return;
  173. ?>
  174. <label>
  175. <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
  176. <select <?php $this->link(); ?>>
  177. <?php
  178. foreach ( $this->choices as $value => $label )
  179. echo '<option value="' . esc_attr( $value ) . '"' . selected( $this->value(), $value, false ) . '>' . $label . '</option>';
  180. ?>
  181. </select>
  182. </label>
  183. <?php
  184. break;
  185. case 'dropdown-pages':
  186. $dropdown = wp_dropdown_pages(
  187. array(
  188. 'name' => '_customize-dropdown-pages-' . $this->id,
  189. 'echo' => 0,
  190. 'show_option_none' => __( '&mdash; Select &mdash;' ),
  191. 'option_none_value' => '0',
  192. 'selected' => $this->value(),
  193. )
  194. );
  195. // Hackily add in the data link parameter.
  196. $dropdown = str_replace( '<select', '<select ' . $this->get_link(), $dropdown );
  197. printf(
  198. '<label class="customize-control-select"><span class="customize-control-title">%s</span> %s</label>',
  199. $this->label,
  200. $dropdown
  201. );
  202. break;
  203. }
  204. }
  205. }
  206. class WP_Customize_Color_Control extends WP_Customize_Control {
  207. public $type = 'color';
  208. public $statuses;
  209. public function __construct( $manager, $id, $args = array() ) {
  210. $this->statuses = array( '' => __('Default') );
  211. parent::__construct( $manager, $id, $args );
  212. }
  213. public function enqueue() {
  214. wp_enqueue_script( 'farbtastic' );
  215. wp_enqueue_style( 'farbtastic' );
  216. }
  217. public function to_json() {
  218. parent::to_json();
  219. $this->json['statuses'] = $this->statuses;
  220. }
  221. public function render_content() {
  222. ?>
  223. <label>
  224. <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
  225. <div class="customize-control-content">
  226. <div class="dropdown">
  227. <div class="dropdown-content">
  228. <div class="dropdown-status"></div>
  229. </div>
  230. <div class="dropdown-arrow"></div>
  231. </div>
  232. <input class="color-picker-hex" type="text" maxlength="7" placeholder="<?php esc_attr_e('Hex Value'); ?>" />
  233. </div>
  234. <div class="farbtastic-placeholder"></div>
  235. </label>
  236. <?php
  237. }
  238. }
  239. class WP_Customize_Upload_Control extends WP_Customize_Control {
  240. public $type = 'upload';
  241. public $removed = '';
  242. public $context;
  243. public function enqueue() {
  244. wp_enqueue_script( 'wp-plupload' );
  245. }
  246. public function to_json() {
  247. parent::to_json();
  248. $this->json['removed'] = $this->removed;
  249. if ( $this->context )
  250. $this->json['context'] = $this->context;
  251. }
  252. public function render_content() {
  253. ?>
  254. <label>
  255. <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
  256. <div>
  257. <a href="#" class="button-secondary upload"><?php _e( 'Upload' ); ?></a>
  258. <a href="#" class="remove"><?php _e( 'Remove' ); ?></a>
  259. </div>
  260. </label>
  261. <?php
  262. }
  263. }
  264. class WP_Customize_Image_Control extends WP_Customize_Upload_Control {
  265. public $type = 'image';
  266. public $get_url;
  267. public $statuses;
  268. protected $tabs = array();
  269. public function __construct( $manager, $id, $args ) {
  270. $this->statuses = array( '' => __('No Image') );
  271. parent::__construct( $manager, $id, $args );
  272. $this->add_tab( 'upload-new', __('Upload New'), array( $this, 'tab_upload_new' ) );
  273. $this->add_tab( 'uploaded', __('Uploaded'), array( $this, 'tab_uploaded' ) );
  274. // Early priority to occur before $this->manager->prepare_controls();
  275. add_action( 'customize_controls_init', array( $this, 'prepare_control' ), 5 );
  276. }
  277. /**
  278. * Prepares the control.
  279. *
  280. * If no tabs exist, removes the control from the manager.
  281. *
  282. * @since 3.4.2
  283. */
  284. public function prepare_control() {
  285. if ( ! $this->tabs )
  286. $this->manager->remove_control( $this->id );
  287. }
  288. public function to_json() {
  289. parent::to_json();
  290. $this->json['statuses'] = $this->statuses;
  291. }
  292. public function render_content() {
  293. $src = $this->value();
  294. if ( isset( $this->get_url ) )
  295. $src = call_user_func( $this->get_url, $src );
  296. ?>
  297. <div class="customize-image-picker">
  298. <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
  299. <div class="customize-control-content">
  300. <div class="dropdown preview-thumbnail">
  301. <div class="dropdown-content">
  302. <?php if ( empty( $src ) ): ?>
  303. <img style="display:none;" />
  304. <?php else: ?>
  305. <img src="<?php echo esc_url( set_url_scheme( $src ) ); ?>" />
  306. <?php endif; ?>
  307. <div class="dropdown-status"></div>
  308. </div>
  309. <div class="dropdown-arrow"></div>
  310. </div>
  311. </div>
  312. <div class="library">
  313. <ul>
  314. <?php foreach ( $this->tabs as $id => $tab ): ?>
  315. <li data-customize-tab='<?php echo esc_attr( $id ); ?>'>
  316. <?php echo esc_html( $tab['label'] ); ?>
  317. </li>
  318. <?php endforeach; ?>
  319. </ul>
  320. <?php foreach ( $this->tabs as $id => $tab ): ?>
  321. <div class="library-content" data-customize-tab='<?php echo esc_attr( $id ); ?>'>
  322. <?php call_user_func( $tab['callback'] ); ?>
  323. </div>
  324. <?php endforeach; ?>
  325. </div>
  326. <div class="actions">
  327. <a href="#" class="remove"><?php _e( 'Remove Image' ); ?></a>
  328. </div>
  329. </div>
  330. <?php
  331. }
  332. public function add_tab( $id, $label, $callback ) {
  333. $this->tabs[ $id ] = array(
  334. 'label' => $label,
  335. 'callback' => $callback,
  336. );
  337. }
  338. public function remove_tab( $id ) {
  339. unset( $this->tabs[ $id ] );
  340. }
  341. public function tab_upload_new() {
  342. if ( ! _device_can_upload() ) {
  343. ?>
  344. <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>
  345. <?php
  346. } else {
  347. ?>
  348. <div class="upload-dropzone">
  349. <?php _e('Drop a file here or <a href="#" class="upload">select a file</a>.'); ?>
  350. </div>
  351. <div class="upload-fallback">
  352. <span class="button-secondary"><?php _e('Select File'); ?></span>
  353. </div>
  354. <?php
  355. }
  356. }
  357. public function tab_uploaded() {
  358. ?>
  359. <div class="uploaded-target"></div>
  360. <?php
  361. }
  362. public function print_tab_image( $url, $thumbnail_url = null ) {
  363. $url = set_url_scheme( $url );
  364. $thumbnail_url = ( $thumbnail_url ) ? set_url_scheme( $thumbnail_url ) : $url;
  365. ?>
  366. <a href="#" class="thumbnail" data-customize-image-value="<?php echo esc_url( $url ); ?>">
  367. <img src="<?php echo esc_url( $thumbnail_url ); ?>" />
  368. </a>
  369. <?php
  370. }
  371. }
  372. class WP_Customize_Background_Image_Control extends WP_Customize_Image_Control {
  373. public function __construct( $manager ) {
  374. parent::__construct( $manager, 'background_image', array(
  375. 'label' => __( 'Background Image' ),
  376. 'section' => 'background_image',
  377. 'context' => 'custom-background',
  378. 'get_url' => 'get_background_image',
  379. ) );
  380. if ( $this->setting->default )
  381. $this->add_tab( 'default', __('Default'), array( $this, 'tab_default_background' ) );
  382. }
  383. public function tab_uploaded() {
  384. $backgrounds = get_posts( array(
  385. 'post_type' => 'attachment',
  386. 'meta_key' => '_wp_attachment_is_custom_background',
  387. 'meta_value' => $this->manager->get_stylesheet(),
  388. 'orderby' => 'none',
  389. 'nopaging' => true,
  390. ) );
  391. ?><div class="uploaded-target"></div><?php
  392. if ( empty( $backgrounds ) )
  393. return;
  394. foreach ( (array) $backgrounds as $background )
  395. $this->print_tab_image( esc_url_raw( $background->guid ) );
  396. }
  397. public function tab_default_background() {
  398. $this->print_tab_image( $this->setting->default );
  399. }
  400. }
  401. class WP_Customize_Header_Image_Control extends WP_Customize_Image_Control {
  402. /**
  403. * The processed default headers.
  404. * @since 3.4.2
  405. * @var array
  406. */
  407. protected $default_headers;
  408. /**
  409. * The uploaded headers.
  410. * @since 3.4.2
  411. * @var array
  412. */
  413. protected $uploaded_headers;
  414. public function __construct( $manager ) {
  415. parent::__construct( $manager, 'header_image', array(
  416. 'label' => __( 'Header Image' ),
  417. 'settings' => array(
  418. 'default' => 'header_image',
  419. 'data' => 'header_image_data',
  420. ),
  421. 'section' => 'header_image',
  422. 'context' => 'custom-header',
  423. 'removed' => 'remove-header',
  424. 'get_url' => 'get_header_image',
  425. 'statuses' => array(
  426. '' => __('Default'),
  427. 'remove-header' => __('No Image'),
  428. 'random-default-image' => __('Random Default Image'),
  429. 'random-uploaded-image' => __('Random Uploaded Image'),
  430. )
  431. ) );
  432. // Remove the upload tab.
  433. $this->remove_tab( 'upload-new' );
  434. }
  435. /**
  436. * Prepares the control.
  437. *
  438. * If no tabs exist, removes the control from the manager.
  439. *
  440. * @since 3.4.2
  441. */
  442. public function prepare_control() {
  443. global $custom_image_header;
  444. if ( empty( $custom_image_header ) )
  445. return parent::prepare_control();
  446. // Process default headers and uploaded headers.
  447. $custom_image_header->process_default_headers();
  448. $this->default_headers = $custom_image_header->default_headers;
  449. $this->uploaded_headers = get_uploaded_header_images();
  450. if ( $this->default_headers )
  451. $this->add_tab( 'default', __('Default'), array( $this, 'tab_default_headers' ) );
  452. if ( ! $this->uploaded_headers )
  453. $this->remove_tab( 'uploaded' );
  454. return parent::prepare_control();
  455. }
  456. public function print_header_image( $choice, $header ) {
  457. $header['url'] = set_url_scheme( $header['url'] );
  458. $header['thumbnail_url'] = set_url_scheme( $header['thumbnail_url'] );
  459. $header_image_data = array( 'choice' => $choice );
  460. foreach ( array( 'attachment_id', 'width', 'height', 'url', 'thumbnail_url' ) as $key ) {
  461. if ( isset( $header[ $key ] ) )
  462. $header_image_data[ $key ] = $header[ $key ];
  463. }
  464. ?>
  465. <a href="#" class="thumbnail"
  466. data-customize-image-value="<?php echo esc_url( $header['url'] ); ?>"
  467. data-customize-header-image-data="<?php echo esc_attr( json_encode( $header_image_data ) ); ?>">
  468. <img src="<?php echo esc_url( $header['thumbnail_url'] ); ?>" />
  469. </a>
  470. <?php
  471. }
  472. public function tab_uploaded() {
  473. ?><div class="uploaded-target"></div><?php
  474. foreach ( $this->uploaded_headers as $choice => $header )
  475. $this->print_header_image( $choice, $header );
  476. }
  477. public function tab_default_headers() {
  478. foreach ( $this->default_headers as $choice => $header )
  479. $this->print_header_image( $choice, $header );
  480. }
  481. }