wordpress-phoenix/abstract-theme-base

PHP类,用于在构建WordPress主题时扩展,允许您遵循智能主题设置标准。

1.0.0 2020-03-06 05:16 UTC

This package is auto-updated.

Last update: 2024-09-06 15:42:39 UTC


README

用作基础类,以帮助标准化我们构建WordPress主题的方式。

CircleCI构建:CircleCI

目录

安装

您可以使用此库从头开始创建新主题,或者使用此库增强您现有的主题。阅读安装说明后,应该很清楚该走哪条路。

Composer风格(推荐)

  1. 使用which composer确认您的开发环境中已安装Composer。如果CLI未打印任何路径,您需要安装Composer
  2. 将CLI工作目录设置为wp-content/themes/{your-theme-name}
  3. 通过命令行使用composer命令安装Abstract_Theme类
    composer require wordpress-phoenix/abstract-theme-base
  4. 查看以下示例代码,了解如何将此库包含到您的主题中。

手动安装

  1. https://api.github.com/repos/WordPress-Phoenix/abstract-theme-base/zipball下载此存储库的最新副本
  2. 解压缩zip文件,并将PHP文件复制到您的主题项目中。
  3. 在您的主题中包含此文件。

用法

为什么在构建主题时应该使用此库?

通过使用面向对象原则构建主题并扩展此Theme_Base类对象,您可以快速有效地构建主题,使其易于开始,但同时又能够保持复杂度,而无需改变其架构。

即时功能包括

  • 内置SPL自动加载,适用于您的includes文件夹,如果您遵循WordPress Codex命名标准进行类文件。
  • 模板类提供了标准主题初始化的最佳实践。
  • 最小化主主题文件中所需的/维护的代码。
  • 帮助新手WordPress主题开发者了解文件/文件夹架构。
  • 通过使所有主题都以相同的架构开始,我们创建了一个对开发社区更好的标准。

主主题功能文件和所需主题类文件的最简单示例

custom-my-theme.php:

<?php
// Avoid direct calls to this file, because now WP core and framework has been used
if ( ! function_exists( 'add_filter' ) ) {
	header( 'Status: 403 Forbidden' );
	header( 'HTTP/1.1 403 Forbidden' );
	exit();
}

$current_dir = trailingslashit( dirname( __FILE__ ) );

/**
 * 3RD PARTY DEPENDENCIES
 * (manually include_once dependencies installed via composer for safety)
 */
if ( ! class_exists( 'WPAZ_Theme_Base\\V_2_6\\Abstract_Plugin' ) ) {
	include_once $current_dir . 'class-abstract-theme.php';
}

// Maybe load vendor director from theme, if it exists.
if ( file_exists( __DIR__ . '/vendor/autoload.php' ) ) {
	include_once( __DIR__ . '/vendor/autoload.php' );
}

/**
 * Load in the main theme application and initialize it.
 */
include_once $current_dir . 'app/class-theme.php';
Mynamespace\Custom_Theme\Theme::run( __FILE__ );

app/class-theme.php:

<?php

namespace Custom\My_theme;

use WPAZ_Theme_Base\V_2_6\Abstract_Theme;

/**
 * Class App
 */
class Theme extends Abstract_Theme {

	public static $autoload_class_prefix = __NAMESPACE__;

	// Set to 2 when you use 2 namespaces in the main app file
	public static $autoload_ns_match_depth = 2;

	public function onload( $instance ) {
		// Nothing yet
	}

	public function init() {
		do_action( static::class . '_before_init' );

		// Do theme stuff usually looks something like
		// $subclass = new OptionalAppSubfolder\Custom_Class_Subclass();
		// $subclass->custom_theme_function();

		do_action( static::class . '_after_init' );
	}

	public function authenticated_init() {
		if ( ! is_user_logged_in() ) {
			return;
		}

		// Ready for wp-admin - but not required
		//$this->admin = new Admin\App( $this );
	}

	protected function defines_and_globals() {
		// None yet.
	}

}