Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/micropackage/dochooks
WordPress hooks in method comments. Annotated hooks.
https://github.com/micropackage/dochooks
annotations bracketspace composer-library micropackage wordpress
Last synced: about 1 month ago
JSON representation
WordPress hooks in method comments. Annotated hooks.
- Host: GitHub
- URL: https://github.com/micropackage/dochooks
- Owner: micropackage
- License: mit
- Created: 2020-01-07T07:08:30.000Z (almost 5 years ago)
- Default Branch: develop
- Last Pushed: 2023-06-23T08:52:49.000Z (over 1 year ago)
- Last Synced: 2024-11-24T09:42:11.290Z (about 1 month ago)
- Topics: annotations, bracketspace, composer-library, micropackage, wordpress
- Language: PHP
- Size: 57.6 KB
- Stars: 36
- Watchers: 5
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# DocHooks
[![BracketSpace Micropackage](https://img.shields.io/badge/BracketSpace-Micropackage-brightgreen)](https://bracketspace.com)
[![Latest Stable Version](https://poser.pugx.org/micropackage/dochooks/v/stable)](https://packagist.org/packages/micropackage/dochooks)
[![PHP from Packagist](https://img.shields.io/packagist/php-v/micropackage/dochooks.svg)](https://packagist.org/packages/micropackage/dochooks)
[![Total Downloads](https://poser.pugx.org/micropackage/dochooks/downloads)](https://packagist.org/packages/micropackage/dochooks)
[![License](https://poser.pugx.org/micropackage/dochooks/license)](https://packagist.org/packages/micropackage/dochooks)
## 🧬 About DocHooks
The Laravel or Symfony projects are using method annotations for various things. This helps to have the project organized without too much code. WordPress has no implementation of such concept and this package is a remedy.
DocHooks package allows you to do:
```php
class Example extends HookAnnotations {/**
* @action test
*/
public function test_action() {}/**
* @filter test 5
*/
public function test_filter( $val, $arg ) {
return $val;
}/**
* @shortcode test
*/
public function test_shortcode( $atts, $content ) {
return 'This is test';
}}
$example = new Example();
$example->add_hooks();
```Instead of old:
```php
$example = new Example();add_action( 'test', [ $example, 'test_action' ] );
add_filter( 'test', [ $example, 'test_filter' ], 5, 2 );
add_shortcode( 'test', [ $example, 'test_shortcode' ] );
```## 💾 Installation
``` bash
composer require micropackage/dochooks
```## 🕹 Usage
### Annotations
```
@action
@filter
@shortcode
```The hook and shortcode name is required while default priority is `10`.
You don't provide the argument count, the class will figure it out. Just use the callback params you want.
### Test if DocHooks are working
When OPCache has the `save_comments` and `load_comments` disabled, this package won't work, because the comments are stripped down. [See the fallback solution](#fallback).
```php
use Micropackage\DocHooks\Helper;(bool) Helper::is_enabled();
```### Using within the class
You can extend the HookAnnotations class:
```php
use Micropackage\DocHooks\HookAnnotations;class Example extends HookAnnotations {
/**
* @action test
*/
public function test_action() {}}
$example = new Example();
$example->add_hooks();
```Or use the Trait:
```php
use Micropackage\DocHooks\HookTrait;class Example {
use HookTrait;
/**
* @action test
*/
public function test_action() {}}
$example = new Example();
$example->add_hooks();
```### Using as a standalone object
```php
use Micropackage\DocHooks\Helper;class Example {
/**
* @action test
*/
public function test_action() {}}
$example = Helper::hook( new Example() );
```### Fallback
Because the HookAnnotations object stores the called hooks in `_called_doc_hooks` property, you are able to pull them out and parse them into a list of old `add_action`, `add_filter` and `add_shortcode` functions.
For this you'll need a central "repository" of all objects with hooks ie. Runtime class. [See the example of this approach](https://github.com/BracketSpace/Notification/blob/master/src/Cli/DumpHooks.php) in the Notification plugin, which uses the WP CLI to dump all the hooks into separate file.
## 📦 About the Micropackage project
Micropackages - as the name suggests - are micro packages with a tiny bit of reusable code, helpful particularly in WordPress development.
The aim is to have multiple packages which can be put together to create something bigger by defining only the structure.
Micropackages are maintained by [BracketSpace](https://bracketspace.com).
## 📖 Changelog
[See the changelog file](./CHANGELOG.md).
## 📃 License
This software is released under MIT license. See the [LICENSE](./LICENSE) file for more information.