prefix . 'affiliatant_queue'; } public static function install() { global $wpdb; require_once ABSPATH . 'wp-admin/includes/upgrade.php'; $table = self::table(); $collate = $wpdb->get_charset_collate(); $sql = "CREATE TABLE {$table} ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, event_id varchar(64) NOT NULL DEFAULT '', action varchar(16) NOT NULL DEFAULT 'conversion', order_id bigint(20) unsigned NOT NULL DEFAULT 0, payload longtext NULL, status varchar(12) NOT NULL DEFAULT 'queued', attempts smallint(5) unsigned NOT NULL DEFAULT 0, next_attempt bigint(20) unsigned NOT NULL DEFAULT 0, amount_minor bigint(20) NOT NULL DEFAULT 0, last_error varchar(255) NOT NULL DEFAULT '', remote_id varchar(64) NOT NULL DEFAULT '', created bigint(20) unsigned NOT NULL DEFAULT 0, updated bigint(20) unsigned NOT NULL DEFAULT 0, PRIMARY KEY (id), UNIQUE KEY event_id (event_id), KEY due (status,next_attempt), KEY order_id (order_id) ) {$collate};"; dbDelta( $sql ); update_option( self::SCHEMA_OPTION, self::SCHEMA_VERSION, true ); } public static function ensure() { if ( (string) get_option( self::SCHEMA_OPTION ) === self::SCHEMA_VERSION ) { return; } self::install(); } public static function enqueue( $action, $event_id, $order_id, array $payload ) { global $wpdb; self::ensure(); $now = time(); $table = self::table(); $sql = $wpdb->prepare( "INSERT IGNORE INTO {$table} (event_id, action, order_id, payload, status, attempts, next_attempt, created, updated) VALUES (%s, %s, %d, %s, 'queued', 0, %d, %d, %d)", (string) $event_id, (string) $action, (int) $order_id, wp_json_encode( $payload ), $now, $now, $now ); $wpdb->query( $sql ); if ( ! $wpdb->insert_id ) { return false; } return (int) $wpdb->insert_id; } public static function row( $id ) { global $wpdb; $table = self::table(); $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$table} WHERE id = %d", (int) $id ), ARRAY_A ); return is_array( $row ) ? $row : null; } public static function due( $limit = 20 ) { global $wpdb; self::ensure(); $table = self::table(); $rows = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$table} WHERE status = 'queued' AND next_attempt <= %d ORDER BY next_attempt ASC, id ASC LIMIT %d", time(), (int) $limit ), ARRAY_A ); return is_array( $rows ) ? $rows : array(); } public static function mark_sent( $id, $remote_id, $amount_minor = 0 ) { global $wpdb; $row = self::row( $id ); $wpdb->update( self::table(), array( 'status' => 'sent', 'attempts' => $row ? (int) $row['attempts'] + 1 : 1, 'last_error' => '', 'remote_id' => substr( (string) $remote_id, 0, 64 ), 'amount_minor' => (int) $amount_minor, 'updated' => time(), ), array( 'id' => (int) $id ), array( '%s', '%d', '%s', '%s', '%d', '%d' ), array( '%d' ) ); } public static function mark_failure( array $row, $message, $retryable ) { global $wpdb; $next = Affiliatant_Retry::after_failure( (int) $row['attempts'], (bool) $retryable, time() ); $wpdb->update( self::table(), array( 'status' => $next['status'], 'attempts' => $next['attempts'], 'next_attempt' => $next['next_attempt'], 'last_error' => substr( (string) $message, 0, 255 ), 'updated' => time(), ), array( 'id' => (int) $row['id'] ), array( '%s', '%d', '%d', '%s', '%d' ), array( '%d' ) ); } public static function reversed_so_far( $order_id ) { global $wpdb; $table = self::table(); $sum = $wpdb->get_var( $wpdb->prepare( "SELECT SUM(amount_minor) FROM {$table} WHERE order_id = %d AND action = 'reverse' AND status = 'sent'", (int) $order_id ) ); return null === $sum ? 0 : (int) $sum; } public static function has_conversion( $order_id ) { global $wpdb; self::ensure(); $table = self::table(); $found = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$table} WHERE order_id = %d AND action = 'conversion' AND status <> 'failed' LIMIT 1", (int) $order_id ) ); return null !== $found; } public static function counts() { global $wpdb; self::ensure(); $table = self::table(); $counts = array( 'queued' => 0, 'sent' => 0, 'failed' => 0, ); $rows = $wpdb->get_results( "SELECT status, COUNT(*) AS n FROM {$table} GROUP BY status", ARRAY_A ); foreach ( (array) $rows as $row ) { $counts[ (string) $row['status'] ] = (int) $row['n']; } return $counts; } public static function recent( $limit = 20 ) { global $wpdb; self::ensure(); $table = self::table(); $rows = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$table} ORDER BY id DESC LIMIT %d", (int) $limit ), ARRAY_A ); return is_array( $rows ) ? $rows : array(); } public static function clear_failed() { global $wpdb; return (int) $wpdb->delete( self::table(), array( 'status' => 'failed' ), array( '%s' ) ); } public static function prune() { global $wpdb; $table = self::table(); $wpdb->query( $wpdb->prepare( "DELETE FROM {$table} WHERE status = 'sent' AND updated < %d LIMIT 500", time() - self::PRUNE_AFTER ) ); } }