$survey, 'step' => array( 'type' => 'send', 'channel' => isset( $step['channel'] ) ? (string) $step['channel'] : 'cascade', 'subject' => isset( $step['subject'] ) ? (string) $step['subject'] : '', 'body' => $body, 'product_source' => 'none', 'offer' => 'none', ), ); } private static function capped( array $step, array $wf, array $run, $survey ) { $days = isset( $step['frequency_cap'] ) ? (int) $step['frequency_cap'] : 0; if ( $days <= 0 ) { return false; } global $wpdb; if ( ! function_exists( 'aieo_workflow_runs_table' ) ) { return false; } $runs = aieo_workflow_runs_table(); $where = array(); $args = array(); if ( (int) ( $run['subject_id'] ?? 0 ) > 0 ) { $where[] = 'subject_id = %d'; $args[] = (int) $run['subject_id']; } elseif ( (int) ( $run['wp_user_id'] ?? 0 ) > 0 ) { $where[] = 'wp_user_id = %d'; $args[] = (int) $run['wp_user_id']; } elseif ( '' !== (string) ( $run['email_hash'] ?? '' ) ) { $where[] = 'email_hash = %s'; $args[] = (string) $run['email_hash']; } else { return false; } $where[] = 'created >= %s'; $args[] = gmdate( 'Y-m-d H:i:s', time() - ( $days * DAY_IN_SECONDS ) ); $where[] = 'id <> %d'; $args[] = (int) ( $run['id'] ?? 0 ); $where[] = 'context_json LIKE %s'; $args[] = '%' . $wpdb->esc_like( '"_aieo_survey":"' . $survey . '"' ) . '%'; unset( $wf ); return (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$runs} WHERE " . implode( ' AND ', $where ), $args ) ) > 0; } private static function recipient_email( array $run ) { $uid = (int) ( isset( $run['wp_user_id'] ) ? $run['wp_user_id'] : 0 ); if ( $uid > 0 ) { $u = get_user_by( 'id', $uid ); if ( $u ) { return (string) $u->user_email; } } $oid = (int) ( isset( $run['order_id'] ) ? $run['order_id'] : 0 ); if ( $oid > 0 && function_exists( 'wc_get_order' ) ) { $o = wc_get_order( $oid ); if ( $o ) { return (string) $o->get_billing_email(); } } return ''; } public static function stats( $survey, $days = 30 ) { global $wpdb; $survey = AIEO_Survey_Blueprint::code( $survey ); $out = array( 'asked' => 0, 'responded' => 0, 'rate' => 0.0 ); if ( '' === $survey || ! function_exists( 'aieo_workflow_runs_table' ) ) { return $out; } $runs = aieo_workflow_runs_table(); $entries = AIEO_Form_CPT::entries_table(); $since = gmdate( 'Y-m-d H:i:s', time() - ( max( 1, (int) $days ) * DAY_IN_SECONDS ) ); $like = '%' . $wpdb->esc_like( '"_aieo_survey":"' . $survey . '"' ) . '%'; $out['asked'] = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$runs} WHERE created >= %s AND context_json LIKE %s", $since, $like ) ); $out['responded'] = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(DISTINCT e.run_id) FROM {$entries} e JOIN {$runs} r ON r.id = e.run_id WHERE e.run_id > 0 AND e.surface = %s AND r.created >= %s AND r.context_json LIKE %s", 'aieo_' . $survey, $since, $like ) ); if ( $out['asked'] > 0 ) { $out['rate'] = round( ( $out['responded'] / $out['asked'] ) * 100, 1 ); } return $out; } }