← All changes
|
src/blocks/recommendations/class-recommendations-block.php
+31
-104
3.2.0
→
3.24.2
View file →
| @@ -1,7 +1,7 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * Parse.ly Recommendations Block class file. | |
| 3 | + * Parse.ly Recommendations Block class | |
| 4 | 4 | * |
| 5 | 5 | * @package Parsely |
| 6 | 6 | * @since 3.2.0 |
| 7 | 7 | */ |
| @@ -15,31 +15,24 @@ | ||
| 15 | 15 | * |
| 16 | 16 | * @since 3.2.0 |
| 17 | 17 | */ |
| 18 | 18 | class Recommendations_Block { |
| 19 | - const MINIMUM_WORDPRESS_VERSION = '5.6'; | |
| 19 | + public const MINIMUM_WORDPRESS_VERSION = '5.9'; | |
| 20 | 20 | |
| 21 | 21 | /** |
| 22 | - * Determine whether the block and its assets should be registered. | |
| 22 | + * Determines whether the block and its assets should be registered. | |
| 23 | 23 | * |
| 24 | 24 | * @since 3.2.0 |
| 25 | - * | |
| 26 | - * @return void | |
| 27 | 25 | */ |
| 28 | 26 | public function run(): void { |
| 29 | 27 | global $wp_version; |
| 30 | 28 | |
| 31 | - if ( ! apply_filters( 'wp_parsely_recommendations_block_enabled', false ) ) { | |
| 32 | - // This block is behind a "feature flag" and it's not enabled. Bail. | |
| 33 | - return; | |
| 34 | - } | |
| 35 | - | |
| 36 | 29 | if ( ! isset( $wp_version ) || version_compare( $wp_version, self::MINIMUM_WORDPRESS_VERSION ) < 0 ) { |
| 37 | 30 | // WordPress is not recent enough to run this block. |
| 38 | 31 | return; |
| 39 | 32 | } |
| 40 | 33 | |
| 41 | - self::register_block_and_assets(); | |
| 34 | + self::register_block(); | |
| 42 | 35 | } |
| 43 | 36 | |
| 44 | 37 | /** |
| 45 | 38 | * Registers all block assets so that they can be enqueued through the |
| @@ -44,120 +37,54 @@ | ||
| 44 | 37 | /** |
| 45 | 38 | * Registers all block assets so that they can be enqueued through the |
| 46 | 39 | * WordPress Block Editor in the corresponding context. |
| 47 | 40 | * |
| 48 | - * @since 3.2.0 | |
| 49 | - * | |
| 50 | - * @return void | |
| 41 | + * @since 3.3.0 | |
| 51 | 42 | */ |
| 52 | - public static function register_block_and_assets(): void { | |
| 53 | - // Temporary workaround - don't register block when in FSE due to issues. | |
| 54 | - global $pagenow; | |
| 55 | - if ( 'site-editor.php' === $pagenow ) { | |
| 56 | - return; | |
| 57 | - } | |
| 58 | - | |
| 59 | - $plugin_url = plugin_dir_url( PARSELY_FILE ); | |
| 60 | - | |
| 61 | - $editor_asset_file = require plugin_dir_path( PARSELY_FILE ) . 'build/recommendations-edit.asset.php'; | |
| 62 | - wp_register_script( | |
| 63 | - 'wp-parsely-recommendations-block-editor', | |
| 64 | - $plugin_url . 'build/recommendations-edit.js', | |
| 65 | - $editor_asset_file['dependencies'], | |
| 66 | - $editor_asset_file['version'], | |
| 67 | - true | |
| 43 | + public static function register_block(): void { | |
| 44 | + /** | |
| 45 | + * Register the block by passing the path to it's block.json file that | |
| 46 | + * contains the majority of it's definition. This file will be copied | |
| 47 | + * into `build/blocks/recommendations`by the build process and should be | |
| 48 | + * accessed there. | |
| 49 | + * | |
| 50 | + * @see https://developer.wordpress.org/reference/functions/register_block_type/ | |
| 51 | + */ | |
| 52 | + register_block_type( | |
| 53 | + plugin_dir_path( PARSELY_FILE ) . 'build/blocks/recommendations/', | |
| 54 | + array( | |
| 55 | + 'render_callback' => self::class . '::render_callback', | |
| 56 | + ) | |
| 68 | 57 | ); |
| 69 | - | |
| 70 | - $script_asset_file = require plugin_dir_path( PARSELY_FILE ) . 'build/recommendations.asset.php'; | |
| 71 | - wp_register_script( | |
| 72 | - 'wp-parsely-recommendations-block', | |
| 73 | - $plugin_url . 'build/recommendations.js', | |
| 74 | - $script_asset_file['dependencies'], | |
| 75 | - $script_asset_file['version'], | |
| 76 | - true | |
| 77 | - ); | |
| 78 | - | |
| 79 | - wp_register_style( | |
| 80 | - 'wp-parsely-recommendations-block', | |
| 81 | - $plugin_url . 'build/style-recommendations-edit.css', | |
| 82 | - array(), | |
| 83 | - $script_asset_file['version'] | |
| 84 | - ); | |
| 85 | - | |
| 86 | - register_block_type( 'wp-parsely/recommendations', self::get_block_registration_args() ); | |
| 87 | 58 | } |
| 88 | 59 | |
| 89 | 60 | /** |
| 90 | - * Return the Block's registration arguments. | |
| 91 | - * | |
| 92 | - * @since 3.2.0 | |
| 93 | - * | |
| 94 | - * @return array<string, mixed> | |
| 95 | - */ | |
| 96 | - private static function get_block_registration_args(): array { | |
| 97 | - return array( | |
| 98 | - 'editor_script' => 'wp-parsely-recommendations-block-editor', | |
| 99 | - 'render_callback' => __CLASS__ . '::render_callback', | |
| 100 | - 'script' => 'wp-parsely-recommendations-block', | |
| 101 | - 'style' => 'wp-parsely-recommendations-block', | |
| 102 | - 'supports' => array( | |
| 103 | - 'html' => false, | |
| 104 | - ), | |
| 105 | - 'attributes' => array( | |
| 106 | - 'boost' => array( | |
| 107 | - 'type' => 'string', | |
| 108 | - 'default' => 'views', | |
| 109 | - ), | |
| 110 | - 'imagestyle' => array( | |
| 111 | - 'type' => 'string', | |
| 112 | - 'default' => 'original', | |
| 113 | - ), | |
| 114 | - 'limit' => array( | |
| 115 | - 'type' => 'number', | |
| 116 | - 'default' => 3, | |
| 117 | - ), | |
| 118 | - 'showimages' => array( | |
| 119 | - 'type' => 'boolean', | |
| 120 | - 'default' => true, | |
| 121 | - ), | |
| 122 | - 'sort' => array( | |
| 123 | - 'type' => 'string', | |
| 124 | - 'default' => 'score', | |
| 125 | - ), | |
| 126 | - 'tag' => array( | |
| 127 | - 'type' => 'string', | |
| 128 | - ), | |
| 129 | - 'title' => array( | |
| 130 | - 'type' => 'string', | |
| 131 | - 'default' => __( 'Related Content', 'wp-parsely' ), | |
| 132 | - ), | |
| 133 | - ), | |
| 134 | - ); | |
| 135 | - } | |
| 136 | - | |
| 137 | - /** | |
| 138 | 61 | * The Server-side render_callback for the wp-parsely/recommendations block. |
| 139 | 62 | * |
| 140 | 63 | * @since 3.2.0 |
| 141 | 64 | * |
| 142 | - * @param array $attributes The user-controlled settings for this block. | |
| 143 | - * @return string | |
| 65 | + * @param array<string, string> $attributes The user-controlled settings for this block. | |
| 66 | + * @return string|false | |
| 144 | 67 | */ |
| 145 | - public static function render_callback( array $attributes ): string { | |
| 68 | + public static function render_callback( array $attributes ) { | |
| 69 | + /** | |
| 70 | + * In block.json we define a `viewScript` that is mean to only be loaded | |
| 71 | + * on the front end. We need to manually enqueue this script here. | |
| 72 | + * | |
| 73 | + * The slug is automatically generated as {namespace}-{block-name}-view-script | |
| 74 | + */ | |
| 75 | + wp_enqueue_script( 'wp-parsely-recommendations-view-script' ); | |
| 146 | 76 | ob_start(); |
| 147 | 77 | ?> |
| 148 | -<section | |
| 78 | + <section | |
| 149 | 79 | <?php |
| 150 | 80 | echo wp_kses_post( get_block_wrapper_attributes() ); |
| 151 | 81 | |
| 152 | - // Remove any attributes that don't need to be in output. | |
| 153 | - unset( $attributes['tag'] ); | |
| 154 | - | |
| 155 | 82 | foreach ( $attributes as $name => $value ) { |
| 156 | 83 | echo ' data-' . esc_attr( $name ) . '="' . esc_attr( $value ) . '"'; |
| 157 | 84 | } |
| 158 | 85 | ?> |
| 159 | -></section> | |
| 86 | + ></section> | |
| 160 | 87 | <?php |
| 161 | 88 | return ob_get_clean(); |
| 162 | 89 | } |
| 163 | 90 | } |