| 1 |
<?php |
| 2 |
/** |
| 3 |
* Responsible for holding instances of Charitable helper objects. |
| 4 |
* |
| 5 |
* @package Charitable/Classes/Charitable_Registry |
| 6 |
* @author David Bisset |
| 7 |
* @copyright Copyright (c) 2023, WP Charitable LLC |
| 8 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 9 |
* @since 1.5.0 |
| 10 |
* @version 1.6.49 |
| 11 |
*/ |
| 12 |
|
| 13 |
// Exit if accessed directly. |
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
exit; |
| 16 |
} |
| 17 |
|
| 18 |
if ( ! class_exists( 'Charitable_Registry' ) ) : |
| 19 |
|
| 20 |
/** |
| 21 |
* Charitable_Registry |
| 22 |
* |
| 23 |
* @since 1.5.0 |
| 24 |
*/ |
| 25 |
class Charitable_Registry { |
| 26 |
|
| 27 |
/** |
| 28 |
* Registered objects. |
| 29 |
* |
| 30 |
* @since 1.5.0 |
| 31 |
* |
| 32 |
* @var array |
| 33 |
*/ |
| 34 |
private $objects; |
| 35 |
|
| 36 |
/** |
| 37 |
* Create class object. |
| 38 |
* |
| 39 |
* @since 1.5.0 |
| 40 |
*/ |
| 41 |
public function __construct() { |
| 42 |
$this->objects = array(); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Return a registered object. |
| 47 |
* |
| 48 |
* @since 1.5.0 |
| 49 |
* |
| 50 |
* @param string $class The name of the class. |
| 51 |
* @return object|false Returns an object if the class exists, otherwise returns false. |
| 52 |
*/ |
| 53 |
public function get( $class ) { |
| 54 |
$class_key = $this->get_class_key( $class ); |
| 55 |
|
| 56 |
if ( ! array_key_exists( $class_key, $this->objects ) ) { |
| 57 |
$this->objects[ $class_key ] = $this->get_class_instance( $this->get_class_name( $class ) ); |
| 58 |
} |
| 59 |
|
| 60 |
return $this->objects[ $class_key ]; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Checks whether an object has already been registered. |
| 65 |
* |
| 66 |
* @since 1.5.0 |
| 67 |
* |
| 68 |
* @param string $class The name of the class. |
| 69 |
* @return boolean |
| 70 |
*/ |
| 71 |
public function has( $class ) { |
| 72 |
return array_key_exists( $this->get_class_key( $class ), $this->objects ); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Register an object that has already been instantiated. |
| 77 |
* |
| 78 |
* @since 1.5.0 |
| 79 |
* |
| 80 |
* @param object $object The object to be registered. |
| 81 |
* @return void |
| 82 |
*/ |
| 83 |
public function register_object( $object ) { |
| 84 |
$class = get_class( $object ); |
| 85 |
$class_key = $this->get_class_key( $class ); |
| 86 |
|
| 87 |
$this->objects[ $class_key ] = $object; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Given a class name, returns the key for that class. |
| 92 |
* |
| 93 |
* @since 1.5.0 |
| 94 |
* |
| 95 |
* @param string $class The class name. |
| 96 |
* @return string |
| 97 |
*/ |
| 98 |
protected function get_class_key( $class ) { |
| 99 |
if ( false === strpos( $class, 'Charitable_' ) ) { |
| 100 |
return $class; |
| 101 |
} |
| 102 |
|
| 103 |
return strtolower( str_replace( 'Charitable_', '', $class ) ); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Return a sanitized class name. |
| 108 |
* |
| 109 |
* @since 1.5.0 |
| 110 |
* |
| 111 |
* @param string $class_key The class to return. |
| 112 |
* @return string |
| 113 |
*/ |
| 114 |
protected function get_class_name( $class_key ) { |
| 115 |
if ( 0 === strpos( $class_key, 'Charitable_' ) || 0 === strpos( $class_key, '\\' ) ) { |
| 116 |
return $class_key; |
| 117 |
} |
| 118 |
|
| 119 |
$class_words = str_replace( '_', ' ', $class_key ); |
| 120 |
$class_words = ucwords( $class_words ); |
| 121 |
return 'Charitable_' . str_replace( ' ', '_', $class_words ); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Return the class instance. |
| 126 |
* |
| 127 |
* @since 1.5.0 |
| 128 |
* |
| 129 |
* @param string $class The class name. |
| 130 |
* @return object |
| 131 |
*/ |
| 132 |
protected function get_class_instance( $class ) { |
| 133 |
if ( ! class_exists( $class ) ) { |
| 134 |
/* translators: %s: class name */ |
| 135 |
wp_die( esc_html( sprintf( _x( 'Class %s does not exist.', 'error message when non-existent class is called', 'charitable' ), $class ) ) ); |
| 136 |
} |
| 137 |
|
| 138 |
return new $class; |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
endif; |
| 143 |
|