GravityView  2.17
The best, easiest way to display Gravity Forms entries on your website.
class-gv-gutenberg-blocks.php
Go to the documentation of this file.
1 <?php
2 
4 
6 use GVCommon;
7 
8 class Blocks {
9  const MIN_WP_VERSION = '6.0.0';
10 
11  const SLUG = 'gk-gravityview-blocks';
12 
14 
15  public function __construct() {
16  global $wp_version;
17 
18  $this->blocks_build_path = str_replace( GRAVITYVIEW_DIR, '', __DIR__ ) . '/build';
19 
20  if ( version_compare( $wp_version, self::MIN_WP_VERSION, '<' ) ) {
21  return;
22  }
23 
24  add_filter( 'block_categories_all', [ $this, 'add_block_category' ] );
25 
26  add_filter( 'enqueue_block_assets', [ $this, 'localize_block_assets' ] );
27 
28  add_action( 'init', [ $this, 'register_blocks' ] );
29  }
30 
31  /**
32  * Registers block renderers.
33  *
34  * @since $ver$
35  *
36  * @return void
37  */
38  public function register_blocks() {
39  foreach ( glob( plugin_dir_path( __FILE__ ) . 'blocks/*' ) as $block_folder ) {
40  $block_meta_file = $block_folder . '/block.json';
41  $block_file = $block_folder . '/block.php';
42 
43  if ( ! file_exists( $block_meta_file ) ) {
44  continue;
45  }
46 
47  $block_meta = json_decode( file_get_contents( $block_meta_file ), true );
48 
49  $block_name = Arr::get( $block_meta, 'name' );
50 
51  if ( file_exists( $block_file ) ) {
52  $declared_classes = get_declared_classes();
53 
54  require_once $block_file;
55 
56  $block_class = array_values( array_diff( get_declared_classes(), $declared_classes ) );
57 
58  if ( ! empty( $block_class ) ) {
59  $block_class = new $block_class[0]();
60 
61  if ( is_callable( [ $block_class, 'modify_block_meta' ] ) ) {
62  $block_meta = array_merge( $block_meta, $block_class->modify_block_meta( $block_meta ) );
63  }
64  }
65 
66  $localization_data = Arr::get( $block_meta, 'localization' );
67 
68  if ( $localization_data ) {
69  add_filter( 'gk/gravityview/gutenberg/blocks/localization', function ( $localization ) use ( $block_name, $localization_data ) {
70  $localization[ $block_name ] = $localization_data;
71 
72  return $localization;
73  } );
74  }
75  }
76 
77  // Assets can be specified in the block.json file, but their paths must be relative to that file location.
78  // We store all build assets in ./build, and while we can use "file:../../build/filename.js' in the block.json,
79  // the MD5 hash will not match the translation file from translations.pot. Manually enqueuing assets fixes this.
80  $editor_script_handle = generate_block_asset_handle( $block_name, 'editorScript' );
81  $editor_script = sprintf( '%s/%s.js', $this->blocks_build_path, basename( $block_folder ) );
82 
83  $editor_style_handle = generate_block_asset_handle( $block_name, 'editorStyle' );
84  $editor_style = sprintf( '%s/%s.css', $this->blocks_build_path, basename( $block_folder ) );
85 
86  $global_style_handle = generate_block_asset_handle( $block_name, 'style' );
87  $global_style = sprintf( '%s/style-%s.css', $this->blocks_build_path, basename( $block_folder ) );
88 
89  if ( file_exists( GRAVITYVIEW_DIR . $editor_script ) ) {
90  wp_register_script(
91  $editor_script_handle,
92  plugins_url( $editor_script, GRAVITYVIEW_FILE ),
93  [ 'wp-editor', 'wp-element' ],
94  filemtime( GRAVITYVIEW_DIR . $editor_script ),
95  true
96  );
97 
98  add_action( 'enqueue_block_editor_assets', function () use ( $editor_script_handle ) {
99  wp_enqueue_script( $editor_script_handle );
100 
101  wp_set_script_translations( $editor_script_handle, 'gk-gravityview' );
102  } );
103  }
104 
105  if ( file_exists( GRAVITYVIEW_DIR . $editor_style ) ) {
106  wp_register_style(
107  $editor_style_handle,
108  plugins_url( $editor_style, GRAVITYVIEW_FILE ),
109  [],
110  filemtime( GRAVITYVIEW_DIR . $editor_style )
111  );
112 
113  add_action( 'enqueue_block_editor_assets', function () use ( $editor_style_handle ) {
114  wp_enqueue_style( $editor_style_handle );
115  } );
116  }
117 
118  if ( file_exists( GRAVITYVIEW_DIR . $global_style ) ) {
119  wp_register_style(
120  $global_style_handle,
121  plugins_url( $global_style, GRAVITYVIEW_FILE ),
122  [],
123  filemtime( GRAVITYVIEW_DIR . $global_style )
124  );
125 
126  add_action( 'enqueue_block_editor_assets', function () use ( $global_style_handle) {
127  wp_enqueue_style( $global_style_handle );
128  } );
129  }
130 
131  register_block_type_from_metadata( $block_meta_file, $block_meta );
132  }
133  }
134 
135  /**
136  * Adds GravityView category to Gutenberg editor.
137  *
138  * @since $ver$
139  *
140  * @param array $categories
141  *
142  * @return array
143  */
144  public function add_block_category( $categories ) {
145  return array_merge(
146  $categories,
147  [
148  [ 'slug' => self::SLUG, 'title' => __( 'GravityView', 'gk-gravityview' ) ],
149  ]
150  );
151  }
152 
153  /**
154  * Localizes shared block assets that's made available to all blocks via the global window.gkGravityKitBlocks object.
155  *
156  * @since $ver$
157  *
158  * @return void
159  */
160  public function localize_block_assets() {
161  /**
162  * @filter `gk/gravityview/gutenberg/blocks/localization` Modifies the global blocks localization data.
163  *
164  * @since $ver$
165  *
166  * @param array $block_localization_data
167  */
168  $block_localization_data = apply_filters( 'gk/gravityview/gutenberg/blocks/localization', [
169  'home_page' => home_url(),
170  'ajax_url' => admin_url( 'admin-ajax.php' ),
171  'create_new_view_url' => gravityview()->plugin->get_link_to_new_view(),
172  'edit_view_url' => add_query_arg(
173  [ 'action' => 'edit', 'post' => '%s' ],
174  admin_url( 'post.php' )
175  ),
176  'views' => $this->get_views()
177  ] );
178 
179  wp_register_script( self::SLUG, false, [] );
180 
181  wp_enqueue_script( self::SLUG );
182 
183  wp_localize_script(
184  self::SLUG,
185  'gkGravityViewBlocks',
186  $block_localization_data
187  );
188  }
189 
190  /**
191  * Returns the list of views for the block editor.
192  *
193  * @since $ver$
194  *
195  * @return array|array[]
196  */
197  public function get_views() {
198  $views = GVCommon::get_all_views( [
199  'orderby' => 'post_title',
200  'order' => 'ASC',
201  ] );
202 
203  $formatted_views = array_map( function ( $view ) {
204  return [
205  'value' => (string) $view->ID,
206  'label' => sprintf(
207  '%s (#%s)',
208  $view->post_title ?: esc_html__( 'View', 'gk-gravityview' ),
209  $view->ID
210  )
211  ];
212  }, $views );
213 
214  /**
215  * @filter `gk/gravityview/gutenberg/blocks/views` Modifies the Views object used in the UI.
216  *
217  * @since $ver$
218  *
219  * @param array $formatted_views
220  */
221  $formatted_views = apply_filters( 'gk/gravityview/gutenberg/blocks/views', $formatted_views );
222 
223  return $formatted_views;
224  }
225 
226  /**
227  * Renders shortcode and returns rendered content along with newly enqueued scripts and styles.
228  *
229  * @since $ver$
230  *
231  * @param string $shortcode
232  *
233  * @return array{content: string, scripts: array, styles: array}
234  */
235  static function render_shortcode( $shortcode ) {
236  global $wp_scripts, $wp_styles;
237 
238  $scripts_before_shortcode = array_keys( $wp_scripts->registered );
239  $styles_before_shortcode = array_keys( $wp_styles->registered );
240 
241  $rendered_shortcode = do_shortcode( $shortcode );
242 
243  do_action( 'wp_enqueue_scripts' );
244 
245  $gravityview_frontend = \GravityView_frontend::getInstance();
246  $gravityview_frontend->setGvOutputData( \GravityView_View_Data::getInstance( $shortcode ) );
247  $gravityview_frontend->add_scripts_and_styles();
248 
249  $scripts_after_shortcode = array_keys( $wp_scripts->registered );
250  $styles_after_shortcode = array_keys( $wp_styles->registered );
251 
252  $newly_registered_scripts = array_diff( $scripts_after_shortcode, $scripts_before_shortcode );
253  $newly_registered_styles = array_diff( $styles_after_shortcode, $styles_before_shortcode );
254 
255  // This will return an array of all dependencies sorted in the order they should be loaded.
256  $get_dependencies = function ( $handle, $source, $dependencies = [] ) use ( &$get_dependencies ) {
257  if ( empty( $source->registered[ $handle ] ) ) {
258  return $dependencies;
259  }
260 
261  if ( $source->registered[ $handle ]->extra && ! empty( $source->registered[ $handle ]->extra['data'] ) ) {
262  array_unshift( $dependencies, array_filter( [ 'src' => $source->registered[ $handle ]->src, 'data' => $source->registered[ $handle ]->extra['data'] ] ) );
263  } else if ( $source->registered[ $handle ]->src ) {
264  array_unshift( $dependencies, $source->registered[ $handle ]->src );
265  }
266 
267  if ( ! $source->registered[ $handle ]->deps ) {
268  return $dependencies;
269  }
270 
271  foreach ( $source->registered[ $handle ]->deps as $dependency ) {
272  array_unshift( $dependencies, $get_dependencies( $dependency, $source ) );
273  }
274 
275  return array_flatten( $dependencies );
276  };
277 
278  $script_dependencies = [];
279  foreach ( $newly_registered_scripts as $script ) {
280  $script_dependencies = array_merge( $script_dependencies, $get_dependencies( $script, $wp_scripts ) );
281  }
282 
283  $style_dependencies = [];
284  foreach ( $newly_registered_styles as $style ) {
285  $style_dependencies = array_merge( $style_dependencies, $get_dependencies( $style, $wp_styles ) );
286  }
287 
288  return [
289  'scripts' => array_unique( $script_dependencies, SORT_REGULAR ),
290  'styles' => array_unique( $style_dependencies, SORT_REGULAR ),
291  'content' => $rendered_shortcode,
292  ];
293  }
294 }
295 
296 new Blocks();
const GRAVITYVIEW_DIR
"GRAVITYVIEW_DIR" "./" The absolute path to the plugin directory, with trailing slash ...
Definition: gravityview.php:49
static getInstance( $passed_post=NULL)
Definition: class-data.php:122
static get( $array, $key, $default=null)
{}
Definition: Arr.php:99
add_block_category( $categories)
Adds GravityView category to Gutenberg editor.
get_views()
Returns the list of views for the block editor.
const GRAVITYVIEW_FILE
Full path to the GravityView file "GRAVITYVIEW_FILE" "./gravityview.php".
Definition: gravityview.php:40
localize_block_assets()
Localizes shared block assets that&#39;s made available to all blocks via the global window.gkGravityKitBlocks object.
gravityview()
The main GravityView wrapper function.
static render_shortcode( $shortcode)
Renders shortcode and returns rendered content along with newly enqueued scripts and styles...
static get_all_views( $args=array())
Get all existing Views.
static getInstance()
Get the one true instantiated self.