11 const SLUG =
'gk-gravityview-blocks';
18 $this->blocks_build_path = str_replace(
GRAVITYVIEW_DIR,
'', __DIR__ ) .
'/build';
20 if ( version_compare( $wp_version, self::MIN_WP_VERSION,
'<' ) ) {
24 add_filter(
'block_categories_all', [ $this,
'add_block_category' ] );
26 add_filter(
'enqueue_block_assets', [ $this,
'localize_block_assets' ] );
28 add_action(
'init', [ $this,
'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';
43 if ( ! file_exists( $block_meta_file ) ) {
47 $block_meta = json_decode( file_get_contents( $block_meta_file ),
true );
49 $block_name =
Arr::get( $block_meta,
'name' );
51 if ( file_exists( $block_file ) ) {
52 $declared_classes = get_declared_classes();
54 require_once $block_file;
56 $block_class = array_values( array_diff( get_declared_classes(), $declared_classes ) );
58 if ( ! empty( $block_class ) ) {
59 $block_class =
new $block_class[0]();
61 if ( is_callable( [ $block_class,
'modify_block_meta' ] ) ) {
62 $block_meta = array_merge( $block_meta, $block_class->modify_block_meta( $block_meta ) );
66 $localization_data =
Arr::get( $block_meta,
'localization' );
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;
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 ) );
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 ) );
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 ) );
91 $editor_script_handle,
93 [
'wp-editor',
'wp-element' ],
98 add_action(
'enqueue_block_editor_assets',
function () use ( $editor_script_handle ) {
99 wp_enqueue_script( $editor_script_handle );
101 wp_set_script_translations( $editor_script_handle,
'gk-gravityview' );
107 $editor_style_handle,
113 add_action(
'enqueue_block_editor_assets',
function () use ( $editor_style_handle ) {
114 wp_enqueue_style( $editor_style_handle );
120 $global_style_handle,
126 add_action(
'enqueue_block_editor_assets',
function () use ( $global_style_handle) {
127 wp_enqueue_style( $global_style_handle );
131 register_block_type_from_metadata( $block_meta_file, $block_meta );
148 [
'slug' => self::SLUG,
'title' => __(
'GravityView',
'gk-gravityview' ) ],
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' )
179 wp_register_script( self::SLUG,
false, [] );
181 wp_enqueue_script( self::SLUG );
185 'gkGravityViewBlocks',
186 $block_localization_data
199 'orderby' =>
'post_title',
203 $formatted_views = array_map(
function ( $view ) {
205 'value' => (string) $view->ID,
208 $view->post_title ?: esc_html__(
'View',
'gk-gravityview' ),
221 $formatted_views = apply_filters(
'gk/gravityview/gutenberg/blocks/views', $formatted_views );
223 return $formatted_views;
236 global $wp_scripts, $wp_styles;
238 $scripts_before_shortcode = array_keys( $wp_scripts->registered );
239 $styles_before_shortcode = array_keys( $wp_styles->registered );
241 $rendered_shortcode = do_shortcode( $shortcode );
243 do_action(
'wp_enqueue_scripts' );
247 $gravityview_frontend->add_scripts_and_styles();
249 $scripts_after_shortcode = array_keys( $wp_scripts->registered );
250 $styles_after_shortcode = array_keys( $wp_styles->registered );
252 $newly_registered_scripts = array_diff( $scripts_after_shortcode, $scripts_before_shortcode );
253 $newly_registered_styles = array_diff( $styles_after_shortcode, $styles_before_shortcode );
256 $get_dependencies =
function ( $handle, $source, $dependencies = [] ) use ( &$get_dependencies ) {
257 if ( empty( $source->registered[ $handle ] ) ) {
258 return $dependencies;
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 );
267 if ( ! $source->registered[ $handle ]->deps ) {
268 return $dependencies;
271 foreach ( $source->registered[ $handle ]->deps as $dependency ) {
272 array_unshift( $dependencies, $get_dependencies( $dependency, $source ) );
275 return array_flatten( $dependencies );
278 $script_dependencies = [];
279 foreach ( $newly_registered_scripts as $script ) {
280 $script_dependencies = array_merge( $script_dependencies, $get_dependencies( $script, $wp_scripts ) );
283 $style_dependencies = [];
284 foreach ( $newly_registered_styles as $style ) {
285 $style_dependencies = array_merge( $style_dependencies, $get_dependencies( $style, $wp_styles ) );
289 'scripts' => array_unique( $script_dependencies, SORT_REGULAR ),
290 'styles' => array_unique( $style_dependencies, SORT_REGULAR ),
291 'content' => $rendered_shortcode,
const GRAVITYVIEW_DIR
"GRAVITYVIEW_DIR" "./" The absolute path to the plugin directory, with trailing slash ...
static getInstance( $passed_post=NULL)
static get( $array, $key, $default=null)
{}
add_block_category( $categories)
Adds GravityView category to Gutenberg editor.
register_blocks()
Registers block renderers.
get_views()
Returns the list of views for the block editor.
const GRAVITYVIEW_FILE
Full path to the GravityView file "GRAVITYVIEW_FILE" "./gravityview.php".
localize_block_assets()
Localizes shared block assets that'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.