Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Read often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Read, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 51 | class Read extends Base |
||
| 52 | { |
||
| 53 | |||
| 54 | |||
| 55 | /** |
||
| 56 | * @var CalculatedModelFields |
||
| 57 | */ |
||
| 58 | protected $fields_calculator; |
||
| 59 | |||
| 60 | |||
| 61 | /** |
||
| 62 | * Read constructor. |
||
| 63 | * @param CalculatedModelFields $fields_calculator |
||
| 64 | */ |
||
| 65 | public function __construct(CalculatedModelFields $fields_calculator) |
||
| 66 | { |
||
| 67 | parent::__construct(); |
||
| 68 | $this->fields_calculator = $fields_calculator; |
||
| 69 | } |
||
| 70 | |||
| 71 | |||
| 72 | /** |
||
| 73 | * Handles requests to get all (or a filtered subset) of entities for a particular model |
||
| 74 | * |
||
| 75 | * @param WP_REST_Request $request |
||
| 76 | * @param string $version |
||
| 77 | * @param string $model_name |
||
| 78 | * @return WP_REST_Response|WP_Error |
||
| 79 | * @throws InvalidArgumentException |
||
| 80 | * @throws InvalidDataTypeException |
||
| 81 | * @throws InvalidInterfaceException |
||
| 82 | */ |
||
| 83 | View Code Duplication | public static function handleRequestGetAll(WP_REST_Request $request, $version, $model_name) |
|
| 84 | { |
||
| 85 | $controller = LoaderFactory::getLoader()->getNew('EventEspresso\core\libraries\rest_api\controllers\model\Read'); |
||
| 86 | try { |
||
| 87 | $controller->setRequestedVersion($version); |
||
| 88 | if (! $controller->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
||
| 89 | return $controller->sendResponse( |
||
| 90 | new WP_Error( |
||
| 91 | 'endpoint_parsing_error', |
||
| 92 | sprintf( |
||
| 93 | __( |
||
| 94 | 'There is no model for endpoint %s. Please contact event espresso support', |
||
| 95 | 'event_espresso' |
||
| 96 | ), |
||
| 97 | $model_name |
||
| 98 | ) |
||
| 99 | ) |
||
| 100 | ); |
||
| 101 | } |
||
| 102 | return $controller->sendResponse( |
||
| 103 | $controller->getEntitiesFromModel( |
||
| 104 | $controller->getModelVersionInfo()->loadModel($model_name), |
||
| 105 | $request |
||
| 106 | ) |
||
| 107 | ); |
||
| 108 | } catch (Exception $e) { |
||
| 109 | return $controller->sendResponse($e); |
||
| 110 | } |
||
| 111 | } |
||
| 112 | |||
| 113 | |||
| 114 | /** |
||
| 115 | * Prepares and returns schema for any OPTIONS request. |
||
| 116 | * |
||
| 117 | * @param string $version The API endpoint version being used. |
||
| 118 | * @param string $model_name Something like `Event` or `Registration` |
||
| 119 | * @return array |
||
| 120 | * @throws InvalidArgumentException |
||
| 121 | * @throws InvalidDataTypeException |
||
| 122 | * @throws InvalidInterfaceException |
||
| 123 | */ |
||
| 124 | public static function handleSchemaRequest($version, $model_name) |
||
| 125 | { |
||
| 126 | $controller = LoaderFactory::getLoader()->getNew('EventEspresso\core\libraries\rest_api\controllers\model\Read'); |
||
| 127 | try { |
||
| 128 | $controller->setRequestedVersion($version); |
||
| 129 | if (! $controller->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
||
| 130 | return array(); |
||
| 131 | } |
||
| 132 | // get the model for this version |
||
| 133 | $model = $controller->getModelVersionInfo()->loadModel($model_name); |
||
| 134 | $model_schema = new JsonModelSchema($model, LoaderFactory::getLoader()->getShared('EventEspresso\core\libraries\rest_api\CalculatedModelFields')); |
||
| 135 | return $model_schema->getModelSchemaForRelations( |
||
| 136 | $controller->getModelVersionInfo()->relationSettings($model), |
||
| 137 | $controller->customizeSchemaForRestResponse( |
||
| 138 | $model, |
||
| 139 | $model_schema->getModelSchemaForFields( |
||
| 140 | $controller->getModelVersionInfo()->fieldsOnModelInThisVersion($model), |
||
| 141 | $model_schema->getInitialSchemaStructure() |
||
| 142 | ) |
||
| 143 | ) |
||
| 144 | ); |
||
| 145 | } catch (Exception $e) { |
||
| 146 | return array(); |
||
| 147 | } |
||
| 148 | } |
||
| 149 | |||
| 150 | |||
| 151 | /** |
||
| 152 | * This loops through each field in the given schema for the model and does the following: |
||
| 153 | * - add any extra fields that are REST API specific and related to existing fields. |
||
| 154 | * - transform default values into the correct format for a REST API response. |
||
| 155 | * |
||
| 156 | * @param EEM_Base $model |
||
| 157 | * @param array $schema |
||
| 158 | * @return array The final schema. |
||
| 159 | * @throws EE_Error |
||
| 160 | */ |
||
| 161 | public function customizeSchemaForRestResponse(EEM_Base $model, array $schema) |
||
| 162 | { |
||
| 163 | foreach ($this->getModelVersionInfo()->fieldsOnModelInThisVersion($model) as $field_name => $field) { |
||
| 164 | $schema = $this->translateDefaultsForRestResponse( |
||
| 165 | $field_name, |
||
| 166 | $field, |
||
| 167 | $this->maybeAddExtraFieldsToSchema($field_name, $field, $schema) |
||
| 168 | ); |
||
| 169 | } |
||
| 170 | return $schema; |
||
| 171 | } |
||
| 172 | |||
| 173 | |||
| 174 | /** |
||
| 175 | * This is used to ensure that the 'default' value set in the schema response is formatted correctly for the REST |
||
| 176 | * response. |
||
| 177 | * |
||
| 178 | * @param $field_name |
||
| 179 | * @param EE_Model_Field_Base $field |
||
| 180 | * @param array $schema |
||
| 181 | * @return array |
||
| 182 | * @throws RestException if a default value has a PHP object, which should never do (and if we |
||
| 183 | * @throws EE_Error |
||
| 184 | * did, let's know about it ASAP, so let the exception bubble up) |
||
| 185 | */ |
||
| 186 | protected function translateDefaultsForRestResponse($field_name, EE_Model_Field_Base $field, array $schema) |
||
| 187 | { |
||
| 188 | if (isset($schema['properties'][ $field_name ]['default'])) { |
||
| 189 | if (is_array($schema['properties'][ $field_name ]['default'])) { |
||
| 190 | foreach ($schema['properties'][ $field_name ]['default'] as $default_key => $default_value) { |
||
| 191 | View Code Duplication | if ($default_key === 'raw') { |
|
| 192 | $schema['properties'][ $field_name ]['default'][ $default_key ] = |
||
| 193 | ModelDataTranslator::prepareFieldValueForJson( |
||
| 194 | $field, |
||
| 195 | $default_value, |
||
| 196 | $this->getModelVersionInfo()->requestedVersion() |
||
| 197 | ); |
||
| 198 | } |
||
| 199 | } |
||
| 200 | View Code Duplication | } else { |
|
| 201 | $schema['properties'][ $field_name ]['default'] = ModelDataTranslator::prepareFieldValueForJson( |
||
| 202 | $field, |
||
| 203 | $schema['properties'][ $field_name ]['default'], |
||
| 204 | $this->getModelVersionInfo()->requestedVersion() |
||
| 205 | ); |
||
| 206 | } |
||
| 207 | } |
||
| 208 | return $schema; |
||
| 209 | } |
||
| 210 | |||
| 211 | |||
| 212 | /** |
||
| 213 | * Adds additional fields to the schema |
||
| 214 | * The REST API returns a GMT value field for each datetime field in the resource. Thus the description about this |
||
| 215 | * needs to be added to the schema. |
||
| 216 | * |
||
| 217 | * @param $field_name |
||
| 218 | * @param EE_Model_Field_Base $field |
||
| 219 | * @param array $schema |
||
| 220 | * @return array |
||
| 221 | */ |
||
| 222 | protected function maybeAddExtraFieldsToSchema($field_name, EE_Model_Field_Base $field, array $schema) |
||
| 223 | { |
||
| 224 | if ($field instanceof EE_Datetime_Field) { |
||
| 225 | $schema['properties'][ $field_name . '_gmt' ] = $field->getSchema(); |
||
| 226 | // modify the description |
||
| 227 | $schema['properties'][ $field_name . '_gmt' ]['description'] = sprintf( |
||
| 228 | esc_html__('%s - the value for this field is in GMT.', 'event_espresso'), |
||
| 229 | wp_specialchars_decode($field->get_nicename(), ENT_QUOTES) |
||
| 230 | ); |
||
| 231 | } |
||
| 232 | return $schema; |
||
| 233 | } |
||
| 234 | |||
| 235 | |||
| 236 | /** |
||
| 237 | * Used to figure out the route from the request when a `WP_REST_Request` object is not available |
||
| 238 | * |
||
| 239 | * @return string |
||
| 240 | */ |
||
| 241 | protected function getRouteFromRequest() |
||
| 242 | { |
||
| 243 | if (isset($GLOBALS['wp']) |
||
| 244 | && $GLOBALS['wp'] instanceof WP |
||
|
|
|||
| 245 | && isset($GLOBALS['wp']->query_vars['rest_route']) |
||
| 246 | ) { |
||
| 247 | return $GLOBALS['wp']->query_vars['rest_route']; |
||
| 248 | } |
||
| 249 | return isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '/'; |
||
| 250 | } |
||
| 251 | |||
| 252 | |||
| 253 | /** |
||
| 254 | * Gets a single entity related to the model indicated in the path and its id |
||
| 255 | * |
||
| 256 | * @param WP_REST_Request $request |
||
| 257 | * @param string $version |
||
| 258 | * @param string $model_name |
||
| 259 | * @return WP_REST_Response|WP_Error |
||
| 260 | * @throws InvalidDataTypeException |
||
| 261 | * @throws InvalidInterfaceException |
||
| 262 | * @throws InvalidArgumentException |
||
| 263 | */ |
||
| 264 | View Code Duplication | public static function handleRequestGetOne(WP_REST_Request $request, $version, $model_name) |
|
| 265 | { |
||
| 266 | $controller = LoaderFactory::getLoader()->getNew('EventEspresso\core\libraries\rest_api\controllers\model\Read'); |
||
| 267 | try { |
||
| 268 | $controller->setRequestedVersion($version); |
||
| 269 | if (! $controller->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
||
| 270 | return $controller->sendResponse( |
||
| 271 | new WP_Error( |
||
| 272 | 'endpoint_parsing_error', |
||
| 273 | sprintf( |
||
| 274 | __( |
||
| 275 | 'There is no model for endpoint %s. Please contact event espresso support', |
||
| 276 | 'event_espresso' |
||
| 277 | ), |
||
| 278 | $model_name |
||
| 279 | ) |
||
| 280 | ) |
||
| 281 | ); |
||
| 282 | } |
||
| 283 | return $controller->sendResponse( |
||
| 284 | $controller->getEntityFromModel( |
||
| 285 | $controller->getModelVersionInfo()->loadModel($model_name), |
||
| 286 | $request |
||
| 287 | ) |
||
| 288 | ); |
||
| 289 | } catch (Exception $e) { |
||
| 290 | return $controller->sendResponse($e); |
||
| 291 | } |
||
| 292 | } |
||
| 293 | |||
| 294 | |||
| 295 | /** |
||
| 296 | * Gets all the related entities (or if its a belongs-to relation just the one) |
||
| 297 | * to the item with the given id |
||
| 298 | * |
||
| 299 | * @param WP_REST_Request $request |
||
| 300 | * @param string $version |
||
| 301 | * @param string $model_name |
||
| 302 | * @param string $related_model_name |
||
| 303 | * @return WP_REST_Response|WP_Error |
||
| 304 | * @throws InvalidDataTypeException |
||
| 305 | * @throws InvalidInterfaceException |
||
| 306 | * @throws InvalidArgumentException |
||
| 307 | */ |
||
| 308 | public static function handleRequestGetRelated( |
||
| 309 | WP_REST_Request $request, |
||
| 310 | $version, |
||
| 311 | $model_name, |
||
| 312 | $related_model_name |
||
| 313 | ) { |
||
| 314 | $controller = LoaderFactory::getLoader()->getNew('EventEspresso\core\libraries\rest_api\controllers\model\Read'); |
||
| 315 | try { |
||
| 316 | $controller->setRequestedVersion($version); |
||
| 317 | $main_model = $controller->validateModel($model_name); |
||
| 318 | $controller->validateModel($related_model_name); |
||
| 319 | return $controller->sendResponse( |
||
| 320 | $controller->getEntitiesFromRelation( |
||
| 321 | $request->get_param('id'), |
||
| 322 | $main_model->related_settings_for($related_model_name), |
||
| 323 | $request |
||
| 324 | ) |
||
| 325 | ); |
||
| 326 | } catch (Exception $e) { |
||
| 327 | return $controller->sendResponse($e); |
||
| 328 | } |
||
| 329 | } |
||
| 330 | |||
| 331 | |||
| 332 | /** |
||
| 333 | * Gets a collection for the given model and filters |
||
| 334 | * |
||
| 335 | * @param EEM_Base $model |
||
| 336 | * @param WP_REST_Request $request |
||
| 337 | * @return array |
||
| 338 | * @throws DomainException |
||
| 339 | * @throws EE_Error |
||
| 340 | * @throws InvalidArgumentException |
||
| 341 | * @throws InvalidDataTypeException |
||
| 342 | * @throws InvalidInterfaceException |
||
| 343 | * @throws ModelConfigurationException |
||
| 344 | * @throws ReflectionException |
||
| 345 | * @throws RestException |
||
| 346 | * @throws RestPasswordIncorrectException |
||
| 347 | * @throws RestPasswordRequiredException |
||
| 348 | * @throws UnexpectedEntityException |
||
| 349 | */ |
||
| 350 | public function getEntitiesFromModel($model, $request) |
||
| 351 | { |
||
| 352 | $query_params = $this->createModelQueryParams($model, $request->get_params()); |
||
| 353 | if (! Capabilities::currentUserHasPartialAccessTo($model, $query_params['caps'])) { |
||
| 354 | $model_name_plural = EEH_Inflector::pluralize_and_lower($model->get_this_model_name()); |
||
| 355 | throw new RestException( |
||
| 356 | sprintf('rest_%s_cannot_list', $model_name_plural), |
||
| 357 | sprintf( |
||
| 358 | __('Sorry, you are not allowed to list %1$s. Missing permissions: %2$s', 'event_espresso'), |
||
| 359 | $model_name_plural, |
||
| 360 | Capabilities::getMissingPermissionsString($model, $query_params['caps']) |
||
| 361 | ), |
||
| 362 | array('status' => 403) |
||
| 363 | ); |
||
| 364 | } |
||
| 365 | if (! $request->get_header('no_rest_headers')) { |
||
| 366 | $this->setHeadersFromQueryParams($model, $query_params); |
||
| 367 | } |
||
| 368 | /** @type array $results */ |
||
| 369 | $results = $model->get_all_wpdb_results($query_params); |
||
| 370 | $nice_results = array(); |
||
| 371 | foreach ($results as $result) { |
||
| 372 | $nice_results[] = $this->createEntityFromWpdbResult( |
||
| 373 | $model, |
||
| 374 | $result, |
||
| 375 | $request |
||
| 376 | ); |
||
| 377 | } |
||
| 378 | return $nice_results; |
||
| 379 | } |
||
| 380 | |||
| 381 | |||
| 382 | /** |
||
| 383 | * Gets the collection for given relation object |
||
| 384 | * The same as Read::get_entities_from_model(), except if the relation |
||
| 385 | * is a HABTM relation, in which case it merges any non-foreign-key fields from |
||
| 386 | * the join-model-object into the results |
||
| 387 | * |
||
| 388 | * @param array $primary_model_query_params query params for finding the item from which |
||
| 389 | * relations will be based |
||
| 390 | * @param EE_Model_Relation_Base $relation |
||
| 391 | * @param WP_REST_Request $request |
||
| 392 | * @return array |
||
| 393 | * @throws DomainException |
||
| 394 | * @throws EE_Error |
||
| 395 | * @throws InvalidArgumentException |
||
| 396 | * @throws InvalidDataTypeException |
||
| 397 | * @throws InvalidInterfaceException |
||
| 398 | * @throws ModelConfigurationException |
||
| 399 | * @throws ReflectionException |
||
| 400 | * @throws RestException |
||
| 401 | * @throws RestPasswordIncorrectException |
||
| 402 | * @throws RestPasswordRequiredException |
||
| 403 | * @throws UnexpectedEntityException |
||
| 404 | */ |
||
| 405 | protected function getEntitiesFromRelationUsingModelQueryParams($primary_model_query_params, $relation, $request) |
||
| 406 | { |
||
| 407 | $context = $this->validateContext($request->get_param('caps')); |
||
| 408 | $model = $relation->get_this_model(); |
||
| 409 | $related_model = $relation->get_other_model(); |
||
| 410 | if (! isset($primary_model_query_params[0])) { |
||
| 411 | $primary_model_query_params[0] = array(); |
||
| 412 | } |
||
| 413 | // check if they can access the 1st model object |
||
| 414 | $primary_model_query_params = array( |
||
| 415 | 0 => $primary_model_query_params[0], |
||
| 416 | 'limit' => 1, |
||
| 417 | ); |
||
| 418 | if ($model instanceof EEM_Soft_Delete_Base) { |
||
| 419 | $primary_model_query_params = $model->alter_query_params_so_deleted_and_undeleted_items_included( |
||
| 420 | $primary_model_query_params |
||
| 421 | ); |
||
| 422 | } |
||
| 423 | $restricted_query_params = $primary_model_query_params; |
||
| 424 | $restricted_query_params['caps'] = $context; |
||
| 425 | $restricted_query_params['limit'] = 1; |
||
| 426 | $this->setDebugInfo('main model query params', $restricted_query_params); |
||
| 427 | $this->setDebugInfo('missing caps', Capabilities::getMissingPermissionsString($related_model, $context)); |
||
| 428 | $primary_model_rows = $model->get_all_wpdb_results($restricted_query_params); |
||
| 429 | $primary_model_row = null; |
||
| 430 | if (is_array($primary_model_rows)) { |
||
| 431 | $primary_model_row = reset($primary_model_rows); |
||
| 432 | } |
||
| 433 | if (! ( |
||
| 434 | $primary_model_row |
||
| 435 | && Capabilities::currentUserHasPartialAccessTo($related_model, $context) |
||
| 436 | ) |
||
| 437 | ) { |
||
| 438 | if ($relation instanceof EE_Belongs_To_Relation) { |
||
| 439 | $related_model_name_maybe_plural = strtolower($related_model->get_this_model_name()); |
||
| 440 | } else { |
||
| 441 | $related_model_name_maybe_plural = EEH_Inflector::pluralize_and_lower( |
||
| 442 | $related_model->get_this_model_name() |
||
| 443 | ); |
||
| 444 | } |
||
| 445 | throw new RestException( |
||
| 446 | sprintf('rest_%s_cannot_list', $related_model_name_maybe_plural), |
||
| 447 | sprintf( |
||
| 448 | __( |
||
| 449 | 'Sorry, you are not allowed to list %1$s related to %2$s. Missing permissions: %3$s', |
||
| 450 | 'event_espresso' |
||
| 451 | ), |
||
| 452 | $related_model_name_maybe_plural, |
||
| 453 | $relation->get_this_model()->get_this_model_name(), |
||
| 454 | implode( |
||
| 455 | ',', |
||
| 456 | array_keys( |
||
| 457 | Capabilities::getMissingPermissions($related_model, $context) |
||
| 458 | ) |
||
| 459 | ) |
||
| 460 | ), |
||
| 461 | array('status' => 403) |
||
| 462 | ); |
||
| 463 | } |
||
| 464 | |||
| 465 | $this->checkPassword( |
||
| 466 | $model, |
||
| 467 | $primary_model_row, |
||
| 468 | $restricted_query_params, |
||
| 469 | $request |
||
| 470 | ); |
||
| 471 | $query_params = $this->createModelQueryParams($relation->get_other_model(), $request->get_params()); |
||
| 472 | foreach ($primary_model_query_params[0] as $where_condition_key => $where_condition_value) { |
||
| 473 | $query_params[0][ $relation->get_this_model()->get_this_model_name() |
||
| 474 | . '.' |
||
| 475 | . $where_condition_key ] = $where_condition_value; |
||
| 476 | } |
||
| 477 | $query_params['default_where_conditions'] = 'none'; |
||
| 478 | $query_params['caps'] = $context; |
||
| 479 | if (! $request->get_header('no_rest_headers')) { |
||
| 480 | $this->setHeadersFromQueryParams($relation->get_other_model(), $query_params); |
||
| 481 | } |
||
| 482 | /** @type array $results */ |
||
| 483 | $results = $relation->get_other_model()->get_all_wpdb_results($query_params); |
||
| 484 | $nice_results = array(); |
||
| 485 | foreach ($results as $result) { |
||
| 486 | $nice_result = $this->createEntityFromWpdbResult( |
||
| 487 | $relation->get_other_model(), |
||
| 488 | $result, |
||
| 489 | $request |
||
| 490 | ); |
||
| 491 | if ($relation instanceof EE_HABTM_Relation) { |
||
| 492 | // put the unusual stuff (properties from the HABTM relation) first, and make sure |
||
| 493 | // if there are conflicts we prefer the properties from the main model |
||
| 494 | $join_model_result = $this->createEntityFromWpdbResult( |
||
| 495 | $relation->get_join_model(), |
||
| 496 | $result, |
||
| 497 | $request |
||
| 498 | ); |
||
| 499 | $joined_result = array_merge($join_model_result, $nice_result); |
||
| 500 | // but keep the meta stuff from the main model |
||
| 501 | if (isset($nice_result['meta'])) { |
||
| 502 | $joined_result['meta'] = $nice_result['meta']; |
||
| 503 | } |
||
| 504 | $nice_result = $joined_result; |
||
| 505 | } |
||
| 506 | $nice_results[] = $nice_result; |
||
| 507 | } |
||
| 508 | if ($relation instanceof EE_Belongs_To_Relation) { |
||
| 509 | return array_shift($nice_results); |
||
| 510 | } else { |
||
| 511 | return $nice_results; |
||
| 512 | } |
||
| 513 | } |
||
| 514 | |||
| 515 | |||
| 516 | /** |
||
| 517 | * Gets the collection for given relation object |
||
| 518 | * The same as Read::get_entities_from_model(), except if the relation |
||
| 519 | * is a HABTM relation, in which case it merges any non-foreign-key fields from |
||
| 520 | * the join-model-object into the results |
||
| 521 | * |
||
| 522 | * @param string $id the ID of the thing we are fetching related stuff from |
||
| 523 | * @param EE_Model_Relation_Base $relation |
||
| 524 | * @param WP_REST_Request $request |
||
| 525 | * @return array |
||
| 526 | * @throws DomainException |
||
| 527 | * @throws EE_Error |
||
| 528 | * @throws InvalidArgumentException |
||
| 529 | * @throws InvalidDataTypeException |
||
| 530 | * @throws InvalidInterfaceException |
||
| 531 | * @throws ModelConfigurationException |
||
| 532 | * @throws ReflectionException |
||
| 533 | * @throws RestException |
||
| 534 | * @throws RestPasswordIncorrectException |
||
| 535 | * @throws RestPasswordRequiredException |
||
| 536 | * @throws UnexpectedEntityException |
||
| 537 | */ |
||
| 538 | public function getEntitiesFromRelation($id, $relation, $request) |
||
| 566 | |||
| 567 | |||
| 568 | /** |
||
| 569 | * Sets the headers that are based on the model and query params, |
||
| 570 | * like the total records. This should only be called on the original request |
||
| 571 | * from the client, not on subsequent internal |
||
| 572 | * |
||
| 573 | * @param EEM_Base $model |
||
| 574 | * @param array $query_params |
||
| 575 | * @return void |
||
| 576 | * @throws EE_Error |
||
| 577 | */ |
||
| 578 | protected function setHeadersFromQueryParams($model, $query_params) |
||
| 606 | |||
| 607 | |||
| 608 | /** |
||
| 609 | * Changes database results into REST API entities |
||
| 610 | * |
||
| 611 | * @param EEM_Base $model |
||
| 612 | * @param array $db_row like results from $wpdb->get_results() |
||
| 613 | * @param WP_REST_Request $rest_request |
||
| 614 | * @param string $deprecated no longer used |
||
| 615 | * @return array ready for being converted into json for sending to client |
||
| 616 | * @throws EE_Error |
||
| 617 | * @throws InvalidArgumentException |
||
| 618 | * @throws InvalidDataTypeException |
||
| 619 | * @throws InvalidInterfaceException |
||
| 620 | * @throws ReflectionException |
||
| 621 | * @throws RestException |
||
| 622 | * @throws RestPasswordIncorrectException |
||
| 623 | * @throws RestPasswordRequiredException |
||
| 624 | * @throws ModelConfigurationException |
||
| 625 | * @throws UnexpectedEntityException |
||
| 626 | * @throws DomainException |
||
| 627 | */ |
||
| 628 | public function createEntityFromWpdbResult($model, $db_row, $rest_request, $deprecated = null) |
||
| 726 | |||
| 727 | |||
| 728 | /** |
||
| 729 | * Returns an array describing which fields can be protected, and which actually were removed this request |
||
| 730 | * |
||
| 731 | * @param $model |
||
| 732 | * @param $results_so_far |
||
| 733 | * @param $protected |
||
| 734 | * @return array results |
||
| 735 | * @throws EE_Error |
||
| 736 | * @since 4.9.74.p |
||
| 737 | */ |
||
| 738 | protected function addProtectedProperty(EEM_Base $model, $results_so_far, $protected) |
||
| 758 | |||
| 759 | |||
| 760 | /** |
||
| 761 | * Creates a REST entity array (JSON object we're going to return in the response, but |
||
| 762 | * for now still a PHP array, but soon enough we'll call json_encode on it, don't worry), |
||
| 763 | * from $wpdb->get_row( $sql, ARRAY_A) |
||
| 764 | * |
||
| 765 | * @param EEM_Base $model |
||
| 766 | * @param array $db_row |
||
| 767 | * @return array entity mostly ready for converting to JSON and sending in the response |
||
| 768 | * @throws EE_Error |
||
| 769 | * @throws InvalidArgumentException |
||
| 770 | * @throws InvalidDataTypeException |
||
| 771 | * @throws InvalidInterfaceException |
||
| 772 | * @throws ReflectionException |
||
| 773 | * @throws RestException |
||
| 774 | */ |
||
| 775 | protected function createBareEntityFromWpdbResults(EEM_Base $model, $db_row) |
||
| 871 | |||
| 872 | |||
| 873 | /** |
||
| 874 | * Takes a value all the way from the DB representation, to the model object's representation, to the |
||
| 875 | * user-facing PHP representation, to the REST API representation. (Assumes you've already taken from the DB |
||
| 876 | * representation using $field_obj->prepare_for_set_from_db()) |
||
| 877 | * |
||
| 878 | * @param EE_Model_Field_Base $field_obj |
||
| 879 | * @param mixed $value as it's stored on a model object |
||
| 880 | * @param string $format valid values are 'normal' (default), 'pretty', 'datetime_obj' |
||
| 881 | * @return mixed |
||
| 882 | * @throws RestException if $value contains a PHP object |
||
| 883 | * @throws EE_Error |
||
| 884 | */ |
||
| 885 | protected function prepareFieldObjValueForJson(EE_Model_Field_Base $field_obj, $value, $format = 'normal') |
||
| 903 | |||
| 904 | |||
| 905 | /** |
||
| 906 | * Adds a few extra fields to the entity response |
||
| 907 | * |
||
| 908 | * @param EEM_Base $model |
||
| 909 | * @param array $db_row |
||
| 910 | * @param array $entity_array |
||
| 911 | * @return array modified entity |
||
| 912 | * @throws EE_Error |
||
| 913 | */ |
||
| 914 | protected function addExtraFields(EEM_Base $model, $db_row, $entity_array) |
||
| 921 | |||
| 922 | |||
| 923 | /** |
||
| 924 | * Gets links we want to add to the response |
||
| 925 | * |
||
| 926 | * @param EEM_Base $model |
||
| 927 | * @param array $db_row |
||
| 928 | * @param array $entity_array |
||
| 929 | * @return array the _links item in the entity |
||
| 930 | * @throws EE_Error |
||
| 931 | * @global WP_REST_Server $wp_rest_server |
||
| 932 | */ |
||
| 933 | protected function getEntityLinks($model, $db_row, $entity_array) |
||
| 975 | |||
| 976 | |||
| 977 | /** |
||
| 978 | * Adds the included models indicated in the request to the entity provided |
||
| 979 | * |
||
| 980 | * @param EEM_Base $model |
||
| 981 | * @param WP_REST_Request $rest_request |
||
| 982 | * @param array $entity_array |
||
| 983 | * @param array $db_row |
||
| 984 | * @param boolean $included_items_protected if the original item is password protected, don't include any related models. |
||
| 985 | * @return array the modified entity |
||
| 986 | * @throws DomainException |
||
| 987 | * @throws EE_Error |
||
| 988 | * @throws InvalidArgumentException |
||
| 989 | * @throws InvalidDataTypeException |
||
| 990 | * @throws InvalidInterfaceException |
||
| 991 | * @throws ModelConfigurationException |
||
| 992 | * @throws ReflectionException |
||
| 993 | * @throws UnexpectedEntityException |
||
| 994 | */ |
||
| 995 | protected function includeRequestedModels( |
||
| 1059 | |||
| 1060 | /** |
||
| 1061 | * If the user has requested only specific properties (including meta properties like _links or _protected) |
||
| 1062 | * remove everything else. |
||
| 1063 | * @since 4.9.74.p |
||
| 1064 | * @param EEM_Base $model |
||
| 1065 | * @param WP_REST_Request $rest_request |
||
| 1066 | * @param $entity_array |
||
| 1067 | * @return array |
||
| 1068 | * @throws EE_Error |
||
| 1069 | */ |
||
| 1070 | protected function includeOnlyRequestedProperties( |
||
| 1091 | |||
| 1092 | |||
| 1093 | /** |
||
| 1094 | * Returns a new array with all the names of models removed. Eg |
||
| 1095 | * array( 'Event', 'Datetime.*', 'foobar' ) would become array( 'Datetime.*', 'foobar' ) |
||
| 1096 | * |
||
| 1097 | * @param array $arr |
||
| 1098 | * @return array |
||
| 1099 | */ |
||
| 1100 | private function removeModelNamesFromArray($arr) |
||
| 1104 | |||
| 1105 | |||
| 1106 | /** |
||
| 1107 | * Gets the calculated fields for the response |
||
| 1108 | * |
||
| 1109 | * @param EEM_Base $model |
||
| 1110 | * @param array $wpdb_row |
||
| 1111 | * @param WP_REST_Request $rest_request |
||
| 1112 | * @param boolean $row_is_protected whether this row is password protected or not |
||
| 1113 | * @return stdClass the _calculations item in the entity |
||
| 1114 | * @throws EE_Error |
||
| 1115 | * @throws RestException if a default value has a PHP object, which should never do (and if we |
||
| 1116 | * did, let's know about it ASAP, so let the exception bubble up) |
||
| 1117 | * @throws UnexpectedEntityException |
||
| 1118 | */ |
||
| 1119 | protected function getEntityCalculations($model, $wpdb_row, $rest_request, $row_is_protected = false) |
||
| 1187 | |||
| 1188 | |||
| 1189 | /** |
||
| 1190 | * Gets the full URL to the resource, taking the requested version into account |
||
| 1191 | * |
||
| 1192 | * @param string $link_part_after_version_and_slash eg "events/10/datetimes" |
||
| 1193 | * @return string url eg "http://mysite.com/wp-json/ee/v4.6/events/10/datetimes" |
||
| 1194 | * @throws EE_Error |
||
| 1195 | */ |
||
| 1196 | public function getVersionedLinkTo($link_part_after_version_and_slash) |
||
| 1205 | |||
| 1206 | |||
| 1207 | /** |
||
| 1208 | * Gets the correct lowercase name for the relation in the API according |
||
| 1209 | * to the relation's type |
||
| 1210 | * |
||
| 1211 | * @param string $relation_name |
||
| 1212 | * @param EE_Model_Relation_Base $relation_obj |
||
| 1213 | * @return string |
||
| 1214 | */ |
||
| 1215 | public static function getRelatedEntityName($relation_name, $relation_obj) |
||
| 1222 | |||
| 1223 | |||
| 1224 | /** |
||
| 1225 | * Gets the one model object with the specified id for the specified model |
||
| 1226 | * |
||
| 1227 | * @param EEM_Base $model |
||
| 1228 | * @param WP_REST_Request $request |
||
| 1229 | * @return array |
||
| 1230 | * @throws EE_Error |
||
| 1231 | * @throws InvalidArgumentException |
||
| 1232 | * @throws InvalidDataTypeException |
||
| 1233 | * @throws InvalidInterfaceException |
||
| 1234 | * @throws ModelConfigurationException |
||
| 1235 | * @throws ReflectionException |
||
| 1236 | * @throws RestException |
||
| 1237 | * @throws RestPasswordIncorrectException |
||
| 1238 | * @throws RestPasswordRequiredException |
||
| 1239 | * @throws UnexpectedEntityException |
||
| 1240 | * @throws DomainException |
||
| 1241 | */ |
||
| 1242 | public function getEntityFromModel($model, $request) |
||
| 1247 | |||
| 1248 | |||
| 1249 | /** |
||
| 1250 | * If a context is provided which isn't valid, maybe it was added in a future |
||
| 1251 | * version so just treat it as a default read |
||
| 1252 | * |
||
| 1253 | * @param string $context |
||
| 1254 | * @return string array key of EEM_Base::cap_contexts_to_cap_action_map() |
||
| 1255 | */ |
||
| 1256 | public function validateContext($context) |
||
| 1267 | |||
| 1268 | |||
| 1269 | /** |
||
| 1270 | * Verifies the passed in value is an allowable default where conditions value. |
||
| 1271 | * |
||
| 1272 | * @param $default_query_params |
||
| 1273 | * @return string |
||
| 1274 | */ |
||
| 1275 | public function validateDefaultQueryParams($default_query_params) |
||
| 1295 | |||
| 1296 | |||
| 1297 | /** |
||
| 1298 | * Translates API filter get parameter into model query params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions. |
||
| 1299 | * Note: right now the query parameter keys for fields (and related fields) |
||
| 1300 | * can be left as-is, but it's quite possible this will change someday. |
||
| 1301 | * Also, this method's contents might be candidate for moving to Model_Data_Translator |
||
| 1302 | * |
||
| 1303 | * @param EEM_Base $model |
||
| 1304 | * @param $query_params |
||
| 1305 | * @return array model query params (@see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions) |
||
| 1306 | * or FALSE to indicate that absolutely no results should be returned |
||
| 1307 | * @throws EE_Error |
||
| 1308 | * @throws InvalidArgumentException |
||
| 1309 | * @throws InvalidDataTypeException |
||
| 1310 | * @throws InvalidInterfaceException |
||
| 1311 | * @throws RestException |
||
| 1312 | * @throws DomainException |
||
| 1313 | */ |
||
| 1314 | public function createModelQueryParams($model, $query_params) |
||
| 1418 | |||
| 1419 | |||
| 1420 | /** |
||
| 1421 | * Changes the REST-style query params for use in the models |
||
| 1422 | * |
||
| 1423 | * @deprecated |
||
| 1424 | * @param EEM_Base $model |
||
| 1425 | * @param array $query_params sub-array from @see EEM_Base::get_all() |
||
| 1426 | * @return array |
||
| 1427 | */ |
||
| 1428 | View Code Duplication | public function prepareRestQueryParamsKeyForModels($model, $query_params) |
|
| 1440 | |||
| 1441 | |||
| 1442 | /** |
||
| 1443 | * @deprecated instead use ModelDataTranslator::prepareFieldValuesFromJson() |
||
| 1444 | * @param $model |
||
| 1445 | * @param $query_params |
||
| 1446 | * @return array |
||
| 1447 | */ |
||
| 1448 | View Code Duplication | public function prepareRestQueryParamsValuesForModels($model, $query_params) |
|
| 1460 | |||
| 1461 | |||
| 1462 | /** |
||
| 1463 | * Explodes the string on commas, and only returns items with $prefix followed by a period. |
||
| 1464 | * If no prefix is specified, returns items with no period. |
||
| 1465 | * |
||
| 1466 | * @param string|array $string_to_explode eg "jibba,jabba, blah, blah, blah" or array('jibba', 'jabba' ) |
||
| 1467 | * @param string $prefix "Event" or "foobar" |
||
| 1468 | * @return array $string_to_exploded exploded on COMMAS, and if a prefix was specified |
||
| 1469 | * we only return strings starting with that and a period; if no prefix was |
||
| 1470 | * specified we return all items containing NO periods |
||
| 1471 | */ |
||
| 1472 | public function explodeAndGetItemsPrefixedWith($string_to_explode, $prefix) |
||
| 1507 | |||
| 1508 | |||
| 1509 | /** |
||
| 1510 | * @param string $include_string @see Read:handle_request_get_all |
||
| 1511 | * @param string $model_name |
||
| 1512 | * @return array of fields for this model. If $model_name is provided, then |
||
| 1513 | * the fields for that model, with the model's name removed from each. |
||
| 1514 | * If $include_string was blank or '*' returns an empty array |
||
| 1515 | * @throws EE_Error |
||
| 1516 | * @deprecated since 4.8.36.rc.001 You should instead use Read::explode_and_get_items_prefixed_with. |
||
| 1517 | * Deprecated because its return values were really quite confusing- sometimes it returned |
||
| 1518 | * an empty array (when the include string was blank or '*') or sometimes it returned |
||
| 1519 | * array('*') (when you provided a model and a model of that kind was found). |
||
| 1520 | * Parses the $include_string so we fetch all the field names relating to THIS model |
||
| 1521 | * (ie have NO period in them), or for the provided model (ie start with the model |
||
| 1522 | * name and then a period). |
||
| 1523 | */ |
||
| 1524 | public function extractIncludesForThisModel($include_string, $model_name = null) |
||
| 1558 | |||
| 1559 | |||
| 1560 | /** |
||
| 1561 | * Gets the single item using the model according to the request in the context given, otherwise |
||
| 1562 | * returns that it's inaccessible to the current user |
||
| 1563 | * |
||
| 1564 | * @param EEM_Base $model |
||
| 1565 | * @param WP_REST_Request $request |
||
| 1566 | * @param null $context |
||
| 1567 | * @return array |
||
| 1568 | * @throws EE_Error |
||
| 1569 | * @throws InvalidArgumentException |
||
| 1570 | * @throws InvalidDataTypeException |
||
| 1571 | * @throws InvalidInterfaceException |
||
| 1572 | * @throws ModelConfigurationException |
||
| 1573 | * @throws ReflectionException |
||
| 1574 | * @throws RestException |
||
| 1575 | * @throws RestPasswordIncorrectException |
||
| 1576 | * @throws RestPasswordRequiredException |
||
| 1577 | * @throws UnexpectedEntityException |
||
| 1578 | * @throws DomainException |
||
| 1579 | */ |
||
| 1580 | public function getOneOrReportPermissionError(EEM_Base $model, WP_REST_Request $request, $context = null) |
||
| 1622 | |||
| 1623 | /** |
||
| 1624 | * Checks that if this content requires a password to be read, that it's been provided and is correct. |
||
| 1625 | * @since 4.9.74.p |
||
| 1626 | * @param EEM_Base $model |
||
| 1627 | * @param $model_row |
||
| 1628 | * @param array $query_params Adds 'default_where_conditions' => 'minimum' to ensure we don't confuse trashed with |
||
| 1629 | * password protected. |
||
| 1630 | * @param WP_REST_Request $request |
||
| 1631 | * @throws EE_Error |
||
| 1632 | * @throws InvalidArgumentException |
||
| 1633 | * @throws InvalidDataTypeException |
||
| 1634 | * @throws InvalidInterfaceException |
||
| 1635 | * @throws RestPasswordRequiredException |
||
| 1636 | * @throws RestPasswordIncorrectException |
||
| 1637 | * @throws ModelConfigurationException |
||
| 1638 | * @throws ReflectionException |
||
| 1639 | */ |
||
| 1640 | protected function checkPassword(EEM_Base $model, $model_row, $query_params, WP_REST_Request $request) |
||
| 1678 | } |
||
| 1679 |