<?php
/**
 * Filter the fields for the Divi module.
 *
 * @param array $fields_unprocessed
 *
 * @return array
 */
if (!function_exists('pac_drh_filter_image_fields')):
    function pac_drh_filter_image_fields($fields_unprocessed)
    {
        $custom_fields = [];
        $fields_unprocessed['show_in_lightbox']['mobile_options'] = true;

        return wp_parse_args($custom_fields, $fields_unprocessed);
    }

    add_filter('et_pb_all_fields_unprocessed_et_pb_image', 'pac_drh_filter_image_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_image_props')):
    function pac_drh_filter_image_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_image' !== $render_slug) {
            return $props;
        }
        $url = isset($props['url']) ? $props['url'] : '';
        $show_in_lightbox = isset($props['show_in_lightbox']) ? $props['show_in_lightbox'] : 'off';
        $show_in_lightbox_values = et_pb_responsive_options()->get_property_values($props, 'show_in_lightbox');
        $show_in_lightbox_tablet = !empty($show_in_lightbox_values['tablet']) ? $show_in_lightbox_values['tablet'] : $show_in_lightbox;
        $show_in_lightbox_phone = !empty($show_in_lightbox_values['phone']) ? $show_in_lightbox_values['phone'] : $show_in_lightbox_tablet;
        ?>
        <script type="text/javascript" id="pac_drh_gallery">
            jQuery(document).ready(function ($) {
                let $device = getDeviceType();
                let lightboxConfig = {
                    'mobile': '<?php echo esc_js($show_in_lightbox_phone);?>',
                    'tablet': '<?php echo esc_js($show_in_lightbox_tablet);?>'
                };
                $('.et_pb_image a.et_pb_lightbox_image').each(function () {
                    let $this = $(this);
                    if (($device === 'mobile' && lightboxConfig.mobile === 'off') || ($device === 'tablet' && lightboxConfig.tablet === 'off')) {
                        $this.removeClass('et_pb_lightbox_image').attr('href', '<?php echo esc_js($url);?>');
                    }
                });
            });
        </script>
        <?php
        return $props;
    }

    add_filter('et_pb_module_shortcode_attributes', 'pac_drh_filter_image_props', 10, 5);
endif;
