| @@ -6,8 +6,8 @@ discard block | ||
| 6 | 6 | use EventEspresso\core\libraries\rest_api\Rest_Exception; | 
| 7 | 7 | use EventEspresso\core\libraries\rest_api\Model_Data_Translator; | 
| 8 | 8 | |
| 9 | -if ( !defined( 'EVENT_ESPRESSO_VERSION' ) ) { | |
| 10 | - exit( 'No direct script access allowed' ); | |
| 9 | +if ( ! defined('EVENT_ESPRESSO_VERSION')) { | |
| 10 | +	exit('No direct script access allowed'); | |
| 11 | 11 | } | 
| 12 | 12 | |
| 13 | 13 | /** | 
| @@ -46,22 +46,22 @@ discard block | ||
| 46 | 46 | * @param \WP_REST_Request $request | 
| 47 | 47 | * @return \WP_REST_Response|\WP_Error | 
| 48 | 48 | */ | 
| 49 | -	public static function handle_request_get_all( \WP_REST_Request $request) { | |
| 49 | +	public static function handle_request_get_all(\WP_REST_Request $request) { | |
| 50 | 50 | $controller = new Read(); | 
| 51 | -		try{ | |
| 51 | +		try { | |
| 52 | 52 | $matches = $controller->parse_route( | 
| 53 | 53 | $request->get_route(), | 
| 54 | - '~' . \EED_Core_Rest_Api::ee_api_namespace_for_regex . '(.*)~', | |
| 55 | - array( 'version', 'model' ) | |
| 54 | + '~'.\EED_Core_Rest_Api::ee_api_namespace_for_regex.'(.*)~', | |
| 55 | +				array('version', 'model') | |
| 56 | 56 | ); | 
| 57 | - $controller->set_requested_version( $matches[ 'version' ] ); | |
| 58 | - $model_name_singular = \EEH_Inflector::singularize_and_upper( $matches[ 'model' ] ); | |
| 59 | -			if ( ! $controller->get_model_version_info()->is_model_name_in_this_version( $model_name_singular ) ) { | |
| 57 | + $controller->set_requested_version($matches['version']); | |
| 58 | + $model_name_singular = \EEH_Inflector::singularize_and_upper($matches['model']); | |
| 59 | +			if ( ! $controller->get_model_version_info()->is_model_name_in_this_version($model_name_singular)) { | |
| 60 | 60 | return $controller->send_response( | 
| 61 | 61 | new \WP_Error( | 
| 62 | 62 | 'endpoint_parsing_error', | 
| 63 | 63 | sprintf( | 
| 64 | - __( 'There is no model for endpoint %s. Please contact event espresso support', 'event_espresso' ), | |
| 64 | +							__('There is no model for endpoint %s. Please contact event espresso support', 'event_espresso'), | |
| 65 | 65 | $model_name_singular | 
| 66 | 66 | ) | 
| 67 | 67 | ) | 
| @@ -69,12 +69,12 @@ discard block | ||
| 69 | 69 | } | 
| 70 | 70 | return $controller->send_response( | 
| 71 | 71 | $controller->get_entities_from_model( | 
| 72 | - $controller->get_model_version_info()->load_model( $model_name_singular ), | |
| 72 | + $controller->get_model_version_info()->load_model($model_name_singular), | |
| 73 | 73 | $request | 
| 74 | 74 | ) | 
| 75 | 75 | ); | 
| 76 | -		} catch( \Exception $e ) { | |
| 77 | - return $controller->send_response( $e ); | |
| 76 | +		} catch (\Exception $e) { | |
| 77 | + return $controller->send_response($e); | |
| 78 | 78 | } | 
| 79 | 79 | } | 
| 80 | 80 | |
| @@ -84,21 +84,21 @@ discard block | ||
| 84 | 84 | * @param \WP_REST_Request $request | 
| 85 | 85 | * @return \WP_REST_Response|\WP_Error | 
| 86 | 86 | */ | 
| 87 | -	public static function handle_request_get_one( \WP_REST_Request $request ) { | |
| 87 | +	public static function handle_request_get_one(\WP_REST_Request $request) { | |
| 88 | 88 | $controller = new Read(); | 
| 89 | -		try{ | |
| 89 | +		try { | |
| 90 | 90 | $matches = $controller->parse_route( | 
| 91 | 91 | $request->get_route(), | 
| 92 | - '~' . \EED_Core_Rest_Api::ee_api_namespace_for_regex . '(.*)/(.*)~', | |
| 93 | - array( 'version', 'model', 'id' ) ); | |
| 94 | - $controller->set_requested_version( $matches[ 'version' ] ); | |
| 95 | - $model_name_singular = \EEH_Inflector::singularize_and_upper( $matches[ 'model' ] ); | |
| 96 | -			if ( ! $controller->get_model_version_info()->is_model_name_in_this_version( $model_name_singular ) ) { | |
| 92 | + '~'.\EED_Core_Rest_Api::ee_api_namespace_for_regex.'(.*)/(.*)~', | |
| 93 | +				array('version', 'model', 'id') ); | |
| 94 | + $controller->set_requested_version($matches['version']); | |
| 95 | + $model_name_singular = \EEH_Inflector::singularize_and_upper($matches['model']); | |
| 96 | +			if ( ! $controller->get_model_version_info()->is_model_name_in_this_version($model_name_singular)) { | |
| 97 | 97 | return $controller->send_response( | 
| 98 | 98 | new \WP_Error( | 
| 99 | 99 | 'endpoint_parsing_error', | 
| 100 | 100 | sprintf( | 
| 101 | - __( 'There is no model for endpoint %s. Please contact event espresso support', 'event_espresso' ), | |
| 101 | +							__('There is no model for endpoint %s. Please contact event espresso support', 'event_espresso'), | |
| 102 | 102 | $model_name_singular | 
| 103 | 103 | ) | 
| 104 | 104 | ) | 
| @@ -106,12 +106,12 @@ discard block | ||
| 106 | 106 | } | 
| 107 | 107 | return $controller->send_response( | 
| 108 | 108 | $controller->get_entity_from_model( | 
| 109 | - $controller->get_model_version_info()->load_model( $model_name_singular ), | |
| 109 | + $controller->get_model_version_info()->load_model($model_name_singular), | |
| 110 | 110 | $request | 
| 111 | 111 | ) | 
| 112 | 112 | ); | 
| 113 | -		} catch( \Exception $e ) { | |
| 114 | - return $controller->send_response( $e ); | |
| 113 | +		} catch (\Exception $e) { | |
| 114 | + return $controller->send_response($e); | |
| 115 | 115 | } | 
| 116 | 116 | } | 
| 117 | 117 | |
| @@ -123,40 +123,40 @@ discard block | ||
| 123 | 123 | * @param \WP_REST_Request $request | 
| 124 | 124 | * @return \WP_REST_Response|\WP_Error | 
| 125 | 125 | */ | 
| 126 | -	public static function handle_request_get_related( \WP_REST_Request $request ) { | |
| 126 | +	public static function handle_request_get_related(\WP_REST_Request $request) { | |
| 127 | 127 | $controller = new Read(); | 
| 128 | -		try{ | |
| 128 | +		try { | |
| 129 | 129 | $matches = $controller->parse_route( | 
| 130 | 130 | $request->get_route(), | 
| 131 | - '~' . \EED_Core_Rest_Api::ee_api_namespace_for_regex . '(.*)/(.*)/(.*)~', | |
| 132 | - array( 'version', 'model', 'id', 'related_model' ) | |
| 131 | + '~'.\EED_Core_Rest_Api::ee_api_namespace_for_regex.'(.*)/(.*)/(.*)~', | |
| 132 | +				array('version', 'model', 'id', 'related_model') | |
| 133 | 133 | ); | 
| 134 | - $controller->set_requested_version( $matches[ 'version' ] ); | |
| 135 | - $main_model_name_singular = \EEH_Inflector::singularize_and_upper( $matches[ 'model' ] ); | |
| 136 | -			if ( ! $controller->get_model_version_info()->is_model_name_in_this_version( $main_model_name_singular ) ) { | |
| 134 | + $controller->set_requested_version($matches['version']); | |
| 135 | + $main_model_name_singular = \EEH_Inflector::singularize_and_upper($matches['model']); | |
| 136 | +			if ( ! $controller->get_model_version_info()->is_model_name_in_this_version($main_model_name_singular)) { | |
| 137 | 137 | return $controller->send_response( | 
| 138 | 138 | new \WP_Error( | 
| 139 | 139 | 'endpoint_parsing_error', | 
| 140 | 140 | sprintf( | 
| 141 | - __( 'There is no model for endpoint %s. Please contact event espresso support', 'event_espresso' ), | |
| 141 | +							__('There is no model for endpoint %s. Please contact event espresso support', 'event_espresso'), | |
| 142 | 142 | $main_model_name_singular | 
| 143 | 143 | ) | 
| 144 | 144 | ) | 
| 145 | 145 | ); | 
| 146 | 146 | } | 
| 147 | - $main_model = $controller->get_model_version_info()->load_model( $main_model_name_singular ); | |
| 147 | + $main_model = $controller->get_model_version_info()->load_model($main_model_name_singular); | |
| 148 | 148 | //assume the related model name is plural and try to find the model's name | 
| 149 | - $related_model_name_singular = \EEH_Inflector::singularize_and_upper( $matches[ 'related_model' ] ); | |
| 150 | -			if ( ! $controller->get_model_version_info()->is_model_name_in_this_version( $related_model_name_singular ) ) { | |
| 149 | + $related_model_name_singular = \EEH_Inflector::singularize_and_upper($matches['related_model']); | |
| 150 | +			if ( ! $controller->get_model_version_info()->is_model_name_in_this_version($related_model_name_singular)) { | |
| 151 | 151 | //so the word didn't singularize well. Maybe that's just because it's a singular word? | 
| 152 | - $related_model_name_singular = \EEH_Inflector::humanize( $matches[ 'related_model' ] ); | |
| 152 | + $related_model_name_singular = \EEH_Inflector::humanize($matches['related_model']); | |
| 153 | 153 | } | 
| 154 | -			if ( ! $controller->get_model_version_info()->is_model_name_in_this_version( $related_model_name_singular ) ) { | |
| 154 | +			if ( ! $controller->get_model_version_info()->is_model_name_in_this_version($related_model_name_singular)) { | |
| 155 | 155 | return $controller->send_response( | 
| 156 | 156 | new \WP_Error( | 
| 157 | 157 | 'endpoint_parsing_error', | 
| 158 | 158 | sprintf( | 
| 159 | - __( 'There is no model for endpoint %s. Please contact event espresso support', 'event_espresso' ), | |
| 159 | +							__('There is no model for endpoint %s. Please contact event espresso support', 'event_espresso'), | |
| 160 | 160 | $related_model_name_singular | 
| 161 | 161 | ) | 
| 162 | 162 | ) | 
| @@ -165,13 +165,13 @@ discard block | ||
| 165 | 165 | |
| 166 | 166 | return $controller->send_response( | 
| 167 | 167 | $controller->get_entities_from_relation( | 
| 168 | - $request->get_param( 'id' ), | |
| 169 | - $main_model->related_settings_for( $related_model_name_singular ) , | |
| 168 | +						$request->get_param('id'), | |
| 169 | + $main_model->related_settings_for($related_model_name_singular), | |
| 170 | 170 | $request | 
| 171 | 171 | ) | 
| 172 | 172 | ); | 
| 173 | -		} catch( \Exception $e ) { | |
| 174 | - return $controller->send_response( $e ); | |
| 173 | +		} catch (\Exception $e) { | |
| 174 | + return $controller->send_response($e); | |
| 175 | 175 | } | 
| 176 | 176 | } | 
| 177 | 177 | |
| @@ -184,28 +184,28 @@ discard block | ||
| 184 | 184 | * @param \WP_REST_Request $request | 
| 185 | 185 | * @return array | 
| 186 | 186 | */ | 
| 187 | -	public function get_entities_from_model( $model, $request) { | |
| 188 | - $query_params = $this->create_model_query_params( $model, $request->get_params() ); | |
| 189 | -		if( ! Capabilities::current_user_has_partial_access_to( $model, $query_params[ 'caps' ] ) ) { | |
| 190 | - $model_name_plural = \EEH_Inflector::pluralize_and_lower( $model->get_this_model_name() ); | |
| 187 | +	public function get_entities_from_model($model, $request) { | |
| 188 | + $query_params = $this->create_model_query_params($model, $request->get_params()); | |
| 189 | +		if ( ! Capabilities::current_user_has_partial_access_to($model, $query_params['caps'])) { | |
| 190 | + $model_name_plural = \EEH_Inflector::pluralize_and_lower($model->get_this_model_name()); | |
| 191 | 191 | return new \WP_Error( | 
| 192 | - sprintf( 'rest_%s_cannot_list', $model_name_plural ), | |
| 192 | +				sprintf('rest_%s_cannot_list', $model_name_plural), | |
| 193 | 193 | sprintf( | 
| 194 | - __( 'Sorry, you are not allowed to list %1$s. Missing permissions: %2$s', 'event_espresso' ), | |
| 194 | +					__('Sorry, you are not allowed to list %1$s. Missing permissions: %2$s', 'event_espresso'), | |
| 195 | 195 | $model_name_plural, | 
| 196 | - Capabilities::get_missing_permissions_string( $model, $query_params[ 'caps' ] ) | |
| 196 | + Capabilities::get_missing_permissions_string($model, $query_params['caps']) | |
| 197 | 197 | ), | 
| 198 | - array( 'status' => 403 ) | |
| 198 | +				array('status' => 403) | |
| 199 | 199 | ); | 
| 200 | 200 | } | 
| 201 | -		if( ! $request->get_header( 'no_rest_headers' ) ) { | |
| 202 | - $this->_set_headers_from_query_params( $model, $query_params ); | |
| 201 | +		if ( ! $request->get_header('no_rest_headers')) { | |
| 202 | + $this->_set_headers_from_query_params($model, $query_params); | |
| 203 | 203 | } | 
| 204 | 204 | /** @type array $results */ | 
| 205 | - $results = $model->get_all_wpdb_results( $query_params ); | |
| 205 | + $results = $model->get_all_wpdb_results($query_params); | |
| 206 | 206 | $nice_results = array( ); | 
| 207 | -		foreach ( $results as $result ) { | |
| 208 | - $nice_results[ ] = $this->create_entity_from_wpdb_result( | |
| 207 | +		foreach ($results as $result) { | |
| 208 | + $nice_results[] = $this->create_entity_from_wpdb_result( | |
| 209 | 209 | $model, | 
| 210 | 210 | $result, | 
| 211 | 211 | $request | 
| @@ -226,64 +226,64 @@ discard block | ||
| 226 | 226 | * @param \WP_REST_Request $request | 
| 227 | 227 | * @return array | 
| 228 | 228 | */ | 
| 229 | -	public function get_entities_from_relation( $id,  $relation, $request ) { | |
| 230 | - $context = $this->validate_context( $request->get_param( 'caps' )); | |
| 229 | +	public function get_entities_from_relation($id, $relation, $request) { | |
| 230 | +		$context = $this->validate_context($request->get_param('caps')); | |
| 231 | 231 | $model = $relation->get_this_model(); | 
| 232 | 232 | $related_model = $relation->get_other_model(); | 
| 233 | 233 | //check if they can access the 1st model object | 
| 234 | - $query_params = array( array( $model->primary_key_name() => $id ),'limit' => 1 ); | |
| 235 | -		if( $model instanceof \EEM_Soft_Delete_Base ){ | |
| 234 | + $query_params = array(array($model->primary_key_name() => $id), 'limit' => 1); | |
| 235 | +		if ($model instanceof \EEM_Soft_Delete_Base) { | |
| 236 | 236 | $query_params = $model->alter_query_params_so_deleted_and_undeleted_items_included($query_params); | 
| 237 | 237 | } | 
| 238 | 238 | $restricted_query_params = $query_params; | 
| 239 | - $restricted_query_params[ 'caps' ] = $context; | |
| 240 | - $this->_set_debug_info( 'main model query params', $restricted_query_params ); | |
| 241 | - $this->_set_debug_info( 'missing caps', Capabilities::get_missing_permissions_string( $related_model, $context ) ); | |
| 239 | + $restricted_query_params['caps'] = $context; | |
| 240 | +		$this->_set_debug_info('main model query params', $restricted_query_params); | |
| 241 | +		$this->_set_debug_info('missing caps', Capabilities::get_missing_permissions_string($related_model, $context)); | |
| 242 | 242 | |
| 243 | - if( | |
| 243 | + if ( | |
| 244 | 244 | ! ( | 
| 245 | - Capabilities::current_user_has_partial_access_to( $related_model, $context ) | |
| 246 | - && $model->exists( $restricted_query_params ) | |
| 245 | + Capabilities::current_user_has_partial_access_to($related_model, $context) | |
| 246 | + && $model->exists($restricted_query_params) | |
| 247 | 247 | ) | 
| 248 | -		){ | |
| 249 | -			if( $relation instanceof \EE_Belongs_To_Relation ) { | |
| 250 | - $related_model_name_maybe_plural = strtolower( $related_model->get_this_model_name() ); | |
| 251 | -			}else{ | |
| 252 | - $related_model_name_maybe_plural = \EEH_Inflector::pluralize_and_lower( $related_model->get_this_model_name() ); | |
| 248 | +		) { | |
| 249 | +			if ($relation instanceof \EE_Belongs_To_Relation) { | |
| 250 | + $related_model_name_maybe_plural = strtolower($related_model->get_this_model_name()); | |
| 251 | +			} else { | |
| 252 | + $related_model_name_maybe_plural = \EEH_Inflector::pluralize_and_lower($related_model->get_this_model_name()); | |
| 253 | 253 | } | 
| 254 | 254 | return new \WP_Error( | 
| 255 | - sprintf( 'rest_%s_cannot_list', $related_model_name_maybe_plural ), | |
| 255 | +				sprintf('rest_%s_cannot_list', $related_model_name_maybe_plural), | |
| 256 | 256 | sprintf( | 
| 257 | - __( 'Sorry, you are not allowed to list %1$s related to %2$s. Missing permissions: %3$s', 'event_espresso' ), | |
| 257 | +					__('Sorry, you are not allowed to list %1$s related to %2$s. Missing permissions: %3$s', 'event_espresso'), | |
| 258 | 258 | $related_model_name_maybe_plural, | 
| 259 | 259 | $relation->get_this_model()->get_this_model_name(), | 
| 260 | 260 | implode( | 
| 261 | 261 | ',', | 
| 262 | 262 | array_keys( | 
| 263 | - Capabilities::get_missing_permissions( $related_model, $context ) | |
| 263 | + Capabilities::get_missing_permissions($related_model, $context) | |
| 264 | 264 | ) | 
| 265 | 265 | ) | 
| 266 | 266 | ), | 
| 267 | - array( 'status' => 403 ) | |
| 267 | +				array('status' => 403) | |
| 268 | 268 | ); | 
| 269 | 269 | } | 
| 270 | - $query_params = $this->create_model_query_params( $relation->get_other_model(), $request->get_params() ); | |
| 271 | - $query_params[0][ $relation->get_this_model()->get_this_model_name() . '.' . $relation->get_this_model()->primary_key_name() ] = $id; | |
| 272 | - $query_params[ 'default_where_conditions' ] = 'none'; | |
| 273 | - $query_params[ 'caps' ] = $context; | |
| 274 | -		if( ! $request->get_header( 'no_rest_headers' ) ) { | |
| 275 | - $this->_set_headers_from_query_params( $relation->get_other_model(), $query_params ); | |
| 270 | + $query_params = $this->create_model_query_params($relation->get_other_model(), $request->get_params()); | |
| 271 | + $query_params[0][$relation->get_this_model()->get_this_model_name().'.'.$relation->get_this_model()->primary_key_name()] = $id; | |
| 272 | + $query_params['default_where_conditions'] = 'none'; | |
| 273 | + $query_params['caps'] = $context; | |
| 274 | +		if ( ! $request->get_header('no_rest_headers')) { | |
| 275 | + $this->_set_headers_from_query_params($relation->get_other_model(), $query_params); | |
| 276 | 276 | } | 
| 277 | 277 | /** @type array $results */ | 
| 278 | - $results = $relation->get_other_model()->get_all_wpdb_results( $query_params ); | |
| 278 | + $results = $relation->get_other_model()->get_all_wpdb_results($query_params); | |
| 279 | 279 | $nice_results = array(); | 
| 280 | -		foreach( $results as $result ) { | |
| 280 | +		foreach ($results as $result) { | |
| 281 | 281 | $nice_result = $this->create_entity_from_wpdb_result( | 
| 282 | 282 | $relation->get_other_model(), | 
| 283 | 283 | $result, | 
| 284 | 284 | $request | 
| 285 | 285 | ); | 
| 286 | -			if( $relation instanceof \EE_HABTM_Relation ) { | |
| 286 | +			if ($relation instanceof \EE_HABTM_Relation) { | |
| 287 | 287 | //put the unusual stuff (properties from the HABTM relation) first, and make sure | 
| 288 | 288 | //if there are conflicts we prefer the properties from the main model | 
| 289 | 289 | $join_model_result = $this->create_entity_from_wpdb_result( | 
| @@ -291,18 +291,18 @@ discard block | ||
| 291 | 291 | $result, | 
| 292 | 292 | $request | 
| 293 | 293 | ); | 
| 294 | - $joined_result = array_merge( $nice_result, $join_model_result ); | |
| 294 | + $joined_result = array_merge($nice_result, $join_model_result); | |
| 295 | 295 | //but keep the meta stuff from the main model | 
| 296 | -				if( isset( $nice_result['meta'] ) ){ | |
| 296 | +				if (isset($nice_result['meta'])) { | |
| 297 | 297 | $joined_result['meta'] = $nice_result['meta']; | 
| 298 | 298 | } | 
| 299 | 299 | $nice_result = $joined_result; | 
| 300 | 300 | } | 
| 301 | 301 | $nice_results[] = $nice_result; | 
| 302 | 302 | } | 
| 303 | -		if( $relation instanceof \EE_Belongs_To_Relation ){ | |
| 304 | - return array_shift( $nice_results ); | |
| 305 | -		}else{ | |
| 303 | +		if ($relation instanceof \EE_Belongs_To_Relation) { | |
| 304 | + return array_shift($nice_results); | |
| 305 | +		} else { | |
| 306 | 306 | return $nice_results; | 
| 307 | 307 | } | 
| 308 | 308 | } | 
| @@ -315,30 +315,30 @@ discard block | ||
| 315 | 315 | * @param array $query_params | 
| 316 | 316 | * @return void | 
| 317 | 317 | */ | 
| 318 | -	protected function _set_headers_from_query_params( $model, $query_params ) { | |
| 319 | - $this->_set_debug_info( 'model query params', $query_params ); | |
| 320 | - $this->_set_debug_info( 'missing caps', Capabilities::get_missing_permissions_string( $model, $query_params[ 'caps' ] ) ); | |
| 318 | +	protected function _set_headers_from_query_params($model, $query_params) { | |
| 319 | +		$this->_set_debug_info('model query params', $query_params); | |
| 320 | +		$this->_set_debug_info('missing caps', Capabilities::get_missing_permissions_string($model, $query_params['caps'])); | |
| 321 | 321 | //normally the limit to a 2-part array, where the 2nd item is the limit | 
| 322 | -		if( ! isset( $query_params[ 'limit' ] ) ) { | |
| 323 | - $query_params[ 'limit' ] = \EED_Core_Rest_Api::get_default_query_limit(); | |
| 322 | +		if ( ! isset($query_params['limit'])) { | |
| 323 | + $query_params['limit'] = \EED_Core_Rest_Api::get_default_query_limit(); | |
| 324 | 324 | } | 
| 325 | -		if( is_array( $query_params[ 'limit' ] )  ) { | |
| 326 | - $limit_parts = $query_params[ 'limit' ]; | |
| 325 | +		if (is_array($query_params['limit'])) { | |
| 326 | + $limit_parts = $query_params['limit']; | |
| 327 | 327 |  		} else { | 
| 328 | -			$limit_parts = explode(',', $query_params[ 'limit' ] ); | |
| 329 | -			if( count( $limit_parts ) == 1 ){ | |
| 330 | - $limit_parts = array(0, $limit_parts[ 0 ] ); | |
| 328 | +			$limit_parts = explode(',', $query_params['limit']); | |
| 329 | +			if (count($limit_parts) == 1) { | |
| 330 | + $limit_parts = array(0, $limit_parts[0]); | |
| 331 | 331 | } | 
| 332 | 332 | } | 
| 333 | 333 | //remove the group by and having parts of the query, as those will | 
| 334 | 334 | //make the sql query return an array of values, instead of just a single value | 
| 335 | - unset( $query_params[ 'group_by' ], $query_params[ 'having' ], $query_params[ 'limit' ] ); | |
| 336 | - $count = $model->count( $query_params, null, true ); | |
| 335 | + unset($query_params['group_by'], $query_params['having'], $query_params['limit']); | |
| 336 | + $count = $model->count($query_params, null, true); | |
| 337 | 337 | |
| 338 | - $pages = $count / $limit_parts[ 1 ]; | |
| 339 | - $this->_set_response_header( 'Total', $count, false ); | |
| 340 | - $this->_set_response_header( 'PageSize', $limit_parts[ 1 ], false ); | |
| 341 | - $this->_set_response_header( 'TotalPages', ceil( $pages ), false ); | |
| 338 | + $pages = $count / $limit_parts[1]; | |
| 339 | +		$this->_set_response_header('Total', $count, false); | |
| 340 | +		$this->_set_response_header('PageSize', $limit_parts[1], false); | |
| 341 | +		$this->_set_response_header('TotalPages', ceil($pages), false); | |
| 342 | 342 | } | 
| 343 | 343 | |
| 344 | 344 | |
| @@ -351,47 +351,47 @@ discard block | ||
| 351 | 351 | * @param string $deprecated no longer used | 
| 352 | 352 | * @return array ready for being converted into json for sending to client | 
| 353 | 353 | */ | 
| 354 | -	public function create_entity_from_wpdb_result( $model, $db_row, $rest_request, $deprecated = null ) { | |
| 355 | -		if( ! $rest_request instanceof \WP_REST_Request ) { | |
| 354 | +	public function create_entity_from_wpdb_result($model, $db_row, $rest_request, $deprecated = null) { | |
| 355 | +		if ( ! $rest_request instanceof \WP_REST_Request) { | |
| 356 | 356 | //ok so this was called in the old style, where the 3rd arg was | 
| 357 | 357 | //$include, and the 4th arg was $context | 
| 358 | 358 | //now setup the request just to avoid fatal errors, although we won't be able | 
| 359 | 359 | //to truly make use of it because it's kinda devoid of info | 
| 360 | 360 | $rest_request = new \WP_REST_Request(); | 
| 361 | - $rest_request->set_param( 'include', $rest_request ); | |
| 362 | - $rest_request->set_param( 'caps', $deprecated ); | |
| 361 | +			$rest_request->set_param('include', $rest_request); | |
| 362 | +			$rest_request->set_param('caps', $deprecated); | |
| 363 | 363 | } | 
| 364 | -		if( $rest_request->get_param( 'caps' ) == null ) { | |
| 365 | - $rest_request->set_param( 'caps', \EEM_Base::caps_read ); | |
| 364 | +		if ($rest_request->get_param('caps') == null) { | |
| 365 | +			$rest_request->set_param('caps', \EEM_Base::caps_read); | |
| 366 | 366 | } | 
| 367 | - $entity_array = $this->_create_bare_entity_from_wpdb_results( $model, $db_row ); | |
| 368 | - $entity_array = $this->_add_extra_fields( $model, $db_row, $entity_array ); | |
| 369 | - $entity_array[ '_links' ] = $this->_get_entity_links( $model, $db_row, $entity_array ); | |
| 370 | - $entity_array[ '_calculated_fields'] = $this->_get_entity_calculations( $model, $db_row, $rest_request ); | |
| 371 | - $entity_array = $this->_include_requested_models( $model, $rest_request, $entity_array ); | |
| 367 | + $entity_array = $this->_create_bare_entity_from_wpdb_results($model, $db_row); | |
| 368 | + $entity_array = $this->_add_extra_fields($model, $db_row, $entity_array); | |
| 369 | + $entity_array['_links'] = $this->_get_entity_links($model, $db_row, $entity_array); | |
| 370 | + $entity_array['_calculated_fields'] = $this->_get_entity_calculations($model, $db_row, $rest_request); | |
| 371 | + $entity_array = $this->_include_requested_models($model, $rest_request, $entity_array); | |
| 372 | 372 | $entity_array = apply_filters( | 
| 373 | 373 | 'FHEE__Read__create_entity_from_wpdb_results__entity_before_inaccessible_field_removal', | 
| 374 | 374 | $entity_array, | 
| 375 | 375 | $model, | 
| 376 | - $rest_request->get_param( 'caps' ), | |
| 376 | +			$rest_request->get_param('caps'), | |
| 377 | 377 | $rest_request, | 
| 378 | 378 | $this | 
| 379 | 379 | ); | 
| 380 | 380 | $result_without_inaccessible_fields = Capabilities::filter_out_inaccessible_entity_fields( | 
| 381 | 381 | $entity_array, | 
| 382 | 382 | $model, | 
| 383 | - $rest_request->get_param( 'caps' ), | |
| 383 | +			$rest_request->get_param('caps'), | |
| 384 | 384 | $this->get_model_version_info() | 
| 385 | 385 | ); | 
| 386 | 386 | $this->_set_debug_info( | 
| 387 | 387 | 'inaccessible fields', | 
| 388 | - array_keys( array_diff_key( $entity_array, $result_without_inaccessible_fields ) ) | |
| 388 | + array_keys(array_diff_key($entity_array, $result_without_inaccessible_fields)) | |
| 389 | 389 | ); | 
| 390 | 390 | return apply_filters( | 
| 391 | 391 | 'FHEE__Read__create_entity_from_wpdb_results__entity_return', | 
| 392 | 392 | $result_without_inaccessible_fields, | 
| 393 | 393 | $model, | 
| 394 | - $rest_request->get_param( 'caps' ) | |
| 394 | +			$rest_request->get_param('caps') | |
| 395 | 395 | ); | 
| 396 | 396 | } | 
| 397 | 397 | |
| @@ -403,38 +403,38 @@ discard block | ||
| 403 | 403 | * @param array $db_row | 
| 404 | 404 | * @return array entity mostly ready for converting to JSON and sending in the response | 
| 405 | 405 | */ | 
| 406 | -	protected function _create_bare_entity_from_wpdb_results( \EEM_Base $model, $db_row ) { | |
| 407 | - $result = $model->deduce_fields_n_values_from_cols_n_values( $db_row ); | |
| 408 | - $result = array_intersect_key( $result, $this->get_model_version_info()->fields_on_model_in_this_version( $model ) ); | |
| 409 | -		foreach( $result as $field_name => $raw_field_value ) { | |
| 406 | +	protected function _create_bare_entity_from_wpdb_results(\EEM_Base $model, $db_row) { | |
| 407 | + $result = $model->deduce_fields_n_values_from_cols_n_values($db_row); | |
| 408 | + $result = array_intersect_key($result, $this->get_model_version_info()->fields_on_model_in_this_version($model)); | |
| 409 | +		foreach ($result as $field_name => $raw_field_value) { | |
| 410 | 410 | $field_obj = $model->field_settings_for($field_name); | 
| 411 | - $field_value = $field_obj->prepare_for_set_from_db( $raw_field_value ); | |
| 412 | -			if( $this->is_subclass_of_one(  $field_obj, $this->get_model_version_info()->fields_ignored() ) ){ | |
| 413 | - unset( $result[ $field_name ] ); | |
| 414 | - }elseif( | |
| 415 | - $this->is_subclass_of_one( $field_obj, $this->get_model_version_info()->fields_that_have_rendered_format() ) | |
| 416 | -			){ | |
| 417 | - $result[ $field_name ] = array( | |
| 418 | - 'raw' => $field_obj->prepare_for_get( $field_value ), | |
| 419 | - 'rendered' => $field_obj->prepare_for_pretty_echoing( $field_value ) | |
| 411 | + $field_value = $field_obj->prepare_for_set_from_db($raw_field_value); | |
| 412 | +			if ($this->is_subclass_of_one($field_obj, $this->get_model_version_info()->fields_ignored())) { | |
| 413 | + unset($result[$field_name]); | |
| 414 | + }elseif ( | |
| 415 | + $this->is_subclass_of_one($field_obj, $this->get_model_version_info()->fields_that_have_rendered_format()) | |
| 416 | +			) { | |
| 417 | + $result[$field_name] = array( | |
| 418 | + 'raw' => $field_obj->prepare_for_get($field_value), | |
| 419 | + 'rendered' => $field_obj->prepare_for_pretty_echoing($field_value) | |
| 420 | 420 | ); | 
| 421 | - }elseif( | |
| 422 | - $this->is_subclass_of_one( $field_obj, $this->get_model_version_info()->fields_that_have_pretty_format() ) | |
| 423 | -			){ | |
| 424 | - $result[ $field_name ] = array( | |
| 425 | - 'raw' => $field_obj->prepare_for_get( $field_value ), | |
| 426 | - 'pretty' => $field_obj->prepare_for_pretty_echoing( $field_value ) | |
| 421 | + }elseif ( | |
| 422 | + $this->is_subclass_of_one($field_obj, $this->get_model_version_info()->fields_that_have_pretty_format()) | |
| 423 | +			) { | |
| 424 | + $result[$field_name] = array( | |
| 425 | + 'raw' => $field_obj->prepare_for_get($field_value), | |
| 426 | + 'pretty' => $field_obj->prepare_for_pretty_echoing($field_value) | |
| 427 | 427 | ); | 
| 428 | -			} elseif ( $field_obj instanceof \EE_Datetime_Field ) { | |
| 429 | - $result[ $field_name ] = Model_Data_Translator::prepare_field_value_for_json( | |
| 428 | +			} elseif ($field_obj instanceof \EE_Datetime_Field) { | |
| 429 | + $result[$field_name] = Model_Data_Translator::prepare_field_value_for_json( | |
| 430 | 430 | $field_obj, | 
| 431 | 431 | $field_value, | 
| 432 | 432 | $this->get_model_version_info()->requested_version() | 
| 433 | 433 | ); | 
| 434 | 434 |  			} else { | 
| 435 | - $result[ $field_name ] = Model_Data_Translator::prepare_field_value_for_json( | |
| 435 | + $result[$field_name] = Model_Data_Translator::prepare_field_value_for_json( | |
| 436 | 436 | $field_obj, | 
| 437 | - $field_obj->prepare_for_get( $field_value ), | |
| 437 | + $field_obj->prepare_for_get($field_value), | |
| 438 | 438 | $this->get_model_version_info()->requested_version() | 
| 439 | 439 | ); | 
| 440 | 440 | } | 
| @@ -449,9 +449,9 @@ discard block | ||
| 449 | 449 | * @param array $entity_array | 
| 450 | 450 | * @return array modified entity | 
| 451 | 451 | */ | 
| 452 | -	protected function _add_extra_fields( \EEM_Base $model, $db_row, $entity_array ) { | |
| 453 | -		if( $model instanceof \EEM_CPT_Base ) { | |
| 454 | - $entity_array[ 'link' ] = get_permalink( $db_row[ $model->get_primary_key_field()->get_qualified_column() ] ); | |
| 452 | +	protected function _add_extra_fields(\EEM_Base $model, $db_row, $entity_array) { | |
| 453 | +		if ($model instanceof \EEM_CPT_Base) { | |
| 454 | + $entity_array['link'] = get_permalink($db_row[$model->get_primary_key_field()->get_qualified_column()]); | |
| 455 | 455 | } | 
| 456 | 456 | return $entity_array; | 
| 457 | 457 | } | 
| @@ -465,52 +465,52 @@ discard block | ||
| 465 | 465 | * @param array $entity_array | 
| 466 | 466 | * @return array the _links item in the entity | 
| 467 | 467 | */ | 
| 468 | -	protected function _get_entity_links( $model, $db_row, $entity_array ) { | |
| 468 | +	protected function _get_entity_links($model, $db_row, $entity_array) { | |
| 469 | 469 | //add basic links | 
| 470 | 470 | $links = array(); | 
| 471 | - if( $model instanceof \EEM_Base | |
| 472 | -			&& $model->has_primary_key_field() ) { | |
| 473 | - $links[ 'self' ] = array( | |
| 471 | + if ($model instanceof \EEM_Base | |
| 472 | +			&& $model->has_primary_key_field()) { | |
| 473 | + $links['self'] = array( | |
| 474 | 474 | array( | 
| 475 | 475 | 'href' => $this->get_versioned_link_to( | 
| 476 | - \EEH_Inflector::pluralize_and_lower( $model->get_this_model_name() ) . '/' . $entity_array[ $model->primary_key_name() ] | |
| 476 | + \EEH_Inflector::pluralize_and_lower($model->get_this_model_name()).'/'.$entity_array[$model->primary_key_name()] | |
| 477 | 477 | ) | 
| 478 | 478 | ) | 
| 479 | 479 | ); | 
| 480 | 480 | } | 
| 481 | - $links[ 'collection' ] = array( | |
| 481 | + $links['collection'] = array( | |
| 482 | 482 | array( | 
| 483 | 483 | 'href' => $this->get_versioned_link_to( | 
| 484 | - \EEH_Inflector::pluralize_and_lower( $model->get_this_model_name() ) | |
| 484 | + \EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) | |
| 485 | 485 | ) | 
| 486 | 486 | ) | 
| 487 | 487 | ); | 
| 488 | 488 | |
| 489 | 489 | //add link to the wp core endpoint, if wp api is active | 
| 490 | 490 | global $wp_rest_server; | 
| 491 | - if( $model instanceof \EEM_CPT_Base | |
| 491 | + if ($model instanceof \EEM_CPT_Base | |
| 492 | 492 | && $wp_rest_server instanceof \WP_REST_Server | 
| 493 | - && $wp_rest_server->get_route_options( '/wp/v2/posts' ) | |
| 494 | - && $model->has_primary_key_field() ) | |
| 493 | +			&& $wp_rest_server->get_route_options('/wp/v2/posts') | |
| 494 | + && $model->has_primary_key_field()) | |
| 495 | 495 |  		{ | 
| 496 | - $links[ \EED_Core_Rest_Api::ee_api_link_namespace . 'self_wp_post' ] = array( | |
| 496 | + $links[\EED_Core_Rest_Api::ee_api_link_namespace.'self_wp_post'] = array( | |
| 497 | 497 | array( | 
| 498 | - 'href' => rest_url( '/wp/v2/posts/' . $db_row[ $model->get_primary_key_field()->get_qualified_column() ] ), | |
| 498 | +					'href' => rest_url('/wp/v2/posts/'.$db_row[$model->get_primary_key_field()->get_qualified_column()]), | |
| 499 | 499 | 'single' => true | 
| 500 | 500 | ) | 
| 501 | 501 | ); | 
| 502 | 502 | } | 
| 503 | 503 | |
| 504 | 504 | //add links to related models | 
| 505 | -		foreach( $this->get_model_version_info()->relation_settings( $model ) as $relation_name => $relation_obj ) { | |
| 506 | - $related_model_part = Read::get_related_entity_name( $relation_name, $relation_obj ); | |
| 507 | - if( $model instanceof \EEM_Base | |
| 508 | - && $model->has_primary_key_field() ) | |
| 505 | +		foreach ($this->get_model_version_info()->relation_settings($model) as $relation_name => $relation_obj) { | |
| 506 | + $related_model_part = Read::get_related_entity_name($relation_name, $relation_obj); | |
| 507 | + if ($model instanceof \EEM_Base | |
| 508 | + && $model->has_primary_key_field()) | |
| 509 | 509 |  			{ | 
| 510 | - $links[ \EED_Core_Rest_Api::ee_api_link_namespace . $related_model_part ] = array( | |
| 510 | + $links[\EED_Core_Rest_Api::ee_api_link_namespace.$related_model_part] = array( | |
| 511 | 511 | array( | 
| 512 | 512 | 'href' => $this->get_versioned_link_to( | 
| 513 | - \EEH_Inflector::pluralize_and_lower( $model->get_this_model_name() ) . '/' . $entity_array[ $model->primary_key_name() ] . '/' . $related_model_part | |
| 513 | + \EEH_Inflector::pluralize_and_lower($model->get_this_model_name()).'/'.$entity_array[$model->primary_key_name()].'/'.$related_model_part | |
| 514 | 514 | ), | 
| 515 | 515 | 'single' => $relation_obj instanceof \EE_Belongs_To_Relation ? true : false | 
| 516 | 516 | ) | 
| @@ -527,51 +527,51 @@ discard block | ||
| 527 | 527 | * @param array $entity_array | 
| 528 | 528 | * @return array the modified entity | 
| 529 | 529 | */ | 
| 530 | -	protected function _include_requested_models( \EEM_Base $model, \WP_REST_Request $rest_request, $entity_array ) { | |
| 531 | - $includes_for_this_model = $this->explode_and_get_items_prefixed_with( $rest_request->get_param( 'include' ), '' ); | |
| 532 | - $includes_for_this_model = $this->_remove_model_names_from_array( $includes_for_this_model ); | |
| 530 | +	protected function _include_requested_models(\EEM_Base $model, \WP_REST_Request $rest_request, $entity_array) { | |
| 531 | +		$includes_for_this_model = $this->explode_and_get_items_prefixed_with($rest_request->get_param('include'), ''); | |
| 532 | + $includes_for_this_model = $this->_remove_model_names_from_array($includes_for_this_model); | |
| 533 | 533 | //if they passed in * or didn't specify any includes, return everything | 
| 534 | - if( ! in_array( '*', $includes_for_this_model ) | |
| 535 | -			&& ! empty( $includes_for_this_model ) ) { | |
| 536 | -			if( $model->has_primary_key_field() ) { | |
| 534 | +		if ( ! in_array('*', $includes_for_this_model) | |
| 535 | +			&& ! empty($includes_for_this_model)) { | |
| 536 | +			if ($model->has_primary_key_field()) { | |
| 537 | 537 | //always include the primary key. ya just gotta know that at least | 
| 538 | 538 | $includes_for_this_model[] = $model->primary_key_name(); | 
| 539 | 539 | } | 
| 540 | -			if( $this->explode_and_get_items_prefixed_with( $rest_request->get_param( 'calculate' ), '' ) ) { | |
| 540 | +			if ($this->explode_and_get_items_prefixed_with($rest_request->get_param('calculate'), '')) { | |
| 541 | 541 | $includes_for_this_model[] = '_calculated_fields'; | 
| 542 | 542 | } | 
| 543 | - $entity_array = array_intersect_key( $entity_array, array_flip( $includes_for_this_model ) ); | |
| 543 | + $entity_array = array_intersect_key($entity_array, array_flip($includes_for_this_model)); | |
| 544 | 544 | } | 
| 545 | - $relation_settings = $this->get_model_version_info()->relation_settings( $model ); | |
| 546 | -		foreach( $relation_settings as $relation_name => $relation_obj ) { | |
| 545 | + $relation_settings = $this->get_model_version_info()->relation_settings($model); | |
| 546 | +		foreach ($relation_settings as $relation_name => $relation_obj) { | |
| 547 | 547 | $related_fields_to_include = $this->explode_and_get_items_prefixed_with( | 
| 548 | - $rest_request->get_param( 'include' ), | |
| 548 | +				$rest_request->get_param('include'), | |
| 549 | 549 | $relation_name | 
| 550 | 550 | ); | 
| 551 | 551 | $related_fields_to_calculate = $this->explode_and_get_items_prefixed_with( | 
| 552 | - $rest_request->get_param( 'calculate' ), | |
| 552 | +				$rest_request->get_param('calculate'), | |
| 553 | 553 | $relation_name | 
| 554 | 554 | ); | 
| 555 | 555 | //did they specify they wanted to include a related model, or | 
| 556 | 556 | //specific fields from a related model? | 
| 557 | 557 | //or did they specify to calculate a field from a related model? | 
| 558 | -			if( $related_fields_to_include || $related_fields_to_calculate ) { | |
| 558 | +			if ($related_fields_to_include || $related_fields_to_calculate) { | |
| 559 | 559 | //if so, we should include at least some part of the related model | 
| 560 | 560 | $pretend_related_request = new \WP_REST_Request(); | 
| 561 | 561 | $pretend_related_request->set_query_params( | 
| 562 | 562 | array( | 
| 563 | - 'caps' => $rest_request->get_param( 'caps' ), | |
| 563 | +						'caps' => $rest_request->get_param('caps'), | |
| 564 | 564 | 'include' => $related_fields_to_include, | 
| 565 | 565 | 'calculate' => $related_fields_to_calculate, | 
| 566 | 566 | ) | 
| 567 | 567 | ); | 
| 568 | - $pretend_related_request->add_header( 'no_rest_headers', true ); | |
| 568 | +				$pretend_related_request->add_header('no_rest_headers', true); | |
| 569 | 569 | $related_results = $this->get_entities_from_relation( | 
| 570 | - $entity_array[ $model->primary_key_name() ], | |
| 570 | + $entity_array[$model->primary_key_name()], | |
| 571 | 571 | $relation_obj, | 
| 572 | 572 | $pretend_related_request | 
| 573 | 573 | ); | 
| 574 | - $entity_array[ Read::get_related_entity_name( $relation_name, $relation_obj ) ] = $related_results instanceof \WP_Error | |
| 574 | + $entity_array[Read::get_related_entity_name($relation_name, $relation_obj)] = $related_results instanceof \WP_Error | |
| 575 | 575 | ? null | 
| 576 | 576 | : $related_results; | 
| 577 | 577 | } | 
| @@ -585,8 +585,8 @@ discard block | ||
| 585 | 585 | * @param array $arr | 
| 586 | 586 | * @return array | 
| 587 | 587 | */ | 
| 588 | -	private function _remove_model_names_from_array( $arr ) { | |
| 589 | - return array_diff( $arr, array_keys( \EE_Registry::instance()->non_abstract_db_models ) ); | |
| 588 | +	private function _remove_model_names_from_array($arr) { | |
| 589 | + return array_diff($arr, array_keys(\EE_Registry::instance()->non_abstract_db_models)); | |
| 590 | 590 | } | 
| 591 | 591 | /** | 
| 592 | 592 | * Gets the calculated fields for the response | 
| @@ -596,15 +596,15 @@ discard block | ||
| 596 | 596 | * @param \WP_REST_Request $rest_request | 
| 597 | 597 | * @return array the _calculations item in the entity | 
| 598 | 598 | */ | 
| 599 | -	protected function _get_entity_calculations( $model, $wpdb_row, $rest_request ) { | |
| 599 | +	protected function _get_entity_calculations($model, $wpdb_row, $rest_request) { | |
| 600 | 600 | $calculated_fields = $this->explode_and_get_items_prefixed_with( | 
| 601 | - $rest_request->get_param( 'calculate' ), | |
| 601 | +			$rest_request->get_param('calculate'), | |
| 602 | 602 | '' | 
| 603 | 603 | ); | 
| 604 | 604 | //note: setting calculate=* doesn't do anything | 
| 605 | 605 | $calculated_fields_to_return = new \stdClass(); | 
| 606 | -		foreach( $calculated_fields as $field_to_calculate ) { | |
| 607 | -			try{ | |
| 606 | +		foreach ($calculated_fields as $field_to_calculate) { | |
| 607 | +			try { | |
| 608 | 608 | $calculated_fields_to_return->$field_to_calculate = Model_Data_Translator::prepare_field_value_for_json( | 
| 609 | 609 | null, | 
| 610 | 610 | $this->_fields_calculator->retrieve_calculated_field_value( | 
| @@ -616,10 +616,10 @@ discard block | ||
| 616 | 616 | ), | 
| 617 | 617 | $this->get_model_version_info()->requested_version() | 
| 618 | 618 | ); | 
| 619 | -			} catch( Rest_Exception $e ) { | |
| 619 | +			} catch (Rest_Exception $e) { | |
| 620 | 620 | //if we don't have permission to read it, just leave it out. but let devs know about the problem | 
| 621 | 621 | $this->_set_response_header( | 
| 622 | - 'Notices-Field-Calculation-Errors[' . $e->get_string_code() . '][' . $model->get_this_model_name() . '][' . $field_to_calculate . ']', | |
| 622 | + 'Notices-Field-Calculation-Errors['.$e->get_string_code().']['.$model->get_this_model_name().']['.$field_to_calculate.']', | |
| 623 | 623 | $e->getMessage(), | 
| 624 | 624 | true | 
| 625 | 625 | ); | 
| @@ -633,7 +633,7 @@ discard block | ||
| 633 | 633 | * @param string $link_part_after_version_and_slash eg "events/10/datetimes" | 
| 634 | 634 | * @return string url eg "http://mysite.com/wp-json/ee/v4.6/events/10/datetimes" | 
| 635 | 635 | */ | 
| 636 | -	public function get_versioned_link_to( $link_part_after_version_and_slash ) { | |
| 636 | +	public function get_versioned_link_to($link_part_after_version_and_slash) { | |
| 637 | 637 | return rest_url( | 
| 638 | 638 | \EED_Core_Rest_Api::ee_api_namespace | 
| 639 | 639 | . $this->get_model_version_info()->requested_version() | 
| @@ -649,11 +649,11 @@ discard block | ||
| 649 | 649 | * @param \EE_Model_Relation_Base $relation_obj | 
| 650 | 650 | * @return string | 
| 651 | 651 | */ | 
| 652 | -	public static function get_related_entity_name( $relation_name, $relation_obj ){ | |
| 653 | -		if( $relation_obj instanceof \EE_Belongs_To_Relation ) { | |
| 654 | - return strtolower( $relation_name ); | |
| 655 | -		}else{ | |
| 656 | - return \EEH_Inflector::pluralize_and_lower( $relation_name ); | |
| 652 | +	public static function get_related_entity_name($relation_name, $relation_obj) { | |
| 653 | +		if ($relation_obj instanceof \EE_Belongs_To_Relation) { | |
| 654 | + return strtolower($relation_name); | |
| 655 | +		} else { | |
| 656 | + return \EEH_Inflector::pluralize_and_lower($relation_name); | |
| 657 | 657 | } | 
| 658 | 658 | } | 
| 659 | 659 | |
| @@ -666,43 +666,43 @@ discard block | ||
| 666 | 666 | * @param \WP_REST_Request $request | 
| 667 | 667 | * @return array | 
| 668 | 668 | */ | 
| 669 | -	public function get_entity_from_model( $model, $request ) { | |
| 670 | - $query_params = array( array( $model->primary_key_name() => $request->get_param( 'id' ) ),'limit' => 1); | |
| 671 | -		if( $model instanceof \EEM_Soft_Delete_Base ){ | |
| 669 | +	public function get_entity_from_model($model, $request) { | |
| 670 | +		$query_params = array(array($model->primary_key_name() => $request->get_param('id')), 'limit' => 1); | |
| 671 | +		if ($model instanceof \EEM_Soft_Delete_Base) { | |
| 672 | 672 | $query_params = $model->alter_query_params_so_deleted_and_undeleted_items_included($query_params); | 
| 673 | 673 | } | 
| 674 | 674 | $restricted_query_params = $query_params; | 
| 675 | - $restricted_query_params[ 'caps' ] = $this->validate_context( $request->get_param( 'caps' ) ); | |
| 676 | - $this->_set_debug_info( 'model query params', $restricted_query_params ); | |
| 677 | - $model_rows = $model->get_all_wpdb_results( $restricted_query_params ); | |
| 678 | -		if ( ! empty ( $model_rows ) ) { | |
| 675 | +		$restricted_query_params['caps'] = $this->validate_context($request->get_param('caps')); | |
| 676 | +		$this->_set_debug_info('model query params', $restricted_query_params); | |
| 677 | + $model_rows = $model->get_all_wpdb_results($restricted_query_params); | |
| 678 | +		if ( ! empty ($model_rows)) { | |
| 679 | 679 | return $this->create_entity_from_wpdb_result( | 
| 680 | 680 | $model, | 
| 681 | - array_shift( $model_rows ), | |
| 681 | + array_shift($model_rows), | |
| 682 | 682 | $request ); | 
| 683 | 683 |  		} else { | 
| 684 | 684 | //ok let's test to see if we WOULD have found it, had we not had restrictions from missing capabilities | 
| 685 | - $lowercase_model_name = strtolower( $model->get_this_model_name() ); | |
| 686 | - $model_rows_found_sans_restrictions = $model->get_all_wpdb_results( $query_params ); | |
| 687 | -			if( ! empty( $model_rows_found_sans_restrictions ) ) { | |
| 685 | + $lowercase_model_name = strtolower($model->get_this_model_name()); | |
| 686 | + $model_rows_found_sans_restrictions = $model->get_all_wpdb_results($query_params); | |
| 687 | +			if ( ! empty($model_rows_found_sans_restrictions)) { | |
| 688 | 688 | //you got shafted- it existed but we didn't want to tell you! | 
| 689 | 689 | return new \WP_Error( | 
| 690 | 690 | 'rest_user_cannot_read', | 
| 691 | 691 | sprintf( | 
| 692 | - __( 'Sorry, you cannot read this %1$s. Missing permissions are: %2$s', 'event_espresso' ), | |
| 693 | - strtolower( $model->get_this_model_name() ), | |
| 692 | +						__('Sorry, you cannot read this %1$s. Missing permissions are: %2$s', 'event_espresso'), | |
| 693 | + strtolower($model->get_this_model_name()), | |
| 694 | 694 | Capabilities::get_missing_permissions_string( | 
| 695 | 695 | $model, | 
| 696 | - $this->validate_context( $request->get_param( 'caps' ) ) ) | |
| 696 | +							$this->validate_context($request->get_param('caps')) ) | |
| 697 | 697 | ), | 
| 698 | - array( 'status' => 403 ) | |
| 698 | +					array('status' => 403) | |
| 699 | 699 | ); | 
| 700 | 700 |  			} else { | 
| 701 | 701 | //it's not you. It just doesn't exist | 
| 702 | 702 | return new \WP_Error( | 
| 703 | - sprintf( 'rest_%s_invalid_id', $lowercase_model_name ), | |
| 704 | - sprintf( __( 'Invalid %s ID.', 'event_espresso' ), $lowercase_model_name ), | |
| 705 | - array( 'status' => 404 ) | |
| 703 | +					sprintf('rest_%s_invalid_id', $lowercase_model_name), | |
| 704 | +					sprintf(__('Invalid %s ID.', 'event_espresso'), $lowercase_model_name), | |
| 705 | +					array('status' => 404) | |
| 706 | 706 | ); | 
| 707 | 707 | } | 
| 708 | 708 | } | 
| @@ -715,14 +715,14 @@ discard block | ||
| 715 | 715 | * @param string $context | 
| 716 | 716 | * @return string array key of EEM_Base::cap_contexts_to_cap_action_map() | 
| 717 | 717 | */ | 
| 718 | -	public function validate_context( $context ) { | |
| 719 | -		if( ! $context ) { | |
| 718 | +	public function validate_context($context) { | |
| 719 | +		if ( ! $context) { | |
| 720 | 720 | $context = \EEM_Base::caps_read; | 
| 721 | 721 | } | 
| 722 | 722 | $valid_contexts = \EEM_Base::valid_cap_contexts(); | 
| 723 | -		if( in_array( $context, $valid_contexts )  ){ | |
| 723 | +		if (in_array($context, $valid_contexts)) { | |
| 724 | 724 | return $context; | 
| 725 | -		}else{ | |
| 725 | +		} else { | |
| 726 | 726 | return \EEM_Base::caps_read; | 
| 727 | 727 | } | 
| 728 | 728 | } | 
| @@ -741,77 +741,77 @@ discard block | ||
| 741 | 741 | * that absolutely no results should be returned | 
| 742 | 742 | * @throws \EE_Error | 
| 743 | 743 | */ | 
| 744 | -	public function create_model_query_params( $model, $query_parameters ) { | |
| 744 | +	public function create_model_query_params($model, $query_parameters) { | |
| 745 | 745 | $model_query_params = array( ); | 
| 746 | -		if ( isset( $query_parameters[ 'where' ] ) ) { | |
| 747 | - $model_query_params[ 0 ] = Model_Data_Translator::prepare_conditions_query_params_for_models( | |
| 748 | - $query_parameters[ 'where' ], | |
| 746 | +		if (isset($query_parameters['where'])) { | |
| 747 | + $model_query_params[0] = Model_Data_Translator::prepare_conditions_query_params_for_models( | |
| 748 | + $query_parameters['where'], | |
| 749 | 749 | $model, | 
| 750 | 750 | $this->get_model_version_info()->requested_version() | 
| 751 | 751 | ); | 
| 752 | 752 | } | 
| 753 | -		if ( isset( $query_parameters[ 'order_by' ] ) ) { | |
| 754 | - $order_by = $query_parameters[ 'order_by' ]; | |
| 755 | -		} elseif ( isset( $query_parameters[ 'orderby' ] ) ) { | |
| 756 | - $order_by = $query_parameters[ 'orderby' ]; | |
| 757 | -		}else{ | |
| 753 | +		if (isset($query_parameters['order_by'])) { | |
| 754 | + $order_by = $query_parameters['order_by']; | |
| 755 | +		} elseif (isset($query_parameters['orderby'])) { | |
| 756 | + $order_by = $query_parameters['orderby']; | |
| 757 | +		} else { | |
| 758 | 758 | $order_by = null; | 
| 759 | 759 | } | 
| 760 | -		if( $order_by !== null ){ | |
| 761 | - $model_query_params[ 'order_by' ] = $order_by; | |
| 760 | +		if ($order_by !== null) { | |
| 761 | + $model_query_params['order_by'] = $order_by; | |
| 762 | 762 | } | 
| 763 | -		if ( isset( $query_parameters[ 'group_by' ] ) ) { | |
| 764 | - $group_by = $query_parameters[ 'group_by' ]; | |
| 765 | -		} elseif ( isset( $query_parameters[ 'groupby' ] ) ) { | |
| 766 | - $group_by = $query_parameters[ 'groupby' ]; | |
| 767 | -		}else{ | |
| 768 | - $group_by = array_keys( $model->get_combined_primary_key_fields() ); | |
| 763 | +		if (isset($query_parameters['group_by'])) { | |
| 764 | + $group_by = $query_parameters['group_by']; | |
| 765 | +		} elseif (isset($query_parameters['groupby'])) { | |
| 766 | + $group_by = $query_parameters['groupby']; | |
| 767 | +		} else { | |
| 768 | + $group_by = array_keys($model->get_combined_primary_key_fields()); | |
| 769 | 769 | } | 
| 770 | -		if( $group_by !== null ){ | |
| 771 | - $model_query_params[ 'group_by' ] = $group_by; | |
| 770 | +		if ($group_by !== null) { | |
| 771 | + $model_query_params['group_by'] = $group_by; | |
| 772 | 772 | } | 
| 773 | -		if ( isset( $query_parameters[ 'having' ] ) ) { | |
| 774 | - $model_query_params[ 'having' ] = Model_Data_Translator::prepare_conditions_query_params_for_models( | |
| 775 | - $query_parameters[ 'having' ], | |
| 773 | +		if (isset($query_parameters['having'])) { | |
| 774 | + $model_query_params['having'] = Model_Data_Translator::prepare_conditions_query_params_for_models( | |
| 775 | + $query_parameters['having'], | |
| 776 | 776 | $model, | 
| 777 | 777 | $this->get_model_version_info()->requested_version() | 
| 778 | 778 | ); | 
| 779 | 779 | } | 
| 780 | -		if ( isset( $query_parameters[ 'order' ] ) ) { | |
| 781 | - $model_query_params[ 'order' ] = $query_parameters[ 'order' ]; | |
| 780 | +		if (isset($query_parameters['order'])) { | |
| 781 | + $model_query_params['order'] = $query_parameters['order']; | |
| 782 | 782 | } | 
| 783 | -		if ( isset( $query_parameters[ 'mine' ] ) ){ | |
| 784 | - $model_query_params = $model->alter_query_params_to_only_include_mine( $model_query_params ); | |
| 783 | +		if (isset($query_parameters['mine'])) { | |
| 784 | + $model_query_params = $model->alter_query_params_to_only_include_mine($model_query_params); | |
| 785 | 785 | } | 
| 786 | -		if( isset( $query_parameters[ 'limit' ] ) ) { | |
| 786 | +		if (isset($query_parameters['limit'])) { | |
| 787 | 787 | //limit should be either a string like '23' or '23,43', or an array with two items in it | 
| 788 | -			if( ! is_array( $query_parameters[ 'limit' ] ) ) { | |
| 789 | -				$limit_array = explode(',', (string)$query_parameters['limit']); | |
| 790 | -			}else { | |
| 791 | - $limit_array = $query_parameters[ 'limit' ]; | |
| 788 | +			if ( ! is_array($query_parameters['limit'])) { | |
| 789 | +				$limit_array = explode(',', (string) $query_parameters['limit']); | |
| 790 | +			} else { | |
| 791 | + $limit_array = $query_parameters['limit']; | |
| 792 | 792 | } | 
| 793 | 793 | $sanitized_limit = array(); | 
| 794 | -			foreach( $limit_array as $key => $limit_part ) { | |
| 795 | -				if( $this->_debug_mode && ( ! is_numeric( $limit_part ) || count( $sanitized_limit ) > 2 ) ) { | |
| 794 | +			foreach ($limit_array as $key => $limit_part) { | |
| 795 | +				if ($this->_debug_mode && ( ! is_numeric($limit_part) || count($sanitized_limit) > 2)) { | |
| 796 | 796 | throw new \EE_Error( | 
| 797 | 797 | sprintf( | 
| 798 | - __( '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' ), | |
| 799 | - json_encode( $query_parameters[ 'limit' ] ) | |
| 798 | +							__('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'), | |
| 799 | + json_encode($query_parameters['limit']) | |
| 800 | 800 | ) | 
| 801 | 801 | ); | 
| 802 | 802 | } | 
| 803 | - $sanitized_limit[] = (int)$limit_part; | |
| 803 | + $sanitized_limit[] = (int) $limit_part; | |
| 804 | 804 | } | 
| 805 | - $model_query_params[ 'limit' ] = implode( ',', $sanitized_limit ); | |
| 806 | -		}else{ | |
| 807 | - $model_query_params[ 'limit' ] = \EED_Core_Rest_Api::get_default_query_limit(); | |
| 805 | +			$model_query_params['limit'] = implode(',', $sanitized_limit); | |
| 806 | +		} else { | |
| 807 | + $model_query_params['limit'] = \EED_Core_Rest_Api::get_default_query_limit(); | |
| 808 | 808 | } | 
| 809 | -		if( isset( $query_parameters[ 'caps' ] ) ) { | |
| 810 | - $model_query_params[ 'caps' ] = $this->validate_context( $query_parameters[ 'caps' ] ); | |
| 811 | -		}else{ | |
| 812 | - $model_query_params[ 'caps' ] = \EEM_Base::caps_read; | |
| 809 | +		if (isset($query_parameters['caps'])) { | |
| 810 | + $model_query_params['caps'] = $this->validate_context($query_parameters['caps']); | |
| 811 | +		} else { | |
| 812 | + $model_query_params['caps'] = \EEM_Base::caps_read; | |
| 813 | 813 | } | 
| 814 | - return apply_filters( 'FHEE__Read__create_model_query_params', $model_query_params, $query_parameters, $model ); | |
| 814 | +		return apply_filters('FHEE__Read__create_model_query_params', $model_query_params, $query_parameters, $model); | |
| 815 | 815 | } | 
| 816 | 816 | |
| 817 | 817 | |
| @@ -823,13 +823,13 @@ discard block | ||
| 823 | 823 | * @param array $query_params sub-array from @see EEM_Base::get_all() | 
| 824 | 824 | * @return array | 
| 825 | 825 | */ | 
| 826 | -	public function prepare_rest_query_params_key_for_models( $model,  $query_params ) { | |
| 826 | +	public function prepare_rest_query_params_key_for_models($model, $query_params) { | |
| 827 | 827 | $model_ready_query_params = array(); | 
| 828 | -		foreach( $query_params as $key => $value ) { | |
| 829 | -			if( is_array( $value ) ) { | |
| 830 | - $model_ready_query_params[ $key ] = $this->prepare_rest_query_params_key_for_models( $model, $value ); | |
| 831 | -			}else{ | |
| 832 | - $model_ready_query_params[ $key ] = $value; | |
| 828 | +		foreach ($query_params as $key => $value) { | |
| 829 | +			if (is_array($value)) { | |
| 830 | + $model_ready_query_params[$key] = $this->prepare_rest_query_params_key_for_models($model, $value); | |
| 831 | +			} else { | |
| 832 | + $model_ready_query_params[$key] = $value; | |
| 833 | 833 | } | 
| 834 | 834 | } | 
| 835 | 835 | return $model_ready_query_params; | 
| @@ -843,13 +843,13 @@ discard block | ||
| 843 | 843 | * @param $query_params | 
| 844 | 844 | * @return array | 
| 845 | 845 | */ | 
| 846 | -	public function prepare_rest_query_params_values_for_models( $model, $query_params ) { | |
| 846 | +	public function prepare_rest_query_params_values_for_models($model, $query_params) { | |
| 847 | 847 | $model_ready_query_params = array(); | 
| 848 | -		foreach( $query_params as $key => $value ) { | |
| 849 | -			if( is_array( $value ) ) { | |
| 850 | - $model_ready_query_params[ $key ] = $this->prepare_rest_query_params_values_for_models( $model, $value ); | |
| 848 | +		foreach ($query_params as $key => $value) { | |
| 849 | +			if (is_array($value)) { | |
| 850 | + $model_ready_query_params[$key] = $this->prepare_rest_query_params_values_for_models($model, $value); | |
| 851 | 851 |  			} else { | 
| 852 | - $model_ready_query_params[ $key ] = $value; | |
| 852 | + $model_ready_query_params[$key] = $value; | |
| 853 | 853 | } | 
| 854 | 854 | } | 
| 855 | 855 | return $model_ready_query_params; | 
| @@ -864,33 +864,33 @@ discard block | ||
| 864 | 864 | * we only return strings starting with that and a period; if no prefix was specified | 
| 865 | 865 | * we return all items containing NO periods | 
| 866 | 866 | */ | 
| 867 | -	public function explode_and_get_items_prefixed_with( $string_to_explode, $prefix ) { | |
| 868 | -		if( is_string( $string_to_explode ) ) { | |
| 869 | - $exploded_contents = explode( ',', $string_to_explode ); | |
| 870 | -		} else if( is_array( $string_to_explode ) ) { | |
| 867 | +	public function explode_and_get_items_prefixed_with($string_to_explode, $prefix) { | |
| 868 | +		if (is_string($string_to_explode)) { | |
| 869 | +			$exploded_contents = explode(',', $string_to_explode); | |
| 870 | +		} else if (is_array($string_to_explode)) { | |
| 871 | 871 | $exploded_contents = $string_to_explode; | 
| 872 | 872 |  		} else { | 
| 873 | 873 | $exploded_contents = array(); | 
| 874 | 874 | } | 
| 875 | 875 | //if the string was empty, we want an empty array | 
| 876 | - $exploded_contents = array_filter( $exploded_contents ); | |
| 876 | + $exploded_contents = array_filter($exploded_contents); | |
| 877 | 877 | $contents_with_prefix = array(); | 
| 878 | -		foreach( $exploded_contents as $item ) { | |
| 879 | - $item = trim( $item ); | |
| 878 | +		foreach ($exploded_contents as $item) { | |
| 879 | + $item = trim($item); | |
| 880 | 880 | //if no prefix was provided, so we look for items with no "." in them | 
| 881 | -			if( ! $prefix ) { | |
| 881 | +			if ( ! $prefix) { | |
| 882 | 882 | //does this item have a period? | 
| 883 | -				if( strpos( $item, '.' ) === false ) { | |
| 883 | +				if (strpos($item, '.') === false) { | |
| 884 | 884 | //if not, then its what we're looking for | 
| 885 | 885 | $contents_with_prefix[] = $item; | 
| 886 | 886 | } | 
| 887 | -			} else if( strpos( $item, $prefix . '.' ) === 0 ) { | |
| 887 | +			} else if (strpos($item, $prefix.'.') === 0) { | |
| 888 | 888 | //this item has the prefix and a period, grab it | 
| 889 | 889 | $contents_with_prefix[] = substr( | 
| 890 | 890 | $item, | 
| 891 | - strpos( $item, $prefix . '.' ) + strlen( $prefix . '.' ) | |
| 891 | + strpos($item, $prefix.'.') + strlen($prefix.'.') | |
| 892 | 892 | ); | 
| 893 | -			} else if( $item === $prefix ) { | |
| 893 | +			} else if ($item === $prefix) { | |
| 894 | 894 | //this item is JUST the prefix | 
| 895 | 895 | //so let's grab everything after, which is a blank string | 
| 896 | 896 | $contents_with_prefix[] = ''; | 
| @@ -913,33 +913,33 @@ discard block | ||
| 913 | 913 | * the fields for that model, with the model's name removed from each. | 
| 914 | 914 | * If $include_string was blank or '*' returns an empty array | 
| 915 | 915 | */ | 
| 916 | -	public function extract_includes_for_this_model( $include_string, $model_name = null ) { | |
| 917 | -		if( is_array( $include_string ) ) { | |
| 918 | - $include_string = implode( ',', $include_string ); | |
| 916 | +	public function extract_includes_for_this_model($include_string, $model_name = null) { | |
| 917 | +		if (is_array($include_string)) { | |
| 918 | +			$include_string = implode(',', $include_string); | |
| 919 | 919 | } | 
| 920 | -		if( $include_string === '*' || $include_string === '' ) { | |
| 920 | +		if ($include_string === '*' || $include_string === '') { | |
| 921 | 921 | return array(); | 
| 922 | 922 | } | 
| 923 | - $includes = explode( ',', $include_string ); | |
| 923 | +		$includes = explode(',', $include_string); | |
| 924 | 924 | $extracted_fields_to_include = array(); | 
| 925 | -		if( $model_name ){ | |
| 926 | -			foreach( $includes as $field_to_include ) { | |
| 927 | - $field_to_include = trim( $field_to_include ); | |
| 928 | -				if( strpos( $field_to_include, $model_name . '.' ) === 0 ) { | |
| 925 | +		if ($model_name) { | |
| 926 | +			foreach ($includes as $field_to_include) { | |
| 927 | + $field_to_include = trim($field_to_include); | |
| 928 | +				if (strpos($field_to_include, $model_name.'.') === 0) { | |
| 929 | 929 | //found the model name at the exact start | 
| 930 | - $field_sans_model_name = str_replace( $model_name . '.', '', $field_to_include ); | |
| 930 | + $field_sans_model_name = str_replace($model_name.'.', '', $field_to_include); | |
| 931 | 931 | $extracted_fields_to_include[] = $field_sans_model_name; | 
| 932 | -				}elseif( $field_to_include == $model_name ){ | |
| 932 | +				}elseif ($field_to_include == $model_name) { | |
| 933 | 933 | $extracted_fields_to_include[] = '*'; | 
| 934 | 934 | } | 
| 935 | 935 | } | 
| 936 | -		}else{ | |
| 936 | +		} else { | |
| 937 | 937 | //look for ones with no period | 
| 938 | -			foreach( $includes as $field_to_include ) { | |
| 939 | - $field_to_include = trim( $field_to_include ); | |
| 938 | +			foreach ($includes as $field_to_include) { | |
| 939 | + $field_to_include = trim($field_to_include); | |
| 940 | 940 | if ( | 
| 941 | - strpos( $field_to_include, '.' ) === false | |
| 942 | - && ! $this->get_model_version_info()->is_model_name_in_this_version( $field_to_include ) | |
| 941 | + strpos($field_to_include, '.') === false | |
| 942 | + && ! $this->get_model_version_info()->is_model_name_in_this_version($field_to_include) | |
| 943 | 943 |  				) { | 
| 944 | 944 | $extracted_fields_to_include[] = $field_to_include; | 
| 945 | 945 | } |