| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Redux Framework is free software: you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU General Public License as published by |
| 6 |
* the Free Software Foundation, either version 2 of the License, or |
| 7 |
* any later version. |
| 8 |
* Redux Framework is distributed in the hope that it will be useful, |
| 9 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
* GNU General Public License for more details. |
| 12 |
* You should have received a copy of the GNU General Public License |
| 13 |
* along with Redux Framework. If not, see <http://www.gnu.org/licenses/>. |
| 14 |
* |
| 15 |
* @package ReduxFrameworkLegacy |
| 16 |
* @subpackage Field_Background |
| 17 |
* @author Dovy Paukstys |
| 18 |
* @version 3.1.5 |
| 19 |
*/ |
| 20 |
// Exit if accessed directly |
| 21 |
if ( ! defined( 'ABSPATH' ) ) { |
| 22 |
exit; |
| 23 |
} |
| 24 |
|
| 25 |
// Don't duplicate me! |
| 26 |
if ( ! class_exists( 'ReduxFrameworkLegacy_background' ) ) { |
| 27 |
|
| 28 |
/** |
| 29 |
* Main ReduxFrameworkLegacy_background class |
| 30 |
* |
| 31 |
* @since 3.1.5 |
| 32 |
*/ |
| 33 |
class ReduxFrameworkLegacy_background { |
| 34 |
|
| 35 |
/** |
| 36 |
* Field Constructor. |
| 37 |
* Required - must call the parent constructor, then assign field and value to vars, and obviously call the render field function |
| 38 |
* |
| 39 |
* @since 3.1.5 |
| 40 |
* @access public |
| 41 |
* @return void |
| 42 |
*/ |
| 43 |
function __construct( $field = array(), $value = '', $parent = null ) { |
| 44 |
|
| 45 |
$this->parent = $parent; |
| 46 |
$this->field = $field; |
| 47 |
$this->value = $value; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Field Render Function. |
| 52 |
* Takes the vars and outputs the HTML for the field in the settings |
| 53 |
* |
| 54 |
* @since 1.0.0 |
| 55 |
* @access public |
| 56 |
* @return void |
| 57 |
*/ |
| 58 |
public function render() { |
| 59 |
|
| 60 |
$defaults = array( |
| 61 |
'background-color' => true, |
| 62 |
'background-repeat' => true, |
| 63 |
'background-attachment' => true, |
| 64 |
'background-position' => true, |
| 65 |
'background-image' => true, |
| 66 |
'background-gradient' => false, |
| 67 |
'background-clip' => false, |
| 68 |
'background-origin' => false, |
| 69 |
'background-size' => true, |
| 70 |
'preview_media' => false, |
| 71 |
'preview' => true, |
| 72 |
'preview_height' => '200px', |
| 73 |
'transparent' => true, |
| 74 |
); |
| 75 |
|
| 76 |
$this->field = wp_parse_args( $this->field, $defaults ); |
| 77 |
|
| 78 |
// No errors please |
| 79 |
$defaults = array( |
| 80 |
'background-color' => '', |
| 81 |
'background-repeat' => '', |
| 82 |
'background-attachment' => '', |
| 83 |
'background-position' => '', |
| 84 |
'background-image' => '', |
| 85 |
'background-clip' => '', |
| 86 |
'background-origin' => '', |
| 87 |
'background-size' => '', |
| 88 |
'media' => array(), |
| 89 |
); |
| 90 |
|
| 91 |
$this->value = wp_parse_args( $this->value, $defaults ); |
| 92 |
|
| 93 |
$defaults = array( |
| 94 |
'id' => '', |
| 95 |
'width' => '', |
| 96 |
'height' => '', |
| 97 |
'thumbnail' => '', |
| 98 |
); |
| 99 |
|
| 100 |
$this->value['media'] = wp_parse_args( $this->value['media'], $defaults ); |
| 101 |
|
| 102 |
// select2 args |
| 103 |
if ( isset( $this->field['select2'] ) ) { // if there are any let's pass them to js |
| 104 |
$select2_params = json_encode( $this->field['select2'] ); |
| 105 |
$select2_params = htmlspecialchars( $select2_params, ENT_QUOTES ); |
| 106 |
|
| 107 |
echo '<input type="hidden" class="select2_params" value="' . $select2_params . '">'; |
| 108 |
} |
| 109 |
|
| 110 |
if ( $this->field['background-color'] === true ) { |
| 111 |
|
| 112 |
if ( isset( $this->value['color'] ) && empty( $this->value['background-color'] ) ) { |
| 113 |
$this->value['background-color'] = $this->value['color']; |
| 114 |
} |
| 115 |
|
| 116 |
echo '<input data-id="' . $this->field['id'] . '" name="' . $this->field['name'] . $this->field['name_suffix'] . '[background-color]" id="' . $this->field['id'] . '-color" class="redux-color redux-background-input redux-color-init ' . $this->field['class'] . '" type="text" value="' . $this->value['background-color'] . '" data-default-color="' . ( isset( $this->field['default']['background-color'] ) ? $this->field['default']['background-color'] : '' ) . '" />'; |
| 117 |
echo '<input type="hidden" class="redux-saved-color" id="' . $this->field['id'] . '-saved-color' . '" value="">'; |
| 118 |
|
| 119 |
if ( ! isset( $this->field['transparent'] ) || $this->field['transparent'] !== false ) { |
| 120 |
$tChecked = ''; |
| 121 |
if ( $this->value['background-color'] == 'transparent' ) { |
| 122 |
$tChecked = ' checked="checked"'; |
| 123 |
} |
| 124 |
echo '<label for="' . $this->field['id'] . '-transparency" class="color-transparency-check"><input type="checkbox" class="checkbox color-transparency redux-background-input ' . $this->field['class'] . '" id="' . $this->field['id'] . '-transparency" data-id="' . $this->field['id'] . '-color" value="1"' . $tChecked . '> ' . __( 'Transparent', 'redux-framework' ) . '</label>'; |
| 125 |
} |
| 126 |
|
| 127 |
if ( $this->field['background-repeat'] === true || $this->field['background-position'] === true || $this->field['background-attachment'] === true ) { |
| 128 |
echo '<br />'; |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
if ( $this->field['background-repeat'] === true ) { |
| 133 |
$array = array( |
| 134 |
'no-repeat' => 'No Repeat', |
| 135 |
'repeat' => 'Repeat All', |
| 136 |
'repeat-x' => 'Repeat Horizontally', |
| 137 |
'repeat-y' => 'Repeat Vertically', |
| 138 |
'inherit' => 'Inherit', |
| 139 |
); |
| 140 |
echo '<select id="' . $this->field['id'] . '-repeat-select" data-placeholder="' . __( 'Background Repeat', 'redux-framework' ) . '" name="' . $this->field['name'] . $this->field['name_suffix'] . '[background-repeat]" class="redux-select-item redux-background-input redux-background-repeat ' . $this->field['class'] . '">'; |
| 141 |
echo '<option></option>'; |
| 142 |
|
| 143 |
foreach ( $array as $k => $v ) { |
| 144 |
echo '<option value="' . $k . '"' . selected( $this->value['background-repeat'], $k, false ) . '>' . $v . '</option>'; |
| 145 |
} |
| 146 |
echo '</select>'; |
| 147 |
} |
| 148 |
|
| 149 |
if ( $this->field['background-clip'] === true ) { |
| 150 |
$array = array( |
| 151 |
'inherit' => 'Inherit', |
| 152 |
'border-box' => 'Border Box', |
| 153 |
'content-box' => 'Content Box', |
| 154 |
'padding-box' => 'Padding Box', |
| 155 |
); |
| 156 |
echo '<select id="' . $this->field['id'] . '-clip-select" data-placeholder="' . __( 'Background Clip', 'redux-framework' ) . '" name="' . $this->field['name'] . $this->field['name_suffix'] . '[background-clip]" class="redux-select-item redux-background-input redux-background-clip ' . $this->field['class'] . '">'; |
| 157 |
echo '<option></option>'; |
| 158 |
|
| 159 |
foreach ( $array as $k => $v ) { |
| 160 |
echo '<option value="' . $k . '"' . selected( $this->value['background-clip'], $k, false ) . '>' . $v . '</option>'; |
| 161 |
} |
| 162 |
echo '</select>'; |
| 163 |
} |
| 164 |
|
| 165 |
if ( $this->field['background-origin'] === true ) { |
| 166 |
$array = array( |
| 167 |
'inherit' => 'Inherit', |
| 168 |
'border-box' => 'Border Box', |
| 169 |
'content-box' => 'Content Box', |
| 170 |
'padding-box' => 'Padding Box', |
| 171 |
); |
| 172 |
echo '<select id="' . $this->field['id'] . '-origin-select" data-placeholder="' . __( 'Background Origin', 'redux-framework' ) . '" name="' . $this->field['name'] . $this->field['name_suffix'] . '[background-origin]" class="redux-select-item redux-background-input redux-background-origin ' . $this->field['class'] . '">'; |
| 173 |
echo '<option></option>'; |
| 174 |
|
| 175 |
foreach ( $array as $k => $v ) { |
| 176 |
echo '<option value="' . $k . '"' . selected( $this->value['background-origin'], $k, false ) . '>' . $v . '</option>'; |
| 177 |
} |
| 178 |
echo '</select>'; |
| 179 |
} |
| 180 |
|
| 181 |
if ( $this->field['background-size'] === true ) { |
| 182 |
$array = array( |
| 183 |
'inherit' => 'Inherit', |
| 184 |
'cover' => 'Cover', |
| 185 |
'contain' => 'Contain', |
| 186 |
); |
| 187 |
echo '<select id="' . $this->field['id'] . '-size-select" data-placeholder="' . __( 'Background Size', 'redux-framework' ) . '" name="' . $this->field['name'] . $this->field['name_suffix'] . '[background-size]" class="redux-select-item redux-background-input redux-background-size ' . $this->field['class'] . '">'; |
| 188 |
echo '<option></option>'; |
| 189 |
|
| 190 |
foreach ( $array as $k => $v ) { |
| 191 |
echo '<option value="' . $k . '"' . selected( $this->value['background-size'], $k, false ) . '>' . $v . '</option>'; |
| 192 |
} |
| 193 |
echo '</select>'; |
| 194 |
} |
| 195 |
|
| 196 |
if ( $this->field['background-attachment'] === true ) { |
| 197 |
$array = array( |
| 198 |
'fixed' => 'Fixed', |
| 199 |
'scroll' => 'Scroll', |
| 200 |
'inherit' => 'Inherit', |
| 201 |
); |
| 202 |
echo '<select id="' . $this->field['id'] . '-attachment-select" data-placeholder="' . __( 'Background Attachment', 'redux-framework' ) . '" name="' . $this->field['name'] . $this->field['name_suffix'] . '[background-attachment]" class="redux-select-item redux-background-input redux-background-attachment ' . $this->field['class'] . '">'; |
| 203 |
echo '<option></option>'; |
| 204 |
foreach ( $array as $k => $v ) { |
| 205 |
echo '<option value="' . $k . '"' . selected( $this->value['background-attachment'], $k, false ) . '>' . $v . '</option>'; |
| 206 |
} |
| 207 |
echo '</select>'; |
| 208 |
} |
| 209 |
|
| 210 |
if ( $this->field['background-position'] === true ) { |
| 211 |
$array = array( |
| 212 |
'left top' => 'Left Top', |
| 213 |
'left center' => 'Left center', |
| 214 |
'left bottom' => 'Left Bottom', |
| 215 |
'center top' => 'Center Top', |
| 216 |
'center center' => 'Center Center', |
| 217 |
'center bottom' => 'Center Bottom', |
| 218 |
'right top' => 'Right Top', |
| 219 |
'right center' => 'Right center', |
| 220 |
'right bottom' => 'Right Bottom', |
| 221 |
); |
| 222 |
echo '<select id="' . $this->field['id'] . '-position-select" data-placeholder="' . __( 'Background Position', 'redux-framework' ) . '" name="' . $this->field['name'] . $this->field['name_suffix'] . '[background-position]" class="redux-select-item redux-background-input redux-background-position ' . $this->field['class'] . '">'; |
| 223 |
echo '<option></option>'; |
| 224 |
|
| 225 |
foreach ( $array as $k => $v ) { |
| 226 |
echo '<option value="' . $k . '"' . selected( $this->value['background-position'], $k, false ) . '>' . $v . '</option>'; |
| 227 |
} |
| 228 |
echo '</select>'; |
| 229 |
} |
| 230 |
|
| 231 |
if ( $this->field['background-image'] === true ) { |
| 232 |
echo '<br />'; |
| 233 |
|
| 234 |
if ( empty( $this->value ) && ! empty( $this->field['default'] ) ) { // If there are standard values and value is empty |
| 235 |
if ( is_array( $this->field['default'] ) ) { |
| 236 |
if ( ! empty( $this->field['default']['media']['id'] ) ) { |
| 237 |
$this->value['media']['id'] = $this->field['default']['media']['id']; |
| 238 |
} elseif ( ! empty( $this->field['default']['id'] ) ) { |
| 239 |
$this->value['media']['id'] = $this->field['default']['id']; |
| 240 |
} |
| 241 |
|
| 242 |
if ( ! empty( $this->field['default']['url'] ) ) { |
| 243 |
$this->value['background-image'] = $this->field['default']['url']; |
| 244 |
} elseif ( ! empty( $this->field['default']['media']['url'] ) ) { |
| 245 |
$this->value['background-image'] = $this->field['default']['media']['url']; |
| 246 |
} elseif ( ! empty( $this->field['default']['background-image'] ) ) { |
| 247 |
$this->value['background-image'] = $this->field['default']['background-image']; |
| 248 |
} |
| 249 |
} elseif ( is_numeric( $this->field['default'] ) ) { |
| 250 |
// Check if it's an attachment ID |
| 251 |
$this->value['media']['id'] = $this->field['default']; |
| 252 |
} else { // Must be a URL |
| 253 |
$this->value['background-image'] = $this->field['default']; |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
if ( empty( $this->value['background-image'] ) && ! empty( $this->value['media']['id'] ) ) { |
| 258 |
$img = wp_get_attachment_image_src( $this->value['media']['id'], 'full' ); |
| 259 |
$this->value['background-image'] = $img[0]; |
| 260 |
$this->value['media']['width'] = $img[1]; |
| 261 |
$this->value['media']['height'] = $img[2]; |
| 262 |
} |
| 263 |
|
| 264 |
$hide = 'hide '; |
| 265 |
|
| 266 |
if ( ( isset( $this->field['preview_media'] ) && $this->field['preview_media'] === false ) ) { |
| 267 |
$this->field['class'] .= ' noPreview'; |
| 268 |
} |
| 269 |
|
| 270 |
if ( ( ! empty( $this->field['background-image'] ) && $this->field['background-image'] === true ) || isset( $this->field['preview'] ) && $this->field['preview'] === false ) { |
| 271 |
$hide = ''; |
| 272 |
} |
| 273 |
|
| 274 |
$placeholder = isset( $this->field['placeholder'] ) ? $this->field['placeholder'] : __( 'No media selected', 'redux-framework' ); |
| 275 |
|
| 276 |
echo '<input placeholder="' . $placeholder . '" type="text" class="redux-background-input ' . $hide . 'upload ' . $this->field['class'] . '" name="' . $this->field['name'] . $this->field['name_suffix'] . '[background-image]" id="' . $this->parent->args['opt_name'] . '-' . $this->field['id'] . '-background-image" value="' . $this->value['background-image'] . '" />'; |
| 277 |
echo '<input type="hidden" class="upload-id ' . $this->field['class'] . '" name="' . $this->field['name'] . $this->field['name_suffix'] . '[media][id]" id="' . $this->parent->args['opt_name'] . '-' . $this->field['id'] . '-media-id" value="' . $this->value['media']['id'] . '" />'; |
| 278 |
echo '<input type="hidden" class="upload-height" name="' . $this->field['name'] . $this->field['name_suffix'] . '[media][height]" id="' . $this->parent->args['opt_name'] . '-' . $this->field['id'] . '-media-height" value="' . $this->value['media']['height'] . '" />'; |
| 279 |
echo '<input type="hidden" class="upload-width" name="' . $this->field['name'] . $this->field['name_suffix'] . '[media][width]" id="' . $this->parent->args['opt_name'] . '-' . $this->field['id'] . '-media-width" value="' . $this->value['media']['width'] . '" />'; |
| 280 |
echo '<input type="hidden" class="upload-thumbnail" name="' . $this->field['name'] . $this->field['name_suffix'] . '[media][thumbnail]" id="' . $this->parent->args['opt_name'] . '-' . $this->field['id'] . '-media-thumbnail" value="' . $this->value['media']['thumbnail'] . '" />'; |
| 281 |
|
| 282 |
// Preview |
| 283 |
$hide = ''; |
| 284 |
|
| 285 |
if ( ( isset( $this->field['preview_media'] ) && $this->field['preview_media'] === false ) || empty( $this->value['background-image'] ) ) { |
| 286 |
$hide = 'hide '; |
| 287 |
} |
| 288 |
|
| 289 |
if ( empty( $this->value['media']['thumbnail'] ) && ! empty( $this->value['background-image'] ) ) { // Just in case |
| 290 |
if ( ! empty( $this->value['media']['id'] ) ) { |
| 291 |
$image = wp_get_attachment_image_src( |
| 292 |
$this->value['media']['id'], |
| 293 |
array( |
| 294 |
150, |
| 295 |
150, |
| 296 |
) |
| 297 |
); |
| 298 |
$this->value['media']['thumbnail'] = $image[0]; |
| 299 |
} else { |
| 300 |
$this->value['media']['thumbnail'] = $this->value['background-image']; |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
echo '<div class="' . $hide . 'screenshot">'; |
| 305 |
echo '<a class="of-uploaded-image" href="' . $this->value['background-image'] . '" target="_blank">'; |
| 306 |
echo '<img class="redux-option-image" id="image_' . $this->value['media']['id'] . '" src="' . $this->value['media']['thumbnail'] . '" alt="" target="_blank" rel="external" />'; |
| 307 |
echo '</a>'; |
| 308 |
echo '</div>'; |
| 309 |
|
| 310 |
// Upload controls DIV |
| 311 |
echo '<div class="upload_button_div">'; |
| 312 |
|
| 313 |
// If the user has WP3.5+ show upload/remove button |
| 314 |
echo '<span class="button redux-background-upload" id="' . $this->field['id'] . '-media">' . __( 'Upload', 'redux-framework' ) . '</span>'; |
| 315 |
|
| 316 |
$hide = ''; |
| 317 |
if ( empty( $this->value['background-image'] ) || $this->value['background-image'] == '' ) { |
| 318 |
$hide = ' hide'; |
| 319 |
} |
| 320 |
|
| 321 |
echo '<span class="button removeCSS redux-remove-background' . $hide . '" id="reset_' . $this->field['id'] . '" rel="' . $this->field['id'] . '">' . __( 'Remove', 'redux-framework' ) . '</span>'; |
| 322 |
|
| 323 |
echo '</div>'; |
| 324 |
} |
| 325 |
|
| 326 |
/** |
| 327 |
* Preview |
| 328 |
* */ |
| 329 |
if ( ! isset( $this->field['preview'] ) || $this->field['preview'] !== false ) : |
| 330 |
|
| 331 |
$css = $this->getCSS(); |
| 332 |
if ( empty( $css ) ) { |
| 333 |
$css = 'display:none;'; |
| 334 |
} |
| 335 |
$css .= 'height: ' . $this->field['preview_height'] . ';'; |
| 336 |
echo '<p class="clear ' . $this->field['id'] . '_previewer background-preview" style="' . $css . '"> </p>'; |
| 337 |
|
| 338 |
endif; |
| 339 |
} |
| 340 |
|
| 341 |
/** |
| 342 |
* Enqueue Function. |
| 343 |
* If this field requires any scripts, or css define this function and register/enqueue the scripts/css |
| 344 |
* |
| 345 |
* @since 1.0.0 |
| 346 |
* @access public |
| 347 |
* @return void |
| 348 |
*/ |
| 349 |
public function enqueue() { |
| 350 |
if ( function_exists( 'wp_enqueue_media' ) ) { |
| 351 |
wp_enqueue_media(); |
| 352 |
} elseif ( ! wp_script_is( 'media-upload' ) ) { |
| 353 |
wp_enqueue_script( 'media-upload' ); |
| 354 |
} |
| 355 |
|
| 356 |
if ( ! wp_style_is( 'select2-css' ) ) { |
| 357 |
wp_enqueue_style( 'select2-css' ); |
| 358 |
} |
| 359 |
|
| 360 |
if ( ! wp_style_is( 'wp-color-picker' ) ) { |
| 361 |
wp_enqueue_style( 'wp-color-picker' ); |
| 362 |
} |
| 363 |
|
| 364 |
if ( ! wp_script_is( 'redux-field-background-js' ) ) { |
| 365 |
wp_enqueue_script( |
| 366 |
'redux-field-background-js', |
| 367 |
ReduxFrameworkLegacy::$_url . 'inc/fields/background/field_background' . ReduxLegacy_Functions::isMin() . '.js', |
| 368 |
array( 'jquery', 'wp-color-picker', 'select2-js', 'redux-js' ), |
| 369 |
time(), |
| 370 |
true |
| 371 |
); |
| 372 |
} |
| 373 |
|
| 374 |
if ( $this->parent->args['dev_mode'] ) { |
| 375 |
if ( ! wp_style_is( 'redux-field-background-css' ) ) { |
| 376 |
wp_enqueue_style( |
| 377 |
'redux-field-background-css', |
| 378 |
ReduxFrameworkLegacy::$_url . 'inc/fields/background/field_background.css', |
| 379 |
array(), |
| 380 |
time(), |
| 381 |
'all' |
| 382 |
); |
| 383 |
} |
| 384 |
|
| 385 |
if ( ! wp_style_is( 'redux-color-picker-css' ) ) { |
| 386 |
wp_enqueue_style( 'redux-color-picker-css' ); |
| 387 |
} |
| 388 |
} |
| 389 |
} |
| 390 |
|
| 391 |
public static function getCSS( $value = array() ) { |
| 392 |
|
| 393 |
$css = ''; |
| 394 |
|
| 395 |
if ( ! empty( $value ) && is_array( $value ) ) { |
| 396 |
foreach ( $value as $key => $value ) { |
| 397 |
if ( ! empty( $value ) && $key != 'media' ) { |
| 398 |
if ( $key == 'background-image' ) { |
| 399 |
$css .= $key . ":url('" . $value . "');"; |
| 400 |
} else { |
| 401 |
$css .= $key . ':' . $value . ';'; |
| 402 |
} |
| 403 |
} |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
return $css; |
| 408 |
} |
| 409 |
|
| 410 |
public function output() { |
| 411 |
$style = $this->getCSS( $this->value ); |
| 412 |
|
| 413 |
if ( ! empty( $style ) ) { |
| 414 |
|
| 415 |
if ( ! empty( $this->field['output'] ) && is_array( $this->field['output'] ) ) { |
| 416 |
$keys = implode( ',', $this->field['output'] ); |
| 417 |
$this->parent->outputCSS .= $keys . '{' . $style . '}'; |
| 418 |
} |
| 419 |
|
| 420 |
if ( ! empty( $this->field['compiler'] ) && is_array( $this->field['compiler'] ) ) { |
| 421 |
$keys = implode( ',', $this->field['compiler'] ); |
| 422 |
$this->parent->compilerCSS .= $keys . '{' . $style . '}'; |
| 423 |
} |
| 424 |
} |
| 425 |
} |
| 426 |
} |
| 427 |
} |
| 428 |
|