コード例 #1
0
 /**
  * Constructor.
  *
  */
 public function __construct($manager, $id, $args = array())
 {
     //let the parent do what it has to
     parent::__construct($manager, $id, $args);
     //add specific js templates for this control
     //this is usually called in the manager for "registered" controls that need to be rendered with js
     //for this control, we'll do it another way because we need several js templates
     //=> that's why this control has not been "registered" and js templates are printed with the following action
     add_action('customize_controls_print_footer_scripts', array($this, 'hu_print_control_templates'), 1);
     //print the view content
     //callback declared in the child classes
     add_action('customize_controls_print_footer_scripts', array($this, 'hu_print_view_content_template'), 1);
 }
コード例 #2
0
 /**
  * Refresh the parameters passed to the JavaScript via JSON.
  *
  *
  * highly based on WP_Customize_Media_Control merged with WP_Customize_Upload_Control ::to_json()
  */
 public function to_json()
 {
     $this->json['mime_type'] = $this->mime_type;
     $this->json['button_labels'] = $this->button_labels;
     $this->json['bg_repeat_options'] = $this->bg_repeat_options;
     $this->json['bg_attachment_options'] = $this->bg_attachment_options;
     $this->json['bg_position_options'] = $this->bg_position_options;
     $this->json['canUpload'] = current_user_can('upload_files');
     $this->json['default_model'] = $this->default_model;
     $value = $this->value();
     if (isset($this->setting) && is_object($this->setting)) {
         $_defaults = isset($this->setting->default) ? $this->setting->default : null;
         $default_bg_img = isset($_defaults['background-image']) ? $_defaults['background-image'] : null;
     }
     $default_bg_im = isset($default_bg_img) ? $default_bg_img : null;
     if ($default_bg_img) {
         // Fake an attachment model - needs all fields used by template.
         // Note that the default value must be a URL, NOT an attachment ID.
         $type = in_array(substr($default_bg_img, -3), array('jpg', 'png', 'gif', 'bmp', 'svg')) ? 'image' : 'document';
         $default_attachment = array('id' => 1, 'url' => $default_bg_img, 'type' => $type, 'icon' => wp_mime_type_icon($type), 'title' => basename($default_bg_img));
         $default_attachment['sizes'] = array('full' => array('url' => $default_bg_img));
         $this->json['defaultAttachment'] = $default_attachment;
     }
     $background_image = isset($value['background-image']) ? $value['background-image'] : null;
     if ($background_image && $default_bg_img && $background_image === $default_bg_img) {
         // Set the default as the attachment.
         $this->json['attachment'] = $this->json['defaultAttachment'];
     } elseif ($background_image) {
         $attachment_id = attachment_url_to_postid($background_image);
         if ($attachment_id) {
             $this->json['attachment'] = wp_prepare_attachment_for_js($attachment_id);
         } else {
             //already an id
             $this->json['attachment'] = wp_prepare_attachment_for_js($background_image);
         }
     }
     parent::to_json();
 }