<?php
/**
 * Blog layout section.
 *
 * Author:          Andrei Baicus <andrei@themeisle.com>
 * Created on:      20/08/2018
 *
 * @package Neve\Customizer\Options
 */

namespace Neve\Customizer\Options;

use Neve\Core\Settings\Config;
use Neve\Core\Settings\Mods;
use Neve\Customizer\Base_Customizer;
use Neve\Customizer\Defaults\Layout;
use Neve\Customizer\Defaults\Utils;
use Neve\Customizer\Types\Control;
use Neve\Customizer\Types\Section;

/**
 * Class Layout_Blog
 *
 * @package Neve\Customizer\Options
 */
class Layout_Blog extends Base_Customizer {
	use Layout;
	use Utils;

	/**
	 * Holds the section name.
	 *
	 * @var string $section
	 */
	private $section = 'neve_blog_archive_layout';

	/**
	 * Function that should be extended to add customizer controls.
	 *
	 * @return void
	 */
	public function add_controls() {
		$this->section_blog();
		$this->add_layout_controls();
		$this->add_post_structure();
		$this->add_post_meta_controls();
		$this->add_featured_post();
		$this->add_design_controls();

		add_action( 'customize_register', [ $this, 'adapt_old_pro' ], PHP_INT_MAX );
	}

	/**
	 * Adapting old pro versions to make them still usable with the old theme version.
	 */
	public function adapt_old_pro() {
		if ( ! defined( 'NEVE_PRO_VERSION' ) ) {
			return;
		}

		if ( version_compare( NEVE_PRO_VERSION, '1.2.8', '>' ) ) {
			return;
		}

		$changes = [
			'neve_posts_order'                   => [ 'priority' => 51 ],
			'neve_post_content_ordering'         => [ 'active_callback' => '__return_true' ],
			'neve_blog_ordering_content_heading' => [ 'controls_to_wrap' => 6 ],
			'neve_blog_post_meta_heading'        => [ 'controls_to_wrap' => 3 ],
			'neve_read_more_options'             => [
				'accordion' => true,
				'expanded'  => false,
				'class'     => 'read-more-options-accordion',
			],
		];

		foreach ( $changes as $control_slug => $props ) {
			foreach ( $props as $prop => $new_value ) {
				$this->change_customizer_object( 'control', $control_slug, $prop, $new_value );
			}
		}

		$this->wpc->remove_control( 'neve_metadata_options' );
	}

	/**
	 * Get the tab slotting for the controls.
	 *
	 * @return array
	 */
	private function get_tabs_slotting() {
		$tab_slotting = [
			'general' => [
				'neve_blog_layout_heading',
				'neve_blog_archive_sidebar_layout',
				'neve_blog_archive_content_width',
				$this->section,
				'neve_grid_layout',
				'neve_archive_hide_title',

				'neve_enable_masonry',
				Config::OPTION_POSTS_PER_PAGE,

				'neve_enable_featured_post',
				'neve_featured_post_target',

				'neve_post_content_ordering',
				'neve_post_excerpt_length',

				'neve_blog_post_meta_fields',

				'neve_blog_archive_upsell_control_features',
			],
			'style'   => [
				'neve_blog_content_padding',

				'neve_enable_card_style',
				'neve_blog_grid_card_bg_color',
				'neve_blog_grid_text_color',
				'neve_blog_card_shadow',
				'neve_blog_covers_text_color',
				'neve_blog_items_border_radius',

				'neve_archive_typography_post_title_accordion_wrap',
				'neve_archive_typography_post_title',
				'neve_archive_typography_post_excerpt_accordion_wrap',
				'neve_archive_typography_post_excerpt',
				'neve_archive_typography_post_meta_accordion_wrap',
				'neve_archive_typography_post_meta',

				'neve_blog_archive_upsell_control_features',
			],
		];

		foreach ( $tab_slotting as $tab_key => $tab_data ) {
			$tab_slotting[ $tab_key ] = array_fill_keys( $tab_data, [] );
		}

		return $tab_slotting;
	}


