-
Notifications
You must be signed in to change notification settings - Fork 0
/
mvx-block.php
240 lines (215 loc) · 7.35 KB
/
mvx-block.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<?php
/**
* Plugin Name: Mvx Block
* Description: Example block scaffolded with Create Block tool.
* Requires at least: 6.1
* Requires PHP: 7.0
* Version: 0.1.0
* Author: The WordPress Contributors
* License: GPL-2.0-or-later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: mvx-block
*
* @package create-block
*/
/**
* Registers the block using the metadata loaded from the `block.json` file.
* Behind the scenes, it registers also all assets so they can be enqueued
* through the block editor in the corresponding context.
*
* @see https://developer.wordpress.org/reference/functions/register_block_type/
*/
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;
$minimum_wp_version = '6.0';
if ( version_compare( $GLOBALS['wp_version'], $minimum_wp_version, '<' ) ) {
/**
* Outputs for an admin notice about running MVX Blocks on outdated WordPress.
*
* @since 1.0.0
*/
function mvx_blocks_admin_unsupported_wp_notice() {
?>
<div class="notice notice-error is-dismissible">
<p><?php esc_html_e( 'MVX Blocks requires a more recent version of WordPress and has been paused. Please update WordPress to continue enjoying WooCommerce Blocks.', 'multivendorx' ); ?></p>
</div>
<?php
}
add_action( 'admin_notices', 'mvx_blocks_admin_unsupported_wp_notice' );
return;
}
/**
* Load MVX Blocks package class.
*
*/
class MVX_Block {
/**
* The single instance of the class.
*
* @var object
*/
protected static $instance = null;
/**
* Constructor
*
* @return void
*/
protected function __construct() {}
/**
* Get class instance.
*
* @return object Instance.
*/
final public static function instance() {
if ( null === static::$instance ) {
static::$instance = new static();
}
return static::$instance;
}
/**
* Init the plugin.
*/
public function init() {
if ( ! $this->has_dependencies() ) {
return;
}
$this->define_constants();
add_action( 'plugins_loaded', array( $this, 'on_plugins_loaded' ) );
}
/**
* Check dependencies exist.
*
* @return boolean
*/
protected function has_dependencies() {
return class_exists( 'MVX' ) && function_exists( 'register_block_type' );
}
/**
* Setup plugin once all other plugins are loaded.
*
* @return void
*/
public function on_plugins_loaded() {
$this->includes();
add_action( 'enqueue_block_editor_assets', array( __CLASS__, 'register_blocks_assets' ) );
add_action( 'enqueue_block_assets', array( __CLASS__, 'enqueue_frontend_assets' ) );
add_filter( 'block_categories_all', array( __CLASS__, 'register_block_categories_all' ) );
add_action( 'init', array( __CLASS__, 'register_blocks' ) );
}
/**
* Register assets block categories.
*/
public static function register_block_categories_all( $categories ) {
return array_merge(
$categories,
array(
array(
'slug' => 'mvx',
'title' => __( 'MultiVendorX', 'multivendorx' ),
'icon' => 'wordpress',
),
)
);
}
/**
* Register assets as needed.
*/
public static function register_blocks_assets() {
// Register block styles for both frontend + backend.
wp_register_style(
'mvx_blocks-style-css',
MVXB_PLUGIN_URL . 'build/style-index.css',
is_admin() ? array( 'wp-editor' ) : null,
filemtime( MVXB_PLUGIN_PATH . '/build/style-index.css' )
);
// Register block editor script for backend.
wp_register_script(
'mvx_blocks-scripts-js',
MVXB_PLUGIN_URL . 'build/index.js',
array( 'wp-blocks', 'wp-i18n', 'wp-element'/*, 'wp-editor'*/, 'wp-components' ),
filemtime( MVXB_PLUGIN_PATH . '/build/index.js' ),
true
);
// Register block editor styles for backend.
wp_register_style(
'mvx_blocks-editor-css',
MVXB_PLUGIN_URL . 'build/index.css',
array( 'wp-edit-blocks' ),
filemtime( MVXB_PLUGIN_PATH . '/build/index.css' )
);
// WP Localized globals
$params = apply_filters( 'mvx_blocks_scripts_data_params',
array(
'pluginDirPath' => MVXB_PLUGIN_PATH,
'pluginDirUrl' => MVXB_PLUGIN_URL,
'allVendors' => mvx_vendor_list_item(),
'recapta' => array(
array(
'key' => 'v2',
'title' => __( 'reCAPTCHA v2', 'multivendorx' ),
),
array(
'key' => 'v3',
'title' => __( 'reCAPTCHA v3', 'multivendorx' ),
),
)
) );
wp_localize_script( 'mvx_blocks-scripts-js', 'mvx_blocks_scripts_data_params', $params );
}
/**
* Enqueue assets for frontend.
*/
public static function enqueue_frontend_assets() {
if ( !wp_script_is( 'mvx_blocks-style-css', 'registered' ) ) {
wp_register_style(
'mvx_blocks-style-css',
MVXB_PLUGIN_URL . 'build/style-index.css',
is_admin() ? array( 'wp-editor' ) : null,
filemtime( MVXB_PLUGIN_PATH . '/build/style-index.css' )
);
}
wp_enqueue_style( 'mvx_blocks-style-css' );
}
/**
* Register blocks, hooking up assets and render functions as needed.
*/
public static function register_blocks() {
$blocks = [
'TopRatedVendors',
'VendorTopProducts',
'VendorsInfo',
'VendorCoupons',
'VendorLocation',
'VendorOnSellProducts',
'VendorPolicies',
'VendorsReview',
'VendorsContact',
'VendorRecentProducts',
'VendorProductsSearch',
'VendorProductCategories',
'VendorLists'
];
foreach ( $blocks as $class ) {
require_once 'src/blocks/' . $class . '/' . $class . '.php';
$instance = new $class();
$instance->register_block_type();
}
}
/**
* Define Constants.
*/
protected function define_constants() {
if ( ! defined( 'MVXB_ABSPATH' ) ) define( 'MVXB_ABSPATH', dirname( __FILE__ ) . '/' );
if ( ! defined( 'MVXB_PLUGIN_URL' ) ) define( 'MVXB_PLUGIN_URL', plugin_dir_url(__FILE__) );
if ( ! defined( 'MVXB_PLUGIN_PATH' ) ) define( 'MVXB_PLUGIN_PATH', untrailingslashit( plugin_dir_path(__FILE__) ) );
if ( ! defined( 'MVXB_VERSION' ) ) define( 'MVXB_VERSION', '0.0.1' );
}
/**
* includes files.
*/
protected function includes() {
include_once MVXB_ABSPATH . 'inc/functions.php';
include_once MVXB_ABSPATH . 'src/blocks/AbstractBlock.php';
}
}
MVX_Block::instance()->init();