PluginProbe ʕ •ᴥ•ʔ
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). / 2.7.92
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). v2.7.92
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 3 months ago purchase-orders 3 months ago zoho-crm 6 months ago zoho-inventory 3 months ago class-api-handler-zoho.php 6 months ago class-auth-zoho.php 3 months ago class-common.php 3 months ago class-plugin.php 3 months ago class-wc-api.php 5 months ago index.php 1 year ago
class-plugin.php
305 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_zoho_sync_category_cron' );
104 wp_clear_scheduled_hook( 'cmbird_zoho_contact_sync' );
105 }
106
107 /**
108 * Deletes all the options and post/user meta set by the plugin.
109 *
110 * This function is called when the plugin is uninstalled.
111 *
112 * @since 1.0.0
113 */
114 public static function uninstall() {
115 $post_meta_keys = array(
116 'zi_item_id',
117 'zi_purchase_account_id',
118 'zi_account_id',
119 'zi_account_name',
120 'zi_inventory_account_id',
121 'zi_salesorder_id',
122 'zi_category_id',
123 '_cost_price',
124 'eo_item_id',
125 );
126 $user_meta_keys = array(
127 'zi_contact_id',
128 'zi_primary_contact_id',
129 'zi_created_time',
130 'zi_last_modified_time',
131 'zi_billing_address_id',
132 'zi_shipping_address_id',
133 'zi_contact_persons_id',
134 'zi_currency_id',
135 'zi_currency_code',
136 'eo_contact_id',
137 'eo_account_id',
138 'eo_gl_account',
139 );
140 $zi_option_keys = array(
141 'cmbird_zi_webhook_password',
142 'cmbird_zoho_inventory_cron_class',
143 'cmbird_zoho_sync_status',
144 'cmbird_zoho_item_category',
145 'cmbird_zoho_stock_sync_status',
146 'cmbird_zoho_item_name_sync_status',
147 'cmbird_zoho_enable_auto_no_status',
148 'cmbird_zoho_product_sync_status',
149 'cmbird_zoho_disable_image_sync_status',
150 'cmbird_zoho_disable_price_sync_status',
151 'cmbird_zoho_disable_name_sync_status',
152 'cmbird_zoho_disable_description_sync_status',
153 'cmbird_zoho_enable_accounting_stock_status',
154 'cmbird_zoho_enable_order_status',
155 'cmbird_wootozoho_custom_fields',
156 'cmbird_zoho_pricelist_id',
157 'cmbird_zoho_location_id_status',
158 'cmbird_zoho_inventory_auth_code',
159 'cmbird_zoho_inventory_access_token',
160 'cmbird_zoho_inventory_refresh_token',
161 'cmbird_zoho_inventory_timestamp',
162 'cmbird_zoho_inventory_oid',
163 'cmbird_zoho_inventory_url',
164 'cmbird_zoho_inventory_cid',
165 'cmbird_zoho_inventory_cs',
166 'cmbird_zoho_inventory_domain',
167 'cmbird_authorization_redirect_uri',
168 'cmbird_zoho_crm_auth_code',
169 'cmbird_zoho_crm_access_token',
170 'cmbird_zoho_crm_refresh_token',
171 'cmbird_zoho_crm_timestamp',
172 'cmbird_location_data',
173 );
174
175 foreach ( $zi_option_keys as $zi_option ) {
176 delete_option( $zi_option );
177 }
178
179 foreach ( $post_meta_keys as $post_key ) {
180 delete_post_meta_by_key( $post_key );
181 }
182
183 $users = get_users(
184 array(
185 'fields' => array( 'ID' ),
186 )
187 );
188 foreach ( $users as $user_id ) {
189 foreach ( $user_meta_keys as $user_key ) {
190 delete_user_meta( $user_id, $user_key );
191 }
192 }
193 // deleting mapped categories.
194 global $wpdb;
195 $table_name = $wpdb->prefix . 'options';
196 $sql = $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM %s WHERE option_name LIKE %s', $table_name, '%zoho_id_for_term_id_%' ) );
197 foreach ( $sql as $key => $row ) {
198 $option_name = $row->option_name;
199 $wpdb->delete( $table_name, array( 'option_name' => $option_name ), array( '%s' ) );
200 }
201 // clear scheduled zcrm_refresh_token.
202 wp_clear_scheduled_hook( 'zcrm_refresh_token' );
203
204 // Remove the mu-plugin file.
205 $mu_plugin_file = WP_CONTENT_DIR . '/mu-plugins/cmbird-media-cleanup-cli.php';
206 if ( file_exists( $mu_plugin_file ) ) {
207 unlink( $mu_plugin_file );
208 }
209 }
210
211 /**
212 * Initializes the plugin.
213 *
214 * Removes the option for Zoho Inventory access token if it contains only one character.
215 * Schedules a cronjob for import sync if the access token is not empty and the interval is not set to 'none'.
216 * Creates the webhook password if it does not exist.
217 * Loads the necessary Ajax handlers and templates.
218 *
219 * @since 1.0.0
220 */
221 public static function init() {
222 if ( is_admin() ) {
223 $zoho_inventory_access_token = get_option( 'cmbird_zoho_inventory_access_token' );
224 // remove option zoho_inventory_access_token if it contains only one character.
225 if ( $zoho_inventory_access_token && strlen( $zoho_inventory_access_token ) === 1 ) {
226 delete_option( 'cmbird_zoho_inventory_access_token' );
227 } else {
228 // schedule cronjob for import sync.
229 $interval = get_option( 'zi_cron_interval' );
230 if ( 'none' !== $interval && ! empty( $zoho_inventory_access_token ) ) {
231 if ( ! wp_next_scheduled( 'zi_execute_import_sync' ) ) {
232 wp_schedule_event( time(), $interval, 'zi_execute_import_sync' );
233 }
234 } else {
235 wp_clear_scheduled_hook( 'zi_execute_import_sync' );
236 }
237 }
238 // create webhook password if does not exists.
239 if ( ! get_option( 'cmbird_zi_webhook_password', false ) ) {
240 add_option( 'cmbird_zi_webhook_password', password_hash( 'commercebird-zi-webhook-token', PASSWORD_BCRYPT ) );
241 }
242 Template::instance();
243 // Load the necessary Ajax handlers and templates.
244 ZohoInventoryAjax::instance();
245 ZohoCRMAjax::instance();
246 AcfAjax::instance();
247 SettingsAjax::instance();
248 ExactOnlineAjax::instance();
249 if ( class_exists( 'ACF' ) ) {
250 Cmbird_Acf::instance();
251 }
252 }
253 new Cmbird_WC_API();
254 new CMBIRD_Purchase_Orders_Automation();
255 }
256
257 /**
258 * Cron callback for zoho import sync interval.
259 *
260 * When the WP cron fires this hook we simply forward the request to the
261 * existing `import_simple_items_cron` action. That action is already
262 * registered in the main plugin bootstrapping script and handles batch
263 * scheduling via Action Scheduler. Passing along the arguments allows for
264 * backwards compatibility if anybody ever runs the hook manually with
265 * parameters.
266 *
267 * @since 2.7.7
268 * @param mixed ...$args Optional arguments supplied by do_action.
269 * @return void
270 */
271 public static function dispatch_import_simple_items( ...$args ) {
272 // if the event was fired with explicit arguments we simply forward them
273 // to the existing hook (primarily useful for manual/testing purposes).
274 if ( ! empty( $args ) ) {
275 do_action( 'import_simple_items_cron', ...$args );
276 return;
277 }
278 $zi_common_class = new \CMBIRD_Common_Functions();
279 $zi_common_class->clear_orphan_data();
280
281 // categories are stored as serialized zoho ids; convert them to WC terms.
282 $zoho_item_category = get_option( 'cmbird_zoho_item_category' );
283 if ( $zoho_item_category ) {
284 // convert serialized string to array.
285 $categories = maybe_unserialize( $zoho_item_category );
286 if ( ! is_array( $categories ) ) {
287 return; // invalid format -> nothing to do.
288 }
289 } else {
290 return; // no categories configured -> nothing to do.
291 }
292 foreach ( $categories as $category_id ) {
293 $data = array(
294 'page' => 1,
295 'category' => $category_id,
296 );
297 if ( function_exists( 'as_has_scheduled_action' )
298 && ! as_has_scheduled_action( 'import_simple_items_cron', $data, 'commercebird' )
299 ) {
300 as_schedule_single_action( time(), 'import_simple_items_cron', $data, 'commercebird' );
301 }
302 }
303 }
304 }
305