	/**
	 * Add customize section
	 */
	private function section_blog() {
		$this->add_section(
			new Section(
				$this->section,
				array(
					'priority' => 35,
					'title'    => esc_html__( 'Blog / Archive', 'neve' ),
					'panel'    => 'neve_blog',
				)
			)
		);
		$this->add_control(
			new Control(
				$this->section . '_tabs',
				[
					'transport' => 'refresh',
				],
				[
					'priority' => -100,
					'section'  => $this->section,
					'tabs'     => [
						'general' => [
							'label' => __( 'General', 'neve' ),
							'icon'  => 'layout',
						],
						'style'   => [
							'label' => __( 'Design', 'neve' ),
							'icon'  => 'admin-customizer',
						],
					],
					'controls' => $this->get_tabs_slotting(),
				],
				'Neve\Customizer\Controls\Tabs'
			)
		);
	}


	/**
	 * Add blog layout controls.
	 */
	private function add_layout_controls() {
		$layout_id = 'neve_blog_archive_sidebar_layout';
		$width_id  = 'neve_blog_archive_content_width';

		$this->add_control(
			new Control(
				$layout_id,
				array(
					'sanitize_callback' => array( $this, 'sanitize_sidebar_layout' ),
					'default'           => $this->sidebar_layout_alignment_default( $layout_id ),
				),
				array(
					'label'       => __( 'Sidebar Layout', 'neve' ),
					'description' => $this->get_sidebar_control_description( $layout_id ),
					'section'     => $this->section,
					'priority'    => 10,
					'choices'     => $this->sidebar_layout_choices( $layout_id ),
				),
				'\Neve\Customizer\Controls\React\Radio_Image'
			)
		);

		$width_default = $this->sidebar_layout_width_default( $width_id );

		$this->add_control(
			new Control(
				$width_id,
				array(
					'sanitize_callback' => 'absint',
					'transport'         => $this->selective_refresh,
					'default'           => $width_default,
				),
				array(
					'label'       => esc_html__( 'Content Width (%)', 'neve' ),
					'section'     => $this->section,
					'type'        => 'neve_range_control',
					'input_attrs' => [
						'min'        => 50,
						'max'        => 100,
						'defaultVal' => $width_default,
					],
					'priority'    => 20,
				),
				'Neve\Customizer\Controls\React\Range'
			)
		);

		$this->add_control(
			new Control(
				$this->section,
				[
					'default'           => 'grid',
					'sanitize_callback' => [ $this, 'sanitize_blog_layout' ],
				],
				[
					'section'  => $this->section,
					'label'    => esc_html__( 'Post Cards Layout', 'neve' ),
					'priority' => 30,
					'choices'  => [
						'default' => [
							'name'  => __( 'List', 'neve' ),
							'image' => 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAF4AAAB5CAYAAACwe5bgAAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH5AkBDC0qBiacfQAABqdJREFUeNrtXU1T40YQfT2WLS/s4kqR5UAK36jiAKdQ+/8vnOKcIIeEG6RSKZZDcIqwXmA6B0nWSOZjbI00Gqn7YooCWXp601/zZoaYmSHWuCmBQIAX4MUEeAFeTIAX4MUEeAFerAngnde8Or90wwV109+nKoFN7m8nuzwRdZrxkc0ffX8Ebh+AhBS8BCZhSXWAfvwIxIOa3qUl25t+0ZHN8F88Kfw5Z4ApgYY4xZ8cPDTwaZwAvwIAN/MmfIyuyGb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rQikHuAyXCm6sICxNE2ONFCC9CeBHCixBehPAihBchvAjhRQgvQniR8O3kJ0Nx/wAXONq4kNEpFQAAAABJRU5ErkJggg==',
						],
						'grid'    => [
							'name'  => __( 'Grid', 'neve' ),
							'image' => 'data:image/jpeg;base64, ',
						],
					],
				],
				'\Neve\Customizer\Controls\React\Radio_Image'
			)
		);

		$grid_layout_default = '{"desktop":3,"tablet":2,"mobile":1}';

		$this->add_control(
			new Control(
				'neve_grid_layout',
				array(
					'sanitize_callback' => 'neve_sanitize_range_value',
					'default'           => $grid_layout_default,
				),
				array(
					'label'           => esc_html__( 'Columns', 'neve' ),
					'section'         => $this->section,
					'units'           => array(
						'items',
					),
					'input_attrs'     => [
						'min'        => 1,
						'max'        => 4,
						'defaultVal' => json_decode( $grid_layout_default, true ),
					],
					'priority'        => 50,
					'active_callback' => array( $this, 'is_column_layout' ),
				),
				'Neve\Customizer\Controls\React\Responsive_Range'
			)
		);

		$posts_per_page = get_option( Config::OPTION_POSTS_PER_PAGE );

		$this->wpc->add_setting(
			Config::OPTION_POSTS_PER_PAGE,
			[
				'type'              => 'option',
				'sanitize_callback' => 'absint',
				'default'           => absint( $posts_per_page ),
			]
		);

		$this->wpc->add_control(
			new \Neve\Customizer\Controls\React\Range(
				$this->wpc,
				Config::OPTION_POSTS_PER_PAGE,
				[
					'type'        => 'neve_range_control',
					'priority'    => 52,
					'section'     => $this->section,
					'label'       => esc_html__( 'Posts per Page', 'neve' ),
					'input_attrs' => [
						'min'        => 1,
						'max'        => 100,
						'defaultVal' => absint( $posts_per_page ),
					],
					'setting'     => Config::OPTION_POSTS_PER_PAGE,
					'default'     => absint( $posts_per_page ),
				]
			)
		);

		$this->add_control(
			new Control(
				'neve_archive_hide_title',
				[
					'sanitize_callback' => 'neve_sanitize_checkbox',
					'default'           => $this->get_v4_defaults( 'neve_archive_hide_title', false ),
				],
				[
					'label'           => esc_html__( 'Disable Title', 'neve' ),
					'section'         => $this->section,
					'type'            => 'neve_toggle_control',
					'priority'        => 70,
					'active_callback' => function () {
						return get_option( 'show_on_front' ) !== 'posts';
					},
				],
				'Neve\Customizer\Controls\Checkbox'
			)
		);

		$this->add_control(
			new Control(
				'neve_enable_masonry',
				array(
					'sanitize_callback' => 'neve_sanitize_checkbox',
					'default'           => false,
				),
				array(
					'type'            => 'neve_toggle_control',
					'priority'        => 55,
					'section'         => $this->section,
					'label'           => esc_html__( 'Enable Masonry', 'neve' ),
					'active_callback' => array( $this, 'should_show_masonry' ),
				)
			)
		);
	}

