.
*
* @package ReduxFramework
* @subpackage Field_Background
* @author Dovy Paukstys
* @version 3.1.5
*/
// Exit if accessed directly
if( !defined( 'ABSPATH' ) ) exit;
// Don't duplicate me!
if( !class_exists( 'ReduxFramework_background' ) ) {
/**
* Main ReduxFramework_background class
*
* @since 3.1.5
*/
class ReduxFramework_background extends ReduxFramework {
/**
* Field Constructor.
*
* Required - must call the parent constructor, then assign field and value to vars, and obviously call the render field function
*
* @since 3.1.5
* @access public
* @return void
*/
function __construct( $field = array(), $value ='', $parent ) {
parent::__construct( $parent->sections, $parent->args );
$this->parent = $parent;
$this->field = $field;
$this->value = $value;
}
/**
* Field Render Function.
*
* Takes the vars and outputs the HTML for the field in the settings
*
* @since 1.0.0
* @access public
* @return void
*/
public function render() {
$defaults = array(
'background-color' => true,
'background-repeat' => true,
'background-attachment' => true,
'background-position' => true,
'background-image' => true,
'background-gradient' => false,
'background-clip' => false,
'background-origin' => false,
'background-size' => true,
'preview_media' => false,
'preview' => true,
'preview_height' => '200px',
'transparent' => true,
);
$this->field = wp_parse_args( $this->field, $defaults );
// No errors please
$defaults = array(
'background-color' => '',
'background-repeat' => '',
'background-attachment' => '',
'background-position' => '',
'background-image' => '',
'background-clip' => '',
'background-origin' => '',
'background-size' => '',
'media' => array(),
);
$this->value = wp_parse_args( $this->value, $defaults );
$defaults = array(
'id' => '',
'width' => '',
'height' => '',
'thumbnail' => '',
);
$this->value['media'] = wp_parse_args( $this->value['media'], $defaults );
if ($this->field['background-color'] === true) {
if (isset($this->value['color']) && empty($this->value['background-color'])) {
$this->value['background-color'] = $this->value['color'];
}
echo '';
if ( !isset( $this->field['transparent'] ) || $this->field['transparent'] !== false ) {
$tChecked = "";
if ( $this->value == "transparent" ) {
$tChecked = ' checked="checked"';
}
echo '';
}
if ( $this->field['background-repeat'] === true || $this->field['background-position'] === true || $this->field['background-attachment'] === true ) {
echo '
';
}
}
if ( $this->field['background-repeat'] === true ) {
$array = array(
'no-repeat' => 'No Repeat',
'repeat' => 'Repeat All',
'repeat-x' => 'Repeat Horizontally',
'repeat-y' => 'Repeat Vertically',
'inherit' => 'Inherit',
);
echo '';
}
if ( $this->field['background-clip'] === true ) {
$array = array(
'inherit' => 'Inherit',
'border-box' => 'Border Box',
'content-box' => 'Content Box',
'padding-box' => 'Padding Box',
);
echo '';
}
if ( $this->field['background-origin'] === true ) {
$array = array(
'inherit' => 'Inherit',
'border-box' => 'Border Box',
'content-box' => 'Content Box',
'padding-box' => 'Padding Box',
);
echo '';
}
if ( $this->field['background-size'] === true ) {
$array = array(
'inherit' => 'Inherit',
'cover' => 'Cover',
'contain' => 'Contain',
);
echo '';
}
if ( $this->field['background-attachment'] === true ) {
$array = array(
'fixed' => 'Fixed',
'scroll' => 'Scroll',
'inherit' => 'Inherit',
);
echo '';
}
if ( $this->field['background-position'] === true ) {
$array = array(
'left top' => 'Left Top',
'left center' => 'Left center',
'left bottom' => 'Left Bottom',
'center top' => 'Center Top',
'center center' => 'Center Center',
'center bottom' => 'Center Bottom',
'right top' => 'Right Top',
'right center' => 'Right center',
'right bottom' => 'Right Bottom',
);
echo '';
}
if ($this->field['background-image'] === true) {
echo '
';
if( empty( $this->value ) && !empty( $this->field['default'] ) ) { // If there are standard values and value is empty
if( is_array( $this->field['default'] ) ) {
if( !empty( $this->field['default']['media']['id'] ) ) {
$this->value['media']['id'] = $this->field['default']['media']['id'];
} else if( !empty( $this->field['default']['id'] ) ) {
$this->value['media']['id'] = $this->field['default']['id'];
}
if( !empty( $this->field['default']['url'] ) ) {
$this->value['background-image'] = $this->field['default']['url'];
} else if( !empty( $this->field['default']['media']['url'] ) ) {
$this->value['background-image'] = $this->field['default']['media']['url'];
} else if( !empty( $this->field['default']['background-image'] ) ) {
$this->value['background-image'] = $this->field['default']['background-image'];
}
} else {
if( is_numeric( $this->field['default'] ) ) { // Check if it's an attachment ID
$this->value['media']['id'] = $this->field['default'];
} else { // Must be a URL
$this->value['background-image'] = $this->field['default'];
}
}
}
if( empty( $this->value['background-image'] ) && !empty( $this->value['media']['id'] ) ) {
$img = wp_get_attachment_image_src( $this->value['media']['id'], 'full' );
$this->value['background-image'] = $img[0];
$this->value['media']['width'] = $img[1];
$this->value['media']['height'] = $img[2];
}
$hide = 'hide ';
if( (isset( $this->field['preview_media'] ) && $this->field['preview_media'] === false) ) {
$this->field['class'] .= " noPreview";
}
if( ( !empty( $this->field['background-image'] ) && $this->field['background-image'] === true ) || isset( $this->field['preview'] ) && $this->field['preview'] === false ) {
$hide = '';
}
$placeholder = isset($this->field['placeholder']) ? $this->field['placeholder'] : __('No media selected','redux-framework');
echo '';
echo '';
echo '';
echo '';
echo '';
//Preview
$hide = '';
if( (isset( $this->field['preview_media'] ) && $this->field['preview_media'] === false) || empty( $this->value['background-image'] ) ) {
$hide = 'hide ';
}
if ( empty( $this->value['media']['thumbnail'] ) && !empty( $this->value['background-image'] ) ) { // Just in case
if ( !empty( $this->value['media']['id'] ) ) {
$image = wp_get_attachment_image_src( $this->value['media']['id'], array(150, 150) );
$this->value['media']['thumbnail'] = $image[0];
} else {
$this->value['media']['thumbnail'] = $this->value['background-image'];
}
}
echo '
'; endif; } /** * Enqueue Function. * * If this field requires any scripts, or css define this function and register/enqueue the scripts/css * * @since 1.0.0 * @access public * @return void */ public function enqueue() { wp_enqueue_script( 'redux-field-background-js', ReduxFramework::$_url . 'inc/fields/background/field_background.js', array( 'jquery', 'wp-color-picker' ), time(), true ); wp_enqueue_style( 'redux-field-background-css', ReduxFramework::$_url . 'inc/fields/background/field_background.css', time(), true ); } public function getCSS() { $css = ''; if (!empty($this->value)) { foreach($this->value as $key=>$value) { if (!empty($value) && $key != "media") { if ($key == "background-image") { $css .= $key.":url('".$value."');"; } else { $css .= $key.":".$value.";"; } } } } return $css; } public function output() { $style = $this->getCSS(); if ( !empty( $style ) ) { if ( !empty( $this->field['output'] ) && is_array( $this->field['output'] ) ) { $keys = implode(",", $this->field['output']); $this->parent->outputCSS .= $keys . "{" . $style . '}'; } if ( !empty( $this->field['compiler'] ) && is_array( $this->field['compiler'] ) ) { $keys = implode(",", $this->field['compiler']); $this->parent->compilerCSS .= $keys . "{" . $style . '}'; } } } } }