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 / admin / class-gallery-datasources.php
foogallery / includes / admin Last commit date
class-admin-notice-custom-css.php 1 week ago class-admin-notices.php 1 week ago class-admin.php 1 week ago class-attachment-fields.php 1 week ago class-columns.php 1 week ago class-demo-content.php 1 week ago class-extensions.php 1 week ago class-gallery-attachment-modal.php 1 week ago class-gallery-datasources.php 1 week ago class-gallery-editor.php 1 week ago class-gallery-metabox-fields.php 1 week ago class-gallery-metabox-items.php 1 week ago class-gallery-metabox-settings-helper.php 1 week ago class-gallery-metabox-settings.php 1 week ago class-gallery-metabox-template.php 1 week ago class-gallery-metaboxes.php 1 week ago class-menu.php 1 week ago class-pro-promotion.php 1 week ago class-settings.php 1 week ago class-silent-installer-skin.php 1 week ago class-trial-mode.php 1 week ago demo-content-galleries.php 1 week ago demo-content-images.php 1 week ago index.php 1 week ago pro-features.php 1 week ago view-features.php 1 week ago view-help-demos.php 1 week ago view-help-getting-started.php 1 week ago view-help-pro.php 1 week ago view-help.php 1 week ago view-system-info.php 1 week ago
class-gallery-datasources.php
398 lines
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) {
4 exit;
5 }
6
7 /**
8 * Class to handle all interactions for Gallery datasources
9 */
10
11 if ( ! class_exists( 'FooGallery_Admin_Gallery_Datasources' ) ) {
12
13 class FooGallery_Admin_Gallery_Datasources {
14
15 const RECOVERY_DATASOURCE_FIELD = '_foogallery_datasource_recovery_original';
16 const RECOVERY_VALUE_FIELD = '_foogallery_datasource_recovery_value';
17
18 /**
19 * Primary class constructor.
20 */
21 public function __construct() {
22 //render the datasource modal
23 add_action( 'foogallery_admin_enqueue_scripts', array( $this, 'enqueue_scripts_and_styles' ) );
24 add_action( 'admin_footer', array( $this, 'render_datasource_modal' ) );
25 add_action( 'foogallery_gallery_metabox_items', array( $this, 'add_datasources_hidden_inputs' ) );
26 add_action( 'foogallery_gallery_metabox_items_add', array( $this, 'add_datasources_button' ) );
27 add_action( 'wp_ajax_foogallery_load_datasource_content', array( $this, 'ajax_load_datasource_content' ) );
28 add_action( 'foogallery_before_save_gallery', array( $this, 'save_gallery_datasource' ), 8, 2 );
29 add_filter( 'foogallery_preview_arguments', array( $this, 'include_datasource_in_preview' ), 10, 3 );
30 add_filter( 'foogallery_render_template_argument_overrides', array( $this, 'override_datasource_arguments' ), 10, 2 );
31 add_action( 'foogallery_admin_datasource_modal_content', array( $this, 'render_datasource_modal_default_content' ) );
32 }
33
34 public function enqueue_scripts_and_styles( $hook ) {
35 wp_enqueue_style( 'foogallery.admin.datasources', FOOGALLERY_URL . 'css/foogallery.admin.datasources.css', array(), FOOGALLERY_VERSION );
36 wp_enqueue_script( 'foogallery.admin.datasources', FOOGALLERY_URL . 'js/foogallery.admin.datasources.js', array( 'jquery' ), FOOGALLERY_VERSION );
37 }
38
39 /**
40 * Include the datasource arguments for previews
41 *
42 * @param $args
43 * @param $form_post
44 * @param $template
45 * @return array
46 */
47 public function include_datasource_in_preview( $args, $form_post, $template ) {
48 if ( isset( $form_post['foogallery_datasource'] ) ) {
49 $args['datasource'] = $form_post['foogallery_datasource'];
50 }
51 if ( isset( $form_post['foogallery_datasource_value'] ) ) {
52 $args['datasource_value'] = $form_post['foogallery_datasource_value'];
53 }
54
55 return $args;
56 }
57
58 /**
59 * Allow the gallery to render using an override for the datasource
60 * @param $foogallery
61 * @param $args
62 * @return FooGallery
63 */
64 public function override_datasource_arguments( $foogallery, $args ) {
65 if ( isset( $args['datasource'] ) ) {
66 $foogallery->datasource_name = $args['datasource'];
67 }
68 if ( isset( $args['datasource_value'] ) ) {
69 $foogallery->datasource_value = $this->get_json_datasource_value( $args['datasource_value'] );
70 }
71
72 return $foogallery;
73 }
74
75 /**
76 * Save the datasource name and value for the gallery
77 * @param $post_id
78 * @param $_post
79 */
80 public function save_gallery_datasource( $post_id, $_post ) {
81 //action pre-save
82 do_action( 'foogallery_before_save_gallery_datasource', $post_id );
83
84 //set some defaults
85 $datasource = '';
86 $datasource_value = array();
87
88 if ( isset( $_POST[FOOGALLERY_META_DATASOURCE] ) ) {
89 $submitted_datasource = wp_unslash( $_POST[FOOGALLERY_META_DATASOURCE] );
90 $stored_datasource = get_post_meta( $post_id, FOOGALLERY_META_DATASOURCE, true );
91
92 if ( $this->should_preserve_unavailable_datasource( $stored_datasource, $submitted_datasource ) ) {
93 $datasource = $stored_datasource;
94 $datasource_value = get_post_meta( $post_id, FOOGALLERY_META_DATASOURCE_VALUE, true );
95 do_action( 'foogallery_after_save_gallery_datasource', $post_id, $datasource, $datasource_value );
96 return;
97 }
98
99 $datasource = $this->normalize_datasource_name( $submitted_datasource );
100
101 update_post_meta( $post_id, FOOGALLERY_META_DATASOURCE, $datasource );
102
103 if ( $datasource === foogallery_default_datasource() ) {
104 delete_post_meta( $post_id, FOOGALLERY_META_DATASOURCE_VALUE );
105 } elseif ( $this->should_preserve_invalid_datasource_value( $stored_datasource, $datasource ) ) {
106 $datasource_value = get_post_meta( $post_id, FOOGALLERY_META_DATASOURCE_VALUE, true );
107 } elseif ( isset( $_POST[FOOGALLERY_META_DATASOURCE_VALUE] ) ) {
108 $datasource_value = $this->get_json_datasource_value( $_POST[FOOGALLERY_META_DATASOURCE_VALUE] );
109
110 if ( !empty( $datasource_value ) ) {
111 update_post_meta( $post_id, FOOGALLERY_META_DATASOURCE_VALUE, $datasource_value );
112 } else {
113 delete_post_meta( $post_id, FOOGALLERY_META_DATASOURCE_VALUE );
114 }
115 }
116
117 } else {
118 $stored_datasource = get_post_meta( $post_id, FOOGALLERY_META_DATASOURCE, true );
119 if ( ! $this->datasource_is_unavailable( $stored_datasource ) ) {
120 delete_post_meta( $post_id, FOOGALLERY_META_DATASOURCE );
121 } else {
122 $datasource = $stored_datasource;
123 $datasource_value = get_post_meta( $post_id, FOOGALLERY_META_DATASOURCE_VALUE, true );
124 }
125 }
126
127 //action for post-save
128 do_action( 'foogallery_after_save_gallery_datasource', $post_id, $datasource, $datasource_value );
129 }
130
131 /**
132 * Check whether an unavailable datasource must survive an unrelated gallery save.
133 *
134 * @param mixed $stored_datasource The stored datasource name.
135 * @param mixed $submitted_datasource The submitted fallback datasource name.
136 *
137 * @return bool
138 */
139 private function should_preserve_unavailable_datasource( $stored_datasource, $submitted_datasource ) {
140 if ( ! isset( $_POST[ self::RECOVERY_DATASOURCE_FIELD ] ) || ! is_scalar( $_POST[ self::RECOVERY_DATASOURCE_FIELD ] ) ) {
141 return false;
142 }
143
144 $recovery_datasource = trim( (string) wp_unslash( $_POST[ self::RECOVERY_DATASOURCE_FIELD ] ) );
145 $stored_datasource = is_scalar( $stored_datasource ) ? trim( (string) $stored_datasource ) : '';
146
147 if ( '' === $stored_datasource || $stored_datasource !== $recovery_datasource || ! $this->datasource_is_unavailable( $stored_datasource ) ) {
148 return false;
149 }
150
151 return foogallery_default_datasource() === $this->normalize_datasource_name( $submitted_datasource );
152 }
153
154 /**
155 * Check whether malformed settings should survive until the user explicitly replaces them.
156 *
157 * @param mixed $stored_datasource The stored datasource name.
158 * @param string $datasource The normalized submitted datasource name.
159 *
160 * @return bool
161 */
162 private function should_preserve_invalid_datasource_value( $stored_datasource, $datasource ) {
163 if ( empty( $_POST[ self::RECOVERY_VALUE_FIELD ] ) ) {
164 return false;
165 }
166
167 $stored_datasource = is_scalar( $stored_datasource ) ? sanitize_key( (string) $stored_datasource ) : '';
168
169 return '' !== $stored_datasource && $stored_datasource === $datasource;
170 }
171
172 /**
173 * Check whether a stored datasource belongs to a currently unavailable add-on.
174 *
175 * @param mixed $datasource_name The stored datasource name.
176 *
177 * @return bool
178 */
179 private function datasource_is_unavailable( $datasource_name ) {
180 if ( ! is_scalar( $datasource_name ) ) {
181 return false;
182 }
183
184 $datasource_name = trim( (string) $datasource_name );
185 if ( '' === $datasource_name || foogallery_default_datasource() !== $this->normalize_datasource_name( $datasource_name ) ) {
186 return false;
187 }
188
189 return foogallery_default_datasource() !== sanitize_key( str_replace( '-', '_', $datasource_name ) );
190 }
191
192 private function normalize_datasource_name( $datasource_name ) {
193 if ( ! is_scalar( $datasource_name ) ) {
194 return foogallery_default_datasource();
195 }
196
197 $datasource_name = sanitize_key( (string) $datasource_name );
198 $datasources = foogallery_gallery_datasources();
199
200 if ( is_array( $datasources ) && array_key_exists( $datasource_name, $datasources ) ) {
201 return $datasource_name;
202 }
203
204 $recovered_datasource_name = str_replace( '-', '_', $datasource_name );
205 if ( is_array( $datasources ) && array_key_exists( $recovered_datasource_name, $datasources ) ) {
206 return $recovered_datasource_name;
207 }
208
209 return foogallery_default_datasource();
210 }
211
212 /**
213 * Safely returns an array from the json string
214 * @param $datasource_value_string
215 * @return mixed
216 */
217 public function get_json_datasource_value( $datasource_value_string ) {
218 $datasource_value = array();
219
220 //check if the value is JSON and convert to object if needed
221 if ( is_string($datasource_value_string) && is_array( json_decode( stripslashes( $datasource_value_string ), true ) ) ) {
222 $datasource_value = json_decode( stripslashes( $datasource_value_string ), true );
223 }
224 return $datasource_value;
225 }
226
227 /**
228 * Outputs the modal content for the specific datasource
229 */
230 public function ajax_load_datasource_content() {
231 if ( ! check_ajax_referer( 'foogallery-datasource-content', 'nonce', false ) ) {
232 wp_send_json_error(
233 array( 'message' => __( 'Invalid security token.', 'foogallery' ) ),
234 403
235 );
236 }
237
238 $post_data = wp_unslash( $_POST );
239 $datasource = isset( $post_data['datasource'] ) ? sanitize_key( $post_data['datasource'] ) : '';
240 $datasource_value = isset( $post_data['datasource_value'] ) ? $this->get_json_datasource_value( $post_data['datasource_value'] ) : array();
241 $foogallery_id = isset( $post_data['foogallery_id'] ) ? absint( $post_data['foogallery_id'] ) : 0;
242
243 if ( ! $foogallery_id ) {
244 wp_send_json_error(
245 array( 'message' => __( 'Invalid gallery ID.', 'foogallery' ) ),
246 400
247 );
248 }
249
250 $gallery = FooGallery::get_by_id( $foogallery_id );
251 if ( ! $gallery ) {
252 wp_send_json_error(
253 array( 'message' => __( 'Gallery not found.', 'foogallery' ) ),
254 404
255 );
256 }
257
258 if ( ! current_user_can( 'edit_post', $foogallery_id ) ) {
259 wp_send_json_error(
260 array( 'message' => __( 'Insufficient permissions.', 'foogallery' ) ),
261 403
262 );
263 }
264
265 $datasources = foogallery_gallery_datasources();
266 if ( empty( $datasource ) || ! is_array( $datasources ) || ! array_key_exists( $datasource, $datasources ) ) {
267 wp_send_json_error(
268 array( 'message' => __( 'Invalid datasource.', 'foogallery' ) ),
269 400
270 );
271 }
272
273 do_action( 'foogallery-datasource-modal-content_' . $datasource, $foogallery_id, $datasource_value );
274
275 wp_die();
276 }
277
278 /**
279 * Adds the datasource hidden inputs to the page
280 * @param FooGallery $gallery
281 */
282 public function add_datasources_hidden_inputs( $gallery ) {
283 $datasources = foogallery_gallery_datasources();
284 if ( count( $datasources ) > 1 ) {
285 $datasource_value = isset( $gallery->datasource_value ) ?
286 $gallery->datasource_value :
287 get_post_meta( $gallery->ID, FOOGALLERY_META_DATASOURCE_VALUE, true );
288 if ( is_array( $datasource_value ) ) {
289 $datasource_value = json_encode( $datasource_value );
290 } ?>
291 <input type="hidden" data-foogallery-preview="include" name="<?php echo esc_attr( FOOGALLERY_META_DATASOURCE ); ?>" value="<?php echo esc_attr( $gallery->datasource_name ); ?>" id="<?php echo esc_attr( FOOGALLERY_META_DATASOURCE ); ?>" />
292 <input type="hidden" data-foogallery-preview="include" value="<?php echo esc_attr( $datasource_value ); ?>" name="<?php echo esc_attr( FOOGALLERY_META_DATASOURCE_VALUE ); ?>" id="<?php echo esc_attr( FOOGALLERY_META_DATASOURCE_VALUE ); ?>" />
293 <?php if ( ! empty( $gallery->_foogallery_invalid_datasource_name ) ) { ?>
294 <input type="hidden" name="<?php echo esc_attr( self::RECOVERY_DATASOURCE_FIELD ); ?>" id="<?php echo esc_attr( self::RECOVERY_DATASOURCE_FIELD ); ?>" value="<?php echo esc_attr( $gallery->_foogallery_invalid_datasource_name ); ?>" />
295 <?php } ?>
296 <?php if ( ! empty( $gallery->_foogallery_invalid_datasource_value ) ) { ?>
297 <input type="hidden" name="<?php echo esc_attr( self::RECOVERY_VALUE_FIELD ); ?>" id="<?php echo esc_attr( self::RECOVERY_VALUE_FIELD ); ?>" value="1" />
298 <?php } ?>
299 <?php }
300 }
301
302 /**
303 * Add the datasources button to the items metabox
304 */
305 public function add_datasources_button() {
306 $datasources = foogallery_gallery_datasources();
307 //we only want to show the datasources button if there are more than 1 datasources
308 if ( count( $datasources ) > 1 ) { ?>
309 <button type="button" class="button button-secondary button-hero gallery_datasources_button">
310 <span class="dashicons dashicons-format-gallery"></span>
311 <span class="foogallery-add-button-label"><?php esc_html_e( 'Add From Another Source', 'foogallery' ); ?></span>
312 </button>
313 <?php }
314 }
315
316 /**
317 * Renders the datasource modal for use on the gallery edit page
318 */
319 public function render_datasource_modal() {
320
321 global $post;
322
323 //check if the gallery edit page is being shown
324 $screen = get_current_screen();
325 if ( 'foogallery' !== $screen->id ) {
326 return;
327 }
328
329 $datasources = foogallery_gallery_datasources();
330
331 ?>
332 <?php wp_nonce_field('foogallery_load_galleries', 'foogallery_load_galleries', false); ?>
333 <div class="foogallery-modal-wrapper foogallery-datasources-modal-wrapper" data-foogalleryid="<?php echo esc_attr( $post->ID ); ?>" data-nonce="<?php echo esc_attr( wp_create_nonce( 'foogallery-datasource-content' ) ); ?>" style="display: none;">
334 <div class="media-modal wp-core-ui">
335 <button type="button" class="media-modal-close">
336 <span class="media-modal-icon"><span class="screen-reader-text">Close media panel</span></span>
337 </button>
338 <div class="media-modal-content">
339 <div class="media-frame wp-core-ui">
340 <div class="foogallery-modal-title foogallery-datasource-modal-title">
341 <h1>
342 <?php esc_html_e('Add To Gallery From Another Source', 'foogallery'); ?>
343 <a class="foogallery-datasource-modal-reload button" href="#" style="display: none; margin-top: -4px;"><span style="padding-top: 3px;" class="dashicons dashicons-update"></span> <?php esc_html_e('Reload', 'foogallery'); ?></a>
344 </h1>
345 </div>
346 <div class="foogallery-datasource-modal-sidebar">
347 <div class="foogallery-datasource-modal-sidebar-menu">
348 <?php foreach ( $datasources as $key=>$datasource ) {
349 if ( $datasource['public'] ) { ?>
350 <a href="#" class="media-menu-item foogallery-datasource-modal-selector" data-datasource="<?php echo esc_attr( $key ); ?>"><?php echo esc_html( $datasource['menu'] ); ?></a>
351 <?php } } ?>
352 </div>
353 </div>
354 <div class="foogallery-modal-container foogallery-datasource-modal-container">
355 <div class="foogallery-datasource-modal-container-inner">
356 <?php do_action( 'foogallery_admin_datasource_modal_content' ); ?>
357 </div>
358 <?php foreach ( $datasources as $key=>$datasource ) {
359 if ( $datasource['public'] ) { ?>
360 <div class="foogallery-datasource-modal-container-inner <?php echo esc_attr( $key ); ?> not-loaded">
361 <div class="spinner"></div>
362 </div>
363 <?php } } ?>
364 </div>
365 <div class="foogallery-modal-toolbar foogallery-datasource-modal-toolbar">
366 <div class="foogallery-datasource-modal-toolbar-inner">
367 <div class="media-toolbar-secondary">
368 <a href="#"
369 class="foogallery-datasource-modal-cancel button media-button button-large button-secondary media-button-insert"
370 title="<?php esc_attr_e('Cancel', 'foogallery'); ?>"><?php esc_html_e('Cancel', 'foogallery'); ?>
371 </a>
372 <p><?php esc_html_e( '* Denotes a 3rd Party Plugin by another company.', 'foogallery' ); ?></p>
373 </div>
374 <div class="media-toolbar-primary">
375 <a href="#"
376 class="foogallery-datasource-modal-insert button media-button button-large button-primary media-button-insert"
377 disabled="disabled"
378 title="<?php esc_attr_e('OK', 'foogallery'); ?>"><?php esc_html_e('OK', 'foogallery'); ?></a>
379 </div>
380 </div>
381 </div>
382 </div>
383 </div>
384 </div>
385 <div class="media-modal-backdrop"></div>
386 </div>
387 <?php
388 }
389
390 function render_datasource_modal_default_content() {
391 ?>
392 <?php esc_html_e('Select a source on the left to get started.', 'foogallery'); ?>
393 <div style="height: 200px;background-repeat: no-repeat;background-size: 50%;width: 200px;background-image: url(&quot;data:image/svg+xml;base64,PHN2ZyBoZWlnaHQ9JzMwMHB4JyB3aWR0aD0nMzAwcHgnICBmaWxsPSIjMDAwMDAwIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHhtbG5zOnhsaW5rPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5L3hsaW5rIiB2ZXJzaW9uPSIxLjEiIHg9IjBweCIgeT0iMHB4IiB2aWV3Qm94PSIwIDAgMTAwIDEwMCIgc3R5bGU9ImVuYWJsZS1iYWNrZ3JvdW5kOm5ldyAwIDAgMTAwIDEwMDsiIHhtbDpzcGFjZT0icHJlc2VydmUiPjxzdHlsZSB0eXBlPSJ0ZXh0L2NzcyI+Cgkuc3Qwe2ZpbGw6IzAwMDAwMDt9Cjwvc3R5bGU+PHBhdGggY2xhc3M9InN0MCIgZD0iTTg3LjksMjMuMUM4My4yLDM0LDc1LjYsNDMuNSw2Ni4yLDUwLjZjLTkuNSw3LjItMjEuMSwxMi41LTMzLjEsMTMuNmMtNC45LDAuNC05LjksMC0xNC40LTEuOCAgYzQuNi0wLjksOS4xLTEuOCwxMy43LTIuN2MxLTAuMiwxLjItMS42LDEuMi0yLjRjMC0wLjUtMC4yLTIuNi0xLjItMi40Yy02LjUsMS4zLTEzLjEsMi42LTE5LjYsMy45Yy0wLjEsMC0wLjMsMC4xLTAuNCwwLjIgIGMtMC4yLTAuMi0wLjQtMC40LTAuNi0wLjVjLTEuNy0xLjQtMi40LDMuMi0xLjIsNC4yYzQuOSw0LjIsOS4yLDksMTIuNiwxNC40YzAuNSwwLjgsMS4yLDAuOSwxLjcsMGMwLjUtMC45LDAuNi0yLjUsMC0zLjQgIGMtMS4yLTEuOS0yLjUtMy42LTMuOC01LjRjOS4xLDIuNSwxOS41LDAuMywyOC0zYzExLjQtNC40LDIxLjYtMTEuNywyOS41LTIxYzQuNS01LjMsOC4yLTExLjIsMTEtMTcuNWMwLjQtMSwwLjUtMi40LDAtMy40ICBDODkuMiwyMi4zLDg4LjMsMjIuMiw4Ny45LDIzLjF6Ij48L3BhdGg+PC9zdmc+&quot;);"></div>
394 <?php
395 }
396 }
397 }
398