Giter Club home page Giter Club logo

php-hooks's Introduction

Build Status Coverage Status Scrutinizer Code Quality Codacy Badge SensioLabsInsight Latest Stable Version Total Downloads Latest Unstable Version PHP 7 ready License

PHP-Hooks

The PHP Hooks Class is a fork of the WordPress filters hook system rolled in to a class to be ported into any php based system

  • This class is heavily based on the WordPress plugin API and most (if not all) of the code comes from there.

How to install?

composer require voku/php-hooks

How to use?

We start with a simple example ...

<?php

$hooks = Hooks::getInstance();

$hooks->add_action('header_action','echo_this_in_header');

function echo_this_in_header(){
   echo 'this came from a hooked function';
}

then all that is left for you is to call the hooked function when you want anywhere in your application, EX:

<?php

$hooks = Hooks::getInstance();

echo '<div id="extra_header">';
$hooks->do_action('header_action');
echo '</div>';

and you output will be: <div id="extra_header">this came from a hooked function</div>

PS: you can also use method from a class for a hook e.g.: $hooks->add_action('header_action', array($this, 'echo_this_in_header_via_method');

Methods

ACTIONS:

add_action Hooks a function on to a specific action.

 - @access public
 - @since 0.1
 - @param string $tag The name of the action to which the $function_to_add is hooked.
 - @param callback $function_to_add The name of the function you wish to be called.
 - @param int $priority optional. Used to specify the order in which the functions associated with a particular action are executed (default: 10). Lower numbers correspond with earlier execution, and functions with the same priority are executed in the order in which they were added to the action.
 - @param int $accepted_args optional. The number of arguments the function accept (default 1).

do_action Execute functions hooked on a specific action hook.

 - @access public
 - @since 0.1
 - @param string $tag The name of the action to be executed.
 - @param mixed $arg,... Optional additional arguments which are passed on to the functions hooked to the action.
 - @return null Will return null if $tag does not exist

remove_action Removes a function from a specified action hook.

 - @access public
 - @since 0.1
 - @param string $tag The action hook to which the function to be removed is hooked.
 - @param callback $function_to_remove The name of the function which should be removed.
 - @param int $priority optional The priority of the function (default: 10).
 - @return boolean Whether the function is removed.

has_action Check if any action has been registered for a hook.

 -  @access public
 -  @since 0.1
 -  @param string $tag The name of the action hook.
 -  @param callback $function_to_check optional.
 -  @return mixed If $function_to_check is omitted, returns boolean for whether the hook has anything registered.
  When checking a specific function, the priority of that hook is returned, or false if the function is not attached.
  When using the $function_to_check argument, this function may return a non-boolean value that evaluates to false (e.g.) 0, so use the === operator for testing the return value.

did_action Retrieve the number of times an action is fired.

 - @access public
 - @since 0.1
 - @param string $tag The name of the action hook.
 - @return int The number of times action hook <tt>$tag</tt> is fired

FILTERS:

add_filter Hooks a function or method to a specific filter action.

 - @access public
 - @since 0.1
 - @param string $tag The name of the filter to hook the $function_to_add to.
 - @param callback $function_to_add The name of the function to be called when the filter is applied.
 - @param int $priority optional. Used to specify the order in which the functions associated with a particular action are executed (default: 10). Lower numbers correspond with earlier execution, and functions with the same priority are executed in the order in which they were added to the action.
 - @param int $accepted_args optional. The number of arguments the function accept (default 1).
 - @return boolean true

remove_filter Removes a function from a specified filter hook.

 - @access public
 - @since 0.1
 - @param string $tag The filter hook to which the function to be removed is hooked.
 - @param callback $function_to_remove The name of the function which should be removed.
 - @param int $priority optional. The priority of the function (default: 10).
 - @param int $accepted_args optional. The number of arguments the function accepts (default: 1).
 - @return boolean Whether the function existed before it was removed.

has_filter Check if any filter has been registered for a hook.

 - @access public
 - @since 0.1
 - @param string $tag The name of the filter hook.
 - @param callback $function_to_check optional.
 - @return mixed If $function_to_check is omitted, returns boolean for whether the hook has anything registered.
   When checking a specific function, the priority of that hook is  returned, or false if the function is not attached.
   When using the $function_to_check argument, this function may return a non-boolean value that evaluates to false (e.g.) 0, so use the === operator for testing the return value.

apply_filters Call the functions added to a filter hook.

 - @access public
 - @since 0.1
 - @param string $tag The name of the filter hook.
 - @param mixed $value The value on which the filters hooked to <tt>$tag</tt> are applied on.
 - @param mixed $var,... Additional variables passed to the functions hooked to <tt>$tag</tt>.
 - @return mixed The filtered value after all hooked functions are applied to it.

License

Since this class is derived from the WordPress Plugin API so are the license and they are GPL http://www.gnu.org/licenses/gpl.html

php-hooks's People

Contributors

amereservant avatar bainternet avatar voku 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  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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

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

php-hooks's Issues

Support for PHP 7.4

Hello,

Thanks a lot for this library ๐Ÿ‘

Are you planning to make sure that the library compatible with PHP 7.4?

Thanks

[Insight] Logical operators should be avoided

in src/voku/helper/Hooks.php, line 1070

The and operator does not have the same precedence as &&.
This could lead to unexpected behavior, use && instead.

            $atts[strtolower($m[1])] = stripcslashes($m[2]);
          } elseif (!empty($m[3])) {
            $atts[strtolower($m[3])] = stripcslashes($m[4]);
          } elseif (!empty($m[5])) {
            $atts[strtolower($m[5])] = stripcslashes($m[6]);
          } elseif (isset($m[7]) and strlen($m[7])) {
            $atts[] = stripcslashes($m[7]);
          } elseif (isset($m[8])) {
            $atts[] = stripcslashes($m[8]);
          }
        }

Posted from SensioLabsInsight

[Insight] Commented code should not be commited

in src/voku/helper/Hooks.php, line 280

Commented out code reduces readability and lowers the code confidence for other developers. If it's common usage for debug, it should not be committed. Using a version control system, such code can be safely removed.

      }

      foreach ((array)array_keys($this->filters[$tag]) as $priority) {

        // DEBUG
        //echo 'lall: ' . $tag . ' | ' . $priority . ' | ' . $idx . "\n<br>";

        if (isset($this->filters[$tag][$priority][$idx])) {
          return $priority;
        }
      }

Posted from SensioLabsInsight

Thanks

Hello,

I just want to thank you for this great library ๐Ÿ‘

Thanks

Autoload failing

Latest commit to change autoload to psr-4 is causing my project to fail initialising. Reason is because, the files are in src/voku/helper and psr-4 means that the given namespace can be found in given dir. So Hooks.php should now be moved to src dir.
Please test this for yourself and fix - my project stopped working after composer update and we depend on you! THANKS!!

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.