承接 gmazzap/url-to-query 相关项目开发

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

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

gmazzap/url-to-query

Composer 安装命令:

composer require gmazzap/url-to-query

包简介

Allow resolving any kind of WordPress url to related main query arguments.

关键字:

README 文档

README

A WordPress plugin that allow resolving any kind of WordPress url (even from custom rewrite rules) to related main query arguments.

#Requirements

  • PHP 5.4+
  • WordPress 3.9+
  • Composer to install

#Installation

The plugin is a Composer package and can be installed in plugin directory via:

composer create-project gmazzap/url-to-query --no-dev

What & Why

WP comes with a set of tools to convert custom urls into specific query variables: is possible to change the permalink structure, and there is also an API to add completely custom rules, however, there is not a way to reverse the process: i.e. to know to which query arguments an arbitrary url is connected to.

The url resolving is done in core by parse_request method of WP class saved in the global $wp variable.

Using that method for the purpose explained above is hard/discouraged because:

  • it directly accesses to $_SERVER, $_POST and $_GET variables, making hard to parse arbitrary urls not related with current HTTP request
  • it triggers some action hooks strictly related to current HTTP request parsing, that makes no sense to trigger for arbitrary urls
  • it accesses and modifies properties of global $wp variable that should not be changed after request is parsed or very likely things will break

This is the reason why I wrote this simple plugin, it adds a template tag url_to_query that accepts an url and returns related main query arguments.

##How to use##

$args = url_to_query( 'http://example.com/sample-page' );
// $args = array(  'pagename' => 'sample-page' );

$args = url_to_query( 'http://example.com/category/uncategorized/' )
// $args = array(  'category_name' => 'uncategorized' );

It is also possible to pass a relative url:

$args = url_to_query( '/sample-page' );
// $args = array(  'pagename' => 'sample-page' );

###Using query string###

When pretty permalinks are not used, (sometimes even in that case) WordPress can make use of query string in the urls to set query arguments. The plugin works perfectly with them:

$args = url_to_query( '/?attachment_id=880' );
$args = array(  'attachment_id' => '880' );

To simplify this task, url_to_query accepts a second argument: an array of query vars to be considered in the same way core considers $_REQUEST variables when an url is parsed:

$args = url_to_query( '/', array( 'attachment_id' => '880' ) );
// $args = array(  'attachment_id' => '880' );

Note that the array passed as second argument is not straight merged to the query vars, only valid query vars will be used, just like core does when parse urls:

$args = url_to_query( '/', array( 'attachment_id' => '880', 'foo' => 'bar' ) );
// $args = array(  'attachment_id' => '880' );

###Custom rewrite rules###

Plugin works with no problems with custom rewrite rules, just few things to consider:

  • url_to_query have to be called after query rules are added, or it can't recognize them
  • just like for core, rewrite rules have to be flushed before url_to_query can recognize a newly added rule
  • just like core, if the rule contains custom query variables, they have to be allowed, maybe using add_rewrite_tag or using 'query_vars' filter (see Codex example)

Example:

add_action( 'init', 'my_rew_rules' );

function my_rew_rules() {
  add_rewrite_tag('%food%', '([^&]+)');
  add_rewrite_tag('%variety%', '([^&]+)');
  add_rewrite_rule(
    '^nutrition/([^/]*)/([^/]*)/?',
    'index.php?page_id=12&food=$matches[1]&variety=$matches[2]',
    'top'
  );
}

add_action( 'footer' function() {
  $args = url_to_query( '/nutrition/cake/cherry/' )
  // $args = array( 'page_id' => '12', 'food' => 'cake', 'variety' => 'cherry' );
} );

###Plugin classes###

Even if plugin provides the 'url_to_query' template tag, it internally uses two classes to resolve urls and it is possible directly use them, instead of the template tag. Indeed, only one of them should be used, the second is used internally.

$resolver = new GM\UrlToQuery();
$args = $resolver->resolve( 'http://example.com/sample-page', array( 'page' => '2' ) );
// $args = array( 'pagename' => 'sample-page', 'page' => '2' );

So resolve() method of GM\UrlToQuery works exactly in the same way url_to_query function does.

The same instance of GM\UrlToQuery can be used to resolve different urls:

$resolver = new GM\UrlToQuery();

$args1 = $resolver->resolve( 'http://example.com/sample-page', array( 'page' => '2' ) );
// $args1 = array( 'pagename' => 'sample-page', 'page' => '2' );

$args2 = $resolver->resolve( '/?attachment_id=880' );
// $args2 = array( 'attachment_id' => '880' );

$args3 = $resolver->resolve( 'http://example.com/category/uncategorized/' );
// $args3 = array( 'category_name' => 'uncategorized' );

================

License

Url_To_Query is released under MIT.

gmazzap/url-to-query 适用场景与选型建议

gmazzap/url-to-query 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.97k 次下载、GitHub Stars 达 31, 最近一次更新时间为 2014 年 10 月 30 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 gmazzap/url-to-query 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 2.97k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 33
  • 点击次数: 22
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-10-30