<?php
/**
 * Filters row stacking fields for Divi Responsive Helper plugin.
 *
 * @param array $fields_unprocessed
 *
 * @return array
 */
if (!function_exists('pac_drh_filter_row_stacking_fields')):
    function pac_drh_filter_row_stacking_fields($fields_unprocessed)
    {
        $custom_fields = [];
        // Column Stacking
        if (pac_drh_get_settings_enabled('pac_drh_enable_col_stacking')) {
            $custom_fields['pac_drh_enable_stacking'] = [
                'label' => __('Enable Column Stacking Order', 'divi-responsive-helper'),
                'type' => 'select',
                'options' => [
                    'off' => __('Default Column Stacking Order', 'divi-responsive-helper'),
                    'pac_drh_stack_row pac_drh_stack_tab_mob' => __('Enable Column Stacking Order on both Tablet and Phone', 'divi-responsive-helper'),
                    'pac_drh_stack_row pac_drh_stack_tab_only' => __('Enable Column Stacking Order on Tablet only', 'divi-responsive-helper'),
                    'pac_drh_stack_row pac_drh_stack_mob_only' => __('Enable Column Stacking Order on Phone only', 'divi-responsive-helper'),
                ],
                'default' => 'off',
                'description' => __('Choose which Column Stacking Order option to use.', 'divi-responsive-helper'),
                'tab_slug' => 'custom_css',
                'toggle_slug' => 'classes',
            ];
        }
        // Column Number
        if (pac_drh_get_settings_enabled('pac_drh_enable_number_of_columns')) {
            $custom_fields['pac_drh_column_numbers_tablet'] = [
                'label' => __('Number of Columns on Tablet', 'divi-responsive-helper'),
                'type' => 'select',
                'options' => [
                    'off' => __('Default', 'divi-responsive-helper'),
                    'pac_drh_tab_col_one' => __('One', 'divi-responsive-helper'),
                    'pac_drh_tab_col_two' => __('Two', 'divi-responsive-helper'),
                    'pac_drh_tab_col_three' => __('Three', 'divi-responsive-helper'),
                    'pac_drh_tab_col_four' => __('Four', 'divi-responsive-helper'),
                    'pac_drh_tab_col_five' => __('Five', 'divi-responsive-helper'),
                    'pac_drh_tab_col_six' => __('Six', 'divi-responsive-helper'),
                ],
                'default' => 'off',
                'description' => __('Select Number of Columns on Tablet', 'divi-responsive-helper'),
                'tab_slug' => 'custom_css',
                'toggle_slug' => 'classes',
            ];
            $custom_fields['pac_drh_column_numbers_phone'] = [
                'label' => __('Number of Columns on Phone', 'divi-responsive-helper'),
                'type' => 'select',
                'options' => [
                    'off' => __('Default', 'divi-responsive-helper'),
                    'pac_drh_phone_col_one' => __('One', 'divi-responsive-helper'),
                    'pac_drh_phone_col_two' => __('Two', 'divi-responsive-helper'),
                    'pac_drh_phone_col_three' => __('Three', 'divi-responsive-helper'),
                    'pac_drh_phone_col_four' => __('Four', 'divi-responsive-helper'),
                    'pac_drh_phone_col_five' => __('Five', 'divi-responsive-helper'),
                    'pac_drh_phone_col_six' => __('Six', 'divi-responsive-helper'),
                ],
                'default' => 'off',
                'description' => __('Select Number of Columns on Phone', 'divi-responsive-helper'),
                'tab_slug' => 'custom_css',
                'toggle_slug' => 'classes',
            ];
        }

        return wp_parse_args($custom_fields, $fields_unprocessed);
    }

    add_filter('et_pb_all_fields_unprocessed_et_pb_row', 'pac_drh_filter_row_stacking_fields');
    add_filter('et_pb_all_fields_unprocessed_et_pb_row_inner', 'pac_drh_filter_row_stacking_fields');
