wpseek.com
A WordPress-centric search engine for devs and theme authors



_wp_get_entity_view_config_posttype_wp_template › WordPress Function

Since7.1.0
Deprecatedn/a
_wp_get_entity_view_config_posttype_wp_template ( $data )
Parameters:
  • (WP_View_Config_Data) $data The view configuration container for the entity.
    Required: Yes
Returns:
  • (WP_View_Config_Data) The updated view configuration container.
Defined at:
Codex:

Provides the view configuration for the `wp_template` post type.



Source

function _wp_get_entity_view_config_posttype_wp_template( $data ) {
	$default_view = array(
		'type'             => 'grid',
		'perPage'          => 20,
		'sort'             => array(
			'field'     => 'title',
			'direction' => 'asc',
		),
		'titleField'       => 'title',
		'descriptionField' => 'description',
		'mediaField'       => 'preview',
		'fields'           => array( 'author', 'active', 'slug', 'theme' ),
		'filters'          => array(),
		'showMedia'        => true,
	);

	$default_layouts = array(
		'table' => array( 'showMedia' => false ),
		'grid'  => array( 'showMedia' => true ),
		'list'  => array( 'showMedia' => false ),
	);

	$view_list = array(
		array(
			'title' => __( 'All templates' ),
			'slug'  => 'all',
		),
	);

	$templates = get_block_templates( array(), 'wp_template' );

	// Collect unique authors, tracking whether they come from a registered
	// source (theme, plugin, site) so we can sort those before user ones.
	$seen_authors       = array();
	$registered_authors = array();
	$user_authors       = array();
	foreach ( $templates as $template ) {
		/*
		 * Determine the original source of the template ('theme', 'plugin',
		 * 'site', or 'user').
		 */
		$original_source = 'user';
		if ( 'wp_template' === $template->type || 'wp_template_part' === $template->type ) {
			if ( $template->has_theme_file &&
				( 'theme' === $template->origin || (
					empty( $template->origin ) && in_array(
						$template->source,
						array(
							'theme',
							'custom',
						),
						true
					) )
				)
			) {
				/*
				 * Added by theme.
				 * Template originally provided by a theme, but customized by a user.
				 * Templates originally didn't have the 'origin' field so identify
				 * older customized templates by checking for no origin and a 'theme'
				 * or 'custom' source.
				 */
				$original_source = 'theme';
			} elseif ( 'plugin' === $template->origin ) {
				// Added by plugin.
				$original_source = 'plugin';
			} elseif ( empty( $template->has_theme_file ) && 'custom' === $template->source && empty( $template->author ) ) {
				/*
				 * Added by site.
				 * Template was created from scratch, but has no author. Author support
				 * was only added to templates in WordPress 5.9. Fallback to showing the
				 * site logo and title.
				 */
				$original_source = 'site';
			}
		}

		// Determine a human readable text for the author of the template.
		$author_text = '';
		switch ( $original_source ) {
			case 'theme':
				$theme_name  = wp_get_theme( $template->theme )->get( 'Name' );
				$author_text = empty( $theme_name ) ? $template->theme : $theme_name;
				break;
			case 'plugin':
				if ( ! function_exists( 'get_plugins' ) ) {
					require_once ABSPATH . 'wp-admin/includes/plugin.php';
				}
				$plugin_name = '';
				if ( isset( $template->plugin ) ) {
					$plugins = wp_get_active_and_valid_plugins();

					foreach ( $plugins as $plugin_file ) {
						$plugin_basename      = plugin_basename( $plugin_file );
						list( $plugin_slug, ) = explode( '/', $plugin_basename );

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

							if ( ! empty( $plugin_data['Name'] ) ) {
								$plugin_name = $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.
				 */
				if ( '' === $plugin_name ) {
					$plugins         = get_plugins();
					$plugin_basename = plugin_basename( sanitize_text_field( $template->theme . '.php' ) );
					if ( isset( $plugins[ $plugin_basename ] ) && isset( $plugins[ $plugin_basename ]['Name'] ) ) {
						$plugin_name = $plugins[ $plugin_basename ]['Name'];
					} else {
						$plugin_name = $template->plugin ?? $template->theme;
					}
				}
				$author_text = $plugin_name;
				break;
			case 'site':
				$author_text = get_bloginfo( 'name' );
				break;
			case 'user':
				$author = get_user_by( 'id', $template->author );
				if ( ! $author ) {
					$author_text = __( 'Unknown author' );
				} else {
					$author_text = $author->get( 'display_name' );
				}
				break;
		}

		if ( ! empty( $author_text ) && ! isset( $seen_authors[ $author_text ] ) ) {
			$seen_authors[ $author_text ] = true;
			$entry                        = array(
				'title' => $author_text,
				'slug'  => $author_text,
				'view'  => array(
					'filters' => array(
						array(
							'field'    => 'author',
							'operator' => 'is',
							'value'    => $author_text,
							'isLocked' => true,
						),
					),
				),
			);
			if ( 'user' === $original_source ) {
				$user_authors[] = $entry;
			} else {
				$registered_authors[] = $entry;
			}
		}
	}

	$form = array(
		'layout' => array( 'type' => 'panel' ),
		'fields' => array(
			array(
				'id'     => 'description',
				'layout' => array(
					'type'          => 'panel',
					'labelPosition' => 'top',
				),
			),
			array(
				'id'     => 'description_readonly',
				'layout' => array(
					'type'          => 'regular',
					'labelPosition' => 'none',
				),
			),
			array(
				'id'     => 'last_edited_date',
				'layout' => array(
					'type'          => 'panel',
					'labelPosition' => 'none',
				),
			),
			'revisions',
			// The following fields are only meaningful in the `home`/`index`
			// template summary. They edit other entities (`root/site` and the
			// posts page); the editor merges those records into the form data
			// under a namespace and controls when the fields are shown.
			'posts_page_title',
			'posts_per_page',
			'default_comment_status',
		),
	);

	$data->set(
		array(
			'default_view'    => $default_view,
			'default_layouts' => $default_layouts,
			'view_list'       => array_merge( $view_list, $registered_authors, $user_authors ),
			'form'            => $form,
		),
		1
	);

	return $data;
}