@@ -456,7 +456,7 @@ |
||
| 456 | 456 | * @param EEM_Base $model |
| 457 | 457 | * @param EE_Model_Relation_Base $relation |
| 458 | 458 | * @param WP_REST_Request $request |
| 459 | - * @return array { |
|
| 459 | + * @return EE_Base_Class[] { |
|
| 460 | 460 | * @type EE_Base_Class $model_obj |
| 461 | 461 | * @type EE_Base_Class|null $other_model_obj |
| 462 | 462 | * } |
@@ -11,7 +11,6 @@ |
||
| 11 | 11 | use EventEspresso\core\services\loaders\LoaderFactory; |
| 12 | 12 | use Exception; |
| 13 | 13 | use InvalidArgumentException; |
| 14 | -use ReflectionException; |
|
| 15 | 14 | use \WP_REST_Request; |
| 16 | 15 | use \WP_REST_Response; |
| 17 | 16 | use EventEspresso\core\libraries\rest_api\Capabilities; |
@@ -134,9 +134,9 @@ discard block |
||
| 134 | 134 | { |
| 135 | 135 | Capabilities::verifyAtLeastPartialAccessTo($model, EEM_Base::caps_edit, 'create'); |
| 136 | 136 | $default_cap_to_check_for = EE_Restriction_Generator_Base::get_default_restrictions_cap(); |
| 137 | - if (! current_user_can($default_cap_to_check_for)) { |
|
| 137 | + if ( ! current_user_can($default_cap_to_check_for)) { |
|
| 138 | 138 | throw new RestException( |
| 139 | - 'rest_cannot_create_' . EEH_Inflector::pluralize_and_lower(($model->get_this_model_name())), |
|
| 139 | + 'rest_cannot_create_'.EEH_Inflector::pluralize_and_lower(($model->get_this_model_name())), |
|
| 140 | 140 | sprintf( |
| 141 | 141 | esc_html__( |
| 142 | 142 | // @codingStandardsIgnoreStart |
@@ -164,7 +164,7 @@ discard block |
||
| 164 | 164 | ); |
| 165 | 165 | $model_obj->save(); |
| 166 | 166 | $new_id = $model_obj->ID(); |
| 167 | - if (! $new_id) { |
|
| 167 | + if ( ! $new_id) { |
|
| 168 | 168 | throw new RestException( |
| 169 | 169 | 'rest_insertion_failed', |
| 170 | 170 | sprintf(__('Could not insert new %1$s', 'event_espresso'), $model->get_this_model_name()) |
@@ -186,9 +186,9 @@ discard block |
||
| 186 | 186 | { |
| 187 | 187 | Capabilities::verifyAtLeastPartialAccessTo($model, EEM_Base::caps_edit, 'edit'); |
| 188 | 188 | $default_cap_to_check_for = EE_Restriction_Generator_Base::get_default_restrictions_cap(); |
| 189 | - if (! current_user_can($default_cap_to_check_for)) { |
|
| 189 | + if ( ! current_user_can($default_cap_to_check_for)) { |
|
| 190 | 190 | throw new RestException( |
| 191 | - 'rest_cannot_edit_' . EEH_Inflector::pluralize_and_lower(($model->get_this_model_name())), |
|
| 191 | + 'rest_cannot_edit_'.EEH_Inflector::pluralize_and_lower(($model->get_this_model_name())), |
|
| 192 | 192 | sprintf( |
| 193 | 193 | esc_html__( |
| 194 | 194 | // @codingStandardsIgnoreStart |
@@ -202,7 +202,7 @@ discard block |
||
| 202 | 202 | ); |
| 203 | 203 | } |
| 204 | 204 | $obj_id = $request->get_param('id'); |
| 205 | - if (! $obj_id) { |
|
| 205 | + if ( ! $obj_id) { |
|
| 206 | 206 | throw new RestException( |
| 207 | 207 | 'rest_edit_failed', |
| 208 | 208 | sprintf(__('Could not edit %1$s', 'event_espresso'), $model->get_this_model_name()) |
@@ -215,7 +215,7 @@ discard block |
||
| 215 | 215 | true |
| 216 | 216 | ); |
| 217 | 217 | $model_obj = $model->get_one_by_ID($obj_id); |
| 218 | - if (! $model_obj instanceof EE_Base_Class) { |
|
| 218 | + if ( ! $model_obj instanceof EE_Base_Class) { |
|
| 219 | 219 | $lowercase_model_name = strtolower($model->get_this_model_name()); |
| 220 | 220 | throw new RestException( |
| 221 | 221 | sprintf('rest_%s_invalid_id', $lowercase_model_name), |
@@ -240,9 +240,9 @@ discard block |
||
| 240 | 240 | { |
| 241 | 241 | Capabilities::verifyAtLeastPartialAccessTo($model, EEM_Base::caps_delete, 'delete'); |
| 242 | 242 | $default_cap_to_check_for = EE_Restriction_Generator_Base::get_default_restrictions_cap(); |
| 243 | - if (! current_user_can($default_cap_to_check_for)) { |
|
| 243 | + if ( ! current_user_can($default_cap_to_check_for)) { |
|
| 244 | 244 | throw new RestException( |
| 245 | - 'rest_cannot_delete_' . EEH_Inflector::pluralize_and_lower(($model->get_this_model_name())), |
|
| 245 | + 'rest_cannot_delete_'.EEH_Inflector::pluralize_and_lower(($model->get_this_model_name())), |
|
| 246 | 246 | sprintf( |
| 247 | 247 | esc_html__( |
| 248 | 248 | // @codingStandardsIgnoreStart |
@@ -258,7 +258,7 @@ discard block |
||
| 258 | 258 | $obj_id = $request->get_param('id'); |
| 259 | 259 | // this is where we would apply more fine-grained caps |
| 260 | 260 | $model_obj = $model->get_one_by_ID($obj_id); |
| 261 | - if (! $model_obj instanceof EE_Base_Class) { |
|
| 261 | + if ( ! $model_obj instanceof EE_Base_Class) { |
|
| 262 | 262 | $lowercase_model_name = strtolower($model->get_this_model_name()); |
| 263 | 263 | throw new RestException( |
| 264 | 264 | sprintf('rest_%s_invalid_id', $lowercase_model_name), |
@@ -316,7 +316,7 @@ discard block |
||
| 316 | 316 | } else { |
| 317 | 317 | $raw_value = $model_obj->get_raw($field_name); |
| 318 | 318 | } |
| 319 | - $simulated_db_row[ $field_obj->get_qualified_column() ] = $field_obj->prepare_for_use_in_db($raw_value); |
|
| 319 | + $simulated_db_row[$field_obj->get_qualified_column()] = $field_obj->prepare_for_use_in_db($raw_value); |
|
| 320 | 320 | } |
| 321 | 321 | $read_controller = LoaderFactory::getLoader()->getNew('EventEspresso\core\libraries\rest_api\controllers\model\Read'); |
| 322 | 322 | $read_controller->setRequestedVersion($this->getRequestedVersion()); |
@@ -434,7 +434,7 @@ discard block |
||
| 434 | 434 | ) |
| 435 | 435 | ) |
| 436 | 436 | ); |
| 437 | - $response['join'][ strtolower($relation->get_join_model()->get_this_model_name()) ] = $this->returnModelObjAsJsonResponse($join_model_obj, $request); |
|
| 437 | + $response['join'][strtolower($relation->get_join_model()->get_this_model_name())] = $this->returnModelObjAsJsonResponse($join_model_obj, $request); |
|
| 438 | 438 | } |
| 439 | 439 | return $response; |
| 440 | 440 | } |
@@ -482,7 +482,7 @@ discard block |
||
| 482 | 482 | { |
| 483 | 483 | // This endpoint doesn't accept body parameters (it's understandable to think it might, so let developers know |
| 484 | 484 | // up-front that it doesn't.) |
| 485 | - if (!empty($request->get_body_params())) { |
|
| 485 | + if ( ! empty($request->get_body_params())) { |
|
| 486 | 486 | $body_params = $request->get_body_params(); |
| 487 | 487 | throw new RestException( |
| 488 | 488 | 'invalid_field', |
@@ -524,9 +524,9 @@ discard block |
||
| 524 | 524 | ) |
| 525 | 525 | ); |
| 526 | 526 | if ($join_model_obj instanceof EE_Base_Class) { |
| 527 | - $response['join'][ strtolower($relation->get_join_model()->get_this_model_name()) ] = $this->returnModelObjAsJsonResponse($join_model_obj, $request); |
|
| 527 | + $response['join'][strtolower($relation->get_join_model()->get_this_model_name())] = $this->returnModelObjAsJsonResponse($join_model_obj, $request); |
|
| 528 | 528 | } else { |
| 529 | - $response['join'][ strtolower($relation->get_join_model()->get_this_model_name()) ] = null; |
|
| 529 | + $response['join'][strtolower($relation->get_join_model()->get_this_model_name())] = null; |
|
| 530 | 530 | } |
| 531 | 531 | } |
| 532 | 532 | return $response; |
@@ -551,9 +551,9 @@ discard block |
||
| 551 | 551 | // Check generic caps. For now, we're only allowing access to this endpoint to full admins. |
| 552 | 552 | Capabilities::verifyAtLeastPartialAccessTo($model, EEM_Base::caps_edit, 'edit'); |
| 553 | 553 | $default_cap_to_check_for = EE_Restriction_Generator_Base::get_default_restrictions_cap(); |
| 554 | - if (! current_user_can($default_cap_to_check_for)) { |
|
| 554 | + if ( ! current_user_can($default_cap_to_check_for)) { |
|
| 555 | 555 | throw new RestException( |
| 556 | - 'rest_cannot_edit_' . EEH_Inflector::pluralize_and_lower(($model->get_this_model_name())), |
|
| 556 | + 'rest_cannot_edit_'.EEH_Inflector::pluralize_and_lower(($model->get_this_model_name())), |
|
| 557 | 557 | sprintf( |
| 558 | 558 | esc_html__( |
| 559 | 559 | // @codingStandardsIgnoreStart |
@@ -570,7 +570,7 @@ discard block |
||
| 570 | 570 | $model_obj = $this->getOneOrThrowException($model, $request->get_param('id')); |
| 571 | 571 | // For now, we require the other model object to exist too. This might be relaxed later. |
| 572 | 572 | $other_obj = $this->getOneOrThrowException($relation->get_other_model(), $request->get_param('related_id')); |
| 573 | - return array($model_obj,$other_obj); |
|
| 573 | + return array($model_obj, $other_obj); |
|
| 574 | 574 | } |
| 575 | 575 | |
| 576 | 576 | /** |
@@ -39,573 +39,573 @@ |
||
| 39 | 39 | { |
| 40 | 40 | |
| 41 | 41 | |
| 42 | - public function __construct() |
|
| 43 | - { |
|
| 44 | - parent::__construct(); |
|
| 45 | - EE_Registry::instance()->load_helper('Inflector'); |
|
| 46 | - } |
|
| 42 | + public function __construct() |
|
| 43 | + { |
|
| 44 | + parent::__construct(); |
|
| 45 | + EE_Registry::instance()->load_helper('Inflector'); |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * Handles requests to get all (or a filtered subset) of entities for a particular model |
|
| 51 | - * |
|
| 52 | - * @param WP_REST_Request $request |
|
| 53 | - * @param string $version |
|
| 54 | - * @param string $model_name |
|
| 55 | - * @return WP_REST_Response|\WP_Error |
|
| 56 | - */ |
|
| 57 | - public static function handleRequestInsert(WP_REST_Request $request, $version, $model_name) |
|
| 58 | - { |
|
| 59 | - $controller = new Write(); |
|
| 60 | - try { |
|
| 61 | - $controller->setRequestedVersion($version); |
|
| 62 | - return $controller->sendResponse( |
|
| 63 | - $controller->insert( |
|
| 64 | - $controller->getModelVersionInfo()->loadModel($model_name), |
|
| 65 | - $request |
|
| 66 | - ) |
|
| 67 | - ); |
|
| 68 | - } catch (Exception $e) { |
|
| 69 | - return $controller->sendResponse($e); |
|
| 70 | - } |
|
| 71 | - } |
|
| 49 | + /** |
|
| 50 | + * Handles requests to get all (or a filtered subset) of entities for a particular model |
|
| 51 | + * |
|
| 52 | + * @param WP_REST_Request $request |
|
| 53 | + * @param string $version |
|
| 54 | + * @param string $model_name |
|
| 55 | + * @return WP_REST_Response|\WP_Error |
|
| 56 | + */ |
|
| 57 | + public static function handleRequestInsert(WP_REST_Request $request, $version, $model_name) |
|
| 58 | + { |
|
| 59 | + $controller = new Write(); |
|
| 60 | + try { |
|
| 61 | + $controller->setRequestedVersion($version); |
|
| 62 | + return $controller->sendResponse( |
|
| 63 | + $controller->insert( |
|
| 64 | + $controller->getModelVersionInfo()->loadModel($model_name), |
|
| 65 | + $request |
|
| 66 | + ) |
|
| 67 | + ); |
|
| 68 | + } catch (Exception $e) { |
|
| 69 | + return $controller->sendResponse($e); |
|
| 70 | + } |
|
| 71 | + } |
|
| 72 | 72 | |
| 73 | 73 | |
| 74 | - /** |
|
| 75 | - * Handles a request from \WP_REST_Server to update an EE model |
|
| 76 | - * |
|
| 77 | - * @param WP_REST_Request $request |
|
| 78 | - * @param string $version |
|
| 79 | - * @param string $model_name |
|
| 80 | - * @return WP_REST_Response|\WP_Error |
|
| 81 | - */ |
|
| 82 | - public static function handleRequestUpdate(WP_REST_Request $request, $version, $model_name) |
|
| 83 | - { |
|
| 84 | - $controller = new Write(); |
|
| 85 | - try { |
|
| 86 | - $controller->setRequestedVersion($version); |
|
| 87 | - return $controller->sendResponse( |
|
| 88 | - $controller->update( |
|
| 89 | - $controller->getModelVersionInfo()->loadModel($model_name), |
|
| 90 | - $request |
|
| 91 | - ) |
|
| 92 | - ); |
|
| 93 | - } catch (Exception $e) { |
|
| 94 | - return $controller->sendResponse($e); |
|
| 95 | - } |
|
| 96 | - } |
|
| 74 | + /** |
|
| 75 | + * Handles a request from \WP_REST_Server to update an EE model |
|
| 76 | + * |
|
| 77 | + * @param WP_REST_Request $request |
|
| 78 | + * @param string $version |
|
| 79 | + * @param string $model_name |
|
| 80 | + * @return WP_REST_Response|\WP_Error |
|
| 81 | + */ |
|
| 82 | + public static function handleRequestUpdate(WP_REST_Request $request, $version, $model_name) |
|
| 83 | + { |
|
| 84 | + $controller = new Write(); |
|
| 85 | + try { |
|
| 86 | + $controller->setRequestedVersion($version); |
|
| 87 | + return $controller->sendResponse( |
|
| 88 | + $controller->update( |
|
| 89 | + $controller->getModelVersionInfo()->loadModel($model_name), |
|
| 90 | + $request |
|
| 91 | + ) |
|
| 92 | + ); |
|
| 93 | + } catch (Exception $e) { |
|
| 94 | + return $controller->sendResponse($e); |
|
| 95 | + } |
|
| 96 | + } |
|
| 97 | 97 | |
| 98 | 98 | |
| 99 | - /** |
|
| 100 | - * Deletes a single model object and returns it. Unless |
|
| 101 | - * |
|
| 102 | - * @param WP_REST_Request $request |
|
| 103 | - * @param string $version |
|
| 104 | - * @param string $model_name |
|
| 105 | - * @return WP_REST_Response|\WP_Error |
|
| 106 | - */ |
|
| 107 | - public static function handleRequestDelete(WP_REST_Request $request, $version, $model_name) |
|
| 108 | - { |
|
| 109 | - $controller = new Write(); |
|
| 110 | - try { |
|
| 111 | - $controller->setRequestedVersion($version); |
|
| 112 | - return $controller->sendResponse( |
|
| 113 | - $controller->delete( |
|
| 114 | - $controller->getModelVersionInfo()->loadModel($model_name), |
|
| 115 | - $request |
|
| 116 | - ) |
|
| 117 | - ); |
|
| 118 | - } catch (Exception $e) { |
|
| 119 | - return $controller->sendResponse($e); |
|
| 120 | - } |
|
| 121 | - } |
|
| 99 | + /** |
|
| 100 | + * Deletes a single model object and returns it. Unless |
|
| 101 | + * |
|
| 102 | + * @param WP_REST_Request $request |
|
| 103 | + * @param string $version |
|
| 104 | + * @param string $model_name |
|
| 105 | + * @return WP_REST_Response|\WP_Error |
|
| 106 | + */ |
|
| 107 | + public static function handleRequestDelete(WP_REST_Request $request, $version, $model_name) |
|
| 108 | + { |
|
| 109 | + $controller = new Write(); |
|
| 110 | + try { |
|
| 111 | + $controller->setRequestedVersion($version); |
|
| 112 | + return $controller->sendResponse( |
|
| 113 | + $controller->delete( |
|
| 114 | + $controller->getModelVersionInfo()->loadModel($model_name), |
|
| 115 | + $request |
|
| 116 | + ) |
|
| 117 | + ); |
|
| 118 | + } catch (Exception $e) { |
|
| 119 | + return $controller->sendResponse($e); |
|
| 120 | + } |
|
| 121 | + } |
|
| 122 | 122 | |
| 123 | 123 | |
| 124 | - /** |
|
| 125 | - * Inserts a new model object according to the $request |
|
| 126 | - * |
|
| 127 | - * @param EEM_Base $model |
|
| 128 | - * @param WP_REST_Request $request |
|
| 129 | - * @return array |
|
| 130 | - * @throws EE_Error |
|
| 131 | - * @throws RestException |
|
| 132 | - */ |
|
| 133 | - public function insert(EEM_Base $model, WP_REST_Request $request) |
|
| 134 | - { |
|
| 135 | - Capabilities::verifyAtLeastPartialAccessTo($model, EEM_Base::caps_edit, 'create'); |
|
| 136 | - $default_cap_to_check_for = EE_Restriction_Generator_Base::get_default_restrictions_cap(); |
|
| 137 | - if (! current_user_can($default_cap_to_check_for)) { |
|
| 138 | - throw new RestException( |
|
| 139 | - 'rest_cannot_create_' . EEH_Inflector::pluralize_and_lower(($model->get_this_model_name())), |
|
| 140 | - sprintf( |
|
| 141 | - esc_html__( |
|
| 142 | - // @codingStandardsIgnoreStart |
|
| 143 | - 'For now, only those with the admin capability to "%1$s" are allowed to use the REST API to insert data into Event Espresso.', |
|
| 144 | - // @codingStandardsIgnoreEnd |
|
| 145 | - 'event_espresso' |
|
| 146 | - ), |
|
| 147 | - $default_cap_to_check_for |
|
| 148 | - ), |
|
| 149 | - array('status' => 403) |
|
| 150 | - ); |
|
| 151 | - } |
|
| 152 | - $submitted_json_data = array_merge((array) $request->get_body_params(), (array) $request->get_json_params()); |
|
| 153 | - $model_data = ModelDataTranslator::prepareConditionsQueryParamsForModels( |
|
| 154 | - $submitted_json_data, |
|
| 155 | - $model, |
|
| 156 | - $this->getModelVersionInfo()->requestedVersion(), |
|
| 157 | - true |
|
| 158 | - ); |
|
| 159 | - $model_obj = EE_Registry::instance()->load_class( |
|
| 160 | - $model->get_this_model_name(), |
|
| 161 | - array($model_data, $model->get_timezone()), |
|
| 162 | - false, |
|
| 163 | - false |
|
| 164 | - ); |
|
| 165 | - $model_obj->save(); |
|
| 166 | - $new_id = $model_obj->ID(); |
|
| 167 | - if (! $new_id) { |
|
| 168 | - throw new RestException( |
|
| 169 | - 'rest_insertion_failed', |
|
| 170 | - sprintf(__('Could not insert new %1$s', 'event_espresso'), $model->get_this_model_name()) |
|
| 171 | - ); |
|
| 172 | - } |
|
| 173 | - return $this->returnModelObjAsJsonResponse($model_obj, $request); |
|
| 174 | - } |
|
| 124 | + /** |
|
| 125 | + * Inserts a new model object according to the $request |
|
| 126 | + * |
|
| 127 | + * @param EEM_Base $model |
|
| 128 | + * @param WP_REST_Request $request |
|
| 129 | + * @return array |
|
| 130 | + * @throws EE_Error |
|
| 131 | + * @throws RestException |
|
| 132 | + */ |
|
| 133 | + public function insert(EEM_Base $model, WP_REST_Request $request) |
|
| 134 | + { |
|
| 135 | + Capabilities::verifyAtLeastPartialAccessTo($model, EEM_Base::caps_edit, 'create'); |
|
| 136 | + $default_cap_to_check_for = EE_Restriction_Generator_Base::get_default_restrictions_cap(); |
|
| 137 | + if (! current_user_can($default_cap_to_check_for)) { |
|
| 138 | + throw new RestException( |
|
| 139 | + 'rest_cannot_create_' . EEH_Inflector::pluralize_and_lower(($model->get_this_model_name())), |
|
| 140 | + sprintf( |
|
| 141 | + esc_html__( |
|
| 142 | + // @codingStandardsIgnoreStart |
|
| 143 | + 'For now, only those with the admin capability to "%1$s" are allowed to use the REST API to insert data into Event Espresso.', |
|
| 144 | + // @codingStandardsIgnoreEnd |
|
| 145 | + 'event_espresso' |
|
| 146 | + ), |
|
| 147 | + $default_cap_to_check_for |
|
| 148 | + ), |
|
| 149 | + array('status' => 403) |
|
| 150 | + ); |
|
| 151 | + } |
|
| 152 | + $submitted_json_data = array_merge((array) $request->get_body_params(), (array) $request->get_json_params()); |
|
| 153 | + $model_data = ModelDataTranslator::prepareConditionsQueryParamsForModels( |
|
| 154 | + $submitted_json_data, |
|
| 155 | + $model, |
|
| 156 | + $this->getModelVersionInfo()->requestedVersion(), |
|
| 157 | + true |
|
| 158 | + ); |
|
| 159 | + $model_obj = EE_Registry::instance()->load_class( |
|
| 160 | + $model->get_this_model_name(), |
|
| 161 | + array($model_data, $model->get_timezone()), |
|
| 162 | + false, |
|
| 163 | + false |
|
| 164 | + ); |
|
| 165 | + $model_obj->save(); |
|
| 166 | + $new_id = $model_obj->ID(); |
|
| 167 | + if (! $new_id) { |
|
| 168 | + throw new RestException( |
|
| 169 | + 'rest_insertion_failed', |
|
| 170 | + sprintf(__('Could not insert new %1$s', 'event_espresso'), $model->get_this_model_name()) |
|
| 171 | + ); |
|
| 172 | + } |
|
| 173 | + return $this->returnModelObjAsJsonResponse($model_obj, $request); |
|
| 174 | + } |
|
| 175 | 175 | |
| 176 | 176 | |
| 177 | - /** |
|
| 178 | - * Updates an existing model object according to the $request |
|
| 179 | - * |
|
| 180 | - * @param EEM_Base $model |
|
| 181 | - * @param WP_REST_Request $request |
|
| 182 | - * @return array |
|
| 183 | - * @throws EE_Error |
|
| 184 | - */ |
|
| 185 | - public function update(EEM_Base $model, WP_REST_Request $request) |
|
| 186 | - { |
|
| 187 | - Capabilities::verifyAtLeastPartialAccessTo($model, EEM_Base::caps_edit, 'edit'); |
|
| 188 | - $default_cap_to_check_for = EE_Restriction_Generator_Base::get_default_restrictions_cap(); |
|
| 189 | - if (! current_user_can($default_cap_to_check_for)) { |
|
| 190 | - throw new RestException( |
|
| 191 | - 'rest_cannot_edit_' . EEH_Inflector::pluralize_and_lower(($model->get_this_model_name())), |
|
| 192 | - sprintf( |
|
| 193 | - esc_html__( |
|
| 194 | - // @codingStandardsIgnoreStart |
|
| 195 | - 'For now, only those with the admin capability to "%1$s" are allowed to use the REST API to update data into Event Espresso.', |
|
| 196 | - // @codingStandardsIgnoreEnd |
|
| 197 | - 'event_espresso' |
|
| 198 | - ), |
|
| 199 | - $default_cap_to_check_for |
|
| 200 | - ), |
|
| 201 | - array('status' => 403) |
|
| 202 | - ); |
|
| 203 | - } |
|
| 204 | - $obj_id = $request->get_param('id'); |
|
| 205 | - if (! $obj_id) { |
|
| 206 | - throw new RestException( |
|
| 207 | - 'rest_edit_failed', |
|
| 208 | - sprintf(__('Could not edit %1$s', 'event_espresso'), $model->get_this_model_name()) |
|
| 209 | - ); |
|
| 210 | - } |
|
| 211 | - $model_data = ModelDataTranslator::prepareConditionsQueryParamsForModels( |
|
| 212 | - $this->getBodyParams($request), |
|
| 213 | - $model, |
|
| 214 | - $this->getModelVersionInfo()->requestedVersion(), |
|
| 215 | - true |
|
| 216 | - ); |
|
| 217 | - $model_obj = $model->get_one_by_ID($obj_id); |
|
| 218 | - if (! $model_obj instanceof EE_Base_Class) { |
|
| 219 | - $lowercase_model_name = strtolower($model->get_this_model_name()); |
|
| 220 | - throw new RestException( |
|
| 221 | - sprintf('rest_%s_invalid_id', $lowercase_model_name), |
|
| 222 | - sprintf(__('Invalid %s ID.', 'event_espresso'), $lowercase_model_name), |
|
| 223 | - array('status' => 404) |
|
| 224 | - ); |
|
| 225 | - } |
|
| 226 | - $model_obj->save($model_data); |
|
| 227 | - return $this->returnModelObjAsJsonResponse($model_obj, $request); |
|
| 228 | - } |
|
| 177 | + /** |
|
| 178 | + * Updates an existing model object according to the $request |
|
| 179 | + * |
|
| 180 | + * @param EEM_Base $model |
|
| 181 | + * @param WP_REST_Request $request |
|
| 182 | + * @return array |
|
| 183 | + * @throws EE_Error |
|
| 184 | + */ |
|
| 185 | + public function update(EEM_Base $model, WP_REST_Request $request) |
|
| 186 | + { |
|
| 187 | + Capabilities::verifyAtLeastPartialAccessTo($model, EEM_Base::caps_edit, 'edit'); |
|
| 188 | + $default_cap_to_check_for = EE_Restriction_Generator_Base::get_default_restrictions_cap(); |
|
| 189 | + if (! current_user_can($default_cap_to_check_for)) { |
|
| 190 | + throw new RestException( |
|
| 191 | + 'rest_cannot_edit_' . EEH_Inflector::pluralize_and_lower(($model->get_this_model_name())), |
|
| 192 | + sprintf( |
|
| 193 | + esc_html__( |
|
| 194 | + // @codingStandardsIgnoreStart |
|
| 195 | + 'For now, only those with the admin capability to "%1$s" are allowed to use the REST API to update data into Event Espresso.', |
|
| 196 | + // @codingStandardsIgnoreEnd |
|
| 197 | + 'event_espresso' |
|
| 198 | + ), |
|
| 199 | + $default_cap_to_check_for |
|
| 200 | + ), |
|
| 201 | + array('status' => 403) |
|
| 202 | + ); |
|
| 203 | + } |
|
| 204 | + $obj_id = $request->get_param('id'); |
|
| 205 | + if (! $obj_id) { |
|
| 206 | + throw new RestException( |
|
| 207 | + 'rest_edit_failed', |
|
| 208 | + sprintf(__('Could not edit %1$s', 'event_espresso'), $model->get_this_model_name()) |
|
| 209 | + ); |
|
| 210 | + } |
|
| 211 | + $model_data = ModelDataTranslator::prepareConditionsQueryParamsForModels( |
|
| 212 | + $this->getBodyParams($request), |
|
| 213 | + $model, |
|
| 214 | + $this->getModelVersionInfo()->requestedVersion(), |
|
| 215 | + true |
|
| 216 | + ); |
|
| 217 | + $model_obj = $model->get_one_by_ID($obj_id); |
|
| 218 | + if (! $model_obj instanceof EE_Base_Class) { |
|
| 219 | + $lowercase_model_name = strtolower($model->get_this_model_name()); |
|
| 220 | + throw new RestException( |
|
| 221 | + sprintf('rest_%s_invalid_id', $lowercase_model_name), |
|
| 222 | + sprintf(__('Invalid %s ID.', 'event_espresso'), $lowercase_model_name), |
|
| 223 | + array('status' => 404) |
|
| 224 | + ); |
|
| 225 | + } |
|
| 226 | + $model_obj->save($model_data); |
|
| 227 | + return $this->returnModelObjAsJsonResponse($model_obj, $request); |
|
| 228 | + } |
|
| 229 | 229 | |
| 230 | 230 | |
| 231 | - /** |
|
| 232 | - * Updates an existing model object according to the $request |
|
| 233 | - * |
|
| 234 | - * @param EEM_Base $model |
|
| 235 | - * @param WP_REST_Request $request |
|
| 236 | - * @return array of either the soft-deleted item, or |
|
| 237 | - * @throws EE_Error |
|
| 238 | - */ |
|
| 239 | - public function delete(EEM_Base $model, WP_REST_Request $request) |
|
| 240 | - { |
|
| 241 | - Capabilities::verifyAtLeastPartialAccessTo($model, EEM_Base::caps_delete, 'delete'); |
|
| 242 | - $default_cap_to_check_for = EE_Restriction_Generator_Base::get_default_restrictions_cap(); |
|
| 243 | - if (! current_user_can($default_cap_to_check_for)) { |
|
| 244 | - throw new RestException( |
|
| 245 | - 'rest_cannot_delete_' . EEH_Inflector::pluralize_and_lower(($model->get_this_model_name())), |
|
| 246 | - sprintf( |
|
| 247 | - esc_html__( |
|
| 248 | - // @codingStandardsIgnoreStart |
|
| 249 | - 'For now, only those with the admin capability to "%1$s" are allowed to use the REST API to delete data into Event Espresso.', |
|
| 250 | - // @codingStandardsIgnoreEnd |
|
| 251 | - 'event_espresso' |
|
| 252 | - ), |
|
| 253 | - $default_cap_to_check_for |
|
| 254 | - ), |
|
| 255 | - array('status' => 403) |
|
| 256 | - ); |
|
| 257 | - } |
|
| 258 | - $obj_id = $request->get_param('id'); |
|
| 259 | - // this is where we would apply more fine-grained caps |
|
| 260 | - $model_obj = $model->get_one_by_ID($obj_id); |
|
| 261 | - if (! $model_obj instanceof EE_Base_Class) { |
|
| 262 | - $lowercase_model_name = strtolower($model->get_this_model_name()); |
|
| 263 | - throw new RestException( |
|
| 264 | - sprintf('rest_%s_invalid_id', $lowercase_model_name), |
|
| 265 | - sprintf(__('Invalid %s ID.', 'event_espresso'), $lowercase_model_name), |
|
| 266 | - array('status' => 404) |
|
| 267 | - ); |
|
| 268 | - } |
|
| 269 | - $requested_permanent_delete = filter_var($request->get_param('force'), FILTER_VALIDATE_BOOLEAN); |
|
| 270 | - $requested_allow_blocking = filter_var($request->get_param('allow_blocking'), FILTER_VALIDATE_BOOLEAN); |
|
| 271 | - if ($requested_permanent_delete) { |
|
| 272 | - $previous = $this->returnModelObjAsJsonResponse($model_obj, $request); |
|
| 273 | - $deleted = (bool) $model->delete_permanently_by_ID($obj_id, $requested_allow_blocking); |
|
| 274 | - return array( |
|
| 275 | - 'deleted' => $deleted, |
|
| 276 | - 'previous' => $previous, |
|
| 277 | - ); |
|
| 278 | - } else { |
|
| 279 | - if ($model instanceof EEM_Soft_Delete_Base) { |
|
| 280 | - $model->delete_by_ID($obj_id, $requested_allow_blocking); |
|
| 281 | - return $this->returnModelObjAsJsonResponse($model_obj, $request); |
|
| 282 | - } else { |
|
| 283 | - throw new RestException( |
|
| 284 | - 'rest_trash_not_supported', |
|
| 285 | - 501, |
|
| 286 | - sprintf( |
|
| 287 | - esc_html__('%1$s do not support trashing. Set force=1 to delete.', 'event_espresso'), |
|
| 288 | - EEH_Inflector::pluralize($model->get_this_model_name()) |
|
| 289 | - ) |
|
| 290 | - ); |
|
| 291 | - } |
|
| 292 | - } |
|
| 293 | - } |
|
| 231 | + /** |
|
| 232 | + * Updates an existing model object according to the $request |
|
| 233 | + * |
|
| 234 | + * @param EEM_Base $model |
|
| 235 | + * @param WP_REST_Request $request |
|
| 236 | + * @return array of either the soft-deleted item, or |
|
| 237 | + * @throws EE_Error |
|
| 238 | + */ |
|
| 239 | + public function delete(EEM_Base $model, WP_REST_Request $request) |
|
| 240 | + { |
|
| 241 | + Capabilities::verifyAtLeastPartialAccessTo($model, EEM_Base::caps_delete, 'delete'); |
|
| 242 | + $default_cap_to_check_for = EE_Restriction_Generator_Base::get_default_restrictions_cap(); |
|
| 243 | + if (! current_user_can($default_cap_to_check_for)) { |
|
| 244 | + throw new RestException( |
|
| 245 | + 'rest_cannot_delete_' . EEH_Inflector::pluralize_and_lower(($model->get_this_model_name())), |
|
| 246 | + sprintf( |
|
| 247 | + esc_html__( |
|
| 248 | + // @codingStandardsIgnoreStart |
|
| 249 | + 'For now, only those with the admin capability to "%1$s" are allowed to use the REST API to delete data into Event Espresso.', |
|
| 250 | + // @codingStandardsIgnoreEnd |
|
| 251 | + 'event_espresso' |
|
| 252 | + ), |
|
| 253 | + $default_cap_to_check_for |
|
| 254 | + ), |
|
| 255 | + array('status' => 403) |
|
| 256 | + ); |
|
| 257 | + } |
|
| 258 | + $obj_id = $request->get_param('id'); |
|
| 259 | + // this is where we would apply more fine-grained caps |
|
| 260 | + $model_obj = $model->get_one_by_ID($obj_id); |
|
| 261 | + if (! $model_obj instanceof EE_Base_Class) { |
|
| 262 | + $lowercase_model_name = strtolower($model->get_this_model_name()); |
|
| 263 | + throw new RestException( |
|
| 264 | + sprintf('rest_%s_invalid_id', $lowercase_model_name), |
|
| 265 | + sprintf(__('Invalid %s ID.', 'event_espresso'), $lowercase_model_name), |
|
| 266 | + array('status' => 404) |
|
| 267 | + ); |
|
| 268 | + } |
|
| 269 | + $requested_permanent_delete = filter_var($request->get_param('force'), FILTER_VALIDATE_BOOLEAN); |
|
| 270 | + $requested_allow_blocking = filter_var($request->get_param('allow_blocking'), FILTER_VALIDATE_BOOLEAN); |
|
| 271 | + if ($requested_permanent_delete) { |
|
| 272 | + $previous = $this->returnModelObjAsJsonResponse($model_obj, $request); |
|
| 273 | + $deleted = (bool) $model->delete_permanently_by_ID($obj_id, $requested_allow_blocking); |
|
| 274 | + return array( |
|
| 275 | + 'deleted' => $deleted, |
|
| 276 | + 'previous' => $previous, |
|
| 277 | + ); |
|
| 278 | + } else { |
|
| 279 | + if ($model instanceof EEM_Soft_Delete_Base) { |
|
| 280 | + $model->delete_by_ID($obj_id, $requested_allow_blocking); |
|
| 281 | + return $this->returnModelObjAsJsonResponse($model_obj, $request); |
|
| 282 | + } else { |
|
| 283 | + throw new RestException( |
|
| 284 | + 'rest_trash_not_supported', |
|
| 285 | + 501, |
|
| 286 | + sprintf( |
|
| 287 | + esc_html__('%1$s do not support trashing. Set force=1 to delete.', 'event_espresso'), |
|
| 288 | + EEH_Inflector::pluralize($model->get_this_model_name()) |
|
| 289 | + ) |
|
| 290 | + ); |
|
| 291 | + } |
|
| 292 | + } |
|
| 293 | + } |
|
| 294 | 294 | |
| 295 | 295 | |
| 296 | - /** |
|
| 297 | - * Returns an array ready to be converted into a JSON response, based solely on the model object |
|
| 298 | - * |
|
| 299 | - * @param EE_Base_Class $model_obj |
|
| 300 | - * @param WP_REST_Request $request |
|
| 301 | - * @return array ready for a response |
|
| 302 | - */ |
|
| 303 | - protected function returnModelObjAsJsonResponse(EE_Base_Class $model_obj, WP_REST_Request $request) |
|
| 304 | - { |
|
| 305 | - $model = $model_obj->get_model(); |
|
| 306 | - // create an array exactly like the wpdb results row, |
|
| 307 | - // so we can pass it to controllers/model/Read::create_entity_from_wpdb_result() |
|
| 308 | - $simulated_db_row = array(); |
|
| 309 | - foreach ($model->field_settings(true) as $field_name => $field_obj) { |
|
| 310 | - // we need to reconstruct the normal wpdb results, including the db-only fields |
|
| 311 | - // like a secondary table's primary key. The models expect those (but don't care what value they have) |
|
| 312 | - if ($field_obj instanceof EE_DB_Only_Field_Base) { |
|
| 313 | - $raw_value = true; |
|
| 314 | - } elseif ($field_obj instanceof EE_Datetime_Field) { |
|
| 315 | - $raw_value = $model_obj->get_DateTime_object($field_name); |
|
| 316 | - } else { |
|
| 317 | - $raw_value = $model_obj->get_raw($field_name); |
|
| 318 | - } |
|
| 319 | - $simulated_db_row[ $field_obj->get_qualified_column() ] = $field_obj->prepare_for_use_in_db($raw_value); |
|
| 320 | - } |
|
| 321 | - $read_controller = LoaderFactory::getLoader()->getNew('EventEspresso\core\libraries\rest_api\controllers\model\Read'); |
|
| 322 | - $read_controller->setRequestedVersion($this->getRequestedVersion()); |
|
| 323 | - // the simulates request really doesn't need any info downstream |
|
| 324 | - $simulated_request = new WP_REST_Request('GET'); |
|
| 325 | - // set the caps context on the simulated according to the original request. |
|
| 326 | - switch ($request->get_method()) { |
|
| 327 | - case 'POST': |
|
| 328 | - case 'PUT': |
|
| 329 | - $caps_context = EEM_Base::caps_edit; |
|
| 330 | - break; |
|
| 331 | - case 'DELETE': |
|
| 332 | - $caps_context = EEM_Base::caps_delete; |
|
| 333 | - break; |
|
| 334 | - default: |
|
| 335 | - $caps_context = EEM_Base::caps_read_admin; |
|
| 336 | - } |
|
| 337 | - $simulated_request->set_param('caps', $caps_context); |
|
| 338 | - return $read_controller->createEntityFromWpdbResult( |
|
| 339 | - $model_obj->get_model(), |
|
| 340 | - $simulated_db_row, |
|
| 341 | - $simulated_request |
|
| 342 | - ); |
|
| 343 | - } |
|
| 296 | + /** |
|
| 297 | + * Returns an array ready to be converted into a JSON response, based solely on the model object |
|
| 298 | + * |
|
| 299 | + * @param EE_Base_Class $model_obj |
|
| 300 | + * @param WP_REST_Request $request |
|
| 301 | + * @return array ready for a response |
|
| 302 | + */ |
|
| 303 | + protected function returnModelObjAsJsonResponse(EE_Base_Class $model_obj, WP_REST_Request $request) |
|
| 304 | + { |
|
| 305 | + $model = $model_obj->get_model(); |
|
| 306 | + // create an array exactly like the wpdb results row, |
|
| 307 | + // so we can pass it to controllers/model/Read::create_entity_from_wpdb_result() |
|
| 308 | + $simulated_db_row = array(); |
|
| 309 | + foreach ($model->field_settings(true) as $field_name => $field_obj) { |
|
| 310 | + // we need to reconstruct the normal wpdb results, including the db-only fields |
|
| 311 | + // like a secondary table's primary key. The models expect those (but don't care what value they have) |
|
| 312 | + if ($field_obj instanceof EE_DB_Only_Field_Base) { |
|
| 313 | + $raw_value = true; |
|
| 314 | + } elseif ($field_obj instanceof EE_Datetime_Field) { |
|
| 315 | + $raw_value = $model_obj->get_DateTime_object($field_name); |
|
| 316 | + } else { |
|
| 317 | + $raw_value = $model_obj->get_raw($field_name); |
|
| 318 | + } |
|
| 319 | + $simulated_db_row[ $field_obj->get_qualified_column() ] = $field_obj->prepare_for_use_in_db($raw_value); |
|
| 320 | + } |
|
| 321 | + $read_controller = LoaderFactory::getLoader()->getNew('EventEspresso\core\libraries\rest_api\controllers\model\Read'); |
|
| 322 | + $read_controller->setRequestedVersion($this->getRequestedVersion()); |
|
| 323 | + // the simulates request really doesn't need any info downstream |
|
| 324 | + $simulated_request = new WP_REST_Request('GET'); |
|
| 325 | + // set the caps context on the simulated according to the original request. |
|
| 326 | + switch ($request->get_method()) { |
|
| 327 | + case 'POST': |
|
| 328 | + case 'PUT': |
|
| 329 | + $caps_context = EEM_Base::caps_edit; |
|
| 330 | + break; |
|
| 331 | + case 'DELETE': |
|
| 332 | + $caps_context = EEM_Base::caps_delete; |
|
| 333 | + break; |
|
| 334 | + default: |
|
| 335 | + $caps_context = EEM_Base::caps_read_admin; |
|
| 336 | + } |
|
| 337 | + $simulated_request->set_param('caps', $caps_context); |
|
| 338 | + return $read_controller->createEntityFromWpdbResult( |
|
| 339 | + $model_obj->get_model(), |
|
| 340 | + $simulated_db_row, |
|
| 341 | + $simulated_request |
|
| 342 | + ); |
|
| 343 | + } |
|
| 344 | 344 | |
| 345 | 345 | |
| 346 | - /** |
|
| 347 | - * Gets the item affected by this request |
|
| 348 | - * |
|
| 349 | - * @param EEM_Base $model |
|
| 350 | - * @param WP_REST_Request $request |
|
| 351 | - * @param int|string $obj_id |
|
| 352 | - * @return \WP_Error|array |
|
| 353 | - */ |
|
| 354 | - protected function getOneBasedOnRequest(EEM_Base $model, WP_REST_Request $request, $obj_id) |
|
| 355 | - { |
|
| 356 | - $requested_version = $this->getRequestedVersion($request->get_route()); |
|
| 357 | - $get_request = new WP_REST_Request( |
|
| 358 | - 'GET', |
|
| 359 | - EED_Core_Rest_Api::ee_api_namespace |
|
| 360 | - . $requested_version |
|
| 361 | - . '/' |
|
| 362 | - . EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
|
| 363 | - . '/' |
|
| 364 | - . $obj_id |
|
| 365 | - ); |
|
| 366 | - $get_request->set_url_params( |
|
| 367 | - array( |
|
| 368 | - 'id' => $obj_id, |
|
| 369 | - 'include' => $request->get_param('include'), |
|
| 370 | - ) |
|
| 371 | - ); |
|
| 372 | - $read_controller = new Read(); |
|
| 373 | - $read_controller->setRequestedVersion($this->getRequestedVersion()); |
|
| 374 | - return $read_controller->getEntityFromModel($model, $get_request); |
|
| 375 | - } |
|
| 346 | + /** |
|
| 347 | + * Gets the item affected by this request |
|
| 348 | + * |
|
| 349 | + * @param EEM_Base $model |
|
| 350 | + * @param WP_REST_Request $request |
|
| 351 | + * @param int|string $obj_id |
|
| 352 | + * @return \WP_Error|array |
|
| 353 | + */ |
|
| 354 | + protected function getOneBasedOnRequest(EEM_Base $model, WP_REST_Request $request, $obj_id) |
|
| 355 | + { |
|
| 356 | + $requested_version = $this->getRequestedVersion($request->get_route()); |
|
| 357 | + $get_request = new WP_REST_Request( |
|
| 358 | + 'GET', |
|
| 359 | + EED_Core_Rest_Api::ee_api_namespace |
|
| 360 | + . $requested_version |
|
| 361 | + . '/' |
|
| 362 | + . EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
|
| 363 | + . '/' |
|
| 364 | + . $obj_id |
|
| 365 | + ); |
|
| 366 | + $get_request->set_url_params( |
|
| 367 | + array( |
|
| 368 | + 'id' => $obj_id, |
|
| 369 | + 'include' => $request->get_param('include'), |
|
| 370 | + ) |
|
| 371 | + ); |
|
| 372 | + $read_controller = new Read(); |
|
| 373 | + $read_controller->setRequestedVersion($this->getRequestedVersion()); |
|
| 374 | + return $read_controller->getEntityFromModel($model, $get_request); |
|
| 375 | + } |
|
| 376 | 376 | |
| 377 | - /** |
|
| 378 | - * Adds a relation between the specified models (if it doesn't already exist.) |
|
| 379 | - * @since $VID:$ |
|
| 380 | - * @param WP_REST_Request $request |
|
| 381 | - * @return WP_REST_Response |
|
| 382 | - */ |
|
| 383 | - public static function handleRequestAddRelation(WP_REST_Request $request, $version, $model_name, $related_model_name) |
|
| 384 | - { |
|
| 385 | - $controller = new Write(); |
|
| 386 | - try { |
|
| 387 | - $controller->setRequestedVersion($version); |
|
| 388 | - $main_model = $controller->validateModel($model_name); |
|
| 389 | - $controller->validateModel($related_model_name); |
|
| 390 | - return $controller->sendResponse( |
|
| 391 | - $controller->addRelation( |
|
| 392 | - $main_model, |
|
| 393 | - $main_model->related_settings_for($related_model_name), |
|
| 394 | - $request |
|
| 395 | - ) |
|
| 396 | - ); |
|
| 397 | - } catch (Exception $e) { |
|
| 398 | - return $controller->sendResponse($e); |
|
| 399 | - } |
|
| 400 | - } |
|
| 377 | + /** |
|
| 378 | + * Adds a relation between the specified models (if it doesn't already exist.) |
|
| 379 | + * @since $VID:$ |
|
| 380 | + * @param WP_REST_Request $request |
|
| 381 | + * @return WP_REST_Response |
|
| 382 | + */ |
|
| 383 | + public static function handleRequestAddRelation(WP_REST_Request $request, $version, $model_name, $related_model_name) |
|
| 384 | + { |
|
| 385 | + $controller = new Write(); |
|
| 386 | + try { |
|
| 387 | + $controller->setRequestedVersion($version); |
|
| 388 | + $main_model = $controller->validateModel($model_name); |
|
| 389 | + $controller->validateModel($related_model_name); |
|
| 390 | + return $controller->sendResponse( |
|
| 391 | + $controller->addRelation( |
|
| 392 | + $main_model, |
|
| 393 | + $main_model->related_settings_for($related_model_name), |
|
| 394 | + $request |
|
| 395 | + ) |
|
| 396 | + ); |
|
| 397 | + } catch (Exception $e) { |
|
| 398 | + return $controller->sendResponse($e); |
|
| 399 | + } |
|
| 400 | + } |
|
| 401 | 401 | |
| 402 | - /** |
|
| 403 | - * Adds a relation between the two model specified model objects. |
|
| 404 | - * @since $VID:$ |
|
| 405 | - * @param EEM_Base $model |
|
| 406 | - * @param EE_Model_Relation_Base $relation |
|
| 407 | - * @param WP_REST_Request $request |
|
| 408 | - * @return array |
|
| 409 | - * @throws EE_Error |
|
| 410 | - * @throws InvalidArgumentException |
|
| 411 | - * @throws InvalidDataTypeException |
|
| 412 | - * @throws InvalidInterfaceException |
|
| 413 | - * @throws RestException |
|
| 414 | - * @throws DomainException |
|
| 415 | - */ |
|
| 416 | - public function addRelation(EEM_Base $model, EE_Model_Relation_Base $relation, WP_REST_Request $request) |
|
| 417 | - { |
|
| 418 | - list($model_obj, $other_obj) = $this->getBothModelObjects($model, $relation, $request); |
|
| 419 | - $extra_params = array(); |
|
| 420 | - if ($relation instanceof EE_HABTM_Relation) { |
|
| 421 | - $extra_params = array_intersect_key( |
|
| 422 | - ModelDataTranslator::prepareConditionsQueryParamsForModels( |
|
| 423 | - $request->get_body_params(), |
|
| 424 | - $relation->get_join_model(), |
|
| 425 | - $this->getModelVersionInfo()->requestedVersion(), |
|
| 426 | - true |
|
| 427 | - ), |
|
| 428 | - $relation->getNonKeyFields() |
|
| 429 | - ); |
|
| 430 | - } |
|
| 431 | - // Add a relation. |
|
| 432 | - $related_obj = $model_obj->_add_relation_to( |
|
| 433 | - $other_obj, |
|
| 434 | - $relation->get_other_model()->get_this_model_name(), |
|
| 435 | - $extra_params |
|
| 436 | - ); |
|
| 437 | - $response = array( |
|
| 438 | - strtolower($model->get_this_model_name()) => $this->returnModelObjAsJsonResponse($model_obj, $request), |
|
| 439 | - strtolower($relation->get_other_model()->get_this_model_name()) => $this->returnModelObjAsJsonResponse($related_obj, $request), |
|
| 440 | - ); |
|
| 441 | - if ($relation instanceof EE_HABTM_Relation) { |
|
| 442 | - $join_model_obj = $relation->get_join_model()->get_one( |
|
| 443 | - array( |
|
| 444 | - array( |
|
| 445 | - $model->primary_key_name() => $model_obj->ID(), |
|
| 446 | - $relation->get_other_model()->primary_key_name() => $related_obj->ID() |
|
| 447 | - ) |
|
| 448 | - ) |
|
| 449 | - ); |
|
| 450 | - $response['join'][ strtolower($relation->get_join_model()->get_this_model_name()) ] = $this->returnModelObjAsJsonResponse($join_model_obj, $request); |
|
| 451 | - } |
|
| 452 | - return $response; |
|
| 453 | - } |
|
| 402 | + /** |
|
| 403 | + * Adds a relation between the two model specified model objects. |
|
| 404 | + * @since $VID:$ |
|
| 405 | + * @param EEM_Base $model |
|
| 406 | + * @param EE_Model_Relation_Base $relation |
|
| 407 | + * @param WP_REST_Request $request |
|
| 408 | + * @return array |
|
| 409 | + * @throws EE_Error |
|
| 410 | + * @throws InvalidArgumentException |
|
| 411 | + * @throws InvalidDataTypeException |
|
| 412 | + * @throws InvalidInterfaceException |
|
| 413 | + * @throws RestException |
|
| 414 | + * @throws DomainException |
|
| 415 | + */ |
|
| 416 | + public function addRelation(EEM_Base $model, EE_Model_Relation_Base $relation, WP_REST_Request $request) |
|
| 417 | + { |
|
| 418 | + list($model_obj, $other_obj) = $this->getBothModelObjects($model, $relation, $request); |
|
| 419 | + $extra_params = array(); |
|
| 420 | + if ($relation instanceof EE_HABTM_Relation) { |
|
| 421 | + $extra_params = array_intersect_key( |
|
| 422 | + ModelDataTranslator::prepareConditionsQueryParamsForModels( |
|
| 423 | + $request->get_body_params(), |
|
| 424 | + $relation->get_join_model(), |
|
| 425 | + $this->getModelVersionInfo()->requestedVersion(), |
|
| 426 | + true |
|
| 427 | + ), |
|
| 428 | + $relation->getNonKeyFields() |
|
| 429 | + ); |
|
| 430 | + } |
|
| 431 | + // Add a relation. |
|
| 432 | + $related_obj = $model_obj->_add_relation_to( |
|
| 433 | + $other_obj, |
|
| 434 | + $relation->get_other_model()->get_this_model_name(), |
|
| 435 | + $extra_params |
|
| 436 | + ); |
|
| 437 | + $response = array( |
|
| 438 | + strtolower($model->get_this_model_name()) => $this->returnModelObjAsJsonResponse($model_obj, $request), |
|
| 439 | + strtolower($relation->get_other_model()->get_this_model_name()) => $this->returnModelObjAsJsonResponse($related_obj, $request), |
|
| 440 | + ); |
|
| 441 | + if ($relation instanceof EE_HABTM_Relation) { |
|
| 442 | + $join_model_obj = $relation->get_join_model()->get_one( |
|
| 443 | + array( |
|
| 444 | + array( |
|
| 445 | + $model->primary_key_name() => $model_obj->ID(), |
|
| 446 | + $relation->get_other_model()->primary_key_name() => $related_obj->ID() |
|
| 447 | + ) |
|
| 448 | + ) |
|
| 449 | + ); |
|
| 450 | + $response['join'][ strtolower($relation->get_join_model()->get_this_model_name()) ] = $this->returnModelObjAsJsonResponse($join_model_obj, $request); |
|
| 451 | + } |
|
| 452 | + return $response; |
|
| 453 | + } |
|
| 454 | 454 | |
| 455 | 455 | |
| 456 | - /** |
|
| 457 | - * Removes the relation between the specified models (if it exists). |
|
| 458 | - * @since $VID:$ |
|
| 459 | - * @param WP_REST_Request $request |
|
| 460 | - * @return WP_REST_Response |
|
| 461 | - */ |
|
| 462 | - public static function handleRequestRemoveRelation(WP_REST_Request $request, $version, $model_name, $related_model_name) |
|
| 463 | - { |
|
| 464 | - $controller = new Write(); |
|
| 465 | - try { |
|
| 466 | - $controller->setRequestedVersion($version); |
|
| 467 | - $main_model = $controller->getModelVersionInfo()->loadModel($model_name); |
|
| 468 | - return $controller->sendResponse( |
|
| 469 | - $controller->removeRelation( |
|
| 470 | - $main_model, |
|
| 471 | - $main_model->related_settings_for($related_model_name), |
|
| 472 | - $request |
|
| 473 | - ) |
|
| 474 | - ); |
|
| 475 | - } catch (Exception $e) { |
|
| 476 | - return $controller->sendResponse($e); |
|
| 477 | - } |
|
| 478 | - } |
|
| 456 | + /** |
|
| 457 | + * Removes the relation between the specified models (if it exists). |
|
| 458 | + * @since $VID:$ |
|
| 459 | + * @param WP_REST_Request $request |
|
| 460 | + * @return WP_REST_Response |
|
| 461 | + */ |
|
| 462 | + public static function handleRequestRemoveRelation(WP_REST_Request $request, $version, $model_name, $related_model_name) |
|
| 463 | + { |
|
| 464 | + $controller = new Write(); |
|
| 465 | + try { |
|
| 466 | + $controller->setRequestedVersion($version); |
|
| 467 | + $main_model = $controller->getModelVersionInfo()->loadModel($model_name); |
|
| 468 | + return $controller->sendResponse( |
|
| 469 | + $controller->removeRelation( |
|
| 470 | + $main_model, |
|
| 471 | + $main_model->related_settings_for($related_model_name), |
|
| 472 | + $request |
|
| 473 | + ) |
|
| 474 | + ); |
|
| 475 | + } catch (Exception $e) { |
|
| 476 | + return $controller->sendResponse($e); |
|
| 477 | + } |
|
| 478 | + } |
|
| 479 | 479 | |
| 480 | - /** |
|
| 481 | - * Adds a relation between the two model specified model objects. |
|
| 482 | - * @since $VID:$ |
|
| 483 | - * @param EEM_Base $model |
|
| 484 | - * @param EE_Model_Relation_Base $relation |
|
| 485 | - * @param WP_REST_Request $request |
|
| 486 | - * @return array |
|
| 487 | - * @throws DomainException |
|
| 488 | - * @throws EE_Error |
|
| 489 | - * @throws InvalidArgumentException |
|
| 490 | - * @throws InvalidDataTypeException |
|
| 491 | - * @throws InvalidInterfaceException |
|
| 492 | - * @throws RestException |
|
| 493 | - */ |
|
| 494 | - public function removeRelation(EEM_Base $model, EE_Model_Relation_Base $relation, WP_REST_Request $request) |
|
| 495 | - { |
|
| 496 | - // This endpoint doesn't accept body parameters (it's understandable to think it might, so let developers know |
|
| 497 | - // up-front that it doesn't.) |
|
| 498 | - if (!empty($request->get_body_params())) { |
|
| 499 | - $body_params = $request->get_body_params(); |
|
| 500 | - throw new RestException( |
|
| 501 | - 'invalid_field', |
|
| 502 | - sprintf( |
|
| 503 | - esc_html__('This endpoint doesn\'t accept post body arguments, you sent in %1$s', 'event_espresso'), |
|
| 504 | - implode(array_keys($body_params)) |
|
| 505 | - ) |
|
| 506 | - ); |
|
| 507 | - } |
|
| 508 | - list($model_obj, $other_obj) = $this->getBothModelObjects($model, $relation, $request); |
|
| 509 | - // Remember the old relation, if it used a join entry. |
|
| 510 | - $join_model_obj = null; |
|
| 511 | - if ($relation instanceof EE_HABTM_Relation) { |
|
| 512 | - $join_model_obj = $relation->get_join_model()->get_one( |
|
| 513 | - array( |
|
| 514 | - array( |
|
| 515 | - $model->primary_key_name() => $model_obj->ID(), |
|
| 516 | - $relation->get_other_model()->primary_key_name() => $other_obj->ID() |
|
| 517 | - ) |
|
| 518 | - ) |
|
| 519 | - ); |
|
| 520 | - } |
|
| 521 | - // Remove the relation. |
|
| 522 | - $related_obj = $model_obj->_remove_relation_to( |
|
| 523 | - $other_obj, |
|
| 524 | - $relation->get_other_model()->get_this_model_name() |
|
| 525 | - ); |
|
| 526 | - $response = array( |
|
| 527 | - strtolower($model->get_this_model_name()) => $this->returnModelObjAsJsonResponse($model_obj, $request), |
|
| 528 | - strtolower($relation->get_other_model()->get_this_model_name()) => $this->returnModelObjAsJsonResponse($related_obj, $request), |
|
| 529 | - ); |
|
| 530 | - if ($relation instanceof EE_HABTM_Relation) { |
|
| 531 | - $join_model_obj_after_removal = $relation->get_join_model()->get_one( |
|
| 532 | - array( |
|
| 533 | - array( |
|
| 534 | - $model->primary_key_name() => $model_obj->ID(), |
|
| 535 | - $relation->get_other_model()->primary_key_name() => $other_obj->ID() |
|
| 536 | - ) |
|
| 537 | - ) |
|
| 538 | - ); |
|
| 539 | - if ($join_model_obj instanceof EE_Base_Class) { |
|
| 540 | - $response['join'][ strtolower($relation->get_join_model()->get_this_model_name()) ] = $this->returnModelObjAsJsonResponse($join_model_obj, $request); |
|
| 541 | - } else { |
|
| 542 | - $response['join'][ strtolower($relation->get_join_model()->get_this_model_name()) ] = null; |
|
| 543 | - } |
|
| 544 | - } |
|
| 545 | - return $response; |
|
| 546 | - } |
|
| 480 | + /** |
|
| 481 | + * Adds a relation between the two model specified model objects. |
|
| 482 | + * @since $VID:$ |
|
| 483 | + * @param EEM_Base $model |
|
| 484 | + * @param EE_Model_Relation_Base $relation |
|
| 485 | + * @param WP_REST_Request $request |
|
| 486 | + * @return array |
|
| 487 | + * @throws DomainException |
|
| 488 | + * @throws EE_Error |
|
| 489 | + * @throws InvalidArgumentException |
|
| 490 | + * @throws InvalidDataTypeException |
|
| 491 | + * @throws InvalidInterfaceException |
|
| 492 | + * @throws RestException |
|
| 493 | + */ |
|
| 494 | + public function removeRelation(EEM_Base $model, EE_Model_Relation_Base $relation, WP_REST_Request $request) |
|
| 495 | + { |
|
| 496 | + // This endpoint doesn't accept body parameters (it's understandable to think it might, so let developers know |
|
| 497 | + // up-front that it doesn't.) |
|
| 498 | + if (!empty($request->get_body_params())) { |
|
| 499 | + $body_params = $request->get_body_params(); |
|
| 500 | + throw new RestException( |
|
| 501 | + 'invalid_field', |
|
| 502 | + sprintf( |
|
| 503 | + esc_html__('This endpoint doesn\'t accept post body arguments, you sent in %1$s', 'event_espresso'), |
|
| 504 | + implode(array_keys($body_params)) |
|
| 505 | + ) |
|
| 506 | + ); |
|
| 507 | + } |
|
| 508 | + list($model_obj, $other_obj) = $this->getBothModelObjects($model, $relation, $request); |
|
| 509 | + // Remember the old relation, if it used a join entry. |
|
| 510 | + $join_model_obj = null; |
|
| 511 | + if ($relation instanceof EE_HABTM_Relation) { |
|
| 512 | + $join_model_obj = $relation->get_join_model()->get_one( |
|
| 513 | + array( |
|
| 514 | + array( |
|
| 515 | + $model->primary_key_name() => $model_obj->ID(), |
|
| 516 | + $relation->get_other_model()->primary_key_name() => $other_obj->ID() |
|
| 517 | + ) |
|
| 518 | + ) |
|
| 519 | + ); |
|
| 520 | + } |
|
| 521 | + // Remove the relation. |
|
| 522 | + $related_obj = $model_obj->_remove_relation_to( |
|
| 523 | + $other_obj, |
|
| 524 | + $relation->get_other_model()->get_this_model_name() |
|
| 525 | + ); |
|
| 526 | + $response = array( |
|
| 527 | + strtolower($model->get_this_model_name()) => $this->returnModelObjAsJsonResponse($model_obj, $request), |
|
| 528 | + strtolower($relation->get_other_model()->get_this_model_name()) => $this->returnModelObjAsJsonResponse($related_obj, $request), |
|
| 529 | + ); |
|
| 530 | + if ($relation instanceof EE_HABTM_Relation) { |
|
| 531 | + $join_model_obj_after_removal = $relation->get_join_model()->get_one( |
|
| 532 | + array( |
|
| 533 | + array( |
|
| 534 | + $model->primary_key_name() => $model_obj->ID(), |
|
| 535 | + $relation->get_other_model()->primary_key_name() => $other_obj->ID() |
|
| 536 | + ) |
|
| 537 | + ) |
|
| 538 | + ); |
|
| 539 | + if ($join_model_obj instanceof EE_Base_Class) { |
|
| 540 | + $response['join'][ strtolower($relation->get_join_model()->get_this_model_name()) ] = $this->returnModelObjAsJsonResponse($join_model_obj, $request); |
|
| 541 | + } else { |
|
| 542 | + $response['join'][ strtolower($relation->get_join_model()->get_this_model_name()) ] = null; |
|
| 543 | + } |
|
| 544 | + } |
|
| 545 | + return $response; |
|
| 546 | + } |
|
| 547 | 547 | |
| 548 | - /** |
|
| 549 | - * Gets the model objects indicated by the model, relation object, and request. |
|
| 550 | - * Throws an exception if the first object doesn't exist, and currently if the related object also doesn't exist. |
|
| 551 | - * However, this behaviour may change, as we may add support for simultaneously creating and relating data. |
|
| 552 | - * @since $VID:$ |
|
| 553 | - * @param EEM_Base $model |
|
| 554 | - * @param EE_Model_Relation_Base $relation |
|
| 555 | - * @param WP_REST_Request $request |
|
| 556 | - * @return array { |
|
| 557 | - * @type EE_Base_Class $model_obj |
|
| 558 | - * @type EE_Base_Class|null $other_model_obj |
|
| 559 | - * } |
|
| 560 | - * @throws RestException |
|
| 561 | - */ |
|
| 562 | - protected function getBothModelObjects(EEM_Base $model, EE_Model_Relation_Base $relation, WP_REST_Request $request) |
|
| 563 | - { |
|
| 564 | - // Check generic caps. For now, we're only allowing access to this endpoint to full admins. |
|
| 565 | - Capabilities::verifyAtLeastPartialAccessTo($model, EEM_Base::caps_edit, 'edit'); |
|
| 566 | - $default_cap_to_check_for = EE_Restriction_Generator_Base::get_default_restrictions_cap(); |
|
| 567 | - if (! current_user_can($default_cap_to_check_for)) { |
|
| 568 | - throw new RestException( |
|
| 569 | - 'rest_cannot_edit_' . EEH_Inflector::pluralize_and_lower(($model->get_this_model_name())), |
|
| 570 | - sprintf( |
|
| 571 | - esc_html__( |
|
| 572 | - // @codingStandardsIgnoreStart |
|
| 573 | - 'For now, only those with the admin capability to "%1$s" are allowed to use the REST API to add relations in Event Espresso.', |
|
| 574 | - // @codingStandardsIgnoreEnd |
|
| 575 | - 'event_espresso' |
|
| 576 | - ), |
|
| 577 | - $default_cap_to_check_for |
|
| 578 | - ), |
|
| 579 | - array('status' => 403) |
|
| 580 | - ); |
|
| 581 | - } |
|
| 582 | - // Get the main model object. |
|
| 583 | - $model_obj = $this->getOneOrThrowException($model, $request->get_param('id')); |
|
| 584 | - // For now, we require the other model object to exist too. This might be relaxed later. |
|
| 585 | - $other_obj = $this->getOneOrThrowException($relation->get_other_model(), $request->get_param('related_id')); |
|
| 586 | - return array($model_obj,$other_obj); |
|
| 587 | - } |
|
| 548 | + /** |
|
| 549 | + * Gets the model objects indicated by the model, relation object, and request. |
|
| 550 | + * Throws an exception if the first object doesn't exist, and currently if the related object also doesn't exist. |
|
| 551 | + * However, this behaviour may change, as we may add support for simultaneously creating and relating data. |
|
| 552 | + * @since $VID:$ |
|
| 553 | + * @param EEM_Base $model |
|
| 554 | + * @param EE_Model_Relation_Base $relation |
|
| 555 | + * @param WP_REST_Request $request |
|
| 556 | + * @return array { |
|
| 557 | + * @type EE_Base_Class $model_obj |
|
| 558 | + * @type EE_Base_Class|null $other_model_obj |
|
| 559 | + * } |
|
| 560 | + * @throws RestException |
|
| 561 | + */ |
|
| 562 | + protected function getBothModelObjects(EEM_Base $model, EE_Model_Relation_Base $relation, WP_REST_Request $request) |
|
| 563 | + { |
|
| 564 | + // Check generic caps. For now, we're only allowing access to this endpoint to full admins. |
|
| 565 | + Capabilities::verifyAtLeastPartialAccessTo($model, EEM_Base::caps_edit, 'edit'); |
|
| 566 | + $default_cap_to_check_for = EE_Restriction_Generator_Base::get_default_restrictions_cap(); |
|
| 567 | + if (! current_user_can($default_cap_to_check_for)) { |
|
| 568 | + throw new RestException( |
|
| 569 | + 'rest_cannot_edit_' . EEH_Inflector::pluralize_and_lower(($model->get_this_model_name())), |
|
| 570 | + sprintf( |
|
| 571 | + esc_html__( |
|
| 572 | + // @codingStandardsIgnoreStart |
|
| 573 | + 'For now, only those with the admin capability to "%1$s" are allowed to use the REST API to add relations in Event Espresso.', |
|
| 574 | + // @codingStandardsIgnoreEnd |
|
| 575 | + 'event_espresso' |
|
| 576 | + ), |
|
| 577 | + $default_cap_to_check_for |
|
| 578 | + ), |
|
| 579 | + array('status' => 403) |
|
| 580 | + ); |
|
| 581 | + } |
|
| 582 | + // Get the main model object. |
|
| 583 | + $model_obj = $this->getOneOrThrowException($model, $request->get_param('id')); |
|
| 584 | + // For now, we require the other model object to exist too. This might be relaxed later. |
|
| 585 | + $other_obj = $this->getOneOrThrowException($relation->get_other_model(), $request->get_param('related_id')); |
|
| 586 | + return array($model_obj,$other_obj); |
|
| 587 | + } |
|
| 588 | 588 | |
| 589 | - /** |
|
| 590 | - * Gets the model with that ID or throws a REST exception. |
|
| 591 | - * @since $VID:$ |
|
| 592 | - * @param EEM_Base $model |
|
| 593 | - * @param $id |
|
| 594 | - * @return EE_Base_Class |
|
| 595 | - * @throws RestException |
|
| 596 | - */ |
|
| 597 | - protected function getOneOrThrowException(EEM_Base $model, $id) |
|
| 598 | - { |
|
| 599 | - $model_obj = $model->get_one_by_ID($id); |
|
| 600 | - // @todo: check they can permission for it. For now unnecessary because only full admins can use this endpoint. |
|
| 601 | - if ($model_obj instanceof EE_Base_Class) { |
|
| 602 | - return $model_obj; |
|
| 603 | - } |
|
| 604 | - $lowercase_model_name = strtolower($model->get_this_model_name()); |
|
| 605 | - throw new RestException( |
|
| 606 | - sprintf('rest_%s_invalid_id', $lowercase_model_name), |
|
| 607 | - sprintf(__('Invalid %s ID.', 'event_espresso'), $lowercase_model_name), |
|
| 608 | - array('status' => 404) |
|
| 609 | - ); |
|
| 610 | - } |
|
| 589 | + /** |
|
| 590 | + * Gets the model with that ID or throws a REST exception. |
|
| 591 | + * @since $VID:$ |
|
| 592 | + * @param EEM_Base $model |
|
| 593 | + * @param $id |
|
| 594 | + * @return EE_Base_Class |
|
| 595 | + * @throws RestException |
|
| 596 | + */ |
|
| 597 | + protected function getOneOrThrowException(EEM_Base $model, $id) |
|
| 598 | + { |
|
| 599 | + $model_obj = $model->get_one_by_ID($id); |
|
| 600 | + // @todo: check they can permission for it. For now unnecessary because only full admins can use this endpoint. |
|
| 601 | + if ($model_obj instanceof EE_Base_Class) { |
|
| 602 | + return $model_obj; |
|
| 603 | + } |
|
| 604 | + $lowercase_model_name = strtolower($model->get_this_model_name()); |
|
| 605 | + throw new RestException( |
|
| 606 | + sprintf('rest_%s_invalid_id', $lowercase_model_name), |
|
| 607 | + sprintf(__('Invalid %s ID.', 'event_espresso'), $lowercase_model_name), |
|
| 608 | + array('status' => 404) |
|
| 609 | + ); |
|
| 610 | + } |
|
| 611 | 611 | } |
@@ -765,6 +765,9 @@ |
||
| 765 | 765 | ); |
| 766 | 766 | } |
| 767 | 767 | |
| 768 | + /** |
|
| 769 | + * @param string $version |
|
| 770 | + */ |
|
| 768 | 771 | protected function _get_add_relation_query_params(\EEM_Base $source_model, \EEM_Base $related_model, $version) |
| 769 | 772 | { |
| 770 | 773 | // if they're related through a HABTM relation, check for any non-FKs |
@@ -23,1332 +23,1332 @@ |
||
| 23 | 23 | class EED_Core_Rest_Api extends \EED_Module |
| 24 | 24 | { |
| 25 | 25 | |
| 26 | - const ee_api_namespace = Domain::API_NAMESPACE; |
|
| 27 | - |
|
| 28 | - const ee_api_namespace_for_regex = 'ee\/v([^/]*)\/'; |
|
| 29 | - |
|
| 30 | - const saved_routes_option_names = 'ee_core_routes'; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * string used in _links response bodies to make them globally unique. |
|
| 34 | - * |
|
| 35 | - * @see http://v2.wp-api.org/extending/linking/ |
|
| 36 | - */ |
|
| 37 | - const ee_api_link_namespace = 'https://api.eventespresso.com/'; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * @var CalculatedModelFields |
|
| 41 | - */ |
|
| 42 | - protected static $_field_calculator; |
|
| 43 | - |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * @return EED_Core_Rest_Api|EED_Module |
|
| 47 | - */ |
|
| 48 | - public static function instance() |
|
| 49 | - { |
|
| 50 | - self::$_field_calculator = LoaderFactory::getLoader()->load('EventEspresso\core\libraries\rest_api\CalculatedModelFields'); |
|
| 51 | - return parent::get_instance(__CLASS__); |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * set_hooks - for hooking into EE Core, other modules, etc |
|
| 57 | - * |
|
| 58 | - * @access public |
|
| 59 | - * @return void |
|
| 60 | - */ |
|
| 61 | - public static function set_hooks() |
|
| 62 | - { |
|
| 63 | - self::set_hooks_both(); |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * set_hooks_admin - for hooking into EE Admin Core, other modules, etc |
|
| 69 | - * |
|
| 70 | - * @access public |
|
| 71 | - * @return void |
|
| 72 | - */ |
|
| 73 | - public static function set_hooks_admin() |
|
| 74 | - { |
|
| 75 | - self::set_hooks_both(); |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - |
|
| 79 | - public static function set_hooks_both() |
|
| 80 | - { |
|
| 81 | - add_action('rest_api_init', array('EED_Core_Rest_Api', 'register_routes'), 10); |
|
| 82 | - add_action('rest_api_init', array('EED_Core_Rest_Api', 'set_hooks_rest_api'), 5); |
|
| 83 | - add_filter('rest_route_data', array('EED_Core_Rest_Api', 'hide_old_endpoints'), 10, 2); |
|
| 84 | - add_filter( |
|
| 85 | - 'rest_index', |
|
| 86 | - array('EventEspresso\core\libraries\rest_api\controllers\model\Meta', 'filterEeMetadataIntoIndex') |
|
| 87 | - ); |
|
| 88 | - EED_Core_Rest_Api::invalidate_cached_route_data_on_version_change(); |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * sets up hooks which only need to be included as part of REST API requests; |
|
| 94 | - * other requests like to the frontend or admin etc don't need them |
|
| 95 | - * |
|
| 96 | - * @throws \EE_Error |
|
| 97 | - */ |
|
| 98 | - public static function set_hooks_rest_api() |
|
| 99 | - { |
|
| 100 | - // set hooks which account for changes made to the API |
|
| 101 | - EED_Core_Rest_Api::_set_hooks_for_changes(); |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * public wrapper of _set_hooks_for_changes. |
|
| 107 | - * Loads all the hooks which make requests to old versions of the API |
|
| 108 | - * appear the same as they always did |
|
| 109 | - * |
|
| 110 | - * @throws EE_Error |
|
| 111 | - */ |
|
| 112 | - public static function set_hooks_for_changes() |
|
| 113 | - { |
|
| 114 | - self::_set_hooks_for_changes(); |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * Loads all the hooks which make requests to old versions of the API |
|
| 120 | - * appear the same as they always did |
|
| 121 | - * |
|
| 122 | - * @throws EE_Error |
|
| 123 | - */ |
|
| 124 | - protected static function _set_hooks_for_changes() |
|
| 125 | - { |
|
| 126 | - $folder_contents = EEH_File::get_contents_of_folders(array(EE_LIBRARIES . 'rest_api' . DS . 'changes'), false); |
|
| 127 | - foreach ($folder_contents as $classname_in_namespace => $filepath) { |
|
| 128 | - // ignore the base parent class |
|
| 129 | - // and legacy named classes |
|
| 130 | - if ($classname_in_namespace === 'ChangesInBase' |
|
| 131 | - || strpos($classname_in_namespace, 'Changes_In_') === 0 |
|
| 132 | - ) { |
|
| 133 | - continue; |
|
| 134 | - } |
|
| 135 | - $full_classname = 'EventEspresso\core\libraries\rest_api\changes\\' . $classname_in_namespace; |
|
| 136 | - if (class_exists($full_classname)) { |
|
| 137 | - $instance_of_class = new $full_classname; |
|
| 138 | - if ($instance_of_class instanceof ChangesInBase) { |
|
| 139 | - $instance_of_class->setHooks(); |
|
| 140 | - } |
|
| 141 | - } |
|
| 142 | - } |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - |
|
| 146 | - /** |
|
| 147 | - * Filters the WP routes to add our EE-related ones. This takes a bit of time |
|
| 148 | - * so we actually prefer to only do it when an EE plugin is activated or upgraded |
|
| 149 | - * |
|
| 150 | - * @throws \EE_Error |
|
| 151 | - */ |
|
| 152 | - public static function register_routes() |
|
| 153 | - { |
|
| 154 | - foreach (EED_Core_Rest_Api::get_ee_route_data() as $namespace => $relative_routes) { |
|
| 155 | - foreach ($relative_routes as $relative_route => $data_for_multiple_endpoints) { |
|
| 156 | - /** |
|
| 157 | - * @var array $data_for_multiple_endpoints numerically indexed array |
|
| 158 | - * but can also contain route options like { |
|
| 159 | - * @type array $schema { |
|
| 160 | - * @type callable $schema_callback |
|
| 161 | - * @type array $callback_args arguments that will be passed to the callback, after the |
|
| 162 | - * WP_REST_Request of course |
|
| 163 | - * } |
|
| 164 | - * } |
|
| 165 | - */ |
|
| 166 | - // when registering routes, register all the endpoints' data at the same time |
|
| 167 | - $multiple_endpoint_args = array(); |
|
| 168 | - foreach ($data_for_multiple_endpoints as $endpoint_key => $data_for_single_endpoint) { |
|
| 169 | - /** |
|
| 170 | - * @var array $data_for_single_endpoint { |
|
| 171 | - * @type callable $callback |
|
| 172 | - * @type string methods |
|
| 173 | - * @type array args |
|
| 174 | - * @type array _links |
|
| 175 | - * @type array $callback_args arguments that will be passed to the callback, after the |
|
| 176 | - * WP_REST_Request of course |
|
| 177 | - * } |
|
| 178 | - */ |
|
| 179 | - // skip route options |
|
| 180 | - if (! is_numeric($endpoint_key)) { |
|
| 181 | - continue; |
|
| 182 | - } |
|
| 183 | - if (! isset($data_for_single_endpoint['callback'], $data_for_single_endpoint['methods'])) { |
|
| 184 | - throw new EE_Error( |
|
| 185 | - esc_html__( |
|
| 186 | - // @codingStandardsIgnoreStart |
|
| 187 | - 'Endpoint configuration data needs to have entries "callback" (callable) and "methods" (comma-separated list of accepts HTTP methods).', |
|
| 188 | - // @codingStandardsIgnoreEnd |
|
| 189 | - 'event_espresso' |
|
| 190 | - ) |
|
| 191 | - ); |
|
| 192 | - } |
|
| 193 | - $callback = $data_for_single_endpoint['callback']; |
|
| 194 | - $single_endpoint_args = array( |
|
| 195 | - 'methods' => $data_for_single_endpoint['methods'], |
|
| 196 | - 'args' => isset($data_for_single_endpoint['args']) ? $data_for_single_endpoint['args'] |
|
| 197 | - : array(), |
|
| 198 | - ); |
|
| 199 | - if (isset($data_for_single_endpoint['_links'])) { |
|
| 200 | - $single_endpoint_args['_links'] = $data_for_single_endpoint['_links']; |
|
| 201 | - } |
|
| 202 | - if (isset($data_for_single_endpoint['callback_args'])) { |
|
| 203 | - $callback_args = $data_for_single_endpoint['callback_args']; |
|
| 204 | - $single_endpoint_args['callback'] = function (\WP_REST_Request $request) use ( |
|
| 205 | - $callback, |
|
| 206 | - $callback_args |
|
| 207 | - ) { |
|
| 208 | - array_unshift($callback_args, $request); |
|
| 209 | - return call_user_func_array( |
|
| 210 | - $callback, |
|
| 211 | - $callback_args |
|
| 212 | - ); |
|
| 213 | - }; |
|
| 214 | - } else { |
|
| 215 | - $single_endpoint_args['callback'] = $data_for_single_endpoint['callback']; |
|
| 216 | - } |
|
| 217 | - $multiple_endpoint_args[] = $single_endpoint_args; |
|
| 218 | - } |
|
| 219 | - if (isset($data_for_multiple_endpoints['schema'])) { |
|
| 220 | - $schema_route_data = $data_for_multiple_endpoints['schema']; |
|
| 221 | - $schema_callback = $schema_route_data['schema_callback']; |
|
| 222 | - $callback_args = $schema_route_data['callback_args']; |
|
| 223 | - $multiple_endpoint_args['schema'] = function () use ($schema_callback, $callback_args) { |
|
| 224 | - return call_user_func_array( |
|
| 225 | - $schema_callback, |
|
| 226 | - $callback_args |
|
| 227 | - ); |
|
| 228 | - }; |
|
| 229 | - } |
|
| 230 | - register_rest_route( |
|
| 231 | - $namespace, |
|
| 232 | - $relative_route, |
|
| 233 | - $multiple_endpoint_args |
|
| 234 | - ); |
|
| 235 | - } |
|
| 236 | - } |
|
| 237 | - } |
|
| 238 | - |
|
| 239 | - |
|
| 240 | - /** |
|
| 241 | - * Checks if there was a version change or something that merits invalidating the cached |
|
| 242 | - * route data. If so, invalidates the cached route data so that it gets refreshed |
|
| 243 | - * next time the WP API is used |
|
| 244 | - */ |
|
| 245 | - public static function invalidate_cached_route_data_on_version_change() |
|
| 246 | - { |
|
| 247 | - if (EE_System::instance()->detect_req_type() !== EE_System::req_type_normal) { |
|
| 248 | - EED_Core_Rest_Api::invalidate_cached_route_data(); |
|
| 249 | - } |
|
| 250 | - foreach (EE_Registry::instance()->addons as $addon) { |
|
| 251 | - if ($addon instanceof EE_Addon && $addon->detect_req_type() !== EE_System::req_type_normal) { |
|
| 252 | - EED_Core_Rest_Api::invalidate_cached_route_data(); |
|
| 253 | - } |
|
| 254 | - } |
|
| 255 | - } |
|
| 256 | - |
|
| 257 | - |
|
| 258 | - /** |
|
| 259 | - * Removes the cached route data so it will get refreshed next time the WP API is used |
|
| 260 | - */ |
|
| 261 | - public static function invalidate_cached_route_data() |
|
| 262 | - { |
|
| 263 | - // delete the saved EE REST API routes |
|
| 264 | - foreach (EED_Core_Rest_Api::versions_served() as $version => $hidden) { |
|
| 265 | - delete_option(EED_Core_Rest_Api::saved_routes_option_names . $version); |
|
| 266 | - } |
|
| 267 | - } |
|
| 268 | - |
|
| 269 | - |
|
| 270 | - /** |
|
| 271 | - * Gets the EE route data |
|
| 272 | - * |
|
| 273 | - * @return array top-level key is the namespace, next-level key is the route and its value is array{ |
|
| 274 | - * @throws \EE_Error |
|
| 275 | - * @type string|array $callback |
|
| 276 | - * @type string $methods |
|
| 277 | - * @type boolean $hidden_endpoint |
|
| 278 | - * } |
|
| 279 | - */ |
|
| 280 | - public static function get_ee_route_data() |
|
| 281 | - { |
|
| 282 | - $ee_routes = array(); |
|
| 283 | - foreach (self::versions_served() as $version => $hidden_endpoints) { |
|
| 284 | - $ee_routes[ self::ee_api_namespace . $version ] = self::_get_ee_route_data_for_version( |
|
| 285 | - $version, |
|
| 286 | - $hidden_endpoints |
|
| 287 | - ); |
|
| 288 | - } |
|
| 289 | - return $ee_routes; |
|
| 290 | - } |
|
| 291 | - |
|
| 292 | - |
|
| 293 | - /** |
|
| 294 | - * Gets the EE route data from the wp options if it exists already, |
|
| 295 | - * otherwise re-generates it and saves it to the option |
|
| 296 | - * |
|
| 297 | - * @param string $version |
|
| 298 | - * @param boolean $hidden_endpoints |
|
| 299 | - * @return array |
|
| 300 | - * @throws \EE_Error |
|
| 301 | - */ |
|
| 302 | - protected static function _get_ee_route_data_for_version($version, $hidden_endpoints = false) |
|
| 303 | - { |
|
| 304 | - $ee_routes = get_option(self::saved_routes_option_names . $version, null); |
|
| 305 | - if (! $ee_routes || EED_Core_Rest_Api::debugMode()) { |
|
| 306 | - $ee_routes = self::_save_ee_route_data_for_version($version, $hidden_endpoints); |
|
| 307 | - } |
|
| 308 | - return $ee_routes; |
|
| 309 | - } |
|
| 310 | - |
|
| 311 | - |
|
| 312 | - /** |
|
| 313 | - * Saves the EE REST API route data to a wp option and returns it |
|
| 314 | - * |
|
| 315 | - * @param string $version |
|
| 316 | - * @param boolean $hidden_endpoints |
|
| 317 | - * @return mixed|null |
|
| 318 | - * @throws \EE_Error |
|
| 319 | - */ |
|
| 320 | - protected static function _save_ee_route_data_for_version($version, $hidden_endpoints = false) |
|
| 321 | - { |
|
| 322 | - $instance = self::instance(); |
|
| 323 | - $routes = apply_filters( |
|
| 324 | - 'EED_Core_Rest_Api__save_ee_route_data_for_version__routes', |
|
| 325 | - array_replace_recursive( |
|
| 326 | - $instance->_get_config_route_data_for_version($version, $hidden_endpoints), |
|
| 327 | - $instance->_get_meta_route_data_for_version($version, $hidden_endpoints), |
|
| 328 | - $instance->_get_model_route_data_for_version($version, $hidden_endpoints), |
|
| 329 | - $instance->_get_rpc_route_data_for_version($version, $hidden_endpoints) |
|
| 330 | - ) |
|
| 331 | - ); |
|
| 332 | - $option_name = self::saved_routes_option_names . $version; |
|
| 333 | - if (get_option($option_name)) { |
|
| 334 | - update_option($option_name, $routes, true); |
|
| 335 | - } else { |
|
| 336 | - add_option($option_name, $routes, null, 'no'); |
|
| 337 | - } |
|
| 338 | - return $routes; |
|
| 339 | - } |
|
| 340 | - |
|
| 341 | - |
|
| 342 | - /** |
|
| 343 | - * Calculates all the EE routes and saves it to a WordPress option so we don't |
|
| 344 | - * need to calculate it on every request |
|
| 345 | - * |
|
| 346 | - * @deprecated since version 4.9.1 |
|
| 347 | - * @return void |
|
| 348 | - */ |
|
| 349 | - public static function save_ee_routes() |
|
| 350 | - { |
|
| 351 | - if (EE_Maintenance_Mode::instance()->models_can_query()) { |
|
| 352 | - $instance = self::instance(); |
|
| 353 | - $routes = apply_filters( |
|
| 354 | - 'EED_Core_Rest_Api__save_ee_routes__routes', |
|
| 355 | - array_replace_recursive( |
|
| 356 | - $instance->_register_config_routes(), |
|
| 357 | - $instance->_register_meta_routes(), |
|
| 358 | - $instance->_register_model_routes(), |
|
| 359 | - $instance->_register_rpc_routes() |
|
| 360 | - ) |
|
| 361 | - ); |
|
| 362 | - update_option(self::saved_routes_option_names, $routes, true); |
|
| 363 | - } |
|
| 364 | - } |
|
| 365 | - |
|
| 366 | - |
|
| 367 | - /** |
|
| 368 | - * Gets all the route information relating to EE models |
|
| 369 | - * |
|
| 370 | - * @return array @see get_ee_route_data |
|
| 371 | - * @deprecated since version 4.9.1 |
|
| 372 | - */ |
|
| 373 | - protected function _register_model_routes() |
|
| 374 | - { |
|
| 375 | - $model_routes = array(); |
|
| 376 | - foreach (self::versions_served() as $version => $hidden_endpoint) { |
|
| 377 | - $model_routes[ EED_Core_Rest_Api::ee_api_namespace |
|
| 378 | - . $version ] = $this->_get_config_route_data_for_version($version, $hidden_endpoint); |
|
| 379 | - } |
|
| 380 | - return $model_routes; |
|
| 381 | - } |
|
| 382 | - |
|
| 383 | - |
|
| 384 | - /** |
|
| 385 | - * Decides whether or not to add write endpoints for this model. |
|
| 386 | - * |
|
| 387 | - * Currently, this defaults to exclude all global tables and models |
|
| 388 | - * which would allow inserting WP core data (we don't want to duplicate |
|
| 389 | - * what WP API does, as it's unnecessary, extra work, and potentially extra bugs) |
|
| 390 | - * |
|
| 391 | - * @param EEM_Base $model |
|
| 392 | - * @return bool |
|
| 393 | - */ |
|
| 394 | - public static function should_have_write_endpoints(EEM_Base $model) |
|
| 395 | - { |
|
| 396 | - if ($model->is_wp_core_model()) { |
|
| 397 | - return false; |
|
| 398 | - } |
|
| 399 | - foreach ($model->get_tables() as $table) { |
|
| 400 | - if ($table->is_global()) { |
|
| 401 | - return false; |
|
| 402 | - } |
|
| 403 | - } |
|
| 404 | - return true; |
|
| 405 | - } |
|
| 406 | - |
|
| 407 | - |
|
| 408 | - /** |
|
| 409 | - * Gets the names of all models which should have plural routes (eg `ee/v4.8.36/events`) |
|
| 410 | - * in this versioned namespace of EE4 |
|
| 411 | - * |
|
| 412 | - * @param $version |
|
| 413 | - * @return array keys are model names (eg 'Event') and values ar either classnames (eg 'EEM_Event') |
|
| 414 | - */ |
|
| 415 | - public static function model_names_with_plural_routes($version) |
|
| 416 | - { |
|
| 417 | - $model_version_info = new ModelVersionInfo($version); |
|
| 418 | - $models_to_register = $model_version_info->modelsForRequestedVersion(); |
|
| 419 | - // let's not bother having endpoints for extra metas |
|
| 420 | - unset( |
|
| 421 | - $models_to_register['Extra_Meta'], |
|
| 422 | - $models_to_register['Extra_Join'], |
|
| 423 | - $models_to_register['Post_Meta'] |
|
| 424 | - ); |
|
| 425 | - return apply_filters( |
|
| 426 | - 'FHEE__EED_Core_REST_API___register_model_routes', |
|
| 427 | - $models_to_register |
|
| 428 | - ); |
|
| 429 | - } |
|
| 430 | - |
|
| 431 | - |
|
| 432 | - /** |
|
| 433 | - * Gets the route data for EE models in the specified version |
|
| 434 | - * |
|
| 435 | - * @param string $version |
|
| 436 | - * @param boolean $hidden_endpoint |
|
| 437 | - * @return array |
|
| 438 | - * @throws EE_Error |
|
| 439 | - */ |
|
| 440 | - protected function _get_model_route_data_for_version($version, $hidden_endpoint = false) |
|
| 441 | - { |
|
| 442 | - $model_routes = array(); |
|
| 443 | - $model_version_info = new ModelVersionInfo($version); |
|
| 444 | - foreach (EED_Core_Rest_Api::model_names_with_plural_routes($version) as $model_name => $model_classname) { |
|
| 445 | - $model = \EE_Registry::instance()->load_model($model_name); |
|
| 446 | - // if this isn't a valid model then let's skip iterate to the next item in the loop. |
|
| 447 | - if (! $model instanceof EEM_Base) { |
|
| 448 | - continue; |
|
| 449 | - } |
|
| 450 | - // yes we could just register one route for ALL models, but then they wouldn't show up in the index |
|
| 451 | - $plural_model_route = EED_Core_Rest_Api::get_collection_route($model); |
|
| 452 | - $singular_model_route = EED_Core_Rest_Api::get_entity_route($model, '(?P<id>[^\/]+)'); |
|
| 453 | - $model_routes[ $plural_model_route ] = array( |
|
| 454 | - array( |
|
| 455 | - 'callback' => array( |
|
| 456 | - 'EventEspresso\core\libraries\rest_api\controllers\model\Read', |
|
| 457 | - 'handleRequestGetAll', |
|
| 458 | - ), |
|
| 459 | - 'callback_args' => array($version, $model_name), |
|
| 460 | - 'methods' => WP_REST_Server::READABLE, |
|
| 461 | - 'hidden_endpoint' => $hidden_endpoint, |
|
| 462 | - 'args' => $this->_get_read_query_params($model, $version), |
|
| 463 | - '_links' => array( |
|
| 464 | - 'self' => rest_url(EED_Core_Rest_Api::ee_api_namespace . $version . $singular_model_route), |
|
| 465 | - ), |
|
| 466 | - ), |
|
| 467 | - 'schema' => array( |
|
| 468 | - 'schema_callback' => array( |
|
| 469 | - 'EventEspresso\core\libraries\rest_api\controllers\model\Read', |
|
| 470 | - 'handleSchemaRequest', |
|
| 471 | - ), |
|
| 472 | - 'callback_args' => array($version, $model_name), |
|
| 473 | - ), |
|
| 474 | - ); |
|
| 475 | - $model_routes[ $singular_model_route ] = array( |
|
| 476 | - array( |
|
| 477 | - 'callback' => array( |
|
| 478 | - 'EventEspresso\core\libraries\rest_api\controllers\model\Read', |
|
| 479 | - 'handleRequestGetOne', |
|
| 480 | - ), |
|
| 481 | - 'callback_args' => array($version, $model_name), |
|
| 482 | - 'methods' => WP_REST_Server::READABLE, |
|
| 483 | - 'hidden_endpoint' => $hidden_endpoint, |
|
| 484 | - 'args' => $this->_get_response_selection_query_params($model, $version, true), |
|
| 485 | - ), |
|
| 486 | - ); |
|
| 487 | - if (apply_filters( |
|
| 488 | - 'FHEE__EED_Core_Rest_Api___get_model_route_data_for_version__add_write_endpoints', |
|
| 489 | - EED_Core_Rest_Api::should_have_write_endpoints($model), |
|
| 490 | - $model |
|
| 491 | - )) { |
|
| 492 | - $model_routes[ $plural_model_route ][] = array( |
|
| 493 | - 'callback' => array( |
|
| 494 | - 'EventEspresso\core\libraries\rest_api\controllers\model\Write', |
|
| 495 | - 'handleRequestInsert', |
|
| 496 | - ), |
|
| 497 | - 'callback_args' => array($version, $model_name), |
|
| 498 | - 'methods' => WP_REST_Server::CREATABLE, |
|
| 499 | - 'hidden_endpoint' => $hidden_endpoint, |
|
| 500 | - 'args' => $this->_get_write_params($model_name, $model_version_info, true), |
|
| 501 | - ); |
|
| 502 | - $model_routes[ $singular_model_route ] = array_merge( |
|
| 503 | - $model_routes[ $singular_model_route ], |
|
| 504 | - array( |
|
| 505 | - array( |
|
| 506 | - 'callback' => array( |
|
| 507 | - 'EventEspresso\core\libraries\rest_api\controllers\model\Write', |
|
| 508 | - 'handleRequestUpdate', |
|
| 509 | - ), |
|
| 510 | - 'callback_args' => array($version, $model_name), |
|
| 511 | - 'methods' => WP_REST_Server::EDITABLE, |
|
| 512 | - 'hidden_endpoint' => $hidden_endpoint, |
|
| 513 | - 'args' => $this->_get_write_params($model_name, $model_version_info), |
|
| 514 | - ), |
|
| 515 | - array( |
|
| 516 | - 'callback' => array( |
|
| 517 | - 'EventEspresso\core\libraries\rest_api\controllers\model\Write', |
|
| 518 | - 'handleRequestDelete', |
|
| 519 | - ), |
|
| 520 | - 'callback_args' => array($version, $model_name), |
|
| 521 | - 'methods' => WP_REST_Server::DELETABLE, |
|
| 522 | - 'hidden_endpoint' => $hidden_endpoint, |
|
| 523 | - 'args' => $this->_get_delete_query_params($model, $version), |
|
| 524 | - ), |
|
| 525 | - ) |
|
| 526 | - ); |
|
| 527 | - } |
|
| 528 | - foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
|
| 529 | - $related_route = EED_Core_Rest_Api::get_relation_route_via( |
|
| 530 | - $model, |
|
| 531 | - '(?P<id>[^\/]+)', |
|
| 532 | - $relation_obj |
|
| 533 | - ); |
|
| 534 | - $model_routes[ $related_route ] = array( |
|
| 535 | - array( |
|
| 536 | - 'callback' => array( |
|
| 537 | - 'EventEspresso\core\libraries\rest_api\controllers\model\Read', |
|
| 538 | - 'handleRequestGetRelated', |
|
| 539 | - ), |
|
| 540 | - 'callback_args' => array($version, $model_name, $relation_name), |
|
| 541 | - 'methods' => WP_REST_Server::READABLE, |
|
| 542 | - 'hidden_endpoint' => $hidden_endpoint, |
|
| 543 | - 'args' => $this->_get_read_query_params($relation_obj->get_other_model(), $version), |
|
| 544 | - ), |
|
| 545 | - ); |
|
| 546 | - |
|
| 547 | - $related_write_route = $related_route . '/' . '(?P<related_id>[^\/]+)'; |
|
| 548 | - $model_routes[ $related_write_route ] = array( |
|
| 549 | - array( |
|
| 550 | - 'callback' => array( |
|
| 551 | - 'EventEspresso\core\libraries\rest_api\controllers\model\Write', |
|
| 552 | - 'handleRequestAddRelation', |
|
| 553 | - ), |
|
| 554 | - 'callback_args' => array($version, $model_name, $relation_name), |
|
| 555 | - 'methods' => WP_REST_Server::EDITABLE, |
|
| 556 | - 'hidden_endpoint' => $hidden_endpoint, |
|
| 557 | - 'args' => $this->_get_add_relation_query_params($model, $relation_obj->get_other_model(), $version) |
|
| 558 | - ), |
|
| 559 | - array( |
|
| 560 | - 'callback' => array( |
|
| 561 | - 'EventEspresso\core\libraries\rest_api\controllers\model\Write', |
|
| 562 | - 'handleRequestRemoveRelation', |
|
| 563 | - ), |
|
| 564 | - 'callback_args' => array($version, $model_name, $relation_name), |
|
| 565 | - 'methods' => WP_REST_Server::DELETABLE, |
|
| 566 | - 'hidden_endpoint' => $hidden_endpoint, |
|
| 567 | - 'args' => array() |
|
| 568 | - ), |
|
| 569 | - ); |
|
| 570 | - } |
|
| 571 | - } |
|
| 572 | - return $model_routes; |
|
| 573 | - } |
|
| 574 | - |
|
| 575 | - |
|
| 576 | - /** |
|
| 577 | - * Gets the relative URI to a model's REST API plural route, after the EE4 versioned namespace, |
|
| 578 | - * excluding the preceding slash. |
|
| 579 | - * Eg you pass get_plural_route_to('Event') = 'events' |
|
| 580 | - * |
|
| 581 | - * @param EEM_Base $model |
|
| 582 | - * @return string |
|
| 583 | - */ |
|
| 584 | - public static function get_collection_route(EEM_Base $model) |
|
| 585 | - { |
|
| 586 | - return EEH_Inflector::pluralize_and_lower($model->get_this_model_name()); |
|
| 587 | - } |
|
| 588 | - |
|
| 589 | - |
|
| 590 | - /** |
|
| 591 | - * Gets the relative URI to a model's REST API singular route, after the EE4 versioned namespace, |
|
| 592 | - * excluding the preceding slash. |
|
| 593 | - * Eg you pass get_plural_route_to('Event', 12) = 'events/12' |
|
| 594 | - * |
|
| 595 | - * @param EEM_Base $model eg Event or Venue |
|
| 596 | - * @param string $id |
|
| 597 | - * @return string |
|
| 598 | - */ |
|
| 599 | - public static function get_entity_route($model, $id) |
|
| 600 | - { |
|
| 601 | - return EED_Core_Rest_Api::get_collection_route($model) . '/' . $id; |
|
| 602 | - } |
|
| 603 | - |
|
| 604 | - |
|
| 605 | - /** |
|
| 606 | - * Gets the relative URI to a model's REST API singular route, after the EE4 versioned namespace, |
|
| 607 | - * excluding the preceding slash. |
|
| 608 | - * Eg you pass get_plural_route_to('Event', 12) = 'events/12' |
|
| 609 | - * |
|
| 610 | - * @param EEM_Base $model eg Event or Venue |
|
| 611 | - * @param string $id |
|
| 612 | - * @param EE_Model_Relation_Base $relation_obj |
|
| 613 | - * @return string |
|
| 614 | - */ |
|
| 615 | - public static function get_relation_route_via(EEM_Base $model, $id, EE_Model_Relation_Base $relation_obj) |
|
| 616 | - { |
|
| 617 | - $related_model_name_endpoint_part = ModelRead::getRelatedEntityName( |
|
| 618 | - $relation_obj->get_other_model()->get_this_model_name(), |
|
| 619 | - $relation_obj |
|
| 620 | - ); |
|
| 621 | - return EED_Core_Rest_Api::get_entity_route($model, $id) . '/' . $related_model_name_endpoint_part; |
|
| 622 | - } |
|
| 623 | - |
|
| 624 | - |
|
| 625 | - /** |
|
| 626 | - * Adds onto the $relative_route the EE4 REST API versioned namespace. |
|
| 627 | - * Eg if given '4.8.36' and 'events', will return 'ee/v4.8.36/events' |
|
| 628 | - * |
|
| 629 | - * @param string $relative_route |
|
| 630 | - * @param string $version |
|
| 631 | - * @return string |
|
| 632 | - */ |
|
| 633 | - public static function get_versioned_route_to($relative_route, $version = '4.8.36') |
|
| 634 | - { |
|
| 635 | - return '/' . EED_Core_Rest_Api::ee_api_namespace . $version . '/' . $relative_route; |
|
| 636 | - } |
|
| 637 | - |
|
| 638 | - |
|
| 639 | - /** |
|
| 640 | - * Adds all the RPC-style routes (remote procedure call-like routes, ie |
|
| 641 | - * routes that don't conform to the traditional REST CRUD-style). |
|
| 642 | - * |
|
| 643 | - * @deprecated since 4.9.1 |
|
| 644 | - */ |
|
| 645 | - protected function _register_rpc_routes() |
|
| 646 | - { |
|
| 647 | - $routes = array(); |
|
| 648 | - foreach (self::versions_served() as $version => $hidden_endpoint) { |
|
| 649 | - $routes[ self::ee_api_namespace . $version ] = $this->_get_rpc_route_data_for_version( |
|
| 650 | - $version, |
|
| 651 | - $hidden_endpoint |
|
| 652 | - ); |
|
| 653 | - } |
|
| 654 | - return $routes; |
|
| 655 | - } |
|
| 656 | - |
|
| 657 | - |
|
| 658 | - /** |
|
| 659 | - * @param string $version |
|
| 660 | - * @param boolean $hidden_endpoint |
|
| 661 | - * @return array |
|
| 662 | - */ |
|
| 663 | - protected function _get_rpc_route_data_for_version($version, $hidden_endpoint = false) |
|
| 664 | - { |
|
| 665 | - $this_versions_routes = array(); |
|
| 666 | - // checkin endpoint |
|
| 667 | - $this_versions_routes['registrations/(?P<REG_ID>\d+)/toggle_checkin_for_datetime/(?P<DTT_ID>\d+)'] = array( |
|
| 668 | - array( |
|
| 669 | - 'callback' => array( |
|
| 670 | - 'EventEspresso\core\libraries\rest_api\controllers\rpc\Checkin', |
|
| 671 | - 'handleRequestToggleCheckin', |
|
| 672 | - ), |
|
| 673 | - 'methods' => WP_REST_Server::CREATABLE, |
|
| 674 | - 'hidden_endpoint' => $hidden_endpoint, |
|
| 675 | - 'args' => array( |
|
| 676 | - 'force' => array( |
|
| 677 | - 'required' => false, |
|
| 678 | - 'default' => false, |
|
| 679 | - 'description' => __( |
|
| 680 | - // @codingStandardsIgnoreStart |
|
| 681 | - 'Whether to force toggle checkin, or to verify the registration status and allowed ticket uses', |
|
| 682 | - // @codingStandardsIgnoreEnd |
|
| 683 | - 'event_espresso' |
|
| 684 | - ), |
|
| 685 | - ), |
|
| 686 | - ), |
|
| 687 | - 'callback_args' => array($version), |
|
| 688 | - ), |
|
| 689 | - ); |
|
| 690 | - return apply_filters( |
|
| 691 | - 'FHEE__EED_Core_Rest_Api___register_rpc_routes__this_versions_routes', |
|
| 692 | - $this_versions_routes, |
|
| 693 | - $version, |
|
| 694 | - $hidden_endpoint |
|
| 695 | - ); |
|
| 696 | - } |
|
| 697 | - |
|
| 698 | - |
|
| 699 | - /** |
|
| 700 | - * Gets the query params that can be used when request one or many |
|
| 701 | - * |
|
| 702 | - * @param EEM_Base $model |
|
| 703 | - * @param string $version |
|
| 704 | - * @return array |
|
| 705 | - */ |
|
| 706 | - protected function _get_response_selection_query_params(\EEM_Base $model, $version, $single_only = false) |
|
| 707 | - { |
|
| 708 | - $query_params = array( |
|
| 709 | - 'include' => array( |
|
| 710 | - 'required' => false, |
|
| 711 | - 'default' => '*', |
|
| 712 | - 'type' => 'string', |
|
| 713 | - ), |
|
| 714 | - 'calculate' => array( |
|
| 715 | - 'required' => false, |
|
| 716 | - 'default' => '', |
|
| 717 | - 'enum' => self::$_field_calculator->retrieveCalculatedFieldsForModel($model), |
|
| 718 | - 'type' => 'string', |
|
| 719 | - // because we accept a CSV'd list of the enumerated strings, WP core validation and sanitization |
|
| 720 | - // freaks out. We'll just validate this argument while handling the request |
|
| 721 | - 'validate_callback' => null, |
|
| 722 | - 'sanitize_callback' => null, |
|
| 723 | - ), |
|
| 724 | - 'password' => array( |
|
| 725 | - 'required' => false, |
|
| 726 | - 'default' => '', |
|
| 727 | - 'type' => 'string' |
|
| 728 | - ) |
|
| 729 | - ); |
|
| 730 | - return apply_filters( |
|
| 731 | - 'FHEE__EED_Core_Rest_Api___get_response_selection_query_params', |
|
| 732 | - $query_params, |
|
| 733 | - $model, |
|
| 734 | - $version |
|
| 735 | - ); |
|
| 736 | - } |
|
| 737 | - |
|
| 738 | - |
|
| 739 | - /** |
|
| 740 | - * Gets the parameters acceptable for delete requests |
|
| 741 | - * |
|
| 742 | - * @param \EEM_Base $model |
|
| 743 | - * @param string $version |
|
| 744 | - * @return array |
|
| 745 | - */ |
|
| 746 | - protected function _get_delete_query_params(\EEM_Base $model, $version) |
|
| 747 | - { |
|
| 748 | - $params_for_delete = array( |
|
| 749 | - 'allow_blocking' => array( |
|
| 750 | - 'required' => false, |
|
| 751 | - 'default' => true, |
|
| 752 | - 'type' => 'boolean', |
|
| 753 | - ), |
|
| 754 | - ); |
|
| 755 | - $params_for_delete['force'] = array( |
|
| 756 | - 'required' => false, |
|
| 757 | - 'default' => false, |
|
| 758 | - 'type' => 'boolean', |
|
| 759 | - ); |
|
| 760 | - return apply_filters( |
|
| 761 | - 'FHEE__EED_Core_Rest_Api___get_delete_query_params', |
|
| 762 | - $params_for_delete, |
|
| 763 | - $model, |
|
| 764 | - $version |
|
| 765 | - ); |
|
| 766 | - } |
|
| 767 | - |
|
| 768 | - protected function _get_add_relation_query_params(\EEM_Base $source_model, \EEM_Base $related_model, $version) |
|
| 769 | - { |
|
| 770 | - // if they're related through a HABTM relation, check for any non-FKs |
|
| 771 | - $all_relation_settings = $source_model->relation_settings(); |
|
| 772 | - $relation_settings = $all_relation_settings[ $related_model->get_this_model_name() ]; |
|
| 773 | - $params = array(); |
|
| 774 | - if ($relation_settings instanceof EE_HABTM_Relation && $relation_settings->hasNonKeyFields()) { |
|
| 775 | - foreach ($relation_settings->getNonKeyFields() as $field) { |
|
| 776 | - /* @var $field EE_Model_Field_Base */ |
|
| 777 | - $params[ $field->get_name() ] = array( |
|
| 778 | - 'required' => ! $field->is_nullable(), |
|
| 779 | - 'default' => ModelDataTranslator::prepareFieldValueForJson($field, $field->get_default_value(), $version), |
|
| 780 | - 'type' => $field->getSchemaType(), |
|
| 781 | - 'validate_callbaack' => null, |
|
| 782 | - 'sanitize_callback' => null |
|
| 783 | - ); |
|
| 784 | - } |
|
| 785 | - } |
|
| 786 | - return $params; |
|
| 787 | - } |
|
| 788 | - |
|
| 789 | - |
|
| 790 | - /** |
|
| 791 | - * Gets info about reading query params that are acceptable |
|
| 792 | - * |
|
| 793 | - * @param \EEM_Base $model eg 'Event' or 'Venue' |
|
| 794 | - * @param string $version |
|
| 795 | - * @return array describing the args acceptable when querying this model |
|
| 796 | - * @throws EE_Error |
|
| 797 | - */ |
|
| 798 | - protected function _get_read_query_params(\EEM_Base $model, $version) |
|
| 799 | - { |
|
| 800 | - $default_orderby = array(); |
|
| 801 | - foreach ($model->get_combined_primary_key_fields() as $key_field) { |
|
| 802 | - $default_orderby[ $key_field->get_name() ] = 'ASC'; |
|
| 803 | - } |
|
| 804 | - return array_merge( |
|
| 805 | - $this->_get_response_selection_query_params($model, $version), |
|
| 806 | - array( |
|
| 807 | - 'where' => array( |
|
| 808 | - 'required' => false, |
|
| 809 | - 'default' => array(), |
|
| 810 | - 'type' => 'object', |
|
| 811 | - // because we accept an almost infinite list of possible where conditions, WP |
|
| 812 | - // core validation and sanitization freaks out. We'll just validate this argument |
|
| 813 | - // while handling the request |
|
| 814 | - 'validate_callback' => null, |
|
| 815 | - 'sanitize_callback' => null, |
|
| 816 | - ), |
|
| 817 | - 'limit' => array( |
|
| 818 | - 'required' => false, |
|
| 819 | - 'default' => EED_Core_Rest_Api::get_default_query_limit(), |
|
| 820 | - 'type' => array( |
|
| 821 | - 'array', |
|
| 822 | - 'string', |
|
| 823 | - 'integer', |
|
| 824 | - ), |
|
| 825 | - // because we accept a variety of types, WP core validation and sanitization |
|
| 826 | - // freaks out. We'll just validate this argument while handling the request |
|
| 827 | - 'validate_callback' => null, |
|
| 828 | - 'sanitize_callback' => null, |
|
| 829 | - ), |
|
| 830 | - 'order_by' => array( |
|
| 831 | - 'required' => false, |
|
| 832 | - 'default' => $default_orderby, |
|
| 833 | - 'type' => array( |
|
| 834 | - 'object', |
|
| 835 | - 'string', |
|
| 836 | - ),// because we accept a variety of types, WP core validation and sanitization |
|
| 837 | - // freaks out. We'll just validate this argument while handling the request |
|
| 838 | - 'validate_callback' => null, |
|
| 839 | - 'sanitize_callback' => null, |
|
| 840 | - ), |
|
| 841 | - 'group_by' => array( |
|
| 842 | - 'required' => false, |
|
| 843 | - 'default' => null, |
|
| 844 | - 'type' => array( |
|
| 845 | - 'object', |
|
| 846 | - 'string', |
|
| 847 | - ), |
|
| 848 | - // because we accept an almost infinite list of possible groupings, |
|
| 849 | - // WP core validation and sanitization |
|
| 850 | - // freaks out. We'll just validate this argument while handling the request |
|
| 851 | - 'validate_callback' => null, |
|
| 852 | - 'sanitize_callback' => null, |
|
| 853 | - ), |
|
| 854 | - 'having' => array( |
|
| 855 | - 'required' => false, |
|
| 856 | - 'default' => null, |
|
| 857 | - 'type' => 'object', |
|
| 858 | - // because we accept an almost infinite list of possible where conditions, WP |
|
| 859 | - // core validation and sanitization freaks out. We'll just validate this argument |
|
| 860 | - // while handling the request |
|
| 861 | - 'validate_callback' => null, |
|
| 862 | - 'sanitize_callback' => null, |
|
| 863 | - ), |
|
| 864 | - 'caps' => array( |
|
| 865 | - 'required' => false, |
|
| 866 | - 'default' => EEM_Base::caps_read, |
|
| 867 | - 'type' => 'string', |
|
| 868 | - 'enum' => array( |
|
| 869 | - EEM_Base::caps_read, |
|
| 870 | - EEM_Base::caps_read_admin, |
|
| 871 | - EEM_Base::caps_edit, |
|
| 872 | - EEM_Base::caps_delete, |
|
| 873 | - ), |
|
| 874 | - ), |
|
| 875 | - ) |
|
| 876 | - ); |
|
| 877 | - } |
|
| 878 | - |
|
| 879 | - |
|
| 880 | - /** |
|
| 881 | - * Gets parameter information for a model regarding writing data |
|
| 882 | - * |
|
| 883 | - * @param string $model_name |
|
| 884 | - * @param ModelVersionInfo $model_version_info |
|
| 885 | - * @param boolean $create whether this is for request to create (in |
|
| 886 | - * which case we need all required params) or |
|
| 887 | - * just to update (in which case we don't |
|
| 888 | - * need those on every request) |
|
| 889 | - * @return array |
|
| 890 | - */ |
|
| 891 | - protected function _get_write_params( |
|
| 892 | - $model_name, |
|
| 893 | - ModelVersionInfo $model_version_info, |
|
| 894 | - $create = false |
|
| 895 | - ) { |
|
| 896 | - $model = EE_Registry::instance()->load_model($model_name); |
|
| 897 | - $fields = $model_version_info->fieldsOnModelInThisVersion($model); |
|
| 898 | - $args_info = array(); |
|
| 899 | - foreach ($fields as $field_name => $field_obj) { |
|
| 900 | - if ($field_obj->is_auto_increment()) { |
|
| 901 | - // totally ignore auto increment IDs |
|
| 902 | - continue; |
|
| 903 | - } |
|
| 904 | - $arg_info = $field_obj->getSchema(); |
|
| 905 | - $required = $create && ! $field_obj->is_nullable() && $field_obj->get_default_value() === null; |
|
| 906 | - $arg_info['required'] = $required; |
|
| 907 | - // remove the read-only flag. If it were read-only we wouldn't list it as an argument while writing, right? |
|
| 908 | - unset($arg_info['readonly']); |
|
| 909 | - $schema_properties = $field_obj->getSchemaProperties(); |
|
| 910 | - if (isset($schema_properties['raw']) |
|
| 911 | - && $field_obj->getSchemaType() === 'object' |
|
| 912 | - ) { |
|
| 913 | - // if there's a "raw" form of this argument, use those properties instead |
|
| 914 | - $arg_info = array_replace( |
|
| 915 | - $arg_info, |
|
| 916 | - $schema_properties['raw'] |
|
| 917 | - ); |
|
| 918 | - } |
|
| 919 | - $arg_info['default'] = ModelDataTranslator::prepareFieldValueForJson( |
|
| 920 | - $field_obj, |
|
| 921 | - $field_obj->get_default_value(), |
|
| 922 | - $model_version_info->requestedVersion() |
|
| 923 | - ); |
|
| 924 | - // we do our own validation and sanitization within the controller |
|
| 925 | - if (function_exists('rest_validate_value_from_schema')) { |
|
| 926 | - $sanitize_callback = array( |
|
| 927 | - 'EED_Core_Rest_Api', |
|
| 928 | - 'default_sanitize_callback', |
|
| 929 | - ); |
|
| 930 | - } else { |
|
| 931 | - $sanitize_callback = null; |
|
| 932 | - } |
|
| 933 | - $arg_info['sanitize_callback'] = $sanitize_callback; |
|
| 934 | - $args_info[ $field_name ] = $arg_info; |
|
| 935 | - if ($field_obj instanceof EE_Datetime_Field) { |
|
| 936 | - $gmt_arg_info = $arg_info; |
|
| 937 | - $gmt_arg_info['description'] = sprintf( |
|
| 938 | - esc_html__( |
|
| 939 | - '%1$s - the value for this field in UTC. Ignored if %2$s is provided.', |
|
| 940 | - 'event_espresso' |
|
| 941 | - ), |
|
| 942 | - $field_obj->get_nicename(), |
|
| 943 | - $field_name |
|
| 944 | - ); |
|
| 945 | - $args_info[ $field_name . '_gmt' ] = $gmt_arg_info; |
|
| 946 | - } |
|
| 947 | - } |
|
| 948 | - return $args_info; |
|
| 949 | - } |
|
| 950 | - |
|
| 951 | - |
|
| 952 | - /** |
|
| 953 | - * Replacement for WP API's 'rest_parse_request_arg'. |
|
| 954 | - * If the value is blank but not required, don't bother validating it. |
|
| 955 | - * Also, it uses our email validation instead of WP API's default. |
|
| 956 | - * |
|
| 957 | - * @param $value |
|
| 958 | - * @param WP_REST_Request $request |
|
| 959 | - * @param $param |
|
| 960 | - * @return bool|true|WP_Error |
|
| 961 | - * @throws InvalidArgumentException |
|
| 962 | - * @throws InvalidInterfaceException |
|
| 963 | - * @throws InvalidDataTypeException |
|
| 964 | - */ |
|
| 965 | - public static function default_sanitize_callback($value, WP_REST_Request $request, $param) |
|
| 966 | - { |
|
| 967 | - $attributes = $request->get_attributes(); |
|
| 968 | - if (! isset($attributes['args'][ $param ]) |
|
| 969 | - || ! is_array($attributes['args'][ $param ])) { |
|
| 970 | - $validation_result = true; |
|
| 971 | - } else { |
|
| 972 | - $args = $attributes['args'][ $param ]; |
|
| 973 | - if (( |
|
| 974 | - $value === '' |
|
| 975 | - || $value === null |
|
| 976 | - ) |
|
| 977 | - && (! isset($args['required']) |
|
| 978 | - || $args['required'] === false |
|
| 979 | - ) |
|
| 980 | - ) { |
|
| 981 | - // not required and not provided? that's cool |
|
| 982 | - $validation_result = true; |
|
| 983 | - } elseif (isset($args['format']) |
|
| 984 | - && $args['format'] === 'email' |
|
| 985 | - ) { |
|
| 986 | - $validation_result = true; |
|
| 987 | - if (! self::_validate_email($value)) { |
|
| 988 | - $validation_result = new WP_Error( |
|
| 989 | - 'rest_invalid_param', |
|
| 990 | - esc_html__( |
|
| 991 | - 'The email address is not valid or does not exist.', |
|
| 992 | - 'event_espresso' |
|
| 993 | - ) |
|
| 994 | - ); |
|
| 995 | - } |
|
| 996 | - } else { |
|
| 997 | - $validation_result = rest_validate_value_from_schema($value, $args, $param); |
|
| 998 | - } |
|
| 999 | - } |
|
| 1000 | - if (is_wp_error($validation_result)) { |
|
| 1001 | - return $validation_result; |
|
| 1002 | - } |
|
| 1003 | - return rest_sanitize_request_arg($value, $request, $param); |
|
| 1004 | - } |
|
| 1005 | - |
|
| 1006 | - |
|
| 1007 | - /** |
|
| 1008 | - * Returns whether or not this email address is valid. Copied from EE_Email_Validation_Strategy::_validate_email() |
|
| 1009 | - * |
|
| 1010 | - * @param $email |
|
| 1011 | - * @return bool |
|
| 1012 | - * @throws InvalidArgumentException |
|
| 1013 | - * @throws InvalidInterfaceException |
|
| 1014 | - * @throws InvalidDataTypeException |
|
| 1015 | - */ |
|
| 1016 | - protected static function _validate_email($email) |
|
| 1017 | - { |
|
| 1018 | - try { |
|
| 1019 | - EmailAddressFactory::create($email); |
|
| 1020 | - return true; |
|
| 1021 | - } catch (EmailValidationException $e) { |
|
| 1022 | - return false; |
|
| 1023 | - } |
|
| 1024 | - } |
|
| 1025 | - |
|
| 1026 | - |
|
| 1027 | - /** |
|
| 1028 | - * Gets routes for the config |
|
| 1029 | - * |
|
| 1030 | - * @return array @see _register_model_routes |
|
| 1031 | - * @deprecated since version 4.9.1 |
|
| 1032 | - */ |
|
| 1033 | - protected function _register_config_routes() |
|
| 1034 | - { |
|
| 1035 | - $config_routes = array(); |
|
| 1036 | - foreach (self::versions_served() as $version => $hidden_endpoint) { |
|
| 1037 | - $config_routes[ self::ee_api_namespace . $version ] = $this->_get_config_route_data_for_version( |
|
| 1038 | - $version, |
|
| 1039 | - $hidden_endpoint |
|
| 1040 | - ); |
|
| 1041 | - } |
|
| 1042 | - return $config_routes; |
|
| 1043 | - } |
|
| 1044 | - |
|
| 1045 | - |
|
| 1046 | - /** |
|
| 1047 | - * Gets routes for the config for the specified version |
|
| 1048 | - * |
|
| 1049 | - * @param string $version |
|
| 1050 | - * @param boolean $hidden_endpoint |
|
| 1051 | - * @return array |
|
| 1052 | - */ |
|
| 1053 | - protected function _get_config_route_data_for_version($version, $hidden_endpoint) |
|
| 1054 | - { |
|
| 1055 | - return array( |
|
| 1056 | - 'config' => array( |
|
| 1057 | - array( |
|
| 1058 | - 'callback' => array( |
|
| 1059 | - 'EventEspresso\core\libraries\rest_api\controllers\config\Read', |
|
| 1060 | - 'handleRequest', |
|
| 1061 | - ), |
|
| 1062 | - 'methods' => WP_REST_Server::READABLE, |
|
| 1063 | - 'hidden_endpoint' => $hidden_endpoint, |
|
| 1064 | - 'callback_args' => array($version), |
|
| 1065 | - ), |
|
| 1066 | - ), |
|
| 1067 | - 'site_info' => array( |
|
| 1068 | - array( |
|
| 1069 | - 'callback' => array( |
|
| 1070 | - 'EventEspresso\core\libraries\rest_api\controllers\config\Read', |
|
| 1071 | - 'handleRequestSiteInfo', |
|
| 1072 | - ), |
|
| 1073 | - 'methods' => WP_REST_Server::READABLE, |
|
| 1074 | - 'hidden_endpoint' => $hidden_endpoint, |
|
| 1075 | - 'callback_args' => array($version), |
|
| 1076 | - ), |
|
| 1077 | - ), |
|
| 1078 | - ); |
|
| 1079 | - } |
|
| 1080 | - |
|
| 1081 | - |
|
| 1082 | - /** |
|
| 1083 | - * Gets the meta info routes |
|
| 1084 | - * |
|
| 1085 | - * @return array @see _register_model_routes |
|
| 1086 | - * @deprecated since version 4.9.1 |
|
| 1087 | - */ |
|
| 1088 | - protected function _register_meta_routes() |
|
| 1089 | - { |
|
| 1090 | - $meta_routes = array(); |
|
| 1091 | - foreach (self::versions_served() as $version => $hidden_endpoint) { |
|
| 1092 | - $meta_routes[ self::ee_api_namespace . $version ] = $this->_get_meta_route_data_for_version( |
|
| 1093 | - $version, |
|
| 1094 | - $hidden_endpoint |
|
| 1095 | - ); |
|
| 1096 | - } |
|
| 1097 | - return $meta_routes; |
|
| 1098 | - } |
|
| 1099 | - |
|
| 1100 | - |
|
| 1101 | - /** |
|
| 1102 | - * @param string $version |
|
| 1103 | - * @param boolean $hidden_endpoint |
|
| 1104 | - * @return array |
|
| 1105 | - */ |
|
| 1106 | - protected function _get_meta_route_data_for_version($version, $hidden_endpoint = false) |
|
| 1107 | - { |
|
| 1108 | - return array( |
|
| 1109 | - 'resources' => array( |
|
| 1110 | - array( |
|
| 1111 | - 'callback' => array( |
|
| 1112 | - 'EventEspresso\core\libraries\rest_api\controllers\model\Meta', |
|
| 1113 | - 'handleRequestModelsMeta', |
|
| 1114 | - ), |
|
| 1115 | - 'methods' => WP_REST_Server::READABLE, |
|
| 1116 | - 'hidden_endpoint' => $hidden_endpoint, |
|
| 1117 | - 'callback_args' => array($version), |
|
| 1118 | - ), |
|
| 1119 | - ), |
|
| 1120 | - ); |
|
| 1121 | - } |
|
| 1122 | - |
|
| 1123 | - |
|
| 1124 | - /** |
|
| 1125 | - * Tries to hide old 4.6 endpoints from the |
|
| 1126 | - * |
|
| 1127 | - * @param array $route_data |
|
| 1128 | - * @return array |
|
| 1129 | - * @throws \EE_Error |
|
| 1130 | - */ |
|
| 1131 | - public static function hide_old_endpoints($route_data) |
|
| 1132 | - { |
|
| 1133 | - // allow API clients to override which endpoints get hidden, in case |
|
| 1134 | - // they want to discover particular endpoints |
|
| 1135 | - // also, we don't have access to the request so we have to just grab it from the superglobal |
|
| 1136 | - $force_show_ee_namespace = ltrim( |
|
| 1137 | - EEH_Array::is_set($_REQUEST, 'force_show_ee_namespace', ''), |
|
| 1138 | - '/' |
|
| 1139 | - ); |
|
| 1140 | - foreach (EED_Core_Rest_Api::get_ee_route_data() as $namespace => $relative_urls) { |
|
| 1141 | - foreach ($relative_urls as $resource_name => $endpoints) { |
|
| 1142 | - foreach ($endpoints as $key => $endpoint) { |
|
| 1143 | - // skip schema and other route options |
|
| 1144 | - if (! is_numeric($key)) { |
|
| 1145 | - continue; |
|
| 1146 | - } |
|
| 1147 | - // by default, hide "hidden_endpoint"s, unless the request indicates |
|
| 1148 | - // to $force_show_ee_namespace, in which case only show that one |
|
| 1149 | - // namespace's endpoints (and hide all others) |
|
| 1150 | - if (($force_show_ee_namespace !== '' && $force_show_ee_namespace !== $namespace) |
|
| 1151 | - || ($endpoint['hidden_endpoint'] && $force_show_ee_namespace === '') |
|
| 1152 | - ) { |
|
| 1153 | - $full_route = '/' . ltrim($namespace, '/'); |
|
| 1154 | - $full_route .= '/' . ltrim($resource_name, '/'); |
|
| 1155 | - unset($route_data[ $full_route ]); |
|
| 1156 | - } |
|
| 1157 | - } |
|
| 1158 | - } |
|
| 1159 | - } |
|
| 1160 | - return $route_data; |
|
| 1161 | - } |
|
| 1162 | - |
|
| 1163 | - |
|
| 1164 | - /** |
|
| 1165 | - * Returns an array describing which versions of core support serving requests for. |
|
| 1166 | - * Keys are core versions' major and minor version, and values are the |
|
| 1167 | - * LOWEST requested version they can serve. Eg, 4.7 can serve requests for 4.6-like |
|
| 1168 | - * data by just removing a few models and fields from the responses. However, 4.15 might remove |
|
| 1169 | - * the answers table entirely, in which case it would be very difficult for |
|
| 1170 | - * it to serve 4.6-style responses. |
|
| 1171 | - * Versions of core that are missing from this array are unknowns. |
|
| 1172 | - * previous ver |
|
| 1173 | - * |
|
| 1174 | - * @return array |
|
| 1175 | - */ |
|
| 1176 | - public static function version_compatibilities() |
|
| 1177 | - { |
|
| 1178 | - return apply_filters( |
|
| 1179 | - 'FHEE__EED_Core_REST_API__version_compatibilities', |
|
| 1180 | - array( |
|
| 1181 | - '4.8.29' => '4.8.29', |
|
| 1182 | - '4.8.33' => '4.8.29', |
|
| 1183 | - '4.8.34' => '4.8.29', |
|
| 1184 | - '4.8.36' => '4.8.29', |
|
| 1185 | - ) |
|
| 1186 | - ); |
|
| 1187 | - } |
|
| 1188 | - |
|
| 1189 | - |
|
| 1190 | - /** |
|
| 1191 | - * Gets the latest API version served. Eg if there |
|
| 1192 | - * are two versions served of the API, 4.8.29 and 4.8.32, and |
|
| 1193 | - * we are on core version 4.8.34, it will return the string "4.8.32" |
|
| 1194 | - * |
|
| 1195 | - * @return string |
|
| 1196 | - */ |
|
| 1197 | - public static function latest_rest_api_version() |
|
| 1198 | - { |
|
| 1199 | - $versions_served = \EED_Core_Rest_Api::versions_served(); |
|
| 1200 | - $versions_served_keys = array_keys($versions_served); |
|
| 1201 | - return end($versions_served_keys); |
|
| 1202 | - } |
|
| 1203 | - |
|
| 1204 | - |
|
| 1205 | - /** |
|
| 1206 | - * Using EED_Core_Rest_Api::version_compatibilities(), determines what version of |
|
| 1207 | - * EE the API can serve requests for. Eg, if we are on 4.15 of core, and |
|
| 1208 | - * we can serve requests from 4.12 or later, this will return array( '4.12', '4.13', '4.14', '4.15' ). |
|
| 1209 | - * We also indicate whether or not this version should be put in the index or not |
|
| 1210 | - * |
|
| 1211 | - * @return array keys are API version numbers (just major and minor numbers), and values |
|
| 1212 | - * are whether or not they should be hidden |
|
| 1213 | - */ |
|
| 1214 | - public static function versions_served() |
|
| 1215 | - { |
|
| 1216 | - $versions_served = array(); |
|
| 1217 | - $possibly_served_versions = EED_Core_Rest_Api::version_compatibilities(); |
|
| 1218 | - $lowest_compatible_version = end($possibly_served_versions); |
|
| 1219 | - reset($possibly_served_versions); |
|
| 1220 | - $versions_served_historically = array_keys($possibly_served_versions); |
|
| 1221 | - $latest_version = end($versions_served_historically); |
|
| 1222 | - reset($versions_served_historically); |
|
| 1223 | - // for each version of core we have ever served: |
|
| 1224 | - foreach ($versions_served_historically as $key_versioned_endpoint) { |
|
| 1225 | - // if it's not above the current core version, and it's compatible with the current version of core |
|
| 1226 | - if ($key_versioned_endpoint === $latest_version) { |
|
| 1227 | - // don't hide the latest version in the index |
|
| 1228 | - $versions_served[ $key_versioned_endpoint ] = false; |
|
| 1229 | - } elseif ($key_versioned_endpoint >= $lowest_compatible_version |
|
| 1230 | - && $key_versioned_endpoint < EED_Core_Rest_Api::core_version() |
|
| 1231 | - ) { |
|
| 1232 | - // include, but hide, previous versions which are still supported |
|
| 1233 | - $versions_served[ $key_versioned_endpoint ] = true; |
|
| 1234 | - } elseif (apply_filters( |
|
| 1235 | - 'FHEE__EED_Core_Rest_Api__versions_served__include_incompatible_versions', |
|
| 1236 | - false, |
|
| 1237 | - $possibly_served_versions |
|
| 1238 | - )) { |
|
| 1239 | - // if a version is no longer supported, don't include it in index or list of versions served |
|
| 1240 | - $versions_served[ $key_versioned_endpoint ] = true; |
|
| 1241 | - } |
|
| 1242 | - } |
|
| 1243 | - return $versions_served; |
|
| 1244 | - } |
|
| 1245 | - |
|
| 1246 | - |
|
| 1247 | - /** |
|
| 1248 | - * Gets the major and minor version of EE core's version string |
|
| 1249 | - * |
|
| 1250 | - * @return string |
|
| 1251 | - */ |
|
| 1252 | - public static function core_version() |
|
| 1253 | - { |
|
| 1254 | - return apply_filters( |
|
| 1255 | - 'FHEE__EED_Core_REST_API__core_version', |
|
| 1256 | - implode( |
|
| 1257 | - '.', |
|
| 1258 | - array_slice( |
|
| 1259 | - explode( |
|
| 1260 | - '.', |
|
| 1261 | - espresso_version() |
|
| 1262 | - ), |
|
| 1263 | - 0, |
|
| 1264 | - 3 |
|
| 1265 | - ) |
|
| 1266 | - ) |
|
| 1267 | - ); |
|
| 1268 | - } |
|
| 1269 | - |
|
| 1270 | - |
|
| 1271 | - /** |
|
| 1272 | - * Gets the default limit that should be used when querying for resources |
|
| 1273 | - * |
|
| 1274 | - * @return int |
|
| 1275 | - */ |
|
| 1276 | - public static function get_default_query_limit() |
|
| 1277 | - { |
|
| 1278 | - // we actually don't use a const because we want folks to always use |
|
| 1279 | - // this method, not the const directly |
|
| 1280 | - return apply_filters( |
|
| 1281 | - 'FHEE__EED_Core_Rest_Api__get_default_query_limit', |
|
| 1282 | - 50 |
|
| 1283 | - ); |
|
| 1284 | - } |
|
| 1285 | - |
|
| 1286 | - |
|
| 1287 | - /** |
|
| 1288 | - * |
|
| 1289 | - * @param string $version api version string (i.e. '4.8.36') |
|
| 1290 | - * @return array |
|
| 1291 | - */ |
|
| 1292 | - public static function getCollectionRoutesIndexedByModelName($version = '') |
|
| 1293 | - { |
|
| 1294 | - $version = empty($version) ? self::latest_rest_api_version() : $version; |
|
| 1295 | - $model_names = self::model_names_with_plural_routes($version); |
|
| 1296 | - $collection_routes = array(); |
|
| 1297 | - foreach ($model_names as $model_name => $model_class_name) { |
|
| 1298 | - $collection_routes[ strtolower($model_name) ] = '/' . self::ee_api_namespace . $version . '/' |
|
| 1299 | - . EEH_Inflector::pluralize_and_lower($model_name); |
|
| 1300 | - } |
|
| 1301 | - return $collection_routes; |
|
| 1302 | - } |
|
| 1303 | - |
|
| 1304 | - |
|
| 1305 | - /** |
|
| 1306 | - * Returns an array of primary key names indexed by model names. |
|
| 1307 | - * @param string $version |
|
| 1308 | - * @return array |
|
| 1309 | - */ |
|
| 1310 | - public static function getPrimaryKeyNamesIndexedByModelName($version = '') |
|
| 1311 | - { |
|
| 1312 | - $version = empty($version) ? self::latest_rest_api_version() : $version; |
|
| 1313 | - $model_names = self::model_names_with_plural_routes($version); |
|
| 1314 | - $primary_key_items = array(); |
|
| 1315 | - foreach ($model_names as $model_name => $model_class_name) { |
|
| 1316 | - $primary_keys = $model_class_name::instance()->get_combined_primary_key_fields(); |
|
| 1317 | - foreach ($primary_keys as $primary_key_name => $primary_key_field) { |
|
| 1318 | - if (count($primary_keys) > 1) { |
|
| 1319 | - $primary_key_items[ strtolower($model_name) ][] = $primary_key_name; |
|
| 1320 | - } else { |
|
| 1321 | - $primary_key_items[ strtolower($model_name) ] = $primary_key_name; |
|
| 1322 | - } |
|
| 1323 | - } |
|
| 1324 | - } |
|
| 1325 | - return $primary_key_items; |
|
| 1326 | - } |
|
| 1327 | - |
|
| 1328 | - /** |
|
| 1329 | - * Determines the EE REST API debug mode is activated, or not. |
|
| 1330 | - * @since $VID:$ |
|
| 1331 | - * @return bool |
|
| 1332 | - */ |
|
| 1333 | - public static function debugMode() |
|
| 1334 | - { |
|
| 1335 | - static $debug_mode = null; // could be class prop |
|
| 1336 | - if ($debug_mode === null) { |
|
| 1337 | - $debug_mode = defined('EE_REST_API_DEBUG_MODE') && EE_REST_API_DEBUG_MODE; |
|
| 1338 | - } |
|
| 1339 | - return $debug_mode; |
|
| 1340 | - } |
|
| 1341 | - |
|
| 1342 | - |
|
| 1343 | - |
|
| 1344 | - /** |
|
| 1345 | - * run - initial module setup |
|
| 1346 | - * |
|
| 1347 | - * @access public |
|
| 1348 | - * @param WP $WP |
|
| 1349 | - * @return void |
|
| 1350 | - */ |
|
| 1351 | - public function run($WP) |
|
| 1352 | - { |
|
| 1353 | - } |
|
| 26 | + const ee_api_namespace = Domain::API_NAMESPACE; |
|
| 27 | + |
|
| 28 | + const ee_api_namespace_for_regex = 'ee\/v([^/]*)\/'; |
|
| 29 | + |
|
| 30 | + const saved_routes_option_names = 'ee_core_routes'; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * string used in _links response bodies to make them globally unique. |
|
| 34 | + * |
|
| 35 | + * @see http://v2.wp-api.org/extending/linking/ |
|
| 36 | + */ |
|
| 37 | + const ee_api_link_namespace = 'https://api.eventespresso.com/'; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * @var CalculatedModelFields |
|
| 41 | + */ |
|
| 42 | + protected static $_field_calculator; |
|
| 43 | + |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * @return EED_Core_Rest_Api|EED_Module |
|
| 47 | + */ |
|
| 48 | + public static function instance() |
|
| 49 | + { |
|
| 50 | + self::$_field_calculator = LoaderFactory::getLoader()->load('EventEspresso\core\libraries\rest_api\CalculatedModelFields'); |
|
| 51 | + return parent::get_instance(__CLASS__); |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * set_hooks - for hooking into EE Core, other modules, etc |
|
| 57 | + * |
|
| 58 | + * @access public |
|
| 59 | + * @return void |
|
| 60 | + */ |
|
| 61 | + public static function set_hooks() |
|
| 62 | + { |
|
| 63 | + self::set_hooks_both(); |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * set_hooks_admin - for hooking into EE Admin Core, other modules, etc |
|
| 69 | + * |
|
| 70 | + * @access public |
|
| 71 | + * @return void |
|
| 72 | + */ |
|
| 73 | + public static function set_hooks_admin() |
|
| 74 | + { |
|
| 75 | + self::set_hooks_both(); |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + |
|
| 79 | + public static function set_hooks_both() |
|
| 80 | + { |
|
| 81 | + add_action('rest_api_init', array('EED_Core_Rest_Api', 'register_routes'), 10); |
|
| 82 | + add_action('rest_api_init', array('EED_Core_Rest_Api', 'set_hooks_rest_api'), 5); |
|
| 83 | + add_filter('rest_route_data', array('EED_Core_Rest_Api', 'hide_old_endpoints'), 10, 2); |
|
| 84 | + add_filter( |
|
| 85 | + 'rest_index', |
|
| 86 | + array('EventEspresso\core\libraries\rest_api\controllers\model\Meta', 'filterEeMetadataIntoIndex') |
|
| 87 | + ); |
|
| 88 | + EED_Core_Rest_Api::invalidate_cached_route_data_on_version_change(); |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * sets up hooks which only need to be included as part of REST API requests; |
|
| 94 | + * other requests like to the frontend or admin etc don't need them |
|
| 95 | + * |
|
| 96 | + * @throws \EE_Error |
|
| 97 | + */ |
|
| 98 | + public static function set_hooks_rest_api() |
|
| 99 | + { |
|
| 100 | + // set hooks which account for changes made to the API |
|
| 101 | + EED_Core_Rest_Api::_set_hooks_for_changes(); |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * public wrapper of _set_hooks_for_changes. |
|
| 107 | + * Loads all the hooks which make requests to old versions of the API |
|
| 108 | + * appear the same as they always did |
|
| 109 | + * |
|
| 110 | + * @throws EE_Error |
|
| 111 | + */ |
|
| 112 | + public static function set_hooks_for_changes() |
|
| 113 | + { |
|
| 114 | + self::_set_hooks_for_changes(); |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + |
|
| 118 | + /** |
|
| 119 | + * Loads all the hooks which make requests to old versions of the API |
|
| 120 | + * appear the same as they always did |
|
| 121 | + * |
|
| 122 | + * @throws EE_Error |
|
| 123 | + */ |
|
| 124 | + protected static function _set_hooks_for_changes() |
|
| 125 | + { |
|
| 126 | + $folder_contents = EEH_File::get_contents_of_folders(array(EE_LIBRARIES . 'rest_api' . DS . 'changes'), false); |
|
| 127 | + foreach ($folder_contents as $classname_in_namespace => $filepath) { |
|
| 128 | + // ignore the base parent class |
|
| 129 | + // and legacy named classes |
|
| 130 | + if ($classname_in_namespace === 'ChangesInBase' |
|
| 131 | + || strpos($classname_in_namespace, 'Changes_In_') === 0 |
|
| 132 | + ) { |
|
| 133 | + continue; |
|
| 134 | + } |
|
| 135 | + $full_classname = 'EventEspresso\core\libraries\rest_api\changes\\' . $classname_in_namespace; |
|
| 136 | + if (class_exists($full_classname)) { |
|
| 137 | + $instance_of_class = new $full_classname; |
|
| 138 | + if ($instance_of_class instanceof ChangesInBase) { |
|
| 139 | + $instance_of_class->setHooks(); |
|
| 140 | + } |
|
| 141 | + } |
|
| 142 | + } |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + |
|
| 146 | + /** |
|
| 147 | + * Filters the WP routes to add our EE-related ones. This takes a bit of time |
|
| 148 | + * so we actually prefer to only do it when an EE plugin is activated or upgraded |
|
| 149 | + * |
|
| 150 | + * @throws \EE_Error |
|
| 151 | + */ |
|
| 152 | + public static function register_routes() |
|
| 153 | + { |
|
| 154 | + foreach (EED_Core_Rest_Api::get_ee_route_data() as $namespace => $relative_routes) { |
|
| 155 | + foreach ($relative_routes as $relative_route => $data_for_multiple_endpoints) { |
|
| 156 | + /** |
|
| 157 | + * @var array $data_for_multiple_endpoints numerically indexed array |
|
| 158 | + * but can also contain route options like { |
|
| 159 | + * @type array $schema { |
|
| 160 | + * @type callable $schema_callback |
|
| 161 | + * @type array $callback_args arguments that will be passed to the callback, after the |
|
| 162 | + * WP_REST_Request of course |
|
| 163 | + * } |
|
| 164 | + * } |
|
| 165 | + */ |
|
| 166 | + // when registering routes, register all the endpoints' data at the same time |
|
| 167 | + $multiple_endpoint_args = array(); |
|
| 168 | + foreach ($data_for_multiple_endpoints as $endpoint_key => $data_for_single_endpoint) { |
|
| 169 | + /** |
|
| 170 | + * @var array $data_for_single_endpoint { |
|
| 171 | + * @type callable $callback |
|
| 172 | + * @type string methods |
|
| 173 | + * @type array args |
|
| 174 | + * @type array _links |
|
| 175 | + * @type array $callback_args arguments that will be passed to the callback, after the |
|
| 176 | + * WP_REST_Request of course |
|
| 177 | + * } |
|
| 178 | + */ |
|
| 179 | + // skip route options |
|
| 180 | + if (! is_numeric($endpoint_key)) { |
|
| 181 | + continue; |
|
| 182 | + } |
|
| 183 | + if (! isset($data_for_single_endpoint['callback'], $data_for_single_endpoint['methods'])) { |
|
| 184 | + throw new EE_Error( |
|
| 185 | + esc_html__( |
|
| 186 | + // @codingStandardsIgnoreStart |
|
| 187 | + 'Endpoint configuration data needs to have entries "callback" (callable) and "methods" (comma-separated list of accepts HTTP methods).', |
|
| 188 | + // @codingStandardsIgnoreEnd |
|
| 189 | + 'event_espresso' |
|
| 190 | + ) |
|
| 191 | + ); |
|
| 192 | + } |
|
| 193 | + $callback = $data_for_single_endpoint['callback']; |
|
| 194 | + $single_endpoint_args = array( |
|
| 195 | + 'methods' => $data_for_single_endpoint['methods'], |
|
| 196 | + 'args' => isset($data_for_single_endpoint['args']) ? $data_for_single_endpoint['args'] |
|
| 197 | + : array(), |
|
| 198 | + ); |
|
| 199 | + if (isset($data_for_single_endpoint['_links'])) { |
|
| 200 | + $single_endpoint_args['_links'] = $data_for_single_endpoint['_links']; |
|
| 201 | + } |
|
| 202 | + if (isset($data_for_single_endpoint['callback_args'])) { |
|
| 203 | + $callback_args = $data_for_single_endpoint['callback_args']; |
|
| 204 | + $single_endpoint_args['callback'] = function (\WP_REST_Request $request) use ( |
|
| 205 | + $callback, |
|
| 206 | + $callback_args |
|
| 207 | + ) { |
|
| 208 | + array_unshift($callback_args, $request); |
|
| 209 | + return call_user_func_array( |
|
| 210 | + $callback, |
|
| 211 | + $callback_args |
|
| 212 | + ); |
|
| 213 | + }; |
|
| 214 | + } else { |
|
| 215 | + $single_endpoint_args['callback'] = $data_for_single_endpoint['callback']; |
|
| 216 | + } |
|
| 217 | + $multiple_endpoint_args[] = $single_endpoint_args; |
|
| 218 | + } |
|
| 219 | + if (isset($data_for_multiple_endpoints['schema'])) { |
|
| 220 | + $schema_route_data = $data_for_multiple_endpoints['schema']; |
|
| 221 | + $schema_callback = $schema_route_data['schema_callback']; |
|
| 222 | + $callback_args = $schema_route_data['callback_args']; |
|
| 223 | + $multiple_endpoint_args['schema'] = function () use ($schema_callback, $callback_args) { |
|
| 224 | + return call_user_func_array( |
|
| 225 | + $schema_callback, |
|
| 226 | + $callback_args |
|
| 227 | + ); |
|
| 228 | + }; |
|
| 229 | + } |
|
| 230 | + register_rest_route( |
|
| 231 | + $namespace, |
|
| 232 | + $relative_route, |
|
| 233 | + $multiple_endpoint_args |
|
| 234 | + ); |
|
| 235 | + } |
|
| 236 | + } |
|
| 237 | + } |
|
| 238 | + |
|
| 239 | + |
|
| 240 | + /** |
|
| 241 | + * Checks if there was a version change or something that merits invalidating the cached |
|
| 242 | + * route data. If so, invalidates the cached route data so that it gets refreshed |
|
| 243 | + * next time the WP API is used |
|
| 244 | + */ |
|
| 245 | + public static function invalidate_cached_route_data_on_version_change() |
|
| 246 | + { |
|
| 247 | + if (EE_System::instance()->detect_req_type() !== EE_System::req_type_normal) { |
|
| 248 | + EED_Core_Rest_Api::invalidate_cached_route_data(); |
|
| 249 | + } |
|
| 250 | + foreach (EE_Registry::instance()->addons as $addon) { |
|
| 251 | + if ($addon instanceof EE_Addon && $addon->detect_req_type() !== EE_System::req_type_normal) { |
|
| 252 | + EED_Core_Rest_Api::invalidate_cached_route_data(); |
|
| 253 | + } |
|
| 254 | + } |
|
| 255 | + } |
|
| 256 | + |
|
| 257 | + |
|
| 258 | + /** |
|
| 259 | + * Removes the cached route data so it will get refreshed next time the WP API is used |
|
| 260 | + */ |
|
| 261 | + public static function invalidate_cached_route_data() |
|
| 262 | + { |
|
| 263 | + // delete the saved EE REST API routes |
|
| 264 | + foreach (EED_Core_Rest_Api::versions_served() as $version => $hidden) { |
|
| 265 | + delete_option(EED_Core_Rest_Api::saved_routes_option_names . $version); |
|
| 266 | + } |
|
| 267 | + } |
|
| 268 | + |
|
| 269 | + |
|
| 270 | + /** |
|
| 271 | + * Gets the EE route data |
|
| 272 | + * |
|
| 273 | + * @return array top-level key is the namespace, next-level key is the route and its value is array{ |
|
| 274 | + * @throws \EE_Error |
|
| 275 | + * @type string|array $callback |
|
| 276 | + * @type string $methods |
|
| 277 | + * @type boolean $hidden_endpoint |
|
| 278 | + * } |
|
| 279 | + */ |
|
| 280 | + public static function get_ee_route_data() |
|
| 281 | + { |
|
| 282 | + $ee_routes = array(); |
|
| 283 | + foreach (self::versions_served() as $version => $hidden_endpoints) { |
|
| 284 | + $ee_routes[ self::ee_api_namespace . $version ] = self::_get_ee_route_data_for_version( |
|
| 285 | + $version, |
|
| 286 | + $hidden_endpoints |
|
| 287 | + ); |
|
| 288 | + } |
|
| 289 | + return $ee_routes; |
|
| 290 | + } |
|
| 291 | + |
|
| 292 | + |
|
| 293 | + /** |
|
| 294 | + * Gets the EE route data from the wp options if it exists already, |
|
| 295 | + * otherwise re-generates it and saves it to the option |
|
| 296 | + * |
|
| 297 | + * @param string $version |
|
| 298 | + * @param boolean $hidden_endpoints |
|
| 299 | + * @return array |
|
| 300 | + * @throws \EE_Error |
|
| 301 | + */ |
|
| 302 | + protected static function _get_ee_route_data_for_version($version, $hidden_endpoints = false) |
|
| 303 | + { |
|
| 304 | + $ee_routes = get_option(self::saved_routes_option_names . $version, null); |
|
| 305 | + if (! $ee_routes || EED_Core_Rest_Api::debugMode()) { |
|
| 306 | + $ee_routes = self::_save_ee_route_data_for_version($version, $hidden_endpoints); |
|
| 307 | + } |
|
| 308 | + return $ee_routes; |
|
| 309 | + } |
|
| 310 | + |
|
| 311 | + |
|
| 312 | + /** |
|
| 313 | + * Saves the EE REST API route data to a wp option and returns it |
|
| 314 | + * |
|
| 315 | + * @param string $version |
|
| 316 | + * @param boolean $hidden_endpoints |
|
| 317 | + * @return mixed|null |
|
| 318 | + * @throws \EE_Error |
|
| 319 | + */ |
|
| 320 | + protected static function _save_ee_route_data_for_version($version, $hidden_endpoints = false) |
|
| 321 | + { |
|
| 322 | + $instance = self::instance(); |
|
| 323 | + $routes = apply_filters( |
|
| 324 | + 'EED_Core_Rest_Api__save_ee_route_data_for_version__routes', |
|
| 325 | + array_replace_recursive( |
|
| 326 | + $instance->_get_config_route_data_for_version($version, $hidden_endpoints), |
|
| 327 | + $instance->_get_meta_route_data_for_version($version, $hidden_endpoints), |
|
| 328 | + $instance->_get_model_route_data_for_version($version, $hidden_endpoints), |
|
| 329 | + $instance->_get_rpc_route_data_for_version($version, $hidden_endpoints) |
|
| 330 | + ) |
|
| 331 | + ); |
|
| 332 | + $option_name = self::saved_routes_option_names . $version; |
|
| 333 | + if (get_option($option_name)) { |
|
| 334 | + update_option($option_name, $routes, true); |
|
| 335 | + } else { |
|
| 336 | + add_option($option_name, $routes, null, 'no'); |
|
| 337 | + } |
|
| 338 | + return $routes; |
|
| 339 | + } |
|
| 340 | + |
|
| 341 | + |
|
| 342 | + /** |
|
| 343 | + * Calculates all the EE routes and saves it to a WordPress option so we don't |
|
| 344 | + * need to calculate it on every request |
|
| 345 | + * |
|
| 346 | + * @deprecated since version 4.9.1 |
|
| 347 | + * @return void |
|
| 348 | + */ |
|
| 349 | + public static function save_ee_routes() |
|
| 350 | + { |
|
| 351 | + if (EE_Maintenance_Mode::instance()->models_can_query()) { |
|
| 352 | + $instance = self::instance(); |
|
| 353 | + $routes = apply_filters( |
|
| 354 | + 'EED_Core_Rest_Api__save_ee_routes__routes', |
|
| 355 | + array_replace_recursive( |
|
| 356 | + $instance->_register_config_routes(), |
|
| 357 | + $instance->_register_meta_routes(), |
|
| 358 | + $instance->_register_model_routes(), |
|
| 359 | + $instance->_register_rpc_routes() |
|
| 360 | + ) |
|
| 361 | + ); |
|
| 362 | + update_option(self::saved_routes_option_names, $routes, true); |
|
| 363 | + } |
|
| 364 | + } |
|
| 365 | + |
|
| 366 | + |
|
| 367 | + /** |
|
| 368 | + * Gets all the route information relating to EE models |
|
| 369 | + * |
|
| 370 | + * @return array @see get_ee_route_data |
|
| 371 | + * @deprecated since version 4.9.1 |
|
| 372 | + */ |
|
| 373 | + protected function _register_model_routes() |
|
| 374 | + { |
|
| 375 | + $model_routes = array(); |
|
| 376 | + foreach (self::versions_served() as $version => $hidden_endpoint) { |
|
| 377 | + $model_routes[ EED_Core_Rest_Api::ee_api_namespace |
|
| 378 | + . $version ] = $this->_get_config_route_data_for_version($version, $hidden_endpoint); |
|
| 379 | + } |
|
| 380 | + return $model_routes; |
|
| 381 | + } |
|
| 382 | + |
|
| 383 | + |
|
| 384 | + /** |
|
| 385 | + * Decides whether or not to add write endpoints for this model. |
|
| 386 | + * |
|
| 387 | + * Currently, this defaults to exclude all global tables and models |
|
| 388 | + * which would allow inserting WP core data (we don't want to duplicate |
|
| 389 | + * what WP API does, as it's unnecessary, extra work, and potentially extra bugs) |
|
| 390 | + * |
|
| 391 | + * @param EEM_Base $model |
|
| 392 | + * @return bool |
|
| 393 | + */ |
|
| 394 | + public static function should_have_write_endpoints(EEM_Base $model) |
|
| 395 | + { |
|
| 396 | + if ($model->is_wp_core_model()) { |
|
| 397 | + return false; |
|
| 398 | + } |
|
| 399 | + foreach ($model->get_tables() as $table) { |
|
| 400 | + if ($table->is_global()) { |
|
| 401 | + return false; |
|
| 402 | + } |
|
| 403 | + } |
|
| 404 | + return true; |
|
| 405 | + } |
|
| 406 | + |
|
| 407 | + |
|
| 408 | + /** |
|
| 409 | + * Gets the names of all models which should have plural routes (eg `ee/v4.8.36/events`) |
|
| 410 | + * in this versioned namespace of EE4 |
|
| 411 | + * |
|
| 412 | + * @param $version |
|
| 413 | + * @return array keys are model names (eg 'Event') and values ar either classnames (eg 'EEM_Event') |
|
| 414 | + */ |
|
| 415 | + public static function model_names_with_plural_routes($version) |
|
| 416 | + { |
|
| 417 | + $model_version_info = new ModelVersionInfo($version); |
|
| 418 | + $models_to_register = $model_version_info->modelsForRequestedVersion(); |
|
| 419 | + // let's not bother having endpoints for extra metas |
|
| 420 | + unset( |
|
| 421 | + $models_to_register['Extra_Meta'], |
|
| 422 | + $models_to_register['Extra_Join'], |
|
| 423 | + $models_to_register['Post_Meta'] |
|
| 424 | + ); |
|
| 425 | + return apply_filters( |
|
| 426 | + 'FHEE__EED_Core_REST_API___register_model_routes', |
|
| 427 | + $models_to_register |
|
| 428 | + ); |
|
| 429 | + } |
|
| 430 | + |
|
| 431 | + |
|
| 432 | + /** |
|
| 433 | + * Gets the route data for EE models in the specified version |
|
| 434 | + * |
|
| 435 | + * @param string $version |
|
| 436 | + * @param boolean $hidden_endpoint |
|
| 437 | + * @return array |
|
| 438 | + * @throws EE_Error |
|
| 439 | + */ |
|
| 440 | + protected function _get_model_route_data_for_version($version, $hidden_endpoint = false) |
|
| 441 | + { |
|
| 442 | + $model_routes = array(); |
|
| 443 | + $model_version_info = new ModelVersionInfo($version); |
|
| 444 | + foreach (EED_Core_Rest_Api::model_names_with_plural_routes($version) as $model_name => $model_classname) { |
|
| 445 | + $model = \EE_Registry::instance()->load_model($model_name); |
|
| 446 | + // if this isn't a valid model then let's skip iterate to the next item in the loop. |
|
| 447 | + if (! $model instanceof EEM_Base) { |
|
| 448 | + continue; |
|
| 449 | + } |
|
| 450 | + // yes we could just register one route for ALL models, but then they wouldn't show up in the index |
|
| 451 | + $plural_model_route = EED_Core_Rest_Api::get_collection_route($model); |
|
| 452 | + $singular_model_route = EED_Core_Rest_Api::get_entity_route($model, '(?P<id>[^\/]+)'); |
|
| 453 | + $model_routes[ $plural_model_route ] = array( |
|
| 454 | + array( |
|
| 455 | + 'callback' => array( |
|
| 456 | + 'EventEspresso\core\libraries\rest_api\controllers\model\Read', |
|
| 457 | + 'handleRequestGetAll', |
|
| 458 | + ), |
|
| 459 | + 'callback_args' => array($version, $model_name), |
|
| 460 | + 'methods' => WP_REST_Server::READABLE, |
|
| 461 | + 'hidden_endpoint' => $hidden_endpoint, |
|
| 462 | + 'args' => $this->_get_read_query_params($model, $version), |
|
| 463 | + '_links' => array( |
|
| 464 | + 'self' => rest_url(EED_Core_Rest_Api::ee_api_namespace . $version . $singular_model_route), |
|
| 465 | + ), |
|
| 466 | + ), |
|
| 467 | + 'schema' => array( |
|
| 468 | + 'schema_callback' => array( |
|
| 469 | + 'EventEspresso\core\libraries\rest_api\controllers\model\Read', |
|
| 470 | + 'handleSchemaRequest', |
|
| 471 | + ), |
|
| 472 | + 'callback_args' => array($version, $model_name), |
|
| 473 | + ), |
|
| 474 | + ); |
|
| 475 | + $model_routes[ $singular_model_route ] = array( |
|
| 476 | + array( |
|
| 477 | + 'callback' => array( |
|
| 478 | + 'EventEspresso\core\libraries\rest_api\controllers\model\Read', |
|
| 479 | + 'handleRequestGetOne', |
|
| 480 | + ), |
|
| 481 | + 'callback_args' => array($version, $model_name), |
|
| 482 | + 'methods' => WP_REST_Server::READABLE, |
|
| 483 | + 'hidden_endpoint' => $hidden_endpoint, |
|
| 484 | + 'args' => $this->_get_response_selection_query_params($model, $version, true), |
|
| 485 | + ), |
|
| 486 | + ); |
|
| 487 | + if (apply_filters( |
|
| 488 | + 'FHEE__EED_Core_Rest_Api___get_model_route_data_for_version__add_write_endpoints', |
|
| 489 | + EED_Core_Rest_Api::should_have_write_endpoints($model), |
|
| 490 | + $model |
|
| 491 | + )) { |
|
| 492 | + $model_routes[ $plural_model_route ][] = array( |
|
| 493 | + 'callback' => array( |
|
| 494 | + 'EventEspresso\core\libraries\rest_api\controllers\model\Write', |
|
| 495 | + 'handleRequestInsert', |
|
| 496 | + ), |
|
| 497 | + 'callback_args' => array($version, $model_name), |
|
| 498 | + 'methods' => WP_REST_Server::CREATABLE, |
|
| 499 | + 'hidden_endpoint' => $hidden_endpoint, |
|
| 500 | + 'args' => $this->_get_write_params($model_name, $model_version_info, true), |
|
| 501 | + ); |
|
| 502 | + $model_routes[ $singular_model_route ] = array_merge( |
|
| 503 | + $model_routes[ $singular_model_route ], |
|
| 504 | + array( |
|
| 505 | + array( |
|
| 506 | + 'callback' => array( |
|
| 507 | + 'EventEspresso\core\libraries\rest_api\controllers\model\Write', |
|
| 508 | + 'handleRequestUpdate', |
|
| 509 | + ), |
|
| 510 | + 'callback_args' => array($version, $model_name), |
|
| 511 | + 'methods' => WP_REST_Server::EDITABLE, |
|
| 512 | + 'hidden_endpoint' => $hidden_endpoint, |
|
| 513 | + 'args' => $this->_get_write_params($model_name, $model_version_info), |
|
| 514 | + ), |
|
| 515 | + array( |
|
| 516 | + 'callback' => array( |
|
| 517 | + 'EventEspresso\core\libraries\rest_api\controllers\model\Write', |
|
| 518 | + 'handleRequestDelete', |
|
| 519 | + ), |
|
| 520 | + 'callback_args' => array($version, $model_name), |
|
| 521 | + 'methods' => WP_REST_Server::DELETABLE, |
|
| 522 | + 'hidden_endpoint' => $hidden_endpoint, |
|
| 523 | + 'args' => $this->_get_delete_query_params($model, $version), |
|
| 524 | + ), |
|
| 525 | + ) |
|
| 526 | + ); |
|
| 527 | + } |
|
| 528 | + foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
|
| 529 | + $related_route = EED_Core_Rest_Api::get_relation_route_via( |
|
| 530 | + $model, |
|
| 531 | + '(?P<id>[^\/]+)', |
|
| 532 | + $relation_obj |
|
| 533 | + ); |
|
| 534 | + $model_routes[ $related_route ] = array( |
|
| 535 | + array( |
|
| 536 | + 'callback' => array( |
|
| 537 | + 'EventEspresso\core\libraries\rest_api\controllers\model\Read', |
|
| 538 | + 'handleRequestGetRelated', |
|
| 539 | + ), |
|
| 540 | + 'callback_args' => array($version, $model_name, $relation_name), |
|
| 541 | + 'methods' => WP_REST_Server::READABLE, |
|
| 542 | + 'hidden_endpoint' => $hidden_endpoint, |
|
| 543 | + 'args' => $this->_get_read_query_params($relation_obj->get_other_model(), $version), |
|
| 544 | + ), |
|
| 545 | + ); |
|
| 546 | + |
|
| 547 | + $related_write_route = $related_route . '/' . '(?P<related_id>[^\/]+)'; |
|
| 548 | + $model_routes[ $related_write_route ] = array( |
|
| 549 | + array( |
|
| 550 | + 'callback' => array( |
|
| 551 | + 'EventEspresso\core\libraries\rest_api\controllers\model\Write', |
|
| 552 | + 'handleRequestAddRelation', |
|
| 553 | + ), |
|
| 554 | + 'callback_args' => array($version, $model_name, $relation_name), |
|
| 555 | + 'methods' => WP_REST_Server::EDITABLE, |
|
| 556 | + 'hidden_endpoint' => $hidden_endpoint, |
|
| 557 | + 'args' => $this->_get_add_relation_query_params($model, $relation_obj->get_other_model(), $version) |
|
| 558 | + ), |
|
| 559 | + array( |
|
| 560 | + 'callback' => array( |
|
| 561 | + 'EventEspresso\core\libraries\rest_api\controllers\model\Write', |
|
| 562 | + 'handleRequestRemoveRelation', |
|
| 563 | + ), |
|
| 564 | + 'callback_args' => array($version, $model_name, $relation_name), |
|
| 565 | + 'methods' => WP_REST_Server::DELETABLE, |
|
| 566 | + 'hidden_endpoint' => $hidden_endpoint, |
|
| 567 | + 'args' => array() |
|
| 568 | + ), |
|
| 569 | + ); |
|
| 570 | + } |
|
| 571 | + } |
|
| 572 | + return $model_routes; |
|
| 573 | + } |
|
| 574 | + |
|
| 575 | + |
|
| 576 | + /** |
|
| 577 | + * Gets the relative URI to a model's REST API plural route, after the EE4 versioned namespace, |
|
| 578 | + * excluding the preceding slash. |
|
| 579 | + * Eg you pass get_plural_route_to('Event') = 'events' |
|
| 580 | + * |
|
| 581 | + * @param EEM_Base $model |
|
| 582 | + * @return string |
|
| 583 | + */ |
|
| 584 | + public static function get_collection_route(EEM_Base $model) |
|
| 585 | + { |
|
| 586 | + return EEH_Inflector::pluralize_and_lower($model->get_this_model_name()); |
|
| 587 | + } |
|
| 588 | + |
|
| 589 | + |
|
| 590 | + /** |
|
| 591 | + * Gets the relative URI to a model's REST API singular route, after the EE4 versioned namespace, |
|
| 592 | + * excluding the preceding slash. |
|
| 593 | + * Eg you pass get_plural_route_to('Event', 12) = 'events/12' |
|
| 594 | + * |
|
| 595 | + * @param EEM_Base $model eg Event or Venue |
|
| 596 | + * @param string $id |
|
| 597 | + * @return string |
|
| 598 | + */ |
|
| 599 | + public static function get_entity_route($model, $id) |
|
| 600 | + { |
|
| 601 | + return EED_Core_Rest_Api::get_collection_route($model) . '/' . $id; |
|
| 602 | + } |
|
| 603 | + |
|
| 604 | + |
|
| 605 | + /** |
|
| 606 | + * Gets the relative URI to a model's REST API singular route, after the EE4 versioned namespace, |
|
| 607 | + * excluding the preceding slash. |
|
| 608 | + * Eg you pass get_plural_route_to('Event', 12) = 'events/12' |
|
| 609 | + * |
|
| 610 | + * @param EEM_Base $model eg Event or Venue |
|
| 611 | + * @param string $id |
|
| 612 | + * @param EE_Model_Relation_Base $relation_obj |
|
| 613 | + * @return string |
|
| 614 | + */ |
|
| 615 | + public static function get_relation_route_via(EEM_Base $model, $id, EE_Model_Relation_Base $relation_obj) |
|
| 616 | + { |
|
| 617 | + $related_model_name_endpoint_part = ModelRead::getRelatedEntityName( |
|
| 618 | + $relation_obj->get_other_model()->get_this_model_name(), |
|
| 619 | + $relation_obj |
|
| 620 | + ); |
|
| 621 | + return EED_Core_Rest_Api::get_entity_route($model, $id) . '/' . $related_model_name_endpoint_part; |
|
| 622 | + } |
|
| 623 | + |
|
| 624 | + |
|
| 625 | + /** |
|
| 626 | + * Adds onto the $relative_route the EE4 REST API versioned namespace. |
|
| 627 | + * Eg if given '4.8.36' and 'events', will return 'ee/v4.8.36/events' |
|
| 628 | + * |
|
| 629 | + * @param string $relative_route |
|
| 630 | + * @param string $version |
|
| 631 | + * @return string |
|
| 632 | + */ |
|
| 633 | + public static function get_versioned_route_to($relative_route, $version = '4.8.36') |
|
| 634 | + { |
|
| 635 | + return '/' . EED_Core_Rest_Api::ee_api_namespace . $version . '/' . $relative_route; |
|
| 636 | + } |
|
| 637 | + |
|
| 638 | + |
|
| 639 | + /** |
|
| 640 | + * Adds all the RPC-style routes (remote procedure call-like routes, ie |
|
| 641 | + * routes that don't conform to the traditional REST CRUD-style). |
|
| 642 | + * |
|
| 643 | + * @deprecated since 4.9.1 |
|
| 644 | + */ |
|
| 645 | + protected function _register_rpc_routes() |
|
| 646 | + { |
|
| 647 | + $routes = array(); |
|
| 648 | + foreach (self::versions_served() as $version => $hidden_endpoint) { |
|
| 649 | + $routes[ self::ee_api_namespace . $version ] = $this->_get_rpc_route_data_for_version( |
|
| 650 | + $version, |
|
| 651 | + $hidden_endpoint |
|
| 652 | + ); |
|
| 653 | + } |
|
| 654 | + return $routes; |
|
| 655 | + } |
|
| 656 | + |
|
| 657 | + |
|
| 658 | + /** |
|
| 659 | + * @param string $version |
|
| 660 | + * @param boolean $hidden_endpoint |
|
| 661 | + * @return array |
|
| 662 | + */ |
|
| 663 | + protected function _get_rpc_route_data_for_version($version, $hidden_endpoint = false) |
|
| 664 | + { |
|
| 665 | + $this_versions_routes = array(); |
|
| 666 | + // checkin endpoint |
|
| 667 | + $this_versions_routes['registrations/(?P<REG_ID>\d+)/toggle_checkin_for_datetime/(?P<DTT_ID>\d+)'] = array( |
|
| 668 | + array( |
|
| 669 | + 'callback' => array( |
|
| 670 | + 'EventEspresso\core\libraries\rest_api\controllers\rpc\Checkin', |
|
| 671 | + 'handleRequestToggleCheckin', |
|
| 672 | + ), |
|
| 673 | + 'methods' => WP_REST_Server::CREATABLE, |
|
| 674 | + 'hidden_endpoint' => $hidden_endpoint, |
|
| 675 | + 'args' => array( |
|
| 676 | + 'force' => array( |
|
| 677 | + 'required' => false, |
|
| 678 | + 'default' => false, |
|
| 679 | + 'description' => __( |
|
| 680 | + // @codingStandardsIgnoreStart |
|
| 681 | + 'Whether to force toggle checkin, or to verify the registration status and allowed ticket uses', |
|
| 682 | + // @codingStandardsIgnoreEnd |
|
| 683 | + 'event_espresso' |
|
| 684 | + ), |
|
| 685 | + ), |
|
| 686 | + ), |
|
| 687 | + 'callback_args' => array($version), |
|
| 688 | + ), |
|
| 689 | + ); |
|
| 690 | + return apply_filters( |
|
| 691 | + 'FHEE__EED_Core_Rest_Api___register_rpc_routes__this_versions_routes', |
|
| 692 | + $this_versions_routes, |
|
| 693 | + $version, |
|
| 694 | + $hidden_endpoint |
|
| 695 | + ); |
|
| 696 | + } |
|
| 697 | + |
|
| 698 | + |
|
| 699 | + /** |
|
| 700 | + * Gets the query params that can be used when request one or many |
|
| 701 | + * |
|
| 702 | + * @param EEM_Base $model |
|
| 703 | + * @param string $version |
|
| 704 | + * @return array |
|
| 705 | + */ |
|
| 706 | + protected function _get_response_selection_query_params(\EEM_Base $model, $version, $single_only = false) |
|
| 707 | + { |
|
| 708 | + $query_params = array( |
|
| 709 | + 'include' => array( |
|
| 710 | + 'required' => false, |
|
| 711 | + 'default' => '*', |
|
| 712 | + 'type' => 'string', |
|
| 713 | + ), |
|
| 714 | + 'calculate' => array( |
|
| 715 | + 'required' => false, |
|
| 716 | + 'default' => '', |
|
| 717 | + 'enum' => self::$_field_calculator->retrieveCalculatedFieldsForModel($model), |
|
| 718 | + 'type' => 'string', |
|
| 719 | + // because we accept a CSV'd list of the enumerated strings, WP core validation and sanitization |
|
| 720 | + // freaks out. We'll just validate this argument while handling the request |
|
| 721 | + 'validate_callback' => null, |
|
| 722 | + 'sanitize_callback' => null, |
|
| 723 | + ), |
|
| 724 | + 'password' => array( |
|
| 725 | + 'required' => false, |
|
| 726 | + 'default' => '', |
|
| 727 | + 'type' => 'string' |
|
| 728 | + ) |
|
| 729 | + ); |
|
| 730 | + return apply_filters( |
|
| 731 | + 'FHEE__EED_Core_Rest_Api___get_response_selection_query_params', |
|
| 732 | + $query_params, |
|
| 733 | + $model, |
|
| 734 | + $version |
|
| 735 | + ); |
|
| 736 | + } |
|
| 737 | + |
|
| 738 | + |
|
| 739 | + /** |
|
| 740 | + * Gets the parameters acceptable for delete requests |
|
| 741 | + * |
|
| 742 | + * @param \EEM_Base $model |
|
| 743 | + * @param string $version |
|
| 744 | + * @return array |
|
| 745 | + */ |
|
| 746 | + protected function _get_delete_query_params(\EEM_Base $model, $version) |
|
| 747 | + { |
|
| 748 | + $params_for_delete = array( |
|
| 749 | + 'allow_blocking' => array( |
|
| 750 | + 'required' => false, |
|
| 751 | + 'default' => true, |
|
| 752 | + 'type' => 'boolean', |
|
| 753 | + ), |
|
| 754 | + ); |
|
| 755 | + $params_for_delete['force'] = array( |
|
| 756 | + 'required' => false, |
|
| 757 | + 'default' => false, |
|
| 758 | + 'type' => 'boolean', |
|
| 759 | + ); |
|
| 760 | + return apply_filters( |
|
| 761 | + 'FHEE__EED_Core_Rest_Api___get_delete_query_params', |
|
| 762 | + $params_for_delete, |
|
| 763 | + $model, |
|
| 764 | + $version |
|
| 765 | + ); |
|
| 766 | + } |
|
| 767 | + |
|
| 768 | + protected function _get_add_relation_query_params(\EEM_Base $source_model, \EEM_Base $related_model, $version) |
|
| 769 | + { |
|
| 770 | + // if they're related through a HABTM relation, check for any non-FKs |
|
| 771 | + $all_relation_settings = $source_model->relation_settings(); |
|
| 772 | + $relation_settings = $all_relation_settings[ $related_model->get_this_model_name() ]; |
|
| 773 | + $params = array(); |
|
| 774 | + if ($relation_settings instanceof EE_HABTM_Relation && $relation_settings->hasNonKeyFields()) { |
|
| 775 | + foreach ($relation_settings->getNonKeyFields() as $field) { |
|
| 776 | + /* @var $field EE_Model_Field_Base */ |
|
| 777 | + $params[ $field->get_name() ] = array( |
|
| 778 | + 'required' => ! $field->is_nullable(), |
|
| 779 | + 'default' => ModelDataTranslator::prepareFieldValueForJson($field, $field->get_default_value(), $version), |
|
| 780 | + 'type' => $field->getSchemaType(), |
|
| 781 | + 'validate_callbaack' => null, |
|
| 782 | + 'sanitize_callback' => null |
|
| 783 | + ); |
|
| 784 | + } |
|
| 785 | + } |
|
| 786 | + return $params; |
|
| 787 | + } |
|
| 788 | + |
|
| 789 | + |
|
| 790 | + /** |
|
| 791 | + * Gets info about reading query params that are acceptable |
|
| 792 | + * |
|
| 793 | + * @param \EEM_Base $model eg 'Event' or 'Venue' |
|
| 794 | + * @param string $version |
|
| 795 | + * @return array describing the args acceptable when querying this model |
|
| 796 | + * @throws EE_Error |
|
| 797 | + */ |
|
| 798 | + protected function _get_read_query_params(\EEM_Base $model, $version) |
|
| 799 | + { |
|
| 800 | + $default_orderby = array(); |
|
| 801 | + foreach ($model->get_combined_primary_key_fields() as $key_field) { |
|
| 802 | + $default_orderby[ $key_field->get_name() ] = 'ASC'; |
|
| 803 | + } |
|
| 804 | + return array_merge( |
|
| 805 | + $this->_get_response_selection_query_params($model, $version), |
|
| 806 | + array( |
|
| 807 | + 'where' => array( |
|
| 808 | + 'required' => false, |
|
| 809 | + 'default' => array(), |
|
| 810 | + 'type' => 'object', |
|
| 811 | + // because we accept an almost infinite list of possible where conditions, WP |
|
| 812 | + // core validation and sanitization freaks out. We'll just validate this argument |
|
| 813 | + // while handling the request |
|
| 814 | + 'validate_callback' => null, |
|
| 815 | + 'sanitize_callback' => null, |
|
| 816 | + ), |
|
| 817 | + 'limit' => array( |
|
| 818 | + 'required' => false, |
|
| 819 | + 'default' => EED_Core_Rest_Api::get_default_query_limit(), |
|
| 820 | + 'type' => array( |
|
| 821 | + 'array', |
|
| 822 | + 'string', |
|
| 823 | + 'integer', |
|
| 824 | + ), |
|
| 825 | + // because we accept a variety of types, WP core validation and sanitization |
|
| 826 | + // freaks out. We'll just validate this argument while handling the request |
|
| 827 | + 'validate_callback' => null, |
|
| 828 | + 'sanitize_callback' => null, |
|
| 829 | + ), |
|
| 830 | + 'order_by' => array( |
|
| 831 | + 'required' => false, |
|
| 832 | + 'default' => $default_orderby, |
|
| 833 | + 'type' => array( |
|
| 834 | + 'object', |
|
| 835 | + 'string', |
|
| 836 | + ),// because we accept a variety of types, WP core validation and sanitization |
|
| 837 | + // freaks out. We'll just validate this argument while handling the request |
|
| 838 | + 'validate_callback' => null, |
|
| 839 | + 'sanitize_callback' => null, |
|
| 840 | + ), |
|
| 841 | + 'group_by' => array( |
|
| 842 | + 'required' => false, |
|
| 843 | + 'default' => null, |
|
| 844 | + 'type' => array( |
|
| 845 | + 'object', |
|
| 846 | + 'string', |
|
| 847 | + ), |
|
| 848 | + // because we accept an almost infinite list of possible groupings, |
|
| 849 | + // WP core validation and sanitization |
|
| 850 | + // freaks out. We'll just validate this argument while handling the request |
|
| 851 | + 'validate_callback' => null, |
|
| 852 | + 'sanitize_callback' => null, |
|
| 853 | + ), |
|
| 854 | + 'having' => array( |
|
| 855 | + 'required' => false, |
|
| 856 | + 'default' => null, |
|
| 857 | + 'type' => 'object', |
|
| 858 | + // because we accept an almost infinite list of possible where conditions, WP |
|
| 859 | + // core validation and sanitization freaks out. We'll just validate this argument |
|
| 860 | + // while handling the request |
|
| 861 | + 'validate_callback' => null, |
|
| 862 | + 'sanitize_callback' => null, |
|
| 863 | + ), |
|
| 864 | + 'caps' => array( |
|
| 865 | + 'required' => false, |
|
| 866 | + 'default' => EEM_Base::caps_read, |
|
| 867 | + 'type' => 'string', |
|
| 868 | + 'enum' => array( |
|
| 869 | + EEM_Base::caps_read, |
|
| 870 | + EEM_Base::caps_read_admin, |
|
| 871 | + EEM_Base::caps_edit, |
|
| 872 | + EEM_Base::caps_delete, |
|
| 873 | + ), |
|
| 874 | + ), |
|
| 875 | + ) |
|
| 876 | + ); |
|
| 877 | + } |
|
| 878 | + |
|
| 879 | + |
|
| 880 | + /** |
|
| 881 | + * Gets parameter information for a model regarding writing data |
|
| 882 | + * |
|
| 883 | + * @param string $model_name |
|
| 884 | + * @param ModelVersionInfo $model_version_info |
|
| 885 | + * @param boolean $create whether this is for request to create (in |
|
| 886 | + * which case we need all required params) or |
|
| 887 | + * just to update (in which case we don't |
|
| 888 | + * need those on every request) |
|
| 889 | + * @return array |
|
| 890 | + */ |
|
| 891 | + protected function _get_write_params( |
|
| 892 | + $model_name, |
|
| 893 | + ModelVersionInfo $model_version_info, |
|
| 894 | + $create = false |
|
| 895 | + ) { |
|
| 896 | + $model = EE_Registry::instance()->load_model($model_name); |
|
| 897 | + $fields = $model_version_info->fieldsOnModelInThisVersion($model); |
|
| 898 | + $args_info = array(); |
|
| 899 | + foreach ($fields as $field_name => $field_obj) { |
|
| 900 | + if ($field_obj->is_auto_increment()) { |
|
| 901 | + // totally ignore auto increment IDs |
|
| 902 | + continue; |
|
| 903 | + } |
|
| 904 | + $arg_info = $field_obj->getSchema(); |
|
| 905 | + $required = $create && ! $field_obj->is_nullable() && $field_obj->get_default_value() === null; |
|
| 906 | + $arg_info['required'] = $required; |
|
| 907 | + // remove the read-only flag. If it were read-only we wouldn't list it as an argument while writing, right? |
|
| 908 | + unset($arg_info['readonly']); |
|
| 909 | + $schema_properties = $field_obj->getSchemaProperties(); |
|
| 910 | + if (isset($schema_properties['raw']) |
|
| 911 | + && $field_obj->getSchemaType() === 'object' |
|
| 912 | + ) { |
|
| 913 | + // if there's a "raw" form of this argument, use those properties instead |
|
| 914 | + $arg_info = array_replace( |
|
| 915 | + $arg_info, |
|
| 916 | + $schema_properties['raw'] |
|
| 917 | + ); |
|
| 918 | + } |
|
| 919 | + $arg_info['default'] = ModelDataTranslator::prepareFieldValueForJson( |
|
| 920 | + $field_obj, |
|
| 921 | + $field_obj->get_default_value(), |
|
| 922 | + $model_version_info->requestedVersion() |
|
| 923 | + ); |
|
| 924 | + // we do our own validation and sanitization within the controller |
|
| 925 | + if (function_exists('rest_validate_value_from_schema')) { |
|
| 926 | + $sanitize_callback = array( |
|
| 927 | + 'EED_Core_Rest_Api', |
|
| 928 | + 'default_sanitize_callback', |
|
| 929 | + ); |
|
| 930 | + } else { |
|
| 931 | + $sanitize_callback = null; |
|
| 932 | + } |
|
| 933 | + $arg_info['sanitize_callback'] = $sanitize_callback; |
|
| 934 | + $args_info[ $field_name ] = $arg_info; |
|
| 935 | + if ($field_obj instanceof EE_Datetime_Field) { |
|
| 936 | + $gmt_arg_info = $arg_info; |
|
| 937 | + $gmt_arg_info['description'] = sprintf( |
|
| 938 | + esc_html__( |
|
| 939 | + '%1$s - the value for this field in UTC. Ignored if %2$s is provided.', |
|
| 940 | + 'event_espresso' |
|
| 941 | + ), |
|
| 942 | + $field_obj->get_nicename(), |
|
| 943 | + $field_name |
|
| 944 | + ); |
|
| 945 | + $args_info[ $field_name . '_gmt' ] = $gmt_arg_info; |
|
| 946 | + } |
|
| 947 | + } |
|
| 948 | + return $args_info; |
|
| 949 | + } |
|
| 950 | + |
|
| 951 | + |
|
| 952 | + /** |
|
| 953 | + * Replacement for WP API's 'rest_parse_request_arg'. |
|
| 954 | + * If the value is blank but not required, don't bother validating it. |
|
| 955 | + * Also, it uses our email validation instead of WP API's default. |
|
| 956 | + * |
|
| 957 | + * @param $value |
|
| 958 | + * @param WP_REST_Request $request |
|
| 959 | + * @param $param |
|
| 960 | + * @return bool|true|WP_Error |
|
| 961 | + * @throws InvalidArgumentException |
|
| 962 | + * @throws InvalidInterfaceException |
|
| 963 | + * @throws InvalidDataTypeException |
|
| 964 | + */ |
|
| 965 | + public static function default_sanitize_callback($value, WP_REST_Request $request, $param) |
|
| 966 | + { |
|
| 967 | + $attributes = $request->get_attributes(); |
|
| 968 | + if (! isset($attributes['args'][ $param ]) |
|
| 969 | + || ! is_array($attributes['args'][ $param ])) { |
|
| 970 | + $validation_result = true; |
|
| 971 | + } else { |
|
| 972 | + $args = $attributes['args'][ $param ]; |
|
| 973 | + if (( |
|
| 974 | + $value === '' |
|
| 975 | + || $value === null |
|
| 976 | + ) |
|
| 977 | + && (! isset($args['required']) |
|
| 978 | + || $args['required'] === false |
|
| 979 | + ) |
|
| 980 | + ) { |
|
| 981 | + // not required and not provided? that's cool |
|
| 982 | + $validation_result = true; |
|
| 983 | + } elseif (isset($args['format']) |
|
| 984 | + && $args['format'] === 'email' |
|
| 985 | + ) { |
|
| 986 | + $validation_result = true; |
|
| 987 | + if (! self::_validate_email($value)) { |
|
| 988 | + $validation_result = new WP_Error( |
|
| 989 | + 'rest_invalid_param', |
|
| 990 | + esc_html__( |
|
| 991 | + 'The email address is not valid or does not exist.', |
|
| 992 | + 'event_espresso' |
|
| 993 | + ) |
|
| 994 | + ); |
|
| 995 | + } |
|
| 996 | + } else { |
|
| 997 | + $validation_result = rest_validate_value_from_schema($value, $args, $param); |
|
| 998 | + } |
|
| 999 | + } |
|
| 1000 | + if (is_wp_error($validation_result)) { |
|
| 1001 | + return $validation_result; |
|
| 1002 | + } |
|
| 1003 | + return rest_sanitize_request_arg($value, $request, $param); |
|
| 1004 | + } |
|
| 1005 | + |
|
| 1006 | + |
|
| 1007 | + /** |
|
| 1008 | + * Returns whether or not this email address is valid. Copied from EE_Email_Validation_Strategy::_validate_email() |
|
| 1009 | + * |
|
| 1010 | + * @param $email |
|
| 1011 | + * @return bool |
|
| 1012 | + * @throws InvalidArgumentException |
|
| 1013 | + * @throws InvalidInterfaceException |
|
| 1014 | + * @throws InvalidDataTypeException |
|
| 1015 | + */ |
|
| 1016 | + protected static function _validate_email($email) |
|
| 1017 | + { |
|
| 1018 | + try { |
|
| 1019 | + EmailAddressFactory::create($email); |
|
| 1020 | + return true; |
|
| 1021 | + } catch (EmailValidationException $e) { |
|
| 1022 | + return false; |
|
| 1023 | + } |
|
| 1024 | + } |
|
| 1025 | + |
|
| 1026 | + |
|
| 1027 | + /** |
|
| 1028 | + * Gets routes for the config |
|
| 1029 | + * |
|
| 1030 | + * @return array @see _register_model_routes |
|
| 1031 | + * @deprecated since version 4.9.1 |
|
| 1032 | + */ |
|
| 1033 | + protected function _register_config_routes() |
|
| 1034 | + { |
|
| 1035 | + $config_routes = array(); |
|
| 1036 | + foreach (self::versions_served() as $version => $hidden_endpoint) { |
|
| 1037 | + $config_routes[ self::ee_api_namespace . $version ] = $this->_get_config_route_data_for_version( |
|
| 1038 | + $version, |
|
| 1039 | + $hidden_endpoint |
|
| 1040 | + ); |
|
| 1041 | + } |
|
| 1042 | + return $config_routes; |
|
| 1043 | + } |
|
| 1044 | + |
|
| 1045 | + |
|
| 1046 | + /** |
|
| 1047 | + * Gets routes for the config for the specified version |
|
| 1048 | + * |
|
| 1049 | + * @param string $version |
|
| 1050 | + * @param boolean $hidden_endpoint |
|
| 1051 | + * @return array |
|
| 1052 | + */ |
|
| 1053 | + protected function _get_config_route_data_for_version($version, $hidden_endpoint) |
|
| 1054 | + { |
|
| 1055 | + return array( |
|
| 1056 | + 'config' => array( |
|
| 1057 | + array( |
|
| 1058 | + 'callback' => array( |
|
| 1059 | + 'EventEspresso\core\libraries\rest_api\controllers\config\Read', |
|
| 1060 | + 'handleRequest', |
|
| 1061 | + ), |
|
| 1062 | + 'methods' => WP_REST_Server::READABLE, |
|
| 1063 | + 'hidden_endpoint' => $hidden_endpoint, |
|
| 1064 | + 'callback_args' => array($version), |
|
| 1065 | + ), |
|
| 1066 | + ), |
|
| 1067 | + 'site_info' => array( |
|
| 1068 | + array( |
|
| 1069 | + 'callback' => array( |
|
| 1070 | + 'EventEspresso\core\libraries\rest_api\controllers\config\Read', |
|
| 1071 | + 'handleRequestSiteInfo', |
|
| 1072 | + ), |
|
| 1073 | + 'methods' => WP_REST_Server::READABLE, |
|
| 1074 | + 'hidden_endpoint' => $hidden_endpoint, |
|
| 1075 | + 'callback_args' => array($version), |
|
| 1076 | + ), |
|
| 1077 | + ), |
|
| 1078 | + ); |
|
| 1079 | + } |
|
| 1080 | + |
|
| 1081 | + |
|
| 1082 | + /** |
|
| 1083 | + * Gets the meta info routes |
|
| 1084 | + * |
|
| 1085 | + * @return array @see _register_model_routes |
|
| 1086 | + * @deprecated since version 4.9.1 |
|
| 1087 | + */ |
|
| 1088 | + protected function _register_meta_routes() |
|
| 1089 | + { |
|
| 1090 | + $meta_routes = array(); |
|
| 1091 | + foreach (self::versions_served() as $version => $hidden_endpoint) { |
|
| 1092 | + $meta_routes[ self::ee_api_namespace . $version ] = $this->_get_meta_route_data_for_version( |
|
| 1093 | + $version, |
|
| 1094 | + $hidden_endpoint |
|
| 1095 | + ); |
|
| 1096 | + } |
|
| 1097 | + return $meta_routes; |
|
| 1098 | + } |
|
| 1099 | + |
|
| 1100 | + |
|
| 1101 | + /** |
|
| 1102 | + * @param string $version |
|
| 1103 | + * @param boolean $hidden_endpoint |
|
| 1104 | + * @return array |
|
| 1105 | + */ |
|
| 1106 | + protected function _get_meta_route_data_for_version($version, $hidden_endpoint = false) |
|
| 1107 | + { |
|
| 1108 | + return array( |
|
| 1109 | + 'resources' => array( |
|
| 1110 | + array( |
|
| 1111 | + 'callback' => array( |
|
| 1112 | + 'EventEspresso\core\libraries\rest_api\controllers\model\Meta', |
|
| 1113 | + 'handleRequestModelsMeta', |
|
| 1114 | + ), |
|
| 1115 | + 'methods' => WP_REST_Server::READABLE, |
|
| 1116 | + 'hidden_endpoint' => $hidden_endpoint, |
|
| 1117 | + 'callback_args' => array($version), |
|
| 1118 | + ), |
|
| 1119 | + ), |
|
| 1120 | + ); |
|
| 1121 | + } |
|
| 1122 | + |
|
| 1123 | + |
|
| 1124 | + /** |
|
| 1125 | + * Tries to hide old 4.6 endpoints from the |
|
| 1126 | + * |
|
| 1127 | + * @param array $route_data |
|
| 1128 | + * @return array |
|
| 1129 | + * @throws \EE_Error |
|
| 1130 | + */ |
|
| 1131 | + public static function hide_old_endpoints($route_data) |
|
| 1132 | + { |
|
| 1133 | + // allow API clients to override which endpoints get hidden, in case |
|
| 1134 | + // they want to discover particular endpoints |
|
| 1135 | + // also, we don't have access to the request so we have to just grab it from the superglobal |
|
| 1136 | + $force_show_ee_namespace = ltrim( |
|
| 1137 | + EEH_Array::is_set($_REQUEST, 'force_show_ee_namespace', ''), |
|
| 1138 | + '/' |
|
| 1139 | + ); |
|
| 1140 | + foreach (EED_Core_Rest_Api::get_ee_route_data() as $namespace => $relative_urls) { |
|
| 1141 | + foreach ($relative_urls as $resource_name => $endpoints) { |
|
| 1142 | + foreach ($endpoints as $key => $endpoint) { |
|
| 1143 | + // skip schema and other route options |
|
| 1144 | + if (! is_numeric($key)) { |
|
| 1145 | + continue; |
|
| 1146 | + } |
|
| 1147 | + // by default, hide "hidden_endpoint"s, unless the request indicates |
|
| 1148 | + // to $force_show_ee_namespace, in which case only show that one |
|
| 1149 | + // namespace's endpoints (and hide all others) |
|
| 1150 | + if (($force_show_ee_namespace !== '' && $force_show_ee_namespace !== $namespace) |
|
| 1151 | + || ($endpoint['hidden_endpoint'] && $force_show_ee_namespace === '') |
|
| 1152 | + ) { |
|
| 1153 | + $full_route = '/' . ltrim($namespace, '/'); |
|
| 1154 | + $full_route .= '/' . ltrim($resource_name, '/'); |
|
| 1155 | + unset($route_data[ $full_route ]); |
|
| 1156 | + } |
|
| 1157 | + } |
|
| 1158 | + } |
|
| 1159 | + } |
|
| 1160 | + return $route_data; |
|
| 1161 | + } |
|
| 1162 | + |
|
| 1163 | + |
|
| 1164 | + /** |
|
| 1165 | + * Returns an array describing which versions of core support serving requests for. |
|
| 1166 | + * Keys are core versions' major and minor version, and values are the |
|
| 1167 | + * LOWEST requested version they can serve. Eg, 4.7 can serve requests for 4.6-like |
|
| 1168 | + * data by just removing a few models and fields from the responses. However, 4.15 might remove |
|
| 1169 | + * the answers table entirely, in which case it would be very difficult for |
|
| 1170 | + * it to serve 4.6-style responses. |
|
| 1171 | + * Versions of core that are missing from this array are unknowns. |
|
| 1172 | + * previous ver |
|
| 1173 | + * |
|
| 1174 | + * @return array |
|
| 1175 | + */ |
|
| 1176 | + public static function version_compatibilities() |
|
| 1177 | + { |
|
| 1178 | + return apply_filters( |
|
| 1179 | + 'FHEE__EED_Core_REST_API__version_compatibilities', |
|
| 1180 | + array( |
|
| 1181 | + '4.8.29' => '4.8.29', |
|
| 1182 | + '4.8.33' => '4.8.29', |
|
| 1183 | + '4.8.34' => '4.8.29', |
|
| 1184 | + '4.8.36' => '4.8.29', |
|
| 1185 | + ) |
|
| 1186 | + ); |
|
| 1187 | + } |
|
| 1188 | + |
|
| 1189 | + |
|
| 1190 | + /** |
|
| 1191 | + * Gets the latest API version served. Eg if there |
|
| 1192 | + * are two versions served of the API, 4.8.29 and 4.8.32, and |
|
| 1193 | + * we are on core version 4.8.34, it will return the string "4.8.32" |
|
| 1194 | + * |
|
| 1195 | + * @return string |
|
| 1196 | + */ |
|
| 1197 | + public static function latest_rest_api_version() |
|
| 1198 | + { |
|
| 1199 | + $versions_served = \EED_Core_Rest_Api::versions_served(); |
|
| 1200 | + $versions_served_keys = array_keys($versions_served); |
|
| 1201 | + return end($versions_served_keys); |
|
| 1202 | + } |
|
| 1203 | + |
|
| 1204 | + |
|
| 1205 | + /** |
|
| 1206 | + * Using EED_Core_Rest_Api::version_compatibilities(), determines what version of |
|
| 1207 | + * EE the API can serve requests for. Eg, if we are on 4.15 of core, and |
|
| 1208 | + * we can serve requests from 4.12 or later, this will return array( '4.12', '4.13', '4.14', '4.15' ). |
|
| 1209 | + * We also indicate whether or not this version should be put in the index or not |
|
| 1210 | + * |
|
| 1211 | + * @return array keys are API version numbers (just major and minor numbers), and values |
|
| 1212 | + * are whether or not they should be hidden |
|
| 1213 | + */ |
|
| 1214 | + public static function versions_served() |
|
| 1215 | + { |
|
| 1216 | + $versions_served = array(); |
|
| 1217 | + $possibly_served_versions = EED_Core_Rest_Api::version_compatibilities(); |
|
| 1218 | + $lowest_compatible_version = end($possibly_served_versions); |
|
| 1219 | + reset($possibly_served_versions); |
|
| 1220 | + $versions_served_historically = array_keys($possibly_served_versions); |
|
| 1221 | + $latest_version = end($versions_served_historically); |
|
| 1222 | + reset($versions_served_historically); |
|
| 1223 | + // for each version of core we have ever served: |
|
| 1224 | + foreach ($versions_served_historically as $key_versioned_endpoint) { |
|
| 1225 | + // if it's not above the current core version, and it's compatible with the current version of core |
|
| 1226 | + if ($key_versioned_endpoint === $latest_version) { |
|
| 1227 | + // don't hide the latest version in the index |
|
| 1228 | + $versions_served[ $key_versioned_endpoint ] = false; |
|
| 1229 | + } elseif ($key_versioned_endpoint >= $lowest_compatible_version |
|
| 1230 | + && $key_versioned_endpoint < EED_Core_Rest_Api::core_version() |
|
| 1231 | + ) { |
|
| 1232 | + // include, but hide, previous versions which are still supported |
|
| 1233 | + $versions_served[ $key_versioned_endpoint ] = true; |
|
| 1234 | + } elseif (apply_filters( |
|
| 1235 | + 'FHEE__EED_Core_Rest_Api__versions_served__include_incompatible_versions', |
|
| 1236 | + false, |
|
| 1237 | + $possibly_served_versions |
|
| 1238 | + )) { |
|
| 1239 | + // if a version is no longer supported, don't include it in index or list of versions served |
|
| 1240 | + $versions_served[ $key_versioned_endpoint ] = true; |
|
| 1241 | + } |
|
| 1242 | + } |
|
| 1243 | + return $versions_served; |
|
| 1244 | + } |
|
| 1245 | + |
|
| 1246 | + |
|
| 1247 | + /** |
|
| 1248 | + * Gets the major and minor version of EE core's version string |
|
| 1249 | + * |
|
| 1250 | + * @return string |
|
| 1251 | + */ |
|
| 1252 | + public static function core_version() |
|
| 1253 | + { |
|
| 1254 | + return apply_filters( |
|
| 1255 | + 'FHEE__EED_Core_REST_API__core_version', |
|
| 1256 | + implode( |
|
| 1257 | + '.', |
|
| 1258 | + array_slice( |
|
| 1259 | + explode( |
|
| 1260 | + '.', |
|
| 1261 | + espresso_version() |
|
| 1262 | + ), |
|
| 1263 | + 0, |
|
| 1264 | + 3 |
|
| 1265 | + ) |
|
| 1266 | + ) |
|
| 1267 | + ); |
|
| 1268 | + } |
|
| 1269 | + |
|
| 1270 | + |
|
| 1271 | + /** |
|
| 1272 | + * Gets the default limit that should be used when querying for resources |
|
| 1273 | + * |
|
| 1274 | + * @return int |
|
| 1275 | + */ |
|
| 1276 | + public static function get_default_query_limit() |
|
| 1277 | + { |
|
| 1278 | + // we actually don't use a const because we want folks to always use |
|
| 1279 | + // this method, not the const directly |
|
| 1280 | + return apply_filters( |
|
| 1281 | + 'FHEE__EED_Core_Rest_Api__get_default_query_limit', |
|
| 1282 | + 50 |
|
| 1283 | + ); |
|
| 1284 | + } |
|
| 1285 | + |
|
| 1286 | + |
|
| 1287 | + /** |
|
| 1288 | + * |
|
| 1289 | + * @param string $version api version string (i.e. '4.8.36') |
|
| 1290 | + * @return array |
|
| 1291 | + */ |
|
| 1292 | + public static function getCollectionRoutesIndexedByModelName($version = '') |
|
| 1293 | + { |
|
| 1294 | + $version = empty($version) ? self::latest_rest_api_version() : $version; |
|
| 1295 | + $model_names = self::model_names_with_plural_routes($version); |
|
| 1296 | + $collection_routes = array(); |
|
| 1297 | + foreach ($model_names as $model_name => $model_class_name) { |
|
| 1298 | + $collection_routes[ strtolower($model_name) ] = '/' . self::ee_api_namespace . $version . '/' |
|
| 1299 | + . EEH_Inflector::pluralize_and_lower($model_name); |
|
| 1300 | + } |
|
| 1301 | + return $collection_routes; |
|
| 1302 | + } |
|
| 1303 | + |
|
| 1304 | + |
|
| 1305 | + /** |
|
| 1306 | + * Returns an array of primary key names indexed by model names. |
|
| 1307 | + * @param string $version |
|
| 1308 | + * @return array |
|
| 1309 | + */ |
|
| 1310 | + public static function getPrimaryKeyNamesIndexedByModelName($version = '') |
|
| 1311 | + { |
|
| 1312 | + $version = empty($version) ? self::latest_rest_api_version() : $version; |
|
| 1313 | + $model_names = self::model_names_with_plural_routes($version); |
|
| 1314 | + $primary_key_items = array(); |
|
| 1315 | + foreach ($model_names as $model_name => $model_class_name) { |
|
| 1316 | + $primary_keys = $model_class_name::instance()->get_combined_primary_key_fields(); |
|
| 1317 | + foreach ($primary_keys as $primary_key_name => $primary_key_field) { |
|
| 1318 | + if (count($primary_keys) > 1) { |
|
| 1319 | + $primary_key_items[ strtolower($model_name) ][] = $primary_key_name; |
|
| 1320 | + } else { |
|
| 1321 | + $primary_key_items[ strtolower($model_name) ] = $primary_key_name; |
|
| 1322 | + } |
|
| 1323 | + } |
|
| 1324 | + } |
|
| 1325 | + return $primary_key_items; |
|
| 1326 | + } |
|
| 1327 | + |
|
| 1328 | + /** |
|
| 1329 | + * Determines the EE REST API debug mode is activated, or not. |
|
| 1330 | + * @since $VID:$ |
|
| 1331 | + * @return bool |
|
| 1332 | + */ |
|
| 1333 | + public static function debugMode() |
|
| 1334 | + { |
|
| 1335 | + static $debug_mode = null; // could be class prop |
|
| 1336 | + if ($debug_mode === null) { |
|
| 1337 | + $debug_mode = defined('EE_REST_API_DEBUG_MODE') && EE_REST_API_DEBUG_MODE; |
|
| 1338 | + } |
|
| 1339 | + return $debug_mode; |
|
| 1340 | + } |
|
| 1341 | + |
|
| 1342 | + |
|
| 1343 | + |
|
| 1344 | + /** |
|
| 1345 | + * run - initial module setup |
|
| 1346 | + * |
|
| 1347 | + * @access public |
|
| 1348 | + * @param WP $WP |
|
| 1349 | + * @return void |
|
| 1350 | + */ |
|
| 1351 | + public function run($WP) |
|
| 1352 | + { |
|
| 1353 | + } |
|
| 1354 | 1354 | } |
@@ -123,7 +123,7 @@ discard block |
||
| 123 | 123 | */ |
| 124 | 124 | protected static function _set_hooks_for_changes() |
| 125 | 125 | { |
| 126 | - $folder_contents = EEH_File::get_contents_of_folders(array(EE_LIBRARIES . 'rest_api' . DS . 'changes'), false); |
|
| 126 | + $folder_contents = EEH_File::get_contents_of_folders(array(EE_LIBRARIES.'rest_api'.DS.'changes'), false); |
|
| 127 | 127 | foreach ($folder_contents as $classname_in_namespace => $filepath) { |
| 128 | 128 | // ignore the base parent class |
| 129 | 129 | // and legacy named classes |
@@ -132,7 +132,7 @@ discard block |
||
| 132 | 132 | ) { |
| 133 | 133 | continue; |
| 134 | 134 | } |
| 135 | - $full_classname = 'EventEspresso\core\libraries\rest_api\changes\\' . $classname_in_namespace; |
|
| 135 | + $full_classname = 'EventEspresso\core\libraries\rest_api\changes\\'.$classname_in_namespace; |
|
| 136 | 136 | if (class_exists($full_classname)) { |
| 137 | 137 | $instance_of_class = new $full_classname; |
| 138 | 138 | if ($instance_of_class instanceof ChangesInBase) { |
@@ -177,10 +177,10 @@ discard block |
||
| 177 | 177 | * } |
| 178 | 178 | */ |
| 179 | 179 | // skip route options |
| 180 | - if (! is_numeric($endpoint_key)) { |
|
| 180 | + if ( ! is_numeric($endpoint_key)) { |
|
| 181 | 181 | continue; |
| 182 | 182 | } |
| 183 | - if (! isset($data_for_single_endpoint['callback'], $data_for_single_endpoint['methods'])) { |
|
| 183 | + if ( ! isset($data_for_single_endpoint['callback'], $data_for_single_endpoint['methods'])) { |
|
| 184 | 184 | throw new EE_Error( |
| 185 | 185 | esc_html__( |
| 186 | 186 | // @codingStandardsIgnoreStart |
@@ -201,7 +201,7 @@ discard block |
||
| 201 | 201 | } |
| 202 | 202 | if (isset($data_for_single_endpoint['callback_args'])) { |
| 203 | 203 | $callback_args = $data_for_single_endpoint['callback_args']; |
| 204 | - $single_endpoint_args['callback'] = function (\WP_REST_Request $request) use ( |
|
| 204 | + $single_endpoint_args['callback'] = function(\WP_REST_Request $request) use ( |
|
| 205 | 205 | $callback, |
| 206 | 206 | $callback_args |
| 207 | 207 | ) { |
@@ -220,7 +220,7 @@ discard block |
||
| 220 | 220 | $schema_route_data = $data_for_multiple_endpoints['schema']; |
| 221 | 221 | $schema_callback = $schema_route_data['schema_callback']; |
| 222 | 222 | $callback_args = $schema_route_data['callback_args']; |
| 223 | - $multiple_endpoint_args['schema'] = function () use ($schema_callback, $callback_args) { |
|
| 223 | + $multiple_endpoint_args['schema'] = function() use ($schema_callback, $callback_args) { |
|
| 224 | 224 | return call_user_func_array( |
| 225 | 225 | $schema_callback, |
| 226 | 226 | $callback_args |
@@ -262,7 +262,7 @@ discard block |
||
| 262 | 262 | { |
| 263 | 263 | // delete the saved EE REST API routes |
| 264 | 264 | foreach (EED_Core_Rest_Api::versions_served() as $version => $hidden) { |
| 265 | - delete_option(EED_Core_Rest_Api::saved_routes_option_names . $version); |
|
| 265 | + delete_option(EED_Core_Rest_Api::saved_routes_option_names.$version); |
|
| 266 | 266 | } |
| 267 | 267 | } |
| 268 | 268 | |
@@ -281,7 +281,7 @@ discard block |
||
| 281 | 281 | { |
| 282 | 282 | $ee_routes = array(); |
| 283 | 283 | foreach (self::versions_served() as $version => $hidden_endpoints) { |
| 284 | - $ee_routes[ self::ee_api_namespace . $version ] = self::_get_ee_route_data_for_version( |
|
| 284 | + $ee_routes[self::ee_api_namespace.$version] = self::_get_ee_route_data_for_version( |
|
| 285 | 285 | $version, |
| 286 | 286 | $hidden_endpoints |
| 287 | 287 | ); |
@@ -301,8 +301,8 @@ discard block |
||
| 301 | 301 | */ |
| 302 | 302 | protected static function _get_ee_route_data_for_version($version, $hidden_endpoints = false) |
| 303 | 303 | { |
| 304 | - $ee_routes = get_option(self::saved_routes_option_names . $version, null); |
|
| 305 | - if (! $ee_routes || EED_Core_Rest_Api::debugMode()) { |
|
| 304 | + $ee_routes = get_option(self::saved_routes_option_names.$version, null); |
|
| 305 | + if ( ! $ee_routes || EED_Core_Rest_Api::debugMode()) { |
|
| 306 | 306 | $ee_routes = self::_save_ee_route_data_for_version($version, $hidden_endpoints); |
| 307 | 307 | } |
| 308 | 308 | return $ee_routes; |
@@ -329,7 +329,7 @@ discard block |
||
| 329 | 329 | $instance->_get_rpc_route_data_for_version($version, $hidden_endpoints) |
| 330 | 330 | ) |
| 331 | 331 | ); |
| 332 | - $option_name = self::saved_routes_option_names . $version; |
|
| 332 | + $option_name = self::saved_routes_option_names.$version; |
|
| 333 | 333 | if (get_option($option_name)) { |
| 334 | 334 | update_option($option_name, $routes, true); |
| 335 | 335 | } else { |
@@ -374,8 +374,8 @@ discard block |
||
| 374 | 374 | { |
| 375 | 375 | $model_routes = array(); |
| 376 | 376 | foreach (self::versions_served() as $version => $hidden_endpoint) { |
| 377 | - $model_routes[ EED_Core_Rest_Api::ee_api_namespace |
|
| 378 | - . $version ] = $this->_get_config_route_data_for_version($version, $hidden_endpoint); |
|
| 377 | + $model_routes[EED_Core_Rest_Api::ee_api_namespace |
|
| 378 | + . $version] = $this->_get_config_route_data_for_version($version, $hidden_endpoint); |
|
| 379 | 379 | } |
| 380 | 380 | return $model_routes; |
| 381 | 381 | } |
@@ -444,13 +444,13 @@ discard block |
||
| 444 | 444 | foreach (EED_Core_Rest_Api::model_names_with_plural_routes($version) as $model_name => $model_classname) { |
| 445 | 445 | $model = \EE_Registry::instance()->load_model($model_name); |
| 446 | 446 | // if this isn't a valid model then let's skip iterate to the next item in the loop. |
| 447 | - if (! $model instanceof EEM_Base) { |
|
| 447 | + if ( ! $model instanceof EEM_Base) { |
|
| 448 | 448 | continue; |
| 449 | 449 | } |
| 450 | 450 | // yes we could just register one route for ALL models, but then they wouldn't show up in the index |
| 451 | 451 | $plural_model_route = EED_Core_Rest_Api::get_collection_route($model); |
| 452 | 452 | $singular_model_route = EED_Core_Rest_Api::get_entity_route($model, '(?P<id>[^\/]+)'); |
| 453 | - $model_routes[ $plural_model_route ] = array( |
|
| 453 | + $model_routes[$plural_model_route] = array( |
|
| 454 | 454 | array( |
| 455 | 455 | 'callback' => array( |
| 456 | 456 | 'EventEspresso\core\libraries\rest_api\controllers\model\Read', |
@@ -461,7 +461,7 @@ discard block |
||
| 461 | 461 | 'hidden_endpoint' => $hidden_endpoint, |
| 462 | 462 | 'args' => $this->_get_read_query_params($model, $version), |
| 463 | 463 | '_links' => array( |
| 464 | - 'self' => rest_url(EED_Core_Rest_Api::ee_api_namespace . $version . $singular_model_route), |
|
| 464 | + 'self' => rest_url(EED_Core_Rest_Api::ee_api_namespace.$version.$singular_model_route), |
|
| 465 | 465 | ), |
| 466 | 466 | ), |
| 467 | 467 | 'schema' => array( |
@@ -472,7 +472,7 @@ discard block |
||
| 472 | 472 | 'callback_args' => array($version, $model_name), |
| 473 | 473 | ), |
| 474 | 474 | ); |
| 475 | - $model_routes[ $singular_model_route ] = array( |
|
| 475 | + $model_routes[$singular_model_route] = array( |
|
| 476 | 476 | array( |
| 477 | 477 | 'callback' => array( |
| 478 | 478 | 'EventEspresso\core\libraries\rest_api\controllers\model\Read', |
@@ -489,7 +489,7 @@ discard block |
||
| 489 | 489 | EED_Core_Rest_Api::should_have_write_endpoints($model), |
| 490 | 490 | $model |
| 491 | 491 | )) { |
| 492 | - $model_routes[ $plural_model_route ][] = array( |
|
| 492 | + $model_routes[$plural_model_route][] = array( |
|
| 493 | 493 | 'callback' => array( |
| 494 | 494 | 'EventEspresso\core\libraries\rest_api\controllers\model\Write', |
| 495 | 495 | 'handleRequestInsert', |
@@ -499,8 +499,8 @@ discard block |
||
| 499 | 499 | 'hidden_endpoint' => $hidden_endpoint, |
| 500 | 500 | 'args' => $this->_get_write_params($model_name, $model_version_info, true), |
| 501 | 501 | ); |
| 502 | - $model_routes[ $singular_model_route ] = array_merge( |
|
| 503 | - $model_routes[ $singular_model_route ], |
|
| 502 | + $model_routes[$singular_model_route] = array_merge( |
|
| 503 | + $model_routes[$singular_model_route], |
|
| 504 | 504 | array( |
| 505 | 505 | array( |
| 506 | 506 | 'callback' => array( |
@@ -531,7 +531,7 @@ discard block |
||
| 531 | 531 | '(?P<id>[^\/]+)', |
| 532 | 532 | $relation_obj |
| 533 | 533 | ); |
| 534 | - $model_routes[ $related_route ] = array( |
|
| 534 | + $model_routes[$related_route] = array( |
|
| 535 | 535 | array( |
| 536 | 536 | 'callback' => array( |
| 537 | 537 | 'EventEspresso\core\libraries\rest_api\controllers\model\Read', |
@@ -544,8 +544,8 @@ discard block |
||
| 544 | 544 | ), |
| 545 | 545 | ); |
| 546 | 546 | |
| 547 | - $related_write_route = $related_route . '/' . '(?P<related_id>[^\/]+)'; |
|
| 548 | - $model_routes[ $related_write_route ] = array( |
|
| 547 | + $related_write_route = $related_route.'/'.'(?P<related_id>[^\/]+)'; |
|
| 548 | + $model_routes[$related_write_route] = array( |
|
| 549 | 549 | array( |
| 550 | 550 | 'callback' => array( |
| 551 | 551 | 'EventEspresso\core\libraries\rest_api\controllers\model\Write', |
@@ -598,7 +598,7 @@ discard block |
||
| 598 | 598 | */ |
| 599 | 599 | public static function get_entity_route($model, $id) |
| 600 | 600 | { |
| 601 | - return EED_Core_Rest_Api::get_collection_route($model) . '/' . $id; |
|
| 601 | + return EED_Core_Rest_Api::get_collection_route($model).'/'.$id; |
|
| 602 | 602 | } |
| 603 | 603 | |
| 604 | 604 | |
@@ -618,7 +618,7 @@ discard block |
||
| 618 | 618 | $relation_obj->get_other_model()->get_this_model_name(), |
| 619 | 619 | $relation_obj |
| 620 | 620 | ); |
| 621 | - return EED_Core_Rest_Api::get_entity_route($model, $id) . '/' . $related_model_name_endpoint_part; |
|
| 621 | + return EED_Core_Rest_Api::get_entity_route($model, $id).'/'.$related_model_name_endpoint_part; |
|
| 622 | 622 | } |
| 623 | 623 | |
| 624 | 624 | |
@@ -632,7 +632,7 @@ discard block |
||
| 632 | 632 | */ |
| 633 | 633 | public static function get_versioned_route_to($relative_route, $version = '4.8.36') |
| 634 | 634 | { |
| 635 | - return '/' . EED_Core_Rest_Api::ee_api_namespace . $version . '/' . $relative_route; |
|
| 635 | + return '/'.EED_Core_Rest_Api::ee_api_namespace.$version.'/'.$relative_route; |
|
| 636 | 636 | } |
| 637 | 637 | |
| 638 | 638 | |
@@ -646,7 +646,7 @@ discard block |
||
| 646 | 646 | { |
| 647 | 647 | $routes = array(); |
| 648 | 648 | foreach (self::versions_served() as $version => $hidden_endpoint) { |
| 649 | - $routes[ self::ee_api_namespace . $version ] = $this->_get_rpc_route_data_for_version( |
|
| 649 | + $routes[self::ee_api_namespace.$version] = $this->_get_rpc_route_data_for_version( |
|
| 650 | 650 | $version, |
| 651 | 651 | $hidden_endpoint |
| 652 | 652 | ); |
@@ -769,12 +769,12 @@ discard block |
||
| 769 | 769 | { |
| 770 | 770 | // if they're related through a HABTM relation, check for any non-FKs |
| 771 | 771 | $all_relation_settings = $source_model->relation_settings(); |
| 772 | - $relation_settings = $all_relation_settings[ $related_model->get_this_model_name() ]; |
|
| 772 | + $relation_settings = $all_relation_settings[$related_model->get_this_model_name()]; |
|
| 773 | 773 | $params = array(); |
| 774 | 774 | if ($relation_settings instanceof EE_HABTM_Relation && $relation_settings->hasNonKeyFields()) { |
| 775 | 775 | foreach ($relation_settings->getNonKeyFields() as $field) { |
| 776 | 776 | /* @var $field EE_Model_Field_Base */ |
| 777 | - $params[ $field->get_name() ] = array( |
|
| 777 | + $params[$field->get_name()] = array( |
|
| 778 | 778 | 'required' => ! $field->is_nullable(), |
| 779 | 779 | 'default' => ModelDataTranslator::prepareFieldValueForJson($field, $field->get_default_value(), $version), |
| 780 | 780 | 'type' => $field->getSchemaType(), |
@@ -799,7 +799,7 @@ discard block |
||
| 799 | 799 | { |
| 800 | 800 | $default_orderby = array(); |
| 801 | 801 | foreach ($model->get_combined_primary_key_fields() as $key_field) { |
| 802 | - $default_orderby[ $key_field->get_name() ] = 'ASC'; |
|
| 802 | + $default_orderby[$key_field->get_name()] = 'ASC'; |
|
| 803 | 803 | } |
| 804 | 804 | return array_merge( |
| 805 | 805 | $this->_get_response_selection_query_params($model, $version), |
@@ -833,7 +833,7 @@ discard block |
||
| 833 | 833 | 'type' => array( |
| 834 | 834 | 'object', |
| 835 | 835 | 'string', |
| 836 | - ),// because we accept a variety of types, WP core validation and sanitization |
|
| 836 | + ), // because we accept a variety of types, WP core validation and sanitization |
|
| 837 | 837 | // freaks out. We'll just validate this argument while handling the request |
| 838 | 838 | 'validate_callback' => null, |
| 839 | 839 | 'sanitize_callback' => null, |
@@ -931,7 +931,7 @@ discard block |
||
| 931 | 931 | $sanitize_callback = null; |
| 932 | 932 | } |
| 933 | 933 | $arg_info['sanitize_callback'] = $sanitize_callback; |
| 934 | - $args_info[ $field_name ] = $arg_info; |
|
| 934 | + $args_info[$field_name] = $arg_info; |
|
| 935 | 935 | if ($field_obj instanceof EE_Datetime_Field) { |
| 936 | 936 | $gmt_arg_info = $arg_info; |
| 937 | 937 | $gmt_arg_info['description'] = sprintf( |
@@ -942,7 +942,7 @@ discard block |
||
| 942 | 942 | $field_obj->get_nicename(), |
| 943 | 943 | $field_name |
| 944 | 944 | ); |
| 945 | - $args_info[ $field_name . '_gmt' ] = $gmt_arg_info; |
|
| 945 | + $args_info[$field_name.'_gmt'] = $gmt_arg_info; |
|
| 946 | 946 | } |
| 947 | 947 | } |
| 948 | 948 | return $args_info; |
@@ -965,16 +965,16 @@ discard block |
||
| 965 | 965 | public static function default_sanitize_callback($value, WP_REST_Request $request, $param) |
| 966 | 966 | { |
| 967 | 967 | $attributes = $request->get_attributes(); |
| 968 | - if (! isset($attributes['args'][ $param ]) |
|
| 969 | - || ! is_array($attributes['args'][ $param ])) { |
|
| 968 | + if ( ! isset($attributes['args'][$param]) |
|
| 969 | + || ! is_array($attributes['args'][$param])) { |
|
| 970 | 970 | $validation_result = true; |
| 971 | 971 | } else { |
| 972 | - $args = $attributes['args'][ $param ]; |
|
| 972 | + $args = $attributes['args'][$param]; |
|
| 973 | 973 | if (( |
| 974 | 974 | $value === '' |
| 975 | 975 | || $value === null |
| 976 | 976 | ) |
| 977 | - && (! isset($args['required']) |
|
| 977 | + && ( ! isset($args['required']) |
|
| 978 | 978 | || $args['required'] === false |
| 979 | 979 | ) |
| 980 | 980 | ) { |
@@ -984,7 +984,7 @@ discard block |
||
| 984 | 984 | && $args['format'] === 'email' |
| 985 | 985 | ) { |
| 986 | 986 | $validation_result = true; |
| 987 | - if (! self::_validate_email($value)) { |
|
| 987 | + if ( ! self::_validate_email($value)) { |
|
| 988 | 988 | $validation_result = new WP_Error( |
| 989 | 989 | 'rest_invalid_param', |
| 990 | 990 | esc_html__( |
@@ -1034,7 +1034,7 @@ discard block |
||
| 1034 | 1034 | { |
| 1035 | 1035 | $config_routes = array(); |
| 1036 | 1036 | foreach (self::versions_served() as $version => $hidden_endpoint) { |
| 1037 | - $config_routes[ self::ee_api_namespace . $version ] = $this->_get_config_route_data_for_version( |
|
| 1037 | + $config_routes[self::ee_api_namespace.$version] = $this->_get_config_route_data_for_version( |
|
| 1038 | 1038 | $version, |
| 1039 | 1039 | $hidden_endpoint |
| 1040 | 1040 | ); |
@@ -1089,7 +1089,7 @@ discard block |
||
| 1089 | 1089 | { |
| 1090 | 1090 | $meta_routes = array(); |
| 1091 | 1091 | foreach (self::versions_served() as $version => $hidden_endpoint) { |
| 1092 | - $meta_routes[ self::ee_api_namespace . $version ] = $this->_get_meta_route_data_for_version( |
|
| 1092 | + $meta_routes[self::ee_api_namespace.$version] = $this->_get_meta_route_data_for_version( |
|
| 1093 | 1093 | $version, |
| 1094 | 1094 | $hidden_endpoint |
| 1095 | 1095 | ); |
@@ -1141,7 +1141,7 @@ discard block |
||
| 1141 | 1141 | foreach ($relative_urls as $resource_name => $endpoints) { |
| 1142 | 1142 | foreach ($endpoints as $key => $endpoint) { |
| 1143 | 1143 | // skip schema and other route options |
| 1144 | - if (! is_numeric($key)) { |
|
| 1144 | + if ( ! is_numeric($key)) { |
|
| 1145 | 1145 | continue; |
| 1146 | 1146 | } |
| 1147 | 1147 | // by default, hide "hidden_endpoint"s, unless the request indicates |
@@ -1150,9 +1150,9 @@ discard block |
||
| 1150 | 1150 | if (($force_show_ee_namespace !== '' && $force_show_ee_namespace !== $namespace) |
| 1151 | 1151 | || ($endpoint['hidden_endpoint'] && $force_show_ee_namespace === '') |
| 1152 | 1152 | ) { |
| 1153 | - $full_route = '/' . ltrim($namespace, '/'); |
|
| 1154 | - $full_route .= '/' . ltrim($resource_name, '/'); |
|
| 1155 | - unset($route_data[ $full_route ]); |
|
| 1153 | + $full_route = '/'.ltrim($namespace, '/'); |
|
| 1154 | + $full_route .= '/'.ltrim($resource_name, '/'); |
|
| 1155 | + unset($route_data[$full_route]); |
|
| 1156 | 1156 | } |
| 1157 | 1157 | } |
| 1158 | 1158 | } |
@@ -1225,19 +1225,19 @@ discard block |
||
| 1225 | 1225 | // if it's not above the current core version, and it's compatible with the current version of core |
| 1226 | 1226 | if ($key_versioned_endpoint === $latest_version) { |
| 1227 | 1227 | // don't hide the latest version in the index |
| 1228 | - $versions_served[ $key_versioned_endpoint ] = false; |
|
| 1228 | + $versions_served[$key_versioned_endpoint] = false; |
|
| 1229 | 1229 | } elseif ($key_versioned_endpoint >= $lowest_compatible_version |
| 1230 | 1230 | && $key_versioned_endpoint < EED_Core_Rest_Api::core_version() |
| 1231 | 1231 | ) { |
| 1232 | 1232 | // include, but hide, previous versions which are still supported |
| 1233 | - $versions_served[ $key_versioned_endpoint ] = true; |
|
| 1233 | + $versions_served[$key_versioned_endpoint] = true; |
|
| 1234 | 1234 | } elseif (apply_filters( |
| 1235 | 1235 | 'FHEE__EED_Core_Rest_Api__versions_served__include_incompatible_versions', |
| 1236 | 1236 | false, |
| 1237 | 1237 | $possibly_served_versions |
| 1238 | 1238 | )) { |
| 1239 | 1239 | // if a version is no longer supported, don't include it in index or list of versions served |
| 1240 | - $versions_served[ $key_versioned_endpoint ] = true; |
|
| 1240 | + $versions_served[$key_versioned_endpoint] = true; |
|
| 1241 | 1241 | } |
| 1242 | 1242 | } |
| 1243 | 1243 | return $versions_served; |
@@ -1295,7 +1295,7 @@ discard block |
||
| 1295 | 1295 | $model_names = self::model_names_with_plural_routes($version); |
| 1296 | 1296 | $collection_routes = array(); |
| 1297 | 1297 | foreach ($model_names as $model_name => $model_class_name) { |
| 1298 | - $collection_routes[ strtolower($model_name) ] = '/' . self::ee_api_namespace . $version . '/' |
|
| 1298 | + $collection_routes[strtolower($model_name)] = '/'.self::ee_api_namespace.$version.'/' |
|
| 1299 | 1299 | . EEH_Inflector::pluralize_and_lower($model_name); |
| 1300 | 1300 | } |
| 1301 | 1301 | return $collection_routes; |
@@ -1316,9 +1316,9 @@ discard block |
||
| 1316 | 1316 | $primary_keys = $model_class_name::instance()->get_combined_primary_key_fields(); |
| 1317 | 1317 | foreach ($primary_keys as $primary_key_name => $primary_key_field) { |
| 1318 | 1318 | if (count($primary_keys) > 1) { |
| 1319 | - $primary_key_items[ strtolower($model_name) ][] = $primary_key_name; |
|
| 1319 | + $primary_key_items[strtolower($model_name)][] = $primary_key_name; |
|
| 1320 | 1320 | } else { |
| 1321 | - $primary_key_items[ strtolower($model_name) ] = $primary_key_name; |
|
| 1321 | + $primary_key_items[strtolower($model_name)] = $primary_key_name; |
|
| 1322 | 1322 | } |
| 1323 | 1323 | } |
| 1324 | 1324 | } |
@@ -18,95 +18,95 @@ |
||
| 18 | 18 | class Base extends Controller_Base |
| 19 | 19 | { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * Holds reference to the model version info, which knows the requested version |
|
| 23 | - * |
|
| 24 | - * @var ModelVersionInfo |
|
| 25 | - */ |
|
| 26 | - protected $model_version_info; |
|
| 21 | + /** |
|
| 22 | + * Holds reference to the model version info, which knows the requested version |
|
| 23 | + * |
|
| 24 | + * @var ModelVersionInfo |
|
| 25 | + */ |
|
| 26 | + protected $model_version_info; |
|
| 27 | 27 | |
| 28 | 28 | |
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * Sets the version the user requested |
|
| 32 | - * |
|
| 33 | - * @param string $version eg '4.8' |
|
| 34 | - */ |
|
| 35 | - public function setRequestedVersion($version) |
|
| 36 | - { |
|
| 37 | - parent::setRequestedVersion($version); |
|
| 38 | - $this->model_version_info = new ModelVersionInfo($version); |
|
| 39 | - } |
|
| 30 | + /** |
|
| 31 | + * Sets the version the user requested |
|
| 32 | + * |
|
| 33 | + * @param string $version eg '4.8' |
|
| 34 | + */ |
|
| 35 | + public function setRequestedVersion($version) |
|
| 36 | + { |
|
| 37 | + parent::setRequestedVersion($version); |
|
| 38 | + $this->model_version_info = new ModelVersionInfo($version); |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | 41 | |
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * Gets the object that should be used for getting any info from the models, |
|
| 45 | - * because it's takes the requested and current core version into account |
|
| 46 | - * |
|
| 47 | - * @return \EventEspresso\core\libraries\rest_api\ModelVersionInfo |
|
| 48 | - * @throws EE_Error |
|
| 49 | - */ |
|
| 50 | - public function getModelVersionInfo() |
|
| 51 | - { |
|
| 52 | - if (! $this->model_version_info) { |
|
| 53 | - throw new EE_Error( |
|
| 54 | - sprintf( |
|
| 55 | - __( |
|
| 56 | - 'Cannot use model version info before setting the requested version in the controller', |
|
| 57 | - 'event_espresso' |
|
| 58 | - ) |
|
| 59 | - ) |
|
| 60 | - ); |
|
| 61 | - } |
|
| 62 | - return $this->model_version_info; |
|
| 63 | - } |
|
| 43 | + /** |
|
| 44 | + * Gets the object that should be used for getting any info from the models, |
|
| 45 | + * because it's takes the requested and current core version into account |
|
| 46 | + * |
|
| 47 | + * @return \EventEspresso\core\libraries\rest_api\ModelVersionInfo |
|
| 48 | + * @throws EE_Error |
|
| 49 | + */ |
|
| 50 | + public function getModelVersionInfo() |
|
| 51 | + { |
|
| 52 | + if (! $this->model_version_info) { |
|
| 53 | + throw new EE_Error( |
|
| 54 | + sprintf( |
|
| 55 | + __( |
|
| 56 | + 'Cannot use model version info before setting the requested version in the controller', |
|
| 57 | + 'event_espresso' |
|
| 58 | + ) |
|
| 59 | + ) |
|
| 60 | + ); |
|
| 61 | + } |
|
| 62 | + return $this->model_version_info; |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | 65 | |
| 66 | 66 | |
| 67 | - /** |
|
| 68 | - * Determines if $object is of one of the classes of $classes. Similar to |
|
| 69 | - * in_array(), except this checks if $object is a subclass of the classnames provided |
|
| 70 | - * in $classnames |
|
| 71 | - * |
|
| 72 | - * @param object $object |
|
| 73 | - * @param array $classnames |
|
| 74 | - * @return boolean |
|
| 75 | - */ |
|
| 76 | - public function isSubclassOfOne($object, $classnames) |
|
| 77 | - { |
|
| 78 | - foreach ($classnames as $classname) { |
|
| 79 | - if (is_a($object, $classname)) { |
|
| 80 | - return true; |
|
| 81 | - } |
|
| 82 | - } |
|
| 83 | - return false; |
|
| 84 | - } |
|
| 67 | + /** |
|
| 68 | + * Determines if $object is of one of the classes of $classes. Similar to |
|
| 69 | + * in_array(), except this checks if $object is a subclass of the classnames provided |
|
| 70 | + * in $classnames |
|
| 71 | + * |
|
| 72 | + * @param object $object |
|
| 73 | + * @param array $classnames |
|
| 74 | + * @return boolean |
|
| 75 | + */ |
|
| 76 | + public function isSubclassOfOne($object, $classnames) |
|
| 77 | + { |
|
| 78 | + foreach ($classnames as $classname) { |
|
| 79 | + if (is_a($object, $classname)) { |
|
| 80 | + return true; |
|
| 81 | + } |
|
| 82 | + } |
|
| 83 | + return false; |
|
| 84 | + } |
|
| 85 | 85 | |
| 86 | - /** |
|
| 87 | - * Verifies the model name provided was valid. If so, returns the model (as an object). Otherwise, throws an |
|
| 88 | - * exception. Must be called after `setRequestedVersion()`. |
|
| 89 | - * @since $VID:$ |
|
| 90 | - * @param $model_name |
|
| 91 | - * @return EEM_Base |
|
| 92 | - * @throws EE_Error |
|
| 93 | - * @throws RestException |
|
| 94 | - */ |
|
| 95 | - protected function validateModel($model_name) |
|
| 96 | - { |
|
| 97 | - if (! $this->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
|
| 98 | - throw new RestException( |
|
| 99 | - 'endpoint_parsing_error', |
|
| 100 | - sprintf( |
|
| 101 | - __( |
|
| 102 | - 'There is no model for endpoint %s. Please contact event espresso support', |
|
| 103 | - 'event_espresso' |
|
| 104 | - ), |
|
| 105 | - $model_name |
|
| 106 | - ) |
|
| 107 | - ); |
|
| 108 | - } |
|
| 109 | - return $this->getModelVersionInfo()->loadModel($model_name); |
|
| 110 | - } |
|
| 86 | + /** |
|
| 87 | + * Verifies the model name provided was valid. If so, returns the model (as an object). Otherwise, throws an |
|
| 88 | + * exception. Must be called after `setRequestedVersion()`. |
|
| 89 | + * @since $VID:$ |
|
| 90 | + * @param $model_name |
|
| 91 | + * @return EEM_Base |
|
| 92 | + * @throws EE_Error |
|
| 93 | + * @throws RestException |
|
| 94 | + */ |
|
| 95 | + protected function validateModel($model_name) |
|
| 96 | + { |
|
| 97 | + if (! $this->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
|
| 98 | + throw new RestException( |
|
| 99 | + 'endpoint_parsing_error', |
|
| 100 | + sprintf( |
|
| 101 | + __( |
|
| 102 | + 'There is no model for endpoint %s. Please contact event espresso support', |
|
| 103 | + 'event_espresso' |
|
| 104 | + ), |
|
| 105 | + $model_name |
|
| 106 | + ) |
|
| 107 | + ); |
|
| 108 | + } |
|
| 109 | + return $this->getModelVersionInfo()->loadModel($model_name); |
|
| 110 | + } |
|
| 111 | 111 | } |
| 112 | 112 | // End of file Base.php |
@@ -49,7 +49,7 @@ discard block |
||
| 49 | 49 | */ |
| 50 | 50 | public function getModelVersionInfo() |
| 51 | 51 | { |
| 52 | - if (! $this->model_version_info) { |
|
| 52 | + if ( ! $this->model_version_info) { |
|
| 53 | 53 | throw new EE_Error( |
| 54 | 54 | sprintf( |
| 55 | 55 | __( |
@@ -94,7 +94,7 @@ discard block |
||
| 94 | 94 | */ |
| 95 | 95 | protected function validateModel($model_name) |
| 96 | 96 | { |
| 97 | - if (! $this->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
|
| 97 | + if ( ! $this->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
|
| 98 | 98 | throw new RestException( |
| 99 | 99 | 'endpoint_parsing_error', |
| 100 | 100 | sprintf( |
@@ -13,3144 +13,3144 @@ |
||
| 13 | 13 | abstract class EE_Base_Class |
| 14 | 14 | { |
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * This is an array of the original properties and values provided during construction |
|
| 18 | - * of this model object. (keys are model field names, values are their values). |
|
| 19 | - * This list is important to remember so that when we are merging data from the db, we know |
|
| 20 | - * which values to override and which to not override. |
|
| 21 | - * |
|
| 22 | - * @var array |
|
| 23 | - */ |
|
| 24 | - protected $_props_n_values_provided_in_constructor; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * Timezone |
|
| 28 | - * This gets set by the "set_timezone()" method so that we know what timezone incoming strings|timestamps are in. |
|
| 29 | - * This can also be used before a get to set what timezone you want strings coming out of the object to be in. NOT |
|
| 30 | - * all EE_Base_Class child classes use this property but any that use a EE_Datetime_Field data type will have |
|
| 31 | - * access to it. |
|
| 32 | - * |
|
| 33 | - * @var string |
|
| 34 | - */ |
|
| 35 | - protected $_timezone; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * date format |
|
| 39 | - * pattern or format for displaying dates |
|
| 40 | - * |
|
| 41 | - * @var string $_dt_frmt |
|
| 42 | - */ |
|
| 43 | - protected $_dt_frmt; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * time format |
|
| 47 | - * pattern or format for displaying time |
|
| 48 | - * |
|
| 49 | - * @var string $_tm_frmt |
|
| 50 | - */ |
|
| 51 | - protected $_tm_frmt; |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * This property is for holding a cached array of object properties indexed by property name as the key. |
|
| 55 | - * The purpose of this is for setting a cache on properties that may have calculated values after a |
|
| 56 | - * prepare_for_get. That way the cache can be checked first and the calculated property returned instead of having |
|
| 57 | - * to recalculate. Used by _set_cached_property() and _get_cached_property() methods. |
|
| 58 | - * |
|
| 59 | - * @var array |
|
| 60 | - */ |
|
| 61 | - protected $_cached_properties = array(); |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * An array containing keys of the related model, and values are either an array of related mode objects or a |
|
| 65 | - * single |
|
| 66 | - * related model object. see the model's _model_relations. The keys should match those specified. And if the |
|
| 67 | - * relation is of type EE_Belongs_To (or one of its children), then there should only be ONE related model object, |
|
| 68 | - * all others have an array) |
|
| 69 | - * |
|
| 70 | - * @var array |
|
| 71 | - */ |
|
| 72 | - protected $_model_relations = array(); |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * Array where keys are field names (see the model's _fields property) and values are their values. To see what |
|
| 76 | - * their types should be, look at what that field object returns on its prepare_for_get and prepare_for_set methods) |
|
| 77 | - * |
|
| 78 | - * @var array |
|
| 79 | - */ |
|
| 80 | - protected $_fields = array(); |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * @var boolean indicating whether or not this model object is intended to ever be saved |
|
| 84 | - * For example, we might create model objects intended to only be used for the duration |
|
| 85 | - * of this request and to be thrown away, and if they were accidentally saved |
|
| 86 | - * it would be a bug. |
|
| 87 | - */ |
|
| 88 | - protected $_allow_persist = true; |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * @var boolean indicating whether or not this model object's properties have changed since construction |
|
| 92 | - */ |
|
| 93 | - protected $_has_changes = false; |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * @var EEM_Base |
|
| 97 | - */ |
|
| 98 | - protected $_model; |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * This is a cache of results from custom selections done on a query that constructs this entity. The only purpose |
|
| 102 | - * for these values is for retrieval of the results, they are not further queryable and they are not persisted to |
|
| 103 | - * the db. They also do not automatically update if there are any changes to the data that produced their results. |
|
| 104 | - * The format is a simple array of field_alias => field_value. So for instance if a custom select was something |
|
| 105 | - * like, "Select COUNT(Registration.REG_ID) as Registration_Count ...", then the resulting value will be in this |
|
| 106 | - * array as: |
|
| 107 | - * array( |
|
| 108 | - * 'Registration_Count' => 24 |
|
| 109 | - * ); |
|
| 110 | - * Note: if the custom select configuration for the query included a data type, the value will be in the data type |
|
| 111 | - * provided for the query (@see EventEspresso\core\domain\values\model\CustomSelects::__construct phpdocs for more |
|
| 112 | - * info) |
|
| 113 | - * |
|
| 114 | - * @var array |
|
| 115 | - */ |
|
| 116 | - protected $custom_selection_results = array(); |
|
| 117 | - |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * basic constructor for Event Espresso classes, performs any necessary initialization, and verifies it's children |
|
| 121 | - * play nice |
|
| 122 | - * |
|
| 123 | - * @param array $fieldValues where each key is a field (ie, array key in the 2nd |
|
| 124 | - * layer of the model's _fields array, (eg, EVT_ID, |
|
| 125 | - * TXN_amount, QST_name, etc) and values are their values |
|
| 126 | - * @param boolean $bydb a flag for setting if the class is instantiated by the |
|
| 127 | - * corresponding db model or not. |
|
| 128 | - * @param string $timezone indicate what timezone you want any datetime fields to |
|
| 129 | - * be in when instantiating a EE_Base_Class object. |
|
| 130 | - * @param array $date_formats An array of date formats to set on construct where first |
|
| 131 | - * value is the date_format and second value is the time |
|
| 132 | - * format. |
|
| 133 | - * @throws InvalidArgumentException |
|
| 134 | - * @throws InvalidInterfaceException |
|
| 135 | - * @throws InvalidDataTypeException |
|
| 136 | - * @throws EE_Error |
|
| 137 | - * @throws ReflectionException |
|
| 138 | - */ |
|
| 139 | - protected function __construct($fieldValues = array(), $bydb = false, $timezone = '', $date_formats = array()) |
|
| 140 | - { |
|
| 141 | - $className = get_class($this); |
|
| 142 | - do_action("AHEE__{$className}__construct", $this, $fieldValues); |
|
| 143 | - $model = $this->get_model(); |
|
| 144 | - $model_fields = $model->field_settings(false); |
|
| 145 | - // ensure $fieldValues is an array |
|
| 146 | - $fieldValues = is_array($fieldValues) ? $fieldValues : array($fieldValues); |
|
| 147 | - // verify client code has not passed any invalid field names |
|
| 148 | - foreach ($fieldValues as $field_name => $field_value) { |
|
| 149 | - if (! isset($model_fields[ $field_name ])) { |
|
| 150 | - throw new EE_Error( |
|
| 151 | - sprintf( |
|
| 152 | - esc_html__( |
|
| 153 | - 'Invalid field (%s) passed to constructor of %s. Allowed fields are :%s', |
|
| 154 | - 'event_espresso' |
|
| 155 | - ), |
|
| 156 | - $field_name, |
|
| 157 | - get_class($this), |
|
| 158 | - implode(', ', array_keys($model_fields)) |
|
| 159 | - ) |
|
| 160 | - ); |
|
| 161 | - } |
|
| 162 | - } |
|
| 163 | - $this->_timezone = EEH_DTT_Helper::get_valid_timezone_string($timezone); |
|
| 164 | - if (! empty($date_formats) && is_array($date_formats)) { |
|
| 165 | - list($this->_dt_frmt, $this->_tm_frmt) = $date_formats; |
|
| 166 | - } else { |
|
| 167 | - // set default formats for date and time |
|
| 168 | - $this->_dt_frmt = (string) get_option('date_format', 'Y-m-d'); |
|
| 169 | - $this->_tm_frmt = (string) get_option('time_format', 'g:i a'); |
|
| 170 | - } |
|
| 171 | - // if db model is instantiating |
|
| 172 | - if ($bydb) { |
|
| 173 | - // client code has indicated these field values are from the database |
|
| 174 | - foreach ($model_fields as $fieldName => $field) { |
|
| 175 | - $this->set_from_db( |
|
| 176 | - $fieldName, |
|
| 177 | - isset($fieldValues[ $fieldName ]) ? $fieldValues[ $fieldName ] : null |
|
| 178 | - ); |
|
| 179 | - } |
|
| 180 | - } else { |
|
| 181 | - // we're constructing a brand |
|
| 182 | - // new instance of the model object. Generally, this means we'll need to do more field validation |
|
| 183 | - foreach ($model_fields as $fieldName => $field) { |
|
| 184 | - $this->set( |
|
| 185 | - $fieldName, |
|
| 186 | - isset($fieldValues[ $fieldName ]) ? $fieldValues[ $fieldName ] : null, |
|
| 187 | - true |
|
| 188 | - ); |
|
| 189 | - } |
|
| 190 | - } |
|
| 191 | - // remember what values were passed to this constructor |
|
| 192 | - $this->_props_n_values_provided_in_constructor = $fieldValues; |
|
| 193 | - // remember in entity mapper |
|
| 194 | - if (! $bydb && $model->has_primary_key_field() && $this->ID()) { |
|
| 195 | - $model->add_to_entity_map($this); |
|
| 196 | - } |
|
| 197 | - // setup all the relations |
|
| 198 | - foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
|
| 199 | - if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
| 200 | - $this->_model_relations[ $relation_name ] = null; |
|
| 201 | - } else { |
|
| 202 | - $this->_model_relations[ $relation_name ] = array(); |
|
| 203 | - } |
|
| 204 | - } |
|
| 205 | - /** |
|
| 206 | - * Action done at the end of each model object construction |
|
| 207 | - * |
|
| 208 | - * @param EE_Base_Class $this the model object just created |
|
| 209 | - */ |
|
| 210 | - do_action('AHEE__EE_Base_Class__construct__finished', $this); |
|
| 211 | - } |
|
| 212 | - |
|
| 213 | - |
|
| 214 | - /** |
|
| 215 | - * Gets whether or not this model object is allowed to persist/be saved to the database. |
|
| 216 | - * |
|
| 217 | - * @return boolean |
|
| 218 | - */ |
|
| 219 | - public function allow_persist() |
|
| 220 | - { |
|
| 221 | - return $this->_allow_persist; |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - |
|
| 225 | - /** |
|
| 226 | - * Sets whether or not this model object should be allowed to be saved to the DB. |
|
| 227 | - * Normally once this is set to FALSE you wouldn't set it back to TRUE, unless |
|
| 228 | - * you got new information that somehow made you change your mind. |
|
| 229 | - * |
|
| 230 | - * @param boolean $allow_persist |
|
| 231 | - * @return boolean |
|
| 232 | - */ |
|
| 233 | - public function set_allow_persist($allow_persist) |
|
| 234 | - { |
|
| 235 | - return $this->_allow_persist = $allow_persist; |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - |
|
| 239 | - /** |
|
| 240 | - * Gets the field's original value when this object was constructed during this request. |
|
| 241 | - * This can be helpful when determining if a model object has changed or not |
|
| 242 | - * |
|
| 243 | - * @param string $field_name |
|
| 244 | - * @return mixed|null |
|
| 245 | - * @throws ReflectionException |
|
| 246 | - * @throws InvalidArgumentException |
|
| 247 | - * @throws InvalidInterfaceException |
|
| 248 | - * @throws InvalidDataTypeException |
|
| 249 | - * @throws EE_Error |
|
| 250 | - */ |
|
| 251 | - public function get_original($field_name) |
|
| 252 | - { |
|
| 253 | - if (isset($this->_props_n_values_provided_in_constructor[ $field_name ]) |
|
| 254 | - && $field_settings = $this->get_model()->field_settings_for($field_name) |
|
| 255 | - ) { |
|
| 256 | - return $field_settings->prepare_for_get($this->_props_n_values_provided_in_constructor[ $field_name ]); |
|
| 257 | - } |
|
| 258 | - return null; |
|
| 259 | - } |
|
| 260 | - |
|
| 261 | - |
|
| 262 | - /** |
|
| 263 | - * @param EE_Base_Class $obj |
|
| 264 | - * @return string |
|
| 265 | - */ |
|
| 266 | - public function get_class($obj) |
|
| 267 | - { |
|
| 268 | - return get_class($obj); |
|
| 269 | - } |
|
| 270 | - |
|
| 271 | - |
|
| 272 | - /** |
|
| 273 | - * Overrides parent because parent expects old models. |
|
| 274 | - * This also doesn't do any validation, and won't work for serialized arrays |
|
| 275 | - * |
|
| 276 | - * @param string $field_name |
|
| 277 | - * @param mixed $field_value |
|
| 278 | - * @param bool $use_default |
|
| 279 | - * @throws InvalidArgumentException |
|
| 280 | - * @throws InvalidInterfaceException |
|
| 281 | - * @throws InvalidDataTypeException |
|
| 282 | - * @throws EE_Error |
|
| 283 | - * @throws ReflectionException |
|
| 284 | - * @throws ReflectionException |
|
| 285 | - * @throws ReflectionException |
|
| 286 | - */ |
|
| 287 | - public function set($field_name, $field_value, $use_default = false) |
|
| 288 | - { |
|
| 289 | - // if not using default and nothing has changed, and object has already been setup (has ID), |
|
| 290 | - // then don't do anything |
|
| 291 | - if (! $use_default |
|
| 292 | - && $this->_fields[ $field_name ] === $field_value |
|
| 293 | - && $this->ID() |
|
| 294 | - ) { |
|
| 295 | - return; |
|
| 296 | - } |
|
| 297 | - $model = $this->get_model(); |
|
| 298 | - $this->_has_changes = true; |
|
| 299 | - $field_obj = $model->field_settings_for($field_name); |
|
| 300 | - if ($field_obj instanceof EE_Model_Field_Base) { |
|
| 301 | - // if ( method_exists( $field_obj, 'set_timezone' )) { |
|
| 302 | - if ($field_obj instanceof EE_Datetime_Field) { |
|
| 303 | - $field_obj->set_timezone($this->_timezone); |
|
| 304 | - $field_obj->set_date_format($this->_dt_frmt); |
|
| 305 | - $field_obj->set_time_format($this->_tm_frmt); |
|
| 306 | - } |
|
| 307 | - $holder_of_value = $field_obj->prepare_for_set($field_value); |
|
| 308 | - // should the value be null? |
|
| 309 | - if (($field_value === null || $holder_of_value === null || $holder_of_value === '') && $use_default) { |
|
| 310 | - $this->_fields[ $field_name ] = $field_obj->get_default_value(); |
|
| 311 | - /** |
|
| 312 | - * To save having to refactor all the models, if a default value is used for a |
|
| 313 | - * EE_Datetime_Field, and that value is not null nor is it a DateTime |
|
| 314 | - * object. Then let's do a set again to ensure that it becomes a DateTime |
|
| 315 | - * object. |
|
| 316 | - * |
|
| 317 | - * @since 4.6.10+ |
|
| 318 | - */ |
|
| 319 | - if ($field_obj instanceof EE_Datetime_Field |
|
| 320 | - && $this->_fields[ $field_name ] !== null |
|
| 321 | - && ! $this->_fields[ $field_name ] instanceof DateTime |
|
| 322 | - ) { |
|
| 323 | - empty($this->_fields[ $field_name ]) |
|
| 324 | - ? $this->set($field_name, time()) |
|
| 325 | - : $this->set($field_name, $this->_fields[ $field_name ]); |
|
| 326 | - } |
|
| 327 | - } else { |
|
| 328 | - $this->_fields[ $field_name ] = $holder_of_value; |
|
| 329 | - } |
|
| 330 | - // if we're not in the constructor... |
|
| 331 | - // now check if what we set was a primary key |
|
| 332 | - if (// note: props_n_values_provided_in_constructor is only set at the END of the constructor |
|
| 333 | - $this->_props_n_values_provided_in_constructor |
|
| 334 | - && $field_value |
|
| 335 | - && $field_name === $model->primary_key_name() |
|
| 336 | - ) { |
|
| 337 | - // if so, we want all this object's fields to be filled either with |
|
| 338 | - // what we've explicitly set on this model |
|
| 339 | - // or what we have in the db |
|
| 340 | - // echo "setting primary key!"; |
|
| 341 | - $fields_on_model = self::_get_model(get_class($this))->field_settings(); |
|
| 342 | - $obj_in_db = self::_get_model(get_class($this))->get_one_by_ID($field_value); |
|
| 343 | - foreach ($fields_on_model as $field_obj) { |
|
| 344 | - if (! array_key_exists($field_obj->get_name(), $this->_props_n_values_provided_in_constructor) |
|
| 345 | - && $field_obj->get_name() !== $field_name |
|
| 346 | - ) { |
|
| 347 | - $this->set($field_obj->get_name(), $obj_in_db->get($field_obj->get_name())); |
|
| 348 | - } |
|
| 349 | - } |
|
| 350 | - // oh this model object has an ID? well make sure its in the entity mapper |
|
| 351 | - $model->add_to_entity_map($this); |
|
| 352 | - } |
|
| 353 | - // let's unset any cache for this field_name from the $_cached_properties property. |
|
| 354 | - $this->_clear_cached_property($field_name); |
|
| 355 | - } else { |
|
| 356 | - throw new EE_Error( |
|
| 357 | - sprintf( |
|
| 358 | - esc_html__( |
|
| 359 | - 'A valid EE_Model_Field_Base could not be found for the given field name: %s', |
|
| 360 | - 'event_espresso' |
|
| 361 | - ), |
|
| 362 | - $field_name |
|
| 363 | - ) |
|
| 364 | - ); |
|
| 365 | - } |
|
| 366 | - } |
|
| 367 | - |
|
| 368 | - |
|
| 369 | - /** |
|
| 370 | - * Set custom select values for model. |
|
| 371 | - * |
|
| 372 | - * @param array $custom_select_values |
|
| 373 | - */ |
|
| 374 | - public function setCustomSelectsValues(array $custom_select_values) |
|
| 375 | - { |
|
| 376 | - $this->custom_selection_results = $custom_select_values; |
|
| 377 | - } |
|
| 378 | - |
|
| 379 | - |
|
| 380 | - /** |
|
| 381 | - * Returns the custom select value for the provided alias if its set. |
|
| 382 | - * If not set, returns null. |
|
| 383 | - * |
|
| 384 | - * @param string $alias |
|
| 385 | - * @return string|int|float|null |
|
| 386 | - */ |
|
| 387 | - public function getCustomSelect($alias) |
|
| 388 | - { |
|
| 389 | - return isset($this->custom_selection_results[ $alias ]) |
|
| 390 | - ? $this->custom_selection_results[ $alias ] |
|
| 391 | - : null; |
|
| 392 | - } |
|
| 393 | - |
|
| 394 | - |
|
| 395 | - /** |
|
| 396 | - * This sets the field value on the db column if it exists for the given $column_name or |
|
| 397 | - * saves it to EE_Extra_Meta if the given $column_name does not match a db column. |
|
| 398 | - * |
|
| 399 | - * @see EE_message::get_column_value for related documentation on the necessity of this method. |
|
| 400 | - * @param string $field_name Must be the exact column name. |
|
| 401 | - * @param mixed $field_value The value to set. |
|
| 402 | - * @return int|bool @see EE_Base_Class::update_extra_meta() for return docs. |
|
| 403 | - * @throws InvalidArgumentException |
|
| 404 | - * @throws InvalidInterfaceException |
|
| 405 | - * @throws InvalidDataTypeException |
|
| 406 | - * @throws EE_Error |
|
| 407 | - * @throws ReflectionException |
|
| 408 | - */ |
|
| 409 | - public function set_field_or_extra_meta($field_name, $field_value) |
|
| 410 | - { |
|
| 411 | - if ($this->get_model()->has_field($field_name)) { |
|
| 412 | - $this->set($field_name, $field_value); |
|
| 413 | - return true; |
|
| 414 | - } |
|
| 415 | - // ensure this object is saved first so that extra meta can be properly related. |
|
| 416 | - $this->save(); |
|
| 417 | - return $this->update_extra_meta($field_name, $field_value); |
|
| 418 | - } |
|
| 419 | - |
|
| 420 | - |
|
| 421 | - /** |
|
| 422 | - * This retrieves the value of the db column set on this class or if that's not present |
|
| 423 | - * it will attempt to retrieve from extra_meta if found. |
|
| 424 | - * Example Usage: |
|
| 425 | - * Via EE_Message child class: |
|
| 426 | - * Due to the dynamic nature of the EE_messages system, EE_messengers will always have a "to", |
|
| 427 | - * "from", "subject", and "content" field (as represented in the EE_Message schema), however they may |
|
| 428 | - * also have additional main fields specific to the messenger. The system accommodates those extra |
|
| 429 | - * fields through the EE_Extra_Meta table. This method allows for EE_messengers to retrieve the |
|
| 430 | - * value for those extra fields dynamically via the EE_message object. |
|
| 431 | - * |
|
| 432 | - * @param string $field_name expecting the fully qualified field name. |
|
| 433 | - * @return mixed|null value for the field if found. null if not found. |
|
| 434 | - * @throws ReflectionException |
|
| 435 | - * @throws InvalidArgumentException |
|
| 436 | - * @throws InvalidInterfaceException |
|
| 437 | - * @throws InvalidDataTypeException |
|
| 438 | - * @throws EE_Error |
|
| 439 | - */ |
|
| 440 | - public function get_field_or_extra_meta($field_name) |
|
| 441 | - { |
|
| 442 | - if ($this->get_model()->has_field($field_name)) { |
|
| 443 | - $column_value = $this->get($field_name); |
|
| 444 | - } else { |
|
| 445 | - // This isn't a column in the main table, let's see if it is in the extra meta. |
|
| 446 | - $column_value = $this->get_extra_meta($field_name, true, null); |
|
| 447 | - } |
|
| 448 | - return $column_value; |
|
| 449 | - } |
|
| 450 | - |
|
| 451 | - |
|
| 452 | - /** |
|
| 453 | - * See $_timezone property for description of what the timezone property is for. This SETS the timezone internally |
|
| 454 | - * for being able to reference what timezone we are running conversions on when converting TO the internal timezone |
|
| 455 | - * (UTC Unix Timestamp) for the object OR when converting FROM the internal timezone (UTC Unix Timestamp). This is |
|
| 456 | - * available to all child classes that may be using the EE_Datetime_Field for a field data type. |
|
| 457 | - * |
|
| 458 | - * @access public |
|
| 459 | - * @param string $timezone A valid timezone string as described by @link http://www.php.net/manual/en/timezones.php |
|
| 460 | - * @return void |
|
| 461 | - * @throws InvalidArgumentException |
|
| 462 | - * @throws InvalidInterfaceException |
|
| 463 | - * @throws InvalidDataTypeException |
|
| 464 | - * @throws EE_Error |
|
| 465 | - * @throws ReflectionException |
|
| 466 | - */ |
|
| 467 | - public function set_timezone($timezone = '') |
|
| 468 | - { |
|
| 469 | - $this->_timezone = EEH_DTT_Helper::get_valid_timezone_string($timezone); |
|
| 470 | - // make sure we clear all cached properties because they won't be relevant now |
|
| 471 | - $this->_clear_cached_properties(); |
|
| 472 | - // make sure we update field settings and the date for all EE_Datetime_Fields |
|
| 473 | - $model_fields = $this->get_model()->field_settings(false); |
|
| 474 | - foreach ($model_fields as $field_name => $field_obj) { |
|
| 475 | - if ($field_obj instanceof EE_Datetime_Field) { |
|
| 476 | - $field_obj->set_timezone($this->_timezone); |
|
| 477 | - if (isset($this->_fields[ $field_name ]) && $this->_fields[ $field_name ] instanceof DateTime) { |
|
| 478 | - EEH_DTT_Helper::setTimezone($this->_fields[ $field_name ], new DateTimeZone($this->_timezone)); |
|
| 479 | - } |
|
| 480 | - } |
|
| 481 | - } |
|
| 482 | - } |
|
| 483 | - |
|
| 484 | - |
|
| 485 | - /** |
|
| 486 | - * This just returns whatever is set for the current timezone. |
|
| 487 | - * |
|
| 488 | - * @access public |
|
| 489 | - * @return string timezone string |
|
| 490 | - */ |
|
| 491 | - public function get_timezone() |
|
| 492 | - { |
|
| 493 | - return $this->_timezone; |
|
| 494 | - } |
|
| 495 | - |
|
| 496 | - |
|
| 497 | - /** |
|
| 498 | - * This sets the internal date format to what is sent in to be used as the new default for the class |
|
| 499 | - * internally instead of wp set date format options |
|
| 500 | - * |
|
| 501 | - * @since 4.6 |
|
| 502 | - * @param string $format should be a format recognizable by PHP date() functions. |
|
| 503 | - */ |
|
| 504 | - public function set_date_format($format) |
|
| 505 | - { |
|
| 506 | - $this->_dt_frmt = $format; |
|
| 507 | - // clear cached_properties because they won't be relevant now. |
|
| 508 | - $this->_clear_cached_properties(); |
|
| 509 | - } |
|
| 510 | - |
|
| 511 | - |
|
| 512 | - /** |
|
| 513 | - * This sets the internal time format string to what is sent in to be used as the new default for the |
|
| 514 | - * class internally instead of wp set time format options. |
|
| 515 | - * |
|
| 516 | - * @since 4.6 |
|
| 517 | - * @param string $format should be a format recognizable by PHP date() functions. |
|
| 518 | - */ |
|
| 519 | - public function set_time_format($format) |
|
| 520 | - { |
|
| 521 | - $this->_tm_frmt = $format; |
|
| 522 | - // clear cached_properties because they won't be relevant now. |
|
| 523 | - $this->_clear_cached_properties(); |
|
| 524 | - } |
|
| 525 | - |
|
| 526 | - |
|
| 527 | - /** |
|
| 528 | - * This returns the current internal set format for the date and time formats. |
|
| 529 | - * |
|
| 530 | - * @param bool $full if true (default), then return the full format. Otherwise will return an array |
|
| 531 | - * where the first value is the date format and the second value is the time format. |
|
| 532 | - * @return mixed string|array |
|
| 533 | - */ |
|
| 534 | - public function get_format($full = true) |
|
| 535 | - { |
|
| 536 | - return $full ? $this->_dt_frmt . ' ' . $this->_tm_frmt : array($this->_dt_frmt, $this->_tm_frmt); |
|
| 537 | - } |
|
| 538 | - |
|
| 539 | - |
|
| 540 | - /** |
|
| 541 | - * cache |
|
| 542 | - * stores the passed model object on the current model object. |
|
| 543 | - * In certain circumstances, we can use this cached model object instead of querying for another one entirely. |
|
| 544 | - * |
|
| 545 | - * @param string $relationName one of the keys in the _model_relations array on the model. Eg |
|
| 546 | - * 'Registration' associated with this model object |
|
| 547 | - * @param EE_Base_Class $object_to_cache that has a relation to this model object. (Eg, if this is a Transaction, |
|
| 548 | - * that could be a payment or a registration) |
|
| 549 | - * @param null $cache_id a string or number that will be used as the key for any Belongs_To_Many |
|
| 550 | - * items which will be stored in an array on this object |
|
| 551 | - * @throws ReflectionException |
|
| 552 | - * @throws InvalidArgumentException |
|
| 553 | - * @throws InvalidInterfaceException |
|
| 554 | - * @throws InvalidDataTypeException |
|
| 555 | - * @throws EE_Error |
|
| 556 | - * @return mixed index into cache, or just TRUE if the relation is of type Belongs_To (because there's only one |
|
| 557 | - * related thing, no array) |
|
| 558 | - */ |
|
| 559 | - public function cache($relationName = '', $object_to_cache = null, $cache_id = null) |
|
| 560 | - { |
|
| 561 | - // its entirely possible that there IS no related object yet in which case there is nothing to cache. |
|
| 562 | - if (! $object_to_cache instanceof EE_Base_Class) { |
|
| 563 | - return false; |
|
| 564 | - } |
|
| 565 | - // also get "how" the object is related, or throw an error |
|
| 566 | - if (! $relationship_to_model = $this->get_model()->related_settings_for($relationName)) { |
|
| 567 | - throw new EE_Error( |
|
| 568 | - sprintf( |
|
| 569 | - esc_html__('There is no relationship to %s on a %s. Cannot cache it', 'event_espresso'), |
|
| 570 | - $relationName, |
|
| 571 | - get_class($this) |
|
| 572 | - ) |
|
| 573 | - ); |
|
| 574 | - } |
|
| 575 | - // how many things are related ? |
|
| 576 | - if ($relationship_to_model instanceof EE_Belongs_To_Relation) { |
|
| 577 | - // if it's a "belongs to" relationship, then there's only one related model object |
|
| 578 | - // eg, if this is a registration, there's only 1 attendee for it |
|
| 579 | - // so for these model objects just set it to be cached |
|
| 580 | - $this->_model_relations[ $relationName ] = $object_to_cache; |
|
| 581 | - $return = true; |
|
| 582 | - } else { |
|
| 583 | - // otherwise, this is the "many" side of a one to many relationship, |
|
| 584 | - // so we'll add the object to the array of related objects for that type. |
|
| 585 | - // eg: if this is an event, there are many registrations for that event, |
|
| 586 | - // so we cache the registrations in an array |
|
| 587 | - if (! is_array($this->_model_relations[ $relationName ])) { |
|
| 588 | - // if for some reason, the cached item is a model object, |
|
| 589 | - // then stick that in the array, otherwise start with an empty array |
|
| 590 | - $this->_model_relations[ $relationName ] = $this->_model_relations[ $relationName ] |
|
| 591 | - instanceof |
|
| 592 | - EE_Base_Class |
|
| 593 | - ? array($this->_model_relations[ $relationName ]) : array(); |
|
| 594 | - } |
|
| 595 | - // first check for a cache_id which is normally empty |
|
| 596 | - if (! empty($cache_id)) { |
|
| 597 | - // if the cache_id exists, then it means we are purposely trying to cache this |
|
| 598 | - // with a known key that can then be used to retrieve the object later on |
|
| 599 | - $this->_model_relations[ $relationName ][ $cache_id ] = $object_to_cache; |
|
| 600 | - $return = $cache_id; |
|
| 601 | - } elseif ($object_to_cache->ID()) { |
|
| 602 | - // OR the cached object originally came from the db, so let's just use it's PK for an ID |
|
| 603 | - $this->_model_relations[ $relationName ][ $object_to_cache->ID() ] = $object_to_cache; |
|
| 604 | - $return = $object_to_cache->ID(); |
|
| 605 | - } else { |
|
| 606 | - // OR it's a new object with no ID, so just throw it in the array with an auto-incremented ID |
|
| 607 | - $this->_model_relations[ $relationName ][] = $object_to_cache; |
|
| 608 | - // move the internal pointer to the end of the array |
|
| 609 | - end($this->_model_relations[ $relationName ]); |
|
| 610 | - // and grab the key so that we can return it |
|
| 611 | - $return = key($this->_model_relations[ $relationName ]); |
|
| 612 | - } |
|
| 613 | - } |
|
| 614 | - return $return; |
|
| 615 | - } |
|
| 616 | - |
|
| 617 | - |
|
| 618 | - /** |
|
| 619 | - * For adding an item to the cached_properties property. |
|
| 620 | - * |
|
| 621 | - * @access protected |
|
| 622 | - * @param string $fieldname the property item the corresponding value is for. |
|
| 623 | - * @param mixed $value The value we are caching. |
|
| 624 | - * @param string|null $cache_type |
|
| 625 | - * @return void |
|
| 626 | - * @throws ReflectionException |
|
| 627 | - * @throws InvalidArgumentException |
|
| 628 | - * @throws InvalidInterfaceException |
|
| 629 | - * @throws InvalidDataTypeException |
|
| 630 | - * @throws EE_Error |
|
| 631 | - */ |
|
| 632 | - protected function _set_cached_property($fieldname, $value, $cache_type = null) |
|
| 633 | - { |
|
| 634 | - // first make sure this property exists |
|
| 635 | - $this->get_model()->field_settings_for($fieldname); |
|
| 636 | - $cache_type = empty($cache_type) ? 'standard' : $cache_type; |
|
| 637 | - $this->_cached_properties[ $fieldname ][ $cache_type ] = $value; |
|
| 638 | - } |
|
| 639 | - |
|
| 640 | - |
|
| 641 | - /** |
|
| 642 | - * This returns the value cached property if it exists OR the actual property value if the cache doesn't exist. |
|
| 643 | - * This also SETS the cache if we return the actual property! |
|
| 644 | - * |
|
| 645 | - * @param string $fieldname the name of the property we're trying to retrieve |
|
| 646 | - * @param bool $pretty |
|
| 647 | - * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
|
| 648 | - * (in cases where the same property may be used for different outputs |
|
| 649 | - * - i.e. datetime, money etc.) |
|
| 650 | - * It can also accept certain pre-defined "schema" strings |
|
| 651 | - * to define how to output the property. |
|
| 652 | - * see the field's prepare_for_pretty_echoing for what strings can be used |
|
| 653 | - * @return mixed whatever the value for the property is we're retrieving |
|
| 654 | - * @throws ReflectionException |
|
| 655 | - * @throws InvalidArgumentException |
|
| 656 | - * @throws InvalidInterfaceException |
|
| 657 | - * @throws InvalidDataTypeException |
|
| 658 | - * @throws EE_Error |
|
| 659 | - */ |
|
| 660 | - protected function _get_cached_property($fieldname, $pretty = false, $extra_cache_ref = null) |
|
| 661 | - { |
|
| 662 | - // verify the field exists |
|
| 663 | - $model = $this->get_model(); |
|
| 664 | - $model->field_settings_for($fieldname); |
|
| 665 | - $cache_type = $pretty ? 'pretty' : 'standard'; |
|
| 666 | - $cache_type .= ! empty($extra_cache_ref) ? '_' . $extra_cache_ref : ''; |
|
| 667 | - if (isset($this->_cached_properties[ $fieldname ][ $cache_type ])) { |
|
| 668 | - return $this->_cached_properties[ $fieldname ][ $cache_type ]; |
|
| 669 | - } |
|
| 670 | - $value = $this->_get_fresh_property($fieldname, $pretty, $extra_cache_ref); |
|
| 671 | - $this->_set_cached_property($fieldname, $value, $cache_type); |
|
| 672 | - return $value; |
|
| 673 | - } |
|
| 674 | - |
|
| 675 | - |
|
| 676 | - /** |
|
| 677 | - * If the cache didn't fetch the needed item, this fetches it. |
|
| 678 | - * |
|
| 679 | - * @param string $fieldname |
|
| 680 | - * @param bool $pretty |
|
| 681 | - * @param string $extra_cache_ref |
|
| 682 | - * @return mixed |
|
| 683 | - * @throws InvalidArgumentException |
|
| 684 | - * @throws InvalidInterfaceException |
|
| 685 | - * @throws InvalidDataTypeException |
|
| 686 | - * @throws EE_Error |
|
| 687 | - * @throws ReflectionException |
|
| 688 | - */ |
|
| 689 | - protected function _get_fresh_property($fieldname, $pretty = false, $extra_cache_ref = null) |
|
| 690 | - { |
|
| 691 | - $field_obj = $this->get_model()->field_settings_for($fieldname); |
|
| 692 | - // If this is an EE_Datetime_Field we need to make sure timezone, formats, and output are correct |
|
| 693 | - if ($field_obj instanceof EE_Datetime_Field) { |
|
| 694 | - $this->_prepare_datetime_field($field_obj, $pretty, $extra_cache_ref); |
|
| 695 | - } |
|
| 696 | - if (! isset($this->_fields[ $fieldname ])) { |
|
| 697 | - $this->_fields[ $fieldname ] = null; |
|
| 698 | - } |
|
| 699 | - $value = $pretty |
|
| 700 | - ? $field_obj->prepare_for_pretty_echoing($this->_fields[ $fieldname ], $extra_cache_ref) |
|
| 701 | - : $field_obj->prepare_for_get($this->_fields[ $fieldname ]); |
|
| 702 | - return $value; |
|
| 703 | - } |
|
| 704 | - |
|
| 705 | - |
|
| 706 | - /** |
|
| 707 | - * set timezone, formats, and output for EE_Datetime_Field objects |
|
| 708 | - * |
|
| 709 | - * @param \EE_Datetime_Field $datetime_field |
|
| 710 | - * @param bool $pretty |
|
| 711 | - * @param null $date_or_time |
|
| 712 | - * @return void |
|
| 713 | - * @throws InvalidArgumentException |
|
| 714 | - * @throws InvalidInterfaceException |
|
| 715 | - * @throws InvalidDataTypeException |
|
| 716 | - * @throws EE_Error |
|
| 717 | - */ |
|
| 718 | - protected function _prepare_datetime_field( |
|
| 719 | - EE_Datetime_Field $datetime_field, |
|
| 720 | - $pretty = false, |
|
| 721 | - $date_or_time = null |
|
| 722 | - ) { |
|
| 723 | - $datetime_field->set_timezone($this->_timezone); |
|
| 724 | - $datetime_field->set_date_format($this->_dt_frmt, $pretty); |
|
| 725 | - $datetime_field->set_time_format($this->_tm_frmt, $pretty); |
|
| 726 | - // set the output returned |
|
| 727 | - switch ($date_or_time) { |
|
| 728 | - case 'D': |
|
| 729 | - $datetime_field->set_date_time_output('date'); |
|
| 730 | - break; |
|
| 731 | - case 'T': |
|
| 732 | - $datetime_field->set_date_time_output('time'); |
|
| 733 | - break; |
|
| 734 | - default: |
|
| 735 | - $datetime_field->set_date_time_output(); |
|
| 736 | - } |
|
| 737 | - } |
|
| 738 | - |
|
| 739 | - |
|
| 740 | - /** |
|
| 741 | - * This just takes care of clearing out the cached_properties |
|
| 742 | - * |
|
| 743 | - * @return void |
|
| 744 | - */ |
|
| 745 | - protected function _clear_cached_properties() |
|
| 746 | - { |
|
| 747 | - $this->_cached_properties = array(); |
|
| 748 | - } |
|
| 749 | - |
|
| 750 | - |
|
| 751 | - /** |
|
| 752 | - * This just clears out ONE property if it exists in the cache |
|
| 753 | - * |
|
| 754 | - * @param string $property_name the property to remove if it exists (from the _cached_properties array) |
|
| 755 | - * @return void |
|
| 756 | - */ |
|
| 757 | - protected function _clear_cached_property($property_name) |
|
| 758 | - { |
|
| 759 | - if (isset($this->_cached_properties[ $property_name ])) { |
|
| 760 | - unset($this->_cached_properties[ $property_name ]); |
|
| 761 | - } |
|
| 762 | - } |
|
| 763 | - |
|
| 764 | - |
|
| 765 | - /** |
|
| 766 | - * Ensures that this related thing is a model object. |
|
| 767 | - * |
|
| 768 | - * @param mixed $object_or_id EE_base_Class/int/string either a related model object, or its ID |
|
| 769 | - * @param string $model_name name of the related thing, eg 'Attendee', |
|
| 770 | - * @return EE_Base_Class |
|
| 771 | - * @throws ReflectionException |
|
| 772 | - * @throws InvalidArgumentException |
|
| 773 | - * @throws InvalidInterfaceException |
|
| 774 | - * @throws InvalidDataTypeException |
|
| 775 | - * @throws EE_Error |
|
| 776 | - */ |
|
| 777 | - protected function ensure_related_thing_is_model_obj($object_or_id, $model_name) |
|
| 778 | - { |
|
| 779 | - $other_model_instance = self::_get_model_instance_with_name( |
|
| 780 | - self::_get_model_classname($model_name), |
|
| 781 | - $this->_timezone |
|
| 782 | - ); |
|
| 783 | - return $other_model_instance->ensure_is_obj($object_or_id); |
|
| 784 | - } |
|
| 785 | - |
|
| 786 | - |
|
| 787 | - /** |
|
| 788 | - * Forgets the cached model of the given relation Name. So the next time we request it, |
|
| 789 | - * we will fetch it again from the database. (Handy if you know it's changed somehow). |
|
| 790 | - * If a specific object is supplied, and the relationship to it is either a HasMany or HABTM, |
|
| 791 | - * then only remove that one object from our cached array. Otherwise, clear the entire list |
|
| 792 | - * |
|
| 793 | - * @param string $relationName one of the keys in the _model_relations array on the model. |
|
| 794 | - * Eg 'Registration' |
|
| 795 | - * @param mixed $object_to_remove_or_index_into_array or an index into the array of cached things, or NULL |
|
| 796 | - * if you intend to use $clear_all = TRUE, or the relation only |
|
| 797 | - * has 1 object anyways (ie, it's a BelongsToRelation) |
|
| 798 | - * @param bool $clear_all This flags clearing the entire cache relation property if |
|
| 799 | - * this is HasMany or HABTM. |
|
| 800 | - * @throws ReflectionException |
|
| 801 | - * @throws InvalidArgumentException |
|
| 802 | - * @throws InvalidInterfaceException |
|
| 803 | - * @throws InvalidDataTypeException |
|
| 804 | - * @throws EE_Error |
|
| 805 | - * @return EE_Base_Class | boolean from which was cleared from the cache, or true if we requested to remove a |
|
| 806 | - * relation from all |
|
| 807 | - */ |
|
| 808 | - public function clear_cache($relationName, $object_to_remove_or_index_into_array = null, $clear_all = false) |
|
| 809 | - { |
|
| 810 | - $relationship_to_model = $this->get_model()->related_settings_for($relationName); |
|
| 811 | - $index_in_cache = ''; |
|
| 812 | - if (! $relationship_to_model) { |
|
| 813 | - throw new EE_Error( |
|
| 814 | - sprintf( |
|
| 815 | - esc_html__('There is no relationship to %s on a %s. Cannot clear that cache', 'event_espresso'), |
|
| 816 | - $relationName, |
|
| 817 | - get_class($this) |
|
| 818 | - ) |
|
| 819 | - ); |
|
| 820 | - } |
|
| 821 | - if ($clear_all) { |
|
| 822 | - $obj_removed = true; |
|
| 823 | - $this->_model_relations[ $relationName ] = null; |
|
| 824 | - } elseif ($relationship_to_model instanceof EE_Belongs_To_Relation) { |
|
| 825 | - $obj_removed = $this->_model_relations[ $relationName ]; |
|
| 826 | - $this->_model_relations[ $relationName ] = null; |
|
| 827 | - } else { |
|
| 828 | - if ($object_to_remove_or_index_into_array instanceof EE_Base_Class |
|
| 829 | - && $object_to_remove_or_index_into_array->ID() |
|
| 830 | - ) { |
|
| 831 | - $index_in_cache = $object_to_remove_or_index_into_array->ID(); |
|
| 832 | - if (is_array($this->_model_relations[ $relationName ]) |
|
| 833 | - && ! isset($this->_model_relations[ $relationName ][ $index_in_cache ]) |
|
| 834 | - ) { |
|
| 835 | - $index_found_at = null; |
|
| 836 | - // find this object in the array even though it has a different key |
|
| 837 | - foreach ($this->_model_relations[ $relationName ] as $index => $obj) { |
|
| 838 | - /** @noinspection TypeUnsafeComparisonInspection */ |
|
| 839 | - if ($obj instanceof EE_Base_Class |
|
| 840 | - && ( |
|
| 841 | - $obj == $object_to_remove_or_index_into_array |
|
| 842 | - || $obj->ID() === $object_to_remove_or_index_into_array->ID() |
|
| 843 | - ) |
|
| 844 | - ) { |
|
| 845 | - $index_found_at = $index; |
|
| 846 | - break; |
|
| 847 | - } |
|
| 848 | - } |
|
| 849 | - if ($index_found_at) { |
|
| 850 | - $index_in_cache = $index_found_at; |
|
| 851 | - } else { |
|
| 852 | - // it wasn't found. huh. well obviously it doesn't need to be removed from teh cache |
|
| 853 | - // if it wasn't in it to begin with. So we're done |
|
| 854 | - return $object_to_remove_or_index_into_array; |
|
| 855 | - } |
|
| 856 | - } |
|
| 857 | - } elseif ($object_to_remove_or_index_into_array instanceof EE_Base_Class) { |
|
| 858 | - // so they provided a model object, but it's not yet saved to the DB... so let's go hunting for it! |
|
| 859 | - foreach ($this->get_all_from_cache($relationName) as $index => $potentially_obj_we_want) { |
|
| 860 | - /** @noinspection TypeUnsafeComparisonInspection */ |
|
| 861 | - if ($potentially_obj_we_want == $object_to_remove_or_index_into_array) { |
|
| 862 | - $index_in_cache = $index; |
|
| 863 | - } |
|
| 864 | - } |
|
| 865 | - } else { |
|
| 866 | - $index_in_cache = $object_to_remove_or_index_into_array; |
|
| 867 | - } |
|
| 868 | - // supposedly we've found it. But it could just be that the client code |
|
| 869 | - // provided a bad index/object |
|
| 870 | - if (isset($this->_model_relations[ $relationName ][ $index_in_cache ])) { |
|
| 871 | - $obj_removed = $this->_model_relations[ $relationName ][ $index_in_cache ]; |
|
| 872 | - unset($this->_model_relations[ $relationName ][ $index_in_cache ]); |
|
| 873 | - } else { |
|
| 874 | - // that thing was never cached anyways. |
|
| 875 | - $obj_removed = null; |
|
| 876 | - } |
|
| 877 | - } |
|
| 878 | - return $obj_removed; |
|
| 879 | - } |
|
| 880 | - |
|
| 881 | - |
|
| 882 | - /** |
|
| 883 | - * update_cache_after_object_save |
|
| 884 | - * Allows a cached item to have it's cache ID (within the array of cached items) reset using the new ID it has |
|
| 885 | - * obtained after being saved to the db |
|
| 886 | - * |
|
| 887 | - * @param string $relationName - the type of object that is cached |
|
| 888 | - * @param EE_Base_Class $newly_saved_object - the newly saved object to be re-cached |
|
| 889 | - * @param string $current_cache_id - the ID that was used when originally caching the object |
|
| 890 | - * @return boolean TRUE on success, FALSE on fail |
|
| 891 | - * @throws ReflectionException |
|
| 892 | - * @throws InvalidArgumentException |
|
| 893 | - * @throws InvalidInterfaceException |
|
| 894 | - * @throws InvalidDataTypeException |
|
| 895 | - * @throws EE_Error |
|
| 896 | - */ |
|
| 897 | - public function update_cache_after_object_save( |
|
| 898 | - $relationName, |
|
| 899 | - EE_Base_Class $newly_saved_object, |
|
| 900 | - $current_cache_id = '' |
|
| 901 | - ) { |
|
| 902 | - // verify that incoming object is of the correct type |
|
| 903 | - $obj_class = 'EE_' . $relationName; |
|
| 904 | - if ($newly_saved_object instanceof $obj_class) { |
|
| 905 | - /* @type EE_Base_Class $newly_saved_object */ |
|
| 906 | - // now get the type of relation |
|
| 907 | - $relationship_to_model = $this->get_model()->related_settings_for($relationName); |
|
| 908 | - // if this is a 1:1 relationship |
|
| 909 | - if ($relationship_to_model instanceof EE_Belongs_To_Relation) { |
|
| 910 | - // then just replace the cached object with the newly saved object |
|
| 911 | - $this->_model_relations[ $relationName ] = $newly_saved_object; |
|
| 912 | - return true; |
|
| 913 | - // or if it's some kind of sordid feral polyamorous relationship... |
|
| 914 | - } |
|
| 915 | - if (is_array($this->_model_relations[ $relationName ]) |
|
| 916 | - && isset($this->_model_relations[ $relationName ][ $current_cache_id ]) |
|
| 917 | - ) { |
|
| 918 | - // then remove the current cached item |
|
| 919 | - unset($this->_model_relations[ $relationName ][ $current_cache_id ]); |
|
| 920 | - // and cache the newly saved object using it's new ID |
|
| 921 | - $this->_model_relations[ $relationName ][ $newly_saved_object->ID() ] = $newly_saved_object; |
|
| 922 | - return true; |
|
| 923 | - } |
|
| 924 | - } |
|
| 925 | - return false; |
|
| 926 | - } |
|
| 927 | - |
|
| 928 | - |
|
| 929 | - /** |
|
| 930 | - * Fetches a single EE_Base_Class on that relation. (If the relation is of type |
|
| 931 | - * BelongsTo, it will only ever have 1 object. However, other relations could have an array of objects) |
|
| 932 | - * |
|
| 933 | - * @param string $relationName |
|
| 934 | - * @return EE_Base_Class |
|
| 935 | - */ |
|
| 936 | - public function get_one_from_cache($relationName) |
|
| 937 | - { |
|
| 938 | - $cached_array_or_object = isset($this->_model_relations[ $relationName ]) |
|
| 939 | - ? $this->_model_relations[ $relationName ] |
|
| 940 | - : null; |
|
| 941 | - if (is_array($cached_array_or_object)) { |
|
| 942 | - return array_shift($cached_array_or_object); |
|
| 943 | - } |
|
| 944 | - return $cached_array_or_object; |
|
| 945 | - } |
|
| 946 | - |
|
| 947 | - |
|
| 948 | - /** |
|
| 949 | - * Fetches a single EE_Base_Class on that relation. (If the relation is of type |
|
| 950 | - * BelongsTo, it will only ever have 1 object. However, other relations could have an array of objects) |
|
| 951 | - * |
|
| 952 | - * @param string $relationName |
|
| 953 | - * @throws ReflectionException |
|
| 954 | - * @throws InvalidArgumentException |
|
| 955 | - * @throws InvalidInterfaceException |
|
| 956 | - * @throws InvalidDataTypeException |
|
| 957 | - * @throws EE_Error |
|
| 958 | - * @return EE_Base_Class[] NOT necessarily indexed by primary keys |
|
| 959 | - */ |
|
| 960 | - public function get_all_from_cache($relationName) |
|
| 961 | - { |
|
| 962 | - $objects = isset($this->_model_relations[ $relationName ]) ? $this->_model_relations[ $relationName ] : array(); |
|
| 963 | - // if the result is not an array, but exists, make it an array |
|
| 964 | - $objects = is_array($objects) ? $objects : array($objects); |
|
| 965 | - // bugfix for https://events.codebasehq.com/projects/event-espresso/tickets/7143 |
|
| 966 | - // basically, if this model object was stored in the session, and these cached model objects |
|
| 967 | - // already have IDs, let's make sure they're in their model's entity mapper |
|
| 968 | - // otherwise we will have duplicates next time we call |
|
| 969 | - // EE_Registry::instance()->load_model( $relationName )->get_one_by_ID( $result->ID() ); |
|
| 970 | - $model = EE_Registry::instance()->load_model($relationName); |
|
| 971 | - foreach ($objects as $model_object) { |
|
| 972 | - if ($model instanceof EEM_Base && $model_object instanceof EE_Base_Class) { |
|
| 973 | - // ensure its in the map if it has an ID; otherwise it will be added to the map when its saved |
|
| 974 | - if ($model_object->ID()) { |
|
| 975 | - $model->add_to_entity_map($model_object); |
|
| 976 | - } |
|
| 977 | - } else { |
|
| 978 | - throw new EE_Error( |
|
| 979 | - sprintf( |
|
| 980 | - esc_html__( |
|
| 981 | - 'Error retrieving related model objects. Either $1%s is not a model or $2%s is not a model object', |
|
| 982 | - 'event_espresso' |
|
| 983 | - ), |
|
| 984 | - $relationName, |
|
| 985 | - gettype($model_object) |
|
| 986 | - ) |
|
| 987 | - ); |
|
| 988 | - } |
|
| 989 | - } |
|
| 990 | - return $objects; |
|
| 991 | - } |
|
| 992 | - |
|
| 993 | - |
|
| 994 | - /** |
|
| 995 | - * Returns the next x number of EE_Base_Class objects in sequence from this object as found in the database |
|
| 996 | - * matching the given query conditions. |
|
| 997 | - * |
|
| 998 | - * @param null $field_to_order_by What field is being used as the reference point. |
|
| 999 | - * @param int $limit How many objects to return. |
|
| 1000 | - * @param array $query_params Any additional conditions on the query. |
|
| 1001 | - * @param null $columns_to_select If left null, then an array of EE_Base_Class objects is returned, otherwise |
|
| 1002 | - * you can indicate just the columns you want returned |
|
| 1003 | - * @return array|EE_Base_Class[] |
|
| 1004 | - * @throws ReflectionException |
|
| 1005 | - * @throws InvalidArgumentException |
|
| 1006 | - * @throws InvalidInterfaceException |
|
| 1007 | - * @throws InvalidDataTypeException |
|
| 1008 | - * @throws EE_Error |
|
| 1009 | - */ |
|
| 1010 | - public function next_x($field_to_order_by = null, $limit = 1, $query_params = array(), $columns_to_select = null) |
|
| 1011 | - { |
|
| 1012 | - $model = $this->get_model(); |
|
| 1013 | - $field = empty($field_to_order_by) && $model->has_primary_key_field() |
|
| 1014 | - ? $model->get_primary_key_field()->get_name() |
|
| 1015 | - : $field_to_order_by; |
|
| 1016 | - $current_value = ! empty($field) ? $this->get($field) : null; |
|
| 1017 | - if (empty($field) || empty($current_value)) { |
|
| 1018 | - return array(); |
|
| 1019 | - } |
|
| 1020 | - return $model->next_x($current_value, $field, $limit, $query_params, $columns_to_select); |
|
| 1021 | - } |
|
| 1022 | - |
|
| 1023 | - |
|
| 1024 | - /** |
|
| 1025 | - * Returns the previous x number of EE_Base_Class objects in sequence from this object as found in the database |
|
| 1026 | - * matching the given query conditions. |
|
| 1027 | - * |
|
| 1028 | - * @param null $field_to_order_by What field is being used as the reference point. |
|
| 1029 | - * @param int $limit How many objects to return. |
|
| 1030 | - * @param array $query_params Any additional conditions on the query. |
|
| 1031 | - * @param null $columns_to_select If left null, then an array of EE_Base_Class objects is returned, otherwise |
|
| 1032 | - * you can indicate just the columns you want returned |
|
| 1033 | - * @return array|EE_Base_Class[] |
|
| 1034 | - * @throws ReflectionException |
|
| 1035 | - * @throws InvalidArgumentException |
|
| 1036 | - * @throws InvalidInterfaceException |
|
| 1037 | - * @throws InvalidDataTypeException |
|
| 1038 | - * @throws EE_Error |
|
| 1039 | - */ |
|
| 1040 | - public function previous_x( |
|
| 1041 | - $field_to_order_by = null, |
|
| 1042 | - $limit = 1, |
|
| 1043 | - $query_params = array(), |
|
| 1044 | - $columns_to_select = null |
|
| 1045 | - ) { |
|
| 1046 | - $model = $this->get_model(); |
|
| 1047 | - $field = empty($field_to_order_by) && $model->has_primary_key_field() |
|
| 1048 | - ? $model->get_primary_key_field()->get_name() |
|
| 1049 | - : $field_to_order_by; |
|
| 1050 | - $current_value = ! empty($field) ? $this->get($field) : null; |
|
| 1051 | - if (empty($field) || empty($current_value)) { |
|
| 1052 | - return array(); |
|
| 1053 | - } |
|
| 1054 | - return $model->previous_x($current_value, $field, $limit, $query_params, $columns_to_select); |
|
| 1055 | - } |
|
| 1056 | - |
|
| 1057 | - |
|
| 1058 | - /** |
|
| 1059 | - * Returns the next EE_Base_Class object in sequence from this object as found in the database |
|
| 1060 | - * matching the given query conditions. |
|
| 1061 | - * |
|
| 1062 | - * @param null $field_to_order_by What field is being used as the reference point. |
|
| 1063 | - * @param array $query_params Any additional conditions on the query. |
|
| 1064 | - * @param null $columns_to_select If left null, then an array of EE_Base_Class objects is returned, otherwise |
|
| 1065 | - * you can indicate just the columns you want returned |
|
| 1066 | - * @return array|EE_Base_Class |
|
| 1067 | - * @throws ReflectionException |
|
| 1068 | - * @throws InvalidArgumentException |
|
| 1069 | - * @throws InvalidInterfaceException |
|
| 1070 | - * @throws InvalidDataTypeException |
|
| 1071 | - * @throws EE_Error |
|
| 1072 | - */ |
|
| 1073 | - public function next($field_to_order_by = null, $query_params = array(), $columns_to_select = null) |
|
| 1074 | - { |
|
| 1075 | - $model = $this->get_model(); |
|
| 1076 | - $field = empty($field_to_order_by) && $model->has_primary_key_field() |
|
| 1077 | - ? $model->get_primary_key_field()->get_name() |
|
| 1078 | - : $field_to_order_by; |
|
| 1079 | - $current_value = ! empty($field) ? $this->get($field) : null; |
|
| 1080 | - if (empty($field) || empty($current_value)) { |
|
| 1081 | - return array(); |
|
| 1082 | - } |
|
| 1083 | - return $model->next($current_value, $field, $query_params, $columns_to_select); |
|
| 1084 | - } |
|
| 1085 | - |
|
| 1086 | - |
|
| 1087 | - /** |
|
| 1088 | - * Returns the previous EE_Base_Class object in sequence from this object as found in the database |
|
| 1089 | - * matching the given query conditions. |
|
| 1090 | - * |
|
| 1091 | - * @param null $field_to_order_by What field is being used as the reference point. |
|
| 1092 | - * @param array $query_params Any additional conditions on the query. |
|
| 1093 | - * @param null $columns_to_select If left null, then an EE_Base_Class object is returned, otherwise |
|
| 1094 | - * you can indicate just the column you want returned |
|
| 1095 | - * @return array|EE_Base_Class |
|
| 1096 | - * @throws ReflectionException |
|
| 1097 | - * @throws InvalidArgumentException |
|
| 1098 | - * @throws InvalidInterfaceException |
|
| 1099 | - * @throws InvalidDataTypeException |
|
| 1100 | - * @throws EE_Error |
|
| 1101 | - */ |
|
| 1102 | - public function previous($field_to_order_by = null, $query_params = array(), $columns_to_select = null) |
|
| 1103 | - { |
|
| 1104 | - $model = $this->get_model(); |
|
| 1105 | - $field = empty($field_to_order_by) && $model->has_primary_key_field() |
|
| 1106 | - ? $model->get_primary_key_field()->get_name() |
|
| 1107 | - : $field_to_order_by; |
|
| 1108 | - $current_value = ! empty($field) ? $this->get($field) : null; |
|
| 1109 | - if (empty($field) || empty($current_value)) { |
|
| 1110 | - return array(); |
|
| 1111 | - } |
|
| 1112 | - return $model->previous($current_value, $field, $query_params, $columns_to_select); |
|
| 1113 | - } |
|
| 1114 | - |
|
| 1115 | - |
|
| 1116 | - /** |
|
| 1117 | - * Overrides parent because parent expects old models. |
|
| 1118 | - * This also doesn't do any validation, and won't work for serialized arrays |
|
| 1119 | - * |
|
| 1120 | - * @param string $field_name |
|
| 1121 | - * @param mixed $field_value_from_db |
|
| 1122 | - * @throws ReflectionException |
|
| 1123 | - * @throws InvalidArgumentException |
|
| 1124 | - * @throws InvalidInterfaceException |
|
| 1125 | - * @throws InvalidDataTypeException |
|
| 1126 | - * @throws EE_Error |
|
| 1127 | - */ |
|
| 1128 | - public function set_from_db($field_name, $field_value_from_db) |
|
| 1129 | - { |
|
| 1130 | - $field_obj = $this->get_model()->field_settings_for($field_name); |
|
| 1131 | - if ($field_obj instanceof EE_Model_Field_Base) { |
|
| 1132 | - // you would think the DB has no NULLs for non-null label fields right? wrong! |
|
| 1133 | - // eg, a CPT model object could have an entry in the posts table, but no |
|
| 1134 | - // entry in the meta table. Meaning that all its columns in the meta table |
|
| 1135 | - // are null! yikes! so when we find one like that, use defaults for its meta columns |
|
| 1136 | - if ($field_value_from_db === null) { |
|
| 1137 | - if ($field_obj->is_nullable()) { |
|
| 1138 | - // if the field allows nulls, then let it be null |
|
| 1139 | - $field_value = null; |
|
| 1140 | - } else { |
|
| 1141 | - $field_value = $field_obj->get_default_value(); |
|
| 1142 | - } |
|
| 1143 | - } else { |
|
| 1144 | - $field_value = $field_obj->prepare_for_set_from_db($field_value_from_db); |
|
| 1145 | - } |
|
| 1146 | - $this->_fields[ $field_name ] = $field_value; |
|
| 1147 | - $this->_clear_cached_property($field_name); |
|
| 1148 | - } |
|
| 1149 | - } |
|
| 1150 | - |
|
| 1151 | - |
|
| 1152 | - /** |
|
| 1153 | - * verifies that the specified field is of the correct type |
|
| 1154 | - * |
|
| 1155 | - * @param string $field_name |
|
| 1156 | - * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
|
| 1157 | - * (in cases where the same property may be used for different outputs |
|
| 1158 | - * - i.e. datetime, money etc.) |
|
| 1159 | - * @return mixed |
|
| 1160 | - * @throws ReflectionException |
|
| 1161 | - * @throws InvalidArgumentException |
|
| 1162 | - * @throws InvalidInterfaceException |
|
| 1163 | - * @throws InvalidDataTypeException |
|
| 1164 | - * @throws EE_Error |
|
| 1165 | - */ |
|
| 1166 | - public function get($field_name, $extra_cache_ref = null) |
|
| 1167 | - { |
|
| 1168 | - return $this->_get_cached_property($field_name, false, $extra_cache_ref); |
|
| 1169 | - } |
|
| 1170 | - |
|
| 1171 | - |
|
| 1172 | - /** |
|
| 1173 | - * This method simply returns the RAW unprocessed value for the given property in this class |
|
| 1174 | - * |
|
| 1175 | - * @param string $field_name A valid fieldname |
|
| 1176 | - * @return mixed Whatever the raw value stored on the property is. |
|
| 1177 | - * @throws ReflectionException |
|
| 1178 | - * @throws InvalidArgumentException |
|
| 1179 | - * @throws InvalidInterfaceException |
|
| 1180 | - * @throws InvalidDataTypeException |
|
| 1181 | - * @throws EE_Error if fieldSettings is misconfigured or the field doesn't exist. |
|
| 1182 | - */ |
|
| 1183 | - public function get_raw($field_name) |
|
| 1184 | - { |
|
| 1185 | - $field_settings = $this->get_model()->field_settings_for($field_name); |
|
| 1186 | - return $field_settings instanceof EE_Datetime_Field && $this->_fields[ $field_name ] instanceof DateTime |
|
| 1187 | - ? $this->_fields[ $field_name ]->format('U') |
|
| 1188 | - : $this->_fields[ $field_name ]; |
|
| 1189 | - } |
|
| 1190 | - |
|
| 1191 | - |
|
| 1192 | - /** |
|
| 1193 | - * This is used to return the internal DateTime object used for a field that is a |
|
| 1194 | - * EE_Datetime_Field. |
|
| 1195 | - * |
|
| 1196 | - * @param string $field_name The field name retrieving the DateTime object. |
|
| 1197 | - * @return mixed null | false | DateTime If the requested field is NOT a EE_Datetime_Field then |
|
| 1198 | - * @throws EE_Error an error is set and false returned. If the field IS an |
|
| 1199 | - * EE_Datetime_Field and but the field value is null, then |
|
| 1200 | - * just null is returned (because that indicates that likely |
|
| 1201 | - * this field is nullable). |
|
| 1202 | - * @throws InvalidArgumentException |
|
| 1203 | - * @throws InvalidDataTypeException |
|
| 1204 | - * @throws InvalidInterfaceException |
|
| 1205 | - * @throws ReflectionException |
|
| 1206 | - */ |
|
| 1207 | - public function get_DateTime_object($field_name) |
|
| 1208 | - { |
|
| 1209 | - $field_settings = $this->get_model()->field_settings_for($field_name); |
|
| 1210 | - if (! $field_settings instanceof EE_Datetime_Field) { |
|
| 1211 | - EE_Error::add_error( |
|
| 1212 | - sprintf( |
|
| 1213 | - esc_html__( |
|
| 1214 | - 'The field %s is not an EE_Datetime_Field field. There is no DateTime object stored on this field type.', |
|
| 1215 | - 'event_espresso' |
|
| 1216 | - ), |
|
| 1217 | - $field_name |
|
| 1218 | - ), |
|
| 1219 | - __FILE__, |
|
| 1220 | - __FUNCTION__, |
|
| 1221 | - __LINE__ |
|
| 1222 | - ); |
|
| 1223 | - return false; |
|
| 1224 | - } |
|
| 1225 | - return isset($this->_fields[ $field_name ]) && $this->_fields[ $field_name ] instanceof DateTime |
|
| 1226 | - ? clone $this->_fields[ $field_name ] |
|
| 1227 | - : null; |
|
| 1228 | - } |
|
| 1229 | - |
|
| 1230 | - |
|
| 1231 | - /** |
|
| 1232 | - * To be used in template to immediately echo out the value, and format it for output. |
|
| 1233 | - * Eg, should call stripslashes and whatnot before echoing |
|
| 1234 | - * |
|
| 1235 | - * @param string $field_name the name of the field as it appears in the DB |
|
| 1236 | - * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
|
| 1237 | - * (in cases where the same property may be used for different outputs |
|
| 1238 | - * - i.e. datetime, money etc.) |
|
| 1239 | - * @return void |
|
| 1240 | - * @throws ReflectionException |
|
| 1241 | - * @throws InvalidArgumentException |
|
| 1242 | - * @throws InvalidInterfaceException |
|
| 1243 | - * @throws InvalidDataTypeException |
|
| 1244 | - * @throws EE_Error |
|
| 1245 | - */ |
|
| 1246 | - public function e($field_name, $extra_cache_ref = null) |
|
| 1247 | - { |
|
| 1248 | - echo $this->get_pretty($field_name, $extra_cache_ref); |
|
| 1249 | - } |
|
| 1250 | - |
|
| 1251 | - |
|
| 1252 | - /** |
|
| 1253 | - * Exactly like e(), echoes out the field, but sets its schema to 'form_input', so that it |
|
| 1254 | - * can be easily used as the value of form input. |
|
| 1255 | - * |
|
| 1256 | - * @param string $field_name |
|
| 1257 | - * @return void |
|
| 1258 | - * @throws ReflectionException |
|
| 1259 | - * @throws InvalidArgumentException |
|
| 1260 | - * @throws InvalidInterfaceException |
|
| 1261 | - * @throws InvalidDataTypeException |
|
| 1262 | - * @throws EE_Error |
|
| 1263 | - */ |
|
| 1264 | - public function f($field_name) |
|
| 1265 | - { |
|
| 1266 | - $this->e($field_name, 'form_input'); |
|
| 1267 | - } |
|
| 1268 | - |
|
| 1269 | - |
|
| 1270 | - /** |
|
| 1271 | - * Same as `f()` but just returns the value instead of echoing it |
|
| 1272 | - * |
|
| 1273 | - * @param string $field_name |
|
| 1274 | - * @return string |
|
| 1275 | - * @throws ReflectionException |
|
| 1276 | - * @throws InvalidArgumentException |
|
| 1277 | - * @throws InvalidInterfaceException |
|
| 1278 | - * @throws InvalidDataTypeException |
|
| 1279 | - * @throws EE_Error |
|
| 1280 | - */ |
|
| 1281 | - public function get_f($field_name) |
|
| 1282 | - { |
|
| 1283 | - return (string) $this->get_pretty($field_name, 'form_input'); |
|
| 1284 | - } |
|
| 1285 | - |
|
| 1286 | - |
|
| 1287 | - /** |
|
| 1288 | - * Gets a pretty view of the field's value. $extra_cache_ref can specify different formats for this. |
|
| 1289 | - * The $extra_cache_ref will be passed to the model field's prepare_for_pretty_echoing, so consult the field's class |
|
| 1290 | - * to see what options are available. |
|
| 1291 | - * |
|
| 1292 | - * @param string $field_name |
|
| 1293 | - * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
|
| 1294 | - * (in cases where the same property may be used for different outputs |
|
| 1295 | - * - i.e. datetime, money etc.) |
|
| 1296 | - * @return mixed |
|
| 1297 | - * @throws ReflectionException |
|
| 1298 | - * @throws InvalidArgumentException |
|
| 1299 | - * @throws InvalidInterfaceException |
|
| 1300 | - * @throws InvalidDataTypeException |
|
| 1301 | - * @throws EE_Error |
|
| 1302 | - */ |
|
| 1303 | - public function get_pretty($field_name, $extra_cache_ref = null) |
|
| 1304 | - { |
|
| 1305 | - return $this->_get_cached_property($field_name, true, $extra_cache_ref); |
|
| 1306 | - } |
|
| 1307 | - |
|
| 1308 | - |
|
| 1309 | - /** |
|
| 1310 | - * This simply returns the datetime for the given field name |
|
| 1311 | - * Note: this protected function is called by the wrapper get_date or get_time or get_datetime functions |
|
| 1312 | - * (and the equivalent e_date, e_time, e_datetime). |
|
| 1313 | - * |
|
| 1314 | - * @access protected |
|
| 1315 | - * @param string $field_name Field on the instantiated EE_Base_Class child object |
|
| 1316 | - * @param string $dt_frmt valid datetime format used for date |
|
| 1317 | - * (if '' then we just use the default on the field, |
|
| 1318 | - * if NULL we use the last-used format) |
|
| 1319 | - * @param string $tm_frmt Same as above except this is for time format |
|
| 1320 | - * @param string $date_or_time if NULL then both are returned, otherwise "D" = only date and "T" = only time. |
|
| 1321 | - * @param boolean $echo Whether the dtt is echoing using pretty echoing or just returned using vanilla get |
|
| 1322 | - * @return string|bool|EE_Error string on success, FALSE on fail, or EE_Error Exception is thrown |
|
| 1323 | - * if field is not a valid dtt field, or void if echoing |
|
| 1324 | - * @throws ReflectionException |
|
| 1325 | - * @throws InvalidArgumentException |
|
| 1326 | - * @throws InvalidInterfaceException |
|
| 1327 | - * @throws InvalidDataTypeException |
|
| 1328 | - * @throws EE_Error |
|
| 1329 | - */ |
|
| 1330 | - protected function _get_datetime($field_name, $dt_frmt = '', $tm_frmt = '', $date_or_time = '', $echo = false) |
|
| 1331 | - { |
|
| 1332 | - // clear cached property |
|
| 1333 | - $this->_clear_cached_property($field_name); |
|
| 1334 | - // reset format properties because they are used in get() |
|
| 1335 | - $this->_dt_frmt = $dt_frmt !== '' ? $dt_frmt : $this->_dt_frmt; |
|
| 1336 | - $this->_tm_frmt = $tm_frmt !== '' ? $tm_frmt : $this->_tm_frmt; |
|
| 1337 | - if ($echo) { |
|
| 1338 | - $this->e($field_name, $date_or_time); |
|
| 1339 | - return ''; |
|
| 1340 | - } |
|
| 1341 | - return $this->get($field_name, $date_or_time); |
|
| 1342 | - } |
|
| 1343 | - |
|
| 1344 | - |
|
| 1345 | - /** |
|
| 1346 | - * below are wrapper functions for the various datetime outputs that can be obtained for JUST returning the date |
|
| 1347 | - * portion of a datetime value. (note the only difference between get_ and e_ is one returns the value and the |
|
| 1348 | - * other echoes the pretty value for dtt) |
|
| 1349 | - * |
|
| 1350 | - * @param string $field_name name of model object datetime field holding the value |
|
| 1351 | - * @param string $format format for the date returned (if NULL we use default in dt_frmt property) |
|
| 1352 | - * @return string datetime value formatted |
|
| 1353 | - * @throws ReflectionException |
|
| 1354 | - * @throws InvalidArgumentException |
|
| 1355 | - * @throws InvalidInterfaceException |
|
| 1356 | - * @throws InvalidDataTypeException |
|
| 1357 | - * @throws EE_Error |
|
| 1358 | - */ |
|
| 1359 | - public function get_date($field_name, $format = '') |
|
| 1360 | - { |
|
| 1361 | - return $this->_get_datetime($field_name, $format, null, 'D'); |
|
| 1362 | - } |
|
| 1363 | - |
|
| 1364 | - |
|
| 1365 | - /** |
|
| 1366 | - * @param $field_name |
|
| 1367 | - * @param string $format |
|
| 1368 | - * @throws ReflectionException |
|
| 1369 | - * @throws InvalidArgumentException |
|
| 1370 | - * @throws InvalidInterfaceException |
|
| 1371 | - * @throws InvalidDataTypeException |
|
| 1372 | - * @throws EE_Error |
|
| 1373 | - */ |
|
| 1374 | - public function e_date($field_name, $format = '') |
|
| 1375 | - { |
|
| 1376 | - $this->_get_datetime($field_name, $format, null, 'D', true); |
|
| 1377 | - } |
|
| 1378 | - |
|
| 1379 | - |
|
| 1380 | - /** |
|
| 1381 | - * below are wrapper functions for the various datetime outputs that can be obtained for JUST returning the time |
|
| 1382 | - * portion of a datetime value. (note the only difference between get_ and e_ is one returns the value and the |
|
| 1383 | - * other echoes the pretty value for dtt) |
|
| 1384 | - * |
|
| 1385 | - * @param string $field_name name of model object datetime field holding the value |
|
| 1386 | - * @param string $format format for the time returned ( if NULL we use default in tm_frmt property) |
|
| 1387 | - * @return string datetime value formatted |
|
| 1388 | - * @throws ReflectionException |
|
| 1389 | - * @throws InvalidArgumentException |
|
| 1390 | - * @throws InvalidInterfaceException |
|
| 1391 | - * @throws InvalidDataTypeException |
|
| 1392 | - * @throws EE_Error |
|
| 1393 | - */ |
|
| 1394 | - public function get_time($field_name, $format = '') |
|
| 1395 | - { |
|
| 1396 | - return $this->_get_datetime($field_name, null, $format, 'T'); |
|
| 1397 | - } |
|
| 1398 | - |
|
| 1399 | - |
|
| 1400 | - /** |
|
| 1401 | - * @param $field_name |
|
| 1402 | - * @param string $format |
|
| 1403 | - * @throws ReflectionException |
|
| 1404 | - * @throws InvalidArgumentException |
|
| 1405 | - * @throws InvalidInterfaceException |
|
| 1406 | - * @throws InvalidDataTypeException |
|
| 1407 | - * @throws EE_Error |
|
| 1408 | - */ |
|
| 1409 | - public function e_time($field_name, $format = '') |
|
| 1410 | - { |
|
| 1411 | - $this->_get_datetime($field_name, null, $format, 'T', true); |
|
| 1412 | - } |
|
| 1413 | - |
|
| 1414 | - |
|
| 1415 | - /** |
|
| 1416 | - * below are wrapper functions for the various datetime outputs that can be obtained for returning the date AND |
|
| 1417 | - * time portion of a datetime value. (note the only difference between get_ and e_ is one returns the value and the |
|
| 1418 | - * other echoes the pretty value for dtt) |
|
| 1419 | - * |
|
| 1420 | - * @param string $field_name name of model object datetime field holding the value |
|
| 1421 | - * @param string $dt_frmt format for the date returned (if NULL we use default in dt_frmt property) |
|
| 1422 | - * @param string $tm_frmt format for the time returned (if NULL we use default in tm_frmt property) |
|
| 1423 | - * @return string datetime value formatted |
|
| 1424 | - * @throws ReflectionException |
|
| 1425 | - * @throws InvalidArgumentException |
|
| 1426 | - * @throws InvalidInterfaceException |
|
| 1427 | - * @throws InvalidDataTypeException |
|
| 1428 | - * @throws EE_Error |
|
| 1429 | - */ |
|
| 1430 | - public function get_datetime($field_name, $dt_frmt = '', $tm_frmt = '') |
|
| 1431 | - { |
|
| 1432 | - return $this->_get_datetime($field_name, $dt_frmt, $tm_frmt); |
|
| 1433 | - } |
|
| 1434 | - |
|
| 1435 | - |
|
| 1436 | - /** |
|
| 1437 | - * @param string $field_name |
|
| 1438 | - * @param string $dt_frmt |
|
| 1439 | - * @param string $tm_frmt |
|
| 1440 | - * @throws ReflectionException |
|
| 1441 | - * @throws InvalidArgumentException |
|
| 1442 | - * @throws InvalidInterfaceException |
|
| 1443 | - * @throws InvalidDataTypeException |
|
| 1444 | - * @throws EE_Error |
|
| 1445 | - */ |
|
| 1446 | - public function e_datetime($field_name, $dt_frmt = '', $tm_frmt = '') |
|
| 1447 | - { |
|
| 1448 | - $this->_get_datetime($field_name, $dt_frmt, $tm_frmt, null, true); |
|
| 1449 | - } |
|
| 1450 | - |
|
| 1451 | - |
|
| 1452 | - /** |
|
| 1453 | - * Get the i8ln value for a date using the WordPress @see date_i18n function. |
|
| 1454 | - * |
|
| 1455 | - * @param string $field_name The EE_Datetime_Field reference for the date being retrieved. |
|
| 1456 | - * @param string $format PHP valid date/time string format. If none is provided then the internal set format |
|
| 1457 | - * on the object will be used. |
|
| 1458 | - * @return string Date and time string in set locale or false if no field exists for the given |
|
| 1459 | - * @throws ReflectionException |
|
| 1460 | - * @throws InvalidArgumentException |
|
| 1461 | - * @throws InvalidInterfaceException |
|
| 1462 | - * @throws InvalidDataTypeException |
|
| 1463 | - * @throws EE_Error |
|
| 1464 | - * field name. |
|
| 1465 | - */ |
|
| 1466 | - public function get_i18n_datetime($field_name, $format = '') |
|
| 1467 | - { |
|
| 1468 | - $format = empty($format) ? $this->_dt_frmt . ' ' . $this->_tm_frmt : $format; |
|
| 1469 | - return date_i18n( |
|
| 1470 | - $format, |
|
| 1471 | - EEH_DTT_Helper::get_timestamp_with_offset( |
|
| 1472 | - $this->get_raw($field_name), |
|
| 1473 | - $this->_timezone |
|
| 1474 | - ) |
|
| 1475 | - ); |
|
| 1476 | - } |
|
| 1477 | - |
|
| 1478 | - |
|
| 1479 | - /** |
|
| 1480 | - * This method validates whether the given field name is a valid field on the model object as well as it is of a |
|
| 1481 | - * type EE_Datetime_Field. On success there will be returned the field settings. On fail an EE_Error exception is |
|
| 1482 | - * thrown. |
|
| 1483 | - * |
|
| 1484 | - * @param string $field_name The field name being checked |
|
| 1485 | - * @throws ReflectionException |
|
| 1486 | - * @throws InvalidArgumentException |
|
| 1487 | - * @throws InvalidInterfaceException |
|
| 1488 | - * @throws InvalidDataTypeException |
|
| 1489 | - * @throws EE_Error |
|
| 1490 | - * @return EE_Datetime_Field |
|
| 1491 | - */ |
|
| 1492 | - protected function _get_dtt_field_settings($field_name) |
|
| 1493 | - { |
|
| 1494 | - $field = $this->get_model()->field_settings_for($field_name); |
|
| 1495 | - // check if field is dtt |
|
| 1496 | - if ($field instanceof EE_Datetime_Field) { |
|
| 1497 | - return $field; |
|
| 1498 | - } |
|
| 1499 | - throw new EE_Error( |
|
| 1500 | - sprintf( |
|
| 1501 | - esc_html__( |
|
| 1502 | - 'The field name "%s" has been requested for the EE_Base_Class datetime functions and it is not a valid EE_Datetime_Field. Please check the spelling of the field and make sure it has been setup as a EE_Datetime_Field in the %s model constructor', |
|
| 1503 | - 'event_espresso' |
|
| 1504 | - ), |
|
| 1505 | - $field_name, |
|
| 1506 | - self::_get_model_classname(get_class($this)) |
|
| 1507 | - ) |
|
| 1508 | - ); |
|
| 1509 | - } |
|
| 1510 | - |
|
| 1511 | - |
|
| 1512 | - |
|
| 1513 | - |
|
| 1514 | - /** |
|
| 1515 | - * NOTE ABOUT BELOW: |
|
| 1516 | - * These convenience date and time setters are for setting date and time independently. In other words you might |
|
| 1517 | - * want to change the time on a datetime_field but leave the date the same (or vice versa). IF on the other hand |
|
| 1518 | - * you want to set both date and time at the same time, you can just use the models default set($fieldname,$value) |
|
| 1519 | - * method and make sure you send the entire datetime value for setting. |
|
| 1520 | - */ |
|
| 1521 | - /** |
|
| 1522 | - * sets the time on a datetime property |
|
| 1523 | - * |
|
| 1524 | - * @access protected |
|
| 1525 | - * @param string|Datetime $time a valid time string for php datetime functions (or DateTime object) |
|
| 1526 | - * @param string $fieldname the name of the field the time is being set on (must match a EE_Datetime_Field) |
|
| 1527 | - * @throws ReflectionException |
|
| 1528 | - * @throws InvalidArgumentException |
|
| 1529 | - * @throws InvalidInterfaceException |
|
| 1530 | - * @throws InvalidDataTypeException |
|
| 1531 | - * @throws EE_Error |
|
| 1532 | - */ |
|
| 1533 | - protected function _set_time_for($time, $fieldname) |
|
| 1534 | - { |
|
| 1535 | - $this->_set_date_time('T', $time, $fieldname); |
|
| 1536 | - } |
|
| 1537 | - |
|
| 1538 | - |
|
| 1539 | - /** |
|
| 1540 | - * sets the date on a datetime property |
|
| 1541 | - * |
|
| 1542 | - * @access protected |
|
| 1543 | - * @param string|DateTime $date a valid date string for php datetime functions ( or DateTime object) |
|
| 1544 | - * @param string $fieldname the name of the field the date is being set on (must match a EE_Datetime_Field) |
|
| 1545 | - * @throws ReflectionException |
|
| 1546 | - * @throws InvalidArgumentException |
|
| 1547 | - * @throws InvalidInterfaceException |
|
| 1548 | - * @throws InvalidDataTypeException |
|
| 1549 | - * @throws EE_Error |
|
| 1550 | - */ |
|
| 1551 | - protected function _set_date_for($date, $fieldname) |
|
| 1552 | - { |
|
| 1553 | - $this->_set_date_time('D', $date, $fieldname); |
|
| 1554 | - } |
|
| 1555 | - |
|
| 1556 | - |
|
| 1557 | - /** |
|
| 1558 | - * This takes care of setting a date or time independently on a given model object property. This method also |
|
| 1559 | - * verifies that the given fieldname matches a model object property and is for a EE_Datetime_Field field |
|
| 1560 | - * |
|
| 1561 | - * @access protected |
|
| 1562 | - * @param string $what "T" for time, 'B' for both, 'D' for Date. |
|
| 1563 | - * @param string|DateTime $datetime_value A valid Date or Time string (or DateTime object) |
|
| 1564 | - * @param string $fieldname the name of the field the date OR time is being set on (must match a |
|
| 1565 | - * EE_Datetime_Field property) |
|
| 1566 | - * @throws ReflectionException |
|
| 1567 | - * @throws InvalidArgumentException |
|
| 1568 | - * @throws InvalidInterfaceException |
|
| 1569 | - * @throws InvalidDataTypeException |
|
| 1570 | - * @throws EE_Error |
|
| 1571 | - */ |
|
| 1572 | - protected function _set_date_time($what = 'T', $datetime_value, $fieldname) |
|
| 1573 | - { |
|
| 1574 | - $field = $this->_get_dtt_field_settings($fieldname); |
|
| 1575 | - $field->set_timezone($this->_timezone); |
|
| 1576 | - $field->set_date_format($this->_dt_frmt); |
|
| 1577 | - $field->set_time_format($this->_tm_frmt); |
|
| 1578 | - switch ($what) { |
|
| 1579 | - case 'T': |
|
| 1580 | - $this->_fields[ $fieldname ] = $field->prepare_for_set_with_new_time( |
|
| 1581 | - $datetime_value, |
|
| 1582 | - $this->_fields[ $fieldname ] |
|
| 1583 | - ); |
|
| 1584 | - break; |
|
| 1585 | - case 'D': |
|
| 1586 | - $this->_fields[ $fieldname ] = $field->prepare_for_set_with_new_date( |
|
| 1587 | - $datetime_value, |
|
| 1588 | - $this->_fields[ $fieldname ] |
|
| 1589 | - ); |
|
| 1590 | - break; |
|
| 1591 | - case 'B': |
|
| 1592 | - $this->_fields[ $fieldname ] = $field->prepare_for_set($datetime_value); |
|
| 1593 | - break; |
|
| 1594 | - } |
|
| 1595 | - $this->_clear_cached_property($fieldname); |
|
| 1596 | - } |
|
| 1597 | - |
|
| 1598 | - |
|
| 1599 | - /** |
|
| 1600 | - * This will return a timestamp for the website timezone but ONLY when the current website timezone is different |
|
| 1601 | - * than the timezone set for the website. NOTE, this currently only works well with methods that return values. If |
|
| 1602 | - * you use it with methods that echo values the $_timestamp property may not get reset to its original value and |
|
| 1603 | - * that could lead to some unexpected results! |
|
| 1604 | - * |
|
| 1605 | - * @access public |
|
| 1606 | - * @param string $field_name This is the name of the field on the object that contains the date/time |
|
| 1607 | - * value being returned. |
|
| 1608 | - * @param string $callback must match a valid method in this class (defaults to get_datetime) |
|
| 1609 | - * @param mixed (array|string) $args This is the arguments that will be passed to the callback. |
|
| 1610 | - * @param string $prepend You can include something to prepend on the timestamp |
|
| 1611 | - * @param string $append You can include something to append on the timestamp |
|
| 1612 | - * @throws ReflectionException |
|
| 1613 | - * @throws InvalidArgumentException |
|
| 1614 | - * @throws InvalidInterfaceException |
|
| 1615 | - * @throws InvalidDataTypeException |
|
| 1616 | - * @throws EE_Error |
|
| 1617 | - * @return string timestamp |
|
| 1618 | - */ |
|
| 1619 | - public function display_in_my_timezone( |
|
| 1620 | - $field_name, |
|
| 1621 | - $callback = 'get_datetime', |
|
| 1622 | - $args = null, |
|
| 1623 | - $prepend = '', |
|
| 1624 | - $append = '' |
|
| 1625 | - ) { |
|
| 1626 | - $timezone = EEH_DTT_Helper::get_timezone(); |
|
| 1627 | - if ($timezone === $this->_timezone) { |
|
| 1628 | - return ''; |
|
| 1629 | - } |
|
| 1630 | - $original_timezone = $this->_timezone; |
|
| 1631 | - $this->set_timezone($timezone); |
|
| 1632 | - $fn = (array) $field_name; |
|
| 1633 | - $args = array_merge($fn, (array) $args); |
|
| 1634 | - if (! method_exists($this, $callback)) { |
|
| 1635 | - throw new EE_Error( |
|
| 1636 | - sprintf( |
|
| 1637 | - esc_html__( |
|
| 1638 | - 'The method named "%s" given as the callback param in "display_in_my_timezone" does not exist. Please check your spelling', |
|
| 1639 | - 'event_espresso' |
|
| 1640 | - ), |
|
| 1641 | - $callback |
|
| 1642 | - ) |
|
| 1643 | - ); |
|
| 1644 | - } |
|
| 1645 | - $args = (array) $args; |
|
| 1646 | - $return = $prepend . call_user_func_array(array($this, $callback), $args) . $append; |
|
| 1647 | - $this->set_timezone($original_timezone); |
|
| 1648 | - return $return; |
|
| 1649 | - } |
|
| 1650 | - |
|
| 1651 | - |
|
| 1652 | - /** |
|
| 1653 | - * Deletes this model object. |
|
| 1654 | - * This calls the `EE_Base_Class::_delete` method. Child classes wishing to change default behaviour should |
|
| 1655 | - * override |
|
| 1656 | - * `EE_Base_Class::_delete` NOT this class. |
|
| 1657 | - * |
|
| 1658 | - * @return boolean | int |
|
| 1659 | - * @throws ReflectionException |
|
| 1660 | - * @throws InvalidArgumentException |
|
| 1661 | - * @throws InvalidInterfaceException |
|
| 1662 | - * @throws InvalidDataTypeException |
|
| 1663 | - * @throws EE_Error |
|
| 1664 | - */ |
|
| 1665 | - public function delete() |
|
| 1666 | - { |
|
| 1667 | - /** |
|
| 1668 | - * Called just before the `EE_Base_Class::_delete` method call. |
|
| 1669 | - * Note: |
|
| 1670 | - * `EE_Base_Class::_delete` might be overridden by child classes so any client code hooking into these actions |
|
| 1671 | - * should be aware that `_delete` may not always result in a permanent delete. |
|
| 1672 | - * For example, `EE_Soft_Delete_Base_Class::_delete` |
|
| 1673 | - * soft deletes (trash) the object and does not permanently delete it. |
|
| 1674 | - * |
|
| 1675 | - * @param EE_Base_Class $model_object about to be 'deleted' |
|
| 1676 | - */ |
|
| 1677 | - do_action('AHEE__EE_Base_Class__delete__before', $this); |
|
| 1678 | - $result = $this->_delete(); |
|
| 1679 | - /** |
|
| 1680 | - * Called just after the `EE_Base_Class::_delete` method call. |
|
| 1681 | - * Note: |
|
| 1682 | - * `EE_Base_Class::_delete` might be overridden by child classes so any client code hooking into these actions |
|
| 1683 | - * should be aware that `_delete` may not always result in a permanent delete. |
|
| 1684 | - * For example `EE_Soft_Base_Class::_delete` |
|
| 1685 | - * soft deletes (trash) the object and does not permanently delete it. |
|
| 1686 | - * |
|
| 1687 | - * @param EE_Base_Class $model_object that was just 'deleted' |
|
| 1688 | - * @param boolean $result |
|
| 1689 | - */ |
|
| 1690 | - do_action('AHEE__EE_Base_Class__delete__end', $this, $result); |
|
| 1691 | - return $result; |
|
| 1692 | - } |
|
| 1693 | - |
|
| 1694 | - |
|
| 1695 | - /** |
|
| 1696 | - * Calls the specific delete method for the instantiated class. |
|
| 1697 | - * This method is called by the public `EE_Base_Class::delete` method. Any child classes desiring to override |
|
| 1698 | - * default functionality for "delete" (which is to call `permanently_delete`) should override this method NOT |
|
| 1699 | - * `EE_Base_Class::delete` |
|
| 1700 | - * |
|
| 1701 | - * @return bool|int |
|
| 1702 | - * @throws ReflectionException |
|
| 1703 | - * @throws InvalidArgumentException |
|
| 1704 | - * @throws InvalidInterfaceException |
|
| 1705 | - * @throws InvalidDataTypeException |
|
| 1706 | - * @throws EE_Error |
|
| 1707 | - */ |
|
| 1708 | - protected function _delete() |
|
| 1709 | - { |
|
| 1710 | - return $this->delete_permanently(); |
|
| 1711 | - } |
|
| 1712 | - |
|
| 1713 | - |
|
| 1714 | - /** |
|
| 1715 | - * Deletes this model object permanently from db |
|
| 1716 | - * (but keep in mind related models may block the delete and return an error) |
|
| 1717 | - * |
|
| 1718 | - * @return bool | int |
|
| 1719 | - * @throws ReflectionException |
|
| 1720 | - * @throws InvalidArgumentException |
|
| 1721 | - * @throws InvalidInterfaceException |
|
| 1722 | - * @throws InvalidDataTypeException |
|
| 1723 | - * @throws EE_Error |
|
| 1724 | - */ |
|
| 1725 | - public function delete_permanently() |
|
| 1726 | - { |
|
| 1727 | - /** |
|
| 1728 | - * Called just before HARD deleting a model object |
|
| 1729 | - * |
|
| 1730 | - * @param EE_Base_Class $model_object about to be 'deleted' |
|
| 1731 | - */ |
|
| 1732 | - do_action('AHEE__EE_Base_Class__delete_permanently__before', $this); |
|
| 1733 | - $model = $this->get_model(); |
|
| 1734 | - $result = $model->delete_permanently_by_ID($this->ID()); |
|
| 1735 | - $this->refresh_cache_of_related_objects(); |
|
| 1736 | - /** |
|
| 1737 | - * Called just after HARD deleting a model object |
|
| 1738 | - * |
|
| 1739 | - * @param EE_Base_Class $model_object that was just 'deleted' |
|
| 1740 | - * @param boolean $result |
|
| 1741 | - */ |
|
| 1742 | - do_action('AHEE__EE_Base_Class__delete_permanently__end', $this, $result); |
|
| 1743 | - return $result; |
|
| 1744 | - } |
|
| 1745 | - |
|
| 1746 | - |
|
| 1747 | - /** |
|
| 1748 | - * When this model object is deleted, it may still be cached on related model objects. This clears the cache of |
|
| 1749 | - * related model objects |
|
| 1750 | - * |
|
| 1751 | - * @throws ReflectionException |
|
| 1752 | - * @throws InvalidArgumentException |
|
| 1753 | - * @throws InvalidInterfaceException |
|
| 1754 | - * @throws InvalidDataTypeException |
|
| 1755 | - * @throws EE_Error |
|
| 1756 | - */ |
|
| 1757 | - public function refresh_cache_of_related_objects() |
|
| 1758 | - { |
|
| 1759 | - $model = $this->get_model(); |
|
| 1760 | - foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
|
| 1761 | - if (! empty($this->_model_relations[ $relation_name ])) { |
|
| 1762 | - $related_objects = $this->_model_relations[ $relation_name ]; |
|
| 1763 | - if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
| 1764 | - // this relation only stores a single model object, not an array |
|
| 1765 | - // but let's make it consistent |
|
| 1766 | - $related_objects = array($related_objects); |
|
| 1767 | - } |
|
| 1768 | - foreach ($related_objects as $related_object) { |
|
| 1769 | - // only refresh their cache if they're in memory |
|
| 1770 | - if ($related_object instanceof EE_Base_Class) { |
|
| 1771 | - $related_object->clear_cache( |
|
| 1772 | - $model->get_this_model_name(), |
|
| 1773 | - $this |
|
| 1774 | - ); |
|
| 1775 | - } |
|
| 1776 | - } |
|
| 1777 | - } |
|
| 1778 | - } |
|
| 1779 | - } |
|
| 1780 | - |
|
| 1781 | - |
|
| 1782 | - /** |
|
| 1783 | - * Saves this object to the database. An array may be supplied to set some values on this |
|
| 1784 | - * object just before saving. |
|
| 1785 | - * |
|
| 1786 | - * @access public |
|
| 1787 | - * @param array $set_cols_n_values keys are field names, values are their new values, |
|
| 1788 | - * if provided during the save() method (often client code will change the fields' |
|
| 1789 | - * values before calling save) |
|
| 1790 | - * @throws InvalidArgumentException |
|
| 1791 | - * @throws InvalidInterfaceException |
|
| 1792 | - * @throws InvalidDataTypeException |
|
| 1793 | - * @throws EE_Error |
|
| 1794 | - * @return int , 1 on a successful update, the ID of the new entry on insert; 0 on failure or if the model object |
|
| 1795 | - * isn't allowed to persist (as determined by EE_Base_Class::allow_persist()) |
|
| 1796 | - * @throws ReflectionException |
|
| 1797 | - * @throws ReflectionException |
|
| 1798 | - * @throws ReflectionException |
|
| 1799 | - */ |
|
| 1800 | - public function save($set_cols_n_values = array()) |
|
| 1801 | - { |
|
| 1802 | - $model = $this->get_model(); |
|
| 1803 | - /** |
|
| 1804 | - * Filters the fields we're about to save on the model object |
|
| 1805 | - * |
|
| 1806 | - * @param array $set_cols_n_values |
|
| 1807 | - * @param EE_Base_Class $model_object |
|
| 1808 | - */ |
|
| 1809 | - $set_cols_n_values = (array) apply_filters( |
|
| 1810 | - 'FHEE__EE_Base_Class__save__set_cols_n_values', |
|
| 1811 | - $set_cols_n_values, |
|
| 1812 | - $this |
|
| 1813 | - ); |
|
| 1814 | - // set attributes as provided in $set_cols_n_values |
|
| 1815 | - foreach ($set_cols_n_values as $column => $value) { |
|
| 1816 | - $this->set($column, $value); |
|
| 1817 | - } |
|
| 1818 | - // no changes ? then don't do anything |
|
| 1819 | - if (! $this->_has_changes && $this->ID() && $model->get_primary_key_field()->is_auto_increment()) { |
|
| 1820 | - return 0; |
|
| 1821 | - } |
|
| 1822 | - /** |
|
| 1823 | - * Saving a model object. |
|
| 1824 | - * Before we perform a save, this action is fired. |
|
| 1825 | - * |
|
| 1826 | - * @param EE_Base_Class $model_object the model object about to be saved. |
|
| 1827 | - */ |
|
| 1828 | - do_action('AHEE__EE_Base_Class__save__begin', $this); |
|
| 1829 | - if (! $this->allow_persist()) { |
|
| 1830 | - return 0; |
|
| 1831 | - } |
|
| 1832 | - // now get current attribute values |
|
| 1833 | - $save_cols_n_values = $this->_fields; |
|
| 1834 | - // if the object already has an ID, update it. Otherwise, insert it |
|
| 1835 | - // also: change the assumption about values passed to the model NOT being prepare dby the model object. |
|
| 1836 | - // They have been |
|
| 1837 | - $old_assumption_concerning_value_preparation = $model |
|
| 1838 | - ->get_assumption_concerning_values_already_prepared_by_model_object(); |
|
| 1839 | - $model->assume_values_already_prepared_by_model_object(true); |
|
| 1840 | - // does this model have an autoincrement PK? |
|
| 1841 | - if ($model->has_primary_key_field()) { |
|
| 1842 | - if ($model->get_primary_key_field()->is_auto_increment()) { |
|
| 1843 | - // ok check if it's set, if so: update; if not, insert |
|
| 1844 | - if (! empty($save_cols_n_values[ $model->primary_key_name() ])) { |
|
| 1845 | - $results = $model->update_by_ID($save_cols_n_values, $this->ID()); |
|
| 1846 | - } else { |
|
| 1847 | - unset($save_cols_n_values[ $model->primary_key_name() ]); |
|
| 1848 | - $results = $model->insert($save_cols_n_values); |
|
| 1849 | - if ($results) { |
|
| 1850 | - // if successful, set the primary key |
|
| 1851 | - // but don't use the normal SET method, because it will check if |
|
| 1852 | - // an item with the same ID exists in the mapper & db, then |
|
| 1853 | - // will find it in the db (because we just added it) and THAT object |
|
| 1854 | - // will get added to the mapper before we can add this one! |
|
| 1855 | - // but if we just avoid using the SET method, all that headache can be avoided |
|
| 1856 | - $pk_field_name = $model->primary_key_name(); |
|
| 1857 | - $this->_fields[ $pk_field_name ] = $results; |
|
| 1858 | - $this->_clear_cached_property($pk_field_name); |
|
| 1859 | - $model->add_to_entity_map($this); |
|
| 1860 | - $this->_update_cached_related_model_objs_fks(); |
|
| 1861 | - } |
|
| 1862 | - } |
|
| 1863 | - } else {// PK is NOT auto-increment |
|
| 1864 | - // so check if one like it already exists in the db |
|
| 1865 | - if ($model->exists_by_ID($this->ID())) { |
|
| 1866 | - if (WP_DEBUG && ! $this->in_entity_map()) { |
|
| 1867 | - throw new EE_Error( |
|
| 1868 | - sprintf( |
|
| 1869 | - esc_html__( |
|
| 1870 | - 'Using a model object %1$s that is NOT in the entity map, can lead to unexpected errors. You should either: %4$s 1. Put it in the entity mapper by calling %2$s %4$s 2. Discard this model object and use what is in the entity mapper %4$s 3. Fetch from the database using %3$s', |
|
| 1871 | - 'event_espresso' |
|
| 1872 | - ), |
|
| 1873 | - get_class($this), |
|
| 1874 | - get_class($model) . '::instance()->add_to_entity_map()', |
|
| 1875 | - get_class($model) . '::instance()->get_one_by_ID()', |
|
| 1876 | - '<br />' |
|
| 1877 | - ) |
|
| 1878 | - ); |
|
| 1879 | - } |
|
| 1880 | - $results = $model->update_by_ID($save_cols_n_values, $this->ID()); |
|
| 1881 | - } else { |
|
| 1882 | - $results = $model->insert($save_cols_n_values); |
|
| 1883 | - $this->_update_cached_related_model_objs_fks(); |
|
| 1884 | - } |
|
| 1885 | - } |
|
| 1886 | - } else {// there is NO primary key |
|
| 1887 | - $already_in_db = false; |
|
| 1888 | - foreach ($model->unique_indexes() as $index) { |
|
| 1889 | - $uniqueness_where_params = array_intersect_key($save_cols_n_values, $index->fields()); |
|
| 1890 | - if ($model->exists(array($uniqueness_where_params))) { |
|
| 1891 | - $already_in_db = true; |
|
| 1892 | - } |
|
| 1893 | - } |
|
| 1894 | - if ($already_in_db) { |
|
| 1895 | - $combined_pk_fields_n_values = array_intersect_key( |
|
| 1896 | - $save_cols_n_values, |
|
| 1897 | - $model->get_combined_primary_key_fields() |
|
| 1898 | - ); |
|
| 1899 | - $results = $model->update( |
|
| 1900 | - $save_cols_n_values, |
|
| 1901 | - $combined_pk_fields_n_values |
|
| 1902 | - ); |
|
| 1903 | - } else { |
|
| 1904 | - $results = $model->insert($save_cols_n_values); |
|
| 1905 | - } |
|
| 1906 | - } |
|
| 1907 | - // restore the old assumption about values being prepared by the model object |
|
| 1908 | - $model->assume_values_already_prepared_by_model_object( |
|
| 1909 | - $old_assumption_concerning_value_preparation |
|
| 1910 | - ); |
|
| 1911 | - /** |
|
| 1912 | - * After saving the model object this action is called |
|
| 1913 | - * |
|
| 1914 | - * @param EE_Base_Class $model_object which was just saved |
|
| 1915 | - * @param boolean|int $results if it were updated, TRUE or FALSE; if it were newly inserted |
|
| 1916 | - * the new ID (or 0 if an error occurred and it wasn't updated) |
|
| 1917 | - */ |
|
| 1918 | - do_action('AHEE__EE_Base_Class__save__end', $this, $results); |
|
| 1919 | - $this->_has_changes = false; |
|
| 1920 | - return $results; |
|
| 1921 | - } |
|
| 1922 | - |
|
| 1923 | - |
|
| 1924 | - /** |
|
| 1925 | - * Updates the foreign key on related models objects pointing to this to have this model object's ID |
|
| 1926 | - * as their foreign key. If the cached related model objects already exist in the db, saves them (so that the DB |
|
| 1927 | - * is consistent) Especially useful in case we JUST added this model object ot the database and we want to let its |
|
| 1928 | - * cached relations with foreign keys to it know about that change. Eg: we've created a transaction but haven't |
|
| 1929 | - * saved it to the db. We also create a registration and don't save it to the DB, but we DO cache it on the |
|
| 1930 | - * transaction. Now, when we save the transaction, the registration's TXN_ID will be automatically updated, whether |
|
| 1931 | - * or not they exist in the DB (if they do, their DB records will be automatically updated) |
|
| 1932 | - * |
|
| 1933 | - * @return void |
|
| 1934 | - * @throws ReflectionException |
|
| 1935 | - * @throws InvalidArgumentException |
|
| 1936 | - * @throws InvalidInterfaceException |
|
| 1937 | - * @throws InvalidDataTypeException |
|
| 1938 | - * @throws EE_Error |
|
| 1939 | - */ |
|
| 1940 | - protected function _update_cached_related_model_objs_fks() |
|
| 1941 | - { |
|
| 1942 | - $model = $this->get_model(); |
|
| 1943 | - foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
|
| 1944 | - if ($relation_obj instanceof EE_Has_Many_Relation) { |
|
| 1945 | - foreach ($this->get_all_from_cache($relation_name) as $related_model_obj_in_cache) { |
|
| 1946 | - $fk_to_this = $related_model_obj_in_cache->get_model()->get_foreign_key_to( |
|
| 1947 | - $model->get_this_model_name() |
|
| 1948 | - ); |
|
| 1949 | - $related_model_obj_in_cache->set($fk_to_this->get_name(), $this->ID()); |
|
| 1950 | - if ($related_model_obj_in_cache->ID()) { |
|
| 1951 | - $related_model_obj_in_cache->save(); |
|
| 1952 | - } |
|
| 1953 | - } |
|
| 1954 | - } |
|
| 1955 | - } |
|
| 1956 | - } |
|
| 1957 | - |
|
| 1958 | - |
|
| 1959 | - /** |
|
| 1960 | - * Saves this model object and its NEW cached relations to the database. |
|
| 1961 | - * (Meaning, for now, IT DOES NOT WORK if the cached items already exist in the DB. |
|
| 1962 | - * In order for that to work, we would need to mark model objects as dirty/clean... |
|
| 1963 | - * because otherwise, there's a potential for infinite looping of saving |
|
| 1964 | - * Saves the cached related model objects, and ensures the relation between them |
|
| 1965 | - * and this object and properly setup |
|
| 1966 | - * |
|
| 1967 | - * @return int ID of new model object on save; 0 on failure+ |
|
| 1968 | - * @throws ReflectionException |
|
| 1969 | - * @throws InvalidArgumentException |
|
| 1970 | - * @throws InvalidInterfaceException |
|
| 1971 | - * @throws InvalidDataTypeException |
|
| 1972 | - * @throws EE_Error |
|
| 1973 | - */ |
|
| 1974 | - public function save_new_cached_related_model_objs() |
|
| 1975 | - { |
|
| 1976 | - // make sure this has been saved |
|
| 1977 | - if (! $this->ID()) { |
|
| 1978 | - $id = $this->save(); |
|
| 1979 | - } else { |
|
| 1980 | - $id = $this->ID(); |
|
| 1981 | - } |
|
| 1982 | - // now save all the NEW cached model objects (ie they don't exist in the DB) |
|
| 1983 | - foreach ($this->get_model()->relation_settings() as $relationName => $relationObj) { |
|
| 1984 | - if ($this->_model_relations[ $relationName ]) { |
|
| 1985 | - // is this a relation where we should expect just ONE related object (ie, EE_Belongs_To_relation) |
|
| 1986 | - // or MANY related objects (ie, EE_HABTM_Relation or EE_Has_Many_Relation)? |
|
| 1987 | - /* @var $related_model_obj EE_Base_Class */ |
|
| 1988 | - if ($relationObj instanceof EE_Belongs_To_Relation) { |
|
| 1989 | - // add a relation to that relation type (which saves the appropriate thing in the process) |
|
| 1990 | - // but ONLY if it DOES NOT exist in the DB |
|
| 1991 | - $related_model_obj = $this->_model_relations[ $relationName ]; |
|
| 1992 | - // if( ! $related_model_obj->ID()){ |
|
| 1993 | - $this->_add_relation_to($related_model_obj, $relationName); |
|
| 1994 | - $related_model_obj->save_new_cached_related_model_objs(); |
|
| 1995 | - // } |
|
| 1996 | - } else { |
|
| 1997 | - foreach ($this->_model_relations[ $relationName ] as $related_model_obj) { |
|
| 1998 | - // add a relation to that relation type (which saves the appropriate thing in the process) |
|
| 1999 | - // but ONLY if it DOES NOT exist in the DB |
|
| 2000 | - // if( ! $related_model_obj->ID()){ |
|
| 2001 | - $this->_add_relation_to($related_model_obj, $relationName); |
|
| 2002 | - $related_model_obj->save_new_cached_related_model_objs(); |
|
| 2003 | - // } |
|
| 2004 | - } |
|
| 2005 | - } |
|
| 2006 | - } |
|
| 2007 | - } |
|
| 2008 | - return $id; |
|
| 2009 | - } |
|
| 2010 | - |
|
| 2011 | - |
|
| 2012 | - /** |
|
| 2013 | - * for getting a model while instantiated. |
|
| 2014 | - * |
|
| 2015 | - * @return EEM_Base | EEM_CPT_Base |
|
| 2016 | - * @throws ReflectionException |
|
| 2017 | - * @throws InvalidArgumentException |
|
| 2018 | - * @throws InvalidInterfaceException |
|
| 2019 | - * @throws InvalidDataTypeException |
|
| 2020 | - * @throws EE_Error |
|
| 2021 | - */ |
|
| 2022 | - public function get_model() |
|
| 2023 | - { |
|
| 2024 | - if (! $this->_model) { |
|
| 2025 | - $modelName = self::_get_model_classname(get_class($this)); |
|
| 2026 | - $this->_model = self::_get_model_instance_with_name($modelName, $this->_timezone); |
|
| 2027 | - } else { |
|
| 2028 | - $this->_model->set_timezone($this->_timezone); |
|
| 2029 | - } |
|
| 2030 | - return $this->_model; |
|
| 2031 | - } |
|
| 2032 | - |
|
| 2033 | - |
|
| 2034 | - /** |
|
| 2035 | - * @param $props_n_values |
|
| 2036 | - * @param $classname |
|
| 2037 | - * @return mixed bool|EE_Base_Class|EEM_CPT_Base |
|
| 2038 | - * @throws ReflectionException |
|
| 2039 | - * @throws InvalidArgumentException |
|
| 2040 | - * @throws InvalidInterfaceException |
|
| 2041 | - * @throws InvalidDataTypeException |
|
| 2042 | - * @throws EE_Error |
|
| 2043 | - */ |
|
| 2044 | - protected static function _get_object_from_entity_mapper($props_n_values, $classname) |
|
| 2045 | - { |
|
| 2046 | - // TODO: will not work for Term_Relationships because they have no PK! |
|
| 2047 | - $primary_id_ref = self::_get_primary_key_name($classname); |
|
| 2048 | - if (array_key_exists($primary_id_ref, $props_n_values) |
|
| 2049 | - && ! empty($props_n_values[ $primary_id_ref ]) |
|
| 2050 | - ) { |
|
| 2051 | - $id = $props_n_values[ $primary_id_ref ]; |
|
| 2052 | - return self::_get_model($classname)->get_from_entity_map($id); |
|
| 2053 | - } |
|
| 2054 | - return false; |
|
| 2055 | - } |
|
| 2056 | - |
|
| 2057 | - |
|
| 2058 | - /** |
|
| 2059 | - * This is called by child static "new_instance" method and we'll check to see if there is an existing db entry for |
|
| 2060 | - * the primary key (if present in incoming values). If there is a key in the incoming array that matches the |
|
| 2061 | - * primary key for the model AND it is not null, then we check the db. If there's a an object we return it. If not |
|
| 2062 | - * we return false. |
|
| 2063 | - * |
|
| 2064 | - * @param array $props_n_values incoming array of properties and their values |
|
| 2065 | - * @param string $classname the classname of the child class |
|
| 2066 | - * @param null $timezone |
|
| 2067 | - * @param array $date_formats incoming date_formats in an array where the first value is the |
|
| 2068 | - * date_format and the second value is the time format |
|
| 2069 | - * @return mixed (EE_Base_Class|bool) |
|
| 2070 | - * @throws InvalidArgumentException |
|
| 2071 | - * @throws InvalidInterfaceException |
|
| 2072 | - * @throws InvalidDataTypeException |
|
| 2073 | - * @throws EE_Error |
|
| 2074 | - * @throws ReflectionException |
|
| 2075 | - * @throws ReflectionException |
|
| 2076 | - * @throws ReflectionException |
|
| 2077 | - */ |
|
| 2078 | - protected static function _check_for_object($props_n_values, $classname, $timezone = null, $date_formats = array()) |
|
| 2079 | - { |
|
| 2080 | - $existing = null; |
|
| 2081 | - $model = self::_get_model($classname, $timezone); |
|
| 2082 | - if ($model->has_primary_key_field()) { |
|
| 2083 | - $primary_id_ref = self::_get_primary_key_name($classname); |
|
| 2084 | - if (array_key_exists($primary_id_ref, $props_n_values) |
|
| 2085 | - && ! empty($props_n_values[ $primary_id_ref ]) |
|
| 2086 | - ) { |
|
| 2087 | - $existing = $model->get_one_by_ID( |
|
| 2088 | - $props_n_values[ $primary_id_ref ] |
|
| 2089 | - ); |
|
| 2090 | - } |
|
| 2091 | - } elseif ($model->has_all_combined_primary_key_fields($props_n_values)) { |
|
| 2092 | - // no primary key on this model, but there's still a matching item in the DB |
|
| 2093 | - $existing = self::_get_model($classname, $timezone)->get_one_by_ID( |
|
| 2094 | - self::_get_model($classname, $timezone) |
|
| 2095 | - ->get_index_primary_key_string($props_n_values) |
|
| 2096 | - ); |
|
| 2097 | - } |
|
| 2098 | - if ($existing) { |
|
| 2099 | - // set date formats if present before setting values |
|
| 2100 | - if (! empty($date_formats) && is_array($date_formats)) { |
|
| 2101 | - $existing->set_date_format($date_formats[0]); |
|
| 2102 | - $existing->set_time_format($date_formats[1]); |
|
| 2103 | - } else { |
|
| 2104 | - // set default formats for date and time |
|
| 2105 | - $existing->set_date_format(get_option('date_format')); |
|
| 2106 | - $existing->set_time_format(get_option('time_format')); |
|
| 2107 | - } |
|
| 2108 | - foreach ($props_n_values as $property => $field_value) { |
|
| 2109 | - $existing->set($property, $field_value); |
|
| 2110 | - } |
|
| 2111 | - return $existing; |
|
| 2112 | - } |
|
| 2113 | - return false; |
|
| 2114 | - } |
|
| 2115 | - |
|
| 2116 | - |
|
| 2117 | - /** |
|
| 2118 | - * Gets the EEM_*_Model for this class |
|
| 2119 | - * |
|
| 2120 | - * @access public now, as this is more convenient |
|
| 2121 | - * @param $classname |
|
| 2122 | - * @param null $timezone |
|
| 2123 | - * @throws ReflectionException |
|
| 2124 | - * @throws InvalidArgumentException |
|
| 2125 | - * @throws InvalidInterfaceException |
|
| 2126 | - * @throws InvalidDataTypeException |
|
| 2127 | - * @throws EE_Error |
|
| 2128 | - * @return EEM_Base |
|
| 2129 | - */ |
|
| 2130 | - protected static function _get_model($classname, $timezone = null) |
|
| 2131 | - { |
|
| 2132 | - // find model for this class |
|
| 2133 | - if (! $classname) { |
|
| 2134 | - throw new EE_Error( |
|
| 2135 | - sprintf( |
|
| 2136 | - esc_html__( |
|
| 2137 | - 'What were you thinking calling _get_model(%s)?? You need to specify the class name', |
|
| 2138 | - 'event_espresso' |
|
| 2139 | - ), |
|
| 2140 | - $classname |
|
| 2141 | - ) |
|
| 2142 | - ); |
|
| 2143 | - } |
|
| 2144 | - $modelName = self::_get_model_classname($classname); |
|
| 2145 | - return self::_get_model_instance_with_name($modelName, $timezone); |
|
| 2146 | - } |
|
| 2147 | - |
|
| 2148 | - |
|
| 2149 | - /** |
|
| 2150 | - * Gets the model instance (eg instance of EEM_Attendee) given its classname (eg EE_Attendee) |
|
| 2151 | - * |
|
| 2152 | - * @param string $model_classname |
|
| 2153 | - * @param null $timezone |
|
| 2154 | - * @return EEM_Base |
|
| 2155 | - * @throws ReflectionException |
|
| 2156 | - * @throws InvalidArgumentException |
|
| 2157 | - * @throws InvalidInterfaceException |
|
| 2158 | - * @throws InvalidDataTypeException |
|
| 2159 | - * @throws EE_Error |
|
| 2160 | - */ |
|
| 2161 | - protected static function _get_model_instance_with_name($model_classname, $timezone = null) |
|
| 2162 | - { |
|
| 2163 | - $model_classname = str_replace('EEM_', '', $model_classname); |
|
| 2164 | - $model = EE_Registry::instance()->load_model($model_classname); |
|
| 2165 | - $model->set_timezone($timezone); |
|
| 2166 | - return $model; |
|
| 2167 | - } |
|
| 2168 | - |
|
| 2169 | - |
|
| 2170 | - /** |
|
| 2171 | - * If a model name is provided (eg Registration), gets the model classname for that model. |
|
| 2172 | - * Also works if a model class's classname is provided (eg EE_Registration). |
|
| 2173 | - * |
|
| 2174 | - * @param null $model_name |
|
| 2175 | - * @return string like EEM_Attendee |
|
| 2176 | - */ |
|
| 2177 | - private static function _get_model_classname($model_name = null) |
|
| 2178 | - { |
|
| 2179 | - if (strpos($model_name, 'EE_') === 0) { |
|
| 2180 | - $model_classname = str_replace('EE_', 'EEM_', $model_name); |
|
| 2181 | - } else { |
|
| 2182 | - $model_classname = 'EEM_' . $model_name; |
|
| 2183 | - } |
|
| 2184 | - return $model_classname; |
|
| 2185 | - } |
|
| 2186 | - |
|
| 2187 | - |
|
| 2188 | - /** |
|
| 2189 | - * returns the name of the primary key attribute |
|
| 2190 | - * |
|
| 2191 | - * @param null $classname |
|
| 2192 | - * @throws ReflectionException |
|
| 2193 | - * @throws InvalidArgumentException |
|
| 2194 | - * @throws InvalidInterfaceException |
|
| 2195 | - * @throws InvalidDataTypeException |
|
| 2196 | - * @throws EE_Error |
|
| 2197 | - * @return string |
|
| 2198 | - */ |
|
| 2199 | - protected static function _get_primary_key_name($classname = null) |
|
| 2200 | - { |
|
| 2201 | - if (! $classname) { |
|
| 2202 | - throw new EE_Error( |
|
| 2203 | - sprintf( |
|
| 2204 | - esc_html__('What were you thinking calling _get_primary_key_name(%s)', 'event_espresso'), |
|
| 2205 | - $classname |
|
| 2206 | - ) |
|
| 2207 | - ); |
|
| 2208 | - } |
|
| 2209 | - return self::_get_model($classname)->get_primary_key_field()->get_name(); |
|
| 2210 | - } |
|
| 2211 | - |
|
| 2212 | - |
|
| 2213 | - /** |
|
| 2214 | - * Gets the value of the primary key. |
|
| 2215 | - * If the object hasn't yet been saved, it should be whatever the model field's default was |
|
| 2216 | - * (eg, if this were the EE_Event class, look at the primary key field on EEM_Event and see what its default value |
|
| 2217 | - * is. Usually defaults for integer primary keys are 0; string primary keys are usually NULL). |
|
| 2218 | - * |
|
| 2219 | - * @return mixed, if the primary key is of type INT it'll be an int. Otherwise it could be a string |
|
| 2220 | - * @throws ReflectionException |
|
| 2221 | - * @throws InvalidArgumentException |
|
| 2222 | - * @throws InvalidInterfaceException |
|
| 2223 | - * @throws InvalidDataTypeException |
|
| 2224 | - * @throws EE_Error |
|
| 2225 | - */ |
|
| 2226 | - public function ID() |
|
| 2227 | - { |
|
| 2228 | - $model = $this->get_model(); |
|
| 2229 | - // now that we know the name of the variable, use a variable variable to get its value and return its |
|
| 2230 | - if ($model->has_primary_key_field()) { |
|
| 2231 | - return $this->_fields[ $model->primary_key_name() ]; |
|
| 2232 | - } |
|
| 2233 | - return $model->get_index_primary_key_string($this->_fields); |
|
| 2234 | - } |
|
| 2235 | - |
|
| 2236 | - |
|
| 2237 | - /** |
|
| 2238 | - * Adds a relationship to the specified EE_Base_Class object, given the relationship's name. Eg, if the current |
|
| 2239 | - * model is related to a group of events, the $relationName should be 'Event', and should be a key in the EE |
|
| 2240 | - * Model's $_model_relations array. If this model object doesn't exist in the DB, just caches the related thing |
|
| 2241 | - * |
|
| 2242 | - * @param mixed $otherObjectModelObjectOrID EE_Base_Class or the ID of the other object |
|
| 2243 | - * @param string $relationName eg 'Events','Question',etc. |
|
| 2244 | - * an attendee to a group, you also want to specify which role they |
|
| 2245 | - * will have in that group. So you would use this parameter to |
|
| 2246 | - * specify array('role-column-name'=>'role-id') |
|
| 2247 | - * @param array $extra_join_model_fields_n_values You can optionally include an array of key=>value pairs that |
|
| 2248 | - * allow you to further constrict the relation to being added. |
|
| 2249 | - * However, keep in mind that the columns (keys) given must match a |
|
| 2250 | - * column on the JOIN table and currently only the HABTM models |
|
| 2251 | - * accept these additional conditions. Also remember that if an |
|
| 2252 | - * exact match isn't found for these extra cols/val pairs, then a |
|
| 2253 | - * NEW row is created in the join table. |
|
| 2254 | - * @param null $cache_id |
|
| 2255 | - * @throws ReflectionException |
|
| 2256 | - * @throws InvalidArgumentException |
|
| 2257 | - * @throws InvalidInterfaceException |
|
| 2258 | - * @throws InvalidDataTypeException |
|
| 2259 | - * @throws EE_Error |
|
| 2260 | - * @return EE_Base_Class the object the relation was added to |
|
| 2261 | - */ |
|
| 2262 | - public function _add_relation_to( |
|
| 2263 | - $otherObjectModelObjectOrID, |
|
| 2264 | - $relationName, |
|
| 2265 | - $extra_join_model_fields_n_values = array(), |
|
| 2266 | - $cache_id = null |
|
| 2267 | - ) { |
|
| 2268 | - $model = $this->get_model(); |
|
| 2269 | - // if this thing exists in the DB, save the relation to the DB |
|
| 2270 | - if ($this->ID()) { |
|
| 2271 | - $otherObject = $model->add_relationship_to( |
|
| 2272 | - $this, |
|
| 2273 | - $otherObjectModelObjectOrID, |
|
| 2274 | - $relationName, |
|
| 2275 | - $extra_join_model_fields_n_values |
|
| 2276 | - ); |
|
| 2277 | - // clear cache so future get_many_related and get_first_related() return new results. |
|
| 2278 | - $this->clear_cache($relationName, $otherObject, true); |
|
| 2279 | - if ($otherObject instanceof EE_Base_Class) { |
|
| 2280 | - $otherObject->clear_cache($model->get_this_model_name(), $this); |
|
| 2281 | - } |
|
| 2282 | - } else { |
|
| 2283 | - // this thing doesn't exist in the DB, so just cache it |
|
| 2284 | - if (! $otherObjectModelObjectOrID instanceof EE_Base_Class) { |
|
| 2285 | - throw new EE_Error( |
|
| 2286 | - sprintf( |
|
| 2287 | - esc_html__( |
|
| 2288 | - 'Before a model object is saved to the database, calls to _add_relation_to must be passed an actual object, not just an ID. You provided %s as the model object to a %s', |
|
| 2289 | - 'event_espresso' |
|
| 2290 | - ), |
|
| 2291 | - $otherObjectModelObjectOrID, |
|
| 2292 | - get_class($this) |
|
| 2293 | - ) |
|
| 2294 | - ); |
|
| 2295 | - } |
|
| 2296 | - $otherObject = $otherObjectModelObjectOrID; |
|
| 2297 | - $this->cache($relationName, $otherObjectModelObjectOrID, $cache_id); |
|
| 2298 | - } |
|
| 2299 | - if ($otherObject instanceof EE_Base_Class) { |
|
| 2300 | - // fix the reciprocal relation too |
|
| 2301 | - if ($otherObject->ID()) { |
|
| 2302 | - // its saved so assumed relations exist in the DB, so we can just |
|
| 2303 | - // clear the cache so future queries use the updated info in the DB |
|
| 2304 | - $otherObject->clear_cache( |
|
| 2305 | - $model->get_this_model_name(), |
|
| 2306 | - null, |
|
| 2307 | - true |
|
| 2308 | - ); |
|
| 2309 | - } else { |
|
| 2310 | - // it's not saved, so it caches relations like this |
|
| 2311 | - $otherObject->cache($model->get_this_model_name(), $this); |
|
| 2312 | - } |
|
| 2313 | - } |
|
| 2314 | - return $otherObject; |
|
| 2315 | - } |
|
| 2316 | - |
|
| 2317 | - |
|
| 2318 | - /** |
|
| 2319 | - * Removes a relationship to the specified EE_Base_Class object, given the relationships' name. Eg, if the current |
|
| 2320 | - * model is related to a group of events, the $relationName should be 'Events', and should be a key in the EE |
|
| 2321 | - * Model's $_model_relations array. If this model object doesn't exist in the DB, just removes the related thing |
|
| 2322 | - * from the cache |
|
| 2323 | - * |
|
| 2324 | - * @param mixed $otherObjectModelObjectOrID |
|
| 2325 | - * EE_Base_Class or the ID of the other object, OR an array key into the cache if this isn't saved |
|
| 2326 | - * to the DB yet |
|
| 2327 | - * @param string $relationName |
|
| 2328 | - * @param array $where_query |
|
| 2329 | - * You can optionally include an array of key=>value pairs that allow you to further constrict the |
|
| 2330 | - * relation to being added. However, keep in mind that the columns (keys) given must match a column |
|
| 2331 | - * on the JOIN table and currently only the HABTM models accept these additional conditions. Also |
|
| 2332 | - * remember that if an exact match isn't found for these extra cols/val pairs, then no row is |
|
| 2333 | - * deleted. |
|
| 2334 | - * @return EE_Base_Class the relation was removed from |
|
| 2335 | - * @throws ReflectionException |
|
| 2336 | - * @throws InvalidArgumentException |
|
| 2337 | - * @throws InvalidInterfaceException |
|
| 2338 | - * @throws InvalidDataTypeException |
|
| 2339 | - * @throws EE_Error |
|
| 2340 | - */ |
|
| 2341 | - public function _remove_relation_to($otherObjectModelObjectOrID, $relationName, $where_query = array()) |
|
| 2342 | - { |
|
| 2343 | - if ($this->ID()) { |
|
| 2344 | - // if this exists in the DB, save the relation change to the DB too |
|
| 2345 | - $otherObject = $this->get_model()->remove_relationship_to( |
|
| 2346 | - $this, |
|
| 2347 | - $otherObjectModelObjectOrID, |
|
| 2348 | - $relationName, |
|
| 2349 | - $where_query |
|
| 2350 | - ); |
|
| 2351 | - $this->clear_cache( |
|
| 2352 | - $relationName, |
|
| 2353 | - $otherObject |
|
| 2354 | - ); |
|
| 2355 | - } else { |
|
| 2356 | - // this doesn't exist in the DB, just remove it from the cache |
|
| 2357 | - $otherObject = $this->clear_cache( |
|
| 2358 | - $relationName, |
|
| 2359 | - $otherObjectModelObjectOrID |
|
| 2360 | - ); |
|
| 2361 | - } |
|
| 2362 | - if ($otherObject instanceof EE_Base_Class) { |
|
| 2363 | - $otherObject->clear_cache( |
|
| 2364 | - $this->get_model()->get_this_model_name(), |
|
| 2365 | - $this |
|
| 2366 | - ); |
|
| 2367 | - } |
|
| 2368 | - return $otherObject; |
|
| 2369 | - } |
|
| 2370 | - |
|
| 2371 | - |
|
| 2372 | - /** |
|
| 2373 | - * Removes ALL the related things for the $relationName. |
|
| 2374 | - * |
|
| 2375 | - * @param string $relationName |
|
| 2376 | - * @param array $where_query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
| 2377 | - * @return EE_Base_Class |
|
| 2378 | - * @throws ReflectionException |
|
| 2379 | - * @throws InvalidArgumentException |
|
| 2380 | - * @throws InvalidInterfaceException |
|
| 2381 | - * @throws InvalidDataTypeException |
|
| 2382 | - * @throws EE_Error |
|
| 2383 | - */ |
|
| 2384 | - public function _remove_relations($relationName, $where_query_params = array()) |
|
| 2385 | - { |
|
| 2386 | - if ($this->ID()) { |
|
| 2387 | - // if this exists in the DB, save the relation change to the DB too |
|
| 2388 | - $otherObjects = $this->get_model()->remove_relations( |
|
| 2389 | - $this, |
|
| 2390 | - $relationName, |
|
| 2391 | - $where_query_params |
|
| 2392 | - ); |
|
| 2393 | - $this->clear_cache( |
|
| 2394 | - $relationName, |
|
| 2395 | - null, |
|
| 2396 | - true |
|
| 2397 | - ); |
|
| 2398 | - } else { |
|
| 2399 | - // this doesn't exist in the DB, just remove it from the cache |
|
| 2400 | - $otherObjects = $this->clear_cache( |
|
| 2401 | - $relationName, |
|
| 2402 | - null, |
|
| 2403 | - true |
|
| 2404 | - ); |
|
| 2405 | - } |
|
| 2406 | - if (is_array($otherObjects)) { |
|
| 2407 | - foreach ($otherObjects as $otherObject) { |
|
| 2408 | - $otherObject->clear_cache( |
|
| 2409 | - $this->get_model()->get_this_model_name(), |
|
| 2410 | - $this |
|
| 2411 | - ); |
|
| 2412 | - } |
|
| 2413 | - } |
|
| 2414 | - return $otherObjects; |
|
| 2415 | - } |
|
| 2416 | - |
|
| 2417 | - |
|
| 2418 | - /** |
|
| 2419 | - * Gets all the related model objects of the specified type. Eg, if the current class if |
|
| 2420 | - * EE_Event, you could call $this->get_many_related('Registration') to get an array of all the |
|
| 2421 | - * EE_Registration objects which related to this event. Note: by default, we remove the "default query params" |
|
| 2422 | - * because we want to get even deleted items etc. |
|
| 2423 | - * |
|
| 2424 | - * @param string $relationName key in the model's _model_relations array |
|
| 2425 | - * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
| 2426 | - * @return EE_Base_Class[] Results not necessarily indexed by IDs, because some results might not have primary |
|
| 2427 | - * keys or might not be saved yet. Consider using EEM_Base::get_IDs() on these |
|
| 2428 | - * results if you want IDs |
|
| 2429 | - * @throws ReflectionException |
|
| 2430 | - * @throws InvalidArgumentException |
|
| 2431 | - * @throws InvalidInterfaceException |
|
| 2432 | - * @throws InvalidDataTypeException |
|
| 2433 | - * @throws EE_Error |
|
| 2434 | - */ |
|
| 2435 | - public function get_many_related($relationName, $query_params = array()) |
|
| 2436 | - { |
|
| 2437 | - if ($this->ID()) { |
|
| 2438 | - // this exists in the DB, so get the related things from either the cache or the DB |
|
| 2439 | - // if there are query parameters, forget about caching the related model objects. |
|
| 2440 | - if ($query_params) { |
|
| 2441 | - $related_model_objects = $this->get_model()->get_all_related( |
|
| 2442 | - $this, |
|
| 2443 | - $relationName, |
|
| 2444 | - $query_params |
|
| 2445 | - ); |
|
| 2446 | - } else { |
|
| 2447 | - // did we already cache the result of this query? |
|
| 2448 | - $cached_results = $this->get_all_from_cache($relationName); |
|
| 2449 | - if (! $cached_results) { |
|
| 2450 | - $related_model_objects = $this->get_model()->get_all_related( |
|
| 2451 | - $this, |
|
| 2452 | - $relationName, |
|
| 2453 | - $query_params |
|
| 2454 | - ); |
|
| 2455 | - // if no query parameters were passed, then we got all the related model objects |
|
| 2456 | - // for that relation. We can cache them then. |
|
| 2457 | - foreach ($related_model_objects as $related_model_object) { |
|
| 2458 | - $this->cache($relationName, $related_model_object); |
|
| 2459 | - } |
|
| 2460 | - } else { |
|
| 2461 | - $related_model_objects = $cached_results; |
|
| 2462 | - } |
|
| 2463 | - } |
|
| 2464 | - } else { |
|
| 2465 | - // this doesn't exist in the DB, so just get the related things from the cache |
|
| 2466 | - $related_model_objects = $this->get_all_from_cache($relationName); |
|
| 2467 | - } |
|
| 2468 | - return $related_model_objects; |
|
| 2469 | - } |
|
| 2470 | - |
|
| 2471 | - |
|
| 2472 | - /** |
|
| 2473 | - * Instead of getting the related model objects, simply counts them. Ignores default_where_conditions by default, |
|
| 2474 | - * unless otherwise specified in the $query_params |
|
| 2475 | - * |
|
| 2476 | - * @param string $relation_name model_name like 'Event', or 'Registration' |
|
| 2477 | - * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 2478 | - * @param string $field_to_count name of field to count by. By default, uses primary key |
|
| 2479 | - * @param bool $distinct if we want to only count the distinct values for the column then you can trigger |
|
| 2480 | - * that by the setting $distinct to TRUE; |
|
| 2481 | - * @return int |
|
| 2482 | - * @throws ReflectionException |
|
| 2483 | - * @throws InvalidArgumentException |
|
| 2484 | - * @throws InvalidInterfaceException |
|
| 2485 | - * @throws InvalidDataTypeException |
|
| 2486 | - * @throws EE_Error |
|
| 2487 | - */ |
|
| 2488 | - public function count_related($relation_name, $query_params = array(), $field_to_count = null, $distinct = false) |
|
| 2489 | - { |
|
| 2490 | - return $this->get_model()->count_related( |
|
| 2491 | - $this, |
|
| 2492 | - $relation_name, |
|
| 2493 | - $query_params, |
|
| 2494 | - $field_to_count, |
|
| 2495 | - $distinct |
|
| 2496 | - ); |
|
| 2497 | - } |
|
| 2498 | - |
|
| 2499 | - |
|
| 2500 | - /** |
|
| 2501 | - * Instead of getting the related model objects, simply sums up the values of the specified field. |
|
| 2502 | - * Note: ignores default_where_conditions by default, unless otherwise specified in the $query_params |
|
| 2503 | - * |
|
| 2504 | - * @param string $relation_name model_name like 'Event', or 'Registration' |
|
| 2505 | - * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 2506 | - * @param string $field_to_sum name of field to count by. |
|
| 2507 | - * By default, uses primary key |
|
| 2508 | - * (which doesn't make much sense, so you should probably change it) |
|
| 2509 | - * @return int |
|
| 2510 | - * @throws ReflectionException |
|
| 2511 | - * @throws InvalidArgumentException |
|
| 2512 | - * @throws InvalidInterfaceException |
|
| 2513 | - * @throws InvalidDataTypeException |
|
| 2514 | - * @throws EE_Error |
|
| 2515 | - */ |
|
| 2516 | - public function sum_related($relation_name, $query_params = array(), $field_to_sum = null) |
|
| 2517 | - { |
|
| 2518 | - return $this->get_model()->sum_related( |
|
| 2519 | - $this, |
|
| 2520 | - $relation_name, |
|
| 2521 | - $query_params, |
|
| 2522 | - $field_to_sum |
|
| 2523 | - ); |
|
| 2524 | - } |
|
| 2525 | - |
|
| 2526 | - |
|
| 2527 | - /** |
|
| 2528 | - * Gets the first (ie, one) related model object of the specified type. |
|
| 2529 | - * |
|
| 2530 | - * @param string $relationName key in the model's _model_relations array |
|
| 2531 | - * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 2532 | - * @return EE_Base_Class (not an array, a single object) |
|
| 2533 | - * @throws ReflectionException |
|
| 2534 | - * @throws InvalidArgumentException |
|
| 2535 | - * @throws InvalidInterfaceException |
|
| 2536 | - * @throws InvalidDataTypeException |
|
| 2537 | - * @throws EE_Error |
|
| 2538 | - */ |
|
| 2539 | - public function get_first_related($relationName, $query_params = array()) |
|
| 2540 | - { |
|
| 2541 | - $model = $this->get_model(); |
|
| 2542 | - if ($this->ID()) {// this exists in the DB, get from the cache OR the DB |
|
| 2543 | - // if they've provided some query parameters, don't bother trying to cache the result |
|
| 2544 | - // also make sure we're not caching the result of get_first_related |
|
| 2545 | - // on a relation which should have an array of objects (because the cache might have an array of objects) |
|
| 2546 | - if ($query_params |
|
| 2547 | - || ! $model->related_settings_for($relationName) |
|
| 2548 | - instanceof |
|
| 2549 | - EE_Belongs_To_Relation |
|
| 2550 | - ) { |
|
| 2551 | - $related_model_object = $model->get_first_related( |
|
| 2552 | - $this, |
|
| 2553 | - $relationName, |
|
| 2554 | - $query_params |
|
| 2555 | - ); |
|
| 2556 | - } else { |
|
| 2557 | - // first, check if we've already cached the result of this query |
|
| 2558 | - $cached_result = $this->get_one_from_cache($relationName); |
|
| 2559 | - if (! $cached_result) { |
|
| 2560 | - $related_model_object = $model->get_first_related( |
|
| 2561 | - $this, |
|
| 2562 | - $relationName, |
|
| 2563 | - $query_params |
|
| 2564 | - ); |
|
| 2565 | - $this->cache($relationName, $related_model_object); |
|
| 2566 | - } else { |
|
| 2567 | - $related_model_object = $cached_result; |
|
| 2568 | - } |
|
| 2569 | - } |
|
| 2570 | - } else { |
|
| 2571 | - $related_model_object = null; |
|
| 2572 | - // this doesn't exist in the Db, |
|
| 2573 | - // but maybe the relation is of type belongs to, and so the related thing might |
|
| 2574 | - if ($model->related_settings_for($relationName) instanceof EE_Belongs_To_Relation) { |
|
| 2575 | - $related_model_object = $model->get_first_related( |
|
| 2576 | - $this, |
|
| 2577 | - $relationName, |
|
| 2578 | - $query_params |
|
| 2579 | - ); |
|
| 2580 | - } |
|
| 2581 | - // this doesn't exist in the DB and apparently the thing it belongs to doesn't either, |
|
| 2582 | - // just get what's cached on this object |
|
| 2583 | - if (! $related_model_object) { |
|
| 2584 | - $related_model_object = $this->get_one_from_cache($relationName); |
|
| 2585 | - } |
|
| 2586 | - } |
|
| 2587 | - return $related_model_object; |
|
| 2588 | - } |
|
| 2589 | - |
|
| 2590 | - |
|
| 2591 | - /** |
|
| 2592 | - * Does a delete on all related objects of type $relationName and removes |
|
| 2593 | - * the current model object's relation to them. If they can't be deleted (because |
|
| 2594 | - * of blocking related model objects) does nothing. If the related model objects are |
|
| 2595 | - * soft-deletable, they will be soft-deleted regardless of related blocking model objects. |
|
| 2596 | - * If this model object doesn't exist yet in the DB, just removes its related things |
|
| 2597 | - * |
|
| 2598 | - * @param string $relationName |
|
| 2599 | - * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 2600 | - * @return int how many deleted |
|
| 2601 | - * @throws ReflectionException |
|
| 2602 | - * @throws InvalidArgumentException |
|
| 2603 | - * @throws InvalidInterfaceException |
|
| 2604 | - * @throws InvalidDataTypeException |
|
| 2605 | - * @throws EE_Error |
|
| 2606 | - */ |
|
| 2607 | - public function delete_related($relationName, $query_params = array()) |
|
| 2608 | - { |
|
| 2609 | - if ($this->ID()) { |
|
| 2610 | - $count = $this->get_model()->delete_related( |
|
| 2611 | - $this, |
|
| 2612 | - $relationName, |
|
| 2613 | - $query_params |
|
| 2614 | - ); |
|
| 2615 | - } else { |
|
| 2616 | - $count = count($this->get_all_from_cache($relationName)); |
|
| 2617 | - $this->clear_cache($relationName, null, true); |
|
| 2618 | - } |
|
| 2619 | - return $count; |
|
| 2620 | - } |
|
| 2621 | - |
|
| 2622 | - |
|
| 2623 | - /** |
|
| 2624 | - * Does a hard delete (ie, removes the DB row) on all related objects of type $relationName and removes |
|
| 2625 | - * the current model object's relation to them. If they can't be deleted (because |
|
| 2626 | - * of blocking related model objects) just does a soft delete on it instead, if possible. |
|
| 2627 | - * If the related thing isn't a soft-deletable model object, this function is identical |
|
| 2628 | - * to delete_related(). If this model object doesn't exist in the DB, just remove its related things |
|
| 2629 | - * |
|
| 2630 | - * @param string $relationName |
|
| 2631 | - * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 2632 | - * @return int how many deleted (including those soft deleted) |
|
| 2633 | - * @throws ReflectionException |
|
| 2634 | - * @throws InvalidArgumentException |
|
| 2635 | - * @throws InvalidInterfaceException |
|
| 2636 | - * @throws InvalidDataTypeException |
|
| 2637 | - * @throws EE_Error |
|
| 2638 | - */ |
|
| 2639 | - public function delete_related_permanently($relationName, $query_params = array()) |
|
| 2640 | - { |
|
| 2641 | - if ($this->ID()) { |
|
| 2642 | - $count = $this->get_model()->delete_related_permanently( |
|
| 2643 | - $this, |
|
| 2644 | - $relationName, |
|
| 2645 | - $query_params |
|
| 2646 | - ); |
|
| 2647 | - } else { |
|
| 2648 | - $count = count($this->get_all_from_cache($relationName)); |
|
| 2649 | - } |
|
| 2650 | - $this->clear_cache($relationName, null, true); |
|
| 2651 | - return $count; |
|
| 2652 | - } |
|
| 2653 | - |
|
| 2654 | - |
|
| 2655 | - /** |
|
| 2656 | - * is_set |
|
| 2657 | - * Just a simple utility function children can use for checking if property exists |
|
| 2658 | - * |
|
| 2659 | - * @access public |
|
| 2660 | - * @param string $field_name property to check |
|
| 2661 | - * @return bool TRUE if existing,FALSE if not. |
|
| 2662 | - */ |
|
| 2663 | - public function is_set($field_name) |
|
| 2664 | - { |
|
| 2665 | - return isset($this->_fields[ $field_name ]); |
|
| 2666 | - } |
|
| 2667 | - |
|
| 2668 | - |
|
| 2669 | - /** |
|
| 2670 | - * Just a simple utility function children can use for checking if property (or properties) exists and throwing an |
|
| 2671 | - * EE_Error exception if they don't |
|
| 2672 | - * |
|
| 2673 | - * @param mixed (string|array) $properties properties to check |
|
| 2674 | - * @throws EE_Error |
|
| 2675 | - * @return bool TRUE if existing, throw EE_Error if not. |
|
| 2676 | - */ |
|
| 2677 | - protected function _property_exists($properties) |
|
| 2678 | - { |
|
| 2679 | - foreach ((array) $properties as $property_name) { |
|
| 2680 | - // first make sure this property exists |
|
| 2681 | - if (! $this->_fields[ $property_name ]) { |
|
| 2682 | - throw new EE_Error( |
|
| 2683 | - sprintf( |
|
| 2684 | - esc_html__( |
|
| 2685 | - 'Trying to retrieve a non-existent property (%s). Double check the spelling please', |
|
| 2686 | - 'event_espresso' |
|
| 2687 | - ), |
|
| 2688 | - $property_name |
|
| 2689 | - ) |
|
| 2690 | - ); |
|
| 2691 | - } |
|
| 2692 | - } |
|
| 2693 | - return true; |
|
| 2694 | - } |
|
| 2695 | - |
|
| 2696 | - |
|
| 2697 | - /** |
|
| 2698 | - * This simply returns an array of model fields for this object |
|
| 2699 | - * |
|
| 2700 | - * @return array |
|
| 2701 | - * @throws ReflectionException |
|
| 2702 | - * @throws InvalidArgumentException |
|
| 2703 | - * @throws InvalidInterfaceException |
|
| 2704 | - * @throws InvalidDataTypeException |
|
| 2705 | - * @throws EE_Error |
|
| 2706 | - */ |
|
| 2707 | - public function model_field_array() |
|
| 2708 | - { |
|
| 2709 | - $fields = $this->get_model()->field_settings(false); |
|
| 2710 | - $properties = array(); |
|
| 2711 | - // remove prepended underscore |
|
| 2712 | - foreach ($fields as $field_name => $settings) { |
|
| 2713 | - $properties[ $field_name ] = $this->get($field_name); |
|
| 2714 | - } |
|
| 2715 | - return $properties; |
|
| 2716 | - } |
|
| 2717 | - |
|
| 2718 | - |
|
| 2719 | - /** |
|
| 2720 | - * Very handy general function to allow for plugins to extend any child of EE_Base_Class. |
|
| 2721 | - * If a method is called on a child of EE_Base_Class that doesn't exist, this function is called |
|
| 2722 | - * (http://www.garfieldtech.com/blog/php-magic-call) and passed the method's name and arguments. |
|
| 2723 | - * Instead of requiring a plugin to extend the EE_Base_Class |
|
| 2724 | - * (which works fine is there's only 1 plugin, but when will that happen?) |
|
| 2725 | - * they can add a hook onto 'filters_hook_espresso__{className}__{methodName}' |
|
| 2726 | - * (eg, filters_hook_espresso__EE_Answer__my_great_function) |
|
| 2727 | - * and accepts 2 arguments: the object on which the function was called, |
|
| 2728 | - * and an array of the original arguments passed to the function. |
|
| 2729 | - * Whatever their callback function returns will be returned by this function. |
|
| 2730 | - * Example: in functions.php (or in a plugin): |
|
| 2731 | - * add_filter('FHEE__EE_Answer__my_callback','my_callback',10,3); |
|
| 2732 | - * function my_callback($previousReturnValue,EE_Base_Class $object,$argsArray){ |
|
| 2733 | - * $returnString= "you called my_callback! and passed args:".implode(",",$argsArray); |
|
| 2734 | - * return $previousReturnValue.$returnString; |
|
| 2735 | - * } |
|
| 2736 | - * require('EE_Answer.class.php'); |
|
| 2737 | - * $answer= EE_Answer::new_instance(array('REG_ID' => 2,'QST_ID' => 3,'ANS_value' => The answer is 42')); |
|
| 2738 | - * echo $answer->my_callback('monkeys',100); |
|
| 2739 | - * //will output "you called my_callback! and passed args:monkeys,100" |
|
| 2740 | - * |
|
| 2741 | - * @param string $methodName name of method which was called on a child of EE_Base_Class, but which |
|
| 2742 | - * @param array $args array of original arguments passed to the function |
|
| 2743 | - * @throws EE_Error |
|
| 2744 | - * @return mixed whatever the plugin which calls add_filter decides |
|
| 2745 | - */ |
|
| 2746 | - public function __call($methodName, $args) |
|
| 2747 | - { |
|
| 2748 | - $className = get_class($this); |
|
| 2749 | - $tagName = "FHEE__{$className}__{$methodName}"; |
|
| 2750 | - if (! has_filter($tagName)) { |
|
| 2751 | - throw new EE_Error( |
|
| 2752 | - sprintf( |
|
| 2753 | - esc_html__( |
|
| 2754 | - "Method %s on class %s does not exist! You can create one with the following code in functions.php or in a plugin: add_filter('%s','my_callback',10,3);function my_callback(\$previousReturnValue,EE_Base_Class \$object, \$argsArray){/*function body*/return \$whatever;}", |
|
| 2755 | - 'event_espresso' |
|
| 2756 | - ), |
|
| 2757 | - $methodName, |
|
| 2758 | - $className, |
|
| 2759 | - $tagName |
|
| 2760 | - ) |
|
| 2761 | - ); |
|
| 2762 | - } |
|
| 2763 | - return apply_filters($tagName, null, $this, $args); |
|
| 2764 | - } |
|
| 2765 | - |
|
| 2766 | - |
|
| 2767 | - /** |
|
| 2768 | - * Similar to insert_post_meta, adds a record in the Extra_Meta model's table with the given key and value. |
|
| 2769 | - * A $previous_value can be specified in case there are many meta rows with the same key |
|
| 2770 | - * |
|
| 2771 | - * @param string $meta_key |
|
| 2772 | - * @param mixed $meta_value |
|
| 2773 | - * @param mixed $previous_value |
|
| 2774 | - * @return bool|int # of records updated (or BOOLEAN if we actually ended up inserting the extra meta row) |
|
| 2775 | - * NOTE: if the values haven't changed, returns 0 |
|
| 2776 | - * @throws InvalidArgumentException |
|
| 2777 | - * @throws InvalidInterfaceException |
|
| 2778 | - * @throws InvalidDataTypeException |
|
| 2779 | - * @throws EE_Error |
|
| 2780 | - * @throws ReflectionException |
|
| 2781 | - */ |
|
| 2782 | - public function update_extra_meta($meta_key, $meta_value, $previous_value = null) |
|
| 2783 | - { |
|
| 2784 | - $query_params = array( |
|
| 2785 | - array( |
|
| 2786 | - 'EXM_key' => $meta_key, |
|
| 2787 | - 'OBJ_ID' => $this->ID(), |
|
| 2788 | - 'EXM_type' => $this->get_model()->get_this_model_name(), |
|
| 2789 | - ), |
|
| 2790 | - ); |
|
| 2791 | - if ($previous_value !== null) { |
|
| 2792 | - $query_params[0]['EXM_value'] = $meta_value; |
|
| 2793 | - } |
|
| 2794 | - $existing_rows_like_that = EEM_Extra_Meta::instance()->get_all($query_params); |
|
| 2795 | - if (! $existing_rows_like_that) { |
|
| 2796 | - return $this->add_extra_meta($meta_key, $meta_value); |
|
| 2797 | - } |
|
| 2798 | - foreach ($existing_rows_like_that as $existing_row) { |
|
| 2799 | - $existing_row->save(array('EXM_value' => $meta_value)); |
|
| 2800 | - } |
|
| 2801 | - return count($existing_rows_like_that); |
|
| 2802 | - } |
|
| 2803 | - |
|
| 2804 | - |
|
| 2805 | - /** |
|
| 2806 | - * Adds a new extra meta record. If $unique is set to TRUE, we'll first double-check |
|
| 2807 | - * no other extra meta for this model object have the same key. Returns TRUE if the |
|
| 2808 | - * extra meta row was entered, false if not |
|
| 2809 | - * |
|
| 2810 | - * @param string $meta_key |
|
| 2811 | - * @param mixed $meta_value |
|
| 2812 | - * @param boolean $unique |
|
| 2813 | - * @return boolean |
|
| 2814 | - * @throws InvalidArgumentException |
|
| 2815 | - * @throws InvalidInterfaceException |
|
| 2816 | - * @throws InvalidDataTypeException |
|
| 2817 | - * @throws EE_Error |
|
| 2818 | - * @throws ReflectionException |
|
| 2819 | - * @throws ReflectionException |
|
| 2820 | - */ |
|
| 2821 | - public function add_extra_meta($meta_key, $meta_value, $unique = false) |
|
| 2822 | - { |
|
| 2823 | - if ($unique) { |
|
| 2824 | - $existing_extra_meta = EEM_Extra_Meta::instance()->get_one( |
|
| 2825 | - array( |
|
| 2826 | - array( |
|
| 2827 | - 'EXM_key' => $meta_key, |
|
| 2828 | - 'OBJ_ID' => $this->ID(), |
|
| 2829 | - 'EXM_type' => $this->get_model()->get_this_model_name(), |
|
| 2830 | - ), |
|
| 2831 | - ) |
|
| 2832 | - ); |
|
| 2833 | - if ($existing_extra_meta) { |
|
| 2834 | - return false; |
|
| 2835 | - } |
|
| 2836 | - } |
|
| 2837 | - $new_extra_meta = EE_Extra_Meta::new_instance( |
|
| 2838 | - array( |
|
| 2839 | - 'EXM_key' => $meta_key, |
|
| 2840 | - 'EXM_value' => $meta_value, |
|
| 2841 | - 'OBJ_ID' => $this->ID(), |
|
| 2842 | - 'EXM_type' => $this->get_model()->get_this_model_name(), |
|
| 2843 | - ) |
|
| 2844 | - ); |
|
| 2845 | - $new_extra_meta->save(); |
|
| 2846 | - return true; |
|
| 2847 | - } |
|
| 2848 | - |
|
| 2849 | - |
|
| 2850 | - /** |
|
| 2851 | - * Deletes all the extra meta rows for this record as specified by key. If $meta_value |
|
| 2852 | - * is specified, only deletes extra meta records with that value. |
|
| 2853 | - * |
|
| 2854 | - * @param string $meta_key |
|
| 2855 | - * @param mixed $meta_value |
|
| 2856 | - * @return int number of extra meta rows deleted |
|
| 2857 | - * @throws InvalidArgumentException |
|
| 2858 | - * @throws InvalidInterfaceException |
|
| 2859 | - * @throws InvalidDataTypeException |
|
| 2860 | - * @throws EE_Error |
|
| 2861 | - * @throws ReflectionException |
|
| 2862 | - */ |
|
| 2863 | - public function delete_extra_meta($meta_key, $meta_value = null) |
|
| 2864 | - { |
|
| 2865 | - $query_params = array( |
|
| 2866 | - array( |
|
| 2867 | - 'EXM_key' => $meta_key, |
|
| 2868 | - 'OBJ_ID' => $this->ID(), |
|
| 2869 | - 'EXM_type' => $this->get_model()->get_this_model_name(), |
|
| 2870 | - ), |
|
| 2871 | - ); |
|
| 2872 | - if ($meta_value !== null) { |
|
| 2873 | - $query_params[0]['EXM_value'] = $meta_value; |
|
| 2874 | - } |
|
| 2875 | - return EEM_Extra_Meta::instance()->delete($query_params); |
|
| 2876 | - } |
|
| 2877 | - |
|
| 2878 | - |
|
| 2879 | - /** |
|
| 2880 | - * Gets the extra meta with the given meta key. If you specify "single" we just return 1, otherwise |
|
| 2881 | - * an array of everything found. Requires that this model actually have a relation of type EE_Has_Many_Any_Relation. |
|
| 2882 | - * You can specify $default is case you haven't found the extra meta |
|
| 2883 | - * |
|
| 2884 | - * @param string $meta_key |
|
| 2885 | - * @param boolean $single |
|
| 2886 | - * @param mixed $default if we don't find anything, what should we return? |
|
| 2887 | - * @return mixed single value if $single; array if ! $single |
|
| 2888 | - * @throws ReflectionException |
|
| 2889 | - * @throws InvalidArgumentException |
|
| 2890 | - * @throws InvalidInterfaceException |
|
| 2891 | - * @throws InvalidDataTypeException |
|
| 2892 | - * @throws EE_Error |
|
| 2893 | - */ |
|
| 2894 | - public function get_extra_meta($meta_key, $single = false, $default = null) |
|
| 2895 | - { |
|
| 2896 | - if ($single) { |
|
| 2897 | - $result = $this->get_first_related( |
|
| 2898 | - 'Extra_Meta', |
|
| 2899 | - array(array('EXM_key' => $meta_key)) |
|
| 2900 | - ); |
|
| 2901 | - if ($result instanceof EE_Extra_Meta) { |
|
| 2902 | - return $result->value(); |
|
| 2903 | - } |
|
| 2904 | - } else { |
|
| 2905 | - $results = $this->get_many_related( |
|
| 2906 | - 'Extra_Meta', |
|
| 2907 | - array(array('EXM_key' => $meta_key)) |
|
| 2908 | - ); |
|
| 2909 | - if ($results) { |
|
| 2910 | - $values = array(); |
|
| 2911 | - foreach ($results as $result) { |
|
| 2912 | - if ($result instanceof EE_Extra_Meta) { |
|
| 2913 | - $values[ $result->ID() ] = $result->value(); |
|
| 2914 | - } |
|
| 2915 | - } |
|
| 2916 | - return $values; |
|
| 2917 | - } |
|
| 2918 | - } |
|
| 2919 | - // if nothing discovered yet return default. |
|
| 2920 | - return apply_filters( |
|
| 2921 | - 'FHEE__EE_Base_Class__get_extra_meta__default_value', |
|
| 2922 | - $default, |
|
| 2923 | - $meta_key, |
|
| 2924 | - $single, |
|
| 2925 | - $this |
|
| 2926 | - ); |
|
| 2927 | - } |
|
| 2928 | - |
|
| 2929 | - |
|
| 2930 | - /** |
|
| 2931 | - * Returns a simple array of all the extra meta associated with this model object. |
|
| 2932 | - * If $one_of_each_key is true (Default), it will be an array of simple key-value pairs, keys being the |
|
| 2933 | - * extra meta's key, and teh value being its value. However, if there are duplicate extra meta rows with |
|
| 2934 | - * the same key, only one will be used. (eg array('foo'=>'bar','monkey'=>123)) |
|
| 2935 | - * If $one_of_each_key is false, it will return an array with the top-level keys being |
|
| 2936 | - * the extra meta keys, but their values are also arrays, which have the extra-meta's ID as their sub-key, and |
|
| 2937 | - * finally the extra meta's value as each sub-value. (eg |
|
| 2938 | - * array('foo'=>array(1=>'bar',2=>'bill'),'monkey'=>array(3=>123))) |
|
| 2939 | - * |
|
| 2940 | - * @param boolean $one_of_each_key |
|
| 2941 | - * @return array |
|
| 2942 | - * @throws ReflectionException |
|
| 2943 | - * @throws InvalidArgumentException |
|
| 2944 | - * @throws InvalidInterfaceException |
|
| 2945 | - * @throws InvalidDataTypeException |
|
| 2946 | - * @throws EE_Error |
|
| 2947 | - */ |
|
| 2948 | - public function all_extra_meta_array($one_of_each_key = true) |
|
| 2949 | - { |
|
| 2950 | - $return_array = array(); |
|
| 2951 | - if ($one_of_each_key) { |
|
| 2952 | - $extra_meta_objs = $this->get_many_related( |
|
| 2953 | - 'Extra_Meta', |
|
| 2954 | - array('group_by' => 'EXM_key') |
|
| 2955 | - ); |
|
| 2956 | - foreach ($extra_meta_objs as $extra_meta_obj) { |
|
| 2957 | - if ($extra_meta_obj instanceof EE_Extra_Meta) { |
|
| 2958 | - $return_array[ $extra_meta_obj->key() ] = $extra_meta_obj->value(); |
|
| 2959 | - } |
|
| 2960 | - } |
|
| 2961 | - } else { |
|
| 2962 | - $extra_meta_objs = $this->get_many_related('Extra_Meta'); |
|
| 2963 | - foreach ($extra_meta_objs as $extra_meta_obj) { |
|
| 2964 | - if ($extra_meta_obj instanceof EE_Extra_Meta) { |
|
| 2965 | - if (! isset($return_array[ $extra_meta_obj->key() ])) { |
|
| 2966 | - $return_array[ $extra_meta_obj->key() ] = array(); |
|
| 2967 | - } |
|
| 2968 | - $return_array[ $extra_meta_obj->key() ][ $extra_meta_obj->ID() ] = $extra_meta_obj->value(); |
|
| 2969 | - } |
|
| 2970 | - } |
|
| 2971 | - } |
|
| 2972 | - return $return_array; |
|
| 2973 | - } |
|
| 2974 | - |
|
| 2975 | - |
|
| 2976 | - /** |
|
| 2977 | - * Gets a pretty nice displayable nice for this model object. Often overridden |
|
| 2978 | - * |
|
| 2979 | - * @return string |
|
| 2980 | - * @throws ReflectionException |
|
| 2981 | - * @throws InvalidArgumentException |
|
| 2982 | - * @throws InvalidInterfaceException |
|
| 2983 | - * @throws InvalidDataTypeException |
|
| 2984 | - * @throws EE_Error |
|
| 2985 | - */ |
|
| 2986 | - public function name() |
|
| 2987 | - { |
|
| 2988 | - // find a field that's not a text field |
|
| 2989 | - $field_we_can_use = $this->get_model()->get_a_field_of_type('EE_Text_Field_Base'); |
|
| 2990 | - if ($field_we_can_use) { |
|
| 2991 | - return $this->get($field_we_can_use->get_name()); |
|
| 2992 | - } |
|
| 2993 | - $first_few_properties = $this->model_field_array(); |
|
| 2994 | - $first_few_properties = array_slice($first_few_properties, 0, 3); |
|
| 2995 | - $name_parts = array(); |
|
| 2996 | - foreach ($first_few_properties as $name => $value) { |
|
| 2997 | - $name_parts[] = "$name:$value"; |
|
| 2998 | - } |
|
| 2999 | - return implode(',', $name_parts); |
|
| 3000 | - } |
|
| 3001 | - |
|
| 3002 | - |
|
| 3003 | - /** |
|
| 3004 | - * in_entity_map |
|
| 3005 | - * Checks if this model object has been proven to already be in the entity map |
|
| 3006 | - * |
|
| 3007 | - * @return boolean |
|
| 3008 | - * @throws ReflectionException |
|
| 3009 | - * @throws InvalidArgumentException |
|
| 3010 | - * @throws InvalidInterfaceException |
|
| 3011 | - * @throws InvalidDataTypeException |
|
| 3012 | - * @throws EE_Error |
|
| 3013 | - */ |
|
| 3014 | - public function in_entity_map() |
|
| 3015 | - { |
|
| 3016 | - // well, if we looked, did we find it in the entity map? |
|
| 3017 | - return $this->ID() && $this->get_model()->get_from_entity_map($this->ID()) === $this; |
|
| 3018 | - } |
|
| 3019 | - |
|
| 3020 | - |
|
| 3021 | - /** |
|
| 3022 | - * refresh_from_db |
|
| 3023 | - * Makes sure the fields and values on this model object are in-sync with what's in the database. |
|
| 3024 | - * |
|
| 3025 | - * @throws ReflectionException |
|
| 3026 | - * @throws InvalidArgumentException |
|
| 3027 | - * @throws InvalidInterfaceException |
|
| 3028 | - * @throws InvalidDataTypeException |
|
| 3029 | - * @throws EE_Error if this model object isn't in the entity mapper (because then you should |
|
| 3030 | - * just use what's in the entity mapper and refresh it) and WP_DEBUG is TRUE |
|
| 3031 | - */ |
|
| 3032 | - public function refresh_from_db() |
|
| 3033 | - { |
|
| 3034 | - if ($this->ID() && $this->in_entity_map()) { |
|
| 3035 | - $this->get_model()->refresh_entity_map_from_db($this->ID()); |
|
| 3036 | - } else { |
|
| 3037 | - // if it doesn't have ID, you shouldn't be asking to refresh it from teh database (because its not in the database) |
|
| 3038 | - // if it has an ID but it's not in the map, and you're asking me to refresh it |
|
| 3039 | - // that's kinda dangerous. You should just use what's in the entity map, or add this to the entity map if there's |
|
| 3040 | - // absolutely nothing in it for this ID |
|
| 3041 | - if (WP_DEBUG) { |
|
| 3042 | - throw new EE_Error( |
|
| 3043 | - sprintf( |
|
| 3044 | - esc_html__( |
|
| 3045 | - 'Trying to refresh a model object with ID "%1$s" that\'s not in the entity map? First off: you should put it in the entity map by calling %2$s. Second off, if you want what\'s in the database right now, you should just call %3$s yourself and discard this model object.', |
|
| 3046 | - 'event_espresso' |
|
| 3047 | - ), |
|
| 3048 | - $this->ID(), |
|
| 3049 | - get_class($this->get_model()) . '::instance()->add_to_entity_map()', |
|
| 3050 | - get_class($this->get_model()) . '::instance()->refresh_entity_map()' |
|
| 3051 | - ) |
|
| 3052 | - ); |
|
| 3053 | - } |
|
| 3054 | - } |
|
| 3055 | - } |
|
| 3056 | - |
|
| 3057 | - |
|
| 3058 | - /** |
|
| 3059 | - * Because some other plugins, like Advanced Cron Manager, expect all objects to have this method |
|
| 3060 | - * (probably a bad assumption they have made, oh well) |
|
| 3061 | - * |
|
| 3062 | - * @return string |
|
| 3063 | - */ |
|
| 3064 | - public function __toString() |
|
| 3065 | - { |
|
| 3066 | - try { |
|
| 3067 | - return sprintf('%s (%s)', $this->name(), $this->ID()); |
|
| 3068 | - } catch (Exception $e) { |
|
| 3069 | - EE_Error::add_error($e->getMessage(), __FILE__, __FUNCTION__, __LINE__); |
|
| 3070 | - return ''; |
|
| 3071 | - } |
|
| 3072 | - } |
|
| 3073 | - |
|
| 3074 | - |
|
| 3075 | - /** |
|
| 3076 | - * Clear related model objects if they're already in the DB, because otherwise when we |
|
| 3077 | - * UN-serialize this model object we'll need to be careful to add them to the entity map. |
|
| 3078 | - * This means if we have made changes to those related model objects, and want to unserialize |
|
| 3079 | - * the this model object on a subsequent request, changes to those related model objects will be lost. |
|
| 3080 | - * Instead, those related model objects should be directly serialized and stored. |
|
| 3081 | - * Eg, the following won't work: |
|
| 3082 | - * $reg = EEM_Registration::instance()->get_one_by_ID( 123 ); |
|
| 3083 | - * $att = $reg->attendee(); |
|
| 3084 | - * $att->set( 'ATT_fname', 'Dirk' ); |
|
| 3085 | - * update_option( 'my_option', serialize( $reg ) ); |
|
| 3086 | - * //END REQUEST |
|
| 3087 | - * //START NEXT REQUEST |
|
| 3088 | - * $reg = get_option( 'my_option' ); |
|
| 3089 | - * $reg->attendee()->save(); |
|
| 3090 | - * And would need to be replace with: |
|
| 3091 | - * $reg = EEM_Registration::instance()->get_one_by_ID( 123 ); |
|
| 3092 | - * $att = $reg->attendee(); |
|
| 3093 | - * $att->set( 'ATT_fname', 'Dirk' ); |
|
| 3094 | - * update_option( 'my_option', serialize( $reg ) ); |
|
| 3095 | - * //END REQUEST |
|
| 3096 | - * //START NEXT REQUEST |
|
| 3097 | - * $att = get_option( 'my_option' ); |
|
| 3098 | - * $att->save(); |
|
| 3099 | - * |
|
| 3100 | - * @return array |
|
| 3101 | - * @throws ReflectionException |
|
| 3102 | - * @throws InvalidArgumentException |
|
| 3103 | - * @throws InvalidInterfaceException |
|
| 3104 | - * @throws InvalidDataTypeException |
|
| 3105 | - * @throws EE_Error |
|
| 3106 | - */ |
|
| 3107 | - public function __sleep() |
|
| 3108 | - { |
|
| 3109 | - $model = $this->get_model(); |
|
| 3110 | - foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
|
| 3111 | - if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
| 3112 | - $classname = 'EE_' . $model->get_this_model_name(); |
|
| 3113 | - if ($this->get_one_from_cache($relation_name) instanceof $classname |
|
| 3114 | - && $this->get_one_from_cache($relation_name)->ID() |
|
| 3115 | - ) { |
|
| 3116 | - $this->clear_cache( |
|
| 3117 | - $relation_name, |
|
| 3118 | - $this->get_one_from_cache($relation_name)->ID() |
|
| 3119 | - ); |
|
| 3120 | - } |
|
| 3121 | - } |
|
| 3122 | - } |
|
| 3123 | - $this->_props_n_values_provided_in_constructor = array(); |
|
| 3124 | - $properties_to_serialize = get_object_vars($this); |
|
| 3125 | - // don't serialize the model. It's big and that risks recursion |
|
| 3126 | - unset($properties_to_serialize['_model']); |
|
| 3127 | - return array_keys($properties_to_serialize); |
|
| 3128 | - } |
|
| 3129 | - |
|
| 3130 | - |
|
| 3131 | - /** |
|
| 3132 | - * restore _props_n_values_provided_in_constructor |
|
| 3133 | - * PLZ NOTE: this will reset the array to whatever fields values were present prior to serialization, |
|
| 3134 | - * and therefore should NOT be used to determine if state change has occurred since initial construction. |
|
| 3135 | - * At best, you would only be able to detect if state change has occurred during THIS request. |
|
| 3136 | - */ |
|
| 3137 | - public function __wakeup() |
|
| 3138 | - { |
|
| 3139 | - $this->_props_n_values_provided_in_constructor = $this->_fields; |
|
| 3140 | - } |
|
| 3141 | - |
|
| 3142 | - |
|
| 3143 | - /** |
|
| 3144 | - * Usage of this magic method is to ensure any internally cached references to object instances that must remain |
|
| 3145 | - * distinct with the clone host instance are also cloned. |
|
| 3146 | - */ |
|
| 3147 | - public function __clone() |
|
| 3148 | - { |
|
| 3149 | - // handle DateTimes (this is handled in here because there's no one specific child class that uses datetimes). |
|
| 3150 | - foreach ($this->_fields as $field => $value) { |
|
| 3151 | - if ($value instanceof DateTime) { |
|
| 3152 | - $this->_fields[ $field ] = clone $value; |
|
| 3153 | - } |
|
| 3154 | - } |
|
| 3155 | - } |
|
| 16 | + /** |
|
| 17 | + * This is an array of the original properties and values provided during construction |
|
| 18 | + * of this model object. (keys are model field names, values are their values). |
|
| 19 | + * This list is important to remember so that when we are merging data from the db, we know |
|
| 20 | + * which values to override and which to not override. |
|
| 21 | + * |
|
| 22 | + * @var array |
|
| 23 | + */ |
|
| 24 | + protected $_props_n_values_provided_in_constructor; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * Timezone |
|
| 28 | + * This gets set by the "set_timezone()" method so that we know what timezone incoming strings|timestamps are in. |
|
| 29 | + * This can also be used before a get to set what timezone you want strings coming out of the object to be in. NOT |
|
| 30 | + * all EE_Base_Class child classes use this property but any that use a EE_Datetime_Field data type will have |
|
| 31 | + * access to it. |
|
| 32 | + * |
|
| 33 | + * @var string |
|
| 34 | + */ |
|
| 35 | + protected $_timezone; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * date format |
|
| 39 | + * pattern or format for displaying dates |
|
| 40 | + * |
|
| 41 | + * @var string $_dt_frmt |
|
| 42 | + */ |
|
| 43 | + protected $_dt_frmt; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * time format |
|
| 47 | + * pattern or format for displaying time |
|
| 48 | + * |
|
| 49 | + * @var string $_tm_frmt |
|
| 50 | + */ |
|
| 51 | + protected $_tm_frmt; |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * This property is for holding a cached array of object properties indexed by property name as the key. |
|
| 55 | + * The purpose of this is for setting a cache on properties that may have calculated values after a |
|
| 56 | + * prepare_for_get. That way the cache can be checked first and the calculated property returned instead of having |
|
| 57 | + * to recalculate. Used by _set_cached_property() and _get_cached_property() methods. |
|
| 58 | + * |
|
| 59 | + * @var array |
|
| 60 | + */ |
|
| 61 | + protected $_cached_properties = array(); |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * An array containing keys of the related model, and values are either an array of related mode objects or a |
|
| 65 | + * single |
|
| 66 | + * related model object. see the model's _model_relations. The keys should match those specified. And if the |
|
| 67 | + * relation is of type EE_Belongs_To (or one of its children), then there should only be ONE related model object, |
|
| 68 | + * all others have an array) |
|
| 69 | + * |
|
| 70 | + * @var array |
|
| 71 | + */ |
|
| 72 | + protected $_model_relations = array(); |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * Array where keys are field names (see the model's _fields property) and values are their values. To see what |
|
| 76 | + * their types should be, look at what that field object returns on its prepare_for_get and prepare_for_set methods) |
|
| 77 | + * |
|
| 78 | + * @var array |
|
| 79 | + */ |
|
| 80 | + protected $_fields = array(); |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * @var boolean indicating whether or not this model object is intended to ever be saved |
|
| 84 | + * For example, we might create model objects intended to only be used for the duration |
|
| 85 | + * of this request and to be thrown away, and if they were accidentally saved |
|
| 86 | + * it would be a bug. |
|
| 87 | + */ |
|
| 88 | + protected $_allow_persist = true; |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * @var boolean indicating whether or not this model object's properties have changed since construction |
|
| 92 | + */ |
|
| 93 | + protected $_has_changes = false; |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * @var EEM_Base |
|
| 97 | + */ |
|
| 98 | + protected $_model; |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * This is a cache of results from custom selections done on a query that constructs this entity. The only purpose |
|
| 102 | + * for these values is for retrieval of the results, they are not further queryable and they are not persisted to |
|
| 103 | + * the db. They also do not automatically update if there are any changes to the data that produced their results. |
|
| 104 | + * The format is a simple array of field_alias => field_value. So for instance if a custom select was something |
|
| 105 | + * like, "Select COUNT(Registration.REG_ID) as Registration_Count ...", then the resulting value will be in this |
|
| 106 | + * array as: |
|
| 107 | + * array( |
|
| 108 | + * 'Registration_Count' => 24 |
|
| 109 | + * ); |
|
| 110 | + * Note: if the custom select configuration for the query included a data type, the value will be in the data type |
|
| 111 | + * provided for the query (@see EventEspresso\core\domain\values\model\CustomSelects::__construct phpdocs for more |
|
| 112 | + * info) |
|
| 113 | + * |
|
| 114 | + * @var array |
|
| 115 | + */ |
|
| 116 | + protected $custom_selection_results = array(); |
|
| 117 | + |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * basic constructor for Event Espresso classes, performs any necessary initialization, and verifies it's children |
|
| 121 | + * play nice |
|
| 122 | + * |
|
| 123 | + * @param array $fieldValues where each key is a field (ie, array key in the 2nd |
|
| 124 | + * layer of the model's _fields array, (eg, EVT_ID, |
|
| 125 | + * TXN_amount, QST_name, etc) and values are their values |
|
| 126 | + * @param boolean $bydb a flag for setting if the class is instantiated by the |
|
| 127 | + * corresponding db model or not. |
|
| 128 | + * @param string $timezone indicate what timezone you want any datetime fields to |
|
| 129 | + * be in when instantiating a EE_Base_Class object. |
|
| 130 | + * @param array $date_formats An array of date formats to set on construct where first |
|
| 131 | + * value is the date_format and second value is the time |
|
| 132 | + * format. |
|
| 133 | + * @throws InvalidArgumentException |
|
| 134 | + * @throws InvalidInterfaceException |
|
| 135 | + * @throws InvalidDataTypeException |
|
| 136 | + * @throws EE_Error |
|
| 137 | + * @throws ReflectionException |
|
| 138 | + */ |
|
| 139 | + protected function __construct($fieldValues = array(), $bydb = false, $timezone = '', $date_formats = array()) |
|
| 140 | + { |
|
| 141 | + $className = get_class($this); |
|
| 142 | + do_action("AHEE__{$className}__construct", $this, $fieldValues); |
|
| 143 | + $model = $this->get_model(); |
|
| 144 | + $model_fields = $model->field_settings(false); |
|
| 145 | + // ensure $fieldValues is an array |
|
| 146 | + $fieldValues = is_array($fieldValues) ? $fieldValues : array($fieldValues); |
|
| 147 | + // verify client code has not passed any invalid field names |
|
| 148 | + foreach ($fieldValues as $field_name => $field_value) { |
|
| 149 | + if (! isset($model_fields[ $field_name ])) { |
|
| 150 | + throw new EE_Error( |
|
| 151 | + sprintf( |
|
| 152 | + esc_html__( |
|
| 153 | + 'Invalid field (%s) passed to constructor of %s. Allowed fields are :%s', |
|
| 154 | + 'event_espresso' |
|
| 155 | + ), |
|
| 156 | + $field_name, |
|
| 157 | + get_class($this), |
|
| 158 | + implode(', ', array_keys($model_fields)) |
|
| 159 | + ) |
|
| 160 | + ); |
|
| 161 | + } |
|
| 162 | + } |
|
| 163 | + $this->_timezone = EEH_DTT_Helper::get_valid_timezone_string($timezone); |
|
| 164 | + if (! empty($date_formats) && is_array($date_formats)) { |
|
| 165 | + list($this->_dt_frmt, $this->_tm_frmt) = $date_formats; |
|
| 166 | + } else { |
|
| 167 | + // set default formats for date and time |
|
| 168 | + $this->_dt_frmt = (string) get_option('date_format', 'Y-m-d'); |
|
| 169 | + $this->_tm_frmt = (string) get_option('time_format', 'g:i a'); |
|
| 170 | + } |
|
| 171 | + // if db model is instantiating |
|
| 172 | + if ($bydb) { |
|
| 173 | + // client code has indicated these field values are from the database |
|
| 174 | + foreach ($model_fields as $fieldName => $field) { |
|
| 175 | + $this->set_from_db( |
|
| 176 | + $fieldName, |
|
| 177 | + isset($fieldValues[ $fieldName ]) ? $fieldValues[ $fieldName ] : null |
|
| 178 | + ); |
|
| 179 | + } |
|
| 180 | + } else { |
|
| 181 | + // we're constructing a brand |
|
| 182 | + // new instance of the model object. Generally, this means we'll need to do more field validation |
|
| 183 | + foreach ($model_fields as $fieldName => $field) { |
|
| 184 | + $this->set( |
|
| 185 | + $fieldName, |
|
| 186 | + isset($fieldValues[ $fieldName ]) ? $fieldValues[ $fieldName ] : null, |
|
| 187 | + true |
|
| 188 | + ); |
|
| 189 | + } |
|
| 190 | + } |
|
| 191 | + // remember what values were passed to this constructor |
|
| 192 | + $this->_props_n_values_provided_in_constructor = $fieldValues; |
|
| 193 | + // remember in entity mapper |
|
| 194 | + if (! $bydb && $model->has_primary_key_field() && $this->ID()) { |
|
| 195 | + $model->add_to_entity_map($this); |
|
| 196 | + } |
|
| 197 | + // setup all the relations |
|
| 198 | + foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
|
| 199 | + if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
| 200 | + $this->_model_relations[ $relation_name ] = null; |
|
| 201 | + } else { |
|
| 202 | + $this->_model_relations[ $relation_name ] = array(); |
|
| 203 | + } |
|
| 204 | + } |
|
| 205 | + /** |
|
| 206 | + * Action done at the end of each model object construction |
|
| 207 | + * |
|
| 208 | + * @param EE_Base_Class $this the model object just created |
|
| 209 | + */ |
|
| 210 | + do_action('AHEE__EE_Base_Class__construct__finished', $this); |
|
| 211 | + } |
|
| 212 | + |
|
| 213 | + |
|
| 214 | + /** |
|
| 215 | + * Gets whether or not this model object is allowed to persist/be saved to the database. |
|
| 216 | + * |
|
| 217 | + * @return boolean |
|
| 218 | + */ |
|
| 219 | + public function allow_persist() |
|
| 220 | + { |
|
| 221 | + return $this->_allow_persist; |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + |
|
| 225 | + /** |
|
| 226 | + * Sets whether or not this model object should be allowed to be saved to the DB. |
|
| 227 | + * Normally once this is set to FALSE you wouldn't set it back to TRUE, unless |
|
| 228 | + * you got new information that somehow made you change your mind. |
|
| 229 | + * |
|
| 230 | + * @param boolean $allow_persist |
|
| 231 | + * @return boolean |
|
| 232 | + */ |
|
| 233 | + public function set_allow_persist($allow_persist) |
|
| 234 | + { |
|
| 235 | + return $this->_allow_persist = $allow_persist; |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + |
|
| 239 | + /** |
|
| 240 | + * Gets the field's original value when this object was constructed during this request. |
|
| 241 | + * This can be helpful when determining if a model object has changed or not |
|
| 242 | + * |
|
| 243 | + * @param string $field_name |
|
| 244 | + * @return mixed|null |
|
| 245 | + * @throws ReflectionException |
|
| 246 | + * @throws InvalidArgumentException |
|
| 247 | + * @throws InvalidInterfaceException |
|
| 248 | + * @throws InvalidDataTypeException |
|
| 249 | + * @throws EE_Error |
|
| 250 | + */ |
|
| 251 | + public function get_original($field_name) |
|
| 252 | + { |
|
| 253 | + if (isset($this->_props_n_values_provided_in_constructor[ $field_name ]) |
|
| 254 | + && $field_settings = $this->get_model()->field_settings_for($field_name) |
|
| 255 | + ) { |
|
| 256 | + return $field_settings->prepare_for_get($this->_props_n_values_provided_in_constructor[ $field_name ]); |
|
| 257 | + } |
|
| 258 | + return null; |
|
| 259 | + } |
|
| 260 | + |
|
| 261 | + |
|
| 262 | + /** |
|
| 263 | + * @param EE_Base_Class $obj |
|
| 264 | + * @return string |
|
| 265 | + */ |
|
| 266 | + public function get_class($obj) |
|
| 267 | + { |
|
| 268 | + return get_class($obj); |
|
| 269 | + } |
|
| 270 | + |
|
| 271 | + |
|
| 272 | + /** |
|
| 273 | + * Overrides parent because parent expects old models. |
|
| 274 | + * This also doesn't do any validation, and won't work for serialized arrays |
|
| 275 | + * |
|
| 276 | + * @param string $field_name |
|
| 277 | + * @param mixed $field_value |
|
| 278 | + * @param bool $use_default |
|
| 279 | + * @throws InvalidArgumentException |
|
| 280 | + * @throws InvalidInterfaceException |
|
| 281 | + * @throws InvalidDataTypeException |
|
| 282 | + * @throws EE_Error |
|
| 283 | + * @throws ReflectionException |
|
| 284 | + * @throws ReflectionException |
|
| 285 | + * @throws ReflectionException |
|
| 286 | + */ |
|
| 287 | + public function set($field_name, $field_value, $use_default = false) |
|
| 288 | + { |
|
| 289 | + // if not using default and nothing has changed, and object has already been setup (has ID), |
|
| 290 | + // then don't do anything |
|
| 291 | + if (! $use_default |
|
| 292 | + && $this->_fields[ $field_name ] === $field_value |
|
| 293 | + && $this->ID() |
|
| 294 | + ) { |
|
| 295 | + return; |
|
| 296 | + } |
|
| 297 | + $model = $this->get_model(); |
|
| 298 | + $this->_has_changes = true; |
|
| 299 | + $field_obj = $model->field_settings_for($field_name); |
|
| 300 | + if ($field_obj instanceof EE_Model_Field_Base) { |
|
| 301 | + // if ( method_exists( $field_obj, 'set_timezone' )) { |
|
| 302 | + if ($field_obj instanceof EE_Datetime_Field) { |
|
| 303 | + $field_obj->set_timezone($this->_timezone); |
|
| 304 | + $field_obj->set_date_format($this->_dt_frmt); |
|
| 305 | + $field_obj->set_time_format($this->_tm_frmt); |
|
| 306 | + } |
|
| 307 | + $holder_of_value = $field_obj->prepare_for_set($field_value); |
|
| 308 | + // should the value be null? |
|
| 309 | + if (($field_value === null || $holder_of_value === null || $holder_of_value === '') && $use_default) { |
|
| 310 | + $this->_fields[ $field_name ] = $field_obj->get_default_value(); |
|
| 311 | + /** |
|
| 312 | + * To save having to refactor all the models, if a default value is used for a |
|
| 313 | + * EE_Datetime_Field, and that value is not null nor is it a DateTime |
|
| 314 | + * object. Then let's do a set again to ensure that it becomes a DateTime |
|
| 315 | + * object. |
|
| 316 | + * |
|
| 317 | + * @since 4.6.10+ |
|
| 318 | + */ |
|
| 319 | + if ($field_obj instanceof EE_Datetime_Field |
|
| 320 | + && $this->_fields[ $field_name ] !== null |
|
| 321 | + && ! $this->_fields[ $field_name ] instanceof DateTime |
|
| 322 | + ) { |
|
| 323 | + empty($this->_fields[ $field_name ]) |
|
| 324 | + ? $this->set($field_name, time()) |
|
| 325 | + : $this->set($field_name, $this->_fields[ $field_name ]); |
|
| 326 | + } |
|
| 327 | + } else { |
|
| 328 | + $this->_fields[ $field_name ] = $holder_of_value; |
|
| 329 | + } |
|
| 330 | + // if we're not in the constructor... |
|
| 331 | + // now check if what we set was a primary key |
|
| 332 | + if (// note: props_n_values_provided_in_constructor is only set at the END of the constructor |
|
| 333 | + $this->_props_n_values_provided_in_constructor |
|
| 334 | + && $field_value |
|
| 335 | + && $field_name === $model->primary_key_name() |
|
| 336 | + ) { |
|
| 337 | + // if so, we want all this object's fields to be filled either with |
|
| 338 | + // what we've explicitly set on this model |
|
| 339 | + // or what we have in the db |
|
| 340 | + // echo "setting primary key!"; |
|
| 341 | + $fields_on_model = self::_get_model(get_class($this))->field_settings(); |
|
| 342 | + $obj_in_db = self::_get_model(get_class($this))->get_one_by_ID($field_value); |
|
| 343 | + foreach ($fields_on_model as $field_obj) { |
|
| 344 | + if (! array_key_exists($field_obj->get_name(), $this->_props_n_values_provided_in_constructor) |
|
| 345 | + && $field_obj->get_name() !== $field_name |
|
| 346 | + ) { |
|
| 347 | + $this->set($field_obj->get_name(), $obj_in_db->get($field_obj->get_name())); |
|
| 348 | + } |
|
| 349 | + } |
|
| 350 | + // oh this model object has an ID? well make sure its in the entity mapper |
|
| 351 | + $model->add_to_entity_map($this); |
|
| 352 | + } |
|
| 353 | + // let's unset any cache for this field_name from the $_cached_properties property. |
|
| 354 | + $this->_clear_cached_property($field_name); |
|
| 355 | + } else { |
|
| 356 | + throw new EE_Error( |
|
| 357 | + sprintf( |
|
| 358 | + esc_html__( |
|
| 359 | + 'A valid EE_Model_Field_Base could not be found for the given field name: %s', |
|
| 360 | + 'event_espresso' |
|
| 361 | + ), |
|
| 362 | + $field_name |
|
| 363 | + ) |
|
| 364 | + ); |
|
| 365 | + } |
|
| 366 | + } |
|
| 367 | + |
|
| 368 | + |
|
| 369 | + /** |
|
| 370 | + * Set custom select values for model. |
|
| 371 | + * |
|
| 372 | + * @param array $custom_select_values |
|
| 373 | + */ |
|
| 374 | + public function setCustomSelectsValues(array $custom_select_values) |
|
| 375 | + { |
|
| 376 | + $this->custom_selection_results = $custom_select_values; |
|
| 377 | + } |
|
| 378 | + |
|
| 379 | + |
|
| 380 | + /** |
|
| 381 | + * Returns the custom select value for the provided alias if its set. |
|
| 382 | + * If not set, returns null. |
|
| 383 | + * |
|
| 384 | + * @param string $alias |
|
| 385 | + * @return string|int|float|null |
|
| 386 | + */ |
|
| 387 | + public function getCustomSelect($alias) |
|
| 388 | + { |
|
| 389 | + return isset($this->custom_selection_results[ $alias ]) |
|
| 390 | + ? $this->custom_selection_results[ $alias ] |
|
| 391 | + : null; |
|
| 392 | + } |
|
| 393 | + |
|
| 394 | + |
|
| 395 | + /** |
|
| 396 | + * This sets the field value on the db column if it exists for the given $column_name or |
|
| 397 | + * saves it to EE_Extra_Meta if the given $column_name does not match a db column. |
|
| 398 | + * |
|
| 399 | + * @see EE_message::get_column_value for related documentation on the necessity of this method. |
|
| 400 | + * @param string $field_name Must be the exact column name. |
|
| 401 | + * @param mixed $field_value The value to set. |
|
| 402 | + * @return int|bool @see EE_Base_Class::update_extra_meta() for return docs. |
|
| 403 | + * @throws InvalidArgumentException |
|
| 404 | + * @throws InvalidInterfaceException |
|
| 405 | + * @throws InvalidDataTypeException |
|
| 406 | + * @throws EE_Error |
|
| 407 | + * @throws ReflectionException |
|
| 408 | + */ |
|
| 409 | + public function set_field_or_extra_meta($field_name, $field_value) |
|
| 410 | + { |
|
| 411 | + if ($this->get_model()->has_field($field_name)) { |
|
| 412 | + $this->set($field_name, $field_value); |
|
| 413 | + return true; |
|
| 414 | + } |
|
| 415 | + // ensure this object is saved first so that extra meta can be properly related. |
|
| 416 | + $this->save(); |
|
| 417 | + return $this->update_extra_meta($field_name, $field_value); |
|
| 418 | + } |
|
| 419 | + |
|
| 420 | + |
|
| 421 | + /** |
|
| 422 | + * This retrieves the value of the db column set on this class or if that's not present |
|
| 423 | + * it will attempt to retrieve from extra_meta if found. |
|
| 424 | + * Example Usage: |
|
| 425 | + * Via EE_Message child class: |
|
| 426 | + * Due to the dynamic nature of the EE_messages system, EE_messengers will always have a "to", |
|
| 427 | + * "from", "subject", and "content" field (as represented in the EE_Message schema), however they may |
|
| 428 | + * also have additional main fields specific to the messenger. The system accommodates those extra |
|
| 429 | + * fields through the EE_Extra_Meta table. This method allows for EE_messengers to retrieve the |
|
| 430 | + * value for those extra fields dynamically via the EE_message object. |
|
| 431 | + * |
|
| 432 | + * @param string $field_name expecting the fully qualified field name. |
|
| 433 | + * @return mixed|null value for the field if found. null if not found. |
|
| 434 | + * @throws ReflectionException |
|
| 435 | + * @throws InvalidArgumentException |
|
| 436 | + * @throws InvalidInterfaceException |
|
| 437 | + * @throws InvalidDataTypeException |
|
| 438 | + * @throws EE_Error |
|
| 439 | + */ |
|
| 440 | + public function get_field_or_extra_meta($field_name) |
|
| 441 | + { |
|
| 442 | + if ($this->get_model()->has_field($field_name)) { |
|
| 443 | + $column_value = $this->get($field_name); |
|
| 444 | + } else { |
|
| 445 | + // This isn't a column in the main table, let's see if it is in the extra meta. |
|
| 446 | + $column_value = $this->get_extra_meta($field_name, true, null); |
|
| 447 | + } |
|
| 448 | + return $column_value; |
|
| 449 | + } |
|
| 450 | + |
|
| 451 | + |
|
| 452 | + /** |
|
| 453 | + * See $_timezone property for description of what the timezone property is for. This SETS the timezone internally |
|
| 454 | + * for being able to reference what timezone we are running conversions on when converting TO the internal timezone |
|
| 455 | + * (UTC Unix Timestamp) for the object OR when converting FROM the internal timezone (UTC Unix Timestamp). This is |
|
| 456 | + * available to all child classes that may be using the EE_Datetime_Field for a field data type. |
|
| 457 | + * |
|
| 458 | + * @access public |
|
| 459 | + * @param string $timezone A valid timezone string as described by @link http://www.php.net/manual/en/timezones.php |
|
| 460 | + * @return void |
|
| 461 | + * @throws InvalidArgumentException |
|
| 462 | + * @throws InvalidInterfaceException |
|
| 463 | + * @throws InvalidDataTypeException |
|
| 464 | + * @throws EE_Error |
|
| 465 | + * @throws ReflectionException |
|
| 466 | + */ |
|
| 467 | + public function set_timezone($timezone = '') |
|
| 468 | + { |
|
| 469 | + $this->_timezone = EEH_DTT_Helper::get_valid_timezone_string($timezone); |
|
| 470 | + // make sure we clear all cached properties because they won't be relevant now |
|
| 471 | + $this->_clear_cached_properties(); |
|
| 472 | + // make sure we update field settings and the date for all EE_Datetime_Fields |
|
| 473 | + $model_fields = $this->get_model()->field_settings(false); |
|
| 474 | + foreach ($model_fields as $field_name => $field_obj) { |
|
| 475 | + if ($field_obj instanceof EE_Datetime_Field) { |
|
| 476 | + $field_obj->set_timezone($this->_timezone); |
|
| 477 | + if (isset($this->_fields[ $field_name ]) && $this->_fields[ $field_name ] instanceof DateTime) { |
|
| 478 | + EEH_DTT_Helper::setTimezone($this->_fields[ $field_name ], new DateTimeZone($this->_timezone)); |
|
| 479 | + } |
|
| 480 | + } |
|
| 481 | + } |
|
| 482 | + } |
|
| 483 | + |
|
| 484 | + |
|
| 485 | + /** |
|
| 486 | + * This just returns whatever is set for the current timezone. |
|
| 487 | + * |
|
| 488 | + * @access public |
|
| 489 | + * @return string timezone string |
|
| 490 | + */ |
|
| 491 | + public function get_timezone() |
|
| 492 | + { |
|
| 493 | + return $this->_timezone; |
|
| 494 | + } |
|
| 495 | + |
|
| 496 | + |
|
| 497 | + /** |
|
| 498 | + * This sets the internal date format to what is sent in to be used as the new default for the class |
|
| 499 | + * internally instead of wp set date format options |
|
| 500 | + * |
|
| 501 | + * @since 4.6 |
|
| 502 | + * @param string $format should be a format recognizable by PHP date() functions. |
|
| 503 | + */ |
|
| 504 | + public function set_date_format($format) |
|
| 505 | + { |
|
| 506 | + $this->_dt_frmt = $format; |
|
| 507 | + // clear cached_properties because they won't be relevant now. |
|
| 508 | + $this->_clear_cached_properties(); |
|
| 509 | + } |
|
| 510 | + |
|
| 511 | + |
|
| 512 | + /** |
|
| 513 | + * This sets the internal time format string to what is sent in to be used as the new default for the |
|
| 514 | + * class internally instead of wp set time format options. |
|
| 515 | + * |
|
| 516 | + * @since 4.6 |
|
| 517 | + * @param string $format should be a format recognizable by PHP date() functions. |
|
| 518 | + */ |
|
| 519 | + public function set_time_format($format) |
|
| 520 | + { |
|
| 521 | + $this->_tm_frmt = $format; |
|
| 522 | + // clear cached_properties because they won't be relevant now. |
|
| 523 | + $this->_clear_cached_properties(); |
|
| 524 | + } |
|
| 525 | + |
|
| 526 | + |
|
| 527 | + /** |
|
| 528 | + * This returns the current internal set format for the date and time formats. |
|
| 529 | + * |
|
| 530 | + * @param bool $full if true (default), then return the full format. Otherwise will return an array |
|
| 531 | + * where the first value is the date format and the second value is the time format. |
|
| 532 | + * @return mixed string|array |
|
| 533 | + */ |
|
| 534 | + public function get_format($full = true) |
|
| 535 | + { |
|
| 536 | + return $full ? $this->_dt_frmt . ' ' . $this->_tm_frmt : array($this->_dt_frmt, $this->_tm_frmt); |
|
| 537 | + } |
|
| 538 | + |
|
| 539 | + |
|
| 540 | + /** |
|
| 541 | + * cache |
|
| 542 | + * stores the passed model object on the current model object. |
|
| 543 | + * In certain circumstances, we can use this cached model object instead of querying for another one entirely. |
|
| 544 | + * |
|
| 545 | + * @param string $relationName one of the keys in the _model_relations array on the model. Eg |
|
| 546 | + * 'Registration' associated with this model object |
|
| 547 | + * @param EE_Base_Class $object_to_cache that has a relation to this model object. (Eg, if this is a Transaction, |
|
| 548 | + * that could be a payment or a registration) |
|
| 549 | + * @param null $cache_id a string or number that will be used as the key for any Belongs_To_Many |
|
| 550 | + * items which will be stored in an array on this object |
|
| 551 | + * @throws ReflectionException |
|
| 552 | + * @throws InvalidArgumentException |
|
| 553 | + * @throws InvalidInterfaceException |
|
| 554 | + * @throws InvalidDataTypeException |
|
| 555 | + * @throws EE_Error |
|
| 556 | + * @return mixed index into cache, or just TRUE if the relation is of type Belongs_To (because there's only one |
|
| 557 | + * related thing, no array) |
|
| 558 | + */ |
|
| 559 | + public function cache($relationName = '', $object_to_cache = null, $cache_id = null) |
|
| 560 | + { |
|
| 561 | + // its entirely possible that there IS no related object yet in which case there is nothing to cache. |
|
| 562 | + if (! $object_to_cache instanceof EE_Base_Class) { |
|
| 563 | + return false; |
|
| 564 | + } |
|
| 565 | + // also get "how" the object is related, or throw an error |
|
| 566 | + if (! $relationship_to_model = $this->get_model()->related_settings_for($relationName)) { |
|
| 567 | + throw new EE_Error( |
|
| 568 | + sprintf( |
|
| 569 | + esc_html__('There is no relationship to %s on a %s. Cannot cache it', 'event_espresso'), |
|
| 570 | + $relationName, |
|
| 571 | + get_class($this) |
|
| 572 | + ) |
|
| 573 | + ); |
|
| 574 | + } |
|
| 575 | + // how many things are related ? |
|
| 576 | + if ($relationship_to_model instanceof EE_Belongs_To_Relation) { |
|
| 577 | + // if it's a "belongs to" relationship, then there's only one related model object |
|
| 578 | + // eg, if this is a registration, there's only 1 attendee for it |
|
| 579 | + // so for these model objects just set it to be cached |
|
| 580 | + $this->_model_relations[ $relationName ] = $object_to_cache; |
|
| 581 | + $return = true; |
|
| 582 | + } else { |
|
| 583 | + // otherwise, this is the "many" side of a one to many relationship, |
|
| 584 | + // so we'll add the object to the array of related objects for that type. |
|
| 585 | + // eg: if this is an event, there are many registrations for that event, |
|
| 586 | + // so we cache the registrations in an array |
|
| 587 | + if (! is_array($this->_model_relations[ $relationName ])) { |
|
| 588 | + // if for some reason, the cached item is a model object, |
|
| 589 | + // then stick that in the array, otherwise start with an empty array |
|
| 590 | + $this->_model_relations[ $relationName ] = $this->_model_relations[ $relationName ] |
|
| 591 | + instanceof |
|
| 592 | + EE_Base_Class |
|
| 593 | + ? array($this->_model_relations[ $relationName ]) : array(); |
|
| 594 | + } |
|
| 595 | + // first check for a cache_id which is normally empty |
|
| 596 | + if (! empty($cache_id)) { |
|
| 597 | + // if the cache_id exists, then it means we are purposely trying to cache this |
|
| 598 | + // with a known key that can then be used to retrieve the object later on |
|
| 599 | + $this->_model_relations[ $relationName ][ $cache_id ] = $object_to_cache; |
|
| 600 | + $return = $cache_id; |
|
| 601 | + } elseif ($object_to_cache->ID()) { |
|
| 602 | + // OR the cached object originally came from the db, so let's just use it's PK for an ID |
|
| 603 | + $this->_model_relations[ $relationName ][ $object_to_cache->ID() ] = $object_to_cache; |
|
| 604 | + $return = $object_to_cache->ID(); |
|
| 605 | + } else { |
|
| 606 | + // OR it's a new object with no ID, so just throw it in the array with an auto-incremented ID |
|
| 607 | + $this->_model_relations[ $relationName ][] = $object_to_cache; |
|
| 608 | + // move the internal pointer to the end of the array |
|
| 609 | + end($this->_model_relations[ $relationName ]); |
|
| 610 | + // and grab the key so that we can return it |
|
| 611 | + $return = key($this->_model_relations[ $relationName ]); |
|
| 612 | + } |
|
| 613 | + } |
|
| 614 | + return $return; |
|
| 615 | + } |
|
| 616 | + |
|
| 617 | + |
|
| 618 | + /** |
|
| 619 | + * For adding an item to the cached_properties property. |
|
| 620 | + * |
|
| 621 | + * @access protected |
|
| 622 | + * @param string $fieldname the property item the corresponding value is for. |
|
| 623 | + * @param mixed $value The value we are caching. |
|
| 624 | + * @param string|null $cache_type |
|
| 625 | + * @return void |
|
| 626 | + * @throws ReflectionException |
|
| 627 | + * @throws InvalidArgumentException |
|
| 628 | + * @throws InvalidInterfaceException |
|
| 629 | + * @throws InvalidDataTypeException |
|
| 630 | + * @throws EE_Error |
|
| 631 | + */ |
|
| 632 | + protected function _set_cached_property($fieldname, $value, $cache_type = null) |
|
| 633 | + { |
|
| 634 | + // first make sure this property exists |
|
| 635 | + $this->get_model()->field_settings_for($fieldname); |
|
| 636 | + $cache_type = empty($cache_type) ? 'standard' : $cache_type; |
|
| 637 | + $this->_cached_properties[ $fieldname ][ $cache_type ] = $value; |
|
| 638 | + } |
|
| 639 | + |
|
| 640 | + |
|
| 641 | + /** |
|
| 642 | + * This returns the value cached property if it exists OR the actual property value if the cache doesn't exist. |
|
| 643 | + * This also SETS the cache if we return the actual property! |
|
| 644 | + * |
|
| 645 | + * @param string $fieldname the name of the property we're trying to retrieve |
|
| 646 | + * @param bool $pretty |
|
| 647 | + * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
|
| 648 | + * (in cases where the same property may be used for different outputs |
|
| 649 | + * - i.e. datetime, money etc.) |
|
| 650 | + * It can also accept certain pre-defined "schema" strings |
|
| 651 | + * to define how to output the property. |
|
| 652 | + * see the field's prepare_for_pretty_echoing for what strings can be used |
|
| 653 | + * @return mixed whatever the value for the property is we're retrieving |
|
| 654 | + * @throws ReflectionException |
|
| 655 | + * @throws InvalidArgumentException |
|
| 656 | + * @throws InvalidInterfaceException |
|
| 657 | + * @throws InvalidDataTypeException |
|
| 658 | + * @throws EE_Error |
|
| 659 | + */ |
|
| 660 | + protected function _get_cached_property($fieldname, $pretty = false, $extra_cache_ref = null) |
|
| 661 | + { |
|
| 662 | + // verify the field exists |
|
| 663 | + $model = $this->get_model(); |
|
| 664 | + $model->field_settings_for($fieldname); |
|
| 665 | + $cache_type = $pretty ? 'pretty' : 'standard'; |
|
| 666 | + $cache_type .= ! empty($extra_cache_ref) ? '_' . $extra_cache_ref : ''; |
|
| 667 | + if (isset($this->_cached_properties[ $fieldname ][ $cache_type ])) { |
|
| 668 | + return $this->_cached_properties[ $fieldname ][ $cache_type ]; |
|
| 669 | + } |
|
| 670 | + $value = $this->_get_fresh_property($fieldname, $pretty, $extra_cache_ref); |
|
| 671 | + $this->_set_cached_property($fieldname, $value, $cache_type); |
|
| 672 | + return $value; |
|
| 673 | + } |
|
| 674 | + |
|
| 675 | + |
|
| 676 | + /** |
|
| 677 | + * If the cache didn't fetch the needed item, this fetches it. |
|
| 678 | + * |
|
| 679 | + * @param string $fieldname |
|
| 680 | + * @param bool $pretty |
|
| 681 | + * @param string $extra_cache_ref |
|
| 682 | + * @return mixed |
|
| 683 | + * @throws InvalidArgumentException |
|
| 684 | + * @throws InvalidInterfaceException |
|
| 685 | + * @throws InvalidDataTypeException |
|
| 686 | + * @throws EE_Error |
|
| 687 | + * @throws ReflectionException |
|
| 688 | + */ |
|
| 689 | + protected function _get_fresh_property($fieldname, $pretty = false, $extra_cache_ref = null) |
|
| 690 | + { |
|
| 691 | + $field_obj = $this->get_model()->field_settings_for($fieldname); |
|
| 692 | + // If this is an EE_Datetime_Field we need to make sure timezone, formats, and output are correct |
|
| 693 | + if ($field_obj instanceof EE_Datetime_Field) { |
|
| 694 | + $this->_prepare_datetime_field($field_obj, $pretty, $extra_cache_ref); |
|
| 695 | + } |
|
| 696 | + if (! isset($this->_fields[ $fieldname ])) { |
|
| 697 | + $this->_fields[ $fieldname ] = null; |
|
| 698 | + } |
|
| 699 | + $value = $pretty |
|
| 700 | + ? $field_obj->prepare_for_pretty_echoing($this->_fields[ $fieldname ], $extra_cache_ref) |
|
| 701 | + : $field_obj->prepare_for_get($this->_fields[ $fieldname ]); |
|
| 702 | + return $value; |
|
| 703 | + } |
|
| 704 | + |
|
| 705 | + |
|
| 706 | + /** |
|
| 707 | + * set timezone, formats, and output for EE_Datetime_Field objects |
|
| 708 | + * |
|
| 709 | + * @param \EE_Datetime_Field $datetime_field |
|
| 710 | + * @param bool $pretty |
|
| 711 | + * @param null $date_or_time |
|
| 712 | + * @return void |
|
| 713 | + * @throws InvalidArgumentException |
|
| 714 | + * @throws InvalidInterfaceException |
|
| 715 | + * @throws InvalidDataTypeException |
|
| 716 | + * @throws EE_Error |
|
| 717 | + */ |
|
| 718 | + protected function _prepare_datetime_field( |
|
| 719 | + EE_Datetime_Field $datetime_field, |
|
| 720 | + $pretty = false, |
|
| 721 | + $date_or_time = null |
|
| 722 | + ) { |
|
| 723 | + $datetime_field->set_timezone($this->_timezone); |
|
| 724 | + $datetime_field->set_date_format($this->_dt_frmt, $pretty); |
|
| 725 | + $datetime_field->set_time_format($this->_tm_frmt, $pretty); |
|
| 726 | + // set the output returned |
|
| 727 | + switch ($date_or_time) { |
|
| 728 | + case 'D': |
|
| 729 | + $datetime_field->set_date_time_output('date'); |
|
| 730 | + break; |
|
| 731 | + case 'T': |
|
| 732 | + $datetime_field->set_date_time_output('time'); |
|
| 733 | + break; |
|
| 734 | + default: |
|
| 735 | + $datetime_field->set_date_time_output(); |
|
| 736 | + } |
|
| 737 | + } |
|
| 738 | + |
|
| 739 | + |
|
| 740 | + /** |
|
| 741 | + * This just takes care of clearing out the cached_properties |
|
| 742 | + * |
|
| 743 | + * @return void |
|
| 744 | + */ |
|
| 745 | + protected function _clear_cached_properties() |
|
| 746 | + { |
|
| 747 | + $this->_cached_properties = array(); |
|
| 748 | + } |
|
| 749 | + |
|
| 750 | + |
|
| 751 | + /** |
|
| 752 | + * This just clears out ONE property if it exists in the cache |
|
| 753 | + * |
|
| 754 | + * @param string $property_name the property to remove if it exists (from the _cached_properties array) |
|
| 755 | + * @return void |
|
| 756 | + */ |
|
| 757 | + protected function _clear_cached_property($property_name) |
|
| 758 | + { |
|
| 759 | + if (isset($this->_cached_properties[ $property_name ])) { |
|
| 760 | + unset($this->_cached_properties[ $property_name ]); |
|
| 761 | + } |
|
| 762 | + } |
|
| 763 | + |
|
| 764 | + |
|
| 765 | + /** |
|
| 766 | + * Ensures that this related thing is a model object. |
|
| 767 | + * |
|
| 768 | + * @param mixed $object_or_id EE_base_Class/int/string either a related model object, or its ID |
|
| 769 | + * @param string $model_name name of the related thing, eg 'Attendee', |
|
| 770 | + * @return EE_Base_Class |
|
| 771 | + * @throws ReflectionException |
|
| 772 | + * @throws InvalidArgumentException |
|
| 773 | + * @throws InvalidInterfaceException |
|
| 774 | + * @throws InvalidDataTypeException |
|
| 775 | + * @throws EE_Error |
|
| 776 | + */ |
|
| 777 | + protected function ensure_related_thing_is_model_obj($object_or_id, $model_name) |
|
| 778 | + { |
|
| 779 | + $other_model_instance = self::_get_model_instance_with_name( |
|
| 780 | + self::_get_model_classname($model_name), |
|
| 781 | + $this->_timezone |
|
| 782 | + ); |
|
| 783 | + return $other_model_instance->ensure_is_obj($object_or_id); |
|
| 784 | + } |
|
| 785 | + |
|
| 786 | + |
|
| 787 | + /** |
|
| 788 | + * Forgets the cached model of the given relation Name. So the next time we request it, |
|
| 789 | + * we will fetch it again from the database. (Handy if you know it's changed somehow). |
|
| 790 | + * If a specific object is supplied, and the relationship to it is either a HasMany or HABTM, |
|
| 791 | + * then only remove that one object from our cached array. Otherwise, clear the entire list |
|
| 792 | + * |
|
| 793 | + * @param string $relationName one of the keys in the _model_relations array on the model. |
|
| 794 | + * Eg 'Registration' |
|
| 795 | + * @param mixed $object_to_remove_or_index_into_array or an index into the array of cached things, or NULL |
|
| 796 | + * if you intend to use $clear_all = TRUE, or the relation only |
|
| 797 | + * has 1 object anyways (ie, it's a BelongsToRelation) |
|
| 798 | + * @param bool $clear_all This flags clearing the entire cache relation property if |
|
| 799 | + * this is HasMany or HABTM. |
|
| 800 | + * @throws ReflectionException |
|
| 801 | + * @throws InvalidArgumentException |
|
| 802 | + * @throws InvalidInterfaceException |
|
| 803 | + * @throws InvalidDataTypeException |
|
| 804 | + * @throws EE_Error |
|
| 805 | + * @return EE_Base_Class | boolean from which was cleared from the cache, or true if we requested to remove a |
|
| 806 | + * relation from all |
|
| 807 | + */ |
|
| 808 | + public function clear_cache($relationName, $object_to_remove_or_index_into_array = null, $clear_all = false) |
|
| 809 | + { |
|
| 810 | + $relationship_to_model = $this->get_model()->related_settings_for($relationName); |
|
| 811 | + $index_in_cache = ''; |
|
| 812 | + if (! $relationship_to_model) { |
|
| 813 | + throw new EE_Error( |
|
| 814 | + sprintf( |
|
| 815 | + esc_html__('There is no relationship to %s on a %s. Cannot clear that cache', 'event_espresso'), |
|
| 816 | + $relationName, |
|
| 817 | + get_class($this) |
|
| 818 | + ) |
|
| 819 | + ); |
|
| 820 | + } |
|
| 821 | + if ($clear_all) { |
|
| 822 | + $obj_removed = true; |
|
| 823 | + $this->_model_relations[ $relationName ] = null; |
|
| 824 | + } elseif ($relationship_to_model instanceof EE_Belongs_To_Relation) { |
|
| 825 | + $obj_removed = $this->_model_relations[ $relationName ]; |
|
| 826 | + $this->_model_relations[ $relationName ] = null; |
|
| 827 | + } else { |
|
| 828 | + if ($object_to_remove_or_index_into_array instanceof EE_Base_Class |
|
| 829 | + && $object_to_remove_or_index_into_array->ID() |
|
| 830 | + ) { |
|
| 831 | + $index_in_cache = $object_to_remove_or_index_into_array->ID(); |
|
| 832 | + if (is_array($this->_model_relations[ $relationName ]) |
|
| 833 | + && ! isset($this->_model_relations[ $relationName ][ $index_in_cache ]) |
|
| 834 | + ) { |
|
| 835 | + $index_found_at = null; |
|
| 836 | + // find this object in the array even though it has a different key |
|
| 837 | + foreach ($this->_model_relations[ $relationName ] as $index => $obj) { |
|
| 838 | + /** @noinspection TypeUnsafeComparisonInspection */ |
|
| 839 | + if ($obj instanceof EE_Base_Class |
|
| 840 | + && ( |
|
| 841 | + $obj == $object_to_remove_or_index_into_array |
|
| 842 | + || $obj->ID() === $object_to_remove_or_index_into_array->ID() |
|
| 843 | + ) |
|
| 844 | + ) { |
|
| 845 | + $index_found_at = $index; |
|
| 846 | + break; |
|
| 847 | + } |
|
| 848 | + } |
|
| 849 | + if ($index_found_at) { |
|
| 850 | + $index_in_cache = $index_found_at; |
|
| 851 | + } else { |
|
| 852 | + // it wasn't found. huh. well obviously it doesn't need to be removed from teh cache |
|
| 853 | + // if it wasn't in it to begin with. So we're done |
|
| 854 | + return $object_to_remove_or_index_into_array; |
|
| 855 | + } |
|
| 856 | + } |
|
| 857 | + } elseif ($object_to_remove_or_index_into_array instanceof EE_Base_Class) { |
|
| 858 | + // so they provided a model object, but it's not yet saved to the DB... so let's go hunting for it! |
|
| 859 | + foreach ($this->get_all_from_cache($relationName) as $index => $potentially_obj_we_want) { |
|
| 860 | + /** @noinspection TypeUnsafeComparisonInspection */ |
|
| 861 | + if ($potentially_obj_we_want == $object_to_remove_or_index_into_array) { |
|
| 862 | + $index_in_cache = $index; |
|
| 863 | + } |
|
| 864 | + } |
|
| 865 | + } else { |
|
| 866 | + $index_in_cache = $object_to_remove_or_index_into_array; |
|
| 867 | + } |
|
| 868 | + // supposedly we've found it. But it could just be that the client code |
|
| 869 | + // provided a bad index/object |
|
| 870 | + if (isset($this->_model_relations[ $relationName ][ $index_in_cache ])) { |
|
| 871 | + $obj_removed = $this->_model_relations[ $relationName ][ $index_in_cache ]; |
|
| 872 | + unset($this->_model_relations[ $relationName ][ $index_in_cache ]); |
|
| 873 | + } else { |
|
| 874 | + // that thing was never cached anyways. |
|
| 875 | + $obj_removed = null; |
|
| 876 | + } |
|
| 877 | + } |
|
| 878 | + return $obj_removed; |
|
| 879 | + } |
|
| 880 | + |
|
| 881 | + |
|
| 882 | + /** |
|
| 883 | + * update_cache_after_object_save |
|
| 884 | + * Allows a cached item to have it's cache ID (within the array of cached items) reset using the new ID it has |
|
| 885 | + * obtained after being saved to the db |
|
| 886 | + * |
|
| 887 | + * @param string $relationName - the type of object that is cached |
|
| 888 | + * @param EE_Base_Class $newly_saved_object - the newly saved object to be re-cached |
|
| 889 | + * @param string $current_cache_id - the ID that was used when originally caching the object |
|
| 890 | + * @return boolean TRUE on success, FALSE on fail |
|
| 891 | + * @throws ReflectionException |
|
| 892 | + * @throws InvalidArgumentException |
|
| 893 | + * @throws InvalidInterfaceException |
|
| 894 | + * @throws InvalidDataTypeException |
|
| 895 | + * @throws EE_Error |
|
| 896 | + */ |
|
| 897 | + public function update_cache_after_object_save( |
|
| 898 | + $relationName, |
|
| 899 | + EE_Base_Class $newly_saved_object, |
|
| 900 | + $current_cache_id = '' |
|
| 901 | + ) { |
|
| 902 | + // verify that incoming object is of the correct type |
|
| 903 | + $obj_class = 'EE_' . $relationName; |
|
| 904 | + if ($newly_saved_object instanceof $obj_class) { |
|
| 905 | + /* @type EE_Base_Class $newly_saved_object */ |
|
| 906 | + // now get the type of relation |
|
| 907 | + $relationship_to_model = $this->get_model()->related_settings_for($relationName); |
|
| 908 | + // if this is a 1:1 relationship |
|
| 909 | + if ($relationship_to_model instanceof EE_Belongs_To_Relation) { |
|
| 910 | + // then just replace the cached object with the newly saved object |
|
| 911 | + $this->_model_relations[ $relationName ] = $newly_saved_object; |
|
| 912 | + return true; |
|
| 913 | + // or if it's some kind of sordid feral polyamorous relationship... |
|
| 914 | + } |
|
| 915 | + if (is_array($this->_model_relations[ $relationName ]) |
|
| 916 | + && isset($this->_model_relations[ $relationName ][ $current_cache_id ]) |
|
| 917 | + ) { |
|
| 918 | + // then remove the current cached item |
|
| 919 | + unset($this->_model_relations[ $relationName ][ $current_cache_id ]); |
|
| 920 | + // and cache the newly saved object using it's new ID |
|
| 921 | + $this->_model_relations[ $relationName ][ $newly_saved_object->ID() ] = $newly_saved_object; |
|
| 922 | + return true; |
|
| 923 | + } |
|
| 924 | + } |
|
| 925 | + return false; |
|
| 926 | + } |
|
| 927 | + |
|
| 928 | + |
|
| 929 | + /** |
|
| 930 | + * Fetches a single EE_Base_Class on that relation. (If the relation is of type |
|
| 931 | + * BelongsTo, it will only ever have 1 object. However, other relations could have an array of objects) |
|
| 932 | + * |
|
| 933 | + * @param string $relationName |
|
| 934 | + * @return EE_Base_Class |
|
| 935 | + */ |
|
| 936 | + public function get_one_from_cache($relationName) |
|
| 937 | + { |
|
| 938 | + $cached_array_or_object = isset($this->_model_relations[ $relationName ]) |
|
| 939 | + ? $this->_model_relations[ $relationName ] |
|
| 940 | + : null; |
|
| 941 | + if (is_array($cached_array_or_object)) { |
|
| 942 | + return array_shift($cached_array_or_object); |
|
| 943 | + } |
|
| 944 | + return $cached_array_or_object; |
|
| 945 | + } |
|
| 946 | + |
|
| 947 | + |
|
| 948 | + /** |
|
| 949 | + * Fetches a single EE_Base_Class on that relation. (If the relation is of type |
|
| 950 | + * BelongsTo, it will only ever have 1 object. However, other relations could have an array of objects) |
|
| 951 | + * |
|
| 952 | + * @param string $relationName |
|
| 953 | + * @throws ReflectionException |
|
| 954 | + * @throws InvalidArgumentException |
|
| 955 | + * @throws InvalidInterfaceException |
|
| 956 | + * @throws InvalidDataTypeException |
|
| 957 | + * @throws EE_Error |
|
| 958 | + * @return EE_Base_Class[] NOT necessarily indexed by primary keys |
|
| 959 | + */ |
|
| 960 | + public function get_all_from_cache($relationName) |
|
| 961 | + { |
|
| 962 | + $objects = isset($this->_model_relations[ $relationName ]) ? $this->_model_relations[ $relationName ] : array(); |
|
| 963 | + // if the result is not an array, but exists, make it an array |
|
| 964 | + $objects = is_array($objects) ? $objects : array($objects); |
|
| 965 | + // bugfix for https://events.codebasehq.com/projects/event-espresso/tickets/7143 |
|
| 966 | + // basically, if this model object was stored in the session, and these cached model objects |
|
| 967 | + // already have IDs, let's make sure they're in their model's entity mapper |
|
| 968 | + // otherwise we will have duplicates next time we call |
|
| 969 | + // EE_Registry::instance()->load_model( $relationName )->get_one_by_ID( $result->ID() ); |
|
| 970 | + $model = EE_Registry::instance()->load_model($relationName); |
|
| 971 | + foreach ($objects as $model_object) { |
|
| 972 | + if ($model instanceof EEM_Base && $model_object instanceof EE_Base_Class) { |
|
| 973 | + // ensure its in the map if it has an ID; otherwise it will be added to the map when its saved |
|
| 974 | + if ($model_object->ID()) { |
|
| 975 | + $model->add_to_entity_map($model_object); |
|
| 976 | + } |
|
| 977 | + } else { |
|
| 978 | + throw new EE_Error( |
|
| 979 | + sprintf( |
|
| 980 | + esc_html__( |
|
| 981 | + 'Error retrieving related model objects. Either $1%s is not a model or $2%s is not a model object', |
|
| 982 | + 'event_espresso' |
|
| 983 | + ), |
|
| 984 | + $relationName, |
|
| 985 | + gettype($model_object) |
|
| 986 | + ) |
|
| 987 | + ); |
|
| 988 | + } |
|
| 989 | + } |
|
| 990 | + return $objects; |
|
| 991 | + } |
|
| 992 | + |
|
| 993 | + |
|
| 994 | + /** |
|
| 995 | + * Returns the next x number of EE_Base_Class objects in sequence from this object as found in the database |
|
| 996 | + * matching the given query conditions. |
|
| 997 | + * |
|
| 998 | + * @param null $field_to_order_by What field is being used as the reference point. |
|
| 999 | + * @param int $limit How many objects to return. |
|
| 1000 | + * @param array $query_params Any additional conditions on the query. |
|
| 1001 | + * @param null $columns_to_select If left null, then an array of EE_Base_Class objects is returned, otherwise |
|
| 1002 | + * you can indicate just the columns you want returned |
|
| 1003 | + * @return array|EE_Base_Class[] |
|
| 1004 | + * @throws ReflectionException |
|
| 1005 | + * @throws InvalidArgumentException |
|
| 1006 | + * @throws InvalidInterfaceException |
|
| 1007 | + * @throws InvalidDataTypeException |
|
| 1008 | + * @throws EE_Error |
|
| 1009 | + */ |
|
| 1010 | + public function next_x($field_to_order_by = null, $limit = 1, $query_params = array(), $columns_to_select = null) |
|
| 1011 | + { |
|
| 1012 | + $model = $this->get_model(); |
|
| 1013 | + $field = empty($field_to_order_by) && $model->has_primary_key_field() |
|
| 1014 | + ? $model->get_primary_key_field()->get_name() |
|
| 1015 | + : $field_to_order_by; |
|
| 1016 | + $current_value = ! empty($field) ? $this->get($field) : null; |
|
| 1017 | + if (empty($field) || empty($current_value)) { |
|
| 1018 | + return array(); |
|
| 1019 | + } |
|
| 1020 | + return $model->next_x($current_value, $field, $limit, $query_params, $columns_to_select); |
|
| 1021 | + } |
|
| 1022 | + |
|
| 1023 | + |
|
| 1024 | + /** |
|
| 1025 | + * Returns the previous x number of EE_Base_Class objects in sequence from this object as found in the database |
|
| 1026 | + * matching the given query conditions. |
|
| 1027 | + * |
|
| 1028 | + * @param null $field_to_order_by What field is being used as the reference point. |
|
| 1029 | + * @param int $limit How many objects to return. |
|
| 1030 | + * @param array $query_params Any additional conditions on the query. |
|
| 1031 | + * @param null $columns_to_select If left null, then an array of EE_Base_Class objects is returned, otherwise |
|
| 1032 | + * you can indicate just the columns you want returned |
|
| 1033 | + * @return array|EE_Base_Class[] |
|
| 1034 | + * @throws ReflectionException |
|
| 1035 | + * @throws InvalidArgumentException |
|
| 1036 | + * @throws InvalidInterfaceException |
|
| 1037 | + * @throws InvalidDataTypeException |
|
| 1038 | + * @throws EE_Error |
|
| 1039 | + */ |
|
| 1040 | + public function previous_x( |
|
| 1041 | + $field_to_order_by = null, |
|
| 1042 | + $limit = 1, |
|
| 1043 | + $query_params = array(), |
|
| 1044 | + $columns_to_select = null |
|
| 1045 | + ) { |
|
| 1046 | + $model = $this->get_model(); |
|
| 1047 | + $field = empty($field_to_order_by) && $model->has_primary_key_field() |
|
| 1048 | + ? $model->get_primary_key_field()->get_name() |
|
| 1049 | + : $field_to_order_by; |
|
| 1050 | + $current_value = ! empty($field) ? $this->get($field) : null; |
|
| 1051 | + if (empty($field) || empty($current_value)) { |
|
| 1052 | + return array(); |
|
| 1053 | + } |
|
| 1054 | + return $model->previous_x($current_value, $field, $limit, $query_params, $columns_to_select); |
|
| 1055 | + } |
|
| 1056 | + |
|
| 1057 | + |
|
| 1058 | + /** |
|
| 1059 | + * Returns the next EE_Base_Class object in sequence from this object as found in the database |
|
| 1060 | + * matching the given query conditions. |
|
| 1061 | + * |
|
| 1062 | + * @param null $field_to_order_by What field is being used as the reference point. |
|
| 1063 | + * @param array $query_params Any additional conditions on the query. |
|
| 1064 | + * @param null $columns_to_select If left null, then an array of EE_Base_Class objects is returned, otherwise |
|
| 1065 | + * you can indicate just the columns you want returned |
|
| 1066 | + * @return array|EE_Base_Class |
|
| 1067 | + * @throws ReflectionException |
|
| 1068 | + * @throws InvalidArgumentException |
|
| 1069 | + * @throws InvalidInterfaceException |
|
| 1070 | + * @throws InvalidDataTypeException |
|
| 1071 | + * @throws EE_Error |
|
| 1072 | + */ |
|
| 1073 | + public function next($field_to_order_by = null, $query_params = array(), $columns_to_select = null) |
|
| 1074 | + { |
|
| 1075 | + $model = $this->get_model(); |
|
| 1076 | + $field = empty($field_to_order_by) && $model->has_primary_key_field() |
|
| 1077 | + ? $model->get_primary_key_field()->get_name() |
|
| 1078 | + : $field_to_order_by; |
|
| 1079 | + $current_value = ! empty($field) ? $this->get($field) : null; |
|
| 1080 | + if (empty($field) || empty($current_value)) { |
|
| 1081 | + return array(); |
|
| 1082 | + } |
|
| 1083 | + return $model->next($current_value, $field, $query_params, $columns_to_select); |
|
| 1084 | + } |
|
| 1085 | + |
|
| 1086 | + |
|
| 1087 | + /** |
|
| 1088 | + * Returns the previous EE_Base_Class object in sequence from this object as found in the database |
|
| 1089 | + * matching the given query conditions. |
|
| 1090 | + * |
|
| 1091 | + * @param null $field_to_order_by What field is being used as the reference point. |
|
| 1092 | + * @param array $query_params Any additional conditions on the query. |
|
| 1093 | + * @param null $columns_to_select If left null, then an EE_Base_Class object is returned, otherwise |
|
| 1094 | + * you can indicate just the column you want returned |
|
| 1095 | + * @return array|EE_Base_Class |
|
| 1096 | + * @throws ReflectionException |
|
| 1097 | + * @throws InvalidArgumentException |
|
| 1098 | + * @throws InvalidInterfaceException |
|
| 1099 | + * @throws InvalidDataTypeException |
|
| 1100 | + * @throws EE_Error |
|
| 1101 | + */ |
|
| 1102 | + public function previous($field_to_order_by = null, $query_params = array(), $columns_to_select = null) |
|
| 1103 | + { |
|
| 1104 | + $model = $this->get_model(); |
|
| 1105 | + $field = empty($field_to_order_by) && $model->has_primary_key_field() |
|
| 1106 | + ? $model->get_primary_key_field()->get_name() |
|
| 1107 | + : $field_to_order_by; |
|
| 1108 | + $current_value = ! empty($field) ? $this->get($field) : null; |
|
| 1109 | + if (empty($field) || empty($current_value)) { |
|
| 1110 | + return array(); |
|
| 1111 | + } |
|
| 1112 | + return $model->previous($current_value, $field, $query_params, $columns_to_select); |
|
| 1113 | + } |
|
| 1114 | + |
|
| 1115 | + |
|
| 1116 | + /** |
|
| 1117 | + * Overrides parent because parent expects old models. |
|
| 1118 | + * This also doesn't do any validation, and won't work for serialized arrays |
|
| 1119 | + * |
|
| 1120 | + * @param string $field_name |
|
| 1121 | + * @param mixed $field_value_from_db |
|
| 1122 | + * @throws ReflectionException |
|
| 1123 | + * @throws InvalidArgumentException |
|
| 1124 | + * @throws InvalidInterfaceException |
|
| 1125 | + * @throws InvalidDataTypeException |
|
| 1126 | + * @throws EE_Error |
|
| 1127 | + */ |
|
| 1128 | + public function set_from_db($field_name, $field_value_from_db) |
|
| 1129 | + { |
|
| 1130 | + $field_obj = $this->get_model()->field_settings_for($field_name); |
|
| 1131 | + if ($field_obj instanceof EE_Model_Field_Base) { |
|
| 1132 | + // you would think the DB has no NULLs for non-null label fields right? wrong! |
|
| 1133 | + // eg, a CPT model object could have an entry in the posts table, but no |
|
| 1134 | + // entry in the meta table. Meaning that all its columns in the meta table |
|
| 1135 | + // are null! yikes! so when we find one like that, use defaults for its meta columns |
|
| 1136 | + if ($field_value_from_db === null) { |
|
| 1137 | + if ($field_obj->is_nullable()) { |
|
| 1138 | + // if the field allows nulls, then let it be null |
|
| 1139 | + $field_value = null; |
|
| 1140 | + } else { |
|
| 1141 | + $field_value = $field_obj->get_default_value(); |
|
| 1142 | + } |
|
| 1143 | + } else { |
|
| 1144 | + $field_value = $field_obj->prepare_for_set_from_db($field_value_from_db); |
|
| 1145 | + } |
|
| 1146 | + $this->_fields[ $field_name ] = $field_value; |
|
| 1147 | + $this->_clear_cached_property($field_name); |
|
| 1148 | + } |
|
| 1149 | + } |
|
| 1150 | + |
|
| 1151 | + |
|
| 1152 | + /** |
|
| 1153 | + * verifies that the specified field is of the correct type |
|
| 1154 | + * |
|
| 1155 | + * @param string $field_name |
|
| 1156 | + * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
|
| 1157 | + * (in cases where the same property may be used for different outputs |
|
| 1158 | + * - i.e. datetime, money etc.) |
|
| 1159 | + * @return mixed |
|
| 1160 | + * @throws ReflectionException |
|
| 1161 | + * @throws InvalidArgumentException |
|
| 1162 | + * @throws InvalidInterfaceException |
|
| 1163 | + * @throws InvalidDataTypeException |
|
| 1164 | + * @throws EE_Error |
|
| 1165 | + */ |
|
| 1166 | + public function get($field_name, $extra_cache_ref = null) |
|
| 1167 | + { |
|
| 1168 | + return $this->_get_cached_property($field_name, false, $extra_cache_ref); |
|
| 1169 | + } |
|
| 1170 | + |
|
| 1171 | + |
|
| 1172 | + /** |
|
| 1173 | + * This method simply returns the RAW unprocessed value for the given property in this class |
|
| 1174 | + * |
|
| 1175 | + * @param string $field_name A valid fieldname |
|
| 1176 | + * @return mixed Whatever the raw value stored on the property is. |
|
| 1177 | + * @throws ReflectionException |
|
| 1178 | + * @throws InvalidArgumentException |
|
| 1179 | + * @throws InvalidInterfaceException |
|
| 1180 | + * @throws InvalidDataTypeException |
|
| 1181 | + * @throws EE_Error if fieldSettings is misconfigured or the field doesn't exist. |
|
| 1182 | + */ |
|
| 1183 | + public function get_raw($field_name) |
|
| 1184 | + { |
|
| 1185 | + $field_settings = $this->get_model()->field_settings_for($field_name); |
|
| 1186 | + return $field_settings instanceof EE_Datetime_Field && $this->_fields[ $field_name ] instanceof DateTime |
|
| 1187 | + ? $this->_fields[ $field_name ]->format('U') |
|
| 1188 | + : $this->_fields[ $field_name ]; |
|
| 1189 | + } |
|
| 1190 | + |
|
| 1191 | + |
|
| 1192 | + /** |
|
| 1193 | + * This is used to return the internal DateTime object used for a field that is a |
|
| 1194 | + * EE_Datetime_Field. |
|
| 1195 | + * |
|
| 1196 | + * @param string $field_name The field name retrieving the DateTime object. |
|
| 1197 | + * @return mixed null | false | DateTime If the requested field is NOT a EE_Datetime_Field then |
|
| 1198 | + * @throws EE_Error an error is set and false returned. If the field IS an |
|
| 1199 | + * EE_Datetime_Field and but the field value is null, then |
|
| 1200 | + * just null is returned (because that indicates that likely |
|
| 1201 | + * this field is nullable). |
|
| 1202 | + * @throws InvalidArgumentException |
|
| 1203 | + * @throws InvalidDataTypeException |
|
| 1204 | + * @throws InvalidInterfaceException |
|
| 1205 | + * @throws ReflectionException |
|
| 1206 | + */ |
|
| 1207 | + public function get_DateTime_object($field_name) |
|
| 1208 | + { |
|
| 1209 | + $field_settings = $this->get_model()->field_settings_for($field_name); |
|
| 1210 | + if (! $field_settings instanceof EE_Datetime_Field) { |
|
| 1211 | + EE_Error::add_error( |
|
| 1212 | + sprintf( |
|
| 1213 | + esc_html__( |
|
| 1214 | + 'The field %s is not an EE_Datetime_Field field. There is no DateTime object stored on this field type.', |
|
| 1215 | + 'event_espresso' |
|
| 1216 | + ), |
|
| 1217 | + $field_name |
|
| 1218 | + ), |
|
| 1219 | + __FILE__, |
|
| 1220 | + __FUNCTION__, |
|
| 1221 | + __LINE__ |
|
| 1222 | + ); |
|
| 1223 | + return false; |
|
| 1224 | + } |
|
| 1225 | + return isset($this->_fields[ $field_name ]) && $this->_fields[ $field_name ] instanceof DateTime |
|
| 1226 | + ? clone $this->_fields[ $field_name ] |
|
| 1227 | + : null; |
|
| 1228 | + } |
|
| 1229 | + |
|
| 1230 | + |
|
| 1231 | + /** |
|
| 1232 | + * To be used in template to immediately echo out the value, and format it for output. |
|
| 1233 | + * Eg, should call stripslashes and whatnot before echoing |
|
| 1234 | + * |
|
| 1235 | + * @param string $field_name the name of the field as it appears in the DB |
|
| 1236 | + * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
|
| 1237 | + * (in cases where the same property may be used for different outputs |
|
| 1238 | + * - i.e. datetime, money etc.) |
|
| 1239 | + * @return void |
|
| 1240 | + * @throws ReflectionException |
|
| 1241 | + * @throws InvalidArgumentException |
|
| 1242 | + * @throws InvalidInterfaceException |
|
| 1243 | + * @throws InvalidDataTypeException |
|
| 1244 | + * @throws EE_Error |
|
| 1245 | + */ |
|
| 1246 | + public function e($field_name, $extra_cache_ref = null) |
|
| 1247 | + { |
|
| 1248 | + echo $this->get_pretty($field_name, $extra_cache_ref); |
|
| 1249 | + } |
|
| 1250 | + |
|
| 1251 | + |
|
| 1252 | + /** |
|
| 1253 | + * Exactly like e(), echoes out the field, but sets its schema to 'form_input', so that it |
|
| 1254 | + * can be easily used as the value of form input. |
|
| 1255 | + * |
|
| 1256 | + * @param string $field_name |
|
| 1257 | + * @return void |
|
| 1258 | + * @throws ReflectionException |
|
| 1259 | + * @throws InvalidArgumentException |
|
| 1260 | + * @throws InvalidInterfaceException |
|
| 1261 | + * @throws InvalidDataTypeException |
|
| 1262 | + * @throws EE_Error |
|
| 1263 | + */ |
|
| 1264 | + public function f($field_name) |
|
| 1265 | + { |
|
| 1266 | + $this->e($field_name, 'form_input'); |
|
| 1267 | + } |
|
| 1268 | + |
|
| 1269 | + |
|
| 1270 | + /** |
|
| 1271 | + * Same as `f()` but just returns the value instead of echoing it |
|
| 1272 | + * |
|
| 1273 | + * @param string $field_name |
|
| 1274 | + * @return string |
|
| 1275 | + * @throws ReflectionException |
|
| 1276 | + * @throws InvalidArgumentException |
|
| 1277 | + * @throws InvalidInterfaceException |
|
| 1278 | + * @throws InvalidDataTypeException |
|
| 1279 | + * @throws EE_Error |
|
| 1280 | + */ |
|
| 1281 | + public function get_f($field_name) |
|
| 1282 | + { |
|
| 1283 | + return (string) $this->get_pretty($field_name, 'form_input'); |
|
| 1284 | + } |
|
| 1285 | + |
|
| 1286 | + |
|
| 1287 | + /** |
|
| 1288 | + * Gets a pretty view of the field's value. $extra_cache_ref can specify different formats for this. |
|
| 1289 | + * The $extra_cache_ref will be passed to the model field's prepare_for_pretty_echoing, so consult the field's class |
|
| 1290 | + * to see what options are available. |
|
| 1291 | + * |
|
| 1292 | + * @param string $field_name |
|
| 1293 | + * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
|
| 1294 | + * (in cases where the same property may be used for different outputs |
|
| 1295 | + * - i.e. datetime, money etc.) |
|
| 1296 | + * @return mixed |
|
| 1297 | + * @throws ReflectionException |
|
| 1298 | + * @throws InvalidArgumentException |
|
| 1299 | + * @throws InvalidInterfaceException |
|
| 1300 | + * @throws InvalidDataTypeException |
|
| 1301 | + * @throws EE_Error |
|
| 1302 | + */ |
|
| 1303 | + public function get_pretty($field_name, $extra_cache_ref = null) |
|
| 1304 | + { |
|
| 1305 | + return $this->_get_cached_property($field_name, true, $extra_cache_ref); |
|
| 1306 | + } |
|
| 1307 | + |
|
| 1308 | + |
|
| 1309 | + /** |
|
| 1310 | + * This simply returns the datetime for the given field name |
|
| 1311 | + * Note: this protected function is called by the wrapper get_date or get_time or get_datetime functions |
|
| 1312 | + * (and the equivalent e_date, e_time, e_datetime). |
|
| 1313 | + * |
|
| 1314 | + * @access protected |
|
| 1315 | + * @param string $field_name Field on the instantiated EE_Base_Class child object |
|
| 1316 | + * @param string $dt_frmt valid datetime format used for date |
|
| 1317 | + * (if '' then we just use the default on the field, |
|
| 1318 | + * if NULL we use the last-used format) |
|
| 1319 | + * @param string $tm_frmt Same as above except this is for time format |
|
| 1320 | + * @param string $date_or_time if NULL then both are returned, otherwise "D" = only date and "T" = only time. |
|
| 1321 | + * @param boolean $echo Whether the dtt is echoing using pretty echoing or just returned using vanilla get |
|
| 1322 | + * @return string|bool|EE_Error string on success, FALSE on fail, or EE_Error Exception is thrown |
|
| 1323 | + * if field is not a valid dtt field, or void if echoing |
|
| 1324 | + * @throws ReflectionException |
|
| 1325 | + * @throws InvalidArgumentException |
|
| 1326 | + * @throws InvalidInterfaceException |
|
| 1327 | + * @throws InvalidDataTypeException |
|
| 1328 | + * @throws EE_Error |
|
| 1329 | + */ |
|
| 1330 | + protected function _get_datetime($field_name, $dt_frmt = '', $tm_frmt = '', $date_or_time = '', $echo = false) |
|
| 1331 | + { |
|
| 1332 | + // clear cached property |
|
| 1333 | + $this->_clear_cached_property($field_name); |
|
| 1334 | + // reset format properties because they are used in get() |
|
| 1335 | + $this->_dt_frmt = $dt_frmt !== '' ? $dt_frmt : $this->_dt_frmt; |
|
| 1336 | + $this->_tm_frmt = $tm_frmt !== '' ? $tm_frmt : $this->_tm_frmt; |
|
| 1337 | + if ($echo) { |
|
| 1338 | + $this->e($field_name, $date_or_time); |
|
| 1339 | + return ''; |
|
| 1340 | + } |
|
| 1341 | + return $this->get($field_name, $date_or_time); |
|
| 1342 | + } |
|
| 1343 | + |
|
| 1344 | + |
|
| 1345 | + /** |
|
| 1346 | + * below are wrapper functions for the various datetime outputs that can be obtained for JUST returning the date |
|
| 1347 | + * portion of a datetime value. (note the only difference between get_ and e_ is one returns the value and the |
|
| 1348 | + * other echoes the pretty value for dtt) |
|
| 1349 | + * |
|
| 1350 | + * @param string $field_name name of model object datetime field holding the value |
|
| 1351 | + * @param string $format format for the date returned (if NULL we use default in dt_frmt property) |
|
| 1352 | + * @return string datetime value formatted |
|
| 1353 | + * @throws ReflectionException |
|
| 1354 | + * @throws InvalidArgumentException |
|
| 1355 | + * @throws InvalidInterfaceException |
|
| 1356 | + * @throws InvalidDataTypeException |
|
| 1357 | + * @throws EE_Error |
|
| 1358 | + */ |
|
| 1359 | + public function get_date($field_name, $format = '') |
|
| 1360 | + { |
|
| 1361 | + return $this->_get_datetime($field_name, $format, null, 'D'); |
|
| 1362 | + } |
|
| 1363 | + |
|
| 1364 | + |
|
| 1365 | + /** |
|
| 1366 | + * @param $field_name |
|
| 1367 | + * @param string $format |
|
| 1368 | + * @throws ReflectionException |
|
| 1369 | + * @throws InvalidArgumentException |
|
| 1370 | + * @throws InvalidInterfaceException |
|
| 1371 | + * @throws InvalidDataTypeException |
|
| 1372 | + * @throws EE_Error |
|
| 1373 | + */ |
|
| 1374 | + public function e_date($field_name, $format = '') |
|
| 1375 | + { |
|
| 1376 | + $this->_get_datetime($field_name, $format, null, 'D', true); |
|
| 1377 | + } |
|
| 1378 | + |
|
| 1379 | + |
|
| 1380 | + /** |
|
| 1381 | + * below are wrapper functions for the various datetime outputs that can be obtained for JUST returning the time |
|
| 1382 | + * portion of a datetime value. (note the only difference between get_ and e_ is one returns the value and the |
|
| 1383 | + * other echoes the pretty value for dtt) |
|
| 1384 | + * |
|
| 1385 | + * @param string $field_name name of model object datetime field holding the value |
|
| 1386 | + * @param string $format format for the time returned ( if NULL we use default in tm_frmt property) |
|
| 1387 | + * @return string datetime value formatted |
|
| 1388 | + * @throws ReflectionException |
|
| 1389 | + * @throws InvalidArgumentException |
|
| 1390 | + * @throws InvalidInterfaceException |
|
| 1391 | + * @throws InvalidDataTypeException |
|
| 1392 | + * @throws EE_Error |
|
| 1393 | + */ |
|
| 1394 | + public function get_time($field_name, $format = '') |
|
| 1395 | + { |
|
| 1396 | + return $this->_get_datetime($field_name, null, $format, 'T'); |
|
| 1397 | + } |
|
| 1398 | + |
|
| 1399 | + |
|
| 1400 | + /** |
|
| 1401 | + * @param $field_name |
|
| 1402 | + * @param string $format |
|
| 1403 | + * @throws ReflectionException |
|
| 1404 | + * @throws InvalidArgumentException |
|
| 1405 | + * @throws InvalidInterfaceException |
|
| 1406 | + * @throws InvalidDataTypeException |
|
| 1407 | + * @throws EE_Error |
|
| 1408 | + */ |
|
| 1409 | + public function e_time($field_name, $format = '') |
|
| 1410 | + { |
|
| 1411 | + $this->_get_datetime($field_name, null, $format, 'T', true); |
|
| 1412 | + } |
|
| 1413 | + |
|
| 1414 | + |
|
| 1415 | + /** |
|
| 1416 | + * below are wrapper functions for the various datetime outputs that can be obtained for returning the date AND |
|
| 1417 | + * time portion of a datetime value. (note the only difference between get_ and e_ is one returns the value and the |
|
| 1418 | + * other echoes the pretty value for dtt) |
|
| 1419 | + * |
|
| 1420 | + * @param string $field_name name of model object datetime field holding the value |
|
| 1421 | + * @param string $dt_frmt format for the date returned (if NULL we use default in dt_frmt property) |
|
| 1422 | + * @param string $tm_frmt format for the time returned (if NULL we use default in tm_frmt property) |
|
| 1423 | + * @return string datetime value formatted |
|
| 1424 | + * @throws ReflectionException |
|
| 1425 | + * @throws InvalidArgumentException |
|
| 1426 | + * @throws InvalidInterfaceException |
|
| 1427 | + * @throws InvalidDataTypeException |
|
| 1428 | + * @throws EE_Error |
|
| 1429 | + */ |
|
| 1430 | + public function get_datetime($field_name, $dt_frmt = '', $tm_frmt = '') |
|
| 1431 | + { |
|
| 1432 | + return $this->_get_datetime($field_name, $dt_frmt, $tm_frmt); |
|
| 1433 | + } |
|
| 1434 | + |
|
| 1435 | + |
|
| 1436 | + /** |
|
| 1437 | + * @param string $field_name |
|
| 1438 | + * @param string $dt_frmt |
|
| 1439 | + * @param string $tm_frmt |
|
| 1440 | + * @throws ReflectionException |
|
| 1441 | + * @throws InvalidArgumentException |
|
| 1442 | + * @throws InvalidInterfaceException |
|
| 1443 | + * @throws InvalidDataTypeException |
|
| 1444 | + * @throws EE_Error |
|
| 1445 | + */ |
|
| 1446 | + public function e_datetime($field_name, $dt_frmt = '', $tm_frmt = '') |
|
| 1447 | + { |
|
| 1448 | + $this->_get_datetime($field_name, $dt_frmt, $tm_frmt, null, true); |
|
| 1449 | + } |
|
| 1450 | + |
|
| 1451 | + |
|
| 1452 | + /** |
|
| 1453 | + * Get the i8ln value for a date using the WordPress @see date_i18n function. |
|
| 1454 | + * |
|
| 1455 | + * @param string $field_name The EE_Datetime_Field reference for the date being retrieved. |
|
| 1456 | + * @param string $format PHP valid date/time string format. If none is provided then the internal set format |
|
| 1457 | + * on the object will be used. |
|
| 1458 | + * @return string Date and time string in set locale or false if no field exists for the given |
|
| 1459 | + * @throws ReflectionException |
|
| 1460 | + * @throws InvalidArgumentException |
|
| 1461 | + * @throws InvalidInterfaceException |
|
| 1462 | + * @throws InvalidDataTypeException |
|
| 1463 | + * @throws EE_Error |
|
| 1464 | + * field name. |
|
| 1465 | + */ |
|
| 1466 | + public function get_i18n_datetime($field_name, $format = '') |
|
| 1467 | + { |
|
| 1468 | + $format = empty($format) ? $this->_dt_frmt . ' ' . $this->_tm_frmt : $format; |
|
| 1469 | + return date_i18n( |
|
| 1470 | + $format, |
|
| 1471 | + EEH_DTT_Helper::get_timestamp_with_offset( |
|
| 1472 | + $this->get_raw($field_name), |
|
| 1473 | + $this->_timezone |
|
| 1474 | + ) |
|
| 1475 | + ); |
|
| 1476 | + } |
|
| 1477 | + |
|
| 1478 | + |
|
| 1479 | + /** |
|
| 1480 | + * This method validates whether the given field name is a valid field on the model object as well as it is of a |
|
| 1481 | + * type EE_Datetime_Field. On success there will be returned the field settings. On fail an EE_Error exception is |
|
| 1482 | + * thrown. |
|
| 1483 | + * |
|
| 1484 | + * @param string $field_name The field name being checked |
|
| 1485 | + * @throws ReflectionException |
|
| 1486 | + * @throws InvalidArgumentException |
|
| 1487 | + * @throws InvalidInterfaceException |
|
| 1488 | + * @throws InvalidDataTypeException |
|
| 1489 | + * @throws EE_Error |
|
| 1490 | + * @return EE_Datetime_Field |
|
| 1491 | + */ |
|
| 1492 | + protected function _get_dtt_field_settings($field_name) |
|
| 1493 | + { |
|
| 1494 | + $field = $this->get_model()->field_settings_for($field_name); |
|
| 1495 | + // check if field is dtt |
|
| 1496 | + if ($field instanceof EE_Datetime_Field) { |
|
| 1497 | + return $field; |
|
| 1498 | + } |
|
| 1499 | + throw new EE_Error( |
|
| 1500 | + sprintf( |
|
| 1501 | + esc_html__( |
|
| 1502 | + 'The field name "%s" has been requested for the EE_Base_Class datetime functions and it is not a valid EE_Datetime_Field. Please check the spelling of the field and make sure it has been setup as a EE_Datetime_Field in the %s model constructor', |
|
| 1503 | + 'event_espresso' |
|
| 1504 | + ), |
|
| 1505 | + $field_name, |
|
| 1506 | + self::_get_model_classname(get_class($this)) |
|
| 1507 | + ) |
|
| 1508 | + ); |
|
| 1509 | + } |
|
| 1510 | + |
|
| 1511 | + |
|
| 1512 | + |
|
| 1513 | + |
|
| 1514 | + /** |
|
| 1515 | + * NOTE ABOUT BELOW: |
|
| 1516 | + * These convenience date and time setters are for setting date and time independently. In other words you might |
|
| 1517 | + * want to change the time on a datetime_field but leave the date the same (or vice versa). IF on the other hand |
|
| 1518 | + * you want to set both date and time at the same time, you can just use the models default set($fieldname,$value) |
|
| 1519 | + * method and make sure you send the entire datetime value for setting. |
|
| 1520 | + */ |
|
| 1521 | + /** |
|
| 1522 | + * sets the time on a datetime property |
|
| 1523 | + * |
|
| 1524 | + * @access protected |
|
| 1525 | + * @param string|Datetime $time a valid time string for php datetime functions (or DateTime object) |
|
| 1526 | + * @param string $fieldname the name of the field the time is being set on (must match a EE_Datetime_Field) |
|
| 1527 | + * @throws ReflectionException |
|
| 1528 | + * @throws InvalidArgumentException |
|
| 1529 | + * @throws InvalidInterfaceException |
|
| 1530 | + * @throws InvalidDataTypeException |
|
| 1531 | + * @throws EE_Error |
|
| 1532 | + */ |
|
| 1533 | + protected function _set_time_for($time, $fieldname) |
|
| 1534 | + { |
|
| 1535 | + $this->_set_date_time('T', $time, $fieldname); |
|
| 1536 | + } |
|
| 1537 | + |
|
| 1538 | + |
|
| 1539 | + /** |
|
| 1540 | + * sets the date on a datetime property |
|
| 1541 | + * |
|
| 1542 | + * @access protected |
|
| 1543 | + * @param string|DateTime $date a valid date string for php datetime functions ( or DateTime object) |
|
| 1544 | + * @param string $fieldname the name of the field the date is being set on (must match a EE_Datetime_Field) |
|
| 1545 | + * @throws ReflectionException |
|
| 1546 | + * @throws InvalidArgumentException |
|
| 1547 | + * @throws InvalidInterfaceException |
|
| 1548 | + * @throws InvalidDataTypeException |
|
| 1549 | + * @throws EE_Error |
|
| 1550 | + */ |
|
| 1551 | + protected function _set_date_for($date, $fieldname) |
|
| 1552 | + { |
|
| 1553 | + $this->_set_date_time('D', $date, $fieldname); |
|
| 1554 | + } |
|
| 1555 | + |
|
| 1556 | + |
|
| 1557 | + /** |
|
| 1558 | + * This takes care of setting a date or time independently on a given model object property. This method also |
|
| 1559 | + * verifies that the given fieldname matches a model object property and is for a EE_Datetime_Field field |
|
| 1560 | + * |
|
| 1561 | + * @access protected |
|
| 1562 | + * @param string $what "T" for time, 'B' for both, 'D' for Date. |
|
| 1563 | + * @param string|DateTime $datetime_value A valid Date or Time string (or DateTime object) |
|
| 1564 | + * @param string $fieldname the name of the field the date OR time is being set on (must match a |
|
| 1565 | + * EE_Datetime_Field property) |
|
| 1566 | + * @throws ReflectionException |
|
| 1567 | + * @throws InvalidArgumentException |
|
| 1568 | + * @throws InvalidInterfaceException |
|
| 1569 | + * @throws InvalidDataTypeException |
|
| 1570 | + * @throws EE_Error |
|
| 1571 | + */ |
|
| 1572 | + protected function _set_date_time($what = 'T', $datetime_value, $fieldname) |
|
| 1573 | + { |
|
| 1574 | + $field = $this->_get_dtt_field_settings($fieldname); |
|
| 1575 | + $field->set_timezone($this->_timezone); |
|
| 1576 | + $field->set_date_format($this->_dt_frmt); |
|
| 1577 | + $field->set_time_format($this->_tm_frmt); |
|
| 1578 | + switch ($what) { |
|
| 1579 | + case 'T': |
|
| 1580 | + $this->_fields[ $fieldname ] = $field->prepare_for_set_with_new_time( |
|
| 1581 | + $datetime_value, |
|
| 1582 | + $this->_fields[ $fieldname ] |
|
| 1583 | + ); |
|
| 1584 | + break; |
|
| 1585 | + case 'D': |
|
| 1586 | + $this->_fields[ $fieldname ] = $field->prepare_for_set_with_new_date( |
|
| 1587 | + $datetime_value, |
|
| 1588 | + $this->_fields[ $fieldname ] |
|
| 1589 | + ); |
|
| 1590 | + break; |
|
| 1591 | + case 'B': |
|
| 1592 | + $this->_fields[ $fieldname ] = $field->prepare_for_set($datetime_value); |
|
| 1593 | + break; |
|
| 1594 | + } |
|
| 1595 | + $this->_clear_cached_property($fieldname); |
|
| 1596 | + } |
|
| 1597 | + |
|
| 1598 | + |
|
| 1599 | + /** |
|
| 1600 | + * This will return a timestamp for the website timezone but ONLY when the current website timezone is different |
|
| 1601 | + * than the timezone set for the website. NOTE, this currently only works well with methods that return values. If |
|
| 1602 | + * you use it with methods that echo values the $_timestamp property may not get reset to its original value and |
|
| 1603 | + * that could lead to some unexpected results! |
|
| 1604 | + * |
|
| 1605 | + * @access public |
|
| 1606 | + * @param string $field_name This is the name of the field on the object that contains the date/time |
|
| 1607 | + * value being returned. |
|
| 1608 | + * @param string $callback must match a valid method in this class (defaults to get_datetime) |
|
| 1609 | + * @param mixed (array|string) $args This is the arguments that will be passed to the callback. |
|
| 1610 | + * @param string $prepend You can include something to prepend on the timestamp |
|
| 1611 | + * @param string $append You can include something to append on the timestamp |
|
| 1612 | + * @throws ReflectionException |
|
| 1613 | + * @throws InvalidArgumentException |
|
| 1614 | + * @throws InvalidInterfaceException |
|
| 1615 | + * @throws InvalidDataTypeException |
|
| 1616 | + * @throws EE_Error |
|
| 1617 | + * @return string timestamp |
|
| 1618 | + */ |
|
| 1619 | + public function display_in_my_timezone( |
|
| 1620 | + $field_name, |
|
| 1621 | + $callback = 'get_datetime', |
|
| 1622 | + $args = null, |
|
| 1623 | + $prepend = '', |
|
| 1624 | + $append = '' |
|
| 1625 | + ) { |
|
| 1626 | + $timezone = EEH_DTT_Helper::get_timezone(); |
|
| 1627 | + if ($timezone === $this->_timezone) { |
|
| 1628 | + return ''; |
|
| 1629 | + } |
|
| 1630 | + $original_timezone = $this->_timezone; |
|
| 1631 | + $this->set_timezone($timezone); |
|
| 1632 | + $fn = (array) $field_name; |
|
| 1633 | + $args = array_merge($fn, (array) $args); |
|
| 1634 | + if (! method_exists($this, $callback)) { |
|
| 1635 | + throw new EE_Error( |
|
| 1636 | + sprintf( |
|
| 1637 | + esc_html__( |
|
| 1638 | + 'The method named "%s" given as the callback param in "display_in_my_timezone" does not exist. Please check your spelling', |
|
| 1639 | + 'event_espresso' |
|
| 1640 | + ), |
|
| 1641 | + $callback |
|
| 1642 | + ) |
|
| 1643 | + ); |
|
| 1644 | + } |
|
| 1645 | + $args = (array) $args; |
|
| 1646 | + $return = $prepend . call_user_func_array(array($this, $callback), $args) . $append; |
|
| 1647 | + $this->set_timezone($original_timezone); |
|
| 1648 | + return $return; |
|
| 1649 | + } |
|
| 1650 | + |
|
| 1651 | + |
|
| 1652 | + /** |
|
| 1653 | + * Deletes this model object. |
|
| 1654 | + * This calls the `EE_Base_Class::_delete` method. Child classes wishing to change default behaviour should |
|
| 1655 | + * override |
|
| 1656 | + * `EE_Base_Class::_delete` NOT this class. |
|
| 1657 | + * |
|
| 1658 | + * @return boolean | int |
|
| 1659 | + * @throws ReflectionException |
|
| 1660 | + * @throws InvalidArgumentException |
|
| 1661 | + * @throws InvalidInterfaceException |
|
| 1662 | + * @throws InvalidDataTypeException |
|
| 1663 | + * @throws EE_Error |
|
| 1664 | + */ |
|
| 1665 | + public function delete() |
|
| 1666 | + { |
|
| 1667 | + /** |
|
| 1668 | + * Called just before the `EE_Base_Class::_delete` method call. |
|
| 1669 | + * Note: |
|
| 1670 | + * `EE_Base_Class::_delete` might be overridden by child classes so any client code hooking into these actions |
|
| 1671 | + * should be aware that `_delete` may not always result in a permanent delete. |
|
| 1672 | + * For example, `EE_Soft_Delete_Base_Class::_delete` |
|
| 1673 | + * soft deletes (trash) the object and does not permanently delete it. |
|
| 1674 | + * |
|
| 1675 | + * @param EE_Base_Class $model_object about to be 'deleted' |
|
| 1676 | + */ |
|
| 1677 | + do_action('AHEE__EE_Base_Class__delete__before', $this); |
|
| 1678 | + $result = $this->_delete(); |
|
| 1679 | + /** |
|
| 1680 | + * Called just after the `EE_Base_Class::_delete` method call. |
|
| 1681 | + * Note: |
|
| 1682 | + * `EE_Base_Class::_delete` might be overridden by child classes so any client code hooking into these actions |
|
| 1683 | + * should be aware that `_delete` may not always result in a permanent delete. |
|
| 1684 | + * For example `EE_Soft_Base_Class::_delete` |
|
| 1685 | + * soft deletes (trash) the object and does not permanently delete it. |
|
| 1686 | + * |
|
| 1687 | + * @param EE_Base_Class $model_object that was just 'deleted' |
|
| 1688 | + * @param boolean $result |
|
| 1689 | + */ |
|
| 1690 | + do_action('AHEE__EE_Base_Class__delete__end', $this, $result); |
|
| 1691 | + return $result; |
|
| 1692 | + } |
|
| 1693 | + |
|
| 1694 | + |
|
| 1695 | + /** |
|
| 1696 | + * Calls the specific delete method for the instantiated class. |
|
| 1697 | + * This method is called by the public `EE_Base_Class::delete` method. Any child classes desiring to override |
|
| 1698 | + * default functionality for "delete" (which is to call `permanently_delete`) should override this method NOT |
|
| 1699 | + * `EE_Base_Class::delete` |
|
| 1700 | + * |
|
| 1701 | + * @return bool|int |
|
| 1702 | + * @throws ReflectionException |
|
| 1703 | + * @throws InvalidArgumentException |
|
| 1704 | + * @throws InvalidInterfaceException |
|
| 1705 | + * @throws InvalidDataTypeException |
|
| 1706 | + * @throws EE_Error |
|
| 1707 | + */ |
|
| 1708 | + protected function _delete() |
|
| 1709 | + { |
|
| 1710 | + return $this->delete_permanently(); |
|
| 1711 | + } |
|
| 1712 | + |
|
| 1713 | + |
|
| 1714 | + /** |
|
| 1715 | + * Deletes this model object permanently from db |
|
| 1716 | + * (but keep in mind related models may block the delete and return an error) |
|
| 1717 | + * |
|
| 1718 | + * @return bool | int |
|
| 1719 | + * @throws ReflectionException |
|
| 1720 | + * @throws InvalidArgumentException |
|
| 1721 | + * @throws InvalidInterfaceException |
|
| 1722 | + * @throws InvalidDataTypeException |
|
| 1723 | + * @throws EE_Error |
|
| 1724 | + */ |
|
| 1725 | + public function delete_permanently() |
|
| 1726 | + { |
|
| 1727 | + /** |
|
| 1728 | + * Called just before HARD deleting a model object |
|
| 1729 | + * |
|
| 1730 | + * @param EE_Base_Class $model_object about to be 'deleted' |
|
| 1731 | + */ |
|
| 1732 | + do_action('AHEE__EE_Base_Class__delete_permanently__before', $this); |
|
| 1733 | + $model = $this->get_model(); |
|
| 1734 | + $result = $model->delete_permanently_by_ID($this->ID()); |
|
| 1735 | + $this->refresh_cache_of_related_objects(); |
|
| 1736 | + /** |
|
| 1737 | + * Called just after HARD deleting a model object |
|
| 1738 | + * |
|
| 1739 | + * @param EE_Base_Class $model_object that was just 'deleted' |
|
| 1740 | + * @param boolean $result |
|
| 1741 | + */ |
|
| 1742 | + do_action('AHEE__EE_Base_Class__delete_permanently__end', $this, $result); |
|
| 1743 | + return $result; |
|
| 1744 | + } |
|
| 1745 | + |
|
| 1746 | + |
|
| 1747 | + /** |
|
| 1748 | + * When this model object is deleted, it may still be cached on related model objects. This clears the cache of |
|
| 1749 | + * related model objects |
|
| 1750 | + * |
|
| 1751 | + * @throws ReflectionException |
|
| 1752 | + * @throws InvalidArgumentException |
|
| 1753 | + * @throws InvalidInterfaceException |
|
| 1754 | + * @throws InvalidDataTypeException |
|
| 1755 | + * @throws EE_Error |
|
| 1756 | + */ |
|
| 1757 | + public function refresh_cache_of_related_objects() |
|
| 1758 | + { |
|
| 1759 | + $model = $this->get_model(); |
|
| 1760 | + foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
|
| 1761 | + if (! empty($this->_model_relations[ $relation_name ])) { |
|
| 1762 | + $related_objects = $this->_model_relations[ $relation_name ]; |
|
| 1763 | + if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
| 1764 | + // this relation only stores a single model object, not an array |
|
| 1765 | + // but let's make it consistent |
|
| 1766 | + $related_objects = array($related_objects); |
|
| 1767 | + } |
|
| 1768 | + foreach ($related_objects as $related_object) { |
|
| 1769 | + // only refresh their cache if they're in memory |
|
| 1770 | + if ($related_object instanceof EE_Base_Class) { |
|
| 1771 | + $related_object->clear_cache( |
|
| 1772 | + $model->get_this_model_name(), |
|
| 1773 | + $this |
|
| 1774 | + ); |
|
| 1775 | + } |
|
| 1776 | + } |
|
| 1777 | + } |
|
| 1778 | + } |
|
| 1779 | + } |
|
| 1780 | + |
|
| 1781 | + |
|
| 1782 | + /** |
|
| 1783 | + * Saves this object to the database. An array may be supplied to set some values on this |
|
| 1784 | + * object just before saving. |
|
| 1785 | + * |
|
| 1786 | + * @access public |
|
| 1787 | + * @param array $set_cols_n_values keys are field names, values are their new values, |
|
| 1788 | + * if provided during the save() method (often client code will change the fields' |
|
| 1789 | + * values before calling save) |
|
| 1790 | + * @throws InvalidArgumentException |
|
| 1791 | + * @throws InvalidInterfaceException |
|
| 1792 | + * @throws InvalidDataTypeException |
|
| 1793 | + * @throws EE_Error |
|
| 1794 | + * @return int , 1 on a successful update, the ID of the new entry on insert; 0 on failure or if the model object |
|
| 1795 | + * isn't allowed to persist (as determined by EE_Base_Class::allow_persist()) |
|
| 1796 | + * @throws ReflectionException |
|
| 1797 | + * @throws ReflectionException |
|
| 1798 | + * @throws ReflectionException |
|
| 1799 | + */ |
|
| 1800 | + public function save($set_cols_n_values = array()) |
|
| 1801 | + { |
|
| 1802 | + $model = $this->get_model(); |
|
| 1803 | + /** |
|
| 1804 | + * Filters the fields we're about to save on the model object |
|
| 1805 | + * |
|
| 1806 | + * @param array $set_cols_n_values |
|
| 1807 | + * @param EE_Base_Class $model_object |
|
| 1808 | + */ |
|
| 1809 | + $set_cols_n_values = (array) apply_filters( |
|
| 1810 | + 'FHEE__EE_Base_Class__save__set_cols_n_values', |
|
| 1811 | + $set_cols_n_values, |
|
| 1812 | + $this |
|
| 1813 | + ); |
|
| 1814 | + // set attributes as provided in $set_cols_n_values |
|
| 1815 | + foreach ($set_cols_n_values as $column => $value) { |
|
| 1816 | + $this->set($column, $value); |
|
| 1817 | + } |
|
| 1818 | + // no changes ? then don't do anything |
|
| 1819 | + if (! $this->_has_changes && $this->ID() && $model->get_primary_key_field()->is_auto_increment()) { |
|
| 1820 | + return 0; |
|
| 1821 | + } |
|
| 1822 | + /** |
|
| 1823 | + * Saving a model object. |
|
| 1824 | + * Before we perform a save, this action is fired. |
|
| 1825 | + * |
|
| 1826 | + * @param EE_Base_Class $model_object the model object about to be saved. |
|
| 1827 | + */ |
|
| 1828 | + do_action('AHEE__EE_Base_Class__save__begin', $this); |
|
| 1829 | + if (! $this->allow_persist()) { |
|
| 1830 | + return 0; |
|
| 1831 | + } |
|
| 1832 | + // now get current attribute values |
|
| 1833 | + $save_cols_n_values = $this->_fields; |
|
| 1834 | + // if the object already has an ID, update it. Otherwise, insert it |
|
| 1835 | + // also: change the assumption about values passed to the model NOT being prepare dby the model object. |
|
| 1836 | + // They have been |
|
| 1837 | + $old_assumption_concerning_value_preparation = $model |
|
| 1838 | + ->get_assumption_concerning_values_already_prepared_by_model_object(); |
|
| 1839 | + $model->assume_values_already_prepared_by_model_object(true); |
|
| 1840 | + // does this model have an autoincrement PK? |
|
| 1841 | + if ($model->has_primary_key_field()) { |
|
| 1842 | + if ($model->get_primary_key_field()->is_auto_increment()) { |
|
| 1843 | + // ok check if it's set, if so: update; if not, insert |
|
| 1844 | + if (! empty($save_cols_n_values[ $model->primary_key_name() ])) { |
|
| 1845 | + $results = $model->update_by_ID($save_cols_n_values, $this->ID()); |
|
| 1846 | + } else { |
|
| 1847 | + unset($save_cols_n_values[ $model->primary_key_name() ]); |
|
| 1848 | + $results = $model->insert($save_cols_n_values); |
|
| 1849 | + if ($results) { |
|
| 1850 | + // if successful, set the primary key |
|
| 1851 | + // but don't use the normal SET method, because it will check if |
|
| 1852 | + // an item with the same ID exists in the mapper & db, then |
|
| 1853 | + // will find it in the db (because we just added it) and THAT object |
|
| 1854 | + // will get added to the mapper before we can add this one! |
|
| 1855 | + // but if we just avoid using the SET method, all that headache can be avoided |
|
| 1856 | + $pk_field_name = $model->primary_key_name(); |
|
| 1857 | + $this->_fields[ $pk_field_name ] = $results; |
|
| 1858 | + $this->_clear_cached_property($pk_field_name); |
|
| 1859 | + $model->add_to_entity_map($this); |
|
| 1860 | + $this->_update_cached_related_model_objs_fks(); |
|
| 1861 | + } |
|
| 1862 | + } |
|
| 1863 | + } else {// PK is NOT auto-increment |
|
| 1864 | + // so check if one like it already exists in the db |
|
| 1865 | + if ($model->exists_by_ID($this->ID())) { |
|
| 1866 | + if (WP_DEBUG && ! $this->in_entity_map()) { |
|
| 1867 | + throw new EE_Error( |
|
| 1868 | + sprintf( |
|
| 1869 | + esc_html__( |
|
| 1870 | + 'Using a model object %1$s that is NOT in the entity map, can lead to unexpected errors. You should either: %4$s 1. Put it in the entity mapper by calling %2$s %4$s 2. Discard this model object and use what is in the entity mapper %4$s 3. Fetch from the database using %3$s', |
|
| 1871 | + 'event_espresso' |
|
| 1872 | + ), |
|
| 1873 | + get_class($this), |
|
| 1874 | + get_class($model) . '::instance()->add_to_entity_map()', |
|
| 1875 | + get_class($model) . '::instance()->get_one_by_ID()', |
|
| 1876 | + '<br />' |
|
| 1877 | + ) |
|
| 1878 | + ); |
|
| 1879 | + } |
|
| 1880 | + $results = $model->update_by_ID($save_cols_n_values, $this->ID()); |
|
| 1881 | + } else { |
|
| 1882 | + $results = $model->insert($save_cols_n_values); |
|
| 1883 | + $this->_update_cached_related_model_objs_fks(); |
|
| 1884 | + } |
|
| 1885 | + } |
|
| 1886 | + } else {// there is NO primary key |
|
| 1887 | + $already_in_db = false; |
|
| 1888 | + foreach ($model->unique_indexes() as $index) { |
|
| 1889 | + $uniqueness_where_params = array_intersect_key($save_cols_n_values, $index->fields()); |
|
| 1890 | + if ($model->exists(array($uniqueness_where_params))) { |
|
| 1891 | + $already_in_db = true; |
|
| 1892 | + } |
|
| 1893 | + } |
|
| 1894 | + if ($already_in_db) { |
|
| 1895 | + $combined_pk_fields_n_values = array_intersect_key( |
|
| 1896 | + $save_cols_n_values, |
|
| 1897 | + $model->get_combined_primary_key_fields() |
|
| 1898 | + ); |
|
| 1899 | + $results = $model->update( |
|
| 1900 | + $save_cols_n_values, |
|
| 1901 | + $combined_pk_fields_n_values |
|
| 1902 | + ); |
|
| 1903 | + } else { |
|
| 1904 | + $results = $model->insert($save_cols_n_values); |
|
| 1905 | + } |
|
| 1906 | + } |
|
| 1907 | + // restore the old assumption about values being prepared by the model object |
|
| 1908 | + $model->assume_values_already_prepared_by_model_object( |
|
| 1909 | + $old_assumption_concerning_value_preparation |
|
| 1910 | + ); |
|
| 1911 | + /** |
|
| 1912 | + * After saving the model object this action is called |
|
| 1913 | + * |
|
| 1914 | + * @param EE_Base_Class $model_object which was just saved |
|
| 1915 | + * @param boolean|int $results if it were updated, TRUE or FALSE; if it were newly inserted |
|
| 1916 | + * the new ID (or 0 if an error occurred and it wasn't updated) |
|
| 1917 | + */ |
|
| 1918 | + do_action('AHEE__EE_Base_Class__save__end', $this, $results); |
|
| 1919 | + $this->_has_changes = false; |
|
| 1920 | + return $results; |
|
| 1921 | + } |
|
| 1922 | + |
|
| 1923 | + |
|
| 1924 | + /** |
|
| 1925 | + * Updates the foreign key on related models objects pointing to this to have this model object's ID |
|
| 1926 | + * as their foreign key. If the cached related model objects already exist in the db, saves them (so that the DB |
|
| 1927 | + * is consistent) Especially useful in case we JUST added this model object ot the database and we want to let its |
|
| 1928 | + * cached relations with foreign keys to it know about that change. Eg: we've created a transaction but haven't |
|
| 1929 | + * saved it to the db. We also create a registration and don't save it to the DB, but we DO cache it on the |
|
| 1930 | + * transaction. Now, when we save the transaction, the registration's TXN_ID will be automatically updated, whether |
|
| 1931 | + * or not they exist in the DB (if they do, their DB records will be automatically updated) |
|
| 1932 | + * |
|
| 1933 | + * @return void |
|
| 1934 | + * @throws ReflectionException |
|
| 1935 | + * @throws InvalidArgumentException |
|
| 1936 | + * @throws InvalidInterfaceException |
|
| 1937 | + * @throws InvalidDataTypeException |
|
| 1938 | + * @throws EE_Error |
|
| 1939 | + */ |
|
| 1940 | + protected function _update_cached_related_model_objs_fks() |
|
| 1941 | + { |
|
| 1942 | + $model = $this->get_model(); |
|
| 1943 | + foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
|
| 1944 | + if ($relation_obj instanceof EE_Has_Many_Relation) { |
|
| 1945 | + foreach ($this->get_all_from_cache($relation_name) as $related_model_obj_in_cache) { |
|
| 1946 | + $fk_to_this = $related_model_obj_in_cache->get_model()->get_foreign_key_to( |
|
| 1947 | + $model->get_this_model_name() |
|
| 1948 | + ); |
|
| 1949 | + $related_model_obj_in_cache->set($fk_to_this->get_name(), $this->ID()); |
|
| 1950 | + if ($related_model_obj_in_cache->ID()) { |
|
| 1951 | + $related_model_obj_in_cache->save(); |
|
| 1952 | + } |
|
| 1953 | + } |
|
| 1954 | + } |
|
| 1955 | + } |
|
| 1956 | + } |
|
| 1957 | + |
|
| 1958 | + |
|
| 1959 | + /** |
|
| 1960 | + * Saves this model object and its NEW cached relations to the database. |
|
| 1961 | + * (Meaning, for now, IT DOES NOT WORK if the cached items already exist in the DB. |
|
| 1962 | + * In order for that to work, we would need to mark model objects as dirty/clean... |
|
| 1963 | + * because otherwise, there's a potential for infinite looping of saving |
|
| 1964 | + * Saves the cached related model objects, and ensures the relation between them |
|
| 1965 | + * and this object and properly setup |
|
| 1966 | + * |
|
| 1967 | + * @return int ID of new model object on save; 0 on failure+ |
|
| 1968 | + * @throws ReflectionException |
|
| 1969 | + * @throws InvalidArgumentException |
|
| 1970 | + * @throws InvalidInterfaceException |
|
| 1971 | + * @throws InvalidDataTypeException |
|
| 1972 | + * @throws EE_Error |
|
| 1973 | + */ |
|
| 1974 | + public function save_new_cached_related_model_objs() |
|
| 1975 | + { |
|
| 1976 | + // make sure this has been saved |
|
| 1977 | + if (! $this->ID()) { |
|
| 1978 | + $id = $this->save(); |
|
| 1979 | + } else { |
|
| 1980 | + $id = $this->ID(); |
|
| 1981 | + } |
|
| 1982 | + // now save all the NEW cached model objects (ie they don't exist in the DB) |
|
| 1983 | + foreach ($this->get_model()->relation_settings() as $relationName => $relationObj) { |
|
| 1984 | + if ($this->_model_relations[ $relationName ]) { |
|
| 1985 | + // is this a relation where we should expect just ONE related object (ie, EE_Belongs_To_relation) |
|
| 1986 | + // or MANY related objects (ie, EE_HABTM_Relation or EE_Has_Many_Relation)? |
|
| 1987 | + /* @var $related_model_obj EE_Base_Class */ |
|
| 1988 | + if ($relationObj instanceof EE_Belongs_To_Relation) { |
|
| 1989 | + // add a relation to that relation type (which saves the appropriate thing in the process) |
|
| 1990 | + // but ONLY if it DOES NOT exist in the DB |
|
| 1991 | + $related_model_obj = $this->_model_relations[ $relationName ]; |
|
| 1992 | + // if( ! $related_model_obj->ID()){ |
|
| 1993 | + $this->_add_relation_to($related_model_obj, $relationName); |
|
| 1994 | + $related_model_obj->save_new_cached_related_model_objs(); |
|
| 1995 | + // } |
|
| 1996 | + } else { |
|
| 1997 | + foreach ($this->_model_relations[ $relationName ] as $related_model_obj) { |
|
| 1998 | + // add a relation to that relation type (which saves the appropriate thing in the process) |
|
| 1999 | + // but ONLY if it DOES NOT exist in the DB |
|
| 2000 | + // if( ! $related_model_obj->ID()){ |
|
| 2001 | + $this->_add_relation_to($related_model_obj, $relationName); |
|
| 2002 | + $related_model_obj->save_new_cached_related_model_objs(); |
|
| 2003 | + // } |
|
| 2004 | + } |
|
| 2005 | + } |
|
| 2006 | + } |
|
| 2007 | + } |
|
| 2008 | + return $id; |
|
| 2009 | + } |
|
| 2010 | + |
|
| 2011 | + |
|
| 2012 | + /** |
|
| 2013 | + * for getting a model while instantiated. |
|
| 2014 | + * |
|
| 2015 | + * @return EEM_Base | EEM_CPT_Base |
|
| 2016 | + * @throws ReflectionException |
|
| 2017 | + * @throws InvalidArgumentException |
|
| 2018 | + * @throws InvalidInterfaceException |
|
| 2019 | + * @throws InvalidDataTypeException |
|
| 2020 | + * @throws EE_Error |
|
| 2021 | + */ |
|
| 2022 | + public function get_model() |
|
| 2023 | + { |
|
| 2024 | + if (! $this->_model) { |
|
| 2025 | + $modelName = self::_get_model_classname(get_class($this)); |
|
| 2026 | + $this->_model = self::_get_model_instance_with_name($modelName, $this->_timezone); |
|
| 2027 | + } else { |
|
| 2028 | + $this->_model->set_timezone($this->_timezone); |
|
| 2029 | + } |
|
| 2030 | + return $this->_model; |
|
| 2031 | + } |
|
| 2032 | + |
|
| 2033 | + |
|
| 2034 | + /** |
|
| 2035 | + * @param $props_n_values |
|
| 2036 | + * @param $classname |
|
| 2037 | + * @return mixed bool|EE_Base_Class|EEM_CPT_Base |
|
| 2038 | + * @throws ReflectionException |
|
| 2039 | + * @throws InvalidArgumentException |
|
| 2040 | + * @throws InvalidInterfaceException |
|
| 2041 | + * @throws InvalidDataTypeException |
|
| 2042 | + * @throws EE_Error |
|
| 2043 | + */ |
|
| 2044 | + protected static function _get_object_from_entity_mapper($props_n_values, $classname) |
|
| 2045 | + { |
|
| 2046 | + // TODO: will not work for Term_Relationships because they have no PK! |
|
| 2047 | + $primary_id_ref = self::_get_primary_key_name($classname); |
|
| 2048 | + if (array_key_exists($primary_id_ref, $props_n_values) |
|
| 2049 | + && ! empty($props_n_values[ $primary_id_ref ]) |
|
| 2050 | + ) { |
|
| 2051 | + $id = $props_n_values[ $primary_id_ref ]; |
|
| 2052 | + return self::_get_model($classname)->get_from_entity_map($id); |
|
| 2053 | + } |
|
| 2054 | + return false; |
|
| 2055 | + } |
|
| 2056 | + |
|
| 2057 | + |
|
| 2058 | + /** |
|
| 2059 | + * This is called by child static "new_instance" method and we'll check to see if there is an existing db entry for |
|
| 2060 | + * the primary key (if present in incoming values). If there is a key in the incoming array that matches the |
|
| 2061 | + * primary key for the model AND it is not null, then we check the db. If there's a an object we return it. If not |
|
| 2062 | + * we return false. |
|
| 2063 | + * |
|
| 2064 | + * @param array $props_n_values incoming array of properties and their values |
|
| 2065 | + * @param string $classname the classname of the child class |
|
| 2066 | + * @param null $timezone |
|
| 2067 | + * @param array $date_formats incoming date_formats in an array where the first value is the |
|
| 2068 | + * date_format and the second value is the time format |
|
| 2069 | + * @return mixed (EE_Base_Class|bool) |
|
| 2070 | + * @throws InvalidArgumentException |
|
| 2071 | + * @throws InvalidInterfaceException |
|
| 2072 | + * @throws InvalidDataTypeException |
|
| 2073 | + * @throws EE_Error |
|
| 2074 | + * @throws ReflectionException |
|
| 2075 | + * @throws ReflectionException |
|
| 2076 | + * @throws ReflectionException |
|
| 2077 | + */ |
|
| 2078 | + protected static function _check_for_object($props_n_values, $classname, $timezone = null, $date_formats = array()) |
|
| 2079 | + { |
|
| 2080 | + $existing = null; |
|
| 2081 | + $model = self::_get_model($classname, $timezone); |
|
| 2082 | + if ($model->has_primary_key_field()) { |
|
| 2083 | + $primary_id_ref = self::_get_primary_key_name($classname); |
|
| 2084 | + if (array_key_exists($primary_id_ref, $props_n_values) |
|
| 2085 | + && ! empty($props_n_values[ $primary_id_ref ]) |
|
| 2086 | + ) { |
|
| 2087 | + $existing = $model->get_one_by_ID( |
|
| 2088 | + $props_n_values[ $primary_id_ref ] |
|
| 2089 | + ); |
|
| 2090 | + } |
|
| 2091 | + } elseif ($model->has_all_combined_primary_key_fields($props_n_values)) { |
|
| 2092 | + // no primary key on this model, but there's still a matching item in the DB |
|
| 2093 | + $existing = self::_get_model($classname, $timezone)->get_one_by_ID( |
|
| 2094 | + self::_get_model($classname, $timezone) |
|
| 2095 | + ->get_index_primary_key_string($props_n_values) |
|
| 2096 | + ); |
|
| 2097 | + } |
|
| 2098 | + if ($existing) { |
|
| 2099 | + // set date formats if present before setting values |
|
| 2100 | + if (! empty($date_formats) && is_array($date_formats)) { |
|
| 2101 | + $existing->set_date_format($date_formats[0]); |
|
| 2102 | + $existing->set_time_format($date_formats[1]); |
|
| 2103 | + } else { |
|
| 2104 | + // set default formats for date and time |
|
| 2105 | + $existing->set_date_format(get_option('date_format')); |
|
| 2106 | + $existing->set_time_format(get_option('time_format')); |
|
| 2107 | + } |
|
| 2108 | + foreach ($props_n_values as $property => $field_value) { |
|
| 2109 | + $existing->set($property, $field_value); |
|
| 2110 | + } |
|
| 2111 | + return $existing; |
|
| 2112 | + } |
|
| 2113 | + return false; |
|
| 2114 | + } |
|
| 2115 | + |
|
| 2116 | + |
|
| 2117 | + /** |
|
| 2118 | + * Gets the EEM_*_Model for this class |
|
| 2119 | + * |
|
| 2120 | + * @access public now, as this is more convenient |
|
| 2121 | + * @param $classname |
|
| 2122 | + * @param null $timezone |
|
| 2123 | + * @throws ReflectionException |
|
| 2124 | + * @throws InvalidArgumentException |
|
| 2125 | + * @throws InvalidInterfaceException |
|
| 2126 | + * @throws InvalidDataTypeException |
|
| 2127 | + * @throws EE_Error |
|
| 2128 | + * @return EEM_Base |
|
| 2129 | + */ |
|
| 2130 | + protected static function _get_model($classname, $timezone = null) |
|
| 2131 | + { |
|
| 2132 | + // find model for this class |
|
| 2133 | + if (! $classname) { |
|
| 2134 | + throw new EE_Error( |
|
| 2135 | + sprintf( |
|
| 2136 | + esc_html__( |
|
| 2137 | + 'What were you thinking calling _get_model(%s)?? You need to specify the class name', |
|
| 2138 | + 'event_espresso' |
|
| 2139 | + ), |
|
| 2140 | + $classname |
|
| 2141 | + ) |
|
| 2142 | + ); |
|
| 2143 | + } |
|
| 2144 | + $modelName = self::_get_model_classname($classname); |
|
| 2145 | + return self::_get_model_instance_with_name($modelName, $timezone); |
|
| 2146 | + } |
|
| 2147 | + |
|
| 2148 | + |
|
| 2149 | + /** |
|
| 2150 | + * Gets the model instance (eg instance of EEM_Attendee) given its classname (eg EE_Attendee) |
|
| 2151 | + * |
|
| 2152 | + * @param string $model_classname |
|
| 2153 | + * @param null $timezone |
|
| 2154 | + * @return EEM_Base |
|
| 2155 | + * @throws ReflectionException |
|
| 2156 | + * @throws InvalidArgumentException |
|
| 2157 | + * @throws InvalidInterfaceException |
|
| 2158 | + * @throws InvalidDataTypeException |
|
| 2159 | + * @throws EE_Error |
|
| 2160 | + */ |
|
| 2161 | + protected static function _get_model_instance_with_name($model_classname, $timezone = null) |
|
| 2162 | + { |
|
| 2163 | + $model_classname = str_replace('EEM_', '', $model_classname); |
|
| 2164 | + $model = EE_Registry::instance()->load_model($model_classname); |
|
| 2165 | + $model->set_timezone($timezone); |
|
| 2166 | + return $model; |
|
| 2167 | + } |
|
| 2168 | + |
|
| 2169 | + |
|
| 2170 | + /** |
|
| 2171 | + * If a model name is provided (eg Registration), gets the model classname for that model. |
|
| 2172 | + * Also works if a model class's classname is provided (eg EE_Registration). |
|
| 2173 | + * |
|
| 2174 | + * @param null $model_name |
|
| 2175 | + * @return string like EEM_Attendee |
|
| 2176 | + */ |
|
| 2177 | + private static function _get_model_classname($model_name = null) |
|
| 2178 | + { |
|
| 2179 | + if (strpos($model_name, 'EE_') === 0) { |
|
| 2180 | + $model_classname = str_replace('EE_', 'EEM_', $model_name); |
|
| 2181 | + } else { |
|
| 2182 | + $model_classname = 'EEM_' . $model_name; |
|
| 2183 | + } |
|
| 2184 | + return $model_classname; |
|
| 2185 | + } |
|
| 2186 | + |
|
| 2187 | + |
|
| 2188 | + /** |
|
| 2189 | + * returns the name of the primary key attribute |
|
| 2190 | + * |
|
| 2191 | + * @param null $classname |
|
| 2192 | + * @throws ReflectionException |
|
| 2193 | + * @throws InvalidArgumentException |
|
| 2194 | + * @throws InvalidInterfaceException |
|
| 2195 | + * @throws InvalidDataTypeException |
|
| 2196 | + * @throws EE_Error |
|
| 2197 | + * @return string |
|
| 2198 | + */ |
|
| 2199 | + protected static function _get_primary_key_name($classname = null) |
|
| 2200 | + { |
|
| 2201 | + if (! $classname) { |
|
| 2202 | + throw new EE_Error( |
|
| 2203 | + sprintf( |
|
| 2204 | + esc_html__('What were you thinking calling _get_primary_key_name(%s)', 'event_espresso'), |
|
| 2205 | + $classname |
|
| 2206 | + ) |
|
| 2207 | + ); |
|
| 2208 | + } |
|
| 2209 | + return self::_get_model($classname)->get_primary_key_field()->get_name(); |
|
| 2210 | + } |
|
| 2211 | + |
|
| 2212 | + |
|
| 2213 | + /** |
|
| 2214 | + * Gets the value of the primary key. |
|
| 2215 | + * If the object hasn't yet been saved, it should be whatever the model field's default was |
|
| 2216 | + * (eg, if this were the EE_Event class, look at the primary key field on EEM_Event and see what its default value |
|
| 2217 | + * is. Usually defaults for integer primary keys are 0; string primary keys are usually NULL). |
|
| 2218 | + * |
|
| 2219 | + * @return mixed, if the primary key is of type INT it'll be an int. Otherwise it could be a string |
|
| 2220 | + * @throws ReflectionException |
|
| 2221 | + * @throws InvalidArgumentException |
|
| 2222 | + * @throws InvalidInterfaceException |
|
| 2223 | + * @throws InvalidDataTypeException |
|
| 2224 | + * @throws EE_Error |
|
| 2225 | + */ |
|
| 2226 | + public function ID() |
|
| 2227 | + { |
|
| 2228 | + $model = $this->get_model(); |
|
| 2229 | + // now that we know the name of the variable, use a variable variable to get its value and return its |
|
| 2230 | + if ($model->has_primary_key_field()) { |
|
| 2231 | + return $this->_fields[ $model->primary_key_name() ]; |
|
| 2232 | + } |
|
| 2233 | + return $model->get_index_primary_key_string($this->_fields); |
|
| 2234 | + } |
|
| 2235 | + |
|
| 2236 | + |
|
| 2237 | + /** |
|
| 2238 | + * Adds a relationship to the specified EE_Base_Class object, given the relationship's name. Eg, if the current |
|
| 2239 | + * model is related to a group of events, the $relationName should be 'Event', and should be a key in the EE |
|
| 2240 | + * Model's $_model_relations array. If this model object doesn't exist in the DB, just caches the related thing |
|
| 2241 | + * |
|
| 2242 | + * @param mixed $otherObjectModelObjectOrID EE_Base_Class or the ID of the other object |
|
| 2243 | + * @param string $relationName eg 'Events','Question',etc. |
|
| 2244 | + * an attendee to a group, you also want to specify which role they |
|
| 2245 | + * will have in that group. So you would use this parameter to |
|
| 2246 | + * specify array('role-column-name'=>'role-id') |
|
| 2247 | + * @param array $extra_join_model_fields_n_values You can optionally include an array of key=>value pairs that |
|
| 2248 | + * allow you to further constrict the relation to being added. |
|
| 2249 | + * However, keep in mind that the columns (keys) given must match a |
|
| 2250 | + * column on the JOIN table and currently only the HABTM models |
|
| 2251 | + * accept these additional conditions. Also remember that if an |
|
| 2252 | + * exact match isn't found for these extra cols/val pairs, then a |
|
| 2253 | + * NEW row is created in the join table. |
|
| 2254 | + * @param null $cache_id |
|
| 2255 | + * @throws ReflectionException |
|
| 2256 | + * @throws InvalidArgumentException |
|
| 2257 | + * @throws InvalidInterfaceException |
|
| 2258 | + * @throws InvalidDataTypeException |
|
| 2259 | + * @throws EE_Error |
|
| 2260 | + * @return EE_Base_Class the object the relation was added to |
|
| 2261 | + */ |
|
| 2262 | + public function _add_relation_to( |
|
| 2263 | + $otherObjectModelObjectOrID, |
|
| 2264 | + $relationName, |
|
| 2265 | + $extra_join_model_fields_n_values = array(), |
|
| 2266 | + $cache_id = null |
|
| 2267 | + ) { |
|
| 2268 | + $model = $this->get_model(); |
|
| 2269 | + // if this thing exists in the DB, save the relation to the DB |
|
| 2270 | + if ($this->ID()) { |
|
| 2271 | + $otherObject = $model->add_relationship_to( |
|
| 2272 | + $this, |
|
| 2273 | + $otherObjectModelObjectOrID, |
|
| 2274 | + $relationName, |
|
| 2275 | + $extra_join_model_fields_n_values |
|
| 2276 | + ); |
|
| 2277 | + // clear cache so future get_many_related and get_first_related() return new results. |
|
| 2278 | + $this->clear_cache($relationName, $otherObject, true); |
|
| 2279 | + if ($otherObject instanceof EE_Base_Class) { |
|
| 2280 | + $otherObject->clear_cache($model->get_this_model_name(), $this); |
|
| 2281 | + } |
|
| 2282 | + } else { |
|
| 2283 | + // this thing doesn't exist in the DB, so just cache it |
|
| 2284 | + if (! $otherObjectModelObjectOrID instanceof EE_Base_Class) { |
|
| 2285 | + throw new EE_Error( |
|
| 2286 | + sprintf( |
|
| 2287 | + esc_html__( |
|
| 2288 | + 'Before a model object is saved to the database, calls to _add_relation_to must be passed an actual object, not just an ID. You provided %s as the model object to a %s', |
|
| 2289 | + 'event_espresso' |
|
| 2290 | + ), |
|
| 2291 | + $otherObjectModelObjectOrID, |
|
| 2292 | + get_class($this) |
|
| 2293 | + ) |
|
| 2294 | + ); |
|
| 2295 | + } |
|
| 2296 | + $otherObject = $otherObjectModelObjectOrID; |
|
| 2297 | + $this->cache($relationName, $otherObjectModelObjectOrID, $cache_id); |
|
| 2298 | + } |
|
| 2299 | + if ($otherObject instanceof EE_Base_Class) { |
|
| 2300 | + // fix the reciprocal relation too |
|
| 2301 | + if ($otherObject->ID()) { |
|
| 2302 | + // its saved so assumed relations exist in the DB, so we can just |
|
| 2303 | + // clear the cache so future queries use the updated info in the DB |
|
| 2304 | + $otherObject->clear_cache( |
|
| 2305 | + $model->get_this_model_name(), |
|
| 2306 | + null, |
|
| 2307 | + true |
|
| 2308 | + ); |
|
| 2309 | + } else { |
|
| 2310 | + // it's not saved, so it caches relations like this |
|
| 2311 | + $otherObject->cache($model->get_this_model_name(), $this); |
|
| 2312 | + } |
|
| 2313 | + } |
|
| 2314 | + return $otherObject; |
|
| 2315 | + } |
|
| 2316 | + |
|
| 2317 | + |
|
| 2318 | + /** |
|
| 2319 | + * Removes a relationship to the specified EE_Base_Class object, given the relationships' name. Eg, if the current |
|
| 2320 | + * model is related to a group of events, the $relationName should be 'Events', and should be a key in the EE |
|
| 2321 | + * Model's $_model_relations array. If this model object doesn't exist in the DB, just removes the related thing |
|
| 2322 | + * from the cache |
|
| 2323 | + * |
|
| 2324 | + * @param mixed $otherObjectModelObjectOrID |
|
| 2325 | + * EE_Base_Class or the ID of the other object, OR an array key into the cache if this isn't saved |
|
| 2326 | + * to the DB yet |
|
| 2327 | + * @param string $relationName |
|
| 2328 | + * @param array $where_query |
|
| 2329 | + * You can optionally include an array of key=>value pairs that allow you to further constrict the |
|
| 2330 | + * relation to being added. However, keep in mind that the columns (keys) given must match a column |
|
| 2331 | + * on the JOIN table and currently only the HABTM models accept these additional conditions. Also |
|
| 2332 | + * remember that if an exact match isn't found for these extra cols/val pairs, then no row is |
|
| 2333 | + * deleted. |
|
| 2334 | + * @return EE_Base_Class the relation was removed from |
|
| 2335 | + * @throws ReflectionException |
|
| 2336 | + * @throws InvalidArgumentException |
|
| 2337 | + * @throws InvalidInterfaceException |
|
| 2338 | + * @throws InvalidDataTypeException |
|
| 2339 | + * @throws EE_Error |
|
| 2340 | + */ |
|
| 2341 | + public function _remove_relation_to($otherObjectModelObjectOrID, $relationName, $where_query = array()) |
|
| 2342 | + { |
|
| 2343 | + if ($this->ID()) { |
|
| 2344 | + // if this exists in the DB, save the relation change to the DB too |
|
| 2345 | + $otherObject = $this->get_model()->remove_relationship_to( |
|
| 2346 | + $this, |
|
| 2347 | + $otherObjectModelObjectOrID, |
|
| 2348 | + $relationName, |
|
| 2349 | + $where_query |
|
| 2350 | + ); |
|
| 2351 | + $this->clear_cache( |
|
| 2352 | + $relationName, |
|
| 2353 | + $otherObject |
|
| 2354 | + ); |
|
| 2355 | + } else { |
|
| 2356 | + // this doesn't exist in the DB, just remove it from the cache |
|
| 2357 | + $otherObject = $this->clear_cache( |
|
| 2358 | + $relationName, |
|
| 2359 | + $otherObjectModelObjectOrID |
|
| 2360 | + ); |
|
| 2361 | + } |
|
| 2362 | + if ($otherObject instanceof EE_Base_Class) { |
|
| 2363 | + $otherObject->clear_cache( |
|
| 2364 | + $this->get_model()->get_this_model_name(), |
|
| 2365 | + $this |
|
| 2366 | + ); |
|
| 2367 | + } |
|
| 2368 | + return $otherObject; |
|
| 2369 | + } |
|
| 2370 | + |
|
| 2371 | + |
|
| 2372 | + /** |
|
| 2373 | + * Removes ALL the related things for the $relationName. |
|
| 2374 | + * |
|
| 2375 | + * @param string $relationName |
|
| 2376 | + * @param array $where_query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
| 2377 | + * @return EE_Base_Class |
|
| 2378 | + * @throws ReflectionException |
|
| 2379 | + * @throws InvalidArgumentException |
|
| 2380 | + * @throws InvalidInterfaceException |
|
| 2381 | + * @throws InvalidDataTypeException |
|
| 2382 | + * @throws EE_Error |
|
| 2383 | + */ |
|
| 2384 | + public function _remove_relations($relationName, $where_query_params = array()) |
|
| 2385 | + { |
|
| 2386 | + if ($this->ID()) { |
|
| 2387 | + // if this exists in the DB, save the relation change to the DB too |
|
| 2388 | + $otherObjects = $this->get_model()->remove_relations( |
|
| 2389 | + $this, |
|
| 2390 | + $relationName, |
|
| 2391 | + $where_query_params |
|
| 2392 | + ); |
|
| 2393 | + $this->clear_cache( |
|
| 2394 | + $relationName, |
|
| 2395 | + null, |
|
| 2396 | + true |
|
| 2397 | + ); |
|
| 2398 | + } else { |
|
| 2399 | + // this doesn't exist in the DB, just remove it from the cache |
|
| 2400 | + $otherObjects = $this->clear_cache( |
|
| 2401 | + $relationName, |
|
| 2402 | + null, |
|
| 2403 | + true |
|
| 2404 | + ); |
|
| 2405 | + } |
|
| 2406 | + if (is_array($otherObjects)) { |
|
| 2407 | + foreach ($otherObjects as $otherObject) { |
|
| 2408 | + $otherObject->clear_cache( |
|
| 2409 | + $this->get_model()->get_this_model_name(), |
|
| 2410 | + $this |
|
| 2411 | + ); |
|
| 2412 | + } |
|
| 2413 | + } |
|
| 2414 | + return $otherObjects; |
|
| 2415 | + } |
|
| 2416 | + |
|
| 2417 | + |
|
| 2418 | + /** |
|
| 2419 | + * Gets all the related model objects of the specified type. Eg, if the current class if |
|
| 2420 | + * EE_Event, you could call $this->get_many_related('Registration') to get an array of all the |
|
| 2421 | + * EE_Registration objects which related to this event. Note: by default, we remove the "default query params" |
|
| 2422 | + * because we want to get even deleted items etc. |
|
| 2423 | + * |
|
| 2424 | + * @param string $relationName key in the model's _model_relations array |
|
| 2425 | + * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
| 2426 | + * @return EE_Base_Class[] Results not necessarily indexed by IDs, because some results might not have primary |
|
| 2427 | + * keys or might not be saved yet. Consider using EEM_Base::get_IDs() on these |
|
| 2428 | + * results if you want IDs |
|
| 2429 | + * @throws ReflectionException |
|
| 2430 | + * @throws InvalidArgumentException |
|
| 2431 | + * @throws InvalidInterfaceException |
|
| 2432 | + * @throws InvalidDataTypeException |
|
| 2433 | + * @throws EE_Error |
|
| 2434 | + */ |
|
| 2435 | + public function get_many_related($relationName, $query_params = array()) |
|
| 2436 | + { |
|
| 2437 | + if ($this->ID()) { |
|
| 2438 | + // this exists in the DB, so get the related things from either the cache or the DB |
|
| 2439 | + // if there are query parameters, forget about caching the related model objects. |
|
| 2440 | + if ($query_params) { |
|
| 2441 | + $related_model_objects = $this->get_model()->get_all_related( |
|
| 2442 | + $this, |
|
| 2443 | + $relationName, |
|
| 2444 | + $query_params |
|
| 2445 | + ); |
|
| 2446 | + } else { |
|
| 2447 | + // did we already cache the result of this query? |
|
| 2448 | + $cached_results = $this->get_all_from_cache($relationName); |
|
| 2449 | + if (! $cached_results) { |
|
| 2450 | + $related_model_objects = $this->get_model()->get_all_related( |
|
| 2451 | + $this, |
|
| 2452 | + $relationName, |
|
| 2453 | + $query_params |
|
| 2454 | + ); |
|
| 2455 | + // if no query parameters were passed, then we got all the related model objects |
|
| 2456 | + // for that relation. We can cache them then. |
|
| 2457 | + foreach ($related_model_objects as $related_model_object) { |
|
| 2458 | + $this->cache($relationName, $related_model_object); |
|
| 2459 | + } |
|
| 2460 | + } else { |
|
| 2461 | + $related_model_objects = $cached_results; |
|
| 2462 | + } |
|
| 2463 | + } |
|
| 2464 | + } else { |
|
| 2465 | + // this doesn't exist in the DB, so just get the related things from the cache |
|
| 2466 | + $related_model_objects = $this->get_all_from_cache($relationName); |
|
| 2467 | + } |
|
| 2468 | + return $related_model_objects; |
|
| 2469 | + } |
|
| 2470 | + |
|
| 2471 | + |
|
| 2472 | + /** |
|
| 2473 | + * Instead of getting the related model objects, simply counts them. Ignores default_where_conditions by default, |
|
| 2474 | + * unless otherwise specified in the $query_params |
|
| 2475 | + * |
|
| 2476 | + * @param string $relation_name model_name like 'Event', or 'Registration' |
|
| 2477 | + * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 2478 | + * @param string $field_to_count name of field to count by. By default, uses primary key |
|
| 2479 | + * @param bool $distinct if we want to only count the distinct values for the column then you can trigger |
|
| 2480 | + * that by the setting $distinct to TRUE; |
|
| 2481 | + * @return int |
|
| 2482 | + * @throws ReflectionException |
|
| 2483 | + * @throws InvalidArgumentException |
|
| 2484 | + * @throws InvalidInterfaceException |
|
| 2485 | + * @throws InvalidDataTypeException |
|
| 2486 | + * @throws EE_Error |
|
| 2487 | + */ |
|
| 2488 | + public function count_related($relation_name, $query_params = array(), $field_to_count = null, $distinct = false) |
|
| 2489 | + { |
|
| 2490 | + return $this->get_model()->count_related( |
|
| 2491 | + $this, |
|
| 2492 | + $relation_name, |
|
| 2493 | + $query_params, |
|
| 2494 | + $field_to_count, |
|
| 2495 | + $distinct |
|
| 2496 | + ); |
|
| 2497 | + } |
|
| 2498 | + |
|
| 2499 | + |
|
| 2500 | + /** |
|
| 2501 | + * Instead of getting the related model objects, simply sums up the values of the specified field. |
|
| 2502 | + * Note: ignores default_where_conditions by default, unless otherwise specified in the $query_params |
|
| 2503 | + * |
|
| 2504 | + * @param string $relation_name model_name like 'Event', or 'Registration' |
|
| 2505 | + * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 2506 | + * @param string $field_to_sum name of field to count by. |
|
| 2507 | + * By default, uses primary key |
|
| 2508 | + * (which doesn't make much sense, so you should probably change it) |
|
| 2509 | + * @return int |
|
| 2510 | + * @throws ReflectionException |
|
| 2511 | + * @throws InvalidArgumentException |
|
| 2512 | + * @throws InvalidInterfaceException |
|
| 2513 | + * @throws InvalidDataTypeException |
|
| 2514 | + * @throws EE_Error |
|
| 2515 | + */ |
|
| 2516 | + public function sum_related($relation_name, $query_params = array(), $field_to_sum = null) |
|
| 2517 | + { |
|
| 2518 | + return $this->get_model()->sum_related( |
|
| 2519 | + $this, |
|
| 2520 | + $relation_name, |
|
| 2521 | + $query_params, |
|
| 2522 | + $field_to_sum |
|
| 2523 | + ); |
|
| 2524 | + } |
|
| 2525 | + |
|
| 2526 | + |
|
| 2527 | + /** |
|
| 2528 | + * Gets the first (ie, one) related model object of the specified type. |
|
| 2529 | + * |
|
| 2530 | + * @param string $relationName key in the model's _model_relations array |
|
| 2531 | + * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 2532 | + * @return EE_Base_Class (not an array, a single object) |
|
| 2533 | + * @throws ReflectionException |
|
| 2534 | + * @throws InvalidArgumentException |
|
| 2535 | + * @throws InvalidInterfaceException |
|
| 2536 | + * @throws InvalidDataTypeException |
|
| 2537 | + * @throws EE_Error |
|
| 2538 | + */ |
|
| 2539 | + public function get_first_related($relationName, $query_params = array()) |
|
| 2540 | + { |
|
| 2541 | + $model = $this->get_model(); |
|
| 2542 | + if ($this->ID()) {// this exists in the DB, get from the cache OR the DB |
|
| 2543 | + // if they've provided some query parameters, don't bother trying to cache the result |
|
| 2544 | + // also make sure we're not caching the result of get_first_related |
|
| 2545 | + // on a relation which should have an array of objects (because the cache might have an array of objects) |
|
| 2546 | + if ($query_params |
|
| 2547 | + || ! $model->related_settings_for($relationName) |
|
| 2548 | + instanceof |
|
| 2549 | + EE_Belongs_To_Relation |
|
| 2550 | + ) { |
|
| 2551 | + $related_model_object = $model->get_first_related( |
|
| 2552 | + $this, |
|
| 2553 | + $relationName, |
|
| 2554 | + $query_params |
|
| 2555 | + ); |
|
| 2556 | + } else { |
|
| 2557 | + // first, check if we've already cached the result of this query |
|
| 2558 | + $cached_result = $this->get_one_from_cache($relationName); |
|
| 2559 | + if (! $cached_result) { |
|
| 2560 | + $related_model_object = $model->get_first_related( |
|
| 2561 | + $this, |
|
| 2562 | + $relationName, |
|
| 2563 | + $query_params |
|
| 2564 | + ); |
|
| 2565 | + $this->cache($relationName, $related_model_object); |
|
| 2566 | + } else { |
|
| 2567 | + $related_model_object = $cached_result; |
|
| 2568 | + } |
|
| 2569 | + } |
|
| 2570 | + } else { |
|
| 2571 | + $related_model_object = null; |
|
| 2572 | + // this doesn't exist in the Db, |
|
| 2573 | + // but maybe the relation is of type belongs to, and so the related thing might |
|
| 2574 | + if ($model->related_settings_for($relationName) instanceof EE_Belongs_To_Relation) { |
|
| 2575 | + $related_model_object = $model->get_first_related( |
|
| 2576 | + $this, |
|
| 2577 | + $relationName, |
|
| 2578 | + $query_params |
|
| 2579 | + ); |
|
| 2580 | + } |
|
| 2581 | + // this doesn't exist in the DB and apparently the thing it belongs to doesn't either, |
|
| 2582 | + // just get what's cached on this object |
|
| 2583 | + if (! $related_model_object) { |
|
| 2584 | + $related_model_object = $this->get_one_from_cache($relationName); |
|
| 2585 | + } |
|
| 2586 | + } |
|
| 2587 | + return $related_model_object; |
|
| 2588 | + } |
|
| 2589 | + |
|
| 2590 | + |
|
| 2591 | + /** |
|
| 2592 | + * Does a delete on all related objects of type $relationName and removes |
|
| 2593 | + * the current model object's relation to them. If they can't be deleted (because |
|
| 2594 | + * of blocking related model objects) does nothing. If the related model objects are |
|
| 2595 | + * soft-deletable, they will be soft-deleted regardless of related blocking model objects. |
|
| 2596 | + * If this model object doesn't exist yet in the DB, just removes its related things |
|
| 2597 | + * |
|
| 2598 | + * @param string $relationName |
|
| 2599 | + * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 2600 | + * @return int how many deleted |
|
| 2601 | + * @throws ReflectionException |
|
| 2602 | + * @throws InvalidArgumentException |
|
| 2603 | + * @throws InvalidInterfaceException |
|
| 2604 | + * @throws InvalidDataTypeException |
|
| 2605 | + * @throws EE_Error |
|
| 2606 | + */ |
|
| 2607 | + public function delete_related($relationName, $query_params = array()) |
|
| 2608 | + { |
|
| 2609 | + if ($this->ID()) { |
|
| 2610 | + $count = $this->get_model()->delete_related( |
|
| 2611 | + $this, |
|
| 2612 | + $relationName, |
|
| 2613 | + $query_params |
|
| 2614 | + ); |
|
| 2615 | + } else { |
|
| 2616 | + $count = count($this->get_all_from_cache($relationName)); |
|
| 2617 | + $this->clear_cache($relationName, null, true); |
|
| 2618 | + } |
|
| 2619 | + return $count; |
|
| 2620 | + } |
|
| 2621 | + |
|
| 2622 | + |
|
| 2623 | + /** |
|
| 2624 | + * Does a hard delete (ie, removes the DB row) on all related objects of type $relationName and removes |
|
| 2625 | + * the current model object's relation to them. If they can't be deleted (because |
|
| 2626 | + * of blocking related model objects) just does a soft delete on it instead, if possible. |
|
| 2627 | + * If the related thing isn't a soft-deletable model object, this function is identical |
|
| 2628 | + * to delete_related(). If this model object doesn't exist in the DB, just remove its related things |
|
| 2629 | + * |
|
| 2630 | + * @param string $relationName |
|
| 2631 | + * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 2632 | + * @return int how many deleted (including those soft deleted) |
|
| 2633 | + * @throws ReflectionException |
|
| 2634 | + * @throws InvalidArgumentException |
|
| 2635 | + * @throws InvalidInterfaceException |
|
| 2636 | + * @throws InvalidDataTypeException |
|
| 2637 | + * @throws EE_Error |
|
| 2638 | + */ |
|
| 2639 | + public function delete_related_permanently($relationName, $query_params = array()) |
|
| 2640 | + { |
|
| 2641 | + if ($this->ID()) { |
|
| 2642 | + $count = $this->get_model()->delete_related_permanently( |
|
| 2643 | + $this, |
|
| 2644 | + $relationName, |
|
| 2645 | + $query_params |
|
| 2646 | + ); |
|
| 2647 | + } else { |
|
| 2648 | + $count = count($this->get_all_from_cache($relationName)); |
|
| 2649 | + } |
|
| 2650 | + $this->clear_cache($relationName, null, true); |
|
| 2651 | + return $count; |
|
| 2652 | + } |
|
| 2653 | + |
|
| 2654 | + |
|
| 2655 | + /** |
|
| 2656 | + * is_set |
|
| 2657 | + * Just a simple utility function children can use for checking if property exists |
|
| 2658 | + * |
|
| 2659 | + * @access public |
|
| 2660 | + * @param string $field_name property to check |
|
| 2661 | + * @return bool TRUE if existing,FALSE if not. |
|
| 2662 | + */ |
|
| 2663 | + public function is_set($field_name) |
|
| 2664 | + { |
|
| 2665 | + return isset($this->_fields[ $field_name ]); |
|
| 2666 | + } |
|
| 2667 | + |
|
| 2668 | + |
|
| 2669 | + /** |
|
| 2670 | + * Just a simple utility function children can use for checking if property (or properties) exists and throwing an |
|
| 2671 | + * EE_Error exception if they don't |
|
| 2672 | + * |
|
| 2673 | + * @param mixed (string|array) $properties properties to check |
|
| 2674 | + * @throws EE_Error |
|
| 2675 | + * @return bool TRUE if existing, throw EE_Error if not. |
|
| 2676 | + */ |
|
| 2677 | + protected function _property_exists($properties) |
|
| 2678 | + { |
|
| 2679 | + foreach ((array) $properties as $property_name) { |
|
| 2680 | + // first make sure this property exists |
|
| 2681 | + if (! $this->_fields[ $property_name ]) { |
|
| 2682 | + throw new EE_Error( |
|
| 2683 | + sprintf( |
|
| 2684 | + esc_html__( |
|
| 2685 | + 'Trying to retrieve a non-existent property (%s). Double check the spelling please', |
|
| 2686 | + 'event_espresso' |
|
| 2687 | + ), |
|
| 2688 | + $property_name |
|
| 2689 | + ) |
|
| 2690 | + ); |
|
| 2691 | + } |
|
| 2692 | + } |
|
| 2693 | + return true; |
|
| 2694 | + } |
|
| 2695 | + |
|
| 2696 | + |
|
| 2697 | + /** |
|
| 2698 | + * This simply returns an array of model fields for this object |
|
| 2699 | + * |
|
| 2700 | + * @return array |
|
| 2701 | + * @throws ReflectionException |
|
| 2702 | + * @throws InvalidArgumentException |
|
| 2703 | + * @throws InvalidInterfaceException |
|
| 2704 | + * @throws InvalidDataTypeException |
|
| 2705 | + * @throws EE_Error |
|
| 2706 | + */ |
|
| 2707 | + public function model_field_array() |
|
| 2708 | + { |
|
| 2709 | + $fields = $this->get_model()->field_settings(false); |
|
| 2710 | + $properties = array(); |
|
| 2711 | + // remove prepended underscore |
|
| 2712 | + foreach ($fields as $field_name => $settings) { |
|
| 2713 | + $properties[ $field_name ] = $this->get($field_name); |
|
| 2714 | + } |
|
| 2715 | + return $properties; |
|
| 2716 | + } |
|
| 2717 | + |
|
| 2718 | + |
|
| 2719 | + /** |
|
| 2720 | + * Very handy general function to allow for plugins to extend any child of EE_Base_Class. |
|
| 2721 | + * If a method is called on a child of EE_Base_Class that doesn't exist, this function is called |
|
| 2722 | + * (http://www.garfieldtech.com/blog/php-magic-call) and passed the method's name and arguments. |
|
| 2723 | + * Instead of requiring a plugin to extend the EE_Base_Class |
|
| 2724 | + * (which works fine is there's only 1 plugin, but when will that happen?) |
|
| 2725 | + * they can add a hook onto 'filters_hook_espresso__{className}__{methodName}' |
|
| 2726 | + * (eg, filters_hook_espresso__EE_Answer__my_great_function) |
|
| 2727 | + * and accepts 2 arguments: the object on which the function was called, |
|
| 2728 | + * and an array of the original arguments passed to the function. |
|
| 2729 | + * Whatever their callback function returns will be returned by this function. |
|
| 2730 | + * Example: in functions.php (or in a plugin): |
|
| 2731 | + * add_filter('FHEE__EE_Answer__my_callback','my_callback',10,3); |
|
| 2732 | + * function my_callback($previousReturnValue,EE_Base_Class $object,$argsArray){ |
|
| 2733 | + * $returnString= "you called my_callback! and passed args:".implode(",",$argsArray); |
|
| 2734 | + * return $previousReturnValue.$returnString; |
|
| 2735 | + * } |
|
| 2736 | + * require('EE_Answer.class.php'); |
|
| 2737 | + * $answer= EE_Answer::new_instance(array('REG_ID' => 2,'QST_ID' => 3,'ANS_value' => The answer is 42')); |
|
| 2738 | + * echo $answer->my_callback('monkeys',100); |
|
| 2739 | + * //will output "you called my_callback! and passed args:monkeys,100" |
|
| 2740 | + * |
|
| 2741 | + * @param string $methodName name of method which was called on a child of EE_Base_Class, but which |
|
| 2742 | + * @param array $args array of original arguments passed to the function |
|
| 2743 | + * @throws EE_Error |
|
| 2744 | + * @return mixed whatever the plugin which calls add_filter decides |
|
| 2745 | + */ |
|
| 2746 | + public function __call($methodName, $args) |
|
| 2747 | + { |
|
| 2748 | + $className = get_class($this); |
|
| 2749 | + $tagName = "FHEE__{$className}__{$methodName}"; |
|
| 2750 | + if (! has_filter($tagName)) { |
|
| 2751 | + throw new EE_Error( |
|
| 2752 | + sprintf( |
|
| 2753 | + esc_html__( |
|
| 2754 | + "Method %s on class %s does not exist! You can create one with the following code in functions.php or in a plugin: add_filter('%s','my_callback',10,3);function my_callback(\$previousReturnValue,EE_Base_Class \$object, \$argsArray){/*function body*/return \$whatever;}", |
|
| 2755 | + 'event_espresso' |
|
| 2756 | + ), |
|
| 2757 | + $methodName, |
|
| 2758 | + $className, |
|
| 2759 | + $tagName |
|
| 2760 | + ) |
|
| 2761 | + ); |
|
| 2762 | + } |
|
| 2763 | + return apply_filters($tagName, null, $this, $args); |
|
| 2764 | + } |
|
| 2765 | + |
|
| 2766 | + |
|
| 2767 | + /** |
|
| 2768 | + * Similar to insert_post_meta, adds a record in the Extra_Meta model's table with the given key and value. |
|
| 2769 | + * A $previous_value can be specified in case there are many meta rows with the same key |
|
| 2770 | + * |
|
| 2771 | + * @param string $meta_key |
|
| 2772 | + * @param mixed $meta_value |
|
| 2773 | + * @param mixed $previous_value |
|
| 2774 | + * @return bool|int # of records updated (or BOOLEAN if we actually ended up inserting the extra meta row) |
|
| 2775 | + * NOTE: if the values haven't changed, returns 0 |
|
| 2776 | + * @throws InvalidArgumentException |
|
| 2777 | + * @throws InvalidInterfaceException |
|
| 2778 | + * @throws InvalidDataTypeException |
|
| 2779 | + * @throws EE_Error |
|
| 2780 | + * @throws ReflectionException |
|
| 2781 | + */ |
|
| 2782 | + public function update_extra_meta($meta_key, $meta_value, $previous_value = null) |
|
| 2783 | + { |
|
| 2784 | + $query_params = array( |
|
| 2785 | + array( |
|
| 2786 | + 'EXM_key' => $meta_key, |
|
| 2787 | + 'OBJ_ID' => $this->ID(), |
|
| 2788 | + 'EXM_type' => $this->get_model()->get_this_model_name(), |
|
| 2789 | + ), |
|
| 2790 | + ); |
|
| 2791 | + if ($previous_value !== null) { |
|
| 2792 | + $query_params[0]['EXM_value'] = $meta_value; |
|
| 2793 | + } |
|
| 2794 | + $existing_rows_like_that = EEM_Extra_Meta::instance()->get_all($query_params); |
|
| 2795 | + if (! $existing_rows_like_that) { |
|
| 2796 | + return $this->add_extra_meta($meta_key, $meta_value); |
|
| 2797 | + } |
|
| 2798 | + foreach ($existing_rows_like_that as $existing_row) { |
|
| 2799 | + $existing_row->save(array('EXM_value' => $meta_value)); |
|
| 2800 | + } |
|
| 2801 | + return count($existing_rows_like_that); |
|
| 2802 | + } |
|
| 2803 | + |
|
| 2804 | + |
|
| 2805 | + /** |
|
| 2806 | + * Adds a new extra meta record. If $unique is set to TRUE, we'll first double-check |
|
| 2807 | + * no other extra meta for this model object have the same key. Returns TRUE if the |
|
| 2808 | + * extra meta row was entered, false if not |
|
| 2809 | + * |
|
| 2810 | + * @param string $meta_key |
|
| 2811 | + * @param mixed $meta_value |
|
| 2812 | + * @param boolean $unique |
|
| 2813 | + * @return boolean |
|
| 2814 | + * @throws InvalidArgumentException |
|
| 2815 | + * @throws InvalidInterfaceException |
|
| 2816 | + * @throws InvalidDataTypeException |
|
| 2817 | + * @throws EE_Error |
|
| 2818 | + * @throws ReflectionException |
|
| 2819 | + * @throws ReflectionException |
|
| 2820 | + */ |
|
| 2821 | + public function add_extra_meta($meta_key, $meta_value, $unique = false) |
|
| 2822 | + { |
|
| 2823 | + if ($unique) { |
|
| 2824 | + $existing_extra_meta = EEM_Extra_Meta::instance()->get_one( |
|
| 2825 | + array( |
|
| 2826 | + array( |
|
| 2827 | + 'EXM_key' => $meta_key, |
|
| 2828 | + 'OBJ_ID' => $this->ID(), |
|
| 2829 | + 'EXM_type' => $this->get_model()->get_this_model_name(), |
|
| 2830 | + ), |
|
| 2831 | + ) |
|
| 2832 | + ); |
|
| 2833 | + if ($existing_extra_meta) { |
|
| 2834 | + return false; |
|
| 2835 | + } |
|
| 2836 | + } |
|
| 2837 | + $new_extra_meta = EE_Extra_Meta::new_instance( |
|
| 2838 | + array( |
|
| 2839 | + 'EXM_key' => $meta_key, |
|
| 2840 | + 'EXM_value' => $meta_value, |
|
| 2841 | + 'OBJ_ID' => $this->ID(), |
|
| 2842 | + 'EXM_type' => $this->get_model()->get_this_model_name(), |
|
| 2843 | + ) |
|
| 2844 | + ); |
|
| 2845 | + $new_extra_meta->save(); |
|
| 2846 | + return true; |
|
| 2847 | + } |
|
| 2848 | + |
|
| 2849 | + |
|
| 2850 | + /** |
|
| 2851 | + * Deletes all the extra meta rows for this record as specified by key. If $meta_value |
|
| 2852 | + * is specified, only deletes extra meta records with that value. |
|
| 2853 | + * |
|
| 2854 | + * @param string $meta_key |
|
| 2855 | + * @param mixed $meta_value |
|
| 2856 | + * @return int number of extra meta rows deleted |
|
| 2857 | + * @throws InvalidArgumentException |
|
| 2858 | + * @throws InvalidInterfaceException |
|
| 2859 | + * @throws InvalidDataTypeException |
|
| 2860 | + * @throws EE_Error |
|
| 2861 | + * @throws ReflectionException |
|
| 2862 | + */ |
|
| 2863 | + public function delete_extra_meta($meta_key, $meta_value = null) |
|
| 2864 | + { |
|
| 2865 | + $query_params = array( |
|
| 2866 | + array( |
|
| 2867 | + 'EXM_key' => $meta_key, |
|
| 2868 | + 'OBJ_ID' => $this->ID(), |
|
| 2869 | + 'EXM_type' => $this->get_model()->get_this_model_name(), |
|
| 2870 | + ), |
|
| 2871 | + ); |
|
| 2872 | + if ($meta_value !== null) { |
|
| 2873 | + $query_params[0]['EXM_value'] = $meta_value; |
|
| 2874 | + } |
|
| 2875 | + return EEM_Extra_Meta::instance()->delete($query_params); |
|
| 2876 | + } |
|
| 2877 | + |
|
| 2878 | + |
|
| 2879 | + /** |
|
| 2880 | + * Gets the extra meta with the given meta key. If you specify "single" we just return 1, otherwise |
|
| 2881 | + * an array of everything found. Requires that this model actually have a relation of type EE_Has_Many_Any_Relation. |
|
| 2882 | + * You can specify $default is case you haven't found the extra meta |
|
| 2883 | + * |
|
| 2884 | + * @param string $meta_key |
|
| 2885 | + * @param boolean $single |
|
| 2886 | + * @param mixed $default if we don't find anything, what should we return? |
|
| 2887 | + * @return mixed single value if $single; array if ! $single |
|
| 2888 | + * @throws ReflectionException |
|
| 2889 | + * @throws InvalidArgumentException |
|
| 2890 | + * @throws InvalidInterfaceException |
|
| 2891 | + * @throws InvalidDataTypeException |
|
| 2892 | + * @throws EE_Error |
|
| 2893 | + */ |
|
| 2894 | + public function get_extra_meta($meta_key, $single = false, $default = null) |
|
| 2895 | + { |
|
| 2896 | + if ($single) { |
|
| 2897 | + $result = $this->get_first_related( |
|
| 2898 | + 'Extra_Meta', |
|
| 2899 | + array(array('EXM_key' => $meta_key)) |
|
| 2900 | + ); |
|
| 2901 | + if ($result instanceof EE_Extra_Meta) { |
|
| 2902 | + return $result->value(); |
|
| 2903 | + } |
|
| 2904 | + } else { |
|
| 2905 | + $results = $this->get_many_related( |
|
| 2906 | + 'Extra_Meta', |
|
| 2907 | + array(array('EXM_key' => $meta_key)) |
|
| 2908 | + ); |
|
| 2909 | + if ($results) { |
|
| 2910 | + $values = array(); |
|
| 2911 | + foreach ($results as $result) { |
|
| 2912 | + if ($result instanceof EE_Extra_Meta) { |
|
| 2913 | + $values[ $result->ID() ] = $result->value(); |
|
| 2914 | + } |
|
| 2915 | + } |
|
| 2916 | + return $values; |
|
| 2917 | + } |
|
| 2918 | + } |
|
| 2919 | + // if nothing discovered yet return default. |
|
| 2920 | + return apply_filters( |
|
| 2921 | + 'FHEE__EE_Base_Class__get_extra_meta__default_value', |
|
| 2922 | + $default, |
|
| 2923 | + $meta_key, |
|
| 2924 | + $single, |
|
| 2925 | + $this |
|
| 2926 | + ); |
|
| 2927 | + } |
|
| 2928 | + |
|
| 2929 | + |
|
| 2930 | + /** |
|
| 2931 | + * Returns a simple array of all the extra meta associated with this model object. |
|
| 2932 | + * If $one_of_each_key is true (Default), it will be an array of simple key-value pairs, keys being the |
|
| 2933 | + * extra meta's key, and teh value being its value. However, if there are duplicate extra meta rows with |
|
| 2934 | + * the same key, only one will be used. (eg array('foo'=>'bar','monkey'=>123)) |
|
| 2935 | + * If $one_of_each_key is false, it will return an array with the top-level keys being |
|
| 2936 | + * the extra meta keys, but their values are also arrays, which have the extra-meta's ID as their sub-key, and |
|
| 2937 | + * finally the extra meta's value as each sub-value. (eg |
|
| 2938 | + * array('foo'=>array(1=>'bar',2=>'bill'),'monkey'=>array(3=>123))) |
|
| 2939 | + * |
|
| 2940 | + * @param boolean $one_of_each_key |
|
| 2941 | + * @return array |
|
| 2942 | + * @throws ReflectionException |
|
| 2943 | + * @throws InvalidArgumentException |
|
| 2944 | + * @throws InvalidInterfaceException |
|
| 2945 | + * @throws InvalidDataTypeException |
|
| 2946 | + * @throws EE_Error |
|
| 2947 | + */ |
|
| 2948 | + public function all_extra_meta_array($one_of_each_key = true) |
|
| 2949 | + { |
|
| 2950 | + $return_array = array(); |
|
| 2951 | + if ($one_of_each_key) { |
|
| 2952 | + $extra_meta_objs = $this->get_many_related( |
|
| 2953 | + 'Extra_Meta', |
|
| 2954 | + array('group_by' => 'EXM_key') |
|
| 2955 | + ); |
|
| 2956 | + foreach ($extra_meta_objs as $extra_meta_obj) { |
|
| 2957 | + if ($extra_meta_obj instanceof EE_Extra_Meta) { |
|
| 2958 | + $return_array[ $extra_meta_obj->key() ] = $extra_meta_obj->value(); |
|
| 2959 | + } |
|
| 2960 | + } |
|
| 2961 | + } else { |
|
| 2962 | + $extra_meta_objs = $this->get_many_related('Extra_Meta'); |
|
| 2963 | + foreach ($extra_meta_objs as $extra_meta_obj) { |
|
| 2964 | + if ($extra_meta_obj instanceof EE_Extra_Meta) { |
|
| 2965 | + if (! isset($return_array[ $extra_meta_obj->key() ])) { |
|
| 2966 | + $return_array[ $extra_meta_obj->key() ] = array(); |
|
| 2967 | + } |
|
| 2968 | + $return_array[ $extra_meta_obj->key() ][ $extra_meta_obj->ID() ] = $extra_meta_obj->value(); |
|
| 2969 | + } |
|
| 2970 | + } |
|
| 2971 | + } |
|
| 2972 | + return $return_array; |
|
| 2973 | + } |
|
| 2974 | + |
|
| 2975 | + |
|
| 2976 | + /** |
|
| 2977 | + * Gets a pretty nice displayable nice for this model object. Often overridden |
|
| 2978 | + * |
|
| 2979 | + * @return string |
|
| 2980 | + * @throws ReflectionException |
|
| 2981 | + * @throws InvalidArgumentException |
|
| 2982 | + * @throws InvalidInterfaceException |
|
| 2983 | + * @throws InvalidDataTypeException |
|
| 2984 | + * @throws EE_Error |
|
| 2985 | + */ |
|
| 2986 | + public function name() |
|
| 2987 | + { |
|
| 2988 | + // find a field that's not a text field |
|
| 2989 | + $field_we_can_use = $this->get_model()->get_a_field_of_type('EE_Text_Field_Base'); |
|
| 2990 | + if ($field_we_can_use) { |
|
| 2991 | + return $this->get($field_we_can_use->get_name()); |
|
| 2992 | + } |
|
| 2993 | + $first_few_properties = $this->model_field_array(); |
|
| 2994 | + $first_few_properties = array_slice($first_few_properties, 0, 3); |
|
| 2995 | + $name_parts = array(); |
|
| 2996 | + foreach ($first_few_properties as $name => $value) { |
|
| 2997 | + $name_parts[] = "$name:$value"; |
|
| 2998 | + } |
|
| 2999 | + return implode(',', $name_parts); |
|
| 3000 | + } |
|
| 3001 | + |
|
| 3002 | + |
|
| 3003 | + /** |
|
| 3004 | + * in_entity_map |
|
| 3005 | + * Checks if this model object has been proven to already be in the entity map |
|
| 3006 | + * |
|
| 3007 | + * @return boolean |
|
| 3008 | + * @throws ReflectionException |
|
| 3009 | + * @throws InvalidArgumentException |
|
| 3010 | + * @throws InvalidInterfaceException |
|
| 3011 | + * @throws InvalidDataTypeException |
|
| 3012 | + * @throws EE_Error |
|
| 3013 | + */ |
|
| 3014 | + public function in_entity_map() |
|
| 3015 | + { |
|
| 3016 | + // well, if we looked, did we find it in the entity map? |
|
| 3017 | + return $this->ID() && $this->get_model()->get_from_entity_map($this->ID()) === $this; |
|
| 3018 | + } |
|
| 3019 | + |
|
| 3020 | + |
|
| 3021 | + /** |
|
| 3022 | + * refresh_from_db |
|
| 3023 | + * Makes sure the fields and values on this model object are in-sync with what's in the database. |
|
| 3024 | + * |
|
| 3025 | + * @throws ReflectionException |
|
| 3026 | + * @throws InvalidArgumentException |
|
| 3027 | + * @throws InvalidInterfaceException |
|
| 3028 | + * @throws InvalidDataTypeException |
|
| 3029 | + * @throws EE_Error if this model object isn't in the entity mapper (because then you should |
|
| 3030 | + * just use what's in the entity mapper and refresh it) and WP_DEBUG is TRUE |
|
| 3031 | + */ |
|
| 3032 | + public function refresh_from_db() |
|
| 3033 | + { |
|
| 3034 | + if ($this->ID() && $this->in_entity_map()) { |
|
| 3035 | + $this->get_model()->refresh_entity_map_from_db($this->ID()); |
|
| 3036 | + } else { |
|
| 3037 | + // if it doesn't have ID, you shouldn't be asking to refresh it from teh database (because its not in the database) |
|
| 3038 | + // if it has an ID but it's not in the map, and you're asking me to refresh it |
|
| 3039 | + // that's kinda dangerous. You should just use what's in the entity map, or add this to the entity map if there's |
|
| 3040 | + // absolutely nothing in it for this ID |
|
| 3041 | + if (WP_DEBUG) { |
|
| 3042 | + throw new EE_Error( |
|
| 3043 | + sprintf( |
|
| 3044 | + esc_html__( |
|
| 3045 | + 'Trying to refresh a model object with ID "%1$s" that\'s not in the entity map? First off: you should put it in the entity map by calling %2$s. Second off, if you want what\'s in the database right now, you should just call %3$s yourself and discard this model object.', |
|
| 3046 | + 'event_espresso' |
|
| 3047 | + ), |
|
| 3048 | + $this->ID(), |
|
| 3049 | + get_class($this->get_model()) . '::instance()->add_to_entity_map()', |
|
| 3050 | + get_class($this->get_model()) . '::instance()->refresh_entity_map()' |
|
| 3051 | + ) |
|
| 3052 | + ); |
|
| 3053 | + } |
|
| 3054 | + } |
|
| 3055 | + } |
|
| 3056 | + |
|
| 3057 | + |
|
| 3058 | + /** |
|
| 3059 | + * Because some other plugins, like Advanced Cron Manager, expect all objects to have this method |
|
| 3060 | + * (probably a bad assumption they have made, oh well) |
|
| 3061 | + * |
|
| 3062 | + * @return string |
|
| 3063 | + */ |
|
| 3064 | + public function __toString() |
|
| 3065 | + { |
|
| 3066 | + try { |
|
| 3067 | + return sprintf('%s (%s)', $this->name(), $this->ID()); |
|
| 3068 | + } catch (Exception $e) { |
|
| 3069 | + EE_Error::add_error($e->getMessage(), __FILE__, __FUNCTION__, __LINE__); |
|
| 3070 | + return ''; |
|
| 3071 | + } |
|
| 3072 | + } |
|
| 3073 | + |
|
| 3074 | + |
|
| 3075 | + /** |
|
| 3076 | + * Clear related model objects if they're already in the DB, because otherwise when we |
|
| 3077 | + * UN-serialize this model object we'll need to be careful to add them to the entity map. |
|
| 3078 | + * This means if we have made changes to those related model objects, and want to unserialize |
|
| 3079 | + * the this model object on a subsequent request, changes to those related model objects will be lost. |
|
| 3080 | + * Instead, those related model objects should be directly serialized and stored. |
|
| 3081 | + * Eg, the following won't work: |
|
| 3082 | + * $reg = EEM_Registration::instance()->get_one_by_ID( 123 ); |
|
| 3083 | + * $att = $reg->attendee(); |
|
| 3084 | + * $att->set( 'ATT_fname', 'Dirk' ); |
|
| 3085 | + * update_option( 'my_option', serialize( $reg ) ); |
|
| 3086 | + * //END REQUEST |
|
| 3087 | + * //START NEXT REQUEST |
|
| 3088 | + * $reg = get_option( 'my_option' ); |
|
| 3089 | + * $reg->attendee()->save(); |
|
| 3090 | + * And would need to be replace with: |
|
| 3091 | + * $reg = EEM_Registration::instance()->get_one_by_ID( 123 ); |
|
| 3092 | + * $att = $reg->attendee(); |
|
| 3093 | + * $att->set( 'ATT_fname', 'Dirk' ); |
|
| 3094 | + * update_option( 'my_option', serialize( $reg ) ); |
|
| 3095 | + * //END REQUEST |
|
| 3096 | + * //START NEXT REQUEST |
|
| 3097 | + * $att = get_option( 'my_option' ); |
|
| 3098 | + * $att->save(); |
|
| 3099 | + * |
|
| 3100 | + * @return array |
|
| 3101 | + * @throws ReflectionException |
|
| 3102 | + * @throws InvalidArgumentException |
|
| 3103 | + * @throws InvalidInterfaceException |
|
| 3104 | + * @throws InvalidDataTypeException |
|
| 3105 | + * @throws EE_Error |
|
| 3106 | + */ |
|
| 3107 | + public function __sleep() |
|
| 3108 | + { |
|
| 3109 | + $model = $this->get_model(); |
|
| 3110 | + foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
|
| 3111 | + if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
| 3112 | + $classname = 'EE_' . $model->get_this_model_name(); |
|
| 3113 | + if ($this->get_one_from_cache($relation_name) instanceof $classname |
|
| 3114 | + && $this->get_one_from_cache($relation_name)->ID() |
|
| 3115 | + ) { |
|
| 3116 | + $this->clear_cache( |
|
| 3117 | + $relation_name, |
|
| 3118 | + $this->get_one_from_cache($relation_name)->ID() |
|
| 3119 | + ); |
|
| 3120 | + } |
|
| 3121 | + } |
|
| 3122 | + } |
|
| 3123 | + $this->_props_n_values_provided_in_constructor = array(); |
|
| 3124 | + $properties_to_serialize = get_object_vars($this); |
|
| 3125 | + // don't serialize the model. It's big and that risks recursion |
|
| 3126 | + unset($properties_to_serialize['_model']); |
|
| 3127 | + return array_keys($properties_to_serialize); |
|
| 3128 | + } |
|
| 3129 | + |
|
| 3130 | + |
|
| 3131 | + /** |
|
| 3132 | + * restore _props_n_values_provided_in_constructor |
|
| 3133 | + * PLZ NOTE: this will reset the array to whatever fields values were present prior to serialization, |
|
| 3134 | + * and therefore should NOT be used to determine if state change has occurred since initial construction. |
|
| 3135 | + * At best, you would only be able to detect if state change has occurred during THIS request. |
|
| 3136 | + */ |
|
| 3137 | + public function __wakeup() |
|
| 3138 | + { |
|
| 3139 | + $this->_props_n_values_provided_in_constructor = $this->_fields; |
|
| 3140 | + } |
|
| 3141 | + |
|
| 3142 | + |
|
| 3143 | + /** |
|
| 3144 | + * Usage of this magic method is to ensure any internally cached references to object instances that must remain |
|
| 3145 | + * distinct with the clone host instance are also cloned. |
|
| 3146 | + */ |
|
| 3147 | + public function __clone() |
|
| 3148 | + { |
|
| 3149 | + // handle DateTimes (this is handled in here because there's no one specific child class that uses datetimes). |
|
| 3150 | + foreach ($this->_fields as $field => $value) { |
|
| 3151 | + if ($value instanceof DateTime) { |
|
| 3152 | + $this->_fields[ $field ] = clone $value; |
|
| 3153 | + } |
|
| 3154 | + } |
|
| 3155 | + } |
|
| 3156 | 3156 | } |
@@ -10,609 +10,609 @@ discard block |
||
| 10 | 10 | class EED_Thank_You_Page extends EED_Module |
| 11 | 11 | { |
| 12 | 12 | |
| 13 | - /** |
|
| 14 | - * time in seconds to wait for the IPN to arrive before telling the registrant to bugger off ( 1200s = 20 minutes ) |
|
| 15 | - */ |
|
| 16 | - const IPN_wait_time = 1200; |
|
| 17 | - |
|
| 18 | - /** |
|
| 19 | - * The transaction specified by the reg_url_link passed from the Request, or from the Session |
|
| 20 | - * |
|
| 21 | - * @var EE_Transaction $_current_txn |
|
| 22 | - */ |
|
| 23 | - private $_current_txn; |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * @var EE_Registration $_primary_registrant |
|
| 27 | - */ |
|
| 28 | - private $_primary_registrant; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * The reg_url_link passed from the Request, or from the Session |
|
| 32 | - * |
|
| 33 | - * @var string $_reg_url_link |
|
| 34 | - */ |
|
| 35 | - private $_reg_url_link; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * whether the incoming reg_url_link is for the primary registrant or not |
|
| 39 | - * |
|
| 40 | - * @var boolean $_is_primary |
|
| 41 | - */ |
|
| 42 | - private $_is_primary; |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * The URL for revisiting the SPCO attendee information step |
|
| 46 | - * |
|
| 47 | - * @var string $_SPCO_attendee_information_url |
|
| 48 | - */ |
|
| 49 | - private $_SPCO_attendee_information_url; |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * The URL for revisiting the SPCO payment options step |
|
| 53 | - * |
|
| 54 | - * @var string $_SPCO_payment_options_url |
|
| 55 | - */ |
|
| 56 | - private $_SPCO_payment_options_url; |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * whether to display the Payment Options link |
|
| 60 | - * |
|
| 61 | - * @var boolean $_show_try_pay_again_link |
|
| 62 | - */ |
|
| 63 | - private $_show_try_pay_again_link = false; |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * whether payments are allowed at this time |
|
| 67 | - * |
|
| 68 | - * @var boolean $_payments_closed |
|
| 69 | - */ |
|
| 70 | - private $_payments_closed = false; |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * whether the selected payment method is Bank, Check , Invoice, etc |
|
| 74 | - * |
|
| 75 | - * @var boolean $_is_offline_payment_method |
|
| 76 | - */ |
|
| 77 | - private $_is_offline_payment_method = true; |
|
| 78 | - |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * @return EED_Module|EED_Thank_You_Page |
|
| 82 | - */ |
|
| 83 | - public static function instance() |
|
| 84 | - { |
|
| 85 | - return parent::get_instance(__CLASS__); |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * set_hooks - for hooking into EE Core, modules, etc |
|
| 91 | - * |
|
| 92 | - * @return void |
|
| 93 | - */ |
|
| 94 | - public static function set_hooks() |
|
| 95 | - { |
|
| 96 | - add_action('wp_loaded', array('EED_Thank_You_Page', 'set_definitions'), 2); |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * set_hooks_admin - for hooking into EE Admin Core, modules, etc |
|
| 102 | - * |
|
| 103 | - * @return void |
|
| 104 | - */ |
|
| 105 | - public static function set_hooks_admin() |
|
| 106 | - { |
|
| 107 | - add_action( |
|
| 108 | - 'wp_ajax_espresso_resend_reg_confirmation_email', |
|
| 109 | - array('EED_Thank_You_Page', 'resend_reg_confirmation_email'), |
|
| 110 | - 10, |
|
| 111 | - 2 |
|
| 112 | - ); |
|
| 113 | - add_action( |
|
| 114 | - 'wp_ajax_nopriv_espresso_resend_reg_confirmation_email', |
|
| 115 | - array('EED_Thank_You_Page', 'resend_reg_confirmation_email'), |
|
| 116 | - 10, |
|
| 117 | - 2 |
|
| 118 | - ); |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * set_definitions |
|
| 124 | - * |
|
| 125 | - * @return void |
|
| 126 | - */ |
|
| 127 | - public static function set_definitions() |
|
| 128 | - { |
|
| 129 | - define('THANK_YOU_ASSETS_URL', plugin_dir_url(__FILE__) . 'assets' . DS); |
|
| 130 | - define('THANK_YOU_TEMPLATES_PATH', str_replace('\\', DS, plugin_dir_path(__FILE__)) . 'templates' . DS); |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - |
|
| 134 | - /** |
|
| 135 | - * get_txn |
|
| 136 | - * |
|
| 137 | - * @return EE_Transaction |
|
| 138 | - */ |
|
| 139 | - public function get_txn() |
|
| 140 | - { |
|
| 141 | - if ($this->_current_txn instanceof EE_Transaction) { |
|
| 142 | - return $this->_current_txn; |
|
| 143 | - } |
|
| 144 | - $TXN_model = EE_Registry::instance()->load_model('Transaction'); |
|
| 145 | - if (! $TXN_model instanceof EEM_Transaction) { |
|
| 146 | - EE_Error::add_error( |
|
| 147 | - __('The transaction model could not be established.', 'event_espresso'), |
|
| 148 | - __FILE__, |
|
| 149 | - __FUNCTION__, |
|
| 150 | - __LINE__ |
|
| 151 | - ); |
|
| 152 | - return null; |
|
| 153 | - } |
|
| 154 | - // get the transaction. yes, we may have just loaded it, but it may have been updated, or this may be via an ajax request |
|
| 155 | - $this->_current_txn = $TXN_model->get_transaction_from_reg_url_link($this->_reg_url_link); |
|
| 156 | - // verify TXN |
|
| 157 | - if (WP_DEBUG && ! $this->_current_txn instanceof EE_Transaction) { |
|
| 158 | - EE_Error::add_error( |
|
| 159 | - __( |
|
| 160 | - 'No transaction information could be retrieved or the transaction data is not of the correct type.', |
|
| 161 | - 'event_espresso' |
|
| 162 | - ), |
|
| 163 | - __FILE__, |
|
| 164 | - __FUNCTION__, |
|
| 165 | - __LINE__ |
|
| 166 | - ); |
|
| 167 | - return null; |
|
| 168 | - } |
|
| 169 | - return $this->_current_txn; |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - |
|
| 173 | - /** |
|
| 174 | - * get_txn_payments |
|
| 175 | - * |
|
| 176 | - * @param int $since |
|
| 177 | - * @return mixed array of EE_Payment || FALSE |
|
| 178 | - * @throws \EE_Error |
|
| 179 | - */ |
|
| 180 | - public function get_txn_payments($since = 0) |
|
| 181 | - { |
|
| 182 | - if (! $this->get_txn()) { |
|
| 183 | - return false; |
|
| 184 | - } |
|
| 185 | - $args = array('order_by' => array('PAY_timestamp' => 'ASC')); |
|
| 186 | - if ($since > 0) { |
|
| 187 | - $args[0] = array('PAY_timestamp' => array('>', $since)); |
|
| 188 | - } |
|
| 189 | - // get array of payments with most recent first |
|
| 190 | - return $this->_current_txn->payments($args); |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - |
|
| 194 | - /** |
|
| 195 | - * @return bool |
|
| 196 | - */ |
|
| 197 | - public function isOfflinePaymentMethod() |
|
| 198 | - { |
|
| 199 | - return $this->_is_offline_payment_method; |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - |
|
| 203 | - |
|
| 204 | - |
|
| 205 | - /** |
|
| 206 | - * get_reg_url_link |
|
| 207 | - * |
|
| 208 | - * @return void |
|
| 209 | - */ |
|
| 210 | - private function _get_reg_url_link() |
|
| 211 | - { |
|
| 212 | - if (! empty($this->_reg_url_link)) { |
|
| 213 | - return; |
|
| 214 | - } |
|
| 215 | - // only do thank you page stuff if we have a REG_url_link in the url |
|
| 216 | - if (WP_DEBUG && ! EE_Registry::instance()->REQ->is_set('e_reg_url_link')) { |
|
| 217 | - EE_Error::add_error( |
|
| 218 | - __( |
|
| 219 | - 'No transaction information could be retrieved because the registration URL link is missing or invalid.', |
|
| 220 | - 'event_espresso' |
|
| 221 | - ), |
|
| 222 | - __FILE__, |
|
| 223 | - __FUNCTION__, |
|
| 224 | - __LINE__ |
|
| 225 | - ); |
|
| 226 | - return; |
|
| 227 | - } |
|
| 228 | - // check for reg_url_link |
|
| 229 | - $this->_reg_url_link = EE_Registry::instance()->REQ->get('e_reg_url_link'); |
|
| 230 | - } |
|
| 231 | - |
|
| 232 | - |
|
| 233 | - /** |
|
| 234 | - * set_reg_url_link |
|
| 235 | - * |
|
| 236 | - * @param string $reg_url_link |
|
| 237 | - */ |
|
| 238 | - public function set_reg_url_link($reg_url_link = null) |
|
| 239 | - { |
|
| 240 | - $this->_reg_url_link = ! empty($reg_url_link) ? $reg_url_link : $this->_reg_url_link; |
|
| 241 | - } |
|
| 242 | - |
|
| 243 | - |
|
| 244 | - /** |
|
| 245 | - * run - initial module setup |
|
| 246 | - * this method is primarily used for activating resources in the EE_Front_Controller thru the use of filters |
|
| 247 | - * |
|
| 248 | - * @param WP $WP |
|
| 249 | - * @return void |
|
| 250 | - * @throws \EE_Error |
|
| 251 | - */ |
|
| 252 | - public function run($WP) |
|
| 253 | - { |
|
| 254 | - } |
|
| 255 | - |
|
| 256 | - |
|
| 257 | - /** |
|
| 258 | - * load_resources |
|
| 259 | - * |
|
| 260 | - * @return void |
|
| 261 | - * @throws \EE_Error |
|
| 262 | - */ |
|
| 263 | - public function load_resources() |
|
| 264 | - { |
|
| 265 | - $this->_get_reg_url_link(); |
|
| 266 | - // resend_reg_confirmation_email ? |
|
| 267 | - if (EE_Registry::instance()->REQ->is_set('resend')) { |
|
| 268 | - EED_Thank_You_Page::resend_reg_confirmation_email(); |
|
| 269 | - } |
|
| 270 | - EE_Registry::instance()->SSN->clear_session(__CLASS__, __FUNCTION__); |
|
| 271 | - $this->_translate_strings(); |
|
| 272 | - // load assets |
|
| 273 | - add_action('wp_enqueue_scripts', array($this, 'load_js'), 10); |
|
| 274 | - } |
|
| 275 | - |
|
| 276 | - |
|
| 277 | - /** |
|
| 278 | - * load_js |
|
| 279 | - * |
|
| 280 | - * @return void |
|
| 281 | - */ |
|
| 282 | - protected function _translate_strings() |
|
| 283 | - { |
|
| 284 | - EE_Registry::$i18n_js_strings['e_reg_url_link'] = $this->_reg_url_link; |
|
| 285 | - EE_Registry::$i18n_js_strings['initial_access'] = time(); |
|
| 286 | - EE_Registry::$i18n_js_strings['IPN_wait_time'] = EED_Thank_You_Page::IPN_wait_time; |
|
| 287 | - EE_Registry::$i18n_js_strings['TXN_complete'] = EEM_Transaction::complete_status_code; |
|
| 288 | - EE_Registry::$i18n_js_strings['TXN_incomplete'] = EEM_Transaction::incomplete_status_code; |
|
| 289 | - EE_Registry::$i18n_js_strings['checking_for_new_payments'] = __( |
|
| 290 | - 'checking for new payments...', |
|
| 291 | - 'event_espresso' |
|
| 292 | - ); |
|
| 293 | - EE_Registry::$i18n_js_strings['loading_payment_info'] = __( |
|
| 294 | - 'loading payment information...', |
|
| 295 | - 'event_espresso' |
|
| 296 | - ); |
|
| 297 | - EE_Registry::$i18n_js_strings['server_error'] = __( |
|
| 298 | - 'An unknown error occurred on the server while attempting to process your request. Please refresh the page and try again.', |
|
| 299 | - 'event_espresso' |
|
| 300 | - ); |
|
| 301 | - EE_Registry::$i18n_js_strings['slow_IPN'] = apply_filters( |
|
| 302 | - 'EED_Thank_You_Page__load_js__slow_IPN', |
|
| 303 | - sprintf( |
|
| 304 | - __( |
|
| 305 | - '%sThe Payment Notification appears to be taking longer than usual to arrive. Maybe check back later or just wait for your payment and registration confirmation results to be sent to you via email. We apologize for any inconvenience this may have caused.%s', |
|
| 306 | - 'event_espresso' |
|
| 307 | - ), |
|
| 308 | - '<div id="espresso-thank-you-page-slow-IPN-dv" class="ee-attention jst-left">', |
|
| 309 | - '</div>' |
|
| 310 | - ) |
|
| 311 | - ); |
|
| 312 | - } |
|
| 313 | - |
|
| 314 | - |
|
| 315 | - /** |
|
| 316 | - * load_js |
|
| 317 | - * |
|
| 318 | - * @return void |
|
| 319 | - */ |
|
| 320 | - public function load_js() |
|
| 321 | - { |
|
| 322 | - wp_register_script( |
|
| 323 | - 'thank_you_page', |
|
| 324 | - THANK_YOU_ASSETS_URL . 'thank_you_page.js', |
|
| 325 | - array('espresso_core', 'heartbeat'), |
|
| 326 | - EVENT_ESPRESSO_VERSION, |
|
| 327 | - true |
|
| 328 | - ); |
|
| 329 | - wp_enqueue_script('thank_you_page'); |
|
| 330 | - wp_enqueue_style('espresso_default'); |
|
| 331 | - } |
|
| 332 | - |
|
| 333 | - |
|
| 334 | - /** |
|
| 335 | - * init |
|
| 336 | - * |
|
| 337 | - * @return void |
|
| 338 | - * @throws \EE_Error |
|
| 339 | - */ |
|
| 340 | - public function init() |
|
| 341 | - { |
|
| 342 | - $this->_get_reg_url_link(); |
|
| 343 | - if (! $this->get_txn()) { |
|
| 344 | - echo EEH_HTML::div( |
|
| 345 | - EEH_HTML::h4(__('We\'re sorry...', 'event_espresso'), '', '') . |
|
| 346 | - sprintf( |
|
| 347 | - __( |
|
| 348 | - 'This is a system page for displaying transaction information after a purchase.%1$sYou are most likely seeing this notice because you have navigated to this page%1$sthrough some means other than completing a transaction.%1$sSorry for the disappointment, but you will most likely find nothing of interest here.%1$s%1$s', |
|
| 349 | - 'event_espresso' |
|
| 350 | - ), |
|
| 351 | - '<br/>' |
|
| 352 | - ), |
|
| 353 | - '', |
|
| 354 | - 'ee-attention' |
|
| 355 | - ); |
|
| 356 | - return null; |
|
| 357 | - } |
|
| 358 | - // if we've made it to the Thank You page, then let's toggle any "Failed" transactions to "Incomplete" |
|
| 359 | - if ($this->_current_txn->status_ID() === EEM_Transaction::failed_status_code) { |
|
| 360 | - $this->_current_txn->set_status(EEM_Transaction::incomplete_status_code); |
|
| 361 | - $this->_current_txn->save(); |
|
| 362 | - } |
|
| 363 | - $this->_primary_registrant = $this->_current_txn->primary_registration() instanceof EE_Registration |
|
| 364 | - ? $this->_current_txn->primary_registration() |
|
| 365 | - : null; |
|
| 366 | - $this->_is_primary = $this->_primary_registrant->reg_url_link() === $this->_reg_url_link ? true : false; |
|
| 367 | - $show_try_pay_again_link_default = apply_filters( |
|
| 368 | - 'AFEE__EED_Thank_You_Page__init__show_try_pay_again_link_default', |
|
| 369 | - true |
|
| 370 | - ); |
|
| 371 | - $this->_show_try_pay_again_link = $show_try_pay_again_link_default; |
|
| 372 | - // txn status ? |
|
| 373 | - if ($this->_current_txn->is_completed()) { |
|
| 374 | - $this->_show_try_pay_again_link = $show_try_pay_again_link_default; |
|
| 375 | - } elseif ($this->_current_txn->is_incomplete() |
|
| 376 | - && ($this->_primary_registrant->is_approved() |
|
| 377 | - || $this->_primary_registrant->is_pending_payment()) |
|
| 378 | - ) { |
|
| 379 | - $this->_show_try_pay_again_link = true; |
|
| 380 | - } elseif ($this->_primary_registrant->is_approved() || $this->_primary_registrant->is_pending_payment()) { |
|
| 381 | - // its pending |
|
| 382 | - $this->_show_try_pay_again_link = isset( |
|
| 383 | - EE_Registry::instance()->CFG->registration->show_pending_payment_options |
|
| 384 | - ) |
|
| 385 | - && EE_Registry::instance()->CFG |
|
| 386 | - ->registration->show_pending_payment_options |
|
| 387 | - ? true |
|
| 388 | - : $show_try_pay_again_link_default; |
|
| 389 | - } |
|
| 390 | - $this->_payments_closed = ! $this->_current_txn->payment_method() instanceof EE_Payment_Method |
|
| 391 | - ? true |
|
| 392 | - : false; |
|
| 393 | - $this->_is_offline_payment_method = false; |
|
| 394 | - if (// if payment method is unknown |
|
| 395 | - ! $this->_current_txn->payment_method() instanceof EE_Payment_Method |
|
| 396 | - || ( |
|
| 397 | - // or is an offline payment method |
|
| 398 | - $this->_current_txn->payment_method() instanceof EE_Payment_Method |
|
| 399 | - && $this->_current_txn->payment_method()->is_off_line() |
|
| 400 | - ) |
|
| 401 | - ) { |
|
| 402 | - $this->_is_offline_payment_method = true; |
|
| 403 | - } |
|
| 404 | - // link to SPCO |
|
| 405 | - $revisit_spco_url = add_query_arg( |
|
| 406 | - array('ee' => '_register', 'revisit' => true, 'e_reg_url_link' => $this->_reg_url_link), |
|
| 407 | - EE_Registry::instance()->CFG->core->reg_page_url() |
|
| 408 | - ); |
|
| 409 | - // link to SPCO payment_options |
|
| 410 | - $this->_SPCO_payment_options_url = $this->_primary_registrant instanceof EE_Registration |
|
| 411 | - ? $this->_primary_registrant->payment_overview_url() |
|
| 412 | - : add_query_arg( |
|
| 413 | - array('step' => 'payment_options'), |
|
| 414 | - $revisit_spco_url |
|
| 415 | - ); |
|
| 416 | - // link to SPCO attendee_information |
|
| 417 | - $this->_SPCO_attendee_information_url = $this->_primary_registrant instanceof EE_Registration |
|
| 418 | - ? $this->_primary_registrant->edit_attendee_information_url() |
|
| 419 | - : false; |
|
| 420 | - do_action('AHEE__EED_Thank_You_Page__init_end', $this->_current_txn); |
|
| 421 | - // set no cache headers and constants |
|
| 422 | - EE_System::do_not_cache(); |
|
| 423 | - } |
|
| 424 | - |
|
| 425 | - |
|
| 426 | - /** |
|
| 427 | - * display_thank_you_page_results |
|
| 428 | - * |
|
| 429 | - * @return string |
|
| 430 | - * @throws \EE_Error |
|
| 431 | - */ |
|
| 432 | - public function thank_you_page_results() |
|
| 433 | - { |
|
| 434 | - $this->init(); |
|
| 435 | - if (! $this->_current_txn instanceof EE_Transaction) { |
|
| 436 | - return EE_Error::get_notices(); |
|
| 437 | - } |
|
| 438 | - // link to receipt |
|
| 439 | - $template_args['TXN_receipt_url'] = $this->_current_txn->receipt_url('html'); |
|
| 440 | - if (! empty($template_args['TXN_receipt_url'])) { |
|
| 441 | - $template_args['order_conf_desc'] = __( |
|
| 442 | - '%1$sCongratulations%2$sYour registration has been successfully processed.%3$sCheck your email for your registration confirmation or click the button below to view / download / print a full description of your purchases and registration information.', |
|
| 443 | - 'event_espresso' |
|
| 444 | - ); |
|
| 445 | - } else { |
|
| 446 | - $template_args['order_conf_desc'] = __( |
|
| 447 | - '%1$sCongratulations%2$sYour registration has been successfully processed.%3$sCheck your email for your registration confirmation.', |
|
| 448 | - 'event_espresso' |
|
| 449 | - ); |
|
| 450 | - } |
|
| 451 | - $template_args['transaction'] = $this->_current_txn; |
|
| 452 | - $template_args['revisit'] = EE_Registry::instance()->REQ->get('revisit', false); |
|
| 453 | - add_action('AHEE__thank_you_page_overview_template__content', array($this, 'get_registration_details')); |
|
| 454 | - if ($this->_is_primary && ! $this->_current_txn->is_free()) { |
|
| 455 | - add_action('AHEE__thank_you_page_overview_template__content', array($this, 'get_ajax_content')); |
|
| 456 | - } |
|
| 457 | - return EEH_Template::locate_template( |
|
| 458 | - THANK_YOU_TEMPLATES_PATH . 'thank-you-page-overview.template.php', |
|
| 459 | - $template_args, |
|
| 460 | - true, |
|
| 461 | - true |
|
| 462 | - ); |
|
| 463 | - } |
|
| 464 | - |
|
| 465 | - |
|
| 466 | - |
|
| 467 | - /** |
|
| 468 | - * _update_server_wait_time |
|
| 469 | - * |
|
| 470 | - * @param array $thank_you_page_data thank you page portion of the incoming JSON array from the WP heartbeat data |
|
| 471 | - * @return array |
|
| 472 | - * @throws \EE_Error |
|
| 473 | - */ |
|
| 474 | - private function _update_server_wait_time($thank_you_page_data = array()) |
|
| 475 | - { |
|
| 476 | - $response['espresso_thank_you_page'] = array( |
|
| 477 | - 'still_waiting' => isset($thank_you_page_data['initial_access']) |
|
| 478 | - ? time() - $thank_you_page_data['initial_access'] |
|
| 479 | - : 0, |
|
| 480 | - 'txn_status' => $this->_current_txn->status_ID(), |
|
| 481 | - ); |
|
| 482 | - return $response; |
|
| 483 | - } |
|
| 484 | - |
|
| 485 | - |
|
| 486 | - /** |
|
| 487 | - * get_registration_details |
|
| 488 | - * |
|
| 489 | - * @throws \EE_Error |
|
| 490 | - */ |
|
| 491 | - public function get_registration_details() |
|
| 492 | - { |
|
| 493 | - // prepare variables for displaying |
|
| 494 | - $template_args = array(); |
|
| 495 | - $template_args['transaction'] = $this->_current_txn; |
|
| 496 | - $template_args['reg_url_link'] = $this->_reg_url_link; |
|
| 497 | - $template_args['is_primary'] = $this->_is_primary; |
|
| 498 | - $template_args['SPCO_attendee_information_url'] = $this->_SPCO_attendee_information_url; |
|
| 499 | - $template_args['resend_reg_confirmation_url'] = add_query_arg( |
|
| 500 | - array('token' => $this->_reg_url_link, 'resend_reg_confirmation' => 'true'), |
|
| 501 | - EE_Registry::instance()->CFG->core->thank_you_page_url() |
|
| 502 | - ); |
|
| 503 | - // verify template arguments |
|
| 504 | - EEH_Template_Validator::verify_instanceof($template_args['transaction'], '$transaction', 'EE_Transaction'); |
|
| 505 | - EEH_Template_Validator::verify_isnt_null( |
|
| 506 | - $template_args['SPCO_attendee_information_url'], |
|
| 507 | - '$SPCO_attendee_information_url' |
|
| 508 | - ); |
|
| 509 | - echo EEH_Template::locate_template( |
|
| 510 | - THANK_YOU_TEMPLATES_PATH . 'thank-you-page-registration-details.template.php', |
|
| 511 | - $template_args, |
|
| 512 | - true, |
|
| 513 | - true |
|
| 514 | - ); |
|
| 515 | - } |
|
| 516 | - |
|
| 517 | - |
|
| 518 | - /** |
|
| 519 | - * resend_reg_confirmation_email |
|
| 520 | - * |
|
| 521 | - * @throws \EE_Error |
|
| 522 | - */ |
|
| 523 | - public static function resend_reg_confirmation_email() |
|
| 524 | - { |
|
| 525 | - EE_Registry::instance()->load_core('Request_Handler'); |
|
| 526 | - $reg_url_link = EE_Registry::instance()->REQ->get('token'); |
|
| 527 | - // was a REG_ID passed ? |
|
| 528 | - if ($reg_url_link) { |
|
| 529 | - $registration = EE_Registry::instance()->load_model('Registration')->get_one( |
|
| 530 | - array(array('REG_url_link' => $reg_url_link)) |
|
| 531 | - ); |
|
| 532 | - if ($registration instanceof EE_Registration) { |
|
| 533 | - // resend email |
|
| 534 | - EED_Messages::process_resend(array('_REG_ID' => $registration->ID())); |
|
| 535 | - } else { |
|
| 536 | - EE_Error::add_error( |
|
| 537 | - __( |
|
| 538 | - 'The Registration Confirmation email could not be sent because a valid Registration could not be retrieved from the database.', |
|
| 539 | - 'event_espresso' |
|
| 540 | - ), |
|
| 541 | - __FILE__, |
|
| 542 | - __FUNCTION__, |
|
| 543 | - __LINE__ |
|
| 544 | - ); |
|
| 545 | - } |
|
| 546 | - } else { |
|
| 547 | - EE_Error::add_error( |
|
| 548 | - __( |
|
| 549 | - 'The Registration Confirmation email could not be sent because a registration token is missing or invalid.', |
|
| 550 | - 'event_espresso' |
|
| 551 | - ), |
|
| 552 | - __FILE__, |
|
| 553 | - __FUNCTION__, |
|
| 554 | - __LINE__ |
|
| 555 | - ); |
|
| 556 | - } |
|
| 557 | - // request sent via AJAX ? |
|
| 558 | - if (EE_FRONT_AJAX) { |
|
| 559 | - echo wp_json_encode(EE_Error::get_notices(false)); |
|
| 560 | - die(); |
|
| 561 | - // or was JS disabled ? |
|
| 562 | - } else { |
|
| 563 | - // save errors so that they get picked up on the next request |
|
| 564 | - EE_Error::get_notices(true, true); |
|
| 565 | - wp_safe_redirect( |
|
| 566 | - add_query_arg( |
|
| 567 | - array('e_reg_url_link' => $reg_url_link), |
|
| 568 | - EE_Registry::instance()->CFG->core->thank_you_page_url() |
|
| 569 | - ) |
|
| 570 | - ); |
|
| 571 | - } |
|
| 572 | - } |
|
| 573 | - |
|
| 574 | - |
|
| 575 | - /** |
|
| 576 | - * get_ajax_content |
|
| 577 | - * |
|
| 578 | - * @return void |
|
| 579 | - * @throws \EE_Error |
|
| 580 | - */ |
|
| 581 | - public function get_ajax_content() |
|
| 582 | - { |
|
| 583 | - if (! $this->get_txn()) { |
|
| 584 | - return; |
|
| 585 | - } |
|
| 586 | - // first determine which event(s) require pre-approval or not |
|
| 587 | - $events = array(); |
|
| 588 | - $events_requiring_pre_approval = array(); |
|
| 589 | - foreach ($this->_current_txn->registrations() as $registration) { |
|
| 590 | - if ($registration instanceof EE_Registration) { |
|
| 591 | - $event = $registration->event(); |
|
| 592 | - if ($event instanceof EE_Event) { |
|
| 593 | - if ($registration->is_not_approved() && $registration->event() instanceof EE_Event) { |
|
| 594 | - $events_requiring_pre_approval[ $event->ID() ] = $event; |
|
| 595 | - } else { |
|
| 596 | - $events[ $event->ID() ] = $event; |
|
| 597 | - } |
|
| 598 | - } |
|
| 599 | - } |
|
| 600 | - } |
|
| 601 | - $this->display_details_for_events_requiring_pre_approval($events_requiring_pre_approval); |
|
| 602 | - $this->display_details_for_events($events); |
|
| 603 | - } |
|
| 604 | - |
|
| 605 | - |
|
| 606 | - /** |
|
| 607 | - * display_details_for_events |
|
| 608 | - * |
|
| 609 | - * @param EE_Event[] $events |
|
| 610 | - * @return void |
|
| 611 | - */ |
|
| 612 | - public function display_details_for_events($events = array()) |
|
| 613 | - { |
|
| 614 | - if (! empty($events)) { |
|
| 615 | - ?> |
|
| 13 | + /** |
|
| 14 | + * time in seconds to wait for the IPN to arrive before telling the registrant to bugger off ( 1200s = 20 minutes ) |
|
| 15 | + */ |
|
| 16 | + const IPN_wait_time = 1200; |
|
| 17 | + |
|
| 18 | + /** |
|
| 19 | + * The transaction specified by the reg_url_link passed from the Request, or from the Session |
|
| 20 | + * |
|
| 21 | + * @var EE_Transaction $_current_txn |
|
| 22 | + */ |
|
| 23 | + private $_current_txn; |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * @var EE_Registration $_primary_registrant |
|
| 27 | + */ |
|
| 28 | + private $_primary_registrant; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * The reg_url_link passed from the Request, or from the Session |
|
| 32 | + * |
|
| 33 | + * @var string $_reg_url_link |
|
| 34 | + */ |
|
| 35 | + private $_reg_url_link; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * whether the incoming reg_url_link is for the primary registrant or not |
|
| 39 | + * |
|
| 40 | + * @var boolean $_is_primary |
|
| 41 | + */ |
|
| 42 | + private $_is_primary; |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * The URL for revisiting the SPCO attendee information step |
|
| 46 | + * |
|
| 47 | + * @var string $_SPCO_attendee_information_url |
|
| 48 | + */ |
|
| 49 | + private $_SPCO_attendee_information_url; |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * The URL for revisiting the SPCO payment options step |
|
| 53 | + * |
|
| 54 | + * @var string $_SPCO_payment_options_url |
|
| 55 | + */ |
|
| 56 | + private $_SPCO_payment_options_url; |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * whether to display the Payment Options link |
|
| 60 | + * |
|
| 61 | + * @var boolean $_show_try_pay_again_link |
|
| 62 | + */ |
|
| 63 | + private $_show_try_pay_again_link = false; |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * whether payments are allowed at this time |
|
| 67 | + * |
|
| 68 | + * @var boolean $_payments_closed |
|
| 69 | + */ |
|
| 70 | + private $_payments_closed = false; |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * whether the selected payment method is Bank, Check , Invoice, etc |
|
| 74 | + * |
|
| 75 | + * @var boolean $_is_offline_payment_method |
|
| 76 | + */ |
|
| 77 | + private $_is_offline_payment_method = true; |
|
| 78 | + |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * @return EED_Module|EED_Thank_You_Page |
|
| 82 | + */ |
|
| 83 | + public static function instance() |
|
| 84 | + { |
|
| 85 | + return parent::get_instance(__CLASS__); |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * set_hooks - for hooking into EE Core, modules, etc |
|
| 91 | + * |
|
| 92 | + * @return void |
|
| 93 | + */ |
|
| 94 | + public static function set_hooks() |
|
| 95 | + { |
|
| 96 | + add_action('wp_loaded', array('EED_Thank_You_Page', 'set_definitions'), 2); |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * set_hooks_admin - for hooking into EE Admin Core, modules, etc |
|
| 102 | + * |
|
| 103 | + * @return void |
|
| 104 | + */ |
|
| 105 | + public static function set_hooks_admin() |
|
| 106 | + { |
|
| 107 | + add_action( |
|
| 108 | + 'wp_ajax_espresso_resend_reg_confirmation_email', |
|
| 109 | + array('EED_Thank_You_Page', 'resend_reg_confirmation_email'), |
|
| 110 | + 10, |
|
| 111 | + 2 |
|
| 112 | + ); |
|
| 113 | + add_action( |
|
| 114 | + 'wp_ajax_nopriv_espresso_resend_reg_confirmation_email', |
|
| 115 | + array('EED_Thank_You_Page', 'resend_reg_confirmation_email'), |
|
| 116 | + 10, |
|
| 117 | + 2 |
|
| 118 | + ); |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * set_definitions |
|
| 124 | + * |
|
| 125 | + * @return void |
|
| 126 | + */ |
|
| 127 | + public static function set_definitions() |
|
| 128 | + { |
|
| 129 | + define('THANK_YOU_ASSETS_URL', plugin_dir_url(__FILE__) . 'assets' . DS); |
|
| 130 | + define('THANK_YOU_TEMPLATES_PATH', str_replace('\\', DS, plugin_dir_path(__FILE__)) . 'templates' . DS); |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + |
|
| 134 | + /** |
|
| 135 | + * get_txn |
|
| 136 | + * |
|
| 137 | + * @return EE_Transaction |
|
| 138 | + */ |
|
| 139 | + public function get_txn() |
|
| 140 | + { |
|
| 141 | + if ($this->_current_txn instanceof EE_Transaction) { |
|
| 142 | + return $this->_current_txn; |
|
| 143 | + } |
|
| 144 | + $TXN_model = EE_Registry::instance()->load_model('Transaction'); |
|
| 145 | + if (! $TXN_model instanceof EEM_Transaction) { |
|
| 146 | + EE_Error::add_error( |
|
| 147 | + __('The transaction model could not be established.', 'event_espresso'), |
|
| 148 | + __FILE__, |
|
| 149 | + __FUNCTION__, |
|
| 150 | + __LINE__ |
|
| 151 | + ); |
|
| 152 | + return null; |
|
| 153 | + } |
|
| 154 | + // get the transaction. yes, we may have just loaded it, but it may have been updated, or this may be via an ajax request |
|
| 155 | + $this->_current_txn = $TXN_model->get_transaction_from_reg_url_link($this->_reg_url_link); |
|
| 156 | + // verify TXN |
|
| 157 | + if (WP_DEBUG && ! $this->_current_txn instanceof EE_Transaction) { |
|
| 158 | + EE_Error::add_error( |
|
| 159 | + __( |
|
| 160 | + 'No transaction information could be retrieved or the transaction data is not of the correct type.', |
|
| 161 | + 'event_espresso' |
|
| 162 | + ), |
|
| 163 | + __FILE__, |
|
| 164 | + __FUNCTION__, |
|
| 165 | + __LINE__ |
|
| 166 | + ); |
|
| 167 | + return null; |
|
| 168 | + } |
|
| 169 | + return $this->_current_txn; |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + |
|
| 173 | + /** |
|
| 174 | + * get_txn_payments |
|
| 175 | + * |
|
| 176 | + * @param int $since |
|
| 177 | + * @return mixed array of EE_Payment || FALSE |
|
| 178 | + * @throws \EE_Error |
|
| 179 | + */ |
|
| 180 | + public function get_txn_payments($since = 0) |
|
| 181 | + { |
|
| 182 | + if (! $this->get_txn()) { |
|
| 183 | + return false; |
|
| 184 | + } |
|
| 185 | + $args = array('order_by' => array('PAY_timestamp' => 'ASC')); |
|
| 186 | + if ($since > 0) { |
|
| 187 | + $args[0] = array('PAY_timestamp' => array('>', $since)); |
|
| 188 | + } |
|
| 189 | + // get array of payments with most recent first |
|
| 190 | + return $this->_current_txn->payments($args); |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + |
|
| 194 | + /** |
|
| 195 | + * @return bool |
|
| 196 | + */ |
|
| 197 | + public function isOfflinePaymentMethod() |
|
| 198 | + { |
|
| 199 | + return $this->_is_offline_payment_method; |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + |
|
| 203 | + |
|
| 204 | + |
|
| 205 | + /** |
|
| 206 | + * get_reg_url_link |
|
| 207 | + * |
|
| 208 | + * @return void |
|
| 209 | + */ |
|
| 210 | + private function _get_reg_url_link() |
|
| 211 | + { |
|
| 212 | + if (! empty($this->_reg_url_link)) { |
|
| 213 | + return; |
|
| 214 | + } |
|
| 215 | + // only do thank you page stuff if we have a REG_url_link in the url |
|
| 216 | + if (WP_DEBUG && ! EE_Registry::instance()->REQ->is_set('e_reg_url_link')) { |
|
| 217 | + EE_Error::add_error( |
|
| 218 | + __( |
|
| 219 | + 'No transaction information could be retrieved because the registration URL link is missing or invalid.', |
|
| 220 | + 'event_espresso' |
|
| 221 | + ), |
|
| 222 | + __FILE__, |
|
| 223 | + __FUNCTION__, |
|
| 224 | + __LINE__ |
|
| 225 | + ); |
|
| 226 | + return; |
|
| 227 | + } |
|
| 228 | + // check for reg_url_link |
|
| 229 | + $this->_reg_url_link = EE_Registry::instance()->REQ->get('e_reg_url_link'); |
|
| 230 | + } |
|
| 231 | + |
|
| 232 | + |
|
| 233 | + /** |
|
| 234 | + * set_reg_url_link |
|
| 235 | + * |
|
| 236 | + * @param string $reg_url_link |
|
| 237 | + */ |
|
| 238 | + public function set_reg_url_link($reg_url_link = null) |
|
| 239 | + { |
|
| 240 | + $this->_reg_url_link = ! empty($reg_url_link) ? $reg_url_link : $this->_reg_url_link; |
|
| 241 | + } |
|
| 242 | + |
|
| 243 | + |
|
| 244 | + /** |
|
| 245 | + * run - initial module setup |
|
| 246 | + * this method is primarily used for activating resources in the EE_Front_Controller thru the use of filters |
|
| 247 | + * |
|
| 248 | + * @param WP $WP |
|
| 249 | + * @return void |
|
| 250 | + * @throws \EE_Error |
|
| 251 | + */ |
|
| 252 | + public function run($WP) |
|
| 253 | + { |
|
| 254 | + } |
|
| 255 | + |
|
| 256 | + |
|
| 257 | + /** |
|
| 258 | + * load_resources |
|
| 259 | + * |
|
| 260 | + * @return void |
|
| 261 | + * @throws \EE_Error |
|
| 262 | + */ |
|
| 263 | + public function load_resources() |
|
| 264 | + { |
|
| 265 | + $this->_get_reg_url_link(); |
|
| 266 | + // resend_reg_confirmation_email ? |
|
| 267 | + if (EE_Registry::instance()->REQ->is_set('resend')) { |
|
| 268 | + EED_Thank_You_Page::resend_reg_confirmation_email(); |
|
| 269 | + } |
|
| 270 | + EE_Registry::instance()->SSN->clear_session(__CLASS__, __FUNCTION__); |
|
| 271 | + $this->_translate_strings(); |
|
| 272 | + // load assets |
|
| 273 | + add_action('wp_enqueue_scripts', array($this, 'load_js'), 10); |
|
| 274 | + } |
|
| 275 | + |
|
| 276 | + |
|
| 277 | + /** |
|
| 278 | + * load_js |
|
| 279 | + * |
|
| 280 | + * @return void |
|
| 281 | + */ |
|
| 282 | + protected function _translate_strings() |
|
| 283 | + { |
|
| 284 | + EE_Registry::$i18n_js_strings['e_reg_url_link'] = $this->_reg_url_link; |
|
| 285 | + EE_Registry::$i18n_js_strings['initial_access'] = time(); |
|
| 286 | + EE_Registry::$i18n_js_strings['IPN_wait_time'] = EED_Thank_You_Page::IPN_wait_time; |
|
| 287 | + EE_Registry::$i18n_js_strings['TXN_complete'] = EEM_Transaction::complete_status_code; |
|
| 288 | + EE_Registry::$i18n_js_strings['TXN_incomplete'] = EEM_Transaction::incomplete_status_code; |
|
| 289 | + EE_Registry::$i18n_js_strings['checking_for_new_payments'] = __( |
|
| 290 | + 'checking for new payments...', |
|
| 291 | + 'event_espresso' |
|
| 292 | + ); |
|
| 293 | + EE_Registry::$i18n_js_strings['loading_payment_info'] = __( |
|
| 294 | + 'loading payment information...', |
|
| 295 | + 'event_espresso' |
|
| 296 | + ); |
|
| 297 | + EE_Registry::$i18n_js_strings['server_error'] = __( |
|
| 298 | + 'An unknown error occurred on the server while attempting to process your request. Please refresh the page and try again.', |
|
| 299 | + 'event_espresso' |
|
| 300 | + ); |
|
| 301 | + EE_Registry::$i18n_js_strings['slow_IPN'] = apply_filters( |
|
| 302 | + 'EED_Thank_You_Page__load_js__slow_IPN', |
|
| 303 | + sprintf( |
|
| 304 | + __( |
|
| 305 | + '%sThe Payment Notification appears to be taking longer than usual to arrive. Maybe check back later or just wait for your payment and registration confirmation results to be sent to you via email. We apologize for any inconvenience this may have caused.%s', |
|
| 306 | + 'event_espresso' |
|
| 307 | + ), |
|
| 308 | + '<div id="espresso-thank-you-page-slow-IPN-dv" class="ee-attention jst-left">', |
|
| 309 | + '</div>' |
|
| 310 | + ) |
|
| 311 | + ); |
|
| 312 | + } |
|
| 313 | + |
|
| 314 | + |
|
| 315 | + /** |
|
| 316 | + * load_js |
|
| 317 | + * |
|
| 318 | + * @return void |
|
| 319 | + */ |
|
| 320 | + public function load_js() |
|
| 321 | + { |
|
| 322 | + wp_register_script( |
|
| 323 | + 'thank_you_page', |
|
| 324 | + THANK_YOU_ASSETS_URL . 'thank_you_page.js', |
|
| 325 | + array('espresso_core', 'heartbeat'), |
|
| 326 | + EVENT_ESPRESSO_VERSION, |
|
| 327 | + true |
|
| 328 | + ); |
|
| 329 | + wp_enqueue_script('thank_you_page'); |
|
| 330 | + wp_enqueue_style('espresso_default'); |
|
| 331 | + } |
|
| 332 | + |
|
| 333 | + |
|
| 334 | + /** |
|
| 335 | + * init |
|
| 336 | + * |
|
| 337 | + * @return void |
|
| 338 | + * @throws \EE_Error |
|
| 339 | + */ |
|
| 340 | + public function init() |
|
| 341 | + { |
|
| 342 | + $this->_get_reg_url_link(); |
|
| 343 | + if (! $this->get_txn()) { |
|
| 344 | + echo EEH_HTML::div( |
|
| 345 | + EEH_HTML::h4(__('We\'re sorry...', 'event_espresso'), '', '') . |
|
| 346 | + sprintf( |
|
| 347 | + __( |
|
| 348 | + 'This is a system page for displaying transaction information after a purchase.%1$sYou are most likely seeing this notice because you have navigated to this page%1$sthrough some means other than completing a transaction.%1$sSorry for the disappointment, but you will most likely find nothing of interest here.%1$s%1$s', |
|
| 349 | + 'event_espresso' |
|
| 350 | + ), |
|
| 351 | + '<br/>' |
|
| 352 | + ), |
|
| 353 | + '', |
|
| 354 | + 'ee-attention' |
|
| 355 | + ); |
|
| 356 | + return null; |
|
| 357 | + } |
|
| 358 | + // if we've made it to the Thank You page, then let's toggle any "Failed" transactions to "Incomplete" |
|
| 359 | + if ($this->_current_txn->status_ID() === EEM_Transaction::failed_status_code) { |
|
| 360 | + $this->_current_txn->set_status(EEM_Transaction::incomplete_status_code); |
|
| 361 | + $this->_current_txn->save(); |
|
| 362 | + } |
|
| 363 | + $this->_primary_registrant = $this->_current_txn->primary_registration() instanceof EE_Registration |
|
| 364 | + ? $this->_current_txn->primary_registration() |
|
| 365 | + : null; |
|
| 366 | + $this->_is_primary = $this->_primary_registrant->reg_url_link() === $this->_reg_url_link ? true : false; |
|
| 367 | + $show_try_pay_again_link_default = apply_filters( |
|
| 368 | + 'AFEE__EED_Thank_You_Page__init__show_try_pay_again_link_default', |
|
| 369 | + true |
|
| 370 | + ); |
|
| 371 | + $this->_show_try_pay_again_link = $show_try_pay_again_link_default; |
|
| 372 | + // txn status ? |
|
| 373 | + if ($this->_current_txn->is_completed()) { |
|
| 374 | + $this->_show_try_pay_again_link = $show_try_pay_again_link_default; |
|
| 375 | + } elseif ($this->_current_txn->is_incomplete() |
|
| 376 | + && ($this->_primary_registrant->is_approved() |
|
| 377 | + || $this->_primary_registrant->is_pending_payment()) |
|
| 378 | + ) { |
|
| 379 | + $this->_show_try_pay_again_link = true; |
|
| 380 | + } elseif ($this->_primary_registrant->is_approved() || $this->_primary_registrant->is_pending_payment()) { |
|
| 381 | + // its pending |
|
| 382 | + $this->_show_try_pay_again_link = isset( |
|
| 383 | + EE_Registry::instance()->CFG->registration->show_pending_payment_options |
|
| 384 | + ) |
|
| 385 | + && EE_Registry::instance()->CFG |
|
| 386 | + ->registration->show_pending_payment_options |
|
| 387 | + ? true |
|
| 388 | + : $show_try_pay_again_link_default; |
|
| 389 | + } |
|
| 390 | + $this->_payments_closed = ! $this->_current_txn->payment_method() instanceof EE_Payment_Method |
|
| 391 | + ? true |
|
| 392 | + : false; |
|
| 393 | + $this->_is_offline_payment_method = false; |
|
| 394 | + if (// if payment method is unknown |
|
| 395 | + ! $this->_current_txn->payment_method() instanceof EE_Payment_Method |
|
| 396 | + || ( |
|
| 397 | + // or is an offline payment method |
|
| 398 | + $this->_current_txn->payment_method() instanceof EE_Payment_Method |
|
| 399 | + && $this->_current_txn->payment_method()->is_off_line() |
|
| 400 | + ) |
|
| 401 | + ) { |
|
| 402 | + $this->_is_offline_payment_method = true; |
|
| 403 | + } |
|
| 404 | + // link to SPCO |
|
| 405 | + $revisit_spco_url = add_query_arg( |
|
| 406 | + array('ee' => '_register', 'revisit' => true, 'e_reg_url_link' => $this->_reg_url_link), |
|
| 407 | + EE_Registry::instance()->CFG->core->reg_page_url() |
|
| 408 | + ); |
|
| 409 | + // link to SPCO payment_options |
|
| 410 | + $this->_SPCO_payment_options_url = $this->_primary_registrant instanceof EE_Registration |
|
| 411 | + ? $this->_primary_registrant->payment_overview_url() |
|
| 412 | + : add_query_arg( |
|
| 413 | + array('step' => 'payment_options'), |
|
| 414 | + $revisit_spco_url |
|
| 415 | + ); |
|
| 416 | + // link to SPCO attendee_information |
|
| 417 | + $this->_SPCO_attendee_information_url = $this->_primary_registrant instanceof EE_Registration |
|
| 418 | + ? $this->_primary_registrant->edit_attendee_information_url() |
|
| 419 | + : false; |
|
| 420 | + do_action('AHEE__EED_Thank_You_Page__init_end', $this->_current_txn); |
|
| 421 | + // set no cache headers and constants |
|
| 422 | + EE_System::do_not_cache(); |
|
| 423 | + } |
|
| 424 | + |
|
| 425 | + |
|
| 426 | + /** |
|
| 427 | + * display_thank_you_page_results |
|
| 428 | + * |
|
| 429 | + * @return string |
|
| 430 | + * @throws \EE_Error |
|
| 431 | + */ |
|
| 432 | + public function thank_you_page_results() |
|
| 433 | + { |
|
| 434 | + $this->init(); |
|
| 435 | + if (! $this->_current_txn instanceof EE_Transaction) { |
|
| 436 | + return EE_Error::get_notices(); |
|
| 437 | + } |
|
| 438 | + // link to receipt |
|
| 439 | + $template_args['TXN_receipt_url'] = $this->_current_txn->receipt_url('html'); |
|
| 440 | + if (! empty($template_args['TXN_receipt_url'])) { |
|
| 441 | + $template_args['order_conf_desc'] = __( |
|
| 442 | + '%1$sCongratulations%2$sYour registration has been successfully processed.%3$sCheck your email for your registration confirmation or click the button below to view / download / print a full description of your purchases and registration information.', |
|
| 443 | + 'event_espresso' |
|
| 444 | + ); |
|
| 445 | + } else { |
|
| 446 | + $template_args['order_conf_desc'] = __( |
|
| 447 | + '%1$sCongratulations%2$sYour registration has been successfully processed.%3$sCheck your email for your registration confirmation.', |
|
| 448 | + 'event_espresso' |
|
| 449 | + ); |
|
| 450 | + } |
|
| 451 | + $template_args['transaction'] = $this->_current_txn; |
|
| 452 | + $template_args['revisit'] = EE_Registry::instance()->REQ->get('revisit', false); |
|
| 453 | + add_action('AHEE__thank_you_page_overview_template__content', array($this, 'get_registration_details')); |
|
| 454 | + if ($this->_is_primary && ! $this->_current_txn->is_free()) { |
|
| 455 | + add_action('AHEE__thank_you_page_overview_template__content', array($this, 'get_ajax_content')); |
|
| 456 | + } |
|
| 457 | + return EEH_Template::locate_template( |
|
| 458 | + THANK_YOU_TEMPLATES_PATH . 'thank-you-page-overview.template.php', |
|
| 459 | + $template_args, |
|
| 460 | + true, |
|
| 461 | + true |
|
| 462 | + ); |
|
| 463 | + } |
|
| 464 | + |
|
| 465 | + |
|
| 466 | + |
|
| 467 | + /** |
|
| 468 | + * _update_server_wait_time |
|
| 469 | + * |
|
| 470 | + * @param array $thank_you_page_data thank you page portion of the incoming JSON array from the WP heartbeat data |
|
| 471 | + * @return array |
|
| 472 | + * @throws \EE_Error |
|
| 473 | + */ |
|
| 474 | + private function _update_server_wait_time($thank_you_page_data = array()) |
|
| 475 | + { |
|
| 476 | + $response['espresso_thank_you_page'] = array( |
|
| 477 | + 'still_waiting' => isset($thank_you_page_data['initial_access']) |
|
| 478 | + ? time() - $thank_you_page_data['initial_access'] |
|
| 479 | + : 0, |
|
| 480 | + 'txn_status' => $this->_current_txn->status_ID(), |
|
| 481 | + ); |
|
| 482 | + return $response; |
|
| 483 | + } |
|
| 484 | + |
|
| 485 | + |
|
| 486 | + /** |
|
| 487 | + * get_registration_details |
|
| 488 | + * |
|
| 489 | + * @throws \EE_Error |
|
| 490 | + */ |
|
| 491 | + public function get_registration_details() |
|
| 492 | + { |
|
| 493 | + // prepare variables for displaying |
|
| 494 | + $template_args = array(); |
|
| 495 | + $template_args['transaction'] = $this->_current_txn; |
|
| 496 | + $template_args['reg_url_link'] = $this->_reg_url_link; |
|
| 497 | + $template_args['is_primary'] = $this->_is_primary; |
|
| 498 | + $template_args['SPCO_attendee_information_url'] = $this->_SPCO_attendee_information_url; |
|
| 499 | + $template_args['resend_reg_confirmation_url'] = add_query_arg( |
|
| 500 | + array('token' => $this->_reg_url_link, 'resend_reg_confirmation' => 'true'), |
|
| 501 | + EE_Registry::instance()->CFG->core->thank_you_page_url() |
|
| 502 | + ); |
|
| 503 | + // verify template arguments |
|
| 504 | + EEH_Template_Validator::verify_instanceof($template_args['transaction'], '$transaction', 'EE_Transaction'); |
|
| 505 | + EEH_Template_Validator::verify_isnt_null( |
|
| 506 | + $template_args['SPCO_attendee_information_url'], |
|
| 507 | + '$SPCO_attendee_information_url' |
|
| 508 | + ); |
|
| 509 | + echo EEH_Template::locate_template( |
|
| 510 | + THANK_YOU_TEMPLATES_PATH . 'thank-you-page-registration-details.template.php', |
|
| 511 | + $template_args, |
|
| 512 | + true, |
|
| 513 | + true |
|
| 514 | + ); |
|
| 515 | + } |
|
| 516 | + |
|
| 517 | + |
|
| 518 | + /** |
|
| 519 | + * resend_reg_confirmation_email |
|
| 520 | + * |
|
| 521 | + * @throws \EE_Error |
|
| 522 | + */ |
|
| 523 | + public static function resend_reg_confirmation_email() |
|
| 524 | + { |
|
| 525 | + EE_Registry::instance()->load_core('Request_Handler'); |
|
| 526 | + $reg_url_link = EE_Registry::instance()->REQ->get('token'); |
|
| 527 | + // was a REG_ID passed ? |
|
| 528 | + if ($reg_url_link) { |
|
| 529 | + $registration = EE_Registry::instance()->load_model('Registration')->get_one( |
|
| 530 | + array(array('REG_url_link' => $reg_url_link)) |
|
| 531 | + ); |
|
| 532 | + if ($registration instanceof EE_Registration) { |
|
| 533 | + // resend email |
|
| 534 | + EED_Messages::process_resend(array('_REG_ID' => $registration->ID())); |
|
| 535 | + } else { |
|
| 536 | + EE_Error::add_error( |
|
| 537 | + __( |
|
| 538 | + 'The Registration Confirmation email could not be sent because a valid Registration could not be retrieved from the database.', |
|
| 539 | + 'event_espresso' |
|
| 540 | + ), |
|
| 541 | + __FILE__, |
|
| 542 | + __FUNCTION__, |
|
| 543 | + __LINE__ |
|
| 544 | + ); |
|
| 545 | + } |
|
| 546 | + } else { |
|
| 547 | + EE_Error::add_error( |
|
| 548 | + __( |
|
| 549 | + 'The Registration Confirmation email could not be sent because a registration token is missing or invalid.', |
|
| 550 | + 'event_espresso' |
|
| 551 | + ), |
|
| 552 | + __FILE__, |
|
| 553 | + __FUNCTION__, |
|
| 554 | + __LINE__ |
|
| 555 | + ); |
|
| 556 | + } |
|
| 557 | + // request sent via AJAX ? |
|
| 558 | + if (EE_FRONT_AJAX) { |
|
| 559 | + echo wp_json_encode(EE_Error::get_notices(false)); |
|
| 560 | + die(); |
|
| 561 | + // or was JS disabled ? |
|
| 562 | + } else { |
|
| 563 | + // save errors so that they get picked up on the next request |
|
| 564 | + EE_Error::get_notices(true, true); |
|
| 565 | + wp_safe_redirect( |
|
| 566 | + add_query_arg( |
|
| 567 | + array('e_reg_url_link' => $reg_url_link), |
|
| 568 | + EE_Registry::instance()->CFG->core->thank_you_page_url() |
|
| 569 | + ) |
|
| 570 | + ); |
|
| 571 | + } |
|
| 572 | + } |
|
| 573 | + |
|
| 574 | + |
|
| 575 | + /** |
|
| 576 | + * get_ajax_content |
|
| 577 | + * |
|
| 578 | + * @return void |
|
| 579 | + * @throws \EE_Error |
|
| 580 | + */ |
|
| 581 | + public function get_ajax_content() |
|
| 582 | + { |
|
| 583 | + if (! $this->get_txn()) { |
|
| 584 | + return; |
|
| 585 | + } |
|
| 586 | + // first determine which event(s) require pre-approval or not |
|
| 587 | + $events = array(); |
|
| 588 | + $events_requiring_pre_approval = array(); |
|
| 589 | + foreach ($this->_current_txn->registrations() as $registration) { |
|
| 590 | + if ($registration instanceof EE_Registration) { |
|
| 591 | + $event = $registration->event(); |
|
| 592 | + if ($event instanceof EE_Event) { |
|
| 593 | + if ($registration->is_not_approved() && $registration->event() instanceof EE_Event) { |
|
| 594 | + $events_requiring_pre_approval[ $event->ID() ] = $event; |
|
| 595 | + } else { |
|
| 596 | + $events[ $event->ID() ] = $event; |
|
| 597 | + } |
|
| 598 | + } |
|
| 599 | + } |
|
| 600 | + } |
|
| 601 | + $this->display_details_for_events_requiring_pre_approval($events_requiring_pre_approval); |
|
| 602 | + $this->display_details_for_events($events); |
|
| 603 | + } |
|
| 604 | + |
|
| 605 | + |
|
| 606 | + /** |
|
| 607 | + * display_details_for_events |
|
| 608 | + * |
|
| 609 | + * @param EE_Event[] $events |
|
| 610 | + * @return void |
|
| 611 | + */ |
|
| 612 | + public function display_details_for_events($events = array()) |
|
| 613 | + { |
|
| 614 | + if (! empty($events)) { |
|
| 615 | + ?> |
|
| 616 | 616 | <div id="espresso-thank-you-page-ajax-content-dv"> |
| 617 | 617 | <div id="espresso-thank-you-page-ajax-transaction-dv"></div> |
| 618 | 618 | <div id="espresso-thank-you-page-ajax-payment-dv"></div> |
@@ -620,19 +620,19 @@ discard block |
||
| 620 | 620 | <div id="ee-ajax-loading-dv" class="float-left lt-blue-text"> |
| 621 | 621 | <span class="dashicons dashicons-upload"></span><span id="ee-ajax-loading-msg-spn"> |
| 622 | 622 | <?php _e( |
| 623 | - 'loading transaction and payment information...', |
|
| 624 | - 'event_espresso' |
|
| 625 | - ); ?></span> |
|
| 623 | + 'loading transaction and payment information...', |
|
| 624 | + 'event_espresso' |
|
| 625 | + ); ?></span> |
|
| 626 | 626 | </div> |
| 627 | 627 | <?php if (! $this->_is_offline_payment_method && ! $this->_payments_closed) : ?> |
| 628 | 628 | <p id="ee-ajax-loading-pg" class="highlight-bg small-text clear"> |
| 629 | 629 | <?php echo apply_filters( |
| 630 | - 'EED_Thank_You_Page__get_ajax_content__waiting_for_IPN_msg', |
|
| 631 | - __( |
|
| 632 | - 'Some payment gateways can take 15 minutes or more to return their payment notification, so please be patient if you require payment confirmation as soon as possible. Please note that as soon as everything is finalized, we will send your full payment and registration confirmation results to you via email.', |
|
| 633 | - 'event_espresso' |
|
| 634 | - ) |
|
| 635 | - ); ?> |
|
| 630 | + 'EED_Thank_You_Page__get_ajax_content__waiting_for_IPN_msg', |
|
| 631 | + __( |
|
| 632 | + 'Some payment gateways can take 15 minutes or more to return their payment notification, so please be patient if you require payment confirmation as soon as possible. Please note that as soon as everything is finalized, we will send your full payment and registration confirmation results to you via email.', |
|
| 633 | + 'event_espresso' |
|
| 634 | + ) |
|
| 635 | + ); ?> |
|
| 636 | 636 | <br/> |
| 637 | 637 | <span class="jst-rght ee-block small-text lt-grey-text"> |
| 638 | 638 | <?php _e('current wait time ', 'event_espresso'); ?> |
@@ -643,117 +643,117 @@ discard block |
||
| 643 | 643 | <div class="clear"></div> |
| 644 | 644 | </div> |
| 645 | 645 | <?php |
| 646 | - } |
|
| 647 | - } |
|
| 648 | - |
|
| 649 | - |
|
| 650 | - /** |
|
| 651 | - * display_details_for_events_requiring_pre_approval |
|
| 652 | - * |
|
| 653 | - * @param EE_Event[] $events |
|
| 654 | - * @return void |
|
| 655 | - */ |
|
| 656 | - public function display_details_for_events_requiring_pre_approval($events = array()) |
|
| 657 | - { |
|
| 658 | - if (! empty($events)) { |
|
| 659 | - ?> |
|
| 646 | + } |
|
| 647 | + } |
|
| 648 | + |
|
| 649 | + |
|
| 650 | + /** |
|
| 651 | + * display_details_for_events_requiring_pre_approval |
|
| 652 | + * |
|
| 653 | + * @param EE_Event[] $events |
|
| 654 | + * @return void |
|
| 655 | + */ |
|
| 656 | + public function display_details_for_events_requiring_pre_approval($events = array()) |
|
| 657 | + { |
|
| 658 | + if (! empty($events)) { |
|
| 659 | + ?> |
|
| 660 | 660 | <div id="espresso-thank-you-page-not-approved-message-dv"> |
| 661 | 661 | <h4 class="orange-text"><?php _e('Important Notice:', 'event_espresso'); ?></h4> |
| 662 | 662 | <p id="events-requiring-pre-approval-pg" class="small-text"> |
| 663 | 663 | <?php echo apply_filters( |
| 664 | - 'AHEE__EED_Thank_You_Page__get_ajax_content__not_approved_message', |
|
| 665 | - __( |
|
| 666 | - 'The following Event(s) you have registered for do not require payment at this time and will not be billed for during this transaction. Billing will only occur after all attendees have been approved by the event organizer. You will be notified when your registration has been processed. If this is a free event, then no billing will occur.', |
|
| 667 | - 'event_espresso' |
|
| 668 | - ) |
|
| 669 | - ); ?> |
|
| 664 | + 'AHEE__EED_Thank_You_Page__get_ajax_content__not_approved_message', |
|
| 665 | + __( |
|
| 666 | + 'The following Event(s) you have registered for do not require payment at this time and will not be billed for during this transaction. Billing will only occur after all attendees have been approved by the event organizer. You will be notified when your registration has been processed. If this is a free event, then no billing will occur.', |
|
| 667 | + 'event_espresso' |
|
| 668 | + ) |
|
| 669 | + ); ?> |
|
| 670 | 670 | </p> |
| 671 | 671 | <ul class="events-requiring-pre-approval-ul"> |
| 672 | 672 | <?php |
| 673 | - foreach ($events as $event) { |
|
| 674 | - if ($event instanceof EE_Event) { |
|
| 675 | - echo '<li><span class="dashicons dashicons-marker ee-icon-size-16 orange-text"></span>', |
|
| 676 | - $event->name(), |
|
| 677 | - '</li>'; |
|
| 678 | - } |
|
| 679 | - } ?> |
|
| 673 | + foreach ($events as $event) { |
|
| 674 | + if ($event instanceof EE_Event) { |
|
| 675 | + echo '<li><span class="dashicons dashicons-marker ee-icon-size-16 orange-text"></span>', |
|
| 676 | + $event->name(), |
|
| 677 | + '</li>'; |
|
| 678 | + } |
|
| 679 | + } ?> |
|
| 680 | 680 | </ul> |
| 681 | 681 | <div class="clear"></div> |
| 682 | 682 | </div> |
| 683 | 683 | <?php |
| 684 | - } |
|
| 685 | - } |
|
| 686 | - |
|
| 687 | - |
|
| 688 | - /** |
|
| 689 | - * get_transaction_details |
|
| 690 | - * |
|
| 691 | - * @return string |
|
| 692 | - * @throws \EE_Error |
|
| 693 | - */ |
|
| 694 | - public function get_transaction_details() |
|
| 695 | - { |
|
| 696 | - // prepare variables for displaying |
|
| 697 | - $template_args = array(); |
|
| 698 | - $template_args['transaction'] = $this->_current_txn; |
|
| 699 | - $template_args['reg_url_link'] = $this->_reg_url_link; |
|
| 700 | - $template_args['primary_registrant_name'] = $this->_primary_registrant->attendee()->full_name(true); |
|
| 701 | - // link to SPCO payment_options |
|
| 702 | - $template_args['show_try_pay_again_link'] = $this->_show_try_pay_again_link; |
|
| 703 | - $template_args['SPCO_payment_options_url'] = $this->_SPCO_payment_options_url; |
|
| 704 | - // verify template arguments |
|
| 705 | - EEH_Template_Validator::verify_instanceof($template_args['transaction'], '$transaction', 'EE_Transaction'); |
|
| 706 | - EEH_Template_Validator::verify_isnt_null( |
|
| 707 | - $template_args['show_try_pay_again_link'], |
|
| 708 | - '$show_try_pay_again_link' |
|
| 709 | - ); |
|
| 710 | - EEH_Template_Validator::verify_isnt_null( |
|
| 711 | - $template_args['SPCO_payment_options_url'], |
|
| 712 | - '$SPCO_payment_options_url' |
|
| 713 | - ); |
|
| 714 | - return EEH_Template::locate_template( |
|
| 715 | - THANK_YOU_TEMPLATES_PATH . 'thank-you-page-transaction-details.template.php', |
|
| 716 | - $template_args, |
|
| 717 | - true, |
|
| 718 | - true |
|
| 719 | - ); |
|
| 720 | - } |
|
| 721 | - |
|
| 722 | - |
|
| 723 | - /** |
|
| 724 | - * get_payment_row_html |
|
| 725 | - * |
|
| 726 | - * @param EE_Payment $payment |
|
| 727 | - * @return string |
|
| 728 | - * @throws \EE_Error |
|
| 729 | - */ |
|
| 730 | - public function get_payment_row_html($payment = null) |
|
| 731 | - { |
|
| 732 | - $html = ''; |
|
| 733 | - if ($payment instanceof EE_Payment) { |
|
| 734 | - if ($payment->payment_method() instanceof EE_Payment_Method |
|
| 735 | - && $payment->status() === EEM_Payment::status_id_failed |
|
| 736 | - && $payment->payment_method()->is_off_site() |
|
| 737 | - ) { |
|
| 738 | - // considering the registrant has made it to the Thank You page, |
|
| 739 | - // any failed payments may actually be pending and the IPN is just slow |
|
| 740 | - // so let's |
|
| 741 | - $payment->set_status(EEM_Payment::status_id_pending); |
|
| 742 | - } |
|
| 743 | - $payment_declined_msg = $payment->STS_ID() === EEM_Payment::status_id_declined |
|
| 744 | - ? '<br /><span class="small-text">' . $payment->gateway_response() . '</span>' |
|
| 745 | - : ''; |
|
| 746 | - $html .= ' |
|
| 684 | + } |
|
| 685 | + } |
|
| 686 | + |
|
| 687 | + |
|
| 688 | + /** |
|
| 689 | + * get_transaction_details |
|
| 690 | + * |
|
| 691 | + * @return string |
|
| 692 | + * @throws \EE_Error |
|
| 693 | + */ |
|
| 694 | + public function get_transaction_details() |
|
| 695 | + { |
|
| 696 | + // prepare variables for displaying |
|
| 697 | + $template_args = array(); |
|
| 698 | + $template_args['transaction'] = $this->_current_txn; |
|
| 699 | + $template_args['reg_url_link'] = $this->_reg_url_link; |
|
| 700 | + $template_args['primary_registrant_name'] = $this->_primary_registrant->attendee()->full_name(true); |
|
| 701 | + // link to SPCO payment_options |
|
| 702 | + $template_args['show_try_pay_again_link'] = $this->_show_try_pay_again_link; |
|
| 703 | + $template_args['SPCO_payment_options_url'] = $this->_SPCO_payment_options_url; |
|
| 704 | + // verify template arguments |
|
| 705 | + EEH_Template_Validator::verify_instanceof($template_args['transaction'], '$transaction', 'EE_Transaction'); |
|
| 706 | + EEH_Template_Validator::verify_isnt_null( |
|
| 707 | + $template_args['show_try_pay_again_link'], |
|
| 708 | + '$show_try_pay_again_link' |
|
| 709 | + ); |
|
| 710 | + EEH_Template_Validator::verify_isnt_null( |
|
| 711 | + $template_args['SPCO_payment_options_url'], |
|
| 712 | + '$SPCO_payment_options_url' |
|
| 713 | + ); |
|
| 714 | + return EEH_Template::locate_template( |
|
| 715 | + THANK_YOU_TEMPLATES_PATH . 'thank-you-page-transaction-details.template.php', |
|
| 716 | + $template_args, |
|
| 717 | + true, |
|
| 718 | + true |
|
| 719 | + ); |
|
| 720 | + } |
|
| 721 | + |
|
| 722 | + |
|
| 723 | + /** |
|
| 724 | + * get_payment_row_html |
|
| 725 | + * |
|
| 726 | + * @param EE_Payment $payment |
|
| 727 | + * @return string |
|
| 728 | + * @throws \EE_Error |
|
| 729 | + */ |
|
| 730 | + public function get_payment_row_html($payment = null) |
|
| 731 | + { |
|
| 732 | + $html = ''; |
|
| 733 | + if ($payment instanceof EE_Payment) { |
|
| 734 | + if ($payment->payment_method() instanceof EE_Payment_Method |
|
| 735 | + && $payment->status() === EEM_Payment::status_id_failed |
|
| 736 | + && $payment->payment_method()->is_off_site() |
|
| 737 | + ) { |
|
| 738 | + // considering the registrant has made it to the Thank You page, |
|
| 739 | + // any failed payments may actually be pending and the IPN is just slow |
|
| 740 | + // so let's |
|
| 741 | + $payment->set_status(EEM_Payment::status_id_pending); |
|
| 742 | + } |
|
| 743 | + $payment_declined_msg = $payment->STS_ID() === EEM_Payment::status_id_declined |
|
| 744 | + ? '<br /><span class="small-text">' . $payment->gateway_response() . '</span>' |
|
| 745 | + : ''; |
|
| 746 | + $html .= ' |
|
| 747 | 747 | <tr> |
| 748 | 748 | <td> |
| 749 | 749 | ' . $payment->timestamp() . ' |
| 750 | 750 | </td> |
| 751 | 751 | <td> |
| 752 | 752 | ' . ( |
| 753 | - $payment->payment_method() instanceof EE_Payment_Method |
|
| 754 | - ? $payment->payment_method()->name() |
|
| 755 | - : __('Unknown', 'event_espresso') |
|
| 756 | - ) . ' |
|
| 753 | + $payment->payment_method() instanceof EE_Payment_Method |
|
| 754 | + ? $payment->payment_method()->name() |
|
| 755 | + : __('Unknown', 'event_espresso') |
|
| 756 | + ) . ' |
|
| 757 | 757 | </td> |
| 758 | 758 | <td class="jst-rght"> |
| 759 | 759 | ' . EEH_Template::format_currency($payment->amount()) . ' |
@@ -762,83 +762,83 @@ discard block |
||
| 762 | 762 | ' . $payment->pretty_status(true) . $payment_declined_msg . ' |
| 763 | 763 | </td> |
| 764 | 764 | </tr>'; |
| 765 | - do_action('AHEE__thank_you_page_payment_details_template__after_each_payment', $payment); |
|
| 766 | - } |
|
| 767 | - return $html; |
|
| 768 | - } |
|
| 769 | - |
|
| 770 | - |
|
| 771 | - /** |
|
| 772 | - * get_payment_details |
|
| 773 | - * |
|
| 774 | - * @param array $payments |
|
| 775 | - * @return string |
|
| 776 | - * @throws \EE_Error |
|
| 777 | - */ |
|
| 778 | - public function get_payment_details($payments = array()) |
|
| 779 | - { |
|
| 780 | - // prepare variables for displaying |
|
| 781 | - $template_args = array(); |
|
| 782 | - $template_args['transaction'] = $this->_current_txn; |
|
| 783 | - $template_args['reg_url_link'] = $this->_reg_url_link; |
|
| 784 | - $template_args['payments'] = array(); |
|
| 785 | - foreach ($payments as $payment) { |
|
| 786 | - $template_args['payments'][] = $this->get_payment_row_html($payment); |
|
| 787 | - } |
|
| 788 | - // create a hacky payment object, but dont save it |
|
| 789 | - $payment = EE_Payment::new_instance( |
|
| 790 | - array( |
|
| 791 | - 'TXN_ID' => $this->_current_txn->ID(), |
|
| 792 | - 'STS_ID' => EEM_Payment::status_id_pending, |
|
| 793 | - 'PAY_timestamp' => time(), |
|
| 794 | - 'PAY_amount' => $this->_current_txn->total(), |
|
| 795 | - 'PMD_ID' => $this->_current_txn->payment_method_ID(), |
|
| 796 | - ) |
|
| 797 | - ); |
|
| 798 | - $payment_method = $this->_current_txn->payment_method(); |
|
| 799 | - if ($payment_method instanceof EE_Payment_Method && $payment_method->type_obj() instanceof EE_PMT_Base) { |
|
| 800 | - $template_args['gateway_content'] = $payment_method->type_obj()->payment_overview_content($payment); |
|
| 801 | - } else { |
|
| 802 | - $template_args['gateway_content'] = ''; |
|
| 803 | - } |
|
| 804 | - // link to SPCO payment_options |
|
| 805 | - $template_args['show_try_pay_again_link'] = $this->_show_try_pay_again_link; |
|
| 806 | - $template_args['SPCO_payment_options_url'] = $this->_SPCO_payment_options_url; |
|
| 807 | - // verify template arguments |
|
| 808 | - EEH_Template_Validator::verify_instanceof($template_args['transaction'], '$transaction', 'EE_Transaction'); |
|
| 809 | - EEH_Template_Validator::verify_isnt_null($template_args['payments'], '$payments'); |
|
| 810 | - EEH_Template_Validator::verify_isnt_null( |
|
| 811 | - $template_args['show_try_pay_again_link'], |
|
| 812 | - '$show_try_pay_again_link' |
|
| 813 | - ); |
|
| 814 | - EEH_Template_Validator::verify_isnt_null($template_args['gateway_content'], '$gateway_content'); |
|
| 815 | - EEH_Template_Validator::verify_isnt_null( |
|
| 816 | - $template_args['SPCO_payment_options_url'], |
|
| 817 | - '$SPCO_payment_options_url' |
|
| 818 | - ); |
|
| 819 | - return EEH_Template::locate_template( |
|
| 820 | - THANK_YOU_TEMPLATES_PATH . 'thank-you-page-payment-details.template.php', |
|
| 821 | - $template_args, |
|
| 822 | - true, |
|
| 823 | - true |
|
| 824 | - ); |
|
| 825 | - } |
|
| 826 | - |
|
| 827 | - |
|
| 828 | - /** |
|
| 829 | - * get_payment_details |
|
| 830 | - * |
|
| 831 | - * @param array $payments |
|
| 832 | - * @return string |
|
| 833 | - * @throws \EE_Error |
|
| 834 | - */ |
|
| 835 | - public function get_new_payments($payments = array()) |
|
| 836 | - { |
|
| 837 | - $payments_html = ''; |
|
| 838 | - // prepare variables for displaying |
|
| 839 | - foreach ($payments as $payment) { |
|
| 840 | - $payments_html .= $this->get_payment_row_html($payment); |
|
| 841 | - } |
|
| 842 | - return $payments_html; |
|
| 843 | - } |
|
| 765 | + do_action('AHEE__thank_you_page_payment_details_template__after_each_payment', $payment); |
|
| 766 | + } |
|
| 767 | + return $html; |
|
| 768 | + } |
|
| 769 | + |
|
| 770 | + |
|
| 771 | + /** |
|
| 772 | + * get_payment_details |
|
| 773 | + * |
|
| 774 | + * @param array $payments |
|
| 775 | + * @return string |
|
| 776 | + * @throws \EE_Error |
|
| 777 | + */ |
|
| 778 | + public function get_payment_details($payments = array()) |
|
| 779 | + { |
|
| 780 | + // prepare variables for displaying |
|
| 781 | + $template_args = array(); |
|
| 782 | + $template_args['transaction'] = $this->_current_txn; |
|
| 783 | + $template_args['reg_url_link'] = $this->_reg_url_link; |
|
| 784 | + $template_args['payments'] = array(); |
|
| 785 | + foreach ($payments as $payment) { |
|
| 786 | + $template_args['payments'][] = $this->get_payment_row_html($payment); |
|
| 787 | + } |
|
| 788 | + // create a hacky payment object, but dont save it |
|
| 789 | + $payment = EE_Payment::new_instance( |
|
| 790 | + array( |
|
| 791 | + 'TXN_ID' => $this->_current_txn->ID(), |
|
| 792 | + 'STS_ID' => EEM_Payment::status_id_pending, |
|
| 793 | + 'PAY_timestamp' => time(), |
|
| 794 | + 'PAY_amount' => $this->_current_txn->total(), |
|
| 795 | + 'PMD_ID' => $this->_current_txn->payment_method_ID(), |
|
| 796 | + ) |
|
| 797 | + ); |
|
| 798 | + $payment_method = $this->_current_txn->payment_method(); |
|
| 799 | + if ($payment_method instanceof EE_Payment_Method && $payment_method->type_obj() instanceof EE_PMT_Base) { |
|
| 800 | + $template_args['gateway_content'] = $payment_method->type_obj()->payment_overview_content($payment); |
|
| 801 | + } else { |
|
| 802 | + $template_args['gateway_content'] = ''; |
|
| 803 | + } |
|
| 804 | + // link to SPCO payment_options |
|
| 805 | + $template_args['show_try_pay_again_link'] = $this->_show_try_pay_again_link; |
|
| 806 | + $template_args['SPCO_payment_options_url'] = $this->_SPCO_payment_options_url; |
|
| 807 | + // verify template arguments |
|
| 808 | + EEH_Template_Validator::verify_instanceof($template_args['transaction'], '$transaction', 'EE_Transaction'); |
|
| 809 | + EEH_Template_Validator::verify_isnt_null($template_args['payments'], '$payments'); |
|
| 810 | + EEH_Template_Validator::verify_isnt_null( |
|
| 811 | + $template_args['show_try_pay_again_link'], |
|
| 812 | + '$show_try_pay_again_link' |
|
| 813 | + ); |
|
| 814 | + EEH_Template_Validator::verify_isnt_null($template_args['gateway_content'], '$gateway_content'); |
|
| 815 | + EEH_Template_Validator::verify_isnt_null( |
|
| 816 | + $template_args['SPCO_payment_options_url'], |
|
| 817 | + '$SPCO_payment_options_url' |
|
| 818 | + ); |
|
| 819 | + return EEH_Template::locate_template( |
|
| 820 | + THANK_YOU_TEMPLATES_PATH . 'thank-you-page-payment-details.template.php', |
|
| 821 | + $template_args, |
|
| 822 | + true, |
|
| 823 | + true |
|
| 824 | + ); |
|
| 825 | + } |
|
| 826 | + |
|
| 827 | + |
|
| 828 | + /** |
|
| 829 | + * get_payment_details |
|
| 830 | + * |
|
| 831 | + * @param array $payments |
|
| 832 | + * @return string |
|
| 833 | + * @throws \EE_Error |
|
| 834 | + */ |
|
| 835 | + public function get_new_payments($payments = array()) |
|
| 836 | + { |
|
| 837 | + $payments_html = ''; |
|
| 838 | + // prepare variables for displaying |
|
| 839 | + foreach ($payments as $payment) { |
|
| 840 | + $payments_html .= $this->get_payment_row_html($payment); |
|
| 841 | + } |
|
| 842 | + return $payments_html; |
|
| 843 | + } |
|
| 844 | 844 | } |
@@ -126,8 +126,8 @@ discard block |
||
| 126 | 126 | */ |
| 127 | 127 | public static function set_definitions() |
| 128 | 128 | { |
| 129 | - define('THANK_YOU_ASSETS_URL', plugin_dir_url(__FILE__) . 'assets' . DS); |
|
| 130 | - define('THANK_YOU_TEMPLATES_PATH', str_replace('\\', DS, plugin_dir_path(__FILE__)) . 'templates' . DS); |
|
| 129 | + define('THANK_YOU_ASSETS_URL', plugin_dir_url(__FILE__).'assets'.DS); |
|
| 130 | + define('THANK_YOU_TEMPLATES_PATH', str_replace('\\', DS, plugin_dir_path(__FILE__)).'templates'.DS); |
|
| 131 | 131 | } |
| 132 | 132 | |
| 133 | 133 | |
@@ -142,7 +142,7 @@ discard block |
||
| 142 | 142 | return $this->_current_txn; |
| 143 | 143 | } |
| 144 | 144 | $TXN_model = EE_Registry::instance()->load_model('Transaction'); |
| 145 | - if (! $TXN_model instanceof EEM_Transaction) { |
|
| 145 | + if ( ! $TXN_model instanceof EEM_Transaction) { |
|
| 146 | 146 | EE_Error::add_error( |
| 147 | 147 | __('The transaction model could not be established.', 'event_espresso'), |
| 148 | 148 | __FILE__, |
@@ -179,7 +179,7 @@ discard block |
||
| 179 | 179 | */ |
| 180 | 180 | public function get_txn_payments($since = 0) |
| 181 | 181 | { |
| 182 | - if (! $this->get_txn()) { |
|
| 182 | + if ( ! $this->get_txn()) { |
|
| 183 | 183 | return false; |
| 184 | 184 | } |
| 185 | 185 | $args = array('order_by' => array('PAY_timestamp' => 'ASC')); |
@@ -209,7 +209,7 @@ discard block |
||
| 209 | 209 | */ |
| 210 | 210 | private function _get_reg_url_link() |
| 211 | 211 | { |
| 212 | - if (! empty($this->_reg_url_link)) { |
|
| 212 | + if ( ! empty($this->_reg_url_link)) { |
|
| 213 | 213 | return; |
| 214 | 214 | } |
| 215 | 215 | // only do thank you page stuff if we have a REG_url_link in the url |
@@ -321,7 +321,7 @@ discard block |
||
| 321 | 321 | { |
| 322 | 322 | wp_register_script( |
| 323 | 323 | 'thank_you_page', |
| 324 | - THANK_YOU_ASSETS_URL . 'thank_you_page.js', |
|
| 324 | + THANK_YOU_ASSETS_URL.'thank_you_page.js', |
|
| 325 | 325 | array('espresso_core', 'heartbeat'), |
| 326 | 326 | EVENT_ESPRESSO_VERSION, |
| 327 | 327 | true |
@@ -340,9 +340,9 @@ discard block |
||
| 340 | 340 | public function init() |
| 341 | 341 | { |
| 342 | 342 | $this->_get_reg_url_link(); |
| 343 | - if (! $this->get_txn()) { |
|
| 343 | + if ( ! $this->get_txn()) { |
|
| 344 | 344 | echo EEH_HTML::div( |
| 345 | - EEH_HTML::h4(__('We\'re sorry...', 'event_espresso'), '', '') . |
|
| 345 | + EEH_HTML::h4(__('We\'re sorry...', 'event_espresso'), '', ''). |
|
| 346 | 346 | sprintf( |
| 347 | 347 | __( |
| 348 | 348 | 'This is a system page for displaying transaction information after a purchase.%1$sYou are most likely seeing this notice because you have navigated to this page%1$sthrough some means other than completing a transaction.%1$sSorry for the disappointment, but you will most likely find nothing of interest here.%1$s%1$s', |
@@ -432,12 +432,12 @@ discard block |
||
| 432 | 432 | public function thank_you_page_results() |
| 433 | 433 | { |
| 434 | 434 | $this->init(); |
| 435 | - if (! $this->_current_txn instanceof EE_Transaction) { |
|
| 435 | + if ( ! $this->_current_txn instanceof EE_Transaction) { |
|
| 436 | 436 | return EE_Error::get_notices(); |
| 437 | 437 | } |
| 438 | 438 | // link to receipt |
| 439 | 439 | $template_args['TXN_receipt_url'] = $this->_current_txn->receipt_url('html'); |
| 440 | - if (! empty($template_args['TXN_receipt_url'])) { |
|
| 440 | + if ( ! empty($template_args['TXN_receipt_url'])) { |
|
| 441 | 441 | $template_args['order_conf_desc'] = __( |
| 442 | 442 | '%1$sCongratulations%2$sYour registration has been successfully processed.%3$sCheck your email for your registration confirmation or click the button below to view / download / print a full description of your purchases and registration information.', |
| 443 | 443 | 'event_espresso' |
@@ -455,7 +455,7 @@ discard block |
||
| 455 | 455 | add_action('AHEE__thank_you_page_overview_template__content', array($this, 'get_ajax_content')); |
| 456 | 456 | } |
| 457 | 457 | return EEH_Template::locate_template( |
| 458 | - THANK_YOU_TEMPLATES_PATH . 'thank-you-page-overview.template.php', |
|
| 458 | + THANK_YOU_TEMPLATES_PATH.'thank-you-page-overview.template.php', |
|
| 459 | 459 | $template_args, |
| 460 | 460 | true, |
| 461 | 461 | true |
@@ -507,7 +507,7 @@ discard block |
||
| 507 | 507 | '$SPCO_attendee_information_url' |
| 508 | 508 | ); |
| 509 | 509 | echo EEH_Template::locate_template( |
| 510 | - THANK_YOU_TEMPLATES_PATH . 'thank-you-page-registration-details.template.php', |
|
| 510 | + THANK_YOU_TEMPLATES_PATH.'thank-you-page-registration-details.template.php', |
|
| 511 | 511 | $template_args, |
| 512 | 512 | true, |
| 513 | 513 | true |
@@ -580,7 +580,7 @@ discard block |
||
| 580 | 580 | */ |
| 581 | 581 | public function get_ajax_content() |
| 582 | 582 | { |
| 583 | - if (! $this->get_txn()) { |
|
| 583 | + if ( ! $this->get_txn()) { |
|
| 584 | 584 | return; |
| 585 | 585 | } |
| 586 | 586 | // first determine which event(s) require pre-approval or not |
@@ -591,9 +591,9 @@ discard block |
||
| 591 | 591 | $event = $registration->event(); |
| 592 | 592 | if ($event instanceof EE_Event) { |
| 593 | 593 | if ($registration->is_not_approved() && $registration->event() instanceof EE_Event) { |
| 594 | - $events_requiring_pre_approval[ $event->ID() ] = $event; |
|
| 594 | + $events_requiring_pre_approval[$event->ID()] = $event; |
|
| 595 | 595 | } else { |
| 596 | - $events[ $event->ID() ] = $event; |
|
| 596 | + $events[$event->ID()] = $event; |
|
| 597 | 597 | } |
| 598 | 598 | } |
| 599 | 599 | } |
@@ -611,7 +611,7 @@ discard block |
||
| 611 | 611 | */ |
| 612 | 612 | public function display_details_for_events($events = array()) |
| 613 | 613 | { |
| 614 | - if (! empty($events)) { |
|
| 614 | + if ( ! empty($events)) { |
|
| 615 | 615 | ?> |
| 616 | 616 | <div id="espresso-thank-you-page-ajax-content-dv"> |
| 617 | 617 | <div id="espresso-thank-you-page-ajax-transaction-dv"></div> |
@@ -624,7 +624,7 @@ discard block |
||
| 624 | 624 | 'event_espresso' |
| 625 | 625 | ); ?></span> |
| 626 | 626 | </div> |
| 627 | - <?php if (! $this->_is_offline_payment_method && ! $this->_payments_closed) : ?> |
|
| 627 | + <?php if ( ! $this->_is_offline_payment_method && ! $this->_payments_closed) : ?> |
|
| 628 | 628 | <p id="ee-ajax-loading-pg" class="highlight-bg small-text clear"> |
| 629 | 629 | <?php echo apply_filters( |
| 630 | 630 | 'EED_Thank_You_Page__get_ajax_content__waiting_for_IPN_msg', |
@@ -655,7 +655,7 @@ discard block |
||
| 655 | 655 | */ |
| 656 | 656 | public function display_details_for_events_requiring_pre_approval($events = array()) |
| 657 | 657 | { |
| 658 | - if (! empty($events)) { |
|
| 658 | + if ( ! empty($events)) { |
|
| 659 | 659 | ?> |
| 660 | 660 | <div id="espresso-thank-you-page-not-approved-message-dv"> |
| 661 | 661 | <h4 class="orange-text"><?php _e('Important Notice:', 'event_espresso'); ?></h4> |
@@ -712,7 +712,7 @@ discard block |
||
| 712 | 712 | '$SPCO_payment_options_url' |
| 713 | 713 | ); |
| 714 | 714 | return EEH_Template::locate_template( |
| 715 | - THANK_YOU_TEMPLATES_PATH . 'thank-you-page-transaction-details.template.php', |
|
| 715 | + THANK_YOU_TEMPLATES_PATH.'thank-you-page-transaction-details.template.php', |
|
| 716 | 716 | $template_args, |
| 717 | 717 | true, |
| 718 | 718 | true |
@@ -741,25 +741,25 @@ discard block |
||
| 741 | 741 | $payment->set_status(EEM_Payment::status_id_pending); |
| 742 | 742 | } |
| 743 | 743 | $payment_declined_msg = $payment->STS_ID() === EEM_Payment::status_id_declined |
| 744 | - ? '<br /><span class="small-text">' . $payment->gateway_response() . '</span>' |
|
| 744 | + ? '<br /><span class="small-text">'.$payment->gateway_response().'</span>' |
|
| 745 | 745 | : ''; |
| 746 | 746 | $html .= ' |
| 747 | 747 | <tr> |
| 748 | 748 | <td> |
| 749 | - ' . $payment->timestamp() . ' |
|
| 749 | + ' . $payment->timestamp().' |
|
| 750 | 750 | </td> |
| 751 | 751 | <td> |
| 752 | 752 | ' . ( |
| 753 | 753 | $payment->payment_method() instanceof EE_Payment_Method |
| 754 | 754 | ? $payment->payment_method()->name() |
| 755 | 755 | : __('Unknown', 'event_espresso') |
| 756 | - ) . ' |
|
| 756 | + ).' |
|
| 757 | 757 | </td> |
| 758 | 758 | <td class="jst-rght"> |
| 759 | - ' . EEH_Template::format_currency($payment->amount()) . ' |
|
| 759 | + ' . EEH_Template::format_currency($payment->amount()).' |
|
| 760 | 760 | </td> |
| 761 | 761 | <td class="jst-rght" style="line-height:1;"> |
| 762 | - ' . $payment->pretty_status(true) . $payment_declined_msg . ' |
|
| 762 | + ' . $payment->pretty_status(true).$payment_declined_msg.' |
|
| 763 | 763 | </td> |
| 764 | 764 | </tr>'; |
| 765 | 765 | do_action('AHEE__thank_you_page_payment_details_template__after_each_payment', $payment); |
@@ -817,7 +817,7 @@ discard block |
||
| 817 | 817 | '$SPCO_payment_options_url' |
| 818 | 818 | ); |
| 819 | 819 | return EEH_Template::locate_template( |
| 820 | - THANK_YOU_TEMPLATES_PATH . 'thank-you-page-payment-details.template.php', |
|
| 820 | + THANK_YOU_TEMPLATES_PATH.'thank-you-page-payment-details.template.php', |
|
| 821 | 821 | $template_args, |
| 822 | 822 | true, |
| 823 | 823 | true |
@@ -16,1257 +16,1257 @@ |
||
| 16 | 16 | { |
| 17 | 17 | |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * Extend_Events_Admin_Page constructor. |
|
| 21 | - * |
|
| 22 | - * @param bool $routing |
|
| 23 | - */ |
|
| 24 | - public function __construct($routing = true) |
|
| 25 | - { |
|
| 26 | - parent::__construct($routing); |
|
| 27 | - if (! defined('EVENTS_CAF_TEMPLATE_PATH')) { |
|
| 28 | - define('EVENTS_CAF_TEMPLATE_PATH', EE_CORE_CAF_ADMIN_EXTEND . 'events/templates/'); |
|
| 29 | - define('EVENTS_CAF_ASSETS', EE_CORE_CAF_ADMIN_EXTEND . 'events/assets/'); |
|
| 30 | - define('EVENTS_CAF_ASSETS_URL', EE_CORE_CAF_ADMIN_EXTEND_URL . 'events/assets/'); |
|
| 31 | - } |
|
| 32 | - } |
|
| 33 | - |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * Sets routes. |
|
| 37 | - */ |
|
| 38 | - protected function _extend_page_config() |
|
| 39 | - { |
|
| 40 | - $this->_admin_base_path = EE_CORE_CAF_ADMIN_EXTEND . 'events'; |
|
| 41 | - // is there a evt_id in the request? |
|
| 42 | - $evt_id = ! empty($this->_req_data['EVT_ID']) && ! is_array($this->_req_data['EVT_ID']) |
|
| 43 | - ? $this->_req_data['EVT_ID'] |
|
| 44 | - : 0; |
|
| 45 | - $evt_id = ! empty($this->_req_data['post']) ? $this->_req_data['post'] : $evt_id; |
|
| 46 | - // tkt_id? |
|
| 47 | - $tkt_id = ! empty($this->_req_data['TKT_ID']) && ! is_array($this->_req_data['TKT_ID']) |
|
| 48 | - ? $this->_req_data['TKT_ID'] |
|
| 49 | - : 0; |
|
| 50 | - $new_page_routes = array( |
|
| 51 | - 'duplicate_event' => array( |
|
| 52 | - 'func' => '_duplicate_event', |
|
| 53 | - 'capability' => 'ee_edit_event', |
|
| 54 | - 'obj_id' => $evt_id, |
|
| 55 | - 'noheader' => true, |
|
| 56 | - ), |
|
| 57 | - 'ticket_list_table' => array( |
|
| 58 | - 'func' => '_tickets_overview_list_table', |
|
| 59 | - 'capability' => 'ee_read_default_tickets', |
|
| 60 | - ), |
|
| 61 | - 'trash_ticket' => array( |
|
| 62 | - 'func' => '_trash_or_restore_ticket', |
|
| 63 | - 'capability' => 'ee_delete_default_ticket', |
|
| 64 | - 'obj_id' => $tkt_id, |
|
| 65 | - 'noheader' => true, |
|
| 66 | - 'args' => array('trash' => true), |
|
| 67 | - ), |
|
| 68 | - 'trash_tickets' => array( |
|
| 69 | - 'func' => '_trash_or_restore_ticket', |
|
| 70 | - 'capability' => 'ee_delete_default_tickets', |
|
| 71 | - 'noheader' => true, |
|
| 72 | - 'args' => array('trash' => true), |
|
| 73 | - ), |
|
| 74 | - 'restore_ticket' => array( |
|
| 75 | - 'func' => '_trash_or_restore_ticket', |
|
| 76 | - 'capability' => 'ee_delete_default_ticket', |
|
| 77 | - 'obj_id' => $tkt_id, |
|
| 78 | - 'noheader' => true, |
|
| 79 | - ), |
|
| 80 | - 'restore_tickets' => array( |
|
| 81 | - 'func' => '_trash_or_restore_ticket', |
|
| 82 | - 'capability' => 'ee_delete_default_tickets', |
|
| 83 | - 'noheader' => true, |
|
| 84 | - ), |
|
| 85 | - 'delete_ticket' => array( |
|
| 86 | - 'func' => '_delete_ticket', |
|
| 87 | - 'capability' => 'ee_delete_default_ticket', |
|
| 88 | - 'obj_id' => $tkt_id, |
|
| 89 | - 'noheader' => true, |
|
| 90 | - ), |
|
| 91 | - 'delete_tickets' => array( |
|
| 92 | - 'func' => '_delete_ticket', |
|
| 93 | - 'capability' => 'ee_delete_default_tickets', |
|
| 94 | - 'noheader' => true, |
|
| 95 | - ), |
|
| 96 | - 'import_page' => array( |
|
| 97 | - 'func' => '_import_page', |
|
| 98 | - 'capability' => 'import', |
|
| 99 | - ), |
|
| 100 | - 'import' => array( |
|
| 101 | - 'func' => '_import_events', |
|
| 102 | - 'capability' => 'import', |
|
| 103 | - 'noheader' => true, |
|
| 104 | - ), |
|
| 105 | - 'import_events' => array( |
|
| 106 | - 'func' => '_import_events', |
|
| 107 | - 'capability' => 'import', |
|
| 108 | - 'noheader' => true, |
|
| 109 | - ), |
|
| 110 | - 'export_events' => array( |
|
| 111 | - 'func' => '_events_export', |
|
| 112 | - 'capability' => 'export', |
|
| 113 | - 'noheader' => true, |
|
| 114 | - ), |
|
| 115 | - 'export_categories' => array( |
|
| 116 | - 'func' => '_categories_export', |
|
| 117 | - 'capability' => 'export', |
|
| 118 | - 'noheader' => true, |
|
| 119 | - ), |
|
| 120 | - 'sample_export_file' => array( |
|
| 121 | - 'func' => '_sample_export_file', |
|
| 122 | - 'capability' => 'export', |
|
| 123 | - 'noheader' => true, |
|
| 124 | - ), |
|
| 125 | - 'update_template_settings' => array( |
|
| 126 | - 'func' => '_update_template_settings', |
|
| 127 | - 'capability' => 'manage_options', |
|
| 128 | - 'noheader' => true, |
|
| 129 | - ), |
|
| 130 | - ); |
|
| 131 | - $this->_page_routes = array_merge($this->_page_routes, $new_page_routes); |
|
| 132 | - // partial route/config override |
|
| 133 | - $this->_page_config['import_events']['metaboxes'] = $this->_default_espresso_metaboxes; |
|
| 134 | - $this->_page_config['create_new']['metaboxes'][] = '_premium_event_editor_meta_boxes'; |
|
| 135 | - $this->_page_config['create_new']['qtips'][] = 'EE_Event_Editor_Tips'; |
|
| 136 | - $this->_page_config['edit']['qtips'][] = 'EE_Event_Editor_Tips'; |
|
| 137 | - $this->_page_config['edit']['metaboxes'][] = '_premium_event_editor_meta_boxes'; |
|
| 138 | - $this->_page_config['default']['list_table'] = 'Extend_Events_Admin_List_Table'; |
|
| 139 | - // add tickets tab but only if there are more than one default ticket! |
|
| 140 | - $tkt_count = EEM_Ticket::instance()->count_deleted_and_undeleted( |
|
| 141 | - array(array('TKT_is_default' => 1)), |
|
| 142 | - 'TKT_ID', |
|
| 143 | - true |
|
| 144 | - ); |
|
| 145 | - if ($tkt_count > 1) { |
|
| 146 | - $new_page_config = array( |
|
| 147 | - 'ticket_list_table' => array( |
|
| 148 | - 'nav' => array( |
|
| 149 | - 'label' => esc_html__('Default Tickets', 'event_espresso'), |
|
| 150 | - 'order' => 60, |
|
| 151 | - ), |
|
| 152 | - 'list_table' => 'Tickets_List_Table', |
|
| 153 | - 'require_nonce' => false, |
|
| 154 | - ), |
|
| 155 | - ); |
|
| 156 | - } |
|
| 157 | - // template settings |
|
| 158 | - $new_page_config['template_settings'] = array( |
|
| 159 | - 'nav' => array( |
|
| 160 | - 'label' => esc_html__('Templates', 'event_espresso'), |
|
| 161 | - 'order' => 30, |
|
| 162 | - ), |
|
| 163 | - 'metaboxes' => array_merge($this->_default_espresso_metaboxes, array('_publish_post_box')), |
|
| 164 | - 'help_tabs' => array( |
|
| 165 | - 'general_settings_templates_help_tab' => array( |
|
| 166 | - 'title' => esc_html__('Templates', 'event_espresso'), |
|
| 167 | - 'filename' => 'general_settings_templates', |
|
| 168 | - ), |
|
| 169 | - ), |
|
| 170 | - 'help_tour' => array('Templates_Help_Tour'), |
|
| 171 | - 'require_nonce' => false, |
|
| 172 | - ); |
|
| 173 | - $this->_page_config = array_merge($this->_page_config, $new_page_config); |
|
| 174 | - // add filters and actions |
|
| 175 | - // modifying _views |
|
| 176 | - add_filter( |
|
| 177 | - 'FHEE_event_datetime_metabox_add_additional_date_time_template', |
|
| 178 | - array($this, 'add_additional_datetime_button'), |
|
| 179 | - 10, |
|
| 180 | - 2 |
|
| 181 | - ); |
|
| 182 | - add_filter( |
|
| 183 | - 'FHEE_event_datetime_metabox_clone_button_template', |
|
| 184 | - array($this, 'add_datetime_clone_button'), |
|
| 185 | - 10, |
|
| 186 | - 2 |
|
| 187 | - ); |
|
| 188 | - add_filter( |
|
| 189 | - 'FHEE_event_datetime_metabox_timezones_template', |
|
| 190 | - array($this, 'datetime_timezones_template'), |
|
| 191 | - 10, |
|
| 192 | - 2 |
|
| 193 | - ); |
|
| 194 | - // filters for event list table |
|
| 195 | - add_filter('FHEE__Extend_Events_Admin_List_Table__filters', array($this, 'list_table_filters'), 10, 2); |
|
| 196 | - add_filter( |
|
| 197 | - 'FHEE__Events_Admin_List_Table__column_actions__action_links', |
|
| 198 | - array($this, 'extra_list_table_actions'), |
|
| 199 | - 10, |
|
| 200 | - 2 |
|
| 201 | - ); |
|
| 202 | - // legend item |
|
| 203 | - add_filter('FHEE__Events_Admin_Page___event_legend_items__items', array($this, 'additional_legend_items')); |
|
| 204 | - add_action('admin_init', array($this, 'admin_init')); |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - |
|
| 208 | - /** |
|
| 209 | - * admin_init |
|
| 210 | - */ |
|
| 211 | - public function admin_init() |
|
| 212 | - { |
|
| 213 | - EE_Registry::$i18n_js_strings = array_merge( |
|
| 214 | - EE_Registry::$i18n_js_strings, |
|
| 215 | - array( |
|
| 216 | - 'image_confirm' => esc_html__( |
|
| 217 | - 'Do you really want to delete this image? Please remember to update your event to complete the removal.', |
|
| 218 | - 'event_espresso' |
|
| 219 | - ), |
|
| 220 | - 'event_starts_on' => esc_html__('Event Starts on', 'event_espresso'), |
|
| 221 | - 'event_ends_on' => esc_html__('Event Ends on', 'event_espresso'), |
|
| 222 | - 'event_datetime_actions' => esc_html__('Actions', 'event_espresso'), |
|
| 223 | - 'event_clone_dt_msg' => esc_html__('Clone this Event Date and Time', 'event_espresso'), |
|
| 224 | - 'remove_event_dt_msg' => esc_html__('Remove this Event Time', 'event_espresso'), |
|
| 225 | - ) |
|
| 226 | - ); |
|
| 227 | - } |
|
| 228 | - |
|
| 229 | - |
|
| 230 | - /** |
|
| 231 | - * Add per page screen options to the default ticket list table view. |
|
| 232 | - */ |
|
| 233 | - protected function _add_screen_options_ticket_list_table() |
|
| 234 | - { |
|
| 235 | - $this->_per_page_screen_option(); |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - |
|
| 239 | - /** |
|
| 240 | - * @param string $return |
|
| 241 | - * @param int $id |
|
| 242 | - * @param string $new_title |
|
| 243 | - * @param string $new_slug |
|
| 244 | - * @return string |
|
| 245 | - */ |
|
| 246 | - public function extra_permalink_field_buttons($return, $id, $new_title, $new_slug) |
|
| 247 | - { |
|
| 248 | - $return = parent::extra_permalink_field_buttons($return, $id, $new_title, $new_slug); |
|
| 249 | - // make sure this is only when editing |
|
| 250 | - if (! empty($id)) { |
|
| 251 | - $href = EE_Admin_Page::add_query_args_and_nonce( |
|
| 252 | - array('action' => 'duplicate_event', 'EVT_ID' => $id), |
|
| 253 | - $this->_admin_base_url |
|
| 254 | - ); |
|
| 255 | - $title = esc_attr__('Duplicate Event', 'event_espresso'); |
|
| 256 | - $return .= '<a href="' |
|
| 257 | - . $href |
|
| 258 | - . '" title="' |
|
| 259 | - . $title |
|
| 260 | - . '" id="ee-duplicate-event-button" class="button button-small" value="duplicate_event">' |
|
| 261 | - . $title |
|
| 262 | - . '</a>'; |
|
| 263 | - } |
|
| 264 | - return $return; |
|
| 265 | - } |
|
| 266 | - |
|
| 267 | - |
|
| 268 | - /** |
|
| 269 | - * Set the list table views for the default ticket list table view. |
|
| 270 | - */ |
|
| 271 | - public function _set_list_table_views_ticket_list_table() |
|
| 272 | - { |
|
| 273 | - $this->_views = array( |
|
| 274 | - 'all' => array( |
|
| 275 | - 'slug' => 'all', |
|
| 276 | - 'label' => esc_html__('All', 'event_espresso'), |
|
| 277 | - 'count' => 0, |
|
| 278 | - 'bulk_action' => array( |
|
| 279 | - 'trash_tickets' => esc_html__('Move to Trash', 'event_espresso'), |
|
| 280 | - ), |
|
| 281 | - ), |
|
| 282 | - 'trashed' => array( |
|
| 283 | - 'slug' => 'trashed', |
|
| 284 | - 'label' => esc_html__('Trash', 'event_espresso'), |
|
| 285 | - 'count' => 0, |
|
| 286 | - 'bulk_action' => array( |
|
| 287 | - 'restore_tickets' => esc_html__('Restore from Trash', 'event_espresso'), |
|
| 288 | - 'delete_tickets' => esc_html__('Delete Permanently', 'event_espresso'), |
|
| 289 | - ), |
|
| 290 | - ), |
|
| 291 | - ); |
|
| 292 | - } |
|
| 293 | - |
|
| 294 | - |
|
| 295 | - /** |
|
| 296 | - * Enqueue scripts and styles for the event editor. |
|
| 297 | - */ |
|
| 298 | - public function load_scripts_styles_edit() |
|
| 299 | - { |
|
| 300 | - wp_register_script( |
|
| 301 | - 'ee-event-editor-heartbeat', |
|
| 302 | - EVENTS_CAF_ASSETS_URL . 'event-editor-heartbeat.js', |
|
| 303 | - array('ee_admin_js', 'heartbeat'), |
|
| 304 | - EVENT_ESPRESSO_VERSION, |
|
| 305 | - true |
|
| 306 | - ); |
|
| 307 | - wp_enqueue_script('ee-accounting'); |
|
| 308 | - // styles |
|
| 309 | - wp_enqueue_style('espresso-ui-theme'); |
|
| 310 | - wp_enqueue_script('event_editor_js'); |
|
| 311 | - wp_enqueue_script('ee-event-editor-heartbeat'); |
|
| 312 | - } |
|
| 313 | - |
|
| 314 | - |
|
| 315 | - /** |
|
| 316 | - * Returns template for the additional datetime. |
|
| 317 | - * |
|
| 318 | - * @param $template |
|
| 319 | - * @param $template_args |
|
| 320 | - * @return mixed |
|
| 321 | - * @throws DomainException |
|
| 322 | - */ |
|
| 323 | - public function add_additional_datetime_button($template, $template_args) |
|
| 324 | - { |
|
| 325 | - return EEH_Template::display_template( |
|
| 326 | - EVENTS_CAF_TEMPLATE_PATH . 'event_datetime_add_additional_time.template.php', |
|
| 327 | - $template_args, |
|
| 328 | - true |
|
| 329 | - ); |
|
| 330 | - } |
|
| 331 | - |
|
| 332 | - |
|
| 333 | - /** |
|
| 334 | - * Returns the template for cloning a datetime. |
|
| 335 | - * |
|
| 336 | - * @param $template |
|
| 337 | - * @param $template_args |
|
| 338 | - * @return mixed |
|
| 339 | - * @throws DomainException |
|
| 340 | - */ |
|
| 341 | - public function add_datetime_clone_button($template, $template_args) |
|
| 342 | - { |
|
| 343 | - return EEH_Template::display_template( |
|
| 344 | - EVENTS_CAF_TEMPLATE_PATH . 'event_datetime_metabox_clone_button.template.php', |
|
| 345 | - $template_args, |
|
| 346 | - true |
|
| 347 | - ); |
|
| 348 | - } |
|
| 349 | - |
|
| 350 | - |
|
| 351 | - /** |
|
| 352 | - * Returns the template for datetime timezones. |
|
| 353 | - * |
|
| 354 | - * @param $template |
|
| 355 | - * @param $template_args |
|
| 356 | - * @return mixed |
|
| 357 | - * @throws DomainException |
|
| 358 | - */ |
|
| 359 | - public function datetime_timezones_template($template, $template_args) |
|
| 360 | - { |
|
| 361 | - return EEH_Template::display_template( |
|
| 362 | - EVENTS_CAF_TEMPLATE_PATH . 'event_datetime_timezones.template.php', |
|
| 363 | - $template_args, |
|
| 364 | - true |
|
| 365 | - ); |
|
| 366 | - } |
|
| 367 | - |
|
| 368 | - |
|
| 369 | - /** |
|
| 370 | - * Sets the views for the default list table view. |
|
| 371 | - */ |
|
| 372 | - protected function _set_list_table_views_default() |
|
| 373 | - { |
|
| 374 | - parent::_set_list_table_views_default(); |
|
| 375 | - $new_views = array( |
|
| 376 | - 'today' => array( |
|
| 377 | - 'slug' => 'today', |
|
| 378 | - 'label' => esc_html__('Today', 'event_espresso'), |
|
| 379 | - 'count' => $this->total_events_today(), |
|
| 380 | - 'bulk_action' => array( |
|
| 381 | - 'trash_events' => esc_html__('Move to Trash', 'event_espresso'), |
|
| 382 | - ), |
|
| 383 | - ), |
|
| 384 | - 'month' => array( |
|
| 385 | - 'slug' => 'month', |
|
| 386 | - 'label' => esc_html__('This Month', 'event_espresso'), |
|
| 387 | - 'count' => $this->total_events_this_month(), |
|
| 388 | - 'bulk_action' => array( |
|
| 389 | - 'trash_events' => esc_html__('Move to Trash', 'event_espresso'), |
|
| 390 | - ), |
|
| 391 | - ), |
|
| 392 | - ); |
|
| 393 | - $this->_views = array_merge($this->_views, $new_views); |
|
| 394 | - } |
|
| 395 | - |
|
| 396 | - |
|
| 397 | - /** |
|
| 398 | - * Returns the extra action links for the default list table view. |
|
| 399 | - * |
|
| 400 | - * @param array $action_links |
|
| 401 | - * @param \EE_Event $event |
|
| 402 | - * @return array |
|
| 403 | - * @throws EE_Error |
|
| 404 | - */ |
|
| 405 | - public function extra_list_table_actions(array $action_links, \EE_Event $event) |
|
| 406 | - { |
|
| 407 | - if (EE_Registry::instance()->CAP->current_user_can( |
|
| 408 | - 'ee_read_registrations', |
|
| 409 | - 'espresso_registrations_reports', |
|
| 410 | - $event->ID() |
|
| 411 | - ) |
|
| 412 | - ) { |
|
| 413 | - $reports_query_args = array( |
|
| 414 | - 'action' => 'reports', |
|
| 415 | - 'EVT_ID' => $event->ID(), |
|
| 416 | - ); |
|
| 417 | - $reports_link = EE_Admin_Page::add_query_args_and_nonce($reports_query_args, REG_ADMIN_URL); |
|
| 418 | - $action_links[] = '<a href="' |
|
| 419 | - . $reports_link |
|
| 420 | - . '" title="' |
|
| 421 | - . esc_attr__('View Report', 'event_espresso') |
|
| 422 | - . '"><div class="dashicons dashicons-chart-bar"></div></a>' |
|
| 423 | - . "\n\t"; |
|
| 424 | - } |
|
| 425 | - if (EE_Registry::instance()->CAP->current_user_can('ee_read_global_messages', 'view_filtered_messages')) { |
|
| 426 | - EE_Registry::instance()->load_helper('MSG_Template'); |
|
| 427 | - $action_links[] = EEH_MSG_Template::get_message_action_link( |
|
| 428 | - 'see_notifications_for', |
|
| 429 | - null, |
|
| 430 | - array('EVT_ID' => $event->ID()) |
|
| 431 | - ); |
|
| 432 | - } |
|
| 433 | - return $action_links; |
|
| 434 | - } |
|
| 435 | - |
|
| 436 | - |
|
| 437 | - /** |
|
| 438 | - * @param $items |
|
| 439 | - * @return mixed |
|
| 440 | - */ |
|
| 441 | - public function additional_legend_items($items) |
|
| 442 | - { |
|
| 443 | - if (EE_Registry::instance()->CAP->current_user_can( |
|
| 444 | - 'ee_read_registrations', |
|
| 445 | - 'espresso_registrations_reports' |
|
| 446 | - ) |
|
| 447 | - ) { |
|
| 448 | - $items['reports'] = array( |
|
| 449 | - 'class' => 'dashicons dashicons-chart-bar', |
|
| 450 | - 'desc' => esc_html__('Event Reports', 'event_espresso'), |
|
| 451 | - ); |
|
| 452 | - } |
|
| 453 | - if (EE_Registry::instance()->CAP->current_user_can('ee_read_global_messages', 'view_filtered_messages')) { |
|
| 454 | - $related_for_icon = EEH_MSG_Template::get_message_action_icon('see_notifications_for'); |
|
| 455 | - if (isset($related_for_icon['css_class']) && isset($related_for_icon['label'])) { |
|
| 456 | - $items['view_related_messages'] = array( |
|
| 457 | - 'class' => $related_for_icon['css_class'], |
|
| 458 | - 'desc' => $related_for_icon['label'], |
|
| 459 | - ); |
|
| 460 | - } |
|
| 461 | - } |
|
| 462 | - return $items; |
|
| 463 | - } |
|
| 464 | - |
|
| 465 | - |
|
| 466 | - /** |
|
| 467 | - * This is the callback method for the duplicate event route |
|
| 468 | - * Method looks for 'EVT_ID' in the request and retrieves that event and its details and duplicates them |
|
| 469 | - * into a new event. We add a hook so that any plugins that add extra event details can hook into this |
|
| 470 | - * action. Note that the dupe will have **DUPLICATE** as its title and slug. |
|
| 471 | - * After duplication the redirect is to the new event edit page. |
|
| 472 | - * |
|
| 473 | - * @return void |
|
| 474 | - * @access protected |
|
| 475 | - * @throws EE_Error If EE_Event is not available with given ID |
|
| 476 | - */ |
|
| 477 | - protected function _duplicate_event() |
|
| 478 | - { |
|
| 479 | - // first make sure the ID for the event is in the request. |
|
| 480 | - // If it isn't then we need to bail and redirect back to overview list table (cause how did we get here?) |
|
| 481 | - if (! isset($this->_req_data['EVT_ID'])) { |
|
| 482 | - EE_Error::add_error( |
|
| 483 | - esc_html__( |
|
| 484 | - 'In order to duplicate an event an Event ID is required. None was given.', |
|
| 485 | - 'event_espresso' |
|
| 486 | - ), |
|
| 487 | - __FILE__, |
|
| 488 | - __FUNCTION__, |
|
| 489 | - __LINE__ |
|
| 490 | - ); |
|
| 491 | - $this->_redirect_after_action(false, '', '', array(), true); |
|
| 492 | - return; |
|
| 493 | - } |
|
| 494 | - // k we've got EVT_ID so let's use that to get the event we'll duplicate |
|
| 495 | - $orig_event = EEM_Event::instance()->get_one_by_ID($this->_req_data['EVT_ID']); |
|
| 496 | - if (! $orig_event instanceof EE_Event) { |
|
| 497 | - throw new EE_Error( |
|
| 498 | - sprintf( |
|
| 499 | - esc_html__('An EE_Event object could not be retrieved for the given ID (%s)', 'event_espresso'), |
|
| 500 | - $this->_req_data['EVT_ID'] |
|
| 501 | - ) |
|
| 502 | - ); |
|
| 503 | - } |
|
| 504 | - // k now let's clone the $orig_event before getting relations |
|
| 505 | - $new_event = clone $orig_event; |
|
| 506 | - // original datetimes |
|
| 507 | - $orig_datetimes = $orig_event->get_many_related('Datetime'); |
|
| 508 | - // other original relations |
|
| 509 | - $orig_ven = $orig_event->get_many_related('Venue'); |
|
| 510 | - // reset the ID and modify other details to make it clear this is a dupe |
|
| 511 | - $new_event->set('EVT_ID', 0); |
|
| 512 | - $new_name = $new_event->name() . ' ' . esc_html__('**DUPLICATE**', 'event_espresso'); |
|
| 513 | - $new_event->set('EVT_name', $new_name); |
|
| 514 | - $new_event->set( |
|
| 515 | - 'EVT_slug', |
|
| 516 | - wp_unique_post_slug( |
|
| 517 | - sanitize_title($orig_event->name()), |
|
| 518 | - 0, |
|
| 519 | - 'publish', |
|
| 520 | - 'espresso_events', |
|
| 521 | - 0 |
|
| 522 | - ) |
|
| 523 | - ); |
|
| 524 | - $new_event->set('status', 'draft'); |
|
| 525 | - // duplicate discussion settings |
|
| 526 | - $new_event->set('comment_status', $orig_event->get('comment_status')); |
|
| 527 | - $new_event->set('ping_status', $orig_event->get('ping_status')); |
|
| 528 | - // save the new event |
|
| 529 | - $new_event->save(); |
|
| 530 | - // venues |
|
| 531 | - foreach ($orig_ven as $ven) { |
|
| 532 | - $new_event->_add_relation_to($ven, 'Venue'); |
|
| 533 | - } |
|
| 534 | - $new_event->save(); |
|
| 535 | - // now we need to get the question group relations and handle that |
|
| 536 | - // first primary question groups |
|
| 537 | - $orig_primary_qgs = $orig_event->get_many_related( |
|
| 538 | - 'Question_Group', |
|
| 539 | - array(array('Event_Question_Group.EQG_primary' => 1)) |
|
| 540 | - ); |
|
| 541 | - if (! empty($orig_primary_qgs)) { |
|
| 542 | - foreach ($orig_primary_qgs as $id => $obj) { |
|
| 543 | - if ($obj instanceof EE_Question_Group) { |
|
| 544 | - $new_event->_add_relation_to($obj, 'Question_Group', array('EQG_primary' => 1)); |
|
| 545 | - } |
|
| 546 | - } |
|
| 547 | - } |
|
| 548 | - // next additional attendee question groups |
|
| 549 | - $orig_additional_qgs = $orig_event->get_many_related( |
|
| 550 | - 'Question_Group', |
|
| 551 | - array(array('Event_Question_Group.EQG_primary' => 0)) |
|
| 552 | - ); |
|
| 553 | - if (! empty($orig_additional_qgs)) { |
|
| 554 | - foreach ($orig_additional_qgs as $id => $obj) { |
|
| 555 | - if ($obj instanceof EE_Question_Group) { |
|
| 556 | - $new_event->_add_relation_to($obj, 'Question_Group', array('EQG_primary' => 0)); |
|
| 557 | - } |
|
| 558 | - } |
|
| 559 | - } |
|
| 560 | - |
|
| 561 | - $new_event->save(); |
|
| 562 | - |
|
| 563 | - // k now that we have the new event saved we can loop through the datetimes and start adding relations. |
|
| 564 | - $cloned_tickets = array(); |
|
| 565 | - foreach ($orig_datetimes as $orig_dtt) { |
|
| 566 | - if (! $orig_dtt instanceof EE_Datetime) { |
|
| 567 | - continue; |
|
| 568 | - } |
|
| 569 | - $new_dtt = clone $orig_dtt; |
|
| 570 | - $orig_tkts = $orig_dtt->tickets(); |
|
| 571 | - // save new dtt then add to event |
|
| 572 | - $new_dtt->set('DTT_ID', 0); |
|
| 573 | - $new_dtt->set('DTT_sold', 0); |
|
| 574 | - $new_dtt->set_reserved(0); |
|
| 575 | - $new_dtt->save(); |
|
| 576 | - $new_event->_add_relation_to($new_dtt, 'Datetime'); |
|
| 577 | - $new_event->save(); |
|
| 578 | - // now let's get the ticket relations setup. |
|
| 579 | - foreach ((array) $orig_tkts as $orig_tkt) { |
|
| 580 | - // it's possible a datetime will have no tickets so let's verify we HAVE a ticket first. |
|
| 581 | - if (! $orig_tkt instanceof EE_Ticket) { |
|
| 582 | - continue; |
|
| 583 | - } |
|
| 584 | - // is this ticket archived? If it is then let's skip |
|
| 585 | - if ($orig_tkt->get('TKT_deleted')) { |
|
| 586 | - continue; |
|
| 587 | - } |
|
| 588 | - // does this original ticket already exist in the clone_tickets cache? |
|
| 589 | - // If so we'll just use the new ticket from it. |
|
| 590 | - if (isset($cloned_tickets[ $orig_tkt->ID() ])) { |
|
| 591 | - $new_tkt = $cloned_tickets[ $orig_tkt->ID() ]; |
|
| 592 | - } else { |
|
| 593 | - $new_tkt = clone $orig_tkt; |
|
| 594 | - // get relations on the $orig_tkt that we need to setup. |
|
| 595 | - $orig_prices = $orig_tkt->prices(); |
|
| 596 | - $new_tkt->set('TKT_ID', 0); |
|
| 597 | - $new_tkt->set('TKT_sold', 0); |
|
| 598 | - $new_tkt->set('TKT_reserved', 0); |
|
| 599 | - $new_tkt->save(); // make sure new ticket has ID. |
|
| 600 | - // price relations on new ticket need to be setup. |
|
| 601 | - foreach ($orig_prices as $orig_price) { |
|
| 602 | - $new_price = clone $orig_price; |
|
| 603 | - $new_price->set('PRC_ID', 0); |
|
| 604 | - $new_price->save(); |
|
| 605 | - $new_tkt->_add_relation_to($new_price, 'Price'); |
|
| 606 | - $new_tkt->save(); |
|
| 607 | - } |
|
| 608 | - |
|
| 609 | - do_action( |
|
| 610 | - 'AHEE__Extend_Events_Admin_Page___duplicate_event__duplicate_ticket__after', |
|
| 611 | - $orig_tkt, |
|
| 612 | - $new_tkt, |
|
| 613 | - $orig_prices, |
|
| 614 | - $orig_event, |
|
| 615 | - $orig_dtt, |
|
| 616 | - $new_dtt |
|
| 617 | - ); |
|
| 618 | - } |
|
| 619 | - // k now we can add the new ticket as a relation to the new datetime |
|
| 620 | - // and make sure its added to our cached $cloned_tickets array |
|
| 621 | - // for use with later datetimes that have the same ticket. |
|
| 622 | - $new_dtt->_add_relation_to($new_tkt, 'Ticket'); |
|
| 623 | - $new_dtt->save(); |
|
| 624 | - $cloned_tickets[ $orig_tkt->ID() ] = $new_tkt; |
|
| 625 | - } |
|
| 626 | - } |
|
| 627 | - // clone taxonomy information |
|
| 628 | - $taxonomies_to_clone_with = apply_filters( |
|
| 629 | - 'FHEE__Extend_Events_Admin_Page___duplicate_event__taxonomies_to_clone', |
|
| 630 | - array('espresso_event_categories', 'espresso_event_type', 'post_tag') |
|
| 631 | - ); |
|
| 632 | - // get terms for original event (notice) |
|
| 633 | - $orig_terms = wp_get_object_terms($orig_event->ID(), $taxonomies_to_clone_with); |
|
| 634 | - // loop through terms and add them to new event. |
|
| 635 | - foreach ($orig_terms as $term) { |
|
| 636 | - wp_set_object_terms($new_event->ID(), $term->term_id, $term->taxonomy, true); |
|
| 637 | - } |
|
| 638 | - |
|
| 639 | - // duplicate other core WP_Post items for this event. |
|
| 640 | - // post thumbnail (feature image). |
|
| 641 | - $feature_image_id = get_post_thumbnail_id($orig_event->ID()); |
|
| 642 | - if ($feature_image_id) { |
|
| 643 | - update_post_meta($new_event->ID(), '_thumbnail_id', $feature_image_id); |
|
| 644 | - } |
|
| 645 | - |
|
| 646 | - // duplicate page_template setting |
|
| 647 | - $page_template = get_post_meta($orig_event->ID(), '_wp_page_template', true); |
|
| 648 | - if ($page_template) { |
|
| 649 | - update_post_meta($new_event->ID(), '_wp_page_template', $page_template); |
|
| 650 | - } |
|
| 651 | - |
|
| 652 | - do_action('AHEE__Extend_Events_Admin_Page___duplicate_event__after', $new_event, $orig_event); |
|
| 653 | - // now let's redirect to the edit page for this duplicated event if we have a new event id. |
|
| 654 | - if ($new_event->ID()) { |
|
| 655 | - $redirect_args = array( |
|
| 656 | - 'post' => $new_event->ID(), |
|
| 657 | - 'action' => 'edit', |
|
| 658 | - ); |
|
| 659 | - EE_Error::add_success( |
|
| 660 | - esc_html__( |
|
| 661 | - 'Event successfully duplicated. Please review the details below and make any necessary edits', |
|
| 662 | - 'event_espresso' |
|
| 663 | - ) |
|
| 664 | - ); |
|
| 665 | - } else { |
|
| 666 | - $redirect_args = array( |
|
| 667 | - 'action' => 'default', |
|
| 668 | - ); |
|
| 669 | - EE_Error::add_error( |
|
| 670 | - esc_html__('Not able to duplicate event. Something went wrong.', 'event_espresso'), |
|
| 671 | - __FILE__, |
|
| 672 | - __FUNCTION__, |
|
| 673 | - __LINE__ |
|
| 674 | - ); |
|
| 675 | - } |
|
| 676 | - $this->_redirect_after_action(false, '', '', $redirect_args, true); |
|
| 677 | - } |
|
| 678 | - |
|
| 679 | - |
|
| 680 | - /** |
|
| 681 | - * Generates output for the import page. |
|
| 682 | - * |
|
| 683 | - * @throws DomainException |
|
| 684 | - */ |
|
| 685 | - protected function _import_page() |
|
| 686 | - { |
|
| 687 | - $title = esc_html__('Import', 'event_espresso'); |
|
| 688 | - $intro = esc_html__( |
|
| 689 | - 'If you have a previously exported Event Espresso 4 information in a Comma Separated Value (CSV) file format, you can upload the file here: ', |
|
| 690 | - 'event_espresso' |
|
| 691 | - ); |
|
| 692 | - $form_url = EVENTS_ADMIN_URL; |
|
| 693 | - $action = 'import_events'; |
|
| 694 | - $type = 'csv'; |
|
| 695 | - $this->_template_args['form'] = EE_Import::instance()->upload_form( |
|
| 696 | - $title, |
|
| 697 | - $intro, |
|
| 698 | - $form_url, |
|
| 699 | - $action, |
|
| 700 | - $type |
|
| 701 | - ); |
|
| 702 | - $this->_template_args['sample_file_link'] = EE_Admin_Page::add_query_args_and_nonce( |
|
| 703 | - array('action' => 'sample_export_file'), |
|
| 704 | - $this->_admin_base_url |
|
| 705 | - ); |
|
| 706 | - $content = EEH_Template::display_template( |
|
| 707 | - EVENTS_CAF_TEMPLATE_PATH . 'import_page.template.php', |
|
| 708 | - $this->_template_args, |
|
| 709 | - true |
|
| 710 | - ); |
|
| 711 | - $this->_template_args['admin_page_content'] = $content; |
|
| 712 | - $this->display_admin_page_with_sidebar(); |
|
| 713 | - } |
|
| 714 | - |
|
| 715 | - |
|
| 716 | - /** |
|
| 717 | - * _import_events |
|
| 718 | - * This handles displaying the screen and running imports for importing events. |
|
| 719 | - * |
|
| 720 | - * @return void |
|
| 721 | - */ |
|
| 722 | - protected function _import_events() |
|
| 723 | - { |
|
| 724 | - require_once(EE_CLASSES . 'EE_Import.class.php'); |
|
| 725 | - $success = EE_Import::instance()->import(); |
|
| 726 | - $this->_redirect_after_action($success, 'Import File', 'ran', array('action' => 'import_page'), true); |
|
| 727 | - } |
|
| 728 | - |
|
| 729 | - |
|
| 730 | - /** |
|
| 731 | - * _events_export |
|
| 732 | - * Will export all (or just the given event) to a Excel compatible file. |
|
| 733 | - * |
|
| 734 | - * @access protected |
|
| 735 | - * @return void |
|
| 736 | - */ |
|
| 737 | - protected function _events_export() |
|
| 738 | - { |
|
| 739 | - if (isset($this->_req_data['EVT_ID'])) { |
|
| 740 | - $event_ids = $this->_req_data['EVT_ID']; |
|
| 741 | - } elseif (isset($this->_req_data['EVT_IDs'])) { |
|
| 742 | - $event_ids = $this->_req_data['EVT_IDs']; |
|
| 743 | - } else { |
|
| 744 | - $event_ids = null; |
|
| 745 | - } |
|
| 746 | - // todo: I don't like doing this but it'll do until we modify EE_Export Class. |
|
| 747 | - $new_request_args = array( |
|
| 748 | - 'export' => 'report', |
|
| 749 | - 'action' => 'all_event_data', |
|
| 750 | - 'EVT_ID' => $event_ids, |
|
| 751 | - ); |
|
| 752 | - $this->_req_data = array_merge($this->_req_data, $new_request_args); |
|
| 753 | - if (is_readable(EE_CLASSES . 'EE_Export.class.php')) { |
|
| 754 | - require_once(EE_CLASSES . 'EE_Export.class.php'); |
|
| 755 | - $EE_Export = EE_Export::instance($this->_req_data); |
|
| 756 | - $EE_Export->export(); |
|
| 757 | - } |
|
| 758 | - } |
|
| 759 | - |
|
| 760 | - |
|
| 761 | - /** |
|
| 762 | - * handle category exports() |
|
| 763 | - * |
|
| 764 | - * @return void |
|
| 765 | - */ |
|
| 766 | - protected function _categories_export() |
|
| 767 | - { |
|
| 768 | - // todo: I don't like doing this but it'll do until we modify EE_Export Class. |
|
| 769 | - $new_request_args = array( |
|
| 770 | - 'export' => 'report', |
|
| 771 | - 'action' => 'categories', |
|
| 772 | - 'category_ids' => $this->_req_data['EVT_CAT_ID'], |
|
| 773 | - ); |
|
| 774 | - $this->_req_data = array_merge($this->_req_data, $new_request_args); |
|
| 775 | - if (is_readable(EE_CLASSES . 'EE_Export.class.php')) { |
|
| 776 | - require_once(EE_CLASSES . 'EE_Export.class.php'); |
|
| 777 | - $EE_Export = EE_Export::instance($this->_req_data); |
|
| 778 | - $EE_Export->export(); |
|
| 779 | - } |
|
| 780 | - } |
|
| 781 | - |
|
| 782 | - |
|
| 783 | - /** |
|
| 784 | - * Creates a sample CSV file for importing |
|
| 785 | - */ |
|
| 786 | - protected function _sample_export_file() |
|
| 787 | - { |
|
| 788 | - // require_once(EE_CLASSES . 'EE_Export.class.php'); |
|
| 789 | - EE_Export::instance()->export_sample(); |
|
| 790 | - } |
|
| 791 | - |
|
| 792 | - |
|
| 793 | - /************* Template Settings *************/ |
|
| 794 | - /** |
|
| 795 | - * Generates template settings page output |
|
| 796 | - * |
|
| 797 | - * @throws DomainException |
|
| 798 | - * @throws EE_Error |
|
| 799 | - */ |
|
| 800 | - protected function _template_settings() |
|
| 801 | - { |
|
| 802 | - $this->_template_args['values'] = $this->_yes_no_values; |
|
| 803 | - /** |
|
| 804 | - * Note leaving this filter in for backward compatibility this was moved in 4.6.x |
|
| 805 | - * from General_Settings_Admin_Page to here. |
|
| 806 | - */ |
|
| 807 | - $this->_template_args = apply_filters( |
|
| 808 | - 'FHEE__General_Settings_Admin_Page__template_settings__template_args', |
|
| 809 | - $this->_template_args |
|
| 810 | - ); |
|
| 811 | - $this->_set_add_edit_form_tags('update_template_settings'); |
|
| 812 | - $this->_set_publish_post_box_vars(null, false, false, null, false); |
|
| 813 | - $this->_template_args['admin_page_content'] = EEH_Template::display_template( |
|
| 814 | - EVENTS_CAF_TEMPLATE_PATH . 'template_settings.template.php', |
|
| 815 | - $this->_template_args, |
|
| 816 | - true |
|
| 817 | - ); |
|
| 818 | - $this->display_admin_page_with_sidebar(); |
|
| 819 | - } |
|
| 820 | - |
|
| 821 | - |
|
| 822 | - /** |
|
| 823 | - * Handler for updating template settings. |
|
| 824 | - * |
|
| 825 | - * @throws InvalidInterfaceException |
|
| 826 | - * @throws InvalidDataTypeException |
|
| 827 | - * @throws InvalidArgumentException |
|
| 828 | - */ |
|
| 829 | - protected function _update_template_settings() |
|
| 830 | - { |
|
| 831 | - /** |
|
| 832 | - * Note leaving this filter in for backward compatibility this was moved in 4.6.x |
|
| 833 | - * from General_Settings_Admin_Page to here. |
|
| 834 | - */ |
|
| 835 | - EE_Registry::instance()->CFG->template_settings = apply_filters( |
|
| 836 | - 'FHEE__General_Settings_Admin_Page__update_template_settings__data', |
|
| 837 | - EE_Registry::instance()->CFG->template_settings, |
|
| 838 | - $this->_req_data |
|
| 839 | - ); |
|
| 840 | - // update custom post type slugs and detect if we need to flush rewrite rules |
|
| 841 | - $old_slug = EE_Registry::instance()->CFG->core->event_cpt_slug; |
|
| 842 | - EE_Registry::instance()->CFG->core->event_cpt_slug = empty($this->_req_data['event_cpt_slug']) |
|
| 843 | - ? EE_Registry::instance()->CFG->core->event_cpt_slug |
|
| 844 | - : EEH_URL::slugify($this->_req_data['event_cpt_slug'], 'events'); |
|
| 845 | - $what = 'Template Settings'; |
|
| 846 | - $success = $this->_update_espresso_configuration( |
|
| 847 | - $what, |
|
| 848 | - EE_Registry::instance()->CFG->template_settings, |
|
| 849 | - __FILE__, |
|
| 850 | - __FUNCTION__, |
|
| 851 | - __LINE__ |
|
| 852 | - ); |
|
| 853 | - if (EE_Registry::instance()->CFG->core->event_cpt_slug != $old_slug) { |
|
| 854 | - /** @var EventEspresso\core\domain\services\custom_post_types\RewriteRules $rewrite_rules */ |
|
| 855 | - $rewrite_rules = LoaderFactory::getLoader()->getShared( |
|
| 856 | - 'EventEspresso\core\domain\services\custom_post_types\RewriteRules' |
|
| 857 | - ); |
|
| 858 | - $rewrite_rules->flush(); |
|
| 859 | - } |
|
| 860 | - $this->_redirect_after_action($success, $what, 'updated', array('action' => 'template_settings')); |
|
| 861 | - } |
|
| 862 | - |
|
| 863 | - |
|
| 864 | - /** |
|
| 865 | - * _premium_event_editor_meta_boxes |
|
| 866 | - * add all metaboxes related to the event_editor |
|
| 867 | - * |
|
| 868 | - * @access protected |
|
| 869 | - * @return void |
|
| 870 | - * @throws EE_Error |
|
| 871 | - */ |
|
| 872 | - protected function _premium_event_editor_meta_boxes() |
|
| 873 | - { |
|
| 874 | - $this->verify_cpt_object(); |
|
| 875 | - add_meta_box( |
|
| 876 | - 'espresso_event_editor_event_options', |
|
| 877 | - esc_html__('Event Registration Options', 'event_espresso'), |
|
| 878 | - array($this, 'registration_options_meta_box'), |
|
| 879 | - $this->page_slug, |
|
| 880 | - 'side', |
|
| 881 | - 'core' |
|
| 882 | - ); |
|
| 883 | - } |
|
| 884 | - |
|
| 885 | - |
|
| 886 | - /** |
|
| 887 | - * override caf metabox |
|
| 888 | - * |
|
| 889 | - * @return void |
|
| 890 | - * @throws DomainException |
|
| 891 | - */ |
|
| 892 | - public function registration_options_meta_box() |
|
| 893 | - { |
|
| 894 | - $yes_no_values = array( |
|
| 895 | - array('id' => true, 'text' => esc_html__('Yes', 'event_espresso')), |
|
| 896 | - array('id' => false, 'text' => esc_html__('No', 'event_espresso')), |
|
| 897 | - ); |
|
| 898 | - $default_reg_status_values = EEM_Registration::reg_status_array( |
|
| 899 | - array( |
|
| 900 | - EEM_Registration::status_id_cancelled, |
|
| 901 | - EEM_Registration::status_id_declined, |
|
| 902 | - EEM_Registration::status_id_incomplete, |
|
| 903 | - EEM_Registration::status_id_wait_list, |
|
| 904 | - ), |
|
| 905 | - true |
|
| 906 | - ); |
|
| 907 | - $template_args['active_status'] = $this->_cpt_model_obj->pretty_active_status(false); |
|
| 908 | - $template_args['_event'] = $this->_cpt_model_obj; |
|
| 909 | - $template_args['additional_limit'] = $this->_cpt_model_obj->additional_limit(); |
|
| 910 | - $template_args['default_registration_status'] = EEH_Form_Fields::select_input( |
|
| 911 | - 'default_reg_status', |
|
| 912 | - $default_reg_status_values, |
|
| 913 | - $this->_cpt_model_obj->default_registration_status() |
|
| 914 | - ); |
|
| 915 | - $template_args['display_description'] = EEH_Form_Fields::select_input( |
|
| 916 | - 'display_desc', |
|
| 917 | - $yes_no_values, |
|
| 918 | - $this->_cpt_model_obj->display_description() |
|
| 919 | - ); |
|
| 920 | - $template_args['display_ticket_selector'] = EEH_Form_Fields::select_input( |
|
| 921 | - 'display_ticket_selector', |
|
| 922 | - $yes_no_values, |
|
| 923 | - $this->_cpt_model_obj->display_ticket_selector(), |
|
| 924 | - '', |
|
| 925 | - '', |
|
| 926 | - false |
|
| 927 | - ); |
|
| 928 | - $template_args['EVT_default_registration_status'] = EEH_Form_Fields::select_input( |
|
| 929 | - 'EVT_default_registration_status', |
|
| 930 | - $default_reg_status_values, |
|
| 931 | - $this->_cpt_model_obj->default_registration_status() |
|
| 932 | - ); |
|
| 933 | - $template_args['additional_registration_options'] = apply_filters( |
|
| 934 | - 'FHEE__Events_Admin_Page__registration_options_meta_box__additional_registration_options', |
|
| 935 | - '', |
|
| 936 | - $template_args, |
|
| 937 | - $yes_no_values, |
|
| 938 | - $default_reg_status_values |
|
| 939 | - ); |
|
| 940 | - EEH_Template::display_template( |
|
| 941 | - EVENTS_CAF_TEMPLATE_PATH . 'event_registration_options.template.php', |
|
| 942 | - $template_args |
|
| 943 | - ); |
|
| 944 | - } |
|
| 945 | - |
|
| 946 | - |
|
| 947 | - |
|
| 948 | - /** |
|
| 949 | - * wp_list_table_mods for caf |
|
| 950 | - * ============================ |
|
| 951 | - */ |
|
| 952 | - /** |
|
| 953 | - * hook into list table filters and provide filters for caffeinated list table |
|
| 954 | - * |
|
| 955 | - * @param array $old_filters any existing filters present |
|
| 956 | - * @param array $list_table_obj the list table object |
|
| 957 | - * @return array new filters |
|
| 958 | - */ |
|
| 959 | - public function list_table_filters($old_filters, $list_table_obj) |
|
| 960 | - { |
|
| 961 | - $filters = array(); |
|
| 962 | - // first month/year filters |
|
| 963 | - $filters[] = $this->espresso_event_months_dropdown(); |
|
| 964 | - $status = isset($this->_req_data['status']) ? $this->_req_data['status'] : null; |
|
| 965 | - // active status dropdown |
|
| 966 | - if ($status !== 'draft') { |
|
| 967 | - $filters[] = $this->active_status_dropdown( |
|
| 968 | - isset($this->_req_data['active_status']) ? $this->_req_data['active_status'] : '' |
|
| 969 | - ); |
|
| 970 | - } |
|
| 971 | - // category filter |
|
| 972 | - $filters[] = $this->category_dropdown(); |
|
| 973 | - return array_merge($old_filters, $filters); |
|
| 974 | - } |
|
| 975 | - |
|
| 976 | - |
|
| 977 | - /** |
|
| 978 | - * espresso_event_months_dropdown |
|
| 979 | - * |
|
| 980 | - * @access public |
|
| 981 | - * @return string dropdown listing month/year selections for events. |
|
| 982 | - */ |
|
| 983 | - public function espresso_event_months_dropdown() |
|
| 984 | - { |
|
| 985 | - // what we need to do is get all PRIMARY datetimes for all events to filter on. |
|
| 986 | - // Note we need to include any other filters that are set! |
|
| 987 | - $status = isset($this->_req_data['status']) ? $this->_req_data['status'] : null; |
|
| 988 | - // categories? |
|
| 989 | - $category = isset($this->_req_data['EVT_CAT']) && $this->_req_data['EVT_CAT'] > 0 |
|
| 990 | - ? $this->_req_data['EVT_CAT'] |
|
| 991 | - : null; |
|
| 992 | - // active status? |
|
| 993 | - $active_status = isset($this->_req_data['active_status']) ? $this->_req_data['active_status'] : null; |
|
| 994 | - $cur_date = isset($this->_req_data['month_range']) ? $this->_req_data['month_range'] : ''; |
|
| 995 | - return EEH_Form_Fields::generate_event_months_dropdown($cur_date, $status, $category, $active_status); |
|
| 996 | - } |
|
| 997 | - |
|
| 998 | - |
|
| 999 | - /** |
|
| 1000 | - * returns a list of "active" statuses on the event |
|
| 1001 | - * |
|
| 1002 | - * @param string $current_value whatever the current active status is |
|
| 1003 | - * @return string |
|
| 1004 | - */ |
|
| 1005 | - public function active_status_dropdown($current_value = '') |
|
| 1006 | - { |
|
| 1007 | - $select_name = 'active_status'; |
|
| 1008 | - $values = array( |
|
| 1009 | - 'none' => esc_html__('Show Active/Inactive', 'event_espresso'), |
|
| 1010 | - 'active' => esc_html__('Active', 'event_espresso'), |
|
| 1011 | - 'upcoming' => esc_html__('Upcoming', 'event_espresso'), |
|
| 1012 | - 'expired' => esc_html__('Expired', 'event_espresso'), |
|
| 1013 | - 'inactive' => esc_html__('Inactive', 'event_espresso'), |
|
| 1014 | - ); |
|
| 1015 | - $id = 'id="espresso-active-status-dropdown-filter"'; |
|
| 1016 | - $class = 'wide'; |
|
| 1017 | - return EEH_Form_Fields::select_input($select_name, $values, $current_value, $id, $class); |
|
| 1018 | - } |
|
| 1019 | - |
|
| 1020 | - |
|
| 1021 | - /** |
|
| 1022 | - * output a dropdown of the categories for the category filter on the event admin list table |
|
| 1023 | - * |
|
| 1024 | - * @access public |
|
| 1025 | - * @return string html |
|
| 1026 | - */ |
|
| 1027 | - public function category_dropdown() |
|
| 1028 | - { |
|
| 1029 | - $cur_cat = isset($this->_req_data['EVT_CAT']) ? $this->_req_data['EVT_CAT'] : -1; |
|
| 1030 | - return EEH_Form_Fields::generate_event_category_dropdown($cur_cat); |
|
| 1031 | - } |
|
| 1032 | - |
|
| 1033 | - |
|
| 1034 | - /** |
|
| 1035 | - * get total number of events today |
|
| 1036 | - * |
|
| 1037 | - * @access public |
|
| 1038 | - * @return int |
|
| 1039 | - * @throws EE_Error |
|
| 1040 | - */ |
|
| 1041 | - public function total_events_today() |
|
| 1042 | - { |
|
| 1043 | - $start = EEM_Datetime::instance()->convert_datetime_for_query( |
|
| 1044 | - 'DTT_EVT_start', |
|
| 1045 | - date('Y-m-d') . ' 00:00:00', |
|
| 1046 | - 'Y-m-d H:i:s', |
|
| 1047 | - 'UTC' |
|
| 1048 | - ); |
|
| 1049 | - $end = EEM_Datetime::instance()->convert_datetime_for_query( |
|
| 1050 | - 'DTT_EVT_start', |
|
| 1051 | - date('Y-m-d') . ' 23:59:59', |
|
| 1052 | - 'Y-m-d H:i:s', |
|
| 1053 | - 'UTC' |
|
| 1054 | - ); |
|
| 1055 | - $where = array( |
|
| 1056 | - 'Datetime.DTT_EVT_start' => array('BETWEEN', array($start, $end)), |
|
| 1057 | - ); |
|
| 1058 | - $count = EEM_Event::instance()->count(array($where, 'caps' => 'read_admin'), 'EVT_ID', true); |
|
| 1059 | - return $count; |
|
| 1060 | - } |
|
| 1061 | - |
|
| 1062 | - |
|
| 1063 | - /** |
|
| 1064 | - * get total number of events this month |
|
| 1065 | - * |
|
| 1066 | - * @access public |
|
| 1067 | - * @return int |
|
| 1068 | - * @throws EE_Error |
|
| 1069 | - */ |
|
| 1070 | - public function total_events_this_month() |
|
| 1071 | - { |
|
| 1072 | - // Dates |
|
| 1073 | - $this_year_r = date('Y'); |
|
| 1074 | - $this_month_r = date('m'); |
|
| 1075 | - $days_this_month = date('t'); |
|
| 1076 | - $start = EEM_Datetime::instance()->convert_datetime_for_query( |
|
| 1077 | - 'DTT_EVT_start', |
|
| 1078 | - $this_year_r . '-' . $this_month_r . '-01 00:00:00', |
|
| 1079 | - 'Y-m-d H:i:s', |
|
| 1080 | - 'UTC' |
|
| 1081 | - ); |
|
| 1082 | - $end = EEM_Datetime::instance()->convert_datetime_for_query( |
|
| 1083 | - 'DTT_EVT_start', |
|
| 1084 | - $this_year_r . '-' . $this_month_r . '-' . $days_this_month . ' 23:59:59', |
|
| 1085 | - 'Y-m-d H:i:s', |
|
| 1086 | - 'UTC' |
|
| 1087 | - ); |
|
| 1088 | - $where = array( |
|
| 1089 | - 'Datetime.DTT_EVT_start' => array('BETWEEN', array($start, $end)), |
|
| 1090 | - ); |
|
| 1091 | - $count = EEM_Event::instance()->count(array($where, 'caps' => 'read_admin'), 'EVT_ID', true); |
|
| 1092 | - return $count; |
|
| 1093 | - } |
|
| 1094 | - |
|
| 1095 | - |
|
| 1096 | - /** DEFAULT TICKETS STUFF **/ |
|
| 1097 | - |
|
| 1098 | - /** |
|
| 1099 | - * Output default tickets list table view. |
|
| 1100 | - */ |
|
| 1101 | - public function _tickets_overview_list_table() |
|
| 1102 | - { |
|
| 1103 | - $this->_search_btn_label = esc_html__('Tickets', 'event_espresso'); |
|
| 1104 | - $this->display_admin_list_table_page_with_no_sidebar(); |
|
| 1105 | - } |
|
| 1106 | - |
|
| 1107 | - |
|
| 1108 | - /** |
|
| 1109 | - * @param int $per_page |
|
| 1110 | - * @param bool $count |
|
| 1111 | - * @param bool $trashed |
|
| 1112 | - * @return \EE_Soft_Delete_Base_Class[]|int |
|
| 1113 | - */ |
|
| 1114 | - public function get_default_tickets($per_page = 10, $count = false, $trashed = false) |
|
| 1115 | - { |
|
| 1116 | - $orderby = empty($this->_req_data['orderby']) ? 'TKT_name' : $this->_req_data['orderby']; |
|
| 1117 | - $order = empty($this->_req_data['order']) ? 'ASC' : $this->_req_data['order']; |
|
| 1118 | - switch ($orderby) { |
|
| 1119 | - case 'TKT_name': |
|
| 1120 | - $orderby = array('TKT_name' => $order); |
|
| 1121 | - break; |
|
| 1122 | - case 'TKT_price': |
|
| 1123 | - $orderby = array('TKT_price' => $order); |
|
| 1124 | - break; |
|
| 1125 | - case 'TKT_uses': |
|
| 1126 | - $orderby = array('TKT_uses' => $order); |
|
| 1127 | - break; |
|
| 1128 | - case 'TKT_min': |
|
| 1129 | - $orderby = array('TKT_min' => $order); |
|
| 1130 | - break; |
|
| 1131 | - case 'TKT_max': |
|
| 1132 | - $orderby = array('TKT_max' => $order); |
|
| 1133 | - break; |
|
| 1134 | - case 'TKT_qty': |
|
| 1135 | - $orderby = array('TKT_qty' => $order); |
|
| 1136 | - break; |
|
| 1137 | - } |
|
| 1138 | - $current_page = isset($this->_req_data['paged']) && ! empty($this->_req_data['paged']) |
|
| 1139 | - ? $this->_req_data['paged'] |
|
| 1140 | - : 1; |
|
| 1141 | - $per_page = isset($this->_req_data['perpage']) && ! empty($this->_req_data['perpage']) |
|
| 1142 | - ? $this->_req_data['perpage'] |
|
| 1143 | - : $per_page; |
|
| 1144 | - $_where = array( |
|
| 1145 | - 'TKT_is_default' => 1, |
|
| 1146 | - 'TKT_deleted' => $trashed, |
|
| 1147 | - ); |
|
| 1148 | - $offset = ($current_page - 1) * $per_page; |
|
| 1149 | - $limit = array($offset, $per_page); |
|
| 1150 | - if (isset($this->_req_data['s'])) { |
|
| 1151 | - $sstr = '%' . $this->_req_data['s'] . '%'; |
|
| 1152 | - $_where['OR'] = array( |
|
| 1153 | - 'TKT_name' => array('LIKE', $sstr), |
|
| 1154 | - 'TKT_description' => array('LIKE', $sstr), |
|
| 1155 | - ); |
|
| 1156 | - } |
|
| 1157 | - $query_params = array( |
|
| 1158 | - $_where, |
|
| 1159 | - 'order_by' => $orderby, |
|
| 1160 | - 'limit' => $limit, |
|
| 1161 | - 'group_by' => 'TKT_ID', |
|
| 1162 | - ); |
|
| 1163 | - if ($count) { |
|
| 1164 | - return EEM_Ticket::instance()->count_deleted_and_undeleted(array($_where)); |
|
| 1165 | - } else { |
|
| 1166 | - return EEM_Ticket::instance()->get_all_deleted_and_undeleted($query_params); |
|
| 1167 | - } |
|
| 1168 | - } |
|
| 1169 | - |
|
| 1170 | - |
|
| 1171 | - /** |
|
| 1172 | - * @param bool $trash |
|
| 1173 | - * @throws EE_Error |
|
| 1174 | - */ |
|
| 1175 | - protected function _trash_or_restore_ticket($trash = false) |
|
| 1176 | - { |
|
| 1177 | - $success = 1; |
|
| 1178 | - $TKT = EEM_Ticket::instance(); |
|
| 1179 | - // checkboxes? |
|
| 1180 | - if (! empty($this->_req_data['checkbox']) && is_array($this->_req_data['checkbox'])) { |
|
| 1181 | - // if array has more than one element then success message should be plural |
|
| 1182 | - $success = count($this->_req_data['checkbox']) > 1 ? 2 : 1; |
|
| 1183 | - // cycle thru the boxes |
|
| 1184 | - while (list($TKT_ID, $value) = each($this->_req_data['checkbox'])) { |
|
| 1185 | - if ($trash) { |
|
| 1186 | - if (! $TKT->delete_by_ID($TKT_ID)) { |
|
| 1187 | - $success = 0; |
|
| 1188 | - } |
|
| 1189 | - } else { |
|
| 1190 | - if (! $TKT->restore_by_ID($TKT_ID)) { |
|
| 1191 | - $success = 0; |
|
| 1192 | - } |
|
| 1193 | - } |
|
| 1194 | - } |
|
| 1195 | - } else { |
|
| 1196 | - // grab single id and trash |
|
| 1197 | - $TKT_ID = absint($this->_req_data['TKT_ID']); |
|
| 1198 | - if ($trash) { |
|
| 1199 | - if (! $TKT->delete_by_ID($TKT_ID)) { |
|
| 1200 | - $success = 0; |
|
| 1201 | - } |
|
| 1202 | - } else { |
|
| 1203 | - if (! $TKT->restore_by_ID($TKT_ID)) { |
|
| 1204 | - $success = 0; |
|
| 1205 | - } |
|
| 1206 | - } |
|
| 1207 | - } |
|
| 1208 | - $action_desc = $trash ? 'moved to the trash' : 'restored'; |
|
| 1209 | - $query_args = array( |
|
| 1210 | - 'action' => 'ticket_list_table', |
|
| 1211 | - 'status' => $trash ? '' : 'trashed', |
|
| 1212 | - ); |
|
| 1213 | - $this->_redirect_after_action($success, 'Tickets', $action_desc, $query_args); |
|
| 1214 | - } |
|
| 1215 | - |
|
| 1216 | - |
|
| 1217 | - /** |
|
| 1218 | - * Handles trashing default ticket. |
|
| 1219 | - */ |
|
| 1220 | - protected function _delete_ticket() |
|
| 1221 | - { |
|
| 1222 | - $success = 1; |
|
| 1223 | - // checkboxes? |
|
| 1224 | - if (! empty($this->_req_data['checkbox']) && is_array($this->_req_data['checkbox'])) { |
|
| 1225 | - // if array has more than one element then success message should be plural |
|
| 1226 | - $success = count($this->_req_data['checkbox']) > 1 ? 2 : 1; |
|
| 1227 | - // cycle thru the boxes |
|
| 1228 | - while (list($TKT_ID, $value) = each($this->_req_data['checkbox'])) { |
|
| 1229 | - // delete |
|
| 1230 | - if (! $this->_delete_the_ticket($TKT_ID)) { |
|
| 1231 | - $success = 0; |
|
| 1232 | - } |
|
| 1233 | - } |
|
| 1234 | - } else { |
|
| 1235 | - // grab single id and trash |
|
| 1236 | - $TKT_ID = absint($this->_req_data['TKT_ID']); |
|
| 1237 | - if (! $this->_delete_the_ticket($TKT_ID)) { |
|
| 1238 | - $success = 0; |
|
| 1239 | - } |
|
| 1240 | - } |
|
| 1241 | - $action_desc = 'deleted'; |
|
| 1242 | - $query_args = array( |
|
| 1243 | - 'action' => 'ticket_list_table', |
|
| 1244 | - 'status' => 'trashed', |
|
| 1245 | - ); |
|
| 1246 | - // fail safe. If the default ticket count === 1 then we need to redirect to event overview. |
|
| 1247 | - if (EEM_Ticket::instance()->count_deleted_and_undeleted( |
|
| 1248 | - array(array('TKT_is_default' => 1)), |
|
| 1249 | - 'TKT_ID', |
|
| 1250 | - true |
|
| 1251 | - ) |
|
| 1252 | - ) { |
|
| 1253 | - $query_args = array(); |
|
| 1254 | - } |
|
| 1255 | - $this->_redirect_after_action($success, 'Tickets', $action_desc, $query_args); |
|
| 1256 | - } |
|
| 1257 | - |
|
| 1258 | - |
|
| 1259 | - /** |
|
| 1260 | - * @param int $TKT_ID |
|
| 1261 | - * @return bool|int |
|
| 1262 | - * @throws EE_Error |
|
| 1263 | - */ |
|
| 1264 | - protected function _delete_the_ticket($TKT_ID) |
|
| 1265 | - { |
|
| 1266 | - $tkt = EEM_Ticket::instance()->get_one_by_ID($TKT_ID); |
|
| 1267 | - $tkt->_remove_relations('Datetime'); |
|
| 1268 | - // delete all related prices first |
|
| 1269 | - $tkt->delete_related_permanently('Price'); |
|
| 1270 | - return $tkt->delete_permanently(); |
|
| 1271 | - } |
|
| 19 | + /** |
|
| 20 | + * Extend_Events_Admin_Page constructor. |
|
| 21 | + * |
|
| 22 | + * @param bool $routing |
|
| 23 | + */ |
|
| 24 | + public function __construct($routing = true) |
|
| 25 | + { |
|
| 26 | + parent::__construct($routing); |
|
| 27 | + if (! defined('EVENTS_CAF_TEMPLATE_PATH')) { |
|
| 28 | + define('EVENTS_CAF_TEMPLATE_PATH', EE_CORE_CAF_ADMIN_EXTEND . 'events/templates/'); |
|
| 29 | + define('EVENTS_CAF_ASSETS', EE_CORE_CAF_ADMIN_EXTEND . 'events/assets/'); |
|
| 30 | + define('EVENTS_CAF_ASSETS_URL', EE_CORE_CAF_ADMIN_EXTEND_URL . 'events/assets/'); |
|
| 31 | + } |
|
| 32 | + } |
|
| 33 | + |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * Sets routes. |
|
| 37 | + */ |
|
| 38 | + protected function _extend_page_config() |
|
| 39 | + { |
|
| 40 | + $this->_admin_base_path = EE_CORE_CAF_ADMIN_EXTEND . 'events'; |
|
| 41 | + // is there a evt_id in the request? |
|
| 42 | + $evt_id = ! empty($this->_req_data['EVT_ID']) && ! is_array($this->_req_data['EVT_ID']) |
|
| 43 | + ? $this->_req_data['EVT_ID'] |
|
| 44 | + : 0; |
|
| 45 | + $evt_id = ! empty($this->_req_data['post']) ? $this->_req_data['post'] : $evt_id; |
|
| 46 | + // tkt_id? |
|
| 47 | + $tkt_id = ! empty($this->_req_data['TKT_ID']) && ! is_array($this->_req_data['TKT_ID']) |
|
| 48 | + ? $this->_req_data['TKT_ID'] |
|
| 49 | + : 0; |
|
| 50 | + $new_page_routes = array( |
|
| 51 | + 'duplicate_event' => array( |
|
| 52 | + 'func' => '_duplicate_event', |
|
| 53 | + 'capability' => 'ee_edit_event', |
|
| 54 | + 'obj_id' => $evt_id, |
|
| 55 | + 'noheader' => true, |
|
| 56 | + ), |
|
| 57 | + 'ticket_list_table' => array( |
|
| 58 | + 'func' => '_tickets_overview_list_table', |
|
| 59 | + 'capability' => 'ee_read_default_tickets', |
|
| 60 | + ), |
|
| 61 | + 'trash_ticket' => array( |
|
| 62 | + 'func' => '_trash_or_restore_ticket', |
|
| 63 | + 'capability' => 'ee_delete_default_ticket', |
|
| 64 | + 'obj_id' => $tkt_id, |
|
| 65 | + 'noheader' => true, |
|
| 66 | + 'args' => array('trash' => true), |
|
| 67 | + ), |
|
| 68 | + 'trash_tickets' => array( |
|
| 69 | + 'func' => '_trash_or_restore_ticket', |
|
| 70 | + 'capability' => 'ee_delete_default_tickets', |
|
| 71 | + 'noheader' => true, |
|
| 72 | + 'args' => array('trash' => true), |
|
| 73 | + ), |
|
| 74 | + 'restore_ticket' => array( |
|
| 75 | + 'func' => '_trash_or_restore_ticket', |
|
| 76 | + 'capability' => 'ee_delete_default_ticket', |
|
| 77 | + 'obj_id' => $tkt_id, |
|
| 78 | + 'noheader' => true, |
|
| 79 | + ), |
|
| 80 | + 'restore_tickets' => array( |
|
| 81 | + 'func' => '_trash_or_restore_ticket', |
|
| 82 | + 'capability' => 'ee_delete_default_tickets', |
|
| 83 | + 'noheader' => true, |
|
| 84 | + ), |
|
| 85 | + 'delete_ticket' => array( |
|
| 86 | + 'func' => '_delete_ticket', |
|
| 87 | + 'capability' => 'ee_delete_default_ticket', |
|
| 88 | + 'obj_id' => $tkt_id, |
|
| 89 | + 'noheader' => true, |
|
| 90 | + ), |
|
| 91 | + 'delete_tickets' => array( |
|
| 92 | + 'func' => '_delete_ticket', |
|
| 93 | + 'capability' => 'ee_delete_default_tickets', |
|
| 94 | + 'noheader' => true, |
|
| 95 | + ), |
|
| 96 | + 'import_page' => array( |
|
| 97 | + 'func' => '_import_page', |
|
| 98 | + 'capability' => 'import', |
|
| 99 | + ), |
|
| 100 | + 'import' => array( |
|
| 101 | + 'func' => '_import_events', |
|
| 102 | + 'capability' => 'import', |
|
| 103 | + 'noheader' => true, |
|
| 104 | + ), |
|
| 105 | + 'import_events' => array( |
|
| 106 | + 'func' => '_import_events', |
|
| 107 | + 'capability' => 'import', |
|
| 108 | + 'noheader' => true, |
|
| 109 | + ), |
|
| 110 | + 'export_events' => array( |
|
| 111 | + 'func' => '_events_export', |
|
| 112 | + 'capability' => 'export', |
|
| 113 | + 'noheader' => true, |
|
| 114 | + ), |
|
| 115 | + 'export_categories' => array( |
|
| 116 | + 'func' => '_categories_export', |
|
| 117 | + 'capability' => 'export', |
|
| 118 | + 'noheader' => true, |
|
| 119 | + ), |
|
| 120 | + 'sample_export_file' => array( |
|
| 121 | + 'func' => '_sample_export_file', |
|
| 122 | + 'capability' => 'export', |
|
| 123 | + 'noheader' => true, |
|
| 124 | + ), |
|
| 125 | + 'update_template_settings' => array( |
|
| 126 | + 'func' => '_update_template_settings', |
|
| 127 | + 'capability' => 'manage_options', |
|
| 128 | + 'noheader' => true, |
|
| 129 | + ), |
|
| 130 | + ); |
|
| 131 | + $this->_page_routes = array_merge($this->_page_routes, $new_page_routes); |
|
| 132 | + // partial route/config override |
|
| 133 | + $this->_page_config['import_events']['metaboxes'] = $this->_default_espresso_metaboxes; |
|
| 134 | + $this->_page_config['create_new']['metaboxes'][] = '_premium_event_editor_meta_boxes'; |
|
| 135 | + $this->_page_config['create_new']['qtips'][] = 'EE_Event_Editor_Tips'; |
|
| 136 | + $this->_page_config['edit']['qtips'][] = 'EE_Event_Editor_Tips'; |
|
| 137 | + $this->_page_config['edit']['metaboxes'][] = '_premium_event_editor_meta_boxes'; |
|
| 138 | + $this->_page_config['default']['list_table'] = 'Extend_Events_Admin_List_Table'; |
|
| 139 | + // add tickets tab but only if there are more than one default ticket! |
|
| 140 | + $tkt_count = EEM_Ticket::instance()->count_deleted_and_undeleted( |
|
| 141 | + array(array('TKT_is_default' => 1)), |
|
| 142 | + 'TKT_ID', |
|
| 143 | + true |
|
| 144 | + ); |
|
| 145 | + if ($tkt_count > 1) { |
|
| 146 | + $new_page_config = array( |
|
| 147 | + 'ticket_list_table' => array( |
|
| 148 | + 'nav' => array( |
|
| 149 | + 'label' => esc_html__('Default Tickets', 'event_espresso'), |
|
| 150 | + 'order' => 60, |
|
| 151 | + ), |
|
| 152 | + 'list_table' => 'Tickets_List_Table', |
|
| 153 | + 'require_nonce' => false, |
|
| 154 | + ), |
|
| 155 | + ); |
|
| 156 | + } |
|
| 157 | + // template settings |
|
| 158 | + $new_page_config['template_settings'] = array( |
|
| 159 | + 'nav' => array( |
|
| 160 | + 'label' => esc_html__('Templates', 'event_espresso'), |
|
| 161 | + 'order' => 30, |
|
| 162 | + ), |
|
| 163 | + 'metaboxes' => array_merge($this->_default_espresso_metaboxes, array('_publish_post_box')), |
|
| 164 | + 'help_tabs' => array( |
|
| 165 | + 'general_settings_templates_help_tab' => array( |
|
| 166 | + 'title' => esc_html__('Templates', 'event_espresso'), |
|
| 167 | + 'filename' => 'general_settings_templates', |
|
| 168 | + ), |
|
| 169 | + ), |
|
| 170 | + 'help_tour' => array('Templates_Help_Tour'), |
|
| 171 | + 'require_nonce' => false, |
|
| 172 | + ); |
|
| 173 | + $this->_page_config = array_merge($this->_page_config, $new_page_config); |
|
| 174 | + // add filters and actions |
|
| 175 | + // modifying _views |
|
| 176 | + add_filter( |
|
| 177 | + 'FHEE_event_datetime_metabox_add_additional_date_time_template', |
|
| 178 | + array($this, 'add_additional_datetime_button'), |
|
| 179 | + 10, |
|
| 180 | + 2 |
|
| 181 | + ); |
|
| 182 | + add_filter( |
|
| 183 | + 'FHEE_event_datetime_metabox_clone_button_template', |
|
| 184 | + array($this, 'add_datetime_clone_button'), |
|
| 185 | + 10, |
|
| 186 | + 2 |
|
| 187 | + ); |
|
| 188 | + add_filter( |
|
| 189 | + 'FHEE_event_datetime_metabox_timezones_template', |
|
| 190 | + array($this, 'datetime_timezones_template'), |
|
| 191 | + 10, |
|
| 192 | + 2 |
|
| 193 | + ); |
|
| 194 | + // filters for event list table |
|
| 195 | + add_filter('FHEE__Extend_Events_Admin_List_Table__filters', array($this, 'list_table_filters'), 10, 2); |
|
| 196 | + add_filter( |
|
| 197 | + 'FHEE__Events_Admin_List_Table__column_actions__action_links', |
|
| 198 | + array($this, 'extra_list_table_actions'), |
|
| 199 | + 10, |
|
| 200 | + 2 |
|
| 201 | + ); |
|
| 202 | + // legend item |
|
| 203 | + add_filter('FHEE__Events_Admin_Page___event_legend_items__items', array($this, 'additional_legend_items')); |
|
| 204 | + add_action('admin_init', array($this, 'admin_init')); |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + |
|
| 208 | + /** |
|
| 209 | + * admin_init |
|
| 210 | + */ |
|
| 211 | + public function admin_init() |
|
| 212 | + { |
|
| 213 | + EE_Registry::$i18n_js_strings = array_merge( |
|
| 214 | + EE_Registry::$i18n_js_strings, |
|
| 215 | + array( |
|
| 216 | + 'image_confirm' => esc_html__( |
|
| 217 | + 'Do you really want to delete this image? Please remember to update your event to complete the removal.', |
|
| 218 | + 'event_espresso' |
|
| 219 | + ), |
|
| 220 | + 'event_starts_on' => esc_html__('Event Starts on', 'event_espresso'), |
|
| 221 | + 'event_ends_on' => esc_html__('Event Ends on', 'event_espresso'), |
|
| 222 | + 'event_datetime_actions' => esc_html__('Actions', 'event_espresso'), |
|
| 223 | + 'event_clone_dt_msg' => esc_html__('Clone this Event Date and Time', 'event_espresso'), |
|
| 224 | + 'remove_event_dt_msg' => esc_html__('Remove this Event Time', 'event_espresso'), |
|
| 225 | + ) |
|
| 226 | + ); |
|
| 227 | + } |
|
| 228 | + |
|
| 229 | + |
|
| 230 | + /** |
|
| 231 | + * Add per page screen options to the default ticket list table view. |
|
| 232 | + */ |
|
| 233 | + protected function _add_screen_options_ticket_list_table() |
|
| 234 | + { |
|
| 235 | + $this->_per_page_screen_option(); |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + |
|
| 239 | + /** |
|
| 240 | + * @param string $return |
|
| 241 | + * @param int $id |
|
| 242 | + * @param string $new_title |
|
| 243 | + * @param string $new_slug |
|
| 244 | + * @return string |
|
| 245 | + */ |
|
| 246 | + public function extra_permalink_field_buttons($return, $id, $new_title, $new_slug) |
|
| 247 | + { |
|
| 248 | + $return = parent::extra_permalink_field_buttons($return, $id, $new_title, $new_slug); |
|
| 249 | + // make sure this is only when editing |
|
| 250 | + if (! empty($id)) { |
|
| 251 | + $href = EE_Admin_Page::add_query_args_and_nonce( |
|
| 252 | + array('action' => 'duplicate_event', 'EVT_ID' => $id), |
|
| 253 | + $this->_admin_base_url |
|
| 254 | + ); |
|
| 255 | + $title = esc_attr__('Duplicate Event', 'event_espresso'); |
|
| 256 | + $return .= '<a href="' |
|
| 257 | + . $href |
|
| 258 | + . '" title="' |
|
| 259 | + . $title |
|
| 260 | + . '" id="ee-duplicate-event-button" class="button button-small" value="duplicate_event">' |
|
| 261 | + . $title |
|
| 262 | + . '</a>'; |
|
| 263 | + } |
|
| 264 | + return $return; |
|
| 265 | + } |
|
| 266 | + |
|
| 267 | + |
|
| 268 | + /** |
|
| 269 | + * Set the list table views for the default ticket list table view. |
|
| 270 | + */ |
|
| 271 | + public function _set_list_table_views_ticket_list_table() |
|
| 272 | + { |
|
| 273 | + $this->_views = array( |
|
| 274 | + 'all' => array( |
|
| 275 | + 'slug' => 'all', |
|
| 276 | + 'label' => esc_html__('All', 'event_espresso'), |
|
| 277 | + 'count' => 0, |
|
| 278 | + 'bulk_action' => array( |
|
| 279 | + 'trash_tickets' => esc_html__('Move to Trash', 'event_espresso'), |
|
| 280 | + ), |
|
| 281 | + ), |
|
| 282 | + 'trashed' => array( |
|
| 283 | + 'slug' => 'trashed', |
|
| 284 | + 'label' => esc_html__('Trash', 'event_espresso'), |
|
| 285 | + 'count' => 0, |
|
| 286 | + 'bulk_action' => array( |
|
| 287 | + 'restore_tickets' => esc_html__('Restore from Trash', 'event_espresso'), |
|
| 288 | + 'delete_tickets' => esc_html__('Delete Permanently', 'event_espresso'), |
|
| 289 | + ), |
|
| 290 | + ), |
|
| 291 | + ); |
|
| 292 | + } |
|
| 293 | + |
|
| 294 | + |
|
| 295 | + /** |
|
| 296 | + * Enqueue scripts and styles for the event editor. |
|
| 297 | + */ |
|
| 298 | + public function load_scripts_styles_edit() |
|
| 299 | + { |
|
| 300 | + wp_register_script( |
|
| 301 | + 'ee-event-editor-heartbeat', |
|
| 302 | + EVENTS_CAF_ASSETS_URL . 'event-editor-heartbeat.js', |
|
| 303 | + array('ee_admin_js', 'heartbeat'), |
|
| 304 | + EVENT_ESPRESSO_VERSION, |
|
| 305 | + true |
|
| 306 | + ); |
|
| 307 | + wp_enqueue_script('ee-accounting'); |
|
| 308 | + // styles |
|
| 309 | + wp_enqueue_style('espresso-ui-theme'); |
|
| 310 | + wp_enqueue_script('event_editor_js'); |
|
| 311 | + wp_enqueue_script('ee-event-editor-heartbeat'); |
|
| 312 | + } |
|
| 313 | + |
|
| 314 | + |
|
| 315 | + /** |
|
| 316 | + * Returns template for the additional datetime. |
|
| 317 | + * |
|
| 318 | + * @param $template |
|
| 319 | + * @param $template_args |
|
| 320 | + * @return mixed |
|
| 321 | + * @throws DomainException |
|
| 322 | + */ |
|
| 323 | + public function add_additional_datetime_button($template, $template_args) |
|
| 324 | + { |
|
| 325 | + return EEH_Template::display_template( |
|
| 326 | + EVENTS_CAF_TEMPLATE_PATH . 'event_datetime_add_additional_time.template.php', |
|
| 327 | + $template_args, |
|
| 328 | + true |
|
| 329 | + ); |
|
| 330 | + } |
|
| 331 | + |
|
| 332 | + |
|
| 333 | + /** |
|
| 334 | + * Returns the template for cloning a datetime. |
|
| 335 | + * |
|
| 336 | + * @param $template |
|
| 337 | + * @param $template_args |
|
| 338 | + * @return mixed |
|
| 339 | + * @throws DomainException |
|
| 340 | + */ |
|
| 341 | + public function add_datetime_clone_button($template, $template_args) |
|
| 342 | + { |
|
| 343 | + return EEH_Template::display_template( |
|
| 344 | + EVENTS_CAF_TEMPLATE_PATH . 'event_datetime_metabox_clone_button.template.php', |
|
| 345 | + $template_args, |
|
| 346 | + true |
|
| 347 | + ); |
|
| 348 | + } |
|
| 349 | + |
|
| 350 | + |
|
| 351 | + /** |
|
| 352 | + * Returns the template for datetime timezones. |
|
| 353 | + * |
|
| 354 | + * @param $template |
|
| 355 | + * @param $template_args |
|
| 356 | + * @return mixed |
|
| 357 | + * @throws DomainException |
|
| 358 | + */ |
|
| 359 | + public function datetime_timezones_template($template, $template_args) |
|
| 360 | + { |
|
| 361 | + return EEH_Template::display_template( |
|
| 362 | + EVENTS_CAF_TEMPLATE_PATH . 'event_datetime_timezones.template.php', |
|
| 363 | + $template_args, |
|
| 364 | + true |
|
| 365 | + ); |
|
| 366 | + } |
|
| 367 | + |
|
| 368 | + |
|
| 369 | + /** |
|
| 370 | + * Sets the views for the default list table view. |
|
| 371 | + */ |
|
| 372 | + protected function _set_list_table_views_default() |
|
| 373 | + { |
|
| 374 | + parent::_set_list_table_views_default(); |
|
| 375 | + $new_views = array( |
|
| 376 | + 'today' => array( |
|
| 377 | + 'slug' => 'today', |
|
| 378 | + 'label' => esc_html__('Today', 'event_espresso'), |
|
| 379 | + 'count' => $this->total_events_today(), |
|
| 380 | + 'bulk_action' => array( |
|
| 381 | + 'trash_events' => esc_html__('Move to Trash', 'event_espresso'), |
|
| 382 | + ), |
|
| 383 | + ), |
|
| 384 | + 'month' => array( |
|
| 385 | + 'slug' => 'month', |
|
| 386 | + 'label' => esc_html__('This Month', 'event_espresso'), |
|
| 387 | + 'count' => $this->total_events_this_month(), |
|
| 388 | + 'bulk_action' => array( |
|
| 389 | + 'trash_events' => esc_html__('Move to Trash', 'event_espresso'), |
|
| 390 | + ), |
|
| 391 | + ), |
|
| 392 | + ); |
|
| 393 | + $this->_views = array_merge($this->_views, $new_views); |
|
| 394 | + } |
|
| 395 | + |
|
| 396 | + |
|
| 397 | + /** |
|
| 398 | + * Returns the extra action links for the default list table view. |
|
| 399 | + * |
|
| 400 | + * @param array $action_links |
|
| 401 | + * @param \EE_Event $event |
|
| 402 | + * @return array |
|
| 403 | + * @throws EE_Error |
|
| 404 | + */ |
|
| 405 | + public function extra_list_table_actions(array $action_links, \EE_Event $event) |
|
| 406 | + { |
|
| 407 | + if (EE_Registry::instance()->CAP->current_user_can( |
|
| 408 | + 'ee_read_registrations', |
|
| 409 | + 'espresso_registrations_reports', |
|
| 410 | + $event->ID() |
|
| 411 | + ) |
|
| 412 | + ) { |
|
| 413 | + $reports_query_args = array( |
|
| 414 | + 'action' => 'reports', |
|
| 415 | + 'EVT_ID' => $event->ID(), |
|
| 416 | + ); |
|
| 417 | + $reports_link = EE_Admin_Page::add_query_args_and_nonce($reports_query_args, REG_ADMIN_URL); |
|
| 418 | + $action_links[] = '<a href="' |
|
| 419 | + . $reports_link |
|
| 420 | + . '" title="' |
|
| 421 | + . esc_attr__('View Report', 'event_espresso') |
|
| 422 | + . '"><div class="dashicons dashicons-chart-bar"></div></a>' |
|
| 423 | + . "\n\t"; |
|
| 424 | + } |
|
| 425 | + if (EE_Registry::instance()->CAP->current_user_can('ee_read_global_messages', 'view_filtered_messages')) { |
|
| 426 | + EE_Registry::instance()->load_helper('MSG_Template'); |
|
| 427 | + $action_links[] = EEH_MSG_Template::get_message_action_link( |
|
| 428 | + 'see_notifications_for', |
|
| 429 | + null, |
|
| 430 | + array('EVT_ID' => $event->ID()) |
|
| 431 | + ); |
|
| 432 | + } |
|
| 433 | + return $action_links; |
|
| 434 | + } |
|
| 435 | + |
|
| 436 | + |
|
| 437 | + /** |
|
| 438 | + * @param $items |
|
| 439 | + * @return mixed |
|
| 440 | + */ |
|
| 441 | + public function additional_legend_items($items) |
|
| 442 | + { |
|
| 443 | + if (EE_Registry::instance()->CAP->current_user_can( |
|
| 444 | + 'ee_read_registrations', |
|
| 445 | + 'espresso_registrations_reports' |
|
| 446 | + ) |
|
| 447 | + ) { |
|
| 448 | + $items['reports'] = array( |
|
| 449 | + 'class' => 'dashicons dashicons-chart-bar', |
|
| 450 | + 'desc' => esc_html__('Event Reports', 'event_espresso'), |
|
| 451 | + ); |
|
| 452 | + } |
|
| 453 | + if (EE_Registry::instance()->CAP->current_user_can('ee_read_global_messages', 'view_filtered_messages')) { |
|
| 454 | + $related_for_icon = EEH_MSG_Template::get_message_action_icon('see_notifications_for'); |
|
| 455 | + if (isset($related_for_icon['css_class']) && isset($related_for_icon['label'])) { |
|
| 456 | + $items['view_related_messages'] = array( |
|
| 457 | + 'class' => $related_for_icon['css_class'], |
|
| 458 | + 'desc' => $related_for_icon['label'], |
|
| 459 | + ); |
|
| 460 | + } |
|
| 461 | + } |
|
| 462 | + return $items; |
|
| 463 | + } |
|
| 464 | + |
|
| 465 | + |
|
| 466 | + /** |
|
| 467 | + * This is the callback method for the duplicate event route |
|
| 468 | + * Method looks for 'EVT_ID' in the request and retrieves that event and its details and duplicates them |
|
| 469 | + * into a new event. We add a hook so that any plugins that add extra event details can hook into this |
|
| 470 | + * action. Note that the dupe will have **DUPLICATE** as its title and slug. |
|
| 471 | + * After duplication the redirect is to the new event edit page. |
|
| 472 | + * |
|
| 473 | + * @return void |
|
| 474 | + * @access protected |
|
| 475 | + * @throws EE_Error If EE_Event is not available with given ID |
|
| 476 | + */ |
|
| 477 | + protected function _duplicate_event() |
|
| 478 | + { |
|
| 479 | + // first make sure the ID for the event is in the request. |
|
| 480 | + // If it isn't then we need to bail and redirect back to overview list table (cause how did we get here?) |
|
| 481 | + if (! isset($this->_req_data['EVT_ID'])) { |
|
| 482 | + EE_Error::add_error( |
|
| 483 | + esc_html__( |
|
| 484 | + 'In order to duplicate an event an Event ID is required. None was given.', |
|
| 485 | + 'event_espresso' |
|
| 486 | + ), |
|
| 487 | + __FILE__, |
|
| 488 | + __FUNCTION__, |
|
| 489 | + __LINE__ |
|
| 490 | + ); |
|
| 491 | + $this->_redirect_after_action(false, '', '', array(), true); |
|
| 492 | + return; |
|
| 493 | + } |
|
| 494 | + // k we've got EVT_ID so let's use that to get the event we'll duplicate |
|
| 495 | + $orig_event = EEM_Event::instance()->get_one_by_ID($this->_req_data['EVT_ID']); |
|
| 496 | + if (! $orig_event instanceof EE_Event) { |
|
| 497 | + throw new EE_Error( |
|
| 498 | + sprintf( |
|
| 499 | + esc_html__('An EE_Event object could not be retrieved for the given ID (%s)', 'event_espresso'), |
|
| 500 | + $this->_req_data['EVT_ID'] |
|
| 501 | + ) |
|
| 502 | + ); |
|
| 503 | + } |
|
| 504 | + // k now let's clone the $orig_event before getting relations |
|
| 505 | + $new_event = clone $orig_event; |
|
| 506 | + // original datetimes |
|
| 507 | + $orig_datetimes = $orig_event->get_many_related('Datetime'); |
|
| 508 | + // other original relations |
|
| 509 | + $orig_ven = $orig_event->get_many_related('Venue'); |
|
| 510 | + // reset the ID and modify other details to make it clear this is a dupe |
|
| 511 | + $new_event->set('EVT_ID', 0); |
|
| 512 | + $new_name = $new_event->name() . ' ' . esc_html__('**DUPLICATE**', 'event_espresso'); |
|
| 513 | + $new_event->set('EVT_name', $new_name); |
|
| 514 | + $new_event->set( |
|
| 515 | + 'EVT_slug', |
|
| 516 | + wp_unique_post_slug( |
|
| 517 | + sanitize_title($orig_event->name()), |
|
| 518 | + 0, |
|
| 519 | + 'publish', |
|
| 520 | + 'espresso_events', |
|
| 521 | + 0 |
|
| 522 | + ) |
|
| 523 | + ); |
|
| 524 | + $new_event->set('status', 'draft'); |
|
| 525 | + // duplicate discussion settings |
|
| 526 | + $new_event->set('comment_status', $orig_event->get('comment_status')); |
|
| 527 | + $new_event->set('ping_status', $orig_event->get('ping_status')); |
|
| 528 | + // save the new event |
|
| 529 | + $new_event->save(); |
|
| 530 | + // venues |
|
| 531 | + foreach ($orig_ven as $ven) { |
|
| 532 | + $new_event->_add_relation_to($ven, 'Venue'); |
|
| 533 | + } |
|
| 534 | + $new_event->save(); |
|
| 535 | + // now we need to get the question group relations and handle that |
|
| 536 | + // first primary question groups |
|
| 537 | + $orig_primary_qgs = $orig_event->get_many_related( |
|
| 538 | + 'Question_Group', |
|
| 539 | + array(array('Event_Question_Group.EQG_primary' => 1)) |
|
| 540 | + ); |
|
| 541 | + if (! empty($orig_primary_qgs)) { |
|
| 542 | + foreach ($orig_primary_qgs as $id => $obj) { |
|
| 543 | + if ($obj instanceof EE_Question_Group) { |
|
| 544 | + $new_event->_add_relation_to($obj, 'Question_Group', array('EQG_primary' => 1)); |
|
| 545 | + } |
|
| 546 | + } |
|
| 547 | + } |
|
| 548 | + // next additional attendee question groups |
|
| 549 | + $orig_additional_qgs = $orig_event->get_many_related( |
|
| 550 | + 'Question_Group', |
|
| 551 | + array(array('Event_Question_Group.EQG_primary' => 0)) |
|
| 552 | + ); |
|
| 553 | + if (! empty($orig_additional_qgs)) { |
|
| 554 | + foreach ($orig_additional_qgs as $id => $obj) { |
|
| 555 | + if ($obj instanceof EE_Question_Group) { |
|
| 556 | + $new_event->_add_relation_to($obj, 'Question_Group', array('EQG_primary' => 0)); |
|
| 557 | + } |
|
| 558 | + } |
|
| 559 | + } |
|
| 560 | + |
|
| 561 | + $new_event->save(); |
|
| 562 | + |
|
| 563 | + // k now that we have the new event saved we can loop through the datetimes and start adding relations. |
|
| 564 | + $cloned_tickets = array(); |
|
| 565 | + foreach ($orig_datetimes as $orig_dtt) { |
|
| 566 | + if (! $orig_dtt instanceof EE_Datetime) { |
|
| 567 | + continue; |
|
| 568 | + } |
|
| 569 | + $new_dtt = clone $orig_dtt; |
|
| 570 | + $orig_tkts = $orig_dtt->tickets(); |
|
| 571 | + // save new dtt then add to event |
|
| 572 | + $new_dtt->set('DTT_ID', 0); |
|
| 573 | + $new_dtt->set('DTT_sold', 0); |
|
| 574 | + $new_dtt->set_reserved(0); |
|
| 575 | + $new_dtt->save(); |
|
| 576 | + $new_event->_add_relation_to($new_dtt, 'Datetime'); |
|
| 577 | + $new_event->save(); |
|
| 578 | + // now let's get the ticket relations setup. |
|
| 579 | + foreach ((array) $orig_tkts as $orig_tkt) { |
|
| 580 | + // it's possible a datetime will have no tickets so let's verify we HAVE a ticket first. |
|
| 581 | + if (! $orig_tkt instanceof EE_Ticket) { |
|
| 582 | + continue; |
|
| 583 | + } |
|
| 584 | + // is this ticket archived? If it is then let's skip |
|
| 585 | + if ($orig_tkt->get('TKT_deleted')) { |
|
| 586 | + continue; |
|
| 587 | + } |
|
| 588 | + // does this original ticket already exist in the clone_tickets cache? |
|
| 589 | + // If so we'll just use the new ticket from it. |
|
| 590 | + if (isset($cloned_tickets[ $orig_tkt->ID() ])) { |
|
| 591 | + $new_tkt = $cloned_tickets[ $orig_tkt->ID() ]; |
|
| 592 | + } else { |
|
| 593 | + $new_tkt = clone $orig_tkt; |
|
| 594 | + // get relations on the $orig_tkt that we need to setup. |
|
| 595 | + $orig_prices = $orig_tkt->prices(); |
|
| 596 | + $new_tkt->set('TKT_ID', 0); |
|
| 597 | + $new_tkt->set('TKT_sold', 0); |
|
| 598 | + $new_tkt->set('TKT_reserved', 0); |
|
| 599 | + $new_tkt->save(); // make sure new ticket has ID. |
|
| 600 | + // price relations on new ticket need to be setup. |
|
| 601 | + foreach ($orig_prices as $orig_price) { |
|
| 602 | + $new_price = clone $orig_price; |
|
| 603 | + $new_price->set('PRC_ID', 0); |
|
| 604 | + $new_price->save(); |
|
| 605 | + $new_tkt->_add_relation_to($new_price, 'Price'); |
|
| 606 | + $new_tkt->save(); |
|
| 607 | + } |
|
| 608 | + |
|
| 609 | + do_action( |
|
| 610 | + 'AHEE__Extend_Events_Admin_Page___duplicate_event__duplicate_ticket__after', |
|
| 611 | + $orig_tkt, |
|
| 612 | + $new_tkt, |
|
| 613 | + $orig_prices, |
|
| 614 | + $orig_event, |
|
| 615 | + $orig_dtt, |
|
| 616 | + $new_dtt |
|
| 617 | + ); |
|
| 618 | + } |
|
| 619 | + // k now we can add the new ticket as a relation to the new datetime |
|
| 620 | + // and make sure its added to our cached $cloned_tickets array |
|
| 621 | + // for use with later datetimes that have the same ticket. |
|
| 622 | + $new_dtt->_add_relation_to($new_tkt, 'Ticket'); |
|
| 623 | + $new_dtt->save(); |
|
| 624 | + $cloned_tickets[ $orig_tkt->ID() ] = $new_tkt; |
|
| 625 | + } |
|
| 626 | + } |
|
| 627 | + // clone taxonomy information |
|
| 628 | + $taxonomies_to_clone_with = apply_filters( |
|
| 629 | + 'FHEE__Extend_Events_Admin_Page___duplicate_event__taxonomies_to_clone', |
|
| 630 | + array('espresso_event_categories', 'espresso_event_type', 'post_tag') |
|
| 631 | + ); |
|
| 632 | + // get terms for original event (notice) |
|
| 633 | + $orig_terms = wp_get_object_terms($orig_event->ID(), $taxonomies_to_clone_with); |
|
| 634 | + // loop through terms and add them to new event. |
|
| 635 | + foreach ($orig_terms as $term) { |
|
| 636 | + wp_set_object_terms($new_event->ID(), $term->term_id, $term->taxonomy, true); |
|
| 637 | + } |
|
| 638 | + |
|
| 639 | + // duplicate other core WP_Post items for this event. |
|
| 640 | + // post thumbnail (feature image). |
|
| 641 | + $feature_image_id = get_post_thumbnail_id($orig_event->ID()); |
|
| 642 | + if ($feature_image_id) { |
|
| 643 | + update_post_meta($new_event->ID(), '_thumbnail_id', $feature_image_id); |
|
| 644 | + } |
|
| 645 | + |
|
| 646 | + // duplicate page_template setting |
|
| 647 | + $page_template = get_post_meta($orig_event->ID(), '_wp_page_template', true); |
|
| 648 | + if ($page_template) { |
|
| 649 | + update_post_meta($new_event->ID(), '_wp_page_template', $page_template); |
|
| 650 | + } |
|
| 651 | + |
|
| 652 | + do_action('AHEE__Extend_Events_Admin_Page___duplicate_event__after', $new_event, $orig_event); |
|
| 653 | + // now let's redirect to the edit page for this duplicated event if we have a new event id. |
|
| 654 | + if ($new_event->ID()) { |
|
| 655 | + $redirect_args = array( |
|
| 656 | + 'post' => $new_event->ID(), |
|
| 657 | + 'action' => 'edit', |
|
| 658 | + ); |
|
| 659 | + EE_Error::add_success( |
|
| 660 | + esc_html__( |
|
| 661 | + 'Event successfully duplicated. Please review the details below and make any necessary edits', |
|
| 662 | + 'event_espresso' |
|
| 663 | + ) |
|
| 664 | + ); |
|
| 665 | + } else { |
|
| 666 | + $redirect_args = array( |
|
| 667 | + 'action' => 'default', |
|
| 668 | + ); |
|
| 669 | + EE_Error::add_error( |
|
| 670 | + esc_html__('Not able to duplicate event. Something went wrong.', 'event_espresso'), |
|
| 671 | + __FILE__, |
|
| 672 | + __FUNCTION__, |
|
| 673 | + __LINE__ |
|
| 674 | + ); |
|
| 675 | + } |
|
| 676 | + $this->_redirect_after_action(false, '', '', $redirect_args, true); |
|
| 677 | + } |
|
| 678 | + |
|
| 679 | + |
|
| 680 | + /** |
|
| 681 | + * Generates output for the import page. |
|
| 682 | + * |
|
| 683 | + * @throws DomainException |
|
| 684 | + */ |
|
| 685 | + protected function _import_page() |
|
| 686 | + { |
|
| 687 | + $title = esc_html__('Import', 'event_espresso'); |
|
| 688 | + $intro = esc_html__( |
|
| 689 | + 'If you have a previously exported Event Espresso 4 information in a Comma Separated Value (CSV) file format, you can upload the file here: ', |
|
| 690 | + 'event_espresso' |
|
| 691 | + ); |
|
| 692 | + $form_url = EVENTS_ADMIN_URL; |
|
| 693 | + $action = 'import_events'; |
|
| 694 | + $type = 'csv'; |
|
| 695 | + $this->_template_args['form'] = EE_Import::instance()->upload_form( |
|
| 696 | + $title, |
|
| 697 | + $intro, |
|
| 698 | + $form_url, |
|
| 699 | + $action, |
|
| 700 | + $type |
|
| 701 | + ); |
|
| 702 | + $this->_template_args['sample_file_link'] = EE_Admin_Page::add_query_args_and_nonce( |
|
| 703 | + array('action' => 'sample_export_file'), |
|
| 704 | + $this->_admin_base_url |
|
| 705 | + ); |
|
| 706 | + $content = EEH_Template::display_template( |
|
| 707 | + EVENTS_CAF_TEMPLATE_PATH . 'import_page.template.php', |
|
| 708 | + $this->_template_args, |
|
| 709 | + true |
|
| 710 | + ); |
|
| 711 | + $this->_template_args['admin_page_content'] = $content; |
|
| 712 | + $this->display_admin_page_with_sidebar(); |
|
| 713 | + } |
|
| 714 | + |
|
| 715 | + |
|
| 716 | + /** |
|
| 717 | + * _import_events |
|
| 718 | + * This handles displaying the screen and running imports for importing events. |
|
| 719 | + * |
|
| 720 | + * @return void |
|
| 721 | + */ |
|
| 722 | + protected function _import_events() |
|
| 723 | + { |
|
| 724 | + require_once(EE_CLASSES . 'EE_Import.class.php'); |
|
| 725 | + $success = EE_Import::instance()->import(); |
|
| 726 | + $this->_redirect_after_action($success, 'Import File', 'ran', array('action' => 'import_page'), true); |
|
| 727 | + } |
|
| 728 | + |
|
| 729 | + |
|
| 730 | + /** |
|
| 731 | + * _events_export |
|
| 732 | + * Will export all (or just the given event) to a Excel compatible file. |
|
| 733 | + * |
|
| 734 | + * @access protected |
|
| 735 | + * @return void |
|
| 736 | + */ |
|
| 737 | + protected function _events_export() |
|
| 738 | + { |
|
| 739 | + if (isset($this->_req_data['EVT_ID'])) { |
|
| 740 | + $event_ids = $this->_req_data['EVT_ID']; |
|
| 741 | + } elseif (isset($this->_req_data['EVT_IDs'])) { |
|
| 742 | + $event_ids = $this->_req_data['EVT_IDs']; |
|
| 743 | + } else { |
|
| 744 | + $event_ids = null; |
|
| 745 | + } |
|
| 746 | + // todo: I don't like doing this but it'll do until we modify EE_Export Class. |
|
| 747 | + $new_request_args = array( |
|
| 748 | + 'export' => 'report', |
|
| 749 | + 'action' => 'all_event_data', |
|
| 750 | + 'EVT_ID' => $event_ids, |
|
| 751 | + ); |
|
| 752 | + $this->_req_data = array_merge($this->_req_data, $new_request_args); |
|
| 753 | + if (is_readable(EE_CLASSES . 'EE_Export.class.php')) { |
|
| 754 | + require_once(EE_CLASSES . 'EE_Export.class.php'); |
|
| 755 | + $EE_Export = EE_Export::instance($this->_req_data); |
|
| 756 | + $EE_Export->export(); |
|
| 757 | + } |
|
| 758 | + } |
|
| 759 | + |
|
| 760 | + |
|
| 761 | + /** |
|
| 762 | + * handle category exports() |
|
| 763 | + * |
|
| 764 | + * @return void |
|
| 765 | + */ |
|
| 766 | + protected function _categories_export() |
|
| 767 | + { |
|
| 768 | + // todo: I don't like doing this but it'll do until we modify EE_Export Class. |
|
| 769 | + $new_request_args = array( |
|
| 770 | + 'export' => 'report', |
|
| 771 | + 'action' => 'categories', |
|
| 772 | + 'category_ids' => $this->_req_data['EVT_CAT_ID'], |
|
| 773 | + ); |
|
| 774 | + $this->_req_data = array_merge($this->_req_data, $new_request_args); |
|
| 775 | + if (is_readable(EE_CLASSES . 'EE_Export.class.php')) { |
|
| 776 | + require_once(EE_CLASSES . 'EE_Export.class.php'); |
|
| 777 | + $EE_Export = EE_Export::instance($this->_req_data); |
|
| 778 | + $EE_Export->export(); |
|
| 779 | + } |
|
| 780 | + } |
|
| 781 | + |
|
| 782 | + |
|
| 783 | + /** |
|
| 784 | + * Creates a sample CSV file for importing |
|
| 785 | + */ |
|
| 786 | + protected function _sample_export_file() |
|
| 787 | + { |
|
| 788 | + // require_once(EE_CLASSES . 'EE_Export.class.php'); |
|
| 789 | + EE_Export::instance()->export_sample(); |
|
| 790 | + } |
|
| 791 | + |
|
| 792 | + |
|
| 793 | + /************* Template Settings *************/ |
|
| 794 | + /** |
|
| 795 | + * Generates template settings page output |
|
| 796 | + * |
|
| 797 | + * @throws DomainException |
|
| 798 | + * @throws EE_Error |
|
| 799 | + */ |
|
| 800 | + protected function _template_settings() |
|
| 801 | + { |
|
| 802 | + $this->_template_args['values'] = $this->_yes_no_values; |
|
| 803 | + /** |
|
| 804 | + * Note leaving this filter in for backward compatibility this was moved in 4.6.x |
|
| 805 | + * from General_Settings_Admin_Page to here. |
|
| 806 | + */ |
|
| 807 | + $this->_template_args = apply_filters( |
|
| 808 | + 'FHEE__General_Settings_Admin_Page__template_settings__template_args', |
|
| 809 | + $this->_template_args |
|
| 810 | + ); |
|
| 811 | + $this->_set_add_edit_form_tags('update_template_settings'); |
|
| 812 | + $this->_set_publish_post_box_vars(null, false, false, null, false); |
|
| 813 | + $this->_template_args['admin_page_content'] = EEH_Template::display_template( |
|
| 814 | + EVENTS_CAF_TEMPLATE_PATH . 'template_settings.template.php', |
|
| 815 | + $this->_template_args, |
|
| 816 | + true |
|
| 817 | + ); |
|
| 818 | + $this->display_admin_page_with_sidebar(); |
|
| 819 | + } |
|
| 820 | + |
|
| 821 | + |
|
| 822 | + /** |
|
| 823 | + * Handler for updating template settings. |
|
| 824 | + * |
|
| 825 | + * @throws InvalidInterfaceException |
|
| 826 | + * @throws InvalidDataTypeException |
|
| 827 | + * @throws InvalidArgumentException |
|
| 828 | + */ |
|
| 829 | + protected function _update_template_settings() |
|
| 830 | + { |
|
| 831 | + /** |
|
| 832 | + * Note leaving this filter in for backward compatibility this was moved in 4.6.x |
|
| 833 | + * from General_Settings_Admin_Page to here. |
|
| 834 | + */ |
|
| 835 | + EE_Registry::instance()->CFG->template_settings = apply_filters( |
|
| 836 | + 'FHEE__General_Settings_Admin_Page__update_template_settings__data', |
|
| 837 | + EE_Registry::instance()->CFG->template_settings, |
|
| 838 | + $this->_req_data |
|
| 839 | + ); |
|
| 840 | + // update custom post type slugs and detect if we need to flush rewrite rules |
|
| 841 | + $old_slug = EE_Registry::instance()->CFG->core->event_cpt_slug; |
|
| 842 | + EE_Registry::instance()->CFG->core->event_cpt_slug = empty($this->_req_data['event_cpt_slug']) |
|
| 843 | + ? EE_Registry::instance()->CFG->core->event_cpt_slug |
|
| 844 | + : EEH_URL::slugify($this->_req_data['event_cpt_slug'], 'events'); |
|
| 845 | + $what = 'Template Settings'; |
|
| 846 | + $success = $this->_update_espresso_configuration( |
|
| 847 | + $what, |
|
| 848 | + EE_Registry::instance()->CFG->template_settings, |
|
| 849 | + __FILE__, |
|
| 850 | + __FUNCTION__, |
|
| 851 | + __LINE__ |
|
| 852 | + ); |
|
| 853 | + if (EE_Registry::instance()->CFG->core->event_cpt_slug != $old_slug) { |
|
| 854 | + /** @var EventEspresso\core\domain\services\custom_post_types\RewriteRules $rewrite_rules */ |
|
| 855 | + $rewrite_rules = LoaderFactory::getLoader()->getShared( |
|
| 856 | + 'EventEspresso\core\domain\services\custom_post_types\RewriteRules' |
|
| 857 | + ); |
|
| 858 | + $rewrite_rules->flush(); |
|
| 859 | + } |
|
| 860 | + $this->_redirect_after_action($success, $what, 'updated', array('action' => 'template_settings')); |
|
| 861 | + } |
|
| 862 | + |
|
| 863 | + |
|
| 864 | + /** |
|
| 865 | + * _premium_event_editor_meta_boxes |
|
| 866 | + * add all metaboxes related to the event_editor |
|
| 867 | + * |
|
| 868 | + * @access protected |
|
| 869 | + * @return void |
|
| 870 | + * @throws EE_Error |
|
| 871 | + */ |
|
| 872 | + protected function _premium_event_editor_meta_boxes() |
|
| 873 | + { |
|
| 874 | + $this->verify_cpt_object(); |
|
| 875 | + add_meta_box( |
|
| 876 | + 'espresso_event_editor_event_options', |
|
| 877 | + esc_html__('Event Registration Options', 'event_espresso'), |
|
| 878 | + array($this, 'registration_options_meta_box'), |
|
| 879 | + $this->page_slug, |
|
| 880 | + 'side', |
|
| 881 | + 'core' |
|
| 882 | + ); |
|
| 883 | + } |
|
| 884 | + |
|
| 885 | + |
|
| 886 | + /** |
|
| 887 | + * override caf metabox |
|
| 888 | + * |
|
| 889 | + * @return void |
|
| 890 | + * @throws DomainException |
|
| 891 | + */ |
|
| 892 | + public function registration_options_meta_box() |
|
| 893 | + { |
|
| 894 | + $yes_no_values = array( |
|
| 895 | + array('id' => true, 'text' => esc_html__('Yes', 'event_espresso')), |
|
| 896 | + array('id' => false, 'text' => esc_html__('No', 'event_espresso')), |
|
| 897 | + ); |
|
| 898 | + $default_reg_status_values = EEM_Registration::reg_status_array( |
|
| 899 | + array( |
|
| 900 | + EEM_Registration::status_id_cancelled, |
|
| 901 | + EEM_Registration::status_id_declined, |
|
| 902 | + EEM_Registration::status_id_incomplete, |
|
| 903 | + EEM_Registration::status_id_wait_list, |
|
| 904 | + ), |
|
| 905 | + true |
|
| 906 | + ); |
|
| 907 | + $template_args['active_status'] = $this->_cpt_model_obj->pretty_active_status(false); |
|
| 908 | + $template_args['_event'] = $this->_cpt_model_obj; |
|
| 909 | + $template_args['additional_limit'] = $this->_cpt_model_obj->additional_limit(); |
|
| 910 | + $template_args['default_registration_status'] = EEH_Form_Fields::select_input( |
|
| 911 | + 'default_reg_status', |
|
| 912 | + $default_reg_status_values, |
|
| 913 | + $this->_cpt_model_obj->default_registration_status() |
|
| 914 | + ); |
|
| 915 | + $template_args['display_description'] = EEH_Form_Fields::select_input( |
|
| 916 | + 'display_desc', |
|
| 917 | + $yes_no_values, |
|
| 918 | + $this->_cpt_model_obj->display_description() |
|
| 919 | + ); |
|
| 920 | + $template_args['display_ticket_selector'] = EEH_Form_Fields::select_input( |
|
| 921 | + 'display_ticket_selector', |
|
| 922 | + $yes_no_values, |
|
| 923 | + $this->_cpt_model_obj->display_ticket_selector(), |
|
| 924 | + '', |
|
| 925 | + '', |
|
| 926 | + false |
|
| 927 | + ); |
|
| 928 | + $template_args['EVT_default_registration_status'] = EEH_Form_Fields::select_input( |
|
| 929 | + 'EVT_default_registration_status', |
|
| 930 | + $default_reg_status_values, |
|
| 931 | + $this->_cpt_model_obj->default_registration_status() |
|
| 932 | + ); |
|
| 933 | + $template_args['additional_registration_options'] = apply_filters( |
|
| 934 | + 'FHEE__Events_Admin_Page__registration_options_meta_box__additional_registration_options', |
|
| 935 | + '', |
|
| 936 | + $template_args, |
|
| 937 | + $yes_no_values, |
|
| 938 | + $default_reg_status_values |
|
| 939 | + ); |
|
| 940 | + EEH_Template::display_template( |
|
| 941 | + EVENTS_CAF_TEMPLATE_PATH . 'event_registration_options.template.php', |
|
| 942 | + $template_args |
|
| 943 | + ); |
|
| 944 | + } |
|
| 945 | + |
|
| 946 | + |
|
| 947 | + |
|
| 948 | + /** |
|
| 949 | + * wp_list_table_mods for caf |
|
| 950 | + * ============================ |
|
| 951 | + */ |
|
| 952 | + /** |
|
| 953 | + * hook into list table filters and provide filters for caffeinated list table |
|
| 954 | + * |
|
| 955 | + * @param array $old_filters any existing filters present |
|
| 956 | + * @param array $list_table_obj the list table object |
|
| 957 | + * @return array new filters |
|
| 958 | + */ |
|
| 959 | + public function list_table_filters($old_filters, $list_table_obj) |
|
| 960 | + { |
|
| 961 | + $filters = array(); |
|
| 962 | + // first month/year filters |
|
| 963 | + $filters[] = $this->espresso_event_months_dropdown(); |
|
| 964 | + $status = isset($this->_req_data['status']) ? $this->_req_data['status'] : null; |
|
| 965 | + // active status dropdown |
|
| 966 | + if ($status !== 'draft') { |
|
| 967 | + $filters[] = $this->active_status_dropdown( |
|
| 968 | + isset($this->_req_data['active_status']) ? $this->_req_data['active_status'] : '' |
|
| 969 | + ); |
|
| 970 | + } |
|
| 971 | + // category filter |
|
| 972 | + $filters[] = $this->category_dropdown(); |
|
| 973 | + return array_merge($old_filters, $filters); |
|
| 974 | + } |
|
| 975 | + |
|
| 976 | + |
|
| 977 | + /** |
|
| 978 | + * espresso_event_months_dropdown |
|
| 979 | + * |
|
| 980 | + * @access public |
|
| 981 | + * @return string dropdown listing month/year selections for events. |
|
| 982 | + */ |
|
| 983 | + public function espresso_event_months_dropdown() |
|
| 984 | + { |
|
| 985 | + // what we need to do is get all PRIMARY datetimes for all events to filter on. |
|
| 986 | + // Note we need to include any other filters that are set! |
|
| 987 | + $status = isset($this->_req_data['status']) ? $this->_req_data['status'] : null; |
|
| 988 | + // categories? |
|
| 989 | + $category = isset($this->_req_data['EVT_CAT']) && $this->_req_data['EVT_CAT'] > 0 |
|
| 990 | + ? $this->_req_data['EVT_CAT'] |
|
| 991 | + : null; |
|
| 992 | + // active status? |
|
| 993 | + $active_status = isset($this->_req_data['active_status']) ? $this->_req_data['active_status'] : null; |
|
| 994 | + $cur_date = isset($this->_req_data['month_range']) ? $this->_req_data['month_range'] : ''; |
|
| 995 | + return EEH_Form_Fields::generate_event_months_dropdown($cur_date, $status, $category, $active_status); |
|
| 996 | + } |
|
| 997 | + |
|
| 998 | + |
|
| 999 | + /** |
|
| 1000 | + * returns a list of "active" statuses on the event |
|
| 1001 | + * |
|
| 1002 | + * @param string $current_value whatever the current active status is |
|
| 1003 | + * @return string |
|
| 1004 | + */ |
|
| 1005 | + public function active_status_dropdown($current_value = '') |
|
| 1006 | + { |
|
| 1007 | + $select_name = 'active_status'; |
|
| 1008 | + $values = array( |
|
| 1009 | + 'none' => esc_html__('Show Active/Inactive', 'event_espresso'), |
|
| 1010 | + 'active' => esc_html__('Active', 'event_espresso'), |
|
| 1011 | + 'upcoming' => esc_html__('Upcoming', 'event_espresso'), |
|
| 1012 | + 'expired' => esc_html__('Expired', 'event_espresso'), |
|
| 1013 | + 'inactive' => esc_html__('Inactive', 'event_espresso'), |
|
| 1014 | + ); |
|
| 1015 | + $id = 'id="espresso-active-status-dropdown-filter"'; |
|
| 1016 | + $class = 'wide'; |
|
| 1017 | + return EEH_Form_Fields::select_input($select_name, $values, $current_value, $id, $class); |
|
| 1018 | + } |
|
| 1019 | + |
|
| 1020 | + |
|
| 1021 | + /** |
|
| 1022 | + * output a dropdown of the categories for the category filter on the event admin list table |
|
| 1023 | + * |
|
| 1024 | + * @access public |
|
| 1025 | + * @return string html |
|
| 1026 | + */ |
|
| 1027 | + public function category_dropdown() |
|
| 1028 | + { |
|
| 1029 | + $cur_cat = isset($this->_req_data['EVT_CAT']) ? $this->_req_data['EVT_CAT'] : -1; |
|
| 1030 | + return EEH_Form_Fields::generate_event_category_dropdown($cur_cat); |
|
| 1031 | + } |
|
| 1032 | + |
|
| 1033 | + |
|
| 1034 | + /** |
|
| 1035 | + * get total number of events today |
|
| 1036 | + * |
|
| 1037 | + * @access public |
|
| 1038 | + * @return int |
|
| 1039 | + * @throws EE_Error |
|
| 1040 | + */ |
|
| 1041 | + public function total_events_today() |
|
| 1042 | + { |
|
| 1043 | + $start = EEM_Datetime::instance()->convert_datetime_for_query( |
|
| 1044 | + 'DTT_EVT_start', |
|
| 1045 | + date('Y-m-d') . ' 00:00:00', |
|
| 1046 | + 'Y-m-d H:i:s', |
|
| 1047 | + 'UTC' |
|
| 1048 | + ); |
|
| 1049 | + $end = EEM_Datetime::instance()->convert_datetime_for_query( |
|
| 1050 | + 'DTT_EVT_start', |
|
| 1051 | + date('Y-m-d') . ' 23:59:59', |
|
| 1052 | + 'Y-m-d H:i:s', |
|
| 1053 | + 'UTC' |
|
| 1054 | + ); |
|
| 1055 | + $where = array( |
|
| 1056 | + 'Datetime.DTT_EVT_start' => array('BETWEEN', array($start, $end)), |
|
| 1057 | + ); |
|
| 1058 | + $count = EEM_Event::instance()->count(array($where, 'caps' => 'read_admin'), 'EVT_ID', true); |
|
| 1059 | + return $count; |
|
| 1060 | + } |
|
| 1061 | + |
|
| 1062 | + |
|
| 1063 | + /** |
|
| 1064 | + * get total number of events this month |
|
| 1065 | + * |
|
| 1066 | + * @access public |
|
| 1067 | + * @return int |
|
| 1068 | + * @throws EE_Error |
|
| 1069 | + */ |
|
| 1070 | + public function total_events_this_month() |
|
| 1071 | + { |
|
| 1072 | + // Dates |
|
| 1073 | + $this_year_r = date('Y'); |
|
| 1074 | + $this_month_r = date('m'); |
|
| 1075 | + $days_this_month = date('t'); |
|
| 1076 | + $start = EEM_Datetime::instance()->convert_datetime_for_query( |
|
| 1077 | + 'DTT_EVT_start', |
|
| 1078 | + $this_year_r . '-' . $this_month_r . '-01 00:00:00', |
|
| 1079 | + 'Y-m-d H:i:s', |
|
| 1080 | + 'UTC' |
|
| 1081 | + ); |
|
| 1082 | + $end = EEM_Datetime::instance()->convert_datetime_for_query( |
|
| 1083 | + 'DTT_EVT_start', |
|
| 1084 | + $this_year_r . '-' . $this_month_r . '-' . $days_this_month . ' 23:59:59', |
|
| 1085 | + 'Y-m-d H:i:s', |
|
| 1086 | + 'UTC' |
|
| 1087 | + ); |
|
| 1088 | + $where = array( |
|
| 1089 | + 'Datetime.DTT_EVT_start' => array('BETWEEN', array($start, $end)), |
|
| 1090 | + ); |
|
| 1091 | + $count = EEM_Event::instance()->count(array($where, 'caps' => 'read_admin'), 'EVT_ID', true); |
|
| 1092 | + return $count; |
|
| 1093 | + } |
|
| 1094 | + |
|
| 1095 | + |
|
| 1096 | + /** DEFAULT TICKETS STUFF **/ |
|
| 1097 | + |
|
| 1098 | + /** |
|
| 1099 | + * Output default tickets list table view. |
|
| 1100 | + */ |
|
| 1101 | + public function _tickets_overview_list_table() |
|
| 1102 | + { |
|
| 1103 | + $this->_search_btn_label = esc_html__('Tickets', 'event_espresso'); |
|
| 1104 | + $this->display_admin_list_table_page_with_no_sidebar(); |
|
| 1105 | + } |
|
| 1106 | + |
|
| 1107 | + |
|
| 1108 | + /** |
|
| 1109 | + * @param int $per_page |
|
| 1110 | + * @param bool $count |
|
| 1111 | + * @param bool $trashed |
|
| 1112 | + * @return \EE_Soft_Delete_Base_Class[]|int |
|
| 1113 | + */ |
|
| 1114 | + public function get_default_tickets($per_page = 10, $count = false, $trashed = false) |
|
| 1115 | + { |
|
| 1116 | + $orderby = empty($this->_req_data['orderby']) ? 'TKT_name' : $this->_req_data['orderby']; |
|
| 1117 | + $order = empty($this->_req_data['order']) ? 'ASC' : $this->_req_data['order']; |
|
| 1118 | + switch ($orderby) { |
|
| 1119 | + case 'TKT_name': |
|
| 1120 | + $orderby = array('TKT_name' => $order); |
|
| 1121 | + break; |
|
| 1122 | + case 'TKT_price': |
|
| 1123 | + $orderby = array('TKT_price' => $order); |
|
| 1124 | + break; |
|
| 1125 | + case 'TKT_uses': |
|
| 1126 | + $orderby = array('TKT_uses' => $order); |
|
| 1127 | + break; |
|
| 1128 | + case 'TKT_min': |
|
| 1129 | + $orderby = array('TKT_min' => $order); |
|
| 1130 | + break; |
|
| 1131 | + case 'TKT_max': |
|
| 1132 | + $orderby = array('TKT_max' => $order); |
|
| 1133 | + break; |
|
| 1134 | + case 'TKT_qty': |
|
| 1135 | + $orderby = array('TKT_qty' => $order); |
|
| 1136 | + break; |
|
| 1137 | + } |
|
| 1138 | + $current_page = isset($this->_req_data['paged']) && ! empty($this->_req_data['paged']) |
|
| 1139 | + ? $this->_req_data['paged'] |
|
| 1140 | + : 1; |
|
| 1141 | + $per_page = isset($this->_req_data['perpage']) && ! empty($this->_req_data['perpage']) |
|
| 1142 | + ? $this->_req_data['perpage'] |
|
| 1143 | + : $per_page; |
|
| 1144 | + $_where = array( |
|
| 1145 | + 'TKT_is_default' => 1, |
|
| 1146 | + 'TKT_deleted' => $trashed, |
|
| 1147 | + ); |
|
| 1148 | + $offset = ($current_page - 1) * $per_page; |
|
| 1149 | + $limit = array($offset, $per_page); |
|
| 1150 | + if (isset($this->_req_data['s'])) { |
|
| 1151 | + $sstr = '%' . $this->_req_data['s'] . '%'; |
|
| 1152 | + $_where['OR'] = array( |
|
| 1153 | + 'TKT_name' => array('LIKE', $sstr), |
|
| 1154 | + 'TKT_description' => array('LIKE', $sstr), |
|
| 1155 | + ); |
|
| 1156 | + } |
|
| 1157 | + $query_params = array( |
|
| 1158 | + $_where, |
|
| 1159 | + 'order_by' => $orderby, |
|
| 1160 | + 'limit' => $limit, |
|
| 1161 | + 'group_by' => 'TKT_ID', |
|
| 1162 | + ); |
|
| 1163 | + if ($count) { |
|
| 1164 | + return EEM_Ticket::instance()->count_deleted_and_undeleted(array($_where)); |
|
| 1165 | + } else { |
|
| 1166 | + return EEM_Ticket::instance()->get_all_deleted_and_undeleted($query_params); |
|
| 1167 | + } |
|
| 1168 | + } |
|
| 1169 | + |
|
| 1170 | + |
|
| 1171 | + /** |
|
| 1172 | + * @param bool $trash |
|
| 1173 | + * @throws EE_Error |
|
| 1174 | + */ |
|
| 1175 | + protected function _trash_or_restore_ticket($trash = false) |
|
| 1176 | + { |
|
| 1177 | + $success = 1; |
|
| 1178 | + $TKT = EEM_Ticket::instance(); |
|
| 1179 | + // checkboxes? |
|
| 1180 | + if (! empty($this->_req_data['checkbox']) && is_array($this->_req_data['checkbox'])) { |
|
| 1181 | + // if array has more than one element then success message should be plural |
|
| 1182 | + $success = count($this->_req_data['checkbox']) > 1 ? 2 : 1; |
|
| 1183 | + // cycle thru the boxes |
|
| 1184 | + while (list($TKT_ID, $value) = each($this->_req_data['checkbox'])) { |
|
| 1185 | + if ($trash) { |
|
| 1186 | + if (! $TKT->delete_by_ID($TKT_ID)) { |
|
| 1187 | + $success = 0; |
|
| 1188 | + } |
|
| 1189 | + } else { |
|
| 1190 | + if (! $TKT->restore_by_ID($TKT_ID)) { |
|
| 1191 | + $success = 0; |
|
| 1192 | + } |
|
| 1193 | + } |
|
| 1194 | + } |
|
| 1195 | + } else { |
|
| 1196 | + // grab single id and trash |
|
| 1197 | + $TKT_ID = absint($this->_req_data['TKT_ID']); |
|
| 1198 | + if ($trash) { |
|
| 1199 | + if (! $TKT->delete_by_ID($TKT_ID)) { |
|
| 1200 | + $success = 0; |
|
| 1201 | + } |
|
| 1202 | + } else { |
|
| 1203 | + if (! $TKT->restore_by_ID($TKT_ID)) { |
|
| 1204 | + $success = 0; |
|
| 1205 | + } |
|
| 1206 | + } |
|
| 1207 | + } |
|
| 1208 | + $action_desc = $trash ? 'moved to the trash' : 'restored'; |
|
| 1209 | + $query_args = array( |
|
| 1210 | + 'action' => 'ticket_list_table', |
|
| 1211 | + 'status' => $trash ? '' : 'trashed', |
|
| 1212 | + ); |
|
| 1213 | + $this->_redirect_after_action($success, 'Tickets', $action_desc, $query_args); |
|
| 1214 | + } |
|
| 1215 | + |
|
| 1216 | + |
|
| 1217 | + /** |
|
| 1218 | + * Handles trashing default ticket. |
|
| 1219 | + */ |
|
| 1220 | + protected function _delete_ticket() |
|
| 1221 | + { |
|
| 1222 | + $success = 1; |
|
| 1223 | + // checkboxes? |
|
| 1224 | + if (! empty($this->_req_data['checkbox']) && is_array($this->_req_data['checkbox'])) { |
|
| 1225 | + // if array has more than one element then success message should be plural |
|
| 1226 | + $success = count($this->_req_data['checkbox']) > 1 ? 2 : 1; |
|
| 1227 | + // cycle thru the boxes |
|
| 1228 | + while (list($TKT_ID, $value) = each($this->_req_data['checkbox'])) { |
|
| 1229 | + // delete |
|
| 1230 | + if (! $this->_delete_the_ticket($TKT_ID)) { |
|
| 1231 | + $success = 0; |
|
| 1232 | + } |
|
| 1233 | + } |
|
| 1234 | + } else { |
|
| 1235 | + // grab single id and trash |
|
| 1236 | + $TKT_ID = absint($this->_req_data['TKT_ID']); |
|
| 1237 | + if (! $this->_delete_the_ticket($TKT_ID)) { |
|
| 1238 | + $success = 0; |
|
| 1239 | + } |
|
| 1240 | + } |
|
| 1241 | + $action_desc = 'deleted'; |
|
| 1242 | + $query_args = array( |
|
| 1243 | + 'action' => 'ticket_list_table', |
|
| 1244 | + 'status' => 'trashed', |
|
| 1245 | + ); |
|
| 1246 | + // fail safe. If the default ticket count === 1 then we need to redirect to event overview. |
|
| 1247 | + if (EEM_Ticket::instance()->count_deleted_and_undeleted( |
|
| 1248 | + array(array('TKT_is_default' => 1)), |
|
| 1249 | + 'TKT_ID', |
|
| 1250 | + true |
|
| 1251 | + ) |
|
| 1252 | + ) { |
|
| 1253 | + $query_args = array(); |
|
| 1254 | + } |
|
| 1255 | + $this->_redirect_after_action($success, 'Tickets', $action_desc, $query_args); |
|
| 1256 | + } |
|
| 1257 | + |
|
| 1258 | + |
|
| 1259 | + /** |
|
| 1260 | + * @param int $TKT_ID |
|
| 1261 | + * @return bool|int |
|
| 1262 | + * @throws EE_Error |
|
| 1263 | + */ |
|
| 1264 | + protected function _delete_the_ticket($TKT_ID) |
|
| 1265 | + { |
|
| 1266 | + $tkt = EEM_Ticket::instance()->get_one_by_ID($TKT_ID); |
|
| 1267 | + $tkt->_remove_relations('Datetime'); |
|
| 1268 | + // delete all related prices first |
|
| 1269 | + $tkt->delete_related_permanently('Price'); |
|
| 1270 | + return $tkt->delete_permanently(); |
|
| 1271 | + } |
|
| 1272 | 1272 | } |
@@ -24,10 +24,10 @@ discard block |
||
| 24 | 24 | public function __construct($routing = true) |
| 25 | 25 | { |
| 26 | 26 | parent::__construct($routing); |
| 27 | - if (! defined('EVENTS_CAF_TEMPLATE_PATH')) { |
|
| 28 | - define('EVENTS_CAF_TEMPLATE_PATH', EE_CORE_CAF_ADMIN_EXTEND . 'events/templates/'); |
|
| 29 | - define('EVENTS_CAF_ASSETS', EE_CORE_CAF_ADMIN_EXTEND . 'events/assets/'); |
|
| 30 | - define('EVENTS_CAF_ASSETS_URL', EE_CORE_CAF_ADMIN_EXTEND_URL . 'events/assets/'); |
|
| 27 | + if ( ! defined('EVENTS_CAF_TEMPLATE_PATH')) { |
|
| 28 | + define('EVENTS_CAF_TEMPLATE_PATH', EE_CORE_CAF_ADMIN_EXTEND.'events/templates/'); |
|
| 29 | + define('EVENTS_CAF_ASSETS', EE_CORE_CAF_ADMIN_EXTEND.'events/assets/'); |
|
| 30 | + define('EVENTS_CAF_ASSETS_URL', EE_CORE_CAF_ADMIN_EXTEND_URL.'events/assets/'); |
|
| 31 | 31 | } |
| 32 | 32 | } |
| 33 | 33 | |
@@ -37,7 +37,7 @@ discard block |
||
| 37 | 37 | */ |
| 38 | 38 | protected function _extend_page_config() |
| 39 | 39 | { |
| 40 | - $this->_admin_base_path = EE_CORE_CAF_ADMIN_EXTEND . 'events'; |
|
| 40 | + $this->_admin_base_path = EE_CORE_CAF_ADMIN_EXTEND.'events'; |
|
| 41 | 41 | // is there a evt_id in the request? |
| 42 | 42 | $evt_id = ! empty($this->_req_data['EVT_ID']) && ! is_array($this->_req_data['EVT_ID']) |
| 43 | 43 | ? $this->_req_data['EVT_ID'] |
@@ -247,7 +247,7 @@ discard block |
||
| 247 | 247 | { |
| 248 | 248 | $return = parent::extra_permalink_field_buttons($return, $id, $new_title, $new_slug); |
| 249 | 249 | // make sure this is only when editing |
| 250 | - if (! empty($id)) { |
|
| 250 | + if ( ! empty($id)) { |
|
| 251 | 251 | $href = EE_Admin_Page::add_query_args_and_nonce( |
| 252 | 252 | array('action' => 'duplicate_event', 'EVT_ID' => $id), |
| 253 | 253 | $this->_admin_base_url |
@@ -299,7 +299,7 @@ discard block |
||
| 299 | 299 | { |
| 300 | 300 | wp_register_script( |
| 301 | 301 | 'ee-event-editor-heartbeat', |
| 302 | - EVENTS_CAF_ASSETS_URL . 'event-editor-heartbeat.js', |
|
| 302 | + EVENTS_CAF_ASSETS_URL.'event-editor-heartbeat.js', |
|
| 303 | 303 | array('ee_admin_js', 'heartbeat'), |
| 304 | 304 | EVENT_ESPRESSO_VERSION, |
| 305 | 305 | true |
@@ -323,7 +323,7 @@ discard block |
||
| 323 | 323 | public function add_additional_datetime_button($template, $template_args) |
| 324 | 324 | { |
| 325 | 325 | return EEH_Template::display_template( |
| 326 | - EVENTS_CAF_TEMPLATE_PATH . 'event_datetime_add_additional_time.template.php', |
|
| 326 | + EVENTS_CAF_TEMPLATE_PATH.'event_datetime_add_additional_time.template.php', |
|
| 327 | 327 | $template_args, |
| 328 | 328 | true |
| 329 | 329 | ); |
@@ -341,7 +341,7 @@ discard block |
||
| 341 | 341 | public function add_datetime_clone_button($template, $template_args) |
| 342 | 342 | { |
| 343 | 343 | return EEH_Template::display_template( |
| 344 | - EVENTS_CAF_TEMPLATE_PATH . 'event_datetime_metabox_clone_button.template.php', |
|
| 344 | + EVENTS_CAF_TEMPLATE_PATH.'event_datetime_metabox_clone_button.template.php', |
|
| 345 | 345 | $template_args, |
| 346 | 346 | true |
| 347 | 347 | ); |
@@ -359,7 +359,7 @@ discard block |
||
| 359 | 359 | public function datetime_timezones_template($template, $template_args) |
| 360 | 360 | { |
| 361 | 361 | return EEH_Template::display_template( |
| 362 | - EVENTS_CAF_TEMPLATE_PATH . 'event_datetime_timezones.template.php', |
|
| 362 | + EVENTS_CAF_TEMPLATE_PATH.'event_datetime_timezones.template.php', |
|
| 363 | 363 | $template_args, |
| 364 | 364 | true |
| 365 | 365 | ); |
@@ -478,7 +478,7 @@ discard block |
||
| 478 | 478 | { |
| 479 | 479 | // first make sure the ID for the event is in the request. |
| 480 | 480 | // If it isn't then we need to bail and redirect back to overview list table (cause how did we get here?) |
| 481 | - if (! isset($this->_req_data['EVT_ID'])) { |
|
| 481 | + if ( ! isset($this->_req_data['EVT_ID'])) { |
|
| 482 | 482 | EE_Error::add_error( |
| 483 | 483 | esc_html__( |
| 484 | 484 | 'In order to duplicate an event an Event ID is required. None was given.', |
@@ -493,7 +493,7 @@ discard block |
||
| 493 | 493 | } |
| 494 | 494 | // k we've got EVT_ID so let's use that to get the event we'll duplicate |
| 495 | 495 | $orig_event = EEM_Event::instance()->get_one_by_ID($this->_req_data['EVT_ID']); |
| 496 | - if (! $orig_event instanceof EE_Event) { |
|
| 496 | + if ( ! $orig_event instanceof EE_Event) { |
|
| 497 | 497 | throw new EE_Error( |
| 498 | 498 | sprintf( |
| 499 | 499 | esc_html__('An EE_Event object could not be retrieved for the given ID (%s)', 'event_espresso'), |
@@ -509,7 +509,7 @@ discard block |
||
| 509 | 509 | $orig_ven = $orig_event->get_many_related('Venue'); |
| 510 | 510 | // reset the ID and modify other details to make it clear this is a dupe |
| 511 | 511 | $new_event->set('EVT_ID', 0); |
| 512 | - $new_name = $new_event->name() . ' ' . esc_html__('**DUPLICATE**', 'event_espresso'); |
|
| 512 | + $new_name = $new_event->name().' '.esc_html__('**DUPLICATE**', 'event_espresso'); |
|
| 513 | 513 | $new_event->set('EVT_name', $new_name); |
| 514 | 514 | $new_event->set( |
| 515 | 515 | 'EVT_slug', |
@@ -538,7 +538,7 @@ discard block |
||
| 538 | 538 | 'Question_Group', |
| 539 | 539 | array(array('Event_Question_Group.EQG_primary' => 1)) |
| 540 | 540 | ); |
| 541 | - if (! empty($orig_primary_qgs)) { |
|
| 541 | + if ( ! empty($orig_primary_qgs)) { |
|
| 542 | 542 | foreach ($orig_primary_qgs as $id => $obj) { |
| 543 | 543 | if ($obj instanceof EE_Question_Group) { |
| 544 | 544 | $new_event->_add_relation_to($obj, 'Question_Group', array('EQG_primary' => 1)); |
@@ -550,7 +550,7 @@ discard block |
||
| 550 | 550 | 'Question_Group', |
| 551 | 551 | array(array('Event_Question_Group.EQG_primary' => 0)) |
| 552 | 552 | ); |
| 553 | - if (! empty($orig_additional_qgs)) { |
|
| 553 | + if ( ! empty($orig_additional_qgs)) { |
|
| 554 | 554 | foreach ($orig_additional_qgs as $id => $obj) { |
| 555 | 555 | if ($obj instanceof EE_Question_Group) { |
| 556 | 556 | $new_event->_add_relation_to($obj, 'Question_Group', array('EQG_primary' => 0)); |
@@ -563,7 +563,7 @@ discard block |
||
| 563 | 563 | // k now that we have the new event saved we can loop through the datetimes and start adding relations. |
| 564 | 564 | $cloned_tickets = array(); |
| 565 | 565 | foreach ($orig_datetimes as $orig_dtt) { |
| 566 | - if (! $orig_dtt instanceof EE_Datetime) { |
|
| 566 | + if ( ! $orig_dtt instanceof EE_Datetime) { |
|
| 567 | 567 | continue; |
| 568 | 568 | } |
| 569 | 569 | $new_dtt = clone $orig_dtt; |
@@ -578,7 +578,7 @@ discard block |
||
| 578 | 578 | // now let's get the ticket relations setup. |
| 579 | 579 | foreach ((array) $orig_tkts as $orig_tkt) { |
| 580 | 580 | // it's possible a datetime will have no tickets so let's verify we HAVE a ticket first. |
| 581 | - if (! $orig_tkt instanceof EE_Ticket) { |
|
| 581 | + if ( ! $orig_tkt instanceof EE_Ticket) { |
|
| 582 | 582 | continue; |
| 583 | 583 | } |
| 584 | 584 | // is this ticket archived? If it is then let's skip |
@@ -587,8 +587,8 @@ discard block |
||
| 587 | 587 | } |
| 588 | 588 | // does this original ticket already exist in the clone_tickets cache? |
| 589 | 589 | // If so we'll just use the new ticket from it. |
| 590 | - if (isset($cloned_tickets[ $orig_tkt->ID() ])) { |
|
| 591 | - $new_tkt = $cloned_tickets[ $orig_tkt->ID() ]; |
|
| 590 | + if (isset($cloned_tickets[$orig_tkt->ID()])) { |
|
| 591 | + $new_tkt = $cloned_tickets[$orig_tkt->ID()]; |
|
| 592 | 592 | } else { |
| 593 | 593 | $new_tkt = clone $orig_tkt; |
| 594 | 594 | // get relations on the $orig_tkt that we need to setup. |
@@ -621,7 +621,7 @@ discard block |
||
| 621 | 621 | // for use with later datetimes that have the same ticket. |
| 622 | 622 | $new_dtt->_add_relation_to($new_tkt, 'Ticket'); |
| 623 | 623 | $new_dtt->save(); |
| 624 | - $cloned_tickets[ $orig_tkt->ID() ] = $new_tkt; |
|
| 624 | + $cloned_tickets[$orig_tkt->ID()] = $new_tkt; |
|
| 625 | 625 | } |
| 626 | 626 | } |
| 627 | 627 | // clone taxonomy information |
@@ -704,7 +704,7 @@ discard block |
||
| 704 | 704 | $this->_admin_base_url |
| 705 | 705 | ); |
| 706 | 706 | $content = EEH_Template::display_template( |
| 707 | - EVENTS_CAF_TEMPLATE_PATH . 'import_page.template.php', |
|
| 707 | + EVENTS_CAF_TEMPLATE_PATH.'import_page.template.php', |
|
| 708 | 708 | $this->_template_args, |
| 709 | 709 | true |
| 710 | 710 | ); |
@@ -721,7 +721,7 @@ discard block |
||
| 721 | 721 | */ |
| 722 | 722 | protected function _import_events() |
| 723 | 723 | { |
| 724 | - require_once(EE_CLASSES . 'EE_Import.class.php'); |
|
| 724 | + require_once(EE_CLASSES.'EE_Import.class.php'); |
|
| 725 | 725 | $success = EE_Import::instance()->import(); |
| 726 | 726 | $this->_redirect_after_action($success, 'Import File', 'ran', array('action' => 'import_page'), true); |
| 727 | 727 | } |
@@ -750,8 +750,8 @@ discard block |
||
| 750 | 750 | 'EVT_ID' => $event_ids, |
| 751 | 751 | ); |
| 752 | 752 | $this->_req_data = array_merge($this->_req_data, $new_request_args); |
| 753 | - if (is_readable(EE_CLASSES . 'EE_Export.class.php')) { |
|
| 754 | - require_once(EE_CLASSES . 'EE_Export.class.php'); |
|
| 753 | + if (is_readable(EE_CLASSES.'EE_Export.class.php')) { |
|
| 754 | + require_once(EE_CLASSES.'EE_Export.class.php'); |
|
| 755 | 755 | $EE_Export = EE_Export::instance($this->_req_data); |
| 756 | 756 | $EE_Export->export(); |
| 757 | 757 | } |
@@ -772,8 +772,8 @@ discard block |
||
| 772 | 772 | 'category_ids' => $this->_req_data['EVT_CAT_ID'], |
| 773 | 773 | ); |
| 774 | 774 | $this->_req_data = array_merge($this->_req_data, $new_request_args); |
| 775 | - if (is_readable(EE_CLASSES . 'EE_Export.class.php')) { |
|
| 776 | - require_once(EE_CLASSES . 'EE_Export.class.php'); |
|
| 775 | + if (is_readable(EE_CLASSES.'EE_Export.class.php')) { |
|
| 776 | + require_once(EE_CLASSES.'EE_Export.class.php'); |
|
| 777 | 777 | $EE_Export = EE_Export::instance($this->_req_data); |
| 778 | 778 | $EE_Export->export(); |
| 779 | 779 | } |
@@ -811,7 +811,7 @@ discard block |
||
| 811 | 811 | $this->_set_add_edit_form_tags('update_template_settings'); |
| 812 | 812 | $this->_set_publish_post_box_vars(null, false, false, null, false); |
| 813 | 813 | $this->_template_args['admin_page_content'] = EEH_Template::display_template( |
| 814 | - EVENTS_CAF_TEMPLATE_PATH . 'template_settings.template.php', |
|
| 814 | + EVENTS_CAF_TEMPLATE_PATH.'template_settings.template.php', |
|
| 815 | 815 | $this->_template_args, |
| 816 | 816 | true |
| 817 | 817 | ); |
@@ -938,7 +938,7 @@ discard block |
||
| 938 | 938 | $default_reg_status_values |
| 939 | 939 | ); |
| 940 | 940 | EEH_Template::display_template( |
| 941 | - EVENTS_CAF_TEMPLATE_PATH . 'event_registration_options.template.php', |
|
| 941 | + EVENTS_CAF_TEMPLATE_PATH.'event_registration_options.template.php', |
|
| 942 | 942 | $template_args |
| 943 | 943 | ); |
| 944 | 944 | } |
@@ -1042,13 +1042,13 @@ discard block |
||
| 1042 | 1042 | { |
| 1043 | 1043 | $start = EEM_Datetime::instance()->convert_datetime_for_query( |
| 1044 | 1044 | 'DTT_EVT_start', |
| 1045 | - date('Y-m-d') . ' 00:00:00', |
|
| 1045 | + date('Y-m-d').' 00:00:00', |
|
| 1046 | 1046 | 'Y-m-d H:i:s', |
| 1047 | 1047 | 'UTC' |
| 1048 | 1048 | ); |
| 1049 | 1049 | $end = EEM_Datetime::instance()->convert_datetime_for_query( |
| 1050 | 1050 | 'DTT_EVT_start', |
| 1051 | - date('Y-m-d') . ' 23:59:59', |
|
| 1051 | + date('Y-m-d').' 23:59:59', |
|
| 1052 | 1052 | 'Y-m-d H:i:s', |
| 1053 | 1053 | 'UTC' |
| 1054 | 1054 | ); |
@@ -1075,13 +1075,13 @@ discard block |
||
| 1075 | 1075 | $days_this_month = date('t'); |
| 1076 | 1076 | $start = EEM_Datetime::instance()->convert_datetime_for_query( |
| 1077 | 1077 | 'DTT_EVT_start', |
| 1078 | - $this_year_r . '-' . $this_month_r . '-01 00:00:00', |
|
| 1078 | + $this_year_r.'-'.$this_month_r.'-01 00:00:00', |
|
| 1079 | 1079 | 'Y-m-d H:i:s', |
| 1080 | 1080 | 'UTC' |
| 1081 | 1081 | ); |
| 1082 | 1082 | $end = EEM_Datetime::instance()->convert_datetime_for_query( |
| 1083 | 1083 | 'DTT_EVT_start', |
| 1084 | - $this_year_r . '-' . $this_month_r . '-' . $days_this_month . ' 23:59:59', |
|
| 1084 | + $this_year_r.'-'.$this_month_r.'-'.$days_this_month.' 23:59:59', |
|
| 1085 | 1085 | 'Y-m-d H:i:s', |
| 1086 | 1086 | 'UTC' |
| 1087 | 1087 | ); |
@@ -1148,7 +1148,7 @@ discard block |
||
| 1148 | 1148 | $offset = ($current_page - 1) * $per_page; |
| 1149 | 1149 | $limit = array($offset, $per_page); |
| 1150 | 1150 | if (isset($this->_req_data['s'])) { |
| 1151 | - $sstr = '%' . $this->_req_data['s'] . '%'; |
|
| 1151 | + $sstr = '%'.$this->_req_data['s'].'%'; |
|
| 1152 | 1152 | $_where['OR'] = array( |
| 1153 | 1153 | 'TKT_name' => array('LIKE', $sstr), |
| 1154 | 1154 | 'TKT_description' => array('LIKE', $sstr), |
@@ -1177,17 +1177,17 @@ discard block |
||
| 1177 | 1177 | $success = 1; |
| 1178 | 1178 | $TKT = EEM_Ticket::instance(); |
| 1179 | 1179 | // checkboxes? |
| 1180 | - if (! empty($this->_req_data['checkbox']) && is_array($this->_req_data['checkbox'])) { |
|
| 1180 | + if ( ! empty($this->_req_data['checkbox']) && is_array($this->_req_data['checkbox'])) { |
|
| 1181 | 1181 | // if array has more than one element then success message should be plural |
| 1182 | 1182 | $success = count($this->_req_data['checkbox']) > 1 ? 2 : 1; |
| 1183 | 1183 | // cycle thru the boxes |
| 1184 | 1184 | while (list($TKT_ID, $value) = each($this->_req_data['checkbox'])) { |
| 1185 | 1185 | if ($trash) { |
| 1186 | - if (! $TKT->delete_by_ID($TKT_ID)) { |
|
| 1186 | + if ( ! $TKT->delete_by_ID($TKT_ID)) { |
|
| 1187 | 1187 | $success = 0; |
| 1188 | 1188 | } |
| 1189 | 1189 | } else { |
| 1190 | - if (! $TKT->restore_by_ID($TKT_ID)) { |
|
| 1190 | + if ( ! $TKT->restore_by_ID($TKT_ID)) { |
|
| 1191 | 1191 | $success = 0; |
| 1192 | 1192 | } |
| 1193 | 1193 | } |
@@ -1196,11 +1196,11 @@ discard block |
||
| 1196 | 1196 | // grab single id and trash |
| 1197 | 1197 | $TKT_ID = absint($this->_req_data['TKT_ID']); |
| 1198 | 1198 | if ($trash) { |
| 1199 | - if (! $TKT->delete_by_ID($TKT_ID)) { |
|
| 1199 | + if ( ! $TKT->delete_by_ID($TKT_ID)) { |
|
| 1200 | 1200 | $success = 0; |
| 1201 | 1201 | } |
| 1202 | 1202 | } else { |
| 1203 | - if (! $TKT->restore_by_ID($TKT_ID)) { |
|
| 1203 | + if ( ! $TKT->restore_by_ID($TKT_ID)) { |
|
| 1204 | 1204 | $success = 0; |
| 1205 | 1205 | } |
| 1206 | 1206 | } |
@@ -1221,20 +1221,20 @@ discard block |
||
| 1221 | 1221 | { |
| 1222 | 1222 | $success = 1; |
| 1223 | 1223 | // checkboxes? |
| 1224 | - if (! empty($this->_req_data['checkbox']) && is_array($this->_req_data['checkbox'])) { |
|
| 1224 | + if ( ! empty($this->_req_data['checkbox']) && is_array($this->_req_data['checkbox'])) { |
|
| 1225 | 1225 | // if array has more than one element then success message should be plural |
| 1226 | 1226 | $success = count($this->_req_data['checkbox']) > 1 ? 2 : 1; |
| 1227 | 1227 | // cycle thru the boxes |
| 1228 | 1228 | while (list($TKT_ID, $value) = each($this->_req_data['checkbox'])) { |
| 1229 | 1229 | // delete |
| 1230 | - if (! $this->_delete_the_ticket($TKT_ID)) { |
|
| 1230 | + if ( ! $this->_delete_the_ticket($TKT_ID)) { |
|
| 1231 | 1231 | $success = 0; |
| 1232 | 1232 | } |
| 1233 | 1233 | } |
| 1234 | 1234 | } else { |
| 1235 | 1235 | // grab single id and trash |
| 1236 | 1236 | $TKT_ID = absint($this->_req_data['TKT_ID']); |
| 1237 | - if (! $this->_delete_the_ticket($TKT_ID)) { |
|
| 1237 | + if ( ! $this->_delete_the_ticket($TKT_ID)) { |
|
| 1238 | 1238 | $success = 0; |
| 1239 | 1239 | } |
| 1240 | 1240 | } |
@@ -27,1312 +27,1312 @@ |
||
| 27 | 27 | final class EE_System implements ResettableInterface |
| 28 | 28 | { |
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * indicates this is a 'normal' request. Ie, not activation, nor upgrade, nor activation. |
|
| 32 | - * So examples of this would be a normal GET request on the frontend or backend, or a POST, etc |
|
| 33 | - */ |
|
| 34 | - const req_type_normal = 0; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * Indicates this is a brand new installation of EE so we should install |
|
| 38 | - * tables and default data etc |
|
| 39 | - */ |
|
| 40 | - const req_type_new_activation = 1; |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * we've detected that EE has been reactivated (or EE was activated during maintenance mode, |
|
| 44 | - * and we just exited maintenance mode). We MUST check the database is setup properly |
|
| 45 | - * and that default data is setup too |
|
| 46 | - */ |
|
| 47 | - const req_type_reactivation = 2; |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * indicates that EE has been upgraded since its previous request. |
|
| 51 | - * We may have data migration scripts to call and will want to trigger maintenance mode |
|
| 52 | - */ |
|
| 53 | - const req_type_upgrade = 3; |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * TODO will detect that EE has been DOWNGRADED. We probably don't want to run in this case... |
|
| 57 | - */ |
|
| 58 | - const req_type_downgrade = 4; |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * @deprecated since version 4.6.0.dev.006 |
|
| 62 | - * Now whenever a new_activation is detected the request type is still just |
|
| 63 | - * new_activation (same for reactivation, upgrade, downgrade etc), but if we'r ein maintenance mode |
|
| 64 | - * EE_System::initialize_db_if_no_migrations_required and EE_Addon::initialize_db_if_no_migrations_required |
|
| 65 | - * will instead enqueue that EE plugin's db initialization for when we're taken out of maintenance mode. |
|
| 66 | - * (Specifically, when the migration manager indicates migrations are finished |
|
| 67 | - * EE_Data_Migration_Manager::initialize_db_for_enqueued_ee_plugins() will be called) |
|
| 68 | - */ |
|
| 69 | - const req_type_activation_but_not_installed = 5; |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * option prefix for recording the activation history (like core's "espresso_db_update") of addons |
|
| 73 | - */ |
|
| 74 | - const addon_activation_history_option_prefix = 'ee_addon_activation_history_'; |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * @var EE_System $_instance |
|
| 78 | - */ |
|
| 79 | - private static $_instance; |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * @var EE_Registry $registry |
|
| 83 | - */ |
|
| 84 | - private $registry; |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * @var LoaderInterface $loader |
|
| 88 | - */ |
|
| 89 | - private $loader; |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * @var EE_Capabilities $capabilities |
|
| 93 | - */ |
|
| 94 | - private $capabilities; |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * @var RequestInterface $request |
|
| 98 | - */ |
|
| 99 | - private $request; |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * @var EE_Maintenance_Mode $maintenance_mode |
|
| 103 | - */ |
|
| 104 | - private $maintenance_mode; |
|
| 105 | - |
|
| 106 | - /** |
|
| 107 | - * Stores which type of request this is, options being one of the constants on EE_System starting with req_type_*. |
|
| 108 | - * It can be a brand-new activation, a reactivation, an upgrade, a downgrade, or a normal request. |
|
| 109 | - * |
|
| 110 | - * @var int $_req_type |
|
| 111 | - */ |
|
| 112 | - private $_req_type; |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * Whether or not there was a non-micro version change in EE core version during this request |
|
| 116 | - * |
|
| 117 | - * @var boolean $_major_version_change |
|
| 118 | - */ |
|
| 119 | - private $_major_version_change = false; |
|
| 120 | - |
|
| 121 | - /** |
|
| 122 | - * A Context DTO dedicated solely to identifying the current request type. |
|
| 123 | - * |
|
| 124 | - * @var RequestTypeContextCheckerInterface $request_type |
|
| 125 | - */ |
|
| 126 | - private $request_type; |
|
| 127 | - |
|
| 128 | - |
|
| 129 | - /** |
|
| 130 | - * @singleton method used to instantiate class object |
|
| 131 | - * @param EE_Registry|null $registry |
|
| 132 | - * @param LoaderInterface|null $loader |
|
| 133 | - * @param RequestInterface|null $request |
|
| 134 | - * @param EE_Maintenance_Mode|null $maintenance_mode |
|
| 135 | - * @return EE_System |
|
| 136 | - */ |
|
| 137 | - public static function instance( |
|
| 138 | - EE_Registry $registry = null, |
|
| 139 | - LoaderInterface $loader = null, |
|
| 140 | - RequestInterface $request = null, |
|
| 141 | - EE_Maintenance_Mode $maintenance_mode = null |
|
| 142 | - ) { |
|
| 143 | - // check if class object is instantiated |
|
| 144 | - if (! self::$_instance instanceof EE_System) { |
|
| 145 | - self::$_instance = new self($registry, $loader, $request, $maintenance_mode); |
|
| 146 | - } |
|
| 147 | - return self::$_instance; |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - |
|
| 151 | - /** |
|
| 152 | - * resets the instance and returns it |
|
| 153 | - * |
|
| 154 | - * @return EE_System |
|
| 155 | - */ |
|
| 156 | - public static function reset() |
|
| 157 | - { |
|
| 158 | - self::$_instance->_req_type = null; |
|
| 159 | - // make sure none of the old hooks are left hanging around |
|
| 160 | - remove_all_actions('AHEE__EE_System__perform_activations_upgrades_and_migrations'); |
|
| 161 | - // we need to reset the migration manager in order for it to detect DMSs properly |
|
| 162 | - EE_Data_Migration_Manager::reset(); |
|
| 163 | - self::instance()->detect_activations_or_upgrades(); |
|
| 164 | - self::instance()->perform_activations_upgrades_and_migrations(); |
|
| 165 | - return self::instance(); |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - |
|
| 169 | - /** |
|
| 170 | - * sets hooks for running rest of system |
|
| 171 | - * provides "AHEE__EE_System__construct__complete" hook for EE Addons to use as their starting point |
|
| 172 | - * starting EE Addons from any other point may lead to problems |
|
| 173 | - * |
|
| 174 | - * @param EE_Registry $registry |
|
| 175 | - * @param LoaderInterface $loader |
|
| 176 | - * @param RequestInterface $request |
|
| 177 | - * @param EE_Maintenance_Mode $maintenance_mode |
|
| 178 | - */ |
|
| 179 | - private function __construct( |
|
| 180 | - EE_Registry $registry, |
|
| 181 | - LoaderInterface $loader, |
|
| 182 | - RequestInterface $request, |
|
| 183 | - EE_Maintenance_Mode $maintenance_mode |
|
| 184 | - ) { |
|
| 185 | - $this->registry = $registry; |
|
| 186 | - $this->loader = $loader; |
|
| 187 | - $this->request = $request; |
|
| 188 | - $this->maintenance_mode = $maintenance_mode; |
|
| 189 | - do_action('AHEE__EE_System__construct__begin', $this); |
|
| 190 | - add_action( |
|
| 191 | - 'AHEE__EE_Bootstrap__load_espresso_addons', |
|
| 192 | - array($this, 'loadCapabilities'), |
|
| 193 | - 5 |
|
| 194 | - ); |
|
| 195 | - add_action( |
|
| 196 | - 'AHEE__EE_Bootstrap__load_espresso_addons', |
|
| 197 | - array($this, 'loadCommandBus'), |
|
| 198 | - 7 |
|
| 199 | - ); |
|
| 200 | - add_action( |
|
| 201 | - 'AHEE__EE_Bootstrap__load_espresso_addons', |
|
| 202 | - array($this, 'loadPluginApi'), |
|
| 203 | - 9 |
|
| 204 | - ); |
|
| 205 | - // allow addons to load first so that they can register autoloaders, set hooks for running DMS's, etc |
|
| 206 | - add_action( |
|
| 207 | - 'AHEE__EE_Bootstrap__load_espresso_addons', |
|
| 208 | - array($this, 'load_espresso_addons') |
|
| 209 | - ); |
|
| 210 | - // when an ee addon is activated, we want to call the core hook(s) again |
|
| 211 | - // because the newly-activated addon didn't get a chance to run at all |
|
| 212 | - add_action('activate_plugin', array($this, 'load_espresso_addons'), 1); |
|
| 213 | - // detect whether install or upgrade |
|
| 214 | - add_action( |
|
| 215 | - 'AHEE__EE_Bootstrap__detect_activations_or_upgrades', |
|
| 216 | - array($this, 'detect_activations_or_upgrades'), |
|
| 217 | - 3 |
|
| 218 | - ); |
|
| 219 | - // load EE_Config, EE_Textdomain, etc |
|
| 220 | - add_action( |
|
| 221 | - 'AHEE__EE_Bootstrap__load_core_configuration', |
|
| 222 | - array($this, 'load_core_configuration'), |
|
| 223 | - 5 |
|
| 224 | - ); |
|
| 225 | - // load specifications for matching routes to current request |
|
| 226 | - add_action( |
|
| 227 | - 'AHEE__EE_Bootstrap__load_core_configuration', |
|
| 228 | - array($this, 'loadRouteMatchSpecifications') |
|
| 229 | - ); |
|
| 230 | - // load EE_Config, EE_Textdomain, etc |
|
| 231 | - add_action( |
|
| 232 | - 'AHEE__EE_Bootstrap__register_shortcodes_modules_and_widgets', |
|
| 233 | - array($this, 'register_shortcodes_modules_and_widgets'), |
|
| 234 | - 7 |
|
| 235 | - ); |
|
| 236 | - // you wanna get going? I wanna get going... let's get going! |
|
| 237 | - add_action( |
|
| 238 | - 'AHEE__EE_Bootstrap__brew_espresso', |
|
| 239 | - array($this, 'brew_espresso'), |
|
| 240 | - 9 |
|
| 241 | - ); |
|
| 242 | - // other housekeeping |
|
| 243 | - // exclude EE critical pages from wp_list_pages |
|
| 244 | - add_filter( |
|
| 245 | - 'wp_list_pages_excludes', |
|
| 246 | - array($this, 'remove_pages_from_wp_list_pages'), |
|
| 247 | - 10 |
|
| 248 | - ); |
|
| 249 | - // ALL EE Addons should use the following hook point to attach their initial setup too |
|
| 250 | - // it's extremely important for EE Addons to register any class autoloaders so that they can be available when the EE_Config loads |
|
| 251 | - do_action('AHEE__EE_System__construct__complete', $this); |
|
| 252 | - } |
|
| 253 | - |
|
| 254 | - |
|
| 255 | - /** |
|
| 256 | - * load and setup EE_Capabilities |
|
| 257 | - * |
|
| 258 | - * @return void |
|
| 259 | - * @throws EE_Error |
|
| 260 | - */ |
|
| 261 | - public function loadCapabilities() |
|
| 262 | - { |
|
| 263 | - $this->capabilities = $this->loader->getShared('EE_Capabilities'); |
|
| 264 | - add_action( |
|
| 265 | - 'AHEE__EE_Capabilities__init_caps__before_initialization', |
|
| 266 | - function () { |
|
| 267 | - LoaderFactory::getLoader()->getShared('EE_Payment_Method_Manager'); |
|
| 268 | - } |
|
| 269 | - ); |
|
| 270 | - } |
|
| 271 | - |
|
| 272 | - |
|
| 273 | - /** |
|
| 274 | - * create and cache the CommandBus, and also add middleware |
|
| 275 | - * The CapChecker middleware requires the use of EE_Capabilities |
|
| 276 | - * which is why we need to load the CommandBus after Caps are set up |
|
| 277 | - * |
|
| 278 | - * @return void |
|
| 279 | - * @throws EE_Error |
|
| 280 | - */ |
|
| 281 | - public function loadCommandBus() |
|
| 282 | - { |
|
| 283 | - $this->loader->getShared( |
|
| 284 | - 'CommandBusInterface', |
|
| 285 | - array( |
|
| 286 | - null, |
|
| 287 | - apply_filters( |
|
| 288 | - 'FHEE__EE_Load_Espresso_Core__handle_request__CommandBus_middleware', |
|
| 289 | - array( |
|
| 290 | - $this->loader->getShared('EventEspresso\core\services\commands\middleware\CapChecker'), |
|
| 291 | - $this->loader->getShared('EventEspresso\core\services\commands\middleware\AddActionHook'), |
|
| 292 | - ) |
|
| 293 | - ), |
|
| 294 | - ) |
|
| 295 | - ); |
|
| 296 | - } |
|
| 297 | - |
|
| 298 | - |
|
| 299 | - /** |
|
| 300 | - * @return void |
|
| 301 | - * @throws EE_Error |
|
| 302 | - */ |
|
| 303 | - public function loadPluginApi() |
|
| 304 | - { |
|
| 305 | - // set autoloaders for all of the classes implementing EEI_Plugin_API |
|
| 306 | - // which provide helpers for EE plugin authors to more easily register certain components with EE. |
|
| 307 | - EEH_Autoloader::instance()->register_autoloaders_for_each_file_in_folder(EE_LIBRARIES . 'plugin_api'); |
|
| 308 | - $this->loader->getShared('EE_Request_Handler'); |
|
| 309 | - } |
|
| 310 | - |
|
| 311 | - |
|
| 312 | - /** |
|
| 313 | - * @param string $addon_name |
|
| 314 | - * @param string $version_constant |
|
| 315 | - * @param string $min_version_required |
|
| 316 | - * @param string $load_callback |
|
| 317 | - * @param string $plugin_file_constant |
|
| 318 | - * @return void |
|
| 319 | - */ |
|
| 320 | - private function deactivateIncompatibleAddon( |
|
| 321 | - $addon_name, |
|
| 322 | - $version_constant, |
|
| 323 | - $min_version_required, |
|
| 324 | - $load_callback, |
|
| 325 | - $plugin_file_constant |
|
| 326 | - ) { |
|
| 327 | - if (! defined($version_constant)) { |
|
| 328 | - return; |
|
| 329 | - } |
|
| 330 | - $addon_version = constant($version_constant); |
|
| 331 | - if ($addon_version && version_compare($addon_version, $min_version_required, '<')) { |
|
| 332 | - remove_action('AHEE__EE_System__load_espresso_addons', $load_callback); |
|
| 333 | - if (! function_exists('deactivate_plugins')) { |
|
| 334 | - require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
|
| 335 | - } |
|
| 336 | - deactivate_plugins(plugin_basename(constant($plugin_file_constant))); |
|
| 337 | - unset($_GET['activate'], $_REQUEST['activate'], $_GET['activate-multi'], $_REQUEST['activate-multi']); |
|
| 338 | - EE_Error::add_error( |
|
| 339 | - sprintf( |
|
| 340 | - esc_html__( |
|
| 341 | - 'We\'re sorry, but the Event Espresso %1$s addon was deactivated because version %2$s or higher is required with this version of Event Espresso core.', |
|
| 342 | - 'event_espresso' |
|
| 343 | - ), |
|
| 344 | - $addon_name, |
|
| 345 | - $min_version_required |
|
| 346 | - ), |
|
| 347 | - __FILE__, |
|
| 348 | - __FUNCTION__ . "({$addon_name})", |
|
| 349 | - __LINE__ |
|
| 350 | - ); |
|
| 351 | - EE_Error::get_notices(false, true); |
|
| 352 | - } |
|
| 353 | - } |
|
| 354 | - |
|
| 355 | - |
|
| 356 | - /** |
|
| 357 | - * load_espresso_addons |
|
| 358 | - * allow addons to load first so that they can set hooks for running DMS's, etc |
|
| 359 | - * this is hooked into both: |
|
| 360 | - * 'AHEE__EE_Bootstrap__load_core_configuration' |
|
| 361 | - * which runs during the WP 'plugins_loaded' action at priority 5 |
|
| 362 | - * and the WP 'activate_plugin' hook point |
|
| 363 | - * |
|
| 364 | - * @access public |
|
| 365 | - * @return void |
|
| 366 | - */ |
|
| 367 | - public function load_espresso_addons() |
|
| 368 | - { |
|
| 369 | - $this->deactivateIncompatibleAddon( |
|
| 370 | - 'Wait Lists', |
|
| 371 | - 'EE_WAIT_LISTS_VERSION', |
|
| 372 | - '1.0.0.beta.074', |
|
| 373 | - 'load_espresso_wait_lists', |
|
| 374 | - 'EE_WAIT_LISTS_PLUGIN_FILE' |
|
| 375 | - ); |
|
| 376 | - $this->deactivateIncompatibleAddon( |
|
| 377 | - 'Automated Upcoming Event Notifications', |
|
| 378 | - 'EE_AUTOMATED_UPCOMING_EVENT_NOTIFICATION_VERSION', |
|
| 379 | - '1.0.0.beta.091', |
|
| 380 | - 'load_espresso_automated_upcoming_event_notification', |
|
| 381 | - 'EE_AUTOMATED_UPCOMING_EVENT_NOTIFICATION_PLUGIN_FILE' |
|
| 382 | - ); |
|
| 383 | - do_action('AHEE__EE_System__load_espresso_addons'); |
|
| 384 | - // if the WP API basic auth plugin isn't already loaded, load it now. |
|
| 385 | - // We want it for mobile apps. Just include the entire plugin |
|
| 386 | - // also, don't load the basic auth when a plugin is getting activated, because |
|
| 387 | - // it could be the basic auth plugin, and it doesn't check if its methods are already defined |
|
| 388 | - // and causes a fatal error |
|
| 389 | - if ($this->request->getRequestParam('activate') !== 'true' |
|
| 390 | - && ! function_exists('json_basic_auth_handler') |
|
| 391 | - && ! function_exists('json_basic_auth_error') |
|
| 392 | - && ! in_array( |
|
| 393 | - $this->request->getRequestParam('action'), |
|
| 394 | - array('activate', 'activate-selected'), |
|
| 395 | - true |
|
| 396 | - ) |
|
| 397 | - ) { |
|
| 398 | - include_once EE_THIRD_PARTY . 'wp-api-basic-auth' . DS . 'basic-auth.php'; |
|
| 399 | - } |
|
| 400 | - do_action('AHEE__EE_System__load_espresso_addons__complete'); |
|
| 401 | - } |
|
| 402 | - |
|
| 403 | - |
|
| 404 | - /** |
|
| 405 | - * detect_activations_or_upgrades |
|
| 406 | - * Checks for activation or upgrade of core first; |
|
| 407 | - * then also checks if any registered addons have been activated or upgraded |
|
| 408 | - * This is hooked into 'AHEE__EE_Bootstrap__detect_activations_or_upgrades' |
|
| 409 | - * which runs during the WP 'plugins_loaded' action at priority 3 |
|
| 410 | - * |
|
| 411 | - * @access public |
|
| 412 | - * @return void |
|
| 413 | - */ |
|
| 414 | - public function detect_activations_or_upgrades() |
|
| 415 | - { |
|
| 416 | - // first off: let's make sure to handle core |
|
| 417 | - $this->detect_if_activation_or_upgrade(); |
|
| 418 | - foreach ($this->registry->addons as $addon) { |
|
| 419 | - if ($addon instanceof EE_Addon) { |
|
| 420 | - // detect teh request type for that addon |
|
| 421 | - $addon->detect_activation_or_upgrade(); |
|
| 422 | - } |
|
| 423 | - } |
|
| 424 | - } |
|
| 425 | - |
|
| 426 | - |
|
| 427 | - /** |
|
| 428 | - * detect_if_activation_or_upgrade |
|
| 429 | - * Takes care of detecting whether this is a brand new install or code upgrade, |
|
| 430 | - * and either setting up the DB or setting up maintenance mode etc. |
|
| 431 | - * |
|
| 432 | - * @access public |
|
| 433 | - * @return void |
|
| 434 | - */ |
|
| 435 | - public function detect_if_activation_or_upgrade() |
|
| 436 | - { |
|
| 437 | - do_action('AHEE__EE_System___detect_if_activation_or_upgrade__begin'); |
|
| 438 | - // check if db has been updated, or if its a brand-new installation |
|
| 439 | - $espresso_db_update = $this->fix_espresso_db_upgrade_option(); |
|
| 440 | - $request_type = $this->detect_req_type($espresso_db_update); |
|
| 441 | - // EEH_Debug_Tools::printr( $request_type, '$request_type', __FILE__, __LINE__ ); |
|
| 442 | - switch ($request_type) { |
|
| 443 | - case EE_System::req_type_new_activation: |
|
| 444 | - do_action('AHEE__EE_System__detect_if_activation_or_upgrade__new_activation'); |
|
| 445 | - $this->_handle_core_version_change($espresso_db_update); |
|
| 446 | - break; |
|
| 447 | - case EE_System::req_type_reactivation: |
|
| 448 | - do_action('AHEE__EE_System__detect_if_activation_or_upgrade__reactivation'); |
|
| 449 | - $this->_handle_core_version_change($espresso_db_update); |
|
| 450 | - break; |
|
| 451 | - case EE_System::req_type_upgrade: |
|
| 452 | - do_action('AHEE__EE_System__detect_if_activation_or_upgrade__upgrade'); |
|
| 453 | - // migrations may be required now that we've upgraded |
|
| 454 | - $this->maintenance_mode->set_maintenance_mode_if_db_old(); |
|
| 455 | - $this->_handle_core_version_change($espresso_db_update); |
|
| 456 | - break; |
|
| 457 | - case EE_System::req_type_downgrade: |
|
| 458 | - do_action('AHEE__EE_System__detect_if_activation_or_upgrade__downgrade'); |
|
| 459 | - // its possible migrations are no longer required |
|
| 460 | - $this->maintenance_mode->set_maintenance_mode_if_db_old(); |
|
| 461 | - $this->_handle_core_version_change($espresso_db_update); |
|
| 462 | - break; |
|
| 463 | - case EE_System::req_type_normal: |
|
| 464 | - default: |
|
| 465 | - break; |
|
| 466 | - } |
|
| 467 | - do_action('AHEE__EE_System__detect_if_activation_or_upgrade__complete'); |
|
| 468 | - } |
|
| 469 | - |
|
| 470 | - |
|
| 471 | - /** |
|
| 472 | - * Updates the list of installed versions and sets hooks for |
|
| 473 | - * initializing the database later during the request |
|
| 474 | - * |
|
| 475 | - * @param array $espresso_db_update |
|
| 476 | - */ |
|
| 477 | - private function _handle_core_version_change($espresso_db_update) |
|
| 478 | - { |
|
| 479 | - $this->update_list_of_installed_versions($espresso_db_update); |
|
| 480 | - // get ready to verify the DB is ok (provided we aren't in maintenance mode, of course) |
|
| 481 | - add_action( |
|
| 482 | - 'AHEE__EE_System__perform_activations_upgrades_and_migrations', |
|
| 483 | - array($this, 'initialize_db_if_no_migrations_required') |
|
| 484 | - ); |
|
| 485 | - } |
|
| 486 | - |
|
| 487 | - |
|
| 488 | - /** |
|
| 489 | - * standardizes the wp option 'espresso_db_upgrade' which actually stores |
|
| 490 | - * information about what versions of EE have been installed and activated, |
|
| 491 | - * NOT necessarily the state of the database |
|
| 492 | - * |
|
| 493 | - * @param mixed $espresso_db_update the value of the WordPress option. |
|
| 494 | - * If not supplied, fetches it from the options table |
|
| 495 | - * @return array the correct value of 'espresso_db_upgrade', after saving it, if it needed correction |
|
| 496 | - */ |
|
| 497 | - private function fix_espresso_db_upgrade_option($espresso_db_update = null) |
|
| 498 | - { |
|
| 499 | - do_action('FHEE__EE_System__manage_fix_espresso_db_upgrade_option__begin', $espresso_db_update); |
|
| 500 | - if (! $espresso_db_update) { |
|
| 501 | - $espresso_db_update = get_option('espresso_db_update'); |
|
| 502 | - } |
|
| 503 | - // check that option is an array |
|
| 504 | - if (! is_array($espresso_db_update)) { |
|
| 505 | - // if option is FALSE, then it never existed |
|
| 506 | - if ($espresso_db_update === false) { |
|
| 507 | - // make $espresso_db_update an array and save option with autoload OFF |
|
| 508 | - $espresso_db_update = array(); |
|
| 509 | - add_option('espresso_db_update', $espresso_db_update, '', 'no'); |
|
| 510 | - } else { |
|
| 511 | - // option is NOT FALSE but also is NOT an array, so make it an array and save it |
|
| 512 | - $espresso_db_update = array($espresso_db_update => array()); |
|
| 513 | - update_option('espresso_db_update', $espresso_db_update); |
|
| 514 | - } |
|
| 515 | - } else { |
|
| 516 | - $corrected_db_update = array(); |
|
| 517 | - // if IS an array, but is it an array where KEYS are version numbers, and values are arrays? |
|
| 518 | - foreach ($espresso_db_update as $should_be_version_string => $should_be_array) { |
|
| 519 | - if (is_int($should_be_version_string) && ! is_array($should_be_array)) { |
|
| 520 | - // the key is an int, and the value IS NOT an array |
|
| 521 | - // so it must be numerically-indexed, where values are versions installed... |
|
| 522 | - // fix it! |
|
| 523 | - $version_string = $should_be_array; |
|
| 524 | - $corrected_db_update[ $version_string ] = array('unknown-date'); |
|
| 525 | - } else { |
|
| 526 | - // ok it checks out |
|
| 527 | - $corrected_db_update[ $should_be_version_string ] = $should_be_array; |
|
| 528 | - } |
|
| 529 | - } |
|
| 530 | - $espresso_db_update = $corrected_db_update; |
|
| 531 | - update_option('espresso_db_update', $espresso_db_update); |
|
| 532 | - } |
|
| 533 | - do_action('FHEE__EE_System__manage_fix_espresso_db_upgrade_option__complete', $espresso_db_update); |
|
| 534 | - return $espresso_db_update; |
|
| 535 | - } |
|
| 536 | - |
|
| 537 | - |
|
| 538 | - /** |
|
| 539 | - * Does the traditional work of setting up the plugin's database and adding default data. |
|
| 540 | - * If migration script/process did not exist, this is what would happen on every activation/reactivation/upgrade. |
|
| 541 | - * NOTE: if we're in maintenance mode (which would be the case if we detect there are data |
|
| 542 | - * migration scripts that need to be run and a version change happens), enqueues core for database initialization, |
|
| 543 | - * so that it will be done when migrations are finished |
|
| 544 | - * |
|
| 545 | - * @param boolean $initialize_addons_too if true, we double-check addons' database tables etc too; |
|
| 546 | - * @param boolean $verify_schema if true will re-check the database tables have the correct schema. |
|
| 547 | - * This is a resource-intensive job |
|
| 548 | - * so we prefer to only do it when necessary |
|
| 549 | - * @return void |
|
| 550 | - * @throws EE_Error |
|
| 551 | - */ |
|
| 552 | - public function initialize_db_if_no_migrations_required($initialize_addons_too = false, $verify_schema = true) |
|
| 553 | - { |
|
| 554 | - $request_type = $this->detect_req_type(); |
|
| 555 | - // only initialize system if we're not in maintenance mode. |
|
| 556 | - if ($this->maintenance_mode->level() !== EE_Maintenance_Mode::level_2_complete_maintenance) { |
|
| 557 | - /** @var EventEspresso\core\domain\services\custom_post_types\RewriteRules $rewrite_rules */ |
|
| 558 | - $rewrite_rules = $this->loader->getShared( |
|
| 559 | - 'EventEspresso\core\domain\services\custom_post_types\RewriteRules' |
|
| 560 | - ); |
|
| 561 | - $rewrite_rules->flush(); |
|
| 562 | - if ($verify_schema) { |
|
| 563 | - EEH_Activation::initialize_db_and_folders(); |
|
| 564 | - } |
|
| 565 | - EEH_Activation::initialize_db_content(); |
|
| 566 | - EEH_Activation::system_initialization(); |
|
| 567 | - if ($initialize_addons_too) { |
|
| 568 | - $this->initialize_addons(); |
|
| 569 | - } |
|
| 570 | - } else { |
|
| 571 | - EE_Data_Migration_Manager::instance()->enqueue_db_initialization_for('Core'); |
|
| 572 | - } |
|
| 573 | - if ($request_type === EE_System::req_type_new_activation |
|
| 574 | - || $request_type === EE_System::req_type_reactivation |
|
| 575 | - || ( |
|
| 576 | - $request_type === EE_System::req_type_upgrade |
|
| 577 | - && $this->is_major_version_change() |
|
| 578 | - ) |
|
| 579 | - ) { |
|
| 580 | - add_action('AHEE__EE_System__initialize_last', array($this, 'redirect_to_about_ee'), 9); |
|
| 581 | - } |
|
| 582 | - } |
|
| 583 | - |
|
| 584 | - |
|
| 585 | - /** |
|
| 586 | - * Initializes the db for all registered addons |
|
| 587 | - * |
|
| 588 | - * @throws EE_Error |
|
| 589 | - */ |
|
| 590 | - public function initialize_addons() |
|
| 591 | - { |
|
| 592 | - // foreach registered addon, make sure its db is up-to-date too |
|
| 593 | - foreach ($this->registry->addons as $addon) { |
|
| 594 | - if ($addon instanceof EE_Addon) { |
|
| 595 | - $addon->initialize_db_if_no_migrations_required(); |
|
| 596 | - } |
|
| 597 | - } |
|
| 598 | - } |
|
| 599 | - |
|
| 600 | - |
|
| 601 | - /** |
|
| 602 | - * Adds the current code version to the saved wp option which stores a list of all ee versions ever installed. |
|
| 603 | - * |
|
| 604 | - * @param array $version_history |
|
| 605 | - * @param string $current_version_to_add version to be added to the version history |
|
| 606 | - * @return boolean success as to whether or not this option was changed |
|
| 607 | - */ |
|
| 608 | - public function update_list_of_installed_versions($version_history = null, $current_version_to_add = null) |
|
| 609 | - { |
|
| 610 | - if (! $version_history) { |
|
| 611 | - $version_history = $this->fix_espresso_db_upgrade_option($version_history); |
|
| 612 | - } |
|
| 613 | - if ($current_version_to_add === null) { |
|
| 614 | - $current_version_to_add = espresso_version(); |
|
| 615 | - } |
|
| 616 | - $version_history[ $current_version_to_add ][] = date('Y-m-d H:i:s', time()); |
|
| 617 | - // re-save |
|
| 618 | - return update_option('espresso_db_update', $version_history); |
|
| 619 | - } |
|
| 620 | - |
|
| 621 | - |
|
| 622 | - /** |
|
| 623 | - * Detects if the current version indicated in the has existed in the list of |
|
| 624 | - * previously-installed versions of EE (espresso_db_update). Does NOT modify it (ie, no side-effect) |
|
| 625 | - * |
|
| 626 | - * @param array $espresso_db_update array from the wp option stored under the name 'espresso_db_update'. |
|
| 627 | - * If not supplied, fetches it from the options table. |
|
| 628 | - * Also, caches its result so later parts of the code can also know whether |
|
| 629 | - * there's been an update or not. This way we can add the current version to |
|
| 630 | - * espresso_db_update, but still know if this is a new install or not |
|
| 631 | - * @return int one of the constants on EE_System::req_type_ |
|
| 632 | - */ |
|
| 633 | - public function detect_req_type($espresso_db_update = null) |
|
| 634 | - { |
|
| 635 | - if ($this->_req_type === null) { |
|
| 636 | - $espresso_db_update = ! empty($espresso_db_update) |
|
| 637 | - ? $espresso_db_update |
|
| 638 | - : $this->fix_espresso_db_upgrade_option(); |
|
| 639 | - $this->_req_type = EE_System::detect_req_type_given_activation_history( |
|
| 640 | - $espresso_db_update, |
|
| 641 | - 'ee_espresso_activation', |
|
| 642 | - espresso_version() |
|
| 643 | - ); |
|
| 644 | - $this->_major_version_change = $this->_detect_major_version_change($espresso_db_update); |
|
| 645 | - $this->request->setIsActivation($this->_req_type !== EE_System::req_type_normal); |
|
| 646 | - } |
|
| 647 | - return $this->_req_type; |
|
| 648 | - } |
|
| 649 | - |
|
| 650 | - |
|
| 651 | - /** |
|
| 652 | - * Returns whether or not there was a non-micro version change (ie, change in either |
|
| 653 | - * the first or second number in the version. Eg 4.9.0.rc.001 to 4.10.0.rc.000, |
|
| 654 | - * but not 4.9.0.rc.0001 to 4.9.1.rc.0001 |
|
| 655 | - * |
|
| 656 | - * @param $activation_history |
|
| 657 | - * @return bool |
|
| 658 | - */ |
|
| 659 | - private function _detect_major_version_change($activation_history) |
|
| 660 | - { |
|
| 661 | - $previous_version = EE_System::_get_most_recently_active_version_from_activation_history($activation_history); |
|
| 662 | - $previous_version_parts = explode('.', $previous_version); |
|
| 663 | - $current_version_parts = explode('.', espresso_version()); |
|
| 664 | - return isset($previous_version_parts[0], $previous_version_parts[1], $current_version_parts[0], $current_version_parts[1]) |
|
| 665 | - && ($previous_version_parts[0] !== $current_version_parts[0] |
|
| 666 | - || $previous_version_parts[1] !== $current_version_parts[1] |
|
| 667 | - ); |
|
| 668 | - } |
|
| 669 | - |
|
| 670 | - |
|
| 671 | - /** |
|
| 672 | - * Returns true if either the major or minor version of EE changed during this request. |
|
| 673 | - * Eg 4.9.0.rc.001 to 4.10.0.rc.000, but not 4.9.0.rc.0001 to 4.9.1.rc.0001 |
|
| 674 | - * |
|
| 675 | - * @return bool |
|
| 676 | - */ |
|
| 677 | - public function is_major_version_change() |
|
| 678 | - { |
|
| 679 | - return $this->_major_version_change; |
|
| 680 | - } |
|
| 681 | - |
|
| 682 | - |
|
| 683 | - /** |
|
| 684 | - * Determines the request type for any ee addon, given three piece of info: the current array of activation |
|
| 685 | - * histories (for core that' 'espresso_db_update' wp option); the name of the WordPress option which is temporarily |
|
| 686 | - * set upon activation of the plugin (for core it's 'ee_espresso_activation'); and the version that this plugin was |
|
| 687 | - * just activated to (for core that will always be espresso_version()) |
|
| 688 | - * |
|
| 689 | - * @param array $activation_history_for_addon the option's value which stores the activation history for this |
|
| 690 | - * ee plugin. for core that's 'espresso_db_update' |
|
| 691 | - * @param string $activation_indicator_option_name the name of the WordPress option that is temporarily set to |
|
| 692 | - * indicate that this plugin was just activated |
|
| 693 | - * @param string $version_to_upgrade_to the version that was just upgraded to (for core that will be |
|
| 694 | - * espresso_version()) |
|
| 695 | - * @return int one of the constants on EE_System::req_type_* |
|
| 696 | - */ |
|
| 697 | - public static function detect_req_type_given_activation_history( |
|
| 698 | - $activation_history_for_addon, |
|
| 699 | - $activation_indicator_option_name, |
|
| 700 | - $version_to_upgrade_to |
|
| 701 | - ) { |
|
| 702 | - $version_is_higher = self::_new_version_is_higher($activation_history_for_addon, $version_to_upgrade_to); |
|
| 703 | - if ($activation_history_for_addon) { |
|
| 704 | - // it exists, so this isn't a completely new install |
|
| 705 | - // check if this version already in that list of previously installed versions |
|
| 706 | - if (! isset($activation_history_for_addon[ $version_to_upgrade_to ])) { |
|
| 707 | - // it a version we haven't seen before |
|
| 708 | - if ($version_is_higher === 1) { |
|
| 709 | - $req_type = EE_System::req_type_upgrade; |
|
| 710 | - } else { |
|
| 711 | - $req_type = EE_System::req_type_downgrade; |
|
| 712 | - } |
|
| 713 | - delete_option($activation_indicator_option_name); |
|
| 714 | - } else { |
|
| 715 | - // its not an update. maybe a reactivation? |
|
| 716 | - if (get_option($activation_indicator_option_name, false)) { |
|
| 717 | - if ($version_is_higher === -1) { |
|
| 718 | - $req_type = EE_System::req_type_downgrade; |
|
| 719 | - } elseif ($version_is_higher === 0) { |
|
| 720 | - // we've seen this version before, but it's an activation. must be a reactivation |
|
| 721 | - $req_type = EE_System::req_type_reactivation; |
|
| 722 | - } else {// $version_is_higher === 1 |
|
| 723 | - $req_type = EE_System::req_type_upgrade; |
|
| 724 | - } |
|
| 725 | - delete_option($activation_indicator_option_name); |
|
| 726 | - } else { |
|
| 727 | - // we've seen this version before and the activation indicate doesn't show it was just activated |
|
| 728 | - if ($version_is_higher === -1) { |
|
| 729 | - $req_type = EE_System::req_type_downgrade; |
|
| 730 | - } elseif ($version_is_higher === 0) { |
|
| 731 | - // we've seen this version before and it's not an activation. its normal request |
|
| 732 | - $req_type = EE_System::req_type_normal; |
|
| 733 | - } else {// $version_is_higher === 1 |
|
| 734 | - $req_type = EE_System::req_type_upgrade; |
|
| 735 | - } |
|
| 736 | - } |
|
| 737 | - } |
|
| 738 | - } else { |
|
| 739 | - // brand new install |
|
| 740 | - $req_type = EE_System::req_type_new_activation; |
|
| 741 | - delete_option($activation_indicator_option_name); |
|
| 742 | - } |
|
| 743 | - return $req_type; |
|
| 744 | - } |
|
| 745 | - |
|
| 746 | - |
|
| 747 | - /** |
|
| 748 | - * Detects if the $version_to_upgrade_to is higher than the most recent version in |
|
| 749 | - * the $activation_history_for_addon |
|
| 750 | - * |
|
| 751 | - * @param array $activation_history_for_addon (keys are versions, values are arrays of times activated, |
|
| 752 | - * sometimes containing 'unknown-date' |
|
| 753 | - * @param string $version_to_upgrade_to (current version) |
|
| 754 | - * @return int results of version_compare( $version_to_upgrade_to, $most_recently_active_version ). |
|
| 755 | - * ie, -1 if $version_to_upgrade_to is LOWER (downgrade); |
|
| 756 | - * 0 if $version_to_upgrade_to MATCHES (reactivation or normal request); |
|
| 757 | - * 1 if $version_to_upgrade_to is HIGHER (upgrade) ; |
|
| 758 | - */ |
|
| 759 | - private static function _new_version_is_higher($activation_history_for_addon, $version_to_upgrade_to) |
|
| 760 | - { |
|
| 761 | - // find the most recently-activated version |
|
| 762 | - $most_recently_active_version = |
|
| 763 | - EE_System::_get_most_recently_active_version_from_activation_history($activation_history_for_addon); |
|
| 764 | - return version_compare($version_to_upgrade_to, $most_recently_active_version); |
|
| 765 | - } |
|
| 766 | - |
|
| 767 | - |
|
| 768 | - /** |
|
| 769 | - * Gets the most recently active version listed in the activation history, |
|
| 770 | - * and if none are found (ie, it's a brand new install) returns '0.0.0.dev.000'. |
|
| 771 | - * |
|
| 772 | - * @param array $activation_history (keys are versions, values are arrays of times activated, |
|
| 773 | - * sometimes containing 'unknown-date' |
|
| 774 | - * @return string |
|
| 775 | - */ |
|
| 776 | - private static function _get_most_recently_active_version_from_activation_history($activation_history) |
|
| 777 | - { |
|
| 778 | - $most_recently_active_version_activation = '1970-01-01 00:00:00'; |
|
| 779 | - $most_recently_active_version = '0.0.0.dev.000'; |
|
| 780 | - if (is_array($activation_history)) { |
|
| 781 | - foreach ($activation_history as $version => $times_activated) { |
|
| 782 | - // check there is a record of when this version was activated. Otherwise, |
|
| 783 | - // mark it as unknown |
|
| 784 | - if (! $times_activated) { |
|
| 785 | - $times_activated = array('unknown-date'); |
|
| 786 | - } |
|
| 787 | - if (is_string($times_activated)) { |
|
| 788 | - $times_activated = array($times_activated); |
|
| 789 | - } |
|
| 790 | - foreach ($times_activated as $an_activation) { |
|
| 791 | - if ($an_activation !== 'unknown-date' |
|
| 792 | - && $an_activation |
|
| 793 | - > $most_recently_active_version_activation) { |
|
| 794 | - $most_recently_active_version = $version; |
|
| 795 | - $most_recently_active_version_activation = $an_activation === 'unknown-date' |
|
| 796 | - ? '1970-01-01 00:00:00' |
|
| 797 | - : $an_activation; |
|
| 798 | - } |
|
| 799 | - } |
|
| 800 | - } |
|
| 801 | - } |
|
| 802 | - return $most_recently_active_version; |
|
| 803 | - } |
|
| 804 | - |
|
| 805 | - |
|
| 806 | - /** |
|
| 807 | - * This redirects to the about EE page after activation |
|
| 808 | - * |
|
| 809 | - * @return void |
|
| 810 | - */ |
|
| 811 | - public function redirect_to_about_ee() |
|
| 812 | - { |
|
| 813 | - $notices = EE_Error::get_notices(false); |
|
| 814 | - // if current user is an admin and it's not an ajax or rest request |
|
| 815 | - if (! isset($notices['errors']) |
|
| 816 | - && $this->request->isAdmin() |
|
| 817 | - && apply_filters( |
|
| 818 | - 'FHEE__EE_System__redirect_to_about_ee__do_redirect', |
|
| 819 | - $this->capabilities->current_user_can('manage_options', 'espresso_about_default') |
|
| 820 | - ) |
|
| 821 | - ) { |
|
| 822 | - $query_params = array('page' => 'espresso_about'); |
|
| 823 | - if (EE_System::instance()->detect_req_type() === EE_System::req_type_new_activation) { |
|
| 824 | - $query_params['new_activation'] = true; |
|
| 825 | - } |
|
| 826 | - if (EE_System::instance()->detect_req_type() === EE_System::req_type_reactivation) { |
|
| 827 | - $query_params['reactivation'] = true; |
|
| 828 | - } |
|
| 829 | - $url = add_query_arg($query_params, admin_url('admin.php')); |
|
| 830 | - wp_safe_redirect($url); |
|
| 831 | - exit(); |
|
| 832 | - } |
|
| 833 | - } |
|
| 834 | - |
|
| 835 | - |
|
| 836 | - /** |
|
| 837 | - * load_core_configuration |
|
| 838 | - * this is hooked into 'AHEE__EE_Bootstrap__load_core_configuration' |
|
| 839 | - * which runs during the WP 'plugins_loaded' action at priority 5 |
|
| 840 | - * |
|
| 841 | - * @return void |
|
| 842 | - * @throws ReflectionException |
|
| 843 | - * @throws Exception |
|
| 844 | - */ |
|
| 845 | - public function load_core_configuration() |
|
| 846 | - { |
|
| 847 | - do_action('AHEE__EE_System__load_core_configuration__begin', $this); |
|
| 848 | - $this->loader->getShared('EE_Load_Textdomain'); |
|
| 849 | - // load textdomain |
|
| 850 | - EE_Load_Textdomain::load_textdomain(); |
|
| 851 | - // load and setup EE_Config and EE_Network_Config |
|
| 852 | - $config = $this->loader->getShared('EE_Config'); |
|
| 853 | - $this->loader->getShared('EE_Network_Config'); |
|
| 854 | - // setup autoloaders |
|
| 855 | - // enable logging? |
|
| 856 | - if ($config->admin->use_full_logging) { |
|
| 857 | - $this->loader->getShared('EE_Log'); |
|
| 858 | - } |
|
| 859 | - // check for activation errors |
|
| 860 | - $activation_errors = get_option('ee_plugin_activation_errors', false); |
|
| 861 | - if ($activation_errors) { |
|
| 862 | - EE_Error::add_error($activation_errors, __FILE__, __FUNCTION__, __LINE__); |
|
| 863 | - update_option('ee_plugin_activation_errors', false); |
|
| 864 | - } |
|
| 865 | - // get model names |
|
| 866 | - $this->_parse_model_names(); |
|
| 867 | - // load caf stuff a chance to play during the activation process too. |
|
| 868 | - $this->_maybe_brew_regular(); |
|
| 869 | - // configure custom post type definitions |
|
| 870 | - $this->loader->getShared('EventEspresso\core\domain\entities\custom_post_types\CustomTaxonomyDefinitions'); |
|
| 871 | - $this->loader->getShared('EventEspresso\core\domain\entities\custom_post_types\CustomPostTypeDefinitions'); |
|
| 872 | - do_action('AHEE__EE_System__load_core_configuration__complete', $this); |
|
| 873 | - } |
|
| 874 | - |
|
| 875 | - |
|
| 876 | - /** |
|
| 877 | - * cycles through all of the models/*.model.php files, and assembles an array of model names |
|
| 878 | - * |
|
| 879 | - * @return void |
|
| 880 | - * @throws ReflectionException |
|
| 881 | - */ |
|
| 882 | - private function _parse_model_names() |
|
| 883 | - { |
|
| 884 | - // get all the files in the EE_MODELS folder that end in .model.php |
|
| 885 | - $models = glob(EE_MODELS . '*.model.php'); |
|
| 886 | - $model_names = array(); |
|
| 887 | - $non_abstract_db_models = array(); |
|
| 888 | - foreach ($models as $model) { |
|
| 889 | - // get model classname |
|
| 890 | - $classname = EEH_File::get_classname_from_filepath_with_standard_filename($model); |
|
| 891 | - $short_name = str_replace('EEM_', '', $classname); |
|
| 892 | - $reflectionClass = new ReflectionClass($classname); |
|
| 893 | - if ($reflectionClass->isSubclassOf('EEM_Base') && ! $reflectionClass->isAbstract()) { |
|
| 894 | - $non_abstract_db_models[ $short_name ] = $classname; |
|
| 895 | - } |
|
| 896 | - $model_names[ $short_name ] = $classname; |
|
| 897 | - } |
|
| 898 | - $this->registry->models = apply_filters('FHEE__EE_System__parse_model_names', $model_names); |
|
| 899 | - $this->registry->non_abstract_db_models = apply_filters( |
|
| 900 | - 'FHEE__EE_System__parse_implemented_model_names', |
|
| 901 | - $non_abstract_db_models |
|
| 902 | - ); |
|
| 903 | - } |
|
| 904 | - |
|
| 905 | - |
|
| 906 | - /** |
|
| 907 | - * The purpose of this method is to simply check for a file named "caffeinated/brewing_regular.php" for any hooks |
|
| 908 | - * that need to be setup before our EE_System launches. |
|
| 909 | - * |
|
| 910 | - * @return void |
|
| 911 | - * @throws DomainException |
|
| 912 | - * @throws InvalidArgumentException |
|
| 913 | - * @throws InvalidDataTypeException |
|
| 914 | - * @throws InvalidInterfaceException |
|
| 915 | - * @throws InvalidClassException |
|
| 916 | - * @throws InvalidFilePathException |
|
| 917 | - */ |
|
| 918 | - private function _maybe_brew_regular() |
|
| 919 | - { |
|
| 920 | - /** @var Domain $domain */ |
|
| 921 | - $domain = DomainFactory::getShared( |
|
| 922 | - new FullyQualifiedName( |
|
| 923 | - 'EventEspresso\core\domain\Domain' |
|
| 924 | - ), |
|
| 925 | - array( |
|
| 926 | - new FilePath(EVENT_ESPRESSO_MAIN_FILE), |
|
| 927 | - Version::fromString(espresso_version()), |
|
| 928 | - ) |
|
| 929 | - ); |
|
| 930 | - if ($domain->isCaffeinated()) { |
|
| 931 | - require_once EE_CAFF_PATH . 'brewing_regular.php'; |
|
| 932 | - } |
|
| 933 | - } |
|
| 934 | - |
|
| 935 | - |
|
| 936 | - /** |
|
| 937 | - * @since 4.9.71.p |
|
| 938 | - * @throws Exception |
|
| 939 | - */ |
|
| 940 | - public function loadRouteMatchSpecifications() |
|
| 941 | - { |
|
| 942 | - try { |
|
| 943 | - $this->loader->getShared( |
|
| 944 | - 'EventEspresso\core\services\route_match\RouteMatchSpecificationManager' |
|
| 945 | - ); |
|
| 946 | - } catch (Exception $exception) { |
|
| 947 | - new ExceptionStackTraceDisplay($exception); |
|
| 948 | - } |
|
| 949 | - do_action('AHEE__EE_System__loadRouteMatchSpecifications'); |
|
| 950 | - } |
|
| 951 | - |
|
| 952 | - |
|
| 953 | - /** |
|
| 954 | - * register_shortcodes_modules_and_widgets |
|
| 955 | - * generate lists of shortcodes and modules, then verify paths and classes |
|
| 956 | - * This is hooked into 'AHEE__EE_Bootstrap__register_shortcodes_modules_and_widgets' |
|
| 957 | - * which runs during the WP 'plugins_loaded' action at priority 7 |
|
| 958 | - * |
|
| 959 | - * @access public |
|
| 960 | - * @return void |
|
| 961 | - * @throws Exception |
|
| 962 | - */ |
|
| 963 | - public function register_shortcodes_modules_and_widgets() |
|
| 964 | - { |
|
| 965 | - if ($this->request->isFrontend() || $this->request->isIframe() || $this->request->isAjax()) { |
|
| 966 | - try { |
|
| 967 | - // load, register, and add shortcodes the new way |
|
| 968 | - $this->loader->getShared( |
|
| 969 | - 'EventEspresso\core\services\shortcodes\ShortcodesManager', |
|
| 970 | - array( |
|
| 971 | - // and the old way, but we'll put it under control of the new system |
|
| 972 | - EE_Config::getLegacyShortcodesManager(), |
|
| 973 | - ) |
|
| 974 | - ); |
|
| 975 | - } catch (Exception $exception) { |
|
| 976 | - new ExceptionStackTraceDisplay($exception); |
|
| 977 | - } |
|
| 978 | - } |
|
| 979 | - do_action('AHEE__EE_System__register_shortcodes_modules_and_widgets'); |
|
| 980 | - // check for addons using old hook point |
|
| 981 | - if (has_action('AHEE__EE_System__register_shortcodes_modules_and_addons')) { |
|
| 982 | - $this->_incompatible_addon_error(); |
|
| 983 | - } |
|
| 984 | - } |
|
| 985 | - |
|
| 986 | - |
|
| 987 | - /** |
|
| 988 | - * _incompatible_addon_error |
|
| 989 | - * |
|
| 990 | - * @access public |
|
| 991 | - * @return void |
|
| 992 | - */ |
|
| 993 | - private function _incompatible_addon_error() |
|
| 994 | - { |
|
| 995 | - // get array of classes hooking into here |
|
| 996 | - $class_names = EEH_Class_Tools::get_class_names_for_all_callbacks_on_hook( |
|
| 997 | - 'AHEE__EE_System__register_shortcodes_modules_and_addons' |
|
| 998 | - ); |
|
| 999 | - if (! empty($class_names)) { |
|
| 1000 | - $msg = __( |
|
| 1001 | - 'The following plugins, addons, or modules appear to be incompatible with this version of Event Espresso and were automatically deactivated to avoid fatal errors:', |
|
| 1002 | - 'event_espresso' |
|
| 1003 | - ); |
|
| 1004 | - $msg .= '<ul>'; |
|
| 1005 | - foreach ($class_names as $class_name) { |
|
| 1006 | - $msg .= '<li><b>Event Espresso - ' |
|
| 1007 | - . str_replace( |
|
| 1008 | - array('EE_', 'EEM_', 'EED_', 'EES_', 'EEW_'), |
|
| 1009 | - '', |
|
| 1010 | - $class_name |
|
| 1011 | - ) . '</b></li>'; |
|
| 1012 | - } |
|
| 1013 | - $msg .= '</ul>'; |
|
| 1014 | - $msg .= __( |
|
| 1015 | - 'Compatibility issues can be avoided and/or resolved by keeping addons and plugins updated to the latest version.', |
|
| 1016 | - 'event_espresso' |
|
| 1017 | - ); |
|
| 1018 | - // save list of incompatible addons to wp-options for later use |
|
| 1019 | - add_option('ee_incompatible_addons', $class_names, '', 'no'); |
|
| 1020 | - if (is_admin()) { |
|
| 1021 | - EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__); |
|
| 1022 | - } |
|
| 1023 | - } |
|
| 1024 | - } |
|
| 1025 | - |
|
| 1026 | - |
|
| 1027 | - /** |
|
| 1028 | - * brew_espresso |
|
| 1029 | - * begins the process of setting hooks for initializing EE in the correct order |
|
| 1030 | - * This is happening on the 'AHEE__EE_Bootstrap__brew_espresso' hook point |
|
| 1031 | - * which runs during the WP 'plugins_loaded' action at priority 9 |
|
| 1032 | - * |
|
| 1033 | - * @return void |
|
| 1034 | - */ |
|
| 1035 | - public function brew_espresso() |
|
| 1036 | - { |
|
| 1037 | - do_action('AHEE__EE_System__brew_espresso__begin', $this); |
|
| 1038 | - // load some final core systems |
|
| 1039 | - add_action('init', array($this, 'set_hooks_for_core'), 1); |
|
| 1040 | - add_action('init', array($this, 'perform_activations_upgrades_and_migrations'), 3); |
|
| 1041 | - add_action('init', array($this, 'load_CPTs_and_session'), 5); |
|
| 1042 | - add_action('init', array($this, 'load_controllers'), 7); |
|
| 1043 | - add_action('init', array($this, 'core_loaded_and_ready'), 9); |
|
| 1044 | - add_action('init', array($this, 'initialize'), 10); |
|
| 1045 | - add_action('init', array($this, 'initialize_last'), 100); |
|
| 1046 | - if (is_admin() && apply_filters('FHEE__EE_System__brew_espresso__load_pue', true)) { |
|
| 1047 | - // pew pew pew |
|
| 1048 | - $this->loader->getShared('EventEspresso\core\services\licensing\LicenseService'); |
|
| 1049 | - do_action('AHEE__EE_System__brew_espresso__after_pue_init'); |
|
| 1050 | - } |
|
| 1051 | - do_action('AHEE__EE_System__brew_espresso__complete', $this); |
|
| 1052 | - } |
|
| 1053 | - |
|
| 1054 | - |
|
| 1055 | - /** |
|
| 1056 | - * set_hooks_for_core |
|
| 1057 | - * |
|
| 1058 | - * @access public |
|
| 1059 | - * @return void |
|
| 1060 | - * @throws EE_Error |
|
| 1061 | - */ |
|
| 1062 | - public function set_hooks_for_core() |
|
| 1063 | - { |
|
| 1064 | - $this->_deactivate_incompatible_addons(); |
|
| 1065 | - do_action('AHEE__EE_System__set_hooks_for_core'); |
|
| 1066 | - $this->loader->getShared('EventEspresso\core\domain\values\session\SessionLifespan'); |
|
| 1067 | - // caps need to be initialized on every request so that capability maps are set. |
|
| 1068 | - // @see https://events.codebasehq.com/projects/event-espresso/tickets/8674 |
|
| 1069 | - $this->registry->CAP->init_caps(); |
|
| 1070 | - } |
|
| 1071 | - |
|
| 1072 | - |
|
| 1073 | - /** |
|
| 1074 | - * Using the information gathered in EE_System::_incompatible_addon_error, |
|
| 1075 | - * deactivates any addons considered incompatible with the current version of EE |
|
| 1076 | - */ |
|
| 1077 | - private function _deactivate_incompatible_addons() |
|
| 1078 | - { |
|
| 1079 | - $incompatible_addons = get_option('ee_incompatible_addons', array()); |
|
| 1080 | - if (! empty($incompatible_addons)) { |
|
| 1081 | - $active_plugins = get_option('active_plugins', array()); |
|
| 1082 | - foreach ($active_plugins as $active_plugin) { |
|
| 1083 | - foreach ($incompatible_addons as $incompatible_addon) { |
|
| 1084 | - if (strpos($active_plugin, $incompatible_addon) !== false) { |
|
| 1085 | - unset($_GET['activate']); |
|
| 1086 | - espresso_deactivate_plugin($active_plugin); |
|
| 1087 | - } |
|
| 1088 | - } |
|
| 1089 | - } |
|
| 1090 | - } |
|
| 1091 | - } |
|
| 1092 | - |
|
| 1093 | - |
|
| 1094 | - /** |
|
| 1095 | - * perform_activations_upgrades_and_migrations |
|
| 1096 | - * |
|
| 1097 | - * @access public |
|
| 1098 | - * @return void |
|
| 1099 | - */ |
|
| 1100 | - public function perform_activations_upgrades_and_migrations() |
|
| 1101 | - { |
|
| 1102 | - do_action('AHEE__EE_System__perform_activations_upgrades_and_migrations'); |
|
| 1103 | - } |
|
| 1104 | - |
|
| 1105 | - |
|
| 1106 | - /** |
|
| 1107 | - * @return void |
|
| 1108 | - * @throws DomainException |
|
| 1109 | - */ |
|
| 1110 | - public function load_CPTs_and_session() |
|
| 1111 | - { |
|
| 1112 | - do_action('AHEE__EE_System__load_CPTs_and_session__start'); |
|
| 1113 | - /** @var EventEspresso\core\domain\services\custom_post_types\RegisterCustomTaxonomies $register_custom_taxonomies */ |
|
| 1114 | - $register_custom_taxonomies = $this->loader->getShared( |
|
| 1115 | - 'EventEspresso\core\domain\services\custom_post_types\RegisterCustomTaxonomies' |
|
| 1116 | - ); |
|
| 1117 | - $register_custom_taxonomies->registerCustomTaxonomies(); |
|
| 1118 | - /** @var EventEspresso\core\domain\services\custom_post_types\RegisterCustomPostTypes $register_custom_post_types */ |
|
| 1119 | - $register_custom_post_types = $this->loader->getShared( |
|
| 1120 | - 'EventEspresso\core\domain\services\custom_post_types\RegisterCustomPostTypes' |
|
| 1121 | - ); |
|
| 1122 | - $register_custom_post_types->registerCustomPostTypes(); |
|
| 1123 | - /** @var EventEspresso\core\domain\services\custom_post_types\RegisterCustomTaxonomyTerms $register_custom_taxonomy_terms */ |
|
| 1124 | - $register_custom_taxonomy_terms = $this->loader->getShared( |
|
| 1125 | - 'EventEspresso\core\domain\services\custom_post_types\RegisterCustomTaxonomyTerms' |
|
| 1126 | - ); |
|
| 1127 | - $register_custom_taxonomy_terms->registerCustomTaxonomyTerms(); |
|
| 1128 | - // load legacy Custom Post Types and Taxonomies |
|
| 1129 | - $this->loader->getShared('EE_Register_CPTs'); |
|
| 1130 | - do_action('AHEE__EE_System__load_CPTs_and_session__complete'); |
|
| 1131 | - } |
|
| 1132 | - |
|
| 1133 | - |
|
| 1134 | - /** |
|
| 1135 | - * load_controllers |
|
| 1136 | - * this is the best place to load any additional controllers that needs access to EE core. |
|
| 1137 | - * it is expected that all basic core EE systems, that are not dependant on the current request are loaded at this |
|
| 1138 | - * time |
|
| 1139 | - * |
|
| 1140 | - * @access public |
|
| 1141 | - * @return void |
|
| 1142 | - */ |
|
| 1143 | - public function load_controllers() |
|
| 1144 | - { |
|
| 1145 | - do_action('AHEE__EE_System__load_controllers__start'); |
|
| 1146 | - // let's get it started |
|
| 1147 | - if (! $this->maintenance_mode->level() |
|
| 1148 | - && ($this->request->isFrontend() || $this->request->isFrontAjax()) |
|
| 1149 | - ) { |
|
| 1150 | - do_action('AHEE__EE_System__load_controllers__load_front_controllers'); |
|
| 1151 | - $this->loader->getShared('EE_Front_Controller'); |
|
| 1152 | - } elseif ($this->request->isAdmin() || $this->request->isAdminAjax()) { |
|
| 1153 | - do_action('AHEE__EE_System__load_controllers__load_admin_controllers'); |
|
| 1154 | - $this->loader->getShared('EE_Admin'); |
|
| 1155 | - } elseif ($this->request->isWordPressHeartbeat()) { |
|
| 1156 | - $this->loader->getShared('EventEspresso\core\domain\services\admin\ajax\WordpressHeartbeat'); |
|
| 1157 | - } |
|
| 1158 | - do_action('AHEE__EE_System__load_controllers__complete'); |
|
| 1159 | - } |
|
| 1160 | - |
|
| 1161 | - |
|
| 1162 | - /** |
|
| 1163 | - * core_loaded_and_ready |
|
| 1164 | - * all of the basic EE core should be loaded at this point and available regardless of M-Mode |
|
| 1165 | - * |
|
| 1166 | - * @access public |
|
| 1167 | - * @return void |
|
| 1168 | - * @throws Exception |
|
| 1169 | - */ |
|
| 1170 | - public function core_loaded_and_ready() |
|
| 1171 | - { |
|
| 1172 | - if ($this->request->isAdmin() |
|
| 1173 | - || $this->request->isFrontend() |
|
| 1174 | - || $this->request->isIframe() |
|
| 1175 | - || $this->request->isWordPressApi() |
|
| 1176 | - ) { |
|
| 1177 | - try { |
|
| 1178 | - $this->loader->getShared('EventEspresso\core\services\assets\Registry'); |
|
| 1179 | - $this->loader->getShared('EventEspresso\core\domain\services\assets\CoreAssetManager'); |
|
| 1180 | - if ($this->canLoadBlocks()) { |
|
| 1181 | - $this->loader->getShared( |
|
| 1182 | - 'EventEspresso\core\services\editor\BlockRegistrationManager' |
|
| 1183 | - ); |
|
| 1184 | - } |
|
| 1185 | - } catch (Exception $exception) { |
|
| 1186 | - new ExceptionStackTraceDisplay($exception); |
|
| 1187 | - } |
|
| 1188 | - } |
|
| 1189 | - if ($this->request->isAdmin() |
|
| 1190 | - || $this->request->isEeAjax() |
|
| 1191 | - || $this->request->isFrontend() |
|
| 1192 | - ) { |
|
| 1193 | - $this->loader->getShared('EE_Session'); |
|
| 1194 | - } |
|
| 1195 | - // integrate WP_Query with the EE models |
|
| 1196 | - $this->loader->getShared('EE_CPT_Strategy'); |
|
| 1197 | - do_action('AHEE__EE_System__core_loaded_and_ready'); |
|
| 1198 | - // always load template tags, because it's faster than checking if it's a front-end request, and many page |
|
| 1199 | - // builders require these even on the front-end |
|
| 1200 | - require_once EE_PUBLIC . 'template_tags.php'; |
|
| 1201 | - do_action('AHEE__EE_System__set_hooks_for_shortcodes_modules_and_addons'); |
|
| 1202 | - } |
|
| 1203 | - |
|
| 1204 | - |
|
| 1205 | - /** |
|
| 1206 | - * initialize |
|
| 1207 | - * this is the best place to begin initializing client code |
|
| 1208 | - * |
|
| 1209 | - * @access public |
|
| 1210 | - * @return void |
|
| 1211 | - */ |
|
| 1212 | - public function initialize() |
|
| 1213 | - { |
|
| 1214 | - do_action('AHEE__EE_System__initialize'); |
|
| 1215 | - } |
|
| 1216 | - |
|
| 1217 | - |
|
| 1218 | - /** |
|
| 1219 | - * initialize_last |
|
| 1220 | - * this is run really late during the WP init hook point, and ensures that mostly everything else that needs to |
|
| 1221 | - * initialize has done so |
|
| 1222 | - * |
|
| 1223 | - * @access public |
|
| 1224 | - * @return void |
|
| 1225 | - */ |
|
| 1226 | - public function initialize_last() |
|
| 1227 | - { |
|
| 1228 | - do_action('AHEE__EE_System__initialize_last'); |
|
| 1229 | - /** @var EventEspresso\core\domain\services\custom_post_types\RewriteRules $rewrite_rules */ |
|
| 1230 | - $rewrite_rules = $this->loader->getShared( |
|
| 1231 | - 'EventEspresso\core\domain\services\custom_post_types\RewriteRules' |
|
| 1232 | - ); |
|
| 1233 | - $rewrite_rules->flushRewriteRules(); |
|
| 1234 | - add_action('admin_bar_init', array($this, 'addEspressoToolbar')); |
|
| 1235 | - if (($this->request->isAjax() || $this->request->isAdmin()) |
|
| 1236 | - && $this->maintenance_mode->models_can_query()) { |
|
| 1237 | - $this->loader->getShared('EventEspresso\core\services\privacy\export\PersonalDataExporterManager'); |
|
| 1238 | - $this->loader->getShared('EventEspresso\core\services\privacy\erasure\PersonalDataEraserManager'); |
|
| 1239 | - } |
|
| 1240 | - } |
|
| 1241 | - |
|
| 1242 | - |
|
| 1243 | - /** |
|
| 1244 | - * @return void |
|
| 1245 | - * @throws EE_Error |
|
| 1246 | - */ |
|
| 1247 | - public function addEspressoToolbar() |
|
| 1248 | - { |
|
| 1249 | - $this->loader->getShared( |
|
| 1250 | - 'EventEspresso\core\domain\services\admin\AdminToolBar', |
|
| 1251 | - array($this->registry->CAP) |
|
| 1252 | - ); |
|
| 1253 | - } |
|
| 1254 | - |
|
| 1255 | - |
|
| 1256 | - /** |
|
| 1257 | - * do_not_cache |
|
| 1258 | - * sets no cache headers and defines no cache constants for WP plugins |
|
| 1259 | - * |
|
| 1260 | - * @access public |
|
| 1261 | - * @return void |
|
| 1262 | - */ |
|
| 1263 | - public static function do_not_cache() |
|
| 1264 | - { |
|
| 1265 | - // set no cache constants |
|
| 1266 | - if (! defined('DONOTCACHEPAGE')) { |
|
| 1267 | - define('DONOTCACHEPAGE', true); |
|
| 1268 | - } |
|
| 1269 | - if (! defined('DONOTCACHCEOBJECT')) { |
|
| 1270 | - define('DONOTCACHCEOBJECT', true); |
|
| 1271 | - } |
|
| 1272 | - if (! defined('DONOTCACHEDB')) { |
|
| 1273 | - define('DONOTCACHEDB', true); |
|
| 1274 | - } |
|
| 1275 | - // add no cache headers |
|
| 1276 | - add_action('send_headers', array('EE_System', 'nocache_headers'), 10); |
|
| 1277 | - // plus a little extra for nginx and Google Chrome |
|
| 1278 | - add_filter('nocache_headers', array('EE_System', 'extra_nocache_headers'), 10, 1); |
|
| 1279 | - // prevent browsers from prefetching of the rel='next' link, because it may contain content that interferes with the registration process |
|
| 1280 | - remove_action('wp_head', 'adjacent_posts_rel_link_wp_head'); |
|
| 1281 | - } |
|
| 1282 | - |
|
| 1283 | - |
|
| 1284 | - /** |
|
| 1285 | - * extra_nocache_headers |
|
| 1286 | - * |
|
| 1287 | - * @access public |
|
| 1288 | - * @param $headers |
|
| 1289 | - * @return array |
|
| 1290 | - */ |
|
| 1291 | - public static function extra_nocache_headers($headers) |
|
| 1292 | - { |
|
| 1293 | - // for NGINX |
|
| 1294 | - $headers['X-Accel-Expires'] = 0; |
|
| 1295 | - // plus extra for Google Chrome since it doesn't seem to respect "no-cache", but WILL respect "no-store" |
|
| 1296 | - $headers['Cache-Control'] = 'no-store, no-cache, must-revalidate, max-age=0'; |
|
| 1297 | - return $headers; |
|
| 1298 | - } |
|
| 1299 | - |
|
| 1300 | - |
|
| 1301 | - /** |
|
| 1302 | - * nocache_headers |
|
| 1303 | - * |
|
| 1304 | - * @access public |
|
| 1305 | - * @return void |
|
| 1306 | - */ |
|
| 1307 | - public static function nocache_headers() |
|
| 1308 | - { |
|
| 1309 | - nocache_headers(); |
|
| 1310 | - } |
|
| 1311 | - |
|
| 1312 | - |
|
| 1313 | - /** |
|
| 1314 | - * simply hooks into "wp_list_pages_exclude" filter (for wp_list_pages method) and makes sure EE critical pages are |
|
| 1315 | - * never returned with the function. |
|
| 1316 | - * |
|
| 1317 | - * @param array $exclude_array any existing pages being excluded are in this array. |
|
| 1318 | - * @return array |
|
| 1319 | - */ |
|
| 1320 | - public function remove_pages_from_wp_list_pages($exclude_array) |
|
| 1321 | - { |
|
| 1322 | - return array_merge($exclude_array, $this->registry->CFG->core->get_critical_pages_array()); |
|
| 1323 | - } |
|
| 1324 | - |
|
| 1325 | - |
|
| 1326 | - /** |
|
| 1327 | - * Return whether blocks can be registered/loaded or not. |
|
| 1328 | - * @return bool |
|
| 1329 | - */ |
|
| 1330 | - private function canLoadBlocks() |
|
| 1331 | - { |
|
| 1332 | - return apply_filters('FHEE__EE_System__canLoadBlocks', true) |
|
| 1333 | - && function_exists('register_block_type') |
|
| 1334 | - // don't load blocks if in the Divi page builder editor context |
|
| 1335 | - // @see https://github.com/eventespresso/event-espresso-core/issues/814 |
|
| 1336 | - && ! $this->request->getRequestParam('et_fb', false); |
|
| 1337 | - } |
|
| 30 | + /** |
|
| 31 | + * indicates this is a 'normal' request. Ie, not activation, nor upgrade, nor activation. |
|
| 32 | + * So examples of this would be a normal GET request on the frontend or backend, or a POST, etc |
|
| 33 | + */ |
|
| 34 | + const req_type_normal = 0; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * Indicates this is a brand new installation of EE so we should install |
|
| 38 | + * tables and default data etc |
|
| 39 | + */ |
|
| 40 | + const req_type_new_activation = 1; |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * we've detected that EE has been reactivated (or EE was activated during maintenance mode, |
|
| 44 | + * and we just exited maintenance mode). We MUST check the database is setup properly |
|
| 45 | + * and that default data is setup too |
|
| 46 | + */ |
|
| 47 | + const req_type_reactivation = 2; |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * indicates that EE has been upgraded since its previous request. |
|
| 51 | + * We may have data migration scripts to call and will want to trigger maintenance mode |
|
| 52 | + */ |
|
| 53 | + const req_type_upgrade = 3; |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * TODO will detect that EE has been DOWNGRADED. We probably don't want to run in this case... |
|
| 57 | + */ |
|
| 58 | + const req_type_downgrade = 4; |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * @deprecated since version 4.6.0.dev.006 |
|
| 62 | + * Now whenever a new_activation is detected the request type is still just |
|
| 63 | + * new_activation (same for reactivation, upgrade, downgrade etc), but if we'r ein maintenance mode |
|
| 64 | + * EE_System::initialize_db_if_no_migrations_required and EE_Addon::initialize_db_if_no_migrations_required |
|
| 65 | + * will instead enqueue that EE plugin's db initialization for when we're taken out of maintenance mode. |
|
| 66 | + * (Specifically, when the migration manager indicates migrations are finished |
|
| 67 | + * EE_Data_Migration_Manager::initialize_db_for_enqueued_ee_plugins() will be called) |
|
| 68 | + */ |
|
| 69 | + const req_type_activation_but_not_installed = 5; |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * option prefix for recording the activation history (like core's "espresso_db_update") of addons |
|
| 73 | + */ |
|
| 74 | + const addon_activation_history_option_prefix = 'ee_addon_activation_history_'; |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * @var EE_System $_instance |
|
| 78 | + */ |
|
| 79 | + private static $_instance; |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * @var EE_Registry $registry |
|
| 83 | + */ |
|
| 84 | + private $registry; |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * @var LoaderInterface $loader |
|
| 88 | + */ |
|
| 89 | + private $loader; |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * @var EE_Capabilities $capabilities |
|
| 93 | + */ |
|
| 94 | + private $capabilities; |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * @var RequestInterface $request |
|
| 98 | + */ |
|
| 99 | + private $request; |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * @var EE_Maintenance_Mode $maintenance_mode |
|
| 103 | + */ |
|
| 104 | + private $maintenance_mode; |
|
| 105 | + |
|
| 106 | + /** |
|
| 107 | + * Stores which type of request this is, options being one of the constants on EE_System starting with req_type_*. |
|
| 108 | + * It can be a brand-new activation, a reactivation, an upgrade, a downgrade, or a normal request. |
|
| 109 | + * |
|
| 110 | + * @var int $_req_type |
|
| 111 | + */ |
|
| 112 | + private $_req_type; |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * Whether or not there was a non-micro version change in EE core version during this request |
|
| 116 | + * |
|
| 117 | + * @var boolean $_major_version_change |
|
| 118 | + */ |
|
| 119 | + private $_major_version_change = false; |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * A Context DTO dedicated solely to identifying the current request type. |
|
| 123 | + * |
|
| 124 | + * @var RequestTypeContextCheckerInterface $request_type |
|
| 125 | + */ |
|
| 126 | + private $request_type; |
|
| 127 | + |
|
| 128 | + |
|
| 129 | + /** |
|
| 130 | + * @singleton method used to instantiate class object |
|
| 131 | + * @param EE_Registry|null $registry |
|
| 132 | + * @param LoaderInterface|null $loader |
|
| 133 | + * @param RequestInterface|null $request |
|
| 134 | + * @param EE_Maintenance_Mode|null $maintenance_mode |
|
| 135 | + * @return EE_System |
|
| 136 | + */ |
|
| 137 | + public static function instance( |
|
| 138 | + EE_Registry $registry = null, |
|
| 139 | + LoaderInterface $loader = null, |
|
| 140 | + RequestInterface $request = null, |
|
| 141 | + EE_Maintenance_Mode $maintenance_mode = null |
|
| 142 | + ) { |
|
| 143 | + // check if class object is instantiated |
|
| 144 | + if (! self::$_instance instanceof EE_System) { |
|
| 145 | + self::$_instance = new self($registry, $loader, $request, $maintenance_mode); |
|
| 146 | + } |
|
| 147 | + return self::$_instance; |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + |
|
| 151 | + /** |
|
| 152 | + * resets the instance and returns it |
|
| 153 | + * |
|
| 154 | + * @return EE_System |
|
| 155 | + */ |
|
| 156 | + public static function reset() |
|
| 157 | + { |
|
| 158 | + self::$_instance->_req_type = null; |
|
| 159 | + // make sure none of the old hooks are left hanging around |
|
| 160 | + remove_all_actions('AHEE__EE_System__perform_activations_upgrades_and_migrations'); |
|
| 161 | + // we need to reset the migration manager in order for it to detect DMSs properly |
|
| 162 | + EE_Data_Migration_Manager::reset(); |
|
| 163 | + self::instance()->detect_activations_or_upgrades(); |
|
| 164 | + self::instance()->perform_activations_upgrades_and_migrations(); |
|
| 165 | + return self::instance(); |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + |
|
| 169 | + /** |
|
| 170 | + * sets hooks for running rest of system |
|
| 171 | + * provides "AHEE__EE_System__construct__complete" hook for EE Addons to use as their starting point |
|
| 172 | + * starting EE Addons from any other point may lead to problems |
|
| 173 | + * |
|
| 174 | + * @param EE_Registry $registry |
|
| 175 | + * @param LoaderInterface $loader |
|
| 176 | + * @param RequestInterface $request |
|
| 177 | + * @param EE_Maintenance_Mode $maintenance_mode |
|
| 178 | + */ |
|
| 179 | + private function __construct( |
|
| 180 | + EE_Registry $registry, |
|
| 181 | + LoaderInterface $loader, |
|
| 182 | + RequestInterface $request, |
|
| 183 | + EE_Maintenance_Mode $maintenance_mode |
|
| 184 | + ) { |
|
| 185 | + $this->registry = $registry; |
|
| 186 | + $this->loader = $loader; |
|
| 187 | + $this->request = $request; |
|
| 188 | + $this->maintenance_mode = $maintenance_mode; |
|
| 189 | + do_action('AHEE__EE_System__construct__begin', $this); |
|
| 190 | + add_action( |
|
| 191 | + 'AHEE__EE_Bootstrap__load_espresso_addons', |
|
| 192 | + array($this, 'loadCapabilities'), |
|
| 193 | + 5 |
|
| 194 | + ); |
|
| 195 | + add_action( |
|
| 196 | + 'AHEE__EE_Bootstrap__load_espresso_addons', |
|
| 197 | + array($this, 'loadCommandBus'), |
|
| 198 | + 7 |
|
| 199 | + ); |
|
| 200 | + add_action( |
|
| 201 | + 'AHEE__EE_Bootstrap__load_espresso_addons', |
|
| 202 | + array($this, 'loadPluginApi'), |
|
| 203 | + 9 |
|
| 204 | + ); |
|
| 205 | + // allow addons to load first so that they can register autoloaders, set hooks for running DMS's, etc |
|
| 206 | + add_action( |
|
| 207 | + 'AHEE__EE_Bootstrap__load_espresso_addons', |
|
| 208 | + array($this, 'load_espresso_addons') |
|
| 209 | + ); |
|
| 210 | + // when an ee addon is activated, we want to call the core hook(s) again |
|
| 211 | + // because the newly-activated addon didn't get a chance to run at all |
|
| 212 | + add_action('activate_plugin', array($this, 'load_espresso_addons'), 1); |
|
| 213 | + // detect whether install or upgrade |
|
| 214 | + add_action( |
|
| 215 | + 'AHEE__EE_Bootstrap__detect_activations_or_upgrades', |
|
| 216 | + array($this, 'detect_activations_or_upgrades'), |
|
| 217 | + 3 |
|
| 218 | + ); |
|
| 219 | + // load EE_Config, EE_Textdomain, etc |
|
| 220 | + add_action( |
|
| 221 | + 'AHEE__EE_Bootstrap__load_core_configuration', |
|
| 222 | + array($this, 'load_core_configuration'), |
|
| 223 | + 5 |
|
| 224 | + ); |
|
| 225 | + // load specifications for matching routes to current request |
|
| 226 | + add_action( |
|
| 227 | + 'AHEE__EE_Bootstrap__load_core_configuration', |
|
| 228 | + array($this, 'loadRouteMatchSpecifications') |
|
| 229 | + ); |
|
| 230 | + // load EE_Config, EE_Textdomain, etc |
|
| 231 | + add_action( |
|
| 232 | + 'AHEE__EE_Bootstrap__register_shortcodes_modules_and_widgets', |
|
| 233 | + array($this, 'register_shortcodes_modules_and_widgets'), |
|
| 234 | + 7 |
|
| 235 | + ); |
|
| 236 | + // you wanna get going? I wanna get going... let's get going! |
|
| 237 | + add_action( |
|
| 238 | + 'AHEE__EE_Bootstrap__brew_espresso', |
|
| 239 | + array($this, 'brew_espresso'), |
|
| 240 | + 9 |
|
| 241 | + ); |
|
| 242 | + // other housekeeping |
|
| 243 | + // exclude EE critical pages from wp_list_pages |
|
| 244 | + add_filter( |
|
| 245 | + 'wp_list_pages_excludes', |
|
| 246 | + array($this, 'remove_pages_from_wp_list_pages'), |
|
| 247 | + 10 |
|
| 248 | + ); |
|
| 249 | + // ALL EE Addons should use the following hook point to attach their initial setup too |
|
| 250 | + // it's extremely important for EE Addons to register any class autoloaders so that they can be available when the EE_Config loads |
|
| 251 | + do_action('AHEE__EE_System__construct__complete', $this); |
|
| 252 | + } |
|
| 253 | + |
|
| 254 | + |
|
| 255 | + /** |
|
| 256 | + * load and setup EE_Capabilities |
|
| 257 | + * |
|
| 258 | + * @return void |
|
| 259 | + * @throws EE_Error |
|
| 260 | + */ |
|
| 261 | + public function loadCapabilities() |
|
| 262 | + { |
|
| 263 | + $this->capabilities = $this->loader->getShared('EE_Capabilities'); |
|
| 264 | + add_action( |
|
| 265 | + 'AHEE__EE_Capabilities__init_caps__before_initialization', |
|
| 266 | + function () { |
|
| 267 | + LoaderFactory::getLoader()->getShared('EE_Payment_Method_Manager'); |
|
| 268 | + } |
|
| 269 | + ); |
|
| 270 | + } |
|
| 271 | + |
|
| 272 | + |
|
| 273 | + /** |
|
| 274 | + * create and cache the CommandBus, and also add middleware |
|
| 275 | + * The CapChecker middleware requires the use of EE_Capabilities |
|
| 276 | + * which is why we need to load the CommandBus after Caps are set up |
|
| 277 | + * |
|
| 278 | + * @return void |
|
| 279 | + * @throws EE_Error |
|
| 280 | + */ |
|
| 281 | + public function loadCommandBus() |
|
| 282 | + { |
|
| 283 | + $this->loader->getShared( |
|
| 284 | + 'CommandBusInterface', |
|
| 285 | + array( |
|
| 286 | + null, |
|
| 287 | + apply_filters( |
|
| 288 | + 'FHEE__EE_Load_Espresso_Core__handle_request__CommandBus_middleware', |
|
| 289 | + array( |
|
| 290 | + $this->loader->getShared('EventEspresso\core\services\commands\middleware\CapChecker'), |
|
| 291 | + $this->loader->getShared('EventEspresso\core\services\commands\middleware\AddActionHook'), |
|
| 292 | + ) |
|
| 293 | + ), |
|
| 294 | + ) |
|
| 295 | + ); |
|
| 296 | + } |
|
| 297 | + |
|
| 298 | + |
|
| 299 | + /** |
|
| 300 | + * @return void |
|
| 301 | + * @throws EE_Error |
|
| 302 | + */ |
|
| 303 | + public function loadPluginApi() |
|
| 304 | + { |
|
| 305 | + // set autoloaders for all of the classes implementing EEI_Plugin_API |
|
| 306 | + // which provide helpers for EE plugin authors to more easily register certain components with EE. |
|
| 307 | + EEH_Autoloader::instance()->register_autoloaders_for_each_file_in_folder(EE_LIBRARIES . 'plugin_api'); |
|
| 308 | + $this->loader->getShared('EE_Request_Handler'); |
|
| 309 | + } |
|
| 310 | + |
|
| 311 | + |
|
| 312 | + /** |
|
| 313 | + * @param string $addon_name |
|
| 314 | + * @param string $version_constant |
|
| 315 | + * @param string $min_version_required |
|
| 316 | + * @param string $load_callback |
|
| 317 | + * @param string $plugin_file_constant |
|
| 318 | + * @return void |
|
| 319 | + */ |
|
| 320 | + private function deactivateIncompatibleAddon( |
|
| 321 | + $addon_name, |
|
| 322 | + $version_constant, |
|
| 323 | + $min_version_required, |
|
| 324 | + $load_callback, |
|
| 325 | + $plugin_file_constant |
|
| 326 | + ) { |
|
| 327 | + if (! defined($version_constant)) { |
|
| 328 | + return; |
|
| 329 | + } |
|
| 330 | + $addon_version = constant($version_constant); |
|
| 331 | + if ($addon_version && version_compare($addon_version, $min_version_required, '<')) { |
|
| 332 | + remove_action('AHEE__EE_System__load_espresso_addons', $load_callback); |
|
| 333 | + if (! function_exists('deactivate_plugins')) { |
|
| 334 | + require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
|
| 335 | + } |
|
| 336 | + deactivate_plugins(plugin_basename(constant($plugin_file_constant))); |
|
| 337 | + unset($_GET['activate'], $_REQUEST['activate'], $_GET['activate-multi'], $_REQUEST['activate-multi']); |
|
| 338 | + EE_Error::add_error( |
|
| 339 | + sprintf( |
|
| 340 | + esc_html__( |
|
| 341 | + 'We\'re sorry, but the Event Espresso %1$s addon was deactivated because version %2$s or higher is required with this version of Event Espresso core.', |
|
| 342 | + 'event_espresso' |
|
| 343 | + ), |
|
| 344 | + $addon_name, |
|
| 345 | + $min_version_required |
|
| 346 | + ), |
|
| 347 | + __FILE__, |
|
| 348 | + __FUNCTION__ . "({$addon_name})", |
|
| 349 | + __LINE__ |
|
| 350 | + ); |
|
| 351 | + EE_Error::get_notices(false, true); |
|
| 352 | + } |
|
| 353 | + } |
|
| 354 | + |
|
| 355 | + |
|
| 356 | + /** |
|
| 357 | + * load_espresso_addons |
|
| 358 | + * allow addons to load first so that they can set hooks for running DMS's, etc |
|
| 359 | + * this is hooked into both: |
|
| 360 | + * 'AHEE__EE_Bootstrap__load_core_configuration' |
|
| 361 | + * which runs during the WP 'plugins_loaded' action at priority 5 |
|
| 362 | + * and the WP 'activate_plugin' hook point |
|
| 363 | + * |
|
| 364 | + * @access public |
|
| 365 | + * @return void |
|
| 366 | + */ |
|
| 367 | + public function load_espresso_addons() |
|
| 368 | + { |
|
| 369 | + $this->deactivateIncompatibleAddon( |
|
| 370 | + 'Wait Lists', |
|
| 371 | + 'EE_WAIT_LISTS_VERSION', |
|
| 372 | + '1.0.0.beta.074', |
|
| 373 | + 'load_espresso_wait_lists', |
|
| 374 | + 'EE_WAIT_LISTS_PLUGIN_FILE' |
|
| 375 | + ); |
|
| 376 | + $this->deactivateIncompatibleAddon( |
|
| 377 | + 'Automated Upcoming Event Notifications', |
|
| 378 | + 'EE_AUTOMATED_UPCOMING_EVENT_NOTIFICATION_VERSION', |
|
| 379 | + '1.0.0.beta.091', |
|
| 380 | + 'load_espresso_automated_upcoming_event_notification', |
|
| 381 | + 'EE_AUTOMATED_UPCOMING_EVENT_NOTIFICATION_PLUGIN_FILE' |
|
| 382 | + ); |
|
| 383 | + do_action('AHEE__EE_System__load_espresso_addons'); |
|
| 384 | + // if the WP API basic auth plugin isn't already loaded, load it now. |
|
| 385 | + // We want it for mobile apps. Just include the entire plugin |
|
| 386 | + // also, don't load the basic auth when a plugin is getting activated, because |
|
| 387 | + // it could be the basic auth plugin, and it doesn't check if its methods are already defined |
|
| 388 | + // and causes a fatal error |
|
| 389 | + if ($this->request->getRequestParam('activate') !== 'true' |
|
| 390 | + && ! function_exists('json_basic_auth_handler') |
|
| 391 | + && ! function_exists('json_basic_auth_error') |
|
| 392 | + && ! in_array( |
|
| 393 | + $this->request->getRequestParam('action'), |
|
| 394 | + array('activate', 'activate-selected'), |
|
| 395 | + true |
|
| 396 | + ) |
|
| 397 | + ) { |
|
| 398 | + include_once EE_THIRD_PARTY . 'wp-api-basic-auth' . DS . 'basic-auth.php'; |
|
| 399 | + } |
|
| 400 | + do_action('AHEE__EE_System__load_espresso_addons__complete'); |
|
| 401 | + } |
|
| 402 | + |
|
| 403 | + |
|
| 404 | + /** |
|
| 405 | + * detect_activations_or_upgrades |
|
| 406 | + * Checks for activation or upgrade of core first; |
|
| 407 | + * then also checks if any registered addons have been activated or upgraded |
|
| 408 | + * This is hooked into 'AHEE__EE_Bootstrap__detect_activations_or_upgrades' |
|
| 409 | + * which runs during the WP 'plugins_loaded' action at priority 3 |
|
| 410 | + * |
|
| 411 | + * @access public |
|
| 412 | + * @return void |
|
| 413 | + */ |
|
| 414 | + public function detect_activations_or_upgrades() |
|
| 415 | + { |
|
| 416 | + // first off: let's make sure to handle core |
|
| 417 | + $this->detect_if_activation_or_upgrade(); |
|
| 418 | + foreach ($this->registry->addons as $addon) { |
|
| 419 | + if ($addon instanceof EE_Addon) { |
|
| 420 | + // detect teh request type for that addon |
|
| 421 | + $addon->detect_activation_or_upgrade(); |
|
| 422 | + } |
|
| 423 | + } |
|
| 424 | + } |
|
| 425 | + |
|
| 426 | + |
|
| 427 | + /** |
|
| 428 | + * detect_if_activation_or_upgrade |
|
| 429 | + * Takes care of detecting whether this is a brand new install or code upgrade, |
|
| 430 | + * and either setting up the DB or setting up maintenance mode etc. |
|
| 431 | + * |
|
| 432 | + * @access public |
|
| 433 | + * @return void |
|
| 434 | + */ |
|
| 435 | + public function detect_if_activation_or_upgrade() |
|
| 436 | + { |
|
| 437 | + do_action('AHEE__EE_System___detect_if_activation_or_upgrade__begin'); |
|
| 438 | + // check if db has been updated, or if its a brand-new installation |
|
| 439 | + $espresso_db_update = $this->fix_espresso_db_upgrade_option(); |
|
| 440 | + $request_type = $this->detect_req_type($espresso_db_update); |
|
| 441 | + // EEH_Debug_Tools::printr( $request_type, '$request_type', __FILE__, __LINE__ ); |
|
| 442 | + switch ($request_type) { |
|
| 443 | + case EE_System::req_type_new_activation: |
|
| 444 | + do_action('AHEE__EE_System__detect_if_activation_or_upgrade__new_activation'); |
|
| 445 | + $this->_handle_core_version_change($espresso_db_update); |
|
| 446 | + break; |
|
| 447 | + case EE_System::req_type_reactivation: |
|
| 448 | + do_action('AHEE__EE_System__detect_if_activation_or_upgrade__reactivation'); |
|
| 449 | + $this->_handle_core_version_change($espresso_db_update); |
|
| 450 | + break; |
|
| 451 | + case EE_System::req_type_upgrade: |
|
| 452 | + do_action('AHEE__EE_System__detect_if_activation_or_upgrade__upgrade'); |
|
| 453 | + // migrations may be required now that we've upgraded |
|
| 454 | + $this->maintenance_mode->set_maintenance_mode_if_db_old(); |
|
| 455 | + $this->_handle_core_version_change($espresso_db_update); |
|
| 456 | + break; |
|
| 457 | + case EE_System::req_type_downgrade: |
|
| 458 | + do_action('AHEE__EE_System__detect_if_activation_or_upgrade__downgrade'); |
|
| 459 | + // its possible migrations are no longer required |
|
| 460 | + $this->maintenance_mode->set_maintenance_mode_if_db_old(); |
|
| 461 | + $this->_handle_core_version_change($espresso_db_update); |
|
| 462 | + break; |
|
| 463 | + case EE_System::req_type_normal: |
|
| 464 | + default: |
|
| 465 | + break; |
|
| 466 | + } |
|
| 467 | + do_action('AHEE__EE_System__detect_if_activation_or_upgrade__complete'); |
|
| 468 | + } |
|
| 469 | + |
|
| 470 | + |
|
| 471 | + /** |
|
| 472 | + * Updates the list of installed versions and sets hooks for |
|
| 473 | + * initializing the database later during the request |
|
| 474 | + * |
|
| 475 | + * @param array $espresso_db_update |
|
| 476 | + */ |
|
| 477 | + private function _handle_core_version_change($espresso_db_update) |
|
| 478 | + { |
|
| 479 | + $this->update_list_of_installed_versions($espresso_db_update); |
|
| 480 | + // get ready to verify the DB is ok (provided we aren't in maintenance mode, of course) |
|
| 481 | + add_action( |
|
| 482 | + 'AHEE__EE_System__perform_activations_upgrades_and_migrations', |
|
| 483 | + array($this, 'initialize_db_if_no_migrations_required') |
|
| 484 | + ); |
|
| 485 | + } |
|
| 486 | + |
|
| 487 | + |
|
| 488 | + /** |
|
| 489 | + * standardizes the wp option 'espresso_db_upgrade' which actually stores |
|
| 490 | + * information about what versions of EE have been installed and activated, |
|
| 491 | + * NOT necessarily the state of the database |
|
| 492 | + * |
|
| 493 | + * @param mixed $espresso_db_update the value of the WordPress option. |
|
| 494 | + * If not supplied, fetches it from the options table |
|
| 495 | + * @return array the correct value of 'espresso_db_upgrade', after saving it, if it needed correction |
|
| 496 | + */ |
|
| 497 | + private function fix_espresso_db_upgrade_option($espresso_db_update = null) |
|
| 498 | + { |
|
| 499 | + do_action('FHEE__EE_System__manage_fix_espresso_db_upgrade_option__begin', $espresso_db_update); |
|
| 500 | + if (! $espresso_db_update) { |
|
| 501 | + $espresso_db_update = get_option('espresso_db_update'); |
|
| 502 | + } |
|
| 503 | + // check that option is an array |
|
| 504 | + if (! is_array($espresso_db_update)) { |
|
| 505 | + // if option is FALSE, then it never existed |
|
| 506 | + if ($espresso_db_update === false) { |
|
| 507 | + // make $espresso_db_update an array and save option with autoload OFF |
|
| 508 | + $espresso_db_update = array(); |
|
| 509 | + add_option('espresso_db_update', $espresso_db_update, '', 'no'); |
|
| 510 | + } else { |
|
| 511 | + // option is NOT FALSE but also is NOT an array, so make it an array and save it |
|
| 512 | + $espresso_db_update = array($espresso_db_update => array()); |
|
| 513 | + update_option('espresso_db_update', $espresso_db_update); |
|
| 514 | + } |
|
| 515 | + } else { |
|
| 516 | + $corrected_db_update = array(); |
|
| 517 | + // if IS an array, but is it an array where KEYS are version numbers, and values are arrays? |
|
| 518 | + foreach ($espresso_db_update as $should_be_version_string => $should_be_array) { |
|
| 519 | + if (is_int($should_be_version_string) && ! is_array($should_be_array)) { |
|
| 520 | + // the key is an int, and the value IS NOT an array |
|
| 521 | + // so it must be numerically-indexed, where values are versions installed... |
|
| 522 | + // fix it! |
|
| 523 | + $version_string = $should_be_array; |
|
| 524 | + $corrected_db_update[ $version_string ] = array('unknown-date'); |
|
| 525 | + } else { |
|
| 526 | + // ok it checks out |
|
| 527 | + $corrected_db_update[ $should_be_version_string ] = $should_be_array; |
|
| 528 | + } |
|
| 529 | + } |
|
| 530 | + $espresso_db_update = $corrected_db_update; |
|
| 531 | + update_option('espresso_db_update', $espresso_db_update); |
|
| 532 | + } |
|
| 533 | + do_action('FHEE__EE_System__manage_fix_espresso_db_upgrade_option__complete', $espresso_db_update); |
|
| 534 | + return $espresso_db_update; |
|
| 535 | + } |
|
| 536 | + |
|
| 537 | + |
|
| 538 | + /** |
|
| 539 | + * Does the traditional work of setting up the plugin's database and adding default data. |
|
| 540 | + * If migration script/process did not exist, this is what would happen on every activation/reactivation/upgrade. |
|
| 541 | + * NOTE: if we're in maintenance mode (which would be the case if we detect there are data |
|
| 542 | + * migration scripts that need to be run and a version change happens), enqueues core for database initialization, |
|
| 543 | + * so that it will be done when migrations are finished |
|
| 544 | + * |
|
| 545 | + * @param boolean $initialize_addons_too if true, we double-check addons' database tables etc too; |
|
| 546 | + * @param boolean $verify_schema if true will re-check the database tables have the correct schema. |
|
| 547 | + * This is a resource-intensive job |
|
| 548 | + * so we prefer to only do it when necessary |
|
| 549 | + * @return void |
|
| 550 | + * @throws EE_Error |
|
| 551 | + */ |
|
| 552 | + public function initialize_db_if_no_migrations_required($initialize_addons_too = false, $verify_schema = true) |
|
| 553 | + { |
|
| 554 | + $request_type = $this->detect_req_type(); |
|
| 555 | + // only initialize system if we're not in maintenance mode. |
|
| 556 | + if ($this->maintenance_mode->level() !== EE_Maintenance_Mode::level_2_complete_maintenance) { |
|
| 557 | + /** @var EventEspresso\core\domain\services\custom_post_types\RewriteRules $rewrite_rules */ |
|
| 558 | + $rewrite_rules = $this->loader->getShared( |
|
| 559 | + 'EventEspresso\core\domain\services\custom_post_types\RewriteRules' |
|
| 560 | + ); |
|
| 561 | + $rewrite_rules->flush(); |
|
| 562 | + if ($verify_schema) { |
|
| 563 | + EEH_Activation::initialize_db_and_folders(); |
|
| 564 | + } |
|
| 565 | + EEH_Activation::initialize_db_content(); |
|
| 566 | + EEH_Activation::system_initialization(); |
|
| 567 | + if ($initialize_addons_too) { |
|
| 568 | + $this->initialize_addons(); |
|
| 569 | + } |
|
| 570 | + } else { |
|
| 571 | + EE_Data_Migration_Manager::instance()->enqueue_db_initialization_for('Core'); |
|
| 572 | + } |
|
| 573 | + if ($request_type === EE_System::req_type_new_activation |
|
| 574 | + || $request_type === EE_System::req_type_reactivation |
|
| 575 | + || ( |
|
| 576 | + $request_type === EE_System::req_type_upgrade |
|
| 577 | + && $this->is_major_version_change() |
|
| 578 | + ) |
|
| 579 | + ) { |
|
| 580 | + add_action('AHEE__EE_System__initialize_last', array($this, 'redirect_to_about_ee'), 9); |
|
| 581 | + } |
|
| 582 | + } |
|
| 583 | + |
|
| 584 | + |
|
| 585 | + /** |
|
| 586 | + * Initializes the db for all registered addons |
|
| 587 | + * |
|
| 588 | + * @throws EE_Error |
|
| 589 | + */ |
|
| 590 | + public function initialize_addons() |
|
| 591 | + { |
|
| 592 | + // foreach registered addon, make sure its db is up-to-date too |
|
| 593 | + foreach ($this->registry->addons as $addon) { |
|
| 594 | + if ($addon instanceof EE_Addon) { |
|
| 595 | + $addon->initialize_db_if_no_migrations_required(); |
|
| 596 | + } |
|
| 597 | + } |
|
| 598 | + } |
|
| 599 | + |
|
| 600 | + |
|
| 601 | + /** |
|
| 602 | + * Adds the current code version to the saved wp option which stores a list of all ee versions ever installed. |
|
| 603 | + * |
|
| 604 | + * @param array $version_history |
|
| 605 | + * @param string $current_version_to_add version to be added to the version history |
|
| 606 | + * @return boolean success as to whether or not this option was changed |
|
| 607 | + */ |
|
| 608 | + public function update_list_of_installed_versions($version_history = null, $current_version_to_add = null) |
|
| 609 | + { |
|
| 610 | + if (! $version_history) { |
|
| 611 | + $version_history = $this->fix_espresso_db_upgrade_option($version_history); |
|
| 612 | + } |
|
| 613 | + if ($current_version_to_add === null) { |
|
| 614 | + $current_version_to_add = espresso_version(); |
|
| 615 | + } |
|
| 616 | + $version_history[ $current_version_to_add ][] = date('Y-m-d H:i:s', time()); |
|
| 617 | + // re-save |
|
| 618 | + return update_option('espresso_db_update', $version_history); |
|
| 619 | + } |
|
| 620 | + |
|
| 621 | + |
|
| 622 | + /** |
|
| 623 | + * Detects if the current version indicated in the has existed in the list of |
|
| 624 | + * previously-installed versions of EE (espresso_db_update). Does NOT modify it (ie, no side-effect) |
|
| 625 | + * |
|
| 626 | + * @param array $espresso_db_update array from the wp option stored under the name 'espresso_db_update'. |
|
| 627 | + * If not supplied, fetches it from the options table. |
|
| 628 | + * Also, caches its result so later parts of the code can also know whether |
|
| 629 | + * there's been an update or not. This way we can add the current version to |
|
| 630 | + * espresso_db_update, but still know if this is a new install or not |
|
| 631 | + * @return int one of the constants on EE_System::req_type_ |
|
| 632 | + */ |
|
| 633 | + public function detect_req_type($espresso_db_update = null) |
|
| 634 | + { |
|
| 635 | + if ($this->_req_type === null) { |
|
| 636 | + $espresso_db_update = ! empty($espresso_db_update) |
|
| 637 | + ? $espresso_db_update |
|
| 638 | + : $this->fix_espresso_db_upgrade_option(); |
|
| 639 | + $this->_req_type = EE_System::detect_req_type_given_activation_history( |
|
| 640 | + $espresso_db_update, |
|
| 641 | + 'ee_espresso_activation', |
|
| 642 | + espresso_version() |
|
| 643 | + ); |
|
| 644 | + $this->_major_version_change = $this->_detect_major_version_change($espresso_db_update); |
|
| 645 | + $this->request->setIsActivation($this->_req_type !== EE_System::req_type_normal); |
|
| 646 | + } |
|
| 647 | + return $this->_req_type; |
|
| 648 | + } |
|
| 649 | + |
|
| 650 | + |
|
| 651 | + /** |
|
| 652 | + * Returns whether or not there was a non-micro version change (ie, change in either |
|
| 653 | + * the first or second number in the version. Eg 4.9.0.rc.001 to 4.10.0.rc.000, |
|
| 654 | + * but not 4.9.0.rc.0001 to 4.9.1.rc.0001 |
|
| 655 | + * |
|
| 656 | + * @param $activation_history |
|
| 657 | + * @return bool |
|
| 658 | + */ |
|
| 659 | + private function _detect_major_version_change($activation_history) |
|
| 660 | + { |
|
| 661 | + $previous_version = EE_System::_get_most_recently_active_version_from_activation_history($activation_history); |
|
| 662 | + $previous_version_parts = explode('.', $previous_version); |
|
| 663 | + $current_version_parts = explode('.', espresso_version()); |
|
| 664 | + return isset($previous_version_parts[0], $previous_version_parts[1], $current_version_parts[0], $current_version_parts[1]) |
|
| 665 | + && ($previous_version_parts[0] !== $current_version_parts[0] |
|
| 666 | + || $previous_version_parts[1] !== $current_version_parts[1] |
|
| 667 | + ); |
|
| 668 | + } |
|
| 669 | + |
|
| 670 | + |
|
| 671 | + /** |
|
| 672 | + * Returns true if either the major or minor version of EE changed during this request. |
|
| 673 | + * Eg 4.9.0.rc.001 to 4.10.0.rc.000, but not 4.9.0.rc.0001 to 4.9.1.rc.0001 |
|
| 674 | + * |
|
| 675 | + * @return bool |
|
| 676 | + */ |
|
| 677 | + public function is_major_version_change() |
|
| 678 | + { |
|
| 679 | + return $this->_major_version_change; |
|
| 680 | + } |
|
| 681 | + |
|
| 682 | + |
|
| 683 | + /** |
|
| 684 | + * Determines the request type for any ee addon, given three piece of info: the current array of activation |
|
| 685 | + * histories (for core that' 'espresso_db_update' wp option); the name of the WordPress option which is temporarily |
|
| 686 | + * set upon activation of the plugin (for core it's 'ee_espresso_activation'); and the version that this plugin was |
|
| 687 | + * just activated to (for core that will always be espresso_version()) |
|
| 688 | + * |
|
| 689 | + * @param array $activation_history_for_addon the option's value which stores the activation history for this |
|
| 690 | + * ee plugin. for core that's 'espresso_db_update' |
|
| 691 | + * @param string $activation_indicator_option_name the name of the WordPress option that is temporarily set to |
|
| 692 | + * indicate that this plugin was just activated |
|
| 693 | + * @param string $version_to_upgrade_to the version that was just upgraded to (for core that will be |
|
| 694 | + * espresso_version()) |
|
| 695 | + * @return int one of the constants on EE_System::req_type_* |
|
| 696 | + */ |
|
| 697 | + public static function detect_req_type_given_activation_history( |
|
| 698 | + $activation_history_for_addon, |
|
| 699 | + $activation_indicator_option_name, |
|
| 700 | + $version_to_upgrade_to |
|
| 701 | + ) { |
|
| 702 | + $version_is_higher = self::_new_version_is_higher($activation_history_for_addon, $version_to_upgrade_to); |
|
| 703 | + if ($activation_history_for_addon) { |
|
| 704 | + // it exists, so this isn't a completely new install |
|
| 705 | + // check if this version already in that list of previously installed versions |
|
| 706 | + if (! isset($activation_history_for_addon[ $version_to_upgrade_to ])) { |
|
| 707 | + // it a version we haven't seen before |
|
| 708 | + if ($version_is_higher === 1) { |
|
| 709 | + $req_type = EE_System::req_type_upgrade; |
|
| 710 | + } else { |
|
| 711 | + $req_type = EE_System::req_type_downgrade; |
|
| 712 | + } |
|
| 713 | + delete_option($activation_indicator_option_name); |
|
| 714 | + } else { |
|
| 715 | + // its not an update. maybe a reactivation? |
|
| 716 | + if (get_option($activation_indicator_option_name, false)) { |
|
| 717 | + if ($version_is_higher === -1) { |
|
| 718 | + $req_type = EE_System::req_type_downgrade; |
|
| 719 | + } elseif ($version_is_higher === 0) { |
|
| 720 | + // we've seen this version before, but it's an activation. must be a reactivation |
|
| 721 | + $req_type = EE_System::req_type_reactivation; |
|
| 722 | + } else {// $version_is_higher === 1 |
|
| 723 | + $req_type = EE_System::req_type_upgrade; |
|
| 724 | + } |
|
| 725 | + delete_option($activation_indicator_option_name); |
|
| 726 | + } else { |
|
| 727 | + // we've seen this version before and the activation indicate doesn't show it was just activated |
|
| 728 | + if ($version_is_higher === -1) { |
|
| 729 | + $req_type = EE_System::req_type_downgrade; |
|
| 730 | + } elseif ($version_is_higher === 0) { |
|
| 731 | + // we've seen this version before and it's not an activation. its normal request |
|
| 732 | + $req_type = EE_System::req_type_normal; |
|
| 733 | + } else {// $version_is_higher === 1 |
|
| 734 | + $req_type = EE_System::req_type_upgrade; |
|
| 735 | + } |
|
| 736 | + } |
|
| 737 | + } |
|
| 738 | + } else { |
|
| 739 | + // brand new install |
|
| 740 | + $req_type = EE_System::req_type_new_activation; |
|
| 741 | + delete_option($activation_indicator_option_name); |
|
| 742 | + } |
|
| 743 | + return $req_type; |
|
| 744 | + } |
|
| 745 | + |
|
| 746 | + |
|
| 747 | + /** |
|
| 748 | + * Detects if the $version_to_upgrade_to is higher than the most recent version in |
|
| 749 | + * the $activation_history_for_addon |
|
| 750 | + * |
|
| 751 | + * @param array $activation_history_for_addon (keys are versions, values are arrays of times activated, |
|
| 752 | + * sometimes containing 'unknown-date' |
|
| 753 | + * @param string $version_to_upgrade_to (current version) |
|
| 754 | + * @return int results of version_compare( $version_to_upgrade_to, $most_recently_active_version ). |
|
| 755 | + * ie, -1 if $version_to_upgrade_to is LOWER (downgrade); |
|
| 756 | + * 0 if $version_to_upgrade_to MATCHES (reactivation or normal request); |
|
| 757 | + * 1 if $version_to_upgrade_to is HIGHER (upgrade) ; |
|
| 758 | + */ |
|
| 759 | + private static function _new_version_is_higher($activation_history_for_addon, $version_to_upgrade_to) |
|
| 760 | + { |
|
| 761 | + // find the most recently-activated version |
|
| 762 | + $most_recently_active_version = |
|
| 763 | + EE_System::_get_most_recently_active_version_from_activation_history($activation_history_for_addon); |
|
| 764 | + return version_compare($version_to_upgrade_to, $most_recently_active_version); |
|
| 765 | + } |
|
| 766 | + |
|
| 767 | + |
|
| 768 | + /** |
|
| 769 | + * Gets the most recently active version listed in the activation history, |
|
| 770 | + * and if none are found (ie, it's a brand new install) returns '0.0.0.dev.000'. |
|
| 771 | + * |
|
| 772 | + * @param array $activation_history (keys are versions, values are arrays of times activated, |
|
| 773 | + * sometimes containing 'unknown-date' |
|
| 774 | + * @return string |
|
| 775 | + */ |
|
| 776 | + private static function _get_most_recently_active_version_from_activation_history($activation_history) |
|
| 777 | + { |
|
| 778 | + $most_recently_active_version_activation = '1970-01-01 00:00:00'; |
|
| 779 | + $most_recently_active_version = '0.0.0.dev.000'; |
|
| 780 | + if (is_array($activation_history)) { |
|
| 781 | + foreach ($activation_history as $version => $times_activated) { |
|
| 782 | + // check there is a record of when this version was activated. Otherwise, |
|
| 783 | + // mark it as unknown |
|
| 784 | + if (! $times_activated) { |
|
| 785 | + $times_activated = array('unknown-date'); |
|
| 786 | + } |
|
| 787 | + if (is_string($times_activated)) { |
|
| 788 | + $times_activated = array($times_activated); |
|
| 789 | + } |
|
| 790 | + foreach ($times_activated as $an_activation) { |
|
| 791 | + if ($an_activation !== 'unknown-date' |
|
| 792 | + && $an_activation |
|
| 793 | + > $most_recently_active_version_activation) { |
|
| 794 | + $most_recently_active_version = $version; |
|
| 795 | + $most_recently_active_version_activation = $an_activation === 'unknown-date' |
|
| 796 | + ? '1970-01-01 00:00:00' |
|
| 797 | + : $an_activation; |
|
| 798 | + } |
|
| 799 | + } |
|
| 800 | + } |
|
| 801 | + } |
|
| 802 | + return $most_recently_active_version; |
|
| 803 | + } |
|
| 804 | + |
|
| 805 | + |
|
| 806 | + /** |
|
| 807 | + * This redirects to the about EE page after activation |
|
| 808 | + * |
|
| 809 | + * @return void |
|
| 810 | + */ |
|
| 811 | + public function redirect_to_about_ee() |
|
| 812 | + { |
|
| 813 | + $notices = EE_Error::get_notices(false); |
|
| 814 | + // if current user is an admin and it's not an ajax or rest request |
|
| 815 | + if (! isset($notices['errors']) |
|
| 816 | + && $this->request->isAdmin() |
|
| 817 | + && apply_filters( |
|
| 818 | + 'FHEE__EE_System__redirect_to_about_ee__do_redirect', |
|
| 819 | + $this->capabilities->current_user_can('manage_options', 'espresso_about_default') |
|
| 820 | + ) |
|
| 821 | + ) { |
|
| 822 | + $query_params = array('page' => 'espresso_about'); |
|
| 823 | + if (EE_System::instance()->detect_req_type() === EE_System::req_type_new_activation) { |
|
| 824 | + $query_params['new_activation'] = true; |
|
| 825 | + } |
|
| 826 | + if (EE_System::instance()->detect_req_type() === EE_System::req_type_reactivation) { |
|
| 827 | + $query_params['reactivation'] = true; |
|
| 828 | + } |
|
| 829 | + $url = add_query_arg($query_params, admin_url('admin.php')); |
|
| 830 | + wp_safe_redirect($url); |
|
| 831 | + exit(); |
|
| 832 | + } |
|
| 833 | + } |
|
| 834 | + |
|
| 835 | + |
|
| 836 | + /** |
|
| 837 | + * load_core_configuration |
|
| 838 | + * this is hooked into 'AHEE__EE_Bootstrap__load_core_configuration' |
|
| 839 | + * which runs during the WP 'plugins_loaded' action at priority 5 |
|
| 840 | + * |
|
| 841 | + * @return void |
|
| 842 | + * @throws ReflectionException |
|
| 843 | + * @throws Exception |
|
| 844 | + */ |
|
| 845 | + public function load_core_configuration() |
|
| 846 | + { |
|
| 847 | + do_action('AHEE__EE_System__load_core_configuration__begin', $this); |
|
| 848 | + $this->loader->getShared('EE_Load_Textdomain'); |
|
| 849 | + // load textdomain |
|
| 850 | + EE_Load_Textdomain::load_textdomain(); |
|
| 851 | + // load and setup EE_Config and EE_Network_Config |
|
| 852 | + $config = $this->loader->getShared('EE_Config'); |
|
| 853 | + $this->loader->getShared('EE_Network_Config'); |
|
| 854 | + // setup autoloaders |
|
| 855 | + // enable logging? |
|
| 856 | + if ($config->admin->use_full_logging) { |
|
| 857 | + $this->loader->getShared('EE_Log'); |
|
| 858 | + } |
|
| 859 | + // check for activation errors |
|
| 860 | + $activation_errors = get_option('ee_plugin_activation_errors', false); |
|
| 861 | + if ($activation_errors) { |
|
| 862 | + EE_Error::add_error($activation_errors, __FILE__, __FUNCTION__, __LINE__); |
|
| 863 | + update_option('ee_plugin_activation_errors', false); |
|
| 864 | + } |
|
| 865 | + // get model names |
|
| 866 | + $this->_parse_model_names(); |
|
| 867 | + // load caf stuff a chance to play during the activation process too. |
|
| 868 | + $this->_maybe_brew_regular(); |
|
| 869 | + // configure custom post type definitions |
|
| 870 | + $this->loader->getShared('EventEspresso\core\domain\entities\custom_post_types\CustomTaxonomyDefinitions'); |
|
| 871 | + $this->loader->getShared('EventEspresso\core\domain\entities\custom_post_types\CustomPostTypeDefinitions'); |
|
| 872 | + do_action('AHEE__EE_System__load_core_configuration__complete', $this); |
|
| 873 | + } |
|
| 874 | + |
|
| 875 | + |
|
| 876 | + /** |
|
| 877 | + * cycles through all of the models/*.model.php files, and assembles an array of model names |
|
| 878 | + * |
|
| 879 | + * @return void |
|
| 880 | + * @throws ReflectionException |
|
| 881 | + */ |
|
| 882 | + private function _parse_model_names() |
|
| 883 | + { |
|
| 884 | + // get all the files in the EE_MODELS folder that end in .model.php |
|
| 885 | + $models = glob(EE_MODELS . '*.model.php'); |
|
| 886 | + $model_names = array(); |
|
| 887 | + $non_abstract_db_models = array(); |
|
| 888 | + foreach ($models as $model) { |
|
| 889 | + // get model classname |
|
| 890 | + $classname = EEH_File::get_classname_from_filepath_with_standard_filename($model); |
|
| 891 | + $short_name = str_replace('EEM_', '', $classname); |
|
| 892 | + $reflectionClass = new ReflectionClass($classname); |
|
| 893 | + if ($reflectionClass->isSubclassOf('EEM_Base') && ! $reflectionClass->isAbstract()) { |
|
| 894 | + $non_abstract_db_models[ $short_name ] = $classname; |
|
| 895 | + } |
|
| 896 | + $model_names[ $short_name ] = $classname; |
|
| 897 | + } |
|
| 898 | + $this->registry->models = apply_filters('FHEE__EE_System__parse_model_names', $model_names); |
|
| 899 | + $this->registry->non_abstract_db_models = apply_filters( |
|
| 900 | + 'FHEE__EE_System__parse_implemented_model_names', |
|
| 901 | + $non_abstract_db_models |
|
| 902 | + ); |
|
| 903 | + } |
|
| 904 | + |
|
| 905 | + |
|
| 906 | + /** |
|
| 907 | + * The purpose of this method is to simply check for a file named "caffeinated/brewing_regular.php" for any hooks |
|
| 908 | + * that need to be setup before our EE_System launches. |
|
| 909 | + * |
|
| 910 | + * @return void |
|
| 911 | + * @throws DomainException |
|
| 912 | + * @throws InvalidArgumentException |
|
| 913 | + * @throws InvalidDataTypeException |
|
| 914 | + * @throws InvalidInterfaceException |
|
| 915 | + * @throws InvalidClassException |
|
| 916 | + * @throws InvalidFilePathException |
|
| 917 | + */ |
|
| 918 | + private function _maybe_brew_regular() |
|
| 919 | + { |
|
| 920 | + /** @var Domain $domain */ |
|
| 921 | + $domain = DomainFactory::getShared( |
|
| 922 | + new FullyQualifiedName( |
|
| 923 | + 'EventEspresso\core\domain\Domain' |
|
| 924 | + ), |
|
| 925 | + array( |
|
| 926 | + new FilePath(EVENT_ESPRESSO_MAIN_FILE), |
|
| 927 | + Version::fromString(espresso_version()), |
|
| 928 | + ) |
|
| 929 | + ); |
|
| 930 | + if ($domain->isCaffeinated()) { |
|
| 931 | + require_once EE_CAFF_PATH . 'brewing_regular.php'; |
|
| 932 | + } |
|
| 933 | + } |
|
| 934 | + |
|
| 935 | + |
|
| 936 | + /** |
|
| 937 | + * @since 4.9.71.p |
|
| 938 | + * @throws Exception |
|
| 939 | + */ |
|
| 940 | + public function loadRouteMatchSpecifications() |
|
| 941 | + { |
|
| 942 | + try { |
|
| 943 | + $this->loader->getShared( |
|
| 944 | + 'EventEspresso\core\services\route_match\RouteMatchSpecificationManager' |
|
| 945 | + ); |
|
| 946 | + } catch (Exception $exception) { |
|
| 947 | + new ExceptionStackTraceDisplay($exception); |
|
| 948 | + } |
|
| 949 | + do_action('AHEE__EE_System__loadRouteMatchSpecifications'); |
|
| 950 | + } |
|
| 951 | + |
|
| 952 | + |
|
| 953 | + /** |
|
| 954 | + * register_shortcodes_modules_and_widgets |
|
| 955 | + * generate lists of shortcodes and modules, then verify paths and classes |
|
| 956 | + * This is hooked into 'AHEE__EE_Bootstrap__register_shortcodes_modules_and_widgets' |
|
| 957 | + * which runs during the WP 'plugins_loaded' action at priority 7 |
|
| 958 | + * |
|
| 959 | + * @access public |
|
| 960 | + * @return void |
|
| 961 | + * @throws Exception |
|
| 962 | + */ |
|
| 963 | + public function register_shortcodes_modules_and_widgets() |
|
| 964 | + { |
|
| 965 | + if ($this->request->isFrontend() || $this->request->isIframe() || $this->request->isAjax()) { |
|
| 966 | + try { |
|
| 967 | + // load, register, and add shortcodes the new way |
|
| 968 | + $this->loader->getShared( |
|
| 969 | + 'EventEspresso\core\services\shortcodes\ShortcodesManager', |
|
| 970 | + array( |
|
| 971 | + // and the old way, but we'll put it under control of the new system |
|
| 972 | + EE_Config::getLegacyShortcodesManager(), |
|
| 973 | + ) |
|
| 974 | + ); |
|
| 975 | + } catch (Exception $exception) { |
|
| 976 | + new ExceptionStackTraceDisplay($exception); |
|
| 977 | + } |
|
| 978 | + } |
|
| 979 | + do_action('AHEE__EE_System__register_shortcodes_modules_and_widgets'); |
|
| 980 | + // check for addons using old hook point |
|
| 981 | + if (has_action('AHEE__EE_System__register_shortcodes_modules_and_addons')) { |
|
| 982 | + $this->_incompatible_addon_error(); |
|
| 983 | + } |
|
| 984 | + } |
|
| 985 | + |
|
| 986 | + |
|
| 987 | + /** |
|
| 988 | + * _incompatible_addon_error |
|
| 989 | + * |
|
| 990 | + * @access public |
|
| 991 | + * @return void |
|
| 992 | + */ |
|
| 993 | + private function _incompatible_addon_error() |
|
| 994 | + { |
|
| 995 | + // get array of classes hooking into here |
|
| 996 | + $class_names = EEH_Class_Tools::get_class_names_for_all_callbacks_on_hook( |
|
| 997 | + 'AHEE__EE_System__register_shortcodes_modules_and_addons' |
|
| 998 | + ); |
|
| 999 | + if (! empty($class_names)) { |
|
| 1000 | + $msg = __( |
|
| 1001 | + 'The following plugins, addons, or modules appear to be incompatible with this version of Event Espresso and were automatically deactivated to avoid fatal errors:', |
|
| 1002 | + 'event_espresso' |
|
| 1003 | + ); |
|
| 1004 | + $msg .= '<ul>'; |
|
| 1005 | + foreach ($class_names as $class_name) { |
|
| 1006 | + $msg .= '<li><b>Event Espresso - ' |
|
| 1007 | + . str_replace( |
|
| 1008 | + array('EE_', 'EEM_', 'EED_', 'EES_', 'EEW_'), |
|
| 1009 | + '', |
|
| 1010 | + $class_name |
|
| 1011 | + ) . '</b></li>'; |
|
| 1012 | + } |
|
| 1013 | + $msg .= '</ul>'; |
|
| 1014 | + $msg .= __( |
|
| 1015 | + 'Compatibility issues can be avoided and/or resolved by keeping addons and plugins updated to the latest version.', |
|
| 1016 | + 'event_espresso' |
|
| 1017 | + ); |
|
| 1018 | + // save list of incompatible addons to wp-options for later use |
|
| 1019 | + add_option('ee_incompatible_addons', $class_names, '', 'no'); |
|
| 1020 | + if (is_admin()) { |
|
| 1021 | + EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__); |
|
| 1022 | + } |
|
| 1023 | + } |
|
| 1024 | + } |
|
| 1025 | + |
|
| 1026 | + |
|
| 1027 | + /** |
|
| 1028 | + * brew_espresso |
|
| 1029 | + * begins the process of setting hooks for initializing EE in the correct order |
|
| 1030 | + * This is happening on the 'AHEE__EE_Bootstrap__brew_espresso' hook point |
|
| 1031 | + * which runs during the WP 'plugins_loaded' action at priority 9 |
|
| 1032 | + * |
|
| 1033 | + * @return void |
|
| 1034 | + */ |
|
| 1035 | + public function brew_espresso() |
|
| 1036 | + { |
|
| 1037 | + do_action('AHEE__EE_System__brew_espresso__begin', $this); |
|
| 1038 | + // load some final core systems |
|
| 1039 | + add_action('init', array($this, 'set_hooks_for_core'), 1); |
|
| 1040 | + add_action('init', array($this, 'perform_activations_upgrades_and_migrations'), 3); |
|
| 1041 | + add_action('init', array($this, 'load_CPTs_and_session'), 5); |
|
| 1042 | + add_action('init', array($this, 'load_controllers'), 7); |
|
| 1043 | + add_action('init', array($this, 'core_loaded_and_ready'), 9); |
|
| 1044 | + add_action('init', array($this, 'initialize'), 10); |
|
| 1045 | + add_action('init', array($this, 'initialize_last'), 100); |
|
| 1046 | + if (is_admin() && apply_filters('FHEE__EE_System__brew_espresso__load_pue', true)) { |
|
| 1047 | + // pew pew pew |
|
| 1048 | + $this->loader->getShared('EventEspresso\core\services\licensing\LicenseService'); |
|
| 1049 | + do_action('AHEE__EE_System__brew_espresso__after_pue_init'); |
|
| 1050 | + } |
|
| 1051 | + do_action('AHEE__EE_System__brew_espresso__complete', $this); |
|
| 1052 | + } |
|
| 1053 | + |
|
| 1054 | + |
|
| 1055 | + /** |
|
| 1056 | + * set_hooks_for_core |
|
| 1057 | + * |
|
| 1058 | + * @access public |
|
| 1059 | + * @return void |
|
| 1060 | + * @throws EE_Error |
|
| 1061 | + */ |
|
| 1062 | + public function set_hooks_for_core() |
|
| 1063 | + { |
|
| 1064 | + $this->_deactivate_incompatible_addons(); |
|
| 1065 | + do_action('AHEE__EE_System__set_hooks_for_core'); |
|
| 1066 | + $this->loader->getShared('EventEspresso\core\domain\values\session\SessionLifespan'); |
|
| 1067 | + // caps need to be initialized on every request so that capability maps are set. |
|
| 1068 | + // @see https://events.codebasehq.com/projects/event-espresso/tickets/8674 |
|
| 1069 | + $this->registry->CAP->init_caps(); |
|
| 1070 | + } |
|
| 1071 | + |
|
| 1072 | + |
|
| 1073 | + /** |
|
| 1074 | + * Using the information gathered in EE_System::_incompatible_addon_error, |
|
| 1075 | + * deactivates any addons considered incompatible with the current version of EE |
|
| 1076 | + */ |
|
| 1077 | + private function _deactivate_incompatible_addons() |
|
| 1078 | + { |
|
| 1079 | + $incompatible_addons = get_option('ee_incompatible_addons', array()); |
|
| 1080 | + if (! empty($incompatible_addons)) { |
|
| 1081 | + $active_plugins = get_option('active_plugins', array()); |
|
| 1082 | + foreach ($active_plugins as $active_plugin) { |
|
| 1083 | + foreach ($incompatible_addons as $incompatible_addon) { |
|
| 1084 | + if (strpos($active_plugin, $incompatible_addon) !== false) { |
|
| 1085 | + unset($_GET['activate']); |
|
| 1086 | + espresso_deactivate_plugin($active_plugin); |
|
| 1087 | + } |
|
| 1088 | + } |
|
| 1089 | + } |
|
| 1090 | + } |
|
| 1091 | + } |
|
| 1092 | + |
|
| 1093 | + |
|
| 1094 | + /** |
|
| 1095 | + * perform_activations_upgrades_and_migrations |
|
| 1096 | + * |
|
| 1097 | + * @access public |
|
| 1098 | + * @return void |
|
| 1099 | + */ |
|
| 1100 | + public function perform_activations_upgrades_and_migrations() |
|
| 1101 | + { |
|
| 1102 | + do_action('AHEE__EE_System__perform_activations_upgrades_and_migrations'); |
|
| 1103 | + } |
|
| 1104 | + |
|
| 1105 | + |
|
| 1106 | + /** |
|
| 1107 | + * @return void |
|
| 1108 | + * @throws DomainException |
|
| 1109 | + */ |
|
| 1110 | + public function load_CPTs_and_session() |
|
| 1111 | + { |
|
| 1112 | + do_action('AHEE__EE_System__load_CPTs_and_session__start'); |
|
| 1113 | + /** @var EventEspresso\core\domain\services\custom_post_types\RegisterCustomTaxonomies $register_custom_taxonomies */ |
|
| 1114 | + $register_custom_taxonomies = $this->loader->getShared( |
|
| 1115 | + 'EventEspresso\core\domain\services\custom_post_types\RegisterCustomTaxonomies' |
|
| 1116 | + ); |
|
| 1117 | + $register_custom_taxonomies->registerCustomTaxonomies(); |
|
| 1118 | + /** @var EventEspresso\core\domain\services\custom_post_types\RegisterCustomPostTypes $register_custom_post_types */ |
|
| 1119 | + $register_custom_post_types = $this->loader->getShared( |
|
| 1120 | + 'EventEspresso\core\domain\services\custom_post_types\RegisterCustomPostTypes' |
|
| 1121 | + ); |
|
| 1122 | + $register_custom_post_types->registerCustomPostTypes(); |
|
| 1123 | + /** @var EventEspresso\core\domain\services\custom_post_types\RegisterCustomTaxonomyTerms $register_custom_taxonomy_terms */ |
|
| 1124 | + $register_custom_taxonomy_terms = $this->loader->getShared( |
|
| 1125 | + 'EventEspresso\core\domain\services\custom_post_types\RegisterCustomTaxonomyTerms' |
|
| 1126 | + ); |
|
| 1127 | + $register_custom_taxonomy_terms->registerCustomTaxonomyTerms(); |
|
| 1128 | + // load legacy Custom Post Types and Taxonomies |
|
| 1129 | + $this->loader->getShared('EE_Register_CPTs'); |
|
| 1130 | + do_action('AHEE__EE_System__load_CPTs_and_session__complete'); |
|
| 1131 | + } |
|
| 1132 | + |
|
| 1133 | + |
|
| 1134 | + /** |
|
| 1135 | + * load_controllers |
|
| 1136 | + * this is the best place to load any additional controllers that needs access to EE core. |
|
| 1137 | + * it is expected that all basic core EE systems, that are not dependant on the current request are loaded at this |
|
| 1138 | + * time |
|
| 1139 | + * |
|
| 1140 | + * @access public |
|
| 1141 | + * @return void |
|
| 1142 | + */ |
|
| 1143 | + public function load_controllers() |
|
| 1144 | + { |
|
| 1145 | + do_action('AHEE__EE_System__load_controllers__start'); |
|
| 1146 | + // let's get it started |
|
| 1147 | + if (! $this->maintenance_mode->level() |
|
| 1148 | + && ($this->request->isFrontend() || $this->request->isFrontAjax()) |
|
| 1149 | + ) { |
|
| 1150 | + do_action('AHEE__EE_System__load_controllers__load_front_controllers'); |
|
| 1151 | + $this->loader->getShared('EE_Front_Controller'); |
|
| 1152 | + } elseif ($this->request->isAdmin() || $this->request->isAdminAjax()) { |
|
| 1153 | + do_action('AHEE__EE_System__load_controllers__load_admin_controllers'); |
|
| 1154 | + $this->loader->getShared('EE_Admin'); |
|
| 1155 | + } elseif ($this->request->isWordPressHeartbeat()) { |
|
| 1156 | + $this->loader->getShared('EventEspresso\core\domain\services\admin\ajax\WordpressHeartbeat'); |
|
| 1157 | + } |
|
| 1158 | + do_action('AHEE__EE_System__load_controllers__complete'); |
|
| 1159 | + } |
|
| 1160 | + |
|
| 1161 | + |
|
| 1162 | + /** |
|
| 1163 | + * core_loaded_and_ready |
|
| 1164 | + * all of the basic EE core should be loaded at this point and available regardless of M-Mode |
|
| 1165 | + * |
|
| 1166 | + * @access public |
|
| 1167 | + * @return void |
|
| 1168 | + * @throws Exception |
|
| 1169 | + */ |
|
| 1170 | + public function core_loaded_and_ready() |
|
| 1171 | + { |
|
| 1172 | + if ($this->request->isAdmin() |
|
| 1173 | + || $this->request->isFrontend() |
|
| 1174 | + || $this->request->isIframe() |
|
| 1175 | + || $this->request->isWordPressApi() |
|
| 1176 | + ) { |
|
| 1177 | + try { |
|
| 1178 | + $this->loader->getShared('EventEspresso\core\services\assets\Registry'); |
|
| 1179 | + $this->loader->getShared('EventEspresso\core\domain\services\assets\CoreAssetManager'); |
|
| 1180 | + if ($this->canLoadBlocks()) { |
|
| 1181 | + $this->loader->getShared( |
|
| 1182 | + 'EventEspresso\core\services\editor\BlockRegistrationManager' |
|
| 1183 | + ); |
|
| 1184 | + } |
|
| 1185 | + } catch (Exception $exception) { |
|
| 1186 | + new ExceptionStackTraceDisplay($exception); |
|
| 1187 | + } |
|
| 1188 | + } |
|
| 1189 | + if ($this->request->isAdmin() |
|
| 1190 | + || $this->request->isEeAjax() |
|
| 1191 | + || $this->request->isFrontend() |
|
| 1192 | + ) { |
|
| 1193 | + $this->loader->getShared('EE_Session'); |
|
| 1194 | + } |
|
| 1195 | + // integrate WP_Query with the EE models |
|
| 1196 | + $this->loader->getShared('EE_CPT_Strategy'); |
|
| 1197 | + do_action('AHEE__EE_System__core_loaded_and_ready'); |
|
| 1198 | + // always load template tags, because it's faster than checking if it's a front-end request, and many page |
|
| 1199 | + // builders require these even on the front-end |
|
| 1200 | + require_once EE_PUBLIC . 'template_tags.php'; |
|
| 1201 | + do_action('AHEE__EE_System__set_hooks_for_shortcodes_modules_and_addons'); |
|
| 1202 | + } |
|
| 1203 | + |
|
| 1204 | + |
|
| 1205 | + /** |
|
| 1206 | + * initialize |
|
| 1207 | + * this is the best place to begin initializing client code |
|
| 1208 | + * |
|
| 1209 | + * @access public |
|
| 1210 | + * @return void |
|
| 1211 | + */ |
|
| 1212 | + public function initialize() |
|
| 1213 | + { |
|
| 1214 | + do_action('AHEE__EE_System__initialize'); |
|
| 1215 | + } |
|
| 1216 | + |
|
| 1217 | + |
|
| 1218 | + /** |
|
| 1219 | + * initialize_last |
|
| 1220 | + * this is run really late during the WP init hook point, and ensures that mostly everything else that needs to |
|
| 1221 | + * initialize has done so |
|
| 1222 | + * |
|
| 1223 | + * @access public |
|
| 1224 | + * @return void |
|
| 1225 | + */ |
|
| 1226 | + public function initialize_last() |
|
| 1227 | + { |
|
| 1228 | + do_action('AHEE__EE_System__initialize_last'); |
|
| 1229 | + /** @var EventEspresso\core\domain\services\custom_post_types\RewriteRules $rewrite_rules */ |
|
| 1230 | + $rewrite_rules = $this->loader->getShared( |
|
| 1231 | + 'EventEspresso\core\domain\services\custom_post_types\RewriteRules' |
|
| 1232 | + ); |
|
| 1233 | + $rewrite_rules->flushRewriteRules(); |
|
| 1234 | + add_action('admin_bar_init', array($this, 'addEspressoToolbar')); |
|
| 1235 | + if (($this->request->isAjax() || $this->request->isAdmin()) |
|
| 1236 | + && $this->maintenance_mode->models_can_query()) { |
|
| 1237 | + $this->loader->getShared('EventEspresso\core\services\privacy\export\PersonalDataExporterManager'); |
|
| 1238 | + $this->loader->getShared('EventEspresso\core\services\privacy\erasure\PersonalDataEraserManager'); |
|
| 1239 | + } |
|
| 1240 | + } |
|
| 1241 | + |
|
| 1242 | + |
|
| 1243 | + /** |
|
| 1244 | + * @return void |
|
| 1245 | + * @throws EE_Error |
|
| 1246 | + */ |
|
| 1247 | + public function addEspressoToolbar() |
|
| 1248 | + { |
|
| 1249 | + $this->loader->getShared( |
|
| 1250 | + 'EventEspresso\core\domain\services\admin\AdminToolBar', |
|
| 1251 | + array($this->registry->CAP) |
|
| 1252 | + ); |
|
| 1253 | + } |
|
| 1254 | + |
|
| 1255 | + |
|
| 1256 | + /** |
|
| 1257 | + * do_not_cache |
|
| 1258 | + * sets no cache headers and defines no cache constants for WP plugins |
|
| 1259 | + * |
|
| 1260 | + * @access public |
|
| 1261 | + * @return void |
|
| 1262 | + */ |
|
| 1263 | + public static function do_not_cache() |
|
| 1264 | + { |
|
| 1265 | + // set no cache constants |
|
| 1266 | + if (! defined('DONOTCACHEPAGE')) { |
|
| 1267 | + define('DONOTCACHEPAGE', true); |
|
| 1268 | + } |
|
| 1269 | + if (! defined('DONOTCACHCEOBJECT')) { |
|
| 1270 | + define('DONOTCACHCEOBJECT', true); |
|
| 1271 | + } |
|
| 1272 | + if (! defined('DONOTCACHEDB')) { |
|
| 1273 | + define('DONOTCACHEDB', true); |
|
| 1274 | + } |
|
| 1275 | + // add no cache headers |
|
| 1276 | + add_action('send_headers', array('EE_System', 'nocache_headers'), 10); |
|
| 1277 | + // plus a little extra for nginx and Google Chrome |
|
| 1278 | + add_filter('nocache_headers', array('EE_System', 'extra_nocache_headers'), 10, 1); |
|
| 1279 | + // prevent browsers from prefetching of the rel='next' link, because it may contain content that interferes with the registration process |
|
| 1280 | + remove_action('wp_head', 'adjacent_posts_rel_link_wp_head'); |
|
| 1281 | + } |
|
| 1282 | + |
|
| 1283 | + |
|
| 1284 | + /** |
|
| 1285 | + * extra_nocache_headers |
|
| 1286 | + * |
|
| 1287 | + * @access public |
|
| 1288 | + * @param $headers |
|
| 1289 | + * @return array |
|
| 1290 | + */ |
|
| 1291 | + public static function extra_nocache_headers($headers) |
|
| 1292 | + { |
|
| 1293 | + // for NGINX |
|
| 1294 | + $headers['X-Accel-Expires'] = 0; |
|
| 1295 | + // plus extra for Google Chrome since it doesn't seem to respect "no-cache", but WILL respect "no-store" |
|
| 1296 | + $headers['Cache-Control'] = 'no-store, no-cache, must-revalidate, max-age=0'; |
|
| 1297 | + return $headers; |
|
| 1298 | + } |
|
| 1299 | + |
|
| 1300 | + |
|
| 1301 | + /** |
|
| 1302 | + * nocache_headers |
|
| 1303 | + * |
|
| 1304 | + * @access public |
|
| 1305 | + * @return void |
|
| 1306 | + */ |
|
| 1307 | + public static function nocache_headers() |
|
| 1308 | + { |
|
| 1309 | + nocache_headers(); |
|
| 1310 | + } |
|
| 1311 | + |
|
| 1312 | + |
|
| 1313 | + /** |
|
| 1314 | + * simply hooks into "wp_list_pages_exclude" filter (for wp_list_pages method) and makes sure EE critical pages are |
|
| 1315 | + * never returned with the function. |
|
| 1316 | + * |
|
| 1317 | + * @param array $exclude_array any existing pages being excluded are in this array. |
|
| 1318 | + * @return array |
|
| 1319 | + */ |
|
| 1320 | + public function remove_pages_from_wp_list_pages($exclude_array) |
|
| 1321 | + { |
|
| 1322 | + return array_merge($exclude_array, $this->registry->CFG->core->get_critical_pages_array()); |
|
| 1323 | + } |
|
| 1324 | + |
|
| 1325 | + |
|
| 1326 | + /** |
|
| 1327 | + * Return whether blocks can be registered/loaded or not. |
|
| 1328 | + * @return bool |
|
| 1329 | + */ |
|
| 1330 | + private function canLoadBlocks() |
|
| 1331 | + { |
|
| 1332 | + return apply_filters('FHEE__EE_System__canLoadBlocks', true) |
|
| 1333 | + && function_exists('register_block_type') |
|
| 1334 | + // don't load blocks if in the Divi page builder editor context |
|
| 1335 | + // @see https://github.com/eventespresso/event-espresso-core/issues/814 |
|
| 1336 | + && ! $this->request->getRequestParam('et_fb', false); |
|
| 1337 | + } |
|
| 1338 | 1338 | } |
@@ -16,155 +16,155 @@ |
||
| 16 | 16 | class RequestTypeContext extends Context |
| 17 | 17 | { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * indicates that the current request involves some form of activation |
|
| 21 | - */ |
|
| 22 | - const ACTIVATION = 'activation-request'; |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * indicates that the current request is for the admin but is not being made via AJAX |
|
| 26 | - */ |
|
| 27 | - const ADMIN = 'non-ajax-admin-request'; |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * indicates that the current request is for the admin AND is being made via AJAX |
|
| 31 | - */ |
|
| 32 | - const AJAX_ADMIN = 'admin-ajax-request'; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * indicates that the current request is for the frontend AND is being made via AJAX |
|
| 36 | - */ |
|
| 37 | - const AJAX_FRONT = 'frontend-ajax-request'; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * indicates that the current request is for the WP Heartbeat |
|
| 41 | - */ |
|
| 42 | - const AJAX_HEARTBEAT = 'admin-ajax-heartbeat'; |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * indicates that the current request is being made via AJAX, but is NOT for EE |
|
| 46 | - */ |
|
| 47 | - const AJAX_OTHER = 'other-ajax-request'; |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * indicates that the current request is for the EE REST API |
|
| 51 | - */ |
|
| 52 | - const API = 'rest-api'; |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * indicates that the current request is from the command line |
|
| 56 | - */ |
|
| 57 | - const CLI = 'command-line'; |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * indicates that the current request is for a WP_Cron |
|
| 61 | - */ |
|
| 62 | - const CRON = 'wp-cron'; |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * indicates that the current request is for a feed (ie: RSS) |
|
| 66 | - */ |
|
| 67 | - const FEED = 'feed-request'; |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * indicates that the current request is for the frontend but is not being made via AJAX |
|
| 71 | - */ |
|
| 72 | - const FRONTEND = 'non-ajax-frontend-request'; |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * indicates that the current request is for content that is to be displayed within an iframe |
|
| 76 | - */ |
|
| 77 | - const IFRAME = 'iframe-request'; |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * indicates that the current request is for the WP REST API |
|
| 81 | - */ |
|
| 82 | - const WP_API = 'wp-rest-api'; |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * indicates that the current request is a loopback sent from WP core to test for errors |
|
| 86 | - */ |
|
| 87 | - const WP_SCRAPE = 'wordpress-scrape'; |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * @var boolean $is_activation |
|
| 91 | - */ |
|
| 92 | - private $is_activation = false; |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * @var array $valid_request_types |
|
| 96 | - */ |
|
| 97 | - private $valid_request_types = array(); |
|
| 98 | - |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * RequestTypeContext constructor. |
|
| 102 | - * |
|
| 103 | - * @param string $slug |
|
| 104 | - * @param string $description |
|
| 105 | - * @throws InvalidArgumentException |
|
| 106 | - */ |
|
| 107 | - public function __construct($slug, $description) |
|
| 108 | - { |
|
| 109 | - parent::__construct($slug, $description); |
|
| 110 | - if (! in_array($this->slug(), $this->validRequestTypes(), true)) { |
|
| 111 | - throw new InvalidArgumentException( |
|
| 112 | - sprintf( |
|
| 113 | - esc_html__( |
|
| 114 | - 'The RequestTypeContext slug must be one of the following values: %1$s %2$s', |
|
| 115 | - 'event_espresso' |
|
| 116 | - ), |
|
| 117 | - var_export($this->validRequestTypes(), true) |
|
| 118 | - ) |
|
| 119 | - ); |
|
| 120 | - } |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * @return array |
|
| 126 | - */ |
|
| 127 | - public function validRequestTypes() |
|
| 128 | - { |
|
| 129 | - if (empty($this->valid_request_types)) { |
|
| 130 | - $this->valid_request_types = apply_filters( |
|
| 131 | - 'FHEE__EventEspresso_core_domain_entities_contexts_RequestTypeContext__validRequestTypes', |
|
| 132 | - array( |
|
| 133 | - RequestTypeContext::ACTIVATION, |
|
| 134 | - RequestTypeContext::ADMIN, |
|
| 135 | - RequestTypeContext::AJAX_ADMIN, |
|
| 136 | - RequestTypeContext::AJAX_FRONT, |
|
| 137 | - RequestTypeContext::AJAX_HEARTBEAT, |
|
| 138 | - RequestTypeContext::AJAX_OTHER, |
|
| 139 | - RequestTypeContext::API, |
|
| 140 | - RequestTypeContext::CLI, |
|
| 141 | - RequestTypeContext::CRON, |
|
| 142 | - RequestTypeContext::FEED, |
|
| 143 | - RequestTypeContext::FRONTEND, |
|
| 144 | - RequestTypeContext::IFRAME, |
|
| 145 | - RequestTypeContext::WP_API, |
|
| 146 | - RequestTypeContext::WP_SCRAPE, |
|
| 147 | - ) |
|
| 148 | - ); |
|
| 149 | - } |
|
| 150 | - return $this->valid_request_types; |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - |
|
| 154 | - /** |
|
| 155 | - * @return bool |
|
| 156 | - */ |
|
| 157 | - public function isActivation() |
|
| 158 | - { |
|
| 159 | - return $this->is_activation; |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - |
|
| 163 | - /** |
|
| 164 | - * @param bool $is_activation |
|
| 165 | - */ |
|
| 166 | - public function setIsActivation($is_activation) |
|
| 167 | - { |
|
| 168 | - $this->is_activation = filter_var($is_activation, FILTER_VALIDATE_BOOLEAN); |
|
| 169 | - } |
|
| 19 | + /** |
|
| 20 | + * indicates that the current request involves some form of activation |
|
| 21 | + */ |
|
| 22 | + const ACTIVATION = 'activation-request'; |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * indicates that the current request is for the admin but is not being made via AJAX |
|
| 26 | + */ |
|
| 27 | + const ADMIN = 'non-ajax-admin-request'; |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * indicates that the current request is for the admin AND is being made via AJAX |
|
| 31 | + */ |
|
| 32 | + const AJAX_ADMIN = 'admin-ajax-request'; |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * indicates that the current request is for the frontend AND is being made via AJAX |
|
| 36 | + */ |
|
| 37 | + const AJAX_FRONT = 'frontend-ajax-request'; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * indicates that the current request is for the WP Heartbeat |
|
| 41 | + */ |
|
| 42 | + const AJAX_HEARTBEAT = 'admin-ajax-heartbeat'; |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * indicates that the current request is being made via AJAX, but is NOT for EE |
|
| 46 | + */ |
|
| 47 | + const AJAX_OTHER = 'other-ajax-request'; |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * indicates that the current request is for the EE REST API |
|
| 51 | + */ |
|
| 52 | + const API = 'rest-api'; |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * indicates that the current request is from the command line |
|
| 56 | + */ |
|
| 57 | + const CLI = 'command-line'; |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * indicates that the current request is for a WP_Cron |
|
| 61 | + */ |
|
| 62 | + const CRON = 'wp-cron'; |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * indicates that the current request is for a feed (ie: RSS) |
|
| 66 | + */ |
|
| 67 | + const FEED = 'feed-request'; |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * indicates that the current request is for the frontend but is not being made via AJAX |
|
| 71 | + */ |
|
| 72 | + const FRONTEND = 'non-ajax-frontend-request'; |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * indicates that the current request is for content that is to be displayed within an iframe |
|
| 76 | + */ |
|
| 77 | + const IFRAME = 'iframe-request'; |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * indicates that the current request is for the WP REST API |
|
| 81 | + */ |
|
| 82 | + const WP_API = 'wp-rest-api'; |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * indicates that the current request is a loopback sent from WP core to test for errors |
|
| 86 | + */ |
|
| 87 | + const WP_SCRAPE = 'wordpress-scrape'; |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * @var boolean $is_activation |
|
| 91 | + */ |
|
| 92 | + private $is_activation = false; |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * @var array $valid_request_types |
|
| 96 | + */ |
|
| 97 | + private $valid_request_types = array(); |
|
| 98 | + |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * RequestTypeContext constructor. |
|
| 102 | + * |
|
| 103 | + * @param string $slug |
|
| 104 | + * @param string $description |
|
| 105 | + * @throws InvalidArgumentException |
|
| 106 | + */ |
|
| 107 | + public function __construct($slug, $description) |
|
| 108 | + { |
|
| 109 | + parent::__construct($slug, $description); |
|
| 110 | + if (! in_array($this->slug(), $this->validRequestTypes(), true)) { |
|
| 111 | + throw new InvalidArgumentException( |
|
| 112 | + sprintf( |
|
| 113 | + esc_html__( |
|
| 114 | + 'The RequestTypeContext slug must be one of the following values: %1$s %2$s', |
|
| 115 | + 'event_espresso' |
|
| 116 | + ), |
|
| 117 | + var_export($this->validRequestTypes(), true) |
|
| 118 | + ) |
|
| 119 | + ); |
|
| 120 | + } |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * @return array |
|
| 126 | + */ |
|
| 127 | + public function validRequestTypes() |
|
| 128 | + { |
|
| 129 | + if (empty($this->valid_request_types)) { |
|
| 130 | + $this->valid_request_types = apply_filters( |
|
| 131 | + 'FHEE__EventEspresso_core_domain_entities_contexts_RequestTypeContext__validRequestTypes', |
|
| 132 | + array( |
|
| 133 | + RequestTypeContext::ACTIVATION, |
|
| 134 | + RequestTypeContext::ADMIN, |
|
| 135 | + RequestTypeContext::AJAX_ADMIN, |
|
| 136 | + RequestTypeContext::AJAX_FRONT, |
|
| 137 | + RequestTypeContext::AJAX_HEARTBEAT, |
|
| 138 | + RequestTypeContext::AJAX_OTHER, |
|
| 139 | + RequestTypeContext::API, |
|
| 140 | + RequestTypeContext::CLI, |
|
| 141 | + RequestTypeContext::CRON, |
|
| 142 | + RequestTypeContext::FEED, |
|
| 143 | + RequestTypeContext::FRONTEND, |
|
| 144 | + RequestTypeContext::IFRAME, |
|
| 145 | + RequestTypeContext::WP_API, |
|
| 146 | + RequestTypeContext::WP_SCRAPE, |
|
| 147 | + ) |
|
| 148 | + ); |
|
| 149 | + } |
|
| 150 | + return $this->valid_request_types; |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + |
|
| 154 | + /** |
|
| 155 | + * @return bool |
|
| 156 | + */ |
|
| 157 | + public function isActivation() |
|
| 158 | + { |
|
| 159 | + return $this->is_activation; |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + |
|
| 163 | + /** |
|
| 164 | + * @param bool $is_activation |
|
| 165 | + */ |
|
| 166 | + public function setIsActivation($is_activation) |
|
| 167 | + { |
|
| 168 | + $this->is_activation = filter_var($is_activation, FILTER_VALIDATE_BOOLEAN); |
|
| 169 | + } |
|
| 170 | 170 | } |
@@ -16,217 +16,217 @@ |
||
| 16 | 16 | class RequestTypeContextChecker extends ContextChecker implements RequestTypeContextCheckerInterface |
| 17 | 17 | { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * @var RequestTypeContext $request_type |
|
| 21 | - */ |
|
| 22 | - private $request_type; |
|
| 23 | - |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * RequestTypeContextChecker constructor. |
|
| 27 | - * |
|
| 28 | - * @param RequestTypeContext $request_type |
|
| 29 | - */ |
|
| 30 | - public function __construct(RequestTypeContext $request_type) |
|
| 31 | - { |
|
| 32 | - $this->request_type = $request_type; |
|
| 33 | - parent::__construct( |
|
| 34 | - 'RequestTypeContextChecker', |
|
| 35 | - $this->request_type->validRequestTypes() |
|
| 36 | - ); |
|
| 37 | - } |
|
| 38 | - |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * true if the current request involves some form of activation |
|
| 42 | - * |
|
| 43 | - * @return bool |
|
| 44 | - */ |
|
| 45 | - public function isActivation() |
|
| 46 | - { |
|
| 47 | - return $this->request_type->isActivation(); |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * @param $is_activation |
|
| 53 | - * @return bool |
|
| 54 | - */ |
|
| 55 | - public function setIsActivation($is_activation) |
|
| 56 | - { |
|
| 57 | - return $this->request_type->setIsActivation($is_activation); |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * true if the current request is for the admin and is not being made via AJAX |
|
| 63 | - * |
|
| 64 | - * @return bool |
|
| 65 | - */ |
|
| 66 | - public function isAdmin() |
|
| 67 | - { |
|
| 68 | - return $this->request_type->slug() === RequestTypeContext::ADMIN; |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * true if the current request is for the admin AND is being made via AJAX |
|
| 74 | - * |
|
| 75 | - * @return bool |
|
| 76 | - */ |
|
| 77 | - public function isAdminAjax() |
|
| 78 | - { |
|
| 79 | - return $this->request_type->slug() === RequestTypeContext::AJAX_ADMIN; |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * true if the current request is being made via AJAX... any AJAX |
|
| 85 | - * |
|
| 86 | - * @return bool |
|
| 87 | - */ |
|
| 88 | - public function isAjax() |
|
| 89 | - { |
|
| 90 | - return $this->isEeAjax() || $this->isOtherAjax(); |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * true if the current request is for either the EE admin or EE frontend AND is being made via AJAX |
|
| 96 | - * |
|
| 97 | - * @return bool |
|
| 98 | - */ |
|
| 99 | - public function isEeAjax() |
|
| 100 | - { |
|
| 101 | - return $this->isAdminAjax() || $this->isFrontAjax(); |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * true if the current request is being made via AJAX but is NOT for EE related logic |
|
| 107 | - * |
|
| 108 | - * @return bool |
|
| 109 | - */ |
|
| 110 | - public function isOtherAjax() |
|
| 111 | - { |
|
| 112 | - return $this->request_type->slug() === RequestTypeContext::AJAX_OTHER; |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - /** |
|
| 116 | - * true if the current request is for the EE REST API |
|
| 117 | - * |
|
| 118 | - * @return bool |
|
| 119 | - */ |
|
| 120 | - public function isApi() |
|
| 121 | - { |
|
| 122 | - return $this->request_type->slug() === RequestTypeContext::API; |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * true if the current request is from the command line |
|
| 128 | - * |
|
| 129 | - * @return bool |
|
| 130 | - */ |
|
| 131 | - public function isCli() |
|
| 132 | - { |
|
| 133 | - return $this->request_type->slug() === RequestTypeContext::CLI; |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - |
|
| 137 | - /** |
|
| 138 | - * true if the current request is for a WP_Cron |
|
| 139 | - * |
|
| 140 | - * @return bool |
|
| 141 | - */ |
|
| 142 | - public function isCron() |
|
| 143 | - { |
|
| 144 | - return $this->request_type->slug() === RequestTypeContext::CRON; |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - |
|
| 148 | - /** |
|
| 149 | - * true if the current request is for a feed (ie: RSS) |
|
| 150 | - * |
|
| 151 | - * @return bool |
|
| 152 | - */ |
|
| 153 | - public function isFeed() |
|
| 154 | - { |
|
| 155 | - return $this->request_type->slug() === RequestTypeContext::FEED; |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - |
|
| 159 | - /** |
|
| 160 | - * true if the current request is for the frontend and is not being made via AJAX |
|
| 161 | - * |
|
| 162 | - * @return bool |
|
| 163 | - */ |
|
| 164 | - public function isFrontend() |
|
| 165 | - { |
|
| 166 | - return $this->request_type->slug() === RequestTypeContext::FRONTEND; |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - |
|
| 170 | - /** |
|
| 171 | - * true if the current request is for the frontend AND is being made via AJAX |
|
| 172 | - * |
|
| 173 | - * @return bool |
|
| 174 | - */ |
|
| 175 | - public function isFrontAjax() |
|
| 176 | - { |
|
| 177 | - return $this->request_type->slug() === RequestTypeContext::AJAX_FRONT; |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - |
|
| 181 | - /** |
|
| 182 | - * true if the current request is for content that is to be displayed within an iframe |
|
| 183 | - * |
|
| 184 | - * @return bool |
|
| 185 | - */ |
|
| 186 | - public function isIframe() |
|
| 187 | - { |
|
| 188 | - return $this->request_type->slug() === RequestTypeContext::IFRAME; |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - |
|
| 192 | - /** |
|
| 193 | - * true if the current request is for the WP REST API |
|
| 194 | - * |
|
| 195 | - * @return bool |
|
| 196 | - */ |
|
| 197 | - public function isWordPressApi() |
|
| 198 | - { |
|
| 199 | - return $this->request_type->slug() === RequestTypeContext::WP_API; |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - |
|
| 203 | - /** |
|
| 204 | - * true if the current request is being made via AJAX for the WP Heartbeat |
|
| 205 | - * |
|
| 206 | - * @return bool |
|
| 207 | - */ |
|
| 208 | - public function isWordPressHeartbeat() |
|
| 209 | - { |
|
| 210 | - return $this->request_type->slug() === RequestTypeContext::AJAX_HEARTBEAT; |
|
| 211 | - } |
|
| 212 | - |
|
| 213 | - |
|
| 214 | - /** |
|
| 215 | - * true if the current request is a loopback sent from WP core to test for errors |
|
| 216 | - * |
|
| 217 | - * @return bool |
|
| 218 | - */ |
|
| 219 | - public function isWordPressScrape() |
|
| 220 | - { |
|
| 221 | - return $this->request_type->slug() === RequestTypeContext::WP_SCRAPE; |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - |
|
| 225 | - /** |
|
| 226 | - * @return string |
|
| 227 | - */ |
|
| 228 | - public function slug() |
|
| 229 | - { |
|
| 230 | - return $this->request_type->slug(); |
|
| 231 | - } |
|
| 19 | + /** |
|
| 20 | + * @var RequestTypeContext $request_type |
|
| 21 | + */ |
|
| 22 | + private $request_type; |
|
| 23 | + |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * RequestTypeContextChecker constructor. |
|
| 27 | + * |
|
| 28 | + * @param RequestTypeContext $request_type |
|
| 29 | + */ |
|
| 30 | + public function __construct(RequestTypeContext $request_type) |
|
| 31 | + { |
|
| 32 | + $this->request_type = $request_type; |
|
| 33 | + parent::__construct( |
|
| 34 | + 'RequestTypeContextChecker', |
|
| 35 | + $this->request_type->validRequestTypes() |
|
| 36 | + ); |
|
| 37 | + } |
|
| 38 | + |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * true if the current request involves some form of activation |
|
| 42 | + * |
|
| 43 | + * @return bool |
|
| 44 | + */ |
|
| 45 | + public function isActivation() |
|
| 46 | + { |
|
| 47 | + return $this->request_type->isActivation(); |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * @param $is_activation |
|
| 53 | + * @return bool |
|
| 54 | + */ |
|
| 55 | + public function setIsActivation($is_activation) |
|
| 56 | + { |
|
| 57 | + return $this->request_type->setIsActivation($is_activation); |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * true if the current request is for the admin and is not being made via AJAX |
|
| 63 | + * |
|
| 64 | + * @return bool |
|
| 65 | + */ |
|
| 66 | + public function isAdmin() |
|
| 67 | + { |
|
| 68 | + return $this->request_type->slug() === RequestTypeContext::ADMIN; |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * true if the current request is for the admin AND is being made via AJAX |
|
| 74 | + * |
|
| 75 | + * @return bool |
|
| 76 | + */ |
|
| 77 | + public function isAdminAjax() |
|
| 78 | + { |
|
| 79 | + return $this->request_type->slug() === RequestTypeContext::AJAX_ADMIN; |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * true if the current request is being made via AJAX... any AJAX |
|
| 85 | + * |
|
| 86 | + * @return bool |
|
| 87 | + */ |
|
| 88 | + public function isAjax() |
|
| 89 | + { |
|
| 90 | + return $this->isEeAjax() || $this->isOtherAjax(); |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * true if the current request is for either the EE admin or EE frontend AND is being made via AJAX |
|
| 96 | + * |
|
| 97 | + * @return bool |
|
| 98 | + */ |
|
| 99 | + public function isEeAjax() |
|
| 100 | + { |
|
| 101 | + return $this->isAdminAjax() || $this->isFrontAjax(); |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * true if the current request is being made via AJAX but is NOT for EE related logic |
|
| 107 | + * |
|
| 108 | + * @return bool |
|
| 109 | + */ |
|
| 110 | + public function isOtherAjax() |
|
| 111 | + { |
|
| 112 | + return $this->request_type->slug() === RequestTypeContext::AJAX_OTHER; |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + /** |
|
| 116 | + * true if the current request is for the EE REST API |
|
| 117 | + * |
|
| 118 | + * @return bool |
|
| 119 | + */ |
|
| 120 | + public function isApi() |
|
| 121 | + { |
|
| 122 | + return $this->request_type->slug() === RequestTypeContext::API; |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * true if the current request is from the command line |
|
| 128 | + * |
|
| 129 | + * @return bool |
|
| 130 | + */ |
|
| 131 | + public function isCli() |
|
| 132 | + { |
|
| 133 | + return $this->request_type->slug() === RequestTypeContext::CLI; |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + |
|
| 137 | + /** |
|
| 138 | + * true if the current request is for a WP_Cron |
|
| 139 | + * |
|
| 140 | + * @return bool |
|
| 141 | + */ |
|
| 142 | + public function isCron() |
|
| 143 | + { |
|
| 144 | + return $this->request_type->slug() === RequestTypeContext::CRON; |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + |
|
| 148 | + /** |
|
| 149 | + * true if the current request is for a feed (ie: RSS) |
|
| 150 | + * |
|
| 151 | + * @return bool |
|
| 152 | + */ |
|
| 153 | + public function isFeed() |
|
| 154 | + { |
|
| 155 | + return $this->request_type->slug() === RequestTypeContext::FEED; |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + |
|
| 159 | + /** |
|
| 160 | + * true if the current request is for the frontend and is not being made via AJAX |
|
| 161 | + * |
|
| 162 | + * @return bool |
|
| 163 | + */ |
|
| 164 | + public function isFrontend() |
|
| 165 | + { |
|
| 166 | + return $this->request_type->slug() === RequestTypeContext::FRONTEND; |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + |
|
| 170 | + /** |
|
| 171 | + * true if the current request is for the frontend AND is being made via AJAX |
|
| 172 | + * |
|
| 173 | + * @return bool |
|
| 174 | + */ |
|
| 175 | + public function isFrontAjax() |
|
| 176 | + { |
|
| 177 | + return $this->request_type->slug() === RequestTypeContext::AJAX_FRONT; |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + |
|
| 181 | + /** |
|
| 182 | + * true if the current request is for content that is to be displayed within an iframe |
|
| 183 | + * |
|
| 184 | + * @return bool |
|
| 185 | + */ |
|
| 186 | + public function isIframe() |
|
| 187 | + { |
|
| 188 | + return $this->request_type->slug() === RequestTypeContext::IFRAME; |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + |
|
| 192 | + /** |
|
| 193 | + * true if the current request is for the WP REST API |
|
| 194 | + * |
|
| 195 | + * @return bool |
|
| 196 | + */ |
|
| 197 | + public function isWordPressApi() |
|
| 198 | + { |
|
| 199 | + return $this->request_type->slug() === RequestTypeContext::WP_API; |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + |
|
| 203 | + /** |
|
| 204 | + * true if the current request is being made via AJAX for the WP Heartbeat |
|
| 205 | + * |
|
| 206 | + * @return bool |
|
| 207 | + */ |
|
| 208 | + public function isWordPressHeartbeat() |
|
| 209 | + { |
|
| 210 | + return $this->request_type->slug() === RequestTypeContext::AJAX_HEARTBEAT; |
|
| 211 | + } |
|
| 212 | + |
|
| 213 | + |
|
| 214 | + /** |
|
| 215 | + * true if the current request is a loopback sent from WP core to test for errors |
|
| 216 | + * |
|
| 217 | + * @return bool |
|
| 218 | + */ |
|
| 219 | + public function isWordPressScrape() |
|
| 220 | + { |
|
| 221 | + return $this->request_type->slug() === RequestTypeContext::WP_SCRAPE; |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + |
|
| 225 | + /** |
|
| 226 | + * @return string |
|
| 227 | + */ |
|
| 228 | + public function slug() |
|
| 229 | + { |
|
| 230 | + return $this->request_type->slug(); |
|
| 231 | + } |
|
| 232 | 232 | } |