Completed
Pull Request — develop (#1588)
by Zack
18:19
created

Blocks::load_blocks()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 0
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
1
<?php
2
3
namespace GV\Gutenberg;
4
5
// Exit if accessed directly
6
if ( ! defined( 'ABSPATH' ) ) {
7
	exit;
8
}
9
10
/**
11
 * GravityView Gutenberg Blocks
12
 *
13
 * @since 2.10.2
14
 */
15
class Blocks {
16
	const ASSETS_HANDLE = 'gv-blocks';
17
	const MIN_WP_VERSION = '5.2';
18
19
	function __construct() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
20
		global $wp_version;
21
22
		if ( ! class_exists( 'GravityView_Plugin' ) ||
23
		     ! function_exists( 'register_block_type' ) ||
24
		     version_compare( $wp_version, self::MIN_WP_VERSION, '<' )
25
		) {
26
			return;
27
		}
28
29
		require_once( plugin_dir_path( __FILE__ ) . 'blocks/block.php' );
30
31
		add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_assets' ) );
32
		add_filter( 'block_categories', array( $this, 'add_block_category' ) );
33
34
		$this->load_blocks();
35
	}
36
37
	/**
38
	 * Register block renderers
39
	 *
40
	 * @since 2.10.2
41
	 *
42
	 * @return void
43
	 */
44
	public function load_blocks() {
45
		foreach ( glob( plugin_dir_path( __FILE__ ) . 'blocks/*/block.php' ) as $file ) {
46
			require_once( $file );
47
48
			$block_name  = basename( dirname( $file ) );
49
			$block_name  = explode( '-', $block_name );
50
			$block_name  = implode( '_', array_map( 'ucfirst', $block_name ) );
51
			$block_class = '\GV\Gutenberg\Blocks\Block\\' . $block_name;
52
53
			if ( ! is_callable( array( $block_class, 'render' ) ) ) {
54
				continue;
55
			}
56
57
			$block_class::register();
58
		}
59
	}
60
61
	/**
62
	 * Add GravityView category to Gutenberg editor
63
	 *
64
	 * @since 2.10.2
65
	 *
66
	 * @param array $categories
67
	 *
68
	 * @return array
69
	 */
70
	public function add_block_category( $categories ) {
71
		return array_merge(
72
			$categories,
73
			array(
74
				array( 'slug' => 'gravityview', 'title' => __( 'GravityView', 'gravityview' ) ),
75
			)
76
		);
77
	}
78
79
	/**
80
	 * Enqueue UI assets
81
	 *
82
	 * @since 2.10.2
83
	 *
84
	 * @return void
85
	 */
86
	public function enqueue_assets() {
87
		$script     = 'assets/js/gv-blocks.js';
88
		$style      = 'assets/css/gv-blocks.css';
89
		$asset_file = include( gravityview()->plugin->dir() . 'assets/js/gv-blocks.asset.php' );
90
91
		wp_enqueue_script(
92
			self::ASSETS_HANDLE,
93
			gravityview()->plugin->url() . $script,
94
			$asset_file['dependencies'],
95
			filemtime( gravityview()->plugin->dir() . $script )
96
		);
97
98
		wp_enqueue_style(
99
			self::ASSETS_HANDLE,
100
			gravityview()->plugin->url() . $style,
101
			array( 'wp-edit-blocks' ),
102
			filemtime( gravityview()->plugin->dir() . $style )
103
		);
104
105
		$views = \GVCommon::get_all_views(
106
			array(
107
				'orderby' => 'post_title',
108
				'order'   => 'ASC',
109
			)
110
		);
111
112
		$views_list_array = array_map( function ( $view ) {
113
114
			$post_title = empty( $view->post_title ) ? __('(no title)', 'gravityview') : $view->post_title;
115
			$post_title = esc_html( sprintf('%s #%d', $post_title, $view->ID ) );
116
117
			return array(
118
				'value' => $view->ID,
119
				'label' => $post_title,
120
			);
121
		}, $views );
122
123
		wp_localize_script(
124
			self::ASSETS_HANDLE,
125
			'GV_BLOCKS',
126
			array(
127
				'home_page' => home_url(),
128
				'ajax_url'  => admin_url( 'admin-ajax.php' ),
129
				'img_url'   => gravityview()->plugin->url( 'assets/images/' ),
130
				'view_list' => $views_list_array,
131
			)
132
		);
133
	}
134
}
135
136
new Blocks();
137