Completed
Push — fix/product-review-comment-end... ( fcd4b9...b9b2bf )
by Justin
174:21 queued 168:12
created

Jetpack_Tiled_Gallery::load_block_assets()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
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
		jetpack_register_block(
21
			'tiled-gallery',
22
			array(
23
				'render_callback' => array( $this, 'load_block_assets' ),
24
			)
25
		);
26
	}
27
28
	/**
29
	 * Tiled gallery block registration/dependency declaration.
30
	 *
31
	 * @param array  $attr - Array containing the block attributes.
32
	 * @param string $content - String containing the block content.
33
	 *
34
	 * @return string
35
	 */
36
	public function load_block_assets( $attr, $content ) {
37
		$dependencies = array(
38
			'lodash',
39
			'wp-i18n',
40
			'wp-token-list',
41
		);
42
		Jetpack_Gutenberg::load_assets_as_required( 'tiled-gallery', $dependencies );
43
		return $content;
44
	}
45
46
	public function tiles_enabled() {
47
		// Check the setting status
48
		return '' != Jetpack_Options::get_option_and_ensure_autoload( 'tiled_galleries', '' );
49
	}
50
51
	public function set_atts( $atts ) {
52
		global $post;
53
54
		$this->atts = shortcode_atts(
0 ignored issues
show
Bug introduced by
The property atts does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
55
			array(
56
				'order'     => 'ASC',
57
				'orderby'   => 'menu_order ID',
58
				'id'        => isset( $post->ID ) ? $post->ID : 0,
59
				'include'   => '',
60
				'exclude'   => '',
61
				'type'      => '',
62
				'grayscale' => false,
63
				'link'      => '',
64
				'columns'   => 3,
65
			),
66
			$atts,
67
			'gallery'
68
		);
69
70
		$this->atts['id'] = (int) $this->atts['id'];
71
		$this->float      = is_rtl() ? 'right' : 'left';
0 ignored issues
show
Bug introduced by
The property float does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
72
73
		// Default to rectangular is tiled galleries are checked
74
		if ( $this->tiles_enabled() && ( ! $this->atts['type'] || 'default' == $this->atts['type'] ) ) {
75
			/** This filter is already documented in functions.gallery.php */
76
			$this->atts['type'] = apply_filters( 'jetpack_default_gallery_type', 'rectangular' );
77
		}
78
79
		if ( ! $this->atts['orderby'] ) {
80
			$this->atts['orderby'] = sanitize_sql_orderby( $this->atts['orderby'] );
81
			if ( ! $this->atts['orderby'] ) {
82
				$this->atts['orderby'] = 'menu_order ID';
83
			}
84
		}
85
86
		if ( 'rand' == strtolower( $this->atts['order'] ) ) {
87
			$this->atts['orderby'] = 'rand';
88
		}
89
90
		// We shouldn't have more than 20 columns.
91
		if ( ! is_numeric( $this->atts['columns'] ) || 20 < $this->atts['columns'] ) {
92
			$this->atts['columns'] = 3;
93
		}
94
	}
