time(), 'was' => $was, 'set' => $was, 'cancelled' => 0 ); update_option( self::DONE_OPTION, $done, false ); return $done; } $set = self::pushes_sent_recently() ? 'live' : 'paused'; $cancelled = 0; if ( 'live' === $set && class_exists( 'AIEO_Automation_Test_Mode' ) ) { if ( class_exists( 'AIEO_Workflow_Runtime' ) ) { AIEO_Workflow_Runtime::mark_held_runs(); } $cancelled = AIEO_Automation_Test_Mode::cancel_held_runs( 'push', false ); } update_option( 'aieo_push_engine_status', $set, false ); $changes = ( 'live' === $set ) ? ( $cancelled > 0 || self::push_workflows() > 0 ) : ( self::subscribers() > 0 ); if ( $changes ) { update_option( self::NOTICE_OPTION, array( 'set' => $set, 'cancelled' => $cancelled ), false ); } $done = array( 'at' => time(), 'was' => $was, 'set' => $set, 'cancelled' => $cancelled ); update_option( self::DONE_OPTION, $done, false ); return $done; } public static function pushes_sent_recently() { global $wpdb; $t = $wpdb->prefix . 'aieo_push_dispatch'; if ( (string) $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $t ) ) !== $t ) { return false; } $push_only = $wpdb->get_var( "SHOW COLUMNS FROM `{$t}` LIKE 'channel'" ) ? " AND channel = 'push'" : ''; return (bool) $wpdb->get_var( $wpdb->prepare( "SELECT 1 FROM `{$t}` WHERE state IN ('sent','clicked','dismissed','converted') AND sent_at >= %s{$push_only} LIMIT 1", gmdate( 'Y-m-d H:i:s', time() - self::WINDOW_DAYS * DAY_IN_SECONDS ) ) ); } private static function push_workflows() { if ( ! class_exists( 'AIEO_Workflows' ) || ! method_exists( 'AIEO_Workflows', 'list_all' ) ) { return 0; } $n = 0; foreach ( (array) AIEO_Workflows::list_all() as $wf ) { if ( 'active' !== (string) ( $wf['status'] ?? '' ) ) { continue; } foreach ( (array) ( $wf['steps'] ?? array() ) as $s ) { if ( in_array( (string) ( $s['type'] ?? '' ), array( 'send', 'ask_survey' ), true ) && in_array( (string) ( $s['channel'] ?? 'cascade' ), array( 'push', 'cascade' ), true ) ) { $n++; break; } } } return $n; } private static function subscribers() { global $wpdb; $t = $wpdb->prefix . 'aieo_push_subscriptions'; if ( (string) $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $t ) ) !== $t ) { return 0; } return (int) $wpdb->get_var( "SELECT COUNT(*) FROM `{$t}` WHERE status = 'active'" ); } public static function render_notice() { $n = get_option( self::NOTICE_OPTION, array() ); if ( ! is_array( $n ) || empty( $n['set'] ) || ! current_user_can( 'manage_options' ) ) { return; } $page = isset( $_GET['page'] ) ? sanitize_key( wp_unslash( $_GET['page'] ) ) : ''; if ( 0 !== strpos( $page, 'ai-eshop' ) && 0 !== strpos( $page, 'rooster-one' ) ) { return; } if ( ! class_exists( 'AIEO_Outbound_Notice' ) || ! AIEO_Outbound_Notice::existing_shop() ) { return; } $cancelled = (int) ( $n['cancelled'] ?? 0 ); if ( 'live' !== $n['set'] ) { $title = __( 'Push notifications are paused', 'ai-eshop-optimizer' ); $text = __( 'No push notification went out from this shop in the last 14 days, so push is set to Paused. Set it to Live on the Automations screen whenever you want to send by push.', 'ai-eshop-optimizer' ); } elseif ( $cancelled > 0 ) { $title = __( 'Push notifications are live', 'ai-eshop-optimizer' ); $text = sprintf( _n( 'Push notifications went out from this shop in the last 14 days, so push is set to Live, for your automations too. %s automation run that was waiting for push was cancelled, so nothing out of date goes out.', 'Push notifications went out from this shop in the last 14 days, so push is set to Live, for your automations too. %s automation runs that were waiting for push were cancelled, so nothing out of date goes out.', $cancelled, 'ai-eshop-optimizer' ), number_format_i18n( $cancelled ) ); } else { $title = __( 'Push notifications are live', 'ai-eshop-optimizer' ); $text = __( 'Push notifications went out from this shop in the last 14 days, so push is set to Live, for your automations too.', 'ai-eshop-optimizer' ); } $open = admin_url( 'admin.php?page=ai-eshop-dmm&dmm_tab=workflows' ); $dismiss = wp_nonce_url( admin_url( 'admin-post.php?action=aieo_push_settle_notice_dismiss' ), 'aieo_push_settle_notice', 'aieo_nonce' ); echo '

' . esc_html( $title ) . '

' . esc_html( $text ) . '

' . '' . esc_html__( 'Open Automations', 'ai-eshop-optimizer' ) . ' ยท ' . '' . esc_html__( 'Got it', 'ai-eshop-optimizer' ) . '

'; } public static function dismiss_notice() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'Insufficient permissions.', 'ai-eshop-optimizer' ) ); } check_admin_referer( 'aieo_push_settle_notice', 'aieo_nonce' ); delete_option( self::NOTICE_OPTION ); wp_safe_redirect( wp_get_referer() ? wp_get_referer() : admin_url() ); exit; } }