PluginProbe ʕ •ᴥ•ʔ
Reviews Feed – Add Testimonials and Customer Reviews From Google Reviews, Yelp, TripAdvisor, and More / 2.6.7
Reviews Feed – Add Testimonials and Customer Reviews From Google Reviews, Yelp, TripAdvisor, and More v2.6.7
2.11.0 2.10.0 2.9.0 2.8.0 2.7.0 2.6.7 2.6.8 2.6.5 2.6.4 2.6.3 2.6.2 2.6.0 2.5.5 2.5.4 2.5.3 2.5.2 trunk 1.0 1.0.1 1.0.2 1.0.3 1.1 1.1.1 1.1.2 1.2.0 2.0 2.1.0 2.1.1 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.5.0 2.5.1
reviews-feed / class / Common / Admin / Blocks / SB_Reviews_Blocks.php
reviews-feed / class / Common / Admin / Blocks Last commit date
SBR_Modern_Feed_Block.php 2 months ago SB_Recommended_Blocks.php 2 months ago SB_Reviews_Blocks.php 2 months ago
SB_Reviews_Blocks.php
350 lines
1 <?php
2
3 /**
4 * SB_Reviews_Blocks
5 *
6 * @since 2.1
7 */
8
9 namespace SmashBalloon\Reviews\Common\Admin\Blocks;
10
11 use SmashBalloon\Reviews\Common\Customizer\DB;
12 use Smashballoon\Stubs\Services\ServiceProvider;
13
14 // Exit if accessed directly
15 if (!defined('ABSPATH')) {
16 exit;
17 }
18
19 class SB_Reviews_Blocks extends ServiceProvider
20 {
21 /**
22 * Register Reviews Block
23 *
24 * @return void
25 */
26 public function register()
27 {
28 if ($this->allow_load()) {
29 $this->load();
30 }
31 }
32
33 /**
34 * Indicates if current integration is allowed to load.
35 *
36 * @since 2.1
37 *
38 * @return bool
39 */
40 public function allow_load()
41 {
42 return function_exists('register_block_type');
43 }
44
45 /**
46 * Loads an integration.
47 *
48 * @since 2.1
49 */
50 public function load()
51 {
52 $this->hooks();
53
54 $modern_block = new SBR_Modern_Feed_Block();
55 $modern_block->register_hooks();
56 }
57 /**
58 * Integration hooks.
59 *
60 * @since 2.1
61 */
62 protected function hooks()
63 {
64 add_action(
65 'init',
66 [
67 $this,
68 'register_block'
69 ]
70 );
71
72 add_action(
73 'enqueue_block_editor_assets',
74 [
75 $this,
76 'enqueue_block_editor_assets'
77 ],
78 SBR_Modern_Feed_Block::EDITOR_ASSETS_PRIORITY
79 );
80
81 add_action(
82 'enqueue_block_assets',
83 array(
84 $this,
85 'enqueue_block_content_assets',
86 )
87 );
88
89 add_filter( 'block_editor_settings_all', array( $this, 'inject_iframe_styles' ) );
90 }
91
92 /**
93 * Inject block UI and feed CSS into the WP 7.0+ iframed editor canvas.
94 *
95 * `block_editor_settings_all` exposes a `styles` array that WordPress renders
96 * inline inside the iframe `<head>`. wp_enqueue_style on the outer admin page
97 * does not propagate to the iframe for api_version 3 blocks, so we have to
98 * push the CSS contents through this filter for it to be visible inside the
99 * iframe.
100 *
101 * @param array $settings Block editor settings.
102 * @return array
103 */
104 public function inject_iframe_styles( $settings ) {
105 // Cache the CSS payload across the request lifecycle. block_editor_settings_all
106 // fires on every block-editor request (post editor, site editor, widget editor)
107 // and the CSS bytes on disk don't change between calls, so re-reading them is
108 // wasteful disk I/O on the hottest path of the editor.
109 // TODO: also scope this by screen so we only inject when the editor could host
110 // this plugin's blocks. Scoping is intentionally skipped for now because
111 // block_editor_settings_all fires in REST contexts where get_current_screen()
112 // is unreliable, and over-scoping would re-break the iframe styling fix.
113 static $cached = null;
114
115 if ( null === $cached ) {
116 $files = array(
117 trailingslashit( SBR_PLUGIN_DIR ) . 'assets/css/sbr-styles.min.css',
118 );
119
120 $cached = array();
121 foreach ( $files as $file ) {
122 if ( ! file_exists( $file ) ) {
123 continue;
124 }
125 $css = file_get_contents( $file );
126 if ( false === $css ) {
127 continue;
128 }
129 $cached[] = array( 'css' => $css );
130 }
131 }
132
133 if ( ! isset( $settings['styles'] ) || ! is_array( $settings['styles'] ) ) {
134 $settings['styles'] = array();
135 }
136
137 foreach ( $cached as $entry ) {
138 $settings['styles'][] = $entry;
139 }
140
141 return $settings;
142 }
143
144 /**
145 * Register Reviews Feed Gutenberg block on the backend.
146 *
147 * @since 2.1
148 */
149 public function register_block()
150 {
151 $attributes = array(
152 'shortcodeSettings' => [
153 'type' => 'string',
154 ],
155 'noNewChanges' => [
156 'type' => 'boolean',
157 ],
158 'executed' => [
159 'type' => 'boolean',
160 ]
161 );
162
163 // Register stylesheet for block editor iframe
164 $min = !empty($_GET['sb_debug']) ? '' : '.min';
165 wp_register_style(
166 'sbr-block-styles',
167 trailingslashit(SBR_PLUGIN_URL) . 'assets/css/sbr-styles' . $min . '.css',
168 [],
169 SBRVER
170 );
171
172 register_block_type(
173 'sbr/sbr-feed-block',
174 array(
175 'api_version' => 3,
176 'attributes' => $attributes,
177 'render_callback' => [$this, 'get_feed_html'],
178 'editor_style' => 'sbr-block-styles',
179 'supports' => array( 'inserter' => false ),
180 )
181 );
182 }
183
184 /**
185 * Enqueue feed frontend assets so the legacy block preview renders inside
186 * the WP 6.7+ iframe block editor. Mirrors SB_Feed_Block::enqueue_block_content_assets().
187 *
188 * @since 2.5.1
189 */
190 public function enqueue_block_content_assets() {
191 if ( ! is_admin() ) {
192 return;
193 }
194 sbr_scripts_enqueue( true );
195 }
196
197 /**
198 * Get Feeds List Options Array
199 *
200 * @return array
201 */
202 public function get_feed_list_options()
203 {
204 $result = [
205 [
206 'value' => '',
207 'label' => __('Select a Feed', 'reviews-feed'),
208 'disabled' => true
209 ]
210 ];
211 $feeds = DB::get_feeds_list([], true);
212 if (is_array($feeds)) {
213 foreach ($feeds as $feed) {
214 array_push(
215 $result,
216 [
217 'value' => $feed['id'],
218 'label' => $feed['feed_name']
219 ]
220 );
221 }
222 }
223 return $result;
224 }
225
226 /**
227 * Load Reviews Feed Gutenberg block scripts.
228 *
229 * @since 2.1
230 */
231 public function enqueue_block_editor_assets()
232 {
233 wp_enqueue_script(
234 'sbr-feed-block',
235 trailingslashit(SBR_PLUGIN_URL) . 'assets/js/sbr-blocks.js',
236 array( 'wp-blocks', 'wp-i18n', 'wp-element', 'wp-block-editor' ),
237 SBRVER,
238 true
239 );
240
241 $shortcodeSettings = '';
242 $feeds_list_option = $this->get_feed_list_options();
243
244 $is_script_debug = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) || ! empty( $_GET['sb_debug'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only debug flag, no state change.
245
246 $sbr_js_file = $is_script_debug
247 ? 'assets/js/sbr-feed.js'
248 : 'assets/js/sbr-feed.min.js';
249
250 $jquery_file = 'js/jquery/jquery' . ( $is_script_debug ? '' : '.min' ) . '.js';
251
252 $sbr_options = array(
253 'adminAjaxUrl' => admin_url( 'admin-ajax.php' ),
254 );
255
256 $i18n = array(
257 'addSettings' => esc_html__('Add Settings', 'reviews-feed'),
258 'shortcodeSettings' => esc_html__('Shortcode Settings', 'reviews-feed'),
259 'example' => esc_html__('Example', 'reviews-feed'),
260 'preview' => esc_html__('Apply Changes', 'reviews-feed'),
261 'selectFeedLabel' => esc_html__('Select a Feed', 'reviews-feed'),
262 );
263
264 if (!empty($_GET['sbr_wizard'])) {
265 $shortcodeSettings = 'feed="' . (int) sanitize_text_field(wp_unslash($_GET['sbr_wizard'])) . '"';
266 }
267
268 wp_localize_script(
269 'sbr-feed-block',
270 'sbr_block_editor',
271 [
272 'wpnonce' => wp_create_nonce('reviews-blocks'),
273 'canShowFeed' => true,
274 'configureLink' => admin_url('admin.php?page=sbr-settings'),
275 'shortcodeSettings' => $shortcodeSettings,
276 'feedsListOption' => $feeds_list_option,
277 'i18n' => $i18n,
278 'iframeScriptUrl' => trailingslashit( SBR_PLUGIN_URL ) . $sbr_js_file,
279 'jqueryUrl' => includes_url( $jquery_file ),
280 'sbrOptions' => $sbr_options,
281 ]
282 );
283 }
284
285 /**
286 * Get form HTML to display in a Reviews Feed Gutenberg block.
287 *
288 * @param array $attr Attributes passed by Reviews Feed Gutenberg block.
289 *
290 * @since 2.1
291 *
292 * @return string
293 */
294 public function get_feed_html($attr)
295 {
296 $return = '';
297
298 $shortcode_settings = isset($attr['shortcodeSettings'])
299 ? $attr['shortcodeSettings']
300 : '';
301
302
303 if (
304 empty($shortcode_settings) ||
305 (
306 strpos($shortcode_settings, 'feed=') === false &&
307 ! is_numeric($shortcode_settings)
308 )
309 ) {
310 $feeds = DB::get_feeds_list([], true);
311 if (!empty($feeds[0]['id'])) {
312 $shortcode_settings = 'feed="' . (int) $feeds[0]['id'] . '"';
313 }
314 } elseif (is_numeric($shortcode_settings)) {
315 $shortcode_settings = 'feed="' . (int) $shortcode_settings . '"';
316 }
317
318
319
320 $shortcode_settings = str_replace(
321 [
322 '[reviews-feed',
323 ']'
324 ],
325 ' ',
326 $shortcode_settings
327 );
328
329 $return .= do_shortcode('[reviews-feed ' . $shortcode_settings . ']');
330
331 return $return;
332 }
333
334 /**
335 * Checking if is Gutenberg REST API call.
336 *
337 * @since 2.1
338 *
339 * @return bool True if is Gutenberg REST API call.
340 */
341 public static function is_gb_editor()
342 {
343 return defined('REST_REQUEST') &&
344 REST_REQUEST &&
345 ! empty($_REQUEST['context']) &&
346 'edit' === $_REQUEST['context']; // phpcs:ignore
347 }
348
349 }
350