commercebird
Last commit date
admin
1 year ago
includes
1 year ago
languages
1 year ago
vendor
1 year ago
LICENSE
1 year ago
changelog.txt
1 year ago
commercebird.php
1 year ago
composer.json
1 year ago
data-sync.php
1 year ago
index.php
1 year ago
readme.txt
1 year ago
commercebird.php
240 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Plugin Name: CommerceBird |
| 4 | * Plugin URI: https://commercebird.com |
| 5 | * Author: CommerceBird |
| 6 | * Description: This plugin helps you get the most of CommerceBird by allowing you to upload product images, use integrations like Zoho Inventory, Zoho CRM, Exact Online and more. Requires a subscription at CommerceBird.com. |
| 7 | * Version: 2.3.6 |
| 8 | * Requires PHP: 8.2 |
| 9 | * Requires Plugins: woocommerce |
| 10 | * Requires at least: 6.5 |
| 11 | * Tested up to: 6.8.1 |
| 12 | * Text Domain: commercebird |
| 13 | * Domain Path: /languages |
| 14 | * |
| 15 | * License: GNU General Public License v3.0 |
| 16 | * License URI: http://www.gnu.org/licenses/gpl-3.0.html |
| 17 | * |
| 18 | * @category Fulfillment |
| 19 | * @package CommerceBird |
| 20 | * @author Fawad Tiemoerie <info@commercebird.com> |
| 21 | * @copyright Copyright (c) 2024, CommerceBird |
| 22 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPL-3.0-or-later |
| 23 | * |
| 24 | * WC requires at least: 9.4.0 |
| 25 | * WC tested up to: 9.8.2 |
| 26 | */ |
| 27 | |
| 28 | if ( ! defined( 'ABSPATH' ) ) { |
| 29 | exit; // Exit if accessed directly |
| 30 | } |
| 31 | |
| 32 | if ( ! defined( 'CMBIRD_VERSION' ) ) { |
| 33 | define( 'CMBIRD_VERSION', '2.3.6' ); |
| 34 | } |
| 35 | if ( ! defined( 'CMBIRD_PATH' ) ) { |
| 36 | define( 'CMBIRD_PATH', plugin_dir_path( __FILE__ ) ); |
| 37 | } |
| 38 | if ( ! defined( 'CMBIRD_URL' ) ) { |
| 39 | define( 'CMBIRD_URL', plugin_dir_url( __FILE__ ) ); |
| 40 | } |
| 41 | if ( ! defined( 'CMBIRD_MENU_SLUG' ) ) { |
| 42 | define( 'CMBIRD_MENU_SLUG', 'commercebird-app' ); |
| 43 | } |
| 44 | |
| 45 | require_once CMBIRD_PATH . 'includes/woo-functions.php'; |
| 46 | require_once CMBIRD_PATH . 'includes/sync/order-backend.php'; |
| 47 | require_once CMBIRD_PATH . 'data-sync.php'; |
| 48 | |
| 49 | require __DIR__ . '/vendor/autoload.php'; |
| 50 | |
| 51 | use Automattic\WooCommerce\Utilities\FeaturesUtil; |
| 52 | use CommerceBird\Admin\Actions\Sync\ExactOnlineSync; |
| 53 | use CommerceBird\Admin\Actions\Sync\ZohoCRMSync; |
| 54 | use CommerceBird\API\CMBird_APIs; |
| 55 | use CommerceBird\API\CreateOrderWebhook; |
| 56 | use CommerceBird\API\Exact; |
| 57 | use CommerceBird\API\ProductWebhook; |
| 58 | use CommerceBird\API\ShippingWebhook; |
| 59 | use CommerceBird\API\Zoho; |
| 60 | use CommerceBird\Plugin; |
| 61 | |
| 62 | /* |
| 63 | |-------------------------------------------------------------------------- |
| 64 | | Activation, deactivation and uninstall event. |
| 65 | |-------------------------------------------------------------------------- |
| 66 | */ |
| 67 | |
| 68 | register_activation_hook( __FILE__, array( Plugin::class, 'activate' ) ); |
| 69 | register_deactivation_hook( __FILE__, array( Plugin::class, 'deactivate' ) ); |
| 70 | register_uninstall_hook( __FILE__, array( Plugin::class, 'uninstall' ) ); |
| 71 | |
| 72 | /* |
| 73 | |-------------------------------------------------------------------------- |
| 74 | | Load the plugin translations |
| 75 | |-------------------------------------------------------------------------- |
| 76 | */ |
| 77 | add_action( 'init', 'cmbird_load_textdomain' ); |
| 78 | function cmbird_load_textdomain() { |
| 79 | load_plugin_textdomain( 'commercebird', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' ); |
| 80 | } |
| 81 | |
| 82 | /** Loading Purchase Order Class |
| 83 | * @since 1.0.0 |
| 84 | */ |
| 85 | function cmbird_purchase_order_class() { |
| 86 | if ( class_exists( 'WooCommerce' ) ) { |
| 87 | new CMBIRD_Purchase_Order(); |
| 88 | } |
| 89 | } |
| 90 | add_action( 'woocommerce_init', 'cmbird_purchase_order_class' ); |
| 91 | add_action( 'init', array( CMBIRD_PO_Admin_Manager::class, 'init' ), 11 ); |
| 92 | |
| 93 | /* |
| 94 | |-------------------------------------------------------------------------- |
| 95 | | Start the plugin |
| 96 | |-------------------------------------------------------------------------- |
| 97 | */ |
| 98 | Plugin::init(); |
| 99 | |
| 100 | /** |
| 101 | * Declaring compatibility for WooCommerce HPOS |
| 102 | */ |
| 103 | add_action( |
| 104 | 'before_woocommerce_init', |
| 105 | function () { |
| 106 | if ( class_exists( FeaturesUtil::class) ) { |
| 107 | FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true ); |
| 108 | } |
| 109 | } |
| 110 | ); |
| 111 | |
| 112 | /** |
| 113 | * Hooks for WC Action Scheduler to import or export products |
| 114 | */ |
| 115 | $import_products = new CMBIRD_Products_ZI(); |
| 116 | $import_pricelist = new CMBIRD_Pricelist_ZI(); |
| 117 | $product_class = new CMBIRD_Products_ZI_Export(); |
| 118 | $order_class = new CMBIRD_Order_Sync_ZI(); |
| 119 | $contact_class = new CMBIRD_Contact_ZI(); |
| 120 | $category_class = new CMBIRD_Categories_ZI(); |
| 121 | $import_pricelist->wc_b2b_groups(); |
| 122 | add_action( 'import_group_items_cron', array( $import_products, 'sync_groupitem_recursively' ), 10, 2 ); |
| 123 | add_action( 'import_simple_items_cron', array( $import_products, 'sync_item_recursively' ), 10, 2 ); |
| 124 | add_action( 'import_variable_product_cron', array( $import_products, 'import_variable_product_variations' ), 10, 2 ); |
| 125 | add_action( 'sync_zi_product_cron', array( $product_class, 'cmbird_zi_products_prepare_sync' ), 10, 2 ); |
| 126 | add_action( 'sync_zi_pricelist', array( $import_pricelist, 'zi_get_pricelist' ), 10, 2 ); |
| 127 | add_action( 'sync_zi_order', array( $order_class, 'zi_orders_prepare_sync' ), 10, 2 ); |
| 128 | add_action( 'sync_zi_import_contacts', array( $contact_class, 'cmbird_get_zoho_contacts' ), 10, 2 ); |
| 129 | add_action( 'cmbird_zi_category_cron', array( $category_class, 'cmbird_zi_category_sync_call' ), 10 ); |
| 130 | // add action to set the zoho rate limit option exceeded to false |
| 131 | add_action( 'cmbird_common', array( CMBIRD_Common_Functions::class, 'set_zoho_rate_limit_option' ) ); |
| 132 | // Exact Online Hooks |
| 133 | add_action( 'cmbird_sync_eo', array( ExactOnlineSync::class, 'sync' ), 10, 3 ); |
| 134 | add_action( 'cmbird_payment_status', array( ExactOnlineSync::class, 'cmbird_payment_status' ), 10, 1 ); |
| 135 | add_action( 'cmbird_eo_get_payment_statuses', array( ExactOnlineSync::class, 'get_payment_status_via_cron' ) ); |
| 136 | // Callback functions for scheduled action to process product or customer chunk |
| 137 | add_action( |
| 138 | 'cmbird_process_product_chunk', |
| 139 | function ($args) { |
| 140 | if ( ! is_array( $args ) || empty( $args['transient_key'] ) ) { |
| 141 | return; |
| 142 | } |
| 143 | $transient_key = $args['transient_key']; |
| 144 | $import_products = $args['import_products'] ?? false; |
| 145 | $chunked_products = get_transient( $transient_key ); |
| 146 | if ( $chunked_products ) { |
| 147 | $sync = new ExactOnlineSync(); |
| 148 | $sync->sync( 'product', $chunked_products, (bool) $import_products ); |
| 149 | // Remove transient after processing |
| 150 | delete_transient( $transient_key ); |
| 151 | } |
| 152 | }, |
| 153 | 10, |
| 154 | 1 |
| 155 | ); |
| 156 | add_action( |
| 157 | 'cmbird_process_customer_chunk', |
| 158 | function ($args) { |
| 159 | if ( ! is_array( $args ) || empty( $args['transient_key'] ) ) { |
| 160 | return; |
| 161 | } |
| 162 | $transient_key = $args['transient_key']; |
| 163 | $import_customers = $args['import_customers'] ?? false; |
| 164 | $chunked_customers = get_transient( $transient_key ); |
| 165 | if ( $chunked_customers ) { |
| 166 | $sync = new ExactOnlineSync(); |
| 167 | $sync->sync( 'customer', $chunked_customers, (bool) $import_customers ); |
| 168 | // Remove transient after processing |
| 169 | delete_transient( $transient_key ); |
| 170 | } |
| 171 | }, |
| 172 | 10, |
| 173 | 1 |
| 174 | ); |
| 175 | // Zoho CRM Hooks |
| 176 | add_action( 'init', array( ZohoCRMSync::class, 'refresh_token' ) ); |
| 177 | |
| 178 | // add classes to REST API |
| 179 | add_action( |
| 180 | 'rest_api_init', |
| 181 | function () { |
| 182 | new Zoho(); |
| 183 | new Exact(); |
| 184 | new ProductWebhook(); |
| 185 | new ShippingWebhook(); |
| 186 | new CreateOrderWebhook(); |
| 187 | new CMBird_APIs(); |
| 188 | $po_controller = new CMBIRD_REST_Shop_Purchase_Controller(); |
| 189 | $po_controller->register_routes(); |
| 190 | } |
| 191 | ); |
| 192 | |
| 193 | add_action( |
| 194 | 'save_post', |
| 195 | function ( $post_id, $post ) { |
| 196 | if ( 'wcb2b_group' === $post->post_type ) { |
| 197 | delete_transient( 'wc_b2b_groups' ); |
| 198 | } |
| 199 | }, |
| 200 | 10, |
| 201 | 2 |
| 202 | ); |
| 203 | |
| 204 | /** |
| 205 | * Perform actions when the plugin is updated |
| 206 | * @param string $upgrader_object |
| 207 | * @param array $options |
| 208 | * @return void |
| 209 | */ |
| 210 | add_action( 'upgrader_process_complete', 'cmbird_update_plugin_tasks', 10, 2 ); |
| 211 | |
| 212 | function cmbird_update_plugin_tasks( $upgrader_object, $options ) { |
| 213 | $this_plugin = plugin_basename( __FILE__ ); |
| 214 | |
| 215 | if ( 'update' === $options['action'] && 'plugin' === $options['type'] ) { |
| 216 | foreach ( $options['plugins'] as $plugin ) { |
| 217 | if ( $plugin === $this_plugin ) { |
| 218 | // Perform tasks when the plugin is updated |
| 219 | $warehouse_id = get_option( 'cmbird_zoho_warehouse_id_status' ); |
| 220 | if ( $warehouse_id ) { |
| 221 | add_option( 'cmbird_zoho_location_id_status', $warehouse_id ); |
| 222 | // delete the option when done |
| 223 | delete_option( 'cmbird_zoho_warehouse_id_status' ); |
| 224 | } |
| 225 | // change the post meta key name from "cost_price" to "_cost_price" using wpdb query |
| 226 | global $wpdb; |
| 227 | $wpdb->query( "UPDATE $wpdb->postmeta SET meta_key = '_cost_price' WHERE meta_key = 'cost_price'" ); |
| 228 | // add 'cmbird_' to every option key name that starts with 'zoho_' using wpdb query |
| 229 | $zoho_options = $wpdb->get_results( "SELECT option_name FROM $wpdb->options WHERE option_name LIKE 'zoho_%' OR option_name LIKE 'zi_%'" ); |
| 230 | foreach ( $zoho_options as $zoho_option ) { |
| 231 | if ( strpos( $zoho_option->option_name, 'cmbird_' ) !== 0 ) { |
| 232 | $new_option_name = 'cmbird_' . $zoho_option->option_name; |
| 233 | $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->options SET option_name = %s WHERE option_name = %s", $new_option_name, $zoho_option->option_name ) ); |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 |