	/**
	 * Add featured post controls.
	 */
	private function add_featured_post() {
		$this->add_control(
			new Control(
				'neve_enable_featured_post',
				[
					'sanitize_callback' => 'neve_sanitize_checkbox',
					'default'           => false,
				],
				[
					'label'    => esc_html__( 'Enable featured post section', 'neve' ),
					'section'  => $this->section,
					'type'     => 'neve_toggle_control',
					'priority' => 260,
				],
				'Neve\Customizer\Controls\Checkbox'
			)
		);

		$this->add_control(
			new Control(
				'neve_featured_post_target',
				array(
					'default'           => 'latest',
					'sanitize_callback' => array( $this, 'sanitize_featured_post_target' ),
				),
				array(
					'label'           => esc_html__( 'Featured Post', 'neve' ),
					'section'         => $this->section,
					'priority'        => 270,
					'type'            => 'select',
					'choices'         => [
						'latest' => esc_html__( 'Latest Post', 'neve' ),
						'sticky' => esc_html__( 'Sticky Post', 'neve' ),
					],
					'active_callback' => function () {
						return get_theme_mod( 'neve_enable_featured_post', false );
					},
				)
			)
		);
	}

	/**
	 * Add post structure controls.
	 */
	private function add_post_structure() {
		$order_default_components = array(
			'thumbnail',
			'title-meta',
			'excerpt',
		);


		$ar_choices = [
			'original' => esc_html__( 'Original', 'neve' ),
			'1-1'      => '1:1',
			'4-3'      => '4:3',
			'16-9'     => '16:9',
			'2-1'      => '2:1',
			'4-5'      => '4:5',
			'3-4'      => '3:4',
			'2-3'      => '2:3',
		];

		$excerpt_length_default = $this->get_v4_defaults( 'neve_post_excerpt_length', 25 );

		$components = array(
			'thumbnail'  => [
				'label'    => __( 'Thumbnail', 'neve' ),
				'controls' => [
					'neve_blog_archive_aspect_ratio' => [
						'label'   => esc_html__( 'Image Aspect Ratio', 'neve' ),
						'type'    => 'select',
						'choices' => $ar_choices,
					],
				],
			],
			'title-meta' => [
				'label'    => __( 'Title & Meta', 'neve' ),
				'controls' => [
					'neve_metadata_separator'     => [
						'label'       => esc_html__( 'Meta Separator', 'neve' ),
						'description' => esc_html__( 'For special characters make sure to use Unicode. For example > can be displayed using \003E.', 'neve' ),
						'type'        => 'text',
					],
					'neve_show_last_updated_date' => [
						'label' => esc_html__( 'Show Last Updated Date', 'neve' ),
						'type'  => 'toggle',
					],
					'neve_author_avatar'          => [
						'label' => esc_html__( 'Show Author Avatar', 'neve' ),
						'type'  => 'toggle',
					],
					'neve_author_avatar_size'     => [
						'type'  => 'responsive-range',
						'label' => esc_html__( 'Author Avatar Size', 'neve' ),
					],
				],
			],
			'excerpt'    => [
				'label'    => __( 'Excerpt', 'neve' ),
				'controls' => [
					'neve_post_excerpt_length' => [
						'label' => esc_html__( 'Excerpt Length', 'neve' ),
						'type'  => 'range',
						'attrs' => [
							'min'        => 5,
							'max'        => 300,
							'defaultVal' => $excerpt_length_default,
							'step'       => 5,
						],
					],
				],
			],
		);

		$this->add_control(
			new Control(
				'neve_post_content_ordering',
				array(
					'sanitize_callback' => array( $this, 'sanitize_post_content_ordering' ),
					'default'           => wp_json_encode( $order_default_components ),
				),
				array(
					'label'      => esc_html__( 'Post Content Order', 'neve' ),
					'section'    => $this->section,
					'components' => $components,
					'priority'   => 65,
				),
				'Neve\Customizer\Controls\React\Ordering'
			)
		);

		$this->add_control(
			new Control(
				'neve_blog_archive_aspect_ratio',
				[
					'default'           => $this->get_v4_defaults( 'neve_blog_archive_aspect_ratio', 'original' ),
					'sanitize_callback' => function( $value ) use ( $ar_choices ) {
						return array_key_exists( $value, $ar_choices ) ? $value : 'original';
					},
				],
				[
					'section'         => $this->section,
					'type'            => 'hidden',
					'active_callback' => function() {
						return get_theme_mod( $this->section, 'grid' ) !== 'default' || get_theme_mod( 'neve_blog_list_image_position', 'left' ) !== 'no';
					},
				]
			)
		);

		$this->add_control(
			new Control(
				'neve_post_excerpt_length',
				array(
					'sanitize_callback' => 'absint',
					'default'           => $excerpt_length_default,
				),
				array(
					'section' => $this->section,
					'type'    => 'hidden',
				)
			)
		);



		$this->add_control(
			new Control(
				'neve_metadata_separator',
				array(
					'sanitize_callback' => 'sanitize_text_field',
					'default'           => esc_html( '/' ),
				),
				array(
					'section' => $this->section,
					'type'    => 'hidden',
				)
			)
		);


		$this->add_control(
			new Control(
				'neve_author_avatar',
				array(
					'sanitize_callback' => 'neve_sanitize_checkbox',
					'default'           => false,
				),
				array(
					'label'   => esc_html__( 'Show Author Avatar', 'neve' ),
					'section' => $this->section,
					'type'    => 'hidden',
				)
			)
		);


		$this->add_control(
			new Control(
				'neve_author_avatar_size',
				array(
					'sanitize_callback' => 'neve_sanitize_range_value',
					'default'           => '{ "mobile": 20, "tablet": 20, "desktop": 20 }',
				),
				array(
					'label'           => esc_html__( 'Avatar Size', 'neve' ),
					'type'            => 'hidden',
					'section'         => $this->section,
					'units'           => array(
						'px',
					),
					'input_attr'      => array(
						'mobile'  => array(
							'min'          => 20,
							'max'          => 50,
							'default'      => 20,
							'default_unit' => 'px',
						),
						'tablet'  => array(
							'min'          => 20,
							'max'          => 50,
							'default'      => 20,
							'default_unit' => 'px',
						),
						'desktop' => array(
							'min'          => 20,
							'max'          => 50,
							'default'      => 20,
							'default_unit' => 'px',
						),
					),
					'input_attrs'     => [
						'units'      => [ 'px', 'em', 'rem' ],
						'min'        => self::RELATIVE_CSS_UNIT_SUPPORTED_MIN_VALUE,
						'max'        => 50,
						'defaultVal' => [
							'mobile'  => 20,
							'tablet'  => 20,
							'desktop' => 20,
							'suffix'  => [
								'mobile'  => 'px',
								'tablet'  => 'px',
								'desktop' => 'px',
							],
						],
					],
					'priority'        => 170,
					'active_callback' => function () {
						return get_theme_mod( 'neve_author_avatar', false ) && $this->is_meta_enabled();
					},
					'responsive'      => true,
				),
				'Neve\Customizer\Controls\React\Responsive_Range'
			)
		);

		$this->add_control(
			new Control(
				'neve_show_last_updated_date',
				array(
					'sanitize_callback' => 'neve_sanitize_checkbox',
					'default'           => false,
				),
				array(
					'label'           => esc_html__( 'Use last updated date instead of the published one', 'neve' ),
					'section'         => $this->section,
					'type'            => 'hidden',
					'priority'        => 180,
					'active_callback' => [ $this, 'is_meta_enabled' ],
				)
			)
		);

	}

