DWS WP Framework
  • Welcome
  • Primary goals
    • Modular design
    • No 3rd-party dependencies
  • Key concepts and dev tools
    • PHP and WP requirements
    • Object-Oriented Programming
    • Semantic Versioning
    • Version Control (git / GitHub)
    • Dependency Management (Composer)
    • Automated Testing (Codeception + Github Actions)
    • Dependency Injection (PHP-DI)
    • Coding Standards (PHPCS and PHPMD)
    • Dependencies Scoping (PHP-Scoper)
    • TypeScript and Sass
    • Task Runners (Grunt)
  • Setting up your dev environment
    • Windows
  • Your first plugin
    • Multiple plugins using the framework on the same site
  • Frequently Asked Questions
  • Bootstrapper Module
    • Motivation
    • How it works
    • How to use
    • White Labeling
  • Helpers Module
    • Motivation
    • How to use
  • Foundations Module
    • Motivation and How to use
    • Actions
      • Local action traits
      • Extension action traits
      • Integration action traits
    • States
    • Utilities
      • Stores
      • Handlers and Services
        • Logging Service
  • Plugin
    • Main Plugin Instance
    • Plugin Components
  • Hierarchies
  • Helpers
  • Utilities Module
    • Motivation and How to use
    • Hooks Service
      • Scoped Handler
    • Shortcodes Service
    • Templating Service
    • Assets Service
      • Scripts Handler
      • Styles Handler
    • CRON Events Service
      • Action Scheduler Handler
    • Admin Notices Service
    • Dependencies Service
    • Validation Service
  • Core Module
    • Motivation and How to use
    • Plugin Tree
      • Plugin Root
      • Plugin Functionality
    • Plugin Components
      • Internationalization
      • Installation / Upgrade / Uninstallation
  • Settings Module
    • Motivation and How to use
    • Settings Service
      • WordPress Handler
      • MetaBox Handler
      • ACF Handler
    • Validated Settings
  • WooCommerce Module
    • Motivation and How to use
    • Extended WC Logger
    • WC Settings Handler
Powered by GitBook
On this page

Was this helpful?

  1. Utilities Module
  2. Hooks Service

Scoped Handler

PreviousHooks ServiceNextShortcodes Service

Last updated 4 years ago

Was this helpful?

In addition to the default hooks handler explained on the previous page, the Utilities Module comes pre-packaged with one more hooks handler -- the so-called .

The scoped handler inherits the DefaultHooksHandler so it still keeps a list of hooks arrays and registers them on run. However, the hooks registered have a so-called scope which is defined by a starting hook and an ending hook.

Basically this handler will automagically register all its hooks an a constructor-given hook and will unregister them on another constructor-given hook. This is useful for scenarios where you only want to enqueue some hooks during a specific method call, for example, and don't want them interfering with the rest of the request. Your custom filter for modifying SQL queries should really not bring the whole page down, but only your own custom output if something should go wrong.

You can use it like this:

<?php

use DeepWebSolutions\Framework\Utilities\Hooks\Handlers\ScopedHooksHandler;

class MyClass {
    public function output_my_content() {
        do_action( 'before-my-awesome-output' );
        
        // output ...
        
        do_action( 'after-my-awesome-output' );
    }
}

$scoped_handler = new ScopedHooksHandler( 
        'my-class-output-handler', 
        array( 'hook' => 'before-my-awesome-output' ), 
        array( 'hook' => 'after-my-awesome-output' )
    );
$scoped_handler ->add_filter( ... );
$scoped_handler ->remove_action( ... );

$my_class = new MyClass();
$my_class->output_my_content();

The scoped handler uses different semantics for the methods remove_filter and remove_action. Basically their purpose is to instruct the handler to temporarily unregister those hooks during the given scope.

If you want to unregister hooks that you've added to the handler using add_action and add_filter, please use the methods remove_added_filter and remove_added_action, respectively.

Of course, it's also possible to call the run and reset action methods manually by simply not setting the start and end hooks in the constructor:

<?php

$scoped_handler = new ScopedHooksHandler( 'my-class-output-handler' );
$scoped_handler ->add_filter( ... );
$scoped_handler ->remove_action( ... );

$my_class = new MyClass();

$scoped_handler->run();
$my_class->output_my_content();
$scoped_handler->reset();
ScopedHooksHandler