query( $wpdb->prepare( "DELETE FROM {$wpdb->options} WHERE option_name = %s AND option_value = %s", $key, maybe_serialize( $old ) ) ); wp_cache_delete( $key, 'options' ); } $lease = array( 'token' => wp_generate_uuid4(), 'expires' => time() + 300 ); return add_option( $key, $lease, '', false ) ? $lease : false; } function vsai_lock_release( $name, $lease ) { global $wpdb; $key = 'vsai_lock_' . sanitize_key( $name ); $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->options} WHERE option_name = %s AND option_value = %s", $key, maybe_serialize( $lease ) ) ); wp_cache_delete( $key, 'options' ); } /** Acknowledge only the sent entries, retaining entries added during HTTP I/O. */ function vsai_ack_queue( $name, $sent, $field = '' ) { global $wpdb; if ( ! in_array( $name, array( 'vsai_events', 'vsai_sensor' ), true ) ) { return false; } for ( $attempt = 0; $attempt < 5; $attempt++ ) { $raw = $wpdb->get_var( $wpdb->prepare( "SELECT option_value FROM {$wpdb->options} WHERE option_name = %s", $name ) ); if ( null === $raw ) { return true; // Consent may have been revoked while the request was running. } $value = maybe_unserialize( $raw ); if ( ! is_array( $value ) || ( $field && 'on' !== ( $value['estado'] ?? '' ) ) ) { return false; } $queue = $field ? (array) ( $value[ $field ] ?? array() ) : $value; foreach ( $sent as $event ) { $index = array_search( $event, $queue, true ); if ( false !== $index ) { unset( $queue[ $index ] ); } } if ( $field ) { $value[ $field ] = array_values( $queue ); $value['enviados'] = (int) ( $value['enviados'] ?? 0 ) + count( $sent ); $value['ultimo_envio'] = time(); } else { $value = array_values( $queue ); } if ( maybe_serialize( $value ) === $raw ) { return true; } $changed = $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->options} SET option_value = %s WHERE option_name = %s AND option_value = %s", maybe_serialize( $value ), $name, $raw ) ); if ( $changed ) { wp_cache_delete( $name, 'options' ); wp_cache_delete( 'alloptions', 'options' ); return true; } } return false; // Leave queued data intact if another writer keeps winning. }