endif;
/**
 * Filters the custom column stacking fields for Divi Responsive Helper plugin.
 *
 * @param array $fields_unprocessed
 *
 * @return array
 */
if (!function_exists('pac_drh_filter_column_stacking_fields')):
    function pac_drh_filter_column_stacking_fields($fields_unprocessed)
    {

        $custom_fields = [];
        // Column Stacking
        if (pac_drh_get_settings_enabled('pac_drh_enable_col_stacking')) {
            $custom_fields['pac_drh_add_staking_order_tab'] = [
                'label' => __('Stacking Order on Tablet', 'divi-responsive-helper'),
                'type' => 'select',
                'options' => [
                    'off' => __('Stacking Order on Tablet', 'divi-responsive-helper'),
                    'pac_drh_order_tab_one' => __('One', 'divi-responsive-helper'),
                    'pac_drh_order_tab_two' => __('Two', 'divi-responsive-helper'),
                    'pac_drh_order_tab_three' => __('Three', 'divi-responsive-helper'),
                    'pac_drh_order_tab_four' => __('Four', 'divi-responsive-helper'),
                    'pac_drh_order_tab_five' => __('Five', 'divi-responsive-helper'),
                    'pac_drh_order_tab_six' => __('Six', 'divi-responsive-helper'),
                    'pac_drh_order_tab_seven' => __('Seven', 'divi-responsive-helper'),
                    'pac_drh_order_tab_eight' => __('Eight', 'divi-responsive-helper'),
                ],
                'default' => 'off',
                'description' => __('Select a number to designate the order in which the columns will stack on Tablet.', 'divi-responsive-helper'),
                'tab_slug' => 'custom_css',
                'toggle_slug' => 'classes',
            ];
            $custom_fields['pac_drh_add_staking_order_mob'] = [
                'label' => __('Stacking Order on Phone', 'divi-responsive-helper'),
                'type' => 'select',
                'options' => [
                    'off' => __('Stacking Order on Phone', 'divi-responsive-helper'),
                    'pac_drh_order_mob_one' => __('One', 'divi-responsive-helper'),
                    'pac_drh_order_mob_two' => __('Two', 'divi-responsive-helper'),
                    'pac_drh_order_mob_three' => __('Three', 'divi-responsive-helper'),
                    'pac_drh_order_mob_four' => __('Four', 'divi-responsive-helper'),
                    'pac_drh_order_mob_five' => __('Five', 'divi-responsive-helper'),
                    'pac_drh_order_mob_six' => __('Six', 'divi-responsive-helper'),
                    'pac_drh_order_mob_seven' => __('Seven', 'divi-responsive-helper'),
                    'pac_drh_order_mob_eight' => __('Eight', 'divi-responsive-helper'),
                ],
                'default' => 'off',
                'description' => __('Select a number to designate the order in which the columns will stack on Phone.', 'divi-responsive-helper'),
                'tab_slug' => 'custom_css',
                'toggle_slug' => 'classes',
            ];
        }

        return wp_parse_args($custom_fields, $fields_unprocessed);
    }

    add_filter('et_pb_all_fields_unprocessed_et_pb_column', 'pac_drh_filter_column_stacking_fields');
endif;
/**
 * Filter function to modify the output of Divi Builder row modules for custom row stacking and column number settings.
 *
 * @param string $output
 * @param string $render_slug
 * @param object $module
 *
 * @return string
 */
