PageRenderTime 557ms CodeModel.GetById 42ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/sergiohzlz/reportaprod
PHP | 516 lines | 378 code | 79 blank | 59 comment | 20 complexity | 85d2dcf8c79b82fdfeed411e0d351645 MD5 | raw file
Possible License(s): GPL-2.0, GPL-3.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. }
  275. public function to_json() {
  276. parent::to_json();
  277. $this->json['statuses'] = $this->statuses;
  278. }
  279. public function render_content() {
  280. $src = $this->value();
  281. if ( isset( $this->get_url ) )
  282. $src = call_user_func( $this->get_url, $src );
  283. ?>
  284. <div class="customize-image-picker">
  285. <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
  286. <div class="customize-control-content">
  287. <div class="dropdown preview-thumbnail">
  288. <div class="dropdown-content">
  289. <?php if ( empty( $src ) ): ?>
  290. <img style="display:none;" />
  291. <?php else: ?>
  292. <img src="<?php echo esc_url( set_url_scheme( $src ) ); ?>" />
  293. <?php endif; ?>
  294. <div class="dropdown-status"></div>
  295. </div>
  296. <div class="dropdown-arrow"></div>
  297. </div>
  298. </div>
  299. <div class="library">
  300. <ul>
  301. <?php foreach ( $this->tabs as $id => $tab ): ?>
  302. <li data-customize-tab='<?php echo esc_attr( $id ); ?>'>
  303. <?php echo esc_html( $tab['label'] ); ?>
  304. </li>
  305. <?php endforeach; ?>
  306. </ul>
  307. <?php foreach ( $this->tabs as $id => $tab ): ?>
  308. <div class="library-content" data-customize-tab='<?php echo esc_attr( $id ); ?>'>
  309. <?php call_user_func( $tab['callback'] ); ?>
  310. </div>
  311. <?php endforeach; ?>
  312. </div>
  313. <div class="actions">
  314. <a href="#" class="remove"><?php _e( 'Remove Image' ); ?></a>
  315. </div>
  316. </div>
  317. <?php
  318. }
  319. public function add_tab( $id, $label, $callback ) {
  320. $this->tabs[ $id ] = array(
  321. 'label' => $label,
  322. 'callback' => $callback,
  323. );
  324. }
  325. public function remove_tab( $id ) {
  326. unset( $this->tabs[ $id ] );
  327. }
  328. public function tab_upload_new() {
  329. if ( ! _device_can_upload() ) {
  330. ?>
  331. <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>
  332. <?php
  333. } else {
  334. ?>
  335. <div class="upload-dropzone">
  336. <?php _e('Drop a file here or <a href="#" class="upload">select a file</a>.'); ?>
  337. </div>
  338. <div class="upload-fallback">
  339. <span class="button-secondary"><?php _e('Select File'); ?></span>
  340. </div>
  341. <?php
  342. }
  343. }
  344. public function tab_uploaded() {
  345. ?>
  346. <div class="uploaded-target"></div>
  347. <?php
  348. }
  349. public function print_tab_image( $url, $thumbnail_url = null ) {
  350. $url = set_url_scheme( $url );
  351. $thumbnail_url = ( $thumbnail_url ) ? set_url_scheme( $thumbnail_url ) : $url;
  352. ?>
  353. <a href="#" class="thumbnail" data-customize-image-value="<?php echo esc_url( $url ); ?>">
  354. <img src="<?php echo esc_url( $thumbnail_url ); ?>" />
  355. </a>
  356. <?php
  357. }
  358. }
  359. class WP_Customize_Background_Image_Control extends WP_Customize_Image_Control {
  360. public function __construct( $manager ) {
  361. parent::__construct( $manager, 'background_image', array(
  362. 'label' => __( 'Background Image' ),
  363. 'section' => 'background_image',
  364. 'context' => 'custom-background',
  365. 'get_url' => 'get_background_image',
  366. ) );
  367. if ( $this->setting->default )
  368. $this->add_tab( 'default', __('Default'), array( $this, 'tab_default_background' ) );
  369. }
  370. public function tab_uploaded() {
  371. $backgrounds = get_posts( array(
  372. 'post_type' => 'attachment',
  373. 'meta_key' => '_wp_attachment_is_custom_background',
  374. 'meta_value' => $this->manager->get_stylesheet(),
  375. 'orderby' => 'none',
  376. 'nopaging' => true,
  377. ) );
  378. ?><div class="uploaded-target"></div><?php
  379. if ( empty( $backgrounds ) )
  380. return;
  381. foreach ( (array) $backgrounds as $background )
  382. $this->print_tab_image( esc_url_raw( $background->guid ) );
  383. }
  384. public function tab_default_background() {
  385. $this->print_tab_image( $this->setting->default );
  386. }
  387. }
  388. class WP_Customize_Header_Image_Control extends WP_Customize_Image_Control {
  389. public function __construct( $manager ) {
  390. parent::__construct( $manager, 'header_image', array(
  391. 'label' => __( 'Header Image' ),
  392. 'settings' => array(
  393. 'default' => 'header_image',
  394. 'data' => 'header_image_data',
  395. ),
  396. 'section' => 'header_image',
  397. 'context' => 'custom-header',
  398. 'removed' => 'remove-header',
  399. 'get_url' => 'get_header_image',
  400. 'statuses' => array(
  401. '' => __('Default'),
  402. 'remove-header' => __('No Image'),
  403. 'random-default-image' => __('Random Default Image'),
  404. 'random-uploaded-image' => __('Random Uploaded Image'),
  405. )
  406. ) );
  407. $this->add_tab( 'default', __('Default'), array( $this, 'tab_default_headers' ) );
  408. }
  409. public function print_header_image( $choice, $header ) {
  410. $header['url'] = set_url_scheme( $header['url'] );
  411. $header['thumbnail_url'] = set_url_scheme( $header['thumbnail_url'] );
  412. $header_image_data = array( 'choice' => $choice );
  413. foreach ( array( 'attachment_id', 'width', 'height', 'url', 'thumbnail_url' ) as $key ) {
  414. if ( isset( $header[ $key ] ) )
  415. $header_image_data[ $key ] = $header[ $key ];
  416. }
  417. ?>
  418. <a href="#" class="thumbnail"
  419. data-customize-image-value="<?php echo esc_url( $header['url'] ); ?>"
  420. data-customize-header-image-data="<?php echo esc_attr( json_encode( $header_image_data ) ); ?>">
  421. <img src="<?php echo esc_url( $header['thumbnail_url'] ); ?>" />
  422. </a>
  423. <?php
  424. }
  425. public function tab_uploaded() {
  426. $headers = get_uploaded_header_images();
  427. ?><div class="uploaded-target"></div><?php
  428. foreach ( $headers as $choice => $header )
  429. $this->print_header_image( $choice, $header );
  430. }
  431. public function tab_default_headers() {
  432. global $custom_image_header;
  433. $custom_image_header->process_default_headers();
  434. foreach ( $custom_image_header->default_headers as $choice => $header )
  435. $this->print_header_image( $choice, $header );
  436. }
  437. }