1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
// Include the class file containing methods for rounding constrained array elements. |
4
|
|
|
// Here the constrained array element is the dimension of a row, group or an image in the tiled gallery. |
5
|
|
|
require_once dirname( __FILE__ ) . '/math/class-constrained-array-rounding.php'; |
6
|
|
|
|
7
|
|
|
// Layouts |
8
|
|
|
require_once dirname( __FILE__ ) . '/tiled-gallery/tiled-gallery-rectangular.php'; |
9
|
|
|
require_once dirname( __FILE__ ) . '/tiled-gallery/tiled-gallery-square.php'; |
10
|
|
|
require_once dirname( __FILE__ ) . '/tiled-gallery/tiled-gallery-circle.php'; |
11
|
|
|
|
12
|
|
|
class Jetpack_Tiled_Gallery { |
13
|
|
|
private static $talaveras = array( 'rectangular', 'square', 'circle', 'rectangle', 'columns' ); |
14
|
|
|
|
15
|
|
|
public function __construct() { |
16
|
|
|
add_action( 'admin_init', array( $this, 'settings_api_init' ) ); |
17
|
|
|
add_filter( 'jetpack_gallery_types', array( $this, 'jetpack_gallery_types' ), 9 ); |
18
|
|
|
add_filter( 'jetpack_default_gallery_type', array( $this, 'jetpack_default_gallery_type' ) ); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function tiles_enabled() { |
22
|
|
|
// Check the setting status |
23
|
|
|
return '' != Jetpack_Options::get_option_and_ensure_autoload( 'tiled_galleries', '' ); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function set_atts( $atts ) { |
27
|
|
|
global $post; |
28
|
|
|
|
29
|
|
|
$this->atts = shortcode_atts( |
|
|
|
|
30
|
|
|
array( |
31
|
|
|
'order' => 'ASC', |
32
|
|
|
'orderby' => 'menu_order ID', |
33
|
|
|
'id' => isset( $post->ID ) ? $post->ID : 0, |
34
|
|
|
'include' => '', |
35
|
|
|
'exclude' => '', |
36
|
|
|
'type' => '', |
37
|
|
|
'grayscale' => false, |
38
|
|
|
'link' => '', |
39
|
|
|
'columns' => 3, |
40
|
|
|
), |
41
|
|
|
$atts, |
42
|
|
|
'gallery' |
43
|
|
|
); |
44
|
|
|
|
45
|
|
|
$this->atts['id'] = (int) $this->atts['id']; |
46
|
|
|
$this->float = is_rtl() ? 'right' : 'left'; |
|
|
|
|
47
|
|
|
|
48
|
|
|
// Default to rectangular is tiled galleries are checked |
49
|
|
|
if ( $this->tiles_enabled() && ( ! $this->atts['type'] || 'default' == $this->atts['type'] ) ) { |
50
|
|
|
/** This filter is already documented in functions.gallery.php */ |
51
|
|
|
$this->atts['type'] = apply_filters( 'jetpack_default_gallery_type', 'rectangular' ); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if ( ! $this->atts['orderby'] ) { |
55
|
|
|
$this->atts['orderby'] = sanitize_sql_orderby( $this->atts['orderby'] ); |
56
|
|
|
if ( ! $this->atts['orderby'] ) { |
57
|
|
|
$this->atts['orderby'] = 'menu_order ID'; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if ( 'rand' == strtolower( $this->atts['order'] ) ) { |
62
|
|
|
$this->atts['orderby'] = 'rand'; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
// We shouldn't have more than 20 columns. |
66
|
|
|
if ( ! is_numeric( $this->atts['columns'] ) || 20 < $this->atts['columns'] ) { |
67
|
|
|
$this->atts['columns'] = 3; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function get_attachments() { |
72
|
|
|
extract( $this->atts ); |
73
|
|
|
|
74
|
|
|
if ( ! empty( $include ) ) { |
75
|
|
|
$include = preg_replace( '/[^0-9,]+/', '', $include ); |
76
|
|
|
$_attachments = get_posts( |
77
|
|
|
array( |
78
|
|
|
'include' => $include, |
79
|
|
|
'post_status' => 'inherit', |
80
|
|
|
'post_type' => 'attachment', |
81
|
|
|
'post_mime_type' => 'image', |
82
|
|
|
'order' => $order, |
83
|
|
|
'orderby' => $orderby, |
84
|
|
|
'suppress_filters' => false, |
85
|
|
|
) |
86
|
|
|
); |
87
|
|
|
|
88
|
|
|
$attachments = array(); |
89
|
|
|
foreach ( $_attachments as $key => $val ) { |
90
|
|
|
$attachments[ $val->ID ] = $_attachments[ $key ]; |
91
|
|
|
} |
92
|
|
|
} elseif ( 0 == $id ) { |
93
|
|
|
// Should NEVER Happen but infinite_scroll_load_other_plugins_scripts means it does |
94
|
|
|
// Querying with post_parent == 0 can generate stupidly memcache sets on sites with 10000's of unattached attachments as get_children puts every post in the cache. |
95
|
|
|
// TODO Fix this properly |
96
|
|
|
$attachments = array(); |
97
|
|
|
} elseif ( ! empty( $exclude ) ) { |
98
|
|
|
$exclude = preg_replace( '/[^0-9,]+/', '', $exclude ); |
99
|
|
|
$attachments = get_children( |
100
|
|
|
array( |
101
|
|
|
'post_parent' => $id, |
102
|
|
|
'exclude' => $exclude, |
103
|
|
|
'post_status' => 'inherit', |
104
|
|
|
'post_type' => 'attachment', |
105
|
|
|
'post_mime_type' => 'image', |
106
|
|
|
'order' => $order, |
107
|
|
|
'orderby' => $orderby, |
108
|
|
|
'suppress_filters' => false, |
109
|
|
|
) |
110
|
|
|
); |
111
|
|
|
} else { |
112
|
|
|
$attachments = get_children( |
113
|
|
|
array( |
114
|
|
|
'post_parent' => $id, |
115
|
|
|
'post_status' => 'inherit', |
116
|
|
|
'post_type' => 'attachment', |
117
|
|
|
'post_mime_type' => 'image', |
118
|
|
|
'order' => $order, |
119
|
|
|
'orderby' => $orderby, |
120
|
|
|
'suppress_filters' => false, |
121
|
|
|
) |
122
|
|
|
); |
123
|
|
|
} |
124
|
|
|
return $attachments; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
View Code Duplication |
public static function default_scripts_and_styles() { |
128
|
|
|
wp_enqueue_script( |
129
|
|
|
'tiled-gallery', |
130
|
|
|
Jetpack::get_file_url_for_environment( |
131
|
|
|
'_inc/build/tiled-gallery/tiled-gallery/tiled-gallery.min.js', |
132
|
|
|
'modules/tiled-gallery/tiled-gallery/tiled-gallery.js' |
133
|
|
|
), |
134
|
|
|
array( 'jquery' ) |
135
|
|
|
); |
136
|
|
|
wp_enqueue_style( 'tiled-gallery', plugins_url( 'tiled-gallery/tiled-gallery.css', __FILE__ ), array(), '2012-09-21' ); |
137
|
|
|
wp_style_add_data( 'tiled-gallery', 'rtl', 'replace' ); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function gallery_shortcode( $val, $atts ) { |
141
|
|
|
if ( ! empty( $val ) ) { // something else is overriding post_gallery, like a custom VIP shortcode |
142
|
|
|
return $val; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
global $post; |
146
|
|
|
|
147
|
|
|
$this->set_atts( $atts ); |
148
|
|
|
|
149
|
|
|
$attachments = $this->get_attachments(); |
150
|
|
|
if ( empty( $attachments ) ) { |
151
|
|
|
return ''; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
if ( is_feed() || defined( 'IS_HTML_EMAIL' ) ) { |
155
|
|
|
return ''; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
if ( |
159
|
|
|
in_array( |
160
|
|
|
$this->atts['type'], |
161
|
|
|
/** |
162
|
|
|
* Filters the permissible Tiled Gallery types. |
163
|
|
|
* |
164
|
|
|
* @module tiled-gallery |
165
|
|
|
* |
166
|
|
|
* @since 3.7.0 |
167
|
|
|
* |
168
|
|
|
* @param array Array of allowed types. Default: 'rectangular', 'square', 'circle', 'rectangle', 'columns'. |
169
|
|
|
*/ |
170
|
|
|
$talaveras = apply_filters( 'jetpack_tiled_gallery_types', self::$talaveras ) |
171
|
|
|
) |
172
|
|
|
) { |
173
|
|
|
// Enqueue styles and scripts |
174
|
|
|
self::default_scripts_and_styles(); |
175
|
|
|
|
176
|
|
|
// Generate gallery HTML |
177
|
|
|
$gallery_class = 'Jetpack_Tiled_Gallery_Layout_' . ucfirst( $this->atts['type'] ); |
178
|
|
|
$gallery = new $gallery_class( $attachments, $this->atts['link'], $this->atts['grayscale'], (int) $this->atts['columns'] ); |
179
|
|
|
$gallery_html = $gallery->HTML(); |
180
|
|
|
|
181
|
|
|
if ( $gallery_html && class_exists( 'Jetpack' ) && class_exists( 'Jetpack_Photon' ) ) { |
182
|
|
|
// Tiled Galleries in Jetpack require that Photon be active. |
183
|
|
|
// If it's not active, run it just on the gallery output. |
184
|
|
|
if ( ! in_array( 'photon', Jetpack::get_active_modules() ) && ! Jetpack::is_development_mode() ) { |
185
|
|
|
$gallery_html = Jetpack_Photon::filter_the_content( $gallery_html ); |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
return trim( preg_replace( '/\s+/', ' ', $gallery_html ) ); // remove any new lines from the output so that the reader parses it better |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
return ''; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
public static function gallery_already_redefined() { |
196
|
|
|
global $shortcode_tags; |
197
|
|
|
$redefined = false; |
198
|
|
|
if ( ! isset( $shortcode_tags['gallery'] ) || $shortcode_tags['gallery'] !== 'gallery_shortcode' ) { |
199
|
|
|
$redefined = true; |
200
|
|
|
} |
201
|
|
|
/** |
202
|
|
|
* Filter the output of the check for another plugin or theme affecting WordPress galleries. |
203
|
|
|
* |
204
|
|
|
* This will let folks that replace core’s shortcode confirm feature parity with it, so Jetpack's Tiled Galleries can still work. |
205
|
|
|
* |
206
|
|
|
* @module tiled-gallery |
207
|
|
|
* |
208
|
|
|
* @since 3.1.0 |
209
|
|
|
* |
210
|
|
|
* @param bool $redefined Does another plugin or theme already redefines the default WordPress gallery? |
211
|
|
|
*/ |
212
|
|
|
return apply_filters( 'jetpack_tiled_gallery_shortcode_redefined', $redefined ); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
public static function init() { |
216
|
|
|
if ( self::gallery_already_redefined() ) { |
217
|
|
|
return; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
$gallery = new Jetpack_Tiled_Gallery(); |
221
|
|
|
add_filter( 'post_gallery', array( $gallery, 'gallery_shortcode' ), 1001, 2 ); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
public static function get_content_width() { |
225
|
|
|
$tiled_gallery_content_width = Jetpack::get_content_width(); |
226
|
|
|
|
227
|
|
|
if ( ! $tiled_gallery_content_width ) { |
228
|
|
|
$tiled_gallery_content_width = 500; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Filter overwriting the default content width. |
233
|
|
|
* |
234
|
|
|
* @module tiled-gallery |
235
|
|
|
* |
236
|
|
|
* @since 2.1.0 |
237
|
|
|
* |
238
|
|
|
* @param string $tiled_gallery_content_width Default Tiled Gallery content width. |
239
|
|
|
*/ |
240
|
|
|
return apply_filters( 'tiled_gallery_content_width', $tiled_gallery_content_width ); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Media UI integration |
245
|
|
|
*/ |
246
|
|
|
function jetpack_gallery_types( $types ) { |
247
|
|
|
if ( get_option( 'tiled_galleries' ) && isset( $types['default'] ) ) { |
248
|
|
|
// Tiled is set as the default, meaning that type='default' |
249
|
|
|
// will still display the mosaic. |
250
|
|
|
$types['thumbnails'] = $types['default']; |
251
|
|
|
unset( $types['default'] ); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
$types['rectangular'] = __( 'Tiled Mosaic', 'jetpack' ); |
255
|
|
|
$types['square'] = __( 'Square Tiles', 'jetpack' ); |
256
|
|
|
$types['circle'] = __( 'Circles', 'jetpack' ); |
257
|
|
|
$types['columns'] = __( 'Tiled Columns', 'jetpack' ); |
258
|
|
|
|
259
|
|
|
return $types; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
function jetpack_default_gallery_type() { |
263
|
|
|
return ( get_option( 'tiled_galleries' ) ? 'rectangular' : 'default' ); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
static function get_talaveras() { |
267
|
|
|
return self::$talaveras; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Add a checkbox field to the Carousel section in Settings > Media |
272
|
|
|
* for setting tiled galleries as the default. |
273
|
|
|
*/ |
274
|
|
|
function settings_api_init() { |
275
|
|
|
global $wp_settings_sections; |
276
|
|
|
|
277
|
|
|
// Add the setting field [tiled_galleries] and place it in Settings > Media |
278
|
|
|
if ( isset( $wp_settings_sections['media']['carousel_section'] ) ) { |
279
|
|
|
$section = 'carousel_section'; |
280
|
|
|
} else { |
281
|
|
|
$section = 'default'; |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
add_settings_field( 'tiled_galleries', __( 'Tiled Galleries', 'jetpack' ), array( $this, 'setting_html' ), 'media', $section ); |
285
|
|
|
register_setting( 'media', 'tiled_galleries', 'esc_attr' ); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
function setting_html() { |
289
|
|
|
echo '<label><input name="tiled_galleries" type="checkbox" value="1" ' . |
290
|
|
|
checked( 1, '' != get_option( 'tiled_galleries' ), false ) . ' /> ' . |
291
|
|
|
__( 'Display all your gallery pictures in a cool mosaic.', 'jetpack' ) . '</br></label>'; |
292
|
|
|
} |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
add_action( 'init', array( 'Jetpack_Tiled_Gallery', 'init' ) ); |
296
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: