PluginProbe
Flex Posts – Responsive Posts Block / trunk
Flex Posts – Responsive Posts Block vtrunk
2.1.0 trunk 1.12.0 2.0.0
flex-posts / blocks / list / block.php

block.php in Flex Posts – Responsive Posts Block trunk, at blocks/list/block.php

119 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Flex Posts List Block
4 *
5 * @package Flex Posts
6 */
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit;
10 }
11
12 /**
13 * Register block
14 */
15 function flex_posts_register_block() {
16 $option = get_option( 'flex_posts' );
17
18 wp_register_script(
19 'flex-posts-helpers',
20 FLEX_POSTS_URL . 'blocks/helpers.js',
21 array(
22 'wp-blocks',
23 'wp-block-editor',
24 'wp-components',
25 'wp-data',
26 'wp-element',
27 'wp-i18n',
28 'wp-server-side-render',
29 ),
30 FLEX_POSTS_VERSION,
31 true
32 );
33
34 wp_register_script(
35 'flex-posts',
36 plugins_url( 'block.js', __FILE__ ),
37 array(
38 'wp-i18n',
39 'flex-posts-helpers',
40 ),
41 FLEX_POSTS_VERSION,
42 true
43 );
44
45 $attributes = flex_posts_get_attributes();
46
47 wp_localize_script(
48 'flex-posts',
49 'flex_posts',
50 array(
51 'layouts' => flex_posts_get_layouts(),
52 'layout_svgs' => flex_posts_get_layout_svgs(),
53 'order_by' => flex_posts_get_order_by(),
54 'image_sizes' => flex_posts_get_image_sizes(),
55 'title_el' => flex_posts_get_title_elements_options(),
56 'attributes' => $attributes,
57 'category' => flex_posts_get_block_category(),
58 )
59 );
60
61 $args = array(
62 'editor_script' => 'flex-posts',
63 'render_callback' => 'flex_posts_render_block',
64 'attributes' => $attributes,
65 'api_version' => 3,
66 );
67
68 if ( empty( $option['disable_css'] ) ) {
69 $args['style'] = 'flex-posts';
70 }
71
72 // Register admin CSS for block editor.
73 wp_register_style(
74 'flex-posts-block-editor',
75 FLEX_POSTS_URL . 'admin/css/block-editor.css',
76 array(),
77 FLEX_POSTS_VERSION
78 );
79
80 $args['editor_style'] = 'flex-posts-block-editor';
81
82 register_block_type( 'flex-posts/list', $args );
83 }
84 add_action( 'init', 'flex_posts_register_block' );
85
86 /**
87 * Render block
88 *
89 * @param array $attributes Attributes.
90 * @return string
91 */
92 function flex_posts_render_block( $attributes ) {
93 return flex_posts_render( $attributes );
94 }
95
96 /**
97 * Prevent custom attributes for ServerSideRender
98 *
99 * @param mixed $result Response to replace the requested version with.
100 * @param object $server Server instance.
101 * @param object $request Request used to generate the response.
102 * @return array
103 */
104 function flex_posts_prevent_custom_attributes( $result, $server, $request ) {
105 $route = $request->get_route();
106
107 if ( strpos( $route, '/block-renderer/flex-posts/list' ) !== false ) {
108 $attributes = $request->get_param( 'attributes' );
109
110 if ( is_array( $attributes ) ) {
111 $defined_attributes = flex_posts_get_attributes();
112 $request->set_param( 'attributes', array_intersect_key( $attributes, $defined_attributes ) );
113 }
114 }
115
116 return $result;
117 }
118 add_filter( 'rest_pre_dispatch', 'flex_posts_prevent_custom_attributes', 10, 3 );
119