nolte/wp-endpoint 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

nolte/wp-endpoint

Composer 安装命令:

composer require nolte/wp-endpoint

包简介

wp-endpoint

README 文档

README

Class that makes the process to create a new WordPress endpoint more easily.

By default the endpoint are under the leen namespace with the version number 1 this data can be overwritten using any of the filters specified below.

The class provides a set of defined methods and mechanism to register a new endpoint, wich makes the process of define a new endpoint pretty straightforward.

Getting Started

The easiest way to install this package is by using composer from your terminal:

composer require nolte/wp-endpoint

Or by adding the following lines on your composer.json file

"require": {
  "nolte/wp-endpoint": "dev-master"
}

This will download the files from the packagist site and set you up with the latest version located on master branch of the repository.

After that you can include the autoload.php file in order to be able to autoload the class during the object creation.

include '/vendor/autoload.php';

Usage

After you added the dependency on your module you need to create a new class that defines your endpoint for example:

<?php
use Nolte\AbstractEndpoint;

class customEndpoint extends AbstractEndpoint {

	protected $endpoint = '/customEndpoint';

	public function endpoint_callback( \WP_REST_Request $request ) {
		$data = [
		    'data' => 'Hi',
		    'count' => 10,
            'id' => $request->get_param( 'id' )
		];
		return $this->filter_data( $data );
	}

	public function endpoint_args() {
		return [
			'id' => [
				'required' => true,
				'sanitize_callback' => function ( $id ) {
					return absint( $id );
				},
			],
		];
	}
}

On this class you have:

  • init or any other name you want to expose is a public static method that always have the same signature in order to execute the code that runs the new endpoint.
  • endpoint_callback , By default you have one method that allow you to define the response of the GET request to the endpoint, GET is the default HTTP Verb.
  • endpoint_args, Same as endpoint_callback, is the callback by default that allow you to define if the endpoint allow parameters and how are going to be used.
  • endpoint_options, if you want to specify a new set of HTTP verbs andor rewrite the default endpoint options, you only need to return an array with the parameters that are required for the endpoint.

For instance to define a new slug parameter with a different HTTP verb and reuse the default get params. You only need to define as follows

	protected function endpoint_options() {
		return [
		    [
			'methods' => \WP_REST_Server::DELETABLE,
			'callback' => [ $this, 'custom_callback_now' ],
			],
			parent::endpoint_options,
		];
	}

parent::endpoint_options is going to return the default options defined on Endpoint class if you don't want to redeclare the options for a GET request.

Collections

There is also an abstract class available for collections.

<?php namespace Nolte\Endpoints;

use Nolte\AbstractCollectionEndpoint;

class MyCollection extends AbstractCollectionEndpoint {

	protected $endpoint = '/my-collection';

	protected function loop() {
		$data = [];

		$this->query = new \WP_Query( $this->args );
		while ( $this->query->have_posts() ) {
			$this->query->the_post();
			$data[] = $this->query->post;
		}

		wp_reset_postdata();

		return [
			'data' => $data,
			'pagination' => $this->get_pagination(
				$this->query->found_posts,
				$this->query->max_num_pages
			)
		];
	}
}

Filters

  • nolte_endpoints_api_namespace, This allow you to overwrite the default namespace used on the API definition.
  • nolte_endpoints_api_version, This filter allow you to change the version number of the API.
  • nolte_endpoints_data_${api}, where ${api} is the name of your endpoint for instance in the case above: nolte_endpoints_data_customEndpoint.
  • nolte_endpoints_collection_item, to do custom formatting for collection items.

nolte/wp-endpoint 适用场景与选型建议

nolte/wp-endpoint 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 27.62k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2017 年 11 月 23 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 nolte/wp-endpoint 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 nolte/wp-endpoint 我们能提供哪些服务?
定制开发 / 二次开发

基于 nolte/wp-endpoint 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 27.62k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 1
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 4
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-11-23