SBR_Modern_Feed_Block.php
1 week ago
SB_Recommended_Blocks.php
1 week ago
SB_Reviews_Blocks.php
1 week ago
SB_Reviews_Blocks.php
305 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 | // Styles enqueued on enqueue_block_assets are copied into the iframed editor |
| 82 | // canvas by core (wp_get_iframed_editor_assets), so this reaches the iframe on |
| 83 | // WP 7.0+ and the plain editor on older versions. Do NOT push raw CSS into the |
| 84 | // block_editor_settings_all `styles` array instead: entries there are treated |
| 85 | // as theme editor styles on every WP version, which suppresses core's default |
| 86 | // editor typography and drops the post title/content to the browser's serif |
| 87 | // fallback on themes that ship no editor styles. sbr-styles is covered twice |
| 88 | // over: sbr_scripts_enqueue() below and the block's registered editor_style. |
| 89 | add_action( |
| 90 | 'enqueue_block_assets', |
| 91 | array( |
| 92 | $this, |
| 93 | 'enqueue_block_content_assets', |
| 94 | ) |
| 95 | ); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Register Reviews Feed Gutenberg block on the backend. |
| 100 | * |
| 101 | * @since 2.1 |
| 102 | */ |
| 103 | public function register_block() |
| 104 | { |
| 105 | $attributes = array( |
| 106 | 'shortcodeSettings' => [ |
| 107 | 'type' => 'string', |
| 108 | ], |
| 109 | 'noNewChanges' => [ |
| 110 | 'type' => 'boolean', |
| 111 | ], |
| 112 | 'executed' => [ |
| 113 | 'type' => 'boolean', |
| 114 | ] |
| 115 | ); |
| 116 | |
| 117 | // Register stylesheet for block editor iframe |
| 118 | $min = !empty($_GET['sb_debug']) ? '' : '.min'; |
| 119 | wp_register_style( |
| 120 | 'sbr-block-styles', |
| 121 | trailingslashit(SBR_PLUGIN_URL) . 'assets/css/sbr-styles' . $min . '.css', |
| 122 | [], |
| 123 | SBRVER |
| 124 | ); |
| 125 | |
| 126 | register_block_type( |
| 127 | 'sbr/sbr-feed-block', |
| 128 | array( |
| 129 | 'api_version' => 3, |
| 130 | 'attributes' => $attributes, |
| 131 | 'render_callback' => [$this, 'get_feed_html'], |
| 132 | 'editor_style' => 'sbr-block-styles', |
| 133 | 'supports' => array( 'inserter' => false ), |
| 134 | ) |
| 135 | ); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Enqueue feed frontend assets so the legacy block preview renders inside |
| 140 | * the WP 6.7+ iframe block editor. Mirrors SB_Feed_Block::enqueue_block_content_assets(). |
| 141 | * |
| 142 | * @since 2.5.1 |
| 143 | */ |
| 144 | public function enqueue_block_content_assets() |
| 145 | { |
| 146 | if (! is_admin()) { |
| 147 | return; |
| 148 | } |
| 149 | sbr_scripts_enqueue(true); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Get Feeds List Options Array |
| 154 | * |
| 155 | * @return array |
| 156 | */ |
| 157 | public function get_feed_list_options() |
| 158 | { |
| 159 | $result = [ |
| 160 | [ |
| 161 | 'value' => '', |
| 162 | 'label' => __('Select a Feed', 'reviews-feed'), |
| 163 | 'disabled' => true |
| 164 | ] |
| 165 | ]; |
| 166 | $feeds = DB::get_feeds_list([], true); |
| 167 | if (is_array($feeds)) { |
| 168 | foreach ($feeds as $feed) { |
| 169 | array_push( |
| 170 | $result, |
| 171 | [ |
| 172 | 'value' => $feed['id'], |
| 173 | 'label' => $feed['feed_name'] |
| 174 | ] |
| 175 | ); |
| 176 | } |
| 177 | } |
| 178 | return $result; |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Load Reviews Feed Gutenberg block scripts. |
| 183 | * |
| 184 | * @since 2.1 |
| 185 | */ |
| 186 | public function enqueue_block_editor_assets() |
| 187 | { |
| 188 | wp_enqueue_script( |
| 189 | 'sbr-feed-block', |
| 190 | trailingslashit(SBR_PLUGIN_URL) . 'assets/js/sbr-blocks.js', |
| 191 | array( 'wp-blocks', 'wp-i18n', 'wp-element', 'wp-block-editor' ), |
| 192 | SBRVER, |
| 193 | true |
| 194 | ); |
| 195 | |
| 196 | $shortcodeSettings = ''; |
| 197 | $feeds_list_option = $this->get_feed_list_options(); |
| 198 | |
| 199 | $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. |
| 200 | |
| 201 | $sbr_js_file = $is_script_debug |
| 202 | ? 'assets/js/sbr-feed.js' |
| 203 | : 'assets/js/sbr-feed.min.js'; |
| 204 | |
| 205 | $jquery_file = 'js/jquery/jquery' . ( $is_script_debug ? '' : '.min' ) . '.js'; |
| 206 | |
| 207 | $sbr_options = array( |
| 208 | 'adminAjaxUrl' => admin_url('admin-ajax.php'), |
| 209 | ); |
| 210 | |
| 211 | $i18n = array( |
| 212 | 'addSettings' => esc_html__('Add Settings', 'reviews-feed'), |
| 213 | 'shortcodeSettings' => esc_html__('Shortcode Settings', 'reviews-feed'), |
| 214 | 'example' => esc_html__('Example', 'reviews-feed'), |
| 215 | 'preview' => esc_html__('Apply Changes', 'reviews-feed'), |
| 216 | 'selectFeedLabel' => esc_html__('Select a Feed', 'reviews-feed'), |
| 217 | ); |
| 218 | |
| 219 | if (!empty($_GET['sbr_wizard'])) { |
| 220 | $shortcodeSettings = 'feed="' . (int) sanitize_text_field(wp_unslash($_GET['sbr_wizard'])) . '"'; |
| 221 | } |
| 222 | |
| 223 | wp_localize_script( |
| 224 | 'sbr-feed-block', |
| 225 | 'sbr_block_editor', |
| 226 | [ |
| 227 | 'wpnonce' => wp_create_nonce('reviews-blocks'), |
| 228 | 'canShowFeed' => true, |
| 229 | 'configureLink' => admin_url('admin.php?page=sbr-settings'), |
| 230 | 'shortcodeSettings' => $shortcodeSettings, |
| 231 | 'feedsListOption' => $feeds_list_option, |
| 232 | 'i18n' => $i18n, |
| 233 | 'iframeScriptUrl' => trailingslashit(SBR_PLUGIN_URL) . $sbr_js_file, |
| 234 | 'jqueryUrl' => includes_url($jquery_file), |
| 235 | 'sbrOptions' => $sbr_options, |
| 236 | ] |
| 237 | ); |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Get form HTML to display in a Reviews Feed Gutenberg block. |
| 242 | * |
| 243 | * @param array $attr Attributes passed by Reviews Feed Gutenberg block. |
| 244 | * |
| 245 | * @since 2.1 |
| 246 | * |
| 247 | * @return string |
| 248 | */ |
| 249 | public function get_feed_html($attr) |
| 250 | { |
| 251 | $return = ''; |
| 252 | |
| 253 | $shortcode_settings = isset($attr['shortcodeSettings']) |
| 254 | ? $attr['shortcodeSettings'] |
| 255 | : ''; |
| 256 | |
| 257 | |
| 258 | if ( |
| 259 | empty($shortcode_settings) || |
| 260 | ( |
| 261 | strpos($shortcode_settings, 'feed=') === false && |
| 262 | ! is_numeric($shortcode_settings) |
| 263 | ) |
| 264 | ) { |
| 265 | $feeds = DB::get_feeds_list([], true); |
| 266 | if (!empty($feeds[0]['id'])) { |
| 267 | $shortcode_settings = 'feed="' . (int) $feeds[0]['id'] . '"'; |
| 268 | } |
| 269 | } elseif (is_numeric($shortcode_settings)) { |
| 270 | $shortcode_settings = 'feed="' . (int) $shortcode_settings . '"'; |
| 271 | } |
| 272 | |
| 273 | |
| 274 | |
| 275 | $shortcode_settings = str_replace( |
| 276 | [ |
| 277 | '[reviews-feed', |
| 278 | ']' |
| 279 | ], |
| 280 | ' ', |
| 281 | $shortcode_settings |
| 282 | ); |
| 283 | |
| 284 | $return .= do_shortcode('[reviews-feed ' . $shortcode_settings . ']'); |
| 285 | |
| 286 | return $return; |
| 287 | } |
| 288 | |
| 289 | /** |
| 290 | * Checking if is Gutenberg REST API call. |
| 291 | * |
| 292 | * @since 2.1 |
| 293 | * |
| 294 | * @return bool True if is Gutenberg REST API call. |
| 295 | */ |
| 296 | public static function is_gb_editor() |
| 297 | { |
| 298 | return defined('REST_REQUEST') && |
| 299 | REST_REQUEST && |
| 300 | ! empty($_REQUEST['context']) && |
| 301 | 'edit' === $_REQUEST['context']; // phpcs:ignore |
| 302 | } |
| 303 | |
| 304 | } |
| 305 |