/**
  * Toggles whether the user is checked in or not.
  *
  * @param \WP_REST_Request $request
  * @return \WP_Error|\WP_REST_Response
  */
 protected function _create_checkin_checkout_object(\WP_REST_Request $request)
 {
     $reg_id = $request->get_param('REG_ID');
     $dtt_id = $request->get_param('DTT_ID');
     $force = $request->get_param('force');
     if ($force == 'true') {
         $force = true;
     } else {
         $force = false;
     }
     $reg = \EEM_Registration::instance()->get_one_by_ID($reg_id);
     if (!$reg instanceof \EE_Registration) {
         return $this->send_response(new \WP_Error('rest_registration_toggle_checkin_invalid_id', sprintf(__('You cannot checkin registration with ID %1$s because it doesn\'t exist.', 'event_espresso'), $reg_id), array('status' => 422)));
     }
     if (!\EE_Capabilities::instance()->current_user_can('ee_edit_checkin', 'rest_api_checkin_endpoint', $reg_id)) {
         return $this->send_response(new \WP_Error('rest_user_cannot_toggle_checkin', sprintf(__('You are not allowed to checkin registration with ID %1$s.', 'event_espresso'), $reg_id), array('status' => 403)));
     }
     $success = $reg->toggle_checkin_status($dtt_id, !$force);
     if ($success === false) {
         //rely on EE_Error::add_error messages to have been added to give more data about hwy it failed
         return $this->send_response(new \WP_Error('rest_toggle_checkin_failed', __('Registration checkin failed. Please see additional error data.', 'event_espresso')));
     }
     $checkin = \EEM_Checkin::instance()->get_one(array(array('REG_ID' => $reg_id, 'DTT_ID' => $dtt_id), 'order_by' => array('CHK_timestamp' => 'DESC')));
     if (!$checkin instanceof \EE_Checkin) {
         return $this->send_response(new \WP_Error('rest_toggle_checkin_error', sprintf(__('Supposedly we created a new checkin object for registration %1$s at datetime %2$s, but we can\'t find it.', 'event_espresso'), $reg_id, $dtt_id)));
     }
     $requested_version = $this->get_requested_version($request->get_route());
     $get_request = new \WP_REST_Request('GET', \EED_Core_Rest_Api::ee_api_namespace . $requested_version . '/checkins/' . $checkin->ID());
     $get_request->set_url_params(array('id' => $checkin->ID()));
     return Read::handle_request_get_one($get_request);
 }
예제 #2
0
 /**
  *
  * Gets all the related entities (or if its a belongs-to relation just the one)
  * to the item with the given id
  *
  * @param \WP_REST_Request $request
  * @return \WP_REST_Response|\WP_Error
  */
 public static function handle_request_get_related(\WP_REST_Request $request)
 {
     $controller = new Read();
     try {
         $matches = $controller->parse_route($request->get_route(), '~' . \EED_Core_Rest_Api::ee_api_namespace_for_regex . '(.*)/(.*)/(.*)~', array('version', 'model', 'id', 'related_model'));
         $controller->set_requested_version($matches['version']);
         $main_model_name_singular = \EEH_Inflector::singularize_and_upper($matches['model']);
         if (!$controller->get_model_version_info()->is_model_name_in_this_version($main_model_name_singular)) {
             return $controller->send_response(new \WP_Error('endpoint_parsing_error', sprintf(__('There is no model for endpoint %s. Please contact event espresso support', 'event_espresso'), $main_model_name_singular)));
         }
         $main_model = $controller->get_model_version_info()->load_model($main_model_name_singular);
         //assume the related model name is plural and try to find the model's name
         $related_model_name_singular = \EEH_Inflector::singularize_and_upper($matches['related_model']);
         if (!$controller->get_model_version_info()->is_model_name_in_this_version($related_model_name_singular)) {
             //so the word didn't singularize well. Maybe that's just because it's a singular word?
             $related_model_name_singular = \EEH_Inflector::humanize($matches['related_model']);
         }
         if (!$controller->get_model_version_info()->is_model_name_in_this_version($related_model_name_singular)) {
             return $controller->send_response(new \WP_Error('endpoint_parsing_error', sprintf(__('There is no model for endpoint %s. Please contact event espresso support', 'event_espresso'), $related_model_name_singular)));
         }
         return $controller->send_response($controller->get_entities_from_relation($request->get_param('id'), $main_model->related_settings_for($related_model_name_singular), $request));
     } catch (\Exception $e) {
         return $controller->send_response($e);
     }
 }
 /**
  * Removes the "_calculate_fields" part of entity responses before 4.8.36
  * @param array $entity_response_array
  * @param \EEM_Base $model
  * @param string $request_context
  * @param \WP_REST_Request $request
  * @param Read $controller
  * @return array
  */
 public function remove_old_featured_image_part_of_cpt_entities($entity_response_array, \EEM_Base $model, $request_context, \WP_REST_Request $request, Read $controller)
 {
     if ($this->applies_to_version($controller->get_model_version_info()->requested_version()) && $model instanceof \EEM_CPT_Base) {
         $attachment = wp_get_attachment_image_src(get_post_thumbnail_id($entity_response_array[$model->primary_key_name()]), 'full');
         $entity_response_array['featured_image_url'] = !empty($attachment) ? $attachment[0] : null;
     }
     return $entity_response_array;
 }
예제 #4
0
 /**
  * Adds the included models indicated in the request to the entity provided
  * @param \EEM_Base $model
  * @param \WP_REST_Request $rest_request
  * @param array $entity_array
  * @return array the modified entity
  */
 protected function _include_requested_models(\EEM_Base $model, \WP_REST_Request $rest_request, $entity_array)
 {
     $includes_for_this_model = $this->explode_and_get_items_prefixed_with($rest_request->get_param('include'), '');
     $includes_for_this_model = $this->_remove_model_names_from_array($includes_for_this_model);
     //if they passed in * or didn't specify any includes, return everything
     if (!in_array('*', $includes_for_this_model) && !empty($includes_for_this_model)) {
         if ($model->has_primary_key_field()) {
             //always include the primary key. ya just gotta know that at least
             $includes_for_this_model[] = $model->primary_key_name();
         }
         if ($this->explode_and_get_items_prefixed_with($rest_request->get_param('calculate'), '')) {
             $includes_for_this_model[] = '_calculated_fields';
         }
         $entity_array = array_intersect_key($entity_array, array_flip($includes_for_this_model));
     }
     $relation_settings = $this->get_model_version_info()->relation_settings($model);
     foreach ($relation_settings as $relation_name => $relation_obj) {
         $related_fields_to_include = $this->explode_and_get_items_prefixed_with($rest_request->get_param('include'), $relation_name);
         $related_fields_to_calculate = $this->explode_and_get_items_prefixed_with($rest_request->get_param('calculate'), $relation_name);
         //did they specify they wanted to include a related model, or
         //specific fields from a related model?
         //or did they specify to calculate a field from a related model?
         if ($related_fields_to_include || $related_fields_to_calculate) {
             //if so, we should include at least some part of the related model
             $pretend_related_request = new \WP_REST_Request();
             $pretend_related_request->set_query_params(array('caps' => $rest_request->get_param('caps'), 'include' => $related_fields_to_include, 'calculate' => $related_fields_to_calculate));
             $pretend_related_request->add_header('no_rest_headers', true);
             $related_results = $this->get_entities_from_relation($entity_array[$model->primary_key_name()], $relation_obj, $pretend_related_request);
             $entity_array[Read::get_related_entity_name($relation_name, $relation_obj)] = $related_results instanceof \WP_Error ? null : $related_results;
         }
     }
     return $entity_array;
 }