|
1
|
|
|
<?php |
|
2
|
|
|
namespace EventEspresso\core\libraries\rest_api\controllers\model; |
|
3
|
|
|
|
|
4
|
|
|
use Exception; |
|
5
|
|
|
use EE_Boolean_Field; |
|
6
|
|
|
use EE_Maintenance_Mode; |
|
7
|
|
|
use EE_Registry; |
|
8
|
|
|
use EE_Serialized_Text_Field; |
|
9
|
|
|
use EED_Core_Rest_Api; |
|
10
|
|
|
use EEM_System_Status; |
|
11
|
|
|
use EventEspresso\core\libraries\rest_api\ModelDataTranslator; |
|
12
|
|
|
|
|
13
|
|
|
if (! defined('EVENT_ESPRESSO_VERSION')) { |
|
14
|
|
|
exit('No direct script access allowed'); |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Controller for handling requests regarding meta info about the models |
|
21
|
|
|
* Handles requests relating to meta info |
|
22
|
|
|
* |
|
23
|
|
|
* @package Event Espresso |
|
24
|
|
|
* @subpackage |
|
25
|
|
|
* @author Mike Nelson |
|
26
|
|
|
*/ |
|
27
|
|
|
class Meta extends Base |
|
28
|
|
|
{ |
|
29
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param \WP_REST_Request $request |
|
33
|
|
|
* @param string $version |
|
34
|
|
|
* @return array|\WP_REST_Response |
|
35
|
|
|
*/ |
|
36
|
|
|
public static function handleRequestModelsMeta(\WP_REST_Request $request, $version) |
|
37
|
|
|
{ |
|
38
|
|
|
$controller = new Meta(); |
|
39
|
|
|
try { |
|
40
|
|
|
$controller->setRequestedVersion($version); |
|
41
|
|
|
return $controller->sendResponse($controller->getModelsMetadataEntity()); |
|
42
|
|
|
} catch (Exception $e) { |
|
43
|
|
|
return $controller->sendResponse($e); |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
/* |
|
50
|
|
|
* Gets the model metadata resource entity |
|
51
|
|
|
* @return array for JSON response, describing all the models available in teh requested version |
|
52
|
|
|
*/ |
|
53
|
|
|
protected function getModelsMetadataEntity() |
|
54
|
|
|
{ |
|
55
|
|
|
$response = array(); |
|
56
|
|
|
foreach ($this->getModelVersionInfo()->modelsForRequestedVersion() as $model_name => $model_classname) { |
|
57
|
|
|
$model = $this->getModelVersionInfo()->loadModel($model_name); |
|
58
|
|
|
$fields_json = array(); |
|
59
|
|
|
foreach ($this->getModelVersionInfo()->fieldsOnModelInThisVersion($model) as $field_name => $field_obj) { |
|
60
|
|
|
if ($this->getModelVersionInfo()->fieldIsIgnored($field_obj)) { |
|
61
|
|
|
continue; |
|
62
|
|
|
} |
|
63
|
|
|
if ($field_obj instanceof EE_Boolean_Field) { |
|
64
|
|
|
$datatype = 'Boolean'; |
|
65
|
|
|
} elseif ($field_obj->get_wpdb_data_type() == '%d') { |
|
66
|
|
|
$datatype = 'Number'; |
|
67
|
|
|
} elseif ($field_name instanceof EE_Serialized_Text_Field) { |
|
68
|
|
|
$datatype = 'Object'; |
|
69
|
|
|
} else { |
|
70
|
|
|
$datatype = 'String'; |
|
71
|
|
|
} |
|
72
|
|
|
$default_value = ModelDataTranslator::prepareFieldValueForJson( |
|
73
|
|
|
$field_obj, |
|
74
|
|
|
$field_obj->get_default_value(), |
|
75
|
|
|
$this->getModelVersionInfo()->requestedVersion() |
|
76
|
|
|
); |
|
77
|
|
|
$field_json = array( |
|
78
|
|
|
'name' => $field_name, |
|
79
|
|
|
'nicename' => $field_obj->get_nicename(), |
|
80
|
|
|
'has_rendered_format' => $this->getModelVersionInfo()->fieldHasRenderedFormat($field_obj), |
|
81
|
|
|
'has_pretty_format' => $this->getModelVersionInfo()->fieldHasPrettyFormat($field_obj), |
|
82
|
|
|
'type' => str_replace('EE_', '', get_class($field_obj)), |
|
83
|
|
|
'datatype' => $datatype, |
|
84
|
|
|
'nullable' => $field_obj->is_nullable(), |
|
85
|
|
|
'default' => $default_value, |
|
86
|
|
|
'table_alias' => $field_obj->get_table_alias(), |
|
87
|
|
|
'table_column' => $field_obj->get_table_column(), |
|
88
|
|
|
); |
|
89
|
|
|
$fields_json[$field_json['name']] = $field_json; |
|
90
|
|
|
} |
|
91
|
|
|
$fields_json = array_merge( |
|
92
|
|
|
$fields_json, |
|
93
|
|
|
$this->getModelVersionInfo()->extraResourcePropertiesForModel($model) |
|
94
|
|
|
); |
|
95
|
|
|
$response[$model_name]['fields'] = apply_filters( |
|
96
|
|
|
'FHEE__Meta__handle_request_models_meta__fields', |
|
97
|
|
|
$fields_json, |
|
98
|
|
|
$model |
|
99
|
|
|
); |
|
100
|
|
|
$relations_json = array(); |
|
101
|
|
|
foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
|
102
|
|
|
$relation_json = array( |
|
103
|
|
|
'name' => $relation_name, |
|
104
|
|
|
'type' => str_replace('EE_', '', get_class($relation_obj)), |
|
105
|
|
|
'single' => $relation_obj instanceof \EE_Belongs_To_Relation ? true : false, |
|
106
|
|
|
); |
|
107
|
|
|
$relations_json[$relation_name] = $relation_json; |
|
108
|
|
|
} |
|
109
|
|
|
$response[$model_name]['relations'] = apply_filters( |
|
110
|
|
|
'FHEE__Meta__handle_request_models_meta__relations', |
|
111
|
|
|
$relations_json, |
|
112
|
|
|
$model |
|
113
|
|
|
); |
|
114
|
|
|
} |
|
115
|
|
|
return $response; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
|
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Adds EE metadata to the index |
|
122
|
|
|
* |
|
123
|
|
|
* @param \WP_REST_Response $rest_response_obj |
|
124
|
|
|
* @return \WP_REST_Response |
|
125
|
|
|
*/ |
|
126
|
|
|
public static function filterEeMetadataIntoIndex(\WP_REST_Response $rest_response_obj) |
|
127
|
|
|
{ |
|
128
|
|
|
$response_data = $rest_response_obj->get_data(); |
|
129
|
|
|
$addons = array(); |
|
130
|
|
|
foreach (EE_Registry::instance()->addons as $addon) { |
|
131
|
|
|
$addon_json = array( |
|
132
|
|
|
'name' => $addon->name(), |
|
133
|
|
|
'version' => $addon->version(), |
|
134
|
|
|
); |
|
135
|
|
|
$addons[$addon_json['name']] = $addon_json; |
|
136
|
|
|
} |
|
137
|
|
|
$response_data['ee'] = array( |
|
138
|
|
|
'version' => EEM_System_Status::instance()->get_ee_version(), |
|
139
|
|
|
// @codingStandardsIgnoreStart |
|
140
|
|
|
'documentation_url' => 'https://github.com/eventespresso/event-espresso-core/tree/master/docs/C--REST-API', |
|
141
|
|
|
// @codingStandardsIgnoreEnd |
|
142
|
|
|
'addons' => $addons, |
|
143
|
|
|
'maintenance_mode' => EE_Maintenance_Mode::instance()->real_level(), |
|
144
|
|
|
'served_core_versions' => array_keys(EED_Core_Rest_Api::versions_served()), |
|
145
|
|
|
); |
|
146
|
|
|
$rest_response_obj->set_data($response_data); |
|
147
|
|
|
return $rest_response_obj; |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
|
|
152
|
|
|
// End of file Read.class.php |
|
153
|
|
|
|