class-media-library-organizer-shortcode.php
3.71 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
<?php
/**
* Gallery Shortcode class.
*
* @package Media_Library_Organizer
* @author WP Media Library
*/
/**
* Registers filters for the [gallery] shortcode for use by e.g.
* Dynamic Galleries.
*
* @since 1.0.8
*/
class Media_Library_Organizer_Shortcode {
/**
* Holds the base class object.
*
* @since 1.0.0
*
* @var object
*/
public $base;
/**
* Shortcode prefix.
*
* @since 1.4.9
*
* @var string
*/
private $prefix = 'media_library_organizer';
/**
* Constructor
*
* @since 1.0.8
*
* @param object $base Base Plugin Class.
*/
public function __construct( $base ) {
// Store base class.
$this->base = $base;
// Register shortcodes in WordPress.
add_action( 'init', array( $this, 'register' ), 10, 1 );
// Register [gallery] shortcode filters.
add_filter( 'post_gallery', array( $this, 'output_gallery' ), 10, 3 );
}
/**
* Register shortcodes in WordPress.
*
* @since 1.4.9
*/
public function register() {
// Get shortcodes.
$shortcodes = $this->get();
// Bail if no shortcodes are available.
if ( ! is_array( $shortcodes ) || ! count( $shortcodes ) ) {
return;
}
// Iterate through shortcodes, registering them as shortcodes.
foreach ( $shortcodes as $shortcode => $properties ) {
// Register shortcode.
add_shortcode(
$this->get_prefix() . '_' . $shortcode,
array(
$properties['render_callback'][0],
$properties['render_callback'][1],
)
);
}
}
/**
* Returns the namespaced prefix for shortcodes e.g. `media_library_organizer`
*
* @since 1.4.9
*
* @return string
*/
public function get_prefix() {
return $this->prefix;
}
/**
* Returns registered shortcodes.
*
* @since 1.4.9
*
* @return array Shortcodes
*/
public function get() {
$shortcodes = array();
/**
* Registers shortcodes.
*
* @since 1.4.9
*
* @param array $shortcodes Shortcodes
*/
$shortcodes = apply_filters( 'media_library_organizer_shortcode_register', $shortcodes );
return $shortcodes;
}
/**
* Unregisters filters added by Addons to the [gallery] shortcode output
*
* @since 1.0.9
*/
public function unregister_gallery_filters() {
remove_all_filters( 'media_library_organizer_shortcode_output_gallery' );
}
/**
* Re-registers filters for the [gallery] shortcode output if they have been removed
* using unregister_gallery_filters() during a request.
*
* @since 1.0.9
*/
public function reregister_gallery_filters() {
do_action( 'media_library_organizer_shortcode_reregister_gallery_filters' );
}
/**
* Allows Addons to modify the [gallery] shortcode HTML
*
* If output is blank, WordPress' gallery_shortcode() will provide the output
* as a fallback.
*
* @since 1.0.8
*
* @param string $output HTML Output (if blank, gallery_shortcode() is used).
* @param array $atts Shortcode Attributes.
* @param ? $instance Shortcode Instance.
* @return string HTML Output (if blank, gallery_shortcode() is used)
*/
public function output_gallery( $output, $atts, $instance ) {
/**
* Allows Addons to modify the [gallery] shortcode HTML
*
* If output is blank, WordPress' gallery_shortcode() will provide the output
* as a fallback.
*
* @since 1.0.8
*
* @param string $output HTML Output (if blank, gallery_shortcode() is used).
* @param array $atts Shortcode Attributes.
* @param ? $instance Shortcode Instance.
* @return string HTML Output (if blank, gallery_shortcode() is used)
*/
$output = apply_filters( 'media_library_organizer_shortcode_output_gallery', $output, $atts, $instance );
return $output;
}
}