Guest User

Untitled

a guest
Jan 21st, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. <?php
  2.  
  3. namespace GutenbergBlockManager;
  4.  
  5. class Blocks
  6. {
  7. public $url;
  8. public $dir;
  9. public $namespace;
  10.  
  11. /**
  12. * Set up block editor basics on instantiation
  13. */
  14. public function __construct($namespace, $url, $dir)
  15. {
  16. $this->url = $url;
  17. $this->namespace = $namespace;
  18. $this->dir = $dir;
  19.  
  20. $this->registerEditorBlocks();
  21. $this->registerFrontendBlocks();
  22. $this->registerEditorScripts();
  23. }
  24.  
  25. /**
  26. * Glob editor blocks
  27. */
  28. public function registerEditorBlocks()
  29. {
  30. foreach (glob($this->dir .'/resources/assets/scripts/blocks/*.js') as $blockname) {
  31. $this->registerEditorBlock(basename($blockname, '.js'));
  32. }
  33. }
  34.  
  35. /**
  36. * Glob frontend blocks
  37. */
  38. public function registerFrontendBlocks()
  39. {
  40. foreach (glob($this->dir .'/resources/assets/scripts/frontend/*.js') as $blockname) {
  41. $this->registerFrontendBlock(basename($blockname, '.js'));
  42. }
  43. }
  44.  
  45. /**
  46. * Register block
  47. */
  48. public function registerEditorBlock($name)
  49. {
  50. add_action('init', function () use ($name) {
  51. register_block_type($this->namespace .'/'. $name, array(
  52. 'editor_script' => $this->namespace .'-editor-js',
  53. 'editor_style' => $this->namespace .'-editor-css'
  54. ));
  55. });
  56. }
  57.  
  58. /**
  59. * Enqueue block editor scripts
  60. */
  61. public function registerEditorScripts()
  62. {
  63. add_action('init', function () {
  64. wp_register_style(
  65. $this->namespace .'-editor-css',
  66. $this->url .'/dist/styles/main.css'
  67. );
  68.  
  69. wp_register_script(
  70. $this->namespace .'-editor-js',
  71. $this->url .'/dist/scripts/main.js',
  72. array(
  73. 'wp-blocks',
  74. 'wp-components',
  75. 'wp-compose',
  76. 'wp-data',
  77. 'wp-date',
  78. 'wp-editor',
  79. 'wp-element',
  80. 'wp-hooks',
  81. 'wp-i18n',
  82. 'wp-plugins',
  83. )
  84. );
  85. });
  86. }
  87.  
  88. /**
  89. * Enqueue public assets
  90. */
  91. public function registerFrontendBlock($name)
  92. {
  93. add_action('wp_enqueue_scripts', function () use ($name) {
  94. wp_enqueue_script(
  95. 'wp-block-'. $name .'-public-js',
  96. $this->url .'/dist/scripts/frontend/'. $name .'.js',
  97. array(
  98. 'wp-blocks',
  99. 'wp-element'
  100. ),
  101. null,
  102. true
  103. );
  104.  
  105. wp_enqueue_style(
  106. 'wp-block-'. $name .'-public-css',
  107. $this->url .'/dist/styles/frontend/'. $name .'.css',
  108. ['wp-block'. $name .'-public-js']
  109. );
  110. });
  111. }
  112. }
Add Comment
Please, Sign In to add comment