| 1 |
<?php |
| 2 |
/** |
| 3 |
* Base class for hooking CMB2 into WordPress. |
| 4 |
* |
| 5 |
* @since 2.2.0 |
| 6 |
* |
| 7 |
* @category WordPress_Plugin |
| 8 |
* @package CMB2 |
| 9 |
* @author CMB2 team |
| 10 |
* @license GPL-2.0+ |
| 11 |
* @link https://cmb2.io |
| 12 |
* |
| 13 |
* @property-read string $object_type |
| 14 |
* @property-read CMB2 $cmb |
| 15 |
*/ |
| 16 |
abstract class CMB2_Hookup_Base { |
| 17 |
|
| 18 |
/** |
| 19 |
* CMB2 object. |
| 20 |
* |
| 21 |
* @var CMB2 object |
| 22 |
* @since 2.0.2 |
| 23 |
*/ |
| 24 |
protected $cmb; |
| 25 |
|
| 26 |
/** |
| 27 |
* The object type we are performing the hookup for |
| 28 |
* |
| 29 |
* @var string |
| 30 |
* @since 2.0.9 |
| 31 |
*/ |
| 32 |
protected $object_type = 'post'; |
| 33 |
|
| 34 |
/** |
| 35 |
* A functionalized constructor, used for the hookup action callbacks. |
| 36 |
* |
| 37 |
* @since 2.2.6 |
| 38 |
* |
| 39 |
* @throws Exception Failed implementation. |
| 40 |
* |
| 41 |
* @param CMB2 $cmb The CMB2 object to hookup. |
| 42 |
*/ |
| 43 |
public static function maybe_init_and_hookup( CMB2 $cmb ) { |
| 44 |
throw new Exception( sprintf( esc_html__( '%1$s should be implemented by the extended class.', 'cmb2' ), __FUNCTION__ ) ); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Constructor |
| 49 |
* |
| 50 |
* @since 2.0.0 |
| 51 |
* @param CMB2 $cmb The CMB2 object to hookup. |
| 52 |
*/ |
| 53 |
public function __construct( CMB2 $cmb ) { |
| 54 |
$this->cmb = $cmb; |
| 55 |
$this->object_type = $this->cmb->mb_object_type(); |
| 56 |
} |
| 57 |
|
| 58 |
abstract public function universal_hooks(); |
| 59 |
|
| 60 |
/** |
| 61 |
* Ensures WordPress hook only gets fired once per object. |
| 62 |
* |
| 63 |
* @since 2.0.0 |
| 64 |
* @param string $action The name of the filter to hook the $hook callback to. |
| 65 |
* @param callback $hook The callback to be run when the filter is applied. |
| 66 |
* @param integer $priority Order the functions are executed. |
| 67 |
* @param int $accepted_args The number of arguments the function accepts. |
| 68 |
*/ |
| 69 |
public function once( $action, $hook, $priority = 10, $accepted_args = 1 ) { |
| 70 |
static $hooks_completed = array(); |
| 71 |
|
| 72 |
$args = func_get_args(); |
| 73 |
|
| 74 |
// Get object hash.. This bypasses issues with serializing closures. |
| 75 |
if ( is_object( $hook ) ) { |
| 76 |
$args[1] = spl_object_hash( $args[1] ); |
| 77 |
} elseif ( is_array( $hook ) && is_object( $hook[0] ) ) { |
| 78 |
$args[1][0] = spl_object_hash( $hook[0] ); |
| 79 |
} |
| 80 |
|
| 81 |
$key = md5( serialize( $args ) ); |
| 82 |
|
| 83 |
if ( ! isset( $hooks_completed[ $key ] ) ) { |
| 84 |
$hooks_completed[ $key ] = 1; |
| 85 |
add_filter( $action, $hook, $priority, $accepted_args ); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Magic getter for our object. |
| 91 |
* |
| 92 |
* @param string $field Property to return. |
| 93 |
* @throws Exception Throws an exception if the field is invalid. |
| 94 |
* @return mixed |
| 95 |
*/ |
| 96 |
public function __get( $field ) { |
| 97 |
switch ( $field ) { |
| 98 |
case 'object_type': |
| 99 |
case 'cmb': |
| 100 |
return $this->{$field}; |
| 101 |
default: |
| 102 |
throw new Exception( sprintf( esc_html__( 'Invalid %1$s property: %2$s', 'cmb2' ), __CLASS__, $field ) ); |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
|