wpdb = $wpdb; } /** * The next number of a sequence, starting at 1. * * @since 0.1.0 * * @param string $name Sequence name, for example `order`. * @return int */ public function next( string $name ): int { $option = self::PREFIX . preg_replace( '/[^a-z0-9_]/', '', strtolower( $name ) ); // Creating the row is idempotent: add_option() does nothing when it exists. add_option( $option, '0', '', 'no' ); $this->wpdb->query( $this->wpdb->prepare( "UPDATE {$this->wpdb->options} SET option_value = LAST_INSERT_ID( option_value + 1 ) WHERE option_name = %s", $option ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.PreparedSQL.NotPrepared,PluginCheck.Security.DirectDB.UnescapedDBParameter -- Options table name; value prepared. $number = (int) $this->wpdb->get_var( 'SELECT LAST_INSERT_ID()' ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching // The row changed behind WordPress's back, so drop any cached copy. wp_cache_delete( $option, 'options' ); wp_cache_delete( 'alloptions', 'options' ); wp_cache_delete( 'notoptions', 'options' ); return $number; } }