rating-star.php
86 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Star Rating Block. |
| 4 | * |
| 5 | * @since 8.0.0 |
| 6 | * |
| 7 | * @package automattic/jetpack |
| 8 | */ |
| 9 | |
| 10 | namespace Automattic\Jetpack\Extensions\Rating_Star; |
| 11 | |
| 12 | use Automattic\Jetpack\Blocks; |
| 13 | use Jetpack_Gutenberg; |
| 14 | |
| 15 | const FEATURE_NAME = 'rating-star'; |
| 16 | const BLOCK_NAME = 'jetpack/' . FEATURE_NAME; |
| 17 | |
| 18 | // Load generic function definitions. |
| 19 | require_once __DIR__ . '/rating-meta.php'; |
| 20 | |
| 21 | /** |
| 22 | * Registers the block for use in Gutenberg |
| 23 | * This is done via an action so that we can disable |
| 24 | * registration if we need to. |
| 25 | */ |
| 26 | function register_block() { |
| 27 | Blocks::jetpack_register_block( |
| 28 | BLOCK_NAME, |
| 29 | array( |
| 30 | 'render_callback' => __NAMESPACE__ . '\render_block', |
| 31 | 'attributes' => array( |
| 32 | 'rating' => array( |
| 33 | 'type' => 'number', |
| 34 | 'default' => 1, |
| 35 | ), |
| 36 | 'maxRating' => array( |
| 37 | 'type' => 'number', |
| 38 | 'default' => 5, |
| 39 | ), |
| 40 | 'color' => array( |
| 41 | 'type' => 'string', |
| 42 | ), |
| 43 | 'ratingStyle' => array( |
| 44 | 'type' => 'string', |
| 45 | 'default' => 'star', |
| 46 | ), |
| 47 | 'className' => array( |
| 48 | 'type' => 'string', |
| 49 | ), |
| 50 | 'align' => array( |
| 51 | 'type' => 'string', |
| 52 | 'default' => 'left', |
| 53 | ), |
| 54 | ), |
| 55 | ) |
| 56 | ); |
| 57 | } |
| 58 | add_action( 'init', __NAMESPACE__ . '\register_block' ); |
| 59 | |
| 60 | /** |
| 61 | * Dynamic rendering of the block. |
| 62 | * |
| 63 | * @param array $attributes Array containing the block attributes. |
| 64 | * |
| 65 | * @return string |
| 66 | */ |
| 67 | function render_block( $attributes ) { |
| 68 | // Tell Jetpack to load the assets registered via jetpack_register_block. |
| 69 | Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME ); |
| 70 | |
| 71 | return jetpack_rating_meta_render_block( $attributes ); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Older versions of AMP (0.6.2) are unable to render the markup, so we hide it |
| 76 | * Newer versions of AMP (1.4.1+) seem OK, but need the screen-reader text hidden |
| 77 | */ |
| 78 | function amp_add_inline_css() { |
| 79 | if ( defined( 'AMP__VERSION' ) && version_compare( AMP__VERSION, '1.4.1', '>=' ) ) { |
| 80 | echo '.wp-block-jetpack-rating-star span.screen-reader-text { border: 0; clip: rect(1px, 1px, 1px, 1px); clip-path: inset(50%); height: 1px; margin: -1px; overflow: hidden; padding: 0; position: absolute; width: 1px; word-wrap: normal; }'; |
| 81 | } else { |
| 82 | echo '.wp-block-jetpack-rating-star span:not([aria-hidden="true"]) { display: none; }'; |
| 83 | } |
| 84 | } |
| 85 | add_action( 'amp_post_template_css', __NAMESPACE__ . '\amp_add_inline_css', 11 ); |
| 86 |