*/ class Activecampaign_For_Woocommerce { use Activecampaign_For_Woocommerce_Features_Checker; /** * The Admin class that handles all admin-facing code. * * @since 1.0.0 * @access private * @var Admin $admin The admin class. */ private $admin; /** * The WC order page meta section for AC info. * * @var Activecampaign_For_Woocommerce_Admin_WC_Order_Page */ private $admin_order_page; /** * The Public class that handles all public-facing code. * * @since 1.0.0 * @access private * @var AC_Public $public The public class. */ private $public; /** * The loader that's responsible for maintaining and registering all hooks that power * the plugin. * * @since 1.0.0 * @access protected * @var Loader $loader Maintains and registers all hooks for the plugin. */ protected $loader; /** * The unique identifier of this plugin. * * @since 1.0.0 * @access protected * @var string $plugin_name The string used to uniquely identify this plugin. */ protected $plugin_name; /** * The current version of the plugin. * * @since 1.0.0 * @access protected * @var string $version The current version of the plugin. */ protected $version; /** * Handles all internationalization. * * @var Activecampaign_For_Woocommerce_I18n $i18n The internationalization class. */ private $i18n; /** * Used for triggering cart events. * * @var Cart_Events The cart event class. */ private $cart_events; /** * Used for triggering order finished event. * * @var New_Order_Created The order finished event class. */ private $new_order_created_event; /** * Used for triggering new user registered event. * * @var User_Registered The user registered event class. */ private $user_registered_event; /** * Handles setting the connection id cache. * * @var Set_Connection_Id_Cache_Command The set connection id command class. */ private $set_connection_id_cache_command; /** * Handles creating or updating the connection ID in Hosted. * * @var Create_Or_Update_Connection_Option_Command The create or update connection option command. */ private $create_or_update_connection_option_command; /** * Handles taking from the $_POST object the customers accepts marketing choice and adding * it to the meta of the customer in the DB. * * @var Add_Accepts_Marketing_To_Customer_Meta The add accepts marketing meta to customer class. */ private $add_accepts_marketing_to_customer_meta_command; /** * Handles clearing user meta if certain circumstances are met. * * @var Clear_User_Meta_Command The clear user meta command class. */ private $clear_user_meta_command; /** * Handles sending the guest customer and pending order to AC. * * @var Sync_Guest_Abandoned_Cart_Command The sync guest abandoned cart command class. */ private $sync_guest_abandoned_cart_command; /** * Handles syncing the abandoned carts to AC. * * @var Run_Abandonment_Sync_Command The sync sync runner command class. */ private $run_abandonment_sync_command; /** * Handles plugin upgrade. * * @var Plugin_Upgrade_Command The upgrade command class. */ private $plugin_upgrade_command; /** * Handles historical sync. * * @since 1.5.0 * @var Historical_Sync The historical sync class. */ private $historical_sync; /** * Handles product sync. * * @since 1.9.0 * @var Product_Sync The product sync class. */ private $product_sync; /** * Handles product cleanup. * * @var Product_Cleanup The product cleanup class. */ private $product_cleanup; /** * Handles new order sync. * * @since 1.8.0 * @var New_Order_Sync The new sync class. */ private $new_order_sync; /** * Handles new subscription sync. * * @since 3.0.0 * @var New_Subscription_Sync The subscription sync class. */ private $new_subscription_sync; /** * Customer utility class. * * @since 1.5.0 * @var Customer_Utilities The customer utility class. */ private $customer_utilities; /** * The bulksync repository class. * * @since 1.6.0 * @var Activecampaign_For_Woocommerce_Bulksync_Repository */ private $bulksync_repository; /** * @var Activecampaign_For_Woocommerce_Product_Repository */ private $product_repository; /** * @var Activecampaign_For_Woocommerce_Cofe_Browse_Session_Repository */ private $browse_session_repository; /** * @var Activecampaign_For_Woocommerce_Order_Action_Events */ private $order_events; /** * @var Activecampaign_For_Woocommerce_Subscription_Events */ private $subscription_events; /** * @var Activecampaign_For_Woocommerce_Logger */ private $logger; /** * @var Activecampaign_For_Woocommerce_AC_Contact_Batch_Repository */ private $ac_contact_batch_repository; /** * @var Activecampaign_For_Woocommerce_AC_Contact_Repository */ private $contact_repository; /** * @var Activecampaign_For_Woocommerce_Account_Status_Manager */ private $account_status_manager; /** * Define the core functionality of the plugin. * * Set the plugin name and the plugin version that can be used throughout the plugin. * * @param string $version The current version of the plugin. * @param string $plugin_name The kebab-case name of the plugin. * @param Loader $loader The loader class. * @param Admin $admin The admin class. * @param AC_Public $public The public class. * @param I18n $i18n The internationalization class. * @param Logger $logger The logger. * @param Cart_Events $cart_events The cart event class. * @param Set_Connection_Id_Cache_Command $set_connection_id_cache_command The connection id cache command class. * @param Create_Or_Update_Connection_Option_Command $c_or_u_co_command The connection option command class. * @param Add_Accepts_Marketing_To_Customer_Meta $add_am_to_meta_command The accepts marketing command class. * @param Clear_User_Meta_Command $clear_user_meta_command The clear user meta command class. * @param Sync_Guest_Abandoned_Cart_Command $sync_guest_abandoned_cart_command The sync guest abandoned cart command class. * @param New_Order_Created $new_order_created_event The order finished event class. * @param User_Registered $user_registered_event The user registered event class. * @param Run_Abandonment_Sync_Command $run_abandonment_sync_command The scheduled runner to sync abandonments. * @param Plugin_Upgrade_Command $plugin_upgrade_command The plugin installation and upgrade commands. * @param Historical_Sync $historical_sync The historical sync commands. * @param AC_Contact_Batch_Repository $ac_contact_batch_repository The contact batching sync commands. * @param New_Order_Sync $new_order_sync The new order sync job. * @param Customer_Utilities $customer_utilities The customer utility functions. * @param Activecampaign_For_Woocommerce_Bulksync_Repository $bulksync_repository The bulksync repository. * @param Activecampaign_For_Woocommerce_AC_Contact_Repository $contact_repository The AC contact repository. * @param Product_Repository $product_repository The product repository. * @param Browse_Session_Repository $browse_session_repository The browse session repository. * @param Product_Sync $product_sync The product sync job. * @param Product_Cleanup $product_cleanup The product cleanup job. * @param Admin_Order_Page $admin_order_page The admin order page. * @param Subscription_Events $subscription_events The subscription events. * @param New_Subscription_Sync $new_subscription_sync The subscription sync job. * @param Account_Status_Manager $account_status_manager The AC account status manager. * * @since 1.0.0 */ public function __construct( $version, $plugin_name, Loader $loader, Admin $admin, AC_Public $public, I18n $i18n, Logger $logger, Cart_Events $cart_events, Set_Connection_Id_Cache_Command $set_connection_id_cache_command, Create_Or_Update_Connection_Option_Command $c_or_u_co_command, Add_Accepts_Marketing_To_Customer_Meta $add_am_to_meta_command, Clear_User_Meta_Command $clear_user_meta_command, Sync_Guest_Abandoned_Cart_Command $sync_guest_abandoned_cart_command, New_Order_Created $new_order_created_event, User_Registered $user_registered_event, Run_Abandonment_Sync_Command $run_abandonment_sync_command, Plugin_Upgrade_Command $plugin_upgrade_command, Historical_Sync $historical_sync, AC_Contact_Batch_Repository $ac_contact_batch_repository, New_Order_Sync $new_order_sync, Customer_Utilities $customer_utilities, Bulksync_Repository $bulksync_repository, Contact_Repository $contact_repository, Product_Repository $product_repository, Browse_Session_Repository $browse_session_repository, Product_Sync $product_sync, Product_Cleanup $product_cleanup, Admin_Order_Page $admin_order_page, Subscription_Events $subscription_events, New_Subscription_Sync $new_subscription_sync, Account_Status_Manager $account_status_manager ) { $this->version = $version; $this->plugin_name = $plugin_name; $this->loader = $loader; $this->admin = $admin; $this->public = $public; $this->i18n = $i18n; $this->logger = $logger; $this->cart_events = $cart_events; $this->set_connection_id_cache_command = $set_connection_id_cache_command; $this->create_or_update_connection_option_command = $c_or_u_co_command; $this->add_accepts_marketing_to_customer_meta_command = $add_am_to_meta_command; $this->clear_user_meta_command = $clear_user_meta_command; $this->sync_guest_abandoned_cart_command = $sync_guest_abandoned_cart_command; $this->new_order_created_event = $new_order_created_event; $this->user_registered_event = $user_registered_event; $this->run_abandonment_sync_command = $run_abandonment_sync_command; $this->plugin_upgrade_command = $plugin_upgrade_command; $this->historical_sync = $historical_sync; $this->ac_contact_batch_repository = $ac_contact_batch_repository; $this->new_order_sync = $new_order_sync; $this->customer_utilities = $customer_utilities; $this->bulksync_repository = $bulksync_repository; $this->contact_repository = $contact_repository; $this->product_sync = $product_sync; $this->product_cleanup = $product_cleanup; $this->product_repository = $product_repository; $this->browse_session_repository = $browse_session_repository; $this->admin_order_page = $admin_order_page; $this->order_events = new Order_Events(); $this->subscription_events = new Subscription_Events(); $this->new_subscription_sync = $new_subscription_sync; $this->account_status_manager = $account_status_manager; } /** * Define the locale for this plugin for internationalization. * * Uses the Activecampaign_For_Woocommerce_I18n class in order to set the domain and to register the hook * with WordPress. * * @throws Exception Thrown when Container definitions are missing. * @since 1.0.0 * @access private */ private function set_locale() { // We are already inside plugins_loaded here, so a plugins_loaded hook would never fire. $this->loader->add_action( 'init', $this->i18n, 'load_plugin_textdomain' ); } /** * On plugin update these hooks should run. */ private function plugin_updates() { $installed_version = get_option( 'activecampaign_for_woocommerce_db_version' ); $db_version = ACTIVECAMPAIGN_FOR_WOOCOMMERCE_DB_VERSION; if ( isset( $installed_version ) && $installed_version === $db_version ) { return; } $this->loader->add_action( 'upgrader_post_install', $this->plugin_upgrade_command, 'execute', 1 ); $this->loader->add_action( 'activecampaign_for_woocommerce_verify_tables', $this->plugin_upgrade_command, 'verify_table', 1 ); $this->loader->add_action( 'upgrader_process_complete', $this->plugin_upgrade_command, 'execute', 1 ); $this->loader->add_action( 'update_plugin_complete_actions', $this->plugin_upgrade_command, 'execute', 1 ); // We are already inside plugins_loaded here; init is the next hook that runs on every request. $this->loader->add_action( 'init', $this->plugin_upgrade_command, 'execute', 1 ); } /** * Register Events to be executed on different actions. * * @throws Exception Thrown when Container definitions are missing. * @since 1.0.0 * @access private */ private function define_event_hooks() { // If we can't get the config stop this function if ( ! $this->is_configured() ) { return; } $this->loader->add_filter( 'cron_schedules', $this->admin, 'cron_add_ten_minute' ); // Cart actions $this->loader->add_action( 'woocommerce_update_cart_action_cart_updated', $this->cart_events, 'cart_updated' ); $this->loader->add_action( 'woocommerce_add_to_cart', $this->cart_events, 'cart_updated' ); $this->loader->add_action( 'woocommerce_cart_item_removed', $this->cart_events, 'cart_updated' ); // Quantity changes outside the classic cart form (Blocks cart / Store API, AJAX side carts) only fire this // hook; the handler skips the classic form submit, which is covered by the filter above. $this->loader->add_action( 'woocommerce_after_cart_item_quantity_update', $this->cart_events, 'cart_quantity_updated', 10, 3 ); $this->loader->add_action( 'woocommerce_cart_emptied', $this->cart_events, 'cart_emptied' ); $this->loader->add_action( 'woocommerce_checkout_update_order_meta', $this->cart_events, 'cart_emptied', 60 ); // end cart actions // order created actions // New order created via any method // 'woocommerce_new_order', $order->get_id(), $order $this->loader->add_action( 'woocommerce_new_order', $this->new_order_created_event, 'ac_woocommerce_new_order', 20, 2 ); /** * Payment has been processed * 'woocommerce_payment_complete', $this->get_id(), $transaction_id */ $this->loader->add_action( 'woocommerce_payment_complete', $this->new_order_created_event, 'ac_woocommerce_payment_complete', 20, 2 ); /** * Action hook fired after an order is created used to add custom meta to the order. * woocommerce_checkout_update_order_meta WC hook * $order_id, $data * * @TODO use woocommerce_checkout_order_processed instead? */ $this->loader->add_action( 'woocommerce_checkout_update_order_meta', // Action hook fired after an order is created used to add custom meta to the order. $this->new_order_created_event, 'ac_woocommerce_checkout_update_order_meta', 20, 2 ); // New checkout order created via checkout, uses order object. $this->loader->add_action( 'woocommerce_checkout_order_created', $this->new_order_created_event, 'ac_woocommerce_checkout_order_created', 20, 1 ); // End order complete hooks $this->loader->add_action( 'user_register', $this->user_registered_event, 'trigger' ); } /** * Registers commands related to the admin portion of the WordPress site with * action hooks. * * @since 1.2.1 * @access private */ private function define_admin_commands() { if ( $this->is_configured() && $this->is_connected() && $this->verify_ac_features( 'historical' ) ) { $this->define_historical_sync_commands(); } if ( $this->is_configured() && $this->is_connected() && $this->verify_ac_features( 'product' ) ) { $this->define_product_sync(); $this->define_product_cleanup(); } /** * By including priority 1, we ensure that the connection id caching occurs * before the create or update command. */ $this->loader->add_action( 'activecampaign_for_woocommerce_admin_settings_updated', $this->set_connection_id_cache_command, 'execute', 1 ); $this->loader->add_action( 'activecampaign_for_woocommerce_admin_settings_updated', $this->create_or_update_connection_option_command, 'execute' ); $this->loader->add_filter( 'activecampaign_for_woocommerce_admin_settings_updated', $this->clear_user_meta_command, 'execute' ); $this->loader->add_action( 'activecampaign_for_woocommerce_update_connection_options', $this->create_or_update_connection_option_command, 'execute_update_all_options' ); $this->loader->add_action( 'activecampaign_for_woocommerce_retrieve_connection_options', $this->create_or_update_connection_option_command, 'execute_retrieve_all_options' ); $this->loader->add_action( 'activecampaign_for_woocommerce_ready_new_order', $this->new_order_created_event, 'ac_woocommerce_new_order', 20, 2 ); $this->loader->add_action( 'activecampaign_for_woocommerce_download_log_data', $this->admin, 'download_log_data', 1 ); // This is called on trashing an order or a product $this->loader->add_action( 'before_delete_post', $this->admin, 'remove_deleted_order', 1 ); // This is called on trashing an order or a product $this->loader->add_action( 'wp_trash_post', $this->admin, 'remove_deleted_order', 1 ); // This is called on trashing an order or a product $this->loader->add_action( 'wp_trash_post', $this->order_events, 'execute_order_deleted', 1 ); // With HPOS orders are not posts; WooCommerce fires its own hooks when an order is trashed or deleted. $this->loader->add_action( 'woocommerce_before_trash_order', $this->admin, 'remove_deleted_order', 1 ); $this->loader->add_action( 'woocommerce_before_delete_order', $this->admin, 'remove_deleted_order', 1 ); $this->loader->add_action( 'woocommerce_before_trash_order', $this->order_events, 'execute_order_deleted', 1 ); // This is called on trashing an order or a product $this->loader->add_action( 'wp_trash_post', $this->admin, 'remove_deleted_product', 2 ); // This is called after trashing an order or a product $this->loader->add_action( 'trashed_post', $this->admin, 'remove_deleted_product', 2 ); $this->loader->add_action( 'woocommerce_before_delete_product_variation', $this->admin, 'remove_deleted_product', 1 ); $this->loader->add_action( 'before_delete_post', $this->admin, 'remove_deleted_product', 2 ); // When a product is restored $this->loader->add_action( 'untrashed_post', $this->admin, 'restore_product', 1 ); // When a product is created $this->loader->add_action( 'wp_create_post', $this->admin, 'restore_product', 1 ); // On order update $this->loader->add_action( 'woocommerce_process_shop_order_meta', $this->order_events, 'execute_order_updated', 90, 1 ); $this->loader->add_action( 'woocommerce_order_status_changed', $this->order_events, 'execute_order_status_changed', 10, 3 ); // needed for stripe // TODO: Might need option to disable this $this->loader->add_action( 'woocommerce_order_edit_status', $this->order_events, 'execute_order_edit_status_event', 20, 2 ); // TODO: Probably want to disable this if stripe is not installed and as a debug option $this->loader->add_action( 'wc_gateway_stripe_process_response', $this->order_events, 'execute_order_updated_stripe', 9, 2 ); // On order refund $this->loader->add_action( 'woocommerce_order_refunded', $this->order_events, 'execute_order_updated_refund', 20, 2 ); $this->loader->add_action( ACTIVECAMPAIGN_FOR_WOOCOMMERCE_RUN_NEW_ORDER_SYNC_NAME, $this->new_order_sync, 'execute', 9, 2 ); $this->loader->add_action( 'activecampaign_for_woocommerce_admin_sync_single_order_active', $this->new_order_sync, 'execute', 1, 2 ); $this->loader->add_action( 'activecampaign_for_woocommerce_admin_sync_single_order_status', $this->new_order_sync, 'execute_from_status', 1, 2 ); $this->loader->add_action( 'activecampaign_for_woocommerce_auto_unblock_account', $this->admin, 'clear_blocked_account', 3, 2 ); } private function define_product_sync() { if ( ! $this->is_configured() || ! $this->is_connected() ) { return; } /** * Product sync actions */ $this->loader->add_action( 'activecampaign_for_woocommerce_run_product_sync', $this->product_sync, 'execute', 1, 2 ); $this->loader->add_action( 'activecampaign_for_woocommerce_run_single_product_sync', $this->product_sync, 'single_product_cofe_sync', 1, 1 ); $this->loader->add_action( 'activecampaign_for_woocommerce_run_single_product_soft_delete', $this->product_sync, 'single_product_cofe_soft_delete', 1, 1 ); $this->loader->add_action( 'activecampaign_for_woocommerce_run_sync_connection', $this->product_sync, 'run_sync_connection', 1, 1 ); $this->loader->add_action( 'activecampaign_for_woocommerce_build_product_sync_schedules', $this->product_sync, 'build_product_sync_schedules', 1, 2 ); $this->loader->add_action( 'woocommerce_product_set_stock_status', $this->product_sync, 'save_or_update_product_status', 12, 3 ); } private function define_product_cleanup() { $this->loader->add_action( 'activecampaign_for_woocommerce_run_product_cleanup', $this->product_cleanup, 'execute', 1, 2 ); if ( ! $this->is_configured() || ! $this->is_connected() ) { return; } $this->loader->add_action( 'activecampaign_for_woocommerce_build_product_cleanup_schedules', $this->product_cleanup, 'build_product_cleanup_schedules', 1, 2 ); } private function define_historical_sync_commands() { if ( ! $this->is_configured() || ! $this->is_connected() ) { return; } $this->loader->add_action( 'activecampaign_for_woocommerce_admin_sync_single_order_historical', $this->historical_sync, 'run_sync', 5, 2 ); $this->loader->add_action( ACTIVECAMPAIGN_FOR_WOOCOMMERCE_RUN_HISTORICAL_RECUR, $this->historical_sync, 'run_sync', 5, 2 ); $this->loader->add_action( ACTIVECAMPAIGN_FOR_WOOCOMMERCE_RUN_HISTORICAL_RECUR, $this->historical_sync, 'run_historical_sync_contacts', 6, 2 ); $this->loader->add_action( 'activecampaign_for_woocommerce_prep_historical_data', $this->historical_sync, 'prep_data', 1 ); $this->loader->add_action( 'add_meta_boxes', $this->admin_order_page, 'order_edit_meta_box', 20, 3 ); $this->loader->add_action( 'activecampaign_for_woocommerce_ready_existing_historical_data', $this->historical_sync, 'ready_existing_data', 1 ); $this->loader->add_action( 'activecampaign_for_woocommerce_run_historical_sync_contacts', $this->historical_sync, 'prep_historical_sync_contacts', 3, 2 ); $this->loader->add_action( ACTIVECAMPAIGN_FOR_WOOCOMMERCE_RUN_HISTORICAL_SYNC_NAME, $this->historical_sync, 'run_sync', 1, 2 ); $this->loader->add_action( 'wp_ajax_activecampaign_for_woocommerce_schedule_bulk_historical_sync', $this->admin, 'schedule_bulk_historical_sync' ); $this->loader->add_action( 'wp_ajax_activecampaign_for_woocommerce_check_historical_sync_status', $this->admin, 'check_historical_sync_status' ); $this->loader->add_action( 'wp_ajax_activecampaign_for_woocommerce_cancel_historical_sync', $this->admin, 'stop_historical_sync' ); $this->loader->add_action( 'wp_ajax_activecampaign_for_woocommerce_pause_historical_sync', $this->admin, 'stop_historical_sync' ); $this->loader->add_action( 'wp_ajax_activecampaign_for_woocommerce_reset_historical_sync', $this->admin, 'reset_historical_sync' ); } private function define_subscription_commands() { // Do not run if subscription is disabled or not installed if ( function_exists( 'is_plugin_active' ) && ! is_plugin_active( 'woocommerce-subscriptions/woocommerce-subscriptions.php' ) ) { return; } $admin_subscription_page = new Admin_Subscription_Page(); $this->loader->add_action( 'add_meta_boxes', $admin_subscription_page, 'subscription_edit_meta_box', 20, 3 ); $this->loader->add_action( 'activecampaign_for_woocommerce_admin_sync_single_subscription_active', $this->new_subscription_sync, 'execute', 1, 2 ); $this->loader->add_action( 'activecampaign_for_woocommerce_admin_sync_single_subscription_historical', $this->new_subscription_sync, 'execute_one_historical', 5, 2 ); // This is for new subscriptions. It is working. $this->loader->add_action( ACTIVECAMPAIGN_FOR_WOOCOMMERCE_RUN_NEW_ORDER_SYNC_NAME, $this->new_subscription_sync, 'execute', 8, 2 ); $this->loader->add_action( 'activecampaign_for_woocommerce_update_subscription', $this->new_subscription_sync, 'execute', 2, 2 ); $this->loader->add_action( 'woocommerce_subscription_status_updated', $this->subscription_events, 'execute_woocommerce_subscription_status_updated', 2, 3 ); $this->loader->add_action( 'activecampaign_for_woocommerce_route_order_update_to_subscription', $this->subscription_events, 'execute_woocommerce_subscription_status_updated', 2, 3 ); $this->loader->add_action( 'woocommerce_scheduled_subscription_trial_end', $this->subscription_events, 'execute_woocommerce_scheduled_subscription_trial_end', 2, 1 ); $this->loader->add_action( 'woocommerce_checkout_subscription_created', $this->subscription_events, 'execute_woocommerce_checkout_subscription_created', 2, 3 ); $this->loader->add_action( 'wp_ajax_activecampaign_for_woocommerce_run_single_subscription_sync', $admin_subscription_page, 'ac_ajax_sync_single_record', 1 ); // If a subscription is incorrectly listed as an order send it to this event $this->loader->add_action( 'activecampaign_for_woocommerce_miscat_order_to_subscription', $this->subscription_events, 'trigger_order_to_subscription', 2, 2 ); $this->loader->add_action( 'activecampaign_for_woocommerce_miscat_order_to_subscription_historical', $this->subscription_events, 'trigger_historical_order_to_historical_subscription', 2, 2 ); // $this->loader->add_action( // 'woocommerce_subscription_payment_complete', // $this->subscription_events, // 'execute_woocommerce_subscription_payment_complete' // ); // $this->loader->add_action( // 'subscriptions_created_for_order', // $this->subscription_events, // 'execute_subscription_created_for_order' // ); } /** * Registers commands related to the public-facing portion of the WordPress site with * action hooks. * * @since 1.2.1 * @access private */ private function define_public_commands() { if ( ! $this->is_configured() ) { return; } // Order checkout finished, order created, transition cart to order $this->loader->add_action( 'woocommerce_checkout_create_order', $this->cart_events, 'cart_to_order_transition' ); // For blocks $this->loader->add_action( 'woocommerce_store_api_checkout_order_processed', $this->cart_events, 'cart_to_order_transition' ); $this->define_subscription_commands(); $this->loader->add_action( 'activecampaign_for_woocommerce_load_sitetracking', $this->admin, 'activecampaign_load_sitetracking' ); // custom hook for hourly abandoned cart $this->loader->add_action( 'activecampaign_for_woocommerce_cart_updated_recurring_event', $this->run_abandonment_sync_command, 'abandoned_cart_hourly_task' ); } /** * Register all of the hooks related to the admin area functionality * of the plugin. * * @throws Exception Thrown when Container definitions are missing. * @since 1.0.0 * @access private */ private function define_admin_hooks() { $this->loader->add_action( 'admin_enqueue_scripts', $this->admin, 'enqueue_styles_scripts' ); // add menu item to bottom of WooCommerce menu $this->loader->add_action( 'admin_menu', $this->admin, 'add_admin_page', 10 ); $this->loader->add_action( 'admin_menu', $this->admin, 'add_admin_entitlements', 11 ); $this->loader->add_action( 'admin_post_' . ACTIVECAMPAIGN_FOR_WOOCOMMERCE_PLUGIN_NAME_SNAKE . '_settings', $this->admin, 'handle_settings_post' ); $this->loader->add_filter( 'plugin_action_links_' . ACTIVECAMPAIGN_FOR_WOOCOMMERCE_PLUGIN_BASE_NAME, $this->admin, 'add_plugin_settings_link' ); $this->loader->add_action( 'rest_api_init', $this->admin, 'active_campaign_register_settings_api', 1 ); $disable_notice = 0; if ( get_option( 'activecampaign_for_woocommerce_dismiss_error_notice' ) ) { $dismiss_setting = json_decode( get_option( 'activecampaign_for_woocommerce_dismiss_error_notice' ), 'array' ); $user_id = get_current_user_id(); if ( isset( $dismiss_setting[ $user_id ] ) && 1 === $dismiss_setting[ $user_id ] ) { $disable_notice = 1; } } if ( ! $disable_notice ) { $this->loader->add_action( 'admin_notices', $this->admin, 'error_admin_notice' ); } $disable_plugin_notice = 0; if ( get_option( 'activecampaign_for_woocommerce_dismiss_plugin_notice' ) ) { $notice_setting = json_decode( get_option( 'activecampaign_for_woocommerce_dismiss_plugin_notice' ), 'array' ); $user_id = get_current_user_id(); if ( isset( $notice_setting[ $user_id ] ) && 1 === $notice_setting[ $user_id ] ) { $disable_plugin_notice = 1; } } if ( ! $this->is_configured() ) { if ( ! $disable_plugin_notice ) { $this->loader->add_filter( 'admin_notices', $this->admin, 'please_configure_plugin_notice', 10, 1 ); } } if ( $this->is_connected() ) { $this->loader->add_action( 'activecampaign_for_woocommerce_run_manual_abandonment_sync', $this->run_abandonment_sync_command, 'abandoned_cart_manual_run' ); $this->loader->add_action( 'activecampaign_for_woocommerce_run_force_row_abandonment_sync', $this->run_abandonment_sync_command, 'force_sync_row' ); $this->loader->add_action( 'activecampaign_for_woocommerce_run_manual_abandonment_delete', $this->run_abandonment_sync_command, 'abandoned_cart_manual_delete' ); } } /** * Rejects the plugin's admin-ajax.php / admin-post.php requests from users who cannot manage WooCommerce. * * Runs on admin_init, i.e. before the wp_ajax_* / admin_post_* hook of the individual handler. */ public function require_capability_for_admin_ajax() { if ( ! wp_doing_ajax() && ( ! isset( $GLOBALS['pagenow'] ) || 'admin-post.php' !== $GLOBALS['pagenow'] ) ) { return; } // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- only the action name is read, to decide whether this request is ours. $action = isset( $_REQUEST['action'] ) ? sanitize_key( wp_unslash( $_REQUEST['action'] ) ) : ''; $is_plugin_action = 0 === strpos( $action, 'activecampaign_for_woocommerce_' ) || in_array( $action, array( 'api_test', 'account-status-blockade-remove' ), true ); // The guest abandoned-cart sync is a storefront call (wp_ajax_nopriv) protected by its own nonce. if ( ! $is_plugin_action || 'activecampaign_for_woocommerce_cart_sync_guest' === $action ) { return; } if ( ! current_user_can( 'manage_woocommerce' ) ) { wp_send_json_error( 'Forbidden', 403 ); } } /** * Defines the admin ajax calls */ public function define_admin_ajax_hooks() { // Every admin AJAX / admin-post action of the plugin is for store managers only; checked once, before any handler runs. $this->loader->add_action( 'admin_init', $this, 'require_capability_for_admin_ajax' ); $this->loader->add_action( 'wp_ajax_api_test', $this->admin, 'handle_api_test' ); $this->loader->add_action( 'wp_ajax_account-status-blockade-remove', $this->admin, 'handle_account_blockade_removal' ); $this->loader->add_action( 'wp_ajax_activecampaign_for_woocommerce_dismiss_error_notice', $this->admin, 'update_dismiss_error_notice_option' ); $this->loader->add_action( 'wp_ajax_activecampaign_for_woocommerce_load_connection_block', $this->admin, 'load_connection_block' ); $this->loader->add_action( 'wp_ajax_activecampaign_for_woocommerce_dismiss_plugin_notice', $this->admin, 'update_dismiss_plugin_notice_option' ); $this->loader->add_action( 'wp_ajax_activecampaign_for_woocommerce_clear_error_log', $this->admin, 'clear_error_logs' ); $this->loader->add_action( 'wp_ajax_activecampaign_for_woocommerce_clear_all_settings', $this->admin, 'handle_clear_plugin_settings' ); // This is for repair connection button $this->loader->add_action( 'wp_ajax_activecampaign_for_woocommerce_reset_connection_id', $this->admin, 'handle_reset_connection_id' ); $this->loader->add_action( 'wp_ajax_activecampaign_for_woocommerce_create_connection', $this->admin, 'add_new_connection' ); $this->loader->add_action( 'wp_ajax_activecampaign_for_woocommerce_update_connection', $this->admin, 'update_existing_connection' ); $this->loader->add_action( 'wp_ajax_activecampaign_for_woocommerce_select_connection', $this->admin, 'select_connection' ); $this->loader->add_action( 'wp_ajax_activecampaign_for_woocommerce_delete_connection', $this->admin, 'delete_existing_connection' ); $this->loader->add_action( 'wp_ajax_activecampaign_for_woocommerce_resync_features', $this->admin, 'clear_entitlements_cache' ); // Anything below this is disabled if the plugin is not configured if ( ! $this->is_configured() || ! $this->is_connected() ) { return; } $this->loader->add_action( 'wp_ajax_activecampaign_for_woocommerce_manual_abandonment_sync', $this->admin, 'handle_abandon_cart_sync' ); $this->loader->add_action( 'wp_ajax_activecampaign_for_woocommerce_reset_failed_abandonment_sync', $this->admin, 'handle_reset_abandon_cart_sync' ); $this->loader->add_action( 'wp_ajax_activecampaign_for_woocommerce_delete_abandoned_cart_row', $this->admin, 'handle_abandon_cart_delete' ); $this->loader->add_action( 'wp_ajax_activecampaign_for_woocommerce_sync_abandoned_cart_row', $this->admin, 'handle_abandon_cart_force_row_sync' ); $this->loader->add_action( 'wp_ajax_activecampaign_for_woocommerce_schedule_product_sync', $this->admin, 'run_product_sync' ); $this->loader->add_action( 'wp_ajax_activecampaign_for_woocommerce_check_product_sync_status', $this->admin, 'check_product_sync_status' ); $this->loader->add_action( 'wp_ajax_activecampaign_for_woocommerce_schedule_product_cleanup', $this->admin, 'run_product_cleanup' ); $this->loader->add_action( 'wp_ajax_activecampaign_for_woocommerce_check_product_cleanup_status', $this->admin, 'check_product_cleanup_status' ); $this->loader->add_action( 'wp_ajax_activecampaign_for_woocommerce_cancel_product_cleanup', $this->product_cleanup, 'handle_cancel_cleanup', 1, 1 ); $this->loader->add_action( 'wp_ajax_activecampaign_for_woocommerce_reset_product_cleanup', $this->product_cleanup, 'handle_reset_cleanup_status', 1, 1 ); $this->loader->add_action( 'wp_ajax_activecampaign_for_woocommerce_cancel_product_sync', $this->product_sync, 'handle_cancel_sync', 1, 1 ); $this->loader->add_action( 'wp_ajax_activecampaign_for_woocommerce_reset_product_sync', $this->product_sync, 'handle_reset_sync_status', 1, 1 ); $this->loader->add_action( 'wp_ajax_activecampaign_for_woocommerce_run_single_record_sync', $this->admin_order_page, 'ac_ajax_sync_single_record', 1 ); $this->loader->add_action( 'wp_ajax_activecampaign_for_woocommerce_create_status_mapping', $this->admin, 'handle_status_mapping_actions', 1 ); $this->loader->add_action( 'wp_ajax_activecampaign_for_woocommerce_delete_status_mapping', $this->admin, 'handle_status_mapping_actions', 1 ); } /** * Register all of the hooks related to the public-facing functionality * of the plugin. * * @throws Exception Thrown when Container definitions are missing. * @since 1.0.0 * @access private */ private function define_public_hooks() { // end this function if not configured if ( ! $this->is_configured() ) { return; } /** * If the site admin has not yet configured their plugin, bail out before * registering any public commands since they will not work without the * plugin being configured. */ $ops = $this->admin->get_local_settings(); $this->loader->add_action( 'wp_enqueue_scripts', $this->public, 'enqueue_styles_scripts' ); // Verify the checkbox should display if ( empty( $ops['checkbox_display_option'] ) || empty( $ops['optin_checkbox_text'] ) || 'not_visible' === $ops['checkbox_display_option'] ) { $this->logger->debug( 'Checkbox actions cannot be run. checkbox_display_option and/or optin_checkbox_text are not defined, or the checkbox is set to not_visible.' ); return; } $this->loader->add_action( 'woocommerce_checkout_create_order', $this->add_accepts_marketing_to_customer_meta_command, 'execute', 90 ); $this->loader->add_action( 'woocommerce_store_api_checkout_order_processed', $this->add_accepts_marketing_to_customer_meta_command, 'execute' ); // Both checkout flavours are always wired up: the additional field only exists in the Checkout block and the // template hooks only fire in the classic (shortcode) checkout, so whichever one the theme or a page builder // renders gets the checkbox - nothing is guessed from the checkout page content at bootstrap. $this->loader->add_action( 'woocommerce_init', $this->public, 'handle_woocommerce_blocks_checkout_form', 5 ); // Classic checkout: the first hook that fires wins (the handler renders once per request). Nothing from // review-order.php / payment.php - that fragment is re-rendered on every address change and would reset the box. foreach ( array( 'woocommerce_after_checkout_billing_form', 'woocommerce_checkout_after_customer_details', 'woocommerce_after_checkout_form' ) as $hook ) { $this->loader->add_action( $hook, $this->public, 'handle_woocommerce_checkout_form', 5 ); } } /** * Defines the public ajax calls */ public function define_public_ajax_hooks() { if ( ! $this->is_configured() ) { return; } $this->loader->add_action( 'wp_ajax_activecampaign_for_woocommerce_cart_sync_guest', $this->sync_guest_abandoned_cart_command, 'execute' ); $this->loader->add_action( 'wp_ajax_nopriv_activecampaign_for_woocommerce_cart_sync_guest', $this->sync_guest_abandoned_cart_command, 'execute' ); } /** * Run the loader to execute all of the hooks with WordPress. * * Load the dependencies, define the locale, and set the hooks for the admin area and * the public-facing side of the site. * * @throws Exception Thrown when Container definitions are missing. * @since 1.0.0 */ public function run() { if ( ! $this->logger ) { $this->logger = new Logger(); } $this->set_locale(); $this->plugin_updates(); $this->define_event_hooks(); $this->define_admin_commands(); $this->define_admin_hooks(); $this->define_admin_ajax_hooks(); $this->define_public_commands(); $this->define_public_hooks(); $this->define_public_ajax_hooks(); $this->loader->run(); } /** * The name of the plugin used to uniquely identify it within the context of * WordPress and to define internationalization functionality. * * @return string The name of the plugin. * @since 1.0.0 */ public function get_plugin_name() { return $this->plugin_name; } /** * The reference to the class that orchestrates the hooks with the plugin. * * @return Activecampaign_For_Woocommerce_Loader Orchestrates the hooks of the plugin. * @since 1.0.0 */ public function get_loader() { return $this->loader; } /** * Retrieve the version number of the plugin. * * @return string The version number of the plugin. * @since 1.0.0 */ public function get_version() { return $this->version; } /** * Verify our plugin is configured * * @return bool */ private function is_configured() { $ops = $this->admin->get_local_settings(); if ( ! $ops || ! $ops['api_key'] || ! $ops['api_url'] ) { return false; } return true; } /** * Verify our plugin has connection_id * * @return bool */ private function is_connected() { if ( ! isset( $this->admin->get_connection_storage()['connection_id'] ) || empty( $this->admin->get_connection_storage()['connection_id'] ) ) { return false; } return true; } }