	/**
	 * Add controls for post meta.
	 */
	private function add_post_meta_controls() {
		$default       = wp_json_encode( [ 'author', 'date', 'comments' ] );
		$default_value = neve_get_default_meta_value( 'neve_post_meta_ordering', $default );
		$this->add_control(
			new Control(
				'neve_blog_post_meta_fields',
				[
					'sanitize_callback' => 'neve_sanitize_meta_repeater',
					'default'           => wp_json_encode( $default_value ),
				],
				[
					'label'            => esc_html__( 'Post Meta Order', 'neve' ),
					'section'          => $this->section,
					'fields'           => [
						'hide_on_mobile' => [
							'type'  => 'checkbox',
							'label' => __( 'Hide on mobile', 'neve' ),
						],
					],
					'components'       => apply_filters(
						'neve_meta_filter',
						array(
							'author'   => __( 'Author', 'neve' ),
							'category' => __( 'Category', 'neve' ),
							'date'     => __( 'Date', 'neve' ),
							'comments' => __( 'Comments', 'neve' ),
						)
					),
					'allow_new_fields' => 'no',
					'priority'         => 65,
					'active_callback'  => [ $this, 'is_meta_enabled' ],
				],
				'\Neve\Customizer\Controls\React\Repeater'
			)
		);
	}

