prefix ); } public static function engine_live( $channel ) { if ( 'email' === $channel ) { return 'live' === (string) get_option( 'aieo_email_engine_status', 'paused' ); } return 'live' === (string) get_option( 'aieo_push_engine_status', 'paused' ); } public static function test_on( $channel ) { if ( self::engine_live( $channel ) ) { return false; } $flags = get_option( self::OPT_CHANNELS, array() ); return is_array( $flags ) && ! empty( $flags[ $channel ] ); } public static function state( $channel ) { if ( self::engine_live( $channel ) ) { return 'live'; } return self::test_on( $channel ) ? 'test' : 'paused'; } public static function any_test() { return self::test_on( 'email' ) || self::test_on( 'push' ); } public static function any_live() { return self::engine_live( 'email' ) || self::engine_live( 'push' ); } public static function restricts() { return self::any_test() && ! self::any_live(); } public static function set_state( $channel, $state ) { $channel = ( 'push' === $channel ) ? 'push' : 'email'; $state = in_array( $state, array( 'live', 'test', 'paused' ), true ) ? $state : 'paused'; $engine = ( 'live' === $state ) ? 'live' : 'paused'; if ( 'email' === $channel ) { if ( class_exists( 'AIEO_Email_Engine' ) ) { AIEO_Email_Engine::set_status( $engine ); } else { update_option( 'aieo_email_engine_status', $engine, false ); } } elseif ( class_exists( 'AIEO_DMM_Web_Push' ) && method_exists( 'AIEO_DMM_Web_Push', 'set_engine_status' ) ) { AIEO_DMM_Web_Push::set_engine_status( $engine ); } else { update_option( 'aieo_push_engine_status', $engine, false ); } $flags = get_option( self::OPT_CHANNELS, array() ); $flags = is_array( $flags ) ? $flags : array(); $flags[ $channel ] = ( 'test' === $state ); update_option( self::OPT_CHANNELS, $flags, false ); return $state; } public static function testers() { $t = get_option( self::OPT_TESTERS, array() ); $t = is_array( $t ) ? $t : array(); $users = array_values( array_filter( array_map( 'intval', (array) ( $t['users'] ?? array() ) ) ) ); $emails = array(); foreach ( (array) ( $t['emails'] ?? array() ) as $e ) { $e = strtolower( sanitize_email( (string) $e ) ); if ( '' !== $e && is_email( $e ) ) { $emails[] = $e; } } return array( 'users' => array_values( array_unique( $users ) ), 'emails' => array_values( array_unique( $emails ) ) ); } public static function set_testers( array $users, array $emails ) { $keep = array(); foreach ( $users as $uid ) { $uid = (int) $uid; if ( $uid > 0 && get_userdata( $uid ) ) { $keep[] = $uid; } } $addrs = array(); foreach ( $emails as $e ) { $e = strtolower( sanitize_email( (string) $e ) ); if ( '' !== $e && is_email( $e ) ) { $addrs[] = $e; } } update_option( self::OPT_TESTERS, array( 'users' => array_values( array_unique( $keep ) ), 'emails' => array_values( array_unique( $addrs ) ) ), false ); return self::testers(); } public static function is_tester( $wp_user_id, $email = '' ) { $t = self::testers(); $uid = (int) $wp_user_id; if ( $uid > 0 && in_array( $uid, $t['users'], true ) ) { return true; } $addrs = array(); if ( '' !== (string) $email ) { $addrs[] = strtolower( trim( (string) $email ) ); } if ( $uid > 0 ) { $u = get_userdata( $uid ); if ( $u ) { $addrs[] = strtolower( (string) $u->user_email ); } } elseif ( '' !== (string) $email && $t['users'] ) { $u = get_user_by( 'email', strtolower( trim( (string) $email ) ) ); if ( $u && in_array( (int) $u->ID, $t['users'], true ) ) { return true; } } return array() !== array_intersect( $addrs, $t['emails'] ); } public static function tester_address( $wp_user_id, $email = '' ) { if ( ! self::is_tester( $wp_user_id, $email ) ) { return ''; } if ( (int) $wp_user_id > 0 ) { $u = get_userdata( (int) $wp_user_id ); if ( $u && is_email( $u->user_email ) ) { return strtolower( (string) $u->user_email ); } } return is_email( (string) $email ) ? strtolower( (string) $email ) : ''; } public static function enter() { self::$scope = true; } public static function leave() { self::$scope = false; } public static function in_scope() { return self::$scope; } public static function opens( $channel ) { if ( ! self::$scope ) { return false; } if ( 'email' === $channel || 'push' === $channel ) { return self::test_on( $channel ); } return self::any_test(); } public static function trigger_id( $workflow_id ) { return 'wf_' . (int) $workflow_id . ( self::$scope ? self::SUFFIX : '' ); } public static function held_runs( $channel ) { global $wpdb; $none = array( 'count' => 0, 'oldest' => '', 'recent' => 0 ); if ( ! function_exists( 'aieo_workflow_runs_table' ) ) { return $none; } $t = aieo_workflow_runs_table(); if ( (string) $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $t ) ) !== $t ) { return $none; } $channel = ( 'push' === $channel ) ? 'push' : 'email'; $row = $wpdb->get_row( $wpdb->prepare( "SELECT COUNT(*) AS n, MIN(created) AS oldest, SUM(created >= %s) AS recent FROM `{$t}` WHERE state IN ('pending','running') AND ( context_json LIKE %s OR context_json LIKE %s ) AND context_json NOT LIKE %s", gmdate( 'Y-m-d H:i:s', time() - 7 * DAY_IN_SECONDS ), '%' . $wpdb->esc_like( '"_held":"' . $channel . '"' ) . '%', '%' . $wpdb->esc_like( '"_held":"cascade"' ) . '%', '%' . $wpdb->esc_like( '"_test":1' ) . '%' ), ARRAY_A ); return array( 'count' => (int) ( $row['n'] ?? 0 ), 'oldest' => (string) ( $row['oldest'] ?? '' ), 'recent' => (int) ( $row['recent'] ?? 0 ), ); } public static function cancel_held_runs( $channel, $keep_recent = false ) { global $wpdb; if ( ! function_exists( 'aieo_workflow_runs_table' ) ) { return 0; } $t = aieo_workflow_runs_table(); if ( (string) $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $t ) ) !== $t ) { return 0; } $channel = ( 'push' === $channel ) ? 'push' : 'email'; $since = $keep_recent ? gmdate( 'Y-m-d H:i:s', time() - 7 * DAY_IN_SECONDS ) : gmdate( 'Y-m-d H:i:s', time() + DAY_IN_SECONDS ); $n = $wpdb->query( $wpdb->prepare( "UPDATE `{$t}` SET state = 'cancelled', step_index = IF( context_json LIKE %s OR context_json LIKE %s, step_index, 0 ), updated = %s WHERE state IN ('pending','running') AND created < %s AND ( context_json LIKE %s OR context_json LIKE %s ) AND context_json NOT LIKE %s", '%' . $wpdb->esc_like( '"_delivered":1' ) . '%', '%' . $wpdb->esc_like( '"_acted":1' ) . '%', gmdate( 'Y-m-d H:i:s' ), $since, '%' . $wpdb->esc_like( '"_held":"' . $channel . '"' ) . '%', '%' . $wpdb->esc_like( '"_held":"cascade"' ) . '%', '%' . $wpdb->esc_like( '"_test":1' ) . '%' ) ); return false === $n ? 0 : (int) $n; } public static function switch_channel( $channel, $state, $keep_recent = false ) { $channel = ( 'push' === $channel ) ? 'push' : 'email'; self::$keep_recent = (bool) $keep_recent; self::$cancelled[ $channel ] = 0; try { $set = self::set_state( $channel, $state ); } finally { self::$keep_recent = false; } return array( 'state' => $set, 'cancelled' => self::cancelled( $channel ) ); } public static function cancelled( $channel ) { return (int) ( self::$cancelled[ ( 'push' === $channel ) ? 'push' : 'email' ] ?? 0 ); } public static function bootstrap() { if ( self::$booted ) { return; } self::$booted = true; foreach ( array( 'email' => 'aieo_email_engine_status', 'push' => 'aieo_push_engine_status' ) as $channel => $option ) { add_filter( 'pre_update_option_' . $option, static function ( $value, $old ) use ( $channel ) { return self::going_live( $channel, $value, $old ); }, 10, 2 ); add_action( 'update_option_' . $option, static function ( $old, $value ) use ( $channel ) { self::gone_live( $channel, $value ); }, 10, 2 ); add_action( 'add_option_' . $option, static function ( $name, $value ) use ( $channel ) { self::gone_live( $channel, $value ); }, 10, 2 ); } add_action( 'shutdown', array( __CLASS__, 'unlock' ) ); } public static function going_live( $channel, $value, $old ) { if ( 'live' !== (string) $value || 'live' === (string) $old ) { return $value; } global $wpdb; if ( ! self::$locked ) { self::$locked = 1 === (int) $wpdb->get_var( $wpdb->prepare( 'SELECT GET_LOCK(%s, %d)', self::lock_name(), 30 ) ); } self::$cancelled[ $channel ] = self::cancel_held_runs( $channel, self::$keep_recent ); return $value; } public static function gone_live( $channel, $value ) { if ( 'live' === (string) $value ) { $flags = get_option( self::OPT_CHANNELS, array() ); if ( is_array( $flags ) && ! empty( $flags[ $channel ] ) ) { $flags[ $channel ] = false; update_option( self::OPT_CHANNELS, $flags, false ); } } self::unlock(); } public static function unlock() { if ( self::$locked ) { global $wpdb; $wpdb->query( $wpdb->prepare( 'SELECT RELEASE_LOCK(%s)', self::lock_name() ) ); self::$locked = false; } } public static function test_runs( $limit = 50 ) { global $wpdb; if ( ! function_exists( 'aieo_workflow_runs_table' ) || ! function_exists( 'aieo_workflows_table' ) ) { return array(); } $runs = aieo_workflow_runs_table(); $wf = aieo_workflows_table(); $rows = $wpdb->get_results( $wpdb->prepare( "SELECT r.id, r.workflow_id, w.name, r.wp_user_id, r.state, r.step_index, r.context_json, r.run_at, r.created, r.updated FROM `{$runs}` r LEFT JOIN `{$wf}` w ON w.id = r.workflow_id WHERE r.context_json LIKE %s ORDER BY r.id DESC LIMIT %d", '%' . $wpdb->esc_like( '"_test":1' ) . '%', max( 1, (int) $limit ) ), ARRAY_A ); return is_array( $rows ) ? $rows : array(); } public static function test_coupons() { global $wpdb; if ( ! function_exists( 'aieo_coupon_grants_table' ) ) { return array(); } $t = aieo_coupon_grants_table(); if ( (string) $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $t ) ) !== $t ) { return array(); } $rows = $wpdb->get_results( $wpdb->prepare( "SELECT id, code, coupon_id, source, email, offer_type, offer_value, status, minted_at, expires_at FROM `{$t}` WHERE source LIKE %s ORDER BY id DESC LIMIT 200", 'wf\_%' . $wpdb->esc_like( self::SUFFIX ) ), ARRAY_A ); return is_array( $rows ) ? $rows : array(); } public static function remove_test_coupons( $grant_ids = null ) { global $wpdb; $n = 0; foreach ( self::test_coupons() as $g ) { if ( is_array( $grant_ids ) && ! in_array( (int) $g['id'], array_map( 'intval', $grant_ids ), true ) ) { continue; } if ( (int) $g['coupon_id'] > 0 && 'shop_coupon' === get_post_type( (int) $g['coupon_id'] ) ) { wp_delete_post( (int) $g['coupon_id'], true ); } $n += (int) $wpdb->delete( aieo_coupon_grants_table(), array( 'id' => (int) $g['id'] ), array( '%d' ) ); } return $n; } }