PluginProbe ʕ •ᴥ•ʔ
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). / 2.8.4
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). v2.8.4
3.0.3 3.0.2 3.0.1 trunk 2.2.14 2.2.15 2.2.16 2.2.17 2.2.18 2.2.19 2.3.0 2.3.1 2.3.10 2.3.11 2.3.12 2.3.13 2.3.14 2.3.2 2.3.3 2.3.4 2.3.5 2.3.6 2.3.7 2.3.8 2.3.9 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.5.0 2.5.1 2.5.2 2.6.0 2.6.1 2.6.2 2.6.3 2.6.4 2.6.5 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 2.7.9 2.7.91 2.7.92 2.7.93 2.8.0 2.8.1 2.8.2 2.8.3 2.8.4 2.8.5 2.9.0 2.9.1 2.9.2 2.9.3 3.0.0
commercebird / includes / classes / class-plugin.php
commercebird / includes / classes Last commit date
apis 2 months ago purchase-orders 3 months ago quotes 2 months ago zoho-crm 6 months ago zoho-inventory 2 months ago class-api-handler-zoho.php 6 months ago class-auth-zoho.php 3 months ago class-common.php 2 months ago class-plugin.php 2 months ago class-wc-api.php 5 months ago index.php 1 year ago
class-plugin.php
307 lines
1 <?php
2
3 namespace CommerceBird;
4
5 if ( ! defined( 'ABSPATH' ) ) {
6 exit;
7 }
8
9 require_once ABSPATH . 'wp-admin/includes/upgrade.php';
10
11 use CommerceBird\Admin\Actions\Ajax\ExactOnlineAjax;
12 use CommerceBird\Admin\Actions\Ajax\ZohoInventoryAjax;
13 use CommerceBird\Admin\Actions\Ajax\ZohoCRMAjax;
14 use CommerceBird\Admin\Actions\Ajax\AcfAjax;
15 use CommerceBird\Admin\Actions\Ajax\SettingsAjax;
16 use CommerceBird\Admin\Template;
17 use CommerceBird\Admin\Cmbird_Acf;
18 use CommerceBird\Cmbird_WC_API;
19 use CMBIRD_Purchase_Orders_Automation;
20
21 /**
22 * Main plugin class for CommerceBird.
23 *
24 * Handles activation, deactivation, uninstall, and initialization logic.
25 *
26 * @since 1.0.0
27 */
28 class Plugin {
29
30
31 /**
32 * The constructor for the plugin.
33 *
34 * @since 1.0.0
35 *
36 * This function is called when the plugin is loaded. It hooks the init method to the admin_init action.
37 */
38 public function __construct() {
39 add_action( 'admin_init', array( $this, 'init' ) );
40 }
41
42 /**
43 * This function is run on plugin activation.
44 * It creates a database table to store log data of products which have errors during sync.
45 * The fields of the table are:
46 * - ID: auto incrementing id
47 * - product_id: the id of the product which has errors
48 * - error_message: the error message
49 * - sync_timestamp: the timestamp when the sync was attempted
50 * - status: the status of the sync (success/fail)
51 */
52 public static function activate() {
53 // Copy CLI script to mu-plugins folder for automatic loading.
54 self::install_mu_plugin();
55 // Create the "vendor" role — must run at activation, not on every init.
56 if ( ! get_role( 'vendor' ) ) {
57 add_role(
58 'vendor',
59 __( 'Vendor', 'commercebird' ),
60 array( 'read' => true )
61 );
62 }
63 // create log table.
64 // global $wpdb;
65 // $charset_collate = $wpdb->get_charset_collate();
66 // $zi_product_log_table = "{$wpdb->prefix}cmbird_zi_product_error";
67 // $zi_create_sql = "CREATE TABLE $zi_product_log_table ( ID bigint(20) PRIMARY KEY auto_increment, product_id bigint(20) NOT NULL, error_message TEXT NOT NULL, sync_timestamp VARCHAR(20) NOT NULL, status int(10) NOT NULL )$charset_collate;";
68 // dbDelta( $zi_create_sql );
69 }
70
71 /**
72 * Copy the CLI script to mu-plugins folder.
73 *
74 * @since 2.6.5
75 */
76 private static function install_mu_plugin() {
77 $mu_plugins_dir = WP_CONTENT_DIR . '/mu-plugins';
78 $source_file = CMBIRD_PATH . 'mu-plugins/cmbird-media-cleanup-cli.php';
79 $dest_file = $mu_plugins_dir . '/cmbird-media-cleanup-cli.php';
80
81 // Create mu-plugins directory if it doesn't exist.
82 if ( ! is_dir( $mu_plugins_dir ) ) {
83 wp_mkdir_p( $mu_plugins_dir );
84 }
85
86 // Copy the file if source exists and destination doesn't or is outdated.
87 if ( file_exists( $source_file ) ) {
88 if ( ! file_exists( $dest_file ) || filemtime( $source_file ) > filemtime( $dest_file ) ) {
89 copy( $source_file, $dest_file );
90 }
91 }
92 }
93
94 /**
95 * Clear all the scheduled actions when the plugin is deactivated.
96 *
97 * @since 1.0.0
98 */
99 public static function deactivate() {
100 wp_clear_scheduled_hook( 'zi_execute_import_sync' );
101 wp_clear_scheduled_hook( 'cmbird_eo_get_payment_statuses' );
102 wp_clear_scheduled_hook( 'cmbird_exact_online_sync_orders' );
103 wp_clear_scheduled_hook( 'cmbird_eo_product_mapping_daily' );
104 wp_clear_scheduled_hook( 'cmbird_zoho_sync_category_cron' );
105 wp_clear_scheduled_hook( 'cmbird_zoho_contact_sync' );
106 }
107
108 /**
109 * Deletes all the options and post/user meta set by the plugin.
110 *
111 * This function is called when the plugin is uninstalled.
112 *
113 * @since 1.0.0
114 */
115 public static function uninstall() {
116 $post_meta_keys = array(
117 'zi_item_id',
118 'zi_purchase_account_id',
119 'zi_account_id',
120 'zi_account_name',
121 'zi_inventory_account_id',
122 'zi_salesorder_id',
123 'zi_category_id',
124 '_cost_price',
125 'eo_item_id',
126 );
127 $user_meta_keys = array(
128 'zi_contact_id',
129 'zi_primary_contact_id',
130 'zi_created_time',
131 'zi_last_modified_time',
132 'zi_billing_address_id',
133 'zi_shipping_address_id',
134 'zi_contact_persons_id',
135 'zi_currency_id',
136 'zi_currency_code',
137 'eo_contact_id',
138 'eo_account_id',
139 'eo_gl_account',
140 );
141 $zi_option_keys = array(
142 'cmbird_zi_webhook_password',
143 'cmbird_zoho_inventory_cron_class',
144 'cmbird_zoho_sync_status',
145 'cmbird_zoho_item_category',
146 'cmbird_zoho_stock_sync_status',
147 'cmbird_zoho_item_name_sync_status',
148 'cmbird_zoho_enable_auto_no_status',
149 'cmbird_zoho_product_sync_status',
150 'cmbird_zoho_disable_image_sync_status',
151 'cmbird_zoho_disable_price_sync_status',
152 'cmbird_zoho_disable_name_sync_status',
153 'cmbird_zoho_disable_description_sync_status',
154 'cmbird_zoho_enable_accounting_stock_status',
155 'cmbird_zoho_enable_order_status',
156 'cmbird_wootozoho_custom_fields',
157 'cmbird_zoho_pricelist_id',
158 'cmbird_zoho_location_id_status',
159 'cmbird_zoho_inventory_auth_code',
160 'cmbird_zoho_inventory_access_token',
161 'cmbird_zoho_inventory_refresh_token',
162 'cmbird_zoho_inventory_timestamp',
163 'cmbird_zoho_inventory_oid',
164 'cmbird_zoho_inventory_url',
165 'cmbird_zoho_inventory_cid',
166 'cmbird_zoho_inventory_cs',
167 'cmbird_zoho_inventory_domain',
168 'cmbird_authorization_redirect_uri',
169 'cmbird_zoho_crm_auth_code',
170 'cmbird_zoho_crm_access_token',
171 'cmbird_zoho_crm_refresh_token',
172 'cmbird_zoho_crm_timestamp',
173 'cmbird_location_data',
174 'cmbird_eo_product_cron_enabled',
175 );
176
177 foreach ( $zi_option_keys as $zi_option ) {
178 delete_option( $zi_option );
179 }
180
181 foreach ( $post_meta_keys as $post_key ) {
182 delete_post_meta_by_key( $post_key );
183 }
184
185 $users = get_users(
186 array(
187 'fields' => array( 'ID' ),
188 )
189 );
190 foreach ( $users as $user_id ) {
191 foreach ( $user_meta_keys as $user_key ) {
192 delete_user_meta( $user_id, $user_key );
193 }
194 }
195 // deleting mapped categories.
196 global $wpdb;
197 $table_name = $wpdb->prefix . 'options';
198 $sql = $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM %s WHERE option_name LIKE %s', $table_name, '%zoho_id_for_term_id_%' ) );
199 foreach ( $sql as $key => $row ) {
200 $option_name = $row->option_name;
201 $wpdb->delete( $table_name, array( 'option_name' => $option_name ), array( '%s' ) );
202 }
203 // clear scheduled zcrm_refresh_token.
204 wp_clear_scheduled_hook( 'zcrm_refresh_token' );
205
206 // Remove the mu-plugin file.
207 $mu_plugin_file = WP_CONTENT_DIR . '/mu-plugins/cmbird-media-cleanup-cli.php';
208 if ( file_exists( $mu_plugin_file ) ) {
209 wp_delete_file( $mu_plugin_file );
210 }
211 }
212
213 /**
214 * Initializes the plugin.
215 *
216 * Removes the option for Zoho Inventory access token if it contains only one character.
217 * Schedules a cronjob for import sync if the access token is not empty and the interval is not set to 'none'.
218 * Creates the webhook password if it does not exist.
219 * Loads the necessary Ajax handlers and templates.
220 *
221 * @since 1.0.0
222 */
223 public static function init() {
224 if ( is_admin() ) {
225 $zoho_inventory_access_token = get_option( 'cmbird_zoho_inventory_access_token' );
226 // remove option zoho_inventory_access_token if it contains only one character.
227 if ( $zoho_inventory_access_token && strlen( $zoho_inventory_access_token ) === 1 ) {
228 delete_option( 'cmbird_zoho_inventory_access_token' );
229 } else {
230 // schedule cronjob for import sync.
231 $interval = get_option( 'zi_cron_interval' );
232 if ( 'none' !== $interval && ! empty( $zoho_inventory_access_token ) ) {
233 if ( ! wp_next_scheduled( 'zi_execute_import_sync' ) ) {
234 wp_schedule_event( time(), $interval, 'zi_execute_import_sync' );
235 }
236 } else {
237 wp_clear_scheduled_hook( 'zi_execute_import_sync' );
238 }
239 }
240 // create webhook password if does not exists.
241 if ( ! get_option( 'cmbird_zi_webhook_password', false ) ) {
242 add_option( 'cmbird_zi_webhook_password', password_hash( 'commercebird-zi-webhook-token', PASSWORD_BCRYPT ) );
243 }
244 Template::instance();
245 // Load the necessary Ajax handlers and templates.
246 ZohoInventoryAjax::instance();
247 ZohoCRMAjax::instance();
248 AcfAjax::instance();
249 SettingsAjax::instance();
250 ExactOnlineAjax::instance();
251 if ( class_exists( 'ACF' ) ) {
252 Cmbird_Acf::instance();
253 }
254 }
255 new Cmbird_WC_API();
256 new CMBIRD_Purchase_Orders_Automation();
257 }
258
259 /**
260 * Cron callback for zoho import sync interval.
261 *
262 * When the WP cron fires this hook we simply forward the request to the
263 * existing `import_simple_items_cron` action. That action is already
264 * registered in the main plugin bootstrapping script and handles batch
265 * scheduling via Action Scheduler. Passing along the arguments allows for
266 * backwards compatibility if anybody ever runs the hook manually with
267 * parameters.
268 *
269 * @since 2.7.7
270 * @param mixed ...$args Optional arguments supplied by do_action.
271 * @return void
272 */
273 public static function dispatch_import_simple_items( ...$args ) {
274 // if the event was fired with explicit arguments we simply forward them
275 // to the existing hook (primarily useful for manual/testing purposes).
276 if ( ! empty( $args ) ) {
277 do_action( 'import_simple_items_cron', ...$args );
278 return;
279 }
280 $zi_common_class = new \CMBIRD_Common_Functions();
281 $zi_common_class->clear_orphan_data();
282
283 // categories are stored as serialized zoho ids; convert them to WC terms.
284 $zoho_item_category = get_option( 'cmbird_zoho_item_category' );
285 if ( $zoho_item_category ) {
286 // convert serialized string to array.
287 $categories = maybe_unserialize( $zoho_item_category );
288 if ( ! is_array( $categories ) ) {
289 return; // invalid format -> nothing to do.
290 }
291 } else {
292 return; // no categories configured -> nothing to do.
293 }
294 foreach ( $categories as $category_id ) {
295 $data = array(
296 'page' => 1,
297 'category' => $category_id,
298 );
299 if ( function_exists( 'as_has_scheduled_action' )
300 && ! as_has_scheduled_action( 'import_simple_items_cron', $data, 'commercebird' )
301 ) {
302 as_schedule_single_action( time(), 'import_simple_items_cron', $data, 'commercebird' );
303 }
304 }
305 }
306 }
307