承接 johnbillion/args 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

johnbillion/args

Composer 安装命令:

composer require johnbillion/args

包简介

I don't want to get into an argument about this.

README 文档

README

Args

Many functions and methods in WordPress accept arguments as an associative array which your IDE or code editor cannot autocomplete like it does for individual function parameters.

$query = new WP_Query( [
	'post_type' => 'post',
	'category' => 'does this accept an ID or a slug?',
	'number_of_...errr'
] );

This library provides well-documented classes which represent many of the associative array parameters used throughout WordPress. Using them at the point where you populate the arguments means you get autocompletion and intellisense in your code editor, and strict typing thanks to typed properties. Comprehensive types and constraints for PHPStan are also included.

Current status

Last updated for WordPress 7.0.

Requirements

  • PHP 8.0+

Installation

composer require johnbillion/args

Changes in version 2

In Args version 2.0 and higher:

  • Many arguments have had their type strictness increased for added type safety.
  • The Base class which is implemented by all of the Args classes no longer implements ArrayAccess, Countable, or IteratorAggregate. See this issue for further information.
  • PHP 8.0+ is now required.

Usage

Usage with a class constructor:

$args = new \Args\WP_Query;

$args->tag = 'amazing';
$args->posts_per_page = 100;

$query = new \WP_Query( $args->toArray() );

Usage with a procedural function parameter:

$args = new \Args\register_post_type;

$args->show_in_rest = true;
$args->taxonomies = [ 'genre', 'audience' ];

$story = register_post_type( 'story', $args->toArray() );

Meta queries, tax queries, and date queries

The query classes in WordPress support variously meta_query, tax_query, and date_query arguments. These are fully supported and you can construct them in a structured and strongly typed way.

Creating a meta_query argument:

$args = new \Args\WP_Query;

// Create a clause
$clause = new \Args\MetaQuery\Clause;
$clause->key = 'my_meta_key';
$clause->value = 'my_meta_value';

// Add the clause
$args->meta_query->clauses[] = $clause;

$query = new \WP_Query( $args->toArray() );

Creating a tax_query argument:

$args = new \Args\WP_Query;

// Create a clause
$clause = new \Args\TaxQuery\Clause;
$clause->taxonomy = 'post_tag';
$clause->terms = [ 'amazing' ];

// Add the clause
$args->tax_query->clauses[] = $clause;

$query = new \WP_Query( $args->toArray() );

Creating a date_query argument:

$args = new \Args\WP_Query;

// Create a clause
$clause = new \Args\DateQuery\Clause;
$clause->year = 2000;
$clause->compare = '>=';

// Add the clause
$args->date_query->clauses[] = $clause;

$query = new \WP_Query( $args->toArray() );

Nested queries are supported:

$args = new \Args\WP_Query;

// Create some clauses
$clause1 = new \Args\MetaQuery\Clause;
$clause1->key = 'my_meta_key';
$clause1->value = 'my_meta_value';

$clause2 = new \Args\MetaQuery\Clause;
$clause2->key = 'another_meta_key';
$clause2->value = '100';
$clause2->compare = '>';

// Create a nested query with a clause
$nested = new \Args\MetaQuery\Query;
$nested->addClause( $clause2 );

// Add the clause and nested query
$args->meta_query->clauses[] = $clause1;
$args->meta_query->queries[] = $nested;

$query = new \WP_Query( $args->toArray() );

Alternatively you can construct a complete query object by calling the fromArray() static method with the same nested array syntax that WordPress core uses:

$args = new \Args\WP_Query;

// Set the meta query from an array
$array = [
	[
		'key' => 'my_meta_key',
		'value' => 'my_meta_value',
	]
];
$args->meta_query = $args->meta_query::fromArray( $array );

$query = new \WP_Query( $args->toArray() );

What's provided

Posts

  • \Args\WP_Query
  • \Args\register_post_type
  • \Args\wp_insert_post
  • \Args\wp_update_post
  • \Args\get_posts
  • \Args\register_post_meta
  • \Args\register_post_status

Taxonomies and terms

  • \Args\WP_Term_Query
  • \Args\register_taxonomy
  • \Args\wp_insert_term
  • \Args\wp_update_term
  • \Args\get_terms
  • \Args\get_categories
  • \Args\get_tags
  • \Args\register_term_meta
  • \Args\wp_count_terms
  • \Args\wp_get_object_terms
  • \Args\wp_dropdown_categories

Users

  • \Args\WP_User_Query
  • \Args\wp_insert_user
  • \Args\wp_update_user
  • \Args\get_users

Comments

  • \Args\WP_Comment_Query
  • \Args\get_comments

HTTP API

  • \Args\wp_remote_get
  • \Args\wp_remote_post
  • \Args\wp_remote_head
  • \Args\wp_remote_request
  • \Args\wp_safe_remote_get
  • \Args\wp_safe_remote_post
  • \Args\wp_safe_remote_head
  • \Args\wp_safe_remote_request

Blocks

  • \Args\WP_Block_Type
  • \Args\register_block_type

Abilities

  • \Args\wp_register_ability
  • \Args\wp_register_ability_category

Customizer

  • \Args\WP_Customize_Control
  • \Args\WP_Customize_Manager
  • \Args\WP_Customize_Panel
  • \Args\WP_Customize_Section
  • \Args\WP_Customize_Setting

Everything else

  • \Args\paginate_links
  • \Args\register_meta
  • \Args\register_rest_field
  • \Args\register_setting
  • \Args\wp_get_nav_menus
  • \Args\wp_nav_menu
  • \Args\wp_die
  • \Args\wp_dropdown_languages
  • \Args\wp_generate_tag_cloud

Type checking

Typed class properties are implemented in this library where possible. If you pass a value of the wrong type to an argument that is typed, you'll get a fatal error as long as you're using strict types:

<?php
declare( strict_types=1 );

No more mysterious bugs due to incorrect types.

Note that several parameters in WordPress accept multiple types, for example the $ignore_sticky_posts argument for \WP_Query can be a boolean or an integer. In some of these cases I've opted to type the parameter with the most appropriate type even though it can technically accept other types.

Static analysis

PHPStan-specific @phpstan-var tags are used for properties that have a fixed set of values or other constraints. This allows for even greater type and value checking via static analysis with PHPStan.

Ensure you're using a recent version of PHPStan to make the best use of these constraints.

Contributing

Check out CONTRIBUTING.md for information about generating your own Args definitions or contributing to the Args library.

But why?

I have a name for these array-type parameters for passing arguments. I call them Stockholm Parameters. We've gotten so used to using them that we forget what a terrible design pattern it is. This library exists to work around the immediate issue without rearchitecting the whole of WordPress.

Sponsors

The time that I spend maintaining this library and others is in part sponsored by:

Automattic

ServMask

Plus all my kind sponsors on GitHub:

Sponsors

Click here to find out about supporting my open source tools and plugins.

License: GPLv2

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

johnbillion/args 适用场景与选型建议

johnbillion/args 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.11M 次下载、GitHub Stars 达 119, 最近一次更新时间为 2020 年 07 月 09 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 1.11M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 119
  • 点击次数: 13
  • 依赖项目数: 8
  • 推荐数: 0

GitHub 信息

  • Stars: 119
  • Watchers: 4
  • Forks: 6
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-2.0-or-later
  • 更新时间: 2020-07-09