<?php
/**
 * Filter the fields for the Divi module.
 *
 * @param array $fields_unprocessed
 *
 * @return array
 */
if (!function_exists('pac_drh_filter_tabs_fields')):
    function pac_drh_filter_tabs_fields($fields_unprocessed)
    {
        $custom_fields = [];
        $custom_fields['pac_drh_tabs_layout_tablet'] = [
            'label' => __('Layout On Tablet', 'divi-responsive-helper'),
            'type' => 'select',
            'options' => [
                'off' => __('Default', 'divi-responsive-helper'),
                'pac_drh_tabs_vertical_tab' => __('Vertical', 'divi-responsive-helper'),
            ],
            'default' => 'off',
            'description' => __('Choose a tab layout to use on Tablet.', 'divi-responsive-helper'),
            'tab_slug' => 'advanced',
        ];
        $custom_fields['pac_drh_tabs_layout_phone'] = [
            'label' => __('Layout On Phone', 'divi-responsive-helper'),
            'type' => 'select',
            'options' => [
                'off' => __('Default', 'divi-responsive-helper'),
                'pac_drh_tabs_horizontal_phone' => __('Horizontal', 'divi-responsive-helper'),
            ],
            'default' => 'off',
            'description' => __('Choose a tab layout to use on Phone.', 'divi-responsive-helper'),
            'tab_slug' => 'advanced',
        ];

        return wp_parse_args($custom_fields, $fields_unprocessed);
    }

    add_filter('et_pb_all_fields_unprocessed_et_pb_tabs', 'pac_drh_filter_tabs_fields');
endif;
/**
 * Filter function to modify Divi module shortcode attributes.
 *
 * @param array $props
 * @param array $attrs
 * @param string $render_slug
 * @param string $_address
 * @param string $content
 *
 * @return array
 */
if (!function_exists('pac_drh_filter_tab_output')):
    function pac_drh_filter_tab_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_tabs' !== $render_slug) {
            return $output;
        }
        $tabs_layout_tablet = isset($module->props['pac_drh_tabs_layout_tablet']) ? $module->props['pac_drh_tabs_layout_tablet'] : 'off';
        if ('off' !== $tabs_layout_tablet) {
            $output = preg_replace('/\bet_pb_module\b/', 'et_pb_module '.$tabs_layout_tablet, $output);
        }
        $tabs_layout_phone = isset($module->props['pac_drh_tabs_layout_phone']) ? $module->props['pac_drh_tabs_layout_phone'] : 'off';
        if ('off' !== $tabs_layout_phone) {
            $output = preg_replace('/\bet_pb_module\b/', 'et_pb_module '.$tabs_layout_phone, $output);
        }

        return $output;
    }

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