Guest User

Untitled

a guest
Dec 18th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.18 KB | None | 0 0
  1. <?php
  2.  
  3. function create_block($namespace, $name, $params, $render_callback)
  4. {
  5. static $config = null;
  6.  
  7. if (!isset($config) && is_admin()) {
  8. $config = [];
  9.  
  10. add_action(
  11. 'init',
  12. function () use ($namespace, &$config) {
  13. wp_add_inline_script('wp-editor', 'loadGutenbergShortcodeBlocks();');
  14. add_action(
  15. 'admin_footer',
  16. function () use ($namespace, &$config) {
  17. ?>
  18. <script>
  19. function loadGutenbergShortcodeBlocks() {
  20. var wp = window.wp;
  21. var registerBlockType = wp.blocks.registerBlockType;
  22. var el = wp.element.createElement;
  23. var __ = wp.i18n.__;
  24.  
  25. function clone(obj) {
  26. if (null == obj || "object" != typeof obj) return obj;
  27. var copy = obj.constructor();
  28. for (var attr in obj) {
  29. if (obj.hasOwnProperty(attr)) copy[attr] = obj[attr];
  30. }
  31. return copy;
  32. }
  33.  
  34. var config = <?= json_encode($config) ?>;
  35.  
  36. for (var blockName in config) {
  37. if (!config.hasOwnProperty(blockName)) {
  38. continue;
  39. }
  40. var blockConfig = config[blockName];
  41.  
  42. var generateController = function (blockConfig) {
  43. return function (props) {
  44. var content = [el('h5', {}, blockConfig.title)];
  45.  
  46. for (var attributeName in blockConfig.attributes) {
  47. if (!blockConfig.attributes.hasOwnProperty(attributeName)) {
  48. continue;
  49. }
  50. var attrConfig = blockConfig.attributes[attributeName];
  51.  
  52. if (attrConfig.options) {
  53. var editor = el(wp.components.SelectControl, {
  54. value: props.attributes[attributeName],
  55. options: attrConfig.options,
  56. onChange: function (content) {
  57. props.setAttributes({[attributeName]: content});
  58. },
  59. });
  60. } else {
  61. var editor = el(wp.editor.PlainText, {
  62. value: props.attributes[attributeName],
  63. placeholder: attrConfig.label,
  64. required: true,
  65. onChange: function (content) {
  66. props.setAttributes({[attributeName]: content});
  67. },
  68. });
  69. }
  70.  
  71. content.push(el('label', {}, attrConfig.label, editor));
  72. }
  73.  
  74. return el('div', {style: {backgroundColor: '#f8f9f9', padding: '14px'}}, content);
  75. }
  76. };
  77.  
  78. registerBlockType(<?= json_encode($namespace) ?> +'/' + blockName, {
  79. title: blockConfig.title,
  80. category: <?= json_encode($namespace) ?>,
  81. icon: blockConfig.icon,
  82. attributes: clone(blockConfig.attributes),
  83. edit: generateController(blockConfig),
  84. save: function () {
  85. return null;
  86. }
  87. });
  88. }
  89.  
  90.  
  91. };
  92. </script>
  93. <?php
  94. },
  95. 9999
  96. );
  97.  
  98. // Register block category
  99. add_filter(
  100. 'block_categories',
  101. function ($categories, $post) use ($namespace) {
  102. if ($post->post_type !== 'post') {
  103. return $categories;
  104. }
  105. return array_merge(
  106. $categories,
  107. [
  108. [
  109. 'slug' => $namespace,
  110. 'title' => $namespace
  111. ],
  112. ]
  113. );
  114. },
  115. 10,
  116. 2
  117. );
  118. }
  119. );
  120.  
  121. }
  122.  
  123. $config[$name] = $params;
  124.  
  125. $render = function ($block_params) use ($render_callback, $params) {
  126. // Set default parameters
  127. foreach ($params['attributes'] as $k => $param_info) {
  128. if (!array_key_exists($k, $block_params)) {
  129. $block_params[$k] = $param_info['default'];
  130. }
  131. }
  132. return call_user_func($render_callback, $block_params);
  133. };
  134.  
  135. // Register shortcode
  136. add_shortcode($name, $render);
  137.  
  138. // Register Gutenberg block
  139. if (function_exists('register_block_type')) {
  140. add_action(
  141. 'init',
  142. function () use ($namespace, $name, $render) {
  143. register_block_type(
  144. "{$namespace}/{$name}",
  145. [
  146. 'attributes' => [
  147. 'match' => ['type' => 'string'],
  148. 'side' => ['type' => 'string']
  149. ],
  150. 'render_callback' => $render
  151. ]
  152. );
  153. }
  154. );
  155. }
  156. }
  157.  
  158. // Usage
  159. create_block(
  160. 'mycompany',
  161. 'hello-word',
  162. [
  163. 'title' => __('Hello world'),
  164. 'icon' => 'groups',
  165. 'attributes' => [
  166. 'name' => [
  167. 'label' => __('Name'),
  168. 'type' => 'string',
  169. 'default' => 'World'
  170. ]
  171. ]
  172. ],
  173. function ($params) {
  174. return "Hello " . $params['name'];
  175. }
  176. );
Add Comment
Please, Sign In to add comment