108 $script_asset_path = realpath( |
82 $script_asset_path = realpath( |
109 dirname( $metadata['file'] ) . '/' . |
83 dirname( $metadata['file'] ) . '/' . |
110 substr_replace( $script_path, '.asset.php', - strlen( '.js' ) ) |
84 substr_replace( $script_path, '.asset.php', - strlen( '.js' ) ) |
111 ); |
85 ); |
112 if ( ! file_exists( $script_asset_path ) ) { |
86 if ( ! file_exists( $script_asset_path ) ) { |
113 $message = sprintf( |
87 _doing_it_wrong( |
114 /* translators: %1: field name. %2: block name */ |
88 __FUNCTION__, |
115 __( 'The asset file for the "%1$s" defined in "%2$s" block definition is missing.', 'default' ), |
89 sprintf( |
116 $field_name, |
90 /* translators: 1: Field name, 2: Block name. */ |
117 $metadata['name'] |
91 __( 'The asset file for the "%1$s" defined in "%2$s" block definition is missing.' ), |
|
92 $field_name, |
|
93 $metadata['name'] |
|
94 ), |
|
95 '5.5.0' |
118 ); |
96 ); |
119 _doing_it_wrong( __FUNCTION__, $message, '5.5.0' ); |
|
120 return false; |
97 return false; |
121 } |
98 } |
122 $script_asset = require $script_asset_path; |
99 $script_asset = require $script_asset_path; |
123 $result = wp_register_script( |
100 $result = wp_register_script( |
124 $script_handle, |
101 $script_handle, |
125 plugins_url( $script_path, $metadata['file'] ), |
102 plugins_url( $script_path, $metadata['file'] ), |
126 $script_asset['dependencies'], |
103 $script_asset['dependencies'], |
127 $script_asset['version'] |
104 $script_asset['version'] |
128 ); |
105 ); |
129 return $result ? $script_handle : false; |
106 if ( ! $result ) { |
|
107 return false; |
|
108 } |
|
109 |
|
110 if ( ! empty( $metadata['textdomain'] ) ) { |
|
111 wp_set_script_translations( $script_handle, $metadata['textdomain'] ); |
|
112 } |
|
113 |
|
114 return $script_handle; |
130 } |
115 } |
131 |
116 |
132 /** |
117 /** |
133 * Finds a style handle for the block metadata field. It detects when a path |
118 * Finds a style handle for the block metadata field. It detects when a path |
134 * to file was provided and registers the style under automatically |
119 * to file was provided and registers the style under automatically |
135 * generated handle name. It returns unprocessed style handle otherwise. |
120 * generated handle name. It returns unprocessed style handle otherwise. |
136 * |
121 * |
137 * @since 5.5.0 |
122 * @since 5.5.0 |
138 * |
123 * |
139 * @param array $metadata Block metadata. |
124 * @param array $metadata Block metadata. |
140 * @param string $field_name Field name to pick from metadata. |
125 * @param string $field_name Field name to pick from metadata. |
141 * @return string|boolean Style handle provided directly or created through |
126 * @return string|false Style handle provided directly or created through |
142 * style's registration, or false on failure. |
127 * style's registration, or false on failure. |
143 */ |
128 */ |
144 function register_block_style_handle( $metadata, $field_name ) { |
129 function register_block_style_handle( $metadata, $field_name ) { |
145 if ( empty( $metadata[ $field_name ] ) ) { |
130 if ( empty( $metadata[ $field_name ] ) ) { |
146 return false; |
131 return false; |
147 } |
132 } |
|
133 $is_core_block = isset( $metadata['file'] ) && 0 === strpos( $metadata['file'], ABSPATH . WPINC ); |
|
134 if ( $is_core_block && ! wp_should_load_separate_core_block_assets() ) { |
|
135 return false; |
|
136 } |
|
137 |
|
138 // Check whether styles should have a ".min" suffix or not. |
|
139 $suffix = SCRIPT_DEBUG ? '' : '.min'; |
|
140 |
148 $style_handle = $metadata[ $field_name ]; |
141 $style_handle = $metadata[ $field_name ]; |
149 $style_path = remove_block_asset_path_prefix( $metadata[ $field_name ] ); |
142 $style_path = remove_block_asset_path_prefix( $metadata[ $field_name ] ); |
150 if ( $style_handle === $style_path ) { |
143 |
|
144 if ( $style_handle === $style_path && ! $is_core_block ) { |
151 return $style_handle; |
145 return $style_handle; |
152 } |
146 } |
153 |
147 |
154 $style_handle = generate_block_asset_handle( $metadata['name'], $field_name ); |
148 $style_uri = plugins_url( $style_path, $metadata['file'] ); |
155 $block_dir = dirname( $metadata['file'] ); |
149 if ( $is_core_block ) { |
156 $result = wp_register_style( |
150 $style_path = "style$suffix.css"; |
|
151 $style_uri = includes_url( 'blocks/' . str_replace( 'core/', '', $metadata['name'] ) . "/style$suffix.css" ); |
|
152 } |
|
153 |
|
154 $style_handle = generate_block_asset_handle( $metadata['name'], $field_name ); |
|
155 $block_dir = dirname( $metadata['file'] ); |
|
156 $style_file = realpath( "$block_dir/$style_path" ); |
|
157 $has_style_file = false !== $style_file; |
|
158 $version = ! $is_core_block && isset( $metadata['version'] ) ? $metadata['version'] : false; |
|
159 $style_uri = $has_style_file ? $style_uri : false; |
|
160 $result = wp_register_style( |
157 $style_handle, |
161 $style_handle, |
158 plugins_url( $style_path, $metadata['file'] ), |
162 $style_uri, |
159 array(), |
163 array(), |
160 filemtime( realpath( "$block_dir/$style_path" ) ) |
164 $version |
161 ); |
165 ); |
|
166 if ( file_exists( str_replace( '.css', '-rtl.css', $style_file ) ) ) { |
|
167 wp_style_add_data( $style_handle, 'rtl', 'replace' ); |
|
168 } |
|
169 if ( $has_style_file ) { |
|
170 wp_style_add_data( $style_handle, 'path', $style_file ); |
|
171 } |
|
172 |
|
173 $rtl_file = str_replace( "$suffix.css", "-rtl$suffix.css", $style_file ); |
|
174 if ( is_rtl() && file_exists( $rtl_file ) ) { |
|
175 wp_style_add_data( $style_handle, 'path', $rtl_file ); |
|
176 } |
|
177 |
162 return $result ? $style_handle : false; |
178 return $result ? $style_handle : false; |
163 } |
179 } |
164 |
180 |
165 /** |
181 /** |
166 * Registers a block type from metadata stored in the `block.json` file. |
182 * Registers a block type from the metadata stored in the `block.json` file. |
167 * |
183 * |
168 * @since 5.5.0 |
184 * @since 5.5.0 |
169 * |
185 * |
170 * @param string $file_or_folder Path to the JSON file with metadata definition for |
186 * @param string $file_or_folder Path to the JSON file with metadata definition for |
171 * the block or path to the folder where the `block.json` file is located. |
187 * the block or path to the folder where the `block.json` file is located. |
172 * @param array $args { |
188 * @param array $args Optional. Array of block type arguments. Accepts any public property |
173 * Optional. Array of block type arguments. Accepts any public property of `WP_Block_Type`. |
189 * of `WP_Block_Type`. See WP_Block_Type::__construct() for information |
174 * Any arguments may be defined, however the ones described below are supported by default. |
190 * on accepted arguments. Default empty array. |
175 * Default empty array. |
|
176 * |
|
177 * @type callable $render_callback Callback used to render blocks of this block type. |
|
178 * } |
|
179 * @return WP_Block_Type|false The registered block type on success, or false on failure. |
191 * @return WP_Block_Type|false The registered block type on success, or false on failure. |
180 */ |
192 */ |
181 function register_block_type_from_metadata( $file_or_folder, $args = array() ) { |
193 function register_block_type_from_metadata( $file_or_folder, $args = array() ) { |
182 $filename = 'block.json'; |
194 $filename = 'block.json'; |
183 $metadata_file = ( substr( $file_or_folder, -strlen( $filename ) ) !== $filename ) ? |
195 $metadata_file = ( substr( $file_or_folder, -strlen( $filename ) ) !== $filename ) ? |
205 'providesContext' => 'provides_context', |
238 'providesContext' => 'provides_context', |
206 'usesContext' => 'uses_context', |
239 'usesContext' => 'uses_context', |
207 'supports' => 'supports', |
240 'supports' => 'supports', |
208 'styles' => 'styles', |
241 'styles' => 'styles', |
209 'example' => 'example', |
242 'example' => 'example', |
|
243 'apiVersion' => 'api_version', |
210 ); |
244 ); |
211 |
245 |
212 foreach ( $property_mappings as $key => $mapped_key ) { |
246 foreach ( $property_mappings as $key => $mapped_key ) { |
213 if ( isset( $metadata[ $key ] ) ) { |
247 if ( isset( $metadata[ $key ] ) ) { |
214 $settings[ $mapped_key ] = $metadata[ $key ]; |
248 $value = $metadata[ $key ]; |
|
249 if ( empty( $metadata['textdomain'] ) ) { |
|
250 $settings[ $mapped_key ] = $value; |
|
251 continue; |
|
252 } |
|
253 $textdomain = $metadata['textdomain']; |
|
254 switch ( $key ) { |
|
255 case 'title': |
|
256 case 'description': |
|
257 // phpcs:ignore WordPress.WP.I18n.LowLevelTranslationFunction,WordPress.WP.I18n.NonSingularStringLiteralText,WordPress.WP.I18n.NonSingularStringLiteralContext,WordPress.WP.I18n.NonSingularStringLiteralDomain |
|
258 $settings[ $mapped_key ] = translate_with_gettext_context( $value, sprintf( 'block %s', $key ), $textdomain ); |
|
259 break; |
|
260 case 'keywords': |
|
261 $settings[ $mapped_key ] = array(); |
|
262 if ( ! is_array( $value ) ) { |
|
263 continue 2; |
|
264 } |
|
265 |
|
266 foreach ( $value as $keyword ) { |
|
267 // phpcs:ignore WordPress.WP.I18n.LowLevelTranslationFunction,WordPress.WP.I18n.NonSingularStringLiteralText,WordPress.WP.I18n.NonSingularStringLiteralDomain |
|
268 $settings[ $mapped_key ][] = translate_with_gettext_context( $keyword, 'block keyword', $textdomain ); |
|
269 } |
|
270 |
|
271 break; |
|
272 case 'styles': |
|
273 $settings[ $mapped_key ] = array(); |
|
274 if ( ! is_array( $value ) ) { |
|
275 continue 2; |
|
276 } |
|
277 |
|
278 foreach ( $value as $style ) { |
|
279 if ( ! empty( $style['label'] ) ) { |
|
280 // phpcs:ignore WordPress.WP.I18n.LowLevelTranslationFunction,WordPress.WP.I18n.NonSingularStringLiteralText,WordPress.WP.I18n.NonSingularStringLiteralDomain |
|
281 $style['label'] = translate_with_gettext_context( $style['label'], 'block style label', $textdomain ); |
|
282 } |
|
283 $settings[ $mapped_key ][] = $style; |
|
284 } |
|
285 |
|
286 break; |
|
287 default: |
|
288 $settings[ $mapped_key ] = $value; |
|
289 } |
215 } |
290 } |
216 } |
291 } |
217 |
292 |
218 if ( ! empty( $metadata['editorScript'] ) ) { |
293 if ( ! empty( $metadata['editorScript'] ) ) { |
219 $settings['editor_script'] = register_block_script_handle( |
294 $settings['editor_script'] = register_block_script_handle( |
241 $metadata, |
316 $metadata, |
242 'style' |
317 'style' |
243 ); |
318 ); |
244 } |
319 } |
245 |
320 |
246 return register_block_type( |
321 /** |
247 $metadata['name'], |
322 * Filters the settings determined from the block type metadata. |
|
323 * |
|
324 * @since 5.7.0 |
|
325 * |
|
326 * @param array $settings Array of determined settings for registering a block type. |
|
327 * @param array $metadata Metadata provided for registering a block type. |
|
328 */ |
|
329 $settings = apply_filters( |
|
330 'block_type_metadata_settings', |
248 array_merge( |
331 array_merge( |
249 $settings, |
332 $settings, |
250 $args |
333 $args |
251 ) |
334 ), |
252 ); |
335 $metadata |
|
336 ); |
|
337 |
|
338 return WP_Block_Type_Registry::get_instance()->register( |
|
339 $metadata['name'], |
|
340 $settings |
|
341 ); |
|
342 } |
|
343 |
|
344 /** |
|
345 * Registers a block type. The recommended way is to register a block type using |
|
346 * the metadata stored in the `block.json` file. |
|
347 * |
|
348 * @since 5.0.0 |
|
349 * @since 5.8.0 First param accepts a path to the `block.json` file. |
|
350 * |
|
351 * @param string|WP_Block_Type $block_type Block type name including namespace, or alternatively |
|
352 * a path to the JSON file with metadata definition for the block, |
|
353 * or a path to the folder where the `block.json` file is located, |
|
354 * or a complete WP_Block_Type instance. |
|
355 * In case a WP_Block_Type is provided, the $args parameter will be ignored. |
|
356 * @param array $args Optional. Array of block type arguments. Accepts any public property |
|
357 * of `WP_Block_Type`. See WP_Block_Type::__construct() for information |
|
358 * on accepted arguments. Default empty array. |
|
359 * |
|
360 * @return WP_Block_Type|false The registered block type on success, or false on failure. |
|
361 */ |
|
362 function register_block_type( $block_type, $args = array() ) { |
|
363 if ( is_string( $block_type ) && file_exists( $block_type ) ) { |
|
364 return register_block_type_from_metadata( $block_type, $args ); |
|
365 } |
|
366 |
|
367 return WP_Block_Type_Registry::get_instance()->register( $block_type, $args ); |
|
368 } |
|
369 |
|
370 /** |
|
371 * Unregisters a block type. |
|
372 * |
|
373 * @since 5.0.0 |
|
374 * |
|
375 * @param string|WP_Block_Type $name Block type name including namespace, or alternatively |
|
376 * a complete WP_Block_Type instance. |
|
377 * @return WP_Block_Type|false The unregistered block type on success, or false on failure. |
|
378 */ |
|
379 function unregister_block_type( $name ) { |
|
380 return WP_Block_Type_Registry::get_instance()->unregister( $name ); |
253 } |
381 } |
254 |
382 |
255 /** |
383 /** |
256 * Determine whether a post or content string has blocks. |
384 * Determine whether a post or content string has blocks. |
257 * |
385 * |
279 |
408 |
280 /** |
409 /** |
281 * Determine whether a $post or a string contains a specific block type. |
410 * Determine whether a $post or a string contains a specific block type. |
282 * |
411 * |
283 * This test optimizes for performance rather than strict accuracy, detecting |
412 * This test optimizes for performance rather than strict accuracy, detecting |
284 * the block type exists but not validating its structure. For strict accuracy, |
413 * whether the block type exists but not validating its structure and not checking |
285 * you should use the block parser on post content. |
414 * reusable blocks. For strict accuracy, you should use the block parser on post content. |
286 * |
415 * |
287 * @since 5.0.0 |
416 * @since 5.0.0 |
288 * |
417 * |
289 * @see parse_blocks() |
418 * @see parse_blocks() |
290 * |
419 * |
291 * @param string $block_name Full Block type to look for. |
420 * @param string $block_name Full block type to look for. |
292 * @param int|string|WP_Post|null $post Optional. Post content, post ID, or post object. Defaults to global $post. |
421 * @param int|string|WP_Post|null $post Optional. Post content, post ID, or post object. |
|
422 * Defaults to global $post. |
293 * @return bool Whether the post content contains the specified block. |
423 * @return bool Whether the post content contains the specified block. |
294 */ |
424 */ |
295 function has_block( $block_name, $post = null ) { |
425 function has_block( $block_name, $post = null ) { |
296 if ( ! has_blocks( $post ) ) { |
426 if ( ! has_blocks( $post ) ) { |
297 return false; |
427 return false; |
356 * |
486 * |
357 * The serialized result is a JSON-encoded string, with unicode escape sequence |
487 * The serialized result is a JSON-encoded string, with unicode escape sequence |
358 * substitution for characters which might otherwise interfere with embedding |
488 * substitution for characters which might otherwise interfere with embedding |
359 * the result in an HTML comment. |
489 * the result in an HTML comment. |
360 * |
490 * |
|
491 * This function must produce output that remains in sync with the output of |
|
492 * the serializeAttributes JavaScript function in the block editor in order |
|
493 * to ensure consistent operation between PHP and JavaScript. |
|
494 * |
361 * @since 5.3.1 |
495 * @since 5.3.1 |
362 * |
496 * |
363 * @param array $block_attributes Attributes object. |
497 * @param array $block_attributes Attributes object. |
364 * @return string Serialized attributes. |
498 * @return string Serialized attributes. |
365 */ |
499 */ |
366 function serialize_block_attributes( $block_attributes ) { |
500 function serialize_block_attributes( $block_attributes ) { |
367 $encoded_attributes = json_encode( $block_attributes ); |
501 $encoded_attributes = wp_json_encode( $block_attributes, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ); |
368 $encoded_attributes = preg_replace( '/--/', '\\u002d\\u002d', $encoded_attributes ); |
502 $encoded_attributes = preg_replace( '/--/', '\\u002d\\u002d', $encoded_attributes ); |
369 $encoded_attributes = preg_replace( '/</', '\\u003c', $encoded_attributes ); |
503 $encoded_attributes = preg_replace( '/</', '\\u003c', $encoded_attributes ); |
370 $encoded_attributes = preg_replace( '/>/', '\\u003e', $encoded_attributes ); |
504 $encoded_attributes = preg_replace( '/>/', '\\u003e', $encoded_attributes ); |
371 $encoded_attributes = preg_replace( '/&/', '\\u0026', $encoded_attributes ); |
505 $encoded_attributes = preg_replace( '/&/', '\\u0026', $encoded_attributes ); |
372 // Regex: /\\"/ |
506 // Regex: /\\"/ |
821 * Unregisters a block style. |
969 * Unregisters a block style. |
822 * |
970 * |
823 * @since 5.3.0 |
971 * @since 5.3.0 |
824 * |
972 * |
825 * @param string $block_name Block type name including namespace. |
973 * @param string $block_name Block type name including namespace. |
826 * @param array $block_style_name Block style name. |
974 * @param string $block_style_name Block style name. |
827 * @return boolean True if the block style was unregistered with success and false otherwise. |
975 * @return bool True if the block style was unregistered with success and false otherwise. |
828 */ |
976 */ |
829 function unregister_block_style( $block_name, $block_style_name ) { |
977 function unregister_block_style( $block_name, $block_style_name ) { |
830 return WP_Block_Styles_Registry::get_instance()->unregister( $block_name, $block_style_name ); |
978 return WP_Block_Styles_Registry::get_instance()->unregister( $block_name, $block_style_name ); |
831 } |
979 } |
|
980 |
|
981 /** |
|
982 * Checks whether the current block type supports the feature requested. |
|
983 * |
|
984 * @since 5.8.0 |
|
985 * |
|
986 * @param WP_Block_Type $block_type Block type to check for support. |
|
987 * @param string $feature Name of the feature to check support for. |
|
988 * @param mixed $default Fallback value for feature support, defaults to false. |
|
989 * |
|
990 * @return boolean Whether or not the feature is supported. |
|
991 */ |
|
992 function block_has_support( $block_type, $feature, $default = false ) { |
|
993 $block_support = $default; |
|
994 if ( $block_type && property_exists( $block_type, 'supports' ) ) { |
|
995 $block_support = _wp_array_get( $block_type->supports, $feature, $default ); |
|
996 } |
|
997 |
|
998 return true === $block_support || is_array( $block_support ); |
|
999 } |
|
1000 |
|
1001 /** |
|
1002 * Converts typography keys declared under `supports.*` to `supports.typography.*`. |
|
1003 * |
|
1004 * Displays a `_doing_it_wrong()` notice when a block using the older format is detected. |
|
1005 * |
|
1006 * @since 5.8.0 |
|
1007 * |
|
1008 * @param array $metadata Metadata for registering a block type. |
|
1009 * @return array Filtered metadata for registering a block type. |
|
1010 */ |
|
1011 function wp_migrate_old_typography_shape( $metadata ) { |
|
1012 if ( ! isset( $metadata['supports'] ) ) { |
|
1013 return $metadata; |
|
1014 } |
|
1015 |
|
1016 $typography_keys = array( |
|
1017 '__experimentalFontFamily', |
|
1018 '__experimentalFontStyle', |
|
1019 '__experimentalFontWeight', |
|
1020 '__experimentalLetterSpacing', |
|
1021 '__experimentalTextDecoration', |
|
1022 '__experimentalTextTransform', |
|
1023 'fontSize', |
|
1024 'lineHeight', |
|
1025 ); |
|
1026 |
|
1027 foreach ( $typography_keys as $typography_key ) { |
|
1028 $support_for_key = _wp_array_get( $metadata['supports'], array( $typography_key ), null ); |
|
1029 |
|
1030 if ( null !== $support_for_key ) { |
|
1031 _doing_it_wrong( |
|
1032 'register_block_type_from_metadata()', |
|
1033 sprintf( |
|
1034 /* translators: 1: Block type, 2: Typography supports key, e.g: fontSize, lineHeight, etc. 3: block.json, 4: Old metadata key, 5: New metadata key. */ |
|
1035 __( 'Block "%1$s" is declaring %2$s support in %3$s file under %4$s. %2$s support is now declared under %5$s.' ), |
|
1036 $metadata['name'], |
|
1037 "<code>$typography_key</code>", |
|
1038 '<code>block.json</code>', |
|
1039 "<code>supports.$typography_key</code>", |
|
1040 "<code>supports.typography.$typography_key</code>" |
|
1041 ), |
|
1042 '5.8.0' |
|
1043 ); |
|
1044 |
|
1045 _wp_array_set( $metadata['supports'], array( 'typography', $typography_key ), $support_for_key ); |
|
1046 unset( $metadata['supports'][ $typography_key ] ); |
|
1047 } |
|
1048 } |
|
1049 |
|
1050 return $metadata; |
|
1051 } |
|
1052 |
|
1053 /** |
|
1054 * Helper function that constructs a WP_Query args array from |
|
1055 * a `Query` block properties. |
|
1056 * |
|
1057 * It's used in Query Loop, Query Pagination Numbers and Query Pagination Next blocks. |
|
1058 * |
|
1059 * @since 5.8.0 |
|
1060 * |
|
1061 * @param WP_Block $block Block instance. |
|
1062 * @param int $page Current query's page. |
|
1063 * |
|
1064 * @return array Returns the constructed WP_Query arguments. |
|
1065 */ |
|
1066 function build_query_vars_from_query_block( $block, $page ) { |
|
1067 $query = array( |
|
1068 'post_type' => 'post', |
|
1069 'order' => 'DESC', |
|
1070 'orderby' => 'date', |
|
1071 'post__not_in' => array(), |
|
1072 ); |
|
1073 |
|
1074 if ( isset( $block->context['query'] ) ) { |
|
1075 if ( ! empty( $block->context['query']['postType'] ) ) { |
|
1076 $post_type_param = $block->context['query']['postType']; |
|
1077 if ( is_post_type_viewable( $post_type_param ) ) { |
|
1078 $query['post_type'] = $post_type_param; |
|
1079 } |
|
1080 } |
|
1081 if ( isset( $block->context['query']['sticky'] ) && ! empty( $block->context['query']['sticky'] ) ) { |
|
1082 $sticky = get_option( 'sticky_posts' ); |
|
1083 if ( 'only' === $block->context['query']['sticky'] ) { |
|
1084 $query['post__in'] = $sticky; |
|
1085 } else { |
|
1086 $query['post__not_in'] = array_merge( $query['post__not_in'], $sticky ); |
|
1087 } |
|
1088 } |
|
1089 if ( ! empty( $block->context['query']['exclude'] ) ) { |
|
1090 $excluded_post_ids = array_map( 'intval', $block->context['query']['exclude'] ); |
|
1091 $excluded_post_ids = array_filter( $excluded_post_ids ); |
|
1092 $query['post__not_in'] = array_merge( $query['post__not_in'], $excluded_post_ids ); |
|
1093 } |
|
1094 if ( |
|
1095 isset( $block->context['query']['perPage'] ) && |
|
1096 is_numeric( $block->context['query']['perPage'] ) |
|
1097 ) { |
|
1098 $per_page = absint( $block->context['query']['perPage'] ); |
|
1099 $offset = 0; |
|
1100 |
|
1101 if ( |
|
1102 isset( $block->context['query']['offset'] ) && |
|
1103 is_numeric( $block->context['query']['offset'] ) |
|
1104 ) { |
|
1105 $offset = absint( $block->context['query']['offset'] ); |
|
1106 } |
|
1107 |
|
1108 $query['offset'] = ( $per_page * ( $page - 1 ) ) + $offset; |
|
1109 $query['posts_per_page'] = $per_page; |
|
1110 } |
|
1111 if ( ! empty( $block->context['query']['categoryIds'] ) ) { |
|
1112 $term_ids = array_map( 'intval', $block->context['query']['categoryIds'] ); |
|
1113 $term_ids = array_filter( $term_ids ); |
|
1114 $query['category__in'] = $term_ids; |
|
1115 } |
|
1116 if ( ! empty( $block->context['query']['tagIds'] ) ) { |
|
1117 $term_ids = array_map( 'intval', $block->context['query']['tagIds'] ); |
|
1118 $term_ids = array_filter( $term_ids ); |
|
1119 $query['tag__in'] = $term_ids; |
|
1120 } |
|
1121 if ( |
|
1122 isset( $block->context['query']['order'] ) && |
|
1123 in_array( strtoupper( $block->context['query']['order'] ), array( 'ASC', 'DESC' ), true ) |
|
1124 ) { |
|
1125 $query['order'] = strtoupper( $block->context['query']['order'] ); |
|
1126 } |
|
1127 if ( isset( $block->context['query']['orderBy'] ) ) { |
|
1128 $query['orderby'] = $block->context['query']['orderBy']; |
|
1129 } |
|
1130 if ( |
|
1131 isset( $block->context['query']['author'] ) && |
|
1132 (int) $block->context['query']['author'] > 0 |
|
1133 ) { |
|
1134 $query['author'] = (int) $block->context['query']['author']; |
|
1135 } |
|
1136 if ( ! empty( $block->context['query']['search'] ) ) { |
|
1137 $query['s'] = $block->context['query']['search']; |
|
1138 } |
|
1139 } |
|
1140 return $query; |
|
1141 } |