Completed
Push — add/crowdsignal-shortcode ( 65c42e...1b4a63 )
by Kuba
14:46 queued 06:22
created

Jetpack_Tiled_Gallery::get_attachments()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 55

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 4
nop 0
dl 0
loc 55
rs 8.6707
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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