spenserhale/wp-autohook-library

A library for automatically hooking classes into WordPress actions and filters.

1.1.1 2024-04-16 04:20 UTC

This package is auto-updated.

Last update: 2024-04-16 04:23:15 UTC


README

Screen Shot

WP AutoHooks is a PHP Library that allows you to define Attributes on Classes and Methods to document and register WordPress Hooks.

Features

  • Developer Experience: Document hooks next to the method, giving you context and a better developer experience.
  • Performance: Hooks are registered without loading classes or instantiating objects until needed.
  • Flexibility: The underlying PHP Library can be used for both simple and complex projects.
  • Modularity: Ability to add and remove standalone classes quickly and easily.

Getting Started

Prerequisites

By default, the library is geared toward Composer, but there is flexibility, and you can integrate the library with your class loading system.

Installation

composer require spenserhale/wp-autohook-library

Basic Usage

Attribute your class and methods with the Hook and Shortcode attributes.

 #[Hook('cli_init')]
 public static function register(): void {...}

Build/Wire Up

// Get the list of classes
$classes = \SH\AutoHook\ComposerJsonParser::getClasses($composerJsonPath);

// Process classes to string
[$output] = \SH\AutoHook\AttributeResolver::processClassesToString($classes);

// Write to file
(bool) $written = \SH\AutoHook\FileWriter::write($output, $outputPath);

Advanced Usage

Some projects may not have the composer.json available at runtime, so you can use the class loader object.

Build/Wire Up

// Get Classloader object
$loader = require 'vendor/autoload.php';

// Get the list of classes
$classes = \SH\AutoHook\ComposerClassLoaderParser::getClasses($loader, ['App\\', 'MyNamespace\\']);

// Process classes to string
[$output] = \SH\AutoHook\AttributeResolver::processClassesToString($classes);

// Write to file
(bool) $written = \SH\AutoHook\FileWriter::write($output, $outputPath);

Tests

To run tests, make sure to create class list through composer:

composer du -o

Then run the tests:

 composer test

License

The WordPress AutoHooks Library is open-sourced software licensed under the MIT license.

(back to top)