PluginProbe
WP Ultimate Post Grid / 3.8.0
WP Ultimate Post Grid v3.8.0
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 3.8.0, at includes/public/class-wpupg-blocks.php

185 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 'attributes' => array(
66 'id' => array(
67 'type' => 'string',
68 'default' => '',
69 ),
70 'align' => array(
71 'type' => 'string',
72 'default' => '',
73 ),
74 'dynamic' => array(
75 'type' => 'string',
76 'default' => '',
77 ),
78 'updated' => array(
79 'type' => 'number',
80 'default' => 0,
81 ),
82 ),
83 'render_callback' => array( __CLASS__, 'render_grid_with_filters_block' ),
84 );
85 register_block_type( 'wp-ultimate-post-grid/grid-with-filters', $block_settings );
86
87 $block_settings['render_callback'] = array( __CLASS__, 'render_grid_block' );
88 register_block_type( 'wp-ultimate-post-grid/grid', $block_settings );
89
90 $block_settings['attributes']['filter'] = array(
91 'type' => 'string',
92 'default' => '',
93 );
94 $block_settings['render_callback'] = array( __CLASS__, 'render_filter_block' );
95 register_block_type( 'wp-ultimate-post-grid/filter', $block_settings );
96 }
97 }
98
99 /**
100 * Parse block attributes.
101 *
102 * @since 3.0.0
103 * @param mixed $atts Block attributes.
104 */
105 public static function parse_atts( $atts ) {
106 if ( isset( $atts['dynamic'] ) ) {
107 $dynamic = trim( $atts['dynamic'] );
108
109 if ( $dynamic ) {
110 $dynamic_atts = shortcode_parse_atts( $dynamic );
111
112 if ( $dynamic_atts ) {
113 $atts = array_merge( $atts, $dynamic_atts );
114 }
115 }
116 }
117
118 unset( $atts['dynamic'] );
119 return $atts;
120 }
121
122 /**
123 * Render the grid with filters block.
124 *
125 * @since 3.0.0
126 * @param mixed $atts Block attributes.
127 */
128 public static function render_grid_with_filters_block( $atts ) {
129 $atts = self::parse_atts( $atts );
130 $output = '';
131
132 // Only do this for the Gutenberg Preview.
133 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'] ) {
134 $grid = WPUPG_Grid_Manager::get_grid( $atts['id'] );
135
136 if ( $grid ) {
137 $template_css = WPUPG_Template_Manager::get_template_css( $grid->template() );
138 $output .= '<style type="text/css">' . $template_css . '</style>';
139 }
140 }
141
142 $output .= WPUPG_Shortcode::grid_with_filters_shortcode( $atts );
143
144 return $output;
145 }
146
147 /**
148 * Render the grid block.
149 *
150 * @since 3.0.0
151 * @param mixed $atts Block attributes.
152 */
153 public static function render_grid_block( $atts ) {
154 $atts = self::parse_atts( $atts );
155 $output = '';
156
157 // Only do this for the Gutenberg Preview.
158 if ( isset( $GLOBALS['wp']->query_vars['rest_route'] ) && '/wp/v2/block-renderer/wp-ultimate-post-grid/grid' === $GLOBALS['wp']->query_vars['rest_route'] ) {
159 $grid = WPUPG_Grid_Manager::get_grid( $atts['id'] );
160
161 if ( $grid ) {
162 $template_css = WPUPG_Template_Manager::get_template_css( $grid->template() );
163 $output .= '<style type="text/css">' . $template_css . '</style>';
164 }
165 }
166
167 $output .= WPUPG_Shortcode::grid_shortcode( $atts );
168
169 return $output;
170 }
171
172 /**
173 * Render the filter block.
174 *
175 * @since 3.0.0
176 * @param mixed $atts Block attributes.
177 */
178 public static function render_filter_block( $atts ) {
179 $atts = self::parse_atts( $atts );
180 return WPUPG_Shortcode::filter_shortcode( $atts );
181 }
182 }
183
184 WPUPG_Blocks::init();
185