-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpaginated-slideshows.php
More file actions
744 lines (653 loc) · 21 KB
/
paginated-slideshows.php
File metadata and controls
744 lines (653 loc) · 21 KB
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
<?php
/**
* Plugin Name: Paginated Slideshows
* Plugin URI: https://github.com/bizbudding/paginated-slideshows
* Description: Create paginated slideshows that can easily be attached to one or more posts.
* Version: 0.2.2
*
* Author: BizBudding, Mike Hemberger
* Author URI: https://bizbudding.com
*
* GitHub Plugin URI: https://github.com/bizbudding/paginated-slideshows
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) exit;
/**
* Main Paginated_Slideshows_Setup Class.
*
* @since 0.1.0
*/
final class Paginated_Slideshows_Setup {
/**
* @var Paginated_Slideshows_Setup The one true Paginated_Slideshows_Setup
* @since 0.1.0
*/
private static $instance;
/**
* Main Paginated_Slideshows_Setup Instance.
*
* Insures that only one instance of Paginated_Slideshows_Setup exists in memory at any one
* time. Also prevents needing to define globals all over the place.
*
* @since 0.1.0
* @static var array $instance
* @uses Paginated_Slideshows_Setup::setup_constants() Setup the constants needed.
* @uses Paginated_Slideshows_Setup::includes() Include the required files.
* @uses Paginated_Slideshows_Setup::setup() Activate, deactivate, etc.
* @see Paginated_Slideshows()
* @return object | Paginated_Slideshows_Setup The one true Paginated_Slideshows_Setup
*/
public static function instance() {
if ( ! isset( self::$instance ) ) {
// Setup the setup
self::$instance = new Paginated_Slideshows_Setup;
// Methods
self::$instance->setup_constants();
self::$instance->includes();
self::$instance->setup();
}
return self::$instance;
}
/**
* Throw error on object clone.
*
* The whole idea of the singleton design pattern is that there is a single
* object therefore, we don't want the object to be cloned.
*
* @since 0.1.0
* @access protected
* @return void
*/
public function __clone() {
// Cloning instances of the class is forbidden.
_doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'paginated-slideshows' ), '1.0' );
}
/**
* Disable unserializing of the class.
*
* @since 0.1.0
* @access protected
* @return void
*/
public function __wakeup() {
// Unserializing instances of the class is forbidden.
_doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'paginated-slideshows' ), '1.0' );
}
/**
* Setup plugin constants.
*
* @since 0.1.0
* @access private
* @return void
*/
private function setup_constants() {
// Plugin version.
if ( ! defined( 'PAGINATED_SLIDESHOWS_VERSION' ) ) {
define( 'PAGINATED_SLIDESHOWS_VERSION', '0.2.2' );
}
// Plugin Folder Path.
if ( ! defined( 'PAGINATED_SLIDESHOWS_DIR' ) ) {
define( 'PAGINATED_SLIDESHOWS_DIR', plugin_dir_path( __FILE__ ) );
}
// Plugin Includes Path
if ( ! defined( 'PAGINATED_SLIDESHOWS_INCLUDES_DIR' ) ) {
define( 'PAGINATED_SLIDESHOWS_INCLUDES_DIR', PAGINATED_SLIDESHOWS_DIR . 'includes/' );
}
// Plugin Folder URL.
if ( ! defined( 'PAGINATED_SLIDESHOWS_URL' ) ) {
define( 'PAGINATED_SLIDESHOWS_URL', plugin_dir_url( __FILE__ ) );
}
// Plugin Root File.
if ( ! defined( 'PAGINATED_SLIDESHOWS_FILE' ) ) {
define( 'PAGINATED_SLIDESHOWS_FILE', __FILE__ );
}
// Plugin Base Name
if ( ! defined( 'PAGINATED_SLIDESHOWS_BASENAME' ) ) {
define( 'PAGINATED_SLIDESHOWS_BASENAME', dirname( plugin_basename( __FILE__ ) ) );
}
}
/**
* Include required files.
*
* @since 0.1.0
* @access private
* @return void
*/
private function includes() {
foreach ( glob( PAGINATED_SLIDESHOWS_INCLUDES_DIR . '*.php' ) as $file ) { include $file; }
}
public function setup() {
add_action( 'admin_init', array( $this, 'updater' ) );
add_action( 'init', array( $this, 'register_content_types' ) );
add_action( 'p2p_init', array( $this, 'register_p2p_connections' ) );
add_action('acf/init', array( $this, 'field_group' ) );
// add_action( 'acf/save_post', array( $this, 'update_slideshow_post_data' ), 20 ); // No longer using, easier to tweak if we dynamically build the markup.
add_action( 'wp_enqueue_scripts', array( $this, 'inline_styles' ), 1000 );
add_action( 'the_post', array( $this, 'create_pages' ) );
register_activation_hook( __FILE__, array( $this, 'activate' ) );
register_deactivation_hook( __FILE__, 'flush_rewrite_rules' );
}
/**
* Initiated the built in plugin updater.
*
* @since 0.1.0
*
* @return void
*/
public function updater() {
/**
* Setup the updater.
*
* @uses https://github.com/YahnisElsts/plugin-update-checker/
*
* @return void
*/
if ( ! class_exists( 'Puc_v4_Factory' ) ) {
require_once PAGINATED_SLIDESHOWS_INCLUDES_DIR . 'vendor/plugin-update-checker/plugin-update-checker.php'; // 4.3.1
} else {
$updater = Puc_v4_Factory::buildUpdateChecker( 'https://github.com/bizbudding/paginated-slideshows/', __FILE__, 'paginated-slideshows' );
}
}
/**
* Register the slideshow custom post type.
*
* @since 0.1.0
*
* @return void
*/
public function register_content_types() {
/***********************
* Custom Post Types *
***********************/
register_post_type( 'slideshow', array(
'exclude_from_search' => true,
'has_archive' => false,
'hierarchical' => false,
'labels' => array(
'name' => _x( 'Slideshows', 'Slideshow general name', 'paginated-slideshows' ),
'singular_name' => _x( 'Slideshow', 'Slideshow singular name', 'paginated-slideshows' ),
'menu_name' => _x( 'Slideshows', 'Slideshow admin menu', 'paginated-slideshows' ),
'name_admin_bar' => _x( 'Slideshow', 'Slideshow add new on admin bar', 'paginated-slideshows' ),
'add_new' => _x( 'Add New', 'Slideshow', 'paginated-slideshows' ),
'add_new_item' => __( 'Add New Slideshow', 'paginated-slideshows' ),
'new_item' => __( 'New Slideshow', 'paginated-slideshows' ),
'edit_item' => __( 'Edit Slideshow', 'paginated-slideshows' ),
'view_item' => __( 'View Slideshow', 'paginated-slideshows' ),
'all_items' => __( 'All Slideshows', 'paginated-slideshows' ),
'search_items' => __( 'Search Slideshows', 'paginated-slideshows' ),
'parent_item_colon' => __( 'Parent Slideshows:', 'paginated-slideshows' ),
'not_found' => __( 'No Slideshows found.', 'paginated-slideshows' ),
'not_found_in_trash' => __( 'No Slideshows found in Trash.', 'paginated-slideshows' )
),
'menu_icon' => 'dashicons-images-alt2',
'public' => false,
'publicly_queryable' => false,
'show_in_menu' => true,
'show_in_nav_menus' => false,
'show_ui' => true,
'rewrite' => false,
'supports' => array( 'title' ),
// 'taxonomies' => array( 'slideshow_cat' ),
) );
}
/**
* Register Posts to Posts connections
*
* @since 0.1.0
*
* @return void
*/
function register_p2p_connections() {
p2p_register_connection_type( array(
'name' => 'slideshow_to_posts',
'from' => 'slideshow',
'to' => 'post',
'cardinality' => 'one-to-many',
'can_create_post' => false,
'from_labels' => array(
'singular_name' => __( 'Slideshow', 'collegespun' ),
'search_items' => __( 'Search slideshows', 'collegespun' ),
'not_found' => __( 'No slideshows found.', 'collegespun' ),
'create' => __( 'Connect a slideshow', 'collegespun' ),
),
'to_labels' => array(
'singular_name' => __( 'Post', 'collegespun' ),
'search_items' => __( 'Search posts', 'collegespun' ),
'not_found' => __( 'No posts found.', 'collegespun' ),
'create' => __( 'Connect a post', 'collegespun' ),
),
) );
}
/**
* NO LONGER USING, BUT SAVING CAUSE IT'S COOL.
*
* Update slideshow meta data into the post content so it's much quicker to load when displaying.
*
* @since 0.1.0
*
* @param int $post_id The post ID.
* @param object $post The post object.
* @param bool $update Whether this is an existing post being updated or not.
*
* @return void
*/
public function update_slideshow_post_data( $post_id ) {
if ( 'slideshow' !== get_post_type( $post_id ) ) {
return;
}
$content = '';
// Unhook this function so it doesn't loop infinitely.
remove_action( 'acf/save_post', array( $this, 'update_slideshow_post_data' ), 20 );
// Get the slides.
$slides = $this->get_field( $post_id, $this->get_config() );
/**
* Get slides.
* The initial $slides array was the entire field group,
* this grabs only the repeater field.
*/
$slides = isset( $slides[ 'slides' ] ) ? $slides[ 'slides' ] : '';
if ( $slides ) {
// http://stackoverflow.com/questions/2348205/how-to-get-last-key-in-an-array
$last_item_key = key( array_slice( $slides, -1, 1, TRUE ) );
foreach ( $slides as $key => $slide ) {
$content .= '<div class="slideshow-slide">';
$content .= $slide['title'] ? '<h3 class="slide-title>' . esc_html($slide['title']) . '</h3>' : '';
$content .= $slide['image'] ? '<div class="slide-image">' . wp_get_attachment_image( $slide['image'], 'featured' ) . '</div>' : '';
$content .= $slide['content'] ? wp_kses_post($slide['content']) : '';
$content .= '</div>';
// If not the last slide, add the next page break.
if ( $key != $last_item_key ) {
$content .= '<!--nextpage-->';
}
}
}
// Update the post, which calls save_post again.
wp_update_post( array(
'ID' => $post_id,
'post_content' => $content,
));
// Re-hook this function.
add_action( 'acf/save_post', array( $this, 'update_slideshow_post_data' ), 20 );
}
/**
* Retrieves all post meta data according to the structure in the $config array.
*
* Provides a convenient and more performant alternative to ACF's get_field().
*
* This function is especially useful when working with ACF repeater fields and
* flexible content layouts.
*
* @link https://www.timjensen.us/acf-get-field-alternative/
*
* @version 1.2.5
* @since 0.1.0
*
* @param integer $post_id Required. Post ID.
* @param array $config Required. An array that represents the structure of
* the custom fields. Follows the same format as the
* ACF export field groups array.
* @return array
*/
function get_field( $post_id, array $config ) {
$results = array();
foreach ( $config as $field ) {
if ( empty( $field['name'] ) ) {
continue;
}
$meta_key = $field['name'];
if ( isset( $field['meta_key_prefix'] ) ) {
$meta_key = $field['meta_key_prefix'] . $meta_key;
}
$field_value = get_post_meta( $post_id, $meta_key, true );
if ( isset( $field['layouts'] ) ) { // We're dealing with flexible content layouts.
if ( empty( $field_value ) ) {
continue;
}
// Build a keyed array of possible layout types.
$layout_types = [];
foreach ( $field['layouts'] as $key => $layout_type ) {
$layout_types[ $layout_type['name'] ] = $layout_type;
}
foreach ( $field_value as $key => $current_layout_type ) {
$new_config = $layout_types[ $current_layout_type ]['sub_fields'];
if ( empty( $new_config ) ) {
continue;
}
foreach ( $new_config as &$field_config ) {
$field_config['meta_key_prefix'] = $meta_key . "_{$key}_";
}
$results[ $field['name'] ][] = array_merge(
[
'acf_fc_layout' => $current_layout_type,
],
$this->get_field( $post_id, $new_config )
);
}
} elseif ( isset( $field['sub_fields'] ) ) { // We're dealing with repeater fields.
if ( empty( $field_value ) ) {
continue;
}
for ( $i = 0; $i < $field_value; $i ++ ) {
$new_config = $field['sub_fields'];
if ( empty( $new_config ) ) {
continue;
}
foreach ( $new_config as &$field_config ) {
$field_config['meta_key_prefix'] = $meta_key . "_{$i}_";
}
$results[ $field['name'] ][] = $this->get_field( $post_id, $new_config );
}
} else {
$results[ $field['name'] ] = $field_value;
} // End if().
} // End foreach().
return $results;
}
/**
* Get the ACF field group config.
*
* @since 0.1.0
*
* @return array The field group config.
*/
public function get_config() {
return array(
array (
'name' => 'slides',
'sub_fields' => array (
array (
'name' => 'title',
),
array (
'name' => 'image',
),
array (
'name' => 'content',
),
),
),
);
}
/**
* Register the field group/metabox.
* @uses Advanced Custom Fields Pro.
*
* @since 0.1.0
*
* @return void
*/
public function field_group() {
acf_add_local_field_group( array(
'key' => 'group_5a314dee51d76',
'title' => 'Slideshows',
'fields' => array(
array(
'key' => 'field_5a314e222cdf7',
'label' => 'Slides',
'name' => 'slides',
'type' => 'repeater',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'collapsed' => 'field_5a314e362cdf8',
'min' => 1,
'max' => 0,
'layout' => 'block',
'button_label' => 'Add Slide',
'sub_fields' => array(
array(
'key' => 'field_5a314e362cdf8',
'label' => 'Title',
'name' => 'title',
'type' => 'text',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'maxlength' => '',
),
array(
'key' => 'field_5a314e5b2cdf9',
'label' => 'Image',
'name' => 'image',
'type' => 'image',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'return_format' => 'id',
'preview_size' => 'thumbnail',
'library' => 'all',
'min_width' => '',
'min_height' => '',
'min_size' => '',
'max_width' => '',
'max_height' => '',
'max_size' => '',
'mime_types' => '',
),
array(
'key' => 'field_5a314eda2cdfa',
'label' => 'Content',
'name' => 'content',
'type' => 'wysiwyg',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'tabs' => 'all',
'toolbar' => 'full',
'media_upload' => 1,
'delay' => 1,
),
),
),
),
'location' => array(
array(
array(
'param' => 'post_type',
'operator' => '==',
'value' => 'slideshow',
),
),
),
'menu_order' => 0,
'position' => 'normal',
'style' => 'default',
'label_placement' => 'top',
'instruction_placement' => 'label',
'hide_on_screen' => '',
'active' => 1,
'description' => '',
) );
}
/**
* Output inline styles in the theme.
*
* @since 0.1.0
*
* @return void
*/
public function inline_styles() {
// wp_register_style( 'paginated-slideshows', PAGINATED_SLIDESHOWS_URL . 'css/paginated-slideshows.css', array(), PAGINATED_SLIDESHOWS_VERSION );
$handle = defined( 'CHILD_THEME_NAME' ) && CHILD_THEME_NAME ? sanitize_title_with_dashes( CHILD_THEME_NAME ) : 'child-theme';
$css = "
.slideshow {
border: 1px solid rgba(0,0,0,0.05);
border-radius: 3px;
}
.slideshow-title {
background-color: #f1f1f1;
font-size: 1.25rem;
text-align: center;
padding: 16px;
margin: 0;
}
.slideshow-slide {
padding: 24px;
}
.slideshow-pagination {
margin-top: 0;
margin-bottom: 24px;
}
.slideshow-pagination ul,
.entry-content .slideshow-pagination ul {
margin-left: 0;
}
.slideshow-pagination a {
min-width: 120px;
font-size: .8rem;
}
";
wp_add_inline_style( $handle, $css );
}
/**
* Get a post's connected slideshow(s)
* and display them.
*
* @return void
*/
public function create_pages( $post ) {
// Bail if Posts 2 Posts is not active.
if ( ! function_exists( 'p2p_type' ) ) {
return;
}
// Bail if not on a single post.
if ( ! is_singular( 'post' ) ) {
return;
}
global $pages, $multipage, $numpages;
// Get the slideshow.
$slideshow = p2p_type( 'slideshow_to_posts' )->get_connected( $post->ID );
// Bail if no slideshow.
if ( ! $slideshow->posts ) {
return;
}
/**
* Get the first slideshow.
* Even if multiple, we can only use one without things breaking.
*/
$slideshow = $slideshow->posts[0];
// Get the slides.
$slides = $this->get_field( $slideshow->ID, $this->get_config() );
// Bail if no slides.
if ( ! $slides ) {
return;
}
/**
* Get slides.
* The initial $slides array was the entire field group,
* this grabs only the repeater field.
*/
$slides = isset( $slides[ 'slides' ] ) ? $slides[ 'slides' ] : '';
// Bail if no slides.
if ( ! $slides ) {
return;
}
/**
* Filter the prev/next page links to add the #slideshow hash.
* @return html
*/
add_filter( 'wp_link_pages_link', function( $link ) {
$jump_to_hash = apply_filters( 'ps_add_slideshow_hash', true );
if ( ! $jump_to_hash ) {
return;
}
return str_replace( '/">', '/#slideshow">', $link );
});
// Remove the default Genesis post pagination.
remove_action( 'genesis_entry_content', 'genesis_do_post_content_nav', 12 );
$total_slides = count( $slides );
$total_pages = count( $pages ) + $total_slides - 1; // Total pages plus the total slides, minus 1 (since the first slide is on the first page).
$page_index = count( $pages ) - 1;
// If we have more than 1 slide, update the globals.
if ( $total_slides > 1 ) {
$multipage = true;
$numpages = $total_pages;
}
// Loop through pages.
foreach ( (array) $slides as $slide ) {
// Prevent building a page that doesn't exist yet.
if ( isset( $pages[$page_index] ) ) {
$pages[$page_index] = $pages[$page_index];
} else {
$pages[$page_index] = '';
}
$pages[$page_index] .= '<div id="slideshow" class="slideshow">';
$pages[$page_index] .= '<h2 class="slideshow-title">' . get_the_title( $slideshow->ID ) . '</h2>';
$pages[$page_index] .= '<div class="slideshow-slide">';
$pages[$page_index] .= $slide['title'] ? '<h3 class="slide-title">' . esc_html( $slide['title'] ) . '</h3>' : '';
$pages[$page_index] .= $slide['image'] ? '<p class="slide-image">' . wp_get_attachment_image( $slide['image'], 'featured' ) . '</p>' : '';
$pages[$page_index] .= $slide['content'] ? wp_kses_post( $slide['content'] ) : '';
$pages[$page_index] .= '</div>';
$pages[$page_index] .= wp_link_pages( array(
'before' => '<div class="slideshow-pagination archive-pagination pagination">',
'after' => '</div>',
'link_before' => '',
'link_after' => '',
'next_or_number' => 'next',
'separator' => '',
'previouspagelink' => '« Previous',
'nextpagelink' => 'Next »',
'pagelink' => '%',
'echo' => 0,
) );
$pages[$page_index] .= '</div>';
$page_index++;
}
}
/**
* Activate, register content types and flush permalinks.
*
* @since 0.1.0
*
* @return void
*/
public function activate() {
$this->register_content_types();
flush_rewrite_rules();
}
}
/**
* The main function for that returns Paginated_Slideshows_Setup
*
* The main function responsible for returning the one true Paginated_Slideshows_Setup
* Instance to functions everywhere.
*
* Use this function like you would a global variable, except without needing
* to declare the global.
*
* Example: <?php $plugin = Paginated_Slideshows(); ?>
*
* @since 0.1.0
*
* @return object|Paginated_Slideshows_Setup The one true Paginated_Slideshows_Setup Instance.
*/
function Paginated_Slideshows() {
return Paginated_Slideshows_Setup::instance();
}
// Get Paginated_Slideshows Running.
Paginated_Slideshows();