/** * @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); }
/** * Returns a slice of Model_Version_Info::model_changes()'s array * indicating exactly what changes happened between the current core version, * and the version requested * * @return array */ public function resource_changes_between_requested_version_and_current() { if ($this->_cached_resource_changes_between_requested_version_and_current === null) { $resource_changes = array(); foreach ($this->resource_changes() as $version => $model_classnames) { if ($version <= \EED_Core_Rest_Api::core_version() && $version > $this->requested_version()) { $resource_changes[$version] = $model_classnames; } } $this->_cached_resource_changes_between_requested_version_and_current = $resource_changes; } return $this->_cached_resource_changes_between_requested_version_and_current; }
/** * Adds EE metadata to the index * @param \WP_REST_Response $rest_response_obj * @return \WP_REST_Response */ public static function filter_ee_metadata_into_index(\WP_REST_Response $rest_response_obj) { $response_data = $rest_response_obj->get_data(); $addons = array(); foreach (\EE_Registry::instance()->addons as $addon) { $addon_json = array('name' => $addon->name(), 'version' => $addon->version()); $addons[$addon_json['name']] = $addon_json; } $response_data['ee'] = array('version' => \EEM_System_Status::instance()->get_ee_version(), 'addons' => $addons, 'maintenance_mode' => \EE_Maintenance_Mode::instance()->real_level(), 'served_core_versions' => array_keys(\EED_Core_Rest_Api::versions_served())); $rest_response_obj->set_data($response_data); return $rest_response_obj; }
/** * Using EED_Core_Rest_Api::version_compatibilities(), determines what version of * EE the API can serve requests for. Eg, if we are on 4.15 of core, and * we can serve requests from 4.12 or later, this will return array( '4.12', '4.13', '4.14', '4.15' ). * We also indicate whether or not this version should be put in the index or not * @return array keys are API version numbers (just major and minor numbers), and values * are whether or not they should be hidden */ public static function versions_served() { $version_compatibilities = EED_Core_Rest_Api::version_compatibilities(); $versions_served = array(); $lowest_compatible_version = $version_compatibilities[EED_Core_Rest_Api::core_version()]; //for each version of core we have ever served: foreach (array_keys(EED_Core_Rest_Api::version_compatibilities()) as $possibly_served_version) { //if it's not above the current core version, and it's compatible with the current version of core if ($possibly_served_version < EED_Core_Rest_Api::core_version() && $possibly_served_version >= $lowest_compatible_version) { $versions_served[$possibly_served_version] = true; } else { $versions_served[$possibly_served_version] = false; } } return $versions_served; }
/** * 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(); } }
/** * Translates API filter get parameter into $query_params array used by EEM_Base::get_all(). * Note: right now the query parameter keys for fields (and related fields) * can be left as-is, but it's quite possible this will change someday. * Also, this method's contents might be candidate for moving to Model_Data_Translator * * @param \EEM_Base $model * @param array $query_parameters from $_GET parameter @see Read:handle_request_get_all * @return array like what EEM_Base::get_all() expects or FALSE to indicate * that absolutely no results should be returned * @throws \EE_Error */ public function create_model_query_params($model, $query_parameters) { $model_query_params = array(); if (isset($query_parameters['where'])) { $model_query_params[0] = Model_Data_Translator::prepare_conditions_query_params_for_models($query_parameters['where'], $model, $this->get_model_version_info()->requested_version()); } if (isset($query_parameters['order_by'])) { $order_by = $query_parameters['order_by']; } elseif (isset($query_parameters['orderby'])) { $order_by = $query_parameters['orderby']; } else { $order_by = null; } if ($order_by !== null) { $model_query_params['order_by'] = $order_by; } if (isset($query_parameters['group_by'])) { $group_by = $query_parameters['group_by']; } elseif (isset($query_parameters['groupby'])) { $group_by = $query_parameters['groupby']; } else { $group_by = array_keys($model->get_combined_primary_key_fields()); } if ($group_by !== null) { $model_query_params['group_by'] = $group_by; } if (isset($query_parameters['having'])) { $model_query_params['having'] = Model_Data_Translator::prepare_conditions_query_params_for_models($query_parameters['having'], $model, $this->get_model_version_info()->requested_version()); } if (isset($query_parameters['order'])) { $model_query_params['order'] = $query_parameters['order']; } if (isset($query_parameters['mine'])) { $model_query_params = $model->alter_query_params_to_only_include_mine($model_query_params); } if (isset($query_parameters['limit'])) { //limit should be either a string like '23' or '23,43', or an array with two items in it if (!is_array($query_parameters['limit'])) { $limit_array = explode(',', (string) $query_parameters['limit']); } else { $limit_array = $query_parameters['limit']; } $sanitized_limit = array(); foreach ($limit_array as $key => $limit_part) { if ($this->_debug_mode && (!is_numeric($limit_part) || count($sanitized_limit) > 2)) { throw new \EE_Error(sprintf(__('An invalid limit filter was provided. It was: %s. If the EE4 JSON REST API weren\'t in debug mode, this message would not appear.', 'event_espresso'), json_encode($query_parameters['limit']))); } $sanitized_limit[] = (int) $limit_part; } $model_query_params['limit'] = implode(',', $sanitized_limit); } else { $model_query_params['limit'] = \EED_Core_Rest_Api::get_default_query_limit(); } if (isset($query_parameters['caps'])) { $model_query_params['caps'] = $this->validate_context($query_parameters['caps']); } else { $model_query_params['caps'] = \EEM_Base::caps_read; } return apply_filters('FHEE__Read__create_model_query_params', $model_query_params, $query_parameters, $model); }
/** * Using EED_Core_Rest_Api::version_compatibilities(), determines what version of * EE the API can serve requests for. Eg, if we are on 4.15 of core, and * we can serve requests from 4.12 or later, this will return array( '4.12', '4.13', '4.14', '4.15' ). * We also indicate whether or not this version should be put in the index or not * @return array keys are API version numbers (just major and minor numbers), and values * are whether or not they should be hidden */ public static function versions_served() { $versions_served = array(); $possibly_served_versions = EED_Core_Rest_Api::version_compatibilities(); $lowest_compatible_version = end($possibly_served_versions); reset($possibly_served_versions); $versions_served_historically = array_keys($possibly_served_versions); $latest_version = end($versions_served_historically); reset($versions_served_historically); //for each version of core we have ever served: foreach ($versions_served_historically as $key_versioned_endpoint) { //if it's not above the current core version, and it's compatible with the current version of core if ($key_versioned_endpoint == $latest_version) { //don't hide the latest version in the index $versions_served[$key_versioned_endpoint] = false; } else { if ($key_versioned_endpoint < EED_Core_Rest_Api::core_version() && $key_versioned_endpoint >= $lowest_compatible_version) { //include, but hide, previous versions which are still supported $versions_served[$key_versioned_endpoint] = true; } elseif (apply_filters('FHEE__EED_Core_Rest_Api__versions_served__include_incompatible_versions', false, $possibly_served_versions)) { //if a version is no longer supported, don't include it in index or list of versions served $versions_served[$key_versioned_endpoint] = true; } } } return $versions_served; }
/** * 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); }