<?php
/**
 * Filter Fields
 *
 * @param $fields_unprocessed
 *
 * @return array|object
 */
if (!function_exists('pac_drh_filter_gallery_fields')):
    function pac_drh_filter_gallery_fields($fields_unprocessed)
    {
        $custom_fields = [];
        // Number Of Posts
        if (pac_drh_get_settings_enabled('pac_drh_enable_responsive_gallery_count')) {
            $fields_unprocessed['posts_number']['mobile_options'] = true;
        }
        // Column Numbering
        if (pac_drh_get_settings_enabled('pac_drh_enable_gallery_number_of_columns')) {
            $custom_fields['pac_drh_column_numbers_desktop'] = [
                'label' => __('Number of Image 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'),
                    'pac_drh_desktop_col_five' => __('Five', 'divi-responsive-helper'),
                    'pac_drh_desktop_col_six' => __('Six', 'divi-responsive-helper'),
                ],
                'default' => 'off',
                'description' => __('Select the number of image 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 Image 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 the number of image 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 Image 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'),
                    'pac_drh_ph_col_five' => __('Five', 'divi-responsive-helper'),
                    'pac_drh_ph_col_six' => __('Six', 'divi-responsive-helper'),
                ],
                'default' => 'off',
                'description' => __('Select the number of image 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_gallery', 'pac_drh_filter_gallery_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_gallery_output')):
    function pac_drh_filter_gallery_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_gallery' !== $render_slug) {
            return $output;
        }
        if (!str_contains($output, 'et_pb_gallery_grid')) {
            return $output;
        }
        $props = $module->props;
        // Column Numbering
        if (pac_drh_get_settings_enabled('pac_drh_enable_gallery_number_of_columns')) {
            $gallery_ids = $props['gallery_ids'];
            $gallery_ids = count(explode(',', $gallery_ids));
            $column_numbers_desktop = isset($props['pac_drh_column_numbers_desktop']) ? $props['pac_drh_column_numbers_desktop'] : 'off';
            $column_numbers_tablet = isset($props['pac_drh_column_numbers_tablet']) ? $props['pac_drh_column_numbers_tablet'] : 'off';
            $column_numbers_phone = isset($props['pac_drh_column_numbers_phone']) ? $props['pac_drh_column_numbers_phone'] : 'off';
            if ('off' !== $column_numbers_desktop) {
                if ($column_numbers_desktop === 'pac_drh_desktop_col_three' & (($gallery_ids % 3) === 1)) {
                    $output = preg_replace('/\bet_pb_module et_pb_gallery\b/', 'et_pb_module et_pb_gallery pac_drh_desktop_3_col_last_r_1_element_fix', $output);
                }
                $output = preg_replace('/\bet_pb_module et_pb_gallery\b/', 'et_pb_module et_pb_gallery '.$column_numbers_desktop, $output);
            }
            if ('off' !== $column_numbers_tablet) {
                if ($column_numbers_tablet === 'pac_drh_tab_col_four' & (($gallery_ids % 4) === 1)) {
                    $output = preg_replace('/\bet_pb_module et_pb_gallery\b/', 'et_pb_module et_pb_gallery pac_drh_tab_4_col_last_r_1_element_fix', $output);
                }
                if ($column_numbers_tablet === 'pac_drh_tab_col_four' & (($gallery_ids % 4) === 2)) {
                    $output = preg_replace('/\bet_pb_module et_pb_gallery\b/', 'et_pb_module et_pb_gallery pac_drh_tab_4_col_last_r_2_element_fix', $output);
                }
                if ($column_numbers_tablet === 'pac_drh_tab_col_five' & (($gallery_ids % 4) === 2)) {
                    $output = preg_replace('/\bet_pb_module et_pb_gallery\b/', 'et_pb_module et_pb_gallery pac_drh_tab_5_col_last_r_2_element_fix', $output);
                }
                if ($column_numbers_tablet === 'pac_drh_tab_col_five' & (($gallery_ids % 4) === 1)) {
                    $output = preg_replace('/\bet_pb_module et_pb_gallery\b/', 'et_pb_module et_pb_gallery pac_drh_tab_5_col_last_r_1_element_fix', $output);
                }
                if ($column_numbers_tablet === 'pac_drh_tab_col_six' & (($gallery_ids % 5) === 2)) {
                    $output = preg_replace('/\bet_pb_module et_pb_gallery\b/', 'et_pb_module et_pb_gallery pac_drh_tab_6_col_last_r_2_element_fix', $output);
                }
                if ($column_numbers_tablet === 'pac_drh_tab_col_six' & (($gallery_ids % 5) === 1)) {
                    $output = preg_replace('/\bet_pb_module et_pb_gallery\b/', 'et_pb_module et_pb_gallery pac_drh_tab_6_col_last_r_1_element_fix', $output);
                }
                $output = preg_replace('/\bet_pb_module et_pb_gallery\b/', 'et_pb_module et_pb_gallery '.$column_numbers_tablet, $output);
            }
            if ('off' !== $column_numbers_phone) {
                if ($column_numbers_phone === 'pac_drh_ph_col_three' & (($gallery_ids % 3) === 1)) {
                    $output = preg_replace('/\bet_pb_module et_pb_gallery\b/', 'et_pb_module et_pb_gallery pac_drh_ph_3_col_last_r_1_element_fix', $output);
                }
                if ($column_numbers_phone === 'pac_drh_ph_col_four' & (($gallery_ids % 4) === 2)) {
                    $output = preg_replace('/\bet_pb_module et_pb_gallery\b/', 'et_pb_module et_pb_gallery pac_drh_ph_4_col_last_r_2_element_fix', $output);
                }
                if ($column_numbers_phone === 'pac_drh_ph_col_four' & (($gallery_ids % 4) === 1)) {
                    $output = preg_replace('/\bet_pb_module et_pb_gallery\b/', 'et_pb_module et_pb_gallery pac_drh_ph_4_col_last_r_1_element_fix', $output);
                }
                if ($column_numbers_phone === 'pac_drh_ph_col_five' & (($gallery_ids % 5) === 2)) {
                    $output = preg_replace('/\bet_pb_module et_pb_gallery\b/', 'et_pb_module et_pb_gallery pac_drh_ph_5_col_last_r_2_element_fix', $output);
                }
                if ($column_numbers_phone === 'pac_drh_ph_col_five' & (($gallery_ids % 5) === 1)) {
                    $output = preg_replace('/\bet_pb_module et_pb_gallery\b/', 'et_pb_module et_pb_gallery pac_drh_ph_5_col_last_r_1_element_fix', $output);
                }
                if ($column_numbers_phone === 'pac_drh_ph_col_six' & (($gallery_ids % 6) === 2)) {
                    $output = preg_replace('/\bet_pb_module et_pb_gallery\b/', 'et_pb_module et_pb_gallery pac_drh_ph_6_col_last_r_2_element_fix', $output);
                }
                if ($column_numbers_phone === 'pac_drh_ph_col_six' & (($gallery_ids % 6) === 1)) {
                    $output = preg_replace('/\bet_pb_module et_pb_gallery\b/', 'et_pb_module et_pb_gallery pac_drh_ph_6_col_last_r_1_element_fix', $output);
                }
                $output = preg_replace('/\bet_pb_module et_pb_gallery\b/', 'et_pb_module et_pb_gallery '.$column_numbers_phone, $output);
            }
        }
        // Number Of Posts
        if (class_exists('DOMDocument') &&
            pac_drh_get_settings_enabled('pac_drh_enable_responsive_gallery_count')
            && et_pb_responsive_options()->is_responsive_enabled($props, 'posts_number')) {
            $posts_number = isset($props['posts_number']) ? esc_html($props['posts_number']) : 4;
            $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;
            $dom = pac_drh_create_dom($output);
            $dom_xpath = new DOMXpath($dom);
            $gallery_node = $dom_xpath->query('//div[contains(@class,"et_pb_gallery_items")]');
            if ($gallery_node->length > 0) {
                $gallery_node_item = $gallery_node->item(0);
                if ($gallery_node_item) {
                    $gallery_node_item->setAttribute('data-is-responsive-count', true);
                    $gallery_node_item->setAttribute('data-posts-per-device', wp_json_encode([
                        'desktop' => esc_attr($posts_number),
                        'tablet' => esc_attr($posts_number_tablet),
                        'phone' => esc_attr($posts_number_phone),
                    ]));
                    $output = $dom->saveHTML();
                }
            }
        }

        return $output;
    }

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