-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-ar-model-viewer-for-woocommerce.php
504 lines (433 loc) · 23.7 KB
/
class-ar-model-viewer-for-woocommerce.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
<?php
/**
* The file that defines the core plugin class
*
* A class definition that includes attributes and functions used across both the
* public-facing side of the site and the admin area.
*
* @link https://racmanuel.dev
* @since 1.0.0
*
* @package Ar_Model_Viewer_For_Woocommerce
* @subpackage Ar_Model_Viewer_For_Woocommerce/includes
*/
/**
* The core plugin class.
*
* This is used to define internationalization, admin-specific hooks, and
* public-facing site hooks.
*
* Also maintains the unique identifier of this plugin as well as the current
* version of the plugin.
*
* @since 1.0.0
* @package Ar_Model_Viewer_For_Woocommerce
* @subpackage Ar_Model_Viewer_For_Woocommerce/includes
* @author Manuel Ramirez Coronel <ra_cm@outlook.com>
*/
class Ar_Model_Viewer_For_Woocommerce
{
/**
* The loader that's responsible for maintaining and registering all hooks that power
* the plugin.
*
* @since 1.0.0
* @access protected
* @var Ar_Model_Viewer_For_Woocommerce_Loader $loader Maintains and registers all hooks for the plugin.
*/
protected $loader;
/**
* The unique identifier of this plugin.
*
* @since 1.0.0
* @access protected
* @var string $plugin_name The string used to uniquely identify this plugin.
*/
protected $plugin_name;
/**
* The unique prefix of this plugin.
*
* @since 1.0.0
* @access protected
* @var string $plugin_prefix The string used to uniquely prefix technical functions of this plugin.
*/
protected $plugin_prefix;
/**
* The current version of the plugin.
*
* @since 1.0.0
* @access protected
* @var string $version The current version of the plugin.
*/
protected $version;
/**
* Define the core functionality of the plugin.
*
* Set the plugin name and the plugin version that can be used throughout the plugin.
* Load the dependencies, define the locale, and set the hooks for the admin area and
* the public-facing side of the site.
*
* @since 1.0.0
*/
public function __construct()
{
if (defined('AR_MODEL_VIEWER_FOR_WOOCOMMERCE_VERSION')) {
$this->version = AR_MODEL_VIEWER_FOR_WOOCOMMERCE_VERSION;
} else {
$this->version = '1.0.0';
}
$this->plugin_name = 'ar-model-viewer-for-woocommerce';
$this->plugin_prefix = 'ar_model_viewer_for_woocommerce_';
$this->load_dependencies();
$this->set_locale();
$this->define_admin_hooks();
$this->define_public_hooks();
}
/**
* Load the required dependencies for this plugin.
*
* Include the following files that make up the plugin:
*
* - Ar_Model_Viewer_For_Woocommerce_Loader. Orchestrates the hooks of the plugin.
* - Ar_Model_Viewer_For_Woocommerce_i18n. Defines internationalization functionality.
* - Ar_Model_Viewer_For_Woocommerce_Admin. Defines all hooks for the admin area.
* - Ar_Model_Viewer_For_Woocommerce_Public. Defines all hooks for the public side of the site.
*
* Create an instance of the loader which will be used to register the hooks
* with WordPress.
*
* @since 1.0.0
* @access private
*/
private function load_dependencies()
{
/**
* The class responsible for orchestrating the actions and filters of the
* core plugin.
*/
require_once plugin_dir_path(dirname(__FILE__)) . 'includes/class-ar-model-viewer-for-woocommerce-loader.php';
/**
* The class responsible for defining internationalization functionality
* of the plugin.
*/
require_once plugin_dir_path(dirname(__FILE__)) . 'includes/class-ar-model-viewer-for-woocommerce-i18n.php';
/**
* The class responsible for defining internationalization functionality
* of the plugin.
*/
require_once plugin_dir_path(dirname(__FILE__)) . 'includes/class-ar-model-viewer-for-woocommerce-logger.php';
/**
* The class responsible for defining all actions that occur in the admin area.
*/
require_once plugin_dir_path(dirname(__FILE__)) . 'admin/class-ar-model-viewer-for-woocommerce-admin.php';
/**
* The class responsible for defining all actions that occur in the admin area.
*/
require_once plugin_dir_path(dirname(__FILE__)) . 'admin/class-ar-model-viewer-for-woocommerce-admin-product.php';
/**
* The class responsible for defining all actions that occur in the admin area.
*/
require_once plugin_dir_path(dirname(__FILE__)) . 'admin/class-ar-model-viewer-for-woocommerce-admin-settings.php';
/**
* The class responsible for defining all actions that occur in the public-facing
* side of the site.
*/
require_once plugin_dir_path(dirname(__FILE__)) . 'public/class-ar-model-viewer-for-woocommerce-public.php';
/**
* The class responsible for defining all actions that occur in the public-facing Shortcode
* side of the site.
*/
require_once plugin_dir_path(dirname(__FILE__)) . 'public/class-ar-model-viewer-for-woocommerce-public-shortcode.php';
/**
* The class responsible for defining all actions that occur in the public-facing Shortcode
* side of the site.
*/
require_once plugin_dir_path(dirname(__FILE__)) . 'public/class-ar-model-viewer-for-woocommerce-public-tab.php';
/**
* The class responsible for defining all actions that occur in the public-facing Shortcode
* side of the site.
*/
require_once plugin_dir_path(dirname(__FILE__)) . 'admin/class-meshy-api.php';
if (ar_model_viewer_for_woocommerce_fs()->is__premium_only()) {
/**
* Check if the user has access to premium features. This condition ensures that
* the following code block is executed only for users who have purchased the premium version.
*/
// Check if the user is on the 'pro' plan and has an active subscription.
if (ar_model_viewer_for_woocommerce_fs()->is_plan('pro', true) && ar_model_viewer_for_woocommerce_fs()->can_use_premium_code()) {
require_once plugin_dir_path(dirname(__FILE__)) . 'admin/class-ar-model-viewer-for-woocommerce-admin-pro.php';
}
}
$this->loader = new Ar_Model_Viewer_For_Woocommerce_Loader();
}
/**
* Define the locale for this plugin for internationalization.
*
* Uses the Ar_Model_Viewer_For_Woocommerce_i18n class in order to set the domain and to register the hook
* with WordPress.
*
* @since 1.0.0
* @access private
*/
private function set_locale()
{
$plugin_i18n = new Ar_Model_Viewer_For_Woocommerce_I18n();
$this->loader->add_action('plugins_loaded', $plugin_i18n, 'load_plugin_textdomain');
}
/**
* Register all of the hooks related to the admin area functionality
* of the plugin.
*
* @since 1.0.0
* @access private
*/
private function define_admin_hooks()
{
// Instantiate the admin class for the free version of the plugin.
$plugin_admin = new Ar_Model_Viewer_For_Woocommerce_Admin($this->get_plugin_name(), $this->get_plugin_prefix(), $this->get_version());
$plugin_admin_product = new Ar_Model_Viewer_For_Woocommerce_Admin_Product($this->get_plugin_name(), $this->get_plugin_prefix(), $this->get_version());
$plugin_admin_settings = new Ar_Model_Viewer_For_Woocommerce_Admin_Settings($this->get_plugin_name(), $this->get_plugin_prefix(), $this->version);
// Include the admin styles in the Admin dashboard.
$this->loader->add_action('admin_enqueue_scripts', $plugin_admin, 'enqueue_styles');
/**
* Enqueues the admin styles for the WordPress admin dashboard.
* The function `enqueue_styles` in `$plugin_admin` will include the necessary CSS files for the plugin.
*/
// Include the admin scripts in the Admin dashboard.
$this->loader->add_action('admin_enqueue_scripts', $plugin_admin, 'enqueue_scripts');
/**
* Enqueues the admin scripts for the WordPress admin dashboard.
* The function `enqueue_scripts` in `$plugin_admin` will include the necessary JavaScript files for the plugin.
*/
// Set the extension and mime type for Android (.glb) and iOS (.usdz) files.
$this->loader->add_filter('wp_check_filetype_and_ext', $plugin_admin, 'ar_model_viewer_for_woocommerce_file_and_ext', 10, 4);
/**
* Adds support for Android `.glb` and iOS `.usdz` file types by defining their extensions and mime types.
* The function `ar_model_viewer_for_woocommerce_file_and_ext` checks the file type during upload and processing.
* This ensures that the file types are correctly identified in WordPress.
* It hooks into the `wp_check_filetype_and_ext` filter, with a priority of 10 and passes 4 arguments.
*/
// Allow Android (.glb) and iOS (.usdz) files to be uploaded by adding them to the allowed MIME types.
$this->loader->add_filter('upload_mimes', $plugin_admin, 'ar_model_viewer_for_woocommerce_mime_types');
/**
* Adds `.glb` and `.usdz` to the list of allowed MIME types for file uploads.
* The function `ar_model_viewer_for_woocommerce_mime_types` ensures that these file types can be uploaded to the WordPress media library.
* It hooks into the `upload_mimes` filter.
*/
// Define and initialize custom metaboxes and field configurations.
$this->loader->add_action('cmb2_admin_init', $plugin_admin_product, 'ar_model_viewer_for_woocommerce_cmb2_metaboxes');
$this->loader->add_action('cmb2_admin_init', $plugin_admin_settings, 'ar_model_viewer_for_woocommerce_cmb2_settings');
/**
* Registers and configures custom metaboxes for the plugin using the CMB2 library.
* The function `ar_model_viewer_for_woocommerce_cmb2_metaboxes` sets up fields for products where users can input 3D model file URLs and other details.
* It hooks into the `cmb2_admin_init` action to ensure the fields are initialized when needed.
*/
// Get the currently active theme.
$theme_actual = wp_get_theme();
/**
* Retrieves the active WordPress theme.
* This is used to check if specific actions or fixes are needed for certain themes (e.g., Bloksy).
*/
if ($theme_actual->name === 'Blocksy') {
// Check if the active theme is Bloksy and apply necessary fixes.
$this->loader->add_filter('blocksy:woocommerce:product-view:use-default', $plugin_admin, 'ar_model_viewer_for_woocommerce_blocksy_fix');
/**
* Adds a filter to fix compatibility issues with the Bloksy theme.
* The function `ar_model_viewer_for_woocommerce_blocksy_fix` handles specific changes needed for the Bloksy theme’s WooCommerce product view.
* It hooks into the `blocksy:woocommerce:product-view:use-default` filter, ensuring the plugin works seamlessly with Bloksy.
*/
}
$this->loader->add_action('wp_ajax_ar_model_viewer_for_woocommerce_get_model_and_settings', $plugin_admin_product, 'ar_model_viewer_for_woocommerce_get_model_and_settings');
$this->loader->add_action('wp_ajax_ar_model_viewer_for_woocommerce_get_tasks', $plugin_admin_product, 'ar_model_viewer_for_woocommerce_get_tasks');
$this->loader->add_action('wp_ajax_ar_model_viewer_for_woocommerce_get_model_preview_with_global_settings', $plugin_admin_settings, 'ar_model_viewer_for_woocommerce_get_model_preview_with_global_settings');
$this->loader->add_action('wp_ajax_ar_model_viewer_for_woocommerce_createTextTo3DTaskPreview',$plugin_admin_product,'ar_model_viewer_for_woocommerce_createTextTo3DTaskPreview');
$this->loader->add_action('wp_ajax_ar_model_viewer_for_woocommerce_createTextTo3DTaskRefine', $plugin_admin_product, 'ar_model_viewer_for_woocommerce_createTextTo3DTaskRefine');
$this->loader->add_action('wp_ajax_ar_model_viewer_for_woocommerce_get_task_and_download', $plugin_admin_product, 'ar_model_viewer_for_woocommerce_get_task_and_download');
if (ar_model_viewer_for_woocommerce_fs()->is__premium_only()) {
/**
* Check if the user has access to premium features. This condition ensures that
* the following code block is executed only for users who have purchased the premium version.
*/
// Check if the user is on the 'pro' plan and has an active subscription.
if (ar_model_viewer_for_woocommerce_fs()->is_plan('pro', true) && ar_model_viewer_for_woocommerce_fs()->can_use_premium_code()) {
/**
* Check if the user is on the "Pro" plan and has an active subscription.
* `is_plan('pro', true)` checks if the user has access to the 'Pro' plan.
* `can_use_premium_code()` ensures that the user has an active subscription.
* If both conditions are met, the premium functionality is executed.
*/
// Instantiate the admin class for the pro version of the plugin.
$plugin_admin_pro = new Ar_Model_Viewer_For_Woocommerce_Admin_Pro($this->get_plugin_name(), $this->get_plugin_prefix(), $this->get_version());
//Register the CMB2 Fields to Pro Version
$this->loader->add_action('cmb2_admin_init', $plugin_admin_pro, 'ar_model_viewer_for_woocommerce_pro_metaboxes');
// Register the AR model viewer widget in Elementor.
$this->loader->add_action('elementor/widgets/register', $plugin_admin_pro, 'register_ar_model_viewer_widget');
/**
* This hook registers the AR model viewer widget with Elementor. It allows the
* premium widget to be used in Elementor’s page builder.
* The `register_ar_model_viewer_widget` function is called when Elementor widgets are registered.
*/
// Hooks for Importer
$this->loader->add_filter('woocommerce_csv_product_import_mapping_options', $plugin_admin_pro, 'ar_model_viewer_for_woocommerce_add_column_to_importer');
/**
* Adds a custom column for 3D model data to the WooCommerce product importer.
* The `ar_model_viewer_for_woocommerce_add_column_to_importer` function defines
* the custom column names that will be displayed when mapping import fields.
*/
$this->loader->add_filter('woocommerce_csv_product_import_mapping_default_columns', $plugin_admin_pro, 'ar_model_viewer_for_woocommerce_add_column_to_mapping_screen');
/**
* Automatically maps the custom columns for 3D model data when importing products.
* This hook connects the column names with their corresponding WooCommerce meta fields.
* The function `ar_model_viewer_for_woocommerce_add_column_to_mapping_screen` handles this mapping.
*/
$this->loader->add_filter('woocommerce_product_import_pre_insert_product_object', $plugin_admin_pro, 'ar_model_viewer_for_woocommerce_process_import', 10, 2);
/**
* Processes the imported data for each product and saves the custom 3D model meta fields.
* The `ar_model_viewer_for_woocommerce_process_import` function takes the product object
* and the imported data, and then updates the metadata with the values from the CSV.
* This is triggered before the product is inserted into the database.
*/
// Hooks for Exporter
$this->loader->add_filter('woocommerce_product_export_column_names', $plugin_admin_pro, 'ar_model_viewer_for_woocommerce_add_export_column');
/**
* Adds custom column names for exporting 3D model data in WooCommerce.
* The `ar_model_viewer_for_woocommerce_add_export_column` function adds the custom
* columns (like Android and iOS model file URLs) to the list of available export fields.
*/
$this->loader->add_filter('woocommerce_product_export_product_default_columns', $plugin_admin_pro, 'ar_model_viewer_for_woocommerce_add_export_column');
/**
* Adds the same custom column names for exporting 3D model data, ensuring they are part
* of the default export fields in WooCommerce.
* The `ar_model_viewer_for_woocommerce_add_export_column` function is reused to achieve this.
*/
// Hook for exporting the Android .glb URL column.
$this->loader->add_filter('woocommerce_product_export_product_column_ar_model_viewer_for_woocommerce_file_android', $plugin_admin_pro, 'ar_model_viewer_for_woocommerce_add_export_data_file_android', 10, 2);
/**
* Exports the Android `.glb` file URL from the product metadata during WooCommerce exports.
* The function `ar_model_viewer_for_woocommerce_add_export_data_file_android` retrieves
* the meta field for the Android file and formats it for export.
*/
// Hook for exporting the IOS .usdz URL column.
$this->loader->add_filter('woocommerce_product_export_product_column_ar_model_viewer_for_woocommerce_file_ios', $plugin_admin_pro, 'ar_model_viewer_for_woocommerce_add_export_data_file_ios', 10, 2);
/**
* Exports the iOS `.usdz` file URL from the product metadata during WooCommerce exports.
* The function `ar_model_viewer_for_woocommerce_add_export_data_file_ios` retrieves
* the meta field for the iOS file and formats it for export.
*/
// Hook for exporting the Poster for 3D Model column.
$this->loader->add_filter('woocommerce_product_export_product_column_ar_model_viewer_for_woocommerce_file_poster', $plugin_admin_pro, 'ar_model_viewer_for_woocommerce_add_export_data_file_poster', 10, 2);
/**
* Exports the poster image URL for the 3D model from the product metadata during WooCommerce exports.
* The function `ar_model_viewer_for_woocommerce_add_export_data_file_poster` retrieves
* the meta field for the poster image and formats it for export.
*/
// Hook for exporting the Alt for 3D Model column.
$this->loader->add_filter('woocommerce_product_export_product_column_ar_model_viewer_for_woocommerce_file_alt', $plugin_admin_pro, 'ar_model_viewer_for_woocommerce_add_export_data_file_alt', 10, 2);
/**
* Exports the alt text for the 3D model from the product metadata during WooCommerce exports.
* The function `ar_model_viewer_for_woocommerce_add_export_data_file_alt` retrieves
* the meta field for the alt text and formats it for export.
*/
}
}
}
/**
* Register all of the hooks related to the public-facing functionality
* of the plugin.
*
* @since 1.0.0
* @access private
*/
private function define_public_hooks()
{
$plugin_public = new Ar_Model_Viewer_For_Woocommerce_Public($this->get_plugin_name(), $this->get_plugin_prefix(), $this->get_version());
$plugin_public_shortcode = new Ar_Model_Viewer_For_Woocommerce_Public_Shortcode($this->get_plugin_name(), $this->get_plugin_prefix(), $this->get_version());
$plugin_public_tab = new Ar_Model_Viewer_For_Woocommerce_Public_Tab($this->get_plugin_name(), $this->get_plugin_prefix(), $this->get_version());
// Include the styles for public web
$this->loader->add_action('wp_enqueue_scripts', $plugin_public, 'enqueue_styles');
// Include the scripts for public web
$this->loader->add_action('wp_enqueue_scripts', $plugin_public, 'enqueue_scripts');
// Check options of the plugin
$ar_model_viewer_settings = get_option('ar_model_viewer_for_woocommerce_settings');
// Check the option where the button is avaible
switch (isset($ar_model_viewer_settings['ar_model_viewer_for_woocommerce_btn'])) {
case 1:
$this->loader->add_action('woocommerce_before_single_product_summary', $plugin_public, 'ar_model_viewer_for_woocommerce_button');
break;
case 2:
$this->loader->add_action('woocommerce_after_single_product_summary', $plugin_public, 'ar_model_viewer_for_woocommerce_button');
break;
case 3:
$this->loader->add_action('woocommerce_before_single_product', $plugin_public, 'ar_model_viewer_for_woocommerce_button');
break;
case 4:
$this->loader->add_action('woocommerce_after_single_product', $plugin_public, 'ar_model_viewer_for_woocommerce_button');
break;
case 5:
$this->loader->add_action('woocommerce_after_add_to_cart_form', $plugin_public, 'ar_model_viewer_for_woocommerce_button');
break;
case 6:
$this->loader->add_action('woocommerce_before_add_to_cart_form', $plugin_public, 'ar_model_viewer_for_woocommerce_button');
break;
}
// Check if in settings show in tabs is active
if (isset($ar_model_viewer_settings['ar_model_viewer_for_woocommerce_single_product_tabs'])) {
if ($ar_model_viewer_settings['ar_model_viewer_for_woocommerce_single_product_tabs'] == 'yes') {
// Show a button before single_product
$this->loader->add_filter('woocommerce_product_tabs', $plugin_public_tab, 'ar_model_viewer_for_woocommerce_tab');
}
}
$this->loader->add_action('wp_ajax_ar_model_viewer_for_woocommerce_get_model_and_settings', $plugin_public, 'ar_model_viewer_for_woocommerce_get_model_and_settings');
$this->loader->add_shortcode($this->plugin_prefix . 'shortcode', $plugin_public_shortcode, 'ar_model_viewer_for_woocommerce_shortcode_func', 10, 1);
}
/**
* Run the loader to execute all of the hooks with WordPress.
*
* @since 1.0.0
*/
public function run()
{
$this->loader->run();
}
/**
* The name of the plugin used to uniquely identify it within the context of
* WordPress and to define internationalization functionality.
*
* @since 1.0.0
* @return string The name of the plugin.
*/
public function get_plugin_name()
{
return $this->plugin_name;
}
/**
* The unique prefix of the plugin used to uniquely prefix technical functions.
*
* @since 1.0.0
* @return string The prefix of the plugin.
*/
public function get_plugin_prefix()
{
return $this->plugin_prefix;
}
/**
* The reference to the class that orchestrates the hooks with the plugin.
*
* @since 1.0.0
* @return Ar_Model_Viewer_For_Woocommerce_Loader Orchestrates the hooks of the plugin.
*/
public function get_loader()
{
return $this->loader;
}
/**
* Retrieve the version number of the plugin.
*
* @since 1.0.0
* @return string The version number of the plugin.
*/
public function get_version()
{
return $this->version;
}
}