PluginProbe
Filter Gallery / 1.1.5
Filter Gallery v1.1.5
1.1.5 1.1.4 1.1.3 1.1.2 trunk 0.0.1 0.0.2 0.0.3 0.0.4 0.0.5 0.0.6 0.0.7 0.0.8 0.0.9 0.1.0 0.1.1 0.1.2 0.1.3 0.1.4 0.1.5 0.1.6 0.1.7 0.1.8 0.1.9 0.2.0 All 28 releases
filter-gallery / filter-gallery.php

filter-gallery.php in Filter Gallery 1.1.5, at filter-gallery.php

1,466 lines 63.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if (!defined('ABSPATH'))
3 exit; // Exit if accessed directly
4
5 /**
6 * Plugin Name: Filter Gallery
7 * Plugin URI: https://wpfrank.com/
8 * Description: Filter Gallery is a lightweight and powerful WordPress plugin to create beautiful filterable galleries.
9 * Version: 1.1.5
10 * Requires at least: 6.0
11 * Requires PHP: 7.4
12 * Author: FARAZFRANK
13 * Author URI: https://profiles.wordpress.org/farazfrank/
14 * License: GPL v2 or later
15 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
16 * Text Domain: filter-gallery
17 * Domain Path: /languages
18 */
19
20 if (!defined('UFG_VERSION')) {
21 define('UFG_VERSION', '1.1.5');
22 }
23
24 require_once plugin_dir_path(__FILE__) . 'includes/class-ufg-migration.php';
25 UFG_Migration::init();
26
27 if (!function_exists('ufg_normalize_filters_recursive')) {
28 function ufg_normalize_filters_recursive(&$filters) {
29 if (!is_array($filters)) return;
30 foreach ($filters as &$item) {
31 $is_obj = is_object($item);
32 $item_arr = $is_obj ? (array)$item : $item;
33 if (is_array($item_arr)) {
34 $legacy_title = isset($item_arr['title']) ? $item_arr['title'] : '';
35 $legacy_text = isset($item_arr['text']) ? $item_arr['text'] : '';
36 $has_filterkey = isset($item_arr['filterkey']);
37
38 if (!$has_filterkey) {
39 if (!empty($legacy_title)) {
40 $item_arr['filterkey'] = strtolower(str_replace(' ', '-', $legacy_title));
41 } else if (!empty($legacy_text)) {
42 $characters = 'abcdefghijklmnopqrstuvwxyz0123456789';
43 $rand_str = '';
44 for ($i = 0; $i < 5; $i++) {
45 $rand_str .= $characters[wp_rand(0, 35)];
46 }
47 $item_arr['filterkey'] = strtolower(str_replace(' ', '-', $legacy_text)) . '-' . $rand_str;
48 } else {
49 $characters = 'abcdefghijklmnopqrstuvwxyz0123456789';
50 $rand_str = '';
51 for ($i = 0; $i < 5; $i++) {
52 $rand_str .= $characters[wp_rand(0, 35)];
53 }
54 $item_arr['filterkey'] = 'filter-' . $rand_str;
55 }
56 $display_text = !empty($legacy_text) ? $legacy_text : (!empty($legacy_title) ? $legacy_title : 'Filter');
57 $item_arr['title'] = $display_text;
58 $item_arr['text'] = $display_text;
59 }
60 if (!isset($item_arr['children'])) {
61 $item_arr['children'] = array();
62 } else if (is_array($item_arr['children'])) {
63 ufg_normalize_filters_recursive($item_arr['children']);
64 }
65 if (!isset($item_arr['color'])) {
66 $item_arr['color'] = '#38B2F6';
67 }
68
69 $item = $is_obj ? (object)$item_arr : $item_arr;
70 }
71 }
72 }
73 }
74
75 if (!function_exists('ufg_remove_blank_filters_recursive')) {
76 function ufg_remove_blank_filters_recursive(&$filters) {
77 if (!is_array($filters)) return;
78 $new_filters = array();
79 foreach ($filters as $item) {
80 $is_obj = is_object($item);
81 $item_arr = $is_obj ? (array)$item : $item;
82 if (is_array($item_arr)) {
83 $title = isset($item_arr['title']) ? trim($item_arr['title']) : '';
84 $text = isset($item_arr['text']) ? trim($item_arr['text']) : '';
85 $icon = isset($item_arr['icon']) ? trim($item_arr['icon']) : '';
86
87 $has_title = ($title !== '') || ($text !== '');
88 $has_icon = ($icon !== '');
89
90 if (!$has_title && !$has_icon) {
91 continue;
92 }
93
94 if (isset($item_arr['children']) && is_array($item_arr['children'])) {
95 ufg_remove_blank_filters_recursive($item_arr['children']);
96 }
97 $new_filters[] = $is_obj ? (object)$item_arr : $item_arr;
98 }
99 }
100 $filters = $new_filters;
101 }
102 }
103
104 // custom image size
105 add_image_size('ufg_200_200', 200, 200, true);
106 add_image_size('ufg_300_300', 300, 300, true);
107 add_image_size('ufg_400_400', 400, 400, true);
108
109 // FG activation
110 function ufg_activation()
111 {
112 // update current plugin version via migration class rather than activation hook to ensure migrations run
113 /*
114 if (is_admin()) {
115 if (!function_exists('get_plugin_data')) {
116 require_once(ABSPATH . 'wp-admin/includes/plugin.php');
117 }
118 $ufg_plugin_data = get_plugin_data(__FILE__);
119 if (isset($ufg_plugin_data['Version'])) {
120 $ufg_plugin_version = $ufg_plugin_data['Version'];
121 update_option('ufg_current_version', $ufg_plugin_version);
122 }
123 }
124 */
125 }
126 register_activation_hook(__FILE__, 'ufg_activation');
127
128 // FG deactivation
129 function ufg_deactivation()
130 {
131 // update last active plugin version
132 $ufg_last_version = get_option('ufg_current_version');
133 if ($ufg_last_version !== "") {
134 update_option('ufg_last_version', $ufg_last_version);
135 }
136 }
137 register_deactivation_hook(__FILE__, 'ufg_deactivation');
138
139 // FG uninstall
140 function ufg_uninstall()
141 {
142 }
143 register_uninstall_hook(__FILE__, 'ufg_uninstall');
144
145 // load translation
146 function ufg_load_translation()
147 {
148 load_plugin_textdomain('filter-gallery', false, dirname(plugin_basename(__FILE__)) . '/languages');
149 }
150 add_action('init', 'ufg_load_translation');
151
152 // FG menu
153 function ufg_menu_page()
154 {
155 // add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $position );
156 add_menu_page(
157 __('Filter Gallery', 'filter-gallery'),
158 __('Filter Gallery', 'filter-gallery'),
159 'manage_options',
160 'filter-gallery',
161 'ufg_main',
162 'dashicons-format-gallery',
163 65
164 );
165
166 //add_submenu_page( string $parent_slug, string $page_title, string $menu_title, string $capability, string $menu_slug, callable $function = '', int $position )
167 add_submenu_page('filter-gallery', __('Manage Gallery', 'filter-gallery'), __('Manage Gallery', 'filter-gallery'), 'manage_options', 'ufg-manage-gallery', 'ufg_manage_gallery');
168 add_submenu_page('filter-gallery', __('Import / Export', 'filter-gallery'), __('Import / Export', 'filter-gallery'), 'manage_options', 'ufg-import-export', 'ufg_import_export_page');
169 add_submenu_page('filter-gallery', __('Docs', 'filter-gallery'), __('Docs', 'filter-gallery'), 'manage_options', 'ufg-docs', 'ufg_docs_page');
170 add_submenu_page('filter-gallery', __('Free vs Pro', 'filter-gallery'), __('Free vs Pro', 'filter-gallery'), 'manage_options', 'ufg-free-vs-pro', 'ufg_free_vs_pro_page');
171 }
172 add_action('admin_menu', 'ufg_menu_page');
173
174 // FG main page body
175 function ufg_main()
176 {
177 ufg_enqueue_react_app();
178 require 'admin/galleries.php';
179 }
180
181 // FG Docs page body
182 function ufg_docs_page()
183 {
184 ufg_enqueue_react_app();
185 require 'admin/docs.php';
186 }
187
188 // Free vs Pro page body
189 function ufg_free_vs_pro_page()
190 {
191 wp_enqueue_style(
192 'ufg-google-font-outfit',
193 'https://fonts.googleapis.com/css2?family=Outfit:wght@300;400;500;600;700;800;900&display=swap',
194 array(),
195 null
196 );
197 wp_enqueue_style(
198 'ufg-fontawesome-admin',
199 plugins_url('admin/assets/fontawesome-free-6.5.2-web/css/all.min.css', __FILE__),
200 array(),
201 '6.5.2'
202 );
203 wp_enqueue_style(
204 'ufg-pricing-css',
205 plugins_url('admin/assets/css/ufg-pricing.css', __FILE__),
206 array(),
207 UFG_VERSION
208 );
209 require 'admin/free-vs-pro.php';
210 }
211
212 // FG sub menu filters page body
213 function ufg_manage_gallery()
214 {
215 ufg_enqueue_react_app();
216 require 'admin/manage-gallery.php';
217 }
218
219 function ufg_enqueue_react_app()
220 {
221 $asset_file = include(plugin_dir_path(__FILE__) . 'build/index.asset.php');
222 wp_register_script(
223 'ufg-react-app',
224 plugins_url('build/index.js', __FILE__),
225 array('jquery', 'wp-element', 'wp-components', 'wp-api-fetch', 'wp-media-utils'),
226 '5.3.1',
227 true
228 );
229 wp_set_script_translations(
230 'ufg-react-app',
231 'filter-gallery',
232 plugin_dir_path(__FILE__) . 'languages'
233 );
234 wp_enqueue_style(
235 'ufg-react-app-style',
236 plugins_url('build/index.css', __FILE__),
237 array('wp-components'),
238 $asset_file['version']
239 );
240
241 // Enqueue FontAwesome for the Icon Picker to render correctly in the admin React UI
242 wp_enqueue_style(
243 'ufg-fontawesome-admin',
244 plugins_url('admin/assets/fontawesome-free-6.5.2-web/css/all.min.css', __FILE__),
245 array(),
246 '6.5.2'
247 );
248
249 // Enqueue custom admin fixes for the React UI
250 wp_enqueue_style(
251 'ufg-admin-fixes',
252 plugins_url('admin/assets/css/ufg-admin-fixes.css', __FILE__),
253 array('ufg-react-app-style'),
254 time()
255 );
256
257
258 // Fetch galleries for the dashboard
259 global $wpdb;
260 $ufg_gallery_key = "ufg_filters_";
261
262 $cache_key = 'ufg_all_galleries';
263 $ufg_all_galleries = wp_cache_get($cache_key, 'ufg_galleries');
264
265 if (false === $ufg_all_galleries) {
266 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
267 $ufg_all_galleries = $wpdb->get_results(
268 $wpdb->prepare("SELECT option_name FROM `{$wpdb->prefix}options` WHERE `option_name` LIKE %s ORDER BY option_id ASC", '%' . $wpdb->esc_like($ufg_gallery_key) . '%')
269 );
270 wp_cache_set($cache_key, $ufg_all_galleries, 'ufg_galleries', 3600);
271 }
272
273 $galleries = array();
274 if (count($ufg_all_galleries)) {
275 foreach ($ufg_all_galleries as $gallery) {
276 $ufg_gallery_key_name = $gallery->option_name;
277 $ufg_underscore_pos = strrpos($ufg_gallery_key_name, '_');
278 $ufg_gallery_id = substr($ufg_gallery_key_name, ($ufg_underscore_pos + 1));
279 $details = get_option("ufg_details_" . $ufg_gallery_id);
280 $g_filters = get_option("ufg_filters_" . $ufg_gallery_id);
281 ufg_normalize_filters_recursive($g_filters);
282 $galleries[] = array(
283 'id' => $ufg_gallery_id,
284 'name' => isset($details['gallery_name']) ? $details['gallery_name'] : '',
285 'gallery' => get_option("ufg_gallery_" . $ufg_gallery_id),
286 'filters' => $g_filters,
287 );
288 }
289 }
290
291 // Fetch single gallery data if editing
292 $currentGalleryData = null;
293 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
294 if (isset($_GET['page']) && $_GET['page'] === 'ufg-manage-gallery' && isset($_GET['id'])) {
295 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
296 $id = sanitize_text_field(wp_unslash($_GET['id']));
297 $filters = get_option("ufg_filters_" . $id);
298 ufg_normalize_filters_recursive($filters);
299 $gallery = get_option("ufg_gallery_" . $id);
300 $settings = get_option("ufg_settings_" . $id);
301 $details = get_option("ufg_details_" . $id);
302
303 $parsedImages = array();
304 if (is_array($gallery) && isset($gallery['ufg-attachment-id']) && is_array($gallery['ufg-attachment-id'])) {
305 foreach ($gallery['ufg-attachment-id'] as $k => $v) {
306 $att_id = $v;
307 $parsedImages[] = array(
308 'id' => (int) $att_id,
309 'url' => wp_get_attachment_image_url($att_id, 'medium'),
310 'link_url' => isset($gallery['ufg-url'][$att_id]) ? $gallery['ufg-url'][$att_id] : (isset($gallery['ufg-url'][$k]) ? $gallery['ufg-url'][$k] : ''),
311 'title' => isset($gallery['ufg-title'][$att_id]) ? $gallery['ufg-title'][$att_id] : '',
312 'alt' => isset($gallery['ufg-alt'][$att_id]) ? $gallery['ufg-alt'][$att_id] : '',
313 'description' => isset($gallery['ufg-description'][$att_id]) ? $gallery['ufg-description'][$att_id] : '',
314 'filters' => isset($gallery['ufg-image-filters'][$att_id]) ? $gallery['ufg-image-filters'][$att_id] : (isset($gallery['ufg-image-filters'][$k]) ? $gallery['ufg-image-filters'][$k] : array())
315 );
316 }
317 }
318
319 $default_5_filters = array(
320 (object) array('title' => 'One', 'text' => 'One', 'filterkey' => 'one', 'icon' => 'fas fa-camera', 'color' => '#38B2F6', 'children' => array()),
321 (object) array('title' => 'Two', 'text' => 'Two', 'filterkey' => 'two', 'icon' => 'fas fa-image', 'color' => '#38B2F6', 'children' => array()),
322 (object) array('title' => 'Three', 'text' => 'Three', 'filterkey' => 'three', 'icon' => 'fas fa-star', 'color' => '#38B2F6', 'children' => array()),
323 (object) array('title' => 'Four', 'text' => 'Four', 'filterkey' => 'four', 'icon' => 'fas fa-heart', 'color' => '#38B2F6', 'children' => array()),
324 (object) array('title' => 'Five', 'text' => 'Five', 'filterkey' => 'five', 'icon' => 'fas fa-shield-alt', 'color' => '#38B2F6', 'children' => array()),
325 );
326 if (empty($filters) || !is_array($filters)) {
327 $filters = $default_5_filters;
328 }
329
330 $currentGalleryData = array(
331 'id' => $id,
332 'name' => isset($details['gallery_name']) ? $details['gallery_name'] : '',
333 'filters' => $filters,
334 'images' => $parsedImages,
335 'settings' => is_array($settings) ? array_merge(array(
336 'show_filters' => 1,
337 'show_filters_icon' => 1,
338 'enable_deep_linking' => 0,
339 'show_filters_count' => 1,
340 'show_search_box' => 0,
341 'search_box_placeholder' => 'Type here to search images',
342 'show_all_button' => 1,
343 'all_button_text' => 'All',
344 'all_button_icon' => 'fas fa-filter',
345 'all_button_color' => '#ffffff',
346 'all_button_bg_color' => '#0A85ED',
347 'parent_button_color' => '#4F46E5',
348 'parent_button_bg_color' => '#EEF2FF',
349 'parent_button_hover_color' => '#000000',
350 'parent_active_button_color' => '#FFFFFF',
351 'parent_active_button_bg_color' => '#4F46E5',
352 'parent_filters_heading' => '',
353 'l1_filters_heading' => '',
354 'l1_button_color' => '#4F46E5',
355 'l1_button_bg_color' => '#EEF2FF',
356 'child_filter_effect' => 'show_hide',
357 'active_button_color' => '#FFFFFF',
358 'active_button_bg_color' => '#4F46E5',
359 'l2_button_color' => '#4F46E5',
360 'l2_button_bg_color' => '#EEF2FF',
361 'l3_button_color' => '#4F46E5',
362 'l3_button_bg_color' => '#EEF2FF',
363 'l4_button_color' => '#4F46E5',
364 'l4_button_bg_color' => '#EEF2FF',
365 'columns_desktop' => 4,
366 'columns_tab' => 3,
367 'columns_mobile_landscape' => 3,
368 'columns_mobile_portrait' => 2,
369 'thumbnail_image' => 1,
370 'thumbnail_image_size' => 'full',
371 'thumbnail_border' => 1,
372 'thumbnail_border_thickness' => 1,
373 'thumbnail_border_color' => '#ffffff',
374 'thumbnail_bg_color' => '#222a33',
375 'image_title' => 1,
376 'image_title_font_size' => 18,
377 'image_title_color' => '#FFFFFF',
378 'image_description' => 1,
379 'image_description_font_size' => 14,
380 'image_description_color' => '#FFFFFF',
381 'image_description_text_limit' => 60,
382 'image_hover_effect' => 'border_overlay',
383 'read_more_link_sh' => 0,
384 'read_more_link' => 1,
385 'read_more_button_text' => 'Read More Link',
386 'read_more_button_icon' => 'fas fa-link',
387 'read_more_button_color' => '#ffffff',
388 'read_more_button_bg_color' => '#0080ff',
389 'read_more_button_target' => '_self',
390 'image_sorting' => 5,
391 'image_search' => 1,
392 'lightbox' => 1,
393 'lightbox_title' => 1,
394 'lightbox_description' => 0,
395 'lightbox_numbering' => 0,
396 'custom_css' => '',
397 'load_more' => 'off',
398 'load_limit' => 10,
399 'load_color' => '#0080ff',
400 'load_txt_color' => '#FFFFFF',
401 'load_btn_txt' => 'Load More',
402 'filter_style' => 'buttons',
403 'combine_filter_search' => '0',
404 'filter_padding' => '8px 16px',
405 'filter_margin' => '5px',
406 'filter_padding_type' => 'small',
407 'filter_padding_v' => '8',
408 'filter_padding_h' => '16',
409 'filter_margin_val' => '5',
410 'l1_button_hover_color' => '#059669',
411 'l1_active_button_color' => '#FFFFFF',
412 'l1_active_button_bg_color' => '#059669',
413 'l2_button_hover_color' => '#4F46E5',
414 'l2_active_button_color' => '#FFFFFF',
415 'l2_active_button_bg_color' => '#4F46E5',
416 'l3_button_hover_color' => '#D97706',
417 'l3_active_button_color' => '#FFFFFF',
418 'l3_active_button_bg_color' => '#D97706',
419 'l4_button_hover_color' => '#E11D48',
420 'l4_active_button_color' => '#FFFFFF',
421 'l4_active_button_bg_color' => '#E11D48'
422 ), $settings) : array(),
423 );
424 }
425
426 wp_enqueue_media();
427
428 wp_localize_script('ufg-react-app', 'ufgAdminData', array(
429 'ajaxUrl' => admin_url('admin-ajax.php'),
430 'galleries' => $galleries,
431 'currentGalleryData' => $currentGalleryData,
432 'nextId' => ufg_get_next_id(),
433 'defaultSettings' => array(
434 'show_filters' => 1,
435 'show_filters_icon' => 1,
436 'enable_deep_linking' => 0,
437 'show_filters_count' => 1,
438 'show_search_box' => 0,
439 'search_box_placeholder' => 'Type here to search images',
440 'show_all_button' => 1,
441 'all_button_text' => 'All',
442 'all_button_icon' => 'fas fa-filter',
443
444 'all_button_color' => '#ffffff',
445 'all_button_bg_color' => '#0A85ED',
446 'parent_button_color' => '#4F46E5',
447 'parent_button_bg_color' => '#EEF2FF',
448 'parent_button_hover_color' => '#000000',
449 'parent_active_button_color' => '#FFFFFF',
450 'parent_active_button_bg_color' => '#4F46E5',
451 'parent_filters_heading' => '',
452 'l1_filters_heading' => '',
453 'l1_button_color' => '#4F46E5',
454 'l1_button_bg_color' => '#EEF2FF',
455 'child_filter_effect' => 'show_hide',
456 'active_button_color' => '#FFFFFF',
457 'active_button_bg_color' => '#4F46E5',
458 'l2_button_color' => '#4F46E5',
459 'l2_button_bg_color' => '#EEF2FF',
460 'l3_button_color' => '#4F46E5',
461 'l3_button_bg_color' => '#EEF2FF',
462 'l4_button_color' => '#4F46E5',
463 'l4_button_bg_color' => '#EEF2FF',
464 'columns_desktop' => 4,
465 'columns_tab' => 3,
466 'columns_mobile_landscape' => 3,
467 'columns_mobile_portrait' => 2,
468 'thumbnail_image' => 1,
469 'thumbnail_image_size' => 'full',
470 'thumbnail_border' => 1,
471 'thumbnail_border_thickness' => 1,
472 'thumbnail_border_color' => '#ffffff',
473 'thumbnail_bg_color' => '#222a33',
474 'image_title' => 1,
475 'image_title_font_size' => 18,
476 'image_title_color' => '#FFFFFF',
477 'image_description' => 1,
478 'image_description_font_size' => 14,
479 'image_description_color' => '#FFFFFF',
480 'image_description_text_limit' => 60,
481 'image_hover_effect' => 'border_overlay',
482 'read_more_link_sh' => 0,
483 'read_more_link' => 1,
484 'read_more_button_text' => 'Read More Link',
485 'read_more_button_icon' => 'fas fa-link',
486 'read_more_button_color' => '#ffffff',
487 'read_more_button_bg_color' => '#0080ff',
488 'read_more_button_target' => '_self',
489 'image_sorting' => 5,
490 'image_search' => 1,
491 'lightbox' => 1,
492 'lightbox_title' => 1,
493 'lightbox_description' => 0,
494 'lightbox_numbering' => 0,
495 'custom_css' => '',
496 'load_more' => 'off',
497 'load_limit' => 10,
498 'load_color' => '#0080ff',
499 'load_txt_color' => '#FFFFFF',
500 'load_btn_txt' => 'Load More',
501 'filter_style' => 'buttons',
502 'combine_filter_search' => '0',
503 'filter_padding' => '8px 16px',
504 'filter_margin' => '5px',
505 'filter_padding_type' => 'small',
506 'filter_padding_v' => '8',
507 'filter_padding_h' => '16',
508 'filter_margin_val' => '5',
509 'parent_button_hover_color' => '#000000',
510 'parent_active_button_color' => '#FFFFFF',
511 'parent_active_button_bg_color' => '#4F46E5',
512 'l1_button_hover_color' => '#059669',
513 'l1_active_button_color' => '#FFFFFF',
514 'l1_active_button_bg_color' => '#059669',
515 'l2_button_hover_color' => '#4F46E5',
516 'l2_active_button_color' => '#FFFFFF',
517 'l2_active_button_bg_color' => '#4F46E5',
518 'l3_button_hover_color' => '#D97706',
519 'l3_active_button_color' => '#FFFFFF',
520 'l3_active_button_bg_color' => '#D97706',
521 'l4_button_hover_color' => '#E11D48',
522 'l4_active_button_color' => '#FFFFFF',
523 'l4_active_button_bg_color' => '#E11D48'
524 ),
525 'nonces' => array(
526 'clone' => wp_create_nonce('ufg-clone-gallery'),
527 'remove' => wp_create_nonce('ufg-remove-gallery'),
528 'addFilters' => wp_create_nonce('add-filters'),
529 'saveGallery' => wp_create_nonce('save-gallery'),
530 'saveSetting' => wp_create_nonce('save-setting'),
531 'addImage' => wp_create_nonce('add-image'),
532 'loadGallery' => wp_create_nonce('load-gallery')
533 ),
534 'version' => UFG_VERSION
535 ));
536 wp_enqueue_script('ufg-react-app');
537 wp_enqueue_script('ufg-admin-custom-js', plugins_url('admin/assets/js/ufg-admin-custom.js', __FILE__), array('jquery', 'ufg-react-app'), UFG_VERSION, true);
538 }
539
540 //get / create next gallery id
541 function ufg_get_next_id()
542 {
543 global $wpdb;
544 $ufg_options_table_name = "{$wpdb->prefix}options";
545 $ufg_gallery_key = "ufg_gallery_";
546 // reference : https://wordpress.stackexchange.com/questions/8825/how-do-you-properly-prepare-a-like-sql-statement
547 $cache_key = 'ufg_next_gallery_id';
548 $ufg_gallery_count_res = wp_cache_get($cache_key, 'ufg_galleries');
549
550 if (false === $ufg_gallery_count_res) {
551 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
552 $ufg_gallery_count_res = $wpdb->get_row(
553 $wpdb->prepare("SELECT option_name FROM `{$wpdb->prefix}options` WHERE `option_name` LIKE %s ORDER BY option_id DESC LIMIT 1", '%' . $wpdb->esc_like($ufg_gallery_key) . '%'),
554 ARRAY_N
555 );
556 wp_cache_set($cache_key, $ufg_gallery_count_res, 'ufg_galleries', 3600);
557 }
558
559 if ($wpdb->num_rows) {
560 $ufg_gallery_last_key = $ufg_gallery_count_res[0];
561 $ufg_underscore_pos = strrpos($ufg_gallery_last_key, '_');
562 $ufg_last_slider_id = (int) substr($ufg_gallery_last_key, ($ufg_underscore_pos + 1));
563 return ($ufg_last_slider_id + 1);
564 } else {
565 return 1;
566 }
567 }
568
569 // 1. save filters ajax
570 function ufg_gallery_filters_callback()
571 {
572 if (!isset($_POST['nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])), 'add-filters')) {
573 wp_send_json_error(__('Nonce verification failed', 'filter-gallery'));
574 }
575
576 if (!current_user_can('manage_options')) {
577 wp_send_json_error(__('Insufficient permissions', 'filter-gallery'));
578 }
579
580 // save filters
581 $ufg_gallery_id = isset($_POST['id']) ? absint(sanitize_text_field(wp_unslash($_POST['id']))) : 0;
582 if ($ufg_gallery_id <= 0) {
583 wp_send_json_error(__('Invalid gallery ID', 'filter-gallery'));
584 }
585 $ufg_gallery_name = isset($_POST['gallery_name']) ? sanitize_text_field(wp_unslash($_POST['gallery_name'])) : '';
586
587 // gerate random unique key string start
588 if (!function_exists('UFGgenerateRandomString')) {
589 function UFGgenerateRandomString($length = 7)
590 {
591 $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
592 $charactersLength = strlen($characters);
593 $randomString = '';
594 for ($i = 0; $i < $length; $i++) {
595 $randomString .= $characters[wp_rand(0, $charactersLength - 1)];
596 }
597 return $randomString;
598 }
599 }
600
601 // add filterkey index into newly added filers start
602 if (!function_exists('UFGaddMissingFilterKeys')) {
603 function UFGaddMissingFilterKeys(&$array)
604 {
605 foreach ($array as &$item) {
606 // Check for missing filterkey in parent
607 if (!isset($item->filterkey)) {
608 $item->filterkey = strtolower(str_replace(' ', '-', $item->title)) . '-' . UFGgenerateRandomString();
609 }
610
611 // Check for children
612 if (isset($item->children) && is_array($item->children)) {
613 foreach ($item->children as &$child) {
614 if (!isset($child->filterkey)) {
615 $child->filterkey = strtolower(str_replace(' ', '-', $child->title)) . '-' . UFGgenerateRandomString();
616 }
617 }
618 }
619 }
620 }
621 }
622
623 $filters = array();
624 if (isset($_POST['filters'])) {
625 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- sanitized in loop below
626 $filters = json_decode(stripslashes(wp_unslash($_POST['filters'])), false);
627 }
628
629 if (is_array($filters)) {
630 // Sanitize filter data
631 foreach ($filters as &$item) {
632 $item->text = sanitize_text_field($item->text);
633 if (isset($item->children) && is_array($item->children)) {
634 foreach ($item->children as &$child) {
635 $child->text = sanitize_text_field($child->text);
636 }
637 }
638 }
639 UFGaddMissingFilterKeys($filters);
640 }
641
642 update_option("ufg_filters_" . $ufg_gallery_id, $filters);
643
644 $ufg_details = array('ufg_gallery_id' => $ufg_gallery_id, 'gallery_name' => $ufg_gallery_name);
645 update_option("ufg_details_" . $ufg_gallery_id, $ufg_details);
646
647 wp_send_json_success();
648 }
649 add_action('wp_ajax_ufg_gallery_filters', 'ufg_gallery_filters_callback');
650
651 // 2. add images to the gallery
652 function ufg_li_generate_ajax_callback()
653 {
654 if (!isset($_POST['nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])), 'add-image')) {
655 wp_die(__('Nonce verification failed', 'filter-gallery'), '', array('response' => 403));
656 }
657
658 if (!current_user_can('manage_options')) {
659 wp_die(__('Insufficient permissions', 'filter-gallery'), '', array('response' => 403));
660 }
661
662 if (isset($_POST['attachment_id']) && isset($_POST['ufg_gallery_id'])) {
663 //defaults
664 $ufg_title = $ufg_alt = $ufg_description = $ufg_url = "";
665 //load values
666 $ufg_attachment_id = isset($_POST['attachment_id']) ? sanitize_text_field(wp_unslash($_POST['attachment_id'])) : 0;
667 $ufg_title = get_the_title($ufg_attachment_id);
668 $ufg_alt = get_post_meta($ufg_attachment_id, '_wp_attachment_image_alt', TRUE);
669 //wp_get_attachment_image_src ( int $ufg_attachment_id, string|array $size = 'thumbnail', bool $icon = false )
670 //thumb, thumbnail, medium, large, post-thumbnail
671 $medium = wp_get_attachment_image_src($ufg_attachment_id, 'medium', true); // attachment medium URL
672 $attachment = get_post($ufg_attachment_id);
673 $ufg_description = $attachment->post_content; // attachment description
674 //get saved filters
675 $ufg_gallery_id = isset($_POST['ufg_gallery_id']) ? sanitize_text_field(wp_unslash($_POST['ufg_gallery_id'])) : 0;
676 $filters = get_option("ufg_filters_" . $ufg_gallery_id);
677 ?>
678 <script>
679 jQuery(document).ready(function () {
680 jQuery(function (jQuery) {
681 jQuery('.ufg-image-filters').multiselect({
682 buttonWidth: '100%',
683 enableFiltering: true,
684 nonSelectedText: "<?php echo esc_js(__('Select Filters', 'filter-gallery')); ?>"
685 });
686 });
687 });
688 </script>
689 <li class="ufg-admin-image-item ufg-image-<?php echo intval($ufg_attachment_id); ?>"
690 data-position="<?php echo intval($ufg_attachment_id); ?>">
691 <div class="ufg-admin-form-group">
692 <input type="hidden" class="ufg-admin-form-control ufg-attachment-id"
693 name="ufg-attachment-id[<?php echo intval($ufg_attachment_id); ?>]"
694 value="<?php echo intval($ufg_attachment_id); ?>">
695 <img loading="lazy" src="<?php echo esc_url($medium[0]); ?>" class="ufg-admin-img" alt="" width="150px"
696 height="150px">
697 </div>
698 <div class="ufg-admin-form-group">
699 <input type="text" class="ufg-admin-form-control ufg-title"
700 name="ufg-title[<?php echo intval($ufg_attachment_id); ?>]" value="<?php echo esc_attr($ufg_title); ?>"
701 placeholder="<?php esc_attr_e('Image Title', 'filter-gallery'); ?>">
702 </div>
703 <div class="ufg-admin-form-group">
704 <input type="text" class="ufg-admin-form-control ufg-alt"
705 name="ufg-alt[<?php echo intval($ufg_attachment_id); ?>]" value="<?php echo esc_attr($ufg_alt); ?>"
706 placeholder="<?php esc_attr_e('Image Alternative Text', 'filter-gallery'); ?>">
707 </div>
708 <div class="ufg-admin-form-group">
709 <textarea class="ufg-admin-form-control ufg-description"
710 name="ufg-description[<?php echo intval($ufg_attachment_id); ?>]"
711 placeholder="<?php esc_attr_e('Image Description', 'filter-gallery'); ?>"><?php echo esc_textarea($ufg_description); ?></textarea>
712 </div>
713 <div class="ufg-admin-form-group">
714 <input type="url" disabled readonly class="ufg-admin-form-control ufg-url"
715 name="ufg-url[<?php echo intval($ufg_attachment_id); ?>]" value=""
716 placeholder="<?php esc_attr_e('Link URL (Pro Only)', 'filter-gallery'); ?>">
717 </div>
718 <div class="ufg-admin-form-group">
719 <?php
720 $ufg_gallery_id_num = (int) $ufg_gallery_id;
721 $ufg_gallery_data = get_option("ufg_gallery_" . $ufg_gallery_id_num);
722 $selected_filters = (isset($ufg_gallery_data['ufg-image-filters'][$ufg_attachment_id])) ? $ufg_gallery_data['ufg-image-filters'][$ufg_attachment_id] : array();
723 echo wp_kses_post(ufg_get_filter_list($ufg_attachment_id, $filters, $selected_filters));
724 ?>
725 </div>
726 <div class="ufg-admin-form-group ufg-admin-text-center">
727 <button type="button" id="ufg-remove-image"
728 onclick="return removeImage('<?php echo intval($ufg_attachment_id); ?>');"
729 class="ufg-admin-btn ufg-admin-btn-remove"><?php esc_html_e('Remove', 'filter-gallery'); ?></button>
730 </div>
731 </li>
732 <?php
733 wp_die(); // this is required to terminate immediately and return a proper response
734 }
735 }
736 add_action('wp_ajax_ufg_image_id', 'ufg_li_generate_ajax_callback');
737
738 // generate filter select for image
739 function ufg_get_filter_list($ufg_attachment_id, $filters, $selected_filters)
740 {
741
742 if (is_array($filters) && $filters_count = count($filters)) {
743
744 /* echo "<pre>";
745 echo "Filters";
746 echo "<hr>";
747 print_r(($filters));
748 echo "</pre>";
749 echo "<hr>"; */
750
751 /* echo "<pre>";
752 echo "Selected Filters";
753 echo "<hr>";
754 print_r(($selected_filters));
755 echo "</pre>";
756 echo "<hr>"; */
757
758 /* foreach ($selected_filters as $key => $value) {
759 echo '<input type="text" class="ufg-filter-image" name="ufg-filter-image['.$value.'][]" id="ufg-filter-image[]" style="width: 98%;" value="'.$ufg_attachment_id.'" >';
760 } */
761 // zero = Parent Filters | level_one = Child Filters
762 echo '<select name="ufg-image-filters[' . intval($ufg_attachment_id) . '][]" class="ufg-image-filters" data-max="" multiple="multiple">';
763 for ($i = 0; $i < $filters_count; $i++) {
764 $text_zero = $filters[$i]->text;
765 $value_zero = str_replace(" ", "-", strtolower($filters[$i]->filterkey));
766 if (is_array($selected_filters)) {
767 if (in_array($value_zero, $selected_filters) === TRUE)
768 $selected = "selected=selected";
769 else
770 $selected = "";
771 } else {
772 $selected_filters = array();
773 }
774 echo "<option value='" . esc_attr($value_zero) . "' " . esc_attr($selected) . ">" . esc_html($text_zero) . "</option>";
775
776 //check level one children
777 $child_count_level_one = 0;
778 $child_count_level_two = 0;
779 if (is_array($filters[$i]->children)) {
780 $child_count_level_one = count($filters[$i]->children);
781 }
782 if ($child_count_level_one) {
783 $level_one_array = $filters[$i]->children;
784 for ($j = 0; $j < $child_count_level_one; $j++) {
785 $value_level_one = str_replace(" ", "-", strtolower($level_one_array[$j]->filterkey));
786 if (in_array($value_level_one, $selected_filters) === TRUE)
787 $selected_one = "selected=selected";
788 else
789 $selected_one = "";
790 echo "<option value='" . esc_attr($value_level_one) . "' " . esc_attr($selected_one) . ">&#10148; " . esc_html($level_one_array[$j]->text) . "</option>";
791
792 /*
793 //check level 2 children
794 $child_count_level_two = count($level_one_array[$j]->children);
795 if($child_count_level_two) {
796 $level_two_array = $level_one_array[$j]->children;
797 for($k = 0; $k < $child_count_level_two; $k++){
798 $value_level_two = str_replace(" ","-", strtolower($level_two_array[$k]->text));
799 if(in_array($value_level_two, $selected_filters) === TRUE) $selected_two = "selected=selected"; else $selected_two = "";
800 echo "<option value='$value_level_two' $selected_two>&#10148; &#10148; ".$level_two_array[$k]->text."</option>";
801
802
803 //check level 3 children
804 $child_count_level_three = count($level_two_array[$k]->children);
805 if($child_count_level_three) {
806 $level_three_array = $level_two_array[$k]->children;
807 for($l = 0; $l < $child_count_level_three; $l++){
808 $value_level_three = str_replace(" ","-", strtolower($level_three_array[$l]->text));
809 if(in_array($value_level_three, $selected_filters) === TRUE) $selected_three = "selected=selected"; else $selected_three = "";
810 echo "<option value='$value_level_three' $selected_three>&#10148; &#10148; &#10148; ".$level_three_array[$l]->text."</option>";
811
812
813 //check level 4 children
814 $child_count_level_four = count($level_three_array[$l]->children);
815 if($child_count_level_four) {
816 $level_four_array = $level_three_array[$l]->children;
817 for($m = 0; $m < $child_count_level_four; $m++){
818 $value_level_four = str_replace(" ","-", strtolower($level_four_array[$m]->text));
819 if(in_array($value_level_four, $selected_filters) === TRUE) $selected_four = "selected=selected"; else $selected_four = "";
820 echo "<option value='$value_level_four' $selected_four>&#10148; &#10148; &#10148; &#10148; ".$level_four_array[$m]->text."</option>";
821
822
823 //check level 5 children
824 $child_count_level_five = count($level_four_array[$m]->children);
825 if($child_count_level_five) {
826 $level_five_array = $level_four_array[$m]->children;
827 for($n = 0; $n < $child_count_level_five; $n++){
828 $value_level_five = str_replace(" ","-", strtolower($level_five_array[$n]->text));
829 if(in_array($value_level_five, $selected_filters) === TRUE) $selected_five = "selected=selected"; else $selected_five = "";
830 echo "<option value='$value_level_five' $selected_five>&#10148; &#10148; &#10148; &#10148; &#10148; ".$level_five_array[$n]->text."</option>";
831 }
832 }
833 //check level 5 children end
834
835 }
836 }
837 //check level 4 children end
838
839 }
840 }
841 //check level 3 children end
842
843 }
844 }
845 //check level 2 children end
846 */
847 }
848 }
849 }
850 echo '</select>';
851 }
852 }
853
854 // 3. save gallery images
855 function ufg_save_gallery_callback()
856 {
857 if (!isset($_POST['nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])), 'save-gallery')) {
858 wp_send_json_error(__('Nonce verification failed', 'filter-gallery'));
859 }
860
861 if (!current_user_can('manage_options')) {
862 wp_send_json_error(__('Insufficient permissions', 'filter-gallery'));
863 }
864
865 $ufg_gallery_id = isset($_POST['id']) ? absint(sanitize_text_field(wp_unslash($_POST['id']))) : 0;
866 if ($ufg_gallery_id <= 0) {
867 wp_send_json_error(__('Invalid gallery ID', 'filter-gallery'));
868 }
869
870 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- data is sanitized after parse_str below
871 $image_id_raw = isset($_POST['image_id']) ? wp_unslash($_POST['image_id']) : '';
872 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- data is sanitized after parse_str below
873 $image_title_raw = isset($_POST['image_title']) ? wp_unslash($_POST['image_title']) : '';
874 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- data is sanitized after parse_str below
875 $image_alt_raw = isset($_POST['image_alt']) ? wp_unslash($_POST['image_alt']) : '';
876 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- data is sanitized after parse_str below
877 $image_description_raw = isset($_POST['image_description']) ? wp_unslash($_POST['image_description']) : '';
878 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- data is sanitized after parse_str below
879 $image_url_raw = isset($_POST['image_url']) ? wp_unslash($_POST['image_url']) : '';
880 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- data is sanitized after parse_str below
881 $image_filters_raw = isset($_POST['image_filters']) ? wp_unslash($_POST['image_filters']) : '';
882
883 $ufg_image_id = $ufg_image_title = $ufg_image_alt = $ufg_image_description = $ufg_image_url = $ufg_image_filters = array();
884
885 parse_str($image_id_raw, $ufg_image_id);
886 parse_str($image_title_raw, $ufg_image_title);
887 parse_str($image_alt_raw, $ufg_image_alt);
888 parse_str($image_description_raw, $ufg_image_description);
889 parse_str($image_url_raw, $ufg_image_url);
890 parse_str($image_filters_raw, $ufg_image_filters);
891
892 // update attachment meta - title, alt, description
893 if (isset($ufg_image_id['ufg-attachment-id']) && is_array($ufg_image_id['ufg-attachment-id'])) {
894 foreach ($ufg_image_id['ufg-attachment-id'] as $ufg_id) {
895 $ufg_id = absint($ufg_id);
896 if (!$ufg_id) {
897 continue;
898 }
899
900 // Security: verify target is an attachment and user has permission to edit it
901 if (get_post_type($ufg_id) !== 'attachment' || !current_user_can('edit_post', $ufg_id)) {
902 continue;
903 }
904
905 $ufg_title = isset($ufg_image_title['ufg-title'][$ufg_id]) ? sanitize_text_field($ufg_image_title['ufg-title'][$ufg_id]) : '';
906 $ufg_description = isset($ufg_image_description['ufg-description'][$ufg_id]) ? sanitize_textarea_field($ufg_image_description['ufg-description'][$ufg_id]) : '';
907 $ufg_alt = isset($ufg_image_alt['ufg-alt'][$ufg_id]) ? sanitize_text_field($ufg_image_alt['ufg-alt'][$ufg_id]) : '';
908
909 $ufg_image_update = array(
910 'ID' => $ufg_id,
911 'post_title' => $ufg_title,
912 'post_content' => $ufg_description,
913 );
914 wp_update_post($ufg_image_update);
915 update_post_meta($ufg_id, '_wp_attachment_image_alt', $ufg_alt);
916 }
917 }
918
919 $ufg_gallery = array(
920 'ufg-attachment-id' => isset($ufg_image_id['ufg-attachment-id']) ? $ufg_image_id['ufg-attachment-id'] : array(),
921 'ufg-title' => isset($ufg_image_title['ufg-title']) ? $ufg_image_title['ufg-title'] : array(),
922 'ufg-alt' => isset($ufg_image_alt['ufg-alt']) ? $ufg_image_alt['ufg-alt'] : array(),
923 'ufg-description' => isset($ufg_image_description['ufg-description']) ? $ufg_image_description['ufg-description'] : array(),
924 'ufg-url' => isset($ufg_image_url['ufg-url']) ? $ufg_image_url['ufg-url'] : array(),
925 'ufg-image-filters' => isset($ufg_image_filters['ufg-image-filters']) ? $ufg_image_filters['ufg-image-filters'] : array(),
926 );
927
928 update_option("ufg_gallery_" . $ufg_gallery_id, $ufg_gallery);
929 wp_send_json_success();
930 }
931 add_action('wp_ajax_ufg_save_gallery', 'ufg_save_gallery_callback');
932
933 // 4. load gallery images
934 function ufg_load_gallery_callback($ufg_gallery_id)
935 {
936 if (!isset($_POST['nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])), 'load-gallery')) {
937 wp_die(__('Nonce verification failed', 'filter-gallery'), '', array('response' => 403));
938 }
939
940 if (!current_user_can('manage_options')) {
941 wp_die(__('Insufficient permissions', 'filter-gallery'), '', array('response' => 403));
942 }
943
944 //get / create next gallery id
945 if (isset($_POST['id'])) {
946 $ufg_gallery_id = absint(sanitize_text_field(wp_unslash($_POST['id'])));
947 if ($ufg_gallery_id <= 0) {
948 wp_die(__('Invalid gallery ID', 'filter-gallery'), '', array('response' => 400));
949 }
950
951 // load filters and gallery options
952 $ufg_filters = get_option("ufg_filters_" . $ufg_gallery_id);
953 $ufg_gallery = get_option("ufg_gallery_" . $ufg_gallery_id);
954 //defaults
955 $ufg_title = $ufg_alt = $ufg_description = $ufg_url = "";
956 if (is_array($ufg_gallery) && isset($ufg_gallery['ufg-attachment-id']) && is_array($ufg_gallery['ufg-attachment-id'])) {
957 foreach ($ufg_gallery['ufg-attachment-id'] as $key => $value) {
958 $ufg_attachment_id = $value;
959 //load values
960 $ufg_title = get_the_title($ufg_attachment_id);
961 $ufg_alt = get_post_meta($ufg_attachment_id, '_wp_attachment_image_alt', TRUE);
962 //wp_get_attachment_image_src ( int $ufg_attachment_id, string|array $size = 'thumbnail', bool $icon = false )
963 //thumb, thumbnail, medium, large, post-thumbnail
964 $medium = wp_get_attachment_image_src($ufg_attachment_id, 'medium', true); // attachment medium URL
965 $attachment = get_post($ufg_attachment_id);
966 $ufg_description = $attachment ? $attachment->post_content : ''; // attachment description
967 //get saved filters
968 $filters = get_option("ufg_filters_" . $ufg_gallery_id);
969 ?>
970 <li class="ufg-admin-image-item ufg-image-<?php echo intval($ufg_attachment_id); ?>"
971 data-position="<?php echo intval($ufg_attachment_id); ?>">
972 <div class="ufg-admin-form-group">
973 <input type="hidden" class="ufg-admin-form-control ufg-attachment-id" id="ufg-attachment-id"
974 name="ufg-attachment-id[<?php echo intval($ufg_attachment_id); ?>]"
975 value="<?php echo intval($ufg_attachment_id); ?>">
976 <img src="<?php echo esc_url($medium[0]); ?>" class="ufg-admin-img" alt="" width="150px" height="150px">
977 <span class="ufg-admin-badge">Image ID: <?php echo intval($ufg_attachment_id); ?></span>
978 </div>
979 <div class="ufg-admin-form-group">
980 <input type="text" class="ufg-admin-form-control ufg-title"
981 name="ufg-title[<?php echo intval($ufg_attachment_id); ?>]" value="<?php echo esc_attr($ufg_title); ?>"
982 placeholder="<?php esc_attr_e('Image Title', 'filter-gallery'); ?>">
983 </div>
984 <div class="ufg-admin-form-group">
985 <input type="text" class="ufg-admin-form-control ufg-alt"
986 name="ufg-alt[<?php echo intval($ufg_attachment_id); ?>]" value="<?php echo esc_attr($ufg_alt); ?>"
987 placeholder="<?php esc_attr_e('Image Alternative Text', 'filter-gallery'); ?>">
988 </div>
989 <div class="ufg-admin-form-group">
990 <textarea class="ufg-admin-form-control ufg-description"
991 name="ufg-description[<?php echo intval($ufg_attachment_id); ?>]"
992 placeholder="<?php esc_attr_e('Image Description', 'filter-gallery'); ?>"><?php echo esc_textarea($ufg_description); ?></textarea>
993 </div>
994 <div class="ufg-admin-form-group">
995 <input type="url" disabled readonly class="ufg-admin-form-control ufg-url"
996 name="ufg-url[<?php echo intval($ufg_attachment_id); ?>]"
997 value=""
998 placeholder="<?php esc_attr_e('Link URL (Pro Only)', 'filter-gallery'); ?>">
999 </div>
1000 <div class="ufg-admin-form-group">
1001 <?php $selected_filters = isset($ufg_gallery['ufg-image-filters'][$value]) ? $ufg_gallery['ufg-image-filters'][$value] : array();
1002
1003 echo wp_kses_post(ufg_get_filter_list($ufg_attachment_id, $filters, $selected_filters));
1004 ?>
1005 </div>
1006 <div class="ufg-admin-form-group ufg-admin-text-center">
1007 <button type="button" id="ufg-remove-image"
1008 onclick="return removeImage('<?php echo intval($ufg_attachment_id); ?>');"
1009 class="ufg-admin-btn ufg-admin-btn-remove"><?php esc_html_e('Remove', 'filter-gallery'); ?></button>
1010 </div>
1011 </li>
1012 <?php
1013 }
1014 }
1015 ?>
1016 <script>
1017 jQuery(document).ready(function () {
1018 jQuery(function (jQuery) {
1019 jQuery('.ufg-image-filters').multiselect({
1020 buttonWidth: '100%',
1021 enableFiltering: true,
1022 nonSelectedText: "<?php echo esc_js(__('Select Filters', 'filter-gallery')); ?>"
1023 });
1024 });
1025 });
1026 </script>
1027 <?php
1028 }
1029 wp_die();
1030 }
1031 add_action('wp_ajax_ufg_load_gallery', 'ufg_load_gallery_callback');
1032
1033 // 5. save gallery settings
1034 function ufg_save_setting_callback()
1035 {
1036 if (!isset($_POST['nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])), 'save-setting')) {
1037 wp_send_json_error(__('Nonce verification failed', 'filter-gallery'));
1038 }
1039
1040 if (!current_user_can('manage_options')) {
1041 wp_send_json_error(__('Insufficient permissions', 'filter-gallery'));
1042 }
1043
1044 $ufg_gallery_id = isset($_POST['ufg_gallery_id']) ? absint(sanitize_text_field(wp_unslash($_POST['ufg_gallery_id']))) : 0;
1045 if ($ufg_gallery_id <= 0) {
1046 wp_send_json_error(__('Invalid gallery ID', 'filter-gallery'));
1047 }
1048 $settings = array(
1049 //gallery details
1050 'ufg_gallery_id' => $ufg_gallery_id,
1051
1052 //filters settings
1053 'default_filter' => isset($_POST['default_filter']) ? sanitize_text_field(wp_unslash($_POST['default_filter'])) : '',
1054 'filter_style' => (isset($_POST['filter_style']) && sanitize_text_field(wp_unslash($_POST['filter_style'])) === 'dropdown') ? 'dropdown' : 'buttons',
1055 'combine_filter_search' => '0',
1056 'filter_padding' => isset($_POST['filter_padding']) ? sanitize_text_field(wp_unslash($_POST['filter_padding'])) : '',
1057 'filter_margin' => isset($_POST['filter_margin']) ? sanitize_text_field(wp_unslash($_POST['filter_margin'])) : '',
1058 'filter_padding_type' => isset($_POST['filter_padding_type']) ? sanitize_text_field(wp_unslash($_POST['filter_padding_type'])) : '',
1059 'filter_padding_v' => isset($_POST['filter_padding_v']) ? sanitize_text_field(wp_unslash($_POST['filter_padding_v'])) : '',
1060 'filter_padding_h' => isset($_POST['filter_padding_h']) ? sanitize_text_field(wp_unslash($_POST['filter_padding_h'])) : '',
1061 'filter_margin_val' => isset($_POST['filter_margin_val']) ? sanitize_text_field(wp_unslash($_POST['filter_margin_val'])) : '',
1062 'parent_button_hover_color' => isset($_POST['parent_button_hover_color']) ? sanitize_text_field(wp_unslash($_POST['parent_button_hover_color'])) : '',
1063 'parent_active_button_color' => isset($_POST['parent_active_button_color']) ? sanitize_text_field(wp_unslash($_POST['parent_active_button_color'])) : '',
1064 'parent_active_button_bg_color' => isset($_POST['parent_active_button_bg_color']) ? sanitize_text_field(wp_unslash($_POST['parent_active_button_bg_color'])) : '',
1065 'show_filters' => isset($_POST['show_filters']) ? sanitize_text_field(wp_unslash($_POST['show_filters'])) : '',
1066 'show_search_box' => isset($_POST['show_search_box']) ? sanitize_text_field(wp_unslash($_POST['show_search_box'])) : '',
1067 'search_box_placeholder' => isset($_POST['search_box_placeholder']) ? sanitize_text_field(wp_unslash($_POST['search_box_placeholder'])) : '',
1068 'show_filters_icon' => isset($_POST['show_filters_icon']) ? sanitize_text_field(wp_unslash($_POST['show_filters_icon'])) : '',
1069 'enable_deep_linking' => '0',
1070 //'show_filters_count' => isset($_POST['show_filters_count']) ? sanitize_text_field(wp_unslash($_POST['show_filters_count'])) : '',
1071 'show_all_button' => isset($_POST['show_all_button']) ? sanitize_text_field(wp_unslash($_POST['show_all_button'])) : '',
1072 'all_button_text' => isset($_POST['all_button_text']) ? sanitize_text_field(wp_unslash($_POST['all_button_text'])) : '',
1073 'all_button_icon' => isset($_POST['all_button_icon']) ? sanitize_text_field(wp_unslash($_POST['all_button_icon'])) : '',
1074
1075 'all_button_color' => isset($_POST['all_button_color']) ? sanitize_text_field(wp_unslash($_POST['all_button_color'])) : '',
1076 'all_button_bg_color' => isset($_POST['all_button_bg_color']) ? sanitize_text_field(wp_unslash($_POST['all_button_bg_color'])) : '',
1077 'parent_filters_heading' => isset($_POST['parent_filters_heading']) ? sanitize_text_field(wp_unslash($_POST['parent_filters_heading'])) : '',
1078 'parent_button_color' => isset($_POST['parent_button_color']) ? sanitize_text_field(wp_unslash($_POST['parent_button_color'])) : '',
1079 'parent_button_bg_color' => isset($_POST['parent_button_bg_color']) ? sanitize_text_field(wp_unslash($_POST['parent_button_bg_color'])) : '',
1080 'l1_filters_heading' => isset($_POST['l1_filters_heading']) ? sanitize_text_field(wp_unslash($_POST['l1_filters_heading'])) : '',
1081 'l1_button_color' => isset($_POST['l1_button_color']) ? sanitize_text_field(wp_unslash($_POST['l1_button_color'])) : '',
1082 'l1_button_bg_color' => isset($_POST['l1_button_bg_color']) ? sanitize_text_field(wp_unslash($_POST['l1_button_bg_color'])) : '',
1083 'l2_button_color' => isset($_POST['l2_button_color']) ? sanitize_text_field(wp_unslash($_POST['l2_button_color'])) : '',
1084 'l2_button_bg_color' => isset($_POST['l2_button_bg_color']) ? sanitize_text_field(wp_unslash($_POST['l2_button_bg_color'])) : '',
1085 'l3_button_color' => isset($_POST['l3_button_color']) ? sanitize_text_field(wp_unslash($_POST['l3_button_color'])) : '',
1086 'l3_button_bg_color' => isset($_POST['l3_button_bg_color']) ? sanitize_text_field(wp_unslash($_POST['l3_button_bg_color'])) : '',
1087 'l4_button_color' => isset($_POST['l4_button_color']) ? sanitize_text_field(wp_unslash($_POST['l4_button_color'])) : '',
1088 'l4_button_bg_color' => isset($_POST['l4_button_bg_color']) ? sanitize_text_field(wp_unslash($_POST['l4_button_bg_color'])) : '',
1089 'child_filter_effect' => isset($_POST['child_filter_effect']) ? sanitize_text_field(wp_unslash($_POST['child_filter_effect'])) : '',
1090 'active_button_color' => isset($_POST['active_button_color']) ? sanitize_text_field(wp_unslash($_POST['active_button_color'])) : '',
1091 'active_button_bg_color' => isset($_POST['active_button_bg_color']) ? sanitize_text_field(wp_unslash($_POST['active_button_bg_color'])) : '',
1092
1093 //gallery settings
1094 'columns_desktop' => (isset($_POST['columns_desktop']) && in_array($_POST['columns_desktop'], array('3', '4', '6'), true)) ? sanitize_text_field(wp_unslash($_POST['columns_desktop'])) : '4',
1095 'columns_tab' => (isset($_POST['columns_tab']) && in_array($_POST['columns_tab'], array('1', '2', '3'), true)) ? sanitize_text_field(wp_unslash($_POST['columns_tab'])) : '3',
1096 'columns_mobile_landscape' => (isset($_POST['columns_mobile_landscape']) && in_array($_POST['columns_mobile_landscape'], array('1', '2', '3'), true)) ? sanitize_text_field(wp_unslash($_POST['columns_mobile_landscape'])) : '3',
1097 'columns_mobile_portrait' => (isset($_POST['columns_mobile_portrait']) && in_array($_POST['columns_mobile_portrait'], array('1', '2'), true)) ? sanitize_text_field(wp_unslash($_POST['columns_mobile_portrait'])) : '2',
1098 'thumbnail_image' => isset($_POST['thumbnail_image']) ? sanitize_text_field(wp_unslash($_POST['thumbnail_image'])) : '',
1099 'thumbnail_image_size' => isset($_POST['thumbnail_image_size']) ? sanitize_text_field(wp_unslash($_POST['thumbnail_image_size'])) : '',
1100 'thumbnail_border' => isset($_POST['thumbnail_border']) ? sanitize_text_field(wp_unslash($_POST['thumbnail_border'])) : '',
1101 'thumbnail_border_thickness' => isset($_POST['thumbnail_border_thickness']) ? sanitize_text_field(wp_unslash($_POST['thumbnail_border_thickness'])) : '',
1102 'thumbnail_border_color' => isset($_POST['thumbnail_border_color']) ? sanitize_text_field(wp_unslash($_POST['thumbnail_border_color'])) : '',
1103 'thumbnail_bg_color' => isset($_POST['thumbnail_bg_color']) ? sanitize_text_field(wp_unslash($_POST['thumbnail_bg_color'])) : '',
1104 'image_title' => isset($_POST['image_title']) ? sanitize_text_field(wp_unslash($_POST['image_title'])) : '',
1105 'image_title_font_size' => isset($_POST['image_title_font_size']) ? sanitize_text_field(wp_unslash($_POST['image_title_font_size'])) : '',
1106 'image_title_color' => isset($_POST['image_title_color']) ? sanitize_text_field(wp_unslash($_POST['image_title_color'])) : '',
1107 'image_description' => isset($_POST['image_description']) ? sanitize_text_field(wp_unslash($_POST['image_description'])) : '',
1108 'image_description_font_size' => isset($_POST['image_description_font_size']) ? sanitize_text_field(wp_unslash($_POST['image_description_font_size'])) : '',
1109 'image_description_color' => isset($_POST['image_description_color']) ? sanitize_text_field(wp_unslash($_POST['image_description_color'])) : '',
1110 'image_description_text_limit' => isset($_POST['image_description_text_limit']) ? sanitize_text_field(wp_unslash($_POST['image_description_text_limit'])) : '',
1111 'image_hover_effect' => isset($_POST['image_hover_effect']) ? sanitize_text_field(wp_unslash($_POST['image_hover_effect'])) : '',
1112 'read_more_link_sh' => '0',
1113 'read_more_link' => '1',
1114 'read_more_button_text' => 'Read More Link',
1115 'read_more_button_icon' => 'fas fa-link',
1116 'read_more_button_color' => '#ffffff',
1117 'read_more_button_bg_color' => '#0080ff',
1118 'read_more_button_target' => '_self',
1119 'image_sorting' => (isset($_POST['image_sorting']) && in_array($_POST['image_sorting'], array('5', '1', '2'), true)) ? sanitize_text_field(wp_unslash($_POST['image_sorting'])) : '5',
1120 'custom_css' => '',
1121
1122 //lightbox settings
1123 'lightbox' => isset($_POST['lightbox']) ? sanitize_text_field(wp_unslash($_POST['lightbox'])) : '',
1124 'lightbox_title' => isset($_POST['lightbox_title']) ? sanitize_text_field(wp_unslash($_POST['lightbox_title'])) : '',
1125 'lightbox_description' => '0',
1126 'lightbox_numbering' => '0',
1127
1128 // Load More
1129 'load_more' => 'off',
1130 'load_limit' => isset($_POST['load_limit']) ? sanitize_text_field(wp_unslash($_POST['load_limit'])) : '',
1131 'load_color' => isset($_POST['load_color']) ? sanitize_text_field(wp_unslash($_POST['load_color'])) : '',
1132 'load_txt_color' => isset($_POST['load_txt_color']) ? sanitize_text_field(wp_unslash($_POST['load_txt_color'])) : '',
1133 'load_btn_txt' => isset($_POST['load_btn_txt']) ? sanitize_text_field(wp_unslash($_POST['load_btn_txt'])) : '',
1134 );
1135
1136 update_option("ufg_settings_" . $ufg_gallery_id, $settings);
1137 wp_send_json_success();
1138 }
1139 add_action('wp_ajax_ufg_save_setting', 'ufg_save_setting_callback');
1140
1141 // 6. remove gallery/galleries start
1142 function ufg_remove_gallery_callback()
1143 {
1144 if (!isset($_POST['nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])), 'ufg-remove-gallery')) {
1145 wp_die(__('Nonce verification failed', 'filter-gallery'), '', array('response' => 403));
1146 }
1147
1148 if (!current_user_can('manage_options')) {
1149 wp_die(__('Insufficient permissions', 'filter-gallery'), '', array('response' => 403));
1150 }
1151
1152 // verified action
1153 if (isset($_POST['ufg_gallery_id']) && isset($_POST['do_action'])) {
1154 $ufg_do_action = sanitize_text_field(wp_unslash($_POST['do_action']));
1155
1156 //single gallery delete
1157 if ($ufg_do_action == 'single') {
1158 $single_id = !is_array($_POST['ufg_gallery_id']) ? absint(wp_unslash($_POST['ufg_gallery_id'])) : 0;
1159 if ($single_id > 0) {
1160 delete_option("ufg_filters_" . $single_id);
1161 delete_option("ufg_gallery_" . $single_id);
1162 delete_option("ufg_settings_" . $single_id);
1163 delete_option("ufg_details_" . $single_id);
1164 }
1165 }
1166
1167 //multiple gallery delete
1168 if ($ufg_do_action == 'multiple' && is_array($_POST['ufg_gallery_id'])) {
1169 $raw_ids = array_map('absint', wp_unslash($_POST['ufg_gallery_id']));
1170 foreach ($raw_ids as $single_id) {
1171 if ($single_id > 0) {
1172 delete_option("ufg_filters_" . $single_id);
1173 delete_option("ufg_gallery_" . $single_id);
1174 delete_option("ufg_settings_" . $single_id);
1175 delete_option("ufg_details_" . $single_id);
1176 }
1177 }
1178 }
1179 }
1180 wp_die();
1181 }
1182 add_action('wp_ajax_ufg_remove_gallery', 'ufg_remove_gallery_callback');
1183 // 6. remove gallery/galleries end
1184
1185 // 7. clone gallery start
1186 function ufg_clone_gallery_callback()
1187 {
1188 if (!isset($_POST['nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])), 'ufg-clone-gallery')) {
1189 wp_die(__('Nonce verification failed action.', 'filter-gallery'), '', array('response' => 403));
1190 }
1191
1192 if (!current_user_can('manage_options')) {
1193 wp_die(__('Insufficient permissions', 'filter-gallery'), '', array('response' => 403));
1194 }
1195
1196 // verified action
1197 if (isset($_POST['ufg_gallery_id']) && isset($_POST['ufg_gallery_counter'])) {
1198 $ufg_gallery_id = absint(sanitize_text_field(wp_unslash($_POST['ufg_gallery_id'])));
1199 $ufg_gallery_counter = absint(sanitize_text_field(wp_unslash($_POST['ufg_gallery_counter'])));
1200 if ($ufg_gallery_id <= 0) {
1201 wp_die(__('Invalid gallery ID', 'filter-gallery'), '', array('response' => 400));
1202 }
1203
1204 //get cloning gallery data
1205 $ufg_cloning_filters = get_option("ufg_filters_" . $ufg_gallery_id);
1206 $ufg_cloning_gallery = get_option("ufg_gallery_" . $ufg_gallery_id);
1207 $ufg_cloning_setting = get_option("ufg_settings_" . $ufg_gallery_id);
1208 $ufg_cloning_details = get_option("ufg_details_" . $ufg_gallery_id);
1209
1210 //print_r($ufg_cloning_gallery);
1211
1212 //generate new gallery id for clone
1213 $new_ufg_gallery_id = ufg_get_next_id();
1214 $new_ufg_gallery_name = $ufg_cloning_details['gallery_name'] . ' - Clone';
1215
1216 // update clone id into gallery data
1217 foreach ($ufg_cloning_gallery as $key => $value) {
1218 $ufg_cloning_setting['ufg_gallery_id'] = $new_ufg_gallery_id;
1219 }
1220 // update gallery details
1221 $ufg_cloning_details = array('ufg_gallery_id' => $ufg_gallery_id, 'gallery_name' => $new_ufg_gallery_name);
1222
1223 //print_r($ufg_cloning_gallery);
1224 if ($new_ufg_gallery_id > $ufg_gallery_id) {
1225 add_option('ufg_filters_' . $new_ufg_gallery_id, $ufg_cloning_filters);
1226 add_option('ufg_gallery_' . $new_ufg_gallery_id, $ufg_cloning_gallery);
1227 add_option('ufg_settings_' . $new_ufg_gallery_id, $ufg_cloning_setting);
1228 update_option('ufg_details_' . $new_ufg_gallery_id, $ufg_cloning_details);
1229 $ufg_do_action = "'single'";
1230
1231 echo '
1232 <tr id=' . intval($new_ufg_gallery_id) . '>
1233 <th scope="row">' . intval($ufg_gallery_counter) . '</th>
1234 <td>' . esc_html($new_ufg_gallery_name) . '</td>
1235 <td>
1236 <input type="text" id="ufg-shortcode-' . intval($new_ufg_gallery_id) . '" class="btn btn-info btn-sm" value="[ufg id=' . intval($new_ufg_gallery_id) . ']">
1237 <button type="button" id="ufg-shortcode-' . intval($new_ufg_gallery_id) . '" class="btn btn-info btn-sm" title="Click To Copy Gallery Shortcode" onclick="return UFGCopyShortcode(' . intval($new_ufg_gallery_id) . ')">Copy</button>
1238 <button class="btn btn-sm btn-success d-none ufg-copied-' . intval($new_ufg_gallery_id) . '">Copied</button>
1239 </td>
1240 <td>
1241 <button type="button" id="ufg-clone" class="btn btn-warning btn-sm" title="Clone Gallery" value="' . intval($new_ufg_gallery_id) . '" onclick="return UFGCloneGallery(' . intval($new_ufg_gallery_id) . ', ' . intval($ufg_gallery_counter) . ');"><i class="fas fa-copy"></i></button>
1242 <a href="?page=ufg-manage-gallery&id=' . intval($new_ufg_gallery_id) . '" class="btn btn-primary btn-sm" href="#"><i class="fas fa-edit"></i></a>
1243 <button id="ufg-delete-gallery" class="btn btn-danger btn-sm" title="Delete Gallery" value="' . intval($new_ufg_gallery_id) . '" onclick="return UFGRemoveGallery(\'' . intval($new_ufg_gallery_id) . '\', \'' . esc_js($ufg_do_action) . '\');"><i class="fas fa-trash-alt"></i></button>
1244 </td>
1245 <td class="text-center">
1246 <input type="checkbox" id="ufg-gallery-id" name="ufg-gallery-id" value="' . intval($new_ufg_gallery_id) . '" title="Select Gallery">
1247 </td>
1248 </tr>
1249 ';
1250 }
1251 }
1252 wp_die();
1253 }
1254 add_action('wp_ajax_ufg_clone_gallery', 'ufg_clone_gallery_callback');
1255 // 7. clone gallery end
1256
1257 // 8. Import / Export page
1258 function ufg_import_export_page()
1259 {
1260 require plugin_dir_path(__FILE__) . 'admin/import-export.php';
1261 }
1262
1263 // 8a. Export galleries AJAX
1264 function ufg_export_galleries_callback()
1265 {
1266 if (!isset($_POST['nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])), 'ufg-import-export')) {
1267 wp_send_json_error('Nonce verification failed.');
1268 }
1269
1270 if (!current_user_can('manage_options')) {
1271 wp_send_json_error('Permission denied.');
1272 }
1273
1274 $gallery_ids = isset($_POST['gallery_ids']) ? array_map('absint', $_POST['gallery_ids']) : array();
1275 if (empty($gallery_ids)) {
1276 wp_send_json_error('No galleries selected.');
1277 }
1278
1279 $export_data = array(
1280 'plugin' => 'filter-gallery-pro',
1281 'version' => '6.0.5',
1282 'export_date' => gmdate('Y-m-d'),
1283 'source_url' => home_url(),
1284 'galleries' => array(),
1285 );
1286
1287 foreach ($gallery_ids as $gid) {
1288 $details = get_option("ufg_details_" . $gid);
1289 $filters = get_option("ufg_filters_" . $gid);
1290 $gallery = get_option("ufg_gallery_" . $gid);
1291 $settings = get_option("ufg_settings_" . $gid);
1292
1293 if (!$gallery && !$filters) {
1294 continue; // skip if gallery doesn't exist
1295 }
1296
1297 // Build image URL map: attachment_id => full URL
1298 $image_urls = array();
1299 if (is_array($gallery) && isset($gallery['ufg-attachment-id']) && is_array($gallery['ufg-attachment-id'])) {
1300 foreach ($gallery['ufg-attachment-id'] as $att_id) {
1301 $url = wp_get_attachment_url($att_id);
1302 if ($url) {
1303 $image_urls[$att_id] = $url;
1304 }
1305 }
1306 }
1307
1308 $export_data['galleries'][] = array(
1309 'original_id' => $gid,
1310 'details' => $details ? $details : array(),
1311 'filters' => $filters ? $filters : array(),
1312 'gallery' => $gallery ? $gallery : array(),
1313 'settings' => $settings ? $settings : array(),
1314 'image_urls' => $image_urls,
1315 );
1316 }
1317
1318 wp_send_json_success($export_data);
1319 }
1320 add_action('wp_ajax_ufg_export_galleries', 'ufg_export_galleries_callback');
1321
1322 // 8b. Import single gallery AJAX
1323 function ufg_import_gallery_callback()
1324 {
1325 if (!isset($_POST['nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])), 'ufg-import-export')) {
1326 wp_send_json_error('Nonce verification failed.');
1327 }
1328
1329 if (!current_user_can('manage_options')) {
1330 wp_send_json_error('Permission denied.');
1331 }
1332
1333 if (!isset($_POST['gallery_data'])) {
1334 wp_send_json_error('No gallery data provided.');
1335 }
1336
1337 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1338 $raw = wp_unslash($_POST['gallery_data']);
1339 $data = json_decode($raw, true);
1340 if (!$data || !is_array($data)) {
1341 wp_send_json_error('Invalid gallery data format.');
1342 }
1343
1344 $skip_images = isset($_POST['skip_images']) && $_POST['skip_images'] === '1';
1345
1346 // Get a new gallery ID
1347 $new_id = ufg_get_next_id();
1348
1349 $details = isset($data['details']) ? $data['details'] : array();
1350 $filters = isset($data['filters']) ? $data['filters'] : array();
1351 $gallery = isset($data['gallery']) ? $data['gallery'] : array();
1352 $settings = isset($data['settings']) ? $data['settings'] : array();
1353 $image_urls = isset($data['image_urls']) ? $data['image_urls'] : array();
1354
1355 $images_imported = 0;
1356 $images_failed = 0;
1357
1358 // Remap attachment IDs if we have images to import
1359 if (!$skip_images && !empty($image_urls) && is_array($gallery) && isset($gallery['ufg-attachment-id'])) {
1360 // Need media sideload function
1361 require_once ABSPATH . 'wp-admin/includes/media.php';
1362 require_once ABSPATH . 'wp-admin/includes/file.php';
1363 require_once ABSPATH . 'wp-admin/includes/image.php';
1364
1365 $id_map = array(); // old_id => new_id
1366
1367 foreach ($image_urls as $old_id => $url) {
1368 // Download and sideload the image
1369 $new_att_id = media_sideload_image($url, 0, null, 'id');
1370 if (!is_wp_error($new_att_id)) {
1371 $id_map[$old_id] = $new_att_id;
1372 $images_imported++;
1373 } else {
1374 $images_failed++;
1375 }
1376 }
1377
1378 // Remap attachment IDs in gallery data
1379 if (!empty($id_map)) {
1380 // Remap ufg-attachment-id array
1381 $new_att_ids = array();
1382 foreach ($gallery['ufg-attachment-id'] as $old_id) {
1383 if (isset($id_map[$old_id])) {
1384 $new_att_ids[] = $id_map[$old_id];
1385 }
1386 }
1387 $gallery['ufg-attachment-id'] = $new_att_ids;
1388
1389 // Remap keyed arrays: ufg-url, ufg-title, ufg-alt, ufg-description, ufg-image-filters
1390 $keyed_fields = array('ufg-url', 'ufg-title', 'ufg-alt', 'ufg-description', 'ufg-image-filters');
1391 foreach ($keyed_fields as $field) {
1392 if (isset($gallery[$field]) && is_array($gallery[$field])) {
1393 $new_data = array();
1394 foreach ($gallery[$field] as $old_key => $val) {
1395 $new_key = isset($id_map[$old_key]) ? $id_map[$old_key] : $old_key;
1396 $new_data[$new_key] = $val;
1397 }
1398 $gallery[$field] = $new_data;
1399 }
1400 }
1401 }
1402 } elseif ($skip_images) {
1403 // Clear image data when skipping
1404 $gallery['ufg-attachment-id'] = array();
1405 $gallery['ufg-url'] = array();
1406 $gallery['ufg-title'] = array();
1407 $gallery['ufg-alt'] = array();
1408 $gallery['ufg-description'] = array();
1409 $gallery['ufg-image-filters'] = array();
1410 }
1411
1412 // Update gallery name for imported gallery
1413 $gallery_name = isset($details['gallery_name']) ? $details['gallery_name'] : 'Imported Gallery';
1414 $details = array(
1415 'ufg_gallery_id' => $new_id,
1416 'gallery_name' => $gallery_name . ' (Imported)',
1417 );
1418
1419 // Save all 4 option entries
1420 add_option('ufg_filters_' . $new_id, $filters);
1421 add_option('ufg_gallery_' . $new_id, $gallery);
1422 add_option('ufg_settings_' . $new_id, $settings);
1423 update_option('ufg_details_' . $new_id, $details);
1424
1425 // Clear cache so new gallery shows up
1426 wp_cache_delete('ufg_all_galleries', 'ufg_galleries');
1427 wp_cache_delete('ufg_next_gallery_id', 'ufg_galleries');
1428
1429 wp_send_json_success(array(
1430 'new_id' => $new_id,
1431 'gallery_name' => $details['gallery_name'],
1432 'images_imported' => $images_imported,
1433 'images_failed' => $images_failed,
1434 ));
1435 }
1436 add_action('wp_ajax_ufg_import_gallery', 'ufg_import_gallery_callback');
1437
1438 // Register and enqueue sf scripts
1439 function ufg_register_scripts()
1440 {
1441 // Register and enqueue jQuery
1442 wp_register_script('jquery', false, array(), '3.6.0', true);
1443 wp_enqueue_script('jquery');
1444
1445 // Register and enqueue styles with versions
1446 wp_register_style('ufg-frontend-css', plugin_dir_url(__FILE__) . 'admin/assets/css/ufg-frontend.css', array(), UFG_VERSION);
1447
1448 wp_register_style('ufg-fontawesome-css', plugin_dir_url(__FILE__) . 'admin/assets/fontawesome-free-6.5.2-web/css/all.min.css', array(), '6.5.2');
1449
1450 wp_register_style('ufg-lightbox-css', plugin_dir_url(__FILE__) . 'admin/assets/lightbox/lokesh/css/ufg-lightbox-min.css', array(), '4.5.2');
1451
1452 // Register and enqueue scripts with versions and dependencies
1453 wp_register_script('ufg-lightbox-js', plugin_dir_url(__FILE__) . 'admin/assets/lightbox/lokesh/js/ufg.lightbox.min.js', array('jquery'), '2.11.2', true);
1454
1455 wp_register_script('ufg-isotope-js', plugin_dir_url(__FILE__) . 'admin/assets/js/isotope.pkgd.min.js', array('jquery'), '3.0.6', true);
1456
1457 // Register a custom script to attach inline script
1458 wp_register_script('ufg-custom-js', plugin_dir_url(__FILE__) . 'admin/assets/js/ufg-custom.js', array('jquery', 'ufg-isotope-js'), '1.0.0', true);
1459 }
1460 add_action('wp_enqueue_scripts', 'ufg_register_scripts');
1461
1462 include('shortcode.php');
1463
1464 // Gallery Text Widget Support
1465 add_filter('widget_text', 'do_shortcode');
1466