<?php
/**
 * Filter the fields for the Divi module.
 * @param array $fields_unprocessed
 *
 * @return array
 */
if (!function_exists('pac_drh_filter_blog_fields')):
    function pac_drh_filter_blog_fields($fields_unprocessed)
    {
        $custom_fields = [];
        // Number Of Posts
        if (pac_drh_get_settings_enabled('pac_drh_enable_responsive_blog_count')) {
            $fields_unprocessed['posts_number']['mobile_options'] = true;
        }
        // Number of Columns
        if (pac_drh_get_settings_enabled('pac_drh_enable_blog_number_of_columns')) {
            $custom_fields['pac_drh_column_numbers_desktop'] = [
                'label' => __('Number of Blog Columns on Desktop', 'divi-responsive-helper'),
                'type' => 'select',
                'options' => [
                    'off' => __('Default', 'divi-responsive-helper'),
                    'pac_drh_desktop_col_one' => __('One', 'divi-responsive-helper'),
                    'pac_drh_desktop_col_two' => __('Two', 'divi-responsive-helper'),
                    'pac_drh_desktop_col_three' => __('Three', 'divi-responsive-helper'),
                    'pac_drh_desktop_col_four' => __('Four', 'divi-responsive-helper'),
                ],
                'default' => 'off',
                'description' => __('Select the number of blog columns to show on Desktop.', 'divi-responsive-helper'),
                'tab_slug' => 'advanced',
                'toggle_slug' => 'layout',
                'show_if' => [
                    'fullwidth' => 'off',
                ],
            ];
            $custom_fields['pac_drh_column_numbers_tablet'] = [
                'label' => __('Number of Blog 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'),
                ],
                'default' => 'off',
                'description' => __('Select the number of blog columns to show on Tablet.', 'divi-responsive-helper'),
                'tab_slug' => 'advanced',
                'toggle_slug' => 'layout',
                'show_if' => [
                    'fullwidth' => 'off',
                ],
            ];
            $custom_fields['pac_drh_column_numbers_phone'] = [
                'label' => __('Number of Blog Columns on Phone', 'divi-responsive-helper'),
                'type' => 'select',
                'options' => [
                    'off' => __('Default', 'divi-responsive-helper'),
                    'pac_drh_ph_col_one' => __('One', 'divi-responsive-helper'),
                    'pac_drh_ph_col_two' => __('Two', 'divi-responsive-helper'),
                    'pac_drh_ph_col_three' => __('Three', 'divi-responsive-helper'),
                    'pac_drh_ph_col_four' => __('Four', 'divi-responsive-helper'),
                ],
                'default' => 'off',
                'description' => __('Select the number of blog columns to show on Phone.', 'divi-responsive-helper'),
                'tab_slug' => 'advanced',
                'toggle_slug' => 'layout',
                'show_if' => [
                    'fullwidth' => 'off',
                ],
            ];
        }

        return wp_parse_args($custom_fields, $fields_unprocessed);
    }

    add_filter('et_pb_all_fields_unprocessed_et_pb_blog', 'pac_drh_filter_blog_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_blog_props')):
    function pac_drh_filter_blog_props($props, $attrs, $render_slug, $_address, $content)
    {
        if (function_exists('et_fb_is_enabled') && et_fb_is_enabled()) {
            return $props;
        }
        if (function_exists('et_builder_bfb_enabled') && et_builder_bfb_enabled()) {
            return $props;
        }
        if (is_admin() || wp_doing_ajax()) {
            return $props;
        }
        if ('et_pb_blog' !== $render_slug) {
            return $props;
        }
        $posts_number = isset($props['posts_number']) ? esc_html($props['posts_number']) : 10;
        $posts_number_values = et_pb_responsive_options()->get_property_values($props, 'posts_number');
        $posts_number_tablet = !empty($posts_number_values['tablet']) ? $posts_number_values['tablet'] : $posts_number;
        $posts_number_phone = !empty($posts_number_values['phone']) ? $posts_number_values['phone'] : $posts_number_tablet;
        if (et_pb_responsive_options()->is_responsive_enabled($props, 'posts_number')) {
            $detect_mobile = pac_drh_detect_mobile();
            if ($detect_mobile === null) {
                return $props;
            }
            if (!empty($posts_number_tablet) && $detect_mobile->isTablet()) {
                $props['posts_number'] = $posts_number_tablet;
            } elseif (!empty($posts_number_phone) && $detect_mobile->isMobile()) {
                $props['posts_number'] = $posts_number_phone;
            }
        }

        return $props;
    }

    if (pac_drh_get_settings_enabled('pac_drh_enable_responsive_blog_count')) {
        add_filter('et_pb_module_shortcode_attributes', 'pac_drh_filter_blog_props', 10, 5);
    }
endif;
/**
 * Filter function to modify the output of the Divi module.
 * This function checks various conditions before modifying the output
 *
 * @param string $output
 * @param string $render_slug
 * @param object $module
 *
 * @return string
 */
if (!function_exists('pac_drh_filter_blog_output')):
    function pac_drh_filter_blog_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_blog' !== $render_slug) {
            return $output;
        }
        if ('on' === $module->props['fullwidth']) {
            return $output;
        }
        $column_numbers_desktop = isset($module->props['pac_drh_column_numbers_desktop']) ? $module->props['pac_drh_column_numbers_desktop'] : 'off';
        $column_numbers_tablet = isset($module->props['pac_drh_column_numbers_tablet']) ? $module->props['pac_drh_column_numbers_tablet'] : 'off';
        $column_numbers_phone = isset($module->props['pac_drh_column_numbers_phone']) ? $module->props['pac_drh_column_numbers_phone'] : 'off';
        if ('off' !== $column_numbers_desktop) {
            $output = preg_replace('/\bet_pb_module\b/', 'et_pb_module '.$column_numbers_desktop, $output);
        }
        if ('off' !== $column_numbers_tablet) {
            $output = preg_replace('/\bet_pb_module\b/', 'et_pb_module '.$column_numbers_tablet, $output);
        }
        if ('off' !== $column_numbers_phone) {
            $output = preg_replace('/\bet_pb_module\b/', 'et_pb_module '.$column_numbers_phone, $output);
        }

        return $output;
    }

    if (pac_drh_get_settings_enabled('pac_drh_enable_blog_number_of_columns')) {
        add_filter('et_module_shortcode_output', 'pac_drh_filter_blog_output', 10, 3);
    }
endif;
