@@ -16,1376 +16,1376 @@ |
||
| 16 | 16 | class EE_Registry |
| 17 | 17 | { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * EE_Registry Object |
|
| 21 | - * |
|
| 22 | - * @var EE_Registry $_instance |
|
| 23 | - * @access private |
|
| 24 | - */ |
|
| 25 | - private static $_instance = null; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * @var EE_Dependency_Map $_dependency_map |
|
| 29 | - * @access protected |
|
| 30 | - */ |
|
| 31 | - protected $_dependency_map = null; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * @var array $_class_abbreviations |
|
| 35 | - * @access protected |
|
| 36 | - */ |
|
| 37 | - protected $_class_abbreviations = array(); |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * @access public |
|
| 41 | - * @var \EventEspresso\core\services\commands\CommandBusInterface $BUS |
|
| 42 | - */ |
|
| 43 | - public $BUS; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * EE_Cart Object |
|
| 47 | - * |
|
| 48 | - * @access public |
|
| 49 | - * @var EE_Cart $CART |
|
| 50 | - */ |
|
| 51 | - public $CART = null; |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * EE_Config Object |
|
| 55 | - * |
|
| 56 | - * @access public |
|
| 57 | - * @var EE_Config $CFG |
|
| 58 | - */ |
|
| 59 | - public $CFG = null; |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * EE_Network_Config Object |
|
| 63 | - * |
|
| 64 | - * @access public |
|
| 65 | - * @var EE_Network_Config $NET_CFG |
|
| 66 | - */ |
|
| 67 | - public $NET_CFG = null; |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * StdClass object for storing library classes in |
|
| 71 | - * |
|
| 72 | - * @public LIB |
|
| 73 | - * @var StdClass $LIB |
|
| 74 | - */ |
|
| 75 | - public $LIB = null; |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * EE_Request_Handler Object |
|
| 79 | - * |
|
| 80 | - * @access public |
|
| 81 | - * @var EE_Request_Handler $REQ |
|
| 82 | - */ |
|
| 83 | - public $REQ = null; |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * EE_Session Object |
|
| 87 | - * |
|
| 88 | - * @access public |
|
| 89 | - * @var EE_Session $SSN |
|
| 90 | - */ |
|
| 91 | - public $SSN = null; |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * holds the ee capabilities object. |
|
| 95 | - * |
|
| 96 | - * @since 4.5.0 |
|
| 97 | - * @var EE_Capabilities |
|
| 98 | - */ |
|
| 99 | - public $CAP = null; |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * holds the EE_Message_Resource_Manager object. |
|
| 103 | - * |
|
| 104 | - * @since 4.9.0 |
|
| 105 | - * @var EE_Message_Resource_Manager |
|
| 106 | - */ |
|
| 107 | - public $MRM = null; |
|
| 108 | - |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * Holds the Assets Registry instance |
|
| 112 | - * @var Registry |
|
| 113 | - */ |
|
| 114 | - public $AssetsRegistry = null; |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * $addons - StdClass object for holding addons which have registered themselves to work with EE core |
|
| 118 | - * |
|
| 119 | - * @access public |
|
| 120 | - * @var EE_Addon[] |
|
| 121 | - */ |
|
| 122 | - public $addons = null; |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * $models |
|
| 126 | - * @access public |
|
| 127 | - * @var string[] $models keys are 'short names' (eg Event), values are class names (eg 'EEM_Event') |
|
| 128 | - */ |
|
| 129 | - public $models = array(); |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * $modules |
|
| 133 | - * @access public |
|
| 134 | - * @var EED_Module[] $modules |
|
| 135 | - */ |
|
| 136 | - public $modules = null; |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * $shortcodes |
|
| 140 | - * @access public |
|
| 141 | - * @var EES_Shortcode[] $shortcodes |
|
| 142 | - */ |
|
| 143 | - public $shortcodes = null; |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * $widgets |
|
| 147 | - * @access public |
|
| 148 | - * @var WP_Widget[] $widgets |
|
| 149 | - */ |
|
| 150 | - public $widgets = null; |
|
| 151 | - |
|
| 152 | - /** |
|
| 153 | - * $non_abstract_db_models |
|
| 154 | - * @access public |
|
| 155 | - * @var array this is an array of all implemented model names (i.e. not the parent abstract models, or models |
|
| 156 | - * which don't actually fetch items from the DB in the normal way (ie, are not children of EEM_Base)). |
|
| 157 | - * Keys are model "short names" (eg "Event") as used in model relations, and values are |
|
| 158 | - * classnames (eg "EEM_Event") |
|
| 159 | - */ |
|
| 160 | - public $non_abstract_db_models = array(); |
|
| 161 | - |
|
| 162 | - |
|
| 163 | - /** |
|
| 164 | - * $i18n_js_strings - internationalization for JS strings |
|
| 165 | - * usage: EE_Registry::i18n_js_strings['string_key'] = __( 'string to translate.', 'event_espresso' ); |
|
| 166 | - * in js file: var translatedString = eei18n.string_key; |
|
| 167 | - * |
|
| 168 | - * @access public |
|
| 169 | - * @var array |
|
| 170 | - */ |
|
| 171 | - public static $i18n_js_strings = array(); |
|
| 172 | - |
|
| 173 | - |
|
| 174 | - /** |
|
| 175 | - * $main_file - path to espresso.php |
|
| 176 | - * |
|
| 177 | - * @access public |
|
| 178 | - * @var array |
|
| 179 | - */ |
|
| 180 | - public $main_file; |
|
| 181 | - |
|
| 182 | - /** |
|
| 183 | - * array of ReflectionClass objects where the key is the class name |
|
| 184 | - * |
|
| 185 | - * @access public |
|
| 186 | - * @var ReflectionClass[] |
|
| 187 | - */ |
|
| 188 | - public $_reflectors; |
|
| 189 | - |
|
| 190 | - /** |
|
| 191 | - * boolean flag to indicate whether or not to load/save dependencies from/to the cache |
|
| 192 | - * |
|
| 193 | - * @access protected |
|
| 194 | - * @var boolean $_cache_on |
|
| 195 | - */ |
|
| 196 | - protected $_cache_on = true; |
|
| 197 | - |
|
| 198 | - |
|
| 199 | - |
|
| 200 | - /** |
|
| 201 | - * @singleton method used to instantiate class object |
|
| 202 | - * @access public |
|
| 203 | - * @param \EE_Dependency_Map $dependency_map |
|
| 204 | - * @return \EE_Registry instance |
|
| 205 | - */ |
|
| 206 | - public static function instance(\EE_Dependency_Map $dependency_map = null) |
|
| 207 | - { |
|
| 208 | - // check if class object is instantiated |
|
| 209 | - if ( ! self::$_instance instanceof EE_Registry) { |
|
| 210 | - self::$_instance = new EE_Registry($dependency_map); |
|
| 211 | - } |
|
| 212 | - return self::$_instance; |
|
| 213 | - } |
|
| 214 | - |
|
| 215 | - |
|
| 216 | - |
|
| 217 | - /** |
|
| 218 | - *protected constructor to prevent direct creation |
|
| 219 | - * |
|
| 220 | - * @Constructor |
|
| 221 | - * @access protected |
|
| 222 | - * @param \EE_Dependency_Map $dependency_map |
|
| 223 | - * @return \EE_Registry |
|
| 224 | - */ |
|
| 225 | - protected function __construct(\EE_Dependency_Map $dependency_map) |
|
| 226 | - { |
|
| 227 | - $this->_dependency_map = $dependency_map; |
|
| 228 | - add_action('EE_Load_Espresso_Core__handle_request__initialize_core_loading', array($this, 'initialize')); |
|
| 229 | - } |
|
| 230 | - |
|
| 231 | - |
|
| 232 | - |
|
| 233 | - /** |
|
| 234 | - * initialize |
|
| 235 | - */ |
|
| 236 | - public function initialize() |
|
| 237 | - { |
|
| 238 | - $this->_class_abbreviations = apply_filters( |
|
| 239 | - 'FHEE__EE_Registry____construct___class_abbreviations', |
|
| 240 | - array( |
|
| 241 | - 'EE_Config' => 'CFG', |
|
| 242 | - 'EE_Session' => 'SSN', |
|
| 243 | - 'EE_Capabilities' => 'CAP', |
|
| 244 | - 'EE_Cart' => 'CART', |
|
| 245 | - 'EE_Network_Config' => 'NET_CFG', |
|
| 246 | - 'EE_Request_Handler' => 'REQ', |
|
| 247 | - 'EE_Message_Resource_Manager' => 'MRM', |
|
| 248 | - 'EventEspresso\core\services\commands\CommandBus' => 'BUS', |
|
| 249 | - 'EventEspresso\core\services\assets\Registry' => 'AssetsRegistry', |
|
| 250 | - ) |
|
| 251 | - ); |
|
| 252 | - // class library |
|
| 253 | - $this->LIB = new stdClass(); |
|
| 254 | - $this->addons = new stdClass(); |
|
| 255 | - $this->modules = new stdClass(); |
|
| 256 | - $this->shortcodes = new stdClass(); |
|
| 257 | - $this->widgets = new stdClass(); |
|
| 258 | - $this->load_core('Base', array(), true); |
|
| 259 | - // add our request and response objects to the cache |
|
| 260 | - $request_loader = $this->_dependency_map->class_loader('EE_Request'); |
|
| 261 | - $this->_set_cached_class( |
|
| 262 | - $request_loader(), |
|
| 263 | - 'EE_Request' |
|
| 264 | - ); |
|
| 265 | - $response_loader = $this->_dependency_map->class_loader('EE_Response'); |
|
| 266 | - $this->_set_cached_class( |
|
| 267 | - $response_loader(), |
|
| 268 | - 'EE_Response' |
|
| 269 | - ); |
|
| 270 | - add_action('AHEE__EE_System__set_hooks_for_core', array($this, 'init')); |
|
| 271 | - } |
|
| 272 | - |
|
| 273 | - |
|
| 274 | - |
|
| 275 | - /** |
|
| 276 | - * init |
|
| 277 | - * |
|
| 278 | - * @access public |
|
| 279 | - * @return void |
|
| 280 | - */ |
|
| 281 | - public function init() |
|
| 282 | - { |
|
| 283 | - // Get current page protocol |
|
| 284 | - $protocol = isset($_SERVER['HTTPS']) ? 'https://' : 'http://'; |
|
| 285 | - // Output admin-ajax.php URL with same protocol as current page |
|
| 286 | - self::$i18n_js_strings['ajax_url'] = admin_url('admin-ajax.php', $protocol); |
|
| 287 | - self::$i18n_js_strings['wp_debug'] = defined('WP_DEBUG') ? WP_DEBUG : false; |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - |
|
| 291 | - |
|
| 292 | - /** |
|
| 293 | - * localize_i18n_js_strings |
|
| 294 | - * |
|
| 295 | - * @return string |
|
| 296 | - */ |
|
| 297 | - public static function localize_i18n_js_strings() |
|
| 298 | - { |
|
| 299 | - $i18n_js_strings = (array)EE_Registry::$i18n_js_strings; |
|
| 300 | - foreach ($i18n_js_strings as $key => $value) { |
|
| 301 | - if (is_scalar($value)) { |
|
| 302 | - $i18n_js_strings[$key] = html_entity_decode((string)$value, ENT_QUOTES, 'UTF-8'); |
|
| 303 | - } |
|
| 304 | - } |
|
| 305 | - return "/* <![CDATA[ */ var eei18n = " . wp_json_encode($i18n_js_strings) . '; /* ]]> */'; |
|
| 306 | - } |
|
| 307 | - |
|
| 308 | - |
|
| 309 | - |
|
| 310 | - /** |
|
| 311 | - * @param mixed string | EED_Module $module |
|
| 312 | - */ |
|
| 313 | - public function add_module($module) |
|
| 314 | - { |
|
| 315 | - if ($module instanceof EED_Module) { |
|
| 316 | - $module_class = get_class($module); |
|
| 317 | - $this->modules->{$module_class} = $module; |
|
| 318 | - } else { |
|
| 319 | - if ( ! class_exists('EE_Module_Request_Router')) { |
|
| 320 | - $this->load_core('Module_Request_Router'); |
|
| 321 | - } |
|
| 322 | - $this->modules->{$module} = EE_Module_Request_Router::module_factory($module); |
|
| 323 | - } |
|
| 324 | - } |
|
| 325 | - |
|
| 326 | - |
|
| 327 | - |
|
| 328 | - /** |
|
| 329 | - * @param string $module_name |
|
| 330 | - * @return mixed EED_Module | NULL |
|
| 331 | - */ |
|
| 332 | - public function get_module($module_name = '') |
|
| 333 | - { |
|
| 334 | - return isset($this->modules->{$module_name}) ? $this->modules->{$module_name} : null; |
|
| 335 | - } |
|
| 336 | - |
|
| 337 | - |
|
| 338 | - |
|
| 339 | - /** |
|
| 340 | - * loads core classes - must be singletons |
|
| 341 | - * |
|
| 342 | - * @access public |
|
| 343 | - * @param string $class_name - simple class name ie: session |
|
| 344 | - * @param mixed $arguments |
|
| 345 | - * @param bool $load_only |
|
| 346 | - * @return mixed |
|
| 347 | - */ |
|
| 348 | - public function load_core($class_name, $arguments = array(), $load_only = false) |
|
| 349 | - { |
|
| 350 | - $core_paths = apply_filters( |
|
| 351 | - 'FHEE__EE_Registry__load_core__core_paths', |
|
| 352 | - array( |
|
| 353 | - EE_CORE, |
|
| 354 | - EE_ADMIN, |
|
| 355 | - EE_CPTS, |
|
| 356 | - EE_CORE . 'data_migration_scripts' . DS, |
|
| 357 | - EE_CORE . 'request_stack' . DS, |
|
| 358 | - EE_CORE . 'middleware' . DS, |
|
| 359 | - ) |
|
| 360 | - ); |
|
| 361 | - // retrieve instantiated class |
|
| 362 | - return $this->_load($core_paths, 'EE_', $class_name, 'core', $arguments, false, true, $load_only); |
|
| 363 | - } |
|
| 364 | - |
|
| 365 | - |
|
| 366 | - |
|
| 367 | - /** |
|
| 368 | - * loads service classes |
|
| 369 | - * |
|
| 370 | - * @access public |
|
| 371 | - * @param string $class_name - simple class name ie: session |
|
| 372 | - * @param mixed $arguments |
|
| 373 | - * @param bool $load_only |
|
| 374 | - * @return mixed |
|
| 375 | - */ |
|
| 376 | - public function load_service($class_name, $arguments = array(), $load_only = false) |
|
| 377 | - { |
|
| 378 | - $service_paths = apply_filters( |
|
| 379 | - 'FHEE__EE_Registry__load_service__service_paths', |
|
| 380 | - array( |
|
| 381 | - EE_CORE . 'services' . DS, |
|
| 382 | - ) |
|
| 383 | - ); |
|
| 384 | - // retrieve instantiated class |
|
| 385 | - return $this->_load($service_paths, 'EE_', $class_name, 'class', $arguments, false, true, $load_only); |
|
| 386 | - } |
|
| 387 | - |
|
| 388 | - |
|
| 389 | - |
|
| 390 | - /** |
|
| 391 | - * loads data_migration_scripts |
|
| 392 | - * |
|
| 393 | - * @access public |
|
| 394 | - * @param string $class_name - class name for the DMS ie: EE_DMS_Core_4_2_0 |
|
| 395 | - * @param mixed $arguments |
|
| 396 | - * @return EE_Data_Migration_Script_Base|mixed |
|
| 397 | - */ |
|
| 398 | - public function load_dms($class_name, $arguments = array()) |
|
| 399 | - { |
|
| 400 | - // retrieve instantiated class |
|
| 401 | - return $this->_load(EE_Data_Migration_Manager::instance()->get_data_migration_script_folders(), 'EE_DMS_', $class_name, 'dms', $arguments, false, false, false); |
|
| 402 | - } |
|
| 403 | - |
|
| 404 | - |
|
| 405 | - |
|
| 406 | - /** |
|
| 407 | - * loads object creating classes - must be singletons |
|
| 408 | - * |
|
| 409 | - * @param string $class_name - simple class name ie: attendee |
|
| 410 | - * @param mixed $arguments - an array of arguments to pass to the class |
|
| 411 | - * @param bool $from_db - some classes are instantiated from the db and thus call a different method to instantiate |
|
| 412 | - * @param bool $cache if you don't want the class to be stored in the internal cache (non-persistent) then set this to FALSE (ie. when instantiating model objects from client in a loop) |
|
| 413 | - * @param bool $load_only whether or not to just load the file and NOT instantiate, or load AND instantiate (default) |
|
| 414 | - * @return EE_Base_Class | bool |
|
| 415 | - */ |
|
| 416 | - public function load_class($class_name, $arguments = array(), $from_db = false, $cache = true, $load_only = false) |
|
| 417 | - { |
|
| 418 | - $paths = apply_filters('FHEE__EE_Registry__load_class__paths', array( |
|
| 419 | - EE_CORE, |
|
| 420 | - EE_CLASSES, |
|
| 421 | - EE_BUSINESS, |
|
| 422 | - )); |
|
| 423 | - // retrieve instantiated class |
|
| 424 | - return $this->_load($paths, 'EE_', $class_name, 'class', $arguments, $from_db, $cache, $load_only); |
|
| 425 | - } |
|
| 426 | - |
|
| 427 | - |
|
| 428 | - |
|
| 429 | - /** |
|
| 430 | - * loads helper classes - must be singletons |
|
| 431 | - * |
|
| 432 | - * @param string $class_name - simple class name ie: price |
|
| 433 | - * @param mixed $arguments |
|
| 434 | - * @param bool $load_only |
|
| 435 | - * @return EEH_Base | bool |
|
| 436 | - */ |
|
| 437 | - public function load_helper($class_name, $arguments = array(), $load_only = true) |
|
| 438 | - { |
|
| 439 | - // todo: add doing_it_wrong() in a few versions after all addons have had calls to this method removed |
|
| 440 | - $helper_paths = apply_filters('FHEE__EE_Registry__load_helper__helper_paths', array(EE_HELPERS)); |
|
| 441 | - // retrieve instantiated class |
|
| 442 | - return $this->_load($helper_paths, 'EEH_', $class_name, 'helper', $arguments, false, true, $load_only); |
|
| 443 | - } |
|
| 444 | - |
|
| 445 | - |
|
| 446 | - |
|
| 447 | - /** |
|
| 448 | - * loads core classes - must be singletons |
|
| 449 | - * |
|
| 450 | - * @access public |
|
| 451 | - * @param string $class_name - simple class name ie: session |
|
| 452 | - * @param mixed $arguments |
|
| 453 | - * @param bool $load_only |
|
| 454 | - * @param bool $cache whether to cache the object or not. |
|
| 455 | - * @return mixed |
|
| 456 | - */ |
|
| 457 | - public function load_lib($class_name, $arguments = array(), $load_only = false, $cache = true) |
|
| 458 | - { |
|
| 459 | - $paths = array( |
|
| 460 | - EE_LIBRARIES, |
|
| 461 | - EE_LIBRARIES . 'messages' . DS, |
|
| 462 | - EE_LIBRARIES . 'shortcodes' . DS, |
|
| 463 | - EE_LIBRARIES . 'qtips' . DS, |
|
| 464 | - EE_LIBRARIES . 'payment_methods' . DS, |
|
| 465 | - ); |
|
| 466 | - // retrieve instantiated class |
|
| 467 | - return $this->_load($paths, 'EE_', $class_name, 'lib', $arguments, false, $cache, $load_only); |
|
| 468 | - } |
|
| 469 | - |
|
| 470 | - |
|
| 471 | - |
|
| 472 | - /** |
|
| 473 | - * loads model classes - must be singletons |
|
| 474 | - * |
|
| 475 | - * @param string $class_name - simple class name ie: price |
|
| 476 | - * @param mixed $arguments |
|
| 477 | - * @param bool $load_only |
|
| 478 | - * @return EEM_Base | bool |
|
| 479 | - */ |
|
| 480 | - public function load_model($class_name, $arguments = array(), $load_only = false) |
|
| 481 | - { |
|
| 482 | - $paths = apply_filters('FHEE__EE_Registry__load_model__paths', array( |
|
| 483 | - EE_MODELS, |
|
| 484 | - EE_CORE, |
|
| 485 | - )); |
|
| 486 | - // retrieve instantiated class |
|
| 487 | - return $this->_load($paths, 'EEM_', $class_name, 'model', $arguments, false, true, $load_only); |
|
| 488 | - } |
|
| 489 | - |
|
| 490 | - |
|
| 491 | - |
|
| 492 | - /** |
|
| 493 | - * loads model classes - must be singletons |
|
| 494 | - * |
|
| 495 | - * @param string $class_name - simple class name ie: price |
|
| 496 | - * @param mixed $arguments |
|
| 497 | - * @param bool $load_only |
|
| 498 | - * @return mixed | bool |
|
| 499 | - */ |
|
| 500 | - public function load_model_class($class_name, $arguments = array(), $load_only = true) |
|
| 501 | - { |
|
| 502 | - $paths = array( |
|
| 503 | - EE_MODELS . 'fields' . DS, |
|
| 504 | - EE_MODELS . 'helpers' . DS, |
|
| 505 | - EE_MODELS . 'relations' . DS, |
|
| 506 | - EE_MODELS . 'strategies' . DS, |
|
| 507 | - ); |
|
| 508 | - // retrieve instantiated class |
|
| 509 | - return $this->_load($paths, 'EE_', $class_name, '', $arguments, false, true, $load_only); |
|
| 510 | - } |
|
| 511 | - |
|
| 512 | - |
|
| 513 | - |
|
| 514 | - /** |
|
| 515 | - * Determines if $model_name is the name of an actual EE model. |
|
| 516 | - * |
|
| 517 | - * @param string $model_name like Event, Attendee, Question_Group_Question, etc. |
|
| 518 | - * @return boolean |
|
| 519 | - */ |
|
| 520 | - public function is_model_name($model_name) |
|
| 521 | - { |
|
| 522 | - return isset($this->models[$model_name]) ? true : false; |
|
| 523 | - } |
|
| 524 | - |
|
| 525 | - |
|
| 526 | - |
|
| 527 | - /** |
|
| 528 | - * generic class loader |
|
| 529 | - * |
|
| 530 | - * @param string $path_to_file - directory path to file location, not including filename |
|
| 531 | - * @param string $file_name - file name ie: my_file.php, including extension |
|
| 532 | - * @param string $type - file type - core? class? helper? model? |
|
| 533 | - * @param mixed $arguments |
|
| 534 | - * @param bool $load_only |
|
| 535 | - * @return mixed |
|
| 536 | - */ |
|
| 537 | - public function load_file($path_to_file, $file_name, $type = '', $arguments = array(), $load_only = true) |
|
| 538 | - { |
|
| 539 | - // retrieve instantiated class |
|
| 540 | - return $this->_load($path_to_file, '', $file_name, $type, $arguments, false, true, $load_only); |
|
| 541 | - } |
|
| 542 | - |
|
| 543 | - |
|
| 544 | - |
|
| 545 | - /** |
|
| 546 | - * load_addon |
|
| 547 | - * |
|
| 548 | - * @param string $path_to_file - directory path to file location, not including filename |
|
| 549 | - * @param string $class_name - full class name ie: My_Class |
|
| 550 | - * @param string $type - file type - core? class? helper? model? |
|
| 551 | - * @param mixed $arguments |
|
| 552 | - * @param bool $load_only |
|
| 553 | - * @return EE_Addon |
|
| 554 | - */ |
|
| 555 | - public function load_addon($path_to_file, $class_name, $type = 'class', $arguments = array(), $load_only = false) |
|
| 556 | - { |
|
| 557 | - // retrieve instantiated class |
|
| 558 | - return $this->_load($path_to_file, 'addon', $class_name, $type, $arguments, false, true, $load_only); |
|
| 559 | - } |
|
| 560 | - |
|
| 561 | - |
|
| 562 | - |
|
| 563 | - /** |
|
| 564 | - * instantiates, caches, and automatically resolves dependencies |
|
| 565 | - * for classes that use a Fully Qualified Class Name. |
|
| 566 | - * if the class is not capable of being loaded using PSR-4 autoloading, |
|
| 567 | - * then you need to use one of the existing load_*() methods |
|
| 568 | - * which can resolve the classname and filepath from the passed arguments |
|
| 569 | - * |
|
| 570 | - * @param bool|string $class_name Fully Qualified Class Name |
|
| 571 | - * @param array $arguments an argument, or array of arguments to pass to the class upon instantiation |
|
| 572 | - * @param bool $cache whether to cache the instantiated object for reuse |
|
| 573 | - * @param bool $from_db some classes are instantiated from the db |
|
| 574 | - * and thus call a different method to instantiate |
|
| 575 | - * @param bool $load_only if true, will only load the file, but will NOT instantiate an object |
|
| 576 | - * @param bool|string $addon if true, will cache the object in the EE_Registry->$addons array |
|
| 577 | - * @return mixed null = failure to load or instantiate class object. |
|
| 578 | - * object = class loaded and instantiated successfully. |
|
| 579 | - * bool = fail or success when $load_only is true |
|
| 580 | - */ |
|
| 581 | - public function create( |
|
| 582 | - $class_name = false, |
|
| 583 | - $arguments = array(), |
|
| 584 | - $cache = false, |
|
| 585 | - $from_db = false, |
|
| 586 | - $load_only = false, |
|
| 587 | - $addon = false |
|
| 588 | - ) { |
|
| 589 | - $class_name = ltrim($class_name, '\\'); |
|
| 590 | - $class_name = $this->_dependency_map->get_alias($class_name); |
|
| 591 | - if ( ! class_exists($class_name)) { |
|
| 592 | - // maybe the class is registered with a preceding \ |
|
| 593 | - $class_name = strpos($class_name, '\\') !== 0 ? '\\' . $class_name : $class_name; |
|
| 594 | - // still doesn't exist ? |
|
| 595 | - if ( ! class_exists($class_name)) { |
|
| 596 | - return null; |
|
| 597 | - } |
|
| 598 | - } |
|
| 599 | - // if we're only loading the class and it already exists, then let's just return true immediately |
|
| 600 | - if ($load_only) { |
|
| 601 | - return true; |
|
| 602 | - } |
|
| 603 | - $addon = $addon ? 'addon' : ''; |
|
| 604 | - // $this->_cache_on is toggled during the recursive loading that can occur with dependency injection |
|
| 605 | - // $cache is controlled by individual calls to separate Registry loader methods like load_class() |
|
| 606 | - // $load_only is also controlled by individual calls to separate Registry loader methods like load_file() |
|
| 607 | - if ($this->_cache_on && $cache && ! $load_only) { |
|
| 608 | - // return object if it's already cached |
|
| 609 | - $cached_class = $this->_get_cached_class($class_name, $addon); |
|
| 610 | - if ($cached_class !== null) { |
|
| 611 | - return $cached_class; |
|
| 612 | - } |
|
| 613 | - } |
|
| 614 | - // instantiate the requested object |
|
| 615 | - $class_obj = $this->_create_object($class_name, $arguments, $addon, $from_db); |
|
| 616 | - if ($this->_cache_on && $cache) { |
|
| 617 | - // save it for later... kinda like gum { : $ |
|
| 618 | - $this->_set_cached_class($class_obj, $class_name, $addon, $from_db); |
|
| 619 | - } |
|
| 620 | - $this->_cache_on = true; |
|
| 621 | - return $class_obj; |
|
| 622 | - } |
|
| 623 | - |
|
| 624 | - |
|
| 625 | - |
|
| 626 | - /** |
|
| 627 | - * instantiates, caches, and injects dependencies for classes |
|
| 628 | - * |
|
| 629 | - * @param array $file_paths an array of paths to folders to look in |
|
| 630 | - * @param string $class_prefix EE or EEM or... ??? |
|
| 631 | - * @param bool|string $class_name $class name |
|
| 632 | - * @param string $type file type - core? class? helper? model? |
|
| 633 | - * @param mixed $arguments an argument or array of arguments to pass to the class upon instantiation |
|
| 634 | - * @param bool $from_db some classes are instantiated from the db |
|
| 635 | - * and thus call a different method to instantiate |
|
| 636 | - * @param bool $cache whether to cache the instantiated object for reuse |
|
| 637 | - * @param bool $load_only if true, will only load the file, but will NOT instantiate an object |
|
| 638 | - * @return null|object|bool null = failure to load or instantiate class object. |
|
| 639 | - * object = class loaded and instantiated successfully. |
|
| 640 | - * bool = fail or success when $load_only is true |
|
| 641 | - */ |
|
| 642 | - protected function _load( |
|
| 643 | - $file_paths = array(), |
|
| 644 | - $class_prefix = 'EE_', |
|
| 645 | - $class_name = false, |
|
| 646 | - $type = 'class', |
|
| 647 | - $arguments = array(), |
|
| 648 | - $from_db = false, |
|
| 649 | - $cache = true, |
|
| 650 | - $load_only = false |
|
| 651 | - ) { |
|
| 652 | - $class_name = ltrim($class_name, '\\'); |
|
| 653 | - // strip php file extension |
|
| 654 | - $class_name = str_replace('.php', '', trim($class_name)); |
|
| 655 | - // does the class have a prefix ? |
|
| 656 | - if ( ! empty($class_prefix) && $class_prefix != 'addon') { |
|
| 657 | - // make sure $class_prefix is uppercase |
|
| 658 | - $class_prefix = strtoupper(trim($class_prefix)); |
|
| 659 | - // add class prefix ONCE!!! |
|
| 660 | - $class_name = $class_prefix . str_replace($class_prefix, '', $class_name); |
|
| 661 | - } |
|
| 662 | - $class_name = $this->_dependency_map->get_alias($class_name); |
|
| 663 | - $class_exists = class_exists($class_name); |
|
| 664 | - // if we're only loading the class and it already exists, then let's just return true immediately |
|
| 665 | - if ($load_only && $class_exists) { |
|
| 666 | - return true; |
|
| 667 | - } |
|
| 668 | - // $this->_cache_on is toggled during the recursive loading that can occur with dependency injection |
|
| 669 | - // $cache is controlled by individual calls to separate Registry loader methods like load_class() |
|
| 670 | - // $load_only is also controlled by individual calls to separate Registry loader methods like load_file() |
|
| 671 | - if ($this->_cache_on && $cache && ! $load_only) { |
|
| 672 | - // return object if it's already cached |
|
| 673 | - $cached_class = $this->_get_cached_class($class_name, $class_prefix); |
|
| 674 | - if ($cached_class !== null) { |
|
| 675 | - return $cached_class; |
|
| 676 | - } |
|
| 677 | - } |
|
| 678 | - // if the class doesn't already exist.. then we need to try and find the file and load it |
|
| 679 | - if ( ! $class_exists) { |
|
| 680 | - // get full path to file |
|
| 681 | - $path = $this->_resolve_path($class_name, $type, $file_paths); |
|
| 682 | - // load the file |
|
| 683 | - $loaded = $this->_require_file($path, $class_name, $type, $file_paths); |
|
| 684 | - // if loading failed, or we are only loading a file but NOT instantiating an object |
|
| 685 | - if ( ! $loaded || $load_only) { |
|
| 686 | - // return boolean if only loading, or null if an object was expected |
|
| 687 | - return $load_only ? $loaded : null; |
|
| 688 | - } |
|
| 689 | - } |
|
| 690 | - // instantiate the requested object |
|
| 691 | - $class_obj = $this->_create_object($class_name, $arguments, $type, $from_db); |
|
| 692 | - if ($this->_cache_on && $cache) { |
|
| 693 | - // save it for later... kinda like gum { : $ |
|
| 694 | - $this->_set_cached_class($class_obj, $class_name, $class_prefix, $from_db); |
|
| 695 | - } |
|
| 696 | - $this->_cache_on = true; |
|
| 697 | - return $class_obj; |
|
| 698 | - } |
|
| 699 | - |
|
| 700 | - |
|
| 701 | - |
|
| 702 | - /** |
|
| 703 | - * _get_cached_class |
|
| 704 | - * attempts to find a cached version of the requested class |
|
| 705 | - * by looking in the following places: |
|
| 706 | - * $this->{$class_abbreviation} ie: $this->CART |
|
| 707 | - * $this->{$class_name} ie: $this->Some_Class |
|
| 708 | - * $this->LIB->{$class_name} ie: $this->LIB->Some_Class |
|
| 709 | - * $this->addon->{$class_name} ie: $this->addon->Some_Addon_Class |
|
| 710 | - * |
|
| 711 | - * @access protected |
|
| 712 | - * @param string $class_name |
|
| 713 | - * @param string $class_prefix |
|
| 714 | - * @return mixed |
|
| 715 | - */ |
|
| 716 | - protected function _get_cached_class($class_name, $class_prefix = '') |
|
| 717 | - { |
|
| 718 | - if (isset($this->_class_abbreviations[$class_name])) { |
|
| 719 | - $class_abbreviation = $this->_class_abbreviations[$class_name]; |
|
| 720 | - } else { |
|
| 721 | - // have to specify something, but not anything that will conflict |
|
| 722 | - $class_abbreviation = 'FANCY_BATMAN_PANTS'; |
|
| 723 | - } |
|
| 724 | - // check if class has already been loaded, and return it if it has been |
|
| 725 | - if (isset($this->{$class_abbreviation}) && ! is_null($this->{$class_abbreviation})) { |
|
| 726 | - return $this->{$class_abbreviation}; |
|
| 727 | - } else if (isset ($this->{$class_name})) { |
|
| 728 | - return $this->{$class_name}; |
|
| 729 | - } else if (isset ($this->LIB->{$class_name})) { |
|
| 730 | - return $this->LIB->{$class_name}; |
|
| 731 | - } else if ($class_prefix == 'addon' && isset ($this->addons->{$class_name})) { |
|
| 732 | - return $this->addons->{$class_name}; |
|
| 733 | - } |
|
| 734 | - return null; |
|
| 735 | - } |
|
| 736 | - |
|
| 737 | - |
|
| 738 | - |
|
| 739 | - /** |
|
| 740 | - * _resolve_path |
|
| 741 | - * attempts to find a full valid filepath for the requested class. |
|
| 742 | - * loops thru each of the base paths in the $file_paths array and appends : "{classname} . {file type} . php" |
|
| 743 | - * then returns that path if the target file has been found and is readable |
|
| 744 | - * |
|
| 745 | - * @access protected |
|
| 746 | - * @param string $class_name |
|
| 747 | - * @param string $type |
|
| 748 | - * @param array $file_paths |
|
| 749 | - * @return string | bool |
|
| 750 | - */ |
|
| 751 | - protected function _resolve_path($class_name, $type = '', $file_paths = array()) |
|
| 752 | - { |
|
| 753 | - // make sure $file_paths is an array |
|
| 754 | - $file_paths = is_array($file_paths) ? $file_paths : array($file_paths); |
|
| 755 | - // cycle thru paths |
|
| 756 | - foreach ($file_paths as $key => $file_path) { |
|
| 757 | - // convert all separators to proper DS, if no filepath, then use EE_CLASSES |
|
| 758 | - $file_path = $file_path ? str_replace(array('/', '\\'), DS, $file_path) : EE_CLASSES; |
|
| 759 | - // prep file type |
|
| 760 | - $type = ! empty($type) ? trim($type, '.') . '.' : ''; |
|
| 761 | - // build full file path |
|
| 762 | - $file_paths[$key] = rtrim($file_path, DS) . DS . $class_name . '.' . $type . 'php'; |
|
| 763 | - //does the file exist and can be read ? |
|
| 764 | - if (is_readable($file_paths[$key])) { |
|
| 765 | - return $file_paths[$key]; |
|
| 766 | - } |
|
| 767 | - } |
|
| 768 | - return false; |
|
| 769 | - } |
|
| 770 | - |
|
| 771 | - |
|
| 772 | - |
|
| 773 | - /** |
|
| 774 | - * _require_file |
|
| 775 | - * basically just performs a require_once() |
|
| 776 | - * but with some error handling |
|
| 777 | - * |
|
| 778 | - * @access protected |
|
| 779 | - * @param string $path |
|
| 780 | - * @param string $class_name |
|
| 781 | - * @param string $type |
|
| 782 | - * @param array $file_paths |
|
| 783 | - * @return boolean |
|
| 784 | - * @throws \EE_Error |
|
| 785 | - */ |
|
| 786 | - protected function _require_file($path, $class_name, $type = '', $file_paths = array()) |
|
| 787 | - { |
|
| 788 | - // don't give up! you gotta... |
|
| 789 | - try { |
|
| 790 | - //does the file exist and can it be read ? |
|
| 791 | - if ( ! $path) { |
|
| 792 | - // so sorry, can't find the file |
|
| 793 | - throw new EE_Error ( |
|
| 794 | - sprintf( |
|
| 795 | - __('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', 'event_espresso'), |
|
| 796 | - trim($type, '.'), |
|
| 797 | - $class_name, |
|
| 798 | - '<br />' . implode(',<br />', $file_paths) |
|
| 799 | - ) |
|
| 800 | - ); |
|
| 801 | - } |
|
| 802 | - // get the file |
|
| 803 | - require_once($path); |
|
| 804 | - // if the class isn't already declared somewhere |
|
| 805 | - if (class_exists($class_name, false) === false) { |
|
| 806 | - // so sorry, not a class |
|
| 807 | - throw new EE_Error( |
|
| 808 | - sprintf( |
|
| 809 | - __('The %s file %s does not appear to contain the %s Class.', 'event_espresso'), |
|
| 810 | - $type, |
|
| 811 | - $path, |
|
| 812 | - $class_name |
|
| 813 | - ) |
|
| 814 | - ); |
|
| 815 | - } |
|
| 816 | - } catch (EE_Error $e) { |
|
| 817 | - $e->get_error(); |
|
| 818 | - return false; |
|
| 819 | - } |
|
| 820 | - return true; |
|
| 821 | - } |
|
| 822 | - |
|
| 823 | - |
|
| 824 | - |
|
| 825 | - /** |
|
| 826 | - * _create_object |
|
| 827 | - * Attempts to instantiate the requested class via any of the |
|
| 828 | - * commonly used instantiation methods employed throughout EE. |
|
| 829 | - * The priority for instantiation is as follows: |
|
| 830 | - * - abstract classes or any class flagged as "load only" (no instantiation occurs) |
|
| 831 | - * - model objects via their 'new_instance_from_db' method |
|
| 832 | - * - model objects via their 'new_instance' method |
|
| 833 | - * - "singleton" classes" via their 'instance' method |
|
| 834 | - * - standard instantiable classes via their __constructor |
|
| 835 | - * Prior to instantiation, if the classname exists in the dependency_map, |
|
| 836 | - * then the constructor for the requested class will be examined to determine |
|
| 837 | - * if any dependencies exist, and if they can be injected. |
|
| 838 | - * If so, then those classes will be added to the array of arguments passed to the constructor |
|
| 839 | - * |
|
| 840 | - * @access protected |
|
| 841 | - * @param string $class_name |
|
| 842 | - * @param array $arguments |
|
| 843 | - * @param string $type |
|
| 844 | - * @param bool $from_db |
|
| 845 | - * @return null | object |
|
| 846 | - * @throws \EE_Error |
|
| 847 | - */ |
|
| 848 | - protected function _create_object($class_name, $arguments = array(), $type = '', $from_db = false) |
|
| 849 | - { |
|
| 850 | - $class_obj = null; |
|
| 851 | - $instantiation_mode = '0) none'; |
|
| 852 | - // don't give up! you gotta... |
|
| 853 | - try { |
|
| 854 | - // create reflection |
|
| 855 | - $reflector = $this->get_ReflectionClass($class_name); |
|
| 856 | - // make sure arguments are an array |
|
| 857 | - $arguments = is_array($arguments) ? $arguments : array($arguments); |
|
| 858 | - // and if arguments array is numerically and sequentially indexed, then we want it to remain as is, |
|
| 859 | - // else wrap it in an additional array so that it doesn't get split into multiple parameters |
|
| 860 | - $arguments = $this->_array_is_numerically_and_sequentially_indexed($arguments) |
|
| 861 | - ? $arguments |
|
| 862 | - : array($arguments); |
|
| 863 | - // attempt to inject dependencies ? |
|
| 864 | - if ($this->_dependency_map->has($class_name)) { |
|
| 865 | - $arguments = $this->_resolve_dependencies($reflector, $class_name, $arguments); |
|
| 866 | - } |
|
| 867 | - // instantiate the class if possible |
|
| 868 | - if ($reflector->isAbstract()) { |
|
| 869 | - // nothing to instantiate, loading file was enough |
|
| 870 | - // does not throw an exception so $instantiation_mode is unused |
|
| 871 | - // $instantiation_mode = "1) no constructor abstract class"; |
|
| 872 | - $class_obj = true; |
|
| 873 | - } else if ($reflector->getConstructor() === null && $reflector->isInstantiable() && empty($arguments)) { |
|
| 874 | - // no constructor = static methods only... nothing to instantiate, loading file was enough |
|
| 875 | - $instantiation_mode = "2) no constructor but instantiable"; |
|
| 876 | - $class_obj = $reflector->newInstance(); |
|
| 877 | - } else if ($from_db && method_exists($class_name, 'new_instance_from_db')) { |
|
| 878 | - $instantiation_mode = "3) new_instance_from_db()"; |
|
| 879 | - $class_obj = call_user_func_array(array($class_name, 'new_instance_from_db'), $arguments); |
|
| 880 | - } else if (method_exists($class_name, 'new_instance')) { |
|
| 881 | - $instantiation_mode = "4) new_instance()"; |
|
| 882 | - $class_obj = call_user_func_array(array($class_name, 'new_instance'), $arguments); |
|
| 883 | - } else if (method_exists($class_name, 'instance')) { |
|
| 884 | - $instantiation_mode = "5) instance()"; |
|
| 885 | - $class_obj = call_user_func_array(array($class_name, 'instance'), $arguments); |
|
| 886 | - } else if ($reflector->isInstantiable()) { |
|
| 887 | - $instantiation_mode = "6) constructor"; |
|
| 888 | - $class_obj = $reflector->newInstanceArgs($arguments); |
|
| 889 | - } else { |
|
| 890 | - // heh ? something's not right ! |
|
| 891 | - throw new EE_Error( |
|
| 892 | - sprintf( |
|
| 893 | - __('The %s file %s could not be instantiated.', 'event_espresso'), |
|
| 894 | - $type, |
|
| 895 | - $class_name |
|
| 896 | - ) |
|
| 897 | - ); |
|
| 898 | - } |
|
| 899 | - } catch (Exception $e) { |
|
| 900 | - if ( ! $e instanceof EE_Error) { |
|
| 901 | - $e = new EE_Error( |
|
| 902 | - sprintf( |
|
| 903 | - __('The following error occurred while attempting to instantiate "%1$s": %2$s %3$s %2$s instantiation mode : %4$s', 'event_espresso'), |
|
| 904 | - $class_name, |
|
| 905 | - '<br />', |
|
| 906 | - $e->getMessage(), |
|
| 907 | - $instantiation_mode |
|
| 908 | - ) |
|
| 909 | - ); |
|
| 910 | - } |
|
| 911 | - $e->get_error(); |
|
| 912 | - } |
|
| 913 | - return $class_obj; |
|
| 914 | - } |
|
| 915 | - |
|
| 916 | - |
|
| 917 | - |
|
| 918 | - /** |
|
| 919 | - * @see http://stackoverflow.com/questions/173400/how-to-check-if-php-array-is-associative-or-sequential |
|
| 920 | - * @param array $array |
|
| 921 | - * @return bool |
|
| 922 | - */ |
|
| 923 | - protected function _array_is_numerically_and_sequentially_indexed(array $array) |
|
| 924 | - { |
|
| 925 | - return ! empty($array) ? array_keys($array) === range(0, count($array) - 1) : true; |
|
| 926 | - } |
|
| 927 | - |
|
| 928 | - |
|
| 929 | - |
|
| 930 | - /** |
|
| 931 | - * getReflectionClass |
|
| 932 | - * checks if a ReflectionClass object has already been generated for a class |
|
| 933 | - * and returns that instead of creating a new one |
|
| 934 | - * |
|
| 935 | - * @access public |
|
| 936 | - * @param string $class_name |
|
| 937 | - * @return ReflectionClass |
|
| 938 | - */ |
|
| 939 | - public function get_ReflectionClass($class_name) |
|
| 940 | - { |
|
| 941 | - if ( |
|
| 942 | - ! isset($this->_reflectors[$class_name]) |
|
| 943 | - || ! $this->_reflectors[$class_name] instanceof ReflectionClass |
|
| 944 | - ) { |
|
| 945 | - $this->_reflectors[$class_name] = new ReflectionClass($class_name); |
|
| 946 | - } |
|
| 947 | - return $this->_reflectors[$class_name]; |
|
| 948 | - } |
|
| 949 | - |
|
| 950 | - |
|
| 951 | - |
|
| 952 | - /** |
|
| 953 | - * _resolve_dependencies |
|
| 954 | - * examines the constructor for the requested class to determine |
|
| 955 | - * if any dependencies exist, and if they can be injected. |
|
| 956 | - * If so, then those classes will be added to the array of arguments passed to the constructor |
|
| 957 | - * PLZ NOTE: this is achieved by type hinting the constructor params |
|
| 958 | - * For example: |
|
| 959 | - * if attempting to load a class "Foo" with the following constructor: |
|
| 960 | - * __construct( Bar $bar_class, Fighter $grohl_class ) |
|
| 961 | - * then $bar_class and $grohl_class will be added to the $arguments array, |
|
| 962 | - * but only IF they are NOT already present in the incoming arguments array, |
|
| 963 | - * and the correct classes can be loaded |
|
| 964 | - * |
|
| 965 | - * @access protected |
|
| 966 | - * @param ReflectionClass $reflector |
|
| 967 | - * @param string $class_name |
|
| 968 | - * @param array $arguments |
|
| 969 | - * @return array |
|
| 970 | - * @throws \ReflectionException |
|
| 971 | - */ |
|
| 972 | - protected function _resolve_dependencies(ReflectionClass $reflector, $class_name, $arguments = array()) |
|
| 973 | - { |
|
| 974 | - // let's examine the constructor |
|
| 975 | - $constructor = $reflector->getConstructor(); |
|
| 976 | - // whu? huh? nothing? |
|
| 977 | - if ( ! $constructor) { |
|
| 978 | - return $arguments; |
|
| 979 | - } |
|
| 980 | - // get constructor parameters |
|
| 981 | - $params = $constructor->getParameters(); |
|
| 982 | - // and the keys for the incoming arguments array so that we can compare existing arguments with what is expected |
|
| 983 | - $argument_keys = array_keys($arguments); |
|
| 984 | - // now loop thru all of the constructors expected parameters |
|
| 985 | - foreach ($params as $index => $param) { |
|
| 986 | - // is this a dependency for a specific class ? |
|
| 987 | - $param_class = $param->getClass() ? $param->getClass()->name : null; |
|
| 988 | - if ( |
|
| 989 | - // param is not even a class |
|
| 990 | - empty($param_class) |
|
| 991 | - // and something already exists in the incoming arguments for this param |
|
| 992 | - && isset($argument_keys[$index], $arguments[$argument_keys[$index]]) |
|
| 993 | - ) { |
|
| 994 | - // so let's skip this argument and move on to the next |
|
| 995 | - continue; |
|
| 996 | - } else if ( |
|
| 997 | - // parameter is type hinted as a class, exists as an incoming argument, AND it's the correct class |
|
| 998 | - ! empty($param_class) |
|
| 999 | - && isset($argument_keys[$index], $arguments[$argument_keys[$index]]) |
|
| 1000 | - && $arguments[$argument_keys[$index]] instanceof $param_class |
|
| 1001 | - ) { |
|
| 1002 | - // skip this argument and move on to the next |
|
| 1003 | - continue; |
|
| 1004 | - } else if ( |
|
| 1005 | - // parameter is type hinted as a class, and should be injected |
|
| 1006 | - ! empty($param_class) |
|
| 1007 | - && $this->_dependency_map->has_dependency_for_class($class_name, $param_class) |
|
| 1008 | - ) { |
|
| 1009 | - $arguments = $this->_resolve_dependency($class_name, $param_class, $arguments, $index); |
|
| 1010 | - } else { |
|
| 1011 | - try { |
|
| 1012 | - $arguments[$index] = $param->getDefaultValue(); |
|
| 1013 | - } catch (ReflectionException $e) { |
|
| 1014 | - throw new ReflectionException( |
|
| 1015 | - sprintf( |
|
| 1016 | - __('%1$s for parameter "$%2$s"', 'event_espresso'), |
|
| 1017 | - $e->getMessage(), |
|
| 1018 | - $param->getName() |
|
| 1019 | - ) |
|
| 1020 | - ); |
|
| 1021 | - } |
|
| 1022 | - } |
|
| 1023 | - } |
|
| 1024 | - return $arguments; |
|
| 1025 | - } |
|
| 1026 | - |
|
| 1027 | - |
|
| 1028 | - |
|
| 1029 | - /** |
|
| 1030 | - * @access protected |
|
| 1031 | - * @param string $class_name |
|
| 1032 | - * @param string $param_class |
|
| 1033 | - * @param array $arguments |
|
| 1034 | - * @param mixed $index |
|
| 1035 | - * @return array |
|
| 1036 | - */ |
|
| 1037 | - protected function _resolve_dependency($class_name, $param_class, $arguments, $index) |
|
| 1038 | - { |
|
| 1039 | - $dependency = null; |
|
| 1040 | - // should dependency be loaded from cache ? |
|
| 1041 | - $cache_on = $this->_dependency_map->loading_strategy_for_class_dependency($class_name, $param_class) |
|
| 1042 | - !== EE_Dependency_Map::load_new_object |
|
| 1043 | - ? true |
|
| 1044 | - : false; |
|
| 1045 | - // we might have a dependency... |
|
| 1046 | - // let's MAYBE try and find it in our cache if that's what's been requested |
|
| 1047 | - $cached_class = $cache_on ? $this->_get_cached_class($param_class) : null; |
|
| 1048 | - // and grab it if it exists |
|
| 1049 | - if ($cached_class instanceof $param_class) { |
|
| 1050 | - $dependency = $cached_class; |
|
| 1051 | - } else if ($param_class != $class_name) { |
|
| 1052 | - // obtain the loader method from the dependency map |
|
| 1053 | - $loader = $this->_dependency_map->class_loader($param_class); |
|
| 1054 | - // is loader a custom closure ? |
|
| 1055 | - if ($loader instanceof Closure) { |
|
| 1056 | - $dependency = $loader(); |
|
| 1057 | - } else { |
|
| 1058 | - // set the cache on property for the recursive loading call |
|
| 1059 | - $this->_cache_on = $cache_on; |
|
| 1060 | - // if not, then let's try and load it via the registry |
|
| 1061 | - if (method_exists($this, $loader)) { |
|
| 1062 | - $dependency = $this->{$loader}($param_class); |
|
| 1063 | - } else { |
|
| 1064 | - $dependency = $this->create($param_class, array(), $cache_on); |
|
| 1065 | - } |
|
| 1066 | - } |
|
| 1067 | - } |
|
| 1068 | - // did we successfully find the correct dependency ? |
|
| 1069 | - if ($dependency instanceof $param_class) { |
|
| 1070 | - // then let's inject it into the incoming array of arguments at the correct location |
|
| 1071 | - if (isset($argument_keys[$index])) { |
|
| 1072 | - $arguments[$argument_keys[$index]] = $dependency; |
|
| 1073 | - } else { |
|
| 1074 | - $arguments[$index] = $dependency; |
|
| 1075 | - } |
|
| 1076 | - } |
|
| 1077 | - return $arguments; |
|
| 1078 | - } |
|
| 1079 | - |
|
| 1080 | - |
|
| 1081 | - |
|
| 1082 | - /** |
|
| 1083 | - * _set_cached_class |
|
| 1084 | - * attempts to cache the instantiated class locally |
|
| 1085 | - * in one of the following places, in the following order: |
|
| 1086 | - * $this->{class_abbreviation} ie: $this->CART |
|
| 1087 | - * $this->{$class_name} ie: $this->Some_Class |
|
| 1088 | - * $this->addon->{$$class_name} ie: $this->addon->Some_Addon_Class |
|
| 1089 | - * $this->LIB->{$class_name} ie: $this->LIB->Some_Class |
|
| 1090 | - * |
|
| 1091 | - * @access protected |
|
| 1092 | - * @param object $class_obj |
|
| 1093 | - * @param string $class_name |
|
| 1094 | - * @param string $class_prefix |
|
| 1095 | - * @param bool $from_db |
|
| 1096 | - * @return void |
|
| 1097 | - */ |
|
| 1098 | - protected function _set_cached_class($class_obj, $class_name, $class_prefix = '', $from_db = false) |
|
| 1099 | - { |
|
| 1100 | - if (empty($class_obj)) { |
|
| 1101 | - return; |
|
| 1102 | - } |
|
| 1103 | - // return newly instantiated class |
|
| 1104 | - if (isset($this->_class_abbreviations[$class_name])) { |
|
| 1105 | - $class_abbreviation = $this->_class_abbreviations[$class_name]; |
|
| 1106 | - $this->{$class_abbreviation} = $class_obj; |
|
| 1107 | - } else if (property_exists($this, $class_name)) { |
|
| 1108 | - $this->{$class_name} = $class_obj; |
|
| 1109 | - } else if ($class_prefix == 'addon') { |
|
| 1110 | - $this->addons->{$class_name} = $class_obj; |
|
| 1111 | - } else if ( ! $from_db) { |
|
| 1112 | - $this->LIB->{$class_name} = $class_obj; |
|
| 1113 | - } |
|
| 1114 | - } |
|
| 1115 | - |
|
| 1116 | - |
|
| 1117 | - |
|
| 1118 | - /** |
|
| 1119 | - * call any loader that's been registered in the EE_Dependency_Map::$_class_loaders array |
|
| 1120 | - * |
|
| 1121 | - * @param string $classname PLEASE NOTE: the class name needs to match what's registered |
|
| 1122 | - * in the EE_Dependency_Map::$_class_loaders array, |
|
| 1123 | - * including the class prefix, ie: "EE_", "EEM_", "EEH_", etc |
|
| 1124 | - * @param array $arguments |
|
| 1125 | - * @return object |
|
| 1126 | - */ |
|
| 1127 | - public static function factory($classname, $arguments = array()) |
|
| 1128 | - { |
|
| 1129 | - $loader = self::instance()->_dependency_map->class_loader($classname); |
|
| 1130 | - if ($loader instanceof Closure) { |
|
| 1131 | - return $loader($arguments); |
|
| 1132 | - } else if (method_exists(EE_Registry::instance(), $loader)) { |
|
| 1133 | - return EE_Registry::instance()->{$loader}($classname, $arguments); |
|
| 1134 | - } |
|
| 1135 | - return null; |
|
| 1136 | - } |
|
| 1137 | - |
|
| 1138 | - |
|
| 1139 | - |
|
| 1140 | - /** |
|
| 1141 | - * Gets the addon by its name/slug (not classname. For that, just |
|
| 1142 | - * use the classname as the property name on EE_Config::instance()->addons) |
|
| 1143 | - * |
|
| 1144 | - * @param string $name |
|
| 1145 | - * @return EE_Addon |
|
| 1146 | - */ |
|
| 1147 | - public function get_addon_by_name($name) |
|
| 1148 | - { |
|
| 1149 | - foreach ($this->addons as $addon) { |
|
| 1150 | - if ($addon->name() == $name) { |
|
| 1151 | - return $addon; |
|
| 1152 | - } |
|
| 1153 | - } |
|
| 1154 | - return null; |
|
| 1155 | - } |
|
| 1156 | - |
|
| 1157 | - |
|
| 1158 | - |
|
| 1159 | - /** |
|
| 1160 | - * Gets an array of all the registered addons, where the keys are their names. (ie, what each returns for their name() function) They're already available on EE_Config::instance()->addons as properties, where each property's name is |
|
| 1161 | - * the addon's classname. So if you just want to get the addon by classname, use EE_Config::instance()->addons->{classname} |
|
| 1162 | - * |
|
| 1163 | - * @return EE_Addon[] where the KEYS are the addon's name() |
|
| 1164 | - */ |
|
| 1165 | - public function get_addons_by_name() |
|
| 1166 | - { |
|
| 1167 | - $addons = array(); |
|
| 1168 | - foreach ($this->addons as $addon) { |
|
| 1169 | - $addons[$addon->name()] = $addon; |
|
| 1170 | - } |
|
| 1171 | - return $addons; |
|
| 1172 | - } |
|
| 1173 | - |
|
| 1174 | - |
|
| 1175 | - |
|
| 1176 | - /** |
|
| 1177 | - * Resets the specified model's instance AND makes sure EE_Registry doesn't keep |
|
| 1178 | - * a stale copy of it around |
|
| 1179 | - * |
|
| 1180 | - * @param string $model_name |
|
| 1181 | - * @return \EEM_Base |
|
| 1182 | - * @throws \EE_Error |
|
| 1183 | - */ |
|
| 1184 | - public function reset_model($model_name) |
|
| 1185 | - { |
|
| 1186 | - $model_class_name = strpos($model_name, 'EEM_') !== 0 ? "EEM_{$model_name}" : $model_name; |
|
| 1187 | - if ( ! isset($this->LIB->{$model_class_name}) || ! $this->LIB->{$model_class_name} instanceof EEM_Base) { |
|
| 1188 | - return null; |
|
| 1189 | - } |
|
| 1190 | - //get that model reset it and make sure we nuke the old reference to it |
|
| 1191 | - if ($this->LIB->{$model_class_name} instanceof $model_class_name && is_callable(array($model_class_name, 'reset'))) { |
|
| 1192 | - $this->LIB->{$model_class_name} = $this->LIB->{$model_class_name}->reset(); |
|
| 1193 | - } else { |
|
| 1194 | - throw new EE_Error(sprintf(__('Model %s does not have a method "reset"', 'event_espresso'), $model_name)); |
|
| 1195 | - } |
|
| 1196 | - return $this->LIB->{$model_class_name}; |
|
| 1197 | - } |
|
| 1198 | - |
|
| 1199 | - |
|
| 1200 | - |
|
| 1201 | - /** |
|
| 1202 | - * Resets the registry. |
|
| 1203 | - * The criteria for what gets reset is based on what can be shared between sites on the same request when switch_to_blog |
|
| 1204 | - * is used in a multisite install. Here is a list of things that are NOT reset. |
|
| 1205 | - * - $_dependency_map |
|
| 1206 | - * - $_class_abbreviations |
|
| 1207 | - * - $NET_CFG (EE_Network_Config): The config is shared network wide so no need to reset. |
|
| 1208 | - * - $REQ: Still on the same request so no need to change. |
|
| 1209 | - * - $CAP: There is no site specific state in the EE_Capability class. |
|
| 1210 | - * - $SSN: Although ideally, the session should not be shared between site switches, we can't reset it because only one Session |
|
| 1211 | - * can be active in a single request. Resetting could resolve in "headers already sent" errors. |
|
| 1212 | - * - $addons: In multisite, the state of the addons is something controlled via hooks etc in a normal request. So |
|
| 1213 | - * for now, we won't reset the addons because it could break calls to an add-ons class/methods in the |
|
| 1214 | - * switch or on the restore. |
|
| 1215 | - * - $modules |
|
| 1216 | - * - $shortcodes |
|
| 1217 | - * - $widgets |
|
| 1218 | - * |
|
| 1219 | - * @param boolean $hard whether to reset data in the database too, or just refresh |
|
| 1220 | - * the Registry to its state at the beginning of the request |
|
| 1221 | - * @param boolean $reinstantiate whether to create new instances of EE_Registry's singletons too, |
|
| 1222 | - * or just reset without re-instantiating (handy to set to FALSE if you're not sure if you CAN |
|
| 1223 | - * currently reinstantiate the singletons at the moment) |
|
| 1224 | - * @param bool $reset_models Defaults to true. When false, then the models are not reset. This is so client |
|
| 1225 | - * code instead can just change the model context to a different blog id if necessary |
|
| 1226 | - * @return EE_Registry |
|
| 1227 | - */ |
|
| 1228 | - public static function reset($hard = false, $reinstantiate = true, $reset_models = true) |
|
| 1229 | - { |
|
| 1230 | - $instance = self::instance(); |
|
| 1231 | - EEH_Activation::reset(); |
|
| 1232 | - //properties that get reset |
|
| 1233 | - $instance->_cache_on = true; |
|
| 1234 | - $instance->CFG = EE_Config::reset($hard, $reinstantiate); |
|
| 1235 | - $instance->CART = null; |
|
| 1236 | - $instance->MRM = null; |
|
| 1237 | - $instance->AssetsRegistry = null; |
|
| 1238 | - $instance->AssetsRegistry = $instance->create('EventEspresso\core\services\assets\Registry'); |
|
| 1239 | - //messages reset |
|
| 1240 | - EED_Messages::reset(); |
|
| 1241 | - if ($reset_models) { |
|
| 1242 | - foreach (array_keys($instance->non_abstract_db_models) as $model_name) { |
|
| 1243 | - $instance->reset_model($model_name); |
|
| 1244 | - } |
|
| 1245 | - } |
|
| 1246 | - $instance->LIB = new stdClass(); |
|
| 1247 | - return $instance; |
|
| 1248 | - } |
|
| 1249 | - |
|
| 1250 | - |
|
| 1251 | - |
|
| 1252 | - /** |
|
| 1253 | - * @override magic methods |
|
| 1254 | - * @return void |
|
| 1255 | - */ |
|
| 1256 | - public final function __destruct() |
|
| 1257 | - { |
|
| 1258 | - } |
|
| 1259 | - |
|
| 1260 | - |
|
| 1261 | - |
|
| 1262 | - /** |
|
| 1263 | - * @param $a |
|
| 1264 | - * @param $b |
|
| 1265 | - */ |
|
| 1266 | - public final function __call($a, $b) |
|
| 1267 | - { |
|
| 1268 | - } |
|
| 1269 | - |
|
| 1270 | - |
|
| 1271 | - |
|
| 1272 | - /** |
|
| 1273 | - * @param $a |
|
| 1274 | - */ |
|
| 1275 | - public final function __get($a) |
|
| 1276 | - { |
|
| 1277 | - } |
|
| 1278 | - |
|
| 1279 | - |
|
| 1280 | - |
|
| 1281 | - /** |
|
| 1282 | - * @param $a |
|
| 1283 | - * @param $b |
|
| 1284 | - */ |
|
| 1285 | - public final function __set($a, $b) |
|
| 1286 | - { |
|
| 1287 | - } |
|
| 1288 | - |
|
| 1289 | - |
|
| 1290 | - |
|
| 1291 | - /** |
|
| 1292 | - * @param $a |
|
| 1293 | - */ |
|
| 1294 | - public final function __isset($a) |
|
| 1295 | - { |
|
| 1296 | - } |
|
| 19 | + /** |
|
| 20 | + * EE_Registry Object |
|
| 21 | + * |
|
| 22 | + * @var EE_Registry $_instance |
|
| 23 | + * @access private |
|
| 24 | + */ |
|
| 25 | + private static $_instance = null; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * @var EE_Dependency_Map $_dependency_map |
|
| 29 | + * @access protected |
|
| 30 | + */ |
|
| 31 | + protected $_dependency_map = null; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * @var array $_class_abbreviations |
|
| 35 | + * @access protected |
|
| 36 | + */ |
|
| 37 | + protected $_class_abbreviations = array(); |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * @access public |
|
| 41 | + * @var \EventEspresso\core\services\commands\CommandBusInterface $BUS |
|
| 42 | + */ |
|
| 43 | + public $BUS; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * EE_Cart Object |
|
| 47 | + * |
|
| 48 | + * @access public |
|
| 49 | + * @var EE_Cart $CART |
|
| 50 | + */ |
|
| 51 | + public $CART = null; |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * EE_Config Object |
|
| 55 | + * |
|
| 56 | + * @access public |
|
| 57 | + * @var EE_Config $CFG |
|
| 58 | + */ |
|
| 59 | + public $CFG = null; |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * EE_Network_Config Object |
|
| 63 | + * |
|
| 64 | + * @access public |
|
| 65 | + * @var EE_Network_Config $NET_CFG |
|
| 66 | + */ |
|
| 67 | + public $NET_CFG = null; |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * StdClass object for storing library classes in |
|
| 71 | + * |
|
| 72 | + * @public LIB |
|
| 73 | + * @var StdClass $LIB |
|
| 74 | + */ |
|
| 75 | + public $LIB = null; |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * EE_Request_Handler Object |
|
| 79 | + * |
|
| 80 | + * @access public |
|
| 81 | + * @var EE_Request_Handler $REQ |
|
| 82 | + */ |
|
| 83 | + public $REQ = null; |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * EE_Session Object |
|
| 87 | + * |
|
| 88 | + * @access public |
|
| 89 | + * @var EE_Session $SSN |
|
| 90 | + */ |
|
| 91 | + public $SSN = null; |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * holds the ee capabilities object. |
|
| 95 | + * |
|
| 96 | + * @since 4.5.0 |
|
| 97 | + * @var EE_Capabilities |
|
| 98 | + */ |
|
| 99 | + public $CAP = null; |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * holds the EE_Message_Resource_Manager object. |
|
| 103 | + * |
|
| 104 | + * @since 4.9.0 |
|
| 105 | + * @var EE_Message_Resource_Manager |
|
| 106 | + */ |
|
| 107 | + public $MRM = null; |
|
| 108 | + |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * Holds the Assets Registry instance |
|
| 112 | + * @var Registry |
|
| 113 | + */ |
|
| 114 | + public $AssetsRegistry = null; |
|
| 115 | + |
|
| 116 | + /** |
|
| 117 | + * $addons - StdClass object for holding addons which have registered themselves to work with EE core |
|
| 118 | + * |
|
| 119 | + * @access public |
|
| 120 | + * @var EE_Addon[] |
|
| 121 | + */ |
|
| 122 | + public $addons = null; |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * $models |
|
| 126 | + * @access public |
|
| 127 | + * @var string[] $models keys are 'short names' (eg Event), values are class names (eg 'EEM_Event') |
|
| 128 | + */ |
|
| 129 | + public $models = array(); |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * $modules |
|
| 133 | + * @access public |
|
| 134 | + * @var EED_Module[] $modules |
|
| 135 | + */ |
|
| 136 | + public $modules = null; |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * $shortcodes |
|
| 140 | + * @access public |
|
| 141 | + * @var EES_Shortcode[] $shortcodes |
|
| 142 | + */ |
|
| 143 | + public $shortcodes = null; |
|
| 144 | + |
|
| 145 | + /** |
|
| 146 | + * $widgets |
|
| 147 | + * @access public |
|
| 148 | + * @var WP_Widget[] $widgets |
|
| 149 | + */ |
|
| 150 | + public $widgets = null; |
|
| 151 | + |
|
| 152 | + /** |
|
| 153 | + * $non_abstract_db_models |
|
| 154 | + * @access public |
|
| 155 | + * @var array this is an array of all implemented model names (i.e. not the parent abstract models, or models |
|
| 156 | + * which don't actually fetch items from the DB in the normal way (ie, are not children of EEM_Base)). |
|
| 157 | + * Keys are model "short names" (eg "Event") as used in model relations, and values are |
|
| 158 | + * classnames (eg "EEM_Event") |
|
| 159 | + */ |
|
| 160 | + public $non_abstract_db_models = array(); |
|
| 161 | + |
|
| 162 | + |
|
| 163 | + /** |
|
| 164 | + * $i18n_js_strings - internationalization for JS strings |
|
| 165 | + * usage: EE_Registry::i18n_js_strings['string_key'] = __( 'string to translate.', 'event_espresso' ); |
|
| 166 | + * in js file: var translatedString = eei18n.string_key; |
|
| 167 | + * |
|
| 168 | + * @access public |
|
| 169 | + * @var array |
|
| 170 | + */ |
|
| 171 | + public static $i18n_js_strings = array(); |
|
| 172 | + |
|
| 173 | + |
|
| 174 | + /** |
|
| 175 | + * $main_file - path to espresso.php |
|
| 176 | + * |
|
| 177 | + * @access public |
|
| 178 | + * @var array |
|
| 179 | + */ |
|
| 180 | + public $main_file; |
|
| 181 | + |
|
| 182 | + /** |
|
| 183 | + * array of ReflectionClass objects where the key is the class name |
|
| 184 | + * |
|
| 185 | + * @access public |
|
| 186 | + * @var ReflectionClass[] |
|
| 187 | + */ |
|
| 188 | + public $_reflectors; |
|
| 189 | + |
|
| 190 | + /** |
|
| 191 | + * boolean flag to indicate whether or not to load/save dependencies from/to the cache |
|
| 192 | + * |
|
| 193 | + * @access protected |
|
| 194 | + * @var boolean $_cache_on |
|
| 195 | + */ |
|
| 196 | + protected $_cache_on = true; |
|
| 197 | + |
|
| 198 | + |
|
| 199 | + |
|
| 200 | + /** |
|
| 201 | + * @singleton method used to instantiate class object |
|
| 202 | + * @access public |
|
| 203 | + * @param \EE_Dependency_Map $dependency_map |
|
| 204 | + * @return \EE_Registry instance |
|
| 205 | + */ |
|
| 206 | + public static function instance(\EE_Dependency_Map $dependency_map = null) |
|
| 207 | + { |
|
| 208 | + // check if class object is instantiated |
|
| 209 | + if ( ! self::$_instance instanceof EE_Registry) { |
|
| 210 | + self::$_instance = new EE_Registry($dependency_map); |
|
| 211 | + } |
|
| 212 | + return self::$_instance; |
|
| 213 | + } |
|
| 214 | + |
|
| 215 | + |
|
| 216 | + |
|
| 217 | + /** |
|
| 218 | + *protected constructor to prevent direct creation |
|
| 219 | + * |
|
| 220 | + * @Constructor |
|
| 221 | + * @access protected |
|
| 222 | + * @param \EE_Dependency_Map $dependency_map |
|
| 223 | + * @return \EE_Registry |
|
| 224 | + */ |
|
| 225 | + protected function __construct(\EE_Dependency_Map $dependency_map) |
|
| 226 | + { |
|
| 227 | + $this->_dependency_map = $dependency_map; |
|
| 228 | + add_action('EE_Load_Espresso_Core__handle_request__initialize_core_loading', array($this, 'initialize')); |
|
| 229 | + } |
|
| 230 | + |
|
| 231 | + |
|
| 232 | + |
|
| 233 | + /** |
|
| 234 | + * initialize |
|
| 235 | + */ |
|
| 236 | + public function initialize() |
|
| 237 | + { |
|
| 238 | + $this->_class_abbreviations = apply_filters( |
|
| 239 | + 'FHEE__EE_Registry____construct___class_abbreviations', |
|
| 240 | + array( |
|
| 241 | + 'EE_Config' => 'CFG', |
|
| 242 | + 'EE_Session' => 'SSN', |
|
| 243 | + 'EE_Capabilities' => 'CAP', |
|
| 244 | + 'EE_Cart' => 'CART', |
|
| 245 | + 'EE_Network_Config' => 'NET_CFG', |
|
| 246 | + 'EE_Request_Handler' => 'REQ', |
|
| 247 | + 'EE_Message_Resource_Manager' => 'MRM', |
|
| 248 | + 'EventEspresso\core\services\commands\CommandBus' => 'BUS', |
|
| 249 | + 'EventEspresso\core\services\assets\Registry' => 'AssetsRegistry', |
|
| 250 | + ) |
|
| 251 | + ); |
|
| 252 | + // class library |
|
| 253 | + $this->LIB = new stdClass(); |
|
| 254 | + $this->addons = new stdClass(); |
|
| 255 | + $this->modules = new stdClass(); |
|
| 256 | + $this->shortcodes = new stdClass(); |
|
| 257 | + $this->widgets = new stdClass(); |
|
| 258 | + $this->load_core('Base', array(), true); |
|
| 259 | + // add our request and response objects to the cache |
|
| 260 | + $request_loader = $this->_dependency_map->class_loader('EE_Request'); |
|
| 261 | + $this->_set_cached_class( |
|
| 262 | + $request_loader(), |
|
| 263 | + 'EE_Request' |
|
| 264 | + ); |
|
| 265 | + $response_loader = $this->_dependency_map->class_loader('EE_Response'); |
|
| 266 | + $this->_set_cached_class( |
|
| 267 | + $response_loader(), |
|
| 268 | + 'EE_Response' |
|
| 269 | + ); |
|
| 270 | + add_action('AHEE__EE_System__set_hooks_for_core', array($this, 'init')); |
|
| 271 | + } |
|
| 272 | + |
|
| 273 | + |
|
| 274 | + |
|
| 275 | + /** |
|
| 276 | + * init |
|
| 277 | + * |
|
| 278 | + * @access public |
|
| 279 | + * @return void |
|
| 280 | + */ |
|
| 281 | + public function init() |
|
| 282 | + { |
|
| 283 | + // Get current page protocol |
|
| 284 | + $protocol = isset($_SERVER['HTTPS']) ? 'https://' : 'http://'; |
|
| 285 | + // Output admin-ajax.php URL with same protocol as current page |
|
| 286 | + self::$i18n_js_strings['ajax_url'] = admin_url('admin-ajax.php', $protocol); |
|
| 287 | + self::$i18n_js_strings['wp_debug'] = defined('WP_DEBUG') ? WP_DEBUG : false; |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + |
|
| 291 | + |
|
| 292 | + /** |
|
| 293 | + * localize_i18n_js_strings |
|
| 294 | + * |
|
| 295 | + * @return string |
|
| 296 | + */ |
|
| 297 | + public static function localize_i18n_js_strings() |
|
| 298 | + { |
|
| 299 | + $i18n_js_strings = (array)EE_Registry::$i18n_js_strings; |
|
| 300 | + foreach ($i18n_js_strings as $key => $value) { |
|
| 301 | + if (is_scalar($value)) { |
|
| 302 | + $i18n_js_strings[$key] = html_entity_decode((string)$value, ENT_QUOTES, 'UTF-8'); |
|
| 303 | + } |
|
| 304 | + } |
|
| 305 | + return "/* <![CDATA[ */ var eei18n = " . wp_json_encode($i18n_js_strings) . '; /* ]]> */'; |
|
| 306 | + } |
|
| 307 | + |
|
| 308 | + |
|
| 309 | + |
|
| 310 | + /** |
|
| 311 | + * @param mixed string | EED_Module $module |
|
| 312 | + */ |
|
| 313 | + public function add_module($module) |
|
| 314 | + { |
|
| 315 | + if ($module instanceof EED_Module) { |
|
| 316 | + $module_class = get_class($module); |
|
| 317 | + $this->modules->{$module_class} = $module; |
|
| 318 | + } else { |
|
| 319 | + if ( ! class_exists('EE_Module_Request_Router')) { |
|
| 320 | + $this->load_core('Module_Request_Router'); |
|
| 321 | + } |
|
| 322 | + $this->modules->{$module} = EE_Module_Request_Router::module_factory($module); |
|
| 323 | + } |
|
| 324 | + } |
|
| 325 | + |
|
| 326 | + |
|
| 327 | + |
|
| 328 | + /** |
|
| 329 | + * @param string $module_name |
|
| 330 | + * @return mixed EED_Module | NULL |
|
| 331 | + */ |
|
| 332 | + public function get_module($module_name = '') |
|
| 333 | + { |
|
| 334 | + return isset($this->modules->{$module_name}) ? $this->modules->{$module_name} : null; |
|
| 335 | + } |
|
| 336 | + |
|
| 337 | + |
|
| 338 | + |
|
| 339 | + /** |
|
| 340 | + * loads core classes - must be singletons |
|
| 341 | + * |
|
| 342 | + * @access public |
|
| 343 | + * @param string $class_name - simple class name ie: session |
|
| 344 | + * @param mixed $arguments |
|
| 345 | + * @param bool $load_only |
|
| 346 | + * @return mixed |
|
| 347 | + */ |
|
| 348 | + public function load_core($class_name, $arguments = array(), $load_only = false) |
|
| 349 | + { |
|
| 350 | + $core_paths = apply_filters( |
|
| 351 | + 'FHEE__EE_Registry__load_core__core_paths', |
|
| 352 | + array( |
|
| 353 | + EE_CORE, |
|
| 354 | + EE_ADMIN, |
|
| 355 | + EE_CPTS, |
|
| 356 | + EE_CORE . 'data_migration_scripts' . DS, |
|
| 357 | + EE_CORE . 'request_stack' . DS, |
|
| 358 | + EE_CORE . 'middleware' . DS, |
|
| 359 | + ) |
|
| 360 | + ); |
|
| 361 | + // retrieve instantiated class |
|
| 362 | + return $this->_load($core_paths, 'EE_', $class_name, 'core', $arguments, false, true, $load_only); |
|
| 363 | + } |
|
| 364 | + |
|
| 365 | + |
|
| 366 | + |
|
| 367 | + /** |
|
| 368 | + * loads service classes |
|
| 369 | + * |
|
| 370 | + * @access public |
|
| 371 | + * @param string $class_name - simple class name ie: session |
|
| 372 | + * @param mixed $arguments |
|
| 373 | + * @param bool $load_only |
|
| 374 | + * @return mixed |
|
| 375 | + */ |
|
| 376 | + public function load_service($class_name, $arguments = array(), $load_only = false) |
|
| 377 | + { |
|
| 378 | + $service_paths = apply_filters( |
|
| 379 | + 'FHEE__EE_Registry__load_service__service_paths', |
|
| 380 | + array( |
|
| 381 | + EE_CORE . 'services' . DS, |
|
| 382 | + ) |
|
| 383 | + ); |
|
| 384 | + // retrieve instantiated class |
|
| 385 | + return $this->_load($service_paths, 'EE_', $class_name, 'class', $arguments, false, true, $load_only); |
|
| 386 | + } |
|
| 387 | + |
|
| 388 | + |
|
| 389 | + |
|
| 390 | + /** |
|
| 391 | + * loads data_migration_scripts |
|
| 392 | + * |
|
| 393 | + * @access public |
|
| 394 | + * @param string $class_name - class name for the DMS ie: EE_DMS_Core_4_2_0 |
|
| 395 | + * @param mixed $arguments |
|
| 396 | + * @return EE_Data_Migration_Script_Base|mixed |
|
| 397 | + */ |
|
| 398 | + public function load_dms($class_name, $arguments = array()) |
|
| 399 | + { |
|
| 400 | + // retrieve instantiated class |
|
| 401 | + return $this->_load(EE_Data_Migration_Manager::instance()->get_data_migration_script_folders(), 'EE_DMS_', $class_name, 'dms', $arguments, false, false, false); |
|
| 402 | + } |
|
| 403 | + |
|
| 404 | + |
|
| 405 | + |
|
| 406 | + /** |
|
| 407 | + * loads object creating classes - must be singletons |
|
| 408 | + * |
|
| 409 | + * @param string $class_name - simple class name ie: attendee |
|
| 410 | + * @param mixed $arguments - an array of arguments to pass to the class |
|
| 411 | + * @param bool $from_db - some classes are instantiated from the db and thus call a different method to instantiate |
|
| 412 | + * @param bool $cache if you don't want the class to be stored in the internal cache (non-persistent) then set this to FALSE (ie. when instantiating model objects from client in a loop) |
|
| 413 | + * @param bool $load_only whether or not to just load the file and NOT instantiate, or load AND instantiate (default) |
|
| 414 | + * @return EE_Base_Class | bool |
|
| 415 | + */ |
|
| 416 | + public function load_class($class_name, $arguments = array(), $from_db = false, $cache = true, $load_only = false) |
|
| 417 | + { |
|
| 418 | + $paths = apply_filters('FHEE__EE_Registry__load_class__paths', array( |
|
| 419 | + EE_CORE, |
|
| 420 | + EE_CLASSES, |
|
| 421 | + EE_BUSINESS, |
|
| 422 | + )); |
|
| 423 | + // retrieve instantiated class |
|
| 424 | + return $this->_load($paths, 'EE_', $class_name, 'class', $arguments, $from_db, $cache, $load_only); |
|
| 425 | + } |
|
| 426 | + |
|
| 427 | + |
|
| 428 | + |
|
| 429 | + /** |
|
| 430 | + * loads helper classes - must be singletons |
|
| 431 | + * |
|
| 432 | + * @param string $class_name - simple class name ie: price |
|
| 433 | + * @param mixed $arguments |
|
| 434 | + * @param bool $load_only |
|
| 435 | + * @return EEH_Base | bool |
|
| 436 | + */ |
|
| 437 | + public function load_helper($class_name, $arguments = array(), $load_only = true) |
|
| 438 | + { |
|
| 439 | + // todo: add doing_it_wrong() in a few versions after all addons have had calls to this method removed |
|
| 440 | + $helper_paths = apply_filters('FHEE__EE_Registry__load_helper__helper_paths', array(EE_HELPERS)); |
|
| 441 | + // retrieve instantiated class |
|
| 442 | + return $this->_load($helper_paths, 'EEH_', $class_name, 'helper', $arguments, false, true, $load_only); |
|
| 443 | + } |
|
| 444 | + |
|
| 445 | + |
|
| 446 | + |
|
| 447 | + /** |
|
| 448 | + * loads core classes - must be singletons |
|
| 449 | + * |
|
| 450 | + * @access public |
|
| 451 | + * @param string $class_name - simple class name ie: session |
|
| 452 | + * @param mixed $arguments |
|
| 453 | + * @param bool $load_only |
|
| 454 | + * @param bool $cache whether to cache the object or not. |
|
| 455 | + * @return mixed |
|
| 456 | + */ |
|
| 457 | + public function load_lib($class_name, $arguments = array(), $load_only = false, $cache = true) |
|
| 458 | + { |
|
| 459 | + $paths = array( |
|
| 460 | + EE_LIBRARIES, |
|
| 461 | + EE_LIBRARIES . 'messages' . DS, |
|
| 462 | + EE_LIBRARIES . 'shortcodes' . DS, |
|
| 463 | + EE_LIBRARIES . 'qtips' . DS, |
|
| 464 | + EE_LIBRARIES . 'payment_methods' . DS, |
|
| 465 | + ); |
|
| 466 | + // retrieve instantiated class |
|
| 467 | + return $this->_load($paths, 'EE_', $class_name, 'lib', $arguments, false, $cache, $load_only); |
|
| 468 | + } |
|
| 469 | + |
|
| 470 | + |
|
| 471 | + |
|
| 472 | + /** |
|
| 473 | + * loads model classes - must be singletons |
|
| 474 | + * |
|
| 475 | + * @param string $class_name - simple class name ie: price |
|
| 476 | + * @param mixed $arguments |
|
| 477 | + * @param bool $load_only |
|
| 478 | + * @return EEM_Base | bool |
|
| 479 | + */ |
|
| 480 | + public function load_model($class_name, $arguments = array(), $load_only = false) |
|
| 481 | + { |
|
| 482 | + $paths = apply_filters('FHEE__EE_Registry__load_model__paths', array( |
|
| 483 | + EE_MODELS, |
|
| 484 | + EE_CORE, |
|
| 485 | + )); |
|
| 486 | + // retrieve instantiated class |
|
| 487 | + return $this->_load($paths, 'EEM_', $class_name, 'model', $arguments, false, true, $load_only); |
|
| 488 | + } |
|
| 489 | + |
|
| 490 | + |
|
| 491 | + |
|
| 492 | + /** |
|
| 493 | + * loads model classes - must be singletons |
|
| 494 | + * |
|
| 495 | + * @param string $class_name - simple class name ie: price |
|
| 496 | + * @param mixed $arguments |
|
| 497 | + * @param bool $load_only |
|
| 498 | + * @return mixed | bool |
|
| 499 | + */ |
|
| 500 | + public function load_model_class($class_name, $arguments = array(), $load_only = true) |
|
| 501 | + { |
|
| 502 | + $paths = array( |
|
| 503 | + EE_MODELS . 'fields' . DS, |
|
| 504 | + EE_MODELS . 'helpers' . DS, |
|
| 505 | + EE_MODELS . 'relations' . DS, |
|
| 506 | + EE_MODELS . 'strategies' . DS, |
|
| 507 | + ); |
|
| 508 | + // retrieve instantiated class |
|
| 509 | + return $this->_load($paths, 'EE_', $class_name, '', $arguments, false, true, $load_only); |
|
| 510 | + } |
|
| 511 | + |
|
| 512 | + |
|
| 513 | + |
|
| 514 | + /** |
|
| 515 | + * Determines if $model_name is the name of an actual EE model. |
|
| 516 | + * |
|
| 517 | + * @param string $model_name like Event, Attendee, Question_Group_Question, etc. |
|
| 518 | + * @return boolean |
|
| 519 | + */ |
|
| 520 | + public function is_model_name($model_name) |
|
| 521 | + { |
|
| 522 | + return isset($this->models[$model_name]) ? true : false; |
|
| 523 | + } |
|
| 524 | + |
|
| 525 | + |
|
| 526 | + |
|
| 527 | + /** |
|
| 528 | + * generic class loader |
|
| 529 | + * |
|
| 530 | + * @param string $path_to_file - directory path to file location, not including filename |
|
| 531 | + * @param string $file_name - file name ie: my_file.php, including extension |
|
| 532 | + * @param string $type - file type - core? class? helper? model? |
|
| 533 | + * @param mixed $arguments |
|
| 534 | + * @param bool $load_only |
|
| 535 | + * @return mixed |
|
| 536 | + */ |
|
| 537 | + public function load_file($path_to_file, $file_name, $type = '', $arguments = array(), $load_only = true) |
|
| 538 | + { |
|
| 539 | + // retrieve instantiated class |
|
| 540 | + return $this->_load($path_to_file, '', $file_name, $type, $arguments, false, true, $load_only); |
|
| 541 | + } |
|
| 542 | + |
|
| 543 | + |
|
| 544 | + |
|
| 545 | + /** |
|
| 546 | + * load_addon |
|
| 547 | + * |
|
| 548 | + * @param string $path_to_file - directory path to file location, not including filename |
|
| 549 | + * @param string $class_name - full class name ie: My_Class |
|
| 550 | + * @param string $type - file type - core? class? helper? model? |
|
| 551 | + * @param mixed $arguments |
|
| 552 | + * @param bool $load_only |
|
| 553 | + * @return EE_Addon |
|
| 554 | + */ |
|
| 555 | + public function load_addon($path_to_file, $class_name, $type = 'class', $arguments = array(), $load_only = false) |
|
| 556 | + { |
|
| 557 | + // retrieve instantiated class |
|
| 558 | + return $this->_load($path_to_file, 'addon', $class_name, $type, $arguments, false, true, $load_only); |
|
| 559 | + } |
|
| 560 | + |
|
| 561 | + |
|
| 562 | + |
|
| 563 | + /** |
|
| 564 | + * instantiates, caches, and automatically resolves dependencies |
|
| 565 | + * for classes that use a Fully Qualified Class Name. |
|
| 566 | + * if the class is not capable of being loaded using PSR-4 autoloading, |
|
| 567 | + * then you need to use one of the existing load_*() methods |
|
| 568 | + * which can resolve the classname and filepath from the passed arguments |
|
| 569 | + * |
|
| 570 | + * @param bool|string $class_name Fully Qualified Class Name |
|
| 571 | + * @param array $arguments an argument, or array of arguments to pass to the class upon instantiation |
|
| 572 | + * @param bool $cache whether to cache the instantiated object for reuse |
|
| 573 | + * @param bool $from_db some classes are instantiated from the db |
|
| 574 | + * and thus call a different method to instantiate |
|
| 575 | + * @param bool $load_only if true, will only load the file, but will NOT instantiate an object |
|
| 576 | + * @param bool|string $addon if true, will cache the object in the EE_Registry->$addons array |
|
| 577 | + * @return mixed null = failure to load or instantiate class object. |
|
| 578 | + * object = class loaded and instantiated successfully. |
|
| 579 | + * bool = fail or success when $load_only is true |
|
| 580 | + */ |
|
| 581 | + public function create( |
|
| 582 | + $class_name = false, |
|
| 583 | + $arguments = array(), |
|
| 584 | + $cache = false, |
|
| 585 | + $from_db = false, |
|
| 586 | + $load_only = false, |
|
| 587 | + $addon = false |
|
| 588 | + ) { |
|
| 589 | + $class_name = ltrim($class_name, '\\'); |
|
| 590 | + $class_name = $this->_dependency_map->get_alias($class_name); |
|
| 591 | + if ( ! class_exists($class_name)) { |
|
| 592 | + // maybe the class is registered with a preceding \ |
|
| 593 | + $class_name = strpos($class_name, '\\') !== 0 ? '\\' . $class_name : $class_name; |
|
| 594 | + // still doesn't exist ? |
|
| 595 | + if ( ! class_exists($class_name)) { |
|
| 596 | + return null; |
|
| 597 | + } |
|
| 598 | + } |
|
| 599 | + // if we're only loading the class and it already exists, then let's just return true immediately |
|
| 600 | + if ($load_only) { |
|
| 601 | + return true; |
|
| 602 | + } |
|
| 603 | + $addon = $addon ? 'addon' : ''; |
|
| 604 | + // $this->_cache_on is toggled during the recursive loading that can occur with dependency injection |
|
| 605 | + // $cache is controlled by individual calls to separate Registry loader methods like load_class() |
|
| 606 | + // $load_only is also controlled by individual calls to separate Registry loader methods like load_file() |
|
| 607 | + if ($this->_cache_on && $cache && ! $load_only) { |
|
| 608 | + // return object if it's already cached |
|
| 609 | + $cached_class = $this->_get_cached_class($class_name, $addon); |
|
| 610 | + if ($cached_class !== null) { |
|
| 611 | + return $cached_class; |
|
| 612 | + } |
|
| 613 | + } |
|
| 614 | + // instantiate the requested object |
|
| 615 | + $class_obj = $this->_create_object($class_name, $arguments, $addon, $from_db); |
|
| 616 | + if ($this->_cache_on && $cache) { |
|
| 617 | + // save it for later... kinda like gum { : $ |
|
| 618 | + $this->_set_cached_class($class_obj, $class_name, $addon, $from_db); |
|
| 619 | + } |
|
| 620 | + $this->_cache_on = true; |
|
| 621 | + return $class_obj; |
|
| 622 | + } |
|
| 623 | + |
|
| 624 | + |
|
| 625 | + |
|
| 626 | + /** |
|
| 627 | + * instantiates, caches, and injects dependencies for classes |
|
| 628 | + * |
|
| 629 | + * @param array $file_paths an array of paths to folders to look in |
|
| 630 | + * @param string $class_prefix EE or EEM or... ??? |
|
| 631 | + * @param bool|string $class_name $class name |
|
| 632 | + * @param string $type file type - core? class? helper? model? |
|
| 633 | + * @param mixed $arguments an argument or array of arguments to pass to the class upon instantiation |
|
| 634 | + * @param bool $from_db some classes are instantiated from the db |
|
| 635 | + * and thus call a different method to instantiate |
|
| 636 | + * @param bool $cache whether to cache the instantiated object for reuse |
|
| 637 | + * @param bool $load_only if true, will only load the file, but will NOT instantiate an object |
|
| 638 | + * @return null|object|bool null = failure to load or instantiate class object. |
|
| 639 | + * object = class loaded and instantiated successfully. |
|
| 640 | + * bool = fail or success when $load_only is true |
|
| 641 | + */ |
|
| 642 | + protected function _load( |
|
| 643 | + $file_paths = array(), |
|
| 644 | + $class_prefix = 'EE_', |
|
| 645 | + $class_name = false, |
|
| 646 | + $type = 'class', |
|
| 647 | + $arguments = array(), |
|
| 648 | + $from_db = false, |
|
| 649 | + $cache = true, |
|
| 650 | + $load_only = false |
|
| 651 | + ) { |
|
| 652 | + $class_name = ltrim($class_name, '\\'); |
|
| 653 | + // strip php file extension |
|
| 654 | + $class_name = str_replace('.php', '', trim($class_name)); |
|
| 655 | + // does the class have a prefix ? |
|
| 656 | + if ( ! empty($class_prefix) && $class_prefix != 'addon') { |
|
| 657 | + // make sure $class_prefix is uppercase |
|
| 658 | + $class_prefix = strtoupper(trim($class_prefix)); |
|
| 659 | + // add class prefix ONCE!!! |
|
| 660 | + $class_name = $class_prefix . str_replace($class_prefix, '', $class_name); |
|
| 661 | + } |
|
| 662 | + $class_name = $this->_dependency_map->get_alias($class_name); |
|
| 663 | + $class_exists = class_exists($class_name); |
|
| 664 | + // if we're only loading the class and it already exists, then let's just return true immediately |
|
| 665 | + if ($load_only && $class_exists) { |
|
| 666 | + return true; |
|
| 667 | + } |
|
| 668 | + // $this->_cache_on is toggled during the recursive loading that can occur with dependency injection |
|
| 669 | + // $cache is controlled by individual calls to separate Registry loader methods like load_class() |
|
| 670 | + // $load_only is also controlled by individual calls to separate Registry loader methods like load_file() |
|
| 671 | + if ($this->_cache_on && $cache && ! $load_only) { |
|
| 672 | + // return object if it's already cached |
|
| 673 | + $cached_class = $this->_get_cached_class($class_name, $class_prefix); |
|
| 674 | + if ($cached_class !== null) { |
|
| 675 | + return $cached_class; |
|
| 676 | + } |
|
| 677 | + } |
|
| 678 | + // if the class doesn't already exist.. then we need to try and find the file and load it |
|
| 679 | + if ( ! $class_exists) { |
|
| 680 | + // get full path to file |
|
| 681 | + $path = $this->_resolve_path($class_name, $type, $file_paths); |
|
| 682 | + // load the file |
|
| 683 | + $loaded = $this->_require_file($path, $class_name, $type, $file_paths); |
|
| 684 | + // if loading failed, or we are only loading a file but NOT instantiating an object |
|
| 685 | + if ( ! $loaded || $load_only) { |
|
| 686 | + // return boolean if only loading, or null if an object was expected |
|
| 687 | + return $load_only ? $loaded : null; |
|
| 688 | + } |
|
| 689 | + } |
|
| 690 | + // instantiate the requested object |
|
| 691 | + $class_obj = $this->_create_object($class_name, $arguments, $type, $from_db); |
|
| 692 | + if ($this->_cache_on && $cache) { |
|
| 693 | + // save it for later... kinda like gum { : $ |
|
| 694 | + $this->_set_cached_class($class_obj, $class_name, $class_prefix, $from_db); |
|
| 695 | + } |
|
| 696 | + $this->_cache_on = true; |
|
| 697 | + return $class_obj; |
|
| 698 | + } |
|
| 699 | + |
|
| 700 | + |
|
| 701 | + |
|
| 702 | + /** |
|
| 703 | + * _get_cached_class |
|
| 704 | + * attempts to find a cached version of the requested class |
|
| 705 | + * by looking in the following places: |
|
| 706 | + * $this->{$class_abbreviation} ie: $this->CART |
|
| 707 | + * $this->{$class_name} ie: $this->Some_Class |
|
| 708 | + * $this->LIB->{$class_name} ie: $this->LIB->Some_Class |
|
| 709 | + * $this->addon->{$class_name} ie: $this->addon->Some_Addon_Class |
|
| 710 | + * |
|
| 711 | + * @access protected |
|
| 712 | + * @param string $class_name |
|
| 713 | + * @param string $class_prefix |
|
| 714 | + * @return mixed |
|
| 715 | + */ |
|
| 716 | + protected function _get_cached_class($class_name, $class_prefix = '') |
|
| 717 | + { |
|
| 718 | + if (isset($this->_class_abbreviations[$class_name])) { |
|
| 719 | + $class_abbreviation = $this->_class_abbreviations[$class_name]; |
|
| 720 | + } else { |
|
| 721 | + // have to specify something, but not anything that will conflict |
|
| 722 | + $class_abbreviation = 'FANCY_BATMAN_PANTS'; |
|
| 723 | + } |
|
| 724 | + // check if class has already been loaded, and return it if it has been |
|
| 725 | + if (isset($this->{$class_abbreviation}) && ! is_null($this->{$class_abbreviation})) { |
|
| 726 | + return $this->{$class_abbreviation}; |
|
| 727 | + } else if (isset ($this->{$class_name})) { |
|
| 728 | + return $this->{$class_name}; |
|
| 729 | + } else if (isset ($this->LIB->{$class_name})) { |
|
| 730 | + return $this->LIB->{$class_name}; |
|
| 731 | + } else if ($class_prefix == 'addon' && isset ($this->addons->{$class_name})) { |
|
| 732 | + return $this->addons->{$class_name}; |
|
| 733 | + } |
|
| 734 | + return null; |
|
| 735 | + } |
|
| 736 | + |
|
| 737 | + |
|
| 738 | + |
|
| 739 | + /** |
|
| 740 | + * _resolve_path |
|
| 741 | + * attempts to find a full valid filepath for the requested class. |
|
| 742 | + * loops thru each of the base paths in the $file_paths array and appends : "{classname} . {file type} . php" |
|
| 743 | + * then returns that path if the target file has been found and is readable |
|
| 744 | + * |
|
| 745 | + * @access protected |
|
| 746 | + * @param string $class_name |
|
| 747 | + * @param string $type |
|
| 748 | + * @param array $file_paths |
|
| 749 | + * @return string | bool |
|
| 750 | + */ |
|
| 751 | + protected function _resolve_path($class_name, $type = '', $file_paths = array()) |
|
| 752 | + { |
|
| 753 | + // make sure $file_paths is an array |
|
| 754 | + $file_paths = is_array($file_paths) ? $file_paths : array($file_paths); |
|
| 755 | + // cycle thru paths |
|
| 756 | + foreach ($file_paths as $key => $file_path) { |
|
| 757 | + // convert all separators to proper DS, if no filepath, then use EE_CLASSES |
|
| 758 | + $file_path = $file_path ? str_replace(array('/', '\\'), DS, $file_path) : EE_CLASSES; |
|
| 759 | + // prep file type |
|
| 760 | + $type = ! empty($type) ? trim($type, '.') . '.' : ''; |
|
| 761 | + // build full file path |
|
| 762 | + $file_paths[$key] = rtrim($file_path, DS) . DS . $class_name . '.' . $type . 'php'; |
|
| 763 | + //does the file exist and can be read ? |
|
| 764 | + if (is_readable($file_paths[$key])) { |
|
| 765 | + return $file_paths[$key]; |
|
| 766 | + } |
|
| 767 | + } |
|
| 768 | + return false; |
|
| 769 | + } |
|
| 770 | + |
|
| 771 | + |
|
| 772 | + |
|
| 773 | + /** |
|
| 774 | + * _require_file |
|
| 775 | + * basically just performs a require_once() |
|
| 776 | + * but with some error handling |
|
| 777 | + * |
|
| 778 | + * @access protected |
|
| 779 | + * @param string $path |
|
| 780 | + * @param string $class_name |
|
| 781 | + * @param string $type |
|
| 782 | + * @param array $file_paths |
|
| 783 | + * @return boolean |
|
| 784 | + * @throws \EE_Error |
|
| 785 | + */ |
|
| 786 | + protected function _require_file($path, $class_name, $type = '', $file_paths = array()) |
|
| 787 | + { |
|
| 788 | + // don't give up! you gotta... |
|
| 789 | + try { |
|
| 790 | + //does the file exist and can it be read ? |
|
| 791 | + if ( ! $path) { |
|
| 792 | + // so sorry, can't find the file |
|
| 793 | + throw new EE_Error ( |
|
| 794 | + sprintf( |
|
| 795 | + __('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', 'event_espresso'), |
|
| 796 | + trim($type, '.'), |
|
| 797 | + $class_name, |
|
| 798 | + '<br />' . implode(',<br />', $file_paths) |
|
| 799 | + ) |
|
| 800 | + ); |
|
| 801 | + } |
|
| 802 | + // get the file |
|
| 803 | + require_once($path); |
|
| 804 | + // if the class isn't already declared somewhere |
|
| 805 | + if (class_exists($class_name, false) === false) { |
|
| 806 | + // so sorry, not a class |
|
| 807 | + throw new EE_Error( |
|
| 808 | + sprintf( |
|
| 809 | + __('The %s file %s does not appear to contain the %s Class.', 'event_espresso'), |
|
| 810 | + $type, |
|
| 811 | + $path, |
|
| 812 | + $class_name |
|
| 813 | + ) |
|
| 814 | + ); |
|
| 815 | + } |
|
| 816 | + } catch (EE_Error $e) { |
|
| 817 | + $e->get_error(); |
|
| 818 | + return false; |
|
| 819 | + } |
|
| 820 | + return true; |
|
| 821 | + } |
|
| 822 | + |
|
| 823 | + |
|
| 824 | + |
|
| 825 | + /** |
|
| 826 | + * _create_object |
|
| 827 | + * Attempts to instantiate the requested class via any of the |
|
| 828 | + * commonly used instantiation methods employed throughout EE. |
|
| 829 | + * The priority for instantiation is as follows: |
|
| 830 | + * - abstract classes or any class flagged as "load only" (no instantiation occurs) |
|
| 831 | + * - model objects via their 'new_instance_from_db' method |
|
| 832 | + * - model objects via their 'new_instance' method |
|
| 833 | + * - "singleton" classes" via their 'instance' method |
|
| 834 | + * - standard instantiable classes via their __constructor |
|
| 835 | + * Prior to instantiation, if the classname exists in the dependency_map, |
|
| 836 | + * then the constructor for the requested class will be examined to determine |
|
| 837 | + * if any dependencies exist, and if they can be injected. |
|
| 838 | + * If so, then those classes will be added to the array of arguments passed to the constructor |
|
| 839 | + * |
|
| 840 | + * @access protected |
|
| 841 | + * @param string $class_name |
|
| 842 | + * @param array $arguments |
|
| 843 | + * @param string $type |
|
| 844 | + * @param bool $from_db |
|
| 845 | + * @return null | object |
|
| 846 | + * @throws \EE_Error |
|
| 847 | + */ |
|
| 848 | + protected function _create_object($class_name, $arguments = array(), $type = '', $from_db = false) |
|
| 849 | + { |
|
| 850 | + $class_obj = null; |
|
| 851 | + $instantiation_mode = '0) none'; |
|
| 852 | + // don't give up! you gotta... |
|
| 853 | + try { |
|
| 854 | + // create reflection |
|
| 855 | + $reflector = $this->get_ReflectionClass($class_name); |
|
| 856 | + // make sure arguments are an array |
|
| 857 | + $arguments = is_array($arguments) ? $arguments : array($arguments); |
|
| 858 | + // and if arguments array is numerically and sequentially indexed, then we want it to remain as is, |
|
| 859 | + // else wrap it in an additional array so that it doesn't get split into multiple parameters |
|
| 860 | + $arguments = $this->_array_is_numerically_and_sequentially_indexed($arguments) |
|
| 861 | + ? $arguments |
|
| 862 | + : array($arguments); |
|
| 863 | + // attempt to inject dependencies ? |
|
| 864 | + if ($this->_dependency_map->has($class_name)) { |
|
| 865 | + $arguments = $this->_resolve_dependencies($reflector, $class_name, $arguments); |
|
| 866 | + } |
|
| 867 | + // instantiate the class if possible |
|
| 868 | + if ($reflector->isAbstract()) { |
|
| 869 | + // nothing to instantiate, loading file was enough |
|
| 870 | + // does not throw an exception so $instantiation_mode is unused |
|
| 871 | + // $instantiation_mode = "1) no constructor abstract class"; |
|
| 872 | + $class_obj = true; |
|
| 873 | + } else if ($reflector->getConstructor() === null && $reflector->isInstantiable() && empty($arguments)) { |
|
| 874 | + // no constructor = static methods only... nothing to instantiate, loading file was enough |
|
| 875 | + $instantiation_mode = "2) no constructor but instantiable"; |
|
| 876 | + $class_obj = $reflector->newInstance(); |
|
| 877 | + } else if ($from_db && method_exists($class_name, 'new_instance_from_db')) { |
|
| 878 | + $instantiation_mode = "3) new_instance_from_db()"; |
|
| 879 | + $class_obj = call_user_func_array(array($class_name, 'new_instance_from_db'), $arguments); |
|
| 880 | + } else if (method_exists($class_name, 'new_instance')) { |
|
| 881 | + $instantiation_mode = "4) new_instance()"; |
|
| 882 | + $class_obj = call_user_func_array(array($class_name, 'new_instance'), $arguments); |
|
| 883 | + } else if (method_exists($class_name, 'instance')) { |
|
| 884 | + $instantiation_mode = "5) instance()"; |
|
| 885 | + $class_obj = call_user_func_array(array($class_name, 'instance'), $arguments); |
|
| 886 | + } else if ($reflector->isInstantiable()) { |
|
| 887 | + $instantiation_mode = "6) constructor"; |
|
| 888 | + $class_obj = $reflector->newInstanceArgs($arguments); |
|
| 889 | + } else { |
|
| 890 | + // heh ? something's not right ! |
|
| 891 | + throw new EE_Error( |
|
| 892 | + sprintf( |
|
| 893 | + __('The %s file %s could not be instantiated.', 'event_espresso'), |
|
| 894 | + $type, |
|
| 895 | + $class_name |
|
| 896 | + ) |
|
| 897 | + ); |
|
| 898 | + } |
|
| 899 | + } catch (Exception $e) { |
|
| 900 | + if ( ! $e instanceof EE_Error) { |
|
| 901 | + $e = new EE_Error( |
|
| 902 | + sprintf( |
|
| 903 | + __('The following error occurred while attempting to instantiate "%1$s": %2$s %3$s %2$s instantiation mode : %4$s', 'event_espresso'), |
|
| 904 | + $class_name, |
|
| 905 | + '<br />', |
|
| 906 | + $e->getMessage(), |
|
| 907 | + $instantiation_mode |
|
| 908 | + ) |
|
| 909 | + ); |
|
| 910 | + } |
|
| 911 | + $e->get_error(); |
|
| 912 | + } |
|
| 913 | + return $class_obj; |
|
| 914 | + } |
|
| 915 | + |
|
| 916 | + |
|
| 917 | + |
|
| 918 | + /** |
|
| 919 | + * @see http://stackoverflow.com/questions/173400/how-to-check-if-php-array-is-associative-or-sequential |
|
| 920 | + * @param array $array |
|
| 921 | + * @return bool |
|
| 922 | + */ |
|
| 923 | + protected function _array_is_numerically_and_sequentially_indexed(array $array) |
|
| 924 | + { |
|
| 925 | + return ! empty($array) ? array_keys($array) === range(0, count($array) - 1) : true; |
|
| 926 | + } |
|
| 927 | + |
|
| 928 | + |
|
| 929 | + |
|
| 930 | + /** |
|
| 931 | + * getReflectionClass |
|
| 932 | + * checks if a ReflectionClass object has already been generated for a class |
|
| 933 | + * and returns that instead of creating a new one |
|
| 934 | + * |
|
| 935 | + * @access public |
|
| 936 | + * @param string $class_name |
|
| 937 | + * @return ReflectionClass |
|
| 938 | + */ |
|
| 939 | + public function get_ReflectionClass($class_name) |
|
| 940 | + { |
|
| 941 | + if ( |
|
| 942 | + ! isset($this->_reflectors[$class_name]) |
|
| 943 | + || ! $this->_reflectors[$class_name] instanceof ReflectionClass |
|
| 944 | + ) { |
|
| 945 | + $this->_reflectors[$class_name] = new ReflectionClass($class_name); |
|
| 946 | + } |
|
| 947 | + return $this->_reflectors[$class_name]; |
|
| 948 | + } |
|
| 949 | + |
|
| 950 | + |
|
| 951 | + |
|
| 952 | + /** |
|
| 953 | + * _resolve_dependencies |
|
| 954 | + * examines the constructor for the requested class to determine |
|
| 955 | + * if any dependencies exist, and if they can be injected. |
|
| 956 | + * If so, then those classes will be added to the array of arguments passed to the constructor |
|
| 957 | + * PLZ NOTE: this is achieved by type hinting the constructor params |
|
| 958 | + * For example: |
|
| 959 | + * if attempting to load a class "Foo" with the following constructor: |
|
| 960 | + * __construct( Bar $bar_class, Fighter $grohl_class ) |
|
| 961 | + * then $bar_class and $grohl_class will be added to the $arguments array, |
|
| 962 | + * but only IF they are NOT already present in the incoming arguments array, |
|
| 963 | + * and the correct classes can be loaded |
|
| 964 | + * |
|
| 965 | + * @access protected |
|
| 966 | + * @param ReflectionClass $reflector |
|
| 967 | + * @param string $class_name |
|
| 968 | + * @param array $arguments |
|
| 969 | + * @return array |
|
| 970 | + * @throws \ReflectionException |
|
| 971 | + */ |
|
| 972 | + protected function _resolve_dependencies(ReflectionClass $reflector, $class_name, $arguments = array()) |
|
| 973 | + { |
|
| 974 | + // let's examine the constructor |
|
| 975 | + $constructor = $reflector->getConstructor(); |
|
| 976 | + // whu? huh? nothing? |
|
| 977 | + if ( ! $constructor) { |
|
| 978 | + return $arguments; |
|
| 979 | + } |
|
| 980 | + // get constructor parameters |
|
| 981 | + $params = $constructor->getParameters(); |
|
| 982 | + // and the keys for the incoming arguments array so that we can compare existing arguments with what is expected |
|
| 983 | + $argument_keys = array_keys($arguments); |
|
| 984 | + // now loop thru all of the constructors expected parameters |
|
| 985 | + foreach ($params as $index => $param) { |
|
| 986 | + // is this a dependency for a specific class ? |
|
| 987 | + $param_class = $param->getClass() ? $param->getClass()->name : null; |
|
| 988 | + if ( |
|
| 989 | + // param is not even a class |
|
| 990 | + empty($param_class) |
|
| 991 | + // and something already exists in the incoming arguments for this param |
|
| 992 | + && isset($argument_keys[$index], $arguments[$argument_keys[$index]]) |
|
| 993 | + ) { |
|
| 994 | + // so let's skip this argument and move on to the next |
|
| 995 | + continue; |
|
| 996 | + } else if ( |
|
| 997 | + // parameter is type hinted as a class, exists as an incoming argument, AND it's the correct class |
|
| 998 | + ! empty($param_class) |
|
| 999 | + && isset($argument_keys[$index], $arguments[$argument_keys[$index]]) |
|
| 1000 | + && $arguments[$argument_keys[$index]] instanceof $param_class |
|
| 1001 | + ) { |
|
| 1002 | + // skip this argument and move on to the next |
|
| 1003 | + continue; |
|
| 1004 | + } else if ( |
|
| 1005 | + // parameter is type hinted as a class, and should be injected |
|
| 1006 | + ! empty($param_class) |
|
| 1007 | + && $this->_dependency_map->has_dependency_for_class($class_name, $param_class) |
|
| 1008 | + ) { |
|
| 1009 | + $arguments = $this->_resolve_dependency($class_name, $param_class, $arguments, $index); |
|
| 1010 | + } else { |
|
| 1011 | + try { |
|
| 1012 | + $arguments[$index] = $param->getDefaultValue(); |
|
| 1013 | + } catch (ReflectionException $e) { |
|
| 1014 | + throw new ReflectionException( |
|
| 1015 | + sprintf( |
|
| 1016 | + __('%1$s for parameter "$%2$s"', 'event_espresso'), |
|
| 1017 | + $e->getMessage(), |
|
| 1018 | + $param->getName() |
|
| 1019 | + ) |
|
| 1020 | + ); |
|
| 1021 | + } |
|
| 1022 | + } |
|
| 1023 | + } |
|
| 1024 | + return $arguments; |
|
| 1025 | + } |
|
| 1026 | + |
|
| 1027 | + |
|
| 1028 | + |
|
| 1029 | + /** |
|
| 1030 | + * @access protected |
|
| 1031 | + * @param string $class_name |
|
| 1032 | + * @param string $param_class |
|
| 1033 | + * @param array $arguments |
|
| 1034 | + * @param mixed $index |
|
| 1035 | + * @return array |
|
| 1036 | + */ |
|
| 1037 | + protected function _resolve_dependency($class_name, $param_class, $arguments, $index) |
|
| 1038 | + { |
|
| 1039 | + $dependency = null; |
|
| 1040 | + // should dependency be loaded from cache ? |
|
| 1041 | + $cache_on = $this->_dependency_map->loading_strategy_for_class_dependency($class_name, $param_class) |
|
| 1042 | + !== EE_Dependency_Map::load_new_object |
|
| 1043 | + ? true |
|
| 1044 | + : false; |
|
| 1045 | + // we might have a dependency... |
|
| 1046 | + // let's MAYBE try and find it in our cache if that's what's been requested |
|
| 1047 | + $cached_class = $cache_on ? $this->_get_cached_class($param_class) : null; |
|
| 1048 | + // and grab it if it exists |
|
| 1049 | + if ($cached_class instanceof $param_class) { |
|
| 1050 | + $dependency = $cached_class; |
|
| 1051 | + } else if ($param_class != $class_name) { |
|
| 1052 | + // obtain the loader method from the dependency map |
|
| 1053 | + $loader = $this->_dependency_map->class_loader($param_class); |
|
| 1054 | + // is loader a custom closure ? |
|
| 1055 | + if ($loader instanceof Closure) { |
|
| 1056 | + $dependency = $loader(); |
|
| 1057 | + } else { |
|
| 1058 | + // set the cache on property for the recursive loading call |
|
| 1059 | + $this->_cache_on = $cache_on; |
|
| 1060 | + // if not, then let's try and load it via the registry |
|
| 1061 | + if (method_exists($this, $loader)) { |
|
| 1062 | + $dependency = $this->{$loader}($param_class); |
|
| 1063 | + } else { |
|
| 1064 | + $dependency = $this->create($param_class, array(), $cache_on); |
|
| 1065 | + } |
|
| 1066 | + } |
|
| 1067 | + } |
|
| 1068 | + // did we successfully find the correct dependency ? |
|
| 1069 | + if ($dependency instanceof $param_class) { |
|
| 1070 | + // then let's inject it into the incoming array of arguments at the correct location |
|
| 1071 | + if (isset($argument_keys[$index])) { |
|
| 1072 | + $arguments[$argument_keys[$index]] = $dependency; |
|
| 1073 | + } else { |
|
| 1074 | + $arguments[$index] = $dependency; |
|
| 1075 | + } |
|
| 1076 | + } |
|
| 1077 | + return $arguments; |
|
| 1078 | + } |
|
| 1079 | + |
|
| 1080 | + |
|
| 1081 | + |
|
| 1082 | + /** |
|
| 1083 | + * _set_cached_class |
|
| 1084 | + * attempts to cache the instantiated class locally |
|
| 1085 | + * in one of the following places, in the following order: |
|
| 1086 | + * $this->{class_abbreviation} ie: $this->CART |
|
| 1087 | + * $this->{$class_name} ie: $this->Some_Class |
|
| 1088 | + * $this->addon->{$$class_name} ie: $this->addon->Some_Addon_Class |
|
| 1089 | + * $this->LIB->{$class_name} ie: $this->LIB->Some_Class |
|
| 1090 | + * |
|
| 1091 | + * @access protected |
|
| 1092 | + * @param object $class_obj |
|
| 1093 | + * @param string $class_name |
|
| 1094 | + * @param string $class_prefix |
|
| 1095 | + * @param bool $from_db |
|
| 1096 | + * @return void |
|
| 1097 | + */ |
|
| 1098 | + protected function _set_cached_class($class_obj, $class_name, $class_prefix = '', $from_db = false) |
|
| 1099 | + { |
|
| 1100 | + if (empty($class_obj)) { |
|
| 1101 | + return; |
|
| 1102 | + } |
|
| 1103 | + // return newly instantiated class |
|
| 1104 | + if (isset($this->_class_abbreviations[$class_name])) { |
|
| 1105 | + $class_abbreviation = $this->_class_abbreviations[$class_name]; |
|
| 1106 | + $this->{$class_abbreviation} = $class_obj; |
|
| 1107 | + } else if (property_exists($this, $class_name)) { |
|
| 1108 | + $this->{$class_name} = $class_obj; |
|
| 1109 | + } else if ($class_prefix == 'addon') { |
|
| 1110 | + $this->addons->{$class_name} = $class_obj; |
|
| 1111 | + } else if ( ! $from_db) { |
|
| 1112 | + $this->LIB->{$class_name} = $class_obj; |
|
| 1113 | + } |
|
| 1114 | + } |
|
| 1115 | + |
|
| 1116 | + |
|
| 1117 | + |
|
| 1118 | + /** |
|
| 1119 | + * call any loader that's been registered in the EE_Dependency_Map::$_class_loaders array |
|
| 1120 | + * |
|
| 1121 | + * @param string $classname PLEASE NOTE: the class name needs to match what's registered |
|
| 1122 | + * in the EE_Dependency_Map::$_class_loaders array, |
|
| 1123 | + * including the class prefix, ie: "EE_", "EEM_", "EEH_", etc |
|
| 1124 | + * @param array $arguments |
|
| 1125 | + * @return object |
|
| 1126 | + */ |
|
| 1127 | + public static function factory($classname, $arguments = array()) |
|
| 1128 | + { |
|
| 1129 | + $loader = self::instance()->_dependency_map->class_loader($classname); |
|
| 1130 | + if ($loader instanceof Closure) { |
|
| 1131 | + return $loader($arguments); |
|
| 1132 | + } else if (method_exists(EE_Registry::instance(), $loader)) { |
|
| 1133 | + return EE_Registry::instance()->{$loader}($classname, $arguments); |
|
| 1134 | + } |
|
| 1135 | + return null; |
|
| 1136 | + } |
|
| 1137 | + |
|
| 1138 | + |
|
| 1139 | + |
|
| 1140 | + /** |
|
| 1141 | + * Gets the addon by its name/slug (not classname. For that, just |
|
| 1142 | + * use the classname as the property name on EE_Config::instance()->addons) |
|
| 1143 | + * |
|
| 1144 | + * @param string $name |
|
| 1145 | + * @return EE_Addon |
|
| 1146 | + */ |
|
| 1147 | + public function get_addon_by_name($name) |
|
| 1148 | + { |
|
| 1149 | + foreach ($this->addons as $addon) { |
|
| 1150 | + if ($addon->name() == $name) { |
|
| 1151 | + return $addon; |
|
| 1152 | + } |
|
| 1153 | + } |
|
| 1154 | + return null; |
|
| 1155 | + } |
|
| 1156 | + |
|
| 1157 | + |
|
| 1158 | + |
|
| 1159 | + /** |
|
| 1160 | + * Gets an array of all the registered addons, where the keys are their names. (ie, what each returns for their name() function) They're already available on EE_Config::instance()->addons as properties, where each property's name is |
|
| 1161 | + * the addon's classname. So if you just want to get the addon by classname, use EE_Config::instance()->addons->{classname} |
|
| 1162 | + * |
|
| 1163 | + * @return EE_Addon[] where the KEYS are the addon's name() |
|
| 1164 | + */ |
|
| 1165 | + public function get_addons_by_name() |
|
| 1166 | + { |
|
| 1167 | + $addons = array(); |
|
| 1168 | + foreach ($this->addons as $addon) { |
|
| 1169 | + $addons[$addon->name()] = $addon; |
|
| 1170 | + } |
|
| 1171 | + return $addons; |
|
| 1172 | + } |
|
| 1173 | + |
|
| 1174 | + |
|
| 1175 | + |
|
| 1176 | + /** |
|
| 1177 | + * Resets the specified model's instance AND makes sure EE_Registry doesn't keep |
|
| 1178 | + * a stale copy of it around |
|
| 1179 | + * |
|
| 1180 | + * @param string $model_name |
|
| 1181 | + * @return \EEM_Base |
|
| 1182 | + * @throws \EE_Error |
|
| 1183 | + */ |
|
| 1184 | + public function reset_model($model_name) |
|
| 1185 | + { |
|
| 1186 | + $model_class_name = strpos($model_name, 'EEM_') !== 0 ? "EEM_{$model_name}" : $model_name; |
|
| 1187 | + if ( ! isset($this->LIB->{$model_class_name}) || ! $this->LIB->{$model_class_name} instanceof EEM_Base) { |
|
| 1188 | + return null; |
|
| 1189 | + } |
|
| 1190 | + //get that model reset it and make sure we nuke the old reference to it |
|
| 1191 | + if ($this->LIB->{$model_class_name} instanceof $model_class_name && is_callable(array($model_class_name, 'reset'))) { |
|
| 1192 | + $this->LIB->{$model_class_name} = $this->LIB->{$model_class_name}->reset(); |
|
| 1193 | + } else { |
|
| 1194 | + throw new EE_Error(sprintf(__('Model %s does not have a method "reset"', 'event_espresso'), $model_name)); |
|
| 1195 | + } |
|
| 1196 | + return $this->LIB->{$model_class_name}; |
|
| 1197 | + } |
|
| 1198 | + |
|
| 1199 | + |
|
| 1200 | + |
|
| 1201 | + /** |
|
| 1202 | + * Resets the registry. |
|
| 1203 | + * The criteria for what gets reset is based on what can be shared between sites on the same request when switch_to_blog |
|
| 1204 | + * is used in a multisite install. Here is a list of things that are NOT reset. |
|
| 1205 | + * - $_dependency_map |
|
| 1206 | + * - $_class_abbreviations |
|
| 1207 | + * - $NET_CFG (EE_Network_Config): The config is shared network wide so no need to reset. |
|
| 1208 | + * - $REQ: Still on the same request so no need to change. |
|
| 1209 | + * - $CAP: There is no site specific state in the EE_Capability class. |
|
| 1210 | + * - $SSN: Although ideally, the session should not be shared between site switches, we can't reset it because only one Session |
|
| 1211 | + * can be active in a single request. Resetting could resolve in "headers already sent" errors. |
|
| 1212 | + * - $addons: In multisite, the state of the addons is something controlled via hooks etc in a normal request. So |
|
| 1213 | + * for now, we won't reset the addons because it could break calls to an add-ons class/methods in the |
|
| 1214 | + * switch or on the restore. |
|
| 1215 | + * - $modules |
|
| 1216 | + * - $shortcodes |
|
| 1217 | + * - $widgets |
|
| 1218 | + * |
|
| 1219 | + * @param boolean $hard whether to reset data in the database too, or just refresh |
|
| 1220 | + * the Registry to its state at the beginning of the request |
|
| 1221 | + * @param boolean $reinstantiate whether to create new instances of EE_Registry's singletons too, |
|
| 1222 | + * or just reset without re-instantiating (handy to set to FALSE if you're not sure if you CAN |
|
| 1223 | + * currently reinstantiate the singletons at the moment) |
|
| 1224 | + * @param bool $reset_models Defaults to true. When false, then the models are not reset. This is so client |
|
| 1225 | + * code instead can just change the model context to a different blog id if necessary |
|
| 1226 | + * @return EE_Registry |
|
| 1227 | + */ |
|
| 1228 | + public static function reset($hard = false, $reinstantiate = true, $reset_models = true) |
|
| 1229 | + { |
|
| 1230 | + $instance = self::instance(); |
|
| 1231 | + EEH_Activation::reset(); |
|
| 1232 | + //properties that get reset |
|
| 1233 | + $instance->_cache_on = true; |
|
| 1234 | + $instance->CFG = EE_Config::reset($hard, $reinstantiate); |
|
| 1235 | + $instance->CART = null; |
|
| 1236 | + $instance->MRM = null; |
|
| 1237 | + $instance->AssetsRegistry = null; |
|
| 1238 | + $instance->AssetsRegistry = $instance->create('EventEspresso\core\services\assets\Registry'); |
|
| 1239 | + //messages reset |
|
| 1240 | + EED_Messages::reset(); |
|
| 1241 | + if ($reset_models) { |
|
| 1242 | + foreach (array_keys($instance->non_abstract_db_models) as $model_name) { |
|
| 1243 | + $instance->reset_model($model_name); |
|
| 1244 | + } |
|
| 1245 | + } |
|
| 1246 | + $instance->LIB = new stdClass(); |
|
| 1247 | + return $instance; |
|
| 1248 | + } |
|
| 1249 | + |
|
| 1250 | + |
|
| 1251 | + |
|
| 1252 | + /** |
|
| 1253 | + * @override magic methods |
|
| 1254 | + * @return void |
|
| 1255 | + */ |
|
| 1256 | + public final function __destruct() |
|
| 1257 | + { |
|
| 1258 | + } |
|
| 1259 | + |
|
| 1260 | + |
|
| 1261 | + |
|
| 1262 | + /** |
|
| 1263 | + * @param $a |
|
| 1264 | + * @param $b |
|
| 1265 | + */ |
|
| 1266 | + public final function __call($a, $b) |
|
| 1267 | + { |
|
| 1268 | + } |
|
| 1269 | + |
|
| 1270 | + |
|
| 1271 | + |
|
| 1272 | + /** |
|
| 1273 | + * @param $a |
|
| 1274 | + */ |
|
| 1275 | + public final function __get($a) |
|
| 1276 | + { |
|
| 1277 | + } |
|
| 1278 | + |
|
| 1279 | + |
|
| 1280 | + |
|
| 1281 | + /** |
|
| 1282 | + * @param $a |
|
| 1283 | + * @param $b |
|
| 1284 | + */ |
|
| 1285 | + public final function __set($a, $b) |
|
| 1286 | + { |
|
| 1287 | + } |
|
| 1288 | + |
|
| 1289 | + |
|
| 1290 | + |
|
| 1291 | + /** |
|
| 1292 | + * @param $a |
|
| 1293 | + */ |
|
| 1294 | + public final function __isset($a) |
|
| 1295 | + { |
|
| 1296 | + } |
|
| 1297 | 1297 | |
| 1298 | 1298 | |
| 1299 | 1299 | |
| 1300 | - /** |
|
| 1301 | - * @param $a |
|
| 1302 | - */ |
|
| 1303 | - public final function __unset($a) |
|
| 1304 | - { |
|
| 1305 | - } |
|
| 1300 | + /** |
|
| 1301 | + * @param $a |
|
| 1302 | + */ |
|
| 1303 | + public final function __unset($a) |
|
| 1304 | + { |
|
| 1305 | + } |
|
| 1306 | 1306 | |
| 1307 | 1307 | |
| 1308 | 1308 | |
| 1309 | - /** |
|
| 1310 | - * @return array |
|
| 1311 | - */ |
|
| 1312 | - public final function __sleep() |
|
| 1313 | - { |
|
| 1314 | - return array(); |
|
| 1315 | - } |
|
| 1309 | + /** |
|
| 1310 | + * @return array |
|
| 1311 | + */ |
|
| 1312 | + public final function __sleep() |
|
| 1313 | + { |
|
| 1314 | + return array(); |
|
| 1315 | + } |
|
| 1316 | 1316 | |
| 1317 | 1317 | |
| 1318 | 1318 | |
| 1319 | - public final function __wakeup() |
|
| 1320 | - { |
|
| 1321 | - } |
|
| 1319 | + public final function __wakeup() |
|
| 1320 | + { |
|
| 1321 | + } |
|
| 1322 | 1322 | |
| 1323 | 1323 | |
| 1324 | 1324 | |
| 1325 | - /** |
|
| 1326 | - * @return string |
|
| 1327 | - */ |
|
| 1328 | - public final function __toString() |
|
| 1329 | - { |
|
| 1330 | - return ''; |
|
| 1331 | - } |
|
| 1325 | + /** |
|
| 1326 | + * @return string |
|
| 1327 | + */ |
|
| 1328 | + public final function __toString() |
|
| 1329 | + { |
|
| 1330 | + return ''; |
|
| 1331 | + } |
|
| 1332 | 1332 | |
| 1333 | 1333 | |
| 1334 | 1334 | |
| 1335 | - public final function __invoke() |
|
| 1336 | - { |
|
| 1337 | - } |
|
| 1335 | + public final function __invoke() |
|
| 1336 | + { |
|
| 1337 | + } |
|
| 1338 | 1338 | |
| 1339 | 1339 | |
| 1340 | 1340 | |
| 1341 | - public final static function __set_state($array = array()) |
|
| 1342 | - { |
|
| 1343 | - return EE_Registry::instance(); |
|
| 1344 | - } |
|
| 1341 | + public final static function __set_state($array = array()) |
|
| 1342 | + { |
|
| 1343 | + return EE_Registry::instance(); |
|
| 1344 | + } |
|
| 1345 | 1345 | |
| 1346 | 1346 | |
| 1347 | 1347 | |
| 1348 | - public final function __clone() |
|
| 1349 | - { |
|
| 1350 | - } |
|
| 1348 | + public final function __clone() |
|
| 1349 | + { |
|
| 1350 | + } |
|
| 1351 | 1351 | |
| 1352 | 1352 | |
| 1353 | 1353 | |
| 1354 | - /** |
|
| 1355 | - * @param $a |
|
| 1356 | - * @param $b |
|
| 1357 | - */ |
|
| 1358 | - public final static function __callStatic($a, $b) |
|
| 1359 | - { |
|
| 1360 | - } |
|
| 1354 | + /** |
|
| 1355 | + * @param $a |
|
| 1356 | + * @param $b |
|
| 1357 | + */ |
|
| 1358 | + public final static function __callStatic($a, $b) |
|
| 1359 | + { |
|
| 1360 | + } |
|
| 1361 | 1361 | |
| 1362 | 1362 | |
| 1363 | 1363 | |
| 1364 | - /** |
|
| 1365 | - * Gets all the custom post type models defined |
|
| 1366 | - * |
|
| 1367 | - * @return array keys are model "short names" (Eg "Event") and keys are classnames (eg "EEM_Event") |
|
| 1368 | - */ |
|
| 1369 | - public function cpt_models() |
|
| 1370 | - { |
|
| 1371 | - $cpt_models = array(); |
|
| 1372 | - foreach ($this->non_abstract_db_models as $short_name => $classname) { |
|
| 1373 | - if (is_subclass_of($classname, 'EEM_CPT_Base')) { |
|
| 1374 | - $cpt_models[$short_name] = $classname; |
|
| 1375 | - } |
|
| 1376 | - } |
|
| 1377 | - return $cpt_models; |
|
| 1378 | - } |
|
| 1364 | + /** |
|
| 1365 | + * Gets all the custom post type models defined |
|
| 1366 | + * |
|
| 1367 | + * @return array keys are model "short names" (Eg "Event") and keys are classnames (eg "EEM_Event") |
|
| 1368 | + */ |
|
| 1369 | + public function cpt_models() |
|
| 1370 | + { |
|
| 1371 | + $cpt_models = array(); |
|
| 1372 | + foreach ($this->non_abstract_db_models as $short_name => $classname) { |
|
| 1373 | + if (is_subclass_of($classname, 'EEM_CPT_Base')) { |
|
| 1374 | + $cpt_models[$short_name] = $classname; |
|
| 1375 | + } |
|
| 1376 | + } |
|
| 1377 | + return $cpt_models; |
|
| 1378 | + } |
|
| 1379 | 1379 | |
| 1380 | 1380 | |
| 1381 | 1381 | |
| 1382 | - /** |
|
| 1383 | - * @return \EE_Config |
|
| 1384 | - */ |
|
| 1385 | - public static function CFG() |
|
| 1386 | - { |
|
| 1387 | - return self::instance()->CFG; |
|
| 1388 | - } |
|
| 1382 | + /** |
|
| 1383 | + * @return \EE_Config |
|
| 1384 | + */ |
|
| 1385 | + public static function CFG() |
|
| 1386 | + { |
|
| 1387 | + return self::instance()->CFG; |
|
| 1388 | + } |
|
| 1389 | 1389 | |
| 1390 | 1390 | |
| 1391 | 1391 | } |
@@ -49,18 +49,18 @@ |
||
| 49 | 49 | EE_Restriction_Generator_Base::get_cap_name( $this->model(), $this->action() ) => new EE_Return_None_Where_Conditions(), |
| 50 | 50 | ); |
| 51 | 51 | //there is no "ee_read_system_questions" cap; in order to read reg form items you only need "ee_read_{model_name}". |
| 52 | - //there is also no "ee_delete_system_questions" cap. But folks shouldn't be deleting system questions anyway |
|
| 52 | + //there is also no "ee_delete_system_questions" cap. But folks shouldn't be deleting system questions anyway |
|
| 53 | 53 | if ($this->action() !== EEM_Base::caps_read) { |
| 54 | - $restrictions[EE_Restriction_Generator_Base::get_cap_name($this->model(), $this->action() . '_system')] = new EE_Default_Where_Conditions( |
|
| 55 | - array( |
|
| 56 | - 'OR*no_' . EE_Restriction_Generator_Base::get_cap_name($this->model(), $this->action() . '_system') => array( |
|
| 57 | - $this->_system_field_name => array('IN', array('', 0)), |
|
| 58 | - $this->_system_field_name . '*' => array('IS_NULL') |
|
| 59 | - ) |
|
| 60 | - ) |
|
| 61 | - ); |
|
| 62 | - } |
|
| 63 | - return $restrictions; |
|
| 54 | + $restrictions[EE_Restriction_Generator_Base::get_cap_name($this->model(), $this->action() . '_system')] = new EE_Default_Where_Conditions( |
|
| 55 | + array( |
|
| 56 | + 'OR*no_' . EE_Restriction_Generator_Base::get_cap_name($this->model(), $this->action() . '_system') => array( |
|
| 57 | + $this->_system_field_name => array('IN', array('', 0)), |
|
| 58 | + $this->_system_field_name . '*' => array('IS_NULL') |
|
| 59 | + ) |
|
| 60 | + ) |
|
| 61 | + ); |
|
| 62 | + } |
|
| 63 | + return $restrictions; |
|
| 64 | 64 | } |
| 65 | 65 | } |
| 66 | 66 | |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | -if ( !defined( 'EVENT_ESPRESSO_VERSION' ) ) { |
|
| 3 | - exit( 'No direct script access allowed' ); |
|
| 2 | +if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
|
| 3 | + exit('No direct script access allowed'); |
|
| 4 | 4 | } |
| 5 | 5 | |
| 6 | 6 | /** |
@@ -15,7 +15,7 @@ discard block |
||
| 15 | 15 | * @author Mike Nelson |
| 16 | 16 | * |
| 17 | 17 | */ |
| 18 | -class EE_Restriction_Generator_Reg_Form extends EE_Restriction_Generator_Base{ |
|
| 18 | +class EE_Restriction_Generator_Reg_Form extends EE_Restriction_Generator_Base { |
|
| 19 | 19 | |
| 20 | 20 | /** |
| 21 | 21 | * |
@@ -27,7 +27,7 @@ discard block |
||
| 27 | 27 | * Accepts the name of the field that indicates whether or not an object is a "system" one or not |
| 28 | 28 | * @param string $system_field_name |
| 29 | 29 | */ |
| 30 | - public function __construct( $system_field_name ) { |
|
| 30 | + public function __construct($system_field_name) { |
|
| 31 | 31 | $this->_system_field_name = $system_field_name; |
| 32 | 32 | } |
| 33 | 33 | |
@@ -40,22 +40,22 @@ discard block |
||
| 40 | 40 | protected function _generate_restrictions() { |
| 41 | 41 | //if there are no standard caps for this model, then for now all we know |
| 42 | 42 | //if they need the default cap to access this |
| 43 | - if( ! $this->model()->cap_slug() ) { |
|
| 43 | + if ( ! $this->model()->cap_slug()) { |
|
| 44 | 44 | return array( |
| 45 | 45 | EE_Restriction_Generator_Base::get_default_restrictions_cap() => new EE_Return_None_Where_Conditions() |
| 46 | 46 | ); |
| 47 | 47 | } |
| 48 | 48 | $restrictions = array( |
| 49 | - EE_Restriction_Generator_Base::get_cap_name( $this->model(), $this->action() ) => new EE_Return_None_Where_Conditions(), |
|
| 49 | + EE_Restriction_Generator_Base::get_cap_name($this->model(), $this->action()) => new EE_Return_None_Where_Conditions(), |
|
| 50 | 50 | ); |
| 51 | 51 | //there is no "ee_read_system_questions" cap; in order to read reg form items you only need "ee_read_{model_name}". |
| 52 | 52 | //there is also no "ee_delete_system_questions" cap. But folks shouldn't be deleting system questions anyway |
| 53 | 53 | if ($this->action() !== EEM_Base::caps_read) { |
| 54 | - $restrictions[EE_Restriction_Generator_Base::get_cap_name($this->model(), $this->action() . '_system')] = new EE_Default_Where_Conditions( |
|
| 54 | + $restrictions[EE_Restriction_Generator_Base::get_cap_name($this->model(), $this->action().'_system')] = new EE_Default_Where_Conditions( |
|
| 55 | 55 | array( |
| 56 | - 'OR*no_' . EE_Restriction_Generator_Base::get_cap_name($this->model(), $this->action() . '_system') => array( |
|
| 56 | + 'OR*no_'.EE_Restriction_Generator_Base::get_cap_name($this->model(), $this->action().'_system') => array( |
|
| 57 | 57 | $this->_system_field_name => array('IN', array('', 0)), |
| 58 | - $this->_system_field_name . '*' => array('IS_NULL') |
|
| 58 | + $this->_system_field_name.'*' => array('IS_NULL') |
|
| 59 | 59 | ) |
| 60 | 60 | ) |
| 61 | 61 | ); |
@@ -29,5871 +29,5871 @@ |
||
| 29 | 29 | abstract class EEM_Base extends EE_Base |
| 30 | 30 | { |
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * Flag to indicate whether the values provided to EEM_Base have already been prepared |
|
| 34 | - * by the model object or not (ie, the model object has used the field's _prepare_for_set function on the values). |
|
| 35 | - * They almost always WILL NOT, but it's not necessarily a requirement. |
|
| 36 | - * For example, if you want to run EEM_Event::instance()->get_all(array(array('EVT_ID'=>$_GET['event_id']))); |
|
| 37 | - * |
|
| 38 | - * @var boolean |
|
| 39 | - */ |
|
| 40 | - private $_values_already_prepared_by_model_object = 0; |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * when $_values_already_prepared_by_model_object equals this, we assume |
|
| 44 | - * the data is just like form input that needs to have the model fields' |
|
| 45 | - * prepare_for_set and prepare_for_use_in_db called on it |
|
| 46 | - */ |
|
| 47 | - const not_prepared_by_model_object = 0; |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * when $_values_already_prepared_by_model_object equals this, we |
|
| 51 | - * assume this value is coming from a model object and doesn't need to have |
|
| 52 | - * prepare_for_set called on it, just prepare_for_use_in_db is used |
|
| 53 | - */ |
|
| 54 | - const prepared_by_model_object = 1; |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * when $_values_already_prepared_by_model_object equals this, we assume |
|
| 58 | - * the values are already to be used in the database (ie no processing is done |
|
| 59 | - * on them by the model's fields) |
|
| 60 | - */ |
|
| 61 | - const prepared_for_use_in_db = 2; |
|
| 62 | - |
|
| 63 | - |
|
| 64 | - protected $singular_item = 'Item'; |
|
| 65 | - |
|
| 66 | - protected $plural_item = 'Items'; |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * @type \EE_Table_Base[] $_tables array of EE_Table objects for defining which tables comprise this model. |
|
| 70 | - */ |
|
| 71 | - protected $_tables; |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * with two levels: top-level has array keys which are database table aliases (ie, keys in _tables) |
|
| 75 | - * and the value is an array. Each of those sub-arrays have keys of field names (eg 'ATT_ID', which should also be |
|
| 76 | - * variable names on the model objects (eg, EE_Attendee), and the keys should be children of EE_Model_Field |
|
| 77 | - * |
|
| 78 | - * @var \EE_Model_Field_Base[][] $_fields |
|
| 79 | - */ |
|
| 80 | - protected $_fields; |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * array of different kinds of relations |
|
| 84 | - * |
|
| 85 | - * @var \EE_Model_Relation_Base[] $_model_relations |
|
| 86 | - */ |
|
| 87 | - protected $_model_relations; |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * @var \EE_Index[] $_indexes |
|
| 91 | - */ |
|
| 92 | - protected $_indexes = array(); |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * Default strategy for getting where conditions on this model. This strategy is used to get default |
|
| 96 | - * where conditions which are added to get_all, update, and delete queries. They can be overridden |
|
| 97 | - * by setting the same columns as used in these queries in the query yourself. |
|
| 98 | - * |
|
| 99 | - * @var EE_Default_Where_Conditions |
|
| 100 | - */ |
|
| 101 | - protected $_default_where_conditions_strategy; |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * Strategy for getting conditions on this model when 'default_where_conditions' equals 'minimum'. |
|
| 105 | - * This is particularly useful when you want something between 'none' and 'default' |
|
| 106 | - * |
|
| 107 | - * @var EE_Default_Where_Conditions |
|
| 108 | - */ |
|
| 109 | - protected $_minimum_where_conditions_strategy; |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * String describing how to find the "owner" of this model's objects. |
|
| 113 | - * When there is a foreign key on this model to the wp_users table, this isn't needed. |
|
| 114 | - * But when there isn't, this indicates which related model, or transiently-related model, |
|
| 115 | - * has the foreign key to the wp_users table. |
|
| 116 | - * Eg, for EEM_Registration this would be 'Event' because registrations are directly |
|
| 117 | - * related to events, and events have a foreign key to wp_users. |
|
| 118 | - * On EEM_Transaction, this would be 'Transaction.Event' |
|
| 119 | - * |
|
| 120 | - * @var string |
|
| 121 | - */ |
|
| 122 | - protected $_model_chain_to_wp_user = ''; |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * This is a flag typically set by updates so that we don't load the where strategy on updates because updates |
|
| 126 | - * don't need it (particularly CPT models) |
|
| 127 | - * |
|
| 128 | - * @var bool |
|
| 129 | - */ |
|
| 130 | - protected $_ignore_where_strategy = false; |
|
| 131 | - |
|
| 132 | - /** |
|
| 133 | - * String used in caps relating to this model. Eg, if the caps relating to this |
|
| 134 | - * model are 'ee_edit_events', 'ee_read_events', etc, it would be 'events'. |
|
| 135 | - * |
|
| 136 | - * @var string. If null it hasn't been initialized yet. If false then we |
|
| 137 | - * have indicated capabilities don't apply to this |
|
| 138 | - */ |
|
| 139 | - protected $_caps_slug = null; |
|
| 140 | - |
|
| 141 | - /** |
|
| 142 | - * 2d array where top-level keys are one of EEM_Base::valid_cap_contexts(), |
|
| 143 | - * and next-level keys are capability names, and each's value is a |
|
| 144 | - * EE_Default_Where_Condition. If the requester requests to apply caps to the query, |
|
| 145 | - * they specify which context to use (ie, frontend, backend, edit or delete) |
|
| 146 | - * and then each capability in the corresponding sub-array that they're missing |
|
| 147 | - * adds the where conditions onto the query. |
|
| 148 | - * |
|
| 149 | - * @var array |
|
| 150 | - */ |
|
| 151 | - protected $_cap_restrictions = array( |
|
| 152 | - self::caps_read => array(), |
|
| 153 | - self::caps_read_admin => array(), |
|
| 154 | - self::caps_edit => array(), |
|
| 155 | - self::caps_delete => array(), |
|
| 156 | - ); |
|
| 157 | - |
|
| 158 | - /** |
|
| 159 | - * Array defining which cap restriction generators to use to create default |
|
| 160 | - * cap restrictions to put in EEM_Base::_cap_restrictions. |
|
| 161 | - * Array-keys are one of EEM_Base::valid_cap_contexts(), and values are a child of |
|
| 162 | - * EE_Restriction_Generator_Base. If you don't want any cap restrictions generated |
|
| 163 | - * automatically set this to false (not just null). |
|
| 164 | - * |
|
| 165 | - * @var EE_Restriction_Generator_Base[] |
|
| 166 | - */ |
|
| 167 | - protected $_cap_restriction_generators = array(); |
|
| 168 | - |
|
| 169 | - /** |
|
| 170 | - * constants used to categorize capability restrictions on EEM_Base::_caps_restrictions |
|
| 171 | - */ |
|
| 172 | - const caps_read = 'read'; |
|
| 173 | - |
|
| 174 | - const caps_read_admin = 'read_admin'; |
|
| 175 | - |
|
| 176 | - const caps_edit = 'edit'; |
|
| 177 | - |
|
| 178 | - const caps_delete = 'delete'; |
|
| 179 | - |
|
| 180 | - /** |
|
| 181 | - * Keys are all the cap contexts (ie constants EEM_Base::_caps_*) and values are their 'action' |
|
| 182 | - * as how they'd be used in capability names. Eg EEM_Base::caps_read ('read_frontend') |
|
| 183 | - * maps to 'read' because when looking for relevant permissions we're going to use |
|
| 184 | - * 'read' in teh capabilities names like 'ee_read_events' etc. |
|
| 185 | - * |
|
| 186 | - * @var array |
|
| 187 | - */ |
|
| 188 | - protected $_cap_contexts_to_cap_action_map = array( |
|
| 189 | - self::caps_read => 'read', |
|
| 190 | - self::caps_read_admin => 'read', |
|
| 191 | - self::caps_edit => 'edit', |
|
| 192 | - self::caps_delete => 'delete', |
|
| 193 | - ); |
|
| 194 | - |
|
| 195 | - /** |
|
| 196 | - * Timezone |
|
| 197 | - * This gets set via the constructor so that we know what timezone incoming strings|timestamps are in when there |
|
| 198 | - * are EE_Datetime_Fields in use. This can also be used before a get to set what timezone you want strings coming |
|
| 199 | - * out of the created objects. NOT all EEM_Base child classes use this property but any that use a |
|
| 200 | - * EE_Datetime_Field data type will have access to it. |
|
| 201 | - * |
|
| 202 | - * @var string |
|
| 203 | - */ |
|
| 204 | - protected $_timezone; |
|
| 205 | - |
|
| 206 | - |
|
| 207 | - /** |
|
| 208 | - * This holds the id of the blog currently making the query. Has no bearing on single site but is used for |
|
| 209 | - * multisite. |
|
| 210 | - * |
|
| 211 | - * @var int |
|
| 212 | - */ |
|
| 213 | - protected static $_model_query_blog_id; |
|
| 214 | - |
|
| 215 | - /** |
|
| 216 | - * A copy of _fields, except the array keys are the model names pointed to by |
|
| 217 | - * the field |
|
| 218 | - * |
|
| 219 | - * @var EE_Model_Field_Base[] |
|
| 220 | - */ |
|
| 221 | - private $_cache_foreign_key_to_fields = array(); |
|
| 222 | - |
|
| 223 | - /** |
|
| 224 | - * Cached list of all the fields on the model, indexed by their name |
|
| 225 | - * |
|
| 226 | - * @var EE_Model_Field_Base[] |
|
| 227 | - */ |
|
| 228 | - private $_cached_fields = null; |
|
| 229 | - |
|
| 230 | - /** |
|
| 231 | - * Cached list of all the fields on the model, except those that are |
|
| 232 | - * marked as only pertinent to the database |
|
| 233 | - * |
|
| 234 | - * @var EE_Model_Field_Base[] |
|
| 235 | - */ |
|
| 236 | - private $_cached_fields_non_db_only = null; |
|
| 237 | - |
|
| 238 | - /** |
|
| 239 | - * A cached reference to the primary key for quick lookup |
|
| 240 | - * |
|
| 241 | - * @var EE_Model_Field_Base |
|
| 242 | - */ |
|
| 243 | - private $_primary_key_field = null; |
|
| 244 | - |
|
| 245 | - /** |
|
| 246 | - * Flag indicating whether this model has a primary key or not |
|
| 247 | - * |
|
| 248 | - * @var boolean |
|
| 249 | - */ |
|
| 250 | - protected $_has_primary_key_field = null; |
|
| 251 | - |
|
| 252 | - /** |
|
| 253 | - * Whether or not this model is based off a table in WP core only (CPTs should set |
|
| 254 | - * this to FALSE, but if we were to make an EE_WP_Post model, it should set this to true). |
|
| 255 | - * This should be true for models that deal with data that should exist independent of EE. |
|
| 256 | - * For example, if the model can read and insert data that isn't used by EE, this should be true. |
|
| 257 | - * It would be false, however, if you could guarantee the model would only interact with EE data, |
|
| 258 | - * even if it uses a WP core table (eg event and venue models set this to false for that reason: |
|
| 259 | - * they can only read and insert events and venues custom post types, not arbitrary post types) |
|
| 260 | - * @var boolean |
|
| 261 | - */ |
|
| 262 | - protected $_wp_core_model = false; |
|
| 263 | - |
|
| 264 | - /** |
|
| 265 | - * List of valid operators that can be used for querying. |
|
| 266 | - * The keys are all operators we'll accept, the values are the real SQL |
|
| 267 | - * operators used |
|
| 268 | - * |
|
| 269 | - * @var array |
|
| 270 | - */ |
|
| 271 | - protected $_valid_operators = array( |
|
| 272 | - '=' => '=', |
|
| 273 | - '<=' => '<=', |
|
| 274 | - '<' => '<', |
|
| 275 | - '>=' => '>=', |
|
| 276 | - '>' => '>', |
|
| 277 | - '!=' => '!=', |
|
| 278 | - 'LIKE' => 'LIKE', |
|
| 279 | - 'like' => 'LIKE', |
|
| 280 | - 'NOT_LIKE' => 'NOT LIKE', |
|
| 281 | - 'not_like' => 'NOT LIKE', |
|
| 282 | - 'NOT LIKE' => 'NOT LIKE', |
|
| 283 | - 'not like' => 'NOT LIKE', |
|
| 284 | - 'IN' => 'IN', |
|
| 285 | - 'in' => 'IN', |
|
| 286 | - 'NOT_IN' => 'NOT IN', |
|
| 287 | - 'not_in' => 'NOT IN', |
|
| 288 | - 'NOT IN' => 'NOT IN', |
|
| 289 | - 'not in' => 'NOT IN', |
|
| 290 | - 'between' => 'BETWEEN', |
|
| 291 | - 'BETWEEN' => 'BETWEEN', |
|
| 292 | - 'IS_NOT_NULL' => 'IS NOT NULL', |
|
| 293 | - 'is_not_null' => 'IS NOT NULL', |
|
| 294 | - 'IS NOT NULL' => 'IS NOT NULL', |
|
| 295 | - 'is not null' => 'IS NOT NULL', |
|
| 296 | - 'IS_NULL' => 'IS NULL', |
|
| 297 | - 'is_null' => 'IS NULL', |
|
| 298 | - 'IS NULL' => 'IS NULL', |
|
| 299 | - 'is null' => 'IS NULL', |
|
| 300 | - 'REGEXP' => 'REGEXP', |
|
| 301 | - 'regexp' => 'REGEXP', |
|
| 302 | - 'NOT_REGEXP' => 'NOT REGEXP', |
|
| 303 | - 'not_regexp' => 'NOT REGEXP', |
|
| 304 | - 'NOT REGEXP' => 'NOT REGEXP', |
|
| 305 | - 'not regexp' => 'NOT REGEXP', |
|
| 306 | - ); |
|
| 307 | - |
|
| 308 | - /** |
|
| 309 | - * operators that work like 'IN', accepting a comma-separated list of values inside brackets. Eg '(1,2,3)' |
|
| 310 | - * |
|
| 311 | - * @var array |
|
| 312 | - */ |
|
| 313 | - protected $_in_style_operators = array('IN', 'NOT IN'); |
|
| 314 | - |
|
| 315 | - /** |
|
| 316 | - * operators that work like 'BETWEEN'. Typically used for datetime calculations, i.e. "BETWEEN '12-1-2011' AND |
|
| 317 | - * '12-31-2012'" |
|
| 318 | - * |
|
| 319 | - * @var array |
|
| 320 | - */ |
|
| 321 | - protected $_between_style_operators = array('BETWEEN'); |
|
| 322 | - |
|
| 323 | - /** |
|
| 324 | - * Operators that work like SQL's like: input should be assumed to be a string, already prepared for a LIKE query. |
|
| 325 | - * @var array |
|
| 326 | - */ |
|
| 327 | - protected $_like_style_operators = array('LIKE', 'NOT LIKE'); |
|
| 328 | - /** |
|
| 329 | - * operators that are used for handling NUll and !NULL queries. Typically used for when checking if a row exists |
|
| 330 | - * on a join table. |
|
| 331 | - * |
|
| 332 | - * @var array |
|
| 333 | - */ |
|
| 334 | - protected $_null_style_operators = array('IS NOT NULL', 'IS NULL'); |
|
| 335 | - |
|
| 336 | - /** |
|
| 337 | - * Allowed values for $query_params['order'] for ordering in queries |
|
| 338 | - * |
|
| 339 | - * @var array |
|
| 340 | - */ |
|
| 341 | - protected $_allowed_order_values = array('asc', 'desc', 'ASC', 'DESC'); |
|
| 342 | - |
|
| 343 | - /** |
|
| 344 | - * When these are keys in a WHERE or HAVING clause, they are handled much differently |
|
| 345 | - * than regular field names. It is assumed that their values are an array of WHERE conditions |
|
| 346 | - * |
|
| 347 | - * @var array |
|
| 348 | - */ |
|
| 349 | - private $_logic_query_param_keys = array('not', 'and', 'or', 'NOT', 'AND', 'OR'); |
|
| 350 | - |
|
| 351 | - /** |
|
| 352 | - * Allowed keys in $query_params arrays passed into queries. Note that 0 is meant to always be a |
|
| 353 | - * 'where', but 'where' clauses are so common that we thought we'd omit it |
|
| 354 | - * |
|
| 355 | - * @var array |
|
| 356 | - */ |
|
| 357 | - private $_allowed_query_params = array( |
|
| 358 | - 0, |
|
| 359 | - 'limit', |
|
| 360 | - 'order_by', |
|
| 361 | - 'group_by', |
|
| 362 | - 'having', |
|
| 363 | - 'force_join', |
|
| 364 | - 'order', |
|
| 365 | - 'on_join_limit', |
|
| 366 | - 'default_where_conditions', |
|
| 367 | - 'caps', |
|
| 368 | - ); |
|
| 369 | - |
|
| 370 | - /** |
|
| 371 | - * All the data types that can be used in $wpdb->prepare statements. |
|
| 372 | - * |
|
| 373 | - * @var array |
|
| 374 | - */ |
|
| 375 | - private $_valid_wpdb_data_types = array('%d', '%s', '%f'); |
|
| 376 | - |
|
| 377 | - /** |
|
| 378 | - * EE_Registry Object |
|
| 379 | - * |
|
| 380 | - * @var object |
|
| 381 | - * @access protected |
|
| 382 | - */ |
|
| 383 | - protected $EE = null; |
|
| 384 | - |
|
| 385 | - |
|
| 386 | - /** |
|
| 387 | - * Property which, when set, will have this model echo out the next X queries to the page for debugging. |
|
| 388 | - * |
|
| 389 | - * @var int |
|
| 390 | - */ |
|
| 391 | - protected $_show_next_x_db_queries = 0; |
|
| 392 | - |
|
| 393 | - /** |
|
| 394 | - * When using _get_all_wpdb_results, you can specify a custom selection. If you do so, |
|
| 395 | - * it gets saved on this property so those selections can be used in WHERE, GROUP_BY, etc. |
|
| 396 | - * |
|
| 397 | - * @var array |
|
| 398 | - */ |
|
| 399 | - protected $_custom_selections = array(); |
|
| 400 | - |
|
| 401 | - /** |
|
| 402 | - * key => value Entity Map using array( EEM_Base::$_model_query_blog_id => array( ID => model object ) ) |
|
| 403 | - * caches every model object we've fetched from the DB on this request |
|
| 404 | - * |
|
| 405 | - * @var array |
|
| 406 | - */ |
|
| 407 | - protected $_entity_map; |
|
| 408 | - |
|
| 409 | - /** |
|
| 410 | - * constant used to show EEM_Base has not yet verified the db on this http request |
|
| 411 | - */ |
|
| 412 | - const db_verified_none = 0; |
|
| 413 | - |
|
| 414 | - /** |
|
| 415 | - * constant used to show EEM_Base has verified the EE core db on this http request, |
|
| 416 | - * but not the addons' dbs |
|
| 417 | - */ |
|
| 418 | - const db_verified_core = 1; |
|
| 419 | - |
|
| 420 | - /** |
|
| 421 | - * constant used to show EEM_Base has verified the addons' dbs (and implicitly |
|
| 422 | - * the EE core db too) |
|
| 423 | - */ |
|
| 424 | - const db_verified_addons = 2; |
|
| 425 | - |
|
| 426 | - /** |
|
| 427 | - * indicates whether an EEM_Base child has already re-verified the DB |
|
| 428 | - * is ok (we don't want to do it repetitively). Should be set to one the constants |
|
| 429 | - * looking like EEM_Base::db_verified_* |
|
| 430 | - * |
|
| 431 | - * @var int - 0 = none, 1 = core, 2 = addons |
|
| 432 | - */ |
|
| 433 | - protected static $_db_verification_level = EEM_Base::db_verified_none; |
|
| 434 | - |
|
| 435 | - /** |
|
| 436 | - * @const constant for 'default_where_conditions' to apply default where conditions to ALL queried models |
|
| 437 | - * (eg, if retrieving registrations ordered by their datetimes, this will only return non-trashed |
|
| 438 | - * registrations for non-trashed tickets for non-trashed datetimes) |
|
| 439 | - */ |
|
| 440 | - const default_where_conditions_all = 'all'; |
|
| 441 | - |
|
| 442 | - /** |
|
| 443 | - * @const constant for 'default_where_conditions' to apply default where conditions to THIS model only, but |
|
| 444 | - * no other models which are joined to (eg, if retrieving registrations ordered by their datetimes, this will |
|
| 445 | - * return non-trashed registrations, regardless of the related datetimes and tickets' statuses). |
|
| 446 | - * It is preferred to use EEM_Base::default_where_conditions_minimum_others because, when joining to |
|
| 447 | - * models which share tables with other models, this can return data for the wrong model. |
|
| 448 | - */ |
|
| 449 | - const default_where_conditions_this_only = 'this_model_only'; |
|
| 450 | - |
|
| 451 | - /** |
|
| 452 | - * @const constant for 'default_where_conditions' to apply default where conditions to other models queried, |
|
| 453 | - * but not the current model (eg, if retrieving registrations ordered by their datetimes, this will |
|
| 454 | - * return all registrations related to non-trashed tickets and non-trashed datetimes) |
|
| 455 | - */ |
|
| 456 | - const default_where_conditions_others_only = 'other_models_only'; |
|
| 457 | - |
|
| 458 | - /** |
|
| 459 | - * @const constant for 'default_where_conditions' to apply minimum where conditions to all models queried. |
|
| 460 | - * For most models this the same as EEM_Base::default_where_conditions_none, except for models which share |
|
| 461 | - * their table with other models, like the Event and Venue models. For example, when querying for events |
|
| 462 | - * ordered by their venues' name, this will be sure to only return real events with associated real venues |
|
| 463 | - * (regardless of whether those events and venues are trashed) |
|
| 464 | - * In contrast, using EEM_Base::default_where_conditions_none would could return WP posts other than EE |
|
| 465 | - * events. |
|
| 466 | - */ |
|
| 467 | - const default_where_conditions_minimum_all = 'minimum'; |
|
| 468 | - |
|
| 469 | - /** |
|
| 470 | - * @const constant for 'default_where_conditions' to apply apply where conditions to other models, and full default |
|
| 471 | - * where conditions for the queried model (eg, when querying events ordered by venues' names, this will |
|
| 472 | - * return non-trashed events for any venues, regardless of whether those associated venues are trashed or |
|
| 473 | - * not) |
|
| 474 | - */ |
|
| 475 | - const default_where_conditions_minimum_others = 'full_this_minimum_others'; |
|
| 476 | - |
|
| 477 | - /** |
|
| 478 | - * @const constant for 'default_where_conditions' to NOT apply any where conditions. This should very rarely be |
|
| 479 | - * used, because when querying from a model which shares its table with another model (eg Events and Venues) |
|
| 480 | - * it's possible it will return table entries for other models. You should use |
|
| 481 | - * EEM_Base::default_where_conditions_minimum_all instead. |
|
| 482 | - */ |
|
| 483 | - const default_where_conditions_none = 'none'; |
|
| 484 | - |
|
| 485 | - |
|
| 486 | - |
|
| 487 | - /** |
|
| 488 | - * About all child constructors: |
|
| 489 | - * they should define the _tables, _fields and _model_relations arrays. |
|
| 490 | - * Should ALWAYS be called after child constructor. |
|
| 491 | - * In order to make the child constructors to be as simple as possible, this parent constructor |
|
| 492 | - * finalizes constructing all the object's attributes. |
|
| 493 | - * Generally, rather than requiring a child to code |
|
| 494 | - * $this->_tables = array( |
|
| 495 | - * 'Event_Post_Table' => new EE_Table('Event_Post_Table','wp_posts') |
|
| 496 | - * ...); |
|
| 497 | - * (thus repeating itself in the array key and in the constructor of the new EE_Table,) |
|
| 498 | - * each EE_Table has a function to set the table's alias after the constructor, using |
|
| 499 | - * the array key ('Event_Post_Table'), instead of repeating it. The model fields and model relations |
|
| 500 | - * do something similar. |
|
| 501 | - * |
|
| 502 | - * @param null $timezone |
|
| 503 | - * @throws \EE_Error |
|
| 504 | - */ |
|
| 505 | - protected function __construct($timezone = null) |
|
| 506 | - { |
|
| 507 | - // check that the model has not been loaded too soon |
|
| 508 | - if (! did_action('AHEE__EE_System__load_espresso_addons')) { |
|
| 509 | - throw new EE_Error ( |
|
| 510 | - sprintf( |
|
| 511 | - __('The %1$s model can not be loaded before the "AHEE__EE_System__load_espresso_addons" hook has been called. This gives other addons a chance to extend this model.', |
|
| 512 | - 'event_espresso'), |
|
| 513 | - get_class($this) |
|
| 514 | - ) |
|
| 515 | - ); |
|
| 516 | - } |
|
| 517 | - /** |
|
| 518 | - * Set blogid for models to current blog. However we ONLY do this if $_model_query_blog_id is not already set. |
|
| 519 | - */ |
|
| 520 | - if (empty(EEM_Base::$_model_query_blog_id)) { |
|
| 521 | - EEM_Base::set_model_query_blog_id(); |
|
| 522 | - } |
|
| 523 | - /** |
|
| 524 | - * Filters the list of tables on a model. It is best to NOT use this directly and instead |
|
| 525 | - * just use EE_Register_Model_Extension |
|
| 526 | - * |
|
| 527 | - * @var EE_Table_Base[] $_tables |
|
| 528 | - */ |
|
| 529 | - $this->_tables = apply_filters('FHEE__' . get_class($this) . '__construct__tables', $this->_tables); |
|
| 530 | - foreach ($this->_tables as $table_alias => $table_obj) { |
|
| 531 | - /** @var $table_obj EE_Table_Base */ |
|
| 532 | - $table_obj->_construct_finalize_with_alias($table_alias); |
|
| 533 | - if ($table_obj instanceof EE_Secondary_Table) { |
|
| 534 | - /** @var $table_obj EE_Secondary_Table */ |
|
| 535 | - $table_obj->_construct_finalize_set_table_to_join_with($this->_get_main_table()); |
|
| 536 | - } |
|
| 537 | - } |
|
| 538 | - /** |
|
| 539 | - * Filters the list of fields on a model. It is best to NOT use this directly and instead just use |
|
| 540 | - * EE_Register_Model_Extension |
|
| 541 | - * |
|
| 542 | - * @param EE_Model_Field_Base[] $_fields |
|
| 543 | - */ |
|
| 544 | - $this->_fields = apply_filters('FHEE__' . get_class($this) . '__construct__fields', $this->_fields); |
|
| 545 | - $this->_invalidate_field_caches(); |
|
| 546 | - foreach ($this->_fields as $table_alias => $fields_for_table) { |
|
| 547 | - if (! array_key_exists($table_alias, $this->_tables)) { |
|
| 548 | - throw new EE_Error(sprintf(__("Table alias %s does not exist in EEM_Base child's _tables array. Only tables defined are %s", |
|
| 549 | - 'event_espresso'), $table_alias, implode(",", $this->_fields))); |
|
| 550 | - } |
|
| 551 | - foreach ($fields_for_table as $field_name => $field_obj) { |
|
| 552 | - /** @var $field_obj EE_Model_Field_Base | EE_Primary_Key_Field_Base */ |
|
| 553 | - //primary key field base has a slightly different _construct_finalize |
|
| 554 | - /** @var $field_obj EE_Model_Field_Base */ |
|
| 555 | - $field_obj->_construct_finalize($table_alias, $field_name, $this->get_this_model_name()); |
|
| 556 | - } |
|
| 557 | - } |
|
| 558 | - // everything is related to Extra_Meta |
|
| 559 | - if (get_class($this) !== 'EEM_Extra_Meta') { |
|
| 560 | - //make extra meta related to everything, but don't block deleting things just |
|
| 561 | - //because they have related extra meta info. For now just orphan those extra meta |
|
| 562 | - //in the future we should automatically delete them |
|
| 563 | - $this->_model_relations['Extra_Meta'] = new EE_Has_Many_Any_Relation(false); |
|
| 564 | - } |
|
| 565 | - //and change logs |
|
| 566 | - if (get_class($this) !== 'EEM_Change_Log') { |
|
| 567 | - $this->_model_relations['Change_Log'] = new EE_Has_Many_Any_Relation(false); |
|
| 568 | - } |
|
| 569 | - /** |
|
| 570 | - * Filters the list of relations on a model. It is best to NOT use this directly and instead just use |
|
| 571 | - * EE_Register_Model_Extension |
|
| 572 | - * |
|
| 573 | - * @param EE_Model_Relation_Base[] $_model_relations |
|
| 574 | - */ |
|
| 575 | - $this->_model_relations = apply_filters('FHEE__' . get_class($this) . '__construct__model_relations', |
|
| 576 | - $this->_model_relations); |
|
| 577 | - foreach ($this->_model_relations as $model_name => $relation_obj) { |
|
| 578 | - /** @var $relation_obj EE_Model_Relation_Base */ |
|
| 579 | - $relation_obj->_construct_finalize_set_models($this->get_this_model_name(), $model_name); |
|
| 580 | - } |
|
| 581 | - foreach ($this->_indexes as $index_name => $index_obj) { |
|
| 582 | - /** @var $index_obj EE_Index */ |
|
| 583 | - $index_obj->_construct_finalize($index_name, $this->get_this_model_name()); |
|
| 584 | - } |
|
| 585 | - $this->set_timezone($timezone); |
|
| 586 | - //finalize default where condition strategy, or set default |
|
| 587 | - if (! $this->_default_where_conditions_strategy) { |
|
| 588 | - //nothing was set during child constructor, so set default |
|
| 589 | - $this->_default_where_conditions_strategy = new EE_Default_Where_Conditions(); |
|
| 590 | - } |
|
| 591 | - $this->_default_where_conditions_strategy->_finalize_construct($this); |
|
| 592 | - if (! $this->_minimum_where_conditions_strategy) { |
|
| 593 | - //nothing was set during child constructor, so set default |
|
| 594 | - $this->_minimum_where_conditions_strategy = new EE_Default_Where_Conditions(); |
|
| 595 | - } |
|
| 596 | - $this->_minimum_where_conditions_strategy->_finalize_construct($this); |
|
| 597 | - //if the cap slug hasn't been set, and we haven't set it to false on purpose |
|
| 598 | - //to indicate to NOT set it, set it to the logical default |
|
| 599 | - if ($this->_caps_slug === null) { |
|
| 600 | - $this->_caps_slug = EEH_Inflector::pluralize_and_lower($this->get_this_model_name()); |
|
| 601 | - } |
|
| 602 | - //initialize the standard cap restriction generators if none were specified by the child constructor |
|
| 603 | - if ($this->_cap_restriction_generators !== false) { |
|
| 604 | - foreach ($this->cap_contexts_to_cap_action_map() as $cap_context => $action) { |
|
| 605 | - if (! isset($this->_cap_restriction_generators[$cap_context])) { |
|
| 606 | - $this->_cap_restriction_generators[$cap_context] = apply_filters( |
|
| 607 | - 'FHEE__EEM_Base___construct__standard_cap_restriction_generator', |
|
| 608 | - new EE_Restriction_Generator_Protected(), |
|
| 609 | - $cap_context, |
|
| 610 | - $this |
|
| 611 | - ); |
|
| 612 | - } |
|
| 613 | - } |
|
| 614 | - } |
|
| 615 | - //if there are cap restriction generators, use them to make the default cap restrictions |
|
| 616 | - if ($this->_cap_restriction_generators !== false) { |
|
| 617 | - foreach ($this->_cap_restriction_generators as $context => $generator_object) { |
|
| 618 | - if (! $generator_object) { |
|
| 619 | - continue; |
|
| 620 | - } |
|
| 621 | - if (! $generator_object instanceof EE_Restriction_Generator_Base) { |
|
| 622 | - throw new EE_Error( |
|
| 623 | - sprintf( |
|
| 624 | - __('Index "%1$s" in the model %2$s\'s _cap_restriction_generators is not a child of EE_Restriction_Generator_Base. It should be that or NULL.', |
|
| 625 | - 'event_espresso'), |
|
| 626 | - $context, |
|
| 627 | - $this->get_this_model_name() |
|
| 628 | - ) |
|
| 629 | - ); |
|
| 630 | - } |
|
| 631 | - $action = $this->cap_action_for_context($context); |
|
| 632 | - if (! $generator_object->construction_finalized()) { |
|
| 633 | - $generator_object->_construct_finalize($this, $action); |
|
| 634 | - } |
|
| 635 | - } |
|
| 636 | - } |
|
| 637 | - do_action('AHEE__' . get_class($this) . '__construct__end'); |
|
| 638 | - } |
|
| 639 | - |
|
| 640 | - |
|
| 641 | - |
|
| 642 | - /** |
|
| 643 | - * Used to set the $_model_query_blog_id static property. |
|
| 644 | - * |
|
| 645 | - * @param int $blog_id If provided then will set the blog_id for the models to this id. If not provided then the |
|
| 646 | - * value for get_current_blog_id() will be used. |
|
| 647 | - */ |
|
| 648 | - public static function set_model_query_blog_id($blog_id = 0) |
|
| 649 | - { |
|
| 650 | - EEM_Base::$_model_query_blog_id = $blog_id > 0 ? (int)$blog_id : get_current_blog_id(); |
|
| 651 | - } |
|
| 652 | - |
|
| 653 | - |
|
| 654 | - |
|
| 655 | - /** |
|
| 656 | - * Returns whatever is set as the internal $model_query_blog_id. |
|
| 657 | - * |
|
| 658 | - * @return int |
|
| 659 | - */ |
|
| 660 | - public static function get_model_query_blog_id() |
|
| 661 | - { |
|
| 662 | - return EEM_Base::$_model_query_blog_id; |
|
| 663 | - } |
|
| 664 | - |
|
| 665 | - |
|
| 666 | - |
|
| 667 | - /** |
|
| 668 | - * This function is a singleton method used to instantiate the Espresso_model object |
|
| 669 | - * |
|
| 670 | - * @access public |
|
| 671 | - * @param string $timezone string representing the timezone we want to set for returned Date Time Strings (and any |
|
| 672 | - * incoming timezone data that gets saved). Note this just sends the timezone info to the |
|
| 673 | - * date time model field objects. Default is NULL (and will be assumed using the set |
|
| 674 | - * timezone in the 'timezone_string' wp option) |
|
| 675 | - * @return static (as in the concrete child class) |
|
| 676 | - */ |
|
| 677 | - public static function instance($timezone = null) |
|
| 678 | - { |
|
| 679 | - // check if instance of Espresso_model already exists |
|
| 680 | - if (! static::$_instance instanceof static) { |
|
| 681 | - // instantiate Espresso_model |
|
| 682 | - static::$_instance = new static($timezone); |
|
| 683 | - } |
|
| 684 | - //we might have a timezone set, let set_timezone decide what to do with it |
|
| 685 | - static::$_instance->set_timezone($timezone); |
|
| 686 | - // Espresso_model object |
|
| 687 | - return static::$_instance; |
|
| 688 | - } |
|
| 689 | - |
|
| 690 | - |
|
| 691 | - |
|
| 692 | - /** |
|
| 693 | - * resets the model and returns it |
|
| 694 | - * |
|
| 695 | - * @param null | string $timezone |
|
| 696 | - * @return EEM_Base|null (if the model was already instantiated, returns it, with |
|
| 697 | - * all its properties reset; if it wasn't instantiated, returns null) |
|
| 698 | - */ |
|
| 699 | - public static function reset($timezone = null) |
|
| 700 | - { |
|
| 701 | - if (static::$_instance instanceof EEM_Base) { |
|
| 702 | - //let's try to NOT swap out the current instance for a new one |
|
| 703 | - //because if someone has a reference to it, we can't remove their reference |
|
| 704 | - //so it's best to keep using the same reference, but change the original object |
|
| 705 | - //reset all its properties to their original values as defined in the class |
|
| 706 | - $r = new ReflectionClass(get_class(static::$_instance)); |
|
| 707 | - $static_properties = $r->getStaticProperties(); |
|
| 708 | - foreach ($r->getDefaultProperties() as $property => $value) { |
|
| 709 | - //don't set instance to null like it was originally, |
|
| 710 | - //but it's static anyways, and we're ignoring static properties (for now at least) |
|
| 711 | - if (! isset($static_properties[$property])) { |
|
| 712 | - static::$_instance->{$property} = $value; |
|
| 713 | - } |
|
| 714 | - } |
|
| 715 | - //and then directly call its constructor again, like we would if we |
|
| 716 | - //were creating a new one |
|
| 717 | - static::$_instance->__construct($timezone); |
|
| 718 | - return self::instance(); |
|
| 719 | - } |
|
| 720 | - return null; |
|
| 721 | - } |
|
| 722 | - |
|
| 723 | - |
|
| 724 | - |
|
| 725 | - /** |
|
| 726 | - * retrieve the status details from esp_status table as an array IF this model has the status table as a relation. |
|
| 727 | - * |
|
| 728 | - * @param boolean $translated return localized strings or JUST the array. |
|
| 729 | - * @return array |
|
| 730 | - * @throws \EE_Error |
|
| 731 | - */ |
|
| 732 | - public function status_array($translated = false) |
|
| 733 | - { |
|
| 734 | - if (! array_key_exists('Status', $this->_model_relations)) { |
|
| 735 | - return array(); |
|
| 736 | - } |
|
| 737 | - $model_name = $this->get_this_model_name(); |
|
| 738 | - $status_type = str_replace(' ', '_', strtolower(str_replace('_', ' ', $model_name))); |
|
| 739 | - $stati = EEM_Status::instance()->get_all(array(array('STS_type' => $status_type))); |
|
| 740 | - $status_array = array(); |
|
| 741 | - foreach ($stati as $status) { |
|
| 742 | - $status_array[$status->ID()] = $status->get('STS_code'); |
|
| 743 | - } |
|
| 744 | - return $translated |
|
| 745 | - ? EEM_Status::instance()->localized_status($status_array, false, 'sentence') |
|
| 746 | - : $status_array; |
|
| 747 | - } |
|
| 748 | - |
|
| 749 | - |
|
| 750 | - |
|
| 751 | - /** |
|
| 752 | - * Gets all the EE_Base_Class objects which match the $query_params, by querying the DB. |
|
| 753 | - * |
|
| 754 | - * @param array $query_params { |
|
| 755 | - * @var array $0 (where) array { |
|
| 756 | - * eg: array('QST_display_text'=>'Are you bob?','QST_admin_text'=>'Determine |
|
| 757 | - * if user is bob') becomes SQL >> "...WHERE QST_display_text = 'Are you |
|
| 758 | - * bob?' AND QST_admin_text = 'Determine if user is bob'...") To add WHERE |
|
| 759 | - * conditions based on related models (and even |
|
| 760 | - * models-related-to-related-models) prepend the model's name onto the field |
|
| 761 | - * name. Eg, |
|
| 762 | - * EEM_Event::instance()->get_all(array(array('Venue.VNU_ID'=>12))); becomes |
|
| 763 | - * SQL >> "SELECT * FROM wp_posts AS Event_CPT LEFT JOIN wp_esp_event_meta |
|
| 764 | - * AS Event_Meta ON Event_CPT.ID = Event_Meta.EVT_ID LEFT JOIN |
|
| 765 | - * wp_esp_event_venue AS Event_Venue ON Event_Venue.EVT_ID=Event_CPT.ID LEFT |
|
| 766 | - * JOIN wp_posts AS Venue_CPT ON Venue_CPT.ID=Event_Venue.VNU_ID LEFT JOIN |
|
| 767 | - * wp_esp_venue_meta AS Venue_Meta ON Venue_CPT.ID = Venue_Meta.VNU_ID WHERE |
|
| 768 | - * Venue_CPT.ID = 12 Notice that automatically took care of joining Events |
|
| 769 | - * to Venues (even when each of those models actually consisted of two |
|
| 770 | - * tables). Also, you may chain the model relations together. Eg instead of |
|
| 771 | - * just having |
|
| 772 | - * "Venue.VNU_ID", you could have |
|
| 773 | - * "Registration.Attendee.ATT_ID" as a field on a query for events (because |
|
| 774 | - * events are related to Registrations, which are related to Attendees). You |
|
| 775 | - * can take it even further with |
|
| 776 | - * "Registration.Transaction.Payment.PAY_amount" etc. To change the operator |
|
| 777 | - * (from the default of '='), change the value to an numerically-indexed |
|
| 778 | - * array, where the first item in the list is the operator. eg: array( |
|
| 779 | - * 'QST_display_text' => array('LIKE','%bob%'), 'QST_ID' => array('<',34), |
|
| 780 | - * 'QST_wp_user' => array('in',array(1,2,7,23))) becomes SQL >> "...WHERE |
|
| 781 | - * QST_display_text LIKE '%bob%' AND QST_ID < 34 AND QST_wp_user IN |
|
| 782 | - * (1,2,7,23)...". Valid operators so far: =, !=, <, <=, >, >=, LIKE, NOT |
|
| 783 | - * LIKE, IN (followed by numeric-indexed array), NOT IN (dido), BETWEEN |
|
| 784 | - * (followed by an array with exactly 2 date strings), IS NULL, and IS NOT |
|
| 785 | - * NULL Values can be a string, int, or float. They can also be arrays IFF |
|
| 786 | - * the operator is IN. Also, values can actually be field names. To indicate |
|
| 787 | - * the value is a field, simply provide a third array item (true) to the |
|
| 788 | - * operator-value array like so: eg: array( 'DTT_reg_limit' => array('>', |
|
| 789 | - * 'DTT_sold', TRUE) ) becomes SQL >> "...WHERE DTT_reg_limit > DTT_sold" |
|
| 790 | - * Note: you can also use related model field names like you would any other |
|
| 791 | - * field name. eg: |
|
| 792 | - * array('Datetime.DTT_reg_limit'=>array('=','Datetime.DTT_sold',TRUE) could |
|
| 793 | - * be used if you were querying EEM_Tickets (because Datetime is directly related to tickets) Also, by default all the where conditions are AND'd together. To override this, add an array key 'OR' (or 'AND') and the array to be OR'd together eg: array('OR'=>array('TXN_ID' => 23 , 'TXN_timestamp__>' => |
|
| 794 | - * 345678912)) becomes SQL >> "...WHERE TXN_ID = 23 OR TXN_timestamp = |
|
| 795 | - * 345678912...". Also, to negate an entire set of conditions, use 'NOT' as |
|
| 796 | - * an array key. eg: array('NOT'=>array('TXN_total' => |
|
| 797 | - * 50, 'TXN_paid'=>23) becomes SQL >> "...where ! (TXN_total =50 AND |
|
| 798 | - * TXN_paid =23) Note: the 'glue' used to join each condition will continue |
|
| 799 | - * to be what you last specified. IE, "AND"s by default, but if you had |
|
| 800 | - * previously specified to use ORs to join, ORs will continue to be used. |
|
| 801 | - * So, if you specify to use an "OR" to join conditions, it will continue to |
|
| 802 | - * "stick" until you specify an AND. eg |
|
| 803 | - * array('OR'=>array('NOT'=>array('TXN_total' => 50, |
|
| 804 | - * 'TXN_paid'=>23)),AND=>array('TXN_ID'=>1,'STS_ID'=>'TIN') becomes SQL >> |
|
| 805 | - * "...where ! (TXN_total =50 OR TXN_paid =23) AND TXN_ID=1 AND |
|
| 806 | - * STS_ID='TIN'" They can be nested indefinitely. eg: |
|
| 807 | - * array('OR'=>array('TXN_total' => 23, 'NOT'=> array( 'TXN_timestamp'=> 345678912, 'AND'=>array('TXN_paid' => 53, 'STS_ID' => 'TIN')))) becomes SQL >> "...WHERE TXN_total = 23 OR ! (TXN_timestamp = 345678912 OR (TXN_paid = 53 AND STS_ID = 'TIN'))..." GOTCHA: because this is an array, array keys must be unique, making it impossible to place two or more where conditions applying to the same field. eg: array('PAY_timestamp'=>array('>',$start_date),'PAY_timestamp'=>array('<',$end_date),'PAY_timestamp'=>array('!=',$special_date)), as PHP enforces that the array keys must be unique, thus removing the first two array entries with key 'PAY_timestamp'. becomes SQL >> "PAY_timestamp != 4234232", ignoring the first two PAY_timestamp conditions). To overcome this, you can add a '*' character to the end of the field's name, followed by anything. These will be removed when generating the SQL string, but allow for the array keys to be unique. eg: you could rewrite the previous query as: array('PAY_timestamp'=>array('>',$start_date),'PAY_timestamp*1st'=>array('<',$end_date),'PAY_timestamp*2nd'=>array('!=',$special_date)) which correctly becomes SQL >> |
|
| 808 | - * "PAY_timestamp > 123412341 AND PAY_timestamp < 2354235235234 AND |
|
| 809 | - * PAY_timestamp != 1241234123" This can be applied to condition operators |
|
| 810 | - * too, eg: |
|
| 811 | - * array('OR'=>array('REG_ID'=>3,'Transaction.TXN_ID'=>23),'OR*whatever'=>array('Attendee.ATT_fname'=>'bob','Attendee.ATT_lname'=>'wilson'))); |
|
| 812 | - * @var mixed $limit int|array adds a limit to the query just like the SQL limit clause, so |
|
| 813 | - * limits of "23", "25,50", and array(23,42) are all valid would become SQL |
|
| 814 | - * "...LIMIT 23", "...LIMIT 25,50", and "...LIMIT 23,42" respectively. |
|
| 815 | - * Remember when you provide two numbers for the limit, the 1st number is |
|
| 816 | - * the OFFSET, the 2nd is the LIMIT |
|
| 817 | - * @var array $on_join_limit allows the setting of a special select join with a internal limit so you |
|
| 818 | - * can do paging on one-to-many multi-table-joins. Send an array in the |
|
| 819 | - * following format array('on_join_limit' |
|
| 820 | - * => array( 'table_alias', array(1,2) ) ). |
|
| 821 | - * @var mixed $order_by name of a column to order by, or an array where keys are field names and |
|
| 822 | - * values are either 'ASC' or 'DESC'. |
|
| 823 | - * 'limit'=>array('STS_ID'=>'ASC','REG_date'=>'DESC'), which would becomes |
|
| 824 | - * SQL "...ORDER BY TXN_timestamp..." and "...ORDER BY STS_ID ASC, REG_date |
|
| 825 | - * DESC..." respectively. Like the |
|
| 826 | - * 'where' conditions, these fields can be on related models. Eg |
|
| 827 | - * 'order_by'=>array('Registration.Transaction.TXN_amount'=>'ASC') is |
|
| 828 | - * perfectly valid from any model related to 'Registration' (like Event, |
|
| 829 | - * Attendee, Price, Datetime, etc.) |
|
| 830 | - * @var string $order If 'order_by' is used and its value is a string (NOT an array), then |
|
| 831 | - * 'order' specifies whether to order the field specified in 'order_by' in |
|
| 832 | - * ascending or descending order. Acceptable values are 'ASC' or 'DESC'. If, |
|
| 833 | - * 'order_by' isn't used, but 'order' is, then it is assumed you want to |
|
| 834 | - * order by the primary key. Eg, |
|
| 835 | - * EEM_Event::instance()->get_all(array('order_by'=>'Datetime.DTT_EVT_start','order'=>'ASC'); |
|
| 836 | - * //(will join with the Datetime model's table(s) and order by its field |
|
| 837 | - * DTT_EVT_start) or |
|
| 838 | - * EEM_Registration::instance()->get_all(array('order'=>'ASC'));//will make |
|
| 839 | - * SQL "SELECT * FROM wp_esp_registration ORDER BY REG_ID ASC" |
|
| 840 | - * @var mixed $group_by name of field to order by, or an array of fields. Eg either |
|
| 841 | - * 'group_by'=>'VNU_ID', or |
|
| 842 | - * 'group_by'=>array('EVT_name','Registration.Transaction.TXN_total') Note: |
|
| 843 | - * if no |
|
| 844 | - * $group_by is specified, and a limit is set, automatically groups by the |
|
| 845 | - * model's primary key (or combined primary keys). This avoids some |
|
| 846 | - * weirdness that results when using limits, tons of joins, and no group by, |
|
| 847 | - * see https://events.codebasehq.com/projects/event-espresso/tickets/9389 |
|
| 848 | - * @var array $having exactly like WHERE parameters array, except these conditions apply to the |
|
| 849 | - * grouped results (whereas WHERE conditions apply to the pre-grouped |
|
| 850 | - * results) |
|
| 851 | - * @var array $force_join forces a join with the models named. Should be a numerically-indexed |
|
| 852 | - * array where values are models to be joined in the query.Eg |
|
| 853 | - * array('Attendee','Payment','Datetime'). You may join with transient |
|
| 854 | - * models using period, eg "Registration.Transaction.Payment". You will |
|
| 855 | - * probably only want to do this in hopes of increasing efficiency, as |
|
| 856 | - * related models which belongs to the current model |
|
| 857 | - * (ie, the current model has a foreign key to them, like how Registration |
|
| 858 | - * belongs to Attendee) can be cached in order to avoid future queries |
|
| 859 | - * @var string $default_where_conditions can be set to 'none', 'this_model_only', 'other_models_only', or 'all'. |
|
| 860 | - * set this to 'none' to disable all default where conditions. Eg, usually |
|
| 861 | - * soft-deleted objects are filtered-out if you want to include them, set |
|
| 862 | - * this query param to 'none'. If you want to ONLY disable THIS model's |
|
| 863 | - * default where conditions set it to 'other_models_only'. If you only want |
|
| 864 | - * this model's default where conditions added to the query, use |
|
| 865 | - * 'this_model_only'. If you want to use all default where conditions |
|
| 866 | - * (default), set to 'all'. |
|
| 867 | - * @var string $caps controls what capability requirements to apply to the query; ie, should |
|
| 868 | - * we just NOT apply any capabilities/permissions/restrictions and return |
|
| 869 | - * everything? Or should we only show the current user items they should be |
|
| 870 | - * able to view on the frontend, backend, edit, or delete? can be set to |
|
| 871 | - * 'none' (default), 'read_frontend', 'read_backend', 'edit' or 'delete' |
|
| 872 | - * } |
|
| 873 | - * @return EE_Base_Class[] *note that there is NO option to pass the output type. If you want results different |
|
| 874 | - * from EE_Base_Class[], use _get_all_wpdb_results()and make it public |
|
| 875 | - * again. Array keys are object IDs (if there is a primary key on the model. |
|
| 876 | - * if not, numerically indexed) Some full examples: get 10 transactions |
|
| 877 | - * which have Scottish attendees: EEM_Transaction::instance()->get_all( |
|
| 878 | - * array( array( |
|
| 879 | - * 'OR'=>array( |
|
| 880 | - * 'Registration.Attendee.ATT_fname'=>array('like','Mc%'), |
|
| 881 | - * 'Registration.Attendee.ATT_fname*other'=>array('like','Mac%') |
|
| 882 | - * ) |
|
| 883 | - * ), |
|
| 884 | - * 'limit'=>10, |
|
| 885 | - * 'group_by'=>'TXN_ID' |
|
| 886 | - * )); |
|
| 887 | - * get all the answers to the question titled "shirt size" for event with id |
|
| 888 | - * 12, ordered by their answer EEM_Answer::instance()->get_all(array( array( |
|
| 889 | - * 'Question.QST_display_text'=>'shirt size', |
|
| 890 | - * 'Registration.Event.EVT_ID'=>12 |
|
| 891 | - * ), |
|
| 892 | - * 'order_by'=>array('ANS_value'=>'ASC') |
|
| 893 | - * )); |
|
| 894 | - * @throws \EE_Error |
|
| 895 | - */ |
|
| 896 | - public function get_all($query_params = array()) |
|
| 897 | - { |
|
| 898 | - if (isset($query_params['limit']) |
|
| 899 | - && ! isset($query_params['group_by']) |
|
| 900 | - ) { |
|
| 901 | - $query_params['group_by'] = array_keys($this->get_combined_primary_key_fields()); |
|
| 902 | - } |
|
| 903 | - return $this->_create_objects($this->_get_all_wpdb_results($query_params, ARRAY_A, null)); |
|
| 904 | - } |
|
| 905 | - |
|
| 906 | - |
|
| 907 | - |
|
| 908 | - /** |
|
| 909 | - * Modifies the query parameters so we only get back model objects |
|
| 910 | - * that "belong" to the current user |
|
| 911 | - * |
|
| 912 | - * @param array $query_params @see EEM_Base::get_all() |
|
| 913 | - * @return array like EEM_Base::get_all |
|
| 914 | - */ |
|
| 915 | - public function alter_query_params_to_only_include_mine($query_params = array()) |
|
| 916 | - { |
|
| 917 | - $wp_user_field_name = $this->wp_user_field_name(); |
|
| 918 | - if ($wp_user_field_name) { |
|
| 919 | - $query_params[0][$wp_user_field_name] = get_current_user_id(); |
|
| 920 | - } |
|
| 921 | - return $query_params; |
|
| 922 | - } |
|
| 923 | - |
|
| 924 | - |
|
| 925 | - |
|
| 926 | - /** |
|
| 927 | - * Returns the name of the field's name that points to the WP_User table |
|
| 928 | - * on this model (or follows the _model_chain_to_wp_user and uses that model's |
|
| 929 | - * foreign key to the WP_User table) |
|
| 930 | - * |
|
| 931 | - * @return string|boolean string on success, boolean false when there is no |
|
| 932 | - * foreign key to the WP_User table |
|
| 933 | - */ |
|
| 934 | - public function wp_user_field_name() |
|
| 935 | - { |
|
| 936 | - try { |
|
| 937 | - if (! empty($this->_model_chain_to_wp_user)) { |
|
| 938 | - $models_to_follow_to_wp_users = explode('.', $this->_model_chain_to_wp_user); |
|
| 939 | - $last_model_name = end($models_to_follow_to_wp_users); |
|
| 940 | - $model_with_fk_to_wp_users = EE_Registry::instance()->load_model($last_model_name); |
|
| 941 | - $model_chain_to_wp_user = $this->_model_chain_to_wp_user . '.'; |
|
| 942 | - } else { |
|
| 943 | - $model_with_fk_to_wp_users = $this; |
|
| 944 | - $model_chain_to_wp_user = ''; |
|
| 945 | - } |
|
| 946 | - $wp_user_field = $model_with_fk_to_wp_users->get_foreign_key_to('WP_User'); |
|
| 947 | - return $model_chain_to_wp_user . $wp_user_field->get_name(); |
|
| 948 | - } catch (EE_Error $e) { |
|
| 949 | - return false; |
|
| 950 | - } |
|
| 951 | - } |
|
| 952 | - |
|
| 953 | - |
|
| 954 | - |
|
| 955 | - /** |
|
| 956 | - * Returns the _model_chain_to_wp_user string, which indicates which related model |
|
| 957 | - * (or transiently-related model) has a foreign key to the wp_users table; |
|
| 958 | - * useful for finding if model objects of this type are 'owned' by the current user. |
|
| 959 | - * This is an empty string when the foreign key is on this model and when it isn't, |
|
| 960 | - * but is only non-empty when this model's ownership is indicated by a RELATED model |
|
| 961 | - * (or transiently-related model) |
|
| 962 | - * |
|
| 963 | - * @return string |
|
| 964 | - */ |
|
| 965 | - public function model_chain_to_wp_user() |
|
| 966 | - { |
|
| 967 | - return $this->_model_chain_to_wp_user; |
|
| 968 | - } |
|
| 969 | - |
|
| 970 | - |
|
| 971 | - |
|
| 972 | - /** |
|
| 973 | - * Whether this model is 'owned' by a specific wordpress user (even indirectly, |
|
| 974 | - * like how registrations don't have a foreign key to wp_users, but the |
|
| 975 | - * events they are for are), or is unrelated to wp users. |
|
| 976 | - * generally available |
|
| 977 | - * |
|
| 978 | - * @return boolean |
|
| 979 | - */ |
|
| 980 | - public function is_owned() |
|
| 981 | - { |
|
| 982 | - if ($this->model_chain_to_wp_user()) { |
|
| 983 | - return true; |
|
| 984 | - } else { |
|
| 985 | - try { |
|
| 986 | - $this->get_foreign_key_to('WP_User'); |
|
| 987 | - return true; |
|
| 988 | - } catch (EE_Error $e) { |
|
| 989 | - return false; |
|
| 990 | - } |
|
| 991 | - } |
|
| 992 | - } |
|
| 993 | - |
|
| 994 | - |
|
| 995 | - |
|
| 996 | - /** |
|
| 997 | - * Used internally to get WPDB results, because other functions, besides get_all, may want to do some queries, but |
|
| 998 | - * may want to preserve the WPDB results (eg, update, which first queries to make sure we have all the tables on |
|
| 999 | - * the model) |
|
| 1000 | - * |
|
| 1001 | - * @param array $query_params like EEM_Base::get_all's $query_params |
|
| 1002 | - * @param string $output ARRAY_A, OBJECT_K, etc. Just like |
|
| 1003 | - * @param mixed $columns_to_select , What columns to select. By default, we select all columns specified by the |
|
| 1004 | - * fields on the model, and the models we joined to in the query. However, you can |
|
| 1005 | - * override this and set the select to "*", or a specific column name, like |
|
| 1006 | - * "ATT_ID", etc. If you would like to use these custom selections in WHERE, |
|
| 1007 | - * GROUP_BY, or HAVING clauses, you must instead provide an array. Array keys are |
|
| 1008 | - * the aliases used to refer to this selection, and values are to be |
|
| 1009 | - * numerically-indexed arrays, where 0 is the selection and 1 is the data type. |
|
| 1010 | - * Eg, array('count'=>array('COUNT(REG_ID)','%d')) |
|
| 1011 | - * @return array | stdClass[] like results of $wpdb->get_results($sql,OBJECT), (ie, output type is OBJECT) |
|
| 1012 | - * @throws \EE_Error |
|
| 1013 | - */ |
|
| 1014 | - protected function _get_all_wpdb_results($query_params = array(), $output = ARRAY_A, $columns_to_select = null) |
|
| 1015 | - { |
|
| 1016 | - // remember the custom selections, if any, and type cast as array |
|
| 1017 | - // (unless $columns_to_select is an object, then just set as an empty array) |
|
| 1018 | - // Note: (array) 'some string' === array( 'some string' ) |
|
| 1019 | - $this->_custom_selections = ! is_object($columns_to_select) ? (array)$columns_to_select : array(); |
|
| 1020 | - $model_query_info = $this->_create_model_query_info_carrier($query_params); |
|
| 1021 | - $select_expressions = $columns_to_select !== null |
|
| 1022 | - ? $this->_construct_select_from_input($columns_to_select) |
|
| 1023 | - : $this->_construct_default_select_sql($model_query_info); |
|
| 1024 | - $SQL = "SELECT $select_expressions " . $this->_construct_2nd_half_of_select_query($model_query_info); |
|
| 1025 | - return $this->_do_wpdb_query('get_results', array($SQL, $output)); |
|
| 1026 | - } |
|
| 1027 | - |
|
| 1028 | - |
|
| 1029 | - |
|
| 1030 | - /** |
|
| 1031 | - * Gets an array of rows from the database just like $wpdb->get_results would, |
|
| 1032 | - * but you can use the $query_params like on EEM_Base::get_all() to more easily |
|
| 1033 | - * take care of joins, field preparation etc. |
|
| 1034 | - * |
|
| 1035 | - * @param array $query_params like EEM_Base::get_all's $query_params |
|
| 1036 | - * @param string $output ARRAY_A, OBJECT_K, etc. Just like |
|
| 1037 | - * @param mixed $columns_to_select , What columns to select. By default, we select all columns specified by the |
|
| 1038 | - * fields on the model, and the models we joined to in the query. However, you can |
|
| 1039 | - * override this and set the select to "*", or a specific column name, like |
|
| 1040 | - * "ATT_ID", etc. If you would like to use these custom selections in WHERE, |
|
| 1041 | - * GROUP_BY, or HAVING clauses, you must instead provide an array. Array keys are |
|
| 1042 | - * the aliases used to refer to this selection, and values are to be |
|
| 1043 | - * numerically-indexed arrays, where 0 is the selection and 1 is the data type. |
|
| 1044 | - * Eg, array('count'=>array('COUNT(REG_ID)','%d')) |
|
| 1045 | - * @return array|stdClass[] like results of $wpdb->get_results($sql,OBJECT), (ie, output type is OBJECT) |
|
| 1046 | - * @throws \EE_Error |
|
| 1047 | - */ |
|
| 1048 | - public function get_all_wpdb_results($query_params = array(), $output = ARRAY_A, $columns_to_select = null) |
|
| 1049 | - { |
|
| 1050 | - return $this->_get_all_wpdb_results($query_params, $output, $columns_to_select); |
|
| 1051 | - } |
|
| 1052 | - |
|
| 1053 | - |
|
| 1054 | - |
|
| 1055 | - /** |
|
| 1056 | - * For creating a custom select statement |
|
| 1057 | - * |
|
| 1058 | - * @param mixed $columns_to_select either a string to be inserted directly as the select statement, |
|
| 1059 | - * or an array where keys are aliases, and values are arrays where 0=>the selection |
|
| 1060 | - * SQL, and 1=>is the datatype |
|
| 1061 | - * @throws EE_Error |
|
| 1062 | - * @return string |
|
| 1063 | - */ |
|
| 1064 | - private function _construct_select_from_input($columns_to_select) |
|
| 1065 | - { |
|
| 1066 | - if (is_array($columns_to_select)) { |
|
| 1067 | - $select_sql_array = array(); |
|
| 1068 | - foreach ($columns_to_select as $alias => $selection_and_datatype) { |
|
| 1069 | - if (! is_array($selection_and_datatype) || ! isset($selection_and_datatype[1])) { |
|
| 1070 | - throw new EE_Error( |
|
| 1071 | - sprintf( |
|
| 1072 | - __( |
|
| 1073 | - "Custom selection %s (alias %s) needs to be an array like array('COUNT(REG_ID)','%%d')", |
|
| 1074 | - "event_espresso" |
|
| 1075 | - ), |
|
| 1076 | - $selection_and_datatype, |
|
| 1077 | - $alias |
|
| 1078 | - ) |
|
| 1079 | - ); |
|
| 1080 | - } |
|
| 1081 | - if (! in_array($selection_and_datatype[1], $this->_valid_wpdb_data_types)) { |
|
| 1082 | - throw new EE_Error( |
|
| 1083 | - sprintf( |
|
| 1084 | - __( |
|
| 1085 | - "Datatype %s (for selection '%s' and alias '%s') is not a valid wpdb datatype (eg %%s)", |
|
| 1086 | - "event_espresso" |
|
| 1087 | - ), |
|
| 1088 | - $selection_and_datatype[1], |
|
| 1089 | - $selection_and_datatype[0], |
|
| 1090 | - $alias, |
|
| 1091 | - implode(",", $this->_valid_wpdb_data_types) |
|
| 1092 | - ) |
|
| 1093 | - ); |
|
| 1094 | - } |
|
| 1095 | - $select_sql_array[] = "{$selection_and_datatype[0]} AS $alias"; |
|
| 1096 | - } |
|
| 1097 | - $columns_to_select_string = implode(", ", $select_sql_array); |
|
| 1098 | - } else { |
|
| 1099 | - $columns_to_select_string = $columns_to_select; |
|
| 1100 | - } |
|
| 1101 | - return $columns_to_select_string; |
|
| 1102 | - } |
|
| 1103 | - |
|
| 1104 | - |
|
| 1105 | - |
|
| 1106 | - /** |
|
| 1107 | - * Convenient wrapper for getting the primary key field's name. Eg, on Registration, this would be 'REG_ID' |
|
| 1108 | - * |
|
| 1109 | - * @return string |
|
| 1110 | - * @throws \EE_Error |
|
| 1111 | - */ |
|
| 1112 | - public function primary_key_name() |
|
| 1113 | - { |
|
| 1114 | - return $this->get_primary_key_field()->get_name(); |
|
| 1115 | - } |
|
| 1116 | - |
|
| 1117 | - |
|
| 1118 | - |
|
| 1119 | - /** |
|
| 1120 | - * Gets a single item for this model from the DB, given only its ID (or null if none is found). |
|
| 1121 | - * If there is no primary key on this model, $id is treated as primary key string |
|
| 1122 | - * |
|
| 1123 | - * @param mixed $id int or string, depending on the type of the model's primary key |
|
| 1124 | - * @return EE_Base_Class |
|
| 1125 | - */ |
|
| 1126 | - public function get_one_by_ID($id) |
|
| 1127 | - { |
|
| 1128 | - if ($this->get_from_entity_map($id)) { |
|
| 1129 | - return $this->get_from_entity_map($id); |
|
| 1130 | - } |
|
| 1131 | - return $this->get_one( |
|
| 1132 | - $this->alter_query_params_to_restrict_by_ID( |
|
| 1133 | - $id, |
|
| 1134 | - array('default_where_conditions' => EEM_Base::default_where_conditions_minimum_all) |
|
| 1135 | - ) |
|
| 1136 | - ); |
|
| 1137 | - } |
|
| 1138 | - |
|
| 1139 | - |
|
| 1140 | - |
|
| 1141 | - /** |
|
| 1142 | - * Alters query parameters to only get items with this ID are returned. |
|
| 1143 | - * Takes into account that the ID might be a string produced by EEM_Base::get_index_primary_key_string(), |
|
| 1144 | - * or could just be a simple primary key ID |
|
| 1145 | - * |
|
| 1146 | - * @param int $id |
|
| 1147 | - * @param array $query_params |
|
| 1148 | - * @return array of normal query params, @see EEM_Base::get_all |
|
| 1149 | - * @throws \EE_Error |
|
| 1150 | - */ |
|
| 1151 | - public function alter_query_params_to_restrict_by_ID($id, $query_params = array()) |
|
| 1152 | - { |
|
| 1153 | - if (! isset($query_params[0])) { |
|
| 1154 | - $query_params[0] = array(); |
|
| 1155 | - } |
|
| 1156 | - $conditions_from_id = $this->parse_index_primary_key_string($id); |
|
| 1157 | - if ($conditions_from_id === null) { |
|
| 1158 | - $query_params[0][$this->primary_key_name()] = $id; |
|
| 1159 | - } else { |
|
| 1160 | - //no primary key, so the $id must be from the get_index_primary_key_string() |
|
| 1161 | - $query_params[0] = array_replace_recursive($query_params[0], $this->parse_index_primary_key_string($id)); |
|
| 1162 | - } |
|
| 1163 | - return $query_params; |
|
| 1164 | - } |
|
| 1165 | - |
|
| 1166 | - |
|
| 1167 | - |
|
| 1168 | - /** |
|
| 1169 | - * Gets a single item for this model from the DB, given the $query_params. Only returns a single class, not an |
|
| 1170 | - * array. If no item is found, null is returned. |
|
| 1171 | - * |
|
| 1172 | - * @param array $query_params like EEM_Base's $query_params variable. |
|
| 1173 | - * @return EE_Base_Class|EE_Soft_Delete_Base_Class|NULL |
|
| 1174 | - * @throws \EE_Error |
|
| 1175 | - */ |
|
| 1176 | - public function get_one($query_params = array()) |
|
| 1177 | - { |
|
| 1178 | - if (! is_array($query_params)) { |
|
| 1179 | - EE_Error::doing_it_wrong('EEM_Base::get_one', |
|
| 1180 | - sprintf(__('$query_params should be an array, you passed a variable of type %s', 'event_espresso'), |
|
| 1181 | - gettype($query_params)), '4.6.0'); |
|
| 1182 | - $query_params = array(); |
|
| 1183 | - } |
|
| 1184 | - $query_params['limit'] = 1; |
|
| 1185 | - $items = $this->get_all($query_params); |
|
| 1186 | - if (empty($items)) { |
|
| 1187 | - return null; |
|
| 1188 | - } else { |
|
| 1189 | - return array_shift($items); |
|
| 1190 | - } |
|
| 1191 | - } |
|
| 1192 | - |
|
| 1193 | - |
|
| 1194 | - |
|
| 1195 | - /** |
|
| 1196 | - * Returns the next x number of items in sequence from the given value as |
|
| 1197 | - * found in the database matching the given query conditions. |
|
| 1198 | - * |
|
| 1199 | - * @param mixed $current_field_value Value used for the reference point. |
|
| 1200 | - * @param null $field_to_order_by What field is used for the |
|
| 1201 | - * reference point. |
|
| 1202 | - * @param int $limit How many to return. |
|
| 1203 | - * @param array $query_params Extra conditions on the query. |
|
| 1204 | - * @param null $columns_to_select If left null, then an array of |
|
| 1205 | - * EE_Base_Class objects is returned, |
|
| 1206 | - * otherwise you can indicate just the |
|
| 1207 | - * columns you want returned. |
|
| 1208 | - * @return EE_Base_Class[]|array |
|
| 1209 | - * @throws \EE_Error |
|
| 1210 | - */ |
|
| 1211 | - public function next_x( |
|
| 1212 | - $current_field_value, |
|
| 1213 | - $field_to_order_by = null, |
|
| 1214 | - $limit = 1, |
|
| 1215 | - $query_params = array(), |
|
| 1216 | - $columns_to_select = null |
|
| 1217 | - ) { |
|
| 1218 | - return $this->_get_consecutive($current_field_value, '>', $field_to_order_by, $limit, $query_params, |
|
| 1219 | - $columns_to_select); |
|
| 1220 | - } |
|
| 1221 | - |
|
| 1222 | - |
|
| 1223 | - |
|
| 1224 | - /** |
|
| 1225 | - * Returns the previous x number of items in sequence from the given value |
|
| 1226 | - * as found in the database matching the given query conditions. |
|
| 1227 | - * |
|
| 1228 | - * @param mixed $current_field_value Value used for the reference point. |
|
| 1229 | - * @param null $field_to_order_by What field is used for the |
|
| 1230 | - * reference point. |
|
| 1231 | - * @param int $limit How many to return. |
|
| 1232 | - * @param array $query_params Extra conditions on the query. |
|
| 1233 | - * @param null $columns_to_select If left null, then an array of |
|
| 1234 | - * EE_Base_Class objects is returned, |
|
| 1235 | - * otherwise you can indicate just the |
|
| 1236 | - * columns you want returned. |
|
| 1237 | - * @return EE_Base_Class[]|array |
|
| 1238 | - * @throws \EE_Error |
|
| 1239 | - */ |
|
| 1240 | - public function previous_x( |
|
| 1241 | - $current_field_value, |
|
| 1242 | - $field_to_order_by = null, |
|
| 1243 | - $limit = 1, |
|
| 1244 | - $query_params = array(), |
|
| 1245 | - $columns_to_select = null |
|
| 1246 | - ) { |
|
| 1247 | - return $this->_get_consecutive($current_field_value, '<', $field_to_order_by, $limit, $query_params, |
|
| 1248 | - $columns_to_select); |
|
| 1249 | - } |
|
| 1250 | - |
|
| 1251 | - |
|
| 1252 | - |
|
| 1253 | - /** |
|
| 1254 | - * Returns the next item in sequence from the given value as found in the |
|
| 1255 | - * database matching the given query conditions. |
|
| 1256 | - * |
|
| 1257 | - * @param mixed $current_field_value Value used for the reference point. |
|
| 1258 | - * @param null $field_to_order_by What field is used for the |
|
| 1259 | - * reference point. |
|
| 1260 | - * @param array $query_params Extra conditions on the query. |
|
| 1261 | - * @param null $columns_to_select If left null, then an EE_Base_Class |
|
| 1262 | - * object is returned, otherwise you |
|
| 1263 | - * can indicate just the columns you |
|
| 1264 | - * want and a single array indexed by |
|
| 1265 | - * the columns will be returned. |
|
| 1266 | - * @return EE_Base_Class|null|array() |
|
| 1267 | - * @throws \EE_Error |
|
| 1268 | - */ |
|
| 1269 | - public function next( |
|
| 1270 | - $current_field_value, |
|
| 1271 | - $field_to_order_by = null, |
|
| 1272 | - $query_params = array(), |
|
| 1273 | - $columns_to_select = null |
|
| 1274 | - ) { |
|
| 1275 | - $results = $this->_get_consecutive($current_field_value, '>', $field_to_order_by, 1, $query_params, |
|
| 1276 | - $columns_to_select); |
|
| 1277 | - return empty($results) ? null : reset($results); |
|
| 1278 | - } |
|
| 1279 | - |
|
| 1280 | - |
|
| 1281 | - |
|
| 1282 | - /** |
|
| 1283 | - * Returns the previous item in sequence from the given value as found in |
|
| 1284 | - * the database matching the given query conditions. |
|
| 1285 | - * |
|
| 1286 | - * @param mixed $current_field_value Value used for the reference point. |
|
| 1287 | - * @param null $field_to_order_by What field is used for the |
|
| 1288 | - * reference point. |
|
| 1289 | - * @param array $query_params Extra conditions on the query. |
|
| 1290 | - * @param null $columns_to_select If left null, then an EE_Base_Class |
|
| 1291 | - * object is returned, otherwise you |
|
| 1292 | - * can indicate just the columns you |
|
| 1293 | - * want and a single array indexed by |
|
| 1294 | - * the columns will be returned. |
|
| 1295 | - * @return EE_Base_Class|null|array() |
|
| 1296 | - * @throws EE_Error |
|
| 1297 | - */ |
|
| 1298 | - public function previous( |
|
| 1299 | - $current_field_value, |
|
| 1300 | - $field_to_order_by = null, |
|
| 1301 | - $query_params = array(), |
|
| 1302 | - $columns_to_select = null |
|
| 1303 | - ) { |
|
| 1304 | - $results = $this->_get_consecutive($current_field_value, '<', $field_to_order_by, 1, $query_params, |
|
| 1305 | - $columns_to_select); |
|
| 1306 | - return empty($results) ? null : reset($results); |
|
| 1307 | - } |
|
| 1308 | - |
|
| 1309 | - |
|
| 1310 | - |
|
| 1311 | - /** |
|
| 1312 | - * Returns the a consecutive number of items in sequence from the given |
|
| 1313 | - * value as found in the database matching the given query conditions. |
|
| 1314 | - * |
|
| 1315 | - * @param mixed $current_field_value Value used for the reference point. |
|
| 1316 | - * @param string $operand What operand is used for the sequence. |
|
| 1317 | - * @param string $field_to_order_by What field is used for the reference point. |
|
| 1318 | - * @param int $limit How many to return. |
|
| 1319 | - * @param array $query_params Extra conditions on the query. |
|
| 1320 | - * @param null $columns_to_select If left null, then an array of EE_Base_Class objects is returned, |
|
| 1321 | - * otherwise you can indicate just the columns you want returned. |
|
| 1322 | - * @return EE_Base_Class[]|array |
|
| 1323 | - * @throws EE_Error |
|
| 1324 | - */ |
|
| 1325 | - protected function _get_consecutive( |
|
| 1326 | - $current_field_value, |
|
| 1327 | - $operand = '>', |
|
| 1328 | - $field_to_order_by = null, |
|
| 1329 | - $limit = 1, |
|
| 1330 | - $query_params = array(), |
|
| 1331 | - $columns_to_select = null |
|
| 1332 | - ) { |
|
| 1333 | - //if $field_to_order_by is empty then let's assume we're ordering by the primary key. |
|
| 1334 | - if (empty($field_to_order_by)) { |
|
| 1335 | - if ($this->has_primary_key_field()) { |
|
| 1336 | - $field_to_order_by = $this->get_primary_key_field()->get_name(); |
|
| 1337 | - } else { |
|
| 1338 | - if (WP_DEBUG) { |
|
| 1339 | - throw new EE_Error(__('EEM_Base::_get_consecutive() has been called with no $field_to_order_by argument and there is no primary key on the field. Please provide the field you would like to use as the base for retrieving the next item(s).', |
|
| 1340 | - 'event_espresso')); |
|
| 1341 | - } |
|
| 1342 | - EE_Error::add_error(__('There was an error with the query.', 'event_espresso')); |
|
| 1343 | - return array(); |
|
| 1344 | - } |
|
| 1345 | - } |
|
| 1346 | - if (! is_array($query_params)) { |
|
| 1347 | - EE_Error::doing_it_wrong('EEM_Base::_get_consecutive', |
|
| 1348 | - sprintf(__('$query_params should be an array, you passed a variable of type %s', 'event_espresso'), |
|
| 1349 | - gettype($query_params)), '4.6.0'); |
|
| 1350 | - $query_params = array(); |
|
| 1351 | - } |
|
| 1352 | - //let's add the where query param for consecutive look up. |
|
| 1353 | - $query_params[0][$field_to_order_by] = array($operand, $current_field_value); |
|
| 1354 | - $query_params['limit'] = $limit; |
|
| 1355 | - //set direction |
|
| 1356 | - $incoming_orderby = isset($query_params['order_by']) ? (array)$query_params['order_by'] : array(); |
|
| 1357 | - $query_params['order_by'] = $operand === '>' |
|
| 1358 | - ? array($field_to_order_by => 'ASC') + $incoming_orderby |
|
| 1359 | - : array($field_to_order_by => 'DESC') + $incoming_orderby; |
|
| 1360 | - //if $columns_to_select is empty then that means we're returning EE_Base_Class objects |
|
| 1361 | - if (empty($columns_to_select)) { |
|
| 1362 | - return $this->get_all($query_params); |
|
| 1363 | - } else { |
|
| 1364 | - //getting just the fields |
|
| 1365 | - return $this->_get_all_wpdb_results($query_params, ARRAY_A, $columns_to_select); |
|
| 1366 | - } |
|
| 1367 | - } |
|
| 1368 | - |
|
| 1369 | - |
|
| 1370 | - |
|
| 1371 | - /** |
|
| 1372 | - * This sets the _timezone property after model object has been instantiated. |
|
| 1373 | - * |
|
| 1374 | - * @param null | string $timezone valid PHP DateTimeZone timezone string |
|
| 1375 | - */ |
|
| 1376 | - public function set_timezone($timezone) |
|
| 1377 | - { |
|
| 1378 | - if ($timezone !== null) { |
|
| 1379 | - $this->_timezone = $timezone; |
|
| 1380 | - } |
|
| 1381 | - //note we need to loop through relations and set the timezone on those objects as well. |
|
| 1382 | - foreach ($this->_model_relations as $relation) { |
|
| 1383 | - $relation->set_timezone($timezone); |
|
| 1384 | - } |
|
| 1385 | - //and finally we do the same for any datetime fields |
|
| 1386 | - foreach ($this->_fields as $field) { |
|
| 1387 | - if ($field instanceof EE_Datetime_Field) { |
|
| 1388 | - $field->set_timezone($timezone); |
|
| 1389 | - } |
|
| 1390 | - } |
|
| 1391 | - } |
|
| 1392 | - |
|
| 1393 | - |
|
| 1394 | - |
|
| 1395 | - /** |
|
| 1396 | - * This just returns whatever is set for the current timezone. |
|
| 1397 | - * |
|
| 1398 | - * @access public |
|
| 1399 | - * @return string |
|
| 1400 | - */ |
|
| 1401 | - public function get_timezone() |
|
| 1402 | - { |
|
| 1403 | - //first validate if timezone is set. If not, then let's set it be whatever is set on the model fields. |
|
| 1404 | - if (empty($this->_timezone)) { |
|
| 1405 | - foreach ($this->_fields as $field) { |
|
| 1406 | - if ($field instanceof EE_Datetime_Field) { |
|
| 1407 | - $this->set_timezone($field->get_timezone()); |
|
| 1408 | - break; |
|
| 1409 | - } |
|
| 1410 | - } |
|
| 1411 | - } |
|
| 1412 | - //if timezone STILL empty then return the default timezone for the site. |
|
| 1413 | - if (empty($this->_timezone)) { |
|
| 1414 | - $this->set_timezone(EEH_DTT_Helper::get_timezone()); |
|
| 1415 | - } |
|
| 1416 | - return $this->_timezone; |
|
| 1417 | - } |
|
| 1418 | - |
|
| 1419 | - |
|
| 1420 | - |
|
| 1421 | - /** |
|
| 1422 | - * This returns the date formats set for the given field name and also ensures that |
|
| 1423 | - * $this->_timezone property is set correctly. |
|
| 1424 | - * |
|
| 1425 | - * @since 4.6.x |
|
| 1426 | - * @param string $field_name The name of the field the formats are being retrieved for. |
|
| 1427 | - * @param bool $pretty Whether to return the pretty formats (true) or not (false). |
|
| 1428 | - * @throws EE_Error If the given field_name is not of the EE_Datetime_Field type. |
|
| 1429 | - * @return array formats in an array with the date format first, and the time format last. |
|
| 1430 | - */ |
|
| 1431 | - public function get_formats_for($field_name, $pretty = false) |
|
| 1432 | - { |
|
| 1433 | - $field_settings = $this->field_settings_for($field_name); |
|
| 1434 | - //if not a valid EE_Datetime_Field then throw error |
|
| 1435 | - if (! $field_settings instanceof EE_Datetime_Field) { |
|
| 1436 | - throw new EE_Error(sprintf(__('The field sent into EEM_Base::get_formats_for (%s) is not registered as a EE_Datetime_Field. Please check the spelling and make sure you are submitting the right field name to retrieve date_formats for.', |
|
| 1437 | - 'event_espresso'), $field_name)); |
|
| 1438 | - } |
|
| 1439 | - //while we are here, let's make sure the timezone internally in EEM_Base matches what is stored on |
|
| 1440 | - //the field. |
|
| 1441 | - $this->_timezone = $field_settings->get_timezone(); |
|
| 1442 | - return array($field_settings->get_date_format($pretty), $field_settings->get_time_format($pretty)); |
|
| 1443 | - } |
|
| 1444 | - |
|
| 1445 | - |
|
| 1446 | - |
|
| 1447 | - /** |
|
| 1448 | - * This returns the current time in a format setup for a query on this model. |
|
| 1449 | - * Usage of this method makes it easier to setup queries against EE_Datetime_Field columns because |
|
| 1450 | - * it will return: |
|
| 1451 | - * - a formatted string in the timezone and format currently set on the EE_Datetime_Field for the given field for |
|
| 1452 | - * NOW |
|
| 1453 | - * - or a unix timestamp (equivalent to time()) |
|
| 1454 | - * |
|
| 1455 | - * @since 4.6.x |
|
| 1456 | - * @param string $field_name The field the current time is needed for. |
|
| 1457 | - * @param bool $timestamp True means to return a unix timestamp. Otherwise a |
|
| 1458 | - * formatted string matching the set format for the field in the set timezone will |
|
| 1459 | - * be returned. |
|
| 1460 | - * @param string $what Whether to return the string in just the time format, the date format, or both. |
|
| 1461 | - * @throws EE_Error If the given field_name is not of the EE_Datetime_Field type. |
|
| 1462 | - * @return int|string If the given field_name is not of the EE_Datetime_Field type, then an EE_Error |
|
| 1463 | - * exception is triggered. |
|
| 1464 | - */ |
|
| 1465 | - public function current_time_for_query($field_name, $timestamp = false, $what = 'both') |
|
| 1466 | - { |
|
| 1467 | - $formats = $this->get_formats_for($field_name); |
|
| 1468 | - $DateTime = new DateTime("now", new DateTimeZone($this->_timezone)); |
|
| 1469 | - if ($timestamp) { |
|
| 1470 | - return $DateTime->format('U'); |
|
| 1471 | - } |
|
| 1472 | - //not returning timestamp, so return formatted string in timezone. |
|
| 1473 | - switch ($what) { |
|
| 1474 | - case 'time' : |
|
| 1475 | - return $DateTime->format($formats[1]); |
|
| 1476 | - break; |
|
| 1477 | - case 'date' : |
|
| 1478 | - return $DateTime->format($formats[0]); |
|
| 1479 | - break; |
|
| 1480 | - default : |
|
| 1481 | - return $DateTime->format(implode(' ', $formats)); |
|
| 1482 | - break; |
|
| 1483 | - } |
|
| 1484 | - } |
|
| 1485 | - |
|
| 1486 | - |
|
| 1487 | - |
|
| 1488 | - /** |
|
| 1489 | - * This receives a time string for a given field and ensures that it is setup to match what the internal settings |
|
| 1490 | - * for the model are. Returns a DateTime object. |
|
| 1491 | - * Note: a gotcha for when you send in unix timestamp. Remember a unix timestamp is already timezone agnostic, |
|
| 1492 | - * (functionally the equivalent of UTC+0). So when you send it in, whatever timezone string you include is |
|
| 1493 | - * ignored. |
|
| 1494 | - * |
|
| 1495 | - * @param string $field_name The field being setup. |
|
| 1496 | - * @param string $timestring The date time string being used. |
|
| 1497 | - * @param string $incoming_format The format for the time string. |
|
| 1498 | - * @param string $timezone By default, it is assumed the incoming time string is in timezone for |
|
| 1499 | - * the blog. If this is not the case, then it can be specified here. If incoming |
|
| 1500 | - * format is |
|
| 1501 | - * 'U', this is ignored. |
|
| 1502 | - * @return DateTime |
|
| 1503 | - * @throws \EE_Error |
|
| 1504 | - */ |
|
| 1505 | - public function convert_datetime_for_query($field_name, $timestring, $incoming_format, $timezone = '') |
|
| 1506 | - { |
|
| 1507 | - //just using this to ensure the timezone is set correctly internally |
|
| 1508 | - $this->get_formats_for($field_name); |
|
| 1509 | - //load EEH_DTT_Helper |
|
| 1510 | - $set_timezone = empty($timezone) ? EEH_DTT_Helper::get_timezone() : $timezone; |
|
| 1511 | - $incomingDateTime = date_create_from_format($incoming_format, $timestring, new DateTimeZone($set_timezone)); |
|
| 1512 | - return \EventEspresso\core\domain\entities\DbSafeDateTime::createFromDateTime( $incomingDateTime->setTimezone(new DateTimeZone($this->_timezone)) ); |
|
| 1513 | - } |
|
| 1514 | - |
|
| 1515 | - |
|
| 1516 | - |
|
| 1517 | - /** |
|
| 1518 | - * Gets all the tables comprising this model. Array keys are the table aliases, and values are EE_Table objects |
|
| 1519 | - * |
|
| 1520 | - * @return EE_Table_Base[] |
|
| 1521 | - */ |
|
| 1522 | - public function get_tables() |
|
| 1523 | - { |
|
| 1524 | - return $this->_tables; |
|
| 1525 | - } |
|
| 1526 | - |
|
| 1527 | - |
|
| 1528 | - |
|
| 1529 | - /** |
|
| 1530 | - * Updates all the database entries (in each table for this model) according to $fields_n_values and optionally |
|
| 1531 | - * also updates all the model objects, where the criteria expressed in $query_params are met.. |
|
| 1532 | - * Also note: if this model has multiple tables, this update verifies all the secondary tables have an entry for |
|
| 1533 | - * each row (in the primary table) we're trying to update; if not, it inserts an entry in the secondary table. Eg: |
|
| 1534 | - * if our model has 2 tables: wp_posts (primary), and wp_esp_event (secondary). Let's say we are trying to update a |
|
| 1535 | - * model object with EVT_ID = 1 |
|
| 1536 | - * (which means where wp_posts has ID = 1, because wp_posts.ID is the primary key's column), which exists, but |
|
| 1537 | - * there is no entry in wp_esp_event for this entry in wp_posts. So, this update script will insert a row into |
|
| 1538 | - * wp_esp_event, using any available parameters from $fields_n_values (eg, if "EVT_limit" => 40 is in |
|
| 1539 | - * $fields_n_values, the new entry in wp_esp_event will set EVT_limit = 40, and use default for other columns which |
|
| 1540 | - * are not specified) |
|
| 1541 | - * |
|
| 1542 | - * @param array $fields_n_values keys are model fields (exactly like keys in EEM_Base::_fields, NOT db |
|
| 1543 | - * columns!), values are strings, ints, floats, and maybe arrays if they |
|
| 1544 | - * are to be serialized. Basically, the values are what you'd expect to be |
|
| 1545 | - * values on the model, NOT necessarily what's in the DB. For example, if |
|
| 1546 | - * we wanted to update only the TXN_details on any Transactions where its |
|
| 1547 | - * ID=34, we'd use this method as follows: |
|
| 1548 | - * EEM_Transaction::instance()->update( |
|
| 1549 | - * array('TXN_details'=>array('detail1'=>'monkey','detail2'=>'banana'), |
|
| 1550 | - * array(array('TXN_ID'=>34))); |
|
| 1551 | - * @param array $query_params very much like EEM_Base::get_all's $query_params |
|
| 1552 | - * in client code into what's expected to be stored on each field. Eg, |
|
| 1553 | - * consider updating Question's QST_admin_label field is of type |
|
| 1554 | - * Simple_HTML. If you use this function to update that field to $new_value |
|
| 1555 | - * = (note replace 8's with appropriate opening and closing tags in the |
|
| 1556 | - * following example)"8script8alert('I hack all');8/script88b8boom |
|
| 1557 | - * baby8/b8", then if you set $values_already_prepared_by_model_object to |
|
| 1558 | - * TRUE, it is assumed that you've already called |
|
| 1559 | - * EE_Simple_HTML_Field->prepare_for_set($new_value), which removes the |
|
| 1560 | - * malicious javascript. However, if |
|
| 1561 | - * $values_already_prepared_by_model_object is left as FALSE, then |
|
| 1562 | - * EE_Simple_HTML_Field->prepare_for_set($new_value) will be called on it, |
|
| 1563 | - * and every other field, before insertion. We provide this parameter |
|
| 1564 | - * because model objects perform their prepare_for_set function on all |
|
| 1565 | - * their values, and so don't need to be called again (and in many cases, |
|
| 1566 | - * shouldn't be called again. Eg: if we escape HTML characters in the |
|
| 1567 | - * prepare_for_set method...) |
|
| 1568 | - * @param boolean $keep_model_objs_in_sync if TRUE, makes sure we ALSO update model objects |
|
| 1569 | - * in this model's entity map according to $fields_n_values that match |
|
| 1570 | - * $query_params. This obviously has some overhead, so you can disable it |
|
| 1571 | - * by setting this to FALSE, but be aware that model objects being used |
|
| 1572 | - * could get out-of-sync with the database |
|
| 1573 | - * @return int how many rows got updated or FALSE if something went wrong with the query (wp returns FALSE or num |
|
| 1574 | - * rows affected which *could* include 0 which DOES NOT mean the query was |
|
| 1575 | - * bad) |
|
| 1576 | - * @throws \EE_Error |
|
| 1577 | - */ |
|
| 1578 | - public function update($fields_n_values, $query_params, $keep_model_objs_in_sync = true) |
|
| 1579 | - { |
|
| 1580 | - if (! is_array($query_params)) { |
|
| 1581 | - EE_Error::doing_it_wrong('EEM_Base::update', |
|
| 1582 | - sprintf(__('$query_params should be an array, you passed a variable of type %s', 'event_espresso'), |
|
| 1583 | - gettype($query_params)), '4.6.0'); |
|
| 1584 | - $query_params = array(); |
|
| 1585 | - } |
|
| 1586 | - /** |
|
| 1587 | - * Action called before a model update call has been made. |
|
| 1588 | - * |
|
| 1589 | - * @param EEM_Base $model |
|
| 1590 | - * @param array $fields_n_values the updated fields and their new values |
|
| 1591 | - * @param array $query_params @see EEM_Base::get_all() |
|
| 1592 | - */ |
|
| 1593 | - do_action('AHEE__EEM_Base__update__begin', $this, $fields_n_values, $query_params); |
|
| 1594 | - /** |
|
| 1595 | - * Filters the fields about to be updated given the query parameters. You can provide the |
|
| 1596 | - * $query_params to $this->get_all() to find exactly which records will be updated |
|
| 1597 | - * |
|
| 1598 | - * @param array $fields_n_values fields and their new values |
|
| 1599 | - * @param EEM_Base $model the model being queried |
|
| 1600 | - * @param array $query_params see EEM_Base::get_all() |
|
| 1601 | - */ |
|
| 1602 | - $fields_n_values = (array)apply_filters('FHEE__EEM_Base__update__fields_n_values', $fields_n_values, $this, |
|
| 1603 | - $query_params); |
|
| 1604 | - //need to verify that, for any entry we want to update, there are entries in each secondary table. |
|
| 1605 | - //to do that, for each table, verify that it's PK isn't null. |
|
| 1606 | - $tables = $this->get_tables(); |
|
| 1607 | - //and if the other tables don't have a row for each table-to-be-updated, we'll insert one with whatever values available in the current update query |
|
| 1608 | - //NOTE: we should make this code more efficient by NOT querying twice |
|
| 1609 | - //before the real update, but that needs to first go through ALPHA testing |
|
| 1610 | - //as it's dangerous. says Mike August 8 2014 |
|
| 1611 | - //we want to make sure the default_where strategy is ignored |
|
| 1612 | - $this->_ignore_where_strategy = true; |
|
| 1613 | - $wpdb_select_results = $this->_get_all_wpdb_results($query_params); |
|
| 1614 | - foreach ($wpdb_select_results as $wpdb_result) { |
|
| 1615 | - // type cast stdClass as array |
|
| 1616 | - $wpdb_result = (array)$wpdb_result; |
|
| 1617 | - //get the model object's PK, as we'll want this if we need to insert a row into secondary tables |
|
| 1618 | - if ($this->has_primary_key_field()) { |
|
| 1619 | - $main_table_pk_value = $wpdb_result[$this->get_primary_key_field()->get_qualified_column()]; |
|
| 1620 | - } else { |
|
| 1621 | - //if there's no primary key, we basically can't support having a 2nd table on the model (we could but it would be lots of work) |
|
| 1622 | - $main_table_pk_value = null; |
|
| 1623 | - } |
|
| 1624 | - //if there are more than 1 tables, we'll want to verify that each table for this model has an entry in the other tables |
|
| 1625 | - //and if the other tables don't have a row for each table-to-be-updated, we'll insert one with whatever values available in the current update query |
|
| 1626 | - if (count($tables) > 1) { |
|
| 1627 | - //foreach matching row in the DB, ensure that each table's PK isn't null. If so, there must not be an entry |
|
| 1628 | - //in that table, and so we'll want to insert one |
|
| 1629 | - foreach ($tables as $table_obj) { |
|
| 1630 | - $this_table_pk_column = $table_obj->get_fully_qualified_pk_column(); |
|
| 1631 | - //if there is no private key for this table on the results, it means there's no entry |
|
| 1632 | - //in this table, right? so insert a row in the current table, using any fields available |
|
| 1633 | - if (! (array_key_exists($this_table_pk_column, $wpdb_result) |
|
| 1634 | - && $wpdb_result[$this_table_pk_column]) |
|
| 1635 | - ) { |
|
| 1636 | - $success = $this->_insert_into_specific_table($table_obj, $fields_n_values, |
|
| 1637 | - $main_table_pk_value); |
|
| 1638 | - //if we died here, report the error |
|
| 1639 | - if (! $success) { |
|
| 1640 | - return false; |
|
| 1641 | - } |
|
| 1642 | - } |
|
| 1643 | - } |
|
| 1644 | - } |
|
| 1645 | - // //and now check that if we have cached any models by that ID on the model, that |
|
| 1646 | - // //they also get updated properly |
|
| 1647 | - // $model_object = $this->get_from_entity_map( $main_table_pk_value ); |
|
| 1648 | - // if( $model_object ){ |
|
| 1649 | - // foreach( $fields_n_values as $field => $value ){ |
|
| 1650 | - // $model_object->set($field, $value); |
|
| 1651 | - //let's make sure default_where strategy is followed now |
|
| 1652 | - $this->_ignore_where_strategy = false; |
|
| 1653 | - } |
|
| 1654 | - //if we want to keep model objects in sync, AND |
|
| 1655 | - //if this wasn't called from a model object (to update itself) |
|
| 1656 | - //then we want to make sure we keep all the existing |
|
| 1657 | - //model objects in sync with the db |
|
| 1658 | - if ($keep_model_objs_in_sync && ! $this->_values_already_prepared_by_model_object) { |
|
| 1659 | - if ($this->has_primary_key_field()) { |
|
| 1660 | - $model_objs_affected_ids = $this->get_col($query_params); |
|
| 1661 | - } else { |
|
| 1662 | - //we need to select a bunch of columns and then combine them into the the "index primary key string"s |
|
| 1663 | - $models_affected_key_columns = $this->_get_all_wpdb_results($query_params, ARRAY_A); |
|
| 1664 | - $model_objs_affected_ids = array(); |
|
| 1665 | - foreach ($models_affected_key_columns as $row) { |
|
| 1666 | - $combined_index_key = $this->get_index_primary_key_string($row); |
|
| 1667 | - $model_objs_affected_ids[$combined_index_key] = $combined_index_key; |
|
| 1668 | - } |
|
| 1669 | - } |
|
| 1670 | - if (! $model_objs_affected_ids) { |
|
| 1671 | - //wait wait wait- if nothing was affected let's stop here |
|
| 1672 | - return 0; |
|
| 1673 | - } |
|
| 1674 | - foreach ($model_objs_affected_ids as $id) { |
|
| 1675 | - $model_obj_in_entity_map = $this->get_from_entity_map($id); |
|
| 1676 | - if ($model_obj_in_entity_map) { |
|
| 1677 | - foreach ($fields_n_values as $field => $new_value) { |
|
| 1678 | - $model_obj_in_entity_map->set($field, $new_value); |
|
| 1679 | - } |
|
| 1680 | - } |
|
| 1681 | - } |
|
| 1682 | - //if there is a primary key on this model, we can now do a slight optimization |
|
| 1683 | - if ($this->has_primary_key_field()) { |
|
| 1684 | - //we already know what we want to update. So let's make the query simpler so it's a little more efficient |
|
| 1685 | - $query_params = array( |
|
| 1686 | - array($this->primary_key_name() => array('IN', $model_objs_affected_ids)), |
|
| 1687 | - 'limit' => count($model_objs_affected_ids), |
|
| 1688 | - 'default_where_conditions' => EEM_Base::default_where_conditions_none, |
|
| 1689 | - ); |
|
| 1690 | - } |
|
| 1691 | - } |
|
| 1692 | - $model_query_info = $this->_create_model_query_info_carrier($query_params); |
|
| 1693 | - $SQL = "UPDATE " |
|
| 1694 | - . $model_query_info->get_full_join_sql() |
|
| 1695 | - . " SET " |
|
| 1696 | - . $this->_construct_update_sql($fields_n_values) |
|
| 1697 | - . $model_query_info->get_where_sql();//note: doesn't use _construct_2nd_half_of_select_query() because doesn't accept LIMIT, ORDER BY, etc. |
|
| 1698 | - $rows_affected = $this->_do_wpdb_query('query', array($SQL)); |
|
| 1699 | - /** |
|
| 1700 | - * Action called after a model update call has been made. |
|
| 1701 | - * |
|
| 1702 | - * @param EEM_Base $model |
|
| 1703 | - * @param array $fields_n_values the updated fields and their new values |
|
| 1704 | - * @param array $query_params @see EEM_Base::get_all() |
|
| 1705 | - * @param int $rows_affected |
|
| 1706 | - */ |
|
| 1707 | - do_action('AHEE__EEM_Base__update__end', $this, $fields_n_values, $query_params, $rows_affected); |
|
| 1708 | - return $rows_affected;//how many supposedly got updated |
|
| 1709 | - } |
|
| 1710 | - |
|
| 1711 | - |
|
| 1712 | - |
|
| 1713 | - /** |
|
| 1714 | - * Analogous to $wpdb->get_col, returns a 1-dimensional array where teh values |
|
| 1715 | - * are teh values of the field specified (or by default the primary key field) |
|
| 1716 | - * that matched the query params. Note that you should pass the name of the |
|
| 1717 | - * model FIELD, not the database table's column name. |
|
| 1718 | - * |
|
| 1719 | - * @param array $query_params @see EEM_Base::get_all() |
|
| 1720 | - * @param string $field_to_select |
|
| 1721 | - * @return array just like $wpdb->get_col() |
|
| 1722 | - * @throws \EE_Error |
|
| 1723 | - */ |
|
| 1724 | - public function get_col($query_params = array(), $field_to_select = null) |
|
| 1725 | - { |
|
| 1726 | - if ($field_to_select) { |
|
| 1727 | - $field = $this->field_settings_for($field_to_select); |
|
| 1728 | - } elseif ($this->has_primary_key_field()) { |
|
| 1729 | - $field = $this->get_primary_key_field(); |
|
| 1730 | - } else { |
|
| 1731 | - //no primary key, just grab the first column |
|
| 1732 | - $field = reset($this->field_settings()); |
|
| 1733 | - } |
|
| 1734 | - $model_query_info = $this->_create_model_query_info_carrier($query_params); |
|
| 1735 | - $select_expressions = $field->get_qualified_column(); |
|
| 1736 | - $SQL = "SELECT $select_expressions " . $this->_construct_2nd_half_of_select_query($model_query_info); |
|
| 1737 | - return $this->_do_wpdb_query('get_col', array($SQL)); |
|
| 1738 | - } |
|
| 1739 | - |
|
| 1740 | - |
|
| 1741 | - |
|
| 1742 | - /** |
|
| 1743 | - * Returns a single column value for a single row from the database |
|
| 1744 | - * |
|
| 1745 | - * @param array $query_params @see EEM_Base::get_all() |
|
| 1746 | - * @param string $field_to_select @see EEM_Base::get_col() |
|
| 1747 | - * @return string |
|
| 1748 | - * @throws \EE_Error |
|
| 1749 | - */ |
|
| 1750 | - public function get_var($query_params = array(), $field_to_select = null) |
|
| 1751 | - { |
|
| 1752 | - $query_params['limit'] = 1; |
|
| 1753 | - $col = $this->get_col($query_params, $field_to_select); |
|
| 1754 | - if (! empty($col)) { |
|
| 1755 | - return reset($col); |
|
| 1756 | - } else { |
|
| 1757 | - return null; |
|
| 1758 | - } |
|
| 1759 | - } |
|
| 1760 | - |
|
| 1761 | - |
|
| 1762 | - |
|
| 1763 | - /** |
|
| 1764 | - * Makes the SQL for after "UPDATE table_X inner join table_Y..." and before "...WHERE". Eg "Question.name='party |
|
| 1765 | - * time?', Question.desc='what do you think?',..." Values are filtered through wpdb->prepare to avoid against SQL |
|
| 1766 | - * injection, but currently no further filtering is done |
|
| 1767 | - * |
|
| 1768 | - * @global $wpdb |
|
| 1769 | - * @param array $fields_n_values array keys are field names on this model, and values are what those fields should |
|
| 1770 | - * be updated to in the DB |
|
| 1771 | - * @return string of SQL |
|
| 1772 | - * @throws \EE_Error |
|
| 1773 | - */ |
|
| 1774 | - public function _construct_update_sql($fields_n_values) |
|
| 1775 | - { |
|
| 1776 | - /** @type WPDB $wpdb */ |
|
| 1777 | - global $wpdb; |
|
| 1778 | - $cols_n_values = array(); |
|
| 1779 | - foreach ($fields_n_values as $field_name => $value) { |
|
| 1780 | - $field_obj = $this->field_settings_for($field_name); |
|
| 1781 | - //if the value is NULL, we want to assign the value to that. |
|
| 1782 | - //wpdb->prepare doesn't really handle that properly |
|
| 1783 | - $prepared_value = $this->_prepare_value_or_use_default($field_obj, $fields_n_values); |
|
| 1784 | - $value_sql = $prepared_value === null ? 'NULL' |
|
| 1785 | - : $wpdb->prepare($field_obj->get_wpdb_data_type(), $prepared_value); |
|
| 1786 | - $cols_n_values[] = $field_obj->get_qualified_column() . "=" . $value_sql; |
|
| 1787 | - } |
|
| 1788 | - return implode(",", $cols_n_values); |
|
| 1789 | - } |
|
| 1790 | - |
|
| 1791 | - |
|
| 1792 | - |
|
| 1793 | - /** |
|
| 1794 | - * Deletes a single row from the DB given the model object's primary key value. (eg, EE_Attendee->ID()'s value). |
|
| 1795 | - * Performs a HARD delete, meaning the database row should always be removed, |
|
| 1796 | - * not just have a flag field on it switched |
|
| 1797 | - * Wrapper for EEM_Base::delete_permanently() |
|
| 1798 | - * |
|
| 1799 | - * @param mixed $id |
|
| 1800 | - * @param boolean $allow_blocking |
|
| 1801 | - * @return int the number of rows deleted |
|
| 1802 | - * @throws \EE_Error |
|
| 1803 | - */ |
|
| 1804 | - public function delete_permanently_by_ID($id, $allow_blocking = true) |
|
| 1805 | - { |
|
| 1806 | - return $this->delete_permanently( |
|
| 1807 | - array( |
|
| 1808 | - array($this->get_primary_key_field()->get_name() => $id), |
|
| 1809 | - 'limit' => 1, |
|
| 1810 | - ), |
|
| 1811 | - $allow_blocking |
|
| 1812 | - ); |
|
| 1813 | - } |
|
| 1814 | - |
|
| 1815 | - |
|
| 1816 | - |
|
| 1817 | - /** |
|
| 1818 | - * Deletes a single row from the DB given the model object's primary key value. (eg, EE_Attendee->ID()'s value). |
|
| 1819 | - * Wrapper for EEM_Base::delete() |
|
| 1820 | - * |
|
| 1821 | - * @param mixed $id |
|
| 1822 | - * @param boolean $allow_blocking |
|
| 1823 | - * @return int the number of rows deleted |
|
| 1824 | - * @throws \EE_Error |
|
| 1825 | - */ |
|
| 1826 | - public function delete_by_ID($id, $allow_blocking = true) |
|
| 1827 | - { |
|
| 1828 | - return $this->delete( |
|
| 1829 | - array( |
|
| 1830 | - array($this->get_primary_key_field()->get_name() => $id), |
|
| 1831 | - 'limit' => 1, |
|
| 1832 | - ), |
|
| 1833 | - $allow_blocking |
|
| 1834 | - ); |
|
| 1835 | - } |
|
| 1836 | - |
|
| 1837 | - |
|
| 1838 | - |
|
| 1839 | - /** |
|
| 1840 | - * Identical to delete_permanently, but does a "soft" delete if possible, |
|
| 1841 | - * meaning if the model has a field that indicates its been "trashed" or |
|
| 1842 | - * "soft deleted", we will just set that instead of actually deleting the rows. |
|
| 1843 | - * |
|
| 1844 | - * @see EEM_Base::delete_permanently |
|
| 1845 | - * @param array $query_params |
|
| 1846 | - * @param boolean $allow_blocking |
|
| 1847 | - * @return int how many rows got deleted |
|
| 1848 | - * @throws \EE_Error |
|
| 1849 | - */ |
|
| 1850 | - public function delete($query_params, $allow_blocking = true) |
|
| 1851 | - { |
|
| 1852 | - return $this->delete_permanently($query_params, $allow_blocking); |
|
| 1853 | - } |
|
| 1854 | - |
|
| 1855 | - |
|
| 1856 | - |
|
| 1857 | - /** |
|
| 1858 | - * Deletes the model objects that meet the query params. Note: this method is overridden |
|
| 1859 | - * in EEM_Soft_Delete_Base so that soft-deleted model objects are instead only flagged |
|
| 1860 | - * as archived, not actually deleted |
|
| 1861 | - * |
|
| 1862 | - * @param array $query_params very much like EEM_Base::get_all's $query_params |
|
| 1863 | - * @param boolean $allow_blocking if TRUE, matched objects will only be deleted if there is no related model info |
|
| 1864 | - * that blocks it (ie, there' sno other data that depends on this data); if false, |
|
| 1865 | - * deletes regardless of other objects which may depend on it. Its generally |
|
| 1866 | - * advisable to always leave this as TRUE, otherwise you could easily corrupt your |
|
| 1867 | - * DB |
|
| 1868 | - * @return int how many rows got deleted |
|
| 1869 | - * @throws \EE_Error |
|
| 1870 | - */ |
|
| 1871 | - public function delete_permanently($query_params, $allow_blocking = true) |
|
| 1872 | - { |
|
| 1873 | - /** |
|
| 1874 | - * Action called just before performing a real deletion query. You can use the |
|
| 1875 | - * model and its $query_params to find exactly which items will be deleted |
|
| 1876 | - * |
|
| 1877 | - * @param EEM_Base $model |
|
| 1878 | - * @param array $query_params @see EEM_Base::get_all() |
|
| 1879 | - * @param boolean $allow_blocking whether or not to allow related model objects |
|
| 1880 | - * to block (prevent) this deletion |
|
| 1881 | - */ |
|
| 1882 | - do_action('AHEE__EEM_Base__delete__begin', $this, $query_params, $allow_blocking); |
|
| 1883 | - //some MySQL databases may be running safe mode, which may restrict |
|
| 1884 | - //deletion if there is no KEY column used in the WHERE statement of a deletion. |
|
| 1885 | - //to get around this, we first do a SELECT, get all the IDs, and then run another query |
|
| 1886 | - //to delete them |
|
| 1887 | - $items_for_deletion = $this->_get_all_wpdb_results($query_params); |
|
| 1888 | - $deletion_where = $this->_setup_ids_for_delete($items_for_deletion, $allow_blocking); |
|
| 1889 | - if ($deletion_where) { |
|
| 1890 | - //echo "objects for deletion:";var_dump($objects_for_deletion); |
|
| 1891 | - $model_query_info = $this->_create_model_query_info_carrier($query_params); |
|
| 1892 | - $table_aliases = array_keys($this->_tables); |
|
| 1893 | - $SQL = "DELETE " |
|
| 1894 | - . implode(", ", $table_aliases) |
|
| 1895 | - . " FROM " |
|
| 1896 | - . $model_query_info->get_full_join_sql() |
|
| 1897 | - . " WHERE " |
|
| 1898 | - . $deletion_where; |
|
| 1899 | - // /echo "delete sql:$SQL"; |
|
| 1900 | - $rows_deleted = $this->_do_wpdb_query('query', array($SQL)); |
|
| 1901 | - } else { |
|
| 1902 | - $rows_deleted = 0; |
|
| 1903 | - } |
|
| 1904 | - //and lastly make sure those items are removed from the entity map; if they could be put into it at all |
|
| 1905 | - if ($this->has_primary_key_field()) { |
|
| 1906 | - foreach ($items_for_deletion as $item_for_deletion_row) { |
|
| 1907 | - $pk_value = $item_for_deletion_row[$this->get_primary_key_field()->get_qualified_column()]; |
|
| 1908 | - if (isset($this->_entity_map[EEM_Base::$_model_query_blog_id][$pk_value])) { |
|
| 1909 | - unset($this->_entity_map[EEM_Base::$_model_query_blog_id][$pk_value]); |
|
| 1910 | - } |
|
| 1911 | - } |
|
| 1912 | - } |
|
| 1913 | - /** |
|
| 1914 | - * Action called just after performing a real deletion query. Although at this point the |
|
| 1915 | - * items should have been deleted |
|
| 1916 | - * |
|
| 1917 | - * @param EEM_Base $model |
|
| 1918 | - * @param array $query_params @see EEM_Base::get_all() |
|
| 1919 | - * @param int $rows_deleted |
|
| 1920 | - */ |
|
| 1921 | - do_action('AHEE__EEM_Base__delete__end', $this, $query_params, $rows_deleted); |
|
| 1922 | - return $rows_deleted;//how many supposedly got deleted |
|
| 1923 | - } |
|
| 1924 | - |
|
| 1925 | - |
|
| 1926 | - |
|
| 1927 | - /** |
|
| 1928 | - * Checks all the relations that throw error messages when there are blocking related objects |
|
| 1929 | - * for related model objects. If there are any related model objects on those relations, |
|
| 1930 | - * adds an EE_Error, and return true |
|
| 1931 | - * |
|
| 1932 | - * @param EE_Base_Class|int $this_model_obj_or_id |
|
| 1933 | - * @param EE_Base_Class $ignore_this_model_obj a model object like 'EE_Event', or 'EE_Term_Taxonomy', which |
|
| 1934 | - * should be ignored when determining whether there are related |
|
| 1935 | - * model objects which block this model object's deletion. Useful |
|
| 1936 | - * if you know A is related to B and are considering deleting A, |
|
| 1937 | - * but want to see if A has any other objects blocking its deletion |
|
| 1938 | - * before removing the relation between A and B |
|
| 1939 | - * @return boolean |
|
| 1940 | - * @throws \EE_Error |
|
| 1941 | - */ |
|
| 1942 | - public function delete_is_blocked_by_related_models($this_model_obj_or_id, $ignore_this_model_obj = null) |
|
| 1943 | - { |
|
| 1944 | - //first, if $ignore_this_model_obj was supplied, get its model |
|
| 1945 | - if ($ignore_this_model_obj && $ignore_this_model_obj instanceof EE_Base_Class) { |
|
| 1946 | - $ignored_model = $ignore_this_model_obj->get_model(); |
|
| 1947 | - } else { |
|
| 1948 | - $ignored_model = null; |
|
| 1949 | - } |
|
| 1950 | - //now check all the relations of $this_model_obj_or_id and see if there |
|
| 1951 | - //are any related model objects blocking it? |
|
| 1952 | - $is_blocked = false; |
|
| 1953 | - foreach ($this->_model_relations as $relation_name => $relation_obj) { |
|
| 1954 | - if ($relation_obj->block_delete_if_related_models_exist()) { |
|
| 1955 | - //if $ignore_this_model_obj was supplied, then for the query |
|
| 1956 | - //on that model needs to be told to ignore $ignore_this_model_obj |
|
| 1957 | - if ($ignored_model && $relation_name === $ignored_model->get_this_model_name()) { |
|
| 1958 | - $related_model_objects = $relation_obj->get_all_related($this_model_obj_or_id, array( |
|
| 1959 | - array( |
|
| 1960 | - $ignored_model->get_primary_key_field()->get_name() => array( |
|
| 1961 | - '!=', |
|
| 1962 | - $ignore_this_model_obj->ID(), |
|
| 1963 | - ), |
|
| 1964 | - ), |
|
| 1965 | - )); |
|
| 1966 | - } else { |
|
| 1967 | - $related_model_objects = $relation_obj->get_all_related($this_model_obj_or_id); |
|
| 1968 | - } |
|
| 1969 | - if ($related_model_objects) { |
|
| 1970 | - EE_Error::add_error($relation_obj->get_deletion_error_message(), __FILE__, __FUNCTION__, __LINE__); |
|
| 1971 | - $is_blocked = true; |
|
| 1972 | - } |
|
| 1973 | - } |
|
| 1974 | - } |
|
| 1975 | - return $is_blocked; |
|
| 1976 | - } |
|
| 1977 | - |
|
| 1978 | - |
|
| 1979 | - |
|
| 1980 | - /** |
|
| 1981 | - * This sets up our delete where sql and accounts for if we have secondary tables that will have rows deleted as |
|
| 1982 | - * well. |
|
| 1983 | - * |
|
| 1984 | - * @param array $objects_for_deletion This should be the values returned by $this->_get_all_wpdb_results() |
|
| 1985 | - * @param boolean $allow_blocking if TRUE, matched objects will only be deleted if there is no related model |
|
| 1986 | - * info that blocks it (ie, there' sno other data that depends on this data); |
|
| 1987 | - * if false, deletes regardless of other objects which may depend on it. Its |
|
| 1988 | - * generally advisable to always leave this as TRUE, otherwise you could |
|
| 1989 | - * easily corrupt your DB |
|
| 1990 | - * @throws EE_Error |
|
| 1991 | - * @return string everything that comes after the WHERE statement. |
|
| 1992 | - */ |
|
| 1993 | - protected function _setup_ids_for_delete($objects_for_deletion, $allow_blocking = true) |
|
| 1994 | - { |
|
| 1995 | - if ($this->has_primary_key_field()) { |
|
| 1996 | - $primary_table = $this->_get_main_table(); |
|
| 1997 | - $pt_pk_field = $this->get_field_by_column($primary_table->get_fully_qualified_pk_column()); |
|
| 1998 | - $other_tables = $this->_get_other_tables(); |
|
| 1999 | - $ot_pk_fields = array(); |
|
| 2000 | - $ot_fk_fields = array(); |
|
| 2001 | - foreach($other_tables as $other_table_alias => $other_table_obj){ |
|
| 2002 | - $ot_pk_fields[$other_table_alias] = $this->get_field_by_column($other_table_obj->get_fully_qualified_pk_column()); |
|
| 2003 | - $ot_fk_fields[$other_table_alias] = $this->get_field_by_column($other_table_obj->get_fully_qualified_fk_column()); |
|
| 2004 | - } |
|
| 2005 | - $deletes = $query = array(); |
|
| 2006 | - foreach ($objects_for_deletion as $delete_object) { |
|
| 2007 | - //before we mark this object for deletion, |
|
| 2008 | - //make sure there's no related objects blocking its deletion (if we're checking) |
|
| 2009 | - if ( |
|
| 2010 | - $allow_blocking |
|
| 2011 | - && $this->delete_is_blocked_by_related_models( |
|
| 2012 | - $delete_object[$primary_table->get_fully_qualified_pk_column()] |
|
| 2013 | - ) |
|
| 2014 | - ) { |
|
| 2015 | - continue; |
|
| 2016 | - } |
|
| 2017 | - //primary table deletes |
|
| 2018 | - if (isset($delete_object[$primary_table->get_fully_qualified_pk_column()])) { |
|
| 2019 | - |
|
| 2020 | - $deletes[$primary_table->get_fully_qualified_pk_column()][] = $this->_wpdb_prepare_using_field( |
|
| 2021 | - $delete_object[$primary_table->get_fully_qualified_pk_column()], |
|
| 2022 | - $pt_pk_field |
|
| 2023 | - ); |
|
| 2024 | - } |
|
| 2025 | - //other tables |
|
| 2026 | - if (! empty($other_tables)) { |
|
| 2027 | - foreach ($other_tables as $ot_alias => $ot) { |
|
| 2028 | - $ot_pk_field = $ot_pk_fields[$ot_alias]; |
|
| 2029 | - $ot_fk_field = $ot_fk_fields[$ot_alias]; |
|
| 2030 | - //first check if we've got the foreign key column here. |
|
| 2031 | - if (isset($delete_object[$ot->get_fully_qualified_fk_column()])) { |
|
| 2032 | - $deletes[$ot->get_fully_qualified_pk_column()][] = $this->_wpdb_prepare_using_field( |
|
| 2033 | - $delete_object[$ot->get_fully_qualified_fk_column()], |
|
| 2034 | - $ot_fk_field |
|
| 2035 | - ); |
|
| 2036 | - } |
|
| 2037 | - // wait! it's entirely possible that we'll have a the primary key |
|
| 2038 | - // for this table in here, if it's a foreign key for one of the other secondary tables |
|
| 2039 | - if (isset($delete_object[$ot->get_fully_qualified_pk_column()])) { |
|
| 2040 | - $deletes[$ot->get_fully_qualified_pk_column()][] = $this->_wpdb_prepare_using_field( |
|
| 2041 | - $delete_object[$ot->get_fully_qualified_pk_column()], |
|
| 2042 | - $ot_pk_field |
|
| 2043 | - ); |
|
| 2044 | - } |
|
| 2045 | - // finally, it is possible that the fk for this table is found |
|
| 2046 | - // in the fully qualified pk column for the fk table, so let's see if that's there! |
|
| 2047 | - if (isset($delete_object[$ot->get_fully_qualified_pk_on_fk_table()])) { |
|
| 2048 | - $deletes[$ot->get_fully_qualified_pk_column()][] = $this->_wpdb_prepare_using_field( |
|
| 2049 | - $delete_object[$ot->get_fully_qualified_pk_column()], |
|
| 2050 | - $ot_pk_field |
|
| 2051 | - ); |
|
| 2052 | - } |
|
| 2053 | - } |
|
| 2054 | - } |
|
| 2055 | - } |
|
| 2056 | - //we should have deletes now, so let's just go through and setup the where statement |
|
| 2057 | - foreach ($deletes as $column => $values) { |
|
| 2058 | - //make sure we have unique $values; |
|
| 2059 | - $values = array_unique($values); |
|
| 2060 | - $query[] = $column . ' IN(' . implode(",", $values) . ')'; |
|
| 2061 | - } |
|
| 2062 | - return ! empty($query) ? implode(' AND ', $query) : ''; |
|
| 2063 | - } elseif (count($this->get_combined_primary_key_fields()) > 1) { |
|
| 2064 | - $ways_to_identify_a_row = array(); |
|
| 2065 | - $fields = $this->get_combined_primary_key_fields(); |
|
| 2066 | - //note: because there' sno primary key, that means nothing else can be pointing to this model, right? |
|
| 2067 | - foreach ($objects_for_deletion as $delete_object) { |
|
| 2068 | - $values_for_each_cpk_for_a_row = array(); |
|
| 2069 | - foreach ($fields as $cpk_field) { |
|
| 2070 | - if ($cpk_field instanceof EE_Model_Field_Base) { |
|
| 2071 | - $values_for_each_cpk_for_a_row[] = $cpk_field->get_qualified_column() |
|
| 2072 | - . "=" |
|
| 2073 | - . $delete_object[$cpk_field->get_qualified_column()]; |
|
| 2074 | - } |
|
| 2075 | - } |
|
| 2076 | - $ways_to_identify_a_row[] = "(" . implode(" AND ", $values_for_each_cpk_for_a_row) . ")"; |
|
| 2077 | - } |
|
| 2078 | - return implode(" OR ", $ways_to_identify_a_row); |
|
| 2079 | - } else { |
|
| 2080 | - //so there's no primary key and no combined key... |
|
| 2081 | - //sorry, can't help you |
|
| 2082 | - throw new EE_Error(sprintf(__("Cannot delete objects of type %s because there is no primary key NOR combined key", |
|
| 2083 | - "event_espresso"), get_class($this))); |
|
| 2084 | - } |
|
| 2085 | - } |
|
| 2086 | - |
|
| 2087 | - |
|
| 2088 | - /** |
|
| 2089 | - * Gets the model field by the fully qualified name |
|
| 2090 | - * @param string $qualified_column_name eg 'Event_CPT.post_name' or $field_obj->get_qualified_column() |
|
| 2091 | - * @return EE_Model_Field_Base |
|
| 2092 | - */ |
|
| 2093 | - public function get_field_by_column($qualified_column_name) |
|
| 2094 | - { |
|
| 2095 | - foreach($this->field_settings(true) as $field_name => $field_obj){ |
|
| 2096 | - if($field_obj->get_qualified_column() === $qualified_column_name){ |
|
| 2097 | - return $field_obj; |
|
| 2098 | - } |
|
| 2099 | - } |
|
| 2100 | - throw new EE_Error( |
|
| 2101 | - sprintf( |
|
| 2102 | - esc_html__('Could not find a field on the model "%1$s" for qualified column "%2$s"', 'event_espresso'), |
|
| 2103 | - $this->get_this_model_name(), |
|
| 2104 | - $qualified_column_name |
|
| 2105 | - ) |
|
| 2106 | - ); |
|
| 2107 | - } |
|
| 2108 | - |
|
| 2109 | - |
|
| 2110 | - |
|
| 2111 | - /** |
|
| 2112 | - * Count all the rows that match criteria expressed in $query_params (an array just like arg to EEM_Base::get_all). |
|
| 2113 | - * If $field_to_count isn't provided, the model's primary key is used. Otherwise, we count by field_to_count's |
|
| 2114 | - * column |
|
| 2115 | - * |
|
| 2116 | - * @param array $query_params like EEM_Base::get_all's |
|
| 2117 | - * @param string $field_to_count field on model to count by (not column name) |
|
| 2118 | - * @param bool $distinct if we want to only count the distinct values for the column then you can trigger |
|
| 2119 | - * that by the setting $distinct to TRUE; |
|
| 2120 | - * @return int |
|
| 2121 | - * @throws \EE_Error |
|
| 2122 | - */ |
|
| 2123 | - public function count($query_params = array(), $field_to_count = null, $distinct = false) |
|
| 2124 | - { |
|
| 2125 | - $model_query_info = $this->_create_model_query_info_carrier($query_params); |
|
| 2126 | - if ($field_to_count) { |
|
| 2127 | - $field_obj = $this->field_settings_for($field_to_count); |
|
| 2128 | - $column_to_count = $field_obj->get_qualified_column(); |
|
| 2129 | - } elseif ($this->has_primary_key_field()) { |
|
| 2130 | - $pk_field_obj = $this->get_primary_key_field(); |
|
| 2131 | - $column_to_count = $pk_field_obj->get_qualified_column(); |
|
| 2132 | - } else { |
|
| 2133 | - //there's no primary key |
|
| 2134 | - //if we're counting distinct items, and there's no primary key, |
|
| 2135 | - //we need to list out the columns for distinction; |
|
| 2136 | - //otherwise we can just use star |
|
| 2137 | - if ($distinct) { |
|
| 2138 | - $columns_to_use = array(); |
|
| 2139 | - foreach ($this->get_combined_primary_key_fields() as $field_obj) { |
|
| 2140 | - $columns_to_use[] = $field_obj->get_qualified_column(); |
|
| 2141 | - } |
|
| 2142 | - $column_to_count = implode(',', $columns_to_use); |
|
| 2143 | - } else { |
|
| 2144 | - $column_to_count = '*'; |
|
| 2145 | - } |
|
| 2146 | - } |
|
| 2147 | - $column_to_count = $distinct ? "DISTINCT " . $column_to_count : $column_to_count; |
|
| 2148 | - $SQL = "SELECT COUNT(" . $column_to_count . ")" . $this->_construct_2nd_half_of_select_query($model_query_info); |
|
| 2149 | - return (int)$this->_do_wpdb_query('get_var', array($SQL)); |
|
| 2150 | - } |
|
| 2151 | - |
|
| 2152 | - |
|
| 2153 | - |
|
| 2154 | - /** |
|
| 2155 | - * Sums up the value of the $field_to_sum (defaults to the primary key, which isn't terribly useful) |
|
| 2156 | - * |
|
| 2157 | - * @param array $query_params like EEM_Base::get_all |
|
| 2158 | - * @param string $field_to_sum name of field (array key in $_fields array) |
|
| 2159 | - * @return float |
|
| 2160 | - * @throws \EE_Error |
|
| 2161 | - */ |
|
| 2162 | - public function sum($query_params, $field_to_sum = null) |
|
| 2163 | - { |
|
| 2164 | - $model_query_info = $this->_create_model_query_info_carrier($query_params); |
|
| 2165 | - if ($field_to_sum) { |
|
| 2166 | - $field_obj = $this->field_settings_for($field_to_sum); |
|
| 2167 | - } else { |
|
| 2168 | - $field_obj = $this->get_primary_key_field(); |
|
| 2169 | - } |
|
| 2170 | - $column_to_count = $field_obj->get_qualified_column(); |
|
| 2171 | - $SQL = "SELECT SUM(" . $column_to_count . ")" . $this->_construct_2nd_half_of_select_query($model_query_info); |
|
| 2172 | - $return_value = $this->_do_wpdb_query('get_var', array($SQL)); |
|
| 2173 | - $data_type = $field_obj->get_wpdb_data_type(); |
|
| 2174 | - if ($data_type === '%d' || $data_type === '%s') { |
|
| 2175 | - return (float)$return_value; |
|
| 2176 | - } else {//must be %f |
|
| 2177 | - return (float)$return_value; |
|
| 2178 | - } |
|
| 2179 | - } |
|
| 2180 | - |
|
| 2181 | - |
|
| 2182 | - |
|
| 2183 | - /** |
|
| 2184 | - * Just calls the specified method on $wpdb with the given arguments |
|
| 2185 | - * Consolidates a little extra error handling code |
|
| 2186 | - * |
|
| 2187 | - * @param string $wpdb_method |
|
| 2188 | - * @param array $arguments_to_provide |
|
| 2189 | - * @throws EE_Error |
|
| 2190 | - * @global wpdb $wpdb |
|
| 2191 | - * @return mixed |
|
| 2192 | - */ |
|
| 2193 | - protected function _do_wpdb_query($wpdb_method, $arguments_to_provide) |
|
| 2194 | - { |
|
| 2195 | - //if we're in maintenance mode level 2, DON'T run any queries |
|
| 2196 | - //because level 2 indicates the database needs updating and |
|
| 2197 | - //is probably out of sync with the code |
|
| 2198 | - if (! EE_Maintenance_Mode::instance()->models_can_query()) { |
|
| 2199 | - throw new EE_Error(sprintf(__("Event Espresso Level 2 Maintenance mode is active. That means EE can not run ANY database queries until the necessary migration scripts have run which will take EE out of maintenance mode level 2. Please inform support of this error.", |
|
| 2200 | - "event_espresso"))); |
|
| 2201 | - } |
|
| 2202 | - /** @type WPDB $wpdb */ |
|
| 2203 | - global $wpdb; |
|
| 2204 | - if (! method_exists($wpdb, $wpdb_method)) { |
|
| 2205 | - throw new EE_Error(sprintf(__('There is no method named "%s" on Wordpress\' $wpdb object', |
|
| 2206 | - 'event_espresso'), $wpdb_method)); |
|
| 2207 | - } |
|
| 2208 | - if (WP_DEBUG) { |
|
| 2209 | - $old_show_errors_value = $wpdb->show_errors; |
|
| 2210 | - $wpdb->show_errors(false); |
|
| 2211 | - } |
|
| 2212 | - $result = $this->_process_wpdb_query($wpdb_method, $arguments_to_provide); |
|
| 2213 | - $this->show_db_query_if_previously_requested($wpdb->last_query); |
|
| 2214 | - if (WP_DEBUG) { |
|
| 2215 | - $wpdb->show_errors($old_show_errors_value); |
|
| 2216 | - if (! empty($wpdb->last_error)) { |
|
| 2217 | - throw new EE_Error(sprintf(__('WPDB Error: "%s"', 'event_espresso'), $wpdb->last_error)); |
|
| 2218 | - } elseif ($result === false) { |
|
| 2219 | - throw new EE_Error(sprintf(__('WPDB Error occurred, but no error message was logged by wpdb! The wpdb method called was "%1$s" and the arguments were "%2$s"', |
|
| 2220 | - 'event_espresso'), $wpdb_method, var_export($arguments_to_provide, true))); |
|
| 2221 | - } |
|
| 2222 | - } elseif ($result === false) { |
|
| 2223 | - EE_Error::add_error( |
|
| 2224 | - sprintf( |
|
| 2225 | - __('A database error has occurred. Turn on WP_DEBUG for more information.||A database error occurred doing wpdb method "%1$s", with arguments "%2$s". The error was "%3$s"', |
|
| 2226 | - 'event_espresso'), |
|
| 2227 | - $wpdb_method, |
|
| 2228 | - var_export($arguments_to_provide, true), |
|
| 2229 | - $wpdb->last_error |
|
| 2230 | - ), |
|
| 2231 | - __FILE__, |
|
| 2232 | - __FUNCTION__, |
|
| 2233 | - __LINE__ |
|
| 2234 | - ); |
|
| 2235 | - } |
|
| 2236 | - return $result; |
|
| 2237 | - } |
|
| 2238 | - |
|
| 2239 | - |
|
| 2240 | - |
|
| 2241 | - /** |
|
| 2242 | - * Attempts to run the indicated WPDB method with the provided arguments, |
|
| 2243 | - * and if there's an error tries to verify the DB is correct. Uses |
|
| 2244 | - * the static property EEM_Base::$_db_verification_level to determine whether |
|
| 2245 | - * we should try to fix the EE core db, the addons, or just give up |
|
| 2246 | - * |
|
| 2247 | - * @param string $wpdb_method |
|
| 2248 | - * @param array $arguments_to_provide |
|
| 2249 | - * @return mixed |
|
| 2250 | - */ |
|
| 2251 | - private function _process_wpdb_query($wpdb_method, $arguments_to_provide) |
|
| 2252 | - { |
|
| 2253 | - /** @type WPDB $wpdb */ |
|
| 2254 | - global $wpdb; |
|
| 2255 | - $wpdb->last_error = null; |
|
| 2256 | - $result = call_user_func_array(array($wpdb, $wpdb_method), $arguments_to_provide); |
|
| 2257 | - // was there an error running the query? but we don't care on new activations |
|
| 2258 | - // (we're going to setup the DB anyway on new activations) |
|
| 2259 | - if (($result === false || ! empty($wpdb->last_error)) |
|
| 2260 | - && EE_System::instance()->detect_req_type() !== EE_System::req_type_new_activation |
|
| 2261 | - ) { |
|
| 2262 | - switch (EEM_Base::$_db_verification_level) { |
|
| 2263 | - case EEM_Base::db_verified_none : |
|
| 2264 | - // let's double-check core's DB |
|
| 2265 | - $error_message = $this->_verify_core_db($wpdb_method, $arguments_to_provide); |
|
| 2266 | - break; |
|
| 2267 | - case EEM_Base::db_verified_core : |
|
| 2268 | - // STILL NO LOVE?? verify all the addons too. Maybe they need to be fixed |
|
| 2269 | - $error_message = $this->_verify_addons_db($wpdb_method, $arguments_to_provide); |
|
| 2270 | - break; |
|
| 2271 | - case EEM_Base::db_verified_addons : |
|
| 2272 | - // ummmm... you in trouble |
|
| 2273 | - return $result; |
|
| 2274 | - break; |
|
| 2275 | - } |
|
| 2276 | - if (! empty($error_message)) { |
|
| 2277 | - EE_Log::instance()->log(__FILE__, __FUNCTION__, $error_message, 'error'); |
|
| 2278 | - trigger_error($error_message); |
|
| 2279 | - } |
|
| 2280 | - return $this->_process_wpdb_query($wpdb_method, $arguments_to_provide); |
|
| 2281 | - } |
|
| 2282 | - return $result; |
|
| 2283 | - } |
|
| 2284 | - |
|
| 2285 | - |
|
| 2286 | - |
|
| 2287 | - /** |
|
| 2288 | - * Verifies the EE core database is up-to-date and records that we've done it on |
|
| 2289 | - * EEM_Base::$_db_verification_level |
|
| 2290 | - * |
|
| 2291 | - * @param string $wpdb_method |
|
| 2292 | - * @param array $arguments_to_provide |
|
| 2293 | - * @return string |
|
| 2294 | - */ |
|
| 2295 | - private function _verify_core_db($wpdb_method, $arguments_to_provide) |
|
| 2296 | - { |
|
| 2297 | - /** @type WPDB $wpdb */ |
|
| 2298 | - global $wpdb; |
|
| 2299 | - //ok remember that we've already attempted fixing the core db, in case the problem persists |
|
| 2300 | - EEM_Base::$_db_verification_level = EEM_Base::db_verified_core; |
|
| 2301 | - $error_message = sprintf( |
|
| 2302 | - __('WPDB Error "%1$s" while running wpdb method "%2$s" with arguments %3$s. Automatically attempting to fix EE Core DB', |
|
| 2303 | - 'event_espresso'), |
|
| 2304 | - $wpdb->last_error, |
|
| 2305 | - $wpdb_method, |
|
| 2306 | - wp_json_encode($arguments_to_provide) |
|
| 2307 | - ); |
|
| 2308 | - EE_System::instance()->initialize_db_if_no_migrations_required(false, true); |
|
| 2309 | - return $error_message; |
|
| 2310 | - } |
|
| 2311 | - |
|
| 2312 | - |
|
| 2313 | - |
|
| 2314 | - /** |
|
| 2315 | - * Verifies the EE addons' database is up-to-date and records that we've done it on |
|
| 2316 | - * EEM_Base::$_db_verification_level |
|
| 2317 | - * |
|
| 2318 | - * @param $wpdb_method |
|
| 2319 | - * @param $arguments_to_provide |
|
| 2320 | - * @return string |
|
| 2321 | - */ |
|
| 2322 | - private function _verify_addons_db($wpdb_method, $arguments_to_provide) |
|
| 2323 | - { |
|
| 2324 | - /** @type WPDB $wpdb */ |
|
| 2325 | - global $wpdb; |
|
| 2326 | - //ok remember that we've already attempted fixing the addons dbs, in case the problem persists |
|
| 2327 | - EEM_Base::$_db_verification_level = EEM_Base::db_verified_addons; |
|
| 2328 | - $error_message = sprintf( |
|
| 2329 | - __('WPDB AGAIN: Error "%1$s" while running the same method and arguments as before. Automatically attempting to fix EE Addons DB', |
|
| 2330 | - 'event_espresso'), |
|
| 2331 | - $wpdb->last_error, |
|
| 2332 | - $wpdb_method, |
|
| 2333 | - wp_json_encode($arguments_to_provide) |
|
| 2334 | - ); |
|
| 2335 | - EE_System::instance()->initialize_addons(); |
|
| 2336 | - return $error_message; |
|
| 2337 | - } |
|
| 2338 | - |
|
| 2339 | - |
|
| 2340 | - |
|
| 2341 | - /** |
|
| 2342 | - * In order to avoid repeating this code for the get_all, sum, and count functions, put the code parts |
|
| 2343 | - * that are identical in here. Returns a string of SQL of everything in a SELECT query except the beginning |
|
| 2344 | - * SELECT clause, eg " FROM wp_posts AS Event INNER JOIN ... WHERE ... ORDER BY ... LIMIT ... GROUP BY ... HAVING |
|
| 2345 | - * ..." |
|
| 2346 | - * |
|
| 2347 | - * @param EE_Model_Query_Info_Carrier $model_query_info |
|
| 2348 | - * @return string |
|
| 2349 | - */ |
|
| 2350 | - private function _construct_2nd_half_of_select_query(EE_Model_Query_Info_Carrier $model_query_info) |
|
| 2351 | - { |
|
| 2352 | - return " FROM " . $model_query_info->get_full_join_sql() . |
|
| 2353 | - $model_query_info->get_where_sql() . |
|
| 2354 | - $model_query_info->get_group_by_sql() . |
|
| 2355 | - $model_query_info->get_having_sql() . |
|
| 2356 | - $model_query_info->get_order_by_sql() . |
|
| 2357 | - $model_query_info->get_limit_sql(); |
|
| 2358 | - } |
|
| 2359 | - |
|
| 2360 | - |
|
| 2361 | - |
|
| 2362 | - /** |
|
| 2363 | - * Set to easily debug the next X queries ran from this model. |
|
| 2364 | - * |
|
| 2365 | - * @param int $count |
|
| 2366 | - */ |
|
| 2367 | - public function show_next_x_db_queries($count = 1) |
|
| 2368 | - { |
|
| 2369 | - $this->_show_next_x_db_queries = $count; |
|
| 2370 | - } |
|
| 2371 | - |
|
| 2372 | - |
|
| 2373 | - |
|
| 2374 | - /** |
|
| 2375 | - * @param $sql_query |
|
| 2376 | - */ |
|
| 2377 | - public function show_db_query_if_previously_requested($sql_query) |
|
| 2378 | - { |
|
| 2379 | - if ($this->_show_next_x_db_queries > 0) { |
|
| 2380 | - echo $sql_query; |
|
| 2381 | - $this->_show_next_x_db_queries--; |
|
| 2382 | - } |
|
| 2383 | - } |
|
| 2384 | - |
|
| 2385 | - |
|
| 2386 | - |
|
| 2387 | - /** |
|
| 2388 | - * Adds a relationship of the correct type between $modelObject and $otherModelObject. |
|
| 2389 | - * There are the 3 cases: |
|
| 2390 | - * 'belongsTo' relationship: sets $id_or_obj's foreign_key to be $other_model_id_or_obj's primary_key. If |
|
| 2391 | - * $otherModelObject has no ID, it is first saved. |
|
| 2392 | - * 'hasMany' relationship: sets $other_model_id_or_obj's foreign_key to be $id_or_obj's primary_key. If $id_or_obj |
|
| 2393 | - * has no ID, it is first saved. |
|
| 2394 | - * 'hasAndBelongsToMany' relationships: checks that there isn't already an entry in the join table, and adds one. |
|
| 2395 | - * If one of the model Objects has not yet been saved to the database, it is saved before adding the entry in the |
|
| 2396 | - * join table |
|
| 2397 | - * |
|
| 2398 | - * @param EE_Base_Class /int $thisModelObject |
|
| 2399 | - * @param EE_Base_Class /int $id_or_obj EE_base_Class or ID of other Model Object |
|
| 2400 | - * @param string $relationName , key in EEM_Base::_relations |
|
| 2401 | - * an attendee to a group, you also want to specify which role they |
|
| 2402 | - * will have in that group. So you would use this parameter to |
|
| 2403 | - * specify array('role-column-name'=>'role-id') |
|
| 2404 | - * @param array $extra_join_model_fields_n_values This allows you to enter further query params for the relation |
|
| 2405 | - * to for relation to methods that allow you to further specify |
|
| 2406 | - * extra columns to join by (such as HABTM). Keep in mind that the |
|
| 2407 | - * only acceptable query_params is strict "col" => "value" pairs |
|
| 2408 | - * because these will be inserted in any new rows created as well. |
|
| 2409 | - * @return EE_Base_Class which was added as a relation. Object referred to by $other_model_id_or_obj |
|
| 2410 | - * @throws \EE_Error |
|
| 2411 | - */ |
|
| 2412 | - public function add_relationship_to( |
|
| 2413 | - $id_or_obj, |
|
| 2414 | - $other_model_id_or_obj, |
|
| 2415 | - $relationName, |
|
| 2416 | - $extra_join_model_fields_n_values = array() |
|
| 2417 | - ) { |
|
| 2418 | - $relation_obj = $this->related_settings_for($relationName); |
|
| 2419 | - return $relation_obj->add_relation_to($id_or_obj, $other_model_id_or_obj, $extra_join_model_fields_n_values); |
|
| 2420 | - } |
|
| 2421 | - |
|
| 2422 | - |
|
| 2423 | - |
|
| 2424 | - /** |
|
| 2425 | - * Removes a relationship of the correct type between $modelObject and $otherModelObject. |
|
| 2426 | - * There are the 3 cases: |
|
| 2427 | - * 'belongsTo' relationship: sets $modelObject's foreign_key to null, if that field is nullable.Otherwise throws an |
|
| 2428 | - * error |
|
| 2429 | - * 'hasMany' relationship: sets $otherModelObject's foreign_key to null,if that field is nullable.Otherwise throws |
|
| 2430 | - * an error |
|
| 2431 | - * 'hasAndBelongsToMany' relationships:removes any existing entry in the join table between the two models. |
|
| 2432 | - * |
|
| 2433 | - * @param EE_Base_Class /int $id_or_obj |
|
| 2434 | - * @param EE_Base_Class /int $other_model_id_or_obj EE_Base_Class or ID of other Model Object |
|
| 2435 | - * @param string $relationName key in EEM_Base::_relations |
|
| 2436 | - * @return boolean of success |
|
| 2437 | - * @throws \EE_Error |
|
| 2438 | - * @param array $where_query This allows you to enter further query params for the relation to for relation to |
|
| 2439 | - * methods that allow you to further specify extra columns to join by (such as HABTM). |
|
| 2440 | - * Keep in mind that the only acceptable query_params is strict "col" => "value" pairs |
|
| 2441 | - * because these will be inserted in any new rows created as well. |
|
| 2442 | - */ |
|
| 2443 | - public function remove_relationship_to($id_or_obj, $other_model_id_or_obj, $relationName, $where_query = array()) |
|
| 2444 | - { |
|
| 2445 | - $relation_obj = $this->related_settings_for($relationName); |
|
| 2446 | - return $relation_obj->remove_relation_to($id_or_obj, $other_model_id_or_obj, $where_query); |
|
| 2447 | - } |
|
| 2448 | - |
|
| 2449 | - |
|
| 2450 | - |
|
| 2451 | - /** |
|
| 2452 | - * @param mixed $id_or_obj |
|
| 2453 | - * @param string $relationName |
|
| 2454 | - * @param array $where_query_params |
|
| 2455 | - * @param EE_Base_Class[] objects to which relations were removed |
|
| 2456 | - * @return \EE_Base_Class[] |
|
| 2457 | - * @throws \EE_Error |
|
| 2458 | - */ |
|
| 2459 | - public function remove_relations($id_or_obj, $relationName, $where_query_params = array()) |
|
| 2460 | - { |
|
| 2461 | - $relation_obj = $this->related_settings_for($relationName); |
|
| 2462 | - return $relation_obj->remove_relations($id_or_obj, $where_query_params); |
|
| 2463 | - } |
|
| 2464 | - |
|
| 2465 | - |
|
| 2466 | - |
|
| 2467 | - /** |
|
| 2468 | - * Gets all the related items of the specified $model_name, using $query_params. |
|
| 2469 | - * Note: by default, we remove the "default query params" |
|
| 2470 | - * because we want to get even deleted items etc. |
|
| 2471 | - * |
|
| 2472 | - * @param mixed $id_or_obj EE_Base_Class child or its ID |
|
| 2473 | - * @param string $model_name like 'Event', 'Registration', etc. always singular |
|
| 2474 | - * @param array $query_params like EEM_Base::get_all |
|
| 2475 | - * @return EE_Base_Class[] |
|
| 2476 | - * @throws \EE_Error |
|
| 2477 | - */ |
|
| 2478 | - public function get_all_related($id_or_obj, $model_name, $query_params = null) |
|
| 2479 | - { |
|
| 2480 | - $model_obj = $this->ensure_is_obj($id_or_obj); |
|
| 2481 | - $relation_settings = $this->related_settings_for($model_name); |
|
| 2482 | - return $relation_settings->get_all_related($model_obj, $query_params); |
|
| 2483 | - } |
|
| 2484 | - |
|
| 2485 | - |
|
| 2486 | - |
|
| 2487 | - /** |
|
| 2488 | - * Deletes all the model objects across the relation indicated by $model_name |
|
| 2489 | - * which are related to $id_or_obj which meet the criteria set in $query_params. |
|
| 2490 | - * However, if the model objects can't be deleted because of blocking related model objects, then |
|
| 2491 | - * they aren't deleted. (Unless the thing that would have been deleted can be soft-deleted, that still happens). |
|
| 2492 | - * |
|
| 2493 | - * @param EE_Base_Class|int|string $id_or_obj |
|
| 2494 | - * @param string $model_name |
|
| 2495 | - * @param array $query_params |
|
| 2496 | - * @return int how many deleted |
|
| 2497 | - * @throws \EE_Error |
|
| 2498 | - */ |
|
| 2499 | - public function delete_related($id_or_obj, $model_name, $query_params = array()) |
|
| 2500 | - { |
|
| 2501 | - $model_obj = $this->ensure_is_obj($id_or_obj); |
|
| 2502 | - $relation_settings = $this->related_settings_for($model_name); |
|
| 2503 | - return $relation_settings->delete_all_related($model_obj, $query_params); |
|
| 2504 | - } |
|
| 2505 | - |
|
| 2506 | - |
|
| 2507 | - |
|
| 2508 | - /** |
|
| 2509 | - * Hard deletes all the model objects across the relation indicated by $model_name |
|
| 2510 | - * which are related to $id_or_obj which meet the criteria set in $query_params. If |
|
| 2511 | - * the model objects can't be hard deleted because of blocking related model objects, |
|
| 2512 | - * just does a soft-delete on them instead. |
|
| 2513 | - * |
|
| 2514 | - * @param EE_Base_Class|int|string $id_or_obj |
|
| 2515 | - * @param string $model_name |
|
| 2516 | - * @param array $query_params |
|
| 2517 | - * @return int how many deleted |
|
| 2518 | - * @throws \EE_Error |
|
| 2519 | - */ |
|
| 2520 | - public function delete_related_permanently($id_or_obj, $model_name, $query_params = array()) |
|
| 2521 | - { |
|
| 2522 | - $model_obj = $this->ensure_is_obj($id_or_obj); |
|
| 2523 | - $relation_settings = $this->related_settings_for($model_name); |
|
| 2524 | - return $relation_settings->delete_related_permanently($model_obj, $query_params); |
|
| 2525 | - } |
|
| 2526 | - |
|
| 2527 | - |
|
| 2528 | - |
|
| 2529 | - /** |
|
| 2530 | - * Instead of getting the related model objects, simply counts them. Ignores default_where_conditions by default, |
|
| 2531 | - * unless otherwise specified in the $query_params |
|
| 2532 | - * |
|
| 2533 | - * @param int /EE_Base_Class $id_or_obj |
|
| 2534 | - * @param string $model_name like 'Event', or 'Registration' |
|
| 2535 | - * @param array $query_params like EEM_Base::get_all's |
|
| 2536 | - * @param string $field_to_count name of field to count by. By default, uses primary key |
|
| 2537 | - * @param bool $distinct if we want to only count the distinct values for the column then you can trigger |
|
| 2538 | - * that by the setting $distinct to TRUE; |
|
| 2539 | - * @return int |
|
| 2540 | - * @throws \EE_Error |
|
| 2541 | - */ |
|
| 2542 | - public function count_related( |
|
| 2543 | - $id_or_obj, |
|
| 2544 | - $model_name, |
|
| 2545 | - $query_params = array(), |
|
| 2546 | - $field_to_count = null, |
|
| 2547 | - $distinct = false |
|
| 2548 | - ) { |
|
| 2549 | - $related_model = $this->get_related_model_obj($model_name); |
|
| 2550 | - //we're just going to use the query params on the related model's normal get_all query, |
|
| 2551 | - //except add a condition to say to match the current mod |
|
| 2552 | - if (! isset($query_params['default_where_conditions'])) { |
|
| 2553 | - $query_params['default_where_conditions'] = EEM_Base::default_where_conditions_none; |
|
| 2554 | - } |
|
| 2555 | - $this_model_name = $this->get_this_model_name(); |
|
| 2556 | - $this_pk_field_name = $this->get_primary_key_field()->get_name(); |
|
| 2557 | - $query_params[0][$this_model_name . "." . $this_pk_field_name] = $id_or_obj; |
|
| 2558 | - return $related_model->count($query_params, $field_to_count, $distinct); |
|
| 2559 | - } |
|
| 2560 | - |
|
| 2561 | - |
|
| 2562 | - |
|
| 2563 | - /** |
|
| 2564 | - * Instead of getting the related model objects, simply sums up the values of the specified field. |
|
| 2565 | - * Note: ignores default_where_conditions by default, unless otherwise specified in the $query_params |
|
| 2566 | - * |
|
| 2567 | - * @param int /EE_Base_Class $id_or_obj |
|
| 2568 | - * @param string $model_name like 'Event', or 'Registration' |
|
| 2569 | - * @param array $query_params like EEM_Base::get_all's |
|
| 2570 | - * @param string $field_to_sum name of field to count by. By default, uses primary key |
|
| 2571 | - * @return float |
|
| 2572 | - * @throws \EE_Error |
|
| 2573 | - */ |
|
| 2574 | - public function sum_related($id_or_obj, $model_name, $query_params, $field_to_sum = null) |
|
| 2575 | - { |
|
| 2576 | - $related_model = $this->get_related_model_obj($model_name); |
|
| 2577 | - if (! is_array($query_params)) { |
|
| 2578 | - EE_Error::doing_it_wrong('EEM_Base::sum_related', |
|
| 2579 | - sprintf(__('$query_params should be an array, you passed a variable of type %s', 'event_espresso'), |
|
| 2580 | - gettype($query_params)), '4.6.0'); |
|
| 2581 | - $query_params = array(); |
|
| 2582 | - } |
|
| 2583 | - //we're just going to use the query params on the related model's normal get_all query, |
|
| 2584 | - //except add a condition to say to match the current mod |
|
| 2585 | - if (! isset($query_params['default_where_conditions'])) { |
|
| 2586 | - $query_params['default_where_conditions'] = EEM_Base::default_where_conditions_none; |
|
| 2587 | - } |
|
| 2588 | - $this_model_name = $this->get_this_model_name(); |
|
| 2589 | - $this_pk_field_name = $this->get_primary_key_field()->get_name(); |
|
| 2590 | - $query_params[0][$this_model_name . "." . $this_pk_field_name] = $id_or_obj; |
|
| 2591 | - return $related_model->sum($query_params, $field_to_sum); |
|
| 2592 | - } |
|
| 2593 | - |
|
| 2594 | - |
|
| 2595 | - |
|
| 2596 | - /** |
|
| 2597 | - * Uses $this->_relatedModels info to find the first related model object of relation $relationName to the given |
|
| 2598 | - * $modelObject |
|
| 2599 | - * |
|
| 2600 | - * @param int | EE_Base_Class $id_or_obj EE_Base_Class child or its ID |
|
| 2601 | - * @param string $other_model_name , key in $this->_relatedModels, eg 'Registration', or 'Events' |
|
| 2602 | - * @param array $query_params like EEM_Base::get_all's |
|
| 2603 | - * @return EE_Base_Class |
|
| 2604 | - * @throws \EE_Error |
|
| 2605 | - */ |
|
| 2606 | - public function get_first_related(EE_Base_Class $id_or_obj, $other_model_name, $query_params) |
|
| 2607 | - { |
|
| 2608 | - $query_params['limit'] = 1; |
|
| 2609 | - $results = $this->get_all_related($id_or_obj, $other_model_name, $query_params); |
|
| 2610 | - if ($results) { |
|
| 2611 | - return array_shift($results); |
|
| 2612 | - } else { |
|
| 2613 | - return null; |
|
| 2614 | - } |
|
| 2615 | - } |
|
| 2616 | - |
|
| 2617 | - |
|
| 2618 | - |
|
| 2619 | - /** |
|
| 2620 | - * Gets the model's name as it's expected in queries. For example, if this is EEM_Event model, that would be Event |
|
| 2621 | - * |
|
| 2622 | - * @return string |
|
| 2623 | - */ |
|
| 2624 | - public function get_this_model_name() |
|
| 2625 | - { |
|
| 2626 | - return str_replace("EEM_", "", get_class($this)); |
|
| 2627 | - } |
|
| 2628 | - |
|
| 2629 | - |
|
| 2630 | - |
|
| 2631 | - /** |
|
| 2632 | - * Gets the model field on this model which is of type EE_Any_Foreign_Model_Name_Field |
|
| 2633 | - * |
|
| 2634 | - * @return EE_Any_Foreign_Model_Name_Field |
|
| 2635 | - * @throws EE_Error |
|
| 2636 | - */ |
|
| 2637 | - public function get_field_containing_related_model_name() |
|
| 2638 | - { |
|
| 2639 | - foreach ($this->field_settings(true) as $field) { |
|
| 2640 | - if ($field instanceof EE_Any_Foreign_Model_Name_Field) { |
|
| 2641 | - $field_with_model_name = $field; |
|
| 2642 | - } |
|
| 2643 | - } |
|
| 2644 | - if (! isset($field_with_model_name) || ! $field_with_model_name) { |
|
| 2645 | - throw new EE_Error(sprintf(__("There is no EE_Any_Foreign_Model_Name field on model %s", "event_espresso"), |
|
| 2646 | - $this->get_this_model_name())); |
|
| 2647 | - } |
|
| 2648 | - return $field_with_model_name; |
|
| 2649 | - } |
|
| 2650 | - |
|
| 2651 | - |
|
| 2652 | - |
|
| 2653 | - /** |
|
| 2654 | - * Inserts a new entry into the database, for each table. |
|
| 2655 | - * Note: does not add the item to the entity map because that is done by EE_Base_Class::save() right after this. |
|
| 2656 | - * If client code uses EEM_Base::insert() directly, then although the item isn't in the entity map, |
|
| 2657 | - * we also know there is no model object with the newly inserted item's ID at the moment (because |
|
| 2658 | - * if there were, then they would already be in the DB and this would fail); and in the future if someone |
|
| 2659 | - * creates a model object with this ID (or grabs it from the DB) then it will be added to the |
|
| 2660 | - * entity map at that time anyways. SO, no need for EEM_Base::insert ot add to the entity map |
|
| 2661 | - * |
|
| 2662 | - * @param array $field_n_values keys are field names, values are their values (in the client code's domain if |
|
| 2663 | - * $values_already_prepared_by_model_object is false, in the model object's domain if |
|
| 2664 | - * $values_already_prepared_by_model_object is true. See comment about this at the top |
|
| 2665 | - * of EEM_Base) |
|
| 2666 | - * @return int new primary key on main table that got inserted |
|
| 2667 | - * @throws EE_Error |
|
| 2668 | - */ |
|
| 2669 | - public function insert($field_n_values) |
|
| 2670 | - { |
|
| 2671 | - /** |
|
| 2672 | - * Filters the fields and their values before inserting an item using the models |
|
| 2673 | - * |
|
| 2674 | - * @param array $fields_n_values keys are the fields and values are their new values |
|
| 2675 | - * @param EEM_Base $model the model used |
|
| 2676 | - */ |
|
| 2677 | - $field_n_values = (array)apply_filters('FHEE__EEM_Base__insert__fields_n_values', $field_n_values, $this); |
|
| 2678 | - if ($this->_satisfies_unique_indexes($field_n_values)) { |
|
| 2679 | - $main_table = $this->_get_main_table(); |
|
| 2680 | - $new_id = $this->_insert_into_specific_table($main_table, $field_n_values, false); |
|
| 2681 | - if ($new_id !== false) { |
|
| 2682 | - foreach ($this->_get_other_tables() as $other_table) { |
|
| 2683 | - $this->_insert_into_specific_table($other_table, $field_n_values, $new_id); |
|
| 2684 | - } |
|
| 2685 | - } |
|
| 2686 | - /** |
|
| 2687 | - * Done just after attempting to insert a new model object |
|
| 2688 | - * |
|
| 2689 | - * @param EEM_Base $model used |
|
| 2690 | - * @param array $fields_n_values fields and their values |
|
| 2691 | - * @param int|string the ID of the newly-inserted model object |
|
| 2692 | - */ |
|
| 2693 | - do_action('AHEE__EEM_Base__insert__end', $this, $field_n_values, $new_id); |
|
| 2694 | - return $new_id; |
|
| 2695 | - } else { |
|
| 2696 | - return false; |
|
| 2697 | - } |
|
| 2698 | - } |
|
| 2699 | - |
|
| 2700 | - |
|
| 2701 | - |
|
| 2702 | - /** |
|
| 2703 | - * Checks that the result would satisfy the unique indexes on this model |
|
| 2704 | - * |
|
| 2705 | - * @param array $field_n_values |
|
| 2706 | - * @param string $action |
|
| 2707 | - * @return boolean |
|
| 2708 | - * @throws \EE_Error |
|
| 2709 | - */ |
|
| 2710 | - protected function _satisfies_unique_indexes($field_n_values, $action = 'insert') |
|
| 2711 | - { |
|
| 2712 | - foreach ($this->unique_indexes() as $index_name => $index) { |
|
| 2713 | - $uniqueness_where_params = array_intersect_key($field_n_values, $index->fields()); |
|
| 2714 | - if ($this->exists(array($uniqueness_where_params))) { |
|
| 2715 | - EE_Error::add_error( |
|
| 2716 | - sprintf( |
|
| 2717 | - __( |
|
| 2718 | - "Could not %s %s. %s uniqueness index failed. Fields %s must form a unique set, but an entry already exists with values %s.", |
|
| 2719 | - "event_espresso" |
|
| 2720 | - ), |
|
| 2721 | - $action, |
|
| 2722 | - $this->_get_class_name(), |
|
| 2723 | - $index_name, |
|
| 2724 | - implode(",", $index->field_names()), |
|
| 2725 | - http_build_query($uniqueness_where_params) |
|
| 2726 | - ), |
|
| 2727 | - __FILE__, |
|
| 2728 | - __FUNCTION__, |
|
| 2729 | - __LINE__ |
|
| 2730 | - ); |
|
| 2731 | - return false; |
|
| 2732 | - } |
|
| 2733 | - } |
|
| 2734 | - return true; |
|
| 2735 | - } |
|
| 2736 | - |
|
| 2737 | - |
|
| 2738 | - |
|
| 2739 | - /** |
|
| 2740 | - * Checks the database for an item that conflicts (ie, if this item were |
|
| 2741 | - * saved to the DB would break some uniqueness requirement, like a primary key |
|
| 2742 | - * or an index primary key set) with the item specified. $id_obj_or_fields_array |
|
| 2743 | - * can be either an EE_Base_Class or an array of fields n values |
|
| 2744 | - * |
|
| 2745 | - * @param EE_Base_Class|array $obj_or_fields_array |
|
| 2746 | - * @param boolean $include_primary_key whether to use the model object's primary key |
|
| 2747 | - * when looking for conflicts |
|
| 2748 | - * (ie, if false, we ignore the model object's primary key |
|
| 2749 | - * when finding "conflicts". If true, it's also considered). |
|
| 2750 | - * Only works for INT primary key, |
|
| 2751 | - * STRING primary keys cannot be ignored |
|
| 2752 | - * @throws EE_Error |
|
| 2753 | - * @return EE_Base_Class|array |
|
| 2754 | - */ |
|
| 2755 | - public function get_one_conflicting($obj_or_fields_array, $include_primary_key = true) |
|
| 2756 | - { |
|
| 2757 | - if ($obj_or_fields_array instanceof EE_Base_Class) { |
|
| 2758 | - $fields_n_values = $obj_or_fields_array->model_field_array(); |
|
| 2759 | - } elseif (is_array($obj_or_fields_array)) { |
|
| 2760 | - $fields_n_values = $obj_or_fields_array; |
|
| 2761 | - } else { |
|
| 2762 | - throw new EE_Error( |
|
| 2763 | - sprintf( |
|
| 2764 | - __( |
|
| 2765 | - "%s get_all_conflicting should be called with a model object or an array of field names and values, you provided %d", |
|
| 2766 | - "event_espresso" |
|
| 2767 | - ), |
|
| 2768 | - get_class($this), |
|
| 2769 | - $obj_or_fields_array |
|
| 2770 | - ) |
|
| 2771 | - ); |
|
| 2772 | - } |
|
| 2773 | - $query_params = array(); |
|
| 2774 | - if ($this->has_primary_key_field() |
|
| 2775 | - && ($include_primary_key |
|
| 2776 | - || $this->get_primary_key_field() |
|
| 2777 | - instanceof |
|
| 2778 | - EE_Primary_Key_String_Field) |
|
| 2779 | - && isset($fields_n_values[$this->primary_key_name()]) |
|
| 2780 | - ) { |
|
| 2781 | - $query_params[0]['OR'][$this->primary_key_name()] = $fields_n_values[$this->primary_key_name()]; |
|
| 2782 | - } |
|
| 2783 | - foreach ($this->unique_indexes() as $unique_index_name => $unique_index) { |
|
| 2784 | - $uniqueness_where_params = array_intersect_key($fields_n_values, $unique_index->fields()); |
|
| 2785 | - $query_params[0]['OR']['AND*' . $unique_index_name] = $uniqueness_where_params; |
|
| 2786 | - } |
|
| 2787 | - //if there is nothing to base this search on, then we shouldn't find anything |
|
| 2788 | - if (empty($query_params)) { |
|
| 2789 | - return array(); |
|
| 2790 | - } else { |
|
| 2791 | - return $this->get_one($query_params); |
|
| 2792 | - } |
|
| 2793 | - } |
|
| 2794 | - |
|
| 2795 | - |
|
| 2796 | - |
|
| 2797 | - /** |
|
| 2798 | - * Like count, but is optimized and returns a boolean instead of an int |
|
| 2799 | - * |
|
| 2800 | - * @param array $query_params |
|
| 2801 | - * @return boolean |
|
| 2802 | - * @throws \EE_Error |
|
| 2803 | - */ |
|
| 2804 | - public function exists($query_params) |
|
| 2805 | - { |
|
| 2806 | - $query_params['limit'] = 1; |
|
| 2807 | - return $this->count($query_params) > 0; |
|
| 2808 | - } |
|
| 2809 | - |
|
| 2810 | - |
|
| 2811 | - |
|
| 2812 | - /** |
|
| 2813 | - * Wrapper for exists, except ignores default query parameters so we're only considering ID |
|
| 2814 | - * |
|
| 2815 | - * @param int|string $id |
|
| 2816 | - * @return boolean |
|
| 2817 | - * @throws \EE_Error |
|
| 2818 | - */ |
|
| 2819 | - public function exists_by_ID($id) |
|
| 2820 | - { |
|
| 2821 | - return $this->exists( |
|
| 2822 | - array( |
|
| 2823 | - 'default_where_conditions' => EEM_Base::default_where_conditions_none, |
|
| 2824 | - array( |
|
| 2825 | - $this->primary_key_name() => $id, |
|
| 2826 | - ), |
|
| 2827 | - ) |
|
| 2828 | - ); |
|
| 2829 | - } |
|
| 2830 | - |
|
| 2831 | - |
|
| 2832 | - |
|
| 2833 | - /** |
|
| 2834 | - * Inserts a new row in $table, using the $cols_n_values which apply to that table. |
|
| 2835 | - * If a $new_id is supplied and if $table is an EE_Other_Table, we assume |
|
| 2836 | - * we need to add a foreign key column to point to $new_id (which should be the primary key's value |
|
| 2837 | - * on the main table) |
|
| 2838 | - * This is protected rather than private because private is not accessible to any child methods and there MAY be |
|
| 2839 | - * cases where we want to call it directly rather than via insert(). |
|
| 2840 | - * |
|
| 2841 | - * @access protected |
|
| 2842 | - * @param EE_Table_Base $table |
|
| 2843 | - * @param array $fields_n_values each key should be in field's keys, and value should be an int, string or |
|
| 2844 | - * float |
|
| 2845 | - * @param int $new_id for now we assume only int keys |
|
| 2846 | - * @throws EE_Error |
|
| 2847 | - * @global WPDB $wpdb only used to get the $wpdb->insert_id after performing an insert |
|
| 2848 | - * @return int ID of new row inserted, or FALSE on failure |
|
| 2849 | - */ |
|
| 2850 | - protected function _insert_into_specific_table(EE_Table_Base $table, $fields_n_values, $new_id = 0) |
|
| 2851 | - { |
|
| 2852 | - global $wpdb; |
|
| 2853 | - $insertion_col_n_values = array(); |
|
| 2854 | - $format_for_insertion = array(); |
|
| 2855 | - $fields_on_table = $this->_get_fields_for_table($table->get_table_alias()); |
|
| 2856 | - foreach ($fields_on_table as $field_name => $field_obj) { |
|
| 2857 | - //check if its an auto-incrementing column, in which case we should just leave it to do its autoincrement thing |
|
| 2858 | - if ($field_obj->is_auto_increment()) { |
|
| 2859 | - continue; |
|
| 2860 | - } |
|
| 2861 | - $prepared_value = $this->_prepare_value_or_use_default($field_obj, $fields_n_values); |
|
| 2862 | - //if the value we want to assign it to is NULL, just don't mention it for the insertion |
|
| 2863 | - if ($prepared_value !== null) { |
|
| 2864 | - $insertion_col_n_values[$field_obj->get_table_column()] = $prepared_value; |
|
| 2865 | - $format_for_insertion[] = $field_obj->get_wpdb_data_type(); |
|
| 2866 | - } |
|
| 2867 | - } |
|
| 2868 | - if ($table instanceof EE_Secondary_Table && $new_id) { |
|
| 2869 | - //its not the main table, so we should have already saved the main table's PK which we just inserted |
|
| 2870 | - //so add the fk to the main table as a column |
|
| 2871 | - $insertion_col_n_values[$table->get_fk_on_table()] = $new_id; |
|
| 2872 | - $format_for_insertion[] = '%d';//yes right now we're only allowing these foreign keys to be INTs |
|
| 2873 | - } |
|
| 2874 | - //insert the new entry |
|
| 2875 | - $result = $this->_do_wpdb_query('insert', |
|
| 2876 | - array($table->get_table_name(), $insertion_col_n_values, $format_for_insertion)); |
|
| 2877 | - if ($result === false) { |
|
| 2878 | - return false; |
|
| 2879 | - } |
|
| 2880 | - //ok, now what do we return for the ID of the newly-inserted thing? |
|
| 2881 | - if ($this->has_primary_key_field()) { |
|
| 2882 | - if ($this->get_primary_key_field()->is_auto_increment()) { |
|
| 2883 | - return $wpdb->insert_id; |
|
| 2884 | - } else { |
|
| 2885 | - //it's not an auto-increment primary key, so |
|
| 2886 | - //it must have been supplied |
|
| 2887 | - return $fields_n_values[$this->get_primary_key_field()->get_name()]; |
|
| 2888 | - } |
|
| 2889 | - } else { |
|
| 2890 | - //we can't return a primary key because there is none. instead return |
|
| 2891 | - //a unique string indicating this model |
|
| 2892 | - return $this->get_index_primary_key_string($fields_n_values); |
|
| 2893 | - } |
|
| 2894 | - } |
|
| 2895 | - |
|
| 2896 | - |
|
| 2897 | - |
|
| 2898 | - /** |
|
| 2899 | - * Prepare the $field_obj 's value in $fields_n_values for use in the database. |
|
| 2900 | - * If the field doesn't allow NULL, try to use its default. (If it doesn't allow NULL, |
|
| 2901 | - * and there is no default, we pass it along. WPDB will take care of it) |
|
| 2902 | - * |
|
| 2903 | - * @param EE_Model_Field_Base $field_obj |
|
| 2904 | - * @param array $fields_n_values |
|
| 2905 | - * @return mixed string|int|float depending on what the table column will be expecting |
|
| 2906 | - * @throws \EE_Error |
|
| 2907 | - */ |
|
| 2908 | - protected function _prepare_value_or_use_default($field_obj, $fields_n_values) |
|
| 2909 | - { |
|
| 2910 | - //if this field doesn't allow nullable, don't allow it |
|
| 2911 | - if (! $field_obj->is_nullable() |
|
| 2912 | - && ( |
|
| 2913 | - ! isset($fields_n_values[$field_obj->get_name()]) || $fields_n_values[$field_obj->get_name()] === null) |
|
| 2914 | - ) { |
|
| 2915 | - $fields_n_values[$field_obj->get_name()] = $field_obj->get_default_value(); |
|
| 2916 | - } |
|
| 2917 | - $unprepared_value = isset($fields_n_values[$field_obj->get_name()]) ? $fields_n_values[$field_obj->get_name()] |
|
| 2918 | - : null; |
|
| 2919 | - return $this->_prepare_value_for_use_in_db($unprepared_value, $field_obj); |
|
| 2920 | - } |
|
| 2921 | - |
|
| 2922 | - |
|
| 2923 | - |
|
| 2924 | - /** |
|
| 2925 | - * Consolidates code for preparing a value supplied to the model for use int eh db. Calls the field's |
|
| 2926 | - * prepare_for_use_in_db method on the value, and depending on $value_already_prepare_by_model_obj, may also call |
|
| 2927 | - * the field's prepare_for_set() method. |
|
| 2928 | - * |
|
| 2929 | - * @param mixed $value value in the client code domain if $value_already_prepared_by_model_object is |
|
| 2930 | - * false, otherwise a value in the model object's domain (see lengthy comment at |
|
| 2931 | - * top of file) |
|
| 2932 | - * @param EE_Model_Field_Base $field field which will be doing the preparing of the value. If null, we assume |
|
| 2933 | - * $value is a custom selection |
|
| 2934 | - * @return mixed a value ready for use in the database for insertions, updating, or in a where clause |
|
| 2935 | - */ |
|
| 2936 | - private function _prepare_value_for_use_in_db($value, $field) |
|
| 2937 | - { |
|
| 2938 | - if ($field && $field instanceof EE_Model_Field_Base) { |
|
| 2939 | - switch ($this->_values_already_prepared_by_model_object) { |
|
| 2940 | - /** @noinspection PhpMissingBreakStatementInspection */ |
|
| 2941 | - case self::not_prepared_by_model_object: |
|
| 2942 | - $value = $field->prepare_for_set($value); |
|
| 2943 | - //purposefully left out "return" |
|
| 2944 | - case self::prepared_by_model_object: |
|
| 2945 | - $value = $field->prepare_for_use_in_db($value); |
|
| 2946 | - case self::prepared_for_use_in_db: |
|
| 2947 | - //leave the value alone |
|
| 2948 | - } |
|
| 2949 | - return $value; |
|
| 2950 | - } else { |
|
| 2951 | - return $value; |
|
| 2952 | - } |
|
| 2953 | - } |
|
| 2954 | - |
|
| 2955 | - |
|
| 2956 | - |
|
| 2957 | - /** |
|
| 2958 | - * Returns the main table on this model |
|
| 2959 | - * |
|
| 2960 | - * @return EE_Primary_Table |
|
| 2961 | - * @throws EE_Error |
|
| 2962 | - */ |
|
| 2963 | - protected function _get_main_table() |
|
| 2964 | - { |
|
| 2965 | - foreach ($this->_tables as $table) { |
|
| 2966 | - if ($table instanceof EE_Primary_Table) { |
|
| 2967 | - return $table; |
|
| 2968 | - } |
|
| 2969 | - } |
|
| 2970 | - throw new EE_Error(sprintf(__('There are no main tables on %s. They should be added to _tables array in the constructor', |
|
| 2971 | - 'event_espresso'), get_class($this))); |
|
| 2972 | - } |
|
| 2973 | - |
|
| 2974 | - |
|
| 2975 | - |
|
| 2976 | - /** |
|
| 2977 | - * table |
|
| 2978 | - * returns EE_Primary_Table table name |
|
| 2979 | - * |
|
| 2980 | - * @return string |
|
| 2981 | - * @throws \EE_Error |
|
| 2982 | - */ |
|
| 2983 | - public function table() |
|
| 2984 | - { |
|
| 2985 | - return $this->_get_main_table()->get_table_name(); |
|
| 2986 | - } |
|
| 2987 | - |
|
| 2988 | - |
|
| 2989 | - |
|
| 2990 | - /** |
|
| 2991 | - * table |
|
| 2992 | - * returns first EE_Secondary_Table table name |
|
| 2993 | - * |
|
| 2994 | - * @return string |
|
| 2995 | - */ |
|
| 2996 | - public function second_table() |
|
| 2997 | - { |
|
| 2998 | - // grab second table from tables array |
|
| 2999 | - $second_table = end($this->_tables); |
|
| 3000 | - return $second_table instanceof EE_Secondary_Table ? $second_table->get_table_name() : null; |
|
| 3001 | - } |
|
| 3002 | - |
|
| 3003 | - |
|
| 3004 | - |
|
| 3005 | - /** |
|
| 3006 | - * get_table_obj_by_alias |
|
| 3007 | - * returns table name given it's alias |
|
| 3008 | - * |
|
| 3009 | - * @param string $table_alias |
|
| 3010 | - * @return EE_Primary_Table | EE_Secondary_Table |
|
| 3011 | - */ |
|
| 3012 | - public function get_table_obj_by_alias($table_alias = '') |
|
| 3013 | - { |
|
| 3014 | - return isset($this->_tables[$table_alias]) ? $this->_tables[$table_alias] : null; |
|
| 3015 | - } |
|
| 3016 | - |
|
| 3017 | - |
|
| 3018 | - |
|
| 3019 | - /** |
|
| 3020 | - * Gets all the tables of type EE_Other_Table from EEM_CPT_Basel_Model::_tables |
|
| 3021 | - * |
|
| 3022 | - * @return EE_Secondary_Table[] |
|
| 3023 | - */ |
|
| 3024 | - protected function _get_other_tables() |
|
| 3025 | - { |
|
| 3026 | - $other_tables = array(); |
|
| 3027 | - foreach ($this->_tables as $table_alias => $table) { |
|
| 3028 | - if ($table instanceof EE_Secondary_Table) { |
|
| 3029 | - $other_tables[$table_alias] = $table; |
|
| 3030 | - } |
|
| 3031 | - } |
|
| 3032 | - return $other_tables; |
|
| 3033 | - } |
|
| 3034 | - |
|
| 3035 | - |
|
| 3036 | - |
|
| 3037 | - /** |
|
| 3038 | - * Finds all the fields that correspond to the given table |
|
| 3039 | - * |
|
| 3040 | - * @param string $table_alias , array key in EEM_Base::_tables |
|
| 3041 | - * @return EE_Model_Field_Base[] |
|
| 3042 | - */ |
|
| 3043 | - public function _get_fields_for_table($table_alias) |
|
| 3044 | - { |
|
| 3045 | - return $this->_fields[$table_alias]; |
|
| 3046 | - } |
|
| 3047 | - |
|
| 3048 | - |
|
| 3049 | - |
|
| 3050 | - /** |
|
| 3051 | - * Recurses through all the where parameters, and finds all the related models we'll need |
|
| 3052 | - * to complete this query. Eg, given where parameters like array('EVT_ID'=>3) from within Event model, we won't |
|
| 3053 | - * need any related models. But if the array were array('Registrations.REG_ID'=>3), we'd need the related |
|
| 3054 | - * Registration model. If it were array('Registrations.Transactions.Payments.PAY_ID'=>3), then we'd need the |
|
| 3055 | - * related Registration, Transaction, and Payment models. |
|
| 3056 | - * |
|
| 3057 | - * @param array $query_params like EEM_Base::get_all's $query_parameters['where'] |
|
| 3058 | - * @return EE_Model_Query_Info_Carrier |
|
| 3059 | - * @throws \EE_Error |
|
| 3060 | - */ |
|
| 3061 | - public function _extract_related_models_from_query($query_params) |
|
| 3062 | - { |
|
| 3063 | - $query_info_carrier = new EE_Model_Query_Info_Carrier(); |
|
| 3064 | - if (array_key_exists(0, $query_params)) { |
|
| 3065 | - $this->_extract_related_models_from_sub_params_array_keys($query_params[0], $query_info_carrier, 0); |
|
| 3066 | - } |
|
| 3067 | - if (array_key_exists('group_by', $query_params)) { |
|
| 3068 | - if (is_array($query_params['group_by'])) { |
|
| 3069 | - $this->_extract_related_models_from_sub_params_array_values( |
|
| 3070 | - $query_params['group_by'], |
|
| 3071 | - $query_info_carrier, |
|
| 3072 | - 'group_by' |
|
| 3073 | - ); |
|
| 3074 | - } elseif (! empty ($query_params['group_by'])) { |
|
| 3075 | - $this->_extract_related_model_info_from_query_param( |
|
| 3076 | - $query_params['group_by'], |
|
| 3077 | - $query_info_carrier, |
|
| 3078 | - 'group_by' |
|
| 3079 | - ); |
|
| 3080 | - } |
|
| 3081 | - } |
|
| 3082 | - if (array_key_exists('having', $query_params)) { |
|
| 3083 | - $this->_extract_related_models_from_sub_params_array_keys( |
|
| 3084 | - $query_params[0], |
|
| 3085 | - $query_info_carrier, |
|
| 3086 | - 'having' |
|
| 3087 | - ); |
|
| 3088 | - } |
|
| 3089 | - if (array_key_exists('order_by', $query_params)) { |
|
| 3090 | - if (is_array($query_params['order_by'])) { |
|
| 3091 | - $this->_extract_related_models_from_sub_params_array_keys( |
|
| 3092 | - $query_params['order_by'], |
|
| 3093 | - $query_info_carrier, |
|
| 3094 | - 'order_by' |
|
| 3095 | - ); |
|
| 3096 | - } elseif (! empty($query_params['order_by'])) { |
|
| 3097 | - $this->_extract_related_model_info_from_query_param( |
|
| 3098 | - $query_params['order_by'], |
|
| 3099 | - $query_info_carrier, |
|
| 3100 | - 'order_by' |
|
| 3101 | - ); |
|
| 3102 | - } |
|
| 3103 | - } |
|
| 3104 | - if (array_key_exists('force_join', $query_params)) { |
|
| 3105 | - $this->_extract_related_models_from_sub_params_array_values( |
|
| 3106 | - $query_params['force_join'], |
|
| 3107 | - $query_info_carrier, |
|
| 3108 | - 'force_join' |
|
| 3109 | - ); |
|
| 3110 | - } |
|
| 3111 | - return $query_info_carrier; |
|
| 3112 | - } |
|
| 3113 | - |
|
| 3114 | - |
|
| 3115 | - |
|
| 3116 | - /** |
|
| 3117 | - * For extracting related models from WHERE (0), HAVING (having), ORDER BY (order_by) or forced joins (force_join) |
|
| 3118 | - * |
|
| 3119 | - * @param array $sub_query_params like EEM_Base::get_all's $query_params[0] or |
|
| 3120 | - * $query_params['having'] |
|
| 3121 | - * @param EE_Model_Query_Info_Carrier $model_query_info_carrier |
|
| 3122 | - * @param string $query_param_type one of $this->_allowed_query_params |
|
| 3123 | - * @throws EE_Error |
|
| 3124 | - * @return \EE_Model_Query_Info_Carrier |
|
| 3125 | - */ |
|
| 3126 | - private function _extract_related_models_from_sub_params_array_keys( |
|
| 3127 | - $sub_query_params, |
|
| 3128 | - EE_Model_Query_Info_Carrier $model_query_info_carrier, |
|
| 3129 | - $query_param_type |
|
| 3130 | - ) { |
|
| 3131 | - if (! empty($sub_query_params)) { |
|
| 3132 | - $sub_query_params = (array)$sub_query_params; |
|
| 3133 | - foreach ($sub_query_params as $param => $possibly_array_of_params) { |
|
| 3134 | - //$param could be simply 'EVT_ID', or it could be 'Registrations.REG_ID', or even 'Registrations.Transactions.Payments.PAY_amount' |
|
| 3135 | - $this->_extract_related_model_info_from_query_param($param, $model_query_info_carrier, |
|
| 3136 | - $query_param_type); |
|
| 3137 | - //if $possibly_array_of_params is an array, try recursing into it, searching for keys which |
|
| 3138 | - //indicate needed joins. Eg, array('NOT'=>array('Registration.TXN_ID'=>23)). In this case, we tried |
|
| 3139 | - //extracting models out of the 'NOT', which obviously wasn't successful, and then we recurse into the value |
|
| 3140 | - //of array('Registration.TXN_ID'=>23) |
|
| 3141 | - $query_param_sans_stars = $this->_remove_stars_and_anything_after_from_condition_query_param_key($param); |
|
| 3142 | - if (in_array($query_param_sans_stars, $this->_logic_query_param_keys, true)) { |
|
| 3143 | - if (! is_array($possibly_array_of_params)) { |
|
| 3144 | - throw new EE_Error(sprintf(__("You used a special where query param %s, but the value isn't an array of where query params, it's just %s'. It should be an array, eg array('EVT_ID'=>23,'OR'=>array('Venue.VNU_ID'=>32,'Venue.VNU_name'=>'monkey_land'))", |
|
| 3145 | - "event_espresso"), |
|
| 3146 | - $param, $possibly_array_of_params)); |
|
| 3147 | - } else { |
|
| 3148 | - $this->_extract_related_models_from_sub_params_array_keys($possibly_array_of_params, |
|
| 3149 | - $model_query_info_carrier, $query_param_type); |
|
| 3150 | - } |
|
| 3151 | - } elseif ($query_param_type === 0 //ie WHERE |
|
| 3152 | - && is_array($possibly_array_of_params) |
|
| 3153 | - && isset($possibly_array_of_params[2]) |
|
| 3154 | - && $possibly_array_of_params[2] == true |
|
| 3155 | - ) { |
|
| 3156 | - //then $possible_array_of_params looks something like array('<','DTT_sold',true) |
|
| 3157 | - //indicating that $possible_array_of_params[1] is actually a field name, |
|
| 3158 | - //from which we should extract query parameters! |
|
| 3159 | - if (! isset($possibly_array_of_params[0], $possibly_array_of_params[1])) { |
|
| 3160 | - throw new EE_Error(sprintf(__("Improperly formed query parameter %s. It should be numerically indexed like array('<','DTT_sold',true); but you provided %s", |
|
| 3161 | - "event_espresso"), $query_param_type, implode(",", $possibly_array_of_params))); |
|
| 3162 | - } |
|
| 3163 | - $this->_extract_related_model_info_from_query_param($possibly_array_of_params[1], |
|
| 3164 | - $model_query_info_carrier, $query_param_type); |
|
| 3165 | - } |
|
| 3166 | - } |
|
| 3167 | - } |
|
| 3168 | - return $model_query_info_carrier; |
|
| 3169 | - } |
|
| 3170 | - |
|
| 3171 | - |
|
| 3172 | - |
|
| 3173 | - /** |
|
| 3174 | - * For extracting related models from forced_joins, where the array values contain the info about what |
|
| 3175 | - * models to join with. Eg an array like array('Attendee','Price.Price_Type'); |
|
| 3176 | - * |
|
| 3177 | - * @param array $sub_query_params like EEM_Base::get_all's $query_params[0] or |
|
| 3178 | - * $query_params['having'] |
|
| 3179 | - * @param EE_Model_Query_Info_Carrier $model_query_info_carrier |
|
| 3180 | - * @param string $query_param_type one of $this->_allowed_query_params |
|
| 3181 | - * @throws EE_Error |
|
| 3182 | - * @return \EE_Model_Query_Info_Carrier |
|
| 3183 | - */ |
|
| 3184 | - private function _extract_related_models_from_sub_params_array_values( |
|
| 3185 | - $sub_query_params, |
|
| 3186 | - EE_Model_Query_Info_Carrier $model_query_info_carrier, |
|
| 3187 | - $query_param_type |
|
| 3188 | - ) { |
|
| 3189 | - if (! empty($sub_query_params)) { |
|
| 3190 | - if (! is_array($sub_query_params)) { |
|
| 3191 | - throw new EE_Error(sprintf(__("Query parameter %s should be an array, but it isn't.", "event_espresso"), |
|
| 3192 | - $sub_query_params)); |
|
| 3193 | - } |
|
| 3194 | - foreach ($sub_query_params as $param) { |
|
| 3195 | - //$param could be simply 'EVT_ID', or it could be 'Registrations.REG_ID', or even 'Registrations.Transactions.Payments.PAY_amount' |
|
| 3196 | - $this->_extract_related_model_info_from_query_param($param, $model_query_info_carrier, |
|
| 3197 | - $query_param_type); |
|
| 3198 | - } |
|
| 3199 | - } |
|
| 3200 | - return $model_query_info_carrier; |
|
| 3201 | - } |
|
| 3202 | - |
|
| 3203 | - |
|
| 3204 | - |
|
| 3205 | - /** |
|
| 3206 | - * Extract all the query parts from $query_params (an array like whats passed to EEM_Base::get_all) |
|
| 3207 | - * and put into a EEM_Related_Model_Info_Carrier for easy extraction into a query. We create this object |
|
| 3208 | - * instead of directly constructing the SQL because often we need to extract info from the $query_params |
|
| 3209 | - * but use them in a different order. Eg, we need to know what models we are querying |
|
| 3210 | - * before we know what joins to perform. However, we need to know what data types correspond to which fields on |
|
| 3211 | - * other models before we can finalize the where clause SQL. |
|
| 3212 | - * |
|
| 3213 | - * @param array $query_params |
|
| 3214 | - * @throws EE_Error |
|
| 3215 | - * @return EE_Model_Query_Info_Carrier |
|
| 3216 | - */ |
|
| 3217 | - public function _create_model_query_info_carrier($query_params) |
|
| 3218 | - { |
|
| 3219 | - if (! is_array($query_params)) { |
|
| 3220 | - EE_Error::doing_it_wrong( |
|
| 3221 | - 'EEM_Base::_create_model_query_info_carrier', |
|
| 3222 | - sprintf( |
|
| 3223 | - __( |
|
| 3224 | - '$query_params should be an array, you passed a variable of type %s', |
|
| 3225 | - 'event_espresso' |
|
| 3226 | - ), |
|
| 3227 | - gettype($query_params) |
|
| 3228 | - ), |
|
| 3229 | - '4.6.0' |
|
| 3230 | - ); |
|
| 3231 | - $query_params = array(); |
|
| 3232 | - } |
|
| 3233 | - $where_query_params = isset($query_params[0]) ? $query_params[0] : array(); |
|
| 3234 | - //first check if we should alter the query to account for caps or not |
|
| 3235 | - //because the caps might require us to do extra joins |
|
| 3236 | - if (isset($query_params['caps']) && $query_params['caps'] !== 'none') { |
|
| 3237 | - $query_params[0] = $where_query_params = array_replace_recursive( |
|
| 3238 | - $where_query_params, |
|
| 3239 | - $this->caps_where_conditions( |
|
| 3240 | - $query_params['caps'] |
|
| 3241 | - ) |
|
| 3242 | - ); |
|
| 3243 | - } |
|
| 3244 | - $query_object = $this->_extract_related_models_from_query($query_params); |
|
| 3245 | - //verify where_query_params has NO numeric indexes.... that's simply not how you use it! |
|
| 3246 | - foreach ($where_query_params as $key => $value) { |
|
| 3247 | - if (is_int($key)) { |
|
| 3248 | - throw new EE_Error( |
|
| 3249 | - sprintf( |
|
| 3250 | - __( |
|
| 3251 | - "WHERE query params must NOT be numerically-indexed. You provided the array key '%s' for value '%s' while querying model %s. All the query params provided were '%s' Please read documentation on EEM_Base::get_all.", |
|
| 3252 | - "event_espresso" |
|
| 3253 | - ), |
|
| 3254 | - $key, |
|
| 3255 | - var_export($value, true), |
|
| 3256 | - var_export($query_params, true), |
|
| 3257 | - get_class($this) |
|
| 3258 | - ) |
|
| 3259 | - ); |
|
| 3260 | - } |
|
| 3261 | - } |
|
| 3262 | - if ( |
|
| 3263 | - array_key_exists('default_where_conditions', $query_params) |
|
| 3264 | - && ! empty($query_params['default_where_conditions']) |
|
| 3265 | - ) { |
|
| 3266 | - $use_default_where_conditions = $query_params['default_where_conditions']; |
|
| 3267 | - } else { |
|
| 3268 | - $use_default_where_conditions = EEM_Base::default_where_conditions_all; |
|
| 3269 | - } |
|
| 3270 | - $where_query_params = array_merge( |
|
| 3271 | - $this->_get_default_where_conditions_for_models_in_query( |
|
| 3272 | - $query_object, |
|
| 3273 | - $use_default_where_conditions, |
|
| 3274 | - $where_query_params |
|
| 3275 | - ), |
|
| 3276 | - $where_query_params |
|
| 3277 | - ); |
|
| 3278 | - $query_object->set_where_sql($this->_construct_where_clause($where_query_params)); |
|
| 3279 | - // if this is a "on_join_limit" then we are limiting on on a specific table in a multi_table join. |
|
| 3280 | - // So we need to setup a subquery and use that for the main join. |
|
| 3281 | - // Note for now this only works on the primary table for the model. |
|
| 3282 | - // So for instance, you could set the limit array like this: |
|
| 3283 | - // array( 'on_join_limit' => array('Primary_Table_Alias', array(1,10) ) ) |
|
| 3284 | - if (array_key_exists('on_join_limit', $query_params) && ! empty($query_params['on_join_limit'])) { |
|
| 3285 | - $query_object->set_main_model_join_sql( |
|
| 3286 | - $this->_construct_limit_join_select( |
|
| 3287 | - $query_params['on_join_limit'][0], |
|
| 3288 | - $query_params['on_join_limit'][1] |
|
| 3289 | - ) |
|
| 3290 | - ); |
|
| 3291 | - } |
|
| 3292 | - //set limit |
|
| 3293 | - if (array_key_exists('limit', $query_params)) { |
|
| 3294 | - if (is_array($query_params['limit'])) { |
|
| 3295 | - if (! isset($query_params['limit'][0], $query_params['limit'][1])) { |
|
| 3296 | - $e = sprintf( |
|
| 3297 | - __( |
|
| 3298 | - "Invalid DB query. You passed '%s' for the LIMIT, but only the following are valid: an integer, string representing an integer, a string like 'int,int', or an array like array(int,int)", |
|
| 3299 | - "event_espresso" |
|
| 3300 | - ), |
|
| 3301 | - http_build_query($query_params['limit']) |
|
| 3302 | - ); |
|
| 3303 | - throw new EE_Error($e . "|" . $e); |
|
| 3304 | - } |
|
| 3305 | - //they passed us an array for the limit. Assume it's like array(50,25), meaning offset by 50, and get 25 |
|
| 3306 | - $query_object->set_limit_sql(" LIMIT " . $query_params['limit'][0] . "," . $query_params['limit'][1]); |
|
| 3307 | - } elseif (! empty ($query_params['limit'])) { |
|
| 3308 | - $query_object->set_limit_sql(" LIMIT " . $query_params['limit']); |
|
| 3309 | - } |
|
| 3310 | - } |
|
| 3311 | - //set order by |
|
| 3312 | - if (array_key_exists('order_by', $query_params)) { |
|
| 3313 | - if (is_array($query_params['order_by'])) { |
|
| 3314 | - //if they're using 'order_by' as an array, they can't use 'order' (because 'order_by' must |
|
| 3315 | - //specify whether to ascend or descend on each field. Eg 'order_by'=>array('EVT_ID'=>'ASC'). So |
|
| 3316 | - //including 'order' wouldn't make any sense if 'order_by' has already specified which way to order! |
|
| 3317 | - if (array_key_exists('order', $query_params)) { |
|
| 3318 | - throw new EE_Error( |
|
| 3319 | - sprintf( |
|
| 3320 | - __( |
|
| 3321 | - "In querying %s, we are using query parameter 'order_by' as an array (keys:%s,values:%s), and so we can't use query parameter 'order' (value %s). You should just use the 'order_by' parameter ", |
|
| 3322 | - "event_espresso" |
|
| 3323 | - ), |
|
| 3324 | - get_class($this), |
|
| 3325 | - implode(", ", array_keys($query_params['order_by'])), |
|
| 3326 | - implode(", ", $query_params['order_by']), |
|
| 3327 | - $query_params['order'] |
|
| 3328 | - ) |
|
| 3329 | - ); |
|
| 3330 | - } |
|
| 3331 | - $this->_extract_related_models_from_sub_params_array_keys( |
|
| 3332 | - $query_params['order_by'], |
|
| 3333 | - $query_object, |
|
| 3334 | - 'order_by' |
|
| 3335 | - ); |
|
| 3336 | - //assume it's an array of fields to order by |
|
| 3337 | - $order_array = array(); |
|
| 3338 | - foreach ($query_params['order_by'] as $field_name_to_order_by => $order) { |
|
| 3339 | - $order = $this->_extract_order($order); |
|
| 3340 | - $order_array[] = $this->_deduce_column_name_from_query_param($field_name_to_order_by) . SP . $order; |
|
| 3341 | - } |
|
| 3342 | - $query_object->set_order_by_sql(" ORDER BY " . implode(",", $order_array)); |
|
| 3343 | - } elseif (! empty ($query_params['order_by'])) { |
|
| 3344 | - $this->_extract_related_model_info_from_query_param( |
|
| 3345 | - $query_params['order_by'], |
|
| 3346 | - $query_object, |
|
| 3347 | - 'order', |
|
| 3348 | - $query_params['order_by'] |
|
| 3349 | - ); |
|
| 3350 | - $order = isset($query_params['order']) |
|
| 3351 | - ? $this->_extract_order($query_params['order']) |
|
| 3352 | - : 'DESC'; |
|
| 3353 | - $query_object->set_order_by_sql( |
|
| 3354 | - " ORDER BY " . $this->_deduce_column_name_from_query_param($query_params['order_by']) . SP . $order |
|
| 3355 | - ); |
|
| 3356 | - } |
|
| 3357 | - } |
|
| 3358 | - //if 'order_by' wasn't set, maybe they are just using 'order' on its own? |
|
| 3359 | - if (! array_key_exists('order_by', $query_params) |
|
| 3360 | - && array_key_exists('order', $query_params) |
|
| 3361 | - && ! empty($query_params['order']) |
|
| 3362 | - ) { |
|
| 3363 | - $pk_field = $this->get_primary_key_field(); |
|
| 3364 | - $order = $this->_extract_order($query_params['order']); |
|
| 3365 | - $query_object->set_order_by_sql(" ORDER BY " . $pk_field->get_qualified_column() . SP . $order); |
|
| 3366 | - } |
|
| 3367 | - //set group by |
|
| 3368 | - if (array_key_exists('group_by', $query_params)) { |
|
| 3369 | - if (is_array($query_params['group_by'])) { |
|
| 3370 | - //it's an array, so assume we'll be grouping by a bunch of stuff |
|
| 3371 | - $group_by_array = array(); |
|
| 3372 | - foreach ($query_params['group_by'] as $field_name_to_group_by) { |
|
| 3373 | - $group_by_array[] = $this->_deduce_column_name_from_query_param($field_name_to_group_by); |
|
| 3374 | - } |
|
| 3375 | - $query_object->set_group_by_sql(" GROUP BY " . implode(", ", $group_by_array)); |
|
| 3376 | - } elseif (! empty ($query_params['group_by'])) { |
|
| 3377 | - $query_object->set_group_by_sql( |
|
| 3378 | - " GROUP BY " . $this->_deduce_column_name_from_query_param($query_params['group_by']) |
|
| 3379 | - ); |
|
| 3380 | - } |
|
| 3381 | - } |
|
| 3382 | - //set having |
|
| 3383 | - if (array_key_exists('having', $query_params) && $query_params['having']) { |
|
| 3384 | - $query_object->set_having_sql($this->_construct_having_clause($query_params['having'])); |
|
| 3385 | - } |
|
| 3386 | - //now, just verify they didn't pass anything wack |
|
| 3387 | - foreach ($query_params as $query_key => $query_value) { |
|
| 3388 | - if (! in_array($query_key, $this->_allowed_query_params, true)) { |
|
| 3389 | - throw new EE_Error( |
|
| 3390 | - sprintf( |
|
| 3391 | - __( |
|
| 3392 | - "You passed %s as a query parameter to %s, which is illegal! The allowed query parameters are %s", |
|
| 3393 | - 'event_espresso' |
|
| 3394 | - ), |
|
| 3395 | - $query_key, |
|
| 3396 | - get_class($this), |
|
| 3397 | - // print_r( $this->_allowed_query_params, TRUE ) |
|
| 3398 | - implode(',', $this->_allowed_query_params) |
|
| 3399 | - ) |
|
| 3400 | - ); |
|
| 3401 | - } |
|
| 3402 | - } |
|
| 3403 | - $main_model_join_sql = $query_object->get_main_model_join_sql(); |
|
| 3404 | - if (empty($main_model_join_sql)) { |
|
| 3405 | - $query_object->set_main_model_join_sql($this->_construct_internal_join()); |
|
| 3406 | - } |
|
| 3407 | - return $query_object; |
|
| 3408 | - } |
|
| 3409 | - |
|
| 3410 | - |
|
| 3411 | - |
|
| 3412 | - /** |
|
| 3413 | - * Gets the where conditions that should be imposed on the query based on the |
|
| 3414 | - * context (eg reading frontend, backend, edit or delete). |
|
| 3415 | - * |
|
| 3416 | - * @param string $context one of EEM_Base::valid_cap_contexts() |
|
| 3417 | - * @return array like EEM_Base::get_all() 's $query_params[0] |
|
| 3418 | - * @throws \EE_Error |
|
| 3419 | - */ |
|
| 3420 | - public function caps_where_conditions($context = self::caps_read) |
|
| 3421 | - { |
|
| 3422 | - EEM_Base::verify_is_valid_cap_context($context); |
|
| 3423 | - $cap_where_conditions = array(); |
|
| 3424 | - $cap_restrictions = $this->caps_missing($context); |
|
| 3425 | - /** |
|
| 3426 | - * @var $cap_restrictions EE_Default_Where_Conditions[] |
|
| 3427 | - */ |
|
| 3428 | - foreach ($cap_restrictions as $cap => $restriction_if_no_cap) { |
|
| 3429 | - $cap_where_conditions = array_replace_recursive($cap_where_conditions, |
|
| 3430 | - $restriction_if_no_cap->get_default_where_conditions()); |
|
| 3431 | - } |
|
| 3432 | - return apply_filters('FHEE__EEM_Base__caps_where_conditions__return', $cap_where_conditions, $this, $context, |
|
| 3433 | - $cap_restrictions); |
|
| 3434 | - } |
|
| 3435 | - |
|
| 3436 | - |
|
| 3437 | - |
|
| 3438 | - /** |
|
| 3439 | - * Verifies that $should_be_order_string is in $this->_allowed_order_values, |
|
| 3440 | - * otherwise throws an exception |
|
| 3441 | - * |
|
| 3442 | - * @param string $should_be_order_string |
|
| 3443 | - * @return string either ASC, asc, DESC or desc |
|
| 3444 | - * @throws EE_Error |
|
| 3445 | - */ |
|
| 3446 | - private function _extract_order($should_be_order_string) |
|
| 3447 | - { |
|
| 3448 | - if (in_array($should_be_order_string, $this->_allowed_order_values)) { |
|
| 3449 | - return $should_be_order_string; |
|
| 3450 | - } else { |
|
| 3451 | - throw new EE_Error(sprintf(__("While performing a query on '%s', tried to use '%s' as an order parameter. ", |
|
| 3452 | - "event_espresso"), get_class($this), $should_be_order_string)); |
|
| 3453 | - } |
|
| 3454 | - } |
|
| 3455 | - |
|
| 3456 | - |
|
| 3457 | - |
|
| 3458 | - /** |
|
| 3459 | - * Looks at all the models which are included in this query, and asks each |
|
| 3460 | - * for their universal_where_params, and returns them in the same format as $query_params[0] (where), |
|
| 3461 | - * so they can be merged |
|
| 3462 | - * |
|
| 3463 | - * @param EE_Model_Query_Info_Carrier $query_info_carrier |
|
| 3464 | - * @param string $use_default_where_conditions can be 'none','other_models_only', or 'all'. |
|
| 3465 | - * 'none' means NO default where conditions will |
|
| 3466 | - * be used AT ALL during this query. |
|
| 3467 | - * 'other_models_only' means default where |
|
| 3468 | - * conditions from other models will be used, but |
|
| 3469 | - * not for this primary model. 'all', the default, |
|
| 3470 | - * means default where conditions will apply as |
|
| 3471 | - * normal |
|
| 3472 | - * @param array $where_query_params like EEM_Base::get_all's $query_params[0] |
|
| 3473 | - * @throws EE_Error |
|
| 3474 | - * @return array like $query_params[0], see EEM_Base::get_all for documentation |
|
| 3475 | - */ |
|
| 3476 | - private function _get_default_where_conditions_for_models_in_query( |
|
| 3477 | - EE_Model_Query_Info_Carrier $query_info_carrier, |
|
| 3478 | - $use_default_where_conditions = EEM_Base::default_where_conditions_all, |
|
| 3479 | - $where_query_params = array() |
|
| 3480 | - ) { |
|
| 3481 | - $allowed_used_default_where_conditions_values = EEM_Base::valid_default_where_conditions(); |
|
| 3482 | - if (! in_array($use_default_where_conditions, $allowed_used_default_where_conditions_values)) { |
|
| 3483 | - throw new EE_Error(sprintf(__("You passed an invalid value to the query parameter 'default_where_conditions' of '%s'. Allowed values are %s", |
|
| 3484 | - "event_espresso"), $use_default_where_conditions, |
|
| 3485 | - implode(", ", $allowed_used_default_where_conditions_values))); |
|
| 3486 | - } |
|
| 3487 | - $universal_query_params = array(); |
|
| 3488 | - if ($this->_should_use_default_where_conditions( $use_default_where_conditions, true)) { |
|
| 3489 | - $universal_query_params = $this->_get_default_where_conditions(); |
|
| 3490 | - } else if ($this->_should_use_minimum_where_conditions( $use_default_where_conditions, true)) { |
|
| 3491 | - $universal_query_params = $this->_get_minimum_where_conditions(); |
|
| 3492 | - } |
|
| 3493 | - foreach ($query_info_carrier->get_model_names_included() as $model_relation_path => $model_name) { |
|
| 3494 | - $related_model = $this->get_related_model_obj($model_name); |
|
| 3495 | - if ( $this->_should_use_default_where_conditions( $use_default_where_conditions, false)) { |
|
| 3496 | - $related_model_universal_where_params = $related_model->_get_default_where_conditions($model_relation_path); |
|
| 3497 | - } elseif ($this->_should_use_minimum_where_conditions( $use_default_where_conditions, false)) { |
|
| 3498 | - $related_model_universal_where_params = $related_model->_get_minimum_where_conditions($model_relation_path); |
|
| 3499 | - } else { |
|
| 3500 | - //we don't want to add full or even minimum default where conditions from this model, so just continue |
|
| 3501 | - continue; |
|
| 3502 | - } |
|
| 3503 | - $overrides = $this->_override_defaults_or_make_null_friendly( |
|
| 3504 | - $related_model_universal_where_params, |
|
| 3505 | - $where_query_params, |
|
| 3506 | - $related_model, |
|
| 3507 | - $model_relation_path |
|
| 3508 | - ); |
|
| 3509 | - $universal_query_params = EEH_Array::merge_arrays_and_overwrite_keys( |
|
| 3510 | - $universal_query_params, |
|
| 3511 | - $overrides |
|
| 3512 | - ); |
|
| 3513 | - } |
|
| 3514 | - return $universal_query_params; |
|
| 3515 | - } |
|
| 3516 | - |
|
| 3517 | - |
|
| 3518 | - |
|
| 3519 | - /** |
|
| 3520 | - * Determines whether or not we should use default where conditions for the model in question |
|
| 3521 | - * (this model, or other related models). |
|
| 3522 | - * Basically, we should use default where conditions on this model if they have requested to use them on all models, |
|
| 3523 | - * this model only, or to use minimum where conditions on all other models and normal where conditions on this one. |
|
| 3524 | - * We should use default where conditions on related models when they requested to use default where conditions |
|
| 3525 | - * on all models, or specifically just on other related models |
|
| 3526 | - * @param $default_where_conditions_value |
|
| 3527 | - * @param bool $for_this_model false means this is for OTHER related models |
|
| 3528 | - * @return bool |
|
| 3529 | - */ |
|
| 3530 | - private function _should_use_default_where_conditions( $default_where_conditions_value, $for_this_model = true ) |
|
| 3531 | - { |
|
| 3532 | - return ( |
|
| 3533 | - $for_this_model |
|
| 3534 | - && in_array( |
|
| 3535 | - $default_where_conditions_value, |
|
| 3536 | - array( |
|
| 3537 | - EEM_Base::default_where_conditions_all, |
|
| 3538 | - EEM_Base::default_where_conditions_this_only, |
|
| 3539 | - EEM_Base::default_where_conditions_minimum_others, |
|
| 3540 | - ), |
|
| 3541 | - true |
|
| 3542 | - ) |
|
| 3543 | - ) |
|
| 3544 | - || ( |
|
| 3545 | - ! $for_this_model |
|
| 3546 | - && in_array( |
|
| 3547 | - $default_where_conditions_value, |
|
| 3548 | - array( |
|
| 3549 | - EEM_Base::default_where_conditions_all, |
|
| 3550 | - EEM_Base::default_where_conditions_others_only, |
|
| 3551 | - ), |
|
| 3552 | - true |
|
| 3553 | - ) |
|
| 3554 | - ); |
|
| 3555 | - } |
|
| 3556 | - |
|
| 3557 | - /** |
|
| 3558 | - * Determines whether or not we should use default minimum conditions for the model in question |
|
| 3559 | - * (this model, or other related models). |
|
| 3560 | - * Basically, we should use minimum where conditions on this model only if they requested all models to use minimum |
|
| 3561 | - * where conditions. |
|
| 3562 | - * We should use minimum where conditions on related models if they requested to use minimum where conditions |
|
| 3563 | - * on this model or others |
|
| 3564 | - * @param $default_where_conditions_value |
|
| 3565 | - * @param bool $for_this_model false means this is for OTHER related models |
|
| 3566 | - * @return bool |
|
| 3567 | - */ |
|
| 3568 | - private function _should_use_minimum_where_conditions($default_where_conditions_value, $for_this_model = true) |
|
| 3569 | - { |
|
| 3570 | - return ( |
|
| 3571 | - $for_this_model |
|
| 3572 | - && $default_where_conditions_value === EEM_Base::default_where_conditions_minimum_all |
|
| 3573 | - ) |
|
| 3574 | - || ( |
|
| 3575 | - ! $for_this_model |
|
| 3576 | - && in_array( |
|
| 3577 | - $default_where_conditions_value, |
|
| 3578 | - array( |
|
| 3579 | - EEM_Base::default_where_conditions_minimum_others, |
|
| 3580 | - EEM_Base::default_where_conditions_minimum_all, |
|
| 3581 | - ), |
|
| 3582 | - true |
|
| 3583 | - ) |
|
| 3584 | - ); |
|
| 3585 | - } |
|
| 3586 | - |
|
| 3587 | - |
|
| 3588 | - /** |
|
| 3589 | - * Checks if any of the defaults have been overridden. If there are any that AREN'T overridden, |
|
| 3590 | - * then we also add a special where condition which allows for that model's primary key |
|
| 3591 | - * to be null (which is important for JOINs. Eg, if you want to see all Events ordered by Venue's name, |
|
| 3592 | - * then Event's with NO Venue won't appear unless you allow VNU_ID to be NULL) |
|
| 3593 | - * |
|
| 3594 | - * @param array $default_where_conditions |
|
| 3595 | - * @param array $provided_where_conditions |
|
| 3596 | - * @param EEM_Base $model |
|
| 3597 | - * @param string $model_relation_path like 'Transaction.Payment.' |
|
| 3598 | - * @return array like EEM_Base::get_all's $query_params[0] |
|
| 3599 | - * @throws \EE_Error |
|
| 3600 | - */ |
|
| 3601 | - private function _override_defaults_or_make_null_friendly( |
|
| 3602 | - $default_where_conditions, |
|
| 3603 | - $provided_where_conditions, |
|
| 3604 | - $model, |
|
| 3605 | - $model_relation_path |
|
| 3606 | - ) { |
|
| 3607 | - $null_friendly_where_conditions = array(); |
|
| 3608 | - $none_overridden = true; |
|
| 3609 | - $or_condition_key_for_defaults = 'OR*' . get_class($model); |
|
| 3610 | - foreach ($default_where_conditions as $key => $val) { |
|
| 3611 | - if (isset($provided_where_conditions[$key])) { |
|
| 3612 | - $none_overridden = false; |
|
| 3613 | - } else { |
|
| 3614 | - $null_friendly_where_conditions[$or_condition_key_for_defaults]['AND'][$key] = $val; |
|
| 3615 | - } |
|
| 3616 | - } |
|
| 3617 | - if ($none_overridden && $default_where_conditions) { |
|
| 3618 | - if ($model->has_primary_key_field()) { |
|
| 3619 | - $null_friendly_where_conditions[$or_condition_key_for_defaults][$model_relation_path |
|
| 3620 | - . "." |
|
| 3621 | - . $model->primary_key_name()] = array('IS NULL'); |
|
| 3622 | - }/*else{ |
|
| 32 | + /** |
|
| 33 | + * Flag to indicate whether the values provided to EEM_Base have already been prepared |
|
| 34 | + * by the model object or not (ie, the model object has used the field's _prepare_for_set function on the values). |
|
| 35 | + * They almost always WILL NOT, but it's not necessarily a requirement. |
|
| 36 | + * For example, if you want to run EEM_Event::instance()->get_all(array(array('EVT_ID'=>$_GET['event_id']))); |
|
| 37 | + * |
|
| 38 | + * @var boolean |
|
| 39 | + */ |
|
| 40 | + private $_values_already_prepared_by_model_object = 0; |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * when $_values_already_prepared_by_model_object equals this, we assume |
|
| 44 | + * the data is just like form input that needs to have the model fields' |
|
| 45 | + * prepare_for_set and prepare_for_use_in_db called on it |
|
| 46 | + */ |
|
| 47 | + const not_prepared_by_model_object = 0; |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * when $_values_already_prepared_by_model_object equals this, we |
|
| 51 | + * assume this value is coming from a model object and doesn't need to have |
|
| 52 | + * prepare_for_set called on it, just prepare_for_use_in_db is used |
|
| 53 | + */ |
|
| 54 | + const prepared_by_model_object = 1; |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * when $_values_already_prepared_by_model_object equals this, we assume |
|
| 58 | + * the values are already to be used in the database (ie no processing is done |
|
| 59 | + * on them by the model's fields) |
|
| 60 | + */ |
|
| 61 | + const prepared_for_use_in_db = 2; |
|
| 62 | + |
|
| 63 | + |
|
| 64 | + protected $singular_item = 'Item'; |
|
| 65 | + |
|
| 66 | + protected $plural_item = 'Items'; |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * @type \EE_Table_Base[] $_tables array of EE_Table objects for defining which tables comprise this model. |
|
| 70 | + */ |
|
| 71 | + protected $_tables; |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * with two levels: top-level has array keys which are database table aliases (ie, keys in _tables) |
|
| 75 | + * and the value is an array. Each of those sub-arrays have keys of field names (eg 'ATT_ID', which should also be |
|
| 76 | + * variable names on the model objects (eg, EE_Attendee), and the keys should be children of EE_Model_Field |
|
| 77 | + * |
|
| 78 | + * @var \EE_Model_Field_Base[][] $_fields |
|
| 79 | + */ |
|
| 80 | + protected $_fields; |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * array of different kinds of relations |
|
| 84 | + * |
|
| 85 | + * @var \EE_Model_Relation_Base[] $_model_relations |
|
| 86 | + */ |
|
| 87 | + protected $_model_relations; |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * @var \EE_Index[] $_indexes |
|
| 91 | + */ |
|
| 92 | + protected $_indexes = array(); |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * Default strategy for getting where conditions on this model. This strategy is used to get default |
|
| 96 | + * where conditions which are added to get_all, update, and delete queries. They can be overridden |
|
| 97 | + * by setting the same columns as used in these queries in the query yourself. |
|
| 98 | + * |
|
| 99 | + * @var EE_Default_Where_Conditions |
|
| 100 | + */ |
|
| 101 | + protected $_default_where_conditions_strategy; |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * Strategy for getting conditions on this model when 'default_where_conditions' equals 'minimum'. |
|
| 105 | + * This is particularly useful when you want something between 'none' and 'default' |
|
| 106 | + * |
|
| 107 | + * @var EE_Default_Where_Conditions |
|
| 108 | + */ |
|
| 109 | + protected $_minimum_where_conditions_strategy; |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * String describing how to find the "owner" of this model's objects. |
|
| 113 | + * When there is a foreign key on this model to the wp_users table, this isn't needed. |
|
| 114 | + * But when there isn't, this indicates which related model, or transiently-related model, |
|
| 115 | + * has the foreign key to the wp_users table. |
|
| 116 | + * Eg, for EEM_Registration this would be 'Event' because registrations are directly |
|
| 117 | + * related to events, and events have a foreign key to wp_users. |
|
| 118 | + * On EEM_Transaction, this would be 'Transaction.Event' |
|
| 119 | + * |
|
| 120 | + * @var string |
|
| 121 | + */ |
|
| 122 | + protected $_model_chain_to_wp_user = ''; |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * This is a flag typically set by updates so that we don't load the where strategy on updates because updates |
|
| 126 | + * don't need it (particularly CPT models) |
|
| 127 | + * |
|
| 128 | + * @var bool |
|
| 129 | + */ |
|
| 130 | + protected $_ignore_where_strategy = false; |
|
| 131 | + |
|
| 132 | + /** |
|
| 133 | + * String used in caps relating to this model. Eg, if the caps relating to this |
|
| 134 | + * model are 'ee_edit_events', 'ee_read_events', etc, it would be 'events'. |
|
| 135 | + * |
|
| 136 | + * @var string. If null it hasn't been initialized yet. If false then we |
|
| 137 | + * have indicated capabilities don't apply to this |
|
| 138 | + */ |
|
| 139 | + protected $_caps_slug = null; |
|
| 140 | + |
|
| 141 | + /** |
|
| 142 | + * 2d array where top-level keys are one of EEM_Base::valid_cap_contexts(), |
|
| 143 | + * and next-level keys are capability names, and each's value is a |
|
| 144 | + * EE_Default_Where_Condition. If the requester requests to apply caps to the query, |
|
| 145 | + * they specify which context to use (ie, frontend, backend, edit or delete) |
|
| 146 | + * and then each capability in the corresponding sub-array that they're missing |
|
| 147 | + * adds the where conditions onto the query. |
|
| 148 | + * |
|
| 149 | + * @var array |
|
| 150 | + */ |
|
| 151 | + protected $_cap_restrictions = array( |
|
| 152 | + self::caps_read => array(), |
|
| 153 | + self::caps_read_admin => array(), |
|
| 154 | + self::caps_edit => array(), |
|
| 155 | + self::caps_delete => array(), |
|
| 156 | + ); |
|
| 157 | + |
|
| 158 | + /** |
|
| 159 | + * Array defining which cap restriction generators to use to create default |
|
| 160 | + * cap restrictions to put in EEM_Base::_cap_restrictions. |
|
| 161 | + * Array-keys are one of EEM_Base::valid_cap_contexts(), and values are a child of |
|
| 162 | + * EE_Restriction_Generator_Base. If you don't want any cap restrictions generated |
|
| 163 | + * automatically set this to false (not just null). |
|
| 164 | + * |
|
| 165 | + * @var EE_Restriction_Generator_Base[] |
|
| 166 | + */ |
|
| 167 | + protected $_cap_restriction_generators = array(); |
|
| 168 | + |
|
| 169 | + /** |
|
| 170 | + * constants used to categorize capability restrictions on EEM_Base::_caps_restrictions |
|
| 171 | + */ |
|
| 172 | + const caps_read = 'read'; |
|
| 173 | + |
|
| 174 | + const caps_read_admin = 'read_admin'; |
|
| 175 | + |
|
| 176 | + const caps_edit = 'edit'; |
|
| 177 | + |
|
| 178 | + const caps_delete = 'delete'; |
|
| 179 | + |
|
| 180 | + /** |
|
| 181 | + * Keys are all the cap contexts (ie constants EEM_Base::_caps_*) and values are their 'action' |
|
| 182 | + * as how they'd be used in capability names. Eg EEM_Base::caps_read ('read_frontend') |
|
| 183 | + * maps to 'read' because when looking for relevant permissions we're going to use |
|
| 184 | + * 'read' in teh capabilities names like 'ee_read_events' etc. |
|
| 185 | + * |
|
| 186 | + * @var array |
|
| 187 | + */ |
|
| 188 | + protected $_cap_contexts_to_cap_action_map = array( |
|
| 189 | + self::caps_read => 'read', |
|
| 190 | + self::caps_read_admin => 'read', |
|
| 191 | + self::caps_edit => 'edit', |
|
| 192 | + self::caps_delete => 'delete', |
|
| 193 | + ); |
|
| 194 | + |
|
| 195 | + /** |
|
| 196 | + * Timezone |
|
| 197 | + * This gets set via the constructor so that we know what timezone incoming strings|timestamps are in when there |
|
| 198 | + * are EE_Datetime_Fields in use. This can also be used before a get to set what timezone you want strings coming |
|
| 199 | + * out of the created objects. NOT all EEM_Base child classes use this property but any that use a |
|
| 200 | + * EE_Datetime_Field data type will have access to it. |
|
| 201 | + * |
|
| 202 | + * @var string |
|
| 203 | + */ |
|
| 204 | + protected $_timezone; |
|
| 205 | + |
|
| 206 | + |
|
| 207 | + /** |
|
| 208 | + * This holds the id of the blog currently making the query. Has no bearing on single site but is used for |
|
| 209 | + * multisite. |
|
| 210 | + * |
|
| 211 | + * @var int |
|
| 212 | + */ |
|
| 213 | + protected static $_model_query_blog_id; |
|
| 214 | + |
|
| 215 | + /** |
|
| 216 | + * A copy of _fields, except the array keys are the model names pointed to by |
|
| 217 | + * the field |
|
| 218 | + * |
|
| 219 | + * @var EE_Model_Field_Base[] |
|
| 220 | + */ |
|
| 221 | + private $_cache_foreign_key_to_fields = array(); |
|
| 222 | + |
|
| 223 | + /** |
|
| 224 | + * Cached list of all the fields on the model, indexed by their name |
|
| 225 | + * |
|
| 226 | + * @var EE_Model_Field_Base[] |
|
| 227 | + */ |
|
| 228 | + private $_cached_fields = null; |
|
| 229 | + |
|
| 230 | + /** |
|
| 231 | + * Cached list of all the fields on the model, except those that are |
|
| 232 | + * marked as only pertinent to the database |
|
| 233 | + * |
|
| 234 | + * @var EE_Model_Field_Base[] |
|
| 235 | + */ |
|
| 236 | + private $_cached_fields_non_db_only = null; |
|
| 237 | + |
|
| 238 | + /** |
|
| 239 | + * A cached reference to the primary key for quick lookup |
|
| 240 | + * |
|
| 241 | + * @var EE_Model_Field_Base |
|
| 242 | + */ |
|
| 243 | + private $_primary_key_field = null; |
|
| 244 | + |
|
| 245 | + /** |
|
| 246 | + * Flag indicating whether this model has a primary key or not |
|
| 247 | + * |
|
| 248 | + * @var boolean |
|
| 249 | + */ |
|
| 250 | + protected $_has_primary_key_field = null; |
|
| 251 | + |
|
| 252 | + /** |
|
| 253 | + * Whether or not this model is based off a table in WP core only (CPTs should set |
|
| 254 | + * this to FALSE, but if we were to make an EE_WP_Post model, it should set this to true). |
|
| 255 | + * This should be true for models that deal with data that should exist independent of EE. |
|
| 256 | + * For example, if the model can read and insert data that isn't used by EE, this should be true. |
|
| 257 | + * It would be false, however, if you could guarantee the model would only interact with EE data, |
|
| 258 | + * even if it uses a WP core table (eg event and venue models set this to false for that reason: |
|
| 259 | + * they can only read and insert events and venues custom post types, not arbitrary post types) |
|
| 260 | + * @var boolean |
|
| 261 | + */ |
|
| 262 | + protected $_wp_core_model = false; |
|
| 263 | + |
|
| 264 | + /** |
|
| 265 | + * List of valid operators that can be used for querying. |
|
| 266 | + * The keys are all operators we'll accept, the values are the real SQL |
|
| 267 | + * operators used |
|
| 268 | + * |
|
| 269 | + * @var array |
|
| 270 | + */ |
|
| 271 | + protected $_valid_operators = array( |
|
| 272 | + '=' => '=', |
|
| 273 | + '<=' => '<=', |
|
| 274 | + '<' => '<', |
|
| 275 | + '>=' => '>=', |
|
| 276 | + '>' => '>', |
|
| 277 | + '!=' => '!=', |
|
| 278 | + 'LIKE' => 'LIKE', |
|
| 279 | + 'like' => 'LIKE', |
|
| 280 | + 'NOT_LIKE' => 'NOT LIKE', |
|
| 281 | + 'not_like' => 'NOT LIKE', |
|
| 282 | + 'NOT LIKE' => 'NOT LIKE', |
|
| 283 | + 'not like' => 'NOT LIKE', |
|
| 284 | + 'IN' => 'IN', |
|
| 285 | + 'in' => 'IN', |
|
| 286 | + 'NOT_IN' => 'NOT IN', |
|
| 287 | + 'not_in' => 'NOT IN', |
|
| 288 | + 'NOT IN' => 'NOT IN', |
|
| 289 | + 'not in' => 'NOT IN', |
|
| 290 | + 'between' => 'BETWEEN', |
|
| 291 | + 'BETWEEN' => 'BETWEEN', |
|
| 292 | + 'IS_NOT_NULL' => 'IS NOT NULL', |
|
| 293 | + 'is_not_null' => 'IS NOT NULL', |
|
| 294 | + 'IS NOT NULL' => 'IS NOT NULL', |
|
| 295 | + 'is not null' => 'IS NOT NULL', |
|
| 296 | + 'IS_NULL' => 'IS NULL', |
|
| 297 | + 'is_null' => 'IS NULL', |
|
| 298 | + 'IS NULL' => 'IS NULL', |
|
| 299 | + 'is null' => 'IS NULL', |
|
| 300 | + 'REGEXP' => 'REGEXP', |
|
| 301 | + 'regexp' => 'REGEXP', |
|
| 302 | + 'NOT_REGEXP' => 'NOT REGEXP', |
|
| 303 | + 'not_regexp' => 'NOT REGEXP', |
|
| 304 | + 'NOT REGEXP' => 'NOT REGEXP', |
|
| 305 | + 'not regexp' => 'NOT REGEXP', |
|
| 306 | + ); |
|
| 307 | + |
|
| 308 | + /** |
|
| 309 | + * operators that work like 'IN', accepting a comma-separated list of values inside brackets. Eg '(1,2,3)' |
|
| 310 | + * |
|
| 311 | + * @var array |
|
| 312 | + */ |
|
| 313 | + protected $_in_style_operators = array('IN', 'NOT IN'); |
|
| 314 | + |
|
| 315 | + /** |
|
| 316 | + * operators that work like 'BETWEEN'. Typically used for datetime calculations, i.e. "BETWEEN '12-1-2011' AND |
|
| 317 | + * '12-31-2012'" |
|
| 318 | + * |
|
| 319 | + * @var array |
|
| 320 | + */ |
|
| 321 | + protected $_between_style_operators = array('BETWEEN'); |
|
| 322 | + |
|
| 323 | + /** |
|
| 324 | + * Operators that work like SQL's like: input should be assumed to be a string, already prepared for a LIKE query. |
|
| 325 | + * @var array |
|
| 326 | + */ |
|
| 327 | + protected $_like_style_operators = array('LIKE', 'NOT LIKE'); |
|
| 328 | + /** |
|
| 329 | + * operators that are used for handling NUll and !NULL queries. Typically used for when checking if a row exists |
|
| 330 | + * on a join table. |
|
| 331 | + * |
|
| 332 | + * @var array |
|
| 333 | + */ |
|
| 334 | + protected $_null_style_operators = array('IS NOT NULL', 'IS NULL'); |
|
| 335 | + |
|
| 336 | + /** |
|
| 337 | + * Allowed values for $query_params['order'] for ordering in queries |
|
| 338 | + * |
|
| 339 | + * @var array |
|
| 340 | + */ |
|
| 341 | + protected $_allowed_order_values = array('asc', 'desc', 'ASC', 'DESC'); |
|
| 342 | + |
|
| 343 | + /** |
|
| 344 | + * When these are keys in a WHERE or HAVING clause, they are handled much differently |
|
| 345 | + * than regular field names. It is assumed that their values are an array of WHERE conditions |
|
| 346 | + * |
|
| 347 | + * @var array |
|
| 348 | + */ |
|
| 349 | + private $_logic_query_param_keys = array('not', 'and', 'or', 'NOT', 'AND', 'OR'); |
|
| 350 | + |
|
| 351 | + /** |
|
| 352 | + * Allowed keys in $query_params arrays passed into queries. Note that 0 is meant to always be a |
|
| 353 | + * 'where', but 'where' clauses are so common that we thought we'd omit it |
|
| 354 | + * |
|
| 355 | + * @var array |
|
| 356 | + */ |
|
| 357 | + private $_allowed_query_params = array( |
|
| 358 | + 0, |
|
| 359 | + 'limit', |
|
| 360 | + 'order_by', |
|
| 361 | + 'group_by', |
|
| 362 | + 'having', |
|
| 363 | + 'force_join', |
|
| 364 | + 'order', |
|
| 365 | + 'on_join_limit', |
|
| 366 | + 'default_where_conditions', |
|
| 367 | + 'caps', |
|
| 368 | + ); |
|
| 369 | + |
|
| 370 | + /** |
|
| 371 | + * All the data types that can be used in $wpdb->prepare statements. |
|
| 372 | + * |
|
| 373 | + * @var array |
|
| 374 | + */ |
|
| 375 | + private $_valid_wpdb_data_types = array('%d', '%s', '%f'); |
|
| 376 | + |
|
| 377 | + /** |
|
| 378 | + * EE_Registry Object |
|
| 379 | + * |
|
| 380 | + * @var object |
|
| 381 | + * @access protected |
|
| 382 | + */ |
|
| 383 | + protected $EE = null; |
|
| 384 | + |
|
| 385 | + |
|
| 386 | + /** |
|
| 387 | + * Property which, when set, will have this model echo out the next X queries to the page for debugging. |
|
| 388 | + * |
|
| 389 | + * @var int |
|
| 390 | + */ |
|
| 391 | + protected $_show_next_x_db_queries = 0; |
|
| 392 | + |
|
| 393 | + /** |
|
| 394 | + * When using _get_all_wpdb_results, you can specify a custom selection. If you do so, |
|
| 395 | + * it gets saved on this property so those selections can be used in WHERE, GROUP_BY, etc. |
|
| 396 | + * |
|
| 397 | + * @var array |
|
| 398 | + */ |
|
| 399 | + protected $_custom_selections = array(); |
|
| 400 | + |
|
| 401 | + /** |
|
| 402 | + * key => value Entity Map using array( EEM_Base::$_model_query_blog_id => array( ID => model object ) ) |
|
| 403 | + * caches every model object we've fetched from the DB on this request |
|
| 404 | + * |
|
| 405 | + * @var array |
|
| 406 | + */ |
|
| 407 | + protected $_entity_map; |
|
| 408 | + |
|
| 409 | + /** |
|
| 410 | + * constant used to show EEM_Base has not yet verified the db on this http request |
|
| 411 | + */ |
|
| 412 | + const db_verified_none = 0; |
|
| 413 | + |
|
| 414 | + /** |
|
| 415 | + * constant used to show EEM_Base has verified the EE core db on this http request, |
|
| 416 | + * but not the addons' dbs |
|
| 417 | + */ |
|
| 418 | + const db_verified_core = 1; |
|
| 419 | + |
|
| 420 | + /** |
|
| 421 | + * constant used to show EEM_Base has verified the addons' dbs (and implicitly |
|
| 422 | + * the EE core db too) |
|
| 423 | + */ |
|
| 424 | + const db_verified_addons = 2; |
|
| 425 | + |
|
| 426 | + /** |
|
| 427 | + * indicates whether an EEM_Base child has already re-verified the DB |
|
| 428 | + * is ok (we don't want to do it repetitively). Should be set to one the constants |
|
| 429 | + * looking like EEM_Base::db_verified_* |
|
| 430 | + * |
|
| 431 | + * @var int - 0 = none, 1 = core, 2 = addons |
|
| 432 | + */ |
|
| 433 | + protected static $_db_verification_level = EEM_Base::db_verified_none; |
|
| 434 | + |
|
| 435 | + /** |
|
| 436 | + * @const constant for 'default_where_conditions' to apply default where conditions to ALL queried models |
|
| 437 | + * (eg, if retrieving registrations ordered by their datetimes, this will only return non-trashed |
|
| 438 | + * registrations for non-trashed tickets for non-trashed datetimes) |
|
| 439 | + */ |
|
| 440 | + const default_where_conditions_all = 'all'; |
|
| 441 | + |
|
| 442 | + /** |
|
| 443 | + * @const constant for 'default_where_conditions' to apply default where conditions to THIS model only, but |
|
| 444 | + * no other models which are joined to (eg, if retrieving registrations ordered by their datetimes, this will |
|
| 445 | + * return non-trashed registrations, regardless of the related datetimes and tickets' statuses). |
|
| 446 | + * It is preferred to use EEM_Base::default_where_conditions_minimum_others because, when joining to |
|
| 447 | + * models which share tables with other models, this can return data for the wrong model. |
|
| 448 | + */ |
|
| 449 | + const default_where_conditions_this_only = 'this_model_only'; |
|
| 450 | + |
|
| 451 | + /** |
|
| 452 | + * @const constant for 'default_where_conditions' to apply default where conditions to other models queried, |
|
| 453 | + * but not the current model (eg, if retrieving registrations ordered by their datetimes, this will |
|
| 454 | + * return all registrations related to non-trashed tickets and non-trashed datetimes) |
|
| 455 | + */ |
|
| 456 | + const default_where_conditions_others_only = 'other_models_only'; |
|
| 457 | + |
|
| 458 | + /** |
|
| 459 | + * @const constant for 'default_where_conditions' to apply minimum where conditions to all models queried. |
|
| 460 | + * For most models this the same as EEM_Base::default_where_conditions_none, except for models which share |
|
| 461 | + * their table with other models, like the Event and Venue models. For example, when querying for events |
|
| 462 | + * ordered by their venues' name, this will be sure to only return real events with associated real venues |
|
| 463 | + * (regardless of whether those events and venues are trashed) |
|
| 464 | + * In contrast, using EEM_Base::default_where_conditions_none would could return WP posts other than EE |
|
| 465 | + * events. |
|
| 466 | + */ |
|
| 467 | + const default_where_conditions_minimum_all = 'minimum'; |
|
| 468 | + |
|
| 469 | + /** |
|
| 470 | + * @const constant for 'default_where_conditions' to apply apply where conditions to other models, and full default |
|
| 471 | + * where conditions for the queried model (eg, when querying events ordered by venues' names, this will |
|
| 472 | + * return non-trashed events for any venues, regardless of whether those associated venues are trashed or |
|
| 473 | + * not) |
|
| 474 | + */ |
|
| 475 | + const default_where_conditions_minimum_others = 'full_this_minimum_others'; |
|
| 476 | + |
|
| 477 | + /** |
|
| 478 | + * @const constant for 'default_where_conditions' to NOT apply any where conditions. This should very rarely be |
|
| 479 | + * used, because when querying from a model which shares its table with another model (eg Events and Venues) |
|
| 480 | + * it's possible it will return table entries for other models. You should use |
|
| 481 | + * EEM_Base::default_where_conditions_minimum_all instead. |
|
| 482 | + */ |
|
| 483 | + const default_where_conditions_none = 'none'; |
|
| 484 | + |
|
| 485 | + |
|
| 486 | + |
|
| 487 | + /** |
|
| 488 | + * About all child constructors: |
|
| 489 | + * they should define the _tables, _fields and _model_relations arrays. |
|
| 490 | + * Should ALWAYS be called after child constructor. |
|
| 491 | + * In order to make the child constructors to be as simple as possible, this parent constructor |
|
| 492 | + * finalizes constructing all the object's attributes. |
|
| 493 | + * Generally, rather than requiring a child to code |
|
| 494 | + * $this->_tables = array( |
|
| 495 | + * 'Event_Post_Table' => new EE_Table('Event_Post_Table','wp_posts') |
|
| 496 | + * ...); |
|
| 497 | + * (thus repeating itself in the array key and in the constructor of the new EE_Table,) |
|
| 498 | + * each EE_Table has a function to set the table's alias after the constructor, using |
|
| 499 | + * the array key ('Event_Post_Table'), instead of repeating it. The model fields and model relations |
|
| 500 | + * do something similar. |
|
| 501 | + * |
|
| 502 | + * @param null $timezone |
|
| 503 | + * @throws \EE_Error |
|
| 504 | + */ |
|
| 505 | + protected function __construct($timezone = null) |
|
| 506 | + { |
|
| 507 | + // check that the model has not been loaded too soon |
|
| 508 | + if (! did_action('AHEE__EE_System__load_espresso_addons')) { |
|
| 509 | + throw new EE_Error ( |
|
| 510 | + sprintf( |
|
| 511 | + __('The %1$s model can not be loaded before the "AHEE__EE_System__load_espresso_addons" hook has been called. This gives other addons a chance to extend this model.', |
|
| 512 | + 'event_espresso'), |
|
| 513 | + get_class($this) |
|
| 514 | + ) |
|
| 515 | + ); |
|
| 516 | + } |
|
| 517 | + /** |
|
| 518 | + * Set blogid for models to current blog. However we ONLY do this if $_model_query_blog_id is not already set. |
|
| 519 | + */ |
|
| 520 | + if (empty(EEM_Base::$_model_query_blog_id)) { |
|
| 521 | + EEM_Base::set_model_query_blog_id(); |
|
| 522 | + } |
|
| 523 | + /** |
|
| 524 | + * Filters the list of tables on a model. It is best to NOT use this directly and instead |
|
| 525 | + * just use EE_Register_Model_Extension |
|
| 526 | + * |
|
| 527 | + * @var EE_Table_Base[] $_tables |
|
| 528 | + */ |
|
| 529 | + $this->_tables = apply_filters('FHEE__' . get_class($this) . '__construct__tables', $this->_tables); |
|
| 530 | + foreach ($this->_tables as $table_alias => $table_obj) { |
|
| 531 | + /** @var $table_obj EE_Table_Base */ |
|
| 532 | + $table_obj->_construct_finalize_with_alias($table_alias); |
|
| 533 | + if ($table_obj instanceof EE_Secondary_Table) { |
|
| 534 | + /** @var $table_obj EE_Secondary_Table */ |
|
| 535 | + $table_obj->_construct_finalize_set_table_to_join_with($this->_get_main_table()); |
|
| 536 | + } |
|
| 537 | + } |
|
| 538 | + /** |
|
| 539 | + * Filters the list of fields on a model. It is best to NOT use this directly and instead just use |
|
| 540 | + * EE_Register_Model_Extension |
|
| 541 | + * |
|
| 542 | + * @param EE_Model_Field_Base[] $_fields |
|
| 543 | + */ |
|
| 544 | + $this->_fields = apply_filters('FHEE__' . get_class($this) . '__construct__fields', $this->_fields); |
|
| 545 | + $this->_invalidate_field_caches(); |
|
| 546 | + foreach ($this->_fields as $table_alias => $fields_for_table) { |
|
| 547 | + if (! array_key_exists($table_alias, $this->_tables)) { |
|
| 548 | + throw new EE_Error(sprintf(__("Table alias %s does not exist in EEM_Base child's _tables array. Only tables defined are %s", |
|
| 549 | + 'event_espresso'), $table_alias, implode(",", $this->_fields))); |
|
| 550 | + } |
|
| 551 | + foreach ($fields_for_table as $field_name => $field_obj) { |
|
| 552 | + /** @var $field_obj EE_Model_Field_Base | EE_Primary_Key_Field_Base */ |
|
| 553 | + //primary key field base has a slightly different _construct_finalize |
|
| 554 | + /** @var $field_obj EE_Model_Field_Base */ |
|
| 555 | + $field_obj->_construct_finalize($table_alias, $field_name, $this->get_this_model_name()); |
|
| 556 | + } |
|
| 557 | + } |
|
| 558 | + // everything is related to Extra_Meta |
|
| 559 | + if (get_class($this) !== 'EEM_Extra_Meta') { |
|
| 560 | + //make extra meta related to everything, but don't block deleting things just |
|
| 561 | + //because they have related extra meta info. For now just orphan those extra meta |
|
| 562 | + //in the future we should automatically delete them |
|
| 563 | + $this->_model_relations['Extra_Meta'] = new EE_Has_Many_Any_Relation(false); |
|
| 564 | + } |
|
| 565 | + //and change logs |
|
| 566 | + if (get_class($this) !== 'EEM_Change_Log') { |
|
| 567 | + $this->_model_relations['Change_Log'] = new EE_Has_Many_Any_Relation(false); |
|
| 568 | + } |
|
| 569 | + /** |
|
| 570 | + * Filters the list of relations on a model. It is best to NOT use this directly and instead just use |
|
| 571 | + * EE_Register_Model_Extension |
|
| 572 | + * |
|
| 573 | + * @param EE_Model_Relation_Base[] $_model_relations |
|
| 574 | + */ |
|
| 575 | + $this->_model_relations = apply_filters('FHEE__' . get_class($this) . '__construct__model_relations', |
|
| 576 | + $this->_model_relations); |
|
| 577 | + foreach ($this->_model_relations as $model_name => $relation_obj) { |
|
| 578 | + /** @var $relation_obj EE_Model_Relation_Base */ |
|
| 579 | + $relation_obj->_construct_finalize_set_models($this->get_this_model_name(), $model_name); |
|
| 580 | + } |
|
| 581 | + foreach ($this->_indexes as $index_name => $index_obj) { |
|
| 582 | + /** @var $index_obj EE_Index */ |
|
| 583 | + $index_obj->_construct_finalize($index_name, $this->get_this_model_name()); |
|
| 584 | + } |
|
| 585 | + $this->set_timezone($timezone); |
|
| 586 | + //finalize default where condition strategy, or set default |
|
| 587 | + if (! $this->_default_where_conditions_strategy) { |
|
| 588 | + //nothing was set during child constructor, so set default |
|
| 589 | + $this->_default_where_conditions_strategy = new EE_Default_Where_Conditions(); |
|
| 590 | + } |
|
| 591 | + $this->_default_where_conditions_strategy->_finalize_construct($this); |
|
| 592 | + if (! $this->_minimum_where_conditions_strategy) { |
|
| 593 | + //nothing was set during child constructor, so set default |
|
| 594 | + $this->_minimum_where_conditions_strategy = new EE_Default_Where_Conditions(); |
|
| 595 | + } |
|
| 596 | + $this->_minimum_where_conditions_strategy->_finalize_construct($this); |
|
| 597 | + //if the cap slug hasn't been set, and we haven't set it to false on purpose |
|
| 598 | + //to indicate to NOT set it, set it to the logical default |
|
| 599 | + if ($this->_caps_slug === null) { |
|
| 600 | + $this->_caps_slug = EEH_Inflector::pluralize_and_lower($this->get_this_model_name()); |
|
| 601 | + } |
|
| 602 | + //initialize the standard cap restriction generators if none were specified by the child constructor |
|
| 603 | + if ($this->_cap_restriction_generators !== false) { |
|
| 604 | + foreach ($this->cap_contexts_to_cap_action_map() as $cap_context => $action) { |
|
| 605 | + if (! isset($this->_cap_restriction_generators[$cap_context])) { |
|
| 606 | + $this->_cap_restriction_generators[$cap_context] = apply_filters( |
|
| 607 | + 'FHEE__EEM_Base___construct__standard_cap_restriction_generator', |
|
| 608 | + new EE_Restriction_Generator_Protected(), |
|
| 609 | + $cap_context, |
|
| 610 | + $this |
|
| 611 | + ); |
|
| 612 | + } |
|
| 613 | + } |
|
| 614 | + } |
|
| 615 | + //if there are cap restriction generators, use them to make the default cap restrictions |
|
| 616 | + if ($this->_cap_restriction_generators !== false) { |
|
| 617 | + foreach ($this->_cap_restriction_generators as $context => $generator_object) { |
|
| 618 | + if (! $generator_object) { |
|
| 619 | + continue; |
|
| 620 | + } |
|
| 621 | + if (! $generator_object instanceof EE_Restriction_Generator_Base) { |
|
| 622 | + throw new EE_Error( |
|
| 623 | + sprintf( |
|
| 624 | + __('Index "%1$s" in the model %2$s\'s _cap_restriction_generators is not a child of EE_Restriction_Generator_Base. It should be that or NULL.', |
|
| 625 | + 'event_espresso'), |
|
| 626 | + $context, |
|
| 627 | + $this->get_this_model_name() |
|
| 628 | + ) |
|
| 629 | + ); |
|
| 630 | + } |
|
| 631 | + $action = $this->cap_action_for_context($context); |
|
| 632 | + if (! $generator_object->construction_finalized()) { |
|
| 633 | + $generator_object->_construct_finalize($this, $action); |
|
| 634 | + } |
|
| 635 | + } |
|
| 636 | + } |
|
| 637 | + do_action('AHEE__' . get_class($this) . '__construct__end'); |
|
| 638 | + } |
|
| 639 | + |
|
| 640 | + |
|
| 641 | + |
|
| 642 | + /** |
|
| 643 | + * Used to set the $_model_query_blog_id static property. |
|
| 644 | + * |
|
| 645 | + * @param int $blog_id If provided then will set the blog_id for the models to this id. If not provided then the |
|
| 646 | + * value for get_current_blog_id() will be used. |
|
| 647 | + */ |
|
| 648 | + public static function set_model_query_blog_id($blog_id = 0) |
|
| 649 | + { |
|
| 650 | + EEM_Base::$_model_query_blog_id = $blog_id > 0 ? (int)$blog_id : get_current_blog_id(); |
|
| 651 | + } |
|
| 652 | + |
|
| 653 | + |
|
| 654 | + |
|
| 655 | + /** |
|
| 656 | + * Returns whatever is set as the internal $model_query_blog_id. |
|
| 657 | + * |
|
| 658 | + * @return int |
|
| 659 | + */ |
|
| 660 | + public static function get_model_query_blog_id() |
|
| 661 | + { |
|
| 662 | + return EEM_Base::$_model_query_blog_id; |
|
| 663 | + } |
|
| 664 | + |
|
| 665 | + |
|
| 666 | + |
|
| 667 | + /** |
|
| 668 | + * This function is a singleton method used to instantiate the Espresso_model object |
|
| 669 | + * |
|
| 670 | + * @access public |
|
| 671 | + * @param string $timezone string representing the timezone we want to set for returned Date Time Strings (and any |
|
| 672 | + * incoming timezone data that gets saved). Note this just sends the timezone info to the |
|
| 673 | + * date time model field objects. Default is NULL (and will be assumed using the set |
|
| 674 | + * timezone in the 'timezone_string' wp option) |
|
| 675 | + * @return static (as in the concrete child class) |
|
| 676 | + */ |
|
| 677 | + public static function instance($timezone = null) |
|
| 678 | + { |
|
| 679 | + // check if instance of Espresso_model already exists |
|
| 680 | + if (! static::$_instance instanceof static) { |
|
| 681 | + // instantiate Espresso_model |
|
| 682 | + static::$_instance = new static($timezone); |
|
| 683 | + } |
|
| 684 | + //we might have a timezone set, let set_timezone decide what to do with it |
|
| 685 | + static::$_instance->set_timezone($timezone); |
|
| 686 | + // Espresso_model object |
|
| 687 | + return static::$_instance; |
|
| 688 | + } |
|
| 689 | + |
|
| 690 | + |
|
| 691 | + |
|
| 692 | + /** |
|
| 693 | + * resets the model and returns it |
|
| 694 | + * |
|
| 695 | + * @param null | string $timezone |
|
| 696 | + * @return EEM_Base|null (if the model was already instantiated, returns it, with |
|
| 697 | + * all its properties reset; if it wasn't instantiated, returns null) |
|
| 698 | + */ |
|
| 699 | + public static function reset($timezone = null) |
|
| 700 | + { |
|
| 701 | + if (static::$_instance instanceof EEM_Base) { |
|
| 702 | + //let's try to NOT swap out the current instance for a new one |
|
| 703 | + //because if someone has a reference to it, we can't remove their reference |
|
| 704 | + //so it's best to keep using the same reference, but change the original object |
|
| 705 | + //reset all its properties to their original values as defined in the class |
|
| 706 | + $r = new ReflectionClass(get_class(static::$_instance)); |
|
| 707 | + $static_properties = $r->getStaticProperties(); |
|
| 708 | + foreach ($r->getDefaultProperties() as $property => $value) { |
|
| 709 | + //don't set instance to null like it was originally, |
|
| 710 | + //but it's static anyways, and we're ignoring static properties (for now at least) |
|
| 711 | + if (! isset($static_properties[$property])) { |
|
| 712 | + static::$_instance->{$property} = $value; |
|
| 713 | + } |
|
| 714 | + } |
|
| 715 | + //and then directly call its constructor again, like we would if we |
|
| 716 | + //were creating a new one |
|
| 717 | + static::$_instance->__construct($timezone); |
|
| 718 | + return self::instance(); |
|
| 719 | + } |
|
| 720 | + return null; |
|
| 721 | + } |
|
| 722 | + |
|
| 723 | + |
|
| 724 | + |
|
| 725 | + /** |
|
| 726 | + * retrieve the status details from esp_status table as an array IF this model has the status table as a relation. |
|
| 727 | + * |
|
| 728 | + * @param boolean $translated return localized strings or JUST the array. |
|
| 729 | + * @return array |
|
| 730 | + * @throws \EE_Error |
|
| 731 | + */ |
|
| 732 | + public function status_array($translated = false) |
|
| 733 | + { |
|
| 734 | + if (! array_key_exists('Status', $this->_model_relations)) { |
|
| 735 | + return array(); |
|
| 736 | + } |
|
| 737 | + $model_name = $this->get_this_model_name(); |
|
| 738 | + $status_type = str_replace(' ', '_', strtolower(str_replace('_', ' ', $model_name))); |
|
| 739 | + $stati = EEM_Status::instance()->get_all(array(array('STS_type' => $status_type))); |
|
| 740 | + $status_array = array(); |
|
| 741 | + foreach ($stati as $status) { |
|
| 742 | + $status_array[$status->ID()] = $status->get('STS_code'); |
|
| 743 | + } |
|
| 744 | + return $translated |
|
| 745 | + ? EEM_Status::instance()->localized_status($status_array, false, 'sentence') |
|
| 746 | + : $status_array; |
|
| 747 | + } |
|
| 748 | + |
|
| 749 | + |
|
| 750 | + |
|
| 751 | + /** |
|
| 752 | + * Gets all the EE_Base_Class objects which match the $query_params, by querying the DB. |
|
| 753 | + * |
|
| 754 | + * @param array $query_params { |
|
| 755 | + * @var array $0 (where) array { |
|
| 756 | + * eg: array('QST_display_text'=>'Are you bob?','QST_admin_text'=>'Determine |
|
| 757 | + * if user is bob') becomes SQL >> "...WHERE QST_display_text = 'Are you |
|
| 758 | + * bob?' AND QST_admin_text = 'Determine if user is bob'...") To add WHERE |
|
| 759 | + * conditions based on related models (and even |
|
| 760 | + * models-related-to-related-models) prepend the model's name onto the field |
|
| 761 | + * name. Eg, |
|
| 762 | + * EEM_Event::instance()->get_all(array(array('Venue.VNU_ID'=>12))); becomes |
|
| 763 | + * SQL >> "SELECT * FROM wp_posts AS Event_CPT LEFT JOIN wp_esp_event_meta |
|
| 764 | + * AS Event_Meta ON Event_CPT.ID = Event_Meta.EVT_ID LEFT JOIN |
|
| 765 | + * wp_esp_event_venue AS Event_Venue ON Event_Venue.EVT_ID=Event_CPT.ID LEFT |
|
| 766 | + * JOIN wp_posts AS Venue_CPT ON Venue_CPT.ID=Event_Venue.VNU_ID LEFT JOIN |
|
| 767 | + * wp_esp_venue_meta AS Venue_Meta ON Venue_CPT.ID = Venue_Meta.VNU_ID WHERE |
|
| 768 | + * Venue_CPT.ID = 12 Notice that automatically took care of joining Events |
|
| 769 | + * to Venues (even when each of those models actually consisted of two |
|
| 770 | + * tables). Also, you may chain the model relations together. Eg instead of |
|
| 771 | + * just having |
|
| 772 | + * "Venue.VNU_ID", you could have |
|
| 773 | + * "Registration.Attendee.ATT_ID" as a field on a query for events (because |
|
| 774 | + * events are related to Registrations, which are related to Attendees). You |
|
| 775 | + * can take it even further with |
|
| 776 | + * "Registration.Transaction.Payment.PAY_amount" etc. To change the operator |
|
| 777 | + * (from the default of '='), change the value to an numerically-indexed |
|
| 778 | + * array, where the first item in the list is the operator. eg: array( |
|
| 779 | + * 'QST_display_text' => array('LIKE','%bob%'), 'QST_ID' => array('<',34), |
|
| 780 | + * 'QST_wp_user' => array('in',array(1,2,7,23))) becomes SQL >> "...WHERE |
|
| 781 | + * QST_display_text LIKE '%bob%' AND QST_ID < 34 AND QST_wp_user IN |
|
| 782 | + * (1,2,7,23)...". Valid operators so far: =, !=, <, <=, >, >=, LIKE, NOT |
|
| 783 | + * LIKE, IN (followed by numeric-indexed array), NOT IN (dido), BETWEEN |
|
| 784 | + * (followed by an array with exactly 2 date strings), IS NULL, and IS NOT |
|
| 785 | + * NULL Values can be a string, int, or float. They can also be arrays IFF |
|
| 786 | + * the operator is IN. Also, values can actually be field names. To indicate |
|
| 787 | + * the value is a field, simply provide a third array item (true) to the |
|
| 788 | + * operator-value array like so: eg: array( 'DTT_reg_limit' => array('>', |
|
| 789 | + * 'DTT_sold', TRUE) ) becomes SQL >> "...WHERE DTT_reg_limit > DTT_sold" |
|
| 790 | + * Note: you can also use related model field names like you would any other |
|
| 791 | + * field name. eg: |
|
| 792 | + * array('Datetime.DTT_reg_limit'=>array('=','Datetime.DTT_sold',TRUE) could |
|
| 793 | + * be used if you were querying EEM_Tickets (because Datetime is directly related to tickets) Also, by default all the where conditions are AND'd together. To override this, add an array key 'OR' (or 'AND') and the array to be OR'd together eg: array('OR'=>array('TXN_ID' => 23 , 'TXN_timestamp__>' => |
|
| 794 | + * 345678912)) becomes SQL >> "...WHERE TXN_ID = 23 OR TXN_timestamp = |
|
| 795 | + * 345678912...". Also, to negate an entire set of conditions, use 'NOT' as |
|
| 796 | + * an array key. eg: array('NOT'=>array('TXN_total' => |
|
| 797 | + * 50, 'TXN_paid'=>23) becomes SQL >> "...where ! (TXN_total =50 AND |
|
| 798 | + * TXN_paid =23) Note: the 'glue' used to join each condition will continue |
|
| 799 | + * to be what you last specified. IE, "AND"s by default, but if you had |
|
| 800 | + * previously specified to use ORs to join, ORs will continue to be used. |
|
| 801 | + * So, if you specify to use an "OR" to join conditions, it will continue to |
|
| 802 | + * "stick" until you specify an AND. eg |
|
| 803 | + * array('OR'=>array('NOT'=>array('TXN_total' => 50, |
|
| 804 | + * 'TXN_paid'=>23)),AND=>array('TXN_ID'=>1,'STS_ID'=>'TIN') becomes SQL >> |
|
| 805 | + * "...where ! (TXN_total =50 OR TXN_paid =23) AND TXN_ID=1 AND |
|
| 806 | + * STS_ID='TIN'" They can be nested indefinitely. eg: |
|
| 807 | + * array('OR'=>array('TXN_total' => 23, 'NOT'=> array( 'TXN_timestamp'=> 345678912, 'AND'=>array('TXN_paid' => 53, 'STS_ID' => 'TIN')))) becomes SQL >> "...WHERE TXN_total = 23 OR ! (TXN_timestamp = 345678912 OR (TXN_paid = 53 AND STS_ID = 'TIN'))..." GOTCHA: because this is an array, array keys must be unique, making it impossible to place two or more where conditions applying to the same field. eg: array('PAY_timestamp'=>array('>',$start_date),'PAY_timestamp'=>array('<',$end_date),'PAY_timestamp'=>array('!=',$special_date)), as PHP enforces that the array keys must be unique, thus removing the first two array entries with key 'PAY_timestamp'. becomes SQL >> "PAY_timestamp != 4234232", ignoring the first two PAY_timestamp conditions). To overcome this, you can add a '*' character to the end of the field's name, followed by anything. These will be removed when generating the SQL string, but allow for the array keys to be unique. eg: you could rewrite the previous query as: array('PAY_timestamp'=>array('>',$start_date),'PAY_timestamp*1st'=>array('<',$end_date),'PAY_timestamp*2nd'=>array('!=',$special_date)) which correctly becomes SQL >> |
|
| 808 | + * "PAY_timestamp > 123412341 AND PAY_timestamp < 2354235235234 AND |
|
| 809 | + * PAY_timestamp != 1241234123" This can be applied to condition operators |
|
| 810 | + * too, eg: |
|
| 811 | + * array('OR'=>array('REG_ID'=>3,'Transaction.TXN_ID'=>23),'OR*whatever'=>array('Attendee.ATT_fname'=>'bob','Attendee.ATT_lname'=>'wilson'))); |
|
| 812 | + * @var mixed $limit int|array adds a limit to the query just like the SQL limit clause, so |
|
| 813 | + * limits of "23", "25,50", and array(23,42) are all valid would become SQL |
|
| 814 | + * "...LIMIT 23", "...LIMIT 25,50", and "...LIMIT 23,42" respectively. |
|
| 815 | + * Remember when you provide two numbers for the limit, the 1st number is |
|
| 816 | + * the OFFSET, the 2nd is the LIMIT |
|
| 817 | + * @var array $on_join_limit allows the setting of a special select join with a internal limit so you |
|
| 818 | + * can do paging on one-to-many multi-table-joins. Send an array in the |
|
| 819 | + * following format array('on_join_limit' |
|
| 820 | + * => array( 'table_alias', array(1,2) ) ). |
|
| 821 | + * @var mixed $order_by name of a column to order by, or an array where keys are field names and |
|
| 822 | + * values are either 'ASC' or 'DESC'. |
|
| 823 | + * 'limit'=>array('STS_ID'=>'ASC','REG_date'=>'DESC'), which would becomes |
|
| 824 | + * SQL "...ORDER BY TXN_timestamp..." and "...ORDER BY STS_ID ASC, REG_date |
|
| 825 | + * DESC..." respectively. Like the |
|
| 826 | + * 'where' conditions, these fields can be on related models. Eg |
|
| 827 | + * 'order_by'=>array('Registration.Transaction.TXN_amount'=>'ASC') is |
|
| 828 | + * perfectly valid from any model related to 'Registration' (like Event, |
|
| 829 | + * Attendee, Price, Datetime, etc.) |
|
| 830 | + * @var string $order If 'order_by' is used and its value is a string (NOT an array), then |
|
| 831 | + * 'order' specifies whether to order the field specified in 'order_by' in |
|
| 832 | + * ascending or descending order. Acceptable values are 'ASC' or 'DESC'. If, |
|
| 833 | + * 'order_by' isn't used, but 'order' is, then it is assumed you want to |
|
| 834 | + * order by the primary key. Eg, |
|
| 835 | + * EEM_Event::instance()->get_all(array('order_by'=>'Datetime.DTT_EVT_start','order'=>'ASC'); |
|
| 836 | + * //(will join with the Datetime model's table(s) and order by its field |
|
| 837 | + * DTT_EVT_start) or |
|
| 838 | + * EEM_Registration::instance()->get_all(array('order'=>'ASC'));//will make |
|
| 839 | + * SQL "SELECT * FROM wp_esp_registration ORDER BY REG_ID ASC" |
|
| 840 | + * @var mixed $group_by name of field to order by, or an array of fields. Eg either |
|
| 841 | + * 'group_by'=>'VNU_ID', or |
|
| 842 | + * 'group_by'=>array('EVT_name','Registration.Transaction.TXN_total') Note: |
|
| 843 | + * if no |
|
| 844 | + * $group_by is specified, and a limit is set, automatically groups by the |
|
| 845 | + * model's primary key (or combined primary keys). This avoids some |
|
| 846 | + * weirdness that results when using limits, tons of joins, and no group by, |
|
| 847 | + * see https://events.codebasehq.com/projects/event-espresso/tickets/9389 |
|
| 848 | + * @var array $having exactly like WHERE parameters array, except these conditions apply to the |
|
| 849 | + * grouped results (whereas WHERE conditions apply to the pre-grouped |
|
| 850 | + * results) |
|
| 851 | + * @var array $force_join forces a join with the models named. Should be a numerically-indexed |
|
| 852 | + * array where values are models to be joined in the query.Eg |
|
| 853 | + * array('Attendee','Payment','Datetime'). You may join with transient |
|
| 854 | + * models using period, eg "Registration.Transaction.Payment". You will |
|
| 855 | + * probably only want to do this in hopes of increasing efficiency, as |
|
| 856 | + * related models which belongs to the current model |
|
| 857 | + * (ie, the current model has a foreign key to them, like how Registration |
|
| 858 | + * belongs to Attendee) can be cached in order to avoid future queries |
|
| 859 | + * @var string $default_where_conditions can be set to 'none', 'this_model_only', 'other_models_only', or 'all'. |
|
| 860 | + * set this to 'none' to disable all default where conditions. Eg, usually |
|
| 861 | + * soft-deleted objects are filtered-out if you want to include them, set |
|
| 862 | + * this query param to 'none'. If you want to ONLY disable THIS model's |
|
| 863 | + * default where conditions set it to 'other_models_only'. If you only want |
|
| 864 | + * this model's default where conditions added to the query, use |
|
| 865 | + * 'this_model_only'. If you want to use all default where conditions |
|
| 866 | + * (default), set to 'all'. |
|
| 867 | + * @var string $caps controls what capability requirements to apply to the query; ie, should |
|
| 868 | + * we just NOT apply any capabilities/permissions/restrictions and return |
|
| 869 | + * everything? Or should we only show the current user items they should be |
|
| 870 | + * able to view on the frontend, backend, edit, or delete? can be set to |
|
| 871 | + * 'none' (default), 'read_frontend', 'read_backend', 'edit' or 'delete' |
|
| 872 | + * } |
|
| 873 | + * @return EE_Base_Class[] *note that there is NO option to pass the output type. If you want results different |
|
| 874 | + * from EE_Base_Class[], use _get_all_wpdb_results()and make it public |
|
| 875 | + * again. Array keys are object IDs (if there is a primary key on the model. |
|
| 876 | + * if not, numerically indexed) Some full examples: get 10 transactions |
|
| 877 | + * which have Scottish attendees: EEM_Transaction::instance()->get_all( |
|
| 878 | + * array( array( |
|
| 879 | + * 'OR'=>array( |
|
| 880 | + * 'Registration.Attendee.ATT_fname'=>array('like','Mc%'), |
|
| 881 | + * 'Registration.Attendee.ATT_fname*other'=>array('like','Mac%') |
|
| 882 | + * ) |
|
| 883 | + * ), |
|
| 884 | + * 'limit'=>10, |
|
| 885 | + * 'group_by'=>'TXN_ID' |
|
| 886 | + * )); |
|
| 887 | + * get all the answers to the question titled "shirt size" for event with id |
|
| 888 | + * 12, ordered by their answer EEM_Answer::instance()->get_all(array( array( |
|
| 889 | + * 'Question.QST_display_text'=>'shirt size', |
|
| 890 | + * 'Registration.Event.EVT_ID'=>12 |
|
| 891 | + * ), |
|
| 892 | + * 'order_by'=>array('ANS_value'=>'ASC') |
|
| 893 | + * )); |
|
| 894 | + * @throws \EE_Error |
|
| 895 | + */ |
|
| 896 | + public function get_all($query_params = array()) |
|
| 897 | + { |
|
| 898 | + if (isset($query_params['limit']) |
|
| 899 | + && ! isset($query_params['group_by']) |
|
| 900 | + ) { |
|
| 901 | + $query_params['group_by'] = array_keys($this->get_combined_primary_key_fields()); |
|
| 902 | + } |
|
| 903 | + return $this->_create_objects($this->_get_all_wpdb_results($query_params, ARRAY_A, null)); |
|
| 904 | + } |
|
| 905 | + |
|
| 906 | + |
|
| 907 | + |
|
| 908 | + /** |
|
| 909 | + * Modifies the query parameters so we only get back model objects |
|
| 910 | + * that "belong" to the current user |
|
| 911 | + * |
|
| 912 | + * @param array $query_params @see EEM_Base::get_all() |
|
| 913 | + * @return array like EEM_Base::get_all |
|
| 914 | + */ |
|
| 915 | + public function alter_query_params_to_only_include_mine($query_params = array()) |
|
| 916 | + { |
|
| 917 | + $wp_user_field_name = $this->wp_user_field_name(); |
|
| 918 | + if ($wp_user_field_name) { |
|
| 919 | + $query_params[0][$wp_user_field_name] = get_current_user_id(); |
|
| 920 | + } |
|
| 921 | + return $query_params; |
|
| 922 | + } |
|
| 923 | + |
|
| 924 | + |
|
| 925 | + |
|
| 926 | + /** |
|
| 927 | + * Returns the name of the field's name that points to the WP_User table |
|
| 928 | + * on this model (or follows the _model_chain_to_wp_user and uses that model's |
|
| 929 | + * foreign key to the WP_User table) |
|
| 930 | + * |
|
| 931 | + * @return string|boolean string on success, boolean false when there is no |
|
| 932 | + * foreign key to the WP_User table |
|
| 933 | + */ |
|
| 934 | + public function wp_user_field_name() |
|
| 935 | + { |
|
| 936 | + try { |
|
| 937 | + if (! empty($this->_model_chain_to_wp_user)) { |
|
| 938 | + $models_to_follow_to_wp_users = explode('.', $this->_model_chain_to_wp_user); |
|
| 939 | + $last_model_name = end($models_to_follow_to_wp_users); |
|
| 940 | + $model_with_fk_to_wp_users = EE_Registry::instance()->load_model($last_model_name); |
|
| 941 | + $model_chain_to_wp_user = $this->_model_chain_to_wp_user . '.'; |
|
| 942 | + } else { |
|
| 943 | + $model_with_fk_to_wp_users = $this; |
|
| 944 | + $model_chain_to_wp_user = ''; |
|
| 945 | + } |
|
| 946 | + $wp_user_field = $model_with_fk_to_wp_users->get_foreign_key_to('WP_User'); |
|
| 947 | + return $model_chain_to_wp_user . $wp_user_field->get_name(); |
|
| 948 | + } catch (EE_Error $e) { |
|
| 949 | + return false; |
|
| 950 | + } |
|
| 951 | + } |
|
| 952 | + |
|
| 953 | + |
|
| 954 | + |
|
| 955 | + /** |
|
| 956 | + * Returns the _model_chain_to_wp_user string, which indicates which related model |
|
| 957 | + * (or transiently-related model) has a foreign key to the wp_users table; |
|
| 958 | + * useful for finding if model objects of this type are 'owned' by the current user. |
|
| 959 | + * This is an empty string when the foreign key is on this model and when it isn't, |
|
| 960 | + * but is only non-empty when this model's ownership is indicated by a RELATED model |
|
| 961 | + * (or transiently-related model) |
|
| 962 | + * |
|
| 963 | + * @return string |
|
| 964 | + */ |
|
| 965 | + public function model_chain_to_wp_user() |
|
| 966 | + { |
|
| 967 | + return $this->_model_chain_to_wp_user; |
|
| 968 | + } |
|
| 969 | + |
|
| 970 | + |
|
| 971 | + |
|
| 972 | + /** |
|
| 973 | + * Whether this model is 'owned' by a specific wordpress user (even indirectly, |
|
| 974 | + * like how registrations don't have a foreign key to wp_users, but the |
|
| 975 | + * events they are for are), or is unrelated to wp users. |
|
| 976 | + * generally available |
|
| 977 | + * |
|
| 978 | + * @return boolean |
|
| 979 | + */ |
|
| 980 | + public function is_owned() |
|
| 981 | + { |
|
| 982 | + if ($this->model_chain_to_wp_user()) { |
|
| 983 | + return true; |
|
| 984 | + } else { |
|
| 985 | + try { |
|
| 986 | + $this->get_foreign_key_to('WP_User'); |
|
| 987 | + return true; |
|
| 988 | + } catch (EE_Error $e) { |
|
| 989 | + return false; |
|
| 990 | + } |
|
| 991 | + } |
|
| 992 | + } |
|
| 993 | + |
|
| 994 | + |
|
| 995 | + |
|
| 996 | + /** |
|
| 997 | + * Used internally to get WPDB results, because other functions, besides get_all, may want to do some queries, but |
|
| 998 | + * may want to preserve the WPDB results (eg, update, which first queries to make sure we have all the tables on |
|
| 999 | + * the model) |
|
| 1000 | + * |
|
| 1001 | + * @param array $query_params like EEM_Base::get_all's $query_params |
|
| 1002 | + * @param string $output ARRAY_A, OBJECT_K, etc. Just like |
|
| 1003 | + * @param mixed $columns_to_select , What columns to select. By default, we select all columns specified by the |
|
| 1004 | + * fields on the model, and the models we joined to in the query. However, you can |
|
| 1005 | + * override this and set the select to "*", or a specific column name, like |
|
| 1006 | + * "ATT_ID", etc. If you would like to use these custom selections in WHERE, |
|
| 1007 | + * GROUP_BY, or HAVING clauses, you must instead provide an array. Array keys are |
|
| 1008 | + * the aliases used to refer to this selection, and values are to be |
|
| 1009 | + * numerically-indexed arrays, where 0 is the selection and 1 is the data type. |
|
| 1010 | + * Eg, array('count'=>array('COUNT(REG_ID)','%d')) |
|
| 1011 | + * @return array | stdClass[] like results of $wpdb->get_results($sql,OBJECT), (ie, output type is OBJECT) |
|
| 1012 | + * @throws \EE_Error |
|
| 1013 | + */ |
|
| 1014 | + protected function _get_all_wpdb_results($query_params = array(), $output = ARRAY_A, $columns_to_select = null) |
|
| 1015 | + { |
|
| 1016 | + // remember the custom selections, if any, and type cast as array |
|
| 1017 | + // (unless $columns_to_select is an object, then just set as an empty array) |
|
| 1018 | + // Note: (array) 'some string' === array( 'some string' ) |
|
| 1019 | + $this->_custom_selections = ! is_object($columns_to_select) ? (array)$columns_to_select : array(); |
|
| 1020 | + $model_query_info = $this->_create_model_query_info_carrier($query_params); |
|
| 1021 | + $select_expressions = $columns_to_select !== null |
|
| 1022 | + ? $this->_construct_select_from_input($columns_to_select) |
|
| 1023 | + : $this->_construct_default_select_sql($model_query_info); |
|
| 1024 | + $SQL = "SELECT $select_expressions " . $this->_construct_2nd_half_of_select_query($model_query_info); |
|
| 1025 | + return $this->_do_wpdb_query('get_results', array($SQL, $output)); |
|
| 1026 | + } |
|
| 1027 | + |
|
| 1028 | + |
|
| 1029 | + |
|
| 1030 | + /** |
|
| 1031 | + * Gets an array of rows from the database just like $wpdb->get_results would, |
|
| 1032 | + * but you can use the $query_params like on EEM_Base::get_all() to more easily |
|
| 1033 | + * take care of joins, field preparation etc. |
|
| 1034 | + * |
|
| 1035 | + * @param array $query_params like EEM_Base::get_all's $query_params |
|
| 1036 | + * @param string $output ARRAY_A, OBJECT_K, etc. Just like |
|
| 1037 | + * @param mixed $columns_to_select , What columns to select. By default, we select all columns specified by the |
|
| 1038 | + * fields on the model, and the models we joined to in the query. However, you can |
|
| 1039 | + * override this and set the select to "*", or a specific column name, like |
|
| 1040 | + * "ATT_ID", etc. If you would like to use these custom selections in WHERE, |
|
| 1041 | + * GROUP_BY, or HAVING clauses, you must instead provide an array. Array keys are |
|
| 1042 | + * the aliases used to refer to this selection, and values are to be |
|
| 1043 | + * numerically-indexed arrays, where 0 is the selection and 1 is the data type. |
|
| 1044 | + * Eg, array('count'=>array('COUNT(REG_ID)','%d')) |
|
| 1045 | + * @return array|stdClass[] like results of $wpdb->get_results($sql,OBJECT), (ie, output type is OBJECT) |
|
| 1046 | + * @throws \EE_Error |
|
| 1047 | + */ |
|
| 1048 | + public function get_all_wpdb_results($query_params = array(), $output = ARRAY_A, $columns_to_select = null) |
|
| 1049 | + { |
|
| 1050 | + return $this->_get_all_wpdb_results($query_params, $output, $columns_to_select); |
|
| 1051 | + } |
|
| 1052 | + |
|
| 1053 | + |
|
| 1054 | + |
|
| 1055 | + /** |
|
| 1056 | + * For creating a custom select statement |
|
| 1057 | + * |
|
| 1058 | + * @param mixed $columns_to_select either a string to be inserted directly as the select statement, |
|
| 1059 | + * or an array where keys are aliases, and values are arrays where 0=>the selection |
|
| 1060 | + * SQL, and 1=>is the datatype |
|
| 1061 | + * @throws EE_Error |
|
| 1062 | + * @return string |
|
| 1063 | + */ |
|
| 1064 | + private function _construct_select_from_input($columns_to_select) |
|
| 1065 | + { |
|
| 1066 | + if (is_array($columns_to_select)) { |
|
| 1067 | + $select_sql_array = array(); |
|
| 1068 | + foreach ($columns_to_select as $alias => $selection_and_datatype) { |
|
| 1069 | + if (! is_array($selection_and_datatype) || ! isset($selection_and_datatype[1])) { |
|
| 1070 | + throw new EE_Error( |
|
| 1071 | + sprintf( |
|
| 1072 | + __( |
|
| 1073 | + "Custom selection %s (alias %s) needs to be an array like array('COUNT(REG_ID)','%%d')", |
|
| 1074 | + "event_espresso" |
|
| 1075 | + ), |
|
| 1076 | + $selection_and_datatype, |
|
| 1077 | + $alias |
|
| 1078 | + ) |
|
| 1079 | + ); |
|
| 1080 | + } |
|
| 1081 | + if (! in_array($selection_and_datatype[1], $this->_valid_wpdb_data_types)) { |
|
| 1082 | + throw new EE_Error( |
|
| 1083 | + sprintf( |
|
| 1084 | + __( |
|
| 1085 | + "Datatype %s (for selection '%s' and alias '%s') is not a valid wpdb datatype (eg %%s)", |
|
| 1086 | + "event_espresso" |
|
| 1087 | + ), |
|
| 1088 | + $selection_and_datatype[1], |
|
| 1089 | + $selection_and_datatype[0], |
|
| 1090 | + $alias, |
|
| 1091 | + implode(",", $this->_valid_wpdb_data_types) |
|
| 1092 | + ) |
|
| 1093 | + ); |
|
| 1094 | + } |
|
| 1095 | + $select_sql_array[] = "{$selection_and_datatype[0]} AS $alias"; |
|
| 1096 | + } |
|
| 1097 | + $columns_to_select_string = implode(", ", $select_sql_array); |
|
| 1098 | + } else { |
|
| 1099 | + $columns_to_select_string = $columns_to_select; |
|
| 1100 | + } |
|
| 1101 | + return $columns_to_select_string; |
|
| 1102 | + } |
|
| 1103 | + |
|
| 1104 | + |
|
| 1105 | + |
|
| 1106 | + /** |
|
| 1107 | + * Convenient wrapper for getting the primary key field's name. Eg, on Registration, this would be 'REG_ID' |
|
| 1108 | + * |
|
| 1109 | + * @return string |
|
| 1110 | + * @throws \EE_Error |
|
| 1111 | + */ |
|
| 1112 | + public function primary_key_name() |
|
| 1113 | + { |
|
| 1114 | + return $this->get_primary_key_field()->get_name(); |
|
| 1115 | + } |
|
| 1116 | + |
|
| 1117 | + |
|
| 1118 | + |
|
| 1119 | + /** |
|
| 1120 | + * Gets a single item for this model from the DB, given only its ID (or null if none is found). |
|
| 1121 | + * If there is no primary key on this model, $id is treated as primary key string |
|
| 1122 | + * |
|
| 1123 | + * @param mixed $id int or string, depending on the type of the model's primary key |
|
| 1124 | + * @return EE_Base_Class |
|
| 1125 | + */ |
|
| 1126 | + public function get_one_by_ID($id) |
|
| 1127 | + { |
|
| 1128 | + if ($this->get_from_entity_map($id)) { |
|
| 1129 | + return $this->get_from_entity_map($id); |
|
| 1130 | + } |
|
| 1131 | + return $this->get_one( |
|
| 1132 | + $this->alter_query_params_to_restrict_by_ID( |
|
| 1133 | + $id, |
|
| 1134 | + array('default_where_conditions' => EEM_Base::default_where_conditions_minimum_all) |
|
| 1135 | + ) |
|
| 1136 | + ); |
|
| 1137 | + } |
|
| 1138 | + |
|
| 1139 | + |
|
| 1140 | + |
|
| 1141 | + /** |
|
| 1142 | + * Alters query parameters to only get items with this ID are returned. |
|
| 1143 | + * Takes into account that the ID might be a string produced by EEM_Base::get_index_primary_key_string(), |
|
| 1144 | + * or could just be a simple primary key ID |
|
| 1145 | + * |
|
| 1146 | + * @param int $id |
|
| 1147 | + * @param array $query_params |
|
| 1148 | + * @return array of normal query params, @see EEM_Base::get_all |
|
| 1149 | + * @throws \EE_Error |
|
| 1150 | + */ |
|
| 1151 | + public function alter_query_params_to_restrict_by_ID($id, $query_params = array()) |
|
| 1152 | + { |
|
| 1153 | + if (! isset($query_params[0])) { |
|
| 1154 | + $query_params[0] = array(); |
|
| 1155 | + } |
|
| 1156 | + $conditions_from_id = $this->parse_index_primary_key_string($id); |
|
| 1157 | + if ($conditions_from_id === null) { |
|
| 1158 | + $query_params[0][$this->primary_key_name()] = $id; |
|
| 1159 | + } else { |
|
| 1160 | + //no primary key, so the $id must be from the get_index_primary_key_string() |
|
| 1161 | + $query_params[0] = array_replace_recursive($query_params[0], $this->parse_index_primary_key_string($id)); |
|
| 1162 | + } |
|
| 1163 | + return $query_params; |
|
| 1164 | + } |
|
| 1165 | + |
|
| 1166 | + |
|
| 1167 | + |
|
| 1168 | + /** |
|
| 1169 | + * Gets a single item for this model from the DB, given the $query_params. Only returns a single class, not an |
|
| 1170 | + * array. If no item is found, null is returned. |
|
| 1171 | + * |
|
| 1172 | + * @param array $query_params like EEM_Base's $query_params variable. |
|
| 1173 | + * @return EE_Base_Class|EE_Soft_Delete_Base_Class|NULL |
|
| 1174 | + * @throws \EE_Error |
|
| 1175 | + */ |
|
| 1176 | + public function get_one($query_params = array()) |
|
| 1177 | + { |
|
| 1178 | + if (! is_array($query_params)) { |
|
| 1179 | + EE_Error::doing_it_wrong('EEM_Base::get_one', |
|
| 1180 | + sprintf(__('$query_params should be an array, you passed a variable of type %s', 'event_espresso'), |
|
| 1181 | + gettype($query_params)), '4.6.0'); |
|
| 1182 | + $query_params = array(); |
|
| 1183 | + } |
|
| 1184 | + $query_params['limit'] = 1; |
|
| 1185 | + $items = $this->get_all($query_params); |
|
| 1186 | + if (empty($items)) { |
|
| 1187 | + return null; |
|
| 1188 | + } else { |
|
| 1189 | + return array_shift($items); |
|
| 1190 | + } |
|
| 1191 | + } |
|
| 1192 | + |
|
| 1193 | + |
|
| 1194 | + |
|
| 1195 | + /** |
|
| 1196 | + * Returns the next x number of items in sequence from the given value as |
|
| 1197 | + * found in the database matching the given query conditions. |
|
| 1198 | + * |
|
| 1199 | + * @param mixed $current_field_value Value used for the reference point. |
|
| 1200 | + * @param null $field_to_order_by What field is used for the |
|
| 1201 | + * reference point. |
|
| 1202 | + * @param int $limit How many to return. |
|
| 1203 | + * @param array $query_params Extra conditions on the query. |
|
| 1204 | + * @param null $columns_to_select If left null, then an array of |
|
| 1205 | + * EE_Base_Class objects is returned, |
|
| 1206 | + * otherwise you can indicate just the |
|
| 1207 | + * columns you want returned. |
|
| 1208 | + * @return EE_Base_Class[]|array |
|
| 1209 | + * @throws \EE_Error |
|
| 1210 | + */ |
|
| 1211 | + public function next_x( |
|
| 1212 | + $current_field_value, |
|
| 1213 | + $field_to_order_by = null, |
|
| 1214 | + $limit = 1, |
|
| 1215 | + $query_params = array(), |
|
| 1216 | + $columns_to_select = null |
|
| 1217 | + ) { |
|
| 1218 | + return $this->_get_consecutive($current_field_value, '>', $field_to_order_by, $limit, $query_params, |
|
| 1219 | + $columns_to_select); |
|
| 1220 | + } |
|
| 1221 | + |
|
| 1222 | + |
|
| 1223 | + |
|
| 1224 | + /** |
|
| 1225 | + * Returns the previous x number of items in sequence from the given value |
|
| 1226 | + * as found in the database matching the given query conditions. |
|
| 1227 | + * |
|
| 1228 | + * @param mixed $current_field_value Value used for the reference point. |
|
| 1229 | + * @param null $field_to_order_by What field is used for the |
|
| 1230 | + * reference point. |
|
| 1231 | + * @param int $limit How many to return. |
|
| 1232 | + * @param array $query_params Extra conditions on the query. |
|
| 1233 | + * @param null $columns_to_select If left null, then an array of |
|
| 1234 | + * EE_Base_Class objects is returned, |
|
| 1235 | + * otherwise you can indicate just the |
|
| 1236 | + * columns you want returned. |
|
| 1237 | + * @return EE_Base_Class[]|array |
|
| 1238 | + * @throws \EE_Error |
|
| 1239 | + */ |
|
| 1240 | + public function previous_x( |
|
| 1241 | + $current_field_value, |
|
| 1242 | + $field_to_order_by = null, |
|
| 1243 | + $limit = 1, |
|
| 1244 | + $query_params = array(), |
|
| 1245 | + $columns_to_select = null |
|
| 1246 | + ) { |
|
| 1247 | + return $this->_get_consecutive($current_field_value, '<', $field_to_order_by, $limit, $query_params, |
|
| 1248 | + $columns_to_select); |
|
| 1249 | + } |
|
| 1250 | + |
|
| 1251 | + |
|
| 1252 | + |
|
| 1253 | + /** |
|
| 1254 | + * Returns the next item in sequence from the given value as found in the |
|
| 1255 | + * database matching the given query conditions. |
|
| 1256 | + * |
|
| 1257 | + * @param mixed $current_field_value Value used for the reference point. |
|
| 1258 | + * @param null $field_to_order_by What field is used for the |
|
| 1259 | + * reference point. |
|
| 1260 | + * @param array $query_params Extra conditions on the query. |
|
| 1261 | + * @param null $columns_to_select If left null, then an EE_Base_Class |
|
| 1262 | + * object is returned, otherwise you |
|
| 1263 | + * can indicate just the columns you |
|
| 1264 | + * want and a single array indexed by |
|
| 1265 | + * the columns will be returned. |
|
| 1266 | + * @return EE_Base_Class|null|array() |
|
| 1267 | + * @throws \EE_Error |
|
| 1268 | + */ |
|
| 1269 | + public function next( |
|
| 1270 | + $current_field_value, |
|
| 1271 | + $field_to_order_by = null, |
|
| 1272 | + $query_params = array(), |
|
| 1273 | + $columns_to_select = null |
|
| 1274 | + ) { |
|
| 1275 | + $results = $this->_get_consecutive($current_field_value, '>', $field_to_order_by, 1, $query_params, |
|
| 1276 | + $columns_to_select); |
|
| 1277 | + return empty($results) ? null : reset($results); |
|
| 1278 | + } |
|
| 1279 | + |
|
| 1280 | + |
|
| 1281 | + |
|
| 1282 | + /** |
|
| 1283 | + * Returns the previous item in sequence from the given value as found in |
|
| 1284 | + * the database matching the given query conditions. |
|
| 1285 | + * |
|
| 1286 | + * @param mixed $current_field_value Value used for the reference point. |
|
| 1287 | + * @param null $field_to_order_by What field is used for the |
|
| 1288 | + * reference point. |
|
| 1289 | + * @param array $query_params Extra conditions on the query. |
|
| 1290 | + * @param null $columns_to_select If left null, then an EE_Base_Class |
|
| 1291 | + * object is returned, otherwise you |
|
| 1292 | + * can indicate just the columns you |
|
| 1293 | + * want and a single array indexed by |
|
| 1294 | + * the columns will be returned. |
|
| 1295 | + * @return EE_Base_Class|null|array() |
|
| 1296 | + * @throws EE_Error |
|
| 1297 | + */ |
|
| 1298 | + public function previous( |
|
| 1299 | + $current_field_value, |
|
| 1300 | + $field_to_order_by = null, |
|
| 1301 | + $query_params = array(), |
|
| 1302 | + $columns_to_select = null |
|
| 1303 | + ) { |
|
| 1304 | + $results = $this->_get_consecutive($current_field_value, '<', $field_to_order_by, 1, $query_params, |
|
| 1305 | + $columns_to_select); |
|
| 1306 | + return empty($results) ? null : reset($results); |
|
| 1307 | + } |
|
| 1308 | + |
|
| 1309 | + |
|
| 1310 | + |
|
| 1311 | + /** |
|
| 1312 | + * Returns the a consecutive number of items in sequence from the given |
|
| 1313 | + * value as found in the database matching the given query conditions. |
|
| 1314 | + * |
|
| 1315 | + * @param mixed $current_field_value Value used for the reference point. |
|
| 1316 | + * @param string $operand What operand is used for the sequence. |
|
| 1317 | + * @param string $field_to_order_by What field is used for the reference point. |
|
| 1318 | + * @param int $limit How many to return. |
|
| 1319 | + * @param array $query_params Extra conditions on the query. |
|
| 1320 | + * @param null $columns_to_select If left null, then an array of EE_Base_Class objects is returned, |
|
| 1321 | + * otherwise you can indicate just the columns you want returned. |
|
| 1322 | + * @return EE_Base_Class[]|array |
|
| 1323 | + * @throws EE_Error |
|
| 1324 | + */ |
|
| 1325 | + protected function _get_consecutive( |
|
| 1326 | + $current_field_value, |
|
| 1327 | + $operand = '>', |
|
| 1328 | + $field_to_order_by = null, |
|
| 1329 | + $limit = 1, |
|
| 1330 | + $query_params = array(), |
|
| 1331 | + $columns_to_select = null |
|
| 1332 | + ) { |
|
| 1333 | + //if $field_to_order_by is empty then let's assume we're ordering by the primary key. |
|
| 1334 | + if (empty($field_to_order_by)) { |
|
| 1335 | + if ($this->has_primary_key_field()) { |
|
| 1336 | + $field_to_order_by = $this->get_primary_key_field()->get_name(); |
|
| 1337 | + } else { |
|
| 1338 | + if (WP_DEBUG) { |
|
| 1339 | + throw new EE_Error(__('EEM_Base::_get_consecutive() has been called with no $field_to_order_by argument and there is no primary key on the field. Please provide the field you would like to use as the base for retrieving the next item(s).', |
|
| 1340 | + 'event_espresso')); |
|
| 1341 | + } |
|
| 1342 | + EE_Error::add_error(__('There was an error with the query.', 'event_espresso')); |
|
| 1343 | + return array(); |
|
| 1344 | + } |
|
| 1345 | + } |
|
| 1346 | + if (! is_array($query_params)) { |
|
| 1347 | + EE_Error::doing_it_wrong('EEM_Base::_get_consecutive', |
|
| 1348 | + sprintf(__('$query_params should be an array, you passed a variable of type %s', 'event_espresso'), |
|
| 1349 | + gettype($query_params)), '4.6.0'); |
|
| 1350 | + $query_params = array(); |
|
| 1351 | + } |
|
| 1352 | + //let's add the where query param for consecutive look up. |
|
| 1353 | + $query_params[0][$field_to_order_by] = array($operand, $current_field_value); |
|
| 1354 | + $query_params['limit'] = $limit; |
|
| 1355 | + //set direction |
|
| 1356 | + $incoming_orderby = isset($query_params['order_by']) ? (array)$query_params['order_by'] : array(); |
|
| 1357 | + $query_params['order_by'] = $operand === '>' |
|
| 1358 | + ? array($field_to_order_by => 'ASC') + $incoming_orderby |
|
| 1359 | + : array($field_to_order_by => 'DESC') + $incoming_orderby; |
|
| 1360 | + //if $columns_to_select is empty then that means we're returning EE_Base_Class objects |
|
| 1361 | + if (empty($columns_to_select)) { |
|
| 1362 | + return $this->get_all($query_params); |
|
| 1363 | + } else { |
|
| 1364 | + //getting just the fields |
|
| 1365 | + return $this->_get_all_wpdb_results($query_params, ARRAY_A, $columns_to_select); |
|
| 1366 | + } |
|
| 1367 | + } |
|
| 1368 | + |
|
| 1369 | + |
|
| 1370 | + |
|
| 1371 | + /** |
|
| 1372 | + * This sets the _timezone property after model object has been instantiated. |
|
| 1373 | + * |
|
| 1374 | + * @param null | string $timezone valid PHP DateTimeZone timezone string |
|
| 1375 | + */ |
|
| 1376 | + public function set_timezone($timezone) |
|
| 1377 | + { |
|
| 1378 | + if ($timezone !== null) { |
|
| 1379 | + $this->_timezone = $timezone; |
|
| 1380 | + } |
|
| 1381 | + //note we need to loop through relations and set the timezone on those objects as well. |
|
| 1382 | + foreach ($this->_model_relations as $relation) { |
|
| 1383 | + $relation->set_timezone($timezone); |
|
| 1384 | + } |
|
| 1385 | + //and finally we do the same for any datetime fields |
|
| 1386 | + foreach ($this->_fields as $field) { |
|
| 1387 | + if ($field instanceof EE_Datetime_Field) { |
|
| 1388 | + $field->set_timezone($timezone); |
|
| 1389 | + } |
|
| 1390 | + } |
|
| 1391 | + } |
|
| 1392 | + |
|
| 1393 | + |
|
| 1394 | + |
|
| 1395 | + /** |
|
| 1396 | + * This just returns whatever is set for the current timezone. |
|
| 1397 | + * |
|
| 1398 | + * @access public |
|
| 1399 | + * @return string |
|
| 1400 | + */ |
|
| 1401 | + public function get_timezone() |
|
| 1402 | + { |
|
| 1403 | + //first validate if timezone is set. If not, then let's set it be whatever is set on the model fields. |
|
| 1404 | + if (empty($this->_timezone)) { |
|
| 1405 | + foreach ($this->_fields as $field) { |
|
| 1406 | + if ($field instanceof EE_Datetime_Field) { |
|
| 1407 | + $this->set_timezone($field->get_timezone()); |
|
| 1408 | + break; |
|
| 1409 | + } |
|
| 1410 | + } |
|
| 1411 | + } |
|
| 1412 | + //if timezone STILL empty then return the default timezone for the site. |
|
| 1413 | + if (empty($this->_timezone)) { |
|
| 1414 | + $this->set_timezone(EEH_DTT_Helper::get_timezone()); |
|
| 1415 | + } |
|
| 1416 | + return $this->_timezone; |
|
| 1417 | + } |
|
| 1418 | + |
|
| 1419 | + |
|
| 1420 | + |
|
| 1421 | + /** |
|
| 1422 | + * This returns the date formats set for the given field name and also ensures that |
|
| 1423 | + * $this->_timezone property is set correctly. |
|
| 1424 | + * |
|
| 1425 | + * @since 4.6.x |
|
| 1426 | + * @param string $field_name The name of the field the formats are being retrieved for. |
|
| 1427 | + * @param bool $pretty Whether to return the pretty formats (true) or not (false). |
|
| 1428 | + * @throws EE_Error If the given field_name is not of the EE_Datetime_Field type. |
|
| 1429 | + * @return array formats in an array with the date format first, and the time format last. |
|
| 1430 | + */ |
|
| 1431 | + public function get_formats_for($field_name, $pretty = false) |
|
| 1432 | + { |
|
| 1433 | + $field_settings = $this->field_settings_for($field_name); |
|
| 1434 | + //if not a valid EE_Datetime_Field then throw error |
|
| 1435 | + if (! $field_settings instanceof EE_Datetime_Field) { |
|
| 1436 | + throw new EE_Error(sprintf(__('The field sent into EEM_Base::get_formats_for (%s) is not registered as a EE_Datetime_Field. Please check the spelling and make sure you are submitting the right field name to retrieve date_formats for.', |
|
| 1437 | + 'event_espresso'), $field_name)); |
|
| 1438 | + } |
|
| 1439 | + //while we are here, let's make sure the timezone internally in EEM_Base matches what is stored on |
|
| 1440 | + //the field. |
|
| 1441 | + $this->_timezone = $field_settings->get_timezone(); |
|
| 1442 | + return array($field_settings->get_date_format($pretty), $field_settings->get_time_format($pretty)); |
|
| 1443 | + } |
|
| 1444 | + |
|
| 1445 | + |
|
| 1446 | + |
|
| 1447 | + /** |
|
| 1448 | + * This returns the current time in a format setup for a query on this model. |
|
| 1449 | + * Usage of this method makes it easier to setup queries against EE_Datetime_Field columns because |
|
| 1450 | + * it will return: |
|
| 1451 | + * - a formatted string in the timezone and format currently set on the EE_Datetime_Field for the given field for |
|
| 1452 | + * NOW |
|
| 1453 | + * - or a unix timestamp (equivalent to time()) |
|
| 1454 | + * |
|
| 1455 | + * @since 4.6.x |
|
| 1456 | + * @param string $field_name The field the current time is needed for. |
|
| 1457 | + * @param bool $timestamp True means to return a unix timestamp. Otherwise a |
|
| 1458 | + * formatted string matching the set format for the field in the set timezone will |
|
| 1459 | + * be returned. |
|
| 1460 | + * @param string $what Whether to return the string in just the time format, the date format, or both. |
|
| 1461 | + * @throws EE_Error If the given field_name is not of the EE_Datetime_Field type. |
|
| 1462 | + * @return int|string If the given field_name is not of the EE_Datetime_Field type, then an EE_Error |
|
| 1463 | + * exception is triggered. |
|
| 1464 | + */ |
|
| 1465 | + public function current_time_for_query($field_name, $timestamp = false, $what = 'both') |
|
| 1466 | + { |
|
| 1467 | + $formats = $this->get_formats_for($field_name); |
|
| 1468 | + $DateTime = new DateTime("now", new DateTimeZone($this->_timezone)); |
|
| 1469 | + if ($timestamp) { |
|
| 1470 | + return $DateTime->format('U'); |
|
| 1471 | + } |
|
| 1472 | + //not returning timestamp, so return formatted string in timezone. |
|
| 1473 | + switch ($what) { |
|
| 1474 | + case 'time' : |
|
| 1475 | + return $DateTime->format($formats[1]); |
|
| 1476 | + break; |
|
| 1477 | + case 'date' : |
|
| 1478 | + return $DateTime->format($formats[0]); |
|
| 1479 | + break; |
|
| 1480 | + default : |
|
| 1481 | + return $DateTime->format(implode(' ', $formats)); |
|
| 1482 | + break; |
|
| 1483 | + } |
|
| 1484 | + } |
|
| 1485 | + |
|
| 1486 | + |
|
| 1487 | + |
|
| 1488 | + /** |
|
| 1489 | + * This receives a time string for a given field and ensures that it is setup to match what the internal settings |
|
| 1490 | + * for the model are. Returns a DateTime object. |
|
| 1491 | + * Note: a gotcha for when you send in unix timestamp. Remember a unix timestamp is already timezone agnostic, |
|
| 1492 | + * (functionally the equivalent of UTC+0). So when you send it in, whatever timezone string you include is |
|
| 1493 | + * ignored. |
|
| 1494 | + * |
|
| 1495 | + * @param string $field_name The field being setup. |
|
| 1496 | + * @param string $timestring The date time string being used. |
|
| 1497 | + * @param string $incoming_format The format for the time string. |
|
| 1498 | + * @param string $timezone By default, it is assumed the incoming time string is in timezone for |
|
| 1499 | + * the blog. If this is not the case, then it can be specified here. If incoming |
|
| 1500 | + * format is |
|
| 1501 | + * 'U', this is ignored. |
|
| 1502 | + * @return DateTime |
|
| 1503 | + * @throws \EE_Error |
|
| 1504 | + */ |
|
| 1505 | + public function convert_datetime_for_query($field_name, $timestring, $incoming_format, $timezone = '') |
|
| 1506 | + { |
|
| 1507 | + //just using this to ensure the timezone is set correctly internally |
|
| 1508 | + $this->get_formats_for($field_name); |
|
| 1509 | + //load EEH_DTT_Helper |
|
| 1510 | + $set_timezone = empty($timezone) ? EEH_DTT_Helper::get_timezone() : $timezone; |
|
| 1511 | + $incomingDateTime = date_create_from_format($incoming_format, $timestring, new DateTimeZone($set_timezone)); |
|
| 1512 | + return \EventEspresso\core\domain\entities\DbSafeDateTime::createFromDateTime( $incomingDateTime->setTimezone(new DateTimeZone($this->_timezone)) ); |
|
| 1513 | + } |
|
| 1514 | + |
|
| 1515 | + |
|
| 1516 | + |
|
| 1517 | + /** |
|
| 1518 | + * Gets all the tables comprising this model. Array keys are the table aliases, and values are EE_Table objects |
|
| 1519 | + * |
|
| 1520 | + * @return EE_Table_Base[] |
|
| 1521 | + */ |
|
| 1522 | + public function get_tables() |
|
| 1523 | + { |
|
| 1524 | + return $this->_tables; |
|
| 1525 | + } |
|
| 1526 | + |
|
| 1527 | + |
|
| 1528 | + |
|
| 1529 | + /** |
|
| 1530 | + * Updates all the database entries (in each table for this model) according to $fields_n_values and optionally |
|
| 1531 | + * also updates all the model objects, where the criteria expressed in $query_params are met.. |
|
| 1532 | + * Also note: if this model has multiple tables, this update verifies all the secondary tables have an entry for |
|
| 1533 | + * each row (in the primary table) we're trying to update; if not, it inserts an entry in the secondary table. Eg: |
|
| 1534 | + * if our model has 2 tables: wp_posts (primary), and wp_esp_event (secondary). Let's say we are trying to update a |
|
| 1535 | + * model object with EVT_ID = 1 |
|
| 1536 | + * (which means where wp_posts has ID = 1, because wp_posts.ID is the primary key's column), which exists, but |
|
| 1537 | + * there is no entry in wp_esp_event for this entry in wp_posts. So, this update script will insert a row into |
|
| 1538 | + * wp_esp_event, using any available parameters from $fields_n_values (eg, if "EVT_limit" => 40 is in |
|
| 1539 | + * $fields_n_values, the new entry in wp_esp_event will set EVT_limit = 40, and use default for other columns which |
|
| 1540 | + * are not specified) |
|
| 1541 | + * |
|
| 1542 | + * @param array $fields_n_values keys are model fields (exactly like keys in EEM_Base::_fields, NOT db |
|
| 1543 | + * columns!), values are strings, ints, floats, and maybe arrays if they |
|
| 1544 | + * are to be serialized. Basically, the values are what you'd expect to be |
|
| 1545 | + * values on the model, NOT necessarily what's in the DB. For example, if |
|
| 1546 | + * we wanted to update only the TXN_details on any Transactions where its |
|
| 1547 | + * ID=34, we'd use this method as follows: |
|
| 1548 | + * EEM_Transaction::instance()->update( |
|
| 1549 | + * array('TXN_details'=>array('detail1'=>'monkey','detail2'=>'banana'), |
|
| 1550 | + * array(array('TXN_ID'=>34))); |
|
| 1551 | + * @param array $query_params very much like EEM_Base::get_all's $query_params |
|
| 1552 | + * in client code into what's expected to be stored on each field. Eg, |
|
| 1553 | + * consider updating Question's QST_admin_label field is of type |
|
| 1554 | + * Simple_HTML. If you use this function to update that field to $new_value |
|
| 1555 | + * = (note replace 8's with appropriate opening and closing tags in the |
|
| 1556 | + * following example)"8script8alert('I hack all');8/script88b8boom |
|
| 1557 | + * baby8/b8", then if you set $values_already_prepared_by_model_object to |
|
| 1558 | + * TRUE, it is assumed that you've already called |
|
| 1559 | + * EE_Simple_HTML_Field->prepare_for_set($new_value), which removes the |
|
| 1560 | + * malicious javascript. However, if |
|
| 1561 | + * $values_already_prepared_by_model_object is left as FALSE, then |
|
| 1562 | + * EE_Simple_HTML_Field->prepare_for_set($new_value) will be called on it, |
|
| 1563 | + * and every other field, before insertion. We provide this parameter |
|
| 1564 | + * because model objects perform their prepare_for_set function on all |
|
| 1565 | + * their values, and so don't need to be called again (and in many cases, |
|
| 1566 | + * shouldn't be called again. Eg: if we escape HTML characters in the |
|
| 1567 | + * prepare_for_set method...) |
|
| 1568 | + * @param boolean $keep_model_objs_in_sync if TRUE, makes sure we ALSO update model objects |
|
| 1569 | + * in this model's entity map according to $fields_n_values that match |
|
| 1570 | + * $query_params. This obviously has some overhead, so you can disable it |
|
| 1571 | + * by setting this to FALSE, but be aware that model objects being used |
|
| 1572 | + * could get out-of-sync with the database |
|
| 1573 | + * @return int how many rows got updated or FALSE if something went wrong with the query (wp returns FALSE or num |
|
| 1574 | + * rows affected which *could* include 0 which DOES NOT mean the query was |
|
| 1575 | + * bad) |
|
| 1576 | + * @throws \EE_Error |
|
| 1577 | + */ |
|
| 1578 | + public function update($fields_n_values, $query_params, $keep_model_objs_in_sync = true) |
|
| 1579 | + { |
|
| 1580 | + if (! is_array($query_params)) { |
|
| 1581 | + EE_Error::doing_it_wrong('EEM_Base::update', |
|
| 1582 | + sprintf(__('$query_params should be an array, you passed a variable of type %s', 'event_espresso'), |
|
| 1583 | + gettype($query_params)), '4.6.0'); |
|
| 1584 | + $query_params = array(); |
|
| 1585 | + } |
|
| 1586 | + /** |
|
| 1587 | + * Action called before a model update call has been made. |
|
| 1588 | + * |
|
| 1589 | + * @param EEM_Base $model |
|
| 1590 | + * @param array $fields_n_values the updated fields and their new values |
|
| 1591 | + * @param array $query_params @see EEM_Base::get_all() |
|
| 1592 | + */ |
|
| 1593 | + do_action('AHEE__EEM_Base__update__begin', $this, $fields_n_values, $query_params); |
|
| 1594 | + /** |
|
| 1595 | + * Filters the fields about to be updated given the query parameters. You can provide the |
|
| 1596 | + * $query_params to $this->get_all() to find exactly which records will be updated |
|
| 1597 | + * |
|
| 1598 | + * @param array $fields_n_values fields and their new values |
|
| 1599 | + * @param EEM_Base $model the model being queried |
|
| 1600 | + * @param array $query_params see EEM_Base::get_all() |
|
| 1601 | + */ |
|
| 1602 | + $fields_n_values = (array)apply_filters('FHEE__EEM_Base__update__fields_n_values', $fields_n_values, $this, |
|
| 1603 | + $query_params); |
|
| 1604 | + //need to verify that, for any entry we want to update, there are entries in each secondary table. |
|
| 1605 | + //to do that, for each table, verify that it's PK isn't null. |
|
| 1606 | + $tables = $this->get_tables(); |
|
| 1607 | + //and if the other tables don't have a row for each table-to-be-updated, we'll insert one with whatever values available in the current update query |
|
| 1608 | + //NOTE: we should make this code more efficient by NOT querying twice |
|
| 1609 | + //before the real update, but that needs to first go through ALPHA testing |
|
| 1610 | + //as it's dangerous. says Mike August 8 2014 |
|
| 1611 | + //we want to make sure the default_where strategy is ignored |
|
| 1612 | + $this->_ignore_where_strategy = true; |
|
| 1613 | + $wpdb_select_results = $this->_get_all_wpdb_results($query_params); |
|
| 1614 | + foreach ($wpdb_select_results as $wpdb_result) { |
|
| 1615 | + // type cast stdClass as array |
|
| 1616 | + $wpdb_result = (array)$wpdb_result; |
|
| 1617 | + //get the model object's PK, as we'll want this if we need to insert a row into secondary tables |
|
| 1618 | + if ($this->has_primary_key_field()) { |
|
| 1619 | + $main_table_pk_value = $wpdb_result[$this->get_primary_key_field()->get_qualified_column()]; |
|
| 1620 | + } else { |
|
| 1621 | + //if there's no primary key, we basically can't support having a 2nd table on the model (we could but it would be lots of work) |
|
| 1622 | + $main_table_pk_value = null; |
|
| 1623 | + } |
|
| 1624 | + //if there are more than 1 tables, we'll want to verify that each table for this model has an entry in the other tables |
|
| 1625 | + //and if the other tables don't have a row for each table-to-be-updated, we'll insert one with whatever values available in the current update query |
|
| 1626 | + if (count($tables) > 1) { |
|
| 1627 | + //foreach matching row in the DB, ensure that each table's PK isn't null. If so, there must not be an entry |
|
| 1628 | + //in that table, and so we'll want to insert one |
|
| 1629 | + foreach ($tables as $table_obj) { |
|
| 1630 | + $this_table_pk_column = $table_obj->get_fully_qualified_pk_column(); |
|
| 1631 | + //if there is no private key for this table on the results, it means there's no entry |
|
| 1632 | + //in this table, right? so insert a row in the current table, using any fields available |
|
| 1633 | + if (! (array_key_exists($this_table_pk_column, $wpdb_result) |
|
| 1634 | + && $wpdb_result[$this_table_pk_column]) |
|
| 1635 | + ) { |
|
| 1636 | + $success = $this->_insert_into_specific_table($table_obj, $fields_n_values, |
|
| 1637 | + $main_table_pk_value); |
|
| 1638 | + //if we died here, report the error |
|
| 1639 | + if (! $success) { |
|
| 1640 | + return false; |
|
| 1641 | + } |
|
| 1642 | + } |
|
| 1643 | + } |
|
| 1644 | + } |
|
| 1645 | + // //and now check that if we have cached any models by that ID on the model, that |
|
| 1646 | + // //they also get updated properly |
|
| 1647 | + // $model_object = $this->get_from_entity_map( $main_table_pk_value ); |
|
| 1648 | + // if( $model_object ){ |
|
| 1649 | + // foreach( $fields_n_values as $field => $value ){ |
|
| 1650 | + // $model_object->set($field, $value); |
|
| 1651 | + //let's make sure default_where strategy is followed now |
|
| 1652 | + $this->_ignore_where_strategy = false; |
|
| 1653 | + } |
|
| 1654 | + //if we want to keep model objects in sync, AND |
|
| 1655 | + //if this wasn't called from a model object (to update itself) |
|
| 1656 | + //then we want to make sure we keep all the existing |
|
| 1657 | + //model objects in sync with the db |
|
| 1658 | + if ($keep_model_objs_in_sync && ! $this->_values_already_prepared_by_model_object) { |
|
| 1659 | + if ($this->has_primary_key_field()) { |
|
| 1660 | + $model_objs_affected_ids = $this->get_col($query_params); |
|
| 1661 | + } else { |
|
| 1662 | + //we need to select a bunch of columns and then combine them into the the "index primary key string"s |
|
| 1663 | + $models_affected_key_columns = $this->_get_all_wpdb_results($query_params, ARRAY_A); |
|
| 1664 | + $model_objs_affected_ids = array(); |
|
| 1665 | + foreach ($models_affected_key_columns as $row) { |
|
| 1666 | + $combined_index_key = $this->get_index_primary_key_string($row); |
|
| 1667 | + $model_objs_affected_ids[$combined_index_key] = $combined_index_key; |
|
| 1668 | + } |
|
| 1669 | + } |
|
| 1670 | + if (! $model_objs_affected_ids) { |
|
| 1671 | + //wait wait wait- if nothing was affected let's stop here |
|
| 1672 | + return 0; |
|
| 1673 | + } |
|
| 1674 | + foreach ($model_objs_affected_ids as $id) { |
|
| 1675 | + $model_obj_in_entity_map = $this->get_from_entity_map($id); |
|
| 1676 | + if ($model_obj_in_entity_map) { |
|
| 1677 | + foreach ($fields_n_values as $field => $new_value) { |
|
| 1678 | + $model_obj_in_entity_map->set($field, $new_value); |
|
| 1679 | + } |
|
| 1680 | + } |
|
| 1681 | + } |
|
| 1682 | + //if there is a primary key on this model, we can now do a slight optimization |
|
| 1683 | + if ($this->has_primary_key_field()) { |
|
| 1684 | + //we already know what we want to update. So let's make the query simpler so it's a little more efficient |
|
| 1685 | + $query_params = array( |
|
| 1686 | + array($this->primary_key_name() => array('IN', $model_objs_affected_ids)), |
|
| 1687 | + 'limit' => count($model_objs_affected_ids), |
|
| 1688 | + 'default_where_conditions' => EEM_Base::default_where_conditions_none, |
|
| 1689 | + ); |
|
| 1690 | + } |
|
| 1691 | + } |
|
| 1692 | + $model_query_info = $this->_create_model_query_info_carrier($query_params); |
|
| 1693 | + $SQL = "UPDATE " |
|
| 1694 | + . $model_query_info->get_full_join_sql() |
|
| 1695 | + . " SET " |
|
| 1696 | + . $this->_construct_update_sql($fields_n_values) |
|
| 1697 | + . $model_query_info->get_where_sql();//note: doesn't use _construct_2nd_half_of_select_query() because doesn't accept LIMIT, ORDER BY, etc. |
|
| 1698 | + $rows_affected = $this->_do_wpdb_query('query', array($SQL)); |
|
| 1699 | + /** |
|
| 1700 | + * Action called after a model update call has been made. |
|
| 1701 | + * |
|
| 1702 | + * @param EEM_Base $model |
|
| 1703 | + * @param array $fields_n_values the updated fields and their new values |
|
| 1704 | + * @param array $query_params @see EEM_Base::get_all() |
|
| 1705 | + * @param int $rows_affected |
|
| 1706 | + */ |
|
| 1707 | + do_action('AHEE__EEM_Base__update__end', $this, $fields_n_values, $query_params, $rows_affected); |
|
| 1708 | + return $rows_affected;//how many supposedly got updated |
|
| 1709 | + } |
|
| 1710 | + |
|
| 1711 | + |
|
| 1712 | + |
|
| 1713 | + /** |
|
| 1714 | + * Analogous to $wpdb->get_col, returns a 1-dimensional array where teh values |
|
| 1715 | + * are teh values of the field specified (or by default the primary key field) |
|
| 1716 | + * that matched the query params. Note that you should pass the name of the |
|
| 1717 | + * model FIELD, not the database table's column name. |
|
| 1718 | + * |
|
| 1719 | + * @param array $query_params @see EEM_Base::get_all() |
|
| 1720 | + * @param string $field_to_select |
|
| 1721 | + * @return array just like $wpdb->get_col() |
|
| 1722 | + * @throws \EE_Error |
|
| 1723 | + */ |
|
| 1724 | + public function get_col($query_params = array(), $field_to_select = null) |
|
| 1725 | + { |
|
| 1726 | + if ($field_to_select) { |
|
| 1727 | + $field = $this->field_settings_for($field_to_select); |
|
| 1728 | + } elseif ($this->has_primary_key_field()) { |
|
| 1729 | + $field = $this->get_primary_key_field(); |
|
| 1730 | + } else { |
|
| 1731 | + //no primary key, just grab the first column |
|
| 1732 | + $field = reset($this->field_settings()); |
|
| 1733 | + } |
|
| 1734 | + $model_query_info = $this->_create_model_query_info_carrier($query_params); |
|
| 1735 | + $select_expressions = $field->get_qualified_column(); |
|
| 1736 | + $SQL = "SELECT $select_expressions " . $this->_construct_2nd_half_of_select_query($model_query_info); |
|
| 1737 | + return $this->_do_wpdb_query('get_col', array($SQL)); |
|
| 1738 | + } |
|
| 1739 | + |
|
| 1740 | + |
|
| 1741 | + |
|
| 1742 | + /** |
|
| 1743 | + * Returns a single column value for a single row from the database |
|
| 1744 | + * |
|
| 1745 | + * @param array $query_params @see EEM_Base::get_all() |
|
| 1746 | + * @param string $field_to_select @see EEM_Base::get_col() |
|
| 1747 | + * @return string |
|
| 1748 | + * @throws \EE_Error |
|
| 1749 | + */ |
|
| 1750 | + public function get_var($query_params = array(), $field_to_select = null) |
|
| 1751 | + { |
|
| 1752 | + $query_params['limit'] = 1; |
|
| 1753 | + $col = $this->get_col($query_params, $field_to_select); |
|
| 1754 | + if (! empty($col)) { |
|
| 1755 | + return reset($col); |
|
| 1756 | + } else { |
|
| 1757 | + return null; |
|
| 1758 | + } |
|
| 1759 | + } |
|
| 1760 | + |
|
| 1761 | + |
|
| 1762 | + |
|
| 1763 | + /** |
|
| 1764 | + * Makes the SQL for after "UPDATE table_X inner join table_Y..." and before "...WHERE". Eg "Question.name='party |
|
| 1765 | + * time?', Question.desc='what do you think?',..." Values are filtered through wpdb->prepare to avoid against SQL |
|
| 1766 | + * injection, but currently no further filtering is done |
|
| 1767 | + * |
|
| 1768 | + * @global $wpdb |
|
| 1769 | + * @param array $fields_n_values array keys are field names on this model, and values are what those fields should |
|
| 1770 | + * be updated to in the DB |
|
| 1771 | + * @return string of SQL |
|
| 1772 | + * @throws \EE_Error |
|
| 1773 | + */ |
|
| 1774 | + public function _construct_update_sql($fields_n_values) |
|
| 1775 | + { |
|
| 1776 | + /** @type WPDB $wpdb */ |
|
| 1777 | + global $wpdb; |
|
| 1778 | + $cols_n_values = array(); |
|
| 1779 | + foreach ($fields_n_values as $field_name => $value) { |
|
| 1780 | + $field_obj = $this->field_settings_for($field_name); |
|
| 1781 | + //if the value is NULL, we want to assign the value to that. |
|
| 1782 | + //wpdb->prepare doesn't really handle that properly |
|
| 1783 | + $prepared_value = $this->_prepare_value_or_use_default($field_obj, $fields_n_values); |
|
| 1784 | + $value_sql = $prepared_value === null ? 'NULL' |
|
| 1785 | + : $wpdb->prepare($field_obj->get_wpdb_data_type(), $prepared_value); |
|
| 1786 | + $cols_n_values[] = $field_obj->get_qualified_column() . "=" . $value_sql; |
|
| 1787 | + } |
|
| 1788 | + return implode(",", $cols_n_values); |
|
| 1789 | + } |
|
| 1790 | + |
|
| 1791 | + |
|
| 1792 | + |
|
| 1793 | + /** |
|
| 1794 | + * Deletes a single row from the DB given the model object's primary key value. (eg, EE_Attendee->ID()'s value). |
|
| 1795 | + * Performs a HARD delete, meaning the database row should always be removed, |
|
| 1796 | + * not just have a flag field on it switched |
|
| 1797 | + * Wrapper for EEM_Base::delete_permanently() |
|
| 1798 | + * |
|
| 1799 | + * @param mixed $id |
|
| 1800 | + * @param boolean $allow_blocking |
|
| 1801 | + * @return int the number of rows deleted |
|
| 1802 | + * @throws \EE_Error |
|
| 1803 | + */ |
|
| 1804 | + public function delete_permanently_by_ID($id, $allow_blocking = true) |
|
| 1805 | + { |
|
| 1806 | + return $this->delete_permanently( |
|
| 1807 | + array( |
|
| 1808 | + array($this->get_primary_key_field()->get_name() => $id), |
|
| 1809 | + 'limit' => 1, |
|
| 1810 | + ), |
|
| 1811 | + $allow_blocking |
|
| 1812 | + ); |
|
| 1813 | + } |
|
| 1814 | + |
|
| 1815 | + |
|
| 1816 | + |
|
| 1817 | + /** |
|
| 1818 | + * Deletes a single row from the DB given the model object's primary key value. (eg, EE_Attendee->ID()'s value). |
|
| 1819 | + * Wrapper for EEM_Base::delete() |
|
| 1820 | + * |
|
| 1821 | + * @param mixed $id |
|
| 1822 | + * @param boolean $allow_blocking |
|
| 1823 | + * @return int the number of rows deleted |
|
| 1824 | + * @throws \EE_Error |
|
| 1825 | + */ |
|
| 1826 | + public function delete_by_ID($id, $allow_blocking = true) |
|
| 1827 | + { |
|
| 1828 | + return $this->delete( |
|
| 1829 | + array( |
|
| 1830 | + array($this->get_primary_key_field()->get_name() => $id), |
|
| 1831 | + 'limit' => 1, |
|
| 1832 | + ), |
|
| 1833 | + $allow_blocking |
|
| 1834 | + ); |
|
| 1835 | + } |
|
| 1836 | + |
|
| 1837 | + |
|
| 1838 | + |
|
| 1839 | + /** |
|
| 1840 | + * Identical to delete_permanently, but does a "soft" delete if possible, |
|
| 1841 | + * meaning if the model has a field that indicates its been "trashed" or |
|
| 1842 | + * "soft deleted", we will just set that instead of actually deleting the rows. |
|
| 1843 | + * |
|
| 1844 | + * @see EEM_Base::delete_permanently |
|
| 1845 | + * @param array $query_params |
|
| 1846 | + * @param boolean $allow_blocking |
|
| 1847 | + * @return int how many rows got deleted |
|
| 1848 | + * @throws \EE_Error |
|
| 1849 | + */ |
|
| 1850 | + public function delete($query_params, $allow_blocking = true) |
|
| 1851 | + { |
|
| 1852 | + return $this->delete_permanently($query_params, $allow_blocking); |
|
| 1853 | + } |
|
| 1854 | + |
|
| 1855 | + |
|
| 1856 | + |
|
| 1857 | + /** |
|
| 1858 | + * Deletes the model objects that meet the query params. Note: this method is overridden |
|
| 1859 | + * in EEM_Soft_Delete_Base so that soft-deleted model objects are instead only flagged |
|
| 1860 | + * as archived, not actually deleted |
|
| 1861 | + * |
|
| 1862 | + * @param array $query_params very much like EEM_Base::get_all's $query_params |
|
| 1863 | + * @param boolean $allow_blocking if TRUE, matched objects will only be deleted if there is no related model info |
|
| 1864 | + * that blocks it (ie, there' sno other data that depends on this data); if false, |
|
| 1865 | + * deletes regardless of other objects which may depend on it. Its generally |
|
| 1866 | + * advisable to always leave this as TRUE, otherwise you could easily corrupt your |
|
| 1867 | + * DB |
|
| 1868 | + * @return int how many rows got deleted |
|
| 1869 | + * @throws \EE_Error |
|
| 1870 | + */ |
|
| 1871 | + public function delete_permanently($query_params, $allow_blocking = true) |
|
| 1872 | + { |
|
| 1873 | + /** |
|
| 1874 | + * Action called just before performing a real deletion query. You can use the |
|
| 1875 | + * model and its $query_params to find exactly which items will be deleted |
|
| 1876 | + * |
|
| 1877 | + * @param EEM_Base $model |
|
| 1878 | + * @param array $query_params @see EEM_Base::get_all() |
|
| 1879 | + * @param boolean $allow_blocking whether or not to allow related model objects |
|
| 1880 | + * to block (prevent) this deletion |
|
| 1881 | + */ |
|
| 1882 | + do_action('AHEE__EEM_Base__delete__begin', $this, $query_params, $allow_blocking); |
|
| 1883 | + //some MySQL databases may be running safe mode, which may restrict |
|
| 1884 | + //deletion if there is no KEY column used in the WHERE statement of a deletion. |
|
| 1885 | + //to get around this, we first do a SELECT, get all the IDs, and then run another query |
|
| 1886 | + //to delete them |
|
| 1887 | + $items_for_deletion = $this->_get_all_wpdb_results($query_params); |
|
| 1888 | + $deletion_where = $this->_setup_ids_for_delete($items_for_deletion, $allow_blocking); |
|
| 1889 | + if ($deletion_where) { |
|
| 1890 | + //echo "objects for deletion:";var_dump($objects_for_deletion); |
|
| 1891 | + $model_query_info = $this->_create_model_query_info_carrier($query_params); |
|
| 1892 | + $table_aliases = array_keys($this->_tables); |
|
| 1893 | + $SQL = "DELETE " |
|
| 1894 | + . implode(", ", $table_aliases) |
|
| 1895 | + . " FROM " |
|
| 1896 | + . $model_query_info->get_full_join_sql() |
|
| 1897 | + . " WHERE " |
|
| 1898 | + . $deletion_where; |
|
| 1899 | + // /echo "delete sql:$SQL"; |
|
| 1900 | + $rows_deleted = $this->_do_wpdb_query('query', array($SQL)); |
|
| 1901 | + } else { |
|
| 1902 | + $rows_deleted = 0; |
|
| 1903 | + } |
|
| 1904 | + //and lastly make sure those items are removed from the entity map; if they could be put into it at all |
|
| 1905 | + if ($this->has_primary_key_field()) { |
|
| 1906 | + foreach ($items_for_deletion as $item_for_deletion_row) { |
|
| 1907 | + $pk_value = $item_for_deletion_row[$this->get_primary_key_field()->get_qualified_column()]; |
|
| 1908 | + if (isset($this->_entity_map[EEM_Base::$_model_query_blog_id][$pk_value])) { |
|
| 1909 | + unset($this->_entity_map[EEM_Base::$_model_query_blog_id][$pk_value]); |
|
| 1910 | + } |
|
| 1911 | + } |
|
| 1912 | + } |
|
| 1913 | + /** |
|
| 1914 | + * Action called just after performing a real deletion query. Although at this point the |
|
| 1915 | + * items should have been deleted |
|
| 1916 | + * |
|
| 1917 | + * @param EEM_Base $model |
|
| 1918 | + * @param array $query_params @see EEM_Base::get_all() |
|
| 1919 | + * @param int $rows_deleted |
|
| 1920 | + */ |
|
| 1921 | + do_action('AHEE__EEM_Base__delete__end', $this, $query_params, $rows_deleted); |
|
| 1922 | + return $rows_deleted;//how many supposedly got deleted |
|
| 1923 | + } |
|
| 1924 | + |
|
| 1925 | + |
|
| 1926 | + |
|
| 1927 | + /** |
|
| 1928 | + * Checks all the relations that throw error messages when there are blocking related objects |
|
| 1929 | + * for related model objects. If there are any related model objects on those relations, |
|
| 1930 | + * adds an EE_Error, and return true |
|
| 1931 | + * |
|
| 1932 | + * @param EE_Base_Class|int $this_model_obj_or_id |
|
| 1933 | + * @param EE_Base_Class $ignore_this_model_obj a model object like 'EE_Event', or 'EE_Term_Taxonomy', which |
|
| 1934 | + * should be ignored when determining whether there are related |
|
| 1935 | + * model objects which block this model object's deletion. Useful |
|
| 1936 | + * if you know A is related to B and are considering deleting A, |
|
| 1937 | + * but want to see if A has any other objects blocking its deletion |
|
| 1938 | + * before removing the relation between A and B |
|
| 1939 | + * @return boolean |
|
| 1940 | + * @throws \EE_Error |
|
| 1941 | + */ |
|
| 1942 | + public function delete_is_blocked_by_related_models($this_model_obj_or_id, $ignore_this_model_obj = null) |
|
| 1943 | + { |
|
| 1944 | + //first, if $ignore_this_model_obj was supplied, get its model |
|
| 1945 | + if ($ignore_this_model_obj && $ignore_this_model_obj instanceof EE_Base_Class) { |
|
| 1946 | + $ignored_model = $ignore_this_model_obj->get_model(); |
|
| 1947 | + } else { |
|
| 1948 | + $ignored_model = null; |
|
| 1949 | + } |
|
| 1950 | + //now check all the relations of $this_model_obj_or_id and see if there |
|
| 1951 | + //are any related model objects blocking it? |
|
| 1952 | + $is_blocked = false; |
|
| 1953 | + foreach ($this->_model_relations as $relation_name => $relation_obj) { |
|
| 1954 | + if ($relation_obj->block_delete_if_related_models_exist()) { |
|
| 1955 | + //if $ignore_this_model_obj was supplied, then for the query |
|
| 1956 | + //on that model needs to be told to ignore $ignore_this_model_obj |
|
| 1957 | + if ($ignored_model && $relation_name === $ignored_model->get_this_model_name()) { |
|
| 1958 | + $related_model_objects = $relation_obj->get_all_related($this_model_obj_or_id, array( |
|
| 1959 | + array( |
|
| 1960 | + $ignored_model->get_primary_key_field()->get_name() => array( |
|
| 1961 | + '!=', |
|
| 1962 | + $ignore_this_model_obj->ID(), |
|
| 1963 | + ), |
|
| 1964 | + ), |
|
| 1965 | + )); |
|
| 1966 | + } else { |
|
| 1967 | + $related_model_objects = $relation_obj->get_all_related($this_model_obj_or_id); |
|
| 1968 | + } |
|
| 1969 | + if ($related_model_objects) { |
|
| 1970 | + EE_Error::add_error($relation_obj->get_deletion_error_message(), __FILE__, __FUNCTION__, __LINE__); |
|
| 1971 | + $is_blocked = true; |
|
| 1972 | + } |
|
| 1973 | + } |
|
| 1974 | + } |
|
| 1975 | + return $is_blocked; |
|
| 1976 | + } |
|
| 1977 | + |
|
| 1978 | + |
|
| 1979 | + |
|
| 1980 | + /** |
|
| 1981 | + * This sets up our delete where sql and accounts for if we have secondary tables that will have rows deleted as |
|
| 1982 | + * well. |
|
| 1983 | + * |
|
| 1984 | + * @param array $objects_for_deletion This should be the values returned by $this->_get_all_wpdb_results() |
|
| 1985 | + * @param boolean $allow_blocking if TRUE, matched objects will only be deleted if there is no related model |
|
| 1986 | + * info that blocks it (ie, there' sno other data that depends on this data); |
|
| 1987 | + * if false, deletes regardless of other objects which may depend on it. Its |
|
| 1988 | + * generally advisable to always leave this as TRUE, otherwise you could |
|
| 1989 | + * easily corrupt your DB |
|
| 1990 | + * @throws EE_Error |
|
| 1991 | + * @return string everything that comes after the WHERE statement. |
|
| 1992 | + */ |
|
| 1993 | + protected function _setup_ids_for_delete($objects_for_deletion, $allow_blocking = true) |
|
| 1994 | + { |
|
| 1995 | + if ($this->has_primary_key_field()) { |
|
| 1996 | + $primary_table = $this->_get_main_table(); |
|
| 1997 | + $pt_pk_field = $this->get_field_by_column($primary_table->get_fully_qualified_pk_column()); |
|
| 1998 | + $other_tables = $this->_get_other_tables(); |
|
| 1999 | + $ot_pk_fields = array(); |
|
| 2000 | + $ot_fk_fields = array(); |
|
| 2001 | + foreach($other_tables as $other_table_alias => $other_table_obj){ |
|
| 2002 | + $ot_pk_fields[$other_table_alias] = $this->get_field_by_column($other_table_obj->get_fully_qualified_pk_column()); |
|
| 2003 | + $ot_fk_fields[$other_table_alias] = $this->get_field_by_column($other_table_obj->get_fully_qualified_fk_column()); |
|
| 2004 | + } |
|
| 2005 | + $deletes = $query = array(); |
|
| 2006 | + foreach ($objects_for_deletion as $delete_object) { |
|
| 2007 | + //before we mark this object for deletion, |
|
| 2008 | + //make sure there's no related objects blocking its deletion (if we're checking) |
|
| 2009 | + if ( |
|
| 2010 | + $allow_blocking |
|
| 2011 | + && $this->delete_is_blocked_by_related_models( |
|
| 2012 | + $delete_object[$primary_table->get_fully_qualified_pk_column()] |
|
| 2013 | + ) |
|
| 2014 | + ) { |
|
| 2015 | + continue; |
|
| 2016 | + } |
|
| 2017 | + //primary table deletes |
|
| 2018 | + if (isset($delete_object[$primary_table->get_fully_qualified_pk_column()])) { |
|
| 2019 | + |
|
| 2020 | + $deletes[$primary_table->get_fully_qualified_pk_column()][] = $this->_wpdb_prepare_using_field( |
|
| 2021 | + $delete_object[$primary_table->get_fully_qualified_pk_column()], |
|
| 2022 | + $pt_pk_field |
|
| 2023 | + ); |
|
| 2024 | + } |
|
| 2025 | + //other tables |
|
| 2026 | + if (! empty($other_tables)) { |
|
| 2027 | + foreach ($other_tables as $ot_alias => $ot) { |
|
| 2028 | + $ot_pk_field = $ot_pk_fields[$ot_alias]; |
|
| 2029 | + $ot_fk_field = $ot_fk_fields[$ot_alias]; |
|
| 2030 | + //first check if we've got the foreign key column here. |
|
| 2031 | + if (isset($delete_object[$ot->get_fully_qualified_fk_column()])) { |
|
| 2032 | + $deletes[$ot->get_fully_qualified_pk_column()][] = $this->_wpdb_prepare_using_field( |
|
| 2033 | + $delete_object[$ot->get_fully_qualified_fk_column()], |
|
| 2034 | + $ot_fk_field |
|
| 2035 | + ); |
|
| 2036 | + } |
|
| 2037 | + // wait! it's entirely possible that we'll have a the primary key |
|
| 2038 | + // for this table in here, if it's a foreign key for one of the other secondary tables |
|
| 2039 | + if (isset($delete_object[$ot->get_fully_qualified_pk_column()])) { |
|
| 2040 | + $deletes[$ot->get_fully_qualified_pk_column()][] = $this->_wpdb_prepare_using_field( |
|
| 2041 | + $delete_object[$ot->get_fully_qualified_pk_column()], |
|
| 2042 | + $ot_pk_field |
|
| 2043 | + ); |
|
| 2044 | + } |
|
| 2045 | + // finally, it is possible that the fk for this table is found |
|
| 2046 | + // in the fully qualified pk column for the fk table, so let's see if that's there! |
|
| 2047 | + if (isset($delete_object[$ot->get_fully_qualified_pk_on_fk_table()])) { |
|
| 2048 | + $deletes[$ot->get_fully_qualified_pk_column()][] = $this->_wpdb_prepare_using_field( |
|
| 2049 | + $delete_object[$ot->get_fully_qualified_pk_column()], |
|
| 2050 | + $ot_pk_field |
|
| 2051 | + ); |
|
| 2052 | + } |
|
| 2053 | + } |
|
| 2054 | + } |
|
| 2055 | + } |
|
| 2056 | + //we should have deletes now, so let's just go through and setup the where statement |
|
| 2057 | + foreach ($deletes as $column => $values) { |
|
| 2058 | + //make sure we have unique $values; |
|
| 2059 | + $values = array_unique($values); |
|
| 2060 | + $query[] = $column . ' IN(' . implode(",", $values) . ')'; |
|
| 2061 | + } |
|
| 2062 | + return ! empty($query) ? implode(' AND ', $query) : ''; |
|
| 2063 | + } elseif (count($this->get_combined_primary_key_fields()) > 1) { |
|
| 2064 | + $ways_to_identify_a_row = array(); |
|
| 2065 | + $fields = $this->get_combined_primary_key_fields(); |
|
| 2066 | + //note: because there' sno primary key, that means nothing else can be pointing to this model, right? |
|
| 2067 | + foreach ($objects_for_deletion as $delete_object) { |
|
| 2068 | + $values_for_each_cpk_for_a_row = array(); |
|
| 2069 | + foreach ($fields as $cpk_field) { |
|
| 2070 | + if ($cpk_field instanceof EE_Model_Field_Base) { |
|
| 2071 | + $values_for_each_cpk_for_a_row[] = $cpk_field->get_qualified_column() |
|
| 2072 | + . "=" |
|
| 2073 | + . $delete_object[$cpk_field->get_qualified_column()]; |
|
| 2074 | + } |
|
| 2075 | + } |
|
| 2076 | + $ways_to_identify_a_row[] = "(" . implode(" AND ", $values_for_each_cpk_for_a_row) . ")"; |
|
| 2077 | + } |
|
| 2078 | + return implode(" OR ", $ways_to_identify_a_row); |
|
| 2079 | + } else { |
|
| 2080 | + //so there's no primary key and no combined key... |
|
| 2081 | + //sorry, can't help you |
|
| 2082 | + throw new EE_Error(sprintf(__("Cannot delete objects of type %s because there is no primary key NOR combined key", |
|
| 2083 | + "event_espresso"), get_class($this))); |
|
| 2084 | + } |
|
| 2085 | + } |
|
| 2086 | + |
|
| 2087 | + |
|
| 2088 | + /** |
|
| 2089 | + * Gets the model field by the fully qualified name |
|
| 2090 | + * @param string $qualified_column_name eg 'Event_CPT.post_name' or $field_obj->get_qualified_column() |
|
| 2091 | + * @return EE_Model_Field_Base |
|
| 2092 | + */ |
|
| 2093 | + public function get_field_by_column($qualified_column_name) |
|
| 2094 | + { |
|
| 2095 | + foreach($this->field_settings(true) as $field_name => $field_obj){ |
|
| 2096 | + if($field_obj->get_qualified_column() === $qualified_column_name){ |
|
| 2097 | + return $field_obj; |
|
| 2098 | + } |
|
| 2099 | + } |
|
| 2100 | + throw new EE_Error( |
|
| 2101 | + sprintf( |
|
| 2102 | + esc_html__('Could not find a field on the model "%1$s" for qualified column "%2$s"', 'event_espresso'), |
|
| 2103 | + $this->get_this_model_name(), |
|
| 2104 | + $qualified_column_name |
|
| 2105 | + ) |
|
| 2106 | + ); |
|
| 2107 | + } |
|
| 2108 | + |
|
| 2109 | + |
|
| 2110 | + |
|
| 2111 | + /** |
|
| 2112 | + * Count all the rows that match criteria expressed in $query_params (an array just like arg to EEM_Base::get_all). |
|
| 2113 | + * If $field_to_count isn't provided, the model's primary key is used. Otherwise, we count by field_to_count's |
|
| 2114 | + * column |
|
| 2115 | + * |
|
| 2116 | + * @param array $query_params like EEM_Base::get_all's |
|
| 2117 | + * @param string $field_to_count field on model to count by (not column name) |
|
| 2118 | + * @param bool $distinct if we want to only count the distinct values for the column then you can trigger |
|
| 2119 | + * that by the setting $distinct to TRUE; |
|
| 2120 | + * @return int |
|
| 2121 | + * @throws \EE_Error |
|
| 2122 | + */ |
|
| 2123 | + public function count($query_params = array(), $field_to_count = null, $distinct = false) |
|
| 2124 | + { |
|
| 2125 | + $model_query_info = $this->_create_model_query_info_carrier($query_params); |
|
| 2126 | + if ($field_to_count) { |
|
| 2127 | + $field_obj = $this->field_settings_for($field_to_count); |
|
| 2128 | + $column_to_count = $field_obj->get_qualified_column(); |
|
| 2129 | + } elseif ($this->has_primary_key_field()) { |
|
| 2130 | + $pk_field_obj = $this->get_primary_key_field(); |
|
| 2131 | + $column_to_count = $pk_field_obj->get_qualified_column(); |
|
| 2132 | + } else { |
|
| 2133 | + //there's no primary key |
|
| 2134 | + //if we're counting distinct items, and there's no primary key, |
|
| 2135 | + //we need to list out the columns for distinction; |
|
| 2136 | + //otherwise we can just use star |
|
| 2137 | + if ($distinct) { |
|
| 2138 | + $columns_to_use = array(); |
|
| 2139 | + foreach ($this->get_combined_primary_key_fields() as $field_obj) { |
|
| 2140 | + $columns_to_use[] = $field_obj->get_qualified_column(); |
|
| 2141 | + } |
|
| 2142 | + $column_to_count = implode(',', $columns_to_use); |
|
| 2143 | + } else { |
|
| 2144 | + $column_to_count = '*'; |
|
| 2145 | + } |
|
| 2146 | + } |
|
| 2147 | + $column_to_count = $distinct ? "DISTINCT " . $column_to_count : $column_to_count; |
|
| 2148 | + $SQL = "SELECT COUNT(" . $column_to_count . ")" . $this->_construct_2nd_half_of_select_query($model_query_info); |
|
| 2149 | + return (int)$this->_do_wpdb_query('get_var', array($SQL)); |
|
| 2150 | + } |
|
| 2151 | + |
|
| 2152 | + |
|
| 2153 | + |
|
| 2154 | + /** |
|
| 2155 | + * Sums up the value of the $field_to_sum (defaults to the primary key, which isn't terribly useful) |
|
| 2156 | + * |
|
| 2157 | + * @param array $query_params like EEM_Base::get_all |
|
| 2158 | + * @param string $field_to_sum name of field (array key in $_fields array) |
|
| 2159 | + * @return float |
|
| 2160 | + * @throws \EE_Error |
|
| 2161 | + */ |
|
| 2162 | + public function sum($query_params, $field_to_sum = null) |
|
| 2163 | + { |
|
| 2164 | + $model_query_info = $this->_create_model_query_info_carrier($query_params); |
|
| 2165 | + if ($field_to_sum) { |
|
| 2166 | + $field_obj = $this->field_settings_for($field_to_sum); |
|
| 2167 | + } else { |
|
| 2168 | + $field_obj = $this->get_primary_key_field(); |
|
| 2169 | + } |
|
| 2170 | + $column_to_count = $field_obj->get_qualified_column(); |
|
| 2171 | + $SQL = "SELECT SUM(" . $column_to_count . ")" . $this->_construct_2nd_half_of_select_query($model_query_info); |
|
| 2172 | + $return_value = $this->_do_wpdb_query('get_var', array($SQL)); |
|
| 2173 | + $data_type = $field_obj->get_wpdb_data_type(); |
|
| 2174 | + if ($data_type === '%d' || $data_type === '%s') { |
|
| 2175 | + return (float)$return_value; |
|
| 2176 | + } else {//must be %f |
|
| 2177 | + return (float)$return_value; |
|
| 2178 | + } |
|
| 2179 | + } |
|
| 2180 | + |
|
| 2181 | + |
|
| 2182 | + |
|
| 2183 | + /** |
|
| 2184 | + * Just calls the specified method on $wpdb with the given arguments |
|
| 2185 | + * Consolidates a little extra error handling code |
|
| 2186 | + * |
|
| 2187 | + * @param string $wpdb_method |
|
| 2188 | + * @param array $arguments_to_provide |
|
| 2189 | + * @throws EE_Error |
|
| 2190 | + * @global wpdb $wpdb |
|
| 2191 | + * @return mixed |
|
| 2192 | + */ |
|
| 2193 | + protected function _do_wpdb_query($wpdb_method, $arguments_to_provide) |
|
| 2194 | + { |
|
| 2195 | + //if we're in maintenance mode level 2, DON'T run any queries |
|
| 2196 | + //because level 2 indicates the database needs updating and |
|
| 2197 | + //is probably out of sync with the code |
|
| 2198 | + if (! EE_Maintenance_Mode::instance()->models_can_query()) { |
|
| 2199 | + throw new EE_Error(sprintf(__("Event Espresso Level 2 Maintenance mode is active. That means EE can not run ANY database queries until the necessary migration scripts have run which will take EE out of maintenance mode level 2. Please inform support of this error.", |
|
| 2200 | + "event_espresso"))); |
|
| 2201 | + } |
|
| 2202 | + /** @type WPDB $wpdb */ |
|
| 2203 | + global $wpdb; |
|
| 2204 | + if (! method_exists($wpdb, $wpdb_method)) { |
|
| 2205 | + throw new EE_Error(sprintf(__('There is no method named "%s" on Wordpress\' $wpdb object', |
|
| 2206 | + 'event_espresso'), $wpdb_method)); |
|
| 2207 | + } |
|
| 2208 | + if (WP_DEBUG) { |
|
| 2209 | + $old_show_errors_value = $wpdb->show_errors; |
|
| 2210 | + $wpdb->show_errors(false); |
|
| 2211 | + } |
|
| 2212 | + $result = $this->_process_wpdb_query($wpdb_method, $arguments_to_provide); |
|
| 2213 | + $this->show_db_query_if_previously_requested($wpdb->last_query); |
|
| 2214 | + if (WP_DEBUG) { |
|
| 2215 | + $wpdb->show_errors($old_show_errors_value); |
|
| 2216 | + if (! empty($wpdb->last_error)) { |
|
| 2217 | + throw new EE_Error(sprintf(__('WPDB Error: "%s"', 'event_espresso'), $wpdb->last_error)); |
|
| 2218 | + } elseif ($result === false) { |
|
| 2219 | + throw new EE_Error(sprintf(__('WPDB Error occurred, but no error message was logged by wpdb! The wpdb method called was "%1$s" and the arguments were "%2$s"', |
|
| 2220 | + 'event_espresso'), $wpdb_method, var_export($arguments_to_provide, true))); |
|
| 2221 | + } |
|
| 2222 | + } elseif ($result === false) { |
|
| 2223 | + EE_Error::add_error( |
|
| 2224 | + sprintf( |
|
| 2225 | + __('A database error has occurred. Turn on WP_DEBUG for more information.||A database error occurred doing wpdb method "%1$s", with arguments "%2$s". The error was "%3$s"', |
|
| 2226 | + 'event_espresso'), |
|
| 2227 | + $wpdb_method, |
|
| 2228 | + var_export($arguments_to_provide, true), |
|
| 2229 | + $wpdb->last_error |
|
| 2230 | + ), |
|
| 2231 | + __FILE__, |
|
| 2232 | + __FUNCTION__, |
|
| 2233 | + __LINE__ |
|
| 2234 | + ); |
|
| 2235 | + } |
|
| 2236 | + return $result; |
|
| 2237 | + } |
|
| 2238 | + |
|
| 2239 | + |
|
| 2240 | + |
|
| 2241 | + /** |
|
| 2242 | + * Attempts to run the indicated WPDB method with the provided arguments, |
|
| 2243 | + * and if there's an error tries to verify the DB is correct. Uses |
|
| 2244 | + * the static property EEM_Base::$_db_verification_level to determine whether |
|
| 2245 | + * we should try to fix the EE core db, the addons, or just give up |
|
| 2246 | + * |
|
| 2247 | + * @param string $wpdb_method |
|
| 2248 | + * @param array $arguments_to_provide |
|
| 2249 | + * @return mixed |
|
| 2250 | + */ |
|
| 2251 | + private function _process_wpdb_query($wpdb_method, $arguments_to_provide) |
|
| 2252 | + { |
|
| 2253 | + /** @type WPDB $wpdb */ |
|
| 2254 | + global $wpdb; |
|
| 2255 | + $wpdb->last_error = null; |
|
| 2256 | + $result = call_user_func_array(array($wpdb, $wpdb_method), $arguments_to_provide); |
|
| 2257 | + // was there an error running the query? but we don't care on new activations |
|
| 2258 | + // (we're going to setup the DB anyway on new activations) |
|
| 2259 | + if (($result === false || ! empty($wpdb->last_error)) |
|
| 2260 | + && EE_System::instance()->detect_req_type() !== EE_System::req_type_new_activation |
|
| 2261 | + ) { |
|
| 2262 | + switch (EEM_Base::$_db_verification_level) { |
|
| 2263 | + case EEM_Base::db_verified_none : |
|
| 2264 | + // let's double-check core's DB |
|
| 2265 | + $error_message = $this->_verify_core_db($wpdb_method, $arguments_to_provide); |
|
| 2266 | + break; |
|
| 2267 | + case EEM_Base::db_verified_core : |
|
| 2268 | + // STILL NO LOVE?? verify all the addons too. Maybe they need to be fixed |
|
| 2269 | + $error_message = $this->_verify_addons_db($wpdb_method, $arguments_to_provide); |
|
| 2270 | + break; |
|
| 2271 | + case EEM_Base::db_verified_addons : |
|
| 2272 | + // ummmm... you in trouble |
|
| 2273 | + return $result; |
|
| 2274 | + break; |
|
| 2275 | + } |
|
| 2276 | + if (! empty($error_message)) { |
|
| 2277 | + EE_Log::instance()->log(__FILE__, __FUNCTION__, $error_message, 'error'); |
|
| 2278 | + trigger_error($error_message); |
|
| 2279 | + } |
|
| 2280 | + return $this->_process_wpdb_query($wpdb_method, $arguments_to_provide); |
|
| 2281 | + } |
|
| 2282 | + return $result; |
|
| 2283 | + } |
|
| 2284 | + |
|
| 2285 | + |
|
| 2286 | + |
|
| 2287 | + /** |
|
| 2288 | + * Verifies the EE core database is up-to-date and records that we've done it on |
|
| 2289 | + * EEM_Base::$_db_verification_level |
|
| 2290 | + * |
|
| 2291 | + * @param string $wpdb_method |
|
| 2292 | + * @param array $arguments_to_provide |
|
| 2293 | + * @return string |
|
| 2294 | + */ |
|
| 2295 | + private function _verify_core_db($wpdb_method, $arguments_to_provide) |
|
| 2296 | + { |
|
| 2297 | + /** @type WPDB $wpdb */ |
|
| 2298 | + global $wpdb; |
|
| 2299 | + //ok remember that we've already attempted fixing the core db, in case the problem persists |
|
| 2300 | + EEM_Base::$_db_verification_level = EEM_Base::db_verified_core; |
|
| 2301 | + $error_message = sprintf( |
|
| 2302 | + __('WPDB Error "%1$s" while running wpdb method "%2$s" with arguments %3$s. Automatically attempting to fix EE Core DB', |
|
| 2303 | + 'event_espresso'), |
|
| 2304 | + $wpdb->last_error, |
|
| 2305 | + $wpdb_method, |
|
| 2306 | + wp_json_encode($arguments_to_provide) |
|
| 2307 | + ); |
|
| 2308 | + EE_System::instance()->initialize_db_if_no_migrations_required(false, true); |
|
| 2309 | + return $error_message; |
|
| 2310 | + } |
|
| 2311 | + |
|
| 2312 | + |
|
| 2313 | + |
|
| 2314 | + /** |
|
| 2315 | + * Verifies the EE addons' database is up-to-date and records that we've done it on |
|
| 2316 | + * EEM_Base::$_db_verification_level |
|
| 2317 | + * |
|
| 2318 | + * @param $wpdb_method |
|
| 2319 | + * @param $arguments_to_provide |
|
| 2320 | + * @return string |
|
| 2321 | + */ |
|
| 2322 | + private function _verify_addons_db($wpdb_method, $arguments_to_provide) |
|
| 2323 | + { |
|
| 2324 | + /** @type WPDB $wpdb */ |
|
| 2325 | + global $wpdb; |
|
| 2326 | + //ok remember that we've already attempted fixing the addons dbs, in case the problem persists |
|
| 2327 | + EEM_Base::$_db_verification_level = EEM_Base::db_verified_addons; |
|
| 2328 | + $error_message = sprintf( |
|
| 2329 | + __('WPDB AGAIN: Error "%1$s" while running the same method and arguments as before. Automatically attempting to fix EE Addons DB', |
|
| 2330 | + 'event_espresso'), |
|
| 2331 | + $wpdb->last_error, |
|
| 2332 | + $wpdb_method, |
|
| 2333 | + wp_json_encode($arguments_to_provide) |
|
| 2334 | + ); |
|
| 2335 | + EE_System::instance()->initialize_addons(); |
|
| 2336 | + return $error_message; |
|
| 2337 | + } |
|
| 2338 | + |
|
| 2339 | + |
|
| 2340 | + |
|
| 2341 | + /** |
|
| 2342 | + * In order to avoid repeating this code for the get_all, sum, and count functions, put the code parts |
|
| 2343 | + * that are identical in here. Returns a string of SQL of everything in a SELECT query except the beginning |
|
| 2344 | + * SELECT clause, eg " FROM wp_posts AS Event INNER JOIN ... WHERE ... ORDER BY ... LIMIT ... GROUP BY ... HAVING |
|
| 2345 | + * ..." |
|
| 2346 | + * |
|
| 2347 | + * @param EE_Model_Query_Info_Carrier $model_query_info |
|
| 2348 | + * @return string |
|
| 2349 | + */ |
|
| 2350 | + private function _construct_2nd_half_of_select_query(EE_Model_Query_Info_Carrier $model_query_info) |
|
| 2351 | + { |
|
| 2352 | + return " FROM " . $model_query_info->get_full_join_sql() . |
|
| 2353 | + $model_query_info->get_where_sql() . |
|
| 2354 | + $model_query_info->get_group_by_sql() . |
|
| 2355 | + $model_query_info->get_having_sql() . |
|
| 2356 | + $model_query_info->get_order_by_sql() . |
|
| 2357 | + $model_query_info->get_limit_sql(); |
|
| 2358 | + } |
|
| 2359 | + |
|
| 2360 | + |
|
| 2361 | + |
|
| 2362 | + /** |
|
| 2363 | + * Set to easily debug the next X queries ran from this model. |
|
| 2364 | + * |
|
| 2365 | + * @param int $count |
|
| 2366 | + */ |
|
| 2367 | + public function show_next_x_db_queries($count = 1) |
|
| 2368 | + { |
|
| 2369 | + $this->_show_next_x_db_queries = $count; |
|
| 2370 | + } |
|
| 2371 | + |
|
| 2372 | + |
|
| 2373 | + |
|
| 2374 | + /** |
|
| 2375 | + * @param $sql_query |
|
| 2376 | + */ |
|
| 2377 | + public function show_db_query_if_previously_requested($sql_query) |
|
| 2378 | + { |
|
| 2379 | + if ($this->_show_next_x_db_queries > 0) { |
|
| 2380 | + echo $sql_query; |
|
| 2381 | + $this->_show_next_x_db_queries--; |
|
| 2382 | + } |
|
| 2383 | + } |
|
| 2384 | + |
|
| 2385 | + |
|
| 2386 | + |
|
| 2387 | + /** |
|
| 2388 | + * Adds a relationship of the correct type between $modelObject and $otherModelObject. |
|
| 2389 | + * There are the 3 cases: |
|
| 2390 | + * 'belongsTo' relationship: sets $id_or_obj's foreign_key to be $other_model_id_or_obj's primary_key. If |
|
| 2391 | + * $otherModelObject has no ID, it is first saved. |
|
| 2392 | + * 'hasMany' relationship: sets $other_model_id_or_obj's foreign_key to be $id_or_obj's primary_key. If $id_or_obj |
|
| 2393 | + * has no ID, it is first saved. |
|
| 2394 | + * 'hasAndBelongsToMany' relationships: checks that there isn't already an entry in the join table, and adds one. |
|
| 2395 | + * If one of the model Objects has not yet been saved to the database, it is saved before adding the entry in the |
|
| 2396 | + * join table |
|
| 2397 | + * |
|
| 2398 | + * @param EE_Base_Class /int $thisModelObject |
|
| 2399 | + * @param EE_Base_Class /int $id_or_obj EE_base_Class or ID of other Model Object |
|
| 2400 | + * @param string $relationName , key in EEM_Base::_relations |
|
| 2401 | + * an attendee to a group, you also want to specify which role they |
|
| 2402 | + * will have in that group. So you would use this parameter to |
|
| 2403 | + * specify array('role-column-name'=>'role-id') |
|
| 2404 | + * @param array $extra_join_model_fields_n_values This allows you to enter further query params for the relation |
|
| 2405 | + * to for relation to methods that allow you to further specify |
|
| 2406 | + * extra columns to join by (such as HABTM). Keep in mind that the |
|
| 2407 | + * only acceptable query_params is strict "col" => "value" pairs |
|
| 2408 | + * because these will be inserted in any new rows created as well. |
|
| 2409 | + * @return EE_Base_Class which was added as a relation. Object referred to by $other_model_id_or_obj |
|
| 2410 | + * @throws \EE_Error |
|
| 2411 | + */ |
|
| 2412 | + public function add_relationship_to( |
|
| 2413 | + $id_or_obj, |
|
| 2414 | + $other_model_id_or_obj, |
|
| 2415 | + $relationName, |
|
| 2416 | + $extra_join_model_fields_n_values = array() |
|
| 2417 | + ) { |
|
| 2418 | + $relation_obj = $this->related_settings_for($relationName); |
|
| 2419 | + return $relation_obj->add_relation_to($id_or_obj, $other_model_id_or_obj, $extra_join_model_fields_n_values); |
|
| 2420 | + } |
|
| 2421 | + |
|
| 2422 | + |
|
| 2423 | + |
|
| 2424 | + /** |
|
| 2425 | + * Removes a relationship of the correct type between $modelObject and $otherModelObject. |
|
| 2426 | + * There are the 3 cases: |
|
| 2427 | + * 'belongsTo' relationship: sets $modelObject's foreign_key to null, if that field is nullable.Otherwise throws an |
|
| 2428 | + * error |
|
| 2429 | + * 'hasMany' relationship: sets $otherModelObject's foreign_key to null,if that field is nullable.Otherwise throws |
|
| 2430 | + * an error |
|
| 2431 | + * 'hasAndBelongsToMany' relationships:removes any existing entry in the join table between the two models. |
|
| 2432 | + * |
|
| 2433 | + * @param EE_Base_Class /int $id_or_obj |
|
| 2434 | + * @param EE_Base_Class /int $other_model_id_or_obj EE_Base_Class or ID of other Model Object |
|
| 2435 | + * @param string $relationName key in EEM_Base::_relations |
|
| 2436 | + * @return boolean of success |
|
| 2437 | + * @throws \EE_Error |
|
| 2438 | + * @param array $where_query This allows you to enter further query params for the relation to for relation to |
|
| 2439 | + * methods that allow you to further specify extra columns to join by (such as HABTM). |
|
| 2440 | + * Keep in mind that the only acceptable query_params is strict "col" => "value" pairs |
|
| 2441 | + * because these will be inserted in any new rows created as well. |
|
| 2442 | + */ |
|
| 2443 | + public function remove_relationship_to($id_or_obj, $other_model_id_or_obj, $relationName, $where_query = array()) |
|
| 2444 | + { |
|
| 2445 | + $relation_obj = $this->related_settings_for($relationName); |
|
| 2446 | + return $relation_obj->remove_relation_to($id_or_obj, $other_model_id_or_obj, $where_query); |
|
| 2447 | + } |
|
| 2448 | + |
|
| 2449 | + |
|
| 2450 | + |
|
| 2451 | + /** |
|
| 2452 | + * @param mixed $id_or_obj |
|
| 2453 | + * @param string $relationName |
|
| 2454 | + * @param array $where_query_params |
|
| 2455 | + * @param EE_Base_Class[] objects to which relations were removed |
|
| 2456 | + * @return \EE_Base_Class[] |
|
| 2457 | + * @throws \EE_Error |
|
| 2458 | + */ |
|
| 2459 | + public function remove_relations($id_or_obj, $relationName, $where_query_params = array()) |
|
| 2460 | + { |
|
| 2461 | + $relation_obj = $this->related_settings_for($relationName); |
|
| 2462 | + return $relation_obj->remove_relations($id_or_obj, $where_query_params); |
|
| 2463 | + } |
|
| 2464 | + |
|
| 2465 | + |
|
| 2466 | + |
|
| 2467 | + /** |
|
| 2468 | + * Gets all the related items of the specified $model_name, using $query_params. |
|
| 2469 | + * Note: by default, we remove the "default query params" |
|
| 2470 | + * because we want to get even deleted items etc. |
|
| 2471 | + * |
|
| 2472 | + * @param mixed $id_or_obj EE_Base_Class child or its ID |
|
| 2473 | + * @param string $model_name like 'Event', 'Registration', etc. always singular |
|
| 2474 | + * @param array $query_params like EEM_Base::get_all |
|
| 2475 | + * @return EE_Base_Class[] |
|
| 2476 | + * @throws \EE_Error |
|
| 2477 | + */ |
|
| 2478 | + public function get_all_related($id_or_obj, $model_name, $query_params = null) |
|
| 2479 | + { |
|
| 2480 | + $model_obj = $this->ensure_is_obj($id_or_obj); |
|
| 2481 | + $relation_settings = $this->related_settings_for($model_name); |
|
| 2482 | + return $relation_settings->get_all_related($model_obj, $query_params); |
|
| 2483 | + } |
|
| 2484 | + |
|
| 2485 | + |
|
| 2486 | + |
|
| 2487 | + /** |
|
| 2488 | + * Deletes all the model objects across the relation indicated by $model_name |
|
| 2489 | + * which are related to $id_or_obj which meet the criteria set in $query_params. |
|
| 2490 | + * However, if the model objects can't be deleted because of blocking related model objects, then |
|
| 2491 | + * they aren't deleted. (Unless the thing that would have been deleted can be soft-deleted, that still happens). |
|
| 2492 | + * |
|
| 2493 | + * @param EE_Base_Class|int|string $id_or_obj |
|
| 2494 | + * @param string $model_name |
|
| 2495 | + * @param array $query_params |
|
| 2496 | + * @return int how many deleted |
|
| 2497 | + * @throws \EE_Error |
|
| 2498 | + */ |
|
| 2499 | + public function delete_related($id_or_obj, $model_name, $query_params = array()) |
|
| 2500 | + { |
|
| 2501 | + $model_obj = $this->ensure_is_obj($id_or_obj); |
|
| 2502 | + $relation_settings = $this->related_settings_for($model_name); |
|
| 2503 | + return $relation_settings->delete_all_related($model_obj, $query_params); |
|
| 2504 | + } |
|
| 2505 | + |
|
| 2506 | + |
|
| 2507 | + |
|
| 2508 | + /** |
|
| 2509 | + * Hard deletes all the model objects across the relation indicated by $model_name |
|
| 2510 | + * which are related to $id_or_obj which meet the criteria set in $query_params. If |
|
| 2511 | + * the model objects can't be hard deleted because of blocking related model objects, |
|
| 2512 | + * just does a soft-delete on them instead. |
|
| 2513 | + * |
|
| 2514 | + * @param EE_Base_Class|int|string $id_or_obj |
|
| 2515 | + * @param string $model_name |
|
| 2516 | + * @param array $query_params |
|
| 2517 | + * @return int how many deleted |
|
| 2518 | + * @throws \EE_Error |
|
| 2519 | + */ |
|
| 2520 | + public function delete_related_permanently($id_or_obj, $model_name, $query_params = array()) |
|
| 2521 | + { |
|
| 2522 | + $model_obj = $this->ensure_is_obj($id_or_obj); |
|
| 2523 | + $relation_settings = $this->related_settings_for($model_name); |
|
| 2524 | + return $relation_settings->delete_related_permanently($model_obj, $query_params); |
|
| 2525 | + } |
|
| 2526 | + |
|
| 2527 | + |
|
| 2528 | + |
|
| 2529 | + /** |
|
| 2530 | + * Instead of getting the related model objects, simply counts them. Ignores default_where_conditions by default, |
|
| 2531 | + * unless otherwise specified in the $query_params |
|
| 2532 | + * |
|
| 2533 | + * @param int /EE_Base_Class $id_or_obj |
|
| 2534 | + * @param string $model_name like 'Event', or 'Registration' |
|
| 2535 | + * @param array $query_params like EEM_Base::get_all's |
|
| 2536 | + * @param string $field_to_count name of field to count by. By default, uses primary key |
|
| 2537 | + * @param bool $distinct if we want to only count the distinct values for the column then you can trigger |
|
| 2538 | + * that by the setting $distinct to TRUE; |
|
| 2539 | + * @return int |
|
| 2540 | + * @throws \EE_Error |
|
| 2541 | + */ |
|
| 2542 | + public function count_related( |
|
| 2543 | + $id_or_obj, |
|
| 2544 | + $model_name, |
|
| 2545 | + $query_params = array(), |
|
| 2546 | + $field_to_count = null, |
|
| 2547 | + $distinct = false |
|
| 2548 | + ) { |
|
| 2549 | + $related_model = $this->get_related_model_obj($model_name); |
|
| 2550 | + //we're just going to use the query params on the related model's normal get_all query, |
|
| 2551 | + //except add a condition to say to match the current mod |
|
| 2552 | + if (! isset($query_params['default_where_conditions'])) { |
|
| 2553 | + $query_params['default_where_conditions'] = EEM_Base::default_where_conditions_none; |
|
| 2554 | + } |
|
| 2555 | + $this_model_name = $this->get_this_model_name(); |
|
| 2556 | + $this_pk_field_name = $this->get_primary_key_field()->get_name(); |
|
| 2557 | + $query_params[0][$this_model_name . "." . $this_pk_field_name] = $id_or_obj; |
|
| 2558 | + return $related_model->count($query_params, $field_to_count, $distinct); |
|
| 2559 | + } |
|
| 2560 | + |
|
| 2561 | + |
|
| 2562 | + |
|
| 2563 | + /** |
|
| 2564 | + * Instead of getting the related model objects, simply sums up the values of the specified field. |
|
| 2565 | + * Note: ignores default_where_conditions by default, unless otherwise specified in the $query_params |
|
| 2566 | + * |
|
| 2567 | + * @param int /EE_Base_Class $id_or_obj |
|
| 2568 | + * @param string $model_name like 'Event', or 'Registration' |
|
| 2569 | + * @param array $query_params like EEM_Base::get_all's |
|
| 2570 | + * @param string $field_to_sum name of field to count by. By default, uses primary key |
|
| 2571 | + * @return float |
|
| 2572 | + * @throws \EE_Error |
|
| 2573 | + */ |
|
| 2574 | + public function sum_related($id_or_obj, $model_name, $query_params, $field_to_sum = null) |
|
| 2575 | + { |
|
| 2576 | + $related_model = $this->get_related_model_obj($model_name); |
|
| 2577 | + if (! is_array($query_params)) { |
|
| 2578 | + EE_Error::doing_it_wrong('EEM_Base::sum_related', |
|
| 2579 | + sprintf(__('$query_params should be an array, you passed a variable of type %s', 'event_espresso'), |
|
| 2580 | + gettype($query_params)), '4.6.0'); |
|
| 2581 | + $query_params = array(); |
|
| 2582 | + } |
|
| 2583 | + //we're just going to use the query params on the related model's normal get_all query, |
|
| 2584 | + //except add a condition to say to match the current mod |
|
| 2585 | + if (! isset($query_params['default_where_conditions'])) { |
|
| 2586 | + $query_params['default_where_conditions'] = EEM_Base::default_where_conditions_none; |
|
| 2587 | + } |
|
| 2588 | + $this_model_name = $this->get_this_model_name(); |
|
| 2589 | + $this_pk_field_name = $this->get_primary_key_field()->get_name(); |
|
| 2590 | + $query_params[0][$this_model_name . "." . $this_pk_field_name] = $id_or_obj; |
|
| 2591 | + return $related_model->sum($query_params, $field_to_sum); |
|
| 2592 | + } |
|
| 2593 | + |
|
| 2594 | + |
|
| 2595 | + |
|
| 2596 | + /** |
|
| 2597 | + * Uses $this->_relatedModels info to find the first related model object of relation $relationName to the given |
|
| 2598 | + * $modelObject |
|
| 2599 | + * |
|
| 2600 | + * @param int | EE_Base_Class $id_or_obj EE_Base_Class child or its ID |
|
| 2601 | + * @param string $other_model_name , key in $this->_relatedModels, eg 'Registration', or 'Events' |
|
| 2602 | + * @param array $query_params like EEM_Base::get_all's |
|
| 2603 | + * @return EE_Base_Class |
|
| 2604 | + * @throws \EE_Error |
|
| 2605 | + */ |
|
| 2606 | + public function get_first_related(EE_Base_Class $id_or_obj, $other_model_name, $query_params) |
|
| 2607 | + { |
|
| 2608 | + $query_params['limit'] = 1; |
|
| 2609 | + $results = $this->get_all_related($id_or_obj, $other_model_name, $query_params); |
|
| 2610 | + if ($results) { |
|
| 2611 | + return array_shift($results); |
|
| 2612 | + } else { |
|
| 2613 | + return null; |
|
| 2614 | + } |
|
| 2615 | + } |
|
| 2616 | + |
|
| 2617 | + |
|
| 2618 | + |
|
| 2619 | + /** |
|
| 2620 | + * Gets the model's name as it's expected in queries. For example, if this is EEM_Event model, that would be Event |
|
| 2621 | + * |
|
| 2622 | + * @return string |
|
| 2623 | + */ |
|
| 2624 | + public function get_this_model_name() |
|
| 2625 | + { |
|
| 2626 | + return str_replace("EEM_", "", get_class($this)); |
|
| 2627 | + } |
|
| 2628 | + |
|
| 2629 | + |
|
| 2630 | + |
|
| 2631 | + /** |
|
| 2632 | + * Gets the model field on this model which is of type EE_Any_Foreign_Model_Name_Field |
|
| 2633 | + * |
|
| 2634 | + * @return EE_Any_Foreign_Model_Name_Field |
|
| 2635 | + * @throws EE_Error |
|
| 2636 | + */ |
|
| 2637 | + public function get_field_containing_related_model_name() |
|
| 2638 | + { |
|
| 2639 | + foreach ($this->field_settings(true) as $field) { |
|
| 2640 | + if ($field instanceof EE_Any_Foreign_Model_Name_Field) { |
|
| 2641 | + $field_with_model_name = $field; |
|
| 2642 | + } |
|
| 2643 | + } |
|
| 2644 | + if (! isset($field_with_model_name) || ! $field_with_model_name) { |
|
| 2645 | + throw new EE_Error(sprintf(__("There is no EE_Any_Foreign_Model_Name field on model %s", "event_espresso"), |
|
| 2646 | + $this->get_this_model_name())); |
|
| 2647 | + } |
|
| 2648 | + return $field_with_model_name; |
|
| 2649 | + } |
|
| 2650 | + |
|
| 2651 | + |
|
| 2652 | + |
|
| 2653 | + /** |
|
| 2654 | + * Inserts a new entry into the database, for each table. |
|
| 2655 | + * Note: does not add the item to the entity map because that is done by EE_Base_Class::save() right after this. |
|
| 2656 | + * If client code uses EEM_Base::insert() directly, then although the item isn't in the entity map, |
|
| 2657 | + * we also know there is no model object with the newly inserted item's ID at the moment (because |
|
| 2658 | + * if there were, then they would already be in the DB and this would fail); and in the future if someone |
|
| 2659 | + * creates a model object with this ID (or grabs it from the DB) then it will be added to the |
|
| 2660 | + * entity map at that time anyways. SO, no need for EEM_Base::insert ot add to the entity map |
|
| 2661 | + * |
|
| 2662 | + * @param array $field_n_values keys are field names, values are their values (in the client code's domain if |
|
| 2663 | + * $values_already_prepared_by_model_object is false, in the model object's domain if |
|
| 2664 | + * $values_already_prepared_by_model_object is true. See comment about this at the top |
|
| 2665 | + * of EEM_Base) |
|
| 2666 | + * @return int new primary key on main table that got inserted |
|
| 2667 | + * @throws EE_Error |
|
| 2668 | + */ |
|
| 2669 | + public function insert($field_n_values) |
|
| 2670 | + { |
|
| 2671 | + /** |
|
| 2672 | + * Filters the fields and their values before inserting an item using the models |
|
| 2673 | + * |
|
| 2674 | + * @param array $fields_n_values keys are the fields and values are their new values |
|
| 2675 | + * @param EEM_Base $model the model used |
|
| 2676 | + */ |
|
| 2677 | + $field_n_values = (array)apply_filters('FHEE__EEM_Base__insert__fields_n_values', $field_n_values, $this); |
|
| 2678 | + if ($this->_satisfies_unique_indexes($field_n_values)) { |
|
| 2679 | + $main_table = $this->_get_main_table(); |
|
| 2680 | + $new_id = $this->_insert_into_specific_table($main_table, $field_n_values, false); |
|
| 2681 | + if ($new_id !== false) { |
|
| 2682 | + foreach ($this->_get_other_tables() as $other_table) { |
|
| 2683 | + $this->_insert_into_specific_table($other_table, $field_n_values, $new_id); |
|
| 2684 | + } |
|
| 2685 | + } |
|
| 2686 | + /** |
|
| 2687 | + * Done just after attempting to insert a new model object |
|
| 2688 | + * |
|
| 2689 | + * @param EEM_Base $model used |
|
| 2690 | + * @param array $fields_n_values fields and their values |
|
| 2691 | + * @param int|string the ID of the newly-inserted model object |
|
| 2692 | + */ |
|
| 2693 | + do_action('AHEE__EEM_Base__insert__end', $this, $field_n_values, $new_id); |
|
| 2694 | + return $new_id; |
|
| 2695 | + } else { |
|
| 2696 | + return false; |
|
| 2697 | + } |
|
| 2698 | + } |
|
| 2699 | + |
|
| 2700 | + |
|
| 2701 | + |
|
| 2702 | + /** |
|
| 2703 | + * Checks that the result would satisfy the unique indexes on this model |
|
| 2704 | + * |
|
| 2705 | + * @param array $field_n_values |
|
| 2706 | + * @param string $action |
|
| 2707 | + * @return boolean |
|
| 2708 | + * @throws \EE_Error |
|
| 2709 | + */ |
|
| 2710 | + protected function _satisfies_unique_indexes($field_n_values, $action = 'insert') |
|
| 2711 | + { |
|
| 2712 | + foreach ($this->unique_indexes() as $index_name => $index) { |
|
| 2713 | + $uniqueness_where_params = array_intersect_key($field_n_values, $index->fields()); |
|
| 2714 | + if ($this->exists(array($uniqueness_where_params))) { |
|
| 2715 | + EE_Error::add_error( |
|
| 2716 | + sprintf( |
|
| 2717 | + __( |
|
| 2718 | + "Could not %s %s. %s uniqueness index failed. Fields %s must form a unique set, but an entry already exists with values %s.", |
|
| 2719 | + "event_espresso" |
|
| 2720 | + ), |
|
| 2721 | + $action, |
|
| 2722 | + $this->_get_class_name(), |
|
| 2723 | + $index_name, |
|
| 2724 | + implode(",", $index->field_names()), |
|
| 2725 | + http_build_query($uniqueness_where_params) |
|
| 2726 | + ), |
|
| 2727 | + __FILE__, |
|
| 2728 | + __FUNCTION__, |
|
| 2729 | + __LINE__ |
|
| 2730 | + ); |
|
| 2731 | + return false; |
|
| 2732 | + } |
|
| 2733 | + } |
|
| 2734 | + return true; |
|
| 2735 | + } |
|
| 2736 | + |
|
| 2737 | + |
|
| 2738 | + |
|
| 2739 | + /** |
|
| 2740 | + * Checks the database for an item that conflicts (ie, if this item were |
|
| 2741 | + * saved to the DB would break some uniqueness requirement, like a primary key |
|
| 2742 | + * or an index primary key set) with the item specified. $id_obj_or_fields_array |
|
| 2743 | + * can be either an EE_Base_Class or an array of fields n values |
|
| 2744 | + * |
|
| 2745 | + * @param EE_Base_Class|array $obj_or_fields_array |
|
| 2746 | + * @param boolean $include_primary_key whether to use the model object's primary key |
|
| 2747 | + * when looking for conflicts |
|
| 2748 | + * (ie, if false, we ignore the model object's primary key |
|
| 2749 | + * when finding "conflicts". If true, it's also considered). |
|
| 2750 | + * Only works for INT primary key, |
|
| 2751 | + * STRING primary keys cannot be ignored |
|
| 2752 | + * @throws EE_Error |
|
| 2753 | + * @return EE_Base_Class|array |
|
| 2754 | + */ |
|
| 2755 | + public function get_one_conflicting($obj_or_fields_array, $include_primary_key = true) |
|
| 2756 | + { |
|
| 2757 | + if ($obj_or_fields_array instanceof EE_Base_Class) { |
|
| 2758 | + $fields_n_values = $obj_or_fields_array->model_field_array(); |
|
| 2759 | + } elseif (is_array($obj_or_fields_array)) { |
|
| 2760 | + $fields_n_values = $obj_or_fields_array; |
|
| 2761 | + } else { |
|
| 2762 | + throw new EE_Error( |
|
| 2763 | + sprintf( |
|
| 2764 | + __( |
|
| 2765 | + "%s get_all_conflicting should be called with a model object or an array of field names and values, you provided %d", |
|
| 2766 | + "event_espresso" |
|
| 2767 | + ), |
|
| 2768 | + get_class($this), |
|
| 2769 | + $obj_or_fields_array |
|
| 2770 | + ) |
|
| 2771 | + ); |
|
| 2772 | + } |
|
| 2773 | + $query_params = array(); |
|
| 2774 | + if ($this->has_primary_key_field() |
|
| 2775 | + && ($include_primary_key |
|
| 2776 | + || $this->get_primary_key_field() |
|
| 2777 | + instanceof |
|
| 2778 | + EE_Primary_Key_String_Field) |
|
| 2779 | + && isset($fields_n_values[$this->primary_key_name()]) |
|
| 2780 | + ) { |
|
| 2781 | + $query_params[0]['OR'][$this->primary_key_name()] = $fields_n_values[$this->primary_key_name()]; |
|
| 2782 | + } |
|
| 2783 | + foreach ($this->unique_indexes() as $unique_index_name => $unique_index) { |
|
| 2784 | + $uniqueness_where_params = array_intersect_key($fields_n_values, $unique_index->fields()); |
|
| 2785 | + $query_params[0]['OR']['AND*' . $unique_index_name] = $uniqueness_where_params; |
|
| 2786 | + } |
|
| 2787 | + //if there is nothing to base this search on, then we shouldn't find anything |
|
| 2788 | + if (empty($query_params)) { |
|
| 2789 | + return array(); |
|
| 2790 | + } else { |
|
| 2791 | + return $this->get_one($query_params); |
|
| 2792 | + } |
|
| 2793 | + } |
|
| 2794 | + |
|
| 2795 | + |
|
| 2796 | + |
|
| 2797 | + /** |
|
| 2798 | + * Like count, but is optimized and returns a boolean instead of an int |
|
| 2799 | + * |
|
| 2800 | + * @param array $query_params |
|
| 2801 | + * @return boolean |
|
| 2802 | + * @throws \EE_Error |
|
| 2803 | + */ |
|
| 2804 | + public function exists($query_params) |
|
| 2805 | + { |
|
| 2806 | + $query_params['limit'] = 1; |
|
| 2807 | + return $this->count($query_params) > 0; |
|
| 2808 | + } |
|
| 2809 | + |
|
| 2810 | + |
|
| 2811 | + |
|
| 2812 | + /** |
|
| 2813 | + * Wrapper for exists, except ignores default query parameters so we're only considering ID |
|
| 2814 | + * |
|
| 2815 | + * @param int|string $id |
|
| 2816 | + * @return boolean |
|
| 2817 | + * @throws \EE_Error |
|
| 2818 | + */ |
|
| 2819 | + public function exists_by_ID($id) |
|
| 2820 | + { |
|
| 2821 | + return $this->exists( |
|
| 2822 | + array( |
|
| 2823 | + 'default_where_conditions' => EEM_Base::default_where_conditions_none, |
|
| 2824 | + array( |
|
| 2825 | + $this->primary_key_name() => $id, |
|
| 2826 | + ), |
|
| 2827 | + ) |
|
| 2828 | + ); |
|
| 2829 | + } |
|
| 2830 | + |
|
| 2831 | + |
|
| 2832 | + |
|
| 2833 | + /** |
|
| 2834 | + * Inserts a new row in $table, using the $cols_n_values which apply to that table. |
|
| 2835 | + * If a $new_id is supplied and if $table is an EE_Other_Table, we assume |
|
| 2836 | + * we need to add a foreign key column to point to $new_id (which should be the primary key's value |
|
| 2837 | + * on the main table) |
|
| 2838 | + * This is protected rather than private because private is not accessible to any child methods and there MAY be |
|
| 2839 | + * cases where we want to call it directly rather than via insert(). |
|
| 2840 | + * |
|
| 2841 | + * @access protected |
|
| 2842 | + * @param EE_Table_Base $table |
|
| 2843 | + * @param array $fields_n_values each key should be in field's keys, and value should be an int, string or |
|
| 2844 | + * float |
|
| 2845 | + * @param int $new_id for now we assume only int keys |
|
| 2846 | + * @throws EE_Error |
|
| 2847 | + * @global WPDB $wpdb only used to get the $wpdb->insert_id after performing an insert |
|
| 2848 | + * @return int ID of new row inserted, or FALSE on failure |
|
| 2849 | + */ |
|
| 2850 | + protected function _insert_into_specific_table(EE_Table_Base $table, $fields_n_values, $new_id = 0) |
|
| 2851 | + { |
|
| 2852 | + global $wpdb; |
|
| 2853 | + $insertion_col_n_values = array(); |
|
| 2854 | + $format_for_insertion = array(); |
|
| 2855 | + $fields_on_table = $this->_get_fields_for_table($table->get_table_alias()); |
|
| 2856 | + foreach ($fields_on_table as $field_name => $field_obj) { |
|
| 2857 | + //check if its an auto-incrementing column, in which case we should just leave it to do its autoincrement thing |
|
| 2858 | + if ($field_obj->is_auto_increment()) { |
|
| 2859 | + continue; |
|
| 2860 | + } |
|
| 2861 | + $prepared_value = $this->_prepare_value_or_use_default($field_obj, $fields_n_values); |
|
| 2862 | + //if the value we want to assign it to is NULL, just don't mention it for the insertion |
|
| 2863 | + if ($prepared_value !== null) { |
|
| 2864 | + $insertion_col_n_values[$field_obj->get_table_column()] = $prepared_value; |
|
| 2865 | + $format_for_insertion[] = $field_obj->get_wpdb_data_type(); |
|
| 2866 | + } |
|
| 2867 | + } |
|
| 2868 | + if ($table instanceof EE_Secondary_Table && $new_id) { |
|
| 2869 | + //its not the main table, so we should have already saved the main table's PK which we just inserted |
|
| 2870 | + //so add the fk to the main table as a column |
|
| 2871 | + $insertion_col_n_values[$table->get_fk_on_table()] = $new_id; |
|
| 2872 | + $format_for_insertion[] = '%d';//yes right now we're only allowing these foreign keys to be INTs |
|
| 2873 | + } |
|
| 2874 | + //insert the new entry |
|
| 2875 | + $result = $this->_do_wpdb_query('insert', |
|
| 2876 | + array($table->get_table_name(), $insertion_col_n_values, $format_for_insertion)); |
|
| 2877 | + if ($result === false) { |
|
| 2878 | + return false; |
|
| 2879 | + } |
|
| 2880 | + //ok, now what do we return for the ID of the newly-inserted thing? |
|
| 2881 | + if ($this->has_primary_key_field()) { |
|
| 2882 | + if ($this->get_primary_key_field()->is_auto_increment()) { |
|
| 2883 | + return $wpdb->insert_id; |
|
| 2884 | + } else { |
|
| 2885 | + //it's not an auto-increment primary key, so |
|
| 2886 | + //it must have been supplied |
|
| 2887 | + return $fields_n_values[$this->get_primary_key_field()->get_name()]; |
|
| 2888 | + } |
|
| 2889 | + } else { |
|
| 2890 | + //we can't return a primary key because there is none. instead return |
|
| 2891 | + //a unique string indicating this model |
|
| 2892 | + return $this->get_index_primary_key_string($fields_n_values); |
|
| 2893 | + } |
|
| 2894 | + } |
|
| 2895 | + |
|
| 2896 | + |
|
| 2897 | + |
|
| 2898 | + /** |
|
| 2899 | + * Prepare the $field_obj 's value in $fields_n_values for use in the database. |
|
| 2900 | + * If the field doesn't allow NULL, try to use its default. (If it doesn't allow NULL, |
|
| 2901 | + * and there is no default, we pass it along. WPDB will take care of it) |
|
| 2902 | + * |
|
| 2903 | + * @param EE_Model_Field_Base $field_obj |
|
| 2904 | + * @param array $fields_n_values |
|
| 2905 | + * @return mixed string|int|float depending on what the table column will be expecting |
|
| 2906 | + * @throws \EE_Error |
|
| 2907 | + */ |
|
| 2908 | + protected function _prepare_value_or_use_default($field_obj, $fields_n_values) |
|
| 2909 | + { |
|
| 2910 | + //if this field doesn't allow nullable, don't allow it |
|
| 2911 | + if (! $field_obj->is_nullable() |
|
| 2912 | + && ( |
|
| 2913 | + ! isset($fields_n_values[$field_obj->get_name()]) || $fields_n_values[$field_obj->get_name()] === null) |
|
| 2914 | + ) { |
|
| 2915 | + $fields_n_values[$field_obj->get_name()] = $field_obj->get_default_value(); |
|
| 2916 | + } |
|
| 2917 | + $unprepared_value = isset($fields_n_values[$field_obj->get_name()]) ? $fields_n_values[$field_obj->get_name()] |
|
| 2918 | + : null; |
|
| 2919 | + return $this->_prepare_value_for_use_in_db($unprepared_value, $field_obj); |
|
| 2920 | + } |
|
| 2921 | + |
|
| 2922 | + |
|
| 2923 | + |
|
| 2924 | + /** |
|
| 2925 | + * Consolidates code for preparing a value supplied to the model for use int eh db. Calls the field's |
|
| 2926 | + * prepare_for_use_in_db method on the value, and depending on $value_already_prepare_by_model_obj, may also call |
|
| 2927 | + * the field's prepare_for_set() method. |
|
| 2928 | + * |
|
| 2929 | + * @param mixed $value value in the client code domain if $value_already_prepared_by_model_object is |
|
| 2930 | + * false, otherwise a value in the model object's domain (see lengthy comment at |
|
| 2931 | + * top of file) |
|
| 2932 | + * @param EE_Model_Field_Base $field field which will be doing the preparing of the value. If null, we assume |
|
| 2933 | + * $value is a custom selection |
|
| 2934 | + * @return mixed a value ready for use in the database for insertions, updating, or in a where clause |
|
| 2935 | + */ |
|
| 2936 | + private function _prepare_value_for_use_in_db($value, $field) |
|
| 2937 | + { |
|
| 2938 | + if ($field && $field instanceof EE_Model_Field_Base) { |
|
| 2939 | + switch ($this->_values_already_prepared_by_model_object) { |
|
| 2940 | + /** @noinspection PhpMissingBreakStatementInspection */ |
|
| 2941 | + case self::not_prepared_by_model_object: |
|
| 2942 | + $value = $field->prepare_for_set($value); |
|
| 2943 | + //purposefully left out "return" |
|
| 2944 | + case self::prepared_by_model_object: |
|
| 2945 | + $value = $field->prepare_for_use_in_db($value); |
|
| 2946 | + case self::prepared_for_use_in_db: |
|
| 2947 | + //leave the value alone |
|
| 2948 | + } |
|
| 2949 | + return $value; |
|
| 2950 | + } else { |
|
| 2951 | + return $value; |
|
| 2952 | + } |
|
| 2953 | + } |
|
| 2954 | + |
|
| 2955 | + |
|
| 2956 | + |
|
| 2957 | + /** |
|
| 2958 | + * Returns the main table on this model |
|
| 2959 | + * |
|
| 2960 | + * @return EE_Primary_Table |
|
| 2961 | + * @throws EE_Error |
|
| 2962 | + */ |
|
| 2963 | + protected function _get_main_table() |
|
| 2964 | + { |
|
| 2965 | + foreach ($this->_tables as $table) { |
|
| 2966 | + if ($table instanceof EE_Primary_Table) { |
|
| 2967 | + return $table; |
|
| 2968 | + } |
|
| 2969 | + } |
|
| 2970 | + throw new EE_Error(sprintf(__('There are no main tables on %s. They should be added to _tables array in the constructor', |
|
| 2971 | + 'event_espresso'), get_class($this))); |
|
| 2972 | + } |
|
| 2973 | + |
|
| 2974 | + |
|
| 2975 | + |
|
| 2976 | + /** |
|
| 2977 | + * table |
|
| 2978 | + * returns EE_Primary_Table table name |
|
| 2979 | + * |
|
| 2980 | + * @return string |
|
| 2981 | + * @throws \EE_Error |
|
| 2982 | + */ |
|
| 2983 | + public function table() |
|
| 2984 | + { |
|
| 2985 | + return $this->_get_main_table()->get_table_name(); |
|
| 2986 | + } |
|
| 2987 | + |
|
| 2988 | + |
|
| 2989 | + |
|
| 2990 | + /** |
|
| 2991 | + * table |
|
| 2992 | + * returns first EE_Secondary_Table table name |
|
| 2993 | + * |
|
| 2994 | + * @return string |
|
| 2995 | + */ |
|
| 2996 | + public function second_table() |
|
| 2997 | + { |
|
| 2998 | + // grab second table from tables array |
|
| 2999 | + $second_table = end($this->_tables); |
|
| 3000 | + return $second_table instanceof EE_Secondary_Table ? $second_table->get_table_name() : null; |
|
| 3001 | + } |
|
| 3002 | + |
|
| 3003 | + |
|
| 3004 | + |
|
| 3005 | + /** |
|
| 3006 | + * get_table_obj_by_alias |
|
| 3007 | + * returns table name given it's alias |
|
| 3008 | + * |
|
| 3009 | + * @param string $table_alias |
|
| 3010 | + * @return EE_Primary_Table | EE_Secondary_Table |
|
| 3011 | + */ |
|
| 3012 | + public function get_table_obj_by_alias($table_alias = '') |
|
| 3013 | + { |
|
| 3014 | + return isset($this->_tables[$table_alias]) ? $this->_tables[$table_alias] : null; |
|
| 3015 | + } |
|
| 3016 | + |
|
| 3017 | + |
|
| 3018 | + |
|
| 3019 | + /** |
|
| 3020 | + * Gets all the tables of type EE_Other_Table from EEM_CPT_Basel_Model::_tables |
|
| 3021 | + * |
|
| 3022 | + * @return EE_Secondary_Table[] |
|
| 3023 | + */ |
|
| 3024 | + protected function _get_other_tables() |
|
| 3025 | + { |
|
| 3026 | + $other_tables = array(); |
|
| 3027 | + foreach ($this->_tables as $table_alias => $table) { |
|
| 3028 | + if ($table instanceof EE_Secondary_Table) { |
|
| 3029 | + $other_tables[$table_alias] = $table; |
|
| 3030 | + } |
|
| 3031 | + } |
|
| 3032 | + return $other_tables; |
|
| 3033 | + } |
|
| 3034 | + |
|
| 3035 | + |
|
| 3036 | + |
|
| 3037 | + /** |
|
| 3038 | + * Finds all the fields that correspond to the given table |
|
| 3039 | + * |
|
| 3040 | + * @param string $table_alias , array key in EEM_Base::_tables |
|
| 3041 | + * @return EE_Model_Field_Base[] |
|
| 3042 | + */ |
|
| 3043 | + public function _get_fields_for_table($table_alias) |
|
| 3044 | + { |
|
| 3045 | + return $this->_fields[$table_alias]; |
|
| 3046 | + } |
|
| 3047 | + |
|
| 3048 | + |
|
| 3049 | + |
|
| 3050 | + /** |
|
| 3051 | + * Recurses through all the where parameters, and finds all the related models we'll need |
|
| 3052 | + * to complete this query. Eg, given where parameters like array('EVT_ID'=>3) from within Event model, we won't |
|
| 3053 | + * need any related models. But if the array were array('Registrations.REG_ID'=>3), we'd need the related |
|
| 3054 | + * Registration model. If it were array('Registrations.Transactions.Payments.PAY_ID'=>3), then we'd need the |
|
| 3055 | + * related Registration, Transaction, and Payment models. |
|
| 3056 | + * |
|
| 3057 | + * @param array $query_params like EEM_Base::get_all's $query_parameters['where'] |
|
| 3058 | + * @return EE_Model_Query_Info_Carrier |
|
| 3059 | + * @throws \EE_Error |
|
| 3060 | + */ |
|
| 3061 | + public function _extract_related_models_from_query($query_params) |
|
| 3062 | + { |
|
| 3063 | + $query_info_carrier = new EE_Model_Query_Info_Carrier(); |
|
| 3064 | + if (array_key_exists(0, $query_params)) { |
|
| 3065 | + $this->_extract_related_models_from_sub_params_array_keys($query_params[0], $query_info_carrier, 0); |
|
| 3066 | + } |
|
| 3067 | + if (array_key_exists('group_by', $query_params)) { |
|
| 3068 | + if (is_array($query_params['group_by'])) { |
|
| 3069 | + $this->_extract_related_models_from_sub_params_array_values( |
|
| 3070 | + $query_params['group_by'], |
|
| 3071 | + $query_info_carrier, |
|
| 3072 | + 'group_by' |
|
| 3073 | + ); |
|
| 3074 | + } elseif (! empty ($query_params['group_by'])) { |
|
| 3075 | + $this->_extract_related_model_info_from_query_param( |
|
| 3076 | + $query_params['group_by'], |
|
| 3077 | + $query_info_carrier, |
|
| 3078 | + 'group_by' |
|
| 3079 | + ); |
|
| 3080 | + } |
|
| 3081 | + } |
|
| 3082 | + if (array_key_exists('having', $query_params)) { |
|
| 3083 | + $this->_extract_related_models_from_sub_params_array_keys( |
|
| 3084 | + $query_params[0], |
|
| 3085 | + $query_info_carrier, |
|
| 3086 | + 'having' |
|
| 3087 | + ); |
|
| 3088 | + } |
|
| 3089 | + if (array_key_exists('order_by', $query_params)) { |
|
| 3090 | + if (is_array($query_params['order_by'])) { |
|
| 3091 | + $this->_extract_related_models_from_sub_params_array_keys( |
|
| 3092 | + $query_params['order_by'], |
|
| 3093 | + $query_info_carrier, |
|
| 3094 | + 'order_by' |
|
| 3095 | + ); |
|
| 3096 | + } elseif (! empty($query_params['order_by'])) { |
|
| 3097 | + $this->_extract_related_model_info_from_query_param( |
|
| 3098 | + $query_params['order_by'], |
|
| 3099 | + $query_info_carrier, |
|
| 3100 | + 'order_by' |
|
| 3101 | + ); |
|
| 3102 | + } |
|
| 3103 | + } |
|
| 3104 | + if (array_key_exists('force_join', $query_params)) { |
|
| 3105 | + $this->_extract_related_models_from_sub_params_array_values( |
|
| 3106 | + $query_params['force_join'], |
|
| 3107 | + $query_info_carrier, |
|
| 3108 | + 'force_join' |
|
| 3109 | + ); |
|
| 3110 | + } |
|
| 3111 | + return $query_info_carrier; |
|
| 3112 | + } |
|
| 3113 | + |
|
| 3114 | + |
|
| 3115 | + |
|
| 3116 | + /** |
|
| 3117 | + * For extracting related models from WHERE (0), HAVING (having), ORDER BY (order_by) or forced joins (force_join) |
|
| 3118 | + * |
|
| 3119 | + * @param array $sub_query_params like EEM_Base::get_all's $query_params[0] or |
|
| 3120 | + * $query_params['having'] |
|
| 3121 | + * @param EE_Model_Query_Info_Carrier $model_query_info_carrier |
|
| 3122 | + * @param string $query_param_type one of $this->_allowed_query_params |
|
| 3123 | + * @throws EE_Error |
|
| 3124 | + * @return \EE_Model_Query_Info_Carrier |
|
| 3125 | + */ |
|
| 3126 | + private function _extract_related_models_from_sub_params_array_keys( |
|
| 3127 | + $sub_query_params, |
|
| 3128 | + EE_Model_Query_Info_Carrier $model_query_info_carrier, |
|
| 3129 | + $query_param_type |
|
| 3130 | + ) { |
|
| 3131 | + if (! empty($sub_query_params)) { |
|
| 3132 | + $sub_query_params = (array)$sub_query_params; |
|
| 3133 | + foreach ($sub_query_params as $param => $possibly_array_of_params) { |
|
| 3134 | + //$param could be simply 'EVT_ID', or it could be 'Registrations.REG_ID', or even 'Registrations.Transactions.Payments.PAY_amount' |
|
| 3135 | + $this->_extract_related_model_info_from_query_param($param, $model_query_info_carrier, |
|
| 3136 | + $query_param_type); |
|
| 3137 | + //if $possibly_array_of_params is an array, try recursing into it, searching for keys which |
|
| 3138 | + //indicate needed joins. Eg, array('NOT'=>array('Registration.TXN_ID'=>23)). In this case, we tried |
|
| 3139 | + //extracting models out of the 'NOT', which obviously wasn't successful, and then we recurse into the value |
|
| 3140 | + //of array('Registration.TXN_ID'=>23) |
|
| 3141 | + $query_param_sans_stars = $this->_remove_stars_and_anything_after_from_condition_query_param_key($param); |
|
| 3142 | + if (in_array($query_param_sans_stars, $this->_logic_query_param_keys, true)) { |
|
| 3143 | + if (! is_array($possibly_array_of_params)) { |
|
| 3144 | + throw new EE_Error(sprintf(__("You used a special where query param %s, but the value isn't an array of where query params, it's just %s'. It should be an array, eg array('EVT_ID'=>23,'OR'=>array('Venue.VNU_ID'=>32,'Venue.VNU_name'=>'monkey_land'))", |
|
| 3145 | + "event_espresso"), |
|
| 3146 | + $param, $possibly_array_of_params)); |
|
| 3147 | + } else { |
|
| 3148 | + $this->_extract_related_models_from_sub_params_array_keys($possibly_array_of_params, |
|
| 3149 | + $model_query_info_carrier, $query_param_type); |
|
| 3150 | + } |
|
| 3151 | + } elseif ($query_param_type === 0 //ie WHERE |
|
| 3152 | + && is_array($possibly_array_of_params) |
|
| 3153 | + && isset($possibly_array_of_params[2]) |
|
| 3154 | + && $possibly_array_of_params[2] == true |
|
| 3155 | + ) { |
|
| 3156 | + //then $possible_array_of_params looks something like array('<','DTT_sold',true) |
|
| 3157 | + //indicating that $possible_array_of_params[1] is actually a field name, |
|
| 3158 | + //from which we should extract query parameters! |
|
| 3159 | + if (! isset($possibly_array_of_params[0], $possibly_array_of_params[1])) { |
|
| 3160 | + throw new EE_Error(sprintf(__("Improperly formed query parameter %s. It should be numerically indexed like array('<','DTT_sold',true); but you provided %s", |
|
| 3161 | + "event_espresso"), $query_param_type, implode(",", $possibly_array_of_params))); |
|
| 3162 | + } |
|
| 3163 | + $this->_extract_related_model_info_from_query_param($possibly_array_of_params[1], |
|
| 3164 | + $model_query_info_carrier, $query_param_type); |
|
| 3165 | + } |
|
| 3166 | + } |
|
| 3167 | + } |
|
| 3168 | + return $model_query_info_carrier; |
|
| 3169 | + } |
|
| 3170 | + |
|
| 3171 | + |
|
| 3172 | + |
|
| 3173 | + /** |
|
| 3174 | + * For extracting related models from forced_joins, where the array values contain the info about what |
|
| 3175 | + * models to join with. Eg an array like array('Attendee','Price.Price_Type'); |
|
| 3176 | + * |
|
| 3177 | + * @param array $sub_query_params like EEM_Base::get_all's $query_params[0] or |
|
| 3178 | + * $query_params['having'] |
|
| 3179 | + * @param EE_Model_Query_Info_Carrier $model_query_info_carrier |
|
| 3180 | + * @param string $query_param_type one of $this->_allowed_query_params |
|
| 3181 | + * @throws EE_Error |
|
| 3182 | + * @return \EE_Model_Query_Info_Carrier |
|
| 3183 | + */ |
|
| 3184 | + private function _extract_related_models_from_sub_params_array_values( |
|
| 3185 | + $sub_query_params, |
|
| 3186 | + EE_Model_Query_Info_Carrier $model_query_info_carrier, |
|
| 3187 | + $query_param_type |
|
| 3188 | + ) { |
|
| 3189 | + if (! empty($sub_query_params)) { |
|
| 3190 | + if (! is_array($sub_query_params)) { |
|
| 3191 | + throw new EE_Error(sprintf(__("Query parameter %s should be an array, but it isn't.", "event_espresso"), |
|
| 3192 | + $sub_query_params)); |
|
| 3193 | + } |
|
| 3194 | + foreach ($sub_query_params as $param) { |
|
| 3195 | + //$param could be simply 'EVT_ID', or it could be 'Registrations.REG_ID', or even 'Registrations.Transactions.Payments.PAY_amount' |
|
| 3196 | + $this->_extract_related_model_info_from_query_param($param, $model_query_info_carrier, |
|
| 3197 | + $query_param_type); |
|
| 3198 | + } |
|
| 3199 | + } |
|
| 3200 | + return $model_query_info_carrier; |
|
| 3201 | + } |
|
| 3202 | + |
|
| 3203 | + |
|
| 3204 | + |
|
| 3205 | + /** |
|
| 3206 | + * Extract all the query parts from $query_params (an array like whats passed to EEM_Base::get_all) |
|
| 3207 | + * and put into a EEM_Related_Model_Info_Carrier for easy extraction into a query. We create this object |
|
| 3208 | + * instead of directly constructing the SQL because often we need to extract info from the $query_params |
|
| 3209 | + * but use them in a different order. Eg, we need to know what models we are querying |
|
| 3210 | + * before we know what joins to perform. However, we need to know what data types correspond to which fields on |
|
| 3211 | + * other models before we can finalize the where clause SQL. |
|
| 3212 | + * |
|
| 3213 | + * @param array $query_params |
|
| 3214 | + * @throws EE_Error |
|
| 3215 | + * @return EE_Model_Query_Info_Carrier |
|
| 3216 | + */ |
|
| 3217 | + public function _create_model_query_info_carrier($query_params) |
|
| 3218 | + { |
|
| 3219 | + if (! is_array($query_params)) { |
|
| 3220 | + EE_Error::doing_it_wrong( |
|
| 3221 | + 'EEM_Base::_create_model_query_info_carrier', |
|
| 3222 | + sprintf( |
|
| 3223 | + __( |
|
| 3224 | + '$query_params should be an array, you passed a variable of type %s', |
|
| 3225 | + 'event_espresso' |
|
| 3226 | + ), |
|
| 3227 | + gettype($query_params) |
|
| 3228 | + ), |
|
| 3229 | + '4.6.0' |
|
| 3230 | + ); |
|
| 3231 | + $query_params = array(); |
|
| 3232 | + } |
|
| 3233 | + $where_query_params = isset($query_params[0]) ? $query_params[0] : array(); |
|
| 3234 | + //first check if we should alter the query to account for caps or not |
|
| 3235 | + //because the caps might require us to do extra joins |
|
| 3236 | + if (isset($query_params['caps']) && $query_params['caps'] !== 'none') { |
|
| 3237 | + $query_params[0] = $where_query_params = array_replace_recursive( |
|
| 3238 | + $where_query_params, |
|
| 3239 | + $this->caps_where_conditions( |
|
| 3240 | + $query_params['caps'] |
|
| 3241 | + ) |
|
| 3242 | + ); |
|
| 3243 | + } |
|
| 3244 | + $query_object = $this->_extract_related_models_from_query($query_params); |
|
| 3245 | + //verify where_query_params has NO numeric indexes.... that's simply not how you use it! |
|
| 3246 | + foreach ($where_query_params as $key => $value) { |
|
| 3247 | + if (is_int($key)) { |
|
| 3248 | + throw new EE_Error( |
|
| 3249 | + sprintf( |
|
| 3250 | + __( |
|
| 3251 | + "WHERE query params must NOT be numerically-indexed. You provided the array key '%s' for value '%s' while querying model %s. All the query params provided were '%s' Please read documentation on EEM_Base::get_all.", |
|
| 3252 | + "event_espresso" |
|
| 3253 | + ), |
|
| 3254 | + $key, |
|
| 3255 | + var_export($value, true), |
|
| 3256 | + var_export($query_params, true), |
|
| 3257 | + get_class($this) |
|
| 3258 | + ) |
|
| 3259 | + ); |
|
| 3260 | + } |
|
| 3261 | + } |
|
| 3262 | + if ( |
|
| 3263 | + array_key_exists('default_where_conditions', $query_params) |
|
| 3264 | + && ! empty($query_params['default_where_conditions']) |
|
| 3265 | + ) { |
|
| 3266 | + $use_default_where_conditions = $query_params['default_where_conditions']; |
|
| 3267 | + } else { |
|
| 3268 | + $use_default_where_conditions = EEM_Base::default_where_conditions_all; |
|
| 3269 | + } |
|
| 3270 | + $where_query_params = array_merge( |
|
| 3271 | + $this->_get_default_where_conditions_for_models_in_query( |
|
| 3272 | + $query_object, |
|
| 3273 | + $use_default_where_conditions, |
|
| 3274 | + $where_query_params |
|
| 3275 | + ), |
|
| 3276 | + $where_query_params |
|
| 3277 | + ); |
|
| 3278 | + $query_object->set_where_sql($this->_construct_where_clause($where_query_params)); |
|
| 3279 | + // if this is a "on_join_limit" then we are limiting on on a specific table in a multi_table join. |
|
| 3280 | + // So we need to setup a subquery and use that for the main join. |
|
| 3281 | + // Note for now this only works on the primary table for the model. |
|
| 3282 | + // So for instance, you could set the limit array like this: |
|
| 3283 | + // array( 'on_join_limit' => array('Primary_Table_Alias', array(1,10) ) ) |
|
| 3284 | + if (array_key_exists('on_join_limit', $query_params) && ! empty($query_params['on_join_limit'])) { |
|
| 3285 | + $query_object->set_main_model_join_sql( |
|
| 3286 | + $this->_construct_limit_join_select( |
|
| 3287 | + $query_params['on_join_limit'][0], |
|
| 3288 | + $query_params['on_join_limit'][1] |
|
| 3289 | + ) |
|
| 3290 | + ); |
|
| 3291 | + } |
|
| 3292 | + //set limit |
|
| 3293 | + if (array_key_exists('limit', $query_params)) { |
|
| 3294 | + if (is_array($query_params['limit'])) { |
|
| 3295 | + if (! isset($query_params['limit'][0], $query_params['limit'][1])) { |
|
| 3296 | + $e = sprintf( |
|
| 3297 | + __( |
|
| 3298 | + "Invalid DB query. You passed '%s' for the LIMIT, but only the following are valid: an integer, string representing an integer, a string like 'int,int', or an array like array(int,int)", |
|
| 3299 | + "event_espresso" |
|
| 3300 | + ), |
|
| 3301 | + http_build_query($query_params['limit']) |
|
| 3302 | + ); |
|
| 3303 | + throw new EE_Error($e . "|" . $e); |
|
| 3304 | + } |
|
| 3305 | + //they passed us an array for the limit. Assume it's like array(50,25), meaning offset by 50, and get 25 |
|
| 3306 | + $query_object->set_limit_sql(" LIMIT " . $query_params['limit'][0] . "," . $query_params['limit'][1]); |
|
| 3307 | + } elseif (! empty ($query_params['limit'])) { |
|
| 3308 | + $query_object->set_limit_sql(" LIMIT " . $query_params['limit']); |
|
| 3309 | + } |
|
| 3310 | + } |
|
| 3311 | + //set order by |
|
| 3312 | + if (array_key_exists('order_by', $query_params)) { |
|
| 3313 | + if (is_array($query_params['order_by'])) { |
|
| 3314 | + //if they're using 'order_by' as an array, they can't use 'order' (because 'order_by' must |
|
| 3315 | + //specify whether to ascend or descend on each field. Eg 'order_by'=>array('EVT_ID'=>'ASC'). So |
|
| 3316 | + //including 'order' wouldn't make any sense if 'order_by' has already specified which way to order! |
|
| 3317 | + if (array_key_exists('order', $query_params)) { |
|
| 3318 | + throw new EE_Error( |
|
| 3319 | + sprintf( |
|
| 3320 | + __( |
|
| 3321 | + "In querying %s, we are using query parameter 'order_by' as an array (keys:%s,values:%s), and so we can't use query parameter 'order' (value %s). You should just use the 'order_by' parameter ", |
|
| 3322 | + "event_espresso" |
|
| 3323 | + ), |
|
| 3324 | + get_class($this), |
|
| 3325 | + implode(", ", array_keys($query_params['order_by'])), |
|
| 3326 | + implode(", ", $query_params['order_by']), |
|
| 3327 | + $query_params['order'] |
|
| 3328 | + ) |
|
| 3329 | + ); |
|
| 3330 | + } |
|
| 3331 | + $this->_extract_related_models_from_sub_params_array_keys( |
|
| 3332 | + $query_params['order_by'], |
|
| 3333 | + $query_object, |
|
| 3334 | + 'order_by' |
|
| 3335 | + ); |
|
| 3336 | + //assume it's an array of fields to order by |
|
| 3337 | + $order_array = array(); |
|
| 3338 | + foreach ($query_params['order_by'] as $field_name_to_order_by => $order) { |
|
| 3339 | + $order = $this->_extract_order($order); |
|
| 3340 | + $order_array[] = $this->_deduce_column_name_from_query_param($field_name_to_order_by) . SP . $order; |
|
| 3341 | + } |
|
| 3342 | + $query_object->set_order_by_sql(" ORDER BY " . implode(",", $order_array)); |
|
| 3343 | + } elseif (! empty ($query_params['order_by'])) { |
|
| 3344 | + $this->_extract_related_model_info_from_query_param( |
|
| 3345 | + $query_params['order_by'], |
|
| 3346 | + $query_object, |
|
| 3347 | + 'order', |
|
| 3348 | + $query_params['order_by'] |
|
| 3349 | + ); |
|
| 3350 | + $order = isset($query_params['order']) |
|
| 3351 | + ? $this->_extract_order($query_params['order']) |
|
| 3352 | + : 'DESC'; |
|
| 3353 | + $query_object->set_order_by_sql( |
|
| 3354 | + " ORDER BY " . $this->_deduce_column_name_from_query_param($query_params['order_by']) . SP . $order |
|
| 3355 | + ); |
|
| 3356 | + } |
|
| 3357 | + } |
|
| 3358 | + //if 'order_by' wasn't set, maybe they are just using 'order' on its own? |
|
| 3359 | + if (! array_key_exists('order_by', $query_params) |
|
| 3360 | + && array_key_exists('order', $query_params) |
|
| 3361 | + && ! empty($query_params['order']) |
|
| 3362 | + ) { |
|
| 3363 | + $pk_field = $this->get_primary_key_field(); |
|
| 3364 | + $order = $this->_extract_order($query_params['order']); |
|
| 3365 | + $query_object->set_order_by_sql(" ORDER BY " . $pk_field->get_qualified_column() . SP . $order); |
|
| 3366 | + } |
|
| 3367 | + //set group by |
|
| 3368 | + if (array_key_exists('group_by', $query_params)) { |
|
| 3369 | + if (is_array($query_params['group_by'])) { |
|
| 3370 | + //it's an array, so assume we'll be grouping by a bunch of stuff |
|
| 3371 | + $group_by_array = array(); |
|
| 3372 | + foreach ($query_params['group_by'] as $field_name_to_group_by) { |
|
| 3373 | + $group_by_array[] = $this->_deduce_column_name_from_query_param($field_name_to_group_by); |
|
| 3374 | + } |
|
| 3375 | + $query_object->set_group_by_sql(" GROUP BY " . implode(", ", $group_by_array)); |
|
| 3376 | + } elseif (! empty ($query_params['group_by'])) { |
|
| 3377 | + $query_object->set_group_by_sql( |
|
| 3378 | + " GROUP BY " . $this->_deduce_column_name_from_query_param($query_params['group_by']) |
|
| 3379 | + ); |
|
| 3380 | + } |
|
| 3381 | + } |
|
| 3382 | + //set having |
|
| 3383 | + if (array_key_exists('having', $query_params) && $query_params['having']) { |
|
| 3384 | + $query_object->set_having_sql($this->_construct_having_clause($query_params['having'])); |
|
| 3385 | + } |
|
| 3386 | + //now, just verify they didn't pass anything wack |
|
| 3387 | + foreach ($query_params as $query_key => $query_value) { |
|
| 3388 | + if (! in_array($query_key, $this->_allowed_query_params, true)) { |
|
| 3389 | + throw new EE_Error( |
|
| 3390 | + sprintf( |
|
| 3391 | + __( |
|
| 3392 | + "You passed %s as a query parameter to %s, which is illegal! The allowed query parameters are %s", |
|
| 3393 | + 'event_espresso' |
|
| 3394 | + ), |
|
| 3395 | + $query_key, |
|
| 3396 | + get_class($this), |
|
| 3397 | + // print_r( $this->_allowed_query_params, TRUE ) |
|
| 3398 | + implode(',', $this->_allowed_query_params) |
|
| 3399 | + ) |
|
| 3400 | + ); |
|
| 3401 | + } |
|
| 3402 | + } |
|
| 3403 | + $main_model_join_sql = $query_object->get_main_model_join_sql(); |
|
| 3404 | + if (empty($main_model_join_sql)) { |
|
| 3405 | + $query_object->set_main_model_join_sql($this->_construct_internal_join()); |
|
| 3406 | + } |
|
| 3407 | + return $query_object; |
|
| 3408 | + } |
|
| 3409 | + |
|
| 3410 | + |
|
| 3411 | + |
|
| 3412 | + /** |
|
| 3413 | + * Gets the where conditions that should be imposed on the query based on the |
|
| 3414 | + * context (eg reading frontend, backend, edit or delete). |
|
| 3415 | + * |
|
| 3416 | + * @param string $context one of EEM_Base::valid_cap_contexts() |
|
| 3417 | + * @return array like EEM_Base::get_all() 's $query_params[0] |
|
| 3418 | + * @throws \EE_Error |
|
| 3419 | + */ |
|
| 3420 | + public function caps_where_conditions($context = self::caps_read) |
|
| 3421 | + { |
|
| 3422 | + EEM_Base::verify_is_valid_cap_context($context); |
|
| 3423 | + $cap_where_conditions = array(); |
|
| 3424 | + $cap_restrictions = $this->caps_missing($context); |
|
| 3425 | + /** |
|
| 3426 | + * @var $cap_restrictions EE_Default_Where_Conditions[] |
|
| 3427 | + */ |
|
| 3428 | + foreach ($cap_restrictions as $cap => $restriction_if_no_cap) { |
|
| 3429 | + $cap_where_conditions = array_replace_recursive($cap_where_conditions, |
|
| 3430 | + $restriction_if_no_cap->get_default_where_conditions()); |
|
| 3431 | + } |
|
| 3432 | + return apply_filters('FHEE__EEM_Base__caps_where_conditions__return', $cap_where_conditions, $this, $context, |
|
| 3433 | + $cap_restrictions); |
|
| 3434 | + } |
|
| 3435 | + |
|
| 3436 | + |
|
| 3437 | + |
|
| 3438 | + /** |
|
| 3439 | + * Verifies that $should_be_order_string is in $this->_allowed_order_values, |
|
| 3440 | + * otherwise throws an exception |
|
| 3441 | + * |
|
| 3442 | + * @param string $should_be_order_string |
|
| 3443 | + * @return string either ASC, asc, DESC or desc |
|
| 3444 | + * @throws EE_Error |
|
| 3445 | + */ |
|
| 3446 | + private function _extract_order($should_be_order_string) |
|
| 3447 | + { |
|
| 3448 | + if (in_array($should_be_order_string, $this->_allowed_order_values)) { |
|
| 3449 | + return $should_be_order_string; |
|
| 3450 | + } else { |
|
| 3451 | + throw new EE_Error(sprintf(__("While performing a query on '%s', tried to use '%s' as an order parameter. ", |
|
| 3452 | + "event_espresso"), get_class($this), $should_be_order_string)); |
|
| 3453 | + } |
|
| 3454 | + } |
|
| 3455 | + |
|
| 3456 | + |
|
| 3457 | + |
|
| 3458 | + /** |
|
| 3459 | + * Looks at all the models which are included in this query, and asks each |
|
| 3460 | + * for their universal_where_params, and returns them in the same format as $query_params[0] (where), |
|
| 3461 | + * so they can be merged |
|
| 3462 | + * |
|
| 3463 | + * @param EE_Model_Query_Info_Carrier $query_info_carrier |
|
| 3464 | + * @param string $use_default_where_conditions can be 'none','other_models_only', or 'all'. |
|
| 3465 | + * 'none' means NO default where conditions will |
|
| 3466 | + * be used AT ALL during this query. |
|
| 3467 | + * 'other_models_only' means default where |
|
| 3468 | + * conditions from other models will be used, but |
|
| 3469 | + * not for this primary model. 'all', the default, |
|
| 3470 | + * means default where conditions will apply as |
|
| 3471 | + * normal |
|
| 3472 | + * @param array $where_query_params like EEM_Base::get_all's $query_params[0] |
|
| 3473 | + * @throws EE_Error |
|
| 3474 | + * @return array like $query_params[0], see EEM_Base::get_all for documentation |
|
| 3475 | + */ |
|
| 3476 | + private function _get_default_where_conditions_for_models_in_query( |
|
| 3477 | + EE_Model_Query_Info_Carrier $query_info_carrier, |
|
| 3478 | + $use_default_where_conditions = EEM_Base::default_where_conditions_all, |
|
| 3479 | + $where_query_params = array() |
|
| 3480 | + ) { |
|
| 3481 | + $allowed_used_default_where_conditions_values = EEM_Base::valid_default_where_conditions(); |
|
| 3482 | + if (! in_array($use_default_where_conditions, $allowed_used_default_where_conditions_values)) { |
|
| 3483 | + throw new EE_Error(sprintf(__("You passed an invalid value to the query parameter 'default_where_conditions' of '%s'. Allowed values are %s", |
|
| 3484 | + "event_espresso"), $use_default_where_conditions, |
|
| 3485 | + implode(", ", $allowed_used_default_where_conditions_values))); |
|
| 3486 | + } |
|
| 3487 | + $universal_query_params = array(); |
|
| 3488 | + if ($this->_should_use_default_where_conditions( $use_default_where_conditions, true)) { |
|
| 3489 | + $universal_query_params = $this->_get_default_where_conditions(); |
|
| 3490 | + } else if ($this->_should_use_minimum_where_conditions( $use_default_where_conditions, true)) { |
|
| 3491 | + $universal_query_params = $this->_get_minimum_where_conditions(); |
|
| 3492 | + } |
|
| 3493 | + foreach ($query_info_carrier->get_model_names_included() as $model_relation_path => $model_name) { |
|
| 3494 | + $related_model = $this->get_related_model_obj($model_name); |
|
| 3495 | + if ( $this->_should_use_default_where_conditions( $use_default_where_conditions, false)) { |
|
| 3496 | + $related_model_universal_where_params = $related_model->_get_default_where_conditions($model_relation_path); |
|
| 3497 | + } elseif ($this->_should_use_minimum_where_conditions( $use_default_where_conditions, false)) { |
|
| 3498 | + $related_model_universal_where_params = $related_model->_get_minimum_where_conditions($model_relation_path); |
|
| 3499 | + } else { |
|
| 3500 | + //we don't want to add full or even minimum default where conditions from this model, so just continue |
|
| 3501 | + continue; |
|
| 3502 | + } |
|
| 3503 | + $overrides = $this->_override_defaults_or_make_null_friendly( |
|
| 3504 | + $related_model_universal_where_params, |
|
| 3505 | + $where_query_params, |
|
| 3506 | + $related_model, |
|
| 3507 | + $model_relation_path |
|
| 3508 | + ); |
|
| 3509 | + $universal_query_params = EEH_Array::merge_arrays_and_overwrite_keys( |
|
| 3510 | + $universal_query_params, |
|
| 3511 | + $overrides |
|
| 3512 | + ); |
|
| 3513 | + } |
|
| 3514 | + return $universal_query_params; |
|
| 3515 | + } |
|
| 3516 | + |
|
| 3517 | + |
|
| 3518 | + |
|
| 3519 | + /** |
|
| 3520 | + * Determines whether or not we should use default where conditions for the model in question |
|
| 3521 | + * (this model, or other related models). |
|
| 3522 | + * Basically, we should use default where conditions on this model if they have requested to use them on all models, |
|
| 3523 | + * this model only, or to use minimum where conditions on all other models and normal where conditions on this one. |
|
| 3524 | + * We should use default where conditions on related models when they requested to use default where conditions |
|
| 3525 | + * on all models, or specifically just on other related models |
|
| 3526 | + * @param $default_where_conditions_value |
|
| 3527 | + * @param bool $for_this_model false means this is for OTHER related models |
|
| 3528 | + * @return bool |
|
| 3529 | + */ |
|
| 3530 | + private function _should_use_default_where_conditions( $default_where_conditions_value, $for_this_model = true ) |
|
| 3531 | + { |
|
| 3532 | + return ( |
|
| 3533 | + $for_this_model |
|
| 3534 | + && in_array( |
|
| 3535 | + $default_where_conditions_value, |
|
| 3536 | + array( |
|
| 3537 | + EEM_Base::default_where_conditions_all, |
|
| 3538 | + EEM_Base::default_where_conditions_this_only, |
|
| 3539 | + EEM_Base::default_where_conditions_minimum_others, |
|
| 3540 | + ), |
|
| 3541 | + true |
|
| 3542 | + ) |
|
| 3543 | + ) |
|
| 3544 | + || ( |
|
| 3545 | + ! $for_this_model |
|
| 3546 | + && in_array( |
|
| 3547 | + $default_where_conditions_value, |
|
| 3548 | + array( |
|
| 3549 | + EEM_Base::default_where_conditions_all, |
|
| 3550 | + EEM_Base::default_where_conditions_others_only, |
|
| 3551 | + ), |
|
| 3552 | + true |
|
| 3553 | + ) |
|
| 3554 | + ); |
|
| 3555 | + } |
|
| 3556 | + |
|
| 3557 | + /** |
|
| 3558 | + * Determines whether or not we should use default minimum conditions for the model in question |
|
| 3559 | + * (this model, or other related models). |
|
| 3560 | + * Basically, we should use minimum where conditions on this model only if they requested all models to use minimum |
|
| 3561 | + * where conditions. |
|
| 3562 | + * We should use minimum where conditions on related models if they requested to use minimum where conditions |
|
| 3563 | + * on this model or others |
|
| 3564 | + * @param $default_where_conditions_value |
|
| 3565 | + * @param bool $for_this_model false means this is for OTHER related models |
|
| 3566 | + * @return bool |
|
| 3567 | + */ |
|
| 3568 | + private function _should_use_minimum_where_conditions($default_where_conditions_value, $for_this_model = true) |
|
| 3569 | + { |
|
| 3570 | + return ( |
|
| 3571 | + $for_this_model |
|
| 3572 | + && $default_where_conditions_value === EEM_Base::default_where_conditions_minimum_all |
|
| 3573 | + ) |
|
| 3574 | + || ( |
|
| 3575 | + ! $for_this_model |
|
| 3576 | + && in_array( |
|
| 3577 | + $default_where_conditions_value, |
|
| 3578 | + array( |
|
| 3579 | + EEM_Base::default_where_conditions_minimum_others, |
|
| 3580 | + EEM_Base::default_where_conditions_minimum_all, |
|
| 3581 | + ), |
|
| 3582 | + true |
|
| 3583 | + ) |
|
| 3584 | + ); |
|
| 3585 | + } |
|
| 3586 | + |
|
| 3587 | + |
|
| 3588 | + /** |
|
| 3589 | + * Checks if any of the defaults have been overridden. If there are any that AREN'T overridden, |
|
| 3590 | + * then we also add a special where condition which allows for that model's primary key |
|
| 3591 | + * to be null (which is important for JOINs. Eg, if you want to see all Events ordered by Venue's name, |
|
| 3592 | + * then Event's with NO Venue won't appear unless you allow VNU_ID to be NULL) |
|
| 3593 | + * |
|
| 3594 | + * @param array $default_where_conditions |
|
| 3595 | + * @param array $provided_where_conditions |
|
| 3596 | + * @param EEM_Base $model |
|
| 3597 | + * @param string $model_relation_path like 'Transaction.Payment.' |
|
| 3598 | + * @return array like EEM_Base::get_all's $query_params[0] |
|
| 3599 | + * @throws \EE_Error |
|
| 3600 | + */ |
|
| 3601 | + private function _override_defaults_or_make_null_friendly( |
|
| 3602 | + $default_where_conditions, |
|
| 3603 | + $provided_where_conditions, |
|
| 3604 | + $model, |
|
| 3605 | + $model_relation_path |
|
| 3606 | + ) { |
|
| 3607 | + $null_friendly_where_conditions = array(); |
|
| 3608 | + $none_overridden = true; |
|
| 3609 | + $or_condition_key_for_defaults = 'OR*' . get_class($model); |
|
| 3610 | + foreach ($default_where_conditions as $key => $val) { |
|
| 3611 | + if (isset($provided_where_conditions[$key])) { |
|
| 3612 | + $none_overridden = false; |
|
| 3613 | + } else { |
|
| 3614 | + $null_friendly_where_conditions[$or_condition_key_for_defaults]['AND'][$key] = $val; |
|
| 3615 | + } |
|
| 3616 | + } |
|
| 3617 | + if ($none_overridden && $default_where_conditions) { |
|
| 3618 | + if ($model->has_primary_key_field()) { |
|
| 3619 | + $null_friendly_where_conditions[$or_condition_key_for_defaults][$model_relation_path |
|
| 3620 | + . "." |
|
| 3621 | + . $model->primary_key_name()] = array('IS NULL'); |
|
| 3622 | + }/*else{ |
|
| 3623 | 3623 | //@todo NO PK, use other defaults |
| 3624 | 3624 | }*/ |
| 3625 | - } |
|
| 3626 | - return $null_friendly_where_conditions; |
|
| 3627 | - } |
|
| 3628 | - |
|
| 3629 | - |
|
| 3630 | - |
|
| 3631 | - /** |
|
| 3632 | - * Uses the _default_where_conditions_strategy set during __construct() to get |
|
| 3633 | - * default where conditions on all get_all, update, and delete queries done by this model. |
|
| 3634 | - * Use the same syntax as client code. Eg on the Event model, use array('Event.EVT_post_type'=>'esp_event'), |
|
| 3635 | - * NOT array('Event_CPT.post_type'=>'esp_event'). |
|
| 3636 | - * |
|
| 3637 | - * @param string $model_relation_path eg, path from Event to Payment is "Registration.Transaction.Payment." |
|
| 3638 | - * @return array like EEM_Base::get_all's $query_params[0] (where conditions) |
|
| 3639 | - */ |
|
| 3640 | - private function _get_default_where_conditions($model_relation_path = null) |
|
| 3641 | - { |
|
| 3642 | - if ($this->_ignore_where_strategy) { |
|
| 3643 | - return array(); |
|
| 3644 | - } |
|
| 3645 | - return $this->_default_where_conditions_strategy->get_default_where_conditions($model_relation_path); |
|
| 3646 | - } |
|
| 3647 | - |
|
| 3648 | - |
|
| 3649 | - |
|
| 3650 | - /** |
|
| 3651 | - * Uses the _minimum_where_conditions_strategy set during __construct() to get |
|
| 3652 | - * minimum where conditions on all get_all, update, and delete queries done by this model. |
|
| 3653 | - * Use the same syntax as client code. Eg on the Event model, use array('Event.EVT_post_type'=>'esp_event'), |
|
| 3654 | - * NOT array('Event_CPT.post_type'=>'esp_event'). |
|
| 3655 | - * Similar to _get_default_where_conditions |
|
| 3656 | - * |
|
| 3657 | - * @param string $model_relation_path eg, path from Event to Payment is "Registration.Transaction.Payment." |
|
| 3658 | - * @return array like EEM_Base::get_all's $query_params[0] (where conditions) |
|
| 3659 | - */ |
|
| 3660 | - protected function _get_minimum_where_conditions($model_relation_path = null) |
|
| 3661 | - { |
|
| 3662 | - if ($this->_ignore_where_strategy) { |
|
| 3663 | - return array(); |
|
| 3664 | - } |
|
| 3665 | - return $this->_minimum_where_conditions_strategy->get_default_where_conditions($model_relation_path); |
|
| 3666 | - } |
|
| 3667 | - |
|
| 3668 | - |
|
| 3669 | - |
|
| 3670 | - /** |
|
| 3671 | - * Creates the string of SQL for the select part of a select query, everything behind SELECT and before FROM. |
|
| 3672 | - * Eg, "Event.post_id, Event.post_name,Event_Detail.EVT_ID..." |
|
| 3673 | - * |
|
| 3674 | - * @param EE_Model_Query_Info_Carrier $model_query_info |
|
| 3675 | - * @return string |
|
| 3676 | - * @throws \EE_Error |
|
| 3677 | - */ |
|
| 3678 | - private function _construct_default_select_sql(EE_Model_Query_Info_Carrier $model_query_info) |
|
| 3679 | - { |
|
| 3680 | - $selects = $this->_get_columns_to_select_for_this_model(); |
|
| 3681 | - foreach ( |
|
| 3682 | - $model_query_info->get_model_names_included() as $model_relation_chain => |
|
| 3683 | - $name_of_other_model_included |
|
| 3684 | - ) { |
|
| 3685 | - $other_model_included = $this->get_related_model_obj($name_of_other_model_included); |
|
| 3686 | - $other_model_selects = $other_model_included->_get_columns_to_select_for_this_model($model_relation_chain); |
|
| 3687 | - foreach ($other_model_selects as $key => $value) { |
|
| 3688 | - $selects[] = $value; |
|
| 3689 | - } |
|
| 3690 | - } |
|
| 3691 | - return implode(", ", $selects); |
|
| 3692 | - } |
|
| 3693 | - |
|
| 3694 | - |
|
| 3695 | - |
|
| 3696 | - /** |
|
| 3697 | - * Gets an array of columns to select for this model, which are necessary for it to create its objects. |
|
| 3698 | - * So that's going to be the columns for all the fields on the model |
|
| 3699 | - * |
|
| 3700 | - * @param string $model_relation_chain like 'Question.Question_Group.Event' |
|
| 3701 | - * @return array numerically indexed, values are columns to select and rename, eg "Event.ID AS 'Event.ID'" |
|
| 3702 | - */ |
|
| 3703 | - public function _get_columns_to_select_for_this_model($model_relation_chain = '') |
|
| 3704 | - { |
|
| 3705 | - $fields = $this->field_settings(); |
|
| 3706 | - $selects = array(); |
|
| 3707 | - $table_alias_with_model_relation_chain_prefix = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
| 3708 | - $this->get_this_model_name()); |
|
| 3709 | - foreach ($fields as $field_obj) { |
|
| 3710 | - $selects[] = $table_alias_with_model_relation_chain_prefix |
|
| 3711 | - . $field_obj->get_table_alias() |
|
| 3712 | - . "." |
|
| 3713 | - . $field_obj->get_table_column() |
|
| 3714 | - . " AS '" |
|
| 3715 | - . $table_alias_with_model_relation_chain_prefix |
|
| 3716 | - . $field_obj->get_table_alias() |
|
| 3717 | - . "." |
|
| 3718 | - . $field_obj->get_table_column() |
|
| 3719 | - . "'"; |
|
| 3720 | - } |
|
| 3721 | - //make sure we are also getting the PKs of each table |
|
| 3722 | - $tables = $this->get_tables(); |
|
| 3723 | - if (count($tables) > 1) { |
|
| 3724 | - foreach ($tables as $table_obj) { |
|
| 3725 | - $qualified_pk_column = $table_alias_with_model_relation_chain_prefix |
|
| 3726 | - . $table_obj->get_fully_qualified_pk_column(); |
|
| 3727 | - if (! in_array($qualified_pk_column, $selects)) { |
|
| 3728 | - $selects[] = "$qualified_pk_column AS '$qualified_pk_column'"; |
|
| 3729 | - } |
|
| 3730 | - } |
|
| 3731 | - } |
|
| 3732 | - return $selects; |
|
| 3733 | - } |
|
| 3734 | - |
|
| 3735 | - |
|
| 3736 | - |
|
| 3737 | - /** |
|
| 3738 | - * Given a $query_param like 'Registration.Transaction.TXN_ID', pops off 'Registration.', |
|
| 3739 | - * gets the join statement for it; gets the data types for it; and passes the remaining 'Transaction.TXN_ID' |
|
| 3740 | - * onto its related Transaction object to do the same. Returns an EE_Join_And_Data_Types object which contains the |
|
| 3741 | - * SQL for joining, and the data types |
|
| 3742 | - * |
|
| 3743 | - * @param null|string $original_query_param |
|
| 3744 | - * @param string $query_param like Registration.Transaction.TXN_ID |
|
| 3745 | - * @param EE_Model_Query_Info_Carrier $passed_in_query_info |
|
| 3746 | - * @param string $query_param_type like Registration.Transaction.TXN_ID |
|
| 3747 | - * or 'PAY_ID'. Otherwise, we don't expect there to be a |
|
| 3748 | - * column name. We only want model names, eg 'Event.Venue' |
|
| 3749 | - * or 'Registration's |
|
| 3750 | - * @param string $original_query_param what it originally was (eg |
|
| 3751 | - * Registration.Transaction.TXN_ID). If null, we assume it |
|
| 3752 | - * matches $query_param |
|
| 3753 | - * @throws EE_Error |
|
| 3754 | - * @return void only modifies the EEM_Related_Model_Info_Carrier passed into it |
|
| 3755 | - */ |
|
| 3756 | - private function _extract_related_model_info_from_query_param( |
|
| 3757 | - $query_param, |
|
| 3758 | - EE_Model_Query_Info_Carrier $passed_in_query_info, |
|
| 3759 | - $query_param_type, |
|
| 3760 | - $original_query_param = null |
|
| 3761 | - ) { |
|
| 3762 | - if ($original_query_param === null) { |
|
| 3763 | - $original_query_param = $query_param; |
|
| 3764 | - } |
|
| 3765 | - $query_param = $this->_remove_stars_and_anything_after_from_condition_query_param_key($query_param); |
|
| 3766 | - /** @var $allow_logic_query_params bool whether or not to allow logic_query_params like 'NOT','OR', or 'AND' */ |
|
| 3767 | - $allow_logic_query_params = in_array($query_param_type, array('where', 'having')); |
|
| 3768 | - $allow_fields = in_array($query_param_type, array('where', 'having', 'order_by', 'group_by', 'order')); |
|
| 3769 | - //check to see if we have a field on this model |
|
| 3770 | - $this_model_fields = $this->field_settings(true); |
|
| 3771 | - if (array_key_exists($query_param, $this_model_fields)) { |
|
| 3772 | - if ($allow_fields) { |
|
| 3773 | - return; |
|
| 3774 | - } else { |
|
| 3775 | - throw new EE_Error(sprintf(__("Using a field name (%s) on model %s is not allowed on this query param type '%s'. Original query param was %s", |
|
| 3776 | - "event_espresso"), |
|
| 3777 | - $query_param, get_class($this), $query_param_type, $original_query_param)); |
|
| 3778 | - } |
|
| 3779 | - } //check if this is a special logic query param |
|
| 3780 | - elseif (in_array($query_param, $this->_logic_query_param_keys, true)) { |
|
| 3781 | - if ($allow_logic_query_params) { |
|
| 3782 | - return; |
|
| 3783 | - } else { |
|
| 3784 | - throw new EE_Error( |
|
| 3785 | - sprintf( |
|
| 3786 | - __('Logic query params ("%1$s") are being used incorrectly with the following query param ("%2$s") on model %3$s. %4$sAdditional Info:%4$s%5$s', |
|
| 3787 | - 'event_espresso'), |
|
| 3788 | - implode('", "', $this->_logic_query_param_keys), |
|
| 3789 | - $query_param, |
|
| 3790 | - get_class($this), |
|
| 3791 | - '<br />', |
|
| 3792 | - "\t" |
|
| 3793 | - . ' $passed_in_query_info = <pre>' |
|
| 3794 | - . print_r($passed_in_query_info, true) |
|
| 3795 | - . '</pre>' |
|
| 3796 | - . "\n\t" |
|
| 3797 | - . ' $query_param_type = ' |
|
| 3798 | - . $query_param_type |
|
| 3799 | - . "\n\t" |
|
| 3800 | - . ' $original_query_param = ' |
|
| 3801 | - . $original_query_param |
|
| 3802 | - ) |
|
| 3803 | - ); |
|
| 3804 | - } |
|
| 3805 | - } //check if it's a custom selection |
|
| 3806 | - elseif (array_key_exists($query_param, $this->_custom_selections)) { |
|
| 3807 | - return; |
|
| 3808 | - } |
|
| 3809 | - //check if has a model name at the beginning |
|
| 3810 | - //and |
|
| 3811 | - //check if it's a field on a related model |
|
| 3812 | - foreach ($this->_model_relations as $valid_related_model_name => $relation_obj) { |
|
| 3813 | - if (strpos($query_param, $valid_related_model_name . ".") === 0) { |
|
| 3814 | - $this->_add_join_to_model($valid_related_model_name, $passed_in_query_info, $original_query_param); |
|
| 3815 | - $query_param = substr($query_param, strlen($valid_related_model_name . ".")); |
|
| 3816 | - if ($query_param === '') { |
|
| 3817 | - //nothing left to $query_param |
|
| 3818 | - //we should actually end in a field name, not a model like this! |
|
| 3819 | - throw new EE_Error(sprintf(__("Query param '%s' (of type %s on model %s) shouldn't end on a period (.) ", |
|
| 3820 | - "event_espresso"), |
|
| 3821 | - $query_param, $query_param_type, get_class($this), $valid_related_model_name)); |
|
| 3822 | - } else { |
|
| 3823 | - $related_model_obj = $this->get_related_model_obj($valid_related_model_name); |
|
| 3824 | - $related_model_obj->_extract_related_model_info_from_query_param($query_param, |
|
| 3825 | - $passed_in_query_info, $query_param_type, $original_query_param); |
|
| 3826 | - return; |
|
| 3827 | - } |
|
| 3828 | - } elseif ($query_param === $valid_related_model_name) { |
|
| 3829 | - $this->_add_join_to_model($valid_related_model_name, $passed_in_query_info, $original_query_param); |
|
| 3830 | - return; |
|
| 3831 | - } |
|
| 3832 | - } |
|
| 3833 | - //ok so $query_param didn't start with a model name |
|
| 3834 | - //and we previously confirmed it wasn't a logic query param or field on the current model |
|
| 3835 | - //it's wack, that's what it is |
|
| 3836 | - throw new EE_Error(sprintf(__("There is no model named '%s' related to %s. Query param type is %s and original query param is %s", |
|
| 3837 | - "event_espresso"), |
|
| 3838 | - $query_param, get_class($this), $query_param_type, $original_query_param)); |
|
| 3839 | - } |
|
| 3840 | - |
|
| 3841 | - |
|
| 3842 | - |
|
| 3843 | - /** |
|
| 3844 | - * Privately used by _extract_related_model_info_from_query_param to add a join to $model_name |
|
| 3845 | - * and store it on $passed_in_query_info |
|
| 3846 | - * |
|
| 3847 | - * @param string $model_name |
|
| 3848 | - * @param EE_Model_Query_Info_Carrier $passed_in_query_info |
|
| 3849 | - * @param string $original_query_param used to extract the relation chain between the queried |
|
| 3850 | - * model and $model_name. Eg, if we are querying Event, |
|
| 3851 | - * and are adding a join to 'Payment' with the original |
|
| 3852 | - * query param key |
|
| 3853 | - * 'Registration.Transaction.Payment.PAY_amount', we want |
|
| 3854 | - * to extract 'Registration.Transaction.Payment', in case |
|
| 3855 | - * Payment wants to add default query params so that it |
|
| 3856 | - * will know what models to prepend onto its default query |
|
| 3857 | - * params or in case it wants to rename tables (in case |
|
| 3858 | - * there are multiple joins to the same table) |
|
| 3859 | - * @return void |
|
| 3860 | - * @throws \EE_Error |
|
| 3861 | - */ |
|
| 3862 | - private function _add_join_to_model( |
|
| 3863 | - $model_name, |
|
| 3864 | - EE_Model_Query_Info_Carrier $passed_in_query_info, |
|
| 3865 | - $original_query_param |
|
| 3866 | - ) { |
|
| 3867 | - $relation_obj = $this->related_settings_for($model_name); |
|
| 3868 | - $model_relation_chain = EE_Model_Parser::extract_model_relation_chain($model_name, $original_query_param); |
|
| 3869 | - //check if the relation is HABTM, because then we're essentially doing two joins |
|
| 3870 | - //If so, join first to the JOIN table, and add its data types, and then continue as normal |
|
| 3871 | - if ($relation_obj instanceof EE_HABTM_Relation) { |
|
| 3872 | - $join_model_obj = $relation_obj->get_join_model(); |
|
| 3873 | - //replace the model specified with the join model for this relation chain, whi |
|
| 3874 | - $relation_chain_to_join_model = EE_Model_Parser::replace_model_name_with_join_model_name_in_model_relation_chain($model_name, |
|
| 3875 | - $join_model_obj->get_this_model_name(), $model_relation_chain); |
|
| 3876 | - $new_query_info = new EE_Model_Query_Info_Carrier( |
|
| 3877 | - array($relation_chain_to_join_model => $join_model_obj->get_this_model_name()), |
|
| 3878 | - $relation_obj->get_join_to_intermediate_model_statement($relation_chain_to_join_model)); |
|
| 3879 | - $passed_in_query_info->merge($new_query_info); |
|
| 3880 | - } |
|
| 3881 | - //now just join to the other table pointed to by the relation object, and add its data types |
|
| 3882 | - $new_query_info = new EE_Model_Query_Info_Carrier( |
|
| 3883 | - array($model_relation_chain => $model_name), |
|
| 3884 | - $relation_obj->get_join_statement($model_relation_chain)); |
|
| 3885 | - $passed_in_query_info->merge($new_query_info); |
|
| 3886 | - } |
|
| 3887 | - |
|
| 3888 | - |
|
| 3889 | - |
|
| 3890 | - /** |
|
| 3891 | - * Constructs SQL for where clause, like "WHERE Event.ID = 23 AND Transaction.amount > 100" etc. |
|
| 3892 | - * |
|
| 3893 | - * @param array $where_params like EEM_Base::get_all |
|
| 3894 | - * @return string of SQL |
|
| 3895 | - * @throws \EE_Error |
|
| 3896 | - */ |
|
| 3897 | - private function _construct_where_clause($where_params) |
|
| 3898 | - { |
|
| 3899 | - $SQL = $this->_construct_condition_clause_recursive($where_params, ' AND '); |
|
| 3900 | - if ($SQL) { |
|
| 3901 | - return " WHERE " . $SQL; |
|
| 3902 | - } else { |
|
| 3903 | - return ''; |
|
| 3904 | - } |
|
| 3905 | - } |
|
| 3906 | - |
|
| 3907 | - |
|
| 3908 | - |
|
| 3909 | - /** |
|
| 3910 | - * Just like the _construct_where_clause, except prepends 'HAVING' instead of 'WHERE', |
|
| 3911 | - * and should be passed HAVING parameters, not WHERE parameters |
|
| 3912 | - * |
|
| 3913 | - * @param array $having_params |
|
| 3914 | - * @return string |
|
| 3915 | - * @throws \EE_Error |
|
| 3916 | - */ |
|
| 3917 | - private function _construct_having_clause($having_params) |
|
| 3918 | - { |
|
| 3919 | - $SQL = $this->_construct_condition_clause_recursive($having_params, ' AND '); |
|
| 3920 | - if ($SQL) { |
|
| 3921 | - return " HAVING " . $SQL; |
|
| 3922 | - } else { |
|
| 3923 | - return ''; |
|
| 3924 | - } |
|
| 3925 | - } |
|
| 3926 | - |
|
| 3927 | - |
|
| 3928 | - /** |
|
| 3929 | - * Used for creating nested WHERE conditions. Eg "WHERE ! (Event.ID = 3 OR ( Event_Meta.meta_key = 'bob' AND |
|
| 3930 | - * Event_Meta.meta_value = 'foo'))" |
|
| 3931 | - * |
|
| 3932 | - * @param array $where_params see EEM_Base::get_all for documentation |
|
| 3933 | - * @param string $glue joins each subclause together. Should really only be " AND " or " OR "... |
|
| 3934 | - * @throws EE_Error |
|
| 3935 | - * @return string of SQL |
|
| 3936 | - */ |
|
| 3937 | - private function _construct_condition_clause_recursive($where_params, $glue = ' AND') |
|
| 3938 | - { |
|
| 3939 | - $where_clauses = array(); |
|
| 3940 | - foreach ($where_params as $query_param => $op_and_value_or_sub_condition) { |
|
| 3941 | - $query_param = $this->_remove_stars_and_anything_after_from_condition_query_param_key($query_param);//str_replace("*",'',$query_param); |
|
| 3942 | - if (in_array($query_param, $this->_logic_query_param_keys)) { |
|
| 3943 | - switch ($query_param) { |
|
| 3944 | - case 'not': |
|
| 3945 | - case 'NOT': |
|
| 3946 | - $where_clauses[] = "! (" |
|
| 3947 | - . $this->_construct_condition_clause_recursive($op_and_value_or_sub_condition, |
|
| 3948 | - $glue) |
|
| 3949 | - . ")"; |
|
| 3950 | - break; |
|
| 3951 | - case 'and': |
|
| 3952 | - case 'AND': |
|
| 3953 | - $where_clauses[] = " (" |
|
| 3954 | - . $this->_construct_condition_clause_recursive($op_and_value_or_sub_condition, |
|
| 3955 | - ' AND ') |
|
| 3956 | - . ")"; |
|
| 3957 | - break; |
|
| 3958 | - case 'or': |
|
| 3959 | - case 'OR': |
|
| 3960 | - $where_clauses[] = " (" |
|
| 3961 | - . $this->_construct_condition_clause_recursive($op_and_value_or_sub_condition, |
|
| 3962 | - ' OR ') |
|
| 3963 | - . ")"; |
|
| 3964 | - break; |
|
| 3965 | - } |
|
| 3966 | - } else { |
|
| 3967 | - $field_obj = $this->_deduce_field_from_query_param($query_param); |
|
| 3968 | - //if it's not a normal field, maybe it's a custom selection? |
|
| 3969 | - if (! $field_obj) { |
|
| 3970 | - if (isset($this->_custom_selections[$query_param][1])) { |
|
| 3971 | - $field_obj = $this->_custom_selections[$query_param][1]; |
|
| 3972 | - } else { |
|
| 3973 | - throw new EE_Error(sprintf(__("%s is neither a valid model field name, nor a custom selection", |
|
| 3974 | - "event_espresso"), $query_param)); |
|
| 3975 | - } |
|
| 3976 | - } |
|
| 3977 | - $op_and_value_sql = $this->_construct_op_and_value($op_and_value_or_sub_condition, $field_obj); |
|
| 3978 | - $where_clauses[] = $this->_deduce_column_name_from_query_param($query_param) . SP . $op_and_value_sql; |
|
| 3979 | - } |
|
| 3980 | - } |
|
| 3981 | - return $where_clauses ? implode($glue, $where_clauses) : ''; |
|
| 3982 | - } |
|
| 3983 | - |
|
| 3984 | - |
|
| 3985 | - |
|
| 3986 | - /** |
|
| 3987 | - * Takes the input parameter and extract the table name (alias) and column name |
|
| 3988 | - * |
|
| 3989 | - * @param array $query_param like Registration.Transaction.TXN_ID, Event.Datetime.start_time, or REG_ID |
|
| 3990 | - * @throws EE_Error |
|
| 3991 | - * @return string table alias and column name for SQL, eg "Transaction.TXN_ID" |
|
| 3992 | - */ |
|
| 3993 | - private function _deduce_column_name_from_query_param($query_param) |
|
| 3994 | - { |
|
| 3995 | - $field = $this->_deduce_field_from_query_param($query_param); |
|
| 3996 | - if ($field) { |
|
| 3997 | - $table_alias_prefix = EE_Model_Parser::extract_table_alias_model_relation_chain_from_query_param($field->get_model_name(), |
|
| 3998 | - $query_param); |
|
| 3999 | - return $table_alias_prefix . $field->get_qualified_column(); |
|
| 4000 | - } elseif (array_key_exists($query_param, $this->_custom_selections)) { |
|
| 4001 | - //maybe it's custom selection item? |
|
| 4002 | - //if so, just use it as the "column name" |
|
| 4003 | - return $query_param; |
|
| 4004 | - } else { |
|
| 4005 | - throw new EE_Error(sprintf(__("%s is not a valid field on this model, nor a custom selection (%s)", |
|
| 4006 | - "event_espresso"), $query_param, implode(",", $this->_custom_selections))); |
|
| 4007 | - } |
|
| 4008 | - } |
|
| 4009 | - |
|
| 4010 | - |
|
| 4011 | - |
|
| 4012 | - /** |
|
| 4013 | - * Removes the * and anything after it from the condition query param key. It is useful to add the * to condition |
|
| 4014 | - * query param keys (eg, 'OR*', 'EVT_ID') in order for the array keys to still be unique, so that they don't get |
|
| 4015 | - * overwritten Takes a string like 'Event.EVT_ID*', 'TXN_total**', 'OR*1st', and 'DTT_reg_start*foobar' to |
|
| 4016 | - * 'Event.EVT_ID', 'TXN_total', 'OR', and 'DTT_reg_start', respectively. |
|
| 4017 | - * |
|
| 4018 | - * @param string $condition_query_param_key |
|
| 4019 | - * @return string |
|
| 4020 | - */ |
|
| 4021 | - private function _remove_stars_and_anything_after_from_condition_query_param_key($condition_query_param_key) |
|
| 4022 | - { |
|
| 4023 | - $pos_of_star = strpos($condition_query_param_key, '*'); |
|
| 4024 | - if ($pos_of_star === false) { |
|
| 4025 | - return $condition_query_param_key; |
|
| 4026 | - } else { |
|
| 4027 | - $condition_query_param_sans_star = substr($condition_query_param_key, 0, $pos_of_star); |
|
| 4028 | - return $condition_query_param_sans_star; |
|
| 4029 | - } |
|
| 4030 | - } |
|
| 4031 | - |
|
| 4032 | - |
|
| 4033 | - |
|
| 4034 | - /** |
|
| 4035 | - * creates the SQL for the operator and the value in a WHERE clause, eg "< 23" or "LIKE '%monkey%'" |
|
| 4036 | - * |
|
| 4037 | - * @param mixed array | string $op_and_value |
|
| 4038 | - * @param EE_Model_Field_Base|string $field_obj . If string, should be one of EEM_Base::_valid_wpdb_data_types |
|
| 4039 | - * @throws EE_Error |
|
| 4040 | - * @return string |
|
| 4041 | - */ |
|
| 4042 | - private function _construct_op_and_value($op_and_value, $field_obj) |
|
| 4043 | - { |
|
| 4044 | - if (is_array($op_and_value)) { |
|
| 4045 | - $operator = isset($op_and_value[0]) ? $this->_prepare_operator_for_sql($op_and_value[0]) : null; |
|
| 4046 | - if (! $operator) { |
|
| 4047 | - $php_array_like_string = array(); |
|
| 4048 | - foreach ($op_and_value as $key => $value) { |
|
| 4049 | - $php_array_like_string[] = "$key=>$value"; |
|
| 4050 | - } |
|
| 4051 | - throw new EE_Error( |
|
| 4052 | - sprintf( |
|
| 4053 | - __( |
|
| 4054 | - "You setup a query parameter like you were going to specify an operator, but didn't. You provided '(%s)', but the operator should be at array key index 0 (eg array('>',32))", |
|
| 4055 | - "event_espresso" |
|
| 4056 | - ), |
|
| 4057 | - implode(",", $php_array_like_string) |
|
| 4058 | - ) |
|
| 4059 | - ); |
|
| 4060 | - } |
|
| 4061 | - $value = isset($op_and_value[1]) ? $op_and_value[1] : null; |
|
| 4062 | - } else { |
|
| 4063 | - $operator = '='; |
|
| 4064 | - $value = $op_and_value; |
|
| 4065 | - } |
|
| 4066 | - //check to see if the value is actually another field |
|
| 4067 | - if (is_array($op_and_value) && isset($op_and_value[2]) && $op_and_value[2] == true) { |
|
| 4068 | - return $operator . SP . $this->_deduce_column_name_from_query_param($value); |
|
| 4069 | - } elseif (in_array($operator, $this->valid_in_style_operators()) && is_array($value)) { |
|
| 4070 | - //in this case, the value should be an array, or at least a comma-separated list |
|
| 4071 | - //it will need to handle a little differently |
|
| 4072 | - $cleaned_value = $this->_construct_in_value($value, $field_obj); |
|
| 4073 | - //note: $cleaned_value has already been run through $wpdb->prepare() |
|
| 4074 | - return $operator . SP . $cleaned_value; |
|
| 4075 | - } elseif (in_array($operator, $this->valid_between_style_operators()) && is_array($value)) { |
|
| 4076 | - //the value should be an array with count of two. |
|
| 4077 | - if (count($value) !== 2) { |
|
| 4078 | - throw new EE_Error( |
|
| 4079 | - sprintf( |
|
| 4080 | - __( |
|
| 4081 | - "The '%s' operator must be used with an array of values and there must be exactly TWO values in that array.", |
|
| 4082 | - 'event_espresso' |
|
| 4083 | - ), |
|
| 4084 | - "BETWEEN" |
|
| 4085 | - ) |
|
| 4086 | - ); |
|
| 4087 | - } |
|
| 4088 | - $cleaned_value = $this->_construct_between_value($value, $field_obj); |
|
| 4089 | - return $operator . SP . $cleaned_value; |
|
| 4090 | - } elseif (in_array($operator, $this->valid_null_style_operators())) { |
|
| 4091 | - if ($value !== null) { |
|
| 4092 | - throw new EE_Error( |
|
| 4093 | - sprintf( |
|
| 4094 | - __( |
|
| 4095 | - "You attempted to give a value (%s) while using a NULL-style operator (%s). That isn't valid", |
|
| 4096 | - "event_espresso" |
|
| 4097 | - ), |
|
| 4098 | - $value, |
|
| 4099 | - $operator |
|
| 4100 | - ) |
|
| 4101 | - ); |
|
| 4102 | - } |
|
| 4103 | - return $operator; |
|
| 4104 | - } elseif (in_array($operator, $this->valid_like_style_operators()) && ! is_array($value)) { |
|
| 4105 | - //if the operator is 'LIKE', we want to allow percent signs (%) and not |
|
| 4106 | - //remove other junk. So just treat it as a string. |
|
| 4107 | - return $operator . SP . $this->_wpdb_prepare_using_field($value, '%s'); |
|
| 4108 | - } elseif (! in_array($operator, $this->valid_in_style_operators()) && ! is_array($value)) { |
|
| 4109 | - return $operator . SP . $this->_wpdb_prepare_using_field($value, $field_obj); |
|
| 4110 | - } elseif (in_array($operator, $this->valid_in_style_operators()) && ! is_array($value)) { |
|
| 4111 | - throw new EE_Error( |
|
| 4112 | - sprintf( |
|
| 4113 | - __( |
|
| 4114 | - "Operator '%s' must be used with an array of values, eg 'Registration.REG_ID' => array('%s',array(1,2,3))", |
|
| 4115 | - 'event_espresso' |
|
| 4116 | - ), |
|
| 4117 | - $operator, |
|
| 4118 | - $operator |
|
| 4119 | - ) |
|
| 4120 | - ); |
|
| 4121 | - } elseif (! in_array($operator, $this->valid_in_style_operators()) && is_array($value)) { |
|
| 4122 | - throw new EE_Error( |
|
| 4123 | - sprintf( |
|
| 4124 | - __( |
|
| 4125 | - "Operator '%s' must be used with a single value, not an array. Eg 'Registration.REG_ID => array('%s',23))", |
|
| 4126 | - 'event_espresso' |
|
| 4127 | - ), |
|
| 4128 | - $operator, |
|
| 4129 | - $operator |
|
| 4130 | - ) |
|
| 4131 | - ); |
|
| 4132 | - } else { |
|
| 4133 | - throw new EE_Error( |
|
| 4134 | - sprintf( |
|
| 4135 | - __( |
|
| 4136 | - "It appears you've provided some totally invalid query parameters. Operator and value were:'%s', which isn't right at all", |
|
| 4137 | - "event_espresso" |
|
| 4138 | - ), |
|
| 4139 | - http_build_query($op_and_value) |
|
| 4140 | - ) |
|
| 4141 | - ); |
|
| 4142 | - } |
|
| 4143 | - } |
|
| 4144 | - |
|
| 4145 | - |
|
| 4146 | - |
|
| 4147 | - /** |
|
| 4148 | - * Creates the operands to be used in a BETWEEN query, eg "'2014-12-31 20:23:33' AND '2015-01-23 12:32:54'" |
|
| 4149 | - * |
|
| 4150 | - * @param array $values |
|
| 4151 | - * @param EE_Model_Field_Base|string $field_obj if string, it should be the datatype to be used when querying, eg |
|
| 4152 | - * '%s' |
|
| 4153 | - * @return string |
|
| 4154 | - * @throws \EE_Error |
|
| 4155 | - */ |
|
| 4156 | - public function _construct_between_value($values, $field_obj) |
|
| 4157 | - { |
|
| 4158 | - $cleaned_values = array(); |
|
| 4159 | - foreach ($values as $value) { |
|
| 4160 | - $cleaned_values[] = $this->_wpdb_prepare_using_field($value, $field_obj); |
|
| 4161 | - } |
|
| 4162 | - return $cleaned_values[0] . " AND " . $cleaned_values[1]; |
|
| 4163 | - } |
|
| 4164 | - |
|
| 4165 | - |
|
| 4166 | - |
|
| 4167 | - /** |
|
| 4168 | - * Takes an array or a comma-separated list of $values and cleans them |
|
| 4169 | - * according to $data_type using $wpdb->prepare, and then makes the list a |
|
| 4170 | - * string surrounded by ( and ). Eg, _construct_in_value(array(1,2,3),'%d') would |
|
| 4171 | - * return '(1,2,3)'; _construct_in_value("1,2,hack",'%d') would return '(1,2,1)' (assuming |
|
| 4172 | - * I'm right that a string, when interpreted as a digit, becomes a 1. It might become a 0) |
|
| 4173 | - * |
|
| 4174 | - * @param mixed $values array or comma-separated string |
|
| 4175 | - * @param EE_Model_Field_Base|string $field_obj if string, it should be a wpdb data type like '%s', or '%d' |
|
| 4176 | - * @return string of SQL to follow an 'IN' or 'NOT IN' operator |
|
| 4177 | - * @throws \EE_Error |
|
| 4178 | - */ |
|
| 4179 | - public function _construct_in_value($values, $field_obj) |
|
| 4180 | - { |
|
| 4181 | - //check if the value is a CSV list |
|
| 4182 | - if (is_string($values)) { |
|
| 4183 | - //in which case, turn it into an array |
|
| 4184 | - $values = explode(",", $values); |
|
| 4185 | - } |
|
| 4186 | - $cleaned_values = array(); |
|
| 4187 | - foreach ($values as $value) { |
|
| 4188 | - $cleaned_values[] = $this->_wpdb_prepare_using_field($value, $field_obj); |
|
| 4189 | - } |
|
| 4190 | - //we would just LOVE to leave $cleaned_values as an empty array, and return the value as "()", |
|
| 4191 | - //but unfortunately that's invalid SQL. So instead we return a string which we KNOW will evaluate to be the empty set |
|
| 4192 | - //which is effectively equivalent to returning "()". We don't return "(0)" because that only works for auto-incrementing columns |
|
| 4193 | - if (empty($cleaned_values)) { |
|
| 4194 | - $all_fields = $this->field_settings(); |
|
| 4195 | - $a_field = array_shift($all_fields); |
|
| 4196 | - $main_table = $this->_get_main_table(); |
|
| 4197 | - $cleaned_values[] = "SELECT " |
|
| 4198 | - . $a_field->get_table_column() |
|
| 4199 | - . " FROM " |
|
| 4200 | - . $main_table->get_table_name() |
|
| 4201 | - . " WHERE FALSE"; |
|
| 4202 | - } |
|
| 4203 | - return "(" . implode(",", $cleaned_values) . ")"; |
|
| 4204 | - } |
|
| 4205 | - |
|
| 4206 | - |
|
| 4207 | - |
|
| 4208 | - /** |
|
| 4209 | - * @param mixed $value |
|
| 4210 | - * @param EE_Model_Field_Base|string $field_obj if string it should be a wpdb data type like '%d' |
|
| 4211 | - * @throws EE_Error |
|
| 4212 | - * @return false|null|string |
|
| 4213 | - */ |
|
| 4214 | - private function _wpdb_prepare_using_field($value, $field_obj) |
|
| 4215 | - { |
|
| 4216 | - /** @type WPDB $wpdb */ |
|
| 4217 | - global $wpdb; |
|
| 4218 | - if ($field_obj instanceof EE_Model_Field_Base) { |
|
| 4219 | - return $wpdb->prepare($field_obj->get_wpdb_data_type(), |
|
| 4220 | - $this->_prepare_value_for_use_in_db($value, $field_obj)); |
|
| 4221 | - } else {//$field_obj should really just be a data type |
|
| 4222 | - if (! in_array($field_obj, $this->_valid_wpdb_data_types)) { |
|
| 4223 | - throw new EE_Error(sprintf(__("%s is not a valid wpdb datatype. Valid ones are %s", "event_espresso"), |
|
| 4224 | - $field_obj, implode(",", $this->_valid_wpdb_data_types))); |
|
| 4225 | - } |
|
| 4226 | - return $wpdb->prepare($field_obj, $value); |
|
| 4227 | - } |
|
| 4228 | - } |
|
| 4229 | - |
|
| 4230 | - |
|
| 4231 | - |
|
| 4232 | - /** |
|
| 4233 | - * Takes the input parameter and finds the model field that it indicates. |
|
| 4234 | - * |
|
| 4235 | - * @param string $query_param_name like Registration.Transaction.TXN_ID, Event.Datetime.start_time, or REG_ID |
|
| 4236 | - * @throws EE_Error |
|
| 4237 | - * @return EE_Model_Field_Base |
|
| 4238 | - */ |
|
| 4239 | - protected function _deduce_field_from_query_param($query_param_name) |
|
| 4240 | - { |
|
| 4241 | - //ok, now proceed with deducing which part is the model's name, and which is the field's name |
|
| 4242 | - //which will help us find the database table and column |
|
| 4243 | - $query_param_parts = explode(".", $query_param_name); |
|
| 4244 | - if (empty($query_param_parts)) { |
|
| 4245 | - throw new EE_Error(sprintf(__("_extract_column_name is empty when trying to extract column and table name from %s", |
|
| 4246 | - 'event_espresso'), $query_param_name)); |
|
| 4247 | - } |
|
| 4248 | - $number_of_parts = count($query_param_parts); |
|
| 4249 | - $last_query_param_part = $query_param_parts[count($query_param_parts) - 1]; |
|
| 4250 | - if ($number_of_parts === 1) { |
|
| 4251 | - $field_name = $last_query_param_part; |
|
| 4252 | - $model_obj = $this; |
|
| 4253 | - } else {// $number_of_parts >= 2 |
|
| 4254 | - //the last part is the column name, and there are only 2parts. therefore... |
|
| 4255 | - $field_name = $last_query_param_part; |
|
| 4256 | - $model_obj = $this->get_related_model_obj($query_param_parts[$number_of_parts - 2]); |
|
| 4257 | - } |
|
| 4258 | - try { |
|
| 4259 | - return $model_obj->field_settings_for($field_name); |
|
| 4260 | - } catch (EE_Error $e) { |
|
| 4261 | - return null; |
|
| 4262 | - } |
|
| 4263 | - } |
|
| 4264 | - |
|
| 4265 | - |
|
| 4266 | - |
|
| 4267 | - /** |
|
| 4268 | - * Given a field's name (ie, a key in $this->field_settings()), uses the EE_Model_Field object to get the table's |
|
| 4269 | - * alias and column which corresponds to it |
|
| 4270 | - * |
|
| 4271 | - * @param string $field_name |
|
| 4272 | - * @throws EE_Error |
|
| 4273 | - * @return string |
|
| 4274 | - */ |
|
| 4275 | - public function _get_qualified_column_for_field($field_name) |
|
| 4276 | - { |
|
| 4277 | - $all_fields = $this->field_settings(); |
|
| 4278 | - $field = isset($all_fields[$field_name]) ? $all_fields[$field_name] : false; |
|
| 4279 | - if ($field) { |
|
| 4280 | - return $field->get_qualified_column(); |
|
| 4281 | - } else { |
|
| 4282 | - throw new EE_Error(sprintf(__("There is no field titled %s on model %s. Either the query trying to use it is bad, or you need to add it to the list of fields on the model.", |
|
| 4283 | - 'event_espresso'), $field_name, get_class($this))); |
|
| 4284 | - } |
|
| 4285 | - } |
|
| 4286 | - |
|
| 4287 | - |
|
| 4288 | - |
|
| 4289 | - /** |
|
| 4290 | - * similar to \EEM_Base::_get_qualified_column_for_field() but returns an array with data for ALL fields. |
|
| 4291 | - * Example usage: |
|
| 4292 | - * EEM_Ticket::instance()->get_all_wpdb_results( |
|
| 4293 | - * array(), |
|
| 4294 | - * ARRAY_A, |
|
| 4295 | - * EEM_Ticket::instance()->get_qualified_columns_for_all_fields() |
|
| 4296 | - * ); |
|
| 4297 | - * is equivalent to |
|
| 4298 | - * EEM_Ticket::instance()->get_all_wpdb_results( array(), ARRAY_A, '*' ); |
|
| 4299 | - * and |
|
| 4300 | - * EEM_Event::instance()->get_all_wpdb_results( |
|
| 4301 | - * array( |
|
| 4302 | - * array( |
|
| 4303 | - * 'Datetime.Ticket.TKT_ID' => array( '<', 100 ), |
|
| 4304 | - * ), |
|
| 4305 | - * ARRAY_A, |
|
| 4306 | - * implode( |
|
| 4307 | - * ', ', |
|
| 4308 | - * array_merge( |
|
| 4309 | - * EEM_Event::instance()->get_qualified_columns_for_all_fields( '', false ), |
|
| 4310 | - * EEM_Ticket::instance()->get_qualified_columns_for_all_fields( 'Datetime', false ) |
|
| 4311 | - * ) |
|
| 4312 | - * ) |
|
| 4313 | - * ) |
|
| 4314 | - * ); |
|
| 4315 | - * selects rows from the database, selecting all the event and ticket columns, where the ticket ID is below 100 |
|
| 4316 | - * |
|
| 4317 | - * @param string $model_relation_chain the chain of models used to join between the model you want to query |
|
| 4318 | - * and the one whose fields you are selecting for example: when querying |
|
| 4319 | - * tickets model and selecting fields from the tickets model you would |
|
| 4320 | - * leave this parameter empty, because no models are needed to join |
|
| 4321 | - * between the queried model and the selected one. Likewise when |
|
| 4322 | - * querying the datetime model and selecting fields from the tickets |
|
| 4323 | - * model, it would also be left empty, because there is a direct |
|
| 4324 | - * relation from datetimes to tickets, so no model is needed to join |
|
| 4325 | - * them together. However, when querying from the event model and |
|
| 4326 | - * selecting fields from the ticket model, you should provide the string |
|
| 4327 | - * 'Datetime', indicating that the event model must first join to the |
|
| 4328 | - * datetime model in order to find its relation to ticket model. |
|
| 4329 | - * Also, when querying from the venue model and selecting fields from |
|
| 4330 | - * the ticket model, you should provide the string 'Event.Datetime', |
|
| 4331 | - * indicating you need to join the venue model to the event model, |
|
| 4332 | - * to the datetime model, in order to find its relation to the ticket model. |
|
| 4333 | - * This string is used to deduce the prefix that gets added onto the |
|
| 4334 | - * models' tables qualified columns |
|
| 4335 | - * @param bool $return_string if true, will return a string with qualified column names separated |
|
| 4336 | - * by ', ' if false, will simply return a numerically indexed array of |
|
| 4337 | - * qualified column names |
|
| 4338 | - * @return array|string |
|
| 4339 | - */ |
|
| 4340 | - public function get_qualified_columns_for_all_fields($model_relation_chain = '', $return_string = true) |
|
| 4341 | - { |
|
| 4342 | - $table_prefix = str_replace('.', '__', $model_relation_chain) . (empty($model_relation_chain) ? '' : '__'); |
|
| 4343 | - $qualified_columns = array(); |
|
| 4344 | - foreach ($this->field_settings() as $field_name => $field) { |
|
| 4345 | - $qualified_columns[] = $table_prefix . $field->get_qualified_column(); |
|
| 4346 | - } |
|
| 4347 | - return $return_string ? implode(', ', $qualified_columns) : $qualified_columns; |
|
| 4348 | - } |
|
| 4349 | - |
|
| 4350 | - |
|
| 4351 | - |
|
| 4352 | - /** |
|
| 4353 | - * constructs the select use on special limit joins |
|
| 4354 | - * NOTE: for now this has only been tested and will work when the table alias is for the PRIMARY table. Although |
|
| 4355 | - * its setup so the select query will be setup on and just doing the special select join off of the primary table |
|
| 4356 | - * (as that is typically where the limits would be set). |
|
| 4357 | - * |
|
| 4358 | - * @param string $table_alias The table the select is being built for |
|
| 4359 | - * @param mixed|string $limit The limit for this select |
|
| 4360 | - * @return string The final select join element for the query. |
|
| 4361 | - */ |
|
| 4362 | - public function _construct_limit_join_select($table_alias, $limit) |
|
| 4363 | - { |
|
| 4364 | - $SQL = ''; |
|
| 4365 | - foreach ($this->_tables as $table_obj) { |
|
| 4366 | - if ($table_obj instanceof EE_Primary_Table) { |
|
| 4367 | - $SQL .= $table_alias === $table_obj->get_table_alias() |
|
| 4368 | - ? $table_obj->get_select_join_limit($limit) |
|
| 4369 | - : SP . $table_obj->get_table_name() . " AS " . $table_obj->get_table_alias() . SP; |
|
| 4370 | - } elseif ($table_obj instanceof EE_Secondary_Table) { |
|
| 4371 | - $SQL .= $table_alias === $table_obj->get_table_alias() |
|
| 4372 | - ? $table_obj->get_select_join_limit_join($limit) |
|
| 4373 | - : SP . $table_obj->get_join_sql($table_alias) . SP; |
|
| 4374 | - } |
|
| 4375 | - } |
|
| 4376 | - return $SQL; |
|
| 4377 | - } |
|
| 4378 | - |
|
| 4379 | - |
|
| 4380 | - |
|
| 4381 | - /** |
|
| 4382 | - * Constructs the internal join if there are multiple tables, or simply the table's name and alias |
|
| 4383 | - * Eg "wp_post AS Event" or "wp_post AS Event INNER JOIN wp_postmeta Event_Meta ON Event.ID = Event_Meta.post_id" |
|
| 4384 | - * |
|
| 4385 | - * @return string SQL |
|
| 4386 | - * @throws \EE_Error |
|
| 4387 | - */ |
|
| 4388 | - public function _construct_internal_join() |
|
| 4389 | - { |
|
| 4390 | - $SQL = $this->_get_main_table()->get_table_sql(); |
|
| 4391 | - $SQL .= $this->_construct_internal_join_to_table_with_alias($this->_get_main_table()->get_table_alias()); |
|
| 4392 | - return $SQL; |
|
| 4393 | - } |
|
| 4394 | - |
|
| 4395 | - |
|
| 4396 | - |
|
| 4397 | - /** |
|
| 4398 | - * Constructs the SQL for joining all the tables on this model. |
|
| 4399 | - * Normally $alias should be the primary table's alias, but in cases where |
|
| 4400 | - * we have already joined to a secondary table (eg, the secondary table has a foreign key and is joined before the |
|
| 4401 | - * primary table) then we should provide that secondary table's alias. Eg, with $alias being the primary table's |
|
| 4402 | - * alias, this will construct SQL like: |
|
| 4403 | - * " INNER JOIN wp_esp_secondary_table AS Secondary_Table ON Primary_Table.pk = Secondary_Table.fk". |
|
| 4404 | - * With $alias being a secondary table's alias, this will construct SQL like: |
|
| 4405 | - * " INNER JOIN wp_esp_primary_table AS Primary_Table ON Primary_Table.pk = Secondary_Table.fk". |
|
| 4406 | - * |
|
| 4407 | - * @param string $alias_prefixed table alias to join to (this table should already be in the FROM SQL clause) |
|
| 4408 | - * @return string |
|
| 4409 | - */ |
|
| 4410 | - public function _construct_internal_join_to_table_with_alias($alias_prefixed) |
|
| 4411 | - { |
|
| 4412 | - $SQL = ''; |
|
| 4413 | - $alias_sans_prefix = EE_Model_Parser::remove_table_alias_model_relation_chain_prefix($alias_prefixed); |
|
| 4414 | - foreach ($this->_tables as $table_obj) { |
|
| 4415 | - if ($table_obj instanceof EE_Secondary_Table) {//table is secondary table |
|
| 4416 | - if ($alias_sans_prefix === $table_obj->get_table_alias()) { |
|
| 4417 | - //so we're joining to this table, meaning the table is already in |
|
| 4418 | - //the FROM statement, BUT the primary table isn't. So we want |
|
| 4419 | - //to add the inverse join sql |
|
| 4420 | - $SQL .= $table_obj->get_inverse_join_sql($alias_prefixed); |
|
| 4421 | - } else { |
|
| 4422 | - //just add a regular JOIN to this table from the primary table |
|
| 4423 | - $SQL .= $table_obj->get_join_sql($alias_prefixed); |
|
| 4424 | - } |
|
| 4425 | - }//if it's a primary table, dont add any SQL. it should already be in the FROM statement |
|
| 4426 | - } |
|
| 4427 | - return $SQL; |
|
| 4428 | - } |
|
| 4429 | - |
|
| 4430 | - |
|
| 4431 | - |
|
| 4432 | - /** |
|
| 4433 | - * Gets an array for storing all the data types on the next-to-be-executed-query. |
|
| 4434 | - * This should be a growing array of keys being table-columns (eg 'EVT_ID' and 'Event.EVT_ID'), and values being |
|
| 4435 | - * their data type (eg, '%s', '%d', etc) |
|
| 4436 | - * |
|
| 4437 | - * @return array |
|
| 4438 | - */ |
|
| 4439 | - public function _get_data_types() |
|
| 4440 | - { |
|
| 4441 | - $data_types = array(); |
|
| 4442 | - foreach ($this->field_settings() as $field_obj) { |
|
| 4443 | - //$data_types[$field_obj->get_table_column()] = $field_obj->get_wpdb_data_type(); |
|
| 4444 | - /** @var $field_obj EE_Model_Field_Base */ |
|
| 4445 | - $data_types[$field_obj->get_qualified_column()] = $field_obj->get_wpdb_data_type(); |
|
| 4446 | - } |
|
| 4447 | - return $data_types; |
|
| 4448 | - } |
|
| 4449 | - |
|
| 4450 | - |
|
| 4451 | - |
|
| 4452 | - /** |
|
| 4453 | - * Gets the model object given the relation's name / model's name (eg, 'Event', 'Registration',etc. Always singular) |
|
| 4454 | - * |
|
| 4455 | - * @param string $model_name |
|
| 4456 | - * @throws EE_Error |
|
| 4457 | - * @return EEM_Base |
|
| 4458 | - */ |
|
| 4459 | - public function get_related_model_obj($model_name) |
|
| 4460 | - { |
|
| 4461 | - $model_classname = "EEM_" . $model_name; |
|
| 4462 | - if (! class_exists($model_classname)) { |
|
| 4463 | - throw new EE_Error(sprintf(__("You specified a related model named %s in your query. No such model exists, if it did, it would have the classname %s", |
|
| 4464 | - 'event_espresso'), $model_name, $model_classname)); |
|
| 4465 | - } |
|
| 4466 | - return call_user_func($model_classname . "::instance"); |
|
| 4467 | - } |
|
| 4468 | - |
|
| 4469 | - |
|
| 4470 | - |
|
| 4471 | - /** |
|
| 4472 | - * Returns the array of EE_ModelRelations for this model. |
|
| 4473 | - * |
|
| 4474 | - * @return EE_Model_Relation_Base[] |
|
| 4475 | - */ |
|
| 4476 | - public function relation_settings() |
|
| 4477 | - { |
|
| 4478 | - return $this->_model_relations; |
|
| 4479 | - } |
|
| 4480 | - |
|
| 4481 | - |
|
| 4482 | - |
|
| 4483 | - /** |
|
| 4484 | - * Gets all related models that this model BELONGS TO. Handy to know sometimes |
|
| 4485 | - * because without THOSE models, this model probably doesn't have much purpose. |
|
| 4486 | - * (Eg, without an event, datetimes have little purpose.) |
|
| 4487 | - * |
|
| 4488 | - * @return EE_Belongs_To_Relation[] |
|
| 4489 | - */ |
|
| 4490 | - public function belongs_to_relations() |
|
| 4491 | - { |
|
| 4492 | - $belongs_to_relations = array(); |
|
| 4493 | - foreach ($this->relation_settings() as $model_name => $relation_obj) { |
|
| 4494 | - if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
| 4495 | - $belongs_to_relations[$model_name] = $relation_obj; |
|
| 4496 | - } |
|
| 4497 | - } |
|
| 4498 | - return $belongs_to_relations; |
|
| 4499 | - } |
|
| 4500 | - |
|
| 4501 | - |
|
| 4502 | - |
|
| 4503 | - /** |
|
| 4504 | - * Returns the specified EE_Model_Relation, or throws an exception |
|
| 4505 | - * |
|
| 4506 | - * @param string $relation_name name of relation, key in $this->_relatedModels |
|
| 4507 | - * @throws EE_Error |
|
| 4508 | - * @return EE_Model_Relation_Base |
|
| 4509 | - */ |
|
| 4510 | - public function related_settings_for($relation_name) |
|
| 4511 | - { |
|
| 4512 | - $relatedModels = $this->relation_settings(); |
|
| 4513 | - if (! array_key_exists($relation_name, $relatedModels)) { |
|
| 4514 | - throw new EE_Error( |
|
| 4515 | - sprintf( |
|
| 4516 | - __('Cannot get %s related to %s. There is no model relation of that type. There is, however, %s...', |
|
| 4517 | - 'event_espresso'), |
|
| 4518 | - $relation_name, |
|
| 4519 | - $this->_get_class_name(), |
|
| 4520 | - implode(', ', array_keys($relatedModels)) |
|
| 4521 | - ) |
|
| 4522 | - ); |
|
| 4523 | - } |
|
| 4524 | - return $relatedModels[$relation_name]; |
|
| 4525 | - } |
|
| 4526 | - |
|
| 4527 | - |
|
| 4528 | - |
|
| 4529 | - /** |
|
| 4530 | - * A convenience method for getting a specific field's settings, instead of getting all field settings for all |
|
| 4531 | - * fields |
|
| 4532 | - * |
|
| 4533 | - * @param string $fieldName |
|
| 4534 | - * @param boolean $include_db_only_fields |
|
| 4535 | - * @throws EE_Error |
|
| 4536 | - * @return EE_Model_Field_Base |
|
| 4537 | - */ |
|
| 4538 | - public function field_settings_for($fieldName, $include_db_only_fields = true) |
|
| 4539 | - { |
|
| 4540 | - $fieldSettings = $this->field_settings($include_db_only_fields); |
|
| 4541 | - if (! array_key_exists($fieldName, $fieldSettings)) { |
|
| 4542 | - throw new EE_Error(sprintf(__("There is no field/column '%s' on '%s'", 'event_espresso'), $fieldName, |
|
| 4543 | - get_class($this))); |
|
| 4544 | - } |
|
| 4545 | - return $fieldSettings[$fieldName]; |
|
| 4546 | - } |
|
| 4547 | - |
|
| 4548 | - |
|
| 4549 | - |
|
| 4550 | - /** |
|
| 4551 | - * Checks if this field exists on this model |
|
| 4552 | - * |
|
| 4553 | - * @param string $fieldName a key in the model's _field_settings array |
|
| 4554 | - * @return boolean |
|
| 4555 | - */ |
|
| 4556 | - public function has_field($fieldName) |
|
| 4557 | - { |
|
| 4558 | - $fieldSettings = $this->field_settings(true); |
|
| 4559 | - if (isset($fieldSettings[$fieldName])) { |
|
| 4560 | - return true; |
|
| 4561 | - } else { |
|
| 4562 | - return false; |
|
| 4563 | - } |
|
| 4564 | - } |
|
| 4565 | - |
|
| 4566 | - |
|
| 4567 | - |
|
| 4568 | - /** |
|
| 4569 | - * Returns whether or not this model has a relation to the specified model |
|
| 4570 | - * |
|
| 4571 | - * @param string $relation_name possibly one of the keys in the relation_settings array |
|
| 4572 | - * @return boolean |
|
| 4573 | - */ |
|
| 4574 | - public function has_relation($relation_name) |
|
| 4575 | - { |
|
| 4576 | - $relations = $this->relation_settings(); |
|
| 4577 | - if (isset($relations[$relation_name])) { |
|
| 4578 | - return true; |
|
| 4579 | - } else { |
|
| 4580 | - return false; |
|
| 4581 | - } |
|
| 4582 | - } |
|
| 4583 | - |
|
| 4584 | - |
|
| 4585 | - |
|
| 4586 | - /** |
|
| 4587 | - * gets the field object of type 'primary_key' from the fieldsSettings attribute. |
|
| 4588 | - * Eg, on EE_Answer that would be ANS_ID field object |
|
| 4589 | - * |
|
| 4590 | - * @param $field_obj |
|
| 4591 | - * @return boolean |
|
| 4592 | - */ |
|
| 4593 | - public function is_primary_key_field($field_obj) |
|
| 4594 | - { |
|
| 4595 | - return $field_obj instanceof EE_Primary_Key_Field_Base ? true : false; |
|
| 4596 | - } |
|
| 4597 | - |
|
| 4598 | - |
|
| 4599 | - |
|
| 4600 | - /** |
|
| 4601 | - * gets the field object of type 'primary_key' from the fieldsSettings attribute. |
|
| 4602 | - * Eg, on EE_Answer that would be ANS_ID field object |
|
| 4603 | - * |
|
| 4604 | - * @return EE_Model_Field_Base |
|
| 4605 | - * @throws EE_Error |
|
| 4606 | - */ |
|
| 4607 | - public function get_primary_key_field() |
|
| 4608 | - { |
|
| 4609 | - if ($this->_primary_key_field === null) { |
|
| 4610 | - foreach ($this->field_settings(true) as $field_obj) { |
|
| 4611 | - if ($this->is_primary_key_field($field_obj)) { |
|
| 4612 | - $this->_primary_key_field = $field_obj; |
|
| 4613 | - break; |
|
| 4614 | - } |
|
| 4615 | - } |
|
| 4616 | - if (! $this->_primary_key_field instanceof EE_Primary_Key_Field_Base) { |
|
| 4617 | - throw new EE_Error(sprintf(__("There is no Primary Key defined on model %s", 'event_espresso'), |
|
| 4618 | - get_class($this))); |
|
| 4619 | - } |
|
| 4620 | - } |
|
| 4621 | - return $this->_primary_key_field; |
|
| 4622 | - } |
|
| 4623 | - |
|
| 4624 | - |
|
| 4625 | - |
|
| 4626 | - /** |
|
| 4627 | - * Returns whether or not not there is a primary key on this model. |
|
| 4628 | - * Internally does some caching. |
|
| 4629 | - * |
|
| 4630 | - * @return boolean |
|
| 4631 | - */ |
|
| 4632 | - public function has_primary_key_field() |
|
| 4633 | - { |
|
| 4634 | - if ($this->_has_primary_key_field === null) { |
|
| 4635 | - try { |
|
| 4636 | - $this->get_primary_key_field(); |
|
| 4637 | - $this->_has_primary_key_field = true; |
|
| 4638 | - } catch (EE_Error $e) { |
|
| 4639 | - $this->_has_primary_key_field = false; |
|
| 4640 | - } |
|
| 4641 | - } |
|
| 4642 | - return $this->_has_primary_key_field; |
|
| 4643 | - } |
|
| 4644 | - |
|
| 4645 | - |
|
| 4646 | - |
|
| 4647 | - /** |
|
| 4648 | - * Finds the first field of type $field_class_name. |
|
| 4649 | - * |
|
| 4650 | - * @param string $field_class_name class name of field that you want to find. Eg, EE_Datetime_Field, |
|
| 4651 | - * EE_Foreign_Key_Field, etc |
|
| 4652 | - * @return EE_Model_Field_Base or null if none is found |
|
| 4653 | - */ |
|
| 4654 | - public function get_a_field_of_type($field_class_name) |
|
| 4655 | - { |
|
| 4656 | - foreach ($this->field_settings() as $field) { |
|
| 4657 | - if ($field instanceof $field_class_name) { |
|
| 4658 | - return $field; |
|
| 4659 | - } |
|
| 4660 | - } |
|
| 4661 | - return null; |
|
| 4662 | - } |
|
| 4663 | - |
|
| 4664 | - |
|
| 4665 | - |
|
| 4666 | - /** |
|
| 4667 | - * Gets a foreign key field pointing to model. |
|
| 4668 | - * |
|
| 4669 | - * @param string $model_name eg Event, Registration, not EEM_Event |
|
| 4670 | - * @return EE_Foreign_Key_Field_Base |
|
| 4671 | - * @throws EE_Error |
|
| 4672 | - */ |
|
| 4673 | - public function get_foreign_key_to($model_name) |
|
| 4674 | - { |
|
| 4675 | - if (! isset($this->_cache_foreign_key_to_fields[$model_name])) { |
|
| 4676 | - foreach ($this->field_settings() as $field) { |
|
| 4677 | - if ( |
|
| 4678 | - $field instanceof EE_Foreign_Key_Field_Base |
|
| 4679 | - && in_array($model_name, $field->get_model_names_pointed_to()) |
|
| 4680 | - ) { |
|
| 4681 | - $this->_cache_foreign_key_to_fields[$model_name] = $field; |
|
| 4682 | - break; |
|
| 4683 | - } |
|
| 4684 | - } |
|
| 4685 | - if (! isset($this->_cache_foreign_key_to_fields[$model_name])) { |
|
| 4686 | - throw new EE_Error(sprintf(__("There is no foreign key field pointing to model %s on model %s", |
|
| 4687 | - 'event_espresso'), $model_name, get_class($this))); |
|
| 4688 | - } |
|
| 4689 | - } |
|
| 4690 | - return $this->_cache_foreign_key_to_fields[$model_name]; |
|
| 4691 | - } |
|
| 4692 | - |
|
| 4693 | - |
|
| 4694 | - |
|
| 4695 | - /** |
|
| 4696 | - * Gets the actual table for the table alias |
|
| 4697 | - * |
|
| 4698 | - * @param string $table_alias eg Event, Event_Meta, Registration, Transaction, but maybe |
|
| 4699 | - * a table alias with a model chain prefix, like 'Venue__Event_Venue___Event_Meta'. |
|
| 4700 | - * Either one works |
|
| 4701 | - * @return EE_Table_Base |
|
| 4702 | - */ |
|
| 4703 | - public function get_table_for_alias($table_alias) |
|
| 4704 | - { |
|
| 4705 | - $table_alias_sans_model_relation_chain_prefix = EE_Model_Parser::remove_table_alias_model_relation_chain_prefix($table_alias); |
|
| 4706 | - return $this->_tables[$table_alias_sans_model_relation_chain_prefix]->get_table_name(); |
|
| 4707 | - } |
|
| 4708 | - |
|
| 4709 | - |
|
| 4710 | - |
|
| 4711 | - /** |
|
| 4712 | - * Returns a flat array of all field son this model, instead of organizing them |
|
| 4713 | - * by table_alias as they are in the constructor. |
|
| 4714 | - * |
|
| 4715 | - * @param bool $include_db_only_fields flag indicating whether or not to include the db-only fields |
|
| 4716 | - * @return EE_Model_Field_Base[] where the keys are the field's name |
|
| 4717 | - */ |
|
| 4718 | - public function field_settings($include_db_only_fields = false) |
|
| 4719 | - { |
|
| 4720 | - if ($include_db_only_fields) { |
|
| 4721 | - if ($this->_cached_fields === null) { |
|
| 4722 | - $this->_cached_fields = array(); |
|
| 4723 | - foreach ($this->_fields as $fields_corresponding_to_table) { |
|
| 4724 | - foreach ($fields_corresponding_to_table as $field_name => $field_obj) { |
|
| 4725 | - $this->_cached_fields[$field_name] = $field_obj; |
|
| 4726 | - } |
|
| 4727 | - } |
|
| 4728 | - } |
|
| 4729 | - return $this->_cached_fields; |
|
| 4730 | - } else { |
|
| 4731 | - if ($this->_cached_fields_non_db_only === null) { |
|
| 4732 | - $this->_cached_fields_non_db_only = array(); |
|
| 4733 | - foreach ($this->_fields as $fields_corresponding_to_table) { |
|
| 4734 | - foreach ($fields_corresponding_to_table as $field_name => $field_obj) { |
|
| 4735 | - /** @var $field_obj EE_Model_Field_Base */ |
|
| 4736 | - if (! $field_obj->is_db_only_field()) { |
|
| 4737 | - $this->_cached_fields_non_db_only[$field_name] = $field_obj; |
|
| 4738 | - } |
|
| 4739 | - } |
|
| 4740 | - } |
|
| 4741 | - } |
|
| 4742 | - return $this->_cached_fields_non_db_only; |
|
| 4743 | - } |
|
| 4744 | - } |
|
| 4745 | - |
|
| 4746 | - |
|
| 4747 | - |
|
| 4748 | - /** |
|
| 4749 | - * cycle though array of attendees and create objects out of each item |
|
| 4750 | - * |
|
| 4751 | - * @access private |
|
| 4752 | - * @param array $rows of results of $wpdb->get_results($query,ARRAY_A) |
|
| 4753 | - * @return \EE_Base_Class[] array keys are primary keys (if there is a primary key on the model. if not, |
|
| 4754 | - * numerically indexed) |
|
| 4755 | - * @throws \EE_Error |
|
| 4756 | - */ |
|
| 4757 | - protected function _create_objects($rows = array()) |
|
| 4758 | - { |
|
| 4759 | - $array_of_objects = array(); |
|
| 4760 | - if (empty($rows)) { |
|
| 4761 | - return array(); |
|
| 4762 | - } |
|
| 4763 | - $count_if_model_has_no_primary_key = 0; |
|
| 4764 | - $has_primary_key = $this->has_primary_key_field(); |
|
| 4765 | - $primary_key_field = $has_primary_key ? $this->get_primary_key_field() : null; |
|
| 4766 | - foreach ((array)$rows as $row) { |
|
| 4767 | - if (empty($row)) { |
|
| 4768 | - //wp did its weird thing where it returns an array like array(0=>null), which is totally not helpful... |
|
| 4769 | - return array(); |
|
| 4770 | - } |
|
| 4771 | - //check if we've already set this object in the results array, |
|
| 4772 | - //in which case there's no need to process it further (again) |
|
| 4773 | - if ($has_primary_key) { |
|
| 4774 | - $table_pk_value = $this->_get_column_value_with_table_alias_or_not( |
|
| 4775 | - $row, |
|
| 4776 | - $primary_key_field->get_qualified_column(), |
|
| 4777 | - $primary_key_field->get_table_column() |
|
| 4778 | - ); |
|
| 4779 | - if ($table_pk_value && isset($array_of_objects[$table_pk_value])) { |
|
| 4780 | - continue; |
|
| 4781 | - } |
|
| 4782 | - } |
|
| 4783 | - $classInstance = $this->instantiate_class_from_array_or_object($row); |
|
| 4784 | - if (! $classInstance) { |
|
| 4785 | - throw new EE_Error( |
|
| 4786 | - sprintf( |
|
| 4787 | - __('Could not create instance of class %s from row %s', 'event_espresso'), |
|
| 4788 | - $this->get_this_model_name(), |
|
| 4789 | - http_build_query($row) |
|
| 4790 | - ) |
|
| 4791 | - ); |
|
| 4792 | - } |
|
| 4793 | - //set the timezone on the instantiated objects |
|
| 4794 | - $classInstance->set_timezone($this->_timezone); |
|
| 4795 | - //make sure if there is any timezone setting present that we set the timezone for the object |
|
| 4796 | - $key = $has_primary_key ? $classInstance->ID() : $count_if_model_has_no_primary_key++; |
|
| 4797 | - $array_of_objects[$key] = $classInstance; |
|
| 4798 | - //also, for all the relations of type BelongsTo, see if we can cache |
|
| 4799 | - //those related models |
|
| 4800 | - //(we could do this for other relations too, but if there are conditions |
|
| 4801 | - //that filtered out some fo the results, then we'd be caching an incomplete set |
|
| 4802 | - //so it requires a little more thought than just caching them immediately...) |
|
| 4803 | - foreach ($this->_model_relations as $modelName => $relation_obj) { |
|
| 4804 | - if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
| 4805 | - //check if this model's INFO is present. If so, cache it on the model |
|
| 4806 | - $other_model = $relation_obj->get_other_model(); |
|
| 4807 | - $other_model_obj_maybe = $other_model->instantiate_class_from_array_or_object($row); |
|
| 4808 | - //if we managed to make a model object from the results, cache it on the main model object |
|
| 4809 | - if ($other_model_obj_maybe) { |
|
| 4810 | - //set timezone on these other model objects if they are present |
|
| 4811 | - $other_model_obj_maybe->set_timezone($this->_timezone); |
|
| 4812 | - $classInstance->cache($modelName, $other_model_obj_maybe); |
|
| 4813 | - } |
|
| 4814 | - } |
|
| 4815 | - } |
|
| 4816 | - } |
|
| 4817 | - return $array_of_objects; |
|
| 4818 | - } |
|
| 4819 | - |
|
| 4820 | - |
|
| 4821 | - |
|
| 4822 | - /** |
|
| 4823 | - * The purpose of this method is to allow us to create a model object that is not in the db that holds default |
|
| 4824 | - * values. A typical example of where this is used is when creating a new item and the initial load of a form. We |
|
| 4825 | - * dont' necessarily want to test for if the object is present but just assume it is BUT load the defaults from the |
|
| 4826 | - * object (as set in the model_field!). |
|
| 4827 | - * |
|
| 4828 | - * @return EE_Base_Class single EE_Base_Class object with default values for the properties. |
|
| 4829 | - */ |
|
| 4830 | - public function create_default_object() |
|
| 4831 | - { |
|
| 4832 | - $this_model_fields_and_values = array(); |
|
| 4833 | - //setup the row using default values; |
|
| 4834 | - foreach ($this->field_settings() as $field_name => $field_obj) { |
|
| 4835 | - $this_model_fields_and_values[$field_name] = $field_obj->get_default_value(); |
|
| 4836 | - } |
|
| 4837 | - $className = $this->_get_class_name(); |
|
| 4838 | - $classInstance = EE_Registry::instance() |
|
| 4839 | - ->load_class($className, array($this_model_fields_and_values), false, false); |
|
| 4840 | - return $classInstance; |
|
| 4841 | - } |
|
| 4842 | - |
|
| 4843 | - |
|
| 4844 | - |
|
| 4845 | - /** |
|
| 4846 | - * @param mixed $cols_n_values either an array of where each key is the name of a field, and the value is its value |
|
| 4847 | - * or an stdClass where each property is the name of a column, |
|
| 4848 | - * @return EE_Base_Class |
|
| 4849 | - * @throws \EE_Error |
|
| 4850 | - */ |
|
| 4851 | - public function instantiate_class_from_array_or_object($cols_n_values) |
|
| 4852 | - { |
|
| 4853 | - if (! is_array($cols_n_values) && is_object($cols_n_values)) { |
|
| 4854 | - $cols_n_values = get_object_vars($cols_n_values); |
|
| 4855 | - } |
|
| 4856 | - $primary_key = null; |
|
| 4857 | - //make sure the array only has keys that are fields/columns on this model |
|
| 4858 | - $this_model_fields_n_values = $this->_deduce_fields_n_values_from_cols_n_values($cols_n_values); |
|
| 4859 | - if ($this->has_primary_key_field() && isset($this_model_fields_n_values[$this->primary_key_name()])) { |
|
| 4860 | - $primary_key = $this_model_fields_n_values[$this->primary_key_name()]; |
|
| 4861 | - } |
|
| 4862 | - $className = $this->_get_class_name(); |
|
| 4863 | - //check we actually found results that we can use to build our model object |
|
| 4864 | - //if not, return null |
|
| 4865 | - if ($this->has_primary_key_field()) { |
|
| 4866 | - if (empty($this_model_fields_n_values[$this->primary_key_name()])) { |
|
| 4867 | - return null; |
|
| 4868 | - } |
|
| 4869 | - } else if ($this->unique_indexes()) { |
|
| 4870 | - $first_column = reset($this_model_fields_n_values); |
|
| 4871 | - if (empty($first_column)) { |
|
| 4872 | - return null; |
|
| 4873 | - } |
|
| 4874 | - } |
|
| 4875 | - // if there is no primary key or the object doesn't already exist in the entity map, then create a new instance |
|
| 4876 | - if ($primary_key) { |
|
| 4877 | - $classInstance = $this->get_from_entity_map($primary_key); |
|
| 4878 | - if (! $classInstance) { |
|
| 4879 | - $classInstance = EE_Registry::instance() |
|
| 4880 | - ->load_class($className, |
|
| 4881 | - array($this_model_fields_n_values, $this->_timezone), true, false); |
|
| 4882 | - // add this new object to the entity map |
|
| 4883 | - $classInstance = $this->add_to_entity_map($classInstance); |
|
| 4884 | - } |
|
| 4885 | - } else { |
|
| 4886 | - $classInstance = EE_Registry::instance() |
|
| 4887 | - ->load_class($className, array($this_model_fields_n_values, $this->_timezone), |
|
| 4888 | - true, false); |
|
| 4889 | - } |
|
| 4890 | - //it is entirely possible that the instantiated class object has a set timezone_string db field and has set it's internal _timezone property accordingly (see new_instance_from_db in model objects particularly EE_Event for example). In this case, we want to make sure the model object doesn't have its timezone string overwritten by any timezone property currently set here on the model so, we intentionally override the model _timezone property with the model_object timezone property. |
|
| 4891 | - $this->set_timezone($classInstance->get_timezone()); |
|
| 4892 | - return $classInstance; |
|
| 4893 | - } |
|
| 4894 | - |
|
| 4895 | - |
|
| 4896 | - |
|
| 4897 | - /** |
|
| 4898 | - * Gets the model object from the entity map if it exists |
|
| 4899 | - * |
|
| 4900 | - * @param int|string $id the ID of the model object |
|
| 4901 | - * @return EE_Base_Class |
|
| 4902 | - */ |
|
| 4903 | - public function get_from_entity_map($id) |
|
| 4904 | - { |
|
| 4905 | - return isset($this->_entity_map[EEM_Base::$_model_query_blog_id][$id]) |
|
| 4906 | - ? $this->_entity_map[EEM_Base::$_model_query_blog_id][$id] : null; |
|
| 4907 | - } |
|
| 4908 | - |
|
| 4909 | - |
|
| 4910 | - |
|
| 4911 | - /** |
|
| 4912 | - * add_to_entity_map |
|
| 4913 | - * Adds the object to the model's entity mappings |
|
| 4914 | - * Effectively tells the models "Hey, this model object is the most up-to-date representation of the data, |
|
| 4915 | - * and for the remainder of the request, it's even more up-to-date than what's in the database. |
|
| 4916 | - * So, if the database doesn't agree with what's in the entity mapper, ignore the database" |
|
| 4917 | - * If the database gets updated directly and you want the entity mapper to reflect that change, |
|
| 4918 | - * then this method should be called immediately after the update query |
|
| 4919 | - * Note: The map is indexed by whatever the current blog id is set (via EEM_Base::$_model_query_blog_id). This is |
|
| 4920 | - * so on multisite, the entity map is specific to the query being done for a specific site. |
|
| 4921 | - * |
|
| 4922 | - * @param EE_Base_Class $object |
|
| 4923 | - * @throws EE_Error |
|
| 4924 | - * @return \EE_Base_Class |
|
| 4925 | - */ |
|
| 4926 | - public function add_to_entity_map(EE_Base_Class $object) |
|
| 4927 | - { |
|
| 4928 | - $className = $this->_get_class_name(); |
|
| 4929 | - if (! $object instanceof $className) { |
|
| 4930 | - throw new EE_Error(sprintf(__("You tried adding a %s to a mapping of %ss", "event_espresso"), |
|
| 4931 | - is_object($object) ? get_class($object) : $object, $className)); |
|
| 4932 | - } |
|
| 4933 | - /** @var $object EE_Base_Class */ |
|
| 4934 | - if (! $object->ID()) { |
|
| 4935 | - throw new EE_Error(sprintf(__("You tried storing a model object with NO ID in the %s entity mapper.", |
|
| 4936 | - "event_espresso"), get_class($this))); |
|
| 4937 | - } |
|
| 4938 | - // double check it's not already there |
|
| 4939 | - $classInstance = $this->get_from_entity_map($object->ID()); |
|
| 4940 | - if ($classInstance) { |
|
| 4941 | - return $classInstance; |
|
| 4942 | - } else { |
|
| 4943 | - $this->_entity_map[EEM_Base::$_model_query_blog_id][$object->ID()] = $object; |
|
| 4944 | - return $object; |
|
| 4945 | - } |
|
| 4946 | - } |
|
| 4947 | - |
|
| 4948 | - |
|
| 4949 | - |
|
| 4950 | - /** |
|
| 4951 | - * if a valid identifier is provided, then that entity is unset from the entity map, |
|
| 4952 | - * if no identifier is provided, then the entire entity map is emptied |
|
| 4953 | - * |
|
| 4954 | - * @param int|string $id the ID of the model object |
|
| 4955 | - * @return boolean |
|
| 4956 | - */ |
|
| 4957 | - public function clear_entity_map($id = null) |
|
| 4958 | - { |
|
| 4959 | - if (empty($id)) { |
|
| 4960 | - $this->_entity_map[EEM_Base::$_model_query_blog_id] = array(); |
|
| 4961 | - return true; |
|
| 4962 | - } |
|
| 4963 | - if (isset($this->_entity_map[EEM_Base::$_model_query_blog_id][$id])) { |
|
| 4964 | - unset($this->_entity_map[EEM_Base::$_model_query_blog_id][$id]); |
|
| 4965 | - return true; |
|
| 4966 | - } |
|
| 4967 | - return false; |
|
| 4968 | - } |
|
| 4969 | - |
|
| 4970 | - |
|
| 4971 | - |
|
| 4972 | - /** |
|
| 4973 | - * Public wrapper for _deduce_fields_n_values_from_cols_n_values. |
|
| 4974 | - * Given an array where keys are column (or column alias) names and values, |
|
| 4975 | - * returns an array of their corresponding field names and database values |
|
| 4976 | - * |
|
| 4977 | - * @param array $cols_n_values |
|
| 4978 | - * @return array |
|
| 4979 | - */ |
|
| 4980 | - public function deduce_fields_n_values_from_cols_n_values($cols_n_values) |
|
| 4981 | - { |
|
| 4982 | - return $this->_deduce_fields_n_values_from_cols_n_values($cols_n_values); |
|
| 4983 | - } |
|
| 4984 | - |
|
| 4985 | - |
|
| 4986 | - |
|
| 4987 | - /** |
|
| 4988 | - * _deduce_fields_n_values_from_cols_n_values |
|
| 4989 | - * Given an array where keys are column (or column alias) names and values, |
|
| 4990 | - * returns an array of their corresponding field names and database values |
|
| 4991 | - * |
|
| 4992 | - * @param string $cols_n_values |
|
| 4993 | - * @return array |
|
| 4994 | - */ |
|
| 4995 | - protected function _deduce_fields_n_values_from_cols_n_values($cols_n_values) |
|
| 4996 | - { |
|
| 4997 | - $this_model_fields_n_values = array(); |
|
| 4998 | - foreach ($this->get_tables() as $table_alias => $table_obj) { |
|
| 4999 | - $table_pk_value = $this->_get_column_value_with_table_alias_or_not($cols_n_values, |
|
| 5000 | - $table_obj->get_fully_qualified_pk_column(), $table_obj->get_pk_column()); |
|
| 5001 | - //there is a primary key on this table and its not set. Use defaults for all its columns |
|
| 5002 | - if ($table_pk_value === null && $table_obj->get_pk_column()) { |
|
| 5003 | - foreach ($this->_get_fields_for_table($table_alias) as $field_name => $field_obj) { |
|
| 5004 | - if (! $field_obj->is_db_only_field()) { |
|
| 5005 | - //prepare field as if its coming from db |
|
| 5006 | - $prepared_value = $field_obj->prepare_for_set($field_obj->get_default_value()); |
|
| 5007 | - $this_model_fields_n_values[$field_name] = $field_obj->prepare_for_use_in_db($prepared_value); |
|
| 5008 | - } |
|
| 5009 | - } |
|
| 5010 | - } else { |
|
| 5011 | - //the table's rows existed. Use their values |
|
| 5012 | - foreach ($this->_get_fields_for_table($table_alias) as $field_name => $field_obj) { |
|
| 5013 | - if (! $field_obj->is_db_only_field()) { |
|
| 5014 | - $this_model_fields_n_values[$field_name] = $this->_get_column_value_with_table_alias_or_not( |
|
| 5015 | - $cols_n_values, $field_obj->get_qualified_column(), |
|
| 5016 | - $field_obj->get_table_column() |
|
| 5017 | - ); |
|
| 5018 | - } |
|
| 5019 | - } |
|
| 5020 | - } |
|
| 5021 | - } |
|
| 5022 | - return $this_model_fields_n_values; |
|
| 5023 | - } |
|
| 5024 | - |
|
| 5025 | - |
|
| 5026 | - |
|
| 5027 | - /** |
|
| 5028 | - * @param $cols_n_values |
|
| 5029 | - * @param $qualified_column |
|
| 5030 | - * @param $regular_column |
|
| 5031 | - * @return null |
|
| 5032 | - */ |
|
| 5033 | - protected function _get_column_value_with_table_alias_or_not($cols_n_values, $qualified_column, $regular_column) |
|
| 5034 | - { |
|
| 5035 | - $value = null; |
|
| 5036 | - //ask the field what it think it's table_name.column_name should be, and call it the "qualified column" |
|
| 5037 | - //does the field on the model relate to this column retrieved from the db? |
|
| 5038 | - //or is it a db-only field? (not relating to the model) |
|
| 5039 | - if (isset($cols_n_values[$qualified_column])) { |
|
| 5040 | - $value = $cols_n_values[$qualified_column]; |
|
| 5041 | - } elseif (isset($cols_n_values[$regular_column])) { |
|
| 5042 | - $value = $cols_n_values[$regular_column]; |
|
| 5043 | - } |
|
| 5044 | - return $value; |
|
| 5045 | - } |
|
| 5046 | - |
|
| 5047 | - |
|
| 5048 | - |
|
| 5049 | - /** |
|
| 5050 | - * refresh_entity_map_from_db |
|
| 5051 | - * Makes sure the model object in the entity map at $id assumes the values |
|
| 5052 | - * of the database (opposite of EE_base_Class::save()) |
|
| 5053 | - * |
|
| 5054 | - * @param int|string $id |
|
| 5055 | - * @return EE_Base_Class |
|
| 5056 | - * @throws \EE_Error |
|
| 5057 | - */ |
|
| 5058 | - public function refresh_entity_map_from_db($id) |
|
| 5059 | - { |
|
| 5060 | - $obj_in_map = $this->get_from_entity_map($id); |
|
| 5061 | - if ($obj_in_map) { |
|
| 5062 | - $wpdb_results = $this->_get_all_wpdb_results( |
|
| 5063 | - array(array($this->get_primary_key_field()->get_name() => $id), 'limit' => 1) |
|
| 5064 | - ); |
|
| 5065 | - if ($wpdb_results && is_array($wpdb_results)) { |
|
| 5066 | - $one_row = reset($wpdb_results); |
|
| 5067 | - foreach ($this->_deduce_fields_n_values_from_cols_n_values($one_row) as $field_name => $db_value) { |
|
| 5068 | - $obj_in_map->set_from_db($field_name, $db_value); |
|
| 5069 | - } |
|
| 5070 | - //clear the cache of related model objects |
|
| 5071 | - foreach ($this->relation_settings() as $relation_name => $relation_obj) { |
|
| 5072 | - $obj_in_map->clear_cache($relation_name, null, true); |
|
| 5073 | - } |
|
| 5074 | - } |
|
| 5075 | - $this->_entity_map[EEM_Base::$_model_query_blog_id][$id] = $obj_in_map; |
|
| 5076 | - return $obj_in_map; |
|
| 5077 | - } else { |
|
| 5078 | - return $this->get_one_by_ID($id); |
|
| 5079 | - } |
|
| 5080 | - } |
|
| 5081 | - |
|
| 5082 | - |
|
| 5083 | - |
|
| 5084 | - /** |
|
| 5085 | - * refresh_entity_map_with |
|
| 5086 | - * Leaves the entry in the entity map alone, but updates it to match the provided |
|
| 5087 | - * $replacing_model_obj (which we assume to be its equivalent but somehow NOT in the entity map). |
|
| 5088 | - * This is useful if you have a model object you want to make authoritative over what's in the entity map currently. |
|
| 5089 | - * Note: The old $replacing_model_obj should now be destroyed as it's now un-authoritative |
|
| 5090 | - * |
|
| 5091 | - * @param int|string $id |
|
| 5092 | - * @param EE_Base_Class $replacing_model_obj |
|
| 5093 | - * @return \EE_Base_Class |
|
| 5094 | - * @throws \EE_Error |
|
| 5095 | - */ |
|
| 5096 | - public function refresh_entity_map_with($id, $replacing_model_obj) |
|
| 5097 | - { |
|
| 5098 | - $obj_in_map = $this->get_from_entity_map($id); |
|
| 5099 | - if ($obj_in_map) { |
|
| 5100 | - if ($replacing_model_obj instanceof EE_Base_Class) { |
|
| 5101 | - foreach ($replacing_model_obj->model_field_array() as $field_name => $value) { |
|
| 5102 | - $obj_in_map->set($field_name, $value); |
|
| 5103 | - } |
|
| 5104 | - //make the model object in the entity map's cache match the $replacing_model_obj |
|
| 5105 | - foreach ($this->relation_settings() as $relation_name => $relation_obj) { |
|
| 5106 | - $obj_in_map->clear_cache($relation_name, null, true); |
|
| 5107 | - foreach ($replacing_model_obj->get_all_from_cache($relation_name) as $cache_id => $cached_obj) { |
|
| 5108 | - $obj_in_map->cache($relation_name, $cached_obj, $cache_id); |
|
| 5109 | - } |
|
| 5110 | - } |
|
| 5111 | - } |
|
| 5112 | - return $obj_in_map; |
|
| 5113 | - } else { |
|
| 5114 | - $this->add_to_entity_map($replacing_model_obj); |
|
| 5115 | - return $replacing_model_obj; |
|
| 5116 | - } |
|
| 5117 | - } |
|
| 5118 | - |
|
| 5119 | - |
|
| 5120 | - |
|
| 5121 | - /** |
|
| 5122 | - * Gets the EE class that corresponds to this model. Eg, for EEM_Answer that |
|
| 5123 | - * would be EE_Answer.To import that class, you'd just add ".class.php" to the name, like so |
|
| 5124 | - * require_once($this->_getClassName().".class.php"); |
|
| 5125 | - * |
|
| 5126 | - * @return string |
|
| 5127 | - */ |
|
| 5128 | - private function _get_class_name() |
|
| 5129 | - { |
|
| 5130 | - return "EE_" . $this->get_this_model_name(); |
|
| 5131 | - } |
|
| 5132 | - |
|
| 5133 | - |
|
| 5134 | - |
|
| 5135 | - /** |
|
| 5136 | - * Get the name of the items this model represents, for the quantity specified. Eg, |
|
| 5137 | - * if $quantity==1, on EEM_Event, it would 'Event' (internationalized), otherwise |
|
| 5138 | - * it would be 'Events'. |
|
| 5139 | - * |
|
| 5140 | - * @param int $quantity |
|
| 5141 | - * @return string |
|
| 5142 | - */ |
|
| 5143 | - public function item_name($quantity = 1) |
|
| 5144 | - { |
|
| 5145 | - return (int)$quantity === 1 ? $this->singular_item : $this->plural_item; |
|
| 5146 | - } |
|
| 5147 | - |
|
| 5148 | - |
|
| 5149 | - |
|
| 5150 | - /** |
|
| 5151 | - * Very handy general function to allow for plugins to extend any child of EE_TempBase. |
|
| 5152 | - * If a method is called on a child of EE_TempBase that doesn't exist, this function is called |
|
| 5153 | - * (http://www.garfieldtech.com/blog/php-magic-call) and passed the method's name and arguments. Instead of |
|
| 5154 | - * requiring a plugin to extend the EE_TempBase (which works fine is there's only 1 plugin, but when will that |
|
| 5155 | - * happen?) they can add a hook onto 'filters_hook_espresso__{className}__{methodName}' (eg, |
|
| 5156 | - * filters_hook_espresso__EE_Answer__my_great_function) and accepts 2 arguments: the object on which the function |
|
| 5157 | - * was called, and an array of the original arguments passed to the function. Whatever their callback function |
|
| 5158 | - * returns will be returned by this function. Example: in functions.php (or in a plugin): |
|
| 5159 | - * add_filter('FHEE__EE_Answer__my_callback','my_callback',10,3); function |
|
| 5160 | - * my_callback($previousReturnValue,EE_TempBase $object,$argsArray){ |
|
| 5161 | - * $returnString= "you called my_callback! and passed args:".implode(",",$argsArray); |
|
| 5162 | - * return $previousReturnValue.$returnString; |
|
| 5163 | - * } |
|
| 5164 | - * require('EEM_Answer.model.php'); |
|
| 5165 | - * $answer=EEM_Answer::instance(); |
|
| 5166 | - * echo $answer->my_callback('monkeys',100); |
|
| 5167 | - * //will output "you called my_callback! and passed args:monkeys,100" |
|
| 5168 | - * |
|
| 5169 | - * @param string $methodName name of method which was called on a child of EE_TempBase, but which |
|
| 5170 | - * @param array $args array of original arguments passed to the function |
|
| 5171 | - * @throws EE_Error |
|
| 5172 | - * @return mixed whatever the plugin which calls add_filter decides |
|
| 5173 | - */ |
|
| 5174 | - public function __call($methodName, $args) |
|
| 5175 | - { |
|
| 5176 | - $className = get_class($this); |
|
| 5177 | - $tagName = "FHEE__{$className}__{$methodName}"; |
|
| 5178 | - if (! has_filter($tagName)) { |
|
| 5179 | - throw new EE_Error( |
|
| 5180 | - sprintf( |
|
| 5181 | - __('Method %1$s on model %2$s does not exist! You can create one with the following code in functions.php or in a plugin: %4$s function my_callback(%4$s \$previousReturnValue, EEM_Base \$object\ $argsArray=NULL ){%4$s /*function body*/%4$s return \$whatever;%4$s }%4$s add_filter( \'%3$s\', \'my_callback\', 10, 3 );', |
|
| 5182 | - 'event_espresso'), |
|
| 5183 | - $methodName, |
|
| 5184 | - $className, |
|
| 5185 | - $tagName, |
|
| 5186 | - '<br />' |
|
| 5187 | - ) |
|
| 5188 | - ); |
|
| 5189 | - } |
|
| 5190 | - return apply_filters($tagName, null, $this, $args); |
|
| 5191 | - } |
|
| 5192 | - |
|
| 5193 | - |
|
| 5194 | - |
|
| 5195 | - /** |
|
| 5196 | - * Ensures $base_class_obj_or_id is of the EE_Base_Class child that corresponds ot this model. |
|
| 5197 | - * If not, assumes its an ID, and uses $this->get_one_by_ID() to get the EE_Base_Class. |
|
| 5198 | - * |
|
| 5199 | - * @param EE_Base_Class|string|int $base_class_obj_or_id either: |
|
| 5200 | - * the EE_Base_Class object that corresponds to this Model, |
|
| 5201 | - * the object's class name |
|
| 5202 | - * or object's ID |
|
| 5203 | - * @param boolean $ensure_is_in_db if set, we will also verify this model object |
|
| 5204 | - * exists in the database. If it does not, we add it |
|
| 5205 | - * @throws EE_Error |
|
| 5206 | - * @return EE_Base_Class |
|
| 5207 | - */ |
|
| 5208 | - public function ensure_is_obj($base_class_obj_or_id, $ensure_is_in_db = false) |
|
| 5209 | - { |
|
| 5210 | - $className = $this->_get_class_name(); |
|
| 5211 | - if ($base_class_obj_or_id instanceof $className) { |
|
| 5212 | - $model_object = $base_class_obj_or_id; |
|
| 5213 | - } else { |
|
| 5214 | - $primary_key_field = $this->get_primary_key_field(); |
|
| 5215 | - if ( |
|
| 5216 | - $primary_key_field instanceof EE_Primary_Key_Int_Field |
|
| 5217 | - && ( |
|
| 5218 | - is_int($base_class_obj_or_id) |
|
| 5219 | - || is_string($base_class_obj_or_id) |
|
| 5220 | - ) |
|
| 5221 | - ) { |
|
| 5222 | - // assume it's an ID. |
|
| 5223 | - // either a proper integer or a string representing an integer (eg "101" instead of 101) |
|
| 5224 | - $model_object = $this->get_one_by_ID($base_class_obj_or_id); |
|
| 5225 | - } else if ( |
|
| 5226 | - $primary_key_field instanceof EE_Primary_Key_String_Field |
|
| 5227 | - && is_string($base_class_obj_or_id) |
|
| 5228 | - ) { |
|
| 5229 | - // assume its a string representation of the object |
|
| 5230 | - $model_object = $this->get_one_by_ID($base_class_obj_or_id); |
|
| 5231 | - } else { |
|
| 5232 | - throw new EE_Error( |
|
| 5233 | - sprintf( |
|
| 5234 | - __( |
|
| 5235 | - "'%s' is neither an object of type %s, nor an ID! Its full value is '%s'", |
|
| 5236 | - 'event_espresso' |
|
| 5237 | - ), |
|
| 5238 | - $base_class_obj_or_id, |
|
| 5239 | - $this->_get_class_name(), |
|
| 5240 | - print_r($base_class_obj_or_id, true) |
|
| 5241 | - ) |
|
| 5242 | - ); |
|
| 5243 | - } |
|
| 5244 | - } |
|
| 5245 | - if ($ensure_is_in_db && $model_object->ID() !== null) { |
|
| 5246 | - $model_object->save(); |
|
| 5247 | - } |
|
| 5248 | - return $model_object; |
|
| 5249 | - } |
|
| 5250 | - |
|
| 5251 | - |
|
| 5252 | - |
|
| 5253 | - /** |
|
| 5254 | - * Similar to ensure_is_obj(), this method makes sure $base_class_obj_or_id |
|
| 5255 | - * is a value of the this model's primary key. If it's an EE_Base_Class child, |
|
| 5256 | - * returns it ID. |
|
| 5257 | - * |
|
| 5258 | - * @param EE_Base_Class|int|string $base_class_obj_or_id |
|
| 5259 | - * @return int|string depending on the type of this model object's ID |
|
| 5260 | - * @throws EE_Error |
|
| 5261 | - */ |
|
| 5262 | - public function ensure_is_ID($base_class_obj_or_id) |
|
| 5263 | - { |
|
| 5264 | - $className = $this->_get_class_name(); |
|
| 5265 | - if ($base_class_obj_or_id instanceof $className) { |
|
| 5266 | - /** @var $base_class_obj_or_id EE_Base_Class */ |
|
| 5267 | - $id = $base_class_obj_or_id->ID(); |
|
| 5268 | - } elseif (is_int($base_class_obj_or_id)) { |
|
| 5269 | - //assume it's an ID |
|
| 5270 | - $id = $base_class_obj_or_id; |
|
| 5271 | - } elseif (is_string($base_class_obj_or_id)) { |
|
| 5272 | - //assume its a string representation of the object |
|
| 5273 | - $id = $base_class_obj_or_id; |
|
| 5274 | - } else { |
|
| 5275 | - throw new EE_Error(sprintf(__("'%s' is neither an object of type %s, nor an ID! Its full value is '%s'", |
|
| 5276 | - 'event_espresso'), $base_class_obj_or_id, $this->_get_class_name(), |
|
| 5277 | - print_r($base_class_obj_or_id, true))); |
|
| 5278 | - } |
|
| 5279 | - return $id; |
|
| 5280 | - } |
|
| 5281 | - |
|
| 5282 | - |
|
| 5283 | - |
|
| 5284 | - /** |
|
| 5285 | - * Sets whether the values passed to the model (eg, values in WHERE, values in INSERT, UPDATE, etc) |
|
| 5286 | - * have already been ran through the appropriate model field's prepare_for_use_in_db method. IE, they have |
|
| 5287 | - * been sanitized and converted into the appropriate domain. |
|
| 5288 | - * Usually the only place you'll want to change the default (which is to assume values have NOT been sanitized by |
|
| 5289 | - * the model object/model field) is when making a method call from WITHIN a model object, which has direct access |
|
| 5290 | - * to its sanitized values. Note: after changing this setting, you should set it back to its previous value (using |
|
| 5291 | - * get_assumption_concerning_values_already_prepared_by_model_object()) eg. |
|
| 5292 | - * $EVT = EEM_Event::instance(); $old_setting = |
|
| 5293 | - * $EVT->get_assumption_concerning_values_already_prepared_by_model_object(); |
|
| 5294 | - * $EVT->assume_values_already_prepared_by_model_object(true); |
|
| 5295 | - * $EVT->update(array('foo'=>'bar'),array(array('foo'=>'monkey'))); |
|
| 5296 | - * $EVT->assume_values_already_prepared_by_model_object($old_setting); |
|
| 5297 | - * |
|
| 5298 | - * @param int $values_already_prepared like one of the constants on EEM_Base |
|
| 5299 | - * @return void |
|
| 5300 | - */ |
|
| 5301 | - public function assume_values_already_prepared_by_model_object( |
|
| 5302 | - $values_already_prepared = self::not_prepared_by_model_object |
|
| 5303 | - ) { |
|
| 5304 | - $this->_values_already_prepared_by_model_object = $values_already_prepared; |
|
| 5305 | - } |
|
| 5306 | - |
|
| 5307 | - |
|
| 5308 | - |
|
| 5309 | - /** |
|
| 5310 | - * Read comments for assume_values_already_prepared_by_model_object() |
|
| 5311 | - * |
|
| 5312 | - * @return int |
|
| 5313 | - */ |
|
| 5314 | - public function get_assumption_concerning_values_already_prepared_by_model_object() |
|
| 5315 | - { |
|
| 5316 | - return $this->_values_already_prepared_by_model_object; |
|
| 5317 | - } |
|
| 5318 | - |
|
| 5319 | - |
|
| 5320 | - |
|
| 5321 | - /** |
|
| 5322 | - * Gets all the indexes on this model |
|
| 5323 | - * |
|
| 5324 | - * @return EE_Index[] |
|
| 5325 | - */ |
|
| 5326 | - public function indexes() |
|
| 5327 | - { |
|
| 5328 | - return $this->_indexes; |
|
| 5329 | - } |
|
| 5330 | - |
|
| 5331 | - |
|
| 5332 | - |
|
| 5333 | - /** |
|
| 5334 | - * Gets all the Unique Indexes on this model |
|
| 5335 | - * |
|
| 5336 | - * @return EE_Unique_Index[] |
|
| 5337 | - */ |
|
| 5338 | - public function unique_indexes() |
|
| 5339 | - { |
|
| 5340 | - $unique_indexes = array(); |
|
| 5341 | - foreach ($this->_indexes as $name => $index) { |
|
| 5342 | - if ($index instanceof EE_Unique_Index) { |
|
| 5343 | - $unique_indexes [$name] = $index; |
|
| 5344 | - } |
|
| 5345 | - } |
|
| 5346 | - return $unique_indexes; |
|
| 5347 | - } |
|
| 5348 | - |
|
| 5349 | - |
|
| 5350 | - |
|
| 5351 | - /** |
|
| 5352 | - * Gets all the fields which, when combined, make the primary key. |
|
| 5353 | - * This is usually just an array with 1 element (the primary key), but in cases |
|
| 5354 | - * where there is no primary key, it's a combination of fields as defined |
|
| 5355 | - * on a primary index |
|
| 5356 | - * |
|
| 5357 | - * @return EE_Model_Field_Base[] indexed by the field's name |
|
| 5358 | - * @throws \EE_Error |
|
| 5359 | - */ |
|
| 5360 | - public function get_combined_primary_key_fields() |
|
| 5361 | - { |
|
| 5362 | - foreach ($this->indexes() as $index) { |
|
| 5363 | - if ($index instanceof EE_Primary_Key_Index) { |
|
| 5364 | - return $index->fields(); |
|
| 5365 | - } |
|
| 5366 | - } |
|
| 5367 | - return array($this->primary_key_name() => $this->get_primary_key_field()); |
|
| 5368 | - } |
|
| 5369 | - |
|
| 5370 | - |
|
| 5371 | - |
|
| 5372 | - /** |
|
| 5373 | - * Used to build a primary key string (when the model has no primary key), |
|
| 5374 | - * which can be used a unique string to identify this model object. |
|
| 5375 | - * |
|
| 5376 | - * @param array $cols_n_values keys are field names, values are their values |
|
| 5377 | - * @return string |
|
| 5378 | - * @throws \EE_Error |
|
| 5379 | - */ |
|
| 5380 | - public function get_index_primary_key_string($cols_n_values) |
|
| 5381 | - { |
|
| 5382 | - $cols_n_values_for_primary_key_index = array_intersect_key($cols_n_values, |
|
| 5383 | - $this->get_combined_primary_key_fields()); |
|
| 5384 | - return http_build_query($cols_n_values_for_primary_key_index); |
|
| 5385 | - } |
|
| 5386 | - |
|
| 5387 | - |
|
| 5388 | - |
|
| 5389 | - /** |
|
| 5390 | - * Gets the field values from the primary key string |
|
| 5391 | - * |
|
| 5392 | - * @see EEM_Base::get_combined_primary_key_fields() and EEM_Base::get_index_primary_key_string() |
|
| 5393 | - * @param string $index_primary_key_string |
|
| 5394 | - * @return null|array |
|
| 5395 | - * @throws \EE_Error |
|
| 5396 | - */ |
|
| 5397 | - public function parse_index_primary_key_string($index_primary_key_string) |
|
| 5398 | - { |
|
| 5399 | - $key_fields = $this->get_combined_primary_key_fields(); |
|
| 5400 | - //check all of them are in the $id |
|
| 5401 | - $key_vals_in_combined_pk = array(); |
|
| 5402 | - parse_str($index_primary_key_string, $key_vals_in_combined_pk); |
|
| 5403 | - foreach ($key_fields as $key_field_name => $field_obj) { |
|
| 5404 | - if (! isset($key_vals_in_combined_pk[$key_field_name])) { |
|
| 5405 | - return null; |
|
| 5406 | - } |
|
| 5407 | - } |
|
| 5408 | - return $key_vals_in_combined_pk; |
|
| 5409 | - } |
|
| 5410 | - |
|
| 5411 | - |
|
| 5412 | - |
|
| 5413 | - /** |
|
| 5414 | - * verifies that an array of key-value pairs for model fields has a key |
|
| 5415 | - * for each field comprising the primary key index |
|
| 5416 | - * |
|
| 5417 | - * @param array $key_vals |
|
| 5418 | - * @return boolean |
|
| 5419 | - * @throws \EE_Error |
|
| 5420 | - */ |
|
| 5421 | - public function has_all_combined_primary_key_fields($key_vals) |
|
| 5422 | - { |
|
| 5423 | - $keys_it_should_have = array_keys($this->get_combined_primary_key_fields()); |
|
| 5424 | - foreach ($keys_it_should_have as $key) { |
|
| 5425 | - if (! isset($key_vals[$key])) { |
|
| 5426 | - return false; |
|
| 5427 | - } |
|
| 5428 | - } |
|
| 5429 | - return true; |
|
| 5430 | - } |
|
| 5431 | - |
|
| 5432 | - |
|
| 5433 | - |
|
| 5434 | - /** |
|
| 5435 | - * Finds all model objects in the DB that appear to be a copy of $model_object_or_attributes_array. |
|
| 5436 | - * We consider something to be a copy if all the attributes match (except the ID, of course). |
|
| 5437 | - * |
|
| 5438 | - * @param array|EE_Base_Class $model_object_or_attributes_array If its an array, it's field-value pairs |
|
| 5439 | - * @param array $query_params like EEM_Base::get_all's query_params. |
|
| 5440 | - * @throws EE_Error |
|
| 5441 | - * @return \EE_Base_Class[] Array keys are object IDs (if there is a primary key on the model. if not, numerically |
|
| 5442 | - * indexed) |
|
| 5443 | - */ |
|
| 5444 | - public function get_all_copies($model_object_or_attributes_array, $query_params = array()) |
|
| 5445 | - { |
|
| 5446 | - if ($model_object_or_attributes_array instanceof EE_Base_Class) { |
|
| 5447 | - $attributes_array = $model_object_or_attributes_array->model_field_array(); |
|
| 5448 | - } elseif (is_array($model_object_or_attributes_array)) { |
|
| 5449 | - $attributes_array = $model_object_or_attributes_array; |
|
| 5450 | - } else { |
|
| 5451 | - throw new EE_Error(sprintf(__("get_all_copies should be provided with either a model object or an array of field-value-pairs, but was given %s", |
|
| 5452 | - "event_espresso"), $model_object_or_attributes_array)); |
|
| 5453 | - } |
|
| 5454 | - //even copies obviously won't have the same ID, so remove the primary key |
|
| 5455 | - //from the WHERE conditions for finding copies (if there is a primary key, of course) |
|
| 5456 | - if ($this->has_primary_key_field() && isset($attributes_array[$this->primary_key_name()])) { |
|
| 5457 | - unset($attributes_array[$this->primary_key_name()]); |
|
| 5458 | - } |
|
| 5459 | - if (isset($query_params[0])) { |
|
| 5460 | - $query_params[0] = array_merge($attributes_array, $query_params); |
|
| 5461 | - } else { |
|
| 5462 | - $query_params[0] = $attributes_array; |
|
| 5463 | - } |
|
| 5464 | - return $this->get_all($query_params); |
|
| 5465 | - } |
|
| 5466 | - |
|
| 5467 | - |
|
| 5468 | - |
|
| 5469 | - /** |
|
| 5470 | - * Gets the first copy we find. See get_all_copies for more details |
|
| 5471 | - * |
|
| 5472 | - * @param mixed EE_Base_Class | array $model_object_or_attributes_array |
|
| 5473 | - * @param array $query_params |
|
| 5474 | - * @return EE_Base_Class |
|
| 5475 | - * @throws \EE_Error |
|
| 5476 | - */ |
|
| 5477 | - public function get_one_copy($model_object_or_attributes_array, $query_params = array()) |
|
| 5478 | - { |
|
| 5479 | - if (! is_array($query_params)) { |
|
| 5480 | - EE_Error::doing_it_wrong('EEM_Base::get_one_copy', |
|
| 5481 | - sprintf(__('$query_params should be an array, you passed a variable of type %s', 'event_espresso'), |
|
| 5482 | - gettype($query_params)), '4.6.0'); |
|
| 5483 | - $query_params = array(); |
|
| 5484 | - } |
|
| 5485 | - $query_params['limit'] = 1; |
|
| 5486 | - $copies = $this->get_all_copies($model_object_or_attributes_array, $query_params); |
|
| 5487 | - if (is_array($copies)) { |
|
| 5488 | - return array_shift($copies); |
|
| 5489 | - } else { |
|
| 5490 | - return null; |
|
| 5491 | - } |
|
| 5492 | - } |
|
| 5493 | - |
|
| 5494 | - |
|
| 5495 | - |
|
| 5496 | - /** |
|
| 5497 | - * Updates the item with the specified id. Ignores default query parameters because |
|
| 5498 | - * we have specified the ID, and its assumed we KNOW what we're doing |
|
| 5499 | - * |
|
| 5500 | - * @param array $fields_n_values keys are field names, values are their new values |
|
| 5501 | - * @param int|string $id the value of the primary key to update |
|
| 5502 | - * @return int number of rows updated |
|
| 5503 | - * @throws \EE_Error |
|
| 5504 | - */ |
|
| 5505 | - public function update_by_ID($fields_n_values, $id) |
|
| 5506 | - { |
|
| 5507 | - $query_params = array( |
|
| 5508 | - 0 => array($this->get_primary_key_field()->get_name() => $id), |
|
| 5509 | - 'default_where_conditions' => EEM_Base::default_where_conditions_others_only, |
|
| 5510 | - ); |
|
| 5511 | - return $this->update($fields_n_values, $query_params); |
|
| 5512 | - } |
|
| 5513 | - |
|
| 5514 | - |
|
| 5515 | - |
|
| 5516 | - /** |
|
| 5517 | - * Changes an operator which was supplied to the models into one usable in SQL |
|
| 5518 | - * |
|
| 5519 | - * @param string $operator_supplied |
|
| 5520 | - * @return string an operator which can be used in SQL |
|
| 5521 | - * @throws EE_Error |
|
| 5522 | - */ |
|
| 5523 | - private function _prepare_operator_for_sql($operator_supplied) |
|
| 5524 | - { |
|
| 5525 | - $sql_operator = isset($this->_valid_operators[$operator_supplied]) ? $this->_valid_operators[$operator_supplied] |
|
| 5526 | - : null; |
|
| 5527 | - if ($sql_operator) { |
|
| 5528 | - return $sql_operator; |
|
| 5529 | - } else { |
|
| 5530 | - throw new EE_Error(sprintf(__("The operator '%s' is not in the list of valid operators: %s", |
|
| 5531 | - "event_espresso"), $operator_supplied, implode(",", array_keys($this->_valid_operators)))); |
|
| 5532 | - } |
|
| 5533 | - } |
|
| 5534 | - |
|
| 5535 | - |
|
| 5536 | - |
|
| 5537 | - /** |
|
| 5538 | - * Gets the valid operators |
|
| 5539 | - * @return array keys are accepted strings, values are the SQL they are converted to |
|
| 5540 | - */ |
|
| 5541 | - public function valid_operators(){ |
|
| 5542 | - return $this->_valid_operators; |
|
| 5543 | - } |
|
| 5544 | - |
|
| 5545 | - |
|
| 5546 | - |
|
| 5547 | - /** |
|
| 5548 | - * Gets the between-style operators (take 2 arguments). |
|
| 5549 | - * @return array keys are accepted strings, values are the SQL they are converted to |
|
| 5550 | - */ |
|
| 5551 | - public function valid_between_style_operators() |
|
| 5552 | - { |
|
| 5553 | - return array_intersect( |
|
| 5554 | - $this->valid_operators(), |
|
| 5555 | - $this->_between_style_operators |
|
| 5556 | - ); |
|
| 5557 | - } |
|
| 5558 | - |
|
| 5559 | - /** |
|
| 5560 | - * Gets the "like"-style operators (take a single argument, but it may contain wildcards) |
|
| 5561 | - * @return array keys are accepted strings, values are the SQL they are converted to |
|
| 5562 | - */ |
|
| 5563 | - public function valid_like_style_operators() |
|
| 5564 | - { |
|
| 5565 | - return array_intersect( |
|
| 5566 | - $this->valid_operators(), |
|
| 5567 | - $this->_like_style_operators |
|
| 5568 | - ); |
|
| 5569 | - } |
|
| 5570 | - |
|
| 5571 | - /** |
|
| 5572 | - * Gets the "in"-style operators |
|
| 5573 | - * @return array keys are accepted strings, values are the SQL they are converted to |
|
| 5574 | - */ |
|
| 5575 | - public function valid_in_style_operators() |
|
| 5576 | - { |
|
| 5577 | - return array_intersect( |
|
| 5578 | - $this->valid_operators(), |
|
| 5579 | - $this->_in_style_operators |
|
| 5580 | - ); |
|
| 5581 | - } |
|
| 5582 | - |
|
| 5583 | - /** |
|
| 5584 | - * Gets the "null"-style operators (accept no arguments) |
|
| 5585 | - * @return array keys are accepted strings, values are the SQL they are converted to |
|
| 5586 | - */ |
|
| 5587 | - public function valid_null_style_operators() |
|
| 5588 | - { |
|
| 5589 | - return array_intersect( |
|
| 5590 | - $this->valid_operators(), |
|
| 5591 | - $this->_null_style_operators |
|
| 5592 | - ); |
|
| 5593 | - } |
|
| 5594 | - |
|
| 5595 | - /** |
|
| 5596 | - * Gets an array where keys are the primary keys and values are their 'names' |
|
| 5597 | - * (as determined by the model object's name() function, which is often overridden) |
|
| 5598 | - * |
|
| 5599 | - * @param array $query_params like get_all's |
|
| 5600 | - * @return string[] |
|
| 5601 | - * @throws \EE_Error |
|
| 5602 | - */ |
|
| 5603 | - public function get_all_names($query_params = array()) |
|
| 5604 | - { |
|
| 5605 | - $objs = $this->get_all($query_params); |
|
| 5606 | - $names = array(); |
|
| 5607 | - foreach ($objs as $obj) { |
|
| 5608 | - $names[$obj->ID()] = $obj->name(); |
|
| 5609 | - } |
|
| 5610 | - return $names; |
|
| 5611 | - } |
|
| 5612 | - |
|
| 5613 | - |
|
| 5614 | - |
|
| 5615 | - /** |
|
| 5616 | - * Gets an array of primary keys from the model objects. If you acquired the model objects |
|
| 5617 | - * using EEM_Base::get_all() you don't need to call this (and probably shouldn't because |
|
| 5618 | - * this is duplicated effort and reduces efficiency) you would be better to use |
|
| 5619 | - * array_keys() on $model_objects. |
|
| 5620 | - * |
|
| 5621 | - * @param \EE_Base_Class[] $model_objects |
|
| 5622 | - * @param boolean $filter_out_empty_ids if a model object has an ID of '' or 0, don't bother including it |
|
| 5623 | - * in the returned array |
|
| 5624 | - * @return array |
|
| 5625 | - * @throws \EE_Error |
|
| 5626 | - */ |
|
| 5627 | - public function get_IDs($model_objects, $filter_out_empty_ids = false) |
|
| 5628 | - { |
|
| 5629 | - if (! $this->has_primary_key_field()) { |
|
| 5630 | - if (WP_DEBUG) { |
|
| 5631 | - EE_Error::add_error( |
|
| 5632 | - __('Trying to get IDs from a model than has no primary key', 'event_espresso'), |
|
| 5633 | - __FILE__, |
|
| 5634 | - __FUNCTION__, |
|
| 5635 | - __LINE__ |
|
| 5636 | - ); |
|
| 5637 | - } |
|
| 5638 | - } |
|
| 5639 | - $IDs = array(); |
|
| 5640 | - foreach ($model_objects as $model_object) { |
|
| 5641 | - $id = $model_object->ID(); |
|
| 5642 | - if (! $id) { |
|
| 5643 | - if ($filter_out_empty_ids) { |
|
| 5644 | - continue; |
|
| 5645 | - } |
|
| 5646 | - if (WP_DEBUG) { |
|
| 5647 | - EE_Error::add_error( |
|
| 5648 | - __( |
|
| 5649 | - 'Called %1$s on a model object that has no ID and so probably hasn\'t been saved to the database', |
|
| 5650 | - 'event_espresso' |
|
| 5651 | - ), |
|
| 5652 | - __FILE__, |
|
| 5653 | - __FUNCTION__, |
|
| 5654 | - __LINE__ |
|
| 5655 | - ); |
|
| 5656 | - } |
|
| 5657 | - } |
|
| 5658 | - $IDs[] = $id; |
|
| 5659 | - } |
|
| 5660 | - return $IDs; |
|
| 5661 | - } |
|
| 5662 | - |
|
| 5663 | - |
|
| 5664 | - |
|
| 5665 | - /** |
|
| 5666 | - * Returns the string used in capabilities relating to this model. If there |
|
| 5667 | - * are no capabilities that relate to this model returns false |
|
| 5668 | - * |
|
| 5669 | - * @return string|false |
|
| 5670 | - */ |
|
| 5671 | - public function cap_slug() |
|
| 5672 | - { |
|
| 5673 | - return apply_filters('FHEE__EEM_Base__cap_slug', $this->_caps_slug, $this); |
|
| 5674 | - } |
|
| 5675 | - |
|
| 5676 | - |
|
| 5677 | - |
|
| 5678 | - /** |
|
| 5679 | - * Returns the capability-restrictions array (@see EEM_Base::_cap_restrictions). |
|
| 5680 | - * If $context is provided (which should be set to one of EEM_Base::valid_cap_contexts()) |
|
| 5681 | - * only returns the cap restrictions array in that context (ie, the array |
|
| 5682 | - * at that key) |
|
| 5683 | - * |
|
| 5684 | - * @param string $context |
|
| 5685 | - * @return EE_Default_Where_Conditions[] indexed by associated capability |
|
| 5686 | - * @throws \EE_Error |
|
| 5687 | - */ |
|
| 5688 | - public function cap_restrictions($context = EEM_Base::caps_read) |
|
| 5689 | - { |
|
| 5690 | - EEM_Base::verify_is_valid_cap_context($context); |
|
| 5691 | - //check if we ought to run the restriction generator first |
|
| 5692 | - if ( |
|
| 5693 | - isset($this->_cap_restriction_generators[$context]) |
|
| 5694 | - && $this->_cap_restriction_generators[$context] instanceof EE_Restriction_Generator_Base |
|
| 5695 | - && ! $this->_cap_restriction_generators[$context]->has_generated_cap_restrictions() |
|
| 5696 | - ) { |
|
| 5697 | - $this->_cap_restrictions[$context] = array_merge( |
|
| 5698 | - $this->_cap_restrictions[$context], |
|
| 5699 | - $this->_cap_restriction_generators[$context]->generate_restrictions() |
|
| 5700 | - ); |
|
| 5701 | - } |
|
| 5702 | - //and make sure we've finalized the construction of each restriction |
|
| 5703 | - foreach ($this->_cap_restrictions[$context] as $where_conditions_obj) { |
|
| 5704 | - if ($where_conditions_obj instanceof EE_Default_Where_Conditions) { |
|
| 5705 | - $where_conditions_obj->_finalize_construct($this); |
|
| 5706 | - } |
|
| 5707 | - } |
|
| 5708 | - return $this->_cap_restrictions[$context]; |
|
| 5709 | - } |
|
| 5710 | - |
|
| 5711 | - |
|
| 5712 | - |
|
| 5713 | - /** |
|
| 5714 | - * Indicating whether or not this model thinks its a wp core model |
|
| 5715 | - * |
|
| 5716 | - * @return boolean |
|
| 5717 | - */ |
|
| 5718 | - public function is_wp_core_model() |
|
| 5719 | - { |
|
| 5720 | - return $this->_wp_core_model; |
|
| 5721 | - } |
|
| 5722 | - |
|
| 5723 | - |
|
| 5724 | - |
|
| 5725 | - /** |
|
| 5726 | - * Gets all the caps that are missing which impose a restriction on |
|
| 5727 | - * queries made in this context |
|
| 5728 | - * |
|
| 5729 | - * @param string $context one of EEM_Base::caps_ constants |
|
| 5730 | - * @return EE_Default_Where_Conditions[] indexed by capability name |
|
| 5731 | - * @throws \EE_Error |
|
| 5732 | - */ |
|
| 5733 | - public function caps_missing($context = EEM_Base::caps_read) |
|
| 5734 | - { |
|
| 5735 | - $missing_caps = array(); |
|
| 5736 | - $cap_restrictions = $this->cap_restrictions($context); |
|
| 5737 | - foreach ($cap_restrictions as $cap => $restriction_if_no_cap) { |
|
| 5738 | - if (! EE_Capabilities::instance() |
|
| 5739 | - ->current_user_can($cap, $this->get_this_model_name() . '_model_applying_caps') |
|
| 5740 | - ) { |
|
| 5741 | - $missing_caps[$cap] = $restriction_if_no_cap; |
|
| 5742 | - } |
|
| 5743 | - } |
|
| 5744 | - return $missing_caps; |
|
| 5745 | - } |
|
| 5746 | - |
|
| 5747 | - |
|
| 5748 | - |
|
| 5749 | - /** |
|
| 5750 | - * Gets the mapping from capability contexts to action strings used in capability names |
|
| 5751 | - * |
|
| 5752 | - * @return array keys are one of EEM_Base::valid_cap_contexts(), and values are usually |
|
| 5753 | - * one of 'read', 'edit', or 'delete' |
|
| 5754 | - */ |
|
| 5755 | - public function cap_contexts_to_cap_action_map() |
|
| 5756 | - { |
|
| 5757 | - return apply_filters('FHEE__EEM_Base__cap_contexts_to_cap_action_map', $this->_cap_contexts_to_cap_action_map, |
|
| 5758 | - $this); |
|
| 5759 | - } |
|
| 5760 | - |
|
| 5761 | - |
|
| 5762 | - |
|
| 5763 | - /** |
|
| 5764 | - * Gets the action string for the specified capability context |
|
| 5765 | - * |
|
| 5766 | - * @param string $context |
|
| 5767 | - * @return string one of EEM_Base::cap_contexts_to_cap_action_map() values |
|
| 5768 | - * @throws \EE_Error |
|
| 5769 | - */ |
|
| 5770 | - public function cap_action_for_context($context) |
|
| 5771 | - { |
|
| 5772 | - $mapping = $this->cap_contexts_to_cap_action_map(); |
|
| 5773 | - if (isset($mapping[$context])) { |
|
| 5774 | - return $mapping[$context]; |
|
| 5775 | - } |
|
| 5776 | - if ($action = apply_filters('FHEE__EEM_Base__cap_action_for_context', null, $this, $mapping, $context)) { |
|
| 5777 | - return $action; |
|
| 5778 | - } |
|
| 5779 | - throw new EE_Error( |
|
| 5780 | - sprintf( |
|
| 5781 | - __('Cannot find capability restrictions for context "%1$s", allowed values are:%2$s', 'event_espresso'), |
|
| 5782 | - $context, |
|
| 5783 | - implode(',', array_keys($this->cap_contexts_to_cap_action_map())) |
|
| 5784 | - ) |
|
| 5785 | - ); |
|
| 5786 | - } |
|
| 5787 | - |
|
| 5788 | - |
|
| 5789 | - |
|
| 5790 | - /** |
|
| 5791 | - * Returns all the capability contexts which are valid when querying models |
|
| 5792 | - * |
|
| 5793 | - * @return array |
|
| 5794 | - */ |
|
| 5795 | - public static function valid_cap_contexts() |
|
| 5796 | - { |
|
| 5797 | - return apply_filters('FHEE__EEM_Base__valid_cap_contexts', array( |
|
| 5798 | - self::caps_read, |
|
| 5799 | - self::caps_read_admin, |
|
| 5800 | - self::caps_edit, |
|
| 5801 | - self::caps_delete, |
|
| 5802 | - )); |
|
| 5803 | - } |
|
| 5804 | - |
|
| 5805 | - |
|
| 5806 | - |
|
| 5807 | - /** |
|
| 5808 | - * Returns all valid options for 'default_where_conditions' |
|
| 5809 | - * |
|
| 5810 | - * @return array |
|
| 5811 | - */ |
|
| 5812 | - public static function valid_default_where_conditions() |
|
| 5813 | - { |
|
| 5814 | - return array( |
|
| 5815 | - EEM_Base::default_where_conditions_all, |
|
| 5816 | - EEM_Base::default_where_conditions_this_only, |
|
| 5817 | - EEM_Base::default_where_conditions_others_only, |
|
| 5818 | - EEM_Base::default_where_conditions_minimum_all, |
|
| 5819 | - EEM_Base::default_where_conditions_minimum_others, |
|
| 5820 | - EEM_Base::default_where_conditions_none |
|
| 5821 | - ); |
|
| 5822 | - } |
|
| 5823 | - |
|
| 5824 | - // public static function default_where_conditions_full |
|
| 5825 | - /** |
|
| 5826 | - * Verifies $context is one of EEM_Base::valid_cap_contexts(), if not it throws an exception |
|
| 5827 | - * |
|
| 5828 | - * @param string $context |
|
| 5829 | - * @return bool |
|
| 5830 | - * @throws \EE_Error |
|
| 5831 | - */ |
|
| 5832 | - static public function verify_is_valid_cap_context($context) |
|
| 5833 | - { |
|
| 5834 | - $valid_cap_contexts = EEM_Base::valid_cap_contexts(); |
|
| 5835 | - if (in_array($context, $valid_cap_contexts)) { |
|
| 5836 | - return true; |
|
| 5837 | - } else { |
|
| 5838 | - throw new EE_Error( |
|
| 5839 | - sprintf( |
|
| 5840 | - __('Context "%1$s" passed into model "%2$s" is not a valid context. They are: %3$s', |
|
| 5841 | - 'event_espresso'), |
|
| 5842 | - $context, |
|
| 5843 | - 'EEM_Base', |
|
| 5844 | - implode(',', $valid_cap_contexts) |
|
| 5845 | - ) |
|
| 5846 | - ); |
|
| 5847 | - } |
|
| 5848 | - } |
|
| 5849 | - |
|
| 5850 | - |
|
| 5851 | - |
|
| 5852 | - /** |
|
| 5853 | - * Clears all the models field caches. This is only useful when a sub-class |
|
| 5854 | - * might have added a field or something and these caches might be invalidated |
|
| 5855 | - */ |
|
| 5856 | - protected function _invalidate_field_caches() |
|
| 5857 | - { |
|
| 5858 | - $this->_cache_foreign_key_to_fields = array(); |
|
| 5859 | - $this->_cached_fields = null; |
|
| 5860 | - $this->_cached_fields_non_db_only = null; |
|
| 5861 | - } |
|
| 5862 | - |
|
| 5863 | - |
|
| 5864 | - |
|
| 5865 | - /** |
|
| 5866 | - * Gets the list of all the where query param keys that relate to logic instead of field names |
|
| 5867 | - * (eg "and", "or", "not"). |
|
| 5868 | - * |
|
| 5869 | - * @return array |
|
| 5870 | - */ |
|
| 5871 | - public function logic_query_param_keys() |
|
| 5872 | - { |
|
| 5873 | - return $this->_logic_query_param_keys; |
|
| 5874 | - } |
|
| 5875 | - |
|
| 5876 | - |
|
| 5877 | - |
|
| 5878 | - /** |
|
| 5879 | - * Determines whether or not the where query param array key is for a logic query param. |
|
| 5880 | - * Eg 'OR', 'not*', and 'and*because-i-say-so' shoudl all return true, whereas |
|
| 5881 | - * 'ATT_fname', 'EVT_name*not-you-or-me', and 'ORG_name' should return false |
|
| 5882 | - * |
|
| 5883 | - * @param $query_param_key |
|
| 5884 | - * @return bool |
|
| 5885 | - */ |
|
| 5886 | - public function is_logic_query_param_key($query_param_key) |
|
| 5887 | - { |
|
| 5888 | - foreach ($this->logic_query_param_keys() as $logic_query_param_key) { |
|
| 5889 | - if ($query_param_key === $logic_query_param_key |
|
| 5890 | - || strpos($query_param_key, $logic_query_param_key . '*') === 0 |
|
| 5891 | - ) { |
|
| 5892 | - return true; |
|
| 5893 | - } |
|
| 5894 | - } |
|
| 5895 | - return false; |
|
| 5896 | - } |
|
| 3625 | + } |
|
| 3626 | + return $null_friendly_where_conditions; |
|
| 3627 | + } |
|
| 3628 | + |
|
| 3629 | + |
|
| 3630 | + |
|
| 3631 | + /** |
|
| 3632 | + * Uses the _default_where_conditions_strategy set during __construct() to get |
|
| 3633 | + * default where conditions on all get_all, update, and delete queries done by this model. |
|
| 3634 | + * Use the same syntax as client code. Eg on the Event model, use array('Event.EVT_post_type'=>'esp_event'), |
|
| 3635 | + * NOT array('Event_CPT.post_type'=>'esp_event'). |
|
| 3636 | + * |
|
| 3637 | + * @param string $model_relation_path eg, path from Event to Payment is "Registration.Transaction.Payment." |
|
| 3638 | + * @return array like EEM_Base::get_all's $query_params[0] (where conditions) |
|
| 3639 | + */ |
|
| 3640 | + private function _get_default_where_conditions($model_relation_path = null) |
|
| 3641 | + { |
|
| 3642 | + if ($this->_ignore_where_strategy) { |
|
| 3643 | + return array(); |
|
| 3644 | + } |
|
| 3645 | + return $this->_default_where_conditions_strategy->get_default_where_conditions($model_relation_path); |
|
| 3646 | + } |
|
| 3647 | + |
|
| 3648 | + |
|
| 3649 | + |
|
| 3650 | + /** |
|
| 3651 | + * Uses the _minimum_where_conditions_strategy set during __construct() to get |
|
| 3652 | + * minimum where conditions on all get_all, update, and delete queries done by this model. |
|
| 3653 | + * Use the same syntax as client code. Eg on the Event model, use array('Event.EVT_post_type'=>'esp_event'), |
|
| 3654 | + * NOT array('Event_CPT.post_type'=>'esp_event'). |
|
| 3655 | + * Similar to _get_default_where_conditions |
|
| 3656 | + * |
|
| 3657 | + * @param string $model_relation_path eg, path from Event to Payment is "Registration.Transaction.Payment." |
|
| 3658 | + * @return array like EEM_Base::get_all's $query_params[0] (where conditions) |
|
| 3659 | + */ |
|
| 3660 | + protected function _get_minimum_where_conditions($model_relation_path = null) |
|
| 3661 | + { |
|
| 3662 | + if ($this->_ignore_where_strategy) { |
|
| 3663 | + return array(); |
|
| 3664 | + } |
|
| 3665 | + return $this->_minimum_where_conditions_strategy->get_default_where_conditions($model_relation_path); |
|
| 3666 | + } |
|
| 3667 | + |
|
| 3668 | + |
|
| 3669 | + |
|
| 3670 | + /** |
|
| 3671 | + * Creates the string of SQL for the select part of a select query, everything behind SELECT and before FROM. |
|
| 3672 | + * Eg, "Event.post_id, Event.post_name,Event_Detail.EVT_ID..." |
|
| 3673 | + * |
|
| 3674 | + * @param EE_Model_Query_Info_Carrier $model_query_info |
|
| 3675 | + * @return string |
|
| 3676 | + * @throws \EE_Error |
|
| 3677 | + */ |
|
| 3678 | + private function _construct_default_select_sql(EE_Model_Query_Info_Carrier $model_query_info) |
|
| 3679 | + { |
|
| 3680 | + $selects = $this->_get_columns_to_select_for_this_model(); |
|
| 3681 | + foreach ( |
|
| 3682 | + $model_query_info->get_model_names_included() as $model_relation_chain => |
|
| 3683 | + $name_of_other_model_included |
|
| 3684 | + ) { |
|
| 3685 | + $other_model_included = $this->get_related_model_obj($name_of_other_model_included); |
|
| 3686 | + $other_model_selects = $other_model_included->_get_columns_to_select_for_this_model($model_relation_chain); |
|
| 3687 | + foreach ($other_model_selects as $key => $value) { |
|
| 3688 | + $selects[] = $value; |
|
| 3689 | + } |
|
| 3690 | + } |
|
| 3691 | + return implode(", ", $selects); |
|
| 3692 | + } |
|
| 3693 | + |
|
| 3694 | + |
|
| 3695 | + |
|
| 3696 | + /** |
|
| 3697 | + * Gets an array of columns to select for this model, which are necessary for it to create its objects. |
|
| 3698 | + * So that's going to be the columns for all the fields on the model |
|
| 3699 | + * |
|
| 3700 | + * @param string $model_relation_chain like 'Question.Question_Group.Event' |
|
| 3701 | + * @return array numerically indexed, values are columns to select and rename, eg "Event.ID AS 'Event.ID'" |
|
| 3702 | + */ |
|
| 3703 | + public function _get_columns_to_select_for_this_model($model_relation_chain = '') |
|
| 3704 | + { |
|
| 3705 | + $fields = $this->field_settings(); |
|
| 3706 | + $selects = array(); |
|
| 3707 | + $table_alias_with_model_relation_chain_prefix = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
| 3708 | + $this->get_this_model_name()); |
|
| 3709 | + foreach ($fields as $field_obj) { |
|
| 3710 | + $selects[] = $table_alias_with_model_relation_chain_prefix |
|
| 3711 | + . $field_obj->get_table_alias() |
|
| 3712 | + . "." |
|
| 3713 | + . $field_obj->get_table_column() |
|
| 3714 | + . " AS '" |
|
| 3715 | + . $table_alias_with_model_relation_chain_prefix |
|
| 3716 | + . $field_obj->get_table_alias() |
|
| 3717 | + . "." |
|
| 3718 | + . $field_obj->get_table_column() |
|
| 3719 | + . "'"; |
|
| 3720 | + } |
|
| 3721 | + //make sure we are also getting the PKs of each table |
|
| 3722 | + $tables = $this->get_tables(); |
|
| 3723 | + if (count($tables) > 1) { |
|
| 3724 | + foreach ($tables as $table_obj) { |
|
| 3725 | + $qualified_pk_column = $table_alias_with_model_relation_chain_prefix |
|
| 3726 | + . $table_obj->get_fully_qualified_pk_column(); |
|
| 3727 | + if (! in_array($qualified_pk_column, $selects)) { |
|
| 3728 | + $selects[] = "$qualified_pk_column AS '$qualified_pk_column'"; |
|
| 3729 | + } |
|
| 3730 | + } |
|
| 3731 | + } |
|
| 3732 | + return $selects; |
|
| 3733 | + } |
|
| 3734 | + |
|
| 3735 | + |
|
| 3736 | + |
|
| 3737 | + /** |
|
| 3738 | + * Given a $query_param like 'Registration.Transaction.TXN_ID', pops off 'Registration.', |
|
| 3739 | + * gets the join statement for it; gets the data types for it; and passes the remaining 'Transaction.TXN_ID' |
|
| 3740 | + * onto its related Transaction object to do the same. Returns an EE_Join_And_Data_Types object which contains the |
|
| 3741 | + * SQL for joining, and the data types |
|
| 3742 | + * |
|
| 3743 | + * @param null|string $original_query_param |
|
| 3744 | + * @param string $query_param like Registration.Transaction.TXN_ID |
|
| 3745 | + * @param EE_Model_Query_Info_Carrier $passed_in_query_info |
|
| 3746 | + * @param string $query_param_type like Registration.Transaction.TXN_ID |
|
| 3747 | + * or 'PAY_ID'. Otherwise, we don't expect there to be a |
|
| 3748 | + * column name. We only want model names, eg 'Event.Venue' |
|
| 3749 | + * or 'Registration's |
|
| 3750 | + * @param string $original_query_param what it originally was (eg |
|
| 3751 | + * Registration.Transaction.TXN_ID). If null, we assume it |
|
| 3752 | + * matches $query_param |
|
| 3753 | + * @throws EE_Error |
|
| 3754 | + * @return void only modifies the EEM_Related_Model_Info_Carrier passed into it |
|
| 3755 | + */ |
|
| 3756 | + private function _extract_related_model_info_from_query_param( |
|
| 3757 | + $query_param, |
|
| 3758 | + EE_Model_Query_Info_Carrier $passed_in_query_info, |
|
| 3759 | + $query_param_type, |
|
| 3760 | + $original_query_param = null |
|
| 3761 | + ) { |
|
| 3762 | + if ($original_query_param === null) { |
|
| 3763 | + $original_query_param = $query_param; |
|
| 3764 | + } |
|
| 3765 | + $query_param = $this->_remove_stars_and_anything_after_from_condition_query_param_key($query_param); |
|
| 3766 | + /** @var $allow_logic_query_params bool whether or not to allow logic_query_params like 'NOT','OR', or 'AND' */ |
|
| 3767 | + $allow_logic_query_params = in_array($query_param_type, array('where', 'having')); |
|
| 3768 | + $allow_fields = in_array($query_param_type, array('where', 'having', 'order_by', 'group_by', 'order')); |
|
| 3769 | + //check to see if we have a field on this model |
|
| 3770 | + $this_model_fields = $this->field_settings(true); |
|
| 3771 | + if (array_key_exists($query_param, $this_model_fields)) { |
|
| 3772 | + if ($allow_fields) { |
|
| 3773 | + return; |
|
| 3774 | + } else { |
|
| 3775 | + throw new EE_Error(sprintf(__("Using a field name (%s) on model %s is not allowed on this query param type '%s'. Original query param was %s", |
|
| 3776 | + "event_espresso"), |
|
| 3777 | + $query_param, get_class($this), $query_param_type, $original_query_param)); |
|
| 3778 | + } |
|
| 3779 | + } //check if this is a special logic query param |
|
| 3780 | + elseif (in_array($query_param, $this->_logic_query_param_keys, true)) { |
|
| 3781 | + if ($allow_logic_query_params) { |
|
| 3782 | + return; |
|
| 3783 | + } else { |
|
| 3784 | + throw new EE_Error( |
|
| 3785 | + sprintf( |
|
| 3786 | + __('Logic query params ("%1$s") are being used incorrectly with the following query param ("%2$s") on model %3$s. %4$sAdditional Info:%4$s%5$s', |
|
| 3787 | + 'event_espresso'), |
|
| 3788 | + implode('", "', $this->_logic_query_param_keys), |
|
| 3789 | + $query_param, |
|
| 3790 | + get_class($this), |
|
| 3791 | + '<br />', |
|
| 3792 | + "\t" |
|
| 3793 | + . ' $passed_in_query_info = <pre>' |
|
| 3794 | + . print_r($passed_in_query_info, true) |
|
| 3795 | + . '</pre>' |
|
| 3796 | + . "\n\t" |
|
| 3797 | + . ' $query_param_type = ' |
|
| 3798 | + . $query_param_type |
|
| 3799 | + . "\n\t" |
|
| 3800 | + . ' $original_query_param = ' |
|
| 3801 | + . $original_query_param |
|
| 3802 | + ) |
|
| 3803 | + ); |
|
| 3804 | + } |
|
| 3805 | + } //check if it's a custom selection |
|
| 3806 | + elseif (array_key_exists($query_param, $this->_custom_selections)) { |
|
| 3807 | + return; |
|
| 3808 | + } |
|
| 3809 | + //check if has a model name at the beginning |
|
| 3810 | + //and |
|
| 3811 | + //check if it's a field on a related model |
|
| 3812 | + foreach ($this->_model_relations as $valid_related_model_name => $relation_obj) { |
|
| 3813 | + if (strpos($query_param, $valid_related_model_name . ".") === 0) { |
|
| 3814 | + $this->_add_join_to_model($valid_related_model_name, $passed_in_query_info, $original_query_param); |
|
| 3815 | + $query_param = substr($query_param, strlen($valid_related_model_name . ".")); |
|
| 3816 | + if ($query_param === '') { |
|
| 3817 | + //nothing left to $query_param |
|
| 3818 | + //we should actually end in a field name, not a model like this! |
|
| 3819 | + throw new EE_Error(sprintf(__("Query param '%s' (of type %s on model %s) shouldn't end on a period (.) ", |
|
| 3820 | + "event_espresso"), |
|
| 3821 | + $query_param, $query_param_type, get_class($this), $valid_related_model_name)); |
|
| 3822 | + } else { |
|
| 3823 | + $related_model_obj = $this->get_related_model_obj($valid_related_model_name); |
|
| 3824 | + $related_model_obj->_extract_related_model_info_from_query_param($query_param, |
|
| 3825 | + $passed_in_query_info, $query_param_type, $original_query_param); |
|
| 3826 | + return; |
|
| 3827 | + } |
|
| 3828 | + } elseif ($query_param === $valid_related_model_name) { |
|
| 3829 | + $this->_add_join_to_model($valid_related_model_name, $passed_in_query_info, $original_query_param); |
|
| 3830 | + return; |
|
| 3831 | + } |
|
| 3832 | + } |
|
| 3833 | + //ok so $query_param didn't start with a model name |
|
| 3834 | + //and we previously confirmed it wasn't a logic query param or field on the current model |
|
| 3835 | + //it's wack, that's what it is |
|
| 3836 | + throw new EE_Error(sprintf(__("There is no model named '%s' related to %s. Query param type is %s and original query param is %s", |
|
| 3837 | + "event_espresso"), |
|
| 3838 | + $query_param, get_class($this), $query_param_type, $original_query_param)); |
|
| 3839 | + } |
|
| 3840 | + |
|
| 3841 | + |
|
| 3842 | + |
|
| 3843 | + /** |
|
| 3844 | + * Privately used by _extract_related_model_info_from_query_param to add a join to $model_name |
|
| 3845 | + * and store it on $passed_in_query_info |
|
| 3846 | + * |
|
| 3847 | + * @param string $model_name |
|
| 3848 | + * @param EE_Model_Query_Info_Carrier $passed_in_query_info |
|
| 3849 | + * @param string $original_query_param used to extract the relation chain between the queried |
|
| 3850 | + * model and $model_name. Eg, if we are querying Event, |
|
| 3851 | + * and are adding a join to 'Payment' with the original |
|
| 3852 | + * query param key |
|
| 3853 | + * 'Registration.Transaction.Payment.PAY_amount', we want |
|
| 3854 | + * to extract 'Registration.Transaction.Payment', in case |
|
| 3855 | + * Payment wants to add default query params so that it |
|
| 3856 | + * will know what models to prepend onto its default query |
|
| 3857 | + * params or in case it wants to rename tables (in case |
|
| 3858 | + * there are multiple joins to the same table) |
|
| 3859 | + * @return void |
|
| 3860 | + * @throws \EE_Error |
|
| 3861 | + */ |
|
| 3862 | + private function _add_join_to_model( |
|
| 3863 | + $model_name, |
|
| 3864 | + EE_Model_Query_Info_Carrier $passed_in_query_info, |
|
| 3865 | + $original_query_param |
|
| 3866 | + ) { |
|
| 3867 | + $relation_obj = $this->related_settings_for($model_name); |
|
| 3868 | + $model_relation_chain = EE_Model_Parser::extract_model_relation_chain($model_name, $original_query_param); |
|
| 3869 | + //check if the relation is HABTM, because then we're essentially doing two joins |
|
| 3870 | + //If so, join first to the JOIN table, and add its data types, and then continue as normal |
|
| 3871 | + if ($relation_obj instanceof EE_HABTM_Relation) { |
|
| 3872 | + $join_model_obj = $relation_obj->get_join_model(); |
|
| 3873 | + //replace the model specified with the join model for this relation chain, whi |
|
| 3874 | + $relation_chain_to_join_model = EE_Model_Parser::replace_model_name_with_join_model_name_in_model_relation_chain($model_name, |
|
| 3875 | + $join_model_obj->get_this_model_name(), $model_relation_chain); |
|
| 3876 | + $new_query_info = new EE_Model_Query_Info_Carrier( |
|
| 3877 | + array($relation_chain_to_join_model => $join_model_obj->get_this_model_name()), |
|
| 3878 | + $relation_obj->get_join_to_intermediate_model_statement($relation_chain_to_join_model)); |
|
| 3879 | + $passed_in_query_info->merge($new_query_info); |
|
| 3880 | + } |
|
| 3881 | + //now just join to the other table pointed to by the relation object, and add its data types |
|
| 3882 | + $new_query_info = new EE_Model_Query_Info_Carrier( |
|
| 3883 | + array($model_relation_chain => $model_name), |
|
| 3884 | + $relation_obj->get_join_statement($model_relation_chain)); |
|
| 3885 | + $passed_in_query_info->merge($new_query_info); |
|
| 3886 | + } |
|
| 3887 | + |
|
| 3888 | + |
|
| 3889 | + |
|
| 3890 | + /** |
|
| 3891 | + * Constructs SQL for where clause, like "WHERE Event.ID = 23 AND Transaction.amount > 100" etc. |
|
| 3892 | + * |
|
| 3893 | + * @param array $where_params like EEM_Base::get_all |
|
| 3894 | + * @return string of SQL |
|
| 3895 | + * @throws \EE_Error |
|
| 3896 | + */ |
|
| 3897 | + private function _construct_where_clause($where_params) |
|
| 3898 | + { |
|
| 3899 | + $SQL = $this->_construct_condition_clause_recursive($where_params, ' AND '); |
|
| 3900 | + if ($SQL) { |
|
| 3901 | + return " WHERE " . $SQL; |
|
| 3902 | + } else { |
|
| 3903 | + return ''; |
|
| 3904 | + } |
|
| 3905 | + } |
|
| 3906 | + |
|
| 3907 | + |
|
| 3908 | + |
|
| 3909 | + /** |
|
| 3910 | + * Just like the _construct_where_clause, except prepends 'HAVING' instead of 'WHERE', |
|
| 3911 | + * and should be passed HAVING parameters, not WHERE parameters |
|
| 3912 | + * |
|
| 3913 | + * @param array $having_params |
|
| 3914 | + * @return string |
|
| 3915 | + * @throws \EE_Error |
|
| 3916 | + */ |
|
| 3917 | + private function _construct_having_clause($having_params) |
|
| 3918 | + { |
|
| 3919 | + $SQL = $this->_construct_condition_clause_recursive($having_params, ' AND '); |
|
| 3920 | + if ($SQL) { |
|
| 3921 | + return " HAVING " . $SQL; |
|
| 3922 | + } else { |
|
| 3923 | + return ''; |
|
| 3924 | + } |
|
| 3925 | + } |
|
| 3926 | + |
|
| 3927 | + |
|
| 3928 | + /** |
|
| 3929 | + * Used for creating nested WHERE conditions. Eg "WHERE ! (Event.ID = 3 OR ( Event_Meta.meta_key = 'bob' AND |
|
| 3930 | + * Event_Meta.meta_value = 'foo'))" |
|
| 3931 | + * |
|
| 3932 | + * @param array $where_params see EEM_Base::get_all for documentation |
|
| 3933 | + * @param string $glue joins each subclause together. Should really only be " AND " or " OR "... |
|
| 3934 | + * @throws EE_Error |
|
| 3935 | + * @return string of SQL |
|
| 3936 | + */ |
|
| 3937 | + private function _construct_condition_clause_recursive($where_params, $glue = ' AND') |
|
| 3938 | + { |
|
| 3939 | + $where_clauses = array(); |
|
| 3940 | + foreach ($where_params as $query_param => $op_and_value_or_sub_condition) { |
|
| 3941 | + $query_param = $this->_remove_stars_and_anything_after_from_condition_query_param_key($query_param);//str_replace("*",'',$query_param); |
|
| 3942 | + if (in_array($query_param, $this->_logic_query_param_keys)) { |
|
| 3943 | + switch ($query_param) { |
|
| 3944 | + case 'not': |
|
| 3945 | + case 'NOT': |
|
| 3946 | + $where_clauses[] = "! (" |
|
| 3947 | + . $this->_construct_condition_clause_recursive($op_and_value_or_sub_condition, |
|
| 3948 | + $glue) |
|
| 3949 | + . ")"; |
|
| 3950 | + break; |
|
| 3951 | + case 'and': |
|
| 3952 | + case 'AND': |
|
| 3953 | + $where_clauses[] = " (" |
|
| 3954 | + . $this->_construct_condition_clause_recursive($op_and_value_or_sub_condition, |
|
| 3955 | + ' AND ') |
|
| 3956 | + . ")"; |
|
| 3957 | + break; |
|
| 3958 | + case 'or': |
|
| 3959 | + case 'OR': |
|
| 3960 | + $where_clauses[] = " (" |
|
| 3961 | + . $this->_construct_condition_clause_recursive($op_and_value_or_sub_condition, |
|
| 3962 | + ' OR ') |
|
| 3963 | + . ")"; |
|
| 3964 | + break; |
|
| 3965 | + } |
|
| 3966 | + } else { |
|
| 3967 | + $field_obj = $this->_deduce_field_from_query_param($query_param); |
|
| 3968 | + //if it's not a normal field, maybe it's a custom selection? |
|
| 3969 | + if (! $field_obj) { |
|
| 3970 | + if (isset($this->_custom_selections[$query_param][1])) { |
|
| 3971 | + $field_obj = $this->_custom_selections[$query_param][1]; |
|
| 3972 | + } else { |
|
| 3973 | + throw new EE_Error(sprintf(__("%s is neither a valid model field name, nor a custom selection", |
|
| 3974 | + "event_espresso"), $query_param)); |
|
| 3975 | + } |
|
| 3976 | + } |
|
| 3977 | + $op_and_value_sql = $this->_construct_op_and_value($op_and_value_or_sub_condition, $field_obj); |
|
| 3978 | + $where_clauses[] = $this->_deduce_column_name_from_query_param($query_param) . SP . $op_and_value_sql; |
|
| 3979 | + } |
|
| 3980 | + } |
|
| 3981 | + return $where_clauses ? implode($glue, $where_clauses) : ''; |
|
| 3982 | + } |
|
| 3983 | + |
|
| 3984 | + |
|
| 3985 | + |
|
| 3986 | + /** |
|
| 3987 | + * Takes the input parameter and extract the table name (alias) and column name |
|
| 3988 | + * |
|
| 3989 | + * @param array $query_param like Registration.Transaction.TXN_ID, Event.Datetime.start_time, or REG_ID |
|
| 3990 | + * @throws EE_Error |
|
| 3991 | + * @return string table alias and column name for SQL, eg "Transaction.TXN_ID" |
|
| 3992 | + */ |
|
| 3993 | + private function _deduce_column_name_from_query_param($query_param) |
|
| 3994 | + { |
|
| 3995 | + $field = $this->_deduce_field_from_query_param($query_param); |
|
| 3996 | + if ($field) { |
|
| 3997 | + $table_alias_prefix = EE_Model_Parser::extract_table_alias_model_relation_chain_from_query_param($field->get_model_name(), |
|
| 3998 | + $query_param); |
|
| 3999 | + return $table_alias_prefix . $field->get_qualified_column(); |
|
| 4000 | + } elseif (array_key_exists($query_param, $this->_custom_selections)) { |
|
| 4001 | + //maybe it's custom selection item? |
|
| 4002 | + //if so, just use it as the "column name" |
|
| 4003 | + return $query_param; |
|
| 4004 | + } else { |
|
| 4005 | + throw new EE_Error(sprintf(__("%s is not a valid field on this model, nor a custom selection (%s)", |
|
| 4006 | + "event_espresso"), $query_param, implode(",", $this->_custom_selections))); |
|
| 4007 | + } |
|
| 4008 | + } |
|
| 4009 | + |
|
| 4010 | + |
|
| 4011 | + |
|
| 4012 | + /** |
|
| 4013 | + * Removes the * and anything after it from the condition query param key. It is useful to add the * to condition |
|
| 4014 | + * query param keys (eg, 'OR*', 'EVT_ID') in order for the array keys to still be unique, so that they don't get |
|
| 4015 | + * overwritten Takes a string like 'Event.EVT_ID*', 'TXN_total**', 'OR*1st', and 'DTT_reg_start*foobar' to |
|
| 4016 | + * 'Event.EVT_ID', 'TXN_total', 'OR', and 'DTT_reg_start', respectively. |
|
| 4017 | + * |
|
| 4018 | + * @param string $condition_query_param_key |
|
| 4019 | + * @return string |
|
| 4020 | + */ |
|
| 4021 | + private function _remove_stars_and_anything_after_from_condition_query_param_key($condition_query_param_key) |
|
| 4022 | + { |
|
| 4023 | + $pos_of_star = strpos($condition_query_param_key, '*'); |
|
| 4024 | + if ($pos_of_star === false) { |
|
| 4025 | + return $condition_query_param_key; |
|
| 4026 | + } else { |
|
| 4027 | + $condition_query_param_sans_star = substr($condition_query_param_key, 0, $pos_of_star); |
|
| 4028 | + return $condition_query_param_sans_star; |
|
| 4029 | + } |
|
| 4030 | + } |
|
| 4031 | + |
|
| 4032 | + |
|
| 4033 | + |
|
| 4034 | + /** |
|
| 4035 | + * creates the SQL for the operator and the value in a WHERE clause, eg "< 23" or "LIKE '%monkey%'" |
|
| 4036 | + * |
|
| 4037 | + * @param mixed array | string $op_and_value |
|
| 4038 | + * @param EE_Model_Field_Base|string $field_obj . If string, should be one of EEM_Base::_valid_wpdb_data_types |
|
| 4039 | + * @throws EE_Error |
|
| 4040 | + * @return string |
|
| 4041 | + */ |
|
| 4042 | + private function _construct_op_and_value($op_and_value, $field_obj) |
|
| 4043 | + { |
|
| 4044 | + if (is_array($op_and_value)) { |
|
| 4045 | + $operator = isset($op_and_value[0]) ? $this->_prepare_operator_for_sql($op_and_value[0]) : null; |
|
| 4046 | + if (! $operator) { |
|
| 4047 | + $php_array_like_string = array(); |
|
| 4048 | + foreach ($op_and_value as $key => $value) { |
|
| 4049 | + $php_array_like_string[] = "$key=>$value"; |
|
| 4050 | + } |
|
| 4051 | + throw new EE_Error( |
|
| 4052 | + sprintf( |
|
| 4053 | + __( |
|
| 4054 | + "You setup a query parameter like you were going to specify an operator, but didn't. You provided '(%s)', but the operator should be at array key index 0 (eg array('>',32))", |
|
| 4055 | + "event_espresso" |
|
| 4056 | + ), |
|
| 4057 | + implode(",", $php_array_like_string) |
|
| 4058 | + ) |
|
| 4059 | + ); |
|
| 4060 | + } |
|
| 4061 | + $value = isset($op_and_value[1]) ? $op_and_value[1] : null; |
|
| 4062 | + } else { |
|
| 4063 | + $operator = '='; |
|
| 4064 | + $value = $op_and_value; |
|
| 4065 | + } |
|
| 4066 | + //check to see if the value is actually another field |
|
| 4067 | + if (is_array($op_and_value) && isset($op_and_value[2]) && $op_and_value[2] == true) { |
|
| 4068 | + return $operator . SP . $this->_deduce_column_name_from_query_param($value); |
|
| 4069 | + } elseif (in_array($operator, $this->valid_in_style_operators()) && is_array($value)) { |
|
| 4070 | + //in this case, the value should be an array, or at least a comma-separated list |
|
| 4071 | + //it will need to handle a little differently |
|
| 4072 | + $cleaned_value = $this->_construct_in_value($value, $field_obj); |
|
| 4073 | + //note: $cleaned_value has already been run through $wpdb->prepare() |
|
| 4074 | + return $operator . SP . $cleaned_value; |
|
| 4075 | + } elseif (in_array($operator, $this->valid_between_style_operators()) && is_array($value)) { |
|
| 4076 | + //the value should be an array with count of two. |
|
| 4077 | + if (count($value) !== 2) { |
|
| 4078 | + throw new EE_Error( |
|
| 4079 | + sprintf( |
|
| 4080 | + __( |
|
| 4081 | + "The '%s' operator must be used with an array of values and there must be exactly TWO values in that array.", |
|
| 4082 | + 'event_espresso' |
|
| 4083 | + ), |
|
| 4084 | + "BETWEEN" |
|
| 4085 | + ) |
|
| 4086 | + ); |
|
| 4087 | + } |
|
| 4088 | + $cleaned_value = $this->_construct_between_value($value, $field_obj); |
|
| 4089 | + return $operator . SP . $cleaned_value; |
|
| 4090 | + } elseif (in_array($operator, $this->valid_null_style_operators())) { |
|
| 4091 | + if ($value !== null) { |
|
| 4092 | + throw new EE_Error( |
|
| 4093 | + sprintf( |
|
| 4094 | + __( |
|
| 4095 | + "You attempted to give a value (%s) while using a NULL-style operator (%s). That isn't valid", |
|
| 4096 | + "event_espresso" |
|
| 4097 | + ), |
|
| 4098 | + $value, |
|
| 4099 | + $operator |
|
| 4100 | + ) |
|
| 4101 | + ); |
|
| 4102 | + } |
|
| 4103 | + return $operator; |
|
| 4104 | + } elseif (in_array($operator, $this->valid_like_style_operators()) && ! is_array($value)) { |
|
| 4105 | + //if the operator is 'LIKE', we want to allow percent signs (%) and not |
|
| 4106 | + //remove other junk. So just treat it as a string. |
|
| 4107 | + return $operator . SP . $this->_wpdb_prepare_using_field($value, '%s'); |
|
| 4108 | + } elseif (! in_array($operator, $this->valid_in_style_operators()) && ! is_array($value)) { |
|
| 4109 | + return $operator . SP . $this->_wpdb_prepare_using_field($value, $field_obj); |
|
| 4110 | + } elseif (in_array($operator, $this->valid_in_style_operators()) && ! is_array($value)) { |
|
| 4111 | + throw new EE_Error( |
|
| 4112 | + sprintf( |
|
| 4113 | + __( |
|
| 4114 | + "Operator '%s' must be used with an array of values, eg 'Registration.REG_ID' => array('%s',array(1,2,3))", |
|
| 4115 | + 'event_espresso' |
|
| 4116 | + ), |
|
| 4117 | + $operator, |
|
| 4118 | + $operator |
|
| 4119 | + ) |
|
| 4120 | + ); |
|
| 4121 | + } elseif (! in_array($operator, $this->valid_in_style_operators()) && is_array($value)) { |
|
| 4122 | + throw new EE_Error( |
|
| 4123 | + sprintf( |
|
| 4124 | + __( |
|
| 4125 | + "Operator '%s' must be used with a single value, not an array. Eg 'Registration.REG_ID => array('%s',23))", |
|
| 4126 | + 'event_espresso' |
|
| 4127 | + ), |
|
| 4128 | + $operator, |
|
| 4129 | + $operator |
|
| 4130 | + ) |
|
| 4131 | + ); |
|
| 4132 | + } else { |
|
| 4133 | + throw new EE_Error( |
|
| 4134 | + sprintf( |
|
| 4135 | + __( |
|
| 4136 | + "It appears you've provided some totally invalid query parameters. Operator and value were:'%s', which isn't right at all", |
|
| 4137 | + "event_espresso" |
|
| 4138 | + ), |
|
| 4139 | + http_build_query($op_and_value) |
|
| 4140 | + ) |
|
| 4141 | + ); |
|
| 4142 | + } |
|
| 4143 | + } |
|
| 4144 | + |
|
| 4145 | + |
|
| 4146 | + |
|
| 4147 | + /** |
|
| 4148 | + * Creates the operands to be used in a BETWEEN query, eg "'2014-12-31 20:23:33' AND '2015-01-23 12:32:54'" |
|
| 4149 | + * |
|
| 4150 | + * @param array $values |
|
| 4151 | + * @param EE_Model_Field_Base|string $field_obj if string, it should be the datatype to be used when querying, eg |
|
| 4152 | + * '%s' |
|
| 4153 | + * @return string |
|
| 4154 | + * @throws \EE_Error |
|
| 4155 | + */ |
|
| 4156 | + public function _construct_between_value($values, $field_obj) |
|
| 4157 | + { |
|
| 4158 | + $cleaned_values = array(); |
|
| 4159 | + foreach ($values as $value) { |
|
| 4160 | + $cleaned_values[] = $this->_wpdb_prepare_using_field($value, $field_obj); |
|
| 4161 | + } |
|
| 4162 | + return $cleaned_values[0] . " AND " . $cleaned_values[1]; |
|
| 4163 | + } |
|
| 4164 | + |
|
| 4165 | + |
|
| 4166 | + |
|
| 4167 | + /** |
|
| 4168 | + * Takes an array or a comma-separated list of $values and cleans them |
|
| 4169 | + * according to $data_type using $wpdb->prepare, and then makes the list a |
|
| 4170 | + * string surrounded by ( and ). Eg, _construct_in_value(array(1,2,3),'%d') would |
|
| 4171 | + * return '(1,2,3)'; _construct_in_value("1,2,hack",'%d') would return '(1,2,1)' (assuming |
|
| 4172 | + * I'm right that a string, when interpreted as a digit, becomes a 1. It might become a 0) |
|
| 4173 | + * |
|
| 4174 | + * @param mixed $values array or comma-separated string |
|
| 4175 | + * @param EE_Model_Field_Base|string $field_obj if string, it should be a wpdb data type like '%s', or '%d' |
|
| 4176 | + * @return string of SQL to follow an 'IN' or 'NOT IN' operator |
|
| 4177 | + * @throws \EE_Error |
|
| 4178 | + */ |
|
| 4179 | + public function _construct_in_value($values, $field_obj) |
|
| 4180 | + { |
|
| 4181 | + //check if the value is a CSV list |
|
| 4182 | + if (is_string($values)) { |
|
| 4183 | + //in which case, turn it into an array |
|
| 4184 | + $values = explode(",", $values); |
|
| 4185 | + } |
|
| 4186 | + $cleaned_values = array(); |
|
| 4187 | + foreach ($values as $value) { |
|
| 4188 | + $cleaned_values[] = $this->_wpdb_prepare_using_field($value, $field_obj); |
|
| 4189 | + } |
|
| 4190 | + //we would just LOVE to leave $cleaned_values as an empty array, and return the value as "()", |
|
| 4191 | + //but unfortunately that's invalid SQL. So instead we return a string which we KNOW will evaluate to be the empty set |
|
| 4192 | + //which is effectively equivalent to returning "()". We don't return "(0)" because that only works for auto-incrementing columns |
|
| 4193 | + if (empty($cleaned_values)) { |
|
| 4194 | + $all_fields = $this->field_settings(); |
|
| 4195 | + $a_field = array_shift($all_fields); |
|
| 4196 | + $main_table = $this->_get_main_table(); |
|
| 4197 | + $cleaned_values[] = "SELECT " |
|
| 4198 | + . $a_field->get_table_column() |
|
| 4199 | + . " FROM " |
|
| 4200 | + . $main_table->get_table_name() |
|
| 4201 | + . " WHERE FALSE"; |
|
| 4202 | + } |
|
| 4203 | + return "(" . implode(",", $cleaned_values) . ")"; |
|
| 4204 | + } |
|
| 4205 | + |
|
| 4206 | + |
|
| 4207 | + |
|
| 4208 | + /** |
|
| 4209 | + * @param mixed $value |
|
| 4210 | + * @param EE_Model_Field_Base|string $field_obj if string it should be a wpdb data type like '%d' |
|
| 4211 | + * @throws EE_Error |
|
| 4212 | + * @return false|null|string |
|
| 4213 | + */ |
|
| 4214 | + private function _wpdb_prepare_using_field($value, $field_obj) |
|
| 4215 | + { |
|
| 4216 | + /** @type WPDB $wpdb */ |
|
| 4217 | + global $wpdb; |
|
| 4218 | + if ($field_obj instanceof EE_Model_Field_Base) { |
|
| 4219 | + return $wpdb->prepare($field_obj->get_wpdb_data_type(), |
|
| 4220 | + $this->_prepare_value_for_use_in_db($value, $field_obj)); |
|
| 4221 | + } else {//$field_obj should really just be a data type |
|
| 4222 | + if (! in_array($field_obj, $this->_valid_wpdb_data_types)) { |
|
| 4223 | + throw new EE_Error(sprintf(__("%s is not a valid wpdb datatype. Valid ones are %s", "event_espresso"), |
|
| 4224 | + $field_obj, implode(",", $this->_valid_wpdb_data_types))); |
|
| 4225 | + } |
|
| 4226 | + return $wpdb->prepare($field_obj, $value); |
|
| 4227 | + } |
|
| 4228 | + } |
|
| 4229 | + |
|
| 4230 | + |
|
| 4231 | + |
|
| 4232 | + /** |
|
| 4233 | + * Takes the input parameter and finds the model field that it indicates. |
|
| 4234 | + * |
|
| 4235 | + * @param string $query_param_name like Registration.Transaction.TXN_ID, Event.Datetime.start_time, or REG_ID |
|
| 4236 | + * @throws EE_Error |
|
| 4237 | + * @return EE_Model_Field_Base |
|
| 4238 | + */ |
|
| 4239 | + protected function _deduce_field_from_query_param($query_param_name) |
|
| 4240 | + { |
|
| 4241 | + //ok, now proceed with deducing which part is the model's name, and which is the field's name |
|
| 4242 | + //which will help us find the database table and column |
|
| 4243 | + $query_param_parts = explode(".", $query_param_name); |
|
| 4244 | + if (empty($query_param_parts)) { |
|
| 4245 | + throw new EE_Error(sprintf(__("_extract_column_name is empty when trying to extract column and table name from %s", |
|
| 4246 | + 'event_espresso'), $query_param_name)); |
|
| 4247 | + } |
|
| 4248 | + $number_of_parts = count($query_param_parts); |
|
| 4249 | + $last_query_param_part = $query_param_parts[count($query_param_parts) - 1]; |
|
| 4250 | + if ($number_of_parts === 1) { |
|
| 4251 | + $field_name = $last_query_param_part; |
|
| 4252 | + $model_obj = $this; |
|
| 4253 | + } else {// $number_of_parts >= 2 |
|
| 4254 | + //the last part is the column name, and there are only 2parts. therefore... |
|
| 4255 | + $field_name = $last_query_param_part; |
|
| 4256 | + $model_obj = $this->get_related_model_obj($query_param_parts[$number_of_parts - 2]); |
|
| 4257 | + } |
|
| 4258 | + try { |
|
| 4259 | + return $model_obj->field_settings_for($field_name); |
|
| 4260 | + } catch (EE_Error $e) { |
|
| 4261 | + return null; |
|
| 4262 | + } |
|
| 4263 | + } |
|
| 4264 | + |
|
| 4265 | + |
|
| 4266 | + |
|
| 4267 | + /** |
|
| 4268 | + * Given a field's name (ie, a key in $this->field_settings()), uses the EE_Model_Field object to get the table's |
|
| 4269 | + * alias and column which corresponds to it |
|
| 4270 | + * |
|
| 4271 | + * @param string $field_name |
|
| 4272 | + * @throws EE_Error |
|
| 4273 | + * @return string |
|
| 4274 | + */ |
|
| 4275 | + public function _get_qualified_column_for_field($field_name) |
|
| 4276 | + { |
|
| 4277 | + $all_fields = $this->field_settings(); |
|
| 4278 | + $field = isset($all_fields[$field_name]) ? $all_fields[$field_name] : false; |
|
| 4279 | + if ($field) { |
|
| 4280 | + return $field->get_qualified_column(); |
|
| 4281 | + } else { |
|
| 4282 | + throw new EE_Error(sprintf(__("There is no field titled %s on model %s. Either the query trying to use it is bad, or you need to add it to the list of fields on the model.", |
|
| 4283 | + 'event_espresso'), $field_name, get_class($this))); |
|
| 4284 | + } |
|
| 4285 | + } |
|
| 4286 | + |
|
| 4287 | + |
|
| 4288 | + |
|
| 4289 | + /** |
|
| 4290 | + * similar to \EEM_Base::_get_qualified_column_for_field() but returns an array with data for ALL fields. |
|
| 4291 | + * Example usage: |
|
| 4292 | + * EEM_Ticket::instance()->get_all_wpdb_results( |
|
| 4293 | + * array(), |
|
| 4294 | + * ARRAY_A, |
|
| 4295 | + * EEM_Ticket::instance()->get_qualified_columns_for_all_fields() |
|
| 4296 | + * ); |
|
| 4297 | + * is equivalent to |
|
| 4298 | + * EEM_Ticket::instance()->get_all_wpdb_results( array(), ARRAY_A, '*' ); |
|
| 4299 | + * and |
|
| 4300 | + * EEM_Event::instance()->get_all_wpdb_results( |
|
| 4301 | + * array( |
|
| 4302 | + * array( |
|
| 4303 | + * 'Datetime.Ticket.TKT_ID' => array( '<', 100 ), |
|
| 4304 | + * ), |
|
| 4305 | + * ARRAY_A, |
|
| 4306 | + * implode( |
|
| 4307 | + * ', ', |
|
| 4308 | + * array_merge( |
|
| 4309 | + * EEM_Event::instance()->get_qualified_columns_for_all_fields( '', false ), |
|
| 4310 | + * EEM_Ticket::instance()->get_qualified_columns_for_all_fields( 'Datetime', false ) |
|
| 4311 | + * ) |
|
| 4312 | + * ) |
|
| 4313 | + * ) |
|
| 4314 | + * ); |
|
| 4315 | + * selects rows from the database, selecting all the event and ticket columns, where the ticket ID is below 100 |
|
| 4316 | + * |
|
| 4317 | + * @param string $model_relation_chain the chain of models used to join between the model you want to query |
|
| 4318 | + * and the one whose fields you are selecting for example: when querying |
|
| 4319 | + * tickets model and selecting fields from the tickets model you would |
|
| 4320 | + * leave this parameter empty, because no models are needed to join |
|
| 4321 | + * between the queried model and the selected one. Likewise when |
|
| 4322 | + * querying the datetime model and selecting fields from the tickets |
|
| 4323 | + * model, it would also be left empty, because there is a direct |
|
| 4324 | + * relation from datetimes to tickets, so no model is needed to join |
|
| 4325 | + * them together. However, when querying from the event model and |
|
| 4326 | + * selecting fields from the ticket model, you should provide the string |
|
| 4327 | + * 'Datetime', indicating that the event model must first join to the |
|
| 4328 | + * datetime model in order to find its relation to ticket model. |
|
| 4329 | + * Also, when querying from the venue model and selecting fields from |
|
| 4330 | + * the ticket model, you should provide the string 'Event.Datetime', |
|
| 4331 | + * indicating you need to join the venue model to the event model, |
|
| 4332 | + * to the datetime model, in order to find its relation to the ticket model. |
|
| 4333 | + * This string is used to deduce the prefix that gets added onto the |
|
| 4334 | + * models' tables qualified columns |
|
| 4335 | + * @param bool $return_string if true, will return a string with qualified column names separated |
|
| 4336 | + * by ', ' if false, will simply return a numerically indexed array of |
|
| 4337 | + * qualified column names |
|
| 4338 | + * @return array|string |
|
| 4339 | + */ |
|
| 4340 | + public function get_qualified_columns_for_all_fields($model_relation_chain = '', $return_string = true) |
|
| 4341 | + { |
|
| 4342 | + $table_prefix = str_replace('.', '__', $model_relation_chain) . (empty($model_relation_chain) ? '' : '__'); |
|
| 4343 | + $qualified_columns = array(); |
|
| 4344 | + foreach ($this->field_settings() as $field_name => $field) { |
|
| 4345 | + $qualified_columns[] = $table_prefix . $field->get_qualified_column(); |
|
| 4346 | + } |
|
| 4347 | + return $return_string ? implode(', ', $qualified_columns) : $qualified_columns; |
|
| 4348 | + } |
|
| 4349 | + |
|
| 4350 | + |
|
| 4351 | + |
|
| 4352 | + /** |
|
| 4353 | + * constructs the select use on special limit joins |
|
| 4354 | + * NOTE: for now this has only been tested and will work when the table alias is for the PRIMARY table. Although |
|
| 4355 | + * its setup so the select query will be setup on and just doing the special select join off of the primary table |
|
| 4356 | + * (as that is typically where the limits would be set). |
|
| 4357 | + * |
|
| 4358 | + * @param string $table_alias The table the select is being built for |
|
| 4359 | + * @param mixed|string $limit The limit for this select |
|
| 4360 | + * @return string The final select join element for the query. |
|
| 4361 | + */ |
|
| 4362 | + public function _construct_limit_join_select($table_alias, $limit) |
|
| 4363 | + { |
|
| 4364 | + $SQL = ''; |
|
| 4365 | + foreach ($this->_tables as $table_obj) { |
|
| 4366 | + if ($table_obj instanceof EE_Primary_Table) { |
|
| 4367 | + $SQL .= $table_alias === $table_obj->get_table_alias() |
|
| 4368 | + ? $table_obj->get_select_join_limit($limit) |
|
| 4369 | + : SP . $table_obj->get_table_name() . " AS " . $table_obj->get_table_alias() . SP; |
|
| 4370 | + } elseif ($table_obj instanceof EE_Secondary_Table) { |
|
| 4371 | + $SQL .= $table_alias === $table_obj->get_table_alias() |
|
| 4372 | + ? $table_obj->get_select_join_limit_join($limit) |
|
| 4373 | + : SP . $table_obj->get_join_sql($table_alias) . SP; |
|
| 4374 | + } |
|
| 4375 | + } |
|
| 4376 | + return $SQL; |
|
| 4377 | + } |
|
| 4378 | + |
|
| 4379 | + |
|
| 4380 | + |
|
| 4381 | + /** |
|
| 4382 | + * Constructs the internal join if there are multiple tables, or simply the table's name and alias |
|
| 4383 | + * Eg "wp_post AS Event" or "wp_post AS Event INNER JOIN wp_postmeta Event_Meta ON Event.ID = Event_Meta.post_id" |
|
| 4384 | + * |
|
| 4385 | + * @return string SQL |
|
| 4386 | + * @throws \EE_Error |
|
| 4387 | + */ |
|
| 4388 | + public function _construct_internal_join() |
|
| 4389 | + { |
|
| 4390 | + $SQL = $this->_get_main_table()->get_table_sql(); |
|
| 4391 | + $SQL .= $this->_construct_internal_join_to_table_with_alias($this->_get_main_table()->get_table_alias()); |
|
| 4392 | + return $SQL; |
|
| 4393 | + } |
|
| 4394 | + |
|
| 4395 | + |
|
| 4396 | + |
|
| 4397 | + /** |
|
| 4398 | + * Constructs the SQL for joining all the tables on this model. |
|
| 4399 | + * Normally $alias should be the primary table's alias, but in cases where |
|
| 4400 | + * we have already joined to a secondary table (eg, the secondary table has a foreign key and is joined before the |
|
| 4401 | + * primary table) then we should provide that secondary table's alias. Eg, with $alias being the primary table's |
|
| 4402 | + * alias, this will construct SQL like: |
|
| 4403 | + * " INNER JOIN wp_esp_secondary_table AS Secondary_Table ON Primary_Table.pk = Secondary_Table.fk". |
|
| 4404 | + * With $alias being a secondary table's alias, this will construct SQL like: |
|
| 4405 | + * " INNER JOIN wp_esp_primary_table AS Primary_Table ON Primary_Table.pk = Secondary_Table.fk". |
|
| 4406 | + * |
|
| 4407 | + * @param string $alias_prefixed table alias to join to (this table should already be in the FROM SQL clause) |
|
| 4408 | + * @return string |
|
| 4409 | + */ |
|
| 4410 | + public function _construct_internal_join_to_table_with_alias($alias_prefixed) |
|
| 4411 | + { |
|
| 4412 | + $SQL = ''; |
|
| 4413 | + $alias_sans_prefix = EE_Model_Parser::remove_table_alias_model_relation_chain_prefix($alias_prefixed); |
|
| 4414 | + foreach ($this->_tables as $table_obj) { |
|
| 4415 | + if ($table_obj instanceof EE_Secondary_Table) {//table is secondary table |
|
| 4416 | + if ($alias_sans_prefix === $table_obj->get_table_alias()) { |
|
| 4417 | + //so we're joining to this table, meaning the table is already in |
|
| 4418 | + //the FROM statement, BUT the primary table isn't. So we want |
|
| 4419 | + //to add the inverse join sql |
|
| 4420 | + $SQL .= $table_obj->get_inverse_join_sql($alias_prefixed); |
|
| 4421 | + } else { |
|
| 4422 | + //just add a regular JOIN to this table from the primary table |
|
| 4423 | + $SQL .= $table_obj->get_join_sql($alias_prefixed); |
|
| 4424 | + } |
|
| 4425 | + }//if it's a primary table, dont add any SQL. it should already be in the FROM statement |
|
| 4426 | + } |
|
| 4427 | + return $SQL; |
|
| 4428 | + } |
|
| 4429 | + |
|
| 4430 | + |
|
| 4431 | + |
|
| 4432 | + /** |
|
| 4433 | + * Gets an array for storing all the data types on the next-to-be-executed-query. |
|
| 4434 | + * This should be a growing array of keys being table-columns (eg 'EVT_ID' and 'Event.EVT_ID'), and values being |
|
| 4435 | + * their data type (eg, '%s', '%d', etc) |
|
| 4436 | + * |
|
| 4437 | + * @return array |
|
| 4438 | + */ |
|
| 4439 | + public function _get_data_types() |
|
| 4440 | + { |
|
| 4441 | + $data_types = array(); |
|
| 4442 | + foreach ($this->field_settings() as $field_obj) { |
|
| 4443 | + //$data_types[$field_obj->get_table_column()] = $field_obj->get_wpdb_data_type(); |
|
| 4444 | + /** @var $field_obj EE_Model_Field_Base */ |
|
| 4445 | + $data_types[$field_obj->get_qualified_column()] = $field_obj->get_wpdb_data_type(); |
|
| 4446 | + } |
|
| 4447 | + return $data_types; |
|
| 4448 | + } |
|
| 4449 | + |
|
| 4450 | + |
|
| 4451 | + |
|
| 4452 | + /** |
|
| 4453 | + * Gets the model object given the relation's name / model's name (eg, 'Event', 'Registration',etc. Always singular) |
|
| 4454 | + * |
|
| 4455 | + * @param string $model_name |
|
| 4456 | + * @throws EE_Error |
|
| 4457 | + * @return EEM_Base |
|
| 4458 | + */ |
|
| 4459 | + public function get_related_model_obj($model_name) |
|
| 4460 | + { |
|
| 4461 | + $model_classname = "EEM_" . $model_name; |
|
| 4462 | + if (! class_exists($model_classname)) { |
|
| 4463 | + throw new EE_Error(sprintf(__("You specified a related model named %s in your query. No such model exists, if it did, it would have the classname %s", |
|
| 4464 | + 'event_espresso'), $model_name, $model_classname)); |
|
| 4465 | + } |
|
| 4466 | + return call_user_func($model_classname . "::instance"); |
|
| 4467 | + } |
|
| 4468 | + |
|
| 4469 | + |
|
| 4470 | + |
|
| 4471 | + /** |
|
| 4472 | + * Returns the array of EE_ModelRelations for this model. |
|
| 4473 | + * |
|
| 4474 | + * @return EE_Model_Relation_Base[] |
|
| 4475 | + */ |
|
| 4476 | + public function relation_settings() |
|
| 4477 | + { |
|
| 4478 | + return $this->_model_relations; |
|
| 4479 | + } |
|
| 4480 | + |
|
| 4481 | + |
|
| 4482 | + |
|
| 4483 | + /** |
|
| 4484 | + * Gets all related models that this model BELONGS TO. Handy to know sometimes |
|
| 4485 | + * because without THOSE models, this model probably doesn't have much purpose. |
|
| 4486 | + * (Eg, without an event, datetimes have little purpose.) |
|
| 4487 | + * |
|
| 4488 | + * @return EE_Belongs_To_Relation[] |
|
| 4489 | + */ |
|
| 4490 | + public function belongs_to_relations() |
|
| 4491 | + { |
|
| 4492 | + $belongs_to_relations = array(); |
|
| 4493 | + foreach ($this->relation_settings() as $model_name => $relation_obj) { |
|
| 4494 | + if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
| 4495 | + $belongs_to_relations[$model_name] = $relation_obj; |
|
| 4496 | + } |
|
| 4497 | + } |
|
| 4498 | + return $belongs_to_relations; |
|
| 4499 | + } |
|
| 4500 | + |
|
| 4501 | + |
|
| 4502 | + |
|
| 4503 | + /** |
|
| 4504 | + * Returns the specified EE_Model_Relation, or throws an exception |
|
| 4505 | + * |
|
| 4506 | + * @param string $relation_name name of relation, key in $this->_relatedModels |
|
| 4507 | + * @throws EE_Error |
|
| 4508 | + * @return EE_Model_Relation_Base |
|
| 4509 | + */ |
|
| 4510 | + public function related_settings_for($relation_name) |
|
| 4511 | + { |
|
| 4512 | + $relatedModels = $this->relation_settings(); |
|
| 4513 | + if (! array_key_exists($relation_name, $relatedModels)) { |
|
| 4514 | + throw new EE_Error( |
|
| 4515 | + sprintf( |
|
| 4516 | + __('Cannot get %s related to %s. There is no model relation of that type. There is, however, %s...', |
|
| 4517 | + 'event_espresso'), |
|
| 4518 | + $relation_name, |
|
| 4519 | + $this->_get_class_name(), |
|
| 4520 | + implode(', ', array_keys($relatedModels)) |
|
| 4521 | + ) |
|
| 4522 | + ); |
|
| 4523 | + } |
|
| 4524 | + return $relatedModels[$relation_name]; |
|
| 4525 | + } |
|
| 4526 | + |
|
| 4527 | + |
|
| 4528 | + |
|
| 4529 | + /** |
|
| 4530 | + * A convenience method for getting a specific field's settings, instead of getting all field settings for all |
|
| 4531 | + * fields |
|
| 4532 | + * |
|
| 4533 | + * @param string $fieldName |
|
| 4534 | + * @param boolean $include_db_only_fields |
|
| 4535 | + * @throws EE_Error |
|
| 4536 | + * @return EE_Model_Field_Base |
|
| 4537 | + */ |
|
| 4538 | + public function field_settings_for($fieldName, $include_db_only_fields = true) |
|
| 4539 | + { |
|
| 4540 | + $fieldSettings = $this->field_settings($include_db_only_fields); |
|
| 4541 | + if (! array_key_exists($fieldName, $fieldSettings)) { |
|
| 4542 | + throw new EE_Error(sprintf(__("There is no field/column '%s' on '%s'", 'event_espresso'), $fieldName, |
|
| 4543 | + get_class($this))); |
|
| 4544 | + } |
|
| 4545 | + return $fieldSettings[$fieldName]; |
|
| 4546 | + } |
|
| 4547 | + |
|
| 4548 | + |
|
| 4549 | + |
|
| 4550 | + /** |
|
| 4551 | + * Checks if this field exists on this model |
|
| 4552 | + * |
|
| 4553 | + * @param string $fieldName a key in the model's _field_settings array |
|
| 4554 | + * @return boolean |
|
| 4555 | + */ |
|
| 4556 | + public function has_field($fieldName) |
|
| 4557 | + { |
|
| 4558 | + $fieldSettings = $this->field_settings(true); |
|
| 4559 | + if (isset($fieldSettings[$fieldName])) { |
|
| 4560 | + return true; |
|
| 4561 | + } else { |
|
| 4562 | + return false; |
|
| 4563 | + } |
|
| 4564 | + } |
|
| 4565 | + |
|
| 4566 | + |
|
| 4567 | + |
|
| 4568 | + /** |
|
| 4569 | + * Returns whether or not this model has a relation to the specified model |
|
| 4570 | + * |
|
| 4571 | + * @param string $relation_name possibly one of the keys in the relation_settings array |
|
| 4572 | + * @return boolean |
|
| 4573 | + */ |
|
| 4574 | + public function has_relation($relation_name) |
|
| 4575 | + { |
|
| 4576 | + $relations = $this->relation_settings(); |
|
| 4577 | + if (isset($relations[$relation_name])) { |
|
| 4578 | + return true; |
|
| 4579 | + } else { |
|
| 4580 | + return false; |
|
| 4581 | + } |
|
| 4582 | + } |
|
| 4583 | + |
|
| 4584 | + |
|
| 4585 | + |
|
| 4586 | + /** |
|
| 4587 | + * gets the field object of type 'primary_key' from the fieldsSettings attribute. |
|
| 4588 | + * Eg, on EE_Answer that would be ANS_ID field object |
|
| 4589 | + * |
|
| 4590 | + * @param $field_obj |
|
| 4591 | + * @return boolean |
|
| 4592 | + */ |
|
| 4593 | + public function is_primary_key_field($field_obj) |
|
| 4594 | + { |
|
| 4595 | + return $field_obj instanceof EE_Primary_Key_Field_Base ? true : false; |
|
| 4596 | + } |
|
| 4597 | + |
|
| 4598 | + |
|
| 4599 | + |
|
| 4600 | + /** |
|
| 4601 | + * gets the field object of type 'primary_key' from the fieldsSettings attribute. |
|
| 4602 | + * Eg, on EE_Answer that would be ANS_ID field object |
|
| 4603 | + * |
|
| 4604 | + * @return EE_Model_Field_Base |
|
| 4605 | + * @throws EE_Error |
|
| 4606 | + */ |
|
| 4607 | + public function get_primary_key_field() |
|
| 4608 | + { |
|
| 4609 | + if ($this->_primary_key_field === null) { |
|
| 4610 | + foreach ($this->field_settings(true) as $field_obj) { |
|
| 4611 | + if ($this->is_primary_key_field($field_obj)) { |
|
| 4612 | + $this->_primary_key_field = $field_obj; |
|
| 4613 | + break; |
|
| 4614 | + } |
|
| 4615 | + } |
|
| 4616 | + if (! $this->_primary_key_field instanceof EE_Primary_Key_Field_Base) { |
|
| 4617 | + throw new EE_Error(sprintf(__("There is no Primary Key defined on model %s", 'event_espresso'), |
|
| 4618 | + get_class($this))); |
|
| 4619 | + } |
|
| 4620 | + } |
|
| 4621 | + return $this->_primary_key_field; |
|
| 4622 | + } |
|
| 4623 | + |
|
| 4624 | + |
|
| 4625 | + |
|
| 4626 | + /** |
|
| 4627 | + * Returns whether or not not there is a primary key on this model. |
|
| 4628 | + * Internally does some caching. |
|
| 4629 | + * |
|
| 4630 | + * @return boolean |
|
| 4631 | + */ |
|
| 4632 | + public function has_primary_key_field() |
|
| 4633 | + { |
|
| 4634 | + if ($this->_has_primary_key_field === null) { |
|
| 4635 | + try { |
|
| 4636 | + $this->get_primary_key_field(); |
|
| 4637 | + $this->_has_primary_key_field = true; |
|
| 4638 | + } catch (EE_Error $e) { |
|
| 4639 | + $this->_has_primary_key_field = false; |
|
| 4640 | + } |
|
| 4641 | + } |
|
| 4642 | + return $this->_has_primary_key_field; |
|
| 4643 | + } |
|
| 4644 | + |
|
| 4645 | + |
|
| 4646 | + |
|
| 4647 | + /** |
|
| 4648 | + * Finds the first field of type $field_class_name. |
|
| 4649 | + * |
|
| 4650 | + * @param string $field_class_name class name of field that you want to find. Eg, EE_Datetime_Field, |
|
| 4651 | + * EE_Foreign_Key_Field, etc |
|
| 4652 | + * @return EE_Model_Field_Base or null if none is found |
|
| 4653 | + */ |
|
| 4654 | + public function get_a_field_of_type($field_class_name) |
|
| 4655 | + { |
|
| 4656 | + foreach ($this->field_settings() as $field) { |
|
| 4657 | + if ($field instanceof $field_class_name) { |
|
| 4658 | + return $field; |
|
| 4659 | + } |
|
| 4660 | + } |
|
| 4661 | + return null; |
|
| 4662 | + } |
|
| 4663 | + |
|
| 4664 | + |
|
| 4665 | + |
|
| 4666 | + /** |
|
| 4667 | + * Gets a foreign key field pointing to model. |
|
| 4668 | + * |
|
| 4669 | + * @param string $model_name eg Event, Registration, not EEM_Event |
|
| 4670 | + * @return EE_Foreign_Key_Field_Base |
|
| 4671 | + * @throws EE_Error |
|
| 4672 | + */ |
|
| 4673 | + public function get_foreign_key_to($model_name) |
|
| 4674 | + { |
|
| 4675 | + if (! isset($this->_cache_foreign_key_to_fields[$model_name])) { |
|
| 4676 | + foreach ($this->field_settings() as $field) { |
|
| 4677 | + if ( |
|
| 4678 | + $field instanceof EE_Foreign_Key_Field_Base |
|
| 4679 | + && in_array($model_name, $field->get_model_names_pointed_to()) |
|
| 4680 | + ) { |
|
| 4681 | + $this->_cache_foreign_key_to_fields[$model_name] = $field; |
|
| 4682 | + break; |
|
| 4683 | + } |
|
| 4684 | + } |
|
| 4685 | + if (! isset($this->_cache_foreign_key_to_fields[$model_name])) { |
|
| 4686 | + throw new EE_Error(sprintf(__("There is no foreign key field pointing to model %s on model %s", |
|
| 4687 | + 'event_espresso'), $model_name, get_class($this))); |
|
| 4688 | + } |
|
| 4689 | + } |
|
| 4690 | + return $this->_cache_foreign_key_to_fields[$model_name]; |
|
| 4691 | + } |
|
| 4692 | + |
|
| 4693 | + |
|
| 4694 | + |
|
| 4695 | + /** |
|
| 4696 | + * Gets the actual table for the table alias |
|
| 4697 | + * |
|
| 4698 | + * @param string $table_alias eg Event, Event_Meta, Registration, Transaction, but maybe |
|
| 4699 | + * a table alias with a model chain prefix, like 'Venue__Event_Venue___Event_Meta'. |
|
| 4700 | + * Either one works |
|
| 4701 | + * @return EE_Table_Base |
|
| 4702 | + */ |
|
| 4703 | + public function get_table_for_alias($table_alias) |
|
| 4704 | + { |
|
| 4705 | + $table_alias_sans_model_relation_chain_prefix = EE_Model_Parser::remove_table_alias_model_relation_chain_prefix($table_alias); |
|
| 4706 | + return $this->_tables[$table_alias_sans_model_relation_chain_prefix]->get_table_name(); |
|
| 4707 | + } |
|
| 4708 | + |
|
| 4709 | + |
|
| 4710 | + |
|
| 4711 | + /** |
|
| 4712 | + * Returns a flat array of all field son this model, instead of organizing them |
|
| 4713 | + * by table_alias as they are in the constructor. |
|
| 4714 | + * |
|
| 4715 | + * @param bool $include_db_only_fields flag indicating whether or not to include the db-only fields |
|
| 4716 | + * @return EE_Model_Field_Base[] where the keys are the field's name |
|
| 4717 | + */ |
|
| 4718 | + public function field_settings($include_db_only_fields = false) |
|
| 4719 | + { |
|
| 4720 | + if ($include_db_only_fields) { |
|
| 4721 | + if ($this->_cached_fields === null) { |
|
| 4722 | + $this->_cached_fields = array(); |
|
| 4723 | + foreach ($this->_fields as $fields_corresponding_to_table) { |
|
| 4724 | + foreach ($fields_corresponding_to_table as $field_name => $field_obj) { |
|
| 4725 | + $this->_cached_fields[$field_name] = $field_obj; |
|
| 4726 | + } |
|
| 4727 | + } |
|
| 4728 | + } |
|
| 4729 | + return $this->_cached_fields; |
|
| 4730 | + } else { |
|
| 4731 | + if ($this->_cached_fields_non_db_only === null) { |
|
| 4732 | + $this->_cached_fields_non_db_only = array(); |
|
| 4733 | + foreach ($this->_fields as $fields_corresponding_to_table) { |
|
| 4734 | + foreach ($fields_corresponding_to_table as $field_name => $field_obj) { |
|
| 4735 | + /** @var $field_obj EE_Model_Field_Base */ |
|
| 4736 | + if (! $field_obj->is_db_only_field()) { |
|
| 4737 | + $this->_cached_fields_non_db_only[$field_name] = $field_obj; |
|
| 4738 | + } |
|
| 4739 | + } |
|
| 4740 | + } |
|
| 4741 | + } |
|
| 4742 | + return $this->_cached_fields_non_db_only; |
|
| 4743 | + } |
|
| 4744 | + } |
|
| 4745 | + |
|
| 4746 | + |
|
| 4747 | + |
|
| 4748 | + /** |
|
| 4749 | + * cycle though array of attendees and create objects out of each item |
|
| 4750 | + * |
|
| 4751 | + * @access private |
|
| 4752 | + * @param array $rows of results of $wpdb->get_results($query,ARRAY_A) |
|
| 4753 | + * @return \EE_Base_Class[] array keys are primary keys (if there is a primary key on the model. if not, |
|
| 4754 | + * numerically indexed) |
|
| 4755 | + * @throws \EE_Error |
|
| 4756 | + */ |
|
| 4757 | + protected function _create_objects($rows = array()) |
|
| 4758 | + { |
|
| 4759 | + $array_of_objects = array(); |
|
| 4760 | + if (empty($rows)) { |
|
| 4761 | + return array(); |
|
| 4762 | + } |
|
| 4763 | + $count_if_model_has_no_primary_key = 0; |
|
| 4764 | + $has_primary_key = $this->has_primary_key_field(); |
|
| 4765 | + $primary_key_field = $has_primary_key ? $this->get_primary_key_field() : null; |
|
| 4766 | + foreach ((array)$rows as $row) { |
|
| 4767 | + if (empty($row)) { |
|
| 4768 | + //wp did its weird thing where it returns an array like array(0=>null), which is totally not helpful... |
|
| 4769 | + return array(); |
|
| 4770 | + } |
|
| 4771 | + //check if we've already set this object in the results array, |
|
| 4772 | + //in which case there's no need to process it further (again) |
|
| 4773 | + if ($has_primary_key) { |
|
| 4774 | + $table_pk_value = $this->_get_column_value_with_table_alias_or_not( |
|
| 4775 | + $row, |
|
| 4776 | + $primary_key_field->get_qualified_column(), |
|
| 4777 | + $primary_key_field->get_table_column() |
|
| 4778 | + ); |
|
| 4779 | + if ($table_pk_value && isset($array_of_objects[$table_pk_value])) { |
|
| 4780 | + continue; |
|
| 4781 | + } |
|
| 4782 | + } |
|
| 4783 | + $classInstance = $this->instantiate_class_from_array_or_object($row); |
|
| 4784 | + if (! $classInstance) { |
|
| 4785 | + throw new EE_Error( |
|
| 4786 | + sprintf( |
|
| 4787 | + __('Could not create instance of class %s from row %s', 'event_espresso'), |
|
| 4788 | + $this->get_this_model_name(), |
|
| 4789 | + http_build_query($row) |
|
| 4790 | + ) |
|
| 4791 | + ); |
|
| 4792 | + } |
|
| 4793 | + //set the timezone on the instantiated objects |
|
| 4794 | + $classInstance->set_timezone($this->_timezone); |
|
| 4795 | + //make sure if there is any timezone setting present that we set the timezone for the object |
|
| 4796 | + $key = $has_primary_key ? $classInstance->ID() : $count_if_model_has_no_primary_key++; |
|
| 4797 | + $array_of_objects[$key] = $classInstance; |
|
| 4798 | + //also, for all the relations of type BelongsTo, see if we can cache |
|
| 4799 | + //those related models |
|
| 4800 | + //(we could do this for other relations too, but if there are conditions |
|
| 4801 | + //that filtered out some fo the results, then we'd be caching an incomplete set |
|
| 4802 | + //so it requires a little more thought than just caching them immediately...) |
|
| 4803 | + foreach ($this->_model_relations as $modelName => $relation_obj) { |
|
| 4804 | + if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
| 4805 | + //check if this model's INFO is present. If so, cache it on the model |
|
| 4806 | + $other_model = $relation_obj->get_other_model(); |
|
| 4807 | + $other_model_obj_maybe = $other_model->instantiate_class_from_array_or_object($row); |
|
| 4808 | + //if we managed to make a model object from the results, cache it on the main model object |
|
| 4809 | + if ($other_model_obj_maybe) { |
|
| 4810 | + //set timezone on these other model objects if they are present |
|
| 4811 | + $other_model_obj_maybe->set_timezone($this->_timezone); |
|
| 4812 | + $classInstance->cache($modelName, $other_model_obj_maybe); |
|
| 4813 | + } |
|
| 4814 | + } |
|
| 4815 | + } |
|
| 4816 | + } |
|
| 4817 | + return $array_of_objects; |
|
| 4818 | + } |
|
| 4819 | + |
|
| 4820 | + |
|
| 4821 | + |
|
| 4822 | + /** |
|
| 4823 | + * The purpose of this method is to allow us to create a model object that is not in the db that holds default |
|
| 4824 | + * values. A typical example of where this is used is when creating a new item and the initial load of a form. We |
|
| 4825 | + * dont' necessarily want to test for if the object is present but just assume it is BUT load the defaults from the |
|
| 4826 | + * object (as set in the model_field!). |
|
| 4827 | + * |
|
| 4828 | + * @return EE_Base_Class single EE_Base_Class object with default values for the properties. |
|
| 4829 | + */ |
|
| 4830 | + public function create_default_object() |
|
| 4831 | + { |
|
| 4832 | + $this_model_fields_and_values = array(); |
|
| 4833 | + //setup the row using default values; |
|
| 4834 | + foreach ($this->field_settings() as $field_name => $field_obj) { |
|
| 4835 | + $this_model_fields_and_values[$field_name] = $field_obj->get_default_value(); |
|
| 4836 | + } |
|
| 4837 | + $className = $this->_get_class_name(); |
|
| 4838 | + $classInstance = EE_Registry::instance() |
|
| 4839 | + ->load_class($className, array($this_model_fields_and_values), false, false); |
|
| 4840 | + return $classInstance; |
|
| 4841 | + } |
|
| 4842 | + |
|
| 4843 | + |
|
| 4844 | + |
|
| 4845 | + /** |
|
| 4846 | + * @param mixed $cols_n_values either an array of where each key is the name of a field, and the value is its value |
|
| 4847 | + * or an stdClass where each property is the name of a column, |
|
| 4848 | + * @return EE_Base_Class |
|
| 4849 | + * @throws \EE_Error |
|
| 4850 | + */ |
|
| 4851 | + public function instantiate_class_from_array_or_object($cols_n_values) |
|
| 4852 | + { |
|
| 4853 | + if (! is_array($cols_n_values) && is_object($cols_n_values)) { |
|
| 4854 | + $cols_n_values = get_object_vars($cols_n_values); |
|
| 4855 | + } |
|
| 4856 | + $primary_key = null; |
|
| 4857 | + //make sure the array only has keys that are fields/columns on this model |
|
| 4858 | + $this_model_fields_n_values = $this->_deduce_fields_n_values_from_cols_n_values($cols_n_values); |
|
| 4859 | + if ($this->has_primary_key_field() && isset($this_model_fields_n_values[$this->primary_key_name()])) { |
|
| 4860 | + $primary_key = $this_model_fields_n_values[$this->primary_key_name()]; |
|
| 4861 | + } |
|
| 4862 | + $className = $this->_get_class_name(); |
|
| 4863 | + //check we actually found results that we can use to build our model object |
|
| 4864 | + //if not, return null |
|
| 4865 | + if ($this->has_primary_key_field()) { |
|
| 4866 | + if (empty($this_model_fields_n_values[$this->primary_key_name()])) { |
|
| 4867 | + return null; |
|
| 4868 | + } |
|
| 4869 | + } else if ($this->unique_indexes()) { |
|
| 4870 | + $first_column = reset($this_model_fields_n_values); |
|
| 4871 | + if (empty($first_column)) { |
|
| 4872 | + return null; |
|
| 4873 | + } |
|
| 4874 | + } |
|
| 4875 | + // if there is no primary key or the object doesn't already exist in the entity map, then create a new instance |
|
| 4876 | + if ($primary_key) { |
|
| 4877 | + $classInstance = $this->get_from_entity_map($primary_key); |
|
| 4878 | + if (! $classInstance) { |
|
| 4879 | + $classInstance = EE_Registry::instance() |
|
| 4880 | + ->load_class($className, |
|
| 4881 | + array($this_model_fields_n_values, $this->_timezone), true, false); |
|
| 4882 | + // add this new object to the entity map |
|
| 4883 | + $classInstance = $this->add_to_entity_map($classInstance); |
|
| 4884 | + } |
|
| 4885 | + } else { |
|
| 4886 | + $classInstance = EE_Registry::instance() |
|
| 4887 | + ->load_class($className, array($this_model_fields_n_values, $this->_timezone), |
|
| 4888 | + true, false); |
|
| 4889 | + } |
|
| 4890 | + //it is entirely possible that the instantiated class object has a set timezone_string db field and has set it's internal _timezone property accordingly (see new_instance_from_db in model objects particularly EE_Event for example). In this case, we want to make sure the model object doesn't have its timezone string overwritten by any timezone property currently set here on the model so, we intentionally override the model _timezone property with the model_object timezone property. |
|
| 4891 | + $this->set_timezone($classInstance->get_timezone()); |
|
| 4892 | + return $classInstance; |
|
| 4893 | + } |
|
| 4894 | + |
|
| 4895 | + |
|
| 4896 | + |
|
| 4897 | + /** |
|
| 4898 | + * Gets the model object from the entity map if it exists |
|
| 4899 | + * |
|
| 4900 | + * @param int|string $id the ID of the model object |
|
| 4901 | + * @return EE_Base_Class |
|
| 4902 | + */ |
|
| 4903 | + public function get_from_entity_map($id) |
|
| 4904 | + { |
|
| 4905 | + return isset($this->_entity_map[EEM_Base::$_model_query_blog_id][$id]) |
|
| 4906 | + ? $this->_entity_map[EEM_Base::$_model_query_blog_id][$id] : null; |
|
| 4907 | + } |
|
| 4908 | + |
|
| 4909 | + |
|
| 4910 | + |
|
| 4911 | + /** |
|
| 4912 | + * add_to_entity_map |
|
| 4913 | + * Adds the object to the model's entity mappings |
|
| 4914 | + * Effectively tells the models "Hey, this model object is the most up-to-date representation of the data, |
|
| 4915 | + * and for the remainder of the request, it's even more up-to-date than what's in the database. |
|
| 4916 | + * So, if the database doesn't agree with what's in the entity mapper, ignore the database" |
|
| 4917 | + * If the database gets updated directly and you want the entity mapper to reflect that change, |
|
| 4918 | + * then this method should be called immediately after the update query |
|
| 4919 | + * Note: The map is indexed by whatever the current blog id is set (via EEM_Base::$_model_query_blog_id). This is |
|
| 4920 | + * so on multisite, the entity map is specific to the query being done for a specific site. |
|
| 4921 | + * |
|
| 4922 | + * @param EE_Base_Class $object |
|
| 4923 | + * @throws EE_Error |
|
| 4924 | + * @return \EE_Base_Class |
|
| 4925 | + */ |
|
| 4926 | + public function add_to_entity_map(EE_Base_Class $object) |
|
| 4927 | + { |
|
| 4928 | + $className = $this->_get_class_name(); |
|
| 4929 | + if (! $object instanceof $className) { |
|
| 4930 | + throw new EE_Error(sprintf(__("You tried adding a %s to a mapping of %ss", "event_espresso"), |
|
| 4931 | + is_object($object) ? get_class($object) : $object, $className)); |
|
| 4932 | + } |
|
| 4933 | + /** @var $object EE_Base_Class */ |
|
| 4934 | + if (! $object->ID()) { |
|
| 4935 | + throw new EE_Error(sprintf(__("You tried storing a model object with NO ID in the %s entity mapper.", |
|
| 4936 | + "event_espresso"), get_class($this))); |
|
| 4937 | + } |
|
| 4938 | + // double check it's not already there |
|
| 4939 | + $classInstance = $this->get_from_entity_map($object->ID()); |
|
| 4940 | + if ($classInstance) { |
|
| 4941 | + return $classInstance; |
|
| 4942 | + } else { |
|
| 4943 | + $this->_entity_map[EEM_Base::$_model_query_blog_id][$object->ID()] = $object; |
|
| 4944 | + return $object; |
|
| 4945 | + } |
|
| 4946 | + } |
|
| 4947 | + |
|
| 4948 | + |
|
| 4949 | + |
|
| 4950 | + /** |
|
| 4951 | + * if a valid identifier is provided, then that entity is unset from the entity map, |
|
| 4952 | + * if no identifier is provided, then the entire entity map is emptied |
|
| 4953 | + * |
|
| 4954 | + * @param int|string $id the ID of the model object |
|
| 4955 | + * @return boolean |
|
| 4956 | + */ |
|
| 4957 | + public function clear_entity_map($id = null) |
|
| 4958 | + { |
|
| 4959 | + if (empty($id)) { |
|
| 4960 | + $this->_entity_map[EEM_Base::$_model_query_blog_id] = array(); |
|
| 4961 | + return true; |
|
| 4962 | + } |
|
| 4963 | + if (isset($this->_entity_map[EEM_Base::$_model_query_blog_id][$id])) { |
|
| 4964 | + unset($this->_entity_map[EEM_Base::$_model_query_blog_id][$id]); |
|
| 4965 | + return true; |
|
| 4966 | + } |
|
| 4967 | + return false; |
|
| 4968 | + } |
|
| 4969 | + |
|
| 4970 | + |
|
| 4971 | + |
|
| 4972 | + /** |
|
| 4973 | + * Public wrapper for _deduce_fields_n_values_from_cols_n_values. |
|
| 4974 | + * Given an array where keys are column (or column alias) names and values, |
|
| 4975 | + * returns an array of their corresponding field names and database values |
|
| 4976 | + * |
|
| 4977 | + * @param array $cols_n_values |
|
| 4978 | + * @return array |
|
| 4979 | + */ |
|
| 4980 | + public function deduce_fields_n_values_from_cols_n_values($cols_n_values) |
|
| 4981 | + { |
|
| 4982 | + return $this->_deduce_fields_n_values_from_cols_n_values($cols_n_values); |
|
| 4983 | + } |
|
| 4984 | + |
|
| 4985 | + |
|
| 4986 | + |
|
| 4987 | + /** |
|
| 4988 | + * _deduce_fields_n_values_from_cols_n_values |
|
| 4989 | + * Given an array where keys are column (or column alias) names and values, |
|
| 4990 | + * returns an array of their corresponding field names and database values |
|
| 4991 | + * |
|
| 4992 | + * @param string $cols_n_values |
|
| 4993 | + * @return array |
|
| 4994 | + */ |
|
| 4995 | + protected function _deduce_fields_n_values_from_cols_n_values($cols_n_values) |
|
| 4996 | + { |
|
| 4997 | + $this_model_fields_n_values = array(); |
|
| 4998 | + foreach ($this->get_tables() as $table_alias => $table_obj) { |
|
| 4999 | + $table_pk_value = $this->_get_column_value_with_table_alias_or_not($cols_n_values, |
|
| 5000 | + $table_obj->get_fully_qualified_pk_column(), $table_obj->get_pk_column()); |
|
| 5001 | + //there is a primary key on this table and its not set. Use defaults for all its columns |
|
| 5002 | + if ($table_pk_value === null && $table_obj->get_pk_column()) { |
|
| 5003 | + foreach ($this->_get_fields_for_table($table_alias) as $field_name => $field_obj) { |
|
| 5004 | + if (! $field_obj->is_db_only_field()) { |
|
| 5005 | + //prepare field as if its coming from db |
|
| 5006 | + $prepared_value = $field_obj->prepare_for_set($field_obj->get_default_value()); |
|
| 5007 | + $this_model_fields_n_values[$field_name] = $field_obj->prepare_for_use_in_db($prepared_value); |
|
| 5008 | + } |
|
| 5009 | + } |
|
| 5010 | + } else { |
|
| 5011 | + //the table's rows existed. Use their values |
|
| 5012 | + foreach ($this->_get_fields_for_table($table_alias) as $field_name => $field_obj) { |
|
| 5013 | + if (! $field_obj->is_db_only_field()) { |
|
| 5014 | + $this_model_fields_n_values[$field_name] = $this->_get_column_value_with_table_alias_or_not( |
|
| 5015 | + $cols_n_values, $field_obj->get_qualified_column(), |
|
| 5016 | + $field_obj->get_table_column() |
|
| 5017 | + ); |
|
| 5018 | + } |
|
| 5019 | + } |
|
| 5020 | + } |
|
| 5021 | + } |
|
| 5022 | + return $this_model_fields_n_values; |
|
| 5023 | + } |
|
| 5024 | + |
|
| 5025 | + |
|
| 5026 | + |
|
| 5027 | + /** |
|
| 5028 | + * @param $cols_n_values |
|
| 5029 | + * @param $qualified_column |
|
| 5030 | + * @param $regular_column |
|
| 5031 | + * @return null |
|
| 5032 | + */ |
|
| 5033 | + protected function _get_column_value_with_table_alias_or_not($cols_n_values, $qualified_column, $regular_column) |
|
| 5034 | + { |
|
| 5035 | + $value = null; |
|
| 5036 | + //ask the field what it think it's table_name.column_name should be, and call it the "qualified column" |
|
| 5037 | + //does the field on the model relate to this column retrieved from the db? |
|
| 5038 | + //or is it a db-only field? (not relating to the model) |
|
| 5039 | + if (isset($cols_n_values[$qualified_column])) { |
|
| 5040 | + $value = $cols_n_values[$qualified_column]; |
|
| 5041 | + } elseif (isset($cols_n_values[$regular_column])) { |
|
| 5042 | + $value = $cols_n_values[$regular_column]; |
|
| 5043 | + } |
|
| 5044 | + return $value; |
|
| 5045 | + } |
|
| 5046 | + |
|
| 5047 | + |
|
| 5048 | + |
|
| 5049 | + /** |
|
| 5050 | + * refresh_entity_map_from_db |
|
| 5051 | + * Makes sure the model object in the entity map at $id assumes the values |
|
| 5052 | + * of the database (opposite of EE_base_Class::save()) |
|
| 5053 | + * |
|
| 5054 | + * @param int|string $id |
|
| 5055 | + * @return EE_Base_Class |
|
| 5056 | + * @throws \EE_Error |
|
| 5057 | + */ |
|
| 5058 | + public function refresh_entity_map_from_db($id) |
|
| 5059 | + { |
|
| 5060 | + $obj_in_map = $this->get_from_entity_map($id); |
|
| 5061 | + if ($obj_in_map) { |
|
| 5062 | + $wpdb_results = $this->_get_all_wpdb_results( |
|
| 5063 | + array(array($this->get_primary_key_field()->get_name() => $id), 'limit' => 1) |
|
| 5064 | + ); |
|
| 5065 | + if ($wpdb_results && is_array($wpdb_results)) { |
|
| 5066 | + $one_row = reset($wpdb_results); |
|
| 5067 | + foreach ($this->_deduce_fields_n_values_from_cols_n_values($one_row) as $field_name => $db_value) { |
|
| 5068 | + $obj_in_map->set_from_db($field_name, $db_value); |
|
| 5069 | + } |
|
| 5070 | + //clear the cache of related model objects |
|
| 5071 | + foreach ($this->relation_settings() as $relation_name => $relation_obj) { |
|
| 5072 | + $obj_in_map->clear_cache($relation_name, null, true); |
|
| 5073 | + } |
|
| 5074 | + } |
|
| 5075 | + $this->_entity_map[EEM_Base::$_model_query_blog_id][$id] = $obj_in_map; |
|
| 5076 | + return $obj_in_map; |
|
| 5077 | + } else { |
|
| 5078 | + return $this->get_one_by_ID($id); |
|
| 5079 | + } |
|
| 5080 | + } |
|
| 5081 | + |
|
| 5082 | + |
|
| 5083 | + |
|
| 5084 | + /** |
|
| 5085 | + * refresh_entity_map_with |
|
| 5086 | + * Leaves the entry in the entity map alone, but updates it to match the provided |
|
| 5087 | + * $replacing_model_obj (which we assume to be its equivalent but somehow NOT in the entity map). |
|
| 5088 | + * This is useful if you have a model object you want to make authoritative over what's in the entity map currently. |
|
| 5089 | + * Note: The old $replacing_model_obj should now be destroyed as it's now un-authoritative |
|
| 5090 | + * |
|
| 5091 | + * @param int|string $id |
|
| 5092 | + * @param EE_Base_Class $replacing_model_obj |
|
| 5093 | + * @return \EE_Base_Class |
|
| 5094 | + * @throws \EE_Error |
|
| 5095 | + */ |
|
| 5096 | + public function refresh_entity_map_with($id, $replacing_model_obj) |
|
| 5097 | + { |
|
| 5098 | + $obj_in_map = $this->get_from_entity_map($id); |
|
| 5099 | + if ($obj_in_map) { |
|
| 5100 | + if ($replacing_model_obj instanceof EE_Base_Class) { |
|
| 5101 | + foreach ($replacing_model_obj->model_field_array() as $field_name => $value) { |
|
| 5102 | + $obj_in_map->set($field_name, $value); |
|
| 5103 | + } |
|
| 5104 | + //make the model object in the entity map's cache match the $replacing_model_obj |
|
| 5105 | + foreach ($this->relation_settings() as $relation_name => $relation_obj) { |
|
| 5106 | + $obj_in_map->clear_cache($relation_name, null, true); |
|
| 5107 | + foreach ($replacing_model_obj->get_all_from_cache($relation_name) as $cache_id => $cached_obj) { |
|
| 5108 | + $obj_in_map->cache($relation_name, $cached_obj, $cache_id); |
|
| 5109 | + } |
|
| 5110 | + } |
|
| 5111 | + } |
|
| 5112 | + return $obj_in_map; |
|
| 5113 | + } else { |
|
| 5114 | + $this->add_to_entity_map($replacing_model_obj); |
|
| 5115 | + return $replacing_model_obj; |
|
| 5116 | + } |
|
| 5117 | + } |
|
| 5118 | + |
|
| 5119 | + |
|
| 5120 | + |
|
| 5121 | + /** |
|
| 5122 | + * Gets the EE class that corresponds to this model. Eg, for EEM_Answer that |
|
| 5123 | + * would be EE_Answer.To import that class, you'd just add ".class.php" to the name, like so |
|
| 5124 | + * require_once($this->_getClassName().".class.php"); |
|
| 5125 | + * |
|
| 5126 | + * @return string |
|
| 5127 | + */ |
|
| 5128 | + private function _get_class_name() |
|
| 5129 | + { |
|
| 5130 | + return "EE_" . $this->get_this_model_name(); |
|
| 5131 | + } |
|
| 5132 | + |
|
| 5133 | + |
|
| 5134 | + |
|
| 5135 | + /** |
|
| 5136 | + * Get the name of the items this model represents, for the quantity specified. Eg, |
|
| 5137 | + * if $quantity==1, on EEM_Event, it would 'Event' (internationalized), otherwise |
|
| 5138 | + * it would be 'Events'. |
|
| 5139 | + * |
|
| 5140 | + * @param int $quantity |
|
| 5141 | + * @return string |
|
| 5142 | + */ |
|
| 5143 | + public function item_name($quantity = 1) |
|
| 5144 | + { |
|
| 5145 | + return (int)$quantity === 1 ? $this->singular_item : $this->plural_item; |
|
| 5146 | + } |
|
| 5147 | + |
|
| 5148 | + |
|
| 5149 | + |
|
| 5150 | + /** |
|
| 5151 | + * Very handy general function to allow for plugins to extend any child of EE_TempBase. |
|
| 5152 | + * If a method is called on a child of EE_TempBase that doesn't exist, this function is called |
|
| 5153 | + * (http://www.garfieldtech.com/blog/php-magic-call) and passed the method's name and arguments. Instead of |
|
| 5154 | + * requiring a plugin to extend the EE_TempBase (which works fine is there's only 1 plugin, but when will that |
|
| 5155 | + * happen?) they can add a hook onto 'filters_hook_espresso__{className}__{methodName}' (eg, |
|
| 5156 | + * filters_hook_espresso__EE_Answer__my_great_function) and accepts 2 arguments: the object on which the function |
|
| 5157 | + * was called, and an array of the original arguments passed to the function. Whatever their callback function |
|
| 5158 | + * returns will be returned by this function. Example: in functions.php (or in a plugin): |
|
| 5159 | + * add_filter('FHEE__EE_Answer__my_callback','my_callback',10,3); function |
|
| 5160 | + * my_callback($previousReturnValue,EE_TempBase $object,$argsArray){ |
|
| 5161 | + * $returnString= "you called my_callback! and passed args:".implode(",",$argsArray); |
|
| 5162 | + * return $previousReturnValue.$returnString; |
|
| 5163 | + * } |
|
| 5164 | + * require('EEM_Answer.model.php'); |
|
| 5165 | + * $answer=EEM_Answer::instance(); |
|
| 5166 | + * echo $answer->my_callback('monkeys',100); |
|
| 5167 | + * //will output "you called my_callback! and passed args:monkeys,100" |
|
| 5168 | + * |
|
| 5169 | + * @param string $methodName name of method which was called on a child of EE_TempBase, but which |
|
| 5170 | + * @param array $args array of original arguments passed to the function |
|
| 5171 | + * @throws EE_Error |
|
| 5172 | + * @return mixed whatever the plugin which calls add_filter decides |
|
| 5173 | + */ |
|
| 5174 | + public function __call($methodName, $args) |
|
| 5175 | + { |
|
| 5176 | + $className = get_class($this); |
|
| 5177 | + $tagName = "FHEE__{$className}__{$methodName}"; |
|
| 5178 | + if (! has_filter($tagName)) { |
|
| 5179 | + throw new EE_Error( |
|
| 5180 | + sprintf( |
|
| 5181 | + __('Method %1$s on model %2$s does not exist! You can create one with the following code in functions.php or in a plugin: %4$s function my_callback(%4$s \$previousReturnValue, EEM_Base \$object\ $argsArray=NULL ){%4$s /*function body*/%4$s return \$whatever;%4$s }%4$s add_filter( \'%3$s\', \'my_callback\', 10, 3 );', |
|
| 5182 | + 'event_espresso'), |
|
| 5183 | + $methodName, |
|
| 5184 | + $className, |
|
| 5185 | + $tagName, |
|
| 5186 | + '<br />' |
|
| 5187 | + ) |
|
| 5188 | + ); |
|
| 5189 | + } |
|
| 5190 | + return apply_filters($tagName, null, $this, $args); |
|
| 5191 | + } |
|
| 5192 | + |
|
| 5193 | + |
|
| 5194 | + |
|
| 5195 | + /** |
|
| 5196 | + * Ensures $base_class_obj_or_id is of the EE_Base_Class child that corresponds ot this model. |
|
| 5197 | + * If not, assumes its an ID, and uses $this->get_one_by_ID() to get the EE_Base_Class. |
|
| 5198 | + * |
|
| 5199 | + * @param EE_Base_Class|string|int $base_class_obj_or_id either: |
|
| 5200 | + * the EE_Base_Class object that corresponds to this Model, |
|
| 5201 | + * the object's class name |
|
| 5202 | + * or object's ID |
|
| 5203 | + * @param boolean $ensure_is_in_db if set, we will also verify this model object |
|
| 5204 | + * exists in the database. If it does not, we add it |
|
| 5205 | + * @throws EE_Error |
|
| 5206 | + * @return EE_Base_Class |
|
| 5207 | + */ |
|
| 5208 | + public function ensure_is_obj($base_class_obj_or_id, $ensure_is_in_db = false) |
|
| 5209 | + { |
|
| 5210 | + $className = $this->_get_class_name(); |
|
| 5211 | + if ($base_class_obj_or_id instanceof $className) { |
|
| 5212 | + $model_object = $base_class_obj_or_id; |
|
| 5213 | + } else { |
|
| 5214 | + $primary_key_field = $this->get_primary_key_field(); |
|
| 5215 | + if ( |
|
| 5216 | + $primary_key_field instanceof EE_Primary_Key_Int_Field |
|
| 5217 | + && ( |
|
| 5218 | + is_int($base_class_obj_or_id) |
|
| 5219 | + || is_string($base_class_obj_or_id) |
|
| 5220 | + ) |
|
| 5221 | + ) { |
|
| 5222 | + // assume it's an ID. |
|
| 5223 | + // either a proper integer or a string representing an integer (eg "101" instead of 101) |
|
| 5224 | + $model_object = $this->get_one_by_ID($base_class_obj_or_id); |
|
| 5225 | + } else if ( |
|
| 5226 | + $primary_key_field instanceof EE_Primary_Key_String_Field |
|
| 5227 | + && is_string($base_class_obj_or_id) |
|
| 5228 | + ) { |
|
| 5229 | + // assume its a string representation of the object |
|
| 5230 | + $model_object = $this->get_one_by_ID($base_class_obj_or_id); |
|
| 5231 | + } else { |
|
| 5232 | + throw new EE_Error( |
|
| 5233 | + sprintf( |
|
| 5234 | + __( |
|
| 5235 | + "'%s' is neither an object of type %s, nor an ID! Its full value is '%s'", |
|
| 5236 | + 'event_espresso' |
|
| 5237 | + ), |
|
| 5238 | + $base_class_obj_or_id, |
|
| 5239 | + $this->_get_class_name(), |
|
| 5240 | + print_r($base_class_obj_or_id, true) |
|
| 5241 | + ) |
|
| 5242 | + ); |
|
| 5243 | + } |
|
| 5244 | + } |
|
| 5245 | + if ($ensure_is_in_db && $model_object->ID() !== null) { |
|
| 5246 | + $model_object->save(); |
|
| 5247 | + } |
|
| 5248 | + return $model_object; |
|
| 5249 | + } |
|
| 5250 | + |
|
| 5251 | + |
|
| 5252 | + |
|
| 5253 | + /** |
|
| 5254 | + * Similar to ensure_is_obj(), this method makes sure $base_class_obj_or_id |
|
| 5255 | + * is a value of the this model's primary key. If it's an EE_Base_Class child, |
|
| 5256 | + * returns it ID. |
|
| 5257 | + * |
|
| 5258 | + * @param EE_Base_Class|int|string $base_class_obj_or_id |
|
| 5259 | + * @return int|string depending on the type of this model object's ID |
|
| 5260 | + * @throws EE_Error |
|
| 5261 | + */ |
|
| 5262 | + public function ensure_is_ID($base_class_obj_or_id) |
|
| 5263 | + { |
|
| 5264 | + $className = $this->_get_class_name(); |
|
| 5265 | + if ($base_class_obj_or_id instanceof $className) { |
|
| 5266 | + /** @var $base_class_obj_or_id EE_Base_Class */ |
|
| 5267 | + $id = $base_class_obj_or_id->ID(); |
|
| 5268 | + } elseif (is_int($base_class_obj_or_id)) { |
|
| 5269 | + //assume it's an ID |
|
| 5270 | + $id = $base_class_obj_or_id; |
|
| 5271 | + } elseif (is_string($base_class_obj_or_id)) { |
|
| 5272 | + //assume its a string representation of the object |
|
| 5273 | + $id = $base_class_obj_or_id; |
|
| 5274 | + } else { |
|
| 5275 | + throw new EE_Error(sprintf(__("'%s' is neither an object of type %s, nor an ID! Its full value is '%s'", |
|
| 5276 | + 'event_espresso'), $base_class_obj_or_id, $this->_get_class_name(), |
|
| 5277 | + print_r($base_class_obj_or_id, true))); |
|
| 5278 | + } |
|
| 5279 | + return $id; |
|
| 5280 | + } |
|
| 5281 | + |
|
| 5282 | + |
|
| 5283 | + |
|
| 5284 | + /** |
|
| 5285 | + * Sets whether the values passed to the model (eg, values in WHERE, values in INSERT, UPDATE, etc) |
|
| 5286 | + * have already been ran through the appropriate model field's prepare_for_use_in_db method. IE, they have |
|
| 5287 | + * been sanitized and converted into the appropriate domain. |
|
| 5288 | + * Usually the only place you'll want to change the default (which is to assume values have NOT been sanitized by |
|
| 5289 | + * the model object/model field) is when making a method call from WITHIN a model object, which has direct access |
|
| 5290 | + * to its sanitized values. Note: after changing this setting, you should set it back to its previous value (using |
|
| 5291 | + * get_assumption_concerning_values_already_prepared_by_model_object()) eg. |
|
| 5292 | + * $EVT = EEM_Event::instance(); $old_setting = |
|
| 5293 | + * $EVT->get_assumption_concerning_values_already_prepared_by_model_object(); |
|
| 5294 | + * $EVT->assume_values_already_prepared_by_model_object(true); |
|
| 5295 | + * $EVT->update(array('foo'=>'bar'),array(array('foo'=>'monkey'))); |
|
| 5296 | + * $EVT->assume_values_already_prepared_by_model_object($old_setting); |
|
| 5297 | + * |
|
| 5298 | + * @param int $values_already_prepared like one of the constants on EEM_Base |
|
| 5299 | + * @return void |
|
| 5300 | + */ |
|
| 5301 | + public function assume_values_already_prepared_by_model_object( |
|
| 5302 | + $values_already_prepared = self::not_prepared_by_model_object |
|
| 5303 | + ) { |
|
| 5304 | + $this->_values_already_prepared_by_model_object = $values_already_prepared; |
|
| 5305 | + } |
|
| 5306 | + |
|
| 5307 | + |
|
| 5308 | + |
|
| 5309 | + /** |
|
| 5310 | + * Read comments for assume_values_already_prepared_by_model_object() |
|
| 5311 | + * |
|
| 5312 | + * @return int |
|
| 5313 | + */ |
|
| 5314 | + public function get_assumption_concerning_values_already_prepared_by_model_object() |
|
| 5315 | + { |
|
| 5316 | + return $this->_values_already_prepared_by_model_object; |
|
| 5317 | + } |
|
| 5318 | + |
|
| 5319 | + |
|
| 5320 | + |
|
| 5321 | + /** |
|
| 5322 | + * Gets all the indexes on this model |
|
| 5323 | + * |
|
| 5324 | + * @return EE_Index[] |
|
| 5325 | + */ |
|
| 5326 | + public function indexes() |
|
| 5327 | + { |
|
| 5328 | + return $this->_indexes; |
|
| 5329 | + } |
|
| 5330 | + |
|
| 5331 | + |
|
| 5332 | + |
|
| 5333 | + /** |
|
| 5334 | + * Gets all the Unique Indexes on this model |
|
| 5335 | + * |
|
| 5336 | + * @return EE_Unique_Index[] |
|
| 5337 | + */ |
|
| 5338 | + public function unique_indexes() |
|
| 5339 | + { |
|
| 5340 | + $unique_indexes = array(); |
|
| 5341 | + foreach ($this->_indexes as $name => $index) { |
|
| 5342 | + if ($index instanceof EE_Unique_Index) { |
|
| 5343 | + $unique_indexes [$name] = $index; |
|
| 5344 | + } |
|
| 5345 | + } |
|
| 5346 | + return $unique_indexes; |
|
| 5347 | + } |
|
| 5348 | + |
|
| 5349 | + |
|
| 5350 | + |
|
| 5351 | + /** |
|
| 5352 | + * Gets all the fields which, when combined, make the primary key. |
|
| 5353 | + * This is usually just an array with 1 element (the primary key), but in cases |
|
| 5354 | + * where there is no primary key, it's a combination of fields as defined |
|
| 5355 | + * on a primary index |
|
| 5356 | + * |
|
| 5357 | + * @return EE_Model_Field_Base[] indexed by the field's name |
|
| 5358 | + * @throws \EE_Error |
|
| 5359 | + */ |
|
| 5360 | + public function get_combined_primary_key_fields() |
|
| 5361 | + { |
|
| 5362 | + foreach ($this->indexes() as $index) { |
|
| 5363 | + if ($index instanceof EE_Primary_Key_Index) { |
|
| 5364 | + return $index->fields(); |
|
| 5365 | + } |
|
| 5366 | + } |
|
| 5367 | + return array($this->primary_key_name() => $this->get_primary_key_field()); |
|
| 5368 | + } |
|
| 5369 | + |
|
| 5370 | + |
|
| 5371 | + |
|
| 5372 | + /** |
|
| 5373 | + * Used to build a primary key string (when the model has no primary key), |
|
| 5374 | + * which can be used a unique string to identify this model object. |
|
| 5375 | + * |
|
| 5376 | + * @param array $cols_n_values keys are field names, values are their values |
|
| 5377 | + * @return string |
|
| 5378 | + * @throws \EE_Error |
|
| 5379 | + */ |
|
| 5380 | + public function get_index_primary_key_string($cols_n_values) |
|
| 5381 | + { |
|
| 5382 | + $cols_n_values_for_primary_key_index = array_intersect_key($cols_n_values, |
|
| 5383 | + $this->get_combined_primary_key_fields()); |
|
| 5384 | + return http_build_query($cols_n_values_for_primary_key_index); |
|
| 5385 | + } |
|
| 5386 | + |
|
| 5387 | + |
|
| 5388 | + |
|
| 5389 | + /** |
|
| 5390 | + * Gets the field values from the primary key string |
|
| 5391 | + * |
|
| 5392 | + * @see EEM_Base::get_combined_primary_key_fields() and EEM_Base::get_index_primary_key_string() |
|
| 5393 | + * @param string $index_primary_key_string |
|
| 5394 | + * @return null|array |
|
| 5395 | + * @throws \EE_Error |
|
| 5396 | + */ |
|
| 5397 | + public function parse_index_primary_key_string($index_primary_key_string) |
|
| 5398 | + { |
|
| 5399 | + $key_fields = $this->get_combined_primary_key_fields(); |
|
| 5400 | + //check all of them are in the $id |
|
| 5401 | + $key_vals_in_combined_pk = array(); |
|
| 5402 | + parse_str($index_primary_key_string, $key_vals_in_combined_pk); |
|
| 5403 | + foreach ($key_fields as $key_field_name => $field_obj) { |
|
| 5404 | + if (! isset($key_vals_in_combined_pk[$key_field_name])) { |
|
| 5405 | + return null; |
|
| 5406 | + } |
|
| 5407 | + } |
|
| 5408 | + return $key_vals_in_combined_pk; |
|
| 5409 | + } |
|
| 5410 | + |
|
| 5411 | + |
|
| 5412 | + |
|
| 5413 | + /** |
|
| 5414 | + * verifies that an array of key-value pairs for model fields has a key |
|
| 5415 | + * for each field comprising the primary key index |
|
| 5416 | + * |
|
| 5417 | + * @param array $key_vals |
|
| 5418 | + * @return boolean |
|
| 5419 | + * @throws \EE_Error |
|
| 5420 | + */ |
|
| 5421 | + public function has_all_combined_primary_key_fields($key_vals) |
|
| 5422 | + { |
|
| 5423 | + $keys_it_should_have = array_keys($this->get_combined_primary_key_fields()); |
|
| 5424 | + foreach ($keys_it_should_have as $key) { |
|
| 5425 | + if (! isset($key_vals[$key])) { |
|
| 5426 | + return false; |
|
| 5427 | + } |
|
| 5428 | + } |
|
| 5429 | + return true; |
|
| 5430 | + } |
|
| 5431 | + |
|
| 5432 | + |
|
| 5433 | + |
|
| 5434 | + /** |
|
| 5435 | + * Finds all model objects in the DB that appear to be a copy of $model_object_or_attributes_array. |
|
| 5436 | + * We consider something to be a copy if all the attributes match (except the ID, of course). |
|
| 5437 | + * |
|
| 5438 | + * @param array|EE_Base_Class $model_object_or_attributes_array If its an array, it's field-value pairs |
|
| 5439 | + * @param array $query_params like EEM_Base::get_all's query_params. |
|
| 5440 | + * @throws EE_Error |
|
| 5441 | + * @return \EE_Base_Class[] Array keys are object IDs (if there is a primary key on the model. if not, numerically |
|
| 5442 | + * indexed) |
|
| 5443 | + */ |
|
| 5444 | + public function get_all_copies($model_object_or_attributes_array, $query_params = array()) |
|
| 5445 | + { |
|
| 5446 | + if ($model_object_or_attributes_array instanceof EE_Base_Class) { |
|
| 5447 | + $attributes_array = $model_object_or_attributes_array->model_field_array(); |
|
| 5448 | + } elseif (is_array($model_object_or_attributes_array)) { |
|
| 5449 | + $attributes_array = $model_object_or_attributes_array; |
|
| 5450 | + } else { |
|
| 5451 | + throw new EE_Error(sprintf(__("get_all_copies should be provided with either a model object or an array of field-value-pairs, but was given %s", |
|
| 5452 | + "event_espresso"), $model_object_or_attributes_array)); |
|
| 5453 | + } |
|
| 5454 | + //even copies obviously won't have the same ID, so remove the primary key |
|
| 5455 | + //from the WHERE conditions for finding copies (if there is a primary key, of course) |
|
| 5456 | + if ($this->has_primary_key_field() && isset($attributes_array[$this->primary_key_name()])) { |
|
| 5457 | + unset($attributes_array[$this->primary_key_name()]); |
|
| 5458 | + } |
|
| 5459 | + if (isset($query_params[0])) { |
|
| 5460 | + $query_params[0] = array_merge($attributes_array, $query_params); |
|
| 5461 | + } else { |
|
| 5462 | + $query_params[0] = $attributes_array; |
|
| 5463 | + } |
|
| 5464 | + return $this->get_all($query_params); |
|
| 5465 | + } |
|
| 5466 | + |
|
| 5467 | + |
|
| 5468 | + |
|
| 5469 | + /** |
|
| 5470 | + * Gets the first copy we find. See get_all_copies for more details |
|
| 5471 | + * |
|
| 5472 | + * @param mixed EE_Base_Class | array $model_object_or_attributes_array |
|
| 5473 | + * @param array $query_params |
|
| 5474 | + * @return EE_Base_Class |
|
| 5475 | + * @throws \EE_Error |
|
| 5476 | + */ |
|
| 5477 | + public function get_one_copy($model_object_or_attributes_array, $query_params = array()) |
|
| 5478 | + { |
|
| 5479 | + if (! is_array($query_params)) { |
|
| 5480 | + EE_Error::doing_it_wrong('EEM_Base::get_one_copy', |
|
| 5481 | + sprintf(__('$query_params should be an array, you passed a variable of type %s', 'event_espresso'), |
|
| 5482 | + gettype($query_params)), '4.6.0'); |
|
| 5483 | + $query_params = array(); |
|
| 5484 | + } |
|
| 5485 | + $query_params['limit'] = 1; |
|
| 5486 | + $copies = $this->get_all_copies($model_object_or_attributes_array, $query_params); |
|
| 5487 | + if (is_array($copies)) { |
|
| 5488 | + return array_shift($copies); |
|
| 5489 | + } else { |
|
| 5490 | + return null; |
|
| 5491 | + } |
|
| 5492 | + } |
|
| 5493 | + |
|
| 5494 | + |
|
| 5495 | + |
|
| 5496 | + /** |
|
| 5497 | + * Updates the item with the specified id. Ignores default query parameters because |
|
| 5498 | + * we have specified the ID, and its assumed we KNOW what we're doing |
|
| 5499 | + * |
|
| 5500 | + * @param array $fields_n_values keys are field names, values are their new values |
|
| 5501 | + * @param int|string $id the value of the primary key to update |
|
| 5502 | + * @return int number of rows updated |
|
| 5503 | + * @throws \EE_Error |
|
| 5504 | + */ |
|
| 5505 | + public function update_by_ID($fields_n_values, $id) |
|
| 5506 | + { |
|
| 5507 | + $query_params = array( |
|
| 5508 | + 0 => array($this->get_primary_key_field()->get_name() => $id), |
|
| 5509 | + 'default_where_conditions' => EEM_Base::default_where_conditions_others_only, |
|
| 5510 | + ); |
|
| 5511 | + return $this->update($fields_n_values, $query_params); |
|
| 5512 | + } |
|
| 5513 | + |
|
| 5514 | + |
|
| 5515 | + |
|
| 5516 | + /** |
|
| 5517 | + * Changes an operator which was supplied to the models into one usable in SQL |
|
| 5518 | + * |
|
| 5519 | + * @param string $operator_supplied |
|
| 5520 | + * @return string an operator which can be used in SQL |
|
| 5521 | + * @throws EE_Error |
|
| 5522 | + */ |
|
| 5523 | + private function _prepare_operator_for_sql($operator_supplied) |
|
| 5524 | + { |
|
| 5525 | + $sql_operator = isset($this->_valid_operators[$operator_supplied]) ? $this->_valid_operators[$operator_supplied] |
|
| 5526 | + : null; |
|
| 5527 | + if ($sql_operator) { |
|
| 5528 | + return $sql_operator; |
|
| 5529 | + } else { |
|
| 5530 | + throw new EE_Error(sprintf(__("The operator '%s' is not in the list of valid operators: %s", |
|
| 5531 | + "event_espresso"), $operator_supplied, implode(",", array_keys($this->_valid_operators)))); |
|
| 5532 | + } |
|
| 5533 | + } |
|
| 5534 | + |
|
| 5535 | + |
|
| 5536 | + |
|
| 5537 | + /** |
|
| 5538 | + * Gets the valid operators |
|
| 5539 | + * @return array keys are accepted strings, values are the SQL they are converted to |
|
| 5540 | + */ |
|
| 5541 | + public function valid_operators(){ |
|
| 5542 | + return $this->_valid_operators; |
|
| 5543 | + } |
|
| 5544 | + |
|
| 5545 | + |
|
| 5546 | + |
|
| 5547 | + /** |
|
| 5548 | + * Gets the between-style operators (take 2 arguments). |
|
| 5549 | + * @return array keys are accepted strings, values are the SQL they are converted to |
|
| 5550 | + */ |
|
| 5551 | + public function valid_between_style_operators() |
|
| 5552 | + { |
|
| 5553 | + return array_intersect( |
|
| 5554 | + $this->valid_operators(), |
|
| 5555 | + $this->_between_style_operators |
|
| 5556 | + ); |
|
| 5557 | + } |
|
| 5558 | + |
|
| 5559 | + /** |
|
| 5560 | + * Gets the "like"-style operators (take a single argument, but it may contain wildcards) |
|
| 5561 | + * @return array keys are accepted strings, values are the SQL they are converted to |
|
| 5562 | + */ |
|
| 5563 | + public function valid_like_style_operators() |
|
| 5564 | + { |
|
| 5565 | + return array_intersect( |
|
| 5566 | + $this->valid_operators(), |
|
| 5567 | + $this->_like_style_operators |
|
| 5568 | + ); |
|
| 5569 | + } |
|
| 5570 | + |
|
| 5571 | + /** |
|
| 5572 | + * Gets the "in"-style operators |
|
| 5573 | + * @return array keys are accepted strings, values are the SQL they are converted to |
|
| 5574 | + */ |
|
| 5575 | + public function valid_in_style_operators() |
|
| 5576 | + { |
|
| 5577 | + return array_intersect( |
|
| 5578 | + $this->valid_operators(), |
|
| 5579 | + $this->_in_style_operators |
|
| 5580 | + ); |
|
| 5581 | + } |
|
| 5582 | + |
|
| 5583 | + /** |
|
| 5584 | + * Gets the "null"-style operators (accept no arguments) |
|
| 5585 | + * @return array keys are accepted strings, values are the SQL they are converted to |
|
| 5586 | + */ |
|
| 5587 | + public function valid_null_style_operators() |
|
| 5588 | + { |
|
| 5589 | + return array_intersect( |
|
| 5590 | + $this->valid_operators(), |
|
| 5591 | + $this->_null_style_operators |
|
| 5592 | + ); |
|
| 5593 | + } |
|
| 5594 | + |
|
| 5595 | + /** |
|
| 5596 | + * Gets an array where keys are the primary keys and values are their 'names' |
|
| 5597 | + * (as determined by the model object's name() function, which is often overridden) |
|
| 5598 | + * |
|
| 5599 | + * @param array $query_params like get_all's |
|
| 5600 | + * @return string[] |
|
| 5601 | + * @throws \EE_Error |
|
| 5602 | + */ |
|
| 5603 | + public function get_all_names($query_params = array()) |
|
| 5604 | + { |
|
| 5605 | + $objs = $this->get_all($query_params); |
|
| 5606 | + $names = array(); |
|
| 5607 | + foreach ($objs as $obj) { |
|
| 5608 | + $names[$obj->ID()] = $obj->name(); |
|
| 5609 | + } |
|
| 5610 | + return $names; |
|
| 5611 | + } |
|
| 5612 | + |
|
| 5613 | + |
|
| 5614 | + |
|
| 5615 | + /** |
|
| 5616 | + * Gets an array of primary keys from the model objects. If you acquired the model objects |
|
| 5617 | + * using EEM_Base::get_all() you don't need to call this (and probably shouldn't because |
|
| 5618 | + * this is duplicated effort and reduces efficiency) you would be better to use |
|
| 5619 | + * array_keys() on $model_objects. |
|
| 5620 | + * |
|
| 5621 | + * @param \EE_Base_Class[] $model_objects |
|
| 5622 | + * @param boolean $filter_out_empty_ids if a model object has an ID of '' or 0, don't bother including it |
|
| 5623 | + * in the returned array |
|
| 5624 | + * @return array |
|
| 5625 | + * @throws \EE_Error |
|
| 5626 | + */ |
|
| 5627 | + public function get_IDs($model_objects, $filter_out_empty_ids = false) |
|
| 5628 | + { |
|
| 5629 | + if (! $this->has_primary_key_field()) { |
|
| 5630 | + if (WP_DEBUG) { |
|
| 5631 | + EE_Error::add_error( |
|
| 5632 | + __('Trying to get IDs from a model than has no primary key', 'event_espresso'), |
|
| 5633 | + __FILE__, |
|
| 5634 | + __FUNCTION__, |
|
| 5635 | + __LINE__ |
|
| 5636 | + ); |
|
| 5637 | + } |
|
| 5638 | + } |
|
| 5639 | + $IDs = array(); |
|
| 5640 | + foreach ($model_objects as $model_object) { |
|
| 5641 | + $id = $model_object->ID(); |
|
| 5642 | + if (! $id) { |
|
| 5643 | + if ($filter_out_empty_ids) { |
|
| 5644 | + continue; |
|
| 5645 | + } |
|
| 5646 | + if (WP_DEBUG) { |
|
| 5647 | + EE_Error::add_error( |
|
| 5648 | + __( |
|
| 5649 | + 'Called %1$s on a model object that has no ID and so probably hasn\'t been saved to the database', |
|
| 5650 | + 'event_espresso' |
|
| 5651 | + ), |
|
| 5652 | + __FILE__, |
|
| 5653 | + __FUNCTION__, |
|
| 5654 | + __LINE__ |
|
| 5655 | + ); |
|
| 5656 | + } |
|
| 5657 | + } |
|
| 5658 | + $IDs[] = $id; |
|
| 5659 | + } |
|
| 5660 | + return $IDs; |
|
| 5661 | + } |
|
| 5662 | + |
|
| 5663 | + |
|
| 5664 | + |
|
| 5665 | + /** |
|
| 5666 | + * Returns the string used in capabilities relating to this model. If there |
|
| 5667 | + * are no capabilities that relate to this model returns false |
|
| 5668 | + * |
|
| 5669 | + * @return string|false |
|
| 5670 | + */ |
|
| 5671 | + public function cap_slug() |
|
| 5672 | + { |
|
| 5673 | + return apply_filters('FHEE__EEM_Base__cap_slug', $this->_caps_slug, $this); |
|
| 5674 | + } |
|
| 5675 | + |
|
| 5676 | + |
|
| 5677 | + |
|
| 5678 | + /** |
|
| 5679 | + * Returns the capability-restrictions array (@see EEM_Base::_cap_restrictions). |
|
| 5680 | + * If $context is provided (which should be set to one of EEM_Base::valid_cap_contexts()) |
|
| 5681 | + * only returns the cap restrictions array in that context (ie, the array |
|
| 5682 | + * at that key) |
|
| 5683 | + * |
|
| 5684 | + * @param string $context |
|
| 5685 | + * @return EE_Default_Where_Conditions[] indexed by associated capability |
|
| 5686 | + * @throws \EE_Error |
|
| 5687 | + */ |
|
| 5688 | + public function cap_restrictions($context = EEM_Base::caps_read) |
|
| 5689 | + { |
|
| 5690 | + EEM_Base::verify_is_valid_cap_context($context); |
|
| 5691 | + //check if we ought to run the restriction generator first |
|
| 5692 | + if ( |
|
| 5693 | + isset($this->_cap_restriction_generators[$context]) |
|
| 5694 | + && $this->_cap_restriction_generators[$context] instanceof EE_Restriction_Generator_Base |
|
| 5695 | + && ! $this->_cap_restriction_generators[$context]->has_generated_cap_restrictions() |
|
| 5696 | + ) { |
|
| 5697 | + $this->_cap_restrictions[$context] = array_merge( |
|
| 5698 | + $this->_cap_restrictions[$context], |
|
| 5699 | + $this->_cap_restriction_generators[$context]->generate_restrictions() |
|
| 5700 | + ); |
|
| 5701 | + } |
|
| 5702 | + //and make sure we've finalized the construction of each restriction |
|
| 5703 | + foreach ($this->_cap_restrictions[$context] as $where_conditions_obj) { |
|
| 5704 | + if ($where_conditions_obj instanceof EE_Default_Where_Conditions) { |
|
| 5705 | + $where_conditions_obj->_finalize_construct($this); |
|
| 5706 | + } |
|
| 5707 | + } |
|
| 5708 | + return $this->_cap_restrictions[$context]; |
|
| 5709 | + } |
|
| 5710 | + |
|
| 5711 | + |
|
| 5712 | + |
|
| 5713 | + /** |
|
| 5714 | + * Indicating whether or not this model thinks its a wp core model |
|
| 5715 | + * |
|
| 5716 | + * @return boolean |
|
| 5717 | + */ |
|
| 5718 | + public function is_wp_core_model() |
|
| 5719 | + { |
|
| 5720 | + return $this->_wp_core_model; |
|
| 5721 | + } |
|
| 5722 | + |
|
| 5723 | + |
|
| 5724 | + |
|
| 5725 | + /** |
|
| 5726 | + * Gets all the caps that are missing which impose a restriction on |
|
| 5727 | + * queries made in this context |
|
| 5728 | + * |
|
| 5729 | + * @param string $context one of EEM_Base::caps_ constants |
|
| 5730 | + * @return EE_Default_Where_Conditions[] indexed by capability name |
|
| 5731 | + * @throws \EE_Error |
|
| 5732 | + */ |
|
| 5733 | + public function caps_missing($context = EEM_Base::caps_read) |
|
| 5734 | + { |
|
| 5735 | + $missing_caps = array(); |
|
| 5736 | + $cap_restrictions = $this->cap_restrictions($context); |
|
| 5737 | + foreach ($cap_restrictions as $cap => $restriction_if_no_cap) { |
|
| 5738 | + if (! EE_Capabilities::instance() |
|
| 5739 | + ->current_user_can($cap, $this->get_this_model_name() . '_model_applying_caps') |
|
| 5740 | + ) { |
|
| 5741 | + $missing_caps[$cap] = $restriction_if_no_cap; |
|
| 5742 | + } |
|
| 5743 | + } |
|
| 5744 | + return $missing_caps; |
|
| 5745 | + } |
|
| 5746 | + |
|
| 5747 | + |
|
| 5748 | + |
|
| 5749 | + /** |
|
| 5750 | + * Gets the mapping from capability contexts to action strings used in capability names |
|
| 5751 | + * |
|
| 5752 | + * @return array keys are one of EEM_Base::valid_cap_contexts(), and values are usually |
|
| 5753 | + * one of 'read', 'edit', or 'delete' |
|
| 5754 | + */ |
|
| 5755 | + public function cap_contexts_to_cap_action_map() |
|
| 5756 | + { |
|
| 5757 | + return apply_filters('FHEE__EEM_Base__cap_contexts_to_cap_action_map', $this->_cap_contexts_to_cap_action_map, |
|
| 5758 | + $this); |
|
| 5759 | + } |
|
| 5760 | + |
|
| 5761 | + |
|
| 5762 | + |
|
| 5763 | + /** |
|
| 5764 | + * Gets the action string for the specified capability context |
|
| 5765 | + * |
|
| 5766 | + * @param string $context |
|
| 5767 | + * @return string one of EEM_Base::cap_contexts_to_cap_action_map() values |
|
| 5768 | + * @throws \EE_Error |
|
| 5769 | + */ |
|
| 5770 | + public function cap_action_for_context($context) |
|
| 5771 | + { |
|
| 5772 | + $mapping = $this->cap_contexts_to_cap_action_map(); |
|
| 5773 | + if (isset($mapping[$context])) { |
|
| 5774 | + return $mapping[$context]; |
|
| 5775 | + } |
|
| 5776 | + if ($action = apply_filters('FHEE__EEM_Base__cap_action_for_context', null, $this, $mapping, $context)) { |
|
| 5777 | + return $action; |
|
| 5778 | + } |
|
| 5779 | + throw new EE_Error( |
|
| 5780 | + sprintf( |
|
| 5781 | + __('Cannot find capability restrictions for context "%1$s", allowed values are:%2$s', 'event_espresso'), |
|
| 5782 | + $context, |
|
| 5783 | + implode(',', array_keys($this->cap_contexts_to_cap_action_map())) |
|
| 5784 | + ) |
|
| 5785 | + ); |
|
| 5786 | + } |
|
| 5787 | + |
|
| 5788 | + |
|
| 5789 | + |
|
| 5790 | + /** |
|
| 5791 | + * Returns all the capability contexts which are valid when querying models |
|
| 5792 | + * |
|
| 5793 | + * @return array |
|
| 5794 | + */ |
|
| 5795 | + public static function valid_cap_contexts() |
|
| 5796 | + { |
|
| 5797 | + return apply_filters('FHEE__EEM_Base__valid_cap_contexts', array( |
|
| 5798 | + self::caps_read, |
|
| 5799 | + self::caps_read_admin, |
|
| 5800 | + self::caps_edit, |
|
| 5801 | + self::caps_delete, |
|
| 5802 | + )); |
|
| 5803 | + } |
|
| 5804 | + |
|
| 5805 | + |
|
| 5806 | + |
|
| 5807 | + /** |
|
| 5808 | + * Returns all valid options for 'default_where_conditions' |
|
| 5809 | + * |
|
| 5810 | + * @return array |
|
| 5811 | + */ |
|
| 5812 | + public static function valid_default_where_conditions() |
|
| 5813 | + { |
|
| 5814 | + return array( |
|
| 5815 | + EEM_Base::default_where_conditions_all, |
|
| 5816 | + EEM_Base::default_where_conditions_this_only, |
|
| 5817 | + EEM_Base::default_where_conditions_others_only, |
|
| 5818 | + EEM_Base::default_where_conditions_minimum_all, |
|
| 5819 | + EEM_Base::default_where_conditions_minimum_others, |
|
| 5820 | + EEM_Base::default_where_conditions_none |
|
| 5821 | + ); |
|
| 5822 | + } |
|
| 5823 | + |
|
| 5824 | + // public static function default_where_conditions_full |
|
| 5825 | + /** |
|
| 5826 | + * Verifies $context is one of EEM_Base::valid_cap_contexts(), if not it throws an exception |
|
| 5827 | + * |
|
| 5828 | + * @param string $context |
|
| 5829 | + * @return bool |
|
| 5830 | + * @throws \EE_Error |
|
| 5831 | + */ |
|
| 5832 | + static public function verify_is_valid_cap_context($context) |
|
| 5833 | + { |
|
| 5834 | + $valid_cap_contexts = EEM_Base::valid_cap_contexts(); |
|
| 5835 | + if (in_array($context, $valid_cap_contexts)) { |
|
| 5836 | + return true; |
|
| 5837 | + } else { |
|
| 5838 | + throw new EE_Error( |
|
| 5839 | + sprintf( |
|
| 5840 | + __('Context "%1$s" passed into model "%2$s" is not a valid context. They are: %3$s', |
|
| 5841 | + 'event_espresso'), |
|
| 5842 | + $context, |
|
| 5843 | + 'EEM_Base', |
|
| 5844 | + implode(',', $valid_cap_contexts) |
|
| 5845 | + ) |
|
| 5846 | + ); |
|
| 5847 | + } |
|
| 5848 | + } |
|
| 5849 | + |
|
| 5850 | + |
|
| 5851 | + |
|
| 5852 | + /** |
|
| 5853 | + * Clears all the models field caches. This is only useful when a sub-class |
|
| 5854 | + * might have added a field or something and these caches might be invalidated |
|
| 5855 | + */ |
|
| 5856 | + protected function _invalidate_field_caches() |
|
| 5857 | + { |
|
| 5858 | + $this->_cache_foreign_key_to_fields = array(); |
|
| 5859 | + $this->_cached_fields = null; |
|
| 5860 | + $this->_cached_fields_non_db_only = null; |
|
| 5861 | + } |
|
| 5862 | + |
|
| 5863 | + |
|
| 5864 | + |
|
| 5865 | + /** |
|
| 5866 | + * Gets the list of all the where query param keys that relate to logic instead of field names |
|
| 5867 | + * (eg "and", "or", "not"). |
|
| 5868 | + * |
|
| 5869 | + * @return array |
|
| 5870 | + */ |
|
| 5871 | + public function logic_query_param_keys() |
|
| 5872 | + { |
|
| 5873 | + return $this->_logic_query_param_keys; |
|
| 5874 | + } |
|
| 5875 | + |
|
| 5876 | + |
|
| 5877 | + |
|
| 5878 | + /** |
|
| 5879 | + * Determines whether or not the where query param array key is for a logic query param. |
|
| 5880 | + * Eg 'OR', 'not*', and 'and*because-i-say-so' shoudl all return true, whereas |
|
| 5881 | + * 'ATT_fname', 'EVT_name*not-you-or-me', and 'ORG_name' should return false |
|
| 5882 | + * |
|
| 5883 | + * @param $query_param_key |
|
| 5884 | + * @return bool |
|
| 5885 | + */ |
|
| 5886 | + public function is_logic_query_param_key($query_param_key) |
|
| 5887 | + { |
|
| 5888 | + foreach ($this->logic_query_param_keys() as $logic_query_param_key) { |
|
| 5889 | + if ($query_param_key === $logic_query_param_key |
|
| 5890 | + || strpos($query_param_key, $logic_query_param_key . '*') === 0 |
|
| 5891 | + ) { |
|
| 5892 | + return true; |
|
| 5893 | + } |
|
| 5894 | + } |
|
| 5895 | + return false; |
|
| 5896 | + } |
|
| 5897 | 5897 | |
| 5898 | 5898 | |
| 5899 | 5899 | |
@@ -21,8 +21,8 @@ discard block |
||
| 21 | 21 | * |
| 22 | 22 | * ------------------------------------------------------------------------ |
| 23 | 23 | */ |
| 24 | -require_once ( EE_MODELS . 'EEM_Soft_Delete_Base.model.php' ); |
|
| 25 | -require_once( EE_CLASSES . 'EE_Question_Option.class.php'); |
|
| 24 | +require_once (EE_MODELS.'EEM_Soft_Delete_Base.model.php'); |
|
| 25 | +require_once(EE_CLASSES.'EE_Question_Option.class.php'); |
|
| 26 | 26 | |
| 27 | 27 | |
| 28 | 28 | class EEM_Question_Option extends EEM_Soft_Delete_Base { |
@@ -30,22 +30,22 @@ discard block |
||
| 30 | 30 | // private instance of the Attendee object |
| 31 | 31 | protected static $_instance = NULL; |
| 32 | 32 | |
| 33 | - protected function __construct( $timezone = NULL ) { |
|
| 34 | - $this->singular_item = __('Question Option','event_espresso'); |
|
| 35 | - $this->plural_item = __('Question Options','event_espresso'); |
|
| 33 | + protected function __construct($timezone = NULL) { |
|
| 34 | + $this->singular_item = __('Question Option', 'event_espresso'); |
|
| 35 | + $this->plural_item = __('Question Options', 'event_espresso'); |
|
| 36 | 36 | |
| 37 | 37 | $this->_tables = array( |
| 38 | - 'Question_Option'=>new EE_Primary_Table('esp_question_option','QSO_ID') |
|
| 38 | + 'Question_Option'=>new EE_Primary_Table('esp_question_option', 'QSO_ID') |
|
| 39 | 39 | ); |
| 40 | 40 | $this->_fields = array( |
| 41 | 41 | 'Question_Option'=>array( |
| 42 | - 'QSO_ID'=>new EE_Primary_Key_Int_Field('QSO_ID', __('Question Option ID','event_espresso')), |
|
| 43 | - 'QST_ID'=>new EE_Foreign_Key_Int_Field('QST_ID', __('Question ID','event_espresso'), false, 0, 'Question'), |
|
| 44 | - 'QSO_value'=>new EE_Plain_Text_Field('QSO_value', __("Question Option Value", "event_espresso"),false,''), |
|
| 45 | - 'QSO_desc'=>new EE_Post_Content_Field('QSO_desc', __('Question Option Description','event_espresso'), false, ''), |
|
| 46 | - 'QSO_order' => new EE_Integer_Field('QSO_order', __('Question Option Order', 'event_espresso' ), false, 0 ), |
|
| 47 | - 'QSO_system'=>new EE_Plain_Text_Field('QSO_system', __('Internal string ID for question option','event_espresso'), TRUE, NULL ), |
|
| 48 | - 'QSO_deleted'=>new EE_Trashed_Flag_Field('QSO_deleted', __('Flag indicating Option was trashed','event_espresso'), false, false) |
|
| 42 | + 'QSO_ID'=>new EE_Primary_Key_Int_Field('QSO_ID', __('Question Option ID', 'event_espresso')), |
|
| 43 | + 'QST_ID'=>new EE_Foreign_Key_Int_Field('QST_ID', __('Question ID', 'event_espresso'), false, 0, 'Question'), |
|
| 44 | + 'QSO_value'=>new EE_Plain_Text_Field('QSO_value', __("Question Option Value", "event_espresso"), false, ''), |
|
| 45 | + 'QSO_desc'=>new EE_Post_Content_Field('QSO_desc', __('Question Option Description', 'event_espresso'), false, ''), |
|
| 46 | + 'QSO_order' => new EE_Integer_Field('QSO_order', __('Question Option Order', 'event_espresso'), false, 0), |
|
| 47 | + 'QSO_system'=>new EE_Plain_Text_Field('QSO_system', __('Internal string ID for question option', 'event_espresso'), TRUE, NULL), |
|
| 48 | + 'QSO_deleted'=>new EE_Trashed_Flag_Field('QSO_deleted', __('Flag indicating Option was trashed', 'event_espresso'), false, false) |
|
| 49 | 49 | ) |
| 50 | 50 | ); |
| 51 | 51 | $this->_model_relations = array( |
@@ -54,12 +54,12 @@ discard block |
||
| 54 | 54 | |
| 55 | 55 | $this->_model_chain_to_wp_user = 'Question'; |
| 56 | 56 | //this model is generally available for reading |
| 57 | - $this->_cap_restriction_generators[ EEM_Base::caps_read ] = new EE_Restriction_Generator_Public(); |
|
| 58 | - $this->_cap_restriction_generators[ EEM_Base::caps_read_admin ] = new EE_Restriction_Generator_Reg_Form('QSO_system'); |
|
| 59 | - $this->_cap_restriction_generators[ EEM_Base::caps_edit ] = new EE_Restriction_Generator_Reg_Form('QSO_system'); |
|
| 60 | - $this->_cap_restriction_generators[ EEM_Base::caps_delete ] = new EE_Restriction_Generator_Reg_Form('QSO_system'); |
|
| 57 | + $this->_cap_restriction_generators[EEM_Base::caps_read] = new EE_Restriction_Generator_Public(); |
|
| 58 | + $this->_cap_restriction_generators[EEM_Base::caps_read_admin] = new EE_Restriction_Generator_Reg_Form('QSO_system'); |
|
| 59 | + $this->_cap_restriction_generators[EEM_Base::caps_edit] = new EE_Restriction_Generator_Reg_Form('QSO_system'); |
|
| 60 | + $this->_cap_restriction_generators[EEM_Base::caps_delete] = new EE_Restriction_Generator_Reg_Form('QSO_system'); |
|
| 61 | 61 | $this->_caps_slug = 'questions'; |
| 62 | - parent::__construct( $timezone ); |
|
| 62 | + parent::__construct($timezone); |
|
| 63 | 63 | } |
| 64 | 64 | |
| 65 | 65 | |
@@ -5,8 +5,8 @@ discard block |
||
| 5 | 5 | // if you're a dev and want to receive all errors via email |
| 6 | 6 | // add this to your wp-config.php: define( 'EE_ERROR_EMAILS', TRUE ); |
| 7 | 7 | if (defined('WP_DEBUG') && WP_DEBUG === true && defined('EE_ERROR_EMAILS') && EE_ERROR_EMAILS === true) { |
| 8 | - set_error_handler(array('EE_Error', 'error_handler')); |
|
| 9 | - register_shutdown_function(array('EE_Error', 'fatal_error_handler')); |
|
| 8 | + set_error_handler(array('EE_Error', 'error_handler')); |
|
| 9 | + register_shutdown_function(array('EE_Error', 'fatal_error_handler')); |
|
| 10 | 10 | } |
| 11 | 11 | |
| 12 | 12 | |
@@ -23,259 +23,259 @@ discard block |
||
| 23 | 23 | { |
| 24 | 24 | |
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * name of the file to log exceptions to |
|
| 28 | - * |
|
| 29 | - * @var string |
|
| 30 | - */ |
|
| 31 | - private static $_exception_log_file = 'espresso_error_log.txt'; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * stores details for all exception |
|
| 35 | - * |
|
| 36 | - * @var array |
|
| 37 | - */ |
|
| 38 | - private static $_all_exceptions = array(); |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * tracks number of errors |
|
| 42 | - * |
|
| 43 | - * @var int |
|
| 44 | - */ |
|
| 45 | - private static $_error_count = 0; |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * has shutdown action been added ? |
|
| 49 | - * |
|
| 50 | - * @var array $_espresso_notices |
|
| 51 | - */ |
|
| 52 | - private static $_espresso_notices = array('success' => false, 'errors' => false, 'attention' => false); |
|
| 53 | - |
|
| 54 | - |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * @override default exception handling |
|
| 58 | - * @param string $message |
|
| 59 | - * @param int $code |
|
| 60 | - * @param Exception|null $previous |
|
| 61 | - */ |
|
| 62 | - public function __construct($message, $code = 0, Exception $previous = null) |
|
| 63 | - { |
|
| 64 | - if (version_compare(PHP_VERSION, '5.3.0', '<')) { |
|
| 65 | - parent::__construct($message, $code); |
|
| 66 | - } else { |
|
| 67 | - parent::__construct($message, $code, $previous); |
|
| 68 | - } |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * error_handler |
|
| 75 | - * |
|
| 76 | - * @param $code |
|
| 77 | - * @param $message |
|
| 78 | - * @param $file |
|
| 79 | - * @param $line |
|
| 80 | - * @return void |
|
| 81 | - */ |
|
| 82 | - public static function error_handler($code, $message, $file, $line) |
|
| 83 | - { |
|
| 84 | - $type = EE_Error::error_type($code); |
|
| 85 | - $site = site_url(); |
|
| 86 | - switch ($site) { |
|
| 87 | - case 'http://ee4.eventespresso.com/' : |
|
| 88 | - case 'http://ee4decaf.eventespresso.com/' : |
|
| 89 | - case 'http://ee4hf.eventespresso.com/' : |
|
| 90 | - case 'http://ee4a.eventespresso.com/' : |
|
| 91 | - case 'http://ee4ad.eventespresso.com/' : |
|
| 92 | - case 'http://ee4b.eventespresso.com/' : |
|
| 93 | - case 'http://ee4bd.eventespresso.com/' : |
|
| 94 | - case 'http://ee4d.eventespresso.com/' : |
|
| 95 | - case 'http://ee4dd.eventespresso.com/' : |
|
| 96 | - $to = '[email protected]'; |
|
| 97 | - break; |
|
| 98 | - default : |
|
| 99 | - $to = get_option('admin_email'); |
|
| 100 | - } |
|
| 101 | - $subject = $type . ' ' . $message . ' in ' . EVENT_ESPRESSO_VERSION . ' on ' . site_url(); |
|
| 102 | - $msg = EE_Error::_format_error($type, $message, $file, $line); |
|
| 103 | - if (function_exists('wp_mail')) { |
|
| 104 | - add_filter('wp_mail_content_type', array('EE_Error', 'set_content_type')); |
|
| 105 | - wp_mail($to, $subject, $msg); |
|
| 106 | - } |
|
| 107 | - echo '<div id="message" class="espresso-notices error"><p>'; |
|
| 108 | - echo $type . ': ' . $message . '<br />' . $file . ' line ' . $line; |
|
| 109 | - echo '<br /></p></div>'; |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * error_type |
|
| 116 | - * http://www.php.net/manual/en/errorfunc.constants.php#109430 |
|
| 117 | - * |
|
| 118 | - * @param $code |
|
| 119 | - * @return string |
|
| 120 | - */ |
|
| 121 | - public static function error_type($code) |
|
| 122 | - { |
|
| 123 | - switch ($code) { |
|
| 124 | - case E_ERROR: // 1 // |
|
| 125 | - return 'E_ERROR'; |
|
| 126 | - case E_WARNING: // 2 // |
|
| 127 | - return 'E_WARNING'; |
|
| 128 | - case E_PARSE: // 4 // |
|
| 129 | - return 'E_PARSE'; |
|
| 130 | - case E_NOTICE: // 8 // |
|
| 131 | - return 'E_NOTICE'; |
|
| 132 | - case E_CORE_ERROR: // 16 // |
|
| 133 | - return 'E_CORE_ERROR'; |
|
| 134 | - case E_CORE_WARNING: // 32 // |
|
| 135 | - return 'E_CORE_WARNING'; |
|
| 136 | - case E_COMPILE_ERROR: // 64 // |
|
| 137 | - return 'E_COMPILE_ERROR'; |
|
| 138 | - case E_COMPILE_WARNING: // 128 // |
|
| 139 | - return 'E_COMPILE_WARNING'; |
|
| 140 | - case E_USER_ERROR: // 256 // |
|
| 141 | - return 'E_USER_ERROR'; |
|
| 142 | - case E_USER_WARNING: // 512 // |
|
| 143 | - return 'E_USER_WARNING'; |
|
| 144 | - case E_USER_NOTICE: // 1024 // |
|
| 145 | - return 'E_USER_NOTICE'; |
|
| 146 | - case E_STRICT: // 2048 // |
|
| 147 | - return 'E_STRICT'; |
|
| 148 | - case E_RECOVERABLE_ERROR: // 4096 // |
|
| 149 | - return 'E_RECOVERABLE_ERROR'; |
|
| 150 | - case E_DEPRECATED: // 8192 // |
|
| 151 | - return 'E_DEPRECATED'; |
|
| 152 | - case E_USER_DEPRECATED: // 16384 // |
|
| 153 | - return 'E_USER_DEPRECATED'; |
|
| 154 | - case E_ALL: // 16384 // |
|
| 155 | - return 'E_ALL'; |
|
| 156 | - } |
|
| 157 | - return ''; |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * fatal_error_handler |
|
| 164 | - * |
|
| 165 | - * @return void |
|
| 166 | - */ |
|
| 167 | - public static function fatal_error_handler() |
|
| 168 | - { |
|
| 169 | - $last_error = error_get_last(); |
|
| 170 | - if ($last_error['type'] === E_ERROR) { |
|
| 171 | - EE_Error::error_handler(E_ERROR, $last_error['message'], $last_error['file'], $last_error['line']); |
|
| 172 | - } |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - |
|
| 176 | - |
|
| 177 | - /** |
|
| 178 | - * _format_error |
|
| 179 | - * |
|
| 180 | - * @param $code |
|
| 181 | - * @param $message |
|
| 182 | - * @param $file |
|
| 183 | - * @param $line |
|
| 184 | - * @return string |
|
| 185 | - */ |
|
| 186 | - private static function _format_error($code, $message, $file, $line) |
|
| 187 | - { |
|
| 188 | - $html = "<table cellpadding='5'><thead bgcolor='#f8f8f8'><th>Item</th><th align='left'>Details</th></thead><tbody>"; |
|
| 189 | - $html .= "<tr valign='top'><td><b>Code</b></td><td>$code</td></tr>"; |
|
| 190 | - $html .= "<tr valign='top'><td><b>Error</b></td><td>$message</td></tr>"; |
|
| 191 | - $html .= "<tr valign='top'><td><b>File</b></td><td>$file</td></tr>"; |
|
| 192 | - $html .= "<tr valign='top'><td><b>Line</b></td><td>$line</td></tr>"; |
|
| 193 | - $html .= '</tbody></table>'; |
|
| 194 | - return $html; |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - |
|
| 198 | - |
|
| 199 | - /** |
|
| 200 | - * set_content_type |
|
| 201 | - * |
|
| 202 | - * @param $content_type |
|
| 203 | - * @return string |
|
| 204 | - */ |
|
| 205 | - public static function set_content_type($content_type) |
|
| 206 | - { |
|
| 207 | - return 'text/html'; |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - |
|
| 211 | - |
|
| 212 | - /** |
|
| 213 | - * @return void |
|
| 214 | - * @throws EE_Error |
|
| 215 | - * @throws ReflectionException |
|
| 216 | - */ |
|
| 217 | - public function get_error() |
|
| 218 | - { |
|
| 219 | - if (apply_filters('FHEE__EE_Error__get_error__show_normal_exceptions', false)) { |
|
| 220 | - throw $this; |
|
| 221 | - } |
|
| 222 | - // get separate user and developer messages if they exist |
|
| 223 | - $msg = explode('||', $this->getMessage()); |
|
| 224 | - $user_msg = $msg[0]; |
|
| 225 | - $dev_msg = isset($msg[1]) ? $msg[1] : $msg[0]; |
|
| 226 | - $msg = WP_DEBUG ? $dev_msg : $user_msg; |
|
| 227 | - // add details to _all_exceptions array |
|
| 228 | - $x_time = time(); |
|
| 229 | - self::$_all_exceptions[$x_time]['name'] = get_class($this); |
|
| 230 | - self::$_all_exceptions[$x_time]['file'] = $this->getFile(); |
|
| 231 | - self::$_all_exceptions[$x_time]['line'] = $this->getLine(); |
|
| 232 | - self::$_all_exceptions[$x_time]['msg'] = $msg; |
|
| 233 | - self::$_all_exceptions[$x_time]['code'] = $this->getCode(); |
|
| 234 | - self::$_all_exceptions[$x_time]['trace'] = $this->getTrace(); |
|
| 235 | - self::$_all_exceptions[$x_time]['string'] = $this->getTraceAsString(); |
|
| 236 | - self::$_error_count++; |
|
| 237 | - //add_action( 'shutdown', array( $this, 'display_errors' )); |
|
| 238 | - $this->display_errors(); |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - |
|
| 242 | - |
|
| 243 | - /** |
|
| 244 | - * has_error |
|
| 245 | - * |
|
| 246 | - * @param bool $check_stored |
|
| 247 | - * @param string $type_to_check |
|
| 248 | - * @return bool |
|
| 249 | - */ |
|
| 250 | - public static function has_error($check_stored = false, $type_to_check = 'errors') |
|
| 251 | - { |
|
| 252 | - $has_error = isset(self::$_espresso_notices[$type_to_check]) |
|
| 253 | - && ! empty(self::$_espresso_notices[$type_to_check]) |
|
| 254 | - ? true |
|
| 255 | - : false; |
|
| 256 | - if ($check_stored && ! $has_error) { |
|
| 257 | - $notices = (array)get_option('ee_notices', array()); |
|
| 258 | - foreach ($notices as $type => $notice) { |
|
| 259 | - if ($type === $type_to_check && $notice) { |
|
| 260 | - return true; |
|
| 261 | - } |
|
| 262 | - } |
|
| 263 | - } |
|
| 264 | - return $has_error; |
|
| 265 | - } |
|
| 266 | - |
|
| 267 | - |
|
| 268 | - |
|
| 269 | - /** |
|
| 270 | - * display_errors |
|
| 271 | - * |
|
| 272 | - * @echo string |
|
| 273 | - * @throws \ReflectionException |
|
| 274 | - */ |
|
| 275 | - public function display_errors() |
|
| 276 | - { |
|
| 277 | - $trace_details = ''; |
|
| 278 | - $output = ' |
|
| 26 | + /** |
|
| 27 | + * name of the file to log exceptions to |
|
| 28 | + * |
|
| 29 | + * @var string |
|
| 30 | + */ |
|
| 31 | + private static $_exception_log_file = 'espresso_error_log.txt'; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * stores details for all exception |
|
| 35 | + * |
|
| 36 | + * @var array |
|
| 37 | + */ |
|
| 38 | + private static $_all_exceptions = array(); |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * tracks number of errors |
|
| 42 | + * |
|
| 43 | + * @var int |
|
| 44 | + */ |
|
| 45 | + private static $_error_count = 0; |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * has shutdown action been added ? |
|
| 49 | + * |
|
| 50 | + * @var array $_espresso_notices |
|
| 51 | + */ |
|
| 52 | + private static $_espresso_notices = array('success' => false, 'errors' => false, 'attention' => false); |
|
| 53 | + |
|
| 54 | + |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * @override default exception handling |
|
| 58 | + * @param string $message |
|
| 59 | + * @param int $code |
|
| 60 | + * @param Exception|null $previous |
|
| 61 | + */ |
|
| 62 | + public function __construct($message, $code = 0, Exception $previous = null) |
|
| 63 | + { |
|
| 64 | + if (version_compare(PHP_VERSION, '5.3.0', '<')) { |
|
| 65 | + parent::__construct($message, $code); |
|
| 66 | + } else { |
|
| 67 | + parent::__construct($message, $code, $previous); |
|
| 68 | + } |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * error_handler |
|
| 75 | + * |
|
| 76 | + * @param $code |
|
| 77 | + * @param $message |
|
| 78 | + * @param $file |
|
| 79 | + * @param $line |
|
| 80 | + * @return void |
|
| 81 | + */ |
|
| 82 | + public static function error_handler($code, $message, $file, $line) |
|
| 83 | + { |
|
| 84 | + $type = EE_Error::error_type($code); |
|
| 85 | + $site = site_url(); |
|
| 86 | + switch ($site) { |
|
| 87 | + case 'http://ee4.eventespresso.com/' : |
|
| 88 | + case 'http://ee4decaf.eventespresso.com/' : |
|
| 89 | + case 'http://ee4hf.eventespresso.com/' : |
|
| 90 | + case 'http://ee4a.eventespresso.com/' : |
|
| 91 | + case 'http://ee4ad.eventespresso.com/' : |
|
| 92 | + case 'http://ee4b.eventespresso.com/' : |
|
| 93 | + case 'http://ee4bd.eventespresso.com/' : |
|
| 94 | + case 'http://ee4d.eventespresso.com/' : |
|
| 95 | + case 'http://ee4dd.eventespresso.com/' : |
|
| 96 | + $to = '[email protected]'; |
|
| 97 | + break; |
|
| 98 | + default : |
|
| 99 | + $to = get_option('admin_email'); |
|
| 100 | + } |
|
| 101 | + $subject = $type . ' ' . $message . ' in ' . EVENT_ESPRESSO_VERSION . ' on ' . site_url(); |
|
| 102 | + $msg = EE_Error::_format_error($type, $message, $file, $line); |
|
| 103 | + if (function_exists('wp_mail')) { |
|
| 104 | + add_filter('wp_mail_content_type', array('EE_Error', 'set_content_type')); |
|
| 105 | + wp_mail($to, $subject, $msg); |
|
| 106 | + } |
|
| 107 | + echo '<div id="message" class="espresso-notices error"><p>'; |
|
| 108 | + echo $type . ': ' . $message . '<br />' . $file . ' line ' . $line; |
|
| 109 | + echo '<br /></p></div>'; |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * error_type |
|
| 116 | + * http://www.php.net/manual/en/errorfunc.constants.php#109430 |
|
| 117 | + * |
|
| 118 | + * @param $code |
|
| 119 | + * @return string |
|
| 120 | + */ |
|
| 121 | + public static function error_type($code) |
|
| 122 | + { |
|
| 123 | + switch ($code) { |
|
| 124 | + case E_ERROR: // 1 // |
|
| 125 | + return 'E_ERROR'; |
|
| 126 | + case E_WARNING: // 2 // |
|
| 127 | + return 'E_WARNING'; |
|
| 128 | + case E_PARSE: // 4 // |
|
| 129 | + return 'E_PARSE'; |
|
| 130 | + case E_NOTICE: // 8 // |
|
| 131 | + return 'E_NOTICE'; |
|
| 132 | + case E_CORE_ERROR: // 16 // |
|
| 133 | + return 'E_CORE_ERROR'; |
|
| 134 | + case E_CORE_WARNING: // 32 // |
|
| 135 | + return 'E_CORE_WARNING'; |
|
| 136 | + case E_COMPILE_ERROR: // 64 // |
|
| 137 | + return 'E_COMPILE_ERROR'; |
|
| 138 | + case E_COMPILE_WARNING: // 128 // |
|
| 139 | + return 'E_COMPILE_WARNING'; |
|
| 140 | + case E_USER_ERROR: // 256 // |
|
| 141 | + return 'E_USER_ERROR'; |
|
| 142 | + case E_USER_WARNING: // 512 // |
|
| 143 | + return 'E_USER_WARNING'; |
|
| 144 | + case E_USER_NOTICE: // 1024 // |
|
| 145 | + return 'E_USER_NOTICE'; |
|
| 146 | + case E_STRICT: // 2048 // |
|
| 147 | + return 'E_STRICT'; |
|
| 148 | + case E_RECOVERABLE_ERROR: // 4096 // |
|
| 149 | + return 'E_RECOVERABLE_ERROR'; |
|
| 150 | + case E_DEPRECATED: // 8192 // |
|
| 151 | + return 'E_DEPRECATED'; |
|
| 152 | + case E_USER_DEPRECATED: // 16384 // |
|
| 153 | + return 'E_USER_DEPRECATED'; |
|
| 154 | + case E_ALL: // 16384 // |
|
| 155 | + return 'E_ALL'; |
|
| 156 | + } |
|
| 157 | + return ''; |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * fatal_error_handler |
|
| 164 | + * |
|
| 165 | + * @return void |
|
| 166 | + */ |
|
| 167 | + public static function fatal_error_handler() |
|
| 168 | + { |
|
| 169 | + $last_error = error_get_last(); |
|
| 170 | + if ($last_error['type'] === E_ERROR) { |
|
| 171 | + EE_Error::error_handler(E_ERROR, $last_error['message'], $last_error['file'], $last_error['line']); |
|
| 172 | + } |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + |
|
| 176 | + |
|
| 177 | + /** |
|
| 178 | + * _format_error |
|
| 179 | + * |
|
| 180 | + * @param $code |
|
| 181 | + * @param $message |
|
| 182 | + * @param $file |
|
| 183 | + * @param $line |
|
| 184 | + * @return string |
|
| 185 | + */ |
|
| 186 | + private static function _format_error($code, $message, $file, $line) |
|
| 187 | + { |
|
| 188 | + $html = "<table cellpadding='5'><thead bgcolor='#f8f8f8'><th>Item</th><th align='left'>Details</th></thead><tbody>"; |
|
| 189 | + $html .= "<tr valign='top'><td><b>Code</b></td><td>$code</td></tr>"; |
|
| 190 | + $html .= "<tr valign='top'><td><b>Error</b></td><td>$message</td></tr>"; |
|
| 191 | + $html .= "<tr valign='top'><td><b>File</b></td><td>$file</td></tr>"; |
|
| 192 | + $html .= "<tr valign='top'><td><b>Line</b></td><td>$line</td></tr>"; |
|
| 193 | + $html .= '</tbody></table>'; |
|
| 194 | + return $html; |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + |
|
| 198 | + |
|
| 199 | + /** |
|
| 200 | + * set_content_type |
|
| 201 | + * |
|
| 202 | + * @param $content_type |
|
| 203 | + * @return string |
|
| 204 | + */ |
|
| 205 | + public static function set_content_type($content_type) |
|
| 206 | + { |
|
| 207 | + return 'text/html'; |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + |
|
| 211 | + |
|
| 212 | + /** |
|
| 213 | + * @return void |
|
| 214 | + * @throws EE_Error |
|
| 215 | + * @throws ReflectionException |
|
| 216 | + */ |
|
| 217 | + public function get_error() |
|
| 218 | + { |
|
| 219 | + if (apply_filters('FHEE__EE_Error__get_error__show_normal_exceptions', false)) { |
|
| 220 | + throw $this; |
|
| 221 | + } |
|
| 222 | + // get separate user and developer messages if they exist |
|
| 223 | + $msg = explode('||', $this->getMessage()); |
|
| 224 | + $user_msg = $msg[0]; |
|
| 225 | + $dev_msg = isset($msg[1]) ? $msg[1] : $msg[0]; |
|
| 226 | + $msg = WP_DEBUG ? $dev_msg : $user_msg; |
|
| 227 | + // add details to _all_exceptions array |
|
| 228 | + $x_time = time(); |
|
| 229 | + self::$_all_exceptions[$x_time]['name'] = get_class($this); |
|
| 230 | + self::$_all_exceptions[$x_time]['file'] = $this->getFile(); |
|
| 231 | + self::$_all_exceptions[$x_time]['line'] = $this->getLine(); |
|
| 232 | + self::$_all_exceptions[$x_time]['msg'] = $msg; |
|
| 233 | + self::$_all_exceptions[$x_time]['code'] = $this->getCode(); |
|
| 234 | + self::$_all_exceptions[$x_time]['trace'] = $this->getTrace(); |
|
| 235 | + self::$_all_exceptions[$x_time]['string'] = $this->getTraceAsString(); |
|
| 236 | + self::$_error_count++; |
|
| 237 | + //add_action( 'shutdown', array( $this, 'display_errors' )); |
|
| 238 | + $this->display_errors(); |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + |
|
| 242 | + |
|
| 243 | + /** |
|
| 244 | + * has_error |
|
| 245 | + * |
|
| 246 | + * @param bool $check_stored |
|
| 247 | + * @param string $type_to_check |
|
| 248 | + * @return bool |
|
| 249 | + */ |
|
| 250 | + public static function has_error($check_stored = false, $type_to_check = 'errors') |
|
| 251 | + { |
|
| 252 | + $has_error = isset(self::$_espresso_notices[$type_to_check]) |
|
| 253 | + && ! empty(self::$_espresso_notices[$type_to_check]) |
|
| 254 | + ? true |
|
| 255 | + : false; |
|
| 256 | + if ($check_stored && ! $has_error) { |
|
| 257 | + $notices = (array)get_option('ee_notices', array()); |
|
| 258 | + foreach ($notices as $type => $notice) { |
|
| 259 | + if ($type === $type_to_check && $notice) { |
|
| 260 | + return true; |
|
| 261 | + } |
|
| 262 | + } |
|
| 263 | + } |
|
| 264 | + return $has_error; |
|
| 265 | + } |
|
| 266 | + |
|
| 267 | + |
|
| 268 | + |
|
| 269 | + /** |
|
| 270 | + * display_errors |
|
| 271 | + * |
|
| 272 | + * @echo string |
|
| 273 | + * @throws \ReflectionException |
|
| 274 | + */ |
|
| 275 | + public function display_errors() |
|
| 276 | + { |
|
| 277 | + $trace_details = ''; |
|
| 278 | + $output = ' |
|
| 279 | 279 | <style type="text/css"> |
| 280 | 280 | #ee-error-message { |
| 281 | 281 | max-width:90% !important; |
@@ -331,19 +331,19 @@ discard block |
||
| 331 | 331 | } |
| 332 | 332 | </style> |
| 333 | 333 | <div id="ee-error-message" class="error">'; |
| 334 | - if (! WP_DEBUG) { |
|
| 335 | - $output .= ' |
|
| 334 | + if (! WP_DEBUG) { |
|
| 335 | + $output .= ' |
|
| 336 | 336 | <p>'; |
| 337 | - } |
|
| 338 | - // cycle thru errors |
|
| 339 | - foreach (self::$_all_exceptions as $time => $ex) { |
|
| 340 | - $error_code = ''; |
|
| 341 | - // process trace info |
|
| 342 | - if (empty($ex['trace'])) { |
|
| 343 | - $trace_details .= __('Sorry, but no trace information was available for this exception.', |
|
| 344 | - 'event_espresso'); |
|
| 345 | - } else { |
|
| 346 | - $trace_details .= ' |
|
| 337 | + } |
|
| 338 | + // cycle thru errors |
|
| 339 | + foreach (self::$_all_exceptions as $time => $ex) { |
|
| 340 | + $error_code = ''; |
|
| 341 | + // process trace info |
|
| 342 | + if (empty($ex['trace'])) { |
|
| 343 | + $trace_details .= __('Sorry, but no trace information was available for this exception.', |
|
| 344 | + 'event_espresso'); |
|
| 345 | + } else { |
|
| 346 | + $trace_details .= ' |
|
| 347 | 347 | <div id="ee-trace-details"> |
| 348 | 348 | <table width="100%" border="0" cellpadding="5" cellspacing="0"> |
| 349 | 349 | <tr> |
@@ -353,43 +353,43 @@ discard block |
||
| 353 | 353 | <th scope="col" align="left">Class</th> |
| 354 | 354 | <th scope="col" align="left">Method( arguments )</th> |
| 355 | 355 | </tr>'; |
| 356 | - $last_on_stack = count($ex['trace']) - 1; |
|
| 357 | - // reverse array so that stack is in proper chronological order |
|
| 358 | - $sorted_trace = array_reverse($ex['trace']); |
|
| 359 | - foreach ($sorted_trace as $nmbr => $trace) { |
|
| 360 | - $file = isset($trace['file']) ? $trace['file'] : ''; |
|
| 361 | - $class = isset($trace['class']) ? $trace['class'] : ''; |
|
| 362 | - $type = isset($trace['type']) ? $trace['type'] : ''; |
|
| 363 | - $function = isset($trace['function']) ? $trace['function'] : ''; |
|
| 364 | - $args = isset($trace['args']) ? $this->_convert_args_to_string($trace['args']) : ''; |
|
| 365 | - $line = isset($trace['line']) ? $trace['line'] : ''; |
|
| 366 | - $zebra = ($nmbr % 2) ? ' odd' : ''; |
|
| 367 | - if (empty($file) && ! empty($class)) { |
|
| 368 | - $a = new ReflectionClass($class); |
|
| 369 | - $file = $a->getFileName(); |
|
| 370 | - if (empty($line) && ! empty($function)) { |
|
| 371 | - try { |
|
| 372 | - //if $function is a closure, this throws an exception |
|
| 373 | - $b = new ReflectionMethod($class, $function); |
|
| 374 | - $line = $b->getStartLine(); |
|
| 375 | - }catch(Exception $closure_exception){ |
|
| 376 | - $line = 'unknown'; |
|
| 377 | - } |
|
| 378 | - } |
|
| 379 | - } |
|
| 380 | - if ($nmbr === $last_on_stack) { |
|
| 381 | - $file = $ex['file'] !== '' ? $ex['file'] : $file; |
|
| 382 | - $line = $ex['line'] !== '' ? $ex['line'] : $line; |
|
| 383 | - $error_code = self::generate_error_code($file, $trace['function'], $line); |
|
| 384 | - } |
|
| 385 | - $nmbr_dsply = ! empty($nmbr) ? $nmbr : ' '; |
|
| 386 | - $line_dsply = ! empty($line) ? $line : ' '; |
|
| 387 | - $file_dsply = ! empty($file) ? $file : ' '; |
|
| 388 | - $class_dsply = ! empty($class) ? $class : ' '; |
|
| 389 | - $type_dsply = ! empty($type) ? $type : ' '; |
|
| 390 | - $function_dsply = ! empty($function) ? $function : ' '; |
|
| 391 | - $args_dsply = ! empty($args) ? '( ' . $args . ' )' : ''; |
|
| 392 | - $trace_details .= ' |
|
| 356 | + $last_on_stack = count($ex['trace']) - 1; |
|
| 357 | + // reverse array so that stack is in proper chronological order |
|
| 358 | + $sorted_trace = array_reverse($ex['trace']); |
|
| 359 | + foreach ($sorted_trace as $nmbr => $trace) { |
|
| 360 | + $file = isset($trace['file']) ? $trace['file'] : ''; |
|
| 361 | + $class = isset($trace['class']) ? $trace['class'] : ''; |
|
| 362 | + $type = isset($trace['type']) ? $trace['type'] : ''; |
|
| 363 | + $function = isset($trace['function']) ? $trace['function'] : ''; |
|
| 364 | + $args = isset($trace['args']) ? $this->_convert_args_to_string($trace['args']) : ''; |
|
| 365 | + $line = isset($trace['line']) ? $trace['line'] : ''; |
|
| 366 | + $zebra = ($nmbr % 2) ? ' odd' : ''; |
|
| 367 | + if (empty($file) && ! empty($class)) { |
|
| 368 | + $a = new ReflectionClass($class); |
|
| 369 | + $file = $a->getFileName(); |
|
| 370 | + if (empty($line) && ! empty($function)) { |
|
| 371 | + try { |
|
| 372 | + //if $function is a closure, this throws an exception |
|
| 373 | + $b = new ReflectionMethod($class, $function); |
|
| 374 | + $line = $b->getStartLine(); |
|
| 375 | + }catch(Exception $closure_exception){ |
|
| 376 | + $line = 'unknown'; |
|
| 377 | + } |
|
| 378 | + } |
|
| 379 | + } |
|
| 380 | + if ($nmbr === $last_on_stack) { |
|
| 381 | + $file = $ex['file'] !== '' ? $ex['file'] : $file; |
|
| 382 | + $line = $ex['line'] !== '' ? $ex['line'] : $line; |
|
| 383 | + $error_code = self::generate_error_code($file, $trace['function'], $line); |
|
| 384 | + } |
|
| 385 | + $nmbr_dsply = ! empty($nmbr) ? $nmbr : ' '; |
|
| 386 | + $line_dsply = ! empty($line) ? $line : ' '; |
|
| 387 | + $file_dsply = ! empty($file) ? $file : ' '; |
|
| 388 | + $class_dsply = ! empty($class) ? $class : ' '; |
|
| 389 | + $type_dsply = ! empty($type) ? $type : ' '; |
|
| 390 | + $function_dsply = ! empty($function) ? $function : ' '; |
|
| 391 | + $args_dsply = ! empty($args) ? '( ' . $args . ' )' : ''; |
|
| 392 | + $trace_details .= ' |
|
| 393 | 393 | <tr> |
| 394 | 394 | <td align="right" class="' . $zebra . '">' . $nmbr_dsply . '</td> |
| 395 | 395 | <td align="right" class="' . $zebra . '">' . $line_dsply . '</td> |
@@ -397,669 +397,669 @@ discard block |
||
| 397 | 397 | <td align="left" class="' . $zebra . '">' . $class_dsply . '</td> |
| 398 | 398 | <td align="left" class="' . $zebra . '">' . $type_dsply . $function_dsply . $args_dsply . '</td> |
| 399 | 399 | </tr>'; |
| 400 | - } |
|
| 401 | - $trace_details .= ' |
|
| 400 | + } |
|
| 401 | + $trace_details .= ' |
|
| 402 | 402 | </table> |
| 403 | 403 | </div>'; |
| 404 | - } |
|
| 405 | - $ex['code'] = $ex['code'] ? $ex['code'] : $error_code; |
|
| 406 | - // add generic non-identifying messages for non-privileged users |
|
| 407 | - if (! WP_DEBUG) { |
|
| 408 | - $output .= '<span class="ee-error-user-msg-spn">' |
|
| 409 | - . trim($ex['msg']) |
|
| 410 | - . '</span> <sup>' |
|
| 411 | - . $ex['code'] |
|
| 412 | - . '</sup><br />'; |
|
| 413 | - } else { |
|
| 414 | - // or helpful developer messages if debugging is on |
|
| 415 | - $output .= ' |
|
| 404 | + } |
|
| 405 | + $ex['code'] = $ex['code'] ? $ex['code'] : $error_code; |
|
| 406 | + // add generic non-identifying messages for non-privileged users |
|
| 407 | + if (! WP_DEBUG) { |
|
| 408 | + $output .= '<span class="ee-error-user-msg-spn">' |
|
| 409 | + . trim($ex['msg']) |
|
| 410 | + . '</span> <sup>' |
|
| 411 | + . $ex['code'] |
|
| 412 | + . '</sup><br />'; |
|
| 413 | + } else { |
|
| 414 | + // or helpful developer messages if debugging is on |
|
| 415 | + $output .= ' |
|
| 416 | 416 | <div class="ee-error-dev-msg-dv"> |
| 417 | 417 | <p class="ee-error-dev-msg-pg"> |
| 418 | 418 | <strong class="ee-error-dev-msg-str">An ' |
| 419 | - . $ex['name'] |
|
| 420 | - . ' exception was thrown!</strong> <span>code: ' |
|
| 421 | - . $ex['code'] |
|
| 422 | - . '</span><br /> |
|
| 419 | + . $ex['name'] |
|
| 420 | + . ' exception was thrown!</strong> <span>code: ' |
|
| 421 | + . $ex['code'] |
|
| 422 | + . '</span><br /> |
|
| 423 | 423 | <span class="big-text">"' |
| 424 | - . trim($ex['msg']) |
|
| 425 | - . '"</span><br/> |
|
| 424 | + . trim($ex['msg']) |
|
| 425 | + . '"</span><br/> |
|
| 426 | 426 | <a id="display-ee-error-trace-' |
| 427 | - . self::$_error_count |
|
| 428 | - . $time |
|
| 429 | - . '" class="display-ee-error-trace-lnk small-text" rel="ee-error-trace-' |
|
| 430 | - . self::$_error_count |
|
| 431 | - . $time |
|
| 432 | - . '"> |
|
| 427 | + . self::$_error_count |
|
| 428 | + . $time |
|
| 429 | + . '" class="display-ee-error-trace-lnk small-text" rel="ee-error-trace-' |
|
| 430 | + . self::$_error_count |
|
| 431 | + . $time |
|
| 432 | + . '"> |
|
| 433 | 433 | ' |
| 434 | - . __('click to view backtrace and class/method details', 'event_espresso') |
|
| 435 | - . ' |
|
| 434 | + . __('click to view backtrace and class/method details', 'event_espresso') |
|
| 435 | + . ' |
|
| 436 | 436 | </a><br /> |
| 437 | 437 | <span class="small-text lt-grey-text">' |
| 438 | - . $ex['file'] |
|
| 439 | - . ' ( line no: ' |
|
| 440 | - . $ex['line'] |
|
| 441 | - . ' )</span> |
|
| 438 | + . $ex['file'] |
|
| 439 | + . ' ( line no: ' |
|
| 440 | + . $ex['line'] |
|
| 441 | + . ' )</span> |
|
| 442 | 442 | </p> |
| 443 | 443 | <div id="ee-error-trace-' |
| 444 | - . self::$_error_count |
|
| 445 | - . $time |
|
| 446 | - . '-dv" class="ee-error-trace-dv" style="display: none;"> |
|
| 444 | + . self::$_error_count |
|
| 445 | + . $time |
|
| 446 | + . '-dv" class="ee-error-trace-dv" style="display: none;"> |
|
| 447 | 447 | ' |
| 448 | - . $trace_details; |
|
| 449 | - if (! empty($class)) { |
|
| 450 | - $output .= ' |
|
| 448 | + . $trace_details; |
|
| 449 | + if (! empty($class)) { |
|
| 450 | + $output .= ' |
|
| 451 | 451 | <div style="padding:3px; margin:0 0 1em; border:1px solid #666; background:#fff; border-radius:3px;"> |
| 452 | 452 | <div style="padding:1em 2em; border:1px solid #666; background:#f9f9f9;"> |
| 453 | 453 | <h3>Class Details</h3>'; |
| 454 | - $a = new ReflectionClass($class); |
|
| 455 | - $output .= ' |
|
| 454 | + $a = new ReflectionClass($class); |
|
| 455 | + $output .= ' |
|
| 456 | 456 | <pre>' . $a . '</pre> |
| 457 | 457 | </div> |
| 458 | 458 | </div>'; |
| 459 | - } |
|
| 460 | - $output .= ' |
|
| 459 | + } |
|
| 460 | + $output .= ' |
|
| 461 | 461 | </div> |
| 462 | 462 | </div> |
| 463 | 463 | <br />'; |
| 464 | - } |
|
| 465 | - $this->write_to_error_log($time, $ex); |
|
| 466 | - } |
|
| 467 | - // remove last linebreak |
|
| 468 | - $output = substr($output, 0, -6); |
|
| 469 | - if (! WP_DEBUG) { |
|
| 470 | - $output .= ' |
|
| 464 | + } |
|
| 465 | + $this->write_to_error_log($time, $ex); |
|
| 466 | + } |
|
| 467 | + // remove last linebreak |
|
| 468 | + $output = substr($output, 0, -6); |
|
| 469 | + if (! WP_DEBUG) { |
|
| 470 | + $output .= ' |
|
| 471 | 471 | </p>'; |
| 472 | - } |
|
| 473 | - $output .= ' |
|
| 472 | + } |
|
| 473 | + $output .= ' |
|
| 474 | 474 | </div>'; |
| 475 | - $output .= self::_print_scripts(true); |
|
| 476 | - if (defined('DOING_AJAX')) { |
|
| 477 | - echo wp_json_encode(array('error' => $output)); |
|
| 478 | - exit(); |
|
| 479 | - } |
|
| 480 | - echo $output; |
|
| 481 | - die(); |
|
| 482 | - } |
|
| 483 | - |
|
| 484 | - |
|
| 485 | - |
|
| 486 | - /** |
|
| 487 | - * generate string from exception trace args |
|
| 488 | - * |
|
| 489 | - * @param array $arguments |
|
| 490 | - * @param bool $array |
|
| 491 | - * @return string |
|
| 492 | - */ |
|
| 493 | - private function _convert_args_to_string($arguments = array(), $array = false) |
|
| 494 | - { |
|
| 495 | - $arg_string = ''; |
|
| 496 | - if (! empty($arguments)) { |
|
| 497 | - $args = array(); |
|
| 498 | - foreach ($arguments as $arg) { |
|
| 499 | - if (! empty($arg)) { |
|
| 500 | - if (is_string($arg)) { |
|
| 501 | - $args[] = " '" . $arg . "'"; |
|
| 502 | - } elseif (is_array($arg)) { |
|
| 503 | - $args[] = 'ARRAY(' . $this->_convert_args_to_string($arg, true); |
|
| 504 | - } elseif ($arg === null) { |
|
| 505 | - $args[] = ' NULL'; |
|
| 506 | - } elseif (is_bool($arg)) { |
|
| 507 | - $args[] = ($arg) ? ' TRUE' : ' FALSE'; |
|
| 508 | - } elseif (is_object($arg)) { |
|
| 509 | - $args[] = ' OBJECT ' . get_class($arg); |
|
| 510 | - } elseif (is_resource($arg)) { |
|
| 511 | - $args[] = get_resource_type($arg); |
|
| 512 | - } else { |
|
| 513 | - $args[] = $arg; |
|
| 514 | - } |
|
| 515 | - } |
|
| 516 | - } |
|
| 517 | - $arg_string = implode(', ', $args); |
|
| 518 | - } |
|
| 519 | - if ($array) { |
|
| 520 | - $arg_string .= ' )'; |
|
| 521 | - } |
|
| 522 | - return $arg_string; |
|
| 523 | - } |
|
| 524 | - |
|
| 525 | - |
|
| 526 | - |
|
| 527 | - /** |
|
| 528 | - * add error message |
|
| 529 | - * |
|
| 530 | - * @param string $msg the message to display to users or developers - adding a double pipe || (OR) creates |
|
| 531 | - * separate messages for user || dev |
|
| 532 | - * @param string $file the file that the error occurred in - just use __FILE__ |
|
| 533 | - * @param string $func the function/method that the error occurred in - just use __FUNCTION__ |
|
| 534 | - * @param string $line the line number where the error occurred - just use __LINE__ |
|
| 535 | - * @return void |
|
| 536 | - */ |
|
| 537 | - public static function add_error($msg = null, $file = null, $func = null, $line = null) |
|
| 538 | - { |
|
| 539 | - self::_add_notice('errors', $msg, $file, $func, $line); |
|
| 540 | - self::$_error_count++; |
|
| 541 | - } |
|
| 542 | - |
|
| 543 | - |
|
| 544 | - |
|
| 545 | - /** |
|
| 546 | - * If WP_DEBUG is active, throws an exception. If WP_DEBUG is off, just |
|
| 547 | - * adds an error |
|
| 548 | - * |
|
| 549 | - * @param string $msg |
|
| 550 | - * @param string $file |
|
| 551 | - * @param string $func |
|
| 552 | - * @param string $line |
|
| 553 | - * @throws EE_Error |
|
| 554 | - */ |
|
| 555 | - public static function throw_exception_if_debugging($msg = null, $file = null, $func = null, $line = null) |
|
| 556 | - { |
|
| 557 | - if (WP_DEBUG) { |
|
| 558 | - throw new EE_Error($msg); |
|
| 559 | - } |
|
| 560 | - EE_Error::add_error($msg, $file, $func, $line); |
|
| 561 | - } |
|
| 562 | - |
|
| 563 | - |
|
| 564 | - |
|
| 565 | - /** |
|
| 566 | - * add success message |
|
| 567 | - * |
|
| 568 | - * @param string $msg the message to display to users or developers - adding a double pipe || (OR) creates |
|
| 569 | - * separate messages for user || dev |
|
| 570 | - * @param string $file the file that the error occurred in - just use __FILE__ |
|
| 571 | - * @param string $func the function/method that the error occurred in - just use __FUNCTION__ |
|
| 572 | - * @param string $line the line number where the error occurred - just use __LINE__ |
|
| 573 | - * @return void |
|
| 574 | - */ |
|
| 575 | - public static function add_success($msg = null, $file = null, $func = null, $line = null) |
|
| 576 | - { |
|
| 577 | - self::_add_notice('success', $msg, $file, $func, $line); |
|
| 578 | - } |
|
| 579 | - |
|
| 580 | - |
|
| 581 | - |
|
| 582 | - /** |
|
| 583 | - * add attention message |
|
| 584 | - * |
|
| 585 | - * @param string $msg the message to display to users or developers - adding a double pipe || (OR) creates |
|
| 586 | - * separate messages for user || dev |
|
| 587 | - * @param string $file the file that the error occurred in - just use __FILE__ |
|
| 588 | - * @param string $func the function/method that the error occurred in - just use __FUNCTION__ |
|
| 589 | - * @param string $line the line number where the error occurred - just use __LINE__ |
|
| 590 | - * @return void |
|
| 591 | - */ |
|
| 592 | - public static function add_attention($msg = null, $file = null, $func = null, $line = null) |
|
| 593 | - { |
|
| 594 | - self::_add_notice('attention', $msg, $file, $func, $line); |
|
| 595 | - } |
|
| 596 | - |
|
| 597 | - |
|
| 598 | - |
|
| 599 | - /** |
|
| 600 | - * add success message |
|
| 601 | - * |
|
| 602 | - * @param string $type whether the message is for a success or error notification |
|
| 603 | - * @param string $msg the message to display to users or developers - adding a double pipe || (OR) creates |
|
| 604 | - * separate messages for user || dev |
|
| 605 | - * @param string $file the file that the error occurred in - just use __FILE__ |
|
| 606 | - * @param string $func the function/method that the error occurred in - just use __FUNCTION__ |
|
| 607 | - * @param string $line the line number where the error occurred - just use __LINE__ |
|
| 608 | - * @return void |
|
| 609 | - */ |
|
| 610 | - private static function _add_notice($type = 'success', $msg = null, $file = null, $func = null, $line = null) |
|
| 611 | - { |
|
| 612 | - if (empty($msg)) { |
|
| 613 | - EE_Error::doing_it_wrong( |
|
| 614 | - 'EE_Error::add_' . $type . '()', |
|
| 615 | - sprintf( |
|
| 616 | - __('Notifications are not much use without a message! Please add a message to the EE_Error::add_%s() call made in %s on line %d', |
|
| 617 | - 'event_espresso'), |
|
| 618 | - $type, |
|
| 619 | - $file, |
|
| 620 | - $line |
|
| 621 | - ), |
|
| 622 | - EVENT_ESPRESSO_VERSION |
|
| 623 | - ); |
|
| 624 | - } |
|
| 625 | - if ($type === 'errors' && (empty($file) || empty($func) || empty($line))) { |
|
| 626 | - EE_Error::doing_it_wrong( |
|
| 627 | - 'EE_Error::add_error()', |
|
| 628 | - __('You need to provide the file name, function name, and line number that the error occurred on in order to better assist with debugging.', |
|
| 629 | - 'event_espresso'), |
|
| 630 | - EVENT_ESPRESSO_VERSION |
|
| 631 | - ); |
|
| 632 | - } |
|
| 633 | - // get separate user and developer messages if they exist |
|
| 634 | - $msg = explode('||', $msg); |
|
| 635 | - $user_msg = $msg[0]; |
|
| 636 | - $dev_msg = isset($msg[1]) ? $msg[1] : $msg[0]; |
|
| 637 | - /** |
|
| 638 | - * Do an action so other code can be triggered when a notice is created |
|
| 639 | - * |
|
| 640 | - * @param string $type can be 'errors', 'attention', or 'success' |
|
| 641 | - * @param string $user_msg message displayed to user when WP_DEBUG is off |
|
| 642 | - * @param string $user_msg message displayed to user when WP_DEBUG is on |
|
| 643 | - * @param string $file file where error was generated |
|
| 644 | - * @param string $func function where error was generated |
|
| 645 | - * @param string $line line where error was generated |
|
| 646 | - */ |
|
| 647 | - do_action('AHEE__EE_Error___add_notice', $type, $user_msg, $dev_msg, $file, $func, $line); |
|
| 648 | - $msg = WP_DEBUG ? $dev_msg : $user_msg; |
|
| 649 | - // add notice if message exists |
|
| 650 | - if (! empty($msg)) { |
|
| 651 | - // get error code |
|
| 652 | - $notice_code = EE_Error::generate_error_code($file, $func, $line); |
|
| 653 | - if (WP_DEBUG && $type === 'errors') { |
|
| 654 | - $msg .= '<br/><span class="tiny-text">' . $notice_code . '</span>'; |
|
| 655 | - } |
|
| 656 | - // add notice. Index by code if it's not blank |
|
| 657 | - if ($notice_code) { |
|
| 658 | - self::$_espresso_notices[$type][$notice_code] = $msg; |
|
| 659 | - } else { |
|
| 660 | - self::$_espresso_notices[$type][] = $msg; |
|
| 661 | - } |
|
| 662 | - add_action('wp_footer', array('EE_Error', 'enqueue_error_scripts'), 1); |
|
| 663 | - } |
|
| 664 | - } |
|
| 665 | - |
|
| 666 | - |
|
| 667 | - |
|
| 668 | - /** |
|
| 669 | - * in some case it may be necessary to overwrite the existing success messages |
|
| 670 | - * |
|
| 671 | - * @return void |
|
| 672 | - */ |
|
| 673 | - public static function overwrite_success() |
|
| 674 | - { |
|
| 675 | - self::$_espresso_notices['success'] = false; |
|
| 676 | - } |
|
| 677 | - |
|
| 678 | - |
|
| 679 | - |
|
| 680 | - /** |
|
| 681 | - * in some case it may be necessary to overwrite the existing attention messages |
|
| 682 | - * |
|
| 683 | - * @return void |
|
| 684 | - */ |
|
| 685 | - public static function overwrite_attention() |
|
| 686 | - { |
|
| 687 | - self::$_espresso_notices['attention'] = false; |
|
| 688 | - } |
|
| 689 | - |
|
| 690 | - |
|
| 691 | - |
|
| 692 | - /** |
|
| 693 | - * in some case it may be necessary to overwrite the existing error messages |
|
| 694 | - * |
|
| 695 | - * @return void |
|
| 696 | - */ |
|
| 697 | - public static function overwrite_errors() |
|
| 698 | - { |
|
| 699 | - self::$_espresso_notices['errors'] = false; |
|
| 700 | - } |
|
| 701 | - |
|
| 702 | - |
|
| 703 | - |
|
| 704 | - /** |
|
| 705 | - * reset_notices |
|
| 706 | - * |
|
| 707 | - * @return void |
|
| 708 | - */ |
|
| 709 | - public static function reset_notices() |
|
| 710 | - { |
|
| 711 | - self::$_espresso_notices['success'] = false; |
|
| 712 | - self::$_espresso_notices['attention'] = false; |
|
| 713 | - self::$_espresso_notices['errors'] = false; |
|
| 714 | - } |
|
| 715 | - |
|
| 716 | - |
|
| 717 | - |
|
| 718 | - /** |
|
| 719 | - * has_errors |
|
| 720 | - * |
|
| 721 | - * @return int |
|
| 722 | - */ |
|
| 723 | - public static function has_notices() |
|
| 724 | - { |
|
| 725 | - $has_notices = 0; |
|
| 726 | - // check for success messages |
|
| 727 | - $has_notices = self::$_espresso_notices['success'] && ! empty(self::$_espresso_notices['success']) ? 3 |
|
| 728 | - : $has_notices; |
|
| 729 | - // check for attention messages |
|
| 730 | - $has_notices = self::$_espresso_notices['attention'] && ! empty(self::$_espresso_notices['attention']) ? 2 |
|
| 731 | - : $has_notices; |
|
| 732 | - // check for error messages |
|
| 733 | - $has_notices = self::$_espresso_notices['errors'] && ! empty(self::$_espresso_notices['errors']) ? 1 |
|
| 734 | - : $has_notices; |
|
| 735 | - return $has_notices; |
|
| 736 | - } |
|
| 737 | - |
|
| 738 | - |
|
| 739 | - |
|
| 740 | - /** |
|
| 741 | - * This simply returns non formatted error notices as they were sent into the EE_Error object. |
|
| 742 | - * |
|
| 743 | - * @since 4.9.0 |
|
| 744 | - * @return array |
|
| 745 | - */ |
|
| 746 | - public static function get_vanilla_notices() |
|
| 747 | - { |
|
| 748 | - return array( |
|
| 749 | - 'success' => isset(self::$_espresso_notices['success']) ? self::$_espresso_notices['success'] : array(), |
|
| 750 | - 'attention' => isset(self::$_espresso_notices['attention']) ? self::$_espresso_notices['attention'] |
|
| 751 | - : array(), |
|
| 752 | - 'errors' => isset(self::$_espresso_notices['errors']) ? self::$_espresso_notices['errors'] : array(), |
|
| 753 | - ); |
|
| 754 | - } |
|
| 755 | - |
|
| 756 | - |
|
| 757 | - |
|
| 758 | - /** |
|
| 759 | - * compile all error or success messages into one string |
|
| 760 | - * |
|
| 761 | - * @see EE_Error::get_raw_notices if you want the raw notices without any preparations made to them |
|
| 762 | - * @param boolean $format_output whether or not to format the messages for display in the WP admin |
|
| 763 | - * @param boolean $save_to_transient whether or not to save notices to the db for retrieval on next request |
|
| 764 | - * - ONLY do this just before redirecting |
|
| 765 | - * @param boolean $remove_empty whether or not to unset empty messages |
|
| 766 | - * @return array |
|
| 767 | - */ |
|
| 768 | - public static function get_notices($format_output = true, $save_to_transient = false, $remove_empty = true) |
|
| 769 | - { |
|
| 770 | - do_action('AHEE_log', __FILE__, __FUNCTION__, ''); |
|
| 771 | - $success_messages = ''; |
|
| 772 | - $attention_messages = ''; |
|
| 773 | - $error_messages = ''; |
|
| 774 | - $print_scripts = false; |
|
| 775 | - // either save notices to the db |
|
| 776 | - if ($save_to_transient) { |
|
| 777 | - update_option('ee_notices', self::$_espresso_notices); |
|
| 778 | - return array(); |
|
| 779 | - } |
|
| 780 | - // grab any notices that have been previously saved |
|
| 781 | - if ($notices = get_option('ee_notices', false)) { |
|
| 782 | - foreach ($notices as $type => $notice) { |
|
| 783 | - if (is_array($notice) && ! empty($notice)) { |
|
| 784 | - // make sure that existing notice type is an array |
|
| 785 | - self::$_espresso_notices[$type] = is_array(self::$_espresso_notices[$type]) |
|
| 786 | - && ! empty(self::$_espresso_notices[$type]) |
|
| 787 | - ? self::$_espresso_notices[$type] : array(); |
|
| 788 | - // merge stored notices with any newly created ones |
|
| 789 | - self::$_espresso_notices[$type] = array_merge(self::$_espresso_notices[$type], $notice); |
|
| 790 | - $print_scripts = true; |
|
| 791 | - } |
|
| 792 | - } |
|
| 793 | - // now clear any stored notices |
|
| 794 | - update_option('ee_notices', false); |
|
| 795 | - } |
|
| 796 | - // check for success messages |
|
| 797 | - if (self::$_espresso_notices['success'] && ! empty(self::$_espresso_notices['success'])) { |
|
| 798 | - // combine messages |
|
| 799 | - $success_messages .= implode(self::$_espresso_notices['success'], '<br /><br />'); |
|
| 800 | - $print_scripts = true; |
|
| 801 | - } |
|
| 802 | - // check for attention messages |
|
| 803 | - if (self::$_espresso_notices['attention'] && ! empty(self::$_espresso_notices['attention'])) { |
|
| 804 | - // combine messages |
|
| 805 | - $attention_messages .= implode(self::$_espresso_notices['attention'], '<br /><br />'); |
|
| 806 | - $print_scripts = true; |
|
| 807 | - } |
|
| 808 | - // check for error messages |
|
| 809 | - if (self::$_espresso_notices['errors'] && ! empty(self::$_espresso_notices['errors'])) { |
|
| 810 | - $error_messages .= count(self::$_espresso_notices['errors']) > 1 |
|
| 811 | - ? __('The following errors have occurred:<br />', 'event_espresso') |
|
| 812 | - : __('An error has occurred:<br />', 'event_espresso'); |
|
| 813 | - // combine messages |
|
| 814 | - $error_messages .= implode(self::$_espresso_notices['errors'], '<br /><br />'); |
|
| 815 | - $print_scripts = true; |
|
| 816 | - } |
|
| 817 | - if ($format_output) { |
|
| 818 | - $notices = '<div id="espresso-notices">'; |
|
| 819 | - $close = is_admin() ? '' |
|
| 820 | - : '<a class="close-espresso-notice hide-if-no-js"><span class="dashicons dashicons-no"></span></a>'; |
|
| 821 | - if ($success_messages !== '') { |
|
| 822 | - $css_id = is_admin() ? 'message' : 'espresso-notices-success'; |
|
| 823 | - $css_class = is_admin() ? 'updated fade' : 'success fade-away'; |
|
| 824 | - //showMessage( $success_messages ); |
|
| 825 | - $notices .= '<div id="' |
|
| 826 | - . $css_id |
|
| 827 | - . '" class="espresso-notices ' |
|
| 828 | - . $css_class |
|
| 829 | - . '" style="display:none;"><p>' |
|
| 830 | - . $success_messages |
|
| 831 | - . '</p>' |
|
| 832 | - . $close |
|
| 833 | - . '</div>'; |
|
| 834 | - } |
|
| 835 | - if ($attention_messages !== '') { |
|
| 836 | - $css_id = is_admin() ? 'message' : 'espresso-notices-attention'; |
|
| 837 | - $css_class = is_admin() ? 'updated ee-notices-attention' : 'attention fade-away'; |
|
| 838 | - //showMessage( $error_messages, TRUE ); |
|
| 839 | - $notices .= '<div id="' |
|
| 840 | - . $css_id |
|
| 841 | - . '" class="espresso-notices ' |
|
| 842 | - . $css_class |
|
| 843 | - . '" style="display:none;"><p>' |
|
| 844 | - . $attention_messages |
|
| 845 | - . '</p>' |
|
| 846 | - . $close |
|
| 847 | - . '</div>'; |
|
| 848 | - } |
|
| 849 | - if ($error_messages !== '') { |
|
| 850 | - $css_id = is_admin() ? 'message' : 'espresso-notices-error'; |
|
| 851 | - $css_class = is_admin() ? 'error' : 'error fade-away'; |
|
| 852 | - //showMessage( $error_messages, TRUE ); |
|
| 853 | - $notices .= '<div id="' |
|
| 854 | - . $css_id |
|
| 855 | - . '" class="espresso-notices ' |
|
| 856 | - . $css_class |
|
| 857 | - . '" style="display:none;"><p>' |
|
| 858 | - . $error_messages |
|
| 859 | - . '</p>' |
|
| 860 | - . $close |
|
| 861 | - . '</div>'; |
|
| 862 | - } |
|
| 863 | - $notices .= '</div>'; |
|
| 864 | - } else { |
|
| 865 | - $notices = array( |
|
| 866 | - 'success' => $success_messages, |
|
| 867 | - 'attention' => $attention_messages, |
|
| 868 | - 'errors' => $error_messages, |
|
| 869 | - ); |
|
| 870 | - if ($remove_empty) { |
|
| 871 | - // remove empty notices |
|
| 872 | - foreach ($notices as $type => $notice) { |
|
| 873 | - if (empty($notice)) { |
|
| 874 | - unset($notices[$type]); |
|
| 875 | - } |
|
| 876 | - } |
|
| 877 | - } |
|
| 878 | - } |
|
| 879 | - if ($print_scripts) { |
|
| 880 | - self::_print_scripts(); |
|
| 881 | - } |
|
| 882 | - return $notices; |
|
| 883 | - } |
|
| 884 | - |
|
| 885 | - |
|
| 886 | - |
|
| 887 | - /** |
|
| 888 | - * add_persistent_admin_notice |
|
| 889 | - * |
|
| 890 | - * @param string $pan_name the name, or key of the Persistent Admin Notice to be stored |
|
| 891 | - * @param string $pan_message the message to be stored persistently until dismissed |
|
| 892 | - * @param bool $force_update allows one to enforce the reappearance of a persistent message. |
|
| 893 | - * @return void |
|
| 894 | - */ |
|
| 895 | - public static function add_persistent_admin_notice($pan_name = '', $pan_message, $force_update = false) |
|
| 896 | - { |
|
| 897 | - if (! empty($pan_name) && ! empty($pan_message)) { |
|
| 898 | - $persistent_admin_notices = get_option('ee_pers_admin_notices', array()); |
|
| 899 | - //maybe initialize persistent_admin_notices |
|
| 900 | - if (empty($persistent_admin_notices)) { |
|
| 901 | - add_option('ee_pers_admin_notices', array(), '', 'no'); |
|
| 902 | - } |
|
| 903 | - $pan_name = sanitize_key($pan_name); |
|
| 904 | - if (! array_key_exists($pan_name, $persistent_admin_notices) || $force_update) { |
|
| 905 | - $persistent_admin_notices[$pan_name] = $pan_message; |
|
| 906 | - update_option('ee_pers_admin_notices', $persistent_admin_notices); |
|
| 907 | - } |
|
| 908 | - } |
|
| 909 | - } |
|
| 910 | - |
|
| 911 | - |
|
| 912 | - |
|
| 913 | - /** |
|
| 914 | - * dismiss_persistent_admin_notice |
|
| 915 | - * |
|
| 916 | - * @param string $pan_name the name, or key of the Persistent Admin Notice to be dismissed |
|
| 917 | - * @param bool $purge |
|
| 918 | - * @param bool $return_immediately |
|
| 919 | - * @return void |
|
| 920 | - */ |
|
| 921 | - public static function dismiss_persistent_admin_notice($pan_name = '', $purge = false, $return_immediately = false) |
|
| 922 | - { |
|
| 923 | - $pan_name = EE_Registry::instance()->REQ->is_set('ee_nag_notice') |
|
| 924 | - ? EE_Registry::instance()->REQ->get('ee_nag_notice') : $pan_name; |
|
| 925 | - if (! empty($pan_name)) { |
|
| 926 | - $persistent_admin_notices = get_option('ee_pers_admin_notices', array()); |
|
| 927 | - // check if notice we wish to dismiss is actually in the $persistent_admin_notices array |
|
| 928 | - if (is_array($persistent_admin_notices) && isset($persistent_admin_notices[$pan_name])) { |
|
| 929 | - // completely delete nag notice, or just NULL message so that it can NOT be added again ? |
|
| 930 | - if ($purge) { |
|
| 931 | - unset($persistent_admin_notices[$pan_name]); |
|
| 932 | - } else { |
|
| 933 | - $persistent_admin_notices[$pan_name] = null; |
|
| 934 | - } |
|
| 935 | - if (update_option('ee_pers_admin_notices', $persistent_admin_notices) === false) { |
|
| 936 | - EE_Error::add_error(sprintf(__('The persistent admin notice for "%s" could not be deleted.', |
|
| 937 | - 'event_espresso'), $pan_name), __FILE__, __FUNCTION__, __LINE__); |
|
| 938 | - } |
|
| 939 | - } |
|
| 940 | - } |
|
| 941 | - if ($return_immediately) { |
|
| 942 | - return; |
|
| 943 | - } |
|
| 944 | - if (EE_Registry::instance()->REQ->ajax) { |
|
| 945 | - // grab any notices and concatenate into string |
|
| 946 | - echo wp_json_encode(array('errors' => implode('<br />', EE_Error::get_notices(false)))); |
|
| 947 | - exit(); |
|
| 948 | - } |
|
| 949 | - // save errors to a transient to be displayed on next request (after redirect) |
|
| 950 | - EE_Error::get_notices(false, true); |
|
| 951 | - $return_url = EE_Registry::instance()->REQ->is_set('return_url') |
|
| 952 | - ? EE_Registry::instance()->REQ->get('return_url') : ''; |
|
| 953 | - wp_safe_redirect(urldecode($return_url)); |
|
| 954 | - } |
|
| 955 | - |
|
| 956 | - |
|
| 957 | - |
|
| 958 | - /** |
|
| 959 | - * display_persistent_admin_notices |
|
| 960 | - * |
|
| 961 | - * @param string $pan_name the name, or key of the Persistent Admin Notice to be stored |
|
| 962 | - * @param string $pan_message the message to be stored persistently until dismissed |
|
| 963 | - * @param string $return_url URL to go back to after nag notice is dismissed |
|
| 964 | - * @return string |
|
| 965 | - */ |
|
| 966 | - public static function display_persistent_admin_notices($pan_name = '', $pan_message = '', $return_url = '') |
|
| 967 | - { |
|
| 968 | - if (! empty($pan_name) && ! empty($pan_message)) { |
|
| 969 | - $args = array( |
|
| 970 | - 'nag_notice' => $pan_name, |
|
| 971 | - 'return_url' => urlencode($return_url), |
|
| 972 | - 'ajax_url' => WP_AJAX_URL, |
|
| 973 | - 'unknown_error' => __('An unknown error has occurred on the server while attempting to dismiss this notice.', |
|
| 974 | - 'event_espresso'), |
|
| 975 | - ); |
|
| 976 | - wp_localize_script('espresso_core', 'ee_dismiss', $args); |
|
| 977 | - return ' |
|
| 475 | + $output .= self::_print_scripts(true); |
|
| 476 | + if (defined('DOING_AJAX')) { |
|
| 477 | + echo wp_json_encode(array('error' => $output)); |
|
| 478 | + exit(); |
|
| 479 | + } |
|
| 480 | + echo $output; |
|
| 481 | + die(); |
|
| 482 | + } |
|
| 483 | + |
|
| 484 | + |
|
| 485 | + |
|
| 486 | + /** |
|
| 487 | + * generate string from exception trace args |
|
| 488 | + * |
|
| 489 | + * @param array $arguments |
|
| 490 | + * @param bool $array |
|
| 491 | + * @return string |
|
| 492 | + */ |
|
| 493 | + private function _convert_args_to_string($arguments = array(), $array = false) |
|
| 494 | + { |
|
| 495 | + $arg_string = ''; |
|
| 496 | + if (! empty($arguments)) { |
|
| 497 | + $args = array(); |
|
| 498 | + foreach ($arguments as $arg) { |
|
| 499 | + if (! empty($arg)) { |
|
| 500 | + if (is_string($arg)) { |
|
| 501 | + $args[] = " '" . $arg . "'"; |
|
| 502 | + } elseif (is_array($arg)) { |
|
| 503 | + $args[] = 'ARRAY(' . $this->_convert_args_to_string($arg, true); |
|
| 504 | + } elseif ($arg === null) { |
|
| 505 | + $args[] = ' NULL'; |
|
| 506 | + } elseif (is_bool($arg)) { |
|
| 507 | + $args[] = ($arg) ? ' TRUE' : ' FALSE'; |
|
| 508 | + } elseif (is_object($arg)) { |
|
| 509 | + $args[] = ' OBJECT ' . get_class($arg); |
|
| 510 | + } elseif (is_resource($arg)) { |
|
| 511 | + $args[] = get_resource_type($arg); |
|
| 512 | + } else { |
|
| 513 | + $args[] = $arg; |
|
| 514 | + } |
|
| 515 | + } |
|
| 516 | + } |
|
| 517 | + $arg_string = implode(', ', $args); |
|
| 518 | + } |
|
| 519 | + if ($array) { |
|
| 520 | + $arg_string .= ' )'; |
|
| 521 | + } |
|
| 522 | + return $arg_string; |
|
| 523 | + } |
|
| 524 | + |
|
| 525 | + |
|
| 526 | + |
|
| 527 | + /** |
|
| 528 | + * add error message |
|
| 529 | + * |
|
| 530 | + * @param string $msg the message to display to users or developers - adding a double pipe || (OR) creates |
|
| 531 | + * separate messages for user || dev |
|
| 532 | + * @param string $file the file that the error occurred in - just use __FILE__ |
|
| 533 | + * @param string $func the function/method that the error occurred in - just use __FUNCTION__ |
|
| 534 | + * @param string $line the line number where the error occurred - just use __LINE__ |
|
| 535 | + * @return void |
|
| 536 | + */ |
|
| 537 | + public static function add_error($msg = null, $file = null, $func = null, $line = null) |
|
| 538 | + { |
|
| 539 | + self::_add_notice('errors', $msg, $file, $func, $line); |
|
| 540 | + self::$_error_count++; |
|
| 541 | + } |
|
| 542 | + |
|
| 543 | + |
|
| 544 | + |
|
| 545 | + /** |
|
| 546 | + * If WP_DEBUG is active, throws an exception. If WP_DEBUG is off, just |
|
| 547 | + * adds an error |
|
| 548 | + * |
|
| 549 | + * @param string $msg |
|
| 550 | + * @param string $file |
|
| 551 | + * @param string $func |
|
| 552 | + * @param string $line |
|
| 553 | + * @throws EE_Error |
|
| 554 | + */ |
|
| 555 | + public static function throw_exception_if_debugging($msg = null, $file = null, $func = null, $line = null) |
|
| 556 | + { |
|
| 557 | + if (WP_DEBUG) { |
|
| 558 | + throw new EE_Error($msg); |
|
| 559 | + } |
|
| 560 | + EE_Error::add_error($msg, $file, $func, $line); |
|
| 561 | + } |
|
| 562 | + |
|
| 563 | + |
|
| 564 | + |
|
| 565 | + /** |
|
| 566 | + * add success message |
|
| 567 | + * |
|
| 568 | + * @param string $msg the message to display to users or developers - adding a double pipe || (OR) creates |
|
| 569 | + * separate messages for user || dev |
|
| 570 | + * @param string $file the file that the error occurred in - just use __FILE__ |
|
| 571 | + * @param string $func the function/method that the error occurred in - just use __FUNCTION__ |
|
| 572 | + * @param string $line the line number where the error occurred - just use __LINE__ |
|
| 573 | + * @return void |
|
| 574 | + */ |
|
| 575 | + public static function add_success($msg = null, $file = null, $func = null, $line = null) |
|
| 576 | + { |
|
| 577 | + self::_add_notice('success', $msg, $file, $func, $line); |
|
| 578 | + } |
|
| 579 | + |
|
| 580 | + |
|
| 581 | + |
|
| 582 | + /** |
|
| 583 | + * add attention message |
|
| 584 | + * |
|
| 585 | + * @param string $msg the message to display to users or developers - adding a double pipe || (OR) creates |
|
| 586 | + * separate messages for user || dev |
|
| 587 | + * @param string $file the file that the error occurred in - just use __FILE__ |
|
| 588 | + * @param string $func the function/method that the error occurred in - just use __FUNCTION__ |
|
| 589 | + * @param string $line the line number where the error occurred - just use __LINE__ |
|
| 590 | + * @return void |
|
| 591 | + */ |
|
| 592 | + public static function add_attention($msg = null, $file = null, $func = null, $line = null) |
|
| 593 | + { |
|
| 594 | + self::_add_notice('attention', $msg, $file, $func, $line); |
|
| 595 | + } |
|
| 596 | + |
|
| 597 | + |
|
| 598 | + |
|
| 599 | + /** |
|
| 600 | + * add success message |
|
| 601 | + * |
|
| 602 | + * @param string $type whether the message is for a success or error notification |
|
| 603 | + * @param string $msg the message to display to users or developers - adding a double pipe || (OR) creates |
|
| 604 | + * separate messages for user || dev |
|
| 605 | + * @param string $file the file that the error occurred in - just use __FILE__ |
|
| 606 | + * @param string $func the function/method that the error occurred in - just use __FUNCTION__ |
|
| 607 | + * @param string $line the line number where the error occurred - just use __LINE__ |
|
| 608 | + * @return void |
|
| 609 | + */ |
|
| 610 | + private static function _add_notice($type = 'success', $msg = null, $file = null, $func = null, $line = null) |
|
| 611 | + { |
|
| 612 | + if (empty($msg)) { |
|
| 613 | + EE_Error::doing_it_wrong( |
|
| 614 | + 'EE_Error::add_' . $type . '()', |
|
| 615 | + sprintf( |
|
| 616 | + __('Notifications are not much use without a message! Please add a message to the EE_Error::add_%s() call made in %s on line %d', |
|
| 617 | + 'event_espresso'), |
|
| 618 | + $type, |
|
| 619 | + $file, |
|
| 620 | + $line |
|
| 621 | + ), |
|
| 622 | + EVENT_ESPRESSO_VERSION |
|
| 623 | + ); |
|
| 624 | + } |
|
| 625 | + if ($type === 'errors' && (empty($file) || empty($func) || empty($line))) { |
|
| 626 | + EE_Error::doing_it_wrong( |
|
| 627 | + 'EE_Error::add_error()', |
|
| 628 | + __('You need to provide the file name, function name, and line number that the error occurred on in order to better assist with debugging.', |
|
| 629 | + 'event_espresso'), |
|
| 630 | + EVENT_ESPRESSO_VERSION |
|
| 631 | + ); |
|
| 632 | + } |
|
| 633 | + // get separate user and developer messages if they exist |
|
| 634 | + $msg = explode('||', $msg); |
|
| 635 | + $user_msg = $msg[0]; |
|
| 636 | + $dev_msg = isset($msg[1]) ? $msg[1] : $msg[0]; |
|
| 637 | + /** |
|
| 638 | + * Do an action so other code can be triggered when a notice is created |
|
| 639 | + * |
|
| 640 | + * @param string $type can be 'errors', 'attention', or 'success' |
|
| 641 | + * @param string $user_msg message displayed to user when WP_DEBUG is off |
|
| 642 | + * @param string $user_msg message displayed to user when WP_DEBUG is on |
|
| 643 | + * @param string $file file where error was generated |
|
| 644 | + * @param string $func function where error was generated |
|
| 645 | + * @param string $line line where error was generated |
|
| 646 | + */ |
|
| 647 | + do_action('AHEE__EE_Error___add_notice', $type, $user_msg, $dev_msg, $file, $func, $line); |
|
| 648 | + $msg = WP_DEBUG ? $dev_msg : $user_msg; |
|
| 649 | + // add notice if message exists |
|
| 650 | + if (! empty($msg)) { |
|
| 651 | + // get error code |
|
| 652 | + $notice_code = EE_Error::generate_error_code($file, $func, $line); |
|
| 653 | + if (WP_DEBUG && $type === 'errors') { |
|
| 654 | + $msg .= '<br/><span class="tiny-text">' . $notice_code . '</span>'; |
|
| 655 | + } |
|
| 656 | + // add notice. Index by code if it's not blank |
|
| 657 | + if ($notice_code) { |
|
| 658 | + self::$_espresso_notices[$type][$notice_code] = $msg; |
|
| 659 | + } else { |
|
| 660 | + self::$_espresso_notices[$type][] = $msg; |
|
| 661 | + } |
|
| 662 | + add_action('wp_footer', array('EE_Error', 'enqueue_error_scripts'), 1); |
|
| 663 | + } |
|
| 664 | + } |
|
| 665 | + |
|
| 666 | + |
|
| 667 | + |
|
| 668 | + /** |
|
| 669 | + * in some case it may be necessary to overwrite the existing success messages |
|
| 670 | + * |
|
| 671 | + * @return void |
|
| 672 | + */ |
|
| 673 | + public static function overwrite_success() |
|
| 674 | + { |
|
| 675 | + self::$_espresso_notices['success'] = false; |
|
| 676 | + } |
|
| 677 | + |
|
| 678 | + |
|
| 679 | + |
|
| 680 | + /** |
|
| 681 | + * in some case it may be necessary to overwrite the existing attention messages |
|
| 682 | + * |
|
| 683 | + * @return void |
|
| 684 | + */ |
|
| 685 | + public static function overwrite_attention() |
|
| 686 | + { |
|
| 687 | + self::$_espresso_notices['attention'] = false; |
|
| 688 | + } |
|
| 689 | + |
|
| 690 | + |
|
| 691 | + |
|
| 692 | + /** |
|
| 693 | + * in some case it may be necessary to overwrite the existing error messages |
|
| 694 | + * |
|
| 695 | + * @return void |
|
| 696 | + */ |
|
| 697 | + public static function overwrite_errors() |
|
| 698 | + { |
|
| 699 | + self::$_espresso_notices['errors'] = false; |
|
| 700 | + } |
|
| 701 | + |
|
| 702 | + |
|
| 703 | + |
|
| 704 | + /** |
|
| 705 | + * reset_notices |
|
| 706 | + * |
|
| 707 | + * @return void |
|
| 708 | + */ |
|
| 709 | + public static function reset_notices() |
|
| 710 | + { |
|
| 711 | + self::$_espresso_notices['success'] = false; |
|
| 712 | + self::$_espresso_notices['attention'] = false; |
|
| 713 | + self::$_espresso_notices['errors'] = false; |
|
| 714 | + } |
|
| 715 | + |
|
| 716 | + |
|
| 717 | + |
|
| 718 | + /** |
|
| 719 | + * has_errors |
|
| 720 | + * |
|
| 721 | + * @return int |
|
| 722 | + */ |
|
| 723 | + public static function has_notices() |
|
| 724 | + { |
|
| 725 | + $has_notices = 0; |
|
| 726 | + // check for success messages |
|
| 727 | + $has_notices = self::$_espresso_notices['success'] && ! empty(self::$_espresso_notices['success']) ? 3 |
|
| 728 | + : $has_notices; |
|
| 729 | + // check for attention messages |
|
| 730 | + $has_notices = self::$_espresso_notices['attention'] && ! empty(self::$_espresso_notices['attention']) ? 2 |
|
| 731 | + : $has_notices; |
|
| 732 | + // check for error messages |
|
| 733 | + $has_notices = self::$_espresso_notices['errors'] && ! empty(self::$_espresso_notices['errors']) ? 1 |
|
| 734 | + : $has_notices; |
|
| 735 | + return $has_notices; |
|
| 736 | + } |
|
| 737 | + |
|
| 738 | + |
|
| 739 | + |
|
| 740 | + /** |
|
| 741 | + * This simply returns non formatted error notices as they were sent into the EE_Error object. |
|
| 742 | + * |
|
| 743 | + * @since 4.9.0 |
|
| 744 | + * @return array |
|
| 745 | + */ |
|
| 746 | + public static function get_vanilla_notices() |
|
| 747 | + { |
|
| 748 | + return array( |
|
| 749 | + 'success' => isset(self::$_espresso_notices['success']) ? self::$_espresso_notices['success'] : array(), |
|
| 750 | + 'attention' => isset(self::$_espresso_notices['attention']) ? self::$_espresso_notices['attention'] |
|
| 751 | + : array(), |
|
| 752 | + 'errors' => isset(self::$_espresso_notices['errors']) ? self::$_espresso_notices['errors'] : array(), |
|
| 753 | + ); |
|
| 754 | + } |
|
| 755 | + |
|
| 756 | + |
|
| 757 | + |
|
| 758 | + /** |
|
| 759 | + * compile all error or success messages into one string |
|
| 760 | + * |
|
| 761 | + * @see EE_Error::get_raw_notices if you want the raw notices without any preparations made to them |
|
| 762 | + * @param boolean $format_output whether or not to format the messages for display in the WP admin |
|
| 763 | + * @param boolean $save_to_transient whether or not to save notices to the db for retrieval on next request |
|
| 764 | + * - ONLY do this just before redirecting |
|
| 765 | + * @param boolean $remove_empty whether or not to unset empty messages |
|
| 766 | + * @return array |
|
| 767 | + */ |
|
| 768 | + public static function get_notices($format_output = true, $save_to_transient = false, $remove_empty = true) |
|
| 769 | + { |
|
| 770 | + do_action('AHEE_log', __FILE__, __FUNCTION__, ''); |
|
| 771 | + $success_messages = ''; |
|
| 772 | + $attention_messages = ''; |
|
| 773 | + $error_messages = ''; |
|
| 774 | + $print_scripts = false; |
|
| 775 | + // either save notices to the db |
|
| 776 | + if ($save_to_transient) { |
|
| 777 | + update_option('ee_notices', self::$_espresso_notices); |
|
| 778 | + return array(); |
|
| 779 | + } |
|
| 780 | + // grab any notices that have been previously saved |
|
| 781 | + if ($notices = get_option('ee_notices', false)) { |
|
| 782 | + foreach ($notices as $type => $notice) { |
|
| 783 | + if (is_array($notice) && ! empty($notice)) { |
|
| 784 | + // make sure that existing notice type is an array |
|
| 785 | + self::$_espresso_notices[$type] = is_array(self::$_espresso_notices[$type]) |
|
| 786 | + && ! empty(self::$_espresso_notices[$type]) |
|
| 787 | + ? self::$_espresso_notices[$type] : array(); |
|
| 788 | + // merge stored notices with any newly created ones |
|
| 789 | + self::$_espresso_notices[$type] = array_merge(self::$_espresso_notices[$type], $notice); |
|
| 790 | + $print_scripts = true; |
|
| 791 | + } |
|
| 792 | + } |
|
| 793 | + // now clear any stored notices |
|
| 794 | + update_option('ee_notices', false); |
|
| 795 | + } |
|
| 796 | + // check for success messages |
|
| 797 | + if (self::$_espresso_notices['success'] && ! empty(self::$_espresso_notices['success'])) { |
|
| 798 | + // combine messages |
|
| 799 | + $success_messages .= implode(self::$_espresso_notices['success'], '<br /><br />'); |
|
| 800 | + $print_scripts = true; |
|
| 801 | + } |
|
| 802 | + // check for attention messages |
|
| 803 | + if (self::$_espresso_notices['attention'] && ! empty(self::$_espresso_notices['attention'])) { |
|
| 804 | + // combine messages |
|
| 805 | + $attention_messages .= implode(self::$_espresso_notices['attention'], '<br /><br />'); |
|
| 806 | + $print_scripts = true; |
|
| 807 | + } |
|
| 808 | + // check for error messages |
|
| 809 | + if (self::$_espresso_notices['errors'] && ! empty(self::$_espresso_notices['errors'])) { |
|
| 810 | + $error_messages .= count(self::$_espresso_notices['errors']) > 1 |
|
| 811 | + ? __('The following errors have occurred:<br />', 'event_espresso') |
|
| 812 | + : __('An error has occurred:<br />', 'event_espresso'); |
|
| 813 | + // combine messages |
|
| 814 | + $error_messages .= implode(self::$_espresso_notices['errors'], '<br /><br />'); |
|
| 815 | + $print_scripts = true; |
|
| 816 | + } |
|
| 817 | + if ($format_output) { |
|
| 818 | + $notices = '<div id="espresso-notices">'; |
|
| 819 | + $close = is_admin() ? '' |
|
| 820 | + : '<a class="close-espresso-notice hide-if-no-js"><span class="dashicons dashicons-no"></span></a>'; |
|
| 821 | + if ($success_messages !== '') { |
|
| 822 | + $css_id = is_admin() ? 'message' : 'espresso-notices-success'; |
|
| 823 | + $css_class = is_admin() ? 'updated fade' : 'success fade-away'; |
|
| 824 | + //showMessage( $success_messages ); |
|
| 825 | + $notices .= '<div id="' |
|
| 826 | + . $css_id |
|
| 827 | + . '" class="espresso-notices ' |
|
| 828 | + . $css_class |
|
| 829 | + . '" style="display:none;"><p>' |
|
| 830 | + . $success_messages |
|
| 831 | + . '</p>' |
|
| 832 | + . $close |
|
| 833 | + . '</div>'; |
|
| 834 | + } |
|
| 835 | + if ($attention_messages !== '') { |
|
| 836 | + $css_id = is_admin() ? 'message' : 'espresso-notices-attention'; |
|
| 837 | + $css_class = is_admin() ? 'updated ee-notices-attention' : 'attention fade-away'; |
|
| 838 | + //showMessage( $error_messages, TRUE ); |
|
| 839 | + $notices .= '<div id="' |
|
| 840 | + . $css_id |
|
| 841 | + . '" class="espresso-notices ' |
|
| 842 | + . $css_class |
|
| 843 | + . '" style="display:none;"><p>' |
|
| 844 | + . $attention_messages |
|
| 845 | + . '</p>' |
|
| 846 | + . $close |
|
| 847 | + . '</div>'; |
|
| 848 | + } |
|
| 849 | + if ($error_messages !== '') { |
|
| 850 | + $css_id = is_admin() ? 'message' : 'espresso-notices-error'; |
|
| 851 | + $css_class = is_admin() ? 'error' : 'error fade-away'; |
|
| 852 | + //showMessage( $error_messages, TRUE ); |
|
| 853 | + $notices .= '<div id="' |
|
| 854 | + . $css_id |
|
| 855 | + . '" class="espresso-notices ' |
|
| 856 | + . $css_class |
|
| 857 | + . '" style="display:none;"><p>' |
|
| 858 | + . $error_messages |
|
| 859 | + . '</p>' |
|
| 860 | + . $close |
|
| 861 | + . '</div>'; |
|
| 862 | + } |
|
| 863 | + $notices .= '</div>'; |
|
| 864 | + } else { |
|
| 865 | + $notices = array( |
|
| 866 | + 'success' => $success_messages, |
|
| 867 | + 'attention' => $attention_messages, |
|
| 868 | + 'errors' => $error_messages, |
|
| 869 | + ); |
|
| 870 | + if ($remove_empty) { |
|
| 871 | + // remove empty notices |
|
| 872 | + foreach ($notices as $type => $notice) { |
|
| 873 | + if (empty($notice)) { |
|
| 874 | + unset($notices[$type]); |
|
| 875 | + } |
|
| 876 | + } |
|
| 877 | + } |
|
| 878 | + } |
|
| 879 | + if ($print_scripts) { |
|
| 880 | + self::_print_scripts(); |
|
| 881 | + } |
|
| 882 | + return $notices; |
|
| 883 | + } |
|
| 884 | + |
|
| 885 | + |
|
| 886 | + |
|
| 887 | + /** |
|
| 888 | + * add_persistent_admin_notice |
|
| 889 | + * |
|
| 890 | + * @param string $pan_name the name, or key of the Persistent Admin Notice to be stored |
|
| 891 | + * @param string $pan_message the message to be stored persistently until dismissed |
|
| 892 | + * @param bool $force_update allows one to enforce the reappearance of a persistent message. |
|
| 893 | + * @return void |
|
| 894 | + */ |
|
| 895 | + public static function add_persistent_admin_notice($pan_name = '', $pan_message, $force_update = false) |
|
| 896 | + { |
|
| 897 | + if (! empty($pan_name) && ! empty($pan_message)) { |
|
| 898 | + $persistent_admin_notices = get_option('ee_pers_admin_notices', array()); |
|
| 899 | + //maybe initialize persistent_admin_notices |
|
| 900 | + if (empty($persistent_admin_notices)) { |
|
| 901 | + add_option('ee_pers_admin_notices', array(), '', 'no'); |
|
| 902 | + } |
|
| 903 | + $pan_name = sanitize_key($pan_name); |
|
| 904 | + if (! array_key_exists($pan_name, $persistent_admin_notices) || $force_update) { |
|
| 905 | + $persistent_admin_notices[$pan_name] = $pan_message; |
|
| 906 | + update_option('ee_pers_admin_notices', $persistent_admin_notices); |
|
| 907 | + } |
|
| 908 | + } |
|
| 909 | + } |
|
| 910 | + |
|
| 911 | + |
|
| 912 | + |
|
| 913 | + /** |
|
| 914 | + * dismiss_persistent_admin_notice |
|
| 915 | + * |
|
| 916 | + * @param string $pan_name the name, or key of the Persistent Admin Notice to be dismissed |
|
| 917 | + * @param bool $purge |
|
| 918 | + * @param bool $return_immediately |
|
| 919 | + * @return void |
|
| 920 | + */ |
|
| 921 | + public static function dismiss_persistent_admin_notice($pan_name = '', $purge = false, $return_immediately = false) |
|
| 922 | + { |
|
| 923 | + $pan_name = EE_Registry::instance()->REQ->is_set('ee_nag_notice') |
|
| 924 | + ? EE_Registry::instance()->REQ->get('ee_nag_notice') : $pan_name; |
|
| 925 | + if (! empty($pan_name)) { |
|
| 926 | + $persistent_admin_notices = get_option('ee_pers_admin_notices', array()); |
|
| 927 | + // check if notice we wish to dismiss is actually in the $persistent_admin_notices array |
|
| 928 | + if (is_array($persistent_admin_notices) && isset($persistent_admin_notices[$pan_name])) { |
|
| 929 | + // completely delete nag notice, or just NULL message so that it can NOT be added again ? |
|
| 930 | + if ($purge) { |
|
| 931 | + unset($persistent_admin_notices[$pan_name]); |
|
| 932 | + } else { |
|
| 933 | + $persistent_admin_notices[$pan_name] = null; |
|
| 934 | + } |
|
| 935 | + if (update_option('ee_pers_admin_notices', $persistent_admin_notices) === false) { |
|
| 936 | + EE_Error::add_error(sprintf(__('The persistent admin notice for "%s" could not be deleted.', |
|
| 937 | + 'event_espresso'), $pan_name), __FILE__, __FUNCTION__, __LINE__); |
|
| 938 | + } |
|
| 939 | + } |
|
| 940 | + } |
|
| 941 | + if ($return_immediately) { |
|
| 942 | + return; |
|
| 943 | + } |
|
| 944 | + if (EE_Registry::instance()->REQ->ajax) { |
|
| 945 | + // grab any notices and concatenate into string |
|
| 946 | + echo wp_json_encode(array('errors' => implode('<br />', EE_Error::get_notices(false)))); |
|
| 947 | + exit(); |
|
| 948 | + } |
|
| 949 | + // save errors to a transient to be displayed on next request (after redirect) |
|
| 950 | + EE_Error::get_notices(false, true); |
|
| 951 | + $return_url = EE_Registry::instance()->REQ->is_set('return_url') |
|
| 952 | + ? EE_Registry::instance()->REQ->get('return_url') : ''; |
|
| 953 | + wp_safe_redirect(urldecode($return_url)); |
|
| 954 | + } |
|
| 955 | + |
|
| 956 | + |
|
| 957 | + |
|
| 958 | + /** |
|
| 959 | + * display_persistent_admin_notices |
|
| 960 | + * |
|
| 961 | + * @param string $pan_name the name, or key of the Persistent Admin Notice to be stored |
|
| 962 | + * @param string $pan_message the message to be stored persistently until dismissed |
|
| 963 | + * @param string $return_url URL to go back to after nag notice is dismissed |
|
| 964 | + * @return string |
|
| 965 | + */ |
|
| 966 | + public static function display_persistent_admin_notices($pan_name = '', $pan_message = '', $return_url = '') |
|
| 967 | + { |
|
| 968 | + if (! empty($pan_name) && ! empty($pan_message)) { |
|
| 969 | + $args = array( |
|
| 970 | + 'nag_notice' => $pan_name, |
|
| 971 | + 'return_url' => urlencode($return_url), |
|
| 972 | + 'ajax_url' => WP_AJAX_URL, |
|
| 973 | + 'unknown_error' => __('An unknown error has occurred on the server while attempting to dismiss this notice.', |
|
| 974 | + 'event_espresso'), |
|
| 975 | + ); |
|
| 976 | + wp_localize_script('espresso_core', 'ee_dismiss', $args); |
|
| 977 | + return ' |
|
| 978 | 978 | <div id="' |
| 979 | - . $pan_name |
|
| 980 | - . '" class="espresso-notices updated ee-nag-notice clearfix" style="border-left: 4px solid #fcb93c;"> |
|
| 979 | + . $pan_name |
|
| 980 | + . '" class="espresso-notices updated ee-nag-notice clearfix" style="border-left: 4px solid #fcb93c;"> |
|
| 981 | 981 | <p>' |
| 982 | - . $pan_message |
|
| 983 | - . '</p> |
|
| 982 | + . $pan_message |
|
| 983 | + . '</p> |
|
| 984 | 984 | <a class="dismiss-ee-nag-notice hide-if-no-js" style="float: right; cursor: pointer; text-decoration:none;" rel="' |
| 985 | - . $pan_name |
|
| 986 | - . '"> |
|
| 985 | + . $pan_name |
|
| 986 | + . '"> |
|
| 987 | 987 | <span class="dashicons dashicons-dismiss" style="position:relative; top:-1px; margin-right:.25em;"></span>' |
| 988 | - . __('Dismiss', 'event_espresso') |
|
| 989 | - . ' |
|
| 988 | + . __('Dismiss', 'event_espresso') |
|
| 989 | + . ' |
|
| 990 | 990 | </a> |
| 991 | 991 | <div style="clear:both;"></div> |
| 992 | 992 | </div>'; |
| 993 | - } |
|
| 994 | - return ''; |
|
| 995 | - } |
|
| 996 | - |
|
| 997 | - |
|
| 998 | - |
|
| 999 | - /** |
|
| 1000 | - * get_persistent_admin_notices |
|
| 1001 | - * |
|
| 1002 | - * @param string $return_url |
|
| 1003 | - * @return string |
|
| 1004 | - */ |
|
| 1005 | - public static function get_persistent_admin_notices($return_url = '') |
|
| 1006 | - { |
|
| 1007 | - $notices = ''; |
|
| 1008 | - // check for persistent admin notices |
|
| 1009 | - //filter the list though so plugins can notify the admin in a different way if they want |
|
| 1010 | - $persistent_admin_notices = apply_filters( |
|
| 1011 | - 'FHEE__EE_Error__get_persistent_admin_notices', |
|
| 1012 | - get_option('ee_pers_admin_notices', false), |
|
| 1013 | - 'ee_pers_admin_notices', |
|
| 1014 | - $return_url |
|
| 1015 | - ); |
|
| 1016 | - if ($persistent_admin_notices) { |
|
| 1017 | - // load scripts |
|
| 1018 | - wp_register_script( |
|
| 1019 | - 'espresso_core', |
|
| 1020 | - EE_GLOBAL_ASSETS_URL . 'scripts/espresso_core.js', |
|
| 1021 | - array('jquery'), |
|
| 1022 | - EVENT_ESPRESSO_VERSION, |
|
| 1023 | - true |
|
| 1024 | - ); |
|
| 1025 | - wp_register_script( |
|
| 1026 | - 'ee_error_js', |
|
| 1027 | - EE_GLOBAL_ASSETS_URL . 'scripts/EE_Error.js', |
|
| 1028 | - array('espresso_core'), |
|
| 1029 | - EVENT_ESPRESSO_VERSION, |
|
| 1030 | - true |
|
| 1031 | - ); |
|
| 1032 | - wp_enqueue_script('ee_error_js'); |
|
| 1033 | - // and display notices |
|
| 1034 | - foreach ($persistent_admin_notices as $pan_name => $pan_message) { |
|
| 1035 | - $notices .= self::display_persistent_admin_notices($pan_name, $pan_message, $return_url); |
|
| 1036 | - } |
|
| 1037 | - } |
|
| 1038 | - return $notices; |
|
| 1039 | - } |
|
| 1040 | - |
|
| 1041 | - |
|
| 1042 | - |
|
| 1043 | - /** |
|
| 1044 | - * _print_scripts |
|
| 1045 | - * |
|
| 1046 | - * @param bool $force_print |
|
| 1047 | - * @return string |
|
| 1048 | - */ |
|
| 1049 | - private static function _print_scripts($force_print = false) |
|
| 1050 | - { |
|
| 1051 | - if (! $force_print && (did_action('admin_enqueue_scripts') || did_action('wp_enqueue_scripts'))) { |
|
| 1052 | - if (wp_script_is('ee_error_js', 'enqueued')) { |
|
| 1053 | - return ''; |
|
| 1054 | - } |
|
| 1055 | - if (wp_script_is('ee_error_js', 'registered')) { |
|
| 1056 | - wp_enqueue_style('espresso_default'); |
|
| 1057 | - wp_enqueue_style('espresso_custom_css'); |
|
| 1058 | - wp_enqueue_script('ee_error_js'); |
|
| 1059 | - wp_localize_script('ee_error_js', 'ee_settings', array('wp_debug' => WP_DEBUG)); |
|
| 1060 | - } |
|
| 1061 | - } else { |
|
| 1062 | - return ' |
|
| 993 | + } |
|
| 994 | + return ''; |
|
| 995 | + } |
|
| 996 | + |
|
| 997 | + |
|
| 998 | + |
|
| 999 | + /** |
|
| 1000 | + * get_persistent_admin_notices |
|
| 1001 | + * |
|
| 1002 | + * @param string $return_url |
|
| 1003 | + * @return string |
|
| 1004 | + */ |
|
| 1005 | + public static function get_persistent_admin_notices($return_url = '') |
|
| 1006 | + { |
|
| 1007 | + $notices = ''; |
|
| 1008 | + // check for persistent admin notices |
|
| 1009 | + //filter the list though so plugins can notify the admin in a different way if they want |
|
| 1010 | + $persistent_admin_notices = apply_filters( |
|
| 1011 | + 'FHEE__EE_Error__get_persistent_admin_notices', |
|
| 1012 | + get_option('ee_pers_admin_notices', false), |
|
| 1013 | + 'ee_pers_admin_notices', |
|
| 1014 | + $return_url |
|
| 1015 | + ); |
|
| 1016 | + if ($persistent_admin_notices) { |
|
| 1017 | + // load scripts |
|
| 1018 | + wp_register_script( |
|
| 1019 | + 'espresso_core', |
|
| 1020 | + EE_GLOBAL_ASSETS_URL . 'scripts/espresso_core.js', |
|
| 1021 | + array('jquery'), |
|
| 1022 | + EVENT_ESPRESSO_VERSION, |
|
| 1023 | + true |
|
| 1024 | + ); |
|
| 1025 | + wp_register_script( |
|
| 1026 | + 'ee_error_js', |
|
| 1027 | + EE_GLOBAL_ASSETS_URL . 'scripts/EE_Error.js', |
|
| 1028 | + array('espresso_core'), |
|
| 1029 | + EVENT_ESPRESSO_VERSION, |
|
| 1030 | + true |
|
| 1031 | + ); |
|
| 1032 | + wp_enqueue_script('ee_error_js'); |
|
| 1033 | + // and display notices |
|
| 1034 | + foreach ($persistent_admin_notices as $pan_name => $pan_message) { |
|
| 1035 | + $notices .= self::display_persistent_admin_notices($pan_name, $pan_message, $return_url); |
|
| 1036 | + } |
|
| 1037 | + } |
|
| 1038 | + return $notices; |
|
| 1039 | + } |
|
| 1040 | + |
|
| 1041 | + |
|
| 1042 | + |
|
| 1043 | + /** |
|
| 1044 | + * _print_scripts |
|
| 1045 | + * |
|
| 1046 | + * @param bool $force_print |
|
| 1047 | + * @return string |
|
| 1048 | + */ |
|
| 1049 | + private static function _print_scripts($force_print = false) |
|
| 1050 | + { |
|
| 1051 | + if (! $force_print && (did_action('admin_enqueue_scripts') || did_action('wp_enqueue_scripts'))) { |
|
| 1052 | + if (wp_script_is('ee_error_js', 'enqueued')) { |
|
| 1053 | + return ''; |
|
| 1054 | + } |
|
| 1055 | + if (wp_script_is('ee_error_js', 'registered')) { |
|
| 1056 | + wp_enqueue_style('espresso_default'); |
|
| 1057 | + wp_enqueue_style('espresso_custom_css'); |
|
| 1058 | + wp_enqueue_script('ee_error_js'); |
|
| 1059 | + wp_localize_script('ee_error_js', 'ee_settings', array('wp_debug' => WP_DEBUG)); |
|
| 1060 | + } |
|
| 1061 | + } else { |
|
| 1062 | + return ' |
|
| 1063 | 1063 | <script> |
| 1064 | 1064 | /* <![CDATA[ */ |
| 1065 | 1065 | var ee_settings = {"wp_debug":"' . WP_DEBUG . '"}; |
@@ -1069,143 +1069,143 @@ discard block |
||
| 1069 | 1069 | <script src="' . EE_GLOBAL_ASSETS_URL . 'scripts/espresso_core.js' . '?ver=' . espresso_version() . '" type="text/javascript"></script> |
| 1070 | 1070 | <script src="' . EE_GLOBAL_ASSETS_URL . 'scripts/EE_Error.js' . '?ver=' . espresso_version() . '" type="text/javascript"></script> |
| 1071 | 1071 | '; |
| 1072 | - } |
|
| 1073 | - return ''; |
|
| 1074 | - } |
|
| 1075 | - |
|
| 1076 | - |
|
| 1077 | - |
|
| 1078 | - /** |
|
| 1079 | - * enqueue_error_scripts |
|
| 1080 | - * |
|
| 1081 | - * @return void |
|
| 1082 | - */ |
|
| 1083 | - public static function enqueue_error_scripts() |
|
| 1084 | - { |
|
| 1085 | - self::_print_scripts(); |
|
| 1086 | - } |
|
| 1087 | - |
|
| 1088 | - |
|
| 1089 | - |
|
| 1090 | - /** |
|
| 1091 | - * create error code from filepath, function name, |
|
| 1092 | - * and line number where exception or error was thrown |
|
| 1093 | - * |
|
| 1094 | - * @param string $file |
|
| 1095 | - * @param string $func |
|
| 1096 | - * @param string $line |
|
| 1097 | - * @return string |
|
| 1098 | - */ |
|
| 1099 | - public static function generate_error_code($file = '', $func = '', $line = '') |
|
| 1100 | - { |
|
| 1101 | - $file = explode('.', basename($file)); |
|
| 1102 | - $error_code = ! empty($file[0]) ? $file[0] : ''; |
|
| 1103 | - $error_code .= ! empty($func) ? ' - ' . $func : ''; |
|
| 1104 | - $error_code .= ! empty($line) ? ' - ' . $line : ''; |
|
| 1105 | - return $error_code; |
|
| 1106 | - } |
|
| 1107 | - |
|
| 1108 | - |
|
| 1109 | - |
|
| 1110 | - /** |
|
| 1111 | - * write exception details to log file |
|
| 1112 | - * |
|
| 1113 | - * @param int $time |
|
| 1114 | - * @param array $ex |
|
| 1115 | - * @param bool $clear |
|
| 1116 | - * @return void |
|
| 1117 | - */ |
|
| 1118 | - public function write_to_error_log($time = 0, $ex = array(), $clear = false) |
|
| 1119 | - { |
|
| 1120 | - if (empty($ex)) { |
|
| 1121 | - return; |
|
| 1122 | - } |
|
| 1123 | - if (! $time) { |
|
| 1124 | - $time = time(); |
|
| 1125 | - } |
|
| 1126 | - $exception_log = '----------------------------------------------------------------------------------------' |
|
| 1127 | - . PHP_EOL; |
|
| 1128 | - $exception_log .= '[' . date('Y-m-d H:i:s', $time) . '] Exception Details' . PHP_EOL; |
|
| 1129 | - $exception_log .= 'Message: ' . $ex['msg'] . PHP_EOL; |
|
| 1130 | - $exception_log .= 'Code: ' . $ex['code'] . PHP_EOL; |
|
| 1131 | - $exception_log .= 'File: ' . $ex['file'] . PHP_EOL; |
|
| 1132 | - $exception_log .= 'Line No: ' . $ex['line'] . PHP_EOL; |
|
| 1133 | - $exception_log .= 'Stack trace: ' . PHP_EOL; |
|
| 1134 | - $exception_log .= $ex['string'] . PHP_EOL; |
|
| 1135 | - $exception_log .= '----------------------------------------------------------------------------------------' |
|
| 1136 | - . PHP_EOL; |
|
| 1137 | - try { |
|
| 1138 | - EEH_File::ensure_file_exists_and_is_writable( |
|
| 1139 | - EVENT_ESPRESSO_UPLOAD_DIR . 'logs' . DS . self::$_exception_log_file |
|
| 1140 | - ); |
|
| 1141 | - EEH_File::add_htaccess_deny_from_all(EVENT_ESPRESSO_UPLOAD_DIR . 'logs'); |
|
| 1142 | - if (! $clear) { |
|
| 1143 | - //get existing log file and append new log info |
|
| 1144 | - $exception_log = EEH_File::get_file_contents( |
|
| 1145 | - EVENT_ESPRESSO_UPLOAD_DIR . 'logs' . DS . self::$_exception_log_file |
|
| 1146 | - ) . $exception_log; |
|
| 1147 | - } |
|
| 1148 | - EEH_File::write_to_file( |
|
| 1149 | - EVENT_ESPRESSO_UPLOAD_DIR . 'logs' . DS . self::$_exception_log_file, |
|
| 1150 | - $exception_log |
|
| 1151 | - ); |
|
| 1152 | - } catch (EE_Error $e) { |
|
| 1153 | - EE_Error::add_error(sprintf(__('Event Espresso error logging could not be setup because: %s', |
|
| 1154 | - 'event_espresso'), $e->getMessage())); |
|
| 1155 | - return; |
|
| 1156 | - } |
|
| 1157 | - } |
|
| 1158 | - |
|
| 1159 | - |
|
| 1160 | - |
|
| 1161 | - /** |
|
| 1162 | - * This is just a wrapper for the EEH_Debug_Tools::instance()->doing_it_wrong() method. |
|
| 1163 | - * doing_it_wrong() is used in those cases where a normal PHP error won't get thrown, |
|
| 1164 | - * but the code execution is done in a manner that could lead to unexpected results |
|
| 1165 | - * (i.e. running to early, or too late in WP or EE loading process). |
|
| 1166 | - * A good test for knowing whether to use this method is: |
|
| 1167 | - * 1. Is there going to be a PHP error if something isn't setup/used correctly? |
|
| 1168 | - * Yes -> use EE_Error::add_error() or throw new EE_Error() |
|
| 1169 | - * 2. If this is loaded before something else, it won't break anything, |
|
| 1170 | - * but just wont' do what its supposed to do? Yes -> use EE_Error::doing_it_wrong() |
|
| 1171 | - * |
|
| 1172 | - * @uses constant WP_DEBUG test if wp_debug is on or not |
|
| 1173 | - * @param string $function The function that was called |
|
| 1174 | - * @param string $message A message explaining what has been done incorrectly |
|
| 1175 | - * @param string $version The version of Event Espresso where the error was added |
|
| 1176 | - * @param string $applies_when a version string for when you want the doing_it_wrong notice to begin appearing |
|
| 1177 | - * for a deprecated function. This allows deprecation to occur during one version, |
|
| 1178 | - * but not have any notices appear until a later version. This allows developers |
|
| 1179 | - * extra time to update their code before notices appear. |
|
| 1180 | - * @param int $error_type |
|
| 1181 | - */ |
|
| 1182 | - public static function doing_it_wrong( |
|
| 1183 | - $function, |
|
| 1184 | - $message, |
|
| 1185 | - $version, |
|
| 1186 | - $applies_when = '', |
|
| 1187 | - $error_type = null |
|
| 1188 | - ) { |
|
| 1189 | - if (defined('WP_DEBUG') && WP_DEBUG) { |
|
| 1190 | - EEH_Debug_Tools::instance()->doing_it_wrong($function, $message, $version, $applies_when, $error_type); |
|
| 1191 | - } |
|
| 1192 | - } |
|
| 1193 | - |
|
| 1194 | - |
|
| 1195 | - |
|
| 1196 | - /** |
|
| 1197 | - * Like get_notices, but returns an array of all the notices of the given type. |
|
| 1198 | - * |
|
| 1199 | - * @return array { |
|
| 1200 | - * @type array $success all the success messages |
|
| 1201 | - * @type array $errors all the error messages |
|
| 1202 | - * @type array $attention all the attention messages |
|
| 1203 | - * } |
|
| 1204 | - */ |
|
| 1205 | - public static function get_raw_notices() |
|
| 1206 | - { |
|
| 1207 | - return self::$_espresso_notices; |
|
| 1208 | - } |
|
| 1072 | + } |
|
| 1073 | + return ''; |
|
| 1074 | + } |
|
| 1075 | + |
|
| 1076 | + |
|
| 1077 | + |
|
| 1078 | + /** |
|
| 1079 | + * enqueue_error_scripts |
|
| 1080 | + * |
|
| 1081 | + * @return void |
|
| 1082 | + */ |
|
| 1083 | + public static function enqueue_error_scripts() |
|
| 1084 | + { |
|
| 1085 | + self::_print_scripts(); |
|
| 1086 | + } |
|
| 1087 | + |
|
| 1088 | + |
|
| 1089 | + |
|
| 1090 | + /** |
|
| 1091 | + * create error code from filepath, function name, |
|
| 1092 | + * and line number where exception or error was thrown |
|
| 1093 | + * |
|
| 1094 | + * @param string $file |
|
| 1095 | + * @param string $func |
|
| 1096 | + * @param string $line |
|
| 1097 | + * @return string |
|
| 1098 | + */ |
|
| 1099 | + public static function generate_error_code($file = '', $func = '', $line = '') |
|
| 1100 | + { |
|
| 1101 | + $file = explode('.', basename($file)); |
|
| 1102 | + $error_code = ! empty($file[0]) ? $file[0] : ''; |
|
| 1103 | + $error_code .= ! empty($func) ? ' - ' . $func : ''; |
|
| 1104 | + $error_code .= ! empty($line) ? ' - ' . $line : ''; |
|
| 1105 | + return $error_code; |
|
| 1106 | + } |
|
| 1107 | + |
|
| 1108 | + |
|
| 1109 | + |
|
| 1110 | + /** |
|
| 1111 | + * write exception details to log file |
|
| 1112 | + * |
|
| 1113 | + * @param int $time |
|
| 1114 | + * @param array $ex |
|
| 1115 | + * @param bool $clear |
|
| 1116 | + * @return void |
|
| 1117 | + */ |
|
| 1118 | + public function write_to_error_log($time = 0, $ex = array(), $clear = false) |
|
| 1119 | + { |
|
| 1120 | + if (empty($ex)) { |
|
| 1121 | + return; |
|
| 1122 | + } |
|
| 1123 | + if (! $time) { |
|
| 1124 | + $time = time(); |
|
| 1125 | + } |
|
| 1126 | + $exception_log = '----------------------------------------------------------------------------------------' |
|
| 1127 | + . PHP_EOL; |
|
| 1128 | + $exception_log .= '[' . date('Y-m-d H:i:s', $time) . '] Exception Details' . PHP_EOL; |
|
| 1129 | + $exception_log .= 'Message: ' . $ex['msg'] . PHP_EOL; |
|
| 1130 | + $exception_log .= 'Code: ' . $ex['code'] . PHP_EOL; |
|
| 1131 | + $exception_log .= 'File: ' . $ex['file'] . PHP_EOL; |
|
| 1132 | + $exception_log .= 'Line No: ' . $ex['line'] . PHP_EOL; |
|
| 1133 | + $exception_log .= 'Stack trace: ' . PHP_EOL; |
|
| 1134 | + $exception_log .= $ex['string'] . PHP_EOL; |
|
| 1135 | + $exception_log .= '----------------------------------------------------------------------------------------' |
|
| 1136 | + . PHP_EOL; |
|
| 1137 | + try { |
|
| 1138 | + EEH_File::ensure_file_exists_and_is_writable( |
|
| 1139 | + EVENT_ESPRESSO_UPLOAD_DIR . 'logs' . DS . self::$_exception_log_file |
|
| 1140 | + ); |
|
| 1141 | + EEH_File::add_htaccess_deny_from_all(EVENT_ESPRESSO_UPLOAD_DIR . 'logs'); |
|
| 1142 | + if (! $clear) { |
|
| 1143 | + //get existing log file and append new log info |
|
| 1144 | + $exception_log = EEH_File::get_file_contents( |
|
| 1145 | + EVENT_ESPRESSO_UPLOAD_DIR . 'logs' . DS . self::$_exception_log_file |
|
| 1146 | + ) . $exception_log; |
|
| 1147 | + } |
|
| 1148 | + EEH_File::write_to_file( |
|
| 1149 | + EVENT_ESPRESSO_UPLOAD_DIR . 'logs' . DS . self::$_exception_log_file, |
|
| 1150 | + $exception_log |
|
| 1151 | + ); |
|
| 1152 | + } catch (EE_Error $e) { |
|
| 1153 | + EE_Error::add_error(sprintf(__('Event Espresso error logging could not be setup because: %s', |
|
| 1154 | + 'event_espresso'), $e->getMessage())); |
|
| 1155 | + return; |
|
| 1156 | + } |
|
| 1157 | + } |
|
| 1158 | + |
|
| 1159 | + |
|
| 1160 | + |
|
| 1161 | + /** |
|
| 1162 | + * This is just a wrapper for the EEH_Debug_Tools::instance()->doing_it_wrong() method. |
|
| 1163 | + * doing_it_wrong() is used in those cases where a normal PHP error won't get thrown, |
|
| 1164 | + * but the code execution is done in a manner that could lead to unexpected results |
|
| 1165 | + * (i.e. running to early, or too late in WP or EE loading process). |
|
| 1166 | + * A good test for knowing whether to use this method is: |
|
| 1167 | + * 1. Is there going to be a PHP error if something isn't setup/used correctly? |
|
| 1168 | + * Yes -> use EE_Error::add_error() or throw new EE_Error() |
|
| 1169 | + * 2. If this is loaded before something else, it won't break anything, |
|
| 1170 | + * but just wont' do what its supposed to do? Yes -> use EE_Error::doing_it_wrong() |
|
| 1171 | + * |
|
| 1172 | + * @uses constant WP_DEBUG test if wp_debug is on or not |
|
| 1173 | + * @param string $function The function that was called |
|
| 1174 | + * @param string $message A message explaining what has been done incorrectly |
|
| 1175 | + * @param string $version The version of Event Espresso where the error was added |
|
| 1176 | + * @param string $applies_when a version string for when you want the doing_it_wrong notice to begin appearing |
|
| 1177 | + * for a deprecated function. This allows deprecation to occur during one version, |
|
| 1178 | + * but not have any notices appear until a later version. This allows developers |
|
| 1179 | + * extra time to update their code before notices appear. |
|
| 1180 | + * @param int $error_type |
|
| 1181 | + */ |
|
| 1182 | + public static function doing_it_wrong( |
|
| 1183 | + $function, |
|
| 1184 | + $message, |
|
| 1185 | + $version, |
|
| 1186 | + $applies_when = '', |
|
| 1187 | + $error_type = null |
|
| 1188 | + ) { |
|
| 1189 | + if (defined('WP_DEBUG') && WP_DEBUG) { |
|
| 1190 | + EEH_Debug_Tools::instance()->doing_it_wrong($function, $message, $version, $applies_when, $error_type); |
|
| 1191 | + } |
|
| 1192 | + } |
|
| 1193 | + |
|
| 1194 | + |
|
| 1195 | + |
|
| 1196 | + /** |
|
| 1197 | + * Like get_notices, but returns an array of all the notices of the given type. |
|
| 1198 | + * |
|
| 1199 | + * @return array { |
|
| 1200 | + * @type array $success all the success messages |
|
| 1201 | + * @type array $errors all the error messages |
|
| 1202 | + * @type array $attention all the attention messages |
|
| 1203 | + * } |
|
| 1204 | + */ |
|
| 1205 | + public static function get_raw_notices() |
|
| 1206 | + { |
|
| 1207 | + return self::$_espresso_notices; |
|
| 1208 | + } |
|
| 1209 | 1209 | |
| 1210 | 1210 | |
| 1211 | 1211 | |
@@ -1221,27 +1221,27 @@ discard block |
||
| 1221 | 1221 | */ |
| 1222 | 1222 | function espresso_error_enqueue_scripts() |
| 1223 | 1223 | { |
| 1224 | - // js for error handling |
|
| 1225 | - wp_register_script( |
|
| 1226 | - 'espresso_core', |
|
| 1227 | - EE_GLOBAL_ASSETS_URL . 'scripts/espresso_core.js', |
|
| 1228 | - array('jquery'), |
|
| 1229 | - EVENT_ESPRESSO_VERSION, |
|
| 1230 | - false |
|
| 1231 | - ); |
|
| 1232 | - wp_register_script( |
|
| 1233 | - 'ee_error_js', |
|
| 1234 | - EE_GLOBAL_ASSETS_URL . 'scripts/EE_Error.js', |
|
| 1235 | - array('espresso_core'), |
|
| 1236 | - EVENT_ESPRESSO_VERSION, |
|
| 1237 | - false |
|
| 1238 | - ); |
|
| 1224 | + // js for error handling |
|
| 1225 | + wp_register_script( |
|
| 1226 | + 'espresso_core', |
|
| 1227 | + EE_GLOBAL_ASSETS_URL . 'scripts/espresso_core.js', |
|
| 1228 | + array('jquery'), |
|
| 1229 | + EVENT_ESPRESSO_VERSION, |
|
| 1230 | + false |
|
| 1231 | + ); |
|
| 1232 | + wp_register_script( |
|
| 1233 | + 'ee_error_js', |
|
| 1234 | + EE_GLOBAL_ASSETS_URL . 'scripts/EE_Error.js', |
|
| 1235 | + array('espresso_core'), |
|
| 1236 | + EVENT_ESPRESSO_VERSION, |
|
| 1237 | + false |
|
| 1238 | + ); |
|
| 1239 | 1239 | } |
| 1240 | 1240 | |
| 1241 | 1241 | if (is_admin()) { |
| 1242 | - add_action('admin_enqueue_scripts', 'espresso_error_enqueue_scripts', 2); |
|
| 1242 | + add_action('admin_enqueue_scripts', 'espresso_error_enqueue_scripts', 2); |
|
| 1243 | 1243 | } else { |
| 1244 | - add_action('wp_enqueue_scripts', 'espresso_error_enqueue_scripts', 2); |
|
| 1244 | + add_action('wp_enqueue_scripts', 'espresso_error_enqueue_scripts', 2); |
|
| 1245 | 1245 | } |
| 1246 | 1246 | |
| 1247 | 1247 | |
@@ -14,12 +14,12 @@ discard block |
||
| 14 | 14 | * @param int | \EE_Event $event |
| 15 | 15 | * @return bool |
| 16 | 16 | */ |
| 17 | -function is_espresso_event( $event = NULL ) { |
|
| 18 | - if ( can_use_espresso_conditionals( __FUNCTION__ )) { |
|
| 17 | +function is_espresso_event($event = NULL) { |
|
| 18 | + if (can_use_espresso_conditionals(__FUNCTION__)) { |
|
| 19 | 19 | // extract EE_Event object from passed param regardless of what it is (within reason of course) |
| 20 | - $event = EEH_Event_View::get_event( $event ); |
|
| 20 | + $event = EEH_Event_View::get_event($event); |
|
| 21 | 21 | // do we have a valid event ? |
| 22 | - return $event instanceof EE_Event ? TRUE : FALSE; |
|
| 22 | + return $event instanceof EE_Event ? TRUE : FALSE; |
|
| 23 | 23 | } |
| 24 | 24 | return FALSE; |
| 25 | 25 | } |
@@ -31,7 +31,7 @@ discard block |
||
| 31 | 31 | * @return bool |
| 32 | 32 | */ |
| 33 | 33 | function is_espresso_event_single() { |
| 34 | - if ( can_use_espresso_conditionals( __FUNCTION__ )) { |
|
| 34 | + if (can_use_espresso_conditionals(__FUNCTION__)) { |
|
| 35 | 35 | global $wp_query; |
| 36 | 36 | // return conditionals set by CPTs |
| 37 | 37 | return $wp_query instanceof WP_Query ? $wp_query->is_espresso_event_single : FALSE; |
@@ -46,7 +46,7 @@ discard block |
||
| 46 | 46 | * @return bool |
| 47 | 47 | */ |
| 48 | 48 | function is_espresso_event_archive() { |
| 49 | - if ( can_use_espresso_conditionals( __FUNCTION__ )) { |
|
| 49 | + if (can_use_espresso_conditionals(__FUNCTION__)) { |
|
| 50 | 50 | global $wp_query; |
| 51 | 51 | return $wp_query instanceof WP_Query ? $wp_query->is_espresso_event_archive : FALSE; |
| 52 | 52 | } |
@@ -60,7 +60,7 @@ discard block |
||
| 60 | 60 | * @return bool |
| 61 | 61 | */ |
| 62 | 62 | function is_espresso_event_taxonomy() { |
| 63 | - if ( can_use_espresso_conditionals( __FUNCTION__ )) { |
|
| 63 | + if (can_use_espresso_conditionals(__FUNCTION__)) { |
|
| 64 | 64 | global $wp_query; |
| 65 | 65 | return $wp_query instanceof WP_Query ? $wp_query->is_espresso_event_taxonomy : FALSE; |
| 66 | 66 | } |
@@ -74,10 +74,10 @@ discard block |
||
| 74 | 74 | * @param int | \EE_Venue $venue |
| 75 | 75 | * @return bool |
| 76 | 76 | */ |
| 77 | -function is_espresso_venue( $venue = NULL ) { |
|
| 78 | - if ( can_use_espresso_conditionals( __FUNCTION__ )) { |
|
| 77 | +function is_espresso_venue($venue = NULL) { |
|
| 78 | + if (can_use_espresso_conditionals(__FUNCTION__)) { |
|
| 79 | 79 | // extract EE_Venue object from passed param regardless of what it is (within reason of course) |
| 80 | - $venue = EEH_Venue_View::get_venue( $venue, FALSE ); |
|
| 80 | + $venue = EEH_Venue_View::get_venue($venue, FALSE); |
|
| 81 | 81 | // do we have a valid event ? |
| 82 | 82 | return $venue instanceof EE_Venue ? TRUE : FALSE; |
| 83 | 83 | } |
@@ -91,7 +91,7 @@ discard block |
||
| 91 | 91 | * @return bool |
| 92 | 92 | */ |
| 93 | 93 | function is_espresso_venue_single() { |
| 94 | - if ( can_use_espresso_conditionals( __FUNCTION__ )) { |
|
| 94 | + if (can_use_espresso_conditionals(__FUNCTION__)) { |
|
| 95 | 95 | global $wp_query; |
| 96 | 96 | return $wp_query instanceof WP_Query ? $wp_query->is_espresso_venue_single : FALSE; |
| 97 | 97 | } |
@@ -105,7 +105,7 @@ discard block |
||
| 105 | 105 | * @return bool |
| 106 | 106 | */ |
| 107 | 107 | function is_espresso_venue_archive() { |
| 108 | - if ( can_use_espresso_conditionals( __FUNCTION__ )) { |
|
| 108 | + if (can_use_espresso_conditionals(__FUNCTION__)) { |
|
| 109 | 109 | global $wp_query; |
| 110 | 110 | return $wp_query instanceof WP_Query ? $wp_query->is_espresso_venue_archive : FALSE; |
| 111 | 111 | } |
@@ -119,7 +119,7 @@ discard block |
||
| 119 | 119 | * @return bool |
| 120 | 120 | */ |
| 121 | 121 | function is_espresso_venue_taxonomy() { |
| 122 | - if ( can_use_espresso_conditionals( __FUNCTION__ )) { |
|
| 122 | + if (can_use_espresso_conditionals(__FUNCTION__)) { |
|
| 123 | 123 | global $wp_query; |
| 124 | 124 | return $wp_query instanceof WP_Query ? $wp_query->is_espresso_venue_taxonomy : FALSE; |
| 125 | 125 | } |
@@ -133,12 +133,12 @@ discard block |
||
| 133 | 133 | * @param $conditional_tag |
| 134 | 134 | * @return bool |
| 135 | 135 | */ |
| 136 | -function can_use_espresso_conditionals( $conditional_tag ) { |
|
| 137 | - if ( ! did_action( 'AHEE__EE_System__initialize' )) { |
|
| 136 | +function can_use_espresso_conditionals($conditional_tag) { |
|
| 137 | + if ( ! did_action('AHEE__EE_System__initialize')) { |
|
| 138 | 138 | EE_Error::doing_it_wrong( |
| 139 | 139 | __FUNCTION__, |
| 140 | 140 | sprintf( |
| 141 | - __( 'The "%s" conditional tag can not be used until after the "init" hook has run, but works best when used within a theme\'s template files.','event_espresso'), |
|
| 141 | + __('The "%s" conditional tag can not be used until after the "init" hook has run, but works best when used within a theme\'s template files.', 'event_espresso'), |
|
| 142 | 142 | $conditional_tag |
| 143 | 143 | ), |
| 144 | 144 | '4.4.0' |
@@ -153,13 +153,13 @@ discard block |
||
| 153 | 153 | |
| 154 | 154 | /*************************** Event Queries ***************************/ |
| 155 | 155 | |
| 156 | -if ( ! function_exists( 'espresso_get_events' )) { |
|
| 156 | +if ( ! function_exists('espresso_get_events')) { |
|
| 157 | 157 | /** |
| 158 | 158 | * espresso_get_events |
| 159 | 159 | * @param array $params |
| 160 | 160 | * @return array |
| 161 | 161 | */ |
| 162 | - function espresso_get_events( $params = array() ) { |
|
| 162 | + function espresso_get_events($params = array()) { |
|
| 163 | 163 | //set default params |
| 164 | 164 | $default_espresso_events_params = array( |
| 165 | 165 | 'limit' => 10, |
@@ -170,18 +170,18 @@ discard block |
||
| 170 | 170 | 'sort' => 'ASC' |
| 171 | 171 | ); |
| 172 | 172 | // allow the defaults to be filtered |
| 173 | - $default_espresso_events_params = apply_filters( 'espresso_get_events__default_espresso_events_params', $default_espresso_events_params ); |
|
| 173 | + $default_espresso_events_params = apply_filters('espresso_get_events__default_espresso_events_params', $default_espresso_events_params); |
|
| 174 | 174 | // grab params and merge with defaults, then extract |
| 175 | - $params = array_merge( $default_espresso_events_params, $params ); |
|
| 175 | + $params = array_merge($default_espresso_events_params, $params); |
|
| 176 | 176 | // run the query |
| 177 | - $events_query = new EventEspresso\core\domain\services\wp_queries\EventListQuery( $params ); |
|
| 177 | + $events_query = new EventEspresso\core\domain\services\wp_queries\EventListQuery($params); |
|
| 178 | 178 | // assign results to a variable so we can return it |
| 179 | 179 | $events = $events_query->have_posts() ? $events_query->posts : array(); |
| 180 | 180 | // but first reset the query and postdata |
| 181 | 181 | wp_reset_query(); |
| 182 | 182 | wp_reset_postdata(); |
| 183 | 183 | EED_Events_Archive::remove_all_events_archive_filters(); |
| 184 | - unset( $events_query ); |
|
| 184 | + unset($events_query); |
|
| 185 | 185 | return $events; |
| 186 | 186 | } |
| 187 | 187 | } |
@@ -195,32 +195,32 @@ discard block |
||
| 195 | 195 | * espresso_load_ticket_selector |
| 196 | 196 | */ |
| 197 | 197 | function espresso_load_ticket_selector() { |
| 198 | - EE_Registry::instance()->load_file( EE_MODULES . 'ticket_selector', 'EED_Ticket_Selector', 'module' ); |
|
| 198 | + EE_Registry::instance()->load_file(EE_MODULES.'ticket_selector', 'EED_Ticket_Selector', 'module'); |
|
| 199 | 199 | } |
| 200 | 200 | |
| 201 | -if ( ! function_exists( 'espresso_ticket_selector' )) { |
|
| 201 | +if ( ! function_exists('espresso_ticket_selector')) { |
|
| 202 | 202 | /** |
| 203 | 203 | * espresso_ticket_selector |
| 204 | 204 | * @param null $event |
| 205 | 205 | */ |
| 206 | - function espresso_ticket_selector( $event = NULL ) { |
|
| 207 | - if ( ! apply_filters( 'FHEE_disable_espresso_ticket_selector', FALSE ) ) { |
|
| 206 | + function espresso_ticket_selector($event = NULL) { |
|
| 207 | + if ( ! apply_filters('FHEE_disable_espresso_ticket_selector', FALSE)) { |
|
| 208 | 208 | espresso_load_ticket_selector(); |
| 209 | - echo EED_Ticket_Selector::display_ticket_selector( $event ); |
|
| 209 | + echo EED_Ticket_Selector::display_ticket_selector($event); |
|
| 210 | 210 | } |
| 211 | 211 | } |
| 212 | 212 | } |
| 213 | 213 | |
| 214 | 214 | |
| 215 | - if ( ! function_exists( 'espresso_view_details_btn' )) { |
|
| 215 | + if ( ! function_exists('espresso_view_details_btn')) { |
|
| 216 | 216 | /** |
| 217 | 217 | * espresso_view_details_btn |
| 218 | 218 | * @param null $event |
| 219 | 219 | */ |
| 220 | - function espresso_view_details_btn( $event = NULL ) { |
|
| 221 | - if ( ! apply_filters( 'FHEE_disable_espresso_view_details_btn', FALSE ) ) { |
|
| 220 | + function espresso_view_details_btn($event = NULL) { |
|
| 221 | + if ( ! apply_filters('FHEE_disable_espresso_view_details_btn', FALSE)) { |
|
| 222 | 222 | espresso_load_ticket_selector(); |
| 223 | - echo EED_Ticket_Selector::display_ticket_selector( $event, TRUE ); |
|
| 223 | + echo EED_Ticket_Selector::display_ticket_selector($event, TRUE); |
|
| 224 | 224 | } |
| 225 | 225 | } |
| 226 | 226 | } |
@@ -230,7 +230,7 @@ discard block |
||
| 230 | 230 | |
| 231 | 231 | /*************************** EEH_Event_View ***************************/ |
| 232 | 232 | |
| 233 | -if ( ! function_exists( 'espresso_load_event_list_assets' )) { |
|
| 233 | +if ( ! function_exists('espresso_load_event_list_assets')) { |
|
| 234 | 234 | /** |
| 235 | 235 | * espresso_load_event_list_assets |
| 236 | 236 | * ensures that event list styles and scripts are loaded |
@@ -239,13 +239,13 @@ discard block |
||
| 239 | 239 | */ |
| 240 | 240 | function espresso_load_event_list_assets() { |
| 241 | 241 | $event_list = EED_Events_Archive::instance(); |
| 242 | - add_action( 'AHEE__EE_System__initialize_last', array( $event_list, 'load_event_list_assets' ), 10 ); |
|
| 243 | - add_filter( 'FHEE_enable_default_espresso_css', '__return_true' ); |
|
| 242 | + add_action('AHEE__EE_System__initialize_last', array($event_list, 'load_event_list_assets'), 10); |
|
| 243 | + add_filter('FHEE_enable_default_espresso_css', '__return_true'); |
|
| 244 | 244 | } |
| 245 | 245 | } |
| 246 | 246 | |
| 247 | 247 | |
| 248 | -if ( ! function_exists( 'espresso_event_reg_button' )) { |
|
| 248 | +if ( ! function_exists('espresso_event_reg_button')) { |
|
| 249 | 249 | /** |
| 250 | 250 | * espresso_event_reg_button |
| 251 | 251 | * returns the "Register Now" button if event is active, |
@@ -257,9 +257,9 @@ discard block |
||
| 257 | 257 | * @param bool $EVT_ID |
| 258 | 258 | * @return string |
| 259 | 259 | */ |
| 260 | - function espresso_event_reg_button( $btn_text_if_active = NULL, $btn_text_if_inactive = FALSE, $EVT_ID = FALSE ) { |
|
| 261 | - $event_status = EEH_Event_View::event_active_status( $EVT_ID ); |
|
| 262 | - switch ( $event_status ) { |
|
| 260 | + function espresso_event_reg_button($btn_text_if_active = NULL, $btn_text_if_inactive = FALSE, $EVT_ID = FALSE) { |
|
| 261 | + $event_status = EEH_Event_View::event_active_status($EVT_ID); |
|
| 262 | + switch ($event_status) { |
|
| 263 | 263 | case EE_Datetime::sold_out : |
| 264 | 264 | $btn_text = __('Sold Out', 'event_espresso'); |
| 265 | 265 | $class = 'ee-pink'; |
@@ -275,10 +275,10 @@ discard block |
||
| 275 | 275 | case EE_Datetime::upcoming : |
| 276 | 276 | case EE_Datetime::active : |
| 277 | 277 | default : |
| 278 | - $btn_text =! empty( $btn_text_if_active ) ? $btn_text_if_active : __( 'Register Now', 'event_espresso' ); |
|
| 278 | + $btn_text = ! empty($btn_text_if_active) ? $btn_text_if_active : __('Register Now', 'event_espresso'); |
|
| 279 | 279 | $class = 'ee-green'; |
| 280 | 280 | } |
| 281 | - if ( $event_status < 1 && ! empty( $btn_text_if_inactive )) { |
|
| 281 | + if ($event_status < 1 && ! empty($btn_text_if_inactive)) { |
|
| 282 | 282 | $btn_text = $btn_text_if_inactive; |
| 283 | 283 | $class = 'ee-grey'; |
| 284 | 284 | } |
@@ -292,7 +292,7 @@ discard block |
||
| 292 | 292 | |
| 293 | 293 | |
| 294 | 294 | |
| 295 | -if ( ! function_exists( 'espresso_display_ticket_selector' )) { |
|
| 295 | +if ( ! function_exists('espresso_display_ticket_selector')) { |
|
| 296 | 296 | /** |
| 297 | 297 | * espresso_display_ticket_selector |
| 298 | 298 | * whether or not to display the Ticket Selector for an event |
@@ -300,14 +300,14 @@ discard block |
||
| 300 | 300 | * @param bool $EVT_ID |
| 301 | 301 | * @return boolean |
| 302 | 302 | */ |
| 303 | - function espresso_display_ticket_selector( $EVT_ID = FALSE ) { |
|
| 304 | - return EEH_Event_View::display_ticket_selector( $EVT_ID ); |
|
| 303 | + function espresso_display_ticket_selector($EVT_ID = FALSE) { |
|
| 304 | + return EEH_Event_View::display_ticket_selector($EVT_ID); |
|
| 305 | 305 | } |
| 306 | 306 | } |
| 307 | 307 | |
| 308 | 308 | |
| 309 | 309 | |
| 310 | -if ( ! function_exists( 'espresso_event_status_banner' )) { |
|
| 310 | +if ( ! function_exists('espresso_event_status_banner')) { |
|
| 311 | 311 | /** |
| 312 | 312 | * espresso_event_status |
| 313 | 313 | * returns a banner showing the event status if it is sold out, expired, or inactive |
@@ -315,13 +315,13 @@ discard block |
||
| 315 | 315 | * @param bool $EVT_ID |
| 316 | 316 | * @return string |
| 317 | 317 | */ |
| 318 | - function espresso_event_status_banner( $EVT_ID = FALSE ) { |
|
| 319 | - return EEH_Event_View::event_status( $EVT_ID ); |
|
| 318 | + function espresso_event_status_banner($EVT_ID = FALSE) { |
|
| 319 | + return EEH_Event_View::event_status($EVT_ID); |
|
| 320 | 320 | } |
| 321 | 321 | } |
| 322 | 322 | |
| 323 | 323 | |
| 324 | -if ( ! function_exists( 'espresso_event_status' )) { |
|
| 324 | +if ( ! function_exists('espresso_event_status')) { |
|
| 325 | 325 | /** |
| 326 | 326 | * espresso_event_status |
| 327 | 327 | * returns the event status if it is sold out, expired, or inactive |
@@ -330,17 +330,17 @@ discard block |
||
| 330 | 330 | * @param bool $echo |
| 331 | 331 | * @return string |
| 332 | 332 | */ |
| 333 | - function espresso_event_status( $EVT_ID = 0, $echo = TRUE ) { |
|
| 334 | - if ( $echo ) { |
|
| 335 | - echo EEH_Event_View::event_active_status( $EVT_ID ); |
|
| 333 | + function espresso_event_status($EVT_ID = 0, $echo = TRUE) { |
|
| 334 | + if ($echo) { |
|
| 335 | + echo EEH_Event_View::event_active_status($EVT_ID); |
|
| 336 | 336 | return ''; |
| 337 | 337 | } |
| 338 | - return EEH_Event_View::event_active_status( $EVT_ID ); |
|
| 338 | + return EEH_Event_View::event_active_status($EVT_ID); |
|
| 339 | 339 | } |
| 340 | 340 | } |
| 341 | 341 | |
| 342 | 342 | |
| 343 | -if ( ! function_exists( 'espresso_event_categories' )) { |
|
| 343 | +if ( ! function_exists('espresso_event_categories')) { |
|
| 344 | 344 | /** |
| 345 | 345 | * espresso_event_categories |
| 346 | 346 | * returns the terms associated with an event |
@@ -350,17 +350,17 @@ discard block |
||
| 350 | 350 | * @param bool $echo |
| 351 | 351 | * @return string |
| 352 | 352 | */ |
| 353 | - function espresso_event_categories( $EVT_ID = 0, $hide_uncategorized = TRUE, $echo = TRUE ) { |
|
| 354 | - if ( $echo ) { |
|
| 355 | - echo EEH_Event_View::event_categories( $EVT_ID, $hide_uncategorized ); |
|
| 353 | + function espresso_event_categories($EVT_ID = 0, $hide_uncategorized = TRUE, $echo = TRUE) { |
|
| 354 | + if ($echo) { |
|
| 355 | + echo EEH_Event_View::event_categories($EVT_ID, $hide_uncategorized); |
|
| 356 | 356 | return ''; |
| 357 | 357 | } |
| 358 | - return EEH_Event_View::event_categories( $EVT_ID, $hide_uncategorized ); |
|
| 358 | + return EEH_Event_View::event_categories($EVT_ID, $hide_uncategorized); |
|
| 359 | 359 | } |
| 360 | 360 | } |
| 361 | 361 | |
| 362 | 362 | |
| 363 | -if ( ! function_exists( 'espresso_event_tickets_available' )) { |
|
| 363 | +if ( ! function_exists('espresso_event_tickets_available')) { |
|
| 364 | 364 | /** |
| 365 | 365 | * espresso_event_tickets_available |
| 366 | 366 | * returns the ticket types available for purchase for an event |
@@ -370,26 +370,26 @@ discard block |
||
| 370 | 370 | * @param bool $format |
| 371 | 371 | * @return string |
| 372 | 372 | */ |
| 373 | - function espresso_event_tickets_available( $EVT_ID = 0, $echo = TRUE, $format = TRUE ) { |
|
| 374 | - $tickets = EEH_Event_View::event_tickets_available( $EVT_ID ); |
|
| 375 | - if ( is_array( $tickets ) && ! empty( $tickets )) { |
|
| 373 | + function espresso_event_tickets_available($EVT_ID = 0, $echo = TRUE, $format = TRUE) { |
|
| 374 | + $tickets = EEH_Event_View::event_tickets_available($EVT_ID); |
|
| 375 | + if (is_array($tickets) && ! empty($tickets)) { |
|
| 376 | 376 | // if formatting then $html will be a string, else it will be an array of ticket objects |
| 377 | - $html = $format ? '<ul id="ee-event-tickets-ul-' . $EVT_ID . '" class="ee-event-tickets-ul">' : array(); |
|
| 378 | - foreach ( $tickets as $ticket ) { |
|
| 379 | - if ( $ticket instanceof EE_Ticket ) { |
|
| 380 | - if ( $format ) { |
|
| 381 | - $html .= '<li id="ee-event-tickets-li-' . $ticket->ID() . '" class="ee-event-tickets-li">'; |
|
| 382 | - $html .= $ticket->name() . ' ' . EEH_Template::format_currency( $ticket->get_ticket_total_with_taxes() ); |
|
| 377 | + $html = $format ? '<ul id="ee-event-tickets-ul-'.$EVT_ID.'" class="ee-event-tickets-ul">' : array(); |
|
| 378 | + foreach ($tickets as $ticket) { |
|
| 379 | + if ($ticket instanceof EE_Ticket) { |
|
| 380 | + if ($format) { |
|
| 381 | + $html .= '<li id="ee-event-tickets-li-'.$ticket->ID().'" class="ee-event-tickets-li">'; |
|
| 382 | + $html .= $ticket->name().' '.EEH_Template::format_currency($ticket->get_ticket_total_with_taxes()); |
|
| 383 | 383 | $html .= '</li>'; |
| 384 | 384 | } else { |
| 385 | 385 | $html[] = $ticket; |
| 386 | 386 | } |
| 387 | 387 | } |
| 388 | 388 | } |
| 389 | - if ( $format ) { |
|
| 389 | + if ($format) { |
|
| 390 | 390 | $html .= '</ul>'; |
| 391 | 391 | } |
| 392 | - if ( $echo && ! $format ) { |
|
| 392 | + if ($echo && ! $format) { |
|
| 393 | 393 | echo $html; |
| 394 | 394 | return ''; |
| 395 | 395 | } |
@@ -399,7 +399,7 @@ discard block |
||
| 399 | 399 | } |
| 400 | 400 | } |
| 401 | 401 | |
| 402 | -if ( ! function_exists( 'espresso_event_date_obj' )) { |
|
| 402 | +if ( ! function_exists('espresso_event_date_obj')) { |
|
| 403 | 403 | /** |
| 404 | 404 | * espresso_event_date_obj |
| 405 | 405 | * returns the primary date object for an event |
@@ -407,13 +407,13 @@ discard block |
||
| 407 | 407 | * @param bool $EVT_ID |
| 408 | 408 | * @return object |
| 409 | 409 | */ |
| 410 | - function espresso_event_date_obj( $EVT_ID = FALSE ) { |
|
| 411 | - return EEH_Event_View::get_primary_date_obj( $EVT_ID ); |
|
| 410 | + function espresso_event_date_obj($EVT_ID = FALSE) { |
|
| 411 | + return EEH_Event_View::get_primary_date_obj($EVT_ID); |
|
| 412 | 412 | } |
| 413 | 413 | } |
| 414 | 414 | |
| 415 | 415 | |
| 416 | -if ( ! function_exists( 'espresso_event_date' )) { |
|
| 416 | +if ( ! function_exists('espresso_event_date')) { |
|
| 417 | 417 | /** |
| 418 | 418 | * espresso_event_date |
| 419 | 419 | * returns the primary date for an event |
@@ -424,22 +424,22 @@ discard block |
||
| 424 | 424 | * @param bool $echo |
| 425 | 425 | * @return string |
| 426 | 426 | */ |
| 427 | - function espresso_event_date( $date_format = '', $time_format = '', $EVT_ID = FALSE, $echo = TRUE ) { |
|
| 428 | - $date_format = ! empty( $date_format ) ? $date_format : get_option( 'date_format' ); |
|
| 429 | - $time_format = ! empty( $time_format ) ? $time_format : get_option( 'time_format' ); |
|
| 430 | - $date_format = apply_filters( 'FHEE__espresso_event_date__date_format', $date_format ); |
|
| 431 | - $time_format = apply_filters( 'FHEE__espresso_event_date__time_format', $time_format ); |
|
| 432 | - if($echo){ |
|
| 433 | - echo EEH_Event_View::the_event_date( $date_format, $time_format, $EVT_ID ); |
|
| 427 | + function espresso_event_date($date_format = '', $time_format = '', $EVT_ID = FALSE, $echo = TRUE) { |
|
| 428 | + $date_format = ! empty($date_format) ? $date_format : get_option('date_format'); |
|
| 429 | + $time_format = ! empty($time_format) ? $time_format : get_option('time_format'); |
|
| 430 | + $date_format = apply_filters('FHEE__espresso_event_date__date_format', $date_format); |
|
| 431 | + $time_format = apply_filters('FHEE__espresso_event_date__time_format', $time_format); |
|
| 432 | + if ($echo) { |
|
| 433 | + echo EEH_Event_View::the_event_date($date_format, $time_format, $EVT_ID); |
|
| 434 | 434 | return ''; |
| 435 | 435 | } |
| 436 | - return EEH_Event_View::the_event_date( $date_format, $time_format, $EVT_ID ); |
|
| 436 | + return EEH_Event_View::the_event_date($date_format, $time_format, $EVT_ID); |
|
| 437 | 437 | |
| 438 | 438 | } |
| 439 | 439 | } |
| 440 | 440 | |
| 441 | 441 | |
| 442 | -if ( ! function_exists( 'espresso_list_of_event_dates' )) { |
|
| 442 | +if ( ! function_exists('espresso_list_of_event_dates')) { |
|
| 443 | 443 | /** |
| 444 | 444 | * espresso_list_of_event_dates |
| 445 | 445 | * returns a unordered list of dates for an event |
@@ -454,40 +454,40 @@ discard block |
||
| 454 | 454 | * @param null $limit |
| 455 | 455 | * @return string |
| 456 | 456 | */ |
| 457 | - function espresso_list_of_event_dates( $EVT_ID = 0, $date_format = '', $time_format = '', $echo = TRUE, $show_expired = NULL, $format = TRUE, $add_breaks = TRUE, $limit = NULL ) { |
|
| 458 | - $date_format = ! empty( $date_format ) ? $date_format : get_option( 'date_format' ); |
|
| 459 | - $time_format = ! empty( $time_format ) ? $time_format : get_option( 'time_format' ); |
|
| 460 | - $date_format = apply_filters( 'FHEE__espresso_list_of_event_dates__date_format', $date_format ); |
|
| 461 | - $time_format = apply_filters( 'FHEE__espresso_list_of_event_dates__time_format', $time_format ); |
|
| 462 | - $datetimes = EEH_Event_View::get_all_date_obj( $EVT_ID, $show_expired, FALSE, $limit ); |
|
| 463 | - if ( ! $format ) { |
|
| 464 | - return apply_filters( 'FHEE__espresso_list_of_event_dates__datetimes', $datetimes ); |
|
| 457 | + function espresso_list_of_event_dates($EVT_ID = 0, $date_format = '', $time_format = '', $echo = TRUE, $show_expired = NULL, $format = TRUE, $add_breaks = TRUE, $limit = NULL) { |
|
| 458 | + $date_format = ! empty($date_format) ? $date_format : get_option('date_format'); |
|
| 459 | + $time_format = ! empty($time_format) ? $time_format : get_option('time_format'); |
|
| 460 | + $date_format = apply_filters('FHEE__espresso_list_of_event_dates__date_format', $date_format); |
|
| 461 | + $time_format = apply_filters('FHEE__espresso_list_of_event_dates__time_format', $time_format); |
|
| 462 | + $datetimes = EEH_Event_View::get_all_date_obj($EVT_ID, $show_expired, FALSE, $limit); |
|
| 463 | + if ( ! $format) { |
|
| 464 | + return apply_filters('FHEE__espresso_list_of_event_dates__datetimes', $datetimes); |
|
| 465 | 465 | } |
| 466 | 466 | //d( $datetimes ); |
| 467 | - if ( is_array( $datetimes ) && ! empty( $datetimes )) { |
|
| 467 | + if (is_array($datetimes) && ! empty($datetimes)) { |
|
| 468 | 468 | global $post; |
| 469 | - $html = $format ? '<ul id="ee-event-datetimes-ul-' . $post->ID . '" class="ee-event-datetimes-ul ee-clearfix">' : ''; |
|
| 470 | - foreach ( $datetimes as $datetime ) { |
|
| 471 | - if ( $datetime instanceof EE_Datetime ) { |
|
| 472 | - $html .= '<li id="ee-event-datetimes-li-' . $datetime->ID(); |
|
| 473 | - $html .= '" class="ee-event-datetimes-li ee-event-datetimes-li-' . $datetime->get_active_status() . '">'; |
|
| 469 | + $html = $format ? '<ul id="ee-event-datetimes-ul-'.$post->ID.'" class="ee-event-datetimes-ul ee-clearfix">' : ''; |
|
| 470 | + foreach ($datetimes as $datetime) { |
|
| 471 | + if ($datetime instanceof EE_Datetime) { |
|
| 472 | + $html .= '<li id="ee-event-datetimes-li-'.$datetime->ID(); |
|
| 473 | + $html .= '" class="ee-event-datetimes-li ee-event-datetimes-li-'.$datetime->get_active_status().'">'; |
|
| 474 | 474 | $datetime_name = $datetime->name(); |
| 475 | - $html .= ! empty( $datetime_name ) ? '<strong>' . $datetime_name . '</strong>' : ''; |
|
| 476 | - $html .= ! empty( $datetime_name ) && $add_breaks ? '<br />' : ''; |
|
| 477 | - $html .= '<span class="dashicons dashicons-calendar"></span><span class="ee-event-datetimes-li-daterange">' . $datetime->date_range( $date_format ) . '</span><br/>'; |
|
| 478 | - $html .= '<span class="dashicons dashicons-clock"></span><span class="ee-event-datetimes-li-timerange">' . $datetime->time_range( $time_format ) . '</span>'; |
|
| 475 | + $html .= ! empty($datetime_name) ? '<strong>'.$datetime_name.'</strong>' : ''; |
|
| 476 | + $html .= ! empty($datetime_name) && $add_breaks ? '<br />' : ''; |
|
| 477 | + $html .= '<span class="dashicons dashicons-calendar"></span><span class="ee-event-datetimes-li-daterange">'.$datetime->date_range($date_format).'</span><br/>'; |
|
| 478 | + $html .= '<span class="dashicons dashicons-clock"></span><span class="ee-event-datetimes-li-timerange">'.$datetime->time_range($time_format).'</span>'; |
|
| 479 | 479 | $datetime_description = $datetime->description(); |
| 480 | - $html .= ! empty( $datetime_description ) && $add_breaks ? '<br />' : ''; |
|
| 481 | - $html .= ! empty( $datetime_description ) ? ' - ' . $datetime_description : ''; |
|
| 482 | - $html = apply_filters( 'FHEE__espresso_list_of_event_dates__datetime_html', $html, $datetime ); |
|
| 480 | + $html .= ! empty($datetime_description) && $add_breaks ? '<br />' : ''; |
|
| 481 | + $html .= ! empty($datetime_description) ? ' - '.$datetime_description : ''; |
|
| 482 | + $html = apply_filters('FHEE__espresso_list_of_event_dates__datetime_html', $html, $datetime); |
|
| 483 | 483 | $html .= '</li>'; |
| 484 | 484 | } |
| 485 | 485 | } |
| 486 | 486 | $html .= $format ? '</ul>' : ''; |
| 487 | 487 | } else { |
| 488 | - $html = $format ? '<p><span class="dashicons dashicons-marker pink-text"></span>' . __( 'There are no upcoming dates for this event.', 'event_espresso' ) . '</p><br/>' : ''; |
|
| 488 | + $html = $format ? '<p><span class="dashicons dashicons-marker pink-text"></span>'.__('There are no upcoming dates for this event.', 'event_espresso').'</p><br/>' : ''; |
|
| 489 | 489 | } |
| 490 | - if ( $echo ) { |
|
| 490 | + if ($echo) { |
|
| 491 | 491 | echo $html; |
| 492 | 492 | return ''; |
| 493 | 493 | } |
@@ -496,7 +496,7 @@ discard block |
||
| 496 | 496 | } |
| 497 | 497 | |
| 498 | 498 | |
| 499 | -if ( ! function_exists( 'espresso_event_end_date' )) { |
|
| 499 | +if ( ! function_exists('espresso_event_end_date')) { |
|
| 500 | 500 | /** |
| 501 | 501 | * espresso_event_end_date |
| 502 | 502 | * returns the last date for an event |
@@ -507,20 +507,20 @@ discard block |
||
| 507 | 507 | * @param bool $echo |
| 508 | 508 | * @return string |
| 509 | 509 | */ |
| 510 | - function espresso_event_end_date( $date_format = '', $time_format = '', $EVT_ID = FALSE, $echo = TRUE ) { |
|
| 511 | - $date_format = ! empty( $date_format ) ? $date_format : get_option( 'date_format' ); |
|
| 512 | - $time_format = ! empty( $time_format ) ? $time_format : get_option( 'time_format' ); |
|
| 513 | - $date_format = apply_filters( 'FHEE__espresso_event_end_date__date_format', $date_format ); |
|
| 514 | - $time_format = apply_filters( 'FHEE__espresso_event_end_date__time_format', $time_format ); |
|
| 515 | - if($echo){ |
|
| 516 | - echo EEH_Event_View::the_event_end_date( $date_format, $time_format, $EVT_ID ); |
|
| 510 | + function espresso_event_end_date($date_format = '', $time_format = '', $EVT_ID = FALSE, $echo = TRUE) { |
|
| 511 | + $date_format = ! empty($date_format) ? $date_format : get_option('date_format'); |
|
| 512 | + $time_format = ! empty($time_format) ? $time_format : get_option('time_format'); |
|
| 513 | + $date_format = apply_filters('FHEE__espresso_event_end_date__date_format', $date_format); |
|
| 514 | + $time_format = apply_filters('FHEE__espresso_event_end_date__time_format', $time_format); |
|
| 515 | + if ($echo) { |
|
| 516 | + echo EEH_Event_View::the_event_end_date($date_format, $time_format, $EVT_ID); |
|
| 517 | 517 | return ''; |
| 518 | 518 | } |
| 519 | - return EEH_Event_View::the_event_end_date( $date_format, $time_format, $EVT_ID ); |
|
| 519 | + return EEH_Event_View::the_event_end_date($date_format, $time_format, $EVT_ID); |
|
| 520 | 520 | } |
| 521 | 521 | } |
| 522 | 522 | |
| 523 | -if ( ! function_exists( 'espresso_event_date_range' )) { |
|
| 523 | +if ( ! function_exists('espresso_event_date_range')) { |
|
| 524 | 524 | /** |
| 525 | 525 | * espresso_event_date_range |
| 526 | 526 | * returns the first and last chronologically ordered dates for an event (if different) |
@@ -533,31 +533,31 @@ discard block |
||
| 533 | 533 | * @param bool $echo |
| 534 | 534 | * @return string |
| 535 | 535 | */ |
| 536 | - function espresso_event_date_range( $date_format = '', $time_format = '', $single_date_format = '', $single_time_format = '', $EVT_ID = FALSE, $echo = TRUE ) { |
|
| 536 | + function espresso_event_date_range($date_format = '', $time_format = '', $single_date_format = '', $single_time_format = '', $EVT_ID = FALSE, $echo = TRUE) { |
|
| 537 | 537 | // set and filter date and time formats when a range is returned |
| 538 | - $date_format = ! empty( $date_format ) ? $date_format : get_option( 'date_format' ); |
|
| 539 | - $date_format = apply_filters( 'FHEE__espresso_event_date_range__date_format', $date_format ); |
|
| 538 | + $date_format = ! empty($date_format) ? $date_format : get_option('date_format'); |
|
| 539 | + $date_format = apply_filters('FHEE__espresso_event_date_range__date_format', $date_format); |
|
| 540 | 540 | // get the start and end date with NO time portion |
| 541 | - $the_event_date = EEH_Event_View::the_earliest_event_date( $date_format, '', $EVT_ID ); |
|
| 542 | - $the_event_end_date = EEH_Event_View::the_latest_event_date( $date_format, '', $EVT_ID ); |
|
| 541 | + $the_event_date = EEH_Event_View::the_earliest_event_date($date_format, '', $EVT_ID); |
|
| 542 | + $the_event_end_date = EEH_Event_View::the_latest_event_date($date_format, '', $EVT_ID); |
|
| 543 | 543 | // now we can determine if date range spans more than one day |
| 544 | - if ( $the_event_date != $the_event_end_date ) { |
|
| 545 | - $time_format = ! empty( $time_format ) ? $time_format : get_option( 'time_format' ); |
|
| 546 | - $time_format = apply_filters( 'FHEE__espresso_event_date_range__time_format', $time_format ); |
|
| 544 | + if ($the_event_date != $the_event_end_date) { |
|
| 545 | + $time_format = ! empty($time_format) ? $time_format : get_option('time_format'); |
|
| 546 | + $time_format = apply_filters('FHEE__espresso_event_date_range__time_format', $time_format); |
|
| 547 | 547 | $html = sprintf( |
| 548 | - __( '%1$s - %2$s', 'event_espresso' ), |
|
| 549 | - EEH_Event_View::the_earliest_event_date( $date_format, $time_format, $EVT_ID ), |
|
| 550 | - EEH_Event_View::the_latest_event_date( $date_format, $time_format, $EVT_ID ) |
|
| 548 | + __('%1$s - %2$s', 'event_espresso'), |
|
| 549 | + EEH_Event_View::the_earliest_event_date($date_format, $time_format, $EVT_ID), |
|
| 550 | + EEH_Event_View::the_latest_event_date($date_format, $time_format, $EVT_ID) |
|
| 551 | 551 | ); |
| 552 | 552 | } else { |
| 553 | 553 | // set and filter date and time formats when only a single datetime is returned |
| 554 | - $single_date_format = ! empty( $single_date_format ) ? $single_date_format : get_option( 'date_format' ); |
|
| 555 | - $single_time_format = ! empty( $single_time_format ) ? $single_time_format : get_option( 'time_format' ); |
|
| 556 | - $single_date_format = apply_filters( 'FHEE__espresso_event_date_range__single_date_format', $single_date_format ); |
|
| 557 | - $single_time_format = apply_filters( 'FHEE__espresso_event_date_range__single_time_format', $single_time_format ); |
|
| 558 | - $html = EEH_Event_View::the_earliest_event_date( $single_date_format, $single_time_format, $EVT_ID ); |
|
| 554 | + $single_date_format = ! empty($single_date_format) ? $single_date_format : get_option('date_format'); |
|
| 555 | + $single_time_format = ! empty($single_time_format) ? $single_time_format : get_option('time_format'); |
|
| 556 | + $single_date_format = apply_filters('FHEE__espresso_event_date_range__single_date_format', $single_date_format); |
|
| 557 | + $single_time_format = apply_filters('FHEE__espresso_event_date_range__single_time_format', $single_time_format); |
|
| 558 | + $html = EEH_Event_View::the_earliest_event_date($single_date_format, $single_time_format, $EVT_ID); |
|
| 559 | 559 | } |
| 560 | - if ( $echo ) { |
|
| 560 | + if ($echo) { |
|
| 561 | 561 | echo $html; |
| 562 | 562 | return ''; |
| 563 | 563 | } |
@@ -566,7 +566,7 @@ discard block |
||
| 566 | 566 | } |
| 567 | 567 | |
| 568 | 568 | |
| 569 | -if ( ! function_exists( 'espresso_event_date_as_calendar_page' )) { |
|
| 569 | +if ( ! function_exists('espresso_event_date_as_calendar_page')) { |
|
| 570 | 570 | /** |
| 571 | 571 | * espresso_event_date_as_calendar_page |
| 572 | 572 | * returns the primary date for an event, stylized to appear as the page of a calendar |
@@ -574,15 +574,15 @@ discard block |
||
| 574 | 574 | * @param bool $EVT_ID |
| 575 | 575 | * @return string |
| 576 | 576 | */ |
| 577 | - function espresso_event_date_as_calendar_page( $EVT_ID = FALSE ) { |
|
| 578 | - EEH_Event_View::event_date_as_calendar_page( $EVT_ID ); |
|
| 577 | + function espresso_event_date_as_calendar_page($EVT_ID = FALSE) { |
|
| 578 | + EEH_Event_View::event_date_as_calendar_page($EVT_ID); |
|
| 579 | 579 | } |
| 580 | 580 | } |
| 581 | 581 | |
| 582 | 582 | |
| 583 | 583 | |
| 584 | 584 | |
| 585 | -if ( ! function_exists( 'espresso_event_link_url' )) { |
|
| 585 | +if ( ! function_exists('espresso_event_link_url')) { |
|
| 586 | 586 | /** |
| 587 | 587 | * espresso_event_link_url |
| 588 | 588 | * |
@@ -590,18 +590,18 @@ discard block |
||
| 590 | 590 | * @param bool $echo |
| 591 | 591 | * @return string |
| 592 | 592 | */ |
| 593 | - function espresso_event_link_url( $EVT_ID = 0, $echo = TRUE ) { |
|
| 594 | - if ( $echo ) { |
|
| 595 | - echo EEH_Event_View::event_link_url( $EVT_ID ); |
|
| 593 | + function espresso_event_link_url($EVT_ID = 0, $echo = TRUE) { |
|
| 594 | + if ($echo) { |
|
| 595 | + echo EEH_Event_View::event_link_url($EVT_ID); |
|
| 596 | 596 | return ''; |
| 597 | 597 | } |
| 598 | - return EEH_Event_View::event_link_url( $EVT_ID ); |
|
| 598 | + return EEH_Event_View::event_link_url($EVT_ID); |
|
| 599 | 599 | } |
| 600 | 600 | } |
| 601 | 601 | |
| 602 | 602 | |
| 603 | 603 | |
| 604 | -if ( ! function_exists( 'espresso_event_has_content_or_excerpt' )) { |
|
| 604 | +if ( ! function_exists('espresso_event_has_content_or_excerpt')) { |
|
| 605 | 605 | /** |
| 606 | 606 | * espresso_event_has_content_or_excerpt |
| 607 | 607 | * |
@@ -609,15 +609,15 @@ discard block |
||
| 609 | 609 | * @param bool $EVT_ID |
| 610 | 610 | * @return boolean |
| 611 | 611 | */ |
| 612 | - function espresso_event_has_content_or_excerpt( $EVT_ID = FALSE ) { |
|
| 613 | - return EEH_Event_View::event_has_content_or_excerpt( $EVT_ID ); |
|
| 612 | + function espresso_event_has_content_or_excerpt($EVT_ID = FALSE) { |
|
| 613 | + return EEH_Event_View::event_has_content_or_excerpt($EVT_ID); |
|
| 614 | 614 | } |
| 615 | 615 | } |
| 616 | 616 | |
| 617 | 617 | |
| 618 | 618 | |
| 619 | 619 | |
| 620 | -if ( ! function_exists( 'espresso_event_content_or_excerpt' )) { |
|
| 620 | +if ( ! function_exists('espresso_event_content_or_excerpt')) { |
|
| 621 | 621 | /** |
| 622 | 622 | * espresso_event_content_or_excerpt |
| 623 | 623 | * |
@@ -626,18 +626,18 @@ discard block |
||
| 626 | 626 | * @param bool $echo |
| 627 | 627 | * @return string |
| 628 | 628 | */ |
| 629 | - function espresso_event_content_or_excerpt( $num_words = 55, $more = NULL, $echo = TRUE ) { |
|
| 630 | - if ( $echo ) { |
|
| 631 | - echo EEH_Event_View::event_content_or_excerpt( $num_words, $more ); |
|
| 629 | + function espresso_event_content_or_excerpt($num_words = 55, $more = NULL, $echo = TRUE) { |
|
| 630 | + if ($echo) { |
|
| 631 | + echo EEH_Event_View::event_content_or_excerpt($num_words, $more); |
|
| 632 | 632 | return ''; |
| 633 | 633 | } |
| 634 | - return EEH_Event_View::event_content_or_excerpt( $num_words, $more ); |
|
| 634 | + return EEH_Event_View::event_content_or_excerpt($num_words, $more); |
|
| 635 | 635 | } |
| 636 | 636 | } |
| 637 | 637 | |
| 638 | 638 | |
| 639 | 639 | |
| 640 | -if ( ! function_exists( 'espresso_event_phone' )) { |
|
| 640 | +if ( ! function_exists('espresso_event_phone')) { |
|
| 641 | 641 | /** |
| 642 | 642 | * espresso_event_phone |
| 643 | 643 | * |
@@ -645,18 +645,18 @@ discard block |
||
| 645 | 645 | * @param bool $echo |
| 646 | 646 | * @return string |
| 647 | 647 | */ |
| 648 | - function espresso_event_phone( $EVT_ID = 0, $echo = TRUE ) { |
|
| 649 | - if ( $echo ) { |
|
| 650 | - echo EEH_Event_View::event_phone( $EVT_ID ); |
|
| 648 | + function espresso_event_phone($EVT_ID = 0, $echo = TRUE) { |
|
| 649 | + if ($echo) { |
|
| 650 | + echo EEH_Event_View::event_phone($EVT_ID); |
|
| 651 | 651 | return ''; |
| 652 | 652 | } |
| 653 | - return EEH_Event_View::event_phone( $EVT_ID ); |
|
| 653 | + return EEH_Event_View::event_phone($EVT_ID); |
|
| 654 | 654 | } |
| 655 | 655 | } |
| 656 | 656 | |
| 657 | 657 | |
| 658 | 658 | |
| 659 | -if ( ! function_exists( 'espresso_edit_event_link' )) { |
|
| 659 | +if ( ! function_exists('espresso_edit_event_link')) { |
|
| 660 | 660 | /** |
| 661 | 661 | * espresso_edit_event_link |
| 662 | 662 | * returns a link to edit an event |
@@ -665,39 +665,39 @@ discard block |
||
| 665 | 665 | * @param bool $echo |
| 666 | 666 | * @return string |
| 667 | 667 | */ |
| 668 | - function espresso_edit_event_link( $EVT_ID = 0, $echo = TRUE ) { |
|
| 669 | - if ( $echo ) { |
|
| 670 | - echo EEH_Event_View::edit_event_link( $EVT_ID ); |
|
| 668 | + function espresso_edit_event_link($EVT_ID = 0, $echo = TRUE) { |
|
| 669 | + if ($echo) { |
|
| 670 | + echo EEH_Event_View::edit_event_link($EVT_ID); |
|
| 671 | 671 | return ''; |
| 672 | 672 | } |
| 673 | - return EEH_Event_View::edit_event_link( $EVT_ID ); |
|
| 673 | + return EEH_Event_View::edit_event_link($EVT_ID); |
|
| 674 | 674 | } |
| 675 | 675 | } |
| 676 | 676 | |
| 677 | 677 | |
| 678 | -if ( ! function_exists( 'espresso_organization_name' )) { |
|
| 678 | +if ( ! function_exists('espresso_organization_name')) { |
|
| 679 | 679 | /** |
| 680 | 680 | * espresso_organization_name |
| 681 | 681 | * @param bool $echo |
| 682 | 682 | * @return string |
| 683 | 683 | */ |
| 684 | 684 | function espresso_organization_name($echo = TRUE) { |
| 685 | - if($echo){ |
|
| 686 | - echo EE_Registry::instance()->CFG->organization->get_pretty( 'name' ); |
|
| 685 | + if ($echo) { |
|
| 686 | + echo EE_Registry::instance()->CFG->organization->get_pretty('name'); |
|
| 687 | 687 | return ''; |
| 688 | 688 | } |
| 689 | - return EE_Registry::instance()->CFG->organization->get_pretty( 'name' ); |
|
| 689 | + return EE_Registry::instance()->CFG->organization->get_pretty('name'); |
|
| 690 | 690 | } |
| 691 | 691 | } |
| 692 | 692 | |
| 693 | -if ( ! function_exists( 'espresso_organization_address' )) { |
|
| 693 | +if ( ! function_exists('espresso_organization_address')) { |
|
| 694 | 694 | /** |
| 695 | 695 | * espresso_organization_address |
| 696 | 696 | * @param string $type |
| 697 | 697 | * @return string |
| 698 | 698 | */ |
| 699 | - function espresso_organization_address( $type = 'inline' ) { |
|
| 700 | - if ( EE_Registry::instance()->CFG->organization instanceof EE_Organization_Config ) { |
|
| 699 | + function espresso_organization_address($type = 'inline') { |
|
| 700 | + if (EE_Registry::instance()->CFG->organization instanceof EE_Organization_Config) { |
|
| 701 | 701 | $address = new EventEspresso\core\domain\entities\GenericAddress( |
| 702 | 702 | EE_Registry::instance()->CFG->organization->address_1, |
| 703 | 703 | EE_Registry::instance()->CFG->organization->address_2, |
@@ -706,129 +706,129 @@ discard block |
||
| 706 | 706 | EE_Registry::instance()->CFG->organization->zip, |
| 707 | 707 | EE_Registry::instance()->CFG->organization->CNT_ISO |
| 708 | 708 | ); |
| 709 | - return EEH_Address::format( $address, $type ); |
|
| 709 | + return EEH_Address::format($address, $type); |
|
| 710 | 710 | } |
| 711 | 711 | return ''; |
| 712 | 712 | } |
| 713 | 713 | } |
| 714 | 714 | |
| 715 | -if ( ! function_exists( 'espresso_organization_email' )) { |
|
| 715 | +if ( ! function_exists('espresso_organization_email')) { |
|
| 716 | 716 | /** |
| 717 | 717 | * espresso_organization_email |
| 718 | 718 | * @param bool $echo |
| 719 | 719 | * @return string |
| 720 | 720 | */ |
| 721 | - function espresso_organization_email( $echo = TRUE ) { |
|
| 722 | - if($echo){ |
|
| 723 | - echo EE_Registry::instance()->CFG->organization->get_pretty( 'email' ); |
|
| 721 | + function espresso_organization_email($echo = TRUE) { |
|
| 722 | + if ($echo) { |
|
| 723 | + echo EE_Registry::instance()->CFG->organization->get_pretty('email'); |
|
| 724 | 724 | return ''; |
| 725 | 725 | } |
| 726 | - return EE_Registry::instance()->CFG->organization->get_pretty( 'email' ); |
|
| 726 | + return EE_Registry::instance()->CFG->organization->get_pretty('email'); |
|
| 727 | 727 | } |
| 728 | 728 | } |
| 729 | 729 | |
| 730 | -if ( ! function_exists( 'espresso_organization_logo_url' )) { |
|
| 730 | +if ( ! function_exists('espresso_organization_logo_url')) { |
|
| 731 | 731 | /** |
| 732 | 732 | * espresso_organization_logo_url |
| 733 | 733 | * @param bool $echo |
| 734 | 734 | * @return string |
| 735 | 735 | */ |
| 736 | - function espresso_organization_logo_url( $echo = TRUE ) { |
|
| 737 | - if($echo){ |
|
| 738 | - echo EE_Registry::instance()->CFG->organization->get_pretty( 'logo_url' ); |
|
| 736 | + function espresso_organization_logo_url($echo = TRUE) { |
|
| 737 | + if ($echo) { |
|
| 738 | + echo EE_Registry::instance()->CFG->organization->get_pretty('logo_url'); |
|
| 739 | 739 | return ''; |
| 740 | 740 | } |
| 741 | - return EE_Registry::instance()->CFG->organization->get_pretty( 'logo_url' ); |
|
| 741 | + return EE_Registry::instance()->CFG->organization->get_pretty('logo_url'); |
|
| 742 | 742 | } |
| 743 | 743 | } |
| 744 | 744 | |
| 745 | -if ( ! function_exists( 'espresso_organization_facebook' )) { |
|
| 745 | +if ( ! function_exists('espresso_organization_facebook')) { |
|
| 746 | 746 | /** |
| 747 | 747 | * espresso_organization_facebook |
| 748 | 748 | * @param bool $echo |
| 749 | 749 | * @return string |
| 750 | 750 | */ |
| 751 | - function espresso_organization_facebook( $echo = TRUE ) { |
|
| 752 | - if($echo){ |
|
| 753 | - echo EE_Registry::instance()->CFG->organization->get_pretty( 'facebook' ); |
|
| 751 | + function espresso_organization_facebook($echo = TRUE) { |
|
| 752 | + if ($echo) { |
|
| 753 | + echo EE_Registry::instance()->CFG->organization->get_pretty('facebook'); |
|
| 754 | 754 | return ''; |
| 755 | 755 | } |
| 756 | - return EE_Registry::instance()->CFG->organization->get_pretty( 'facebook' ); |
|
| 756 | + return EE_Registry::instance()->CFG->organization->get_pretty('facebook'); |
|
| 757 | 757 | } |
| 758 | 758 | } |
| 759 | 759 | |
| 760 | -if ( ! function_exists( 'espresso_organization_twitter' )) { |
|
| 760 | +if ( ! function_exists('espresso_organization_twitter')) { |
|
| 761 | 761 | /** |
| 762 | 762 | * espresso_organization_twitter |
| 763 | 763 | * @param bool $echo |
| 764 | 764 | * @return string |
| 765 | 765 | */ |
| 766 | - function espresso_organization_twitter( $echo = TRUE ) { |
|
| 767 | - if($echo){ |
|
| 768 | - echo EE_Registry::instance()->CFG->organization->get_pretty( 'twitter' ); |
|
| 766 | + function espresso_organization_twitter($echo = TRUE) { |
|
| 767 | + if ($echo) { |
|
| 768 | + echo EE_Registry::instance()->CFG->organization->get_pretty('twitter'); |
|
| 769 | 769 | return ''; |
| 770 | 770 | } |
| 771 | - return EE_Registry::instance()->CFG->organization->get_pretty( 'twitter' ); |
|
| 771 | + return EE_Registry::instance()->CFG->organization->get_pretty('twitter'); |
|
| 772 | 772 | } |
| 773 | 773 | } |
| 774 | 774 | |
| 775 | -if ( ! function_exists( 'espresso_organization_linkedin' )) { |
|
| 775 | +if ( ! function_exists('espresso_organization_linkedin')) { |
|
| 776 | 776 | /** |
| 777 | 777 | * espresso_organization_linkedin |
| 778 | 778 | * @param bool $echo |
| 779 | 779 | * @return string |
| 780 | 780 | */ |
| 781 | - function espresso_organization_linkedin( $echo = TRUE ) { |
|
| 782 | - if($echo){ |
|
| 783 | - echo EE_Registry::instance()->CFG->organization->get_pretty( 'linkedin' ); |
|
| 781 | + function espresso_organization_linkedin($echo = TRUE) { |
|
| 782 | + if ($echo) { |
|
| 783 | + echo EE_Registry::instance()->CFG->organization->get_pretty('linkedin'); |
|
| 784 | 784 | return ''; |
| 785 | 785 | } |
| 786 | - return EE_Registry::instance()->CFG->organization->get_pretty( 'linkedin' ); |
|
| 786 | + return EE_Registry::instance()->CFG->organization->get_pretty('linkedin'); |
|
| 787 | 787 | } |
| 788 | 788 | } |
| 789 | 789 | |
| 790 | -if ( ! function_exists( 'espresso_organization_pinterest' )) { |
|
| 790 | +if ( ! function_exists('espresso_organization_pinterest')) { |
|
| 791 | 791 | /** |
| 792 | 792 | * espresso_organization_pinterest |
| 793 | 793 | * @param bool $echo |
| 794 | 794 | * @return string |
| 795 | 795 | */ |
| 796 | - function espresso_organization_pinterest( $echo = TRUE ) { |
|
| 797 | - if($echo){ |
|
| 798 | - echo EE_Registry::instance()->CFG->organization->get_pretty( 'pinterest' ); |
|
| 796 | + function espresso_organization_pinterest($echo = TRUE) { |
|
| 797 | + if ($echo) { |
|
| 798 | + echo EE_Registry::instance()->CFG->organization->get_pretty('pinterest'); |
|
| 799 | 799 | return ''; |
| 800 | 800 | } |
| 801 | - return EE_Registry::instance()->CFG->organization->get_pretty( 'pinterest' ); |
|
| 801 | + return EE_Registry::instance()->CFG->organization->get_pretty('pinterest'); |
|
| 802 | 802 | } |
| 803 | 803 | } |
| 804 | 804 | |
| 805 | -if ( ! function_exists( 'espresso_organization_google' )) { |
|
| 805 | +if ( ! function_exists('espresso_organization_google')) { |
|
| 806 | 806 | /** |
| 807 | 807 | * espresso_organization_google |
| 808 | 808 | * @param bool $echo |
| 809 | 809 | * @return string |
| 810 | 810 | */ |
| 811 | - function espresso_organization_google( $echo = TRUE ) { |
|
| 812 | - if($echo){ |
|
| 813 | - echo EE_Registry::instance()->CFG->organization->get_pretty( 'google' ); |
|
| 811 | + function espresso_organization_google($echo = TRUE) { |
|
| 812 | + if ($echo) { |
|
| 813 | + echo EE_Registry::instance()->CFG->organization->get_pretty('google'); |
|
| 814 | 814 | return ''; |
| 815 | 815 | } |
| 816 | - return EE_Registry::instance()->CFG->organization->get_pretty( 'google' ); |
|
| 816 | + return EE_Registry::instance()->CFG->organization->get_pretty('google'); |
|
| 817 | 817 | } |
| 818 | 818 | } |
| 819 | 819 | |
| 820 | -if ( ! function_exists( 'espresso_organization_instagram' )) { |
|
| 820 | +if ( ! function_exists('espresso_organization_instagram')) { |
|
| 821 | 821 | /** |
| 822 | 822 | * espresso_organization_instagram |
| 823 | 823 | * @param bool $echo |
| 824 | 824 | * @return string |
| 825 | 825 | */ |
| 826 | - function espresso_organization_instagram( $echo = TRUE ) { |
|
| 827 | - if($echo){ |
|
| 828 | - echo EE_Registry::instance()->CFG->organization->get_pretty( 'instagram' ); |
|
| 826 | + function espresso_organization_instagram($echo = TRUE) { |
|
| 827 | + if ($echo) { |
|
| 828 | + echo EE_Registry::instance()->CFG->organization->get_pretty('instagram'); |
|
| 829 | 829 | return ''; |
| 830 | 830 | } |
| 831 | - return EE_Registry::instance()->CFG->organization->get_pretty( 'instagram' ); |
|
| 831 | + return EE_Registry::instance()->CFG->organization->get_pretty('instagram'); |
|
| 832 | 832 | } |
| 833 | 833 | } |
| 834 | 834 | |
@@ -838,7 +838,7 @@ discard block |
||
| 838 | 838 | |
| 839 | 839 | |
| 840 | 840 | |
| 841 | -if ( ! function_exists( 'espresso_event_venues' )) { |
|
| 841 | +if ( ! function_exists('espresso_event_venues')) { |
|
| 842 | 842 | /** |
| 843 | 843 | * espresso_event_venues |
| 844 | 844 | * |
@@ -852,7 +852,7 @@ discard block |
||
| 852 | 852 | |
| 853 | 853 | |
| 854 | 854 | |
| 855 | -if ( ! function_exists( 'espresso_venue_id' )) { |
|
| 855 | +if ( ! function_exists('espresso_venue_id')) { |
|
| 856 | 856 | /** |
| 857 | 857 | * espresso_venue_name |
| 858 | 858 | * |
@@ -860,15 +860,15 @@ discard block |
||
| 860 | 860 | * @param int $EVT_ID |
| 861 | 861 | * @return string |
| 862 | 862 | */ |
| 863 | - function espresso_venue_id( $EVT_ID = 0 ) { |
|
| 864 | - $venue = EEH_Venue_View::get_venue( $EVT_ID ); |
|
| 863 | + function espresso_venue_id($EVT_ID = 0) { |
|
| 864 | + $venue = EEH_Venue_View::get_venue($EVT_ID); |
|
| 865 | 865 | return $venue instanceof EE_Venue ? $venue->ID() : 0; |
| 866 | 866 | } |
| 867 | 867 | } |
| 868 | 868 | |
| 869 | 869 | |
| 870 | 870 | |
| 871 | -if ( ! function_exists( 'espresso_is_venue_private' ) ) { |
|
| 871 | +if ( ! function_exists('espresso_is_venue_private')) { |
|
| 872 | 872 | /** |
| 873 | 873 | * Return whether a venue is private or not. |
| 874 | 874 | * @see EEH_Venue_View::get_venue() for more info on expected return results. |
@@ -877,45 +877,45 @@ discard block |
||
| 877 | 877 | * |
| 878 | 878 | * @return bool | null |
| 879 | 879 | */ |
| 880 | - function espresso_is_venue_private( $VNU_ID = 0 ) { |
|
| 881 | - return EEH_Venue_View::is_venue_private( $VNU_ID ); |
|
| 880 | + function espresso_is_venue_private($VNU_ID = 0) { |
|
| 881 | + return EEH_Venue_View::is_venue_private($VNU_ID); |
|
| 882 | 882 | } |
| 883 | 883 | } |
| 884 | 884 | |
| 885 | 885 | |
| 886 | 886 | |
| 887 | -if ( ! function_exists( 'espresso_venue_is_password_protected' ) ) { |
|
| 887 | +if ( ! function_exists('espresso_venue_is_password_protected')) { |
|
| 888 | 888 | /** |
| 889 | 889 | * returns true or false if a venue is password protected or not |
| 890 | 890 | * |
| 891 | 891 | * @param int $VNU_ID optional, the venue id to check. |
| 892 | 892 | * @return string |
| 893 | 893 | */ |
| 894 | - function espresso_venue_is_password_protected( $VNU_ID = 0 ) { |
|
| 895 | - EE_Registry::instance()->load_helper( 'Venue_View' ); |
|
| 896 | - return EEH_Venue_View::is_venue_password_protected( $VNU_ID ); |
|
| 894 | + function espresso_venue_is_password_protected($VNU_ID = 0) { |
|
| 895 | + EE_Registry::instance()->load_helper('Venue_View'); |
|
| 896 | + return EEH_Venue_View::is_venue_password_protected($VNU_ID); |
|
| 897 | 897 | } |
| 898 | 898 | } |
| 899 | 899 | |
| 900 | 900 | |
| 901 | 901 | |
| 902 | -if ( ! function_exists( 'espresso_password_protected_venue_form' ) ) { |
|
| 902 | +if ( ! function_exists('espresso_password_protected_venue_form')) { |
|
| 903 | 903 | /** |
| 904 | 904 | * Returns a password form if venue is password protected. |
| 905 | 905 | * |
| 906 | 906 | * @param int $VNU_ID optional, the venue id to check. |
| 907 | 907 | * @return string |
| 908 | 908 | */ |
| 909 | - function espresso_password_protected_venue_form( $VNU_ID = 0 ) { |
|
| 910 | - EE_Registry::instance()->load_helper( 'Venue_View' ); |
|
| 911 | - return EEH_Venue_View::password_protected_venue_form( $VNU_ID ); |
|
| 909 | + function espresso_password_protected_venue_form($VNU_ID = 0) { |
|
| 910 | + EE_Registry::instance()->load_helper('Venue_View'); |
|
| 911 | + return EEH_Venue_View::password_protected_venue_form($VNU_ID); |
|
| 912 | 912 | } |
| 913 | 913 | } |
| 914 | 914 | |
| 915 | 915 | |
| 916 | 916 | |
| 917 | 917 | |
| 918 | -if ( ! function_exists( 'espresso_venue_name' )) { |
|
| 918 | +if ( ! function_exists('espresso_venue_name')) { |
|
| 919 | 919 | /** |
| 920 | 920 | * espresso_venue_name |
| 921 | 921 | * |
@@ -925,19 +925,19 @@ discard block |
||
| 925 | 925 | * @param bool $echo |
| 926 | 926 | * @return string |
| 927 | 927 | */ |
| 928 | - function espresso_venue_name( $VNU_ID = 0, $link_to = 'details', $echo = TRUE ) { |
|
| 929 | - if($echo){ |
|
| 930 | - echo EEH_Venue_View::venue_name( $link_to, $VNU_ID ); |
|
| 928 | + function espresso_venue_name($VNU_ID = 0, $link_to = 'details', $echo = TRUE) { |
|
| 929 | + if ($echo) { |
|
| 930 | + echo EEH_Venue_View::venue_name($link_to, $VNU_ID); |
|
| 931 | 931 | return ''; |
| 932 | 932 | } |
| 933 | - return EEH_Venue_View::venue_name( $link_to, $VNU_ID ); |
|
| 933 | + return EEH_Venue_View::venue_name($link_to, $VNU_ID); |
|
| 934 | 934 | } |
| 935 | 935 | } |
| 936 | 936 | |
| 937 | 937 | |
| 938 | 938 | |
| 939 | 939 | |
| 940 | -if ( ! function_exists( 'espresso_venue_link' )) { |
|
| 940 | +if ( ! function_exists('espresso_venue_link')) { |
|
| 941 | 941 | /** |
| 942 | 942 | * espresso_venue_link |
| 943 | 943 | * |
@@ -946,14 +946,14 @@ discard block |
||
| 946 | 946 | * @param string $text |
| 947 | 947 | * @return string |
| 948 | 948 | */ |
| 949 | - function espresso_venue_link( $VNU_ID = 0, $text = '' ) { |
|
| 950 | - return EEH_Venue_View::venue_details_link( $VNU_ID, $text ); |
|
| 949 | + function espresso_venue_link($VNU_ID = 0, $text = '') { |
|
| 950 | + return EEH_Venue_View::venue_details_link($VNU_ID, $text); |
|
| 951 | 951 | } |
| 952 | 952 | } |
| 953 | 953 | |
| 954 | 954 | |
| 955 | 955 | |
| 956 | -if ( ! function_exists( 'espresso_venue_description' )) { |
|
| 956 | +if ( ! function_exists('espresso_venue_description')) { |
|
| 957 | 957 | /** |
| 958 | 958 | * espresso_venue_description |
| 959 | 959 | * |
@@ -962,17 +962,17 @@ discard block |
||
| 962 | 962 | * @param bool $echo |
| 963 | 963 | * @return string |
| 964 | 964 | */ |
| 965 | - function espresso_venue_description( $VNU_ID = FALSE, $echo = TRUE ) { |
|
| 966 | - if($echo){ |
|
| 967 | - echo EEH_Venue_View::venue_description( $VNU_ID ); |
|
| 965 | + function espresso_venue_description($VNU_ID = FALSE, $echo = TRUE) { |
|
| 966 | + if ($echo) { |
|
| 967 | + echo EEH_Venue_View::venue_description($VNU_ID); |
|
| 968 | 968 | return ''; |
| 969 | 969 | } |
| 970 | - return EEH_Venue_View::venue_description( $VNU_ID ); |
|
| 970 | + return EEH_Venue_View::venue_description($VNU_ID); |
|
| 971 | 971 | } |
| 972 | 972 | } |
| 973 | 973 | |
| 974 | 974 | |
| 975 | -if ( ! function_exists( 'espresso_venue_excerpt' )) { |
|
| 975 | +if ( ! function_exists('espresso_venue_excerpt')) { |
|
| 976 | 976 | /** |
| 977 | 977 | * espresso_venue_excerpt |
| 978 | 978 | * |
@@ -981,18 +981,18 @@ discard block |
||
| 981 | 981 | * @param bool $echo |
| 982 | 982 | * @return string |
| 983 | 983 | */ |
| 984 | - function espresso_venue_excerpt( $VNU_ID = 0, $echo = TRUE ) { |
|
| 985 | - if ( $echo ) { |
|
| 986 | - echo EEH_Venue_View::venue_excerpt( $VNU_ID ); |
|
| 984 | + function espresso_venue_excerpt($VNU_ID = 0, $echo = TRUE) { |
|
| 985 | + if ($echo) { |
|
| 986 | + echo EEH_Venue_View::venue_excerpt($VNU_ID); |
|
| 987 | 987 | return ''; |
| 988 | 988 | } |
| 989 | - return EEH_Venue_View::venue_excerpt( $VNU_ID ); |
|
| 989 | + return EEH_Venue_View::venue_excerpt($VNU_ID); |
|
| 990 | 990 | } |
| 991 | 991 | } |
| 992 | 992 | |
| 993 | 993 | |
| 994 | 994 | |
| 995 | -if ( ! function_exists( 'espresso_venue_categories' )) { |
|
| 995 | +if ( ! function_exists('espresso_venue_categories')) { |
|
| 996 | 996 | /** |
| 997 | 997 | * espresso_venue_categories |
| 998 | 998 | * returns the terms associated with a venue |
@@ -1002,17 +1002,17 @@ discard block |
||
| 1002 | 1002 | * @param bool $echo |
| 1003 | 1003 | * @return string |
| 1004 | 1004 | */ |
| 1005 | - function espresso_venue_categories( $VNU_ID = 0, $hide_uncategorized = TRUE, $echo = TRUE ) { |
|
| 1006 | - if ( $echo ) { |
|
| 1007 | - echo EEH_Venue_View::venue_categories( $VNU_ID, $hide_uncategorized ); |
|
| 1005 | + function espresso_venue_categories($VNU_ID = 0, $hide_uncategorized = TRUE, $echo = TRUE) { |
|
| 1006 | + if ($echo) { |
|
| 1007 | + echo EEH_Venue_View::venue_categories($VNU_ID, $hide_uncategorized); |
|
| 1008 | 1008 | return ''; |
| 1009 | 1009 | } |
| 1010 | - return EEH_Venue_View::venue_categories( $VNU_ID, $hide_uncategorized ); |
|
| 1010 | + return EEH_Venue_View::venue_categories($VNU_ID, $hide_uncategorized); |
|
| 1011 | 1011 | } |
| 1012 | 1012 | } |
| 1013 | 1013 | |
| 1014 | 1014 | |
| 1015 | -if ( ! function_exists( 'espresso_venue_address' )) { |
|
| 1015 | +if ( ! function_exists('espresso_venue_address')) { |
|
| 1016 | 1016 | /** |
| 1017 | 1017 | * espresso_venue_address |
| 1018 | 1018 | * returns a formatted block of html for displaying a venue's address |
@@ -1022,17 +1022,17 @@ discard block |
||
| 1022 | 1022 | * @param bool $echo |
| 1023 | 1023 | * @return string |
| 1024 | 1024 | */ |
| 1025 | - function espresso_venue_address( $type = 'multiline', $VNU_ID = 0, $echo = TRUE ) { |
|
| 1026 | - if ( $echo ) { |
|
| 1027 | - echo EEH_Venue_View::venue_address( $type, $VNU_ID ); |
|
| 1025 | + function espresso_venue_address($type = 'multiline', $VNU_ID = 0, $echo = TRUE) { |
|
| 1026 | + if ($echo) { |
|
| 1027 | + echo EEH_Venue_View::venue_address($type, $VNU_ID); |
|
| 1028 | 1028 | return ''; |
| 1029 | 1029 | } |
| 1030 | - return EEH_Venue_View::venue_address( $type, $VNU_ID ); |
|
| 1030 | + return EEH_Venue_View::venue_address($type, $VNU_ID); |
|
| 1031 | 1031 | } |
| 1032 | 1032 | } |
| 1033 | 1033 | |
| 1034 | 1034 | |
| 1035 | -if ( ! function_exists( 'espresso_venue_raw_address' )) { |
|
| 1035 | +if ( ! function_exists('espresso_venue_raw_address')) { |
|
| 1036 | 1036 | /** |
| 1037 | 1037 | * espresso_venue_address |
| 1038 | 1038 | * returns an UN-formatted string containing a venue's address |
@@ -1042,17 +1042,17 @@ discard block |
||
| 1042 | 1042 | * @param bool $echo |
| 1043 | 1043 | * @return string |
| 1044 | 1044 | */ |
| 1045 | - function espresso_venue_raw_address( $type = 'multiline', $VNU_ID = 0, $echo = TRUE ) { |
|
| 1046 | - if ( $echo ) { |
|
| 1047 | - echo EEH_Venue_View::venue_address( $type, $VNU_ID, FALSE, FALSE ); |
|
| 1045 | + function espresso_venue_raw_address($type = 'multiline', $VNU_ID = 0, $echo = TRUE) { |
|
| 1046 | + if ($echo) { |
|
| 1047 | + echo EEH_Venue_View::venue_address($type, $VNU_ID, FALSE, FALSE); |
|
| 1048 | 1048 | return ''; |
| 1049 | 1049 | } |
| 1050 | - return EEH_Venue_View::venue_address( $type, $VNU_ID, FALSE, FALSE ); |
|
| 1050 | + return EEH_Venue_View::venue_address($type, $VNU_ID, FALSE, FALSE); |
|
| 1051 | 1051 | } |
| 1052 | 1052 | } |
| 1053 | 1053 | |
| 1054 | 1054 | |
| 1055 | -if ( ! function_exists( 'espresso_venue_has_address' )) { |
|
| 1055 | +if ( ! function_exists('espresso_venue_has_address')) { |
|
| 1056 | 1056 | /** |
| 1057 | 1057 | * espresso_venue_has_address |
| 1058 | 1058 | * returns TRUE or FALSE if a Venue has address information |
@@ -1060,13 +1060,13 @@ discard block |
||
| 1060 | 1060 | * @param int $VNU_ID |
| 1061 | 1061 | * @return bool |
| 1062 | 1062 | */ |
| 1063 | - function espresso_venue_has_address( $VNU_ID = 0 ) { |
|
| 1064 | - return EEH_Venue_View::venue_has_address( $VNU_ID ); |
|
| 1063 | + function espresso_venue_has_address($VNU_ID = 0) { |
|
| 1064 | + return EEH_Venue_View::venue_has_address($VNU_ID); |
|
| 1065 | 1065 | } |
| 1066 | 1066 | } |
| 1067 | 1067 | |
| 1068 | 1068 | |
| 1069 | -if ( ! function_exists( 'espresso_venue_gmap' )) { |
|
| 1069 | +if ( ! function_exists('espresso_venue_gmap')) { |
|
| 1070 | 1070 | /** |
| 1071 | 1071 | * espresso_venue_gmap |
| 1072 | 1072 | * returns a google map for the venue address |
@@ -1077,17 +1077,17 @@ discard block |
||
| 1077 | 1077 | * @param bool $echo |
| 1078 | 1078 | * @return string |
| 1079 | 1079 | */ |
| 1080 | - function espresso_venue_gmap( $VNU_ID = 0, $map_ID = FALSE, $gmap = array(), $echo = TRUE ) { |
|
| 1081 | - if ( $echo ) { |
|
| 1082 | - echo EEH_Venue_View::venue_gmap( $VNU_ID, $map_ID, $gmap ); |
|
| 1080 | + function espresso_venue_gmap($VNU_ID = 0, $map_ID = FALSE, $gmap = array(), $echo = TRUE) { |
|
| 1081 | + if ($echo) { |
|
| 1082 | + echo EEH_Venue_View::venue_gmap($VNU_ID, $map_ID, $gmap); |
|
| 1083 | 1083 | return ''; |
| 1084 | 1084 | } |
| 1085 | - return EEH_Venue_View::venue_gmap( $VNU_ID, $map_ID, $gmap ); |
|
| 1085 | + return EEH_Venue_View::venue_gmap($VNU_ID, $map_ID, $gmap); |
|
| 1086 | 1086 | } |
| 1087 | 1087 | } |
| 1088 | 1088 | |
| 1089 | 1089 | |
| 1090 | -if ( ! function_exists( 'espresso_venue_phone' )) { |
|
| 1090 | +if ( ! function_exists('espresso_venue_phone')) { |
|
| 1091 | 1091 | /** |
| 1092 | 1092 | * espresso_venue_phone |
| 1093 | 1093 | * |
@@ -1095,18 +1095,18 @@ discard block |
||
| 1095 | 1095 | * @param bool $echo |
| 1096 | 1096 | * @return string |
| 1097 | 1097 | */ |
| 1098 | - function espresso_venue_phone( $VNU_ID = 0, $echo = TRUE ) { |
|
| 1099 | - if ( $echo ) { |
|
| 1100 | - echo EEH_Venue_View::venue_phone( $VNU_ID ); |
|
| 1098 | + function espresso_venue_phone($VNU_ID = 0, $echo = TRUE) { |
|
| 1099 | + if ($echo) { |
|
| 1100 | + echo EEH_Venue_View::venue_phone($VNU_ID); |
|
| 1101 | 1101 | return ''; |
| 1102 | 1102 | } |
| 1103 | - return EEH_Venue_View::venue_phone( $VNU_ID ); |
|
| 1103 | + return EEH_Venue_View::venue_phone($VNU_ID); |
|
| 1104 | 1104 | } |
| 1105 | 1105 | } |
| 1106 | 1106 | |
| 1107 | 1107 | |
| 1108 | 1108 | |
| 1109 | -if ( ! function_exists( 'espresso_venue_website' )) { |
|
| 1109 | +if ( ! function_exists('espresso_venue_website')) { |
|
| 1110 | 1110 | /** |
| 1111 | 1111 | * espresso_venue_website |
| 1112 | 1112 | * |
@@ -1114,18 +1114,18 @@ discard block |
||
| 1114 | 1114 | * @param bool $echo |
| 1115 | 1115 | * @return string |
| 1116 | 1116 | */ |
| 1117 | - function espresso_venue_website( $VNU_ID = 0, $echo = TRUE ) { |
|
| 1118 | - if ( $echo ) { |
|
| 1119 | - echo EEH_Venue_View::venue_website_link( $VNU_ID ); |
|
| 1117 | + function espresso_venue_website($VNU_ID = 0, $echo = TRUE) { |
|
| 1118 | + if ($echo) { |
|
| 1119 | + echo EEH_Venue_View::venue_website_link($VNU_ID); |
|
| 1120 | 1120 | return ''; |
| 1121 | 1121 | } |
| 1122 | - return EEH_Venue_View::venue_website_link( $VNU_ID ); |
|
| 1122 | + return EEH_Venue_View::venue_website_link($VNU_ID); |
|
| 1123 | 1123 | } |
| 1124 | 1124 | } |
| 1125 | 1125 | |
| 1126 | 1126 | |
| 1127 | 1127 | |
| 1128 | -if ( ! function_exists( 'espresso_edit_venue_link' )) { |
|
| 1128 | +if ( ! function_exists('espresso_edit_venue_link')) { |
|
| 1129 | 1129 | /** |
| 1130 | 1130 | * espresso_edit_venue_link |
| 1131 | 1131 | * |
@@ -1133,12 +1133,12 @@ discard block |
||
| 1133 | 1133 | * @param bool $echo |
| 1134 | 1134 | * @return string |
| 1135 | 1135 | */ |
| 1136 | - function espresso_edit_venue_link( $VNU_ID = 0, $echo = TRUE ) { |
|
| 1137 | - if($echo){ |
|
| 1138 | - echo EEH_Venue_View::edit_venue_link( $VNU_ID ); |
|
| 1136 | + function espresso_edit_venue_link($VNU_ID = 0, $echo = TRUE) { |
|
| 1137 | + if ($echo) { |
|
| 1138 | + echo EEH_Venue_View::edit_venue_link($VNU_ID); |
|
| 1139 | 1139 | return ''; |
| 1140 | 1140 | } |
| 1141 | - return EEH_Venue_View::edit_venue_link( $VNU_ID ); |
|
| 1141 | + return EEH_Venue_View::edit_venue_link($VNU_ID); |
|
| 1142 | 1142 | } |
| 1143 | 1143 | } |
| 1144 | 1144 | |
@@ -11,7 +11,7 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | function ee_recurse_into_array_for_display($data) { |
| 13 | 13 | if (is_object($data) || $data instanceof __PHP_Incomplete_Class) {//is_object($incomplete_class) actually returns false, hence why we check for it |
| 14 | - $data = json_decode(json_encode($data), true); |
|
| 14 | + $data = json_decode(json_encode($data), true); |
|
| 15 | 15 | } |
| 16 | 16 | if (is_array($data)) { |
| 17 | 17 | if (EEH_Array::is_associative_array($data)) { |
@@ -23,10 +23,10 @@ discard block |
||
| 23 | 23 | ?> |
| 24 | 24 | <tr> |
| 25 | 25 | <td> |
| 26 | - <?php echo $data_key;?> |
|
| 26 | + <?php echo $data_key; ?> |
|
| 27 | 27 | </td> |
| 28 | 28 | <td> |
| 29 | - <?php ee_recurse_into_array_for_display($data_values);?> |
|
| 29 | + <?php ee_recurse_into_array_for_display($data_values); ?> |
|
| 30 | 30 | </td> |
| 31 | 31 | </tr> |
| 32 | 32 | <?php |
@@ -40,19 +40,19 @@ discard block |
||
| 40 | 40 | <ul> |
| 41 | 41 | <?php |
| 42 | 42 | foreach ($data as $datum) { |
| 43 | - echo "<li>";ee_recurse_into_array_for_display($datum);echo "</li>"; |
|
| 43 | + echo "<li>"; ee_recurse_into_array_for_display($datum); echo "</li>"; |
|
| 44 | 44 | }?> |
| 45 | 45 | </ul> |
| 46 | 46 | <?php |
| 47 | 47 | } |
| 48 | - }else { |
|
| 48 | + } else { |
|
| 49 | 49 | //simple value |
| 50 | 50 | echo $data; |
| 51 | 51 | } |
| 52 | 52 | } |
| 53 | 53 | ?> |
| 54 | 54 | <h1> |
| 55 | - <?php _e("System Information", "event_espresso");?> <a href="<?php echo $download_system_status_url;?>" class="button-secondary"><?php esc_html_e( 'Download to File', 'event_espresso' );?></a> |
|
| 55 | + <?php _e("System Information", "event_espresso"); ?> <a href="<?php echo $download_system_status_url; ?>" class="button-secondary"><?php esc_html_e('Download to File', 'event_espresso'); ?></a> |
|
| 56 | 56 | </h1> |
| 57 | 57 | <div class="padding"> |
| 58 | 58 | |