@@ -15,37 +15,37 @@ |
||
| 15 | 15 | interface FileSubmissionInterface |
| 16 | 16 | { |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * @return string |
|
| 20 | - */ |
|
| 21 | - public function getName(); |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * @return string |
|
| 25 | - */ |
|
| 26 | - public function getType(); |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * @return int |
|
| 30 | - */ |
|
| 31 | - public function getSize(); |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * @return string |
|
| 35 | - */ |
|
| 36 | - public function getTmpFile(); |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * Should just return the filename. |
|
| 40 | - * @since $VID:$ |
|
| 41 | - * @return string |
|
| 42 | - */ |
|
| 43 | - public function __toString(); |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * @return string |
|
| 47 | - */ |
|
| 48 | - public function getErrorCode(); |
|
| 18 | + /** |
|
| 19 | + * @return string |
|
| 20 | + */ |
|
| 21 | + public function getName(); |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * @return string |
|
| 25 | + */ |
|
| 26 | + public function getType(); |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * @return int |
|
| 30 | + */ |
|
| 31 | + public function getSize(); |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * @return string |
|
| 35 | + */ |
|
| 36 | + public function getTmpFile(); |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * Should just return the filename. |
|
| 40 | + * @since $VID:$ |
|
| 41 | + * @return string |
|
| 42 | + */ |
|
| 43 | + public function __toString(); |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * @return string |
|
| 47 | + */ |
|
| 48 | + public function getErrorCode(); |
|
| 49 | 49 | } |
| 50 | 50 | // End of file FileSubmissionInterface.php |
| 51 | 51 | // Location: EventEspresso\core\services\request\files/FileSubmissionInterface.php |
@@ -19,86 +19,86 @@ |
||
| 19 | 19 | */ |
| 20 | 20 | class EE_File_Input extends EE_Form_Input_Base |
| 21 | 21 | { |
| 22 | - /** |
|
| 23 | - * @var array |
|
| 24 | - */ |
|
| 25 | - protected $allowed_file_extensions; |
|
| 22 | + /** |
|
| 23 | + * @var array |
|
| 24 | + */ |
|
| 25 | + protected $allowed_file_extensions; |
|
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * @var array |
|
| 29 | - */ |
|
| 30 | - protected $allowed_mime_types; |
|
| 27 | + /** |
|
| 28 | + * @var array |
|
| 29 | + */ |
|
| 30 | + protected $allowed_mime_types; |
|
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * @param array $options |
|
| 34 | - * @throws InvalidArgumentException |
|
| 35 | - */ |
|
| 36 | - public function __construct($options = array()) |
|
| 37 | - { |
|
| 38 | - if (isset($options['allowed_file_extensions'])) { |
|
| 39 | - $this->allowed_file_extensions = (array) $options['allowed_file_extensions']; |
|
| 40 | - } else { |
|
| 41 | - $this->allowed_file_extensions = ['csv']; |
|
| 42 | - } |
|
| 43 | - if (isset($options['allowed_mime_types'])) { |
|
| 44 | - $this->allowed_mime_types = (array) $options['allowed_file_extensions']; |
|
| 45 | - } else { |
|
| 46 | - $this->allowed_mime_types = ['text/csv']; |
|
| 47 | - } |
|
| 32 | + /** |
|
| 33 | + * @param array $options |
|
| 34 | + * @throws InvalidArgumentException |
|
| 35 | + */ |
|
| 36 | + public function __construct($options = array()) |
|
| 37 | + { |
|
| 38 | + if (isset($options['allowed_file_extensions'])) { |
|
| 39 | + $this->allowed_file_extensions = (array) $options['allowed_file_extensions']; |
|
| 40 | + } else { |
|
| 41 | + $this->allowed_file_extensions = ['csv']; |
|
| 42 | + } |
|
| 43 | + if (isset($options['allowed_mime_types'])) { |
|
| 44 | + $this->allowed_mime_types = (array) $options['allowed_file_extensions']; |
|
| 45 | + } else { |
|
| 46 | + $this->allowed_mime_types = ['text/csv']; |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | - $this->_set_display_strategy(new EE_File_Input_Display_Strategy()); |
|
| 50 | - $this->_set_normalization_strategy(new EE_File_Normalization()); |
|
| 51 | - $this->add_validation_strategy( |
|
| 52 | - new EE_Text_Validation_Strategy( |
|
| 53 | - sprintf( |
|
| 54 | - // translators: %1$s is a list of allowed file extensions. |
|
| 55 | - esc_html__('Please provide a file of the requested filetype: %1$s', 'event_espresso'), |
|
| 56 | - implode(', ', $this->allowed_file_extensions) |
|
| 57 | - ), |
|
| 58 | - '~.*\.(' . implode('|', $this->allowed_file_extensions) . ')$~' |
|
| 59 | - ) |
|
| 60 | - ); |
|
| 61 | - parent::__construct($options); |
|
| 49 | + $this->_set_display_strategy(new EE_File_Input_Display_Strategy()); |
|
| 50 | + $this->_set_normalization_strategy(new EE_File_Normalization()); |
|
| 51 | + $this->add_validation_strategy( |
|
| 52 | + new EE_Text_Validation_Strategy( |
|
| 53 | + sprintf( |
|
| 54 | + // translators: %1$s is a list of allowed file extensions. |
|
| 55 | + esc_html__('Please provide a file of the requested filetype: %1$s', 'event_espresso'), |
|
| 56 | + implode(', ', $this->allowed_file_extensions) |
|
| 57 | + ), |
|
| 58 | + '~.*\.(' . implode('|', $this->allowed_file_extensions) . ')$~' |
|
| 59 | + ) |
|
| 60 | + ); |
|
| 61 | + parent::__construct($options); |
|
| 62 | 62 | |
| 63 | 63 | // It would be great to add this HTML attribute, but jQuery validate chokes on it. |
| 64 | - $this->set_other_html_attributes( |
|
| 65 | - $this->other_html_attributes() |
|
| 66 | - . ' extension="' |
|
| 67 | - . implode( |
|
| 68 | - ',', |
|
| 69 | - $this->allowed_file_extensions |
|
| 70 | - ) |
|
| 71 | - . '"' |
|
| 72 | - ); |
|
| 73 | - } |
|
| 64 | + $this->set_other_html_attributes( |
|
| 65 | + $this->other_html_attributes() |
|
| 66 | + . ' extension="' |
|
| 67 | + . implode( |
|
| 68 | + ',', |
|
| 69 | + $this->allowed_file_extensions |
|
| 70 | + ) |
|
| 71 | + . '"' |
|
| 72 | + ); |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - /** |
|
| 76 | - * $_FILES has a really weird structure. So we let `FilesDataHandler` take care of finding the file info for |
|
| 77 | - * this input. |
|
| 78 | - * @since $VID:$ |
|
| 79 | - * @param array $req_data |
|
| 80 | - * @return FileSubmissionInterface |
|
| 81 | - * @throws InvalidArgumentException |
|
| 82 | - * @throws InvalidDataTypeException |
|
| 83 | - * @throws InvalidInterfaceException |
|
| 84 | - */ |
|
| 85 | - public function find_form_data_for_this_section($req_data) |
|
| 86 | - { |
|
| 87 | - // ignore $req_data. Files are in the files data handler. |
|
| 88 | - $fileDataHandler = LoaderFactory::getLoader()->getShared( |
|
| 89 | - 'EventEspresso\core\services\request\files\FilesDataHandler' |
|
| 90 | - ); |
|
| 91 | - return $fileDataHandler->getFileObject($this->html_name()); |
|
| 92 | - } |
|
| 75 | + /** |
|
| 76 | + * $_FILES has a really weird structure. So we let `FilesDataHandler` take care of finding the file info for |
|
| 77 | + * this input. |
|
| 78 | + * @since $VID:$ |
|
| 79 | + * @param array $req_data |
|
| 80 | + * @return FileSubmissionInterface |
|
| 81 | + * @throws InvalidArgumentException |
|
| 82 | + * @throws InvalidDataTypeException |
|
| 83 | + * @throws InvalidInterfaceException |
|
| 84 | + */ |
|
| 85 | + public function find_form_data_for_this_section($req_data) |
|
| 86 | + { |
|
| 87 | + // ignore $req_data. Files are in the files data handler. |
|
| 88 | + $fileDataHandler = LoaderFactory::getLoader()->getShared( |
|
| 89 | + 'EventEspresso\core\services\request\files\FilesDataHandler' |
|
| 90 | + ); |
|
| 91 | + return $fileDataHandler->getFileObject($this->html_name()); |
|
| 92 | + } |
|
| 93 | 93 | |
| 94 | - /** |
|
| 95 | - * Don't transform the file submission object into a string, thanks. |
|
| 96 | - * |
|
| 97 | - * @param string $value |
|
| 98 | - * @return null|string |
|
| 99 | - */ |
|
| 100 | - protected function _sanitize($value) |
|
| 101 | - { |
|
| 102 | - return $value; |
|
| 103 | - } |
|
| 94 | + /** |
|
| 95 | + * Don't transform the file submission object into a string, thanks. |
|
| 96 | + * |
|
| 97 | + * @param string $value |
|
| 98 | + * @return null|string |
|
| 99 | + */ |
|
| 100 | + protected function _sanitize($value) |
|
| 101 | + { |
|
| 102 | + return $value; |
|
| 103 | + } |
|
| 104 | 104 | } |
@@ -55,7 +55,7 @@ |
||
| 55 | 55 | esc_html__('Please provide a file of the requested filetype: %1$s', 'event_espresso'), |
| 56 | 56 | implode(', ', $this->allowed_file_extensions) |
| 57 | 57 | ), |
| 58 | - '~.*\.(' . implode('|', $this->allowed_file_extensions) . ')$~' |
|
| 58 | + '~.*\.('.implode('|', $this->allowed_file_extensions).')$~' |
|
| 59 | 59 | ) |
| 60 | 60 | ); |
| 61 | 61 | parent::__construct($options); |
@@ -29,639 +29,639 @@ |
||
| 29 | 29 | abstract class FormHandler implements FormHandlerInterface |
| 30 | 30 | { |
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * will add opening and closing HTML form tags as well as a submit button |
|
| 34 | - */ |
|
| 35 | - const ADD_FORM_TAGS_AND_SUBMIT = 'add_form_tags_and_submit'; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * will add opening and closing HTML form tags but NOT a submit button |
|
| 39 | - */ |
|
| 40 | - const ADD_FORM_TAGS_ONLY = 'add_form_tags_only'; |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * will NOT add opening and closing HTML form tags but will add a submit button |
|
| 44 | - */ |
|
| 45 | - const ADD_FORM_SUBMIT_ONLY = 'add_form_submit_only'; |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * will NOT add opening and closing HTML form tags NOR a submit button |
|
| 49 | - */ |
|
| 50 | - const DO_NOT_SETUP_FORM = 'do_not_setup_form'; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * if set to false, then this form has no displayable content, |
|
| 54 | - * and will only be used for processing data sent passed via GET or POST |
|
| 55 | - * defaults to true ( ie: form has displayable content ) |
|
| 56 | - * |
|
| 57 | - * @var boolean $displayable |
|
| 58 | - */ |
|
| 59 | - private $displayable = true; |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * @var string $form_name |
|
| 63 | - */ |
|
| 64 | - private $form_name; |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * @var string $admin_name |
|
| 68 | - */ |
|
| 69 | - private $admin_name; |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * @var string $slug |
|
| 73 | - */ |
|
| 74 | - private $slug; |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * @var string $submit_btn_text |
|
| 78 | - */ |
|
| 79 | - private $submit_btn_text; |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * @var string $form_action |
|
| 83 | - */ |
|
| 84 | - private $form_action; |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * form params in key value pairs |
|
| 88 | - * can be added to form action URL or as hidden inputs |
|
| 89 | - * |
|
| 90 | - * @var array $form_args |
|
| 91 | - */ |
|
| 92 | - private $form_args = array(); |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * value of one of the string constant above |
|
| 96 | - * |
|
| 97 | - * @var string $form_config |
|
| 98 | - */ |
|
| 99 | - private $form_config; |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * whether or not the form was determined to be invalid |
|
| 103 | - * |
|
| 104 | - * @var boolean $form_has_errors |
|
| 105 | - */ |
|
| 106 | - private $form_has_errors; |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * the absolute top level form section being used on the page |
|
| 110 | - * |
|
| 111 | - * @var EE_Form_Section_Proper $form |
|
| 112 | - */ |
|
| 113 | - private $form; |
|
| 114 | - |
|
| 115 | - /** |
|
| 116 | - * @var EE_Registry $registry |
|
| 117 | - */ |
|
| 118 | - protected $registry; |
|
| 119 | - |
|
| 120 | - // phpcs:disable PEAR.Functions.ValidDefaultValue.NotAtEnd |
|
| 121 | - /** |
|
| 122 | - * Form constructor. |
|
| 123 | - * |
|
| 124 | - * @param string $form_name |
|
| 125 | - * @param string $admin_name |
|
| 126 | - * @param string $slug |
|
| 127 | - * @param string $form_action |
|
| 128 | - * @param string $form_config |
|
| 129 | - * @param EE_Registry $registry |
|
| 130 | - * @throws InvalidDataTypeException |
|
| 131 | - * @throws DomainException |
|
| 132 | - * @throws InvalidArgumentException |
|
| 133 | - */ |
|
| 134 | - public function __construct( |
|
| 135 | - $form_name, |
|
| 136 | - $admin_name, |
|
| 137 | - $slug, |
|
| 138 | - $form_action = '', |
|
| 139 | - $form_config = FormHandler::ADD_FORM_TAGS_AND_SUBMIT, |
|
| 140 | - EE_Registry $registry |
|
| 141 | - ) { |
|
| 142 | - $this->setFormName($form_name); |
|
| 143 | - $this->setAdminName($admin_name); |
|
| 144 | - $this->setSlug($slug); |
|
| 145 | - $this->setFormAction($form_action); |
|
| 146 | - $this->setFormConfig($form_config); |
|
| 147 | - $this->setSubmitBtnText(esc_html__('Submit', 'event_espresso')); |
|
| 148 | - $this->registry = $registry; |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - |
|
| 152 | - /** |
|
| 153 | - * @return array |
|
| 154 | - */ |
|
| 155 | - public static function getFormConfigConstants() |
|
| 156 | - { |
|
| 157 | - return array( |
|
| 158 | - FormHandler::ADD_FORM_TAGS_AND_SUBMIT, |
|
| 159 | - FormHandler::ADD_FORM_TAGS_ONLY, |
|
| 160 | - FormHandler::ADD_FORM_SUBMIT_ONLY, |
|
| 161 | - FormHandler::DO_NOT_SETUP_FORM, |
|
| 162 | - ); |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - |
|
| 166 | - /** |
|
| 167 | - * @param bool $for_display |
|
| 168 | - * @return EE_Form_Section_Proper |
|
| 169 | - * @throws EE_Error |
|
| 170 | - * @throws LogicException |
|
| 171 | - */ |
|
| 172 | - public function form($for_display = false) |
|
| 173 | - { |
|
| 174 | - if (! $this->formIsValid()) { |
|
| 175 | - return null; |
|
| 176 | - } |
|
| 177 | - if ($for_display) { |
|
| 178 | - $form_config = $this->formConfig(); |
|
| 179 | - if ($form_config === FormHandler::ADD_FORM_TAGS_AND_SUBMIT |
|
| 180 | - || $form_config === FormHandler::ADD_FORM_SUBMIT_ONLY |
|
| 181 | - ) { |
|
| 182 | - $this->appendSubmitButton(); |
|
| 183 | - $this->clearFormButtonFloats(); |
|
| 184 | - } |
|
| 185 | - } |
|
| 186 | - return $this->form; |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - |
|
| 190 | - /** |
|
| 191 | - * @return boolean |
|
| 192 | - * @throws LogicException |
|
| 193 | - */ |
|
| 194 | - public function formIsValid() |
|
| 195 | - { |
|
| 196 | - if ($this->form instanceof EE_Form_Section_Proper) { |
|
| 197 | - return true; |
|
| 198 | - } |
|
| 199 | - $form = apply_filters( |
|
| 200 | - 'FHEE__EventEspresso_core_libraries_form_sections_form_handlers_FormHandler__formIsValid__generated_form_object', |
|
| 201 | - $this->generate(), |
|
| 202 | - $this |
|
| 203 | - ); |
|
| 204 | - if ($this->verifyForm($form)) { |
|
| 205 | - $this->setForm($form); |
|
| 206 | - } |
|
| 207 | - return true; |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - |
|
| 211 | - /** |
|
| 212 | - * @param EE_Form_Section_Proper|null $form |
|
| 213 | - * @return bool |
|
| 214 | - * @throws LogicException |
|
| 215 | - */ |
|
| 216 | - public function verifyForm(EE_Form_Section_Proper $form = null) |
|
| 217 | - { |
|
| 218 | - $form = $form !== null ? $form : $this->form; |
|
| 219 | - if ($form instanceof EE_Form_Section_Proper) { |
|
| 220 | - return true; |
|
| 221 | - } |
|
| 222 | - throw new LogicException( |
|
| 223 | - sprintf( |
|
| 224 | - esc_html__('The "%1$s" form is invalid or missing. %2$s', 'event_espresso'), |
|
| 225 | - $this->form_name, |
|
| 226 | - var_export($form, true) |
|
| 227 | - ) |
|
| 228 | - ); |
|
| 229 | - } |
|
| 230 | - |
|
| 231 | - |
|
| 232 | - /** |
|
| 233 | - * @param EE_Form_Section_Proper $form |
|
| 234 | - */ |
|
| 235 | - public function setForm(EE_Form_Section_Proper $form) |
|
| 236 | - { |
|
| 237 | - $this->form = $form; |
|
| 238 | - } |
|
| 239 | - |
|
| 240 | - |
|
| 241 | - /** |
|
| 242 | - * @return boolean |
|
| 243 | - */ |
|
| 244 | - public function displayable() |
|
| 245 | - { |
|
| 246 | - return $this->displayable; |
|
| 247 | - } |
|
| 248 | - |
|
| 249 | - |
|
| 250 | - /** |
|
| 251 | - * @param boolean $displayable |
|
| 252 | - */ |
|
| 253 | - public function setDisplayable($displayable = false) |
|
| 254 | - { |
|
| 255 | - $this->displayable = filter_var($displayable, FILTER_VALIDATE_BOOLEAN); |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - |
|
| 259 | - /** |
|
| 260 | - * a public name for the form that can be displayed on the frontend of a site |
|
| 261 | - * |
|
| 262 | - * @return string |
|
| 263 | - */ |
|
| 264 | - public function formName() |
|
| 265 | - { |
|
| 266 | - return $this->form_name; |
|
| 267 | - } |
|
| 268 | - |
|
| 269 | - |
|
| 270 | - /** |
|
| 271 | - * @param string $form_name |
|
| 272 | - * @throws InvalidDataTypeException |
|
| 273 | - */ |
|
| 274 | - public function setFormName($form_name) |
|
| 275 | - { |
|
| 276 | - if (! is_string($form_name)) { |
|
| 277 | - throw new InvalidDataTypeException('$form_name', $form_name, 'string'); |
|
| 278 | - } |
|
| 279 | - $this->form_name = $form_name; |
|
| 280 | - } |
|
| 281 | - |
|
| 282 | - |
|
| 283 | - /** |
|
| 284 | - * a public name for the form that can be displayed, but only in the admin |
|
| 285 | - * |
|
| 286 | - * @return string |
|
| 287 | - */ |
|
| 288 | - public function adminName() |
|
| 289 | - { |
|
| 290 | - return $this->admin_name; |
|
| 291 | - } |
|
| 292 | - |
|
| 293 | - |
|
| 294 | - /** |
|
| 295 | - * @param string $admin_name |
|
| 296 | - * @throws InvalidDataTypeException |
|
| 297 | - */ |
|
| 298 | - public function setAdminName($admin_name) |
|
| 299 | - { |
|
| 300 | - if (! is_string($admin_name)) { |
|
| 301 | - throw new InvalidDataTypeException('$admin_name', $admin_name, 'string'); |
|
| 302 | - } |
|
| 303 | - $this->admin_name = $admin_name; |
|
| 304 | - } |
|
| 305 | - |
|
| 306 | - |
|
| 307 | - /** |
|
| 308 | - * a URL friendly string that can be used for identifying the form |
|
| 309 | - * |
|
| 310 | - * @return string |
|
| 311 | - */ |
|
| 312 | - public function slug() |
|
| 313 | - { |
|
| 314 | - return $this->slug; |
|
| 315 | - } |
|
| 316 | - |
|
| 317 | - |
|
| 318 | - /** |
|
| 319 | - * @param string $slug |
|
| 320 | - * @throws InvalidDataTypeException |
|
| 321 | - */ |
|
| 322 | - public function setSlug($slug) |
|
| 323 | - { |
|
| 324 | - if (! is_string($slug)) { |
|
| 325 | - throw new InvalidDataTypeException('$slug', $slug, 'string'); |
|
| 326 | - } |
|
| 327 | - $this->slug = $slug; |
|
| 328 | - } |
|
| 329 | - |
|
| 330 | - |
|
| 331 | - /** |
|
| 332 | - * @return string |
|
| 333 | - */ |
|
| 334 | - public function submitBtnText() |
|
| 335 | - { |
|
| 336 | - return $this->submit_btn_text; |
|
| 337 | - } |
|
| 338 | - |
|
| 339 | - |
|
| 340 | - /** |
|
| 341 | - * @param string $submit_btn_text |
|
| 342 | - * @throws InvalidDataTypeException |
|
| 343 | - * @throws InvalidArgumentException |
|
| 344 | - */ |
|
| 345 | - public function setSubmitBtnText($submit_btn_text) |
|
| 346 | - { |
|
| 347 | - if (! is_string($submit_btn_text)) { |
|
| 348 | - throw new InvalidDataTypeException('$submit_btn_text', $submit_btn_text, 'string'); |
|
| 349 | - } |
|
| 350 | - if (empty($submit_btn_text)) { |
|
| 351 | - throw new InvalidArgumentException( |
|
| 352 | - esc_html__('Can not set Submit button text because an empty string was provided.', 'event_espresso') |
|
| 353 | - ); |
|
| 354 | - } |
|
| 355 | - $this->submit_btn_text = $submit_btn_text; |
|
| 356 | - } |
|
| 357 | - |
|
| 358 | - |
|
| 359 | - /** |
|
| 360 | - * @return string |
|
| 361 | - */ |
|
| 362 | - public function formAction() |
|
| 363 | - { |
|
| 364 | - return ! empty($this->form_args) |
|
| 365 | - ? add_query_arg($this->form_args, $this->form_action) |
|
| 366 | - : $this->form_action; |
|
| 367 | - } |
|
| 368 | - |
|
| 369 | - |
|
| 370 | - /** |
|
| 371 | - * @param string $form_action |
|
| 372 | - * @throws InvalidDataTypeException |
|
| 373 | - */ |
|
| 374 | - public function setFormAction($form_action) |
|
| 375 | - { |
|
| 376 | - if (! is_string($form_action)) { |
|
| 377 | - throw new InvalidDataTypeException('$form_action', $form_action, 'string'); |
|
| 378 | - } |
|
| 379 | - $this->form_action = $form_action; |
|
| 380 | - } |
|
| 381 | - |
|
| 382 | - |
|
| 383 | - /** |
|
| 384 | - * @param array $form_args |
|
| 385 | - * @throws InvalidDataTypeException |
|
| 386 | - * @throws InvalidArgumentException |
|
| 387 | - */ |
|
| 388 | - public function addFormActionArgs($form_args = array()) |
|
| 389 | - { |
|
| 390 | - if (is_object($form_args)) { |
|
| 391 | - throw new InvalidDataTypeException( |
|
| 392 | - '$form_args', |
|
| 393 | - $form_args, |
|
| 394 | - 'anything other than an object was expected.' |
|
| 395 | - ); |
|
| 396 | - } |
|
| 397 | - if (empty($form_args)) { |
|
| 398 | - throw new InvalidArgumentException( |
|
| 399 | - esc_html__('The redirect arguments can not be an empty array.', 'event_espresso') |
|
| 400 | - ); |
|
| 401 | - } |
|
| 402 | - $this->form_args = array_merge($this->form_args, $form_args); |
|
| 403 | - } |
|
| 404 | - |
|
| 405 | - |
|
| 406 | - /** |
|
| 407 | - * @return string |
|
| 408 | - */ |
|
| 409 | - public function formConfig() |
|
| 410 | - { |
|
| 411 | - return $this->form_config; |
|
| 412 | - } |
|
| 413 | - |
|
| 414 | - |
|
| 415 | - /** |
|
| 416 | - * @param string $form_config |
|
| 417 | - * @throws DomainException |
|
| 418 | - */ |
|
| 419 | - public function setFormConfig($form_config) |
|
| 420 | - { |
|
| 421 | - if (! in_array( |
|
| 422 | - $form_config, |
|
| 423 | - array( |
|
| 424 | - FormHandler::ADD_FORM_TAGS_AND_SUBMIT, |
|
| 425 | - FormHandler::ADD_FORM_TAGS_ONLY, |
|
| 426 | - FormHandler::ADD_FORM_SUBMIT_ONLY, |
|
| 427 | - FormHandler::DO_NOT_SETUP_FORM, |
|
| 428 | - ), |
|
| 429 | - true |
|
| 430 | - ) |
|
| 431 | - ) { |
|
| 432 | - throw new DomainException( |
|
| 433 | - sprintf( |
|
| 434 | - esc_html__( |
|
| 435 | - '"%1$s" is not a valid value for the form config. Please use one of the class constants on \EventEspresso\core\libraries\form_sections\form_handlers\Form', |
|
| 436 | - 'event_espresso' |
|
| 437 | - ), |
|
| 438 | - $form_config |
|
| 439 | - ) |
|
| 440 | - ); |
|
| 441 | - } |
|
| 442 | - $this->form_config = $form_config; |
|
| 443 | - } |
|
| 444 | - |
|
| 445 | - |
|
| 446 | - /** |
|
| 447 | - * called after the form is instantiated |
|
| 448 | - * and used for performing any logic that needs to occur early |
|
| 449 | - * before any of the other methods are called. |
|
| 450 | - * returns true if everything is ok to proceed, |
|
| 451 | - * and false if no further form logic should be implemented |
|
| 452 | - * |
|
| 453 | - * @return boolean |
|
| 454 | - */ |
|
| 455 | - public function initialize() |
|
| 456 | - { |
|
| 457 | - $this->form_has_errors = EE_Error::has_error(true); |
|
| 458 | - return true; |
|
| 459 | - } |
|
| 460 | - |
|
| 461 | - |
|
| 462 | - /** |
|
| 463 | - * used for setting up css and js |
|
| 464 | - * |
|
| 465 | - * @return void |
|
| 466 | - * @throws LogicException |
|
| 467 | - * @throws EE_Error |
|
| 468 | - */ |
|
| 469 | - public function enqueueStylesAndScripts() |
|
| 470 | - { |
|
| 471 | - $this->form()->enqueue_js(); |
|
| 472 | - } |
|
| 473 | - |
|
| 474 | - |
|
| 475 | - /** |
|
| 476 | - * creates and returns the actual form |
|
| 477 | - * |
|
| 478 | - * @return EE_Form_Section_Proper |
|
| 479 | - */ |
|
| 480 | - abstract public function generate(); |
|
| 481 | - |
|
| 482 | - |
|
| 483 | - /** |
|
| 484 | - * creates and returns an EE_Submit_Input labeled "Submit" |
|
| 485 | - * |
|
| 486 | - * @param string $text |
|
| 487 | - * @return EE_Submit_Input |
|
| 488 | - */ |
|
| 489 | - public function generateSubmitButton($text = '') |
|
| 490 | - { |
|
| 491 | - $text = ! empty($text) ? $text : $this->submitBtnText(); |
|
| 492 | - return new EE_Submit_Input( |
|
| 493 | - array( |
|
| 494 | - 'html_name' => 'ee-form-submit-' . $this->slug(), |
|
| 495 | - 'html_id' => 'ee-form-submit-' . $this->slug(), |
|
| 496 | - 'html_class' => 'ee-form-submit', |
|
| 497 | - 'html_label' => ' ', |
|
| 498 | - 'other_html_attributes' => ' rel="' . $this->slug() . '"', |
|
| 499 | - 'default' => $text, |
|
| 500 | - ) |
|
| 501 | - ); |
|
| 502 | - } |
|
| 503 | - |
|
| 504 | - |
|
| 505 | - /** |
|
| 506 | - * calls generateSubmitButton() and appends it onto the form along with a float clearing div |
|
| 507 | - * |
|
| 508 | - * @param string $text |
|
| 509 | - * @return void |
|
| 510 | - * @throws EE_Error |
|
| 511 | - */ |
|
| 512 | - public function appendSubmitButton($text = '') |
|
| 513 | - { |
|
| 514 | - if ($this->form->subsection_exists($this->slug() . '-submit-btn')) { |
|
| 515 | - return; |
|
| 516 | - } |
|
| 517 | - $this->form->add_subsections( |
|
| 518 | - array($this->slug() . '-submit-btn' => $this->generateSubmitButton($text)), |
|
| 519 | - null, |
|
| 520 | - false |
|
| 521 | - ); |
|
| 522 | - } |
|
| 523 | - |
|
| 524 | - |
|
| 525 | - /** |
|
| 526 | - * creates and returns an EE_Submit_Input labeled "Cancel" |
|
| 527 | - * |
|
| 528 | - * @param string $text |
|
| 529 | - * @return EE_Submit_Input |
|
| 530 | - */ |
|
| 531 | - public function generateCancelButton($text = '') |
|
| 532 | - { |
|
| 533 | - $cancel_button = new EE_Submit_Input( |
|
| 534 | - array( |
|
| 535 | - 'html_name' => 'ee-form-submit-' . $this->slug(), // YES! Same name as submit !!! |
|
| 536 | - 'html_id' => 'ee-cancel-form-' . $this->slug(), |
|
| 537 | - 'html_class' => 'ee-cancel-form', |
|
| 538 | - 'html_label' => ' ', |
|
| 539 | - 'other_html_attributes' => ' rel="' . $this->slug() . '"', |
|
| 540 | - 'default' => ! empty($text) ? $text : esc_html__('Cancel', 'event_espresso'), |
|
| 541 | - ) |
|
| 542 | - ); |
|
| 543 | - $cancel_button->set_button_css_attributes(false); |
|
| 544 | - return $cancel_button; |
|
| 545 | - } |
|
| 546 | - |
|
| 547 | - |
|
| 548 | - /** |
|
| 549 | - * appends a float clearing div onto end of form |
|
| 550 | - * |
|
| 551 | - * @return void |
|
| 552 | - * @throws EE_Error |
|
| 553 | - */ |
|
| 554 | - public function clearFormButtonFloats() |
|
| 555 | - { |
|
| 556 | - $this->form->add_subsections( |
|
| 557 | - array( |
|
| 558 | - 'clear-submit-btn-float' => new EE_Form_Section_HTML( |
|
| 559 | - EEH_HTML::div('', '', 'clear-float') . EEH_HTML::divx() |
|
| 560 | - ), |
|
| 561 | - ), |
|
| 562 | - null, |
|
| 563 | - false |
|
| 564 | - ); |
|
| 565 | - } |
|
| 566 | - |
|
| 567 | - |
|
| 568 | - /** |
|
| 569 | - * takes the generated form and displays it along with ony other non-form HTML that may be required |
|
| 570 | - * returns a string of HTML that can be directly echoed in a template |
|
| 571 | - * |
|
| 572 | - * @return string |
|
| 573 | - * @throws \InvalidArgumentException |
|
| 574 | - * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 575 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 576 | - * @throws LogicException |
|
| 577 | - * @throws EE_Error |
|
| 578 | - */ |
|
| 579 | - public function display() |
|
| 580 | - { |
|
| 581 | - $form_html = apply_filters( |
|
| 582 | - 'FHEE__EventEspresso_core_libraries_form_sections_form_handlers_FormHandler__display__before_form', |
|
| 583 | - '' |
|
| 584 | - ); |
|
| 585 | - $form_config = $this->formConfig(); |
|
| 586 | - if ($form_config === FormHandler::ADD_FORM_TAGS_AND_SUBMIT |
|
| 587 | - || $form_config === FormHandler::ADD_FORM_TAGS_ONLY |
|
| 588 | - ) { |
|
| 589 | - $additional_props = $this->requiresMultipartEnctype() |
|
| 590 | - ? 'enctype="multipart/form-data"' |
|
| 591 | - : ''; |
|
| 592 | - $form_html .= $this->form()->form_open( |
|
| 593 | - $this->formAction(), |
|
| 594 | - 'POST', |
|
| 595 | - $additional_props |
|
| 596 | - ); |
|
| 597 | - } |
|
| 598 | - $form_html .= $this->form(true)->get_html(); |
|
| 599 | - if ($form_config === FormHandler::ADD_FORM_TAGS_AND_SUBMIT |
|
| 600 | - || $form_config === FormHandler::ADD_FORM_TAGS_ONLY |
|
| 601 | - ) { |
|
| 602 | - $form_html .= $this->form()->form_close(); |
|
| 603 | - } |
|
| 604 | - $form_html .= apply_filters( |
|
| 605 | - 'FHEE__EventEspresso_core_libraries_form_sections_form_handlers_FormHandler__display__after_form', |
|
| 606 | - '' |
|
| 607 | - ); |
|
| 608 | - return $form_html; |
|
| 609 | - } |
|
| 610 | - |
|
| 611 | - /** |
|
| 612 | - * Determines if this form needs "enctype='multipart/form-data'" or not. |
|
| 613 | - * @since $VID:$ |
|
| 614 | - * @return bool |
|
| 615 | - * @throws EE_Error |
|
| 616 | - */ |
|
| 617 | - public function requiresMultipartEnctype() |
|
| 618 | - { |
|
| 619 | - foreach ($this->form()->inputs_in_subsections() as $input) { |
|
| 620 | - if ($input instanceof EE_File_Input) { |
|
| 621 | - return true; |
|
| 622 | - } |
|
| 623 | - } |
|
| 624 | - return false; |
|
| 625 | - } |
|
| 626 | - |
|
| 627 | - |
|
| 628 | - /** |
|
| 629 | - * handles processing the form submission |
|
| 630 | - * returns true or false depending on whether the form was processed successfully or not |
|
| 631 | - * |
|
| 632 | - * @param array $submitted_form_data |
|
| 633 | - * @return array |
|
| 634 | - * @throws \InvalidArgumentException |
|
| 635 | - * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 636 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 637 | - * @throws EE_Error |
|
| 638 | - * @throws LogicException |
|
| 639 | - * @throws InvalidFormSubmissionException |
|
| 640 | - */ |
|
| 641 | - public function process($submitted_form_data = array()) |
|
| 642 | - { |
|
| 643 | - if (! $this->form()->was_submitted($submitted_form_data)) { |
|
| 644 | - throw new InvalidFormSubmissionException($this->form_name); |
|
| 645 | - } |
|
| 646 | - $this->form(true)->receive_form_submission($submitted_form_data); |
|
| 647 | - if (! $this->form()->is_valid()) { |
|
| 648 | - throw new InvalidFormSubmissionException( |
|
| 649 | - $this->form_name, |
|
| 650 | - sprintf( |
|
| 651 | - esc_html__( |
|
| 652 | - 'The "%1$s" form is invalid. Please correct the following errors and resubmit: %2$s %3$s', |
|
| 653 | - 'event_espresso' |
|
| 654 | - ), |
|
| 655 | - $this->form_name, |
|
| 656 | - '<br />', |
|
| 657 | - implode('<br />', $this->form()->get_validation_errors_accumulated()) |
|
| 658 | - ) |
|
| 659 | - ); |
|
| 660 | - } |
|
| 661 | - return apply_filters( |
|
| 662 | - 'FHEE__EventEspresso_core_libraries_form_sections_form_handlers_FormHandler__process__valid_data', |
|
| 663 | - $this->form()->valid_data(), |
|
| 664 | - $this |
|
| 665 | - ); |
|
| 666 | - } |
|
| 32 | + /** |
|
| 33 | + * will add opening and closing HTML form tags as well as a submit button |
|
| 34 | + */ |
|
| 35 | + const ADD_FORM_TAGS_AND_SUBMIT = 'add_form_tags_and_submit'; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * will add opening and closing HTML form tags but NOT a submit button |
|
| 39 | + */ |
|
| 40 | + const ADD_FORM_TAGS_ONLY = 'add_form_tags_only'; |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * will NOT add opening and closing HTML form tags but will add a submit button |
|
| 44 | + */ |
|
| 45 | + const ADD_FORM_SUBMIT_ONLY = 'add_form_submit_only'; |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * will NOT add opening and closing HTML form tags NOR a submit button |
|
| 49 | + */ |
|
| 50 | + const DO_NOT_SETUP_FORM = 'do_not_setup_form'; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * if set to false, then this form has no displayable content, |
|
| 54 | + * and will only be used for processing data sent passed via GET or POST |
|
| 55 | + * defaults to true ( ie: form has displayable content ) |
|
| 56 | + * |
|
| 57 | + * @var boolean $displayable |
|
| 58 | + */ |
|
| 59 | + private $displayable = true; |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * @var string $form_name |
|
| 63 | + */ |
|
| 64 | + private $form_name; |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * @var string $admin_name |
|
| 68 | + */ |
|
| 69 | + private $admin_name; |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * @var string $slug |
|
| 73 | + */ |
|
| 74 | + private $slug; |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * @var string $submit_btn_text |
|
| 78 | + */ |
|
| 79 | + private $submit_btn_text; |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * @var string $form_action |
|
| 83 | + */ |
|
| 84 | + private $form_action; |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * form params in key value pairs |
|
| 88 | + * can be added to form action URL or as hidden inputs |
|
| 89 | + * |
|
| 90 | + * @var array $form_args |
|
| 91 | + */ |
|
| 92 | + private $form_args = array(); |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * value of one of the string constant above |
|
| 96 | + * |
|
| 97 | + * @var string $form_config |
|
| 98 | + */ |
|
| 99 | + private $form_config; |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * whether or not the form was determined to be invalid |
|
| 103 | + * |
|
| 104 | + * @var boolean $form_has_errors |
|
| 105 | + */ |
|
| 106 | + private $form_has_errors; |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * the absolute top level form section being used on the page |
|
| 110 | + * |
|
| 111 | + * @var EE_Form_Section_Proper $form |
|
| 112 | + */ |
|
| 113 | + private $form; |
|
| 114 | + |
|
| 115 | + /** |
|
| 116 | + * @var EE_Registry $registry |
|
| 117 | + */ |
|
| 118 | + protected $registry; |
|
| 119 | + |
|
| 120 | + // phpcs:disable PEAR.Functions.ValidDefaultValue.NotAtEnd |
|
| 121 | + /** |
|
| 122 | + * Form constructor. |
|
| 123 | + * |
|
| 124 | + * @param string $form_name |
|
| 125 | + * @param string $admin_name |
|
| 126 | + * @param string $slug |
|
| 127 | + * @param string $form_action |
|
| 128 | + * @param string $form_config |
|
| 129 | + * @param EE_Registry $registry |
|
| 130 | + * @throws InvalidDataTypeException |
|
| 131 | + * @throws DomainException |
|
| 132 | + * @throws InvalidArgumentException |
|
| 133 | + */ |
|
| 134 | + public function __construct( |
|
| 135 | + $form_name, |
|
| 136 | + $admin_name, |
|
| 137 | + $slug, |
|
| 138 | + $form_action = '', |
|
| 139 | + $form_config = FormHandler::ADD_FORM_TAGS_AND_SUBMIT, |
|
| 140 | + EE_Registry $registry |
|
| 141 | + ) { |
|
| 142 | + $this->setFormName($form_name); |
|
| 143 | + $this->setAdminName($admin_name); |
|
| 144 | + $this->setSlug($slug); |
|
| 145 | + $this->setFormAction($form_action); |
|
| 146 | + $this->setFormConfig($form_config); |
|
| 147 | + $this->setSubmitBtnText(esc_html__('Submit', 'event_espresso')); |
|
| 148 | + $this->registry = $registry; |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + |
|
| 152 | + /** |
|
| 153 | + * @return array |
|
| 154 | + */ |
|
| 155 | + public static function getFormConfigConstants() |
|
| 156 | + { |
|
| 157 | + return array( |
|
| 158 | + FormHandler::ADD_FORM_TAGS_AND_SUBMIT, |
|
| 159 | + FormHandler::ADD_FORM_TAGS_ONLY, |
|
| 160 | + FormHandler::ADD_FORM_SUBMIT_ONLY, |
|
| 161 | + FormHandler::DO_NOT_SETUP_FORM, |
|
| 162 | + ); |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + |
|
| 166 | + /** |
|
| 167 | + * @param bool $for_display |
|
| 168 | + * @return EE_Form_Section_Proper |
|
| 169 | + * @throws EE_Error |
|
| 170 | + * @throws LogicException |
|
| 171 | + */ |
|
| 172 | + public function form($for_display = false) |
|
| 173 | + { |
|
| 174 | + if (! $this->formIsValid()) { |
|
| 175 | + return null; |
|
| 176 | + } |
|
| 177 | + if ($for_display) { |
|
| 178 | + $form_config = $this->formConfig(); |
|
| 179 | + if ($form_config === FormHandler::ADD_FORM_TAGS_AND_SUBMIT |
|
| 180 | + || $form_config === FormHandler::ADD_FORM_SUBMIT_ONLY |
|
| 181 | + ) { |
|
| 182 | + $this->appendSubmitButton(); |
|
| 183 | + $this->clearFormButtonFloats(); |
|
| 184 | + } |
|
| 185 | + } |
|
| 186 | + return $this->form; |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + |
|
| 190 | + /** |
|
| 191 | + * @return boolean |
|
| 192 | + * @throws LogicException |
|
| 193 | + */ |
|
| 194 | + public function formIsValid() |
|
| 195 | + { |
|
| 196 | + if ($this->form instanceof EE_Form_Section_Proper) { |
|
| 197 | + return true; |
|
| 198 | + } |
|
| 199 | + $form = apply_filters( |
|
| 200 | + 'FHEE__EventEspresso_core_libraries_form_sections_form_handlers_FormHandler__formIsValid__generated_form_object', |
|
| 201 | + $this->generate(), |
|
| 202 | + $this |
|
| 203 | + ); |
|
| 204 | + if ($this->verifyForm($form)) { |
|
| 205 | + $this->setForm($form); |
|
| 206 | + } |
|
| 207 | + return true; |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + |
|
| 211 | + /** |
|
| 212 | + * @param EE_Form_Section_Proper|null $form |
|
| 213 | + * @return bool |
|
| 214 | + * @throws LogicException |
|
| 215 | + */ |
|
| 216 | + public function verifyForm(EE_Form_Section_Proper $form = null) |
|
| 217 | + { |
|
| 218 | + $form = $form !== null ? $form : $this->form; |
|
| 219 | + if ($form instanceof EE_Form_Section_Proper) { |
|
| 220 | + return true; |
|
| 221 | + } |
|
| 222 | + throw new LogicException( |
|
| 223 | + sprintf( |
|
| 224 | + esc_html__('The "%1$s" form is invalid or missing. %2$s', 'event_espresso'), |
|
| 225 | + $this->form_name, |
|
| 226 | + var_export($form, true) |
|
| 227 | + ) |
|
| 228 | + ); |
|
| 229 | + } |
|
| 230 | + |
|
| 231 | + |
|
| 232 | + /** |
|
| 233 | + * @param EE_Form_Section_Proper $form |
|
| 234 | + */ |
|
| 235 | + public function setForm(EE_Form_Section_Proper $form) |
|
| 236 | + { |
|
| 237 | + $this->form = $form; |
|
| 238 | + } |
|
| 239 | + |
|
| 240 | + |
|
| 241 | + /** |
|
| 242 | + * @return boolean |
|
| 243 | + */ |
|
| 244 | + public function displayable() |
|
| 245 | + { |
|
| 246 | + return $this->displayable; |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + |
|
| 250 | + /** |
|
| 251 | + * @param boolean $displayable |
|
| 252 | + */ |
|
| 253 | + public function setDisplayable($displayable = false) |
|
| 254 | + { |
|
| 255 | + $this->displayable = filter_var($displayable, FILTER_VALIDATE_BOOLEAN); |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + |
|
| 259 | + /** |
|
| 260 | + * a public name for the form that can be displayed on the frontend of a site |
|
| 261 | + * |
|
| 262 | + * @return string |
|
| 263 | + */ |
|
| 264 | + public function formName() |
|
| 265 | + { |
|
| 266 | + return $this->form_name; |
|
| 267 | + } |
|
| 268 | + |
|
| 269 | + |
|
| 270 | + /** |
|
| 271 | + * @param string $form_name |
|
| 272 | + * @throws InvalidDataTypeException |
|
| 273 | + */ |
|
| 274 | + public function setFormName($form_name) |
|
| 275 | + { |
|
| 276 | + if (! is_string($form_name)) { |
|
| 277 | + throw new InvalidDataTypeException('$form_name', $form_name, 'string'); |
|
| 278 | + } |
|
| 279 | + $this->form_name = $form_name; |
|
| 280 | + } |
|
| 281 | + |
|
| 282 | + |
|
| 283 | + /** |
|
| 284 | + * a public name for the form that can be displayed, but only in the admin |
|
| 285 | + * |
|
| 286 | + * @return string |
|
| 287 | + */ |
|
| 288 | + public function adminName() |
|
| 289 | + { |
|
| 290 | + return $this->admin_name; |
|
| 291 | + } |
|
| 292 | + |
|
| 293 | + |
|
| 294 | + /** |
|
| 295 | + * @param string $admin_name |
|
| 296 | + * @throws InvalidDataTypeException |
|
| 297 | + */ |
|
| 298 | + public function setAdminName($admin_name) |
|
| 299 | + { |
|
| 300 | + if (! is_string($admin_name)) { |
|
| 301 | + throw new InvalidDataTypeException('$admin_name', $admin_name, 'string'); |
|
| 302 | + } |
|
| 303 | + $this->admin_name = $admin_name; |
|
| 304 | + } |
|
| 305 | + |
|
| 306 | + |
|
| 307 | + /** |
|
| 308 | + * a URL friendly string that can be used for identifying the form |
|
| 309 | + * |
|
| 310 | + * @return string |
|
| 311 | + */ |
|
| 312 | + public function slug() |
|
| 313 | + { |
|
| 314 | + return $this->slug; |
|
| 315 | + } |
|
| 316 | + |
|
| 317 | + |
|
| 318 | + /** |
|
| 319 | + * @param string $slug |
|
| 320 | + * @throws InvalidDataTypeException |
|
| 321 | + */ |
|
| 322 | + public function setSlug($slug) |
|
| 323 | + { |
|
| 324 | + if (! is_string($slug)) { |
|
| 325 | + throw new InvalidDataTypeException('$slug', $slug, 'string'); |
|
| 326 | + } |
|
| 327 | + $this->slug = $slug; |
|
| 328 | + } |
|
| 329 | + |
|
| 330 | + |
|
| 331 | + /** |
|
| 332 | + * @return string |
|
| 333 | + */ |
|
| 334 | + public function submitBtnText() |
|
| 335 | + { |
|
| 336 | + return $this->submit_btn_text; |
|
| 337 | + } |
|
| 338 | + |
|
| 339 | + |
|
| 340 | + /** |
|
| 341 | + * @param string $submit_btn_text |
|
| 342 | + * @throws InvalidDataTypeException |
|
| 343 | + * @throws InvalidArgumentException |
|
| 344 | + */ |
|
| 345 | + public function setSubmitBtnText($submit_btn_text) |
|
| 346 | + { |
|
| 347 | + if (! is_string($submit_btn_text)) { |
|
| 348 | + throw new InvalidDataTypeException('$submit_btn_text', $submit_btn_text, 'string'); |
|
| 349 | + } |
|
| 350 | + if (empty($submit_btn_text)) { |
|
| 351 | + throw new InvalidArgumentException( |
|
| 352 | + esc_html__('Can not set Submit button text because an empty string was provided.', 'event_espresso') |
|
| 353 | + ); |
|
| 354 | + } |
|
| 355 | + $this->submit_btn_text = $submit_btn_text; |
|
| 356 | + } |
|
| 357 | + |
|
| 358 | + |
|
| 359 | + /** |
|
| 360 | + * @return string |
|
| 361 | + */ |
|
| 362 | + public function formAction() |
|
| 363 | + { |
|
| 364 | + return ! empty($this->form_args) |
|
| 365 | + ? add_query_arg($this->form_args, $this->form_action) |
|
| 366 | + : $this->form_action; |
|
| 367 | + } |
|
| 368 | + |
|
| 369 | + |
|
| 370 | + /** |
|
| 371 | + * @param string $form_action |
|
| 372 | + * @throws InvalidDataTypeException |
|
| 373 | + */ |
|
| 374 | + public function setFormAction($form_action) |
|
| 375 | + { |
|
| 376 | + if (! is_string($form_action)) { |
|
| 377 | + throw new InvalidDataTypeException('$form_action', $form_action, 'string'); |
|
| 378 | + } |
|
| 379 | + $this->form_action = $form_action; |
|
| 380 | + } |
|
| 381 | + |
|
| 382 | + |
|
| 383 | + /** |
|
| 384 | + * @param array $form_args |
|
| 385 | + * @throws InvalidDataTypeException |
|
| 386 | + * @throws InvalidArgumentException |
|
| 387 | + */ |
|
| 388 | + public function addFormActionArgs($form_args = array()) |
|
| 389 | + { |
|
| 390 | + if (is_object($form_args)) { |
|
| 391 | + throw new InvalidDataTypeException( |
|
| 392 | + '$form_args', |
|
| 393 | + $form_args, |
|
| 394 | + 'anything other than an object was expected.' |
|
| 395 | + ); |
|
| 396 | + } |
|
| 397 | + if (empty($form_args)) { |
|
| 398 | + throw new InvalidArgumentException( |
|
| 399 | + esc_html__('The redirect arguments can not be an empty array.', 'event_espresso') |
|
| 400 | + ); |
|
| 401 | + } |
|
| 402 | + $this->form_args = array_merge($this->form_args, $form_args); |
|
| 403 | + } |
|
| 404 | + |
|
| 405 | + |
|
| 406 | + /** |
|
| 407 | + * @return string |
|
| 408 | + */ |
|
| 409 | + public function formConfig() |
|
| 410 | + { |
|
| 411 | + return $this->form_config; |
|
| 412 | + } |
|
| 413 | + |
|
| 414 | + |
|
| 415 | + /** |
|
| 416 | + * @param string $form_config |
|
| 417 | + * @throws DomainException |
|
| 418 | + */ |
|
| 419 | + public function setFormConfig($form_config) |
|
| 420 | + { |
|
| 421 | + if (! in_array( |
|
| 422 | + $form_config, |
|
| 423 | + array( |
|
| 424 | + FormHandler::ADD_FORM_TAGS_AND_SUBMIT, |
|
| 425 | + FormHandler::ADD_FORM_TAGS_ONLY, |
|
| 426 | + FormHandler::ADD_FORM_SUBMIT_ONLY, |
|
| 427 | + FormHandler::DO_NOT_SETUP_FORM, |
|
| 428 | + ), |
|
| 429 | + true |
|
| 430 | + ) |
|
| 431 | + ) { |
|
| 432 | + throw new DomainException( |
|
| 433 | + sprintf( |
|
| 434 | + esc_html__( |
|
| 435 | + '"%1$s" is not a valid value for the form config. Please use one of the class constants on \EventEspresso\core\libraries\form_sections\form_handlers\Form', |
|
| 436 | + 'event_espresso' |
|
| 437 | + ), |
|
| 438 | + $form_config |
|
| 439 | + ) |
|
| 440 | + ); |
|
| 441 | + } |
|
| 442 | + $this->form_config = $form_config; |
|
| 443 | + } |
|
| 444 | + |
|
| 445 | + |
|
| 446 | + /** |
|
| 447 | + * called after the form is instantiated |
|
| 448 | + * and used for performing any logic that needs to occur early |
|
| 449 | + * before any of the other methods are called. |
|
| 450 | + * returns true if everything is ok to proceed, |
|
| 451 | + * and false if no further form logic should be implemented |
|
| 452 | + * |
|
| 453 | + * @return boolean |
|
| 454 | + */ |
|
| 455 | + public function initialize() |
|
| 456 | + { |
|
| 457 | + $this->form_has_errors = EE_Error::has_error(true); |
|
| 458 | + return true; |
|
| 459 | + } |
|
| 460 | + |
|
| 461 | + |
|
| 462 | + /** |
|
| 463 | + * used for setting up css and js |
|
| 464 | + * |
|
| 465 | + * @return void |
|
| 466 | + * @throws LogicException |
|
| 467 | + * @throws EE_Error |
|
| 468 | + */ |
|
| 469 | + public function enqueueStylesAndScripts() |
|
| 470 | + { |
|
| 471 | + $this->form()->enqueue_js(); |
|
| 472 | + } |
|
| 473 | + |
|
| 474 | + |
|
| 475 | + /** |
|
| 476 | + * creates and returns the actual form |
|
| 477 | + * |
|
| 478 | + * @return EE_Form_Section_Proper |
|
| 479 | + */ |
|
| 480 | + abstract public function generate(); |
|
| 481 | + |
|
| 482 | + |
|
| 483 | + /** |
|
| 484 | + * creates and returns an EE_Submit_Input labeled "Submit" |
|
| 485 | + * |
|
| 486 | + * @param string $text |
|
| 487 | + * @return EE_Submit_Input |
|
| 488 | + */ |
|
| 489 | + public function generateSubmitButton($text = '') |
|
| 490 | + { |
|
| 491 | + $text = ! empty($text) ? $text : $this->submitBtnText(); |
|
| 492 | + return new EE_Submit_Input( |
|
| 493 | + array( |
|
| 494 | + 'html_name' => 'ee-form-submit-' . $this->slug(), |
|
| 495 | + 'html_id' => 'ee-form-submit-' . $this->slug(), |
|
| 496 | + 'html_class' => 'ee-form-submit', |
|
| 497 | + 'html_label' => ' ', |
|
| 498 | + 'other_html_attributes' => ' rel="' . $this->slug() . '"', |
|
| 499 | + 'default' => $text, |
|
| 500 | + ) |
|
| 501 | + ); |
|
| 502 | + } |
|
| 503 | + |
|
| 504 | + |
|
| 505 | + /** |
|
| 506 | + * calls generateSubmitButton() and appends it onto the form along with a float clearing div |
|
| 507 | + * |
|
| 508 | + * @param string $text |
|
| 509 | + * @return void |
|
| 510 | + * @throws EE_Error |
|
| 511 | + */ |
|
| 512 | + public function appendSubmitButton($text = '') |
|
| 513 | + { |
|
| 514 | + if ($this->form->subsection_exists($this->slug() . '-submit-btn')) { |
|
| 515 | + return; |
|
| 516 | + } |
|
| 517 | + $this->form->add_subsections( |
|
| 518 | + array($this->slug() . '-submit-btn' => $this->generateSubmitButton($text)), |
|
| 519 | + null, |
|
| 520 | + false |
|
| 521 | + ); |
|
| 522 | + } |
|
| 523 | + |
|
| 524 | + |
|
| 525 | + /** |
|
| 526 | + * creates and returns an EE_Submit_Input labeled "Cancel" |
|
| 527 | + * |
|
| 528 | + * @param string $text |
|
| 529 | + * @return EE_Submit_Input |
|
| 530 | + */ |
|
| 531 | + public function generateCancelButton($text = '') |
|
| 532 | + { |
|
| 533 | + $cancel_button = new EE_Submit_Input( |
|
| 534 | + array( |
|
| 535 | + 'html_name' => 'ee-form-submit-' . $this->slug(), // YES! Same name as submit !!! |
|
| 536 | + 'html_id' => 'ee-cancel-form-' . $this->slug(), |
|
| 537 | + 'html_class' => 'ee-cancel-form', |
|
| 538 | + 'html_label' => ' ', |
|
| 539 | + 'other_html_attributes' => ' rel="' . $this->slug() . '"', |
|
| 540 | + 'default' => ! empty($text) ? $text : esc_html__('Cancel', 'event_espresso'), |
|
| 541 | + ) |
|
| 542 | + ); |
|
| 543 | + $cancel_button->set_button_css_attributes(false); |
|
| 544 | + return $cancel_button; |
|
| 545 | + } |
|
| 546 | + |
|
| 547 | + |
|
| 548 | + /** |
|
| 549 | + * appends a float clearing div onto end of form |
|
| 550 | + * |
|
| 551 | + * @return void |
|
| 552 | + * @throws EE_Error |
|
| 553 | + */ |
|
| 554 | + public function clearFormButtonFloats() |
|
| 555 | + { |
|
| 556 | + $this->form->add_subsections( |
|
| 557 | + array( |
|
| 558 | + 'clear-submit-btn-float' => new EE_Form_Section_HTML( |
|
| 559 | + EEH_HTML::div('', '', 'clear-float') . EEH_HTML::divx() |
|
| 560 | + ), |
|
| 561 | + ), |
|
| 562 | + null, |
|
| 563 | + false |
|
| 564 | + ); |
|
| 565 | + } |
|
| 566 | + |
|
| 567 | + |
|
| 568 | + /** |
|
| 569 | + * takes the generated form and displays it along with ony other non-form HTML that may be required |
|
| 570 | + * returns a string of HTML that can be directly echoed in a template |
|
| 571 | + * |
|
| 572 | + * @return string |
|
| 573 | + * @throws \InvalidArgumentException |
|
| 574 | + * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 575 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 576 | + * @throws LogicException |
|
| 577 | + * @throws EE_Error |
|
| 578 | + */ |
|
| 579 | + public function display() |
|
| 580 | + { |
|
| 581 | + $form_html = apply_filters( |
|
| 582 | + 'FHEE__EventEspresso_core_libraries_form_sections_form_handlers_FormHandler__display__before_form', |
|
| 583 | + '' |
|
| 584 | + ); |
|
| 585 | + $form_config = $this->formConfig(); |
|
| 586 | + if ($form_config === FormHandler::ADD_FORM_TAGS_AND_SUBMIT |
|
| 587 | + || $form_config === FormHandler::ADD_FORM_TAGS_ONLY |
|
| 588 | + ) { |
|
| 589 | + $additional_props = $this->requiresMultipartEnctype() |
|
| 590 | + ? 'enctype="multipart/form-data"' |
|
| 591 | + : ''; |
|
| 592 | + $form_html .= $this->form()->form_open( |
|
| 593 | + $this->formAction(), |
|
| 594 | + 'POST', |
|
| 595 | + $additional_props |
|
| 596 | + ); |
|
| 597 | + } |
|
| 598 | + $form_html .= $this->form(true)->get_html(); |
|
| 599 | + if ($form_config === FormHandler::ADD_FORM_TAGS_AND_SUBMIT |
|
| 600 | + || $form_config === FormHandler::ADD_FORM_TAGS_ONLY |
|
| 601 | + ) { |
|
| 602 | + $form_html .= $this->form()->form_close(); |
|
| 603 | + } |
|
| 604 | + $form_html .= apply_filters( |
|
| 605 | + 'FHEE__EventEspresso_core_libraries_form_sections_form_handlers_FormHandler__display__after_form', |
|
| 606 | + '' |
|
| 607 | + ); |
|
| 608 | + return $form_html; |
|
| 609 | + } |
|
| 610 | + |
|
| 611 | + /** |
|
| 612 | + * Determines if this form needs "enctype='multipart/form-data'" or not. |
|
| 613 | + * @since $VID:$ |
|
| 614 | + * @return bool |
|
| 615 | + * @throws EE_Error |
|
| 616 | + */ |
|
| 617 | + public function requiresMultipartEnctype() |
|
| 618 | + { |
|
| 619 | + foreach ($this->form()->inputs_in_subsections() as $input) { |
|
| 620 | + if ($input instanceof EE_File_Input) { |
|
| 621 | + return true; |
|
| 622 | + } |
|
| 623 | + } |
|
| 624 | + return false; |
|
| 625 | + } |
|
| 626 | + |
|
| 627 | + |
|
| 628 | + /** |
|
| 629 | + * handles processing the form submission |
|
| 630 | + * returns true or false depending on whether the form was processed successfully or not |
|
| 631 | + * |
|
| 632 | + * @param array $submitted_form_data |
|
| 633 | + * @return array |
|
| 634 | + * @throws \InvalidArgumentException |
|
| 635 | + * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 636 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 637 | + * @throws EE_Error |
|
| 638 | + * @throws LogicException |
|
| 639 | + * @throws InvalidFormSubmissionException |
|
| 640 | + */ |
|
| 641 | + public function process($submitted_form_data = array()) |
|
| 642 | + { |
|
| 643 | + if (! $this->form()->was_submitted($submitted_form_data)) { |
|
| 644 | + throw new InvalidFormSubmissionException($this->form_name); |
|
| 645 | + } |
|
| 646 | + $this->form(true)->receive_form_submission($submitted_form_data); |
|
| 647 | + if (! $this->form()->is_valid()) { |
|
| 648 | + throw new InvalidFormSubmissionException( |
|
| 649 | + $this->form_name, |
|
| 650 | + sprintf( |
|
| 651 | + esc_html__( |
|
| 652 | + 'The "%1$s" form is invalid. Please correct the following errors and resubmit: %2$s %3$s', |
|
| 653 | + 'event_espresso' |
|
| 654 | + ), |
|
| 655 | + $this->form_name, |
|
| 656 | + '<br />', |
|
| 657 | + implode('<br />', $this->form()->get_validation_errors_accumulated()) |
|
| 658 | + ) |
|
| 659 | + ); |
|
| 660 | + } |
|
| 661 | + return apply_filters( |
|
| 662 | + 'FHEE__EventEspresso_core_libraries_form_sections_form_handlers_FormHandler__process__valid_data', |
|
| 663 | + $this->form()->valid_data(), |
|
| 664 | + $this |
|
| 665 | + ); |
|
| 666 | + } |
|
| 667 | 667 | } |
@@ -13,40 +13,40 @@ |
||
| 13 | 13 | class EE_File_Normalization extends EE_Normalization_Strategy_Base |
| 14 | 14 | { |
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * Keep in mind $value_to_normalize should be a FileSubmissionInterface or null, so this shouldn't really do |
|
| 18 | - * much (other than NOT convert it to a string or something). |
|
| 19 | - * @param string $value_to_normalize |
|
| 20 | - * @return FileSubmissionInterface |
|
| 21 | - */ |
|
| 22 | - public function normalize($value_to_normalize) |
|
| 23 | - { |
|
| 24 | - if ($value_to_normalize instanceof FileSubmissionInterface || is_null($value_to_normalize)) { |
|
| 25 | - return $value_to_normalize; |
|
| 26 | - } else { |
|
| 27 | - throw new EE_Validation_Error( |
|
| 28 | - esc_html__('The file input has an invalid format.', 'event_espresso') |
|
| 29 | - ); |
|
| 30 | - } |
|
| 31 | - } |
|
| 16 | + /** |
|
| 17 | + * Keep in mind $value_to_normalize should be a FileSubmissionInterface or null, so this shouldn't really do |
|
| 18 | + * much (other than NOT convert it to a string or something). |
|
| 19 | + * @param string $value_to_normalize |
|
| 20 | + * @return FileSubmissionInterface |
|
| 21 | + */ |
|
| 22 | + public function normalize($value_to_normalize) |
|
| 23 | + { |
|
| 24 | + if ($value_to_normalize instanceof FileSubmissionInterface || is_null($value_to_normalize)) { |
|
| 25 | + return $value_to_normalize; |
|
| 26 | + } else { |
|
| 27 | + throw new EE_Validation_Error( |
|
| 28 | + esc_html__('The file input has an invalid format.', 'event_espresso') |
|
| 29 | + ); |
|
| 30 | + } |
|
| 31 | + } |
|
| 32 | 32 | |
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * This may be called prematurely on submitted data, so we actually don't want to convert it into a string because |
|
| 36 | - * we'll lose all the FileSubmissionInterface data. So prefer to leave it alone. FileSubmissionInterface |
|
| 37 | - * can be cast to a string just fine so it's good as-is. |
|
| 38 | - * |
|
| 39 | - * @param string $normalized_value |
|
| 40 | - * @return string |
|
| 41 | - */ |
|
| 42 | - public function unnormalize($normalized_value) |
|
| 43 | - { |
|
| 44 | - if ($normalized_value instanceof FileSubmissionInterface || is_null($normalized_value)) { |
|
| 45 | - // Leave it as the object, it can be treated like a string because it |
|
| 46 | - // overrides __toString() |
|
| 47 | - return $normalized_value; |
|
| 48 | - } else { |
|
| 49 | - return (string) $normalized_value; |
|
| 50 | - } |
|
| 51 | - } |
|
| 34 | + /** |
|
| 35 | + * This may be called prematurely on submitted data, so we actually don't want to convert it into a string because |
|
| 36 | + * we'll lose all the FileSubmissionInterface data. So prefer to leave it alone. FileSubmissionInterface |
|
| 37 | + * can be cast to a string just fine so it's good as-is. |
|
| 38 | + * |
|
| 39 | + * @param string $normalized_value |
|
| 40 | + * @return string |
|
| 41 | + */ |
|
| 42 | + public function unnormalize($normalized_value) |
|
| 43 | + { |
|
| 44 | + if ($normalized_value instanceof FileSubmissionInterface || is_null($normalized_value)) { |
|
| 45 | + // Leave it as the object, it can be treated like a string because it |
|
| 46 | + // overrides __toString() |
|
| 47 | + return $normalized_value; |
|
| 48 | + } else { |
|
| 49 | + return (string) $normalized_value; |
|
| 50 | + } |
|
| 51 | + } |
|
| 52 | 52 | } |
@@ -18,156 +18,156 @@ |
||
| 18 | 18 | class FqcnLocator extends Locator |
| 19 | 19 | { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * @var array $FQCNs |
|
| 23 | - */ |
|
| 24 | - protected $FQCNs = array(); |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * @var array $namespaces |
|
| 28 | - */ |
|
| 29 | - protected $namespaces = array(); |
|
| 30 | - |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * @access protected |
|
| 34 | - * @param string $namespace |
|
| 35 | - * @param string $namespace_base_dir |
|
| 36 | - * @throws InvalidDataTypeException |
|
| 37 | - */ |
|
| 38 | - protected function setNamespace($namespace, $namespace_base_dir) |
|
| 39 | - { |
|
| 40 | - if (! is_string($namespace)) { |
|
| 41 | - throw new InvalidDataTypeException('$namespace', $namespace, 'string'); |
|
| 42 | - } |
|
| 43 | - if (! is_string($namespace_base_dir)) { |
|
| 44 | - throw new InvalidDataTypeException('$namespace_base_dir', $namespace_base_dir, 'string'); |
|
| 45 | - } |
|
| 46 | - $this->namespaces[ $namespace ] = $namespace_base_dir; |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * @access public |
|
| 52 | - * @return array |
|
| 53 | - */ |
|
| 54 | - public function getFQCNs() |
|
| 55 | - { |
|
| 56 | - return $this->FQCNs; |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * @access public |
|
| 62 | - * @return int |
|
| 63 | - */ |
|
| 64 | - public function count() |
|
| 65 | - { |
|
| 66 | - return count($this->FQCNs); |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * given a valid namespace, will find all files that match the provided mask |
|
| 72 | - * |
|
| 73 | - * @access public |
|
| 74 | - * @param string|array $namespaces |
|
| 75 | - * @return array |
|
| 76 | - * @throws InvalidClassException |
|
| 77 | - * @throws InvalidDataTypeException |
|
| 78 | - */ |
|
| 79 | - public function locate($namespaces) |
|
| 80 | - { |
|
| 81 | - if (! (is_string($namespaces) || is_array($namespaces))) { |
|
| 82 | - throw new InvalidDataTypeException('$namespaces', $namespaces, 'string or array'); |
|
| 83 | - } |
|
| 84 | - foreach ((array) $namespaces as $namespace) { |
|
| 85 | - foreach ($this->findFQCNsByNamespace($namespace) as $key => $file) { |
|
| 86 | - $this->FQCNs[ $key ] = $file; |
|
| 87 | - } |
|
| 88 | - } |
|
| 89 | - return $this->FQCNs; |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * given a partial namespace, will find all files in that folder |
|
| 95 | - * ** PLZ NOTE ** |
|
| 96 | - * This assumes that all files within the specified folder should be loaded |
|
| 97 | - * |
|
| 98 | - * @access protected |
|
| 99 | - * @param string $partial_namespace |
|
| 100 | - * @return array |
|
| 101 | - * @throws InvalidClassException |
|
| 102 | - * @throws InvalidDataTypeException |
|
| 103 | - */ |
|
| 104 | - protected function findFQCNsByNamespace($partial_namespace) |
|
| 105 | - { |
|
| 106 | - $iterator = new FilesystemIterator( |
|
| 107 | - $this->getDirectoryFromPartialNamespace($partial_namespace) |
|
| 108 | - ); |
|
| 109 | - $iterator->setFlags(FilesystemIterator::CURRENT_AS_FILEINFO); |
|
| 110 | - $iterator->setFlags(FilesystemIterator::UNIX_PATHS); |
|
| 111 | - if (iterator_count($iterator) === 0) { |
|
| 112 | - return array(); |
|
| 113 | - } |
|
| 114 | - foreach ($iterator as $file) { |
|
| 115 | - if ($file->isFile() && $file->getExtension() === 'php') { |
|
| 116 | - $file = $file->getPath() . DS . $file->getBasename('.php'); |
|
| 117 | - foreach ($this->namespaces as $namespace => $base_dir) { |
|
| 118 | - $namespace .= Psr4Autoloader::NS; |
|
| 119 | - if (strpos($file, $base_dir) === 0) { |
|
| 120 | - $this->FQCNs[] = Psr4Autoloader::NS . str_replace( |
|
| 121 | - array($base_dir, DS), |
|
| 122 | - array($namespace, Psr4Autoloader::NS), |
|
| 123 | - $file |
|
| 124 | - ); |
|
| 125 | - } |
|
| 126 | - } |
|
| 127 | - } |
|
| 128 | - } |
|
| 129 | - return $this->FQCNs; |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - |
|
| 133 | - /** |
|
| 134 | - * getDirectoryFromPartialNamespace |
|
| 135 | - * |
|
| 136 | - * @access protected |
|
| 137 | - * @param string $partial_namespace almost fully qualified class name ? |
|
| 138 | - * @return string |
|
| 139 | - * @throws InvalidDataTypeException |
|
| 140 | - * @throws InvalidClassException |
|
| 141 | - */ |
|
| 142 | - protected function getDirectoryFromPartialNamespace($partial_namespace) |
|
| 143 | - { |
|
| 144 | - if (empty($partial_namespace)) { |
|
| 145 | - throw new InvalidClassException($partial_namespace); |
|
| 146 | - } |
|
| 147 | - // load our PSR-4 Autoloader so we can get the list of registered namespaces from it |
|
| 148 | - $psr4_loader = \EE_Psr4AutoloaderInit::psr4_loader(); |
|
| 149 | - // breakup the incoming namespace into segments so we can loop thru them |
|
| 150 | - $namespace_segments = explode(Psr4Autoloader::NS, trim($partial_namespace, Psr4Autoloader::NS)); |
|
| 151 | - // we're only interested in the Vendor and secondary base, so pull those from the array |
|
| 152 | - $vendor_base = array_slice($namespace_segments, 0, 2); |
|
| 153 | - $namespace = $prefix = null; |
|
| 154 | - while (! empty($vendor_base)) { |
|
| 155 | - $namespace = implode(Psr4Autoloader::NS, $vendor_base); |
|
| 156 | - // check if there's a base directory registered for that namespace |
|
| 157 | - $prefix = $psr4_loader->prefixes($namespace . Psr4Autoloader::NS); |
|
| 158 | - if (! empty($prefix) && ! empty($prefix[0])) { |
|
| 159 | - // found one! |
|
| 160 | - break; |
|
| 161 | - } |
|
| 162 | - // remove base and try vendor only portion of namespace |
|
| 163 | - array_pop($vendor_base); |
|
| 164 | - } |
|
| 165 | - // nope? then the incoming namespace is invalid |
|
| 166 | - if (empty($prefix) || empty($prefix[0])) { |
|
| 167 | - throw new InvalidClassException($partial_namespace); |
|
| 168 | - } |
|
| 169 | - $this->setNamespace($namespace, $prefix[0]); |
|
| 170 | - // but if it's good, add that base directory to the rest of the path, and return it |
|
| 171 | - return $prefix[0] . implode(DS, array_diff($namespace_segments, $vendor_base)) . DS; |
|
| 172 | - } |
|
| 21 | + /** |
|
| 22 | + * @var array $FQCNs |
|
| 23 | + */ |
|
| 24 | + protected $FQCNs = array(); |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * @var array $namespaces |
|
| 28 | + */ |
|
| 29 | + protected $namespaces = array(); |
|
| 30 | + |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * @access protected |
|
| 34 | + * @param string $namespace |
|
| 35 | + * @param string $namespace_base_dir |
|
| 36 | + * @throws InvalidDataTypeException |
|
| 37 | + */ |
|
| 38 | + protected function setNamespace($namespace, $namespace_base_dir) |
|
| 39 | + { |
|
| 40 | + if (! is_string($namespace)) { |
|
| 41 | + throw new InvalidDataTypeException('$namespace', $namespace, 'string'); |
|
| 42 | + } |
|
| 43 | + if (! is_string($namespace_base_dir)) { |
|
| 44 | + throw new InvalidDataTypeException('$namespace_base_dir', $namespace_base_dir, 'string'); |
|
| 45 | + } |
|
| 46 | + $this->namespaces[ $namespace ] = $namespace_base_dir; |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * @access public |
|
| 52 | + * @return array |
|
| 53 | + */ |
|
| 54 | + public function getFQCNs() |
|
| 55 | + { |
|
| 56 | + return $this->FQCNs; |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * @access public |
|
| 62 | + * @return int |
|
| 63 | + */ |
|
| 64 | + public function count() |
|
| 65 | + { |
|
| 66 | + return count($this->FQCNs); |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * given a valid namespace, will find all files that match the provided mask |
|
| 72 | + * |
|
| 73 | + * @access public |
|
| 74 | + * @param string|array $namespaces |
|
| 75 | + * @return array |
|
| 76 | + * @throws InvalidClassException |
|
| 77 | + * @throws InvalidDataTypeException |
|
| 78 | + */ |
|
| 79 | + public function locate($namespaces) |
|
| 80 | + { |
|
| 81 | + if (! (is_string($namespaces) || is_array($namespaces))) { |
|
| 82 | + throw new InvalidDataTypeException('$namespaces', $namespaces, 'string or array'); |
|
| 83 | + } |
|
| 84 | + foreach ((array) $namespaces as $namespace) { |
|
| 85 | + foreach ($this->findFQCNsByNamespace($namespace) as $key => $file) { |
|
| 86 | + $this->FQCNs[ $key ] = $file; |
|
| 87 | + } |
|
| 88 | + } |
|
| 89 | + return $this->FQCNs; |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * given a partial namespace, will find all files in that folder |
|
| 95 | + * ** PLZ NOTE ** |
|
| 96 | + * This assumes that all files within the specified folder should be loaded |
|
| 97 | + * |
|
| 98 | + * @access protected |
|
| 99 | + * @param string $partial_namespace |
|
| 100 | + * @return array |
|
| 101 | + * @throws InvalidClassException |
|
| 102 | + * @throws InvalidDataTypeException |
|
| 103 | + */ |
|
| 104 | + protected function findFQCNsByNamespace($partial_namespace) |
|
| 105 | + { |
|
| 106 | + $iterator = new FilesystemIterator( |
|
| 107 | + $this->getDirectoryFromPartialNamespace($partial_namespace) |
|
| 108 | + ); |
|
| 109 | + $iterator->setFlags(FilesystemIterator::CURRENT_AS_FILEINFO); |
|
| 110 | + $iterator->setFlags(FilesystemIterator::UNIX_PATHS); |
|
| 111 | + if (iterator_count($iterator) === 0) { |
|
| 112 | + return array(); |
|
| 113 | + } |
|
| 114 | + foreach ($iterator as $file) { |
|
| 115 | + if ($file->isFile() && $file->getExtension() === 'php') { |
|
| 116 | + $file = $file->getPath() . DS . $file->getBasename('.php'); |
|
| 117 | + foreach ($this->namespaces as $namespace => $base_dir) { |
|
| 118 | + $namespace .= Psr4Autoloader::NS; |
|
| 119 | + if (strpos($file, $base_dir) === 0) { |
|
| 120 | + $this->FQCNs[] = Psr4Autoloader::NS . str_replace( |
|
| 121 | + array($base_dir, DS), |
|
| 122 | + array($namespace, Psr4Autoloader::NS), |
|
| 123 | + $file |
|
| 124 | + ); |
|
| 125 | + } |
|
| 126 | + } |
|
| 127 | + } |
|
| 128 | + } |
|
| 129 | + return $this->FQCNs; |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + |
|
| 133 | + /** |
|
| 134 | + * getDirectoryFromPartialNamespace |
|
| 135 | + * |
|
| 136 | + * @access protected |
|
| 137 | + * @param string $partial_namespace almost fully qualified class name ? |
|
| 138 | + * @return string |
|
| 139 | + * @throws InvalidDataTypeException |
|
| 140 | + * @throws InvalidClassException |
|
| 141 | + */ |
|
| 142 | + protected function getDirectoryFromPartialNamespace($partial_namespace) |
|
| 143 | + { |
|
| 144 | + if (empty($partial_namespace)) { |
|
| 145 | + throw new InvalidClassException($partial_namespace); |
|
| 146 | + } |
|
| 147 | + // load our PSR-4 Autoloader so we can get the list of registered namespaces from it |
|
| 148 | + $psr4_loader = \EE_Psr4AutoloaderInit::psr4_loader(); |
|
| 149 | + // breakup the incoming namespace into segments so we can loop thru them |
|
| 150 | + $namespace_segments = explode(Psr4Autoloader::NS, trim($partial_namespace, Psr4Autoloader::NS)); |
|
| 151 | + // we're only interested in the Vendor and secondary base, so pull those from the array |
|
| 152 | + $vendor_base = array_slice($namespace_segments, 0, 2); |
|
| 153 | + $namespace = $prefix = null; |
|
| 154 | + while (! empty($vendor_base)) { |
|
| 155 | + $namespace = implode(Psr4Autoloader::NS, $vendor_base); |
|
| 156 | + // check if there's a base directory registered for that namespace |
|
| 157 | + $prefix = $psr4_loader->prefixes($namespace . Psr4Autoloader::NS); |
|
| 158 | + if (! empty($prefix) && ! empty($prefix[0])) { |
|
| 159 | + // found one! |
|
| 160 | + break; |
|
| 161 | + } |
|
| 162 | + // remove base and try vendor only portion of namespace |
|
| 163 | + array_pop($vendor_base); |
|
| 164 | + } |
|
| 165 | + // nope? then the incoming namespace is invalid |
|
| 166 | + if (empty($prefix) || empty($prefix[0])) { |
|
| 167 | + throw new InvalidClassException($partial_namespace); |
|
| 168 | + } |
|
| 169 | + $this->setNamespace($namespace, $prefix[0]); |
|
| 170 | + // but if it's good, add that base directory to the rest of the path, and return it |
|
| 171 | + return $prefix[0] . implode(DS, array_diff($namespace_segments, $vendor_base)) . DS; |
|
| 172 | + } |
|
| 173 | 173 | } |
@@ -37,13 +37,13 @@ discard block |
||
| 37 | 37 | */ |
| 38 | 38 | protected function setNamespace($namespace, $namespace_base_dir) |
| 39 | 39 | { |
| 40 | - if (! is_string($namespace)) { |
|
| 40 | + if ( ! is_string($namespace)) { |
|
| 41 | 41 | throw new InvalidDataTypeException('$namespace', $namespace, 'string'); |
| 42 | 42 | } |
| 43 | - if (! is_string($namespace_base_dir)) { |
|
| 43 | + if ( ! is_string($namespace_base_dir)) { |
|
| 44 | 44 | throw new InvalidDataTypeException('$namespace_base_dir', $namespace_base_dir, 'string'); |
| 45 | 45 | } |
| 46 | - $this->namespaces[ $namespace ] = $namespace_base_dir; |
|
| 46 | + $this->namespaces[$namespace] = $namespace_base_dir; |
|
| 47 | 47 | } |
| 48 | 48 | |
| 49 | 49 | |
@@ -78,12 +78,12 @@ discard block |
||
| 78 | 78 | */ |
| 79 | 79 | public function locate($namespaces) |
| 80 | 80 | { |
| 81 | - if (! (is_string($namespaces) || is_array($namespaces))) { |
|
| 81 | + if ( ! (is_string($namespaces) || is_array($namespaces))) { |
|
| 82 | 82 | throw new InvalidDataTypeException('$namespaces', $namespaces, 'string or array'); |
| 83 | 83 | } |
| 84 | 84 | foreach ((array) $namespaces as $namespace) { |
| 85 | 85 | foreach ($this->findFQCNsByNamespace($namespace) as $key => $file) { |
| 86 | - $this->FQCNs[ $key ] = $file; |
|
| 86 | + $this->FQCNs[$key] = $file; |
|
| 87 | 87 | } |
| 88 | 88 | } |
| 89 | 89 | return $this->FQCNs; |
@@ -113,11 +113,11 @@ discard block |
||
| 113 | 113 | } |
| 114 | 114 | foreach ($iterator as $file) { |
| 115 | 115 | if ($file->isFile() && $file->getExtension() === 'php') { |
| 116 | - $file = $file->getPath() . DS . $file->getBasename('.php'); |
|
| 116 | + $file = $file->getPath().DS.$file->getBasename('.php'); |
|
| 117 | 117 | foreach ($this->namespaces as $namespace => $base_dir) { |
| 118 | 118 | $namespace .= Psr4Autoloader::NS; |
| 119 | 119 | if (strpos($file, $base_dir) === 0) { |
| 120 | - $this->FQCNs[] = Psr4Autoloader::NS . str_replace( |
|
| 120 | + $this->FQCNs[] = Psr4Autoloader::NS.str_replace( |
|
| 121 | 121 | array($base_dir, DS), |
| 122 | 122 | array($namespace, Psr4Autoloader::NS), |
| 123 | 123 | $file |
@@ -151,11 +151,11 @@ discard block |
||
| 151 | 151 | // we're only interested in the Vendor and secondary base, so pull those from the array |
| 152 | 152 | $vendor_base = array_slice($namespace_segments, 0, 2); |
| 153 | 153 | $namespace = $prefix = null; |
| 154 | - while (! empty($vendor_base)) { |
|
| 154 | + while ( ! empty($vendor_base)) { |
|
| 155 | 155 | $namespace = implode(Psr4Autoloader::NS, $vendor_base); |
| 156 | 156 | // check if there's a base directory registered for that namespace |
| 157 | - $prefix = $psr4_loader->prefixes($namespace . Psr4Autoloader::NS); |
|
| 158 | - if (! empty($prefix) && ! empty($prefix[0])) { |
|
| 157 | + $prefix = $psr4_loader->prefixes($namespace.Psr4Autoloader::NS); |
|
| 158 | + if ( ! empty($prefix) && ! empty($prefix[0])) { |
|
| 159 | 159 | // found one! |
| 160 | 160 | break; |
| 161 | 161 | } |
@@ -168,6 +168,6 @@ discard block |
||
| 168 | 168 | } |
| 169 | 169 | $this->setNamespace($namespace, $prefix[0]); |
| 170 | 170 | // but if it's good, add that base directory to the rest of the path, and return it |
| 171 | - return $prefix[0] . implode(DS, array_diff($namespace_segments, $vendor_base)) . DS; |
|
| 171 | + return $prefix[0].implode(DS, array_diff($namespace_segments, $vendor_base)).DS; |
|
| 172 | 172 | } |
| 173 | 173 | } |
@@ -21,2045 +21,2045 @@ |
||
| 21 | 21 | class EEH_Line_Item |
| 22 | 22 | { |
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * Adds a simple item (unrelated to any other model object) to the provided PARENT line item. |
|
| 26 | - * Does NOT automatically re-calculate the line item totals or update the related transaction. |
|
| 27 | - * You should call recalculate_total_including_taxes() on the grant total line item after this |
|
| 28 | - * to update the subtotals, and EE_Registration_Processor::calculate_reg_final_prices_per_line_item() |
|
| 29 | - * to keep the registration final prices in-sync with the transaction's total. |
|
| 30 | - * |
|
| 31 | - * @param EE_Line_Item $parent_line_item |
|
| 32 | - * @param string $name |
|
| 33 | - * @param float $unit_price |
|
| 34 | - * @param string $description |
|
| 35 | - * @param int $quantity |
|
| 36 | - * @param boolean $taxable |
|
| 37 | - * @param boolean $code if set to a value, ensures there is only one line item with that code |
|
| 38 | - * @return boolean success |
|
| 39 | - * @throws EE_Error |
|
| 40 | - * @throws InvalidArgumentException |
|
| 41 | - * @throws InvalidDataTypeException |
|
| 42 | - * @throws InvalidInterfaceException |
|
| 43 | - * @throws ReflectionException |
|
| 44 | - */ |
|
| 45 | - public static function add_unrelated_item( |
|
| 46 | - EE_Line_Item $parent_line_item, |
|
| 47 | - $name, |
|
| 48 | - $unit_price, |
|
| 49 | - $description = '', |
|
| 50 | - $quantity = 1, |
|
| 51 | - $taxable = false, |
|
| 52 | - $code = null |
|
| 53 | - ) { |
|
| 54 | - $items_subtotal = self::get_pre_tax_subtotal($parent_line_item); |
|
| 55 | - $line_item = EE_Line_Item::new_instance(array( |
|
| 56 | - 'LIN_name' => $name, |
|
| 57 | - 'LIN_desc' => $description, |
|
| 58 | - 'LIN_unit_price' => $unit_price, |
|
| 59 | - 'LIN_quantity' => $quantity, |
|
| 60 | - 'LIN_percent' => null, |
|
| 61 | - 'LIN_is_taxable' => $taxable, |
|
| 62 | - 'LIN_order' => $items_subtotal instanceof EE_Line_Item ? count($items_subtotal->children()) : 0, |
|
| 63 | - 'LIN_total' => (float) $unit_price * (int) $quantity, |
|
| 64 | - 'LIN_type' => EEM_Line_Item::type_line_item, |
|
| 65 | - 'LIN_code' => $code, |
|
| 66 | - )); |
|
| 67 | - $line_item = apply_filters( |
|
| 68 | - 'FHEE__EEH_Line_Item__add_unrelated_item__line_item', |
|
| 69 | - $line_item, |
|
| 70 | - $parent_line_item |
|
| 71 | - ); |
|
| 72 | - return self::add_item($parent_line_item, $line_item); |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * Adds a simple item ( unrelated to any other model object) to the total line item, |
|
| 78 | - * in the correct spot in the line item tree. Does not automatically |
|
| 79 | - * re-calculate the line item totals, nor update the related transaction, nor upgrade the transaction's |
|
| 80 | - * registrations' final prices (which should probably change because of this). |
|
| 81 | - * You should call recalculate_total_including_taxes() on the grand total line item, then |
|
| 82 | - * update the transaction's total, and EE_Registration_Processor::update_registration_final_prices() |
|
| 83 | - * after using this, to keep the registration final prices in-sync with the transaction's total. |
|
| 84 | - * |
|
| 85 | - * @param EE_Line_Item $parent_line_item |
|
| 86 | - * @param string $name |
|
| 87 | - * @param float $percentage_amount |
|
| 88 | - * @param string $description |
|
| 89 | - * @param boolean $taxable |
|
| 90 | - * @return boolean success |
|
| 91 | - * @throws EE_Error |
|
| 92 | - */ |
|
| 93 | - public static function add_percentage_based_item( |
|
| 94 | - EE_Line_Item $parent_line_item, |
|
| 95 | - $name, |
|
| 96 | - $percentage_amount, |
|
| 97 | - $description = '', |
|
| 98 | - $taxable = false |
|
| 99 | - ) { |
|
| 100 | - $line_item = EE_Line_Item::new_instance(array( |
|
| 101 | - 'LIN_name' => $name, |
|
| 102 | - 'LIN_desc' => $description, |
|
| 103 | - 'LIN_unit_price' => 0, |
|
| 104 | - 'LIN_percent' => $percentage_amount, |
|
| 105 | - 'LIN_quantity' => 1, |
|
| 106 | - 'LIN_is_taxable' => $taxable, |
|
| 107 | - 'LIN_total' => (float) ($percentage_amount * ($parent_line_item->total() / 100)), |
|
| 108 | - 'LIN_type' => EEM_Line_Item::type_line_item, |
|
| 109 | - 'LIN_parent' => $parent_line_item->ID(), |
|
| 110 | - )); |
|
| 111 | - $line_item = apply_filters( |
|
| 112 | - 'FHEE__EEH_Line_Item__add_percentage_based_item__line_item', |
|
| 113 | - $line_item |
|
| 114 | - ); |
|
| 115 | - return $parent_line_item->add_child_line_item($line_item, false); |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * Returns the new line item created by adding a purchase of the ticket |
|
| 121 | - * ensures that ticket line item is saved, and that cart total has been recalculated. |
|
| 122 | - * If this ticket has already been purchased, just increments its count. |
|
| 123 | - * Automatically re-calculates the line item totals and updates the related transaction. But |
|
| 124 | - * DOES NOT automatically upgrade the transaction's registrations' final prices (which |
|
| 125 | - * should probably change because of this). |
|
| 126 | - * You should call EE_Registration_Processor::calculate_reg_final_prices_per_line_item() |
|
| 127 | - * after using this, to keep the registration final prices in-sync with the transaction's total. |
|
| 128 | - * |
|
| 129 | - * @param EE_Line_Item $total_line_item grand total line item of type EEM_Line_Item::type_total |
|
| 130 | - * @param EE_Ticket $ticket |
|
| 131 | - * @param int $qty |
|
| 132 | - * @return EE_Line_Item |
|
| 133 | - * @throws EE_Error |
|
| 134 | - * @throws InvalidArgumentException |
|
| 135 | - * @throws InvalidDataTypeException |
|
| 136 | - * @throws InvalidInterfaceException |
|
| 137 | - * @throws ReflectionException |
|
| 138 | - */ |
|
| 139 | - public static function add_ticket_purchase(EE_Line_Item $total_line_item, EE_Ticket $ticket, $qty = 1) |
|
| 140 | - { |
|
| 141 | - if (! $total_line_item instanceof EE_Line_Item || ! $total_line_item->is_total()) { |
|
| 142 | - throw new EE_Error( |
|
| 143 | - sprintf( |
|
| 144 | - esc_html__( |
|
| 145 | - 'A valid line item total is required in order to add tickets. A line item of type "%s" was passed.', |
|
| 146 | - 'event_espresso' |
|
| 147 | - ), |
|
| 148 | - $ticket->ID(), |
|
| 149 | - $total_line_item->ID() |
|
| 150 | - ) |
|
| 151 | - ); |
|
| 152 | - } |
|
| 153 | - // either increment the qty for an existing ticket |
|
| 154 | - $line_item = self::increment_ticket_qty_if_already_in_cart($total_line_item, $ticket, $qty); |
|
| 155 | - // or add a new one |
|
| 156 | - if (! $line_item instanceof EE_Line_Item) { |
|
| 157 | - $line_item = self::create_ticket_line_item($total_line_item, $ticket, $qty); |
|
| 158 | - } |
|
| 159 | - $total_line_item->recalculate_total_including_taxes(); |
|
| 160 | - return $line_item; |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - |
|
| 164 | - /** |
|
| 165 | - * Returns the new line item created by adding a purchase of the ticket |
|
| 166 | - * |
|
| 167 | - * @param EE_Line_Item $total_line_item |
|
| 168 | - * @param EE_Ticket $ticket |
|
| 169 | - * @param int $qty |
|
| 170 | - * @return EE_Line_Item |
|
| 171 | - * @throws EE_Error |
|
| 172 | - * @throws InvalidArgumentException |
|
| 173 | - * @throws InvalidDataTypeException |
|
| 174 | - * @throws InvalidInterfaceException |
|
| 175 | - * @throws ReflectionException |
|
| 176 | - */ |
|
| 177 | - public static function increment_ticket_qty_if_already_in_cart( |
|
| 178 | - EE_Line_Item $total_line_item, |
|
| 179 | - EE_Ticket $ticket, |
|
| 180 | - $qty = 1 |
|
| 181 | - ) { |
|
| 182 | - $line_item = null; |
|
| 183 | - if ($total_line_item instanceof EE_Line_Item && $total_line_item->is_total()) { |
|
| 184 | - $ticket_line_items = EEH_Line_Item::get_ticket_line_items($total_line_item); |
|
| 185 | - foreach ((array) $ticket_line_items as $ticket_line_item) { |
|
| 186 | - if ($ticket_line_item instanceof EE_Line_Item |
|
| 187 | - && (int) $ticket_line_item->OBJ_ID() === (int) $ticket->ID() |
|
| 188 | - ) { |
|
| 189 | - $line_item = $ticket_line_item; |
|
| 190 | - break; |
|
| 191 | - } |
|
| 192 | - } |
|
| 193 | - } |
|
| 194 | - if ($line_item instanceof EE_Line_Item) { |
|
| 195 | - EEH_Line_Item::increment_quantity($line_item, $qty); |
|
| 196 | - return $line_item; |
|
| 197 | - } |
|
| 198 | - return null; |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - |
|
| 202 | - /** |
|
| 203 | - * Increments the line item and all its children's quantity by $qty (but percent line items are unaffected). |
|
| 204 | - * Does NOT save or recalculate other line items totals |
|
| 205 | - * |
|
| 206 | - * @param EE_Line_Item $line_item |
|
| 207 | - * @param int $qty |
|
| 208 | - * @return void |
|
| 209 | - * @throws EE_Error |
|
| 210 | - * @throws InvalidArgumentException |
|
| 211 | - * @throws InvalidDataTypeException |
|
| 212 | - * @throws InvalidInterfaceException |
|
| 213 | - * @throws ReflectionException |
|
| 214 | - */ |
|
| 215 | - public static function increment_quantity(EE_Line_Item $line_item, $qty = 1) |
|
| 216 | - { |
|
| 217 | - if (! $line_item->is_percent()) { |
|
| 218 | - $qty += $line_item->quantity(); |
|
| 219 | - $line_item->set_quantity($qty); |
|
| 220 | - $line_item->set_total($line_item->unit_price() * $qty); |
|
| 221 | - $line_item->save(); |
|
| 222 | - } |
|
| 223 | - foreach ($line_item->children() as $child) { |
|
| 224 | - if ($child->is_sub_line_item()) { |
|
| 225 | - EEH_Line_Item::update_quantity($child, $qty); |
|
| 226 | - } |
|
| 227 | - } |
|
| 228 | - } |
|
| 229 | - |
|
| 230 | - |
|
| 231 | - /** |
|
| 232 | - * Decrements the line item and all its children's quantity by $qty (but percent line items are unaffected). |
|
| 233 | - * Does NOT save or recalculate other line items totals |
|
| 234 | - * |
|
| 235 | - * @param EE_Line_Item $line_item |
|
| 236 | - * @param int $qty |
|
| 237 | - * @return void |
|
| 238 | - * @throws EE_Error |
|
| 239 | - * @throws InvalidArgumentException |
|
| 240 | - * @throws InvalidDataTypeException |
|
| 241 | - * @throws InvalidInterfaceException |
|
| 242 | - * @throws ReflectionException |
|
| 243 | - */ |
|
| 244 | - public static function decrement_quantity(EE_Line_Item $line_item, $qty = 1) |
|
| 245 | - { |
|
| 246 | - if (! $line_item->is_percent()) { |
|
| 247 | - $qty = $line_item->quantity() - $qty; |
|
| 248 | - $qty = max($qty, 0); |
|
| 249 | - $line_item->set_quantity($qty); |
|
| 250 | - $line_item->set_total($line_item->unit_price() * $qty); |
|
| 251 | - $line_item->save(); |
|
| 252 | - } |
|
| 253 | - foreach ($line_item->children() as $child) { |
|
| 254 | - if ($child->is_sub_line_item()) { |
|
| 255 | - EEH_Line_Item::update_quantity($child, $qty); |
|
| 256 | - } |
|
| 257 | - } |
|
| 258 | - } |
|
| 259 | - |
|
| 260 | - |
|
| 261 | - /** |
|
| 262 | - * Updates the line item and its children's quantities to the specified number. |
|
| 263 | - * Does NOT save them or recalculate totals. |
|
| 264 | - * |
|
| 265 | - * @param EE_Line_Item $line_item |
|
| 266 | - * @param int $new_quantity |
|
| 267 | - * @throws EE_Error |
|
| 268 | - * @throws InvalidArgumentException |
|
| 269 | - * @throws InvalidDataTypeException |
|
| 270 | - * @throws InvalidInterfaceException |
|
| 271 | - * @throws ReflectionException |
|
| 272 | - */ |
|
| 273 | - public static function update_quantity(EE_Line_Item $line_item, $new_quantity) |
|
| 274 | - { |
|
| 275 | - if (! $line_item->is_percent()) { |
|
| 276 | - $line_item->set_quantity($new_quantity); |
|
| 277 | - $line_item->set_total($line_item->unit_price() * $new_quantity); |
|
| 278 | - $line_item->save(); |
|
| 279 | - } |
|
| 280 | - foreach ($line_item->children() as $child) { |
|
| 281 | - if ($child->is_sub_line_item()) { |
|
| 282 | - EEH_Line_Item::update_quantity($child, $new_quantity); |
|
| 283 | - } |
|
| 284 | - } |
|
| 285 | - } |
|
| 286 | - |
|
| 287 | - |
|
| 288 | - /** |
|
| 289 | - * Returns the new line item created by adding a purchase of the ticket |
|
| 290 | - * |
|
| 291 | - * @param EE_Line_Item $total_line_item of type EEM_Line_Item::type_total |
|
| 292 | - * @param EE_Ticket $ticket |
|
| 293 | - * @param int $qty |
|
| 294 | - * @return EE_Line_Item |
|
| 295 | - * @throws EE_Error |
|
| 296 | - * @throws InvalidArgumentException |
|
| 297 | - * @throws InvalidDataTypeException |
|
| 298 | - * @throws InvalidInterfaceException |
|
| 299 | - * @throws ReflectionException |
|
| 300 | - */ |
|
| 301 | - public static function create_ticket_line_item(EE_Line_Item $total_line_item, EE_Ticket $ticket, $qty = 1) |
|
| 302 | - { |
|
| 303 | - $datetimes = $ticket->datetimes(); |
|
| 304 | - $first_datetime = reset($datetimes); |
|
| 305 | - $first_datetime_name = esc_html__('Event', 'event_espresso'); |
|
| 306 | - if ($first_datetime instanceof EE_Datetime && $first_datetime->event() instanceof EE_Event) { |
|
| 307 | - $first_datetime_name = $first_datetime->event()->name(); |
|
| 308 | - } |
|
| 309 | - $event = sprintf(_x('(For %1$s)', '(For Event Name)', 'event_espresso'), $first_datetime_name); |
|
| 310 | - // get event subtotal line |
|
| 311 | - $events_sub_total = self::get_event_line_item_for_ticket($total_line_item, $ticket); |
|
| 312 | - // add $ticket to cart |
|
| 313 | - $line_item = EE_Line_Item::new_instance(array( |
|
| 314 | - 'LIN_name' => $ticket->name(), |
|
| 315 | - 'LIN_desc' => $ticket->description() !== '' ? $ticket->description() . ' ' . $event : $event, |
|
| 316 | - 'LIN_unit_price' => $ticket->price(), |
|
| 317 | - 'LIN_quantity' => $qty, |
|
| 318 | - 'LIN_is_taxable' => $ticket->taxable(), |
|
| 319 | - 'LIN_order' => count($events_sub_total->children()), |
|
| 320 | - 'LIN_total' => $ticket->price() * $qty, |
|
| 321 | - 'LIN_type' => EEM_Line_Item::type_line_item, |
|
| 322 | - 'OBJ_ID' => $ticket->ID(), |
|
| 323 | - 'OBJ_type' => EEM_Line_Item::OBJ_TYPE_TICKET, |
|
| 324 | - )); |
|
| 325 | - $line_item = apply_filters( |
|
| 326 | - 'FHEE__EEH_Line_Item__create_ticket_line_item__line_item', |
|
| 327 | - $line_item |
|
| 328 | - ); |
|
| 329 | - $events_sub_total->add_child_line_item($line_item); |
|
| 330 | - // now add the sub-line items |
|
| 331 | - $running_total_for_ticket = 0; |
|
| 332 | - foreach ($ticket->prices(array('order_by' => array('PRC_order' => 'ASC'))) as $price) { |
|
| 333 | - $sign = $price->is_discount() ? -1 : 1; |
|
| 334 | - $price_total = $price->is_percent() |
|
| 335 | - ? $running_total_for_ticket * $price->amount() / 100 |
|
| 336 | - : $price->amount() * $qty; |
|
| 337 | - $sub_line_item = EE_Line_Item::new_instance(array( |
|
| 338 | - 'LIN_name' => $price->name(), |
|
| 339 | - 'LIN_desc' => $price->desc(), |
|
| 340 | - 'LIN_quantity' => $price->is_percent() ? null : $qty, |
|
| 341 | - 'LIN_is_taxable' => false, |
|
| 342 | - 'LIN_order' => $price->order(), |
|
| 343 | - 'LIN_total' => $sign * $price_total, |
|
| 344 | - 'LIN_type' => EEM_Line_Item::type_sub_line_item, |
|
| 345 | - 'OBJ_ID' => $price->ID(), |
|
| 346 | - 'OBJ_type' => EEM_Line_Item::OBJ_TYPE_PRICE, |
|
| 347 | - )); |
|
| 348 | - $sub_line_item = apply_filters( |
|
| 349 | - 'FHEE__EEH_Line_Item__create_ticket_line_item__sub_line_item', |
|
| 350 | - $sub_line_item |
|
| 351 | - ); |
|
| 352 | - if ($price->is_percent()) { |
|
| 353 | - $sub_line_item->set_percent($sign * $price->amount()); |
|
| 354 | - } else { |
|
| 355 | - $sub_line_item->set_unit_price($sign * $price->amount()); |
|
| 356 | - } |
|
| 357 | - $running_total_for_ticket += $price_total; |
|
| 358 | - $line_item->add_child_line_item($sub_line_item); |
|
| 359 | - } |
|
| 360 | - return $line_item; |
|
| 361 | - } |
|
| 362 | - |
|
| 363 | - |
|
| 364 | - /** |
|
| 365 | - * Adds the specified item under the pre-tax-sub-total line item. Automatically |
|
| 366 | - * re-calculates the line item totals and updates the related transaction. But |
|
| 367 | - * DOES NOT automatically upgrade the transaction's registrations' final prices (which |
|
| 368 | - * should probably change because of this). |
|
| 369 | - * You should call EE_Registration_Processor::calculate_reg_final_prices_per_line_item() |
|
| 370 | - * after using this, to keep the registration final prices in-sync with the transaction's total. |
|
| 371 | - * |
|
| 372 | - * @param EE_Line_Item $total_line_item |
|
| 373 | - * @param EE_Line_Item $item to be added |
|
| 374 | - * @return boolean |
|
| 375 | - * @throws EE_Error |
|
| 376 | - * @throws InvalidArgumentException |
|
| 377 | - * @throws InvalidDataTypeException |
|
| 378 | - * @throws InvalidInterfaceException |
|
| 379 | - * @throws ReflectionException |
|
| 380 | - */ |
|
| 381 | - public static function add_item(EE_Line_Item $total_line_item, EE_Line_Item $item) |
|
| 382 | - { |
|
| 383 | - $pre_tax_subtotal = self::get_pre_tax_subtotal($total_line_item); |
|
| 384 | - if ($pre_tax_subtotal instanceof EE_Line_Item) { |
|
| 385 | - $success = $pre_tax_subtotal->add_child_line_item($item); |
|
| 386 | - } else { |
|
| 387 | - return false; |
|
| 388 | - } |
|
| 389 | - $total_line_item->recalculate_total_including_taxes(); |
|
| 390 | - return $success; |
|
| 391 | - } |
|
| 392 | - |
|
| 393 | - |
|
| 394 | - /** |
|
| 395 | - * cancels an existing ticket line item, |
|
| 396 | - * by decrementing it's quantity by 1 and adding a new "type_cancellation" sub-line-item. |
|
| 397 | - * ALL totals and subtotals will NEED TO BE UPDATED after performing this action |
|
| 398 | - * |
|
| 399 | - * @param EE_Line_Item $ticket_line_item |
|
| 400 | - * @param int $qty |
|
| 401 | - * @return bool success |
|
| 402 | - * @throws EE_Error |
|
| 403 | - * @throws InvalidArgumentException |
|
| 404 | - * @throws InvalidDataTypeException |
|
| 405 | - * @throws InvalidInterfaceException |
|
| 406 | - * @throws ReflectionException |
|
| 407 | - */ |
|
| 408 | - public static function cancel_ticket_line_item(EE_Line_Item $ticket_line_item, $qty = 1) |
|
| 409 | - { |
|
| 410 | - // validate incoming line_item |
|
| 411 | - if ($ticket_line_item->OBJ_type() !== EEM_Line_Item::OBJ_TYPE_TICKET) { |
|
| 412 | - throw new EE_Error( |
|
| 413 | - sprintf( |
|
| 414 | - esc_html__( |
|
| 415 | - 'The supplied line item must have an Object Type of "Ticket", not %1$s.', |
|
| 416 | - 'event_espresso' |
|
| 417 | - ), |
|
| 418 | - $ticket_line_item->type() |
|
| 419 | - ) |
|
| 420 | - ); |
|
| 421 | - } |
|
| 422 | - if ($ticket_line_item->quantity() < $qty) { |
|
| 423 | - throw new EE_Error( |
|
| 424 | - sprintf( |
|
| 425 | - esc_html__( |
|
| 426 | - 'Can not cancel %1$d ticket(s) because the supplied line item has a quantity of %2$d.', |
|
| 427 | - 'event_espresso' |
|
| 428 | - ), |
|
| 429 | - $qty, |
|
| 430 | - $ticket_line_item->quantity() |
|
| 431 | - ) |
|
| 432 | - ); |
|
| 433 | - } |
|
| 434 | - // decrement ticket quantity; don't rely on auto-fixing when recalculating totals to do this |
|
| 435 | - $ticket_line_item->set_quantity($ticket_line_item->quantity() - $qty); |
|
| 436 | - foreach ($ticket_line_item->children() as $child_line_item) { |
|
| 437 | - if ($child_line_item->is_sub_line_item() |
|
| 438 | - && ! $child_line_item->is_percent() |
|
| 439 | - && ! $child_line_item->is_cancellation() |
|
| 440 | - ) { |
|
| 441 | - $child_line_item->set_quantity($child_line_item->quantity() - $qty); |
|
| 442 | - } |
|
| 443 | - } |
|
| 444 | - // get cancellation sub line item |
|
| 445 | - $cancellation_line_item = EEH_Line_Item::get_descendants_of_type( |
|
| 446 | - $ticket_line_item, |
|
| 447 | - EEM_Line_Item::type_cancellation |
|
| 448 | - ); |
|
| 449 | - $cancellation_line_item = reset($cancellation_line_item); |
|
| 450 | - // verify that this ticket was indeed previously cancelled |
|
| 451 | - if ($cancellation_line_item instanceof EE_Line_Item) { |
|
| 452 | - // increment cancelled quantity |
|
| 453 | - $cancellation_line_item->set_quantity($cancellation_line_item->quantity() + $qty); |
|
| 454 | - } else { |
|
| 455 | - // create cancellation sub line item |
|
| 456 | - $cancellation_line_item = EE_Line_Item::new_instance(array( |
|
| 457 | - 'LIN_name' => esc_html__('Cancellation', 'event_espresso'), |
|
| 458 | - 'LIN_desc' => sprintf( |
|
| 459 | - esc_html_x( |
|
| 460 | - 'Cancelled %1$s : %2$s', |
|
| 461 | - 'Cancelled Ticket Name : 2015-01-01 11:11', |
|
| 462 | - 'event_espresso' |
|
| 463 | - ), |
|
| 464 | - $ticket_line_item->name(), |
|
| 465 | - current_time(get_option('date_format') . ' ' . get_option('time_format')) |
|
| 466 | - ), |
|
| 467 | - 'LIN_unit_price' => 0, // $ticket_line_item->unit_price() |
|
| 468 | - 'LIN_quantity' => $qty, |
|
| 469 | - 'LIN_is_taxable' => $ticket_line_item->is_taxable(), |
|
| 470 | - 'LIN_order' => count($ticket_line_item->children()), |
|
| 471 | - 'LIN_total' => 0, // $ticket_line_item->unit_price() |
|
| 472 | - 'LIN_type' => EEM_Line_Item::type_cancellation, |
|
| 473 | - )); |
|
| 474 | - $ticket_line_item->add_child_line_item($cancellation_line_item); |
|
| 475 | - } |
|
| 476 | - if ($ticket_line_item->save_this_and_descendants() > 0) { |
|
| 477 | - // decrement parent line item quantity |
|
| 478 | - $event_line_item = $ticket_line_item->parent(); |
|
| 479 | - if ($event_line_item instanceof EE_Line_Item |
|
| 480 | - && $event_line_item->OBJ_type() === EEM_Line_Item::OBJ_TYPE_EVENT |
|
| 481 | - ) { |
|
| 482 | - $event_line_item->set_quantity($event_line_item->quantity() - $qty); |
|
| 483 | - $event_line_item->save(); |
|
| 484 | - } |
|
| 485 | - EEH_Line_Item::get_grand_total_and_recalculate_everything($ticket_line_item); |
|
| 486 | - return true; |
|
| 487 | - } |
|
| 488 | - return false; |
|
| 489 | - } |
|
| 490 | - |
|
| 491 | - |
|
| 492 | - /** |
|
| 493 | - * reinstates (un-cancels?) a previously canceled ticket line item, |
|
| 494 | - * by incrementing it's quantity by 1, and decrementing it's "type_cancellation" sub-line-item. |
|
| 495 | - * ALL totals and subtotals will NEED TO BE UPDATED after performing this action |
|
| 496 | - * |
|
| 497 | - * @param EE_Line_Item $ticket_line_item |
|
| 498 | - * @param int $qty |
|
| 499 | - * @return bool success |
|
| 500 | - * @throws EE_Error |
|
| 501 | - * @throws InvalidArgumentException |
|
| 502 | - * @throws InvalidDataTypeException |
|
| 503 | - * @throws InvalidInterfaceException |
|
| 504 | - * @throws ReflectionException |
|
| 505 | - */ |
|
| 506 | - public static function reinstate_canceled_ticket_line_item(EE_Line_Item $ticket_line_item, $qty = 1) |
|
| 507 | - { |
|
| 508 | - // validate incoming line_item |
|
| 509 | - if ($ticket_line_item->OBJ_type() !== EEM_Line_Item::OBJ_TYPE_TICKET) { |
|
| 510 | - throw new EE_Error( |
|
| 511 | - sprintf( |
|
| 512 | - esc_html__( |
|
| 513 | - 'The supplied line item must have an Object Type of "Ticket", not %1$s.', |
|
| 514 | - 'event_espresso' |
|
| 515 | - ), |
|
| 516 | - $ticket_line_item->type() |
|
| 517 | - ) |
|
| 518 | - ); |
|
| 519 | - } |
|
| 520 | - // get cancellation sub line item |
|
| 521 | - $cancellation_line_item = EEH_Line_Item::get_descendants_of_type( |
|
| 522 | - $ticket_line_item, |
|
| 523 | - EEM_Line_Item::type_cancellation |
|
| 524 | - ); |
|
| 525 | - $cancellation_line_item = reset($cancellation_line_item); |
|
| 526 | - // verify that this ticket was indeed previously cancelled |
|
| 527 | - if (! $cancellation_line_item instanceof EE_Line_Item) { |
|
| 528 | - return false; |
|
| 529 | - } |
|
| 530 | - if ($cancellation_line_item->quantity() > $qty) { |
|
| 531 | - // decrement cancelled quantity |
|
| 532 | - $cancellation_line_item->set_quantity($cancellation_line_item->quantity() - $qty); |
|
| 533 | - } elseif ($cancellation_line_item->quantity() === $qty) { |
|
| 534 | - // decrement cancelled quantity in case anyone still has the object kicking around |
|
| 535 | - $cancellation_line_item->set_quantity($cancellation_line_item->quantity() - $qty); |
|
| 536 | - // delete because quantity will end up as 0 |
|
| 537 | - $cancellation_line_item->delete(); |
|
| 538 | - // and attempt to destroy the object, |
|
| 539 | - // even though PHP won't actually destroy it until it needs the memory |
|
| 540 | - unset($cancellation_line_item); |
|
| 541 | - } else { |
|
| 542 | - // what ?!?! negative quantity ?!?! |
|
| 543 | - throw new EE_Error( |
|
| 544 | - sprintf( |
|
| 545 | - esc_html__( |
|
| 546 | - 'Can not reinstate %1$d cancelled ticket(s) because the cancelled ticket quantity is only %2$d.', |
|
| 547 | - 'event_espresso' |
|
| 548 | - ), |
|
| 549 | - $qty, |
|
| 550 | - $cancellation_line_item->quantity() |
|
| 551 | - ) |
|
| 552 | - ); |
|
| 553 | - } |
|
| 554 | - // increment ticket quantity |
|
| 555 | - $ticket_line_item->set_quantity($ticket_line_item->quantity() + $qty); |
|
| 556 | - if ($ticket_line_item->save_this_and_descendants() > 0) { |
|
| 557 | - // increment parent line item quantity |
|
| 558 | - $event_line_item = $ticket_line_item->parent(); |
|
| 559 | - if ($event_line_item instanceof EE_Line_Item |
|
| 560 | - && $event_line_item->OBJ_type() === EEM_Line_Item::OBJ_TYPE_EVENT |
|
| 561 | - ) { |
|
| 562 | - $event_line_item->set_quantity($event_line_item->quantity() + $qty); |
|
| 563 | - } |
|
| 564 | - EEH_Line_Item::get_grand_total_and_recalculate_everything($ticket_line_item); |
|
| 565 | - return true; |
|
| 566 | - } |
|
| 567 | - return false; |
|
| 568 | - } |
|
| 569 | - |
|
| 570 | - |
|
| 571 | - /** |
|
| 572 | - * calls EEH_Line_Item::find_transaction_grand_total_for_line_item() |
|
| 573 | - * then EE_Line_Item::recalculate_total_including_taxes() on the result |
|
| 574 | - * |
|
| 575 | - * @param EE_Line_Item $line_item |
|
| 576 | - * @return float |
|
| 577 | - * @throws EE_Error |
|
| 578 | - * @throws InvalidArgumentException |
|
| 579 | - * @throws InvalidDataTypeException |
|
| 580 | - * @throws InvalidInterfaceException |
|
| 581 | - * @throws ReflectionException |
|
| 582 | - */ |
|
| 583 | - public static function get_grand_total_and_recalculate_everything(EE_Line_Item $line_item) |
|
| 584 | - { |
|
| 585 | - $grand_total_line_item = EEH_Line_Item::find_transaction_grand_total_for_line_item($line_item); |
|
| 586 | - return $grand_total_line_item->recalculate_total_including_taxes(); |
|
| 587 | - } |
|
| 588 | - |
|
| 589 | - |
|
| 590 | - /** |
|
| 591 | - * Gets the line item which contains the subtotal of all the items |
|
| 592 | - * |
|
| 593 | - * @param EE_Line_Item $total_line_item of type EEM_Line_Item::type_total |
|
| 594 | - * @return EE_Line_Item |
|
| 595 | - * @throws EE_Error |
|
| 596 | - * @throws InvalidArgumentException |
|
| 597 | - * @throws InvalidDataTypeException |
|
| 598 | - * @throws InvalidInterfaceException |
|
| 599 | - * @throws ReflectionException |
|
| 600 | - */ |
|
| 601 | - public static function get_pre_tax_subtotal(EE_Line_Item $total_line_item) |
|
| 602 | - { |
|
| 603 | - $pre_tax_subtotal = $total_line_item->get_child_line_item('pre-tax-subtotal'); |
|
| 604 | - return $pre_tax_subtotal instanceof EE_Line_Item |
|
| 605 | - ? $pre_tax_subtotal |
|
| 606 | - : self::create_pre_tax_subtotal($total_line_item); |
|
| 607 | - } |
|
| 608 | - |
|
| 609 | - |
|
| 610 | - /** |
|
| 611 | - * Gets the line item for the taxes subtotal |
|
| 612 | - * |
|
| 613 | - * @param EE_Line_Item $total_line_item of type EEM_Line_Item::type_total |
|
| 614 | - * @return EE_Line_Item |
|
| 615 | - * @throws EE_Error |
|
| 616 | - * @throws InvalidArgumentException |
|
| 617 | - * @throws InvalidDataTypeException |
|
| 618 | - * @throws InvalidInterfaceException |
|
| 619 | - * @throws ReflectionException |
|
| 620 | - */ |
|
| 621 | - public static function get_taxes_subtotal(EE_Line_Item $total_line_item) |
|
| 622 | - { |
|
| 623 | - $taxes = $total_line_item->get_child_line_item('taxes'); |
|
| 624 | - return $taxes ? $taxes : self::create_taxes_subtotal($total_line_item); |
|
| 625 | - } |
|
| 626 | - |
|
| 627 | - |
|
| 628 | - /** |
|
| 629 | - * sets the TXN ID on an EE_Line_Item if passed a valid EE_Transaction object |
|
| 630 | - * |
|
| 631 | - * @param EE_Line_Item $line_item |
|
| 632 | - * @param EE_Transaction $transaction |
|
| 633 | - * @return void |
|
| 634 | - * @throws EE_Error |
|
| 635 | - * @throws InvalidArgumentException |
|
| 636 | - * @throws InvalidDataTypeException |
|
| 637 | - * @throws InvalidInterfaceException |
|
| 638 | - * @throws ReflectionException |
|
| 639 | - */ |
|
| 640 | - public static function set_TXN_ID(EE_Line_Item $line_item, $transaction = null) |
|
| 641 | - { |
|
| 642 | - if ($transaction) { |
|
| 643 | - /** @type EEM_Transaction $EEM_Transaction */ |
|
| 644 | - $EEM_Transaction = EE_Registry::instance()->load_model('Transaction'); |
|
| 645 | - $TXN_ID = $EEM_Transaction->ensure_is_ID($transaction); |
|
| 646 | - $line_item->set_TXN_ID($TXN_ID); |
|
| 647 | - } |
|
| 648 | - } |
|
| 649 | - |
|
| 650 | - |
|
| 651 | - /** |
|
| 652 | - * Creates a new default total line item for the transaction, |
|
| 653 | - * and its tickets subtotal and taxes subtotal line items (and adds the |
|
| 654 | - * existing taxes as children of the taxes subtotal line item) |
|
| 655 | - * |
|
| 656 | - * @param EE_Transaction $transaction |
|
| 657 | - * @return EE_Line_Item of type total |
|
| 658 | - * @throws EE_Error |
|
| 659 | - * @throws InvalidArgumentException |
|
| 660 | - * @throws InvalidDataTypeException |
|
| 661 | - * @throws InvalidInterfaceException |
|
| 662 | - * @throws ReflectionException |
|
| 663 | - */ |
|
| 664 | - public static function create_total_line_item($transaction = null) |
|
| 665 | - { |
|
| 666 | - $total_line_item = EE_Line_Item::new_instance(array( |
|
| 667 | - 'LIN_code' => 'total', |
|
| 668 | - 'LIN_name' => esc_html__('Grand Total', 'event_espresso'), |
|
| 669 | - 'LIN_type' => EEM_Line_Item::type_total, |
|
| 670 | - 'OBJ_type' => EEM_Line_Item::OBJ_TYPE_TRANSACTION, |
|
| 671 | - )); |
|
| 672 | - $total_line_item = apply_filters( |
|
| 673 | - 'FHEE__EEH_Line_Item__create_total_line_item__total_line_item', |
|
| 674 | - $total_line_item |
|
| 675 | - ); |
|
| 676 | - self::set_TXN_ID($total_line_item, $transaction); |
|
| 677 | - self::create_pre_tax_subtotal($total_line_item, $transaction); |
|
| 678 | - self::create_taxes_subtotal($total_line_item, $transaction); |
|
| 679 | - return $total_line_item; |
|
| 680 | - } |
|
| 681 | - |
|
| 682 | - |
|
| 683 | - /** |
|
| 684 | - * Creates a default items subtotal line item |
|
| 685 | - * |
|
| 686 | - * @param EE_Line_Item $total_line_item |
|
| 687 | - * @param EE_Transaction $transaction |
|
| 688 | - * @return EE_Line_Item |
|
| 689 | - * @throws EE_Error |
|
| 690 | - * @throws InvalidArgumentException |
|
| 691 | - * @throws InvalidDataTypeException |
|
| 692 | - * @throws InvalidInterfaceException |
|
| 693 | - * @throws ReflectionException |
|
| 694 | - */ |
|
| 695 | - protected static function create_pre_tax_subtotal(EE_Line_Item $total_line_item, $transaction = null) |
|
| 696 | - { |
|
| 697 | - $pre_tax_line_item = EE_Line_Item::new_instance(array( |
|
| 698 | - 'LIN_code' => 'pre-tax-subtotal', |
|
| 699 | - 'LIN_name' => esc_html__('Pre-Tax Subtotal', 'event_espresso'), |
|
| 700 | - 'LIN_type' => EEM_Line_Item::type_sub_total, |
|
| 701 | - )); |
|
| 702 | - $pre_tax_line_item = apply_filters( |
|
| 703 | - 'FHEE__EEH_Line_Item__create_pre_tax_subtotal__pre_tax_line_item', |
|
| 704 | - $pre_tax_line_item |
|
| 705 | - ); |
|
| 706 | - self::set_TXN_ID($pre_tax_line_item, $transaction); |
|
| 707 | - $total_line_item->add_child_line_item($pre_tax_line_item); |
|
| 708 | - self::create_event_subtotal($pre_tax_line_item, $transaction); |
|
| 709 | - return $pre_tax_line_item; |
|
| 710 | - } |
|
| 711 | - |
|
| 712 | - |
|
| 713 | - /** |
|
| 714 | - * Creates a line item for the taxes subtotal and finds all the tax prices |
|
| 715 | - * and applies taxes to it |
|
| 716 | - * |
|
| 717 | - * @param EE_Line_Item $total_line_item of type EEM_Line_Item::type_total |
|
| 718 | - * @param EE_Transaction $transaction |
|
| 719 | - * @return EE_Line_Item |
|
| 720 | - * @throws EE_Error |
|
| 721 | - * @throws InvalidArgumentException |
|
| 722 | - * @throws InvalidDataTypeException |
|
| 723 | - * @throws InvalidInterfaceException |
|
| 724 | - * @throws ReflectionException |
|
| 725 | - */ |
|
| 726 | - protected static function create_taxes_subtotal(EE_Line_Item $total_line_item, $transaction = null) |
|
| 727 | - { |
|
| 728 | - $tax_line_item = EE_Line_Item::new_instance(array( |
|
| 729 | - 'LIN_code' => 'taxes', |
|
| 730 | - 'LIN_name' => esc_html__('Taxes', 'event_espresso'), |
|
| 731 | - 'LIN_type' => EEM_Line_Item::type_tax_sub_total, |
|
| 732 | - 'LIN_order' => 1000,// this should always come last |
|
| 733 | - )); |
|
| 734 | - $tax_line_item = apply_filters( |
|
| 735 | - 'FHEE__EEH_Line_Item__create_taxes_subtotal__tax_line_item', |
|
| 736 | - $tax_line_item |
|
| 737 | - ); |
|
| 738 | - self::set_TXN_ID($tax_line_item, $transaction); |
|
| 739 | - $total_line_item->add_child_line_item($tax_line_item); |
|
| 740 | - // and lastly, add the actual taxes |
|
| 741 | - self::apply_taxes($total_line_item); |
|
| 742 | - return $tax_line_item; |
|
| 743 | - } |
|
| 744 | - |
|
| 745 | - |
|
| 746 | - /** |
|
| 747 | - * Creates a default items subtotal line item |
|
| 748 | - * |
|
| 749 | - * @param EE_Line_Item $pre_tax_line_item |
|
| 750 | - * @param EE_Transaction $transaction |
|
| 751 | - * @param EE_Event $event |
|
| 752 | - * @return EE_Line_Item |
|
| 753 | - * @throws EE_Error |
|
| 754 | - * @throws InvalidArgumentException |
|
| 755 | - * @throws InvalidDataTypeException |
|
| 756 | - * @throws InvalidInterfaceException |
|
| 757 | - * @throws ReflectionException |
|
| 758 | - */ |
|
| 759 | - public static function create_event_subtotal(EE_Line_Item $pre_tax_line_item, $transaction = null, $event = null) |
|
| 760 | - { |
|
| 761 | - $event_line_item = EE_Line_Item::new_instance(array( |
|
| 762 | - 'LIN_code' => self::get_event_code($event), |
|
| 763 | - 'LIN_name' => self::get_event_name($event), |
|
| 764 | - 'LIN_desc' => self::get_event_desc($event), |
|
| 765 | - 'LIN_type' => EEM_Line_Item::type_sub_total, |
|
| 766 | - 'OBJ_type' => EEM_Line_Item::OBJ_TYPE_EVENT, |
|
| 767 | - 'OBJ_ID' => $event instanceof EE_Event ? $event->ID() : 0, |
|
| 768 | - )); |
|
| 769 | - $event_line_item = apply_filters( |
|
| 770 | - 'FHEE__EEH_Line_Item__create_event_subtotal__event_line_item', |
|
| 771 | - $event_line_item |
|
| 772 | - ); |
|
| 773 | - self::set_TXN_ID($event_line_item, $transaction); |
|
| 774 | - $pre_tax_line_item->add_child_line_item($event_line_item); |
|
| 775 | - return $event_line_item; |
|
| 776 | - } |
|
| 777 | - |
|
| 778 | - |
|
| 779 | - /** |
|
| 780 | - * Gets what the event ticket's code SHOULD be |
|
| 781 | - * |
|
| 782 | - * @param EE_Event $event |
|
| 783 | - * @return string |
|
| 784 | - * @throws EE_Error |
|
| 785 | - */ |
|
| 786 | - public static function get_event_code($event) |
|
| 787 | - { |
|
| 788 | - return 'event-' . ($event instanceof EE_Event ? $event->ID() : '0'); |
|
| 789 | - } |
|
| 790 | - |
|
| 791 | - |
|
| 792 | - /** |
|
| 793 | - * Gets the event name |
|
| 794 | - * |
|
| 795 | - * @param EE_Event $event |
|
| 796 | - * @return string |
|
| 797 | - * @throws EE_Error |
|
| 798 | - */ |
|
| 799 | - public static function get_event_name($event) |
|
| 800 | - { |
|
| 801 | - return $event instanceof EE_Event |
|
| 802 | - ? mb_substr($event->name(), 0, 245) |
|
| 803 | - : esc_html__('Event', 'event_espresso'); |
|
| 804 | - } |
|
| 805 | - |
|
| 806 | - |
|
| 807 | - /** |
|
| 808 | - * Gets the event excerpt |
|
| 809 | - * |
|
| 810 | - * @param EE_Event $event |
|
| 811 | - * @return string |
|
| 812 | - * @throws EE_Error |
|
| 813 | - */ |
|
| 814 | - public static function get_event_desc($event) |
|
| 815 | - { |
|
| 816 | - return $event instanceof EE_Event ? $event->short_description() : ''; |
|
| 817 | - } |
|
| 818 | - |
|
| 819 | - |
|
| 820 | - /** |
|
| 821 | - * Given the grand total line item and a ticket, finds the event sub-total |
|
| 822 | - * line item the ticket's purchase should be added onto |
|
| 823 | - * |
|
| 824 | - * @access public |
|
| 825 | - * @param EE_Line_Item $grand_total the grand total line item |
|
| 826 | - * @param EE_Ticket $ticket |
|
| 827 | - * @return EE_Line_Item |
|
| 828 | - * @throws EE_Error |
|
| 829 | - * @throws InvalidArgumentException |
|
| 830 | - * @throws InvalidDataTypeException |
|
| 831 | - * @throws InvalidInterfaceException |
|
| 832 | - * @throws ReflectionException |
|
| 833 | - */ |
|
| 834 | - public static function get_event_line_item_for_ticket(EE_Line_Item $grand_total, EE_Ticket $ticket) |
|
| 835 | - { |
|
| 836 | - $first_datetime = $ticket->first_datetime(); |
|
| 837 | - if (! $first_datetime instanceof EE_Datetime) { |
|
| 838 | - throw new EE_Error( |
|
| 839 | - sprintf( |
|
| 840 | - __('The supplied ticket (ID %d) has no datetimes', 'event_espresso'), |
|
| 841 | - $ticket->ID() |
|
| 842 | - ) |
|
| 843 | - ); |
|
| 844 | - } |
|
| 845 | - $event = $first_datetime->event(); |
|
| 846 | - if (! $event instanceof EE_Event) { |
|
| 847 | - throw new EE_Error( |
|
| 848 | - sprintf( |
|
| 849 | - esc_html__( |
|
| 850 | - 'The supplied ticket (ID %d) has no event data associated with it.', |
|
| 851 | - 'event_espresso' |
|
| 852 | - ), |
|
| 853 | - $ticket->ID() |
|
| 854 | - ) |
|
| 855 | - ); |
|
| 856 | - } |
|
| 857 | - $events_sub_total = EEH_Line_Item::get_event_line_item($grand_total, $event); |
|
| 858 | - if (! $events_sub_total instanceof EE_Line_Item) { |
|
| 859 | - throw new EE_Error( |
|
| 860 | - sprintf( |
|
| 861 | - esc_html__( |
|
| 862 | - 'There is no events sub-total for ticket %s on total line item %d', |
|
| 863 | - 'event_espresso' |
|
| 864 | - ), |
|
| 865 | - $ticket->ID(), |
|
| 866 | - $grand_total->ID() |
|
| 867 | - ) |
|
| 868 | - ); |
|
| 869 | - } |
|
| 870 | - return $events_sub_total; |
|
| 871 | - } |
|
| 872 | - |
|
| 873 | - |
|
| 874 | - /** |
|
| 875 | - * Gets the event line item |
|
| 876 | - * |
|
| 877 | - * @param EE_Line_Item $grand_total |
|
| 878 | - * @param EE_Event $event |
|
| 879 | - * @return EE_Line_Item for the event subtotal which is a child of $grand_total |
|
| 880 | - * @throws EE_Error |
|
| 881 | - * @throws InvalidArgumentException |
|
| 882 | - * @throws InvalidDataTypeException |
|
| 883 | - * @throws InvalidInterfaceException |
|
| 884 | - * @throws ReflectionException |
|
| 885 | - */ |
|
| 886 | - public static function get_event_line_item(EE_Line_Item $grand_total, $event) |
|
| 887 | - { |
|
| 888 | - /** @type EE_Event $event */ |
|
| 889 | - $event = EEM_Event::instance()->ensure_is_obj($event, true); |
|
| 890 | - $event_line_item = null; |
|
| 891 | - $found = false; |
|
| 892 | - foreach (EEH_Line_Item::get_event_subtotals($grand_total) as $event_line_item) { |
|
| 893 | - // default event subtotal, we should only ever find this the first time this method is called |
|
| 894 | - if (! $event_line_item->OBJ_ID()) { |
|
| 895 | - // let's use this! but first... set the event details |
|
| 896 | - EEH_Line_Item::set_event_subtotal_details($event_line_item, $event); |
|
| 897 | - $found = true; |
|
| 898 | - break; |
|
| 899 | - } |
|
| 900 | - if ($event_line_item->OBJ_ID() === $event->ID()) { |
|
| 901 | - // found existing line item for this event in the cart, so break out of loop and use this one |
|
| 902 | - $found = true; |
|
| 903 | - break; |
|
| 904 | - } |
|
| 905 | - } |
|
| 906 | - if (! $found) { |
|
| 907 | - // there is no event sub-total yet, so add it |
|
| 908 | - $pre_tax_subtotal = EEH_Line_Item::get_pre_tax_subtotal($grand_total); |
|
| 909 | - // create a new "event" subtotal below that |
|
| 910 | - $event_line_item = EEH_Line_Item::create_event_subtotal($pre_tax_subtotal, null, $event); |
|
| 911 | - // and set the event details |
|
| 912 | - EEH_Line_Item::set_event_subtotal_details($event_line_item, $event); |
|
| 913 | - } |
|
| 914 | - return $event_line_item; |
|
| 915 | - } |
|
| 916 | - |
|
| 917 | - |
|
| 918 | - /** |
|
| 919 | - * Creates a default items subtotal line item |
|
| 920 | - * |
|
| 921 | - * @param EE_Line_Item $event_line_item |
|
| 922 | - * @param EE_Event $event |
|
| 923 | - * @param EE_Transaction $transaction |
|
| 924 | - * @return void |
|
| 925 | - * @throws EE_Error |
|
| 926 | - * @throws InvalidArgumentException |
|
| 927 | - * @throws InvalidDataTypeException |
|
| 928 | - * @throws InvalidInterfaceException |
|
| 929 | - * @throws ReflectionException |
|
| 930 | - */ |
|
| 931 | - public static function set_event_subtotal_details( |
|
| 932 | - EE_Line_Item $event_line_item, |
|
| 933 | - EE_Event $event, |
|
| 934 | - $transaction = null |
|
| 935 | - ) { |
|
| 936 | - if ($event instanceof EE_Event) { |
|
| 937 | - $event_line_item->set_code(self::get_event_code($event)); |
|
| 938 | - $event_line_item->set_name(self::get_event_name($event)); |
|
| 939 | - $event_line_item->set_desc(self::get_event_desc($event)); |
|
| 940 | - $event_line_item->set_OBJ_ID($event->ID()); |
|
| 941 | - } |
|
| 942 | - self::set_TXN_ID($event_line_item, $transaction); |
|
| 943 | - } |
|
| 944 | - |
|
| 945 | - |
|
| 946 | - /** |
|
| 947 | - * Finds what taxes should apply, adds them as tax line items under the taxes sub-total, |
|
| 948 | - * and recalculates the taxes sub-total and the grand total. Resets the taxes, so |
|
| 949 | - * any old taxes are removed |
|
| 950 | - * |
|
| 951 | - * @param EE_Line_Item $total_line_item of type EEM_Line_Item::type_total |
|
| 952 | - * @param bool $update_txn_status |
|
| 953 | - * @return bool |
|
| 954 | - * @throws EE_Error |
|
| 955 | - * @throws InvalidArgumentException |
|
| 956 | - * @throws InvalidDataTypeException |
|
| 957 | - * @throws InvalidInterfaceException |
|
| 958 | - * @throws ReflectionException |
|
| 959 | - * @throws RuntimeException |
|
| 960 | - */ |
|
| 961 | - public static function apply_taxes(EE_Line_Item $total_line_item, $update_txn_status = false) |
|
| 962 | - { |
|
| 963 | - /** @type EEM_Price $EEM_Price */ |
|
| 964 | - $EEM_Price = EE_Registry::instance()->load_model('Price'); |
|
| 965 | - // get array of taxes via Price Model |
|
| 966 | - $ordered_taxes = $EEM_Price->get_all_prices_that_are_taxes(); |
|
| 967 | - ksort($ordered_taxes); |
|
| 968 | - $taxes_line_item = self::get_taxes_subtotal($total_line_item); |
|
| 969 | - // just to be safe, remove its old tax line items |
|
| 970 | - $deleted = $taxes_line_item->delete_children_line_items(); |
|
| 971 | - $updates = false; |
|
| 972 | - // loop thru taxes |
|
| 973 | - foreach ($ordered_taxes as $order => $taxes) { |
|
| 974 | - foreach ($taxes as $tax) { |
|
| 975 | - if ($tax instanceof EE_Price) { |
|
| 976 | - $tax_line_item = EE_Line_Item::new_instance( |
|
| 977 | - array( |
|
| 978 | - 'LIN_name' => $tax->name(), |
|
| 979 | - 'LIN_desc' => $tax->desc(), |
|
| 980 | - 'LIN_percent' => $tax->amount(), |
|
| 981 | - 'LIN_is_taxable' => false, |
|
| 982 | - 'LIN_order' => $order, |
|
| 983 | - 'LIN_total' => 0, |
|
| 984 | - 'LIN_type' => EEM_Line_Item::type_tax, |
|
| 985 | - 'OBJ_type' => EEM_Line_Item::OBJ_TYPE_PRICE, |
|
| 986 | - 'OBJ_ID' => $tax->ID(), |
|
| 987 | - ) |
|
| 988 | - ); |
|
| 989 | - $tax_line_item = apply_filters( |
|
| 990 | - 'FHEE__EEH_Line_Item__apply_taxes__tax_line_item', |
|
| 991 | - $tax_line_item |
|
| 992 | - ); |
|
| 993 | - $updates = $taxes_line_item->add_child_line_item($tax_line_item) ? |
|
| 994 | - true : |
|
| 995 | - $updates; |
|
| 996 | - } |
|
| 997 | - } |
|
| 998 | - } |
|
| 999 | - // only recalculate totals if something changed |
|
| 1000 | - if ($deleted || $updates) { |
|
| 1001 | - $total_line_item->recalculate_total_including_taxes($update_txn_status); |
|
| 1002 | - return true; |
|
| 1003 | - } |
|
| 1004 | - return false; |
|
| 1005 | - } |
|
| 1006 | - |
|
| 1007 | - |
|
| 1008 | - /** |
|
| 1009 | - * Ensures that taxes have been applied to the order, if not applies them. |
|
| 1010 | - * Returns the total amount of tax |
|
| 1011 | - * |
|
| 1012 | - * @param EE_Line_Item $total_line_item of type EEM_Line_Item::type_total |
|
| 1013 | - * @return float |
|
| 1014 | - * @throws EE_Error |
|
| 1015 | - * @throws InvalidArgumentException |
|
| 1016 | - * @throws InvalidDataTypeException |
|
| 1017 | - * @throws InvalidInterfaceException |
|
| 1018 | - * @throws ReflectionException |
|
| 1019 | - */ |
|
| 1020 | - public static function ensure_taxes_applied($total_line_item) |
|
| 1021 | - { |
|
| 1022 | - $taxes_subtotal = self::get_taxes_subtotal($total_line_item); |
|
| 1023 | - if (! $taxes_subtotal->children()) { |
|
| 1024 | - self::apply_taxes($total_line_item); |
|
| 1025 | - } |
|
| 1026 | - return $taxes_subtotal->total(); |
|
| 1027 | - } |
|
| 1028 | - |
|
| 1029 | - |
|
| 1030 | - /** |
|
| 1031 | - * Deletes ALL children of the passed line item |
|
| 1032 | - * |
|
| 1033 | - * @param EE_Line_Item $parent_line_item |
|
| 1034 | - * @return bool |
|
| 1035 | - * @throws EE_Error |
|
| 1036 | - * @throws InvalidArgumentException |
|
| 1037 | - * @throws InvalidDataTypeException |
|
| 1038 | - * @throws InvalidInterfaceException |
|
| 1039 | - * @throws ReflectionException |
|
| 1040 | - */ |
|
| 1041 | - public static function delete_all_child_items(EE_Line_Item $parent_line_item) |
|
| 1042 | - { |
|
| 1043 | - $deleted = 0; |
|
| 1044 | - foreach ($parent_line_item->children() as $child_line_item) { |
|
| 1045 | - if ($child_line_item instanceof EE_Line_Item) { |
|
| 1046 | - $deleted += EEH_Line_Item::delete_all_child_items($child_line_item); |
|
| 1047 | - if ($child_line_item->ID()) { |
|
| 1048 | - $child_line_item->delete(); |
|
| 1049 | - unset($child_line_item); |
|
| 1050 | - } else { |
|
| 1051 | - $parent_line_item->delete_child_line_item($child_line_item->code()); |
|
| 1052 | - } |
|
| 1053 | - $deleted++; |
|
| 1054 | - } |
|
| 1055 | - } |
|
| 1056 | - return $deleted; |
|
| 1057 | - } |
|
| 1058 | - |
|
| 1059 | - |
|
| 1060 | - /** |
|
| 1061 | - * Deletes the line items as indicated by the line item code(s) provided, |
|
| 1062 | - * regardless of where they're found in the line item tree. Automatically |
|
| 1063 | - * re-calculates the line item totals and updates the related transaction. But |
|
| 1064 | - * DOES NOT automatically upgrade the transaction's registrations' final prices (which |
|
| 1065 | - * should probably change because of this). |
|
| 1066 | - * You should call EE_Registration_Processor::calculate_reg_final_prices_per_line_item() |
|
| 1067 | - * after using this, to keep the registration final prices in-sync with the transaction's total. |
|
| 1068 | - * |
|
| 1069 | - * @param EE_Line_Item $total_line_item of type EEM_Line_Item::type_total |
|
| 1070 | - * @param array|bool|string $line_item_codes |
|
| 1071 | - * @return int number of items successfully removed |
|
| 1072 | - * @throws EE_Error |
|
| 1073 | - */ |
|
| 1074 | - public static function delete_items(EE_Line_Item $total_line_item, $line_item_codes = false) |
|
| 1075 | - { |
|
| 1076 | - |
|
| 1077 | - if ($total_line_item->type() !== EEM_Line_Item::type_total) { |
|
| 1078 | - EE_Error::doing_it_wrong( |
|
| 1079 | - 'EEH_Line_Item::delete_items', |
|
| 1080 | - esc_html__( |
|
| 1081 | - 'This static method should only be called with a TOTAL line item, otherwise we won\'t recalculate the totals correctly', |
|
| 1082 | - 'event_espresso' |
|
| 1083 | - ), |
|
| 1084 | - '4.6.18' |
|
| 1085 | - ); |
|
| 1086 | - } |
|
| 1087 | - do_action('AHEE_log', __FILE__, __FUNCTION__, ''); |
|
| 1088 | - |
|
| 1089 | - // check if only a single line_item_id was passed |
|
| 1090 | - if (! empty($line_item_codes) && ! is_array($line_item_codes)) { |
|
| 1091 | - // place single line_item_id in an array to appear as multiple line_item_ids |
|
| 1092 | - $line_item_codes = array($line_item_codes); |
|
| 1093 | - } |
|
| 1094 | - $removals = 0; |
|
| 1095 | - // cycle thru line_item_ids |
|
| 1096 | - foreach ($line_item_codes as $line_item_id) { |
|
| 1097 | - $removals += $total_line_item->delete_child_line_item($line_item_id); |
|
| 1098 | - } |
|
| 1099 | - |
|
| 1100 | - if ($removals > 0) { |
|
| 1101 | - $total_line_item->recalculate_taxes_and_tax_total(); |
|
| 1102 | - return $removals; |
|
| 1103 | - } else { |
|
| 1104 | - return false; |
|
| 1105 | - } |
|
| 1106 | - } |
|
| 1107 | - |
|
| 1108 | - |
|
| 1109 | - /** |
|
| 1110 | - * Overwrites the previous tax by clearing out the old taxes, and creates a new |
|
| 1111 | - * tax and updates the total line item accordingly |
|
| 1112 | - * |
|
| 1113 | - * @param EE_Line_Item $total_line_item |
|
| 1114 | - * @param float $amount |
|
| 1115 | - * @param string $name |
|
| 1116 | - * @param string $description |
|
| 1117 | - * @param string $code |
|
| 1118 | - * @param boolean $add_to_existing_line_item |
|
| 1119 | - * if true, and a duplicate line item with the same code is found, |
|
| 1120 | - * $amount will be added onto it; otherwise will simply set the taxes to match $amount |
|
| 1121 | - * @return EE_Line_Item the new tax line item created |
|
| 1122 | - * @throws EE_Error |
|
| 1123 | - * @throws InvalidArgumentException |
|
| 1124 | - * @throws InvalidDataTypeException |
|
| 1125 | - * @throws InvalidInterfaceException |
|
| 1126 | - * @throws ReflectionException |
|
| 1127 | - */ |
|
| 1128 | - public static function set_total_tax_to( |
|
| 1129 | - EE_Line_Item $total_line_item, |
|
| 1130 | - $amount, |
|
| 1131 | - $name = null, |
|
| 1132 | - $description = null, |
|
| 1133 | - $code = null, |
|
| 1134 | - $add_to_existing_line_item = false |
|
| 1135 | - ) { |
|
| 1136 | - $tax_subtotal = self::get_taxes_subtotal($total_line_item); |
|
| 1137 | - $taxable_total = $total_line_item->taxable_total(); |
|
| 1138 | - |
|
| 1139 | - if ($add_to_existing_line_item) { |
|
| 1140 | - $new_tax = $tax_subtotal->get_child_line_item($code); |
|
| 1141 | - EEM_Line_Item::instance()->delete( |
|
| 1142 | - array(array('LIN_code' => array('!=', $code), 'LIN_parent' => $tax_subtotal->ID())) |
|
| 1143 | - ); |
|
| 1144 | - } else { |
|
| 1145 | - $new_tax = null; |
|
| 1146 | - $tax_subtotal->delete_children_line_items(); |
|
| 1147 | - } |
|
| 1148 | - if ($new_tax) { |
|
| 1149 | - $new_tax->set_total($new_tax->total() + $amount); |
|
| 1150 | - $new_tax->set_percent($taxable_total ? $new_tax->total() / $taxable_total * 100 : 0); |
|
| 1151 | - } else { |
|
| 1152 | - // no existing tax item. Create it |
|
| 1153 | - $new_tax = EE_Line_Item::new_instance(array( |
|
| 1154 | - 'TXN_ID' => $total_line_item->TXN_ID(), |
|
| 1155 | - 'LIN_name' => $name ? $name : esc_html__('Tax', 'event_espresso'), |
|
| 1156 | - 'LIN_desc' => $description ? $description : '', |
|
| 1157 | - 'LIN_percent' => $taxable_total ? ($amount / $taxable_total * 100) : 0, |
|
| 1158 | - 'LIN_total' => $amount, |
|
| 1159 | - 'LIN_parent' => $tax_subtotal->ID(), |
|
| 1160 | - 'LIN_type' => EEM_Line_Item::type_tax, |
|
| 1161 | - 'LIN_code' => $code, |
|
| 1162 | - )); |
|
| 1163 | - } |
|
| 1164 | - |
|
| 1165 | - $new_tax = apply_filters( |
|
| 1166 | - 'FHEE__EEH_Line_Item__set_total_tax_to__new_tax_subtotal', |
|
| 1167 | - $new_tax, |
|
| 1168 | - $total_line_item |
|
| 1169 | - ); |
|
| 1170 | - $new_tax->save(); |
|
| 1171 | - $tax_subtotal->set_total($new_tax->total()); |
|
| 1172 | - $tax_subtotal->save(); |
|
| 1173 | - $total_line_item->recalculate_total_including_taxes(); |
|
| 1174 | - return $new_tax; |
|
| 1175 | - } |
|
| 1176 | - |
|
| 1177 | - |
|
| 1178 | - /** |
|
| 1179 | - * Makes all the line items which are children of $line_item taxable (or not). |
|
| 1180 | - * Does NOT save the line items |
|
| 1181 | - * |
|
| 1182 | - * @param EE_Line_Item $line_item |
|
| 1183 | - * @param boolean $taxable |
|
| 1184 | - * @param string $code_substring_for_whitelist if this string is part of the line item's code |
|
| 1185 | - * it will be whitelisted (ie, except from becoming taxable) |
|
| 1186 | - * @throws EE_Error |
|
| 1187 | - */ |
|
| 1188 | - public static function set_line_items_taxable( |
|
| 1189 | - EE_Line_Item $line_item, |
|
| 1190 | - $taxable = true, |
|
| 1191 | - $code_substring_for_whitelist = null |
|
| 1192 | - ) { |
|
| 1193 | - $whitelisted = false; |
|
| 1194 | - if ($code_substring_for_whitelist !== null) { |
|
| 1195 | - $whitelisted = strpos($line_item->code(), $code_substring_for_whitelist) !== false; |
|
| 1196 | - } |
|
| 1197 | - if (! $whitelisted && $line_item->is_line_item()) { |
|
| 1198 | - $line_item->set_is_taxable($taxable); |
|
| 1199 | - } |
|
| 1200 | - foreach ($line_item->children() as $child_line_item) { |
|
| 1201 | - EEH_Line_Item::set_line_items_taxable( |
|
| 1202 | - $child_line_item, |
|
| 1203 | - $taxable, |
|
| 1204 | - $code_substring_for_whitelist |
|
| 1205 | - ); |
|
| 1206 | - } |
|
| 1207 | - } |
|
| 1208 | - |
|
| 1209 | - |
|
| 1210 | - /** |
|
| 1211 | - * Gets all descendants that are event subtotals |
|
| 1212 | - * |
|
| 1213 | - * @uses EEH_Line_Item::get_subtotals_of_object_type() |
|
| 1214 | - * @param EE_Line_Item $parent_line_item - the line item to find descendants of |
|
| 1215 | - * @return EE_Line_Item[] |
|
| 1216 | - * @throws EE_Error |
|
| 1217 | - */ |
|
| 1218 | - public static function get_event_subtotals(EE_Line_Item $parent_line_item) |
|
| 1219 | - { |
|
| 1220 | - return self::get_subtotals_of_object_type($parent_line_item, EEM_Line_Item::OBJ_TYPE_EVENT); |
|
| 1221 | - } |
|
| 1222 | - |
|
| 1223 | - |
|
| 1224 | - /** |
|
| 1225 | - * Gets all descendants subtotals that match the supplied object type |
|
| 1226 | - * |
|
| 1227 | - * @uses EEH_Line_Item::_get_descendants_by_type_and_object_type() |
|
| 1228 | - * @param EE_Line_Item $parent_line_item - the line item to find descendants of |
|
| 1229 | - * @param string $obj_type |
|
| 1230 | - * @return EE_Line_Item[] |
|
| 1231 | - * @throws EE_Error |
|
| 1232 | - */ |
|
| 1233 | - public static function get_subtotals_of_object_type(EE_Line_Item $parent_line_item, $obj_type = '') |
|
| 1234 | - { |
|
| 1235 | - return self::_get_descendants_by_type_and_object_type( |
|
| 1236 | - $parent_line_item, |
|
| 1237 | - EEM_Line_Item::type_sub_total, |
|
| 1238 | - $obj_type |
|
| 1239 | - ); |
|
| 1240 | - } |
|
| 1241 | - |
|
| 1242 | - |
|
| 1243 | - /** |
|
| 1244 | - * Gets all descendants that are tickets |
|
| 1245 | - * |
|
| 1246 | - * @uses EEH_Line_Item::get_line_items_of_object_type() |
|
| 1247 | - * @param EE_Line_Item $parent_line_item - the line item to find descendants of |
|
| 1248 | - * @return EE_Line_Item[] |
|
| 1249 | - * @throws EE_Error |
|
| 1250 | - */ |
|
| 1251 | - public static function get_ticket_line_items(EE_Line_Item $parent_line_item) |
|
| 1252 | - { |
|
| 1253 | - return self::get_line_items_of_object_type( |
|
| 1254 | - $parent_line_item, |
|
| 1255 | - EEM_Line_Item::OBJ_TYPE_TICKET |
|
| 1256 | - ); |
|
| 1257 | - } |
|
| 1258 | - |
|
| 1259 | - |
|
| 1260 | - /** |
|
| 1261 | - * Gets all descendants subtotals that match the supplied object type |
|
| 1262 | - * |
|
| 1263 | - * @uses EEH_Line_Item::_get_descendants_by_type_and_object_type() |
|
| 1264 | - * @param EE_Line_Item $parent_line_item - the line item to find descendants of |
|
| 1265 | - * @param string $obj_type |
|
| 1266 | - * @return EE_Line_Item[] |
|
| 1267 | - * @throws EE_Error |
|
| 1268 | - */ |
|
| 1269 | - public static function get_line_items_of_object_type(EE_Line_Item $parent_line_item, $obj_type = '') |
|
| 1270 | - { |
|
| 1271 | - return self::_get_descendants_by_type_and_object_type( |
|
| 1272 | - $parent_line_item, |
|
| 1273 | - EEM_Line_Item::type_line_item, |
|
| 1274 | - $obj_type |
|
| 1275 | - ); |
|
| 1276 | - } |
|
| 1277 | - |
|
| 1278 | - |
|
| 1279 | - /** |
|
| 1280 | - * Gets all the descendants (ie, children or children of children etc) that are of the type 'tax' |
|
| 1281 | - * |
|
| 1282 | - * @uses EEH_Line_Item::get_descendants_of_type() |
|
| 1283 | - * @param EE_Line_Item $parent_line_item - the line item to find descendants of |
|
| 1284 | - * @return EE_Line_Item[] |
|
| 1285 | - * @throws EE_Error |
|
| 1286 | - */ |
|
| 1287 | - public static function get_tax_descendants(EE_Line_Item $parent_line_item) |
|
| 1288 | - { |
|
| 1289 | - return EEH_Line_Item::get_descendants_of_type( |
|
| 1290 | - $parent_line_item, |
|
| 1291 | - EEM_Line_Item::type_tax |
|
| 1292 | - ); |
|
| 1293 | - } |
|
| 1294 | - |
|
| 1295 | - |
|
| 1296 | - /** |
|
| 1297 | - * Gets all the real items purchased which are children of this item |
|
| 1298 | - * |
|
| 1299 | - * @uses EEH_Line_Item::get_descendants_of_type() |
|
| 1300 | - * @param EE_Line_Item $parent_line_item - the line item to find descendants of |
|
| 1301 | - * @return EE_Line_Item[] |
|
| 1302 | - * @throws EE_Error |
|
| 1303 | - */ |
|
| 1304 | - public static function get_line_item_descendants(EE_Line_Item $parent_line_item) |
|
| 1305 | - { |
|
| 1306 | - return EEH_Line_Item::get_descendants_of_type( |
|
| 1307 | - $parent_line_item, |
|
| 1308 | - EEM_Line_Item::type_line_item |
|
| 1309 | - ); |
|
| 1310 | - } |
|
| 1311 | - |
|
| 1312 | - |
|
| 1313 | - /** |
|
| 1314 | - * Gets all descendants of supplied line item that match the supplied line item type |
|
| 1315 | - * |
|
| 1316 | - * @uses EEH_Line_Item::_get_descendants_by_type_and_object_type() |
|
| 1317 | - * @param EE_Line_Item $parent_line_item - the line item to find descendants of |
|
| 1318 | - * @param string $line_item_type one of the EEM_Line_Item constants |
|
| 1319 | - * @return EE_Line_Item[] |
|
| 1320 | - * @throws EE_Error |
|
| 1321 | - */ |
|
| 1322 | - public static function get_descendants_of_type(EE_Line_Item $parent_line_item, $line_item_type) |
|
| 1323 | - { |
|
| 1324 | - return self::_get_descendants_by_type_and_object_type( |
|
| 1325 | - $parent_line_item, |
|
| 1326 | - $line_item_type, |
|
| 1327 | - null |
|
| 1328 | - ); |
|
| 1329 | - } |
|
| 1330 | - |
|
| 1331 | - |
|
| 1332 | - /** |
|
| 1333 | - * Gets all descendants of supplied line item that match the supplied line item type and possibly the object type |
|
| 1334 | - * as well |
|
| 1335 | - * |
|
| 1336 | - * @param EE_Line_Item $parent_line_item - the line item to find descendants of |
|
| 1337 | - * @param string $line_item_type one of the EEM_Line_Item constants |
|
| 1338 | - * @param string | NULL $obj_type object model class name (minus prefix) or NULL to ignore object type when |
|
| 1339 | - * searching |
|
| 1340 | - * @return EE_Line_Item[] |
|
| 1341 | - * @throws EE_Error |
|
| 1342 | - */ |
|
| 1343 | - protected static function _get_descendants_by_type_and_object_type( |
|
| 1344 | - EE_Line_Item $parent_line_item, |
|
| 1345 | - $line_item_type, |
|
| 1346 | - $obj_type = null |
|
| 1347 | - ) { |
|
| 1348 | - $objects = array(); |
|
| 1349 | - foreach ($parent_line_item->children() as $child_line_item) { |
|
| 1350 | - if ($child_line_item instanceof EE_Line_Item) { |
|
| 1351 | - if ($child_line_item->type() === $line_item_type |
|
| 1352 | - && ( |
|
| 1353 | - $child_line_item->OBJ_type() === $obj_type || $obj_type === null |
|
| 1354 | - ) |
|
| 1355 | - ) { |
|
| 1356 | - $objects[] = $child_line_item; |
|
| 1357 | - } else { |
|
| 1358 | - // go-through-all-its children looking for more matches |
|
| 1359 | - $objects = array_merge( |
|
| 1360 | - $objects, |
|
| 1361 | - self::_get_descendants_by_type_and_object_type( |
|
| 1362 | - $child_line_item, |
|
| 1363 | - $line_item_type, |
|
| 1364 | - $obj_type |
|
| 1365 | - ) |
|
| 1366 | - ); |
|
| 1367 | - } |
|
| 1368 | - } |
|
| 1369 | - } |
|
| 1370 | - return $objects; |
|
| 1371 | - } |
|
| 1372 | - |
|
| 1373 | - |
|
| 1374 | - /** |
|
| 1375 | - * Gets all descendants subtotals that match the supplied object type |
|
| 1376 | - * |
|
| 1377 | - * @uses EEH_Line_Item::_get_descendants_by_type_and_object_type() |
|
| 1378 | - * @param EE_Line_Item $parent_line_item - the line item to find descendants of |
|
| 1379 | - * @param string $OBJ_type object type (like Event) |
|
| 1380 | - * @param array $OBJ_IDs array of OBJ_IDs |
|
| 1381 | - * @return EE_Line_Item[] |
|
| 1382 | - * @throws EE_Error |
|
| 1383 | - */ |
|
| 1384 | - public static function get_line_items_by_object_type_and_IDs( |
|
| 1385 | - EE_Line_Item $parent_line_item, |
|
| 1386 | - $OBJ_type = '', |
|
| 1387 | - $OBJ_IDs = array() |
|
| 1388 | - ) { |
|
| 1389 | - return self::_get_descendants_by_object_type_and_object_ID( |
|
| 1390 | - $parent_line_item, |
|
| 1391 | - $OBJ_type, |
|
| 1392 | - $OBJ_IDs |
|
| 1393 | - ); |
|
| 1394 | - } |
|
| 1395 | - |
|
| 1396 | - |
|
| 1397 | - /** |
|
| 1398 | - * Gets all descendants of supplied line item that match the supplied line item type and possibly the object type |
|
| 1399 | - * as well |
|
| 1400 | - * |
|
| 1401 | - * @param EE_Line_Item $parent_line_item - the line item to find descendants of |
|
| 1402 | - * @param string $OBJ_type object type (like Event) |
|
| 1403 | - * @param array $OBJ_IDs array of OBJ_IDs |
|
| 1404 | - * @return EE_Line_Item[] |
|
| 1405 | - * @throws EE_Error |
|
| 1406 | - */ |
|
| 1407 | - protected static function _get_descendants_by_object_type_and_object_ID( |
|
| 1408 | - EE_Line_Item $parent_line_item, |
|
| 1409 | - $OBJ_type, |
|
| 1410 | - $OBJ_IDs |
|
| 1411 | - ) { |
|
| 1412 | - $objects = array(); |
|
| 1413 | - foreach ($parent_line_item->children() as $child_line_item) { |
|
| 1414 | - if ($child_line_item instanceof EE_Line_Item) { |
|
| 1415 | - if ($child_line_item->OBJ_type() === $OBJ_type |
|
| 1416 | - && is_array($OBJ_IDs) |
|
| 1417 | - && in_array($child_line_item->OBJ_ID(), $OBJ_IDs) |
|
| 1418 | - ) { |
|
| 1419 | - $objects[] = $child_line_item; |
|
| 1420 | - } else { |
|
| 1421 | - // go-through-all-its children looking for more matches |
|
| 1422 | - $objects = array_merge( |
|
| 1423 | - $objects, |
|
| 1424 | - self::_get_descendants_by_object_type_and_object_ID( |
|
| 1425 | - $child_line_item, |
|
| 1426 | - $OBJ_type, |
|
| 1427 | - $OBJ_IDs |
|
| 1428 | - ) |
|
| 1429 | - ); |
|
| 1430 | - } |
|
| 1431 | - } |
|
| 1432 | - } |
|
| 1433 | - return $objects; |
|
| 1434 | - } |
|
| 1435 | - |
|
| 1436 | - |
|
| 1437 | - /** |
|
| 1438 | - * Uses a breadth-first-search in order to find the nearest descendant of |
|
| 1439 | - * the specified type and returns it, else NULL |
|
| 1440 | - * |
|
| 1441 | - * @uses EEH_Line_Item::_get_nearest_descendant() |
|
| 1442 | - * @param EE_Line_Item $parent_line_item - the line item to find descendants of |
|
| 1443 | - * @param string $type like one of the EEM_Line_Item::type_* |
|
| 1444 | - * @return EE_Line_Item |
|
| 1445 | - * @throws EE_Error |
|
| 1446 | - * @throws InvalidArgumentException |
|
| 1447 | - * @throws InvalidDataTypeException |
|
| 1448 | - * @throws InvalidInterfaceException |
|
| 1449 | - * @throws ReflectionException |
|
| 1450 | - */ |
|
| 1451 | - public static function get_nearest_descendant_of_type(EE_Line_Item $parent_line_item, $type) |
|
| 1452 | - { |
|
| 1453 | - return self::_get_nearest_descendant($parent_line_item, 'LIN_type', $type); |
|
| 1454 | - } |
|
| 1455 | - |
|
| 1456 | - |
|
| 1457 | - /** |
|
| 1458 | - * Uses a breadth-first-search in order to find the nearest descendant |
|
| 1459 | - * having the specified LIN_code and returns it, else NULL |
|
| 1460 | - * |
|
| 1461 | - * @uses EEH_Line_Item::_get_nearest_descendant() |
|
| 1462 | - * @param EE_Line_Item $parent_line_item - the line item to find descendants of |
|
| 1463 | - * @param string $code any value used for LIN_code |
|
| 1464 | - * @return EE_Line_Item |
|
| 1465 | - * @throws EE_Error |
|
| 1466 | - * @throws InvalidArgumentException |
|
| 1467 | - * @throws InvalidDataTypeException |
|
| 1468 | - * @throws InvalidInterfaceException |
|
| 1469 | - * @throws ReflectionException |
|
| 1470 | - */ |
|
| 1471 | - public static function get_nearest_descendant_having_code(EE_Line_Item $parent_line_item, $code) |
|
| 1472 | - { |
|
| 1473 | - return self::_get_nearest_descendant($parent_line_item, 'LIN_code', $code); |
|
| 1474 | - } |
|
| 1475 | - |
|
| 1476 | - |
|
| 1477 | - /** |
|
| 1478 | - * Uses a breadth-first-search in order to find the nearest descendant |
|
| 1479 | - * having the specified LIN_code and returns it, else NULL |
|
| 1480 | - * |
|
| 1481 | - * @param EE_Line_Item $parent_line_item - the line item to find descendants of |
|
| 1482 | - * @param string $search_field name of EE_Line_Item property |
|
| 1483 | - * @param string $value any value stored in $search_field |
|
| 1484 | - * @return EE_Line_Item |
|
| 1485 | - * @throws EE_Error |
|
| 1486 | - * @throws InvalidArgumentException |
|
| 1487 | - * @throws InvalidDataTypeException |
|
| 1488 | - * @throws InvalidInterfaceException |
|
| 1489 | - * @throws ReflectionException |
|
| 1490 | - */ |
|
| 1491 | - protected static function _get_nearest_descendant(EE_Line_Item $parent_line_item, $search_field, $value) |
|
| 1492 | - { |
|
| 1493 | - foreach ($parent_line_item->children() as $child) { |
|
| 1494 | - if ($child->get($search_field) == $value) { |
|
| 1495 | - return $child; |
|
| 1496 | - } |
|
| 1497 | - } |
|
| 1498 | - foreach ($parent_line_item->children() as $child) { |
|
| 1499 | - $descendant_found = self::_get_nearest_descendant( |
|
| 1500 | - $child, |
|
| 1501 | - $search_field, |
|
| 1502 | - $value |
|
| 1503 | - ); |
|
| 1504 | - if ($descendant_found) { |
|
| 1505 | - return $descendant_found; |
|
| 1506 | - } |
|
| 1507 | - } |
|
| 1508 | - return null; |
|
| 1509 | - } |
|
| 1510 | - |
|
| 1511 | - |
|
| 1512 | - /** |
|
| 1513 | - * if passed line item has a TXN ID, uses that to jump directly to the grand total line item for the transaction, |
|
| 1514 | - * else recursively walks up the line item tree until a parent of type total is found, |
|
| 1515 | - * |
|
| 1516 | - * @param EE_Line_Item $line_item |
|
| 1517 | - * @return EE_Line_Item |
|
| 1518 | - * @throws EE_Error |
|
| 1519 | - */ |
|
| 1520 | - public static function find_transaction_grand_total_for_line_item(EE_Line_Item $line_item) |
|
| 1521 | - { |
|
| 1522 | - if ($line_item->TXN_ID()) { |
|
| 1523 | - $total_line_item = $line_item->transaction()->total_line_item(false); |
|
| 1524 | - if ($total_line_item instanceof EE_Line_Item) { |
|
| 1525 | - return $total_line_item; |
|
| 1526 | - } |
|
| 1527 | - } else { |
|
| 1528 | - $line_item_parent = $line_item->parent(); |
|
| 1529 | - if ($line_item_parent instanceof EE_Line_Item) { |
|
| 1530 | - if ($line_item_parent->is_total()) { |
|
| 1531 | - return $line_item_parent; |
|
| 1532 | - } |
|
| 1533 | - return EEH_Line_Item::find_transaction_grand_total_for_line_item($line_item_parent); |
|
| 1534 | - } |
|
| 1535 | - } |
|
| 1536 | - throw new EE_Error( |
|
| 1537 | - sprintf( |
|
| 1538 | - esc_html__( |
|
| 1539 | - 'A valid grand total for line item %1$d was not found.', |
|
| 1540 | - 'event_espresso' |
|
| 1541 | - ), |
|
| 1542 | - $line_item->ID() |
|
| 1543 | - ) |
|
| 1544 | - ); |
|
| 1545 | - } |
|
| 1546 | - |
|
| 1547 | - |
|
| 1548 | - /** |
|
| 1549 | - * Prints out a representation of the line item tree |
|
| 1550 | - * |
|
| 1551 | - * @param EE_Line_Item $line_item |
|
| 1552 | - * @param int $indentation |
|
| 1553 | - * @return void |
|
| 1554 | - * @throws EE_Error |
|
| 1555 | - */ |
|
| 1556 | - public static function visualize(EE_Line_Item $line_item, $indentation = 0) |
|
| 1557 | - { |
|
| 1558 | - echo defined('EE_TESTS_DIR') ? "\n" : '<br />'; |
|
| 1559 | - if (! $indentation) { |
|
| 1560 | - echo defined('EE_TESTS_DIR') ? "\n" : '<br />'; |
|
| 1561 | - } |
|
| 1562 | - for ($i = 0; $i < $indentation; $i++) { |
|
| 1563 | - echo '. '; |
|
| 1564 | - } |
|
| 1565 | - $breakdown = ''; |
|
| 1566 | - if ($line_item->is_line_item()) { |
|
| 1567 | - if ($line_item->is_percent()) { |
|
| 1568 | - $breakdown = "{$line_item->percent()}%"; |
|
| 1569 | - } else { |
|
| 1570 | - $breakdown = '$' . "{$line_item->unit_price()} x {$line_item->quantity()}"; |
|
| 1571 | - } |
|
| 1572 | - } |
|
| 1573 | - echo $line_item->name(); |
|
| 1574 | - echo " [ ID:{$line_item->ID()} | qty:{$line_item->quantity()} ] {$line_item->type()} : "; |
|
| 1575 | - echo '$' . (string) $line_item->total(); |
|
| 1576 | - if ($breakdown) { |
|
| 1577 | - echo " ( {$breakdown} )"; |
|
| 1578 | - } |
|
| 1579 | - if ($line_item->is_taxable()) { |
|
| 1580 | - echo ' * taxable'; |
|
| 1581 | - } |
|
| 1582 | - if ($line_item->children()) { |
|
| 1583 | - foreach ($line_item->children() as $child) { |
|
| 1584 | - self::visualize($child, $indentation + 1); |
|
| 1585 | - } |
|
| 1586 | - } |
|
| 1587 | - } |
|
| 1588 | - |
|
| 1589 | - |
|
| 1590 | - /** |
|
| 1591 | - * Calculates the registration's final price, taking into account that they |
|
| 1592 | - * need to not only help pay for their OWN ticket, but also any transaction-wide surcharges and taxes, |
|
| 1593 | - * and receive a portion of any transaction-wide discounts. |
|
| 1594 | - * eg1, if I buy a $1 ticket and brent buys a $9 ticket, and we receive a $5 discount |
|
| 1595 | - * then I'll get 1/10 of that $5 discount, which is $0.50, and brent will get |
|
| 1596 | - * 9/10ths of that $5 discount, which is $4.50. So my final price should be $0.50 |
|
| 1597 | - * and brent's final price should be $5.50. |
|
| 1598 | - * In order to do this, we basically need to traverse the line item tree calculating |
|
| 1599 | - * the running totals (just as if we were recalculating the total), but when we identify |
|
| 1600 | - * regular line items, we need to keep track of their share of the grand total. |
|
| 1601 | - * Also, we need to keep track of the TAXABLE total for each ticket purchase, so |
|
| 1602 | - * we can know how to apply taxes to it. (Note: "taxable total" does not equal the "pretax total" |
|
| 1603 | - * when there are non-taxable items; otherwise they would be the same) |
|
| 1604 | - * |
|
| 1605 | - * @param EE_Line_Item $line_item |
|
| 1606 | - * @param array $billable_ticket_quantities array of EE_Ticket IDs and their corresponding quantity that |
|
| 1607 | - * can be included in price calculations at this moment |
|
| 1608 | - * @return array keys are line items for tickets IDs and values are their share of the running total, |
|
| 1609 | - * plus the key 'total', and 'taxable' which also has keys of all |
|
| 1610 | - * the ticket IDs. |
|
| 1611 | - * Eg array( |
|
| 1612 | - * 12 => 4.3 |
|
| 1613 | - * 23 => 8.0 |
|
| 1614 | - * 'total' => 16.6, |
|
| 1615 | - * 'taxable' => array( |
|
| 1616 | - * 12 => 10, |
|
| 1617 | - * 23 => 4 |
|
| 1618 | - * ). |
|
| 1619 | - * So to find which registrations have which final price, we need |
|
| 1620 | - * to find which line item is theirs, which can be done with |
|
| 1621 | - * `EEM_Line_Item::instance()->get_line_item_for_registration( |
|
| 1622 | - * $registration );` |
|
| 1623 | - * @throws EE_Error |
|
| 1624 | - * @throws InvalidArgumentException |
|
| 1625 | - * @throws InvalidDataTypeException |
|
| 1626 | - * @throws InvalidInterfaceException |
|
| 1627 | - * @throws ReflectionException |
|
| 1628 | - */ |
|
| 1629 | - public static function calculate_reg_final_prices_per_line_item( |
|
| 1630 | - EE_Line_Item $line_item, |
|
| 1631 | - $billable_ticket_quantities = array() |
|
| 1632 | - ) { |
|
| 1633 | - $running_totals = [ |
|
| 1634 | - 'total' => 0, |
|
| 1635 | - 'taxable' => ['total' => 0] |
|
| 1636 | - ]; |
|
| 1637 | - foreach ($line_item->children() as $child_line_item) { |
|
| 1638 | - switch ($child_line_item->type()) { |
|
| 1639 | - case EEM_Line_Item::type_sub_total: |
|
| 1640 | - $running_totals_from_subtotal = EEH_Line_Item::calculate_reg_final_prices_per_line_item( |
|
| 1641 | - $child_line_item, |
|
| 1642 | - $billable_ticket_quantities |
|
| 1643 | - ); |
|
| 1644 | - // combine arrays but preserve numeric keys |
|
| 1645 | - $running_totals = array_replace_recursive($running_totals_from_subtotal, $running_totals); |
|
| 1646 | - $running_totals['total'] += $running_totals_from_subtotal['total']; |
|
| 1647 | - $running_totals['taxable']['total'] += $running_totals_from_subtotal['taxable']['total']; |
|
| 1648 | - break; |
|
| 1649 | - |
|
| 1650 | - case EEM_Line_Item::type_tax_sub_total: |
|
| 1651 | - // find how much the taxes percentage is |
|
| 1652 | - if ($child_line_item->percent() !== 0) { |
|
| 1653 | - $tax_percent_decimal = $child_line_item->percent() / 100; |
|
| 1654 | - } else { |
|
| 1655 | - $tax_percent_decimal = EE_Taxes::get_total_taxes_percentage() / 100; |
|
| 1656 | - } |
|
| 1657 | - // and apply to all the taxable totals, and add to the pretax totals |
|
| 1658 | - foreach ($running_totals as $line_item_id => $this_running_total) { |
|
| 1659 | - // "total" and "taxable" array key is an exception |
|
| 1660 | - if ($line_item_id === 'taxable') { |
|
| 1661 | - continue; |
|
| 1662 | - } |
|
| 1663 | - $taxable_total = $running_totals['taxable'][ $line_item_id ]; |
|
| 1664 | - $running_totals[ $line_item_id ] += ($taxable_total * $tax_percent_decimal); |
|
| 1665 | - } |
|
| 1666 | - break; |
|
| 1667 | - |
|
| 1668 | - case EEM_Line_Item::type_line_item: |
|
| 1669 | - // ticket line items or ???? |
|
| 1670 | - if ($child_line_item->OBJ_type() === EEM_Line_Item::OBJ_TYPE_TICKET) { |
|
| 1671 | - // kk it's a ticket |
|
| 1672 | - if (isset($running_totals[ $child_line_item->ID() ])) { |
|
| 1673 | - // huh? that shouldn't happen. |
|
| 1674 | - $running_totals['total'] += $child_line_item->total(); |
|
| 1675 | - } else { |
|
| 1676 | - // its not in our running totals yet. great. |
|
| 1677 | - if ($child_line_item->is_taxable()) { |
|
| 1678 | - $taxable_amount = $child_line_item->unit_price(); |
|
| 1679 | - } else { |
|
| 1680 | - $taxable_amount = 0; |
|
| 1681 | - } |
|
| 1682 | - // are we only calculating totals for some tickets? |
|
| 1683 | - if (isset($billable_ticket_quantities[ $child_line_item->OBJ_ID() ])) { |
|
| 1684 | - $quantity = $billable_ticket_quantities[ $child_line_item->OBJ_ID() ]; |
|
| 1685 | - $running_totals[ $child_line_item->ID() ] = $quantity |
|
| 1686 | - ? $child_line_item->unit_price() |
|
| 1687 | - : 0; |
|
| 1688 | - $running_totals['taxable'][ $child_line_item->ID() ] = $quantity |
|
| 1689 | - ? $taxable_amount |
|
| 1690 | - : 0; |
|
| 1691 | - } else { |
|
| 1692 | - $quantity = $child_line_item->quantity(); |
|
| 1693 | - $running_totals[ $child_line_item->ID() ] = $child_line_item->unit_price(); |
|
| 1694 | - $running_totals['taxable'][ $child_line_item->ID() ] = $taxable_amount; |
|
| 1695 | - } |
|
| 1696 | - $running_totals['taxable']['total'] += $taxable_amount * $quantity; |
|
| 1697 | - $running_totals['total'] += $child_line_item->unit_price() * $quantity; |
|
| 1698 | - } |
|
| 1699 | - } else { |
|
| 1700 | - // it's some other type of item added to the cart |
|
| 1701 | - // it should affect the running totals |
|
| 1702 | - // basically we want to convert it into a PERCENT modifier. Because |
|
| 1703 | - // more clearly affect all registration's final price equally |
|
| 1704 | - $line_items_percent_of_running_total = $running_totals['total'] > 0 |
|
| 1705 | - ? ($child_line_item->total() / $running_totals['total']) + 1 |
|
| 1706 | - : 1; |
|
| 1707 | - foreach ($running_totals as $line_item_id => $this_running_total) { |
|
| 1708 | - // the "taxable" array key is an exception |
|
| 1709 | - if ($line_item_id === 'taxable') { |
|
| 1710 | - continue; |
|
| 1711 | - } |
|
| 1712 | - // update the running totals |
|
| 1713 | - // yes this actually even works for the running grand total! |
|
| 1714 | - $running_totals[ $line_item_id ] = |
|
| 1715 | - $line_items_percent_of_running_total * $this_running_total; |
|
| 1716 | - |
|
| 1717 | - if ($child_line_item->is_taxable()) { |
|
| 1718 | - $running_totals['taxable'][ $line_item_id ] = |
|
| 1719 | - $line_items_percent_of_running_total * $running_totals['taxable'][ $line_item_id ]; |
|
| 1720 | - } |
|
| 1721 | - } |
|
| 1722 | - } |
|
| 1723 | - break; |
|
| 1724 | - } |
|
| 1725 | - } |
|
| 1726 | - return $running_totals; |
|
| 1727 | - } |
|
| 1728 | - |
|
| 1729 | - |
|
| 1730 | - /** |
|
| 1731 | - * @param EE_Line_Item $total_line_item |
|
| 1732 | - * @param EE_Line_Item $ticket_line_item |
|
| 1733 | - * @return float | null |
|
| 1734 | - * @throws EE_Error |
|
| 1735 | - * @throws InvalidArgumentException |
|
| 1736 | - * @throws InvalidDataTypeException |
|
| 1737 | - * @throws InvalidInterfaceException |
|
| 1738 | - * @throws OutOfRangeException |
|
| 1739 | - * @throws ReflectionException |
|
| 1740 | - */ |
|
| 1741 | - public static function calculate_final_price_for_ticket_line_item( |
|
| 1742 | - EE_Line_Item $total_line_item, |
|
| 1743 | - EE_Line_Item $ticket_line_item |
|
| 1744 | - ) { |
|
| 1745 | - static $final_prices_per_ticket_line_item = array(); |
|
| 1746 | - if (empty($final_prices_per_ticket_line_item) || empty($final_prices_per_ticket_line_item[ $total_line_item->ID() ])) { |
|
| 1747 | - $final_prices_per_ticket_line_item[ $total_line_item->ID() ] = EEH_Line_Item::calculate_reg_final_prices_per_line_item( |
|
| 1748 | - $total_line_item |
|
| 1749 | - ); |
|
| 1750 | - } |
|
| 1751 | - // ok now find this new registration's final price |
|
| 1752 | - if (isset($final_prices_per_ticket_line_item[ $total_line_item->ID() ][ $ticket_line_item->ID() ])) { |
|
| 1753 | - return $final_prices_per_ticket_line_item[ $total_line_item->ID() ][ $ticket_line_item->ID() ]; |
|
| 1754 | - } |
|
| 1755 | - $message = sprintf( |
|
| 1756 | - esc_html__( |
|
| 1757 | - 'The final price for the ticket line item (ID:%1$d) could not be calculated.', |
|
| 1758 | - 'event_espresso' |
|
| 1759 | - ), |
|
| 1760 | - $ticket_line_item->ID() |
|
| 1761 | - ); |
|
| 1762 | - if (WP_DEBUG) { |
|
| 1763 | - $message .= '<br>' . print_r($final_prices_per_ticket_line_item, true); |
|
| 1764 | - throw new OutOfRangeException($message); |
|
| 1765 | - } |
|
| 1766 | - EE_Log::instance()->log(__CLASS__, __FUNCTION__, $message); |
|
| 1767 | - return null; |
|
| 1768 | - } |
|
| 1769 | - |
|
| 1770 | - |
|
| 1771 | - /** |
|
| 1772 | - * Creates a duplicate of the line item tree, except only includes billable items |
|
| 1773 | - * and the portion of line items attributed to billable things |
|
| 1774 | - * |
|
| 1775 | - * @param EE_Line_Item $line_item |
|
| 1776 | - * @param EE_Registration[] $registrations |
|
| 1777 | - * @return EE_Line_Item |
|
| 1778 | - * @throws EE_Error |
|
| 1779 | - * @throws InvalidArgumentException |
|
| 1780 | - * @throws InvalidDataTypeException |
|
| 1781 | - * @throws InvalidInterfaceException |
|
| 1782 | - * @throws ReflectionException |
|
| 1783 | - */ |
|
| 1784 | - public static function billable_line_item_tree(EE_Line_Item $line_item, $registrations) |
|
| 1785 | - { |
|
| 1786 | - $copy_li = EEH_Line_Item::billable_line_item($line_item, $registrations); |
|
| 1787 | - foreach ($line_item->children() as $child_li) { |
|
| 1788 | - $copy_li->add_child_line_item( |
|
| 1789 | - EEH_Line_Item::billable_line_item_tree($child_li, $registrations) |
|
| 1790 | - ); |
|
| 1791 | - } |
|
| 1792 | - // if this is the grand total line item, make sure the totals all add up |
|
| 1793 | - // (we could have duplicated this logic AS we copied the line items, but |
|
| 1794 | - // it seems DRYer this way) |
|
| 1795 | - if ($copy_li->type() === EEM_Line_Item::type_total) { |
|
| 1796 | - $copy_li->recalculate_total_including_taxes(); |
|
| 1797 | - } |
|
| 1798 | - return $copy_li; |
|
| 1799 | - } |
|
| 1800 | - |
|
| 1801 | - |
|
| 1802 | - /** |
|
| 1803 | - * Creates a new, unsaved line item from $line_item that factors in the |
|
| 1804 | - * number of billable registrations on $registrations. |
|
| 1805 | - * |
|
| 1806 | - * @param EE_Line_Item $line_item |
|
| 1807 | - * @param EE_Registration[] $registrations |
|
| 1808 | - * @return EE_Line_Item |
|
| 1809 | - * @throws EE_Error |
|
| 1810 | - * @throws InvalidArgumentException |
|
| 1811 | - * @throws InvalidDataTypeException |
|
| 1812 | - * @throws InvalidInterfaceException |
|
| 1813 | - * @throws ReflectionException |
|
| 1814 | - */ |
|
| 1815 | - public static function billable_line_item(EE_Line_Item $line_item, $registrations) |
|
| 1816 | - { |
|
| 1817 | - $new_li_fields = $line_item->model_field_array(); |
|
| 1818 | - if ($line_item->type() === EEM_Line_Item::type_line_item && |
|
| 1819 | - $line_item->OBJ_type() === EEM_Line_Item::OBJ_TYPE_TICKET |
|
| 1820 | - ) { |
|
| 1821 | - $count = 0; |
|
| 1822 | - foreach ($registrations as $registration) { |
|
| 1823 | - if ($line_item->OBJ_ID() === $registration->ticket_ID() && |
|
| 1824 | - in_array( |
|
| 1825 | - $registration->status_ID(), |
|
| 1826 | - EEM_Registration::reg_statuses_that_allow_payment(), |
|
| 1827 | - true |
|
| 1828 | - ) |
|
| 1829 | - ) { |
|
| 1830 | - $count++; |
|
| 1831 | - } |
|
| 1832 | - } |
|
| 1833 | - $new_li_fields['LIN_quantity'] = $count; |
|
| 1834 | - } |
|
| 1835 | - // don't set the total. We'll leave that up to the code that calculates it |
|
| 1836 | - unset($new_li_fields['LIN_ID'], $new_li_fields['LIN_parent'], $new_li_fields['LIN_total']); |
|
| 1837 | - return EE_Line_Item::new_instance($new_li_fields); |
|
| 1838 | - } |
|
| 1839 | - |
|
| 1840 | - |
|
| 1841 | - /** |
|
| 1842 | - * Returns a modified line item tree where all the subtotals which have a total of 0 |
|
| 1843 | - * are removed, and line items with a quantity of 0 |
|
| 1844 | - * |
|
| 1845 | - * @param EE_Line_Item $line_item |null |
|
| 1846 | - * @return EE_Line_Item|null |
|
| 1847 | - * @throws EE_Error |
|
| 1848 | - * @throws InvalidArgumentException |
|
| 1849 | - * @throws InvalidDataTypeException |
|
| 1850 | - * @throws InvalidInterfaceException |
|
| 1851 | - * @throws ReflectionException |
|
| 1852 | - */ |
|
| 1853 | - public static function non_empty_line_items(EE_Line_Item $line_item) |
|
| 1854 | - { |
|
| 1855 | - $copied_li = EEH_Line_Item::non_empty_line_item($line_item); |
|
| 1856 | - if ($copied_li === null) { |
|
| 1857 | - return null; |
|
| 1858 | - } |
|
| 1859 | - // if this is an event subtotal, we want to only include it if it |
|
| 1860 | - // has a non-zero total and at least one ticket line item child |
|
| 1861 | - $ticket_children = 0; |
|
| 1862 | - foreach ($line_item->children() as $child_li) { |
|
| 1863 | - $child_li_copy = EEH_Line_Item::non_empty_line_items($child_li); |
|
| 1864 | - if ($child_li_copy !== null) { |
|
| 1865 | - $copied_li->add_child_line_item($child_li_copy); |
|
| 1866 | - if ($child_li_copy->type() === EEM_Line_Item::type_line_item && |
|
| 1867 | - $child_li_copy->OBJ_type() === EEM_Line_Item::OBJ_TYPE_TICKET |
|
| 1868 | - ) { |
|
| 1869 | - $ticket_children++; |
|
| 1870 | - } |
|
| 1871 | - } |
|
| 1872 | - } |
|
| 1873 | - // if this is an event subtotal with NO ticket children |
|
| 1874 | - // we basically want to ignore it |
|
| 1875 | - if ($ticket_children === 0 |
|
| 1876 | - && $line_item->type() === EEM_Line_Item::type_sub_total |
|
| 1877 | - && $line_item->OBJ_type() === EEM_Line_Item::OBJ_TYPE_EVENT |
|
| 1878 | - && $line_item->total() === 0 |
|
| 1879 | - ) { |
|
| 1880 | - return null; |
|
| 1881 | - } |
|
| 1882 | - return $copied_li; |
|
| 1883 | - } |
|
| 1884 | - |
|
| 1885 | - |
|
| 1886 | - /** |
|
| 1887 | - * Creates a new, unsaved line item, but if it's a ticket line item |
|
| 1888 | - * with a total of 0, or a subtotal of 0, returns null instead |
|
| 1889 | - * |
|
| 1890 | - * @param EE_Line_Item $line_item |
|
| 1891 | - * @return EE_Line_Item |
|
| 1892 | - * @throws EE_Error |
|
| 1893 | - * @throws InvalidArgumentException |
|
| 1894 | - * @throws InvalidDataTypeException |
|
| 1895 | - * @throws InvalidInterfaceException |
|
| 1896 | - * @throws ReflectionException |
|
| 1897 | - */ |
|
| 1898 | - public static function non_empty_line_item(EE_Line_Item $line_item) |
|
| 1899 | - { |
|
| 1900 | - if ($line_item->type() === EEM_Line_Item::type_line_item |
|
| 1901 | - && $line_item->OBJ_type() === EEM_Line_Item::OBJ_TYPE_TICKET |
|
| 1902 | - && $line_item->quantity() === 0 |
|
| 1903 | - ) { |
|
| 1904 | - return null; |
|
| 1905 | - } |
|
| 1906 | - $new_li_fields = $line_item->model_field_array(); |
|
| 1907 | - // don't set the total. We'll leave that up to the code that calculates it |
|
| 1908 | - unset($new_li_fields['LIN_ID'], $new_li_fields['LIN_parent']); |
|
| 1909 | - return EE_Line_Item::new_instance($new_li_fields); |
|
| 1910 | - } |
|
| 1911 | - |
|
| 1912 | - |
|
| 1913 | - /** |
|
| 1914 | - * Cycles through all of the ticket line items for the supplied total line item |
|
| 1915 | - * and ensures that the line item's "is_taxable" field matches that of its corresponding ticket |
|
| 1916 | - * |
|
| 1917 | - * @param EE_Line_Item $total_line_item |
|
| 1918 | - * @since 4.9.79.p |
|
| 1919 | - * @throws EE_Error |
|
| 1920 | - * @throws InvalidArgumentException |
|
| 1921 | - * @throws InvalidDataTypeException |
|
| 1922 | - * @throws InvalidInterfaceException |
|
| 1923 | - * @throws ReflectionException |
|
| 1924 | - */ |
|
| 1925 | - public static function resetIsTaxableForTickets(EE_Line_Item $total_line_item) |
|
| 1926 | - { |
|
| 1927 | - $ticket_line_items = self::get_ticket_line_items($total_line_item); |
|
| 1928 | - foreach ($ticket_line_items as $ticket_line_item) { |
|
| 1929 | - if ($ticket_line_item instanceof EE_Line_Item |
|
| 1930 | - && $ticket_line_item->OBJ_type() === EEM_Line_Item::OBJ_TYPE_TICKET |
|
| 1931 | - ) { |
|
| 1932 | - $ticket = $ticket_line_item->ticket(); |
|
| 1933 | - if ($ticket instanceof EE_Ticket && $ticket->taxable() !== $ticket_line_item->is_taxable()) { |
|
| 1934 | - $ticket_line_item->set_is_taxable($ticket->taxable()); |
|
| 1935 | - $ticket_line_item->save(); |
|
| 1936 | - } |
|
| 1937 | - } |
|
| 1938 | - } |
|
| 1939 | - } |
|
| 1940 | - |
|
| 1941 | - |
|
| 1942 | - |
|
| 1943 | - /**************************************** @DEPRECATED METHODS *************************************** */ |
|
| 1944 | - /** |
|
| 1945 | - * @deprecated |
|
| 1946 | - * @param EE_Line_Item $total_line_item |
|
| 1947 | - * @return EE_Line_Item |
|
| 1948 | - * @throws EE_Error |
|
| 1949 | - * @throws InvalidArgumentException |
|
| 1950 | - * @throws InvalidDataTypeException |
|
| 1951 | - * @throws InvalidInterfaceException |
|
| 1952 | - * @throws ReflectionException |
|
| 1953 | - */ |
|
| 1954 | - public static function get_items_subtotal(EE_Line_Item $total_line_item) |
|
| 1955 | - { |
|
| 1956 | - EE_Error::doing_it_wrong( |
|
| 1957 | - 'EEH_Line_Item::get_items_subtotal()', |
|
| 1958 | - sprintf( |
|
| 1959 | - esc_html__('Method replaced with %1$s', 'event_espresso'), |
|
| 1960 | - 'EEH_Line_Item::get_pre_tax_subtotal()' |
|
| 1961 | - ), |
|
| 1962 | - '4.6.0' |
|
| 1963 | - ); |
|
| 1964 | - return self::get_pre_tax_subtotal($total_line_item); |
|
| 1965 | - } |
|
| 1966 | - |
|
| 1967 | - |
|
| 1968 | - /** |
|
| 1969 | - * @deprecated |
|
| 1970 | - * @param EE_Transaction $transaction |
|
| 1971 | - * @return EE_Line_Item |
|
| 1972 | - * @throws EE_Error |
|
| 1973 | - * @throws InvalidArgumentException |
|
| 1974 | - * @throws InvalidDataTypeException |
|
| 1975 | - * @throws InvalidInterfaceException |
|
| 1976 | - * @throws ReflectionException |
|
| 1977 | - */ |
|
| 1978 | - public static function create_default_total_line_item($transaction = null) |
|
| 1979 | - { |
|
| 1980 | - EE_Error::doing_it_wrong( |
|
| 1981 | - 'EEH_Line_Item::create_default_total_line_item()', |
|
| 1982 | - sprintf( |
|
| 1983 | - esc_html__('Method replaced with %1$s', 'event_espresso'), |
|
| 1984 | - 'EEH_Line_Item::create_total_line_item()' |
|
| 1985 | - ), |
|
| 1986 | - '4.6.0' |
|
| 1987 | - ); |
|
| 1988 | - return self::create_total_line_item($transaction); |
|
| 1989 | - } |
|
| 1990 | - |
|
| 1991 | - |
|
| 1992 | - /** |
|
| 1993 | - * @deprecated |
|
| 1994 | - * @param EE_Line_Item $total_line_item |
|
| 1995 | - * @param EE_Transaction $transaction |
|
| 1996 | - * @return EE_Line_Item |
|
| 1997 | - * @throws EE_Error |
|
| 1998 | - * @throws InvalidArgumentException |
|
| 1999 | - * @throws InvalidDataTypeException |
|
| 2000 | - * @throws InvalidInterfaceException |
|
| 2001 | - * @throws ReflectionException |
|
| 2002 | - */ |
|
| 2003 | - public static function create_default_tickets_subtotal(EE_Line_Item $total_line_item, $transaction = null) |
|
| 2004 | - { |
|
| 2005 | - EE_Error::doing_it_wrong( |
|
| 2006 | - 'EEH_Line_Item::create_default_tickets_subtotal()', |
|
| 2007 | - sprintf( |
|
| 2008 | - esc_html__('Method replaced with %1$s', 'event_espresso'), |
|
| 2009 | - 'EEH_Line_Item::create_pre_tax_subtotal()' |
|
| 2010 | - ), |
|
| 2011 | - '4.6.0' |
|
| 2012 | - ); |
|
| 2013 | - return self::create_pre_tax_subtotal($total_line_item, $transaction); |
|
| 2014 | - } |
|
| 2015 | - |
|
| 2016 | - |
|
| 2017 | - /** |
|
| 2018 | - * @deprecated |
|
| 2019 | - * @param EE_Line_Item $total_line_item |
|
| 2020 | - * @param EE_Transaction $transaction |
|
| 2021 | - * @return EE_Line_Item |
|
| 2022 | - * @throws EE_Error |
|
| 2023 | - * @throws InvalidArgumentException |
|
| 2024 | - * @throws InvalidDataTypeException |
|
| 2025 | - * @throws InvalidInterfaceException |
|
| 2026 | - * @throws ReflectionException |
|
| 2027 | - */ |
|
| 2028 | - public static function create_default_taxes_subtotal(EE_Line_Item $total_line_item, $transaction = null) |
|
| 2029 | - { |
|
| 2030 | - EE_Error::doing_it_wrong( |
|
| 2031 | - 'EEH_Line_Item::create_default_taxes_subtotal()', |
|
| 2032 | - sprintf( |
|
| 2033 | - esc_html__('Method replaced with %1$s', 'event_espresso'), |
|
| 2034 | - 'EEH_Line_Item::create_taxes_subtotal()' |
|
| 2035 | - ), |
|
| 2036 | - '4.6.0' |
|
| 2037 | - ); |
|
| 2038 | - return self::create_taxes_subtotal($total_line_item, $transaction); |
|
| 2039 | - } |
|
| 2040 | - |
|
| 2041 | - |
|
| 2042 | - /** |
|
| 2043 | - * @deprecated |
|
| 2044 | - * @param EE_Line_Item $total_line_item |
|
| 2045 | - * @param EE_Transaction $transaction |
|
| 2046 | - * @return EE_Line_Item |
|
| 2047 | - * @throws EE_Error |
|
| 2048 | - * @throws InvalidArgumentException |
|
| 2049 | - * @throws InvalidDataTypeException |
|
| 2050 | - * @throws InvalidInterfaceException |
|
| 2051 | - * @throws ReflectionException |
|
| 2052 | - */ |
|
| 2053 | - public static function create_default_event_subtotal(EE_Line_Item $total_line_item, $transaction = null) |
|
| 2054 | - { |
|
| 2055 | - EE_Error::doing_it_wrong( |
|
| 2056 | - 'EEH_Line_Item::create_default_event_subtotal()', |
|
| 2057 | - sprintf( |
|
| 2058 | - esc_html__('Method replaced with %1$s', 'event_espresso'), |
|
| 2059 | - 'EEH_Line_Item::create_event_subtotal()' |
|
| 2060 | - ), |
|
| 2061 | - '4.6.0' |
|
| 2062 | - ); |
|
| 2063 | - return self::create_event_subtotal($total_line_item, $transaction); |
|
| 2064 | - } |
|
| 24 | + /** |
|
| 25 | + * Adds a simple item (unrelated to any other model object) to the provided PARENT line item. |
|
| 26 | + * Does NOT automatically re-calculate the line item totals or update the related transaction. |
|
| 27 | + * You should call recalculate_total_including_taxes() on the grant total line item after this |
|
| 28 | + * to update the subtotals, and EE_Registration_Processor::calculate_reg_final_prices_per_line_item() |
|
| 29 | + * to keep the registration final prices in-sync with the transaction's total. |
|
| 30 | + * |
|
| 31 | + * @param EE_Line_Item $parent_line_item |
|
| 32 | + * @param string $name |
|
| 33 | + * @param float $unit_price |
|
| 34 | + * @param string $description |
|
| 35 | + * @param int $quantity |
|
| 36 | + * @param boolean $taxable |
|
| 37 | + * @param boolean $code if set to a value, ensures there is only one line item with that code |
|
| 38 | + * @return boolean success |
|
| 39 | + * @throws EE_Error |
|
| 40 | + * @throws InvalidArgumentException |
|
| 41 | + * @throws InvalidDataTypeException |
|
| 42 | + * @throws InvalidInterfaceException |
|
| 43 | + * @throws ReflectionException |
|
| 44 | + */ |
|
| 45 | + public static function add_unrelated_item( |
|
| 46 | + EE_Line_Item $parent_line_item, |
|
| 47 | + $name, |
|
| 48 | + $unit_price, |
|
| 49 | + $description = '', |
|
| 50 | + $quantity = 1, |
|
| 51 | + $taxable = false, |
|
| 52 | + $code = null |
|
| 53 | + ) { |
|
| 54 | + $items_subtotal = self::get_pre_tax_subtotal($parent_line_item); |
|
| 55 | + $line_item = EE_Line_Item::new_instance(array( |
|
| 56 | + 'LIN_name' => $name, |
|
| 57 | + 'LIN_desc' => $description, |
|
| 58 | + 'LIN_unit_price' => $unit_price, |
|
| 59 | + 'LIN_quantity' => $quantity, |
|
| 60 | + 'LIN_percent' => null, |
|
| 61 | + 'LIN_is_taxable' => $taxable, |
|
| 62 | + 'LIN_order' => $items_subtotal instanceof EE_Line_Item ? count($items_subtotal->children()) : 0, |
|
| 63 | + 'LIN_total' => (float) $unit_price * (int) $quantity, |
|
| 64 | + 'LIN_type' => EEM_Line_Item::type_line_item, |
|
| 65 | + 'LIN_code' => $code, |
|
| 66 | + )); |
|
| 67 | + $line_item = apply_filters( |
|
| 68 | + 'FHEE__EEH_Line_Item__add_unrelated_item__line_item', |
|
| 69 | + $line_item, |
|
| 70 | + $parent_line_item |
|
| 71 | + ); |
|
| 72 | + return self::add_item($parent_line_item, $line_item); |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * Adds a simple item ( unrelated to any other model object) to the total line item, |
|
| 78 | + * in the correct spot in the line item tree. Does not automatically |
|
| 79 | + * re-calculate the line item totals, nor update the related transaction, nor upgrade the transaction's |
|
| 80 | + * registrations' final prices (which should probably change because of this). |
|
| 81 | + * You should call recalculate_total_including_taxes() on the grand total line item, then |
|
| 82 | + * update the transaction's total, and EE_Registration_Processor::update_registration_final_prices() |
|
| 83 | + * after using this, to keep the registration final prices in-sync with the transaction's total. |
|
| 84 | + * |
|
| 85 | + * @param EE_Line_Item $parent_line_item |
|
| 86 | + * @param string $name |
|
| 87 | + * @param float $percentage_amount |
|
| 88 | + * @param string $description |
|
| 89 | + * @param boolean $taxable |
|
| 90 | + * @return boolean success |
|
| 91 | + * @throws EE_Error |
|
| 92 | + */ |
|
| 93 | + public static function add_percentage_based_item( |
|
| 94 | + EE_Line_Item $parent_line_item, |
|
| 95 | + $name, |
|
| 96 | + $percentage_amount, |
|
| 97 | + $description = '', |
|
| 98 | + $taxable = false |
|
| 99 | + ) { |
|
| 100 | + $line_item = EE_Line_Item::new_instance(array( |
|
| 101 | + 'LIN_name' => $name, |
|
| 102 | + 'LIN_desc' => $description, |
|
| 103 | + 'LIN_unit_price' => 0, |
|
| 104 | + 'LIN_percent' => $percentage_amount, |
|
| 105 | + 'LIN_quantity' => 1, |
|
| 106 | + 'LIN_is_taxable' => $taxable, |
|
| 107 | + 'LIN_total' => (float) ($percentage_amount * ($parent_line_item->total() / 100)), |
|
| 108 | + 'LIN_type' => EEM_Line_Item::type_line_item, |
|
| 109 | + 'LIN_parent' => $parent_line_item->ID(), |
|
| 110 | + )); |
|
| 111 | + $line_item = apply_filters( |
|
| 112 | + 'FHEE__EEH_Line_Item__add_percentage_based_item__line_item', |
|
| 113 | + $line_item |
|
| 114 | + ); |
|
| 115 | + return $parent_line_item->add_child_line_item($line_item, false); |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * Returns the new line item created by adding a purchase of the ticket |
|
| 121 | + * ensures that ticket line item is saved, and that cart total has been recalculated. |
|
| 122 | + * If this ticket has already been purchased, just increments its count. |
|
| 123 | + * Automatically re-calculates the line item totals and updates the related transaction. But |
|
| 124 | + * DOES NOT automatically upgrade the transaction's registrations' final prices (which |
|
| 125 | + * should probably change because of this). |
|
| 126 | + * You should call EE_Registration_Processor::calculate_reg_final_prices_per_line_item() |
|
| 127 | + * after using this, to keep the registration final prices in-sync with the transaction's total. |
|
| 128 | + * |
|
| 129 | + * @param EE_Line_Item $total_line_item grand total line item of type EEM_Line_Item::type_total |
|
| 130 | + * @param EE_Ticket $ticket |
|
| 131 | + * @param int $qty |
|
| 132 | + * @return EE_Line_Item |
|
| 133 | + * @throws EE_Error |
|
| 134 | + * @throws InvalidArgumentException |
|
| 135 | + * @throws InvalidDataTypeException |
|
| 136 | + * @throws InvalidInterfaceException |
|
| 137 | + * @throws ReflectionException |
|
| 138 | + */ |
|
| 139 | + public static function add_ticket_purchase(EE_Line_Item $total_line_item, EE_Ticket $ticket, $qty = 1) |
|
| 140 | + { |
|
| 141 | + if (! $total_line_item instanceof EE_Line_Item || ! $total_line_item->is_total()) { |
|
| 142 | + throw new EE_Error( |
|
| 143 | + sprintf( |
|
| 144 | + esc_html__( |
|
| 145 | + 'A valid line item total is required in order to add tickets. A line item of type "%s" was passed.', |
|
| 146 | + 'event_espresso' |
|
| 147 | + ), |
|
| 148 | + $ticket->ID(), |
|
| 149 | + $total_line_item->ID() |
|
| 150 | + ) |
|
| 151 | + ); |
|
| 152 | + } |
|
| 153 | + // either increment the qty for an existing ticket |
|
| 154 | + $line_item = self::increment_ticket_qty_if_already_in_cart($total_line_item, $ticket, $qty); |
|
| 155 | + // or add a new one |
|
| 156 | + if (! $line_item instanceof EE_Line_Item) { |
|
| 157 | + $line_item = self::create_ticket_line_item($total_line_item, $ticket, $qty); |
|
| 158 | + } |
|
| 159 | + $total_line_item->recalculate_total_including_taxes(); |
|
| 160 | + return $line_item; |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + |
|
| 164 | + /** |
|
| 165 | + * Returns the new line item created by adding a purchase of the ticket |
|
| 166 | + * |
|
| 167 | + * @param EE_Line_Item $total_line_item |
|
| 168 | + * @param EE_Ticket $ticket |
|
| 169 | + * @param int $qty |
|
| 170 | + * @return EE_Line_Item |
|
| 171 | + * @throws EE_Error |
|
| 172 | + * @throws InvalidArgumentException |
|
| 173 | + * @throws InvalidDataTypeException |
|
| 174 | + * @throws InvalidInterfaceException |
|
| 175 | + * @throws ReflectionException |
|
| 176 | + */ |
|
| 177 | + public static function increment_ticket_qty_if_already_in_cart( |
|
| 178 | + EE_Line_Item $total_line_item, |
|
| 179 | + EE_Ticket $ticket, |
|
| 180 | + $qty = 1 |
|
| 181 | + ) { |
|
| 182 | + $line_item = null; |
|
| 183 | + if ($total_line_item instanceof EE_Line_Item && $total_line_item->is_total()) { |
|
| 184 | + $ticket_line_items = EEH_Line_Item::get_ticket_line_items($total_line_item); |
|
| 185 | + foreach ((array) $ticket_line_items as $ticket_line_item) { |
|
| 186 | + if ($ticket_line_item instanceof EE_Line_Item |
|
| 187 | + && (int) $ticket_line_item->OBJ_ID() === (int) $ticket->ID() |
|
| 188 | + ) { |
|
| 189 | + $line_item = $ticket_line_item; |
|
| 190 | + break; |
|
| 191 | + } |
|
| 192 | + } |
|
| 193 | + } |
|
| 194 | + if ($line_item instanceof EE_Line_Item) { |
|
| 195 | + EEH_Line_Item::increment_quantity($line_item, $qty); |
|
| 196 | + return $line_item; |
|
| 197 | + } |
|
| 198 | + return null; |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + |
|
| 202 | + /** |
|
| 203 | + * Increments the line item and all its children's quantity by $qty (but percent line items are unaffected). |
|
| 204 | + * Does NOT save or recalculate other line items totals |
|
| 205 | + * |
|
| 206 | + * @param EE_Line_Item $line_item |
|
| 207 | + * @param int $qty |
|
| 208 | + * @return void |
|
| 209 | + * @throws EE_Error |
|
| 210 | + * @throws InvalidArgumentException |
|
| 211 | + * @throws InvalidDataTypeException |
|
| 212 | + * @throws InvalidInterfaceException |
|
| 213 | + * @throws ReflectionException |
|
| 214 | + */ |
|
| 215 | + public static function increment_quantity(EE_Line_Item $line_item, $qty = 1) |
|
| 216 | + { |
|
| 217 | + if (! $line_item->is_percent()) { |
|
| 218 | + $qty += $line_item->quantity(); |
|
| 219 | + $line_item->set_quantity($qty); |
|
| 220 | + $line_item->set_total($line_item->unit_price() * $qty); |
|
| 221 | + $line_item->save(); |
|
| 222 | + } |
|
| 223 | + foreach ($line_item->children() as $child) { |
|
| 224 | + if ($child->is_sub_line_item()) { |
|
| 225 | + EEH_Line_Item::update_quantity($child, $qty); |
|
| 226 | + } |
|
| 227 | + } |
|
| 228 | + } |
|
| 229 | + |
|
| 230 | + |
|
| 231 | + /** |
|
| 232 | + * Decrements the line item and all its children's quantity by $qty (but percent line items are unaffected). |
|
| 233 | + * Does NOT save or recalculate other line items totals |
|
| 234 | + * |
|
| 235 | + * @param EE_Line_Item $line_item |
|
| 236 | + * @param int $qty |
|
| 237 | + * @return void |
|
| 238 | + * @throws EE_Error |
|
| 239 | + * @throws InvalidArgumentException |
|
| 240 | + * @throws InvalidDataTypeException |
|
| 241 | + * @throws InvalidInterfaceException |
|
| 242 | + * @throws ReflectionException |
|
| 243 | + */ |
|
| 244 | + public static function decrement_quantity(EE_Line_Item $line_item, $qty = 1) |
|
| 245 | + { |
|
| 246 | + if (! $line_item->is_percent()) { |
|
| 247 | + $qty = $line_item->quantity() - $qty; |
|
| 248 | + $qty = max($qty, 0); |
|
| 249 | + $line_item->set_quantity($qty); |
|
| 250 | + $line_item->set_total($line_item->unit_price() * $qty); |
|
| 251 | + $line_item->save(); |
|
| 252 | + } |
|
| 253 | + foreach ($line_item->children() as $child) { |
|
| 254 | + if ($child->is_sub_line_item()) { |
|
| 255 | + EEH_Line_Item::update_quantity($child, $qty); |
|
| 256 | + } |
|
| 257 | + } |
|
| 258 | + } |
|
| 259 | + |
|
| 260 | + |
|
| 261 | + /** |
|
| 262 | + * Updates the line item and its children's quantities to the specified number. |
|
| 263 | + * Does NOT save them or recalculate totals. |
|
| 264 | + * |
|
| 265 | + * @param EE_Line_Item $line_item |
|
| 266 | + * @param int $new_quantity |
|
| 267 | + * @throws EE_Error |
|
| 268 | + * @throws InvalidArgumentException |
|
| 269 | + * @throws InvalidDataTypeException |
|
| 270 | + * @throws InvalidInterfaceException |
|
| 271 | + * @throws ReflectionException |
|
| 272 | + */ |
|
| 273 | + public static function update_quantity(EE_Line_Item $line_item, $new_quantity) |
|
| 274 | + { |
|
| 275 | + if (! $line_item->is_percent()) { |
|
| 276 | + $line_item->set_quantity($new_quantity); |
|
| 277 | + $line_item->set_total($line_item->unit_price() * $new_quantity); |
|
| 278 | + $line_item->save(); |
|
| 279 | + } |
|
| 280 | + foreach ($line_item->children() as $child) { |
|
| 281 | + if ($child->is_sub_line_item()) { |
|
| 282 | + EEH_Line_Item::update_quantity($child, $new_quantity); |
|
| 283 | + } |
|
| 284 | + } |
|
| 285 | + } |
|
| 286 | + |
|
| 287 | + |
|
| 288 | + /** |
|
| 289 | + * Returns the new line item created by adding a purchase of the ticket |
|
| 290 | + * |
|
| 291 | + * @param EE_Line_Item $total_line_item of type EEM_Line_Item::type_total |
|
| 292 | + * @param EE_Ticket $ticket |
|
| 293 | + * @param int $qty |
|
| 294 | + * @return EE_Line_Item |
|
| 295 | + * @throws EE_Error |
|
| 296 | + * @throws InvalidArgumentException |
|
| 297 | + * @throws InvalidDataTypeException |
|
| 298 | + * @throws InvalidInterfaceException |
|
| 299 | + * @throws ReflectionException |
|
| 300 | + */ |
|
| 301 | + public static function create_ticket_line_item(EE_Line_Item $total_line_item, EE_Ticket $ticket, $qty = 1) |
|
| 302 | + { |
|
| 303 | + $datetimes = $ticket->datetimes(); |
|
| 304 | + $first_datetime = reset($datetimes); |
|
| 305 | + $first_datetime_name = esc_html__('Event', 'event_espresso'); |
|
| 306 | + if ($first_datetime instanceof EE_Datetime && $first_datetime->event() instanceof EE_Event) { |
|
| 307 | + $first_datetime_name = $first_datetime->event()->name(); |
|
| 308 | + } |
|
| 309 | + $event = sprintf(_x('(For %1$s)', '(For Event Name)', 'event_espresso'), $first_datetime_name); |
|
| 310 | + // get event subtotal line |
|
| 311 | + $events_sub_total = self::get_event_line_item_for_ticket($total_line_item, $ticket); |
|
| 312 | + // add $ticket to cart |
|
| 313 | + $line_item = EE_Line_Item::new_instance(array( |
|
| 314 | + 'LIN_name' => $ticket->name(), |
|
| 315 | + 'LIN_desc' => $ticket->description() !== '' ? $ticket->description() . ' ' . $event : $event, |
|
| 316 | + 'LIN_unit_price' => $ticket->price(), |
|
| 317 | + 'LIN_quantity' => $qty, |
|
| 318 | + 'LIN_is_taxable' => $ticket->taxable(), |
|
| 319 | + 'LIN_order' => count($events_sub_total->children()), |
|
| 320 | + 'LIN_total' => $ticket->price() * $qty, |
|
| 321 | + 'LIN_type' => EEM_Line_Item::type_line_item, |
|
| 322 | + 'OBJ_ID' => $ticket->ID(), |
|
| 323 | + 'OBJ_type' => EEM_Line_Item::OBJ_TYPE_TICKET, |
|
| 324 | + )); |
|
| 325 | + $line_item = apply_filters( |
|
| 326 | + 'FHEE__EEH_Line_Item__create_ticket_line_item__line_item', |
|
| 327 | + $line_item |
|
| 328 | + ); |
|
| 329 | + $events_sub_total->add_child_line_item($line_item); |
|
| 330 | + // now add the sub-line items |
|
| 331 | + $running_total_for_ticket = 0; |
|
| 332 | + foreach ($ticket->prices(array('order_by' => array('PRC_order' => 'ASC'))) as $price) { |
|
| 333 | + $sign = $price->is_discount() ? -1 : 1; |
|
| 334 | + $price_total = $price->is_percent() |
|
| 335 | + ? $running_total_for_ticket * $price->amount() / 100 |
|
| 336 | + : $price->amount() * $qty; |
|
| 337 | + $sub_line_item = EE_Line_Item::new_instance(array( |
|
| 338 | + 'LIN_name' => $price->name(), |
|
| 339 | + 'LIN_desc' => $price->desc(), |
|
| 340 | + 'LIN_quantity' => $price->is_percent() ? null : $qty, |
|
| 341 | + 'LIN_is_taxable' => false, |
|
| 342 | + 'LIN_order' => $price->order(), |
|
| 343 | + 'LIN_total' => $sign * $price_total, |
|
| 344 | + 'LIN_type' => EEM_Line_Item::type_sub_line_item, |
|
| 345 | + 'OBJ_ID' => $price->ID(), |
|
| 346 | + 'OBJ_type' => EEM_Line_Item::OBJ_TYPE_PRICE, |
|
| 347 | + )); |
|
| 348 | + $sub_line_item = apply_filters( |
|
| 349 | + 'FHEE__EEH_Line_Item__create_ticket_line_item__sub_line_item', |
|
| 350 | + $sub_line_item |
|
| 351 | + ); |
|
| 352 | + if ($price->is_percent()) { |
|
| 353 | + $sub_line_item->set_percent($sign * $price->amount()); |
|
| 354 | + } else { |
|
| 355 | + $sub_line_item->set_unit_price($sign * $price->amount()); |
|
| 356 | + } |
|
| 357 | + $running_total_for_ticket += $price_total; |
|
| 358 | + $line_item->add_child_line_item($sub_line_item); |
|
| 359 | + } |
|
| 360 | + return $line_item; |
|
| 361 | + } |
|
| 362 | + |
|
| 363 | + |
|
| 364 | + /** |
|
| 365 | + * Adds the specified item under the pre-tax-sub-total line item. Automatically |
|
| 366 | + * re-calculates the line item totals and updates the related transaction. But |
|
| 367 | + * DOES NOT automatically upgrade the transaction's registrations' final prices (which |
|
| 368 | + * should probably change because of this). |
|
| 369 | + * You should call EE_Registration_Processor::calculate_reg_final_prices_per_line_item() |
|
| 370 | + * after using this, to keep the registration final prices in-sync with the transaction's total. |
|
| 371 | + * |
|
| 372 | + * @param EE_Line_Item $total_line_item |
|
| 373 | + * @param EE_Line_Item $item to be added |
|
| 374 | + * @return boolean |
|
| 375 | + * @throws EE_Error |
|
| 376 | + * @throws InvalidArgumentException |
|
| 377 | + * @throws InvalidDataTypeException |
|
| 378 | + * @throws InvalidInterfaceException |
|
| 379 | + * @throws ReflectionException |
|
| 380 | + */ |
|
| 381 | + public static function add_item(EE_Line_Item $total_line_item, EE_Line_Item $item) |
|
| 382 | + { |
|
| 383 | + $pre_tax_subtotal = self::get_pre_tax_subtotal($total_line_item); |
|
| 384 | + if ($pre_tax_subtotal instanceof EE_Line_Item) { |
|
| 385 | + $success = $pre_tax_subtotal->add_child_line_item($item); |
|
| 386 | + } else { |
|
| 387 | + return false; |
|
| 388 | + } |
|
| 389 | + $total_line_item->recalculate_total_including_taxes(); |
|
| 390 | + return $success; |
|
| 391 | + } |
|
| 392 | + |
|
| 393 | + |
|
| 394 | + /** |
|
| 395 | + * cancels an existing ticket line item, |
|
| 396 | + * by decrementing it's quantity by 1 and adding a new "type_cancellation" sub-line-item. |
|
| 397 | + * ALL totals and subtotals will NEED TO BE UPDATED after performing this action |
|
| 398 | + * |
|
| 399 | + * @param EE_Line_Item $ticket_line_item |
|
| 400 | + * @param int $qty |
|
| 401 | + * @return bool success |
|
| 402 | + * @throws EE_Error |
|
| 403 | + * @throws InvalidArgumentException |
|
| 404 | + * @throws InvalidDataTypeException |
|
| 405 | + * @throws InvalidInterfaceException |
|
| 406 | + * @throws ReflectionException |
|
| 407 | + */ |
|
| 408 | + public static function cancel_ticket_line_item(EE_Line_Item $ticket_line_item, $qty = 1) |
|
| 409 | + { |
|
| 410 | + // validate incoming line_item |
|
| 411 | + if ($ticket_line_item->OBJ_type() !== EEM_Line_Item::OBJ_TYPE_TICKET) { |
|
| 412 | + throw new EE_Error( |
|
| 413 | + sprintf( |
|
| 414 | + esc_html__( |
|
| 415 | + 'The supplied line item must have an Object Type of "Ticket", not %1$s.', |
|
| 416 | + 'event_espresso' |
|
| 417 | + ), |
|
| 418 | + $ticket_line_item->type() |
|
| 419 | + ) |
|
| 420 | + ); |
|
| 421 | + } |
|
| 422 | + if ($ticket_line_item->quantity() < $qty) { |
|
| 423 | + throw new EE_Error( |
|
| 424 | + sprintf( |
|
| 425 | + esc_html__( |
|
| 426 | + 'Can not cancel %1$d ticket(s) because the supplied line item has a quantity of %2$d.', |
|
| 427 | + 'event_espresso' |
|
| 428 | + ), |
|
| 429 | + $qty, |
|
| 430 | + $ticket_line_item->quantity() |
|
| 431 | + ) |
|
| 432 | + ); |
|
| 433 | + } |
|
| 434 | + // decrement ticket quantity; don't rely on auto-fixing when recalculating totals to do this |
|
| 435 | + $ticket_line_item->set_quantity($ticket_line_item->quantity() - $qty); |
|
| 436 | + foreach ($ticket_line_item->children() as $child_line_item) { |
|
| 437 | + if ($child_line_item->is_sub_line_item() |
|
| 438 | + && ! $child_line_item->is_percent() |
|
| 439 | + && ! $child_line_item->is_cancellation() |
|
| 440 | + ) { |
|
| 441 | + $child_line_item->set_quantity($child_line_item->quantity() - $qty); |
|
| 442 | + } |
|
| 443 | + } |
|
| 444 | + // get cancellation sub line item |
|
| 445 | + $cancellation_line_item = EEH_Line_Item::get_descendants_of_type( |
|
| 446 | + $ticket_line_item, |
|
| 447 | + EEM_Line_Item::type_cancellation |
|
| 448 | + ); |
|
| 449 | + $cancellation_line_item = reset($cancellation_line_item); |
|
| 450 | + // verify that this ticket was indeed previously cancelled |
|
| 451 | + if ($cancellation_line_item instanceof EE_Line_Item) { |
|
| 452 | + // increment cancelled quantity |
|
| 453 | + $cancellation_line_item->set_quantity($cancellation_line_item->quantity() + $qty); |
|
| 454 | + } else { |
|
| 455 | + // create cancellation sub line item |
|
| 456 | + $cancellation_line_item = EE_Line_Item::new_instance(array( |
|
| 457 | + 'LIN_name' => esc_html__('Cancellation', 'event_espresso'), |
|
| 458 | + 'LIN_desc' => sprintf( |
|
| 459 | + esc_html_x( |
|
| 460 | + 'Cancelled %1$s : %2$s', |
|
| 461 | + 'Cancelled Ticket Name : 2015-01-01 11:11', |
|
| 462 | + 'event_espresso' |
|
| 463 | + ), |
|
| 464 | + $ticket_line_item->name(), |
|
| 465 | + current_time(get_option('date_format') . ' ' . get_option('time_format')) |
|
| 466 | + ), |
|
| 467 | + 'LIN_unit_price' => 0, // $ticket_line_item->unit_price() |
|
| 468 | + 'LIN_quantity' => $qty, |
|
| 469 | + 'LIN_is_taxable' => $ticket_line_item->is_taxable(), |
|
| 470 | + 'LIN_order' => count($ticket_line_item->children()), |
|
| 471 | + 'LIN_total' => 0, // $ticket_line_item->unit_price() |
|
| 472 | + 'LIN_type' => EEM_Line_Item::type_cancellation, |
|
| 473 | + )); |
|
| 474 | + $ticket_line_item->add_child_line_item($cancellation_line_item); |
|
| 475 | + } |
|
| 476 | + if ($ticket_line_item->save_this_and_descendants() > 0) { |
|
| 477 | + // decrement parent line item quantity |
|
| 478 | + $event_line_item = $ticket_line_item->parent(); |
|
| 479 | + if ($event_line_item instanceof EE_Line_Item |
|
| 480 | + && $event_line_item->OBJ_type() === EEM_Line_Item::OBJ_TYPE_EVENT |
|
| 481 | + ) { |
|
| 482 | + $event_line_item->set_quantity($event_line_item->quantity() - $qty); |
|
| 483 | + $event_line_item->save(); |
|
| 484 | + } |
|
| 485 | + EEH_Line_Item::get_grand_total_and_recalculate_everything($ticket_line_item); |
|
| 486 | + return true; |
|
| 487 | + } |
|
| 488 | + return false; |
|
| 489 | + } |
|
| 490 | + |
|
| 491 | + |
|
| 492 | + /** |
|
| 493 | + * reinstates (un-cancels?) a previously canceled ticket line item, |
|
| 494 | + * by incrementing it's quantity by 1, and decrementing it's "type_cancellation" sub-line-item. |
|
| 495 | + * ALL totals and subtotals will NEED TO BE UPDATED after performing this action |
|
| 496 | + * |
|
| 497 | + * @param EE_Line_Item $ticket_line_item |
|
| 498 | + * @param int $qty |
|
| 499 | + * @return bool success |
|
| 500 | + * @throws EE_Error |
|
| 501 | + * @throws InvalidArgumentException |
|
| 502 | + * @throws InvalidDataTypeException |
|
| 503 | + * @throws InvalidInterfaceException |
|
| 504 | + * @throws ReflectionException |
|
| 505 | + */ |
|
| 506 | + public static function reinstate_canceled_ticket_line_item(EE_Line_Item $ticket_line_item, $qty = 1) |
|
| 507 | + { |
|
| 508 | + // validate incoming line_item |
|
| 509 | + if ($ticket_line_item->OBJ_type() !== EEM_Line_Item::OBJ_TYPE_TICKET) { |
|
| 510 | + throw new EE_Error( |
|
| 511 | + sprintf( |
|
| 512 | + esc_html__( |
|
| 513 | + 'The supplied line item must have an Object Type of "Ticket", not %1$s.', |
|
| 514 | + 'event_espresso' |
|
| 515 | + ), |
|
| 516 | + $ticket_line_item->type() |
|
| 517 | + ) |
|
| 518 | + ); |
|
| 519 | + } |
|
| 520 | + // get cancellation sub line item |
|
| 521 | + $cancellation_line_item = EEH_Line_Item::get_descendants_of_type( |
|
| 522 | + $ticket_line_item, |
|
| 523 | + EEM_Line_Item::type_cancellation |
|
| 524 | + ); |
|
| 525 | + $cancellation_line_item = reset($cancellation_line_item); |
|
| 526 | + // verify that this ticket was indeed previously cancelled |
|
| 527 | + if (! $cancellation_line_item instanceof EE_Line_Item) { |
|
| 528 | + return false; |
|
| 529 | + } |
|
| 530 | + if ($cancellation_line_item->quantity() > $qty) { |
|
| 531 | + // decrement cancelled quantity |
|
| 532 | + $cancellation_line_item->set_quantity($cancellation_line_item->quantity() - $qty); |
|
| 533 | + } elseif ($cancellation_line_item->quantity() === $qty) { |
|
| 534 | + // decrement cancelled quantity in case anyone still has the object kicking around |
|
| 535 | + $cancellation_line_item->set_quantity($cancellation_line_item->quantity() - $qty); |
|
| 536 | + // delete because quantity will end up as 0 |
|
| 537 | + $cancellation_line_item->delete(); |
|
| 538 | + // and attempt to destroy the object, |
|
| 539 | + // even though PHP won't actually destroy it until it needs the memory |
|
| 540 | + unset($cancellation_line_item); |
|
| 541 | + } else { |
|
| 542 | + // what ?!?! negative quantity ?!?! |
|
| 543 | + throw new EE_Error( |
|
| 544 | + sprintf( |
|
| 545 | + esc_html__( |
|
| 546 | + 'Can not reinstate %1$d cancelled ticket(s) because the cancelled ticket quantity is only %2$d.', |
|
| 547 | + 'event_espresso' |
|
| 548 | + ), |
|
| 549 | + $qty, |
|
| 550 | + $cancellation_line_item->quantity() |
|
| 551 | + ) |
|
| 552 | + ); |
|
| 553 | + } |
|
| 554 | + // increment ticket quantity |
|
| 555 | + $ticket_line_item->set_quantity($ticket_line_item->quantity() + $qty); |
|
| 556 | + if ($ticket_line_item->save_this_and_descendants() > 0) { |
|
| 557 | + // increment parent line item quantity |
|
| 558 | + $event_line_item = $ticket_line_item->parent(); |
|
| 559 | + if ($event_line_item instanceof EE_Line_Item |
|
| 560 | + && $event_line_item->OBJ_type() === EEM_Line_Item::OBJ_TYPE_EVENT |
|
| 561 | + ) { |
|
| 562 | + $event_line_item->set_quantity($event_line_item->quantity() + $qty); |
|
| 563 | + } |
|
| 564 | + EEH_Line_Item::get_grand_total_and_recalculate_everything($ticket_line_item); |
|
| 565 | + return true; |
|
| 566 | + } |
|
| 567 | + return false; |
|
| 568 | + } |
|
| 569 | + |
|
| 570 | + |
|
| 571 | + /** |
|
| 572 | + * calls EEH_Line_Item::find_transaction_grand_total_for_line_item() |
|
| 573 | + * then EE_Line_Item::recalculate_total_including_taxes() on the result |
|
| 574 | + * |
|
| 575 | + * @param EE_Line_Item $line_item |
|
| 576 | + * @return float |
|
| 577 | + * @throws EE_Error |
|
| 578 | + * @throws InvalidArgumentException |
|
| 579 | + * @throws InvalidDataTypeException |
|
| 580 | + * @throws InvalidInterfaceException |
|
| 581 | + * @throws ReflectionException |
|
| 582 | + */ |
|
| 583 | + public static function get_grand_total_and_recalculate_everything(EE_Line_Item $line_item) |
|
| 584 | + { |
|
| 585 | + $grand_total_line_item = EEH_Line_Item::find_transaction_grand_total_for_line_item($line_item); |
|
| 586 | + return $grand_total_line_item->recalculate_total_including_taxes(); |
|
| 587 | + } |
|
| 588 | + |
|
| 589 | + |
|
| 590 | + /** |
|
| 591 | + * Gets the line item which contains the subtotal of all the items |
|
| 592 | + * |
|
| 593 | + * @param EE_Line_Item $total_line_item of type EEM_Line_Item::type_total |
|
| 594 | + * @return EE_Line_Item |
|
| 595 | + * @throws EE_Error |
|
| 596 | + * @throws InvalidArgumentException |
|
| 597 | + * @throws InvalidDataTypeException |
|
| 598 | + * @throws InvalidInterfaceException |
|
| 599 | + * @throws ReflectionException |
|
| 600 | + */ |
|
| 601 | + public static function get_pre_tax_subtotal(EE_Line_Item $total_line_item) |
|
| 602 | + { |
|
| 603 | + $pre_tax_subtotal = $total_line_item->get_child_line_item('pre-tax-subtotal'); |
|
| 604 | + return $pre_tax_subtotal instanceof EE_Line_Item |
|
| 605 | + ? $pre_tax_subtotal |
|
| 606 | + : self::create_pre_tax_subtotal($total_line_item); |
|
| 607 | + } |
|
| 608 | + |
|
| 609 | + |
|
| 610 | + /** |
|
| 611 | + * Gets the line item for the taxes subtotal |
|
| 612 | + * |
|
| 613 | + * @param EE_Line_Item $total_line_item of type EEM_Line_Item::type_total |
|
| 614 | + * @return EE_Line_Item |
|
| 615 | + * @throws EE_Error |
|
| 616 | + * @throws InvalidArgumentException |
|
| 617 | + * @throws InvalidDataTypeException |
|
| 618 | + * @throws InvalidInterfaceException |
|
| 619 | + * @throws ReflectionException |
|
| 620 | + */ |
|
| 621 | + public static function get_taxes_subtotal(EE_Line_Item $total_line_item) |
|
| 622 | + { |
|
| 623 | + $taxes = $total_line_item->get_child_line_item('taxes'); |
|
| 624 | + return $taxes ? $taxes : self::create_taxes_subtotal($total_line_item); |
|
| 625 | + } |
|
| 626 | + |
|
| 627 | + |
|
| 628 | + /** |
|
| 629 | + * sets the TXN ID on an EE_Line_Item if passed a valid EE_Transaction object |
|
| 630 | + * |
|
| 631 | + * @param EE_Line_Item $line_item |
|
| 632 | + * @param EE_Transaction $transaction |
|
| 633 | + * @return void |
|
| 634 | + * @throws EE_Error |
|
| 635 | + * @throws InvalidArgumentException |
|
| 636 | + * @throws InvalidDataTypeException |
|
| 637 | + * @throws InvalidInterfaceException |
|
| 638 | + * @throws ReflectionException |
|
| 639 | + */ |
|
| 640 | + public static function set_TXN_ID(EE_Line_Item $line_item, $transaction = null) |
|
| 641 | + { |
|
| 642 | + if ($transaction) { |
|
| 643 | + /** @type EEM_Transaction $EEM_Transaction */ |
|
| 644 | + $EEM_Transaction = EE_Registry::instance()->load_model('Transaction'); |
|
| 645 | + $TXN_ID = $EEM_Transaction->ensure_is_ID($transaction); |
|
| 646 | + $line_item->set_TXN_ID($TXN_ID); |
|
| 647 | + } |
|
| 648 | + } |
|
| 649 | + |
|
| 650 | + |
|
| 651 | + /** |
|
| 652 | + * Creates a new default total line item for the transaction, |
|
| 653 | + * and its tickets subtotal and taxes subtotal line items (and adds the |
|
| 654 | + * existing taxes as children of the taxes subtotal line item) |
|
| 655 | + * |
|
| 656 | + * @param EE_Transaction $transaction |
|
| 657 | + * @return EE_Line_Item of type total |
|
| 658 | + * @throws EE_Error |
|
| 659 | + * @throws InvalidArgumentException |
|
| 660 | + * @throws InvalidDataTypeException |
|
| 661 | + * @throws InvalidInterfaceException |
|
| 662 | + * @throws ReflectionException |
|
| 663 | + */ |
|
| 664 | + public static function create_total_line_item($transaction = null) |
|
| 665 | + { |
|
| 666 | + $total_line_item = EE_Line_Item::new_instance(array( |
|
| 667 | + 'LIN_code' => 'total', |
|
| 668 | + 'LIN_name' => esc_html__('Grand Total', 'event_espresso'), |
|
| 669 | + 'LIN_type' => EEM_Line_Item::type_total, |
|
| 670 | + 'OBJ_type' => EEM_Line_Item::OBJ_TYPE_TRANSACTION, |
|
| 671 | + )); |
|
| 672 | + $total_line_item = apply_filters( |
|
| 673 | + 'FHEE__EEH_Line_Item__create_total_line_item__total_line_item', |
|
| 674 | + $total_line_item |
|
| 675 | + ); |
|
| 676 | + self::set_TXN_ID($total_line_item, $transaction); |
|
| 677 | + self::create_pre_tax_subtotal($total_line_item, $transaction); |
|
| 678 | + self::create_taxes_subtotal($total_line_item, $transaction); |
|
| 679 | + return $total_line_item; |
|
| 680 | + } |
|
| 681 | + |
|
| 682 | + |
|
| 683 | + /** |
|
| 684 | + * Creates a default items subtotal line item |
|
| 685 | + * |
|
| 686 | + * @param EE_Line_Item $total_line_item |
|
| 687 | + * @param EE_Transaction $transaction |
|
| 688 | + * @return EE_Line_Item |
|
| 689 | + * @throws EE_Error |
|
| 690 | + * @throws InvalidArgumentException |
|
| 691 | + * @throws InvalidDataTypeException |
|
| 692 | + * @throws InvalidInterfaceException |
|
| 693 | + * @throws ReflectionException |
|
| 694 | + */ |
|
| 695 | + protected static function create_pre_tax_subtotal(EE_Line_Item $total_line_item, $transaction = null) |
|
| 696 | + { |
|
| 697 | + $pre_tax_line_item = EE_Line_Item::new_instance(array( |
|
| 698 | + 'LIN_code' => 'pre-tax-subtotal', |
|
| 699 | + 'LIN_name' => esc_html__('Pre-Tax Subtotal', 'event_espresso'), |
|
| 700 | + 'LIN_type' => EEM_Line_Item::type_sub_total, |
|
| 701 | + )); |
|
| 702 | + $pre_tax_line_item = apply_filters( |
|
| 703 | + 'FHEE__EEH_Line_Item__create_pre_tax_subtotal__pre_tax_line_item', |
|
| 704 | + $pre_tax_line_item |
|
| 705 | + ); |
|
| 706 | + self::set_TXN_ID($pre_tax_line_item, $transaction); |
|
| 707 | + $total_line_item->add_child_line_item($pre_tax_line_item); |
|
| 708 | + self::create_event_subtotal($pre_tax_line_item, $transaction); |
|
| 709 | + return $pre_tax_line_item; |
|
| 710 | + } |
|
| 711 | + |
|
| 712 | + |
|
| 713 | + /** |
|
| 714 | + * Creates a line item for the taxes subtotal and finds all the tax prices |
|
| 715 | + * and applies taxes to it |
|
| 716 | + * |
|
| 717 | + * @param EE_Line_Item $total_line_item of type EEM_Line_Item::type_total |
|
| 718 | + * @param EE_Transaction $transaction |
|
| 719 | + * @return EE_Line_Item |
|
| 720 | + * @throws EE_Error |
|
| 721 | + * @throws InvalidArgumentException |
|
| 722 | + * @throws InvalidDataTypeException |
|
| 723 | + * @throws InvalidInterfaceException |
|
| 724 | + * @throws ReflectionException |
|
| 725 | + */ |
|
| 726 | + protected static function create_taxes_subtotal(EE_Line_Item $total_line_item, $transaction = null) |
|
| 727 | + { |
|
| 728 | + $tax_line_item = EE_Line_Item::new_instance(array( |
|
| 729 | + 'LIN_code' => 'taxes', |
|
| 730 | + 'LIN_name' => esc_html__('Taxes', 'event_espresso'), |
|
| 731 | + 'LIN_type' => EEM_Line_Item::type_tax_sub_total, |
|
| 732 | + 'LIN_order' => 1000,// this should always come last |
|
| 733 | + )); |
|
| 734 | + $tax_line_item = apply_filters( |
|
| 735 | + 'FHEE__EEH_Line_Item__create_taxes_subtotal__tax_line_item', |
|
| 736 | + $tax_line_item |
|
| 737 | + ); |
|
| 738 | + self::set_TXN_ID($tax_line_item, $transaction); |
|
| 739 | + $total_line_item->add_child_line_item($tax_line_item); |
|
| 740 | + // and lastly, add the actual taxes |
|
| 741 | + self::apply_taxes($total_line_item); |
|
| 742 | + return $tax_line_item; |
|
| 743 | + } |
|
| 744 | + |
|
| 745 | + |
|
| 746 | + /** |
|
| 747 | + * Creates a default items subtotal line item |
|
| 748 | + * |
|
| 749 | + * @param EE_Line_Item $pre_tax_line_item |
|
| 750 | + * @param EE_Transaction $transaction |
|
| 751 | + * @param EE_Event $event |
|
| 752 | + * @return EE_Line_Item |
|
| 753 | + * @throws EE_Error |
|
| 754 | + * @throws InvalidArgumentException |
|
| 755 | + * @throws InvalidDataTypeException |
|
| 756 | + * @throws InvalidInterfaceException |
|
| 757 | + * @throws ReflectionException |
|
| 758 | + */ |
|
| 759 | + public static function create_event_subtotal(EE_Line_Item $pre_tax_line_item, $transaction = null, $event = null) |
|
| 760 | + { |
|
| 761 | + $event_line_item = EE_Line_Item::new_instance(array( |
|
| 762 | + 'LIN_code' => self::get_event_code($event), |
|
| 763 | + 'LIN_name' => self::get_event_name($event), |
|
| 764 | + 'LIN_desc' => self::get_event_desc($event), |
|
| 765 | + 'LIN_type' => EEM_Line_Item::type_sub_total, |
|
| 766 | + 'OBJ_type' => EEM_Line_Item::OBJ_TYPE_EVENT, |
|
| 767 | + 'OBJ_ID' => $event instanceof EE_Event ? $event->ID() : 0, |
|
| 768 | + )); |
|
| 769 | + $event_line_item = apply_filters( |
|
| 770 | + 'FHEE__EEH_Line_Item__create_event_subtotal__event_line_item', |
|
| 771 | + $event_line_item |
|
| 772 | + ); |
|
| 773 | + self::set_TXN_ID($event_line_item, $transaction); |
|
| 774 | + $pre_tax_line_item->add_child_line_item($event_line_item); |
|
| 775 | + return $event_line_item; |
|
| 776 | + } |
|
| 777 | + |
|
| 778 | + |
|
| 779 | + /** |
|
| 780 | + * Gets what the event ticket's code SHOULD be |
|
| 781 | + * |
|
| 782 | + * @param EE_Event $event |
|
| 783 | + * @return string |
|
| 784 | + * @throws EE_Error |
|
| 785 | + */ |
|
| 786 | + public static function get_event_code($event) |
|
| 787 | + { |
|
| 788 | + return 'event-' . ($event instanceof EE_Event ? $event->ID() : '0'); |
|
| 789 | + } |
|
| 790 | + |
|
| 791 | + |
|
| 792 | + /** |
|
| 793 | + * Gets the event name |
|
| 794 | + * |
|
| 795 | + * @param EE_Event $event |
|
| 796 | + * @return string |
|
| 797 | + * @throws EE_Error |
|
| 798 | + */ |
|
| 799 | + public static function get_event_name($event) |
|
| 800 | + { |
|
| 801 | + return $event instanceof EE_Event |
|
| 802 | + ? mb_substr($event->name(), 0, 245) |
|
| 803 | + : esc_html__('Event', 'event_espresso'); |
|
| 804 | + } |
|
| 805 | + |
|
| 806 | + |
|
| 807 | + /** |
|
| 808 | + * Gets the event excerpt |
|
| 809 | + * |
|
| 810 | + * @param EE_Event $event |
|
| 811 | + * @return string |
|
| 812 | + * @throws EE_Error |
|
| 813 | + */ |
|
| 814 | + public static function get_event_desc($event) |
|
| 815 | + { |
|
| 816 | + return $event instanceof EE_Event ? $event->short_description() : ''; |
|
| 817 | + } |
|
| 818 | + |
|
| 819 | + |
|
| 820 | + /** |
|
| 821 | + * Given the grand total line item and a ticket, finds the event sub-total |
|
| 822 | + * line item the ticket's purchase should be added onto |
|
| 823 | + * |
|
| 824 | + * @access public |
|
| 825 | + * @param EE_Line_Item $grand_total the grand total line item |
|
| 826 | + * @param EE_Ticket $ticket |
|
| 827 | + * @return EE_Line_Item |
|
| 828 | + * @throws EE_Error |
|
| 829 | + * @throws InvalidArgumentException |
|
| 830 | + * @throws InvalidDataTypeException |
|
| 831 | + * @throws InvalidInterfaceException |
|
| 832 | + * @throws ReflectionException |
|
| 833 | + */ |
|
| 834 | + public static function get_event_line_item_for_ticket(EE_Line_Item $grand_total, EE_Ticket $ticket) |
|
| 835 | + { |
|
| 836 | + $first_datetime = $ticket->first_datetime(); |
|
| 837 | + if (! $first_datetime instanceof EE_Datetime) { |
|
| 838 | + throw new EE_Error( |
|
| 839 | + sprintf( |
|
| 840 | + __('The supplied ticket (ID %d) has no datetimes', 'event_espresso'), |
|
| 841 | + $ticket->ID() |
|
| 842 | + ) |
|
| 843 | + ); |
|
| 844 | + } |
|
| 845 | + $event = $first_datetime->event(); |
|
| 846 | + if (! $event instanceof EE_Event) { |
|
| 847 | + throw new EE_Error( |
|
| 848 | + sprintf( |
|
| 849 | + esc_html__( |
|
| 850 | + 'The supplied ticket (ID %d) has no event data associated with it.', |
|
| 851 | + 'event_espresso' |
|
| 852 | + ), |
|
| 853 | + $ticket->ID() |
|
| 854 | + ) |
|
| 855 | + ); |
|
| 856 | + } |
|
| 857 | + $events_sub_total = EEH_Line_Item::get_event_line_item($grand_total, $event); |
|
| 858 | + if (! $events_sub_total instanceof EE_Line_Item) { |
|
| 859 | + throw new EE_Error( |
|
| 860 | + sprintf( |
|
| 861 | + esc_html__( |
|
| 862 | + 'There is no events sub-total for ticket %s on total line item %d', |
|
| 863 | + 'event_espresso' |
|
| 864 | + ), |
|
| 865 | + $ticket->ID(), |
|
| 866 | + $grand_total->ID() |
|
| 867 | + ) |
|
| 868 | + ); |
|
| 869 | + } |
|
| 870 | + return $events_sub_total; |
|
| 871 | + } |
|
| 872 | + |
|
| 873 | + |
|
| 874 | + /** |
|
| 875 | + * Gets the event line item |
|
| 876 | + * |
|
| 877 | + * @param EE_Line_Item $grand_total |
|
| 878 | + * @param EE_Event $event |
|
| 879 | + * @return EE_Line_Item for the event subtotal which is a child of $grand_total |
|
| 880 | + * @throws EE_Error |
|
| 881 | + * @throws InvalidArgumentException |
|
| 882 | + * @throws InvalidDataTypeException |
|
| 883 | + * @throws InvalidInterfaceException |
|
| 884 | + * @throws ReflectionException |
|
| 885 | + */ |
|
| 886 | + public static function get_event_line_item(EE_Line_Item $grand_total, $event) |
|
| 887 | + { |
|
| 888 | + /** @type EE_Event $event */ |
|
| 889 | + $event = EEM_Event::instance()->ensure_is_obj($event, true); |
|
| 890 | + $event_line_item = null; |
|
| 891 | + $found = false; |
|
| 892 | + foreach (EEH_Line_Item::get_event_subtotals($grand_total) as $event_line_item) { |
|
| 893 | + // default event subtotal, we should only ever find this the first time this method is called |
|
| 894 | + if (! $event_line_item->OBJ_ID()) { |
|
| 895 | + // let's use this! but first... set the event details |
|
| 896 | + EEH_Line_Item::set_event_subtotal_details($event_line_item, $event); |
|
| 897 | + $found = true; |
|
| 898 | + break; |
|
| 899 | + } |
|
| 900 | + if ($event_line_item->OBJ_ID() === $event->ID()) { |
|
| 901 | + // found existing line item for this event in the cart, so break out of loop and use this one |
|
| 902 | + $found = true; |
|
| 903 | + break; |
|
| 904 | + } |
|
| 905 | + } |
|
| 906 | + if (! $found) { |
|
| 907 | + // there is no event sub-total yet, so add it |
|
| 908 | + $pre_tax_subtotal = EEH_Line_Item::get_pre_tax_subtotal($grand_total); |
|
| 909 | + // create a new "event" subtotal below that |
|
| 910 | + $event_line_item = EEH_Line_Item::create_event_subtotal($pre_tax_subtotal, null, $event); |
|
| 911 | + // and set the event details |
|
| 912 | + EEH_Line_Item::set_event_subtotal_details($event_line_item, $event); |
|
| 913 | + } |
|
| 914 | + return $event_line_item; |
|
| 915 | + } |
|
| 916 | + |
|
| 917 | + |
|
| 918 | + /** |
|
| 919 | + * Creates a default items subtotal line item |
|
| 920 | + * |
|
| 921 | + * @param EE_Line_Item $event_line_item |
|
| 922 | + * @param EE_Event $event |
|
| 923 | + * @param EE_Transaction $transaction |
|
| 924 | + * @return void |
|
| 925 | + * @throws EE_Error |
|
| 926 | + * @throws InvalidArgumentException |
|
| 927 | + * @throws InvalidDataTypeException |
|
| 928 | + * @throws InvalidInterfaceException |
|
| 929 | + * @throws ReflectionException |
|
| 930 | + */ |
|
| 931 | + public static function set_event_subtotal_details( |
|
| 932 | + EE_Line_Item $event_line_item, |
|
| 933 | + EE_Event $event, |
|
| 934 | + $transaction = null |
|
| 935 | + ) { |
|
| 936 | + if ($event instanceof EE_Event) { |
|
| 937 | + $event_line_item->set_code(self::get_event_code($event)); |
|
| 938 | + $event_line_item->set_name(self::get_event_name($event)); |
|
| 939 | + $event_line_item->set_desc(self::get_event_desc($event)); |
|
| 940 | + $event_line_item->set_OBJ_ID($event->ID()); |
|
| 941 | + } |
|
| 942 | + self::set_TXN_ID($event_line_item, $transaction); |
|
| 943 | + } |
|
| 944 | + |
|
| 945 | + |
|
| 946 | + /** |
|
| 947 | + * Finds what taxes should apply, adds them as tax line items under the taxes sub-total, |
|
| 948 | + * and recalculates the taxes sub-total and the grand total. Resets the taxes, so |
|
| 949 | + * any old taxes are removed |
|
| 950 | + * |
|
| 951 | + * @param EE_Line_Item $total_line_item of type EEM_Line_Item::type_total |
|
| 952 | + * @param bool $update_txn_status |
|
| 953 | + * @return bool |
|
| 954 | + * @throws EE_Error |
|
| 955 | + * @throws InvalidArgumentException |
|
| 956 | + * @throws InvalidDataTypeException |
|
| 957 | + * @throws InvalidInterfaceException |
|
| 958 | + * @throws ReflectionException |
|
| 959 | + * @throws RuntimeException |
|
| 960 | + */ |
|
| 961 | + public static function apply_taxes(EE_Line_Item $total_line_item, $update_txn_status = false) |
|
| 962 | + { |
|
| 963 | + /** @type EEM_Price $EEM_Price */ |
|
| 964 | + $EEM_Price = EE_Registry::instance()->load_model('Price'); |
|
| 965 | + // get array of taxes via Price Model |
|
| 966 | + $ordered_taxes = $EEM_Price->get_all_prices_that_are_taxes(); |
|
| 967 | + ksort($ordered_taxes); |
|
| 968 | + $taxes_line_item = self::get_taxes_subtotal($total_line_item); |
|
| 969 | + // just to be safe, remove its old tax line items |
|
| 970 | + $deleted = $taxes_line_item->delete_children_line_items(); |
|
| 971 | + $updates = false; |
|
| 972 | + // loop thru taxes |
|
| 973 | + foreach ($ordered_taxes as $order => $taxes) { |
|
| 974 | + foreach ($taxes as $tax) { |
|
| 975 | + if ($tax instanceof EE_Price) { |
|
| 976 | + $tax_line_item = EE_Line_Item::new_instance( |
|
| 977 | + array( |
|
| 978 | + 'LIN_name' => $tax->name(), |
|
| 979 | + 'LIN_desc' => $tax->desc(), |
|
| 980 | + 'LIN_percent' => $tax->amount(), |
|
| 981 | + 'LIN_is_taxable' => false, |
|
| 982 | + 'LIN_order' => $order, |
|
| 983 | + 'LIN_total' => 0, |
|
| 984 | + 'LIN_type' => EEM_Line_Item::type_tax, |
|
| 985 | + 'OBJ_type' => EEM_Line_Item::OBJ_TYPE_PRICE, |
|
| 986 | + 'OBJ_ID' => $tax->ID(), |
|
| 987 | + ) |
|
| 988 | + ); |
|
| 989 | + $tax_line_item = apply_filters( |
|
| 990 | + 'FHEE__EEH_Line_Item__apply_taxes__tax_line_item', |
|
| 991 | + $tax_line_item |
|
| 992 | + ); |
|
| 993 | + $updates = $taxes_line_item->add_child_line_item($tax_line_item) ? |
|
| 994 | + true : |
|
| 995 | + $updates; |
|
| 996 | + } |
|
| 997 | + } |
|
| 998 | + } |
|
| 999 | + // only recalculate totals if something changed |
|
| 1000 | + if ($deleted || $updates) { |
|
| 1001 | + $total_line_item->recalculate_total_including_taxes($update_txn_status); |
|
| 1002 | + return true; |
|
| 1003 | + } |
|
| 1004 | + return false; |
|
| 1005 | + } |
|
| 1006 | + |
|
| 1007 | + |
|
| 1008 | + /** |
|
| 1009 | + * Ensures that taxes have been applied to the order, if not applies them. |
|
| 1010 | + * Returns the total amount of tax |
|
| 1011 | + * |
|
| 1012 | + * @param EE_Line_Item $total_line_item of type EEM_Line_Item::type_total |
|
| 1013 | + * @return float |
|
| 1014 | + * @throws EE_Error |
|
| 1015 | + * @throws InvalidArgumentException |
|
| 1016 | + * @throws InvalidDataTypeException |
|
| 1017 | + * @throws InvalidInterfaceException |
|
| 1018 | + * @throws ReflectionException |
|
| 1019 | + */ |
|
| 1020 | + public static function ensure_taxes_applied($total_line_item) |
|
| 1021 | + { |
|
| 1022 | + $taxes_subtotal = self::get_taxes_subtotal($total_line_item); |
|
| 1023 | + if (! $taxes_subtotal->children()) { |
|
| 1024 | + self::apply_taxes($total_line_item); |
|
| 1025 | + } |
|
| 1026 | + return $taxes_subtotal->total(); |
|
| 1027 | + } |
|
| 1028 | + |
|
| 1029 | + |
|
| 1030 | + /** |
|
| 1031 | + * Deletes ALL children of the passed line item |
|
| 1032 | + * |
|
| 1033 | + * @param EE_Line_Item $parent_line_item |
|
| 1034 | + * @return bool |
|
| 1035 | + * @throws EE_Error |
|
| 1036 | + * @throws InvalidArgumentException |
|
| 1037 | + * @throws InvalidDataTypeException |
|
| 1038 | + * @throws InvalidInterfaceException |
|
| 1039 | + * @throws ReflectionException |
|
| 1040 | + */ |
|
| 1041 | + public static function delete_all_child_items(EE_Line_Item $parent_line_item) |
|
| 1042 | + { |
|
| 1043 | + $deleted = 0; |
|
| 1044 | + foreach ($parent_line_item->children() as $child_line_item) { |
|
| 1045 | + if ($child_line_item instanceof EE_Line_Item) { |
|
| 1046 | + $deleted += EEH_Line_Item::delete_all_child_items($child_line_item); |
|
| 1047 | + if ($child_line_item->ID()) { |
|
| 1048 | + $child_line_item->delete(); |
|
| 1049 | + unset($child_line_item); |
|
| 1050 | + } else { |
|
| 1051 | + $parent_line_item->delete_child_line_item($child_line_item->code()); |
|
| 1052 | + } |
|
| 1053 | + $deleted++; |
|
| 1054 | + } |
|
| 1055 | + } |
|
| 1056 | + return $deleted; |
|
| 1057 | + } |
|
| 1058 | + |
|
| 1059 | + |
|
| 1060 | + /** |
|
| 1061 | + * Deletes the line items as indicated by the line item code(s) provided, |
|
| 1062 | + * regardless of where they're found in the line item tree. Automatically |
|
| 1063 | + * re-calculates the line item totals and updates the related transaction. But |
|
| 1064 | + * DOES NOT automatically upgrade the transaction's registrations' final prices (which |
|
| 1065 | + * should probably change because of this). |
|
| 1066 | + * You should call EE_Registration_Processor::calculate_reg_final_prices_per_line_item() |
|
| 1067 | + * after using this, to keep the registration final prices in-sync with the transaction's total. |
|
| 1068 | + * |
|
| 1069 | + * @param EE_Line_Item $total_line_item of type EEM_Line_Item::type_total |
|
| 1070 | + * @param array|bool|string $line_item_codes |
|
| 1071 | + * @return int number of items successfully removed |
|
| 1072 | + * @throws EE_Error |
|
| 1073 | + */ |
|
| 1074 | + public static function delete_items(EE_Line_Item $total_line_item, $line_item_codes = false) |
|
| 1075 | + { |
|
| 1076 | + |
|
| 1077 | + if ($total_line_item->type() !== EEM_Line_Item::type_total) { |
|
| 1078 | + EE_Error::doing_it_wrong( |
|
| 1079 | + 'EEH_Line_Item::delete_items', |
|
| 1080 | + esc_html__( |
|
| 1081 | + 'This static method should only be called with a TOTAL line item, otherwise we won\'t recalculate the totals correctly', |
|
| 1082 | + 'event_espresso' |
|
| 1083 | + ), |
|
| 1084 | + '4.6.18' |
|
| 1085 | + ); |
|
| 1086 | + } |
|
| 1087 | + do_action('AHEE_log', __FILE__, __FUNCTION__, ''); |
|
| 1088 | + |
|
| 1089 | + // check if only a single line_item_id was passed |
|
| 1090 | + if (! empty($line_item_codes) && ! is_array($line_item_codes)) { |
|
| 1091 | + // place single line_item_id in an array to appear as multiple line_item_ids |
|
| 1092 | + $line_item_codes = array($line_item_codes); |
|
| 1093 | + } |
|
| 1094 | + $removals = 0; |
|
| 1095 | + // cycle thru line_item_ids |
|
| 1096 | + foreach ($line_item_codes as $line_item_id) { |
|
| 1097 | + $removals += $total_line_item->delete_child_line_item($line_item_id); |
|
| 1098 | + } |
|
| 1099 | + |
|
| 1100 | + if ($removals > 0) { |
|
| 1101 | + $total_line_item->recalculate_taxes_and_tax_total(); |
|
| 1102 | + return $removals; |
|
| 1103 | + } else { |
|
| 1104 | + return false; |
|
| 1105 | + } |
|
| 1106 | + } |
|
| 1107 | + |
|
| 1108 | + |
|
| 1109 | + /** |
|
| 1110 | + * Overwrites the previous tax by clearing out the old taxes, and creates a new |
|
| 1111 | + * tax and updates the total line item accordingly |
|
| 1112 | + * |
|
| 1113 | + * @param EE_Line_Item $total_line_item |
|
| 1114 | + * @param float $amount |
|
| 1115 | + * @param string $name |
|
| 1116 | + * @param string $description |
|
| 1117 | + * @param string $code |
|
| 1118 | + * @param boolean $add_to_existing_line_item |
|
| 1119 | + * if true, and a duplicate line item with the same code is found, |
|
| 1120 | + * $amount will be added onto it; otherwise will simply set the taxes to match $amount |
|
| 1121 | + * @return EE_Line_Item the new tax line item created |
|
| 1122 | + * @throws EE_Error |
|
| 1123 | + * @throws InvalidArgumentException |
|
| 1124 | + * @throws InvalidDataTypeException |
|
| 1125 | + * @throws InvalidInterfaceException |
|
| 1126 | + * @throws ReflectionException |
|
| 1127 | + */ |
|
| 1128 | + public static function set_total_tax_to( |
|
| 1129 | + EE_Line_Item $total_line_item, |
|
| 1130 | + $amount, |
|
| 1131 | + $name = null, |
|
| 1132 | + $description = null, |
|
| 1133 | + $code = null, |
|
| 1134 | + $add_to_existing_line_item = false |
|
| 1135 | + ) { |
|
| 1136 | + $tax_subtotal = self::get_taxes_subtotal($total_line_item); |
|
| 1137 | + $taxable_total = $total_line_item->taxable_total(); |
|
| 1138 | + |
|
| 1139 | + if ($add_to_existing_line_item) { |
|
| 1140 | + $new_tax = $tax_subtotal->get_child_line_item($code); |
|
| 1141 | + EEM_Line_Item::instance()->delete( |
|
| 1142 | + array(array('LIN_code' => array('!=', $code), 'LIN_parent' => $tax_subtotal->ID())) |
|
| 1143 | + ); |
|
| 1144 | + } else { |
|
| 1145 | + $new_tax = null; |
|
| 1146 | + $tax_subtotal->delete_children_line_items(); |
|
| 1147 | + } |
|
| 1148 | + if ($new_tax) { |
|
| 1149 | + $new_tax->set_total($new_tax->total() + $amount); |
|
| 1150 | + $new_tax->set_percent($taxable_total ? $new_tax->total() / $taxable_total * 100 : 0); |
|
| 1151 | + } else { |
|
| 1152 | + // no existing tax item. Create it |
|
| 1153 | + $new_tax = EE_Line_Item::new_instance(array( |
|
| 1154 | + 'TXN_ID' => $total_line_item->TXN_ID(), |
|
| 1155 | + 'LIN_name' => $name ? $name : esc_html__('Tax', 'event_espresso'), |
|
| 1156 | + 'LIN_desc' => $description ? $description : '', |
|
| 1157 | + 'LIN_percent' => $taxable_total ? ($amount / $taxable_total * 100) : 0, |
|
| 1158 | + 'LIN_total' => $amount, |
|
| 1159 | + 'LIN_parent' => $tax_subtotal->ID(), |
|
| 1160 | + 'LIN_type' => EEM_Line_Item::type_tax, |
|
| 1161 | + 'LIN_code' => $code, |
|
| 1162 | + )); |
|
| 1163 | + } |
|
| 1164 | + |
|
| 1165 | + $new_tax = apply_filters( |
|
| 1166 | + 'FHEE__EEH_Line_Item__set_total_tax_to__new_tax_subtotal', |
|
| 1167 | + $new_tax, |
|
| 1168 | + $total_line_item |
|
| 1169 | + ); |
|
| 1170 | + $new_tax->save(); |
|
| 1171 | + $tax_subtotal->set_total($new_tax->total()); |
|
| 1172 | + $tax_subtotal->save(); |
|
| 1173 | + $total_line_item->recalculate_total_including_taxes(); |
|
| 1174 | + return $new_tax; |
|
| 1175 | + } |
|
| 1176 | + |
|
| 1177 | + |
|
| 1178 | + /** |
|
| 1179 | + * Makes all the line items which are children of $line_item taxable (or not). |
|
| 1180 | + * Does NOT save the line items |
|
| 1181 | + * |
|
| 1182 | + * @param EE_Line_Item $line_item |
|
| 1183 | + * @param boolean $taxable |
|
| 1184 | + * @param string $code_substring_for_whitelist if this string is part of the line item's code |
|
| 1185 | + * it will be whitelisted (ie, except from becoming taxable) |
|
| 1186 | + * @throws EE_Error |
|
| 1187 | + */ |
|
| 1188 | + public static function set_line_items_taxable( |
|
| 1189 | + EE_Line_Item $line_item, |
|
| 1190 | + $taxable = true, |
|
| 1191 | + $code_substring_for_whitelist = null |
|
| 1192 | + ) { |
|
| 1193 | + $whitelisted = false; |
|
| 1194 | + if ($code_substring_for_whitelist !== null) { |
|
| 1195 | + $whitelisted = strpos($line_item->code(), $code_substring_for_whitelist) !== false; |
|
| 1196 | + } |
|
| 1197 | + if (! $whitelisted && $line_item->is_line_item()) { |
|
| 1198 | + $line_item->set_is_taxable($taxable); |
|
| 1199 | + } |
|
| 1200 | + foreach ($line_item->children() as $child_line_item) { |
|
| 1201 | + EEH_Line_Item::set_line_items_taxable( |
|
| 1202 | + $child_line_item, |
|
| 1203 | + $taxable, |
|
| 1204 | + $code_substring_for_whitelist |
|
| 1205 | + ); |
|
| 1206 | + } |
|
| 1207 | + } |
|
| 1208 | + |
|
| 1209 | + |
|
| 1210 | + /** |
|
| 1211 | + * Gets all descendants that are event subtotals |
|
| 1212 | + * |
|
| 1213 | + * @uses EEH_Line_Item::get_subtotals_of_object_type() |
|
| 1214 | + * @param EE_Line_Item $parent_line_item - the line item to find descendants of |
|
| 1215 | + * @return EE_Line_Item[] |
|
| 1216 | + * @throws EE_Error |
|
| 1217 | + */ |
|
| 1218 | + public static function get_event_subtotals(EE_Line_Item $parent_line_item) |
|
| 1219 | + { |
|
| 1220 | + return self::get_subtotals_of_object_type($parent_line_item, EEM_Line_Item::OBJ_TYPE_EVENT); |
|
| 1221 | + } |
|
| 1222 | + |
|
| 1223 | + |
|
| 1224 | + /** |
|
| 1225 | + * Gets all descendants subtotals that match the supplied object type |
|
| 1226 | + * |
|
| 1227 | + * @uses EEH_Line_Item::_get_descendants_by_type_and_object_type() |
|
| 1228 | + * @param EE_Line_Item $parent_line_item - the line item to find descendants of |
|
| 1229 | + * @param string $obj_type |
|
| 1230 | + * @return EE_Line_Item[] |
|
| 1231 | + * @throws EE_Error |
|
| 1232 | + */ |
|
| 1233 | + public static function get_subtotals_of_object_type(EE_Line_Item $parent_line_item, $obj_type = '') |
|
| 1234 | + { |
|
| 1235 | + return self::_get_descendants_by_type_and_object_type( |
|
| 1236 | + $parent_line_item, |
|
| 1237 | + EEM_Line_Item::type_sub_total, |
|
| 1238 | + $obj_type |
|
| 1239 | + ); |
|
| 1240 | + } |
|
| 1241 | + |
|
| 1242 | + |
|
| 1243 | + /** |
|
| 1244 | + * Gets all descendants that are tickets |
|
| 1245 | + * |
|
| 1246 | + * @uses EEH_Line_Item::get_line_items_of_object_type() |
|
| 1247 | + * @param EE_Line_Item $parent_line_item - the line item to find descendants of |
|
| 1248 | + * @return EE_Line_Item[] |
|
| 1249 | + * @throws EE_Error |
|
| 1250 | + */ |
|
| 1251 | + public static function get_ticket_line_items(EE_Line_Item $parent_line_item) |
|
| 1252 | + { |
|
| 1253 | + return self::get_line_items_of_object_type( |
|
| 1254 | + $parent_line_item, |
|
| 1255 | + EEM_Line_Item::OBJ_TYPE_TICKET |
|
| 1256 | + ); |
|
| 1257 | + } |
|
| 1258 | + |
|
| 1259 | + |
|
| 1260 | + /** |
|
| 1261 | + * Gets all descendants subtotals that match the supplied object type |
|
| 1262 | + * |
|
| 1263 | + * @uses EEH_Line_Item::_get_descendants_by_type_and_object_type() |
|
| 1264 | + * @param EE_Line_Item $parent_line_item - the line item to find descendants of |
|
| 1265 | + * @param string $obj_type |
|
| 1266 | + * @return EE_Line_Item[] |
|
| 1267 | + * @throws EE_Error |
|
| 1268 | + */ |
|
| 1269 | + public static function get_line_items_of_object_type(EE_Line_Item $parent_line_item, $obj_type = '') |
|
| 1270 | + { |
|
| 1271 | + return self::_get_descendants_by_type_and_object_type( |
|
| 1272 | + $parent_line_item, |
|
| 1273 | + EEM_Line_Item::type_line_item, |
|
| 1274 | + $obj_type |
|
| 1275 | + ); |
|
| 1276 | + } |
|
| 1277 | + |
|
| 1278 | + |
|
| 1279 | + /** |
|
| 1280 | + * Gets all the descendants (ie, children or children of children etc) that are of the type 'tax' |
|
| 1281 | + * |
|
| 1282 | + * @uses EEH_Line_Item::get_descendants_of_type() |
|
| 1283 | + * @param EE_Line_Item $parent_line_item - the line item to find descendants of |
|
| 1284 | + * @return EE_Line_Item[] |
|
| 1285 | + * @throws EE_Error |
|
| 1286 | + */ |
|
| 1287 | + public static function get_tax_descendants(EE_Line_Item $parent_line_item) |
|
| 1288 | + { |
|
| 1289 | + return EEH_Line_Item::get_descendants_of_type( |
|
| 1290 | + $parent_line_item, |
|
| 1291 | + EEM_Line_Item::type_tax |
|
| 1292 | + ); |
|
| 1293 | + } |
|
| 1294 | + |
|
| 1295 | + |
|
| 1296 | + /** |
|
| 1297 | + * Gets all the real items purchased which are children of this item |
|
| 1298 | + * |
|
| 1299 | + * @uses EEH_Line_Item::get_descendants_of_type() |
|
| 1300 | + * @param EE_Line_Item $parent_line_item - the line item to find descendants of |
|
| 1301 | + * @return EE_Line_Item[] |
|
| 1302 | + * @throws EE_Error |
|
| 1303 | + */ |
|
| 1304 | + public static function get_line_item_descendants(EE_Line_Item $parent_line_item) |
|
| 1305 | + { |
|
| 1306 | + return EEH_Line_Item::get_descendants_of_type( |
|
| 1307 | + $parent_line_item, |
|
| 1308 | + EEM_Line_Item::type_line_item |
|
| 1309 | + ); |
|
| 1310 | + } |
|
| 1311 | + |
|
| 1312 | + |
|
| 1313 | + /** |
|
| 1314 | + * Gets all descendants of supplied line item that match the supplied line item type |
|
| 1315 | + * |
|
| 1316 | + * @uses EEH_Line_Item::_get_descendants_by_type_and_object_type() |
|
| 1317 | + * @param EE_Line_Item $parent_line_item - the line item to find descendants of |
|
| 1318 | + * @param string $line_item_type one of the EEM_Line_Item constants |
|
| 1319 | + * @return EE_Line_Item[] |
|
| 1320 | + * @throws EE_Error |
|
| 1321 | + */ |
|
| 1322 | + public static function get_descendants_of_type(EE_Line_Item $parent_line_item, $line_item_type) |
|
| 1323 | + { |
|
| 1324 | + return self::_get_descendants_by_type_and_object_type( |
|
| 1325 | + $parent_line_item, |
|
| 1326 | + $line_item_type, |
|
| 1327 | + null |
|
| 1328 | + ); |
|
| 1329 | + } |
|
| 1330 | + |
|
| 1331 | + |
|
| 1332 | + /** |
|
| 1333 | + * Gets all descendants of supplied line item that match the supplied line item type and possibly the object type |
|
| 1334 | + * as well |
|
| 1335 | + * |
|
| 1336 | + * @param EE_Line_Item $parent_line_item - the line item to find descendants of |
|
| 1337 | + * @param string $line_item_type one of the EEM_Line_Item constants |
|
| 1338 | + * @param string | NULL $obj_type object model class name (minus prefix) or NULL to ignore object type when |
|
| 1339 | + * searching |
|
| 1340 | + * @return EE_Line_Item[] |
|
| 1341 | + * @throws EE_Error |
|
| 1342 | + */ |
|
| 1343 | + protected static function _get_descendants_by_type_and_object_type( |
|
| 1344 | + EE_Line_Item $parent_line_item, |
|
| 1345 | + $line_item_type, |
|
| 1346 | + $obj_type = null |
|
| 1347 | + ) { |
|
| 1348 | + $objects = array(); |
|
| 1349 | + foreach ($parent_line_item->children() as $child_line_item) { |
|
| 1350 | + if ($child_line_item instanceof EE_Line_Item) { |
|
| 1351 | + if ($child_line_item->type() === $line_item_type |
|
| 1352 | + && ( |
|
| 1353 | + $child_line_item->OBJ_type() === $obj_type || $obj_type === null |
|
| 1354 | + ) |
|
| 1355 | + ) { |
|
| 1356 | + $objects[] = $child_line_item; |
|
| 1357 | + } else { |
|
| 1358 | + // go-through-all-its children looking for more matches |
|
| 1359 | + $objects = array_merge( |
|
| 1360 | + $objects, |
|
| 1361 | + self::_get_descendants_by_type_and_object_type( |
|
| 1362 | + $child_line_item, |
|
| 1363 | + $line_item_type, |
|
| 1364 | + $obj_type |
|
| 1365 | + ) |
|
| 1366 | + ); |
|
| 1367 | + } |
|
| 1368 | + } |
|
| 1369 | + } |
|
| 1370 | + return $objects; |
|
| 1371 | + } |
|
| 1372 | + |
|
| 1373 | + |
|
| 1374 | + /** |
|
| 1375 | + * Gets all descendants subtotals that match the supplied object type |
|
| 1376 | + * |
|
| 1377 | + * @uses EEH_Line_Item::_get_descendants_by_type_and_object_type() |
|
| 1378 | + * @param EE_Line_Item $parent_line_item - the line item to find descendants of |
|
| 1379 | + * @param string $OBJ_type object type (like Event) |
|
| 1380 | + * @param array $OBJ_IDs array of OBJ_IDs |
|
| 1381 | + * @return EE_Line_Item[] |
|
| 1382 | + * @throws EE_Error |
|
| 1383 | + */ |
|
| 1384 | + public static function get_line_items_by_object_type_and_IDs( |
|
| 1385 | + EE_Line_Item $parent_line_item, |
|
| 1386 | + $OBJ_type = '', |
|
| 1387 | + $OBJ_IDs = array() |
|
| 1388 | + ) { |
|
| 1389 | + return self::_get_descendants_by_object_type_and_object_ID( |
|
| 1390 | + $parent_line_item, |
|
| 1391 | + $OBJ_type, |
|
| 1392 | + $OBJ_IDs |
|
| 1393 | + ); |
|
| 1394 | + } |
|
| 1395 | + |
|
| 1396 | + |
|
| 1397 | + /** |
|
| 1398 | + * Gets all descendants of supplied line item that match the supplied line item type and possibly the object type |
|
| 1399 | + * as well |
|
| 1400 | + * |
|
| 1401 | + * @param EE_Line_Item $parent_line_item - the line item to find descendants of |
|
| 1402 | + * @param string $OBJ_type object type (like Event) |
|
| 1403 | + * @param array $OBJ_IDs array of OBJ_IDs |
|
| 1404 | + * @return EE_Line_Item[] |
|
| 1405 | + * @throws EE_Error |
|
| 1406 | + */ |
|
| 1407 | + protected static function _get_descendants_by_object_type_and_object_ID( |
|
| 1408 | + EE_Line_Item $parent_line_item, |
|
| 1409 | + $OBJ_type, |
|
| 1410 | + $OBJ_IDs |
|
| 1411 | + ) { |
|
| 1412 | + $objects = array(); |
|
| 1413 | + foreach ($parent_line_item->children() as $child_line_item) { |
|
| 1414 | + if ($child_line_item instanceof EE_Line_Item) { |
|
| 1415 | + if ($child_line_item->OBJ_type() === $OBJ_type |
|
| 1416 | + && is_array($OBJ_IDs) |
|
| 1417 | + && in_array($child_line_item->OBJ_ID(), $OBJ_IDs) |
|
| 1418 | + ) { |
|
| 1419 | + $objects[] = $child_line_item; |
|
| 1420 | + } else { |
|
| 1421 | + // go-through-all-its children looking for more matches |
|
| 1422 | + $objects = array_merge( |
|
| 1423 | + $objects, |
|
| 1424 | + self::_get_descendants_by_object_type_and_object_ID( |
|
| 1425 | + $child_line_item, |
|
| 1426 | + $OBJ_type, |
|
| 1427 | + $OBJ_IDs |
|
| 1428 | + ) |
|
| 1429 | + ); |
|
| 1430 | + } |
|
| 1431 | + } |
|
| 1432 | + } |
|
| 1433 | + return $objects; |
|
| 1434 | + } |
|
| 1435 | + |
|
| 1436 | + |
|
| 1437 | + /** |
|
| 1438 | + * Uses a breadth-first-search in order to find the nearest descendant of |
|
| 1439 | + * the specified type and returns it, else NULL |
|
| 1440 | + * |
|
| 1441 | + * @uses EEH_Line_Item::_get_nearest_descendant() |
|
| 1442 | + * @param EE_Line_Item $parent_line_item - the line item to find descendants of |
|
| 1443 | + * @param string $type like one of the EEM_Line_Item::type_* |
|
| 1444 | + * @return EE_Line_Item |
|
| 1445 | + * @throws EE_Error |
|
| 1446 | + * @throws InvalidArgumentException |
|
| 1447 | + * @throws InvalidDataTypeException |
|
| 1448 | + * @throws InvalidInterfaceException |
|
| 1449 | + * @throws ReflectionException |
|
| 1450 | + */ |
|
| 1451 | + public static function get_nearest_descendant_of_type(EE_Line_Item $parent_line_item, $type) |
|
| 1452 | + { |
|
| 1453 | + return self::_get_nearest_descendant($parent_line_item, 'LIN_type', $type); |
|
| 1454 | + } |
|
| 1455 | + |
|
| 1456 | + |
|
| 1457 | + /** |
|
| 1458 | + * Uses a breadth-first-search in order to find the nearest descendant |
|
| 1459 | + * having the specified LIN_code and returns it, else NULL |
|
| 1460 | + * |
|
| 1461 | + * @uses EEH_Line_Item::_get_nearest_descendant() |
|
| 1462 | + * @param EE_Line_Item $parent_line_item - the line item to find descendants of |
|
| 1463 | + * @param string $code any value used for LIN_code |
|
| 1464 | + * @return EE_Line_Item |
|
| 1465 | + * @throws EE_Error |
|
| 1466 | + * @throws InvalidArgumentException |
|
| 1467 | + * @throws InvalidDataTypeException |
|
| 1468 | + * @throws InvalidInterfaceException |
|
| 1469 | + * @throws ReflectionException |
|
| 1470 | + */ |
|
| 1471 | + public static function get_nearest_descendant_having_code(EE_Line_Item $parent_line_item, $code) |
|
| 1472 | + { |
|
| 1473 | + return self::_get_nearest_descendant($parent_line_item, 'LIN_code', $code); |
|
| 1474 | + } |
|
| 1475 | + |
|
| 1476 | + |
|
| 1477 | + /** |
|
| 1478 | + * Uses a breadth-first-search in order to find the nearest descendant |
|
| 1479 | + * having the specified LIN_code and returns it, else NULL |
|
| 1480 | + * |
|
| 1481 | + * @param EE_Line_Item $parent_line_item - the line item to find descendants of |
|
| 1482 | + * @param string $search_field name of EE_Line_Item property |
|
| 1483 | + * @param string $value any value stored in $search_field |
|
| 1484 | + * @return EE_Line_Item |
|
| 1485 | + * @throws EE_Error |
|
| 1486 | + * @throws InvalidArgumentException |
|
| 1487 | + * @throws InvalidDataTypeException |
|
| 1488 | + * @throws InvalidInterfaceException |
|
| 1489 | + * @throws ReflectionException |
|
| 1490 | + */ |
|
| 1491 | + protected static function _get_nearest_descendant(EE_Line_Item $parent_line_item, $search_field, $value) |
|
| 1492 | + { |
|
| 1493 | + foreach ($parent_line_item->children() as $child) { |
|
| 1494 | + if ($child->get($search_field) == $value) { |
|
| 1495 | + return $child; |
|
| 1496 | + } |
|
| 1497 | + } |
|
| 1498 | + foreach ($parent_line_item->children() as $child) { |
|
| 1499 | + $descendant_found = self::_get_nearest_descendant( |
|
| 1500 | + $child, |
|
| 1501 | + $search_field, |
|
| 1502 | + $value |
|
| 1503 | + ); |
|
| 1504 | + if ($descendant_found) { |
|
| 1505 | + return $descendant_found; |
|
| 1506 | + } |
|
| 1507 | + } |
|
| 1508 | + return null; |
|
| 1509 | + } |
|
| 1510 | + |
|
| 1511 | + |
|
| 1512 | + /** |
|
| 1513 | + * if passed line item has a TXN ID, uses that to jump directly to the grand total line item for the transaction, |
|
| 1514 | + * else recursively walks up the line item tree until a parent of type total is found, |
|
| 1515 | + * |
|
| 1516 | + * @param EE_Line_Item $line_item |
|
| 1517 | + * @return EE_Line_Item |
|
| 1518 | + * @throws EE_Error |
|
| 1519 | + */ |
|
| 1520 | + public static function find_transaction_grand_total_for_line_item(EE_Line_Item $line_item) |
|
| 1521 | + { |
|
| 1522 | + if ($line_item->TXN_ID()) { |
|
| 1523 | + $total_line_item = $line_item->transaction()->total_line_item(false); |
|
| 1524 | + if ($total_line_item instanceof EE_Line_Item) { |
|
| 1525 | + return $total_line_item; |
|
| 1526 | + } |
|
| 1527 | + } else { |
|
| 1528 | + $line_item_parent = $line_item->parent(); |
|
| 1529 | + if ($line_item_parent instanceof EE_Line_Item) { |
|
| 1530 | + if ($line_item_parent->is_total()) { |
|
| 1531 | + return $line_item_parent; |
|
| 1532 | + } |
|
| 1533 | + return EEH_Line_Item::find_transaction_grand_total_for_line_item($line_item_parent); |
|
| 1534 | + } |
|
| 1535 | + } |
|
| 1536 | + throw new EE_Error( |
|
| 1537 | + sprintf( |
|
| 1538 | + esc_html__( |
|
| 1539 | + 'A valid grand total for line item %1$d was not found.', |
|
| 1540 | + 'event_espresso' |
|
| 1541 | + ), |
|
| 1542 | + $line_item->ID() |
|
| 1543 | + ) |
|
| 1544 | + ); |
|
| 1545 | + } |
|
| 1546 | + |
|
| 1547 | + |
|
| 1548 | + /** |
|
| 1549 | + * Prints out a representation of the line item tree |
|
| 1550 | + * |
|
| 1551 | + * @param EE_Line_Item $line_item |
|
| 1552 | + * @param int $indentation |
|
| 1553 | + * @return void |
|
| 1554 | + * @throws EE_Error |
|
| 1555 | + */ |
|
| 1556 | + public static function visualize(EE_Line_Item $line_item, $indentation = 0) |
|
| 1557 | + { |
|
| 1558 | + echo defined('EE_TESTS_DIR') ? "\n" : '<br />'; |
|
| 1559 | + if (! $indentation) { |
|
| 1560 | + echo defined('EE_TESTS_DIR') ? "\n" : '<br />'; |
|
| 1561 | + } |
|
| 1562 | + for ($i = 0; $i < $indentation; $i++) { |
|
| 1563 | + echo '. '; |
|
| 1564 | + } |
|
| 1565 | + $breakdown = ''; |
|
| 1566 | + if ($line_item->is_line_item()) { |
|
| 1567 | + if ($line_item->is_percent()) { |
|
| 1568 | + $breakdown = "{$line_item->percent()}%"; |
|
| 1569 | + } else { |
|
| 1570 | + $breakdown = '$' . "{$line_item->unit_price()} x {$line_item->quantity()}"; |
|
| 1571 | + } |
|
| 1572 | + } |
|
| 1573 | + echo $line_item->name(); |
|
| 1574 | + echo " [ ID:{$line_item->ID()} | qty:{$line_item->quantity()} ] {$line_item->type()} : "; |
|
| 1575 | + echo '$' . (string) $line_item->total(); |
|
| 1576 | + if ($breakdown) { |
|
| 1577 | + echo " ( {$breakdown} )"; |
|
| 1578 | + } |
|
| 1579 | + if ($line_item->is_taxable()) { |
|
| 1580 | + echo ' * taxable'; |
|
| 1581 | + } |
|
| 1582 | + if ($line_item->children()) { |
|
| 1583 | + foreach ($line_item->children() as $child) { |
|
| 1584 | + self::visualize($child, $indentation + 1); |
|
| 1585 | + } |
|
| 1586 | + } |
|
| 1587 | + } |
|
| 1588 | + |
|
| 1589 | + |
|
| 1590 | + /** |
|
| 1591 | + * Calculates the registration's final price, taking into account that they |
|
| 1592 | + * need to not only help pay for their OWN ticket, but also any transaction-wide surcharges and taxes, |
|
| 1593 | + * and receive a portion of any transaction-wide discounts. |
|
| 1594 | + * eg1, if I buy a $1 ticket and brent buys a $9 ticket, and we receive a $5 discount |
|
| 1595 | + * then I'll get 1/10 of that $5 discount, which is $0.50, and brent will get |
|
| 1596 | + * 9/10ths of that $5 discount, which is $4.50. So my final price should be $0.50 |
|
| 1597 | + * and brent's final price should be $5.50. |
|
| 1598 | + * In order to do this, we basically need to traverse the line item tree calculating |
|
| 1599 | + * the running totals (just as if we were recalculating the total), but when we identify |
|
| 1600 | + * regular line items, we need to keep track of their share of the grand total. |
|
| 1601 | + * Also, we need to keep track of the TAXABLE total for each ticket purchase, so |
|
| 1602 | + * we can know how to apply taxes to it. (Note: "taxable total" does not equal the "pretax total" |
|
| 1603 | + * when there are non-taxable items; otherwise they would be the same) |
|
| 1604 | + * |
|
| 1605 | + * @param EE_Line_Item $line_item |
|
| 1606 | + * @param array $billable_ticket_quantities array of EE_Ticket IDs and their corresponding quantity that |
|
| 1607 | + * can be included in price calculations at this moment |
|
| 1608 | + * @return array keys are line items for tickets IDs and values are their share of the running total, |
|
| 1609 | + * plus the key 'total', and 'taxable' which also has keys of all |
|
| 1610 | + * the ticket IDs. |
|
| 1611 | + * Eg array( |
|
| 1612 | + * 12 => 4.3 |
|
| 1613 | + * 23 => 8.0 |
|
| 1614 | + * 'total' => 16.6, |
|
| 1615 | + * 'taxable' => array( |
|
| 1616 | + * 12 => 10, |
|
| 1617 | + * 23 => 4 |
|
| 1618 | + * ). |
|
| 1619 | + * So to find which registrations have which final price, we need |
|
| 1620 | + * to find which line item is theirs, which can be done with |
|
| 1621 | + * `EEM_Line_Item::instance()->get_line_item_for_registration( |
|
| 1622 | + * $registration );` |
|
| 1623 | + * @throws EE_Error |
|
| 1624 | + * @throws InvalidArgumentException |
|
| 1625 | + * @throws InvalidDataTypeException |
|
| 1626 | + * @throws InvalidInterfaceException |
|
| 1627 | + * @throws ReflectionException |
|
| 1628 | + */ |
|
| 1629 | + public static function calculate_reg_final_prices_per_line_item( |
|
| 1630 | + EE_Line_Item $line_item, |
|
| 1631 | + $billable_ticket_quantities = array() |
|
| 1632 | + ) { |
|
| 1633 | + $running_totals = [ |
|
| 1634 | + 'total' => 0, |
|
| 1635 | + 'taxable' => ['total' => 0] |
|
| 1636 | + ]; |
|
| 1637 | + foreach ($line_item->children() as $child_line_item) { |
|
| 1638 | + switch ($child_line_item->type()) { |
|
| 1639 | + case EEM_Line_Item::type_sub_total: |
|
| 1640 | + $running_totals_from_subtotal = EEH_Line_Item::calculate_reg_final_prices_per_line_item( |
|
| 1641 | + $child_line_item, |
|
| 1642 | + $billable_ticket_quantities |
|
| 1643 | + ); |
|
| 1644 | + // combine arrays but preserve numeric keys |
|
| 1645 | + $running_totals = array_replace_recursive($running_totals_from_subtotal, $running_totals); |
|
| 1646 | + $running_totals['total'] += $running_totals_from_subtotal['total']; |
|
| 1647 | + $running_totals['taxable']['total'] += $running_totals_from_subtotal['taxable']['total']; |
|
| 1648 | + break; |
|
| 1649 | + |
|
| 1650 | + case EEM_Line_Item::type_tax_sub_total: |
|
| 1651 | + // find how much the taxes percentage is |
|
| 1652 | + if ($child_line_item->percent() !== 0) { |
|
| 1653 | + $tax_percent_decimal = $child_line_item->percent() / 100; |
|
| 1654 | + } else { |
|
| 1655 | + $tax_percent_decimal = EE_Taxes::get_total_taxes_percentage() / 100; |
|
| 1656 | + } |
|
| 1657 | + // and apply to all the taxable totals, and add to the pretax totals |
|
| 1658 | + foreach ($running_totals as $line_item_id => $this_running_total) { |
|
| 1659 | + // "total" and "taxable" array key is an exception |
|
| 1660 | + if ($line_item_id === 'taxable') { |
|
| 1661 | + continue; |
|
| 1662 | + } |
|
| 1663 | + $taxable_total = $running_totals['taxable'][ $line_item_id ]; |
|
| 1664 | + $running_totals[ $line_item_id ] += ($taxable_total * $tax_percent_decimal); |
|
| 1665 | + } |
|
| 1666 | + break; |
|
| 1667 | + |
|
| 1668 | + case EEM_Line_Item::type_line_item: |
|
| 1669 | + // ticket line items or ???? |
|
| 1670 | + if ($child_line_item->OBJ_type() === EEM_Line_Item::OBJ_TYPE_TICKET) { |
|
| 1671 | + // kk it's a ticket |
|
| 1672 | + if (isset($running_totals[ $child_line_item->ID() ])) { |
|
| 1673 | + // huh? that shouldn't happen. |
|
| 1674 | + $running_totals['total'] += $child_line_item->total(); |
|
| 1675 | + } else { |
|
| 1676 | + // its not in our running totals yet. great. |
|
| 1677 | + if ($child_line_item->is_taxable()) { |
|
| 1678 | + $taxable_amount = $child_line_item->unit_price(); |
|
| 1679 | + } else { |
|
| 1680 | + $taxable_amount = 0; |
|
| 1681 | + } |
|
| 1682 | + // are we only calculating totals for some tickets? |
|
| 1683 | + if (isset($billable_ticket_quantities[ $child_line_item->OBJ_ID() ])) { |
|
| 1684 | + $quantity = $billable_ticket_quantities[ $child_line_item->OBJ_ID() ]; |
|
| 1685 | + $running_totals[ $child_line_item->ID() ] = $quantity |
|
| 1686 | + ? $child_line_item->unit_price() |
|
| 1687 | + : 0; |
|
| 1688 | + $running_totals['taxable'][ $child_line_item->ID() ] = $quantity |
|
| 1689 | + ? $taxable_amount |
|
| 1690 | + : 0; |
|
| 1691 | + } else { |
|
| 1692 | + $quantity = $child_line_item->quantity(); |
|
| 1693 | + $running_totals[ $child_line_item->ID() ] = $child_line_item->unit_price(); |
|
| 1694 | + $running_totals['taxable'][ $child_line_item->ID() ] = $taxable_amount; |
|
| 1695 | + } |
|
| 1696 | + $running_totals['taxable']['total'] += $taxable_amount * $quantity; |
|
| 1697 | + $running_totals['total'] += $child_line_item->unit_price() * $quantity; |
|
| 1698 | + } |
|
| 1699 | + } else { |
|
| 1700 | + // it's some other type of item added to the cart |
|
| 1701 | + // it should affect the running totals |
|
| 1702 | + // basically we want to convert it into a PERCENT modifier. Because |
|
| 1703 | + // more clearly affect all registration's final price equally |
|
| 1704 | + $line_items_percent_of_running_total = $running_totals['total'] > 0 |
|
| 1705 | + ? ($child_line_item->total() / $running_totals['total']) + 1 |
|
| 1706 | + : 1; |
|
| 1707 | + foreach ($running_totals as $line_item_id => $this_running_total) { |
|
| 1708 | + // the "taxable" array key is an exception |
|
| 1709 | + if ($line_item_id === 'taxable') { |
|
| 1710 | + continue; |
|
| 1711 | + } |
|
| 1712 | + // update the running totals |
|
| 1713 | + // yes this actually even works for the running grand total! |
|
| 1714 | + $running_totals[ $line_item_id ] = |
|
| 1715 | + $line_items_percent_of_running_total * $this_running_total; |
|
| 1716 | + |
|
| 1717 | + if ($child_line_item->is_taxable()) { |
|
| 1718 | + $running_totals['taxable'][ $line_item_id ] = |
|
| 1719 | + $line_items_percent_of_running_total * $running_totals['taxable'][ $line_item_id ]; |
|
| 1720 | + } |
|
| 1721 | + } |
|
| 1722 | + } |
|
| 1723 | + break; |
|
| 1724 | + } |
|
| 1725 | + } |
|
| 1726 | + return $running_totals; |
|
| 1727 | + } |
|
| 1728 | + |
|
| 1729 | + |
|
| 1730 | + /** |
|
| 1731 | + * @param EE_Line_Item $total_line_item |
|
| 1732 | + * @param EE_Line_Item $ticket_line_item |
|
| 1733 | + * @return float | null |
|
| 1734 | + * @throws EE_Error |
|
| 1735 | + * @throws InvalidArgumentException |
|
| 1736 | + * @throws InvalidDataTypeException |
|
| 1737 | + * @throws InvalidInterfaceException |
|
| 1738 | + * @throws OutOfRangeException |
|
| 1739 | + * @throws ReflectionException |
|
| 1740 | + */ |
|
| 1741 | + public static function calculate_final_price_for_ticket_line_item( |
|
| 1742 | + EE_Line_Item $total_line_item, |
|
| 1743 | + EE_Line_Item $ticket_line_item |
|
| 1744 | + ) { |
|
| 1745 | + static $final_prices_per_ticket_line_item = array(); |
|
| 1746 | + if (empty($final_prices_per_ticket_line_item) || empty($final_prices_per_ticket_line_item[ $total_line_item->ID() ])) { |
|
| 1747 | + $final_prices_per_ticket_line_item[ $total_line_item->ID() ] = EEH_Line_Item::calculate_reg_final_prices_per_line_item( |
|
| 1748 | + $total_line_item |
|
| 1749 | + ); |
|
| 1750 | + } |
|
| 1751 | + // ok now find this new registration's final price |
|
| 1752 | + if (isset($final_prices_per_ticket_line_item[ $total_line_item->ID() ][ $ticket_line_item->ID() ])) { |
|
| 1753 | + return $final_prices_per_ticket_line_item[ $total_line_item->ID() ][ $ticket_line_item->ID() ]; |
|
| 1754 | + } |
|
| 1755 | + $message = sprintf( |
|
| 1756 | + esc_html__( |
|
| 1757 | + 'The final price for the ticket line item (ID:%1$d) could not be calculated.', |
|
| 1758 | + 'event_espresso' |
|
| 1759 | + ), |
|
| 1760 | + $ticket_line_item->ID() |
|
| 1761 | + ); |
|
| 1762 | + if (WP_DEBUG) { |
|
| 1763 | + $message .= '<br>' . print_r($final_prices_per_ticket_line_item, true); |
|
| 1764 | + throw new OutOfRangeException($message); |
|
| 1765 | + } |
|
| 1766 | + EE_Log::instance()->log(__CLASS__, __FUNCTION__, $message); |
|
| 1767 | + return null; |
|
| 1768 | + } |
|
| 1769 | + |
|
| 1770 | + |
|
| 1771 | + /** |
|
| 1772 | + * Creates a duplicate of the line item tree, except only includes billable items |
|
| 1773 | + * and the portion of line items attributed to billable things |
|
| 1774 | + * |
|
| 1775 | + * @param EE_Line_Item $line_item |
|
| 1776 | + * @param EE_Registration[] $registrations |
|
| 1777 | + * @return EE_Line_Item |
|
| 1778 | + * @throws EE_Error |
|
| 1779 | + * @throws InvalidArgumentException |
|
| 1780 | + * @throws InvalidDataTypeException |
|
| 1781 | + * @throws InvalidInterfaceException |
|
| 1782 | + * @throws ReflectionException |
|
| 1783 | + */ |
|
| 1784 | + public static function billable_line_item_tree(EE_Line_Item $line_item, $registrations) |
|
| 1785 | + { |
|
| 1786 | + $copy_li = EEH_Line_Item::billable_line_item($line_item, $registrations); |
|
| 1787 | + foreach ($line_item->children() as $child_li) { |
|
| 1788 | + $copy_li->add_child_line_item( |
|
| 1789 | + EEH_Line_Item::billable_line_item_tree($child_li, $registrations) |
|
| 1790 | + ); |
|
| 1791 | + } |
|
| 1792 | + // if this is the grand total line item, make sure the totals all add up |
|
| 1793 | + // (we could have duplicated this logic AS we copied the line items, but |
|
| 1794 | + // it seems DRYer this way) |
|
| 1795 | + if ($copy_li->type() === EEM_Line_Item::type_total) { |
|
| 1796 | + $copy_li->recalculate_total_including_taxes(); |
|
| 1797 | + } |
|
| 1798 | + return $copy_li; |
|
| 1799 | + } |
|
| 1800 | + |
|
| 1801 | + |
|
| 1802 | + /** |
|
| 1803 | + * Creates a new, unsaved line item from $line_item that factors in the |
|
| 1804 | + * number of billable registrations on $registrations. |
|
| 1805 | + * |
|
| 1806 | + * @param EE_Line_Item $line_item |
|
| 1807 | + * @param EE_Registration[] $registrations |
|
| 1808 | + * @return EE_Line_Item |
|
| 1809 | + * @throws EE_Error |
|
| 1810 | + * @throws InvalidArgumentException |
|
| 1811 | + * @throws InvalidDataTypeException |
|
| 1812 | + * @throws InvalidInterfaceException |
|
| 1813 | + * @throws ReflectionException |
|
| 1814 | + */ |
|
| 1815 | + public static function billable_line_item(EE_Line_Item $line_item, $registrations) |
|
| 1816 | + { |
|
| 1817 | + $new_li_fields = $line_item->model_field_array(); |
|
| 1818 | + if ($line_item->type() === EEM_Line_Item::type_line_item && |
|
| 1819 | + $line_item->OBJ_type() === EEM_Line_Item::OBJ_TYPE_TICKET |
|
| 1820 | + ) { |
|
| 1821 | + $count = 0; |
|
| 1822 | + foreach ($registrations as $registration) { |
|
| 1823 | + if ($line_item->OBJ_ID() === $registration->ticket_ID() && |
|
| 1824 | + in_array( |
|
| 1825 | + $registration->status_ID(), |
|
| 1826 | + EEM_Registration::reg_statuses_that_allow_payment(), |
|
| 1827 | + true |
|
| 1828 | + ) |
|
| 1829 | + ) { |
|
| 1830 | + $count++; |
|
| 1831 | + } |
|
| 1832 | + } |
|
| 1833 | + $new_li_fields['LIN_quantity'] = $count; |
|
| 1834 | + } |
|
| 1835 | + // don't set the total. We'll leave that up to the code that calculates it |
|
| 1836 | + unset($new_li_fields['LIN_ID'], $new_li_fields['LIN_parent'], $new_li_fields['LIN_total']); |
|
| 1837 | + return EE_Line_Item::new_instance($new_li_fields); |
|
| 1838 | + } |
|
| 1839 | + |
|
| 1840 | + |
|
| 1841 | + /** |
|
| 1842 | + * Returns a modified line item tree where all the subtotals which have a total of 0 |
|
| 1843 | + * are removed, and line items with a quantity of 0 |
|
| 1844 | + * |
|
| 1845 | + * @param EE_Line_Item $line_item |null |
|
| 1846 | + * @return EE_Line_Item|null |
|
| 1847 | + * @throws EE_Error |
|
| 1848 | + * @throws InvalidArgumentException |
|
| 1849 | + * @throws InvalidDataTypeException |
|
| 1850 | + * @throws InvalidInterfaceException |
|
| 1851 | + * @throws ReflectionException |
|
| 1852 | + */ |
|
| 1853 | + public static function non_empty_line_items(EE_Line_Item $line_item) |
|
| 1854 | + { |
|
| 1855 | + $copied_li = EEH_Line_Item::non_empty_line_item($line_item); |
|
| 1856 | + if ($copied_li === null) { |
|
| 1857 | + return null; |
|
| 1858 | + } |
|
| 1859 | + // if this is an event subtotal, we want to only include it if it |
|
| 1860 | + // has a non-zero total and at least one ticket line item child |
|
| 1861 | + $ticket_children = 0; |
|
| 1862 | + foreach ($line_item->children() as $child_li) { |
|
| 1863 | + $child_li_copy = EEH_Line_Item::non_empty_line_items($child_li); |
|
| 1864 | + if ($child_li_copy !== null) { |
|
| 1865 | + $copied_li->add_child_line_item($child_li_copy); |
|
| 1866 | + if ($child_li_copy->type() === EEM_Line_Item::type_line_item && |
|
| 1867 | + $child_li_copy->OBJ_type() === EEM_Line_Item::OBJ_TYPE_TICKET |
|
| 1868 | + ) { |
|
| 1869 | + $ticket_children++; |
|
| 1870 | + } |
|
| 1871 | + } |
|
| 1872 | + } |
|
| 1873 | + // if this is an event subtotal with NO ticket children |
|
| 1874 | + // we basically want to ignore it |
|
| 1875 | + if ($ticket_children === 0 |
|
| 1876 | + && $line_item->type() === EEM_Line_Item::type_sub_total |
|
| 1877 | + && $line_item->OBJ_type() === EEM_Line_Item::OBJ_TYPE_EVENT |
|
| 1878 | + && $line_item->total() === 0 |
|
| 1879 | + ) { |
|
| 1880 | + return null; |
|
| 1881 | + } |
|
| 1882 | + return $copied_li; |
|
| 1883 | + } |
|
| 1884 | + |
|
| 1885 | + |
|
| 1886 | + /** |
|
| 1887 | + * Creates a new, unsaved line item, but if it's a ticket line item |
|
| 1888 | + * with a total of 0, or a subtotal of 0, returns null instead |
|
| 1889 | + * |
|
| 1890 | + * @param EE_Line_Item $line_item |
|
| 1891 | + * @return EE_Line_Item |
|
| 1892 | + * @throws EE_Error |
|
| 1893 | + * @throws InvalidArgumentException |
|
| 1894 | + * @throws InvalidDataTypeException |
|
| 1895 | + * @throws InvalidInterfaceException |
|
| 1896 | + * @throws ReflectionException |
|
| 1897 | + */ |
|
| 1898 | + public static function non_empty_line_item(EE_Line_Item $line_item) |
|
| 1899 | + { |
|
| 1900 | + if ($line_item->type() === EEM_Line_Item::type_line_item |
|
| 1901 | + && $line_item->OBJ_type() === EEM_Line_Item::OBJ_TYPE_TICKET |
|
| 1902 | + && $line_item->quantity() === 0 |
|
| 1903 | + ) { |
|
| 1904 | + return null; |
|
| 1905 | + } |
|
| 1906 | + $new_li_fields = $line_item->model_field_array(); |
|
| 1907 | + // don't set the total. We'll leave that up to the code that calculates it |
|
| 1908 | + unset($new_li_fields['LIN_ID'], $new_li_fields['LIN_parent']); |
|
| 1909 | + return EE_Line_Item::new_instance($new_li_fields); |
|
| 1910 | + } |
|
| 1911 | + |
|
| 1912 | + |
|
| 1913 | + /** |
|
| 1914 | + * Cycles through all of the ticket line items for the supplied total line item |
|
| 1915 | + * and ensures that the line item's "is_taxable" field matches that of its corresponding ticket |
|
| 1916 | + * |
|
| 1917 | + * @param EE_Line_Item $total_line_item |
|
| 1918 | + * @since 4.9.79.p |
|
| 1919 | + * @throws EE_Error |
|
| 1920 | + * @throws InvalidArgumentException |
|
| 1921 | + * @throws InvalidDataTypeException |
|
| 1922 | + * @throws InvalidInterfaceException |
|
| 1923 | + * @throws ReflectionException |
|
| 1924 | + */ |
|
| 1925 | + public static function resetIsTaxableForTickets(EE_Line_Item $total_line_item) |
|
| 1926 | + { |
|
| 1927 | + $ticket_line_items = self::get_ticket_line_items($total_line_item); |
|
| 1928 | + foreach ($ticket_line_items as $ticket_line_item) { |
|
| 1929 | + if ($ticket_line_item instanceof EE_Line_Item |
|
| 1930 | + && $ticket_line_item->OBJ_type() === EEM_Line_Item::OBJ_TYPE_TICKET |
|
| 1931 | + ) { |
|
| 1932 | + $ticket = $ticket_line_item->ticket(); |
|
| 1933 | + if ($ticket instanceof EE_Ticket && $ticket->taxable() !== $ticket_line_item->is_taxable()) { |
|
| 1934 | + $ticket_line_item->set_is_taxable($ticket->taxable()); |
|
| 1935 | + $ticket_line_item->save(); |
|
| 1936 | + } |
|
| 1937 | + } |
|
| 1938 | + } |
|
| 1939 | + } |
|
| 1940 | + |
|
| 1941 | + |
|
| 1942 | + |
|
| 1943 | + /**************************************** @DEPRECATED METHODS *************************************** */ |
|
| 1944 | + /** |
|
| 1945 | + * @deprecated |
|
| 1946 | + * @param EE_Line_Item $total_line_item |
|
| 1947 | + * @return EE_Line_Item |
|
| 1948 | + * @throws EE_Error |
|
| 1949 | + * @throws InvalidArgumentException |
|
| 1950 | + * @throws InvalidDataTypeException |
|
| 1951 | + * @throws InvalidInterfaceException |
|
| 1952 | + * @throws ReflectionException |
|
| 1953 | + */ |
|
| 1954 | + public static function get_items_subtotal(EE_Line_Item $total_line_item) |
|
| 1955 | + { |
|
| 1956 | + EE_Error::doing_it_wrong( |
|
| 1957 | + 'EEH_Line_Item::get_items_subtotal()', |
|
| 1958 | + sprintf( |
|
| 1959 | + esc_html__('Method replaced with %1$s', 'event_espresso'), |
|
| 1960 | + 'EEH_Line_Item::get_pre_tax_subtotal()' |
|
| 1961 | + ), |
|
| 1962 | + '4.6.0' |
|
| 1963 | + ); |
|
| 1964 | + return self::get_pre_tax_subtotal($total_line_item); |
|
| 1965 | + } |
|
| 1966 | + |
|
| 1967 | + |
|
| 1968 | + /** |
|
| 1969 | + * @deprecated |
|
| 1970 | + * @param EE_Transaction $transaction |
|
| 1971 | + * @return EE_Line_Item |
|
| 1972 | + * @throws EE_Error |
|
| 1973 | + * @throws InvalidArgumentException |
|
| 1974 | + * @throws InvalidDataTypeException |
|
| 1975 | + * @throws InvalidInterfaceException |
|
| 1976 | + * @throws ReflectionException |
|
| 1977 | + */ |
|
| 1978 | + public static function create_default_total_line_item($transaction = null) |
|
| 1979 | + { |
|
| 1980 | + EE_Error::doing_it_wrong( |
|
| 1981 | + 'EEH_Line_Item::create_default_total_line_item()', |
|
| 1982 | + sprintf( |
|
| 1983 | + esc_html__('Method replaced with %1$s', 'event_espresso'), |
|
| 1984 | + 'EEH_Line_Item::create_total_line_item()' |
|
| 1985 | + ), |
|
| 1986 | + '4.6.0' |
|
| 1987 | + ); |
|
| 1988 | + return self::create_total_line_item($transaction); |
|
| 1989 | + } |
|
| 1990 | + |
|
| 1991 | + |
|
| 1992 | + /** |
|
| 1993 | + * @deprecated |
|
| 1994 | + * @param EE_Line_Item $total_line_item |
|
| 1995 | + * @param EE_Transaction $transaction |
|
| 1996 | + * @return EE_Line_Item |
|
| 1997 | + * @throws EE_Error |
|
| 1998 | + * @throws InvalidArgumentException |
|
| 1999 | + * @throws InvalidDataTypeException |
|
| 2000 | + * @throws InvalidInterfaceException |
|
| 2001 | + * @throws ReflectionException |
|
| 2002 | + */ |
|
| 2003 | + public static function create_default_tickets_subtotal(EE_Line_Item $total_line_item, $transaction = null) |
|
| 2004 | + { |
|
| 2005 | + EE_Error::doing_it_wrong( |
|
| 2006 | + 'EEH_Line_Item::create_default_tickets_subtotal()', |
|
| 2007 | + sprintf( |
|
| 2008 | + esc_html__('Method replaced with %1$s', 'event_espresso'), |
|
| 2009 | + 'EEH_Line_Item::create_pre_tax_subtotal()' |
|
| 2010 | + ), |
|
| 2011 | + '4.6.0' |
|
| 2012 | + ); |
|
| 2013 | + return self::create_pre_tax_subtotal($total_line_item, $transaction); |
|
| 2014 | + } |
|
| 2015 | + |
|
| 2016 | + |
|
| 2017 | + /** |
|
| 2018 | + * @deprecated |
|
| 2019 | + * @param EE_Line_Item $total_line_item |
|
| 2020 | + * @param EE_Transaction $transaction |
|
| 2021 | + * @return EE_Line_Item |
|
| 2022 | + * @throws EE_Error |
|
| 2023 | + * @throws InvalidArgumentException |
|
| 2024 | + * @throws InvalidDataTypeException |
|
| 2025 | + * @throws InvalidInterfaceException |
|
| 2026 | + * @throws ReflectionException |
|
| 2027 | + */ |
|
| 2028 | + public static function create_default_taxes_subtotal(EE_Line_Item $total_line_item, $transaction = null) |
|
| 2029 | + { |
|
| 2030 | + EE_Error::doing_it_wrong( |
|
| 2031 | + 'EEH_Line_Item::create_default_taxes_subtotal()', |
|
| 2032 | + sprintf( |
|
| 2033 | + esc_html__('Method replaced with %1$s', 'event_espresso'), |
|
| 2034 | + 'EEH_Line_Item::create_taxes_subtotal()' |
|
| 2035 | + ), |
|
| 2036 | + '4.6.0' |
|
| 2037 | + ); |
|
| 2038 | + return self::create_taxes_subtotal($total_line_item, $transaction); |
|
| 2039 | + } |
|
| 2040 | + |
|
| 2041 | + |
|
| 2042 | + /** |
|
| 2043 | + * @deprecated |
|
| 2044 | + * @param EE_Line_Item $total_line_item |
|
| 2045 | + * @param EE_Transaction $transaction |
|
| 2046 | + * @return EE_Line_Item |
|
| 2047 | + * @throws EE_Error |
|
| 2048 | + * @throws InvalidArgumentException |
|
| 2049 | + * @throws InvalidDataTypeException |
|
| 2050 | + * @throws InvalidInterfaceException |
|
| 2051 | + * @throws ReflectionException |
|
| 2052 | + */ |
|
| 2053 | + public static function create_default_event_subtotal(EE_Line_Item $total_line_item, $transaction = null) |
|
| 2054 | + { |
|
| 2055 | + EE_Error::doing_it_wrong( |
|
| 2056 | + 'EEH_Line_Item::create_default_event_subtotal()', |
|
| 2057 | + sprintf( |
|
| 2058 | + esc_html__('Method replaced with %1$s', 'event_espresso'), |
|
| 2059 | + 'EEH_Line_Item::create_event_subtotal()' |
|
| 2060 | + ), |
|
| 2061 | + '4.6.0' |
|
| 2062 | + ); |
|
| 2063 | + return self::create_event_subtotal($total_line_item, $transaction); |
|
| 2064 | + } |
|
| 2065 | 2065 | } |
@@ -138,7 +138,7 @@ discard block |
||
| 138 | 138 | */ |
| 139 | 139 | public static function add_ticket_purchase(EE_Line_Item $total_line_item, EE_Ticket $ticket, $qty = 1) |
| 140 | 140 | { |
| 141 | - if (! $total_line_item instanceof EE_Line_Item || ! $total_line_item->is_total()) { |
|
| 141 | + if ( ! $total_line_item instanceof EE_Line_Item || ! $total_line_item->is_total()) { |
|
| 142 | 142 | throw new EE_Error( |
| 143 | 143 | sprintf( |
| 144 | 144 | esc_html__( |
@@ -153,7 +153,7 @@ discard block |
||
| 153 | 153 | // either increment the qty for an existing ticket |
| 154 | 154 | $line_item = self::increment_ticket_qty_if_already_in_cart($total_line_item, $ticket, $qty); |
| 155 | 155 | // or add a new one |
| 156 | - if (! $line_item instanceof EE_Line_Item) { |
|
| 156 | + if ( ! $line_item instanceof EE_Line_Item) { |
|
| 157 | 157 | $line_item = self::create_ticket_line_item($total_line_item, $ticket, $qty); |
| 158 | 158 | } |
| 159 | 159 | $total_line_item->recalculate_total_including_taxes(); |
@@ -214,7 +214,7 @@ discard block |
||
| 214 | 214 | */ |
| 215 | 215 | public static function increment_quantity(EE_Line_Item $line_item, $qty = 1) |
| 216 | 216 | { |
| 217 | - if (! $line_item->is_percent()) { |
|
| 217 | + if ( ! $line_item->is_percent()) { |
|
| 218 | 218 | $qty += $line_item->quantity(); |
| 219 | 219 | $line_item->set_quantity($qty); |
| 220 | 220 | $line_item->set_total($line_item->unit_price() * $qty); |
@@ -243,7 +243,7 @@ discard block |
||
| 243 | 243 | */ |
| 244 | 244 | public static function decrement_quantity(EE_Line_Item $line_item, $qty = 1) |
| 245 | 245 | { |
| 246 | - if (! $line_item->is_percent()) { |
|
| 246 | + if ( ! $line_item->is_percent()) { |
|
| 247 | 247 | $qty = $line_item->quantity() - $qty; |
| 248 | 248 | $qty = max($qty, 0); |
| 249 | 249 | $line_item->set_quantity($qty); |
@@ -272,7 +272,7 @@ discard block |
||
| 272 | 272 | */ |
| 273 | 273 | public static function update_quantity(EE_Line_Item $line_item, $new_quantity) |
| 274 | 274 | { |
| 275 | - if (! $line_item->is_percent()) { |
|
| 275 | + if ( ! $line_item->is_percent()) { |
|
| 276 | 276 | $line_item->set_quantity($new_quantity); |
| 277 | 277 | $line_item->set_total($line_item->unit_price() * $new_quantity); |
| 278 | 278 | $line_item->save(); |
@@ -312,7 +312,7 @@ discard block |
||
| 312 | 312 | // add $ticket to cart |
| 313 | 313 | $line_item = EE_Line_Item::new_instance(array( |
| 314 | 314 | 'LIN_name' => $ticket->name(), |
| 315 | - 'LIN_desc' => $ticket->description() !== '' ? $ticket->description() . ' ' . $event : $event, |
|
| 315 | + 'LIN_desc' => $ticket->description() !== '' ? $ticket->description().' '.$event : $event, |
|
| 316 | 316 | 'LIN_unit_price' => $ticket->price(), |
| 317 | 317 | 'LIN_quantity' => $qty, |
| 318 | 318 | 'LIN_is_taxable' => $ticket->taxable(), |
@@ -462,7 +462,7 @@ discard block |
||
| 462 | 462 | 'event_espresso' |
| 463 | 463 | ), |
| 464 | 464 | $ticket_line_item->name(), |
| 465 | - current_time(get_option('date_format') . ' ' . get_option('time_format')) |
|
| 465 | + current_time(get_option('date_format').' '.get_option('time_format')) |
|
| 466 | 466 | ), |
| 467 | 467 | 'LIN_unit_price' => 0, // $ticket_line_item->unit_price() |
| 468 | 468 | 'LIN_quantity' => $qty, |
@@ -524,7 +524,7 @@ discard block |
||
| 524 | 524 | ); |
| 525 | 525 | $cancellation_line_item = reset($cancellation_line_item); |
| 526 | 526 | // verify that this ticket was indeed previously cancelled |
| 527 | - if (! $cancellation_line_item instanceof EE_Line_Item) { |
|
| 527 | + if ( ! $cancellation_line_item instanceof EE_Line_Item) { |
|
| 528 | 528 | return false; |
| 529 | 529 | } |
| 530 | 530 | if ($cancellation_line_item->quantity() > $qty) { |
@@ -729,7 +729,7 @@ discard block |
||
| 729 | 729 | 'LIN_code' => 'taxes', |
| 730 | 730 | 'LIN_name' => esc_html__('Taxes', 'event_espresso'), |
| 731 | 731 | 'LIN_type' => EEM_Line_Item::type_tax_sub_total, |
| 732 | - 'LIN_order' => 1000,// this should always come last |
|
| 732 | + 'LIN_order' => 1000, // this should always come last |
|
| 733 | 733 | )); |
| 734 | 734 | $tax_line_item = apply_filters( |
| 735 | 735 | 'FHEE__EEH_Line_Item__create_taxes_subtotal__tax_line_item', |
@@ -785,7 +785,7 @@ discard block |
||
| 785 | 785 | */ |
| 786 | 786 | public static function get_event_code($event) |
| 787 | 787 | { |
| 788 | - return 'event-' . ($event instanceof EE_Event ? $event->ID() : '0'); |
|
| 788 | + return 'event-'.($event instanceof EE_Event ? $event->ID() : '0'); |
|
| 789 | 789 | } |
| 790 | 790 | |
| 791 | 791 | |
@@ -834,7 +834,7 @@ discard block |
||
| 834 | 834 | public static function get_event_line_item_for_ticket(EE_Line_Item $grand_total, EE_Ticket $ticket) |
| 835 | 835 | { |
| 836 | 836 | $first_datetime = $ticket->first_datetime(); |
| 837 | - if (! $first_datetime instanceof EE_Datetime) { |
|
| 837 | + if ( ! $first_datetime instanceof EE_Datetime) { |
|
| 838 | 838 | throw new EE_Error( |
| 839 | 839 | sprintf( |
| 840 | 840 | __('The supplied ticket (ID %d) has no datetimes', 'event_espresso'), |
@@ -843,7 +843,7 @@ discard block |
||
| 843 | 843 | ); |
| 844 | 844 | } |
| 845 | 845 | $event = $first_datetime->event(); |
| 846 | - if (! $event instanceof EE_Event) { |
|
| 846 | + if ( ! $event instanceof EE_Event) { |
|
| 847 | 847 | throw new EE_Error( |
| 848 | 848 | sprintf( |
| 849 | 849 | esc_html__( |
@@ -855,7 +855,7 @@ discard block |
||
| 855 | 855 | ); |
| 856 | 856 | } |
| 857 | 857 | $events_sub_total = EEH_Line_Item::get_event_line_item($grand_total, $event); |
| 858 | - if (! $events_sub_total instanceof EE_Line_Item) { |
|
| 858 | + if ( ! $events_sub_total instanceof EE_Line_Item) { |
|
| 859 | 859 | throw new EE_Error( |
| 860 | 860 | sprintf( |
| 861 | 861 | esc_html__( |
@@ -891,7 +891,7 @@ discard block |
||
| 891 | 891 | $found = false; |
| 892 | 892 | foreach (EEH_Line_Item::get_event_subtotals($grand_total) as $event_line_item) { |
| 893 | 893 | // default event subtotal, we should only ever find this the first time this method is called |
| 894 | - if (! $event_line_item->OBJ_ID()) { |
|
| 894 | + if ( ! $event_line_item->OBJ_ID()) { |
|
| 895 | 895 | // let's use this! but first... set the event details |
| 896 | 896 | EEH_Line_Item::set_event_subtotal_details($event_line_item, $event); |
| 897 | 897 | $found = true; |
@@ -903,7 +903,7 @@ discard block |
||
| 903 | 903 | break; |
| 904 | 904 | } |
| 905 | 905 | } |
| 906 | - if (! $found) { |
|
| 906 | + if ( ! $found) { |
|
| 907 | 907 | // there is no event sub-total yet, so add it |
| 908 | 908 | $pre_tax_subtotal = EEH_Line_Item::get_pre_tax_subtotal($grand_total); |
| 909 | 909 | // create a new "event" subtotal below that |
@@ -1020,7 +1020,7 @@ discard block |
||
| 1020 | 1020 | public static function ensure_taxes_applied($total_line_item) |
| 1021 | 1021 | { |
| 1022 | 1022 | $taxes_subtotal = self::get_taxes_subtotal($total_line_item); |
| 1023 | - if (! $taxes_subtotal->children()) { |
|
| 1023 | + if ( ! $taxes_subtotal->children()) { |
|
| 1024 | 1024 | self::apply_taxes($total_line_item); |
| 1025 | 1025 | } |
| 1026 | 1026 | return $taxes_subtotal->total(); |
@@ -1087,7 +1087,7 @@ discard block |
||
| 1087 | 1087 | do_action('AHEE_log', __FILE__, __FUNCTION__, ''); |
| 1088 | 1088 | |
| 1089 | 1089 | // check if only a single line_item_id was passed |
| 1090 | - if (! empty($line_item_codes) && ! is_array($line_item_codes)) { |
|
| 1090 | + if ( ! empty($line_item_codes) && ! is_array($line_item_codes)) { |
|
| 1091 | 1091 | // place single line_item_id in an array to appear as multiple line_item_ids |
| 1092 | 1092 | $line_item_codes = array($line_item_codes); |
| 1093 | 1093 | } |
@@ -1194,7 +1194,7 @@ discard block |
||
| 1194 | 1194 | if ($code_substring_for_whitelist !== null) { |
| 1195 | 1195 | $whitelisted = strpos($line_item->code(), $code_substring_for_whitelist) !== false; |
| 1196 | 1196 | } |
| 1197 | - if (! $whitelisted && $line_item->is_line_item()) { |
|
| 1197 | + if ( ! $whitelisted && $line_item->is_line_item()) { |
|
| 1198 | 1198 | $line_item->set_is_taxable($taxable); |
| 1199 | 1199 | } |
| 1200 | 1200 | foreach ($line_item->children() as $child_line_item) { |
@@ -1556,7 +1556,7 @@ discard block |
||
| 1556 | 1556 | public static function visualize(EE_Line_Item $line_item, $indentation = 0) |
| 1557 | 1557 | { |
| 1558 | 1558 | echo defined('EE_TESTS_DIR') ? "\n" : '<br />'; |
| 1559 | - if (! $indentation) { |
|
| 1559 | + if ( ! $indentation) { |
|
| 1560 | 1560 | echo defined('EE_TESTS_DIR') ? "\n" : '<br />'; |
| 1561 | 1561 | } |
| 1562 | 1562 | for ($i = 0; $i < $indentation; $i++) { |
@@ -1567,12 +1567,12 @@ discard block |
||
| 1567 | 1567 | if ($line_item->is_percent()) { |
| 1568 | 1568 | $breakdown = "{$line_item->percent()}%"; |
| 1569 | 1569 | } else { |
| 1570 | - $breakdown = '$' . "{$line_item->unit_price()} x {$line_item->quantity()}"; |
|
| 1570 | + $breakdown = '$'."{$line_item->unit_price()} x {$line_item->quantity()}"; |
|
| 1571 | 1571 | } |
| 1572 | 1572 | } |
| 1573 | 1573 | echo $line_item->name(); |
| 1574 | 1574 | echo " [ ID:{$line_item->ID()} | qty:{$line_item->quantity()} ] {$line_item->type()} : "; |
| 1575 | - echo '$' . (string) $line_item->total(); |
|
| 1575 | + echo '$'.(string) $line_item->total(); |
|
| 1576 | 1576 | if ($breakdown) { |
| 1577 | 1577 | echo " ( {$breakdown} )"; |
| 1578 | 1578 | } |
@@ -1660,8 +1660,8 @@ discard block |
||
| 1660 | 1660 | if ($line_item_id === 'taxable') { |
| 1661 | 1661 | continue; |
| 1662 | 1662 | } |
| 1663 | - $taxable_total = $running_totals['taxable'][ $line_item_id ]; |
|
| 1664 | - $running_totals[ $line_item_id ] += ($taxable_total * $tax_percent_decimal); |
|
| 1663 | + $taxable_total = $running_totals['taxable'][$line_item_id]; |
|
| 1664 | + $running_totals[$line_item_id] += ($taxable_total * $tax_percent_decimal); |
|
| 1665 | 1665 | } |
| 1666 | 1666 | break; |
| 1667 | 1667 | |
@@ -1669,7 +1669,7 @@ discard block |
||
| 1669 | 1669 | // ticket line items or ???? |
| 1670 | 1670 | if ($child_line_item->OBJ_type() === EEM_Line_Item::OBJ_TYPE_TICKET) { |
| 1671 | 1671 | // kk it's a ticket |
| 1672 | - if (isset($running_totals[ $child_line_item->ID() ])) { |
|
| 1672 | + if (isset($running_totals[$child_line_item->ID()])) { |
|
| 1673 | 1673 | // huh? that shouldn't happen. |
| 1674 | 1674 | $running_totals['total'] += $child_line_item->total(); |
| 1675 | 1675 | } else { |
@@ -1680,18 +1680,18 @@ discard block |
||
| 1680 | 1680 | $taxable_amount = 0; |
| 1681 | 1681 | } |
| 1682 | 1682 | // are we only calculating totals for some tickets? |
| 1683 | - if (isset($billable_ticket_quantities[ $child_line_item->OBJ_ID() ])) { |
|
| 1684 | - $quantity = $billable_ticket_quantities[ $child_line_item->OBJ_ID() ]; |
|
| 1685 | - $running_totals[ $child_line_item->ID() ] = $quantity |
|
| 1683 | + if (isset($billable_ticket_quantities[$child_line_item->OBJ_ID()])) { |
|
| 1684 | + $quantity = $billable_ticket_quantities[$child_line_item->OBJ_ID()]; |
|
| 1685 | + $running_totals[$child_line_item->ID()] = $quantity |
|
| 1686 | 1686 | ? $child_line_item->unit_price() |
| 1687 | 1687 | : 0; |
| 1688 | - $running_totals['taxable'][ $child_line_item->ID() ] = $quantity |
|
| 1688 | + $running_totals['taxable'][$child_line_item->ID()] = $quantity |
|
| 1689 | 1689 | ? $taxable_amount |
| 1690 | 1690 | : 0; |
| 1691 | 1691 | } else { |
| 1692 | 1692 | $quantity = $child_line_item->quantity(); |
| 1693 | - $running_totals[ $child_line_item->ID() ] = $child_line_item->unit_price(); |
|
| 1694 | - $running_totals['taxable'][ $child_line_item->ID() ] = $taxable_amount; |
|
| 1693 | + $running_totals[$child_line_item->ID()] = $child_line_item->unit_price(); |
|
| 1694 | + $running_totals['taxable'][$child_line_item->ID()] = $taxable_amount; |
|
| 1695 | 1695 | } |
| 1696 | 1696 | $running_totals['taxable']['total'] += $taxable_amount * $quantity; |
| 1697 | 1697 | $running_totals['total'] += $child_line_item->unit_price() * $quantity; |
@@ -1711,12 +1711,12 @@ discard block |
||
| 1711 | 1711 | } |
| 1712 | 1712 | // update the running totals |
| 1713 | 1713 | // yes this actually even works for the running grand total! |
| 1714 | - $running_totals[ $line_item_id ] = |
|
| 1714 | + $running_totals[$line_item_id] = |
|
| 1715 | 1715 | $line_items_percent_of_running_total * $this_running_total; |
| 1716 | 1716 | |
| 1717 | 1717 | if ($child_line_item->is_taxable()) { |
| 1718 | - $running_totals['taxable'][ $line_item_id ] = |
|
| 1719 | - $line_items_percent_of_running_total * $running_totals['taxable'][ $line_item_id ]; |
|
| 1718 | + $running_totals['taxable'][$line_item_id] = |
|
| 1719 | + $line_items_percent_of_running_total * $running_totals['taxable'][$line_item_id]; |
|
| 1720 | 1720 | } |
| 1721 | 1721 | } |
| 1722 | 1722 | } |
@@ -1743,14 +1743,14 @@ discard block |
||
| 1743 | 1743 | EE_Line_Item $ticket_line_item |
| 1744 | 1744 | ) { |
| 1745 | 1745 | static $final_prices_per_ticket_line_item = array(); |
| 1746 | - if (empty($final_prices_per_ticket_line_item) || empty($final_prices_per_ticket_line_item[ $total_line_item->ID() ])) { |
|
| 1747 | - $final_prices_per_ticket_line_item[ $total_line_item->ID() ] = EEH_Line_Item::calculate_reg_final_prices_per_line_item( |
|
| 1746 | + if (empty($final_prices_per_ticket_line_item) || empty($final_prices_per_ticket_line_item[$total_line_item->ID()])) { |
|
| 1747 | + $final_prices_per_ticket_line_item[$total_line_item->ID()] = EEH_Line_Item::calculate_reg_final_prices_per_line_item( |
|
| 1748 | 1748 | $total_line_item |
| 1749 | 1749 | ); |
| 1750 | 1750 | } |
| 1751 | 1751 | // ok now find this new registration's final price |
| 1752 | - if (isset($final_prices_per_ticket_line_item[ $total_line_item->ID() ][ $ticket_line_item->ID() ])) { |
|
| 1753 | - return $final_prices_per_ticket_line_item[ $total_line_item->ID() ][ $ticket_line_item->ID() ]; |
|
| 1752 | + if (isset($final_prices_per_ticket_line_item[$total_line_item->ID()][$ticket_line_item->ID()])) { |
|
| 1753 | + return $final_prices_per_ticket_line_item[$total_line_item->ID()][$ticket_line_item->ID()]; |
|
| 1754 | 1754 | } |
| 1755 | 1755 | $message = sprintf( |
| 1756 | 1756 | esc_html__( |
@@ -1760,7 +1760,7 @@ discard block |
||
| 1760 | 1760 | $ticket_line_item->ID() |
| 1761 | 1761 | ); |
| 1762 | 1762 | if (WP_DEBUG) { |
| 1763 | - $message .= '<br>' . print_r($final_prices_per_ticket_line_item, true); |
|
| 1763 | + $message .= '<br>'.print_r($final_prices_per_ticket_line_item, true); |
|
| 1764 | 1764 | throw new OutOfRangeException($message); |
| 1765 | 1765 | } |
| 1766 | 1766 | EE_Log::instance()->log(__CLASS__, __FUNCTION__, $message); |
@@ -23,1679 +23,1679 @@ |
||
| 23 | 23 | class EE_Registry implements ResettableInterface |
| 24 | 24 | { |
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * @var EE_Registry $_instance |
|
| 28 | - */ |
|
| 29 | - private static $_instance; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * @var EE_Dependency_Map $_dependency_map |
|
| 33 | - */ |
|
| 34 | - protected $_dependency_map; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * @var Mirror |
|
| 38 | - */ |
|
| 39 | - private $mirror; |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * @var ClassInterfaceCache $class_cache |
|
| 43 | - */ |
|
| 44 | - private $class_cache; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * @var array $_class_abbreviations |
|
| 48 | - */ |
|
| 49 | - protected $_class_abbreviations = array(); |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * @var CommandBusInterface $BUS |
|
| 53 | - */ |
|
| 54 | - public $BUS; |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * @var EE_Cart $CART |
|
| 58 | - */ |
|
| 59 | - public $CART; |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * @var EE_Config $CFG |
|
| 63 | - */ |
|
| 64 | - public $CFG; |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * @var EE_Network_Config $NET_CFG |
|
| 68 | - */ |
|
| 69 | - public $NET_CFG; |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * StdClass object for storing library classes in |
|
| 73 | - * |
|
| 74 | - * @var RegistryContainer $LIB |
|
| 75 | - */ |
|
| 76 | - public $LIB; |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * @var EE_Request_Handler $REQ |
|
| 80 | - */ |
|
| 81 | - public $REQ; |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * @var EE_Session $SSN |
|
| 85 | - */ |
|
| 86 | - public $SSN; |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * @since 4.5.0 |
|
| 90 | - * @var EE_Capabilities $CAP |
|
| 91 | - */ |
|
| 92 | - public $CAP; |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * @since 4.9.0 |
|
| 96 | - * @var EE_Message_Resource_Manager $MRM |
|
| 97 | - */ |
|
| 98 | - public $MRM; |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * @var Registry $AssetsRegistry |
|
| 102 | - */ |
|
| 103 | - public $AssetsRegistry; |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * StdClass object for holding addons which have registered themselves to work with EE core |
|
| 107 | - * |
|
| 108 | - * @var EE_Addon[] $addons |
|
| 109 | - */ |
|
| 110 | - public $addons; |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * keys are 'short names' (eg Event), values are class names (eg 'EEM_Event') |
|
| 114 | - * |
|
| 115 | - * @var EEM_Base[] $models |
|
| 116 | - */ |
|
| 117 | - public $models = array(); |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * @var EED_Module[] $modules |
|
| 121 | - */ |
|
| 122 | - public $modules; |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * @var EES_Shortcode[] $shortcodes |
|
| 126 | - */ |
|
| 127 | - public $shortcodes; |
|
| 128 | - |
|
| 129 | - /** |
|
| 130 | - * @var WP_Widget[] $widgets |
|
| 131 | - */ |
|
| 132 | - public $widgets; |
|
| 133 | - |
|
| 134 | - /** |
|
| 135 | - * this is an array of all implemented model names (i.e. not the parent abstract models, or models |
|
| 136 | - * which don't actually fetch items from the DB in the normal way (ie, are not children of EEM_Base)). |
|
| 137 | - * Keys are model "short names" (eg "Event") as used in model relations, and values are |
|
| 138 | - * classnames (eg "EEM_Event") |
|
| 139 | - * |
|
| 140 | - * @var array $non_abstract_db_models |
|
| 141 | - */ |
|
| 142 | - public $non_abstract_db_models = array(); |
|
| 143 | - |
|
| 144 | - /** |
|
| 145 | - * internationalization for JS strings |
|
| 146 | - * usage: EE_Registry::i18n_js_strings['string_key'] = esc_html__( 'string to translate.', 'event_espresso' ); |
|
| 147 | - * in js file: var translatedString = eei18n.string_key; |
|
| 148 | - * |
|
| 149 | - * @var array $i18n_js_strings |
|
| 150 | - */ |
|
| 151 | - public static $i18n_js_strings = array(); |
|
| 152 | - |
|
| 153 | - /** |
|
| 154 | - * $main_file - path to espresso.php |
|
| 155 | - * |
|
| 156 | - * @var array $main_file |
|
| 157 | - */ |
|
| 158 | - public $main_file; |
|
| 159 | - |
|
| 160 | - /** |
|
| 161 | - * array of ReflectionClass objects where the key is the class name |
|
| 162 | - * |
|
| 163 | - * @deprecated 4.9.62.p |
|
| 164 | - * @var ReflectionClass[] $_reflectors |
|
| 165 | - */ |
|
| 166 | - public $_reflectors; |
|
| 167 | - |
|
| 168 | - /** |
|
| 169 | - * boolean flag to indicate whether or not to load/save dependencies from/to the cache |
|
| 170 | - * |
|
| 171 | - * @var boolean $_cache_on |
|
| 172 | - */ |
|
| 173 | - protected $_cache_on = true; |
|
| 174 | - |
|
| 175 | - /** |
|
| 176 | - * @var ObjectIdentifier |
|
| 177 | - */ |
|
| 178 | - private $object_identifier; |
|
| 179 | - |
|
| 180 | - |
|
| 181 | - /** |
|
| 182 | - * @singleton method used to instantiate class object |
|
| 183 | - * @param EE_Dependency_Map|null $dependency_map |
|
| 184 | - * @param Mirror|null $mirror |
|
| 185 | - * @param ClassInterfaceCache|null $class_cache |
|
| 186 | - * @param ObjectIdentifier|null $object_identifier |
|
| 187 | - * @return EE_Registry instance |
|
| 188 | - */ |
|
| 189 | - public static function instance( |
|
| 190 | - EE_Dependency_Map $dependency_map = null, |
|
| 191 | - Mirror $mirror = null, |
|
| 192 | - ClassInterfaceCache $class_cache = null, |
|
| 193 | - ObjectIdentifier $object_identifier = null |
|
| 194 | - ) { |
|
| 195 | - // check if class object is instantiated |
|
| 196 | - if (! self::$_instance instanceof EE_Registry |
|
| 197 | - && $dependency_map instanceof EE_Dependency_Map |
|
| 198 | - && $mirror instanceof Mirror |
|
| 199 | - && $class_cache instanceof ClassInterfaceCache |
|
| 200 | - && $object_identifier instanceof ObjectIdentifier |
|
| 201 | - ) { |
|
| 202 | - self::$_instance = new self( |
|
| 203 | - $dependency_map, |
|
| 204 | - $mirror, |
|
| 205 | - $class_cache, |
|
| 206 | - $object_identifier |
|
| 207 | - ); |
|
| 208 | - } |
|
| 209 | - return self::$_instance; |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - |
|
| 213 | - /** |
|
| 214 | - * protected constructor to prevent direct creation |
|
| 215 | - * |
|
| 216 | - * @Constructor |
|
| 217 | - * @param EE_Dependency_Map $dependency_map |
|
| 218 | - * @param Mirror $mirror |
|
| 219 | - * @param ClassInterfaceCache $class_cache |
|
| 220 | - * @param ObjectIdentifier $object_identifier |
|
| 221 | - */ |
|
| 222 | - protected function __construct( |
|
| 223 | - EE_Dependency_Map $dependency_map, |
|
| 224 | - Mirror $mirror, |
|
| 225 | - ClassInterfaceCache $class_cache, |
|
| 226 | - ObjectIdentifier $object_identifier |
|
| 227 | - ) { |
|
| 228 | - $this->_dependency_map = $dependency_map; |
|
| 229 | - $this->mirror = $mirror; |
|
| 230 | - $this->class_cache = $class_cache; |
|
| 231 | - $this->object_identifier = $object_identifier; |
|
| 232 | - // $registry_container = new RegistryContainer(); |
|
| 233 | - $this->LIB = new RegistryContainer(); |
|
| 234 | - $this->addons = new RegistryContainer(); |
|
| 235 | - $this->modules = new RegistryContainer(); |
|
| 236 | - $this->shortcodes = new RegistryContainer(); |
|
| 237 | - $this->widgets = new RegistryContainer(); |
|
| 238 | - add_action('EE_Load_Espresso_Core__handle_request__initialize_core_loading', array($this, 'initialize')); |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - |
|
| 242 | - /** |
|
| 243 | - * initialize |
|
| 244 | - * |
|
| 245 | - * @throws OutOfBoundsException |
|
| 246 | - * @throws InvalidArgumentException |
|
| 247 | - * @throws InvalidInterfaceException |
|
| 248 | - * @throws InvalidDataTypeException |
|
| 249 | - * @throws EE_Error |
|
| 250 | - * @throws ReflectionException |
|
| 251 | - */ |
|
| 252 | - public function initialize() |
|
| 253 | - { |
|
| 254 | - $this->_class_abbreviations = apply_filters( |
|
| 255 | - 'FHEE__EE_Registry____construct___class_abbreviations', |
|
| 256 | - array( |
|
| 257 | - 'EE_Config' => 'CFG', |
|
| 258 | - 'EE_Session' => 'SSN', |
|
| 259 | - 'EE_Capabilities' => 'CAP', |
|
| 260 | - 'EE_Cart' => 'CART', |
|
| 261 | - 'EE_Network_Config' => 'NET_CFG', |
|
| 262 | - 'EE_Request_Handler' => 'REQ', |
|
| 263 | - 'EE_Message_Resource_Manager' => 'MRM', |
|
| 264 | - 'EventEspresso\core\services\commands\CommandBus' => 'BUS', |
|
| 265 | - 'EventEspresso\core\services\assets\Registry' => 'AssetsRegistry', |
|
| 266 | - ) |
|
| 267 | - ); |
|
| 268 | - $this->load_core('Base', array(), true); |
|
| 269 | - // add our request and response objects to the cache |
|
| 270 | - $request_loader = $this->_dependency_map->class_loader( |
|
| 271 | - 'EventEspresso\core\services\request\Request' |
|
| 272 | - ); |
|
| 273 | - $this->_set_cached_class( |
|
| 274 | - $request_loader(), |
|
| 275 | - 'EventEspresso\core\services\request\Request' |
|
| 276 | - ); |
|
| 277 | - $response_loader = $this->_dependency_map->class_loader( |
|
| 278 | - 'EventEspresso\core\services\request\Response' |
|
| 279 | - ); |
|
| 280 | - $this->_set_cached_class( |
|
| 281 | - $response_loader(), |
|
| 282 | - 'EventEspresso\core\services\request\Response' |
|
| 283 | - ); |
|
| 284 | - add_action('AHEE__EE_System__set_hooks_for_core', array($this, 'init')); |
|
| 285 | - } |
|
| 286 | - |
|
| 287 | - |
|
| 288 | - /** |
|
| 289 | - * @return void |
|
| 290 | - */ |
|
| 291 | - public function init() |
|
| 292 | - { |
|
| 293 | - // Get current page protocol |
|
| 294 | - $protocol = isset($_SERVER['HTTPS']) ? 'https://' : 'http://'; |
|
| 295 | - // Output admin-ajax.php URL with same protocol as current page |
|
| 296 | - self::$i18n_js_strings['ajax_url'] = admin_url('admin-ajax.php', $protocol); |
|
| 297 | - self::$i18n_js_strings['wp_debug'] = defined('WP_DEBUG') ? WP_DEBUG : false; |
|
| 298 | - } |
|
| 299 | - |
|
| 300 | - |
|
| 301 | - /** |
|
| 302 | - * localize_i18n_js_strings |
|
| 303 | - * |
|
| 304 | - * @return string |
|
| 305 | - */ |
|
| 306 | - public static function localize_i18n_js_strings() |
|
| 307 | - { |
|
| 308 | - $i18n_js_strings = (array) self::$i18n_js_strings; |
|
| 309 | - foreach ($i18n_js_strings as $key => $value) { |
|
| 310 | - if (is_scalar($value)) { |
|
| 311 | - $i18n_js_strings[ $key ] = html_entity_decode((string) $value, ENT_QUOTES, 'UTF-8'); |
|
| 312 | - } |
|
| 313 | - } |
|
| 314 | - return '/* <![CDATA[ */ var eei18n = ' . wp_json_encode($i18n_js_strings) . '; /* ]]> */'; |
|
| 315 | - } |
|
| 316 | - |
|
| 317 | - |
|
| 318 | - /** |
|
| 319 | - * @param mixed string | EED_Module $module |
|
| 320 | - * @throws OutOfBoundsException |
|
| 321 | - * @throws InvalidArgumentException |
|
| 322 | - * @throws InvalidInterfaceException |
|
| 323 | - * @throws InvalidDataTypeException |
|
| 324 | - * @throws EE_Error |
|
| 325 | - * @throws ReflectionException |
|
| 326 | - */ |
|
| 327 | - public function add_module($module) |
|
| 328 | - { |
|
| 329 | - if ($module instanceof EED_Module) { |
|
| 330 | - $module_class = get_class($module); |
|
| 331 | - $this->modules->{$module_class} = $module; |
|
| 332 | - } else { |
|
| 333 | - if (! class_exists('EE_Module_Request_Router', false)) { |
|
| 334 | - $this->load_core('Module_Request_Router'); |
|
| 335 | - } |
|
| 336 | - EE_Module_Request_Router::module_factory($module); |
|
| 337 | - } |
|
| 338 | - } |
|
| 339 | - |
|
| 340 | - |
|
| 341 | - /** |
|
| 342 | - * @param string $module_name |
|
| 343 | - * @return mixed EED_Module | NULL |
|
| 344 | - */ |
|
| 345 | - public function get_module($module_name = '') |
|
| 346 | - { |
|
| 347 | - return isset($this->modules->{$module_name}) |
|
| 348 | - ? $this->modules->{$module_name} |
|
| 349 | - : null; |
|
| 350 | - } |
|
| 351 | - |
|
| 352 | - |
|
| 353 | - /** |
|
| 354 | - * loads core classes - must be singletons |
|
| 355 | - * |
|
| 356 | - * @param string $class_name - simple class name ie: session |
|
| 357 | - * @param mixed $arguments |
|
| 358 | - * @param bool $load_only |
|
| 359 | - * @return mixed |
|
| 360 | - * @throws InvalidInterfaceException |
|
| 361 | - * @throws InvalidDataTypeException |
|
| 362 | - * @throws EE_Error |
|
| 363 | - * @throws ReflectionException |
|
| 364 | - * @throws InvalidArgumentException |
|
| 365 | - */ |
|
| 366 | - public function load_core($class_name, $arguments = array(), $load_only = false) |
|
| 367 | - { |
|
| 368 | - $core_paths = apply_filters( |
|
| 369 | - 'FHEE__EE_Registry__load_core__core_paths', |
|
| 370 | - array( |
|
| 371 | - EE_CORE, |
|
| 372 | - EE_ADMIN, |
|
| 373 | - EE_CPTS, |
|
| 374 | - EE_CORE . 'data_migration_scripts' . DS, |
|
| 375 | - EE_CORE . 'capabilities' . DS, |
|
| 376 | - EE_CORE . 'request_stack' . DS, |
|
| 377 | - EE_CORE . 'middleware' . DS, |
|
| 378 | - ) |
|
| 379 | - ); |
|
| 380 | - // retrieve instantiated class |
|
| 381 | - return $this->_load( |
|
| 382 | - $core_paths, |
|
| 383 | - 'EE_', |
|
| 384 | - $class_name, |
|
| 385 | - 'core', |
|
| 386 | - $arguments, |
|
| 387 | - false, |
|
| 388 | - true, |
|
| 389 | - $load_only |
|
| 390 | - ); |
|
| 391 | - } |
|
| 392 | - |
|
| 393 | - |
|
| 394 | - /** |
|
| 395 | - * loads service classes |
|
| 396 | - * |
|
| 397 | - * @param string $class_name - simple class name ie: session |
|
| 398 | - * @param mixed $arguments |
|
| 399 | - * @param bool $load_only |
|
| 400 | - * @return mixed |
|
| 401 | - * @throws InvalidInterfaceException |
|
| 402 | - * @throws InvalidDataTypeException |
|
| 403 | - * @throws EE_Error |
|
| 404 | - * @throws ReflectionException |
|
| 405 | - * @throws InvalidArgumentException |
|
| 406 | - */ |
|
| 407 | - public function load_service($class_name, $arguments = array(), $load_only = false) |
|
| 408 | - { |
|
| 409 | - $service_paths = apply_filters( |
|
| 410 | - 'FHEE__EE_Registry__load_service__service_paths', |
|
| 411 | - array( |
|
| 412 | - EE_CORE . 'services' . DS, |
|
| 413 | - ) |
|
| 414 | - ); |
|
| 415 | - // retrieve instantiated class |
|
| 416 | - return $this->_load( |
|
| 417 | - $service_paths, |
|
| 418 | - 'EE_', |
|
| 419 | - $class_name, |
|
| 420 | - 'class', |
|
| 421 | - $arguments, |
|
| 422 | - false, |
|
| 423 | - true, |
|
| 424 | - $load_only |
|
| 425 | - ); |
|
| 426 | - } |
|
| 427 | - |
|
| 428 | - |
|
| 429 | - /** |
|
| 430 | - * loads data_migration_scripts |
|
| 431 | - * |
|
| 432 | - * @param string $class_name - class name for the DMS ie: EE_DMS_Core_4_2_0 |
|
| 433 | - * @param mixed $arguments |
|
| 434 | - * @return EE_Data_Migration_Script_Base|mixed |
|
| 435 | - * @throws InvalidInterfaceException |
|
| 436 | - * @throws InvalidDataTypeException |
|
| 437 | - * @throws EE_Error |
|
| 438 | - * @throws ReflectionException |
|
| 439 | - * @throws InvalidArgumentException |
|
| 440 | - */ |
|
| 441 | - public function load_dms($class_name, $arguments = array()) |
|
| 442 | - { |
|
| 443 | - // retrieve instantiated class |
|
| 444 | - return $this->_load( |
|
| 445 | - EE_Data_Migration_Manager::instance()->get_data_migration_script_folders(), |
|
| 446 | - 'EE_DMS_', |
|
| 447 | - $class_name, |
|
| 448 | - 'dms', |
|
| 449 | - $arguments, |
|
| 450 | - false, |
|
| 451 | - false |
|
| 452 | - ); |
|
| 453 | - } |
|
| 454 | - |
|
| 455 | - |
|
| 456 | - /** |
|
| 457 | - * loads object creating classes - must be singletons |
|
| 458 | - * |
|
| 459 | - * @param string $class_name - simple class name ie: attendee |
|
| 460 | - * @param mixed $arguments - an array of arguments to pass to the class |
|
| 461 | - * @param bool $from_db - some classes are instantiated from the db and thus call a different method to |
|
| 462 | - * instantiate |
|
| 463 | - * @param bool $cache if you don't want the class to be stored in the internal cache (non-persistent) then |
|
| 464 | - * set this to FALSE (ie. when instantiating model objects from client in a loop) |
|
| 465 | - * @param bool $load_only whether or not to just load the file and NOT instantiate, or load AND instantiate |
|
| 466 | - * (default) |
|
| 467 | - * @return EE_Base_Class | bool |
|
| 468 | - * @throws InvalidInterfaceException |
|
| 469 | - * @throws InvalidDataTypeException |
|
| 470 | - * @throws EE_Error |
|
| 471 | - * @throws ReflectionException |
|
| 472 | - * @throws InvalidArgumentException |
|
| 473 | - */ |
|
| 474 | - public function load_class($class_name, $arguments = array(), $from_db = false, $cache = true, $load_only = false) |
|
| 475 | - { |
|
| 476 | - $paths = apply_filters( |
|
| 477 | - 'FHEE__EE_Registry__load_class__paths', |
|
| 478 | - array( |
|
| 479 | - EE_CORE, |
|
| 480 | - EE_CLASSES, |
|
| 481 | - EE_BUSINESS, |
|
| 482 | - ) |
|
| 483 | - ); |
|
| 484 | - // retrieve instantiated class |
|
| 485 | - return $this->_load( |
|
| 486 | - $paths, |
|
| 487 | - 'EE_', |
|
| 488 | - $class_name, |
|
| 489 | - 'class', |
|
| 490 | - $arguments, |
|
| 491 | - $from_db, |
|
| 492 | - $cache, |
|
| 493 | - $load_only |
|
| 494 | - ); |
|
| 495 | - } |
|
| 496 | - |
|
| 497 | - |
|
| 498 | - /** |
|
| 499 | - * loads helper classes - must be singletons |
|
| 500 | - * |
|
| 501 | - * @param string $class_name - simple class name ie: price |
|
| 502 | - * @param mixed $arguments |
|
| 503 | - * @param bool $load_only |
|
| 504 | - * @return EEH_Base | bool |
|
| 505 | - * @throws InvalidInterfaceException |
|
| 506 | - * @throws InvalidDataTypeException |
|
| 507 | - * @throws EE_Error |
|
| 508 | - * @throws ReflectionException |
|
| 509 | - * @throws InvalidArgumentException |
|
| 510 | - */ |
|
| 511 | - public function load_helper($class_name, $arguments = array(), $load_only = true) |
|
| 512 | - { |
|
| 513 | - // todo: add doing_it_wrong() in a few versions after all addons have had calls to this method removed |
|
| 514 | - $helper_paths = apply_filters('FHEE__EE_Registry__load_helper__helper_paths', array(EE_HELPERS)); |
|
| 515 | - // retrieve instantiated class |
|
| 516 | - return $this->_load( |
|
| 517 | - $helper_paths, |
|
| 518 | - 'EEH_', |
|
| 519 | - $class_name, |
|
| 520 | - 'helper', |
|
| 521 | - $arguments, |
|
| 522 | - false, |
|
| 523 | - true, |
|
| 524 | - $load_only |
|
| 525 | - ); |
|
| 526 | - } |
|
| 527 | - |
|
| 528 | - |
|
| 529 | - /** |
|
| 530 | - * loads core classes - must be singletons |
|
| 531 | - * |
|
| 532 | - * @param string $class_name - simple class name ie: session |
|
| 533 | - * @param mixed $arguments |
|
| 534 | - * @param bool $load_only |
|
| 535 | - * @param bool $cache whether to cache the object or not. |
|
| 536 | - * @return mixed |
|
| 537 | - * @throws InvalidInterfaceException |
|
| 538 | - * @throws InvalidDataTypeException |
|
| 539 | - * @throws EE_Error |
|
| 540 | - * @throws ReflectionException |
|
| 541 | - * @throws InvalidArgumentException |
|
| 542 | - */ |
|
| 543 | - public function load_lib($class_name, $arguments = array(), $load_only = false, $cache = true) |
|
| 544 | - { |
|
| 545 | - $paths = array( |
|
| 546 | - EE_LIBRARIES, |
|
| 547 | - EE_LIBRARIES . 'messages' . DS, |
|
| 548 | - EE_LIBRARIES . 'shortcodes' . DS, |
|
| 549 | - EE_LIBRARIES . 'qtips' . DS, |
|
| 550 | - EE_LIBRARIES . 'payment_methods' . DS, |
|
| 551 | - ); |
|
| 552 | - // retrieve instantiated class |
|
| 553 | - return $this->_load( |
|
| 554 | - $paths, |
|
| 555 | - 'EE_', |
|
| 556 | - $class_name, |
|
| 557 | - 'lib', |
|
| 558 | - $arguments, |
|
| 559 | - false, |
|
| 560 | - $cache, |
|
| 561 | - $load_only |
|
| 562 | - ); |
|
| 563 | - } |
|
| 564 | - |
|
| 565 | - |
|
| 566 | - /** |
|
| 567 | - * loads model classes - must be singletons |
|
| 568 | - * |
|
| 569 | - * @param string $class_name - simple class name ie: price |
|
| 570 | - * @param mixed $arguments |
|
| 571 | - * @param bool $load_only |
|
| 572 | - * @return EEM_Base | bool |
|
| 573 | - * @throws InvalidInterfaceException |
|
| 574 | - * @throws InvalidDataTypeException |
|
| 575 | - * @throws EE_Error |
|
| 576 | - * @throws ReflectionException |
|
| 577 | - * @throws InvalidArgumentException |
|
| 578 | - */ |
|
| 579 | - public function load_model($class_name, $arguments = array(), $load_only = false) |
|
| 580 | - { |
|
| 581 | - $paths = apply_filters( |
|
| 582 | - 'FHEE__EE_Registry__load_model__paths', |
|
| 583 | - array( |
|
| 584 | - EE_MODELS, |
|
| 585 | - EE_CORE, |
|
| 586 | - ) |
|
| 587 | - ); |
|
| 588 | - // retrieve instantiated class |
|
| 589 | - return $this->_load( |
|
| 590 | - $paths, |
|
| 591 | - 'EEM_', |
|
| 592 | - $class_name, |
|
| 593 | - 'model', |
|
| 594 | - $arguments, |
|
| 595 | - false, |
|
| 596 | - true, |
|
| 597 | - $load_only |
|
| 598 | - ); |
|
| 599 | - } |
|
| 600 | - |
|
| 601 | - |
|
| 602 | - /** |
|
| 603 | - * loads model classes - must be singletons |
|
| 604 | - * |
|
| 605 | - * @param string $class_name - simple class name ie: price |
|
| 606 | - * @param mixed $arguments |
|
| 607 | - * @param bool $load_only |
|
| 608 | - * @return mixed | bool |
|
| 609 | - * @throws InvalidInterfaceException |
|
| 610 | - * @throws InvalidDataTypeException |
|
| 611 | - * @throws EE_Error |
|
| 612 | - * @throws ReflectionException |
|
| 613 | - * @throws InvalidArgumentException |
|
| 614 | - */ |
|
| 615 | - public function load_model_class($class_name, $arguments = array(), $load_only = true) |
|
| 616 | - { |
|
| 617 | - $paths = array( |
|
| 618 | - EE_MODELS . 'fields' . DS, |
|
| 619 | - EE_MODELS . 'helpers' . DS, |
|
| 620 | - EE_MODELS . 'relations' . DS, |
|
| 621 | - EE_MODELS . 'strategies' . DS, |
|
| 622 | - ); |
|
| 623 | - // retrieve instantiated class |
|
| 624 | - return $this->_load( |
|
| 625 | - $paths, |
|
| 626 | - 'EE_', |
|
| 627 | - $class_name, |
|
| 628 | - '', |
|
| 629 | - $arguments, |
|
| 630 | - false, |
|
| 631 | - true, |
|
| 632 | - $load_only |
|
| 633 | - ); |
|
| 634 | - } |
|
| 635 | - |
|
| 636 | - |
|
| 637 | - /** |
|
| 638 | - * Determines if $model_name is the name of an actual EE model. |
|
| 639 | - * |
|
| 640 | - * @param string $model_name like Event, Attendee, Question_Group_Question, etc. |
|
| 641 | - * @return boolean |
|
| 642 | - */ |
|
| 643 | - public function is_model_name($model_name) |
|
| 644 | - { |
|
| 645 | - return isset($this->models[ $model_name ]); |
|
| 646 | - } |
|
| 647 | - |
|
| 648 | - |
|
| 649 | - /** |
|
| 650 | - * generic class loader |
|
| 651 | - * |
|
| 652 | - * @param string $path_to_file - directory path to file location, not including filename |
|
| 653 | - * @param string $file_name - file name ie: my_file.php, including extension |
|
| 654 | - * @param string $type - file type - core? class? helper? model? |
|
| 655 | - * @param mixed $arguments |
|
| 656 | - * @param bool $load_only |
|
| 657 | - * @return mixed |
|
| 658 | - * @throws InvalidInterfaceException |
|
| 659 | - * @throws InvalidDataTypeException |
|
| 660 | - * @throws EE_Error |
|
| 661 | - * @throws ReflectionException |
|
| 662 | - * @throws InvalidArgumentException |
|
| 663 | - */ |
|
| 664 | - public function load_file($path_to_file, $file_name, $type = '', $arguments = array(), $load_only = true) |
|
| 665 | - { |
|
| 666 | - // retrieve instantiated class |
|
| 667 | - return $this->_load( |
|
| 668 | - $path_to_file, |
|
| 669 | - '', |
|
| 670 | - $file_name, |
|
| 671 | - $type, |
|
| 672 | - $arguments, |
|
| 673 | - false, |
|
| 674 | - true, |
|
| 675 | - $load_only |
|
| 676 | - ); |
|
| 677 | - } |
|
| 678 | - |
|
| 679 | - |
|
| 680 | - /** |
|
| 681 | - * @param string $path_to_file - directory path to file location, not including filename |
|
| 682 | - * @param string $class_name - full class name ie: My_Class |
|
| 683 | - * @param string $type - file type - core? class? helper? model? |
|
| 684 | - * @param mixed $arguments |
|
| 685 | - * @param bool $load_only |
|
| 686 | - * @return bool|EE_Addon|object |
|
| 687 | - * @throws InvalidInterfaceException |
|
| 688 | - * @throws InvalidDataTypeException |
|
| 689 | - * @throws EE_Error |
|
| 690 | - * @throws ReflectionException |
|
| 691 | - * @throws InvalidArgumentException |
|
| 692 | - */ |
|
| 693 | - public function load_addon($path_to_file, $class_name, $type = 'class', $arguments = array(), $load_only = false) |
|
| 694 | - { |
|
| 695 | - // retrieve instantiated class |
|
| 696 | - return $this->_load( |
|
| 697 | - $path_to_file, |
|
| 698 | - 'addon', |
|
| 699 | - $class_name, |
|
| 700 | - $type, |
|
| 701 | - $arguments, |
|
| 702 | - false, |
|
| 703 | - true, |
|
| 704 | - $load_only |
|
| 705 | - ); |
|
| 706 | - } |
|
| 707 | - |
|
| 708 | - |
|
| 709 | - /** |
|
| 710 | - * instantiates, caches, and automatically resolves dependencies |
|
| 711 | - * for classes that use a Fully Qualified Class Name. |
|
| 712 | - * if the class is not capable of being loaded using PSR-4 autoloading, |
|
| 713 | - * then you need to use one of the existing load_*() methods |
|
| 714 | - * which can resolve the classname and filepath from the passed arguments |
|
| 715 | - * |
|
| 716 | - * @param bool|string $class_name Fully Qualified Class Name |
|
| 717 | - * @param array $arguments an argument, or array of arguments to pass to the class upon instantiation |
|
| 718 | - * @param bool $cache whether to cache the instantiated object for reuse |
|
| 719 | - * @param bool $from_db some classes are instantiated from the db |
|
| 720 | - * and thus call a different method to instantiate |
|
| 721 | - * @param bool $load_only if true, will only load the file, but will NOT instantiate an object |
|
| 722 | - * @param bool|string $addon if true, will cache the object in the EE_Registry->$addons array |
|
| 723 | - * @return bool|null|mixed null = failure to load or instantiate class object. |
|
| 724 | - * object = class loaded and instantiated successfully. |
|
| 725 | - * bool = fail or success when $load_only is true |
|
| 726 | - * @throws InvalidInterfaceException |
|
| 727 | - * @throws InvalidDataTypeException |
|
| 728 | - * @throws EE_Error |
|
| 729 | - * @throws ReflectionException |
|
| 730 | - * @throws InvalidArgumentException |
|
| 731 | - */ |
|
| 732 | - public function create( |
|
| 733 | - $class_name = false, |
|
| 734 | - $arguments = array(), |
|
| 735 | - $cache = false, |
|
| 736 | - $from_db = false, |
|
| 737 | - $load_only = false, |
|
| 738 | - $addon = false |
|
| 739 | - ) { |
|
| 740 | - $class_name = ltrim($class_name, '\\'); |
|
| 741 | - $class_name = $this->class_cache->getFqnForAlias($class_name); |
|
| 742 | - $class_exists = $this->loadOrVerifyClassExists($class_name, $arguments); |
|
| 743 | - // if a non-FQCN was passed, then |
|
| 744 | - // verifyClassExists() might return an object |
|
| 745 | - // or it could return null if the class just could not be found anywhere |
|
| 746 | - if ($class_exists instanceof $class_name || $class_exists === null) { |
|
| 747 | - // either way, return the results |
|
| 748 | - return $class_exists; |
|
| 749 | - } |
|
| 750 | - $class_name = $class_exists; |
|
| 751 | - // if we're only loading the class and it already exists, then let's just return true immediately |
|
| 752 | - if ($load_only) { |
|
| 753 | - return true; |
|
| 754 | - } |
|
| 755 | - $addon = $addon ? 'addon' : ''; |
|
| 756 | - // $this->_cache_on is toggled during the recursive loading that can occur with dependency injection |
|
| 757 | - // $cache is controlled by individual calls to separate Registry loader methods like load_class() |
|
| 758 | - // $load_only is also controlled by individual calls to separate Registry loader methods like load_file() |
|
| 759 | - if ($this->_cache_on && $cache && ! $load_only) { |
|
| 760 | - // return object if it's already cached |
|
| 761 | - $cached_class = $this->_get_cached_class($class_name, $addon, $arguments); |
|
| 762 | - if ($cached_class !== null) { |
|
| 763 | - return $cached_class; |
|
| 764 | - } |
|
| 765 | - }// obtain the loader method from the dependency map |
|
| 766 | - $loader = $this->_dependency_map->class_loader($class_name);// instantiate the requested object |
|
| 767 | - if ($loader instanceof Closure) { |
|
| 768 | - $class_obj = $loader($arguments); |
|
| 769 | - } else { |
|
| 770 | - if ($loader && method_exists($this, $loader)) { |
|
| 771 | - $class_obj = $this->{$loader}($class_name, $arguments); |
|
| 772 | - } else { |
|
| 773 | - $class_obj = $this->_create_object($class_name, $arguments, $addon, $from_db); |
|
| 774 | - } |
|
| 775 | - } |
|
| 776 | - if (($this->_cache_on && $cache) || $this->get_class_abbreviation($class_name, '')) { |
|
| 777 | - // save it for later... kinda like gum { : $ |
|
| 778 | - $this->_set_cached_class( |
|
| 779 | - $class_obj, |
|
| 780 | - $class_name, |
|
| 781 | - $addon, |
|
| 782 | - $from_db, |
|
| 783 | - $arguments |
|
| 784 | - ); |
|
| 785 | - } |
|
| 786 | - $this->_cache_on = true; |
|
| 787 | - return $class_obj; |
|
| 788 | - } |
|
| 789 | - |
|
| 790 | - |
|
| 791 | - /** |
|
| 792 | - * Recursively checks that a class exists and potentially attempts to load classes with non-FQCNs |
|
| 793 | - * |
|
| 794 | - * @param string|object $class_name |
|
| 795 | - * @param array $arguments |
|
| 796 | - * @param int $attempt |
|
| 797 | - * @return mixed |
|
| 798 | - */ |
|
| 799 | - private function loadOrVerifyClassExists($class_name, array $arguments, $attempt = 1) |
|
| 800 | - { |
|
| 801 | - if (is_object($class_name) || class_exists($class_name)) { |
|
| 802 | - return $class_name; |
|
| 803 | - } |
|
| 804 | - switch ($attempt) { |
|
| 805 | - case 1: |
|
| 806 | - // if it's a FQCN then maybe the class is registered with a preceding \ |
|
| 807 | - $class_name = strpos($class_name, '\\') !== false |
|
| 808 | - ? '\\' . ltrim($class_name, '\\') |
|
| 809 | - : $class_name; |
|
| 810 | - break; |
|
| 811 | - case 2: |
|
| 812 | - // |
|
| 813 | - $loader = $this->_dependency_map->class_loader($class_name); |
|
| 814 | - if ($loader && method_exists($this, $loader)) { |
|
| 815 | - return $this->{$loader}($class_name, $arguments); |
|
| 816 | - } |
|
| 817 | - break; |
|
| 818 | - case 3: |
|
| 819 | - default: |
|
| 820 | - return null; |
|
| 821 | - } |
|
| 822 | - $attempt++; |
|
| 823 | - return $this->loadOrVerifyClassExists($class_name, $arguments, $attempt); |
|
| 824 | - } |
|
| 825 | - |
|
| 826 | - |
|
| 827 | - /** |
|
| 828 | - * instantiates, caches, and injects dependencies for classes |
|
| 829 | - * |
|
| 830 | - * @param array $file_paths an array of paths to folders to look in |
|
| 831 | - * @param string $class_prefix EE or EEM or... ??? |
|
| 832 | - * @param bool|string $class_name $class name |
|
| 833 | - * @param string $type file type - core? class? helper? model? |
|
| 834 | - * @param mixed $arguments an argument or array of arguments to pass to the class upon instantiation |
|
| 835 | - * @param bool $from_db some classes are instantiated from the db |
|
| 836 | - * and thus call a different method to instantiate |
|
| 837 | - * @param bool $cache whether to cache the instantiated object for reuse |
|
| 838 | - * @param bool $load_only if true, will only load the file, but will NOT instantiate an object |
|
| 839 | - * @return bool|null|object null = failure to load or instantiate class object. |
|
| 840 | - * object = class loaded and instantiated successfully. |
|
| 841 | - * bool = fail or success when $load_only is true |
|
| 842 | - * @throws EE_Error |
|
| 843 | - * @throws ReflectionException |
|
| 844 | - * @throws InvalidInterfaceException |
|
| 845 | - * @throws InvalidDataTypeException |
|
| 846 | - * @throws InvalidArgumentException |
|
| 847 | - */ |
|
| 848 | - protected function _load( |
|
| 849 | - $file_paths = array(), |
|
| 850 | - $class_prefix = 'EE_', |
|
| 851 | - $class_name = false, |
|
| 852 | - $type = 'class', |
|
| 853 | - $arguments = array(), |
|
| 854 | - $from_db = false, |
|
| 855 | - $cache = true, |
|
| 856 | - $load_only = false |
|
| 857 | - ) { |
|
| 858 | - $class_name = ltrim($class_name, '\\'); |
|
| 859 | - // strip php file extension |
|
| 860 | - $class_name = str_replace('.php', '', trim($class_name)); |
|
| 861 | - // does the class have a prefix ? |
|
| 862 | - if (! empty($class_prefix) && $class_prefix !== 'addon') { |
|
| 863 | - // make sure $class_prefix is uppercase |
|
| 864 | - $class_prefix = strtoupper(trim($class_prefix)); |
|
| 865 | - // add class prefix ONCE!!! |
|
| 866 | - $class_name = $class_prefix . str_replace($class_prefix, '', $class_name); |
|
| 867 | - } |
|
| 868 | - $class_name = $this->class_cache->getFqnForAlias($class_name); |
|
| 869 | - $class_exists = class_exists($class_name, false); |
|
| 870 | - // if we're only loading the class and it already exists, then let's just return true immediately |
|
| 871 | - if ($load_only && $class_exists) { |
|
| 872 | - return true; |
|
| 873 | - } |
|
| 874 | - $arguments = is_array($arguments) ? $arguments : array($arguments); |
|
| 875 | - // $this->_cache_on is toggled during the recursive loading that can occur with dependency injection |
|
| 876 | - // $cache is controlled by individual calls to separate Registry loader methods like load_class() |
|
| 877 | - // $load_only is also controlled by individual calls to separate Registry loader methods like load_file() |
|
| 878 | - if ($this->_cache_on && $cache && ! $load_only) { |
|
| 879 | - // return object if it's already cached |
|
| 880 | - $cached_class = $this->_get_cached_class($class_name, $class_prefix, $arguments); |
|
| 881 | - if ($cached_class !== null) { |
|
| 882 | - return $cached_class; |
|
| 883 | - } |
|
| 884 | - } |
|
| 885 | - // if the class doesn't already exist.. then we need to try and find the file and load it |
|
| 886 | - if (! $class_exists) { |
|
| 887 | - // get full path to file |
|
| 888 | - $path = $this->_resolve_path($class_name, $type, $file_paths); |
|
| 889 | - // load the file |
|
| 890 | - $loaded = $this->_require_file($path, $class_name, $type, $file_paths); |
|
| 891 | - // if we are only loading a file but NOT instantiating an object |
|
| 892 | - // then return boolean for whether class was loaded or not |
|
| 893 | - if ($load_only) { |
|
| 894 | - return $loaded; |
|
| 895 | - } |
|
| 896 | - // if an object was expected but loading failed, then return nothing |
|
| 897 | - if (! $loaded) { |
|
| 898 | - return null; |
|
| 899 | - } |
|
| 900 | - } |
|
| 901 | - // instantiate the requested object |
|
| 902 | - $class_obj = $this->_create_object($class_name, $arguments, $type, $from_db); |
|
| 903 | - if ($this->_cache_on && $cache) { |
|
| 904 | - // save it for later... kinda like gum { : $ |
|
| 905 | - $this->_set_cached_class( |
|
| 906 | - $class_obj, |
|
| 907 | - $class_name, |
|
| 908 | - $class_prefix, |
|
| 909 | - $from_db, |
|
| 910 | - $arguments |
|
| 911 | - ); |
|
| 912 | - } |
|
| 913 | - $this->_cache_on = true; |
|
| 914 | - return $class_obj; |
|
| 915 | - } |
|
| 916 | - |
|
| 917 | - |
|
| 918 | - /** |
|
| 919 | - * @param string $class_name |
|
| 920 | - * @param string $default have to specify something, but not anything that will conflict |
|
| 921 | - * @return mixed|string |
|
| 922 | - */ |
|
| 923 | - protected function get_class_abbreviation($class_name, $default = 'FANCY_BATMAN_PANTS') |
|
| 924 | - { |
|
| 925 | - return isset($this->_class_abbreviations[ $class_name ]) |
|
| 926 | - ? $this->_class_abbreviations[ $class_name ] |
|
| 927 | - : $default; |
|
| 928 | - } |
|
| 929 | - |
|
| 930 | - |
|
| 931 | - /** |
|
| 932 | - * attempts to find a cached version of the requested class |
|
| 933 | - * by looking in the following places: |
|
| 934 | - * $this->{$class_abbreviation} ie: $this->CART |
|
| 935 | - * $this->{$class_name} ie: $this->Some_Class |
|
| 936 | - * $this->LIB->{$class_name} ie: $this->LIB->Some_Class |
|
| 937 | - * $this->addon->{$class_name} ie: $this->addon->Some_Addon_Class |
|
| 938 | - * |
|
| 939 | - * @param string $class_name |
|
| 940 | - * @param string $class_prefix |
|
| 941 | - * @param array $arguments |
|
| 942 | - * @return mixed |
|
| 943 | - */ |
|
| 944 | - protected function _get_cached_class( |
|
| 945 | - $class_name, |
|
| 946 | - $class_prefix = '', |
|
| 947 | - $arguments = array() |
|
| 948 | - ) { |
|
| 949 | - if ($class_name === 'EE_Registry') { |
|
| 950 | - return $this; |
|
| 951 | - } |
|
| 952 | - $class_abbreviation = $this->get_class_abbreviation($class_name); |
|
| 953 | - // check if class has already been loaded, and return it if it has been |
|
| 954 | - if (isset($this->{$class_abbreviation})) { |
|
| 955 | - return $this->{$class_abbreviation}; |
|
| 956 | - } |
|
| 957 | - $class_name = str_replace('\\', '_', $class_name); |
|
| 958 | - if (isset($this->{$class_name})) { |
|
| 959 | - return $this->{$class_name}; |
|
| 960 | - } |
|
| 961 | - if ($class_prefix === 'addon' && isset($this->addons->{$class_name})) { |
|
| 962 | - return $this->addons->{$class_name}; |
|
| 963 | - } |
|
| 964 | - $object_identifier = $this->object_identifier->getIdentifier($class_name, $arguments); |
|
| 965 | - if (isset($this->LIB->{$object_identifier})) { |
|
| 966 | - return $this->LIB->{$object_identifier}; |
|
| 967 | - } |
|
| 968 | - foreach ($this->LIB as $key => $object) { |
|
| 969 | - if (// request does not contain new arguments and therefore no args identifier |
|
| 970 | - ! $this->object_identifier->hasArguments($object_identifier) |
|
| 971 | - // but previously cached class with args was found |
|
| 972 | - && $this->object_identifier->fqcnMatchesObjectIdentifier($class_name, $key) |
|
| 973 | - ) { |
|
| 974 | - return $object; |
|
| 975 | - } |
|
| 976 | - } |
|
| 977 | - return null; |
|
| 978 | - } |
|
| 979 | - |
|
| 980 | - |
|
| 981 | - /** |
|
| 982 | - * removes a cached version of the requested class |
|
| 983 | - * |
|
| 984 | - * @param string $class_name |
|
| 985 | - * @param boolean $addon |
|
| 986 | - * @param array $arguments |
|
| 987 | - * @return boolean |
|
| 988 | - */ |
|
| 989 | - public function clear_cached_class( |
|
| 990 | - $class_name, |
|
| 991 | - $addon = false, |
|
| 992 | - $arguments = array() |
|
| 993 | - ) { |
|
| 994 | - $class_abbreviation = $this->get_class_abbreviation($class_name); |
|
| 995 | - // check if class has already been loaded, and return it if it has been |
|
| 996 | - if (isset($this->{$class_abbreviation})) { |
|
| 997 | - $this->{$class_abbreviation} = null; |
|
| 998 | - return true; |
|
| 999 | - } |
|
| 1000 | - $class_name = str_replace('\\', '_', $class_name); |
|
| 1001 | - if (isset($this->{$class_name})) { |
|
| 1002 | - $this->{$class_name} = null; |
|
| 1003 | - return true; |
|
| 1004 | - } |
|
| 1005 | - if ($addon && isset($this->addons->{$class_name})) { |
|
| 1006 | - unset($this->addons->{$class_name}); |
|
| 1007 | - return true; |
|
| 1008 | - } |
|
| 1009 | - $class_name = $this->object_identifier->getIdentifier($class_name, $arguments); |
|
| 1010 | - if (isset($this->LIB->{$class_name})) { |
|
| 1011 | - unset($this->LIB->{$class_name}); |
|
| 1012 | - return true; |
|
| 1013 | - } |
|
| 1014 | - return false; |
|
| 1015 | - } |
|
| 1016 | - |
|
| 1017 | - |
|
| 1018 | - /** |
|
| 1019 | - * _set_cached_class |
|
| 1020 | - * attempts to cache the instantiated class locally |
|
| 1021 | - * in one of the following places, in the following order: |
|
| 1022 | - * $this->{class_abbreviation} ie: $this->CART |
|
| 1023 | - * $this->{$class_name} ie: $this->Some_Class |
|
| 1024 | - * $this->addon->{$$class_name} ie: $this->addon->Some_Addon_Class |
|
| 1025 | - * $this->LIB->{$class_name} ie: $this->LIB->Some_Class |
|
| 1026 | - * |
|
| 1027 | - * @param object $class_obj |
|
| 1028 | - * @param string $class_name |
|
| 1029 | - * @param string $class_prefix |
|
| 1030 | - * @param bool $from_db |
|
| 1031 | - * @param array $arguments |
|
| 1032 | - * @return void |
|
| 1033 | - */ |
|
| 1034 | - protected function _set_cached_class( |
|
| 1035 | - $class_obj, |
|
| 1036 | - $class_name, |
|
| 1037 | - $class_prefix = '', |
|
| 1038 | - $from_db = false, |
|
| 1039 | - $arguments = array() |
|
| 1040 | - ) { |
|
| 1041 | - if ($class_name === 'EE_Registry' || empty($class_obj)) { |
|
| 1042 | - return; |
|
| 1043 | - } |
|
| 1044 | - // return newly instantiated class |
|
| 1045 | - $class_abbreviation = $this->get_class_abbreviation($class_name, ''); |
|
| 1046 | - if ($class_abbreviation) { |
|
| 1047 | - $this->{$class_abbreviation} = $class_obj; |
|
| 1048 | - return; |
|
| 1049 | - } |
|
| 1050 | - $class_name = str_replace('\\', '_', $class_name); |
|
| 1051 | - if (property_exists($this, $class_name)) { |
|
| 1052 | - $this->{$class_name} = $class_obj; |
|
| 1053 | - return; |
|
| 1054 | - } |
|
| 1055 | - if ($class_prefix === 'addon') { |
|
| 1056 | - $this->addons->{$class_name} = $class_obj; |
|
| 1057 | - return; |
|
| 1058 | - } |
|
| 1059 | - if (! $from_db) { |
|
| 1060 | - $class_name = $this->object_identifier->getIdentifier($class_name, $arguments); |
|
| 1061 | - $this->LIB->{$class_name} = $class_obj; |
|
| 1062 | - } |
|
| 1063 | - } |
|
| 1064 | - |
|
| 1065 | - |
|
| 1066 | - /** |
|
| 1067 | - * attempts to find a full valid filepath for the requested class. |
|
| 1068 | - * loops thru each of the base paths in the $file_paths array and appends : "{classname} . {file type} . php" |
|
| 1069 | - * then returns that path if the target file has been found and is readable |
|
| 1070 | - * |
|
| 1071 | - * @param string $class_name |
|
| 1072 | - * @param string $type |
|
| 1073 | - * @param array $file_paths |
|
| 1074 | - * @return string | bool |
|
| 1075 | - */ |
|
| 1076 | - protected function _resolve_path($class_name, $type = '', $file_paths = array()) |
|
| 1077 | - { |
|
| 1078 | - // make sure $file_paths is an array |
|
| 1079 | - $file_paths = is_array($file_paths) |
|
| 1080 | - ? $file_paths |
|
| 1081 | - : array($file_paths); |
|
| 1082 | - // cycle thru paths |
|
| 1083 | - foreach ($file_paths as $key => $file_path) { |
|
| 1084 | - // convert all separators to proper DS, if no filepath, then use EE_CLASSES |
|
| 1085 | - $file_path = $file_path |
|
| 1086 | - ? str_replace(array('/', '\\'), DS, $file_path) |
|
| 1087 | - : EE_CLASSES; |
|
| 1088 | - // prep file type |
|
| 1089 | - $type = ! empty($type) |
|
| 1090 | - ? trim($type, '.') . '.' |
|
| 1091 | - : ''; |
|
| 1092 | - // build full file path |
|
| 1093 | - $file_paths[ $key ] = rtrim($file_path, DS) . DS . $class_name . '.' . $type . 'php'; |
|
| 1094 | - // does the file exist and can be read ? |
|
| 1095 | - if (is_readable($file_paths[ $key ])) { |
|
| 1096 | - return $file_paths[ $key ]; |
|
| 1097 | - } |
|
| 1098 | - } |
|
| 1099 | - return false; |
|
| 1100 | - } |
|
| 1101 | - |
|
| 1102 | - |
|
| 1103 | - /** |
|
| 1104 | - * basically just performs a require_once() |
|
| 1105 | - * but with some error handling |
|
| 1106 | - * |
|
| 1107 | - * @param string $path |
|
| 1108 | - * @param string $class_name |
|
| 1109 | - * @param string $type |
|
| 1110 | - * @param array $file_paths |
|
| 1111 | - * @return bool |
|
| 1112 | - * @throws EE_Error |
|
| 1113 | - * @throws ReflectionException |
|
| 1114 | - */ |
|
| 1115 | - protected function _require_file($path, $class_name, $type = '', $file_paths = array()) |
|
| 1116 | - { |
|
| 1117 | - $this->resolve_legacy_class_parent($class_name); |
|
| 1118 | - // don't give up! you gotta... |
|
| 1119 | - try { |
|
| 1120 | - // does the file exist and can it be read ? |
|
| 1121 | - if (! $path) { |
|
| 1122 | - // just in case the file has already been autoloaded, |
|
| 1123 | - // but discrepancies in the naming schema are preventing it from |
|
| 1124 | - // being loaded via one of the EE_Registry::load_*() methods, |
|
| 1125 | - // then let's try one last hail mary before throwing an exception |
|
| 1126 | - // and call class_exists() again, but with autoloading turned ON |
|
| 1127 | - if (class_exists($class_name)) { |
|
| 1128 | - return true; |
|
| 1129 | - } |
|
| 1130 | - // so sorry, can't find the file |
|
| 1131 | - throw new EE_Error( |
|
| 1132 | - sprintf( |
|
| 1133 | - esc_html__( |
|
| 1134 | - 'The %1$s file %2$s could not be located or is not readable due to file permissions. Please ensure that the following filepath(s) are correct: %3$s', |
|
| 1135 | - 'event_espresso' |
|
| 1136 | - ), |
|
| 1137 | - trim($type, '.'), |
|
| 1138 | - $class_name, |
|
| 1139 | - '<br />' . implode(',<br />', $file_paths) |
|
| 1140 | - ) |
|
| 1141 | - ); |
|
| 1142 | - } |
|
| 1143 | - // get the file |
|
| 1144 | - require_once($path); |
|
| 1145 | - // if the class isn't already declared somewhere |
|
| 1146 | - if (class_exists($class_name, false) === false) { |
|
| 1147 | - // so sorry, not a class |
|
| 1148 | - throw new EE_Error( |
|
| 1149 | - sprintf( |
|
| 1150 | - esc_html__( |
|
| 1151 | - 'The %s file %s does not appear to contain the %s Class.', |
|
| 1152 | - 'event_espresso' |
|
| 1153 | - ), |
|
| 1154 | - $type, |
|
| 1155 | - $path, |
|
| 1156 | - $class_name |
|
| 1157 | - ) |
|
| 1158 | - ); |
|
| 1159 | - } |
|
| 1160 | - } catch (EE_Error $e) { |
|
| 1161 | - $e->get_error(); |
|
| 1162 | - return false; |
|
| 1163 | - } |
|
| 1164 | - return true; |
|
| 1165 | - } |
|
| 1166 | - |
|
| 1167 | - |
|
| 1168 | - /** |
|
| 1169 | - * Some of our legacy classes that extended a parent class would simply use a require() statement |
|
| 1170 | - * before their class declaration in order to ensure that the parent class was loaded. |
|
| 1171 | - * This is not ideal, but it's nearly impossible to determine the parent class of a non-namespaced class, |
|
| 1172 | - * without triggering a fatal error because the parent class has yet to be loaded and therefore doesn't exist. |
|
| 1173 | - * |
|
| 1174 | - * @param string $class_name |
|
| 1175 | - */ |
|
| 1176 | - protected function resolve_legacy_class_parent($class_name = '') |
|
| 1177 | - { |
|
| 1178 | - try { |
|
| 1179 | - $legacy_parent_class_map = array( |
|
| 1180 | - 'EE_Payment_Processor' => 'core/business/EE_Processor_Base.class.php', |
|
| 1181 | - ); |
|
| 1182 | - if (isset($legacy_parent_class_map[ $class_name ])) { |
|
| 1183 | - require_once EE_PLUGIN_DIR_PATH . $legacy_parent_class_map[ $class_name ]; |
|
| 1184 | - } |
|
| 1185 | - } catch (Exception $exception) { |
|
| 1186 | - } |
|
| 1187 | - } |
|
| 1188 | - |
|
| 1189 | - |
|
| 1190 | - /** |
|
| 1191 | - * _create_object |
|
| 1192 | - * Attempts to instantiate the requested class via any of the |
|
| 1193 | - * commonly used instantiation methods employed throughout EE. |
|
| 1194 | - * The priority for instantiation is as follows: |
|
| 1195 | - * - abstract classes or any class flagged as "load only" (no instantiation occurs) |
|
| 1196 | - * - model objects via their 'new_instance_from_db' method |
|
| 1197 | - * - model objects via their 'new_instance' method |
|
| 1198 | - * - "singleton" classes" via their 'instance' method |
|
| 1199 | - * - standard instantiable classes via their __constructor |
|
| 1200 | - * Prior to instantiation, if the classname exists in the dependency_map, |
|
| 1201 | - * then the constructor for the requested class will be examined to determine |
|
| 1202 | - * if any dependencies exist, and if they can be injected. |
|
| 1203 | - * If so, then those classes will be added to the array of arguments passed to the constructor |
|
| 1204 | - * |
|
| 1205 | - * @param string $class_name |
|
| 1206 | - * @param array $arguments |
|
| 1207 | - * @param string $type |
|
| 1208 | - * @param bool $from_db |
|
| 1209 | - * @return null|object|bool |
|
| 1210 | - * @throws InvalidArgumentException |
|
| 1211 | - * @throws InvalidInterfaceException |
|
| 1212 | - * @throws EE_Error |
|
| 1213 | - * @throws ReflectionException |
|
| 1214 | - * @throws InvalidDataTypeException |
|
| 1215 | - */ |
|
| 1216 | - protected function _create_object($class_name, $arguments = array(), $type = '', $from_db = false) |
|
| 1217 | - { |
|
| 1218 | - // create reflection |
|
| 1219 | - $reflector = $this->mirror->getReflectionClass($class_name); |
|
| 1220 | - // make sure arguments are an array |
|
| 1221 | - $arguments = is_array($arguments) |
|
| 1222 | - ? $arguments |
|
| 1223 | - : array($arguments); |
|
| 1224 | - // and if arguments array is numerically and sequentially indexed, then we want it to remain as is, |
|
| 1225 | - // else wrap it in an additional array so that it doesn't get split into multiple parameters |
|
| 1226 | - $arguments = $this->_array_is_numerically_and_sequentially_indexed($arguments) |
|
| 1227 | - ? $arguments |
|
| 1228 | - : array($arguments); |
|
| 1229 | - // attempt to inject dependencies ? |
|
| 1230 | - if ($this->_dependency_map->has($class_name)) { |
|
| 1231 | - $arguments = $this->_resolve_dependencies($reflector, $class_name, $arguments); |
|
| 1232 | - } |
|
| 1233 | - // instantiate the class if possible |
|
| 1234 | - if ($reflector->isAbstract()) { |
|
| 1235 | - // nothing to instantiate, loading file was enough |
|
| 1236 | - // does not throw an exception so $instantiation_mode is unused |
|
| 1237 | - // $instantiation_mode = "1) no constructor abstract class"; |
|
| 1238 | - return true; |
|
| 1239 | - } |
|
| 1240 | - if (empty($arguments) |
|
| 1241 | - && $this->mirror->getConstructorFromReflection($reflector) === null |
|
| 1242 | - && $reflector->isInstantiable() |
|
| 1243 | - ) { |
|
| 1244 | - // no constructor = static methods only... nothing to instantiate, loading file was enough |
|
| 1245 | - // $instantiation_mode = "2) no constructor but instantiable"; |
|
| 1246 | - return $reflector->newInstance(); |
|
| 1247 | - } |
|
| 1248 | - if ($from_db && method_exists($class_name, 'new_instance_from_db')) { |
|
| 1249 | - // $instantiation_mode = "3) new_instance_from_db()"; |
|
| 1250 | - return call_user_func_array(array($class_name, 'new_instance_from_db'), $arguments); |
|
| 1251 | - } |
|
| 1252 | - if (method_exists($class_name, 'new_instance')) { |
|
| 1253 | - // $instantiation_mode = "4) new_instance()"; |
|
| 1254 | - return call_user_func_array(array($class_name, 'new_instance'), $arguments); |
|
| 1255 | - } |
|
| 1256 | - if (method_exists($class_name, 'instance')) { |
|
| 1257 | - // $instantiation_mode = "5) instance()"; |
|
| 1258 | - return call_user_func_array(array($class_name, 'instance'), $arguments); |
|
| 1259 | - } |
|
| 1260 | - if ($reflector->isInstantiable()) { |
|
| 1261 | - // $instantiation_mode = "6) constructor"; |
|
| 1262 | - return $reflector->newInstanceArgs($arguments); |
|
| 1263 | - } |
|
| 1264 | - // heh ? something's not right ! |
|
| 1265 | - throw new EE_Error( |
|
| 1266 | - sprintf( |
|
| 1267 | - __('The %s file %s could not be instantiated.', 'event_espresso'), |
|
| 1268 | - $type, |
|
| 1269 | - $class_name |
|
| 1270 | - ) |
|
| 1271 | - ); |
|
| 1272 | - } |
|
| 1273 | - |
|
| 1274 | - |
|
| 1275 | - /** |
|
| 1276 | - * @see http://stackoverflow.com/questions/173400/how-to-check-if-php-array-is-associative-or-sequential |
|
| 1277 | - * @param array $array |
|
| 1278 | - * @return bool |
|
| 1279 | - */ |
|
| 1280 | - protected function _array_is_numerically_and_sequentially_indexed(array $array) |
|
| 1281 | - { |
|
| 1282 | - return ! empty($array) |
|
| 1283 | - ? array_keys($array) === range(0, count($array) - 1) |
|
| 1284 | - : true; |
|
| 1285 | - } |
|
| 1286 | - |
|
| 1287 | - |
|
| 1288 | - /** |
|
| 1289 | - * _resolve_dependencies |
|
| 1290 | - * examines the constructor for the requested class to determine |
|
| 1291 | - * if any dependencies exist, and if they can be injected. |
|
| 1292 | - * If so, then those classes will be added to the array of arguments passed to the constructor |
|
| 1293 | - * PLZ NOTE: this is achieved by type hinting the constructor params |
|
| 1294 | - * For example: |
|
| 1295 | - * if attempting to load a class "Foo" with the following constructor: |
|
| 1296 | - * __construct( Bar $bar_class, Fighter $grohl_class ) |
|
| 1297 | - * then $bar_class and $grohl_class will be added to the $arguments array, |
|
| 1298 | - * but only IF they are NOT already present in the incoming arguments array, |
|
| 1299 | - * and the correct classes can be loaded |
|
| 1300 | - * |
|
| 1301 | - * @param ReflectionClass $reflector |
|
| 1302 | - * @param string $class_name |
|
| 1303 | - * @param array $arguments |
|
| 1304 | - * @return array |
|
| 1305 | - * @throws InvalidArgumentException |
|
| 1306 | - * @throws InvalidDataTypeException |
|
| 1307 | - * @throws InvalidInterfaceException |
|
| 1308 | - * @throws ReflectionException |
|
| 1309 | - */ |
|
| 1310 | - protected function _resolve_dependencies(ReflectionClass $reflector, $class_name, array $arguments = array()) |
|
| 1311 | - { |
|
| 1312 | - // let's examine the constructor |
|
| 1313 | - $constructor = $this->mirror->getConstructorFromReflection($reflector); |
|
| 1314 | - // whu? huh? nothing? |
|
| 1315 | - if (! $constructor) { |
|
| 1316 | - return $arguments; |
|
| 1317 | - } |
|
| 1318 | - // get constructor parameters |
|
| 1319 | - $params = $this->mirror->getParametersFromReflection($reflector); |
|
| 1320 | - // and the keys for the incoming arguments array so that we can compare existing arguments with what is expected |
|
| 1321 | - $argument_keys = array_keys($arguments); |
|
| 1322 | - // now loop thru all of the constructors expected parameters |
|
| 1323 | - foreach ($params as $index => $param) { |
|
| 1324 | - try { |
|
| 1325 | - // is this a dependency for a specific class ? |
|
| 1326 | - $param_class = $this->mirror->getParameterClassName($param, $class_name, $index); |
|
| 1327 | - } catch (ReflectionException $exception) { |
|
| 1328 | - // uh-oh... most likely a legacy class that has not been autoloaded |
|
| 1329 | - // let's try to derive the classname from what we have now |
|
| 1330 | - // and hope that the property var name is close to the class name |
|
| 1331 | - $param_class = $param->getName(); |
|
| 1332 | - $param_class = str_replace('_', ' ',$param_class); |
|
| 1333 | - $param_class = ucwords($param_class); |
|
| 1334 | - $param_class = str_replace(' ', '_', $param_class); |
|
| 1335 | - } |
|
| 1336 | - // BUT WAIT !!! This class may be an alias for something else (or getting replaced at runtime) |
|
| 1337 | - $param_class = $this->class_cache->isAlias($param_class, $class_name) |
|
| 1338 | - ? $this->class_cache->getFqnForAlias($param_class, $class_name) |
|
| 1339 | - : $param_class; |
|
| 1340 | - if (// param is not even a class |
|
| 1341 | - $param_class === null |
|
| 1342 | - // and something already exists in the incoming arguments for this param |
|
| 1343 | - && array_key_exists($index, $argument_keys) |
|
| 1344 | - && array_key_exists($argument_keys[ $index ], $arguments) |
|
| 1345 | - ) { |
|
| 1346 | - // so let's skip this argument and move on to the next |
|
| 1347 | - continue; |
|
| 1348 | - } |
|
| 1349 | - if (// parameter is type hinted as a class, exists as an incoming argument, AND it's the correct class |
|
| 1350 | - $param_class !== null |
|
| 1351 | - && isset($argument_keys[ $index ], $arguments[ $argument_keys[ $index ] ]) |
|
| 1352 | - && $arguments[ $argument_keys[ $index ] ] instanceof $param_class |
|
| 1353 | - ) { |
|
| 1354 | - // skip this argument and move on to the next |
|
| 1355 | - continue; |
|
| 1356 | - } |
|
| 1357 | - if (// parameter is type hinted as a class, and should be injected |
|
| 1358 | - $param_class !== null |
|
| 1359 | - && $this->_dependency_map->has_dependency_for_class($class_name, $param_class) |
|
| 1360 | - ) { |
|
| 1361 | - $arguments = $this->_resolve_dependency( |
|
| 1362 | - $class_name, |
|
| 1363 | - $param_class, |
|
| 1364 | - $arguments, |
|
| 1365 | - $index |
|
| 1366 | - ); |
|
| 1367 | - } |
|
| 1368 | - if (empty($arguments[ $index ])) { |
|
| 1369 | - $arguments[ $index ] = $this->mirror->getParameterDefaultValue( |
|
| 1370 | - $param, |
|
| 1371 | - $class_name, |
|
| 1372 | - $index |
|
| 1373 | - ); |
|
| 1374 | - } |
|
| 1375 | - } |
|
| 1376 | - return $arguments; |
|
| 1377 | - } |
|
| 1378 | - |
|
| 1379 | - |
|
| 1380 | - /** |
|
| 1381 | - * @param string $class_name |
|
| 1382 | - * @param string $param_class |
|
| 1383 | - * @param array $arguments |
|
| 1384 | - * @param mixed $index |
|
| 1385 | - * @return array |
|
| 1386 | - * @throws InvalidArgumentException |
|
| 1387 | - * @throws InvalidInterfaceException |
|
| 1388 | - * @throws InvalidDataTypeException |
|
| 1389 | - */ |
|
| 1390 | - protected function _resolve_dependency($class_name, $param_class, $arguments, $index) |
|
| 1391 | - { |
|
| 1392 | - $dependency = null; |
|
| 1393 | - // should dependency be loaded from cache ? |
|
| 1394 | - $cache_on = $this->_dependency_map->loading_strategy_for_class_dependency( |
|
| 1395 | - $class_name, |
|
| 1396 | - $param_class |
|
| 1397 | - ); |
|
| 1398 | - $cache_on = $cache_on !== EE_Dependency_Map::load_new_object; |
|
| 1399 | - // we might have a dependency... |
|
| 1400 | - // let's MAYBE try and find it in our cache if that's what's been requested |
|
| 1401 | - $cached_class = $cache_on |
|
| 1402 | - ? $this->_get_cached_class($param_class) |
|
| 1403 | - : null; |
|
| 1404 | - // and grab it if it exists |
|
| 1405 | - if ($cached_class instanceof $param_class) { |
|
| 1406 | - $dependency = $cached_class; |
|
| 1407 | - } elseif ($param_class !== $class_name) { |
|
| 1408 | - // obtain the loader method from the dependency map |
|
| 1409 | - $loader = $this->_dependency_map->class_loader($param_class); |
|
| 1410 | - // is loader a custom closure ? |
|
| 1411 | - if ($loader instanceof Closure) { |
|
| 1412 | - $dependency = $loader($arguments); |
|
| 1413 | - } else { |
|
| 1414 | - // set the cache on property for the recursive loading call |
|
| 1415 | - $this->_cache_on = $cache_on; |
|
| 1416 | - // if not, then let's try and load it via the registry |
|
| 1417 | - if ($loader && method_exists($this, $loader)) { |
|
| 1418 | - $dependency = $this->{$loader}($param_class); |
|
| 1419 | - } else { |
|
| 1420 | - $dependency = LoaderFactory::getLoader()->load( |
|
| 1421 | - $param_class, |
|
| 1422 | - array(), |
|
| 1423 | - $cache_on |
|
| 1424 | - ); |
|
| 1425 | - } |
|
| 1426 | - } |
|
| 1427 | - } |
|
| 1428 | - // did we successfully find the correct dependency ? |
|
| 1429 | - if ($dependency instanceof $param_class) { |
|
| 1430 | - // then let's inject it into the incoming array of arguments at the correct location |
|
| 1431 | - $arguments[ $index ] = $dependency; |
|
| 1432 | - } |
|
| 1433 | - return $arguments; |
|
| 1434 | - } |
|
| 1435 | - |
|
| 1436 | - |
|
| 1437 | - /** |
|
| 1438 | - * call any loader that's been registered in the EE_Dependency_Map::$_class_loaders array |
|
| 1439 | - * |
|
| 1440 | - * @param string $classname PLEASE NOTE: the class name needs to match what's registered |
|
| 1441 | - * in the EE_Dependency_Map::$_class_loaders array, |
|
| 1442 | - * including the class prefix, ie: "EE_", "EEM_", "EEH_", etc |
|
| 1443 | - * @param array $arguments |
|
| 1444 | - * @return object |
|
| 1445 | - */ |
|
| 1446 | - public static function factory($classname, $arguments = array()) |
|
| 1447 | - { |
|
| 1448 | - $loader = self::instance()->_dependency_map->class_loader($classname); |
|
| 1449 | - if ($loader instanceof Closure) { |
|
| 1450 | - return $loader($arguments); |
|
| 1451 | - } |
|
| 1452 | - if (method_exists(self::instance(), $loader)) { |
|
| 1453 | - return self::instance()->{$loader}($classname, $arguments); |
|
| 1454 | - } |
|
| 1455 | - return null; |
|
| 1456 | - } |
|
| 1457 | - |
|
| 1458 | - |
|
| 1459 | - /** |
|
| 1460 | - * Gets the addon by its class name |
|
| 1461 | - * |
|
| 1462 | - * @param string $class_name |
|
| 1463 | - * @return EE_Addon |
|
| 1464 | - */ |
|
| 1465 | - public function getAddon($class_name) |
|
| 1466 | - { |
|
| 1467 | - $class_name = str_replace('\\', '_', $class_name); |
|
| 1468 | - if (isset($this->addons->{$class_name})) { |
|
| 1469 | - return $this->addons->{$class_name}; |
|
| 1470 | - } else { |
|
| 1471 | - return null; |
|
| 1472 | - } |
|
| 1473 | - } |
|
| 1474 | - |
|
| 1475 | - |
|
| 1476 | - /** |
|
| 1477 | - * removes the addon from the internal cache |
|
| 1478 | - * |
|
| 1479 | - * @param string $class_name |
|
| 1480 | - * @return void |
|
| 1481 | - */ |
|
| 1482 | - public function removeAddon($class_name) |
|
| 1483 | - { |
|
| 1484 | - $class_name = str_replace('\\', '_', $class_name); |
|
| 1485 | - unset($this->addons->{$class_name}); |
|
| 1486 | - } |
|
| 1487 | - |
|
| 1488 | - |
|
| 1489 | - /** |
|
| 1490 | - * Gets the addon by its name/slug (not classname. For that, just |
|
| 1491 | - * use the get_addon() method above |
|
| 1492 | - * |
|
| 1493 | - * @param string $name |
|
| 1494 | - * @return EE_Addon |
|
| 1495 | - */ |
|
| 1496 | - public function get_addon_by_name($name) |
|
| 1497 | - { |
|
| 1498 | - foreach ($this->addons as $addon) { |
|
| 1499 | - if ($addon->name() === $name) { |
|
| 1500 | - return $addon; |
|
| 1501 | - } |
|
| 1502 | - } |
|
| 1503 | - return null; |
|
| 1504 | - } |
|
| 1505 | - |
|
| 1506 | - |
|
| 1507 | - /** |
|
| 1508 | - * Gets an array of all the registered addons, where the keys are their names. |
|
| 1509 | - * (ie, what each returns for their name() function) |
|
| 1510 | - * They're already available on EE_Registry::instance()->addons as properties, |
|
| 1511 | - * where each property's name is the addon's classname, |
|
| 1512 | - * So if you just want to get the addon by classname, |
|
| 1513 | - * OR use the get_addon() method above. |
|
| 1514 | - * PLEASE NOTE: |
|
| 1515 | - * addons with Fully Qualified Class Names |
|
| 1516 | - * have had the namespace separators converted to underscores, |
|
| 1517 | - * so a classname like Fully\Qualified\ClassName |
|
| 1518 | - * would have been converted to Fully_Qualified_ClassName |
|
| 1519 | - * |
|
| 1520 | - * @return EE_Addon[] where the KEYS are the addon's name() |
|
| 1521 | - */ |
|
| 1522 | - public function get_addons_by_name() |
|
| 1523 | - { |
|
| 1524 | - $addons = array(); |
|
| 1525 | - foreach ($this->addons as $addon) { |
|
| 1526 | - $addons[ $addon->name() ] = $addon; |
|
| 1527 | - } |
|
| 1528 | - return $addons; |
|
| 1529 | - } |
|
| 1530 | - |
|
| 1531 | - |
|
| 1532 | - /** |
|
| 1533 | - * Resets the specified model's instance AND makes sure EE_Registry doesn't keep |
|
| 1534 | - * a stale copy of it around |
|
| 1535 | - * |
|
| 1536 | - * @param string $model_name |
|
| 1537 | - * @return \EEM_Base |
|
| 1538 | - * @throws \EE_Error |
|
| 1539 | - */ |
|
| 1540 | - public function reset_model($model_name) |
|
| 1541 | - { |
|
| 1542 | - $model_class_name = strpos($model_name, 'EEM_') !== 0 |
|
| 1543 | - ? "EEM_{$model_name}" |
|
| 1544 | - : $model_name; |
|
| 1545 | - if (! isset($this->LIB->{$model_class_name}) || ! $this->LIB->{$model_class_name} instanceof EEM_Base) { |
|
| 1546 | - return null; |
|
| 1547 | - } |
|
| 1548 | - // get that model reset it and make sure we nuke the old reference to it |
|
| 1549 | - if ($this->LIB->{$model_class_name} instanceof $model_class_name |
|
| 1550 | - && is_callable( |
|
| 1551 | - array($model_class_name, 'reset') |
|
| 1552 | - )) { |
|
| 1553 | - $this->LIB->{$model_class_name} = $this->LIB->{$model_class_name}->reset(); |
|
| 1554 | - } else { |
|
| 1555 | - throw new EE_Error( |
|
| 1556 | - sprintf( |
|
| 1557 | - esc_html__('Model %s does not have a method "reset"', 'event_espresso'), |
|
| 1558 | - $model_name |
|
| 1559 | - ) |
|
| 1560 | - ); |
|
| 1561 | - } |
|
| 1562 | - return $this->LIB->{$model_class_name}; |
|
| 1563 | - } |
|
| 1564 | - |
|
| 1565 | - |
|
| 1566 | - /** |
|
| 1567 | - * Resets the registry. |
|
| 1568 | - * The criteria for what gets reset is based on what can be shared between sites on the same request when |
|
| 1569 | - * switch_to_blog is used in a multisite install. Here is a list of things that are NOT reset. |
|
| 1570 | - * - $_dependency_map |
|
| 1571 | - * - $_class_abbreviations |
|
| 1572 | - * - $NET_CFG (EE_Network_Config): The config is shared network wide so no need to reset. |
|
| 1573 | - * - $REQ: Still on the same request so no need to change. |
|
| 1574 | - * - $CAP: There is no site specific state in the EE_Capability class. |
|
| 1575 | - * - $SSN: Although ideally, the session should not be shared between site switches, we can't reset it because only |
|
| 1576 | - * one Session can be active in a single request. Resetting could resolve in "headers already sent" errors. |
|
| 1577 | - * - $addons: In multisite, the state of the addons is something controlled via hooks etc in a normal request. So |
|
| 1578 | - * for now, we won't reset the addons because it could break calls to an add-ons class/methods in the |
|
| 1579 | - * switch or on the restore. |
|
| 1580 | - * - $modules |
|
| 1581 | - * - $shortcodes |
|
| 1582 | - * - $widgets |
|
| 1583 | - * |
|
| 1584 | - * @param boolean $hard [deprecated] |
|
| 1585 | - * @param boolean $reinstantiate whether to create new instances of EE_Registry's singletons too, |
|
| 1586 | - * or just reset without re-instantiating (handy to set to FALSE if you're not |
|
| 1587 | - * sure if you CAN currently reinstantiate the singletons at the moment) |
|
| 1588 | - * @param bool $reset_models Defaults to true. When false, then the models are not reset. This is so |
|
| 1589 | - * client |
|
| 1590 | - * code instead can just change the model context to a different blog id if |
|
| 1591 | - * necessary |
|
| 1592 | - * @return EE_Registry |
|
| 1593 | - * @throws InvalidInterfaceException |
|
| 1594 | - * @throws InvalidDataTypeException |
|
| 1595 | - * @throws EE_Error |
|
| 1596 | - * @throws ReflectionException |
|
| 1597 | - * @throws InvalidArgumentException |
|
| 1598 | - */ |
|
| 1599 | - public static function reset($hard = false, $reinstantiate = true, $reset_models = true) |
|
| 1600 | - { |
|
| 1601 | - $instance = self::instance(); |
|
| 1602 | - $instance->_cache_on = true; |
|
| 1603 | - // reset some "special" classes |
|
| 1604 | - EEH_Activation::reset(); |
|
| 1605 | - $hard = apply_filters('FHEE__EE_Registry__reset__hard', $hard); |
|
| 1606 | - $instance->CFG = EE_Config::reset($hard, $reinstantiate); |
|
| 1607 | - $instance->CART = null; |
|
| 1608 | - $instance->MRM = null; |
|
| 1609 | - $instance->AssetsRegistry = LoaderFactory::getLoader()->getShared( |
|
| 1610 | - 'EventEspresso\core\services\assets\Registry' |
|
| 1611 | - ); |
|
| 1612 | - // messages reset |
|
| 1613 | - EED_Messages::reset(); |
|
| 1614 | - // handle of objects cached on LIB |
|
| 1615 | - foreach (array('LIB', 'modules') as $cache) { |
|
| 1616 | - foreach ($instance->{$cache} as $class_name => $class) { |
|
| 1617 | - if (self::_reset_and_unset_object($class, $reset_models)) { |
|
| 1618 | - unset($instance->{$cache}->{$class_name}); |
|
| 1619 | - } |
|
| 1620 | - } |
|
| 1621 | - } |
|
| 1622 | - return $instance; |
|
| 1623 | - } |
|
| 1624 | - |
|
| 1625 | - |
|
| 1626 | - /** |
|
| 1627 | - * if passed object implements ResettableInterface, then call it's reset() method |
|
| 1628 | - * if passed object implements InterminableInterface, then return false, |
|
| 1629 | - * to indicate that it should NOT be cleared from the Registry cache |
|
| 1630 | - * |
|
| 1631 | - * @param $object |
|
| 1632 | - * @param bool $reset_models |
|
| 1633 | - * @return bool returns true if cached object should be unset |
|
| 1634 | - */ |
|
| 1635 | - private static function _reset_and_unset_object($object, $reset_models) |
|
| 1636 | - { |
|
| 1637 | - if (! is_object($object)) { |
|
| 1638 | - // don't unset anything that's not an object |
|
| 1639 | - return false; |
|
| 1640 | - } |
|
| 1641 | - if ($object instanceof EED_Module) { |
|
| 1642 | - $object::reset(); |
|
| 1643 | - // don't unset modules |
|
| 1644 | - return false; |
|
| 1645 | - } |
|
| 1646 | - if ($object instanceof ResettableInterface) { |
|
| 1647 | - if ($object instanceof EEM_Base) { |
|
| 1648 | - if ($reset_models) { |
|
| 1649 | - $object->reset(); |
|
| 1650 | - return true; |
|
| 1651 | - } |
|
| 1652 | - return false; |
|
| 1653 | - } |
|
| 1654 | - $object->reset(); |
|
| 1655 | - return true; |
|
| 1656 | - } |
|
| 1657 | - if (! $object instanceof InterminableInterface) { |
|
| 1658 | - return true; |
|
| 1659 | - } |
|
| 1660 | - return false; |
|
| 1661 | - } |
|
| 1662 | - |
|
| 1663 | - |
|
| 1664 | - /** |
|
| 1665 | - * Gets all the custom post type models defined |
|
| 1666 | - * |
|
| 1667 | - * @return array keys are model "short names" (Eg "Event") and keys are classnames (eg "EEM_Event") |
|
| 1668 | - */ |
|
| 1669 | - public function cpt_models() |
|
| 1670 | - { |
|
| 1671 | - $cpt_models = array(); |
|
| 1672 | - foreach ($this->non_abstract_db_models as $short_name => $classname) { |
|
| 1673 | - if (is_subclass_of($classname, 'EEM_CPT_Base')) { |
|
| 1674 | - $cpt_models[ $short_name ] = $classname; |
|
| 1675 | - } |
|
| 1676 | - } |
|
| 1677 | - return $cpt_models; |
|
| 1678 | - } |
|
| 1679 | - |
|
| 1680 | - |
|
| 1681 | - /** |
|
| 1682 | - * @return \EE_Config |
|
| 1683 | - */ |
|
| 1684 | - public static function CFG() |
|
| 1685 | - { |
|
| 1686 | - return self::instance()->CFG; |
|
| 1687 | - } |
|
| 1688 | - |
|
| 1689 | - |
|
| 1690 | - /** |
|
| 1691 | - * @deprecated 4.9.62.p |
|
| 1692 | - * @param string $class_name |
|
| 1693 | - * @return ReflectionClass |
|
| 1694 | - * @throws ReflectionException |
|
| 1695 | - * @throws InvalidDataTypeException |
|
| 1696 | - */ |
|
| 1697 | - public function get_ReflectionClass($class_name) |
|
| 1698 | - { |
|
| 1699 | - return $this->mirror->getReflectionClass($class_name); |
|
| 1700 | - } |
|
| 26 | + /** |
|
| 27 | + * @var EE_Registry $_instance |
|
| 28 | + */ |
|
| 29 | + private static $_instance; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * @var EE_Dependency_Map $_dependency_map |
|
| 33 | + */ |
|
| 34 | + protected $_dependency_map; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * @var Mirror |
|
| 38 | + */ |
|
| 39 | + private $mirror; |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * @var ClassInterfaceCache $class_cache |
|
| 43 | + */ |
|
| 44 | + private $class_cache; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * @var array $_class_abbreviations |
|
| 48 | + */ |
|
| 49 | + protected $_class_abbreviations = array(); |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * @var CommandBusInterface $BUS |
|
| 53 | + */ |
|
| 54 | + public $BUS; |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * @var EE_Cart $CART |
|
| 58 | + */ |
|
| 59 | + public $CART; |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * @var EE_Config $CFG |
|
| 63 | + */ |
|
| 64 | + public $CFG; |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * @var EE_Network_Config $NET_CFG |
|
| 68 | + */ |
|
| 69 | + public $NET_CFG; |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * StdClass object for storing library classes in |
|
| 73 | + * |
|
| 74 | + * @var RegistryContainer $LIB |
|
| 75 | + */ |
|
| 76 | + public $LIB; |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * @var EE_Request_Handler $REQ |
|
| 80 | + */ |
|
| 81 | + public $REQ; |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * @var EE_Session $SSN |
|
| 85 | + */ |
|
| 86 | + public $SSN; |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * @since 4.5.0 |
|
| 90 | + * @var EE_Capabilities $CAP |
|
| 91 | + */ |
|
| 92 | + public $CAP; |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * @since 4.9.0 |
|
| 96 | + * @var EE_Message_Resource_Manager $MRM |
|
| 97 | + */ |
|
| 98 | + public $MRM; |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * @var Registry $AssetsRegistry |
|
| 102 | + */ |
|
| 103 | + public $AssetsRegistry; |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * StdClass object for holding addons which have registered themselves to work with EE core |
|
| 107 | + * |
|
| 108 | + * @var EE_Addon[] $addons |
|
| 109 | + */ |
|
| 110 | + public $addons; |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * keys are 'short names' (eg Event), values are class names (eg 'EEM_Event') |
|
| 114 | + * |
|
| 115 | + * @var EEM_Base[] $models |
|
| 116 | + */ |
|
| 117 | + public $models = array(); |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * @var EED_Module[] $modules |
|
| 121 | + */ |
|
| 122 | + public $modules; |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * @var EES_Shortcode[] $shortcodes |
|
| 126 | + */ |
|
| 127 | + public $shortcodes; |
|
| 128 | + |
|
| 129 | + /** |
|
| 130 | + * @var WP_Widget[] $widgets |
|
| 131 | + */ |
|
| 132 | + public $widgets; |
|
| 133 | + |
|
| 134 | + /** |
|
| 135 | + * this is an array of all implemented model names (i.e. not the parent abstract models, or models |
|
| 136 | + * which don't actually fetch items from the DB in the normal way (ie, are not children of EEM_Base)). |
|
| 137 | + * Keys are model "short names" (eg "Event") as used in model relations, and values are |
|
| 138 | + * classnames (eg "EEM_Event") |
|
| 139 | + * |
|
| 140 | + * @var array $non_abstract_db_models |
|
| 141 | + */ |
|
| 142 | + public $non_abstract_db_models = array(); |
|
| 143 | + |
|
| 144 | + /** |
|
| 145 | + * internationalization for JS strings |
|
| 146 | + * usage: EE_Registry::i18n_js_strings['string_key'] = esc_html__( 'string to translate.', 'event_espresso' ); |
|
| 147 | + * in js file: var translatedString = eei18n.string_key; |
|
| 148 | + * |
|
| 149 | + * @var array $i18n_js_strings |
|
| 150 | + */ |
|
| 151 | + public static $i18n_js_strings = array(); |
|
| 152 | + |
|
| 153 | + /** |
|
| 154 | + * $main_file - path to espresso.php |
|
| 155 | + * |
|
| 156 | + * @var array $main_file |
|
| 157 | + */ |
|
| 158 | + public $main_file; |
|
| 159 | + |
|
| 160 | + /** |
|
| 161 | + * array of ReflectionClass objects where the key is the class name |
|
| 162 | + * |
|
| 163 | + * @deprecated 4.9.62.p |
|
| 164 | + * @var ReflectionClass[] $_reflectors |
|
| 165 | + */ |
|
| 166 | + public $_reflectors; |
|
| 167 | + |
|
| 168 | + /** |
|
| 169 | + * boolean flag to indicate whether or not to load/save dependencies from/to the cache |
|
| 170 | + * |
|
| 171 | + * @var boolean $_cache_on |
|
| 172 | + */ |
|
| 173 | + protected $_cache_on = true; |
|
| 174 | + |
|
| 175 | + /** |
|
| 176 | + * @var ObjectIdentifier |
|
| 177 | + */ |
|
| 178 | + private $object_identifier; |
|
| 179 | + |
|
| 180 | + |
|
| 181 | + /** |
|
| 182 | + * @singleton method used to instantiate class object |
|
| 183 | + * @param EE_Dependency_Map|null $dependency_map |
|
| 184 | + * @param Mirror|null $mirror |
|
| 185 | + * @param ClassInterfaceCache|null $class_cache |
|
| 186 | + * @param ObjectIdentifier|null $object_identifier |
|
| 187 | + * @return EE_Registry instance |
|
| 188 | + */ |
|
| 189 | + public static function instance( |
|
| 190 | + EE_Dependency_Map $dependency_map = null, |
|
| 191 | + Mirror $mirror = null, |
|
| 192 | + ClassInterfaceCache $class_cache = null, |
|
| 193 | + ObjectIdentifier $object_identifier = null |
|
| 194 | + ) { |
|
| 195 | + // check if class object is instantiated |
|
| 196 | + if (! self::$_instance instanceof EE_Registry |
|
| 197 | + && $dependency_map instanceof EE_Dependency_Map |
|
| 198 | + && $mirror instanceof Mirror |
|
| 199 | + && $class_cache instanceof ClassInterfaceCache |
|
| 200 | + && $object_identifier instanceof ObjectIdentifier |
|
| 201 | + ) { |
|
| 202 | + self::$_instance = new self( |
|
| 203 | + $dependency_map, |
|
| 204 | + $mirror, |
|
| 205 | + $class_cache, |
|
| 206 | + $object_identifier |
|
| 207 | + ); |
|
| 208 | + } |
|
| 209 | + return self::$_instance; |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + |
|
| 213 | + /** |
|
| 214 | + * protected constructor to prevent direct creation |
|
| 215 | + * |
|
| 216 | + * @Constructor |
|
| 217 | + * @param EE_Dependency_Map $dependency_map |
|
| 218 | + * @param Mirror $mirror |
|
| 219 | + * @param ClassInterfaceCache $class_cache |
|
| 220 | + * @param ObjectIdentifier $object_identifier |
|
| 221 | + */ |
|
| 222 | + protected function __construct( |
|
| 223 | + EE_Dependency_Map $dependency_map, |
|
| 224 | + Mirror $mirror, |
|
| 225 | + ClassInterfaceCache $class_cache, |
|
| 226 | + ObjectIdentifier $object_identifier |
|
| 227 | + ) { |
|
| 228 | + $this->_dependency_map = $dependency_map; |
|
| 229 | + $this->mirror = $mirror; |
|
| 230 | + $this->class_cache = $class_cache; |
|
| 231 | + $this->object_identifier = $object_identifier; |
|
| 232 | + // $registry_container = new RegistryContainer(); |
|
| 233 | + $this->LIB = new RegistryContainer(); |
|
| 234 | + $this->addons = new RegistryContainer(); |
|
| 235 | + $this->modules = new RegistryContainer(); |
|
| 236 | + $this->shortcodes = new RegistryContainer(); |
|
| 237 | + $this->widgets = new RegistryContainer(); |
|
| 238 | + add_action('EE_Load_Espresso_Core__handle_request__initialize_core_loading', array($this, 'initialize')); |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + |
|
| 242 | + /** |
|
| 243 | + * initialize |
|
| 244 | + * |
|
| 245 | + * @throws OutOfBoundsException |
|
| 246 | + * @throws InvalidArgumentException |
|
| 247 | + * @throws InvalidInterfaceException |
|
| 248 | + * @throws InvalidDataTypeException |
|
| 249 | + * @throws EE_Error |
|
| 250 | + * @throws ReflectionException |
|
| 251 | + */ |
|
| 252 | + public function initialize() |
|
| 253 | + { |
|
| 254 | + $this->_class_abbreviations = apply_filters( |
|
| 255 | + 'FHEE__EE_Registry____construct___class_abbreviations', |
|
| 256 | + array( |
|
| 257 | + 'EE_Config' => 'CFG', |
|
| 258 | + 'EE_Session' => 'SSN', |
|
| 259 | + 'EE_Capabilities' => 'CAP', |
|
| 260 | + 'EE_Cart' => 'CART', |
|
| 261 | + 'EE_Network_Config' => 'NET_CFG', |
|
| 262 | + 'EE_Request_Handler' => 'REQ', |
|
| 263 | + 'EE_Message_Resource_Manager' => 'MRM', |
|
| 264 | + 'EventEspresso\core\services\commands\CommandBus' => 'BUS', |
|
| 265 | + 'EventEspresso\core\services\assets\Registry' => 'AssetsRegistry', |
|
| 266 | + ) |
|
| 267 | + ); |
|
| 268 | + $this->load_core('Base', array(), true); |
|
| 269 | + // add our request and response objects to the cache |
|
| 270 | + $request_loader = $this->_dependency_map->class_loader( |
|
| 271 | + 'EventEspresso\core\services\request\Request' |
|
| 272 | + ); |
|
| 273 | + $this->_set_cached_class( |
|
| 274 | + $request_loader(), |
|
| 275 | + 'EventEspresso\core\services\request\Request' |
|
| 276 | + ); |
|
| 277 | + $response_loader = $this->_dependency_map->class_loader( |
|
| 278 | + 'EventEspresso\core\services\request\Response' |
|
| 279 | + ); |
|
| 280 | + $this->_set_cached_class( |
|
| 281 | + $response_loader(), |
|
| 282 | + 'EventEspresso\core\services\request\Response' |
|
| 283 | + ); |
|
| 284 | + add_action('AHEE__EE_System__set_hooks_for_core', array($this, 'init')); |
|
| 285 | + } |
|
| 286 | + |
|
| 287 | + |
|
| 288 | + /** |
|
| 289 | + * @return void |
|
| 290 | + */ |
|
| 291 | + public function init() |
|
| 292 | + { |
|
| 293 | + // Get current page protocol |
|
| 294 | + $protocol = isset($_SERVER['HTTPS']) ? 'https://' : 'http://'; |
|
| 295 | + // Output admin-ajax.php URL with same protocol as current page |
|
| 296 | + self::$i18n_js_strings['ajax_url'] = admin_url('admin-ajax.php', $protocol); |
|
| 297 | + self::$i18n_js_strings['wp_debug'] = defined('WP_DEBUG') ? WP_DEBUG : false; |
|
| 298 | + } |
|
| 299 | + |
|
| 300 | + |
|
| 301 | + /** |
|
| 302 | + * localize_i18n_js_strings |
|
| 303 | + * |
|
| 304 | + * @return string |
|
| 305 | + */ |
|
| 306 | + public static function localize_i18n_js_strings() |
|
| 307 | + { |
|
| 308 | + $i18n_js_strings = (array) self::$i18n_js_strings; |
|
| 309 | + foreach ($i18n_js_strings as $key => $value) { |
|
| 310 | + if (is_scalar($value)) { |
|
| 311 | + $i18n_js_strings[ $key ] = html_entity_decode((string) $value, ENT_QUOTES, 'UTF-8'); |
|
| 312 | + } |
|
| 313 | + } |
|
| 314 | + return '/* <![CDATA[ */ var eei18n = ' . wp_json_encode($i18n_js_strings) . '; /* ]]> */'; |
|
| 315 | + } |
|
| 316 | + |
|
| 317 | + |
|
| 318 | + /** |
|
| 319 | + * @param mixed string | EED_Module $module |
|
| 320 | + * @throws OutOfBoundsException |
|
| 321 | + * @throws InvalidArgumentException |
|
| 322 | + * @throws InvalidInterfaceException |
|
| 323 | + * @throws InvalidDataTypeException |
|
| 324 | + * @throws EE_Error |
|
| 325 | + * @throws ReflectionException |
|
| 326 | + */ |
|
| 327 | + public function add_module($module) |
|
| 328 | + { |
|
| 329 | + if ($module instanceof EED_Module) { |
|
| 330 | + $module_class = get_class($module); |
|
| 331 | + $this->modules->{$module_class} = $module; |
|
| 332 | + } else { |
|
| 333 | + if (! class_exists('EE_Module_Request_Router', false)) { |
|
| 334 | + $this->load_core('Module_Request_Router'); |
|
| 335 | + } |
|
| 336 | + EE_Module_Request_Router::module_factory($module); |
|
| 337 | + } |
|
| 338 | + } |
|
| 339 | + |
|
| 340 | + |
|
| 341 | + /** |
|
| 342 | + * @param string $module_name |
|
| 343 | + * @return mixed EED_Module | NULL |
|
| 344 | + */ |
|
| 345 | + public function get_module($module_name = '') |
|
| 346 | + { |
|
| 347 | + return isset($this->modules->{$module_name}) |
|
| 348 | + ? $this->modules->{$module_name} |
|
| 349 | + : null; |
|
| 350 | + } |
|
| 351 | + |
|
| 352 | + |
|
| 353 | + /** |
|
| 354 | + * loads core classes - must be singletons |
|
| 355 | + * |
|
| 356 | + * @param string $class_name - simple class name ie: session |
|
| 357 | + * @param mixed $arguments |
|
| 358 | + * @param bool $load_only |
|
| 359 | + * @return mixed |
|
| 360 | + * @throws InvalidInterfaceException |
|
| 361 | + * @throws InvalidDataTypeException |
|
| 362 | + * @throws EE_Error |
|
| 363 | + * @throws ReflectionException |
|
| 364 | + * @throws InvalidArgumentException |
|
| 365 | + */ |
|
| 366 | + public function load_core($class_name, $arguments = array(), $load_only = false) |
|
| 367 | + { |
|
| 368 | + $core_paths = apply_filters( |
|
| 369 | + 'FHEE__EE_Registry__load_core__core_paths', |
|
| 370 | + array( |
|
| 371 | + EE_CORE, |
|
| 372 | + EE_ADMIN, |
|
| 373 | + EE_CPTS, |
|
| 374 | + EE_CORE . 'data_migration_scripts' . DS, |
|
| 375 | + EE_CORE . 'capabilities' . DS, |
|
| 376 | + EE_CORE . 'request_stack' . DS, |
|
| 377 | + EE_CORE . 'middleware' . DS, |
|
| 378 | + ) |
|
| 379 | + ); |
|
| 380 | + // retrieve instantiated class |
|
| 381 | + return $this->_load( |
|
| 382 | + $core_paths, |
|
| 383 | + 'EE_', |
|
| 384 | + $class_name, |
|
| 385 | + 'core', |
|
| 386 | + $arguments, |
|
| 387 | + false, |
|
| 388 | + true, |
|
| 389 | + $load_only |
|
| 390 | + ); |
|
| 391 | + } |
|
| 392 | + |
|
| 393 | + |
|
| 394 | + /** |
|
| 395 | + * loads service classes |
|
| 396 | + * |
|
| 397 | + * @param string $class_name - simple class name ie: session |
|
| 398 | + * @param mixed $arguments |
|
| 399 | + * @param bool $load_only |
|
| 400 | + * @return mixed |
|
| 401 | + * @throws InvalidInterfaceException |
|
| 402 | + * @throws InvalidDataTypeException |
|
| 403 | + * @throws EE_Error |
|
| 404 | + * @throws ReflectionException |
|
| 405 | + * @throws InvalidArgumentException |
|
| 406 | + */ |
|
| 407 | + public function load_service($class_name, $arguments = array(), $load_only = false) |
|
| 408 | + { |
|
| 409 | + $service_paths = apply_filters( |
|
| 410 | + 'FHEE__EE_Registry__load_service__service_paths', |
|
| 411 | + array( |
|
| 412 | + EE_CORE . 'services' . DS, |
|
| 413 | + ) |
|
| 414 | + ); |
|
| 415 | + // retrieve instantiated class |
|
| 416 | + return $this->_load( |
|
| 417 | + $service_paths, |
|
| 418 | + 'EE_', |
|
| 419 | + $class_name, |
|
| 420 | + 'class', |
|
| 421 | + $arguments, |
|
| 422 | + false, |
|
| 423 | + true, |
|
| 424 | + $load_only |
|
| 425 | + ); |
|
| 426 | + } |
|
| 427 | + |
|
| 428 | + |
|
| 429 | + /** |
|
| 430 | + * loads data_migration_scripts |
|
| 431 | + * |
|
| 432 | + * @param string $class_name - class name for the DMS ie: EE_DMS_Core_4_2_0 |
|
| 433 | + * @param mixed $arguments |
|
| 434 | + * @return EE_Data_Migration_Script_Base|mixed |
|
| 435 | + * @throws InvalidInterfaceException |
|
| 436 | + * @throws InvalidDataTypeException |
|
| 437 | + * @throws EE_Error |
|
| 438 | + * @throws ReflectionException |
|
| 439 | + * @throws InvalidArgumentException |
|
| 440 | + */ |
|
| 441 | + public function load_dms($class_name, $arguments = array()) |
|
| 442 | + { |
|
| 443 | + // retrieve instantiated class |
|
| 444 | + return $this->_load( |
|
| 445 | + EE_Data_Migration_Manager::instance()->get_data_migration_script_folders(), |
|
| 446 | + 'EE_DMS_', |
|
| 447 | + $class_name, |
|
| 448 | + 'dms', |
|
| 449 | + $arguments, |
|
| 450 | + false, |
|
| 451 | + false |
|
| 452 | + ); |
|
| 453 | + } |
|
| 454 | + |
|
| 455 | + |
|
| 456 | + /** |
|
| 457 | + * loads object creating classes - must be singletons |
|
| 458 | + * |
|
| 459 | + * @param string $class_name - simple class name ie: attendee |
|
| 460 | + * @param mixed $arguments - an array of arguments to pass to the class |
|
| 461 | + * @param bool $from_db - some classes are instantiated from the db and thus call a different method to |
|
| 462 | + * instantiate |
|
| 463 | + * @param bool $cache if you don't want the class to be stored in the internal cache (non-persistent) then |
|
| 464 | + * set this to FALSE (ie. when instantiating model objects from client in a loop) |
|
| 465 | + * @param bool $load_only whether or not to just load the file and NOT instantiate, or load AND instantiate |
|
| 466 | + * (default) |
|
| 467 | + * @return EE_Base_Class | bool |
|
| 468 | + * @throws InvalidInterfaceException |
|
| 469 | + * @throws InvalidDataTypeException |
|
| 470 | + * @throws EE_Error |
|
| 471 | + * @throws ReflectionException |
|
| 472 | + * @throws InvalidArgumentException |
|
| 473 | + */ |
|
| 474 | + public function load_class($class_name, $arguments = array(), $from_db = false, $cache = true, $load_only = false) |
|
| 475 | + { |
|
| 476 | + $paths = apply_filters( |
|
| 477 | + 'FHEE__EE_Registry__load_class__paths', |
|
| 478 | + array( |
|
| 479 | + EE_CORE, |
|
| 480 | + EE_CLASSES, |
|
| 481 | + EE_BUSINESS, |
|
| 482 | + ) |
|
| 483 | + ); |
|
| 484 | + // retrieve instantiated class |
|
| 485 | + return $this->_load( |
|
| 486 | + $paths, |
|
| 487 | + 'EE_', |
|
| 488 | + $class_name, |
|
| 489 | + 'class', |
|
| 490 | + $arguments, |
|
| 491 | + $from_db, |
|
| 492 | + $cache, |
|
| 493 | + $load_only |
|
| 494 | + ); |
|
| 495 | + } |
|
| 496 | + |
|
| 497 | + |
|
| 498 | + /** |
|
| 499 | + * loads helper classes - must be singletons |
|
| 500 | + * |
|
| 501 | + * @param string $class_name - simple class name ie: price |
|
| 502 | + * @param mixed $arguments |
|
| 503 | + * @param bool $load_only |
|
| 504 | + * @return EEH_Base | bool |
|
| 505 | + * @throws InvalidInterfaceException |
|
| 506 | + * @throws InvalidDataTypeException |
|
| 507 | + * @throws EE_Error |
|
| 508 | + * @throws ReflectionException |
|
| 509 | + * @throws InvalidArgumentException |
|
| 510 | + */ |
|
| 511 | + public function load_helper($class_name, $arguments = array(), $load_only = true) |
|
| 512 | + { |
|
| 513 | + // todo: add doing_it_wrong() in a few versions after all addons have had calls to this method removed |
|
| 514 | + $helper_paths = apply_filters('FHEE__EE_Registry__load_helper__helper_paths', array(EE_HELPERS)); |
|
| 515 | + // retrieve instantiated class |
|
| 516 | + return $this->_load( |
|
| 517 | + $helper_paths, |
|
| 518 | + 'EEH_', |
|
| 519 | + $class_name, |
|
| 520 | + 'helper', |
|
| 521 | + $arguments, |
|
| 522 | + false, |
|
| 523 | + true, |
|
| 524 | + $load_only |
|
| 525 | + ); |
|
| 526 | + } |
|
| 527 | + |
|
| 528 | + |
|
| 529 | + /** |
|
| 530 | + * loads core classes - must be singletons |
|
| 531 | + * |
|
| 532 | + * @param string $class_name - simple class name ie: session |
|
| 533 | + * @param mixed $arguments |
|
| 534 | + * @param bool $load_only |
|
| 535 | + * @param bool $cache whether to cache the object or not. |
|
| 536 | + * @return mixed |
|
| 537 | + * @throws InvalidInterfaceException |
|
| 538 | + * @throws InvalidDataTypeException |
|
| 539 | + * @throws EE_Error |
|
| 540 | + * @throws ReflectionException |
|
| 541 | + * @throws InvalidArgumentException |
|
| 542 | + */ |
|
| 543 | + public function load_lib($class_name, $arguments = array(), $load_only = false, $cache = true) |
|
| 544 | + { |
|
| 545 | + $paths = array( |
|
| 546 | + EE_LIBRARIES, |
|
| 547 | + EE_LIBRARIES . 'messages' . DS, |
|
| 548 | + EE_LIBRARIES . 'shortcodes' . DS, |
|
| 549 | + EE_LIBRARIES . 'qtips' . DS, |
|
| 550 | + EE_LIBRARIES . 'payment_methods' . DS, |
|
| 551 | + ); |
|
| 552 | + // retrieve instantiated class |
|
| 553 | + return $this->_load( |
|
| 554 | + $paths, |
|
| 555 | + 'EE_', |
|
| 556 | + $class_name, |
|
| 557 | + 'lib', |
|
| 558 | + $arguments, |
|
| 559 | + false, |
|
| 560 | + $cache, |
|
| 561 | + $load_only |
|
| 562 | + ); |
|
| 563 | + } |
|
| 564 | + |
|
| 565 | + |
|
| 566 | + /** |
|
| 567 | + * loads model classes - must be singletons |
|
| 568 | + * |
|
| 569 | + * @param string $class_name - simple class name ie: price |
|
| 570 | + * @param mixed $arguments |
|
| 571 | + * @param bool $load_only |
|
| 572 | + * @return EEM_Base | bool |
|
| 573 | + * @throws InvalidInterfaceException |
|
| 574 | + * @throws InvalidDataTypeException |
|
| 575 | + * @throws EE_Error |
|
| 576 | + * @throws ReflectionException |
|
| 577 | + * @throws InvalidArgumentException |
|
| 578 | + */ |
|
| 579 | + public function load_model($class_name, $arguments = array(), $load_only = false) |
|
| 580 | + { |
|
| 581 | + $paths = apply_filters( |
|
| 582 | + 'FHEE__EE_Registry__load_model__paths', |
|
| 583 | + array( |
|
| 584 | + EE_MODELS, |
|
| 585 | + EE_CORE, |
|
| 586 | + ) |
|
| 587 | + ); |
|
| 588 | + // retrieve instantiated class |
|
| 589 | + return $this->_load( |
|
| 590 | + $paths, |
|
| 591 | + 'EEM_', |
|
| 592 | + $class_name, |
|
| 593 | + 'model', |
|
| 594 | + $arguments, |
|
| 595 | + false, |
|
| 596 | + true, |
|
| 597 | + $load_only |
|
| 598 | + ); |
|
| 599 | + } |
|
| 600 | + |
|
| 601 | + |
|
| 602 | + /** |
|
| 603 | + * loads model classes - must be singletons |
|
| 604 | + * |
|
| 605 | + * @param string $class_name - simple class name ie: price |
|
| 606 | + * @param mixed $arguments |
|
| 607 | + * @param bool $load_only |
|
| 608 | + * @return mixed | bool |
|
| 609 | + * @throws InvalidInterfaceException |
|
| 610 | + * @throws InvalidDataTypeException |
|
| 611 | + * @throws EE_Error |
|
| 612 | + * @throws ReflectionException |
|
| 613 | + * @throws InvalidArgumentException |
|
| 614 | + */ |
|
| 615 | + public function load_model_class($class_name, $arguments = array(), $load_only = true) |
|
| 616 | + { |
|
| 617 | + $paths = array( |
|
| 618 | + EE_MODELS . 'fields' . DS, |
|
| 619 | + EE_MODELS . 'helpers' . DS, |
|
| 620 | + EE_MODELS . 'relations' . DS, |
|
| 621 | + EE_MODELS . 'strategies' . DS, |
|
| 622 | + ); |
|
| 623 | + // retrieve instantiated class |
|
| 624 | + return $this->_load( |
|
| 625 | + $paths, |
|
| 626 | + 'EE_', |
|
| 627 | + $class_name, |
|
| 628 | + '', |
|
| 629 | + $arguments, |
|
| 630 | + false, |
|
| 631 | + true, |
|
| 632 | + $load_only |
|
| 633 | + ); |
|
| 634 | + } |
|
| 635 | + |
|
| 636 | + |
|
| 637 | + /** |
|
| 638 | + * Determines if $model_name is the name of an actual EE model. |
|
| 639 | + * |
|
| 640 | + * @param string $model_name like Event, Attendee, Question_Group_Question, etc. |
|
| 641 | + * @return boolean |
|
| 642 | + */ |
|
| 643 | + public function is_model_name($model_name) |
|
| 644 | + { |
|
| 645 | + return isset($this->models[ $model_name ]); |
|
| 646 | + } |
|
| 647 | + |
|
| 648 | + |
|
| 649 | + /** |
|
| 650 | + * generic class loader |
|
| 651 | + * |
|
| 652 | + * @param string $path_to_file - directory path to file location, not including filename |
|
| 653 | + * @param string $file_name - file name ie: my_file.php, including extension |
|
| 654 | + * @param string $type - file type - core? class? helper? model? |
|
| 655 | + * @param mixed $arguments |
|
| 656 | + * @param bool $load_only |
|
| 657 | + * @return mixed |
|
| 658 | + * @throws InvalidInterfaceException |
|
| 659 | + * @throws InvalidDataTypeException |
|
| 660 | + * @throws EE_Error |
|
| 661 | + * @throws ReflectionException |
|
| 662 | + * @throws InvalidArgumentException |
|
| 663 | + */ |
|
| 664 | + public function load_file($path_to_file, $file_name, $type = '', $arguments = array(), $load_only = true) |
|
| 665 | + { |
|
| 666 | + // retrieve instantiated class |
|
| 667 | + return $this->_load( |
|
| 668 | + $path_to_file, |
|
| 669 | + '', |
|
| 670 | + $file_name, |
|
| 671 | + $type, |
|
| 672 | + $arguments, |
|
| 673 | + false, |
|
| 674 | + true, |
|
| 675 | + $load_only |
|
| 676 | + ); |
|
| 677 | + } |
|
| 678 | + |
|
| 679 | + |
|
| 680 | + /** |
|
| 681 | + * @param string $path_to_file - directory path to file location, not including filename |
|
| 682 | + * @param string $class_name - full class name ie: My_Class |
|
| 683 | + * @param string $type - file type - core? class? helper? model? |
|
| 684 | + * @param mixed $arguments |
|
| 685 | + * @param bool $load_only |
|
| 686 | + * @return bool|EE_Addon|object |
|
| 687 | + * @throws InvalidInterfaceException |
|
| 688 | + * @throws InvalidDataTypeException |
|
| 689 | + * @throws EE_Error |
|
| 690 | + * @throws ReflectionException |
|
| 691 | + * @throws InvalidArgumentException |
|
| 692 | + */ |
|
| 693 | + public function load_addon($path_to_file, $class_name, $type = 'class', $arguments = array(), $load_only = false) |
|
| 694 | + { |
|
| 695 | + // retrieve instantiated class |
|
| 696 | + return $this->_load( |
|
| 697 | + $path_to_file, |
|
| 698 | + 'addon', |
|
| 699 | + $class_name, |
|
| 700 | + $type, |
|
| 701 | + $arguments, |
|
| 702 | + false, |
|
| 703 | + true, |
|
| 704 | + $load_only |
|
| 705 | + ); |
|
| 706 | + } |
|
| 707 | + |
|
| 708 | + |
|
| 709 | + /** |
|
| 710 | + * instantiates, caches, and automatically resolves dependencies |
|
| 711 | + * for classes that use a Fully Qualified Class Name. |
|
| 712 | + * if the class is not capable of being loaded using PSR-4 autoloading, |
|
| 713 | + * then you need to use one of the existing load_*() methods |
|
| 714 | + * which can resolve the classname and filepath from the passed arguments |
|
| 715 | + * |
|
| 716 | + * @param bool|string $class_name Fully Qualified Class Name |
|
| 717 | + * @param array $arguments an argument, or array of arguments to pass to the class upon instantiation |
|
| 718 | + * @param bool $cache whether to cache the instantiated object for reuse |
|
| 719 | + * @param bool $from_db some classes are instantiated from the db |
|
| 720 | + * and thus call a different method to instantiate |
|
| 721 | + * @param bool $load_only if true, will only load the file, but will NOT instantiate an object |
|
| 722 | + * @param bool|string $addon if true, will cache the object in the EE_Registry->$addons array |
|
| 723 | + * @return bool|null|mixed null = failure to load or instantiate class object. |
|
| 724 | + * object = class loaded and instantiated successfully. |
|
| 725 | + * bool = fail or success when $load_only is true |
|
| 726 | + * @throws InvalidInterfaceException |
|
| 727 | + * @throws InvalidDataTypeException |
|
| 728 | + * @throws EE_Error |
|
| 729 | + * @throws ReflectionException |
|
| 730 | + * @throws InvalidArgumentException |
|
| 731 | + */ |
|
| 732 | + public function create( |
|
| 733 | + $class_name = false, |
|
| 734 | + $arguments = array(), |
|
| 735 | + $cache = false, |
|
| 736 | + $from_db = false, |
|
| 737 | + $load_only = false, |
|
| 738 | + $addon = false |
|
| 739 | + ) { |
|
| 740 | + $class_name = ltrim($class_name, '\\'); |
|
| 741 | + $class_name = $this->class_cache->getFqnForAlias($class_name); |
|
| 742 | + $class_exists = $this->loadOrVerifyClassExists($class_name, $arguments); |
|
| 743 | + // if a non-FQCN was passed, then |
|
| 744 | + // verifyClassExists() might return an object |
|
| 745 | + // or it could return null if the class just could not be found anywhere |
|
| 746 | + if ($class_exists instanceof $class_name || $class_exists === null) { |
|
| 747 | + // either way, return the results |
|
| 748 | + return $class_exists; |
|
| 749 | + } |
|
| 750 | + $class_name = $class_exists; |
|
| 751 | + // if we're only loading the class and it already exists, then let's just return true immediately |
|
| 752 | + if ($load_only) { |
|
| 753 | + return true; |
|
| 754 | + } |
|
| 755 | + $addon = $addon ? 'addon' : ''; |
|
| 756 | + // $this->_cache_on is toggled during the recursive loading that can occur with dependency injection |
|
| 757 | + // $cache is controlled by individual calls to separate Registry loader methods like load_class() |
|
| 758 | + // $load_only is also controlled by individual calls to separate Registry loader methods like load_file() |
|
| 759 | + if ($this->_cache_on && $cache && ! $load_only) { |
|
| 760 | + // return object if it's already cached |
|
| 761 | + $cached_class = $this->_get_cached_class($class_name, $addon, $arguments); |
|
| 762 | + if ($cached_class !== null) { |
|
| 763 | + return $cached_class; |
|
| 764 | + } |
|
| 765 | + }// obtain the loader method from the dependency map |
|
| 766 | + $loader = $this->_dependency_map->class_loader($class_name);// instantiate the requested object |
|
| 767 | + if ($loader instanceof Closure) { |
|
| 768 | + $class_obj = $loader($arguments); |
|
| 769 | + } else { |
|
| 770 | + if ($loader && method_exists($this, $loader)) { |
|
| 771 | + $class_obj = $this->{$loader}($class_name, $arguments); |
|
| 772 | + } else { |
|
| 773 | + $class_obj = $this->_create_object($class_name, $arguments, $addon, $from_db); |
|
| 774 | + } |
|
| 775 | + } |
|
| 776 | + if (($this->_cache_on && $cache) || $this->get_class_abbreviation($class_name, '')) { |
|
| 777 | + // save it for later... kinda like gum { : $ |
|
| 778 | + $this->_set_cached_class( |
|
| 779 | + $class_obj, |
|
| 780 | + $class_name, |
|
| 781 | + $addon, |
|
| 782 | + $from_db, |
|
| 783 | + $arguments |
|
| 784 | + ); |
|
| 785 | + } |
|
| 786 | + $this->_cache_on = true; |
|
| 787 | + return $class_obj; |
|
| 788 | + } |
|
| 789 | + |
|
| 790 | + |
|
| 791 | + /** |
|
| 792 | + * Recursively checks that a class exists and potentially attempts to load classes with non-FQCNs |
|
| 793 | + * |
|
| 794 | + * @param string|object $class_name |
|
| 795 | + * @param array $arguments |
|
| 796 | + * @param int $attempt |
|
| 797 | + * @return mixed |
|
| 798 | + */ |
|
| 799 | + private function loadOrVerifyClassExists($class_name, array $arguments, $attempt = 1) |
|
| 800 | + { |
|
| 801 | + if (is_object($class_name) || class_exists($class_name)) { |
|
| 802 | + return $class_name; |
|
| 803 | + } |
|
| 804 | + switch ($attempt) { |
|
| 805 | + case 1: |
|
| 806 | + // if it's a FQCN then maybe the class is registered with a preceding \ |
|
| 807 | + $class_name = strpos($class_name, '\\') !== false |
|
| 808 | + ? '\\' . ltrim($class_name, '\\') |
|
| 809 | + : $class_name; |
|
| 810 | + break; |
|
| 811 | + case 2: |
|
| 812 | + // |
|
| 813 | + $loader = $this->_dependency_map->class_loader($class_name); |
|
| 814 | + if ($loader && method_exists($this, $loader)) { |
|
| 815 | + return $this->{$loader}($class_name, $arguments); |
|
| 816 | + } |
|
| 817 | + break; |
|
| 818 | + case 3: |
|
| 819 | + default: |
|
| 820 | + return null; |
|
| 821 | + } |
|
| 822 | + $attempt++; |
|
| 823 | + return $this->loadOrVerifyClassExists($class_name, $arguments, $attempt); |
|
| 824 | + } |
|
| 825 | + |
|
| 826 | + |
|
| 827 | + /** |
|
| 828 | + * instantiates, caches, and injects dependencies for classes |
|
| 829 | + * |
|
| 830 | + * @param array $file_paths an array of paths to folders to look in |
|
| 831 | + * @param string $class_prefix EE or EEM or... ??? |
|
| 832 | + * @param bool|string $class_name $class name |
|
| 833 | + * @param string $type file type - core? class? helper? model? |
|
| 834 | + * @param mixed $arguments an argument or array of arguments to pass to the class upon instantiation |
|
| 835 | + * @param bool $from_db some classes are instantiated from the db |
|
| 836 | + * and thus call a different method to instantiate |
|
| 837 | + * @param bool $cache whether to cache the instantiated object for reuse |
|
| 838 | + * @param bool $load_only if true, will only load the file, but will NOT instantiate an object |
|
| 839 | + * @return bool|null|object null = failure to load or instantiate class object. |
|
| 840 | + * object = class loaded and instantiated successfully. |
|
| 841 | + * bool = fail or success when $load_only is true |
|
| 842 | + * @throws EE_Error |
|
| 843 | + * @throws ReflectionException |
|
| 844 | + * @throws InvalidInterfaceException |
|
| 845 | + * @throws InvalidDataTypeException |
|
| 846 | + * @throws InvalidArgumentException |
|
| 847 | + */ |
|
| 848 | + protected function _load( |
|
| 849 | + $file_paths = array(), |
|
| 850 | + $class_prefix = 'EE_', |
|
| 851 | + $class_name = false, |
|
| 852 | + $type = 'class', |
|
| 853 | + $arguments = array(), |
|
| 854 | + $from_db = false, |
|
| 855 | + $cache = true, |
|
| 856 | + $load_only = false |
|
| 857 | + ) { |
|
| 858 | + $class_name = ltrim($class_name, '\\'); |
|
| 859 | + // strip php file extension |
|
| 860 | + $class_name = str_replace('.php', '', trim($class_name)); |
|
| 861 | + // does the class have a prefix ? |
|
| 862 | + if (! empty($class_prefix) && $class_prefix !== 'addon') { |
|
| 863 | + // make sure $class_prefix is uppercase |
|
| 864 | + $class_prefix = strtoupper(trim($class_prefix)); |
|
| 865 | + // add class prefix ONCE!!! |
|
| 866 | + $class_name = $class_prefix . str_replace($class_prefix, '', $class_name); |
|
| 867 | + } |
|
| 868 | + $class_name = $this->class_cache->getFqnForAlias($class_name); |
|
| 869 | + $class_exists = class_exists($class_name, false); |
|
| 870 | + // if we're only loading the class and it already exists, then let's just return true immediately |
|
| 871 | + if ($load_only && $class_exists) { |
|
| 872 | + return true; |
|
| 873 | + } |
|
| 874 | + $arguments = is_array($arguments) ? $arguments : array($arguments); |
|
| 875 | + // $this->_cache_on is toggled during the recursive loading that can occur with dependency injection |
|
| 876 | + // $cache is controlled by individual calls to separate Registry loader methods like load_class() |
|
| 877 | + // $load_only is also controlled by individual calls to separate Registry loader methods like load_file() |
|
| 878 | + if ($this->_cache_on && $cache && ! $load_only) { |
|
| 879 | + // return object if it's already cached |
|
| 880 | + $cached_class = $this->_get_cached_class($class_name, $class_prefix, $arguments); |
|
| 881 | + if ($cached_class !== null) { |
|
| 882 | + return $cached_class; |
|
| 883 | + } |
|
| 884 | + } |
|
| 885 | + // if the class doesn't already exist.. then we need to try and find the file and load it |
|
| 886 | + if (! $class_exists) { |
|
| 887 | + // get full path to file |
|
| 888 | + $path = $this->_resolve_path($class_name, $type, $file_paths); |
|
| 889 | + // load the file |
|
| 890 | + $loaded = $this->_require_file($path, $class_name, $type, $file_paths); |
|
| 891 | + // if we are only loading a file but NOT instantiating an object |
|
| 892 | + // then return boolean for whether class was loaded or not |
|
| 893 | + if ($load_only) { |
|
| 894 | + return $loaded; |
|
| 895 | + } |
|
| 896 | + // if an object was expected but loading failed, then return nothing |
|
| 897 | + if (! $loaded) { |
|
| 898 | + return null; |
|
| 899 | + } |
|
| 900 | + } |
|
| 901 | + // instantiate the requested object |
|
| 902 | + $class_obj = $this->_create_object($class_name, $arguments, $type, $from_db); |
|
| 903 | + if ($this->_cache_on && $cache) { |
|
| 904 | + // save it for later... kinda like gum { : $ |
|
| 905 | + $this->_set_cached_class( |
|
| 906 | + $class_obj, |
|
| 907 | + $class_name, |
|
| 908 | + $class_prefix, |
|
| 909 | + $from_db, |
|
| 910 | + $arguments |
|
| 911 | + ); |
|
| 912 | + } |
|
| 913 | + $this->_cache_on = true; |
|
| 914 | + return $class_obj; |
|
| 915 | + } |
|
| 916 | + |
|
| 917 | + |
|
| 918 | + /** |
|
| 919 | + * @param string $class_name |
|
| 920 | + * @param string $default have to specify something, but not anything that will conflict |
|
| 921 | + * @return mixed|string |
|
| 922 | + */ |
|
| 923 | + protected function get_class_abbreviation($class_name, $default = 'FANCY_BATMAN_PANTS') |
|
| 924 | + { |
|
| 925 | + return isset($this->_class_abbreviations[ $class_name ]) |
|
| 926 | + ? $this->_class_abbreviations[ $class_name ] |
|
| 927 | + : $default; |
|
| 928 | + } |
|
| 929 | + |
|
| 930 | + |
|
| 931 | + /** |
|
| 932 | + * attempts to find a cached version of the requested class |
|
| 933 | + * by looking in the following places: |
|
| 934 | + * $this->{$class_abbreviation} ie: $this->CART |
|
| 935 | + * $this->{$class_name} ie: $this->Some_Class |
|
| 936 | + * $this->LIB->{$class_name} ie: $this->LIB->Some_Class |
|
| 937 | + * $this->addon->{$class_name} ie: $this->addon->Some_Addon_Class |
|
| 938 | + * |
|
| 939 | + * @param string $class_name |
|
| 940 | + * @param string $class_prefix |
|
| 941 | + * @param array $arguments |
|
| 942 | + * @return mixed |
|
| 943 | + */ |
|
| 944 | + protected function _get_cached_class( |
|
| 945 | + $class_name, |
|
| 946 | + $class_prefix = '', |
|
| 947 | + $arguments = array() |
|
| 948 | + ) { |
|
| 949 | + if ($class_name === 'EE_Registry') { |
|
| 950 | + return $this; |
|
| 951 | + } |
|
| 952 | + $class_abbreviation = $this->get_class_abbreviation($class_name); |
|
| 953 | + // check if class has already been loaded, and return it if it has been |
|
| 954 | + if (isset($this->{$class_abbreviation})) { |
|
| 955 | + return $this->{$class_abbreviation}; |
|
| 956 | + } |
|
| 957 | + $class_name = str_replace('\\', '_', $class_name); |
|
| 958 | + if (isset($this->{$class_name})) { |
|
| 959 | + return $this->{$class_name}; |
|
| 960 | + } |
|
| 961 | + if ($class_prefix === 'addon' && isset($this->addons->{$class_name})) { |
|
| 962 | + return $this->addons->{$class_name}; |
|
| 963 | + } |
|
| 964 | + $object_identifier = $this->object_identifier->getIdentifier($class_name, $arguments); |
|
| 965 | + if (isset($this->LIB->{$object_identifier})) { |
|
| 966 | + return $this->LIB->{$object_identifier}; |
|
| 967 | + } |
|
| 968 | + foreach ($this->LIB as $key => $object) { |
|
| 969 | + if (// request does not contain new arguments and therefore no args identifier |
|
| 970 | + ! $this->object_identifier->hasArguments($object_identifier) |
|
| 971 | + // but previously cached class with args was found |
|
| 972 | + && $this->object_identifier->fqcnMatchesObjectIdentifier($class_name, $key) |
|
| 973 | + ) { |
|
| 974 | + return $object; |
|
| 975 | + } |
|
| 976 | + } |
|
| 977 | + return null; |
|
| 978 | + } |
|
| 979 | + |
|
| 980 | + |
|
| 981 | + /** |
|
| 982 | + * removes a cached version of the requested class |
|
| 983 | + * |
|
| 984 | + * @param string $class_name |
|
| 985 | + * @param boolean $addon |
|
| 986 | + * @param array $arguments |
|
| 987 | + * @return boolean |
|
| 988 | + */ |
|
| 989 | + public function clear_cached_class( |
|
| 990 | + $class_name, |
|
| 991 | + $addon = false, |
|
| 992 | + $arguments = array() |
|
| 993 | + ) { |
|
| 994 | + $class_abbreviation = $this->get_class_abbreviation($class_name); |
|
| 995 | + // check if class has already been loaded, and return it if it has been |
|
| 996 | + if (isset($this->{$class_abbreviation})) { |
|
| 997 | + $this->{$class_abbreviation} = null; |
|
| 998 | + return true; |
|
| 999 | + } |
|
| 1000 | + $class_name = str_replace('\\', '_', $class_name); |
|
| 1001 | + if (isset($this->{$class_name})) { |
|
| 1002 | + $this->{$class_name} = null; |
|
| 1003 | + return true; |
|
| 1004 | + } |
|
| 1005 | + if ($addon && isset($this->addons->{$class_name})) { |
|
| 1006 | + unset($this->addons->{$class_name}); |
|
| 1007 | + return true; |
|
| 1008 | + } |
|
| 1009 | + $class_name = $this->object_identifier->getIdentifier($class_name, $arguments); |
|
| 1010 | + if (isset($this->LIB->{$class_name})) { |
|
| 1011 | + unset($this->LIB->{$class_name}); |
|
| 1012 | + return true; |
|
| 1013 | + } |
|
| 1014 | + return false; |
|
| 1015 | + } |
|
| 1016 | + |
|
| 1017 | + |
|
| 1018 | + /** |
|
| 1019 | + * _set_cached_class |
|
| 1020 | + * attempts to cache the instantiated class locally |
|
| 1021 | + * in one of the following places, in the following order: |
|
| 1022 | + * $this->{class_abbreviation} ie: $this->CART |
|
| 1023 | + * $this->{$class_name} ie: $this->Some_Class |
|
| 1024 | + * $this->addon->{$$class_name} ie: $this->addon->Some_Addon_Class |
|
| 1025 | + * $this->LIB->{$class_name} ie: $this->LIB->Some_Class |
|
| 1026 | + * |
|
| 1027 | + * @param object $class_obj |
|
| 1028 | + * @param string $class_name |
|
| 1029 | + * @param string $class_prefix |
|
| 1030 | + * @param bool $from_db |
|
| 1031 | + * @param array $arguments |
|
| 1032 | + * @return void |
|
| 1033 | + */ |
|
| 1034 | + protected function _set_cached_class( |
|
| 1035 | + $class_obj, |
|
| 1036 | + $class_name, |
|
| 1037 | + $class_prefix = '', |
|
| 1038 | + $from_db = false, |
|
| 1039 | + $arguments = array() |
|
| 1040 | + ) { |
|
| 1041 | + if ($class_name === 'EE_Registry' || empty($class_obj)) { |
|
| 1042 | + return; |
|
| 1043 | + } |
|
| 1044 | + // return newly instantiated class |
|
| 1045 | + $class_abbreviation = $this->get_class_abbreviation($class_name, ''); |
|
| 1046 | + if ($class_abbreviation) { |
|
| 1047 | + $this->{$class_abbreviation} = $class_obj; |
|
| 1048 | + return; |
|
| 1049 | + } |
|
| 1050 | + $class_name = str_replace('\\', '_', $class_name); |
|
| 1051 | + if (property_exists($this, $class_name)) { |
|
| 1052 | + $this->{$class_name} = $class_obj; |
|
| 1053 | + return; |
|
| 1054 | + } |
|
| 1055 | + if ($class_prefix === 'addon') { |
|
| 1056 | + $this->addons->{$class_name} = $class_obj; |
|
| 1057 | + return; |
|
| 1058 | + } |
|
| 1059 | + if (! $from_db) { |
|
| 1060 | + $class_name = $this->object_identifier->getIdentifier($class_name, $arguments); |
|
| 1061 | + $this->LIB->{$class_name} = $class_obj; |
|
| 1062 | + } |
|
| 1063 | + } |
|
| 1064 | + |
|
| 1065 | + |
|
| 1066 | + /** |
|
| 1067 | + * attempts to find a full valid filepath for the requested class. |
|
| 1068 | + * loops thru each of the base paths in the $file_paths array and appends : "{classname} . {file type} . php" |
|
| 1069 | + * then returns that path if the target file has been found and is readable |
|
| 1070 | + * |
|
| 1071 | + * @param string $class_name |
|
| 1072 | + * @param string $type |
|
| 1073 | + * @param array $file_paths |
|
| 1074 | + * @return string | bool |
|
| 1075 | + */ |
|
| 1076 | + protected function _resolve_path($class_name, $type = '', $file_paths = array()) |
|
| 1077 | + { |
|
| 1078 | + // make sure $file_paths is an array |
|
| 1079 | + $file_paths = is_array($file_paths) |
|
| 1080 | + ? $file_paths |
|
| 1081 | + : array($file_paths); |
|
| 1082 | + // cycle thru paths |
|
| 1083 | + foreach ($file_paths as $key => $file_path) { |
|
| 1084 | + // convert all separators to proper DS, if no filepath, then use EE_CLASSES |
|
| 1085 | + $file_path = $file_path |
|
| 1086 | + ? str_replace(array('/', '\\'), DS, $file_path) |
|
| 1087 | + : EE_CLASSES; |
|
| 1088 | + // prep file type |
|
| 1089 | + $type = ! empty($type) |
|
| 1090 | + ? trim($type, '.') . '.' |
|
| 1091 | + : ''; |
|
| 1092 | + // build full file path |
|
| 1093 | + $file_paths[ $key ] = rtrim($file_path, DS) . DS . $class_name . '.' . $type . 'php'; |
|
| 1094 | + // does the file exist and can be read ? |
|
| 1095 | + if (is_readable($file_paths[ $key ])) { |
|
| 1096 | + return $file_paths[ $key ]; |
|
| 1097 | + } |
|
| 1098 | + } |
|
| 1099 | + return false; |
|
| 1100 | + } |
|
| 1101 | + |
|
| 1102 | + |
|
| 1103 | + /** |
|
| 1104 | + * basically just performs a require_once() |
|
| 1105 | + * but with some error handling |
|
| 1106 | + * |
|
| 1107 | + * @param string $path |
|
| 1108 | + * @param string $class_name |
|
| 1109 | + * @param string $type |
|
| 1110 | + * @param array $file_paths |
|
| 1111 | + * @return bool |
|
| 1112 | + * @throws EE_Error |
|
| 1113 | + * @throws ReflectionException |
|
| 1114 | + */ |
|
| 1115 | + protected function _require_file($path, $class_name, $type = '', $file_paths = array()) |
|
| 1116 | + { |
|
| 1117 | + $this->resolve_legacy_class_parent($class_name); |
|
| 1118 | + // don't give up! you gotta... |
|
| 1119 | + try { |
|
| 1120 | + // does the file exist and can it be read ? |
|
| 1121 | + if (! $path) { |
|
| 1122 | + // just in case the file has already been autoloaded, |
|
| 1123 | + // but discrepancies in the naming schema are preventing it from |
|
| 1124 | + // being loaded via one of the EE_Registry::load_*() methods, |
|
| 1125 | + // then let's try one last hail mary before throwing an exception |
|
| 1126 | + // and call class_exists() again, but with autoloading turned ON |
|
| 1127 | + if (class_exists($class_name)) { |
|
| 1128 | + return true; |
|
| 1129 | + } |
|
| 1130 | + // so sorry, can't find the file |
|
| 1131 | + throw new EE_Error( |
|
| 1132 | + sprintf( |
|
| 1133 | + esc_html__( |
|
| 1134 | + 'The %1$s file %2$s could not be located or is not readable due to file permissions. Please ensure that the following filepath(s) are correct: %3$s', |
|
| 1135 | + 'event_espresso' |
|
| 1136 | + ), |
|
| 1137 | + trim($type, '.'), |
|
| 1138 | + $class_name, |
|
| 1139 | + '<br />' . implode(',<br />', $file_paths) |
|
| 1140 | + ) |
|
| 1141 | + ); |
|
| 1142 | + } |
|
| 1143 | + // get the file |
|
| 1144 | + require_once($path); |
|
| 1145 | + // if the class isn't already declared somewhere |
|
| 1146 | + if (class_exists($class_name, false) === false) { |
|
| 1147 | + // so sorry, not a class |
|
| 1148 | + throw new EE_Error( |
|
| 1149 | + sprintf( |
|
| 1150 | + esc_html__( |
|
| 1151 | + 'The %s file %s does not appear to contain the %s Class.', |
|
| 1152 | + 'event_espresso' |
|
| 1153 | + ), |
|
| 1154 | + $type, |
|
| 1155 | + $path, |
|
| 1156 | + $class_name |
|
| 1157 | + ) |
|
| 1158 | + ); |
|
| 1159 | + } |
|
| 1160 | + } catch (EE_Error $e) { |
|
| 1161 | + $e->get_error(); |
|
| 1162 | + return false; |
|
| 1163 | + } |
|
| 1164 | + return true; |
|
| 1165 | + } |
|
| 1166 | + |
|
| 1167 | + |
|
| 1168 | + /** |
|
| 1169 | + * Some of our legacy classes that extended a parent class would simply use a require() statement |
|
| 1170 | + * before their class declaration in order to ensure that the parent class was loaded. |
|
| 1171 | + * This is not ideal, but it's nearly impossible to determine the parent class of a non-namespaced class, |
|
| 1172 | + * without triggering a fatal error because the parent class has yet to be loaded and therefore doesn't exist. |
|
| 1173 | + * |
|
| 1174 | + * @param string $class_name |
|
| 1175 | + */ |
|
| 1176 | + protected function resolve_legacy_class_parent($class_name = '') |
|
| 1177 | + { |
|
| 1178 | + try { |
|
| 1179 | + $legacy_parent_class_map = array( |
|
| 1180 | + 'EE_Payment_Processor' => 'core/business/EE_Processor_Base.class.php', |
|
| 1181 | + ); |
|
| 1182 | + if (isset($legacy_parent_class_map[ $class_name ])) { |
|
| 1183 | + require_once EE_PLUGIN_DIR_PATH . $legacy_parent_class_map[ $class_name ]; |
|
| 1184 | + } |
|
| 1185 | + } catch (Exception $exception) { |
|
| 1186 | + } |
|
| 1187 | + } |
|
| 1188 | + |
|
| 1189 | + |
|
| 1190 | + /** |
|
| 1191 | + * _create_object |
|
| 1192 | + * Attempts to instantiate the requested class via any of the |
|
| 1193 | + * commonly used instantiation methods employed throughout EE. |
|
| 1194 | + * The priority for instantiation is as follows: |
|
| 1195 | + * - abstract classes or any class flagged as "load only" (no instantiation occurs) |
|
| 1196 | + * - model objects via their 'new_instance_from_db' method |
|
| 1197 | + * - model objects via their 'new_instance' method |
|
| 1198 | + * - "singleton" classes" via their 'instance' method |
|
| 1199 | + * - standard instantiable classes via their __constructor |
|
| 1200 | + * Prior to instantiation, if the classname exists in the dependency_map, |
|
| 1201 | + * then the constructor for the requested class will be examined to determine |
|
| 1202 | + * if any dependencies exist, and if they can be injected. |
|
| 1203 | + * If so, then those classes will be added to the array of arguments passed to the constructor |
|
| 1204 | + * |
|
| 1205 | + * @param string $class_name |
|
| 1206 | + * @param array $arguments |
|
| 1207 | + * @param string $type |
|
| 1208 | + * @param bool $from_db |
|
| 1209 | + * @return null|object|bool |
|
| 1210 | + * @throws InvalidArgumentException |
|
| 1211 | + * @throws InvalidInterfaceException |
|
| 1212 | + * @throws EE_Error |
|
| 1213 | + * @throws ReflectionException |
|
| 1214 | + * @throws InvalidDataTypeException |
|
| 1215 | + */ |
|
| 1216 | + protected function _create_object($class_name, $arguments = array(), $type = '', $from_db = false) |
|
| 1217 | + { |
|
| 1218 | + // create reflection |
|
| 1219 | + $reflector = $this->mirror->getReflectionClass($class_name); |
|
| 1220 | + // make sure arguments are an array |
|
| 1221 | + $arguments = is_array($arguments) |
|
| 1222 | + ? $arguments |
|
| 1223 | + : array($arguments); |
|
| 1224 | + // and if arguments array is numerically and sequentially indexed, then we want it to remain as is, |
|
| 1225 | + // else wrap it in an additional array so that it doesn't get split into multiple parameters |
|
| 1226 | + $arguments = $this->_array_is_numerically_and_sequentially_indexed($arguments) |
|
| 1227 | + ? $arguments |
|
| 1228 | + : array($arguments); |
|
| 1229 | + // attempt to inject dependencies ? |
|
| 1230 | + if ($this->_dependency_map->has($class_name)) { |
|
| 1231 | + $arguments = $this->_resolve_dependencies($reflector, $class_name, $arguments); |
|
| 1232 | + } |
|
| 1233 | + // instantiate the class if possible |
|
| 1234 | + if ($reflector->isAbstract()) { |
|
| 1235 | + // nothing to instantiate, loading file was enough |
|
| 1236 | + // does not throw an exception so $instantiation_mode is unused |
|
| 1237 | + // $instantiation_mode = "1) no constructor abstract class"; |
|
| 1238 | + return true; |
|
| 1239 | + } |
|
| 1240 | + if (empty($arguments) |
|
| 1241 | + && $this->mirror->getConstructorFromReflection($reflector) === null |
|
| 1242 | + && $reflector->isInstantiable() |
|
| 1243 | + ) { |
|
| 1244 | + // no constructor = static methods only... nothing to instantiate, loading file was enough |
|
| 1245 | + // $instantiation_mode = "2) no constructor but instantiable"; |
|
| 1246 | + return $reflector->newInstance(); |
|
| 1247 | + } |
|
| 1248 | + if ($from_db && method_exists($class_name, 'new_instance_from_db')) { |
|
| 1249 | + // $instantiation_mode = "3) new_instance_from_db()"; |
|
| 1250 | + return call_user_func_array(array($class_name, 'new_instance_from_db'), $arguments); |
|
| 1251 | + } |
|
| 1252 | + if (method_exists($class_name, 'new_instance')) { |
|
| 1253 | + // $instantiation_mode = "4) new_instance()"; |
|
| 1254 | + return call_user_func_array(array($class_name, 'new_instance'), $arguments); |
|
| 1255 | + } |
|
| 1256 | + if (method_exists($class_name, 'instance')) { |
|
| 1257 | + // $instantiation_mode = "5) instance()"; |
|
| 1258 | + return call_user_func_array(array($class_name, 'instance'), $arguments); |
|
| 1259 | + } |
|
| 1260 | + if ($reflector->isInstantiable()) { |
|
| 1261 | + // $instantiation_mode = "6) constructor"; |
|
| 1262 | + return $reflector->newInstanceArgs($arguments); |
|
| 1263 | + } |
|
| 1264 | + // heh ? something's not right ! |
|
| 1265 | + throw new EE_Error( |
|
| 1266 | + sprintf( |
|
| 1267 | + __('The %s file %s could not be instantiated.', 'event_espresso'), |
|
| 1268 | + $type, |
|
| 1269 | + $class_name |
|
| 1270 | + ) |
|
| 1271 | + ); |
|
| 1272 | + } |
|
| 1273 | + |
|
| 1274 | + |
|
| 1275 | + /** |
|
| 1276 | + * @see http://stackoverflow.com/questions/173400/how-to-check-if-php-array-is-associative-or-sequential |
|
| 1277 | + * @param array $array |
|
| 1278 | + * @return bool |
|
| 1279 | + */ |
|
| 1280 | + protected function _array_is_numerically_and_sequentially_indexed(array $array) |
|
| 1281 | + { |
|
| 1282 | + return ! empty($array) |
|
| 1283 | + ? array_keys($array) === range(0, count($array) - 1) |
|
| 1284 | + : true; |
|
| 1285 | + } |
|
| 1286 | + |
|
| 1287 | + |
|
| 1288 | + /** |
|
| 1289 | + * _resolve_dependencies |
|
| 1290 | + * examines the constructor for the requested class to determine |
|
| 1291 | + * if any dependencies exist, and if they can be injected. |
|
| 1292 | + * If so, then those classes will be added to the array of arguments passed to the constructor |
|
| 1293 | + * PLZ NOTE: this is achieved by type hinting the constructor params |
|
| 1294 | + * For example: |
|
| 1295 | + * if attempting to load a class "Foo" with the following constructor: |
|
| 1296 | + * __construct( Bar $bar_class, Fighter $grohl_class ) |
|
| 1297 | + * then $bar_class and $grohl_class will be added to the $arguments array, |
|
| 1298 | + * but only IF they are NOT already present in the incoming arguments array, |
|
| 1299 | + * and the correct classes can be loaded |
|
| 1300 | + * |
|
| 1301 | + * @param ReflectionClass $reflector |
|
| 1302 | + * @param string $class_name |
|
| 1303 | + * @param array $arguments |
|
| 1304 | + * @return array |
|
| 1305 | + * @throws InvalidArgumentException |
|
| 1306 | + * @throws InvalidDataTypeException |
|
| 1307 | + * @throws InvalidInterfaceException |
|
| 1308 | + * @throws ReflectionException |
|
| 1309 | + */ |
|
| 1310 | + protected function _resolve_dependencies(ReflectionClass $reflector, $class_name, array $arguments = array()) |
|
| 1311 | + { |
|
| 1312 | + // let's examine the constructor |
|
| 1313 | + $constructor = $this->mirror->getConstructorFromReflection($reflector); |
|
| 1314 | + // whu? huh? nothing? |
|
| 1315 | + if (! $constructor) { |
|
| 1316 | + return $arguments; |
|
| 1317 | + } |
|
| 1318 | + // get constructor parameters |
|
| 1319 | + $params = $this->mirror->getParametersFromReflection($reflector); |
|
| 1320 | + // and the keys for the incoming arguments array so that we can compare existing arguments with what is expected |
|
| 1321 | + $argument_keys = array_keys($arguments); |
|
| 1322 | + // now loop thru all of the constructors expected parameters |
|
| 1323 | + foreach ($params as $index => $param) { |
|
| 1324 | + try { |
|
| 1325 | + // is this a dependency for a specific class ? |
|
| 1326 | + $param_class = $this->mirror->getParameterClassName($param, $class_name, $index); |
|
| 1327 | + } catch (ReflectionException $exception) { |
|
| 1328 | + // uh-oh... most likely a legacy class that has not been autoloaded |
|
| 1329 | + // let's try to derive the classname from what we have now |
|
| 1330 | + // and hope that the property var name is close to the class name |
|
| 1331 | + $param_class = $param->getName(); |
|
| 1332 | + $param_class = str_replace('_', ' ',$param_class); |
|
| 1333 | + $param_class = ucwords($param_class); |
|
| 1334 | + $param_class = str_replace(' ', '_', $param_class); |
|
| 1335 | + } |
|
| 1336 | + // BUT WAIT !!! This class may be an alias for something else (or getting replaced at runtime) |
|
| 1337 | + $param_class = $this->class_cache->isAlias($param_class, $class_name) |
|
| 1338 | + ? $this->class_cache->getFqnForAlias($param_class, $class_name) |
|
| 1339 | + : $param_class; |
|
| 1340 | + if (// param is not even a class |
|
| 1341 | + $param_class === null |
|
| 1342 | + // and something already exists in the incoming arguments for this param |
|
| 1343 | + && array_key_exists($index, $argument_keys) |
|
| 1344 | + && array_key_exists($argument_keys[ $index ], $arguments) |
|
| 1345 | + ) { |
|
| 1346 | + // so let's skip this argument and move on to the next |
|
| 1347 | + continue; |
|
| 1348 | + } |
|
| 1349 | + if (// parameter is type hinted as a class, exists as an incoming argument, AND it's the correct class |
|
| 1350 | + $param_class !== null |
|
| 1351 | + && isset($argument_keys[ $index ], $arguments[ $argument_keys[ $index ] ]) |
|
| 1352 | + && $arguments[ $argument_keys[ $index ] ] instanceof $param_class |
|
| 1353 | + ) { |
|
| 1354 | + // skip this argument and move on to the next |
|
| 1355 | + continue; |
|
| 1356 | + } |
|
| 1357 | + if (// parameter is type hinted as a class, and should be injected |
|
| 1358 | + $param_class !== null |
|
| 1359 | + && $this->_dependency_map->has_dependency_for_class($class_name, $param_class) |
|
| 1360 | + ) { |
|
| 1361 | + $arguments = $this->_resolve_dependency( |
|
| 1362 | + $class_name, |
|
| 1363 | + $param_class, |
|
| 1364 | + $arguments, |
|
| 1365 | + $index |
|
| 1366 | + ); |
|
| 1367 | + } |
|
| 1368 | + if (empty($arguments[ $index ])) { |
|
| 1369 | + $arguments[ $index ] = $this->mirror->getParameterDefaultValue( |
|
| 1370 | + $param, |
|
| 1371 | + $class_name, |
|
| 1372 | + $index |
|
| 1373 | + ); |
|
| 1374 | + } |
|
| 1375 | + } |
|
| 1376 | + return $arguments; |
|
| 1377 | + } |
|
| 1378 | + |
|
| 1379 | + |
|
| 1380 | + /** |
|
| 1381 | + * @param string $class_name |
|
| 1382 | + * @param string $param_class |
|
| 1383 | + * @param array $arguments |
|
| 1384 | + * @param mixed $index |
|
| 1385 | + * @return array |
|
| 1386 | + * @throws InvalidArgumentException |
|
| 1387 | + * @throws InvalidInterfaceException |
|
| 1388 | + * @throws InvalidDataTypeException |
|
| 1389 | + */ |
|
| 1390 | + protected function _resolve_dependency($class_name, $param_class, $arguments, $index) |
|
| 1391 | + { |
|
| 1392 | + $dependency = null; |
|
| 1393 | + // should dependency be loaded from cache ? |
|
| 1394 | + $cache_on = $this->_dependency_map->loading_strategy_for_class_dependency( |
|
| 1395 | + $class_name, |
|
| 1396 | + $param_class |
|
| 1397 | + ); |
|
| 1398 | + $cache_on = $cache_on !== EE_Dependency_Map::load_new_object; |
|
| 1399 | + // we might have a dependency... |
|
| 1400 | + // let's MAYBE try and find it in our cache if that's what's been requested |
|
| 1401 | + $cached_class = $cache_on |
|
| 1402 | + ? $this->_get_cached_class($param_class) |
|
| 1403 | + : null; |
|
| 1404 | + // and grab it if it exists |
|
| 1405 | + if ($cached_class instanceof $param_class) { |
|
| 1406 | + $dependency = $cached_class; |
|
| 1407 | + } elseif ($param_class !== $class_name) { |
|
| 1408 | + // obtain the loader method from the dependency map |
|
| 1409 | + $loader = $this->_dependency_map->class_loader($param_class); |
|
| 1410 | + // is loader a custom closure ? |
|
| 1411 | + if ($loader instanceof Closure) { |
|
| 1412 | + $dependency = $loader($arguments); |
|
| 1413 | + } else { |
|
| 1414 | + // set the cache on property for the recursive loading call |
|
| 1415 | + $this->_cache_on = $cache_on; |
|
| 1416 | + // if not, then let's try and load it via the registry |
|
| 1417 | + if ($loader && method_exists($this, $loader)) { |
|
| 1418 | + $dependency = $this->{$loader}($param_class); |
|
| 1419 | + } else { |
|
| 1420 | + $dependency = LoaderFactory::getLoader()->load( |
|
| 1421 | + $param_class, |
|
| 1422 | + array(), |
|
| 1423 | + $cache_on |
|
| 1424 | + ); |
|
| 1425 | + } |
|
| 1426 | + } |
|
| 1427 | + } |
|
| 1428 | + // did we successfully find the correct dependency ? |
|
| 1429 | + if ($dependency instanceof $param_class) { |
|
| 1430 | + // then let's inject it into the incoming array of arguments at the correct location |
|
| 1431 | + $arguments[ $index ] = $dependency; |
|
| 1432 | + } |
|
| 1433 | + return $arguments; |
|
| 1434 | + } |
|
| 1435 | + |
|
| 1436 | + |
|
| 1437 | + /** |
|
| 1438 | + * call any loader that's been registered in the EE_Dependency_Map::$_class_loaders array |
|
| 1439 | + * |
|
| 1440 | + * @param string $classname PLEASE NOTE: the class name needs to match what's registered |
|
| 1441 | + * in the EE_Dependency_Map::$_class_loaders array, |
|
| 1442 | + * including the class prefix, ie: "EE_", "EEM_", "EEH_", etc |
|
| 1443 | + * @param array $arguments |
|
| 1444 | + * @return object |
|
| 1445 | + */ |
|
| 1446 | + public static function factory($classname, $arguments = array()) |
|
| 1447 | + { |
|
| 1448 | + $loader = self::instance()->_dependency_map->class_loader($classname); |
|
| 1449 | + if ($loader instanceof Closure) { |
|
| 1450 | + return $loader($arguments); |
|
| 1451 | + } |
|
| 1452 | + if (method_exists(self::instance(), $loader)) { |
|
| 1453 | + return self::instance()->{$loader}($classname, $arguments); |
|
| 1454 | + } |
|
| 1455 | + return null; |
|
| 1456 | + } |
|
| 1457 | + |
|
| 1458 | + |
|
| 1459 | + /** |
|
| 1460 | + * Gets the addon by its class name |
|
| 1461 | + * |
|
| 1462 | + * @param string $class_name |
|
| 1463 | + * @return EE_Addon |
|
| 1464 | + */ |
|
| 1465 | + public function getAddon($class_name) |
|
| 1466 | + { |
|
| 1467 | + $class_name = str_replace('\\', '_', $class_name); |
|
| 1468 | + if (isset($this->addons->{$class_name})) { |
|
| 1469 | + return $this->addons->{$class_name}; |
|
| 1470 | + } else { |
|
| 1471 | + return null; |
|
| 1472 | + } |
|
| 1473 | + } |
|
| 1474 | + |
|
| 1475 | + |
|
| 1476 | + /** |
|
| 1477 | + * removes the addon from the internal cache |
|
| 1478 | + * |
|
| 1479 | + * @param string $class_name |
|
| 1480 | + * @return void |
|
| 1481 | + */ |
|
| 1482 | + public function removeAddon($class_name) |
|
| 1483 | + { |
|
| 1484 | + $class_name = str_replace('\\', '_', $class_name); |
|
| 1485 | + unset($this->addons->{$class_name}); |
|
| 1486 | + } |
|
| 1487 | + |
|
| 1488 | + |
|
| 1489 | + /** |
|
| 1490 | + * Gets the addon by its name/slug (not classname. For that, just |
|
| 1491 | + * use the get_addon() method above |
|
| 1492 | + * |
|
| 1493 | + * @param string $name |
|
| 1494 | + * @return EE_Addon |
|
| 1495 | + */ |
|
| 1496 | + public function get_addon_by_name($name) |
|
| 1497 | + { |
|
| 1498 | + foreach ($this->addons as $addon) { |
|
| 1499 | + if ($addon->name() === $name) { |
|
| 1500 | + return $addon; |
|
| 1501 | + } |
|
| 1502 | + } |
|
| 1503 | + return null; |
|
| 1504 | + } |
|
| 1505 | + |
|
| 1506 | + |
|
| 1507 | + /** |
|
| 1508 | + * Gets an array of all the registered addons, where the keys are their names. |
|
| 1509 | + * (ie, what each returns for their name() function) |
|
| 1510 | + * They're already available on EE_Registry::instance()->addons as properties, |
|
| 1511 | + * where each property's name is the addon's classname, |
|
| 1512 | + * So if you just want to get the addon by classname, |
|
| 1513 | + * OR use the get_addon() method above. |
|
| 1514 | + * PLEASE NOTE: |
|
| 1515 | + * addons with Fully Qualified Class Names |
|
| 1516 | + * have had the namespace separators converted to underscores, |
|
| 1517 | + * so a classname like Fully\Qualified\ClassName |
|
| 1518 | + * would have been converted to Fully_Qualified_ClassName |
|
| 1519 | + * |
|
| 1520 | + * @return EE_Addon[] where the KEYS are the addon's name() |
|
| 1521 | + */ |
|
| 1522 | + public function get_addons_by_name() |
|
| 1523 | + { |
|
| 1524 | + $addons = array(); |
|
| 1525 | + foreach ($this->addons as $addon) { |
|
| 1526 | + $addons[ $addon->name() ] = $addon; |
|
| 1527 | + } |
|
| 1528 | + return $addons; |
|
| 1529 | + } |
|
| 1530 | + |
|
| 1531 | + |
|
| 1532 | + /** |
|
| 1533 | + * Resets the specified model's instance AND makes sure EE_Registry doesn't keep |
|
| 1534 | + * a stale copy of it around |
|
| 1535 | + * |
|
| 1536 | + * @param string $model_name |
|
| 1537 | + * @return \EEM_Base |
|
| 1538 | + * @throws \EE_Error |
|
| 1539 | + */ |
|
| 1540 | + public function reset_model($model_name) |
|
| 1541 | + { |
|
| 1542 | + $model_class_name = strpos($model_name, 'EEM_') !== 0 |
|
| 1543 | + ? "EEM_{$model_name}" |
|
| 1544 | + : $model_name; |
|
| 1545 | + if (! isset($this->LIB->{$model_class_name}) || ! $this->LIB->{$model_class_name} instanceof EEM_Base) { |
|
| 1546 | + return null; |
|
| 1547 | + } |
|
| 1548 | + // get that model reset it and make sure we nuke the old reference to it |
|
| 1549 | + if ($this->LIB->{$model_class_name} instanceof $model_class_name |
|
| 1550 | + && is_callable( |
|
| 1551 | + array($model_class_name, 'reset') |
|
| 1552 | + )) { |
|
| 1553 | + $this->LIB->{$model_class_name} = $this->LIB->{$model_class_name}->reset(); |
|
| 1554 | + } else { |
|
| 1555 | + throw new EE_Error( |
|
| 1556 | + sprintf( |
|
| 1557 | + esc_html__('Model %s does not have a method "reset"', 'event_espresso'), |
|
| 1558 | + $model_name |
|
| 1559 | + ) |
|
| 1560 | + ); |
|
| 1561 | + } |
|
| 1562 | + return $this->LIB->{$model_class_name}; |
|
| 1563 | + } |
|
| 1564 | + |
|
| 1565 | + |
|
| 1566 | + /** |
|
| 1567 | + * Resets the registry. |
|
| 1568 | + * The criteria for what gets reset is based on what can be shared between sites on the same request when |
|
| 1569 | + * switch_to_blog is used in a multisite install. Here is a list of things that are NOT reset. |
|
| 1570 | + * - $_dependency_map |
|
| 1571 | + * - $_class_abbreviations |
|
| 1572 | + * - $NET_CFG (EE_Network_Config): The config is shared network wide so no need to reset. |
|
| 1573 | + * - $REQ: Still on the same request so no need to change. |
|
| 1574 | + * - $CAP: There is no site specific state in the EE_Capability class. |
|
| 1575 | + * - $SSN: Although ideally, the session should not be shared between site switches, we can't reset it because only |
|
| 1576 | + * one Session can be active in a single request. Resetting could resolve in "headers already sent" errors. |
|
| 1577 | + * - $addons: In multisite, the state of the addons is something controlled via hooks etc in a normal request. So |
|
| 1578 | + * for now, we won't reset the addons because it could break calls to an add-ons class/methods in the |
|
| 1579 | + * switch or on the restore. |
|
| 1580 | + * - $modules |
|
| 1581 | + * - $shortcodes |
|
| 1582 | + * - $widgets |
|
| 1583 | + * |
|
| 1584 | + * @param boolean $hard [deprecated] |
|
| 1585 | + * @param boolean $reinstantiate whether to create new instances of EE_Registry's singletons too, |
|
| 1586 | + * or just reset without re-instantiating (handy to set to FALSE if you're not |
|
| 1587 | + * sure if you CAN currently reinstantiate the singletons at the moment) |
|
| 1588 | + * @param bool $reset_models Defaults to true. When false, then the models are not reset. This is so |
|
| 1589 | + * client |
|
| 1590 | + * code instead can just change the model context to a different blog id if |
|
| 1591 | + * necessary |
|
| 1592 | + * @return EE_Registry |
|
| 1593 | + * @throws InvalidInterfaceException |
|
| 1594 | + * @throws InvalidDataTypeException |
|
| 1595 | + * @throws EE_Error |
|
| 1596 | + * @throws ReflectionException |
|
| 1597 | + * @throws InvalidArgumentException |
|
| 1598 | + */ |
|
| 1599 | + public static function reset($hard = false, $reinstantiate = true, $reset_models = true) |
|
| 1600 | + { |
|
| 1601 | + $instance = self::instance(); |
|
| 1602 | + $instance->_cache_on = true; |
|
| 1603 | + // reset some "special" classes |
|
| 1604 | + EEH_Activation::reset(); |
|
| 1605 | + $hard = apply_filters('FHEE__EE_Registry__reset__hard', $hard); |
|
| 1606 | + $instance->CFG = EE_Config::reset($hard, $reinstantiate); |
|
| 1607 | + $instance->CART = null; |
|
| 1608 | + $instance->MRM = null; |
|
| 1609 | + $instance->AssetsRegistry = LoaderFactory::getLoader()->getShared( |
|
| 1610 | + 'EventEspresso\core\services\assets\Registry' |
|
| 1611 | + ); |
|
| 1612 | + // messages reset |
|
| 1613 | + EED_Messages::reset(); |
|
| 1614 | + // handle of objects cached on LIB |
|
| 1615 | + foreach (array('LIB', 'modules') as $cache) { |
|
| 1616 | + foreach ($instance->{$cache} as $class_name => $class) { |
|
| 1617 | + if (self::_reset_and_unset_object($class, $reset_models)) { |
|
| 1618 | + unset($instance->{$cache}->{$class_name}); |
|
| 1619 | + } |
|
| 1620 | + } |
|
| 1621 | + } |
|
| 1622 | + return $instance; |
|
| 1623 | + } |
|
| 1624 | + |
|
| 1625 | + |
|
| 1626 | + /** |
|
| 1627 | + * if passed object implements ResettableInterface, then call it's reset() method |
|
| 1628 | + * if passed object implements InterminableInterface, then return false, |
|
| 1629 | + * to indicate that it should NOT be cleared from the Registry cache |
|
| 1630 | + * |
|
| 1631 | + * @param $object |
|
| 1632 | + * @param bool $reset_models |
|
| 1633 | + * @return bool returns true if cached object should be unset |
|
| 1634 | + */ |
|
| 1635 | + private static function _reset_and_unset_object($object, $reset_models) |
|
| 1636 | + { |
|
| 1637 | + if (! is_object($object)) { |
|
| 1638 | + // don't unset anything that's not an object |
|
| 1639 | + return false; |
|
| 1640 | + } |
|
| 1641 | + if ($object instanceof EED_Module) { |
|
| 1642 | + $object::reset(); |
|
| 1643 | + // don't unset modules |
|
| 1644 | + return false; |
|
| 1645 | + } |
|
| 1646 | + if ($object instanceof ResettableInterface) { |
|
| 1647 | + if ($object instanceof EEM_Base) { |
|
| 1648 | + if ($reset_models) { |
|
| 1649 | + $object->reset(); |
|
| 1650 | + return true; |
|
| 1651 | + } |
|
| 1652 | + return false; |
|
| 1653 | + } |
|
| 1654 | + $object->reset(); |
|
| 1655 | + return true; |
|
| 1656 | + } |
|
| 1657 | + if (! $object instanceof InterminableInterface) { |
|
| 1658 | + return true; |
|
| 1659 | + } |
|
| 1660 | + return false; |
|
| 1661 | + } |
|
| 1662 | + |
|
| 1663 | + |
|
| 1664 | + /** |
|
| 1665 | + * Gets all the custom post type models defined |
|
| 1666 | + * |
|
| 1667 | + * @return array keys are model "short names" (Eg "Event") and keys are classnames (eg "EEM_Event") |
|
| 1668 | + */ |
|
| 1669 | + public function cpt_models() |
|
| 1670 | + { |
|
| 1671 | + $cpt_models = array(); |
|
| 1672 | + foreach ($this->non_abstract_db_models as $short_name => $classname) { |
|
| 1673 | + if (is_subclass_of($classname, 'EEM_CPT_Base')) { |
|
| 1674 | + $cpt_models[ $short_name ] = $classname; |
|
| 1675 | + } |
|
| 1676 | + } |
|
| 1677 | + return $cpt_models; |
|
| 1678 | + } |
|
| 1679 | + |
|
| 1680 | + |
|
| 1681 | + /** |
|
| 1682 | + * @return \EE_Config |
|
| 1683 | + */ |
|
| 1684 | + public static function CFG() |
|
| 1685 | + { |
|
| 1686 | + return self::instance()->CFG; |
|
| 1687 | + } |
|
| 1688 | + |
|
| 1689 | + |
|
| 1690 | + /** |
|
| 1691 | + * @deprecated 4.9.62.p |
|
| 1692 | + * @param string $class_name |
|
| 1693 | + * @return ReflectionClass |
|
| 1694 | + * @throws ReflectionException |
|
| 1695 | + * @throws InvalidDataTypeException |
|
| 1696 | + */ |
|
| 1697 | + public function get_ReflectionClass($class_name) |
|
| 1698 | + { |
|
| 1699 | + return $this->mirror->getReflectionClass($class_name); |
|
| 1700 | + } |
|
| 1701 | 1701 | } |
@@ -193,7 +193,7 @@ discard block |
||
| 193 | 193 | ObjectIdentifier $object_identifier = null |
| 194 | 194 | ) { |
| 195 | 195 | // check if class object is instantiated |
| 196 | - if (! self::$_instance instanceof EE_Registry |
|
| 196 | + if ( ! self::$_instance instanceof EE_Registry |
|
| 197 | 197 | && $dependency_map instanceof EE_Dependency_Map |
| 198 | 198 | && $mirror instanceof Mirror |
| 199 | 199 | && $class_cache instanceof ClassInterfaceCache |
@@ -308,10 +308,10 @@ discard block |
||
| 308 | 308 | $i18n_js_strings = (array) self::$i18n_js_strings; |
| 309 | 309 | foreach ($i18n_js_strings as $key => $value) { |
| 310 | 310 | if (is_scalar($value)) { |
| 311 | - $i18n_js_strings[ $key ] = html_entity_decode((string) $value, ENT_QUOTES, 'UTF-8'); |
|
| 311 | + $i18n_js_strings[$key] = html_entity_decode((string) $value, ENT_QUOTES, 'UTF-8'); |
|
| 312 | 312 | } |
| 313 | 313 | } |
| 314 | - return '/* <![CDATA[ */ var eei18n = ' . wp_json_encode($i18n_js_strings) . '; /* ]]> */'; |
|
| 314 | + return '/* <![CDATA[ */ var eei18n = '.wp_json_encode($i18n_js_strings).'; /* ]]> */'; |
|
| 315 | 315 | } |
| 316 | 316 | |
| 317 | 317 | |
@@ -330,7 +330,7 @@ discard block |
||
| 330 | 330 | $module_class = get_class($module); |
| 331 | 331 | $this->modules->{$module_class} = $module; |
| 332 | 332 | } else { |
| 333 | - if (! class_exists('EE_Module_Request_Router', false)) { |
|
| 333 | + if ( ! class_exists('EE_Module_Request_Router', false)) { |
|
| 334 | 334 | $this->load_core('Module_Request_Router'); |
| 335 | 335 | } |
| 336 | 336 | EE_Module_Request_Router::module_factory($module); |
@@ -371,10 +371,10 @@ discard block |
||
| 371 | 371 | EE_CORE, |
| 372 | 372 | EE_ADMIN, |
| 373 | 373 | EE_CPTS, |
| 374 | - EE_CORE . 'data_migration_scripts' . DS, |
|
| 375 | - EE_CORE . 'capabilities' . DS, |
|
| 376 | - EE_CORE . 'request_stack' . DS, |
|
| 377 | - EE_CORE . 'middleware' . DS, |
|
| 374 | + EE_CORE.'data_migration_scripts'.DS, |
|
| 375 | + EE_CORE.'capabilities'.DS, |
|
| 376 | + EE_CORE.'request_stack'.DS, |
|
| 377 | + EE_CORE.'middleware'.DS, |
|
| 378 | 378 | ) |
| 379 | 379 | ); |
| 380 | 380 | // retrieve instantiated class |
@@ -409,7 +409,7 @@ discard block |
||
| 409 | 409 | $service_paths = apply_filters( |
| 410 | 410 | 'FHEE__EE_Registry__load_service__service_paths', |
| 411 | 411 | array( |
| 412 | - EE_CORE . 'services' . DS, |
|
| 412 | + EE_CORE.'services'.DS, |
|
| 413 | 413 | ) |
| 414 | 414 | ); |
| 415 | 415 | // retrieve instantiated class |
@@ -544,10 +544,10 @@ discard block |
||
| 544 | 544 | { |
| 545 | 545 | $paths = array( |
| 546 | 546 | EE_LIBRARIES, |
| 547 | - EE_LIBRARIES . 'messages' . DS, |
|
| 548 | - EE_LIBRARIES . 'shortcodes' . DS, |
|
| 549 | - EE_LIBRARIES . 'qtips' . DS, |
|
| 550 | - EE_LIBRARIES . 'payment_methods' . DS, |
|
| 547 | + EE_LIBRARIES.'messages'.DS, |
|
| 548 | + EE_LIBRARIES.'shortcodes'.DS, |
|
| 549 | + EE_LIBRARIES.'qtips'.DS, |
|
| 550 | + EE_LIBRARIES.'payment_methods'.DS, |
|
| 551 | 551 | ); |
| 552 | 552 | // retrieve instantiated class |
| 553 | 553 | return $this->_load( |
@@ -615,10 +615,10 @@ discard block |
||
| 615 | 615 | public function load_model_class($class_name, $arguments = array(), $load_only = true) |
| 616 | 616 | { |
| 617 | 617 | $paths = array( |
| 618 | - EE_MODELS . 'fields' . DS, |
|
| 619 | - EE_MODELS . 'helpers' . DS, |
|
| 620 | - EE_MODELS . 'relations' . DS, |
|
| 621 | - EE_MODELS . 'strategies' . DS, |
|
| 618 | + EE_MODELS.'fields'.DS, |
|
| 619 | + EE_MODELS.'helpers'.DS, |
|
| 620 | + EE_MODELS.'relations'.DS, |
|
| 621 | + EE_MODELS.'strategies'.DS, |
|
| 622 | 622 | ); |
| 623 | 623 | // retrieve instantiated class |
| 624 | 624 | return $this->_load( |
@@ -642,7 +642,7 @@ discard block |
||
| 642 | 642 | */ |
| 643 | 643 | public function is_model_name($model_name) |
| 644 | 644 | { |
| 645 | - return isset($this->models[ $model_name ]); |
|
| 645 | + return isset($this->models[$model_name]); |
|
| 646 | 646 | } |
| 647 | 647 | |
| 648 | 648 | |
@@ -763,7 +763,7 @@ discard block |
||
| 763 | 763 | return $cached_class; |
| 764 | 764 | } |
| 765 | 765 | }// obtain the loader method from the dependency map |
| 766 | - $loader = $this->_dependency_map->class_loader($class_name);// instantiate the requested object |
|
| 766 | + $loader = $this->_dependency_map->class_loader($class_name); // instantiate the requested object |
|
| 767 | 767 | if ($loader instanceof Closure) { |
| 768 | 768 | $class_obj = $loader($arguments); |
| 769 | 769 | } else { |
@@ -805,7 +805,7 @@ discard block |
||
| 805 | 805 | case 1: |
| 806 | 806 | // if it's a FQCN then maybe the class is registered with a preceding \ |
| 807 | 807 | $class_name = strpos($class_name, '\\') !== false |
| 808 | - ? '\\' . ltrim($class_name, '\\') |
|
| 808 | + ? '\\'.ltrim($class_name, '\\') |
|
| 809 | 809 | : $class_name; |
| 810 | 810 | break; |
| 811 | 811 | case 2: |
@@ -859,11 +859,11 @@ discard block |
||
| 859 | 859 | // strip php file extension |
| 860 | 860 | $class_name = str_replace('.php', '', trim($class_name)); |
| 861 | 861 | // does the class have a prefix ? |
| 862 | - if (! empty($class_prefix) && $class_prefix !== 'addon') { |
|
| 862 | + if ( ! empty($class_prefix) && $class_prefix !== 'addon') { |
|
| 863 | 863 | // make sure $class_prefix is uppercase |
| 864 | 864 | $class_prefix = strtoupper(trim($class_prefix)); |
| 865 | 865 | // add class prefix ONCE!!! |
| 866 | - $class_name = $class_prefix . str_replace($class_prefix, '', $class_name); |
|
| 866 | + $class_name = $class_prefix.str_replace($class_prefix, '', $class_name); |
|
| 867 | 867 | } |
| 868 | 868 | $class_name = $this->class_cache->getFqnForAlias($class_name); |
| 869 | 869 | $class_exists = class_exists($class_name, false); |
@@ -883,7 +883,7 @@ discard block |
||
| 883 | 883 | } |
| 884 | 884 | } |
| 885 | 885 | // if the class doesn't already exist.. then we need to try and find the file and load it |
| 886 | - if (! $class_exists) { |
|
| 886 | + if ( ! $class_exists) { |
|
| 887 | 887 | // get full path to file |
| 888 | 888 | $path = $this->_resolve_path($class_name, $type, $file_paths); |
| 889 | 889 | // load the file |
@@ -894,7 +894,7 @@ discard block |
||
| 894 | 894 | return $loaded; |
| 895 | 895 | } |
| 896 | 896 | // if an object was expected but loading failed, then return nothing |
| 897 | - if (! $loaded) { |
|
| 897 | + if ( ! $loaded) { |
|
| 898 | 898 | return null; |
| 899 | 899 | } |
| 900 | 900 | } |
@@ -922,8 +922,8 @@ discard block |
||
| 922 | 922 | */ |
| 923 | 923 | protected function get_class_abbreviation($class_name, $default = 'FANCY_BATMAN_PANTS') |
| 924 | 924 | { |
| 925 | - return isset($this->_class_abbreviations[ $class_name ]) |
|
| 926 | - ? $this->_class_abbreviations[ $class_name ] |
|
| 925 | + return isset($this->_class_abbreviations[$class_name]) |
|
| 926 | + ? $this->_class_abbreviations[$class_name] |
|
| 927 | 927 | : $default; |
| 928 | 928 | } |
| 929 | 929 | |
@@ -1056,7 +1056,7 @@ discard block |
||
| 1056 | 1056 | $this->addons->{$class_name} = $class_obj; |
| 1057 | 1057 | return; |
| 1058 | 1058 | } |
| 1059 | - if (! $from_db) { |
|
| 1059 | + if ( ! $from_db) { |
|
| 1060 | 1060 | $class_name = $this->object_identifier->getIdentifier($class_name, $arguments); |
| 1061 | 1061 | $this->LIB->{$class_name} = $class_obj; |
| 1062 | 1062 | } |
@@ -1087,13 +1087,13 @@ discard block |
||
| 1087 | 1087 | : EE_CLASSES; |
| 1088 | 1088 | // prep file type |
| 1089 | 1089 | $type = ! empty($type) |
| 1090 | - ? trim($type, '.') . '.' |
|
| 1090 | + ? trim($type, '.').'.' |
|
| 1091 | 1091 | : ''; |
| 1092 | 1092 | // build full file path |
| 1093 | - $file_paths[ $key ] = rtrim($file_path, DS) . DS . $class_name . '.' . $type . 'php'; |
|
| 1093 | + $file_paths[$key] = rtrim($file_path, DS).DS.$class_name.'.'.$type.'php'; |
|
| 1094 | 1094 | // does the file exist and can be read ? |
| 1095 | - if (is_readable($file_paths[ $key ])) { |
|
| 1096 | - return $file_paths[ $key ]; |
|
| 1095 | + if (is_readable($file_paths[$key])) { |
|
| 1096 | + return $file_paths[$key]; |
|
| 1097 | 1097 | } |
| 1098 | 1098 | } |
| 1099 | 1099 | return false; |
@@ -1118,7 +1118,7 @@ discard block |
||
| 1118 | 1118 | // don't give up! you gotta... |
| 1119 | 1119 | try { |
| 1120 | 1120 | // does the file exist and can it be read ? |
| 1121 | - if (! $path) { |
|
| 1121 | + if ( ! $path) { |
|
| 1122 | 1122 | // just in case the file has already been autoloaded, |
| 1123 | 1123 | // but discrepancies in the naming schema are preventing it from |
| 1124 | 1124 | // being loaded via one of the EE_Registry::load_*() methods, |
@@ -1136,7 +1136,7 @@ discard block |
||
| 1136 | 1136 | ), |
| 1137 | 1137 | trim($type, '.'), |
| 1138 | 1138 | $class_name, |
| 1139 | - '<br />' . implode(',<br />', $file_paths) |
|
| 1139 | + '<br />'.implode(',<br />', $file_paths) |
|
| 1140 | 1140 | ) |
| 1141 | 1141 | ); |
| 1142 | 1142 | } |
@@ -1179,8 +1179,8 @@ discard block |
||
| 1179 | 1179 | $legacy_parent_class_map = array( |
| 1180 | 1180 | 'EE_Payment_Processor' => 'core/business/EE_Processor_Base.class.php', |
| 1181 | 1181 | ); |
| 1182 | - if (isset($legacy_parent_class_map[ $class_name ])) { |
|
| 1183 | - require_once EE_PLUGIN_DIR_PATH . $legacy_parent_class_map[ $class_name ]; |
|
| 1182 | + if (isset($legacy_parent_class_map[$class_name])) { |
|
| 1183 | + require_once EE_PLUGIN_DIR_PATH.$legacy_parent_class_map[$class_name]; |
|
| 1184 | 1184 | } |
| 1185 | 1185 | } catch (Exception $exception) { |
| 1186 | 1186 | } |
@@ -1312,7 +1312,7 @@ discard block |
||
| 1312 | 1312 | // let's examine the constructor |
| 1313 | 1313 | $constructor = $this->mirror->getConstructorFromReflection($reflector); |
| 1314 | 1314 | // whu? huh? nothing? |
| 1315 | - if (! $constructor) { |
|
| 1315 | + if ( ! $constructor) { |
|
| 1316 | 1316 | return $arguments; |
| 1317 | 1317 | } |
| 1318 | 1318 | // get constructor parameters |
@@ -1329,7 +1329,7 @@ discard block |
||
| 1329 | 1329 | // let's try to derive the classname from what we have now |
| 1330 | 1330 | // and hope that the property var name is close to the class name |
| 1331 | 1331 | $param_class = $param->getName(); |
| 1332 | - $param_class = str_replace('_', ' ',$param_class); |
|
| 1332 | + $param_class = str_replace('_', ' ', $param_class); |
|
| 1333 | 1333 | $param_class = ucwords($param_class); |
| 1334 | 1334 | $param_class = str_replace(' ', '_', $param_class); |
| 1335 | 1335 | } |
@@ -1341,15 +1341,15 @@ discard block |
||
| 1341 | 1341 | $param_class === null |
| 1342 | 1342 | // and something already exists in the incoming arguments for this param |
| 1343 | 1343 | && array_key_exists($index, $argument_keys) |
| 1344 | - && array_key_exists($argument_keys[ $index ], $arguments) |
|
| 1344 | + && array_key_exists($argument_keys[$index], $arguments) |
|
| 1345 | 1345 | ) { |
| 1346 | 1346 | // so let's skip this argument and move on to the next |
| 1347 | 1347 | continue; |
| 1348 | 1348 | } |
| 1349 | 1349 | if (// parameter is type hinted as a class, exists as an incoming argument, AND it's the correct class |
| 1350 | 1350 | $param_class !== null |
| 1351 | - && isset($argument_keys[ $index ], $arguments[ $argument_keys[ $index ] ]) |
|
| 1352 | - && $arguments[ $argument_keys[ $index ] ] instanceof $param_class |
|
| 1351 | + && isset($argument_keys[$index], $arguments[$argument_keys[$index]]) |
|
| 1352 | + && $arguments[$argument_keys[$index]] instanceof $param_class |
|
| 1353 | 1353 | ) { |
| 1354 | 1354 | // skip this argument and move on to the next |
| 1355 | 1355 | continue; |
@@ -1365,8 +1365,8 @@ discard block |
||
| 1365 | 1365 | $index |
| 1366 | 1366 | ); |
| 1367 | 1367 | } |
| 1368 | - if (empty($arguments[ $index ])) { |
|
| 1369 | - $arguments[ $index ] = $this->mirror->getParameterDefaultValue( |
|
| 1368 | + if (empty($arguments[$index])) { |
|
| 1369 | + $arguments[$index] = $this->mirror->getParameterDefaultValue( |
|
| 1370 | 1370 | $param, |
| 1371 | 1371 | $class_name, |
| 1372 | 1372 | $index |
@@ -1428,7 +1428,7 @@ discard block |
||
| 1428 | 1428 | // did we successfully find the correct dependency ? |
| 1429 | 1429 | if ($dependency instanceof $param_class) { |
| 1430 | 1430 | // then let's inject it into the incoming array of arguments at the correct location |
| 1431 | - $arguments[ $index ] = $dependency; |
|
| 1431 | + $arguments[$index] = $dependency; |
|
| 1432 | 1432 | } |
| 1433 | 1433 | return $arguments; |
| 1434 | 1434 | } |
@@ -1523,7 +1523,7 @@ discard block |
||
| 1523 | 1523 | { |
| 1524 | 1524 | $addons = array(); |
| 1525 | 1525 | foreach ($this->addons as $addon) { |
| 1526 | - $addons[ $addon->name() ] = $addon; |
|
| 1526 | + $addons[$addon->name()] = $addon; |
|
| 1527 | 1527 | } |
| 1528 | 1528 | return $addons; |
| 1529 | 1529 | } |
@@ -1542,7 +1542,7 @@ discard block |
||
| 1542 | 1542 | $model_class_name = strpos($model_name, 'EEM_') !== 0 |
| 1543 | 1543 | ? "EEM_{$model_name}" |
| 1544 | 1544 | : $model_name; |
| 1545 | - if (! isset($this->LIB->{$model_class_name}) || ! $this->LIB->{$model_class_name} instanceof EEM_Base) { |
|
| 1545 | + if ( ! isset($this->LIB->{$model_class_name}) || ! $this->LIB->{$model_class_name} instanceof EEM_Base) { |
|
| 1546 | 1546 | return null; |
| 1547 | 1547 | } |
| 1548 | 1548 | // get that model reset it and make sure we nuke the old reference to it |
@@ -1634,7 +1634,7 @@ discard block |
||
| 1634 | 1634 | */ |
| 1635 | 1635 | private static function _reset_and_unset_object($object, $reset_models) |
| 1636 | 1636 | { |
| 1637 | - if (! is_object($object)) { |
|
| 1637 | + if ( ! is_object($object)) { |
|
| 1638 | 1638 | // don't unset anything that's not an object |
| 1639 | 1639 | return false; |
| 1640 | 1640 | } |
@@ -1654,7 +1654,7 @@ discard block |
||
| 1654 | 1654 | $object->reset(); |
| 1655 | 1655 | return true; |
| 1656 | 1656 | } |
| 1657 | - if (! $object instanceof InterminableInterface) { |
|
| 1657 | + if ( ! $object instanceof InterminableInterface) { |
|
| 1658 | 1658 | return true; |
| 1659 | 1659 | } |
| 1660 | 1660 | return false; |
@@ -1671,7 +1671,7 @@ discard block |
||
| 1671 | 1671 | $cpt_models = array(); |
| 1672 | 1672 | foreach ($this->non_abstract_db_models as $short_name => $classname) { |
| 1673 | 1673 | if (is_subclass_of($classname, 'EEM_CPT_Base')) { |
| 1674 | - $cpt_models[ $short_name ] = $classname; |
|
| 1674 | + $cpt_models[$short_name] = $classname; |
|
| 1675 | 1675 | } |
| 1676 | 1676 | } |
| 1677 | 1677 | return $cpt_models; |
@@ -17,165 +17,165 @@ |
||
| 17 | 17 | class ClassInterfaceCache |
| 18 | 18 | { |
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * array of interfaces indexed by FQCNs where values are arrays of interface FQNs |
|
| 22 | - * |
|
| 23 | - * @var string[][] $interfaces |
|
| 24 | - */ |
|
| 25 | - private $interfaces = array(); |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * @type string[][] $aliases |
|
| 29 | - */ |
|
| 30 | - protected $aliases = array(); |
|
| 31 | - |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * @param string $fqn |
|
| 35 | - * @return string |
|
| 36 | - */ |
|
| 37 | - public function getFqn($fqn) |
|
| 38 | - { |
|
| 39 | - $fqn = $fqn instanceof FullyQualifiedName ? $fqn->string() : $fqn; |
|
| 40 | - return ltrim($fqn, '\\'); |
|
| 41 | - } |
|
| 42 | - |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * @param string $fqn |
|
| 46 | - * @return array |
|
| 47 | - */ |
|
| 48 | - public function getInterfaces($fqn) |
|
| 49 | - { |
|
| 50 | - $fqn = $this->getFqn($fqn); |
|
| 51 | - // have we already seen this FQCN ? |
|
| 52 | - if (! array_key_exists($fqn, $this->interfaces)) { |
|
| 53 | - $this->interfaces[ $fqn ] = array(); |
|
| 54 | - if (class_exists($fqn)) { |
|
| 55 | - $this->interfaces[ $fqn ] = class_implements($fqn, false); |
|
| 56 | - $this->interfaces[ $fqn ] = $this->interfaces[ $fqn ] !== false |
|
| 57 | - ? $this->interfaces[ $fqn ] |
|
| 58 | - : array(); |
|
| 59 | - } |
|
| 60 | - } |
|
| 61 | - return $this->interfaces[ $fqn ]; |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * @param string $fqn |
|
| 67 | - * @param string $interface |
|
| 68 | - * @return bool |
|
| 69 | - */ |
|
| 70 | - public function hasInterface($fqn, $interface) |
|
| 71 | - { |
|
| 72 | - $fqn = $this->getFqn($fqn); |
|
| 73 | - $interfaces = $this->getInterfaces($fqn); |
|
| 74 | - return in_array($interface, $interfaces, true); |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * adds an alias for a classname |
|
| 80 | - * |
|
| 81 | - * @param string $fqn the class name that should be used (concrete class to replace interface) |
|
| 82 | - * @param string $alias the class name that would be type hinted for (abstract parent or interface) |
|
| 83 | - * @param string $for_class the class that has the dependency (is type hinting for the interface) |
|
| 84 | - * @throws InvalidAliasException |
|
| 85 | - */ |
|
| 86 | - public function addAlias($fqn, $alias, $for_class = '') |
|
| 87 | - { |
|
| 88 | - $fqn = $this->getFqn($fqn); |
|
| 89 | - $alias = $this->getFqn($alias); |
|
| 90 | - if (strpos($alias, '\\') !== false && ! is_subclass_of($fqn, $alias)) { |
|
| 91 | - throw new InvalidAliasException($fqn, $alias); |
|
| 92 | - } |
|
| 93 | - // are we adding an alias for a specific class? |
|
| 94 | - if ($for_class !== '') { |
|
| 95 | - // make sure it's set up as an array |
|
| 96 | - if (! isset($this->aliases[ $for_class ])) { |
|
| 97 | - $this->aliases[ $for_class ] = array(); |
|
| 98 | - } |
|
| 99 | - $this->aliases[ $for_class ][ $alias ] = $fqn; |
|
| 100 | - return; |
|
| 101 | - } |
|
| 102 | - $this->aliases[ $alias ] = $fqn; |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - |
|
| 106 | - /** |
|
| 107 | - * returns TRUE if the provided FQN is an alias |
|
| 108 | - * |
|
| 109 | - * @param string $fqn |
|
| 110 | - * @param string $for_class |
|
| 111 | - * @return bool |
|
| 112 | - */ |
|
| 113 | - public function isAlias($fqn = '', $for_class = '') |
|
| 114 | - { |
|
| 115 | - $fqn = $this->getFqn($fqn); |
|
| 116 | - if ($this->isAliasForClass($fqn, $for_class)) { |
|
| 117 | - return true; |
|
| 118 | - } |
|
| 119 | - if ($this->isDirectAlias($fqn)) { |
|
| 120 | - return true; |
|
| 121 | - } |
|
| 122 | - return false; |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * returns TRUE if the provided FQN is an alias |
|
| 128 | - * |
|
| 129 | - * @param string $fqn |
|
| 130 | - * @return bool |
|
| 131 | - */ |
|
| 132 | - protected function isDirectAlias($fqn = '') |
|
| 133 | - { |
|
| 134 | - return isset($this->aliases[ (string) $fqn ]) && ! is_array($this->aliases[ (string) $fqn ]); |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * returns TRUE if the provided FQN is an alias for the specified class |
|
| 140 | - * |
|
| 141 | - * @param string $fqn |
|
| 142 | - * @param string $for_class |
|
| 143 | - * @return bool |
|
| 144 | - */ |
|
| 145 | - protected function isAliasForClass($fqn = '', $for_class = '') |
|
| 146 | - { |
|
| 147 | - return ( |
|
| 148 | - $for_class !== '' |
|
| 149 | - && isset($this->aliases[ (string) $for_class ][ (string) $fqn ]) |
|
| 150 | - ); |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - |
|
| 154 | - /** |
|
| 155 | - * returns FQN for provided alias if one exists, otherwise returns the original FQN |
|
| 156 | - * functions recursively, so that multiple aliases can be used to drill down to a FQN |
|
| 157 | - * for example: |
|
| 158 | - * if the following two entries were added to the aliases array: |
|
| 159 | - * array( |
|
| 160 | - * 'interface_alias' => 'some\namespace\interface' |
|
| 161 | - * 'some\namespace\interface' => 'some\namespace\classname' |
|
| 162 | - * ) |
|
| 163 | - * then one could use Loader::getNew( 'interface_alias' ) |
|
| 164 | - * to load an instance of 'some\namespace\classname' |
|
| 165 | - * |
|
| 166 | - * @param string $alias |
|
| 167 | - * @param string $for_class |
|
| 168 | - * @return string |
|
| 169 | - */ |
|
| 170 | - public function getFqnForAlias($alias = '', $for_class = '') |
|
| 171 | - { |
|
| 172 | - $alias = $this->getFqn($alias); |
|
| 173 | - if ($this->isAliasForClass($alias, $for_class)) { |
|
| 174 | - return $this->getFqnForAlias($this->aliases[ (string) $for_class ][ (string) $alias ], $for_class); |
|
| 175 | - } |
|
| 176 | - if ($this->isDirectAlias($alias)) { |
|
| 177 | - return $this->getFqnForAlias($this->aliases[ (string) $alias ], ''); |
|
| 178 | - } |
|
| 179 | - return $alias; |
|
| 180 | - } |
|
| 20 | + /** |
|
| 21 | + * array of interfaces indexed by FQCNs where values are arrays of interface FQNs |
|
| 22 | + * |
|
| 23 | + * @var string[][] $interfaces |
|
| 24 | + */ |
|
| 25 | + private $interfaces = array(); |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * @type string[][] $aliases |
|
| 29 | + */ |
|
| 30 | + protected $aliases = array(); |
|
| 31 | + |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * @param string $fqn |
|
| 35 | + * @return string |
|
| 36 | + */ |
|
| 37 | + public function getFqn($fqn) |
|
| 38 | + { |
|
| 39 | + $fqn = $fqn instanceof FullyQualifiedName ? $fqn->string() : $fqn; |
|
| 40 | + return ltrim($fqn, '\\'); |
|
| 41 | + } |
|
| 42 | + |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * @param string $fqn |
|
| 46 | + * @return array |
|
| 47 | + */ |
|
| 48 | + public function getInterfaces($fqn) |
|
| 49 | + { |
|
| 50 | + $fqn = $this->getFqn($fqn); |
|
| 51 | + // have we already seen this FQCN ? |
|
| 52 | + if (! array_key_exists($fqn, $this->interfaces)) { |
|
| 53 | + $this->interfaces[ $fqn ] = array(); |
|
| 54 | + if (class_exists($fqn)) { |
|
| 55 | + $this->interfaces[ $fqn ] = class_implements($fqn, false); |
|
| 56 | + $this->interfaces[ $fqn ] = $this->interfaces[ $fqn ] !== false |
|
| 57 | + ? $this->interfaces[ $fqn ] |
|
| 58 | + : array(); |
|
| 59 | + } |
|
| 60 | + } |
|
| 61 | + return $this->interfaces[ $fqn ]; |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * @param string $fqn |
|
| 67 | + * @param string $interface |
|
| 68 | + * @return bool |
|
| 69 | + */ |
|
| 70 | + public function hasInterface($fqn, $interface) |
|
| 71 | + { |
|
| 72 | + $fqn = $this->getFqn($fqn); |
|
| 73 | + $interfaces = $this->getInterfaces($fqn); |
|
| 74 | + return in_array($interface, $interfaces, true); |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * adds an alias for a classname |
|
| 80 | + * |
|
| 81 | + * @param string $fqn the class name that should be used (concrete class to replace interface) |
|
| 82 | + * @param string $alias the class name that would be type hinted for (abstract parent or interface) |
|
| 83 | + * @param string $for_class the class that has the dependency (is type hinting for the interface) |
|
| 84 | + * @throws InvalidAliasException |
|
| 85 | + */ |
|
| 86 | + public function addAlias($fqn, $alias, $for_class = '') |
|
| 87 | + { |
|
| 88 | + $fqn = $this->getFqn($fqn); |
|
| 89 | + $alias = $this->getFqn($alias); |
|
| 90 | + if (strpos($alias, '\\') !== false && ! is_subclass_of($fqn, $alias)) { |
|
| 91 | + throw new InvalidAliasException($fqn, $alias); |
|
| 92 | + } |
|
| 93 | + // are we adding an alias for a specific class? |
|
| 94 | + if ($for_class !== '') { |
|
| 95 | + // make sure it's set up as an array |
|
| 96 | + if (! isset($this->aliases[ $for_class ])) { |
|
| 97 | + $this->aliases[ $for_class ] = array(); |
|
| 98 | + } |
|
| 99 | + $this->aliases[ $for_class ][ $alias ] = $fqn; |
|
| 100 | + return; |
|
| 101 | + } |
|
| 102 | + $this->aliases[ $alias ] = $fqn; |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + |
|
| 106 | + /** |
|
| 107 | + * returns TRUE if the provided FQN is an alias |
|
| 108 | + * |
|
| 109 | + * @param string $fqn |
|
| 110 | + * @param string $for_class |
|
| 111 | + * @return bool |
|
| 112 | + */ |
|
| 113 | + public function isAlias($fqn = '', $for_class = '') |
|
| 114 | + { |
|
| 115 | + $fqn = $this->getFqn($fqn); |
|
| 116 | + if ($this->isAliasForClass($fqn, $for_class)) { |
|
| 117 | + return true; |
|
| 118 | + } |
|
| 119 | + if ($this->isDirectAlias($fqn)) { |
|
| 120 | + return true; |
|
| 121 | + } |
|
| 122 | + return false; |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * returns TRUE if the provided FQN is an alias |
|
| 128 | + * |
|
| 129 | + * @param string $fqn |
|
| 130 | + * @return bool |
|
| 131 | + */ |
|
| 132 | + protected function isDirectAlias($fqn = '') |
|
| 133 | + { |
|
| 134 | + return isset($this->aliases[ (string) $fqn ]) && ! is_array($this->aliases[ (string) $fqn ]); |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * returns TRUE if the provided FQN is an alias for the specified class |
|
| 140 | + * |
|
| 141 | + * @param string $fqn |
|
| 142 | + * @param string $for_class |
|
| 143 | + * @return bool |
|
| 144 | + */ |
|
| 145 | + protected function isAliasForClass($fqn = '', $for_class = '') |
|
| 146 | + { |
|
| 147 | + return ( |
|
| 148 | + $for_class !== '' |
|
| 149 | + && isset($this->aliases[ (string) $for_class ][ (string) $fqn ]) |
|
| 150 | + ); |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + |
|
| 154 | + /** |
|
| 155 | + * returns FQN for provided alias if one exists, otherwise returns the original FQN |
|
| 156 | + * functions recursively, so that multiple aliases can be used to drill down to a FQN |
|
| 157 | + * for example: |
|
| 158 | + * if the following two entries were added to the aliases array: |
|
| 159 | + * array( |
|
| 160 | + * 'interface_alias' => 'some\namespace\interface' |
|
| 161 | + * 'some\namespace\interface' => 'some\namespace\classname' |
|
| 162 | + * ) |
|
| 163 | + * then one could use Loader::getNew( 'interface_alias' ) |
|
| 164 | + * to load an instance of 'some\namespace\classname' |
|
| 165 | + * |
|
| 166 | + * @param string $alias |
|
| 167 | + * @param string $for_class |
|
| 168 | + * @return string |
|
| 169 | + */ |
|
| 170 | + public function getFqnForAlias($alias = '', $for_class = '') |
|
| 171 | + { |
|
| 172 | + $alias = $this->getFqn($alias); |
|
| 173 | + if ($this->isAliasForClass($alias, $for_class)) { |
|
| 174 | + return $this->getFqnForAlias($this->aliases[ (string) $for_class ][ (string) $alias ], $for_class); |
|
| 175 | + } |
|
| 176 | + if ($this->isDirectAlias($alias)) { |
|
| 177 | + return $this->getFqnForAlias($this->aliases[ (string) $alias ], ''); |
|
| 178 | + } |
|
| 179 | + return $alias; |
|
| 180 | + } |
|
| 181 | 181 | } |
@@ -20,1093 +20,1093 @@ |
||
| 20 | 20 | class EE_Dependency_Map |
| 21 | 21 | { |
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * This means that the requested class dependency is not present in the dependency map |
|
| 25 | - */ |
|
| 26 | - const not_registered = 0; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * This instructs class loaders to ALWAYS return a newly instantiated object for the requested class. |
|
| 30 | - */ |
|
| 31 | - const load_new_object = 1; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * This instructs class loaders to return a previously instantiated and cached object for the requested class. |
|
| 35 | - * IF a previously instantiated object does not exist, a new one will be created and added to the cache. |
|
| 36 | - */ |
|
| 37 | - const load_from_cache = 2; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * When registering a dependency, |
|
| 41 | - * this indicates to keep any existing dependencies that already exist, |
|
| 42 | - * and simply discard any new dependencies declared in the incoming data |
|
| 43 | - */ |
|
| 44 | - const KEEP_EXISTING_DEPENDENCIES = 0; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * When registering a dependency, |
|
| 48 | - * this indicates to overwrite any existing dependencies that already exist using the incoming data |
|
| 49 | - */ |
|
| 50 | - const OVERWRITE_DEPENDENCIES = 1; |
|
| 51 | - |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * @type EE_Dependency_Map $_instance |
|
| 55 | - */ |
|
| 56 | - protected static $_instance; |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * @var ClassInterfaceCache $class_cache |
|
| 60 | - */ |
|
| 61 | - private $class_cache; |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * @type RequestInterface $request |
|
| 65 | - */ |
|
| 66 | - protected $request; |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * @type LegacyRequestInterface $legacy_request |
|
| 70 | - */ |
|
| 71 | - protected $legacy_request; |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * @type ResponseInterface $response |
|
| 75 | - */ |
|
| 76 | - protected $response; |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * @type LoaderInterface $loader |
|
| 80 | - */ |
|
| 81 | - protected $loader; |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * @type array $_dependency_map |
|
| 85 | - */ |
|
| 86 | - protected $_dependency_map = array(); |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * @type array $_class_loaders |
|
| 90 | - */ |
|
| 91 | - protected $_class_loaders = array(); |
|
| 92 | - |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * EE_Dependency_Map constructor. |
|
| 96 | - * |
|
| 97 | - * @param ClassInterfaceCache $class_cache |
|
| 98 | - */ |
|
| 99 | - protected function __construct(ClassInterfaceCache $class_cache) |
|
| 100 | - { |
|
| 101 | - $this->class_cache = $class_cache; |
|
| 102 | - do_action('EE_Dependency_Map____construct', $this); |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - |
|
| 106 | - /** |
|
| 107 | - * @return void |
|
| 108 | - */ |
|
| 109 | - public function initialize() |
|
| 110 | - { |
|
| 111 | - $this->_register_core_dependencies(); |
|
| 112 | - $this->_register_core_class_loaders(); |
|
| 113 | - $this->_register_core_aliases(); |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * @singleton method used to instantiate class object |
|
| 119 | - * @param ClassInterfaceCache|null $class_cache |
|
| 120 | - * @return EE_Dependency_Map |
|
| 121 | - */ |
|
| 122 | - public static function instance(ClassInterfaceCache $class_cache = null) |
|
| 123 | - { |
|
| 124 | - // check if class object is instantiated, and instantiated properly |
|
| 125 | - if (! self::$_instance instanceof EE_Dependency_Map |
|
| 126 | - && $class_cache instanceof ClassInterfaceCache |
|
| 127 | - ) { |
|
| 128 | - self::$_instance = new EE_Dependency_Map($class_cache); |
|
| 129 | - } |
|
| 130 | - return self::$_instance; |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - |
|
| 134 | - /** |
|
| 135 | - * @param RequestInterface $request |
|
| 136 | - */ |
|
| 137 | - public function setRequest(RequestInterface $request) |
|
| 138 | - { |
|
| 139 | - $this->request = $request; |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - |
|
| 143 | - /** |
|
| 144 | - * @param LegacyRequestInterface $legacy_request |
|
| 145 | - */ |
|
| 146 | - public function setLegacyRequest(LegacyRequestInterface $legacy_request) |
|
| 147 | - { |
|
| 148 | - $this->legacy_request = $legacy_request; |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - |
|
| 152 | - /** |
|
| 153 | - * @param ResponseInterface $response |
|
| 154 | - */ |
|
| 155 | - public function setResponse(ResponseInterface $response) |
|
| 156 | - { |
|
| 157 | - $this->response = $response; |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - |
|
| 161 | - /** |
|
| 162 | - * @param LoaderInterface $loader |
|
| 163 | - */ |
|
| 164 | - public function setLoader(LoaderInterface $loader) |
|
| 165 | - { |
|
| 166 | - $this->loader = $loader; |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - |
|
| 170 | - /** |
|
| 171 | - * @param string $class |
|
| 172 | - * @param array $dependencies |
|
| 173 | - * @param int $overwrite |
|
| 174 | - * @return bool |
|
| 175 | - */ |
|
| 176 | - public static function register_dependencies( |
|
| 177 | - $class, |
|
| 178 | - array $dependencies, |
|
| 179 | - $overwrite = EE_Dependency_Map::KEEP_EXISTING_DEPENDENCIES |
|
| 180 | - ) { |
|
| 181 | - return self::$_instance->registerDependencies($class, $dependencies, $overwrite); |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - |
|
| 185 | - /** |
|
| 186 | - * Assigns an array of class names and corresponding load sources (new or cached) |
|
| 187 | - * to the class specified by the first parameter. |
|
| 188 | - * IMPORTANT !!! |
|
| 189 | - * The order of elements in the incoming $dependencies array MUST match |
|
| 190 | - * the order of the constructor parameters for the class in question. |
|
| 191 | - * This is especially important when overriding any existing dependencies that are registered. |
|
| 192 | - * the third parameter controls whether any duplicate dependencies are overwritten or not. |
|
| 193 | - * |
|
| 194 | - * @param string $class |
|
| 195 | - * @param array $dependencies |
|
| 196 | - * @param int $overwrite |
|
| 197 | - * @return bool |
|
| 198 | - */ |
|
| 199 | - public function registerDependencies( |
|
| 200 | - $class, |
|
| 201 | - array $dependencies, |
|
| 202 | - $overwrite = EE_Dependency_Map::KEEP_EXISTING_DEPENDENCIES |
|
| 203 | - ) { |
|
| 204 | - $class = trim($class, '\\'); |
|
| 205 | - $registered = false; |
|
| 206 | - if (empty(self::$_instance->_dependency_map[ $class ])) { |
|
| 207 | - self::$_instance->_dependency_map[ $class ] = array(); |
|
| 208 | - } |
|
| 209 | - // we need to make sure that any aliases used when registering a dependency |
|
| 210 | - // get resolved to the correct class name |
|
| 211 | - foreach ($dependencies as $dependency => $load_source) { |
|
| 212 | - $alias = self::$_instance->getFqnForAlias($dependency); |
|
| 213 | - if ($overwrite === EE_Dependency_Map::OVERWRITE_DEPENDENCIES |
|
| 214 | - || ! isset(self::$_instance->_dependency_map[ $class ][ $alias ]) |
|
| 215 | - ) { |
|
| 216 | - unset($dependencies[ $dependency ]); |
|
| 217 | - $dependencies[ $alias ] = $load_source; |
|
| 218 | - $registered = true; |
|
| 219 | - } |
|
| 220 | - } |
|
| 221 | - // now add our two lists of dependencies together. |
|
| 222 | - // using Union (+=) favours the arrays in precedence from left to right, |
|
| 223 | - // so $dependencies is NOT overwritten because it is listed first |
|
| 224 | - // ie: with A = B + C, entries in B take precedence over duplicate entries in C |
|
| 225 | - // Union is way faster than array_merge() but should be used with caution... |
|
| 226 | - // especially with numerically indexed arrays |
|
| 227 | - $dependencies += self::$_instance->_dependency_map[ $class ]; |
|
| 228 | - // now we need to ensure that the resulting dependencies |
|
| 229 | - // array only has the entries that are required for the class |
|
| 230 | - // so first count how many dependencies were originally registered for the class |
|
| 231 | - $dependency_count = count(self::$_instance->_dependency_map[ $class ]); |
|
| 232 | - // if that count is non-zero (meaning dependencies were already registered) |
|
| 233 | - self::$_instance->_dependency_map[ $class ] = $dependency_count |
|
| 234 | - // then truncate the final array to match that count |
|
| 235 | - ? array_slice($dependencies, 0, $dependency_count) |
|
| 236 | - // otherwise just take the incoming array because nothing previously existed |
|
| 237 | - : $dependencies; |
|
| 238 | - return $registered; |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - |
|
| 242 | - /** |
|
| 243 | - * @param string $class_name |
|
| 244 | - * @param string $loader |
|
| 245 | - * @return bool |
|
| 246 | - * @throws DomainException |
|
| 247 | - */ |
|
| 248 | - public static function register_class_loader($class_name, $loader = 'load_core') |
|
| 249 | - { |
|
| 250 | - if (! $loader instanceof Closure && strpos($class_name, '\\') !== false) { |
|
| 251 | - throw new DomainException( |
|
| 252 | - esc_html__('Don\'t use class loaders for FQCNs.', 'event_espresso') |
|
| 253 | - ); |
|
| 254 | - } |
|
| 255 | - // check that loader is callable or method starts with "load_" and exists in EE_Registry |
|
| 256 | - if (! is_callable($loader) |
|
| 257 | - && ( |
|
| 258 | - strpos($loader, 'load_') !== 0 |
|
| 259 | - || ! method_exists('EE_Registry', $loader) |
|
| 260 | - ) |
|
| 261 | - ) { |
|
| 262 | - throw new DomainException( |
|
| 263 | - sprintf( |
|
| 264 | - esc_html__( |
|
| 265 | - '"%1$s" is not a valid loader method on EE_Registry.', |
|
| 266 | - 'event_espresso' |
|
| 267 | - ), |
|
| 268 | - $loader |
|
| 269 | - ) |
|
| 270 | - ); |
|
| 271 | - } |
|
| 272 | - $class_name = self::$_instance->getFqnForAlias($class_name); |
|
| 273 | - if (! isset(self::$_instance->_class_loaders[ $class_name ])) { |
|
| 274 | - self::$_instance->_class_loaders[ $class_name ] = $loader; |
|
| 275 | - return true; |
|
| 276 | - } |
|
| 277 | - return false; |
|
| 278 | - } |
|
| 279 | - |
|
| 280 | - |
|
| 281 | - /** |
|
| 282 | - * @return array |
|
| 283 | - */ |
|
| 284 | - public function dependency_map() |
|
| 285 | - { |
|
| 286 | - return $this->_dependency_map; |
|
| 287 | - } |
|
| 288 | - |
|
| 289 | - |
|
| 290 | - /** |
|
| 291 | - * returns TRUE if dependency map contains a listing for the provided class name |
|
| 292 | - * |
|
| 293 | - * @param string $class_name |
|
| 294 | - * @return boolean |
|
| 295 | - */ |
|
| 296 | - public function has($class_name = '') |
|
| 297 | - { |
|
| 298 | - // all legacy models have the same dependencies |
|
| 299 | - if (strpos($class_name, 'EEM_') === 0) { |
|
| 300 | - $class_name = 'LEGACY_MODELS'; |
|
| 301 | - } |
|
| 302 | - return isset($this->_dependency_map[ $class_name ]) ? true : false; |
|
| 303 | - } |
|
| 304 | - |
|
| 305 | - |
|
| 306 | - /** |
|
| 307 | - * returns TRUE if dependency map contains a listing for the provided class name AND dependency |
|
| 308 | - * |
|
| 309 | - * @param string $class_name |
|
| 310 | - * @param string $dependency |
|
| 311 | - * @return bool |
|
| 312 | - */ |
|
| 313 | - public function has_dependency_for_class($class_name = '', $dependency = '') |
|
| 314 | - { |
|
| 315 | - // all legacy models have the same dependencies |
|
| 316 | - if (strpos($class_name, 'EEM_') === 0) { |
|
| 317 | - $class_name = 'LEGACY_MODELS'; |
|
| 318 | - } |
|
| 319 | - $dependency = $this->getFqnForAlias($dependency, $class_name); |
|
| 320 | - return isset($this->_dependency_map[ $class_name ][ $dependency ]) |
|
| 321 | - ? true |
|
| 322 | - : false; |
|
| 323 | - } |
|
| 324 | - |
|
| 325 | - |
|
| 326 | - /** |
|
| 327 | - * returns loading strategy for whether a previously cached dependency should be loaded or a new instance returned |
|
| 328 | - * |
|
| 329 | - * @param string $class_name |
|
| 330 | - * @param string $dependency |
|
| 331 | - * @return int |
|
| 332 | - */ |
|
| 333 | - public function loading_strategy_for_class_dependency($class_name = '', $dependency = '') |
|
| 334 | - { |
|
| 335 | - // all legacy models have the same dependencies |
|
| 336 | - if (strpos($class_name, 'EEM_') === 0) { |
|
| 337 | - $class_name = 'LEGACY_MODELS'; |
|
| 338 | - } |
|
| 339 | - $dependency = $this->getFqnForAlias($dependency); |
|
| 340 | - return $this->has_dependency_for_class($class_name, $dependency) |
|
| 341 | - ? $this->_dependency_map[ $class_name ][ $dependency ] |
|
| 342 | - : EE_Dependency_Map::not_registered; |
|
| 343 | - } |
|
| 344 | - |
|
| 345 | - |
|
| 346 | - /** |
|
| 347 | - * @param string $class_name |
|
| 348 | - * @return string | Closure |
|
| 349 | - */ |
|
| 350 | - public function class_loader($class_name) |
|
| 351 | - { |
|
| 352 | - // all legacy models use load_model() |
|
| 353 | - if (strpos($class_name, 'EEM_') === 0) { |
|
| 354 | - return 'load_model'; |
|
| 355 | - } |
|
| 356 | - // EE_CPT_*_Strategy classes like EE_CPT_Event_Strategy, EE_CPT_Venue_Strategy, etc |
|
| 357 | - // perform strpos() first to avoid loading regex every time we load a class |
|
| 358 | - if (strpos($class_name, 'EE_CPT_') === 0 |
|
| 359 | - && preg_match('/^EE_CPT_([a-zA-Z]+)_Strategy$/', $class_name) |
|
| 360 | - ) { |
|
| 361 | - return 'load_core'; |
|
| 362 | - } |
|
| 363 | - $class_name = $this->getFqnForAlias($class_name); |
|
| 364 | - return isset($this->_class_loaders[ $class_name ]) ? $this->_class_loaders[ $class_name ] : ''; |
|
| 365 | - } |
|
| 366 | - |
|
| 367 | - |
|
| 368 | - /** |
|
| 369 | - * @return array |
|
| 370 | - */ |
|
| 371 | - public function class_loaders() |
|
| 372 | - { |
|
| 373 | - return $this->_class_loaders; |
|
| 374 | - } |
|
| 375 | - |
|
| 376 | - |
|
| 377 | - /** |
|
| 378 | - * adds an alias for a classname |
|
| 379 | - * |
|
| 380 | - * @param string $fqcn the class name that should be used (concrete class to replace interface) |
|
| 381 | - * @param string $alias the class name that would be type hinted for (abstract parent or interface) |
|
| 382 | - * @param string $for_class the class that has the dependency (is type hinting for the interface) |
|
| 383 | - */ |
|
| 384 | - public function add_alias($fqcn, $alias, $for_class = '') |
|
| 385 | - { |
|
| 386 | - $this->class_cache->addAlias($fqcn, $alias, $for_class); |
|
| 387 | - } |
|
| 388 | - |
|
| 389 | - |
|
| 390 | - /** |
|
| 391 | - * Returns TRUE if the provided fully qualified name IS an alias |
|
| 392 | - * WHY? |
|
| 393 | - * Because if a class is type hinting for a concretion, |
|
| 394 | - * then why would we need to find another class to supply it? |
|
| 395 | - * ie: if a class asks for `Fully/Qualified/Namespace/SpecificClassName`, |
|
| 396 | - * then give it an instance of `Fully/Qualified/Namespace/SpecificClassName`. |
|
| 397 | - * Don't go looking for some substitute. |
|
| 398 | - * Whereas if a class is type hinting for an interface... |
|
| 399 | - * then we need to find an actual class to use. |
|
| 400 | - * So the interface IS the alias for some other FQN, |
|
| 401 | - * and we need to find out if `Fully/Qualified/Namespace/SomeInterface` |
|
| 402 | - * represents some other class. |
|
| 403 | - * |
|
| 404 | - * @param string $fqn |
|
| 405 | - * @param string $for_class |
|
| 406 | - * @return bool |
|
| 407 | - */ |
|
| 408 | - public function isAlias($fqn = '', $for_class = '') |
|
| 409 | - { |
|
| 410 | - return $this->class_cache->isAlias($fqn, $for_class); |
|
| 411 | - } |
|
| 412 | - |
|
| 413 | - |
|
| 414 | - /** |
|
| 415 | - * Returns a FQN for provided alias if one exists, otherwise returns the original $alias |
|
| 416 | - * functions recursively, so that multiple aliases can be used to drill down to a FQN |
|
| 417 | - * for example: |
|
| 418 | - * if the following two entries were added to the _aliases array: |
|
| 419 | - * array( |
|
| 420 | - * 'interface_alias' => 'some\namespace\interface' |
|
| 421 | - * 'some\namespace\interface' => 'some\namespace\classname' |
|
| 422 | - * ) |
|
| 423 | - * then one could use EE_Registry::instance()->create( 'interface_alias' ) |
|
| 424 | - * to load an instance of 'some\namespace\classname' |
|
| 425 | - * |
|
| 426 | - * @param string $alias |
|
| 427 | - * @param string $for_class |
|
| 428 | - * @return string |
|
| 429 | - */ |
|
| 430 | - public function getFqnForAlias($alias = '', $for_class = '') |
|
| 431 | - { |
|
| 432 | - return (string) $this->class_cache->getFqnForAlias($alias, $for_class); |
|
| 433 | - } |
|
| 434 | - |
|
| 435 | - |
|
| 436 | - /** |
|
| 437 | - * Registers the core dependencies and whether a previously instantiated object should be loaded from the cache, |
|
| 438 | - * if one exists, or whether a new object should be generated every time the requested class is loaded. |
|
| 439 | - * This is done by using the following class constants: |
|
| 440 | - * EE_Dependency_Map::load_from_cache - loads previously instantiated object |
|
| 441 | - * EE_Dependency_Map::load_new_object - generates a new object every time |
|
| 442 | - */ |
|
| 443 | - protected function _register_core_dependencies() |
|
| 444 | - { |
|
| 445 | - $this->_dependency_map = array( |
|
| 446 | - 'EE_Request_Handler' => array( |
|
| 447 | - 'EE_Request' => EE_Dependency_Map::load_from_cache, |
|
| 448 | - ), |
|
| 449 | - 'EE_System' => array( |
|
| 450 | - 'EE_Registry' => EE_Dependency_Map::load_from_cache, |
|
| 451 | - 'EventEspresso\core\services\loaders\Loader' => EE_Dependency_Map::load_from_cache, |
|
| 452 | - 'EventEspresso\core\services\request\Request' => EE_Dependency_Map::load_from_cache, |
|
| 453 | - 'EE_Maintenance_Mode' => EE_Dependency_Map::load_from_cache, |
|
| 454 | - ), |
|
| 455 | - 'EE_Session' => array( |
|
| 456 | - 'EventEspresso\core\services\cache\TransientCacheStorage' => EE_Dependency_Map::load_from_cache, |
|
| 457 | - 'EventEspresso\core\domain\values\session\SessionLifespan' => EE_Dependency_Map::load_from_cache, |
|
| 458 | - 'EventEspresso\core\services\request\Request' => EE_Dependency_Map::load_from_cache, |
|
| 459 | - 'EventEspresso\core\services\session\SessionStartHandler' => EE_Dependency_Map::load_from_cache, |
|
| 460 | - 'EE_Encryption' => EE_Dependency_Map::load_from_cache, |
|
| 461 | - ), |
|
| 462 | - 'EE_Cart' => array( |
|
| 463 | - 'EE_Session' => EE_Dependency_Map::load_from_cache, |
|
| 464 | - ), |
|
| 465 | - 'EE_Front_Controller' => array( |
|
| 466 | - 'EE_Registry' => EE_Dependency_Map::load_from_cache, |
|
| 467 | - 'EE_Request_Handler' => EE_Dependency_Map::load_from_cache, |
|
| 468 | - 'EE_Module_Request_Router' => EE_Dependency_Map::load_from_cache, |
|
| 469 | - ), |
|
| 470 | - 'EE_Messenger_Collection_Loader' => array( |
|
| 471 | - 'EE_Messenger_Collection' => EE_Dependency_Map::load_new_object, |
|
| 472 | - ), |
|
| 473 | - 'EE_Message_Type_Collection_Loader' => array( |
|
| 474 | - 'EE_Message_Type_Collection' => EE_Dependency_Map::load_new_object, |
|
| 475 | - ), |
|
| 476 | - 'EE_Message_Resource_Manager' => array( |
|
| 477 | - 'EE_Messenger_Collection_Loader' => EE_Dependency_Map::load_new_object, |
|
| 478 | - 'EE_Message_Type_Collection_Loader' => EE_Dependency_Map::load_new_object, |
|
| 479 | - 'EEM_Message_Template_Group' => EE_Dependency_Map::load_from_cache, |
|
| 480 | - ), |
|
| 481 | - 'EE_Message_Factory' => array( |
|
| 482 | - 'EE_Message_Resource_Manager' => EE_Dependency_Map::load_from_cache, |
|
| 483 | - ), |
|
| 484 | - 'EE_messages' => array( |
|
| 485 | - 'EE_Message_Resource_Manager' => EE_Dependency_Map::load_from_cache, |
|
| 486 | - ), |
|
| 487 | - 'EE_Messages_Generator' => array( |
|
| 488 | - 'EE_Messages_Queue' => EE_Dependency_Map::load_new_object, |
|
| 489 | - 'EE_Messages_Data_Handler_Collection' => EE_Dependency_Map::load_new_object, |
|
| 490 | - 'EE_Message_Template_Group_Collection' => EE_Dependency_Map::load_new_object, |
|
| 491 | - 'EEH_Parse_Shortcodes' => EE_Dependency_Map::load_from_cache, |
|
| 492 | - ), |
|
| 493 | - 'EE_Messages_Processor' => array( |
|
| 494 | - 'EE_Message_Resource_Manager' => EE_Dependency_Map::load_from_cache, |
|
| 495 | - ), |
|
| 496 | - 'EE_Messages_Queue' => array( |
|
| 497 | - 'EE_Message_Repository' => EE_Dependency_Map::load_new_object, |
|
| 498 | - ), |
|
| 499 | - 'EE_Messages_Template_Defaults' => array( |
|
| 500 | - 'EEM_Message_Template_Group' => EE_Dependency_Map::load_from_cache, |
|
| 501 | - 'EEM_Message_Template' => EE_Dependency_Map::load_from_cache, |
|
| 502 | - ), |
|
| 503 | - 'EE_Message_To_Generate_From_Request' => array( |
|
| 504 | - 'EE_Message_Resource_Manager' => EE_Dependency_Map::load_from_cache, |
|
| 505 | - 'EE_Request_Handler' => EE_Dependency_Map::load_from_cache, |
|
| 506 | - ), |
|
| 507 | - 'EventEspresso\core\services\commands\CommandBus' => array( |
|
| 508 | - 'EventEspresso\core\services\commands\CommandHandlerManager' => EE_Dependency_Map::load_from_cache, |
|
| 509 | - ), |
|
| 510 | - 'EventEspresso\services\commands\CommandHandler' => array( |
|
| 511 | - 'EE_Registry' => EE_Dependency_Map::load_from_cache, |
|
| 512 | - 'CommandBusInterface' => EE_Dependency_Map::load_from_cache, |
|
| 513 | - ), |
|
| 514 | - 'EventEspresso\core\services\commands\CommandHandlerManager' => array( |
|
| 515 | - 'EventEspresso\core\services\loaders\Loader' => EE_Dependency_Map::load_from_cache, |
|
| 516 | - ), |
|
| 517 | - 'EventEspresso\core\services\commands\CompositeCommandHandler' => array( |
|
| 518 | - 'EventEspresso\core\services\commands\CommandBus' => EE_Dependency_Map::load_from_cache, |
|
| 519 | - 'EventEspresso\core\services\commands\CommandFactory' => EE_Dependency_Map::load_from_cache, |
|
| 520 | - ), |
|
| 521 | - 'EventEspresso\core\services\commands\CommandFactory' => array( |
|
| 522 | - 'EventEspresso\core\services\loaders\Loader' => EE_Dependency_Map::load_from_cache, |
|
| 523 | - ), |
|
| 524 | - 'EventEspresso\core\services\commands\middleware\CapChecker' => array( |
|
| 525 | - 'EventEspresso\core\domain\services\capabilities\CapabilitiesChecker' => EE_Dependency_Map::load_from_cache, |
|
| 526 | - ), |
|
| 527 | - 'EventEspresso\core\domain\services\capabilities\CapabilitiesChecker' => array( |
|
| 528 | - 'EE_Capabilities' => EE_Dependency_Map::load_from_cache, |
|
| 529 | - ), |
|
| 530 | - 'EventEspresso\core\domain\services\capabilities\RegistrationsCapChecker' => array( |
|
| 531 | - 'EE_Capabilities' => EE_Dependency_Map::load_from_cache, |
|
| 532 | - ), |
|
| 533 | - 'EventEspresso\core\services\commands\registration\CreateRegistrationCommandHandler' => array( |
|
| 534 | - 'EventEspresso\core\domain\services\registration\CreateRegistrationService' => EE_Dependency_Map::load_from_cache, |
|
| 535 | - ), |
|
| 536 | - 'EventEspresso\core\services\commands\registration\CopyRegistrationDetailsCommandHandler' => array( |
|
| 537 | - 'EventEspresso\core\domain\services\registration\CopyRegistrationService' => EE_Dependency_Map::load_from_cache, |
|
| 538 | - ), |
|
| 539 | - 'EventEspresso\core\services\commands\registration\CopyRegistrationPaymentsCommandHandler' => array( |
|
| 540 | - 'EventEspresso\core\domain\services\registration\CopyRegistrationService' => EE_Dependency_Map::load_from_cache, |
|
| 541 | - ), |
|
| 542 | - 'EventEspresso\core\services\commands\registration\CancelRegistrationAndTicketLineItemCommandHandler' => array( |
|
| 543 | - 'EventEspresso\core\domain\services\registration\CancelTicketLineItemService' => EE_Dependency_Map::load_from_cache, |
|
| 544 | - ), |
|
| 545 | - 'EventEspresso\core\services\commands\registration\UpdateRegistrationAndTransactionAfterChangeCommandHandler' => array( |
|
| 546 | - 'EventEspresso\core\domain\services\registration\UpdateRegistrationService' => EE_Dependency_Map::load_from_cache, |
|
| 547 | - ), |
|
| 548 | - 'EventEspresso\core\services\commands\ticket\CreateTicketLineItemCommandHandler' => array( |
|
| 549 | - 'EventEspresso\core\domain\services\ticket\CreateTicketLineItemService' => EE_Dependency_Map::load_from_cache, |
|
| 550 | - ), |
|
| 551 | - 'EventEspresso\core\services\commands\ticket\CancelTicketLineItemCommandHandler' => array( |
|
| 552 | - 'EventEspresso\core\domain\services\ticket\CancelTicketLineItemService' => EE_Dependency_Map::load_from_cache, |
|
| 553 | - ), |
|
| 554 | - 'EventEspresso\core\domain\services\registration\CancelRegistrationService' => array( |
|
| 555 | - 'EventEspresso\core\domain\services\ticket\CancelTicketLineItemService' => EE_Dependency_Map::load_from_cache, |
|
| 556 | - ), |
|
| 557 | - 'EventEspresso\core\services\commands\attendee\CreateAttendeeCommandHandler' => array( |
|
| 558 | - 'EEM_Attendee' => EE_Dependency_Map::load_from_cache, |
|
| 559 | - ), |
|
| 560 | - 'EventEspresso\core\services\database\TableManager' => array( |
|
| 561 | - 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
| 562 | - ), |
|
| 563 | - 'EE_Data_Migration_Class_Base' => array( |
|
| 564 | - 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
| 565 | - 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
| 566 | - ), |
|
| 567 | - 'EE_DMS_Core_4_1_0' => array( |
|
| 568 | - 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
| 569 | - 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
| 570 | - ), |
|
| 571 | - 'EE_DMS_Core_4_2_0' => array( |
|
| 572 | - 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
| 573 | - 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
| 574 | - ), |
|
| 575 | - 'EE_DMS_Core_4_3_0' => array( |
|
| 576 | - 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
| 577 | - 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
| 578 | - ), |
|
| 579 | - 'EE_DMS_Core_4_4_0' => array( |
|
| 580 | - 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
| 581 | - 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
| 582 | - ), |
|
| 583 | - 'EE_DMS_Core_4_5_0' => array( |
|
| 584 | - 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
| 585 | - 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
| 586 | - ), |
|
| 587 | - 'EE_DMS_Core_4_6_0' => array( |
|
| 588 | - 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
| 589 | - 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
| 590 | - ), |
|
| 591 | - 'EE_DMS_Core_4_7_0' => array( |
|
| 592 | - 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
| 593 | - 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
| 594 | - ), |
|
| 595 | - 'EE_DMS_Core_4_8_0' => array( |
|
| 596 | - 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
| 597 | - 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
| 598 | - ), |
|
| 599 | - 'EE_DMS_Core_4_9_0' => array( |
|
| 600 | - 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
| 601 | - 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
| 602 | - ), |
|
| 603 | - 'EventEspresso\core\services\assets\I18nRegistry' => array( |
|
| 604 | - array(), |
|
| 605 | - 'EventEspresso\core\domain\Domain' => EE_Dependency_Map::load_from_cache, |
|
| 606 | - ), |
|
| 607 | - 'EventEspresso\core\services\assets\Registry' => array( |
|
| 608 | - 'EventEspresso\core\services\assets\AssetCollection' => EE_Dependency_Map::load_from_cache, |
|
| 609 | - 'EventEspresso\core\services\assets\I18nRegistry' => EE_Dependency_Map::load_from_cache, |
|
| 610 | - ), |
|
| 611 | - 'EventEspresso\core\domain\entities\shortcodes\EspressoCancelled' => array( |
|
| 612 | - 'EventEspresso\core\services\cache\PostRelatedCacheManager' => EE_Dependency_Map::load_from_cache, |
|
| 613 | - ), |
|
| 614 | - 'EventEspresso\core\domain\entities\shortcodes\EspressoCheckout' => array( |
|
| 615 | - 'EventEspresso\core\services\cache\PostRelatedCacheManager' => EE_Dependency_Map::load_from_cache, |
|
| 616 | - ), |
|
| 617 | - 'EventEspresso\core\domain\entities\shortcodes\EspressoEventAttendees' => array( |
|
| 618 | - 'EventEspresso\core\services\cache\PostRelatedCacheManager' => EE_Dependency_Map::load_from_cache, |
|
| 619 | - ), |
|
| 620 | - 'EventEspresso\core\domain\entities\shortcodes\EspressoEvents' => array( |
|
| 621 | - 'EventEspresso\core\services\cache\PostRelatedCacheManager' => EE_Dependency_Map::load_from_cache, |
|
| 622 | - ), |
|
| 623 | - 'EventEspresso\core\domain\entities\shortcodes\EspressoThankYou' => array( |
|
| 624 | - 'EventEspresso\core\services\cache\PostRelatedCacheManager' => EE_Dependency_Map::load_from_cache, |
|
| 625 | - ), |
|
| 626 | - 'EventEspresso\core\domain\entities\shortcodes\EspressoTicketSelector' => array( |
|
| 627 | - 'EventEspresso\core\services\cache\PostRelatedCacheManager' => EE_Dependency_Map::load_from_cache, |
|
| 628 | - ), |
|
| 629 | - 'EventEspresso\core\domain\entities\shortcodes\EspressoTxnPage' => array( |
|
| 630 | - 'EventEspresso\core\services\cache\PostRelatedCacheManager' => EE_Dependency_Map::load_from_cache, |
|
| 631 | - ), |
|
| 632 | - 'EventEspresso\core\services\cache\BasicCacheManager' => array( |
|
| 633 | - 'EventEspresso\core\services\cache\TransientCacheStorage' => EE_Dependency_Map::load_from_cache, |
|
| 634 | - ), |
|
| 635 | - 'EventEspresso\core\services\cache\PostRelatedCacheManager' => array( |
|
| 636 | - 'EventEspresso\core\services\cache\TransientCacheStorage' => EE_Dependency_Map::load_from_cache, |
|
| 637 | - ), |
|
| 638 | - 'EventEspresso\core\domain\services\validation\email\EmailValidationService' => array( |
|
| 639 | - 'EE_Registration_Config' => EE_Dependency_Map::load_from_cache, |
|
| 640 | - 'EventEspresso\core\services\loaders\Loader' => EE_Dependency_Map::load_from_cache, |
|
| 641 | - ), |
|
| 642 | - 'EventEspresso\core\domain\values\EmailAddress' => array( |
|
| 643 | - null, |
|
| 644 | - 'EventEspresso\core\domain\services\validation\email\EmailValidationService' => EE_Dependency_Map::load_from_cache, |
|
| 645 | - ), |
|
| 646 | - 'EventEspresso\core\services\orm\ModelFieldFactory' => array( |
|
| 647 | - 'EventEspresso\core\services\loaders\Loader' => EE_Dependency_Map::load_from_cache, |
|
| 648 | - ), |
|
| 649 | - 'LEGACY_MODELS' => array( |
|
| 650 | - null, |
|
| 651 | - 'EventEspresso\core\services\database\ModelFieldFactory' => EE_Dependency_Map::load_from_cache, |
|
| 652 | - ), |
|
| 653 | - 'EE_Module_Request_Router' => array( |
|
| 654 | - 'EE_Request' => EE_Dependency_Map::load_from_cache, |
|
| 655 | - ), |
|
| 656 | - 'EE_Registration_Processor' => array( |
|
| 657 | - 'EE_Request' => EE_Dependency_Map::load_from_cache, |
|
| 658 | - ), |
|
| 659 | - 'EventEspresso\core\services\notifications\PersistentAdminNoticeManager' => array( |
|
| 660 | - null, |
|
| 661 | - 'EventEspresso\core\domain\services\capabilities\CapabilitiesChecker' => EE_Dependency_Map::load_from_cache, |
|
| 662 | - 'EventEspresso\core\services\request\Request' => EE_Dependency_Map::load_from_cache, |
|
| 663 | - ), |
|
| 664 | - 'EventEspresso\core\services\licensing\LicenseService' => array( |
|
| 665 | - 'EventEspresso\core\domain\services\pue\Stats' => EE_Dependency_Map::load_from_cache, |
|
| 666 | - 'EventEspresso\core\domain\services\pue\Config' => EE_Dependency_Map::load_from_cache, |
|
| 667 | - ), |
|
| 668 | - 'EE_Admin_Transactions_List_Table' => array( |
|
| 669 | - null, |
|
| 670 | - 'EventEspresso\core\domain\values\session\SessionLifespan' => EE_Dependency_Map::load_from_cache, |
|
| 671 | - ), |
|
| 672 | - 'EventEspresso\core\domain\services\pue\Stats' => array( |
|
| 673 | - 'EventEspresso\core\domain\services\pue\Config' => EE_Dependency_Map::load_from_cache, |
|
| 674 | - 'EE_Maintenance_Mode' => EE_Dependency_Map::load_from_cache, |
|
| 675 | - 'EventEspresso\core\domain\services\pue\StatsGatherer' => EE_Dependency_Map::load_from_cache, |
|
| 676 | - ), |
|
| 677 | - 'EventEspresso\core\domain\services\pue\Config' => array( |
|
| 678 | - 'EE_Network_Config' => EE_Dependency_Map::load_from_cache, |
|
| 679 | - 'EE_Config' => EE_Dependency_Map::load_from_cache, |
|
| 680 | - ), |
|
| 681 | - 'EventEspresso\core\domain\services\pue\StatsGatherer' => array( |
|
| 682 | - 'EEM_Payment_Method' => EE_Dependency_Map::load_from_cache, |
|
| 683 | - 'EEM_Event' => EE_Dependency_Map::load_from_cache, |
|
| 684 | - 'EEM_Datetime' => EE_Dependency_Map::load_from_cache, |
|
| 685 | - 'EEM_Ticket' => EE_Dependency_Map::load_from_cache, |
|
| 686 | - 'EEM_Registration' => EE_Dependency_Map::load_from_cache, |
|
| 687 | - 'EEM_Transaction' => EE_Dependency_Map::load_from_cache, |
|
| 688 | - 'EE_Config' => EE_Dependency_Map::load_from_cache, |
|
| 689 | - ), |
|
| 690 | - 'EventEspresso\core\domain\services\admin\ExitModal' => array( |
|
| 691 | - 'EventEspresso\core\services\assets\Registry' => EE_Dependency_Map::load_from_cache, |
|
| 692 | - ), |
|
| 693 | - 'EventEspresso\core\domain\services\admin\PluginUpsells' => array( |
|
| 694 | - 'EventEspresso\core\domain\Domain' => EE_Dependency_Map::load_from_cache, |
|
| 695 | - ), |
|
| 696 | - 'EventEspresso\caffeinated\modules\recaptcha_invisible\InvisibleRecaptcha' => array( |
|
| 697 | - 'EE_Registration_Config' => EE_Dependency_Map::load_from_cache, |
|
| 698 | - 'EE_Session' => EE_Dependency_Map::load_from_cache, |
|
| 699 | - ), |
|
| 700 | - 'EventEspresso\caffeinated\modules\recaptcha_invisible\RecaptchaAdminSettings' => array( |
|
| 701 | - 'EE_Registration_Config' => EE_Dependency_Map::load_from_cache, |
|
| 702 | - ), |
|
| 703 | - 'EventEspresso\modules\ticket_selector\ProcessTicketSelector' => array( |
|
| 704 | - 'EE_Core_Config' => EE_Dependency_Map::load_from_cache, |
|
| 705 | - 'EventEspresso\core\services\request\Request' => EE_Dependency_Map::load_from_cache, |
|
| 706 | - 'EE_Session' => EE_Dependency_Map::load_from_cache, |
|
| 707 | - 'EEM_Ticket' => EE_Dependency_Map::load_from_cache, |
|
| 708 | - 'EventEspresso\modules\ticket_selector\TicketDatetimeAvailabilityTracker' => EE_Dependency_Map::load_from_cache, |
|
| 709 | - ), |
|
| 710 | - 'EventEspresso\modules\ticket_selector\TicketDatetimeAvailabilityTracker' => array( |
|
| 711 | - 'EEM_Datetime' => EE_Dependency_Map::load_from_cache, |
|
| 712 | - ), |
|
| 713 | - 'EventEspresso\core\domain\entities\custom_post_types\CustomPostTypeDefinitions' => array( |
|
| 714 | - 'EE_Core_Config' => EE_Dependency_Map::load_from_cache, |
|
| 715 | - 'EventEspresso\core\services\loaders\Loader' => EE_Dependency_Map::load_from_cache, |
|
| 716 | - ), |
|
| 717 | - 'EventEspresso\core\domain\services\custom_post_types\RegisterCustomPostTypes' => array( |
|
| 718 | - 'EventEspresso\core\domain\entities\custom_post_types\CustomPostTypeDefinitions' => EE_Dependency_Map::load_from_cache, |
|
| 719 | - ), |
|
| 720 | - 'EventEspresso\core\domain\services\custom_post_types\RegisterCustomTaxonomies' => array( |
|
| 721 | - 'EventEspresso\core\domain\entities\custom_post_types\CustomTaxonomyDefinitions' => EE_Dependency_Map::load_from_cache, |
|
| 722 | - ), |
|
| 723 | - 'EE_CPT_Strategy' => array( |
|
| 724 | - 'EventEspresso\core\domain\entities\custom_post_types\CustomPostTypeDefinitions' => EE_Dependency_Map::load_from_cache, |
|
| 725 | - 'EventEspresso\core\domain\entities\custom_post_types\CustomTaxonomyDefinitions' => EE_Dependency_Map::load_from_cache, |
|
| 726 | - ), |
|
| 727 | - 'EventEspresso\core\services\loaders\ObjectIdentifier' => array( |
|
| 728 | - 'EventEspresso\core\services\loaders\ClassInterfaceCache' => EE_Dependency_Map::load_from_cache, |
|
| 729 | - ), |
|
| 730 | - 'EventEspresso\core\domain\services\assets\CoreAssetManager' => array( |
|
| 731 | - 'EventEspresso\core\services\assets\AssetCollection' => EE_Dependency_Map::load_from_cache, |
|
| 732 | - 'EE_Currency_Config' => EE_Dependency_Map::load_from_cache, |
|
| 733 | - 'EE_Template_Config' => EE_Dependency_Map::load_from_cache, |
|
| 734 | - 'EventEspresso\core\domain\Domain' => EE_Dependency_Map::load_from_cache, |
|
| 735 | - 'EventEspresso\core\services\assets\Registry' => EE_Dependency_Map::load_from_cache, |
|
| 736 | - ), |
|
| 737 | - 'EventEspresso\core\domain\services\admin\privacy\policy\PrivacyPolicy' => array( |
|
| 738 | - 'EEM_Payment_Method' => EE_Dependency_Map::load_from_cache, |
|
| 739 | - 'EventEspresso\core\domain\values\session\SessionLifespan' => EE_Dependency_Map::load_from_cache |
|
| 740 | - ), |
|
| 741 | - 'EventEspresso\core\domain\services\admin\privacy\export\ExportAttendee' => array( |
|
| 742 | - 'EEM_Attendee' => EE_Dependency_Map::load_from_cache, |
|
| 743 | - ), |
|
| 744 | - 'EventEspresso\core\domain\services\admin\privacy\export\ExportAttendeeBillingData' => array( |
|
| 745 | - 'EEM_Attendee' => EE_Dependency_Map::load_from_cache, |
|
| 746 | - 'EEM_Payment_Method' => EE_Dependency_Map::load_from_cache |
|
| 747 | - ), |
|
| 748 | - 'EventEspresso\core\domain\services\admin\privacy\export\ExportCheckins' => array( |
|
| 749 | - 'EEM_Checkin' => EE_Dependency_Map::load_from_cache, |
|
| 750 | - ), |
|
| 751 | - 'EventEspresso\core\domain\services\admin\privacy\export\ExportRegistration' => array( |
|
| 752 | - 'EEM_Registration' => EE_Dependency_Map::load_from_cache, |
|
| 753 | - ), |
|
| 754 | - 'EventEspresso\core\domain\services\admin\privacy\export\ExportTransaction' => array( |
|
| 755 | - 'EEM_Transaction' => EE_Dependency_Map::load_from_cache, |
|
| 756 | - ), |
|
| 757 | - 'EventEspresso\core\domain\services\admin\privacy\erasure\EraseAttendeeData' => array( |
|
| 758 | - 'EEM_Attendee' => EE_Dependency_Map::load_from_cache, |
|
| 759 | - ), |
|
| 760 | - 'EventEspresso\core\domain\services\admin\privacy\erasure\EraseAnswers' => array( |
|
| 761 | - 'EEM_Answer' => EE_Dependency_Map::load_from_cache, |
|
| 762 | - 'EEM_Question' => EE_Dependency_Map::load_from_cache, |
|
| 763 | - ), |
|
| 764 | - 'EventEspresso\core\CPTs\CptQueryModifier' => array( |
|
| 765 | - null, |
|
| 766 | - null, |
|
| 767 | - null, |
|
| 768 | - 'EE_Request_Handler' => EE_Dependency_Map::load_from_cache, |
|
| 769 | - 'EventEspresso\core\services\request\Request' => EE_Dependency_Map::load_from_cache, |
|
| 770 | - 'EventEspresso\core\services\loaders\Loader' => EE_Dependency_Map::load_from_cache, |
|
| 771 | - ), |
|
| 772 | - 'EventEspresso\core\domain\services\admin\privacy\forms\PrivacySettingsFormHandler' => array( |
|
| 773 | - 'EE_Registry' => EE_Dependency_Map::load_from_cache, |
|
| 774 | - 'EE_Config' => EE_Dependency_Map::load_from_cache |
|
| 775 | - ), |
|
| 776 | - 'EventEspresso\core\services\editor\BlockRegistrationManager' => array( |
|
| 777 | - 'EventEspresso\core\services\assets\BlockAssetManagerCollection' => EE_Dependency_Map::load_from_cache, |
|
| 778 | - 'EventEspresso\core\domain\entities\editor\BlockCollection' => EE_Dependency_Map::load_from_cache, |
|
| 779 | - 'EventEspresso\core\services\route_match\RouteMatchSpecificationManager' => EE_Dependency_Map::load_from_cache, |
|
| 780 | - 'EventEspresso\core\services\request\Request' => EE_Dependency_Map::load_from_cache, |
|
| 781 | - ), |
|
| 782 | - 'EventEspresso\core\domain\entities\editor\CoreBlocksAssetManager' => array( |
|
| 783 | - 'EventEspresso\core\domain\Domain' => EE_Dependency_Map::load_from_cache, |
|
| 784 | - 'EventEspresso\core\services\assets\AssetCollection' => EE_Dependency_Map::load_from_cache, |
|
| 785 | - 'EventEspresso\core\services\assets\Registry' => EE_Dependency_Map::load_from_cache, |
|
| 786 | - ), |
|
| 787 | - 'EventEspresso\core\domain\services\blocks\EventAttendeesBlockRenderer' => array( |
|
| 788 | - 'EventEspresso\core\domain\Domain' => EE_Dependency_Map::load_from_cache, |
|
| 789 | - 'EEM_Attendee' => EE_Dependency_Map::load_from_cache, |
|
| 790 | - ), |
|
| 791 | - 'EventEspresso\core\domain\entities\editor\blocks\EventAttendees' => array( |
|
| 792 | - 'EventEspresso\core\domain\entities\editor\CoreBlocksAssetManager' => self::load_from_cache, |
|
| 793 | - 'EventEspresso\core\services\request\Request' => EE_Dependency_Map::load_from_cache, |
|
| 794 | - 'EventEspresso\core\domain\services\blocks\EventAttendeesBlockRenderer' => self::load_from_cache, |
|
| 795 | - ), |
|
| 796 | - 'EventEspresso\core\services\route_match\RouteMatchSpecificationDependencyResolver' => array( |
|
| 797 | - 'EventEspresso\core\services\container\Mirror' => EE_Dependency_Map::load_from_cache, |
|
| 798 | - 'EventEspresso\core\services\loaders\ClassInterfaceCache' => EE_Dependency_Map::load_from_cache, |
|
| 799 | - 'EE_Dependency_Map' => EE_Dependency_Map::load_from_cache, |
|
| 800 | - ), |
|
| 801 | - 'EventEspresso\core\services\route_match\RouteMatchSpecificationFactory' => array( |
|
| 802 | - 'EventEspresso\core\services\route_match\RouteMatchSpecificationDependencyResolver' => EE_Dependency_Map::load_from_cache, |
|
| 803 | - 'EventEspresso\core\services\loaders\Loader' => EE_Dependency_Map::load_from_cache, |
|
| 804 | - ), |
|
| 805 | - 'EventEspresso\core\services\route_match\RouteMatchSpecificationManager' => array( |
|
| 806 | - 'EventEspresso\core\services\route_match\RouteMatchSpecificationCollection' => EE_Dependency_Map::load_from_cache, |
|
| 807 | - 'EventEspresso\core\services\route_match\RouteMatchSpecificationFactory' => EE_Dependency_Map::load_from_cache, |
|
| 808 | - ), |
|
| 809 | - 'EventEspresso\core\libraries\rest_api\CalculatedModelFields' => array( |
|
| 810 | - 'EventEspresso\core\libraries\rest_api\calculations\CalculatedModelFieldsFactory' => EE_Dependency_Map::load_from_cache |
|
| 811 | - ), |
|
| 812 | - 'EventEspresso\core\libraries\rest_api\calculations\CalculatedModelFieldsFactory' => array( |
|
| 813 | - 'EventEspresso\core\services\loaders\Loader' => EE_Dependency_Map::load_from_cache, |
|
| 814 | - ), |
|
| 815 | - 'EventEspresso\core\libraries\rest_api\controllers\model\Read' => array( |
|
| 816 | - 'EventEspresso\core\libraries\rest_api\CalculatedModelFields' => EE_Dependency_Map::load_from_cache |
|
| 817 | - ), |
|
| 818 | - 'EventEspresso\core\libraries\rest_api\calculations\Datetime' => array( |
|
| 819 | - 'EEM_Datetime' => EE_Dependency_Map::load_from_cache, |
|
| 820 | - 'EEM_Registration' => EE_Dependency_Map::load_from_cache |
|
| 821 | - ), |
|
| 822 | - 'EventEspresso\core\libraries\rest_api\calculations\Event' => array( |
|
| 823 | - 'EEM_Event' => EE_Dependency_Map::load_from_cache, |
|
| 824 | - 'EEM_Registration' => EE_Dependency_Map::load_from_cache |
|
| 825 | - ), |
|
| 826 | - 'EventEspresso\core\libraries\rest_api\calculations\Registration' => array( |
|
| 827 | - 'EEM_Registration' => EE_Dependency_Map::load_from_cache |
|
| 828 | - ), |
|
| 829 | - 'EventEspresso\core\services\session\SessionStartHandler' => array( |
|
| 830 | - 'EventEspresso\core\services\request\Request' => EE_Dependency_Map::load_from_cache, |
|
| 831 | - ), |
|
| 832 | - 'EE_URL_Validation_Strategy' => array( |
|
| 833 | - null, |
|
| 834 | - null, |
|
| 835 | - 'EventEspresso\core\services\validators\URLValidator' => EE_Dependency_Map::load_from_cache |
|
| 836 | - ), |
|
| 837 | - 'EventEspresso\admin_pages\general_settings\OrganizationSettings' => array( |
|
| 838 | - 'EE_Registry' => EE_Dependency_Map::load_from_cache, |
|
| 839 | - 'EE_Organization_Config' => EE_Dependency_Map::load_from_cache, |
|
| 840 | - 'EE_Core_Config' => EE_Dependency_Map::load_from_cache, |
|
| 841 | - 'EE_Network_Core_Config' => EE_Dependency_Map::load_from_cache, |
|
| 842 | - 'EventEspresso\core\services\address\CountrySubRegionDao' => EE_Dependency_Map::load_from_cache, |
|
| 843 | - ), |
|
| 844 | - 'EventEspresso\core\services\address\CountrySubRegionDao' => array( |
|
| 845 | - 'EEM_State' => EE_Dependency_Map::load_from_cache, |
|
| 846 | - 'EventEspresso\core\services\validators\JsonValidator' => EE_Dependency_Map::load_from_cache |
|
| 847 | - ), |
|
| 848 | - 'EventEspresso\core\domain\services\admin\ajax\WordpressHeartbeat' => array( |
|
| 849 | - 'EventEspresso\core\services\loaders\Loader' => EE_Dependency_Map::load_from_cache, |
|
| 850 | - 'EventEspresso\core\services\request\Request' => EE_Dependency_Map::load_from_cache, |
|
| 851 | - ), |
|
| 852 | - 'EventEspresso\core\domain\services\admin\ajax\EventEditorHeartbeat' => array( |
|
| 853 | - 'EventEspresso\core\domain\Domain' => EE_Dependency_Map::load_from_cache, |
|
| 854 | - 'EE_Environment_Config' => EE_Dependency_Map::load_from_cache, |
|
| 855 | - ), |
|
| 856 | - 'EventEspresso\core\services\request\files\FilesDataHandler' => array( |
|
| 857 | - 'EventEspresso\core\services\request\Request' => EE_Dependency_Map::load_from_cache, |
|
| 858 | - ), |
|
| 859 | - 'EventEspressoBatchRequest\BatchRequestProcessor' => [ |
|
| 860 | - 'EventEspresso\core\services\loaders\Loader' => EE_Dependency_Map::load_from_cache, |
|
| 861 | - ] |
|
| 862 | - ); |
|
| 863 | - } |
|
| 864 | - |
|
| 865 | - |
|
| 866 | - /** |
|
| 867 | - * Registers how core classes are loaded. |
|
| 868 | - * This can either be done by simply providing the name of one of the EE_Registry loader methods such as: |
|
| 869 | - * 'EE_Request_Handler' => 'load_core' |
|
| 870 | - * 'EE_Messages_Queue' => 'load_lib' |
|
| 871 | - * 'EEH_Debug_Tools' => 'load_helper' |
|
| 872 | - * or, if greater control is required, by providing a custom closure. For example: |
|
| 873 | - * 'Some_Class' => function () { |
|
| 874 | - * return new Some_Class(); |
|
| 875 | - * }, |
|
| 876 | - * This is required for instantiating dependencies |
|
| 877 | - * where an interface has been type hinted in a class constructor. For example: |
|
| 878 | - * 'Required_Interface' => function () { |
|
| 879 | - * return new A_Class_That_Implements_Required_Interface(); |
|
| 880 | - * }, |
|
| 881 | - */ |
|
| 882 | - protected function _register_core_class_loaders() |
|
| 883 | - { |
|
| 884 | - $this->_class_loaders = array( |
|
| 885 | - // load_core |
|
| 886 | - 'EE_Dependency_Map' => function () { |
|
| 887 | - return $this; |
|
| 888 | - }, |
|
| 889 | - 'EE_Capabilities' => 'load_core', |
|
| 890 | - 'EE_Encryption' => 'load_core', |
|
| 891 | - 'EE_Front_Controller' => 'load_core', |
|
| 892 | - 'EE_Module_Request_Router' => 'load_core', |
|
| 893 | - 'EE_Registry' => 'load_core', |
|
| 894 | - 'EE_Request' => function () { |
|
| 895 | - return $this->legacy_request; |
|
| 896 | - }, |
|
| 897 | - 'EventEspresso\core\services\request\Request' => function () { |
|
| 898 | - return $this->request; |
|
| 899 | - }, |
|
| 900 | - 'EventEspresso\core\services\request\Response' => function () { |
|
| 901 | - return $this->response; |
|
| 902 | - }, |
|
| 903 | - 'EE_Base' => 'load_core', |
|
| 904 | - 'EE_Request_Handler' => 'load_core', |
|
| 905 | - 'EE_Session' => 'load_core', |
|
| 906 | - 'EE_Cron_Tasks' => 'load_core', |
|
| 907 | - 'EE_System' => 'load_core', |
|
| 908 | - 'EE_Maintenance_Mode' => 'load_core', |
|
| 909 | - 'EE_Register_CPTs' => 'load_core', |
|
| 910 | - 'EE_Admin' => 'load_core', |
|
| 911 | - 'EE_CPT_Strategy' => 'load_core', |
|
| 912 | - // load_class |
|
| 913 | - 'EE_Registration_Processor' => 'load_class', |
|
| 914 | - // load_lib |
|
| 915 | - 'EE_Message_Resource_Manager' => 'load_lib', |
|
| 916 | - 'EE_Message_Type_Collection' => 'load_lib', |
|
| 917 | - 'EE_Message_Type_Collection_Loader' => 'load_lib', |
|
| 918 | - 'EE_Messenger_Collection' => 'load_lib', |
|
| 919 | - 'EE_Messenger_Collection_Loader' => 'load_lib', |
|
| 920 | - 'EE_Messages_Processor' => 'load_lib', |
|
| 921 | - 'EE_Message_Repository' => 'load_lib', |
|
| 922 | - 'EE_Messages_Queue' => 'load_lib', |
|
| 923 | - 'EE_Messages_Data_Handler_Collection' => 'load_lib', |
|
| 924 | - 'EE_Message_Template_Group_Collection' => 'load_lib', |
|
| 925 | - 'EE_Payment_Method_Manager' => 'load_lib', |
|
| 926 | - 'EE_Messages_Generator' => function () { |
|
| 927 | - return EE_Registry::instance()->load_lib( |
|
| 928 | - 'Messages_Generator', |
|
| 929 | - array(), |
|
| 930 | - false, |
|
| 931 | - false |
|
| 932 | - ); |
|
| 933 | - }, |
|
| 934 | - 'EE_Messages_Template_Defaults' => function ($arguments = array()) { |
|
| 935 | - return EE_Registry::instance()->load_lib( |
|
| 936 | - 'Messages_Template_Defaults', |
|
| 937 | - $arguments, |
|
| 938 | - false, |
|
| 939 | - false |
|
| 940 | - ); |
|
| 941 | - }, |
|
| 942 | - // load_helper |
|
| 943 | - 'EEH_Parse_Shortcodes' => function () { |
|
| 944 | - if (EE_Registry::instance()->load_helper('Parse_Shortcodes')) { |
|
| 945 | - return new EEH_Parse_Shortcodes(); |
|
| 946 | - } |
|
| 947 | - return null; |
|
| 948 | - }, |
|
| 949 | - 'EE_Template_Config' => function () { |
|
| 950 | - return EE_Config::instance()->template_settings; |
|
| 951 | - }, |
|
| 952 | - 'EE_Currency_Config' => function () { |
|
| 953 | - return EE_Config::instance()->currency; |
|
| 954 | - }, |
|
| 955 | - 'EE_Registration_Config' => function () { |
|
| 956 | - return EE_Config::instance()->registration; |
|
| 957 | - }, |
|
| 958 | - 'EE_Core_Config' => function () { |
|
| 959 | - return EE_Config::instance()->core; |
|
| 960 | - }, |
|
| 961 | - 'EventEspresso\core\services\loaders\Loader' => function () { |
|
| 962 | - return LoaderFactory::getLoader(); |
|
| 963 | - }, |
|
| 964 | - 'EE_Network_Config' => function () { |
|
| 965 | - return EE_Network_Config::instance(); |
|
| 966 | - }, |
|
| 967 | - 'EE_Config' => function () { |
|
| 968 | - return EE_Config::instance(); |
|
| 969 | - }, |
|
| 970 | - 'EventEspresso\core\domain\Domain' => function () { |
|
| 971 | - return DomainFactory::getEventEspressoCoreDomain(); |
|
| 972 | - }, |
|
| 973 | - 'EE_Admin_Config' => function () { |
|
| 974 | - return EE_Config::instance()->admin; |
|
| 975 | - }, |
|
| 976 | - 'EE_Organization_Config' => function () { |
|
| 977 | - return EE_Config::instance()->organization; |
|
| 978 | - }, |
|
| 979 | - 'EE_Network_Core_Config' => function () { |
|
| 980 | - return EE_Network_Config::instance()->core; |
|
| 981 | - }, |
|
| 982 | - 'EE_Environment_Config' => function () { |
|
| 983 | - return EE_Config::instance()->environment; |
|
| 984 | - }, |
|
| 985 | - ); |
|
| 986 | - } |
|
| 987 | - |
|
| 988 | - |
|
| 989 | - /** |
|
| 990 | - * can be used for supplying alternate names for classes, |
|
| 991 | - * or for connecting interface names to instantiable classes |
|
| 992 | - */ |
|
| 993 | - protected function _register_core_aliases() |
|
| 994 | - { |
|
| 995 | - $aliases = array( |
|
| 996 | - 'CommandBusInterface' => 'EventEspresso\core\services\commands\CommandBusInterface', |
|
| 997 | - 'EventEspresso\core\services\commands\CommandBusInterface' => 'EventEspresso\core\services\commands\CommandBus', |
|
| 998 | - 'CommandHandlerManagerInterface' => 'EventEspresso\core\services\commands\CommandHandlerManagerInterface', |
|
| 999 | - 'EventEspresso\core\services\commands\CommandHandlerManagerInterface' => 'EventEspresso\core\services\commands\CommandHandlerManager', |
|
| 1000 | - 'CapChecker' => 'EventEspresso\core\services\commands\middleware\CapChecker', |
|
| 1001 | - 'AddActionHook' => 'EventEspresso\core\services\commands\middleware\AddActionHook', |
|
| 1002 | - 'CapabilitiesChecker' => 'EventEspresso\core\domain\services\capabilities\CapabilitiesChecker', |
|
| 1003 | - 'CapabilitiesCheckerInterface' => 'EventEspresso\core\domain\services\capabilities\CapabilitiesCheckerInterface', |
|
| 1004 | - 'EventEspresso\core\domain\services\capabilities\CapabilitiesCheckerInterface' => 'EventEspresso\core\domain\services\capabilities\CapabilitiesChecker', |
|
| 1005 | - 'CreateRegistrationService' => 'EventEspresso\core\domain\services\registration\CreateRegistrationService', |
|
| 1006 | - 'CreateRegistrationCommandHandler' => 'EventEspresso\core\services\commands\registration\CreateRegistrationCommand', |
|
| 1007 | - 'CopyRegistrationDetailsCommandHandler' => 'EventEspresso\core\services\commands\registration\CopyRegistrationDetailsCommand', |
|
| 1008 | - 'CopyRegistrationPaymentsCommandHandler' => 'EventEspresso\core\services\commands\registration\CopyRegistrationPaymentsCommand', |
|
| 1009 | - 'CancelRegistrationAndTicketLineItemCommandHandler' => 'EventEspresso\core\services\commands\registration\CancelRegistrationAndTicketLineItemCommandHandler', |
|
| 1010 | - 'UpdateRegistrationAndTransactionAfterChangeCommandHandler' => 'EventEspresso\core\services\commands\registration\UpdateRegistrationAndTransactionAfterChangeCommandHandler', |
|
| 1011 | - 'CreateTicketLineItemCommandHandler' => 'EventEspresso\core\services\commands\ticket\CreateTicketLineItemCommand', |
|
| 1012 | - 'CreateTransactionCommandHandler' => 'EventEspresso\core\services\commands\transaction\CreateTransactionCommandHandler', |
|
| 1013 | - 'CreateAttendeeCommandHandler' => 'EventEspresso\core\services\commands\attendee\CreateAttendeeCommandHandler', |
|
| 1014 | - 'TableManager' => 'EventEspresso\core\services\database\TableManager', |
|
| 1015 | - 'TableAnalysis' => 'EventEspresso\core\services\database\TableAnalysis', |
|
| 1016 | - 'EspressoShortcode' => 'EventEspresso\core\services\shortcodes\EspressoShortcode', |
|
| 1017 | - 'ShortcodeInterface' => 'EventEspresso\core\services\shortcodes\ShortcodeInterface', |
|
| 1018 | - 'EventEspresso\core\services\shortcodes\ShortcodeInterface' => 'EventEspresso\core\services\shortcodes\EspressoShortcode', |
|
| 1019 | - 'EventEspresso\core\services\cache\CacheStorageInterface' => 'EventEspresso\core\services\cache\TransientCacheStorage', |
|
| 1020 | - 'LoaderInterface' => 'EventEspresso\core\services\loaders\LoaderInterface', |
|
| 1021 | - 'EventEspresso\core\services\loaders\LoaderInterface' => 'EventEspresso\core\services\loaders\Loader', |
|
| 1022 | - 'CommandFactoryInterface' => 'EventEspresso\core\services\commands\CommandFactoryInterface', |
|
| 1023 | - 'EventEspresso\core\services\commands\CommandFactoryInterface' => 'EventEspresso\core\services\commands\CommandFactory', |
|
| 1024 | - 'EmailValidatorInterface' => 'EventEspresso\core\domain\services\validation\email\EmailValidatorInterface', |
|
| 1025 | - 'EventEspresso\core\domain\services\validation\email\EmailValidatorInterface' => 'EventEspresso\core\domain\services\validation\email\EmailValidationService', |
|
| 1026 | - 'NoticeConverterInterface' => 'EventEspresso\core\services\notices\NoticeConverterInterface', |
|
| 1027 | - 'EventEspresso\core\services\notices\NoticeConverterInterface' => 'EventEspresso\core\services\notices\ConvertNoticesToEeErrors', |
|
| 1028 | - 'NoticesContainerInterface' => 'EventEspresso\core\services\notices\NoticesContainerInterface', |
|
| 1029 | - 'EventEspresso\core\services\notices\NoticesContainerInterface' => 'EventEspresso\core\services\notices\NoticesContainer', |
|
| 1030 | - 'EventEspresso\core\services\request\RequestInterface' => 'EventEspresso\core\services\request\Request', |
|
| 1031 | - 'EventEspresso\core\services\request\ResponseInterface' => 'EventEspresso\core\services\request\Response', |
|
| 1032 | - 'EventEspresso\core\domain\DomainInterface' => 'EventEspresso\core\domain\Domain', |
|
| 1033 | - 'Registration_Processor' => 'EE_Registration_Processor', |
|
| 1034 | - ); |
|
| 1035 | - foreach ($aliases as $alias => $fqn) { |
|
| 1036 | - if (is_array($fqn)) { |
|
| 1037 | - foreach ($fqn as $class => $for_class) { |
|
| 1038 | - $this->class_cache->addAlias($class, $alias, $for_class); |
|
| 1039 | - } |
|
| 1040 | - continue; |
|
| 1041 | - } |
|
| 1042 | - $this->class_cache->addAlias($fqn, $alias); |
|
| 1043 | - } |
|
| 1044 | - if (! (defined('DOING_AJAX') && DOING_AJAX) && is_admin()) { |
|
| 1045 | - $this->class_cache->addAlias( |
|
| 1046 | - 'EventEspresso\core\services\notices\ConvertNoticesToAdminNotices', |
|
| 1047 | - 'EventEspresso\core\services\notices\NoticeConverterInterface' |
|
| 1048 | - ); |
|
| 1049 | - } |
|
| 1050 | - } |
|
| 1051 | - |
|
| 1052 | - |
|
| 1053 | - /** |
|
| 1054 | - * This is used to reset the internal map and class_loaders to their original default state at the beginning of the |
|
| 1055 | - * request Primarily used by unit tests. |
|
| 1056 | - */ |
|
| 1057 | - public function reset() |
|
| 1058 | - { |
|
| 1059 | - $this->_register_core_class_loaders(); |
|
| 1060 | - $this->_register_core_dependencies(); |
|
| 1061 | - } |
|
| 1062 | - |
|
| 1063 | - |
|
| 1064 | - /** |
|
| 1065 | - * PLZ NOTE: a better name for this method would be is_alias() |
|
| 1066 | - * because it returns TRUE if the provided fully qualified name IS an alias |
|
| 1067 | - * WHY? |
|
| 1068 | - * Because if a class is type hinting for a concretion, |
|
| 1069 | - * then why would we need to find another class to supply it? |
|
| 1070 | - * ie: if a class asks for `Fully/Qualified/Namespace/SpecificClassName`, |
|
| 1071 | - * then give it an instance of `Fully/Qualified/Namespace/SpecificClassName`. |
|
| 1072 | - * Don't go looking for some substitute. |
|
| 1073 | - * Whereas if a class is type hinting for an interface... |
|
| 1074 | - * then we need to find an actual class to use. |
|
| 1075 | - * So the interface IS the alias for some other FQN, |
|
| 1076 | - * and we need to find out if `Fully/Qualified/Namespace/SomeInterface` |
|
| 1077 | - * represents some other class. |
|
| 1078 | - * |
|
| 1079 | - * @deprecated 4.9.62.p |
|
| 1080 | - * @param string $fqn |
|
| 1081 | - * @param string $for_class |
|
| 1082 | - * @return bool |
|
| 1083 | - */ |
|
| 1084 | - public function has_alias($fqn = '', $for_class = '') |
|
| 1085 | - { |
|
| 1086 | - return $this->isAlias($fqn, $for_class); |
|
| 1087 | - } |
|
| 1088 | - |
|
| 1089 | - |
|
| 1090 | - /** |
|
| 1091 | - * PLZ NOTE: a better name for this method would be get_fqn_for_alias() |
|
| 1092 | - * because it returns a FQN for provided alias if one exists, otherwise returns the original $alias |
|
| 1093 | - * functions recursively, so that multiple aliases can be used to drill down to a FQN |
|
| 1094 | - * for example: |
|
| 1095 | - * if the following two entries were added to the _aliases array: |
|
| 1096 | - * array( |
|
| 1097 | - * 'interface_alias' => 'some\namespace\interface' |
|
| 1098 | - * 'some\namespace\interface' => 'some\namespace\classname' |
|
| 1099 | - * ) |
|
| 1100 | - * then one could use EE_Registry::instance()->create( 'interface_alias' ) |
|
| 1101 | - * to load an instance of 'some\namespace\classname' |
|
| 1102 | - * |
|
| 1103 | - * @deprecated 4.9.62.p |
|
| 1104 | - * @param string $alias |
|
| 1105 | - * @param string $for_class |
|
| 1106 | - * @return string |
|
| 1107 | - */ |
|
| 1108 | - public function get_alias($alias = '', $for_class = '') |
|
| 1109 | - { |
|
| 1110 | - return $this->getFqnForAlias($alias, $for_class); |
|
| 1111 | - } |
|
| 23 | + /** |
|
| 24 | + * This means that the requested class dependency is not present in the dependency map |
|
| 25 | + */ |
|
| 26 | + const not_registered = 0; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * This instructs class loaders to ALWAYS return a newly instantiated object for the requested class. |
|
| 30 | + */ |
|
| 31 | + const load_new_object = 1; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * This instructs class loaders to return a previously instantiated and cached object for the requested class. |
|
| 35 | + * IF a previously instantiated object does not exist, a new one will be created and added to the cache. |
|
| 36 | + */ |
|
| 37 | + const load_from_cache = 2; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * When registering a dependency, |
|
| 41 | + * this indicates to keep any existing dependencies that already exist, |
|
| 42 | + * and simply discard any new dependencies declared in the incoming data |
|
| 43 | + */ |
|
| 44 | + const KEEP_EXISTING_DEPENDENCIES = 0; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * When registering a dependency, |
|
| 48 | + * this indicates to overwrite any existing dependencies that already exist using the incoming data |
|
| 49 | + */ |
|
| 50 | + const OVERWRITE_DEPENDENCIES = 1; |
|
| 51 | + |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * @type EE_Dependency_Map $_instance |
|
| 55 | + */ |
|
| 56 | + protected static $_instance; |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * @var ClassInterfaceCache $class_cache |
|
| 60 | + */ |
|
| 61 | + private $class_cache; |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * @type RequestInterface $request |
|
| 65 | + */ |
|
| 66 | + protected $request; |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * @type LegacyRequestInterface $legacy_request |
|
| 70 | + */ |
|
| 71 | + protected $legacy_request; |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * @type ResponseInterface $response |
|
| 75 | + */ |
|
| 76 | + protected $response; |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * @type LoaderInterface $loader |
|
| 80 | + */ |
|
| 81 | + protected $loader; |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * @type array $_dependency_map |
|
| 85 | + */ |
|
| 86 | + protected $_dependency_map = array(); |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * @type array $_class_loaders |
|
| 90 | + */ |
|
| 91 | + protected $_class_loaders = array(); |
|
| 92 | + |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * EE_Dependency_Map constructor. |
|
| 96 | + * |
|
| 97 | + * @param ClassInterfaceCache $class_cache |
|
| 98 | + */ |
|
| 99 | + protected function __construct(ClassInterfaceCache $class_cache) |
|
| 100 | + { |
|
| 101 | + $this->class_cache = $class_cache; |
|
| 102 | + do_action('EE_Dependency_Map____construct', $this); |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + |
|
| 106 | + /** |
|
| 107 | + * @return void |
|
| 108 | + */ |
|
| 109 | + public function initialize() |
|
| 110 | + { |
|
| 111 | + $this->_register_core_dependencies(); |
|
| 112 | + $this->_register_core_class_loaders(); |
|
| 113 | + $this->_register_core_aliases(); |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * @singleton method used to instantiate class object |
|
| 119 | + * @param ClassInterfaceCache|null $class_cache |
|
| 120 | + * @return EE_Dependency_Map |
|
| 121 | + */ |
|
| 122 | + public static function instance(ClassInterfaceCache $class_cache = null) |
|
| 123 | + { |
|
| 124 | + // check if class object is instantiated, and instantiated properly |
|
| 125 | + if (! self::$_instance instanceof EE_Dependency_Map |
|
| 126 | + && $class_cache instanceof ClassInterfaceCache |
|
| 127 | + ) { |
|
| 128 | + self::$_instance = new EE_Dependency_Map($class_cache); |
|
| 129 | + } |
|
| 130 | + return self::$_instance; |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + |
|
| 134 | + /** |
|
| 135 | + * @param RequestInterface $request |
|
| 136 | + */ |
|
| 137 | + public function setRequest(RequestInterface $request) |
|
| 138 | + { |
|
| 139 | + $this->request = $request; |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + |
|
| 143 | + /** |
|
| 144 | + * @param LegacyRequestInterface $legacy_request |
|
| 145 | + */ |
|
| 146 | + public function setLegacyRequest(LegacyRequestInterface $legacy_request) |
|
| 147 | + { |
|
| 148 | + $this->legacy_request = $legacy_request; |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + |
|
| 152 | + /** |
|
| 153 | + * @param ResponseInterface $response |
|
| 154 | + */ |
|
| 155 | + public function setResponse(ResponseInterface $response) |
|
| 156 | + { |
|
| 157 | + $this->response = $response; |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + |
|
| 161 | + /** |
|
| 162 | + * @param LoaderInterface $loader |
|
| 163 | + */ |
|
| 164 | + public function setLoader(LoaderInterface $loader) |
|
| 165 | + { |
|
| 166 | + $this->loader = $loader; |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + |
|
| 170 | + /** |
|
| 171 | + * @param string $class |
|
| 172 | + * @param array $dependencies |
|
| 173 | + * @param int $overwrite |
|
| 174 | + * @return bool |
|
| 175 | + */ |
|
| 176 | + public static function register_dependencies( |
|
| 177 | + $class, |
|
| 178 | + array $dependencies, |
|
| 179 | + $overwrite = EE_Dependency_Map::KEEP_EXISTING_DEPENDENCIES |
|
| 180 | + ) { |
|
| 181 | + return self::$_instance->registerDependencies($class, $dependencies, $overwrite); |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + |
|
| 185 | + /** |
|
| 186 | + * Assigns an array of class names and corresponding load sources (new or cached) |
|
| 187 | + * to the class specified by the first parameter. |
|
| 188 | + * IMPORTANT !!! |
|
| 189 | + * The order of elements in the incoming $dependencies array MUST match |
|
| 190 | + * the order of the constructor parameters for the class in question. |
|
| 191 | + * This is especially important when overriding any existing dependencies that are registered. |
|
| 192 | + * the third parameter controls whether any duplicate dependencies are overwritten or not. |
|
| 193 | + * |
|
| 194 | + * @param string $class |
|
| 195 | + * @param array $dependencies |
|
| 196 | + * @param int $overwrite |
|
| 197 | + * @return bool |
|
| 198 | + */ |
|
| 199 | + public function registerDependencies( |
|
| 200 | + $class, |
|
| 201 | + array $dependencies, |
|
| 202 | + $overwrite = EE_Dependency_Map::KEEP_EXISTING_DEPENDENCIES |
|
| 203 | + ) { |
|
| 204 | + $class = trim($class, '\\'); |
|
| 205 | + $registered = false; |
|
| 206 | + if (empty(self::$_instance->_dependency_map[ $class ])) { |
|
| 207 | + self::$_instance->_dependency_map[ $class ] = array(); |
|
| 208 | + } |
|
| 209 | + // we need to make sure that any aliases used when registering a dependency |
|
| 210 | + // get resolved to the correct class name |
|
| 211 | + foreach ($dependencies as $dependency => $load_source) { |
|
| 212 | + $alias = self::$_instance->getFqnForAlias($dependency); |
|
| 213 | + if ($overwrite === EE_Dependency_Map::OVERWRITE_DEPENDENCIES |
|
| 214 | + || ! isset(self::$_instance->_dependency_map[ $class ][ $alias ]) |
|
| 215 | + ) { |
|
| 216 | + unset($dependencies[ $dependency ]); |
|
| 217 | + $dependencies[ $alias ] = $load_source; |
|
| 218 | + $registered = true; |
|
| 219 | + } |
|
| 220 | + } |
|
| 221 | + // now add our two lists of dependencies together. |
|
| 222 | + // using Union (+=) favours the arrays in precedence from left to right, |
|
| 223 | + // so $dependencies is NOT overwritten because it is listed first |
|
| 224 | + // ie: with A = B + C, entries in B take precedence over duplicate entries in C |
|
| 225 | + // Union is way faster than array_merge() but should be used with caution... |
|
| 226 | + // especially with numerically indexed arrays |
|
| 227 | + $dependencies += self::$_instance->_dependency_map[ $class ]; |
|
| 228 | + // now we need to ensure that the resulting dependencies |
|
| 229 | + // array only has the entries that are required for the class |
|
| 230 | + // so first count how many dependencies were originally registered for the class |
|
| 231 | + $dependency_count = count(self::$_instance->_dependency_map[ $class ]); |
|
| 232 | + // if that count is non-zero (meaning dependencies were already registered) |
|
| 233 | + self::$_instance->_dependency_map[ $class ] = $dependency_count |
|
| 234 | + // then truncate the final array to match that count |
|
| 235 | + ? array_slice($dependencies, 0, $dependency_count) |
|
| 236 | + // otherwise just take the incoming array because nothing previously existed |
|
| 237 | + : $dependencies; |
|
| 238 | + return $registered; |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + |
|
| 242 | + /** |
|
| 243 | + * @param string $class_name |
|
| 244 | + * @param string $loader |
|
| 245 | + * @return bool |
|
| 246 | + * @throws DomainException |
|
| 247 | + */ |
|
| 248 | + public static function register_class_loader($class_name, $loader = 'load_core') |
|
| 249 | + { |
|
| 250 | + if (! $loader instanceof Closure && strpos($class_name, '\\') !== false) { |
|
| 251 | + throw new DomainException( |
|
| 252 | + esc_html__('Don\'t use class loaders for FQCNs.', 'event_espresso') |
|
| 253 | + ); |
|
| 254 | + } |
|
| 255 | + // check that loader is callable or method starts with "load_" and exists in EE_Registry |
|
| 256 | + if (! is_callable($loader) |
|
| 257 | + && ( |
|
| 258 | + strpos($loader, 'load_') !== 0 |
|
| 259 | + || ! method_exists('EE_Registry', $loader) |
|
| 260 | + ) |
|
| 261 | + ) { |
|
| 262 | + throw new DomainException( |
|
| 263 | + sprintf( |
|
| 264 | + esc_html__( |
|
| 265 | + '"%1$s" is not a valid loader method on EE_Registry.', |
|
| 266 | + 'event_espresso' |
|
| 267 | + ), |
|
| 268 | + $loader |
|
| 269 | + ) |
|
| 270 | + ); |
|
| 271 | + } |
|
| 272 | + $class_name = self::$_instance->getFqnForAlias($class_name); |
|
| 273 | + if (! isset(self::$_instance->_class_loaders[ $class_name ])) { |
|
| 274 | + self::$_instance->_class_loaders[ $class_name ] = $loader; |
|
| 275 | + return true; |
|
| 276 | + } |
|
| 277 | + return false; |
|
| 278 | + } |
|
| 279 | + |
|
| 280 | + |
|
| 281 | + /** |
|
| 282 | + * @return array |
|
| 283 | + */ |
|
| 284 | + public function dependency_map() |
|
| 285 | + { |
|
| 286 | + return $this->_dependency_map; |
|
| 287 | + } |
|
| 288 | + |
|
| 289 | + |
|
| 290 | + /** |
|
| 291 | + * returns TRUE if dependency map contains a listing for the provided class name |
|
| 292 | + * |
|
| 293 | + * @param string $class_name |
|
| 294 | + * @return boolean |
|
| 295 | + */ |
|
| 296 | + public function has($class_name = '') |
|
| 297 | + { |
|
| 298 | + // all legacy models have the same dependencies |
|
| 299 | + if (strpos($class_name, 'EEM_') === 0) { |
|
| 300 | + $class_name = 'LEGACY_MODELS'; |
|
| 301 | + } |
|
| 302 | + return isset($this->_dependency_map[ $class_name ]) ? true : false; |
|
| 303 | + } |
|
| 304 | + |
|
| 305 | + |
|
| 306 | + /** |
|
| 307 | + * returns TRUE if dependency map contains a listing for the provided class name AND dependency |
|
| 308 | + * |
|
| 309 | + * @param string $class_name |
|
| 310 | + * @param string $dependency |
|
| 311 | + * @return bool |
|
| 312 | + */ |
|
| 313 | + public function has_dependency_for_class($class_name = '', $dependency = '') |
|
| 314 | + { |
|
| 315 | + // all legacy models have the same dependencies |
|
| 316 | + if (strpos($class_name, 'EEM_') === 0) { |
|
| 317 | + $class_name = 'LEGACY_MODELS'; |
|
| 318 | + } |
|
| 319 | + $dependency = $this->getFqnForAlias($dependency, $class_name); |
|
| 320 | + return isset($this->_dependency_map[ $class_name ][ $dependency ]) |
|
| 321 | + ? true |
|
| 322 | + : false; |
|
| 323 | + } |
|
| 324 | + |
|
| 325 | + |
|
| 326 | + /** |
|
| 327 | + * returns loading strategy for whether a previously cached dependency should be loaded or a new instance returned |
|
| 328 | + * |
|
| 329 | + * @param string $class_name |
|
| 330 | + * @param string $dependency |
|
| 331 | + * @return int |
|
| 332 | + */ |
|
| 333 | + public function loading_strategy_for_class_dependency($class_name = '', $dependency = '') |
|
| 334 | + { |
|
| 335 | + // all legacy models have the same dependencies |
|
| 336 | + if (strpos($class_name, 'EEM_') === 0) { |
|
| 337 | + $class_name = 'LEGACY_MODELS'; |
|
| 338 | + } |
|
| 339 | + $dependency = $this->getFqnForAlias($dependency); |
|
| 340 | + return $this->has_dependency_for_class($class_name, $dependency) |
|
| 341 | + ? $this->_dependency_map[ $class_name ][ $dependency ] |
|
| 342 | + : EE_Dependency_Map::not_registered; |
|
| 343 | + } |
|
| 344 | + |
|
| 345 | + |
|
| 346 | + /** |
|
| 347 | + * @param string $class_name |
|
| 348 | + * @return string | Closure |
|
| 349 | + */ |
|
| 350 | + public function class_loader($class_name) |
|
| 351 | + { |
|
| 352 | + // all legacy models use load_model() |
|
| 353 | + if (strpos($class_name, 'EEM_') === 0) { |
|
| 354 | + return 'load_model'; |
|
| 355 | + } |
|
| 356 | + // EE_CPT_*_Strategy classes like EE_CPT_Event_Strategy, EE_CPT_Venue_Strategy, etc |
|
| 357 | + // perform strpos() first to avoid loading regex every time we load a class |
|
| 358 | + if (strpos($class_name, 'EE_CPT_') === 0 |
|
| 359 | + && preg_match('/^EE_CPT_([a-zA-Z]+)_Strategy$/', $class_name) |
|
| 360 | + ) { |
|
| 361 | + return 'load_core'; |
|
| 362 | + } |
|
| 363 | + $class_name = $this->getFqnForAlias($class_name); |
|
| 364 | + return isset($this->_class_loaders[ $class_name ]) ? $this->_class_loaders[ $class_name ] : ''; |
|
| 365 | + } |
|
| 366 | + |
|
| 367 | + |
|
| 368 | + /** |
|
| 369 | + * @return array |
|
| 370 | + */ |
|
| 371 | + public function class_loaders() |
|
| 372 | + { |
|
| 373 | + return $this->_class_loaders; |
|
| 374 | + } |
|
| 375 | + |
|
| 376 | + |
|
| 377 | + /** |
|
| 378 | + * adds an alias for a classname |
|
| 379 | + * |
|
| 380 | + * @param string $fqcn the class name that should be used (concrete class to replace interface) |
|
| 381 | + * @param string $alias the class name that would be type hinted for (abstract parent or interface) |
|
| 382 | + * @param string $for_class the class that has the dependency (is type hinting for the interface) |
|
| 383 | + */ |
|
| 384 | + public function add_alias($fqcn, $alias, $for_class = '') |
|
| 385 | + { |
|
| 386 | + $this->class_cache->addAlias($fqcn, $alias, $for_class); |
|
| 387 | + } |
|
| 388 | + |
|
| 389 | + |
|
| 390 | + /** |
|
| 391 | + * Returns TRUE if the provided fully qualified name IS an alias |
|
| 392 | + * WHY? |
|
| 393 | + * Because if a class is type hinting for a concretion, |
|
| 394 | + * then why would we need to find another class to supply it? |
|
| 395 | + * ie: if a class asks for `Fully/Qualified/Namespace/SpecificClassName`, |
|
| 396 | + * then give it an instance of `Fully/Qualified/Namespace/SpecificClassName`. |
|
| 397 | + * Don't go looking for some substitute. |
|
| 398 | + * Whereas if a class is type hinting for an interface... |
|
| 399 | + * then we need to find an actual class to use. |
|
| 400 | + * So the interface IS the alias for some other FQN, |
|
| 401 | + * and we need to find out if `Fully/Qualified/Namespace/SomeInterface` |
|
| 402 | + * represents some other class. |
|
| 403 | + * |
|
| 404 | + * @param string $fqn |
|
| 405 | + * @param string $for_class |
|
| 406 | + * @return bool |
|
| 407 | + */ |
|
| 408 | + public function isAlias($fqn = '', $for_class = '') |
|
| 409 | + { |
|
| 410 | + return $this->class_cache->isAlias($fqn, $for_class); |
|
| 411 | + } |
|
| 412 | + |
|
| 413 | + |
|
| 414 | + /** |
|
| 415 | + * Returns a FQN for provided alias if one exists, otherwise returns the original $alias |
|
| 416 | + * functions recursively, so that multiple aliases can be used to drill down to a FQN |
|
| 417 | + * for example: |
|
| 418 | + * if the following two entries were added to the _aliases array: |
|
| 419 | + * array( |
|
| 420 | + * 'interface_alias' => 'some\namespace\interface' |
|
| 421 | + * 'some\namespace\interface' => 'some\namespace\classname' |
|
| 422 | + * ) |
|
| 423 | + * then one could use EE_Registry::instance()->create( 'interface_alias' ) |
|
| 424 | + * to load an instance of 'some\namespace\classname' |
|
| 425 | + * |
|
| 426 | + * @param string $alias |
|
| 427 | + * @param string $for_class |
|
| 428 | + * @return string |
|
| 429 | + */ |
|
| 430 | + public function getFqnForAlias($alias = '', $for_class = '') |
|
| 431 | + { |
|
| 432 | + return (string) $this->class_cache->getFqnForAlias($alias, $for_class); |
|
| 433 | + } |
|
| 434 | + |
|
| 435 | + |
|
| 436 | + /** |
|
| 437 | + * Registers the core dependencies and whether a previously instantiated object should be loaded from the cache, |
|
| 438 | + * if one exists, or whether a new object should be generated every time the requested class is loaded. |
|
| 439 | + * This is done by using the following class constants: |
|
| 440 | + * EE_Dependency_Map::load_from_cache - loads previously instantiated object |
|
| 441 | + * EE_Dependency_Map::load_new_object - generates a new object every time |
|
| 442 | + */ |
|
| 443 | + protected function _register_core_dependencies() |
|
| 444 | + { |
|
| 445 | + $this->_dependency_map = array( |
|
| 446 | + 'EE_Request_Handler' => array( |
|
| 447 | + 'EE_Request' => EE_Dependency_Map::load_from_cache, |
|
| 448 | + ), |
|
| 449 | + 'EE_System' => array( |
|
| 450 | + 'EE_Registry' => EE_Dependency_Map::load_from_cache, |
|
| 451 | + 'EventEspresso\core\services\loaders\Loader' => EE_Dependency_Map::load_from_cache, |
|
| 452 | + 'EventEspresso\core\services\request\Request' => EE_Dependency_Map::load_from_cache, |
|
| 453 | + 'EE_Maintenance_Mode' => EE_Dependency_Map::load_from_cache, |
|
| 454 | + ), |
|
| 455 | + 'EE_Session' => array( |
|
| 456 | + 'EventEspresso\core\services\cache\TransientCacheStorage' => EE_Dependency_Map::load_from_cache, |
|
| 457 | + 'EventEspresso\core\domain\values\session\SessionLifespan' => EE_Dependency_Map::load_from_cache, |
|
| 458 | + 'EventEspresso\core\services\request\Request' => EE_Dependency_Map::load_from_cache, |
|
| 459 | + 'EventEspresso\core\services\session\SessionStartHandler' => EE_Dependency_Map::load_from_cache, |
|
| 460 | + 'EE_Encryption' => EE_Dependency_Map::load_from_cache, |
|
| 461 | + ), |
|
| 462 | + 'EE_Cart' => array( |
|
| 463 | + 'EE_Session' => EE_Dependency_Map::load_from_cache, |
|
| 464 | + ), |
|
| 465 | + 'EE_Front_Controller' => array( |
|
| 466 | + 'EE_Registry' => EE_Dependency_Map::load_from_cache, |
|
| 467 | + 'EE_Request_Handler' => EE_Dependency_Map::load_from_cache, |
|
| 468 | + 'EE_Module_Request_Router' => EE_Dependency_Map::load_from_cache, |
|
| 469 | + ), |
|
| 470 | + 'EE_Messenger_Collection_Loader' => array( |
|
| 471 | + 'EE_Messenger_Collection' => EE_Dependency_Map::load_new_object, |
|
| 472 | + ), |
|
| 473 | + 'EE_Message_Type_Collection_Loader' => array( |
|
| 474 | + 'EE_Message_Type_Collection' => EE_Dependency_Map::load_new_object, |
|
| 475 | + ), |
|
| 476 | + 'EE_Message_Resource_Manager' => array( |
|
| 477 | + 'EE_Messenger_Collection_Loader' => EE_Dependency_Map::load_new_object, |
|
| 478 | + 'EE_Message_Type_Collection_Loader' => EE_Dependency_Map::load_new_object, |
|
| 479 | + 'EEM_Message_Template_Group' => EE_Dependency_Map::load_from_cache, |
|
| 480 | + ), |
|
| 481 | + 'EE_Message_Factory' => array( |
|
| 482 | + 'EE_Message_Resource_Manager' => EE_Dependency_Map::load_from_cache, |
|
| 483 | + ), |
|
| 484 | + 'EE_messages' => array( |
|
| 485 | + 'EE_Message_Resource_Manager' => EE_Dependency_Map::load_from_cache, |
|
| 486 | + ), |
|
| 487 | + 'EE_Messages_Generator' => array( |
|
| 488 | + 'EE_Messages_Queue' => EE_Dependency_Map::load_new_object, |
|
| 489 | + 'EE_Messages_Data_Handler_Collection' => EE_Dependency_Map::load_new_object, |
|
| 490 | + 'EE_Message_Template_Group_Collection' => EE_Dependency_Map::load_new_object, |
|
| 491 | + 'EEH_Parse_Shortcodes' => EE_Dependency_Map::load_from_cache, |
|
| 492 | + ), |
|
| 493 | + 'EE_Messages_Processor' => array( |
|
| 494 | + 'EE_Message_Resource_Manager' => EE_Dependency_Map::load_from_cache, |
|
| 495 | + ), |
|
| 496 | + 'EE_Messages_Queue' => array( |
|
| 497 | + 'EE_Message_Repository' => EE_Dependency_Map::load_new_object, |
|
| 498 | + ), |
|
| 499 | + 'EE_Messages_Template_Defaults' => array( |
|
| 500 | + 'EEM_Message_Template_Group' => EE_Dependency_Map::load_from_cache, |
|
| 501 | + 'EEM_Message_Template' => EE_Dependency_Map::load_from_cache, |
|
| 502 | + ), |
|
| 503 | + 'EE_Message_To_Generate_From_Request' => array( |
|
| 504 | + 'EE_Message_Resource_Manager' => EE_Dependency_Map::load_from_cache, |
|
| 505 | + 'EE_Request_Handler' => EE_Dependency_Map::load_from_cache, |
|
| 506 | + ), |
|
| 507 | + 'EventEspresso\core\services\commands\CommandBus' => array( |
|
| 508 | + 'EventEspresso\core\services\commands\CommandHandlerManager' => EE_Dependency_Map::load_from_cache, |
|
| 509 | + ), |
|
| 510 | + 'EventEspresso\services\commands\CommandHandler' => array( |
|
| 511 | + 'EE_Registry' => EE_Dependency_Map::load_from_cache, |
|
| 512 | + 'CommandBusInterface' => EE_Dependency_Map::load_from_cache, |
|
| 513 | + ), |
|
| 514 | + 'EventEspresso\core\services\commands\CommandHandlerManager' => array( |
|
| 515 | + 'EventEspresso\core\services\loaders\Loader' => EE_Dependency_Map::load_from_cache, |
|
| 516 | + ), |
|
| 517 | + 'EventEspresso\core\services\commands\CompositeCommandHandler' => array( |
|
| 518 | + 'EventEspresso\core\services\commands\CommandBus' => EE_Dependency_Map::load_from_cache, |
|
| 519 | + 'EventEspresso\core\services\commands\CommandFactory' => EE_Dependency_Map::load_from_cache, |
|
| 520 | + ), |
|
| 521 | + 'EventEspresso\core\services\commands\CommandFactory' => array( |
|
| 522 | + 'EventEspresso\core\services\loaders\Loader' => EE_Dependency_Map::load_from_cache, |
|
| 523 | + ), |
|
| 524 | + 'EventEspresso\core\services\commands\middleware\CapChecker' => array( |
|
| 525 | + 'EventEspresso\core\domain\services\capabilities\CapabilitiesChecker' => EE_Dependency_Map::load_from_cache, |
|
| 526 | + ), |
|
| 527 | + 'EventEspresso\core\domain\services\capabilities\CapabilitiesChecker' => array( |
|
| 528 | + 'EE_Capabilities' => EE_Dependency_Map::load_from_cache, |
|
| 529 | + ), |
|
| 530 | + 'EventEspresso\core\domain\services\capabilities\RegistrationsCapChecker' => array( |
|
| 531 | + 'EE_Capabilities' => EE_Dependency_Map::load_from_cache, |
|
| 532 | + ), |
|
| 533 | + 'EventEspresso\core\services\commands\registration\CreateRegistrationCommandHandler' => array( |
|
| 534 | + 'EventEspresso\core\domain\services\registration\CreateRegistrationService' => EE_Dependency_Map::load_from_cache, |
|
| 535 | + ), |
|
| 536 | + 'EventEspresso\core\services\commands\registration\CopyRegistrationDetailsCommandHandler' => array( |
|
| 537 | + 'EventEspresso\core\domain\services\registration\CopyRegistrationService' => EE_Dependency_Map::load_from_cache, |
|
| 538 | + ), |
|
| 539 | + 'EventEspresso\core\services\commands\registration\CopyRegistrationPaymentsCommandHandler' => array( |
|
| 540 | + 'EventEspresso\core\domain\services\registration\CopyRegistrationService' => EE_Dependency_Map::load_from_cache, |
|
| 541 | + ), |
|
| 542 | + 'EventEspresso\core\services\commands\registration\CancelRegistrationAndTicketLineItemCommandHandler' => array( |
|
| 543 | + 'EventEspresso\core\domain\services\registration\CancelTicketLineItemService' => EE_Dependency_Map::load_from_cache, |
|
| 544 | + ), |
|
| 545 | + 'EventEspresso\core\services\commands\registration\UpdateRegistrationAndTransactionAfterChangeCommandHandler' => array( |
|
| 546 | + 'EventEspresso\core\domain\services\registration\UpdateRegistrationService' => EE_Dependency_Map::load_from_cache, |
|
| 547 | + ), |
|
| 548 | + 'EventEspresso\core\services\commands\ticket\CreateTicketLineItemCommandHandler' => array( |
|
| 549 | + 'EventEspresso\core\domain\services\ticket\CreateTicketLineItemService' => EE_Dependency_Map::load_from_cache, |
|
| 550 | + ), |
|
| 551 | + 'EventEspresso\core\services\commands\ticket\CancelTicketLineItemCommandHandler' => array( |
|
| 552 | + 'EventEspresso\core\domain\services\ticket\CancelTicketLineItemService' => EE_Dependency_Map::load_from_cache, |
|
| 553 | + ), |
|
| 554 | + 'EventEspresso\core\domain\services\registration\CancelRegistrationService' => array( |
|
| 555 | + 'EventEspresso\core\domain\services\ticket\CancelTicketLineItemService' => EE_Dependency_Map::load_from_cache, |
|
| 556 | + ), |
|
| 557 | + 'EventEspresso\core\services\commands\attendee\CreateAttendeeCommandHandler' => array( |
|
| 558 | + 'EEM_Attendee' => EE_Dependency_Map::load_from_cache, |
|
| 559 | + ), |
|
| 560 | + 'EventEspresso\core\services\database\TableManager' => array( |
|
| 561 | + 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
| 562 | + ), |
|
| 563 | + 'EE_Data_Migration_Class_Base' => array( |
|
| 564 | + 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
| 565 | + 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
| 566 | + ), |
|
| 567 | + 'EE_DMS_Core_4_1_0' => array( |
|
| 568 | + 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
| 569 | + 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
| 570 | + ), |
|
| 571 | + 'EE_DMS_Core_4_2_0' => array( |
|
| 572 | + 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
| 573 | + 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
| 574 | + ), |
|
| 575 | + 'EE_DMS_Core_4_3_0' => array( |
|
| 576 | + 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
| 577 | + 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
| 578 | + ), |
|
| 579 | + 'EE_DMS_Core_4_4_0' => array( |
|
| 580 | + 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
| 581 | + 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
| 582 | + ), |
|
| 583 | + 'EE_DMS_Core_4_5_0' => array( |
|
| 584 | + 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
| 585 | + 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
| 586 | + ), |
|
| 587 | + 'EE_DMS_Core_4_6_0' => array( |
|
| 588 | + 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
| 589 | + 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
| 590 | + ), |
|
| 591 | + 'EE_DMS_Core_4_7_0' => array( |
|
| 592 | + 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
| 593 | + 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
| 594 | + ), |
|
| 595 | + 'EE_DMS_Core_4_8_0' => array( |
|
| 596 | + 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
| 597 | + 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
| 598 | + ), |
|
| 599 | + 'EE_DMS_Core_4_9_0' => array( |
|
| 600 | + 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
| 601 | + 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
| 602 | + ), |
|
| 603 | + 'EventEspresso\core\services\assets\I18nRegistry' => array( |
|
| 604 | + array(), |
|
| 605 | + 'EventEspresso\core\domain\Domain' => EE_Dependency_Map::load_from_cache, |
|
| 606 | + ), |
|
| 607 | + 'EventEspresso\core\services\assets\Registry' => array( |
|
| 608 | + 'EventEspresso\core\services\assets\AssetCollection' => EE_Dependency_Map::load_from_cache, |
|
| 609 | + 'EventEspresso\core\services\assets\I18nRegistry' => EE_Dependency_Map::load_from_cache, |
|
| 610 | + ), |
|
| 611 | + 'EventEspresso\core\domain\entities\shortcodes\EspressoCancelled' => array( |
|
| 612 | + 'EventEspresso\core\services\cache\PostRelatedCacheManager' => EE_Dependency_Map::load_from_cache, |
|
| 613 | + ), |
|
| 614 | + 'EventEspresso\core\domain\entities\shortcodes\EspressoCheckout' => array( |
|
| 615 | + 'EventEspresso\core\services\cache\PostRelatedCacheManager' => EE_Dependency_Map::load_from_cache, |
|
| 616 | + ), |
|
| 617 | + 'EventEspresso\core\domain\entities\shortcodes\EspressoEventAttendees' => array( |
|
| 618 | + 'EventEspresso\core\services\cache\PostRelatedCacheManager' => EE_Dependency_Map::load_from_cache, |
|
| 619 | + ), |
|
| 620 | + 'EventEspresso\core\domain\entities\shortcodes\EspressoEvents' => array( |
|
| 621 | + 'EventEspresso\core\services\cache\PostRelatedCacheManager' => EE_Dependency_Map::load_from_cache, |
|
| 622 | + ), |
|
| 623 | + 'EventEspresso\core\domain\entities\shortcodes\EspressoThankYou' => array( |
|
| 624 | + 'EventEspresso\core\services\cache\PostRelatedCacheManager' => EE_Dependency_Map::load_from_cache, |
|
| 625 | + ), |
|
| 626 | + 'EventEspresso\core\domain\entities\shortcodes\EspressoTicketSelector' => array( |
|
| 627 | + 'EventEspresso\core\services\cache\PostRelatedCacheManager' => EE_Dependency_Map::load_from_cache, |
|
| 628 | + ), |
|
| 629 | + 'EventEspresso\core\domain\entities\shortcodes\EspressoTxnPage' => array( |
|
| 630 | + 'EventEspresso\core\services\cache\PostRelatedCacheManager' => EE_Dependency_Map::load_from_cache, |
|
| 631 | + ), |
|
| 632 | + 'EventEspresso\core\services\cache\BasicCacheManager' => array( |
|
| 633 | + 'EventEspresso\core\services\cache\TransientCacheStorage' => EE_Dependency_Map::load_from_cache, |
|
| 634 | + ), |
|
| 635 | + 'EventEspresso\core\services\cache\PostRelatedCacheManager' => array( |
|
| 636 | + 'EventEspresso\core\services\cache\TransientCacheStorage' => EE_Dependency_Map::load_from_cache, |
|
| 637 | + ), |
|
| 638 | + 'EventEspresso\core\domain\services\validation\email\EmailValidationService' => array( |
|
| 639 | + 'EE_Registration_Config' => EE_Dependency_Map::load_from_cache, |
|
| 640 | + 'EventEspresso\core\services\loaders\Loader' => EE_Dependency_Map::load_from_cache, |
|
| 641 | + ), |
|
| 642 | + 'EventEspresso\core\domain\values\EmailAddress' => array( |
|
| 643 | + null, |
|
| 644 | + 'EventEspresso\core\domain\services\validation\email\EmailValidationService' => EE_Dependency_Map::load_from_cache, |
|
| 645 | + ), |
|
| 646 | + 'EventEspresso\core\services\orm\ModelFieldFactory' => array( |
|
| 647 | + 'EventEspresso\core\services\loaders\Loader' => EE_Dependency_Map::load_from_cache, |
|
| 648 | + ), |
|
| 649 | + 'LEGACY_MODELS' => array( |
|
| 650 | + null, |
|
| 651 | + 'EventEspresso\core\services\database\ModelFieldFactory' => EE_Dependency_Map::load_from_cache, |
|
| 652 | + ), |
|
| 653 | + 'EE_Module_Request_Router' => array( |
|
| 654 | + 'EE_Request' => EE_Dependency_Map::load_from_cache, |
|
| 655 | + ), |
|
| 656 | + 'EE_Registration_Processor' => array( |
|
| 657 | + 'EE_Request' => EE_Dependency_Map::load_from_cache, |
|
| 658 | + ), |
|
| 659 | + 'EventEspresso\core\services\notifications\PersistentAdminNoticeManager' => array( |
|
| 660 | + null, |
|
| 661 | + 'EventEspresso\core\domain\services\capabilities\CapabilitiesChecker' => EE_Dependency_Map::load_from_cache, |
|
| 662 | + 'EventEspresso\core\services\request\Request' => EE_Dependency_Map::load_from_cache, |
|
| 663 | + ), |
|
| 664 | + 'EventEspresso\core\services\licensing\LicenseService' => array( |
|
| 665 | + 'EventEspresso\core\domain\services\pue\Stats' => EE_Dependency_Map::load_from_cache, |
|
| 666 | + 'EventEspresso\core\domain\services\pue\Config' => EE_Dependency_Map::load_from_cache, |
|
| 667 | + ), |
|
| 668 | + 'EE_Admin_Transactions_List_Table' => array( |
|
| 669 | + null, |
|
| 670 | + 'EventEspresso\core\domain\values\session\SessionLifespan' => EE_Dependency_Map::load_from_cache, |
|
| 671 | + ), |
|
| 672 | + 'EventEspresso\core\domain\services\pue\Stats' => array( |
|
| 673 | + 'EventEspresso\core\domain\services\pue\Config' => EE_Dependency_Map::load_from_cache, |
|
| 674 | + 'EE_Maintenance_Mode' => EE_Dependency_Map::load_from_cache, |
|
| 675 | + 'EventEspresso\core\domain\services\pue\StatsGatherer' => EE_Dependency_Map::load_from_cache, |
|
| 676 | + ), |
|
| 677 | + 'EventEspresso\core\domain\services\pue\Config' => array( |
|
| 678 | + 'EE_Network_Config' => EE_Dependency_Map::load_from_cache, |
|
| 679 | + 'EE_Config' => EE_Dependency_Map::load_from_cache, |
|
| 680 | + ), |
|
| 681 | + 'EventEspresso\core\domain\services\pue\StatsGatherer' => array( |
|
| 682 | + 'EEM_Payment_Method' => EE_Dependency_Map::load_from_cache, |
|
| 683 | + 'EEM_Event' => EE_Dependency_Map::load_from_cache, |
|
| 684 | + 'EEM_Datetime' => EE_Dependency_Map::load_from_cache, |
|
| 685 | + 'EEM_Ticket' => EE_Dependency_Map::load_from_cache, |
|
| 686 | + 'EEM_Registration' => EE_Dependency_Map::load_from_cache, |
|
| 687 | + 'EEM_Transaction' => EE_Dependency_Map::load_from_cache, |
|
| 688 | + 'EE_Config' => EE_Dependency_Map::load_from_cache, |
|
| 689 | + ), |
|
| 690 | + 'EventEspresso\core\domain\services\admin\ExitModal' => array( |
|
| 691 | + 'EventEspresso\core\services\assets\Registry' => EE_Dependency_Map::load_from_cache, |
|
| 692 | + ), |
|
| 693 | + 'EventEspresso\core\domain\services\admin\PluginUpsells' => array( |
|
| 694 | + 'EventEspresso\core\domain\Domain' => EE_Dependency_Map::load_from_cache, |
|
| 695 | + ), |
|
| 696 | + 'EventEspresso\caffeinated\modules\recaptcha_invisible\InvisibleRecaptcha' => array( |
|
| 697 | + 'EE_Registration_Config' => EE_Dependency_Map::load_from_cache, |
|
| 698 | + 'EE_Session' => EE_Dependency_Map::load_from_cache, |
|
| 699 | + ), |
|
| 700 | + 'EventEspresso\caffeinated\modules\recaptcha_invisible\RecaptchaAdminSettings' => array( |
|
| 701 | + 'EE_Registration_Config' => EE_Dependency_Map::load_from_cache, |
|
| 702 | + ), |
|
| 703 | + 'EventEspresso\modules\ticket_selector\ProcessTicketSelector' => array( |
|
| 704 | + 'EE_Core_Config' => EE_Dependency_Map::load_from_cache, |
|
| 705 | + 'EventEspresso\core\services\request\Request' => EE_Dependency_Map::load_from_cache, |
|
| 706 | + 'EE_Session' => EE_Dependency_Map::load_from_cache, |
|
| 707 | + 'EEM_Ticket' => EE_Dependency_Map::load_from_cache, |
|
| 708 | + 'EventEspresso\modules\ticket_selector\TicketDatetimeAvailabilityTracker' => EE_Dependency_Map::load_from_cache, |
|
| 709 | + ), |
|
| 710 | + 'EventEspresso\modules\ticket_selector\TicketDatetimeAvailabilityTracker' => array( |
|
| 711 | + 'EEM_Datetime' => EE_Dependency_Map::load_from_cache, |
|
| 712 | + ), |
|
| 713 | + 'EventEspresso\core\domain\entities\custom_post_types\CustomPostTypeDefinitions' => array( |
|
| 714 | + 'EE_Core_Config' => EE_Dependency_Map::load_from_cache, |
|
| 715 | + 'EventEspresso\core\services\loaders\Loader' => EE_Dependency_Map::load_from_cache, |
|
| 716 | + ), |
|
| 717 | + 'EventEspresso\core\domain\services\custom_post_types\RegisterCustomPostTypes' => array( |
|
| 718 | + 'EventEspresso\core\domain\entities\custom_post_types\CustomPostTypeDefinitions' => EE_Dependency_Map::load_from_cache, |
|
| 719 | + ), |
|
| 720 | + 'EventEspresso\core\domain\services\custom_post_types\RegisterCustomTaxonomies' => array( |
|
| 721 | + 'EventEspresso\core\domain\entities\custom_post_types\CustomTaxonomyDefinitions' => EE_Dependency_Map::load_from_cache, |
|
| 722 | + ), |
|
| 723 | + 'EE_CPT_Strategy' => array( |
|
| 724 | + 'EventEspresso\core\domain\entities\custom_post_types\CustomPostTypeDefinitions' => EE_Dependency_Map::load_from_cache, |
|
| 725 | + 'EventEspresso\core\domain\entities\custom_post_types\CustomTaxonomyDefinitions' => EE_Dependency_Map::load_from_cache, |
|
| 726 | + ), |
|
| 727 | + 'EventEspresso\core\services\loaders\ObjectIdentifier' => array( |
|
| 728 | + 'EventEspresso\core\services\loaders\ClassInterfaceCache' => EE_Dependency_Map::load_from_cache, |
|
| 729 | + ), |
|
| 730 | + 'EventEspresso\core\domain\services\assets\CoreAssetManager' => array( |
|
| 731 | + 'EventEspresso\core\services\assets\AssetCollection' => EE_Dependency_Map::load_from_cache, |
|
| 732 | + 'EE_Currency_Config' => EE_Dependency_Map::load_from_cache, |
|
| 733 | + 'EE_Template_Config' => EE_Dependency_Map::load_from_cache, |
|
| 734 | + 'EventEspresso\core\domain\Domain' => EE_Dependency_Map::load_from_cache, |
|
| 735 | + 'EventEspresso\core\services\assets\Registry' => EE_Dependency_Map::load_from_cache, |
|
| 736 | + ), |
|
| 737 | + 'EventEspresso\core\domain\services\admin\privacy\policy\PrivacyPolicy' => array( |
|
| 738 | + 'EEM_Payment_Method' => EE_Dependency_Map::load_from_cache, |
|
| 739 | + 'EventEspresso\core\domain\values\session\SessionLifespan' => EE_Dependency_Map::load_from_cache |
|
| 740 | + ), |
|
| 741 | + 'EventEspresso\core\domain\services\admin\privacy\export\ExportAttendee' => array( |
|
| 742 | + 'EEM_Attendee' => EE_Dependency_Map::load_from_cache, |
|
| 743 | + ), |
|
| 744 | + 'EventEspresso\core\domain\services\admin\privacy\export\ExportAttendeeBillingData' => array( |
|
| 745 | + 'EEM_Attendee' => EE_Dependency_Map::load_from_cache, |
|
| 746 | + 'EEM_Payment_Method' => EE_Dependency_Map::load_from_cache |
|
| 747 | + ), |
|
| 748 | + 'EventEspresso\core\domain\services\admin\privacy\export\ExportCheckins' => array( |
|
| 749 | + 'EEM_Checkin' => EE_Dependency_Map::load_from_cache, |
|
| 750 | + ), |
|
| 751 | + 'EventEspresso\core\domain\services\admin\privacy\export\ExportRegistration' => array( |
|
| 752 | + 'EEM_Registration' => EE_Dependency_Map::load_from_cache, |
|
| 753 | + ), |
|
| 754 | + 'EventEspresso\core\domain\services\admin\privacy\export\ExportTransaction' => array( |
|
| 755 | + 'EEM_Transaction' => EE_Dependency_Map::load_from_cache, |
|
| 756 | + ), |
|
| 757 | + 'EventEspresso\core\domain\services\admin\privacy\erasure\EraseAttendeeData' => array( |
|
| 758 | + 'EEM_Attendee' => EE_Dependency_Map::load_from_cache, |
|
| 759 | + ), |
|
| 760 | + 'EventEspresso\core\domain\services\admin\privacy\erasure\EraseAnswers' => array( |
|
| 761 | + 'EEM_Answer' => EE_Dependency_Map::load_from_cache, |
|
| 762 | + 'EEM_Question' => EE_Dependency_Map::load_from_cache, |
|
| 763 | + ), |
|
| 764 | + 'EventEspresso\core\CPTs\CptQueryModifier' => array( |
|
| 765 | + null, |
|
| 766 | + null, |
|
| 767 | + null, |
|
| 768 | + 'EE_Request_Handler' => EE_Dependency_Map::load_from_cache, |
|
| 769 | + 'EventEspresso\core\services\request\Request' => EE_Dependency_Map::load_from_cache, |
|
| 770 | + 'EventEspresso\core\services\loaders\Loader' => EE_Dependency_Map::load_from_cache, |
|
| 771 | + ), |
|
| 772 | + 'EventEspresso\core\domain\services\admin\privacy\forms\PrivacySettingsFormHandler' => array( |
|
| 773 | + 'EE_Registry' => EE_Dependency_Map::load_from_cache, |
|
| 774 | + 'EE_Config' => EE_Dependency_Map::load_from_cache |
|
| 775 | + ), |
|
| 776 | + 'EventEspresso\core\services\editor\BlockRegistrationManager' => array( |
|
| 777 | + 'EventEspresso\core\services\assets\BlockAssetManagerCollection' => EE_Dependency_Map::load_from_cache, |
|
| 778 | + 'EventEspresso\core\domain\entities\editor\BlockCollection' => EE_Dependency_Map::load_from_cache, |
|
| 779 | + 'EventEspresso\core\services\route_match\RouteMatchSpecificationManager' => EE_Dependency_Map::load_from_cache, |
|
| 780 | + 'EventEspresso\core\services\request\Request' => EE_Dependency_Map::load_from_cache, |
|
| 781 | + ), |
|
| 782 | + 'EventEspresso\core\domain\entities\editor\CoreBlocksAssetManager' => array( |
|
| 783 | + 'EventEspresso\core\domain\Domain' => EE_Dependency_Map::load_from_cache, |
|
| 784 | + 'EventEspresso\core\services\assets\AssetCollection' => EE_Dependency_Map::load_from_cache, |
|
| 785 | + 'EventEspresso\core\services\assets\Registry' => EE_Dependency_Map::load_from_cache, |
|
| 786 | + ), |
|
| 787 | + 'EventEspresso\core\domain\services\blocks\EventAttendeesBlockRenderer' => array( |
|
| 788 | + 'EventEspresso\core\domain\Domain' => EE_Dependency_Map::load_from_cache, |
|
| 789 | + 'EEM_Attendee' => EE_Dependency_Map::load_from_cache, |
|
| 790 | + ), |
|
| 791 | + 'EventEspresso\core\domain\entities\editor\blocks\EventAttendees' => array( |
|
| 792 | + 'EventEspresso\core\domain\entities\editor\CoreBlocksAssetManager' => self::load_from_cache, |
|
| 793 | + 'EventEspresso\core\services\request\Request' => EE_Dependency_Map::load_from_cache, |
|
| 794 | + 'EventEspresso\core\domain\services\blocks\EventAttendeesBlockRenderer' => self::load_from_cache, |
|
| 795 | + ), |
|
| 796 | + 'EventEspresso\core\services\route_match\RouteMatchSpecificationDependencyResolver' => array( |
|
| 797 | + 'EventEspresso\core\services\container\Mirror' => EE_Dependency_Map::load_from_cache, |
|
| 798 | + 'EventEspresso\core\services\loaders\ClassInterfaceCache' => EE_Dependency_Map::load_from_cache, |
|
| 799 | + 'EE_Dependency_Map' => EE_Dependency_Map::load_from_cache, |
|
| 800 | + ), |
|
| 801 | + 'EventEspresso\core\services\route_match\RouteMatchSpecificationFactory' => array( |
|
| 802 | + 'EventEspresso\core\services\route_match\RouteMatchSpecificationDependencyResolver' => EE_Dependency_Map::load_from_cache, |
|
| 803 | + 'EventEspresso\core\services\loaders\Loader' => EE_Dependency_Map::load_from_cache, |
|
| 804 | + ), |
|
| 805 | + 'EventEspresso\core\services\route_match\RouteMatchSpecificationManager' => array( |
|
| 806 | + 'EventEspresso\core\services\route_match\RouteMatchSpecificationCollection' => EE_Dependency_Map::load_from_cache, |
|
| 807 | + 'EventEspresso\core\services\route_match\RouteMatchSpecificationFactory' => EE_Dependency_Map::load_from_cache, |
|
| 808 | + ), |
|
| 809 | + 'EventEspresso\core\libraries\rest_api\CalculatedModelFields' => array( |
|
| 810 | + 'EventEspresso\core\libraries\rest_api\calculations\CalculatedModelFieldsFactory' => EE_Dependency_Map::load_from_cache |
|
| 811 | + ), |
|
| 812 | + 'EventEspresso\core\libraries\rest_api\calculations\CalculatedModelFieldsFactory' => array( |
|
| 813 | + 'EventEspresso\core\services\loaders\Loader' => EE_Dependency_Map::load_from_cache, |
|
| 814 | + ), |
|
| 815 | + 'EventEspresso\core\libraries\rest_api\controllers\model\Read' => array( |
|
| 816 | + 'EventEspresso\core\libraries\rest_api\CalculatedModelFields' => EE_Dependency_Map::load_from_cache |
|
| 817 | + ), |
|
| 818 | + 'EventEspresso\core\libraries\rest_api\calculations\Datetime' => array( |
|
| 819 | + 'EEM_Datetime' => EE_Dependency_Map::load_from_cache, |
|
| 820 | + 'EEM_Registration' => EE_Dependency_Map::load_from_cache |
|
| 821 | + ), |
|
| 822 | + 'EventEspresso\core\libraries\rest_api\calculations\Event' => array( |
|
| 823 | + 'EEM_Event' => EE_Dependency_Map::load_from_cache, |
|
| 824 | + 'EEM_Registration' => EE_Dependency_Map::load_from_cache |
|
| 825 | + ), |
|
| 826 | + 'EventEspresso\core\libraries\rest_api\calculations\Registration' => array( |
|
| 827 | + 'EEM_Registration' => EE_Dependency_Map::load_from_cache |
|
| 828 | + ), |
|
| 829 | + 'EventEspresso\core\services\session\SessionStartHandler' => array( |
|
| 830 | + 'EventEspresso\core\services\request\Request' => EE_Dependency_Map::load_from_cache, |
|
| 831 | + ), |
|
| 832 | + 'EE_URL_Validation_Strategy' => array( |
|
| 833 | + null, |
|
| 834 | + null, |
|
| 835 | + 'EventEspresso\core\services\validators\URLValidator' => EE_Dependency_Map::load_from_cache |
|
| 836 | + ), |
|
| 837 | + 'EventEspresso\admin_pages\general_settings\OrganizationSettings' => array( |
|
| 838 | + 'EE_Registry' => EE_Dependency_Map::load_from_cache, |
|
| 839 | + 'EE_Organization_Config' => EE_Dependency_Map::load_from_cache, |
|
| 840 | + 'EE_Core_Config' => EE_Dependency_Map::load_from_cache, |
|
| 841 | + 'EE_Network_Core_Config' => EE_Dependency_Map::load_from_cache, |
|
| 842 | + 'EventEspresso\core\services\address\CountrySubRegionDao' => EE_Dependency_Map::load_from_cache, |
|
| 843 | + ), |
|
| 844 | + 'EventEspresso\core\services\address\CountrySubRegionDao' => array( |
|
| 845 | + 'EEM_State' => EE_Dependency_Map::load_from_cache, |
|
| 846 | + 'EventEspresso\core\services\validators\JsonValidator' => EE_Dependency_Map::load_from_cache |
|
| 847 | + ), |
|
| 848 | + 'EventEspresso\core\domain\services\admin\ajax\WordpressHeartbeat' => array( |
|
| 849 | + 'EventEspresso\core\services\loaders\Loader' => EE_Dependency_Map::load_from_cache, |
|
| 850 | + 'EventEspresso\core\services\request\Request' => EE_Dependency_Map::load_from_cache, |
|
| 851 | + ), |
|
| 852 | + 'EventEspresso\core\domain\services\admin\ajax\EventEditorHeartbeat' => array( |
|
| 853 | + 'EventEspresso\core\domain\Domain' => EE_Dependency_Map::load_from_cache, |
|
| 854 | + 'EE_Environment_Config' => EE_Dependency_Map::load_from_cache, |
|
| 855 | + ), |
|
| 856 | + 'EventEspresso\core\services\request\files\FilesDataHandler' => array( |
|
| 857 | + 'EventEspresso\core\services\request\Request' => EE_Dependency_Map::load_from_cache, |
|
| 858 | + ), |
|
| 859 | + 'EventEspressoBatchRequest\BatchRequestProcessor' => [ |
|
| 860 | + 'EventEspresso\core\services\loaders\Loader' => EE_Dependency_Map::load_from_cache, |
|
| 861 | + ] |
|
| 862 | + ); |
|
| 863 | + } |
|
| 864 | + |
|
| 865 | + |
|
| 866 | + /** |
|
| 867 | + * Registers how core classes are loaded. |
|
| 868 | + * This can either be done by simply providing the name of one of the EE_Registry loader methods such as: |
|
| 869 | + * 'EE_Request_Handler' => 'load_core' |
|
| 870 | + * 'EE_Messages_Queue' => 'load_lib' |
|
| 871 | + * 'EEH_Debug_Tools' => 'load_helper' |
|
| 872 | + * or, if greater control is required, by providing a custom closure. For example: |
|
| 873 | + * 'Some_Class' => function () { |
|
| 874 | + * return new Some_Class(); |
|
| 875 | + * }, |
|
| 876 | + * This is required for instantiating dependencies |
|
| 877 | + * where an interface has been type hinted in a class constructor. For example: |
|
| 878 | + * 'Required_Interface' => function () { |
|
| 879 | + * return new A_Class_That_Implements_Required_Interface(); |
|
| 880 | + * }, |
|
| 881 | + */ |
|
| 882 | + protected function _register_core_class_loaders() |
|
| 883 | + { |
|
| 884 | + $this->_class_loaders = array( |
|
| 885 | + // load_core |
|
| 886 | + 'EE_Dependency_Map' => function () { |
|
| 887 | + return $this; |
|
| 888 | + }, |
|
| 889 | + 'EE_Capabilities' => 'load_core', |
|
| 890 | + 'EE_Encryption' => 'load_core', |
|
| 891 | + 'EE_Front_Controller' => 'load_core', |
|
| 892 | + 'EE_Module_Request_Router' => 'load_core', |
|
| 893 | + 'EE_Registry' => 'load_core', |
|
| 894 | + 'EE_Request' => function () { |
|
| 895 | + return $this->legacy_request; |
|
| 896 | + }, |
|
| 897 | + 'EventEspresso\core\services\request\Request' => function () { |
|
| 898 | + return $this->request; |
|
| 899 | + }, |
|
| 900 | + 'EventEspresso\core\services\request\Response' => function () { |
|
| 901 | + return $this->response; |
|
| 902 | + }, |
|
| 903 | + 'EE_Base' => 'load_core', |
|
| 904 | + 'EE_Request_Handler' => 'load_core', |
|
| 905 | + 'EE_Session' => 'load_core', |
|
| 906 | + 'EE_Cron_Tasks' => 'load_core', |
|
| 907 | + 'EE_System' => 'load_core', |
|
| 908 | + 'EE_Maintenance_Mode' => 'load_core', |
|
| 909 | + 'EE_Register_CPTs' => 'load_core', |
|
| 910 | + 'EE_Admin' => 'load_core', |
|
| 911 | + 'EE_CPT_Strategy' => 'load_core', |
|
| 912 | + // load_class |
|
| 913 | + 'EE_Registration_Processor' => 'load_class', |
|
| 914 | + // load_lib |
|
| 915 | + 'EE_Message_Resource_Manager' => 'load_lib', |
|
| 916 | + 'EE_Message_Type_Collection' => 'load_lib', |
|
| 917 | + 'EE_Message_Type_Collection_Loader' => 'load_lib', |
|
| 918 | + 'EE_Messenger_Collection' => 'load_lib', |
|
| 919 | + 'EE_Messenger_Collection_Loader' => 'load_lib', |
|
| 920 | + 'EE_Messages_Processor' => 'load_lib', |
|
| 921 | + 'EE_Message_Repository' => 'load_lib', |
|
| 922 | + 'EE_Messages_Queue' => 'load_lib', |
|
| 923 | + 'EE_Messages_Data_Handler_Collection' => 'load_lib', |
|
| 924 | + 'EE_Message_Template_Group_Collection' => 'load_lib', |
|
| 925 | + 'EE_Payment_Method_Manager' => 'load_lib', |
|
| 926 | + 'EE_Messages_Generator' => function () { |
|
| 927 | + return EE_Registry::instance()->load_lib( |
|
| 928 | + 'Messages_Generator', |
|
| 929 | + array(), |
|
| 930 | + false, |
|
| 931 | + false |
|
| 932 | + ); |
|
| 933 | + }, |
|
| 934 | + 'EE_Messages_Template_Defaults' => function ($arguments = array()) { |
|
| 935 | + return EE_Registry::instance()->load_lib( |
|
| 936 | + 'Messages_Template_Defaults', |
|
| 937 | + $arguments, |
|
| 938 | + false, |
|
| 939 | + false |
|
| 940 | + ); |
|
| 941 | + }, |
|
| 942 | + // load_helper |
|
| 943 | + 'EEH_Parse_Shortcodes' => function () { |
|
| 944 | + if (EE_Registry::instance()->load_helper('Parse_Shortcodes')) { |
|
| 945 | + return new EEH_Parse_Shortcodes(); |
|
| 946 | + } |
|
| 947 | + return null; |
|
| 948 | + }, |
|
| 949 | + 'EE_Template_Config' => function () { |
|
| 950 | + return EE_Config::instance()->template_settings; |
|
| 951 | + }, |
|
| 952 | + 'EE_Currency_Config' => function () { |
|
| 953 | + return EE_Config::instance()->currency; |
|
| 954 | + }, |
|
| 955 | + 'EE_Registration_Config' => function () { |
|
| 956 | + return EE_Config::instance()->registration; |
|
| 957 | + }, |
|
| 958 | + 'EE_Core_Config' => function () { |
|
| 959 | + return EE_Config::instance()->core; |
|
| 960 | + }, |
|
| 961 | + 'EventEspresso\core\services\loaders\Loader' => function () { |
|
| 962 | + return LoaderFactory::getLoader(); |
|
| 963 | + }, |
|
| 964 | + 'EE_Network_Config' => function () { |
|
| 965 | + return EE_Network_Config::instance(); |
|
| 966 | + }, |
|
| 967 | + 'EE_Config' => function () { |
|
| 968 | + return EE_Config::instance(); |
|
| 969 | + }, |
|
| 970 | + 'EventEspresso\core\domain\Domain' => function () { |
|
| 971 | + return DomainFactory::getEventEspressoCoreDomain(); |
|
| 972 | + }, |
|
| 973 | + 'EE_Admin_Config' => function () { |
|
| 974 | + return EE_Config::instance()->admin; |
|
| 975 | + }, |
|
| 976 | + 'EE_Organization_Config' => function () { |
|
| 977 | + return EE_Config::instance()->organization; |
|
| 978 | + }, |
|
| 979 | + 'EE_Network_Core_Config' => function () { |
|
| 980 | + return EE_Network_Config::instance()->core; |
|
| 981 | + }, |
|
| 982 | + 'EE_Environment_Config' => function () { |
|
| 983 | + return EE_Config::instance()->environment; |
|
| 984 | + }, |
|
| 985 | + ); |
|
| 986 | + } |
|
| 987 | + |
|
| 988 | + |
|
| 989 | + /** |
|
| 990 | + * can be used for supplying alternate names for classes, |
|
| 991 | + * or for connecting interface names to instantiable classes |
|
| 992 | + */ |
|
| 993 | + protected function _register_core_aliases() |
|
| 994 | + { |
|
| 995 | + $aliases = array( |
|
| 996 | + 'CommandBusInterface' => 'EventEspresso\core\services\commands\CommandBusInterface', |
|
| 997 | + 'EventEspresso\core\services\commands\CommandBusInterface' => 'EventEspresso\core\services\commands\CommandBus', |
|
| 998 | + 'CommandHandlerManagerInterface' => 'EventEspresso\core\services\commands\CommandHandlerManagerInterface', |
|
| 999 | + 'EventEspresso\core\services\commands\CommandHandlerManagerInterface' => 'EventEspresso\core\services\commands\CommandHandlerManager', |
|
| 1000 | + 'CapChecker' => 'EventEspresso\core\services\commands\middleware\CapChecker', |
|
| 1001 | + 'AddActionHook' => 'EventEspresso\core\services\commands\middleware\AddActionHook', |
|
| 1002 | + 'CapabilitiesChecker' => 'EventEspresso\core\domain\services\capabilities\CapabilitiesChecker', |
|
| 1003 | + 'CapabilitiesCheckerInterface' => 'EventEspresso\core\domain\services\capabilities\CapabilitiesCheckerInterface', |
|
| 1004 | + 'EventEspresso\core\domain\services\capabilities\CapabilitiesCheckerInterface' => 'EventEspresso\core\domain\services\capabilities\CapabilitiesChecker', |
|
| 1005 | + 'CreateRegistrationService' => 'EventEspresso\core\domain\services\registration\CreateRegistrationService', |
|
| 1006 | + 'CreateRegistrationCommandHandler' => 'EventEspresso\core\services\commands\registration\CreateRegistrationCommand', |
|
| 1007 | + 'CopyRegistrationDetailsCommandHandler' => 'EventEspresso\core\services\commands\registration\CopyRegistrationDetailsCommand', |
|
| 1008 | + 'CopyRegistrationPaymentsCommandHandler' => 'EventEspresso\core\services\commands\registration\CopyRegistrationPaymentsCommand', |
|
| 1009 | + 'CancelRegistrationAndTicketLineItemCommandHandler' => 'EventEspresso\core\services\commands\registration\CancelRegistrationAndTicketLineItemCommandHandler', |
|
| 1010 | + 'UpdateRegistrationAndTransactionAfterChangeCommandHandler' => 'EventEspresso\core\services\commands\registration\UpdateRegistrationAndTransactionAfterChangeCommandHandler', |
|
| 1011 | + 'CreateTicketLineItemCommandHandler' => 'EventEspresso\core\services\commands\ticket\CreateTicketLineItemCommand', |
|
| 1012 | + 'CreateTransactionCommandHandler' => 'EventEspresso\core\services\commands\transaction\CreateTransactionCommandHandler', |
|
| 1013 | + 'CreateAttendeeCommandHandler' => 'EventEspresso\core\services\commands\attendee\CreateAttendeeCommandHandler', |
|
| 1014 | + 'TableManager' => 'EventEspresso\core\services\database\TableManager', |
|
| 1015 | + 'TableAnalysis' => 'EventEspresso\core\services\database\TableAnalysis', |
|
| 1016 | + 'EspressoShortcode' => 'EventEspresso\core\services\shortcodes\EspressoShortcode', |
|
| 1017 | + 'ShortcodeInterface' => 'EventEspresso\core\services\shortcodes\ShortcodeInterface', |
|
| 1018 | + 'EventEspresso\core\services\shortcodes\ShortcodeInterface' => 'EventEspresso\core\services\shortcodes\EspressoShortcode', |
|
| 1019 | + 'EventEspresso\core\services\cache\CacheStorageInterface' => 'EventEspresso\core\services\cache\TransientCacheStorage', |
|
| 1020 | + 'LoaderInterface' => 'EventEspresso\core\services\loaders\LoaderInterface', |
|
| 1021 | + 'EventEspresso\core\services\loaders\LoaderInterface' => 'EventEspresso\core\services\loaders\Loader', |
|
| 1022 | + 'CommandFactoryInterface' => 'EventEspresso\core\services\commands\CommandFactoryInterface', |
|
| 1023 | + 'EventEspresso\core\services\commands\CommandFactoryInterface' => 'EventEspresso\core\services\commands\CommandFactory', |
|
| 1024 | + 'EmailValidatorInterface' => 'EventEspresso\core\domain\services\validation\email\EmailValidatorInterface', |
|
| 1025 | + 'EventEspresso\core\domain\services\validation\email\EmailValidatorInterface' => 'EventEspresso\core\domain\services\validation\email\EmailValidationService', |
|
| 1026 | + 'NoticeConverterInterface' => 'EventEspresso\core\services\notices\NoticeConverterInterface', |
|
| 1027 | + 'EventEspresso\core\services\notices\NoticeConverterInterface' => 'EventEspresso\core\services\notices\ConvertNoticesToEeErrors', |
|
| 1028 | + 'NoticesContainerInterface' => 'EventEspresso\core\services\notices\NoticesContainerInterface', |
|
| 1029 | + 'EventEspresso\core\services\notices\NoticesContainerInterface' => 'EventEspresso\core\services\notices\NoticesContainer', |
|
| 1030 | + 'EventEspresso\core\services\request\RequestInterface' => 'EventEspresso\core\services\request\Request', |
|
| 1031 | + 'EventEspresso\core\services\request\ResponseInterface' => 'EventEspresso\core\services\request\Response', |
|
| 1032 | + 'EventEspresso\core\domain\DomainInterface' => 'EventEspresso\core\domain\Domain', |
|
| 1033 | + 'Registration_Processor' => 'EE_Registration_Processor', |
|
| 1034 | + ); |
|
| 1035 | + foreach ($aliases as $alias => $fqn) { |
|
| 1036 | + if (is_array($fqn)) { |
|
| 1037 | + foreach ($fqn as $class => $for_class) { |
|
| 1038 | + $this->class_cache->addAlias($class, $alias, $for_class); |
|
| 1039 | + } |
|
| 1040 | + continue; |
|
| 1041 | + } |
|
| 1042 | + $this->class_cache->addAlias($fqn, $alias); |
|
| 1043 | + } |
|
| 1044 | + if (! (defined('DOING_AJAX') && DOING_AJAX) && is_admin()) { |
|
| 1045 | + $this->class_cache->addAlias( |
|
| 1046 | + 'EventEspresso\core\services\notices\ConvertNoticesToAdminNotices', |
|
| 1047 | + 'EventEspresso\core\services\notices\NoticeConverterInterface' |
|
| 1048 | + ); |
|
| 1049 | + } |
|
| 1050 | + } |
|
| 1051 | + |
|
| 1052 | + |
|
| 1053 | + /** |
|
| 1054 | + * This is used to reset the internal map and class_loaders to their original default state at the beginning of the |
|
| 1055 | + * request Primarily used by unit tests. |
|
| 1056 | + */ |
|
| 1057 | + public function reset() |
|
| 1058 | + { |
|
| 1059 | + $this->_register_core_class_loaders(); |
|
| 1060 | + $this->_register_core_dependencies(); |
|
| 1061 | + } |
|
| 1062 | + |
|
| 1063 | + |
|
| 1064 | + /** |
|
| 1065 | + * PLZ NOTE: a better name for this method would be is_alias() |
|
| 1066 | + * because it returns TRUE if the provided fully qualified name IS an alias |
|
| 1067 | + * WHY? |
|
| 1068 | + * Because if a class is type hinting for a concretion, |
|
| 1069 | + * then why would we need to find another class to supply it? |
|
| 1070 | + * ie: if a class asks for `Fully/Qualified/Namespace/SpecificClassName`, |
|
| 1071 | + * then give it an instance of `Fully/Qualified/Namespace/SpecificClassName`. |
|
| 1072 | + * Don't go looking for some substitute. |
|
| 1073 | + * Whereas if a class is type hinting for an interface... |
|
| 1074 | + * then we need to find an actual class to use. |
|
| 1075 | + * So the interface IS the alias for some other FQN, |
|
| 1076 | + * and we need to find out if `Fully/Qualified/Namespace/SomeInterface` |
|
| 1077 | + * represents some other class. |
|
| 1078 | + * |
|
| 1079 | + * @deprecated 4.9.62.p |
|
| 1080 | + * @param string $fqn |
|
| 1081 | + * @param string $for_class |
|
| 1082 | + * @return bool |
|
| 1083 | + */ |
|
| 1084 | + public function has_alias($fqn = '', $for_class = '') |
|
| 1085 | + { |
|
| 1086 | + return $this->isAlias($fqn, $for_class); |
|
| 1087 | + } |
|
| 1088 | + |
|
| 1089 | + |
|
| 1090 | + /** |
|
| 1091 | + * PLZ NOTE: a better name for this method would be get_fqn_for_alias() |
|
| 1092 | + * because it returns a FQN for provided alias if one exists, otherwise returns the original $alias |
|
| 1093 | + * functions recursively, so that multiple aliases can be used to drill down to a FQN |
|
| 1094 | + * for example: |
|
| 1095 | + * if the following two entries were added to the _aliases array: |
|
| 1096 | + * array( |
|
| 1097 | + * 'interface_alias' => 'some\namespace\interface' |
|
| 1098 | + * 'some\namespace\interface' => 'some\namespace\classname' |
|
| 1099 | + * ) |
|
| 1100 | + * then one could use EE_Registry::instance()->create( 'interface_alias' ) |
|
| 1101 | + * to load an instance of 'some\namespace\classname' |
|
| 1102 | + * |
|
| 1103 | + * @deprecated 4.9.62.p |
|
| 1104 | + * @param string $alias |
|
| 1105 | + * @param string $for_class |
|
| 1106 | + * @return string |
|
| 1107 | + */ |
|
| 1108 | + public function get_alias($alias = '', $for_class = '') |
|
| 1109 | + { |
|
| 1110 | + return $this->getFqnForAlias($alias, $for_class); |
|
| 1111 | + } |
|
| 1112 | 1112 | } |