123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340 |
- <?php if ( ! defined( 'ABSPATH' ) ) { die; } // Cannot access directly.
- /**
- *
- * Field: background
- *
- * @since 1.0.0
- * @version 1.0.0
- *
- */
- if ( ! class_exists( 'CSF_Field_background' ) ) {
- class CSF_Field_background extends CSF_Fields {
- public function __construct( $field, $value = '', $unique = '', $where = '', $parent = '' ) {
- parent::__construct( $field, $value, $unique, $where, $parent );
- }
- public function render() {
- $args = wp_parse_args( $this->field, array(
- 'background_color' => true,
- 'background_image' => true,
- 'background_position' => true,
- 'background_repeat' => true,
- 'background_attachment' => true,
- 'background_size' => true,
- 'background_origin' => false,
- 'background_clip' => false,
- 'background_blend_mode' => false,
- 'background_gradient' => false,
- 'background_gradient_color' => true,
- 'background_gradient_direction' => true,
- 'background_image_preview' => true,
- 'background_auto_attributes' => false,
- 'compact' => false,
- 'background_image_library' => 'image',
- 'background_image_placeholder' => esc_html__( 'Not selected', 'sakurairo_csf' ),
- ) );
- if ( $args['compact'] ) {
- $args['background_color'] = false;
- $args['background_auto_attributes'] = true;
- }
- $default_value = array(
- 'background-color' => '',
- 'background-image' => '',
- 'background-position' => '',
- 'background-repeat' => '',
- 'background-attachment' => '',
- 'background-size' => '',
- 'background-origin' => '',
- 'background-clip' => '',
- 'background-blend-mode' => '',
- 'background-gradient-color' => '',
- 'background-gradient-direction' => '',
- );
- $default_value = ( ! empty( $this->field['default'] ) ) ? wp_parse_args( $this->field['default'], $default_value ) : $default_value;
- $this->value = wp_parse_args( $this->value, $default_value );
- echo $this->field_before();
- echo '<div class="csf--background-colors">';
- //
- // Background Color
- if ( ! empty( $args['background_color'] ) ) {
- echo '<div class="csf--color">';
- echo ( ! empty( $args['background_gradient'] ) ) ? '<div class="csf--title">'. esc_html__( 'From', 'sakurairo_csf' ) .'</div>' : '';
- Sakurairo_CSF::field( array(
- 'id' => 'background-color',
- 'type' => 'color',
- 'default' => $default_value['background-color'],
- ), $this->value['background-color'], $this->field_name(), 'field/background' );
- echo '</div>';
- }
- //
- // Background Gradient Color
- if ( ! empty( $args['background_gradient_color'] ) && ! empty( $args['background_gradient'] ) ) {
- echo '<div class="csf--color">';
- echo ( ! empty( $args['background_gradient'] ) ) ? '<div class="csf--title">'. esc_html__( 'To', 'sakurairo_csf' ) .'</div>' : '';
- Sakurairo_CSF::field( array(
- 'id' => 'background-gradient-color',
- 'type' => 'color',
- 'default' => $default_value['background-gradient-color'],
- ), $this->value['background-gradient-color'], $this->field_name(), 'field/background' );
- echo '</div>';
- }
- //
- // Background Gradient Direction
- if ( ! empty( $args['background_gradient_direction'] ) && ! empty( $args['background_gradient'] ) ) {
- echo '<div class="csf--color">';
- echo ( ! empty( $args['background_gradient'] ) ) ? '<div class="csf---title">'. esc_html__( 'Direction', 'sakurairo_csf' ) .'</div>' : '';
- Sakurairo_CSF::field( array(
- 'id' => 'background-gradient-direction',
- 'type' => 'select',
- 'options' => array(
- '' => esc_html__( 'Gradient Direction', 'sakurairo_csf' ),
- 'to bottom' => esc_html__( '⇓ top to bottom', 'sakurairo_csf' ),
- 'to right' => esc_html__( '⇒ left to right', 'sakurairo_csf' ),
- '135deg' => esc_html__( '⇘ corner top to right', 'sakurairo_csf' ),
- '-135deg' => esc_html__( '⇙ corner top to left', 'sakurairo_csf' ),
- ),
- ), $this->value['background-gradient-direction'], $this->field_name(), 'field/background' );
- echo '</div>';
- }
- echo '</div>';
- //
- // Background Image
- if ( ! empty( $args['background_image'] ) ) {
- echo '<div class="csf--background-image">';
- Sakurairo_CSF::field( array(
- 'id' => 'background-image',
- 'type' => 'media',
- 'class' => 'csf-assign-field-background',
- 'library' => $args['background_image_library'],
- 'preview' => $args['background_image_preview'],
- 'placeholder' => $args['background_image_placeholder'],
- 'attributes' => array( 'data-depend-id' => $this->field['id'] ),
- ), $this->value['background-image'], $this->field_name(), 'field/background' );
- echo '</div>';
- }
- $auto_class = ( ! empty( $args['background_auto_attributes'] ) ) ? ' csf--auto-attributes' : '';
- $hidden_class = ( ! empty( $args['background_auto_attributes'] ) && empty( $this->value['background-image']['url'] ) ) ? ' csf--attributes-hidden' : '';
- echo '<div class="csf--background-attributes'. esc_attr( $auto_class . $hidden_class ) .'">';
- //
- // Background Position
- if ( ! empty( $args['background_position'] ) ) {
- Sakurairo_CSF::field( array(
- 'id' => 'background-position',
- 'type' => 'select',
- 'options' => array(
- '' => esc_html__( 'Background Position', 'sakurairo_csf' ),
- 'left top' => esc_html__( 'Left Top', 'sakurairo_csf' ),
- 'left center' => esc_html__( 'Left Center', 'sakurairo_csf' ),
- 'left bottom' => esc_html__( 'Left Bottom', 'sakurairo_csf' ),
- 'center top' => esc_html__( 'Center Top', 'sakurairo_csf' ),
- 'center center' => esc_html__( 'Center Center', 'sakurairo_csf' ),
- 'center bottom' => esc_html__( 'Center Bottom', 'sakurairo_csf' ),
- 'right top' => esc_html__( 'Right Top', 'sakurairo_csf' ),
- 'right center' => esc_html__( 'Right Center', 'sakurairo_csf' ),
- 'right bottom' => esc_html__( 'Right Bottom', 'sakurairo_csf' ),
- ),
- ), $this->value['background-position'], $this->field_name(), 'field/background' );
- }
- //
- // Background Repeat
- if ( ! empty( $args['background_repeat'] ) ) {
- Sakurairo_CSF::field( array(
- 'id' => 'background-repeat',
- 'type' => 'select',
- 'options' => array(
- '' => esc_html__( 'Background Repeat', 'sakurairo_csf' ),
- 'repeat' => esc_html__( 'Repeat', 'sakurairo_csf' ),
- 'no-repeat' => esc_html__( 'No Repeat', 'sakurairo_csf' ),
- 'repeat-x' => esc_html__( 'Repeat Horizontally', 'sakurairo_csf' ),
- 'repeat-y' => esc_html__( 'Repeat Vertically', 'sakurairo_csf' ),
- ),
- ), $this->value['background-repeat'], $this->field_name(), 'field/background' );
- }
- //
- // Background Attachment
- if ( ! empty( $args['background_attachment'] ) ) {
- Sakurairo_CSF::field( array(
- 'id' => 'background-attachment',
- 'type' => 'select',
- 'options' => array(
- '' => esc_html__( 'Background Attachment', 'sakurairo_csf' ),
- 'scroll' => esc_html__( 'Scroll', 'sakurairo_csf' ),
- 'fixed' => esc_html__( 'Fixed', 'sakurairo_csf' ),
- ),
- ), $this->value['background-attachment'], $this->field_name(), 'field/background' );
- }
- //
- // Background Size
- if ( ! empty( $args['background_size'] ) ) {
- Sakurairo_CSF::field( array(
- 'id' => 'background-size',
- 'type' => 'select',
- 'options' => array(
- '' => esc_html__( 'Background Size', 'sakurairo_csf' ),
- 'cover' => esc_html__( 'Cover', 'sakurairo_csf' ),
- 'contain' => esc_html__( 'Contain', 'sakurairo_csf' ),
- 'auto' => esc_html__( 'Auto', 'sakurairo_csf' ),
- ),
- ), $this->value['background-size'], $this->field_name(), 'field/background' );
- }
- //
- // Background Origin
- if ( ! empty( $args['background_origin'] ) ) {
- Sakurairo_CSF::field( array(
- 'id' => 'background-origin',
- 'type' => 'select',
- 'options' => array(
- '' => esc_html__( 'Background Origin', 'sakurairo_csf' ),
- 'padding-box' => esc_html__( 'Padding Box', 'sakurairo_csf' ),
- 'border-box' => esc_html__( 'Border Box', 'sakurairo_csf' ),
- 'content-box' => esc_html__( 'Content Box', 'sakurairo_csf' ),
- ),
- ), $this->value['background-origin'], $this->field_name(), 'field/background' );
- }
- //
- // Background Clip
- if ( ! empty( $args['background_clip'] ) ) {
- Sakurairo_CSF::field( array(
- 'id' => 'background-clip',
- 'type' => 'select',
- 'options' => array(
- '' => esc_html__( 'Background Clip', 'sakurairo_csf' ),
- 'border-box' => esc_html__( 'Border Box', 'sakurairo_csf' ),
- 'padding-box' => esc_html__( 'Padding Box', 'sakurairo_csf' ),
- 'content-box' => esc_html__( 'Content Box', 'sakurairo_csf' ),
- ),
- ), $this->value['background-clip'], $this->field_name(), 'field/background' );
- }
- //
- // Background Blend Mode
- if ( ! empty( $args['background_blend_mode'] ) ) {
- Sakurairo_CSF::field( array(
- 'id' => 'background-blend-mode',
- 'type' => 'select',
- 'options' => array(
- '' => esc_html__( 'Background Blend Mode', 'sakurairo_csf' ),
- 'normal' => esc_html__( 'Normal', 'sakurairo_csf' ),
- 'multiply' => esc_html__( 'Multiply', 'sakurairo_csf' ),
- 'screen' => esc_html__( 'Screen', 'sakurairo_csf' ),
- 'overlay' => esc_html__( 'Overlay', 'sakurairo_csf' ),
- 'darken' => esc_html__( 'Darken', 'sakurairo_csf' ),
- 'lighten' => esc_html__( 'Lighten', 'sakurairo_csf' ),
- 'color-dodge' => esc_html__( 'Color Dodge', 'sakurairo_csf' ),
- 'saturation' => esc_html__( 'Saturation', 'sakurairo_csf' ),
- 'color' => esc_html__( 'Color', 'sakurairo_csf' ),
- 'luminosity' => esc_html__( 'Luminosity', 'sakurairo_csf' ),
- ),
- ), $this->value['background-blend-mode'], $this->field_name(), 'field/background' );
- }
- echo '</div>';
- echo $this->field_after();
- }
- public function output() {
- $output = '';
- $bg_image = array();
- $important = ( ! empty( $this->field['output_important'] ) ) ? '!important' : '';
- $element = ( is_array( $this->field['output'] ) ) ? join( ',', $this->field['output'] ) : $this->field['output'];
- // Background image and gradient
- $background_color = ( ! empty( $this->value['background-color'] ) ) ? $this->value['background-color'] : '';
- $background_gd_color = ( ! empty( $this->value['background-gradient-color'] ) ) ? $this->value['background-gradient-color'] : '';
- $background_gd_direction = ( ! empty( $this->value['background-gradient-direction'] ) ) ? $this->value['background-gradient-direction'] : '';
- $background_image = ( ! empty( $this->value['background-image']['url'] ) ) ? $this->value['background-image']['url'] : '';
- if ( $background_color && $background_gd_color ) {
- $gd_direction = ( $background_gd_direction ) ? $background_gd_direction .',' : '';
- $bg_image[] = 'linear-gradient('. $gd_direction . $background_color .','. $background_gd_color .')';
- unset( $this->value['background-color'] );
- }
- if ( $background_image ) {
- $bg_image[] = 'url('. $background_image .')';
- }
- if ( ! empty( $bg_image ) ) {
- $output .= 'background-image:'. implode( ',', $bg_image ) . $important .';';
- }
- // Common background properties
- $properties = array( 'color', 'position', 'repeat', 'attachment', 'size', 'origin', 'clip', 'blend-mode' );
- foreach ( $properties as $property ) {
- $property = 'background-'. $property;
- if ( ! empty( $this->value[$property] ) ) {
- $output .= $property .':'. $this->value[$property] . $important .';';
- }
- }
- if ( $output ) {
- $output = $element .'{'. $output .'}';
- }
- $this->parent->output_css .= $output;
- return $output;
- }
- }
- }
|