Advertisement
wclovers

Untitled

Mar 17th, 2022
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.78 KB | None | 0 0
  1. if( ! function_exists( 'remove_class_filter' ) ){
  2.  
  3.     /**
  4.      * Remove Class Filter Without Access to Class Object
  5.      *
  6.      * In order to use the core WordPress remove_filter() on a filter added with the callback
  7.      * to a class, you either have to have access to that class object, or it has to be a call
  8.      * to a static method.  This method allows you to remove filters with a callback to a class
  9.      * you don't have access to.
  10.      *
  11.      * Works with WordPress 1.2+ (4.7+ support added 9-19-2016)
  12.      * Updated 2-27-2017 to use internal WordPress removal for 4.7+ (to prevent PHP warnings output)
  13.      *
  14.      * @param string $tag         Filter to remove
  15.      * @param string $class_name  Class name for the filter's callback
  16.      * @param string $method_name Method name for the filter's callback
  17.      * @param int    $priority    Priority of the filter (default 10)
  18.      *
  19.      * @return bool Whether the function is removed.
  20.      */
  21.     function remove_class_filter( $tag, $class_name = '', $method_name = '', $priority = 10 ) {
  22.  
  23.         global $wp_filter;
  24.  
  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.  
  46.         // Exit if there aren't any callbacks for specified priority
  47.         if ( ! isset( $callbacks[ $priority ] ) || empty( $callbacks[ $priority ] ) ) {
  48.             return FALSE;
  49.         }
  50.  
  51.         // Loop through each filter for the specified priority, looking for our class & method
  52.         foreach ( (array) $callbacks[ $priority ] as $filter_id => $filter ) {
  53.  
  54.             // Filter should always be an array - array( $this, 'method' ), if not goto next
  55.             if ( ! isset( $filter['function'] ) || ! is_array( $filter['function'] ) ) {
  56.                 continue;
  57.             }
  58.  
  59.             // If first value in array is not an object, it can't be a class
  60.             if ( ! is_object( $filter['function'][0] ) ) {
  61.                 continue;
  62.             }
  63.  
  64.             // Method doesn't match the one we're looking for, goto next
  65.             if ( $filter['function'][1] !== $method_name ) {
  66.                 continue;
  67.             }
  68.  
  69.             // Method matched, now let's check the Class
  70.             if ( get_class( $filter['function'][0] ) === $class_name ) {
  71.  
  72.                 // WordPress 4.7+ use core remove_filter() since we found the class object
  73.                 if ( isset( $fob ) ) {
  74.                     // Handles removing filter, reseting callback priority keys mid-iteration, etc.
  75.                     $fob->remove_filter( $tag, $filter['function'], $priority );
  76.  
  77.                 } else {
  78.                     // Use legacy removal process (pre 4.7)
  79.                     unset( $callbacks[ $priority ][ $filter_id ] );
  80.                     // and if it was the only filter in that priority, unset that priority
  81.                     if ( empty( $callbacks[ $priority ] ) ) {
  82.                         unset( $callbacks[ $priority ] );
  83.                     }
  84.                     // and if the only filter for that tag, set the tag to an empty array
  85.                     if ( empty( $callbacks ) ) {
  86.                         $callbacks = array();
  87.                     }
  88.                     // Remove this filter from merged_filters, which specifies if filters have been sorted
  89.                     unset( $GLOBALS['merged_filters'][ $tag ] );
  90.                 }
  91.  
  92.                 return TRUE;
  93.             }
  94.         }
  95.  
  96.         return FALSE;
  97.     }
  98. }
  99.  
  100. add_filter( 'wp_insert_post_data' , function() {
  101.     remove_class_filter( 'wp_insert_post_data', 'mangopayWCAdmin', 'post_save_preauth_process', '99' );
  102. }, 98 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement