PluginProbe
WP Ultimate Post Grid / 4.1.1
WP Ultimate Post Grid v4.1.1
4.1.1 trunk 1.5 1.6 1.7 1.7.1 1.7.2 1.8 1.9 2.0 2.1 2.2 2.3 2.4.0 2.5.0 2.6.0 2.7.0 2.8.0 2.8.2 3.0.0 3.3.0 3.4.0 3.5.0 3.6.0 3.7.0 All 32 releases
wp-ultimate-post-grid / includes / public / class-wpupg-blocks.php

class-wpupg-blocks.php in WP Ultimate Post Grid 4.1.1, at includes/public/class-wpupg-blocks.php

186 lines 4.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Handle Gutenberg Blocks.
4 *
5 * @link https://bootstrapped.ventures
6 * @since 3.0.0
7 *
8 * @package WP_Ultimate_Post_Grid
9 * @subpackage WP_Ultimate_Post_Grid/includes/public
10 */
11
12 /**
13 * Handle Gutenberg Blocks.
14 *
15 * @since 3.0.0
16 * @package WP_Ultimate_Post_Grid
17 * @subpackage WP_Ultimate_Post_Grid/includes/public
18 * @author Brecht Vandersmissen <brecht@bootstrapped.ventures>
19 */
20 class WPUPG_Blocks {
21
22 /**
23 * Register actions and filters.
24 *
25 * @since 3.0.0
26 */
27 public static function init() {
28 add_action( 'init', array( __CLASS__, 'register_blocks' ) );
29
30 // Deprecation notice after 5.8.0.
31 global $wp_version;
32 if ( $wp_version && version_compare( $wp_version, '5.8', '<' ) ) {
33 add_filter( 'block_categories', array( __CLASS__, 'block_categories' ) );
34 } else {
35 add_filter( 'block_categories_all', array( __CLASS__, 'block_categories' ) );
36 }
37 }
38
39 /**
40 * Register block categories.
41 *
42 * @since 3.0.0
43 * @param array $categories Existing block categories.
44 */
45 public static function block_categories( $categories ) {
46 return array_merge(
47 $categories,
48 array(
49 array(
50 'slug' => 'wp-ultimate-post-grid',
51 'title' => 'WP Ultimate Post Grid',
52 ),
53 )
54 );
55 }
56
57 /**
58 * Register all blocks.
59 *
60 * @since 3.0.0
61 */
62 public static function register_blocks() {
63 if ( function_exists( 'register_block_type' ) ) {
64 $block_settings = array(
65 'api_version' => 3,
66 'attributes' => array(
67 'id' => array(
68 'type' => 'string',
69 'default' => '',
70 ),
71 'align' => array(
72 'type' => 'string',
73 'default' => '',
74 ),
75 'dynamic' => array(
76 'type' => 'string',
77 'default' => '',
78 ),
79 'updated' => array(
80 'type' => 'number',
81 'default' => 0,
82 ),
83 ),
84 'render_callback' => array( __CLASS__, 'render_grid_with_filters_block' ),
85 );
86 register_block_type( 'wp-ultimate-post-grid/grid-with-filters', $block_settings );
87
88 $block_settings['render_callback'] = array( __CLASS__, 'render_grid_block' );
89 register_block_type( 'wp-ultimate-post-grid/grid', $block_settings );
90
91 $block_settings['attributes']['filter'] = array(
92 'type' => 'string',
93 'default' => '',
94 );
95 $block_settings['render_callback'] = array( __CLASS__, 'render_filter_block' );
96 register_block_type( 'wp-ultimate-post-grid/filter', $block_settings );
97 }
98 }
99
100 /**
101 * Parse block attributes.
102 *
103 * @since 3.0.0
104 * @param mixed $atts Block attributes.
105 */
106 public static function parse_atts( $atts ) {
107 if ( isset( $atts['dynamic'] ) ) {
108 $dynamic = trim( $atts['dynamic'] );
109
110 if ( $dynamic ) {
111 $dynamic_atts = shortcode_parse_atts( $dynamic );
112
113 if ( $dynamic_atts ) {
114 $atts = array_merge( $atts, $dynamic_atts );
115 }
116 }
117 }
118
119 unset( $atts['dynamic'] );
120 return $atts;
121 }
122
123 /**
124 * Render the grid with filters block.
125 *
126 * @since 3.0.0
127 * @param mixed $atts Block attributes.
128 */
129 public static function render_grid_with_filters_block( $atts ) {
130 $atts = self::parse_atts( $atts );
131 $output = '';
132
133 // Only do this for the Gutenberg Preview.
134 if ( isset( $GLOBALS['wp']->query_vars['rest_route'] ) && '/wp/v2/block-renderer/wp-ultimate-post-grid/grid-with-filters' === $GLOBALS['wp']->query_vars['rest_route'] ) {
135 $grid = WPUPG_Grid_Manager::get_grid( $atts['id'] );
136
137 if ( $grid ) {
138 $template_css = WPUPG_Template_Manager::get_template_css( $grid->template() );
139 $output .= '<style>' . $template_css . '</style>';
140 }
141 }
142
143 $output .= WPUPG_Shortcode::grid_with_filters_shortcode( $atts );
144
145 return $output;
146 }
147
148 /**
149 * Render the grid block.
150 *
151 * @since 3.0.0
152 * @param mixed $atts Block attributes.
153 */
154 public static function render_grid_block( $atts ) {
155 $atts = self::parse_atts( $atts );
156 $output = '';
157
158 // Only do this for the Gutenberg Preview.
159 if ( isset( $GLOBALS['wp']->query_vars['rest_route'] ) && '/wp/v2/block-renderer/wp-ultimate-post-grid/grid' === $GLOBALS['wp']->query_vars['rest_route'] ) {
160 $grid = WPUPG_Grid_Manager::get_grid( $atts['id'] );
161
162 if ( $grid ) {
163 $template_css = WPUPG_Template_Manager::get_template_css( $grid->template() );
164 $output .= '<style>' . $template_css . '</style>';
165 }
166 }
167
168 $output .= WPUPG_Shortcode::grid_shortcode( $atts );
169
170 return $output;
171 }
172
173 /**
174 * Render the filter block.
175 *
176 * @since 3.0.0
177 * @param mixed $atts Block attributes.
178 */
179 public static function render_filter_block( $atts ) {
180 $atts = self::parse_atts( $atts );
181 return WPUPG_Shortcode::filter_shortcode( $atts );
182 }
183 }
184
185 WPUPG_Blocks::init();
186