	/**
	 * Sanitize the container layout value
	 *
	 * @param string $value value from the control.
	 *
	 * @return string
	 */
	public function sanitize_blog_layout( $value ) {
		$allowed_values = array( 'default', 'covers', 'grid' );
		if ( ! in_array( $value, $allowed_values, true ) ) {
			return 'grid';
		}

		return sanitize_text_field( $value );
	}

	/**
	 * Sanitize featured post target
	 *
	 * @param string $value value from the control.
	 *
	 * @return string
	 */
	public function sanitize_featured_post_target( $value ) {
		$allowed_values = [ 'sticky', 'latest' ];
		if ( ! in_array( $value, $allowed_values, true ) ) {
			return 'latest';
		}

		return esc_html( $value );
	}

	/**
	 * Sanitize content order control.
	 */
	public function sanitize_post_content_ordering( $value ) {
		$allowed = array(
			'thumbnail',
			'title-meta',
			'excerpt',
		);

		if ( empty( $value ) ) {
			return wp_json_encode( $allowed );
		}

		$decoded = is_string( $value ) ? json_decode( $value, true ) : $value;

		if ( ! is_array( $decoded ) || empty( $decoded ) ) {
			return wp_json_encode( $allowed );
		}

		foreach ( $decoded as $val ) {
			if ( ! in_array( $val, $allowed, true ) ) {
				return wp_json_encode( $allowed );
			}
		}

		return wp_json_encode( array_values( $decoded ) );
	}

	/**
	 * Checks if the post meta is enabled.
	 *
	 * @return bool
	 */
	public function is_meta_enabled() {
		return in_array( 'title-meta', $this->get_post_elements_order(), true );
	}

	/**
	 * Checks if the excerpt is enabled.
	 *
	 * @return bool
	 */
	public function is_excerpt_enabled() {
		return in_array( 'excerpt', $this->get_post_elements_order(), true );
	}

