      1: <?php
      2:
      3: class RESTian_Base {
      4:
      5:   /**
      6:    * Adds a filter hook for an object
      7:    *
      8:    *    $this->add_filter( 'filter_result' );
      9:    *    $this->add_filter( 'filter_result', 11 );
     10:    *    $this->add_filter( 'filter_result', 'special_func' );
     11:    *    $this->add_filter( 'filter_result', 'special_func', 11 );
     12:    *    $this->add_filter( 'filter_result', array( __CLASS__, 'class_func' ) );
     13:    *    $this->add_filter( 'filter_result', array( __CLASS__, 'class_func' ), 11 );
     14:    *    $this->add_filter( 'filter_result', new SpecialClass() );
     15:    *    $this->add_filter( 'filter_result', new SpecialClass(), 11 );
     16:    *
     17:    * @param string $filter_name
     18:    * @param string|array|object|int $callable_object_or_priority Array Callable, String Callable, Class w/Named Filter or Priority
     19:    * @param int|null $priority
     20:    *
     21:    * @return mixed
     22:    */
     23:   function add_filter( $filter_name, $callable_object_or_priority = null, $priority = null ) {
     24:     list( $callable, $priority ) = $this->_parse_callable_args( $filter_name, $callable_object_or_priority, $priority );
     25:     if ( defined( 'WP_CONTENT_DIR') ) {
     26:       $filter_name = spl_object_hash( $this ) . "->{$filter_name}()";
     27:       add_filter( $filter_name, $callable, $priority, 99 );
     28:     } else {
     29:       RESTian::add_filter( $filter_name, $callable, $priority );
     30:     }
     31:     return true;
     32:   }
     33:   /**
     34:    * Adds a filter hook for an object
     35:    *
     36:    *    $this->add_action( 'process_action' );
     37:    *    $this->add_action( 'process_action', 11 );
     38:    *    $this->add_action( 'process_action', 'special_func' );
     39:    *    $this->add_action( 'process_action', 'special_func', 11 );
     40:    *    $this->add_action( 'process_action', array( __CLASS__, 'class_func' ) );
     41:    *    $this->add_action( 'process_action', array( __CLASS__, 'class_func' ), 11 );
     42:    *    $this->add_action( 'process_action', new SpecialClass() );
     43:    *    $this->add_action( 'process_action', new SpecialClass(), 11 );
     44:    *
     45:    * @param string $action_name
     46:    * @param string|array|object|int $callable_object_or_priority Array Callable, String Callable, Class w/Named Action or Priority
     47:    * @param int|null $priority
     48:    *
     49:    * @return mixed
     50:    */
     51:   function add_action( $action_name, $callable_object_or_priority = null, $priority = null ) {
     52:     list( $callable, $priority ) = $this->_parse_callable_args( $action_name, $callable_object_or_priority, $priority );
     53:     if ( defined( 'WP_CONTENT_DIR') ) {
     54:       add_action( $this->_hash_hook_name( $action_name ), $callable, $priority, 99 );
     55:     } else {
     56:       RESTian::add_action( $action_name, $callable, $priority );
     57:     }
     58:   }
     59:
     60:   /**
     61:    * @param string $filter_name
     62:    * @param mixed $value
     63:    *
     64:    * @return mixed
     65:    */
     66:   function apply_filters( $filter_name, $value ) {
     67:     if ( defined( 'WP_CONTENT_DIR') ) {
     68:       $args = func_get_args();
     69:       $args[0] = $this->_hash_hook_name( $filter_name );
     70:       $value = call_user_func_array( 'apply_filters', $args );
     71:     } else {
     72:       $filters = RESTian::get_filters( $filter_name );
     73:       if ( count( $filters ) ) {
     74:         $args = func_get_args();
     75:         array_shift( $args );
     76:         foreach( $filters as $function ) {
     77:           /**
     78:            * Sort by priorty
     79:            */
     80:           ksort( $function );
     81:           foreach( $function as $priority ) {
     82:             foreach( $priority as $callable ) {
     83:               $value = call_user_func_array( $callable, $args );
     84:             }
     85:           }
     86:         }
     87:       }
     88:     }
     89:     return $value;
     90:   }
     91:
     92:   /**
     93:    * @param string $action_name
     94:    *
     95:    * @return mixed
     96:    */
     97:   function do_action( $action_name ) {
     98:     if ( defined( 'WP_CONTENT_DIR') ) {
     99:       $args = func_get_args();
    100:       $args[0] = $this->_hash_hook_name( $action_name );
    101:       call_user_func_array( 'do_action', $args );
    102:     } else {
    103:       call_user_func_array( array( $this, 'apply_filters' ), func_get_args() );
    104:     }
    105:   }
    106:
    107:   /**
    108:    * Parse args for $this->add_action() and $this->add_filter().
    109:    *
    110:    * @param string $filter_name
    111:    * @param string|array|object|int $callable_object_or_priority Array Callable, String Callable, Class w/Named Action or Priority
    112:    * @param int|null $priority
    113:    *
    114:    * @return array
    115:    */
    116:   private function _parse_callable_args( $filter_name, $callable_object_or_priority = null, $priority = null ) {
    117:     if ( ! $callable_object_or_priority ) {
    118:       $callable = array( $this, "_{$filter_name}" );
    119:     } else if ( is_numeric( $callable_object_or_priority ) ) {
    120:       $priority = $callable_object_or_priority;
    121:     } else if ( is_string( $callable_object_or_priority ) ) {
    122:       $callable = array( $this, $callable_object_or_priority );
    123:     } else if ( is_array( $callable_object_or_priority ) ) {
    124:       $callable = $callable_object_or_priority;
    125:     } else if ( is_object( $callable_object_or_priority ) ) {
    126:       $callable = array( $callable_object_or_priority, "_{$filter_name}" );
    127:     }
    128:     if ( is_null( $priority ) ) {
    129:        $priority = 10;
    130:     }
    131:     return array( $callable, $priority );
    132:   }
    133:
    134:   private function _hash_hook_name( $hook_name ) {
    135:     return spl_object_hash( $this ) . "->{$hook_name}()";
    136:   }
    137:
    138: }
    139: