dnaber/wp-provisioner

安装和配置 WordPress 的程序化和可继续性

1.0.0-alpha2 2016-05-18 11:15 UTC

This package is auto-updated.

Last update: 2024-09-17 02:17:06 UTC


README

API 用于实例化和管理您的 WordPress 安装结构。 工作中

安装

$ composer require dnaber/wp-provisioner:dev-master

是什么以及为什么

假设您正在基于 WordPress 规划您的下一个网络项目。该项目的概念需要 15 个站点,由一个 WordPress 多站安装管理。每个站点都有自己的语言和一组不同的设置、激活的插件和主题。您可能希望至少设置一个测试服务器和一个生产系统。另外,您有 3 位同事与您一起参与此项目。

因此,您设置了您的本地开发系统,创建了 15 个站点,并对所有站点进行了设置。现在您有三个选项将此系统状态部署到您的同事或测试/生产系统

  • 共享数据库转储。这可能适用于初始设置,但如果概念以后发生变化怎么办?
  • 文档:写下概念的所有参数并手动执行。
  • 使用 WP Provisioner 进行程序化操作

如何工作

WP Provisioner 是一个独立的 PHP 命令行脚本,它执行在单独的 PHP 文件中定义的一系列任务。目前它提供了两个命令来完成这项工作。

provision

$ vendor/bin/wp-provisioner provision <VERSION> [--file <PROVISION_FILE>] [--wp-dir <WP_DIR>] [--wp_cli <WP_CLI>]

此命令执行 <VERSION> 定义在 <PROVISION_FILE> 中。默认情况下,配置文件是当前工作目录中的 provision.php

以下是一个示例配置文件,它定义了版本 1.0.0 并安装了一个多站,设置了两个站点并激活了两个插件

<?php # -*- coding: utf-8 -*-

namespace WpProvision\Api;

/**
 * @param \WpProvision\Api\WpProvisioner $api
 * @param \WpProvision\Api\WpCommandProvider $wp
 * @param \WpProvision\Api\ConsoleOutput $output
 */
return function( Versions $versions, WpCommandProvider $wp, ConsoleOutput $output ) {

    // add a provision routine named '1.0.0'. The VersionList contains all provision routines for all versions
    $versions->addProvision(
        '1.0.0',
        function() use ( $wp, $output ) {

            $admin_email = 'david@wp-provisioner.tld';
            $admin_login = 'david';

            // install a multisite
            $wp->core()->multisiteInstall(
                "http://myproject.net",
                [ 'login' => $admin_login, 'email' => $admin_email ]
            );

            // create some sites
            $site_1_id = $wp->site()->create(
                "http://de.myproject.net/",
                [ 'user_email' => $admin_email ]
            );
            $site_2_id = $wp->site()->create(
                "http://fr.myproject.net/shop/",
                [ 'user_email' => $admin_email ]
            );

            // install some plugins (they usually should be as you're using composer, aren't you?)
            $wp->plugin()->activate(
                [ 'woocommerce', 'akismet' ],
                [ 'site_url' => 'http://fr.myproject.net/shop/' ]
            );
            $wp->plugin()->activate(
                'multilingual-press',
                [ 'network' => TRUE ]
            );

            $output->writeln( "Successfully set up version 1.0.0" );
        }
    );
};

task

$ vendor/bin/wp-provisioner task tasks.php [--wp-dir <WP_DIR>] [--wp_cli <WP_CLI>]

任务文件必须返回一个可调用的函数,该函数将使用参数 WpCliCommandProviderConsoleOutput 执行。示例

<?php # -*- coding: utf-8 -*-

/**
 * WP Provisioner task file
 */

namespace WpProvision\Api;

/**
 * @param WpCliCommandProvider $wp
 * @param ConsoleOutput $output
 * 
 * @return int
 */
return function( WpCliCommandProvider $wp, ConsoleOutput $output ) {

	$output->writeln( [ "Fetching post IDs" ] );
	$tables = $wp->db()
		->query( "SELECT ID from wp_posts LIMIT 10" );
	$output->writeln( $tables );
	
	return 0;
};

目标

这个工具的目的是尽可能地自动化配置 WordPress 的过程,以便将其集成到已经自动化的部署流程中。然而,它处于早期 alpha 状态。一些功能尚未实现,并且 API 可能会略有变化。

API

