1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
namespace Rarst\Fragment_Cache;
|
4
|
|
|
|
5
|
|
|
/**
|
6
|
|
|
* Cache galleries.
|
7
|
|
|
*/
|
8
|
|
|
class Gallery_Cache extends Fragment_Cache {
|
9
|
|
|
|
10
|
|
|
/** @var mixed $original_shortcode Callback originally registered for gallery shortcode. */
|
11
|
|
|
protected $original_shortcode;
|
12
|
|
|
|
13
|
|
|
/**
|
14
|
|
|
* @inheritDoc
|
15
|
|
|
*/
|
16
|
|
|
public function enable() {
|
17
|
|
|
|
18
|
|
|
global $shortcode_tags;
|
|
|
|
|
19
|
|
|
|
20
|
|
|
if ( isset( $shortcode_tags['gallery'] ) ) {
|
21
|
|
|
$this->original_shortcode = $shortcode_tags['gallery'];
|
22
|
|
|
}
|
23
|
|
|
|
24
|
|
|
add_shortcode( 'gallery', array( $this, 'gallery_shortcode' ) );
|
25
|
|
|
}
|
26
|
|
|
|
27
|
|
|
/**
|
28
|
|
|
* @inheritDoc
|
29
|
|
|
*/
|
30
|
|
|
public function disable() {
|
31
|
|
|
|
32
|
|
|
if ( ! empty( $this->original_shortcode ) ) {
|
33
|
|
|
add_shortcode( 'gallery', $this->original_shortcode );
|
34
|
|
|
}
|
35
|
|
|
}
|
36
|
|
|
|
37
|
|
|
/**
|
38
|
|
|
* Fetch and return cached gallery.
|
39
|
|
|
*
|
40
|
|
|
* @param array $args Gallery arguments.
|
41
|
|
|
*
|
42
|
|
|
* @return string
|
43
|
|
|
*/
|
44
|
|
|
public function gallery_shortcode( $args ) {
|
45
|
|
|
|
46
|
|
|
if ( empty( $args ) ) {
|
47
|
|
|
$args = array();
|
48
|
|
|
}
|
49
|
|
|
|
50
|
|
|
// Salt for cases post edited or attachments changed.
|
51
|
|
|
$args['fc_post_modified'] = get_the_modified_time( 'U' );
|
52
|
|
|
$args['fc_post_attachments'] = $this->get_attachment_ids();
|
53
|
|
|
|
54
|
|
|
$post_id = get_the_ID();
|
55
|
|
|
$output = $this->fetch( 'post-' . $post_id, compact( 'args', 'post_id' ), $args );
|
56
|
|
|
|
57
|
|
|
return $output;
|
58
|
|
|
}
|
59
|
|
|
|
60
|
|
|
/**
|
61
|
|
|
* Retrieve array of attachment IDs for current post.
|
62
|
|
|
*
|
63
|
|
|
* @return array
|
64
|
|
|
*/
|
65
|
|
|
public function get_attachment_ids() {
|
66
|
|
|
|
67
|
|
|
static $attachments = array();
|
68
|
|
|
|
69
|
|
|
$post_id = get_the_ID();
|
70
|
|
|
|
71
|
|
|
if ( ! isset( $attachments[ $post_id ] ) ) {
|
72
|
|
|
|
73
|
|
|
$attachments[ $post_id ] = get_posts( array(
|
74
|
|
|
'post_type' => 'attachment',
|
75
|
|
|
'post_parent' => $post_id,
|
76
|
|
|
'orderby' => 'ID',
|
77
|
|
|
'fields' => 'ids',
|
78
|
|
|
) );
|
79
|
|
|
}
|
80
|
|
|
|
81
|
|
|
return $attachments[ $post_id ];
|
82
|
|
|
}
|
83
|
|
|
|
84
|
|
|
/**
|
85
|
|
|
* Set up post context and generate gallery output.
|
86
|
|
|
*
|
87
|
|
|
* @param string $name Fragment name.
|
88
|
|
|
* @param array $args Arguments.
|
89
|
|
|
*
|
90
|
|
|
* @return string
|
91
|
|
|
*/
|
92
|
|
|
protected function callback( $name, $args ) {
|
93
|
|
|
|
94
|
|
|
global $post;
|
|
|
|
|
95
|
|
|
|
96
|
|
|
$post = get_post( $args['post_id'] );
|
97
|
|
|
setup_postdata( $post );
|
98
|
|
|
$shortcode = isset( $this->original_shortcode ) ? $this->original_shortcode : 'gallery_shortcode';
|
99
|
|
|
$output = call_user_func( $shortcode, $args['args'] ) . $this->get_comment( $name );
|
100
|
|
|
wp_reset_postdata();
|
101
|
|
|
|
102
|
|
|
return $output;
|
103
|
|
|
}
|
104
|
|
|
}
|
105
|
|
|
|
Instead of relying on
global
state, we recommend one of these alternatives:1. Pass all data via parameters
2. Create a class that maintains your state