PluginProbe
SimpLy Gallery / trunk
SimpLy Gallery vtrunk
3.4.2 3.4.1 3.4.0 3.3.3.3 3.3.3.2 3.3.3.1 trunk 2.4.0 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 All 51 releases
simply-gallery-block / plugin.php

plugin.php in SimpLy Gallery trunk, at plugin.php

1,046 lines 49.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Plugin Name: SimpLy Gallery Block & Lightbox
5 * Plugin URI: https://simplygallery.co/
6 * Description: Simply Gallery is a mixed media gallery plugin that lets you combine images, video, and audio in a single gallery. Supports HTML5 video, YouTube, Vimeo, and VideoPress, and adds a customizable lightbox to native WordPress galleries.
7 * Author: GalleryCreator
8 * Author URI: https://blockslib.com/
9 * Version: 3.4.2
10 * Text Domain: simply-gallery-block
11 * Domain Path: /languages
12 * License: GPL2+
13 * License URI: https://www.gnu.org/licenses/gpl-2.0.txt
14 *
15 * @package SimpLy Gallery Block
16 */
17 /**
18 * Exit if accessed directly.
19 */
20 if ( !defined( 'ABSPATH' ) ) {
21 exit;
22 }
23 if ( function_exists( 'pgc_sgb_fs' ) ) {
24 pgc_sgb_fs()->set_basename( false, __FILE__ );
25 } else {
26 define( 'PGC_SGB_VERSION', '3.4.2' );
27 define( 'PGC_SGB_SLUG', 'simply-gallery-block' );
28 define( 'PGC_SGB_BLOCK_PREF', 'wp-block-pgcsimplygalleryblock-' );
29 define( 'PGC_SGB_PLUGIN_SLUG', 'pgc-simply-gallery-plugin' );
30 define( 'PGC_SGB_POST_TYPE', 'pgc_simply_gallery' );
31 define( 'PGC_SGB_TAXONOMY', 'pgc_simply_category' );
32 define( 'PGC_SGB_TAGS', 'pgc_simply_tags' );
33 define( 'PGC_SGB_PRESET', 'pgc_simply_preset' );
34 define( 'PGC_SGB_FILE', __FILE__ );
35 define( 'PGC_SGB_PATH', __DIR__ );
36 define( 'PGC_SGB_DIRNAME', basename( PGC_SGB_PATH ) );
37 $pgc_sgb_skins_list = array();
38 $pgc_sgb_skins_presets = array();
39 $pgc_sgb_global_lightbox_use = false;
40 $pgc_sgb_wc_to_sgb = null;
41 if ( !function_exists( 'pgc_sgb_fs' ) ) {
42 // Create a helper function for easy SDK access.
43 function pgc_sgb_fs() {
44 global $pgc_sgb_fs;
45 if ( !isset( $pgc_sgb_fs ) ) {
46 // Include Freemius SDK.
47 require_once dirname( __FILE__ ) . '/freemius/start.php';
48 $pgc_sgb_fs = fs_dynamic_init( array(
49 'id' => '7208',
50 'slug' => 'simply-gallery-block',
51 'type' => 'plugin',
52 'public_key' => 'pk_0e7076e3ce718684690406736d9be',
53 'is_premium' => false,
54 'premium_suffix' => 'Pro',
55 'has_addons' => false,
56 'has_paid_plans' => true,
57 'trial' => array(
58 'days' => 7,
59 'is_require_payment' => true,
60 ),
61 'menu' => array(
62 'slug' => 'edit.php?post_type=pgc_simply_gallery',
63 'first-path' => 'edit.php?post_type=pgc_simply_gallery&page=pgc-simply-welcome',
64 ),
65 'is_live' => true,
66 'is_org_compliant' => true,
67 ) );
68 }
69 return $pgc_sgb_fs;
70 }
71
72 // Init Freemius.
73 pgc_sgb_fs();
74 // Signal that SDK was initiated.
75 do_action( 'pgc_sgb_fs_loaded' );
76 }
77 function pgc_sgb_fs_uninstall_cleanup() {
78 delete_option( "pgc_sgb_global_lightbox_use" );
79 delete_site_option( 'pgc_sgb_global_lightbox_use' );
80 }
81
82 pgc_sgb_fs()->add_action( 'after_uninstall', 'pgc_sgb_fs_uninstall_cleanup' );
83 function pgc_sgb_load_textdomain() {
84 define( 'PGC_SGB_URL', plugin_dir_url( __FILE__ ) );
85 load_plugin_textdomain( 'simply-gallery-block', false, basename( PGC_SGB_URL ) . '/languages' );
86 }
87
88 add_action( 'plugins_loaded', 'pgc_sgb_load_textdomain' );
89 function pgc_sgb_create_preset( $preset_slug, $preset_data = '' ) {
90 $preset_title = str_replace( 'pgc_sgb_', '', $preset_slug );
91 $post_id = wp_insert_post( array(
92 'post_type' => PGC_SGB_PRESET,
93 'post_status' => 'publish',
94 'post_title' => ucfirst( $preset_title ) . ' Preset',
95 ) );
96 if ( $post_id && !is_wp_error( $post_id ) ) {
97 update_post_meta( $post_id, '_pgc_preset_slug', $preset_slug );
98 update_post_meta( $post_id, '_pgc_preset_data', $preset_data );
99 }
100 return $post_id;
101 }
102
103 function pgc_sgb_migrate_presets() {
104 $skins_folders = glob( realpath( dirname( __FILE__ ) ) . '/blocks/skins/*.js' );
105 $presets_to_migrate = array(
106 'lightbox' => 'pgc_sgb_lightbox',
107 );
108 foreach ( $skins_folders as $file ) {
109 $fileName = substr( $file, strrpos( $file, "/" ) + 1 );
110 $skinSlug = substr( $fileName, 0, -3 );
111 $preset_title = str_replace( 'pgc_sgb_', '', $skinSlug );
112 $presets_to_migrate[$preset_title] = $skinSlug;
113 }
114 foreach ( $presets_to_migrate as $preset_slug => $option_key ) {
115 $preset_data = get_option( $option_key );
116 if ( !$preset_data ) {
117 continue;
118 }
119 pgc_sgb_create_preset( $option_key, $preset_data );
120 }
121 }
122
123 function pgc_sgb_get_preset_by_slug( $slug, $get_preset_id = false ) {
124 $posts = get_posts( [
125 'post_type' => PGC_SGB_PRESET,
126 'meta_key' => '_pgc_preset_slug',
127 'meta_value' => $slug,
128 'numberposts' => 1,
129 'fields' => 'ids',
130 ] );
131 if ( empty( $posts ) ) {
132 return null;
133 }
134 $preset_id = $posts[0];
135 if ( $get_preset_id ) {
136 return $preset_id;
137 }
138 $data = get_post_meta( $preset_id, '_pgc_preset_data', true );
139 return $data;
140 }
141
142 function pgc_sgb_update_preset( $slug, $preset_data ) {
143 $preset_id = pgc_sgb_get_preset_by_slug( $slug, true );
144 if ( !$preset_id ) {
145 $preset_id = pgc_sgb_create_preset( $slug, $preset_data );
146 if ( $preset_id && !is_wp_error( $preset_id ) ) {
147 return true;
148 }
149 return false;
150 }
151 return update_post_meta( $preset_id, '_pgc_preset_data', $preset_data );
152 }
153
154 function pgc_sgb_getGlobalPresets() {
155 global $pgc_sgb_skins_list, $pgc_sgb_skins_presets;
156 $skins_folders = glob( realpath( dirname( __FILE__ ) ) . '/blocks/skins/*.js' );
157 foreach ( $skins_folders as $file ) {
158 $fileName = substr( $file, strrpos( $file, "/" ) + 1 );
159 $skinSlug = substr( $fileName, 0, -3 );
160 $pgc_sgb_skins_list[$skinSlug] = PGC_SGB_URL . 'blocks/skins/' . $fileName . '?ver=' . PGC_SGB_VERSION;
161 $pgc_sgb_skins_presets[$skinSlug] = pgc_sgb_get_preset_by_slug( $skinSlug );
162 }
163 }
164
165 add_action( 'init', 'pgc_sgb_getGlobalPresets', 1 );
166 function pgc_sgb_prepare_attachment_post_for_sgb( $attachment ) {
167 if ( !$attachment ) {
168 return;
169 }
170 if ( 'attachment' !== $attachment->post_type ) {
171 return;
172 }
173 $meta = wp_get_attachment_metadata( $attachment->ID );
174 if ( false !== strpos( $attachment->post_mime_type, '/' ) ) {
175 list( $type, $subtype ) = explode( '/', $attachment->post_mime_type );
176 } else {
177 list( $type, $subtype ) = array($attachment->post_mime_type, '');
178 }
179 $attachment_url = wp_get_attachment_url( $attachment->ID );
180 $base_url = str_replace( wp_basename( $attachment_url ), '', $attachment_url );
181 $response = array(
182 'id' => $attachment->ID,
183 'title' => $attachment->post_title,
184 'filename' => wp_basename( get_attached_file( $attachment->ID ) ),
185 'url' => $attachment_url,
186 'link' => get_attachment_link( $attachment->ID ),
187 'alt' => get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ),
188 'description' => $attachment->post_content,
189 'caption' => $attachment->post_excerpt,
190 'date' => strtotime( $attachment->post_date_gmt ) * 1000,
191 'mime' => $attachment->post_mime_type,
192 'type' => $type,
193 'subtype' => $subtype,
194 'meta' => false,
195 );
196 if ( $meta && ('image' === $type || !empty( $meta['image_meta'] )) ) {
197 $response['imageMeta'] = $meta['image_meta'];
198 }
199 if ( $meta && ('image' === $type || !empty( $meta['sizes'] )) ) {
200 $sizes = array();
201 $possible_sizes = apply_filters( 'image_size_names_choose', array(
202 'thumbnail' => __( 'Thumbnail', 'simply-gallery-block' ),
203 'medium' => __( 'Medium', 'simply-gallery-block' ),
204 'large' => __( 'Large', 'simply-gallery-block' ),
205 'full' => __( 'Full Size', 'simply-gallery-block' ),
206 ) );
207 unset($possible_sizes['full']);
208 foreach ( $possible_sizes as $size => $label ) {
209 $downsize = apply_filters(
210 'image_downsize',
211 false,
212 $attachment->ID,
213 $size
214 );
215 if ( $downsize ) {
216 if ( empty( $downsize[3] ) ) {
217 continue;
218 }
219 $sizes[$size] = array(
220 'height' => $downsize[2],
221 'width' => $downsize[1],
222 'url' => $downsize[0],
223 'orientation' => ( $downsize[2] > $downsize[1] ? 'portrait' : 'landscape' ),
224 );
225 } elseif ( isset( $meta['sizes'][$size] ) ) {
226 $size_meta = $meta['sizes'][$size];
227 $height = ( isset( $size_meta['height'] ) ? $size_meta['height'] : 300 );
228 $width = ( isset( $size_meta['width'] ) ? $size_meta['width'] : 300 );
229 $sizes[$size] = array(
230 'height' => $height,
231 'width' => $width,
232 'url' => $base_url . $size_meta['file'],
233 'orientation' => ( $height > $width ? 'portrait' : 'landscape' ),
234 );
235 }
236 }
237 if ( 'image' === $type ) {
238 if ( !empty( $meta['original_image'] ) ) {
239 $response['originalImageURL'] = wp_get_original_image_url( $attachment->ID );
240 $response['originalImageName'] = wp_basename( wp_get_original_image_path( $attachment->ID ) );
241 }
242 $sizes['full'] = array(
243 'url' => $attachment_url,
244 );
245 if ( isset( $meta['height'], $meta['width'] ) ) {
246 $sizes['full']['height'] = $meta['height'];
247 $sizes['full']['width'] = $meta['width'];
248 $sizes['full']['orientation'] = ( $meta['height'] > $meta['width'] ? 'portrait' : 'landscape' );
249 }
250 $response = array_merge( $response, $sizes['full'] );
251 } elseif ( $meta['sizes']['full']['file'] ) {
252 $sizes['full'] = array(
253 'url' => $base_url . $meta['sizes']['full']['file'],
254 'height' => $meta['sizes']['full']['height'],
255 'width' => $meta['sizes']['full']['width'],
256 'orientation' => ( $meta['sizes']['full']['height'] > $meta['sizes']['full']['width'] ? 'portrait' : 'landscape' ),
257 );
258 }
259 $response = array_merge( $response, array(
260 'sizes' => $sizes,
261 ) );
262 }
263 if ( $meta && 'video' === $type ) {
264 if ( isset( $meta['width'] ) ) {
265 $response['width'] = (int) $meta['width'];
266 }
267 if ( isset( $meta['height'] ) ) {
268 $response['height'] = (int) $meta['height'];
269 }
270 }
271 if ( $meta && ('audio' === $type || 'video' === $type) ) {
272 if ( isset( $meta['length_formatted'] ) ) {
273 $response['fileLength'] = $meta['length_formatted'];
274 $response['fileLengthHumanReadable'] = human_readable_duration( $meta['length_formatted'] );
275 }
276 $response['meta'] = array();
277 foreach ( wp_get_attachment_id3_keys( $attachment, 'js' ) as $key => $label ) {
278 $response['meta'][$key] = false;
279 if ( !empty( $meta[$key] ) ) {
280 $response['meta'][$key] = $meta[$key];
281 }
282 }
283 $id = get_post_thumbnail_id( $attachment->ID );
284 if ( !empty( $id ) ) {
285 list( $src, $width, $height ) = wp_get_attachment_image_src( $id, 'full' );
286 $response['image'] = compact( 'src', 'width', 'height' );
287 list( $src, $width, $height ) = wp_get_attachment_image_src( $id, 'thumbnail' );
288 $response['thumb'] = compact( 'src', 'width', 'height' );
289 } else {
290 $src = wp_mime_type_icon( $attachment->ID );
291 $width = 48;
292 $height = 64;
293 $response['image'] = compact( 'src', 'width', 'height' );
294 $response['thumb'] = compact( 'src', 'width', 'height' );
295 }
296 }
297 return $response;
298 }
299
300 function pgc_sgb_woocommerce_helper() {
301 global $pgc_sgb_wc_to_sgb;
302 }
303
304 add_action( 'init', 'pgc_sgb_woocommerce_helper' );
305 /** Frontend Script and Style */
306 function pgc_sgb_menager_script() {
307 global $pgc_sgb_skins_list, $pgc_sgb_skins_presets;
308 /** Block style CSS. */
309 wp_register_style(
310 PGC_SGB_SLUG . '-frontend',
311 PGC_SGB_URL . 'blocks/pgc_sgb.min.style.css',
312 array(),
313 PGC_SGB_VERSION
314 );
315 /** Parser */
316 wp_register_script(
317 PGC_SGB_SLUG . '-script',
318 PGC_SGB_URL . 'blocks/pgc_sgb.min.js',
319 array(),
320 PGC_SGB_VERSION,
321 true
322 );
323 if ( is_array( $pgc_sgb_skins_list ) ) {
324 foreach ( $pgc_sgb_skins_list as $skin_key => $skin_url ) {
325 $pgc_sgb_skins_list[$skin_key] = esc_url_raw( (string) $skin_url );
326 }
327 }
328 $globalJS = array(
329 'assets' => PGC_SGB_URL . 'assets/',
330 'skinsFolder' => PGC_SGB_URL . 'blocks/skins/',
331 'skinsList' => $pgc_sgb_skins_list,
332 'wpApiRoot' => esc_url_raw( rest_url() ),
333 'postType' => PGC_SGB_POST_TYPE,
334 'skinsSettings' => $pgc_sgb_skins_presets,
335 );
336 wp_localize_script( PGC_SGB_SLUG . '-script', 'PGC_SGB', $globalJS );
337 }
338
339 add_action( 'wp_enqueue_scripts', 'pgc_sgb_menager_script' );
340 function pgc_sgb_tags_migrate() {
341 /** Tags to Terms 3.3.1 */
342 $old_tags = get_option( 'pgc_sgb_tags_list' );
343 if ( !empty( $old_tags ) ) {
344 $tagsList = array_filter( array_map( 'trim', explode( ',', $old_tags ) ) );
345 foreach ( $tagsList as $tag ) {
346 $tag = sanitize_text_field( $tag );
347 if ( $tag === '' ) {
348 continue;
349 }
350 if ( !term_exists( $tag, PGC_SGB_TAGS ) ) {
351 wp_insert_term( $tag, PGC_SGB_TAGS );
352 }
353 }
354 }
355 //delete_option('pgc_sgb_tags_list');
356 }
357
358 function pgc_sgb_get_tags_list() {
359 $terms = get_terms( array(
360 'taxonomy' => PGC_SGB_TAGS,
361 'hide_empty' => false,
362 'orderby' => 'name',
363 'order' => 'ASC',
364 ) );
365 if ( empty( $terms ) || is_wp_error( $terms ) ) {
366 return '';
367 }
368 $names = wp_list_pluck( $terms, 'name' );
369 return implode( ',', $names );
370 }
371
372 function pgc_sgb_update_tags_list( $tagsArr, $delete = NULL ) {
373 if ( is_string( $tagsArr ) ) {
374 $inputTags = array_filter( array_map( 'trim', explode( ',', $tagsArr ) ) );
375 } elseif ( is_array( $tagsArr ) ) {
376 $inputTags = array_filter( array_map( 'trim', $tagsArr ) );
377 } else {
378 return pgc_sgb_get_tags_list();
379 }
380 $deleted = array();
381 foreach ( $inputTags as $tag ) {
382 $tag = sanitize_text_field( $tag );
383 if ( $tag === '' ) {
384 continue;
385 }
386 if ( is_null( $delete ) ) {
387 if ( !term_exists( $tag, PGC_SGB_TAGS ) ) {
388 wp_insert_term( $tag, PGC_SGB_TAGS );
389 }
390 } else {
391 $term = term_exists( $tag, PGC_SGB_TAGS );
392 if ( $term && !is_wp_error( $term ) ) {
393 wp_delete_term( $term['term_id'], PGC_SGB_TAGS );
394 $deleted[] = $tag;
395 }
396 }
397 }
398 $res = array();
399 if ( !is_null( $delete ) ) {
400 $res['delete'] = true;
401 }
402 $tagsString = pgc_sgb_get_tags_list();
403 $res['tagsList'] = $tagsString;
404 return $res;
405 }
406
407 function pgc_sgb_delete_post_tags_meta( $postIDs, $tagsArr, $attachment_only = false ) {
408 if ( !is_array( $postIDs ) ) {
409 $postIDs = array($postIDs);
410 }
411 if ( !is_array( $tagsArr ) ) {
412 $tagsArr = array($tagsArr);
413 }
414 $postIDs = array_values( array_filter( array_map( 'absint', $postIDs ) ) );
415 $tagsArr = array_values( array_filter( array_map( 'sanitize_text_field', $tagsArr ) ) );
416 $deleted = array();
417 $skipped = array();
418 foreach ( $postIDs as $postId ) {
419 if ( $attachment_only && get_post_type( $postId ) !== 'attachment' ) {
420 $skipped[] = $postId;
421 continue;
422 }
423 if ( !current_user_can( 'delete_post_meta', $postId ) ) {
424 $skipped[] = $postId;
425 continue;
426 }
427 foreach ( $tagsArr as $tag ) {
428 if ( !isset( $deleted[$postId] ) ) {
429 $deleted[$postId] = array();
430 }
431 if ( $tag !== '' && delete_post_meta( $postId, 'pgc_sgb_tag', $tag ) ) {
432 $deleted[$postId][] = $tag;
433 }
434 }
435 }
436 return array(
437 'deleted' => $deleted,
438 'skipped' => $skipped,
439 );
440 }
441
442 function pgc_sgb_can_write_direct( $path ) {
443 require_once ABSPATH . 'wp-admin/includes/file.php';
444 if ( get_filesystem_method( array(), $path, true ) === 'direct' ) {
445 $creds = request_filesystem_credentials(
446 site_url() . '/wp-admin/',
447 '',
448 false,
449 false,
450 array()
451 );
452 if ( !WP_Filesystem( $creds ) ) {
453 return false;
454 }
455 return true;
456 }
457 return false;
458 }
459
460 function pgc_sgb_run_migrations() {
461 $installed_version = get_option( 'pgc_sgb_version' );
462 if ( !$installed_version ) {
463 pgc_sgb_tags_migrate();
464 pgc_sgb_migrate_presets();
465 update_option( 'pgc_sgb_version', PGC_SGB_VERSION );
466 return;
467 }
468 if ( $installed_version !== PGC_SGB_VERSION ) {
469 if ( version_compare( $installed_version, '3.3.1', '<' ) ) {
470 pgc_sgb_tags_migrate();
471 pgc_sgb_migrate_presets();
472 }
473 update_option( 'pgc_sgb_version', PGC_SGB_VERSION );
474 }
475 }
476
477 function pgc_sgb_init() {
478 register_taxonomy( PGC_SGB_TAGS, array('post'), array(
479 'label' => __( 'SimpLy Tags', 'simply-gallery-block' ),
480 'public' => false,
481 'show_ui' => false,
482 'hierarchical' => false,
483 'capabilities' => array(
484 'manage_terms' => 'edit_posts',
485 'edit_terms' => 'edit_posts',
486 'delete_terms' => 'edit_posts',
487 'assign_terms' => 'edit_posts',
488 ),
489 ) );
490 register_post_type( PGC_SGB_PRESET, array(
491 'label' => 'Simply Gallery Preset',
492 'public' => false,
493 'show_ui' => false,
494 'capability_type' => 'post',
495 'supports' => array('title'),
496 ) );
497 pgc_sgb_run_migrations();
498 }
499
500 add_action( 'init', 'pgc_sgb_init' );
501 function pgc_sgb_action_wizard() {
502 global $post, $pgc_sgb_wc_to_sgb, $pgc_sgb_skins_list;
503 check_ajax_referer( 'pgc-sgb-nonce', 'nonce' );
504 $json = array();
505 $out = array();
506 $out['message'] = array();
507 $data = array();
508 if ( isset( $_POST['props'] ) ) {
509 $globaldata = sanitize_text_field( wp_unslash( $_POST['props'] ) );
510 $json = json_decode( $globaldata, true );
511 }
512 switch ( $json['type'] ) {
513 case 'create_post_thumbnail':
514 $post_id = ( isset( $json['postId'] ) ? absint( $json['postId'] ) : 0 );
515 if ( $post_id && current_user_can( 'add_post_meta', $post_id ) && current_user_can( 'edit_post', $post_id ) && current_user_can( 'upload_files' ) ) {
516 $videoName_raw = ( isset( $json['name'] ) ? wp_unslash( $json['name'] ) : '' );
517 // Force a safe, traversal-free filename (no slashes, no dot-dot segments).
518 $poster_base = sanitize_file_name( $videoName_raw . '_poster.jpg' );
519 $poster_base = wp_basename( $poster_base );
520 if ( $poster_base === '' || $poster_base === '.' || $poster_base === '..' ) {
521 $poster_base = 'poster.jpg';
522 }
523 $uploadsDir = wp_upload_dir();
524 $poster_tmp_dir = trailingslashit( $uploadsDir['path'] ) . 'poster_tmp/';
525 wp_mkdir_p( $poster_tmp_dir );
526 // Ensure unique filename in tmp dir.
527 $poster_filename = wp_unique_filename( $poster_tmp_dir, $poster_base );
528 $tmpPosterPath = $poster_tmp_dir . $poster_filename;
529 if ( isset( $_POST['thumb_raw_data'] ) ) {
530 $imgData = wp_unslash( $_POST['thumb_raw_data'] );
531 }
532 if ( isset( $imgData ) ) {
533 // Strictly parse data URI, only allow PNG/JPEG.
534 $imgData = trim( (string) $imgData );
535 if ( !preg_match( '~^data:(image/(png|jpeg));base64,(.*)$~s', $imgData, $m ) ) {
536 $out['message']['poster'] = 'false';
537 $out['message']['error'] = 'Error: Unsupported image format';
538 break;
539 }
540 $raw_b64 = str_replace( ' ', '+', $m[3] );
541 $decoded_png = base64_decode( $raw_b64, true );
542 // Basic size and content validation to avoid abuse.
543 if ( $decoded_png === false ) {
544 $out['message']['poster'] = 'false';
545 $out['message']['error'] = 'Error: Invalid base64 data';
546 break;
547 }
548 $max_bytes = (int) apply_filters( 'pgc_sgb_max_thumbnail_bytes', 5 * 1024 * 1024, $post_id );
549 if ( strlen( $decoded_png ) > $max_bytes ) {
550 $out['message']['poster'] = 'false';
551 $out['message']['error'] = 'Error: Image too large';
552 break;
553 }
554 $img_info = @getimagesizefromstring( $decoded_png );
555 if ( empty( $img_info ) || empty( $img_info['mime'] ) || $img_info['mime'] !== $m[1] || !in_array( $img_info['mime'], array('image/jpeg', 'image/png'), true ) ) {
556 $out['message']['poster'] = 'false';
557 $out['message']['error'] = 'Error: Invalid image';
558 break;
559 }
560 if ( pgc_sgb_can_write_direct( dirname( $tmpPosterPath ) ) ) {
561 global $wp_filesystem;
562 $success = $wp_filesystem->put_contents( $tmpPosterPath, $decoded_png );
563 if ( $success ) {
564 $file = array(
565 'name' => $poster_filename,
566 'type' => $img_info['mime'],
567 'tmp_name' => $tmpPosterPath,
568 'size' => filesize( $tmpPosterPath ),
569 );
570 $sideload = wp_handle_sideload( $file, array(
571 'test_form' => false,
572 ) );
573 if ( !empty( $sideload['error'] ) ) {
574 $out['message']['poster'] = 'false';
575 $out['message']['error'] = $sideload;
576 } else {
577 $attachment_id = wp_insert_attachment( array(
578 'guid' => $sideload['url'],
579 'post_mime_type' => $sideload['type'],
580 'post_title' => preg_replace( '/\\.[^.]+$/', '', basename( $sideload['file'] ) ),
581 'post_content' => '',
582 'post_status' => 'inherit',
583 ), $sideload['file'] );
584 if ( is_wp_error( $attachment_id ) || !$attachment_id ) {
585 $out['message']['poster'] = 'false';
586 $out['message']['error'] = 'Error: insert attachment';
587 } else {
588 require_once ABSPATH . 'wp-admin/includes/image.php';
589 $meta_data = wp_generate_attachment_metadata( $attachment_id, $sideload['file'] );
590 wp_update_attachment_metadata( $attachment_id, $meta_data );
591 $wp_filesystem->delete( $tmpPosterPath );
592 $out['message']['metaData'] = $meta_data;
593 $out['message']['sideload'] = $sideload;
594 $out['message']['posterId'] = $attachment_id;
595 $out['message']['poster'] = set_post_thumbnail( $post_id, $attachment_id );
596 }
597 }
598 } else {
599 $out['message']['poster'] = 'false';
600 $out['message']['error'] = 'Error: temp file';
601 }
602 }
603 } else {
604 $out['message']['poster'] = 'false';
605 $out['message']['error'] = 'Error: Image data';
606 }
607 }
608 break;
609 case 'update_post_thumbnail':
610 $post_id = ( isset( $json['postId'] ) ? absint( $json['postId'] ) : 0 );
611 if ( $post_id && current_user_can( 'add_post_meta', $post_id ) ) {
612 if ( intval( $json['value'] ) === 0 ) {
613 $out['message'][$json['key']] = delete_post_thumbnail( $post_id );
614 } else {
615 $out['message'][$json['key']] = set_post_thumbnail( $post_id, intval( $json['value'] ) );
616 }
617 }
618 break;
619 case 'update_post_meta':
620 $post_id = ( isset( $json['postId'] ) ? absint( $json['postId'] ) : 0 );
621 $key_raw = ( isset( $json['key'] ) ? (string) $json['key'] : '' );
622 $value_raw = ( isset( $json['value'] ) ? $json['value'] : '' );
623 if ( $post_id && current_user_can( 'edit_post', $post_id ) && current_user_can( 'add_post_meta', $post_id ) ) {
624 // Only allow plugin-owned meta keys.
625 $key = sanitize_key( $key_raw );
626 if ( strpos( $key, 'pgc_sgb_' ) === 0 ) {
627 $out['message'][$key] = update_post_meta( $post_id, $key, sanitize_text_field( $value_raw ) );
628 } else {
629 $out['message']['error'] = 'Error: Invalid meta key';
630 }
631 }
632 break;
633 case 'add_posts_meta':
634 $tagsArr = $json['value'];
635 $postIDs = $json['postIDs'];
636 $key = $json['key'];
637 if ( isset( $tagsArr ) && isset( $postIDs ) && isset( $key ) && $key === 'pgc_sgb_tag' ) {
638 $out['message'][$json['key']] = true;
639 foreach ( $postIDs as $postId ) {
640 if ( current_user_can( 'add_post_meta', intval( $postId ) ) ) {
641 $itemTags = get_post_meta( $postId, 'pgc_sgb_tag' );
642 foreach ( $tagsArr as $val ) {
643 if ( $val !== '' ) {
644 if ( !isset( $data[$postId] ) ) {
645 $data[$postId] = array();
646 }
647 if ( !empty( $itemTags ) ) {
648 if ( array_search( $val, $itemTags ) === false ) {
649 if ( add_post_meta(
650 $postId,
651 $key,
652 $val,
653 false
654 ) ) {
655 array_push( $data[$postId], $val );
656 }
657 }
658 } else {
659 if ( add_post_meta(
660 $postId,
661 $key,
662 $val,
663 false
664 ) ) {
665 array_push( $data[$postId], $val );
666 }
667 }
668 }
669 }
670 }
671 $out['message']['tags_list'] = pgc_sgb_update_tags_list( $tagsArr );
672 }
673 }
674 break;
675 case 'delete_posts_meta':
676 $tagsArr = $json['value'];
677 $postIDs = $json['postIDs'];
678 $key = $json['key'];
679 if ( isset( $tagsArr ) && isset( $postIDs ) && isset( $key ) && $key === 'pgc_sgb_tag' ) {
680 $out['message'][$json['key']] = true;
681 $delete_result = pgc_sgb_delete_post_tags_meta( $postIDs, $tagsArr );
682 $data = $delete_result['deleted'];
683 }
684 break;
685 case 'get_attachments_for_admin':
686 $query = ( array_key_exists( 'query', $json ) ? $json['query'] : null );
687 if ( !current_user_can( 'upload_files' ) || !$query ) {
688 $out['message']['success'] = false;
689 } else {
690 $q_args = array(
691 'post_mime_type' => array('image', 'video', 'audio'),
692 'post_status' => 'inherit',
693 'post_type' => 'attachment',
694 'orderby' => 'post__in',
695 'order' => 'DESC',
696 'posts_per_page' => -1,
697 'paged' => 1,
698 );
699 if ( isset( $query['tax_query'] ) ) {
700 $tax_query = array();
701 foreach ( $query['tax_query'] as $tax ) {
702 array_push( $tax_query, (array) $tax );
703 }
704 $query['tax_query'] = $tax_query;
705 }
706 if ( isset( $query['meta_query'] ) ) {
707 $meta_query = array(
708 'relation' => 'OR',
709 );
710 foreach ( $query['meta_query'] as $meta ) {
711 array_push( $meta_query, (array) $meta );
712 }
713 $query['meta_query'] = $meta_query;
714 }
715 $query = array_merge( $q_args, (array) $query );
716 $attachments_query = new WP_Query($query);
717 $posts = array_map( 'wp_prepare_attachment_for_js', $attachments_query->posts );
718 $postsWithMeta = array();
719 $itemsMetaDataCollection = array();
720 foreach ( $posts as $my_post ) {
721 $meta_data = get_post_meta( $my_post['id'] );
722 $attachment_meta_data = wp_get_attachment_metadata( $my_post['id'], true );
723 if ( $meta_data ) {
724 if ( isset( $meta_data['pgc_sgb_link'] ) || isset( $meta_data['pgc_sgb_tag'] ) ) {
725 $itemSubMeta = array();
726 $itemSubMeta['id'] = $my_post['id'];
727 if ( isset( $meta_data['pgc_sgb_link'] ) ) {
728 $linkMeta = json_decode( $meta_data['pgc_sgb_link'][0], true );
729 if ( $linkMeta ) {
730 $itemSubMeta = array_merge( $itemSubMeta, $linkMeta );
731 }
732 }
733 if ( isset( $meta_data['pgc_sgb_tag'] ) ) {
734 $itemSubMeta['tags'] = $meta_data['pgc_sgb_tag'];
735 }
736 array_push( $itemsMetaDataCollection, $itemSubMeta );
737 }
738 }
739 $postExt = $my_post;
740 if ( $attachment_meta_data ) {
741 if ( isset( $attachment_meta_data['sizes'] ) && isset( $postExt['sizes'] ) ) {
742 if ( isset( $postExt['sizes']['large'] ) && isset( $attachment_meta_data['sizes']['large'] ) ) {
743 $postExt['sizes']['large']['height'] = $attachment_meta_data['sizes']['large']['height'];
744 $postExt['sizes']['large']['width'] = $attachment_meta_data['sizes']['large']['width'];
745 }
746 if ( isset( $postExt['sizes']['medium'] ) && isset( $attachment_meta_data['sizes']['medium'] ) ) {
747 $postExt['sizes']['medium']['height'] = $attachment_meta_data['sizes']['medium']['height'];
748 $postExt['sizes']['medium']['width'] = $attachment_meta_data['sizes']['medium']['width'];
749 }
750 }
751 if ( isset( $attachment_meta_data['image_meta'] ) ) {
752 $postExt['imageMeta'] = $attachment_meta_data['image_meta'];
753 }
754 }
755 array_push( $postsWithMeta, $postExt );
756 }
757 $out['message']['success'] = true;
758 $out['message']['itemsMetaData'] = $itemsMetaDataCollection;
759 $data = $postsWithMeta;
760 }
761 wp_reset_query();
762 break;
763 case 'get_attachments_metadata':
764 foreach ( $json['iDs'] as $i => $value ) {
765 if ( current_user_can( 'read_post', intval( $json['iDs'][$i] ) ) ) {
766 $data[$json['iDs'][$i]] = wp_get_attachment_metadata( $json['iDs'][$i], true );
767 }
768 }
769 break;
770 case 'get_posts_metadata':
771 foreach ( $json['iDs'] as $i => $value ) {
772 if ( current_user_can( 'read_post', intval( $json['iDs'][$i] ) ) ) {
773 $main_meta = get_post_meta( $json['iDs'][$i], ( $json['key'] ? $json['key'] : '' ), true );
774 $tags = get_post_meta( $json['iDs'][$i], 'pgc_sgb_tag' );
775 if ( !$main_meta || !empty( $main_meta ) ) {
776 $main_meta = json_decode( $main_meta, true );
777 } else {
778 $main_meta = array();
779 }
780 if ( $tags || !empty( $main_meta ) ) {
781 $main_meta['tags'] = $tags;
782 }
783 if ( !empty( $main_meta ) ) {
784 $data[$json['iDs'][$i]] = wp_json_encode( $main_meta );
785 }
786 }
787 }
788 break;
789 case 'update_tags_list':
790 $value = $json['value'];
791 if ( current_user_can( 'edit_posts' ) ) {
792 $out['message'][$json['key']] = true;
793 $out['message']['tags_list'] = pgc_sgb_update_tags_list( $value, ( $json['action'] === 'delete' ? true : NULL ) );
794 }
795 break;
796 case 'update_option':
797 foreach ( $json['options'] as $key => $value ) {
798 if ( strpos( $key, 'pgc_sgb' ) === 0 ) {
799 if ( isset( $pgc_sgb_skins_list[$key] ) ) {
800 $out['message'][$key] = pgc_sgb_update_preset( $key, $value );
801 } elseif ( $key === 'pgc_sgb_lightbox' ) {
802 $out['message'][$key] = pgc_sgb_update_preset( $key, $value );
803 } else {
804 if ( current_user_can( 'manage_options' ) ) {
805 $out['message'][$key] = update_option( $key, $value );
806 }
807 }
808 }
809 }
810 break;
811 case 'get_option':
812 foreach ( $json['options'] as $key => $value ) {
813 if ( strpos( $key, 'pgc_sgb' ) === 0 ) {
814 if ( $key === 'pgc_sgb_tags_list' ) {
815 $out['message'][$key] = pgc_sgb_get_tags_list();
816 } elseif ( isset( $pgc_sgb_skins_list[$key] ) ) {
817 $out['message'][$key] = pgc_sgb_get_preset_by_slug( $key );
818 } else {
819 if ( current_user_can( 'manage_options' ) ) {
820 $out['message'][$key] = get_option( $key );
821 }
822 }
823 }
824 }
825 break;
826 case 'get_media_organization_sources':
827 $data = array(
828 'folders' => array(),
829 'tags' => array(),
830 );
831 if ( !current_user_can( 'upload_files' ) ) {
832 $out['message']['success'] = false;
833 $out['message']['error'] = 'Error: Insufficient permissions';
834 break;
835 }
836 if ( function_exists( 'pgc_sgb_media_folders_get_terms' ) && function_exists( 'pgc_sgb_media_folders_normalize_term' ) ) {
837 $folder_counts = ( function_exists( 'pgc_sgb_media_folders_get_counts' ) ? pgc_sgb_media_folders_get_counts() : array() );
838 foreach ( pgc_sgb_media_folders_get_terms() as $term ) {
839 $count_key = (string) $term->term_id;
840 $data['folders'][] = pgc_sgb_media_folders_normalize_term( $term, ( isset( $folder_counts[$count_key] ) ? $folder_counts[$count_key] : null ) );
841 }
842 }
843 if ( function_exists( 'pgc_sgb_media_folders_get_normalized_tags' ) ) {
844 $data['tags'] = pgc_sgb_media_folders_get_normalized_tags();
845 } else {
846 $tags_string = ( function_exists( 'pgc_sgb_get_tags_list' ) ? pgc_sgb_get_tags_list() : '' );
847 $tags = ( $tags_string === '' ? array() : array_filter( array_map( 'trim', explode( ',', $tags_string ) ) ) );
848 natcasesort( $tags );
849 foreach ( $tags as $tag ) {
850 $data['tags'][] = array(
851 'name' => $tag,
852 'count' => 0,
853 );
854 }
855 }
856 $out['message']['success'] = true;
857 break;
858 case 'get_categories_by_taxonomy':
859 $taxonomy = $json['taxonomy'];
860 $categories = get_categories( [
861 'taxonomy' => $taxonomy,
862 'hide_empty' => 1,
863 'orderby' => 'name',
864 'order' => 'ASC',
865 ] );
866 $data = [];
867 foreach ( $categories as $cat ) {
868 $catData = array();
869 $catData['term_name'] = $cat->name;
870 $catData['term_id'] = $cat->term_id;
871 $catData['count'] = $cat->count;
872 $catData['description'] = $cat->category_description;
873 array_push( $data, $catData );
874 }
875 break;
876 case 'get_posts_by_type':
877 $my_query = null;
878 $postType = $json['postType'];
879 $extended = ( array_key_exists( 'extended', $json ) ? true : false );
880 $term_id = ( isset( $json['term_id'] ) ? $json['term_id'] : null );
881 $q_args = [
882 'post_type' => $postType,
883 'posts_per_page' => -1,
884 ];
885 if ( $extended ) {
886 $q_args['post_status'] = 'publish';
887 }
888 if ( isset( $term_id ) ) {
889 $q_args['tax_query'] = array(array(
890 'taxonomy' => PGC_SGB_TAXONOMY,
891 'terms' => $term_id,
892 'include_children' => false,
893 ));
894 }
895 $my_query = new WP_Query($q_args);
896 if ( $my_query->have_posts() ) {
897 $postslist = $my_query->posts;
898 $data = [];
899 foreach ( $postslist as $post ) {
900 $postData = array();
901 $postData['title'] = ( $post->post_title !== '' ? $post->post_title : $post->ID );
902 $postData['ID'] = $post->ID;
903 $postData['postStatus'] = $post->post_status;
904 if ( current_user_can( 'read_post', intval( $postData['ID'] ) ) ) {
905 if ( $extended ) {
906 $postData['id'] = $post->ID;
907 $postData['modified'] = $post->post_modified;
908 $postData['date'] = $post->post_date;
909 $postData['postLink'] = get_post_permalink( $post->ID );
910 $postData['type'] = $post->post_type;
911 $postData['slug'] = $post->post_name;
912 }
913 if ( has_post_thumbnail( $post ) ) {
914 $postData['thumbURL'] = get_the_post_thumbnail_url( $post, 'thumbnail' );
915 if ( $extended ) {
916 $thumbId = get_post_thumbnail_id( $post );
917 if ( $thumbId !== false ) {
918 $thumbPost = get_post( $thumbId );
919 $thumbData = wp_prepare_attachment_for_js( $thumbPost );
920 $postData['thumb'] = ( isset( $thumbData['sizes'] ) ? $thumbData['sizes'] : null );
921 }
922 }
923 } else {
924 if ( $extended ) {
925 $postData['thumbURL'] = PGC_SGB_URL . 'assets/coverAlbum-400x400.png';
926 } else {
927 $postData['thumbURL'] = PGC_SGB_URL . 'assets/icon-150x150.png';
928 }
929 }
930 array_push( $data, $postData );
931 }
932 }
933 }
934 wp_reset_query();
935 break;
936 case 'get_post_content':
937 $id = intval( $json['postID'] );
938 if ( current_user_can( 'read_post', $id ) ) {
939 $postData = get_post_field( 'post_content', $id, 'display' );
940 $postStatus = get_post_field( 'post_status', $id, 'attribute' );
941 $output = '';
942 if ( $postData !== '' || !is_wp_error( $postData ) ) {
943 $blocks = parse_blocks( $postData );
944 foreach ( $blocks as $block ) {
945 $output .= render_block( $block );
946 }
947 }
948 if ( $postStatus !== '' || !is_wp_error( $postStatus ) ) {
949 $data['postStatus'] = $postStatus;
950 }
951 $data['postID'] = $id;
952 $data['raw'] = $output;
953 }
954 break;
955 case 'get_terms_for_taxonomy':
956 $taxonomyName = $json['name'];
957 $terms = get_terms( [
958 'taxonomy' => $taxonomyName,
959 'hide_empty' => false,
960 ] );
961 $data[$taxonomyName] = $terms;
962 break;
963 case 'deletePosts':
964 $post_type = ( isset( $json['post_type'] ) ? sanitize_key( $json['post_type'] ) : '' );
965 if ( $post_type !== 'pgc_simply_cache' ) {
966 $out['message']['success'] = false;
967 $out['message']['error'] = 'Error: Invalid cache post type';
968 break;
969 }
970 $p_arg = array(
971 'post_type' => 'pgc_simply_cache',
972 'post_status' => 'publish',
973 'numberposts' => -1,
974 'fields' => 'ids',
975 'no_found_rows' => true,
976 );
977 if ( isset( $json['name'] ) ) {
978 $name = ( function_exists( 'pgc_sgb_normalize_cache_name' ) ? pgc_sgb_normalize_cache_name( $json['name'] ) : sanitize_title( $json['name'] ) );
979 if ( !$name ) {
980 $out['message']['success'] = false;
981 $out['message']['error'] = 'Error: Invalid cache name';
982 break;
983 }
984 $p_arg['name'] = $name;
985 }
986 $post_ids = get_posts( $p_arg );
987 if ( !empty( $post_ids ) ) {
988 foreach ( $post_ids as $post_id ) {
989 $postId = intval( $post_id );
990 if ( current_user_can( 'delete_post', intval( $postId ) ) ) {
991 $deleted = is_object( wp_delete_post( $postId ) );
992 array_push( $data, array(
993 $postId . '' => $deleted,
994 ) );
995 }
996 }
997 }
998 break;
999 case 'get_products_for_admin':
1000 if ( isset( $pgc_sgb_wc_to_sgb ) ) {
1001 $query = ( array_key_exists( 'query', $json ) ? $json['query'] : null );
1002 if ( isset( $query['naviHelper'] ) ) {
1003 $out['message']['naviHelper'] = $query['naviHelper'];
1004 unset($query['naviHelper']);
1005 }
1006 $q_args = array();
1007 $q_args = array(
1008 'post_status' => 'publish',
1009 'perm' => 'readable',
1010 );
1011 if ( isset( $query['tax_query'] ) ) {
1012 $tax_query = array();
1013 foreach ( $query['tax_query'] as $tax ) {
1014 array_push( $tax_query, (array) $tax );
1015 }
1016 $query['tax_query'] = $tax_query;
1017 }
1018 $query = array_merge( $q_args, (array) $query );
1019 $products_query = new WP_Query($query);
1020 $posts = $products_query->posts;
1021 $productsData = $pgc_sgb_wc_to_sgb( $posts );
1022 $products = $productsData['products'];
1023 $itemsMetaDataCollection = $productsData['itemsMetaData'];
1024 $out['message']['success'] = true;
1025 $out['message']['itemsMetaData'] = $itemsMetaDataCollection;
1026 $data = $products;
1027 wp_reset_query();
1028 } else {
1029 $out['message']['success'] = false;
1030 $data = array();
1031 }
1032 break;
1033 }
1034 $out['message']['data'] = $data;
1035 header( 'Content-Type: application/json; charset=' . get_option( 'blog_charset' ), true );
1036 echo wp_json_encode( $out );
1037 wp_die();
1038 }
1039
1040 if ( wp_doing_ajax() ) {
1041 add_action( 'wp_ajax_pgc_sgb_action_wizard', 'pgc_sgb_action_wizard' );
1042 }
1043 require_once plugin_dir_path( __FILE__ ) . 'blocks/init.php';
1044 require_once plugin_dir_path( __FILE__ ) . 'plugins/init.php';
1045 require_once plugin_dir_path( __FILE__ ) . 'plugins/media_folders/init.php';
1046 }