关于 $graceful 参数:每个 create() 方法都有一个名为 $graceful 的布尔参数(通常是最后一个),它使该方法的行为类似于 create-if-not-exists,这是默认行为。如果设置为 FALSE,则方法将抛出异常,例如如果创建了已经存在的站点。

Wp\Core

bool isInstalled( [ bool $network = FALSE ] )

检查 WordPress 是否已安装。

bool install( string $url, array $admin [, array $options = [ ] [, bool $graceful = TRUE ] ] )

安装 WordPress。

bool multisiteConvert( [ array $options = [ ] ] )

将单站转换为多站。 修改您的 wp-config.php(见问题 #1)

bool multisiteInstall( $url, array $admin [, array $options = [ ] [, bool $graceful = TRUE ] ] );

从头开始安装多站。 修改您的 wp-config.php(见问题 #1)

Wp\Plugin

bool activate( string $plugin [ , array $options = [ ] ] )

激活一个插件。

bool deactivate( string $plugin [ , array $options = [ ] ] )

停用插件。

bool isInstalled( string $plugin [ , array $options = [ ] ] )

检查插件是否“已安装”。这意味着插件文件可用于激活。

bool isActive( string $plugin [ , array $options = [ ] ] )

检查插件是否已激活。将 $options[ 'network' ] 设置为 TRUE 以检查网络范围的激活。

bool uninstall( $plugin, array $options = [ ] )

运行插件的卸载程序。这会在(除非您指定 $options[ 'deactivate' ] = FALSE)之前尝试停用插件。 我建议根据插件激活状态手动停用插件。如果您还想删除插件文件,将 $options[ 'delete' ] = TRUE 传递给该方法。

Wp\Site

bool Wp\Site::exists( string $site_url[, int $network_id = 0 ] )

检查网站 URL 是否已存在。

int Wp\Site::siteId( string $site_url[, int $network_id = 0 ] )

从网站 URL 获取网站 ID。

int Wp\Site::create( string $url [, array $attributes = [] [, $network_id = 0 [, bool $graceful = TRUE ] ] ] )

通过完整的 URL 创建一个新的站点。这适用于独立目录或子域安装,但强烈建议使用子域安装。您当然可以省略 URL,并通过 slug 创建站点。

参数

  • $url 网站的完整URL,例如 http://ch.mysite.org/fr/
  • $attributes
    • $attributes[ 'user_email' ] (字符串,必填) 新网站管理员的电子邮件地址(用户必须已存在)
    • $attributes[ 'private' ] (布尔值) 新网站是否为私有
    • $attribute[ 'title' ] (字符串) 新网站的标题。
    • $attribute[ 'slug' ] (字符串) 如果提供,则忽略 $url 参数,并使用此别名(slug)创建网站(它将成为子域名或子目录)
  • $network_id (整数) 新网站应创建的网络ID。默认为 0,表示使用当前网络(由 wp-config.php 定义)
  • $graceful (布尔值) TRUE 表示“如果不存在则创建网站”(默认)并返回现有ID,如果存在则返回,FALSE 否则将抛出异常

Wp\User

int Wp\User::userId( string $email_or_login )

通过电子邮件或登录名获取用户ID。

bool Wp\User::exists( string $email_or_login )

检查通过给定电子邮件或登录名是否存在用户。

int Wp\User::create( string $login, string $email [, array $attributes = [] [, string $site_url = '' [, bool $graceful = TRUE ] ] ] )

创建新用户。

参数

  • $login (字符串,必填) 新用户的登录名
  • $email (字符串,必填) 新用户的电子邮件地址
  • $attributes
    • $attributes[ 'role' ] (字符串) 新用户的角色(必须在给定的网站上存在)
    • $attributes[ 'password' ] (字符串) 明文密码(如果使用版本控制系统,应省略,使用密码找回功能)
    • $attributes[ 'first_name' ] (字符串)
    • $attributes[ 'last_name' ] (字符串)
    • $attributes[ 'display_name' ] (字符串)
    • $attributes[ 'send_mail' ] (布尔值) 向用户发送确认邮件(默认为 FALSE
    • $attibutes[ 'registered_at' ] (DateTimeInterface) 用户注册的时间
  • $site_url (字符串) 用户应注册的网站URL。(默认为主网络网站)
  • $graceful (布尔值) TRUE 表示“如果不存在则创建用户”(默认)并返回现有ID,如果存在则返回,FALSE 否则将抛出异常