Ejemplo n.º 1
0
 /**
  * Gets the collection for given relation object
  *
  * The same as Read::get_entities_from_model(), except if the relation
  * is a HABTM relation, in which case it merges any non-foreign-key fields from
  * the join-model-object into the results
  *
  * @param string $id the ID of the thing we are fetching related stuff from
  * @param \EE_Model_Relation_Base $relation
  * @param \WP_REST_Request $request
  * @return array
  */
 public function get_entities_from_relation($id, $relation, $request)
 {
     $context = $this->validate_context($request->get_param('caps'));
     $model = $relation->get_this_model();
     $related_model = $relation->get_other_model();
     //check if they can access the 1st model object
     $query_params = array(array($model->primary_key_name() => $id), 'limit' => 1);
     if ($model instanceof \EEM_Soft_Delete_Base) {
         $query_params = $model->alter_query_params_so_deleted_and_undeleted_items_included($query_params);
     }
     $restricted_query_params = $query_params;
     $restricted_query_params['caps'] = $context;
     $this->_set_debug_info('main model query params', $restricted_query_params);
     $this->_set_debug_info('missing caps', Capabilities::get_missing_permissions_string($related_model, $context));
     if (!(Capabilities::current_user_has_partial_access_to($related_model, $context) && $model->exists($restricted_query_params))) {
         if ($relation instanceof \EE_Belongs_To_Relation) {
             $related_model_name_maybe_plural = strtolower($related_model->get_this_model_name());
         } else {
             $related_model_name_maybe_plural = \EEH_Inflector::pluralize_and_lower($related_model->get_this_model_name());
         }
         return new \WP_Error(sprintf('rest_%s_cannot_list', $related_model_name_maybe_plural), sprintf(__('Sorry, you are not allowed to list %1$s related to %2$s. Missing permissions: %3$s', 'event_espresso'), $related_model_name_maybe_plural, $relation->get_this_model()->get_this_model_name(), implode(',', array_keys(Capabilities::get_missing_permissions($related_model, $context)))), array('status' => 403));
     }
     $query_params = $this->create_model_query_params($relation->get_other_model(), $request->get_params());
     $query_params[0][$relation->get_this_model()->get_this_model_name() . '.' . $relation->get_this_model()->primary_key_name()] = $id;
     $query_params['default_where_conditions'] = 'none';
     $query_params['caps'] = $context;
     $this->_set_debug_info('model query params', $query_params);
     /** @type array $results */
     $results = $relation->get_other_model()->get_all_wpdb_results($query_params);
     $nice_results = array();
     foreach ($results as $result) {
         $nice_result = $this->create_entity_from_wpdb_result($relation->get_other_model(), $result, $request->get_param('include'), $query_params['caps']);
         if ($relation instanceof \EE_HABTM_Relation) {
             //put the unusual stuff (properties from the HABTM relation) first, and make sure
             //if there are conflicts we prefer the properties from the main model
             $join_model_result = $this->create_entity_from_wpdb_result($relation->get_join_model(), $result, $request->get_param('include'), $query_params['caps']);
             $joined_result = array_merge($nice_result, $join_model_result);
             //but keep the meta stuff from the main model
             if (isset($nice_result['meta'])) {
                 $joined_result['meta'] = $nice_result['meta'];
             }
             $nice_result = $joined_result;
         }
         $nice_results[] = $nice_result;
     }
     if ($relation instanceof \EE_Belongs_To_Relation) {
         return array_shift($nice_results);
     } else {
         return $nice_results;
     }
 }