@@ -25,230 +25,230 @@ |
||
| 25 | 25 | class JsonModelSchema |
| 26 | 26 | { |
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * @var EEM_Base |
|
| 30 | - */ |
|
| 31 | - protected $model; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * @var CalculatedModelFields |
|
| 35 | - */ |
|
| 36 | - protected $fields_calculator; |
|
| 37 | - |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * JsonModelSchema constructor. |
|
| 41 | - * |
|
| 42 | - * @param EEM_Base $model |
|
| 43 | - * @param CalculatedModelFields $fields_calculator |
|
| 44 | - */ |
|
| 45 | - public function __construct(EEM_Base $model, CalculatedModelFields $fields_calculator) |
|
| 46 | - { |
|
| 47 | - $this->model = $model; |
|
| 48 | - $this->fields_calculator = $fields_calculator; |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * Return the schema for a given model from a given model. |
|
| 54 | - * |
|
| 55 | - * @return array |
|
| 56 | - */ |
|
| 57 | - public function getModelSchema() |
|
| 58 | - { |
|
| 59 | - return $this->getModelSchemaForRelations( |
|
| 60 | - $this->model->relation_settings(), |
|
| 61 | - $this->getModelSchemaForFields( |
|
| 62 | - $this->model->field_settings(), |
|
| 63 | - $this->getInitialSchemaStructure() |
|
| 64 | - ) |
|
| 65 | - ); |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * Get the schema for a given set of model fields. |
|
| 71 | - * |
|
| 72 | - * @param EE_Model_Field_Base[] $model_fields |
|
| 73 | - * @param array $schema |
|
| 74 | - * @return array |
|
| 75 | - */ |
|
| 76 | - public function getModelSchemaForFields(array $model_fields, array $schema) |
|
| 77 | - { |
|
| 78 | - foreach ($model_fields as $field => $model_field) { |
|
| 79 | - if (! $model_field instanceof EE_Model_Field_Base) { |
|
| 80 | - continue; |
|
| 81 | - } |
|
| 82 | - $schema['properties'][ $field ] = $model_field->getSchema(); |
|
| 83 | - |
|
| 84 | - // if this is a primary key field add the primary key item |
|
| 85 | - if ($model_field instanceof EE_Primary_Key_Field_Base) { |
|
| 86 | - $schema['properties'][ $field ]['primary_key'] = true; |
|
| 87 | - if ($model_field instanceof EE_Primary_Key_Int_Field) { |
|
| 88 | - $schema['properties'][ $field ]['readonly'] = true; |
|
| 89 | - } |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - // if this is a foreign key field add the foreign key item |
|
| 93 | - if ($model_field instanceof EE_Foreign_Key_Field_Base) { |
|
| 94 | - $schema['properties'][ $field ]['foreign_key'] = array( |
|
| 95 | - 'description' => esc_html__( |
|
| 96 | - 'This is a foreign key the points to the given models.', |
|
| 97 | - 'event_espresso' |
|
| 98 | - ), |
|
| 99 | - 'type' => 'array', |
|
| 100 | - 'enum' => $model_field->get_model_class_names_pointed_to(), |
|
| 101 | - ); |
|
| 102 | - } |
|
| 103 | - } |
|
| 104 | - return $schema; |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * Get the schema for a given set of model relations |
|
| 110 | - * |
|
| 111 | - * @param EE_Model_Relation_Base[] $relations_on_model |
|
| 112 | - * @param array $schema |
|
| 113 | - * @return array |
|
| 114 | - */ |
|
| 115 | - public function getModelSchemaForRelations(array $relations_on_model, array $schema) |
|
| 116 | - { |
|
| 117 | - foreach ($relations_on_model as $model_name => $relation) { |
|
| 118 | - if (! $relation instanceof EE_Model_Relation_Base) { |
|
| 119 | - continue; |
|
| 120 | - } |
|
| 121 | - $model_name_for_schema = $relation instanceof EE_Belongs_To_Relation |
|
| 122 | - ? strtolower($model_name) |
|
| 123 | - : EEH_Inflector::pluralize_and_lower($model_name); |
|
| 124 | - $schema['properties'][ $model_name_for_schema ] = $relation->getSchema(); |
|
| 125 | - $schema['properties'][ $model_name_for_schema ]['relation_model'] = $model_name; |
|
| 126 | - |
|
| 127 | - // links schema |
|
| 128 | - $links_key = 'https://api.eventespresso.com/' . strtolower($model_name); |
|
| 129 | - $schema['properties']['_links']['properties'][ $links_key ] = array( |
|
| 130 | - 'description' => esc_html__( |
|
| 131 | - 'Array of objects describing the link(s) for this relation resource.', |
|
| 132 | - 'event_espresso' |
|
| 133 | - ), |
|
| 134 | - 'type' => 'array', |
|
| 135 | - 'readonly' => true, |
|
| 136 | - 'items' => array( |
|
| 137 | - 'type' => 'object', |
|
| 138 | - 'properties' => array( |
|
| 139 | - 'href' => array( |
|
| 140 | - 'type' => 'string', |
|
| 141 | - 'description' => sprintf( |
|
| 142 | - // translators: placeholder is the model name for the relation. |
|
| 143 | - esc_html__( |
|
| 144 | - 'The link to the resource for the %s relation(s) to this entity', |
|
| 145 | - 'event_espresso' |
|
| 146 | - ), |
|
| 147 | - $model_name |
|
| 148 | - ), |
|
| 149 | - ), |
|
| 150 | - 'single' => array( |
|
| 151 | - 'type' => 'boolean', |
|
| 152 | - 'description' => sprintf( |
|
| 153 | - // translators: placeholder is the model name for the relation. |
|
| 154 | - esc_html__( |
|
| 155 | - 'Whether or not there is only a single %s relation to this entity', |
|
| 156 | - 'event_espresso' |
|
| 157 | - ), |
|
| 158 | - $model_name |
|
| 159 | - ), |
|
| 160 | - ), |
|
| 161 | - ), |
|
| 162 | - 'additionalProperties' => false |
|
| 163 | - ), |
|
| 164 | - ); |
|
| 165 | - } |
|
| 166 | - return $schema; |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - |
|
| 170 | - /** |
|
| 171 | - * Outputs the schema header for a model. |
|
| 172 | - * |
|
| 173 | - * @return array |
|
| 174 | - */ |
|
| 175 | - public function getInitialSchemaStructure() |
|
| 176 | - { |
|
| 177 | - return array( |
|
| 178 | - '$schema' => 'http://json-schema.org/draft-04/schema#', |
|
| 179 | - 'title' => $this->model->get_this_model_name(), |
|
| 180 | - 'type' => 'object', |
|
| 181 | - 'properties' => array( |
|
| 182 | - 'link' => array( |
|
| 183 | - 'description' => esc_html__( |
|
| 184 | - 'Link to event on WordPress site hosting events.', |
|
| 185 | - 'event_espresso' |
|
| 186 | - ), |
|
| 187 | - 'type' => 'string', |
|
| 188 | - 'readonly' => true, |
|
| 189 | - ), |
|
| 190 | - '_links' => array( |
|
| 191 | - 'description' => esc_html__( |
|
| 192 | - 'Various links for resources related to the entity.', |
|
| 193 | - 'event_espresso' |
|
| 194 | - ), |
|
| 195 | - 'type' => 'object', |
|
| 196 | - 'readonly' => true, |
|
| 197 | - 'properties' => array( |
|
| 198 | - 'self' => array( |
|
| 199 | - 'description' => esc_html__( |
|
| 200 | - 'Link to this entities resource.', |
|
| 201 | - 'event_espresso' |
|
| 202 | - ), |
|
| 203 | - 'type' => 'array', |
|
| 204 | - 'items' => array( |
|
| 205 | - 'type' => 'object', |
|
| 206 | - 'properties' => array( |
|
| 207 | - 'href' => array( |
|
| 208 | - 'type' => 'string', |
|
| 209 | - ), |
|
| 210 | - ), |
|
| 211 | - 'additionalProperties' => false |
|
| 212 | - ), |
|
| 213 | - 'readonly' => true |
|
| 214 | - ), |
|
| 215 | - 'collection' => array( |
|
| 216 | - 'description' => esc_html__( |
|
| 217 | - 'Link to this entities collection resource.', |
|
| 218 | - 'event_espresso' |
|
| 219 | - ), |
|
| 220 | - 'type' => 'array', |
|
| 221 | - 'items' => array( |
|
| 222 | - 'type' => 'object', |
|
| 223 | - 'properties' => array( |
|
| 224 | - 'href' => array( |
|
| 225 | - 'type' => 'string' |
|
| 226 | - ), |
|
| 227 | - ), |
|
| 228 | - 'additionalProperties' => false |
|
| 229 | - ), |
|
| 230 | - 'readonly' => true |
|
| 231 | - ), |
|
| 232 | - ), |
|
| 233 | - 'additionalProperties' => false, |
|
| 234 | - ), |
|
| 235 | - '_calculated_fields' => $this->fields_calculator->getJsonSchemaForModel($this->model) |
|
| 236 | - ), |
|
| 237 | - 'additionalProperties' => false, |
|
| 238 | - ); |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - |
|
| 242 | - /** |
|
| 243 | - * Allows one to just use the object as a string to get the json. |
|
| 244 | - * eg. |
|
| 245 | - * $json_schema = new JsonModelSchema(EEM_Event::instance(), new CalculatedModelFields); |
|
| 246 | - * echo $json_schema; //outputs the schema as a json formatted string. |
|
| 247 | - * |
|
| 248 | - * @return bool|false|mixed|string |
|
| 249 | - */ |
|
| 250 | - public function __toString() |
|
| 251 | - { |
|
| 252 | - return wp_json_encode($this->getModelSchema()); |
|
| 253 | - } |
|
| 28 | + /** |
|
| 29 | + * @var EEM_Base |
|
| 30 | + */ |
|
| 31 | + protected $model; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * @var CalculatedModelFields |
|
| 35 | + */ |
|
| 36 | + protected $fields_calculator; |
|
| 37 | + |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * JsonModelSchema constructor. |
|
| 41 | + * |
|
| 42 | + * @param EEM_Base $model |
|
| 43 | + * @param CalculatedModelFields $fields_calculator |
|
| 44 | + */ |
|
| 45 | + public function __construct(EEM_Base $model, CalculatedModelFields $fields_calculator) |
|
| 46 | + { |
|
| 47 | + $this->model = $model; |
|
| 48 | + $this->fields_calculator = $fields_calculator; |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * Return the schema for a given model from a given model. |
|
| 54 | + * |
|
| 55 | + * @return array |
|
| 56 | + */ |
|
| 57 | + public function getModelSchema() |
|
| 58 | + { |
|
| 59 | + return $this->getModelSchemaForRelations( |
|
| 60 | + $this->model->relation_settings(), |
|
| 61 | + $this->getModelSchemaForFields( |
|
| 62 | + $this->model->field_settings(), |
|
| 63 | + $this->getInitialSchemaStructure() |
|
| 64 | + ) |
|
| 65 | + ); |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * Get the schema for a given set of model fields. |
|
| 71 | + * |
|
| 72 | + * @param EE_Model_Field_Base[] $model_fields |
|
| 73 | + * @param array $schema |
|
| 74 | + * @return array |
|
| 75 | + */ |
|
| 76 | + public function getModelSchemaForFields(array $model_fields, array $schema) |
|
| 77 | + { |
|
| 78 | + foreach ($model_fields as $field => $model_field) { |
|
| 79 | + if (! $model_field instanceof EE_Model_Field_Base) { |
|
| 80 | + continue; |
|
| 81 | + } |
|
| 82 | + $schema['properties'][ $field ] = $model_field->getSchema(); |
|
| 83 | + |
|
| 84 | + // if this is a primary key field add the primary key item |
|
| 85 | + if ($model_field instanceof EE_Primary_Key_Field_Base) { |
|
| 86 | + $schema['properties'][ $field ]['primary_key'] = true; |
|
| 87 | + if ($model_field instanceof EE_Primary_Key_Int_Field) { |
|
| 88 | + $schema['properties'][ $field ]['readonly'] = true; |
|
| 89 | + } |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + // if this is a foreign key field add the foreign key item |
|
| 93 | + if ($model_field instanceof EE_Foreign_Key_Field_Base) { |
|
| 94 | + $schema['properties'][ $field ]['foreign_key'] = array( |
|
| 95 | + 'description' => esc_html__( |
|
| 96 | + 'This is a foreign key the points to the given models.', |
|
| 97 | + 'event_espresso' |
|
| 98 | + ), |
|
| 99 | + 'type' => 'array', |
|
| 100 | + 'enum' => $model_field->get_model_class_names_pointed_to(), |
|
| 101 | + ); |
|
| 102 | + } |
|
| 103 | + } |
|
| 104 | + return $schema; |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * Get the schema for a given set of model relations |
|
| 110 | + * |
|
| 111 | + * @param EE_Model_Relation_Base[] $relations_on_model |
|
| 112 | + * @param array $schema |
|
| 113 | + * @return array |
|
| 114 | + */ |
|
| 115 | + public function getModelSchemaForRelations(array $relations_on_model, array $schema) |
|
| 116 | + { |
|
| 117 | + foreach ($relations_on_model as $model_name => $relation) { |
|
| 118 | + if (! $relation instanceof EE_Model_Relation_Base) { |
|
| 119 | + continue; |
|
| 120 | + } |
|
| 121 | + $model_name_for_schema = $relation instanceof EE_Belongs_To_Relation |
|
| 122 | + ? strtolower($model_name) |
|
| 123 | + : EEH_Inflector::pluralize_and_lower($model_name); |
|
| 124 | + $schema['properties'][ $model_name_for_schema ] = $relation->getSchema(); |
|
| 125 | + $schema['properties'][ $model_name_for_schema ]['relation_model'] = $model_name; |
|
| 126 | + |
|
| 127 | + // links schema |
|
| 128 | + $links_key = 'https://api.eventespresso.com/' . strtolower($model_name); |
|
| 129 | + $schema['properties']['_links']['properties'][ $links_key ] = array( |
|
| 130 | + 'description' => esc_html__( |
|
| 131 | + 'Array of objects describing the link(s) for this relation resource.', |
|
| 132 | + 'event_espresso' |
|
| 133 | + ), |
|
| 134 | + 'type' => 'array', |
|
| 135 | + 'readonly' => true, |
|
| 136 | + 'items' => array( |
|
| 137 | + 'type' => 'object', |
|
| 138 | + 'properties' => array( |
|
| 139 | + 'href' => array( |
|
| 140 | + 'type' => 'string', |
|
| 141 | + 'description' => sprintf( |
|
| 142 | + // translators: placeholder is the model name for the relation. |
|
| 143 | + esc_html__( |
|
| 144 | + 'The link to the resource for the %s relation(s) to this entity', |
|
| 145 | + 'event_espresso' |
|
| 146 | + ), |
|
| 147 | + $model_name |
|
| 148 | + ), |
|
| 149 | + ), |
|
| 150 | + 'single' => array( |
|
| 151 | + 'type' => 'boolean', |
|
| 152 | + 'description' => sprintf( |
|
| 153 | + // translators: placeholder is the model name for the relation. |
|
| 154 | + esc_html__( |
|
| 155 | + 'Whether or not there is only a single %s relation to this entity', |
|
| 156 | + 'event_espresso' |
|
| 157 | + ), |
|
| 158 | + $model_name |
|
| 159 | + ), |
|
| 160 | + ), |
|
| 161 | + ), |
|
| 162 | + 'additionalProperties' => false |
|
| 163 | + ), |
|
| 164 | + ); |
|
| 165 | + } |
|
| 166 | + return $schema; |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + |
|
| 170 | + /** |
|
| 171 | + * Outputs the schema header for a model. |
|
| 172 | + * |
|
| 173 | + * @return array |
|
| 174 | + */ |
|
| 175 | + public function getInitialSchemaStructure() |
|
| 176 | + { |
|
| 177 | + return array( |
|
| 178 | + '$schema' => 'http://json-schema.org/draft-04/schema#', |
|
| 179 | + 'title' => $this->model->get_this_model_name(), |
|
| 180 | + 'type' => 'object', |
|
| 181 | + 'properties' => array( |
|
| 182 | + 'link' => array( |
|
| 183 | + 'description' => esc_html__( |
|
| 184 | + 'Link to event on WordPress site hosting events.', |
|
| 185 | + 'event_espresso' |
|
| 186 | + ), |
|
| 187 | + 'type' => 'string', |
|
| 188 | + 'readonly' => true, |
|
| 189 | + ), |
|
| 190 | + '_links' => array( |
|
| 191 | + 'description' => esc_html__( |
|
| 192 | + 'Various links for resources related to the entity.', |
|
| 193 | + 'event_espresso' |
|
| 194 | + ), |
|
| 195 | + 'type' => 'object', |
|
| 196 | + 'readonly' => true, |
|
| 197 | + 'properties' => array( |
|
| 198 | + 'self' => array( |
|
| 199 | + 'description' => esc_html__( |
|
| 200 | + 'Link to this entities resource.', |
|
| 201 | + 'event_espresso' |
|
| 202 | + ), |
|
| 203 | + 'type' => 'array', |
|
| 204 | + 'items' => array( |
|
| 205 | + 'type' => 'object', |
|
| 206 | + 'properties' => array( |
|
| 207 | + 'href' => array( |
|
| 208 | + 'type' => 'string', |
|
| 209 | + ), |
|
| 210 | + ), |
|
| 211 | + 'additionalProperties' => false |
|
| 212 | + ), |
|
| 213 | + 'readonly' => true |
|
| 214 | + ), |
|
| 215 | + 'collection' => array( |
|
| 216 | + 'description' => esc_html__( |
|
| 217 | + 'Link to this entities collection resource.', |
|
| 218 | + 'event_espresso' |
|
| 219 | + ), |
|
| 220 | + 'type' => 'array', |
|
| 221 | + 'items' => array( |
|
| 222 | + 'type' => 'object', |
|
| 223 | + 'properties' => array( |
|
| 224 | + 'href' => array( |
|
| 225 | + 'type' => 'string' |
|
| 226 | + ), |
|
| 227 | + ), |
|
| 228 | + 'additionalProperties' => false |
|
| 229 | + ), |
|
| 230 | + 'readonly' => true |
|
| 231 | + ), |
|
| 232 | + ), |
|
| 233 | + 'additionalProperties' => false, |
|
| 234 | + ), |
|
| 235 | + '_calculated_fields' => $this->fields_calculator->getJsonSchemaForModel($this->model) |
|
| 236 | + ), |
|
| 237 | + 'additionalProperties' => false, |
|
| 238 | + ); |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + |
|
| 242 | + /** |
|
| 243 | + * Allows one to just use the object as a string to get the json. |
|
| 244 | + * eg. |
|
| 245 | + * $json_schema = new JsonModelSchema(EEM_Event::instance(), new CalculatedModelFields); |
|
| 246 | + * echo $json_schema; //outputs the schema as a json formatted string. |
|
| 247 | + * |
|
| 248 | + * @return bool|false|mixed|string |
|
| 249 | + */ |
|
| 250 | + public function __toString() |
|
| 251 | + { |
|
| 252 | + return wp_json_encode($this->getModelSchema()); |
|
| 253 | + } |
|
| 254 | 254 | } |
@@ -76,22 +76,22 @@ discard block |
||
| 76 | 76 | public function getModelSchemaForFields(array $model_fields, array $schema) |
| 77 | 77 | { |
| 78 | 78 | foreach ($model_fields as $field => $model_field) { |
| 79 | - if (! $model_field instanceof EE_Model_Field_Base) { |
|
| 79 | + if ( ! $model_field instanceof EE_Model_Field_Base) { |
|
| 80 | 80 | continue; |
| 81 | 81 | } |
| 82 | - $schema['properties'][ $field ] = $model_field->getSchema(); |
|
| 82 | + $schema['properties'][$field] = $model_field->getSchema(); |
|
| 83 | 83 | |
| 84 | 84 | // if this is a primary key field add the primary key item |
| 85 | 85 | if ($model_field instanceof EE_Primary_Key_Field_Base) { |
| 86 | - $schema['properties'][ $field ]['primary_key'] = true; |
|
| 86 | + $schema['properties'][$field]['primary_key'] = true; |
|
| 87 | 87 | if ($model_field instanceof EE_Primary_Key_Int_Field) { |
| 88 | - $schema['properties'][ $field ]['readonly'] = true; |
|
| 88 | + $schema['properties'][$field]['readonly'] = true; |
|
| 89 | 89 | } |
| 90 | 90 | } |
| 91 | 91 | |
| 92 | 92 | // if this is a foreign key field add the foreign key item |
| 93 | 93 | if ($model_field instanceof EE_Foreign_Key_Field_Base) { |
| 94 | - $schema['properties'][ $field ]['foreign_key'] = array( |
|
| 94 | + $schema['properties'][$field]['foreign_key'] = array( |
|
| 95 | 95 | 'description' => esc_html__( |
| 96 | 96 | 'This is a foreign key the points to the given models.', |
| 97 | 97 | 'event_espresso' |
@@ -115,18 +115,18 @@ discard block |
||
| 115 | 115 | public function getModelSchemaForRelations(array $relations_on_model, array $schema) |
| 116 | 116 | { |
| 117 | 117 | foreach ($relations_on_model as $model_name => $relation) { |
| 118 | - if (! $relation instanceof EE_Model_Relation_Base) { |
|
| 118 | + if ( ! $relation instanceof EE_Model_Relation_Base) { |
|
| 119 | 119 | continue; |
| 120 | 120 | } |
| 121 | 121 | $model_name_for_schema = $relation instanceof EE_Belongs_To_Relation |
| 122 | 122 | ? strtolower($model_name) |
| 123 | 123 | : EEH_Inflector::pluralize_and_lower($model_name); |
| 124 | - $schema['properties'][ $model_name_for_schema ] = $relation->getSchema(); |
|
| 125 | - $schema['properties'][ $model_name_for_schema ]['relation_model'] = $model_name; |
|
| 124 | + $schema['properties'][$model_name_for_schema] = $relation->getSchema(); |
|
| 125 | + $schema['properties'][$model_name_for_schema]['relation_model'] = $model_name; |
|
| 126 | 126 | |
| 127 | 127 | // links schema |
| 128 | - $links_key = 'https://api.eventespresso.com/' . strtolower($model_name); |
|
| 129 | - $schema['properties']['_links']['properties'][ $links_key ] = array( |
|
| 128 | + $links_key = 'https://api.eventespresso.com/'.strtolower($model_name); |
|
| 129 | + $schema['properties']['_links']['properties'][$links_key] = array( |
|
| 130 | 130 | 'description' => esc_html__( |
| 131 | 131 | 'Array of objects describing the link(s) for this relation resource.', |
| 132 | 132 | 'event_espresso' |
@@ -43,7 +43,7 @@ discard block |
||
| 43 | 43 | } else { |
| 44 | 44 | $reg = null; |
| 45 | 45 | } |
| 46 | - if (! $reg instanceof EE_Registration |
|
| 46 | + if ( ! $reg instanceof EE_Registration |
|
| 47 | 47 | ) { |
| 48 | 48 | throw new EE_Error( |
| 49 | 49 | sprintf( |
@@ -81,7 +81,7 @@ discard block |
||
| 81 | 81 | $status_pretty = 'NEVER'; |
| 82 | 82 | break; |
| 83 | 83 | } |
| 84 | - $checkin_stati[ $datetime_id ] = $status_pretty; |
|
| 84 | + $checkin_stati[$datetime_id] = $status_pretty; |
|
| 85 | 85 | } |
| 86 | 86 | return $checkin_stati; |
| 87 | 87 | } |
@@ -24,108 +24,108 @@ |
||
| 24 | 24 | */ |
| 25 | 25 | class Registration extends RegistrationCalculationBase |
| 26 | 26 | { |
| 27 | - /** |
|
| 28 | - * @var EEM_Registration |
|
| 29 | - */ |
|
| 30 | - protected $registration_model; |
|
| 27 | + /** |
|
| 28 | + * @var EEM_Registration |
|
| 29 | + */ |
|
| 30 | + protected $registration_model; |
|
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * Registration constructor. |
|
| 34 | - * @param EEM_Registration $registration_model |
|
| 35 | - */ |
|
| 36 | - public function __construct(EEM_Registration $registration_model) |
|
| 37 | - { |
|
| 38 | - $this->registration_model = $registration_model; |
|
| 39 | - } |
|
| 32 | + /** |
|
| 33 | + * Registration constructor. |
|
| 34 | + * @param EEM_Registration $registration_model |
|
| 35 | + */ |
|
| 36 | + public function __construct(EEM_Registration $registration_model) |
|
| 37 | + { |
|
| 38 | + $this->registration_model = $registration_model; |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * Calculates the checkin status for each datetime this registration has access to |
|
| 43 | - * |
|
| 44 | - * @param array $wpdb_row |
|
| 45 | - * @param WP_REST_Request $request |
|
| 46 | - * @param RegistrationControllerBase $controller |
|
| 47 | - * @return array |
|
| 48 | - * @throws EE_Error |
|
| 49 | - * @throws InvalidDataTypeException |
|
| 50 | - * @throws InvalidInterfaceException |
|
| 51 | - * @throws InvalidArgumentException |
|
| 52 | - */ |
|
| 53 | - public function datetimeCheckinStati($wpdb_row, $request, $controller) |
|
| 54 | - { |
|
| 55 | - if (is_array($wpdb_row) && isset($wpdb_row['Registration.REG_ID'])) { |
|
| 56 | - $reg = $this->registration_model->get_one_by_ID($wpdb_row['Registration.REG_ID']); |
|
| 57 | - } else { |
|
| 58 | - $reg = null; |
|
| 59 | - } |
|
| 60 | - if (! $reg instanceof EE_Registration |
|
| 61 | - ) { |
|
| 62 | - throw new EE_Error( |
|
| 63 | - sprintf( |
|
| 64 | - __( |
|
| 65 | - // @codingStandardsIgnoreStart |
|
| 66 | - 'Cannot calculate datetime_checkin_stati because the registration with ID %1$s (from database row %2$s) was not found', |
|
| 67 | - // @codingStandardsIgnoreEnd |
|
| 68 | - 'event_espresso' |
|
| 69 | - ), |
|
| 70 | - $wpdb_row['Registration.REG_ID'], |
|
| 71 | - print_r($wpdb_row, true) |
|
| 72 | - ) |
|
| 73 | - ); |
|
| 74 | - } |
|
| 75 | - $datetime_ids = EEM_Datetime::instance()->get_col( |
|
| 76 | - [ |
|
| 77 | - [ |
|
| 78 | - 'Ticket.TKT_ID' => $reg->ticket_ID(), |
|
| 79 | - ], |
|
| 80 | - 'default_where_conditions' => EEM_Base::default_where_conditions_minimum_all, |
|
| 81 | - ] |
|
| 82 | - ); |
|
| 83 | - $checkin_stati = array(); |
|
| 84 | - foreach ($datetime_ids as $datetime_id) { |
|
| 85 | - $status = $reg->check_in_status_for_datetime($datetime_id); |
|
| 86 | - switch ($status) { |
|
| 87 | - case EE_Checkin::status_checked_out: |
|
| 88 | - $status_pretty = 'OUT'; |
|
| 89 | - break; |
|
| 90 | - case EE_Checkin::status_checked_in: |
|
| 91 | - $status_pretty = 'IN'; |
|
| 92 | - break; |
|
| 93 | - case EE_Checkin::status_checked_never: |
|
| 94 | - default: |
|
| 95 | - $status_pretty = 'NEVER'; |
|
| 96 | - break; |
|
| 97 | - } |
|
| 98 | - $checkin_stati[ $datetime_id ] = $status_pretty; |
|
| 99 | - } |
|
| 100 | - return $checkin_stati; |
|
| 101 | - } |
|
| 41 | + /** |
|
| 42 | + * Calculates the checkin status for each datetime this registration has access to |
|
| 43 | + * |
|
| 44 | + * @param array $wpdb_row |
|
| 45 | + * @param WP_REST_Request $request |
|
| 46 | + * @param RegistrationControllerBase $controller |
|
| 47 | + * @return array |
|
| 48 | + * @throws EE_Error |
|
| 49 | + * @throws InvalidDataTypeException |
|
| 50 | + * @throws InvalidInterfaceException |
|
| 51 | + * @throws InvalidArgumentException |
|
| 52 | + */ |
|
| 53 | + public function datetimeCheckinStati($wpdb_row, $request, $controller) |
|
| 54 | + { |
|
| 55 | + if (is_array($wpdb_row) && isset($wpdb_row['Registration.REG_ID'])) { |
|
| 56 | + $reg = $this->registration_model->get_one_by_ID($wpdb_row['Registration.REG_ID']); |
|
| 57 | + } else { |
|
| 58 | + $reg = null; |
|
| 59 | + } |
|
| 60 | + if (! $reg instanceof EE_Registration |
|
| 61 | + ) { |
|
| 62 | + throw new EE_Error( |
|
| 63 | + sprintf( |
|
| 64 | + __( |
|
| 65 | + // @codingStandardsIgnoreStart |
|
| 66 | + 'Cannot calculate datetime_checkin_stati because the registration with ID %1$s (from database row %2$s) was not found', |
|
| 67 | + // @codingStandardsIgnoreEnd |
|
| 68 | + 'event_espresso' |
|
| 69 | + ), |
|
| 70 | + $wpdb_row['Registration.REG_ID'], |
|
| 71 | + print_r($wpdb_row, true) |
|
| 72 | + ) |
|
| 73 | + ); |
|
| 74 | + } |
|
| 75 | + $datetime_ids = EEM_Datetime::instance()->get_col( |
|
| 76 | + [ |
|
| 77 | + [ |
|
| 78 | + 'Ticket.TKT_ID' => $reg->ticket_ID(), |
|
| 79 | + ], |
|
| 80 | + 'default_where_conditions' => EEM_Base::default_where_conditions_minimum_all, |
|
| 81 | + ] |
|
| 82 | + ); |
|
| 83 | + $checkin_stati = array(); |
|
| 84 | + foreach ($datetime_ids as $datetime_id) { |
|
| 85 | + $status = $reg->check_in_status_for_datetime($datetime_id); |
|
| 86 | + switch ($status) { |
|
| 87 | + case EE_Checkin::status_checked_out: |
|
| 88 | + $status_pretty = 'OUT'; |
|
| 89 | + break; |
|
| 90 | + case EE_Checkin::status_checked_in: |
|
| 91 | + $status_pretty = 'IN'; |
|
| 92 | + break; |
|
| 93 | + case EE_Checkin::status_checked_never: |
|
| 94 | + default: |
|
| 95 | + $status_pretty = 'NEVER'; |
|
| 96 | + break; |
|
| 97 | + } |
|
| 98 | + $checkin_stati[ $datetime_id ] = $status_pretty; |
|
| 99 | + } |
|
| 100 | + return $checkin_stati; |
|
| 101 | + } |
|
| 102 | 102 | |
| 103 | 103 | |
| 104 | - /** |
|
| 105 | - * Provides an array for all the calculations possible that outlines a json schema for those calculations. |
|
| 106 | - * Array is indexed by calculation (snake case) and value is the schema for that calculation. |
|
| 107 | - * |
|
| 108 | - * @since $VID:$ |
|
| 109 | - * @return array |
|
| 110 | - */ |
|
| 111 | - public function schemaForCalculations() |
|
| 112 | - { |
|
| 113 | - return array( |
|
| 114 | - 'datetime_checkin_stati' => array( |
|
| 115 | - 'description' => esc_html__( |
|
| 116 | - 'Returns the checkin status for each datetime this registration has access to.', |
|
| 117 | - 'event_espresso' |
|
| 118 | - ), |
|
| 119 | - 'type' => 'object', |
|
| 120 | - 'properties' => array(), |
|
| 121 | - 'additionalProperties' => array( |
|
| 122 | - 'description' => esc_html( |
|
| 123 | - 'Keys are date-time ids and values are the check-in status', |
|
| 124 | - 'event_espresso' |
|
| 125 | - ), |
|
| 126 | - 'type' => 'string' |
|
| 127 | - ), |
|
| 128 | - ), |
|
| 129 | - ); |
|
| 130 | - } |
|
| 104 | + /** |
|
| 105 | + * Provides an array for all the calculations possible that outlines a json schema for those calculations. |
|
| 106 | + * Array is indexed by calculation (snake case) and value is the schema for that calculation. |
|
| 107 | + * |
|
| 108 | + * @since $VID:$ |
|
| 109 | + * @return array |
|
| 110 | + */ |
|
| 111 | + public function schemaForCalculations() |
|
| 112 | + { |
|
| 113 | + return array( |
|
| 114 | + 'datetime_checkin_stati' => array( |
|
| 115 | + 'description' => esc_html__( |
|
| 116 | + 'Returns the checkin status for each datetime this registration has access to.', |
|
| 117 | + 'event_espresso' |
|
| 118 | + ), |
|
| 119 | + 'type' => 'object', |
|
| 120 | + 'properties' => array(), |
|
| 121 | + 'additionalProperties' => array( |
|
| 122 | + 'description' => esc_html( |
|
| 123 | + 'Keys are date-time ids and values are the check-in status', |
|
| 124 | + 'event_espresso' |
|
| 125 | + ), |
|
| 126 | + 'type' => 'string' |
|
| 127 | + ), |
|
| 128 | + ), |
|
| 129 | + ); |
|
| 130 | + } |
|
| 131 | 131 | } |
@@ -5,9 +5,8 @@ |
||
| 5 | 5 | use EEM_Base; |
| 6 | 6 | use EventEspresso\core\exceptions\UnexpectedEntityException; |
| 7 | 7 | use EventEspresso\core\libraries\rest_api\calculations\CalculatedModelFieldsFactory; |
| 8 | -use EventEspresso\core\libraries\rest_api\controllers\Base; |
|
| 9 | -use EEH_Inflector; |
|
| 10 | 8 | use EventEspresso\core\libraries\rest_api\controllers\Base as BaseController; |
| 9 | +use EEH_Inflector; |
|
| 11 | 10 | |
| 12 | 11 | /** |
| 13 | 12 | * Class CalculatedModelFields |
@@ -57,7 +57,7 @@ discard block |
||
| 57 | 57 | */ |
| 58 | 58 | public function mapping($refresh = false) |
| 59 | 59 | { |
| 60 | - if (! $this->mapping || $refresh) { |
|
| 60 | + if ( ! $this->mapping || $refresh) { |
|
| 61 | 61 | $this->mapping = $this->generateNewMapping(); |
| 62 | 62 | } |
| 63 | 63 | return $this->mapping; |
@@ -84,7 +84,7 @@ discard block |
||
| 84 | 84 | foreach ($models_with_calculated_fields as $model_name) { |
| 85 | 85 | $calculator = $this->factory->createFromModel($model_name); |
| 86 | 86 | foreach (array_keys(call_user_func(array($calculator, 'schemaForCalculations'))) as $field_name) { |
| 87 | - $mapping[ $model_name ][ $field_name ] = get_class($calculator); |
|
| 87 | + $mapping[$model_name][$field_name] = get_class($calculator); |
|
| 88 | 88 | } |
| 89 | 89 | } |
| 90 | 90 | return apply_filters( |
@@ -113,8 +113,8 @@ discard block |
||
| 113 | 113 | foreach ($map_for_model as $calculation_index => $calculations_class) { |
| 114 | 114 | $calculator = $this->factory->createFromClassname($calculations_class); |
| 115 | 115 | $schema = call_user_func(array($calculator, 'schemaForCalculation'), $calculation_index); |
| 116 | - if (! empty($schema)) { |
|
| 117 | - $schema_map[ $map_model ][ $calculation_index ] = $schema; |
|
| 116 | + if ( ! empty($schema)) { |
|
| 117 | + $schema_map[$map_model][$calculation_index] = $schema; |
|
| 118 | 118 | } |
| 119 | 119 | } |
| 120 | 120 | } |
@@ -131,8 +131,8 @@ discard block |
||
| 131 | 131 | public function retrieveCalculatedFieldsForModel(EEM_Base $model) |
| 132 | 132 | { |
| 133 | 133 | $mapping = $this->mapping(); |
| 134 | - if (isset($mapping[ $model->get_this_model_name() ])) { |
|
| 135 | - return array_keys($mapping[ $model->get_this_model_name() ]); |
|
| 134 | + if (isset($mapping[$model->get_this_model_name()])) { |
|
| 135 | + return array_keys($mapping[$model->get_this_model_name()]); |
|
| 136 | 136 | } |
| 137 | 137 | return array(); |
| 138 | 138 | } |
@@ -145,7 +145,7 @@ discard block |
||
| 145 | 145 | */ |
| 146 | 146 | public function getJsonSchemaForModel(EEM_Base $model) |
| 147 | 147 | { |
| 148 | - if (! $this->mapping_schema) { |
|
| 148 | + if ( ! $this->mapping_schema) { |
|
| 149 | 149 | $this->mapping_schema = $this->generateNewMappingSchema(); |
| 150 | 150 | } |
| 151 | 151 | return array( |
@@ -154,8 +154,8 @@ discard block |
||
| 154 | 154 | 'event_espresso' |
| 155 | 155 | ), |
| 156 | 156 | 'type' => 'object', |
| 157 | - 'properties' => isset($this->mapping_schema[ $model->get_this_model_name() ]) |
|
| 158 | - ? $this->mapping_schema[ $model->get_this_model_name() ] |
|
| 157 | + 'properties' => isset($this->mapping_schema[$model->get_this_model_name()]) |
|
| 158 | + ? $this->mapping_schema[$model->get_this_model_name()] |
|
| 159 | 159 | : array(), |
| 160 | 160 | 'additionalProperties' => false, |
| 161 | 161 | 'readonly' => true, |
@@ -183,10 +183,10 @@ discard block |
||
| 183 | 183 | Base $controller |
| 184 | 184 | ) { |
| 185 | 185 | $mapping = $this->mapping(); |
| 186 | - if (isset($mapping[ $model->get_this_model_name() ]) |
|
| 187 | - && isset($mapping[ $model->get_this_model_name() ][ $field_name ]) |
|
| 186 | + if (isset($mapping[$model->get_this_model_name()]) |
|
| 187 | + && isset($mapping[$model->get_this_model_name()][$field_name]) |
|
| 188 | 188 | ) { |
| 189 | - $classname = $mapping[ $model->get_this_model_name() ][ $field_name ]; |
|
| 189 | + $classname = $mapping[$model->get_this_model_name()][$field_name]; |
|
| 190 | 190 | $calculator = $this->factory->createFromClassname($classname); |
| 191 | 191 | $class_method_name = EEH_Inflector::camelize_all_but_first($field_name); |
| 192 | 192 | return call_user_func(array($calculator, $class_method_name), $wpdb_row, $rest_request, $controller); |
@@ -22,177 +22,177 @@ |
||
| 22 | 22 | class CalculatedModelFields |
| 23 | 23 | { |
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * @var array |
|
| 27 | - */ |
|
| 28 | - protected $mapping; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * @var array |
|
| 32 | - */ |
|
| 33 | - protected $mapping_schema; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * @var CalculatedModelFieldsFactory |
|
| 37 | - */ |
|
| 38 | - private $factory; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * CalculatedModelFields constructor. |
|
| 42 | - * @param CalculatedModelFieldsFactory $factory |
|
| 43 | - */ |
|
| 44 | - public function __construct(CalculatedModelFieldsFactory $factory) |
|
| 45 | - { |
|
| 46 | - $this->factory = $factory; |
|
| 47 | - } |
|
| 48 | - /** |
|
| 49 | - * @param bool $refresh |
|
| 50 | - * @return array top-level-keys are model names (eg "Event") |
|
| 51 | - * next-level are the calculated field names AND method names on classes |
|
| 52 | - * which perform calculations, values are the fully qualified classnames which do the calculations |
|
| 53 | - * These callbacks should accept as arguments: |
|
| 54 | - * the wpdb row results, |
|
| 55 | - * the WP_Request object, |
|
| 56 | - * the controller object |
|
| 57 | - */ |
|
| 58 | - public function mapping($refresh = false) |
|
| 59 | - { |
|
| 60 | - if (! $this->mapping || $refresh) { |
|
| 61 | - $this->mapping = $this->generateNewMapping(); |
|
| 62 | - } |
|
| 63 | - return $this->mapping; |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * Generates a new mapping between model calculated fields and their callbacks |
|
| 69 | - * |
|
| 70 | - * @return array |
|
| 71 | - */ |
|
| 72 | - protected function generateNewMapping() |
|
| 73 | - { |
|
| 74 | - $mapping = array(); |
|
| 75 | - $models_with_calculated_fields = array( |
|
| 76 | - 'Attendee', |
|
| 77 | - 'Datetime', |
|
| 78 | - 'Event', |
|
| 79 | - 'Registration' |
|
| 80 | - ); |
|
| 81 | - foreach ($models_with_calculated_fields as $model_name) { |
|
| 82 | - $calculator = $this->factory->createFromModel($model_name); |
|
| 83 | - foreach (array_keys(call_user_func(array($calculator, 'schemaForCalculations'))) as $field_name) { |
|
| 84 | - $mapping[ $model_name ][ $field_name ] = get_class($calculator); |
|
| 85 | - } |
|
| 86 | - } |
|
| 87 | - return apply_filters( |
|
| 88 | - 'FHEE__EventEspresso\core\libraries\rest_api\Calculated_Model_Fields__mapping', |
|
| 89 | - $mapping |
|
| 90 | - ); |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * Generates the schema for each calculation index in the calculation map. |
|
| 96 | - * |
|
| 97 | - * @return array |
|
| 98 | - * @throws UnexpectedEntityException |
|
| 99 | - */ |
|
| 100 | - protected function generateNewMappingSchema() |
|
| 101 | - { |
|
| 102 | - $schema_map = array(); |
|
| 103 | - foreach ($this->mapping() as $map_model => $map_for_model) { |
|
| 104 | - /** |
|
| 105 | - * @var string $calculation_index |
|
| 106 | - * @var string $calculations_class |
|
| 107 | - */ |
|
| 108 | - foreach ($map_for_model as $calculation_index => $calculations_class) { |
|
| 109 | - $calculator = $this->factory->createFromClassname($calculations_class); |
|
| 110 | - $schema = call_user_func(array($calculator, 'schemaForCalculation'), $calculation_index); |
|
| 111 | - if (! empty($schema)) { |
|
| 112 | - $schema_map[ $map_model ][ $calculation_index ] = $schema; |
|
| 113 | - } |
|
| 114 | - } |
|
| 115 | - } |
|
| 116 | - return $schema_map; |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - |
|
| 120 | - /** |
|
| 121 | - * Gets the known calculated fields for model |
|
| 122 | - * |
|
| 123 | - * @param EEM_Base $model |
|
| 124 | - * @return array allowable values for this field |
|
| 125 | - */ |
|
| 126 | - public function retrieveCalculatedFieldsForModel(EEM_Base $model) |
|
| 127 | - { |
|
| 128 | - $mapping = $this->mapping(); |
|
| 129 | - if (isset($mapping[ $model->get_this_model_name() ])) { |
|
| 130 | - return array_keys($mapping[ $model->get_this_model_name() ]); |
|
| 131 | - } |
|
| 132 | - return array(); |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * Returns the JsonSchema for the calculated fields on the given model. |
|
| 138 | - * @param EEM_Base $model |
|
| 139 | - * @return array |
|
| 140 | - */ |
|
| 141 | - public function getJsonSchemaForModel(EEM_Base $model) |
|
| 142 | - { |
|
| 143 | - if (! $this->mapping_schema) { |
|
| 144 | - $this->mapping_schema = $this->generateNewMappingSchema(); |
|
| 145 | - } |
|
| 146 | - return array( |
|
| 147 | - 'description' => esc_html__( |
|
| 148 | - 'Available calculated fields for this model. Fields are only present in the response if explicitly requested', |
|
| 149 | - 'event_espresso' |
|
| 150 | - ), |
|
| 151 | - 'type' => 'object', |
|
| 152 | - 'properties' => isset($this->mapping_schema[ $model->get_this_model_name() ]) |
|
| 153 | - ? $this->mapping_schema[ $model->get_this_model_name() ] |
|
| 154 | - : array(), |
|
| 155 | - 'additionalProperties' => false, |
|
| 156 | - 'readonly' => true, |
|
| 157 | - ); |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - |
|
| 161 | - /** |
|
| 162 | - * Retrieves the value for this calculation |
|
| 163 | - * |
|
| 164 | - * @param EEM_Base $model |
|
| 165 | - * @param string $field_name |
|
| 166 | - * @param array $wpdb_row |
|
| 167 | - * @param $rest_request |
|
| 168 | - * @param BaseController $controller |
|
| 169 | - * @return mixed|null |
|
| 170 | - * @throws RestException |
|
| 171 | - * @throws UnexpectedEntityException |
|
| 172 | - */ |
|
| 173 | - public function retrieveCalculatedFieldValue( |
|
| 174 | - EEM_Base $model, |
|
| 175 | - $field_name, |
|
| 176 | - $wpdb_row, |
|
| 177 | - $rest_request, |
|
| 178 | - Base $controller |
|
| 179 | - ) { |
|
| 180 | - $mapping = $this->mapping(); |
|
| 181 | - if (isset($mapping[ $model->get_this_model_name() ]) |
|
| 182 | - && isset($mapping[ $model->get_this_model_name() ][ $field_name ]) |
|
| 183 | - ) { |
|
| 184 | - $classname = $mapping[ $model->get_this_model_name() ][ $field_name ]; |
|
| 185 | - $calculator = $this->factory->createFromClassname($classname); |
|
| 186 | - $class_method_name = EEH_Inflector::camelize_all_but_first($field_name); |
|
| 187 | - return call_user_func(array($calculator, $class_method_name), $wpdb_row, $rest_request, $controller); |
|
| 188 | - } |
|
| 189 | - throw new RestException( |
|
| 190 | - 'calculated_field_does_not_exist', |
|
| 191 | - sprintf( |
|
| 192 | - __('There is no calculated field %1$s on resource %2$s', 'event_espresso'), |
|
| 193 | - $field_name, |
|
| 194 | - $model->get_this_model_name() |
|
| 195 | - ) |
|
| 196 | - ); |
|
| 197 | - } |
|
| 25 | + /** |
|
| 26 | + * @var array |
|
| 27 | + */ |
|
| 28 | + protected $mapping; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * @var array |
|
| 32 | + */ |
|
| 33 | + protected $mapping_schema; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * @var CalculatedModelFieldsFactory |
|
| 37 | + */ |
|
| 38 | + private $factory; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * CalculatedModelFields constructor. |
|
| 42 | + * @param CalculatedModelFieldsFactory $factory |
|
| 43 | + */ |
|
| 44 | + public function __construct(CalculatedModelFieldsFactory $factory) |
|
| 45 | + { |
|
| 46 | + $this->factory = $factory; |
|
| 47 | + } |
|
| 48 | + /** |
|
| 49 | + * @param bool $refresh |
|
| 50 | + * @return array top-level-keys are model names (eg "Event") |
|
| 51 | + * next-level are the calculated field names AND method names on classes |
|
| 52 | + * which perform calculations, values are the fully qualified classnames which do the calculations |
|
| 53 | + * These callbacks should accept as arguments: |
|
| 54 | + * the wpdb row results, |
|
| 55 | + * the WP_Request object, |
|
| 56 | + * the controller object |
|
| 57 | + */ |
|
| 58 | + public function mapping($refresh = false) |
|
| 59 | + { |
|
| 60 | + if (! $this->mapping || $refresh) { |
|
| 61 | + $this->mapping = $this->generateNewMapping(); |
|
| 62 | + } |
|
| 63 | + return $this->mapping; |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * Generates a new mapping between model calculated fields and their callbacks |
|
| 69 | + * |
|
| 70 | + * @return array |
|
| 71 | + */ |
|
| 72 | + protected function generateNewMapping() |
|
| 73 | + { |
|
| 74 | + $mapping = array(); |
|
| 75 | + $models_with_calculated_fields = array( |
|
| 76 | + 'Attendee', |
|
| 77 | + 'Datetime', |
|
| 78 | + 'Event', |
|
| 79 | + 'Registration' |
|
| 80 | + ); |
|
| 81 | + foreach ($models_with_calculated_fields as $model_name) { |
|
| 82 | + $calculator = $this->factory->createFromModel($model_name); |
|
| 83 | + foreach (array_keys(call_user_func(array($calculator, 'schemaForCalculations'))) as $field_name) { |
|
| 84 | + $mapping[ $model_name ][ $field_name ] = get_class($calculator); |
|
| 85 | + } |
|
| 86 | + } |
|
| 87 | + return apply_filters( |
|
| 88 | + 'FHEE__EventEspresso\core\libraries\rest_api\Calculated_Model_Fields__mapping', |
|
| 89 | + $mapping |
|
| 90 | + ); |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * Generates the schema for each calculation index in the calculation map. |
|
| 96 | + * |
|
| 97 | + * @return array |
|
| 98 | + * @throws UnexpectedEntityException |
|
| 99 | + */ |
|
| 100 | + protected function generateNewMappingSchema() |
|
| 101 | + { |
|
| 102 | + $schema_map = array(); |
|
| 103 | + foreach ($this->mapping() as $map_model => $map_for_model) { |
|
| 104 | + /** |
|
| 105 | + * @var string $calculation_index |
|
| 106 | + * @var string $calculations_class |
|
| 107 | + */ |
|
| 108 | + foreach ($map_for_model as $calculation_index => $calculations_class) { |
|
| 109 | + $calculator = $this->factory->createFromClassname($calculations_class); |
|
| 110 | + $schema = call_user_func(array($calculator, 'schemaForCalculation'), $calculation_index); |
|
| 111 | + if (! empty($schema)) { |
|
| 112 | + $schema_map[ $map_model ][ $calculation_index ] = $schema; |
|
| 113 | + } |
|
| 114 | + } |
|
| 115 | + } |
|
| 116 | + return $schema_map; |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + |
|
| 120 | + /** |
|
| 121 | + * Gets the known calculated fields for model |
|
| 122 | + * |
|
| 123 | + * @param EEM_Base $model |
|
| 124 | + * @return array allowable values for this field |
|
| 125 | + */ |
|
| 126 | + public function retrieveCalculatedFieldsForModel(EEM_Base $model) |
|
| 127 | + { |
|
| 128 | + $mapping = $this->mapping(); |
|
| 129 | + if (isset($mapping[ $model->get_this_model_name() ])) { |
|
| 130 | + return array_keys($mapping[ $model->get_this_model_name() ]); |
|
| 131 | + } |
|
| 132 | + return array(); |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * Returns the JsonSchema for the calculated fields on the given model. |
|
| 138 | + * @param EEM_Base $model |
|
| 139 | + * @return array |
|
| 140 | + */ |
|
| 141 | + public function getJsonSchemaForModel(EEM_Base $model) |
|
| 142 | + { |
|
| 143 | + if (! $this->mapping_schema) { |
|
| 144 | + $this->mapping_schema = $this->generateNewMappingSchema(); |
|
| 145 | + } |
|
| 146 | + return array( |
|
| 147 | + 'description' => esc_html__( |
|
| 148 | + 'Available calculated fields for this model. Fields are only present in the response if explicitly requested', |
|
| 149 | + 'event_espresso' |
|
| 150 | + ), |
|
| 151 | + 'type' => 'object', |
|
| 152 | + 'properties' => isset($this->mapping_schema[ $model->get_this_model_name() ]) |
|
| 153 | + ? $this->mapping_schema[ $model->get_this_model_name() ] |
|
| 154 | + : array(), |
|
| 155 | + 'additionalProperties' => false, |
|
| 156 | + 'readonly' => true, |
|
| 157 | + ); |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + |
|
| 161 | + /** |
|
| 162 | + * Retrieves the value for this calculation |
|
| 163 | + * |
|
| 164 | + * @param EEM_Base $model |
|
| 165 | + * @param string $field_name |
|
| 166 | + * @param array $wpdb_row |
|
| 167 | + * @param $rest_request |
|
| 168 | + * @param BaseController $controller |
|
| 169 | + * @return mixed|null |
|
| 170 | + * @throws RestException |
|
| 171 | + * @throws UnexpectedEntityException |
|
| 172 | + */ |
|
| 173 | + public function retrieveCalculatedFieldValue( |
|
| 174 | + EEM_Base $model, |
|
| 175 | + $field_name, |
|
| 176 | + $wpdb_row, |
|
| 177 | + $rest_request, |
|
| 178 | + Base $controller |
|
| 179 | + ) { |
|
| 180 | + $mapping = $this->mapping(); |
|
| 181 | + if (isset($mapping[ $model->get_this_model_name() ]) |
|
| 182 | + && isset($mapping[ $model->get_this_model_name() ][ $field_name ]) |
|
| 183 | + ) { |
|
| 184 | + $classname = $mapping[ $model->get_this_model_name() ][ $field_name ]; |
|
| 185 | + $calculator = $this->factory->createFromClassname($classname); |
|
| 186 | + $class_method_name = EEH_Inflector::camelize_all_but_first($field_name); |
|
| 187 | + return call_user_func(array($calculator, $class_method_name), $wpdb_row, $rest_request, $controller); |
|
| 188 | + } |
|
| 189 | + throw new RestException( |
|
| 190 | + 'calculated_field_does_not_exist', |
|
| 191 | + sprintf( |
|
| 192 | + __('There is no calculated field %1$s on resource %2$s', 'event_espresso'), |
|
| 193 | + $field_name, |
|
| 194 | + $model->get_this_model_name() |
|
| 195 | + ) |
|
| 196 | + ); |
|
| 197 | + } |
|
| 198 | 198 | } |
@@ -23,1225 +23,1225 @@ |
||
| 23 | 23 | class EED_Core_Rest_Api extends \EED_Module |
| 24 | 24 | { |
| 25 | 25 | |
| 26 | - const ee_api_namespace = Domain::API_NAMESPACE; |
|
| 27 | - |
|
| 28 | - const ee_api_namespace_for_regex = 'ee\/v([^/]*)\/'; |
|
| 29 | - |
|
| 30 | - const saved_routes_option_names = 'ee_core_routes'; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * string used in _links response bodies to make them globally unique. |
|
| 34 | - * |
|
| 35 | - * @see http://v2.wp-api.org/extending/linking/ |
|
| 36 | - */ |
|
| 37 | - const ee_api_link_namespace = 'https://api.eventespresso.com/'; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * @var CalculatedModelFields |
|
| 41 | - */ |
|
| 42 | - protected static $_field_calculator; |
|
| 43 | - |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * @return EED_Core_Rest_Api|EED_Module |
|
| 47 | - */ |
|
| 48 | - public static function instance() |
|
| 49 | - { |
|
| 50 | - self::$_field_calculator = LoaderFactory::getLoader()->load('EventEspresso\core\libraries\rest_api\CalculatedModelFields'); |
|
| 51 | - return parent::get_instance(__CLASS__); |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * set_hooks - for hooking into EE Core, other modules, etc |
|
| 57 | - * |
|
| 58 | - * @access public |
|
| 59 | - * @return void |
|
| 60 | - */ |
|
| 61 | - public static function set_hooks() |
|
| 62 | - { |
|
| 63 | - self::set_hooks_both(); |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * set_hooks_admin - for hooking into EE Admin Core, other modules, etc |
|
| 69 | - * |
|
| 70 | - * @access public |
|
| 71 | - * @return void |
|
| 72 | - */ |
|
| 73 | - public static function set_hooks_admin() |
|
| 74 | - { |
|
| 75 | - self::set_hooks_both(); |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - |
|
| 79 | - public static function set_hooks_both() |
|
| 80 | - { |
|
| 81 | - add_action('rest_api_init', array('EED_Core_Rest_Api', 'register_routes'), 10); |
|
| 82 | - add_action('rest_api_init', array('EED_Core_Rest_Api', 'set_hooks_rest_api'), 5); |
|
| 83 | - add_filter('rest_route_data', array('EED_Core_Rest_Api', 'hide_old_endpoints'), 10, 2); |
|
| 84 | - add_filter( |
|
| 85 | - 'rest_index', |
|
| 86 | - array('EventEspresso\core\libraries\rest_api\controllers\model\Meta', 'filterEeMetadataIntoIndex') |
|
| 87 | - ); |
|
| 88 | - EED_Core_Rest_Api::invalidate_cached_route_data_on_version_change(); |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * sets up hooks which only need to be included as part of REST API requests; |
|
| 94 | - * other requests like to the frontend or admin etc don't need them |
|
| 95 | - * |
|
| 96 | - * @throws \EE_Error |
|
| 97 | - */ |
|
| 98 | - public static function set_hooks_rest_api() |
|
| 99 | - { |
|
| 100 | - // set hooks which account for changes made to the API |
|
| 101 | - EED_Core_Rest_Api::_set_hooks_for_changes(); |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * public wrapper of _set_hooks_for_changes. |
|
| 107 | - * Loads all the hooks which make requests to old versions of the API |
|
| 108 | - * appear the same as they always did |
|
| 109 | - * |
|
| 110 | - * @throws EE_Error |
|
| 111 | - */ |
|
| 112 | - public static function set_hooks_for_changes() |
|
| 113 | - { |
|
| 114 | - self::_set_hooks_for_changes(); |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * Loads all the hooks which make requests to old versions of the API |
|
| 120 | - * appear the same as they always did |
|
| 121 | - * |
|
| 122 | - * @throws EE_Error |
|
| 123 | - */ |
|
| 124 | - protected static function _set_hooks_for_changes() |
|
| 125 | - { |
|
| 126 | - $folder_contents = EEH_File::get_contents_of_folders(array(EE_LIBRARIES . 'rest_api' . DS . 'changes'), false); |
|
| 127 | - foreach ($folder_contents as $classname_in_namespace => $filepath) { |
|
| 128 | - // ignore the base parent class |
|
| 129 | - // and legacy named classes |
|
| 130 | - if ($classname_in_namespace === 'ChangesInBase' |
|
| 131 | - || strpos($classname_in_namespace, 'Changes_In_') === 0 |
|
| 132 | - ) { |
|
| 133 | - continue; |
|
| 134 | - } |
|
| 135 | - $full_classname = 'EventEspresso\core\libraries\rest_api\changes\\' . $classname_in_namespace; |
|
| 136 | - if (class_exists($full_classname)) { |
|
| 137 | - $instance_of_class = new $full_classname; |
|
| 138 | - if ($instance_of_class instanceof ChangesInBase) { |
|
| 139 | - $instance_of_class->setHooks(); |
|
| 140 | - } |
|
| 141 | - } |
|
| 142 | - } |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - |
|
| 146 | - /** |
|
| 147 | - * Filters the WP routes to add our EE-related ones. This takes a bit of time |
|
| 148 | - * so we actually prefer to only do it when an EE plugin is activated or upgraded |
|
| 149 | - * |
|
| 150 | - * @throws \EE_Error |
|
| 151 | - */ |
|
| 152 | - public static function register_routes() |
|
| 153 | - { |
|
| 154 | - foreach (EED_Core_Rest_Api::get_ee_route_data() as $namespace => $relative_routes) { |
|
| 155 | - foreach ($relative_routes as $relative_route => $data_for_multiple_endpoints) { |
|
| 156 | - /** |
|
| 157 | - * @var array $data_for_multiple_endpoints numerically indexed array |
|
| 158 | - * but can also contain route options like { |
|
| 159 | - * @type array $schema { |
|
| 160 | - * @type callable $schema_callback |
|
| 161 | - * @type array $callback_args arguments that will be passed to the callback, after the |
|
| 162 | - * WP_REST_Request of course |
|
| 163 | - * } |
|
| 164 | - * } |
|
| 165 | - */ |
|
| 166 | - // when registering routes, register all the endpoints' data at the same time |
|
| 167 | - $multiple_endpoint_args = array(); |
|
| 168 | - foreach ($data_for_multiple_endpoints as $endpoint_key => $data_for_single_endpoint) { |
|
| 169 | - /** |
|
| 170 | - * @var array $data_for_single_endpoint { |
|
| 171 | - * @type callable $callback |
|
| 172 | - * @type string methods |
|
| 173 | - * @type array args |
|
| 174 | - * @type array _links |
|
| 175 | - * @type array $callback_args arguments that will be passed to the callback, after the |
|
| 176 | - * WP_REST_Request of course |
|
| 177 | - * } |
|
| 178 | - */ |
|
| 179 | - // skip route options |
|
| 180 | - if (! is_numeric($endpoint_key)) { |
|
| 181 | - continue; |
|
| 182 | - } |
|
| 183 | - if (! isset($data_for_single_endpoint['callback'], $data_for_single_endpoint['methods'])) { |
|
| 184 | - throw new EE_Error( |
|
| 185 | - esc_html__( |
|
| 186 | - // @codingStandardsIgnoreStart |
|
| 187 | - 'Endpoint configuration data needs to have entries "callback" (callable) and "methods" (comma-separated list of accepts HTTP methods).', |
|
| 188 | - // @codingStandardsIgnoreEnd |
|
| 189 | - 'event_espresso' |
|
| 190 | - ) |
|
| 191 | - ); |
|
| 192 | - } |
|
| 193 | - $callback = $data_for_single_endpoint['callback']; |
|
| 194 | - $single_endpoint_args = array( |
|
| 195 | - 'methods' => $data_for_single_endpoint['methods'], |
|
| 196 | - 'args' => isset($data_for_single_endpoint['args']) ? $data_for_single_endpoint['args'] |
|
| 197 | - : array(), |
|
| 198 | - ); |
|
| 199 | - if (isset($data_for_single_endpoint['_links'])) { |
|
| 200 | - $single_endpoint_args['_links'] = $data_for_single_endpoint['_links']; |
|
| 201 | - } |
|
| 202 | - if (isset($data_for_single_endpoint['callback_args'])) { |
|
| 203 | - $callback_args = $data_for_single_endpoint['callback_args']; |
|
| 204 | - $single_endpoint_args['callback'] = function (\WP_REST_Request $request) use ( |
|
| 205 | - $callback, |
|
| 206 | - $callback_args |
|
| 207 | - ) { |
|
| 208 | - array_unshift($callback_args, $request); |
|
| 209 | - return call_user_func_array( |
|
| 210 | - $callback, |
|
| 211 | - $callback_args |
|
| 212 | - ); |
|
| 213 | - }; |
|
| 214 | - } else { |
|
| 215 | - $single_endpoint_args['callback'] = $data_for_single_endpoint['callback']; |
|
| 216 | - } |
|
| 217 | - $multiple_endpoint_args[] = $single_endpoint_args; |
|
| 218 | - } |
|
| 219 | - if (isset($data_for_multiple_endpoints['schema'])) { |
|
| 220 | - $schema_route_data = $data_for_multiple_endpoints['schema']; |
|
| 221 | - $schema_callback = $schema_route_data['schema_callback']; |
|
| 222 | - $callback_args = $schema_route_data['callback_args']; |
|
| 223 | - $multiple_endpoint_args['schema'] = function () use ($schema_callback, $callback_args) { |
|
| 224 | - return call_user_func_array( |
|
| 225 | - $schema_callback, |
|
| 226 | - $callback_args |
|
| 227 | - ); |
|
| 228 | - }; |
|
| 229 | - } |
|
| 230 | - register_rest_route( |
|
| 231 | - $namespace, |
|
| 232 | - $relative_route, |
|
| 233 | - $multiple_endpoint_args |
|
| 234 | - ); |
|
| 235 | - } |
|
| 236 | - } |
|
| 237 | - } |
|
| 238 | - |
|
| 239 | - |
|
| 240 | - /** |
|
| 241 | - * Checks if there was a version change or something that merits invalidating the cached |
|
| 242 | - * route data. If so, invalidates the cached route data so that it gets refreshed |
|
| 243 | - * next time the WP API is used |
|
| 244 | - */ |
|
| 245 | - public static function invalidate_cached_route_data_on_version_change() |
|
| 246 | - { |
|
| 247 | - if (EE_System::instance()->detect_req_type() !== EE_System::req_type_normal) { |
|
| 248 | - EED_Core_Rest_Api::invalidate_cached_route_data(); |
|
| 249 | - } |
|
| 250 | - foreach (EE_Registry::instance()->addons as $addon) { |
|
| 251 | - if ($addon instanceof EE_Addon && $addon->detect_req_type() !== EE_System::req_type_normal) { |
|
| 252 | - EED_Core_Rest_Api::invalidate_cached_route_data(); |
|
| 253 | - } |
|
| 254 | - } |
|
| 255 | - } |
|
| 256 | - |
|
| 257 | - |
|
| 258 | - /** |
|
| 259 | - * Removes the cached route data so it will get refreshed next time the WP API is used |
|
| 260 | - */ |
|
| 261 | - public static function invalidate_cached_route_data() |
|
| 262 | - { |
|
| 263 | - // delete the saved EE REST API routes |
|
| 264 | - foreach (EED_Core_Rest_Api::versions_served() as $version => $hidden) { |
|
| 265 | - delete_option(EED_Core_Rest_Api::saved_routes_option_names . $version); |
|
| 266 | - } |
|
| 267 | - } |
|
| 268 | - |
|
| 269 | - |
|
| 270 | - /** |
|
| 271 | - * Gets the EE route data |
|
| 272 | - * |
|
| 273 | - * @return array top-level key is the namespace, next-level key is the route and its value is array{ |
|
| 274 | - * @throws \EE_Error |
|
| 275 | - * @type string|array $callback |
|
| 276 | - * @type string $methods |
|
| 277 | - * @type boolean $hidden_endpoint |
|
| 278 | - * } |
|
| 279 | - */ |
|
| 280 | - public static function get_ee_route_data() |
|
| 281 | - { |
|
| 282 | - $ee_routes = array(); |
|
| 283 | - foreach (self::versions_served() as $version => $hidden_endpoints) { |
|
| 284 | - $ee_routes[ self::ee_api_namespace . $version ] = self::_get_ee_route_data_for_version( |
|
| 285 | - $version, |
|
| 286 | - $hidden_endpoints |
|
| 287 | - ); |
|
| 288 | - } |
|
| 289 | - return $ee_routes; |
|
| 290 | - } |
|
| 291 | - |
|
| 292 | - |
|
| 293 | - /** |
|
| 294 | - * Gets the EE route data from the wp options if it exists already, |
|
| 295 | - * otherwise re-generates it and saves it to the option |
|
| 296 | - * |
|
| 297 | - * @param string $version |
|
| 298 | - * @param boolean $hidden_endpoints |
|
| 299 | - * @return array |
|
| 300 | - * @throws \EE_Error |
|
| 301 | - */ |
|
| 302 | - protected static function _get_ee_route_data_for_version($version, $hidden_endpoints = false) |
|
| 303 | - { |
|
| 304 | - $ee_routes = get_option(self::saved_routes_option_names . $version, null); |
|
| 305 | - if (! $ee_routes || (defined('EE_REST_API_DEBUG_MODE') && EE_REST_API_DEBUG_MODE)) { |
|
| 306 | - $ee_routes = self::_save_ee_route_data_for_version($version, $hidden_endpoints); |
|
| 307 | - } |
|
| 308 | - return $ee_routes; |
|
| 309 | - } |
|
| 310 | - |
|
| 311 | - |
|
| 312 | - /** |
|
| 313 | - * Saves the EE REST API route data to a wp option and returns it |
|
| 314 | - * |
|
| 315 | - * @param string $version |
|
| 316 | - * @param boolean $hidden_endpoints |
|
| 317 | - * @return mixed|null |
|
| 318 | - * @throws \EE_Error |
|
| 319 | - */ |
|
| 320 | - protected static function _save_ee_route_data_for_version($version, $hidden_endpoints = false) |
|
| 321 | - { |
|
| 322 | - $instance = self::instance(); |
|
| 323 | - $routes = apply_filters( |
|
| 324 | - 'EED_Core_Rest_Api__save_ee_route_data_for_version__routes', |
|
| 325 | - array_replace_recursive( |
|
| 326 | - $instance->_get_config_route_data_for_version($version, $hidden_endpoints), |
|
| 327 | - $instance->_get_meta_route_data_for_version($version, $hidden_endpoints), |
|
| 328 | - $instance->_get_model_route_data_for_version($version, $hidden_endpoints), |
|
| 329 | - $instance->_get_rpc_route_data_for_version($version, $hidden_endpoints) |
|
| 330 | - ) |
|
| 331 | - ); |
|
| 332 | - $option_name = self::saved_routes_option_names . $version; |
|
| 333 | - if (get_option($option_name)) { |
|
| 334 | - update_option($option_name, $routes, true); |
|
| 335 | - } else { |
|
| 336 | - add_option($option_name, $routes, null, 'no'); |
|
| 337 | - } |
|
| 338 | - return $routes; |
|
| 339 | - } |
|
| 340 | - |
|
| 341 | - |
|
| 342 | - /** |
|
| 343 | - * Calculates all the EE routes and saves it to a WordPress option so we don't |
|
| 344 | - * need to calculate it on every request |
|
| 345 | - * |
|
| 346 | - * @deprecated since version 4.9.1 |
|
| 347 | - * @return void |
|
| 348 | - */ |
|
| 349 | - public static function save_ee_routes() |
|
| 350 | - { |
|
| 351 | - if (EE_Maintenance_Mode::instance()->models_can_query()) { |
|
| 352 | - $instance = self::instance(); |
|
| 353 | - $routes = apply_filters( |
|
| 354 | - 'EED_Core_Rest_Api__save_ee_routes__routes', |
|
| 355 | - array_replace_recursive( |
|
| 356 | - $instance->_register_config_routes(), |
|
| 357 | - $instance->_register_meta_routes(), |
|
| 358 | - $instance->_register_model_routes(), |
|
| 359 | - $instance->_register_rpc_routes() |
|
| 360 | - ) |
|
| 361 | - ); |
|
| 362 | - update_option(self::saved_routes_option_names, $routes, true); |
|
| 363 | - } |
|
| 364 | - } |
|
| 365 | - |
|
| 366 | - |
|
| 367 | - /** |
|
| 368 | - * Gets all the route information relating to EE models |
|
| 369 | - * |
|
| 370 | - * @return array @see get_ee_route_data |
|
| 371 | - * @deprecated since version 4.9.1 |
|
| 372 | - */ |
|
| 373 | - protected function _register_model_routes() |
|
| 374 | - { |
|
| 375 | - $model_routes = array(); |
|
| 376 | - foreach (self::versions_served() as $version => $hidden_endpoint) { |
|
| 377 | - $model_routes[ EED_Core_Rest_Api::ee_api_namespace |
|
| 378 | - . $version ] = $this->_get_config_route_data_for_version($version, $hidden_endpoint); |
|
| 379 | - } |
|
| 380 | - return $model_routes; |
|
| 381 | - } |
|
| 382 | - |
|
| 383 | - |
|
| 384 | - /** |
|
| 385 | - * Decides whether or not to add write endpoints for this model. |
|
| 386 | - * |
|
| 387 | - * Currently, this defaults to exclude all global tables and models |
|
| 388 | - * which would allow inserting WP core data (we don't want to duplicate |
|
| 389 | - * what WP API does, as it's unnecessary, extra work, and potentially extra bugs) |
|
| 390 | - * |
|
| 391 | - * @param EEM_Base $model |
|
| 392 | - * @return bool |
|
| 393 | - */ |
|
| 394 | - public static function should_have_write_endpoints(EEM_Base $model) |
|
| 395 | - { |
|
| 396 | - if ($model->is_wp_core_model()) { |
|
| 397 | - return false; |
|
| 398 | - } |
|
| 399 | - foreach ($model->get_tables() as $table) { |
|
| 400 | - if ($table->is_global()) { |
|
| 401 | - return false; |
|
| 402 | - } |
|
| 403 | - } |
|
| 404 | - return true; |
|
| 405 | - } |
|
| 406 | - |
|
| 407 | - |
|
| 408 | - /** |
|
| 409 | - * Gets the names of all models which should have plural routes (eg `ee/v4.8.36/events`) |
|
| 410 | - * in this versioned namespace of EE4 |
|
| 411 | - * |
|
| 412 | - * @param $version |
|
| 413 | - * @return array keys are model names (eg 'Event') and values ar either classnames (eg 'EEM_Event') |
|
| 414 | - */ |
|
| 415 | - public static function model_names_with_plural_routes($version) |
|
| 416 | - { |
|
| 417 | - $model_version_info = new ModelVersionInfo($version); |
|
| 418 | - $models_to_register = $model_version_info->modelsForRequestedVersion(); |
|
| 419 | - // let's not bother having endpoints for extra metas |
|
| 420 | - unset( |
|
| 421 | - $models_to_register['Extra_Meta'], |
|
| 422 | - $models_to_register['Extra_Join'], |
|
| 423 | - $models_to_register['Post_Meta'] |
|
| 424 | - ); |
|
| 425 | - return apply_filters( |
|
| 426 | - 'FHEE__EED_Core_REST_API___register_model_routes', |
|
| 427 | - $models_to_register |
|
| 428 | - ); |
|
| 429 | - } |
|
| 430 | - |
|
| 431 | - |
|
| 432 | - /** |
|
| 433 | - * Gets the route data for EE models in the specified version |
|
| 434 | - * |
|
| 435 | - * @param string $version |
|
| 436 | - * @param boolean $hidden_endpoint |
|
| 437 | - * @return array |
|
| 438 | - * @throws EE_Error |
|
| 439 | - */ |
|
| 440 | - protected function _get_model_route_data_for_version($version, $hidden_endpoint = false) |
|
| 441 | - { |
|
| 442 | - $model_routes = array(); |
|
| 443 | - $model_version_info = new ModelVersionInfo($version); |
|
| 444 | - foreach (EED_Core_Rest_Api::model_names_with_plural_routes($version) as $model_name => $model_classname) { |
|
| 445 | - $model = \EE_Registry::instance()->load_model($model_name); |
|
| 446 | - // if this isn't a valid model then let's skip iterate to the next item in the loop. |
|
| 447 | - if (! $model instanceof EEM_Base) { |
|
| 448 | - continue; |
|
| 449 | - } |
|
| 450 | - // yes we could just register one route for ALL models, but then they wouldn't show up in the index |
|
| 451 | - $plural_model_route = EED_Core_Rest_Api::get_collection_route($model); |
|
| 452 | - $singular_model_route = EED_Core_Rest_Api::get_entity_route($model, '(?P<id>[^\/]+)'); |
|
| 453 | - $model_routes[ $plural_model_route ] = array( |
|
| 454 | - array( |
|
| 455 | - 'callback' => array( |
|
| 456 | - 'EventEspresso\core\libraries\rest_api\controllers\model\Read', |
|
| 457 | - 'handleRequestGetAll', |
|
| 458 | - ), |
|
| 459 | - 'callback_args' => array($version, $model_name), |
|
| 460 | - 'methods' => WP_REST_Server::READABLE, |
|
| 461 | - 'hidden_endpoint' => $hidden_endpoint, |
|
| 462 | - 'args' => $this->_get_read_query_params($model, $version), |
|
| 463 | - '_links' => array( |
|
| 464 | - 'self' => rest_url(EED_Core_Rest_Api::ee_api_namespace . $version . $singular_model_route), |
|
| 465 | - ), |
|
| 466 | - ), |
|
| 467 | - 'schema' => array( |
|
| 468 | - 'schema_callback' => array( |
|
| 469 | - 'EventEspresso\core\libraries\rest_api\controllers\model\Read', |
|
| 470 | - 'handleSchemaRequest', |
|
| 471 | - ), |
|
| 472 | - 'callback_args' => array($version, $model_name), |
|
| 473 | - ), |
|
| 474 | - ); |
|
| 475 | - $model_routes[ $singular_model_route ] = array( |
|
| 476 | - array( |
|
| 477 | - 'callback' => array( |
|
| 478 | - 'EventEspresso\core\libraries\rest_api\controllers\model\Read', |
|
| 479 | - 'handleRequestGetOne', |
|
| 480 | - ), |
|
| 481 | - 'callback_args' => array($version, $model_name), |
|
| 482 | - 'methods' => WP_REST_Server::READABLE, |
|
| 483 | - 'hidden_endpoint' => $hidden_endpoint, |
|
| 484 | - 'args' => $this->_get_response_selection_query_params($model, $version), |
|
| 485 | - ), |
|
| 486 | - ); |
|
| 487 | - if (apply_filters( |
|
| 488 | - 'FHEE__EED_Core_Rest_Api___get_model_route_data_for_version__add_write_endpoints', |
|
| 489 | - EED_Core_Rest_Api::should_have_write_endpoints($model), |
|
| 490 | - $model |
|
| 491 | - )) { |
|
| 492 | - $model_routes[ $plural_model_route ][] = array( |
|
| 493 | - 'callback' => array( |
|
| 494 | - 'EventEspresso\core\libraries\rest_api\controllers\model\Write', |
|
| 495 | - 'handleRequestInsert', |
|
| 496 | - ), |
|
| 497 | - 'callback_args' => array($version, $model_name), |
|
| 498 | - 'methods' => WP_REST_Server::CREATABLE, |
|
| 499 | - 'hidden_endpoint' => $hidden_endpoint, |
|
| 500 | - 'args' => $this->_get_write_params($model_name, $model_version_info, true), |
|
| 501 | - ); |
|
| 502 | - $model_routes[ $singular_model_route ] = array_merge( |
|
| 503 | - $model_routes[ $singular_model_route ], |
|
| 504 | - array( |
|
| 505 | - array( |
|
| 506 | - 'callback' => array( |
|
| 507 | - 'EventEspresso\core\libraries\rest_api\controllers\model\Write', |
|
| 508 | - 'handleRequestUpdate', |
|
| 509 | - ), |
|
| 510 | - 'callback_args' => array($version, $model_name), |
|
| 511 | - 'methods' => WP_REST_Server::EDITABLE, |
|
| 512 | - 'hidden_endpoint' => $hidden_endpoint, |
|
| 513 | - 'args' => $this->_get_write_params($model_name, $model_version_info), |
|
| 514 | - ), |
|
| 515 | - array( |
|
| 516 | - 'callback' => array( |
|
| 517 | - 'EventEspresso\core\libraries\rest_api\controllers\model\Write', |
|
| 518 | - 'handleRequestDelete', |
|
| 519 | - ), |
|
| 520 | - 'callback_args' => array($version, $model_name), |
|
| 521 | - 'methods' => WP_REST_Server::DELETABLE, |
|
| 522 | - 'hidden_endpoint' => $hidden_endpoint, |
|
| 523 | - 'args' => $this->_get_delete_query_params($model, $version), |
|
| 524 | - ), |
|
| 525 | - ) |
|
| 526 | - ); |
|
| 527 | - } |
|
| 528 | - foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
|
| 529 | - $related_route = EED_Core_Rest_Api::get_relation_route_via( |
|
| 530 | - $model, |
|
| 531 | - '(?P<id>[^\/]+)', |
|
| 532 | - $relation_obj |
|
| 533 | - ); |
|
| 534 | - $endpoints = array( |
|
| 535 | - array( |
|
| 536 | - 'callback' => array( |
|
| 537 | - 'EventEspresso\core\libraries\rest_api\controllers\model\Read', |
|
| 538 | - 'handleRequestGetRelated', |
|
| 539 | - ), |
|
| 540 | - 'callback_args' => array($version, $model_name, $relation_name), |
|
| 541 | - 'methods' => WP_REST_Server::READABLE, |
|
| 542 | - 'hidden_endpoint' => $hidden_endpoint, |
|
| 543 | - 'args' => $this->_get_read_query_params($relation_obj->get_other_model(), $version), |
|
| 544 | - ), |
|
| 545 | - ); |
|
| 546 | - $model_routes[ $related_route ] = $endpoints; |
|
| 547 | - } |
|
| 548 | - } |
|
| 549 | - return $model_routes; |
|
| 550 | - } |
|
| 551 | - |
|
| 552 | - |
|
| 553 | - /** |
|
| 554 | - * Gets the relative URI to a model's REST API plural route, after the EE4 versioned namespace, |
|
| 555 | - * excluding the preceding slash. |
|
| 556 | - * Eg you pass get_plural_route_to('Event') = 'events' |
|
| 557 | - * |
|
| 558 | - * @param EEM_Base $model |
|
| 559 | - * @return string |
|
| 560 | - */ |
|
| 561 | - public static function get_collection_route(EEM_Base $model) |
|
| 562 | - { |
|
| 563 | - return EEH_Inflector::pluralize_and_lower($model->get_this_model_name()); |
|
| 564 | - } |
|
| 565 | - |
|
| 566 | - |
|
| 567 | - /** |
|
| 568 | - * Gets the relative URI to a model's REST API singular route, after the EE4 versioned namespace, |
|
| 569 | - * excluding the preceding slash. |
|
| 570 | - * Eg you pass get_plural_route_to('Event', 12) = 'events/12' |
|
| 571 | - * |
|
| 572 | - * @param EEM_Base $model eg Event or Venue |
|
| 573 | - * @param string $id |
|
| 574 | - * @return string |
|
| 575 | - */ |
|
| 576 | - public static function get_entity_route($model, $id) |
|
| 577 | - { |
|
| 578 | - return EED_Core_Rest_Api::get_collection_route($model) . '/' . $id; |
|
| 579 | - } |
|
| 580 | - |
|
| 581 | - |
|
| 582 | - /** |
|
| 583 | - * Gets the relative URI to a model's REST API singular route, after the EE4 versioned namespace, |
|
| 584 | - * excluding the preceding slash. |
|
| 585 | - * Eg you pass get_plural_route_to('Event', 12) = 'events/12' |
|
| 586 | - * |
|
| 587 | - * @param EEM_Base $model eg Event or Venue |
|
| 588 | - * @param string $id |
|
| 589 | - * @param EE_Model_Relation_Base $relation_obj |
|
| 590 | - * @return string |
|
| 591 | - */ |
|
| 592 | - public static function get_relation_route_via(EEM_Base $model, $id, EE_Model_Relation_Base $relation_obj) |
|
| 593 | - { |
|
| 594 | - $related_model_name_endpoint_part = ModelRead::getRelatedEntityName( |
|
| 595 | - $relation_obj->get_other_model()->get_this_model_name(), |
|
| 596 | - $relation_obj |
|
| 597 | - ); |
|
| 598 | - return EED_Core_Rest_Api::get_entity_route($model, $id) . '/' . $related_model_name_endpoint_part; |
|
| 599 | - } |
|
| 600 | - |
|
| 601 | - |
|
| 602 | - /** |
|
| 603 | - * Adds onto the $relative_route the EE4 REST API versioned namespace. |
|
| 604 | - * Eg if given '4.8.36' and 'events', will return 'ee/v4.8.36/events' |
|
| 605 | - * |
|
| 606 | - * @param string $relative_route |
|
| 607 | - * @param string $version |
|
| 608 | - * @return string |
|
| 609 | - */ |
|
| 610 | - public static function get_versioned_route_to($relative_route, $version = '4.8.36') |
|
| 611 | - { |
|
| 612 | - return '/' . EED_Core_Rest_Api::ee_api_namespace . $version . '/' . $relative_route; |
|
| 613 | - } |
|
| 614 | - |
|
| 615 | - |
|
| 616 | - /** |
|
| 617 | - * Adds all the RPC-style routes (remote procedure call-like routes, ie |
|
| 618 | - * routes that don't conform to the traditional REST CRUD-style). |
|
| 619 | - * |
|
| 620 | - * @deprecated since 4.9.1 |
|
| 621 | - */ |
|
| 622 | - protected function _register_rpc_routes() |
|
| 623 | - { |
|
| 624 | - $routes = array(); |
|
| 625 | - foreach (self::versions_served() as $version => $hidden_endpoint) { |
|
| 626 | - $routes[ self::ee_api_namespace . $version ] = $this->_get_rpc_route_data_for_version( |
|
| 627 | - $version, |
|
| 628 | - $hidden_endpoint |
|
| 629 | - ); |
|
| 630 | - } |
|
| 631 | - return $routes; |
|
| 632 | - } |
|
| 633 | - |
|
| 634 | - |
|
| 635 | - /** |
|
| 636 | - * @param string $version |
|
| 637 | - * @param boolean $hidden_endpoint |
|
| 638 | - * @return array |
|
| 639 | - */ |
|
| 640 | - protected function _get_rpc_route_data_for_version($version, $hidden_endpoint = false) |
|
| 641 | - { |
|
| 642 | - $this_versions_routes = array(); |
|
| 643 | - // checkin endpoint |
|
| 644 | - $this_versions_routes['registrations/(?P<REG_ID>\d+)/toggle_checkin_for_datetime/(?P<DTT_ID>\d+)'] = array( |
|
| 645 | - array( |
|
| 646 | - 'callback' => array( |
|
| 647 | - 'EventEspresso\core\libraries\rest_api\controllers\rpc\Checkin', |
|
| 648 | - 'handleRequestToggleCheckin', |
|
| 649 | - ), |
|
| 650 | - 'methods' => WP_REST_Server::CREATABLE, |
|
| 651 | - 'hidden_endpoint' => $hidden_endpoint, |
|
| 652 | - 'args' => array( |
|
| 653 | - 'force' => array( |
|
| 654 | - 'required' => false, |
|
| 655 | - 'default' => false, |
|
| 656 | - 'description' => __( |
|
| 657 | - // @codingStandardsIgnoreStart |
|
| 658 | - 'Whether to force toggle checkin, or to verify the registration status and allowed ticket uses', |
|
| 659 | - // @codingStandardsIgnoreEnd |
|
| 660 | - 'event_espresso' |
|
| 661 | - ), |
|
| 662 | - ), |
|
| 663 | - ), |
|
| 664 | - 'callback_args' => array($version), |
|
| 665 | - ), |
|
| 666 | - ); |
|
| 667 | - return apply_filters( |
|
| 668 | - 'FHEE__EED_Core_Rest_Api___register_rpc_routes__this_versions_routes', |
|
| 669 | - $this_versions_routes, |
|
| 670 | - $version, |
|
| 671 | - $hidden_endpoint |
|
| 672 | - ); |
|
| 673 | - } |
|
| 674 | - |
|
| 675 | - |
|
| 676 | - /** |
|
| 677 | - * Gets the query params that can be used when request one or many |
|
| 678 | - * |
|
| 679 | - * @param EEM_Base $model |
|
| 680 | - * @param string $version |
|
| 681 | - * @return array |
|
| 682 | - */ |
|
| 683 | - protected function _get_response_selection_query_params(\EEM_Base $model, $version) |
|
| 684 | - { |
|
| 685 | - return apply_filters( |
|
| 686 | - 'FHEE__EED_Core_Rest_Api___get_response_selection_query_params', |
|
| 687 | - array( |
|
| 688 | - 'include' => array( |
|
| 689 | - 'required' => false, |
|
| 690 | - 'default' => '*', |
|
| 691 | - 'type' => 'string', |
|
| 692 | - ), |
|
| 693 | - 'calculate' => array( |
|
| 694 | - 'required' => false, |
|
| 695 | - 'default' => '', |
|
| 696 | - 'enum' => self::$_field_calculator->retrieveCalculatedFieldsForModel($model), |
|
| 697 | - 'type' => 'string', |
|
| 698 | - // because we accept a CSV'd list of the enumerated strings, WP core validation and sanitization |
|
| 699 | - // freaks out. We'll just validate this argument while handling the request |
|
| 700 | - 'validate_callback' => null, |
|
| 701 | - 'sanitize_callback' => null, |
|
| 702 | - ), |
|
| 703 | - ), |
|
| 704 | - $model, |
|
| 705 | - $version |
|
| 706 | - ); |
|
| 707 | - } |
|
| 708 | - |
|
| 709 | - |
|
| 710 | - /** |
|
| 711 | - * Gets the parameters acceptable for delete requests |
|
| 712 | - * |
|
| 713 | - * @param \EEM_Base $model |
|
| 714 | - * @param string $version |
|
| 715 | - * @return array |
|
| 716 | - */ |
|
| 717 | - protected function _get_delete_query_params(\EEM_Base $model, $version) |
|
| 718 | - { |
|
| 719 | - $params_for_delete = array( |
|
| 720 | - 'allow_blocking' => array( |
|
| 721 | - 'required' => false, |
|
| 722 | - 'default' => true, |
|
| 723 | - 'type' => 'boolean', |
|
| 724 | - ), |
|
| 725 | - ); |
|
| 726 | - $params_for_delete['force'] = array( |
|
| 727 | - 'required' => false, |
|
| 728 | - 'default' => false, |
|
| 729 | - 'type' => 'boolean', |
|
| 730 | - ); |
|
| 731 | - return apply_filters( |
|
| 732 | - 'FHEE__EED_Core_Rest_Api___get_delete_query_params', |
|
| 733 | - $params_for_delete, |
|
| 734 | - $model, |
|
| 735 | - $version |
|
| 736 | - ); |
|
| 737 | - } |
|
| 738 | - |
|
| 739 | - |
|
| 740 | - /** |
|
| 741 | - * Gets info about reading query params that are acceptable |
|
| 742 | - * |
|
| 743 | - * @param \EEM_Base $model eg 'Event' or 'Venue' |
|
| 744 | - * @param string $version |
|
| 745 | - * @return array describing the args acceptable when querying this model |
|
| 746 | - * @throws EE_Error |
|
| 747 | - */ |
|
| 748 | - protected function _get_read_query_params(\EEM_Base $model, $version) |
|
| 749 | - { |
|
| 750 | - $default_orderby = array(); |
|
| 751 | - foreach ($model->get_combined_primary_key_fields() as $key_field) { |
|
| 752 | - $default_orderby[ $key_field->get_name() ] = 'ASC'; |
|
| 753 | - } |
|
| 754 | - return array_merge( |
|
| 755 | - $this->_get_response_selection_query_params($model, $version), |
|
| 756 | - array( |
|
| 757 | - 'where' => array( |
|
| 758 | - 'required' => false, |
|
| 759 | - 'default' => array(), |
|
| 760 | - 'type' => 'object', |
|
| 761 | - // because we accept an almost infinite list of possible where conditions, WP |
|
| 762 | - // core validation and sanitization freaks out. We'll just validate this argument |
|
| 763 | - // while handling the request |
|
| 764 | - 'validate_callback' => null, |
|
| 765 | - 'sanitize_callback' => null, |
|
| 766 | - ), |
|
| 767 | - 'limit' => array( |
|
| 768 | - 'required' => false, |
|
| 769 | - 'default' => EED_Core_Rest_Api::get_default_query_limit(), |
|
| 770 | - 'type' => array( |
|
| 771 | - 'array', |
|
| 772 | - 'string', |
|
| 773 | - 'integer', |
|
| 774 | - ), |
|
| 775 | - // because we accept a variety of types, WP core validation and sanitization |
|
| 776 | - // freaks out. We'll just validate this argument while handling the request |
|
| 777 | - 'validate_callback' => null, |
|
| 778 | - 'sanitize_callback' => null, |
|
| 779 | - ), |
|
| 780 | - 'order_by' => array( |
|
| 781 | - 'required' => false, |
|
| 782 | - 'default' => $default_orderby, |
|
| 783 | - 'type' => array( |
|
| 784 | - 'object', |
|
| 785 | - 'string', |
|
| 786 | - ),// because we accept a variety of types, WP core validation and sanitization |
|
| 787 | - // freaks out. We'll just validate this argument while handling the request |
|
| 788 | - 'validate_callback' => null, |
|
| 789 | - 'sanitize_callback' => null, |
|
| 790 | - ), |
|
| 791 | - 'group_by' => array( |
|
| 792 | - 'required' => false, |
|
| 793 | - 'default' => null, |
|
| 794 | - 'type' => array( |
|
| 795 | - 'object', |
|
| 796 | - 'string', |
|
| 797 | - ), |
|
| 798 | - // because we accept an almost infinite list of possible groupings, |
|
| 799 | - // WP core validation and sanitization |
|
| 800 | - // freaks out. We'll just validate this argument while handling the request |
|
| 801 | - 'validate_callback' => null, |
|
| 802 | - 'sanitize_callback' => null, |
|
| 803 | - ), |
|
| 804 | - 'having' => array( |
|
| 805 | - 'required' => false, |
|
| 806 | - 'default' => null, |
|
| 807 | - 'type' => 'object', |
|
| 808 | - // because we accept an almost infinite list of possible where conditions, WP |
|
| 809 | - // core validation and sanitization freaks out. We'll just validate this argument |
|
| 810 | - // while handling the request |
|
| 811 | - 'validate_callback' => null, |
|
| 812 | - 'sanitize_callback' => null, |
|
| 813 | - ), |
|
| 814 | - 'caps' => array( |
|
| 815 | - 'required' => false, |
|
| 816 | - 'default' => EEM_Base::caps_read, |
|
| 817 | - 'type' => 'string', |
|
| 818 | - 'enum' => array( |
|
| 819 | - EEM_Base::caps_read, |
|
| 820 | - EEM_Base::caps_read_admin, |
|
| 821 | - EEM_Base::caps_edit, |
|
| 822 | - EEM_Base::caps_delete, |
|
| 823 | - ), |
|
| 824 | - ), |
|
| 825 | - ) |
|
| 826 | - ); |
|
| 827 | - } |
|
| 828 | - |
|
| 829 | - |
|
| 830 | - /** |
|
| 831 | - * Gets parameter information for a model regarding writing data |
|
| 832 | - * |
|
| 833 | - * @param string $model_name |
|
| 834 | - * @param ModelVersionInfo $model_version_info |
|
| 835 | - * @param boolean $create whether this is for request to create (in |
|
| 836 | - * which case we need all required params) or |
|
| 837 | - * just to update (in which case we don't |
|
| 838 | - * need those on every request) |
|
| 839 | - * @return array |
|
| 840 | - */ |
|
| 841 | - protected function _get_write_params( |
|
| 842 | - $model_name, |
|
| 843 | - ModelVersionInfo $model_version_info, |
|
| 844 | - $create = false |
|
| 845 | - ) { |
|
| 846 | - $model = EE_Registry::instance()->load_model($model_name); |
|
| 847 | - $fields = $model_version_info->fieldsOnModelInThisVersion($model); |
|
| 848 | - $args_info = array(); |
|
| 849 | - foreach ($fields as $field_name => $field_obj) { |
|
| 850 | - if ($field_obj->is_auto_increment()) { |
|
| 851 | - // totally ignore auto increment IDs |
|
| 852 | - continue; |
|
| 853 | - } |
|
| 854 | - $arg_info = $field_obj->getSchema(); |
|
| 855 | - $required = $create && ! $field_obj->is_nullable() && $field_obj->get_default_value() === null; |
|
| 856 | - $arg_info['required'] = $required; |
|
| 857 | - // remove the read-only flag. If it were read-only we wouldn't list it as an argument while writing, right? |
|
| 858 | - unset($arg_info['readonly']); |
|
| 859 | - $schema_properties = $field_obj->getSchemaProperties(); |
|
| 860 | - if (isset($schema_properties['raw']) |
|
| 861 | - && $field_obj->getSchemaType() === 'object' |
|
| 862 | - ) { |
|
| 863 | - // if there's a "raw" form of this argument, use those properties instead |
|
| 864 | - $arg_info = array_replace( |
|
| 865 | - $arg_info, |
|
| 866 | - $schema_properties['raw'] |
|
| 867 | - ); |
|
| 868 | - } |
|
| 869 | - $arg_info['default'] = ModelDataTranslator::prepareFieldValueForJson( |
|
| 870 | - $field_obj, |
|
| 871 | - $field_obj->get_default_value(), |
|
| 872 | - $model_version_info->requestedVersion() |
|
| 873 | - ); |
|
| 874 | - // we do our own validation and sanitization within the controller |
|
| 875 | - if (function_exists('rest_validate_value_from_schema')) { |
|
| 876 | - $sanitize_callback = array( |
|
| 877 | - 'EED_Core_Rest_Api', |
|
| 878 | - 'default_sanitize_callback', |
|
| 879 | - ); |
|
| 880 | - } else { |
|
| 881 | - $sanitize_callback = null; |
|
| 882 | - } |
|
| 883 | - $arg_info['sanitize_callback'] = $sanitize_callback; |
|
| 884 | - $args_info[ $field_name ] = $arg_info; |
|
| 885 | - if ($field_obj instanceof EE_Datetime_Field) { |
|
| 886 | - $gmt_arg_info = $arg_info; |
|
| 887 | - $gmt_arg_info['description'] = sprintf( |
|
| 888 | - esc_html__( |
|
| 889 | - '%1$s - the value for this field in UTC. Ignored if %2$s is provided.', |
|
| 890 | - 'event_espresso' |
|
| 891 | - ), |
|
| 892 | - $field_obj->get_nicename(), |
|
| 893 | - $field_name |
|
| 894 | - ); |
|
| 895 | - $args_info[ $field_name . '_gmt' ] = $gmt_arg_info; |
|
| 896 | - } |
|
| 897 | - } |
|
| 898 | - return $args_info; |
|
| 899 | - } |
|
| 900 | - |
|
| 901 | - |
|
| 902 | - /** |
|
| 903 | - * Replacement for WP API's 'rest_parse_request_arg'. |
|
| 904 | - * If the value is blank but not required, don't bother validating it. |
|
| 905 | - * Also, it uses our email validation instead of WP API's default. |
|
| 906 | - * |
|
| 907 | - * @param $value |
|
| 908 | - * @param WP_REST_Request $request |
|
| 909 | - * @param $param |
|
| 910 | - * @return bool|true|WP_Error |
|
| 911 | - * @throws InvalidArgumentException |
|
| 912 | - * @throws InvalidInterfaceException |
|
| 913 | - * @throws InvalidDataTypeException |
|
| 914 | - */ |
|
| 915 | - public static function default_sanitize_callback($value, WP_REST_Request $request, $param) |
|
| 916 | - { |
|
| 917 | - $attributes = $request->get_attributes(); |
|
| 918 | - if (! isset($attributes['args'][ $param ]) |
|
| 919 | - || ! is_array($attributes['args'][ $param ])) { |
|
| 920 | - $validation_result = true; |
|
| 921 | - } else { |
|
| 922 | - $args = $attributes['args'][ $param ]; |
|
| 923 | - if (( |
|
| 924 | - $value === '' |
|
| 925 | - || $value === null |
|
| 926 | - ) |
|
| 927 | - && (! isset($args['required']) |
|
| 928 | - || $args['required'] === false |
|
| 929 | - ) |
|
| 930 | - ) { |
|
| 931 | - // not required and not provided? that's cool |
|
| 932 | - $validation_result = true; |
|
| 933 | - } elseif (isset($args['format']) |
|
| 934 | - && $args['format'] === 'email' |
|
| 935 | - ) { |
|
| 936 | - $validation_result = true; |
|
| 937 | - if (! self::_validate_email($value)) { |
|
| 938 | - $validation_result = new WP_Error( |
|
| 939 | - 'rest_invalid_param', |
|
| 940 | - esc_html__( |
|
| 941 | - 'The email address is not valid or does not exist.', |
|
| 942 | - 'event_espresso' |
|
| 943 | - ) |
|
| 944 | - ); |
|
| 945 | - } |
|
| 946 | - } else { |
|
| 947 | - $validation_result = rest_validate_value_from_schema($value, $args, $param); |
|
| 948 | - } |
|
| 949 | - } |
|
| 950 | - if (is_wp_error($validation_result)) { |
|
| 951 | - return $validation_result; |
|
| 952 | - } |
|
| 953 | - return rest_sanitize_request_arg($value, $request, $param); |
|
| 954 | - } |
|
| 955 | - |
|
| 956 | - |
|
| 957 | - /** |
|
| 958 | - * Returns whether or not this email address is valid. Copied from EE_Email_Validation_Strategy::_validate_email() |
|
| 959 | - * |
|
| 960 | - * @param $email |
|
| 961 | - * @return bool |
|
| 962 | - * @throws InvalidArgumentException |
|
| 963 | - * @throws InvalidInterfaceException |
|
| 964 | - * @throws InvalidDataTypeException |
|
| 965 | - */ |
|
| 966 | - protected static function _validate_email($email) |
|
| 967 | - { |
|
| 968 | - try { |
|
| 969 | - EmailAddressFactory::create($email); |
|
| 970 | - return true; |
|
| 971 | - } catch (EmailValidationException $e) { |
|
| 972 | - return false; |
|
| 973 | - } |
|
| 974 | - } |
|
| 975 | - |
|
| 976 | - |
|
| 977 | - /** |
|
| 978 | - * Gets routes for the config |
|
| 979 | - * |
|
| 980 | - * @return array @see _register_model_routes |
|
| 981 | - * @deprecated since version 4.9.1 |
|
| 982 | - */ |
|
| 983 | - protected function _register_config_routes() |
|
| 984 | - { |
|
| 985 | - $config_routes = array(); |
|
| 986 | - foreach (self::versions_served() as $version => $hidden_endpoint) { |
|
| 987 | - $config_routes[ self::ee_api_namespace . $version ] = $this->_get_config_route_data_for_version( |
|
| 988 | - $version, |
|
| 989 | - $hidden_endpoint |
|
| 990 | - ); |
|
| 991 | - } |
|
| 992 | - return $config_routes; |
|
| 993 | - } |
|
| 994 | - |
|
| 995 | - |
|
| 996 | - /** |
|
| 997 | - * Gets routes for the config for the specified version |
|
| 998 | - * |
|
| 999 | - * @param string $version |
|
| 1000 | - * @param boolean $hidden_endpoint |
|
| 1001 | - * @return array |
|
| 1002 | - */ |
|
| 1003 | - protected function _get_config_route_data_for_version($version, $hidden_endpoint) |
|
| 1004 | - { |
|
| 1005 | - return array( |
|
| 1006 | - 'config' => array( |
|
| 1007 | - array( |
|
| 1008 | - 'callback' => array( |
|
| 1009 | - 'EventEspresso\core\libraries\rest_api\controllers\config\Read', |
|
| 1010 | - 'handleRequest', |
|
| 1011 | - ), |
|
| 1012 | - 'methods' => WP_REST_Server::READABLE, |
|
| 1013 | - 'hidden_endpoint' => $hidden_endpoint, |
|
| 1014 | - 'callback_args' => array($version), |
|
| 1015 | - ), |
|
| 1016 | - ), |
|
| 1017 | - 'site_info' => array( |
|
| 1018 | - array( |
|
| 1019 | - 'callback' => array( |
|
| 1020 | - 'EventEspresso\core\libraries\rest_api\controllers\config\Read', |
|
| 1021 | - 'handleRequestSiteInfo', |
|
| 1022 | - ), |
|
| 1023 | - 'methods' => WP_REST_Server::READABLE, |
|
| 1024 | - 'hidden_endpoint' => $hidden_endpoint, |
|
| 1025 | - 'callback_args' => array($version), |
|
| 1026 | - ), |
|
| 1027 | - ), |
|
| 1028 | - ); |
|
| 1029 | - } |
|
| 1030 | - |
|
| 1031 | - |
|
| 1032 | - /** |
|
| 1033 | - * Gets the meta info routes |
|
| 1034 | - * |
|
| 1035 | - * @return array @see _register_model_routes |
|
| 1036 | - * @deprecated since version 4.9.1 |
|
| 1037 | - */ |
|
| 1038 | - protected function _register_meta_routes() |
|
| 1039 | - { |
|
| 1040 | - $meta_routes = array(); |
|
| 1041 | - foreach (self::versions_served() as $version => $hidden_endpoint) { |
|
| 1042 | - $meta_routes[ self::ee_api_namespace . $version ] = $this->_get_meta_route_data_for_version( |
|
| 1043 | - $version, |
|
| 1044 | - $hidden_endpoint |
|
| 1045 | - ); |
|
| 1046 | - } |
|
| 1047 | - return $meta_routes; |
|
| 1048 | - } |
|
| 1049 | - |
|
| 1050 | - |
|
| 1051 | - /** |
|
| 1052 | - * @param string $version |
|
| 1053 | - * @param boolean $hidden_endpoint |
|
| 1054 | - * @return array |
|
| 1055 | - */ |
|
| 1056 | - protected function _get_meta_route_data_for_version($version, $hidden_endpoint = false) |
|
| 1057 | - { |
|
| 1058 | - return array( |
|
| 1059 | - 'resources' => array( |
|
| 1060 | - array( |
|
| 1061 | - 'callback' => array( |
|
| 1062 | - 'EventEspresso\core\libraries\rest_api\controllers\model\Meta', |
|
| 1063 | - 'handleRequestModelsMeta', |
|
| 1064 | - ), |
|
| 1065 | - 'methods' => WP_REST_Server::READABLE, |
|
| 1066 | - 'hidden_endpoint' => $hidden_endpoint, |
|
| 1067 | - 'callback_args' => array($version), |
|
| 1068 | - ), |
|
| 1069 | - ), |
|
| 1070 | - ); |
|
| 1071 | - } |
|
| 1072 | - |
|
| 1073 | - |
|
| 1074 | - /** |
|
| 1075 | - * Tries to hide old 4.6 endpoints from the |
|
| 1076 | - * |
|
| 1077 | - * @param array $route_data |
|
| 1078 | - * @return array |
|
| 1079 | - * @throws \EE_Error |
|
| 1080 | - */ |
|
| 1081 | - public static function hide_old_endpoints($route_data) |
|
| 1082 | - { |
|
| 1083 | - // allow API clients to override which endpoints get hidden, in case |
|
| 1084 | - // they want to discover particular endpoints |
|
| 1085 | - // also, we don't have access to the request so we have to just grab it from the superglobal |
|
| 1086 | - $force_show_ee_namespace = ltrim( |
|
| 1087 | - EEH_Array::is_set($_REQUEST, 'force_show_ee_namespace', ''), |
|
| 1088 | - '/' |
|
| 1089 | - ); |
|
| 1090 | - foreach (EED_Core_Rest_Api::get_ee_route_data() as $namespace => $relative_urls) { |
|
| 1091 | - foreach ($relative_urls as $resource_name => $endpoints) { |
|
| 1092 | - foreach ($endpoints as $key => $endpoint) { |
|
| 1093 | - // skip schema and other route options |
|
| 1094 | - if (! is_numeric($key)) { |
|
| 1095 | - continue; |
|
| 1096 | - } |
|
| 1097 | - // by default, hide "hidden_endpoint"s, unless the request indicates |
|
| 1098 | - // to $force_show_ee_namespace, in which case only show that one |
|
| 1099 | - // namespace's endpoints (and hide all others) |
|
| 1100 | - if (($force_show_ee_namespace !== '' && $force_show_ee_namespace !== $namespace) |
|
| 1101 | - || ($endpoint['hidden_endpoint'] && $force_show_ee_namespace === '') |
|
| 1102 | - ) { |
|
| 1103 | - $full_route = '/' . ltrim($namespace, '/'); |
|
| 1104 | - $full_route .= '/' . ltrim($resource_name, '/'); |
|
| 1105 | - unset($route_data[ $full_route ]); |
|
| 1106 | - } |
|
| 1107 | - } |
|
| 1108 | - } |
|
| 1109 | - } |
|
| 1110 | - return $route_data; |
|
| 1111 | - } |
|
| 1112 | - |
|
| 1113 | - |
|
| 1114 | - /** |
|
| 1115 | - * Returns an array describing which versions of core support serving requests for. |
|
| 1116 | - * Keys are core versions' major and minor version, and values are the |
|
| 1117 | - * LOWEST requested version they can serve. Eg, 4.7 can serve requests for 4.6-like |
|
| 1118 | - * data by just removing a few models and fields from the responses. However, 4.15 might remove |
|
| 1119 | - * the answers table entirely, in which case it would be very difficult for |
|
| 1120 | - * it to serve 4.6-style responses. |
|
| 1121 | - * Versions of core that are missing from this array are unknowns. |
|
| 1122 | - * previous ver |
|
| 1123 | - * |
|
| 1124 | - * @return array |
|
| 1125 | - */ |
|
| 1126 | - public static function version_compatibilities() |
|
| 1127 | - { |
|
| 1128 | - return apply_filters( |
|
| 1129 | - 'FHEE__EED_Core_REST_API__version_compatibilities', |
|
| 1130 | - array( |
|
| 1131 | - '4.8.29' => '4.8.29', |
|
| 1132 | - '4.8.33' => '4.8.29', |
|
| 1133 | - '4.8.34' => '4.8.29', |
|
| 1134 | - '4.8.36' => '4.8.29', |
|
| 1135 | - ) |
|
| 1136 | - ); |
|
| 1137 | - } |
|
| 1138 | - |
|
| 1139 | - |
|
| 1140 | - /** |
|
| 1141 | - * Gets the latest API version served. Eg if there |
|
| 1142 | - * are two versions served of the API, 4.8.29 and 4.8.32, and |
|
| 1143 | - * we are on core version 4.8.34, it will return the string "4.8.32" |
|
| 1144 | - * |
|
| 1145 | - * @return string |
|
| 1146 | - */ |
|
| 1147 | - public static function latest_rest_api_version() |
|
| 1148 | - { |
|
| 1149 | - $versions_served = \EED_Core_Rest_Api::versions_served(); |
|
| 1150 | - $versions_served_keys = array_keys($versions_served); |
|
| 1151 | - return end($versions_served_keys); |
|
| 1152 | - } |
|
| 1153 | - |
|
| 1154 | - |
|
| 1155 | - /** |
|
| 1156 | - * Using EED_Core_Rest_Api::version_compatibilities(), determines what version of |
|
| 1157 | - * EE the API can serve requests for. Eg, if we are on 4.15 of core, and |
|
| 1158 | - * we can serve requests from 4.12 or later, this will return array( '4.12', '4.13', '4.14', '4.15' ). |
|
| 1159 | - * We also indicate whether or not this version should be put in the index or not |
|
| 1160 | - * |
|
| 1161 | - * @return array keys are API version numbers (just major and minor numbers), and values |
|
| 1162 | - * are whether or not they should be hidden |
|
| 1163 | - */ |
|
| 1164 | - public static function versions_served() |
|
| 1165 | - { |
|
| 1166 | - $versions_served = array(); |
|
| 1167 | - $possibly_served_versions = EED_Core_Rest_Api::version_compatibilities(); |
|
| 1168 | - $lowest_compatible_version = end($possibly_served_versions); |
|
| 1169 | - reset($possibly_served_versions); |
|
| 1170 | - $versions_served_historically = array_keys($possibly_served_versions); |
|
| 1171 | - $latest_version = end($versions_served_historically); |
|
| 1172 | - reset($versions_served_historically); |
|
| 1173 | - // for each version of core we have ever served: |
|
| 1174 | - foreach ($versions_served_historically as $key_versioned_endpoint) { |
|
| 1175 | - // if it's not above the current core version, and it's compatible with the current version of core |
|
| 1176 | - if ($key_versioned_endpoint === $latest_version) { |
|
| 1177 | - // don't hide the latest version in the index |
|
| 1178 | - $versions_served[ $key_versioned_endpoint ] = false; |
|
| 1179 | - } elseif ($key_versioned_endpoint >= $lowest_compatible_version |
|
| 1180 | - && $key_versioned_endpoint < EED_Core_Rest_Api::core_version() |
|
| 1181 | - ) { |
|
| 1182 | - // include, but hide, previous versions which are still supported |
|
| 1183 | - $versions_served[ $key_versioned_endpoint ] = true; |
|
| 1184 | - } elseif (apply_filters( |
|
| 1185 | - 'FHEE__EED_Core_Rest_Api__versions_served__include_incompatible_versions', |
|
| 1186 | - false, |
|
| 1187 | - $possibly_served_versions |
|
| 1188 | - )) { |
|
| 1189 | - // if a version is no longer supported, don't include it in index or list of versions served |
|
| 1190 | - $versions_served[ $key_versioned_endpoint ] = true; |
|
| 1191 | - } |
|
| 1192 | - } |
|
| 1193 | - return $versions_served; |
|
| 1194 | - } |
|
| 1195 | - |
|
| 1196 | - |
|
| 1197 | - /** |
|
| 1198 | - * Gets the major and minor version of EE core's version string |
|
| 1199 | - * |
|
| 1200 | - * @return string |
|
| 1201 | - */ |
|
| 1202 | - public static function core_version() |
|
| 1203 | - { |
|
| 1204 | - return apply_filters( |
|
| 1205 | - 'FHEE__EED_Core_REST_API__core_version', |
|
| 1206 | - implode( |
|
| 1207 | - '.', |
|
| 1208 | - array_slice( |
|
| 1209 | - explode( |
|
| 1210 | - '.', |
|
| 1211 | - espresso_version() |
|
| 1212 | - ), |
|
| 1213 | - 0, |
|
| 1214 | - 3 |
|
| 1215 | - ) |
|
| 1216 | - ) |
|
| 1217 | - ); |
|
| 1218 | - } |
|
| 1219 | - |
|
| 1220 | - |
|
| 1221 | - /** |
|
| 1222 | - * Gets the default limit that should be used when querying for resources |
|
| 1223 | - * |
|
| 1224 | - * @return int |
|
| 1225 | - */ |
|
| 1226 | - public static function get_default_query_limit() |
|
| 1227 | - { |
|
| 1228 | - // we actually don't use a const because we want folks to always use |
|
| 1229 | - // this method, not the const directly |
|
| 1230 | - return apply_filters( |
|
| 1231 | - 'FHEE__EED_Core_Rest_Api__get_default_query_limit', |
|
| 1232 | - 50 |
|
| 1233 | - ); |
|
| 1234 | - } |
|
| 1235 | - |
|
| 1236 | - |
|
| 1237 | - /** |
|
| 1238 | - * run - initial module setup |
|
| 1239 | - * |
|
| 1240 | - * @access public |
|
| 1241 | - * @param WP $WP |
|
| 1242 | - * @return void |
|
| 1243 | - */ |
|
| 1244 | - public function run($WP) |
|
| 1245 | - { |
|
| 1246 | - } |
|
| 26 | + const ee_api_namespace = Domain::API_NAMESPACE; |
|
| 27 | + |
|
| 28 | + const ee_api_namespace_for_regex = 'ee\/v([^/]*)\/'; |
|
| 29 | + |
|
| 30 | + const saved_routes_option_names = 'ee_core_routes'; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * string used in _links response bodies to make them globally unique. |
|
| 34 | + * |
|
| 35 | + * @see http://v2.wp-api.org/extending/linking/ |
|
| 36 | + */ |
|
| 37 | + const ee_api_link_namespace = 'https://api.eventespresso.com/'; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * @var CalculatedModelFields |
|
| 41 | + */ |
|
| 42 | + protected static $_field_calculator; |
|
| 43 | + |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * @return EED_Core_Rest_Api|EED_Module |
|
| 47 | + */ |
|
| 48 | + public static function instance() |
|
| 49 | + { |
|
| 50 | + self::$_field_calculator = LoaderFactory::getLoader()->load('EventEspresso\core\libraries\rest_api\CalculatedModelFields'); |
|
| 51 | + return parent::get_instance(__CLASS__); |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * set_hooks - for hooking into EE Core, other modules, etc |
|
| 57 | + * |
|
| 58 | + * @access public |
|
| 59 | + * @return void |
|
| 60 | + */ |
|
| 61 | + public static function set_hooks() |
|
| 62 | + { |
|
| 63 | + self::set_hooks_both(); |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * set_hooks_admin - for hooking into EE Admin Core, other modules, etc |
|
| 69 | + * |
|
| 70 | + * @access public |
|
| 71 | + * @return void |
|
| 72 | + */ |
|
| 73 | + public static function set_hooks_admin() |
|
| 74 | + { |
|
| 75 | + self::set_hooks_both(); |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + |
|
| 79 | + public static function set_hooks_both() |
|
| 80 | + { |
|
| 81 | + add_action('rest_api_init', array('EED_Core_Rest_Api', 'register_routes'), 10); |
|
| 82 | + add_action('rest_api_init', array('EED_Core_Rest_Api', 'set_hooks_rest_api'), 5); |
|
| 83 | + add_filter('rest_route_data', array('EED_Core_Rest_Api', 'hide_old_endpoints'), 10, 2); |
|
| 84 | + add_filter( |
|
| 85 | + 'rest_index', |
|
| 86 | + array('EventEspresso\core\libraries\rest_api\controllers\model\Meta', 'filterEeMetadataIntoIndex') |
|
| 87 | + ); |
|
| 88 | + EED_Core_Rest_Api::invalidate_cached_route_data_on_version_change(); |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * sets up hooks which only need to be included as part of REST API requests; |
|
| 94 | + * other requests like to the frontend or admin etc don't need them |
|
| 95 | + * |
|
| 96 | + * @throws \EE_Error |
|
| 97 | + */ |
|
| 98 | + public static function set_hooks_rest_api() |
|
| 99 | + { |
|
| 100 | + // set hooks which account for changes made to the API |
|
| 101 | + EED_Core_Rest_Api::_set_hooks_for_changes(); |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * public wrapper of _set_hooks_for_changes. |
|
| 107 | + * Loads all the hooks which make requests to old versions of the API |
|
| 108 | + * appear the same as they always did |
|
| 109 | + * |
|
| 110 | + * @throws EE_Error |
|
| 111 | + */ |
|
| 112 | + public static function set_hooks_for_changes() |
|
| 113 | + { |
|
| 114 | + self::_set_hooks_for_changes(); |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + |
|
| 118 | + /** |
|
| 119 | + * Loads all the hooks which make requests to old versions of the API |
|
| 120 | + * appear the same as they always did |
|
| 121 | + * |
|
| 122 | + * @throws EE_Error |
|
| 123 | + */ |
|
| 124 | + protected static function _set_hooks_for_changes() |
|
| 125 | + { |
|
| 126 | + $folder_contents = EEH_File::get_contents_of_folders(array(EE_LIBRARIES . 'rest_api' . DS . 'changes'), false); |
|
| 127 | + foreach ($folder_contents as $classname_in_namespace => $filepath) { |
|
| 128 | + // ignore the base parent class |
|
| 129 | + // and legacy named classes |
|
| 130 | + if ($classname_in_namespace === 'ChangesInBase' |
|
| 131 | + || strpos($classname_in_namespace, 'Changes_In_') === 0 |
|
| 132 | + ) { |
|
| 133 | + continue; |
|
| 134 | + } |
|
| 135 | + $full_classname = 'EventEspresso\core\libraries\rest_api\changes\\' . $classname_in_namespace; |
|
| 136 | + if (class_exists($full_classname)) { |
|
| 137 | + $instance_of_class = new $full_classname; |
|
| 138 | + if ($instance_of_class instanceof ChangesInBase) { |
|
| 139 | + $instance_of_class->setHooks(); |
|
| 140 | + } |
|
| 141 | + } |
|
| 142 | + } |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + |
|
| 146 | + /** |
|
| 147 | + * Filters the WP routes to add our EE-related ones. This takes a bit of time |
|
| 148 | + * so we actually prefer to only do it when an EE plugin is activated or upgraded |
|
| 149 | + * |
|
| 150 | + * @throws \EE_Error |
|
| 151 | + */ |
|
| 152 | + public static function register_routes() |
|
| 153 | + { |
|
| 154 | + foreach (EED_Core_Rest_Api::get_ee_route_data() as $namespace => $relative_routes) { |
|
| 155 | + foreach ($relative_routes as $relative_route => $data_for_multiple_endpoints) { |
|
| 156 | + /** |
|
| 157 | + * @var array $data_for_multiple_endpoints numerically indexed array |
|
| 158 | + * but can also contain route options like { |
|
| 159 | + * @type array $schema { |
|
| 160 | + * @type callable $schema_callback |
|
| 161 | + * @type array $callback_args arguments that will be passed to the callback, after the |
|
| 162 | + * WP_REST_Request of course |
|
| 163 | + * } |
|
| 164 | + * } |
|
| 165 | + */ |
|
| 166 | + // when registering routes, register all the endpoints' data at the same time |
|
| 167 | + $multiple_endpoint_args = array(); |
|
| 168 | + foreach ($data_for_multiple_endpoints as $endpoint_key => $data_for_single_endpoint) { |
|
| 169 | + /** |
|
| 170 | + * @var array $data_for_single_endpoint { |
|
| 171 | + * @type callable $callback |
|
| 172 | + * @type string methods |
|
| 173 | + * @type array args |
|
| 174 | + * @type array _links |
|
| 175 | + * @type array $callback_args arguments that will be passed to the callback, after the |
|
| 176 | + * WP_REST_Request of course |
|
| 177 | + * } |
|
| 178 | + */ |
|
| 179 | + // skip route options |
|
| 180 | + if (! is_numeric($endpoint_key)) { |
|
| 181 | + continue; |
|
| 182 | + } |
|
| 183 | + if (! isset($data_for_single_endpoint['callback'], $data_for_single_endpoint['methods'])) { |
|
| 184 | + throw new EE_Error( |
|
| 185 | + esc_html__( |
|
| 186 | + // @codingStandardsIgnoreStart |
|
| 187 | + 'Endpoint configuration data needs to have entries "callback" (callable) and "methods" (comma-separated list of accepts HTTP methods).', |
|
| 188 | + // @codingStandardsIgnoreEnd |
|
| 189 | + 'event_espresso' |
|
| 190 | + ) |
|
| 191 | + ); |
|
| 192 | + } |
|
| 193 | + $callback = $data_for_single_endpoint['callback']; |
|
| 194 | + $single_endpoint_args = array( |
|
| 195 | + 'methods' => $data_for_single_endpoint['methods'], |
|
| 196 | + 'args' => isset($data_for_single_endpoint['args']) ? $data_for_single_endpoint['args'] |
|
| 197 | + : array(), |
|
| 198 | + ); |
|
| 199 | + if (isset($data_for_single_endpoint['_links'])) { |
|
| 200 | + $single_endpoint_args['_links'] = $data_for_single_endpoint['_links']; |
|
| 201 | + } |
|
| 202 | + if (isset($data_for_single_endpoint['callback_args'])) { |
|
| 203 | + $callback_args = $data_for_single_endpoint['callback_args']; |
|
| 204 | + $single_endpoint_args['callback'] = function (\WP_REST_Request $request) use ( |
|
| 205 | + $callback, |
|
| 206 | + $callback_args |
|
| 207 | + ) { |
|
| 208 | + array_unshift($callback_args, $request); |
|
| 209 | + return call_user_func_array( |
|
| 210 | + $callback, |
|
| 211 | + $callback_args |
|
| 212 | + ); |
|
| 213 | + }; |
|
| 214 | + } else { |
|
| 215 | + $single_endpoint_args['callback'] = $data_for_single_endpoint['callback']; |
|
| 216 | + } |
|
| 217 | + $multiple_endpoint_args[] = $single_endpoint_args; |
|
| 218 | + } |
|
| 219 | + if (isset($data_for_multiple_endpoints['schema'])) { |
|
| 220 | + $schema_route_data = $data_for_multiple_endpoints['schema']; |
|
| 221 | + $schema_callback = $schema_route_data['schema_callback']; |
|
| 222 | + $callback_args = $schema_route_data['callback_args']; |
|
| 223 | + $multiple_endpoint_args['schema'] = function () use ($schema_callback, $callback_args) { |
|
| 224 | + return call_user_func_array( |
|
| 225 | + $schema_callback, |
|
| 226 | + $callback_args |
|
| 227 | + ); |
|
| 228 | + }; |
|
| 229 | + } |
|
| 230 | + register_rest_route( |
|
| 231 | + $namespace, |
|
| 232 | + $relative_route, |
|
| 233 | + $multiple_endpoint_args |
|
| 234 | + ); |
|
| 235 | + } |
|
| 236 | + } |
|
| 237 | + } |
|
| 238 | + |
|
| 239 | + |
|
| 240 | + /** |
|
| 241 | + * Checks if there was a version change or something that merits invalidating the cached |
|
| 242 | + * route data. If so, invalidates the cached route data so that it gets refreshed |
|
| 243 | + * next time the WP API is used |
|
| 244 | + */ |
|
| 245 | + public static function invalidate_cached_route_data_on_version_change() |
|
| 246 | + { |
|
| 247 | + if (EE_System::instance()->detect_req_type() !== EE_System::req_type_normal) { |
|
| 248 | + EED_Core_Rest_Api::invalidate_cached_route_data(); |
|
| 249 | + } |
|
| 250 | + foreach (EE_Registry::instance()->addons as $addon) { |
|
| 251 | + if ($addon instanceof EE_Addon && $addon->detect_req_type() !== EE_System::req_type_normal) { |
|
| 252 | + EED_Core_Rest_Api::invalidate_cached_route_data(); |
|
| 253 | + } |
|
| 254 | + } |
|
| 255 | + } |
|
| 256 | + |
|
| 257 | + |
|
| 258 | + /** |
|
| 259 | + * Removes the cached route data so it will get refreshed next time the WP API is used |
|
| 260 | + */ |
|
| 261 | + public static function invalidate_cached_route_data() |
|
| 262 | + { |
|
| 263 | + // delete the saved EE REST API routes |
|
| 264 | + foreach (EED_Core_Rest_Api::versions_served() as $version => $hidden) { |
|
| 265 | + delete_option(EED_Core_Rest_Api::saved_routes_option_names . $version); |
|
| 266 | + } |
|
| 267 | + } |
|
| 268 | + |
|
| 269 | + |
|
| 270 | + /** |
|
| 271 | + * Gets the EE route data |
|
| 272 | + * |
|
| 273 | + * @return array top-level key is the namespace, next-level key is the route and its value is array{ |
|
| 274 | + * @throws \EE_Error |
|
| 275 | + * @type string|array $callback |
|
| 276 | + * @type string $methods |
|
| 277 | + * @type boolean $hidden_endpoint |
|
| 278 | + * } |
|
| 279 | + */ |
|
| 280 | + public static function get_ee_route_data() |
|
| 281 | + { |
|
| 282 | + $ee_routes = array(); |
|
| 283 | + foreach (self::versions_served() as $version => $hidden_endpoints) { |
|
| 284 | + $ee_routes[ self::ee_api_namespace . $version ] = self::_get_ee_route_data_for_version( |
|
| 285 | + $version, |
|
| 286 | + $hidden_endpoints |
|
| 287 | + ); |
|
| 288 | + } |
|
| 289 | + return $ee_routes; |
|
| 290 | + } |
|
| 291 | + |
|
| 292 | + |
|
| 293 | + /** |
|
| 294 | + * Gets the EE route data from the wp options if it exists already, |
|
| 295 | + * otherwise re-generates it and saves it to the option |
|
| 296 | + * |
|
| 297 | + * @param string $version |
|
| 298 | + * @param boolean $hidden_endpoints |
|
| 299 | + * @return array |
|
| 300 | + * @throws \EE_Error |
|
| 301 | + */ |
|
| 302 | + protected static function _get_ee_route_data_for_version($version, $hidden_endpoints = false) |
|
| 303 | + { |
|
| 304 | + $ee_routes = get_option(self::saved_routes_option_names . $version, null); |
|
| 305 | + if (! $ee_routes || (defined('EE_REST_API_DEBUG_MODE') && EE_REST_API_DEBUG_MODE)) { |
|
| 306 | + $ee_routes = self::_save_ee_route_data_for_version($version, $hidden_endpoints); |
|
| 307 | + } |
|
| 308 | + return $ee_routes; |
|
| 309 | + } |
|
| 310 | + |
|
| 311 | + |
|
| 312 | + /** |
|
| 313 | + * Saves the EE REST API route data to a wp option and returns it |
|
| 314 | + * |
|
| 315 | + * @param string $version |
|
| 316 | + * @param boolean $hidden_endpoints |
|
| 317 | + * @return mixed|null |
|
| 318 | + * @throws \EE_Error |
|
| 319 | + */ |
|
| 320 | + protected static function _save_ee_route_data_for_version($version, $hidden_endpoints = false) |
|
| 321 | + { |
|
| 322 | + $instance = self::instance(); |
|
| 323 | + $routes = apply_filters( |
|
| 324 | + 'EED_Core_Rest_Api__save_ee_route_data_for_version__routes', |
|
| 325 | + array_replace_recursive( |
|
| 326 | + $instance->_get_config_route_data_for_version($version, $hidden_endpoints), |
|
| 327 | + $instance->_get_meta_route_data_for_version($version, $hidden_endpoints), |
|
| 328 | + $instance->_get_model_route_data_for_version($version, $hidden_endpoints), |
|
| 329 | + $instance->_get_rpc_route_data_for_version($version, $hidden_endpoints) |
|
| 330 | + ) |
|
| 331 | + ); |
|
| 332 | + $option_name = self::saved_routes_option_names . $version; |
|
| 333 | + if (get_option($option_name)) { |
|
| 334 | + update_option($option_name, $routes, true); |
|
| 335 | + } else { |
|
| 336 | + add_option($option_name, $routes, null, 'no'); |
|
| 337 | + } |
|
| 338 | + return $routes; |
|
| 339 | + } |
|
| 340 | + |
|
| 341 | + |
|
| 342 | + /** |
|
| 343 | + * Calculates all the EE routes and saves it to a WordPress option so we don't |
|
| 344 | + * need to calculate it on every request |
|
| 345 | + * |
|
| 346 | + * @deprecated since version 4.9.1 |
|
| 347 | + * @return void |
|
| 348 | + */ |
|
| 349 | + public static function save_ee_routes() |
|
| 350 | + { |
|
| 351 | + if (EE_Maintenance_Mode::instance()->models_can_query()) { |
|
| 352 | + $instance = self::instance(); |
|
| 353 | + $routes = apply_filters( |
|
| 354 | + 'EED_Core_Rest_Api__save_ee_routes__routes', |
|
| 355 | + array_replace_recursive( |
|
| 356 | + $instance->_register_config_routes(), |
|
| 357 | + $instance->_register_meta_routes(), |
|
| 358 | + $instance->_register_model_routes(), |
|
| 359 | + $instance->_register_rpc_routes() |
|
| 360 | + ) |
|
| 361 | + ); |
|
| 362 | + update_option(self::saved_routes_option_names, $routes, true); |
|
| 363 | + } |
|
| 364 | + } |
|
| 365 | + |
|
| 366 | + |
|
| 367 | + /** |
|
| 368 | + * Gets all the route information relating to EE models |
|
| 369 | + * |
|
| 370 | + * @return array @see get_ee_route_data |
|
| 371 | + * @deprecated since version 4.9.1 |
|
| 372 | + */ |
|
| 373 | + protected function _register_model_routes() |
|
| 374 | + { |
|
| 375 | + $model_routes = array(); |
|
| 376 | + foreach (self::versions_served() as $version => $hidden_endpoint) { |
|
| 377 | + $model_routes[ EED_Core_Rest_Api::ee_api_namespace |
|
| 378 | + . $version ] = $this->_get_config_route_data_for_version($version, $hidden_endpoint); |
|
| 379 | + } |
|
| 380 | + return $model_routes; |
|
| 381 | + } |
|
| 382 | + |
|
| 383 | + |
|
| 384 | + /** |
|
| 385 | + * Decides whether or not to add write endpoints for this model. |
|
| 386 | + * |
|
| 387 | + * Currently, this defaults to exclude all global tables and models |
|
| 388 | + * which would allow inserting WP core data (we don't want to duplicate |
|
| 389 | + * what WP API does, as it's unnecessary, extra work, and potentially extra bugs) |
|
| 390 | + * |
|
| 391 | + * @param EEM_Base $model |
|
| 392 | + * @return bool |
|
| 393 | + */ |
|
| 394 | + public static function should_have_write_endpoints(EEM_Base $model) |
|
| 395 | + { |
|
| 396 | + if ($model->is_wp_core_model()) { |
|
| 397 | + return false; |
|
| 398 | + } |
|
| 399 | + foreach ($model->get_tables() as $table) { |
|
| 400 | + if ($table->is_global()) { |
|
| 401 | + return false; |
|
| 402 | + } |
|
| 403 | + } |
|
| 404 | + return true; |
|
| 405 | + } |
|
| 406 | + |
|
| 407 | + |
|
| 408 | + /** |
|
| 409 | + * Gets the names of all models which should have plural routes (eg `ee/v4.8.36/events`) |
|
| 410 | + * in this versioned namespace of EE4 |
|
| 411 | + * |
|
| 412 | + * @param $version |
|
| 413 | + * @return array keys are model names (eg 'Event') and values ar either classnames (eg 'EEM_Event') |
|
| 414 | + */ |
|
| 415 | + public static function model_names_with_plural_routes($version) |
|
| 416 | + { |
|
| 417 | + $model_version_info = new ModelVersionInfo($version); |
|
| 418 | + $models_to_register = $model_version_info->modelsForRequestedVersion(); |
|
| 419 | + // let's not bother having endpoints for extra metas |
|
| 420 | + unset( |
|
| 421 | + $models_to_register['Extra_Meta'], |
|
| 422 | + $models_to_register['Extra_Join'], |
|
| 423 | + $models_to_register['Post_Meta'] |
|
| 424 | + ); |
|
| 425 | + return apply_filters( |
|
| 426 | + 'FHEE__EED_Core_REST_API___register_model_routes', |
|
| 427 | + $models_to_register |
|
| 428 | + ); |
|
| 429 | + } |
|
| 430 | + |
|
| 431 | + |
|
| 432 | + /** |
|
| 433 | + * Gets the route data for EE models in the specified version |
|
| 434 | + * |
|
| 435 | + * @param string $version |
|
| 436 | + * @param boolean $hidden_endpoint |
|
| 437 | + * @return array |
|
| 438 | + * @throws EE_Error |
|
| 439 | + */ |
|
| 440 | + protected function _get_model_route_data_for_version($version, $hidden_endpoint = false) |
|
| 441 | + { |
|
| 442 | + $model_routes = array(); |
|
| 443 | + $model_version_info = new ModelVersionInfo($version); |
|
| 444 | + foreach (EED_Core_Rest_Api::model_names_with_plural_routes($version) as $model_name => $model_classname) { |
|
| 445 | + $model = \EE_Registry::instance()->load_model($model_name); |
|
| 446 | + // if this isn't a valid model then let's skip iterate to the next item in the loop. |
|
| 447 | + if (! $model instanceof EEM_Base) { |
|
| 448 | + continue; |
|
| 449 | + } |
|
| 450 | + // yes we could just register one route for ALL models, but then they wouldn't show up in the index |
|
| 451 | + $plural_model_route = EED_Core_Rest_Api::get_collection_route($model); |
|
| 452 | + $singular_model_route = EED_Core_Rest_Api::get_entity_route($model, '(?P<id>[^\/]+)'); |
|
| 453 | + $model_routes[ $plural_model_route ] = array( |
|
| 454 | + array( |
|
| 455 | + 'callback' => array( |
|
| 456 | + 'EventEspresso\core\libraries\rest_api\controllers\model\Read', |
|
| 457 | + 'handleRequestGetAll', |
|
| 458 | + ), |
|
| 459 | + 'callback_args' => array($version, $model_name), |
|
| 460 | + 'methods' => WP_REST_Server::READABLE, |
|
| 461 | + 'hidden_endpoint' => $hidden_endpoint, |
|
| 462 | + 'args' => $this->_get_read_query_params($model, $version), |
|
| 463 | + '_links' => array( |
|
| 464 | + 'self' => rest_url(EED_Core_Rest_Api::ee_api_namespace . $version . $singular_model_route), |
|
| 465 | + ), |
|
| 466 | + ), |
|
| 467 | + 'schema' => array( |
|
| 468 | + 'schema_callback' => array( |
|
| 469 | + 'EventEspresso\core\libraries\rest_api\controllers\model\Read', |
|
| 470 | + 'handleSchemaRequest', |
|
| 471 | + ), |
|
| 472 | + 'callback_args' => array($version, $model_name), |
|
| 473 | + ), |
|
| 474 | + ); |
|
| 475 | + $model_routes[ $singular_model_route ] = array( |
|
| 476 | + array( |
|
| 477 | + 'callback' => array( |
|
| 478 | + 'EventEspresso\core\libraries\rest_api\controllers\model\Read', |
|
| 479 | + 'handleRequestGetOne', |
|
| 480 | + ), |
|
| 481 | + 'callback_args' => array($version, $model_name), |
|
| 482 | + 'methods' => WP_REST_Server::READABLE, |
|
| 483 | + 'hidden_endpoint' => $hidden_endpoint, |
|
| 484 | + 'args' => $this->_get_response_selection_query_params($model, $version), |
|
| 485 | + ), |
|
| 486 | + ); |
|
| 487 | + if (apply_filters( |
|
| 488 | + 'FHEE__EED_Core_Rest_Api___get_model_route_data_for_version__add_write_endpoints', |
|
| 489 | + EED_Core_Rest_Api::should_have_write_endpoints($model), |
|
| 490 | + $model |
|
| 491 | + )) { |
|
| 492 | + $model_routes[ $plural_model_route ][] = array( |
|
| 493 | + 'callback' => array( |
|
| 494 | + 'EventEspresso\core\libraries\rest_api\controllers\model\Write', |
|
| 495 | + 'handleRequestInsert', |
|
| 496 | + ), |
|
| 497 | + 'callback_args' => array($version, $model_name), |
|
| 498 | + 'methods' => WP_REST_Server::CREATABLE, |
|
| 499 | + 'hidden_endpoint' => $hidden_endpoint, |
|
| 500 | + 'args' => $this->_get_write_params($model_name, $model_version_info, true), |
|
| 501 | + ); |
|
| 502 | + $model_routes[ $singular_model_route ] = array_merge( |
|
| 503 | + $model_routes[ $singular_model_route ], |
|
| 504 | + array( |
|
| 505 | + array( |
|
| 506 | + 'callback' => array( |
|
| 507 | + 'EventEspresso\core\libraries\rest_api\controllers\model\Write', |
|
| 508 | + 'handleRequestUpdate', |
|
| 509 | + ), |
|
| 510 | + 'callback_args' => array($version, $model_name), |
|
| 511 | + 'methods' => WP_REST_Server::EDITABLE, |
|
| 512 | + 'hidden_endpoint' => $hidden_endpoint, |
|
| 513 | + 'args' => $this->_get_write_params($model_name, $model_version_info), |
|
| 514 | + ), |
|
| 515 | + array( |
|
| 516 | + 'callback' => array( |
|
| 517 | + 'EventEspresso\core\libraries\rest_api\controllers\model\Write', |
|
| 518 | + 'handleRequestDelete', |
|
| 519 | + ), |
|
| 520 | + 'callback_args' => array($version, $model_name), |
|
| 521 | + 'methods' => WP_REST_Server::DELETABLE, |
|
| 522 | + 'hidden_endpoint' => $hidden_endpoint, |
|
| 523 | + 'args' => $this->_get_delete_query_params($model, $version), |
|
| 524 | + ), |
|
| 525 | + ) |
|
| 526 | + ); |
|
| 527 | + } |
|
| 528 | + foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
|
| 529 | + $related_route = EED_Core_Rest_Api::get_relation_route_via( |
|
| 530 | + $model, |
|
| 531 | + '(?P<id>[^\/]+)', |
|
| 532 | + $relation_obj |
|
| 533 | + ); |
|
| 534 | + $endpoints = array( |
|
| 535 | + array( |
|
| 536 | + 'callback' => array( |
|
| 537 | + 'EventEspresso\core\libraries\rest_api\controllers\model\Read', |
|
| 538 | + 'handleRequestGetRelated', |
|
| 539 | + ), |
|
| 540 | + 'callback_args' => array($version, $model_name, $relation_name), |
|
| 541 | + 'methods' => WP_REST_Server::READABLE, |
|
| 542 | + 'hidden_endpoint' => $hidden_endpoint, |
|
| 543 | + 'args' => $this->_get_read_query_params($relation_obj->get_other_model(), $version), |
|
| 544 | + ), |
|
| 545 | + ); |
|
| 546 | + $model_routes[ $related_route ] = $endpoints; |
|
| 547 | + } |
|
| 548 | + } |
|
| 549 | + return $model_routes; |
|
| 550 | + } |
|
| 551 | + |
|
| 552 | + |
|
| 553 | + /** |
|
| 554 | + * Gets the relative URI to a model's REST API plural route, after the EE4 versioned namespace, |
|
| 555 | + * excluding the preceding slash. |
|
| 556 | + * Eg you pass get_plural_route_to('Event') = 'events' |
|
| 557 | + * |
|
| 558 | + * @param EEM_Base $model |
|
| 559 | + * @return string |
|
| 560 | + */ |
|
| 561 | + public static function get_collection_route(EEM_Base $model) |
|
| 562 | + { |
|
| 563 | + return EEH_Inflector::pluralize_and_lower($model->get_this_model_name()); |
|
| 564 | + } |
|
| 565 | + |
|
| 566 | + |
|
| 567 | + /** |
|
| 568 | + * Gets the relative URI to a model's REST API singular route, after the EE4 versioned namespace, |
|
| 569 | + * excluding the preceding slash. |
|
| 570 | + * Eg you pass get_plural_route_to('Event', 12) = 'events/12' |
|
| 571 | + * |
|
| 572 | + * @param EEM_Base $model eg Event or Venue |
|
| 573 | + * @param string $id |
|
| 574 | + * @return string |
|
| 575 | + */ |
|
| 576 | + public static function get_entity_route($model, $id) |
|
| 577 | + { |
|
| 578 | + return EED_Core_Rest_Api::get_collection_route($model) . '/' . $id; |
|
| 579 | + } |
|
| 580 | + |
|
| 581 | + |
|
| 582 | + /** |
|
| 583 | + * Gets the relative URI to a model's REST API singular route, after the EE4 versioned namespace, |
|
| 584 | + * excluding the preceding slash. |
|
| 585 | + * Eg you pass get_plural_route_to('Event', 12) = 'events/12' |
|
| 586 | + * |
|
| 587 | + * @param EEM_Base $model eg Event or Venue |
|
| 588 | + * @param string $id |
|
| 589 | + * @param EE_Model_Relation_Base $relation_obj |
|
| 590 | + * @return string |
|
| 591 | + */ |
|
| 592 | + public static function get_relation_route_via(EEM_Base $model, $id, EE_Model_Relation_Base $relation_obj) |
|
| 593 | + { |
|
| 594 | + $related_model_name_endpoint_part = ModelRead::getRelatedEntityName( |
|
| 595 | + $relation_obj->get_other_model()->get_this_model_name(), |
|
| 596 | + $relation_obj |
|
| 597 | + ); |
|
| 598 | + return EED_Core_Rest_Api::get_entity_route($model, $id) . '/' . $related_model_name_endpoint_part; |
|
| 599 | + } |
|
| 600 | + |
|
| 601 | + |
|
| 602 | + /** |
|
| 603 | + * Adds onto the $relative_route the EE4 REST API versioned namespace. |
|
| 604 | + * Eg if given '4.8.36' and 'events', will return 'ee/v4.8.36/events' |
|
| 605 | + * |
|
| 606 | + * @param string $relative_route |
|
| 607 | + * @param string $version |
|
| 608 | + * @return string |
|
| 609 | + */ |
|
| 610 | + public static function get_versioned_route_to($relative_route, $version = '4.8.36') |
|
| 611 | + { |
|
| 612 | + return '/' . EED_Core_Rest_Api::ee_api_namespace . $version . '/' . $relative_route; |
|
| 613 | + } |
|
| 614 | + |
|
| 615 | + |
|
| 616 | + /** |
|
| 617 | + * Adds all the RPC-style routes (remote procedure call-like routes, ie |
|
| 618 | + * routes that don't conform to the traditional REST CRUD-style). |
|
| 619 | + * |
|
| 620 | + * @deprecated since 4.9.1 |
|
| 621 | + */ |
|
| 622 | + protected function _register_rpc_routes() |
|
| 623 | + { |
|
| 624 | + $routes = array(); |
|
| 625 | + foreach (self::versions_served() as $version => $hidden_endpoint) { |
|
| 626 | + $routes[ self::ee_api_namespace . $version ] = $this->_get_rpc_route_data_for_version( |
|
| 627 | + $version, |
|
| 628 | + $hidden_endpoint |
|
| 629 | + ); |
|
| 630 | + } |
|
| 631 | + return $routes; |
|
| 632 | + } |
|
| 633 | + |
|
| 634 | + |
|
| 635 | + /** |
|
| 636 | + * @param string $version |
|
| 637 | + * @param boolean $hidden_endpoint |
|
| 638 | + * @return array |
|
| 639 | + */ |
|
| 640 | + protected function _get_rpc_route_data_for_version($version, $hidden_endpoint = false) |
|
| 641 | + { |
|
| 642 | + $this_versions_routes = array(); |
|
| 643 | + // checkin endpoint |
|
| 644 | + $this_versions_routes['registrations/(?P<REG_ID>\d+)/toggle_checkin_for_datetime/(?P<DTT_ID>\d+)'] = array( |
|
| 645 | + array( |
|
| 646 | + 'callback' => array( |
|
| 647 | + 'EventEspresso\core\libraries\rest_api\controllers\rpc\Checkin', |
|
| 648 | + 'handleRequestToggleCheckin', |
|
| 649 | + ), |
|
| 650 | + 'methods' => WP_REST_Server::CREATABLE, |
|
| 651 | + 'hidden_endpoint' => $hidden_endpoint, |
|
| 652 | + 'args' => array( |
|
| 653 | + 'force' => array( |
|
| 654 | + 'required' => false, |
|
| 655 | + 'default' => false, |
|
| 656 | + 'description' => __( |
|
| 657 | + // @codingStandardsIgnoreStart |
|
| 658 | + 'Whether to force toggle checkin, or to verify the registration status and allowed ticket uses', |
|
| 659 | + // @codingStandardsIgnoreEnd |
|
| 660 | + 'event_espresso' |
|
| 661 | + ), |
|
| 662 | + ), |
|
| 663 | + ), |
|
| 664 | + 'callback_args' => array($version), |
|
| 665 | + ), |
|
| 666 | + ); |
|
| 667 | + return apply_filters( |
|
| 668 | + 'FHEE__EED_Core_Rest_Api___register_rpc_routes__this_versions_routes', |
|
| 669 | + $this_versions_routes, |
|
| 670 | + $version, |
|
| 671 | + $hidden_endpoint |
|
| 672 | + ); |
|
| 673 | + } |
|
| 674 | + |
|
| 675 | + |
|
| 676 | + /** |
|
| 677 | + * Gets the query params that can be used when request one or many |
|
| 678 | + * |
|
| 679 | + * @param EEM_Base $model |
|
| 680 | + * @param string $version |
|
| 681 | + * @return array |
|
| 682 | + */ |
|
| 683 | + protected function _get_response_selection_query_params(\EEM_Base $model, $version) |
|
| 684 | + { |
|
| 685 | + return apply_filters( |
|
| 686 | + 'FHEE__EED_Core_Rest_Api___get_response_selection_query_params', |
|
| 687 | + array( |
|
| 688 | + 'include' => array( |
|
| 689 | + 'required' => false, |
|
| 690 | + 'default' => '*', |
|
| 691 | + 'type' => 'string', |
|
| 692 | + ), |
|
| 693 | + 'calculate' => array( |
|
| 694 | + 'required' => false, |
|
| 695 | + 'default' => '', |
|
| 696 | + 'enum' => self::$_field_calculator->retrieveCalculatedFieldsForModel($model), |
|
| 697 | + 'type' => 'string', |
|
| 698 | + // because we accept a CSV'd list of the enumerated strings, WP core validation and sanitization |
|
| 699 | + // freaks out. We'll just validate this argument while handling the request |
|
| 700 | + 'validate_callback' => null, |
|
| 701 | + 'sanitize_callback' => null, |
|
| 702 | + ), |
|
| 703 | + ), |
|
| 704 | + $model, |
|
| 705 | + $version |
|
| 706 | + ); |
|
| 707 | + } |
|
| 708 | + |
|
| 709 | + |
|
| 710 | + /** |
|
| 711 | + * Gets the parameters acceptable for delete requests |
|
| 712 | + * |
|
| 713 | + * @param \EEM_Base $model |
|
| 714 | + * @param string $version |
|
| 715 | + * @return array |
|
| 716 | + */ |
|
| 717 | + protected function _get_delete_query_params(\EEM_Base $model, $version) |
|
| 718 | + { |
|
| 719 | + $params_for_delete = array( |
|
| 720 | + 'allow_blocking' => array( |
|
| 721 | + 'required' => false, |
|
| 722 | + 'default' => true, |
|
| 723 | + 'type' => 'boolean', |
|
| 724 | + ), |
|
| 725 | + ); |
|
| 726 | + $params_for_delete['force'] = array( |
|
| 727 | + 'required' => false, |
|
| 728 | + 'default' => false, |
|
| 729 | + 'type' => 'boolean', |
|
| 730 | + ); |
|
| 731 | + return apply_filters( |
|
| 732 | + 'FHEE__EED_Core_Rest_Api___get_delete_query_params', |
|
| 733 | + $params_for_delete, |
|
| 734 | + $model, |
|
| 735 | + $version |
|
| 736 | + ); |
|
| 737 | + } |
|
| 738 | + |
|
| 739 | + |
|
| 740 | + /** |
|
| 741 | + * Gets info about reading query params that are acceptable |
|
| 742 | + * |
|
| 743 | + * @param \EEM_Base $model eg 'Event' or 'Venue' |
|
| 744 | + * @param string $version |
|
| 745 | + * @return array describing the args acceptable when querying this model |
|
| 746 | + * @throws EE_Error |
|
| 747 | + */ |
|
| 748 | + protected function _get_read_query_params(\EEM_Base $model, $version) |
|
| 749 | + { |
|
| 750 | + $default_orderby = array(); |
|
| 751 | + foreach ($model->get_combined_primary_key_fields() as $key_field) { |
|
| 752 | + $default_orderby[ $key_field->get_name() ] = 'ASC'; |
|
| 753 | + } |
|
| 754 | + return array_merge( |
|
| 755 | + $this->_get_response_selection_query_params($model, $version), |
|
| 756 | + array( |
|
| 757 | + 'where' => array( |
|
| 758 | + 'required' => false, |
|
| 759 | + 'default' => array(), |
|
| 760 | + 'type' => 'object', |
|
| 761 | + // because we accept an almost infinite list of possible where conditions, WP |
|
| 762 | + // core validation and sanitization freaks out. We'll just validate this argument |
|
| 763 | + // while handling the request |
|
| 764 | + 'validate_callback' => null, |
|
| 765 | + 'sanitize_callback' => null, |
|
| 766 | + ), |
|
| 767 | + 'limit' => array( |
|
| 768 | + 'required' => false, |
|
| 769 | + 'default' => EED_Core_Rest_Api::get_default_query_limit(), |
|
| 770 | + 'type' => array( |
|
| 771 | + 'array', |
|
| 772 | + 'string', |
|
| 773 | + 'integer', |
|
| 774 | + ), |
|
| 775 | + // because we accept a variety of types, WP core validation and sanitization |
|
| 776 | + // freaks out. We'll just validate this argument while handling the request |
|
| 777 | + 'validate_callback' => null, |
|
| 778 | + 'sanitize_callback' => null, |
|
| 779 | + ), |
|
| 780 | + 'order_by' => array( |
|
| 781 | + 'required' => false, |
|
| 782 | + 'default' => $default_orderby, |
|
| 783 | + 'type' => array( |
|
| 784 | + 'object', |
|
| 785 | + 'string', |
|
| 786 | + ),// because we accept a variety of types, WP core validation and sanitization |
|
| 787 | + // freaks out. We'll just validate this argument while handling the request |
|
| 788 | + 'validate_callback' => null, |
|
| 789 | + 'sanitize_callback' => null, |
|
| 790 | + ), |
|
| 791 | + 'group_by' => array( |
|
| 792 | + 'required' => false, |
|
| 793 | + 'default' => null, |
|
| 794 | + 'type' => array( |
|
| 795 | + 'object', |
|
| 796 | + 'string', |
|
| 797 | + ), |
|
| 798 | + // because we accept an almost infinite list of possible groupings, |
|
| 799 | + // WP core validation and sanitization |
|
| 800 | + // freaks out. We'll just validate this argument while handling the request |
|
| 801 | + 'validate_callback' => null, |
|
| 802 | + 'sanitize_callback' => null, |
|
| 803 | + ), |
|
| 804 | + 'having' => array( |
|
| 805 | + 'required' => false, |
|
| 806 | + 'default' => null, |
|
| 807 | + 'type' => 'object', |
|
| 808 | + // because we accept an almost infinite list of possible where conditions, WP |
|
| 809 | + // core validation and sanitization freaks out. We'll just validate this argument |
|
| 810 | + // while handling the request |
|
| 811 | + 'validate_callback' => null, |
|
| 812 | + 'sanitize_callback' => null, |
|
| 813 | + ), |
|
| 814 | + 'caps' => array( |
|
| 815 | + 'required' => false, |
|
| 816 | + 'default' => EEM_Base::caps_read, |
|
| 817 | + 'type' => 'string', |
|
| 818 | + 'enum' => array( |
|
| 819 | + EEM_Base::caps_read, |
|
| 820 | + EEM_Base::caps_read_admin, |
|
| 821 | + EEM_Base::caps_edit, |
|
| 822 | + EEM_Base::caps_delete, |
|
| 823 | + ), |
|
| 824 | + ), |
|
| 825 | + ) |
|
| 826 | + ); |
|
| 827 | + } |
|
| 828 | + |
|
| 829 | + |
|
| 830 | + /** |
|
| 831 | + * Gets parameter information for a model regarding writing data |
|
| 832 | + * |
|
| 833 | + * @param string $model_name |
|
| 834 | + * @param ModelVersionInfo $model_version_info |
|
| 835 | + * @param boolean $create whether this is for request to create (in |
|
| 836 | + * which case we need all required params) or |
|
| 837 | + * just to update (in which case we don't |
|
| 838 | + * need those on every request) |
|
| 839 | + * @return array |
|
| 840 | + */ |
|
| 841 | + protected function _get_write_params( |
|
| 842 | + $model_name, |
|
| 843 | + ModelVersionInfo $model_version_info, |
|
| 844 | + $create = false |
|
| 845 | + ) { |
|
| 846 | + $model = EE_Registry::instance()->load_model($model_name); |
|
| 847 | + $fields = $model_version_info->fieldsOnModelInThisVersion($model); |
|
| 848 | + $args_info = array(); |
|
| 849 | + foreach ($fields as $field_name => $field_obj) { |
|
| 850 | + if ($field_obj->is_auto_increment()) { |
|
| 851 | + // totally ignore auto increment IDs |
|
| 852 | + continue; |
|
| 853 | + } |
|
| 854 | + $arg_info = $field_obj->getSchema(); |
|
| 855 | + $required = $create && ! $field_obj->is_nullable() && $field_obj->get_default_value() === null; |
|
| 856 | + $arg_info['required'] = $required; |
|
| 857 | + // remove the read-only flag. If it were read-only we wouldn't list it as an argument while writing, right? |
|
| 858 | + unset($arg_info['readonly']); |
|
| 859 | + $schema_properties = $field_obj->getSchemaProperties(); |
|
| 860 | + if (isset($schema_properties['raw']) |
|
| 861 | + && $field_obj->getSchemaType() === 'object' |
|
| 862 | + ) { |
|
| 863 | + // if there's a "raw" form of this argument, use those properties instead |
|
| 864 | + $arg_info = array_replace( |
|
| 865 | + $arg_info, |
|
| 866 | + $schema_properties['raw'] |
|
| 867 | + ); |
|
| 868 | + } |
|
| 869 | + $arg_info['default'] = ModelDataTranslator::prepareFieldValueForJson( |
|
| 870 | + $field_obj, |
|
| 871 | + $field_obj->get_default_value(), |
|
| 872 | + $model_version_info->requestedVersion() |
|
| 873 | + ); |
|
| 874 | + // we do our own validation and sanitization within the controller |
|
| 875 | + if (function_exists('rest_validate_value_from_schema')) { |
|
| 876 | + $sanitize_callback = array( |
|
| 877 | + 'EED_Core_Rest_Api', |
|
| 878 | + 'default_sanitize_callback', |
|
| 879 | + ); |
|
| 880 | + } else { |
|
| 881 | + $sanitize_callback = null; |
|
| 882 | + } |
|
| 883 | + $arg_info['sanitize_callback'] = $sanitize_callback; |
|
| 884 | + $args_info[ $field_name ] = $arg_info; |
|
| 885 | + if ($field_obj instanceof EE_Datetime_Field) { |
|
| 886 | + $gmt_arg_info = $arg_info; |
|
| 887 | + $gmt_arg_info['description'] = sprintf( |
|
| 888 | + esc_html__( |
|
| 889 | + '%1$s - the value for this field in UTC. Ignored if %2$s is provided.', |
|
| 890 | + 'event_espresso' |
|
| 891 | + ), |
|
| 892 | + $field_obj->get_nicename(), |
|
| 893 | + $field_name |
|
| 894 | + ); |
|
| 895 | + $args_info[ $field_name . '_gmt' ] = $gmt_arg_info; |
|
| 896 | + } |
|
| 897 | + } |
|
| 898 | + return $args_info; |
|
| 899 | + } |
|
| 900 | + |
|
| 901 | + |
|
| 902 | + /** |
|
| 903 | + * Replacement for WP API's 'rest_parse_request_arg'. |
|
| 904 | + * If the value is blank but not required, don't bother validating it. |
|
| 905 | + * Also, it uses our email validation instead of WP API's default. |
|
| 906 | + * |
|
| 907 | + * @param $value |
|
| 908 | + * @param WP_REST_Request $request |
|
| 909 | + * @param $param |
|
| 910 | + * @return bool|true|WP_Error |
|
| 911 | + * @throws InvalidArgumentException |
|
| 912 | + * @throws InvalidInterfaceException |
|
| 913 | + * @throws InvalidDataTypeException |
|
| 914 | + */ |
|
| 915 | + public static function default_sanitize_callback($value, WP_REST_Request $request, $param) |
|
| 916 | + { |
|
| 917 | + $attributes = $request->get_attributes(); |
|
| 918 | + if (! isset($attributes['args'][ $param ]) |
|
| 919 | + || ! is_array($attributes['args'][ $param ])) { |
|
| 920 | + $validation_result = true; |
|
| 921 | + } else { |
|
| 922 | + $args = $attributes['args'][ $param ]; |
|
| 923 | + if (( |
|
| 924 | + $value === '' |
|
| 925 | + || $value === null |
|
| 926 | + ) |
|
| 927 | + && (! isset($args['required']) |
|
| 928 | + || $args['required'] === false |
|
| 929 | + ) |
|
| 930 | + ) { |
|
| 931 | + // not required and not provided? that's cool |
|
| 932 | + $validation_result = true; |
|
| 933 | + } elseif (isset($args['format']) |
|
| 934 | + && $args['format'] === 'email' |
|
| 935 | + ) { |
|
| 936 | + $validation_result = true; |
|
| 937 | + if (! self::_validate_email($value)) { |
|
| 938 | + $validation_result = new WP_Error( |
|
| 939 | + 'rest_invalid_param', |
|
| 940 | + esc_html__( |
|
| 941 | + 'The email address is not valid or does not exist.', |
|
| 942 | + 'event_espresso' |
|
| 943 | + ) |
|
| 944 | + ); |
|
| 945 | + } |
|
| 946 | + } else { |
|
| 947 | + $validation_result = rest_validate_value_from_schema($value, $args, $param); |
|
| 948 | + } |
|
| 949 | + } |
|
| 950 | + if (is_wp_error($validation_result)) { |
|
| 951 | + return $validation_result; |
|
| 952 | + } |
|
| 953 | + return rest_sanitize_request_arg($value, $request, $param); |
|
| 954 | + } |
|
| 955 | + |
|
| 956 | + |
|
| 957 | + /** |
|
| 958 | + * Returns whether or not this email address is valid. Copied from EE_Email_Validation_Strategy::_validate_email() |
|
| 959 | + * |
|
| 960 | + * @param $email |
|
| 961 | + * @return bool |
|
| 962 | + * @throws InvalidArgumentException |
|
| 963 | + * @throws InvalidInterfaceException |
|
| 964 | + * @throws InvalidDataTypeException |
|
| 965 | + */ |
|
| 966 | + protected static function _validate_email($email) |
|
| 967 | + { |
|
| 968 | + try { |
|
| 969 | + EmailAddressFactory::create($email); |
|
| 970 | + return true; |
|
| 971 | + } catch (EmailValidationException $e) { |
|
| 972 | + return false; |
|
| 973 | + } |
|
| 974 | + } |
|
| 975 | + |
|
| 976 | + |
|
| 977 | + /** |
|
| 978 | + * Gets routes for the config |
|
| 979 | + * |
|
| 980 | + * @return array @see _register_model_routes |
|
| 981 | + * @deprecated since version 4.9.1 |
|
| 982 | + */ |
|
| 983 | + protected function _register_config_routes() |
|
| 984 | + { |
|
| 985 | + $config_routes = array(); |
|
| 986 | + foreach (self::versions_served() as $version => $hidden_endpoint) { |
|
| 987 | + $config_routes[ self::ee_api_namespace . $version ] = $this->_get_config_route_data_for_version( |
|
| 988 | + $version, |
|
| 989 | + $hidden_endpoint |
|
| 990 | + ); |
|
| 991 | + } |
|
| 992 | + return $config_routes; |
|
| 993 | + } |
|
| 994 | + |
|
| 995 | + |
|
| 996 | + /** |
|
| 997 | + * Gets routes for the config for the specified version |
|
| 998 | + * |
|
| 999 | + * @param string $version |
|
| 1000 | + * @param boolean $hidden_endpoint |
|
| 1001 | + * @return array |
|
| 1002 | + */ |
|
| 1003 | + protected function _get_config_route_data_for_version($version, $hidden_endpoint) |
|
| 1004 | + { |
|
| 1005 | + return array( |
|
| 1006 | + 'config' => array( |
|
| 1007 | + array( |
|
| 1008 | + 'callback' => array( |
|
| 1009 | + 'EventEspresso\core\libraries\rest_api\controllers\config\Read', |
|
| 1010 | + 'handleRequest', |
|
| 1011 | + ), |
|
| 1012 | + 'methods' => WP_REST_Server::READABLE, |
|
| 1013 | + 'hidden_endpoint' => $hidden_endpoint, |
|
| 1014 | + 'callback_args' => array($version), |
|
| 1015 | + ), |
|
| 1016 | + ), |
|
| 1017 | + 'site_info' => array( |
|
| 1018 | + array( |
|
| 1019 | + 'callback' => array( |
|
| 1020 | + 'EventEspresso\core\libraries\rest_api\controllers\config\Read', |
|
| 1021 | + 'handleRequestSiteInfo', |
|
| 1022 | + ), |
|
| 1023 | + 'methods' => WP_REST_Server::READABLE, |
|
| 1024 | + 'hidden_endpoint' => $hidden_endpoint, |
|
| 1025 | + 'callback_args' => array($version), |
|
| 1026 | + ), |
|
| 1027 | + ), |
|
| 1028 | + ); |
|
| 1029 | + } |
|
| 1030 | + |
|
| 1031 | + |
|
| 1032 | + /** |
|
| 1033 | + * Gets the meta info routes |
|
| 1034 | + * |
|
| 1035 | + * @return array @see _register_model_routes |
|
| 1036 | + * @deprecated since version 4.9.1 |
|
| 1037 | + */ |
|
| 1038 | + protected function _register_meta_routes() |
|
| 1039 | + { |
|
| 1040 | + $meta_routes = array(); |
|
| 1041 | + foreach (self::versions_served() as $version => $hidden_endpoint) { |
|
| 1042 | + $meta_routes[ self::ee_api_namespace . $version ] = $this->_get_meta_route_data_for_version( |
|
| 1043 | + $version, |
|
| 1044 | + $hidden_endpoint |
|
| 1045 | + ); |
|
| 1046 | + } |
|
| 1047 | + return $meta_routes; |
|
| 1048 | + } |
|
| 1049 | + |
|
| 1050 | + |
|
| 1051 | + /** |
|
| 1052 | + * @param string $version |
|
| 1053 | + * @param boolean $hidden_endpoint |
|
| 1054 | + * @return array |
|
| 1055 | + */ |
|
| 1056 | + protected function _get_meta_route_data_for_version($version, $hidden_endpoint = false) |
|
| 1057 | + { |
|
| 1058 | + return array( |
|
| 1059 | + 'resources' => array( |
|
| 1060 | + array( |
|
| 1061 | + 'callback' => array( |
|
| 1062 | + 'EventEspresso\core\libraries\rest_api\controllers\model\Meta', |
|
| 1063 | + 'handleRequestModelsMeta', |
|
| 1064 | + ), |
|
| 1065 | + 'methods' => WP_REST_Server::READABLE, |
|
| 1066 | + 'hidden_endpoint' => $hidden_endpoint, |
|
| 1067 | + 'callback_args' => array($version), |
|
| 1068 | + ), |
|
| 1069 | + ), |
|
| 1070 | + ); |
|
| 1071 | + } |
|
| 1072 | + |
|
| 1073 | + |
|
| 1074 | + /** |
|
| 1075 | + * Tries to hide old 4.6 endpoints from the |
|
| 1076 | + * |
|
| 1077 | + * @param array $route_data |
|
| 1078 | + * @return array |
|
| 1079 | + * @throws \EE_Error |
|
| 1080 | + */ |
|
| 1081 | + public static function hide_old_endpoints($route_data) |
|
| 1082 | + { |
|
| 1083 | + // allow API clients to override which endpoints get hidden, in case |
|
| 1084 | + // they want to discover particular endpoints |
|
| 1085 | + // also, we don't have access to the request so we have to just grab it from the superglobal |
|
| 1086 | + $force_show_ee_namespace = ltrim( |
|
| 1087 | + EEH_Array::is_set($_REQUEST, 'force_show_ee_namespace', ''), |
|
| 1088 | + '/' |
|
| 1089 | + ); |
|
| 1090 | + foreach (EED_Core_Rest_Api::get_ee_route_data() as $namespace => $relative_urls) { |
|
| 1091 | + foreach ($relative_urls as $resource_name => $endpoints) { |
|
| 1092 | + foreach ($endpoints as $key => $endpoint) { |
|
| 1093 | + // skip schema and other route options |
|
| 1094 | + if (! is_numeric($key)) { |
|
| 1095 | + continue; |
|
| 1096 | + } |
|
| 1097 | + // by default, hide "hidden_endpoint"s, unless the request indicates |
|
| 1098 | + // to $force_show_ee_namespace, in which case only show that one |
|
| 1099 | + // namespace's endpoints (and hide all others) |
|
| 1100 | + if (($force_show_ee_namespace !== '' && $force_show_ee_namespace !== $namespace) |
|
| 1101 | + || ($endpoint['hidden_endpoint'] && $force_show_ee_namespace === '') |
|
| 1102 | + ) { |
|
| 1103 | + $full_route = '/' . ltrim($namespace, '/'); |
|
| 1104 | + $full_route .= '/' . ltrim($resource_name, '/'); |
|
| 1105 | + unset($route_data[ $full_route ]); |
|
| 1106 | + } |
|
| 1107 | + } |
|
| 1108 | + } |
|
| 1109 | + } |
|
| 1110 | + return $route_data; |
|
| 1111 | + } |
|
| 1112 | + |
|
| 1113 | + |
|
| 1114 | + /** |
|
| 1115 | + * Returns an array describing which versions of core support serving requests for. |
|
| 1116 | + * Keys are core versions' major and minor version, and values are the |
|
| 1117 | + * LOWEST requested version they can serve. Eg, 4.7 can serve requests for 4.6-like |
|
| 1118 | + * data by just removing a few models and fields from the responses. However, 4.15 might remove |
|
| 1119 | + * the answers table entirely, in which case it would be very difficult for |
|
| 1120 | + * it to serve 4.6-style responses. |
|
| 1121 | + * Versions of core that are missing from this array are unknowns. |
|
| 1122 | + * previous ver |
|
| 1123 | + * |
|
| 1124 | + * @return array |
|
| 1125 | + */ |
|
| 1126 | + public static function version_compatibilities() |
|
| 1127 | + { |
|
| 1128 | + return apply_filters( |
|
| 1129 | + 'FHEE__EED_Core_REST_API__version_compatibilities', |
|
| 1130 | + array( |
|
| 1131 | + '4.8.29' => '4.8.29', |
|
| 1132 | + '4.8.33' => '4.8.29', |
|
| 1133 | + '4.8.34' => '4.8.29', |
|
| 1134 | + '4.8.36' => '4.8.29', |
|
| 1135 | + ) |
|
| 1136 | + ); |
|
| 1137 | + } |
|
| 1138 | + |
|
| 1139 | + |
|
| 1140 | + /** |
|
| 1141 | + * Gets the latest API version served. Eg if there |
|
| 1142 | + * are two versions served of the API, 4.8.29 and 4.8.32, and |
|
| 1143 | + * we are on core version 4.8.34, it will return the string "4.8.32" |
|
| 1144 | + * |
|
| 1145 | + * @return string |
|
| 1146 | + */ |
|
| 1147 | + public static function latest_rest_api_version() |
|
| 1148 | + { |
|
| 1149 | + $versions_served = \EED_Core_Rest_Api::versions_served(); |
|
| 1150 | + $versions_served_keys = array_keys($versions_served); |
|
| 1151 | + return end($versions_served_keys); |
|
| 1152 | + } |
|
| 1153 | + |
|
| 1154 | + |
|
| 1155 | + /** |
|
| 1156 | + * Using EED_Core_Rest_Api::version_compatibilities(), determines what version of |
|
| 1157 | + * EE the API can serve requests for. Eg, if we are on 4.15 of core, and |
|
| 1158 | + * we can serve requests from 4.12 or later, this will return array( '4.12', '4.13', '4.14', '4.15' ). |
|
| 1159 | + * We also indicate whether or not this version should be put in the index or not |
|
| 1160 | + * |
|
| 1161 | + * @return array keys are API version numbers (just major and minor numbers), and values |
|
| 1162 | + * are whether or not they should be hidden |
|
| 1163 | + */ |
|
| 1164 | + public static function versions_served() |
|
| 1165 | + { |
|
| 1166 | + $versions_served = array(); |
|
| 1167 | + $possibly_served_versions = EED_Core_Rest_Api::version_compatibilities(); |
|
| 1168 | + $lowest_compatible_version = end($possibly_served_versions); |
|
| 1169 | + reset($possibly_served_versions); |
|
| 1170 | + $versions_served_historically = array_keys($possibly_served_versions); |
|
| 1171 | + $latest_version = end($versions_served_historically); |
|
| 1172 | + reset($versions_served_historically); |
|
| 1173 | + // for each version of core we have ever served: |
|
| 1174 | + foreach ($versions_served_historically as $key_versioned_endpoint) { |
|
| 1175 | + // if it's not above the current core version, and it's compatible with the current version of core |
|
| 1176 | + if ($key_versioned_endpoint === $latest_version) { |
|
| 1177 | + // don't hide the latest version in the index |
|
| 1178 | + $versions_served[ $key_versioned_endpoint ] = false; |
|
| 1179 | + } elseif ($key_versioned_endpoint >= $lowest_compatible_version |
|
| 1180 | + && $key_versioned_endpoint < EED_Core_Rest_Api::core_version() |
|
| 1181 | + ) { |
|
| 1182 | + // include, but hide, previous versions which are still supported |
|
| 1183 | + $versions_served[ $key_versioned_endpoint ] = true; |
|
| 1184 | + } elseif (apply_filters( |
|
| 1185 | + 'FHEE__EED_Core_Rest_Api__versions_served__include_incompatible_versions', |
|
| 1186 | + false, |
|
| 1187 | + $possibly_served_versions |
|
| 1188 | + )) { |
|
| 1189 | + // if a version is no longer supported, don't include it in index or list of versions served |
|
| 1190 | + $versions_served[ $key_versioned_endpoint ] = true; |
|
| 1191 | + } |
|
| 1192 | + } |
|
| 1193 | + return $versions_served; |
|
| 1194 | + } |
|
| 1195 | + |
|
| 1196 | + |
|
| 1197 | + /** |
|
| 1198 | + * Gets the major and minor version of EE core's version string |
|
| 1199 | + * |
|
| 1200 | + * @return string |
|
| 1201 | + */ |
|
| 1202 | + public static function core_version() |
|
| 1203 | + { |
|
| 1204 | + return apply_filters( |
|
| 1205 | + 'FHEE__EED_Core_REST_API__core_version', |
|
| 1206 | + implode( |
|
| 1207 | + '.', |
|
| 1208 | + array_slice( |
|
| 1209 | + explode( |
|
| 1210 | + '.', |
|
| 1211 | + espresso_version() |
|
| 1212 | + ), |
|
| 1213 | + 0, |
|
| 1214 | + 3 |
|
| 1215 | + ) |
|
| 1216 | + ) |
|
| 1217 | + ); |
|
| 1218 | + } |
|
| 1219 | + |
|
| 1220 | + |
|
| 1221 | + /** |
|
| 1222 | + * Gets the default limit that should be used when querying for resources |
|
| 1223 | + * |
|
| 1224 | + * @return int |
|
| 1225 | + */ |
|
| 1226 | + public static function get_default_query_limit() |
|
| 1227 | + { |
|
| 1228 | + // we actually don't use a const because we want folks to always use |
|
| 1229 | + // this method, not the const directly |
|
| 1230 | + return apply_filters( |
|
| 1231 | + 'FHEE__EED_Core_Rest_Api__get_default_query_limit', |
|
| 1232 | + 50 |
|
| 1233 | + ); |
|
| 1234 | + } |
|
| 1235 | + |
|
| 1236 | + |
|
| 1237 | + /** |
|
| 1238 | + * run - initial module setup |
|
| 1239 | + * |
|
| 1240 | + * @access public |
|
| 1241 | + * @param WP $WP |
|
| 1242 | + * @return void |
|
| 1243 | + */ |
|
| 1244 | + public function run($WP) |
|
| 1245 | + { |
|
| 1246 | + } |
|
| 1247 | 1247 | } |
@@ -40,7 +40,7 @@ discard block |
||
| 40 | 40 | */ |
| 41 | 41 | public function createFromModel($model_name) |
| 42 | 42 | { |
| 43 | - return $this->createFromClassname('EventEspresso\core\libraries\rest_api\calculations\\' . $model_name); |
|
| 43 | + return $this->createFromClassname('EventEspresso\core\libraries\rest_api\calculations\\'.$model_name); |
|
| 44 | 44 | } |
| 45 | 45 | |
| 46 | 46 | /** |
@@ -51,7 +51,7 @@ discard block |
||
| 51 | 51 | public function createFromClassname($calculator_classname) |
| 52 | 52 | { |
| 53 | 53 | $calculator = $this->loader->getShared($calculator_classname); |
| 54 | - if (!$calculator instanceof Base) { |
|
| 54 | + if ( ! $calculator instanceof Base) { |
|
| 55 | 55 | throw new UnexpectedEntityException( |
| 56 | 56 | $calculator_classname, |
| 57 | 57 | 'EventEspresso\core\libraries\rest_api\calculations\Base' |
@@ -18,46 +18,46 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | class CalculatedModelFieldsFactory |
| 20 | 20 | { |
| 21 | - private $loader; |
|
| 21 | + private $loader; |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * CalculatedModelFieldsFactory constructor. |
|
| 25 | - * @param LoaderInterface $loader |
|
| 26 | - */ |
|
| 27 | - public function __construct(LoaderInterface $loader) |
|
| 28 | - { |
|
| 29 | - $this->loader = $loader; |
|
| 30 | - } |
|
| 23 | + /** |
|
| 24 | + * CalculatedModelFieldsFactory constructor. |
|
| 25 | + * @param LoaderInterface $loader |
|
| 26 | + */ |
|
| 27 | + public function __construct(LoaderInterface $loader) |
|
| 28 | + { |
|
| 29 | + $this->loader = $loader; |
|
| 30 | + } |
|
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * Creates the calculator class that corresponds to that particular model |
|
| 34 | - * @since $VID:$ |
|
| 35 | - * @param string $model_name |
|
| 36 | - * @return Base |
|
| 37 | - * @throws UnexpectedEntityException |
|
| 38 | - */ |
|
| 39 | - public function createFromModel($model_name) |
|
| 40 | - { |
|
| 41 | - return $this->createFromClassname('EventEspresso\core\libraries\rest_api\calculations\\' . $model_name); |
|
| 42 | - } |
|
| 32 | + /** |
|
| 33 | + * Creates the calculator class that corresponds to that particular model |
|
| 34 | + * @since $VID:$ |
|
| 35 | + * @param string $model_name |
|
| 36 | + * @return Base |
|
| 37 | + * @throws UnexpectedEntityException |
|
| 38 | + */ |
|
| 39 | + public function createFromModel($model_name) |
|
| 40 | + { |
|
| 41 | + return $this->createFromClassname('EventEspresso\core\libraries\rest_api\calculations\\' . $model_name); |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | - /** |
|
| 45 | - * Creates the calculator class that corresponds to that classname and verifies it's of the correct type |
|
| 46 | - * @param string $calculator_classname |
|
| 47 | - * @return Base |
|
| 48 | - * @throws UnexpectedEntityException |
|
| 49 | - */ |
|
| 50 | - public function createFromClassname($calculator_classname) |
|
| 51 | - { |
|
| 52 | - $calculator = $this->loader->getShared($calculator_classname); |
|
| 53 | - if (!$calculator instanceof Base) { |
|
| 54 | - throw new UnexpectedEntityException( |
|
| 55 | - $calculator_classname, |
|
| 56 | - 'EventEspresso\core\libraries\rest_api\calculations\Base' |
|
| 57 | - ); |
|
| 58 | - } |
|
| 59 | - return $calculator; |
|
| 60 | - } |
|
| 44 | + /** |
|
| 45 | + * Creates the calculator class that corresponds to that classname and verifies it's of the correct type |
|
| 46 | + * @param string $calculator_classname |
|
| 47 | + * @return Base |
|
| 48 | + * @throws UnexpectedEntityException |
|
| 49 | + */ |
|
| 50 | + public function createFromClassname($calculator_classname) |
|
| 51 | + { |
|
| 52 | + $calculator = $this->loader->getShared($calculator_classname); |
|
| 53 | + if (!$calculator instanceof Base) { |
|
| 54 | + throw new UnexpectedEntityException( |
|
| 55 | + $calculator_classname, |
|
| 56 | + 'EventEspresso\core\libraries\rest_api\calculations\Base' |
|
| 57 | + ); |
|
| 58 | + } |
|
| 59 | + return $calculator; |
|
| 60 | + } |
|
| 61 | 61 | } |
| 62 | 62 | // End of file CalculationsFactory.php |
| 63 | 63 | // Location: EventEspresso\core\libraries\rest_api\calculations/CalculationsFactory.php |
@@ -17,210 +17,210 @@ |
||
| 17 | 17 | |
| 18 | 18 | class Datetime extends DatetimeCalculationBase |
| 19 | 19 | { |
| 20 | - /** |
|
| 21 | - * @var EEM_Datetime |
|
| 22 | - */ |
|
| 23 | - protected $datetime_model; |
|
| 20 | + /** |
|
| 21 | + * @var EEM_Datetime |
|
| 22 | + */ |
|
| 23 | + protected $datetime_model; |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * @var EEM_Registration |
|
| 27 | - */ |
|
| 28 | - protected $registration_model; |
|
| 29 | - public function __construct(EEM_Datetime $datetime_model, EEM_Registration $registration_model) |
|
| 30 | - { |
|
| 31 | - $this->datetime_model = $datetime_model; |
|
| 32 | - $this->registration_model = $registration_model; |
|
| 33 | - } |
|
| 25 | + /** |
|
| 26 | + * @var EEM_Registration |
|
| 27 | + */ |
|
| 28 | + protected $registration_model; |
|
| 29 | + public function __construct(EEM_Datetime $datetime_model, EEM_Registration $registration_model) |
|
| 30 | + { |
|
| 31 | + $this->datetime_model = $datetime_model; |
|
| 32 | + $this->registration_model = $registration_model; |
|
| 33 | + } |
|
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * Calculates the total spaces available on the datetime, taking into account |
|
| 37 | - * ticket limits too. |
|
| 38 | - * |
|
| 39 | - * @see EE_Datetime::spaces_remaining( true ) |
|
| 40 | - * @param array $wpdb_row |
|
| 41 | - * @param WP_REST_Request $request |
|
| 42 | - * @param DatetimeControllerBase $controller |
|
| 43 | - * @return int |
|
| 44 | - * @throws EE_Error |
|
| 45 | - * @throws InvalidDataTypeException |
|
| 46 | - * @throws InvalidInterfaceException |
|
| 47 | - * @throws InvalidArgumentException |
|
| 48 | - * @throws ReflectionException |
|
| 49 | - */ |
|
| 50 | - public function spacesRemainingConsideringTickets($wpdb_row, $request, $controller) |
|
| 51 | - { |
|
| 52 | - if (is_array($wpdb_row) && isset($wpdb_row['Datetime.DTT_ID'])) { |
|
| 53 | - $dtt_obj = $this->datetime_model->get_one_by_ID($wpdb_row['Datetime.DTT_ID']); |
|
| 54 | - } else { |
|
| 55 | - $dtt_obj = null; |
|
| 56 | - } |
|
| 57 | - if ($dtt_obj instanceof EE_Datetime) { |
|
| 58 | - return $dtt_obj->spaces_remaining(true); |
|
| 59 | - } |
|
| 60 | - throw new EE_Error( |
|
| 61 | - sprintf( |
|
| 62 | - __( |
|
| 63 | - // @codingStandardsIgnoreStart |
|
| 64 | - 'Cannot calculate spaces_remaining_considering_tickets because the datetime with ID %1$s (from database row %2$s) was not found', |
|
| 65 | - // @codingStandardsIgnoreEnd |
|
| 66 | - 'event_espresso' |
|
| 67 | - ), |
|
| 68 | - $wpdb_row['Datetime.DTT_ID'], |
|
| 69 | - print_r($wpdb_row, true) |
|
| 70 | - ) |
|
| 71 | - ); |
|
| 72 | - } |
|
| 35 | + /** |
|
| 36 | + * Calculates the total spaces available on the datetime, taking into account |
|
| 37 | + * ticket limits too. |
|
| 38 | + * |
|
| 39 | + * @see EE_Datetime::spaces_remaining( true ) |
|
| 40 | + * @param array $wpdb_row |
|
| 41 | + * @param WP_REST_Request $request |
|
| 42 | + * @param DatetimeControllerBase $controller |
|
| 43 | + * @return int |
|
| 44 | + * @throws EE_Error |
|
| 45 | + * @throws InvalidDataTypeException |
|
| 46 | + * @throws InvalidInterfaceException |
|
| 47 | + * @throws InvalidArgumentException |
|
| 48 | + * @throws ReflectionException |
|
| 49 | + */ |
|
| 50 | + public function spacesRemainingConsideringTickets($wpdb_row, $request, $controller) |
|
| 51 | + { |
|
| 52 | + if (is_array($wpdb_row) && isset($wpdb_row['Datetime.DTT_ID'])) { |
|
| 53 | + $dtt_obj = $this->datetime_model->get_one_by_ID($wpdb_row['Datetime.DTT_ID']); |
|
| 54 | + } else { |
|
| 55 | + $dtt_obj = null; |
|
| 56 | + } |
|
| 57 | + if ($dtt_obj instanceof EE_Datetime) { |
|
| 58 | + return $dtt_obj->spaces_remaining(true); |
|
| 59 | + } |
|
| 60 | + throw new EE_Error( |
|
| 61 | + sprintf( |
|
| 62 | + __( |
|
| 63 | + // @codingStandardsIgnoreStart |
|
| 64 | + 'Cannot calculate spaces_remaining_considering_tickets because the datetime with ID %1$s (from database row %2$s) was not found', |
|
| 65 | + // @codingStandardsIgnoreEnd |
|
| 66 | + 'event_espresso' |
|
| 67 | + ), |
|
| 68 | + $wpdb_row['Datetime.DTT_ID'], |
|
| 69 | + print_r($wpdb_row, true) |
|
| 70 | + ) |
|
| 71 | + ); |
|
| 72 | + } |
|
| 73 | 73 | |
| 74 | 74 | |
| 75 | - /** |
|
| 76 | - * Counts registrations who have checked into this datetime |
|
| 77 | - * |
|
| 78 | - * @param array $wpdb_row |
|
| 79 | - * @param WP_REST_Request $request |
|
| 80 | - * @param DatetimeControllerBase $controller |
|
| 81 | - * @return int |
|
| 82 | - * @throws EE_Error |
|
| 83 | - * @throws InvalidArgumentException |
|
| 84 | - * @throws InvalidDataTypeException |
|
| 85 | - * @throws InvalidInterfaceException |
|
| 86 | - * @throws RestException |
|
| 87 | - */ |
|
| 88 | - public function registrationsCheckedInCount($wpdb_row, $request, $controller) |
|
| 89 | - { |
|
| 90 | - if (! is_array($wpdb_row) || ! isset($wpdb_row['Datetime.DTT_ID'])) { |
|
| 91 | - throw new EE_Error( |
|
| 92 | - sprintf( |
|
| 93 | - __( |
|
| 94 | - // @codingStandardsIgnoreStart |
|
| 95 | - 'Cannot calculate registrations_checked_in_count because the database row %1$s does not have an entry for "Datetime.DTT_ID"', |
|
| 96 | - // @codingStandardsIgnoreEnd |
|
| 97 | - 'event_espresso' |
|
| 98 | - ), |
|
| 99 | - print_r($wpdb_row, true) |
|
| 100 | - ) |
|
| 101 | - ); |
|
| 102 | - } |
|
| 103 | - $this->verifyCurrentUserCan('ee_read_checkins', 'registrations_checked_in_count'); |
|
| 104 | - return $this->registration_model |
|
| 105 | - ->count_registrations_checked_into_datetime($wpdb_row['Datetime.DTT_ID'], true); |
|
| 106 | - } |
|
| 75 | + /** |
|
| 76 | + * Counts registrations who have checked into this datetime |
|
| 77 | + * |
|
| 78 | + * @param array $wpdb_row |
|
| 79 | + * @param WP_REST_Request $request |
|
| 80 | + * @param DatetimeControllerBase $controller |
|
| 81 | + * @return int |
|
| 82 | + * @throws EE_Error |
|
| 83 | + * @throws InvalidArgumentException |
|
| 84 | + * @throws InvalidDataTypeException |
|
| 85 | + * @throws InvalidInterfaceException |
|
| 86 | + * @throws RestException |
|
| 87 | + */ |
|
| 88 | + public function registrationsCheckedInCount($wpdb_row, $request, $controller) |
|
| 89 | + { |
|
| 90 | + if (! is_array($wpdb_row) || ! isset($wpdb_row['Datetime.DTT_ID'])) { |
|
| 91 | + throw new EE_Error( |
|
| 92 | + sprintf( |
|
| 93 | + __( |
|
| 94 | + // @codingStandardsIgnoreStart |
|
| 95 | + 'Cannot calculate registrations_checked_in_count because the database row %1$s does not have an entry for "Datetime.DTT_ID"', |
|
| 96 | + // @codingStandardsIgnoreEnd |
|
| 97 | + 'event_espresso' |
|
| 98 | + ), |
|
| 99 | + print_r($wpdb_row, true) |
|
| 100 | + ) |
|
| 101 | + ); |
|
| 102 | + } |
|
| 103 | + $this->verifyCurrentUserCan('ee_read_checkins', 'registrations_checked_in_count'); |
|
| 104 | + return $this->registration_model |
|
| 105 | + ->count_registrations_checked_into_datetime($wpdb_row['Datetime.DTT_ID'], true); |
|
| 106 | + } |
|
| 107 | 107 | |
| 108 | 108 | |
| 109 | - /** |
|
| 110 | - * Counts registrations who have checked out of this datetime |
|
| 111 | - * |
|
| 112 | - * @param array $wpdb_row |
|
| 113 | - * @param WP_REST_Request $request |
|
| 114 | - * @param DatetimeControllerBase $controller |
|
| 115 | - * @return int |
|
| 116 | - * @throws EE_Error |
|
| 117 | - * @throws InvalidArgumentException |
|
| 118 | - * @throws InvalidDataTypeException |
|
| 119 | - * @throws InvalidInterfaceException |
|
| 120 | - * @throws RestException |
|
| 121 | - */ |
|
| 122 | - public function registrationsCheckedOutCount($wpdb_row, $request, $controller) |
|
| 123 | - { |
|
| 124 | - if (! is_array($wpdb_row) || ! isset($wpdb_row['Datetime.DTT_ID'])) { |
|
| 125 | - throw new EE_Error( |
|
| 126 | - sprintf( |
|
| 127 | - __( |
|
| 128 | - // @codingStandardsIgnoreStart |
|
| 129 | - 'Cannot calculate registrations_checked_out_count because the database row %1$s does not have an entry for "Datetime.DTT_ID"', |
|
| 130 | - // @codingStandardsIgnoreEnd |
|
| 131 | - 'event_espresso' |
|
| 132 | - ), |
|
| 133 | - print_r($wpdb_row, true) |
|
| 134 | - ) |
|
| 135 | - ); |
|
| 136 | - } |
|
| 137 | - $this->verifyCurrentUserCan('ee_read_checkins', 'registrations_checked_out_count'); |
|
| 138 | - return $this->registration_model |
|
| 139 | - ->count_registrations_checked_into_datetime($wpdb_row['Datetime.DTT_ID'], false); |
|
| 140 | - } |
|
| 109 | + /** |
|
| 110 | + * Counts registrations who have checked out of this datetime |
|
| 111 | + * |
|
| 112 | + * @param array $wpdb_row |
|
| 113 | + * @param WP_REST_Request $request |
|
| 114 | + * @param DatetimeControllerBase $controller |
|
| 115 | + * @return int |
|
| 116 | + * @throws EE_Error |
|
| 117 | + * @throws InvalidArgumentException |
|
| 118 | + * @throws InvalidDataTypeException |
|
| 119 | + * @throws InvalidInterfaceException |
|
| 120 | + * @throws RestException |
|
| 121 | + */ |
|
| 122 | + public function registrationsCheckedOutCount($wpdb_row, $request, $controller) |
|
| 123 | + { |
|
| 124 | + if (! is_array($wpdb_row) || ! isset($wpdb_row['Datetime.DTT_ID'])) { |
|
| 125 | + throw new EE_Error( |
|
| 126 | + sprintf( |
|
| 127 | + __( |
|
| 128 | + // @codingStandardsIgnoreStart |
|
| 129 | + 'Cannot calculate registrations_checked_out_count because the database row %1$s does not have an entry for "Datetime.DTT_ID"', |
|
| 130 | + // @codingStandardsIgnoreEnd |
|
| 131 | + 'event_espresso' |
|
| 132 | + ), |
|
| 133 | + print_r($wpdb_row, true) |
|
| 134 | + ) |
|
| 135 | + ); |
|
| 136 | + } |
|
| 137 | + $this->verifyCurrentUserCan('ee_read_checkins', 'registrations_checked_out_count'); |
|
| 138 | + return $this->registration_model |
|
| 139 | + ->count_registrations_checked_into_datetime($wpdb_row['Datetime.DTT_ID'], false); |
|
| 140 | + } |
|
| 141 | 141 | |
| 142 | 142 | |
| 143 | - /** |
|
| 144 | - * Counts the number of pending-payment registrations for this event (regardless |
|
| 145 | - * of how many datetimes each registrations' ticket purchase is for) |
|
| 146 | - * |
|
| 147 | - * @param array $wpdb_row |
|
| 148 | - * @param WP_REST_Request $request |
|
| 149 | - * @param DatetimeControllerBase $controller |
|
| 150 | - * @return int |
|
| 151 | - * @throws EE_Error |
|
| 152 | - * @throws InvalidArgumentException |
|
| 153 | - * @throws InvalidDataTypeException |
|
| 154 | - * @throws InvalidInterfaceException |
|
| 155 | - * @throws RestException |
|
| 156 | - */ |
|
| 157 | - public function spotsTakenPendingPayment($wpdb_row, $request, $controller) |
|
| 158 | - { |
|
| 159 | - if (! is_array($wpdb_row) || ! isset($wpdb_row['Datetime.DTT_ID'])) { |
|
| 160 | - throw new EE_Error( |
|
| 161 | - sprintf( |
|
| 162 | - __( |
|
| 163 | - // @codingStandardsIgnoreStart |
|
| 164 | - 'Cannot calculate spots_taken_pending_payment because the database row %1$s does not have an entry for "Datetime.DTT_ID"', |
|
| 165 | - // @codingStandardsIgnoreEnd |
|
| 166 | - 'event_espresso' |
|
| 167 | - ), |
|
| 168 | - print_r($wpdb_row, true) |
|
| 169 | - ) |
|
| 170 | - ); |
|
| 171 | - } |
|
| 172 | - $this->verifyCurrentUserCan('ee_read_registrations', 'spots_taken_pending_payment'); |
|
| 173 | - return $this->registration_model->count( |
|
| 174 | - array( |
|
| 175 | - array( |
|
| 176 | - 'Ticket.Datetime.DTT_ID' => $wpdb_row['Datetime.DTT_ID'], |
|
| 177 | - 'STS_ID' => EEM_Registration::status_id_pending_payment, |
|
| 178 | - ), |
|
| 179 | - ), |
|
| 180 | - 'REG_ID', |
|
| 181 | - true |
|
| 182 | - ); |
|
| 183 | - } |
|
| 143 | + /** |
|
| 144 | + * Counts the number of pending-payment registrations for this event (regardless |
|
| 145 | + * of how many datetimes each registrations' ticket purchase is for) |
|
| 146 | + * |
|
| 147 | + * @param array $wpdb_row |
|
| 148 | + * @param WP_REST_Request $request |
|
| 149 | + * @param DatetimeControllerBase $controller |
|
| 150 | + * @return int |
|
| 151 | + * @throws EE_Error |
|
| 152 | + * @throws InvalidArgumentException |
|
| 153 | + * @throws InvalidDataTypeException |
|
| 154 | + * @throws InvalidInterfaceException |
|
| 155 | + * @throws RestException |
|
| 156 | + */ |
|
| 157 | + public function spotsTakenPendingPayment($wpdb_row, $request, $controller) |
|
| 158 | + { |
|
| 159 | + if (! is_array($wpdb_row) || ! isset($wpdb_row['Datetime.DTT_ID'])) { |
|
| 160 | + throw new EE_Error( |
|
| 161 | + sprintf( |
|
| 162 | + __( |
|
| 163 | + // @codingStandardsIgnoreStart |
|
| 164 | + 'Cannot calculate spots_taken_pending_payment because the database row %1$s does not have an entry for "Datetime.DTT_ID"', |
|
| 165 | + // @codingStandardsIgnoreEnd |
|
| 166 | + 'event_espresso' |
|
| 167 | + ), |
|
| 168 | + print_r($wpdb_row, true) |
|
| 169 | + ) |
|
| 170 | + ); |
|
| 171 | + } |
|
| 172 | + $this->verifyCurrentUserCan('ee_read_registrations', 'spots_taken_pending_payment'); |
|
| 173 | + return $this->registration_model->count( |
|
| 174 | + array( |
|
| 175 | + array( |
|
| 176 | + 'Ticket.Datetime.DTT_ID' => $wpdb_row['Datetime.DTT_ID'], |
|
| 177 | + 'STS_ID' => EEM_Registration::status_id_pending_payment, |
|
| 178 | + ), |
|
| 179 | + ), |
|
| 180 | + 'REG_ID', |
|
| 181 | + true |
|
| 182 | + ); |
|
| 183 | + } |
|
| 184 | 184 | |
| 185 | 185 | |
| 186 | - /** |
|
| 187 | - * Provides an array for all the calculations possible that outlines a json schema for those calculations. |
|
| 188 | - * Array is indexed by calculation (snake case) and value is the schema for that calculation. |
|
| 189 | - * |
|
| 190 | - * @since $VID:$ |
|
| 191 | - * @return array |
|
| 192 | - */ |
|
| 193 | - public function schemaForCalculations() |
|
| 194 | - { |
|
| 195 | - return array( |
|
| 196 | - 'spaces_remaining_considering_tickets' => array( |
|
| 197 | - 'description' => esc_html__( |
|
| 198 | - 'Calculates the total spaces available on the datetime, taking into account ticket limits too.', |
|
| 199 | - 'event_espresso' |
|
| 200 | - ), |
|
| 201 | - 'type' => 'number' |
|
| 202 | - ), |
|
| 203 | - 'registrations_checked_in_count' => array( |
|
| 204 | - 'description' => esc_html__( |
|
| 205 | - 'Counts registrations who have checked into this datetime.', |
|
| 206 | - 'event_espresso' |
|
| 207 | - ), |
|
| 208 | - 'type' => 'number' |
|
| 209 | - ), |
|
| 210 | - 'registrations_checked_out_count' => array( |
|
| 211 | - 'description' => esc_html__( |
|
| 212 | - 'Counts registrations who have checked out of this datetime.', |
|
| 213 | - 'event_espresso' |
|
| 214 | - ), |
|
| 215 | - 'type' => 'number' |
|
| 216 | - ), |
|
| 217 | - 'spots_taken_pending_payment' => array( |
|
| 218 | - 'description' => esc_html__( |
|
| 219 | - 'The count of pending-payment registrations for this event (regardless of how many datetimes each registration\'s ticket purchase is for', |
|
| 220 | - 'event_espresso' |
|
| 221 | - ), |
|
| 222 | - 'type' => 'number' |
|
| 223 | - ), |
|
| 224 | - ); |
|
| 225 | - } |
|
| 186 | + /** |
|
| 187 | + * Provides an array for all the calculations possible that outlines a json schema for those calculations. |
|
| 188 | + * Array is indexed by calculation (snake case) and value is the schema for that calculation. |
|
| 189 | + * |
|
| 190 | + * @since $VID:$ |
|
| 191 | + * @return array |
|
| 192 | + */ |
|
| 193 | + public function schemaForCalculations() |
|
| 194 | + { |
|
| 195 | + return array( |
|
| 196 | + 'spaces_remaining_considering_tickets' => array( |
|
| 197 | + 'description' => esc_html__( |
|
| 198 | + 'Calculates the total spaces available on the datetime, taking into account ticket limits too.', |
|
| 199 | + 'event_espresso' |
|
| 200 | + ), |
|
| 201 | + 'type' => 'number' |
|
| 202 | + ), |
|
| 203 | + 'registrations_checked_in_count' => array( |
|
| 204 | + 'description' => esc_html__( |
|
| 205 | + 'Counts registrations who have checked into this datetime.', |
|
| 206 | + 'event_espresso' |
|
| 207 | + ), |
|
| 208 | + 'type' => 'number' |
|
| 209 | + ), |
|
| 210 | + 'registrations_checked_out_count' => array( |
|
| 211 | + 'description' => esc_html__( |
|
| 212 | + 'Counts registrations who have checked out of this datetime.', |
|
| 213 | + 'event_espresso' |
|
| 214 | + ), |
|
| 215 | + 'type' => 'number' |
|
| 216 | + ), |
|
| 217 | + 'spots_taken_pending_payment' => array( |
|
| 218 | + 'description' => esc_html__( |
|
| 219 | + 'The count of pending-payment registrations for this event (regardless of how many datetimes each registration\'s ticket purchase is for', |
|
| 220 | + 'event_espresso' |
|
| 221 | + ), |
|
| 222 | + 'type' => 'number' |
|
| 223 | + ), |
|
| 224 | + ); |
|
| 225 | + } |
|
| 226 | 226 | } |
@@ -87,7 +87,7 @@ discard block |
||
| 87 | 87 | */ |
| 88 | 88 | public function registrationsCheckedInCount($wpdb_row, $request, $controller) |
| 89 | 89 | { |
| 90 | - if (! is_array($wpdb_row) || ! isset($wpdb_row['Datetime.DTT_ID'])) { |
|
| 90 | + if ( ! is_array($wpdb_row) || ! isset($wpdb_row['Datetime.DTT_ID'])) { |
|
| 91 | 91 | throw new EE_Error( |
| 92 | 92 | sprintf( |
| 93 | 93 | __( |
@@ -121,7 +121,7 @@ discard block |
||
| 121 | 121 | */ |
| 122 | 122 | public function registrationsCheckedOutCount($wpdb_row, $request, $controller) |
| 123 | 123 | { |
| 124 | - if (! is_array($wpdb_row) || ! isset($wpdb_row['Datetime.DTT_ID'])) { |
|
| 124 | + if ( ! is_array($wpdb_row) || ! isset($wpdb_row['Datetime.DTT_ID'])) { |
|
| 125 | 125 | throw new EE_Error( |
| 126 | 126 | sprintf( |
| 127 | 127 | __( |
@@ -156,7 +156,7 @@ discard block |
||
| 156 | 156 | */ |
| 157 | 157 | public function spotsTakenPendingPayment($wpdb_row, $request, $controller) |
| 158 | 158 | { |
| 159 | - if (! is_array($wpdb_row) || ! isset($wpdb_row['Datetime.DTT_ID'])) { |
|
| 159 | + if ( ! is_array($wpdb_row) || ! isset($wpdb_row['Datetime.DTT_ID'])) { |
|
| 160 | 160 | throw new EE_Error( |
| 161 | 161 | sprintf( |
| 162 | 162 | __( |
@@ -17,43 +17,43 @@ |
||
| 17 | 17 | class Attendee extends AttendeeCalculationsBase |
| 18 | 18 | { |
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * @param array $wpdb_row |
|
| 22 | - * @param WP_REST_Request $request |
|
| 23 | - * @param AttendeeControllerBase $controller |
|
| 24 | - * @since 4.9.66.p |
|
| 25 | - * @return string |
|
| 26 | - */ |
|
| 27 | - public function userAvatar(array $wpdb_row, WP_REST_Request $request, AttendeeControllerBase $controller) |
|
| 28 | - { |
|
| 29 | - if (is_array($wpdb_row) && isset($wpdb_row['Attendee_Meta.ATT_email'])) { |
|
| 30 | - $email_address = $wpdb_row['Attendee_Meta.ATT_email']; |
|
| 31 | - } |
|
| 32 | - if (empty($email_address)) { |
|
| 33 | - return ''; |
|
| 34 | - } |
|
| 35 | - $avatar = get_avatar_url($email_address); |
|
| 36 | - return $avatar ? $avatar : ''; |
|
| 37 | - } |
|
| 20 | + /** |
|
| 21 | + * @param array $wpdb_row |
|
| 22 | + * @param WP_REST_Request $request |
|
| 23 | + * @param AttendeeControllerBase $controller |
|
| 24 | + * @since 4.9.66.p |
|
| 25 | + * @return string |
|
| 26 | + */ |
|
| 27 | + public function userAvatar(array $wpdb_row, WP_REST_Request $request, AttendeeControllerBase $controller) |
|
| 28 | + { |
|
| 29 | + if (is_array($wpdb_row) && isset($wpdb_row['Attendee_Meta.ATT_email'])) { |
|
| 30 | + $email_address = $wpdb_row['Attendee_Meta.ATT_email']; |
|
| 31 | + } |
|
| 32 | + if (empty($email_address)) { |
|
| 33 | + return ''; |
|
| 34 | + } |
|
| 35 | + $avatar = get_avatar_url($email_address); |
|
| 36 | + return $avatar ? $avatar : ''; |
|
| 37 | + } |
|
| 38 | 38 | |
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * Provides an array for all the calculations possible that outlines a json schema for those calculations. |
|
| 42 | - * Array is indexed by calculation (snake case) and value is the schema for that calculation. |
|
| 43 | - * |
|
| 44 | - * @since $VID:$ |
|
| 45 | - * @return array |
|
| 46 | - */ |
|
| 47 | - public function schemaForCalculations() |
|
| 48 | - { |
|
| 49 | - return array( |
|
| 50 | - 'user_avatar' => array( |
|
| 51 | - 'description' => esc_html__( |
|
| 52 | - 'The avatar url for the attendee (if available).', |
|
| 53 | - 'event_espresso' |
|
| 54 | - ), |
|
| 55 | - 'type' => 'string', |
|
| 56 | - ), |
|
| 57 | - ); |
|
| 58 | - } |
|
| 40 | + /** |
|
| 41 | + * Provides an array for all the calculations possible that outlines a json schema for those calculations. |
|
| 42 | + * Array is indexed by calculation (snake case) and value is the schema for that calculation. |
|
| 43 | + * |
|
| 44 | + * @since $VID:$ |
|
| 45 | + * @return array |
|
| 46 | + */ |
|
| 47 | + public function schemaForCalculations() |
|
| 48 | + { |
|
| 49 | + return array( |
|
| 50 | + 'user_avatar' => array( |
|
| 51 | + 'description' => esc_html__( |
|
| 52 | + 'The avatar url for the attendee (if available).', |
|
| 53 | + 'event_espresso' |
|
| 54 | + ), |
|
| 55 | + 'type' => 'string', |
|
| 56 | + ), |
|
| 57 | + ); |
|
| 58 | + } |
|
| 59 | 59 | } |
@@ -23,7 +23,7 @@ discard block |
||
| 23 | 23 | */ |
| 24 | 24 | protected function verifyCurrentUserCan($required_permission, $attempted_calculation) |
| 25 | 25 | { |
| 26 | - if (! current_user_can($required_permission)) { |
|
| 26 | + if ( ! current_user_can($required_permission)) { |
|
| 27 | 27 | throw new RestException( |
| 28 | 28 | 'permission_denied', |
| 29 | 29 | sprintf( |
@@ -75,6 +75,6 @@ discard block |
||
| 75 | 75 | public function schemaForCalculation($calculation_index) |
| 76 | 76 | { |
| 77 | 77 | $schema_map = $this->schemaForCalculations(); |
| 78 | - return isset($schema_map[ $calculation_index ]) ? $schema_map[ $calculation_index ] : array(); |
|
| 78 | + return isset($schema_map[$calculation_index]) ? $schema_map[$calculation_index] : array(); |
|
| 79 | 79 | } |
| 80 | 80 | } |
@@ -16,65 +16,65 @@ |
||
| 16 | 16 | class Base |
| 17 | 17 | { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * @param $required_permission |
|
| 21 | - * @param $attempted_calculation |
|
| 22 | - * @throws RestException |
|
| 23 | - */ |
|
| 24 | - protected function verifyCurrentUserCan($required_permission, $attempted_calculation) |
|
| 25 | - { |
|
| 26 | - if (! current_user_can($required_permission)) { |
|
| 27 | - throw new RestException( |
|
| 28 | - 'permission_denied', |
|
| 29 | - sprintf( |
|
| 30 | - __( |
|
| 31 | - // @codingStandardsIgnoreStart |
|
| 32 | - 'Permission denied, you cannot calculate %1$s on %2$s because you do not have the capability "%3$s"', |
|
| 33 | - // @codingStandardsIgnoreEnd |
|
| 34 | - 'event_espresso' |
|
| 35 | - ), |
|
| 36 | - $attempted_calculation, |
|
| 37 | - EEH_Inflector::pluralize_and_lower($this->getResourceName()), |
|
| 38 | - $required_permission |
|
| 39 | - ) |
|
| 40 | - ); |
|
| 41 | - } |
|
| 42 | - } |
|
| 19 | + /** |
|
| 20 | + * @param $required_permission |
|
| 21 | + * @param $attempted_calculation |
|
| 22 | + * @throws RestException |
|
| 23 | + */ |
|
| 24 | + protected function verifyCurrentUserCan($required_permission, $attempted_calculation) |
|
| 25 | + { |
|
| 26 | + if (! current_user_can($required_permission)) { |
|
| 27 | + throw new RestException( |
|
| 28 | + 'permission_denied', |
|
| 29 | + sprintf( |
|
| 30 | + __( |
|
| 31 | + // @codingStandardsIgnoreStart |
|
| 32 | + 'Permission denied, you cannot calculate %1$s on %2$s because you do not have the capability "%3$s"', |
|
| 33 | + // @codingStandardsIgnoreEnd |
|
| 34 | + 'event_espresso' |
|
| 35 | + ), |
|
| 36 | + $attempted_calculation, |
|
| 37 | + EEH_Inflector::pluralize_and_lower($this->getResourceName()), |
|
| 38 | + $required_permission |
|
| 39 | + ) |
|
| 40 | + ); |
|
| 41 | + } |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | 44 | |
| 45 | - /** |
|
| 46 | - * Gets the name of the resource of the called class |
|
| 47 | - * |
|
| 48 | - * @return string |
|
| 49 | - */ |
|
| 50 | - public function getResourceName() |
|
| 51 | - { |
|
| 52 | - return substr(__CLASS__, strrpos(__CLASS__, '\\') + 1); |
|
| 53 | - } |
|
| 45 | + /** |
|
| 46 | + * Gets the name of the resource of the called class |
|
| 47 | + * |
|
| 48 | + * @return string |
|
| 49 | + */ |
|
| 50 | + public function getResourceName() |
|
| 51 | + { |
|
| 52 | + return substr(__CLASS__, strrpos(__CLASS__, '\\') + 1); |
|
| 53 | + } |
|
| 54 | 54 | |
| 55 | - /** |
|
| 56 | - * Returns an array to be used for the schema for the calculated fields. |
|
| 57 | - * @since $VID:$ |
|
| 58 | - * @return array keys are calculated field names (eg "optimum_sales_at_start") values are arrays { |
|
| 59 | - * @type string $description |
|
| 60 | - * @type string $type, eg "string", "int", "boolean", "object", "array", etc |
|
| 61 | - * } |
|
| 62 | - */ |
|
| 63 | - public function schemaForCalculations() |
|
| 64 | - { |
|
| 65 | - return array(); |
|
| 66 | - } |
|
| 55 | + /** |
|
| 56 | + * Returns an array to be used for the schema for the calculated fields. |
|
| 57 | + * @since $VID:$ |
|
| 58 | + * @return array keys are calculated field names (eg "optimum_sales_at_start") values are arrays { |
|
| 59 | + * @type string $description |
|
| 60 | + * @type string $type, eg "string", "int", "boolean", "object", "array", etc |
|
| 61 | + * } |
|
| 62 | + */ |
|
| 63 | + public function schemaForCalculations() |
|
| 64 | + { |
|
| 65 | + return array(); |
|
| 66 | + } |
|
| 67 | 67 | |
| 68 | - /** |
|
| 69 | - * Returns the json schema for the given calculation index. |
|
| 70 | - * |
|
| 71 | - * @since $VID:$ |
|
| 72 | - * @param $calculation_index |
|
| 73 | - * @return array |
|
| 74 | - */ |
|
| 75 | - public function schemaForCalculation($calculation_index) |
|
| 76 | - { |
|
| 77 | - $schema_map = $this->schemaForCalculations(); |
|
| 78 | - return isset($schema_map[ $calculation_index ]) ? $schema_map[ $calculation_index ] : array(); |
|
| 79 | - } |
|
| 68 | + /** |
|
| 69 | + * Returns the json schema for the given calculation index. |
|
| 70 | + * |
|
| 71 | + * @since $VID:$ |
|
| 72 | + * @param $calculation_index |
|
| 73 | + * @return array |
|
| 74 | + */ |
|
| 75 | + public function schemaForCalculation($calculation_index) |
|
| 76 | + { |
|
| 77 | + $schema_map = $this->schemaForCalculations(); |
|
| 78 | + return isset($schema_map[ $calculation_index ]) ? $schema_map[ $calculation_index ] : array(); |
|
| 79 | + } |
|
| 80 | 80 | } |
@@ -26,564 +26,564 @@ |
||
| 26 | 26 | */ |
| 27 | 27 | class Event extends EventCalculationBase |
| 28 | 28 | { |
| 29 | - /** |
|
| 30 | - * @var EEM_Event |
|
| 31 | - */ |
|
| 32 | - protected $event_model; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * @var EEM_Registration |
|
| 36 | - */ |
|
| 37 | - protected $registration_model; |
|
| 38 | - public function __construct(EEM_Event $event_model, EEM_Registration $registration_model) |
|
| 39 | - { |
|
| 40 | - $this->event_model = $event_model; |
|
| 41 | - $this->registration_model = $registration_model; |
|
| 42 | - } |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * Calculates the total spaces on the event (not subtracting sales, but taking |
|
| 46 | - * sales into account; so this is the optimum sales that CAN still be achieved) |
|
| 47 | - * See EE_Event::total_available_spaces( true ); |
|
| 48 | - * |
|
| 49 | - * @param array $wpdb_row |
|
| 50 | - * @param WP_REST_Request $request |
|
| 51 | - * @param EventControllerBase $controller |
|
| 52 | - * @return int |
|
| 53 | - * @throws EE_Error |
|
| 54 | - * @throws DomainException |
|
| 55 | - * @throws InvalidDataTypeException |
|
| 56 | - * @throws InvalidInterfaceException |
|
| 57 | - * @throws UnexpectedEntityException |
|
| 58 | - * @throws InvalidArgumentException |
|
| 59 | - */ |
|
| 60 | - public function optimumSalesAtStart($wpdb_row, $request, $controller) |
|
| 61 | - { |
|
| 62 | - $event_obj = null; |
|
| 63 | - if (Event::wpdbRowHasEventId($wpdb_row)) { |
|
| 64 | - $event_obj = $this->event_model->get_one_by_ID($wpdb_row['Event_CPT.ID']); |
|
| 65 | - } |
|
| 66 | - if ($event_obj instanceof EE_Event) { |
|
| 67 | - return $event_obj->total_available_spaces(); |
|
| 68 | - } |
|
| 69 | - throw new EE_Error( |
|
| 70 | - sprintf( |
|
| 71 | - __( |
|
| 72 | - // @codingStandardsIgnoreStart |
|
| 73 | - 'Cannot calculate optimum_sales_at_start because the event with ID %1$s (from database row %2$s) was not found', |
|
| 74 | - // @codingStandardsIgnoreEnd |
|
| 75 | - 'event_espresso' |
|
| 76 | - ), |
|
| 77 | - $wpdb_row['Event_CPT.ID'], |
|
| 78 | - print_r($wpdb_row, true) |
|
| 79 | - ) |
|
| 80 | - ); |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * Calculates the total spaces on the event (ignoring all sales; so this is the optimum |
|
| 86 | - * sales that COULD have been achieved) |
|
| 87 | - * See EE_Event::total_available_spaces( true ); |
|
| 88 | - * |
|
| 89 | - * @param array $wpdb_row |
|
| 90 | - * @param WP_REST_Request $request |
|
| 91 | - * @param EventControllerBase $controller |
|
| 92 | - * @return int |
|
| 93 | - * @throws DomainException |
|
| 94 | - * @throws EE_Error |
|
| 95 | - * @throws InvalidArgumentException |
|
| 96 | - * @throws InvalidDataTypeException |
|
| 97 | - * @throws InvalidInterfaceException |
|
| 98 | - * @throws UnexpectedEntityException |
|
| 99 | - */ |
|
| 100 | - public function optimumSalesNow($wpdb_row, $request, $controller) |
|
| 101 | - { |
|
| 102 | - $event_obj = null; |
|
| 103 | - if (Event::wpdbRowHasEventId($wpdb_row)) { |
|
| 104 | - $event_obj = $this->event_model->get_one_by_ID($wpdb_row['Event_CPT.ID']); |
|
| 105 | - } |
|
| 106 | - if ($event_obj instanceof EE_Event) { |
|
| 107 | - return $event_obj->total_available_spaces(true); |
|
| 108 | - } |
|
| 109 | - throw new EE_Error( |
|
| 110 | - sprintf( |
|
| 111 | - __( |
|
| 112 | - // @codingStandardsIgnoreStart |
|
| 113 | - 'Cannot calculate optimum_sales_now because the event with ID %1$s (from database row %2$s) was not found', |
|
| 114 | - // @codingStandardsIgnoreEnd |
|
| 115 | - 'event_espresso' |
|
| 116 | - ), |
|
| 117 | - $wpdb_row['Event_CPT.ID'], |
|
| 118 | - print_r($wpdb_row, true) |
|
| 119 | - ) |
|
| 120 | - ); |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * Like optimum_sales_now, but minus total sales so far. |
|
| 126 | - * See EE_Event::spaces_remaining_for_sale( true ); |
|
| 127 | - * |
|
| 128 | - * @param array $wpdb_row |
|
| 129 | - * @param WP_REST_Request $request |
|
| 130 | - * @param EventControllerBase $controller |
|
| 131 | - * @return int |
|
| 132 | - * @throws DomainException |
|
| 133 | - * @throws EE_Error |
|
| 134 | - * @throws InvalidArgumentException |
|
| 135 | - * @throws InvalidDataTypeException |
|
| 136 | - * @throws InvalidInterfaceException |
|
| 137 | - * @throws UnexpectedEntityException |
|
| 138 | - */ |
|
| 139 | - public function spacesRemaining($wpdb_row, $request, $controller) |
|
| 140 | - { |
|
| 141 | - $event_obj = null; |
|
| 142 | - if (Event::wpdbRowHasEventId($wpdb_row)) { |
|
| 143 | - $event_obj = $this->event_model->get_one_by_ID($wpdb_row['Event_CPT.ID']); |
|
| 144 | - } |
|
| 145 | - if ($event_obj instanceof EE_Event) { |
|
| 146 | - return $event_obj->spaces_remaining_for_sale(); |
|
| 147 | - } |
|
| 148 | - throw new EE_Error( |
|
| 149 | - sprintf( |
|
| 150 | - __( |
|
| 151 | - // @codingStandardsIgnoreStart |
|
| 152 | - 'Cannot calculate spaces_remaining because the event with ID %1$s (from database row %2$s) was not found', |
|
| 153 | - // @codingStandardsIgnoreEnd |
|
| 154 | - 'event_espresso' |
|
| 155 | - ), |
|
| 156 | - $wpdb_row['Event_CPT.ID'], |
|
| 157 | - print_r($wpdb_row, true) |
|
| 158 | - ) |
|
| 159 | - ); |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - |
|
| 163 | - /** |
|
| 164 | - * Counts the number of approved registrations for this event (regardless |
|
| 165 | - * of how many datetimes each registrations' ticket purchase is for) |
|
| 166 | - * |
|
| 167 | - * @param array $wpdb_row |
|
| 168 | - * @param WP_REST_Request $request |
|
| 169 | - * @param EventControllerBase $controller |
|
| 170 | - * @return int |
|
| 171 | - * @throws EE_Error |
|
| 172 | - * @throws InvalidArgumentException |
|
| 173 | - * @throws InvalidDataTypeException |
|
| 174 | - * @throws InvalidInterfaceException |
|
| 175 | - */ |
|
| 176 | - public function spotsTaken($wpdb_row, $request, $controller) |
|
| 177 | - { |
|
| 178 | - if (! Event::wpdbRowHasEventId($wpdb_row)) { |
|
| 179 | - throw new EE_Error( |
|
| 180 | - sprintf( |
|
| 181 | - __( |
|
| 182 | - // @codingStandardsIgnoreStart |
|
| 183 | - 'Cannot calculate spots_taken because the database row %1$s does not have a valid entry for "Event_CPT.ID"', |
|
| 184 | - // @codingStandardsIgnoreEnd |
|
| 185 | - 'event_espresso' |
|
| 186 | - ), |
|
| 187 | - print_r($wpdb_row, true) |
|
| 188 | - ) |
|
| 189 | - ); |
|
| 190 | - } |
|
| 191 | - return $this->registration_model->count( |
|
| 192 | - array( |
|
| 193 | - array( |
|
| 194 | - 'EVT_ID' => $wpdb_row['Event_CPT.ID'], |
|
| 195 | - 'STS_ID' => EEM_Registration::status_id_approved, |
|
| 196 | - ), |
|
| 197 | - ), |
|
| 198 | - 'REG_ID', |
|
| 199 | - true |
|
| 200 | - ); |
|
| 201 | - } |
|
| 202 | - |
|
| 203 | - |
|
| 204 | - /** |
|
| 205 | - * Counts the number of pending-payment registrations for this event (regardless |
|
| 206 | - * of how many datetimes each registrations' ticket purchase is for) |
|
| 207 | - * |
|
| 208 | - * @param array $wpdb_row |
|
| 209 | - * @param WP_REST_Request $request |
|
| 210 | - * @param EventControllerBase $controller |
|
| 211 | - * @return int |
|
| 212 | - * @throws EE_Error |
|
| 213 | - * @throws InvalidArgumentException |
|
| 214 | - * @throws InvalidDataTypeException |
|
| 215 | - * @throws InvalidInterfaceException |
|
| 216 | - * @throws RestException |
|
| 217 | - */ |
|
| 218 | - public function spotsTakenPendingPayment($wpdb_row, $request, $controller) |
|
| 219 | - { |
|
| 220 | - if (! Event::wpdbRowHasEventId($wpdb_row)) { |
|
| 221 | - throw new EE_Error( |
|
| 222 | - sprintf( |
|
| 223 | - __( |
|
| 224 | - // @codingStandardsIgnoreStart |
|
| 225 | - 'Cannot calculate spots_taken_pending_payment because the database row %1$s does not have an entry for "Event_CPT.ID"', |
|
| 226 | - // @codingStandardsIgnoreEnd |
|
| 227 | - 'event_espresso' |
|
| 228 | - ), |
|
| 229 | - print_r($wpdb_row, true) |
|
| 230 | - ) |
|
| 231 | - ); |
|
| 232 | - } |
|
| 233 | - $this->verifyCurrentUserCan('ee_read_registrations', 'spots_taken_pending_payment'); |
|
| 234 | - return $this->registration_model->count( |
|
| 235 | - array( |
|
| 236 | - array( |
|
| 237 | - 'EVT_ID' => $wpdb_row['Event_CPT.ID'], |
|
| 238 | - 'STS_ID' => EEM_Registration::status_id_pending_payment, |
|
| 239 | - ), |
|
| 240 | - ), |
|
| 241 | - 'REG_ID', |
|
| 242 | - true |
|
| 243 | - ); |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - |
|
| 247 | - /** |
|
| 248 | - * Counts all the registrations who have checked into one of this events' datetimes |
|
| 249 | - * See EE_Event::total_available_spaces( false ); |
|
| 250 | - * |
|
| 251 | - * @param array $wpdb_row |
|
| 252 | - * @param WP_REST_Request $request |
|
| 253 | - * @param EventControllerBase $controller |
|
| 254 | - * @return int|null if permission denied |
|
| 255 | - * @throws EE_Error |
|
| 256 | - * @throws InvalidArgumentException |
|
| 257 | - * @throws InvalidDataTypeException |
|
| 258 | - * @throws InvalidInterfaceException |
|
| 259 | - * @throws RestException |
|
| 260 | - */ |
|
| 261 | - public function registrationsCheckedInCount($wpdb_row, $request, $controller) |
|
| 262 | - { |
|
| 263 | - if (! Event::wpdbRowHasEventId($wpdb_row)) { |
|
| 264 | - throw new EE_Error( |
|
| 265 | - sprintf( |
|
| 266 | - __( |
|
| 267 | - // @codingStandardsIgnoreStart |
|
| 268 | - 'Cannot calculate registrations_checked_in_count because the database row %1$s does not have an entry for "Event_CPT.ID"', |
|
| 269 | - // @codingStandardsIgnoreEnd |
|
| 270 | - 'event_espresso' |
|
| 271 | - ), |
|
| 272 | - print_r($wpdb_row, true) |
|
| 273 | - ) |
|
| 274 | - ); |
|
| 275 | - } |
|
| 276 | - $this->verifyCurrentUserCan('ee_read_checkins', 'registrations_checked_in_count'); |
|
| 277 | - return $this->registration_model->count_registrations_checked_into_event($wpdb_row['Event_CPT.ID'], true); |
|
| 278 | - } |
|
| 279 | - |
|
| 280 | - |
|
| 281 | - /** |
|
| 282 | - * Counts all the registrations who have checked out of one of this events' datetimes |
|
| 283 | - * See EE_Event::total_available_spaces( false ); |
|
| 284 | - * |
|
| 285 | - * @param array $wpdb_row |
|
| 286 | - * @param WP_REST_Request $request |
|
| 287 | - * @param EventControllerBase $controller |
|
| 288 | - * @return int |
|
| 289 | - * @throws EE_Error |
|
| 290 | - * @throws InvalidArgumentException |
|
| 291 | - * @throws InvalidDataTypeException |
|
| 292 | - * @throws InvalidInterfaceException |
|
| 293 | - * @throws RestException |
|
| 294 | - */ |
|
| 295 | - public function registrationsCheckedOutCount($wpdb_row, $request, $controller) |
|
| 296 | - { |
|
| 297 | - if (! Event::wpdbRowHasEventId($wpdb_row)) { |
|
| 298 | - throw new EE_Error( |
|
| 299 | - sprintf( |
|
| 300 | - __( |
|
| 301 | - // @codingStandardsIgnoreStart |
|
| 302 | - 'Cannot calculate registrations_checked_out_count because the database row %1$s does not have an entry for "Event_CPT.ID"', |
|
| 303 | - // @codingStandardsIgnoreEnd |
|
| 304 | - 'event_espresso' |
|
| 305 | - ), |
|
| 306 | - print_r($wpdb_row, true) |
|
| 307 | - ) |
|
| 308 | - ); |
|
| 309 | - } |
|
| 310 | - $this->verifyCurrentUserCan('ee_read_checkins', 'registrations_checked_out_count'); |
|
| 311 | - return $this->registration_model->count_registrations_checked_into_event($wpdb_row['Event_CPT.ID'], false); |
|
| 312 | - } |
|
| 313 | - |
|
| 314 | - |
|
| 315 | - /** |
|
| 316 | - * Gets the thumbnail image |
|
| 317 | - * |
|
| 318 | - * @param array $wpdb_row |
|
| 319 | - * @param WP_REST_Request $request |
|
| 320 | - * @param EventControllerBase $controller |
|
| 321 | - * @return array |
|
| 322 | - * @throws EE_Error |
|
| 323 | - */ |
|
| 324 | - public function imageThumbnail($wpdb_row, $request, $controller) |
|
| 325 | - { |
|
| 326 | - return self::calculateImageData($wpdb_row, 'thumbnail'); |
|
| 327 | - } |
|
| 328 | - |
|
| 329 | - |
|
| 330 | - /** |
|
| 331 | - * Gets the medium image |
|
| 332 | - * |
|
| 333 | - * @param array $wpdb_row |
|
| 334 | - * @param WP_REST_Request $request |
|
| 335 | - * @param EventControllerBase $controller |
|
| 336 | - * @return array |
|
| 337 | - * @throws EE_Error |
|
| 338 | - */ |
|
| 339 | - public function imageMedium($wpdb_row, $request, $controller) |
|
| 340 | - { |
|
| 341 | - return self::calculateImageData($wpdb_row, 'medium'); |
|
| 342 | - } |
|
| 343 | - |
|
| 344 | - |
|
| 345 | - /** |
|
| 346 | - * Gets the medium-large image |
|
| 347 | - * |
|
| 348 | - * @param array $wpdb_row |
|
| 349 | - * @param WP_REST_Request $request |
|
| 350 | - * @param EventControllerBase $controller |
|
| 351 | - * @return array |
|
| 352 | - * @throws EE_Error |
|
| 353 | - */ |
|
| 354 | - public function imageMediumLarge($wpdb_row, $request, $controller) |
|
| 355 | - { |
|
| 356 | - return self::calculateImageData($wpdb_row, 'medium_large'); |
|
| 357 | - } |
|
| 358 | - |
|
| 359 | - |
|
| 360 | - /** |
|
| 361 | - * Gets the large image |
|
| 362 | - * |
|
| 363 | - * @param array $wpdb_row |
|
| 364 | - * @param WP_REST_Request $request |
|
| 365 | - * @param EventControllerBase $controller |
|
| 366 | - * @return array |
|
| 367 | - * @throws EE_Error |
|
| 368 | - */ |
|
| 369 | - public function imageLarge($wpdb_row, $request, $controller) |
|
| 370 | - { |
|
| 371 | - return self::calculateImageData($wpdb_row, 'large'); |
|
| 372 | - } |
|
| 373 | - |
|
| 374 | - |
|
| 375 | - /** |
|
| 376 | - * Gets the post-thumbnail image |
|
| 377 | - * |
|
| 378 | - * @param array $wpdb_row |
|
| 379 | - * @param WP_REST_Request $request |
|
| 380 | - * @param EventControllerBase $controller |
|
| 381 | - * @return array |
|
| 382 | - * @throws EE_Error |
|
| 383 | - */ |
|
| 384 | - public function imagePostThumbnail($wpdb_row, $request, $controller) |
|
| 385 | - { |
|
| 386 | - return self::calculateImageData($wpdb_row, 'post-thumbnail'); |
|
| 387 | - } |
|
| 388 | - |
|
| 389 | - |
|
| 390 | - /** |
|
| 391 | - * Gets the full size image |
|
| 392 | - * |
|
| 393 | - * @param array $wpdb_row |
|
| 394 | - * @param WP_REST_Request $request |
|
| 395 | - * @param EventControllerBase $controller |
|
| 396 | - * @return array |
|
| 397 | - * @throws EE_Error |
|
| 398 | - */ |
|
| 399 | - public function imageFull($wpdb_row, $request, $controller) |
|
| 400 | - { |
|
| 401 | - return self::calculateImageData($wpdb_row, 'full'); |
|
| 402 | - } |
|
| 403 | - |
|
| 404 | - |
|
| 405 | - /** |
|
| 406 | - * Gets image specs and formats them for the display in the API, |
|
| 407 | - * according to the image size requested |
|
| 408 | - * |
|
| 409 | - * @param array $wpdb_row |
|
| 410 | - * @param string $image_size one of these: thumbnail, medium, medium_large, large, post-thumbnail, full |
|
| 411 | - * @return array|false if no such image exists. If array it will have keys 'url', 'width', 'height' and 'original' |
|
| 412 | - * @throws EE_Error |
|
| 413 | - */ |
|
| 414 | - protected function calculateImageData($wpdb_row, $image_size) |
|
| 415 | - { |
|
| 416 | - if (! Event::wpdbRowHasEventId($wpdb_row)) { |
|
| 417 | - throw new EE_Error( |
|
| 418 | - sprintf( |
|
| 419 | - __( |
|
| 420 | - // @codingStandardsIgnoreStart |
|
| 421 | - 'Cannot calculate image because the database row %1$s does not have an entry for "Event_CPT.ID"', |
|
| 422 | - // @codingStandardsIgnoreEnd |
|
| 423 | - 'event_espresso' |
|
| 424 | - ), |
|
| 425 | - print_r($wpdb_row, true) |
|
| 426 | - ) |
|
| 427 | - ); |
|
| 428 | - } |
|
| 429 | - $EVT_ID = $wpdb_row['Event_CPT.ID']; |
|
| 430 | - $attachment_id = get_post_thumbnail_id($EVT_ID); |
|
| 431 | - $data = wp_get_attachment_image_src($attachment_id, $image_size); |
|
| 432 | - if (! $data) { |
|
| 433 | - return null; |
|
| 434 | - } |
|
| 435 | - $generated = true; |
|
| 436 | - if (isset($data[3])) { |
|
| 437 | - $generated = $data[3]; |
|
| 438 | - } |
|
| 439 | - return array( |
|
| 440 | - 'url' => $data[0], |
|
| 441 | - 'width' => $data[1], |
|
| 442 | - 'height' => $data[2], |
|
| 443 | - 'generated' => $generated, |
|
| 444 | - ); |
|
| 445 | - } |
|
| 446 | - |
|
| 447 | - |
|
| 448 | - /** |
|
| 449 | - * Returns true if the array of data contains 'Event_CPT.ID'. False otherwise |
|
| 450 | - * |
|
| 451 | - * @param array $wpdb_row |
|
| 452 | - * @return bool |
|
| 453 | - */ |
|
| 454 | - protected function wpdbRowHasEventId($wpdb_row) |
|
| 455 | - { |
|
| 456 | - return (is_array($wpdb_row) && isset($wpdb_row['Event_CPT.ID']) && absint($wpdb_row['Event_CPT.ID'])); |
|
| 457 | - } |
|
| 458 | - |
|
| 459 | - |
|
| 460 | - /** |
|
| 461 | - * Provides an array for all the calculations possible that outlines a json schema for those calculations. |
|
| 462 | - * Array is indexed by calculation (snake case) and value is the schema for that calculation. |
|
| 463 | - * |
|
| 464 | - * @since $VID:$ |
|
| 465 | - * @return array |
|
| 466 | - */ |
|
| 467 | - public function schemaForCalculations() |
|
| 468 | - { |
|
| 469 | - $image_object_properties = array( |
|
| 470 | - 'url' => array( |
|
| 471 | - 'type' => 'string', |
|
| 472 | - ), |
|
| 473 | - 'width' => array( |
|
| 474 | - 'type' => 'number', |
|
| 475 | - ), |
|
| 476 | - 'height' => array( |
|
| 477 | - 'type' => 'number', |
|
| 478 | - ), |
|
| 479 | - 'generated' => array( |
|
| 480 | - 'type' => 'boolean', |
|
| 481 | - ), |
|
| 482 | - ); |
|
| 483 | - return array( |
|
| 484 | - 'optimum_sales_at_start' => array( |
|
| 485 | - 'description' => esc_html__( |
|
| 486 | - 'The total spaces on the event (not subtracting sales, but taking sales into account; so this is the optimum sales that CAN still be achieved.', |
|
| 487 | - 'event_espresso' |
|
| 488 | - ), |
|
| 489 | - 'type' => 'number', |
|
| 490 | - ), |
|
| 491 | - 'optimum_sales_now' => array( |
|
| 492 | - 'description' => esc_html__( |
|
| 493 | - 'The total spaces on the event (ignoring all sales; so this is the optimum sales that could have been achieved.', |
|
| 494 | - 'event_espresso' |
|
| 495 | - ), |
|
| 496 | - 'type' => 'number', |
|
| 497 | - ), |
|
| 498 | - 'spaces_remaining' => array( |
|
| 499 | - 'description' => esc_html__( |
|
| 500 | - 'The optimum_sales_number result, minus total sales so far.', |
|
| 501 | - 'event_espresso' |
|
| 502 | - ), |
|
| 503 | - 'type' => 'number', |
|
| 504 | - ), |
|
| 505 | - 'spots_taken' => array( |
|
| 506 | - 'description' => esc_html__( |
|
| 507 | - 'The number of approved registrations for this event (regardless of how many datetimes each registration\'s ticket purchase is for)', |
|
| 508 | - 'event_espresso' |
|
| 509 | - ), |
|
| 510 | - 'type' => 'number', |
|
| 511 | - ), |
|
| 512 | - 'spots_taken_pending_payment' => array( |
|
| 513 | - 'description' => esc_html__( |
|
| 514 | - 'The number of pending-payment registrations for this event (regardless of how many datetimes each registration\'s ticket purchase is for)', |
|
| 515 | - 'event_espresso' |
|
| 516 | - ), |
|
| 517 | - 'type' => 'number', |
|
| 518 | - ), |
|
| 519 | - 'registrations_checked_in_count' => array( |
|
| 520 | - 'description' => esc_html__( |
|
| 521 | - 'The count of all the registrations who have checked into one of this event\'s datetimes.', |
|
| 522 | - 'event_espresso' |
|
| 523 | - ), |
|
| 524 | - 'type' => 'number', |
|
| 525 | - ), |
|
| 526 | - 'registrations_checked_out_count' => array( |
|
| 527 | - 'description' => esc_html__( |
|
| 528 | - 'The count of all registrations who have checked out of one of this event\'s datetimes.', |
|
| 529 | - 'event_espresso' |
|
| 530 | - ), |
|
| 531 | - 'type' => 'number', |
|
| 532 | - ), |
|
| 533 | - 'image_thumbnail' => array( |
|
| 534 | - 'description' => esc_html__( |
|
| 535 | - 'The thumbnail image data.', |
|
| 536 | - 'event_espresso' |
|
| 537 | - ), |
|
| 538 | - 'type' => 'object', |
|
| 539 | - 'properties' => $image_object_properties, |
|
| 540 | - 'additionalProperties' => false, |
|
| 541 | - ), |
|
| 542 | - 'image_medium' => array( |
|
| 543 | - 'description' => esc_html__( |
|
| 544 | - 'The medium image data.', |
|
| 545 | - 'event_espresso' |
|
| 546 | - ), |
|
| 547 | - 'type' => 'object', |
|
| 548 | - 'properties' => $image_object_properties, |
|
| 549 | - 'additionalProperties' => false, |
|
| 550 | - ), |
|
| 551 | - 'image_medium_large' => array( |
|
| 552 | - 'description' => esc_html__( |
|
| 553 | - 'The medium-large image data.', |
|
| 554 | - 'event_espresso' |
|
| 555 | - ), |
|
| 556 | - 'type' => 'object', |
|
| 557 | - 'properties' => $image_object_properties, |
|
| 558 | - 'additionalProperties' => false, |
|
| 559 | - ), |
|
| 560 | - 'image_large' => array( |
|
| 561 | - 'description' => esc_html__( |
|
| 562 | - 'The large image data.', |
|
| 563 | - 'event_espresso' |
|
| 564 | - ), |
|
| 565 | - 'type' => 'object', |
|
| 566 | - 'properties' => $image_object_properties, |
|
| 567 | - 'additionalProperties' => false, |
|
| 568 | - ), |
|
| 569 | - 'image_post_thumbnail' => array( |
|
| 570 | - 'description' => esc_html__( |
|
| 571 | - 'The post-thumbnail image data.', |
|
| 572 | - 'event_espresso' |
|
| 573 | - ), |
|
| 574 | - 'type' => 'object', |
|
| 575 | - 'properties' => $image_object_properties, |
|
| 576 | - 'additionalProperties' => false, |
|
| 577 | - ), |
|
| 578 | - 'image_full' => array( |
|
| 579 | - 'description' => esc_html__( |
|
| 580 | - 'The full size image data', |
|
| 581 | - 'event_espresso' |
|
| 582 | - ), |
|
| 583 | - 'type' => 'object', |
|
| 584 | - 'properties' => $image_object_properties, |
|
| 585 | - 'additionalProperties' => false, |
|
| 586 | - ), |
|
| 587 | - ); |
|
| 588 | - } |
|
| 29 | + /** |
|
| 30 | + * @var EEM_Event |
|
| 31 | + */ |
|
| 32 | + protected $event_model; |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * @var EEM_Registration |
|
| 36 | + */ |
|
| 37 | + protected $registration_model; |
|
| 38 | + public function __construct(EEM_Event $event_model, EEM_Registration $registration_model) |
|
| 39 | + { |
|
| 40 | + $this->event_model = $event_model; |
|
| 41 | + $this->registration_model = $registration_model; |
|
| 42 | + } |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * Calculates the total spaces on the event (not subtracting sales, but taking |
|
| 46 | + * sales into account; so this is the optimum sales that CAN still be achieved) |
|
| 47 | + * See EE_Event::total_available_spaces( true ); |
|
| 48 | + * |
|
| 49 | + * @param array $wpdb_row |
|
| 50 | + * @param WP_REST_Request $request |
|
| 51 | + * @param EventControllerBase $controller |
|
| 52 | + * @return int |
|
| 53 | + * @throws EE_Error |
|
| 54 | + * @throws DomainException |
|
| 55 | + * @throws InvalidDataTypeException |
|
| 56 | + * @throws InvalidInterfaceException |
|
| 57 | + * @throws UnexpectedEntityException |
|
| 58 | + * @throws InvalidArgumentException |
|
| 59 | + */ |
|
| 60 | + public function optimumSalesAtStart($wpdb_row, $request, $controller) |
|
| 61 | + { |
|
| 62 | + $event_obj = null; |
|
| 63 | + if (Event::wpdbRowHasEventId($wpdb_row)) { |
|
| 64 | + $event_obj = $this->event_model->get_one_by_ID($wpdb_row['Event_CPT.ID']); |
|
| 65 | + } |
|
| 66 | + if ($event_obj instanceof EE_Event) { |
|
| 67 | + return $event_obj->total_available_spaces(); |
|
| 68 | + } |
|
| 69 | + throw new EE_Error( |
|
| 70 | + sprintf( |
|
| 71 | + __( |
|
| 72 | + // @codingStandardsIgnoreStart |
|
| 73 | + 'Cannot calculate optimum_sales_at_start because the event with ID %1$s (from database row %2$s) was not found', |
|
| 74 | + // @codingStandardsIgnoreEnd |
|
| 75 | + 'event_espresso' |
|
| 76 | + ), |
|
| 77 | + $wpdb_row['Event_CPT.ID'], |
|
| 78 | + print_r($wpdb_row, true) |
|
| 79 | + ) |
|
| 80 | + ); |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * Calculates the total spaces on the event (ignoring all sales; so this is the optimum |
|
| 86 | + * sales that COULD have been achieved) |
|
| 87 | + * See EE_Event::total_available_spaces( true ); |
|
| 88 | + * |
|
| 89 | + * @param array $wpdb_row |
|
| 90 | + * @param WP_REST_Request $request |
|
| 91 | + * @param EventControllerBase $controller |
|
| 92 | + * @return int |
|
| 93 | + * @throws DomainException |
|
| 94 | + * @throws EE_Error |
|
| 95 | + * @throws InvalidArgumentException |
|
| 96 | + * @throws InvalidDataTypeException |
|
| 97 | + * @throws InvalidInterfaceException |
|
| 98 | + * @throws UnexpectedEntityException |
|
| 99 | + */ |
|
| 100 | + public function optimumSalesNow($wpdb_row, $request, $controller) |
|
| 101 | + { |
|
| 102 | + $event_obj = null; |
|
| 103 | + if (Event::wpdbRowHasEventId($wpdb_row)) { |
|
| 104 | + $event_obj = $this->event_model->get_one_by_ID($wpdb_row['Event_CPT.ID']); |
|
| 105 | + } |
|
| 106 | + if ($event_obj instanceof EE_Event) { |
|
| 107 | + return $event_obj->total_available_spaces(true); |
|
| 108 | + } |
|
| 109 | + throw new EE_Error( |
|
| 110 | + sprintf( |
|
| 111 | + __( |
|
| 112 | + // @codingStandardsIgnoreStart |
|
| 113 | + 'Cannot calculate optimum_sales_now because the event with ID %1$s (from database row %2$s) was not found', |
|
| 114 | + // @codingStandardsIgnoreEnd |
|
| 115 | + 'event_espresso' |
|
| 116 | + ), |
|
| 117 | + $wpdb_row['Event_CPT.ID'], |
|
| 118 | + print_r($wpdb_row, true) |
|
| 119 | + ) |
|
| 120 | + ); |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * Like optimum_sales_now, but minus total sales so far. |
|
| 126 | + * See EE_Event::spaces_remaining_for_sale( true ); |
|
| 127 | + * |
|
| 128 | + * @param array $wpdb_row |
|
| 129 | + * @param WP_REST_Request $request |
|
| 130 | + * @param EventControllerBase $controller |
|
| 131 | + * @return int |
|
| 132 | + * @throws DomainException |
|
| 133 | + * @throws EE_Error |
|
| 134 | + * @throws InvalidArgumentException |
|
| 135 | + * @throws InvalidDataTypeException |
|
| 136 | + * @throws InvalidInterfaceException |
|
| 137 | + * @throws UnexpectedEntityException |
|
| 138 | + */ |
|
| 139 | + public function spacesRemaining($wpdb_row, $request, $controller) |
|
| 140 | + { |
|
| 141 | + $event_obj = null; |
|
| 142 | + if (Event::wpdbRowHasEventId($wpdb_row)) { |
|
| 143 | + $event_obj = $this->event_model->get_one_by_ID($wpdb_row['Event_CPT.ID']); |
|
| 144 | + } |
|
| 145 | + if ($event_obj instanceof EE_Event) { |
|
| 146 | + return $event_obj->spaces_remaining_for_sale(); |
|
| 147 | + } |
|
| 148 | + throw new EE_Error( |
|
| 149 | + sprintf( |
|
| 150 | + __( |
|
| 151 | + // @codingStandardsIgnoreStart |
|
| 152 | + 'Cannot calculate spaces_remaining because the event with ID %1$s (from database row %2$s) was not found', |
|
| 153 | + // @codingStandardsIgnoreEnd |
|
| 154 | + 'event_espresso' |
|
| 155 | + ), |
|
| 156 | + $wpdb_row['Event_CPT.ID'], |
|
| 157 | + print_r($wpdb_row, true) |
|
| 158 | + ) |
|
| 159 | + ); |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + |
|
| 163 | + /** |
|
| 164 | + * Counts the number of approved registrations for this event (regardless |
|
| 165 | + * of how many datetimes each registrations' ticket purchase is for) |
|
| 166 | + * |
|
| 167 | + * @param array $wpdb_row |
|
| 168 | + * @param WP_REST_Request $request |
|
| 169 | + * @param EventControllerBase $controller |
|
| 170 | + * @return int |
|
| 171 | + * @throws EE_Error |
|
| 172 | + * @throws InvalidArgumentException |
|
| 173 | + * @throws InvalidDataTypeException |
|
| 174 | + * @throws InvalidInterfaceException |
|
| 175 | + */ |
|
| 176 | + public function spotsTaken($wpdb_row, $request, $controller) |
|
| 177 | + { |
|
| 178 | + if (! Event::wpdbRowHasEventId($wpdb_row)) { |
|
| 179 | + throw new EE_Error( |
|
| 180 | + sprintf( |
|
| 181 | + __( |
|
| 182 | + // @codingStandardsIgnoreStart |
|
| 183 | + 'Cannot calculate spots_taken because the database row %1$s does not have a valid entry for "Event_CPT.ID"', |
|
| 184 | + // @codingStandardsIgnoreEnd |
|
| 185 | + 'event_espresso' |
|
| 186 | + ), |
|
| 187 | + print_r($wpdb_row, true) |
|
| 188 | + ) |
|
| 189 | + ); |
|
| 190 | + } |
|
| 191 | + return $this->registration_model->count( |
|
| 192 | + array( |
|
| 193 | + array( |
|
| 194 | + 'EVT_ID' => $wpdb_row['Event_CPT.ID'], |
|
| 195 | + 'STS_ID' => EEM_Registration::status_id_approved, |
|
| 196 | + ), |
|
| 197 | + ), |
|
| 198 | + 'REG_ID', |
|
| 199 | + true |
|
| 200 | + ); |
|
| 201 | + } |
|
| 202 | + |
|
| 203 | + |
|
| 204 | + /** |
|
| 205 | + * Counts the number of pending-payment registrations for this event (regardless |
|
| 206 | + * of how many datetimes each registrations' ticket purchase is for) |
|
| 207 | + * |
|
| 208 | + * @param array $wpdb_row |
|
| 209 | + * @param WP_REST_Request $request |
|
| 210 | + * @param EventControllerBase $controller |
|
| 211 | + * @return int |
|
| 212 | + * @throws EE_Error |
|
| 213 | + * @throws InvalidArgumentException |
|
| 214 | + * @throws InvalidDataTypeException |
|
| 215 | + * @throws InvalidInterfaceException |
|
| 216 | + * @throws RestException |
|
| 217 | + */ |
|
| 218 | + public function spotsTakenPendingPayment($wpdb_row, $request, $controller) |
|
| 219 | + { |
|
| 220 | + if (! Event::wpdbRowHasEventId($wpdb_row)) { |
|
| 221 | + throw new EE_Error( |
|
| 222 | + sprintf( |
|
| 223 | + __( |
|
| 224 | + // @codingStandardsIgnoreStart |
|
| 225 | + 'Cannot calculate spots_taken_pending_payment because the database row %1$s does not have an entry for "Event_CPT.ID"', |
|
| 226 | + // @codingStandardsIgnoreEnd |
|
| 227 | + 'event_espresso' |
|
| 228 | + ), |
|
| 229 | + print_r($wpdb_row, true) |
|
| 230 | + ) |
|
| 231 | + ); |
|
| 232 | + } |
|
| 233 | + $this->verifyCurrentUserCan('ee_read_registrations', 'spots_taken_pending_payment'); |
|
| 234 | + return $this->registration_model->count( |
|
| 235 | + array( |
|
| 236 | + array( |
|
| 237 | + 'EVT_ID' => $wpdb_row['Event_CPT.ID'], |
|
| 238 | + 'STS_ID' => EEM_Registration::status_id_pending_payment, |
|
| 239 | + ), |
|
| 240 | + ), |
|
| 241 | + 'REG_ID', |
|
| 242 | + true |
|
| 243 | + ); |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + |
|
| 247 | + /** |
|
| 248 | + * Counts all the registrations who have checked into one of this events' datetimes |
|
| 249 | + * See EE_Event::total_available_spaces( false ); |
|
| 250 | + * |
|
| 251 | + * @param array $wpdb_row |
|
| 252 | + * @param WP_REST_Request $request |
|
| 253 | + * @param EventControllerBase $controller |
|
| 254 | + * @return int|null if permission denied |
|
| 255 | + * @throws EE_Error |
|
| 256 | + * @throws InvalidArgumentException |
|
| 257 | + * @throws InvalidDataTypeException |
|
| 258 | + * @throws InvalidInterfaceException |
|
| 259 | + * @throws RestException |
|
| 260 | + */ |
|
| 261 | + public function registrationsCheckedInCount($wpdb_row, $request, $controller) |
|
| 262 | + { |
|
| 263 | + if (! Event::wpdbRowHasEventId($wpdb_row)) { |
|
| 264 | + throw new EE_Error( |
|
| 265 | + sprintf( |
|
| 266 | + __( |
|
| 267 | + // @codingStandardsIgnoreStart |
|
| 268 | + 'Cannot calculate registrations_checked_in_count because the database row %1$s does not have an entry for "Event_CPT.ID"', |
|
| 269 | + // @codingStandardsIgnoreEnd |
|
| 270 | + 'event_espresso' |
|
| 271 | + ), |
|
| 272 | + print_r($wpdb_row, true) |
|
| 273 | + ) |
|
| 274 | + ); |
|
| 275 | + } |
|
| 276 | + $this->verifyCurrentUserCan('ee_read_checkins', 'registrations_checked_in_count'); |
|
| 277 | + return $this->registration_model->count_registrations_checked_into_event($wpdb_row['Event_CPT.ID'], true); |
|
| 278 | + } |
|
| 279 | + |
|
| 280 | + |
|
| 281 | + /** |
|
| 282 | + * Counts all the registrations who have checked out of one of this events' datetimes |
|
| 283 | + * See EE_Event::total_available_spaces( false ); |
|
| 284 | + * |
|
| 285 | + * @param array $wpdb_row |
|
| 286 | + * @param WP_REST_Request $request |
|
| 287 | + * @param EventControllerBase $controller |
|
| 288 | + * @return int |
|
| 289 | + * @throws EE_Error |
|
| 290 | + * @throws InvalidArgumentException |
|
| 291 | + * @throws InvalidDataTypeException |
|
| 292 | + * @throws InvalidInterfaceException |
|
| 293 | + * @throws RestException |
|
| 294 | + */ |
|
| 295 | + public function registrationsCheckedOutCount($wpdb_row, $request, $controller) |
|
| 296 | + { |
|
| 297 | + if (! Event::wpdbRowHasEventId($wpdb_row)) { |
|
| 298 | + throw new EE_Error( |
|
| 299 | + sprintf( |
|
| 300 | + __( |
|
| 301 | + // @codingStandardsIgnoreStart |
|
| 302 | + 'Cannot calculate registrations_checked_out_count because the database row %1$s does not have an entry for "Event_CPT.ID"', |
|
| 303 | + // @codingStandardsIgnoreEnd |
|
| 304 | + 'event_espresso' |
|
| 305 | + ), |
|
| 306 | + print_r($wpdb_row, true) |
|
| 307 | + ) |
|
| 308 | + ); |
|
| 309 | + } |
|
| 310 | + $this->verifyCurrentUserCan('ee_read_checkins', 'registrations_checked_out_count'); |
|
| 311 | + return $this->registration_model->count_registrations_checked_into_event($wpdb_row['Event_CPT.ID'], false); |
|
| 312 | + } |
|
| 313 | + |
|
| 314 | + |
|
| 315 | + /** |
|
| 316 | + * Gets the thumbnail image |
|
| 317 | + * |
|
| 318 | + * @param array $wpdb_row |
|
| 319 | + * @param WP_REST_Request $request |
|
| 320 | + * @param EventControllerBase $controller |
|
| 321 | + * @return array |
|
| 322 | + * @throws EE_Error |
|
| 323 | + */ |
|
| 324 | + public function imageThumbnail($wpdb_row, $request, $controller) |
|
| 325 | + { |
|
| 326 | + return self::calculateImageData($wpdb_row, 'thumbnail'); |
|
| 327 | + } |
|
| 328 | + |
|
| 329 | + |
|
| 330 | + /** |
|
| 331 | + * Gets the medium image |
|
| 332 | + * |
|
| 333 | + * @param array $wpdb_row |
|
| 334 | + * @param WP_REST_Request $request |
|
| 335 | + * @param EventControllerBase $controller |
|
| 336 | + * @return array |
|
| 337 | + * @throws EE_Error |
|
| 338 | + */ |
|
| 339 | + public function imageMedium($wpdb_row, $request, $controller) |
|
| 340 | + { |
|
| 341 | + return self::calculateImageData($wpdb_row, 'medium'); |
|
| 342 | + } |
|
| 343 | + |
|
| 344 | + |
|
| 345 | + /** |
|
| 346 | + * Gets the medium-large image |
|
| 347 | + * |
|
| 348 | + * @param array $wpdb_row |
|
| 349 | + * @param WP_REST_Request $request |
|
| 350 | + * @param EventControllerBase $controller |
|
| 351 | + * @return array |
|
| 352 | + * @throws EE_Error |
|
| 353 | + */ |
|
| 354 | + public function imageMediumLarge($wpdb_row, $request, $controller) |
|
| 355 | + { |
|
| 356 | + return self::calculateImageData($wpdb_row, 'medium_large'); |
|
| 357 | + } |
|
| 358 | + |
|
| 359 | + |
|
| 360 | + /** |
|
| 361 | + * Gets the large image |
|
| 362 | + * |
|
| 363 | + * @param array $wpdb_row |
|
| 364 | + * @param WP_REST_Request $request |
|
| 365 | + * @param EventControllerBase $controller |
|
| 366 | + * @return array |
|
| 367 | + * @throws EE_Error |
|
| 368 | + */ |
|
| 369 | + public function imageLarge($wpdb_row, $request, $controller) |
|
| 370 | + { |
|
| 371 | + return self::calculateImageData($wpdb_row, 'large'); |
|
| 372 | + } |
|
| 373 | + |
|
| 374 | + |
|
| 375 | + /** |
|
| 376 | + * Gets the post-thumbnail image |
|
| 377 | + * |
|
| 378 | + * @param array $wpdb_row |
|
| 379 | + * @param WP_REST_Request $request |
|
| 380 | + * @param EventControllerBase $controller |
|
| 381 | + * @return array |
|
| 382 | + * @throws EE_Error |
|
| 383 | + */ |
|
| 384 | + public function imagePostThumbnail($wpdb_row, $request, $controller) |
|
| 385 | + { |
|
| 386 | + return self::calculateImageData($wpdb_row, 'post-thumbnail'); |
|
| 387 | + } |
|
| 388 | + |
|
| 389 | + |
|
| 390 | + /** |
|
| 391 | + * Gets the full size image |
|
| 392 | + * |
|
| 393 | + * @param array $wpdb_row |
|
| 394 | + * @param WP_REST_Request $request |
|
| 395 | + * @param EventControllerBase $controller |
|
| 396 | + * @return array |
|
| 397 | + * @throws EE_Error |
|
| 398 | + */ |
|
| 399 | + public function imageFull($wpdb_row, $request, $controller) |
|
| 400 | + { |
|
| 401 | + return self::calculateImageData($wpdb_row, 'full'); |
|
| 402 | + } |
|
| 403 | + |
|
| 404 | + |
|
| 405 | + /** |
|
| 406 | + * Gets image specs and formats them for the display in the API, |
|
| 407 | + * according to the image size requested |
|
| 408 | + * |
|
| 409 | + * @param array $wpdb_row |
|
| 410 | + * @param string $image_size one of these: thumbnail, medium, medium_large, large, post-thumbnail, full |
|
| 411 | + * @return array|false if no such image exists. If array it will have keys 'url', 'width', 'height' and 'original' |
|
| 412 | + * @throws EE_Error |
|
| 413 | + */ |
|
| 414 | + protected function calculateImageData($wpdb_row, $image_size) |
|
| 415 | + { |
|
| 416 | + if (! Event::wpdbRowHasEventId($wpdb_row)) { |
|
| 417 | + throw new EE_Error( |
|
| 418 | + sprintf( |
|
| 419 | + __( |
|
| 420 | + // @codingStandardsIgnoreStart |
|
| 421 | + 'Cannot calculate image because the database row %1$s does not have an entry for "Event_CPT.ID"', |
|
| 422 | + // @codingStandardsIgnoreEnd |
|
| 423 | + 'event_espresso' |
|
| 424 | + ), |
|
| 425 | + print_r($wpdb_row, true) |
|
| 426 | + ) |
|
| 427 | + ); |
|
| 428 | + } |
|
| 429 | + $EVT_ID = $wpdb_row['Event_CPT.ID']; |
|
| 430 | + $attachment_id = get_post_thumbnail_id($EVT_ID); |
|
| 431 | + $data = wp_get_attachment_image_src($attachment_id, $image_size); |
|
| 432 | + if (! $data) { |
|
| 433 | + return null; |
|
| 434 | + } |
|
| 435 | + $generated = true; |
|
| 436 | + if (isset($data[3])) { |
|
| 437 | + $generated = $data[3]; |
|
| 438 | + } |
|
| 439 | + return array( |
|
| 440 | + 'url' => $data[0], |
|
| 441 | + 'width' => $data[1], |
|
| 442 | + 'height' => $data[2], |
|
| 443 | + 'generated' => $generated, |
|
| 444 | + ); |
|
| 445 | + } |
|
| 446 | + |
|
| 447 | + |
|
| 448 | + /** |
|
| 449 | + * Returns true if the array of data contains 'Event_CPT.ID'. False otherwise |
|
| 450 | + * |
|
| 451 | + * @param array $wpdb_row |
|
| 452 | + * @return bool |
|
| 453 | + */ |
|
| 454 | + protected function wpdbRowHasEventId($wpdb_row) |
|
| 455 | + { |
|
| 456 | + return (is_array($wpdb_row) && isset($wpdb_row['Event_CPT.ID']) && absint($wpdb_row['Event_CPT.ID'])); |
|
| 457 | + } |
|
| 458 | + |
|
| 459 | + |
|
| 460 | + /** |
|
| 461 | + * Provides an array for all the calculations possible that outlines a json schema for those calculations. |
|
| 462 | + * Array is indexed by calculation (snake case) and value is the schema for that calculation. |
|
| 463 | + * |
|
| 464 | + * @since $VID:$ |
|
| 465 | + * @return array |
|
| 466 | + */ |
|
| 467 | + public function schemaForCalculations() |
|
| 468 | + { |
|
| 469 | + $image_object_properties = array( |
|
| 470 | + 'url' => array( |
|
| 471 | + 'type' => 'string', |
|
| 472 | + ), |
|
| 473 | + 'width' => array( |
|
| 474 | + 'type' => 'number', |
|
| 475 | + ), |
|
| 476 | + 'height' => array( |
|
| 477 | + 'type' => 'number', |
|
| 478 | + ), |
|
| 479 | + 'generated' => array( |
|
| 480 | + 'type' => 'boolean', |
|
| 481 | + ), |
|
| 482 | + ); |
|
| 483 | + return array( |
|
| 484 | + 'optimum_sales_at_start' => array( |
|
| 485 | + 'description' => esc_html__( |
|
| 486 | + 'The total spaces on the event (not subtracting sales, but taking sales into account; so this is the optimum sales that CAN still be achieved.', |
|
| 487 | + 'event_espresso' |
|
| 488 | + ), |
|
| 489 | + 'type' => 'number', |
|
| 490 | + ), |
|
| 491 | + 'optimum_sales_now' => array( |
|
| 492 | + 'description' => esc_html__( |
|
| 493 | + 'The total spaces on the event (ignoring all sales; so this is the optimum sales that could have been achieved.', |
|
| 494 | + 'event_espresso' |
|
| 495 | + ), |
|
| 496 | + 'type' => 'number', |
|
| 497 | + ), |
|
| 498 | + 'spaces_remaining' => array( |
|
| 499 | + 'description' => esc_html__( |
|
| 500 | + 'The optimum_sales_number result, minus total sales so far.', |
|
| 501 | + 'event_espresso' |
|
| 502 | + ), |
|
| 503 | + 'type' => 'number', |
|
| 504 | + ), |
|
| 505 | + 'spots_taken' => array( |
|
| 506 | + 'description' => esc_html__( |
|
| 507 | + 'The number of approved registrations for this event (regardless of how many datetimes each registration\'s ticket purchase is for)', |
|
| 508 | + 'event_espresso' |
|
| 509 | + ), |
|
| 510 | + 'type' => 'number', |
|
| 511 | + ), |
|
| 512 | + 'spots_taken_pending_payment' => array( |
|
| 513 | + 'description' => esc_html__( |
|
| 514 | + 'The number of pending-payment registrations for this event (regardless of how many datetimes each registration\'s ticket purchase is for)', |
|
| 515 | + 'event_espresso' |
|
| 516 | + ), |
|
| 517 | + 'type' => 'number', |
|
| 518 | + ), |
|
| 519 | + 'registrations_checked_in_count' => array( |
|
| 520 | + 'description' => esc_html__( |
|
| 521 | + 'The count of all the registrations who have checked into one of this event\'s datetimes.', |
|
| 522 | + 'event_espresso' |
|
| 523 | + ), |
|
| 524 | + 'type' => 'number', |
|
| 525 | + ), |
|
| 526 | + 'registrations_checked_out_count' => array( |
|
| 527 | + 'description' => esc_html__( |
|
| 528 | + 'The count of all registrations who have checked out of one of this event\'s datetimes.', |
|
| 529 | + 'event_espresso' |
|
| 530 | + ), |
|
| 531 | + 'type' => 'number', |
|
| 532 | + ), |
|
| 533 | + 'image_thumbnail' => array( |
|
| 534 | + 'description' => esc_html__( |
|
| 535 | + 'The thumbnail image data.', |
|
| 536 | + 'event_espresso' |
|
| 537 | + ), |
|
| 538 | + 'type' => 'object', |
|
| 539 | + 'properties' => $image_object_properties, |
|
| 540 | + 'additionalProperties' => false, |
|
| 541 | + ), |
|
| 542 | + 'image_medium' => array( |
|
| 543 | + 'description' => esc_html__( |
|
| 544 | + 'The medium image data.', |
|
| 545 | + 'event_espresso' |
|
| 546 | + ), |
|
| 547 | + 'type' => 'object', |
|
| 548 | + 'properties' => $image_object_properties, |
|
| 549 | + 'additionalProperties' => false, |
|
| 550 | + ), |
|
| 551 | + 'image_medium_large' => array( |
|
| 552 | + 'description' => esc_html__( |
|
| 553 | + 'The medium-large image data.', |
|
| 554 | + 'event_espresso' |
|
| 555 | + ), |
|
| 556 | + 'type' => 'object', |
|
| 557 | + 'properties' => $image_object_properties, |
|
| 558 | + 'additionalProperties' => false, |
|
| 559 | + ), |
|
| 560 | + 'image_large' => array( |
|
| 561 | + 'description' => esc_html__( |
|
| 562 | + 'The large image data.', |
|
| 563 | + 'event_espresso' |
|
| 564 | + ), |
|
| 565 | + 'type' => 'object', |
|
| 566 | + 'properties' => $image_object_properties, |
|
| 567 | + 'additionalProperties' => false, |
|
| 568 | + ), |
|
| 569 | + 'image_post_thumbnail' => array( |
|
| 570 | + 'description' => esc_html__( |
|
| 571 | + 'The post-thumbnail image data.', |
|
| 572 | + 'event_espresso' |
|
| 573 | + ), |
|
| 574 | + 'type' => 'object', |
|
| 575 | + 'properties' => $image_object_properties, |
|
| 576 | + 'additionalProperties' => false, |
|
| 577 | + ), |
|
| 578 | + 'image_full' => array( |
|
| 579 | + 'description' => esc_html__( |
|
| 580 | + 'The full size image data', |
|
| 581 | + 'event_espresso' |
|
| 582 | + ), |
|
| 583 | + 'type' => 'object', |
|
| 584 | + 'properties' => $image_object_properties, |
|
| 585 | + 'additionalProperties' => false, |
|
| 586 | + ), |
|
| 587 | + ); |
|
| 588 | + } |
|
| 589 | 589 | } |
@@ -175,7 +175,7 @@ discard block |
||
| 175 | 175 | */ |
| 176 | 176 | public function spotsTaken($wpdb_row, $request, $controller) |
| 177 | 177 | { |
| 178 | - if (! Event::wpdbRowHasEventId($wpdb_row)) { |
|
| 178 | + if ( ! Event::wpdbRowHasEventId($wpdb_row)) { |
|
| 179 | 179 | throw new EE_Error( |
| 180 | 180 | sprintf( |
| 181 | 181 | __( |
@@ -217,7 +217,7 @@ discard block |
||
| 217 | 217 | */ |
| 218 | 218 | public function spotsTakenPendingPayment($wpdb_row, $request, $controller) |
| 219 | 219 | { |
| 220 | - if (! Event::wpdbRowHasEventId($wpdb_row)) { |
|
| 220 | + if ( ! Event::wpdbRowHasEventId($wpdb_row)) { |
|
| 221 | 221 | throw new EE_Error( |
| 222 | 222 | sprintf( |
| 223 | 223 | __( |
@@ -260,7 +260,7 @@ discard block |
||
| 260 | 260 | */ |
| 261 | 261 | public function registrationsCheckedInCount($wpdb_row, $request, $controller) |
| 262 | 262 | { |
| 263 | - if (! Event::wpdbRowHasEventId($wpdb_row)) { |
|
| 263 | + if ( ! Event::wpdbRowHasEventId($wpdb_row)) { |
|
| 264 | 264 | throw new EE_Error( |
| 265 | 265 | sprintf( |
| 266 | 266 | __( |
@@ -294,7 +294,7 @@ discard block |
||
| 294 | 294 | */ |
| 295 | 295 | public function registrationsCheckedOutCount($wpdb_row, $request, $controller) |
| 296 | 296 | { |
| 297 | - if (! Event::wpdbRowHasEventId($wpdb_row)) { |
|
| 297 | + if ( ! Event::wpdbRowHasEventId($wpdb_row)) { |
|
| 298 | 298 | throw new EE_Error( |
| 299 | 299 | sprintf( |
| 300 | 300 | __( |
@@ -413,7 +413,7 @@ discard block |
||
| 413 | 413 | */ |
| 414 | 414 | protected function calculateImageData($wpdb_row, $image_size) |
| 415 | 415 | { |
| 416 | - if (! Event::wpdbRowHasEventId($wpdb_row)) { |
|
| 416 | + if ( ! Event::wpdbRowHasEventId($wpdb_row)) { |
|
| 417 | 417 | throw new EE_Error( |
| 418 | 418 | sprintf( |
| 419 | 419 | __( |
@@ -429,7 +429,7 @@ discard block |
||
| 429 | 429 | $EVT_ID = $wpdb_row['Event_CPT.ID']; |
| 430 | 430 | $attachment_id = get_post_thumbnail_id($EVT_ID); |
| 431 | 431 | $data = wp_get_attachment_image_src($attachment_id, $image_size); |
| 432 | - if (! $data) { |
|
| 432 | + if ( ! $data) { |
|
| 433 | 433 | return null; |
| 434 | 434 | } |
| 435 | 435 | $generated = true; |