PluginProbe ʕ •ᴥ•ʔ
Gallery : FooGallery / 3.1.36
Gallery : FooGallery v3.1.36
3.3.0 3.1.31 3.1.32 3.1.34 3.1.36 3.2.0 3.2.6 trunk 1.10.3 2.0.24 2.1.34 2.2.44 2.3.3 2.4.32 3.0.6 3.1.11 3.1.12 3.1.13 3.1.20 3.1.25 3.1.26 3.1.26.1 3.1.26.2
foogallery / includes / class-previews.php
foogallery / includes Last commit date
abilities 2 months ago admin 2 months ago compatibility 2 months ago extensions 2 months ago foopluginbase 2 months ago public 2 months ago thumbs 2 months ago asset-manifest.php 2 months ago class-attachment-filters.php 2 months ago class-command-palette.php 2 months ago class-foogallery-animated-gif-support.php 2 months ago class-foogallery-attachment-custom-class.php 2 months ago class-foogallery-attachment-filename.php 2 months ago class-foogallery-attachment-type.php 2 months ago class-foogallery-attachment.php 2 months ago class-foogallery-cache.php 2 months ago class-foogallery-common-fields.php 2 months ago class-foogallery-crop-position.php 2 months ago class-foogallery-datasource-media_library.php 2 months ago class-foogallery-debug.php 2 months ago class-foogallery-delayed-runtime-loader.php 2 months ago class-foogallery-extensions-compatibility.php 2 months ago class-foogallery-force-https.php 2 months ago class-foogallery-lazyload.php 2 months ago class-foogallery-license-constant-handler.php 2 months ago class-foogallery-lightbox.php 2 months ago class-foogallery-paging.php 2 months ago class-foogallery-password-protect.php 2 months ago class-foogallery-sitemaps.php 2 months ago class-foogallery-widget.php 2 months ago class-foogallery.php 2 months ago class-gallery-advanced-settings.php 2 months ago class-il8n.php 2 months ago class-override-thumbnail.php 2 months ago class-posttypes.php 2 months ago class-previews.php 2 months ago class-retina.php 2 months ago class-thumbnail-dimensions.php 2 months ago class-thumbnails.php 2 months ago class-version-check.php 2 months ago constants.php 2 months ago functions.php 2 months ago includes.php 2 months ago index.php 2 months ago render-functions.php 2 months ago
class-previews.php
141 lines
1 <?php
2
3 /**
4 * Class to handle preview scenarios, where rest/ajax calls are made from a gallery that is in preview mode.
5 */
6 if (! class_exists('FooGallery_Previews')) {
7
8 class FooGallery_Previews
9 {
10
11 /**
12 * FooGallery_Previews constructor.
13 */
14 function __construct()
15 {
16 //override gallery settings specifically for previews.
17
18 if ($this->should_init()) {
19 $this->safe_init();
20 }
21
22 add_action('rest_api_init', function () {
23 if ($this->should_init()) {
24 $this->safe_init();
25 }
26 });
27 }
28
29 /**
30 * Check if the preview functionality should be initialized.
31 */
32 private function should_init()
33 {
34 //first, check if we are in the admin
35 if (is_admin()) {
36 return true;
37 }
38
39 //then, check if we are a rest request in admin
40 if (foogallery_is_rest_request_from_admin()) {
41 return true;
42 }
43
44 return false;
45 }
46
47 /**
48 * Safely init the preview functionality.
49 */
50 private function safe_init()
51 {
52 if (has_filter('foogallery_instance_get_setting')) {
53 return;
54 }
55
56 add_filter('foogallery_instance_get_setting', array($this, 'override_instance_get_setting_for_previews'), 10, 4);
57 add_action('foogallery_instance_after_load', array($this, 'override_gallery_properties_for_previews'), 10, 1);
58 add_action('foogallery_preview_before_render', array($this, 'store_preview_data'), 10, 2);
59 }
60
61 /**
62 * Temporarily store the preview data, so it can be picked up by AJAX / Rest requests.
63 */
64 public function store_preview_data($foogallery_id, $args)
65 {
66 //store transient for 5 minutes
67 set_transient('foogallery_preview_data_' . $foogallery_id, $args, 60 * 5);
68 }
69
70 /**
71 * Override the gallery settings for previews.
72 */
73 public function override_instance_get_setting_for_previews($value, $key, $default, $gallery)
74 {
75 if (!isset($gallery) || !is_a($gallery, 'FooGallery')) {
76 return $value;
77 }
78
79 $temp_preview_data = $this->get_preview_data($gallery);
80 if (!is_array($temp_preview_data) || empty($temp_preview_data)) {
81 return $value;
82 }
83
84 if (isset($temp_preview_data[$key])) {
85 return $temp_preview_data[$key];
86 }
87
88 return $value;
89 }
90
91 /**
92 * Override gallery properties for previews.
93 *
94 * @param FooGallery $gallery The gallery instance being loaded.
95 */
96 public function override_gallery_properties_for_previews($gallery)
97 {
98 if (!isset($gallery) || !is_a($gallery, 'FooGallery')) {
99 return;
100 }
101
102 $temp_preview_data = $this->get_preview_data($gallery);
103 if (!is_array($temp_preview_data) || empty($temp_preview_data)) {
104 return;
105 }
106
107 if (array_key_exists('sort', $temp_preview_data)) {
108 $sort = sanitize_text_field($temp_preview_data['sort']);
109 $sort_options = foogallery_sorting_options();
110
111 if (array_key_exists($sort, $sort_options)) {
112 $gallery->sorting = $sort;
113 }
114 }
115 }
116
117 /**
118 * Get stored preview data for a gallery.
119 *
120 * @param FooGallery $gallery The gallery instance being loaded.
121 *
122 * @return array|false
123 */
124 private function get_preview_data($gallery)
125 {
126 $gallery_id = intval($gallery->ID);
127 if ($gallery_id === 0) {
128 return false;
129 }
130
131 $temp_preview_data = get_transient('foogallery_preview_data_' . $gallery_id);
132
133 if (!is_array($temp_preview_data) || empty($temp_preview_data)) {
134 return false;
135 }
136
137 return $temp_preview_data;
138 }
139 }
140 }
141