定制 cedaro/wprestcop 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

cedaro/wprestcop

Composer 安装命令:

composer require cedaro/wprestcop

包简介

A WordPress plugin for managing access to the WP REST API.

关键字:

README 文档

README

Manage access to the WP REST API with rate limits and IP-based rules.

Contributors: Brady Vercher Requires: WP 4.4+, PHP 5.4+ Tested up to: 4.4 License: GPL-2.0+

Rate Limits

Rate limits allow for configuring the number of requests a client can make within a certain interval. The default in WP Rest Cop is 500 requests per hour.

The rate limit functionality requires a persistent object cache.

Headers

A few headers are sent with every request so clients can keep track of their current limit:

Header Description
X-RateLimit-Limit Requests allowed per interval.
X-RateLimit-Remaining Remaining requests allowed in the current interval.
X-RateLimit-Reset Seconds until the limit is reset.

If client has reached their limit, an additional header will be sent.

Header Description
Retry-After Seconds until the limit is reset

Clients may send a HEAD request to view their current limit without ticking the meter.

Configuring Settings

Configure the default limit and interval settings using the simple API from the main plugin instance:

<?php
/**
 * Set the rate limit to 10 requests every 5 minutes.
 */
add_action( 'wprestcop_plugin_loaded', function( $wprestcop ) {
	$wprestcop
		->set_limit( 10 )
		->set_interval( 5 * MINUTE_IN_SECONDS );
} );

Settings can also be configured with the built-in WP CLI commands.

Disable Rate Limiting

If you just want the IP rules functionality and want to disable the rate limits, set the interval to -1.

IP Rules

IP rules can be configured globally, or at the route level as a simple whitelist or blacklist.

Global Configuration

<?php
/**
 * Global IP rules configuration.
 */
add_action( 'wprestcop_plugin_loaded', function( $wprestcop ) {
	$wprestcop->get_ip_rules()
		->allow( '192.168.50.4' ); // Also accepts an array of IP addresses.

	// Or...

	$wprestcop->get_ip_rules()
		->deny( '66.249.66.1' ); // Also accepts an array of IP addresses.
} );

When allowing an IP address, the policy is to deny any requests from IPs not in the whitelist.

The opposite is true when denying IP addresses. All IPs not in the blacklist will have access.

Global IP rules can also be configured with the built-in WP CLI commands.

Route Configuration

Routes may also be configured with their own IP rules:

<?php
/**
 * Register routes.
 */
add_action( 'rest_api_init', function () {
    register_rest_route( 'myplugin/v1', '/internal/(?P<id>\d+)', [
        'methods'  => 'GET',
        'callback' => 'my_awesome_expensive_func',
        'ips'      => [
            'allow' => [ '192.168.50.4' ],
            'deny'  => [ '66.249.66.1' ],
        ]
    ] );
} );

WP CLI Commands

A few WP CLI commands are included to configure the plugin without requiring code.

Command Description
wp restcop allow <ip>... Whitelist one or more IPs.
wp restcop check <ip> Check whether an IP has access.
wp restcop deny <ip>... Blacklist one or more IPs.
wp restcop set <key> <value> Update a setting value.
wp restcop status View global IP rules.

Potential Roadmap

  • Support for logging various events.
  • Additional rate limit strategies.
  • More route-level capabilities.
  • Advanced access rules.
  • Administration UI.

cedaro/wprestcop 适用场景与选型建议

cedaro/wprestcop 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 169 次下载、GitHub Stars 达 73, 最近一次更新时间为 2016 年 02 月 06 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「wordpress」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

围绕 cedaro/wprestcop 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 169
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 73
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 73
  • Watchers: 9
  • Forks: 12
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-2.0
  • 更新时间: 2016-02-06