stevegrunwell/one-time-callbacks 问题修复 & 功能扩展

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

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

stevegrunwell/one-time-callbacks

Composer 安装命令:

composer require stevegrunwell/one-time-callbacks

包简介

Enable WordPress actions and filter callbacks to be called exactly once.

README 文档

README

Build Status Coverage Status GitHub release

The The WordPress plugin API is a fantastic way for third-party scripts to be able to inject themselves into the WordPress lifecycle. Thanks to WordPress actions and filters (collectively "hooks"), theme and plugin developers can introduce all sorts of new functionality.

Occasionally, however, the "all or nothing" mentality of WordPress hooks can put developers in a pinch, since they only want their callback to run once. For example, maybe your theme has a simple add_top_story_class() function, which appends .top-story to a list of classes. If you only wanted to apply it to the first post in a loop, you might find yourself writing code like this:

<?php while ( $query->have_posts() ) : $query->the_post(); ?>

  <?php if ( 0 === $query->current_post ) add_filter( 'post_class', 'add_top_story_class' ); ?>

  <article <?php post_class(); ?>>
    <h2><?php the_title(); ?></h2>
    <?php the_excerpt(); ?>
  </article>

  <?php remove_filter( 'post_class', 'add_top_story_class' ); ?>

<?php endwhile; ?>

Yuck! We're conditionally adding a filter based on the current post's index, then removing the filter at the end to ensure it isn't getting applied to every post.

With One-time callbacks, this becomes much cleaner:

<?php while ( $query->have_posts() ) : $query->the_post(); ?>

  <?php add_filter_once( 'post_class', 'add_top_story_class' ); ?>

  <article <?php post_class(); ?>>
    <h2><?php the_title(); ?></h2>
    <?php the_excerpt(); ?>
  </article>

<?php endwhile; ?>

The add_filter_once() function will let the callback execute exactly one time, then it will automatically clean up after itself. It's a small but helpful tool for themes and plugins that make heavy use of actions and filters.

Installation

The best way to install this package is via Composer:

$ composer require stevegrunwell/one-time-callbacks

The package ships with the composer/installers package, enabling you to control where you'd like the package to be installed. For example, if you're using One-time Hooks in a WordPress plugin, you might store the file in an includes/ directory. To accomplish this, add the following to your plugin's composer.json file:

{
    "extra": {
        "installer-paths": {
            "includes/{$name}/": ["stevegrunwell/one-time-callbacks"]
        }
    }
}

Then, from within your plugin, simply include or require the file:

require_once __DIR__ . '/includes/one-time-callbacks/one-time-callbacks.php';

Using as a plugin

If you'd prefer, the package also includes the necessary file headers to be used as a WordPress plugin.

After downloading or cloning the package, move one-time-callbacks.php into either your wp-content/mu-plugins/ (preferred) or wp-content/plugins/ directory. If you chose the regular plugins directory, you'll need to activate the plugin manually via the Plugins › Installed Plugins page within WP Admin.

Bundling within a plugin or theme

One-time Callbacks has been built in a way that it can be easily bundled within a WordPress plugin or theme, even commercially.

Each function declaration is wrapped in appropriate function_exists() checks, ensuring that multiple copies of the library can co-exist in the same WordPress environment.

Usage

One-time Callbacks provides the following functions for WordPress:

add_action_once()

Register an action to run exactly one time.

The arguments match that of add_action(), but this function will also register a second callback designed to remove the first immediately after it runs.

Parameters

(string) $hook
The action name.
(callable) $callback
The callback function.
(int) $priority
Optional. The priority at which the callback should be executed. Default is 10.
(int) $args
Optional. The number of arguments expected by the callback function. Default is 1.

Return value

Like add_action(), this function always returns true.

add_filter_once()

Register a filter to run exactly one time.

The arguments match that of add_filter(), but this function will also register a second callback designed to remove the first immediately after it runs.

Parameters

(string) $hook
The action name.
(callable) $callback
The callback function.
(int) $priority
Optional. The priority at which the callback should be executed. Default is 10.
(int) $args
Optional. The number of arguments expected by the callback function. Default is 1.

Return value

Like add_filter(), this function always returns true.

License

Copyright 2018 Steve Grunwell

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

stevegrunwell/one-time-callbacks 适用场景与选型建议

stevegrunwell/one-time-callbacks 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 47.38k 次下载、GitHub Stars 达 68, 最近一次更新时间为 2018 年 02 月 17 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 stevegrunwell/one-time-callbacks 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 47.38k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 68
  • 点击次数: 18
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 68
  • Watchers: 2
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-02-17