apis
5 months ago
purchase-orders
5 months ago
zoho-crm
6 months ago
zoho-inventory
5 months ago
class-api-handler-zoho.php
6 months ago
class-auth-zoho.php
6 months ago
class-common.php
6 months ago
class-plugin.php
5 months ago
class-wc-api.php
5 months ago
index.php
1 year ago
class-plugin.php
249 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 log table. |
| 56 | // global $wpdb; |
| 57 | // $charset_collate = $wpdb->get_charset_collate(); |
| 58 | // $zi_product_log_table = "{$wpdb->prefix}cmbird_zi_product_error"; |
| 59 | // $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;"; |
| 60 | // dbDelta( $zi_create_sql ); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Copy the CLI script to mu-plugins folder. |
| 65 | * |
| 66 | * @since 2.6.5 |
| 67 | */ |
| 68 | private static function install_mu_plugin() { |
| 69 | $mu_plugins_dir = WP_CONTENT_DIR . '/mu-plugins'; |
| 70 | $source_file = CMBIRD_PATH . 'mu-plugins/cmbird-media-cleanup-cli.php'; |
| 71 | $dest_file = $mu_plugins_dir . '/cmbird-media-cleanup-cli.php'; |
| 72 | |
| 73 | // Create mu-plugins directory if it doesn't exist. |
| 74 | if ( ! is_dir( $mu_plugins_dir ) ) { |
| 75 | wp_mkdir_p( $mu_plugins_dir ); |
| 76 | } |
| 77 | |
| 78 | // Copy the file if source exists and destination doesn't or is outdated. |
| 79 | if ( file_exists( $source_file ) ) { |
| 80 | if ( ! file_exists( $dest_file ) || filemtime( $source_file ) > filemtime( $dest_file ) ) { |
| 81 | copy( $source_file, $dest_file ); |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Clear all the scheduled actions when the plugin is deactivated. |
| 88 | * |
| 89 | * @since 1.0.0 |
| 90 | */ |
| 91 | public static function deactivate() { |
| 92 | wp_clear_scheduled_hook( 'zi_execute_import_sync' ); |
| 93 | wp_clear_scheduled_hook( 'cmbird_eo_get_payment_statuses' ); |
| 94 | wp_clear_scheduled_hook( 'cmbird_exact_online_sync_orders' ); |
| 95 | wp_clear_scheduled_hook( 'cmbird_zoho_sync_category_cron' ); |
| 96 | wp_clear_scheduled_hook( 'cmbird_zoho_contact_sync' ); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Deletes all the options and post/user meta set by the plugin. |
| 101 | * |
| 102 | * This function is called when the plugin is uninstalled. |
| 103 | * |
| 104 | * @since 1.0.0 |
| 105 | */ |
| 106 | public static function uninstall() { |
| 107 | $post_meta_keys = array( |
| 108 | 'zi_item_id', |
| 109 | 'zi_purchase_account_id', |
| 110 | 'zi_account_id', |
| 111 | 'zi_account_name', |
| 112 | 'zi_inventory_account_id', |
| 113 | 'zi_salesorder_id', |
| 114 | 'zi_category_id', |
| 115 | '_cost_price', |
| 116 | 'eo_item_id', |
| 117 | ); |
| 118 | $user_meta_keys = array( |
| 119 | 'zi_contact_id', |
| 120 | 'zi_primary_contact_id', |
| 121 | 'zi_created_time', |
| 122 | 'zi_last_modified_time', |
| 123 | 'zi_billing_address_id', |
| 124 | 'zi_shipping_address_id', |
| 125 | 'zi_contact_persons_id', |
| 126 | 'zi_currency_id', |
| 127 | 'zi_currency_code', |
| 128 | 'eo_contact_id', |
| 129 | 'eo_account_id', |
| 130 | 'eo_gl_account', |
| 131 | ); |
| 132 | $zi_option_keys = array( |
| 133 | 'cmbird_zi_webhook_password', |
| 134 | 'cmbird_zoho_inventory_cron_class', |
| 135 | 'cmbird_zoho_sync_status', |
| 136 | 'cmbird_zoho_item_category', |
| 137 | 'cmbird_zoho_stock_sync_status', |
| 138 | 'cmbird_zoho_item_name_sync_status', |
| 139 | 'cmbird_zoho_enable_auto_no_status', |
| 140 | 'cmbird_zoho_product_sync_status', |
| 141 | 'cmbird_zoho_disable_image_sync_status', |
| 142 | 'cmbird_zoho_disable_price_sync_status', |
| 143 | 'cmbird_zoho_disable_name_sync_status', |
| 144 | 'cmbird_zoho_disable_description_sync_status', |
| 145 | 'cmbird_zoho_enable_accounting_stock_status', |
| 146 | 'cmbird_zoho_enable_order_status', |
| 147 | 'cmbird_wootozoho_custom_fields', |
| 148 | 'cmbird_zoho_pricelist_id', |
| 149 | 'cmbird_zoho_location_id_status', |
| 150 | 'cmbird_zoho_inventory_auth_code', |
| 151 | 'cmbird_zoho_inventory_access_token', |
| 152 | 'cmbird_zoho_inventory_refresh_token', |
| 153 | 'cmbird_zoho_inventory_timestamp', |
| 154 | 'cmbird_zoho_inventory_oid', |
| 155 | 'cmbird_zoho_inventory_url', |
| 156 | 'cmbird_zoho_inventory_cid', |
| 157 | 'cmbird_zoho_inventory_cs', |
| 158 | 'cmbird_zoho_inventory_domain', |
| 159 | 'cmbird_authorization_redirect_uri', |
| 160 | 'cmbird_zoho_crm_auth_code', |
| 161 | 'cmbird_zoho_crm_access_token', |
| 162 | 'cmbird_zoho_crm_refresh_token', |
| 163 | 'cmbird_zoho_crm_timestamp', |
| 164 | 'cmbird_location_data', |
| 165 | ); |
| 166 | |
| 167 | foreach ( $zi_option_keys as $zi_option ) { |
| 168 | delete_option( $zi_option ); |
| 169 | } |
| 170 | |
| 171 | foreach ( $post_meta_keys as $post_key ) { |
| 172 | delete_post_meta_by_key( $post_key ); |
| 173 | } |
| 174 | |
| 175 | $users = get_users( |
| 176 | array( |
| 177 | 'fields' => array( 'ID' ), |
| 178 | ) |
| 179 | ); |
| 180 | foreach ( $users as $user_id ) { |
| 181 | foreach ( $user_meta_keys as $user_key ) { |
| 182 | delete_user_meta( $user_id, $user_key ); |
| 183 | } |
| 184 | } |
| 185 | // deleting mapped categories. |
| 186 | global $wpdb; |
| 187 | $table_name = $wpdb->prefix . 'options'; |
| 188 | $sql = $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM %s WHERE option_name LIKE %s', $table_name, '%zoho_id_for_term_id_%' ) ); |
| 189 | foreach ( $sql as $key => $row ) { |
| 190 | $option_name = $row->option_name; |
| 191 | $wpdb->delete( $table_name, array( 'option_name' => $option_name ), array( '%s' ) ); |
| 192 | } |
| 193 | // clear scheduled zcrm_refresh_token. |
| 194 | wp_clear_scheduled_hook( 'zcrm_refresh_token' ); |
| 195 | |
| 196 | // Remove the mu-plugin file. |
| 197 | $mu_plugin_file = WP_CONTENT_DIR . '/mu-plugins/cmbird-media-cleanup-cli.php'; |
| 198 | if ( file_exists( $mu_plugin_file ) ) { |
| 199 | unlink( $mu_plugin_file ); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Initializes the plugin. |
| 205 | * |
| 206 | * Removes the option for Zoho Inventory access token if it contains only one character. |
| 207 | * Schedules a cronjob for import sync if the access token is not empty and the interval is not set to 'none'. |
| 208 | * Creates the webhook password if it does not exist. |
| 209 | * Loads the necessary Ajax handlers and templates. |
| 210 | * |
| 211 | * @since 1.0.0 |
| 212 | */ |
| 213 | public static function init() { |
| 214 | if ( is_admin() ) { |
| 215 | $zoho_inventory_access_token = get_option( 'cmbird_zoho_inventory_access_token' ); |
| 216 | // remove option zoho_inventory_access_token if it contains only one character. |
| 217 | if ( $zoho_inventory_access_token && strlen( $zoho_inventory_access_token ) === 1 ) { |
| 218 | delete_option( 'cmbird_zoho_inventory_access_token' ); |
| 219 | } else { |
| 220 | // schedule cronjob for import sync. |
| 221 | $interval = get_option( 'zi_cron_interval' ); |
| 222 | if ( 'none' !== $interval && ! empty( $zoho_inventory_access_token ) ) { |
| 223 | if ( ! wp_next_scheduled( 'zi_execute_import_sync' ) ) { |
| 224 | wp_schedule_event( time(), $interval, 'zi_execute_import_sync' ); |
| 225 | } |
| 226 | } else { |
| 227 | wp_clear_scheduled_hook( 'zi_execute_import_sync' ); |
| 228 | } |
| 229 | } |
| 230 | // create webhook password if does not exists. |
| 231 | if ( ! get_option( 'cmbird_zi_webhook_password', false ) ) { |
| 232 | add_option( 'cmbird_zi_webhook_password', password_hash( 'commercebird-zi-webhook-token', PASSWORD_BCRYPT ) ); |
| 233 | } |
| 234 | Template::instance(); |
| 235 | // Load the necessary Ajax handlers and templates. |
| 236 | ZohoInventoryAjax::instance(); |
| 237 | ZohoCRMAjax::instance(); |
| 238 | AcfAjax::instance(); |
| 239 | SettingsAjax::instance(); |
| 240 | ExactOnlineAjax::instance(); |
| 241 | if ( class_exists( 'ACF' ) ) { |
| 242 | Cmbird_Acf::instance(); |
| 243 | } |
| 244 | } |
| 245 | new Cmbird_WC_API(); |
| 246 | new CMBIRD_Purchase_Orders_Automation(); |
| 247 | } |
| 248 | } |
| 249 |