| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Class Pix_Customize_Background_Control |
| 5 |
*/ |
| 6 |
class Pix_Customize_Background_Control extends Pix_Customize_Control { |
| 7 |
public $type = 'custom_background'; |
| 8 |
|
| 9 |
public $value = array(); |
| 10 |
|
| 11 |
public $field = array( |
| 12 |
'class' => '' |
| 13 |
); |
| 14 |
|
| 15 |
public $field_defaults = array( |
| 16 |
'background-color' => true, |
| 17 |
'background-repeat' => true, |
| 18 |
'background-attachment' => true, |
| 19 |
'background-position' => true, |
| 20 |
'background-image' => true, |
| 21 |
'background-gradient' => false, |
| 22 |
'background-clip' => false, |
| 23 |
'background-origin' => false, |
| 24 |
'background-size' => true, |
| 25 |
'preview_media' => true, |
| 26 |
'preview' => true, |
| 27 |
'preview_height' => '200px', |
| 28 |
'transparent' => true, |
| 29 |
); |
| 30 |
|
| 31 |
/** |
| 32 |
* Render the control's content. |
| 33 |
*/ |
| 34 |
public function render_content() { |
| 35 |
|
| 36 |
// ensure some values |
| 37 |
$this->value = $this->value(); |
| 38 |
$this->field = array_merge( $this->field, $this->field_defaults ); |
| 39 |
|
| 40 |
if ( $this->field['background-image'] === true ) { |
| 41 |
|
| 42 |
if ( empty( $this->value['background-image'] ) && ! empty( $this->value['media']['id'] ) ) { |
| 43 |
$img = wp_get_attachment_image_src( $this->value['media']['id'], 'full' ); |
| 44 |
$this->value['background-image'] = $img[0]; |
| 45 |
$this->value['media']['width'] = $img[1]; |
| 46 |
$this->value['media']['height'] = $img[2]; |
| 47 |
} |
| 48 |
|
| 49 |
if ( ! is_array( $this->value ) || ! isset( $this->value['media'] ) || empty( $this->value['media'] ) ) { |
| 50 |
$this->value = array(); |
| 51 |
$media_array = array( |
| 52 |
'id' => '', |
| 53 |
'height' => '', |
| 54 |
'width' => '', |
| 55 |
'thumbnail' => '' |
| 56 |
); |
| 57 |
$this->value['media'] = $media_array; |
| 58 |
} |
| 59 |
|
| 60 |
if ( ! isset( $this->value['background-image'] ) ) { |
| 61 |
$this->value['background-image'] = ''; |
| 62 |
} |
| 63 |
|
| 64 |
$hide = 'hide '; |
| 65 |
|
| 66 |
if ( ( isset( $this->field['preview_media'] ) && $this->field['preview_media'] === false ) ) { |
| 67 |
$this->field['class'] .= " noPreview"; |
| 68 |
} |
| 69 |
|
| 70 |
if ( ( ! empty( $this->field['background-image'] ) && $this->field['background-image'] === true ) || isset( $this->field['preview'] ) && $this->field['preview'] === false ) { |
| 71 |
$hide = ''; |
| 72 |
} |
| 73 |
|
| 74 |
$placeholder = isset( $this->field['placeholder'] ) ? $this->field['placeholder'] : __( 'No media selected', 'customify' ); |
| 75 |
|
| 76 |
echo '<input type="text" |
| 77 |
class="customify_background_input background-image ' . $hide . 'upload ' . $this->field['class'] . '" |
| 78 |
name="' . esc_attr( $this->setting->id ) . '[background-image]" |
| 79 |
id="_customize-input-' . esc_attr( $this->setting->id ) . '[background-image]" |
| 80 |
value="' . $this->value['background-image'] . '" |
| 81 |
data-select_name="background-image" |
| 82 |
data-customize-setting-link="' . esc_attr( $this->setting->id ) . '[background-image]" |
| 83 |
placeholder="' . esc_attr( $placeholder ) . '"/>'; |
| 84 |
echo '<input type="hidden" |
| 85 |
class="upload-id background-media" |
| 86 |
name="' . esc_attr( $this->setting->id ) . '[media][id]" |
| 87 |
id="_customize-input-' . esc_attr( $this->setting->id ) . '[media][id]" |
| 88 |
value="' . esc_attr( $this->value['media']['id'] ) . '" />'; |
| 89 |
echo '<input type="hidden" |
| 90 |
class="upload-height background-media" |
| 91 |
name="' . esc_attr( $this->setting->id ) . '[media][height]" |
| 92 |
id="_customize-input-' . esc_attr( $this->setting->id ) . '[media][height]" |
| 93 |
value="' . esc_attr( $this->value['media']['height'] ) . '" />'; |
| 94 |
echo '<input type="hidden" |
| 95 |
class="upload-width background-media" |
| 96 |
name="' . esc_attr( $this->setting->id ) . '[media][width]" |
| 97 |
id="_customize-input-' . esc_attr( $this->setting->id ) . '[media][width]" |
| 98 |
value="' . esc_attr( $this->value['media']['width'] ) . '" />'; |
| 99 |
echo '<input type="hidden" |
| 100 |
class="upload-thumbnail background-media" |
| 101 |
name="' . esc_attr( $this->setting->id ) . '[media][thumbnail]" |
| 102 |
id="_customize-input-' . esc_attr( $this->setting->id ) . '[media][thumbnail]" |
| 103 |
value="' . esc_attr( $this->value['media']['thumbnail'] ) . '" />'; |
| 104 |
|
| 105 |
// Preview |
| 106 |
$hide = ''; |
| 107 |
|
| 108 |
if ( ( isset( $this->field['preview_media'] ) && $this->field['preview_media'] === false ) || empty( $this->value['background-image'] ) ) { |
| 109 |
$hide = 'hide '; |
| 110 |
} |
| 111 |
|
| 112 |
if ( empty( $this->value['media']['thumbnail'] ) && ! empty( $this->value['background-image'] ) ) { // Just in case |
| 113 |
if ( ! empty( $this->value['media']['id'] ) ) { |
| 114 |
$image = wp_get_attachment_image_src( $this->value['media']['id'] ); |
| 115 |
$this->value['media']['thumbnail'] = $image[0]; |
| 116 |
} else { |
| 117 |
$this->value['media']['thumbnail'] = $this->value['background-image']; |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
echo '<div class="' . $hide . 'preview_screenshot">'; |
| 122 |
echo '<a class="of-uploaded-image" href="' . $this->value['background-image'] . '" target="_blank">'; |
| 123 |
echo '<img class="preview_image" id="image_' . $this->value['media']['id'] . '" src="' . $this->value['media']['thumbnail'] . '" alt="" target="_blank" rel="external" />'; |
| 124 |
echo '</a>'; |
| 125 |
echo '</div>'; |
| 126 |
|
| 127 |
//Upload controls DIV |
| 128 |
echo '<div class="upload_button_div">'; |
| 129 |
|
| 130 |
//If the user has WP3.5+ show upload/remove button |
| 131 |
echo '<span class="button background_upload_button" id="' . esc_attr( $this->id ) . '-media" data-setting_id="' . esc_attr( $this->setting->id ) . '" >' . esc_html__( 'Upload', 'customify' ) . '</span>'; |
| 132 |
|
| 133 |
$hide = ''; |
| 134 |
if ( empty( $this->value['background-image'] ) || $this->value['background-image'] == '' ) { |
| 135 |
$hide = ' hide'; |
| 136 |
} |
| 137 |
|
| 138 |
echo '<span class="button remove-image' . $hide . '" id="reset_' . esc_attr( $this->id ) . '" rel="' . esc_attr( $this->id ) . '">' . esc_html__( 'Remove', 'customify' ) . '</span>'; |
| 139 |
|
| 140 |
echo '</div>'; |
| 141 |
} |
| 142 |
|
| 143 |
if ( $this->field['background-repeat'] === true ) { |
| 144 |
$array = array( |
| 145 |
'no-repeat' => 'No Repeat', |
| 146 |
'repeat' => 'Repeat All', |
| 147 |
'repeat-x' => 'Repeat Horizontally', |
| 148 |
'repeat-y' => 'Repeat Vertically', |
| 149 |
'inherit' => 'Inherit', |
| 150 |
); |
| 151 |
|
| 152 |
if ( ! isset( $this->value['background-repeat'] ) ) { |
| 153 |
$this->value['background-repeat'] = ''; |
| 154 |
} |
| 155 |
|
| 156 |
echo '<select id="' . $this->id . '-repeat-select" |
| 157 |
name="_customize-input-' . esc_attr( $this->setting->id ) . '[background-repeat]" |
| 158 |
class="customify_background_select ' . $this->field['class'] . ' ' . $hide . '" |
| 159 |
data-select_name="background-repeat" |
| 160 |
data-customize-setting-link="' . esc_attr( $this->setting->id ) . '[background-repeat]">'; |
| 161 |
echo '<option disabled ' . selected( $this->value['background-repeat'], '', false ) . '>' . esc_html__( 'Background repeat..', 'customify' ) . '</option>'; |
| 162 |
foreach ( $array as $k => $v ) { |
| 163 |
echo '<option value="' . $k . '"' . selected( $this->value['background-repeat'], $k, false ) . '>' . $v . '</option>'; |
| 164 |
} |
| 165 |
echo '</select>'; |
| 166 |
} |
| 167 |
|
| 168 |
if ( $this->field['background-clip'] === true ) { |
| 169 |
$array = array( |
| 170 |
'inherit' => 'Inherit', |
| 171 |
'border-box' => 'Border Box', |
| 172 |
'content-box' => 'Content Box', |
| 173 |
'padding-box' => 'Padding Box', |
| 174 |
); |
| 175 |
|
| 176 |
if ( ! isset( $this->value['background-clip'] ) ) { |
| 177 |
$this->value['background-clip'] = ''; |
| 178 |
} |
| 179 |
|
| 180 |
echo '<select id="' . $this->id . '-clip-select" |
| 181 |
name="_customize-input-' . esc_attr( $this->setting->id ) . '[background-clip]" |
| 182 |
class="customify_background_select ' . $this->field['class'] . ' ' . $hide . '" |
| 183 |
data-select_name="background-clip" |
| 184 |
data-customize-setting-link="' . esc_attr( $this->setting->id ) . '[background-clip]">'; |
| 185 |
echo '<option disabled ' . selected( $this->value['background-clip'], '', false ) . '>' . esc_html__( 'Background clip..', 'customify' ) . '</option>'; |
| 186 |
foreach ( $array as $k => $v ) { |
| 187 |
echo '<option value="' . $k . '"' . selected( $this->value['background-clip'], $k, false ) . '>' . $v . '</option>'; |
| 188 |
} |
| 189 |
echo '</select>'; |
| 190 |
} |
| 191 |
|
| 192 |
if ( $this->field['background-origin'] === true ) { |
| 193 |
$array = array( |
| 194 |
'inherit' => 'Inherit', |
| 195 |
'border-box' => 'Border Box', |
| 196 |
'content-box' => 'Content Box', |
| 197 |
'padding-box' => 'Padding Box', |
| 198 |
); |
| 199 |
|
| 200 |
if ( ! isset( $this->value['background-origin'] ) ) { |
| 201 |
$this->value['background-origin'] = ''; |
| 202 |
} |
| 203 |
|
| 204 |
echo '<select id="' . $this->id . '-origin-select" |
| 205 |
name="_customize-input-' . esc_attr( $this->setting->id ) . '[background-origin]" |
| 206 |
class="customify_background_select ' . $this->field['class'] . ' ' . $hide . '" |
| 207 |
data-select_name="background-origin" |
| 208 |
data-customize-setting-link="' . esc_attr( $this->setting->id ) . '[background-origin]">'; |
| 209 |
echo '<option disabled ' . selected( $this->value['background-origin'], '', false ) . '>' . esc_html__( 'Background origin..', 'customify' ) . '</option>'; |
| 210 |
foreach ( $array as $k => $v ) { |
| 211 |
echo '<option value="' . $k . '"' . selected( $this->value['background-origin'], $k, false ) . '>' . $v . '</option>'; |
| 212 |
} |
| 213 |
echo '</select>'; |
| 214 |
} |
| 215 |
|
| 216 |
if ( $this->field['background-size'] === true ) { |
| 217 |
$array = array( |
| 218 |
'inherit' => 'Inherit', |
| 219 |
'cover' => 'Cover', |
| 220 |
'contain' => 'Contain', |
| 221 |
); |
| 222 |
|
| 223 |
if ( ! isset( $this->value['background-size'] ) ) { |
| 224 |
$this->value['background-size'] = ''; |
| 225 |
} |
| 226 |
|
| 227 |
echo '<select id="' . $this->id . '-size-select" |
| 228 |
name="_customize-input-' . esc_attr( $this->setting->id ) . '[background-size]" |
| 229 |
class="customify_background_select ' . $this->field['class'] . ' ' . $hide . '" |
| 230 |
data-select_name="background-size" |
| 231 |
data-customize-setting-link="' . esc_attr( $this->setting->id ) . '[background-size]">'; |
| 232 |
echo '<option disabled ' . selected( $this->value['background-size'], '', false ) . '>' . esc_html__( 'Background size..', 'customify' ) . '</option>'; |
| 233 |
foreach ( $array as $k => $v ) { |
| 234 |
echo '<option value="' . $k . '"' . selected( $this->value['background-size'], $k, false ) . '>' . $v . '</option>'; |
| 235 |
} |
| 236 |
echo '</select>'; |
| 237 |
} |
| 238 |
|
| 239 |
if ( $this->field['background-attachment'] === true ) { |
| 240 |
$array = array( |
| 241 |
'fixed' => 'Fixed', |
| 242 |
'scroll' => 'Scroll', |
| 243 |
'inherit' => 'Inherit', |
| 244 |
); |
| 245 |
|
| 246 |
if ( ! isset( $this->value['background-attachment'] ) ) { |
| 247 |
$this->value['background-attachment'] = ''; |
| 248 |
} |
| 249 |
|
| 250 |
echo '<select id="' . $this->id . '-attachment-select" name="_customize-input-' . esc_attr( $this->setting->id ) . '[background-attachment]" class="customify_background_select ' . $this->field['class'] . ' ' . $hide . '" data-select_name="background-attachment" data-customize-setting-link="' . esc_attr( $this->setting->id ) . '[background-attachment]">'; |
| 251 |
echo '<option disabled ' . selected( $this->value['background-attachment'], '', false ) . '>' . esc_html__( 'Background attachment..', 'customify' ) . '</option>'; |
| 252 |
foreach ( $array as $k => $v ) { |
| 253 |
echo '<option value="' . $k . '"' . selected( $this->value['background-attachment'], $k, false ) . '>' . $v . '</option>'; |
| 254 |
} |
| 255 |
echo '</select>'; |
| 256 |
} |
| 257 |
|
| 258 |
if ( $this->field['background-position'] === true ) { |
| 259 |
$array = array( |
| 260 |
'left top' => 'Left Top', |
| 261 |
'left center' => 'Left center', |
| 262 |
'left bottom' => 'Left Bottom', |
| 263 |
'center top' => 'Center Top', |
| 264 |
'center center' => 'Center Center', |
| 265 |
'center bottom' => 'Center Bottom', |
| 266 |
'right top' => 'Right Top', |
| 267 |
'right center' => 'Right center', |
| 268 |
'right bottom' => 'Right Bottom', |
| 269 |
); |
| 270 |
|
| 271 |
if ( ! isset( $this->value['background-position'] ) ) { |
| 272 |
$this->value['background-position'] = ''; |
| 273 |
} |
| 274 |
|
| 275 |
echo '<select id="' . $this->id . '-position-select" |
| 276 |
name="_customize-input-' . esc_attr( $this->setting->id ) . '[background-position]" |
| 277 |
class="customify_background_select ' . $this->field['class'] . ' ' . $hide . '" |
| 278 |
placeholder="Background position?" |
| 279 |
data-select_name="background-position" |
| 280 |
data-customize-setting-link="' . esc_attr( $this->setting->id ) . '[background-position]">'; |
| 281 |
echo '<option disabled ' . selected( $this->value['background-position'], '', false ) . '>' . esc_html__( 'Background position..', 'customify' ) . '</option>'; |
| 282 |
foreach ( $array as $k => $v ) { |
| 283 |
echo '<option value="' . $k . '"' . selected( $this->value['background-position'], $k, false ) . '>' . $v . '</option>'; |
| 284 |
} |
| 285 |
echo '</select>'; |
| 286 |
} |
| 287 |
} |
| 288 |
} |
| 289 |
|