/** * @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); }
/** * Finds which version of the API was requested given the route, and returns it. * eg in a request to "mysite.com/wp-json/ee/v4.8.29/events/123" this would return * "4.8.29" * @param string $route * @return string */ public function get_requested_version($route) { $matches = $this->parse_route($route, '~' . \EED_Core_Rest_Api::ee_api_namespace_for_regex . '~', array('version')); if (isset($matches['version'])) { return $matches['version']; } else { return \EED_Core_Rest_Api::latest_rest_api_version(); } }
/** * Prepares an array of model query params for use in the REST API * @param type $model_query_params * @param \EEM_Base $model * @param string $requested_version eg "4.8.36". If null is provided, defaults to the latest release of the EE4 REST API * @return array which can be passed into the EE4 REST API when querying a model resource */ public static function prepare_query_params_for_rest_api($model_query_params, \EEM_Base $model, $requested_version = null) { if ($requested_version === null) { $requested_version = \EED_Core_Rest_Api::latest_rest_api_version(); } $rest_query_params = $model_query_params; if (isset($model_query_params[0])) { $rest_query_params['where'] = Model_Data_Translator::prepare_conditions_query_params_for_rest_api($model_query_params[0], $model, $requested_version); unset($rest_query_params[0]); } if (isset($model_query_params['having'])) { $rest_query_params['having'] = Model_Data_Translator::prepare_conditions_query_params_for_rest_api($model_query_params['having'], $model, $requested_version); } return apply_filters('FHEE__EventEspresso\\core\\libraries\\rest_api\\Model_Data_Translator__prepare_query_params_for_rest_api', $rest_query_params, $model_query_params, $model, $requested_version); }