Completed
Push — renovate/webpack-cli-3.x ( 961c1b...02322b )
by
unknown
06:55
created

Jetpack_Tiled_Gallery_Block::render()   F

Complexity

Conditions 19
Paths 3

Size

Total Lines 105

Duplication

Lines 32
Ratio 30.48 %

Importance

Changes 0
Metric Value
cc 19
nc 3
nop 2
dl 32
loc 105
rs 3.6133
c 0
b 0
f 0

How to fix   Long Method    Complexity   

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 //phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2
3
/**
4
 * Tiled Gallery block. Depends on the Photon module.
5
 *
6
 * @since 6.9.0
7
 *
8
 * @package Jetpack
9
 */
10
11
/**
12
 * Jetpack Tiled Gallery Block class
13
 *
14
 * @since 7.3
15
 */
16
class Jetpack_Tiled_Gallery_Block {
17
	/* Values for building srcsets */
18
	const IMG_SRCSET_WIDTH_MAX  = 2000;
19
	const IMG_SRCSET_WIDTH_MIN  = 600;
20
	const IMG_SRCSET_WIDTH_STEP = 300;
21
22
	/**
23
	 * Register the block
24
	 */
25
	public static function register() {
26
		jetpack_register_block(
27
			'jetpack/tiled-gallery',
28
			array(
29
				'render_callback' => array( __CLASS__, 'render' ),
30
			)
31
		);
32
	}
33
34
	/**
35
	 * Tiled gallery block registration
36
	 *
37
	 * @param array  $attr    Array containing the block attributes.
38
	 * @param string $content String containing the block content.
39
	 *
40
	 * @return string
41
	 */
42
	public static function render( $attr, $content ) {
43
		Jetpack_Gutenberg::load_assets_as_required( 'tiled-gallery' );
44
45
		$is_squareish_layout = self::is_squareish_layout( $attr );
46
47
		if ( preg_match_all( '/<img [^>]+>/', $content, $images ) ) {
48
			/**
49
			 * This block processes all of the images that are found and builds $find and $replace.
50
			 *
51
			 * The original img is added to the $find array and the replacement is made and added
52
			 * to the $replace array. This is so that the same find and replace operations can be
53
			 * made on the entire $content.
54
			 */
55
			$find    = array();
56
			$replace = array();
57
58
			foreach ( $images[0] as $image_html ) {
59
				if (
60
					preg_match( '/data-width="([0-9]+)"/', $image_html, $img_height )
61
					&& preg_match( '/data-height="([0-9]+)"/', $image_html, $img_width )
62
					&& preg_match( '/src="([^"]+)"/', $image_html, $img_src )
63
				) {
64
					// Drop img src query string so it can be used as a base to add photon params
65
					// for the srcset.
66
					$src_parts   = explode( '?', $img_src[1], 2 );
67
					$orig_src    = $src_parts[0];
68
					$orig_height = absint( $img_height[1] );
69
					$orig_width  = absint( $img_width[1] );
70
71
					// Because URLs are already "photon", the photon function used short-circuits
72
					// before ssl is added. Detect ssl and add is if necessary.
73
					$is_ssl = ! empty( $src_parts[1] ) && false !== strpos( $src_parts[1], 'ssl=1' );
74
75
					if ( ! $orig_width || ! $orig_height || ! $orig_src ) {
76
						continue;
77
					}
78
79
					$srcset_parts = array();
80
					if ( $is_squareish_layout ) {
81
						$min_width = min( self::IMG_SRCSET_WIDTH_MIN, $orig_width, $orig_height );
82
						$max_width = min( self::IMG_SRCSET_WIDTH_MAX, $orig_width, $orig_height );
83
84 View Code Duplication
						for ( $w = $min_width; $w <= $max_width; $w = min( $max_width, $w + self::IMG_SRCSET_WIDTH_STEP ) ) {
85
							$srcset_src = add_query_arg(
86
								array(
87
									'resize' => $w . ',' . $w,
88
									'strip'  => 'all',
89
								),
90
								$orig_src
91
							);
92
							if ( $is_ssl ) {
93
								$srcset_src = add_query_arg( 'ssl', '1', $srcset_src );
94
							}
95
							$srcset_parts[] = esc_url( $srcset_src ) . ' ' . $w . 'w';
96
							if ( $w >= $max_width ) {
97
								break;
98
							}
99
						}
100
					} else {
101
						$min_width = min( self::IMG_SRCSET_WIDTH_MIN, $orig_width );
102
						$max_width = min( self::IMG_SRCSET_WIDTH_MAX, $orig_width );
103
104 View Code Duplication
						for ( $w = $min_width; $w <= $max_width; $w = min( $max_width, $w + self::IMG_SRCSET_WIDTH_STEP ) ) {
105
							$srcset_src = add_query_arg(
106
								array(
107
									'strip' => 'all',
108
									'w'     => $w,
109
								),
110
								$orig_src
111
							);
112
							if ( $is_ssl ) {
113
								$srcset_src = add_query_arg( 'ssl', '1', $srcset_src );
114
							}
115
							$srcset_parts[] = esc_url( $srcset_src ) . ' ' . $w . 'w';
116
							if ( $w >= $max_width ) {
117
								break;
118
							}
119
						}
120
					}
121
122
					if ( ! empty( $srcset_parts ) ) {
123
						$srcset = 'srcset="' . esc_attr( implode( ',', $srcset_parts ) ) . '"';
124
125
						$find[]    = $image_html;
126
						$replace[] = str_replace( '<img', '<img ' . $srcset, $image_html );
127
					}
128
				}
129
			}
130
131
			if ( ! empty( $find ) ) {
132
				$content = str_replace( $find, $replace, $content );
133
			}
134
		}
135
136
		/**
137
		 * Filter the output of the Tiled Galleries content.
138
		 *
139
		 * @module tiled-gallery
140
		 *
141
		 * @since 6.9.0
142
		 *
143
		 * @param string $content Tiled Gallery block content.
144
		 */
145
		return apply_filters( 'jetpack_tiled_galleries_block_content', $content );
146
	}
147
148
	/**
149
	 * Determines whether a Tiled Gallery block uses square or circle images (1:1 ratio)
150
	 *
151
	 * Layouts are block styles and will be available as `is-style-[LAYOUT]` in the className
152
	 * attribute. The default (rectangular) will be omitted.
153
	 *
154
	 * @param  {Array} $attr Attributes key/value array.
0 ignored issues
show
Documentation introduced by
The doc-type {Array} could not be parsed: Unknown type name "{Array}" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
155
	 * @return {boolean} True if layout is squareish, otherwise false.
0 ignored issues
show
Documentation introduced by
The doc-type {boolean} could not be parsed: Unknown type name "{boolean}" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
156
	 */
157
	private static function is_squareish_layout( $attr ) {
158
		return isset( $attr['className'] )
159
			&& (
160
				'is-style-square' === $attr['className']
161
				|| 'is-style-circle' === $attr['className']
162
			);
163
	}
164
}
165
166
if (
167
	( defined( 'IS_WPCOM' ) && IS_WPCOM )
168
	|| class_exists( 'Jetpack_Photon' ) && Jetpack::is_module_active( 'photon' )
169
) {
170
	Jetpack_Tiled_Gallery_Block::register();
171
}
172