summaryrefslogtreecommitdiff
blob: 768f874927636e942174de7a18ba07ce93b87fc3 (plain)
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
<?php

use Automattic\Jetpack\Assets;
use Automattic\Jetpack\Status;

// Include the class file containing methods for rounding constrained array elements.
// Here the constrained array element is the dimension of a row, group or an image in the tiled gallery.
require_once dirname( __FILE__ ) . '/math/class-constrained-array-rounding.php';

// Layouts
require_once dirname( __FILE__ ) . '/tiled-gallery/tiled-gallery-rectangular.php';
require_once dirname( __FILE__ ) . '/tiled-gallery/tiled-gallery-square.php';
require_once dirname( __FILE__ ) . '/tiled-gallery/tiled-gallery-circle.php';

class Jetpack_Tiled_Gallery {
	private static $talaveras = array( 'rectangular', 'square', 'circle', 'rectangle', 'columns' );

	public function __construct() {
		add_action( 'admin_init', array( $this, 'settings_api_init' ) );
		add_filter( 'jetpack_gallery_types', array( $this, 'jetpack_gallery_types' ), 9 );
		add_filter( 'jetpack_default_gallery_type', array( $this, 'jetpack_default_gallery_type' ) );
	}

	public function tiles_enabled() {
		// Check the setting status
		return '' != Jetpack_Options::get_option_and_ensure_autoload( 'tiled_galleries', '' );
	}

	public function set_atts( $atts ) {
		global $post;

		$this->atts = shortcode_atts(
			array(
				'order'     => 'ASC',
				'orderby'   => 'menu_order ID',
				'id'        => isset( $post->ID ) ? $post->ID : 0,
				'include'   => '',
				'exclude'   => '',
				'type'      => '',
				'grayscale' => false,
				'link'      => '',
				'columns'   => 3,
			),
			$atts,
			'gallery'
		);

		$this->atts['id'] = (int) $this->atts['id'];
		$this->float      = is_rtl() ? 'right' : 'left';

		// Default to rectangular is tiled galleries are checked
		if ( $this->tiles_enabled() && ( ! $this->atts['type'] || 'default' == $this->atts['type'] ) ) {
			/** This filter is already documented in functions.gallery.php */
			$this->atts['type'] = apply_filters( 'jetpack_default_gallery_type', 'rectangular' );
		}

		if ( ! $this->atts['orderby'] ) {
			$this->atts['orderby'] = sanitize_sql_orderby( $this->atts['orderby'] );
			if ( ! $this->atts['orderby'] ) {
				$this->atts['orderby'] = 'menu_order ID';
			}
		}

		if ( 'rand' == strtolower( $this->atts['order'] ) ) {
			$this->atts['orderby'] = 'rand';
		}

		// We shouldn't have more than 20 columns.
		if ( ! is_numeric( $this->atts['columns'] ) || 20 < $this->atts['columns'] ) {
			$this->atts['columns'] = 3;
		}
	}

	public function get_attachments() {
		$atts = $this->atts;

		if ( ! empty( $atts['include'] ) ) {
			$include      = preg_replace( '/[^0-9,]+/', '', $atts['include'] );
			$_attachments = get_posts(
				array(
					'include'          => $include,
					'post_status'      => 'inherit',
					'post_type'        => 'attachment',
					'post_mime_type'   => 'image',
					'order'            => $atts['order'],
					'orderby'          => $atts['orderby'],
					'suppress_filters' => false,
				)
			);

			$attachments = array();
			foreach ( $_attachments as $key => $val ) {
				$attachments[ $val->ID ] = $_attachments[ $key ];
			}
		} elseif ( 0 === $atts['id'] ) {
			/*
			 * Should NEVER Happen but infinite_scroll_load_other_plugins_scripts means it does
			 * 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.
			 * TODO Fix this properly.
			 */
			$attachments = array();
		} elseif ( ! empty( $atts['exclude'] ) ) {
			$exclude     = preg_replace( '/[^0-9,]+/', '', $atts['exclude'] );
			$attachments = get_children(
				array(
					'post_parent'      => $atts['id'],
					'exclude'          => $exclude,
					'post_status'      => 'inherit',
					'post_type'        => 'attachment',
					'post_mime_type'   => 'image',
					'order'            => $atts['order'],
					'orderby'          => $atts['orderby'],
					'suppress_filters' => false,
				)
			);
		} else {
			$attachments = get_children(
				array(
					'post_parent'      => $atts['id'],
					'post_status'      => 'inherit',
					'post_type'        => 'attachment',
					'post_mime_type'   => 'image',
					'order'            => $atts['order'],
					'orderby'          => $atts['orderby'],
					'suppress_filters' => false,
				)
			);
		}
		return $attachments;
	}

	public static function default_scripts_and_styles() {
		wp_enqueue_script(
			'tiled-gallery',
			Assets::get_file_url_for_environment(
				'_inc/build/tiled-gallery/tiled-gallery/tiled-gallery.min.js',
				'modules/tiled-gallery/tiled-gallery/tiled-gallery.js'
			),
			array()
		);
		wp_enqueue_style( 'tiled-gallery', plugins_url( 'tiled-gallery/tiled-gallery.css', __FILE__ ), array(), '2012-09-21' );
		wp_style_add_data( 'tiled-gallery', 'rtl', 'replace' );
	}

	public function gallery_shortcode( $val, $atts ) {
		if ( ! empty( $val ) ) { // something else is overriding post_gallery, like a custom VIP shortcode
			return $val;
		}

		global $post;

		$this->set_atts( $atts );

		$attachments = $this->get_attachments();
		if ( empty( $attachments ) ) {
			return '';
		}

		if ( is_feed() || defined( 'IS_HTML_EMAIL' ) ) {
			return '';
		}

		if (
			in_array(
				$this->atts['type'],
				/**
				 * Filters the permissible Tiled Gallery types.
				 *
				 * @module tiled-gallery
				 *
				 * @since 3.7.0
				 *
				 * @param array Array of allowed types. Default: 'rectangular', 'square', 'circle', 'rectangle', 'columns'.
				 */
				$talaveras = apply_filters( 'jetpack_tiled_gallery_types', self::$talaveras )
			)
		) {
			// Enqueue styles and scripts
			self::default_scripts_and_styles();

			// Generate gallery HTML
			$gallery_class = 'Jetpack_Tiled_Gallery_Layout_' . ucfirst( $this->atts['type'] );
			$gallery       = new $gallery_class( $attachments, $this->atts['link'], $this->atts['grayscale'], (int) $this->atts['columns'] );
			$gallery_html  = $gallery->HTML();

			if ( $gallery_html && class_exists( 'Jetpack' ) && class_exists( 'Jetpack_Photon' ) ) {
				// Tiled Galleries in Jetpack require that Photon be active.
				// If it's not active, run it just on the gallery output.
				if ( ! in_array( 'photon', Jetpack::get_active_modules(), true ) && ! ( new Status() )->is_offline_mode() ) {
					$gallery_html = Jetpack_Photon::filter_the_content( $gallery_html );
				}
			}

			return trim( preg_replace( '/\s+/', ' ', $gallery_html ) ); // remove any new lines from the output so that the reader parses it better
		}

		return '';
	}

	public static function gallery_already_redefined() {
		global $shortcode_tags;
		$redefined = false;
		if ( ! isset( $shortcode_tags['gallery'] ) || $shortcode_tags['gallery'] !== 'gallery_shortcode' ) {
			$redefined = true;
		}
		/**
		 * Filter the output of the check for another plugin or theme affecting WordPress galleries.
		 *
		 * This will let folks that replace core’s shortcode confirm feature parity with it, so Jetpack's Tiled Galleries can still work.
		 *
		 * @module tiled-gallery
		 *
		 * @since 3.1.0
		 *
		 * @param bool $redefined Does another plugin or theme already redefines the default WordPress gallery?
		 */
		return apply_filters( 'jetpack_tiled_gallery_shortcode_redefined', $redefined );
	}

	public static function init() {
		if ( self::gallery_already_redefined() ) {
			return;
		}

		$gallery = new Jetpack_Tiled_Gallery();
		add_filter( 'post_gallery', array( $gallery, 'gallery_shortcode' ), 1001, 2 );
	}

	public static function get_content_width() {
		$tiled_gallery_content_width = Jetpack::get_content_width();

		if ( ! $tiled_gallery_content_width ) {
			$tiled_gallery_content_width = 500;
		}

		/**
		 * Filter overwriting the default content width.
		 *
		 * @module tiled-gallery
		 *
		 * @since 2.1.0
		 *
		 * @param string $tiled_gallery_content_width Default Tiled Gallery content width.
		 */
		return apply_filters( 'tiled_gallery_content_width', $tiled_gallery_content_width );
	}

	/**
	 * Media UI integration
	 */
	function jetpack_gallery_types( $types ) {
		if ( get_option( 'tiled_galleries' ) && isset( $types['default'] ) ) {
			// Tiled is set as the default, meaning that type='default'
			// will still display the mosaic.
			$types['thumbnails'] = $types['default'];
			unset( $types['default'] );
		}

		$types['rectangular'] = __( 'Tiled Mosaic', 'jetpack' );
		$types['square']      = __( 'Square Tiles', 'jetpack' );
		$types['circle']      = __( 'Circles', 'jetpack' );
		$types['columns']     = __( 'Tiled Columns', 'jetpack' );

		return $types;
	}

	function jetpack_default_gallery_type() {
		return ( get_option( 'tiled_galleries' ) ? 'rectangular' : 'default' );
	}

	static function get_talaveras() {
		return self::$talaveras;
	}

	/**
	 * Add a checkbox field to the Carousel section in Settings > Media
	 * for setting tiled galleries as the default.
	 */
	function settings_api_init() {
		global $wp_settings_sections;

		// Add the setting field [tiled_galleries] and place it in Settings > Media
		if ( isset( $wp_settings_sections['media']['carousel_section'] ) ) {
			$section = 'carousel_section';
		} else {
			$section = 'default';
		}

		add_settings_field( 'tiled_galleries', __( 'Tiled Galleries', 'jetpack' ), array( $this, 'setting_html' ), 'media', $section );
		register_setting( 'media', 'tiled_galleries', 'esc_attr' );
	}

	function setting_html() {
		echo '<label><input name="tiled_galleries" type="checkbox" value="1" ' .
			checked( 1, '' != get_option( 'tiled_galleries' ), false ) . ' /> ' .
			__( 'Display all your gallery pictures in a cool mosaic.', 'jetpack' ) . '</br></label>';
	}
}

add_action( 'init', array( 'Jetpack_Tiled_Gallery', 'init' ) );