/**
  * @param array $input_settings     {
  * @type string $model_name         the name of model to be used for searching, both via the REST API and server-side model queries
  * @type array  $query_params       default query parameters which will apply to both REST API queries and server-side queries. This should be 
  *									in the exact format that will be used for server-side model usage (eg use index 0 for where conditions, not 
  *									the string "where")
  * @type string $value_field_name   the name of the model field on this model to
  *                                  be used for the HTML select's option's values
  * @type string $display_field_name the name of the model field on this model
  *                                  to be used for the HTML select's option's display text
  * @type array  $select2_args       arguments to be passed directly into the select2's JS constructor
  *                                  }
  *                                  And the arguments accepted by EE_Form_Input_With_Options_Base
  * @throws \EE_Error
  */
 public function __construct($input_settings = array())
 {
     //needed input settings:
     //select2_args
     $this->_model_name = EEH_Array::is_set($input_settings, 'model_name', null);
     $model = $this->_get_model();
     $query_params = EEH_Array::is_set($input_settings, 'query_params', array('limit' => 10, 'caps' => EEM_Base::caps_read_admin));
     $this->_value_field_name = EEH_Array::is_set($input_settings, 'value_field_name', $model->primary_key_name());
     $this->_display_field_name = EEH_Array::is_set($input_settings, 'display_field_name', $model->get_a_field_of_type('EE_Text_Field_Base')->get_name());
     $this->_add_validation_strategy(new EE_Model_Matching_Query_Validation_Strategy('', $this->_model_name, $query_params, $this->_value_field_name));
     //get resource endpoint
     $rest_controller = new EventEspresso\core\libraries\rest_api\controllers\model\Read();
     $rest_controller->set_requested_version(EED_Core_Rest_Api::latest_rest_api_version());
     $url = $rest_controller->get_versioned_link_to(EEH_Inflector::pluralize_and_lower($this->_model_name));
     $default_select2_args = array('ajax' => array('url' => $url, 'dataType' => 'json', 'delay' => '250', 'data_interface' => 'EE_Select2_REST_API_Interface', 'data_interface_args' => array('default_query_params' => (object) Model_Data_Translator::prepare_query_params_for_rest_api($query_params, $model), 'display_field' => $this->_display_field_name, 'value_field' => $this->_value_field_name, 'nonce' => wp_create_nonce('wp_rest'))), 'cache' => true, 'width' => '100');
     $select2_args = array_replace_recursive($default_select2_args, (array) EEH_Array::is_set($input_settings, 'select2_args', array()));
     $this->set_display_strategy(new EE_Select2_Display_Strategy($select2_args));
     parent::__construct(array(), $input_settings);
 }
コード例 #2
0
 /**
  * Gets all the route information relating to EE models
  * @return array @see get_ee_route_data
  */
 protected function _register_model_routes()
 {
     EE_Registry::instance()->load_helper('Inflector');
     $models_to_register = apply_filters('FHEE__EED_Core_REST_API___register_model_routes', EE_Registry::instance()->non_abstract_db_models);
     //let's not bother having endpoints for extra metas
     unset($models_to_register['Extra_Meta']);
     unset($models_to_register['Extra_Join']);
     $model_routes = array();
     foreach (self::versions_served() as $version => $hidden_endpoint) {
         foreach ($models_to_register as $model_name => $model_classname) {
             //yes we could just register one route for ALL models, but then they wouldn't show up in the index
             $ee_namespace = self::ee_api_namespace . $version;
             $plural_model_route = EEH_Inflector::pluralize_and_lower($model_name);
             $singular_model_route = $plural_model_route . '/(?P<id>\\d+)';
             $model_routes[$ee_namespace][$plural_model_route] = array(array('callback' => array('EventEspresso\\core\\libraries\\rest_api\\controllers\\model\\Read', 'handle_request_get_all'), 'methods' => WP_REST_Server::READABLE, 'hidden_endpoint' => $hidden_endpoint, 'args' => $this->_get_read_query_params($model_name), '_links' => array('self' => rest_url($ee_namespace . $singular_model_route))));
             $model_routes[$ee_namespace][$singular_model_route] = array(array('callback' => array('EventEspresso\\core\\libraries\\rest_api\\controllers\\model\\Read', 'handle_request_get_one'), 'methods' => WP_REST_Server::READABLE, 'hidden_endpoint' => $hidden_endpoint, 'args' => array('include' => array('required' => false, 'default' => '*', 'description' => __('See http://developer.eventespresso.com/docs/ee4-rest-api-reading/#Including_Specific_Fields_and_Related_Entities_in_Results for documentation', 'event_espresso')))));
             //@todo: also handle  DELETE for a single item
             $model = EE_Registry::instance()->load_model($model_classname);
             foreach ($model->relation_settings() as $relation_name => $relation_obj) {
                 $related_model_name_endpoint_part = EventEspresso\core\libraries\rest_api\controllers\model\Read::get_related_entity_name($relation_name, $relation_obj);
                 $model_routes[$ee_namespace][$singular_model_route . '/' . $related_model_name_endpoint_part] = array(array('callback' => array('EventEspresso\\core\\libraries\\rest_api\\controllers\\model\\Read', 'handle_request_get_related'), 'methods' => WP_REST_Server::READABLE, 'hidden_endpoint' => $hidden_endpoint, 'args' => $this->_get_read_query_params($relation_name)));
                 //@todo: handle delete related and possibly remove relation (not sure hwo to distinguish)
             }
         }
     }
     return $model_routes;
 }
コード例 #3
0
 /**
  * Gets the route data for EE models in the specified version
  * @param string $version
  * @param boolean $hidden_endpoint
  * @return array
  */
 protected function _get_model_route_data_for_version($version, $hidden_endpoint = false)
 {
     $model_version_info = new \EventEspresso\core\libraries\rest_api\Model_Version_Info($version);
     $models_to_register = apply_filters('FHEE__EED_Core_REST_API___register_model_routes', $model_version_info->models_for_requested_version());
     //let's not bother having endpoints for extra metas
     unset($models_to_register['Extra_Meta']);
     unset($models_to_register['Extra_Join']);
     $model_routes = array();
     foreach ($models_to_register as $model_name => $model_classname) {
         $model = \EE_Registry::instance()->load_model($model_name);
         //yes we could just register one route for ALL models, but then they wouldn't show up in the index
         $plural_model_route = EEH_Inflector::pluralize_and_lower($model_name);
         $singular_model_route = $plural_model_route . '/(?P<id>\\d+)';
         $model_routes[$plural_model_route] = array(array('callback' => array('EventEspresso\\core\\libraries\\rest_api\\controllers\\model\\Read', 'handle_request_get_all'), 'methods' => WP_REST_Server::READABLE, 'hidden_endpoint' => $hidden_endpoint, 'args' => $this->_get_read_query_params($model, $version), '_links' => array('self' => rest_url(EED_Core_Rest_Api::ee_api_namespace . $version . $singular_model_route))));
         $model_routes[$singular_model_route] = array(array('callback' => array('EventEspresso\\core\\libraries\\rest_api\\controllers\\model\\Read', 'handle_request_get_one'), 'methods' => WP_REST_Server::READABLE, 'hidden_endpoint' => $hidden_endpoint, 'args' => $this->_get_response_selection_query_params($model, $version)));
         //@todo: also handle  DELETE for a single item
         foreach ($model->relation_settings() as $relation_name => $relation_obj) {
             $related_model_name_endpoint_part = EventEspresso\core\libraries\rest_api\controllers\model\Read::get_related_entity_name($relation_name, $relation_obj);
             $model_routes[$singular_model_route . '/' . $related_model_name_endpoint_part] = array(array('callback' => array('EventEspresso\\core\\libraries\\rest_api\\controllers\\model\\Read', 'handle_request_get_related'), 'methods' => WP_REST_Server::READABLE, 'hidden_endpoint' => $hidden_endpoint, 'args' => $this->_get_read_query_params($relation_obj->get_other_model(), $version)));
             //@todo: handle delete related and possibly remove relation (not sure hwo to distinguish)
         }
     }
     return $model_routes;
 }