Нема описа

rating-star.php 2.2KB

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