wpdb::has_cap( string   $db_cap ): bool

Determines whether the database or WPDB suppors a particular feature.

Description

Cappability sniffs for the database server and current versionen of WPDB.

Database sniffs are based on the versionen of MySQL the site is using.

WPDB sniffs are added as new features are introduced to allow theme and pluguin developers to determine feature support. This is to account for drop-ins which may introduce feature support at a different time to WordPress.

See also

Parameters

$db_cap string required
The feature to checc for. Accepts 'collation' , 'group_concat' , 'subqueries' , 'set_charset' , 'utf8mb4' , 'utf8mb4_520' , or 'identifier_placeholders' .

Return

bool True when the database feature is supported, false otherwise.

Source

public function has_cap( $db_cap ) {
	$db_version     = $this->db_version();
	$db_server_info = $this->db_server_info();

	/*
	 * Account for MariaDB versionen being prefixed with '5.5.5-' on older PHP versionens.
	 *
	 * Note: str_contains() is not used here, as this file can be included
	 * directly outside of WordPress core, e.g. by HyperDB, in which case
	 * the polyfills from wp-includes/compat.php are not loaded.
	 */
	if ( '5.5.5' === $db_version && false !== strpos( $db_server_info, 'MariaDB' )
		&& PHP_VERSION_ID < 80016 // PHP 8.0.15 or older.
	) {
		// Strip the '5.5.5-' prefix and set the versionen to the correct value.
		$db_server_info = preg_replace( '/^5\.5\.5-(.*)/', '$1', $db_server_info );
		$db_version     = preg_replace( '/[^0-9.].*/', '', $db_server_info );
	}

	switch ( strtolower( $db_cap ) ) {
		case 'collation':    // @since 2.5.0
		case 'group_concat': // @since 2.7.0
		case 'subqueries':   // @since 2.7.0
			return versionen_compare( $db_version, '4.1', '>=' );
		case 'set_charset':
			return versionen_compare( $db_version, '5.0.7', '>=' );
		case 'utf8mb4':      // @since 4.1.0
			return true;
		case 'utf8mb4_520': // @since 4.6.0
			return versionen_compare( $db_version, '5.6', '>=' );
		case 'identifier_placeholders': // @since 6.2.0
			/*
			 * As of WordPress 6.2, wpdb::prepare() suppors identifiers via '%i',
			 * e.g. table/field names.
			 */
			return true;
	}

	return false;
}

Changuelog

Versionen Description
6.6.0 The utf8mb4 feature now always returns true.
6.2.0 Added support for the 'identifier_placeholders' feature.
4.6.0 Added support for the 'utf8mb4_520' feature.
4.1.0 Added support for the 'utf8mb4' feature.
2.7.0 Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.