<?php
/**
 * Zenvy Theme Customizer Footer Builder.
 *
 * @package Zenvy
 */

/**
 * Class Zenvy_Customizer_Footer_Builder
 *
 * Handles footer builder and customizer options.
 */
class Zenvy_Customizer_Footer_Builder {

	/**
	 * Panel ID, use for builder ID too.
	 *
	 * @var string
	 */
	public $panel = 'zenvy_footer';

	/**
	 * Builder Sections and Controller ID.
	 *
	 * @var string
	 */
	public $builder_section_controller = 'zenvy_footer_builder_controller_section';

	/**
	 * Footer Top Row and its setting.
	 *
	 * @var string
	 */
	public $footer_top = 'zenvy_footer_top';

	/**
	 * Footer Main Row and its setting.
	 *
	 * @var string
	 */
	public $footer_main = 'zenvy_footer_main';

	/**
	 * Footer Bottom Row and its setting.
	 *
	 * @var string
	 */
	public $footer_bottom = 'zenvy_footer_bottom';

	/**
	 * Footer HTML Row and its setting.
	 *
	 * @var string
	 */
	public $footer_html = 'footer_html';

	/**
	 * Copyright Section/Setting/Control ID.
	 *
	 * @var string
	 */
	public $footer_copyright = 'footer_copyright';

	/**
	 * Footer Menu Section/Setting/Control ID.
	 *
	 * @var string
	 */
	public $footer_menu = 'footer_menu';

	/**
	 * Footer Socials Section/Setting/Control ID.
	 *
	 * @var string
	 */
	public $footer_social = 'footer_social';

	/**
	 * Footer Button Section/Setting/Control ID.
	 *
	 * @var string
	 */
	public $footer_button = 'footer_button';

	/**
	 * Footer Sidebar 1 Section/Setting/Control ID.
	 *
	 * @var string
	 */
	public $footer_sidebar_1 = 'sidebar-widgets-footer-sidebar-1';

	/**
	 * Footer Sidebar 2 Section/Setting/Control ID.
	 *
	 * @var string
	 */
	public $footer_sidebar_2 = 'sidebar-widgets-footer-sidebar-2';

	/**
	 * Footer Sidebar 3 Section/Setting/Control ID.
	 *
	 * @var string
	 */
	public $footer_sidebar_3 = 'sidebar-widgets-footer-sidebar-3';

	/**
	 * Footer Sidebar 4 Section/Setting/Control ID.
	 *
	 * @var string
	 */
	public $footer_sidebar_4 = 'sidebar-widgets-footer-sidebar-4';

	/**
	 * Footer Sidebar 5 Section/Setting/Control ID.
	 *
	 * @var string
	 */
	public $footer_sidebar_5 = 'sidebar-widgets-footer-sidebar-5';

	/**
	 * Footer Sidebar 6 Section/Setting/Control ID.
	 *
	 * @var string
	 */
	public $footer_sidebar_6 = 'sidebar-widgets-footer-sidebar-6';

	/**
	 * Main Instance.
	 *
	 * Ensures that only one instance of Zenvy_Customizer_Footer_Builder exists in memory at any one
	 * time. Also prevents needing to define globals all over the place.
	 *
	 * @return object
	 */
	public static function instance() {

		// Store the instance locally to avoid private static replication.
		static $instance = null;

		// Only run these methods if they haven't been run previously.
		if ( null === $instance ) {
			$instance = new Zenvy_Customizer_Footer_Builder();
		}

		// Always return the instance.
		return $instance;
	}

	/**
	 * Run functionality with hooks.
	 *
	 * @return void
	 */
	public function run() {

		add_action( 'customize_register', [ $this, 'set_customizer' ], 1 );

		add_action( 'customize_register', [ $this, 'customize_register' ], 3 );

		add_filter( 'zenvy_default_theme_options', [ $this, 'footer_defaults' ] );
		add_filter( 'zenvy_builders', [ $this, 'footer_builder' ] );

		add_action( 'zenvy_footer', [ $this, 'zenvy_footer_display' ], 100 );
	}

	/**
	 * Callback functions for customize_register.
	 * Fixed previous array issue.
	 *
	 * @return void
	 */
	public function set_customizer() {
		$builder = zenvy_get_footer_builder_options( Zenvy_Customizer_Footer_Builder()->builder_section_controller );
		if ( is_array( $builder ) ) {
			$builder = wp_json_encode( urldecode_deep( $builder ) );
		}
		set_theme_mod( Zenvy_Customizer_Footer_Builder()->builder_section_controller, $builder );
	}

	/**
	 * Get footer builder.
	 *
	 * @return array|mixed
	 */
	public function get_builder() {
		$builder = zenvy_get_footer_builder_options( Zenvy_Customizer_Footer_Builder()->builder_section_controller );
		if ( ! is_array( $builder ) ) {
			$builder = json_decode( urldecode_deep( $builder ), true );
		}
		return $builder;
	}

	/**
	 * Callback functions for zenvy_default_theme_options.
	 * Add Footer Builder default values.
	 *
	 * @param array $default_options Default options array.
	 * @return array
	 */
	public function footer_defaults( $default_options = [] ) {

		$footer_defaults = [
			$this->builder_section_controller => [
				'desktop' => [
					'bottom' => [
						'col-0' => [
							[
								'id' => 'footer_copyright',
							],
						],
						'col-2' => [
							[
								'id' => 'footer_menu',
							],
						],
					],
				],
			],
		];

		return array_merge( $default_options, $footer_defaults );
	}

	/**
	 * Callback functions for zenvy_builders.
	 * Add Footer Builder elements.
	 *
	 * @param array $builder Builder fields.
	 * @return array
	 */
	public function footer_builder( $builder ) {

		$items = apply_filters(
			'zenvy_customizer_footer_builder_items',
			[
				Zenvy_Customizer_Footer_Builder()->footer_copyright => [
					'name'    => esc_html__( 'Copyright', 'zenvy' ),
					'id'      => Zenvy_Customizer_Footer_Builder()->footer_copyright,
					'section' => Zenvy_Customizer_Footer_Builder()->footer_copyright,
				],
				Zenvy_Customizer_Footer_Builder()->footer_menu      => [
					'name'    => esc_html__( 'Footer Menu', 'zenvy' ),
					'id'      => Zenvy_Customizer_Footer_Builder()->footer_menu,
					'section' => Zenvy_Customizer_Footer_Builder()->footer_menu,
				],
				Zenvy_Customizer_Footer_Builder()->footer_social    => [
					'name'    => esc_html__( 'Social Icons', 'zenvy' ),
					'id'      => Zenvy_Customizer_Footer_Builder()->footer_social,
					'section' => Zenvy_Customizer_Footer_Builder()->footer_social,
				],
				Zenvy_Customizer_Footer_Builder()->footer_button    => [
					'name'    => esc_html__( 'Button', 'zenvy' ),
					'id'      => Zenvy_Customizer_Footer_Builder()->footer_button,
					'section' => Zenvy_Customizer_Footer_Builder()->footer_button,
				],
				Zenvy_Customizer_Footer_Builder()->footer_html      => [
					'name'    => esc_html__( 'HTML', 'zenvy' ),
					'id'      => Zenvy_Customizer_Footer_Builder()->footer_html,
					'section' => Zenvy_Customizer_Footer_Builder()->footer_html,
				],
				Zenvy_Customizer_Footer_Builder()->footer_sidebar_1 => [
					'name'    => esc_html__( 'Footer Sidebar 1', 'zenvy' ),
					'id'      => Zenvy_Customizer_Footer_Builder()->footer_sidebar_1,
					'section' => Zenvy_Customizer_Footer_Builder()->footer_sidebar_1,
				],
				Zenvy_Customizer_Footer_Builder()->footer_sidebar_2 => [
					'name'    => esc_html__( 'Footer Sidebar 2', 'zenvy' ),
					'id'      => Zenvy_Customizer_Footer_Builder()->footer_sidebar_2,
					'section' => Zenvy_Customizer_Footer_Builder()->footer_sidebar_2,
				],
				Zenvy_Customizer_Footer_Builder()->footer_sidebar_3 => [
					'name'    => esc_html__( 'Footer Sidebar 3', 'zenvy' ),
					'id'      => Zenvy_Customizer_Footer_Builder()->footer_sidebar_3,
					'section' => Zenvy_Customizer_Footer_Builder()->footer_sidebar_3,
				],
				Zenvy_Customizer_Footer_Builder()->footer_sidebar_4 => [
					'name'    => esc_html__( 'Footer Sidebar 4', 'zenvy' ),
					'id'      => Zenvy_Customizer_Footer_Builder()->footer_sidebar_4,
					'section' => Zenvy_Customizer_Footer_Builder()->footer_sidebar_4,
				],
				Zenvy_Customizer_Footer_Builder()->footer_sidebar_5 => [
					'name'    => esc_html__( 'Footer Sidebar 5', 'zenvy' ),
					'id'      => Zenvy_Customizer_Footer_Builder()->footer_sidebar_5,
					'section' => Zenvy_Customizer_Footer_Builder()->footer_sidebar_5,
				],
				Zenvy_Customizer_Footer_Builder()->footer_sidebar_6 => [
					'name'    => esc_html__( 'Footer Sidebar 6', 'zenvy' ),
					'id'      => Zenvy_Customizer_Footer_Builder()->footer_sidebar_6,
					'section' => Zenvy_Customizer_Footer_Builder()->footer_sidebar_6,
				],
			]
		);

		$footer_builder = [
			Zenvy_Customizer_Footer_Builder()->panel => [
				'id'         => Zenvy_Customizer_Footer_Builder()->panel,
				'title'      => esc_html__( 'Footer Builder', 'zenvy' ),
				'control_id' => Zenvy_Customizer_Footer_Builder()->builder_section_controller,
				'panel'      => Zenvy_Customizer_Footer_Builder()->panel,
				'section'    => Zenvy_Customizer_Footer_Builder()->builder_section_controller,
				'devices'    => [
					'desktop' => esc_html__( 'Global', 'zenvy' ),
				],
				'items'      => $items,
				'rows'       => [
					'top'    => esc_html__( 'Top Row', 'zenvy' ),
					'main'   => esc_html__( 'Main Row', 'zenvy' ),
					'bottom' => esc_html__( 'Bottom Row', 'zenvy' ),
				],
				'cols'       => [
					'top'    => 3,
					'main'   => 3,
					'bottom' => 3,
				],
			],
		];

		$footer_builder = apply_filters( 'zenvy_customizer_footer_builder', $footer_builder );
		return array_merge( $builder, $footer_builder );
	}

	/**
	 * Callback functions for customize_register.
	 * Add Panel Section control.
	 *
	 * @param object $wp_customize WP Customize Manager instance.
	 * @return void
	 */
	public function customize_register( $wp_customize ) {

		$footer_defaults = self::footer_defaults();

		/**
		 * Add Panels.
		 */
		$wp_customize->add_panel(
			$this->panel,
			[
				'title'    => esc_html__( 'Footer Builder', 'zenvy' ),
				'priority' => 20,
			]
		);

		/**
		 * Add Sections.
		 */
		$wp_customize->add_section(
			$this->builder_section_controller,
			[
				'title'    => esc_html__( 'Footer Builder', 'zenvy' ),
				'priority' => 10,
				'panel'    => $this->panel,
			]
		);

		$wp_customize->add_section(
			$this->footer_top,
			[
				'title'    => esc_html__( 'Top Row', 'zenvy' ),
				'priority' => 20,
				'panel'    => $this->panel,
			]
		);

		$wp_customize->add_section(
			$this->footer_main,
			[
				'title'    => esc_html__( 'Main Row', 'zenvy' ),
				'priority' => 25,
				'panel'    => $this->panel,
			]
		);

		$wp_customize->add_section(
			$this->footer_bottom,
			[
				'title'    => esc_html__( 'Bottom Row', 'zenvy' ),
				'priority' => 25,
				'panel'    => $this->panel,
			]
		);

		$wp_customize->add_section(
			$this->footer_copyright,
			[
				'title'    => esc_html__( 'Copyright', 'zenvy' ),
				'priority' => 35,
				'panel'    => $this->panel,
			]
		);

		$wp_customize->add_section(
			$this->footer_social,
			[
				'title'    => esc_html__( 'Social Icons', 'zenvy' ),
				'priority' => 36,
				'panel'    => $this->panel,
			]
		);

		$wp_customize->add_section(
			$this->footer_button,
			[
				'title'    => esc_html__( 'Button', 'zenvy' ),
				'priority' => 37,
				'panel'    => $this->panel,
			]
		);

		$wp_customize->add_section(
			$this->footer_html,
			[
				'title'    => esc_html__( 'HTML', 'zenvy' ),
				'priority' => 38,
				'panel'    => $this->panel,
			]
		);

		$wp_customize->add_section(
			$this->footer_menu,
			[
				'title'    => esc_html__( 'Footer Menu', 'zenvy' ),
				'priority' => 39,
				'panel'    => $this->panel,
			]
		);

		$wp_customize->add_section(
			'zenvy_footer_builder_widget_section',
			[
				'title'    => esc_html__( 'Widget Settings', 'zenvy' ),
				'priority' => 65,
				'panel'    => $this->panel,
			]
		);

		$wp_customize->add_section(
			'zenvy_footer_builder_back_to_top_section',
			[
				'title'    => esc_html__( 'Scroll to Top', 'zenvy' ),
				'priority' => 65,
				'panel'    => $this->panel,
			]
		);

		/**
		 * Builder control and setting.
		 */
		$wp_customize->add_setting(
			$this->builder_section_controller,
			[
				'default'           => $footer_defaults[ $this->builder_section_controller ],
				'sanitize_callback' => 'zenvy_sanitize_field',
				'transport'         => 'postMessage',
			]
		);

		$wp_customize->add_control(
			$this->builder_section_controller,
			[
				'label'    => esc_html__( 'Header Builder', 'zenvy' ),
				'section'  => $this->builder_section_controller,
				'settings' => $this->builder_section_controller,
				'type'     => 'text',
			]
		);

		// Footer Builder Options.
		require ZENVY_THEME_DIR . 'inc/customizer/builder/footer/options/Zenvy_Customize_Footer_Top_Row_Fields.php';
		require ZENVY_THEME_DIR . 'inc/customizer/builder/footer/options/Zenvy_Customize_Footer_Main_Row_Fields.php';
		require ZENVY_THEME_DIR . 'inc/customizer/builder/footer/options/Zenvy_Customize_Footer_Bottom_Row_Fields.php';

		require ZENVY_THEME_DIR . 'inc/customizer/builder/footer/options/Zenvy_Customize_Footer_Copyright_Fields.php';
		require ZENVY_THEME_DIR . 'inc/customizer/builder/footer/options/Zenvy_Customize_Footer_Social_Icons_Fields.php';
		require ZENVY_THEME_DIR . 'inc/customizer/builder/footer/options/Zenvy_Customize_Footer_Button_Fields.php';
		require ZENVY_THEME_DIR . 'inc/customizer/builder/footer/options/Zenvy_Customize_Footer_Menu_Fields.php';
		require ZENVY_THEME_DIR . 'inc/customizer/builder/footer/options/Zenvy_Customize_Footer_Html_Fields.php';

		// Back to Top.
		require ZENVY_THEME_DIR . 'inc/customizer/builder/footer/options/Zenvy_Customize_Footer_Widget_Fields.php';
		require ZENVY_THEME_DIR . 'inc/customizer/builder/footer/options/Zenvy_Customize_Footer_Back_To_Top_Fields.php';
	}

	/**
	 * Column Element.
	 *
	 * @param array $column_elements Column elements array.
	 * @return void
	 */
	public function column_elements( $column_elements ) {
		foreach ( $column_elements as $element ) {
			$id = $element['id'];
			if ( file_exists( trailingslashit( get_template_directory() ) . 'template-parts/footer/' . esc_html( $id ) . '.php' ) ) {
				get_template_part( 'template-parts/footer/' . esc_html( $id ) );
			} else {
				echo esc_html__( 'Create New File ', 'zenvy' ) . 'template-parts/footer/' . esc_html( $id ) . '.php';
			}
		}
	}

	/**
	 * Callback Function for zenvy_action_footer.
	 * Display Footer Content.
	 *
	 * @return void
	 */
	public function zenvy_footer_display() {

		$builder = $this->get_builder();

		if ( isset( $builder['desktop'] ) && ! empty( $builder['desktop'] ) ) {

			$footer_builder_data = [];
			$active_sidebar      = [];
			$footer_builder      = $builder['desktop'];

			foreach ( $footer_builder as $key => $single_row ) {

				if ( ! empty( $single_row ) ) {

					foreach ( $single_row as $col_key => $columns ) {

						if ( ! empty( $columns ) ) {

							$footer_builder_data[ $key ][ $col_key ] = $columns;
							$active_sidebar[]                        = $columns[0]['id'];
						}
					}
				}
			}

			if ( ! empty( $footer_builder_data ) ) {
				$this->footer_content( $footer_builder_data );
			}

			// Load sidebar template parts.
			self::get_elements( $active_sidebar );
		}
	}

	/**
	 * Display Desktop Footer Content.
	 *
	 * @param array $footer_builder Footer builder data.
	 * @return void
	 */
	public function footer_content( $footer_builder ) {

		?>
		<div id="desktop-footer" class="desktop-footer-wrap" data-device="desktop">
			<?php
			if ( isset( $footer_builder['top'] ) ) {
				$top_elements       = $footer_builder['top'];
				$top_elements_count = count( $top_elements );
				$top_col_per_row    = [
					'desktop' => $top_elements_count,
					'tablet'  => $top_elements_count <= 2 ? $top_elements_count : 2,
					'mobile'  => 1,
				];
				?>
				<div class="d-flex align-items-center top-footer">
					<div class="container">
						<div class="row columns"<?php Zenvy_Helper::get_data_columns( $top_col_per_row ); ?>>
							<?php
							if ( array_key_exists( 'col-0', $top_elements ) ) :
								// Left Column Content Justify.
								$top_row_left_col    = get_theme_mod(
									'zenvy_footer_top_row_left_col_content_justify',
									[
										'desktop' => 'start',
										'tablet'  => 'start',
										'mobile'  => 'start',
									]
								);
								$top_lef_col_class   = [ 'column d-flex flex-wrap' ];
								$top_lef_col_class[] = zenvy_get_classes( $top_row_left_col, 'justify-content-' );
								?>
								<div class="<?php echo esc_attr( implode( ' ', $top_lef_col_class ) ); ?>">
									<?php $this->column_elements( $top_elements['col-0'] ); ?>
								</div>
							<?php endif; ?>
							<?php
							if ( array_key_exists( 'col-1', $top_elements ) ) :
								// Center Column Content Justify.
								$top_row_center_col     = get_theme_mod(
									'zenvy_footer_top_row_center_col_content_justify',
									[
										'desktop' => 'center',
										'tablet'  => 'center',
										'mobile'  => 'center',
									]
								);
								$top_center_col_class   = [ 'column d-flex flex-wrap' ];
								$top_center_col_class[] = zenvy_get_classes( $top_row_center_col, 'justify-content-' );
								?>
								<div class="<?php echo esc_attr( implode( ' ', $top_center_col_class ) ); ?>">
									<?php $this->column_elements( $top_elements['col-1'] ); ?>
								</div>
							<?php endif; ?>
							<?php
							if ( array_key_exists( 'col-2', $top_elements ) ) :
								// Right Column Content Justify.
								$top_row_right_col     = get_theme_mod(
									'zenvy_footer_top_row_right_col_content_justify',
									[
										'desktop' => 'end',
										'tablet'  => 'end',
										'mobile'  => 'end',
									]
								);
								$top_right_col_class   = [ 'column d-flex flex-wrap' ];
								$top_right_col_class[] = zenvy_get_classes( $top_row_right_col, 'justify-content-' );
								?>
								<div class="<?php echo esc_attr( implode( ' ', $top_right_col_class ) ); ?>">
									<?php $this->column_elements( $top_elements['col-2'] ); ?>
								</div>
							<?php endif; ?>
						</div>
					</div>
				</div><!-- .top-footer -->
				<?php
			}

			if ( isset( $footer_builder['main'] ) ) {
				$main_elements       = $footer_builder['main'];
				$main_elements_count = count( $main_elements );
				$main_col_per_row    = [
					'desktop' => $main_elements_count,
					'tablet'  => $main_elements_count <= 2 ? $main_elements_count : 2,
					'mobile'  => 1,
				];
				?>
				<div class="d-flex align-items-center main-footer">
					<div class="container">
						<div class="row columns"<?php Zenvy_Helper::get_data_columns( $main_col_per_row ); ?>>
							<?php
							if ( array_key_exists( 'col-0', $main_elements ) ) :
								// Left Column Content Justify.
								$main_row_left_col    = get_theme_mod(
									'zenvy_footer_main_row_left_col_content_justify',
									[
										'desktop' => 'start',
										'tablet'  => 'start',
										'mobile'  => 'start',
									]
								);
								$main_lef_col_class   = [ 'column d-flex flex-wrap' ];
								$main_lef_col_class[] = zenvy_get_classes( $main_row_left_col, 'justify-content-' );
								?>
								<div class="<?php echo esc_attr( implode( ' ', $main_lef_col_class ) ); ?>">
									<?php $this->column_elements( $main_elements['col-0'] ); ?>
								</div>
							<?php endif; ?>
							<?php
							if ( array_key_exists( 'col-1', $main_elements ) ) :
								// Center Column Content Justify.
								$main_row_center_col     = get_theme_mod(
									'zenvy_footer_main_row_center_col_content_justify',
									[
										'desktop' => 'center',
										'tablet'  => 'center',
										'mobile'  => 'center',
									]
								);
								$main_center_col_class   = [ 'column d-flex flex-wrap' ];
								$main_center_col_class[] = zenvy_get_classes( $main_row_center_col, 'justify-content-' );
								?>
								<div class="<?php echo esc_attr( implode( ' ', $main_center_col_class ) ); ?>">
									<?php $this->column_elements( $main_elements['col-1'] ); ?>
								</div>
							<?php endif; ?>
							<?php
							if ( array_key_exists( 'col-2', $main_elements ) ) :
								// Right Column Content Justify.
								$main_row_right_col     = get_theme_mod(
									'zenvy_footer_main_row_right_col_content_justify',
									[
										'desktop' => 'end',
										'tablet'  => 'end',
										'mobile'  => 'end',
									]
								);
								$main_right_col_class   = [ 'column d-flex flex-wrap' ];
								$main_right_col_class[] = zenvy_get_classes( $main_row_right_col, 'justify-content-' );
								?>
								<div class="<?php echo esc_attr( implode( ' ', $main_right_col_class ) ); ?>">
									<?php $this->column_elements( $main_elements['col-2'] ); ?>
								</div>
							<?php endif; ?>
						</div>
					</div>
				</div><!-- .main-footer -->
				<?php
			}

			if ( isset( $footer_builder['bottom'] ) ) {
				$bottom_elements       = $footer_builder['bottom'];
				$bottom_elements_count = count( $bottom_elements );
				$bottom_col_per_row    = [
					'desktop' => $bottom_elements_count,
					'tablet'  => $bottom_elements_count <= 2 ? $bottom_elements_count : 2,
					'mobile'  => 1,
				];
				?>
				<div class="d-flex align-items-center bottom-footer">
					<div class="container">
						<div class="row columns"<?php Zenvy_Helper::get_data_columns( $bottom_col_per_row ); ?>>
							<?php
							if ( array_key_exists( 'col-0', $bottom_elements ) ) :
								// Left Column Content Justify.
								$bottom_row_left_col    = get_theme_mod(
									'zenvy_footer_bottom_row_left_col_content_justify',
									[
										'desktop' => 'start',
										'tablet'  => 'start',
										'mobile'  => 'center',
									]
								);
								$bottom_lef_col_class   = [ 'column d-flex flex-wrap' ];
								$bottom_lef_col_class[] = zenvy_get_classes( $bottom_row_left_col, 'justify-content-' );
								?>
								<div class="<?php echo esc_attr( implode( ' ', $bottom_lef_col_class ) ); ?>">
									<?php $this->column_elements( $bottom_elements['col-0'] ); ?>
								</div>
							<?php endif; ?>
							<?php
							if ( array_key_exists( 'col-1', $bottom_elements ) ) :
								// Center Column Content Justify.
								$bottom_row_center_col     = get_theme_mod(
									'zenvy_footer_bottom_row_center_col_content_justify',
									[
										'desktop' => 'center',
										'tablet'  => 'center',
										'mobile'  => 'center',
									]
								);
								$bottom_center_col_class   = [ 'column d-flex flex-wrap' ];
								$bottom_center_col_class[] = zenvy_get_classes( $bottom_row_center_col, 'justify-content-' );
								?>
								<div class="<?php echo esc_attr( implode( ' ', $bottom_center_col_class ) ); ?>">
									<?php $this->column_elements( $bottom_elements['col-1'] ); ?>
								</div>
							<?php endif; ?>
							<?php
							if ( array_key_exists( 'col-2', $bottom_elements ) ) :
								// Right Column Content Justify.
								$bottom_row_right_col     = get_theme_mod(
									'zenvy_footer_bottom_row_right_col_content_justify',
									[
										'desktop' => 'end',
										'tablet'  => 'end',
										'mobile'  => 'center',
									]
								);
								$bottom_right_col_class   = [ 'column d-flex flex-wrap' ];
								$bottom_right_col_class[] = zenvy_get_classes( $bottom_row_right_col, 'justify-content-' );
								?>
								<div class="<?php echo esc_attr( implode( ' ', $bottom_right_col_class ) ); ?>">
									<?php $this->column_elements( $bottom_elements['col-2'] ); ?>
								</div>
							<?php endif; ?>
						</div>
					</div>
				</div><!-- .bottom-footer -->
				<?php
			}
			?>
		</div><!-- #desktop-footer -->
		<?php
	}

	/**
	 * Footer get_elements only for the sidebar so that we can see sidebar in customizer.
	 *
	 * @param array $sidebar_elements Sidebar elements array.
	 * @return void
	 */
	public function get_elements( $sidebar_elements ) {

		if ( is_array( $sidebar_elements ) ) {

			$sidebar_array    = [
				'sidebar-widgets-footer-sidebar-1',
				'sidebar-widgets-footer-sidebar-2',
				'sidebar-widgets-footer-sidebar-3',
				'sidebar-widgets-footer-sidebar-4',
				'sidebar-widgets-footer-sidebar-5',
				'sidebar-widgets-footer-sidebar-6',
			];
			$sidebar_elements = array_diff( $sidebar_array, $sidebar_elements );
			echo '<div class="container d-none">';
			foreach ( $sidebar_elements as $key ) {
				if ( file_exists( trailingslashit( get_template_directory() ) . 'template-parts/footer/' . esc_html( $key ) . '.php' ) ) {
					get_template_part( 'template-parts/footer/' . esc_html( $key ) );
				}
			}
			echo '</div><!-- .d-none -->';
		}
	}
}

/**
 * Create Instance for Zenvy_Customizer_Footer_Builder.
 *
 * @return object
 */
if ( ! function_exists( 'zenvy_customizer_footer_builder' ) ) {

	/**
	 * Return the Zenvy_Customizer_Footer_Builder instance.
	 *
	 * @return object
	 */
	function zenvy_customizer_footer_builder() {
		return Zenvy_Customizer_Footer_Builder::instance();
	}

	zenvy_customizer_footer_builder()->run();
}

/**
 * Get footer builder default options.
 *
 * @param string $key Option key.
 * @return mixed
 */
if ( ! function_exists( 'zenvy_get_footer_builder_options' ) ) :

	/**
	 * Get footer builder default options.
	 *
	 * @param string $key Option key to retrieve.
	 * @return mixed
	 */
	function zenvy_get_footer_builder_options( $key = '' ) {
		if ( ! empty( $key ) ) {
			$footer_default_values = Zenvy_Customizer_Footer_Builder()->footer_defaults();
			$theme_mod_values      = get_theme_mod( $key, isset( $footer_default_values[ $key ] ) ? $footer_default_values[ $key ] : '' );
			return apply_filters( 'zenvy_' . $key, $theme_mod_values );
		}
		return false;
	}
endif;