| 1 |
<?php |
| 2 |
/** |
| 3 |
* Parse.ly Recommendations Block class |
| 4 |
* |
| 5 |
* @package Parsely |
| 6 |
* @since 3.2.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
declare(strict_types=1); |
| 10 |
|
| 11 |
namespace Parsely; |
| 12 |
|
| 13 |
/** |
| 14 |
* Parse.ly Recommendations Block for the WordPress Block Editor. |
| 15 |
* |
| 16 |
* @since 3.2.0 |
| 17 |
*/ |
| 18 |
class Recommendations_Block { |
| 19 |
public const MINIMUM_WORDPRESS_VERSION = '5.9'; |
| 20 |
|
| 21 |
/** |
| 22 |
* Determines whether the block and its assets should be registered. |
| 23 |
* |
| 24 |
* @since 3.2.0 |
| 25 |
*/ |
| 26 |
public function run(): void { |
| 27 |
global $wp_version; |
| 28 |
|
| 29 |
if ( ! isset( $wp_version ) || version_compare( $wp_version, self::MINIMUM_WORDPRESS_VERSION ) < 0 ) { |
| 30 |
// WordPress is not recent enough to run this block. |
| 31 |
return; |
| 32 |
} |
| 33 |
|
| 34 |
self::register_block(); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Registers all block assets so that they can be enqueued through the |
| 39 |
* WordPress Block Editor in the corresponding context. |
| 40 |
* |
| 41 |
* @since 3.3.0 |
| 42 |
*/ |
| 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 |
) |
| 57 |
); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* The Server-side render_callback for the wp-parsely/recommendations block. |
| 62 |
* |
| 63 |
* @since 3.2.0 |
| 64 |
* |
| 65 |
* @param array<string, string> $attributes The user-controlled settings for this block. |
| 66 |
* @return string|false |
| 67 |
*/ |
| 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' ); |
| 76 |
ob_start(); |
| 77 |
?> |
| 78 |
<section |
| 79 |
<?php |
| 80 |
echo wp_kses_post( get_block_wrapper_attributes() ); |
| 81 |
|
| 82 |
foreach ( $attributes as $name => $value ) { |
| 83 |
echo ' data-' . esc_attr( $name ) . '="' . esc_attr( $value ) . '"'; |
| 84 |
} |
| 85 |
?> |
| 86 |
></section> |
| 87 |
<?php |
| 88 |
return ob_get_clean(); |
| 89 |
} |
| 90 |
} |
| 91 |
|