if (!function_exists('pac_drh_filter_row_stacking_output')):
    function pac_drh_filter_row_stacking_output($output, $render_slug, $module)
    {
        if (function_exists('et_fb_is_enabled') && et_fb_is_enabled()) {
            return $output;
        }
        if (function_exists('et_builder_bfb_enabled') && et_builder_bfb_enabled()) {
            return $output;
        }
        if (is_admin() || wp_doing_ajax() || is_array($output)) {
            return $output;
        }
        if ('et_pb_row' !== $render_slug && 'et_pb_row_inner' !== $render_slug) {
            return $output;
        }
        // Column Stacking
        if (pac_drh_get_settings_enabled('pac_drh_enable_col_stacking')) {
            $enable_stacking = isset($module->props['pac_drh_enable_stacking']) ? $module->props['pac_drh_enable_stacking'] : 'off';
            if ('off' !== $enable_stacking) {
                $output = preg_replace("/\b$render_slug\b/", "$render_slug {$module->props['pac_drh_enable_stacking']}", $output);
            }
        }
        // Column Number
        if (pac_drh_get_settings_enabled('pac_drh_enable_number_of_columns')) {
            $div_columns = explode(",", $module->props['column_structure']);
            $column_structure = preg_replace("/\d_/", "", end($div_columns));
            $tablet_columns = [
                'pac_drh_tab_col_one' => 1,
                'pac_drh_tab_col_two' => 2,
                'pac_drh_tab_col_three' => 3,
                'pac_drh_tab_col_four' => 4,
                'pac_drh_tab_col_five' => 5,
                'pac_drh_tab_col_six' => 6,
            ];
            $phone_columns = [
                'pac_drh_phone_col_one' => 1,
                'pac_drh_phone_col_two' => 2,
                'pac_drh_phone_col_three' => 3,
                'pac_drh_phone_col_four' => 4,
                'pac_drh_phone_col_five' => 5,
                'pac_drh_phone_col_six' => 6,
            ];
            // Tablet
            $column_numbers_tablet = isset($module->props['pac_drh_column_numbers_tablet']) ? $module->props['pac_drh_column_numbers_tablet'] : 'off';
            if ('off' !== $column_numbers_tablet) {
                $col_on_tab = $tablet_columns[$column_numbers_tablet];
                if ('pac_drh_tab_col_two' === $column_numbers_tablet && ($column_structure % $col_on_tab) === 1 && 5 !== count($div_columns)) {
                    $output = preg_replace("/\b$render_slug\b/", "$render_slug pac_drh_tab_fix_2_last_1_col $column_numbers_tablet", $output);
                } elseif ('pac_drh_tab_col_three' === $column_numbers_tablet && ($column_structure % $col_on_tab) === 1) {
                    $output = preg_replace("/\b$render_slug\b/", "$render_slug pac_drh_tab_fix_3_last_1_col $column_numbers_tablet", $output);
                } elseif ('pac_drh_tab_col_three' === $column_numbers_tablet && ($column_structure % $col_on_tab) === 2) {
                    $output = preg_replace("/\b$render_slug\b/", "$render_slug pac_drh_tab_fix_3_last_2_col $column_numbers_tablet", $output);
                } elseif ('pac_drh_tab_col_four' === $column_numbers_tablet && ($column_structure % $col_on_tab) === 1) {
                    $output = preg_replace("/\b$render_slug\b/", "$render_slug pac_drh_tab_fix_4_last_1_col $column_numbers_tablet", $output);
                } elseif ('pac_drh_tab_col_four' === $column_numbers_tablet && ($column_structure % $col_on_tab) === 2) {
                    $output = preg_replace("/\b$render_slug\b/", "$render_slug pac_drh_tab_fix_4_last_2_col $column_numbers_tablet", $output);
                } elseif ('pac_drh_tab_col_five' === $column_numbers_tablet && ($column_structure % $col_on_tab) === 1) {
                    $output = preg_replace("/\b$render_slug\b/", "$render_slug pac_drh_tab_fix_5_last_1_col $column_numbers_tablet", $output);
                } else {
                    $output = preg_replace("/\b$render_slug\b/", "$render_slug $column_numbers_tablet", $output);
                }
            }
            // Phone
            $column_numbers_phone = isset($module->props['pac_drh_column_numbers_phone']) ? $module->props['pac_drh_column_numbers_phone'] : 'off';
            if ('off' !== $column_numbers_phone) {
                $col_on_phone = $phone_columns[$column_numbers_phone];
                if ('pac_drh_phone_col_two' === $column_numbers_phone && ($column_structure % $col_on_phone) === 1) {
                    $output = preg_replace("/\b$render_slug\b/", "$render_slug pac_drh_phone_fix_2_last_1_col $column_numbers_phone", $output);
                } elseif ('pac_drh_phone_col_three' === $column_numbers_phone && ($column_structure % $col_on_phone) === 1) {
                    $output = preg_replace("/\b$render_slug\b/", "$render_slug pac_drh_phone_fix_3_last_1_col $column_numbers_phone", $output);
                } elseif ('pac_drh_phone_col_three' === $column_numbers_phone && ($column_structure % $col_on_phone) === 2) {
                    $output = preg_replace("/\b$render_slug\b/", "$render_slug pac_drh_phone_fix_3_last_2_col $column_numbers_phone", $output);
                } elseif ('pac_drh_phone_col_four' === $column_numbers_phone && ($column_structure % $col_on_phone) === 1) {
                    $output = preg_replace("/\b$render_slug\b/", "$render_slug pac_drh_phone_fix_4_last_1_col $column_numbers_phone", $output);
                } elseif ('pac_drh_phone_col_four' === $column_numbers_phone && ($column_structure % $col_on_phone) === 2) {
                    $output = preg_replace("/\b$render_slug\b/", "$render_slug pac_drh_phone_fix_4_last_2_col $column_numbers_phone", $output);
                } elseif ('pac_drh_phone_col_five' === $column_numbers_phone && ($column_structure % $col_on_phone) === 1) {
                    $output = preg_replace("/\b$render_slug\b/", "$render_slug pac_drh_phone_fix_5_last_1_col $column_numbers_phone", $output);
                } else {
                    $output = preg_replace("/\b$render_slug\b/", "$render_slug $column_numbers_phone", $output);
                }
            }
        }

        return $output;
    }

    add_filter('et_module_shortcode_output', 'pac_drh_filter_row_stacking_output', 10, 3);
