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 |
||
| 44 | class Read extends Base |
||
| 45 | { |
||
| 46 | |||
| 47 | |||
| 48 | /** |
||
| 49 | * @var CalculatedModelFields |
||
| 50 | */ |
||
| 51 | protected $fields_calculator; |
||
| 52 | |||
| 53 | |||
| 54 | /** |
||
| 55 | * Read constructor. |
||
| 56 | * @param CalculatedModelFields $fields_calculator |
||
| 57 | */ |
||
| 58 | public function __construct(CalculatedModelFields $fields_calculator) |
||
| 59 | { |
||
| 60 | parent::__construct(); |
||
| 61 | $this->fields_calculator = $fields_calculator; |
||
| 62 | } |
||
| 63 | |||
| 64 | |||
| 65 | /** |
||
| 66 | * Handles requests to get all (or a filtered subset) of entities for a particular model |
||
| 67 | * |
||
| 68 | * @param WP_REST_Request $request |
||
| 69 | * @param string $version |
||
| 70 | * @param string $model_name |
||
| 71 | * @return WP_REST_Response|WP_Error |
||
| 72 | * @throws InvalidArgumentException |
||
| 73 | * @throws InvalidDataTypeException |
||
| 74 | * @throws InvalidInterfaceException |
||
| 75 | */ |
||
| 76 | View Code Duplication | public static function handleRequestGetAll(WP_REST_Request $request, $version, $model_name) |
|
| 77 | { |
||
| 78 | $controller = LoaderFactory::getLoader()->getNew('EventEspresso\core\libraries\rest_api\controllers\model\Read'); |
||
| 79 | try { |
||
| 80 | $controller->setRequestedVersion($version); |
||
| 81 | if (! $controller->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
||
| 82 | return $controller->sendResponse( |
||
| 83 | new WP_Error( |
||
| 84 | 'endpoint_parsing_error', |
||
| 85 | sprintf( |
||
| 86 | __( |
||
| 87 | 'There is no model for endpoint %s. Please contact event espresso support', |
||
| 88 | 'event_espresso' |
||
| 89 | ), |
||
| 90 | $model_name |
||
| 91 | ) |
||
| 92 | ) |
||
| 93 | ); |
||
| 94 | } |
||
| 95 | return $controller->sendResponse( |
||
| 96 | $controller->getEntitiesFromModel( |
||
| 97 | $controller->getModelVersionInfo()->loadModel($model_name), |
||
| 98 | $request |
||
| 99 | ) |
||
| 100 | ); |
||
| 101 | } catch (Exception $e) { |
||
| 102 | return $controller->sendResponse($e); |
||
| 103 | } |
||
| 104 | } |
||
| 105 | |||
| 106 | |||
| 107 | /** |
||
| 108 | * Prepares and returns schema for any OPTIONS request. |
||
| 109 | * |
||
| 110 | * @param string $version The API endpoint version being used. |
||
| 111 | * @param string $model_name Something like `Event` or `Registration` |
||
| 112 | * @return array |
||
| 113 | * @throws InvalidArgumentException |
||
| 114 | * @throws InvalidDataTypeException |
||
| 115 | * @throws InvalidInterfaceException |
||
| 116 | */ |
||
| 117 | public static function handleSchemaRequest($version, $model_name) |
||
| 118 | { |
||
| 119 | $controller = LoaderFactory::getLoader()->getNew('EventEspresso\core\libraries\rest_api\controllers\model\Read'); |
||
| 120 | try { |
||
| 121 | $controller->setRequestedVersion($version); |
||
| 122 | if (! $controller->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
||
| 123 | return array(); |
||
| 124 | } |
||
| 125 | // get the model for this version |
||
| 126 | $model = $controller->getModelVersionInfo()->loadModel($model_name); |
||
| 127 | $model_schema = new JsonModelSchema($model, LoaderFactory::getLoader()->getShared('EventEspresso\core\libraries\rest_api\CalculatedModelFields')); |
||
| 128 | return $model_schema->getModelSchemaForRelations( |
||
| 129 | $controller->getModelVersionInfo()->relationSettings($model), |
||
| 130 | $controller->customizeSchemaForRestResponse( |
||
| 131 | $model, |
||
| 132 | $model_schema->getModelSchemaForFields( |
||
| 133 | $controller->getModelVersionInfo()->fieldsOnModelInThisVersion($model), |
||
| 134 | $model_schema->getInitialSchemaStructure() |
||
| 135 | ) |
||
| 136 | ) |
||
| 137 | ); |
||
| 138 | } catch (Exception $e) { |
||
| 139 | return array(); |
||
| 140 | } |
||
| 141 | } |
||
| 142 | |||
| 143 | |||
| 144 | /** |
||
| 145 | * This loops through each field in the given schema for the model and does the following: |
||
| 146 | * - add any extra fields that are REST API specific and related to existing fields. |
||
| 147 | * - transform default values into the correct format for a REST API response. |
||
| 148 | * |
||
| 149 | * @param EEM_Base $model |
||
| 150 | * @param array $schema |
||
| 151 | * @return array The final schema. |
||
| 152 | */ |
||
| 153 | protected function customizeSchemaForRestResponse(EEM_Base $model, array $schema) |
||
| 154 | { |
||
| 155 | foreach ($this->getModelVersionInfo()->fieldsOnModelInThisVersion($model) as $field_name => $field) { |
||
| 156 | $schema = $this->translateDefaultsForRestResponse( |
||
| 157 | $field_name, |
||
| 158 | $field, |
||
| 159 | $this->maybeAddExtraFieldsToSchema($field_name, $field, $schema) |
||
| 160 | ); |
||
| 161 | } |
||
| 162 | return $schema; |
||
| 163 | } |
||
| 164 | |||
| 165 | |||
| 166 | /** |
||
| 167 | * This is used to ensure that the 'default' value set in the schema response is formatted correctly for the REST |
||
| 168 | * response. |
||
| 169 | * |
||
| 170 | * @param $field_name |
||
| 171 | * @param EE_Model_Field_Base $field |
||
| 172 | * @param array $schema |
||
| 173 | * @return array |
||
| 174 | * @throws ObjectDetectedException if a default value has a PHP object, which should never do (and if we |
||
| 175 | * did, let's know about it ASAP, so let the exception bubble up) |
||
| 176 | */ |
||
| 177 | protected function translateDefaultsForRestResponse($field_name, EE_Model_Field_Base $field, array $schema) |
||
| 178 | { |
||
| 179 | if (isset($schema['properties'][ $field_name ]['default'])) { |
||
| 180 | if (is_array($schema['properties'][ $field_name ]['default'])) { |
||
| 181 | foreach ($schema['properties'][ $field_name ]['default'] as $default_key => $default_value) { |
||
| 182 | View Code Duplication | if ($default_key === 'raw') { |
|
| 183 | $schema['properties'][ $field_name ]['default'][ $default_key ] = |
||
| 184 | ModelDataTranslator::prepareFieldValueForJson( |
||
| 185 | $field, |
||
| 186 | $default_value, |
||
| 187 | $this->getModelVersionInfo()->requestedVersion() |
||
| 188 | ); |
||
| 189 | } |
||
| 190 | } |
||
| 191 | View Code Duplication | } else { |
|
| 192 | $schema['properties'][ $field_name ]['default'] = ModelDataTranslator::prepareFieldValueForJson( |
||
| 193 | $field, |
||
| 194 | $schema['properties'][ $field_name ]['default'], |
||
| 195 | $this->getModelVersionInfo()->requestedVersion() |
||
| 196 | ); |
||
| 197 | } |
||
| 198 | } |
||
| 199 | return $schema; |
||
| 200 | } |
||
| 201 | |||
| 202 | |||
| 203 | /** |
||
| 204 | * Adds additional fields to the schema |
||
| 205 | * The REST API returns a GMT value field for each datetime field in the resource. Thus the description about this |
||
| 206 | * needs to be added to the schema. |
||
| 207 | * |
||
| 208 | * @param $field_name |
||
| 209 | * @param EE_Model_Field_Base $field |
||
| 210 | * @param array $schema |
||
| 211 | * @return array |
||
| 212 | */ |
||
| 213 | protected function maybeAddExtraFieldsToSchema($field_name, EE_Model_Field_Base $field, array $schema) |
||
| 214 | { |
||
| 215 | if ($field instanceof EE_Datetime_Field) { |
||
| 216 | $schema['properties'][ $field_name . '_gmt' ] = $field->getSchema(); |
||
| 217 | // modify the description |
||
| 218 | $schema['properties'][ $field_name . '_gmt' ]['description'] = sprintf( |
||
| 219 | esc_html__('%s - the value for this field is in GMT.', 'event_espresso'), |
||
| 220 | wp_specialchars_decode($field->get_nicename(), ENT_QUOTES) |
||
| 221 | ); |
||
| 222 | } |
||
| 223 | return $schema; |
||
| 224 | } |
||
| 225 | |||
| 226 | |||
| 227 | /** |
||
| 228 | * Used to figure out the route from the request when a `WP_REST_Request` object is not available |
||
| 229 | * |
||
| 230 | * @return string |
||
| 231 | */ |
||
| 232 | protected function getRouteFromRequest() |
||
| 233 | { |
||
| 234 | if (isset($GLOBALS['wp']) |
||
| 235 | && $GLOBALS['wp'] instanceof \WP |
||
| 236 | && isset($GLOBALS['wp']->query_vars['rest_route']) |
||
| 237 | ) { |
||
| 238 | return $GLOBALS['wp']->query_vars['rest_route']; |
||
| 239 | } else { |
||
| 240 | return isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '/'; |
||
| 241 | } |
||
| 242 | } |
||
| 243 | |||
| 244 | |||
| 245 | /** |
||
| 246 | * Gets a single entity related to the model indicated in the path and its id |
||
| 247 | * |
||
| 248 | * @param WP_REST_Request $request |
||
| 249 | * @param string $version |
||
| 250 | * @param string $model_name |
||
| 251 | * @return WP_REST_Response|WP_Error |
||
| 252 | * @throws InvalidDataTypeException |
||
| 253 | * @throws InvalidInterfaceException |
||
| 254 | * @throws InvalidArgumentException |
||
| 255 | */ |
||
| 256 | View Code Duplication | public static function handleRequestGetOne(WP_REST_Request $request, $version, $model_name) |
|
| 257 | { |
||
| 258 | $controller = LoaderFactory::getLoader()->getNew('EventEspresso\core\libraries\rest_api\controllers\model\Read'); |
||
| 259 | try { |
||
| 260 | $controller->setRequestedVersion($version); |
||
| 261 | if (! $controller->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
||
| 262 | return $controller->sendResponse( |
||
| 263 | new WP_Error( |
||
| 264 | 'endpoint_parsing_error', |
||
| 265 | sprintf( |
||
| 266 | __( |
||
| 267 | 'There is no model for endpoint %s. Please contact event espresso support', |
||
| 268 | 'event_espresso' |
||
| 269 | ), |
||
| 270 | $model_name |
||
| 271 | ) |
||
| 272 | ) |
||
| 273 | ); |
||
| 274 | } |
||
| 275 | return $controller->sendResponse( |
||
| 276 | $controller->getEntityFromModel( |
||
| 277 | $controller->getModelVersionInfo()->loadModel($model_name), |
||
| 278 | $request |
||
| 279 | ) |
||
| 280 | ); |
||
| 281 | } catch (Exception $e) { |
||
| 282 | return $controller->sendResponse($e); |
||
| 283 | } |
||
| 284 | } |
||
| 285 | |||
| 286 | |||
| 287 | /** |
||
| 288 | * Gets all the related entities (or if its a belongs-to relation just the one) |
||
| 289 | * to the item with the given id |
||
| 290 | * |
||
| 291 | * @param WP_REST_Request $request |
||
| 292 | * @param string $version |
||
| 293 | * @param string $model_name |
||
| 294 | * @param string $related_model_name |
||
| 295 | * @return WP_REST_Response|WP_Error |
||
| 296 | * @throws InvalidDataTypeException |
||
| 297 | * @throws InvalidInterfaceException |
||
| 298 | * @throws InvalidArgumentException |
||
| 299 | */ |
||
| 300 | public static function handleRequestGetRelated( |
||
| 301 | WP_REST_Request $request, |
||
| 302 | $version, |
||
| 303 | $model_name, |
||
| 304 | $related_model_name |
||
| 305 | ) { |
||
| 306 | $controller = LoaderFactory::getLoader()->getNew('EventEspresso\core\libraries\rest_api\controllers\model\Read'); |
||
| 307 | try { |
||
| 308 | $controller->setRequestedVersion($version); |
||
| 309 | $main_model = $controller->validateModel($model_name); |
||
| 310 | $controller->validateModel($related_model_name); |
||
| 311 | return $controller->sendResponse( |
||
| 312 | $controller->getEntitiesFromRelation( |
||
| 313 | $request->get_param('id'), |
||
| 314 | $main_model->related_settings_for($related_model_name), |
||
| 315 | $request |
||
| 316 | ) |
||
| 317 | ); |
||
| 318 | } catch (Exception $e) { |
||
| 319 | return $controller->sendResponse($e); |
||
| 320 | } |
||
| 321 | } |
||
| 322 | |||
| 323 | |||
| 324 | /** |
||
| 325 | * Gets a collection for the given model and filters |
||
| 326 | * |
||
| 327 | * @param EEM_Base $model |
||
| 328 | * @param WP_REST_Request $request |
||
| 329 | * @return array |
||
| 330 | * @throws EE_Error |
||
| 331 | * @throws InvalidArgumentException |
||
| 332 | * @throws InvalidDataTypeException |
||
| 333 | * @throws InvalidInterfaceException |
||
| 334 | * @throws ReflectionException |
||
| 335 | * @throws RestException |
||
| 336 | */ |
||
| 337 | public function getEntitiesFromModel($model, $request) |
||
| 338 | { |
||
| 339 | $query_params = $this->createModelQueryParams($model, $request->get_params()); |
||
| 340 | if (! Capabilities::currentUserHasPartialAccessTo($model, $query_params['caps'])) { |
||
| 341 | $model_name_plural = EEH_Inflector::pluralize_and_lower($model->get_this_model_name()); |
||
| 342 | throw new RestException( |
||
| 343 | sprintf('rest_%s_cannot_list', $model_name_plural), |
||
| 344 | sprintf( |
||
| 345 | __('Sorry, you are not allowed to list %1$s. Missing permissions: %2$s', 'event_espresso'), |
||
| 346 | $model_name_plural, |
||
| 347 | Capabilities::getMissingPermissionsString($model, $query_params['caps']) |
||
| 348 | ), |
||
| 349 | array('status' => 403) |
||
| 350 | ); |
||
| 351 | } |
||
| 352 | if (! $request->get_header('no_rest_headers')) { |
||
| 353 | $this->setHeadersFromQueryParams($model, $query_params); |
||
| 354 | } |
||
| 355 | /** @type array $results */ |
||
| 356 | $results = $model->get_all_wpdb_results($query_params); |
||
| 357 | $nice_results = array(); |
||
| 358 | foreach ($results as $result) { |
||
| 359 | $nice_results[] = $this->createEntityFromWpdbResult( |
||
| 360 | $model, |
||
| 361 | $result, |
||
| 362 | $request |
||
| 363 | ); |
||
| 364 | } |
||
| 365 | return $nice_results; |
||
| 366 | } |
||
| 367 | |||
| 368 | |||
| 369 | /** |
||
| 370 | * Gets the collection for given relation object |
||
| 371 | * The same as Read::get_entities_from_model(), except if the relation |
||
| 372 | * is a HABTM relation, in which case it merges any non-foreign-key fields from |
||
| 373 | * the join-model-object into the results |
||
| 374 | * |
||
| 375 | * @param array $primary_model_query_params query params for finding the item from which |
||
| 376 | * relations will be based |
||
| 377 | * @param \EE_Model_Relation_Base $relation |
||
| 378 | * @param WP_REST_Request $request |
||
| 379 | * @return array |
||
| 380 | * @throws EE_Error |
||
| 381 | * @throws InvalidArgumentException |
||
| 382 | * @throws InvalidDataTypeException |
||
| 383 | * @throws InvalidInterfaceException |
||
| 384 | * @throws ReflectionException |
||
| 385 | * @throws RestException |
||
| 386 | * @throws \EventEspresso\core\exceptions\ModelConfigurationException |
||
| 387 | */ |
||
| 388 | protected function getEntitiesFromRelationUsingModelQueryParams($primary_model_query_params, $relation, $request) |
||
| 389 | { |
||
| 390 | $context = $this->validateContext($request->get_param('caps')); |
||
| 391 | $model = $relation->get_this_model(); |
||
| 392 | $related_model = $relation->get_other_model(); |
||
| 393 | if (! isset($primary_model_query_params[0])) { |
||
| 394 | $primary_model_query_params[0] = array(); |
||
| 395 | } |
||
| 396 | // check if they can access the 1st model object |
||
| 397 | $primary_model_query_params = array( |
||
| 398 | 0 => $primary_model_query_params[0], |
||
| 399 | 'limit' => 1, |
||
| 400 | ); |
||
| 401 | if ($model instanceof EEM_Soft_Delete_Base) { |
||
| 402 | $primary_model_query_params = $model->alter_query_params_so_deleted_and_undeleted_items_included( |
||
| 403 | $primary_model_query_params |
||
| 404 | ); |
||
| 405 | } |
||
| 406 | $restricted_query_params = $primary_model_query_params; |
||
| 407 | $restricted_query_params['caps'] = $context; |
||
| 408 | $restricted_query_params['limit'] = 1; |
||
| 409 | $this->setDebugInfo('main model query params', $restricted_query_params); |
||
| 410 | $this->setDebugInfo('missing caps', Capabilities::getMissingPermissionsString($related_model, $context)); |
||
| 411 | $primary_model_rows = $model->get_all_wpdb_results($restricted_query_params); |
||
| 412 | $primary_model_row = null; |
||
| 413 | if (is_array($primary_model_rows)) { |
||
| 414 | $primary_model_row = reset($primary_model_rows); |
||
| 415 | } |
||
| 416 | if (! ( |
||
| 417 | Capabilities::currentUserHasPartialAccessTo($related_model, $context) |
||
| 418 | && $primary_model_row |
||
| 419 | ) |
||
| 420 | ) { |
||
| 421 | if ($relation instanceof EE_Belongs_To_Relation) { |
||
| 422 | $related_model_name_maybe_plural = strtolower($related_model->get_this_model_name()); |
||
| 423 | } else { |
||
| 424 | $related_model_name_maybe_plural = EEH_Inflector::pluralize_and_lower( |
||
| 425 | $related_model->get_this_model_name() |
||
| 426 | ); |
||
| 427 | } |
||
| 428 | throw new RestException( |
||
| 429 | sprintf('rest_%s_cannot_list', $related_model_name_maybe_plural), |
||
| 430 | sprintf( |
||
| 431 | __( |
||
| 432 | 'Sorry, you are not allowed to list %1$s related to %2$s. Missing permissions: %3$s', |
||
| 433 | 'event_espresso' |
||
| 434 | ), |
||
| 435 | $related_model_name_maybe_plural, |
||
| 436 | $relation->get_this_model()->get_this_model_name(), |
||
| 437 | implode( |
||
| 438 | ',', |
||
| 439 | array_keys( |
||
| 440 | Capabilities::getMissingPermissions($related_model, $context) |
||
| 441 | ) |
||
| 442 | ) |
||
| 443 | ), |
||
| 444 | array('status' => 403) |
||
| 445 | ); |
||
| 446 | } |
||
| 447 | |||
| 448 | $this->checkPassword( |
||
| 449 | $model, |
||
| 450 | $primary_model_row, |
||
| 451 | $restricted_query_params, |
||
| 452 | $request |
||
| 453 | ); |
||
| 454 | $query_params = $this->createModelQueryParams($relation->get_other_model(), $request->get_params()); |
||
|
|
|||
| 455 | foreach ($primary_model_query_params[0] as $where_condition_key => $where_condition_value) { |
||
| 456 | $query_params[0][ $relation->get_this_model()->get_this_model_name() |
||
| 457 | . '.' |
||
| 458 | . $where_condition_key ] = $where_condition_value; |
||
| 459 | } |
||
| 460 | $query_params['default_where_conditions'] = 'none'; |
||
| 461 | $query_params['caps'] = $context; |
||
| 462 | if (! $request->get_header('no_rest_headers')) { |
||
| 463 | $this->setHeadersFromQueryParams($relation->get_other_model(), $query_params); |
||
| 464 | } |
||
| 465 | /** @type array $results */ |
||
| 466 | $results = $relation->get_other_model()->get_all_wpdb_results($query_params); |
||
| 467 | $nice_results = array(); |
||
| 468 | foreach ($results as $result) { |
||
| 469 | $nice_result = $this->createEntityFromWpdbResult( |
||
| 470 | $relation->get_other_model(), |
||
| 471 | $result, |
||
| 472 | $request |
||
| 473 | ); |
||
| 474 | if ($relation instanceof \EE_HABTM_Relation) { |
||
| 475 | // put the unusual stuff (properties from the HABTM relation) first, and make sure |
||
| 476 | // if there are conflicts we prefer the properties from the main model |
||
| 477 | $join_model_result = $this->createEntityFromWpdbResult( |
||
| 478 | $relation->get_join_model(), |
||
| 479 | $result, |
||
| 480 | $request |
||
| 481 | ); |
||
| 482 | $joined_result = array_merge($nice_result, $join_model_result); |
||
| 483 | // but keep the meta stuff from the main model |
||
| 484 | if (isset($nice_result['meta'])) { |
||
| 485 | $joined_result['meta'] = $nice_result['meta']; |
||
| 486 | } |
||
| 487 | $nice_result = $joined_result; |
||
| 488 | } |
||
| 489 | $nice_results[] = $nice_result; |
||
| 490 | } |
||
| 491 | if ($relation instanceof EE_Belongs_To_Relation) { |
||
| 492 | return array_shift($nice_results); |
||
| 493 | } else { |
||
| 494 | return $nice_results; |
||
| 495 | } |
||
| 496 | } |
||
| 497 | |||
| 498 | |||
| 499 | /** |
||
| 500 | * Gets the collection for given relation object |
||
| 501 | * The same as Read::get_entities_from_model(), except if the relation |
||
| 502 | * is a HABTM relation, in which case it merges any non-foreign-key fields from |
||
| 503 | * the join-model-object into the results |
||
| 504 | * |
||
| 505 | * @param string $id the ID of the thing we are fetching related stuff from |
||
| 506 | * @param \EE_Model_Relation_Base $relation |
||
| 507 | * @param WP_REST_Request $request |
||
| 508 | * @return array |
||
| 509 | * @throws EE_Error |
||
| 510 | */ |
||
| 511 | public function getEntitiesFromRelation($id, $relation, $request) |
||
| 512 | { |
||
| 513 | View Code Duplication | if (! $relation->get_this_model()->has_primary_key_field()) { |
|
| 514 | throw new EE_Error( |
||
| 515 | sprintf( |
||
| 516 | __( |
||
| 517 | // @codingStandardsIgnoreStart |
||
| 518 | 'Read::get_entities_from_relation should only be called from a model with a primary key, it was called from %1$s', |
||
| 519 | // @codingStandardsIgnoreEnd |
||
| 520 | 'event_espresso' |
||
| 521 | ), |
||
| 522 | $relation->get_this_model()->get_this_model_name() |
||
| 523 | ) |
||
| 524 | ); |
||
| 525 | } |
||
| 526 | // can we edit that main item? |
||
| 527 | // if not, show nothing but an error |
||
| 528 | // otherwise, please proceed |
||
| 529 | return $this->getEntitiesFromRelationUsingModelQueryParams( |
||
| 530 | array( |
||
| 531 | array( |
||
| 532 | $relation->get_this_model()->primary_key_name() => $id, |
||
| 533 | ), |
||
| 534 | ), |
||
| 535 | $relation, |
||
| 536 | $request |
||
| 537 | ); |
||
| 538 | } |
||
| 539 | |||
| 540 | |||
| 541 | /** |
||
| 542 | * Sets the headers that are based on the model and query params, |
||
| 543 | * like the total records. This should only be called on the original request |
||
| 544 | * from the client, not on subsequent internal |
||
| 545 | * |
||
| 546 | * @param EEM_Base $model |
||
| 547 | * @param array $query_params |
||
| 548 | * @return void |
||
| 549 | */ |
||
| 550 | protected function setHeadersFromQueryParams($model, $query_params) |
||
| 578 | |||
| 579 | |||
| 580 | /** |
||
| 581 | * Changes database results into REST API entities |
||
| 582 | * |
||
| 583 | * @param EEM_Base $model |
||
| 584 | * @param array $db_row like results from $wpdb->get_results() |
||
| 585 | * @param WP_REST_Request $rest_request |
||
| 586 | * @param string $deprecated no longer used |
||
| 587 | * @return array ready for being converted into json for sending to client |
||
| 588 | * @throws EE_Error |
||
| 589 | * @throws RestException |
||
| 590 | * @throws InvalidDataTypeException |
||
| 591 | * @throws InvalidInterfaceException |
||
| 592 | * @throws InvalidArgumentException |
||
| 593 | * @throws ReflectionException |
||
| 594 | */ |
||
| 595 | public function createEntityFromWpdbResult($model, $db_row, $rest_request, $deprecated = null) |
||
| 693 | |||
| 694 | /** |
||
| 695 | * Returns an array describing which fields can be protected, and which actually were removed this request |
||
| 696 | * @since 4.9.74.p |
||
| 697 | * @param $model |
||
| 698 | * @param $results_so_far |
||
| 699 | * @param $protected |
||
| 700 | * @return array results |
||
| 701 | */ |
||
| 702 | protected function addProtectedProperty(EEM_Base $model, $results_so_far, $protected) |
||
| 722 | |||
| 723 | /** |
||
| 724 | * Creates a REST entity array (JSON object we're going to return in the response, but |
||
| 725 | * for now still a PHP array, but soon enough we'll call json_encode on it, don't worry), |
||
| 726 | * from $wpdb->get_row( $sql, ARRAY_A) |
||
| 727 | * |
||
| 728 | * @param EEM_Base $model |
||
| 729 | * @param array $db_row |
||
| 730 | * @return array entity mostly ready for converting to JSON and sending in the response |
||
| 731 | */ |
||
| 732 | protected function createBareEntityFromWpdbResults(EEM_Base $model, $db_row) |
||
| 828 | |||
| 829 | |||
| 830 | /** |
||
| 831 | * Takes a value all the way from the DB representation, to the model object's representation, to the |
||
| 832 | * user-facing PHP representation, to the REST API representation. (Assumes you've already taken from the DB |
||
| 833 | * representation using $field_obj->prepare_for_set_from_db()) |
||
| 834 | * |
||
| 835 | * @param EE_Model_Field_Base $field_obj |
||
| 836 | * @param mixed $value as it's stored on a model object |
||
| 837 | * @param string $format valid values are 'normal' (default), 'pretty', 'datetime_obj' |
||
| 838 | * @return mixed |
||
| 839 | * @throws ObjectDetectedException if $value contains a PHP object |
||
| 840 | */ |
||
| 841 | protected function prepareFieldObjValueForJson(EE_Model_Field_Base $field_obj, $value, $format = 'normal') |
||
| 859 | |||
| 860 | |||
| 861 | /** |
||
| 862 | * Adds a few extra fields to the entity response |
||
| 863 | * |
||
| 864 | * @param EEM_Base $model |
||
| 865 | * @param array $db_row |
||
| 866 | * @param array $entity_array |
||
| 867 | * @return array modified entity |
||
| 868 | */ |
||
| 869 | protected function addExtraFields(EEM_Base $model, $db_row, $entity_array) |
||
| 876 | |||
| 877 | |||
| 878 | /** |
||
| 879 | * Gets links we want to add to the response |
||
| 880 | * |
||
| 881 | * @global \WP_REST_Server $wp_rest_server |
||
| 882 | * @param EEM_Base $model |
||
| 883 | * @param array $db_row |
||
| 884 | * @param array $entity_array |
||
| 885 | * @return array the _links item in the entity |
||
| 886 | */ |
||
| 887 | protected function getEntityLinks($model, $db_row, $entity_array) |
||
| 929 | |||
| 930 | |||
| 931 | /** |
||
| 932 | * Adds the included models indicated in the request to the entity provided |
||
| 933 | * |
||
| 934 | * @param EEM_Base $model |
||
| 935 | * @param WP_REST_Request $rest_request |
||
| 936 | * @param array $entity_array |
||
| 937 | * @param array $db_row |
||
| 938 | * @param boolean $included_items_protected if the original item is password protected, don't include any related models. |
||
| 939 | * @return array the modified entity |
||
| 940 | * @throws RestException |
||
| 941 | */ |
||
| 942 | protected function includeRequestedModels( |
||
| 1006 | |||
| 1007 | /** |
||
| 1008 | * If the user has requested only specific properties (including meta properties like _links or _protected) |
||
| 1009 | * remove everything else. |
||
| 1010 | * @since 4.9.74.p |
||
| 1011 | * @param EEM_Base $model |
||
| 1012 | * @param WP_REST_Request $rest_request |
||
| 1013 | * @param $entity_array |
||
| 1014 | * @return array |
||
| 1015 | * @throws EE_Error |
||
| 1016 | */ |
||
| 1017 | protected function includeOnlyRequestedProperties( |
||
| 1040 | |||
| 1041 | |||
| 1042 | /** |
||
| 1043 | * Returns a new array with all the names of models removed. Eg |
||
| 1044 | * array( 'Event', 'Datetime.*', 'foobar' ) would become array( 'Datetime.*', 'foobar' ) |
||
| 1045 | * |
||
| 1046 | * @param array $arr |
||
| 1047 | * @return array |
||
| 1048 | */ |
||
| 1049 | private function removeModelNamesFromArray($arr) |
||
| 1053 | |||
| 1054 | |||
| 1055 | /** |
||
| 1056 | * Gets the calculated fields for the response |
||
| 1057 | * |
||
| 1058 | * @param EEM_Base $model |
||
| 1059 | * @param array $wpdb_row |
||
| 1060 | * @param WP_REST_Request $rest_request |
||
| 1061 | * @param boolean $row_is_protected whether this row is password protected or not |
||
| 1062 | * @return \stdClass the _calculations item in the entity |
||
| 1063 | * @throws ObjectDetectedException if a default value has a PHP object, which should never do (and if we |
||
| 1064 | * did, let's know about it ASAP, so let the exception bubble up) |
||
| 1065 | */ |
||
| 1066 | protected function getEntityCalculations($model, $wpdb_row, $rest_request, $row_is_protected = false) |
||
| 1135 | |||
| 1136 | |||
| 1137 | /** |
||
| 1138 | * Gets the full URL to the resource, taking the requested version into account |
||
| 1139 | * |
||
| 1140 | * @param string $link_part_after_version_and_slash eg "events/10/datetimes" |
||
| 1141 | * @return string url eg "http://mysite.com/wp-json/ee/v4.6/events/10/datetimes" |
||
| 1142 | */ |
||
| 1143 | public function getVersionedLinkTo($link_part_after_version_and_slash) |
||
| 1152 | |||
| 1153 | |||
| 1154 | /** |
||
| 1155 | * Gets the correct lowercase name for the relation in the API according |
||
| 1156 | * to the relation's type |
||
| 1157 | * |
||
| 1158 | * @param string $relation_name |
||
| 1159 | * @param \EE_Model_Relation_Base $relation_obj |
||
| 1160 | * @return string |
||
| 1161 | */ |
||
| 1162 | public static function getRelatedEntityName($relation_name, $relation_obj) |
||
| 1170 | |||
| 1171 | |||
| 1172 | /** |
||
| 1173 | * Gets the one model object with the specified id for the specified model |
||
| 1174 | * |
||
| 1175 | * @param EEM_Base $model |
||
| 1176 | * @param WP_REST_Request $request |
||
| 1177 | * @return array |
||
| 1178 | */ |
||
| 1179 | public function getEntityFromModel($model, $request) |
||
| 1184 | |||
| 1185 | |||
| 1186 | /** |
||
| 1187 | * If a context is provided which isn't valid, maybe it was added in a future |
||
| 1188 | * version so just treat it as a default read |
||
| 1189 | * |
||
| 1190 | * @param string $context |
||
| 1191 | * @return string array key of EEM_Base::cap_contexts_to_cap_action_map() |
||
| 1192 | */ |
||
| 1193 | public function validateContext($context) |
||
| 1205 | |||
| 1206 | |||
| 1207 | /** |
||
| 1208 | * Verifies the passed in value is an allowable default where conditions value. |
||
| 1209 | * |
||
| 1210 | * @param $default_query_params |
||
| 1211 | * @return string |
||
| 1212 | */ |
||
| 1213 | public function validateDefaultQueryParams($default_query_params) |
||
| 1233 | |||
| 1234 | |||
| 1235 | /** |
||
| 1236 | * 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. |
||
| 1237 | * Note: right now the query parameter keys for fields (and related fields) |
||
| 1238 | * can be left as-is, but it's quite possible this will change someday. |
||
| 1239 | * Also, this method's contents might be candidate for moving to Model_Data_Translator |
||
| 1240 | * |
||
| 1241 | * @param EEM_Base $model |
||
| 1242 | * @param array $query_parameters from $_GET parameter @see Read:handle_request_get_all |
||
| 1243 | * @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) |
||
| 1244 | * or FALSE to indicate that absolutely no results should be returned |
||
| 1245 | * @throws EE_Error |
||
| 1246 | * @throws RestException |
||
| 1247 | */ |
||
| 1248 | public function createModelQueryParams($model, $query_params) |
||
| 1352 | |||
| 1353 | |||
| 1354 | /** |
||
| 1355 | * Changes the REST-style query params for use in the models |
||
| 1356 | * |
||
| 1357 | * @deprecated |
||
| 1358 | * @param EEM_Base $model |
||
| 1359 | * @param array $query_params sub-array from @see EEM_Base::get_all() |
||
| 1360 | * @return array |
||
| 1361 | */ |
||
| 1362 | View Code Duplication | public function prepareRestQueryParamsKeyForModels($model, $query_params) |
|
| 1374 | |||
| 1375 | |||
| 1376 | /** |
||
| 1377 | * @deprecated instead use ModelDataTranslator::prepareFieldValuesFromJson() |
||
| 1378 | * @param $model |
||
| 1379 | * @param $query_params |
||
| 1380 | * @return array |
||
| 1381 | */ |
||
| 1382 | View Code Duplication | public function prepareRestQueryParamsValuesForModels($model, $query_params) |
|
| 1394 | |||
| 1395 | |||
| 1396 | /** |
||
| 1397 | * Explodes the string on commas, and only returns items with $prefix followed by a period. |
||
| 1398 | * If no prefix is specified, returns items with no period. |
||
| 1399 | * |
||
| 1400 | * @param string|array $string_to_explode eg "jibba,jabba, blah, blah, blah" or array('jibba', 'jabba' ) |
||
| 1401 | * @param string $prefix "Event" or "foobar" |
||
| 1402 | * @return array $string_to_exploded exploded on COMMAS, and if a prefix was specified |
||
| 1403 | * we only return strings starting with that and a period; if no prefix was |
||
| 1404 | * specified we return all items containing NO periods |
||
| 1405 | */ |
||
| 1406 | public function explodeAndGetItemsPrefixedWith($string_to_explode, $prefix) |
||
| 1441 | |||
| 1442 | |||
| 1443 | /** |
||
| 1444 | * @deprecated since 4.8.36.rc.001 You should instead use Read::explode_and_get_items_prefixed_with. |
||
| 1445 | * Deprecated because its return values were really quite confusing- sometimes it returned |
||
| 1446 | * an empty array (when the include string was blank or '*') or sometimes it returned |
||
| 1447 | * array('*') (when you provided a model and a model of that kind was found). |
||
| 1448 | * Parses the $include_string so we fetch all the field names relating to THIS model |
||
| 1449 | * (ie have NO period in them), or for the provided model (ie start with the model |
||
| 1450 | * name and then a period). |
||
| 1451 | * @param string $include_string @see Read:handle_request_get_all |
||
| 1452 | * @param string $model_name |
||
| 1453 | * @return array of fields for this model. If $model_name is provided, then |
||
| 1454 | * the fields for that model, with the model's name removed from each. |
||
| 1455 | * If $include_string was blank or '*' returns an empty array |
||
| 1456 | */ |
||
| 1457 | public function extractIncludesForThisModel($include_string, $model_name = null) |
||
| 1491 | |||
| 1492 | |||
| 1493 | /** |
||
| 1494 | * Gets the single item using the model according to the request in the context given, otherwise |
||
| 1495 | * returns that it's inaccessible to the current user |
||
| 1496 | * |
||
| 1497 | * @param EEM_Base $model |
||
| 1498 | * @param WP_REST_Request $request |
||
| 1499 | * @param null $context |
||
| 1500 | * @return array |
||
| 1501 | * @throws EE_Error |
||
| 1502 | */ |
||
| 1503 | public function getOneOrReportPermissionError(EEM_Base $model, WP_REST_Request $request, $context = null) |
||
| 1547 | |||
| 1548 | /** |
||
| 1549 | * Checks that if this content requires a password to be read, that it's been provided and is correct. |
||
| 1550 | * @since 4.9.74.p |
||
| 1551 | * @param EEM_Base $model |
||
| 1552 | * @param $model_row |
||
| 1553 | * @param $query_params Adds 'default_where_conditions' => 'minimum' to ensure we don't confuse trashed with |
||
| 1554 | * password protected. |
||
| 1555 | * @param WP_REST_Request $request |
||
| 1556 | * @throws EE_Error |
||
| 1557 | * @throws InvalidArgumentException |
||
| 1558 | * @throws InvalidDataTypeException |
||
| 1559 | * @throws InvalidInterfaceException |
||
| 1560 | * @throws RestPasswordRequiredException |
||
| 1561 | * @throws RestPasswordIncorrectException |
||
| 1562 | * @throws \EventEspresso\core\exceptions\ModelConfigurationException |
||
| 1563 | * @throws ReflectionException |
||
| 1564 | */ |
||
| 1565 | protected function checkPassword(EEM_Base $model, $model_row, $query_params, WP_REST_Request $request) |
||
| 1602 | } |
||
| 1603 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: