alesanchezr/wpas-wordpress-dash

WordPress 开发加速器

1.3.4 2020-11-03 05:01 UTC

This package is auto-updated.

Last update: 2024-09-29 05:14:36 UTC


README

你是 WordPress 开发者吗?那么你可能正在为和我每天一样的事情挣扎。

  1. WordPress 中的 MVC 模式实现(模型-视图-控制器)。
  2. 使用 WordPress 快速创建 API。

安装

  1. 使用 composer 需要库(注意:运行此命令之前,您必须在您的 WordPress 目录中。安装程序将尝试在 ./wp-content/<your_theme_directory_name> 中创建您的主题)
$ composer require alesanchezr/wpas-wordpress-dash:dev-master
  1. 使用安装脚本创建一个新主题。或者选择一个已经创建的主题(它将尝试自动创建文件夹结构)
$ php vendor/alesanchezr/wpas-wordpress-dash/run.php <your_theme_directory_name>
  1. 根据您的需求在 functions.php 中更新 WPASController
use \WPAS\Controller\WPASController;
$controller = new WPASController([
        //Here you specify the path to your consollers folder
        'namespace' => 'php\\Controllers\\'
    ]);

注意:此库期望您的主题在 functions.php 中加载 vendor/autoload.php 文件。一种好的方法是

/**
* Autoload for PHP Composer and definition of the ABSPATH
*/

//defining the absolute path for the wordpress instalation.
if ( !defined('ABSPATH') ) define('ABSPATH', dirname(__FILE__) . '/');

//including composer autoload
require ABSPATH."vendor/autoload.php";

如果您正在使用 Flywheel 托管(以及/或 Flywheel Local),您需要以下方式要求路径

if(!strpos($_SERVER['SERVER_NAME'], '.local')){
  require 'vendor/autoload.php';
}else{
  require ABSPATH . 'vendor/autoload.php';
}

这是因为他们在推送网站上线后,将内容、插件与根 WordPress 安装分开的文件夹结构。

MVC 模式的工作方式

模型(自定义类型)

实例化 PostTypeManager

    $typeManager = new \WPAS\Types\PostTypesManager([
        'namespace' => '\php\Types\\' \\this will be the path to your models folder
    ]);

在 functions.php 中定义您的类型

    //You can react a new custom post type and specify his class
    $typeManager->newType(['type' => 'your_type_slug', 'class' => 'AnyPostTypeModelClass'])->register();

在 types 文件夹中定义您的类型类

    namespace php\Types;

    class AnyPostTypeModelClass extends \WPAS\Types\BasePostType{
    
        //any method here
    }

注意:你必须从 BasePostType 类扩展,这不是可选的。

继续阅读有关模型的内容

控制器

创建您的 Controller 类,并将它们绑定到视图、页面、分类、帖子等。

//Here we are saying that we have a class Course.php with a function getCourseInfo that fetches the data needed to render any custom post tipe course
$controller->route([ 'slug' => 'Single:course', 'controller' => 'Course' ]);  

我们的 Course.php 控制器类将如下所示

namespace php\Controllers;

class Course{
    
    public function renderCourse(){
        
        $args = [];
        $args['course'] = WP_Query(['post_type' => 'course', 'param2' => 'value2', ...);
        return $args; //Always return an Array type
    }
    
}

继续阅读有关在 WordPress 上实现 MVC 的内容

调试

在 wo-config.php 文件中添加以下常量

define('WP_DEBUG_CONTEXT', true);

它将添加一个顶部的当前正在使用的模板栏。

即将推出的实验性功能(不稳定)

  1. 以编程方式添加 WordPress 角色。
  2. 限制角色对特定页面、帖子、分类等的访问。
  3. 仅用几行代码创建和管理所有自定义帖子类型。
  4. Google Page Speed 测试 中达到 100%。
  5. 使用 WordPress 标准的 WordPress 管理用户的消息通知系统。
  6. 仅用 5 行代码为您的主题创建新的 Visual Composer 组件。
  7. 扩展 Gravity Forms 功能。

作者

Alejandro Sanchez

仓库网站: https://github.com/alesanchezr/wpas-wordpress-dash

关于我: alesanchezr.com