Нет описания

BlockEditor.php 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /*******************************************************************************
  3. * Copyright (c) 2019, Code Atlantic LLC
  4. ******************************************************************************/
  5. /**
  6. * Class PUM_Admin_BlockEditor
  7. *
  8. * @since 1.10.0
  9. */
  10. class PUM_Admin_BlockEditor {
  11. public static $version = '1.0.0';
  12. /**
  13. * Initialize
  14. */
  15. public static function init() {
  16. // Bail early if the Block Playground is active and ahead of core.
  17. if ( defined( 'PUM_BLOCK_PLAYGROUND' ) && version_compare( PUM_BLOCK_PLAYGROUND, self::$version, '>' ) ) {
  18. return;
  19. }
  20. // TODO Test if this is needed in core or not.
  21. add_action( 'enqueue_block_editor_assets', [ 'PUM_Site_Assets', 'register_styles' ] );
  22. add_action( 'enqueue_block_editor_assets', [ __CLASS__, 'register_editor_assets' ] );
  23. add_action( 'wp_loaded', [ __CLASS__, 'add_attributes_to_registered_blocks' ], 999 );
  24. // Here for future use.
  25. // add_action( 'enqueue_block_assets', [ __CLASS__, 'register_block_assets' ] );
  26. }
  27. /**
  28. * Registers all block assets so that they can be enqueued through Gutenberg in
  29. * the corresponding context.
  30. *
  31. * Passes translations to JavaScript.
  32. *
  33. * @since 1.10.0
  34. */
  35. public static function register_editor_assets() {
  36. $build_path = 'dist/block-editor/';
  37. $script_path = $build_path . 'block-editor.js';
  38. $script_asset_path = $build_path . 'block-editor.asset.php';
  39. $script_asset = file_exists( Popup_Maker::$DIR . $script_asset_path ) ? require Popup_Maker::$DIR . $script_asset_path : [
  40. 'dependencies' => [],
  41. 'version' => Popup_Maker::$VER,
  42. ];
  43. $script_url = plugins_url( $script_path, Popup_Maker::$FILE );
  44. wp_enqueue_script( 'popup-maker-block-editor', $script_url, array_merge( $script_asset['dependencies'], [ 'wp-edit-post' ] ), $script_asset['version'] );
  45. wp_localize_script(
  46. 'popup-maker-block-editor',
  47. 'pum_block_editor_vars',
  48. [
  49. 'popups' => pum_get_all_popups(),
  50. 'popup_trigger_excluded_blocks' => apply_filters(
  51. 'pum_block_editor_popup_trigger_excluded_blocks',
  52. [
  53. 'core/nextpage',
  54. ]
  55. ),
  56. ]
  57. );
  58. $editor_styles_path = $build_path . 'block-editor-styles.css';
  59. $editor_styles_asset_path = $build_path . 'block-editor-styles.asset.php';
  60. $editor_styles_asset = file_exists( Popup_Maker::$DIR . $editor_styles_asset_path ) ? require Popup_Maker::$DIR . $editor_styles_asset_path : [
  61. 'dependencies' => [],
  62. 'version' => Popup_Maker::$VER,
  63. ];
  64. wp_enqueue_style( 'popup-maker-block-editor', plugins_url( $editor_styles_path, Popup_Maker::$FILE ), [], $editor_styles_asset['version'] );
  65. if ( function_exists( 'wp_set_script_translations' ) ) {
  66. /**
  67. * May be extended to wp_set_script_translations( 'my-handle', 'my-domain',
  68. * plugin_dir_path( MY_PLUGIN ) . 'languages' ) ). For details see
  69. * https://make.wordpress.org/core/2018/11/09/new-javascript-i18n-support-in-wordpress/
  70. */
  71. wp_set_script_translations( 'popup-maker-block-editor', 'popup-maker' );
  72. }
  73. }
  74. /**
  75. * Register assets for individual block styles
  76. */
  77. public static function register_block_assets() {
  78. $build_path = 'dist/block-editor/';
  79. $block_styles_path = $build_path . 'block-styles.css';
  80. $block_styles_asset_path = $build_path . 'block-styles.asset.php';
  81. $block_styles_asset = file_exists( Popup_Maker::$DIR . $block_styles_asset_path ) ? require Popup_Maker::$DIR . $block_styles_asset_path : [
  82. 'dependencies' => [],
  83. 'version' => Popup_Maker::$VER,
  84. ];
  85. wp_enqueue_style( 'popup-maker-block-styles', plugins_url( $block_styles_path, Popup_Maker::$FILE ), [], $block_styles_asset['version'] );
  86. }
  87. /**
  88. * This is needed to resolve an issue with blocks that use the
  89. * ServerSideRender component. Registering the attributes only in js
  90. * can cause an error message to appear. Registering the attributes in
  91. * PHP as well, seems to resolve the issue. Ideally, this bug will be
  92. * fixed in the future.
  93. *
  94. * Reference: https://github.com/WordPress/gutenberg/issues/16850
  95. *
  96. * @since 1.16.0
  97. */
  98. public static function add_attributes_to_registered_blocks() {
  99. global $wp_version;
  100. if ( version_compare( $wp_version, '5.0' ) === -1 ) {
  101. return;
  102. }
  103. $registered_blocks = WP_Block_Type_Registry::get_instance()->get_all_registered();
  104. foreach ( $registered_blocks as $block ) {
  105. $block->attributes['openPopupId'] = [
  106. 'type' => 'string',
  107. 'default' => '',
  108. ];
  109. }
  110. }
  111. }