-
Notifications
You must be signed in to change notification settings - Fork 0
/
bu-slideshow.php
1192 lines (987 loc) · 37.1 KB
/
bu-slideshow.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
Plugin Name: BU Slideshow
Plugin URI: http://developer.bu.edu/bu-slideshow/
Description: Allows for the creation and display of animated slideshows. Uses sequence.js.
Version: 2.3.13
Author: Boston University (IS&T)
Author URI: http://www.bu.edu/tech/
Requires at least: 3.5
Tested up to: 4.9.6
*/
define('BU_SLIDESHOW_VERSION', '2.3.13');
define('BU_SLIDESHOW_BASEDIR', plugin_dir_path(__FILE__));
define('BU_SLIDESHOW_BASEURL', plugin_dir_url(__FILE__));
//define('SCRIPT_DEBUG', true);
/*possible remove this definition - check usage
It was at least partly in use as the text domain parameter for translation function __(string, 'text-domain') but that usage throws an error in WP plugin checker and may prevent publication of the pligin*/
if (!defined('BU_SSHOW_LOCAL')) {
define('BU_SSHOW_LOCAL', 'BU_Slideshow');
}
if (defined('SCRIPT_DEBUG') && SCRIPT_DEBUG) {
define('BU_SSHOW_MIN', '');
} else {
define('BU_SSHOW_MIN', '');//.min
}
//define('BU_SSHOW_MIN', '');
require_once BU_SLIDESHOW_BASEDIR . 'class-bu-slideshow.php';
require_once BU_SLIDESHOW_BASEDIR . 'class-bu-slide.php';
require_once BU_SLIDESHOW_BASEDIR . 'slideshow-upgrade.php';
// Load block.
require_once BU_SLIDESHOW_BASEDIR . '/src/block.php';
class BU_Slideshow {
static $wp_version;
static $meta_key = 'bu_slideshows';
static $show_id_meta_key = 'bu_slideshow_last_id';
static $custom_thumb_size = 'bu-slideshow-thumb';
static $post_support_slug = 'bu_slideshow';
// explicitly add page_alt to allow slideshow ui with bu-versions
static $supported_post_types = array('page', 'post', 'page_alt');// post types to support Add Slideshow button
static $editor_screens = array(); // other screens on which to include Add Slideshow modal
static $caption_positions = array(
'Top Right' => 'caption-top-right',
'Top Center' => 'caption-top-center',
'Top Left' => 'caption-top-left',
'Middle Center' => 'caption-center-center',
'Bottom Right' => 'caption-bottom-right',
'Bottom Center' => 'caption-bottom-center',
'Bottom Left' => 'caption-bottom-left'
);
static $slide_templates = array();
static $manage_url = 'admin.php?page=bu-slideshow';
static $edit_url = 'admin.php?page=bu-edit-slideshow';
static $add_url = 'admin.php?page=bu-add-slideshow';
static $preview_url = 'admin.php?page=bu-preview-slideshow';
static $min_cap = 'edit_posts';
static $shortcode_defaults = array(
'show_id' => 0,
'show_nav' => 1,
'transition' => 'slide',
'nav_style' => 'icon',
'autoplay' => 1,
'show_arrows' => 0,
'transition_delay' => 5,
'width' => 'auto',
'align' => 'center',
'shuffle' => false
);
static $transitions = array('slide', 'fade'); // prepackaged transitions
static $nav_styles = array('icon', 'number');
static $image_mimes = array('jpg|jpeg|jpe', 'png', 'gif');
//static $upload_error = 'That does not appear to be a valid image. Please upload a JPEG, PNG or GIF file.';
static public function add_plugins_loaded_hook(){
add_action('plugins_loaded', array(__CLASS__, 'init'));
}
static public function init() {
global $pagenow;
self::$wp_version = get_bloginfo('version');
$upload_error = __('That does not appear to be a valid image. Please upload a JPEG, PNG or GIF file.', 'bu-slideshow');
add_action('init', array(__CLASS__, 'register_cpt'), 6);
add_action('init', array(__CLASS__, 'custom_thumb_size'));
add_action('init', array(__CLASS__, 'add_post_support'),99);
add_action('admin_menu', array(__CLASS__, 'admin_menu'));
add_action('admin_enqueue_scripts', array(__CLASS__, 'admin_scripts_styles'));
add_action('wp_enqueue_scripts', array(__CLASS__, 'public_scripts_styles'));
add_action('media_buttons', array(__CLASS__, 'add_media_button'),99);
add_action('admin_footer-post.php', array(__CLASS__, 'admin_footer'));
add_action('admin_footer-post-new.php', array(__CLASS__, 'admin_footer'));
// media upload/insert restrictions
if ('media-upload.php' === $pagenow || 'async-upload.php' === $pagenow) {
self::media_upload_custom();
}
add_action('media_upload_bu_slideshow', array(__CLASS__, 'handle_upload'));
add_action('pre_get_posts', array(__CLASS__, 'media_library_filter'));
add_filter('upload_file_glob', array(__CLASS__, 'flash_file_types')); // does not exist in 3.3+
add_action('wp_ajax_bu_delete_slideshow', array(__CLASS__, 'delete_slideshow_ajax'));
add_action('wp_ajax_bu_add_slide', array(__CLASS__, 'add_slide_ajax'));
add_action('wp_ajax_bu_get_slide_thumb', array(__CLASS__, 'get_slide_thumb_ajax'));
add_action('wp_ajax_bu_slideshow_get_url', array(__CLASS__, 'get_url'));
add_shortcode('bu_slideshow', array(__CLASS__, 'shortcode_handler'));
}
static public function register_cpt(){
$args = array(
'labels' => array(
'name' => __( 'Slideshows', 'bu-slideshow' ),
'singular_name' => __( 'Slideshow', 'bu-slideshow' ),
'add_new' => __( 'Add New', 'bu-slideshow' ),
'add_new_item' => __( 'Add New Slideshow', 'bu-slideshow' ),
'edit_item' => __( 'Edit Slideshow', 'bu-slideshow' ),
'new_item' => __( 'New Slideshow', 'bu-slideshow' ),
'view_item' => __( 'View Slideshow', 'bu-slideshow' ),
'search_items' => __( 'Search Slideshows', 'bu-slideshow' ),
'not_found' => __( 'No Slideshows found', 'bu-slideshow' ),
'not_found_in_trash' => __( 'No Slideshows in the trash', 'bu-slideshow' ),
'parent_item_colon' => __( 'Parent Slideshows:', 'bu-slideshow' ),
'menu_name' => __( 'Slideshows', 'bu-slideshow' ),
),
'public' => false,
'publicly_queryable' => false,
'show_ui' => false,
'show_in_menu' => false,
'show_in_rest' => true,
'query_var' => false,
'rewrite' => false,
'capability_type' => 'post',
'has_archive' => false,
'hierarchical' => false,
'menu_position' => null,
'supports' => false,
'can_export' => true,
);
register_post_type( 'bu_slideshow', $args );
}
static public function add_post_support() {
$post_types = apply_filters('bu_slideshow_supported_post_types', self::$supported_post_types);
if (!is_array($post_types)) {
$post_types = array();
}
foreach ($post_types as $pt) {
add_post_type_support($pt, self::$post_support_slug);
}
}
/**
* Loads admin scripts/styles on plugin's pages. Add a page's id using by hooking
* the bu_slideshow_selector_pages filter to load the selector scripts/styles.
*
* @global type $current_screen
*/
static public function admin_scripts_styles() {
global $current_screen;
if (self::using_editor()) {
self::selector_scripts_styles();
}
self::admin_scripts();
/* preview page needs public scripts/styles */
if ($current_screen->id === 'admin_page_bu-preview-slideshow') {
self::public_scripts_styles();
}
}
/**
* Admin scripts, for older and newer jQuery.
*/
static public function admin_scripts() {
global $current_screen;
$admin_pages = array(
'toplevel_page_bu-slideshow',
'slideshows_page_bu-slideshow',
'admin_page_bu-edit-slideshow',
'slideshows_page_bu-add-slideshow'
);
$js_url = BU_SLIDESHOW_BASEURL . 'interface/js/';
if (in_array($current_screen->id, $admin_pages) || self::using_editor()) {
wp_enqueue_script('bu-modal', $js_url . 'bu-modal/bu-modal' . BU_SSHOW_MIN . '.js', array('jquery'), BU_SLIDESHOW_VERSION, false);
wp_enqueue_style('bu-modal', $js_url . 'bu-modal/css/bu-modal.css');
wp_register_script('bu-slideshow-admin', $js_url . 'bu-slideshow-admin' . BU_SSHOW_MIN . '.js', array('jquery', 'bu-modal'), BU_SLIDESHOW_VERSION, true);
wp_enqueue_script('media-upload');
wp_enqueue_script('bu-slideshow-admin');
wp_enqueue_script('jquery-ui-sortable');
wp_enqueue_script('thickbox');
self::localize('bu-slideshow-admin');
wp_register_style('bu-slideshow-admin', BU_SLIDESHOW_BASEURL . 'interface/css/bu-slideshow-admin' . BU_SSHOW_MIN . '.css', array(), BU_SLIDESHOW_VERSION);
wp_enqueue_style('bu-slideshow-admin');
wp_enqueue_style('thickbox');
}
/* enqueue new media uploader stuff */
if ( ($current_screen->id === 'admin_page_bu-edit-slideshow' || $current_screen->id === 'slideshows_page_bu-add-slideshow')
&& function_exists('wp_enqueue_media')) {
wp_enqueue_media();
}
}
/**
* Prepares styles and scripts for front end. Scripts are registered here and enqueued in shortcode handler
* (or in WP < 3.3, printed in footer; see conditional_script_load()).
*
* Define BU_SLIDESHOW_CUSTOM_CSS in a theme to prevent default CSS from loading. You will
* need to supply your own CSS transitions in this case.
*/
static public function public_scripts_styles() {
self::public_scripts();
if (!defined('BU_SLIDESHOW_CUSTOM_CSS') || !BU_SLIDESHOW_CUSTOM_CSS) {
wp_register_style('bu-slideshow', BU_SLIDESHOW_BASEURL . 'interface/css/bu-slideshow' . BU_SSHOW_MIN . '.css', array(), BU_SLIDESHOW_VERSION);
wp_enqueue_style('bu-slideshow');
}
/* enqueue public styles on preview page */
global $current_screen;
if ($current_screen && $current_screen->id === 'admin_page_bu-preview-slideshow') {
wp_register_style('bu-slideshow', BU_SLIDESHOW_BASEURL . 'interface/css/bu-slideshow' . BU_SSHOW_MIN . '.css', array(), BU_SLIDESHOW_VERSION);
wp_enqueue_style('bu-slideshow');
}
}
/**
* Front end scripts, for older and newer jQuery. For jQuery < 1.71, patches jQuery 'on' to support sequence.js
*/
static public function public_scripts() {
$js_url = BU_SLIDESHOW_BASEURL . 'interface/js/';
$seq_deps = array('jquery');
$slideshow_deps = array('jquery','jquery-sequence');
wp_register_script('jquery-sequence', BU_SLIDESHOW_BASEURL . 'interface/js/vendor/sequence/sequence.jquery' . BU_SSHOW_MIN . '.js', $seq_deps, BU_SLIDESHOW_VERSION, true);
wp_register_script('bu-slideshow', $js_url . 'bu-slideshow' . BU_SSHOW_MIN . '.js', $slideshow_deps, BU_SLIDESHOW_VERSION, true);
}
/**
* Load scripts and styles for the selector UI
*/
static public function selector_scripts_styles() {
wp_register_script('bu-slideshow-selector', BU_SLIDESHOW_BASEURL . 'interface/js/bu-slideshow-selector' . BU_SSHOW_MIN . '.js', array('jquery'), BU_SLIDESHOW_VERSION, true);
wp_enqueue_script('bu-slideshow-selector');
wp_register_style('bu-slideshow-selector', BU_SLIDESHOW_BASEURL . 'interface/css/bu-slideshow-selector' . BU_SSHOW_MIN . '.css', array(), BU_SLIDESHOW_VERSION);
wp_enqueue_style('bu-slideshow-selector');
self::localize('bu-slideshow-selector');
}
/**
* Localize text in javascript
* @param string $script
*/
static public function localize($script = '') {
switch($script) {
case 'bu-slideshow-admin':
$local = array(
'noSlideshowsMsg' => __('No slideshows yet.', 'bu-slideshow'),
'addButtonText' => __('Add a slideshow', 'bu-slideshow'),
'deleteConfirm' => __('Are you sure you want to delete this slideshow? This action cannot be undone.', 'bu-slideshow'),
'deleteConfirmSlide' => __('Are you sure you want to delete this slide?', 'bu-slideshow'),
'deleteError' => __('Could not delete slideshow.', 'bu-slideshow'),
'noneSelectedError' => __('You must select a slideshow.', 'bu-slideshow'),
'emptyNameError' => __('The name field for the slideshow cannot be empty.', 'bu-slideshow'),
'thumbFailError' => __('Could not load image thumbnail.', 'bu-slideshow'),
'addSlideFailError' => __('Could not create new slide.', 'bu-slideshow'),
'mediaUploadTitle' => __('Select Image', 'bu-slideshow'),
'mediaUploadButton' => __('Select Image', 'bu-slideshow')
);
wp_localize_script($script, 'buSlideshowLocalAdmin', $local);
break;
case 'bu-slideshow-selector':
$local = array(
'toggleTextShow' => __('Show advanced', 'bu-slideshow'),
'toggleTextHide' => __('Hide advanced', 'bu-slideshow')
);
wp_localize_script($script, 'buSlideshowLocalSelector', $local);
default:
break;
}
}
/**
* Helper for retrieving plugin admin URLs via ajax
*/
static public function get_url() {
$urls = array(
'manage_url' => self::$manage_url,
'edit_url' => self::$edit_url,
'add_url' => self::$add_url,
'preview_url' => self::$preview_url
);
if (isset($_POST['url'])) {
if (array_key_exists($_POST['url'], $urls)) {
echo esc_url($urls[$_POST['url']]);
}
}
exit;
}
/**
* Loads scripts only when global variable is set by shortcode handler. Scripts
* are never enqueued, so a filter is available should another script need to
* prevent these from loading.
*
* This method is only used in WP < 3.3; later version simply enqueue scripts
* in the shortcode handler.
*
* @global int|bool $bu_slideshow_loadscripts
*/
static public function conditional_script_load() {
global $bu_slideshow_loadscripts;
if ($bu_slideshow_loadscripts) {
$conditional_scripts = array('bu-sequence-patch', 'jquery-sequence', 'bu-slideshow');
apply_filters('bu_slideshow_conditional_scripts', $conditional_scripts);
foreach($conditional_scripts as $script) {
wp_print_scripts($script);
}
}
}
/**
* Establishes custom thumbnail size.
*/
static public function custom_thumb_size() {
add_image_size(static::$custom_thumb_size, 100, 100, true);
}
/**
* Handles customizations to media upload for slide images
*/
static public function media_upload_custom() {
$referer = strpos( wp_get_referer(), 'bu_slideshow' );
if ($referer !== FALSE) {
add_filter('gettext', array(__CLASS__, 'replace_thickbox_text'), 99, 3);
add_filter('media_upload_tabs', array(__CLASS__, 'remove_url_tab'), 99);
add_filter('post_mime_types', array(__CLASS__, 'post_mime_types'), 99);
}
}
/**
* Called when media upload form is first loaded and again when the upload is
* complete, with the image info in POST. This function exists so we can add
* the mime type filter hook only when the
*/
static public function handle_upload() {
add_filter('upload_mimes', array(__CLASS__, 'upload_mime_types'), 99);
// non-flash upload field
if (isset($_POST['html-upload']) && !empty($_FILES)) {
// uploads the file, inserts the attachment
$id = media_handle_upload('async-upload', 0);
unset($_FILES);
if (is_wp_error($id)) {
$errors['upload_error'] = $id;
$id = false;
}
}
// user has pressed 'insert into post' or equivalent
if (!empty($_POST)) {
$return = media_upload_form_handler();
if (is_string($return))
return $return;
if (is_array($return))
$errors = $return;
}
if (isset($_POST['save'])) {
$errors['upload_notice'] = __('Saved.', 'bu-slideshow');
}
return wp_iframe('media_upload_type_form', 'bu_slideshow', $errors, $id);
}
/**
* Change the text on the media upload button.
*
* @param string $translated_text
* @param string $text
* @param string $domain
* @return string
*/
static public function replace_thickbox_text($translated_text, $text, $domain) {
if ($text === 'Insert into Post') {
return __('Select Image', 'bu-slideshow');
}
return $translated_text;
}
/**
* Remove 'insert from URL' tab, which breaks without a post ID
*
* @param array $tabs
* @return array
*/
static public function remove_url_tab($tabs) {
unset($tabs['type_url']);
return $tabs;
}
/**
* Restrict 'insert media' filter choices to image file types
*
* @param array $mime_types
* @return array
*/
static public function post_mime_types($mime_types) {
foreach($mime_types as $key => $val) {
if ($key !== 'image') {
unset($mime_types[$key]);
}
}
return $mime_types;
}
/**
* Restrict media that can be uploaded to images. Is not applied to flash uploader.
*
* @param array $mime_types
* @return array
*/
static public function upload_mime_types($mime_types) {
foreach($mime_types as $key => $val) {
if (!in_array($key, self::$image_mimes)) {
unset($mime_types[$key]);
}
}
return $mime_types;
}
/**
* When Flash uploader is being used on Edit page, restrict allowed file types
*
* @param string $types
* @return string
*/
static public function flash_file_types($types) {
if (strpos(wp_get_referer(), self::$edit_url) !== false) {
$new_types = '';
foreach (self::$image_mimes as $mime) {
if (strpos($mime, 'jpg') !== false) {
$submimes = explode('|', $mime);
foreach ($submimes as $sub) {
$new_types .= $sub . ';';
}
} else {
$new_types .= $mime . ';';
}
}
return $new_types;
}
return $types;
}
/**
* Restrict query that populates the 'insert from media library' view to images
*
* @global string $pagenow
* @param obj $query
*/
static public function media_library_filter($query) {
global $pagenow;
if ($pagenow !== 'media-upload.php') {
return;
}
if (strpos( wp_get_referer(), 'bu_slideshow' ) === false) {
return;
}
$query->set('post_mime_type', 'image');
}
static public function admin_menu() {
$index = self::get_menu_index(21);
add_menu_page(__('Slideshows', 'bu-slideshow'), __('Slideshows', 'bu-slideshow'), self::$min_cap, 'bu-slideshow', array(__CLASS__, 'manage_slideshow_page'), 'dashicons-format-gallery', $index);
add_submenu_page('bu-slideshow', __('Add Slideshow', 'bu-slideshow'), __('Add Slideshow', 'bu-slideshow'), self::$min_cap, 'bu-add-slideshow', array(__CLASS__, 'add_slideshow_page'));
add_submenu_page('bu-preview-slideshow', __('Preview Slideshow', 'bu-slideshow'), __('Preview Slideshow', 'bu-slideshow'), self::$min_cap, 'bu-preview-slideshow', array(__CLASS__, 'preview_slideshow_page'));
add_submenu_page('bu-edit-slideshow', __('Edit Slideshow', 'bu-slideshow'), __('Edit Slideshow', 'bu-slideshow'), self::$min_cap, 'bu-edit-slideshow', array(__CLASS__, 'edit_slideshow_page'));
}
/**
* Hack to prevent admin menu position from overwriting any existing menu items.
* A better solution should be available in the future, see http://core.trac.wordpress.org/ticket/12718
*
* @global array $menu
* @param int $index
* @return int
*/
static protected function get_menu_index($index) {
if (!is_numeric($index)) {
return NULL;
}
global $menu;
if (isset($menu[$index])) {
return self::get_menu_index( ($index + 1) );
}
return (int) $index;
}
static private function save_show($show){
$height = (intval($_POST['bu_slideshow_height']) > 0) ? intval($_POST['bu_slideshow_height']) : 0 ;
$caption_positions = apply_filters('bu_slideshow_caption_positions', self::$caption_positions);
$valid_templates = apply_filters('bu_slideshow_slide_templates', self::$slide_templates);
$template = ( isset($_POST['bu_slideshow_template']) && array_key_exists( $_POST['bu_slideshow_template'], $valid_templates ) ) ? $_POST['bu_slideshow_template'] : '';
$all_templates = apply_filters('bu_slideshow_slide_templates', BU_Slideshow::$slide_templates);
// okay to have no slides
if (!isset($_POST['bu_slides']) || !is_array($_POST['bu_slides'])) {
$_POST['bu_slides'] = array();
}
$slides = array();
$show->set_view('admin');
$show->set_name($_POST['bu_slideshow_name']);
$show->set_template( $template );
$show->set_height($height);
foreach ($_POST['bu_slides'] as $i => $arr) {
$customfields = array();
if( $show->template_id && is_array( $arr['custom_fields'] ) ){
foreach( $arr['custom_fields'] as $k => $v){
if( ! array_key_exists($k, $all_templates[ $show->template_id ]['custom_fields'] ) ){
continue;
}
$customfields[ $k ] = wp_kses_post( $v );
}
}
$args = array(
'view' => 'admin',
'order' => $i,
'image_id' => intval($arr['image_id']),
'image_size' => esc_attr(wp_kses_data($arr['image_size'])),
'caption' => array(
'title' => wp_kses_data($arr['caption']['title']),
'link' => esc_attr(wp_kses_data($arr['caption']['link'])),
'text' => wp_kses_data($arr['caption']['text']),
'position' => ( FALSE === array_search($arr['caption']['position'], $caption_positions) ) ? 'caption-bottom-right' : $arr['caption']['position']
),
'template_id' => $template,
'additional_styles' => esc_attr(wp_kses_data($arr['additional_styles'])),
'custom_fields' => $customfields,
);
$slides[] = new BU_Slide($args);
}
$show->set_slides($slides);
}
/**
* Loads and handles submissions from Add Slideshow page.
*/
static public function add_slideshow_page() {
$action = !empty( $_POST['bu_slideshow_save_show'] ) ? 'do_create' : 'view_form';
$msg = '';
$name = '';
$height = 0;
$slides = array();
$valid_templates = apply_filters('bu_slideshow_slide_templates', BU_Slideshow::$slide_templates );
$template_id = '';
switch ( $action ) {
case 'do_create':
if ( !isset($_POST['bu_slideshow_nonce']) || !wp_verify_nonce($_POST['bu_slideshow_nonce'], 'bu_update_slideshow') || !current_user_can(self::$min_cap) ) {
require_once(ABSPATH . 'wp-admin/admin-header.php');
wp_die(esc_html__('You are not authorized to perform this action.', 'bu-slideshow'));
exit;
}
if( !isset( $_POST['bu_slideshow_name'] ) || '' == trim( $_POST['bu_slideshow_name'] ) ){
$msg .= __('Could not create slideshow: missing name.', 'bu-slideshow');
break;
} else {
$show = self::create_slideshow( filter_var($_POST['bu_slideshow_name'], FILTER_SANITIZE_STRING) );
if( !$show || is_wp_error($show) ){
$msg = __('Error creating slideshow', 'bu-slideshow');
break;
}
}
// we are handling a form submission & all validation complete
self::save_show($show);
if ($show->update()) {
$url = 'admin.php?page=bu-edit-slideshow&bu_slideshow_id=' . $show->id . "&msg=";
$url .= urlencode( __("Slideshow created successfully.", 'bu-slideshow') );
wp_safe_redirect( admin_url( $url ) );
exit;
} else {
require_once(ABSPATH . 'wp-admin/admin-header.php');
$msg = __("Error creating slideshow", 'bu-slideshow');
}
break;
}
require_once(ABSPATH . 'wp-admin/admin-header.php');
require_once BU_SLIDESHOW_BASEDIR . 'interface/add-slideshow.php';
}
/**
* Displays Manage Slideshow page.
*/
static public function manage_slideshow_page() {
$slideshows = self::get_slideshows();
if(isset($_GET['msg'])){
$msg = filter_var( $_GET['msg'], FILTER_SANITIZE_STRING );
}
require_once BU_SLIDESHOW_BASEDIR . 'interface/manage-slideshows.php';
}
/**
* Displays Preview Slideshow page.
*/
static public function preview_slideshow_page() {
if (isset($_GET['bu_slideshow_id']) && !empty($_GET['bu_slideshow_id'])) {
$id = intval($_GET['bu_slideshow_id']);
if (!self::slideshow_exists($id)) {
$msg = __("Could not find slideshow.", 'bu-slideshow');
$id = false;
}
}
require_once BU_SLIDESHOW_BASEDIR . 'interface/preview-slideshow.php';
}
/**
* Creates a new, empty slideshow and returns it. IDs begin at 1.
* @param string $name
* @return array
*/
static public function create_slideshow($name) {
if (!is_string($name) || !trim($name)) {
return new WP_Error(__('invalid argument', 'bu-slideshow'), __('Invalid name supplied for slideshow.', 'bu-slideshow'));
}
$show = new BU_Slideshow_Instance();
$show->set_name(trim($name));
$show->update();
return $show;
}
/**
* Returns next unassigned numeric slideshow id. Slideshow ids begin at 1.
* @return int
*/
static public function get_new_id() {
$last_id = get_option(self::$show_id_meta_key, 0);
$new_id = $last_id + 1;
update_option(self::$show_id_meta_key, $new_id);
return $new_id;
}
/**
* Handles AJAX request to delete slideshow
*/
static public function delete_slideshow_ajax() {
if (!isset($_POST['bu_slideshow_nonce'])) {
wp_die(esc_html__("You are not authorized to perform that action.", 'bu-slideshow'));
}
if (!wp_verify_nonce($_POST['bu_slideshow_nonce'], 'bu_delete_slideshow')) {
wp_die(esc_html__("You are not authorized to perform that action.", 'bu-slideshow'));
}
if (!current_user_can(self::$min_cap)) {
wp_die(esc_html__("You do not have the necessary permissions to delete slideshows.", 'bu-slideshow'));
}
if (!isset($_POST['id']) || empty($_POST['id'])) {
return;
}
$id = intval($_POST['id']);
echo wp_kses_post(self::delete_slideshow($id));
exit;
}
/**
* Deletes slideshow with given id if it exists.
*
* @param int $id
* @return int
*/
static public function delete_slideshow($id) {
$id = self::slideshow_maybe_translate_id( $id );
return ( FALSE !== wp_delete_post( $id ) );
}
/**
* Returns true if a slideshow with an id of $id exists.
*
* @param int $id
* @return boolean
*/
static public function slideshow_exists($id) {
$id = self::slideshow_maybe_translate_id( $id );
return ( 'object' === gettype( get_post_meta( $id, '_bu_slideshow', TRUE ) ) );
}
/**
* Determine if slideshow ID was created before v3.2
* If it was, fetch the new post ID.
*
* @param int $id
* @return int
*/
static public function slideshow_maybe_translate_id($id){
$old_slideshow_ids = get_option( 'bu_slideshow_id_map', array() );
if( array_key_exists( $id, $old_slideshow_ids ) ){
$id = $old_slideshow_ids[ $id ];
}
return $id;
}
/**
* Returns slideshow with given id, or false if slideshow doesn't exist.
*
* @param int $id
* @return bool|array
*/
static public function get_slideshow($id) {
$id = self::slideshow_maybe_translate_id( $id );
$slideshow = get_post_meta( $id, '_bu_slideshow', TRUE );
return ( 'object' === gettype( $slideshow ) ) ? $slideshow : FALSE;
}
/**
* Returns array of all slideshows defined.
*
* @return array
*/
static public function get_slideshows() {
$slideshows = array();
$slideshow_ids = get_posts(
array(
'post_type' => 'bu_slideshow',
'posts_per_page' => 500,
'orderby' => 'title',
'order' => 'asc',
'no_found_rows' => true,
'update_post_meta_cache' => false,
'update_post_term_cache' => false,
'fields' => 'ids',
)
);
foreach ( $slideshow_ids as $id ) {
$slideshows[ $id ] = self::get_slideshow( $id );
}
return $slideshows;
}
/**
* Displays and handles submissions from Edit Slideshow page.
*/
static public function edit_slideshow_page() {
$action = !empty( $_POST['bu_slideshow_save_show'] ) ? 'save' : 'view';
$msg = !empty($_GET['msg']) ? filter_var( $_GET['msg'], FILTER_SANITIZE_STRING ) : '';
switch ( $action ) {
case 'save':
if ( !isset($_POST['bu_slideshow_nonce']) || !wp_verify_nonce($_POST['bu_slideshow_nonce'], 'bu_update_slideshow') || !current_user_can(self::$min_cap) ) {
wp_die(esc_html__('You are not authorized to perform this action.', 'bu-slideshow'));
exit;
}
if( !self::slideshow_exists( intval( $_POST['bu_slideshow_id'] ) ) ){
wp_die(esc_html__('Invalid slideshow.', 'bu-slideshow'));
exit;
}
if( !isset( $_POST['bu_slideshow_name'] ) || '' == trim( $_POST['bu_slideshow_name'] ) ){
$msg = __('Could not save slideshow: missing name.', 'bu-slideshow');
break;
} else {
$show = self::get_slideshow(intval($_POST['bu_slideshow_id']));
if( !$show || is_wp_error($show) ){
$msg = __('Error getting slideshow', 'bu-slideshow');
break;
}
}
// we are handling a form submission & all validation complete
self::save_show($show);
$msg = $show->update() ? __("Slideshow updated successfully.", 'bu-slideshow') : __("Slideshow did not save succesfully.", 'bu-slideshow');
break;
case 'view':
if ( !isset($_GET['bu_slideshow_id']) || empty($_GET['bu_slideshow_id']) ) {
wp_die(esc_html__('Invalid slideshow', 'bu-slideshow'));
exit;
}
$show = self::get_slideshow( intval( $_GET['bu_slideshow_id'] ) );
if( !$show || is_wp_error($show) ){
wp_die(esc_html__('Error getting slideshow', 'bu-slideshow'));
exit;
}
break;
}
$show->set_view('admin');
echo wp_kses_post($show->get(array('msg' => $msg)));
}
/**
* Loads edit view for slideshow with given id.
* @param int $id
*/
static public function edit_slideshow_ui($id, $msg = '') {
if (!self::slideshow_exists($id)) {
return;
}
$show = self::get_slideshow($id);
$show->set_view('admin');
echo wp_kses_post($show->get(array('msg' => $msg)));
}
/**
* AJAX handler for adding a new slide.
* @todo add nonce check
*/
static public function add_slide_ajax() {
if (!current_user_can(self::$min_cap)) {
return;
}
$slide = new BU_Slide(array('view' => 'admin', 'order' => $_POST['order']));
echo wp_kses_post($slide->get());
exit;
}
/**
* Echoes slide image thumb data as JSON
* @return string
*/
static public function get_slide_thumb_ajax() {
if (!isset($_POST['image_id']) || !$_POST['image_id']) {
return;
}
$img_info = self::get_slide_image_thumb(intval($_POST['image_id']));
echo wp_kses_post($img_info);
exit;
}
/**
* Generates thumbnail if it doesn't yet exist.
* Supports images uploaded before plugin was activated.
*
* @param int $img_id
* @return bool
*/
static public function generate_slideshow_thumb( $img_id ) {
$img_arr = wp_get_attachment_image_src($img_id, static::$custom_thumb_size);
/* if the regular img url is returned it means we don't have an existing thumb of correct size */
if (strpos($img_arr[0], strval($img_arr[1])) === false) {
$img_path = get_attached_file($img_id);
$success = wp_update_attachment_metadata($img_id, wp_generate_attachment_metadata($img_id, $img_path));
return false !== $success;
}
return true;
}
/**
* Gets thumbnail for custom size. Generates thumbnail if it doesn't exist.
* @return array
*/
static public function get_slide_image_thumb( $img_id ) {
if ( ! static::generate_slideshow_thumb( $img_id ) ) {
error_log( sprintf( '%s: Failed generating thumbnail for image (%s).', __METHOD__, $img_id ) );
}
return wp_get_attachment_image( $img_id, static::$custom_thumb_size );
}
/**
* Implements shortcode. Supported shortcode attributes:
* show_id: mandatory, id of slideshow
* show_nav: optional, whether or not to display slideshow 'navigation'
*
* @global int $bu_slideshow_loadscripts
* @param array $atts
*
* @todo check for presence of titles in all slides, prevent user subitted atts
* from doing anything awkward
*/
static public function shortcode_handler($atts) {
/**
* Trigger loading of plugin scripts in footer in older versions of WP.
*/
if (version_compare(self::$wp_version, '3.3', '<')) {
global $bu_slideshow_loadscripts;
$bu_slideshow_loadscripts = 1;
} else {
wp_enqueue_script('jquery-sequence');
wp_enqueue_script('bu-slideshow');
do_action('bu_slideshow_enqueued');
}
$att_defaults = self::$shortcode_defaults;
$falsish = array('0', 'false', 'no', 'none');
// try to show arrows if no autoplay
if (isset($atts['autoplay']) && in_array(strtolower($atts['autoplay']), $falsish)) {
$att_defaults['show_arrows'] = 1;
}
$atts = shortcode_atts($att_defaults, $atts);
if (!self::slideshow_exists(intval($atts['show_id']))) {
echo '';
return;