Giter VIP home page Giter VIP logo

piotrpress / wordpress-hooks Goto Github PK

View Code? Open in Web Editor NEW
33.0 3.0 0.0 30 KB

The library allows using PHP Attributes (introduced in PHP version 8) to automagically add WordPress Hooks (Filters and Actions) to objects' methods.

License: GNU General Public License v3.0

PHP 100.00%
wordpress wp wordpress-plugin wp-plugin hooks hook php8 php-attibutes attributes filters actions wordpress-hooks wordpress-oop wordpress-oop-php wordpress-package wordpress-composer wordpress-library

wordpress-hooks's Introduction

WordPress Hooks

This library uses PHP Attributes (introduced in PHP version 8.0) to automagically add/remove WordPress Hooks (Filters and Actions) to/from functions and methods.

Installation

$ composer require piotrpress/wordpress-hooks

Load

require __DIR__ . '/vendor/autoload.php';

Usage

Attributes

#[ Action( string $hook_name, int $priority = 10 ) ]
#[ Filter( string $hook_name, int $priority = 10 ) ]

Functions

Hooks::add( object $object = null, string $callback = '', PiotrPress\CacheInterface $cache = null ) : void
Hooks::remove( object $object = null, string $callback = '', PiotrPress\CacheInterface $cache = null ) : void

Examples

Hooks::add/remove( $object )

If object argument is passed and callback is omitted, then all hooks from object are added or removed.

use PiotrPress\WordPress\Hooks;
use PiotrPress\WordPress\Hooks\Action;
use PiotrPress\WordPress\Hooks\Filter;

class Example {
    public function add_hooks() {
        Hooks::add( $this );
    }

    #[ Action( 'init' ) ]
    public function example_init() : void {
        // do something
    }

    #[ Filter( 'the_title', 1 ) ]
    public function example_the_title( string $post_title, int $post_id ) : string {
        // do something
    }
}

$example = new Example();
$example->add_hooks();

Hooks::remove( $example );

This is an equivalent to:

$example = new Example();

add_action( 'init', [ $example, 'example_init' ] );
add_filter( 'the_title', [ $example, 'example_the_title' ], 1, 2 );

remove_action( 'init', [ $example, 'example_init' ] );
remove_filter( 'the_title', [ $example, 'example_the_title' ], 1, 2 );

Note: Hooks::add/remove() methods can be called from the method, or even outside the object.

Hooks::add/remove( $object, $callback )

If object and callback arguments are passed, then only hooks for this method are added or removed.

use PiotrPress\WordPress\Hooks;
use PiotrPress\WordPress\Hooks\Action;
use PiotrPress\WordPress\Hooks\Filter;

class Example {
    public function add_hooks() {
        Hooks::add( $this, 'example_init' );
        Hooks::add( $this, 'example_the_title' );
    }

    #[ Action( 'init' ) ]
    public function example_init() : void {
        // do something
    }

    #[ Filter( 'the_title', 1 ) ]
    public function example_the_title( string $post_title, int $post_id ) : string {
        // do something
    }
}

$example = new Example();
$example->add_hooks();

Hooks::remove( $example, 'example_init' );
Hooks::remove( $example, 'example_the_title' );

This is an equivalent to:

$example = new Example();

add_action( 'init', [ $example, 'example_init' ] );
add_filter( 'the_title', [ $example, 'example_the_title' ], 1, 2 );

remove_action( 'init', [ $example, 'example_init' ] );
remove_filter( 'the_title', [ $example, 'example_the_title' ], 1, 2 );

Hooks::add/remove( callback: $callback )

If object argument is omitted and callback is passed, then only hooks for this function are added or removed.

use PiotrPress\WordPress\Hooks;
use PiotrPress\WordPress\Hooks\Action;
use PiotrPress\WordPress\Hooks\Filter;

#[ Action( 'init' ) ]
public function example_init() : void {
    // do something
}

#[ Filter( 'the_title', 1 ) ]
public function example_the_title( string $post_title, int $post_id ) : string {
    // do something
}

Hooks::add( callback: 'example_init' );
Hooks::add( callback: 'example_the_title' );

Hooks::remove( callback: 'example_init' );
Hooks::remove( callback: 'example_the_title' );

This is an equivalent to:

add_action( 'init', 'example_init' );
add_filter( 'the_title', 'example_the_title', 1, 2 );

remove_action( 'init', 'example_init' );
remove_filter( 'the_title', 'example_the_title', 1, 2 );

Cache

Optionally, you can pass a cache object, which must implement PiotrPress\CacheInterface interface, as a third cache argument to Hooks::add/remove() methods.

This will cache the result of Hooks::get() method, which provides a list of hooks for a given object, method or function using Reflection API, so caching its result can significantly improve the performance.

Example

use PiotrPress\Cacher;
use PiotrPress\WordPress\Hooks;
use PiotrPress\WordPress\Hooks\Action;
use PiotrPress\WordPress\Hooks\Filter;

class Example {
    #[ Action( 'init' ) ]
    public function example_init() : void {
        // do something
    }

    #[ Filter( 'the_title', 1 ) ]
    public function example_the_title( string $post_title, int $post_id ) : string {
        // do something
    }
}

$example = new Example();
$cache = new Cacher( '.hooks' );

Hooks::add( object: $example, cache: $cache );
Hooks::remove( object: $example, cache: $cache );

Note: You can use simple file-based cache, which is provided by PiotrPress\Cacher library distributed with this library.

Kudos

Inspirations, feedback, ideas and feature requests provided by:

Requirements

PHP >= 8.0 version.

License

GPL3.0

wordpress-hooks's People

Contributors

piotrpress avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.