nitropack
Last commit date
classes
1 year ago
languages
2 years ago
nitropack-sdk
1 year ago
view
1 year ago
advanced-cache.php
2 years ago
batcache-compat.php
4 years ago
constants.php
1 year ago
diagnostics.php
2 years ago
functions.php
1 year ago
helpers.php
3 years ago
main.php
1 year ago
readme.txt
1 year ago
uninstall.php
2 years ago
wp-cli.php
2 years ago
helpers.php
111 lines
| 1 | <?php |
| 2 | defined( 'ABSPATH' ) or die( 'No script kiddies please!' ); |
| 3 | |
| 4 | if( ! function_exists( 'remove_class_filter' ) ){ |
| 5 | /** |
| 6 | * Remove Class Filter Without Access to Class Object |
| 7 | * |
| 8 | * In order to use the core WordPress remove_filter() on a filter added with the callback |
| 9 | * to a class, you either have to have access to that class object, or it has to be a call |
| 10 | * to a static method. This method allows you to remove filters with a callback to a class |
| 11 | * you don't have access to. |
| 12 | * |
| 13 | * Works with WordPress 1.2+ (4.7+ support added 9-19-2016) |
| 14 | * Updated 2-27-2017 to use internal WordPress removal for 4.7+ (to prevent PHP warnings output) |
| 15 | * |
| 16 | * @param string $tag Filter to remove |
| 17 | * @param string $class_name Class name for the filter's callback |
| 18 | * @param string $method_name Method name for the filter's callback |
| 19 | * @param int $priority Priority of the filter (default 10) |
| 20 | * |
| 21 | * @return bool Whether the function is removed. |
| 22 | */ |
| 23 | function remove_class_filter( $tag, $class_name = '', $method_name = '', $priority = 10 ) { |
| 24 | global $wp_filter; |
| 25 | // Check that filter actually exists first |
| 26 | if ( ! isset( $wp_filter[ $tag ] ) ) { |
| 27 | return FALSE; |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * If filter config is an object, means we're using WordPress 4.7+ and the config is no longer |
| 32 | * a simple array, rather it is an object that implements the ArrayAccess interface. |
| 33 | * |
| 34 | * To be backwards compatible, we set $callbacks equal to the correct array as a reference (so $wp_filter is updated) |
| 35 | * |
| 36 | * @see https://make.wordpress.org/core/2016/09/08/wp_hook-next-generation-actions-and-filters/ |
| 37 | */ |
| 38 | if ( is_object( $wp_filter[ $tag ] ) && isset( $wp_filter[ $tag ]->callbacks ) ) { |
| 39 | // Create $fob object from filter tag, to use below |
| 40 | $fob = $wp_filter[ $tag ]; |
| 41 | $callbacks = &$wp_filter[ $tag ]->callbacks; |
| 42 | } else { |
| 43 | $callbacks = &$wp_filter[ $tag ]; |
| 44 | } |
| 45 | // Exit if there aren't any callbacks for specified priority |
| 46 | if ( ! isset( $callbacks[ $priority ] ) || empty( $callbacks[ $priority ] ) ) { |
| 47 | return FALSE; |
| 48 | } |
| 49 | // Loop through each filter for the specified priority, looking for our class & method |
| 50 | foreach ( (array) $callbacks[ $priority ] as $filter_id => $filter ) { |
| 51 | // Filter should always be an array - array( $this, 'method' ), if not goto next |
| 52 | if ( ! isset( $filter['function'] ) || ! is_array( $filter['function'] ) ) { |
| 53 | continue; |
| 54 | } |
| 55 | // If first value in array is not an object, it can't be a class |
| 56 | if ( ! is_object( $filter['function'][0] ) ) { |
| 57 | continue; |
| 58 | } |
| 59 | // Method doesn't match the one we're looking for, goto next |
| 60 | if ( $filter['function'][1] !== $method_name ) { |
| 61 | continue; |
| 62 | } |
| 63 | // Method matched, now let's check the Class |
| 64 | if ( get_class( $filter['function'][0] ) === $class_name ) { |
| 65 | // WordPress 4.7+ use core remove_filter() since we found the class object |
| 66 | if ( isset( $fob ) ) { |
| 67 | // Handles removing filter, reseting callback priority keys mid-iteration, etc. |
| 68 | $fob->remove_filter( $tag, $filter['function'], $priority ); |
| 69 | } else { |
| 70 | // Use legacy removal process (pre 4.7) |
| 71 | unset( $callbacks[ $priority ][ $filter_id ] ); |
| 72 | // and if it was the only filter in that priority, unset that priority |
| 73 | if ( empty( $callbacks[ $priority ] ) ) { |
| 74 | unset( $callbacks[ $priority ] ); |
| 75 | } |
| 76 | // and if the only filter for that tag, set the tag to an empty array |
| 77 | if ( empty( $callbacks ) ) { |
| 78 | $callbacks = array(); |
| 79 | } |
| 80 | // Remove this filter from merged_filters, which specifies if filters have been sorted |
| 81 | unset( $GLOBALS['merged_filters'][ $tag ] ); |
| 82 | } |
| 83 | return TRUE; |
| 84 | } |
| 85 | } |
| 86 | return FALSE; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | if( ! function_exists( 'remove_class_action') ){ |
| 91 | /** |
| 92 | * Remove Class Action Without Access to Class Object |
| 93 | * |
| 94 | * In order to use the core WordPress remove_action() on an action added with the callback |
| 95 | * to a class, you either have to have access to that class object, or it has to be a call |
| 96 | * to a static method. This method allows you to remove actions with a callback to a class |
| 97 | * you don't have access to. |
| 98 | * |
| 99 | * Works with WordPress 1.2+ (4.7+ support added 9-19-2016) |
| 100 | * |
| 101 | * @param string $tag Action to remove |
| 102 | * @param string $class_name Class name for the action's callback |
| 103 | * @param string $method_name Method name for the action's callback |
| 104 | * @param int $priority Priority of the action (default 10) |
| 105 | * |
| 106 | * @return bool Whether the function is removed. |
| 107 | */ |
| 108 | function remove_class_action( $tag, $class_name = '', $method_name = '', $priority = 10 ) { |
| 109 | remove_class_filter( $tag, $class_name, $method_name, $priority ); |
| 110 | } |
| 111 | } |