commercebird
Last commit date
admin
2 months ago
includes
2 months ago
languages
1 year ago
mu-plugins
5 months ago
templates
2 months ago
vendor
2 months ago
LICENSE
1 year ago
commercebird.php
2 months ago
composer.json
2 months ago
index.php
1 year ago
readme.txt
2 months ago
wp-dependencies.json
8 months ago
commercebird.php
293 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 AI App and use integrations like Zoho Inventory, Zoho CRM, Exact Online and more. Requires a subscription at CommerceBird.com. |
| 7 | * Version: 2.8.2 |
| 8 | * Requires PHP: 8.2 |
| 9 | * Requires Plugins: woocommerce |
| 10 | * Requires at least: 6.5 |
| 11 | * Tested up to: 6.9.4 |
| 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) 2026, CommerceBird |
| 22 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPL-3.0-or-later |
| 23 | * |
| 24 | * WC requires at least: 9.8.0 |
| 25 | * WC tested up to: 10.6.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.8.2' ); |
| 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 | |
| 48 | // Load the Jetpack autoloader instead of vendor/autoload.php. |
| 49 | require_once plugin_dir_path( __FILE__ ) . 'vendor/autoload_packages.php'; |
| 50 | |
| 51 | use Automattic\WooCommerce\Utilities\FeaturesUtil; |
| 52 | use WP\MCP\Core\McpAdapter; |
| 53 | use CommerceBird\Admin\Actions\Sync\ExactOnlineSync; |
| 54 | use CommerceBird\Admin\Actions\Sync\ZohoCRMSync; |
| 55 | use CommerceBird\API\CMBird_APIs; |
| 56 | use CommerceBird\API\CreateOrderWebhook; |
| 57 | use CommerceBird\API\Exact; |
| 58 | use CommerceBird\API\ProductWebhook; |
| 59 | use CommerceBird\API\ShippingWebhook; |
| 60 | use CommerceBird\API\Zoho; |
| 61 | use CommerceBird\Plugin; |
| 62 | |
| 63 | /* |
| 64 | |-------------------------------------------------------------------------- |
| 65 | | Activation, deactivation and uninstall event. |
| 66 | |-------------------------------------------------------------------------- |
| 67 | */ |
| 68 | |
| 69 | register_activation_hook( __FILE__, array( Plugin::class, 'activate' ) ); |
| 70 | register_deactivation_hook( __FILE__, array( Plugin::class, 'deactivate' ) ); |
| 71 | register_uninstall_hook( __FILE__, array( Plugin::class, 'uninstall' ) ); |
| 72 | |
| 73 | /* |
| 74 | |-------------------------------------------------------------------------- |
| 75 | | Load the plugin translations |
| 76 | |-------------------------------------------------------------------------- |
| 77 | | Note: WordPress.org automatically loads translations for plugins since WP 4.6. |
| 78 | | Manual loading not required for plugins hosted on WordPress.org. |
| 79 | */ |
| 80 | |
| 81 | /** Loading Purchase Order Class |
| 82 | * |
| 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 | FeaturesUtil::declare_compatibility( 'product_instance_caching', __FILE__, true ); |
| 109 | } |
| 110 | } |
| 111 | ); |
| 112 | |
| 113 | /* |
| 114 | -------------------------------------------------------------------------- |
| 115 | | Install required dependencies |
| 116 | |-------------------------------------------------------------------------- |
| 117 | */ |
| 118 | add_action( |
| 119 | 'plugins_loaded', |
| 120 | static function () { |
| 121 | // Initialize Quotes module. |
| 122 | CMBIRD_Quotes::init(); |
| 123 | // Only if Premium plan is active via transient subscription_details. |
| 124 | $subscription = get_transient( 'subscription_details' ); |
| 125 | // return if no subscription found. |
| 126 | if ( empty( $subscription ) ) { |
| 127 | return; |
| 128 | } |
| 129 | if ( 'active' === $subscription['status'] ) { |
| 130 | if ( in_array( $subscription['plan'], array( 'premium', 'Premium' ), true ) ) { |
| 131 | if ( class_exists( 'ACF' ) ) { |
| 132 | return; |
| 133 | } |
| 134 | WP_Dependency_Installer::instance( __DIR__ )->run(); |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | ); |
| 139 | |
| 140 | /** |
| 141 | * Hooks for WC Action Scheduler to import or export products |
| 142 | */ |
| 143 | $cmbird_import_products = new CMBIRD_Products_ZI(); |
| 144 | $cmbird_import_pricelist = new CMBIRD_Pricelist_ZI(); |
| 145 | $cmbird_product_class = new CMBIRD_Products_ZI_Export(); |
| 146 | $cmbird_common_class = new CMBIRD_Common_Functions(); |
| 147 | $cmbird_contact_class = new CMBIRD_Contact_ZI(); |
| 148 | $cmbird_category_class = new CMBIRD_Categories_ZI(); |
| 149 | $cmbird_import_pricelist->wc_b2b_groups(); |
| 150 | add_action( 'import_group_items_cron', array( $cmbird_import_products, 'sync_groupitem_recursively' ), 10, 2 ); |
| 151 | add_action( 'import_simple_items_cron', array( $cmbird_import_products, 'sync_items_batch' ), 10, 2 ); |
| 152 | add_action( 'import_variable_product_cron', array( $cmbird_import_products, 'import_variable_product_variations' ), 10, 2 ); |
| 153 | add_action( 'sync_zi_product_cron', array( $cmbird_product_class, 'cmbird_zi_products_prepare_sync' ), 10, 2 ); |
| 154 | add_action( 'sync_zi_pricelist', array( $cmbird_import_pricelist, 'zi_get_pricelist' ), 10, 2 ); |
| 155 | add_action( 'sync_zi_order', array( $cmbird_common_class, 'cmbird_orders_prepare_sync' ), 10, 2 ); |
| 156 | add_action( 'sync_zi_import_contacts', array( $cmbird_contact_class, 'cmbird_get_zoho_contacts' ), 10, 2 ); |
| 157 | // add action to set the zoho rate limit option exceeded to false. |
| 158 | add_action( 'cmbird_common', array( CMBIRD_Common_Functions::class, 'set_zoho_rate_limit_option' ) ); |
| 159 | // Zoho CRM Hooks. |
| 160 | add_action( 'sync_zcrm_order', array( $cmbird_common_class, 'cmbird_orders_prepare_sync' ) ); |
| 161 | add_action( 'sync_zcrm_contact', array( ZohoCRMSync::class, 'cmbird_zcrm_contact_sync' ) ); |
| 162 | // Exact Online Hooks. |
| 163 | add_action( 'cmbird_sync_eo', array( ExactOnlineSync::class, 'sync' ), 10, 3 ); |
| 164 | add_action( 'cmbird_exact_online_sync_orders', array( ExactOnlineSync::class, 'sync_orders_via_cron' ) ); |
| 165 | add_action( 'cmbird_payment_status', array( ExactOnlineSync::class, 'cmbird_payment_status' ), 10, 1 ); |
| 166 | add_action( 'cmbird_eo_get_payment_statuses', array( ExactOnlineSync::class, 'get_payment_status_via_cron' ) ); |
| 167 | // Callback functions for scheduled action to process product or customer chunk. |
| 168 | add_action( |
| 169 | 'cmbird_process_product_chunk', |
| 170 | function ( $args ) { |
| 171 | if ( ! is_array( $args ) || empty( $args['transient_key'] ) ) { |
| 172 | return; |
| 173 | } |
| 174 | $transient_key = $args['transient_key']; |
| 175 | $import_products = $args['import_products'] ?? false; |
| 176 | $chunked_products = get_transient( $transient_key ); |
| 177 | if ( $chunked_products ) { |
| 178 | $sync = new ExactOnlineSync(); |
| 179 | $sync->sync( 'product', $chunked_products, (bool) $import_products ); |
| 180 | // Remove transient after processing. |
| 181 | delete_transient( $transient_key ); |
| 182 | } |
| 183 | }, |
| 184 | 10, |
| 185 | 1 |
| 186 | ); |
| 187 | add_action( |
| 188 | 'cmbird_process_customer_chunk', |
| 189 | function ( $args ) { |
| 190 | if ( ! is_array( $args ) || empty( $args['transient_key'] ) ) { |
| 191 | return; |
| 192 | } |
| 193 | $transient_key = $args['transient_key']; |
| 194 | $import_customers = $args['import_customers'] ?? false; |
| 195 | $chunked_customers = get_transient( $transient_key ); |
| 196 | if ( $chunked_customers ) { |
| 197 | $sync = new ExactOnlineSync(); |
| 198 | $sync->sync( 'customer', $chunked_customers, (bool) $import_customers ); |
| 199 | // Remove transient after processing. |
| 200 | delete_transient( $transient_key ); |
| 201 | } |
| 202 | }, |
| 203 | 10, |
| 204 | 1 |
| 205 | ); |
| 206 | // Zoho CRM Hooks. |
| 207 | add_action( 'init', array( ZohoCRMSync::class, 'refresh_token' ) ); |
| 208 | // Zoho Inventory Cron Hook for importing items. |
| 209 | add_action( 'zi_execute_import_sync', array( Plugin::class, 'dispatch_import_simple_items' ) ); |
| 210 | |
| 211 | // add classes to REST API. |
| 212 | add_action( |
| 213 | 'rest_api_init', |
| 214 | function () { |
| 215 | new Zoho(); |
| 216 | new Exact(); |
| 217 | new ProductWebhook(); |
| 218 | new ShippingWebhook(); |
| 219 | new CreateOrderWebhook(); |
| 220 | new CMBird_APIs(); |
| 221 | $po_controller = new CMBIRD_REST_Shop_Purchase_Controller(); |
| 222 | $po_controller->register_routes(); |
| 223 | } |
| 224 | ); |
| 225 | |
| 226 | add_action( |
| 227 | 'save_post', |
| 228 | function ( $post_id, $post ) { |
| 229 | if ( 'wcb2b_group' === $post->post_type ) { |
| 230 | delete_transient( 'wc_b2b_groups' ); |
| 231 | } |
| 232 | }, |
| 233 | 10, |
| 234 | 2 |
| 235 | ); |
| 236 | |
| 237 | /** |
| 238 | * Perform actions when the plugin is updated |
| 239 | * |
| 240 | * @param string $upgrader_object |
| 241 | * @param array $options |
| 242 | * @return void |
| 243 | */ |
| 244 | add_action( 'upgrader_process_complete', 'cmbird_update_plugin_tasks', 10, 2 ); |
| 245 | |
| 246 | /** |
| 247 | * Perform tasks when the plugin is updated. |
| 248 | * |
| 249 | * @param \WP_Upgrader $upgrader_object - Upgrader object. |
| 250 | * @param array $options - Options array. |
| 251 | * |
| 252 | * @see https://developer.wordpress.org/reference/hooks/upgrader_process_complete/ |
| 253 | */ |
| 254 | function cmbird_update_plugin_tasks( $upgrader_object, $options ) { |
| 255 | $this_plugin = plugin_basename( __FILE__ ); |
| 256 | |
| 257 | if ( 'update' === $options['action'] && 'plugin' === $options['type'] ) { |
| 258 | foreach ( $options['plugins'] as $plugin ) { |
| 259 | if ( $plugin === $this_plugin ) { |
| 260 | // Perform tasks when the plugin is updated. |
| 261 | // Updating meta key from '_cost_price' to '_cogs_total_value'. |
| 262 | global $wpdb; |
| 263 | $table_name = $wpdb->prefix . 'postmeta'; |
| 264 | $wpdb->query( |
| 265 | $wpdb->prepare( |
| 266 | 'UPDATE %s SET meta_key = %s WHERE meta_key = %s', |
| 267 | $table_name, |
| 268 | '_cogs_total_value', |
| 269 | '_cost_price' |
| 270 | ) |
| 271 | ); |
| 272 | break; |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * Instantiate the MCP adapter. Remove this when its included in Woo core. |
| 280 | */ |
| 281 | // 1. Check if MCP Adapter is available |
| 282 | if ( ! class_exists( McpAdapter::class ) ) { |
| 283 | // Handle missing dependency (show admin notice, etc.) |
| 284 | return; |
| 285 | } |
| 286 | // 2. Initialize the adapter |
| 287 | McpAdapter::instance(); |
| 288 | |
| 289 | // 3. Enable mcp_integration feature. |
| 290 | add_filter( 'woocommerce_features', function( $features ) { |
| 291 | $features['mcp_integration'] = true; |
| 292 | return $features; |
| 293 | }); |