PluginProbe ʕ •ᴥ•ʔ
Reviews Feed – Add Testimonials and Customer Reviews From Google Reviews, Yelp, TripAdvisor, and More / 2.7.0
Reviews Feed – Add Testimonials and Customer Reviews From Google Reviews, Yelp, TripAdvisor, and More v2.7.0
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 1 month ago SB_Recommended_Blocks.php 1 month ago SB_Reviews_Blocks.php 1 month ago
SB_Reviews_Blocks.php
352 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 {
106 // Cache the CSS payload across the request lifecycle. block_editor_settings_all
107 // fires on every block-editor request (post editor, site editor, widget editor)
108 // and the CSS bytes on disk don't change between calls, so re-reading them is
109 // wasteful disk I/O on the hottest path of the editor.
110 // TODO: also scope this by screen so we only inject when the editor could host
111 // this plugin's blocks. Scoping is intentionally skipped for now because
112 // block_editor_settings_all fires in REST contexts where get_current_screen()
113 // is unreliable, and over-scoping would re-break the iframe styling fix.
114 static $cached = null;
115
116 if (null === $cached) {
117 $files = array(
118 trailingslashit(SBR_PLUGIN_DIR) . 'assets/css/sbr-styles.min.css',
119 );
120
121 $cached = array();
122 foreach ($files as $file) {
123 if (! file_exists($file)) {
124 continue;
125 }
126 $css = file_get_contents($file);
127 if (false === $css) {
128 continue;
129 }
130 $cached[] = array( 'css' => $css );
131 }
132 }
133
134 if (! isset($settings['styles']) || ! is_array($settings['styles'])) {
135 $settings['styles'] = array();
136 }
137
138 foreach ($cached as $entry) {
139 $settings['styles'][] = $entry;
140 }
141
142 return $settings;
143 }
144
145 /**
146 * Register Reviews Feed Gutenberg block on the backend.
147 *
148 * @since 2.1
149 */
150 public function register_block()
151 {
152 $attributes = array(
153 'shortcodeSettings' => [
154 'type' => 'string',
155 ],
156 'noNewChanges' => [
157 'type' => 'boolean',
158 ],
159 'executed' => [
160 'type' => 'boolean',
161 ]
162 );
163
164 // Register stylesheet for block editor iframe
165 $min = !empty($_GET['sb_debug']) ? '' : '.min';
166 wp_register_style(
167 'sbr-block-styles',
168 trailingslashit(SBR_PLUGIN_URL) . 'assets/css/sbr-styles' . $min . '.css',
169 [],
170 SBRVER
171 );
172
173 register_block_type(
174 'sbr/sbr-feed-block',
175 array(
176 'api_version' => 3,
177 'attributes' => $attributes,
178 'render_callback' => [$this, 'get_feed_html'],
179 'editor_style' => 'sbr-block-styles',
180 'supports' => array( 'inserter' => false ),
181 )
182 );
183 }
184
185 /**
186 * Enqueue feed frontend assets so the legacy block preview renders inside
187 * the WP 6.7+ iframe block editor. Mirrors SB_Feed_Block::enqueue_block_content_assets().
188 *
189 * @since 2.5.1
190 */
191 public function enqueue_block_content_assets()
192 {
193 if (! is_admin()) {
194 return;
195 }
196 sbr_scripts_enqueue(true);
197 }
198
199 /**
200 * Get Feeds List Options Array
201 *
202 * @return array
203 */
204 public function get_feed_list_options()
205 {
206 $result = [
207 [
208 'value' => '',
209 'label' => __('Select a Feed', 'reviews-feed'),
210 'disabled' => true
211 ]
212 ];
213 $feeds = DB::get_feeds_list([], true);
214 if (is_array($feeds)) {
215 foreach ($feeds as $feed) {
216 array_push(
217 $result,
218 [
219 'value' => $feed['id'],
220 'label' => $feed['feed_name']
221 ]
222 );
223 }
224 }
225 return $result;
226 }
227
228 /**
229 * Load Reviews Feed Gutenberg block scripts.
230 *
231 * @since 2.1
232 */
233 public function enqueue_block_editor_assets()
234 {
235 wp_enqueue_script(
236 'sbr-feed-block',
237 trailingslashit(SBR_PLUGIN_URL) . 'assets/js/sbr-blocks.js',
238 array( 'wp-blocks', 'wp-i18n', 'wp-element', 'wp-block-editor' ),
239 SBRVER,
240 true
241 );
242
243 $shortcodeSettings = '';
244 $feeds_list_option = $this->get_feed_list_options();
245
246 $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.
247
248 $sbr_js_file = $is_script_debug
249 ? 'assets/js/sbr-feed.js'
250 : 'assets/js/sbr-feed.min.js';
251
252 $jquery_file = 'js/jquery/jquery' . ( $is_script_debug ? '' : '.min' ) . '.js';
253
254 $sbr_options = array(
255 'adminAjaxUrl' => admin_url('admin-ajax.php'),
256 );
257
258 $i18n = array(
259 'addSettings' => esc_html__('Add Settings', 'reviews-feed'),
260 'shortcodeSettings' => esc_html__('Shortcode Settings', 'reviews-feed'),
261 'example' => esc_html__('Example', 'reviews-feed'),
262 'preview' => esc_html__('Apply Changes', 'reviews-feed'),
263 'selectFeedLabel' => esc_html__('Select a Feed', 'reviews-feed'),
264 );
265
266 if (!empty($_GET['sbr_wizard'])) {
267 $shortcodeSettings = 'feed="' . (int) sanitize_text_field(wp_unslash($_GET['sbr_wizard'])) . '"';
268 }
269
270 wp_localize_script(
271 'sbr-feed-block',
272 'sbr_block_editor',
273 [
274 'wpnonce' => wp_create_nonce('reviews-blocks'),
275 'canShowFeed' => true,
276 'configureLink' => admin_url('admin.php?page=sbr-settings'),
277 'shortcodeSettings' => $shortcodeSettings,
278 'feedsListOption' => $feeds_list_option,
279 'i18n' => $i18n,
280 'iframeScriptUrl' => trailingslashit(SBR_PLUGIN_URL) . $sbr_js_file,
281 'jqueryUrl' => includes_url($jquery_file),
282 'sbrOptions' => $sbr_options,
283 ]
284 );
285 }
286
287 /**
288 * Get form HTML to display in a Reviews Feed Gutenberg block.
289 *
290 * @param array $attr Attributes passed by Reviews Feed Gutenberg block.
291 *
292 * @since 2.1
293 *
294 * @return string
295 */
296 public function get_feed_html($attr)
297 {
298 $return = '';
299
300 $shortcode_settings = isset($attr['shortcodeSettings'])
301 ? $attr['shortcodeSettings']
302 : '';
303
304
305 if (
306 empty($shortcode_settings) ||
307 (
308 strpos($shortcode_settings, 'feed=') === false &&
309 ! is_numeric($shortcode_settings)
310 )
311 ) {
312 $feeds = DB::get_feeds_list([], true);
313 if (!empty($feeds[0]['id'])) {
314 $shortcode_settings = 'feed="' . (int) $feeds[0]['id'] . '"';
315 }
316 } elseif (is_numeric($shortcode_settings)) {
317 $shortcode_settings = 'feed="' . (int) $shortcode_settings . '"';
318 }
319
320
321
322 $shortcode_settings = str_replace(
323 [
324 '[reviews-feed',
325 ']'
326 ],
327 ' ',
328 $shortcode_settings
329 );
330
331 $return .= do_shortcode('[reviews-feed ' . $shortcode_settings . ']');
332
333 return $return;
334 }
335
336 /**
337 * Checking if is Gutenberg REST API call.
338 *
339 * @since 2.1
340 *
341 * @return bool True if is Gutenberg REST API call.
342 */
343 public static function is_gb_editor()
344 {
345 return defined('REST_REQUEST') &&
346 REST_REQUEST &&
347 ! empty($_REQUEST['context']) &&
348 'edit' === $_REQUEST['context']; // phpcs:ignore
349 }
350
351 }
352