	/**
	 * Checks if the thumbnail is enabled.
	 *
	 * @return bool
	 */
	public function is_thumbnail_enabled() {
		return in_array( 'thumbnail', $this->get_post_elements_order(), true );
	}

	/**
	 * Get the post elements order.
	 *
	 * @return array
	 */
	private function get_post_elements_order() {
		$default       = array(
			'thumbnail',
			'title-meta',
			'excerpt',
		);
		$content_order = get_theme_mod( 'neve_post_content_ordering', wp_json_encode( $default ) );

		if ( is_string( $content_order ) ) {
			$content_order = json_decode( $content_order, true );
		}

		return is_array( $content_order ) ? $content_order : $default;
	}

	/**
	 * Callback to show grid columns control.
	 *
	 * @return bool
	 */
	public function is_column_layout() {
		$blog_layout = get_theme_mod( $this->section, 'grid' );

		return in_array( $blog_layout, [ 'grid', 'covers' ], true );
	}

	/**
	 * Checks if the layout is covers.
	 *
	 * @return bool
	 */
	public function is_covers_layout() {
		return get_theme_mod( $this->section, 'grid' ) === 'covers';
	}


	/**
	 * Callback to show masonry control.
	 *
	 * @return bool
	 */
	public function should_show_masonry() {
		if ( ! $this->is_column_layout() ) {
			return false;
		}

		$columns = json_decode( get_theme_mod( 'neve_grid_layout', $this->grid_columns_default() ), true );
		$columns = array_filter(
			array_values( $columns ),
			function ( $value ) {
				return $value > 1;
			}
		);

		if ( empty( $columns ) ) {
			return false;
		}

		return true;
	}


	/**
	 * Adds design tab controls.
	 */
	private function add_design_controls() {
		$this->add_control(
			new Control(
				'neve_blog_covers_text_color',
				array(
					'sanitize_callback' => 'neve_sanitize_colors',
					'default'           => '#ffffff',
					'transport'         => 'postMessage',
				),
				array(
					'label'                 => esc_html__( 'Text Color', 'neve' ),
					'section'               => $this->section,
					'priority'              => 220,
					'default'               => '#ffffff',
					'active_callback'       => function () {
						return get_theme_mod( $this->section ) === 'covers';
					},
					'live_refresh_selector' => true,
					'live_refresh_css_prop' => [
						'cssVar' => [
							'vars'     => '--color',
							'selector' => '.neve-main',
						],
					],
				),
				'Neve\Customizer\Controls\React\Color'
			)
		);

		$this->add_control(
			new Control(
				'neve_blog_items_border_radius',
				[
					'sanitize_callback' => 'neve_sanitize_range_value',
					'transport'         => 'postMessage',
					'default'           => $this->get_v4_defaults( 'neve_blog_items_border_radius', 0 ),
				],
				[
					'label'                 => esc_html__( 'Border Radius', 'neve' ),
					'section'               => $this->section,
					'type'                  => 'neve_range_control',
					'input_attrs'           => [
						'min'        => 0,
						'max'        => 100,
						'defaultVal' => $this->get_v4_defaults( 'neve_blog_items_border_radius', 0 ),
					],
					'priority'              => 190,
					'live_refresh_selector' => true,
					'live_refresh_css_prop' => [
						'cssVar'   => [
							'vars'     => '--borderradius',
							'suffix'   => 'px',
							'fallback' => '0',
							'selector' => '.posts-wrapper, .nv-ft-post',
						],
						'fallback' => 0,
						'template' =>
						'body .cover-post, body .layout-grid .article-content-col .content {
							    border-radius: {{value}}px;
					    	}',
					],
				],
				'\Neve\Customizer\Controls\React\Range'
			)
		);

		$content_padding_default = $this->get_v4_defaults( 'neve_blog_content_padding', $this->blog_archive_card_padding_default() );

		$this->add_control(
			new Control(
				'neve_blog_content_padding',
				array(
					'sanitize_callback' => array( $this, 'sanitize_spacing_array' ),
					'transport'         => 'refresh',
					'default'           => $content_padding_default,
				),
				array(
					'label'       => esc_html__( 'Content Padding', 'neve' ),
					'section'     => $this->section,
					'priority'    => 80,
					'input_attrs' => array(
						'units' => [ 'px', 'em', 'rem' ],
						'min'   => 0,
					),
					'default'     => $content_padding_default,
				),
				'\Neve\Customizer\Controls\React\Spacing'
			)
		);

