class-vamtam-customize-background-control.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. <?php
  2. /**
  3. Background image + colors
  4. **/
  5. class Vamtam_Customize_Background_Control extends Vamtam_Customize_Control {
  6. public $type = 'vamtam-background';
  7. /**
  8. * Media control mime type.
  9. *
  10. * @access public
  11. * @var string
  12. */
  13. public $mime_type = 'image';
  14. /**
  15. * Button labels.
  16. *
  17. * @access public
  18. * @var array
  19. */
  20. public $button_labels = array();
  21. /**
  22. * Defines which properties are configurable
  23. */
  24. public $show = array();
  25. /**
  26. * Holds all possible values for the dropdown options
  27. *
  28. * @var array
  29. */
  30. public static $selects = array();
  31. /**
  32. * Constructor.
  33. *
  34. * @since 4.1.0
  35. * @since 4.2.0 Moved from WP_Customize_Upload_Control.
  36. *
  37. * @param WP_Customize_Manager $manager Customizer bootstrap instance.
  38. * @param string $id Control ID.
  39. * @param array $args Optional. Arguments to override class property defaults.
  40. */
  41. public function __construct( $manager, $id, $args = array() ) {
  42. parent::__construct( $manager, $id, $args );
  43. $this->button_labels = wp_parse_args( $this->button_labels, array(
  44. 'select' => esc_html__( 'Select Image', 'vamtam-consulting' ),
  45. 'change' => esc_html__( 'Change Image', 'vamtam-consulting' ),
  46. 'remove' => esc_html__( 'Remove', 'vamtam-consulting' ),
  47. 'default' => esc_html__( 'Default', 'vamtam-consulting' ),
  48. 'placeholder' => esc_html__( 'No image selected', 'vamtam-consulting' ),
  49. 'frame_title' => esc_html__( 'Select Image', 'vamtam-consulting' ),
  50. 'frame_button' => esc_html__( 'Choose Image', 'vamtam-consulting' ),
  51. ) );
  52. $this->show = wp_parse_args( $this->show, array(
  53. 'background-image' => true,
  54. 'background-color' => true,
  55. 'background-attachment' => true,
  56. 'background-size' => true,
  57. 'background-repeat' => true,
  58. 'background-position' => true,
  59. ) );
  60. self::$selects = array(
  61. 'background-repeat' => array(
  62. 'no-repeat' => esc_html__( 'No repeat', 'vamtam-consulting' ),
  63. 'repeat-x' => esc_html__( 'Repeat horizontally', 'vamtam-consulting' ),
  64. 'repeat-y' => esc_html__( 'Repeat vertically', 'vamtam-consulting' ),
  65. 'repeat' => esc_html__( 'Repeat both', 'vamtam-consulting' ),
  66. ),
  67. 'background-attachment' => array(
  68. 'scroll' => esc_html__( 'scroll', 'vamtam-consulting' ),
  69. 'fixed' => esc_html__( 'fixed', 'vamtam-consulting' ),
  70. ),
  71. 'background-size' => array(
  72. 'auto' => esc_html__( 'auto', 'vamtam-consulting' ),
  73. 'cover' => esc_html__( 'cover', 'vamtam-consulting' ),
  74. 'contain' => esc_html__( 'contain', 'vamtam-consulting' ),
  75. ),
  76. 'background-position' => array(
  77. 'left top' => esc_html__( 'left top', 'vamtam-consulting' ),
  78. 'left center' => esc_html__( 'left center', 'vamtam-consulting' ),
  79. 'left bottom' => esc_html__( 'left bottom', 'vamtam-consulting' ),
  80. 'center top' => esc_html__( 'center top', 'vamtam-consulting' ),
  81. 'center center' => esc_html__( 'center center', 'vamtam-consulting' ),
  82. 'center bottom' => esc_html__( 'center bottom', 'vamtam-consulting' ),
  83. 'right top' => esc_html__( 'right top', 'vamtam-consulting' ),
  84. 'right center' => esc_html__( 'right center', 'vamtam-consulting' ),
  85. 'right bottom' => esc_html__( 'right bottom', 'vamtam-consulting' ),
  86. ),
  87. );
  88. }
  89. /**
  90. * Enqueue control related scripts/styles.
  91. *
  92. * @since 3.4.0
  93. * @since 4.2.0 Moved from WP_Customize_Upload_Control.
  94. */
  95. public function enqueue() {
  96. wp_enqueue_media();
  97. wp_enqueue_script(
  98. 'customizer-control-vamtam-background-js',
  99. VAMTAM_CUSTOMIZER_LIB_URL . 'assets/js/background' . ( WP_DEBUG ? '' : '.min' ) . '.js',
  100. array( 'jquery-core', 'customize-base', 'wp-color-picker' ),
  101. Vamtam_Customizer::$version,
  102. true
  103. );
  104. wp_enqueue_style(
  105. 'customizer-control-vamtam-background',
  106. VAMTAM_CUSTOMIZER_LIB_URL . 'assets/css/background.css',
  107. array( 'wp-color-picker' ),
  108. Vamtam_Customizer::$version
  109. );
  110. }
  111. /**
  112. * Refresh the parameters passed to the JavaScript via JSON.
  113. *
  114. * @since 3.4.0
  115. * @since 4.2.0 Moved from WP_Customize_Upload_Control.
  116. *
  117. * @see WP_Customize_Control::to_json()
  118. */
  119. public function to_json() {
  120. parent::to_json();
  121. $this->json['label'] = html_entity_decode( $this->label, ENT_QUOTES, get_bloginfo( 'charset' ) );
  122. $this->json['mime_type'] = $this->mime_type;
  123. $this->json['button_labels'] = $this->button_labels;
  124. $this->json['canUpload'] = current_user_can( 'upload_files' );
  125. $this->json['show'] = $this->show;
  126. $this->json['alt_medium'] = esc_html__( 'Medium-sized attachment', 'vamtam-consulting' );
  127. $this->json['alt_full'] = esc_html__( 'Full-sized attachment', 'vamtam-consulting' );
  128. $this->json['option_labels'] = array(
  129. 'background-repeat' => esc_html__( 'Repeat', 'vamtam-consulting' ),
  130. 'background-attachment' => esc_html__( 'Attachment', 'vamtam-consulting' ),
  131. 'background-size' => esc_html__( 'Size', 'vamtam-consulting' ),
  132. 'background-position' => esc_html__( 'Position', 'vamtam-consulting' ),
  133. 'background-color' => esc_html__( 'Color', 'vamtam-consulting' ),
  134. );
  135. $this->json['selects'] = self::$selects;
  136. $value = $this->value();
  137. if ( is_object( $this->setting ) ) {
  138. if ( $this->setting->default ) {
  139. // Fake an attachment model - needs all fields used by template.
  140. // Note that the default value must be a URL, NOT an attachment ID.
  141. $url = $this->setting->default['background-image'];
  142. $default = $this->setting->default;
  143. if ( ! empty( $url ) ) {
  144. $default['background-image'] = array(
  145. 'id' => 1,
  146. 'url' => $url,
  147. 'type' => 'image',
  148. 'icon' => 'image',
  149. 'title' => basename( $url ),
  150. 'sizes' => array(
  151. 'full' => array(
  152. 'url' => $url,
  153. ),
  154. ),
  155. );
  156. }
  157. $this->json['default'] = $default;
  158. }
  159. if ( $value && $this->setting->default && $value === $this->setting->default ) {
  160. // Set the default as the attachment.
  161. $this->json['bg'] = $this->json['default'];
  162. } elseif ( $value ) {
  163. if ( ! empty( $value['background-image'] ) && 0 !== ( $bg_image_id = attachment_url_to_postid( $value['background-image'] ) ) ) {
  164. $value['background-image'] = wp_prepare_attachment_for_js( $bg_image_id );
  165. }
  166. $this->json['bg'] = $value;
  167. }
  168. }
  169. }
  170. /**
  171. * Don't render any content for this control from PHP.
  172. *
  173. * @since 3.4.0
  174. * @since 4.2.0 Moved from WP_Customize_Upload_Control.
  175. *
  176. * @see WP_Customize_Media_Control::content_template()
  177. */
  178. public function render_content() {}
  179. /**
  180. * Render a JS template for the content of the media control.
  181. *
  182. * @since 4.1.0
  183. * @since 4.2.0 Moved from WP_Customize_Upload_Control.
  184. */
  185. public function content_template() {
  186. ?>
  187. <label for="{{ data.settings['default'] }}-button">
  188. <# if ( data.label ) { #>
  189. <span class="customize-control-title">{{ data.label }}</span>
  190. <# } #>
  191. <# if ( data.description ) { #>
  192. <span class="description customize-control-description">{{{ data.description }}}</span>
  193. <# } #>
  194. </label>
  195. <# if ( data.bg['background-image'] && data.bg['background-image'].id ) { #>
  196. <div class="current">
  197. <div class="container">
  198. <div class="attachment-media-view attachment-media-view-{{ data.bg['background-image'].type }} {{ data.bg['background-image'].orientation }}">
  199. <div class="thumbnail thumbnail-{{ data.bg['background-image'].type }}">
  200. <# if ( data.bg['background-image'].sizes && data.bg['background-image'].sizes.medium ) { #>
  201. <img class="attachment-thumb" src="{{ data.bg['background-image'].sizes.medium.url }}" draggable="false" alt="{{ data.alt_medium }}" />
  202. <# } else if ( data.bg['background-image'].sizes && data.bg['background-image'].sizes.full ) { #>
  203. <img class="attachment-thumb" src="{{ data.bg['background-image'].sizes.full.url }}" draggable="false" alt="{{ data.alt_full }}" />
  204. <# } #>
  205. </div>
  206. </div>
  207. </div>
  208. </div>
  209. <div class="actions">
  210. <# if ( data.canUpload ) { #>
  211. <button type="button" class="button remove-button">{{ data.button_labels.remove }}</button>
  212. <button type="button" class="button upload-button control-focus" id="{{ data.settings['default'] }}-button">{{ data.button_labels.change }}</button>
  213. <div style="clear:both"></div>
  214. <# } #>
  215. </div>
  216. <# } else { #>
  217. <div class="current">
  218. <div class="container">
  219. <div class="placeholder">
  220. <div class="inner">
  221. <span>
  222. {{ data.button_labels.placeholder }}
  223. </span>
  224. </div>
  225. </div>
  226. </div>
  227. </div>
  228. <div class="actions">
  229. <# if ( data.default ) { #>
  230. <button type="button" class="button default-button">{{ data.button_labels.default }}</button>
  231. <# } #>
  232. <# if ( data.canUpload ) { #>
  233. <button type="button" class="button upload-button" id="{{ data.settings['default'] }}-button">{{ data.button_labels.select }}</button>
  234. <# } #>
  235. <div style="clear:both"></div>
  236. </div>
  237. <# } #>
  238. <div class="background-grid">
  239. <# for ( key in data.selects ) { #>
  240. <# if ( data.show[ key ] ) { #>
  241. <div class="background-grid-el">
  242. <h5>{{ data.option_labels[ key ] }}</h5>
  243. <select id="{{ data.id }}-{{ key }}" data-key="{{ key }}">
  244. <# _.each( data.selects[ key ], function( val, opt_key ) { #>
  245. <option {{ data.bg[ key ] === opt_key ? 'selected' : '' }} value="{{ opt_key }}">{{ val }}</option>
  246. <# } ) #>
  247. </select>
  248. </div>
  249. <# } #>
  250. <# } #>
  251. <# if ( data.show['background-color'] ) { #>
  252. <div class="background-grid-el">
  253. <h5>{{ data.option_labels[ 'background-color' ] }}</h5>
  254. <input id="{{ data.id }}-color" type="text" data-default-color="{{ data.default[ 'background-color' ] }}" data-alpha="true" value="{{ data.bg[ 'background-color' ] }}" class="color-picker vamtam-bg-color" />
  255. </div>
  256. <# } #>
  257. </div>
  258. <?php
  259. }
  260. /**
  261. * Sanitize setting value
  262. *
  263. * @uses shortcode_atts to ensure that a fixed set of properties is saved for this setting
  264. */
  265. public static function sanitize_callback( $value ) {
  266. // must-have attributes
  267. $value = shortcode_atts( array(
  268. 'background-image' => '',
  269. 'background-color' => '',
  270. 'background-repeat' => '',
  271. 'background-attachment' => '',
  272. 'background-size' => '',
  273. 'background-position' => '',
  274. ), $value );
  275. // sanitize color and image
  276. $value['background-color'] = sanitize_hex_color( $value['background-color'] );
  277. $value['background-image'] = esc_url_raw( $value['background-image'] );
  278. // sanitize selects
  279. if ( ! in_array( $value['background-repeat'], array_keys( self::$selects['background-repeat'] ), true ) ) {
  280. $value['background-repeat'] = 'repeat';
  281. }
  282. if ( ! in_array( $value['background-attachment'], array_keys( self::$selects['background-attachment'] ), true ) ) {
  283. $value['background-attachment'] = 'scroll';
  284. }
  285. if ( ! in_array( $value['background-size'], array_keys( self::$selects['background-size'] ), true ) ) {
  286. $value['background-size'] = 'auto';
  287. }
  288. if ( ! in_array( $value['background-position'], array_keys( self::$selects['background-position'] ), true ) ) {
  289. $value['background-position'] = 'left top';
  290. }
  291. return $value;
  292. }
  293. }