| @@ -1,0 +1,107 @@ | ||
| 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 | + // translators: %s: function name. | |
| 45 | + throw new Exception( sprintf( esc_html__( '%1$s should be implemented by the extended class.', 'cmb2' ), __FUNCTION__ ) ); | |
| 46 | + } | |
| 47 | + | |
| 48 | + /** | |
| 49 | + * Constructor | |
| 50 | + * | |
| 51 | + * @since 2.0.0 | |
| 52 | + * @param CMB2 $cmb The CMB2 object to hookup. | |
| 53 | + */ | |
| 54 | + public function __construct( CMB2 $cmb ) { | |
| 55 | + $this->cmb = $cmb; | |
| 56 | + $this->object_type = $this->cmb->mb_object_type(); | |
| 57 | + } | |
| 58 | + | |
| 59 | + abstract public function universal_hooks(); | |
| 60 | + | |
| 61 | + /** | |
| 62 | + * Ensures WordPress hook only gets fired once per object. | |
| 63 | + * | |
| 64 | + * @since 2.0.0 | |
| 65 | + * @param string $action The name of the filter to hook the $hook callback to. | |
| 66 | + * @param callback $hook The callback to be run when the filter is applied. | |
| 67 | + * @param integer $priority Order the functions are executed. | |
| 68 | + * @param int $accepted_args The number of arguments the function accepts. | |
| 69 | + */ | |
| 70 | + public function once( $action, $hook, $priority = 10, $accepted_args = 1 ) { | |
| 71 | + static $hooks_completed = array(); | |
| 72 | + | |
| 73 | + $args = func_get_args(); | |
| 74 | + | |
| 75 | + // Get object hash.. This bypasses issues with serializing closures. | |
| 76 | + if ( is_object( $hook ) ) { | |
| 77 | + $args[1] = spl_object_hash( $args[1] ); | |
| 78 | + } elseif ( is_array( $hook ) && is_object( $hook[0] ) ) { | |
| 79 | + $args[1][0] = spl_object_hash( $hook[0] ); | |
| 80 | + } | |
| 81 | + | |
| 82 | + $key = md5( serialize( $args ) ); | |
| 83 | + | |
| 84 | + if ( ! isset( $hooks_completed[ $key ] ) ) { | |
| 85 | + $hooks_completed[ $key ] = 1; | |
| 86 | + add_filter( $action, $hook, $priority, $accepted_args ); | |
| 87 | + } | |
| 88 | + } | |
| 89 | + | |
| 90 | + /** | |
| 91 | + * Magic getter for our object. | |
| 92 | + * | |
| 93 | + * @param string $field Property to return. | |
| 94 | + * @throws Exception Throws an exception if the field is invalid. | |
| 95 | + * @return mixed | |
| 96 | + */ | |
| 97 | + public function __get( $field ) { | |
| 98 | + switch ( $field ) { | |
| 99 | + case 'object_type': | |
| 100 | + case 'cmb': | |
| 101 | + return $this->{$field}; | |
| 102 | + default: | |
| 103 | + // translators: 1: class name, 2: property name. | |
| 104 | + throw new Exception( sprintf( esc_html__( 'Invalid %1$s property: %2$s', 'cmb2' ), __CLASS__, $field ) ); | |
| 105 | + } | |
| 106 | + } | |
| 107 | +} | |