PluginProbe
MaxGalleria / trunk
MaxGalleria vtrunk
6.5.3 trunk 2.0.0 2.0.1 2.1.0 2.2.0 2.2.1 2.2.2 2.3 2.4 2.5 2.6 2.6.1 3.0.0 3.0.1 3.0.2 3.0.3 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 All 135 releases
maxgalleria / maxgalleria.php

maxgalleria.php in MaxGalleria trunk, at maxgalleria.php

2,093 lines 77.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: MaxGalleria
4 Plugin URI: https://maxgalleria.com
5 Description: The gallery platform for WordPress.
6 Version: 6.5.3
7 Author: Max Foundry
8 Author URI: https://maxfoundry.com
9
10 Copyright 2014-2022 Max Foundry, LLC (https://maxfoundry.com)
11 */
12
13 class MaxGalleria {
14 private $_addons;
15
16 public $admin;
17 public $common;
18 public $meta;
19 public $nextgen;
20 public $settings;
21 public $shortcode;
22 public $shortcode_thumb;
23 public $new_gallery;
24 public $image_gallery;
25 public $video_gallery;
26 public $gallery_widget;
27 public $gallery_thumb_widget;
28 //public $license_valid;
29
30 public function __construct() {
31 $this->_addons = array();
32
33 $this->set_global_constants();
34 $this->set_activation_hooks();
35 $this->add_thumb_sizes();
36 $this->setup_hooks();
37 }
38
39 function activate() {
40 update_option(MAXGALLERIA_VERSION_KEY, MAXGALLERIA_VERSION_NUM);
41
42 $this->copy_template();
43
44 $current_user_id = get_current_user_id();
45 $havemeta = get_user_meta( $current_user_id, MAXGALLERIA_REVIEW_NOTICE, true );
46 if ($havemeta === '') {
47 $review_date = date('Y-m-d', strtotime("+1 days"));
48 update_user_meta( $current_user_id, MAXGALLERIA_REVIEW_NOTICE, $review_date );
49 }
50
51 //if ( 'impossible_default_value_12143' === get_option( 'mg_check_presets', 'impossible_default_value_12143' ) ) {
52 $mg_hide_presets = get_option( 'mg_hide_presets', 'none' );
53 if($mg_hide_presets !== 'on' && $mg_hide_presets !== 'off') {
54 if($this->check_for_mg_posts())
55 update_option( "mg_hide_presets", "off", true );
56 else
57 update_option( "mg_hide_presets", "on", true );
58 //update_option( "mg_check_presets", "impossible_default_value_12143", true );
59 }
60
61 }
62
63 function copy_template() {
64
65 // Copy gallery post type template file to theme directory
66 $source = MAXGALLERIA_PLUGIN_DIR . '/single-maxgallery.php';
67 $destination = $this->get_theme_dir() . '/single-maxgallery.php';
68 if(!defined('PRESERVE_MAXGALLERIA_TEMPLATE')) {
69 copy($source, $destination);
70 }
71 else if(!file_exists($destination)) {
72 copy($source, $destination);
73 }
74 flush_rewrite_rules();
75 }
76
77 public function add_thumb_sizes() {
78 // In addition to the thumbnail support when registering the custom post type, we need to add theme support
79 // to properly handle the featured image for a gallery, just in case the theme itself doesn't have it.
80 add_theme_support('post-thumbnails');
81
82 // Additional sizes, cropped
83 add_image_size(MAXGALLERIA_META_IMAGE_THUMB_SMALL, 100, 100, true);
84 add_image_size(MAXGALLERIA_META_IMAGE_THUMB_MEDIUM, 150, 150, true);
85 add_image_size(MAXGALLERIA_META_IMAGE_THUMB_LARGE, 200, 200, true);
86 add_image_size(MAXGALLERIA_META_VIDEO_THUMB_SMALL, 150, 100, true);
87 add_image_size(MAXGALLERIA_META_VIDEO_THUMB_MEDIUM, 200, 133, true);
88 add_image_size(MAXGALLERIA_META_VIDEO_THUMB_LARGE, 250, 166, true);
89 }
90
91 public function admin_page_is_maxgallery_post_type($post_id = 0) {
92 global $post;
93 global $post_type;
94
95 if (isset($post_id) && $post_id > 0 && get_post_type($post_id) == MAXGALLERIA_POST_TYPE) {
96 return true;
97 }
98
99 if (isset($_GET['post']) && $_GET['post'] > 0 && get_post_type($_GET['post']) == MAXGALLERIA_POST_TYPE) {
100 return true;
101 }
102
103 if (isset($_GET['post_type']) && $_GET['post_type'] == MAXGALLERIA_POST_TYPE) {
104 return true;
105 }
106
107 if (isset($post_type) && $post_type == MAXGALLERIA_POST_TYPE) {
108 return true;
109 }
110
111 if (isset($post) && $post->post_type == MAXGALLERIA_POST_TYPE) {
112 return true;
113 }
114
115 return false;
116 }
117
118 public function admin_page_is_media_edit() {
119 if ($this->common->url_contains('wp-admin/media.php') && $this->common->url_contains('action=edit')) {
120 return true;
121 }
122
123 return false;
124 }
125
126 public function admin_page_is_post_edit() {
127 if ($this->common->url_contains('wp-admin/post.php') && $this->common->url_contains('action=edit')) {
128 return true;
129 }
130
131 return false;
132 }
133
134 public function call_function_for_each_site($function) {
135 global $wpdb;
136
137 // Hold this so we can switch back to it
138 $current_blog = $wpdb->blogid;
139
140 // Get all the blogs/sites in the network and invoke the function for each one
141 $blog_ids = $wpdb->get_col("SELECT blog_id FROM $wpdb->blogs");
142 foreach ($blog_ids as $blog_id) {
143 switch_to_blog($blog_id);
144 call_user_func($function);
145 }
146
147 // Now switch back to the root blog
148 switch_to_blog($current_blog);
149 }
150
151 public function create_gallery_columns($column) {
152 // The Title and Date columns are standard, so we don't have to explicitly provide output for them
153
154 global $post;
155 $maxgallery = new MaxGalleryOptions($post->ID);
156
157 // Get all the attachments (the -1 gets all of them)
158 $args = array('post_parent' => $post->ID, 'post_type' => 'attachment', 'orderby' => 'menu_order', 'order' => 'asc', 'numberposts' => -1);
159 $attachments = get_posts($args);
160
161 // Rounded borders
162 $style = 'border-radius: 2px; -moz-border-radius: 2px; -webkit-border-radius: 2px;';
163
164 switch ($column) {
165 case 'type':
166 if ($maxgallery->is_image_gallery()) {
167 echo '<img src="' . MAXGALLERIA_PLUGIN_URL . '/images/image-32.png" alt="' . esc_html__('Image', 'maxgalleria') . '" title="' . esc_html__('Image', 'maxgalleria') . '" style="' . esc_attr($style) . '" />';
168 }
169
170 if ($maxgallery->is_video_gallery()) {
171 echo '<img src="' . MAXGALLERIA_PLUGIN_URL . '/images/video-32.png" alt="' . esc_html__('Video', 'maxgalleria') . '" title="' . esc_html__('Video', 'maxgalleria') . '" style="' . esc_attr($style) . '" />';
172 }
173
174 break;
175 case 'thumbnail':
176 if (has_post_thumbnail($post->ID)) {
177 echo get_the_post_thumbnail($post->ID, array(32, 32), array('style' => $style));
178 }
179 else {
180 // Show the first thumb
181 foreach ($attachments as $attachment) {
182 $no_media_icon = 0;
183 echo wp_get_attachment_image($attachment->ID, array(32, 32), $no_media_icon, array('style' => $style));
184 break;
185 }
186 }
187 break;
188 case 'template':
189 $template_key = $maxgallery->get_template();
190 echo esc_html($this->get_template_name($template_key));
191 break;
192 case 'number':
193 if ($maxgallery->is_image_gallery()) {
194 if (count($attachments) == 0) { esc_html_e('0 images', 'maxgalleria'); }
195 if (count($attachments) == 1) { esc_html_e('1 image', 'maxgalleria'); }
196 if (count($attachments) > 1) { printf(esc_html__('%d images', 'maxgalleria'), count($attachments)); }
197 }
198
199 if ($maxgallery->is_video_gallery()) {
200 if (count($attachments) == 0) { esc_html_e('0 videos', 'maxgalleria'); }
201 if (count($attachments) == 1) { esc_html_e('1 video', 'maxgalleria'); }
202 if (count($attachments) > 1) { printf(esc_html__('%d videos', 'maxgalleria'), count($attachments)); }
203 }
204
205 break;
206 case 'shortcode':
207 echo '[maxgallery id="' . esc_html($post->ID) . '"]';
208
209 if ($post->post_status == 'publish') {
210 echo '<br />';
211 echo '[maxgallery name="' . esc_html($post->post_name) . '"]';
212 }
213
214 break;
215 }
216 }
217
218 public function create_plugin_action_links($links, $file) {
219 static $this_plugin;
220
221 if (!$this_plugin) {
222 $this_plugin = plugin_basename(__FILE__);
223 }
224
225 if ($file == $this_plugin) {
226 $settings_link = '<a href="' . admin_url() . 'edit.php?post_type=' . MAXGALLERIA_POST_TYPE . '&page=maxgalleria-settings">' . esc_html__('Settings', 'maxgalleria') . '</a>';
227 array_unshift($links, $settings_link);
228
229 $galleries_link = '<a href="' . admin_url() . 'edit.php?post_type=' . MAXGALLERIA_POST_TYPE . '">' . esc_html__('Galleries', 'maxgalleria') . '</a>';
230 array_unshift($links, $galleries_link);
231 }
232
233 return $links;
234 }
235
236 public function create_sortable_gallery_columns($vars) {
237 if (isset($vars['orderby'])) {
238 switch ($vars['orderby']) {
239 case 'type':
240 $vars = array_merge($vars, array('meta_key' => 'maxgallery_type', 'orderby' => 'meta_value'));
241 break;
242 case 'template':
243 $vars = array_merge($vars, array('meta_key' => 'maxgallery_template', 'orderby' => 'meta_value'));
244 break;
245 }
246 }
247
248 return $vars;
249 }
250
251 function deactivate() {
252 delete_option(MAXGALLERIA_VERSION_KEY);
253
254 if(!defined('PRESERVE_MAXGALLERIA_TEMPLATE')) {
255 // Delete the gallery post type template file from the theme directory
256 $file = $this->get_theme_dir() . '/single-maxgallery.php';
257 unlink($file);
258 }
259
260 flush_rewrite_rules();
261 }
262
263 public function define_gallery_columns($columns) {
264 $columns = apply_filters(MAXGALLERIA_FILTER_GALLERY_COLUMNS, array(
265 'cb' => '<input type="checkbox" />',
266 'title' => esc_html__('Title', 'maxgalleria'),
267 'thumbnail' => esc_html__('Thumbnail', 'maxgalleria'),
268 'type' => esc_html__('Type', 'maxgalleria'),
269 'template' => esc_html__('Template', 'maxgalleria'),
270 'number' => esc_html__('Number of Media', 'maxgalleria'),
271 'shortcode' => esc_html__('Shortcode', 'maxgalleria'),
272 'date' => esc_html__('Date', 'maxgalleria')
273 ));
274
275 return $columns;
276 }
277
278 public function define_sortable_gallery_columns($columns) {
279 // Title and Date are sortable by default
280
281 $columns['type'] = 'type';
282 $columns['template'] = 'template';
283 $columns['number'] = 'number';
284
285 return $columns;
286 }
287
288 public function do_activation($network_wide) {
289 if ($network_wide) {
290 $this->call_function_for_each_site(array($this, 'activate'));
291 }
292 else {
293 $this->activate();
294 }
295 }
296
297 public function do_deactivation($network_wide) {
298 if ($network_wide) {
299 $this->call_function_for_each_site(array($this, 'deactivate'));
300 }
301 else {
302 $this->deactivate();
303 }
304 }
305
306 public function enqueue_admin_print_scripts() {
307 if ($this->admin_page_is_maxgallery_post_type()) {
308 wp_enqueue_script('jquery');
309 wp_enqueue_script('jquery-migrate', ABSPATH . WPINC . '/js/jquery/jquery-migrate.min.js', array('jquery'));
310 wp_enqueue_script('thickbox');
311 wp_enqueue_script('media-upload');
312
313 // For the media uploader
314 wp_enqueue_media();
315 wp_enqueue_script('maxgalleria-media-script', MAXGALLERIA_PLUGIN_URL . '/js/media.js', array('jquery'));
316 wp_enqueue_script('maxgalleria-media-script');
317 wp_localize_script('maxgalleria-media-script', 'mg_media',
318 array('nonce' => wp_create_nonce(MG_META_NONCE )
319 ));
320
321
322 // Other stuff
323 wp_enqueue_script('jquery-ui-core');
324 wp_enqueue_script('jquery-ui-tabs');
325
326 wp_enqueue_script('maxgalleria-topbox', MAXGALLERIA_PLUGIN_URL . '/libs/topbox/js/topbox.js', array('jquery'));
327 wp_enqueue_script('maxgalleria-colorpicker-js', MAXGALLERIA_PLUGIN_URL . '/js/colpick/colpick.js', array('jquery'), null );
328
329 wp_enqueue_style('fontawesome', MAXGALLERIA_PLUGIN_URL . '/libs/fontawesome-free-6.0.0-web/css/all.min.css');
330
331 $screen = get_current_screen();
332 if($screen->id == 'edit-maxgallery') {
333 $show_gallery_ad = get_option('show_gallery_ad', "on");
334 wp_enqueue_script('maxgalleria-promo', MAXGALLERIA_PLUGIN_URL . '/js/promo.js', array('jquery'));
335 wp_localize_script( 'maxgalleria-promo', 'mg_promo',
336 array( 'pluginurl' => MAXGALLERIA_PLUGIN_URL,
337 'show_promo' => $show_gallery_ad,
338 'nonce' => wp_create_nonce(MG_META_NONCE ),
339 'admin_url' => admin_url('admin-ajax.php'),
340 'addon_link' => MG_ADDON_PAGE_LINK,
341 'carousel_link' => MG_IMAGE_CAROUSEL_LINK,
342 'albums_link' => MG_ALBUMS_LINK,
343 'video_showcase_link' => MG_VIDEO_SHOWCASE_LINK,
344 'slick_slider_link' => MG_SLICK_SLIDER_LINK,
345 'masonry_link' => MG_MASONRY_LINK,
346 'image_showcase_link' => MG_IMAGE_SHOWCASE_LINK,
347 'image_slider_link' => MG_IMAGE_SLIDER_LINK,
348 'facebook_link' => MG_FACEBOOK_LINK,
349 'vimeo_link' => MG_VIMEO_LINK,
350 'instgram_link' => MG_INSTAGRAM_LINK,
351 'flickr_link' => MG_FLICKR_LINK,
352 'fwgrid_link' => MG_FWGRID_LINK,
353 'mg_md_link' => MG_MD_LINK,
354 'hero_link' => MG_HERO_LINK
355 ));
356 }
357 } else {
358 wp_enqueue_script('maxgalleria-promo', MAXGALLERIA_PLUGIN_URL . '/js/tm.js', array('jquery'));
359 if(defined('MAXGALLERIA_ALBUMS_PLUGIN_URL')) {
360 $albumsurls = MAXGALLERIA_ALBUMS_PLUGIN_URL;
361 } else {
362 $albumsurls = "";
363 }
364 wp_localize_script( 'maxgalleria-promo', 'mg_promo',
365 array( 'pluginurl' => MAXGALLERIA_PLUGIN_URL,
366 'albumsurl' => $albumsurls
367 ));
368
369 }
370 }
371
372 public function enqueue_admin_print_styles() {
373
374 $screen = get_current_screen();
375
376 if ($this->admin_page_is_maxgallery_post_type()) {
377 wp_enqueue_style('thickbox');
378 wp_enqueue_style('maxgalleria-jquery-ui', MAXGALLERIA_PLUGIN_URL . '/libs/jquery-ui/jquery-ui.css');
379 wp_enqueue_style('maxgalleria-topbox', MAXGALLERIA_PLUGIN_URL . '/libs/topbox/css/topbox.css');
380 wp_enqueue_style('maxgalleria', MAXGALLERIA_PLUGIN_URL . '/maxgalleria.css');
381 wp_enqueue_style('maxgalleria-colorpicker', MAXGALLERIA_PLUGIN_URL . '/js/colpick/css/colpick.css');
382 wp_enqueue_style('maxgalleria-md-css', MAXGALLERIA_PLUGIN_URL . '/admin/material-plugin.css');
383
384 }
385 wp_enqueue_style('mg-notice', MAXGALLERIA_PLUGIN_URL . '/admin/mg-notice.css');
386
387 if($screen->id == 'maxgallery_page_mg-upgrade-to-pro') {
388 wp_enqueue_style('mg-upgrade', MAXGALLERIA_PLUGIN_URL . '/admin/mg-upgrade-to-pro.css');
389
390 wp_enqueue_style('mg-foundation', MAXGALLERIA_PLUGIN_URL . '/libs/foundation/foundation-float.min.css');
391 }
392
393 }
394
395 public function get_all_addons() {
396 return $this->_addons;
397 }
398
399 public function get_media_source_addons() {
400 $media_source_addons = array();
401
402 foreach ($this->_addons as $addon) {
403 if ($addon['type'] == 'media_source') {
404 array_push($media_source_addons, $addon);
405 }
406 }
407
408 return $media_source_addons;
409 }
410
411 public function get_template_addons() {
412 $template_addons = array();
413
414 foreach ($this->_addons as $addon) {
415 if ($addon['type'] == 'template') {
416 array_push($template_addons, $addon);
417 }
418 }
419
420 return $template_addons;
421 }
422
423 public function get_template_name($template_key) {
424 $template_name = '';
425 $templates = $this->get_template_addons();
426
427 foreach ($templates as $template) {
428 if ($template['key'] == $template_key) {
429 $template_name = $template['name'];
430 break;
431 }
432 }
433
434 return $template_name;
435 }
436
437 public function get_theme_dir() {
438 if(is_child_theme())
439 return WP_CONTENT_DIR . '/themes/' . get_stylesheet();
440 else
441 return WP_CONTENT_DIR . '/themes/' . get_template();
442 }
443
444 public function hide_add_new() {
445 global $submenu;
446 unset($submenu['edit.php?post_type=' . MAXGALLERIA_POST_TYPE][10]);
447 }
448
449 public function bootstrap() {
450 $this->load_textdomain();
451 $this->initialize_properties();
452 $this->register_media_sources();
453 $this->register_templates();
454 }
455
456 public function initialize_properties() {
457 // The order doesn't really matter, except maxgallery-options.php must be included first so
458 // that the MaxGalleryOptions class can be created in other parts of the system as needed
459
460 require_once 'maxgallery-options.php';
461 require_once 'maxgalleria-admin.php';
462 require_once 'maxgalleria-common.php';
463 require_once 'maxgalleria-meta.php';
464 require_once 'maxgalleria-nextgen.php';
465 require_once 'maxgalleria-settings.php';
466 require_once 'maxgalleria-shortcode.php';
467 require_once 'maxgalleria-shortcode-thumb.php';
468 require_once 'maxgalleria-new-gallery.php';
469 require_once 'maxgalleria-image-gallery.php';
470 require_once 'maxgalleria-video-gallery.php';
471 require_once 'widgets/gallery-widget.php';
472 require_once 'widgets/gallery-thumb-widget.php';
473
474 $this->admin = new MaxGalleriaAdmin();
475 $this->common = new MaxGalleriaCommon();
476 $this->meta = new MaxGalleriaMeta();
477 $this->nextgen = new MaxGalleriaNextGen();
478 $this->settings = new MaxGalleriaSettings();
479 $this->shortcode = new MaxGalleriaShortcode();
480 $this->shortcode_thumb = new MaxGalleriaShortcodeThumb();
481 $this->new_gallery = new MaxGalleriaNewGallery();
482 $this->image_gallery = new MaxGalleriaImageGallery();
483 $this->video_gallery = new MaxGalleriaVideoGallery();
484 $this->gallery_widget = new MaxGalleriaGalleryWidget();
485 $this->gallery_thumb_widget = new MaxGalleriaGalleryThumbWidget();
486 }
487
488 public function load_textdomain() {
489 load_plugin_textdomain('maxgalleria', false, dirname(plugin_basename(__FILE__)) . '/languages/');
490 }
491
492 public function media_button() {
493 global $pagenow, $wp_version;
494 $output = '';
495
496 // Only run in post/page creation and edit screens
497 if (in_array($pagenow, array('post.php', 'page.php', 'post-new.php', 'post-edit.php'))) {
498 $title = esc_html__('MaxGalleria Gallery', 'maxgalleria');
499 $icon = MAXGALLERIA_PLUGIN_URL . '/images/maxgalleria-icon-16.png';
500 $img = '<span class="wp-media-buttons-icon" style="background-image: url(' . $icon . '); width: 16px; height: 16px; margin-top: 1px;"></span>';
501 $output = '<a href="#TB_inline?width=640&inlineId=select-maxgallery-container" class="thickbox button" title="' . $title . '" style="padding-left: .4em;">' . $img . ' ' . $title . '</a>';
502 }
503
504 echo wp_kses_post($output);
505 }
506
507 public function media_button_admin_footer() {
508 require_once 'maxgalleria-media-button.php';
509 }
510
511 public function register_gallery_post_type() {
512 $slug = $this->settings->get_rewrite_slug();
513 $exclude_from_search = $this->settings->get_exclude_galleries_from_search();
514 $exclude_from_search = $exclude_from_search == 'on' ? true : false;
515
516 $labels = apply_filters(MAXGALLERIA_FILTER_GALLERY_POST_TYPE_LABELS, array(
517 'name' => esc_html__('MaxGalleria', 'maxgalleria'),
518 'singular_name' => esc_html__('Gallery', 'maxgalleria'),
519 'add_new' => esc_html__('Add New', 'maxgalleria'),
520 'add_new_item' => esc_html__('Add New Gallery', 'maxgalleria'),
521 'edit_item' => esc_html__('Edit Gallery', 'maxgalleria'),
522 'new_item' => esc_html__('New Gallery', 'maxgalleria'),
523 'view_item' => esc_html__('View Gallery', 'maxgalleria'),
524 'search_items' => esc_html__('Search Galleries', 'maxgalleria'),
525 'not_found' => esc_html__('No galleries found', 'maxgalleria'),
526 'not_found_in_trash' => esc_html__('No galleries found in trash', 'maxgalleria'),
527 'parent_item_colon' => ''
528 ));
529
530 $args = apply_filters(MAXGALLERIA_FILTER_GALLERY_POST_TYPE_ARGS, array(
531 'labels' => $labels,
532 'public' => true,
533 'publicly_queryable' => true,
534 'show_ui' => true,
535 'show_in_menu' => true,
536 'query_var' => true,
537 'menu_icon' => MAXGALLERIA_PLUGIN_URL . '/images/maxgalleria-icon-16.png',
538 'rewrite' => array('slug' => $slug),
539 'capability_type' => 'post',
540 'hierarchical' => false,
541 'supports' => array('title', 'thumbnail'),
542 'taxonomies' => array('category', 'post_tag'),
543 'exclude_from_search' => $exclude_from_search
544 ));
545
546 register_post_type(MAXGALLERIA_POST_TYPE, $args);
547 }
548
549 public function register_addon($addon) {
550 array_push($this->_addons, $addon);
551 }
552
553 public function register_media_sources() {
554 // YouTube
555 require_once MAXGALLERIA_PLUGIN_DIR . '/addons/media-sources/youtube/youtube.php';
556 $youtube = new MaxGalleriaYouTube();
557 $youtube_addon = array(
558 'key' => $youtube->addon_key,
559 'name' => $youtube->addon_name,
560 'type' => $youtube->addon_type,
561 'subtype' => $youtube->addon_subtype,
562 'settings' => $youtube->addon_settings
563 );
564 $this->register_addon($youtube_addon);
565 }
566
567 public function register_templates() {
568 // Image Tiles template
569 require_once MAXGALLERIA_PLUGIN_DIR . '/addons/templates/image-tiles/image-tiles.php';
570 $image_tiles = new MaxGalleriaImageTiles();
571 $image_tiles_addon = array(
572 'key' => $image_tiles->addon_key,
573 'name' => $image_tiles->addon_name,
574 'type' => $image_tiles->addon_type,
575 'subtype' => $image_tiles->addon_subtype,
576 'settings' => $image_tiles->addon_settings,
577 'image' => $image_tiles->addon_image,
578 'output' => $image_tiles->addon_output
579 );
580 $this->register_addon($image_tiles_addon);
581
582 // Video Tiles template
583 require_once MAXGALLERIA_PLUGIN_DIR . '/addons/templates/video-tiles/video-tiles.php';
584 $video_tiles = new MaxGalleriaVideoTiles();
585 $video_tiles_addon = array(
586 'key' => $video_tiles->addon_key,
587 'name' => $video_tiles->addon_name,
588 'type' => $video_tiles->addon_type,
589 'subtype' => $video_tiles->addon_subtype,
590 'settings' => $video_tiles->addon_settings,
591 'image' => $video_tiles->addon_image,
592 'output' => $video_tiles->addon_output
593 );
594 $this->register_addon($video_tiles_addon);
595 }
596
597 public function register_widgets() {
598 register_widget('MaxGalleriaGalleryWidget');
599 register_widget('MaxGalleriaGalleryThumbWidget');
600 }
601
602 public function set_activation_hooks() {
603 register_activation_hook(__FILE__, array($this, 'do_activation'));
604 register_deactivation_hook(__FILE__, array($this, 'do_deactivation'));
605 }
606
607 public function set_global_constants() {
608 define('MAXGALLERIA_VERSION_KEY', 'maxgalleria_version');
609 define('MAXGALLERIA_VERSION_NUM', '6.5.3');
610 define('MAXGALLERIA_PLUGIN_NAME', trim(dirname(plugin_basename(__FILE__)), '/'));
611 define('MAXGALLERIA_PLUGIN_DIR', WP_PLUGIN_DIR . '/' . MAXGALLERIA_PLUGIN_NAME);
612 define('MAXGALLERIA_PLUGIN_URL', rtrim(plugin_dir_url(__FILE__), '/'));
613 define('MAXGALLERIA_POST_TYPE', 'maxgallery');
614 define('MAXGALLERIA_SETTINGS', admin_url() . 'edit.php?post_type=' . MAXGALLERIA_POST_TYPE . '&page=maxgalleria-settings');
615 define('MAXGALLERIA_META_IMAGE_THUMB_SMALL', 'maxgallery-meta-image-thumb-small');
616 define('MAXGALLERIA_META_IMAGE_THUMB_MEDIUM', 'maxgallery-meta-image-thumb-medium');
617 define('MAXGALLERIA_META_IMAGE_THUMB_LARGE', 'maxgallery-meta-image-thumb-large');
618 define('MAXGALLERIA_META_VIDEO_THUMB_SMALL', 'maxgallery-meta-video-thumb-small');
619 define('MAXGALLERIA_META_VIDEO_THUMB_MEDIUM', 'maxgallery-meta-video-thumb-medium');
620 define('MAXGALLERIA_META_VIDEO_THUMB_LARGE', 'maxgallery-meta-video-thumb-large');
621 define('MAXGALLERIA_THUMB_SHAPE_LANDSCAPE', 'landscape');
622 define('MAXGALLERIA_THUMB_SHAPE_PORTRAIT', 'portrait');
623 define('MAXGALLERIA_THUMB_SHAPE_SQUARE', 'square');
624 define('MAXGALLERIA_SETTING_REWRITE_SLUG', 'maxgalleria_setting_rewrite_slug');
625 define('MAXGALLERIA_SETTING_EXCLUDE_GALLERIES_FROM_SEARCH', 'maxgalleria_setting_exlude_galleries_from_search');
626 define('MAXGALLERIA_SETTING_DEFAULT_IMAGE_GALLERY_TEMPLATE', 'maxgalleria_setting_default_image_gallery_template');
627 define('MAXGALLERIA_SETTING_DEFAULT_VIDEO_GALLERY_TEMPLATE', 'maxgalleria_setting_default_video_gallery_template');
628 define('MAXGALLERIA_ADMIN_NOTICE', 'maxgalleria_admin_notice-2');
629 define('MAXGALLERIA_REVIEW_NOTICE', 'maxgalleria_review_notice');
630 define('MG_META_NONCE', 'maxgalleria_meta_nonce');
631 define('MG_WP_CONTENT_FOLDER_NAME', basename(WP_CONTENT_DIR));
632
633 define('MG_EDD_SHOP_URL', 'https://maxgalleria.com/');
634 define('MAXGALLERIA_SETTING_SHOW_ADDON_PAGE', 'maxgalleria_setting_default_show_addons_page');
635
636 if(!defined('MAXGALLERIA_MEDIA_LIBRARY_SRC_FIX'))
637 define("MAXGALLERIA_MEDIA_LIBRARY_SRC_FIX", "mgmlp_src_fix");
638
639 define('MG_ADDON_PAGE_LINK',
640 'https://maxgalleria.com/addons/?utm_source=MGGetAddon&utm_medium=tout&utm_campaign=tout');
641 define('MG_IMAGE_CAROUSEL_LINK',
642 'https://maxgalleria.com/downloads/maxgalleria-image-carousel/?utm_source=MGGetAddon&amp;utm_medium=image-carousel&amp;utm_campaign=image-carousel');
643 define('MG_ALBUMS_LINK',
644 'https://maxgalleria.com/downloads/maxgalleria-albums/?utm_source=MGGetAddon&amp;utm_medium=albums&amp;utm_campaign=albums' );
645 define('MG_VIDEO_SHOWCASE_LINK',
646 'https://maxgalleria.com/downloads/maxgalleria-video-showcase/?utm_source=MGGetAddon&utm_medium=videoshowcase&utm_campaign=videoshowcase');
647 define('MG_SLICK_SLIDER_LINK',
648 'https://maxgalleria.com/downloads/slick-slider-for-wordpress/?utm_source=MGGetAddon&utm_medium=slick&utm_campaign=slick' );
649 define('MG_MASONRY_LINK',
650 'https://maxgalleria.com/downloads/masonry-pinterest-like-layout/?utm_source=MGGetAddon&utm_medium=masonry&utm_campaign=masonry');
651 define('MG_IMAGE_SHOWCASE_LINK',
652 'https://maxgalleria.com/downloads/maxgalleria-image-showcase/?utm_source=MGGetAddon&utm_medium=imageshowcase&utm_campaign=imageshowcase');
653 define ('MG_IMAGE_SLIDER_LINK',
654 'https://maxgalleria.com/downloads/maxgalleria-image-slider/?utm_source=MGGetAddon&utm_medium=imageslider&utm_campaign=imageslider');
655 define('MG_FACEBOOK_LINK',
656 'https://maxgalleria.com/downloads/maxgalleria-facebook/?utm_source=MGGetAddon&utm_medium=facebook&utm_campaign=facebook');
657 define('MG_VIMEO_LINK',
658 'https://maxgalleria.com/downloads/maxgalleria-vimeo/?utm_source=MGGetAddon&utm_medium=vimeo&utm_campaign=vimeo');
659 define('MG_INSTAGRAM_LINK',
660 'https://maxgalleria.com/downloads/maxgalleria-instagram/?utm_source=MGGetAddon&utm_medium=instagram&utm_campaign=instagram');
661 define('MG_FLICKR_LINK',
662 'https://maxgalleria.com/downloads/maxgalleria-flickr/?utm_source=MGGetAddon&utm_medium=flickr&utm_campaign=flickr');
663 define('MLPP_LINK', 'https://maxgalleria.com/downloads/media-library-plus-pro/?utm_source=MGGetAddon ');
664 define('MG_FWGRID_LINK',
665 'https://maxgalleria.com/downloads/full-width-grid/?utm_source=MGGetAddon&utm_medium=fwgrid&utm_campaign=slick' );
666 define('MG_MD_LINK',
667 'https://maxgalleria.com/downloads/material-deisgn/?utm_source=MGGetAddon&utm_medium=material-deisgn&utm_campaign=slick' );
668 define('MG_HERO_LINK',
669 'https://www.maxgalleria.com/hero-slider-wordpress/?utm_source=MGGetAddon&utm_medium=hero-slider&utm_campaign=hero' );
670
671
672 // Bring in all the actions and filters
673 require_once 'maxgalleria-hooks.php';
674 }
675
676 public function set_icon_edit_image() {
677 if ($this->admin_page_is_maxgallery_post_type()) {
678 echo '<style>';
679 echo '#icon-edit { background: url("'. MAXGALLERIA_PLUGIN_URL . '/images/maxgalleria-icon-32.png' . '") no-repeat transparent; }';
680 echo '</style>';
681 }
682 }
683
684 public function setup_hooks() {
685 add_action('init', array($this, 'bootstrap'), 0);
686 add_action('init', array($this, 'register_gallery_post_type'));
687 add_action('init', array($this, 'display_mg_admin_notice'));
688 add_filter('plugin_action_links', array($this, 'create_plugin_action_links'), 10, 2);
689 add_action('admin_print_scripts', array($this, 'enqueue_admin_print_scripts'));
690 add_action('admin_print_styles', array($this, 'enqueue_admin_print_styles'));
691 add_action('admin_head', array($this, 'set_icon_edit_image'));
692 add_action('admin_menu', array($this, 'hide_add_new'));
693 add_filter('manage_edit-' . MAXGALLERIA_POST_TYPE . '_columns', array($this, 'define_gallery_columns'));
694 add_filter('manage_edit-' . MAXGALLERIA_POST_TYPE . '_sortable_columns', array($this, 'define_sortable_gallery_columns'));
695 add_action('manage_posts_custom_column', array($this, 'create_gallery_columns'));
696 add_filter('request', array($this, 'create_sortable_gallery_columns'));
697 add_filter('media_upload_tabs', array($this, 'set_media_upload_tabs'), 50, 1);
698 add_filter('media_view_strings', array($this, 'set_media_view_strings'), 50, 1);
699 add_filter('post_mime_types', array($this, 'set_post_mime_types'), 50, 1);
700 add_filter('upload_mimes', array($this, 'set_upload_mimes'), 50, 1);
701 add_action('media_buttons', array($this, 'media_button'));
702 add_action('admin_footer', array($this, 'media_button_admin_footer'));
703 add_action('widgets_init', array($this, 'register_widgets'));
704 add_action('after_switch_theme', array($this, 'copy_template'));
705
706 if(!defined('ATTACHMENT_QUERY_OFF')) {
707 add_filter('ajax_query_attachments_args', array($this, 'exclude_album_gallery_attachments_from_media_library'));
708 add_action( 'pre_get_posts', array($this, 'modify_attachments'));
709 }
710
711 //add_action( 'pre_get_posts', array($this, 'limit_contributor_access'));
712
713 add_action( 'admin_menu', array($this, 'limit_contributor_access'));
714
715 //add_action( 'admin_bar_menu', array($this, 'current_maxgalleria_gallery'), 999 );
716
717
718 // this is not working yet:
719 // check daily for the template in the theme folder; copy and update permalinks if missing.
720 // if ( ! wp_next_scheduled( 'mg_task_hook' ) ) {
721 // wp_schedule_event( time(), 'daily', 'mg_task_hook' );
722 // }
723 //
724 // add_action( 'mg_task_hook', array($this, 'mg_daily_check') );
725
726 add_action('admin_head', array($this, 'admin_head_hook'), 1);
727
728 //add_filter('mce_buttons', array($this, 'tinymce_button'));
729 //add_filter('mce_external_plugins', array($this, 'add_tinymce_button'));
730
731 add_action('enqueue_block_editor_assets', array($this, 'load_mg_block'));
732
733 add_filter('query_vars', array($this, 'mg_query_vars'));
734
735 add_action('wp_ajax_mg_get_image_info', array($this, 'mg_get_image_info'));
736
737 add_action('wp_ajax_mg_save_image_info', array($this, 'mg_save_image_info'));
738
739 add_action('wp_ajax_mg_display_bulk_edit', array($this, 'mg_display_bulk_edit'));
740
741 add_action('wp_ajax_mg_save_bulk_info', array($this, 'mg_save_bulk_info'));
742
743 add_action('wp_ajax_mg_get_video_info', array($this, 'mg_get_video_info'));
744
745 add_action('wp_ajax_mg_save_video_info', array($this, 'mg_save_video_info'));
746
747 add_action('wp_ajax_mg_display_bulk_video', array($this, 'mg_display_bulk_video'));
748
749 add_action('wp_ajax_mg_save_bulk_video', array($this, 'mg_save_bulk_video'));
750
751 add_action('wp_ajax_mg_add_videos', array($this, 'mg_add_videos'));
752
753 // added 'display' to the list of accepted styles used in wp_kses_post()
754 add_filter( 'safe_style_css', function( $styles ) {
755 $styles[] = 'display';
756 return $styles;
757 } );
758
759 }
760
761 public function load_mg_block() {
762
763 global $wpdb;
764
765 wp_register_script(
766 'mg-block', MAXGALLERIA_PLUGIN_URL . '/block/index.js',
767 array('wp-blocks','wp-editor','wp-i18n'),
768 true
769 );
770
771 $sql = "SELECT ID, post_title from {$wpdb->prefix}posts where post_type = 'maxgallery' and post_status = 'publish' order by post_title";
772
773 $gallery_list = array();
774
775 $rows = $wpdb->get_results($sql);
776
777 if($rows) {
778 foreach($rows as $row) {
779 $gallery_list[] = array(
780 'id' => strval($row->ID),
781 'name' => $row->post_title
782 );
783 }
784 }
785
786 // keep as array, not casting as an object
787 wp_localize_script( 'mg-block', 'mg_block_info',
788 array( 'galleries' => $gallery_list));
789
790 wp_enqueue_script('mg-block');
791
792 }
793
794 function limit_contributor_access() {
795 if( !current_user_can( 'publish_posts' ) ):
796 remove_menu_page( 'edit.php?post_type=maxgallery' );
797 endif;
798 }
799
800 public function can_edit_maxgallery($post_id) {
801 $post_id = absint($post_id);
802
803 return $post_id
804 && get_post_type($post_id) === MAXGALLERIA_POST_TYPE
805 && current_user_can('edit_post', $post_id);
806 }
807
808 public function can_edit_gallery_attachment($attachment_id, $gallery_id = 0) {
809 $attachment = get_post(absint($attachment_id));
810
811 if (!$attachment || $attachment->post_type !== 'attachment') {
812 return false;
813 }
814
815 if (!current_user_can('edit_post', $attachment->ID)) {
816 return false;
817 }
818
819 $parent_id = $gallery_id ? absint($gallery_id) : absint($attachment->post_parent);
820
821 return $parent_id
822 && absint($attachment->post_parent) === $parent_id
823 && $this->can_edit_maxgallery($parent_id);
824 }
825
826 private function gallery_media_permission_denied() {
827 wp_die(esc_html__('You do not have permission to edit this gallery media.', 'maxgalleria'), '', array('response' => 403));
828 }
829
830 public function modify_attachments( $query ) {
831
832 if ( $this->is_media_library_attachment_request($query) ) {
833 if (!$this->media_library_folders_pro_is_filtering_media()) {
834 $query->set('meta_query', $this->get_album_gallery_attachment_exclusion($query->get('meta_query')));
835 }
836
837 if (!class_exists('eml') && !function_exists( 'wpuxss_get_eml_slug' ) && $this->is_ajax_query_attachments_request()) {
838 add_filter( 'posts_groupby', array($this, 'group_attachments') );
839 }
840 }
841 return $query;
842 }
843
844 public function exclude_album_gallery_attachments_from_media_library($args) {
845 if (!is_admin()) {
846 return $args;
847 }
848
849 if ($this->media_library_folders_pro_is_filtering_media()) {
850 return $args;
851 }
852
853 return $this->add_album_gallery_attachment_exclusion($args);
854 }
855
856 private function is_media_library_attachment_request($query = null) {
857 if (!is_admin()) {
858 return false;
859 }
860
861 $post_type = is_object($query) ? $query->get('post_type') : '';
862 if (is_array($post_type)) {
863 if (!in_array('attachment', $post_type, true)) {
864 return false;
865 }
866 }
867 else if ($post_type !== 'attachment' && $post_type !== '') {
868 return false;
869 }
870
871 global $pagenow;
872
873 if ($pagenow === 'upload.php' || $pagenow === 'media-upload.php') {
874 return true;
875 }
876
877 $action = isset($_REQUEST['action']) ? sanitize_text_field(wp_unslash($_REQUEST['action'])) : '';
878 return $pagenow === 'admin-ajax.php' && $action === 'query-attachments';
879 }
880
881 private function is_ajax_query_attachments_request() {
882 global $pagenow;
883
884 $action = isset($_REQUEST['action']) ? sanitize_text_field(wp_unslash($_REQUEST['action'])) : '';
885 return $pagenow === 'admin-ajax.php' && $action === 'query-attachments';
886 }
887
888 private function media_library_folders_pro_is_filtering_media() {
889 if (!class_exists('MGMediaLibraryFoldersPro')) {
890 return false;
891 }
892
893 $remove_folder_tree_option = defined('MAXGALLERIA_REMOVE_FT') ? MAXGALLERIA_REMOVE_FT : 'mlf_remove_ft';
894 if (get_option($remove_folder_tree_option, 'off') === 'on') {
895 return false;
896 }
897
898 global $maxgalleria_media_library_pro;
899
900 if (!is_object($maxgalleria_media_library_pro)) {
901 return true;
902 }
903
904 if (isset($maxgalleria_media_library_pro->license_valid)) {
905 return (bool) $maxgalleria_media_library_pro->license_valid;
906 }
907
908 if (method_exists($maxgalleria_media_library_pro, 'is_valid_license')) {
909 return (bool) $maxgalleria_media_library_pro->is_valid_license();
910 }
911
912 return true;
913 }
914
915 private function add_album_gallery_attachment_exclusion($args) {
916 $args['meta_query'] = $this->get_album_gallery_attachment_exclusion(isset($args['meta_query']) ? $args['meta_query'] : array());
917
918 return $args;
919 }
920
921 private function get_album_gallery_attachment_exclusion($meta_query) {
922 $exclude_album_gallery_attachments = array(
923 'key' => 'maxgallery_id',
924 'compare' => 'NOT EXISTS'
925 );
926
927 if (empty($meta_query)) {
928 return array($exclude_album_gallery_attachments);
929 }
930
931 return array(
932 'relation' => 'AND',
933 $meta_query,
934 $exclude_album_gallery_attachments
935 );
936 }
937
938 public function group_attachments($groupby) {
939 if ($groupby != '') {
940 $groupby .= " , guid";
941 } else {
942 $groupby .= " guid";
943 }
944 return $groupby;
945 }
946
947 public function set_media_upload_tabs($tabs) {
948 // Remove the "From URL", "Gallery", and "NextGEN" tabs from the media library popup.
949 // Only the tabs "From Computer" and "Media Library" should be shown.
950
951 if ($this->admin_page_is_maxgallery_post_type()) {
952 unset($tabs['type_url']); // From URL tab
953 unset($tabs['gallery']); // Gallery tab
954 unset($tabs['nextgen']); // NextGEN tab
955 }
956
957 return $tabs;
958 }
959
960 public function set_media_view_strings($strings) {
961 if ($this->admin_page_is_maxgallery_post_type()) {
962 // Remove these
963 unset($strings['insertFromUrlTitle']);
964 unset($strings['setFeaturedImageTitle']);
965 unset($strings['createGalleryTitle']);
966 unset($strings['createPlaylistTitle']);
967 unset($strings['createVideoPlaylistTitle']);
968
969 // Change these for better context in MaxGalleria galleries
970 $strings['insertMediaTitle'] = esc_html__('Add Images', 'maxgalleria');
971 $strings['insertIntoPost'] = esc_html__('Add to Gallery', 'maxgalleria');
972 $strings['uploadedToThisPost'] = esc_html__('Added to this gallery', 'maxgalleria');
973 }
974
975 return $strings;
976 }
977
978 public function set_post_mime_types($mime_types) {
979 // Remove the video and audio types
980
981 if ($this->admin_page_is_maxgallery_post_type()) {
982 unset($mime_types['video']);
983 unset($mime_types['audio']);
984 }
985
986 return $mime_types;
987 }
988
989 public function set_upload_mimes($mime_types) {
990 // Only allow image file type uploads. The complete list allowed by WordPress is
991 // located in the get_allowed_mime_types() function in wp-includes/functions.php.
992
993 if ($this->admin_page_is_maxgallery_post_type()) {
994 $mime_types = array(
995 'jpg|jpeg|jpe' => 'image/jpeg',
996 'gif' => 'image/gif',
997 'png' => 'image/png',
998 'bmp' => 'image/bmp',
999 'webp' => 'image/webp',
1000 'avif' => 'image/avif',
1001 'tif/tiff' => 'image/tiff'
1002 );
1003 }
1004
1005 return $mime_types;
1006 }
1007
1008 public function thickbox_l10n_fix() {
1009 // When combining scripts, localization is lost for thickbox.js, so we call this
1010 // function to fix it. See https://wordpress.org/support/topic/plugin-affecting-photo-galleriessliders
1011 // for more details.
1012 echo '<script type="text/javascript">';
1013 echo "var thickboxL10n = " . json_encode(array(
1014 'next' => esc_html__('Next >'),
1015 'prev' => esc_html__('< Prev'),
1016 'image' => esc_html__('Image'),
1017 'of' => esc_html__('of'),
1018 'close' => esc_html__('Close'),
1019 'noiframes' => esc_html__('This feature requires inline frames. You have iframes disabled or your browser does not support them.'),
1020 'loadingAnimation' => includes_url('js/thickbox/loadingAnimation.gif'),
1021 'closeImage' => includes_url('js/thickbox/tb-close.png')));
1022 echo '</script>';
1023 }
1024
1025 public function display_mg_admin_notice () {
1026
1027 $current_user_id = get_current_user_id();
1028
1029 $notice = get_user_meta( $current_user_id, MAXGALLERIA_ADMIN_NOTICE, true );
1030 $review = get_user_meta( $current_user_id, MAXGALLERIA_REVIEW_NOTICE, true );
1031
1032 //if( $notice !== 'off' )
1033 // add_action( 'admin_notices', array($this, 'mg_admin_notice' ));
1034
1035 if( $review !== 'off') {
1036 if($review === false)
1037 add_action( 'admin_notices', array($this, 'mg_review_notice' ));
1038 else {
1039 $now = date("Y-m-d");
1040 $review_time = strtotime($review);
1041 $now_time = strtotime($now);
1042 if($now_time > $review_time)
1043 add_action( 'admin_notices', array($this, 'mg_review_notice' ));
1044 }
1045 }
1046 }
1047
1048 // public function mg_admin_notice() {
1049 // if( current_user_can( 'manage_options' ) ) { ? >
1050 // <div class="updated notice">
1051 // </div>
1052 // < ?php
1053 // }
1054 // }
1055
1056 public function mg_review_notice() {
1057 if( current_user_can( 'manage_options' ) ) { ?>
1058 <div class="updated notice maxgalleria-notice">
1059 <div id='maxgallery_logo'></div>
1060 <div id='maxgalleria-notice-3'><p id='mg-notice-title'><?php esc_html_e( 'Rate us Please!', 'maxgalleria' ); ?></p>
1061 <p><?php esc_html_e( 'Your rating is the simplest way to support MaxGalleria. We really appreciate it!', 'maxgalleria' ); ?></p>
1062
1063 <ul id="review-notice-links">
1064 <li> <span class="dashicons dashicons-smiley"></span><a href="<?php echo admin_url(); ?>edit.php?post_type=maxgallery&page=mg-review-notice"><?php esc_html_e( "I've already left a review", "maxgalleria" ); ?></a></li>
1065 <li><span class="dashicons dashicons-calendar-alt"></span><a href="<?php echo admin_url(); ?>edit.php?post_type=maxgallery&page=mg-review-later"><?php esc_html_e( "Maybe Later", "maxgalleria" ); ?></a></li>
1066 <li><span class="dashicons dashicons-external"></span><a target="_blank" href="https://wordpress.org/support/view/plugin-reviews/maxgalleria?filter=5"><?php esc_html_e( "Sure! I'd love to!", "maxgalleria" ); ?></a></li>
1067 </ul>
1068 </div>
1069 <a class="dashicons dashicons-dismiss close-mg-notice" href="<?php echo admin_url(); ?>edit.php?post_type=maxgallery&page=mg-review-notice"></a>
1070
1071 </div>
1072 <?php
1073 }
1074 }
1075
1076 private function mg_daily_check() {
1077
1078 $source = MAXGALLERIA_PLUGIN_DIR . '/single-maxgallery.php';
1079 $destination = $this->get_theme_dir() . '/single-maxgallery.php';
1080
1081 if(!file_exists($destination)) {
1082 copy($source, $destination);
1083 flush_rewrite_rules();
1084 }
1085
1086 }
1087
1088 public function mg_get_attachment_url($attachment, $uploads) {
1089 $url = "";
1090 if ( $file = get_post_meta( $attachment->ID, '_wp_attached_file', true) ) {
1091 if ( 0 === strpos( $file, $uploads['basedir'] ) ) {
1092 // Replace file location with url location.
1093 $url = str_replace($uploads['basedir'], $uploads['baseurl'], $file);
1094 } elseif ( false !== strpos($file, MG_WP_CONTENT_FOLDER_NAME . '/uploads') ) {
1095 $url = $uploads['baseurl'] . substr( $file, strpos($file, MG_WP_CONTENT_FOLDER_NAME . '/uploads') + 18 );
1096 } else {
1097 // It's a newly-uploaded file, therefore $file is relative to the basedir.
1098 $url = $uploads['baseurl'] . "/$file";
1099 }
1100 } else {
1101 $url = $attachment->guid;
1102 }
1103
1104 // On SSL front-end, URLs should be HTTPS.
1105 if ( is_ssl() && ! is_admin() && 'wp-login.php' !== $GLOBALS['pagenow'] ) {
1106 $url = set_url_scheme( $url );
1107 }
1108
1109 $url = apply_filters( 'wp_get_attachment_url', $url, $attachment->ID );
1110
1111 return $url;
1112 }
1113
1114 // check if MG is already running on the site
1115 private function check_for_mg_posts() {
1116 global $wpdb;
1117
1118 $sql = "SELECT ID from {$wpdb->prefix}posts where post_type = 'maxgallery' limit 0, 1";
1119
1120 if($wpdb->get_row($sql))
1121 return true;
1122 else
1123 return false;
1124
1125 }
1126
1127 public function current_maxgalleria_gallery( $wp_admin_bar ) {
1128
1129 global $wpdb;
1130
1131 if(current_user_can( 'manage_options')) {
1132
1133 $sql = "select option_value from {$wpdb->prefix}options where option_name = 'mg_current_gallery'";
1134
1135 $gallery_id = $wpdb->get_var($sql);
1136
1137 if($gallery_id != null) {
1138
1139 $args = array(
1140 'id' => 'mg_current_gallery',
1141 'title' => 'Current Gallery',
1142 'href' => admin_url() . "post.php?post={$gallery_id}&action=edit",
1143 );
1144
1145 $wp_admin_bar->add_node( $args );
1146 }
1147 }
1148 }
1149
1150 public function admin_head_hook() {
1151 global $wp_query;
1152 if(isset($wp_query->post->ID)) {
1153 if(MAXGALLERIA_POST_TYPE == get_post_type($wp_query->post->ID)) {
1154 add_filter( 'admin_body_class', array($this, 'mg_body_class'));
1155 }
1156 }
1157 }
1158
1159 public function mg_body_class($classes) {
1160 $mg_class = "maxgalery-pt";
1161 if(is_array($classes))
1162 $classes[] = $mg_class;
1163 else
1164 $classes .= " " . $mg_class;
1165 return $classes;
1166 }
1167
1168 public function tinymce_button($buttons){
1169 array_push($buttons, "maxgalleria_tinymce");
1170 return $buttons;
1171 }
1172
1173 public function add_tinymce_button($plugin_array){
1174 $js_url = trailingslashit(MAXGALLERIA_PLUGIN_URL . '/js/');
1175 $plugin_array['maxgalleria_tinymce'] = $js_url . 'maxgalleria_tinymce.js' ;
1176 return $plugin_array;
1177 }
1178
1179 // function by stever, https://www.php.net/strip%20tags
1180 public function stripUnwantedTagsAndAttrs($html_str){
1181 $content = mb_convert_encoding($html_str, 'HTML-ENTITIES', 'UTF-8');
1182 $xml = new DOMDocument('utf8');
1183 //$xml->encoding = 'utf8';
1184 //Suppress warnings: proper error handling is beyond scope of example
1185 libxml_use_internal_errors(true);
1186 //List the tags you want to allow here, NOTE you MUST allow html and body otherwise entire string will be cleared
1187 $allowed_tags = array("b", "br", "em", "hr", "i", "li", "ol", "p", "s", "span", "table", "tr", "td", "u", "ul", "strong");
1188 //List the attributes you want to allow here
1189 $allowed_attrs = array ("class", "id", "style");
1190 if (!strlen($content)){return false;}
1191 if ($xml->loadHTML($content, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD)){
1192 foreach ($xml->getElementsByTagName("*") as $tag){
1193 if (!in_array($tag->tagName, $allowed_tags)){
1194 $tag->parentNode->removeChild($tag);
1195 }else{
1196 foreach ($tag->attributes as $attr){
1197 if (!in_array($attr->nodeName, $allowed_attrs)){
1198 $tag->removeAttribute($attr->nodeName);
1199 }
1200 }
1201 }
1202 }
1203 }
1204 return $xml->saveHTML();
1205 }
1206
1207 // removes unneeded <p></p> tags from HTML string
1208 // used to allow HTML in image captions
1209 public function remove_ptags($html_string) {
1210 $begin_pg_tag = strpos($html_string, '<p>');
1211 if($begin_pg_tag > -1) {
1212 $html_string = substr($html_string, $begin_pg_tag+3);
1213 $end_pg_tag = strrpos($html_string, '</p>');
1214 $updated_string = substr($html_string, 0 , $end_pg_tag);
1215 } else {
1216 $updated_string = $html_string;
1217 }
1218 return $updated_string;
1219 }
1220
1221 // determines is the license has expired
1222 public function is_valid_license($license_expires_option, $license_status_option) {
1223
1224 $expriation_date = get_option($license_expires_option);
1225 $license_status = get_option($license_status_option, 'inactive');
1226
1227 if($license_status == 'inactive');
1228 return $license_status;
1229
1230 $expire_time = strtotime($expriation_date);
1231
1232 $currnet_date_time = date('Y-m-d H:i:s');
1233 $today_time = strtotime($currnet_date_time);
1234 if($expire_time < $today_time || $license_status != 'valid') {
1235 $valid = 'expired';
1236 } else {
1237 $valid = 'valid';
1238 }
1239 return $valid;
1240 }
1241
1242 public function mg_query_vars( $vars ) {
1243 $vars[] = 'mg_page';
1244 return $vars;
1245 }
1246
1247 public function mg_get_image_info() {
1248
1249 if (isset($_POST) && check_admin_referer($this->image_gallery->nonce_image_edit['action'], $this->image_gallery->nonce_image_edit['name'])) {
1250
1251 if ((isset($_POST['image_id'])) && (strlen(trim($_POST['image_id'])) > 0))
1252 $image_id = trim(sanitize_text_field($_POST['image_id']));
1253 else
1254 $image_id = 0;
1255
1256 if($image_id) {
1257 if (!$this->can_edit_gallery_attachment($image_id)) {
1258 $this->gallery_media_permission_denied();
1259 }
1260
1261 $image = get_post($image_id);
1262
1263 $post_alt = get_post_meta($image_id, '_wp_attachment_image_alt', true);
1264
1265 $image_link = get_post_meta($image->ID, 'maxgallery_attachment_image_link', true);
1266
1267 $action_link_text = get_post_meta($image->ID, 'maxgallery_attachment_action_link_text', true);
1268
1269 $image_html = wp_get_attachment_image($image->ID, MAXGALLERIA_META_IMAGE_THUMB_LARGE);
1270
1271 $data = array ('post_title' => esc_html($image->post_title), 'post_alt' => esc_attr($post_alt), 'caption' => wp_kses_post($image->post_excerpt), 'image_link' => esc_url($image_link), 'action_link_text' => wp_kses_post($action_link_text), 'image_html' => $image_html);
1272 echo json_encode($data);
1273
1274 }
1275
1276 }
1277 die();
1278
1279 }
1280
1281 public function mg_save_image_info() {
1282
1283 if (isset($_POST) && check_admin_referer($this->image_gallery->nonce_image_edit['action'], $this->image_gallery->nonce_image_edit['name'])) {
1284
1285 if ((isset($_POST['image_id'])) && (strlen(trim($_POST['image_id'])) > 0))
1286 $image_id = trim(sanitize_text_field($_POST['image_id']));
1287 else
1288 $image_id = 0;
1289
1290 if ((isset($_POST['post_title'])) && (strlen(trim($_POST['post_title'])) > 0))
1291 $post_title = $this->stripUnwantedTagsAndAttrs(wp_filter_post_kses($_POST['post_title']));
1292 else
1293 $post_title = '';
1294
1295 if ((isset($_POST['caption'])) && (strlen(trim($_POST['caption'])) > 0))
1296 $post_excerpt = $this->stripUnwantedTagsAndAttrs(wp_filter_post_kses($_POST['caption']));
1297 else
1298 $post_excerpt = '';
1299
1300 if ((isset($_POST['post_alt'])) && (strlen(trim($_POST['post_alt'])) > 0))
1301 $post_alt = stripslashes(sanitize_text_field($_POST['post_alt']));
1302 else
1303 $post_alt = '';
1304
1305 if ((isset($_POST['image_link'])) && (strlen(trim($_POST['image_link'])) > 0)) {
1306 $image_link = sanitize_url($_POST['image_link']);
1307 if ($image_link != '' && !$this->common->string_starts_with($image_link, 'http://') && !$this->common->string_starts_with($image_link, 'https://') ) {
1308 $image_link = 'http://' . $image_link;
1309 }
1310 } else
1311 $image_link = '';
1312
1313 if ((isset($_POST['action_link_text'])) && (strlen(trim($_POST['action_link_text'])) > 0))
1314 $action_link_text = stripslashes(sanitize_text_field($_POST['action_link_text']));
1315 else
1316 $action_link_text = '';
1317
1318 if ((isset($_POST['template'])) && (strlen(trim($_POST['template'])) > 0))
1319 $template = stripslashes(sanitize_text_field($_POST['template']));
1320 else
1321 $template = '';
1322
1323
1324 if($image_id != 0) {
1325 if (!$this->can_edit_gallery_attachment($image_id)) {
1326 $this->gallery_media_permission_denied();
1327 }
1328
1329 $temp = array();
1330 $temp['ID'] = $image_id;
1331 $temp['post_title'] = $this->remove_ptags($post_title);
1332 $temp['post_excerpt'] = $this->remove_ptags($post_excerpt);
1333 wp_update_post($temp);
1334
1335 update_post_meta($image_id, '_wp_attachment_image_alt', $post_alt);
1336
1337 update_post_meta($image_id, 'maxgallery_attachment_image_link', stripslashes($image_link));
1338
1339 if($template === 'material-design') {
1340 update_post_meta($image_id, 'maxgallery_attachment_action_link_text', $action_link_text);
1341 }
1342
1343 }
1344
1345 }
1346
1347 die();
1348 }
1349
1350 public function mg_display_bulk_edit() {
1351
1352 $html = '';
1353
1354 if (isset($_POST) && check_admin_referer($this->image_gallery->nonce_image_edit['action'], $this->image_gallery->nonce_image_edit['name'])) {
1355
1356 if ((isset($_POST['image_ids'])) && (strlen(trim($_POST['image_ids'])) > 0))
1357 $image_ids = trim(sanitize_text_field($_POST['image_ids']));
1358 else
1359 $image_ids = '';
1360
1361 if (isset($image_ids)) {
1362 $image_ids_array = explode(',', $image_ids);
1363
1364 $html .= '<table id="bulk-edit-table" cellpadding="0" cellspacing="0">' . PHP_EOL;
1365 foreach ($image_ids_array as $image_id) {
1366 if (isset($image_id)) {
1367 $image = get_post($image_id);
1368 if (isset($image)) {
1369 if (!$this->can_edit_gallery_attachment($image->ID)) {
1370 $this->gallery_media_permission_denied();
1371 }
1372
1373 $html .= ' <tr>' . PHP_EOL;
1374 $html .= ' <td class="thumb">' . PHP_EOL;
1375 $html .= wp_get_attachment_image($image->ID, MAXGALLERIA_META_IMAGE_THUMB_SMALL) . PHP_EOL;
1376 $html .= ' <input type="hidden" name="image-edit-id[]" value="' . esc_attr($image->ID) . '" />' . PHP_EOL;
1377 $html .= ' </td>' . PHP_EOL;
1378 $html .= ' <td>' . PHP_EOL;
1379 $html .= ' <div class="fields">' . PHP_EOL;
1380 $html .= ' <div class="field">' . PHP_EOL;
1381 $html .= ' <div class="field-label">' . esc_html__('Title', 'maxgalleria') . '</div>' . PHP_EOL;
1382 $html .= ' <div class="field-value">' . PHP_EOL;
1383 $html .= ' <input type="text" name="image-edit-title[]" value="' . esc_html($image->post_title) .'" />' . PHP_EOL;
1384 $html .= ' </div>' . PHP_EOL;
1385 $html .= ' </div>' . PHP_EOL;
1386 $html .= ' <div class="clear"></div>' . PHP_EOL;
1387
1388 $html .= ' <div class="field">' . PHP_EOL;
1389 $html .= ' <div class="field-label">' . esc_html__('Alt Text', 'maxgalleria') . '</div>' . PHP_EOL;
1390 $html .= ' <div class="field-value">' . PHP_EOL;
1391 $html .= ' <input type="text" name="image-edit-alt[]" value="' . esc_html(get_post_meta($image->ID, '_wp_attachment_image_alt', true)) . '" />' . PHP_EOL;
1392 $html .= ' </div>' . PHP_EOL;
1393 $html .= ' </div>' . PHP_EOL;
1394 $html .= ' <div class="clear"></div>' . PHP_EOL;
1395
1396 $html .= ' <div class="field">' . PHP_EOL;
1397 $html .= ' <div class="field-label">'. esc_html__('Caption', 'maxgalleria') . '</div>' . PHP_EOL;
1398 $html .= ' <div class="field-value">' . PHP_EOL;
1399 $html .= ' <input type="text" name="image-edit-caption[]" value="' . esc_html($image->post_excerpt) . '" />' . PHP_EOL;
1400 $html .= ' </div>' . PHP_EOL;
1401 $html .= ' </div>' . PHP_EOL;
1402 $html .= ' <div class="clear"></div>' . PHP_EOL;
1403
1404 $html .= ' <div class="field">' . PHP_EOL;
1405 $html .= ' <div class="field-label">' . esc_html__('Link', 'maxgalleria') . '</div>' . PHP_EOL;
1406 $html .= ' <div class="field-value last">' . PHP_EOL;
1407 $html .= ' <input type="text" name="image-edit-link[]" value="' . esc_url(get_post_meta($image->ID, 'maxgallery_attachment_image_link', true)) . '" />' . PHP_EOL;
1408 $html .= ' </div>' . PHP_EOL;
1409 $html .= ' </div>' . PHP_EOL;
1410 $html .= ' <div class="clear"></div>' . PHP_EOL;
1411 $html .= ' </div>' . PHP_EOL;
1412 $html .= ' </td>' . PHP_EOL;
1413 $html .= '</tr>' . PHP_EOL;
1414 }
1415 }
1416 }
1417 $html .= '<tr>' . PHP_EOL;
1418 $html .= ' <td>' . PHP_EOL;
1419 $html .= ' <a class="btn btn-primary" id="bulk-save-button">' . esc_html__('Save Changes', 'maxgalleria') . '</a>' . PHP_EOL;
1420 $html .= ' <input type="button" class="btn" id="bulk-cancel-button" value="' . esc_html__('Cancel', 'maxgalleria') . '" />' . PHP_EOL;
1421 $html .= ' </td>' . PHP_EOL;
1422 $html .= ' <td></td>' . PHP_EOL;
1423 $html .= '</tr>' . PHP_EOL;
1424
1425 $html .= '</table>' . PHP_EOL;
1426
1427 }
1428
1429 $html .= wp_nonce_field($this->image_gallery->nonce_image_edit_bulk['action'], $this->image_gallery->nonce_image_edit_bulk['name']);
1430
1431 $allowed_html = array(
1432 'a' => array(
1433 'href' => array(),
1434 'title' => array(),
1435 'class' => array(),
1436 'id' => array()
1437 ),
1438 'img' => array(
1439 'src' => array(),
1440 'alt' => array(),
1441 'class' => array(),
1442 'loading' => array(),
1443 'srcset' => array(),
1444 'srcset' => array(),
1445 'sizes' => array(),
1446 'width' => array(),
1447 'height' => array()
1448 ),
1449 'input' => array(
1450 'id' => array(),
1451 'class' => array(),
1452 'value' => array(),
1453 'name' => array(),
1454 'type' => array()
1455 ),
1456 'div' => array(
1457 'class' => array()
1458 ),
1459 'br' => array(),
1460 'em' => array(),
1461 'strong' => array(),
1462 'table' => array(
1463 'id' => array(),
1464 'cellpadding' => array(),
1465 'cellspacing' => array()
1466 ),
1467 'tr' => array(),
1468 'td' => array(
1469 'class' => array()
1470 )
1471 );
1472
1473 echo wp_kses($html, $allowed_html);
1474
1475 }
1476
1477 die();
1478
1479 }
1480
1481 public function mg_save_bulk_info() {
1482
1483 global $wpdb;
1484 $form_data = '';
1485
1486 if (isset($_POST) && check_admin_referer($this->image_gallery->nonce_image_edit_bulk['action'], $this->image_gallery->nonce_image_edit_bulk['name'])) {
1487
1488 if ((isset($_POST['template'])) && (strlen(trim($_POST['template'])) > 0))
1489 $template = trim(sanitize_text_field($_POST['template']));
1490 else
1491 $template = '';
1492
1493 if ((isset($_POST['form_data'])) && (strlen(trim($_POST['form_data'])) > 0)) {
1494 parse_str($_POST['form_data'], $form_data);
1495
1496 $i = 0;
1497 if(isset($form_data['image-edit-id'])) {
1498 foreach($form_data['image-edit-id'] as $image_edit_id) {
1499 if (!$this->can_edit_gallery_attachment($image_edit_id)) {
1500 $this->gallery_media_permission_denied();
1501 }
1502
1503 $temp = array();
1504 $temp['ID'] = sanitize_text_field($image_edit_id);
1505 $post_title = $this->stripUnwantedTagsAndAttrs(wp_filter_post_kses($form_data['image-edit-title'][$i]));
1506 $post_excerpt = $this->stripUnwantedTagsAndAttrs(wp_filter_post_kses($form_data['image-edit-caption'][$i]));
1507 $temp['post_title'] = $this->remove_ptags($post_title);
1508 $temp['post_excerpt'] = $this->remove_ptags($post_excerpt);
1509
1510 wp_update_post($temp);
1511
1512 // Determine if we need to prepend http:// to the link
1513 $link = esc_url_raw($form_data['image-edit-link'][$i]);
1514 if (isset($link) && $link != '') {
1515 if (!$this->common->string_starts_with($link, 'http://') && !$this->common->string_starts_with($link, 'https://')) {
1516 $link = 'http://' . $link;
1517 }
1518 }
1519
1520 // Now update the meta
1521 update_post_meta($image_edit_id, '_wp_attachment_image_alt', stripslashes(sanitize_text_field($form_data['image-edit-alt'][$i])));
1522 update_post_meta($image_edit_id, 'maxgallery_attachment_image_link', stripslashes($link));
1523
1524 if($template == 'material-design' && isset($form_data['image-edit-action-text'])) {
1525 $action_link_text = sanitize_text_field($form_data['image-edit-action-text'][$i]);
1526 update_post_meta($image->ID, 'maxgallery_attachment_action_link_text', stripslashes($action_link_text));
1527 }
1528
1529 $i++;
1530 }
1531
1532 }
1533 }
1534 }
1535 die();
1536 }
1537
1538 public function mg_get_video_info() {
1539
1540 if (isset($_POST) && check_admin_referer($this->video_gallery->nonce_video_edit['action'], $this->video_gallery->nonce_video_edit['name'])) {
1541
1542 if ((isset($_POST['video_id'])) && (strlen(trim($_POST['video_id'])) > 0))
1543 $video_id = trim(sanitize_text_field($_POST['video_id']));
1544 else
1545 $video_id = 0;
1546
1547 if($video_id != 0) {
1548 if (!$this->can_edit_gallery_attachment($video_id)) {
1549 $this->gallery_media_permission_denied();
1550 }
1551
1552 $video = get_post($video_id);
1553
1554 $post_alt = get_post_meta($video_id, '_wp_attachment_image_alt', true);
1555
1556 $image_html = wp_get_attachment_image($video->ID, MAXGALLERIA_META_IMAGE_THUMB_LARGE);
1557
1558 $data = array ('post_title' => esc_html($video->post_title), 'post_alt' => esc_attr($post_alt), 'caption' => wp_kses_post($video->post_excerpt), 'image_html' => $image_html);
1559 echo json_encode($data);
1560
1561 }
1562
1563 }
1564
1565 die();
1566 }
1567
1568 public function mg_save_video_info() {
1569
1570
1571 if (isset($_POST) && check_admin_referer($this->video_gallery->nonce_video_edit['action'], $this->video_gallery->nonce_video_edit['name'])) {
1572
1573
1574 if ((isset($_POST['video_id'])) && (strlen(trim($_POST['video_id'])) > 0))
1575 $video_id = trim(sanitize_text_field($_POST['video_id']));
1576 else
1577 $video_id = 0;
1578
1579 if ((isset($_POST['post_title'])) && (strlen(trim($_POST['post_title'])) > 0))
1580 $post_title = $this->stripUnwantedTagsAndAttrs(wp_filter_post_kses($_POST['post_title']));
1581 else
1582 $post_title = '';
1583
1584 if ((isset($_POST['caption'])) && (strlen(trim($_POST['caption'])) > 0))
1585 $post_excerpt = $this->stripUnwantedTagsAndAttrs(wp_filter_post_kses($_POST['caption']));
1586 else
1587 $post_excerpt = '';
1588
1589 if ((isset($_POST['post_alt'])) && (strlen(trim($_POST['post_alt'])) > 0))
1590 $post_alt = stripslashes(sanitize_text_field($_POST['post_alt']));
1591 else
1592 $post_alt = '';
1593
1594 if($video_id != 0) {
1595 if (!$this->can_edit_gallery_attachment($video_id)) {
1596 $this->gallery_media_permission_denied();
1597 }
1598
1599 $video = get_post($video_id);
1600
1601 // First update the post itself
1602 $temp = array();
1603 $temp['ID'] = $video->ID;
1604
1605 $temp['post_title'] = $this->remove_ptags($post_title);
1606 $temp['post_excerpt'] = $this->remove_ptags($post_excerpt);
1607
1608 wp_update_post($temp);
1609
1610 update_post_meta($video->ID, '_wp_attachment_image_alt', $post_alt);
1611
1612 update_post_meta($video->ID, 'maxgallery_attachment_video_enable_related_videos', 1);
1613
1614 update_post_meta($video->ID, 'maxgallery_attachment_video_enable_hd_playback', 1);
1615
1616 }
1617
1618 }
1619
1620 die();
1621
1622 }
1623
1624 public function mg_display_bulk_video() {
1625
1626 $html = '';
1627
1628 if (isset($_POST) && check_admin_referer($this->video_gallery->nonce_video_edit['action'], $this->video_gallery->nonce_video_edit['name'])) {
1629
1630 if ((isset($_POST['video_ids'])) && (strlen(trim($_POST['video_ids'])) > 0))
1631 $video_ids = trim(sanitize_text_field($_POST['video_ids']));
1632 else
1633 $video_ids = '';
1634
1635 if (isset($video_ids)) {
1636 $video_ids_array = explode(',', $video_ids);
1637
1638 $html .= '<table id="bulk-edit-table" cellpadding="0" cellspacing="0">' . PHP_EOL;
1639 foreach ($video_ids_array as $video_id) {
1640 if (isset($video_id)) {
1641 $video = get_post($video_id);
1642 if (isset($video)) {
1643 if (!$this->can_edit_gallery_attachment($video->ID)) {
1644 $this->gallery_media_permission_denied();
1645 }
1646
1647 $html .= ' <tr>' . PHP_EOL;
1648 $html .= ' <td class="thumb">' . PHP_EOL;
1649 $html .= wp_get_attachment_image($video->ID, MAXGALLERIA_META_VIDEO_THUMB_SMALL);
1650 $html .= ' <input type="hidden" name="video-edit-id[]" value="' . esc_attr($video->ID) . '" />' . PHP_EOL;
1651 $html .= ' </td>' . PHP_EOL;
1652 $html .= ' <td>' . PHP_EOL;
1653 $html .= ' <div class="fields">' . PHP_EOL;
1654 $html .= ' <div class="field">' . PHP_EOL;
1655 $html .= ' <div class="field-label">' . esc_html__('Title', 'maxgalleria') . '</div>' . PHP_EOL;
1656 $html .= ' <div class="field-value">' . PHP_EOL;
1657 $html .= ' <input type="text" name="video-edit-title[]" value="' . esc_html($video->post_title) . '" />' . PHP_EOL;
1658 $html .= ' </div>' . PHP_EOL;
1659 $html .= ' </div>' . PHP_EOL;
1660 $html .= ' <div class="clear"></div>' . PHP_EOL;
1661
1662 $html .= ' <div class="field">' . PHP_EOL;
1663 $html .= ' <div class="field-label">' . esc_html__('Alt Text', 'maxgalleria') . '</div>' . PHP_EOL;
1664 $html .= ' <div class="field-value">' . PHP_EOL;
1665 $html .= ' <input type="text" name="video-edit-alt[]" value="' . esc_html(get_post_meta($video->ID, '_wp_attachment_image_alt', true)) . '" />' . PHP_EOL;
1666 $html .= ' </div>' . PHP_EOL;
1667 $html .= ' </div>' . PHP_EOL;
1668 $html .= ' <div class="clear"></div>' . PHP_EOL;
1669
1670 $html .= ' <div class="field">' . PHP_EOL;
1671 $html .= ' <div class="field-label">' . esc_html__('Caption', 'maxgalleria') .'</div>' . PHP_EOL;
1672 $html .= ' <div class="field-value">' . PHP_EOL;
1673 $html .= ' <input type="text" name="video-edit-caption[]" value="' . esc_html($video->post_excerpt) . '" />' . PHP_EOL;
1674 $html .= ' </div>' . PHP_EOL;
1675 $html .= ' </div>' . PHP_EOL;
1676 $html .= ' <div class="clear"></div>' . PHP_EOL;
1677 $html .= ' </div>' . PHP_EOL;
1678 $html .= ' </td>' . PHP_EOL;
1679 $html .= ' </tr>' . PHP_EOL;
1680 }
1681 }
1682 }
1683 $html .= '<tr>' . PHP_EOL;
1684 $html .= ' <td></td>' . PHP_EOL;
1685 $html .= ' <td>' . PHP_EOL;
1686 $html .= ' <a class="btn btn-primary" id="bulk-save-button">' . esc_html__('Save Changes', 'maxgalleria') . '</a>' . PHP_EOL;
1687 $html .= ' <input type="button" class="btn" id="bulk-cancel-button" value="' . esc_html__('Cancel', 'maxgalleria') . '" />' . PHP_EOL;
1688 $html .= ' </td>' . PHP_EOL;
1689 $html .= ' <td></td>' . PHP_EOL;
1690 $html .= '</tr>' . PHP_EOL;
1691
1692 $html .= '</table>' . PHP_EOL;
1693
1694 }
1695
1696 $html .= wp_nonce_field($this->video_gallery->nonce_video_edit_bulk['action'], $this->video_gallery->nonce_video_edit_bulk['name']);
1697
1698 $allowed_html = array(
1699 'a' => array(
1700 'href' => array(),
1701 'title' => array(),
1702 'class' => array(),
1703 'id' => array()
1704 ),
1705 'img' => array(
1706 'src' => array(),
1707 'alt' => array(),
1708 'class' => array(),
1709 'loading' => array(),
1710 'srcset' => array(),
1711 'srcset' => array(),
1712 'sizes' => array(),
1713 'width' => array(),
1714 'height' => array()
1715 ),
1716 'input' => array(
1717 'id' => array(),
1718 'class' => array(),
1719 'value' => array(),
1720 'name' => array(),
1721 'type' => array()
1722 ),
1723 'div' => array(
1724 'class' => array()
1725 ),
1726 'br' => array(),
1727 'em' => array(),
1728 'strong' => array(),
1729 'table' => array(
1730 'id' => array(),
1731 'cellpadding' => array(),
1732 'cellspacing' => array()
1733 ),
1734 'tr' => array(),
1735 'td' => array(
1736 'class' => array()
1737 )
1738 );
1739
1740 echo wp_kses($html, $allowed_html);
1741
1742 }
1743
1744 die();
1745
1746 }
1747
1748 public function mg_save_bulk_video() {
1749
1750 global $wpdb;
1751 $form_data = '';
1752
1753 if (isset($_POST) && check_admin_referer($this->video_gallery->nonce_video_edit_bulk['action'], $this->video_gallery->nonce_video_edit_bulk['name'])) {
1754
1755 if ((isset($_POST['form_data'])) && (strlen(trim($_POST['form_data'])) > 0)) {
1756 parse_str($_POST['form_data'], $form_data);
1757
1758 if(isset($form_data['video-edit-id'])) {
1759
1760 $i = 0;
1761
1762 foreach ($form_data['video-edit-id'] as $video_edit_id) {
1763 if (!$this->can_edit_gallery_attachment($video_edit_id)) {
1764 $this->gallery_media_permission_denied();
1765 }
1766
1767 // First update the post itself
1768 $temp = array();
1769 $temp['ID'] = sanitize_text_field($video_edit_id);
1770
1771 $post_title = $this->stripUnwantedTagsAndAttrs(wp_filter_post_kses($form_data['video-edit-title'][$i]));
1772 $post_excerpt = $this->stripUnwantedTagsAndAttrs(wp_filter_post_kses($form_data['video-edit-caption'][$i]));
1773
1774 $temp['post_title'] = $this->remove_ptags($post_title);
1775 $temp['post_excerpt'] = $this->remove_ptags($post_excerpt);
1776
1777 wp_update_post($temp);
1778
1779 // Now update the image alt in the meta
1780 update_post_meta($video_edit_id, '_wp_attachment_image_alt', stripslashes(sanitize_text_field($form_data['video-edit-alt'][$i])));
1781
1782 // Increment the counter
1783 $i++;
1784 }
1785
1786 }
1787
1788 }
1789
1790 }
1791
1792 die();
1793
1794 }
1795
1796 private function check_for_mp4_video($gallery, $video_url, $uploads_dir) {
1797 global $wpdb;
1798 $updated = false;
1799
1800 $comma_position = strpos($video_url, ',');
1801 if($comma_position !== false) {
1802 $comma_position2 = strrpos($video_url, ',');
1803
1804 $video = esc_url_raw(substr($video_url, 0, $comma_position ));
1805 $thumb_url = esc_url_raw(substr($video_url, $comma_position+1, $comma_position2 - ($comma_position+1) ));
1806 $thumb_id = sanitize_text_field(substr($video_url, $comma_position2+1));
1807
1808 $video_path = str_replace( $uploads_dir['baseurl'], $uploads_dir['basedir'], $video );
1809 $thumbnail_path = str_replace( $uploads_dir['baseurl'], $uploads_dir['basedir'], $thumb_url );
1810
1811 require_once(ABSPATH . 'wp-admin/includes/media.php');
1812
1813 $filetype = wp_check_filetype(basename($thumbnail_path), null);
1814 $data = wp_read_video_metadata($video_path);
1815
1816 // Include image.php so we can call wp_generate_attachment_metadata()
1817 require_once(ABSPATH . 'wp-admin/includes/image.php');
1818
1819 // Insert the attachment
1820 $attachment_id = $thumb_id;
1821
1822 $attachment = get_post(absint($attachment_id));
1823 if (!$attachment || $attachment->post_type !== 'attachment' || !current_user_can('edit_post', $attachment->ID)) {
1824 $this->gallery_media_permission_denied();
1825 }
1826
1827 // update the partent of the post thumbnail record
1828 $menu_order = $this->common->get_next_menu_order($gallery->ID);
1829
1830 $post_table = $wpdb->prefix . "posts";
1831 $record = array('post_parent' => $gallery->ID, 'menu_order' => $menu_order );
1832 $where = array('ID' => $thumb_id);
1833 $wpdb->update( $post_table, $record, $where);
1834
1835 $data['id'] = $attachment_id;
1836 if(!isset($data['duration']) && isset($data['length']))
1837 $data['duration'] = $data['length'];
1838 if(!isset($data['title']))
1839 $data['title'] = preg_replace( '/\.[^.]+$/', '', basename($video_path));
1840
1841 // Save some of the data in the post meta of the attachment
1842 do_action(MAXGALLERIA_ACTION_VIDEO_ATTACHMENT_POST_META, $attachment_id, $video, $thumb_url, $data);
1843 $updated = true;
1844 }
1845 return $updated;
1846 }
1847
1848 public function mg_add_videos() {
1849
1850 global $wpdb;
1851 //global $maxgalleria;
1852 //$video_gallery = $this->video_gallery;
1853
1854 if (isset($_POST) && check_admin_referer($this->video_gallery->nonce_video_add['action'], $this->video_gallery->nonce_video_add['name'])) {
1855
1856 // lines could contain mixed data, so too early to sanitize until it is parsed
1857 if ((isset($_POST['video_urls'])) && (strlen(trim($_POST['video_urls'])) > 0)) {
1858 $video_urls = explode("\n", $_POST['video_urls']);
1859 foreach($video_urls as $video_url) {
1860 $video_url = sanitize_url($video_url);
1861 }
1862 } else
1863 $video_urls = '';
1864
1865 if ((isset($_POST['post_id'])) && (strlen(trim($_POST['post_id'])) > 0))
1866 $post_id = sanitize_text_field($_POST['post_id']);
1867 else
1868 $post_id = 0;
1869
1870 if(is_numeric($post_id) && $post_id != 0) {
1871 if (!$this->can_edit_maxgallery($post_id)) {
1872 $this->gallery_media_permission_denied();
1873 }
1874
1875 $gallery = get_post($post_id);
1876
1877 do_action(MAXGALLERIA_ACTION_BEFORE_ADD_VIDEOS_TO_GALLERY, $video_urls);
1878
1879 if(class_exists('MaxGalleriaVideo'))
1880 global $maxgalleria_video;
1881
1882 $site_url = site_url();
1883 $uploads_dir = wp_upload_dir();
1884
1885 foreach ($video_urls as $video_url) {
1886
1887 // Use rtrim to remove the \n on the end of the strings
1888 $video_url = rtrim($video_url);
1889
1890 if ($video_url != '') {
1891
1892 if(strpos($video_url, '.mp4') != false)
1893 $mp4 = true;
1894 else
1895 $mp4 = false;
1896
1897 if(class_exists('MaxGalleriaVideo') && class_exists("MaxGalleriaMediaLibProS3") && $mp4) {
1898
1899 global $maxgalleria_media_library_pro_s3;
1900
1901 if(strpos($video_url, $maxgalleria_media_library_pro_s3->bucket) != false) {
1902 $updated = $this->check_for_mp4_video($gallery, $video_url, $uploads_dir);
1903 }
1904 } else {
1905
1906 $updated = $this->check_for_mp4_video($gallery, $video_url, $uploads_dir);
1907
1908 $video_url = sanitize_url($video_url);
1909 // is this a vimeo url?
1910 if( strpos($video_url, "vimeo.com") === false) {
1911 $vimeo = false;
1912 if(strpos($video_url, "youtube.com") != false) {
1913 $page_link = strpos($video_url, "youtube.com/watch?v=");
1914
1915 // if an yourtube embedded url convert it to a page link
1916 if($page_link === false) {
1917 $video_pos = strrpos($video_url, '/');
1918 $video_url = "https://www.youtube.com/watch?v=" . substr($video_url, $video_pos+1);
1919 }
1920 }
1921 } else {
1922 $vimeo = true;
1923 }
1924
1925 // Get the data for the video; first initialize the API URL
1926 // and then pass it to the filter so it can get populated
1927 $api_url = '';
1928 $api_url = apply_filters(MAXGALLERIA_FILTER_VIDEO_API_URL, $api_url, $video_url);
1929
1930
1931 // Continue only if we have an API URL to call
1932 if ($api_url != '') {
1933 // Perform a remote GET to get the body of data from
1934 // the API URL and then decode it into JSON bits
1935 $response = wp_remote_get($api_url);
1936 $contents = wp_remote_retrieve_body($response);
1937 $data = json_decode($contents, true);
1938
1939
1940 // Get the URL of the video thumbnail; first initialize the thumb
1941 // URL and then pass it to the filter so it can get populated
1942 $thumb_url = '';
1943 $thumb_url = apply_filters(MAXGALLERIA_FILTER_VIDEO_THUMB_URL, $thumb_url, $video_url, $data);
1944
1945 // Now that we have the thumb URL, get its remote contents
1946 // so we can store it as an attachment for the gallery
1947 $response = wp_remote_get($thumb_url);
1948 $contents = wp_remote_retrieve_body($response);
1949
1950 // Upload and get file data
1951 $default_name = basename($thumb_url);
1952 $position = strrpos($default_name, '.');
1953
1954 if($vimeo)
1955 $new_file_name = $data[0]['title'];
1956 else
1957 $new_file_name = $data['items'][0]['snippet']['title'];
1958
1959 // remove illegal characters
1960 $new_file_name = preg_replace("[\\~#%&*{}/:<>?|\"-]", "", $new_file_name);
1961 // replace spaces
1962 $new_file_name = str_replace(' ', '-', $new_file_name);
1963 // remove extra dashes
1964 $new_file_name = preg_replace('/-+/', '-', $new_file_name);
1965 // limit to 30 characters
1966 $new_file_name = substr($new_file_name, 0, 30);
1967 // remove ending dashes
1968 $new_file_name = rtrim($new_file_name, '-');
1969 // add the extention
1970 $new_file_name = $new_file_name . substr($default_name, $position);
1971 //$upload = wp_upload_bits(basename($thumb_url), null, $contents);
1972
1973 if($vimeo) // add missing extention
1974 $new_file_name .= '.jpg';
1975
1976 $upload = wp_upload_bits($new_file_name, null, $contents);
1977
1978 $guid = $upload['url'];
1979 $file = $upload['file'];
1980 $file_type = wp_check_filetype(basename($file), null);
1981
1982 // Set up the video thumb as an attachment; first initialize the
1983 // attachment and then pass it to the filter so it can get populated
1984 $attachment = array();
1985 $attachment = apply_filters(MAXGALLERIA_FILTER_VIDEO_ATTACHMENT, $attachment, $video_url, $gallery->ID, $guid, $file_type['type'], $data);
1986
1987 // Include image.php so we can call wp_generate_attachment_metadata()
1988 require_once(ABSPATH . 'wp-admin/includes/image.php');
1989
1990 // Insert the attachment
1991 $attachment_id = wp_insert_attachment($attachment, $file, $gallery->ID);
1992 $attachment_data = wp_generate_attachment_metadata($attachment_id, $file);
1993 wp_update_attachment_metadata($attachment_id, $attachment_data);
1994
1995
1996 if(class_exists("MaxGalleriaMediaLibProS3")) {
1997
1998 global $maxgalleria_media_library_pro_s3;
1999
2000 $image_url = wp_get_attachment_url($attachment_id);
2001 $filename = get_attached_file($attachment_id);
2002
2003 // upload the image
2004 $post_type = 'attachment';
2005 $location = $maxgalleria_media_library_pro_s3->get_location($image_url, $maxgalleria_media_library_pro_s3->maxgalleria_media_library_pro->uploads_folder_name);
2006 $destination_location = $maxgalleria_media_library_pro_s3->get_destination_location($location);
2007 $destination_folder = $maxgalleria_media_library_pro_s3->get_destination_folder($destination_location, $maxgalleria_media_library_pro_s3->maxgalleria_media_library_pro->uploads_folder_name_length);
2008 $upload_result = $maxgalleria_media_library_pro_s3->upload_to_s3($post_type, $location, $filename, $attachment_id);
2009
2010 // upload thumbnails
2011 $metadata = wp_get_attachment_metadata($attachment_id);
2012
2013 foreach($metadata['sizes'] as $thumbnail) {
2014 $source_file = $maxgalleria_media_library_pro_s3->maxgalleria_media_library_pro->get_absolute_path($maxgalleria_media_library_pro_s3->uploadsurl . $destination_folder . $thumbnail['file']);
2015 $upload_result = $maxgalleria_media_library_pro_s3->upload_to_s3($post_type, $destination_location . '/' . $thumbnail['file'], $source_file, 0);
2016 }
2017
2018 // delete from local server
2019 if($maxgalleria_media_library_pro_s3->remove_from_local) {
2020 if($upload_result['statusCode'] == '200') {
2021 $maxgalleria_media_library_pro_s3->remove_media_file($filename);
2022 foreach($metadata['sizes'] as $thumbnail) {
2023 $source_file = $maxgalleria_media_library_pro_s3->maxgalleria_media_library_pro->get_absolute_path($maxgalleria_media_library_pro_s3->uploadsurl . $destination_folder . $thumbnail['file']);
2024 $maxgalleria_media_library_pro_s3->remove_media_file($source_file);
2025 }
2026 }
2027 }
2028 }
2029
2030 // Save some of the data in the post meta of the attachment
2031 do_action(MAXGALLERIA_ACTION_VIDEO_ATTACHMENT_POST_META, $attachment_id, $video_url, $thumb_url, $data);
2032 $updated = true;
2033 }
2034 }
2035 }
2036 }
2037
2038 do_action(MAXGALLERIA_ACTION_AFTER_ADD_VIDEOS_TO_GALLERY, $video_urls);
2039
2040 }
2041 echo 'ok';
2042
2043 }
2044
2045 die();
2046 }
2047
2048 public function has_double_slashes_except_protocol($str) {
2049 $protocol = '';
2050 if (strpos($str, 'http://') === 0) {
2051 $protocol = 'http://';
2052 $str = substr($str, 7);
2053 } elseif (strpos($str, 'https://') === 0) {
2054 $protocol = 'https://';
2055 $str = substr($str, 8);
2056 }
2057
2058 return strpos($str, '//') !== false && strpos($str, '//', strpos($str, '//') + 2) === false;
2059
2060 }
2061
2062 public function remove_double_slash_except_protocol($str) {
2063 $protocol = '';
2064 if (strpos($str, 'http://') === 0) {
2065 $protocol = 'http://';
2066 $str = substr($str, 7);
2067 } elseif (strpos($str, 'https://') === 0) {
2068 $protocol = 'https://';
2069 $str = substr($str, 8);
2070 }
2071
2072 while(strpos($str, '//') !== false) {
2073 $str = str_replace('//', '/', $str);
2074 }
2075
2076 return $protocol . $str;
2077 }
2078
2079
2080 public function remove_double_backslash($str) {
2081 while(strpos($str, '\\\\') !== false) {
2082 $str = str_replace('\\\\', '\\', $str);
2083 }
2084 return $str;
2085 }
2086
2087 }
2088
2089 // Let's get this party started
2090 $maxgalleria = new MaxGalleria();
2091
2092 ?>
2093