95
96
	public function get_attachments() {
97
		extract( $this->atts );
98
99
		if ( ! empty( $include ) ) {
100
			$include      = preg_replace( '/[^0-9,]+/', '', $include );
101
			$_attachments = get_posts(
102
				array(
103
					'include'          => $include,
104
					'post_status'      => 'inherit',
105
					'post_type'        => 'attachment',
106
					'post_mime_type'   => 'image',
107
					'order'            => $order,
108
					'orderby'          => $orderby,
109
					'suppress_filters' => false,
110
				)
111
			);
112
113
			$attachments = array();
114
			foreach ( $_attachments as $key => $val ) {
115
				$attachments[ $val->ID ] = $_attachments[ $key ];
116
			}
117
		} elseif ( 0 == $id ) {
118
			// Should NEVER Happen but infinite_scroll_load_other_plugins_scripts means it does
119
			// 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.
120
			// TODO Fix this properly
121
			$attachments = array();
122
		} elseif ( ! empty( $exclude ) ) {
123
			$exclude     = preg_replace( '/[^0-9,]+/', '', $exclude );
124
			$attachments = get_children(
125
				array(
126
					'post_parent'      => $id,
127
					'exclude'          => $exclude,
128
					'post_status'      => 'inherit',
129
					'post_type'        => 'attachment',
130
					'post_mime_type'   => 'image',
131
					'order'            => $order,
132
					'orderby'          => $orderby,
133
					'suppress_filters' => false,
134
				)
135
			);
136
		} else {
137
			$attachments = get_children(
138
				array(
139
					'post_parent'      => $id,
140
					'post_status'      => 'inherit',
141
					'post_type'        => 'attachment',
142
					'post_mime_type'   => 'image',
143
					'order'            => $order,
144
					'orderby'          => $orderby,
145
					'suppress_filters' => false,
146
				)
147
			);
148
		}
149
		return $attachments;
150
	}
151
152 View Code Duplication
	public static function default_scripts_and_styles() {
153
		wp_enqueue_script(
154
			'tiled-gallery',
155
			Jetpack::get_file_url_for_environment(
156
				'_inc/build/tiled-gallery/tiled-gallery/tiled-gallery.min.js',
157
				'modules/tiled-gallery/tiled-gallery/tiled-gallery.js'
158
			),
159
			array( 'jquery' )
160
		);
161
		wp_enqueue_style( 'tiled-gallery', plugins_url( 'tiled-gallery/tiled-gallery.css', __FILE__ ), array(), '2012-09-21' );
162
		wp_style_add_data( 'tiled-gallery', 'rtl', 'replace' );
163
	}
164
165
	public function gallery_shortcode( $val, $atts ) {
166
		if ( ! empty( $val ) ) { // something else is overriding post_gallery, like a custom VIP shortcode
167
			return $val;
168
		}
169
170
		global $post;
171
172
		$this->set_atts( $atts );
173
174
		$attachments = $this->get_attachments();
175
		if ( empty( $attachments ) ) {
176
			return '';
177
		}
178
179
		if ( is_feed() || defined( 'IS_HTML_EMAIL' ) ) {
180
			return '';
181
		}
182
183
		if (
184
			in_array(
185
				$this->atts['type'],
186
				/**
187
				 * Filters the permissible Tiled Gallery types.
188
				 *
189
				 * @module tiled-gallery
190
				 *
191
				 * @since 3.7.0
192
				 *
193
				 * @param array Array of allowed types. Default: 'rectangular', 'square', 'circle', 'rectangle', 'columns'.
194
				 */
195
				$talaveras = apply_filters( 'jetpack_tiled_gallery_types', self::$talaveras )
196
			)
197
		) {
198
			// Enqueue styles and scripts
199
			self::default_scripts_and_styles();
200
201
			// Generate gallery HTML
202
			$gallery_class = 'Jetpack_Tiled_Gallery_Layout_' . ucfirst( $this->atts['type'] );
203
			$gallery       = new $gallery_class( $attachments, $this->atts['link'], $this->atts['grayscale'], (int) $this->atts['columns'] );
204
			$gallery_html  = $gallery->HTML();
205
206
			if ( $gallery_html && class_exists( 'Jetpack' ) && class_exists( 'Jetpack_Photon' ) ) {
207
				// Tiled Galleries in Jetpack require that Photon be active.
208
				// If it's not active, run it just on the gallery output.
209
				if ( ! in_array( 'photon', Jetpack::get_active_modules() ) && ! Jetpack::is_development_mode() ) {
210
					$gallery_html = Jetpack_Photon::filter_the_content( $gallery_html );
211
				}
212
			}
213
214
			return trim( preg_replace( '/\s+/', ' ', $gallery_html ) ); // remove any new lines from the output so that the reader parses it better
215
		}
216
217
		return '';
218
	}
