' . esc_html__('Elementor Advanced Gallery requires Elementor plugin to be active. Please activate Elementor to continue.', 'advanced-image-gallery') . '
' . esc_html__('Activate Elementor', 'advanced-image-gallery') . '
'); } } // Prevent plugin activation if Elementor is not active function zlfms_activate_plugin() { if (!did_action('elementor/loaded')) { deactivate_plugins(plugin_basename(__FILE__)); // Deactivate this plugin wp_die( esc_html__('Elementor Advanced Gallery requires Elementor to be installed and activated. The plugin has been deactivated.', 'advanced-image-gallery'), esc_html__('Plugin dependency check failed', 'advanced-image-gallery'), array('back_link' => true) ); } } register_activation_hook(__FILE__, 'zlfms_activate_plugin'); /** * Store the plugin installation time for later use. */ function zlfms_aig_set_install_time() { if ( false === get_option( 'zlfms_aig_install_time' ) ) { add_option( 'zlfms_aig_install_time', time() ); } } register_activation_hook( __FILE__, 'zlfms_aig_set_install_time' ); // Register the widget only if Elementor is active function zlfms_register_media_gallery($widgets_manager) { if (!did_action('elementor/loaded')) { return; // Stop if Elementor is not active } require_once plugin_dir_path(__FILE__) . 'widgets/class-image-gallery-widget.php'; $widgets_manager->register(new zlfms_advanced_image_gallery()); } add_action('elementor/widgets/register', 'zlfms_register_media_gallery'); // Check whether the current document contains the Advanced Image Gallery widget function zlfms_has_advanced_gallery($post_id) { $has_widget = false; // Search Elementor document for the widget if (did_action('elementor/loaded')) { $document = \Elementor\Plugin::$instance->documents->get($post_id); if ($document && method_exists($document, 'get_elements_data')) { $elements = $document->get_elements_data(); $has_widget = zlfms_search_elementor_widget($elements, 'advanced_image_gallery'); } } // Fallback: check the post content for a shortcode with the widget name if (!$has_widget) { $post = get_post($post_id); if ($post && has_shortcode($post->post_content, 'advanced_image_gallery')) { $has_widget = true; } } return $has_widget; } // Recursively search Elementor elements for a specific widget type function zlfms_search_elementor_widget($elements, $widget) { foreach ($elements as $element) { if ( isset($element['elType']) && 'widget' === $element['elType'] && isset($element['widgetType']) && $element['widgetType'] === $widget ) { return true; } if (!empty($element['elements']) && zlfms_search_elementor_widget($element['elements'], $widget)) { return true; } } return false; } // Enqueue styles and scripts for the plugin function zlfms_enqueue_styles_scripts() { if (!(is_page() || is_single())) { return; } $post_id = get_queried_object_id(); // Skip if the Advanced Image Gallery widget is not present if (!$post_id || !zlfms_has_advanced_gallery($post_id)) { return; } // Enqueue styles and scripts on the frontend only when needed if ($post_id) { // Always enqueue base styles wp_enqueue_style('elementor_advanced_gallery_style', plugin_dir_url(__FILE__) . 'assets/css/advanced-gallery-style.css', [], ZLFMS_AIG_VERSION); // Determine which skins are used to conditionally load assets $skins = zlfms_get_advanced_gallery_skins($post_id); $needs_swiper = in_array('carousel', $skins, true) || in_array('slideshow', $skins, true); if ($needs_swiper) { wp_enqueue_style('elementor_advanced_gallery_bundle_css', plugin_dir_url(__FILE__) . 'assets/swiper/swiper-bundle.min.css', [], ZLFMS_AIG_VERSION); } // Enqueue Elementor Font Awesome only if Elementor is active if (defined('ELEMENTOR_VERSION')) { wp_enqueue_style('elementor-fontawesome', plugins_url('/elementor/assets/lib/font-awesome/css/fontawesome.min.css'), [], ELEMENTOR_VERSION); wp_enqueue_style('elementor-fontawesome-regular', plugins_url('/elementor/assets/lib/font-awesome/css/regular.min.css'), [], ELEMENTOR_VERSION); wp_enqueue_style('elementor-fontawesome-brands', plugins_url('/elementor/assets/lib/font-awesome/css/brands.min.css'), [], ELEMENTOR_VERSION); wp_enqueue_style('elementor-fontawesome-solid', plugins_url('/elementor/assets/lib/font-awesome/css/solid.min.css'), [], ELEMENTOR_VERSION); } // Conditionally enqueue JS based on skins if ($needs_swiper) { wp_enqueue_script('elementor_swiper_gallery_carousel_element_js', plugin_dir_url(__FILE__) . 'assets/swiper/swiper-bundle.min.js', ['jquery'], ZLFMS_AIG_VERSION, true); } if (in_array('carousel', $skins, true)) { wp_enqueue_script('elementor_advanced_gallery_carousel_js', plugin_dir_url(__FILE__) . 'assets/js/advanced-gallery-carousel.js', ['jquery'], ZLFMS_AIG_VERSION, true); } if (in_array('slideshow', $skins, true)) { wp_enqueue_script('elementor_advanced_gallery_slideshow_js', plugin_dir_url(__FILE__) . 'assets/js/advanced-gallery-slideshow.js', ['jquery'], ZLFMS_AIG_VERSION, true); } } } add_action('wp_enqueue_scripts', 'zlfms_enqueue_styles_scripts', 999); /** * Collect Advanced Image Gallery skins used in a given Elementor document. * * @param array $elements Elementor elements tree * @param array $skins Reference to array collecting unique skins */ function zlfms_collect_gallery_skins_recursive($elements, &$skins) { foreach ($elements as $element) { if ( isset($element['elType']) && 'widget' === $element['elType'] && isset($element['widgetType']) && $element['widgetType'] === 'advanced_image_gallery' ) { $skin = 'grid'; if (isset($element['settings']['skin']) && is_string($element['settings']['skin'])) { $skin = sanitize_text_field($element['settings']['skin']); } if (!in_array($skin, $skins, true)) { $skins[] = $skin; } } if (!empty($element['elements'])) { zlfms_collect_gallery_skins_recursive($element['elements'], $skins); } } } /** * Return unique list of Advanced Image Gallery skins present in the post. * * @param int $post_id * @return array skins, e.g. ['grid', 'carousel'] */ function zlfms_get_advanced_gallery_skins($post_id) { $skins = []; if (did_action('elementor/loaded')) { $document = \Elementor\Plugin::$instance->documents->get($post_id); if ($document && method_exists($document, 'get_elements_data')) { $elements = $document->get_elements_data(); zlfms_collect_gallery_skins_recursive($elements, $skins); } } return $skins; } /** * Parse video URL and return video details for embedding. * * @param string $video_url The video URL to parse * @return array|false Array with 'video_id', 'embed_url', 'video_type' or false on failure */ function zlfms_parse_video_url($video_url) { if (empty($video_url)) { return false; } $video_url = esc_url($video_url); $matches = []; // YouTube URL patterns if (preg_match('/(?:https?:\/\/)?(?:www\.)?(?:youtube\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})/', $video_url, $matches)) { return [ 'video_id' => $matches[1], 'embed_url' => 'https://www.youtube.com/embed/' . esc_attr($matches[1]), 'video_type' => 'youtube' ]; } // Vimeo URL patterns if (preg_match('/(?:https?:\/\/)?(?:www\.)?(?:vimeo\.com\/(?:channels\/(?:\w+\/)?|groups\/(?:\w+\/)?videos\/|album\/(\d+)\/video\/|video\/|)(\d+)(?:$|\/|\?))/', $video_url, $matches)) { return [ 'video_id' => $matches[2], 'embed_url' => 'https://player.vimeo.com/video/' . esc_attr($matches[2]), 'video_type' => 'vimeo' ]; } return false; } add_action('admin_enqueue_scripts', 'zlfms_register_advanced_gallery_admin_assets'); function zlfms_register_advanced_gallery_admin_assets($hook_suffix) { // Register the style for the block editor. wp_register_style( 'zlfms-counter-block-gallery', plugins_url('assets/css/admin_gallery.css', __FILE__), // URL to the CSS file [], // Dependencies: WordPress block editor styles filemtime(plugin_dir_path(__FILE__) . 'assets/css/admin_gallery.css') // Version parameter: File modification time for cache-busting ); // Load assets only on the Plugins page. if ($hook_suffix === 'plugins.php') { // Enqueue the registered style wp_enqueue_style('zlfms-counter-block-gallery'); // Enqueue the script for the deactivation popup wp_enqueue_script( 'zlfms-admin', plugin_dir_url(__FILE__) . 'assets/js/deactivation-feedback.js', // URL to the JavaScript file array('jquery'), // Dependencies: jQuery filemtime(plugin_dir_path(__FILE__) . 'assets/js/deactivation-feedback.js'), // Version parameter: File modification time for cache-busting true // Load the script in the footer ); // Localize the script with necessary data wp_localize_script('zlfms-admin', 'zl_ajax_obj', array( 'ajax_url' => admin_url('admin-ajax.php'), // URL for AJAX requests 'nonce' => wp_create_nonce('zlfms_deactivation_nonce') // Nonce for security )); } } /** * Handle review notice actions. */ function zlfms_aig_handle_review_actions() { if ( ! isset( $_GET['zlfms_aig_review_action'] ) || ! current_user_can( 'manage_options' ) ) { return; } $action = sanitize_text_field( wp_unslash( $_GET['zlfms_aig_review_action'] ) ); switch ( $action ) { case 'done': update_option( 'zlfms_aig_review_status', 'done' ); wp_safe_redirect( remove_query_arg( 'zlfms_aig_review_action' ) ); exit; case 'review_now': $week_in_seconds = defined('WEEK_IN_SECONDS') ? WEEK_IN_SECONDS : 604800; update_option( 'zlfms_aig_review_dismissed_until', time() + $week_in_seconds ); wp_redirect( 'https://wordpress.org/support/plugin/advanced-image-gallery/reviews/#new-post' ); exit; case 'later': $week_in_seconds = defined('WEEK_IN_SECONDS') ? WEEK_IN_SECONDS : 604800; update_option( 'zlfms_aig_review_dismissed_until', time() + $week_in_seconds ); wp_safe_redirect( remove_query_arg( 'zlfms_aig_review_action' ) ); exit; } } add_action( 'admin_init', 'zlfms_aig_handle_review_actions' ); /** * Display a review notice in the admin area after seven days. */ function zlfms_aig_review_notice() { if ( ! current_user_can( 'manage_options' ) ) { return; } $status = get_option( 'zlfms_aig_review_status' ); $install_time = (int) get_option( 'zlfms_aig_install_time', 0 ); $dismissed_until = (int) get_option( 'zlfms_aig_review_dismissed_until', 0 ); if ( 'done' === $status || ! $install_time ) { return; } // Check if 7 days have passed since installation (7 * 24 * 60 * 60 = 604800 seconds) $week_in_seconds = defined('WEEK_IN_SECONDS') ? WEEK_IN_SECONDS : 604800; if ( time() - $install_time < $week_in_seconds || time() < $dismissed_until ) { return; } $base_url = remove_query_arg( 'zlfms_aig_review_action' ); $did_url = esc_url( add_query_arg( 'zlfms_aig_review_action', 'done', $base_url ) ); $now_url = esc_url( add_query_arg( 'zlfms_aig_review_action', 'review_now', $base_url ) ); $later_url = esc_url( add_query_arg( 'zlfms_aig_review_action', 'later', $base_url ) ); echo '' . esc_html__( 'Enjoying Advanced Image Gallery? Please consider leaving a review to support the project.', 'advanced-image-gallery' ) . '
' . esc_html__( 'I did', 'advanced-image-gallery' ) . ' | ' . esc_html__( 'Review now', 'advanced-image-gallery' ) . ' | ' . esc_html__( 'Later', 'advanced-image-gallery' ) . '