		// If this gets enabled, we remove the toggle.
		$this->add_control(
			new Control(
				'neve_enable_card_style',
				array(
					'sanitize_callback' => 'neve_sanitize_checkbox',
					'default'           => $this->get_v4_defaults( 'neve_enable_card_style', false ),
				),
				array(
					'type'     => $this->is_card_style_enabled() ? 'hidden' : 'neve_toggle_control',
					'priority' => 0,
					'section'  => $this->section,
					'label'    => esc_html__( 'Enable Card Style', 'neve' ),
				)
			)
		);


		$card_bg_default = $this->get_v4_defaults( 'neve_blog_grid_card_bg_color', '#333333' );
		$this->add_control(
			new Control(
				'neve_blog_grid_card_bg_color',
				array(
					'sanitize_callback' => 'neve_sanitize_colors',
					'default'           => $card_bg_default,
					'transport'         => 'postMessage',
				),
				array(
					'label'                 => esc_html__( 'Card Background Color', 'neve' ),
					'section'               => $this->section,
					'priority'              => 220,
					'default'               => $card_bg_default,
					'active_callback'       => function () {
						return ( ! $this->is_covers_layout() && $this->is_card_style_enabled() );
					},
					'input_attrs'           => [
						'allow_gradient' => true,
					],
					'live_refresh_selector' => true,
					'live_refresh_css_prop' => [
						'cssVar'   => [
							'vars'     => '--cardbgcolor',
							'selector' => '.posts-wrapper',
						],
						'template' =>
						'.layout-grid .article-content-col .content {
							background: {{value}};
						}',
					],
				),
				'Neve\Customizer\Controls\React\Color'
			)
		);

		$card_text_default = $this->get_v4_defaults( 'neve_blog_grid_text_color', '#ffffff' );
		$this->add_control(
			new Control(
				'neve_blog_grid_text_color',
				array(
					'sanitize_callback' => 'neve_sanitize_colors',
					'default'           => $card_text_default,
					'transport'         => 'postMessage',
				),
				array(
					'label'                 => esc_html__( 'Text Color', 'neve' ),
					'section'               => $this->section,
					'priority'              => 230,
					'default'               => $card_text_default,
					'active_callback'       => function () {
						return ( ! $this->is_covers_layout() && $this->is_card_style_enabled() );
					},
					'live_refresh_selector' => true,
					'live_refresh_css_prop' => [
						'cssVar'   => [
							'vars'     => '--cardcolor',
							'selector' => '.posts-wrapper',
						],
						'template' =>
						'.layout-grid .article-content-col .content, .layout-grid .article-content-col .content a:not(.button), .layout-grid .article-content-col .content li {
							color: {{value}};
						}',
					],
				),
				'Neve\Customizer\Controls\React\Color'
			)
		);


		$shadow_default = $this->get_v4_defaults( 'neve_blog_card_shadow', 0 );
		$this->add_control(
			new Control(
				'neve_blog_card_shadow',
				[
					'sanitize_callback' => 'neve_sanitize_range_value',
					'transport'         => 'refresh',
					'default'           => $shadow_default,
				],
				[
					'label'                 => esc_html__( 'Card Box Shadow', 'neve' ),
					'section'               => $this->section,
					'type'                  => 'neve_range_control',
					'input_attrs'           => [
						'min'        => 0,
						'max'        => 5,
						'defaultVal' => $shadow_default,
						'step'       => 0.1,
					],
					'priority'              => 240,
					'active_callback'       => [ $this, 'is_card_style_enabled' ],
					'live_refresh_selector' => true,
					'live_refresh_css_prop' => [
						'fallback' => 0,
						'template' =>
						'body .layout-grid .article-content-col .content {
							    box-shadow: 0 0 calc({{value}}px * 4) 0 rgba(0,0,0,calc(0.1 + 0.{{value}}));
					    	}',
					],
				],
				'\Neve\Customizer\Controls\React\Range'
			)
		);
	}

	/**
	 * Check if the card style is enabled.
	 *
	 * @return bool
	 */
	public function is_card_style_enabled() {
		$card_style_default = $this->get_v4_defaults( 'neve_enable_card_style', false );
		return Mods::get( 'neve_enable_card_style', $card_style_default ) === true;
	}
}