219
220
	public static function gallery_already_redefined() {
221
		global $shortcode_tags;
222
		$redefined = false;
223
		if ( ! isset( $shortcode_tags['gallery'] ) || $shortcode_tags['gallery'] !== 'gallery_shortcode' ) {
224
			$redefined = true;
225
		}
226
		/**
227
		 * Filter the output of the check for another plugin or theme affecting WordPress galleries.
228
		 *
229
		 * This will let folks that replace core’s shortcode confirm feature parity with it, so Jetpack's Tiled Galleries can still work.
230
		 *
231
		 * @module tiled-gallery
232
		 *
233
		 * @since 3.1.0
234
		 *
235
		 * @param bool $redefined Does another plugin or theme already redefines the default WordPress gallery?
236
		 */
237
		return apply_filters( 'jetpack_tiled_gallery_shortcode_redefined', $redefined );
238
	}
239
240
	public static function init() {
241
		if ( self::gallery_already_redefined() ) {
242
			return;
243
		}
244
245
		$gallery = new Jetpack_Tiled_Gallery();
246
		add_filter( 'post_gallery', array( $gallery, 'gallery_shortcode' ), 1001, 2 );
247
	}
248
249
	public static function get_content_width() {
250
		$tiled_gallery_content_width = Jetpack::get_content_width();
251
252
		if ( ! $tiled_gallery_content_width ) {
253
			$tiled_gallery_content_width = 500;
254
		}
255
256
		/**
257
		 * Filter overwriting the default content width.
258
		 *
259
		 * @module tiled-gallery
260
		 *
261
		 * @since 2.1.0
262
		 *
263
		 * @param string $tiled_gallery_content_width Default Tiled Gallery content width.
264
		 */
265
		return apply_filters( 'tiled_gallery_content_width', $tiled_gallery_content_width );
266
	}
267
268
	/**
269
	 * Media UI integration
270
	 */
271
	function jetpack_gallery_types( $types ) {
272
		if ( get_option( 'tiled_galleries' ) && isset( $types['default'] ) ) {
273
			// Tiled is set as the default, meaning that type='default'
274
			// will still display the mosaic.
275
			$types['thumbnails'] = $types['default'];
276
			unset( $types['default'] );
277
		}
278
279
		$types['rectangular'] = __( 'Tiled Mosaic', 'jetpack' );
280
		$types['square']      = __( 'Square Tiles', 'jetpack' );
281
		$types['circle']      = __( 'Circles', 'jetpack' );
282
		$types['columns']     = __( 'Tiled Columns', 'jetpack' );
283
284
		return $types;
285
	}
286
287
	function jetpack_default_gallery_type() {
288
		return ( get_option( 'tiled_galleries' ) ? 'rectangular' : 'default' );
289
	}
290
291
	static function get_talaveras() {
292
		return self::$talaveras;
293
	}
294
295
	/**
296
	 * Add a checkbox field to the Carousel section in Settings > Media
297
	 * for setting tiled galleries as the default.
298
	 */
299
	function settings_api_init() {
300
		global $wp_settings_sections;
301
302
		// Add the setting field [tiled_galleries] and place it in Settings > Media
303
		if ( isset( $wp_settings_sections['media']['carousel_section'] ) ) {
304
			$section = 'carousel_section';
305
		} else {
306
			$section = 'default';
307
		}
308
309
		add_settings_field( 'tiled_galleries', __( 'Tiled Galleries', 'jetpack' ), array( $this, 'setting_html' ), 'media', $section );
310
		register_setting( 'media', 'tiled_galleries', 'esc_attr' );
311
	}
312
313
	function setting_html() {
314
		echo '<label><input name="tiled_galleries" type="checkbox" value="1" ' .
315
			checked( 1, '' != get_option( 'tiled_galleries' ), false ) . ' /> ' .
316
			__( 'Display all your gallery pictures in a cool mosaic.', 'jetpack' ) . '</br></label>';
317
	}
318
}
319
320
add_action( 'init', array( 'Jetpack_Tiled_Gallery', 'init' ) );
321