Completed
Push — fix/11256-post-format-handling ( 3eab3b...912cc3 )
by Kirk
71:57 queued 65:23
created

blocks.php ➔ jetpack_tiled_gallery_load_block_assets()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
1
<?php
2
/**
3
 * Load code specific to Gutenberg blocks which are not tied to a module.
4
 * This file is unusual, and is not an actual `module` as such.
5
 * It is included in ./module-extras.php
6
 */
7
8
/**
9
 * Map block.
10
 *
11
 * @since 6.8.0
12
 */
13
jetpack_register_block(
14
	'map',
15
	array(
16
		'render_callback' => 'jetpack_map_block_load_assets',
17
	)
18
);
19
20
/**
21
 * Map block registration/dependency declaration.
22
 *
23
 * @param array $attr - Array containing the map block attributes.
24
 * @param string $content - String containing the map block content.
25
 *
26
 * @return string
27
 */
28
function jetpack_map_block_load_assets( $attr, $content ) {
29
	$dependencies = array(
30
		'lodash',
31
		'wp-element',
32
		'wp-i18n',
33
	);
34
35
	$api_key = Jetpack_Options::get_option( 'mapbox_api_key' );
36
37
	Jetpack_Gutenberg::load_assets_as_required( 'map', $dependencies );
38
39
	return preg_replace( '/<div /', '<div data-api-key="' . esc_attr( $api_key ) . '" ', $content, 1 );
40
}
41
42
43
/**
44
 * Tiled Gallery block. Depends on the Photon module.
45
 *
46
 * @since 6.9.0
47
 */
48
if (
49
	( defined( 'IS_WPCOM' ) && IS_WPCOM ) ||
50
	class_exists( 'Jetpack_Photon' ) && Jetpack::is_module_active( 'photon' )
51
) {
52
	jetpack_register_block(
53
		'tiled-gallery',
54
		array(
55
			'render_callback' => 'jetpack_tiled_gallery_load_block_assets',
56
		)
57
	);
58
59
	/**
60
	 * Tiled gallery block registration/dependency declaration.
61
	 *
62
	 * @param array $attr - Array containing the block attributes.
63
	 * @param string $content - String containing the block content.
64
	 *
65
	 * @return string
66
	 */
67
	function jetpack_tiled_gallery_load_block_assets( $attr, $content ) {
68
		$dependencies = array(
69
			'lodash',
70
			'wp-i18n',
71
			'wp-token-list',
72
		);
73
		Jetpack_Gutenberg::load_assets_as_required( 'tiled-gallery', $dependencies );
74
75
		/**
76
		 * Filter the output of the Tiled Galleries content.
77
		 *
78
		 * @module tiled-gallery
79
		 *
80
		 * @since 6.9.0
81
		 *
82
		 * @param string $content Tiled Gallery block content.
83
		 */
84
		return apply_filters( 'jetpack_tiled_galleries_block_content', $content );
85
	}
86
}
87
88
/**
89
 * GIF Block.
90
 *
91
 * @since 7.0.0
92
 */
93
jetpack_register_block(
94
	'gif',
95
	array(
96
		'render_callback' => 'jetpack_gif_block_render',
97
	)
98
);
99
100
/**
101
 * Gif block registration/dependency declaration.
102
 *
103
 * @param array $attr - Array containing the map block attributes.
104
 *
105
 * @return string
106
 */
107
function jetpack_gif_block_render( $attr ) {
108
	$align       = isset( $attr['align'] ) ? $attr['align'] : 'center';
109
	$padding_top = isset( $attr['paddingTop'] ) ? $attr['paddingTop'] : 0;
110
	$style       = 'padding-top:' . $padding_top;
111
	$giphy_url   = isset( $attr['giphyUrl'] ) ? $attr['giphyUrl'] : null;
112
	$search_text = isset( $attr['searchText'] ) ? $attr['searchText'] : '';
113
	$caption     = isset( $attr['caption'] ) ? $attr['caption'] : null;
114
115
	if ( ! $giphy_url ) {
116
		return null;
117
	}
118
119
	$classes = array(
120
		'wp-block-jetpack-gif',
121
		'align' . $align,
122
	);
123
	if ( isset( $attr['className'] ) ) {
124
		array_push( $classes, $attr['className'] );
125
	}
126
127
	ob_start();
128
	?>
129
    <div class="<?php echo esc_attr( implode( $classes, ' ' ) ); ?>">
130
        <figure>
131
            <div class="wp-block-jetpack-gif-wrapper" style="<?php echo esc_attr( $style ); ?>">
132
                <iframe src="<?php echo esc_url( $giphy_url ); ?>"
133
                        title="<?php echo esc_attr( $search_text ); ?>"></iframe>
134
            </div>
135
			<?php if ( $caption ) : ?>
136
                <figcaption
137
                        class="wp-block-jetpack-gif-caption gallery-caption"><?php echo wp_kses_post( $caption ); ?></figcaption>
138
			<?php endif; ?>
139
        </figure>
140
    </div>
141
	<?php
142
	$html = ob_get_clean();
143
144
	Jetpack_Gutenberg::load_assets_as_required( 'gif' );
145
146
	return $html;
147
}
148
149
/**
150
 * Contact Info block and its child blocks.
151
 */
152
jetpack_register_block( 'contact-info' );
153
jetpack_register_block(
154
	'email',
155
	array( 'parent' => array( 'jetpack/contact-info' ) )
156
);
157
jetpack_register_block(
158
	'address',
159
	array( 'parent' => array( 'jetpack/contact-info' ) )
160
);
161
jetpack_register_block(
162
	'phone',
163
	array( 'parent' => array( 'jetpack/contact-info' ) )
164
);
165
166
/**
167
 * VR Block.
168
 */
169
jetpack_register_block( 'vr' );
170
171
/**
172
 * Slideshow Block.
173
 */
174
jetpack_register_block(
175
	'slideshow',
176
	array(
177
		'render_callback' => 'jetpack_slideshow_block_load_assets',
178
	)
179
);
180
181
/**
182
 * Slideshow block registration/dependency declaration.
183
 *
184
 * @param array $attr - Array containing the map block attributes.
185
 * @param string $content - String containing the map block content.
186
 *
187
 * @return string
188
 */
189
function jetpack_slideshow_block_load_assets( $attr, $content ) {
190
	$dependencies = array(
191
		'lodash',
192
		'wp-element',
193
		'wp-i18n',
194
	);
195
	Jetpack_Gutenberg::load_assets_as_required( 'slideshow', $dependencies );
196
197
	return $content;
198
}
199
200
/**
201
 * Business Hours Block.
202
 */
203
jetpack_register_block(
204
	'business-hours',
205
	array( 'render_callback' => 'jetpack_business_hours_render' )
206
);
207
/**
208
 * Business Hours Block dynamic rending of the glock.
209
 *
210
 * @param array $attr - Array containing the business hours block attributes.
0 ignored issues
show
Bug introduced by
There is no parameter named $attr. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
211
 * @param string $content - String containing the business hours block content.
212
 *
213
 * @return string
214
 */
215
function jetpack_business_hours_render( $attributes, $content ) {
216
	global $wp_locale;
217
218
	if ( empty( $attributes['hours'] ) || ! is_array( $attributes['hours'] ) ) {
219
		return $content;
220
	}
221
222
	$start_of_week     = (int) get_option( 'start_of_week', 0 );
223
	$time_format       = get_option( 'time_format' );
224
	$today             = current_time( 'D' );
225
	$custom_class_name = isset( $attributes['className'] ) ? $attributes['className'] : '';
0 ignored issues
show
Unused Code introduced by
$custom_class_name is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
226
	$content           = sprintf(
227
		'<dl class="jetpack-business-hours %s">',
228
		! empty( $attributes['className'] ) ? esc_attr( $attributes['className'] ) : ''
229
	);
230
231
	$days = array( 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat' );
232
233
	if ( $start_of_week ) {
234
		$chunk1              = array_slice( $attributes['hours'], 0, $start_of_week );
235
		$chunk2              = array_slice( $attributes['hours'], $start_of_week );
236
		$attributes['hours'] = array_merge( $chunk2, $chunk1 );
237
	}
238
239
	foreach ( $attributes['hours'] as $day => $hours ) {
240
		$opening = strtotime( $hours['opening'] );
241
		$closing = strtotime( $hours['closing'] );
242
243
		$content .= '<dt class="' . esc_attr( $day ) . '">' .
244
		            ucfirst( $wp_locale->get_weekday( array_search( $day, $days ) ) ) .
245
		            '</dt>';
246
		$content .= '<dd class="' . esc_attr( $day ) . '">';
247
		if ( $hours['opening'] && $hours['closing'] ) {
248
			$content .= sprintf(
249
				_x( 'From %1$s to %2$s', 'from business opening hour to closing hour', 'jetpack' ),
250
				date( $time_format, $opening ),
251
				date( $time_format, $closing )
252
			);
253
254
			if ( $today === $day ) {
255
				$now = strtotime( current_time( 'H:i' ) );
256
				if ( $now < $opening ) {
257
					$content .= '<br />';
258
					$content .= esc_html( sprintf(
259
						_x( 'Opening in %s', 'Amount of time until business opens', 'jetpack' ),
260
						human_time_diff( $now, $opening )
261
					) );
262
				} elseif ( $now >= $opening && $now < $closing ) {
263
					$content .= '<br />';
264
					$content .= esc_html( sprintf(
265
						_x( 'Closing in %s', 'Amount of time until business closes', 'jetpack' ),
266
						human_time_diff( $now, $closing )
267
					) );
268
				}
269
			}
270
		} else {
271
			$content .= esc_html__( 'CLOSED', 'jetpack' );
272
		}
273
		$content .= '</dd>';
274
	}
275
276
	$content .= '</dl>';
277
278
	return $content;
279
}
280