@@ -39,564 +39,564 @@ |
||
| 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 | - return $read_controller->createEntityFromWpdbResult( |
|
| 326 | - $model_obj->get_model(), |
|
| 327 | - $simulated_db_row, |
|
| 328 | - $simulated_request |
|
| 329 | - ); |
|
| 330 | - } |
|
| 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 | + return $read_controller->createEntityFromWpdbResult( |
|
| 326 | + $model_obj->get_model(), |
|
| 327 | + $simulated_db_row, |
|
| 328 | + $simulated_request |
|
| 329 | + ); |
|
| 330 | + } |
|
| 331 | 331 | |
| 332 | 332 | |
| 333 | - /** |
|
| 334 | - * Gets the item affected by this request |
|
| 335 | - * |
|
| 336 | - * @param EEM_Base $model |
|
| 337 | - * @param WP_REST_Request $request |
|
| 338 | - * @param int|string $obj_id |
|
| 339 | - * @return \WP_Error|array |
|
| 340 | - */ |
|
| 341 | - protected function getOneBasedOnRequest(EEM_Base $model, WP_REST_Request $request, $obj_id) |
|
| 342 | - { |
|
| 343 | - $requested_version = $this->getRequestedVersion($request->get_route()); |
|
| 344 | - $get_request = new WP_REST_Request( |
|
| 345 | - 'GET', |
|
| 346 | - EED_Core_Rest_Api::ee_api_namespace |
|
| 347 | - . $requested_version |
|
| 348 | - . '/' |
|
| 349 | - . EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
|
| 350 | - . '/' |
|
| 351 | - . $obj_id |
|
| 352 | - ); |
|
| 353 | - $get_request->set_url_params( |
|
| 354 | - array( |
|
| 355 | - 'id' => $obj_id, |
|
| 356 | - 'include' => $request->get_param('include'), |
|
| 357 | - ) |
|
| 358 | - ); |
|
| 359 | - $read_controller = new Read(); |
|
| 360 | - $read_controller->setRequestedVersion($this->getRequestedVersion()); |
|
| 361 | - return $read_controller->getEntityFromModel($model, $get_request); |
|
| 362 | - } |
|
| 333 | + /** |
|
| 334 | + * Gets the item affected by this request |
|
| 335 | + * |
|
| 336 | + * @param EEM_Base $model |
|
| 337 | + * @param WP_REST_Request $request |
|
| 338 | + * @param int|string $obj_id |
|
| 339 | + * @return \WP_Error|array |
|
| 340 | + */ |
|
| 341 | + protected function getOneBasedOnRequest(EEM_Base $model, WP_REST_Request $request, $obj_id) |
|
| 342 | + { |
|
| 343 | + $requested_version = $this->getRequestedVersion($request->get_route()); |
|
| 344 | + $get_request = new WP_REST_Request( |
|
| 345 | + 'GET', |
|
| 346 | + EED_Core_Rest_Api::ee_api_namespace |
|
| 347 | + . $requested_version |
|
| 348 | + . '/' |
|
| 349 | + . EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
|
| 350 | + . '/' |
|
| 351 | + . $obj_id |
|
| 352 | + ); |
|
| 353 | + $get_request->set_url_params( |
|
| 354 | + array( |
|
| 355 | + 'id' => $obj_id, |
|
| 356 | + 'include' => $request->get_param('include'), |
|
| 357 | + ) |
|
| 358 | + ); |
|
| 359 | + $read_controller = new Read(); |
|
| 360 | + $read_controller->setRequestedVersion($this->getRequestedVersion()); |
|
| 361 | + return $read_controller->getEntityFromModel($model, $get_request); |
|
| 362 | + } |
|
| 363 | 363 | |
| 364 | - /** |
|
| 365 | - * Adds a relation between the specified models (if it doesn't already exist.) |
|
| 366 | - * @since $VID:$ |
|
| 367 | - * @param WP_REST_Request $request |
|
| 368 | - * @return WP_REST_Response |
|
| 369 | - */ |
|
| 370 | - public static function handleRequestAddRelation(WP_REST_Request $request, $version, $model_name, $related_model_name) |
|
| 371 | - { |
|
| 372 | - $controller = new Write(); |
|
| 373 | - try { |
|
| 374 | - $controller->setRequestedVersion($version); |
|
| 375 | - $main_model = $controller->validateModel($model_name); |
|
| 376 | - $controller->validateModel($related_model_name); |
|
| 377 | - return $controller->sendResponse( |
|
| 378 | - $controller->addRelation( |
|
| 379 | - $main_model, |
|
| 380 | - $main_model->related_settings_for($related_model_name), |
|
| 381 | - $request |
|
| 382 | - ) |
|
| 383 | - ); |
|
| 384 | - } catch (Exception $e) { |
|
| 385 | - return $controller->sendResponse($e); |
|
| 386 | - } |
|
| 387 | - } |
|
| 364 | + /** |
|
| 365 | + * Adds a relation between the specified models (if it doesn't already exist.) |
|
| 366 | + * @since $VID:$ |
|
| 367 | + * @param WP_REST_Request $request |
|
| 368 | + * @return WP_REST_Response |
|
| 369 | + */ |
|
| 370 | + public static function handleRequestAddRelation(WP_REST_Request $request, $version, $model_name, $related_model_name) |
|
| 371 | + { |
|
| 372 | + $controller = new Write(); |
|
| 373 | + try { |
|
| 374 | + $controller->setRequestedVersion($version); |
|
| 375 | + $main_model = $controller->validateModel($model_name); |
|
| 376 | + $controller->validateModel($related_model_name); |
|
| 377 | + return $controller->sendResponse( |
|
| 378 | + $controller->addRelation( |
|
| 379 | + $main_model, |
|
| 380 | + $main_model->related_settings_for($related_model_name), |
|
| 381 | + $request |
|
| 382 | + ) |
|
| 383 | + ); |
|
| 384 | + } catch (Exception $e) { |
|
| 385 | + return $controller->sendResponse($e); |
|
| 386 | + } |
|
| 387 | + } |
|
| 388 | 388 | |
| 389 | - /** |
|
| 390 | - * Adds a relation between the two model specified model objects. |
|
| 391 | - * @since $VID:$ |
|
| 392 | - * @param EEM_Base $model |
|
| 393 | - * @param EE_Model_Relation_Base $relation |
|
| 394 | - * @param WP_REST_Request $request |
|
| 395 | - * @return array |
|
| 396 | - * @throws EE_Error |
|
| 397 | - * @throws InvalidArgumentException |
|
| 398 | - * @throws InvalidDataTypeException |
|
| 399 | - * @throws InvalidInterfaceException |
|
| 400 | - * @throws RestException |
|
| 401 | - * @throws DomainException |
|
| 402 | - */ |
|
| 403 | - public function addRelation(EEM_Base $model, EE_Model_Relation_Base $relation, WP_REST_Request $request) |
|
| 404 | - { |
|
| 405 | - list($model_obj, $other_obj) = $this->getBothModelObjects($model, $relation, $request); |
|
| 406 | - $extra_params = array(); |
|
| 407 | - if ($relation instanceof EE_HABTM_Relation) { |
|
| 408 | - $extra_params = array_intersect_key( |
|
| 409 | - ModelDataTranslator::prepareConditionsQueryParamsForModels( |
|
| 410 | - $request->get_body_params(), |
|
| 411 | - $relation->get_join_model(), |
|
| 412 | - $this->getModelVersionInfo()->requestedVersion(), |
|
| 413 | - true |
|
| 414 | - ), |
|
| 415 | - $relation->getNonKeyFields() |
|
| 416 | - ); |
|
| 417 | - } |
|
| 418 | - // Add a relation. |
|
| 419 | - $related_obj = $model_obj->_add_relation_to( |
|
| 420 | - $other_obj, |
|
| 421 | - $relation->get_other_model()->get_this_model_name(), |
|
| 422 | - $extra_params |
|
| 423 | - ); |
|
| 424 | - $response = array( |
|
| 425 | - 'success' => $related_obj === $other_obj, |
|
| 426 | - strtolower($model->get_this_model_name()) => $this->returnModelObjAsJsonResponse($model_obj, $request), |
|
| 427 | - strtolower($relation->get_other_model()->get_this_model_name()) => $this->returnModelObjAsJsonResponse($related_obj, $request), |
|
| 428 | - ); |
|
| 429 | - if ($relation instanceof EE_HABTM_Relation) { |
|
| 430 | - $join_model_obj = $relation->get_join_model()->get_one( |
|
| 431 | - array( |
|
| 432 | - array( |
|
| 433 | - $model->primary_key_name() => $model_obj->ID(), |
|
| 434 | - $relation->get_other_model()->primary_key_name() => $related_obj->ID() |
|
| 435 | - ) |
|
| 436 | - ) |
|
| 437 | - ); |
|
| 438 | - $response['join'][ strtolower($relation->get_join_model()->get_this_model_name()) ] = $this->returnModelObjAsJsonResponse($join_model_obj, $request); |
|
| 439 | - } |
|
| 440 | - return $response; |
|
| 441 | - } |
|
| 389 | + /** |
|
| 390 | + * Adds a relation between the two model specified model objects. |
|
| 391 | + * @since $VID:$ |
|
| 392 | + * @param EEM_Base $model |
|
| 393 | + * @param EE_Model_Relation_Base $relation |
|
| 394 | + * @param WP_REST_Request $request |
|
| 395 | + * @return array |
|
| 396 | + * @throws EE_Error |
|
| 397 | + * @throws InvalidArgumentException |
|
| 398 | + * @throws InvalidDataTypeException |
|
| 399 | + * @throws InvalidInterfaceException |
|
| 400 | + * @throws RestException |
|
| 401 | + * @throws DomainException |
|
| 402 | + */ |
|
| 403 | + public function addRelation(EEM_Base $model, EE_Model_Relation_Base $relation, WP_REST_Request $request) |
|
| 404 | + { |
|
| 405 | + list($model_obj, $other_obj) = $this->getBothModelObjects($model, $relation, $request); |
|
| 406 | + $extra_params = array(); |
|
| 407 | + if ($relation instanceof EE_HABTM_Relation) { |
|
| 408 | + $extra_params = array_intersect_key( |
|
| 409 | + ModelDataTranslator::prepareConditionsQueryParamsForModels( |
|
| 410 | + $request->get_body_params(), |
|
| 411 | + $relation->get_join_model(), |
|
| 412 | + $this->getModelVersionInfo()->requestedVersion(), |
|
| 413 | + true |
|
| 414 | + ), |
|
| 415 | + $relation->getNonKeyFields() |
|
| 416 | + ); |
|
| 417 | + } |
|
| 418 | + // Add a relation. |
|
| 419 | + $related_obj = $model_obj->_add_relation_to( |
|
| 420 | + $other_obj, |
|
| 421 | + $relation->get_other_model()->get_this_model_name(), |
|
| 422 | + $extra_params |
|
| 423 | + ); |
|
| 424 | + $response = array( |
|
| 425 | + 'success' => $related_obj === $other_obj, |
|
| 426 | + strtolower($model->get_this_model_name()) => $this->returnModelObjAsJsonResponse($model_obj, $request), |
|
| 427 | + strtolower($relation->get_other_model()->get_this_model_name()) => $this->returnModelObjAsJsonResponse($related_obj, $request), |
|
| 428 | + ); |
|
| 429 | + if ($relation instanceof EE_HABTM_Relation) { |
|
| 430 | + $join_model_obj = $relation->get_join_model()->get_one( |
|
| 431 | + array( |
|
| 432 | + array( |
|
| 433 | + $model->primary_key_name() => $model_obj->ID(), |
|
| 434 | + $relation->get_other_model()->primary_key_name() => $related_obj->ID() |
|
| 435 | + ) |
|
| 436 | + ) |
|
| 437 | + ); |
|
| 438 | + $response['join'][ strtolower($relation->get_join_model()->get_this_model_name()) ] = $this->returnModelObjAsJsonResponse($join_model_obj, $request); |
|
| 439 | + } |
|
| 440 | + return $response; |
|
| 441 | + } |
|
| 442 | 442 | |
| 443 | 443 | |
| 444 | - /** |
|
| 445 | - * Removes the relation between the specified models (if it exists). |
|
| 446 | - * @since $VID:$ |
|
| 447 | - * @param WP_REST_Request $request |
|
| 448 | - * @return WP_REST_Response |
|
| 449 | - */ |
|
| 450 | - public static function handleRequestRemoveRelation(WP_REST_Request $request, $version, $model_name, $related_model_name) |
|
| 451 | - { |
|
| 452 | - $controller = new Write(); |
|
| 453 | - try { |
|
| 454 | - $controller->setRequestedVersion($version); |
|
| 455 | - $main_model = $controller->getModelVersionInfo()->loadModel($model_name); |
|
| 456 | - return $controller->sendResponse( |
|
| 457 | - $controller->removeRelation( |
|
| 458 | - $main_model, |
|
| 459 | - $main_model->related_settings_for($related_model_name), |
|
| 460 | - $request |
|
| 461 | - ) |
|
| 462 | - ); |
|
| 463 | - } catch (Exception $e) { |
|
| 464 | - return $controller->sendResponse($e); |
|
| 465 | - } |
|
| 466 | - } |
|
| 444 | + /** |
|
| 445 | + * Removes the relation between the specified models (if it exists). |
|
| 446 | + * @since $VID:$ |
|
| 447 | + * @param WP_REST_Request $request |
|
| 448 | + * @return WP_REST_Response |
|
| 449 | + */ |
|
| 450 | + public static function handleRequestRemoveRelation(WP_REST_Request $request, $version, $model_name, $related_model_name) |
|
| 451 | + { |
|
| 452 | + $controller = new Write(); |
|
| 453 | + try { |
|
| 454 | + $controller->setRequestedVersion($version); |
|
| 455 | + $main_model = $controller->getModelVersionInfo()->loadModel($model_name); |
|
| 456 | + return $controller->sendResponse( |
|
| 457 | + $controller->removeRelation( |
|
| 458 | + $main_model, |
|
| 459 | + $main_model->related_settings_for($related_model_name), |
|
| 460 | + $request |
|
| 461 | + ) |
|
| 462 | + ); |
|
| 463 | + } catch (Exception $e) { |
|
| 464 | + return $controller->sendResponse($e); |
|
| 465 | + } |
|
| 466 | + } |
|
| 467 | 467 | |
| 468 | - /** |
|
| 469 | - * Adds a relation between the two model specified model objects. |
|
| 470 | - * @since $VID:$ |
|
| 471 | - * @param EEM_Base $model |
|
| 472 | - * @param EE_Model_Relation_Base $relation |
|
| 473 | - * @param WP_REST_Request $request |
|
| 474 | - * @return array |
|
| 475 | - * @throws DomainException |
|
| 476 | - * @throws EE_Error |
|
| 477 | - * @throws InvalidArgumentException |
|
| 478 | - * @throws InvalidDataTypeException |
|
| 479 | - * @throws InvalidInterfaceException |
|
| 480 | - * @throws RestException |
|
| 481 | - */ |
|
| 482 | - public function removeRelation(EEM_Base $model, EE_Model_Relation_Base $relation, WP_REST_Request $request) |
|
| 483 | - { |
|
| 484 | - // This endpoint doesn't accept body parameters (it's understandable to think it might, so let developers know |
|
| 485 | - // up-front that it doesn't.) |
|
| 486 | - if (!empty($request->get_body_params())) { |
|
| 487 | - $body_params = $request->get_body_params(); |
|
| 488 | - throw new RestException( |
|
| 489 | - 'invalid_field', |
|
| 490 | - sprintf( |
|
| 491 | - esc_html__('This endpoint doesn\'t accept post body arguments, you sent in %1$s', 'event_espresso'), |
|
| 492 | - implode(array_keys($body_params)) |
|
| 493 | - ) |
|
| 494 | - ); |
|
| 495 | - } |
|
| 496 | - list($model_obj, $other_obj) = $this->getBothModelObjects($model, $relation, $request); |
|
| 497 | - // Remember the old relation, if it used a join entry. |
|
| 498 | - $join_model_obj = null; |
|
| 499 | - if ($relation instanceof EE_HABTM_Relation) { |
|
| 500 | - $join_model_obj = $relation->get_join_model()->get_one( |
|
| 501 | - array( |
|
| 502 | - array( |
|
| 503 | - $model->primary_key_name() => $model_obj->ID(), |
|
| 504 | - $relation->get_other_model()->primary_key_name() => $other_obj->ID() |
|
| 505 | - ) |
|
| 506 | - ) |
|
| 507 | - ); |
|
| 508 | - } |
|
| 509 | - // Remove the relation. |
|
| 510 | - $related_obj = $model_obj->_remove_relation_to( |
|
| 511 | - $other_obj, |
|
| 512 | - $relation->get_other_model()->get_this_model_name() |
|
| 513 | - ); |
|
| 514 | - $response = array( |
|
| 515 | - 'success' => $related_obj === $other_obj, |
|
| 516 | - strtolower($model->get_this_model_name()) => $this->returnModelObjAsJsonResponse($model_obj, $request), |
|
| 517 | - strtolower($relation->get_other_model()->get_this_model_name()) => $this->returnModelObjAsJsonResponse($related_obj, $request), |
|
| 518 | - ); |
|
| 519 | - if ($relation instanceof EE_HABTM_Relation) { |
|
| 520 | - $join_model_obj_after_removal = $relation->get_join_model()->get_one( |
|
| 521 | - array( |
|
| 522 | - array( |
|
| 523 | - $model->primary_key_name() => $model_obj->ID(), |
|
| 524 | - $relation->get_other_model()->primary_key_name() => $other_obj->ID() |
|
| 525 | - ) |
|
| 526 | - ) |
|
| 527 | - ); |
|
| 528 | - // If there's a join table, the measure of success is actually if the join row was removed. |
|
| 529 | - $response['success'] = $join_model_obj_after_removal ? false : true; |
|
| 530 | - if ($join_model_obj instanceof EE_Base_Class) { |
|
| 531 | - $response['join'][ strtolower($relation->get_join_model()->get_this_model_name()) ] = $this->returnModelObjAsJsonResponse($join_model_obj, $request); |
|
| 532 | - } else { |
|
| 533 | - $response['join'][ strtolower($relation->get_join_model()->get_this_model_name()) ] = null; |
|
| 534 | - } |
|
| 535 | - } |
|
| 536 | - return $response; |
|
| 537 | - } |
|
| 468 | + /** |
|
| 469 | + * Adds a relation between the two model specified model objects. |
|
| 470 | + * @since $VID:$ |
|
| 471 | + * @param EEM_Base $model |
|
| 472 | + * @param EE_Model_Relation_Base $relation |
|
| 473 | + * @param WP_REST_Request $request |
|
| 474 | + * @return array |
|
| 475 | + * @throws DomainException |
|
| 476 | + * @throws EE_Error |
|
| 477 | + * @throws InvalidArgumentException |
|
| 478 | + * @throws InvalidDataTypeException |
|
| 479 | + * @throws InvalidInterfaceException |
|
| 480 | + * @throws RestException |
|
| 481 | + */ |
|
| 482 | + public function removeRelation(EEM_Base $model, EE_Model_Relation_Base $relation, WP_REST_Request $request) |
|
| 483 | + { |
|
| 484 | + // This endpoint doesn't accept body parameters (it's understandable to think it might, so let developers know |
|
| 485 | + // up-front that it doesn't.) |
|
| 486 | + if (!empty($request->get_body_params())) { |
|
| 487 | + $body_params = $request->get_body_params(); |
|
| 488 | + throw new RestException( |
|
| 489 | + 'invalid_field', |
|
| 490 | + sprintf( |
|
| 491 | + esc_html__('This endpoint doesn\'t accept post body arguments, you sent in %1$s', 'event_espresso'), |
|
| 492 | + implode(array_keys($body_params)) |
|
| 493 | + ) |
|
| 494 | + ); |
|
| 495 | + } |
|
| 496 | + list($model_obj, $other_obj) = $this->getBothModelObjects($model, $relation, $request); |
|
| 497 | + // Remember the old relation, if it used a join entry. |
|
| 498 | + $join_model_obj = null; |
|
| 499 | + if ($relation instanceof EE_HABTM_Relation) { |
|
| 500 | + $join_model_obj = $relation->get_join_model()->get_one( |
|
| 501 | + array( |
|
| 502 | + array( |
|
| 503 | + $model->primary_key_name() => $model_obj->ID(), |
|
| 504 | + $relation->get_other_model()->primary_key_name() => $other_obj->ID() |
|
| 505 | + ) |
|
| 506 | + ) |
|
| 507 | + ); |
|
| 508 | + } |
|
| 509 | + // Remove the relation. |
|
| 510 | + $related_obj = $model_obj->_remove_relation_to( |
|
| 511 | + $other_obj, |
|
| 512 | + $relation->get_other_model()->get_this_model_name() |
|
| 513 | + ); |
|
| 514 | + $response = array( |
|
| 515 | + 'success' => $related_obj === $other_obj, |
|
| 516 | + strtolower($model->get_this_model_name()) => $this->returnModelObjAsJsonResponse($model_obj, $request), |
|
| 517 | + strtolower($relation->get_other_model()->get_this_model_name()) => $this->returnModelObjAsJsonResponse($related_obj, $request), |
|
| 518 | + ); |
|
| 519 | + if ($relation instanceof EE_HABTM_Relation) { |
|
| 520 | + $join_model_obj_after_removal = $relation->get_join_model()->get_one( |
|
| 521 | + array( |
|
| 522 | + array( |
|
| 523 | + $model->primary_key_name() => $model_obj->ID(), |
|
| 524 | + $relation->get_other_model()->primary_key_name() => $other_obj->ID() |
|
| 525 | + ) |
|
| 526 | + ) |
|
| 527 | + ); |
|
| 528 | + // If there's a join table, the measure of success is actually if the join row was removed. |
|
| 529 | + $response['success'] = $join_model_obj_after_removal ? false : true; |
|
| 530 | + if ($join_model_obj instanceof EE_Base_Class) { |
|
| 531 | + $response['join'][ strtolower($relation->get_join_model()->get_this_model_name()) ] = $this->returnModelObjAsJsonResponse($join_model_obj, $request); |
|
| 532 | + } else { |
|
| 533 | + $response['join'][ strtolower($relation->get_join_model()->get_this_model_name()) ] = null; |
|
| 534 | + } |
|
| 535 | + } |
|
| 536 | + return $response; |
|
| 537 | + } |
|
| 538 | 538 | |
| 539 | - /** |
|
| 540 | - * Gets the model objects indicated by the model, relation object, and request. |
|
| 541 | - * Throws an exception if the first object doesn't exist, and currently if the related object also doesn't exist. |
|
| 542 | - * However, this behaviour may change, as we may add support for simultaneously creating and relating data. |
|
| 543 | - * @since $VID:$ |
|
| 544 | - * @param EEM_Base $model |
|
| 545 | - * @param EE_Model_Relation_Base $relation |
|
| 546 | - * @param WP_REST_Request $request |
|
| 547 | - * @return array { |
|
| 548 | - * @type EE_Base_Class $model_obj |
|
| 549 | - * @type EE_Base_Class|null $other_model_obj |
|
| 550 | - * } |
|
| 551 | - * @throws RestException |
|
| 552 | - */ |
|
| 553 | - protected function getBothModelObjects(EEM_Base $model, EE_Model_Relation_Base $relation, WP_REST_Request $request) |
|
| 554 | - { |
|
| 555 | - // Check generic caps. For now, we're only allowing access to this endpoint to full admins. |
|
| 556 | - Capabilities::verifyAtLeastPartialAccessTo($model, EEM_Base::caps_edit, 'edit'); |
|
| 557 | - $default_cap_to_check_for = EE_Restriction_Generator_Base::get_default_restrictions_cap(); |
|
| 558 | - if (! current_user_can($default_cap_to_check_for)) { |
|
| 559 | - throw new RestException( |
|
| 560 | - 'rest_cannot_edit_' . EEH_Inflector::pluralize_and_lower(($model->get_this_model_name())), |
|
| 561 | - sprintf( |
|
| 562 | - esc_html__( |
|
| 563 | - // @codingStandardsIgnoreStart |
|
| 564 | - 'For now, only those with the admin capability to "%1$s" are allowed to use the REST API to add relations in Event Espresso.', |
|
| 565 | - // @codingStandardsIgnoreEnd |
|
| 566 | - 'event_espresso' |
|
| 567 | - ), |
|
| 568 | - $default_cap_to_check_for |
|
| 569 | - ), |
|
| 570 | - array('status' => 403) |
|
| 571 | - ); |
|
| 572 | - } |
|
| 573 | - // Get the main model object. |
|
| 574 | - $model_obj = $this->getOneOrThrowException($model, $request->get_param('id')); |
|
| 575 | - // For now, we require the other model object to exist too. This might be relaxed later. |
|
| 576 | - $other_obj = $this->getOneOrThrowException($relation->get_other_model(), $request->get_param('related_id')); |
|
| 577 | - return array($model_obj,$other_obj); |
|
| 578 | - } |
|
| 539 | + /** |
|
| 540 | + * Gets the model objects indicated by the model, relation object, and request. |
|
| 541 | + * Throws an exception if the first object doesn't exist, and currently if the related object also doesn't exist. |
|
| 542 | + * However, this behaviour may change, as we may add support for simultaneously creating and relating data. |
|
| 543 | + * @since $VID:$ |
|
| 544 | + * @param EEM_Base $model |
|
| 545 | + * @param EE_Model_Relation_Base $relation |
|
| 546 | + * @param WP_REST_Request $request |
|
| 547 | + * @return array { |
|
| 548 | + * @type EE_Base_Class $model_obj |
|
| 549 | + * @type EE_Base_Class|null $other_model_obj |
|
| 550 | + * } |
|
| 551 | + * @throws RestException |
|
| 552 | + */ |
|
| 553 | + protected function getBothModelObjects(EEM_Base $model, EE_Model_Relation_Base $relation, WP_REST_Request $request) |
|
| 554 | + { |
|
| 555 | + // Check generic caps. For now, we're only allowing access to this endpoint to full admins. |
|
| 556 | + Capabilities::verifyAtLeastPartialAccessTo($model, EEM_Base::caps_edit, 'edit'); |
|
| 557 | + $default_cap_to_check_for = EE_Restriction_Generator_Base::get_default_restrictions_cap(); |
|
| 558 | + if (! current_user_can($default_cap_to_check_for)) { |
|
| 559 | + throw new RestException( |
|
| 560 | + 'rest_cannot_edit_' . EEH_Inflector::pluralize_and_lower(($model->get_this_model_name())), |
|
| 561 | + sprintf( |
|
| 562 | + esc_html__( |
|
| 563 | + // @codingStandardsIgnoreStart |
|
| 564 | + 'For now, only those with the admin capability to "%1$s" are allowed to use the REST API to add relations in Event Espresso.', |
|
| 565 | + // @codingStandardsIgnoreEnd |
|
| 566 | + 'event_espresso' |
|
| 567 | + ), |
|
| 568 | + $default_cap_to_check_for |
|
| 569 | + ), |
|
| 570 | + array('status' => 403) |
|
| 571 | + ); |
|
| 572 | + } |
|
| 573 | + // Get the main model object. |
|
| 574 | + $model_obj = $this->getOneOrThrowException($model, $request->get_param('id')); |
|
| 575 | + // For now, we require the other model object to exist too. This might be relaxed later. |
|
| 576 | + $other_obj = $this->getOneOrThrowException($relation->get_other_model(), $request->get_param('related_id')); |
|
| 577 | + return array($model_obj,$other_obj); |
|
| 578 | + } |
|
| 579 | 579 | |
| 580 | - /** |
|
| 581 | - * Gets the model with that ID or throws a REST exception. |
|
| 582 | - * @since $VID:$ |
|
| 583 | - * @param EEM_Base $model |
|
| 584 | - * @param $id |
|
| 585 | - * @return EE_Base_Class |
|
| 586 | - * @throws RestException |
|
| 587 | - */ |
|
| 588 | - protected function getOneOrThrowException(EEM_Base $model, $id) |
|
| 589 | - { |
|
| 590 | - $model_obj = $model->get_one_by_ID($id); |
|
| 591 | - // @todo: check they can permission for it. For now unnecessary because only full admins can use this endpoint. |
|
| 592 | - if ($model_obj instanceof EE_Base_Class) { |
|
| 593 | - return $model_obj; |
|
| 594 | - } |
|
| 595 | - $lowercase_model_name = strtolower($model->get_this_model_name()); |
|
| 596 | - throw new RestException( |
|
| 597 | - sprintf('rest_%s_invalid_id', $lowercase_model_name), |
|
| 598 | - sprintf(__('Invalid %s ID.', 'event_espresso'), $lowercase_model_name), |
|
| 599 | - array('status' => 404) |
|
| 600 | - ); |
|
| 601 | - } |
|
| 580 | + /** |
|
| 581 | + * Gets the model with that ID or throws a REST exception. |
|
| 582 | + * @since $VID:$ |
|
| 583 | + * @param EEM_Base $model |
|
| 584 | + * @param $id |
|
| 585 | + * @return EE_Base_Class |
|
| 586 | + * @throws RestException |
|
| 587 | + */ |
|
| 588 | + protected function getOneOrThrowException(EEM_Base $model, $id) |
|
| 589 | + { |
|
| 590 | + $model_obj = $model->get_one_by_ID($id); |
|
| 591 | + // @todo: check they can permission for it. For now unnecessary because only full admins can use this endpoint. |
|
| 592 | + if ($model_obj instanceof EE_Base_Class) { |
|
| 593 | + return $model_obj; |
|
| 594 | + } |
|
| 595 | + $lowercase_model_name = strtolower($model->get_this_model_name()); |
|
| 596 | + throw new RestException( |
|
| 597 | + sprintf('rest_%s_invalid_id', $lowercase_model_name), |
|
| 598 | + sprintf(__('Invalid %s ID.', 'event_espresso'), $lowercase_model_name), |
|
| 599 | + array('status' => 404) |
|
| 600 | + ); |
|
| 601 | + } |
|
| 602 | 602 | } |
@@ -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()); |
@@ -435,7 +435,7 @@ discard block |
||
| 435 | 435 | ) |
| 436 | 436 | ) |
| 437 | 437 | ); |
| 438 | - $response['join'][ strtolower($relation->get_join_model()->get_this_model_name()) ] = $this->returnModelObjAsJsonResponse($join_model_obj, $request); |
|
| 438 | + $response['join'][strtolower($relation->get_join_model()->get_this_model_name())] = $this->returnModelObjAsJsonResponse($join_model_obj, $request); |
|
| 439 | 439 | } |
| 440 | 440 | return $response; |
| 441 | 441 | } |
@@ -483,7 +483,7 @@ discard block |
||
| 483 | 483 | { |
| 484 | 484 | // This endpoint doesn't accept body parameters (it's understandable to think it might, so let developers know |
| 485 | 485 | // up-front that it doesn't.) |
| 486 | - if (!empty($request->get_body_params())) { |
|
| 486 | + if ( ! empty($request->get_body_params())) { |
|
| 487 | 487 | $body_params = $request->get_body_params(); |
| 488 | 488 | throw new RestException( |
| 489 | 489 | 'invalid_field', |
@@ -528,9 +528,9 @@ discard block |
||
| 528 | 528 | // If there's a join table, the measure of success is actually if the join row was removed. |
| 529 | 529 | $response['success'] = $join_model_obj_after_removal ? false : true; |
| 530 | 530 | if ($join_model_obj instanceof EE_Base_Class) { |
| 531 | - $response['join'][ strtolower($relation->get_join_model()->get_this_model_name()) ] = $this->returnModelObjAsJsonResponse($join_model_obj, $request); |
|
| 531 | + $response['join'][strtolower($relation->get_join_model()->get_this_model_name())] = $this->returnModelObjAsJsonResponse($join_model_obj, $request); |
|
| 532 | 532 | } else { |
| 533 | - $response['join'][ strtolower($relation->get_join_model()->get_this_model_name()) ] = null; |
|
| 533 | + $response['join'][strtolower($relation->get_join_model()->get_this_model_name())] = null; |
|
| 534 | 534 | } |
| 535 | 535 | } |
| 536 | 536 | return $response; |
@@ -555,9 +555,9 @@ discard block |
||
| 555 | 555 | // Check generic caps. For now, we're only allowing access to this endpoint to full admins. |
| 556 | 556 | Capabilities::verifyAtLeastPartialAccessTo($model, EEM_Base::caps_edit, 'edit'); |
| 557 | 557 | $default_cap_to_check_for = EE_Restriction_Generator_Base::get_default_restrictions_cap(); |
| 558 | - if (! current_user_can($default_cap_to_check_for)) { |
|
| 558 | + if ( ! current_user_can($default_cap_to_check_for)) { |
|
| 559 | 559 | throw new RestException( |
| 560 | - 'rest_cannot_edit_' . EEH_Inflector::pluralize_and_lower(($model->get_this_model_name())), |
|
| 560 | + 'rest_cannot_edit_'.EEH_Inflector::pluralize_and_lower(($model->get_this_model_name())), |
|
| 561 | 561 | sprintf( |
| 562 | 562 | esc_html__( |
| 563 | 563 | // @codingStandardsIgnoreStart |
@@ -574,7 +574,7 @@ discard block |
||
| 574 | 574 | $model_obj = $this->getOneOrThrowException($model, $request->get_param('id')); |
| 575 | 575 | // For now, we require the other model object to exist too. This might be relaxed later. |
| 576 | 576 | $other_obj = $this->getOneOrThrowException($relation->get_other_model(), $request->get_param('related_id')); |
| 577 | - return array($model_obj,$other_obj); |
|
| 577 | + return array($model_obj, $other_obj); |
|
| 578 | 578 | } |
| 579 | 579 | |
| 580 | 580 | /** |