W3cubDocs

/WordPress

WP_REST_Templates_Controller::get_wp_templates_author_text_field( WP_Block_Template $template_object ): string

Returns a human readable text for the author of the template.

Parameters

$template_objectWP_Block_Templaterequired
Template instance.

Return

string Human readable text for the author.

Source

private static function get_wp_templates_author_text_field( $template_object ) {
	$original_source = self::get_wp_templates_original_source_field( $template_object );
	switch ( $original_source ) {
		case 'theme':
			$theme_name = wp_get_theme( $template_object->theme )->get( 'Name' );
			return empty( $theme_name ) ? $template_object->theme : $theme_name;
		case 'plugin':
			if ( ! function_exists( 'get_plugins' ) || ! function_exists( 'get_plugin_data' ) ) {
				require_once ABSPATH . 'wp-admin/includes/plugin.php';
			}
			if ( isset( $template_object->plugin ) ) {
				$plugins = wp_get_active_and_valid_plugins();

				foreach ( $plugins as $plugin_file ) {
					$plugin_basename = plugin_basename( $plugin_file );
					// Split basename by '/' to get the plugin slug.
					list( $plugin_slug, ) = explode( '/', $plugin_basename );

					if ( $plugin_slug === $template_object->plugin ) {
						$plugin_data = get_plugin_data( $plugin_file );

						if ( ! empty( $plugin_data['Name'] ) ) {
							return $plugin_data['Name'];
						}

						break;
					}
				}
			}

			/*
			 * Fall back to the theme name if the plugin is not defined. That's needed to keep backwards
			 * compatibility with templates that were registered before the plugin attribute was added.
			 */
			$plugins         = get_plugins();
			$plugin_basename = plugin_basename( sanitize_text_field( $template_object->theme . '.php' ) );
			if ( isset( $plugins[ $plugin_basename ] ) && isset( $plugins[ $plugin_basename ]['Name'] ) ) {
				return $plugins[ $plugin_basename ]['Name'];
			}
			return isset( $template_object->plugin ) ?
				$template_object->plugin :
				$template_object->theme;
		case 'site':
			return get_bloginfo( 'name' );
		case 'user':
			$author = get_user_by( 'id', $template_object->author );
			if ( ! $author ) {
				return __( 'Unknown author' );
			}
			return $author->get( 'display_name' );
	}

	// Fail-safe to return a string should the original source ever fall through.
	return '';
}

Changelog

Version Description
6.5.0 Introduced.

© 2003–2024 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/classes/wp_rest_templates_controller/get_wp_templates_author_text_field