intermobile/wp-solidify

组件化WordPress主题的坚实基础

1.2.5 2024-07-31 17:20 UTC

This package is auto-updated.

Last update: 2024-08-31 20:55:36 UTC


README

一个使用最佳面向对象实践来为组件化WordPress主题提供坚实项目结构的库。

安装

WP Solidify是一个Composer包,并且可以通过Packagist获取。

要在项目中安装它,请运行以下命令。

composer require intermobile/wp-solidify

设置

composer.json文件中,在你的主题内部设置一个文件夹作为根命名空间。

{
	"autoload": {
		"psr-4": {
			"App\\": "src/"
		}
	}
}

functions.php中初始化Solidify。

use Solidify\Core\Theme;
use Solidify\Core\WPTemplate;

// Composer autoload
require get_template_directory() . '/vendor/autoload.php';

$registrable_namespaces = [];

// Check if ACF plugin is active to register fields
if (function_exists('acf_add_local_field_group')) {
	$registrable_namespaces[] = 'FieldsGroup';
	$registrable_namespaces[] = 'Options';
}

// Set core registrables
$registrable_namespaces = array_merge(
	$registrable_namespaces,
	array(
		'Taxonomies',
		'PostTypes',
		'Hooks',
	)
);

// Setup a theme instance for Solidify
global $theme_class;
$theme_class = new Theme(
	array(
		'template_engine' => new WPTemplate(),
		'namespace' => 'App',
		'base_folder' => 'src',
		'registrable_namespaces' => $registrable_namespaces,
		'theme_name' => 'wp-solidify-theme',
	)
);

你可以参考此主题

手册

可注册项

《PostType》、《Taxonomy》和《FieldGroup》类扩展了《Registrable》接口,这些类必须有一个构造函数,它将在站点启动时自动调用。

注册新的文章类型

  1. 在《PostTypes》命名空间内部创建一个新类。

  2. 在构造函数中设置post_typeargs属性,这些属性将被转发到《register_post_type》函数。

查看《register_post_type》文档以获取关于《args》属性的更多详细信息。

示例

注册一个新的文章类型称为《Products》。

// # src/PostTypes/Products.php

namespace App\PostTypes;

use Solidify\Core\PostType;

class Products extends PostType{
	public function __construct()
	{
		$this->post_type = "product";

		$labels = [
			"name" => "Products",
			"singular_name" => "Product",
			"menu_name" => "Products",
			"all_items" => "All Products",
			"add_new" => "Add new",
			"add_new_item" => "Add new product",
			"edit_item" => "Edit product",
			"new_item" => "New product",
			"view_item" => "View proct",
			"insert_into_item" => "Insert in product",
			"view_items" => "View products",
			"search_items" => "Search for products",
			"not_found" => "No products found",
			"not_found_in_trash" => "No products found in trash"
		];

		$this->args = [
			"label"               => "Products",
			"labels"              => $labels,
			"description"         => "",
			"public"              => true,
			"publicly_queryable"  => false,
			"show_ui"             => true,
			"show_in_rest"        => false,
			"rest_base"           => "",
			"has_archive"         => false,
			"show_in_menu"        => true,
			"exclude_from_search" => true,
			"capability_type"     => "post",
			"map_meta_cap"        => true,
			"hierarchical"        => false,
			"menu_position"       => 8,
			'rewrite'             => array("slug" => $this->post_type, "with_front" => false),
			'query_var'           => false,
			"supports"            => array("title", "editor", "revisions", "excerpt"),
			"menu_icon"           => 'dashicons-book-alt',
			"taxonomies"          => []
		];
	}
}

创建新的自定义字段组

  1. 在《FieldsGroup》命名空间内部创建一个新类,在类构造函数中调用《set_fields》并设置《args》。

  2. 《set_fields》方法接收一个数组作为参数,数组的键是字段名,值是《Field》类实例。

示例

为《product》文章类型创建一个新的字段组。

// # src/FieldsGroup/Product.php

namespace App\FieldsGroup;

use Solidify\Core\FieldGroup;
use Solidify\Core\PostType;
use Solidify\Fields;

class Product extends FieldGroup{
	public function __construct() {
		// Set fields
		$this->set_fields([
			'product_detailts_tab' => new Fields\Tab('Product Details'),
			'value' => new Fields\Number('Value'),
			'subtitle' => new Fields\Text('Subtitle', [
				// You can pass an acf options array as the second argument
				'wrapper' => [
					'width' => 70
				]
			]),
			'gallery_thumbnail' => new Fields\Image('Gallery Thumbnail', [
				'wrapper' => [
					'width' => 30
				]
			])
		]);

		// Set arguments
		$this->args = [
			'key' => 'product-fields',
			'title' => 'Product Fields',
			'location' => [
				[
					// Pages, Taxonomies, OptionsPages, and PostTypes
					// classes have static methods for acf conditionals.
					PostType::is_equal_to('product'),
				],
			]
		];
	}
}