PluginProbe
Smart Post – Post Grid, Post Carousel, Post Slider Gutenberg Blocks for Blog & News / 4.0.7
Smart Post – Post Grid, Post Carousel, Post Slider Gutenberg Blocks for Blog & News v4.0.7
4.0.8 4.0.7 4.0.6 4.0.5 4.0.4 4.0.3 4.0.2 4.0.1 2.3.5 2.3.6 2.4.0 2.4.1 2.4.10 2.4.11 2.4.12 2.4.13 2.4.14 2.4.15 2.4.16 2.4.17 2.4.18 2.4.19 2.4.2 2.4.20 2.4.21 All 88 releases
post-carousel / blocks / includes / class-smart-block-abstract.php

class-smart-block-abstract.php in Smart Post – Post Grid, Post Carousel, Post Slider Gutenberg Blocks for Blog & News 4.0.7, at blocks/includes/class-smart-block-abstract.php

197 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Abstract base class for Smart Post Show Blocks.
4 *
5 * @package Smart_Post_Show
6 * @subpackage Smart_Post_Show/blocks/includes
7 */
8
9 namespace SmartPostShow\Blocks;
10
11 use SmartPostShow\Blocks\Helper;
12
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit;
15 }
16
17 if ( ! class_exists( 'SP_Smart_Block_Base' ) ) {
18
19 /**
20 * Abstract Smart Post Block Base Class.
21 */
22 abstract class Block_Base {
23
24 /**
25 * Block name.
26 *
27 * @var string
28 */
29 protected $block_name;
30
31 /**
32 * Script handles.
33 *
34 * @var array
35 */
36 protected $scripts = array();
37
38 /**
39 * Style handles.
40 *
41 * @var array
42 */
43 protected $styles = array();
44
45 /**
46 * Check admin/editor page.
47 *
48 * @var boolean
49 */
50 protected $is_editor_page = false;
51
52 /**
53 * Editor script handles.
54 *
55 * @var array
56 */
57 protected $editor_scripts = array();
58
59 /**
60 * Editor style handles.
61 *
62 * @var array
63 */
64 protected $editor_styles = array();
65 /**
66 * Block keywords.
67 *
68 * @var array
69 */
70 protected $keywords = array( 'Smart post' );
71
72 /**
73 * Block category.
74 *
75 * @var string
76 */
77 protected $category = 'sp-smart-post-show';
78
79 /**
80 * Attributes
81 *
82 * @var array
83 */
84 protected $attributes = array(); // Add this property to hold block attributes.
85
86 /**
87 * Constructor function.
88 *
89 * @param array $block_attributes Block attributes.
90 */
91 public function __construct( $block_attributes = array() ) {
92 $this->attributes = $block_attributes;
93 $this->init();
94 }
95
96 /**
97 * Initialize block.
98 */
99 protected function init() {
100 $this->set_block_properties();
101 $this->register_block();
102 }
103
104 /**
105 * Set block-specific properties.
106 *
107 * Must be defined in child classes.
108 */
109 abstract protected function set_block_properties();
110
111 /**
112 * Get render callback for the block.
113 *
114 * Can be overridden in child classes.
115 *
116 * @return callable|null
117 */
118 protected function get_render_callback() {
119 return array( $this, 'render_block' );
120 }
121
122 /**
123 * Get cached post query result
124 *
125 * @param mixed $attributes Array of block attributes.
126 * @param mixed $unique_id Unique identifier for the block instance.
127 * @param mixed $block_id Block id.
128 * @return data
129 */
130 public function get_cached_post_query_result( $attributes, $unique_id = '', $block_id = '' ) {
131 $block = isset( $_GET['block'] ) ? sanitize_text_field( wp_unslash( $_GET['block'] ) ) : '';
132 $skip_cache_orderby = array( 'rand', 'most_viewed', 'most_liked', 'comment_count' );
133 if ( ( isset( $attributes['orderBy'] ) && in_array( $attributes['orderBy'], $skip_cache_orderby, true ) ) || ( ( ! empty( $block ) && $block == $block_id ) ) ) {
134 return \Sp_Smart_Post_Blocks_Query::post_query_frontend( $attributes, $block_id );
135 }
136
137 $transient_key = 'sp_smart_post_query_' . $block_id;
138
139 // Track transient key.
140 $keys = get_option( 'sp_smart_post_transients', array() );
141 if ( ! in_array( $transient_key, $keys, true ) ) {
142 $keys[] = $transient_key;
143 update_option( 'sp_smart_post_transients', $keys, false );
144 }
145 $data = Helper::get_transient( $transient_key );
146 if ( false === $data ) {
147 $data = \Sp_Smart_Post_Blocks_Query::post_query_frontend( $attributes, $block_id );
148 Helper::set_transient( $transient_key, $data, DAY_IN_SECONDS );
149 }
150 return $data;
151 }
152
153 /**
154 * Block render logic.
155 *
156 * Should be overridden if the block is dynamic.
157 *
158 * @param array $attributes Block attributes.
159 * @param string $content Inner block content.
160 * @param string $blocks Inner blocks.
161 * @return string
162 */
163 public function render_block( $attributes, $content = '', $blocks = array() ) {
164 return $content;
165 }
166
167 /**
168 * Register the block with WordPress.
169 */
170 protected function register_block() {
171 if ( ! function_exists( 'register_block_type' ) ) {
172 return;
173 }
174 $args = array(
175 'editor_script' => $this->editor_scripts,
176 'editor_style' => $this->editor_styles,
177 'script' => is_admin() ? '' : $this->scripts,
178 'style' => $this->styles,
179 );
180 if ( ! empty( $this->attributes ) ) {
181 $args['$schema'] = 'https://schemas.wp.org/trunk/block.json';
182 $args['attributes'] = $this->attributes;
183 $args['keywords'] = $this->keywords;
184 $args['apiVersion'] = 3;
185 $args['category'] = $this->category;
186 $args['blockName'] = "sp-smart-post-show/{$this->block_name}";
187 }
188 $render_callback = $this->get_render_callback();
189 if ( is_callable( $render_callback ) ) {
190 $args['render_callback'] = $render_callback;
191 }
192
193 register_block_type( "sp-smart-post-show/{$this->block_name}", $args );
194 }
195 }
196 }
197