acrExecuteCron( $hookName, 'manual-run' ) ) { wp_redirect( 'admin.php?page=wc-settings&tab=acr_settings§ion=acr_settings_help_section&msg=' . $msgID . '&cron=' . $hookName . '&debug=true' ); }else{ wp_redirect( 'admin.php?page=wc-settings&tab=acr_settings§ion=acr_settings_help_section&msg=error&cron=' . $hookName . '&debug=true' ); } }elseif( isset( $_GET[ 'action'] ) && $_GET[ 'action'] == 'acr_manual_run_clear_all_emails' ){ check_admin_referer( 'acr-manual-' . $_GET[ 'action'] ); if( $this->acrExecuteCron( ACR_EMAIL_SENDER_CRON, 'unschedule-hook' ) ){ wp_redirect( 'admin.php?page=wc-settings&tab=acr_settings§ion=acr_settings_help_section&msg=3&debug=true' ); }else{ wp_redirect( 'admin.php?page=wc-settings&tab=acr_settings§ion=acr_settings_help_section&msg=error&debug=true' ); } }elseif( isset( $_GET[ 'action'] ) && $_GET[ 'action'] == 'acr_manual_run_clear_all_abandoned_carts' ){ check_admin_referer( 'acr-manual-' . $_GET[ 'action'] ); if( $this->acrSettingsClearAllAbandonedCarts() ){ wp_redirect( 'admin.php?page=wc-settings&tab=acr_settings§ion=acr_settings_help_section&msg=4&debug=true' ); }else{ wp_redirect( 'admin.php?page=wc-settings&tab=acr_settings§ion=acr_settings_help_section&msg=error&debug=true' ); } } do_action( 'acr_settings_run_cron_manually' ); } /** * Execute cron by action * * @param string $hookName * @param string $action * * @return boolean * @since 1.0.0 */ public function acrExecuteCron( $hookname, $action ) { $metaKey = ''; $continue = false; if( $hookname == ACR_ABANDONED_CART_CRON ){ $acrAbandonedStatuses = get_option( 'acr_general_status_considered_abandoned' ); $postType = 'shop_order'; $metaKey = ACR_ABANDONED_CART_CRON_ARGS; }elseif( $hookname == ACR_EMAIL_SENDER_CRON ){ $metaKey = ACR_EMAIL_SENDER_CRON_ARGS; }elseif( $hookname == ACR_CANCELLED_CART_CRON ){ $metaKey = ACR_CANCELLED_CART_CRON_ARGS; } $sqlArgs = array( 'post_type' => ! empty( $postType ) ? $postType : ACR_CPT_NAME, 'post_status' => ! empty( $acrAbandonedStatuses ) ? $acrAbandonedStatuses : 'acr-not-recovered', 'meta_query' => array( array( 'key' => $metaKey, 'value' => '', 'compare' => '!=', ) ) ); $items = new WP_Query( $sqlArgs ); if ( $items->have_posts() ) { $continue = true; while ( $items->have_posts() ) { $items->the_post(); $cartID = get_the_id(); $args = get_post_meta( $cartID, $metaKey, true ); switch ( $action ) { case 'manual-run': if( $hookname == ACR_EMAIL_SENDER_CRON ){ // Emails can have multiple schedules so we need to loop foreach ( $args as $key => $arg ) { // Unschedule to avoid duplicate $timestamp = wp_next_scheduled( $hookname, $arg ); wp_unschedule_event( $timestamp, $hookname, $arg ); // Running it now wp_schedule_single_event( current_time( 'timestamp', true ) - 1, $hookname, $arg ); } }else{ // Unschedule to avoid duplicate $timestamp = wp_next_scheduled( $hookname, $args ); wp_unschedule_event( $timestamp, $hookname, $args ); // Running it now wp_schedule_single_event( current_time( 'timestamp', true ) - 1, $hookname, $args ); } break; case 'unschedule-hook': if( $hookname == ACR_EMAIL_SENDER_CRON ){ foreach ( $args as $key => $arg ) { // Unschedule $timestamp = wp_next_scheduled( $hookname, $arg ); wp_unschedule_event( $timestamp, $hookname, $arg ); foreach ( $arg[ 1 ] as $emailKey => $email ) { $acrStatus = get_post_meta( $cartID, '_acr_email_status', true ); $acrStatus[ $emailKey ][ 'status' ] = 'failed'; $acrStatus[ $emailKey ][ 'time_failed' ] = current_time( 'Y-m-d H:i:s', true ); update_post_meta( $cartID, '_acr_email_status', $acrStatus ); } } }else{ // Unschedule $timestamp = wp_next_scheduled( $hookname, $args ); wp_unschedule_event( $timestamp, $hookname, $args ); } break; default: break; } // Delete post meta delete_post_meta( $cartID, $metaKey ); } } do_action( 'acr_settings_run_cron', $continue, $hookname, $metaKey ); return $continue; } /** * Option to clear all abandoned carts and remove any attached/running cron on the cart object. * * @return boolean * @since 1.0.0 */ public function acrSettingsClearAllAbandonedCarts(){ $continue = false; $args = array( 'post_type' => ACR_CPT_NAME, 'post_status' => array( 'acr-not-recovered' ) ); $items = new WP_Query( $args ); if ( $items->have_posts() ) { $continue = true; while ( $items->have_posts() ) { $items->the_post(); $cartID = get_the_id(); $this->acrUnscheduleCronEventsByCartID( $cartID ); // We now remove the cart object wp_delete_post( $cartID, true ); } } do_action( 'acr_settings_clear_all_abandoned_carts', $continue, $items ); return apply_filters( 'acr_clear_all_abandoned_carts', $continue ); } /** * Unschedule cron events attached on the Cart Object ID. * * @param int $cartID * * @return boolean * @since 1.0.0 */ public function acrUnscheduleCronEventsByCartID( $cartID ){ $emailSenderArgs = get_post_meta( $cartID, ACR_EMAIL_SENDER_CRON_ARGS, true ); $cancelledCartArgs = get_post_meta( $cartID, ACR_CANCELLED_CART_CRON_ARGS, true ); // For email sender cron if( ! empty( $emailSenderArgs ) ) { // Emails can have multiple schedules so we need to loop foreach ( $emailSenderArgs as $key => $args ) { // Unschedule to avoid duplicate $timestamp = wp_next_scheduled( ACR_EMAIL_SENDER_CRON, $args ); wp_unschedule_event( $timestamp, ACR_EMAIL_SENDER_CRON, $args ); } } // For cancelled cart cron if( ! empty( $cancelledCartArgs ) ) { // Unschedule to avoid duplicate $timestamp = wp_next_scheduled( ACR_CANCELLED_CART_CRON, $cancelledCartArgs ); wp_unschedule_event( $timestamp, ACR_CANCELLED_CART_CRON, $cancelledCartArgs ); } do_action( 'acr_unschedule_cron_events_by_cart_id', $cartID, $emailSenderArgs, $cancelledCartArgs ); } /** * Add admin notices for manual cron * * @since 1.0.0 */ public function acrAddAdminNotices() { if( isset( $_GET[ 'tab' ] ) && $_GET[ 'tab' ] == 'acr_settings' && isset( $_GET[ 'section' ] ) && $_GET[ 'section' ] == 'acr_settings_help_section' && isset( $_GET[ 'msg' ] ) && isset( $_GET[ 'debug' ] ) && $_GET[ 'debug' ] == 'true' ) { $messages = array( '1' => array( 'status' => 'updated', 'msg' => __( 'Successfully run email sender.', 'advanced-cart-recovery' ) ) , '2' => array( 'status' => 'updated', 'msg' => __( 'Successfully run cancelled carts.', 'advanced-cart-recovery' ) ) , '3' => array( 'status' => 'updated', 'msg' => __( 'All scheduled emails are removed successfully.', 'advanced-cart-recovery' ) ) , '4' => array( 'status' => 'updated', 'msg' => __( 'All not recovered ( abandoned ) carts are removed successfully.', 'advanced-cart-recovery' ) ) , '5' => array( 'status' => 'updated', 'msg' => __( 'Successfully run abandoned carts.', 'advanced-cart-recovery' ) ) , 'error' => array( 'status' => 'error', 'msg' => __( 'Error! This action can\'t be completed, nothing to run.', 'advanced-cart-recovery' ) ), ); $msg = $messages[ $_GET[ 'msg' ] ][ 'msg' ]; $status = $messages[ $_GET[ 'msg' ] ][ 'status' ]; echo '
' . $msg . '