Outil de recherche WordPress pour les développeurs et auteurs de thèmes



validate_plugin_requirements ›

Depuis5.2.0
Dépréciéen/a
validate_plugin_requirements ( $plugin )
Paramètres:
  • (string) $plugin Path to the plugin file relative to the plugins directory.
    Requis: Oui
Retourne:
  • (true|WP_Error) True if requirements are met, WP_Error on failure.
Défini(e) dans:
Codex:

Validate the plugin requirements for WP version and PHP version.



Source

function validate_plugin_requirements( $plugin ) {
	$readme_file = WP_PLUGIN_DIR . '/' . dirname( $plugin ) . '/readme.txt';
	$plugin_data = array(
		'requires'     => '',
		'requires_php' => '',
	);

	if ( file_exists( $readme_file ) ) {
		$plugin_data = get_file_data(
			$readme_file,
			array(
				'requires'     => 'Requires at least',
				'requires_php' => 'Requires PHP',
			),
			'plugin'
		);
	}

	$plugin_data = array_merge( $plugin_data, get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin ) );

	// Check for headers in the plugin's PHP file, give precedence to the plugin headers.
	$plugin_data['requires']     = ! empty( $plugin_data['RequiresWP'] ) ? $plugin_data['RequiresWP'] : $plugin_data['requires'];
	$plugin_data['requires_php'] = ! empty( $plugin_data['RequiresPHP'] ) ? $plugin_data['RequiresPHP'] : $plugin_data['requires_php'];

	$plugin_data['wp_compatible']  = is_wp_version_compatible( $plugin_data['requires'] );
	$plugin_data['php_compatible'] = is_php_version_compatible( $plugin_data['requires_php'] );

	if ( ! $plugin_data['wp_compatible'] && ! $plugin_data['php_compatible'] ) {
		return new WP_Error(
			'plugin_wp_php_incompatible',
			sprintf(
				/* translators: %s: Plugin name. */
				__( '<strong>Error:</strong> Current WordPress and PHP versions do not meet minimum requirements for %s.' ),
				$plugin_data['Name']
			)
		);
	} elseif ( ! $plugin_data['php_compatible'] ) {
		return new WP_Error(
			'plugin_php_incompatible',
			sprintf(
				/* translators: %s: Plugin name. */
				__( '<strong>Error:</strong> Current PHP version does not meet minimum requirements for %s.' ),
				$plugin_data['Name']
			)
		);
	} elseif ( ! $plugin_data['wp_compatible'] ) {
		return new WP_Error(
			'plugin_wp_incompatible',
			sprintf(
				/* translators: %s: Plugin name. */
				__( '<strong>Error:</strong> Current WordPress version does not meet minimum requirements for %s.' ),
				$plugin_data['Name']
			)
		);
	}

	return true;
}