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 / admin / class-gallery-datasources.php
foogallery / includes / admin Last commit date
class-admin-notice-custom-css.php 2 months ago class-admin-notices.php 2 months ago class-admin.php 2 months ago class-attachment-fields.php 2 months ago class-columns.php 2 months ago class-demo-content.php 2 months ago class-extensions.php 2 months ago class-gallery-attachment-modal.php 2 months ago class-gallery-datasources.php 2 months ago class-gallery-editor.php 2 months ago class-gallery-metabox-fields.php 2 months ago class-gallery-metabox-items.php 2 months ago class-gallery-metabox-settings-helper.php 2 months ago class-gallery-metabox-settings.php 2 months ago class-gallery-metabox-template.php 2 months ago class-gallery-metaboxes.php 2 months ago class-menu.php 2 months ago class-pro-promotion.php 2 months ago class-settings.php 2 months ago class-silent-installer-skin.php 2 months ago class-trial-mode.php 2 months ago demo-content-galleries.php 2 months ago demo-content-images.php 2 months ago index.php 2 months ago pro-features.php 2 months ago view-features.php 2 months ago view-help-demos.php 2 months ago view-help-getting-started.php 2 months ago view-help-pro.php 2 months ago view-help.php 2 months ago view-system-info.php 2 months ago
class-gallery-datasources.php
305 lines
1 <?php
2 /**
3 * Class to handle all interactions for Gallery datasources
4 */
5
6 if ( ! class_exists( 'FooGallery_Admin_Gallery_Datasources' ) ) {
7
8 class FooGallery_Admin_Gallery_Datasources {
9
10 /**
11 * Primary class constructor.
12 */
13 public function __construct() {
14 //render the datasource modal
15 add_action( 'foogallery_admin_enqueue_scripts', array( $this, 'enqueue_scripts_and_styles' ) );
16 add_action( 'admin_footer', array( $this, 'render_datasource_modal' ) );
17 add_action( 'foogallery_gallery_metabox_items', array( $this, 'add_datasources_hidden_inputs' ) );
18 add_action( 'foogallery_gallery_metabox_items_add', array( $this, 'add_datasources_button' ) );
19 add_action( 'wp_ajax_foogallery_load_datasource_content', array( $this, 'ajax_load_datasource_content' ) );
20 add_action( 'foogallery_before_save_gallery', array( $this, 'save_gallery_datasource' ), 8, 2 );
21 add_filter( 'foogallery_preview_arguments', array( $this, 'include_datasource_in_preview' ), 10, 3 );
22 add_filter( 'foogallery_render_template_argument_overrides', array( $this, 'override_datasource_arguments' ), 10, 2 );
23 add_action( 'foogallery_admin_datasource_modal_content', array( $this, 'render_datasource_modal_default_content' ) );
24 }
25
26 public function enqueue_scripts_and_styles( $hook ) {
27 wp_enqueue_style( 'foogallery.admin.datasources', FOOGALLERY_URL . 'css/foogallery.admin.datasources.css', array(), FOOGALLERY_VERSION );
28 wp_enqueue_script( 'foogallery.admin.datasources', FOOGALLERY_URL . 'js/foogallery.admin.datasources.js', array( 'jquery' ), FOOGALLERY_VERSION );
29 }
30
31 /**
32 * Include the datasource arguments for previews
33 *
34 * @param $args
35 * @param $form_post
36 * @param $template
37 * @return array
38 */
39 public function include_datasource_in_preview( $args, $form_post, $template ) {
40 if ( isset( $form_post['foogallery_datasource'] ) ) {
41 $args['datasource'] = $form_post['foogallery_datasource'];
42 }
43 if ( isset( $form_post['foogallery_datasource_value'] ) ) {
44 $args['datasource_value'] = $form_post['foogallery_datasource_value'];
45 }
46
47 return $args;
48 }
49
50 /**
51 * Allow the gallery to render using an override for the datasource
52 * @param $foogallery
53 * @param $args
54 * @return FooGallery
55 */
56 public function override_datasource_arguments( $foogallery, $args ) {
57 if ( isset( $args['datasource'] ) ) {
58 $foogallery->datasource_name = $args['datasource'];
59 }
60 if ( isset( $args['datasource_value'] ) ) {
61 $foogallery->datasource_value = $this->get_json_datasource_value( $args['datasource_value'] );
62 }
63
64 return $foogallery;
65 }
66
67 /**
68 * Save the datasource name and value for the gallery
69 * @param $post_id
70 * @param $_post
71 */
72 public function save_gallery_datasource( $post_id, $_post ) {
73 //action pre-save
74 do_action( 'foogallery_before_save_gallery_datasource', $post_id );
75
76 //set some defaults
77 $datasource = '';
78 $datasource_value = array();
79
80 if ( isset( $_POST[FOOGALLERY_META_DATASOURCE] ) ) {
81 $datasource = $this->normalize_datasource_name( wp_unslash( $_POST[FOOGALLERY_META_DATASOURCE] ) );
82
83 update_post_meta( $post_id, FOOGALLERY_META_DATASOURCE, $datasource );
84
85 if ( $datasource === foogallery_default_datasource() ) {
86 delete_post_meta( $post_id, FOOGALLERY_META_DATASOURCE_VALUE );
87 } elseif ( isset( $_POST[FOOGALLERY_META_DATASOURCE_VALUE] ) ) {
88 $datasource_value = $this->get_json_datasource_value( $_POST[FOOGALLERY_META_DATASOURCE_VALUE] );
89
90 if ( !empty( $datasource_value ) ) {
91 update_post_meta( $post_id, FOOGALLERY_META_DATASOURCE_VALUE, $datasource_value );
92 } else {
93 delete_post_meta( $post_id, FOOGALLERY_META_DATASOURCE_VALUE );
94 }
95 }
96
97 } else {
98 delete_post_meta( $post_id, FOOGALLERY_META_DATASOURCE );
99 }
100
101 //action for post-save
102 do_action( 'foogallery_after_save_gallery_datasource', $post_id, $datasource, $datasource_value );
103 }
104
105 private function normalize_datasource_name( $datasource_name ) {
106 if ( ! is_scalar( $datasource_name ) ) {
107 return foogallery_default_datasource();
108 }
109
110 $datasource_name = sanitize_key( (string) $datasource_name );
111 $datasources = foogallery_gallery_datasources();
112
113 if ( is_array( $datasources ) && array_key_exists( $datasource_name, $datasources ) ) {
114 return $datasource_name;
115 }
116
117 $recovered_datasource_name = str_replace( '-', '_', $datasource_name );
118 if ( is_array( $datasources ) && array_key_exists( $recovered_datasource_name, $datasources ) ) {
119 return $recovered_datasource_name;
120 }
121
122 return foogallery_default_datasource();
123 }
124
125 /**
126 * Safely returns an array from the json string
127 * @param $datasource_value_string
128 * @return mixed
129 */
130 public function get_json_datasource_value( $datasource_value_string ) {
131 $datasource_value = array();
132
133 //check if the value is JSON and convert to object if needed
134 if ( is_string($datasource_value_string) && is_array( json_decode( stripslashes( $datasource_value_string ), true ) ) ) {
135 $datasource_value = json_decode( stripslashes( $datasource_value_string ), true );
136 }
137 return $datasource_value;
138 }
139
140 /**
141 * Outputs the modal content for the specific datasource
142 */
143 public function ajax_load_datasource_content() {
144 if ( ! check_ajax_referer( 'foogallery-datasource-content', 'nonce', false ) ) {
145 wp_send_json_error(
146 array( 'message' => __( 'Invalid security token.', 'foogallery' ) ),
147 403
148 );
149 }
150
151 $post_data = wp_unslash( $_POST );
152 $datasource = isset( $post_data['datasource'] ) ? sanitize_key( $post_data['datasource'] ) : '';
153 $datasource_value = isset( $post_data['datasource_value'] ) ? $this->get_json_datasource_value( $post_data['datasource_value'] ) : array();
154 $foogallery_id = isset( $post_data['foogallery_id'] ) ? absint( $post_data['foogallery_id'] ) : 0;
155
156 if ( ! $foogallery_id ) {
157 wp_send_json_error(
158 array( 'message' => __( 'Invalid gallery ID.', 'foogallery' ) ),
159 400
160 );
161 }
162
163 $gallery = FooGallery::get_by_id( $foogallery_id );
164 if ( ! $gallery ) {
165 wp_send_json_error(
166 array( 'message' => __( 'Gallery not found.', 'foogallery' ) ),
167 404
168 );
169 }
170
171 if ( ! current_user_can( 'edit_post', $foogallery_id ) ) {
172 wp_send_json_error(
173 array( 'message' => __( 'Insufficient permissions.', 'foogallery' ) ),
174 403
175 );
176 }
177
178 $datasources = foogallery_gallery_datasources();
179 if ( empty( $datasource ) || ! is_array( $datasources ) || ! array_key_exists( $datasource, $datasources ) ) {
180 wp_send_json_error(
181 array( 'message' => __( 'Invalid datasource.', 'foogallery' ) ),
182 400
183 );
184 }
185
186 do_action( 'foogallery-datasource-modal-content_' . $datasource, $foogallery_id, $datasource_value );
187
188 wp_die();
189 }
190
191 /**
192 * Adds the datasource hidden inputs to the page
193 * @param FooGallery $gallery
194 */
195 public function add_datasources_hidden_inputs( $gallery ) {
196 $datasources = foogallery_gallery_datasources();
197 if ( count( $datasources ) > 1 ) {
198 $datasource_value = isset( $gallery->datasource_value ) ?
199 $gallery->datasource_value :
200 get_post_meta( $gallery->ID, FOOGALLERY_META_DATASOURCE_VALUE, true );
201 if ( is_array( $datasource_value ) ) {
202 $datasource_value = json_encode( $datasource_value );
203 } ?>
204 <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 ); ?>" />
205 <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 ); ?>" />
206 <?php }
207 }
208
209 /**
210 * Add the datasources button to the items metabox
211 */
212 public function add_datasources_button() {
213 $datasources = foogallery_gallery_datasources();
214 //we only want to show the datasources button if there are more than 1 datasources
215 if ( count( $datasources ) > 1 ) { ?>
216 <button type="button" class="button button-secondary button-hero gallery_datasources_button">
217 <span class="dashicons dashicons-format-gallery"></span>
218 <span class="foogallery-add-button-label"><?php esc_html_e( 'Add From Another Source', 'foogallery' ); ?></span>
219 </button>
220 <?php }
221 }
222
223 /**
224 * Renders the datasource modal for use on the gallery edit page
225 */
226 public function render_datasource_modal() {
227
228 global $post;
229
230 //check if the gallery edit page is being shown
231 $screen = get_current_screen();
232 if ( 'foogallery' !== $screen->id ) {
233 return;
234 }
235
236 $datasources = foogallery_gallery_datasources();
237
238 ?>
239 <?php wp_nonce_field('foogallery_load_galleries', 'foogallery_load_galleries', false); ?>
240 <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;">
241 <div class="media-modal wp-core-ui">
242 <button type="button" class="media-modal-close">
243 <span class="media-modal-icon"><span class="screen-reader-text">Close media panel</span></span>
244 </button>
245 <div class="media-modal-content">
246 <div class="media-frame wp-core-ui">
247 <div class="foogallery-modal-title foogallery-datasource-modal-title">
248 <h1>
249 <?php esc_html_e('Add To Gallery From Another Source', 'foogallery'); ?>
250 <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>
251 </h1>
252 </div>
253 <div class="foogallery-datasource-modal-sidebar">
254 <div class="foogallery-datasource-modal-sidebar-menu">
255 <?php foreach ( $datasources as $key=>$datasource ) {
256 if ( $datasource['public'] ) { ?>
257 <a href="#" class="media-menu-item foogallery-datasource-modal-selector" data-datasource="<?php echo esc_attr( $key ); ?>"><?php echo esc_html( $datasource['menu'] ); ?></a>
258 <?php } } ?>
259 </div>
260 </div>
261 <div class="foogallery-modal-container foogallery-datasource-modal-container">
262 <div class="foogallery-datasource-modal-container-inner">
263 <?php do_action( 'foogallery_admin_datasource_modal_content' ); ?>
264 </div>
265 <?php foreach ( $datasources as $key=>$datasource ) {
266 if ( $datasource['public'] ) { ?>
267 <div class="foogallery-datasource-modal-container-inner <?php echo esc_attr( $key ); ?> not-loaded">
268 <div class="spinner"></div>
269 </div>
270 <?php } } ?>
271 </div>
272 <div class="foogallery-modal-toolbar foogallery-datasource-modal-toolbar">
273 <div class="foogallery-datasource-modal-toolbar-inner">
274 <div class="media-toolbar-secondary">
275 <a href="#"
276 class="foogallery-datasource-modal-cancel button media-button button-large button-secondary media-button-insert"
277 title="<?php esc_attr_e('Cancel', 'foogallery'); ?>"><?php esc_html_e('Cancel', 'foogallery'); ?>
278 </a>
279 <p><?php esc_html_e( '* Denotes a 3rd Party Plugin by another company.', 'foogallery' ); ?></p>
280 </div>
281 <div class="media-toolbar-primary">
282 <a href="#"
283 class="foogallery-datasource-modal-insert button media-button button-large button-primary media-button-insert"
284 disabled="disabled"
285 title="<?php esc_attr_e('OK', 'foogallery'); ?>"><?php esc_html_e('OK', 'foogallery'); ?></a>
286 </div>
287 </div>
288 </div>
289 </div>
290 </div>
291 </div>
292 <div class="media-modal-backdrop"></div>
293 </div>
294 <?php
295 }
296
297 function render_datasource_modal_default_content() {
298 ?>
299 <?php esc_html_e('Select a source on the left to get started.', 'foogallery'); ?>
300 <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>
301 <?php
302 }
303 }
304 }
305