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