endif;
/**
 * Filters the output of a Divi module shortcode to add column stacking functionality.
 *
 * @param string $output
 * @param string $render_slug
 * @param object $module
 *
 * @return string The modified output of the Divi module shortcode.
 */
if (!function_exists('pac_drh_filter_column_stacking_output')):
    function pac_drh_filter_column_stacking_output($output, $render_slug, $module)
    {
        if (function_exists('et_fb_is_enabled') && et_fb_is_enabled()) {
            return $output;
        }
        if (function_exists('et_builder_bfb_enabled') && et_builder_bfb_enabled()) {
            return $output;
        }
        if (is_admin() || wp_doing_ajax() || is_array($output)) {
            return $output;
        }
        if ('et_pb_column' !== $render_slug && 'et_pb_column_inner' !== $render_slug) {
            return $output;
        }
        // Column Stacking
        if (pac_drh_get_settings_enabled('pac_drh_enable_col_stacking')) {
            $staking_order_tab = isset($module->props['pac_drh_add_staking_order_tab']) ? $module->props['pac_drh_add_staking_order_tab'] : 'off';
            $staking_order_mob = isset($module->props['pac_drh_add_staking_order_mob']) ? $module->props['pac_drh_add_staking_order_mob'] : 'off';
            if ('off' !== $staking_order_tab) {
                $output = preg_replace("/\b$render_slug\b/", "$render_slug {$module->props['pac_drh_add_staking_order_tab']}", $output);
            }
            if ('off' !== $staking_order_mob) {
                $output = preg_replace("/\b$render_slug\b/", "$render_slug {$module->props['pac_drh_add_staking_order_mob']}", $output);
            }
        }

        return $output;
    }

    add_filter('et_module_shortcode_output', 'pac_drh_filter_column_stacking_output', 10, 3);
endif;
