piassi/solidpress

为WordPress主题提供坚实的基础

1.2.0 2023-04-06 13:24 UTC

This package is auto-updated.

Last update: 2024-09-07 20:20:22 UTC


README

一个使用最佳OOP实践来为基于组件的WordPress主题提供稳固项目结构的库。

安装

SolidPress是一个Composer包,它通过Packagist提供

composer require piassi/solidpress

设置

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

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

在functions.php中初始化Solidpress

use SolidPress\Core\Theme;
use SolidPress\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, [
	'Taxonomies',
	'PostTypes',
	'Hooks',
]);

// Setup a theme instance for SolidPress
global $theme_class;
$theme_class = new Theme([
	'template_engine' => new WPTemplate(),
	'namespace' => 'App',
	'base_folder' => 'src',
	'registrable_namespaces' => $registrable_namespaces,
	'theme_name' => 'solidpress-theme',
	'css_dist_path' => get_template_directory_uri() . '/dist/%s.css', // %s will be replaced with page bundle css file.
	'js_dist_path' => get_template_directory_uri() . '/dist/%s.js', // %s will be replaced with page bundle js file.
]);

您可以将这个主题作为参考。

手册

可注册项

PostTypeTaxonomyFieldGroup类扩展了Registrable接口,这些类必须有一个构造函数,该函数将在站点启动时自动调用。

注册新文章类型

  1. PostTypes命名空间内创建一个新的类。

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

参见register_post_type文档以了解更多关于args属性的信息。

示例

注册一个名为"Products"的新文章类型

// Filepath: src/PostTypes/Products.php

namespace App\PostTypes;

use SolidPress\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文章类型创建一个新的字段组。

// Filepath: src/FieldsGroup/Product.php

namespace App\FieldsGroup;

use SolidPress\Core\FieldGroup;
use SolidPress\Core\PostType;
use SolidPress\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'),
				],
			]
		];
	}
}