@@ -18,95 +18,95 @@ |
||
18 | 18 | class Base extends Controller_Base |
19 | 19 | { |
20 | 20 | |
21 | - /** |
|
22 | - * Holds reference to the model version info, which knows the requested version |
|
23 | - * |
|
24 | - * @var ModelVersionInfo |
|
25 | - */ |
|
26 | - protected $model_version_info; |
|
21 | + /** |
|
22 | + * Holds reference to the model version info, which knows the requested version |
|
23 | + * |
|
24 | + * @var ModelVersionInfo |
|
25 | + */ |
|
26 | + protected $model_version_info; |
|
27 | 27 | |
28 | 28 | |
29 | 29 | |
30 | - /** |
|
31 | - * Sets the version the user requested |
|
32 | - * |
|
33 | - * @param string $version eg '4.8' |
|
34 | - */ |
|
35 | - public function setRequestedVersion($version) |
|
36 | - { |
|
37 | - parent::setRequestedVersion($version); |
|
38 | - $this->model_version_info = new ModelVersionInfo($version); |
|
39 | - } |
|
30 | + /** |
|
31 | + * Sets the version the user requested |
|
32 | + * |
|
33 | + * @param string $version eg '4.8' |
|
34 | + */ |
|
35 | + public function setRequestedVersion($version) |
|
36 | + { |
|
37 | + parent::setRequestedVersion($version); |
|
38 | + $this->model_version_info = new ModelVersionInfo($version); |
|
39 | + } |
|
40 | 40 | |
41 | 41 | |
42 | 42 | |
43 | - /** |
|
44 | - * Gets the object that should be used for getting any info from the models, |
|
45 | - * because it's takes the requested and current core version into account |
|
46 | - * |
|
47 | - * @return \EventEspresso\core\libraries\rest_api\ModelVersionInfo |
|
48 | - * @throws EE_Error |
|
49 | - */ |
|
50 | - public function getModelVersionInfo() |
|
51 | - { |
|
52 | - if (! $this->model_version_info) { |
|
53 | - throw new EE_Error( |
|
54 | - sprintf( |
|
55 | - __( |
|
56 | - 'Cannot use model version info before setting the requested version in the controller', |
|
57 | - 'event_espresso' |
|
58 | - ) |
|
59 | - ) |
|
60 | - ); |
|
61 | - } |
|
62 | - return $this->model_version_info; |
|
63 | - } |
|
43 | + /** |
|
44 | + * Gets the object that should be used for getting any info from the models, |
|
45 | + * because it's takes the requested and current core version into account |
|
46 | + * |
|
47 | + * @return \EventEspresso\core\libraries\rest_api\ModelVersionInfo |
|
48 | + * @throws EE_Error |
|
49 | + */ |
|
50 | + public function getModelVersionInfo() |
|
51 | + { |
|
52 | + if (! $this->model_version_info) { |
|
53 | + throw new EE_Error( |
|
54 | + sprintf( |
|
55 | + __( |
|
56 | + 'Cannot use model version info before setting the requested version in the controller', |
|
57 | + 'event_espresso' |
|
58 | + ) |
|
59 | + ) |
|
60 | + ); |
|
61 | + } |
|
62 | + return $this->model_version_info; |
|
63 | + } |
|
64 | 64 | |
65 | 65 | |
66 | 66 | |
67 | - /** |
|
68 | - * Determines if $object is of one of the classes of $classes. Similar to |
|
69 | - * in_array(), except this checks if $object is a subclass of the classnames provided |
|
70 | - * in $classnames |
|
71 | - * |
|
72 | - * @param object $object |
|
73 | - * @param array $classnames |
|
74 | - * @return boolean |
|
75 | - */ |
|
76 | - public function isSubclassOfOne($object, $classnames) |
|
77 | - { |
|
78 | - foreach ($classnames as $classname) { |
|
79 | - if (is_a($object, $classname)) { |
|
80 | - return true; |
|
81 | - } |
|
82 | - } |
|
83 | - return false; |
|
84 | - } |
|
67 | + /** |
|
68 | + * Determines if $object is of one of the classes of $classes. Similar to |
|
69 | + * in_array(), except this checks if $object is a subclass of the classnames provided |
|
70 | + * in $classnames |
|
71 | + * |
|
72 | + * @param object $object |
|
73 | + * @param array $classnames |
|
74 | + * @return boolean |
|
75 | + */ |
|
76 | + public function isSubclassOfOne($object, $classnames) |
|
77 | + { |
|
78 | + foreach ($classnames as $classname) { |
|
79 | + if (is_a($object, $classname)) { |
|
80 | + return true; |
|
81 | + } |
|
82 | + } |
|
83 | + return false; |
|
84 | + } |
|
85 | 85 | |
86 | - /** |
|
87 | - * Verifies the model name provided was valid. If so, returns the model (as an object). Otherwise, throws an |
|
88 | - * exception. Must be called after `setRequestedVersion()`. |
|
89 | - * @since 4.9.76.p |
|
90 | - * @param $model_name |
|
91 | - * @return EEM_Base |
|
92 | - * @throws EE_Error |
|
93 | - * @throws RestException |
|
94 | - */ |
|
95 | - protected function validateModel($model_name) |
|
96 | - { |
|
97 | - if (! $this->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
|
98 | - throw new RestException( |
|
99 | - 'endpoint_parsing_error', |
|
100 | - sprintf( |
|
101 | - __( |
|
102 | - 'There is no model for endpoint %s. Please contact event espresso support', |
|
103 | - 'event_espresso' |
|
104 | - ), |
|
105 | - $model_name |
|
106 | - ) |
|
107 | - ); |
|
108 | - } |
|
109 | - return $this->getModelVersionInfo()->loadModel($model_name); |
|
110 | - } |
|
86 | + /** |
|
87 | + * Verifies the model name provided was valid. If so, returns the model (as an object). Otherwise, throws an |
|
88 | + * exception. Must be called after `setRequestedVersion()`. |
|
89 | + * @since 4.9.76.p |
|
90 | + * @param $model_name |
|
91 | + * @return EEM_Base |
|
92 | + * @throws EE_Error |
|
93 | + * @throws RestException |
|
94 | + */ |
|
95 | + protected function validateModel($model_name) |
|
96 | + { |
|
97 | + if (! $this->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
|
98 | + throw new RestException( |
|
99 | + 'endpoint_parsing_error', |
|
100 | + sprintf( |
|
101 | + __( |
|
102 | + 'There is no model for endpoint %s. Please contact event espresso support', |
|
103 | + 'event_espresso' |
|
104 | + ), |
|
105 | + $model_name |
|
106 | + ) |
|
107 | + ); |
|
108 | + } |
|
109 | + return $this->getModelVersionInfo()->loadModel($model_name); |
|
110 | + } |
|
111 | 111 | } |
112 | 112 | // End of file Base.php |
@@ -17,50 +17,50 @@ |
||
17 | 17 | class WordpressHeartbeat |
18 | 18 | { |
19 | 19 | |
20 | - /** |
|
21 | - * @var LoaderInterface $loader |
|
22 | - */ |
|
23 | - protected $loader; |
|
20 | + /** |
|
21 | + * @var LoaderInterface $loader |
|
22 | + */ |
|
23 | + protected $loader; |
|
24 | 24 | |
25 | - /** |
|
26 | - * @var RequestInterface $request |
|
27 | - */ |
|
28 | - protected $request; |
|
25 | + /** |
|
26 | + * @var RequestInterface $request |
|
27 | + */ |
|
28 | + protected $request; |
|
29 | 29 | |
30 | 30 | |
31 | - /** |
|
32 | - * WordpressHeartbeat constructor. |
|
33 | - * |
|
34 | - * @param LoaderInterface $loader |
|
35 | - * @param RequestInterface $request |
|
36 | - */ |
|
37 | - public function __construct( |
|
38 | - LoaderInterface $loader, |
|
39 | - RequestInterface $request |
|
40 | - ) { |
|
41 | - $this->loader = $loader; |
|
42 | - $this->request = $request; |
|
43 | - do_action('AHEE__EventEspresso_core_domain_services_admin_ajax_WordpressHeartbeat__constructor', $this); |
|
44 | - add_action('AHEE__EE_System__core_loaded_and_ready', array($this, 'resolveRoutes')); |
|
45 | - } |
|
31 | + /** |
|
32 | + * WordpressHeartbeat constructor. |
|
33 | + * |
|
34 | + * @param LoaderInterface $loader |
|
35 | + * @param RequestInterface $request |
|
36 | + */ |
|
37 | + public function __construct( |
|
38 | + LoaderInterface $loader, |
|
39 | + RequestInterface $request |
|
40 | + ) { |
|
41 | + $this->loader = $loader; |
|
42 | + $this->request = $request; |
|
43 | + do_action('AHEE__EventEspresso_core_domain_services_admin_ajax_WordpressHeartbeat__constructor', $this); |
|
44 | + add_action('AHEE__EE_System__core_loaded_and_ready', array($this, 'resolveRoutes')); |
|
45 | + } |
|
46 | 46 | |
47 | 47 | |
48 | - /** |
|
49 | - * @since 4.9.76.p |
|
50 | - * @throws InvalidClassException |
|
51 | - */ |
|
52 | - public function resolveRoutes() |
|
53 | - { |
|
54 | - $screenID = $this->request->getRequestParam('screen_id'); |
|
55 | - $heartbeat_data = $this->request->getRequestParam('data', []); |
|
56 | - if ($screenID === 'espresso_events') { |
|
57 | - $this->loader->getShared( |
|
58 | - 'EventEspresso\core\domain\services\admin\ajax\EventEditorHeartbeat' |
|
59 | - ); |
|
60 | - } elseif ($screenID === 'front' && ! empty($heartbeat_data['espresso_thank_you_page'])) { |
|
61 | - $this->loader->getShared( |
|
62 | - 'EventEspresso\core\domain\services\admin\ajax\ThankYouPageIpnMonitor' |
|
63 | - ); |
|
64 | - } |
|
65 | - } |
|
48 | + /** |
|
49 | + * @since 4.9.76.p |
|
50 | + * @throws InvalidClassException |
|
51 | + */ |
|
52 | + public function resolveRoutes() |
|
53 | + { |
|
54 | + $screenID = $this->request->getRequestParam('screen_id'); |
|
55 | + $heartbeat_data = $this->request->getRequestParam('data', []); |
|
56 | + if ($screenID === 'espresso_events') { |
|
57 | + $this->loader->getShared( |
|
58 | + 'EventEspresso\core\domain\services\admin\ajax\EventEditorHeartbeat' |
|
59 | + ); |
|
60 | + } elseif ($screenID === 'front' && ! empty($heartbeat_data['espresso_thank_you_page'])) { |
|
61 | + $this->loader->getShared( |
|
62 | + 'EventEspresso\core\domain\services\admin\ajax\ThankYouPageIpnMonitor' |
|
63 | + ); |
|
64 | + } |
|
65 | + } |
|
66 | 66 | } |
@@ -26,248 +26,248 @@ |
||
26 | 26 | class CountrySubRegionDao |
27 | 27 | { |
28 | 28 | |
29 | - const REPO_URL = 'https://raw.githubusercontent.com/eventespresso/countries-and-subregions/master/'; |
|
29 | + const REPO_URL = 'https://raw.githubusercontent.com/eventespresso/countries-and-subregions/master/'; |
|
30 | 30 | |
31 | - const OPTION_NAME_COUNTRY_DATA_VERSION = 'espresso-country-sub-region-data-version'; |
|
31 | + const OPTION_NAME_COUNTRY_DATA_VERSION = 'espresso-country-sub-region-data-version'; |
|
32 | 32 | |
33 | - /** |
|
34 | - * @var EEM_State $state_model |
|
35 | - */ |
|
36 | - private $state_model; |
|
33 | + /** |
|
34 | + * @var EEM_State $state_model |
|
35 | + */ |
|
36 | + private $state_model; |
|
37 | 37 | |
38 | - /** |
|
39 | - * @var JsonValidator $json_validator |
|
40 | - */ |
|
41 | - private $json_validator; |
|
38 | + /** |
|
39 | + * @var JsonValidator $json_validator |
|
40 | + */ |
|
41 | + private $json_validator; |
|
42 | 42 | |
43 | - /** |
|
44 | - * @var string $data_version |
|
45 | - */ |
|
46 | - private $data_version; |
|
43 | + /** |
|
44 | + * @var string $data_version |
|
45 | + */ |
|
46 | + private $data_version; |
|
47 | 47 | |
48 | - /** |
|
49 | - * @var array $countries |
|
50 | - */ |
|
51 | - private $countries = array(); |
|
48 | + /** |
|
49 | + * @var array $countries |
|
50 | + */ |
|
51 | + private $countries = array(); |
|
52 | 52 | |
53 | 53 | |
54 | - /** |
|
55 | - * CountrySubRegionDao constructor. |
|
56 | - * |
|
57 | - * @param EEM_State $state_model |
|
58 | - * @param JsonValidator $json_validator |
|
59 | - */ |
|
60 | - public function __construct(EEM_State $state_model, JsonValidator $json_validator) |
|
61 | - { |
|
62 | - $this->state_model = $state_model; |
|
63 | - $this->json_validator = $json_validator; |
|
64 | - } |
|
54 | + /** |
|
55 | + * CountrySubRegionDao constructor. |
|
56 | + * |
|
57 | + * @param EEM_State $state_model |
|
58 | + * @param JsonValidator $json_validator |
|
59 | + */ |
|
60 | + public function __construct(EEM_State $state_model, JsonValidator $json_validator) |
|
61 | + { |
|
62 | + $this->state_model = $state_model; |
|
63 | + $this->json_validator = $json_validator; |
|
64 | + } |
|
65 | 65 | |
66 | 66 | |
67 | - /** |
|
68 | - * @param EE_Country $country_object |
|
69 | - * @return bool |
|
70 | - * @throws EE_Error |
|
71 | - * @throws InvalidArgumentException |
|
72 | - * @throws InvalidDataTypeException |
|
73 | - * @throws InvalidInterfaceException |
|
74 | - * @throws ReflectionException |
|
75 | - */ |
|
76 | - public function saveCountrySubRegions(EE_Country $country_object) |
|
77 | - { |
|
78 | - $CNT_ISO = $country_object->ID(); |
|
79 | - $has_sub_regions = $this->state_model->count(array(array('Country.CNT_ISO' => $CNT_ISO))); |
|
80 | - $data = []; |
|
81 | - if (empty($this->countries)) { |
|
82 | - $this->data_version = $this->getCountrySubRegionDataVersion(); |
|
83 | - $data = $this->retrieveJsonData(self::REPO_URL . 'countries.json'); |
|
84 | - } |
|
85 | - if (empty($data)) { |
|
86 | - EE_Error::add_error( |
|
87 | - 'Country Subregion Data could not be retrieved', |
|
88 | - __FILE__, |
|
89 | - __METHOD__, |
|
90 | - __LINE__ |
|
91 | - ); |
|
92 | - } |
|
93 | - if (! $has_sub_regions |
|
94 | - || (isset($data->version) && version_compare($data->version, $this->data_version)) |
|
95 | - ) { |
|
96 | - if (isset($data->countries) |
|
97 | - && $this->processCountryData($CNT_ISO, $data->countries) > 0 |
|
98 | - ) { |
|
99 | - $this->countries = $data->countries; |
|
100 | - $this->updateCountrySubRegionDataVersion($data->version); |
|
101 | - return true; |
|
102 | - } |
|
103 | - } |
|
104 | - return false; |
|
105 | - } |
|
67 | + /** |
|
68 | + * @param EE_Country $country_object |
|
69 | + * @return bool |
|
70 | + * @throws EE_Error |
|
71 | + * @throws InvalidArgumentException |
|
72 | + * @throws InvalidDataTypeException |
|
73 | + * @throws InvalidInterfaceException |
|
74 | + * @throws ReflectionException |
|
75 | + */ |
|
76 | + public function saveCountrySubRegions(EE_Country $country_object) |
|
77 | + { |
|
78 | + $CNT_ISO = $country_object->ID(); |
|
79 | + $has_sub_regions = $this->state_model->count(array(array('Country.CNT_ISO' => $CNT_ISO))); |
|
80 | + $data = []; |
|
81 | + if (empty($this->countries)) { |
|
82 | + $this->data_version = $this->getCountrySubRegionDataVersion(); |
|
83 | + $data = $this->retrieveJsonData(self::REPO_URL . 'countries.json'); |
|
84 | + } |
|
85 | + if (empty($data)) { |
|
86 | + EE_Error::add_error( |
|
87 | + 'Country Subregion Data could not be retrieved', |
|
88 | + __FILE__, |
|
89 | + __METHOD__, |
|
90 | + __LINE__ |
|
91 | + ); |
|
92 | + } |
|
93 | + if (! $has_sub_regions |
|
94 | + || (isset($data->version) && version_compare($data->version, $this->data_version)) |
|
95 | + ) { |
|
96 | + if (isset($data->countries) |
|
97 | + && $this->processCountryData($CNT_ISO, $data->countries) > 0 |
|
98 | + ) { |
|
99 | + $this->countries = $data->countries; |
|
100 | + $this->updateCountrySubRegionDataVersion($data->version); |
|
101 | + return true; |
|
102 | + } |
|
103 | + } |
|
104 | + return false; |
|
105 | + } |
|
106 | 106 | |
107 | 107 | |
108 | - /** |
|
109 | - * @since 4.9.70.p |
|
110 | - * @return string |
|
111 | - */ |
|
112 | - private function getCountrySubRegionDataVersion() |
|
113 | - { |
|
114 | - return get_option(self::OPTION_NAME_COUNTRY_DATA_VERSION, null); |
|
115 | - } |
|
108 | + /** |
|
109 | + * @since 4.9.70.p |
|
110 | + * @return string |
|
111 | + */ |
|
112 | + private function getCountrySubRegionDataVersion() |
|
113 | + { |
|
114 | + return get_option(self::OPTION_NAME_COUNTRY_DATA_VERSION, null); |
|
115 | + } |
|
116 | 116 | |
117 | 117 | |
118 | - /** |
|
119 | - * @param string $version |
|
120 | - */ |
|
121 | - private function updateCountrySubRegionDataVersion($version = '') |
|
122 | - { |
|
123 | - // add version option if it has never been added before, or update existing |
|
124 | - if ($this->data_version === null) { |
|
125 | - add_option(self::OPTION_NAME_COUNTRY_DATA_VERSION, $version, '', false); |
|
126 | - } else { |
|
127 | - update_option(self::OPTION_NAME_COUNTRY_DATA_VERSION, $version); |
|
128 | - } |
|
129 | - } |
|
118 | + /** |
|
119 | + * @param string $version |
|
120 | + */ |
|
121 | + private function updateCountrySubRegionDataVersion($version = '') |
|
122 | + { |
|
123 | + // add version option if it has never been added before, or update existing |
|
124 | + if ($this->data_version === null) { |
|
125 | + add_option(self::OPTION_NAME_COUNTRY_DATA_VERSION, $version, '', false); |
|
126 | + } else { |
|
127 | + update_option(self::OPTION_NAME_COUNTRY_DATA_VERSION, $version); |
|
128 | + } |
|
129 | + } |
|
130 | 130 | |
131 | 131 | |
132 | - /** |
|
133 | - * @param string $CNT_ISO |
|
134 | - * @param array $countries |
|
135 | - * @return int |
|
136 | - * @throws EE_Error |
|
137 | - * @throws InvalidArgumentException |
|
138 | - * @throws InvalidDataTypeException |
|
139 | - * @throws InvalidInterfaceException |
|
140 | - * @throws ReflectionException |
|
141 | - * @since 4.9.70.p |
|
142 | - */ |
|
143 | - private function processCountryData($CNT_ISO, $countries = array()) |
|
144 | - { |
|
145 | - if (! empty($countries)) { |
|
146 | - foreach ($countries as $key => $country) { |
|
147 | - if ($country instanceof stdClass |
|
148 | - && $country->code === $CNT_ISO |
|
149 | - && empty($country->sub_regions) |
|
150 | - && ! empty($country->filename) |
|
151 | - ) { |
|
152 | - $country->sub_regions = $this->retrieveJsonData( |
|
153 | - self::REPO_URL . 'countries/' . $country->filename . '.json' |
|
154 | - ); |
|
155 | - return $this->saveSubRegionData($country, $country->sub_regions); |
|
156 | - } |
|
157 | - } |
|
158 | - } |
|
159 | - return 0; |
|
160 | - } |
|
132 | + /** |
|
133 | + * @param string $CNT_ISO |
|
134 | + * @param array $countries |
|
135 | + * @return int |
|
136 | + * @throws EE_Error |
|
137 | + * @throws InvalidArgumentException |
|
138 | + * @throws InvalidDataTypeException |
|
139 | + * @throws InvalidInterfaceException |
|
140 | + * @throws ReflectionException |
|
141 | + * @since 4.9.70.p |
|
142 | + */ |
|
143 | + private function processCountryData($CNT_ISO, $countries = array()) |
|
144 | + { |
|
145 | + if (! empty($countries)) { |
|
146 | + foreach ($countries as $key => $country) { |
|
147 | + if ($country instanceof stdClass |
|
148 | + && $country->code === $CNT_ISO |
|
149 | + && empty($country->sub_regions) |
|
150 | + && ! empty($country->filename) |
|
151 | + ) { |
|
152 | + $country->sub_regions = $this->retrieveJsonData( |
|
153 | + self::REPO_URL . 'countries/' . $country->filename . '.json' |
|
154 | + ); |
|
155 | + return $this->saveSubRegionData($country, $country->sub_regions); |
|
156 | + } |
|
157 | + } |
|
158 | + } |
|
159 | + return 0; |
|
160 | + } |
|
161 | 161 | |
162 | 162 | |
163 | - /** |
|
164 | - * @param string $url |
|
165 | - * @return array |
|
166 | - */ |
|
167 | - private function retrieveJsonData($url) |
|
168 | - { |
|
169 | - if (empty($url)) { |
|
170 | - EE_Error::add_error( |
|
171 | - 'No URL was provided!', |
|
172 | - __FILE__, |
|
173 | - __METHOD__, |
|
174 | - __LINE__ |
|
175 | - ); |
|
176 | - return array(); |
|
177 | - } |
|
178 | - $request = wp_safe_remote_get($url); |
|
179 | - if ($request instanceof WP_Error) { |
|
180 | - EE_Error::add_error( |
|
181 | - $request->get_error_message(), |
|
182 | - __FILE__, |
|
183 | - __METHOD__, |
|
184 | - __LINE__ |
|
185 | - ); |
|
186 | - return array(); |
|
187 | - } |
|
188 | - $body = wp_remote_retrieve_body($request); |
|
189 | - $json = json_decode($body); |
|
190 | - if ($this->json_validator->isValid(__FILE__, __METHOD__, __LINE__)) { |
|
191 | - return $json; |
|
192 | - } |
|
193 | - return array(); |
|
194 | - } |
|
163 | + /** |
|
164 | + * @param string $url |
|
165 | + * @return array |
|
166 | + */ |
|
167 | + private function retrieveJsonData($url) |
|
168 | + { |
|
169 | + if (empty($url)) { |
|
170 | + EE_Error::add_error( |
|
171 | + 'No URL was provided!', |
|
172 | + __FILE__, |
|
173 | + __METHOD__, |
|
174 | + __LINE__ |
|
175 | + ); |
|
176 | + return array(); |
|
177 | + } |
|
178 | + $request = wp_safe_remote_get($url); |
|
179 | + if ($request instanceof WP_Error) { |
|
180 | + EE_Error::add_error( |
|
181 | + $request->get_error_message(), |
|
182 | + __FILE__, |
|
183 | + __METHOD__, |
|
184 | + __LINE__ |
|
185 | + ); |
|
186 | + return array(); |
|
187 | + } |
|
188 | + $body = wp_remote_retrieve_body($request); |
|
189 | + $json = json_decode($body); |
|
190 | + if ($this->json_validator->isValid(__FILE__, __METHOD__, __LINE__)) { |
|
191 | + return $json; |
|
192 | + } |
|
193 | + return array(); |
|
194 | + } |
|
195 | 195 | |
196 | 196 | |
197 | - /** |
|
198 | - * @param stdClass $country |
|
199 | - * @param array $sub_regions |
|
200 | - * @return int |
|
201 | - * @throws EE_Error |
|
202 | - * @throws InvalidArgumentException |
|
203 | - * @throws InvalidDataTypeException |
|
204 | - * @throws InvalidInterfaceException |
|
205 | - * @throws ReflectionException |
|
206 | - */ |
|
207 | - private function saveSubRegionData(stdClass $country, $sub_regions = array()) |
|
208 | - { |
|
209 | - $results = 0; |
|
210 | - if (is_array($sub_regions)) { |
|
211 | - $existing_sub_regions = $this->getExistingStateAbbreviations($country->code); |
|
212 | - foreach ($sub_regions as $sub_region) { |
|
213 | - // remove country code from sub region code |
|
214 | - $abbrev = str_replace( |
|
215 | - $country->code . '-', |
|
216 | - '', |
|
217 | - sanitize_text_field($sub_region->code) |
|
218 | - ); |
|
219 | - // but NOT if sub region code results in only a number |
|
220 | - if (absint($abbrev) !== 0) { |
|
221 | - $abbrev = sanitize_text_field($sub_region->code); |
|
222 | - } |
|
223 | - if (! in_array($abbrev, $existing_sub_regions, true) |
|
224 | - && $this->state_model->insert( |
|
225 | - [ |
|
226 | - // STA_ID CNT_ISO STA_abbrev STA_name STA_active |
|
227 | - 'CNT_ISO' => $country->code, |
|
228 | - 'STA_abbrev' => $abbrev, |
|
229 | - 'STA_name' => sanitize_text_field($sub_region->name), |
|
230 | - 'STA_active' => 1, |
|
231 | - ] |
|
232 | - ) |
|
233 | - ) { |
|
234 | - $results++; |
|
235 | - } |
|
236 | - } |
|
237 | - } |
|
238 | - return $results; |
|
239 | - } |
|
197 | + /** |
|
198 | + * @param stdClass $country |
|
199 | + * @param array $sub_regions |
|
200 | + * @return int |
|
201 | + * @throws EE_Error |
|
202 | + * @throws InvalidArgumentException |
|
203 | + * @throws InvalidDataTypeException |
|
204 | + * @throws InvalidInterfaceException |
|
205 | + * @throws ReflectionException |
|
206 | + */ |
|
207 | + private function saveSubRegionData(stdClass $country, $sub_regions = array()) |
|
208 | + { |
|
209 | + $results = 0; |
|
210 | + if (is_array($sub_regions)) { |
|
211 | + $existing_sub_regions = $this->getExistingStateAbbreviations($country->code); |
|
212 | + foreach ($sub_regions as $sub_region) { |
|
213 | + // remove country code from sub region code |
|
214 | + $abbrev = str_replace( |
|
215 | + $country->code . '-', |
|
216 | + '', |
|
217 | + sanitize_text_field($sub_region->code) |
|
218 | + ); |
|
219 | + // but NOT if sub region code results in only a number |
|
220 | + if (absint($abbrev) !== 0) { |
|
221 | + $abbrev = sanitize_text_field($sub_region->code); |
|
222 | + } |
|
223 | + if (! in_array($abbrev, $existing_sub_regions, true) |
|
224 | + && $this->state_model->insert( |
|
225 | + [ |
|
226 | + // STA_ID CNT_ISO STA_abbrev STA_name STA_active |
|
227 | + 'CNT_ISO' => $country->code, |
|
228 | + 'STA_abbrev' => $abbrev, |
|
229 | + 'STA_name' => sanitize_text_field($sub_region->name), |
|
230 | + 'STA_active' => 1, |
|
231 | + ] |
|
232 | + ) |
|
233 | + ) { |
|
234 | + $results++; |
|
235 | + } |
|
236 | + } |
|
237 | + } |
|
238 | + return $results; |
|
239 | + } |
|
240 | 240 | |
241 | 241 | |
242 | - /** |
|
243 | - * @param string $CNT_ISO |
|
244 | - * @since 4.9.76.p |
|
245 | - * @return array |
|
246 | - * @throws EE_Error |
|
247 | - * @throws InvalidArgumentException |
|
248 | - * @throws InvalidDataTypeException |
|
249 | - * @throws InvalidInterfaceException |
|
250 | - * @throws ReflectionException |
|
251 | - */ |
|
252 | - private function getExistingStateAbbreviations($CNT_ISO) |
|
253 | - { |
|
254 | - $existing_sub_region_IDs = []; |
|
255 | - $existing_sub_regions = $this->state_model->get_all(array( |
|
256 | - array( |
|
257 | - 'Country.CNT_ISO' => array( |
|
258 | - 'IN', |
|
259 | - [$CNT_ISO] |
|
260 | - ) |
|
261 | - ), |
|
262 | - 'order_by' => array('Country.CNT_name' => 'ASC', 'STA_name' => 'ASC') |
|
263 | - )); |
|
264 | - if (is_array($existing_sub_regions)) { |
|
265 | - foreach ($existing_sub_regions as $existing_sub_region) { |
|
266 | - if ($existing_sub_region instanceof EE_State) { |
|
267 | - $existing_sub_region_IDs[] = $existing_sub_region->abbrev(); |
|
268 | - } |
|
269 | - } |
|
270 | - } |
|
271 | - return $existing_sub_region_IDs; |
|
272 | - } |
|
242 | + /** |
|
243 | + * @param string $CNT_ISO |
|
244 | + * @since 4.9.76.p |
|
245 | + * @return array |
|
246 | + * @throws EE_Error |
|
247 | + * @throws InvalidArgumentException |
|
248 | + * @throws InvalidDataTypeException |
|
249 | + * @throws InvalidInterfaceException |
|
250 | + * @throws ReflectionException |
|
251 | + */ |
|
252 | + private function getExistingStateAbbreviations($CNT_ISO) |
|
253 | + { |
|
254 | + $existing_sub_region_IDs = []; |
|
255 | + $existing_sub_regions = $this->state_model->get_all(array( |
|
256 | + array( |
|
257 | + 'Country.CNT_ISO' => array( |
|
258 | + 'IN', |
|
259 | + [$CNT_ISO] |
|
260 | + ) |
|
261 | + ), |
|
262 | + 'order_by' => array('Country.CNT_name' => 'ASC', 'STA_name' => 'ASC') |
|
263 | + )); |
|
264 | + if (is_array($existing_sub_regions)) { |
|
265 | + foreach ($existing_sub_regions as $existing_sub_region) { |
|
266 | + if ($existing_sub_region instanceof EE_State) { |
|
267 | + $existing_sub_region_IDs[] = $existing_sub_region->abbrev(); |
|
268 | + } |
|
269 | + } |
|
270 | + } |
|
271 | + return $existing_sub_region_IDs; |
|
272 | + } |
|
273 | 273 | } |
@@ -23,1333 +23,1333 @@ |
||
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 || EED_Core_Rest_Api::debugMode()) { |
|
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, true), |
|
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 | - $model_routes[ $related_route ] = 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 | - |
|
547 | - $related_write_route = $related_route . '/' . '(?P<related_id>[^\/]+)'; |
|
548 | - $model_routes[ $related_write_route ] = array( |
|
549 | - array( |
|
550 | - 'callback' => array( |
|
551 | - 'EventEspresso\core\libraries\rest_api\controllers\model\Write', |
|
552 | - 'handleRequestAddRelation', |
|
553 | - ), |
|
554 | - 'callback_args' => array($version, $model_name, $relation_name), |
|
555 | - 'methods' => WP_REST_Server::EDITABLE, |
|
556 | - 'hidden_endpoint' => $hidden_endpoint, |
|
557 | - 'args' => $this->_get_add_relation_query_params($model, $relation_obj->get_other_model(), $version) |
|
558 | - ), |
|
559 | - array( |
|
560 | - 'callback' => array( |
|
561 | - 'EventEspresso\core\libraries\rest_api\controllers\model\Write', |
|
562 | - 'handleRequestRemoveRelation', |
|
563 | - ), |
|
564 | - 'callback_args' => array($version, $model_name, $relation_name), |
|
565 | - 'methods' => WP_REST_Server::DELETABLE, |
|
566 | - 'hidden_endpoint' => $hidden_endpoint, |
|
567 | - 'args' => array() |
|
568 | - ), |
|
569 | - ); |
|
570 | - } |
|
571 | - } |
|
572 | - return $model_routes; |
|
573 | - } |
|
574 | - |
|
575 | - |
|
576 | - /** |
|
577 | - * Gets the relative URI to a model's REST API plural route, after the EE4 versioned namespace, |
|
578 | - * excluding the preceding slash. |
|
579 | - * Eg you pass get_plural_route_to('Event') = 'events' |
|
580 | - * |
|
581 | - * @param EEM_Base $model |
|
582 | - * @return string |
|
583 | - */ |
|
584 | - public static function get_collection_route(EEM_Base $model) |
|
585 | - { |
|
586 | - return EEH_Inflector::pluralize_and_lower($model->get_this_model_name()); |
|
587 | - } |
|
588 | - |
|
589 | - |
|
590 | - /** |
|
591 | - * Gets the relative URI to a model's REST API singular route, after the EE4 versioned namespace, |
|
592 | - * excluding the preceding slash. |
|
593 | - * Eg you pass get_plural_route_to('Event', 12) = 'events/12' |
|
594 | - * |
|
595 | - * @param EEM_Base $model eg Event or Venue |
|
596 | - * @param string $id |
|
597 | - * @return string |
|
598 | - */ |
|
599 | - public static function get_entity_route($model, $id) |
|
600 | - { |
|
601 | - return EED_Core_Rest_Api::get_collection_route($model) . '/' . $id; |
|
602 | - } |
|
603 | - |
|
604 | - |
|
605 | - /** |
|
606 | - * Gets the relative URI to a model's REST API singular route, after the EE4 versioned namespace, |
|
607 | - * excluding the preceding slash. |
|
608 | - * Eg you pass get_plural_route_to('Event', 12) = 'events/12' |
|
609 | - * |
|
610 | - * @param EEM_Base $model eg Event or Venue |
|
611 | - * @param string $id |
|
612 | - * @param EE_Model_Relation_Base $relation_obj |
|
613 | - * @return string |
|
614 | - */ |
|
615 | - public static function get_relation_route_via(EEM_Base $model, $id, EE_Model_Relation_Base $relation_obj) |
|
616 | - { |
|
617 | - $related_model_name_endpoint_part = ModelRead::getRelatedEntityName( |
|
618 | - $relation_obj->get_other_model()->get_this_model_name(), |
|
619 | - $relation_obj |
|
620 | - ); |
|
621 | - return EED_Core_Rest_Api::get_entity_route($model, $id) . '/' . $related_model_name_endpoint_part; |
|
622 | - } |
|
623 | - |
|
624 | - |
|
625 | - /** |
|
626 | - * Adds onto the $relative_route the EE4 REST API versioned namespace. |
|
627 | - * Eg if given '4.8.36' and 'events', will return 'ee/v4.8.36/events' |
|
628 | - * |
|
629 | - * @param string $relative_route |
|
630 | - * @param string $version |
|
631 | - * @return string |
|
632 | - */ |
|
633 | - public static function get_versioned_route_to($relative_route, $version = '4.8.36') |
|
634 | - { |
|
635 | - return '/' . EED_Core_Rest_Api::ee_api_namespace . $version . '/' . $relative_route; |
|
636 | - } |
|
637 | - |
|
638 | - |
|
639 | - /** |
|
640 | - * Adds all the RPC-style routes (remote procedure call-like routes, ie |
|
641 | - * routes that don't conform to the traditional REST CRUD-style). |
|
642 | - * |
|
643 | - * @deprecated since 4.9.1 |
|
644 | - */ |
|
645 | - protected function _register_rpc_routes() |
|
646 | - { |
|
647 | - $routes = array(); |
|
648 | - foreach (self::versions_served() as $version => $hidden_endpoint) { |
|
649 | - $routes[ self::ee_api_namespace . $version ] = $this->_get_rpc_route_data_for_version( |
|
650 | - $version, |
|
651 | - $hidden_endpoint |
|
652 | - ); |
|
653 | - } |
|
654 | - return $routes; |
|
655 | - } |
|
656 | - |
|
657 | - |
|
658 | - /** |
|
659 | - * @param string $version |
|
660 | - * @param boolean $hidden_endpoint |
|
661 | - * @return array |
|
662 | - */ |
|
663 | - protected function _get_rpc_route_data_for_version($version, $hidden_endpoint = false) |
|
664 | - { |
|
665 | - $this_versions_routes = array(); |
|
666 | - // checkin endpoint |
|
667 | - $this_versions_routes['registrations/(?P<REG_ID>\d+)/toggle_checkin_for_datetime/(?P<DTT_ID>\d+)'] = array( |
|
668 | - array( |
|
669 | - 'callback' => array( |
|
670 | - 'EventEspresso\core\libraries\rest_api\controllers\rpc\Checkin', |
|
671 | - 'handleRequestToggleCheckin', |
|
672 | - ), |
|
673 | - 'methods' => WP_REST_Server::CREATABLE, |
|
674 | - 'hidden_endpoint' => $hidden_endpoint, |
|
675 | - 'args' => array( |
|
676 | - 'force' => array( |
|
677 | - 'required' => false, |
|
678 | - 'default' => false, |
|
679 | - 'description' => __( |
|
680 | - // @codingStandardsIgnoreStart |
|
681 | - 'Whether to force toggle checkin, or to verify the registration status and allowed ticket uses', |
|
682 | - // @codingStandardsIgnoreEnd |
|
683 | - 'event_espresso' |
|
684 | - ), |
|
685 | - ), |
|
686 | - ), |
|
687 | - 'callback_args' => array($version), |
|
688 | - ), |
|
689 | - ); |
|
690 | - return apply_filters( |
|
691 | - 'FHEE__EED_Core_Rest_Api___register_rpc_routes__this_versions_routes', |
|
692 | - $this_versions_routes, |
|
693 | - $version, |
|
694 | - $hidden_endpoint |
|
695 | - ); |
|
696 | - } |
|
697 | - |
|
698 | - |
|
699 | - /** |
|
700 | - * Gets the query params that can be used when request one or many |
|
701 | - * |
|
702 | - * @param EEM_Base $model |
|
703 | - * @param string $version |
|
704 | - * @return array |
|
705 | - */ |
|
706 | - protected function _get_response_selection_query_params(\EEM_Base $model, $version, $single_only = false) |
|
707 | - { |
|
708 | - $query_params = array( |
|
709 | - 'include' => array( |
|
710 | - 'required' => false, |
|
711 | - 'default' => '*', |
|
712 | - 'type' => 'string', |
|
713 | - ), |
|
714 | - 'calculate' => array( |
|
715 | - 'required' => false, |
|
716 | - 'default' => '', |
|
717 | - 'enum' => self::$_field_calculator->retrieveCalculatedFieldsForModel($model), |
|
718 | - 'type' => 'string', |
|
719 | - // because we accept a CSV'd list of the enumerated strings, WP core validation and sanitization |
|
720 | - // freaks out. We'll just validate this argument while handling the request |
|
721 | - 'validate_callback' => null, |
|
722 | - 'sanitize_callback' => null, |
|
723 | - ), |
|
724 | - 'password' => array( |
|
725 | - 'required' => false, |
|
726 | - 'default' => '', |
|
727 | - 'type' => 'string' |
|
728 | - ) |
|
729 | - ); |
|
730 | - return apply_filters( |
|
731 | - 'FHEE__EED_Core_Rest_Api___get_response_selection_query_params', |
|
732 | - $query_params, |
|
733 | - $model, |
|
734 | - $version |
|
735 | - ); |
|
736 | - } |
|
737 | - |
|
738 | - |
|
739 | - /** |
|
740 | - * Gets the parameters acceptable for delete requests |
|
741 | - * |
|
742 | - * @param \EEM_Base $model |
|
743 | - * @param string $version |
|
744 | - * @return array |
|
745 | - */ |
|
746 | - protected function _get_delete_query_params(\EEM_Base $model, $version) |
|
747 | - { |
|
748 | - $params_for_delete = array( |
|
749 | - 'allow_blocking' => array( |
|
750 | - 'required' => false, |
|
751 | - 'default' => true, |
|
752 | - 'type' => 'boolean', |
|
753 | - ), |
|
754 | - ); |
|
755 | - $params_for_delete['force'] = array( |
|
756 | - 'required' => false, |
|
757 | - 'default' => false, |
|
758 | - 'type' => 'boolean', |
|
759 | - ); |
|
760 | - return apply_filters( |
|
761 | - 'FHEE__EED_Core_Rest_Api___get_delete_query_params', |
|
762 | - $params_for_delete, |
|
763 | - $model, |
|
764 | - $version |
|
765 | - ); |
|
766 | - } |
|
767 | - |
|
768 | - protected function _get_add_relation_query_params(\EEM_Base $source_model, \EEM_Base $related_model, $version) |
|
769 | - { |
|
770 | - // if they're related through a HABTM relation, check for any non-FKs |
|
771 | - $all_relation_settings = $source_model->relation_settings(); |
|
772 | - $relation_settings = $all_relation_settings[ $related_model->get_this_model_name() ]; |
|
773 | - $params = array(); |
|
774 | - if ($relation_settings instanceof EE_HABTM_Relation && $relation_settings->hasNonKeyFields()) { |
|
775 | - foreach ($relation_settings->getNonKeyFields() as $field) { |
|
776 | - /* @var $field EE_Model_Field_Base */ |
|
777 | - $params[ $field->get_name() ] = array( |
|
778 | - 'required' => ! $field->is_nullable(), |
|
779 | - 'default' => ModelDataTranslator::prepareFieldValueForJson($field, $field->get_default_value(), $version), |
|
780 | - 'type' => $field->getSchemaType(), |
|
781 | - 'validate_callbaack' => null, |
|
782 | - 'sanitize_callback' => null |
|
783 | - ); |
|
784 | - } |
|
785 | - } |
|
786 | - return $params; |
|
787 | - } |
|
788 | - |
|
789 | - |
|
790 | - /** |
|
791 | - * Gets info about reading query params that are acceptable |
|
792 | - * |
|
793 | - * @param \EEM_Base $model eg 'Event' or 'Venue' |
|
794 | - * @param string $version |
|
795 | - * @return array describing the args acceptable when querying this model |
|
796 | - * @throws EE_Error |
|
797 | - */ |
|
798 | - protected function _get_read_query_params(\EEM_Base $model, $version) |
|
799 | - { |
|
800 | - $default_orderby = array(); |
|
801 | - foreach ($model->get_combined_primary_key_fields() as $key_field) { |
|
802 | - $default_orderby[ $key_field->get_name() ] = 'ASC'; |
|
803 | - } |
|
804 | - return array_merge( |
|
805 | - $this->_get_response_selection_query_params($model, $version), |
|
806 | - array( |
|
807 | - 'where' => array( |
|
808 | - 'required' => false, |
|
809 | - 'default' => array(), |
|
810 | - 'type' => 'object', |
|
811 | - // because we accept an almost infinite list of possible where conditions, WP |
|
812 | - // core validation and sanitization freaks out. We'll just validate this argument |
|
813 | - // while handling the request |
|
814 | - 'validate_callback' => null, |
|
815 | - 'sanitize_callback' => null, |
|
816 | - ), |
|
817 | - 'limit' => array( |
|
818 | - 'required' => false, |
|
819 | - 'default' => EED_Core_Rest_Api::get_default_query_limit(), |
|
820 | - 'type' => array( |
|
821 | - 'array', |
|
822 | - 'string', |
|
823 | - 'integer', |
|
824 | - ), |
|
825 | - // because we accept a variety of types, WP core validation and sanitization |
|
826 | - // freaks out. We'll just validate this argument while handling the request |
|
827 | - 'validate_callback' => null, |
|
828 | - 'sanitize_callback' => null, |
|
829 | - ), |
|
830 | - 'order_by' => array( |
|
831 | - 'required' => false, |
|
832 | - 'default' => $default_orderby, |
|
833 | - 'type' => array( |
|
834 | - 'object', |
|
835 | - 'string', |
|
836 | - ),// because we accept a variety of types, WP core validation and sanitization |
|
837 | - // freaks out. We'll just validate this argument while handling the request |
|
838 | - 'validate_callback' => null, |
|
839 | - 'sanitize_callback' => null, |
|
840 | - ), |
|
841 | - 'group_by' => array( |
|
842 | - 'required' => false, |
|
843 | - 'default' => null, |
|
844 | - 'type' => array( |
|
845 | - 'object', |
|
846 | - 'string', |
|
847 | - ), |
|
848 | - // because we accept an almost infinite list of possible groupings, |
|
849 | - // WP core validation and sanitization |
|
850 | - // freaks out. We'll just validate this argument while handling the request |
|
851 | - 'validate_callback' => null, |
|
852 | - 'sanitize_callback' => null, |
|
853 | - ), |
|
854 | - 'having' => array( |
|
855 | - 'required' => false, |
|
856 | - 'default' => null, |
|
857 | - 'type' => 'object', |
|
858 | - // because we accept an almost infinite list of possible where conditions, WP |
|
859 | - // core validation and sanitization freaks out. We'll just validate this argument |
|
860 | - // while handling the request |
|
861 | - 'validate_callback' => null, |
|
862 | - 'sanitize_callback' => null, |
|
863 | - ), |
|
864 | - 'caps' => array( |
|
865 | - 'required' => false, |
|
866 | - 'default' => EEM_Base::caps_read, |
|
867 | - 'type' => 'string', |
|
868 | - 'enum' => array( |
|
869 | - EEM_Base::caps_read, |
|
870 | - EEM_Base::caps_read_admin, |
|
871 | - EEM_Base::caps_edit, |
|
872 | - EEM_Base::caps_delete, |
|
873 | - ), |
|
874 | - ), |
|
875 | - ) |
|
876 | - ); |
|
877 | - } |
|
878 | - |
|
879 | - |
|
880 | - /** |
|
881 | - * Gets parameter information for a model regarding writing data |
|
882 | - * |
|
883 | - * @param string $model_name |
|
884 | - * @param ModelVersionInfo $model_version_info |
|
885 | - * @param boolean $create whether this is for request to create (in |
|
886 | - * which case we need all required params) or |
|
887 | - * just to update (in which case we don't |
|
888 | - * need those on every request) |
|
889 | - * @return array |
|
890 | - */ |
|
891 | - protected function _get_write_params( |
|
892 | - $model_name, |
|
893 | - ModelVersionInfo $model_version_info, |
|
894 | - $create = false |
|
895 | - ) { |
|
896 | - $model = EE_Registry::instance()->load_model($model_name); |
|
897 | - $fields = $model_version_info->fieldsOnModelInThisVersion($model); |
|
898 | - $args_info = array(); |
|
899 | - foreach ($fields as $field_name => $field_obj) { |
|
900 | - if ($field_obj->is_auto_increment()) { |
|
901 | - // totally ignore auto increment IDs |
|
902 | - continue; |
|
903 | - } |
|
904 | - $arg_info = $field_obj->getSchema(); |
|
905 | - $required = $create && ! $field_obj->is_nullable() && $field_obj->get_default_value() === null; |
|
906 | - $arg_info['required'] = $required; |
|
907 | - // remove the read-only flag. If it were read-only we wouldn't list it as an argument while writing, right? |
|
908 | - unset($arg_info['readonly']); |
|
909 | - $schema_properties = $field_obj->getSchemaProperties(); |
|
910 | - if (isset($schema_properties['raw']) |
|
911 | - && $field_obj->getSchemaType() === 'object' |
|
912 | - ) { |
|
913 | - // if there's a "raw" form of this argument, use those properties instead |
|
914 | - $arg_info = array_replace( |
|
915 | - $arg_info, |
|
916 | - $schema_properties['raw'] |
|
917 | - ); |
|
918 | - } |
|
919 | - $arg_info['default'] = ModelDataTranslator::prepareFieldValueForJson( |
|
920 | - $field_obj, |
|
921 | - $field_obj->get_default_value(), |
|
922 | - $model_version_info->requestedVersion() |
|
923 | - ); |
|
924 | - // we do our own validation and sanitization within the controller |
|
925 | - if (function_exists('rest_validate_value_from_schema')) { |
|
926 | - $sanitize_callback = array( |
|
927 | - 'EED_Core_Rest_Api', |
|
928 | - 'default_sanitize_callback', |
|
929 | - ); |
|
930 | - } else { |
|
931 | - $sanitize_callback = null; |
|
932 | - } |
|
933 | - $arg_info['sanitize_callback'] = $sanitize_callback; |
|
934 | - $args_info[ $field_name ] = $arg_info; |
|
935 | - if ($field_obj instanceof EE_Datetime_Field) { |
|
936 | - $gmt_arg_info = $arg_info; |
|
937 | - $gmt_arg_info['description'] = sprintf( |
|
938 | - esc_html__( |
|
939 | - '%1$s - the value for this field in UTC. Ignored if %2$s is provided.', |
|
940 | - 'event_espresso' |
|
941 | - ), |
|
942 | - $field_obj->get_nicename(), |
|
943 | - $field_name |
|
944 | - ); |
|
945 | - $args_info[ $field_name . '_gmt' ] = $gmt_arg_info; |
|
946 | - } |
|
947 | - } |
|
948 | - return $args_info; |
|
949 | - } |
|
950 | - |
|
951 | - |
|
952 | - /** |
|
953 | - * Replacement for WP API's 'rest_parse_request_arg'. |
|
954 | - * If the value is blank but not required, don't bother validating it. |
|
955 | - * Also, it uses our email validation instead of WP API's default. |
|
956 | - * |
|
957 | - * @param $value |
|
958 | - * @param WP_REST_Request $request |
|
959 | - * @param $param |
|
960 | - * @return bool|true|WP_Error |
|
961 | - * @throws InvalidArgumentException |
|
962 | - * @throws InvalidInterfaceException |
|
963 | - * @throws InvalidDataTypeException |
|
964 | - */ |
|
965 | - public static function default_sanitize_callback($value, WP_REST_Request $request, $param) |
|
966 | - { |
|
967 | - $attributes = $request->get_attributes(); |
|
968 | - if (! isset($attributes['args'][ $param ]) |
|
969 | - || ! is_array($attributes['args'][ $param ])) { |
|
970 | - $validation_result = true; |
|
971 | - } else { |
|
972 | - $args = $attributes['args'][ $param ]; |
|
973 | - if (( |
|
974 | - $value === '' |
|
975 | - || $value === null |
|
976 | - ) |
|
977 | - && (! isset($args['required']) |
|
978 | - || $args['required'] === false |
|
979 | - ) |
|
980 | - ) { |
|
981 | - // not required and not provided? that's cool |
|
982 | - $validation_result = true; |
|
983 | - } elseif (isset($args['format']) |
|
984 | - && $args['format'] === 'email' |
|
985 | - ) { |
|
986 | - $validation_result = true; |
|
987 | - if (! self::_validate_email($value)) { |
|
988 | - $validation_result = new WP_Error( |
|
989 | - 'rest_invalid_param', |
|
990 | - esc_html__( |
|
991 | - 'The email address is not valid or does not exist.', |
|
992 | - 'event_espresso' |
|
993 | - ) |
|
994 | - ); |
|
995 | - } |
|
996 | - } else { |
|
997 | - $validation_result = rest_validate_value_from_schema($value, $args, $param); |
|
998 | - } |
|
999 | - } |
|
1000 | - if (is_wp_error($validation_result)) { |
|
1001 | - return $validation_result; |
|
1002 | - } |
|
1003 | - return rest_sanitize_request_arg($value, $request, $param); |
|
1004 | - } |
|
1005 | - |
|
1006 | - |
|
1007 | - /** |
|
1008 | - * Returns whether or not this email address is valid. Copied from EE_Email_Validation_Strategy::_validate_email() |
|
1009 | - * |
|
1010 | - * @param $email |
|
1011 | - * @return bool |
|
1012 | - * @throws InvalidArgumentException |
|
1013 | - * @throws InvalidInterfaceException |
|
1014 | - * @throws InvalidDataTypeException |
|
1015 | - */ |
|
1016 | - protected static function _validate_email($email) |
|
1017 | - { |
|
1018 | - try { |
|
1019 | - EmailAddressFactory::create($email); |
|
1020 | - return true; |
|
1021 | - } catch (EmailValidationException $e) { |
|
1022 | - return false; |
|
1023 | - } |
|
1024 | - } |
|
1025 | - |
|
1026 | - |
|
1027 | - /** |
|
1028 | - * Gets routes for the config |
|
1029 | - * |
|
1030 | - * @return array @see _register_model_routes |
|
1031 | - * @deprecated since version 4.9.1 |
|
1032 | - */ |
|
1033 | - protected function _register_config_routes() |
|
1034 | - { |
|
1035 | - $config_routes = array(); |
|
1036 | - foreach (self::versions_served() as $version => $hidden_endpoint) { |
|
1037 | - $config_routes[ self::ee_api_namespace . $version ] = $this->_get_config_route_data_for_version( |
|
1038 | - $version, |
|
1039 | - $hidden_endpoint |
|
1040 | - ); |
|
1041 | - } |
|
1042 | - return $config_routes; |
|
1043 | - } |
|
1044 | - |
|
1045 | - |
|
1046 | - /** |
|
1047 | - * Gets routes for the config for the specified version |
|
1048 | - * |
|
1049 | - * @param string $version |
|
1050 | - * @param boolean $hidden_endpoint |
|
1051 | - * @return array |
|
1052 | - */ |
|
1053 | - protected function _get_config_route_data_for_version($version, $hidden_endpoint) |
|
1054 | - { |
|
1055 | - return array( |
|
1056 | - 'config' => array( |
|
1057 | - array( |
|
1058 | - 'callback' => array( |
|
1059 | - 'EventEspresso\core\libraries\rest_api\controllers\config\Read', |
|
1060 | - 'handleRequest', |
|
1061 | - ), |
|
1062 | - 'methods' => WP_REST_Server::READABLE, |
|
1063 | - 'hidden_endpoint' => $hidden_endpoint, |
|
1064 | - 'callback_args' => array($version), |
|
1065 | - ), |
|
1066 | - ), |
|
1067 | - 'site_info' => array( |
|
1068 | - array( |
|
1069 | - 'callback' => array( |
|
1070 | - 'EventEspresso\core\libraries\rest_api\controllers\config\Read', |
|
1071 | - 'handleRequestSiteInfo', |
|
1072 | - ), |
|
1073 | - 'methods' => WP_REST_Server::READABLE, |
|
1074 | - 'hidden_endpoint' => $hidden_endpoint, |
|
1075 | - 'callback_args' => array($version), |
|
1076 | - ), |
|
1077 | - ), |
|
1078 | - ); |
|
1079 | - } |
|
1080 | - |
|
1081 | - |
|
1082 | - /** |
|
1083 | - * Gets the meta info routes |
|
1084 | - * |
|
1085 | - * @return array @see _register_model_routes |
|
1086 | - * @deprecated since version 4.9.1 |
|
1087 | - */ |
|
1088 | - protected function _register_meta_routes() |
|
1089 | - { |
|
1090 | - $meta_routes = array(); |
|
1091 | - foreach (self::versions_served() as $version => $hidden_endpoint) { |
|
1092 | - $meta_routes[ self::ee_api_namespace . $version ] = $this->_get_meta_route_data_for_version( |
|
1093 | - $version, |
|
1094 | - $hidden_endpoint |
|
1095 | - ); |
|
1096 | - } |
|
1097 | - return $meta_routes; |
|
1098 | - } |
|
1099 | - |
|
1100 | - |
|
1101 | - /** |
|
1102 | - * @param string $version |
|
1103 | - * @param boolean $hidden_endpoint |
|
1104 | - * @return array |
|
1105 | - */ |
|
1106 | - protected function _get_meta_route_data_for_version($version, $hidden_endpoint = false) |
|
1107 | - { |
|
1108 | - return array( |
|
1109 | - 'resources' => array( |
|
1110 | - array( |
|
1111 | - 'callback' => array( |
|
1112 | - 'EventEspresso\core\libraries\rest_api\controllers\model\Meta', |
|
1113 | - 'handleRequestModelsMeta', |
|
1114 | - ), |
|
1115 | - 'methods' => WP_REST_Server::READABLE, |
|
1116 | - 'hidden_endpoint' => $hidden_endpoint, |
|
1117 | - 'callback_args' => array($version), |
|
1118 | - ), |
|
1119 | - ), |
|
1120 | - ); |
|
1121 | - } |
|
1122 | - |
|
1123 | - |
|
1124 | - /** |
|
1125 | - * Tries to hide old 4.6 endpoints from the |
|
1126 | - * |
|
1127 | - * @param array $route_data |
|
1128 | - * @return array |
|
1129 | - * @throws \EE_Error |
|
1130 | - */ |
|
1131 | - public static function hide_old_endpoints($route_data) |
|
1132 | - { |
|
1133 | - // allow API clients to override which endpoints get hidden, in case |
|
1134 | - // they want to discover particular endpoints |
|
1135 | - // also, we don't have access to the request so we have to just grab it from the superglobal |
|
1136 | - $force_show_ee_namespace = ltrim( |
|
1137 | - EEH_Array::is_set($_REQUEST, 'force_show_ee_namespace', ''), |
|
1138 | - '/' |
|
1139 | - ); |
|
1140 | - foreach (EED_Core_Rest_Api::get_ee_route_data() as $namespace => $relative_urls) { |
|
1141 | - foreach ($relative_urls as $resource_name => $endpoints) { |
|
1142 | - foreach ($endpoints as $key => $endpoint) { |
|
1143 | - // skip schema and other route options |
|
1144 | - if (! is_numeric($key)) { |
|
1145 | - continue; |
|
1146 | - } |
|
1147 | - // by default, hide "hidden_endpoint"s, unless the request indicates |
|
1148 | - // to $force_show_ee_namespace, in which case only show that one |
|
1149 | - // namespace's endpoints (and hide all others) |
|
1150 | - if (($force_show_ee_namespace !== '' && $force_show_ee_namespace !== $namespace) |
|
1151 | - || ($endpoint['hidden_endpoint'] && $force_show_ee_namespace === '') |
|
1152 | - ) { |
|
1153 | - $full_route = '/' . ltrim($namespace, '/'); |
|
1154 | - $full_route .= '/' . ltrim($resource_name, '/'); |
|
1155 | - unset($route_data[ $full_route ]); |
|
1156 | - } |
|
1157 | - } |
|
1158 | - } |
|
1159 | - } |
|
1160 | - return $route_data; |
|
1161 | - } |
|
1162 | - |
|
1163 | - |
|
1164 | - /** |
|
1165 | - * Returns an array describing which versions of core support serving requests for. |
|
1166 | - * Keys are core versions' major and minor version, and values are the |
|
1167 | - * LOWEST requested version they can serve. Eg, 4.7 can serve requests for 4.6-like |
|
1168 | - * data by just removing a few models and fields from the responses. However, 4.15 might remove |
|
1169 | - * the answers table entirely, in which case it would be very difficult for |
|
1170 | - * it to serve 4.6-style responses. |
|
1171 | - * Versions of core that are missing from this array are unknowns. |
|
1172 | - * previous ver |
|
1173 | - * |
|
1174 | - * @return array |
|
1175 | - */ |
|
1176 | - public static function version_compatibilities() |
|
1177 | - { |
|
1178 | - return apply_filters( |
|
1179 | - 'FHEE__EED_Core_REST_API__version_compatibilities', |
|
1180 | - array( |
|
1181 | - '4.8.29' => '4.8.29', |
|
1182 | - '4.8.33' => '4.8.29', |
|
1183 | - '4.8.34' => '4.8.29', |
|
1184 | - '4.8.36' => '4.8.29', |
|
1185 | - ) |
|
1186 | - ); |
|
1187 | - } |
|
1188 | - |
|
1189 | - |
|
1190 | - /** |
|
1191 | - * Gets the latest API version served. Eg if there |
|
1192 | - * are two versions served of the API, 4.8.29 and 4.8.32, and |
|
1193 | - * we are on core version 4.8.34, it will return the string "4.8.32" |
|
1194 | - * |
|
1195 | - * @return string |
|
1196 | - */ |
|
1197 | - public static function latest_rest_api_version() |
|
1198 | - { |
|
1199 | - $versions_served = \EED_Core_Rest_Api::versions_served(); |
|
1200 | - $versions_served_keys = array_keys($versions_served); |
|
1201 | - return end($versions_served_keys); |
|
1202 | - } |
|
1203 | - |
|
1204 | - |
|
1205 | - /** |
|
1206 | - * Using EED_Core_Rest_Api::version_compatibilities(), determines what version of |
|
1207 | - * EE the API can serve requests for. Eg, if we are on 4.15 of core, and |
|
1208 | - * we can serve requests from 4.12 or later, this will return array( '4.12', '4.13', '4.14', '4.15' ). |
|
1209 | - * We also indicate whether or not this version should be put in the index or not |
|
1210 | - * |
|
1211 | - * @return array keys are API version numbers (just major and minor numbers), and values |
|
1212 | - * are whether or not they should be hidden |
|
1213 | - */ |
|
1214 | - public static function versions_served() |
|
1215 | - { |
|
1216 | - $versions_served = array(); |
|
1217 | - $possibly_served_versions = EED_Core_Rest_Api::version_compatibilities(); |
|
1218 | - $lowest_compatible_version = end($possibly_served_versions); |
|
1219 | - reset($possibly_served_versions); |
|
1220 | - $versions_served_historically = array_keys($possibly_served_versions); |
|
1221 | - $latest_version = end($versions_served_historically); |
|
1222 | - reset($versions_served_historically); |
|
1223 | - // for each version of core we have ever served: |
|
1224 | - foreach ($versions_served_historically as $key_versioned_endpoint) { |
|
1225 | - // if it's not above the current core version, and it's compatible with the current version of core |
|
1226 | - |
|
1227 | - if ($key_versioned_endpoint === $latest_version) { |
|
1228 | - // don't hide the latest version in the index |
|
1229 | - $versions_served[ $key_versioned_endpoint ] = false; |
|
1230 | - } elseif (version_compare($key_versioned_endpoint, $lowest_compatible_version, '>=') |
|
1231 | - && version_compare($key_versioned_endpoint, EED_Core_Rest_Api::core_version(), '<') |
|
1232 | - ) { |
|
1233 | - // include, but hide, previous versions which are still supported |
|
1234 | - $versions_served[ $key_versioned_endpoint ] = true; |
|
1235 | - } elseif (apply_filters( |
|
1236 | - 'FHEE__EED_Core_Rest_Api__versions_served__include_incompatible_versions', |
|
1237 | - false, |
|
1238 | - $possibly_served_versions |
|
1239 | - )) { |
|
1240 | - // if a version is no longer supported, don't include it in index or list of versions served |
|
1241 | - $versions_served[ $key_versioned_endpoint ] = true; |
|
1242 | - } |
|
1243 | - } |
|
1244 | - return $versions_served; |
|
1245 | - } |
|
1246 | - |
|
1247 | - |
|
1248 | - /** |
|
1249 | - * Gets the major and minor version of EE core's version string |
|
1250 | - * |
|
1251 | - * @return string |
|
1252 | - */ |
|
1253 | - public static function core_version() |
|
1254 | - { |
|
1255 | - return apply_filters( |
|
1256 | - 'FHEE__EED_Core_REST_API__core_version', |
|
1257 | - implode( |
|
1258 | - '.', |
|
1259 | - array_slice( |
|
1260 | - explode( |
|
1261 | - '.', |
|
1262 | - espresso_version() |
|
1263 | - ), |
|
1264 | - 0, |
|
1265 | - 3 |
|
1266 | - ) |
|
1267 | - ) |
|
1268 | - ); |
|
1269 | - } |
|
1270 | - |
|
1271 | - |
|
1272 | - /** |
|
1273 | - * Gets the default limit that should be used when querying for resources |
|
1274 | - * |
|
1275 | - * @return int |
|
1276 | - */ |
|
1277 | - public static function get_default_query_limit() |
|
1278 | - { |
|
1279 | - // we actually don't use a const because we want folks to always use |
|
1280 | - // this method, not the const directly |
|
1281 | - return apply_filters( |
|
1282 | - 'FHEE__EED_Core_Rest_Api__get_default_query_limit', |
|
1283 | - 50 |
|
1284 | - ); |
|
1285 | - } |
|
1286 | - |
|
1287 | - |
|
1288 | - /** |
|
1289 | - * |
|
1290 | - * @param string $version api version string (i.e. '4.8.36') |
|
1291 | - * @return array |
|
1292 | - */ |
|
1293 | - public static function getCollectionRoutesIndexedByModelName($version = '') |
|
1294 | - { |
|
1295 | - $version = empty($version) ? self::latest_rest_api_version() : $version; |
|
1296 | - $model_names = self::model_names_with_plural_routes($version); |
|
1297 | - $collection_routes = array(); |
|
1298 | - foreach ($model_names as $model_name => $model_class_name) { |
|
1299 | - $collection_routes[ strtolower($model_name) ] = '/' . self::ee_api_namespace . $version . '/' |
|
1300 | - . EEH_Inflector::pluralize_and_lower($model_name); |
|
1301 | - } |
|
1302 | - return $collection_routes; |
|
1303 | - } |
|
1304 | - |
|
1305 | - |
|
1306 | - /** |
|
1307 | - * Returns an array of primary key names indexed by model names. |
|
1308 | - * @param string $version |
|
1309 | - * @return array |
|
1310 | - */ |
|
1311 | - public static function getPrimaryKeyNamesIndexedByModelName($version = '') |
|
1312 | - { |
|
1313 | - $version = empty($version) ? self::latest_rest_api_version() : $version; |
|
1314 | - $model_names = self::model_names_with_plural_routes($version); |
|
1315 | - $primary_key_items = array(); |
|
1316 | - foreach ($model_names as $model_name => $model_class_name) { |
|
1317 | - $primary_keys = $model_class_name::instance()->get_combined_primary_key_fields(); |
|
1318 | - foreach ($primary_keys as $primary_key_name => $primary_key_field) { |
|
1319 | - if (count($primary_keys) > 1) { |
|
1320 | - $primary_key_items[ strtolower($model_name) ][] = $primary_key_name; |
|
1321 | - } else { |
|
1322 | - $primary_key_items[ strtolower($model_name) ] = $primary_key_name; |
|
1323 | - } |
|
1324 | - } |
|
1325 | - } |
|
1326 | - return $primary_key_items; |
|
1327 | - } |
|
1328 | - |
|
1329 | - /** |
|
1330 | - * Determines the EE REST API debug mode is activated, or not. |
|
1331 | - * @since 4.9.76.p |
|
1332 | - * @return bool |
|
1333 | - */ |
|
1334 | - public static function debugMode() |
|
1335 | - { |
|
1336 | - static $debug_mode = null; // could be class prop |
|
1337 | - if ($debug_mode === null) { |
|
1338 | - $debug_mode = defined('EE_REST_API_DEBUG_MODE') && EE_REST_API_DEBUG_MODE; |
|
1339 | - } |
|
1340 | - return $debug_mode; |
|
1341 | - } |
|
1342 | - |
|
1343 | - |
|
1344 | - |
|
1345 | - /** |
|
1346 | - * run - initial module setup |
|
1347 | - * |
|
1348 | - * @access public |
|
1349 | - * @param WP $WP |
|
1350 | - * @return void |
|
1351 | - */ |
|
1352 | - public function run($WP) |
|
1353 | - { |
|
1354 | - } |
|
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 || EED_Core_Rest_Api::debugMode()) { |
|
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, true), |
|
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 | + $model_routes[ $related_route ] = 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 | + |
|
547 | + $related_write_route = $related_route . '/' . '(?P<related_id>[^\/]+)'; |
|
548 | + $model_routes[ $related_write_route ] = array( |
|
549 | + array( |
|
550 | + 'callback' => array( |
|
551 | + 'EventEspresso\core\libraries\rest_api\controllers\model\Write', |
|
552 | + 'handleRequestAddRelation', |
|
553 | + ), |
|
554 | + 'callback_args' => array($version, $model_name, $relation_name), |
|
555 | + 'methods' => WP_REST_Server::EDITABLE, |
|
556 | + 'hidden_endpoint' => $hidden_endpoint, |
|
557 | + 'args' => $this->_get_add_relation_query_params($model, $relation_obj->get_other_model(), $version) |
|
558 | + ), |
|
559 | + array( |
|
560 | + 'callback' => array( |
|
561 | + 'EventEspresso\core\libraries\rest_api\controllers\model\Write', |
|
562 | + 'handleRequestRemoveRelation', |
|
563 | + ), |
|
564 | + 'callback_args' => array($version, $model_name, $relation_name), |
|
565 | + 'methods' => WP_REST_Server::DELETABLE, |
|
566 | + 'hidden_endpoint' => $hidden_endpoint, |
|
567 | + 'args' => array() |
|
568 | + ), |
|
569 | + ); |
|
570 | + } |
|
571 | + } |
|
572 | + return $model_routes; |
|
573 | + } |
|
574 | + |
|
575 | + |
|
576 | + /** |
|
577 | + * Gets the relative URI to a model's REST API plural route, after the EE4 versioned namespace, |
|
578 | + * excluding the preceding slash. |
|
579 | + * Eg you pass get_plural_route_to('Event') = 'events' |
|
580 | + * |
|
581 | + * @param EEM_Base $model |
|
582 | + * @return string |
|
583 | + */ |
|
584 | + public static function get_collection_route(EEM_Base $model) |
|
585 | + { |
|
586 | + return EEH_Inflector::pluralize_and_lower($model->get_this_model_name()); |
|
587 | + } |
|
588 | + |
|
589 | + |
|
590 | + /** |
|
591 | + * Gets the relative URI to a model's REST API singular route, after the EE4 versioned namespace, |
|
592 | + * excluding the preceding slash. |
|
593 | + * Eg you pass get_plural_route_to('Event', 12) = 'events/12' |
|
594 | + * |
|
595 | + * @param EEM_Base $model eg Event or Venue |
|
596 | + * @param string $id |
|
597 | + * @return string |
|
598 | + */ |
|
599 | + public static function get_entity_route($model, $id) |
|
600 | + { |
|
601 | + return EED_Core_Rest_Api::get_collection_route($model) . '/' . $id; |
|
602 | + } |
|
603 | + |
|
604 | + |
|
605 | + /** |
|
606 | + * Gets the relative URI to a model's REST API singular route, after the EE4 versioned namespace, |
|
607 | + * excluding the preceding slash. |
|
608 | + * Eg you pass get_plural_route_to('Event', 12) = 'events/12' |
|
609 | + * |
|
610 | + * @param EEM_Base $model eg Event or Venue |
|
611 | + * @param string $id |
|
612 | + * @param EE_Model_Relation_Base $relation_obj |
|
613 | + * @return string |
|
614 | + */ |
|
615 | + public static function get_relation_route_via(EEM_Base $model, $id, EE_Model_Relation_Base $relation_obj) |
|
616 | + { |
|
617 | + $related_model_name_endpoint_part = ModelRead::getRelatedEntityName( |
|
618 | + $relation_obj->get_other_model()->get_this_model_name(), |
|
619 | + $relation_obj |
|
620 | + ); |
|
621 | + return EED_Core_Rest_Api::get_entity_route($model, $id) . '/' . $related_model_name_endpoint_part; |
|
622 | + } |
|
623 | + |
|
624 | + |
|
625 | + /** |
|
626 | + * Adds onto the $relative_route the EE4 REST API versioned namespace. |
|
627 | + * Eg if given '4.8.36' and 'events', will return 'ee/v4.8.36/events' |
|
628 | + * |
|
629 | + * @param string $relative_route |
|
630 | + * @param string $version |
|
631 | + * @return string |
|
632 | + */ |
|
633 | + public static function get_versioned_route_to($relative_route, $version = '4.8.36') |
|
634 | + { |
|
635 | + return '/' . EED_Core_Rest_Api::ee_api_namespace . $version . '/' . $relative_route; |
|
636 | + } |
|
637 | + |
|
638 | + |
|
639 | + /** |
|
640 | + * Adds all the RPC-style routes (remote procedure call-like routes, ie |
|
641 | + * routes that don't conform to the traditional REST CRUD-style). |
|
642 | + * |
|
643 | + * @deprecated since 4.9.1 |
|
644 | + */ |
|
645 | + protected function _register_rpc_routes() |
|
646 | + { |
|
647 | + $routes = array(); |
|
648 | + foreach (self::versions_served() as $version => $hidden_endpoint) { |
|
649 | + $routes[ self::ee_api_namespace . $version ] = $this->_get_rpc_route_data_for_version( |
|
650 | + $version, |
|
651 | + $hidden_endpoint |
|
652 | + ); |
|
653 | + } |
|
654 | + return $routes; |
|
655 | + } |
|
656 | + |
|
657 | + |
|
658 | + /** |
|
659 | + * @param string $version |
|
660 | + * @param boolean $hidden_endpoint |
|
661 | + * @return array |
|
662 | + */ |
|
663 | + protected function _get_rpc_route_data_for_version($version, $hidden_endpoint = false) |
|
664 | + { |
|
665 | + $this_versions_routes = array(); |
|
666 | + // checkin endpoint |
|
667 | + $this_versions_routes['registrations/(?P<REG_ID>\d+)/toggle_checkin_for_datetime/(?P<DTT_ID>\d+)'] = array( |
|
668 | + array( |
|
669 | + 'callback' => array( |
|
670 | + 'EventEspresso\core\libraries\rest_api\controllers\rpc\Checkin', |
|
671 | + 'handleRequestToggleCheckin', |
|
672 | + ), |
|
673 | + 'methods' => WP_REST_Server::CREATABLE, |
|
674 | + 'hidden_endpoint' => $hidden_endpoint, |
|
675 | + 'args' => array( |
|
676 | + 'force' => array( |
|
677 | + 'required' => false, |
|
678 | + 'default' => false, |
|
679 | + 'description' => __( |
|
680 | + // @codingStandardsIgnoreStart |
|
681 | + 'Whether to force toggle checkin, or to verify the registration status and allowed ticket uses', |
|
682 | + // @codingStandardsIgnoreEnd |
|
683 | + 'event_espresso' |
|
684 | + ), |
|
685 | + ), |
|
686 | + ), |
|
687 | + 'callback_args' => array($version), |
|
688 | + ), |
|
689 | + ); |
|
690 | + return apply_filters( |
|
691 | + 'FHEE__EED_Core_Rest_Api___register_rpc_routes__this_versions_routes', |
|
692 | + $this_versions_routes, |
|
693 | + $version, |
|
694 | + $hidden_endpoint |
|
695 | + ); |
|
696 | + } |
|
697 | + |
|
698 | + |
|
699 | + /** |
|
700 | + * Gets the query params that can be used when request one or many |
|
701 | + * |
|
702 | + * @param EEM_Base $model |
|
703 | + * @param string $version |
|
704 | + * @return array |
|
705 | + */ |
|
706 | + protected function _get_response_selection_query_params(\EEM_Base $model, $version, $single_only = false) |
|
707 | + { |
|
708 | + $query_params = array( |
|
709 | + 'include' => array( |
|
710 | + 'required' => false, |
|
711 | + 'default' => '*', |
|
712 | + 'type' => 'string', |
|
713 | + ), |
|
714 | + 'calculate' => array( |
|
715 | + 'required' => false, |
|
716 | + 'default' => '', |
|
717 | + 'enum' => self::$_field_calculator->retrieveCalculatedFieldsForModel($model), |
|
718 | + 'type' => 'string', |
|
719 | + // because we accept a CSV'd list of the enumerated strings, WP core validation and sanitization |
|
720 | + // freaks out. We'll just validate this argument while handling the request |
|
721 | + 'validate_callback' => null, |
|
722 | + 'sanitize_callback' => null, |
|
723 | + ), |
|
724 | + 'password' => array( |
|
725 | + 'required' => false, |
|
726 | + 'default' => '', |
|
727 | + 'type' => 'string' |
|
728 | + ) |
|
729 | + ); |
|
730 | + return apply_filters( |
|
731 | + 'FHEE__EED_Core_Rest_Api___get_response_selection_query_params', |
|
732 | + $query_params, |
|
733 | + $model, |
|
734 | + $version |
|
735 | + ); |
|
736 | + } |
|
737 | + |
|
738 | + |
|
739 | + /** |
|
740 | + * Gets the parameters acceptable for delete requests |
|
741 | + * |
|
742 | + * @param \EEM_Base $model |
|
743 | + * @param string $version |
|
744 | + * @return array |
|
745 | + */ |
|
746 | + protected function _get_delete_query_params(\EEM_Base $model, $version) |
|
747 | + { |
|
748 | + $params_for_delete = array( |
|
749 | + 'allow_blocking' => array( |
|
750 | + 'required' => false, |
|
751 | + 'default' => true, |
|
752 | + 'type' => 'boolean', |
|
753 | + ), |
|
754 | + ); |
|
755 | + $params_for_delete['force'] = array( |
|
756 | + 'required' => false, |
|
757 | + 'default' => false, |
|
758 | + 'type' => 'boolean', |
|
759 | + ); |
|
760 | + return apply_filters( |
|
761 | + 'FHEE__EED_Core_Rest_Api___get_delete_query_params', |
|
762 | + $params_for_delete, |
|
763 | + $model, |
|
764 | + $version |
|
765 | + ); |
|
766 | + } |
|
767 | + |
|
768 | + protected function _get_add_relation_query_params(\EEM_Base $source_model, \EEM_Base $related_model, $version) |
|
769 | + { |
|
770 | + // if they're related through a HABTM relation, check for any non-FKs |
|
771 | + $all_relation_settings = $source_model->relation_settings(); |
|
772 | + $relation_settings = $all_relation_settings[ $related_model->get_this_model_name() ]; |
|
773 | + $params = array(); |
|
774 | + if ($relation_settings instanceof EE_HABTM_Relation && $relation_settings->hasNonKeyFields()) { |
|
775 | + foreach ($relation_settings->getNonKeyFields() as $field) { |
|
776 | + /* @var $field EE_Model_Field_Base */ |
|
777 | + $params[ $field->get_name() ] = array( |
|
778 | + 'required' => ! $field->is_nullable(), |
|
779 | + 'default' => ModelDataTranslator::prepareFieldValueForJson($field, $field->get_default_value(), $version), |
|
780 | + 'type' => $field->getSchemaType(), |
|
781 | + 'validate_callbaack' => null, |
|
782 | + 'sanitize_callback' => null |
|
783 | + ); |
|
784 | + } |
|
785 | + } |
|
786 | + return $params; |
|
787 | + } |
|
788 | + |
|
789 | + |
|
790 | + /** |
|
791 | + * Gets info about reading query params that are acceptable |
|
792 | + * |
|
793 | + * @param \EEM_Base $model eg 'Event' or 'Venue' |
|
794 | + * @param string $version |
|
795 | + * @return array describing the args acceptable when querying this model |
|
796 | + * @throws EE_Error |
|
797 | + */ |
|
798 | + protected function _get_read_query_params(\EEM_Base $model, $version) |
|
799 | + { |
|
800 | + $default_orderby = array(); |
|
801 | + foreach ($model->get_combined_primary_key_fields() as $key_field) { |
|
802 | + $default_orderby[ $key_field->get_name() ] = 'ASC'; |
|
803 | + } |
|
804 | + return array_merge( |
|
805 | + $this->_get_response_selection_query_params($model, $version), |
|
806 | + array( |
|
807 | + 'where' => array( |
|
808 | + 'required' => false, |
|
809 | + 'default' => array(), |
|
810 | + 'type' => 'object', |
|
811 | + // because we accept an almost infinite list of possible where conditions, WP |
|
812 | + // core validation and sanitization freaks out. We'll just validate this argument |
|
813 | + // while handling the request |
|
814 | + 'validate_callback' => null, |
|
815 | + 'sanitize_callback' => null, |
|
816 | + ), |
|
817 | + 'limit' => array( |
|
818 | + 'required' => false, |
|
819 | + 'default' => EED_Core_Rest_Api::get_default_query_limit(), |
|
820 | + 'type' => array( |
|
821 | + 'array', |
|
822 | + 'string', |
|
823 | + 'integer', |
|
824 | + ), |
|
825 | + // because we accept a variety of types, WP core validation and sanitization |
|
826 | + // freaks out. We'll just validate this argument while handling the request |
|
827 | + 'validate_callback' => null, |
|
828 | + 'sanitize_callback' => null, |
|
829 | + ), |
|
830 | + 'order_by' => array( |
|
831 | + 'required' => false, |
|
832 | + 'default' => $default_orderby, |
|
833 | + 'type' => array( |
|
834 | + 'object', |
|
835 | + 'string', |
|
836 | + ),// because we accept a variety of types, WP core validation and sanitization |
|
837 | + // freaks out. We'll just validate this argument while handling the request |
|
838 | + 'validate_callback' => null, |
|
839 | + 'sanitize_callback' => null, |
|
840 | + ), |
|
841 | + 'group_by' => array( |
|
842 | + 'required' => false, |
|
843 | + 'default' => null, |
|
844 | + 'type' => array( |
|
845 | + 'object', |
|
846 | + 'string', |
|
847 | + ), |
|
848 | + // because we accept an almost infinite list of possible groupings, |
|
849 | + // WP core validation and sanitization |
|
850 | + // freaks out. We'll just validate this argument while handling the request |
|
851 | + 'validate_callback' => null, |
|
852 | + 'sanitize_callback' => null, |
|
853 | + ), |
|
854 | + 'having' => array( |
|
855 | + 'required' => false, |
|
856 | + 'default' => null, |
|
857 | + 'type' => 'object', |
|
858 | + // because we accept an almost infinite list of possible where conditions, WP |
|
859 | + // core validation and sanitization freaks out. We'll just validate this argument |
|
860 | + // while handling the request |
|
861 | + 'validate_callback' => null, |
|
862 | + 'sanitize_callback' => null, |
|
863 | + ), |
|
864 | + 'caps' => array( |
|
865 | + 'required' => false, |
|
866 | + 'default' => EEM_Base::caps_read, |
|
867 | + 'type' => 'string', |
|
868 | + 'enum' => array( |
|
869 | + EEM_Base::caps_read, |
|
870 | + EEM_Base::caps_read_admin, |
|
871 | + EEM_Base::caps_edit, |
|
872 | + EEM_Base::caps_delete, |
|
873 | + ), |
|
874 | + ), |
|
875 | + ) |
|
876 | + ); |
|
877 | + } |
|
878 | + |
|
879 | + |
|
880 | + /** |
|
881 | + * Gets parameter information for a model regarding writing data |
|
882 | + * |
|
883 | + * @param string $model_name |
|
884 | + * @param ModelVersionInfo $model_version_info |
|
885 | + * @param boolean $create whether this is for request to create (in |
|
886 | + * which case we need all required params) or |
|
887 | + * just to update (in which case we don't |
|
888 | + * need those on every request) |
|
889 | + * @return array |
|
890 | + */ |
|
891 | + protected function _get_write_params( |
|
892 | + $model_name, |
|
893 | + ModelVersionInfo $model_version_info, |
|
894 | + $create = false |
|
895 | + ) { |
|
896 | + $model = EE_Registry::instance()->load_model($model_name); |
|
897 | + $fields = $model_version_info->fieldsOnModelInThisVersion($model); |
|
898 | + $args_info = array(); |
|
899 | + foreach ($fields as $field_name => $field_obj) { |
|
900 | + if ($field_obj->is_auto_increment()) { |
|
901 | + // totally ignore auto increment IDs |
|
902 | + continue; |
|
903 | + } |
|
904 | + $arg_info = $field_obj->getSchema(); |
|
905 | + $required = $create && ! $field_obj->is_nullable() && $field_obj->get_default_value() === null; |
|
906 | + $arg_info['required'] = $required; |
|
907 | + // remove the read-only flag. If it were read-only we wouldn't list it as an argument while writing, right? |
|
908 | + unset($arg_info['readonly']); |
|
909 | + $schema_properties = $field_obj->getSchemaProperties(); |
|
910 | + if (isset($schema_properties['raw']) |
|
911 | + && $field_obj->getSchemaType() === 'object' |
|
912 | + ) { |
|
913 | + // if there's a "raw" form of this argument, use those properties instead |
|
914 | + $arg_info = array_replace( |
|
915 | + $arg_info, |
|
916 | + $schema_properties['raw'] |
|
917 | + ); |
|
918 | + } |
|
919 | + $arg_info['default'] = ModelDataTranslator::prepareFieldValueForJson( |
|
920 | + $field_obj, |
|
921 | + $field_obj->get_default_value(), |
|
922 | + $model_version_info->requestedVersion() |
|
923 | + ); |
|
924 | + // we do our own validation and sanitization within the controller |
|
925 | + if (function_exists('rest_validate_value_from_schema')) { |
|
926 | + $sanitize_callback = array( |
|
927 | + 'EED_Core_Rest_Api', |
|
928 | + 'default_sanitize_callback', |
|
929 | + ); |
|
930 | + } else { |
|
931 | + $sanitize_callback = null; |
|
932 | + } |
|
933 | + $arg_info['sanitize_callback'] = $sanitize_callback; |
|
934 | + $args_info[ $field_name ] = $arg_info; |
|
935 | + if ($field_obj instanceof EE_Datetime_Field) { |
|
936 | + $gmt_arg_info = $arg_info; |
|
937 | + $gmt_arg_info['description'] = sprintf( |
|
938 | + esc_html__( |
|
939 | + '%1$s - the value for this field in UTC. Ignored if %2$s is provided.', |
|
940 | + 'event_espresso' |
|
941 | + ), |
|
942 | + $field_obj->get_nicename(), |
|
943 | + $field_name |
|
944 | + ); |
|
945 | + $args_info[ $field_name . '_gmt' ] = $gmt_arg_info; |
|
946 | + } |
|
947 | + } |
|
948 | + return $args_info; |
|
949 | + } |
|
950 | + |
|
951 | + |
|
952 | + /** |
|
953 | + * Replacement for WP API's 'rest_parse_request_arg'. |
|
954 | + * If the value is blank but not required, don't bother validating it. |
|
955 | + * Also, it uses our email validation instead of WP API's default. |
|
956 | + * |
|
957 | + * @param $value |
|
958 | + * @param WP_REST_Request $request |
|
959 | + * @param $param |
|
960 | + * @return bool|true|WP_Error |
|
961 | + * @throws InvalidArgumentException |
|
962 | + * @throws InvalidInterfaceException |
|
963 | + * @throws InvalidDataTypeException |
|
964 | + */ |
|
965 | + public static function default_sanitize_callback($value, WP_REST_Request $request, $param) |
|
966 | + { |
|
967 | + $attributes = $request->get_attributes(); |
|
968 | + if (! isset($attributes['args'][ $param ]) |
|
969 | + || ! is_array($attributes['args'][ $param ])) { |
|
970 | + $validation_result = true; |
|
971 | + } else { |
|
972 | + $args = $attributes['args'][ $param ]; |
|
973 | + if (( |
|
974 | + $value === '' |
|
975 | + || $value === null |
|
976 | + ) |
|
977 | + && (! isset($args['required']) |
|
978 | + || $args['required'] === false |
|
979 | + ) |
|
980 | + ) { |
|
981 | + // not required and not provided? that's cool |
|
982 | + $validation_result = true; |
|
983 | + } elseif (isset($args['format']) |
|
984 | + && $args['format'] === 'email' |
|
985 | + ) { |
|
986 | + $validation_result = true; |
|
987 | + if (! self::_validate_email($value)) { |
|
988 | + $validation_result = new WP_Error( |
|
989 | + 'rest_invalid_param', |
|
990 | + esc_html__( |
|
991 | + 'The email address is not valid or does not exist.', |
|
992 | + 'event_espresso' |
|
993 | + ) |
|
994 | + ); |
|
995 | + } |
|
996 | + } else { |
|
997 | + $validation_result = rest_validate_value_from_schema($value, $args, $param); |
|
998 | + } |
|
999 | + } |
|
1000 | + if (is_wp_error($validation_result)) { |
|
1001 | + return $validation_result; |
|
1002 | + } |
|
1003 | + return rest_sanitize_request_arg($value, $request, $param); |
|
1004 | + } |
|
1005 | + |
|
1006 | + |
|
1007 | + /** |
|
1008 | + * Returns whether or not this email address is valid. Copied from EE_Email_Validation_Strategy::_validate_email() |
|
1009 | + * |
|
1010 | + * @param $email |
|
1011 | + * @return bool |
|
1012 | + * @throws InvalidArgumentException |
|
1013 | + * @throws InvalidInterfaceException |
|
1014 | + * @throws InvalidDataTypeException |
|
1015 | + */ |
|
1016 | + protected static function _validate_email($email) |
|
1017 | + { |
|
1018 | + try { |
|
1019 | + EmailAddressFactory::create($email); |
|
1020 | + return true; |
|
1021 | + } catch (EmailValidationException $e) { |
|
1022 | + return false; |
|
1023 | + } |
|
1024 | + } |
|
1025 | + |
|
1026 | + |
|
1027 | + /** |
|
1028 | + * Gets routes for the config |
|
1029 | + * |
|
1030 | + * @return array @see _register_model_routes |
|
1031 | + * @deprecated since version 4.9.1 |
|
1032 | + */ |
|
1033 | + protected function _register_config_routes() |
|
1034 | + { |
|
1035 | + $config_routes = array(); |
|
1036 | + foreach (self::versions_served() as $version => $hidden_endpoint) { |
|
1037 | + $config_routes[ self::ee_api_namespace . $version ] = $this->_get_config_route_data_for_version( |
|
1038 | + $version, |
|
1039 | + $hidden_endpoint |
|
1040 | + ); |
|
1041 | + } |
|
1042 | + return $config_routes; |
|
1043 | + } |
|
1044 | + |
|
1045 | + |
|
1046 | + /** |
|
1047 | + * Gets routes for the config for the specified version |
|
1048 | + * |
|
1049 | + * @param string $version |
|
1050 | + * @param boolean $hidden_endpoint |
|
1051 | + * @return array |
|
1052 | + */ |
|
1053 | + protected function _get_config_route_data_for_version($version, $hidden_endpoint) |
|
1054 | + { |
|
1055 | + return array( |
|
1056 | + 'config' => array( |
|
1057 | + array( |
|
1058 | + 'callback' => array( |
|
1059 | + 'EventEspresso\core\libraries\rest_api\controllers\config\Read', |
|
1060 | + 'handleRequest', |
|
1061 | + ), |
|
1062 | + 'methods' => WP_REST_Server::READABLE, |
|
1063 | + 'hidden_endpoint' => $hidden_endpoint, |
|
1064 | + 'callback_args' => array($version), |
|
1065 | + ), |
|
1066 | + ), |
|
1067 | + 'site_info' => array( |
|
1068 | + array( |
|
1069 | + 'callback' => array( |
|
1070 | + 'EventEspresso\core\libraries\rest_api\controllers\config\Read', |
|
1071 | + 'handleRequestSiteInfo', |
|
1072 | + ), |
|
1073 | + 'methods' => WP_REST_Server::READABLE, |
|
1074 | + 'hidden_endpoint' => $hidden_endpoint, |
|
1075 | + 'callback_args' => array($version), |
|
1076 | + ), |
|
1077 | + ), |
|
1078 | + ); |
|
1079 | + } |
|
1080 | + |
|
1081 | + |
|
1082 | + /** |
|
1083 | + * Gets the meta info routes |
|
1084 | + * |
|
1085 | + * @return array @see _register_model_routes |
|
1086 | + * @deprecated since version 4.9.1 |
|
1087 | + */ |
|
1088 | + protected function _register_meta_routes() |
|
1089 | + { |
|
1090 | + $meta_routes = array(); |
|
1091 | + foreach (self::versions_served() as $version => $hidden_endpoint) { |
|
1092 | + $meta_routes[ self::ee_api_namespace . $version ] = $this->_get_meta_route_data_for_version( |
|
1093 | + $version, |
|
1094 | + $hidden_endpoint |
|
1095 | + ); |
|
1096 | + } |
|
1097 | + return $meta_routes; |
|
1098 | + } |
|
1099 | + |
|
1100 | + |
|
1101 | + /** |
|
1102 | + * @param string $version |
|
1103 | + * @param boolean $hidden_endpoint |
|
1104 | + * @return array |
|
1105 | + */ |
|
1106 | + protected function _get_meta_route_data_for_version($version, $hidden_endpoint = false) |
|
1107 | + { |
|
1108 | + return array( |
|
1109 | + 'resources' => array( |
|
1110 | + array( |
|
1111 | + 'callback' => array( |
|
1112 | + 'EventEspresso\core\libraries\rest_api\controllers\model\Meta', |
|
1113 | + 'handleRequestModelsMeta', |
|
1114 | + ), |
|
1115 | + 'methods' => WP_REST_Server::READABLE, |
|
1116 | + 'hidden_endpoint' => $hidden_endpoint, |
|
1117 | + 'callback_args' => array($version), |
|
1118 | + ), |
|
1119 | + ), |
|
1120 | + ); |
|
1121 | + } |
|
1122 | + |
|
1123 | + |
|
1124 | + /** |
|
1125 | + * Tries to hide old 4.6 endpoints from the |
|
1126 | + * |
|
1127 | + * @param array $route_data |
|
1128 | + * @return array |
|
1129 | + * @throws \EE_Error |
|
1130 | + */ |
|
1131 | + public static function hide_old_endpoints($route_data) |
|
1132 | + { |
|
1133 | + // allow API clients to override which endpoints get hidden, in case |
|
1134 | + // they want to discover particular endpoints |
|
1135 | + // also, we don't have access to the request so we have to just grab it from the superglobal |
|
1136 | + $force_show_ee_namespace = ltrim( |
|
1137 | + EEH_Array::is_set($_REQUEST, 'force_show_ee_namespace', ''), |
|
1138 | + '/' |
|
1139 | + ); |
|
1140 | + foreach (EED_Core_Rest_Api::get_ee_route_data() as $namespace => $relative_urls) { |
|
1141 | + foreach ($relative_urls as $resource_name => $endpoints) { |
|
1142 | + foreach ($endpoints as $key => $endpoint) { |
|
1143 | + // skip schema and other route options |
|
1144 | + if (! is_numeric($key)) { |
|
1145 | + continue; |
|
1146 | + } |
|
1147 | + // by default, hide "hidden_endpoint"s, unless the request indicates |
|
1148 | + // to $force_show_ee_namespace, in which case only show that one |
|
1149 | + // namespace's endpoints (and hide all others) |
|
1150 | + if (($force_show_ee_namespace !== '' && $force_show_ee_namespace !== $namespace) |
|
1151 | + || ($endpoint['hidden_endpoint'] && $force_show_ee_namespace === '') |
|
1152 | + ) { |
|
1153 | + $full_route = '/' . ltrim($namespace, '/'); |
|
1154 | + $full_route .= '/' . ltrim($resource_name, '/'); |
|
1155 | + unset($route_data[ $full_route ]); |
|
1156 | + } |
|
1157 | + } |
|
1158 | + } |
|
1159 | + } |
|
1160 | + return $route_data; |
|
1161 | + } |
|
1162 | + |
|
1163 | + |
|
1164 | + /** |
|
1165 | + * Returns an array describing which versions of core support serving requests for. |
|
1166 | + * Keys are core versions' major and minor version, and values are the |
|
1167 | + * LOWEST requested version they can serve. Eg, 4.7 can serve requests for 4.6-like |
|
1168 | + * data by just removing a few models and fields from the responses. However, 4.15 might remove |
|
1169 | + * the answers table entirely, in which case it would be very difficult for |
|
1170 | + * it to serve 4.6-style responses. |
|
1171 | + * Versions of core that are missing from this array are unknowns. |
|
1172 | + * previous ver |
|
1173 | + * |
|
1174 | + * @return array |
|
1175 | + */ |
|
1176 | + public static function version_compatibilities() |
|
1177 | + { |
|
1178 | + return apply_filters( |
|
1179 | + 'FHEE__EED_Core_REST_API__version_compatibilities', |
|
1180 | + array( |
|
1181 | + '4.8.29' => '4.8.29', |
|
1182 | + '4.8.33' => '4.8.29', |
|
1183 | + '4.8.34' => '4.8.29', |
|
1184 | + '4.8.36' => '4.8.29', |
|
1185 | + ) |
|
1186 | + ); |
|
1187 | + } |
|
1188 | + |
|
1189 | + |
|
1190 | + /** |
|
1191 | + * Gets the latest API version served. Eg if there |
|
1192 | + * are two versions served of the API, 4.8.29 and 4.8.32, and |
|
1193 | + * we are on core version 4.8.34, it will return the string "4.8.32" |
|
1194 | + * |
|
1195 | + * @return string |
|
1196 | + */ |
|
1197 | + public static function latest_rest_api_version() |
|
1198 | + { |
|
1199 | + $versions_served = \EED_Core_Rest_Api::versions_served(); |
|
1200 | + $versions_served_keys = array_keys($versions_served); |
|
1201 | + return end($versions_served_keys); |
|
1202 | + } |
|
1203 | + |
|
1204 | + |
|
1205 | + /** |
|
1206 | + * Using EED_Core_Rest_Api::version_compatibilities(), determines what version of |
|
1207 | + * EE the API can serve requests for. Eg, if we are on 4.15 of core, and |
|
1208 | + * we can serve requests from 4.12 or later, this will return array( '4.12', '4.13', '4.14', '4.15' ). |
|
1209 | + * We also indicate whether or not this version should be put in the index or not |
|
1210 | + * |
|
1211 | + * @return array keys are API version numbers (just major and minor numbers), and values |
|
1212 | + * are whether or not they should be hidden |
|
1213 | + */ |
|
1214 | + public static function versions_served() |
|
1215 | + { |
|
1216 | + $versions_served = array(); |
|
1217 | + $possibly_served_versions = EED_Core_Rest_Api::version_compatibilities(); |
|
1218 | + $lowest_compatible_version = end($possibly_served_versions); |
|
1219 | + reset($possibly_served_versions); |
|
1220 | + $versions_served_historically = array_keys($possibly_served_versions); |
|
1221 | + $latest_version = end($versions_served_historically); |
|
1222 | + reset($versions_served_historically); |
|
1223 | + // for each version of core we have ever served: |
|
1224 | + foreach ($versions_served_historically as $key_versioned_endpoint) { |
|
1225 | + // if it's not above the current core version, and it's compatible with the current version of core |
|
1226 | + |
|
1227 | + if ($key_versioned_endpoint === $latest_version) { |
|
1228 | + // don't hide the latest version in the index |
|
1229 | + $versions_served[ $key_versioned_endpoint ] = false; |
|
1230 | + } elseif (version_compare($key_versioned_endpoint, $lowest_compatible_version, '>=') |
|
1231 | + && version_compare($key_versioned_endpoint, EED_Core_Rest_Api::core_version(), '<') |
|
1232 | + ) { |
|
1233 | + // include, but hide, previous versions which are still supported |
|
1234 | + $versions_served[ $key_versioned_endpoint ] = true; |
|
1235 | + } elseif (apply_filters( |
|
1236 | + 'FHEE__EED_Core_Rest_Api__versions_served__include_incompatible_versions', |
|
1237 | + false, |
|
1238 | + $possibly_served_versions |
|
1239 | + )) { |
|
1240 | + // if a version is no longer supported, don't include it in index or list of versions served |
|
1241 | + $versions_served[ $key_versioned_endpoint ] = true; |
|
1242 | + } |
|
1243 | + } |
|
1244 | + return $versions_served; |
|
1245 | + } |
|
1246 | + |
|
1247 | + |
|
1248 | + /** |
|
1249 | + * Gets the major and minor version of EE core's version string |
|
1250 | + * |
|
1251 | + * @return string |
|
1252 | + */ |
|
1253 | + public static function core_version() |
|
1254 | + { |
|
1255 | + return apply_filters( |
|
1256 | + 'FHEE__EED_Core_REST_API__core_version', |
|
1257 | + implode( |
|
1258 | + '.', |
|
1259 | + array_slice( |
|
1260 | + explode( |
|
1261 | + '.', |
|
1262 | + espresso_version() |
|
1263 | + ), |
|
1264 | + 0, |
|
1265 | + 3 |
|
1266 | + ) |
|
1267 | + ) |
|
1268 | + ); |
|
1269 | + } |
|
1270 | + |
|
1271 | + |
|
1272 | + /** |
|
1273 | + * Gets the default limit that should be used when querying for resources |
|
1274 | + * |
|
1275 | + * @return int |
|
1276 | + */ |
|
1277 | + public static function get_default_query_limit() |
|
1278 | + { |
|
1279 | + // we actually don't use a const because we want folks to always use |
|
1280 | + // this method, not the const directly |
|
1281 | + return apply_filters( |
|
1282 | + 'FHEE__EED_Core_Rest_Api__get_default_query_limit', |
|
1283 | + 50 |
|
1284 | + ); |
|
1285 | + } |
|
1286 | + |
|
1287 | + |
|
1288 | + /** |
|
1289 | + * |
|
1290 | + * @param string $version api version string (i.e. '4.8.36') |
|
1291 | + * @return array |
|
1292 | + */ |
|
1293 | + public static function getCollectionRoutesIndexedByModelName($version = '') |
|
1294 | + { |
|
1295 | + $version = empty($version) ? self::latest_rest_api_version() : $version; |
|
1296 | + $model_names = self::model_names_with_plural_routes($version); |
|
1297 | + $collection_routes = array(); |
|
1298 | + foreach ($model_names as $model_name => $model_class_name) { |
|
1299 | + $collection_routes[ strtolower($model_name) ] = '/' . self::ee_api_namespace . $version . '/' |
|
1300 | + . EEH_Inflector::pluralize_and_lower($model_name); |
|
1301 | + } |
|
1302 | + return $collection_routes; |
|
1303 | + } |
|
1304 | + |
|
1305 | + |
|
1306 | + /** |
|
1307 | + * Returns an array of primary key names indexed by model names. |
|
1308 | + * @param string $version |
|
1309 | + * @return array |
|
1310 | + */ |
|
1311 | + public static function getPrimaryKeyNamesIndexedByModelName($version = '') |
|
1312 | + { |
|
1313 | + $version = empty($version) ? self::latest_rest_api_version() : $version; |
|
1314 | + $model_names = self::model_names_with_plural_routes($version); |
|
1315 | + $primary_key_items = array(); |
|
1316 | + foreach ($model_names as $model_name => $model_class_name) { |
|
1317 | + $primary_keys = $model_class_name::instance()->get_combined_primary_key_fields(); |
|
1318 | + foreach ($primary_keys as $primary_key_name => $primary_key_field) { |
|
1319 | + if (count($primary_keys) > 1) { |
|
1320 | + $primary_key_items[ strtolower($model_name) ][] = $primary_key_name; |
|
1321 | + } else { |
|
1322 | + $primary_key_items[ strtolower($model_name) ] = $primary_key_name; |
|
1323 | + } |
|
1324 | + } |
|
1325 | + } |
|
1326 | + return $primary_key_items; |
|
1327 | + } |
|
1328 | + |
|
1329 | + /** |
|
1330 | + * Determines the EE REST API debug mode is activated, or not. |
|
1331 | + * @since 4.9.76.p |
|
1332 | + * @return bool |
|
1333 | + */ |
|
1334 | + public static function debugMode() |
|
1335 | + { |
|
1336 | + static $debug_mode = null; // could be class prop |
|
1337 | + if ($debug_mode === null) { |
|
1338 | + $debug_mode = defined('EE_REST_API_DEBUG_MODE') && EE_REST_API_DEBUG_MODE; |
|
1339 | + } |
|
1340 | + return $debug_mode; |
|
1341 | + } |
|
1342 | + |
|
1343 | + |
|
1344 | + |
|
1345 | + /** |
|
1346 | + * run - initial module setup |
|
1347 | + * |
|
1348 | + * @access public |
|
1349 | + * @param WP $WP |
|
1350 | + * @return void |
|
1351 | + */ |
|
1352 | + public function run($WP) |
|
1353 | + { |
|
1354 | + } |
|
1355 | 1355 | } |
@@ -9,266 +9,266 @@ |
||
9 | 9 | */ |
10 | 10 | class EE_HABTM_Relation extends EE_Model_Relation_Base |
11 | 11 | { |
12 | - /** |
|
13 | - * Model which defines the relation between two other models. Eg, the EE_Event_Question_Group model, |
|
14 | - * which joins EE_Event and EE_Question_Group |
|
15 | - * |
|
16 | - * @var EEM_Base |
|
17 | - */ |
|
18 | - protected $_joining_model_name; |
|
12 | + /** |
|
13 | + * Model which defines the relation between two other models. Eg, the EE_Event_Question_Group model, |
|
14 | + * which joins EE_Event and EE_Question_Group |
|
15 | + * |
|
16 | + * @var EEM_Base |
|
17 | + */ |
|
18 | + protected $_joining_model_name; |
|
19 | 19 | |
20 | - protected $_model_relation_chain_to_join_model; |
|
20 | + protected $_model_relation_chain_to_join_model; |
|
21 | 21 | |
22 | 22 | |
23 | - /** |
|
24 | - * Object representing the relationship between two models. HasAndBelongsToMany relations always use a join-table |
|
25 | - * (and an ee joining-model.) This knows how to join the models, |
|
26 | - * get related models across the relation, and add-and-remove the relationships. |
|
27 | - * |
|
28 | - * @param bool $joining_model_name |
|
29 | - * @param boolean $block_deletes for this type of relation, we block by default for now. if there |
|
30 | - * are related models across this relation, block (prevent and add an |
|
31 | - * error) the deletion of this model |
|
32 | - * @param string $blocking_delete_error_message a customized error message on blocking deletes instead of the |
|
33 | - * default |
|
34 | - */ |
|
35 | - public function __construct($joining_model_name, $block_deletes = true, $blocking_delete_error_message = '') |
|
36 | - { |
|
37 | - $this->_joining_model_name = $joining_model_name; |
|
38 | - parent::__construct($block_deletes, $blocking_delete_error_message); |
|
39 | - } |
|
23 | + /** |
|
24 | + * Object representing the relationship between two models. HasAndBelongsToMany relations always use a join-table |
|
25 | + * (and an ee joining-model.) This knows how to join the models, |
|
26 | + * get related models across the relation, and add-and-remove the relationships. |
|
27 | + * |
|
28 | + * @param bool $joining_model_name |
|
29 | + * @param boolean $block_deletes for this type of relation, we block by default for now. if there |
|
30 | + * are related models across this relation, block (prevent and add an |
|
31 | + * error) the deletion of this model |
|
32 | + * @param string $blocking_delete_error_message a customized error message on blocking deletes instead of the |
|
33 | + * default |
|
34 | + */ |
|
35 | + public function __construct($joining_model_name, $block_deletes = true, $blocking_delete_error_message = '') |
|
36 | + { |
|
37 | + $this->_joining_model_name = $joining_model_name; |
|
38 | + parent::__construct($block_deletes, $blocking_delete_error_message); |
|
39 | + } |
|
40 | 40 | |
41 | - /** |
|
42 | - * Gets the joining model's object |
|
43 | - * |
|
44 | - * @return EEM_Base |
|
45 | - */ |
|
46 | - public function get_join_model() |
|
47 | - { |
|
48 | - return $this->_get_model($this->_joining_model_name); |
|
49 | - } |
|
41 | + /** |
|
42 | + * Gets the joining model's object |
|
43 | + * |
|
44 | + * @return EEM_Base |
|
45 | + */ |
|
46 | + public function get_join_model() |
|
47 | + { |
|
48 | + return $this->_get_model($this->_joining_model_name); |
|
49 | + } |
|
50 | 50 | |
51 | 51 | |
52 | - /** |
|
53 | - * Gets the SQL string for joining the main model's table containing the pk to the join table. Eg "LEFT JOIN |
|
54 | - * real_join_table AS join_table_alias ON this_table_alias.pk = join_table_alias.fk_to_this_table" |
|
55 | - * |
|
56 | - * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
57 | - * @return string of SQL |
|
58 | - * @throws \EE_Error |
|
59 | - */ |
|
60 | - public function get_join_to_intermediate_model_statement($model_relation_chain) |
|
61 | - { |
|
62 | - // create sql like |
|
63 | - // LEFT JOIN join_table AS join_table_alias ON this_table_alias.this_table_pk = join_table_alias.join_table_fk_to_this |
|
64 | - // LEFT JOIN other_table AS other_table_alias ON join_table_alias.join_table_fk_to_other = other_table_alias.other_table_pk |
|
65 | - // remember the model relation chain to the JOIN model, because we'll |
|
66 | - // need it for get_join_statement() |
|
67 | - $this->_model_relation_chain_to_join_model = $model_relation_chain; |
|
68 | - $this_table_pk_field = $this->get_this_model()->get_primary_key_field();// get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
69 | - $join_table_fk_field_to_this_table = $this->get_join_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
70 | - $this_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
71 | - $model_relation_chain, |
|
72 | - $this->get_this_model()->get_this_model_name() |
|
73 | - ) . $this_table_pk_field->get_table_alias(); |
|
52 | + /** |
|
53 | + * Gets the SQL string for joining the main model's table containing the pk to the join table. Eg "LEFT JOIN |
|
54 | + * real_join_table AS join_table_alias ON this_table_alias.pk = join_table_alias.fk_to_this_table" |
|
55 | + * |
|
56 | + * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
57 | + * @return string of SQL |
|
58 | + * @throws \EE_Error |
|
59 | + */ |
|
60 | + public function get_join_to_intermediate_model_statement($model_relation_chain) |
|
61 | + { |
|
62 | + // create sql like |
|
63 | + // LEFT JOIN join_table AS join_table_alias ON this_table_alias.this_table_pk = join_table_alias.join_table_fk_to_this |
|
64 | + // LEFT JOIN other_table AS other_table_alias ON join_table_alias.join_table_fk_to_other = other_table_alias.other_table_pk |
|
65 | + // remember the model relation chain to the JOIN model, because we'll |
|
66 | + // need it for get_join_statement() |
|
67 | + $this->_model_relation_chain_to_join_model = $model_relation_chain; |
|
68 | + $this_table_pk_field = $this->get_this_model()->get_primary_key_field();// get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
69 | + $join_table_fk_field_to_this_table = $this->get_join_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
70 | + $this_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
71 | + $model_relation_chain, |
|
72 | + $this->get_this_model()->get_this_model_name() |
|
73 | + ) . $this_table_pk_field->get_table_alias(); |
|
74 | 74 | |
75 | - $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
76 | - $model_relation_chain, |
|
77 | - $this->get_join_model()->get_this_model_name() |
|
78 | - ) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
79 | - $join_table = $this->get_join_model()->get_table_for_alias($join_table_alias); |
|
80 | - // phew! ok, we have all the info we need, now we can create the SQL join string |
|
81 | - $SQL = $this->_left_join( |
|
82 | - $join_table, |
|
83 | - $join_table_alias, |
|
84 | - $join_table_fk_field_to_this_table->get_table_column(), |
|
85 | - $this_table_alias, |
|
86 | - $this_table_pk_field->get_table_column() |
|
87 | - ) . $this->get_join_model()->_construct_internal_join_to_table_with_alias($join_table_alias); |
|
75 | + $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
76 | + $model_relation_chain, |
|
77 | + $this->get_join_model()->get_this_model_name() |
|
78 | + ) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
79 | + $join_table = $this->get_join_model()->get_table_for_alias($join_table_alias); |
|
80 | + // phew! ok, we have all the info we need, now we can create the SQL join string |
|
81 | + $SQL = $this->_left_join( |
|
82 | + $join_table, |
|
83 | + $join_table_alias, |
|
84 | + $join_table_fk_field_to_this_table->get_table_column(), |
|
85 | + $this_table_alias, |
|
86 | + $this_table_pk_field->get_table_column() |
|
87 | + ) . $this->get_join_model()->_construct_internal_join_to_table_with_alias($join_table_alias); |
|
88 | 88 | |
89 | - return $SQL; |
|
90 | - } |
|
89 | + return $SQL; |
|
90 | + } |
|
91 | 91 | |
92 | 92 | |
93 | - /** |
|
94 | - * Gets the SQL string for joining the join table to the other model's pk's table. Eg "LEFT JOIN real_other_table |
|
95 | - * AS other_table_alias ON join_table_alias.fk_to_other_table = other_table_alias.pk" If you want to join between |
|
96 | - * modelA -> joinModelAB -> modelB (eg, Event -> Event_Question_Group -> Question_Group), you should prepend the |
|
97 | - * result of this function with results from get_join_to_intermediate_model_statement(), so that you join first to |
|
98 | - * the intermediate join table, and then to the other model's pk's table |
|
99 | - * |
|
100 | - * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
101 | - * @return string of SQL |
|
102 | - * @throws \EE_Error |
|
103 | - */ |
|
104 | - public function get_join_statement($model_relation_chain) |
|
105 | - { |
|
106 | - if ($this->_model_relation_chain_to_join_model === null) { |
|
107 | - throw new EE_Error(sprintf(__( |
|
108 | - 'When using EE_HABTM_Relation to create a join, you must call get_join_to_intermediate_model_statement BEFORE get_join_statement', |
|
109 | - 'event_espresso' |
|
110 | - ))); |
|
111 | - } |
|
112 | - $join_table_fk_field_to_this_table = $this->get_join_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
113 | - $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
114 | - $this->_model_relation_chain_to_join_model, |
|
115 | - $this->get_join_model()->get_this_model_name() |
|
116 | - ) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
117 | - $other_table_pk_field = $this->get_other_model()->get_primary_key_field(); |
|
118 | - $join_table_fk_field_to_other_table = $this->get_join_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
119 | - $other_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
120 | - $model_relation_chain, |
|
121 | - $this->get_other_model()->get_this_model_name() |
|
122 | - ) . $other_table_pk_field->get_table_alias(); |
|
123 | - $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); |
|
93 | + /** |
|
94 | + * Gets the SQL string for joining the join table to the other model's pk's table. Eg "LEFT JOIN real_other_table |
|
95 | + * AS other_table_alias ON join_table_alias.fk_to_other_table = other_table_alias.pk" If you want to join between |
|
96 | + * modelA -> joinModelAB -> modelB (eg, Event -> Event_Question_Group -> Question_Group), you should prepend the |
|
97 | + * result of this function with results from get_join_to_intermediate_model_statement(), so that you join first to |
|
98 | + * the intermediate join table, and then to the other model's pk's table |
|
99 | + * |
|
100 | + * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
101 | + * @return string of SQL |
|
102 | + * @throws \EE_Error |
|
103 | + */ |
|
104 | + public function get_join_statement($model_relation_chain) |
|
105 | + { |
|
106 | + if ($this->_model_relation_chain_to_join_model === null) { |
|
107 | + throw new EE_Error(sprintf(__( |
|
108 | + 'When using EE_HABTM_Relation to create a join, you must call get_join_to_intermediate_model_statement BEFORE get_join_statement', |
|
109 | + 'event_espresso' |
|
110 | + ))); |
|
111 | + } |
|
112 | + $join_table_fk_field_to_this_table = $this->get_join_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
113 | + $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
114 | + $this->_model_relation_chain_to_join_model, |
|
115 | + $this->get_join_model()->get_this_model_name() |
|
116 | + ) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
117 | + $other_table_pk_field = $this->get_other_model()->get_primary_key_field(); |
|
118 | + $join_table_fk_field_to_other_table = $this->get_join_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
119 | + $other_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
120 | + $model_relation_chain, |
|
121 | + $this->get_other_model()->get_this_model_name() |
|
122 | + ) . $other_table_pk_field->get_table_alias(); |
|
123 | + $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); |
|
124 | 124 | |
125 | - $SQL = $this->_left_join( |
|
126 | - $other_table, |
|
127 | - $other_table_alias, |
|
128 | - $other_table_pk_field->get_table_column(), |
|
129 | - $join_table_alias, |
|
130 | - $join_table_fk_field_to_other_table->get_table_column() |
|
131 | - ) . $this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
|
132 | - return $SQL; |
|
133 | - } |
|
125 | + $SQL = $this->_left_join( |
|
126 | + $other_table, |
|
127 | + $other_table_alias, |
|
128 | + $other_table_pk_field->get_table_column(), |
|
129 | + $join_table_alias, |
|
130 | + $join_table_fk_field_to_other_table->get_table_column() |
|
131 | + ) . $this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
|
132 | + return $SQL; |
|
133 | + } |
|
134 | 134 | |
135 | 135 | |
136 | - /** |
|
137 | - * Ensures there is an entry in the join table between these two models. Feel free to do this manually if you like. |
|
138 | - * If the join table has additional columns (eg, the Event_Question_Group table has a is_primary column), then |
|
139 | - * you'll want to directly use the EEM_Event_Question_Group model to add the entry to the table and set those extra |
|
140 | - * columns' values |
|
141 | - * |
|
142 | - * @param EE_Base_Class|int $this_obj_or_id |
|
143 | - * @param EE_Base_Class|int $other_obj_or_id |
|
144 | - * @param array $extra_join_model_fields_n_values col=>val pairs that are used as extra conditions for |
|
145 | - * checking existing values and for setting new rows if |
|
146 | - * no exact matches. |
|
147 | - * @return EE_Base_Class |
|
148 | - * @throws \EE_Error |
|
149 | - */ |
|
150 | - public function add_relation_to($this_obj_or_id, $other_obj_or_id, $extra_join_model_fields_n_values = array()) |
|
151 | - { |
|
152 | - $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
153 | - $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
154 | - // check if such a relationship already exists |
|
155 | - $join_model_fk_to_this_model = $this->get_join_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
156 | - $join_model_fk_to_other_model = $this->get_join_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
136 | + /** |
|
137 | + * Ensures there is an entry in the join table between these two models. Feel free to do this manually if you like. |
|
138 | + * If the join table has additional columns (eg, the Event_Question_Group table has a is_primary column), then |
|
139 | + * you'll want to directly use the EEM_Event_Question_Group model to add the entry to the table and set those extra |
|
140 | + * columns' values |
|
141 | + * |
|
142 | + * @param EE_Base_Class|int $this_obj_or_id |
|
143 | + * @param EE_Base_Class|int $other_obj_or_id |
|
144 | + * @param array $extra_join_model_fields_n_values col=>val pairs that are used as extra conditions for |
|
145 | + * checking existing values and for setting new rows if |
|
146 | + * no exact matches. |
|
147 | + * @return EE_Base_Class |
|
148 | + * @throws \EE_Error |
|
149 | + */ |
|
150 | + public function add_relation_to($this_obj_or_id, $other_obj_or_id, $extra_join_model_fields_n_values = array()) |
|
151 | + { |
|
152 | + $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
153 | + $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
154 | + // check if such a relationship already exists |
|
155 | + $join_model_fk_to_this_model = $this->get_join_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
156 | + $join_model_fk_to_other_model = $this->get_join_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
157 | 157 | |
158 | - $foreign_keys = $all_fields = array( |
|
159 | - $join_model_fk_to_this_model->get_name() => $this_model_obj->ID(), |
|
160 | - $join_model_fk_to_other_model->get_name() => $other_model_obj->ID(), |
|
161 | - ); |
|
158 | + $foreign_keys = $all_fields = array( |
|
159 | + $join_model_fk_to_this_model->get_name() => $this_model_obj->ID(), |
|
160 | + $join_model_fk_to_other_model->get_name() => $other_model_obj->ID(), |
|
161 | + ); |
|
162 | 162 | |
163 | - // if $where_query exists lets add them to the query_params. |
|
164 | - if (! empty($extra_join_model_fields_n_values)) { |
|
165 | - // make sure we strip any of the join model names from the $where_query cause we don't need that in here (why? because client code may have used the same conditionals for get_all_related which DOES need the join model name) |
|
166 | - // make sure we strip THIS models name from the query param |
|
167 | - $parsed_query = array(); |
|
168 | - foreach ($extra_join_model_fields_n_values as $query_param => $val) { |
|
169 | - $query_param = str_replace( |
|
170 | - $this->get_join_model()->get_this_model_name() . ".", |
|
171 | - "", |
|
172 | - $query_param |
|
173 | - ); |
|
174 | - $parsed_query[ $query_param ] = $val; |
|
175 | - } |
|
176 | - $all_fields = array_merge($foreign_keys, $parsed_query); |
|
177 | - } |
|
163 | + // if $where_query exists lets add them to the query_params. |
|
164 | + if (! empty($extra_join_model_fields_n_values)) { |
|
165 | + // make sure we strip any of the join model names from the $where_query cause we don't need that in here (why? because client code may have used the same conditionals for get_all_related which DOES need the join model name) |
|
166 | + // make sure we strip THIS models name from the query param |
|
167 | + $parsed_query = array(); |
|
168 | + foreach ($extra_join_model_fields_n_values as $query_param => $val) { |
|
169 | + $query_param = str_replace( |
|
170 | + $this->get_join_model()->get_this_model_name() . ".", |
|
171 | + "", |
|
172 | + $query_param |
|
173 | + ); |
|
174 | + $parsed_query[ $query_param ] = $val; |
|
175 | + } |
|
176 | + $all_fields = array_merge($foreign_keys, $parsed_query); |
|
177 | + } |
|
178 | 178 | |
179 | - $existing_entry_in_join_table = $this->get_join_model()->get_one(array($foreign_keys)); |
|
180 | - // If there is already an entry in the join table, indicating a relationship, update it instead of adding a |
|
181 | - // new row. |
|
182 | - // Again, if you want more sophisticated logic or insertions (handling more columns than just 2 foreign keys to |
|
183 | - // the other tables) use the joining model directly! |
|
184 | - if (! $existing_entry_in_join_table) { |
|
185 | - $this->get_join_model()->insert($all_fields); |
|
186 | - } else { |
|
187 | - $this->get_join_model()->update( |
|
188 | - $all_fields, |
|
189 | - [$foreign_keys] |
|
190 | - ); |
|
191 | - } |
|
192 | - return $other_model_obj; |
|
193 | - } |
|
179 | + $existing_entry_in_join_table = $this->get_join_model()->get_one(array($foreign_keys)); |
|
180 | + // If there is already an entry in the join table, indicating a relationship, update it instead of adding a |
|
181 | + // new row. |
|
182 | + // Again, if you want more sophisticated logic or insertions (handling more columns than just 2 foreign keys to |
|
183 | + // the other tables) use the joining model directly! |
|
184 | + if (! $existing_entry_in_join_table) { |
|
185 | + $this->get_join_model()->insert($all_fields); |
|
186 | + } else { |
|
187 | + $this->get_join_model()->update( |
|
188 | + $all_fields, |
|
189 | + [$foreign_keys] |
|
190 | + ); |
|
191 | + } |
|
192 | + return $other_model_obj; |
|
193 | + } |
|
194 | 194 | |
195 | 195 | |
196 | - /** |
|
197 | - * Deletes any rows in the join table that have foreign keys matching the other model objects specified |
|
198 | - * |
|
199 | - * @param EE_Base_Class|int $this_obj_or_id |
|
200 | - * @param EE_Base_Class|int $other_obj_or_id |
|
201 | - * @param array $where_query col=>val pairs that are used as extra conditions for checking existing |
|
202 | - * values and for removing existing rows if exact matches exist. |
|
203 | - * @return EE_Base_Class |
|
204 | - * @throws \EE_Error |
|
205 | - */ |
|
206 | - public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array()) |
|
207 | - { |
|
208 | - $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
209 | - $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
210 | - // check if such a relationship already exists |
|
211 | - $join_model_fk_to_this_model = $this->get_join_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
212 | - $join_model_fk_to_other_model = $this->get_join_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
196 | + /** |
|
197 | + * Deletes any rows in the join table that have foreign keys matching the other model objects specified |
|
198 | + * |
|
199 | + * @param EE_Base_Class|int $this_obj_or_id |
|
200 | + * @param EE_Base_Class|int $other_obj_or_id |
|
201 | + * @param array $where_query col=>val pairs that are used as extra conditions for checking existing |
|
202 | + * values and for removing existing rows if exact matches exist. |
|
203 | + * @return EE_Base_Class |
|
204 | + * @throws \EE_Error |
|
205 | + */ |
|
206 | + public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array()) |
|
207 | + { |
|
208 | + $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
209 | + $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
210 | + // check if such a relationship already exists |
|
211 | + $join_model_fk_to_this_model = $this->get_join_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
212 | + $join_model_fk_to_other_model = $this->get_join_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
213 | 213 | |
214 | - $cols_n_values = array( |
|
215 | - $join_model_fk_to_this_model->get_name() => $this_model_obj->ID(), |
|
216 | - $join_model_fk_to_other_model->get_name() => $other_model_obj->ID(), |
|
217 | - ); |
|
214 | + $cols_n_values = array( |
|
215 | + $join_model_fk_to_this_model->get_name() => $this_model_obj->ID(), |
|
216 | + $join_model_fk_to_other_model->get_name() => $other_model_obj->ID(), |
|
217 | + ); |
|
218 | 218 | |
219 | - // if $where_query exists lets add them to the query_params. |
|
220 | - if (! empty($where_query)) { |
|
221 | - // make sure we strip any of the join model names from the $where_query cause we don't need that in here (why? because client code may have used the same conditionals for get_all_related which DOES need the join model name) |
|
222 | - // make sure we strip THIS models name from the query param |
|
223 | - $parsed_query = array(); |
|
224 | - foreach ($where_query as $query_param => $val) { |
|
225 | - $query_param = str_replace( |
|
226 | - $this->get_join_model()->get_this_model_name() . ".", |
|
227 | - "", |
|
228 | - $query_param |
|
229 | - ); |
|
230 | - $parsed_query[ $query_param ] = $val; |
|
231 | - } |
|
232 | - $cols_n_values = array_merge($cols_n_values, $parsed_query); |
|
233 | - } |
|
219 | + // if $where_query exists lets add them to the query_params. |
|
220 | + if (! empty($where_query)) { |
|
221 | + // make sure we strip any of the join model names from the $where_query cause we don't need that in here (why? because client code may have used the same conditionals for get_all_related which DOES need the join model name) |
|
222 | + // make sure we strip THIS models name from the query param |
|
223 | + $parsed_query = array(); |
|
224 | + foreach ($where_query as $query_param => $val) { |
|
225 | + $query_param = str_replace( |
|
226 | + $this->get_join_model()->get_this_model_name() . ".", |
|
227 | + "", |
|
228 | + $query_param |
|
229 | + ); |
|
230 | + $parsed_query[ $query_param ] = $val; |
|
231 | + } |
|
232 | + $cols_n_values = array_merge($cols_n_values, $parsed_query); |
|
233 | + } |
|
234 | 234 | |
235 | - $this->get_join_model()->delete(array($cols_n_values)); |
|
236 | - return $other_model_obj; |
|
237 | - } |
|
235 | + $this->get_join_model()->delete(array($cols_n_values)); |
|
236 | + return $other_model_obj; |
|
237 | + } |
|
238 | 238 | |
239 | - /** |
|
240 | - * Gets all the non-key fields (ie, not the primary key and not foreign keys) on the join model. |
|
241 | - * @since 4.9.76.p |
|
242 | - * @return EE_Model_Field_Base[] |
|
243 | - * @throws EE_Error |
|
244 | - */ |
|
245 | - public function getNonKeyFields() |
|
246 | - { |
|
247 | - // all fields besides the primary key and two foreign keys should be parameters |
|
248 | - $join_model = $this->get_join_model(); |
|
249 | - $standard_fields = array(); |
|
250 | - if ($join_model->has_primary_key_field()) { |
|
251 | - $standard_fields[] = $join_model->primary_key_name(); |
|
252 | - } |
|
253 | - if ($this->get_this_model()->has_primary_key_field()) { |
|
254 | - $standard_fields[] = $this->get_this_model()->primary_key_name(); |
|
255 | - } |
|
256 | - if ($this->get_other_model()->has_primary_key_field()) { |
|
257 | - $standard_fields[] = $this->get_other_model()->primary_key_name(); |
|
258 | - } |
|
259 | - return array_diff_key( |
|
260 | - $join_model->field_settings(), |
|
261 | - array_flip($standard_fields) |
|
262 | - ); |
|
263 | - } |
|
239 | + /** |
|
240 | + * Gets all the non-key fields (ie, not the primary key and not foreign keys) on the join model. |
|
241 | + * @since 4.9.76.p |
|
242 | + * @return EE_Model_Field_Base[] |
|
243 | + * @throws EE_Error |
|
244 | + */ |
|
245 | + public function getNonKeyFields() |
|
246 | + { |
|
247 | + // all fields besides the primary key and two foreign keys should be parameters |
|
248 | + $join_model = $this->get_join_model(); |
|
249 | + $standard_fields = array(); |
|
250 | + if ($join_model->has_primary_key_field()) { |
|
251 | + $standard_fields[] = $join_model->primary_key_name(); |
|
252 | + } |
|
253 | + if ($this->get_this_model()->has_primary_key_field()) { |
|
254 | + $standard_fields[] = $this->get_this_model()->primary_key_name(); |
|
255 | + } |
|
256 | + if ($this->get_other_model()->has_primary_key_field()) { |
|
257 | + $standard_fields[] = $this->get_other_model()->primary_key_name(); |
|
258 | + } |
|
259 | + return array_diff_key( |
|
260 | + $join_model->field_settings(), |
|
261 | + array_flip($standard_fields) |
|
262 | + ); |
|
263 | + } |
|
264 | 264 | |
265 | - /** |
|
266 | - * Returns true if the join model has non-key fields (ie, fields that aren't the primary key or foreign keys.) |
|
267 | - * @since 4.9.76.p |
|
268 | - * @return boolean |
|
269 | - */ |
|
270 | - public function hasNonKeyFields() |
|
271 | - { |
|
272 | - return count($this->get_join_model()->field_settings()) > 3; |
|
273 | - } |
|
265 | + /** |
|
266 | + * Returns true if the join model has non-key fields (ie, fields that aren't the primary key or foreign keys.) |
|
267 | + * @since 4.9.76.p |
|
268 | + * @return boolean |
|
269 | + */ |
|
270 | + public function hasNonKeyFields() |
|
271 | + { |
|
272 | + return count($this->get_join_model()->field_settings()) > 3; |
|
273 | + } |
|
274 | 274 | } |
@@ -15,913 +15,913 @@ |
||
15 | 15 | class EEM_Event extends EEM_CPT_Base |
16 | 16 | { |
17 | 17 | |
18 | - /** |
|
19 | - * constant used by status(), indicating that no more tickets can be purchased for any of the datetimes for the |
|
20 | - * event |
|
21 | - */ |
|
22 | - const sold_out = 'sold_out'; |
|
23 | - |
|
24 | - /** |
|
25 | - * constant used by status(), indicating that upcoming event dates have been postponed (may be pushed to a later |
|
26 | - * date) |
|
27 | - */ |
|
28 | - const postponed = 'postponed'; |
|
29 | - |
|
30 | - /** |
|
31 | - * constant used by status(), indicating that the event will no longer occur |
|
32 | - */ |
|
33 | - const cancelled = 'cancelled'; |
|
34 | - |
|
35 | - |
|
36 | - /** |
|
37 | - * @var string |
|
38 | - */ |
|
39 | - protected static $_default_reg_status; |
|
40 | - |
|
41 | - |
|
42 | - /** |
|
43 | - * This is the default for the additional limit field. |
|
44 | - * @var int |
|
45 | - */ |
|
46 | - protected static $_default_additional_limit = 10; |
|
47 | - |
|
48 | - |
|
49 | - /** |
|
50 | - * private instance of the Event object |
|
51 | - * |
|
52 | - * @var EEM_Event |
|
53 | - */ |
|
54 | - protected static $_instance; |
|
55 | - |
|
56 | - |
|
57 | - |
|
58 | - |
|
59 | - /** |
|
60 | - * Adds a relationship to Term_Taxonomy for each CPT_Base |
|
61 | - * |
|
62 | - * @param string $timezone |
|
63 | - * @throws \EE_Error |
|
64 | - */ |
|
65 | - protected function __construct($timezone = null) |
|
66 | - { |
|
67 | - EE_Registry::instance()->load_model('Registration'); |
|
68 | - $this->singular_item = esc_html__('Event', 'event_espresso'); |
|
69 | - $this->plural_item = esc_html__('Events', 'event_espresso'); |
|
70 | - // to remove Cancelled events from the frontend, copy the following filter to your functions.php file |
|
71 | - // add_filter( 'AFEE__EEM_Event__construct___custom_stati__cancelled__Public', '__return_false' ); |
|
72 | - // to remove Postponed events from the frontend, copy the following filter to your functions.php file |
|
73 | - // add_filter( 'AFEE__EEM_Event__construct___custom_stati__postponed__Public', '__return_false' ); |
|
74 | - // to remove Sold Out events from the frontend, copy the following filter to your functions.php file |
|
75 | - // add_filter( 'AFEE__EEM_Event__construct___custom_stati__sold_out__Public', '__return_false' ); |
|
76 | - $this->_custom_stati = apply_filters( |
|
77 | - 'AFEE__EEM_Event__construct___custom_stati', |
|
78 | - array( |
|
79 | - EEM_Event::cancelled => array( |
|
80 | - 'label' => esc_html__('Cancelled', 'event_espresso'), |
|
81 | - 'public' => apply_filters('AFEE__EEM_Event__construct___custom_stati__cancelled__Public', true), |
|
82 | - ), |
|
83 | - EEM_Event::postponed => array( |
|
84 | - 'label' => esc_html__('Postponed', 'event_espresso'), |
|
85 | - 'public' => apply_filters('AFEE__EEM_Event__construct___custom_stati__postponed__Public', true), |
|
86 | - ), |
|
87 | - EEM_Event::sold_out => array( |
|
88 | - 'label' => esc_html__('Sold Out', 'event_espresso'), |
|
89 | - 'public' => apply_filters('AFEE__EEM_Event__construct___custom_stati__sold_out__Public', true), |
|
90 | - ), |
|
91 | - ) |
|
92 | - ); |
|
93 | - self::$_default_reg_status = empty(self::$_default_reg_status) ? EEM_Registration::status_id_pending_payment |
|
94 | - : self::$_default_reg_status; |
|
95 | - $this->_tables = array( |
|
96 | - 'Event_CPT' => new EE_Primary_Table('posts', 'ID'), |
|
97 | - 'Event_Meta' => new EE_Secondary_Table('esp_event_meta', 'EVTM_ID', 'EVT_ID'), |
|
98 | - ); |
|
99 | - $this->_fields = array( |
|
100 | - 'Event_CPT' => array( |
|
101 | - 'EVT_ID' => new EE_Primary_Key_Int_Field( |
|
102 | - 'ID', |
|
103 | - esc_html__('Post ID for Event', 'event_espresso') |
|
104 | - ), |
|
105 | - 'EVT_name' => new EE_Plain_Text_Field( |
|
106 | - 'post_title', |
|
107 | - esc_html__('Event Name', 'event_espresso'), |
|
108 | - false, |
|
109 | - '' |
|
110 | - ), |
|
111 | - 'EVT_desc' => new EE_Post_Content_Field( |
|
112 | - 'post_content', |
|
113 | - esc_html__('Event Description', 'event_espresso'), |
|
114 | - false, |
|
115 | - '' |
|
116 | - ), |
|
117 | - 'EVT_slug' => new EE_Slug_Field( |
|
118 | - 'post_name', |
|
119 | - esc_html__('Event Slug', 'event_espresso'), |
|
120 | - false, |
|
121 | - '' |
|
122 | - ), |
|
123 | - 'EVT_created' => new EE_Datetime_Field( |
|
124 | - 'post_date', |
|
125 | - esc_html__('Date/Time Event Created', 'event_espresso'), |
|
126 | - false, |
|
127 | - EE_Datetime_Field::now |
|
128 | - ), |
|
129 | - 'EVT_short_desc' => new EE_Simple_HTML_Field( |
|
130 | - 'post_excerpt', |
|
131 | - esc_html__('Event Short Description', 'event_espresso'), |
|
132 | - false, |
|
133 | - '' |
|
134 | - ), |
|
135 | - 'EVT_modified' => new EE_Datetime_Field( |
|
136 | - 'post_modified', |
|
137 | - esc_html__('Date/Time Event Modified', 'event_espresso'), |
|
138 | - false, |
|
139 | - EE_Datetime_Field::now |
|
140 | - ), |
|
141 | - 'EVT_wp_user' => new EE_WP_User_Field( |
|
142 | - 'post_author', |
|
143 | - esc_html__('Event Creator ID', 'event_espresso'), |
|
144 | - false |
|
145 | - ), |
|
146 | - 'parent' => new EE_Integer_Field( |
|
147 | - 'post_parent', |
|
148 | - esc_html__('Event Parent ID', 'event_espresso'), |
|
149 | - false, |
|
150 | - 0 |
|
151 | - ), |
|
152 | - 'EVT_order' => new EE_Integer_Field( |
|
153 | - 'menu_order', |
|
154 | - esc_html__('Event Menu Order', 'event_espresso'), |
|
155 | - false, |
|
156 | - 1 |
|
157 | - ), |
|
158 | - 'post_type' => new EE_WP_Post_Type_Field('espresso_events'), |
|
159 | - // EE_Plain_Text_Field( 'post_type', esc_html__( 'Event Post Type', 'event_espresso' ), FALSE, 'espresso_events' ), |
|
160 | - 'status' => new EE_WP_Post_Status_Field( |
|
161 | - 'post_status', |
|
162 | - esc_html__('Event Status', 'event_espresso'), |
|
163 | - false, |
|
164 | - 'draft', |
|
165 | - $this->_custom_stati |
|
166 | - ), |
|
167 | - 'password' => new EE_Password_Field( |
|
168 | - 'post_password', |
|
169 | - __('Password', 'event_espresso'), |
|
170 | - false, |
|
171 | - '', |
|
172 | - array( |
|
173 | - 'EVT_desc', |
|
174 | - 'EVT_short_desc', |
|
175 | - 'EVT_display_desc', |
|
176 | - 'EVT_display_ticket_selector', |
|
177 | - 'EVT_visible_on', |
|
178 | - 'EVT_additional_limit', |
|
179 | - 'EVT_default_registration_status', |
|
180 | - 'EVT_member_only', |
|
181 | - 'EVT_phone', |
|
182 | - 'EVT_allow_overflow', |
|
183 | - 'EVT_timezone_string', |
|
184 | - 'EVT_external_URL', |
|
185 | - 'EVT_donations' |
|
186 | - ) |
|
187 | - ) |
|
188 | - ), |
|
189 | - 'Event_Meta' => array( |
|
190 | - 'EVTM_ID' => new EE_DB_Only_Float_Field( |
|
191 | - 'EVTM_ID', |
|
192 | - esc_html__('Event Meta Row ID', 'event_espresso'), |
|
193 | - false |
|
194 | - ), |
|
195 | - 'EVT_ID_fk' => new EE_DB_Only_Int_Field( |
|
196 | - 'EVT_ID', |
|
197 | - esc_html__('Foreign key to Event ID from Event Meta table', 'event_espresso'), |
|
198 | - false |
|
199 | - ), |
|
200 | - 'EVT_display_desc' => new EE_Boolean_Field( |
|
201 | - 'EVT_display_desc', |
|
202 | - esc_html__('Display Description Flag', 'event_espresso'), |
|
203 | - false, |
|
204 | - true |
|
205 | - ), |
|
206 | - 'EVT_display_ticket_selector' => new EE_Boolean_Field( |
|
207 | - 'EVT_display_ticket_selector', |
|
208 | - esc_html__('Display Ticket Selector Flag', 'event_espresso'), |
|
209 | - false, |
|
210 | - true |
|
211 | - ), |
|
212 | - 'EVT_visible_on' => new EE_Datetime_Field( |
|
213 | - 'EVT_visible_on', |
|
214 | - esc_html__('Event Visible Date', 'event_espresso'), |
|
215 | - true, |
|
216 | - EE_Datetime_Field::now |
|
217 | - ), |
|
218 | - 'EVT_additional_limit' => new EE_Integer_Field( |
|
219 | - 'EVT_additional_limit', |
|
220 | - esc_html__('Limit of Additional Registrations on Same Transaction', 'event_espresso'), |
|
221 | - true, |
|
222 | - self::$_default_additional_limit |
|
223 | - ), |
|
224 | - 'EVT_default_registration_status' => new EE_Enum_Text_Field( |
|
225 | - 'EVT_default_registration_status', |
|
226 | - esc_html__('Default Registration Status on this Event', 'event_espresso'), |
|
227 | - false, |
|
228 | - EEM_Event::$_default_reg_status, |
|
229 | - EEM_Registration::reg_status_array() |
|
230 | - ), |
|
231 | - 'EVT_member_only' => new EE_Boolean_Field( |
|
232 | - 'EVT_member_only', |
|
233 | - esc_html__('Member-Only Event Flag', 'event_espresso'), |
|
234 | - false, |
|
235 | - false |
|
236 | - ), |
|
237 | - 'EVT_phone' => new EE_Plain_Text_Field( |
|
238 | - 'EVT_phone', |
|
239 | - esc_html__('Event Phone Number', 'event_espresso'), |
|
240 | - false, |
|
241 | - '' |
|
242 | - ), |
|
243 | - 'EVT_allow_overflow' => new EE_Boolean_Field( |
|
244 | - 'EVT_allow_overflow', |
|
245 | - esc_html__('Allow Overflow on Event', 'event_espresso'), |
|
246 | - false, |
|
247 | - false |
|
248 | - ), |
|
249 | - 'EVT_timezone_string' => new EE_Plain_Text_Field( |
|
250 | - 'EVT_timezone_string', |
|
251 | - esc_html__('Timezone (name) for Event times', 'event_espresso'), |
|
252 | - false, |
|
253 | - '' |
|
254 | - ), |
|
255 | - 'EVT_external_URL' => new EE_Plain_Text_Field( |
|
256 | - 'EVT_external_URL', |
|
257 | - esc_html__('URL of Event Page if hosted elsewhere', 'event_espresso'), |
|
258 | - true |
|
259 | - ), |
|
260 | - 'EVT_donations' => new EE_Boolean_Field( |
|
261 | - 'EVT_donations', |
|
262 | - esc_html__('Accept Donations?', 'event_espresso'), |
|
263 | - false, |
|
264 | - false |
|
265 | - ), |
|
266 | - ), |
|
267 | - ); |
|
268 | - $this->_model_relations = array( |
|
269 | - 'Registration' => new EE_Has_Many_Relation(), |
|
270 | - 'Datetime' => new EE_Has_Many_Relation(), |
|
271 | - 'Question_Group' => new EE_HABTM_Relation('Event_Question_Group'), |
|
272 | - 'Event_Question_Group' => new EE_Has_Many_Relation(), |
|
273 | - 'Venue' => new EE_HABTM_Relation('Event_Venue'), |
|
274 | - 'Term_Relationship' => new EE_Has_Many_Relation(), |
|
275 | - 'Term_Taxonomy' => new EE_HABTM_Relation('Term_Relationship'), |
|
276 | - 'Message_Template_Group' => new EE_HABTM_Relation('Event_Message_Template'), |
|
277 | - 'Attendee' => new EE_HABTM_Relation('Registration'), |
|
278 | - 'WP_User' => new EE_Belongs_To_Relation(), |
|
279 | - ); |
|
280 | - // this model is generally available for reading |
|
281 | - $this->_cap_restriction_generators[ EEM_Base::caps_read ] = new EE_Restriction_Generator_Public(); |
|
282 | - $this->model_chain_to_password = ''; |
|
283 | - parent::__construct($timezone); |
|
284 | - } |
|
285 | - |
|
286 | - |
|
287 | - |
|
288 | - /** |
|
289 | - * @param string $default_reg_status |
|
290 | - */ |
|
291 | - public static function set_default_reg_status($default_reg_status) |
|
292 | - { |
|
293 | - self::$_default_reg_status = $default_reg_status; |
|
294 | - // if EEM_Event has already been instantiated, |
|
295 | - // then we need to reset the `EVT_default_reg_status` field to use the new default. |
|
296 | - if (self::$_instance instanceof EEM_Event) { |
|
297 | - $default_reg_status = new EE_Enum_Text_Field( |
|
298 | - 'EVT_default_registration_status', |
|
299 | - esc_html__('Default Registration Status on this Event', 'event_espresso'), |
|
300 | - false, |
|
301 | - $default_reg_status, |
|
302 | - EEM_Registration::reg_status_array() |
|
303 | - ); |
|
304 | - $default_reg_status->_construct_finalize( |
|
305 | - 'Event_Meta', |
|
306 | - 'EVT_default_registration_status', |
|
307 | - 'EEM_Event' |
|
308 | - ); |
|
309 | - self::$_instance->_fields['Event_Meta']['EVT_default_registration_status'] = $default_reg_status; |
|
310 | - } |
|
311 | - } |
|
312 | - |
|
313 | - |
|
314 | - /** |
|
315 | - * Used to override the default for the additional limit field. |
|
316 | - * @param $additional_limit |
|
317 | - */ |
|
318 | - public static function set_default_additional_limit($additional_limit) |
|
319 | - { |
|
320 | - self::$_default_additional_limit = (int) $additional_limit; |
|
321 | - if (self::$_instance instanceof EEM_Event) { |
|
322 | - self::$_instance->_fields['Event_Meta']['EVT_additional_limit'] = new EE_Integer_Field( |
|
323 | - 'EVT_additional_limit', |
|
324 | - __('Limit of Additional Registrations on Same Transaction', 'event_espresso'), |
|
325 | - true, |
|
326 | - self::$_default_additional_limit |
|
327 | - ); |
|
328 | - self::$_instance->_fields['Event_Meta']['EVT_additional_limit']->_construct_finalize( |
|
329 | - 'Event_Meta', |
|
330 | - 'EVT_additional_limit', |
|
331 | - 'EEM_Event' |
|
332 | - ); |
|
333 | - } |
|
334 | - } |
|
335 | - |
|
336 | - |
|
337 | - /** |
|
338 | - * Return what is currently set as the default additional limit for the event. |
|
339 | - * @return int |
|
340 | - */ |
|
341 | - public static function get_default_additional_limit() |
|
342 | - { |
|
343 | - return apply_filters('FHEE__EEM_Event__get_default_additional_limit', self::$_default_additional_limit); |
|
344 | - } |
|
345 | - |
|
346 | - |
|
347 | - /** |
|
348 | - * get_question_groups |
|
349 | - * |
|
350 | - * @return array |
|
351 | - * @throws \EE_Error |
|
352 | - */ |
|
353 | - public function get_all_question_groups() |
|
354 | - { |
|
355 | - return EE_Registry::instance()->load_model('Question_Group')->get_all( |
|
356 | - array( |
|
357 | - array('QSG_deleted' => false), |
|
358 | - 'order_by' => array('QSG_order' => 'ASC'), |
|
359 | - ) |
|
360 | - ); |
|
361 | - } |
|
362 | - |
|
363 | - |
|
364 | - |
|
365 | - /** |
|
366 | - * get_question_groups |
|
367 | - * |
|
368 | - * @param int $EVT_ID |
|
369 | - * @return array|bool |
|
370 | - * @throws \EE_Error |
|
371 | - */ |
|
372 | - public function get_all_event_question_groups($EVT_ID = 0) |
|
373 | - { |
|
374 | - if (! isset($EVT_ID) || ! absint($EVT_ID)) { |
|
375 | - EE_Error::add_error( |
|
376 | - esc_html__( |
|
377 | - 'An error occurred. No Event Question Groups could be retrieved because an Event ID was not received.', |
|
378 | - 'event_espresso' |
|
379 | - ), |
|
380 | - __FILE__, |
|
381 | - __FUNCTION__, |
|
382 | - __LINE__ |
|
383 | - ); |
|
384 | - return false; |
|
385 | - } |
|
386 | - return EE_Registry::instance()->load_model('Event_Question_Group')->get_all( |
|
387 | - array( |
|
388 | - array('EVT_ID' => $EVT_ID), |
|
389 | - ) |
|
390 | - ); |
|
391 | - } |
|
392 | - |
|
393 | - |
|
394 | - /** |
|
395 | - * get_question_groups |
|
396 | - * |
|
397 | - * @param int $EVT_ID |
|
398 | - * @param boolean $for_primary_attendee |
|
399 | - * @return array|bool |
|
400 | - * @throws EE_Error |
|
401 | - * @throws InvalidArgumentException |
|
402 | - * @throws ReflectionException |
|
403 | - * @throws InvalidDataTypeException |
|
404 | - * @throws InvalidInterfaceException |
|
405 | - */ |
|
406 | - public function get_event_question_groups($EVT_ID = 0, $for_primary_attendee = true) |
|
407 | - { |
|
408 | - if (! isset($EVT_ID) || ! absint($EVT_ID)) { |
|
409 | - EE_Error::add_error( |
|
410 | - esc_html__( |
|
411 | - // @codingStandardsIgnoreStart |
|
412 | - 'An error occurred. No Event Question Groups could be retrieved because an Event ID was not received.', |
|
413 | - // @codingStandardsIgnoreEnd |
|
414 | - 'event_espresso' |
|
415 | - ), |
|
416 | - __FILE__, |
|
417 | - __FUNCTION__, |
|
418 | - __LINE__ |
|
419 | - ); |
|
420 | - return false; |
|
421 | - } |
|
422 | - $query_params = [ |
|
423 | - [ |
|
424 | - 'EVT_ID' => $EVT_ID, |
|
425 | - EEM_Event_Question_Group::instance()->fieldNameForContext($for_primary_attendee) => true |
|
426 | - ] |
|
427 | - ]; |
|
428 | - if ($for_primary_attendee) { |
|
429 | - $query_params[0]['EQG_primary'] = true; |
|
430 | - } else { |
|
431 | - $query_params[0]['EQG_additional'] = true; |
|
432 | - } |
|
433 | - return EE_Registry::instance()->load_model('Event_Question_Group')->get_all($query_params); |
|
434 | - } |
|
435 | - |
|
436 | - |
|
437 | - /** |
|
438 | - * get_question_groups |
|
439 | - * |
|
440 | - * @param int $EVT_ID |
|
441 | - * @param EE_Registration $registration |
|
442 | - * @return array|bool |
|
443 | - * @throws EE_Error |
|
444 | - * @throws InvalidArgumentException |
|
445 | - * @throws InvalidDataTypeException |
|
446 | - * @throws InvalidInterfaceException |
|
447 | - * @throws ReflectionException |
|
448 | - */ |
|
449 | - public function get_question_groups_for_event($EVT_ID = 0, EE_Registration $registration) |
|
450 | - { |
|
451 | - if (! isset($EVT_ID) || ! absint($EVT_ID)) { |
|
452 | - EE_Error::add_error( |
|
453 | - esc_html__( |
|
454 | - 'An error occurred. No Question Groups could be retrieved because an Event ID was not received.', |
|
455 | - 'event_espresso' |
|
456 | - ), |
|
457 | - __FILE__, |
|
458 | - __FUNCTION__, |
|
459 | - __LINE__ |
|
460 | - ); |
|
461 | - return false; |
|
462 | - } |
|
463 | - return EE_Registry::instance()->load_model('Question_Group')->get_all( |
|
464 | - [ |
|
465 | - [ |
|
466 | - 'Event_Question_Group.EVT_ID' => $EVT_ID, |
|
467 | - 'Event_Question_Group.' |
|
468 | - . EEM_Event_Question_Group::instance()->fieldNameForContext( |
|
469 | - $registration->is_primary_registrant() |
|
470 | - ) => true |
|
471 | - ], |
|
472 | - 'order_by' => ['QSG_order' => 'ASC'], |
|
473 | - ] |
|
474 | - ); |
|
475 | - } |
|
476 | - |
|
477 | - |
|
478 | - |
|
479 | - /** |
|
480 | - * get_question_target_db_column |
|
481 | - * |
|
482 | - * @param string $QSG_IDs csv list of $QSG IDs |
|
483 | - * @return array|bool |
|
484 | - * @throws \EE_Error |
|
485 | - */ |
|
486 | - public function get_questions_in_groups($QSG_IDs = '') |
|
487 | - { |
|
488 | - if (empty($QSG_IDs)) { |
|
489 | - EE_Error::add_error( |
|
490 | - esc_html__('An error occurred. No Question Group IDs were received.', 'event_espresso'), |
|
491 | - __FILE__, |
|
492 | - __FUNCTION__, |
|
493 | - __LINE__ |
|
494 | - ); |
|
495 | - return false; |
|
496 | - } |
|
497 | - return EE_Registry::instance()->load_model('Question')->get_all( |
|
498 | - array( |
|
499 | - array( |
|
500 | - 'Question_Group.QSG_ID' => array('IN', $QSG_IDs), |
|
501 | - 'QST_deleted' => false, |
|
502 | - 'QST_admin_only' => is_admin(), |
|
503 | - ), |
|
504 | - 'order_by' => 'QST_order', |
|
505 | - ) |
|
506 | - ); |
|
507 | - } |
|
508 | - |
|
509 | - |
|
510 | - |
|
511 | - /** |
|
512 | - * get_options_for_question |
|
513 | - * |
|
514 | - * @param string $QST_IDs csv list of $QST IDs |
|
515 | - * @return array|bool |
|
516 | - * @throws \EE_Error |
|
517 | - */ |
|
518 | - public function get_options_for_question($QST_IDs) |
|
519 | - { |
|
520 | - if (empty($QST_IDs)) { |
|
521 | - EE_Error::add_error( |
|
522 | - esc_html__('An error occurred. No Question IDs were received.', 'event_espresso'), |
|
523 | - __FILE__, |
|
524 | - __FUNCTION__, |
|
525 | - __LINE__ |
|
526 | - ); |
|
527 | - return false; |
|
528 | - } |
|
529 | - return EE_Registry::instance()->load_model('Question_Option')->get_all( |
|
530 | - array( |
|
531 | - array( |
|
532 | - 'Question.QST_ID' => array('IN', $QST_IDs), |
|
533 | - 'QSO_deleted' => false, |
|
534 | - ), |
|
535 | - 'order_by' => 'QSO_ID', |
|
536 | - ) |
|
537 | - ); |
|
538 | - } |
|
539 | - |
|
540 | - |
|
541 | - |
|
542 | - |
|
543 | - |
|
544 | - |
|
545 | - |
|
546 | - /** |
|
547 | - * Gets all events that are published |
|
548 | - * and have event start time earlier than now and an event end time later than now |
|
549 | - * |
|
550 | - * @param array $query_params An array of query params to further filter on |
|
551 | - * (note that status and DTT_EVT_start and DTT_EVT_end will be overridden) |
|
552 | - * @param bool $count whether to return the count or not (default FALSE) |
|
553 | - * @return EE_Event[]|int |
|
554 | - * @throws \EE_Error |
|
555 | - */ |
|
556 | - public function get_active_events($query_params, $count = false) |
|
557 | - { |
|
558 | - if (array_key_exists(0, $query_params)) { |
|
559 | - $where_params = $query_params[0]; |
|
560 | - unset($query_params[0]); |
|
561 | - } else { |
|
562 | - $where_params = array(); |
|
563 | - } |
|
564 | - // if we have count make sure we don't include group by |
|
565 | - if ($count && isset($query_params['group_by'])) { |
|
566 | - unset($query_params['group_by']); |
|
567 | - } |
|
568 | - // let's add specific query_params for active_events |
|
569 | - // keep in mind this will override any sent status in the query AND any date queries. |
|
570 | - $where_params['status'] = array('IN', array('publish', EEM_Event::sold_out)); |
|
571 | - // if already have where params for DTT_EVT_start or DTT_EVT_end then append these conditions |
|
572 | - if (isset($where_params['Datetime.DTT_EVT_start'])) { |
|
573 | - $where_params['Datetime.DTT_EVT_start******'] = array( |
|
574 | - '<', |
|
575 | - EEM_Datetime::instance()->current_time_for_query('DTT_EVT_start'), |
|
576 | - ); |
|
577 | - } else { |
|
578 | - $where_params['Datetime.DTT_EVT_start'] = array( |
|
579 | - '<', |
|
580 | - EEM_Datetime::instance()->current_time_for_query('DTT_EVT_start'), |
|
581 | - ); |
|
582 | - } |
|
583 | - if (isset($where_params['Datetime.DTT_EVT_end'])) { |
|
584 | - $where_params['Datetime.DTT_EVT_end*****'] = array( |
|
585 | - '>', |
|
586 | - EEM_Datetime::instance()->current_time_for_query('DTT_EVT_end'), |
|
587 | - ); |
|
588 | - } else { |
|
589 | - $where_params['Datetime.DTT_EVT_end'] = array( |
|
590 | - '>', |
|
591 | - EEM_Datetime::instance()->current_time_for_query('DTT_EVT_end'), |
|
592 | - ); |
|
593 | - } |
|
594 | - $query_params[0] = $where_params; |
|
595 | - // don't use $query_params with count() |
|
596 | - // because we don't want to include additional query clauses like "GROUP BY" |
|
597 | - return $count |
|
598 | - ? $this->count(array($where_params), 'EVT_ID', true) |
|
599 | - : $this->get_all($query_params); |
|
600 | - } |
|
601 | - |
|
602 | - |
|
603 | - |
|
604 | - /** |
|
605 | - * get all events that are published and have an event start time later than now |
|
606 | - * |
|
607 | - * @param array $query_params An array of query params to further filter on |
|
608 | - * (Note that status and DTT_EVT_start will be overridden) |
|
609 | - * @param bool $count whether to return the count or not (default FALSE) |
|
610 | - * @return EE_Event[]|int |
|
611 | - * @throws \EE_Error |
|
612 | - */ |
|
613 | - public function get_upcoming_events($query_params, $count = false) |
|
614 | - { |
|
615 | - if (array_key_exists(0, $query_params)) { |
|
616 | - $where_params = $query_params[0]; |
|
617 | - unset($query_params[0]); |
|
618 | - } else { |
|
619 | - $where_params = array(); |
|
620 | - } |
|
621 | - // if we have count make sure we don't include group by |
|
622 | - if ($count && isset($query_params['group_by'])) { |
|
623 | - unset($query_params['group_by']); |
|
624 | - } |
|
625 | - // let's add specific query_params for active_events |
|
626 | - // keep in mind this will override any sent status in the query AND any date queries. |
|
627 | - $where_params['status'] = array('IN', array('publish', EEM_Event::sold_out)); |
|
628 | - // if there are already query_params matching DTT_EVT_start then we need to modify that to add them. |
|
629 | - if (isset($where_params['Datetime.DTT_EVT_start'])) { |
|
630 | - $where_params['Datetime.DTT_EVT_start*****'] = array( |
|
631 | - '>', |
|
632 | - EEM_Datetime::instance()->current_time_for_query('DTT_EVT_start'), |
|
633 | - ); |
|
634 | - } else { |
|
635 | - $where_params['Datetime.DTT_EVT_start'] = array( |
|
636 | - '>', |
|
637 | - EEM_Datetime::instance()->current_time_for_query('DTT_EVT_start'), |
|
638 | - ); |
|
639 | - } |
|
640 | - $query_params[0] = $where_params; |
|
641 | - // don't use $query_params with count() |
|
642 | - // because we don't want to include additional query clauses like "GROUP BY" |
|
643 | - return $count |
|
644 | - ? $this->count(array($where_params), 'EVT_ID', true) |
|
645 | - : $this->get_all($query_params); |
|
646 | - } |
|
647 | - |
|
648 | - |
|
649 | - |
|
650 | - /** |
|
651 | - * Gets all events that are published |
|
652 | - * and have an event end time later than now |
|
653 | - * |
|
654 | - * @param array $query_params An array of query params to further filter on |
|
655 | - * (note that status and DTT_EVT_end will be overridden) |
|
656 | - * @param bool $count whether to return the count or not (default FALSE) |
|
657 | - * @return EE_Event[]|int |
|
658 | - * @throws \EE_Error |
|
659 | - */ |
|
660 | - public function get_active_and_upcoming_events($query_params, $count = false) |
|
661 | - { |
|
662 | - if (array_key_exists(0, $query_params)) { |
|
663 | - $where_params = $query_params[0]; |
|
664 | - unset($query_params[0]); |
|
665 | - } else { |
|
666 | - $where_params = array(); |
|
667 | - } |
|
668 | - // if we have count make sure we don't include group by |
|
669 | - if ($count && isset($query_params['group_by'])) { |
|
670 | - unset($query_params['group_by']); |
|
671 | - } |
|
672 | - // let's add specific query_params for active_events |
|
673 | - // keep in mind this will override any sent status in the query AND any date queries. |
|
674 | - $where_params['status'] = array('IN', array('publish', EEM_Event::sold_out)); |
|
675 | - // add where params for DTT_EVT_end |
|
676 | - if (isset($where_params['Datetime.DTT_EVT_end'])) { |
|
677 | - $where_params['Datetime.DTT_EVT_end*****'] = array( |
|
678 | - '>', |
|
679 | - EEM_Datetime::instance()->current_time_for_query('DTT_EVT_end'), |
|
680 | - ); |
|
681 | - } else { |
|
682 | - $where_params['Datetime.DTT_EVT_end'] = array( |
|
683 | - '>', |
|
684 | - EEM_Datetime::instance()->current_time_for_query('DTT_EVT_end'), |
|
685 | - ); |
|
686 | - } |
|
687 | - $query_params[0] = $where_params; |
|
688 | - // don't use $query_params with count() |
|
689 | - // because we don't want to include additional query clauses like "GROUP BY" |
|
690 | - return $count |
|
691 | - ? $this->count(array($where_params), 'EVT_ID', true) |
|
692 | - : $this->get_all($query_params); |
|
693 | - } |
|
694 | - |
|
695 | - |
|
696 | - |
|
697 | - /** |
|
698 | - * This only returns events that are expired. |
|
699 | - * They may still be published but all their datetimes have expired. |
|
700 | - * |
|
701 | - * @param array $query_params An array of query params to further filter on |
|
702 | - * (note that status and DTT_EVT_end will be overridden) |
|
703 | - * @param bool $count whether to return the count or not (default FALSE) |
|
704 | - * @return EE_Event[]|int |
|
705 | - * @throws \EE_Error |
|
706 | - */ |
|
707 | - public function get_expired_events($query_params, $count = false) |
|
708 | - { |
|
709 | - $where_params = isset($query_params[0]) ? $query_params[0] : array(); |
|
710 | - // if we have count make sure we don't include group by |
|
711 | - if ($count && isset($query_params['group_by'])) { |
|
712 | - unset($query_params['group_by']); |
|
713 | - } |
|
714 | - // let's add specific query_params for active_events |
|
715 | - // keep in mind this will override any sent status in the query AND any date queries. |
|
716 | - if (isset($where_params['status'])) { |
|
717 | - unset($where_params['status']); |
|
718 | - } |
|
719 | - $exclude_query = $query_params; |
|
720 | - if (isset($exclude_query[0])) { |
|
721 | - unset($exclude_query[0]); |
|
722 | - } |
|
723 | - $exclude_query[0] = array( |
|
724 | - 'Datetime.DTT_EVT_end' => array( |
|
725 | - '>', |
|
726 | - EEM_Datetime::instance()->current_time_for_query('DTT_EVT_end'), |
|
727 | - ), |
|
728 | - ); |
|
729 | - // first get all events that have datetimes where its not expired. |
|
730 | - $event_ids = $this->_get_all_wpdb_results($exclude_query, OBJECT_K, 'Event_CPT.ID'); |
|
731 | - $event_ids = array_keys($event_ids); |
|
732 | - // if we have any additional query_params, let's add them to the 'AND' condition |
|
733 | - $and_condition = array( |
|
734 | - 'Datetime.DTT_EVT_end' => array('<', EEM_Datetime::instance()->current_time_for_query('DTT_EVT_end')), |
|
735 | - 'EVT_ID' => array('NOT IN', $event_ids), |
|
736 | - ); |
|
737 | - if (isset($where_params['OR'])) { |
|
738 | - $and_condition['OR'] = $where_params['OR']; |
|
739 | - unset($where_params['OR']); |
|
740 | - } |
|
741 | - if (isset($where_params['Datetime.DTT_EVT_end'])) { |
|
742 | - $and_condition['Datetime.DTT_EVT_end****'] = $where_params['Datetime.DTT_EVT_end']; |
|
743 | - unset($where_params['Datetime.DTT_EVT_end']); |
|
744 | - } |
|
745 | - if (isset($where_params['Datetime.DTT_EVT_start'])) { |
|
746 | - $and_condition['Datetime.DTT_EVT_start'] = $where_params['Datetime.DTT_EVT_start']; |
|
747 | - unset($where_params['Datetime.DTT_EVT_start']); |
|
748 | - } |
|
749 | - // merge remaining $where params with the and conditions. |
|
750 | - $where_params['AND'] = array_merge($and_condition, $where_params); |
|
751 | - $query_params[0] = $where_params; |
|
752 | - // don't use $query_params with count() |
|
753 | - // because we don't want to include additional query clauses like "GROUP BY" |
|
754 | - return $count |
|
755 | - ? $this->count(array($where_params), 'EVT_ID', true) |
|
756 | - : $this->get_all($query_params); |
|
757 | - } |
|
758 | - |
|
759 | - |
|
760 | - |
|
761 | - /** |
|
762 | - * This basically just returns the events that do not have the publish status. |
|
763 | - * |
|
764 | - * @param array $query_params An array of query params to further filter on |
|
765 | - * (note that status will be overwritten) |
|
766 | - * @param boolean $count whether to return the count or not (default FALSE) |
|
767 | - * @return EE_Event[]|int |
|
768 | - * @throws \EE_Error |
|
769 | - */ |
|
770 | - public function get_inactive_events($query_params, $count = false) |
|
771 | - { |
|
772 | - $where_params = isset($query_params[0]) ? $query_params[0] : array(); |
|
773 | - // let's add in specific query_params for inactive events. |
|
774 | - if (isset($where_params['status'])) { |
|
775 | - unset($where_params['status']); |
|
776 | - } |
|
777 | - // if we have count make sure we don't include group by |
|
778 | - if ($count && isset($query_params['group_by'])) { |
|
779 | - unset($query_params['group_by']); |
|
780 | - } |
|
781 | - // if we have any additional query_params, let's add them to the 'AND' condition |
|
782 | - $where_params['AND']['status'] = array('!=', 'publish'); |
|
783 | - if (isset($where_params['OR'])) { |
|
784 | - $where_params['AND']['OR'] = $where_params['OR']; |
|
785 | - unset($where_params['OR']); |
|
786 | - } |
|
787 | - if (isset($where_params['Datetime.DTT_EVT_end'])) { |
|
788 | - $where_params['AND']['Datetime.DTT_EVT_end****'] = $where_params['Datetime.DTT_EVT_end']; |
|
789 | - unset($where_params['Datetime.DTT_EVT_end']); |
|
790 | - } |
|
791 | - if (isset($where_params['Datetime.DTT_EVT_start'])) { |
|
792 | - $where_params['AND']['Datetime.DTT_EVT_start'] = $where_params['Datetime.DTT_EVT_start']; |
|
793 | - unset($where_params['Datetime.DTT_EVT_start']); |
|
794 | - } |
|
795 | - $query_params[0] = $where_params; |
|
796 | - // don't use $query_params with count() |
|
797 | - // because we don't want to include additional query clauses like "GROUP BY" |
|
798 | - return $count |
|
799 | - ? $this->count(array($where_params), 'EVT_ID', true) |
|
800 | - : $this->get_all($query_params); |
|
801 | - } |
|
802 | - |
|
803 | - |
|
804 | - |
|
805 | - /** |
|
806 | - * This is just injecting into the parent add_relationship_to so we do special handling on price relationships |
|
807 | - * because we don't want to override any existing global default prices but instead insert NEW prices that get |
|
808 | - * attached to the event. See parent for param descriptions |
|
809 | - * |
|
810 | - * @param $id_or_obj |
|
811 | - * @param $other_model_id_or_obj |
|
812 | - * @param string $relationName |
|
813 | - * @param array $where_query |
|
814 | - * @return EE_Base_Class |
|
815 | - * @throws EE_Error |
|
816 | - */ |
|
817 | - public function add_relationship_to($id_or_obj, $other_model_id_or_obj, $relationName, $where_query = array()) |
|
818 | - { |
|
819 | - if ($relationName === 'Price') { |
|
820 | - // let's get the PRC object for the given ID to make sure that we aren't dealing with a default |
|
821 | - $prc_chk = $this->get_related_model_obj($relationName)->ensure_is_obj($other_model_id_or_obj); |
|
822 | - // if EVT_ID = 0, then this is a default |
|
823 | - if ((int) $prc_chk->get('EVT_ID') === 0) { |
|
824 | - // let's set the prc_id as 0 so we force an insert on the add_relation_to carried out by relation |
|
825 | - $prc_chk->set('PRC_ID', 0); |
|
826 | - } |
|
827 | - // run parent |
|
828 | - return parent::add_relationship_to($id_or_obj, $prc_chk, $relationName, $where_query); |
|
829 | - } |
|
830 | - // otherwise carry on as normal |
|
831 | - return parent::add_relationship_to($id_or_obj, $other_model_id_or_obj, $relationName, $where_query); |
|
832 | - } |
|
833 | - |
|
834 | - |
|
835 | - |
|
836 | - /******************** DEPRECATED METHODS ********************/ |
|
837 | - |
|
838 | - |
|
839 | - |
|
840 | - /** |
|
841 | - * _get_question_target_db_column |
|
842 | - * |
|
843 | - * @deprecated as of 4.8.32.rc.001. Instead consider using |
|
844 | - * EE_Registration_Custom_Questions_Form located in |
|
845 | - * admin_pages/registrations/form_sections/EE_Registration_Custom_Questions_Form.form.php |
|
846 | - * @access public |
|
847 | - * @param EE_Registration $registration (so existing answers for registration are included) |
|
848 | - * @param int $EVT_ID so all question groups are included for event (not just answers from |
|
849 | - * registration). |
|
850 | - * @throws EE_Error |
|
851 | - * @return array |
|
852 | - */ |
|
853 | - public function assemble_array_of_groups_questions_and_options(EE_Registration $registration, $EVT_ID = 0) |
|
854 | - { |
|
855 | - if (empty($EVT_ID)) { |
|
856 | - throw new EE_Error(__( |
|
857 | - 'An error occurred. No EVT_ID is included. Needed to know which question groups to retrieve.', |
|
858 | - 'event_espresso' |
|
859 | - )); |
|
860 | - } |
|
861 | - $questions = array(); |
|
862 | - // get all question groups for event |
|
863 | - $qgs = $this->get_question_groups_for_event($EVT_ID, $registration); |
|
864 | - if (! empty($qgs)) { |
|
865 | - foreach ($qgs as $qg) { |
|
866 | - $qsts = $qg->questions(); |
|
867 | - $questions[ $qg->ID() ] = $qg->model_field_array(); |
|
868 | - $questions[ $qg->ID() ]['QSG_questions'] = array(); |
|
869 | - foreach ($qsts as $qst) { |
|
870 | - if ($qst->is_system_question()) { |
|
871 | - continue; |
|
872 | - } |
|
873 | - $answer = EEM_Answer::instance()->get_one(array( |
|
874 | - array( |
|
875 | - 'QST_ID' => $qst->ID(), |
|
876 | - 'REG_ID' => $registration->ID(), |
|
877 | - ), |
|
878 | - )); |
|
879 | - $answer = $answer instanceof EE_Answer ? $answer : EEM_Answer::instance()->create_default_object(); |
|
880 | - $qst_name = $qstn_id = $qst->ID(); |
|
881 | - $ans_id = $answer->ID(); |
|
882 | - $qst_name = ! empty($ans_id) ? '[' . $qst_name . '][' . $ans_id . ']' : '[' . $qst_name . ']'; |
|
883 | - $input_name = ''; |
|
884 | - $input_id = sanitize_key($qst->display_text()); |
|
885 | - $input_class = ''; |
|
886 | - $questions[ $qg->ID() ]['QSG_questions'][ $qst->ID() ] = $qst->model_field_array(); |
|
887 | - $questions[ $qg->ID() ]['QSG_questions'][ $qst->ID() ]['QST_input_name'] = 'qstn' |
|
888 | - . $input_name |
|
889 | - . $qst_name; |
|
890 | - $questions[ $qg->ID() ]['QSG_questions'][ $qst->ID() ]['QST_input_id'] = $input_id . '-' . $qstn_id; |
|
891 | - $questions[ $qg->ID() ]['QSG_questions'][ $qst->ID() ]['QST_input_class'] = $input_class; |
|
892 | - $questions[ $qg->ID() ]['QSG_questions'][ $qst->ID() ]['QST_options'] = array(); |
|
893 | - $questions[ $qg->ID() ]['QSG_questions'][ $qst->ID() ]['qst_obj'] = $qst; |
|
894 | - $questions[ $qg->ID() ]['QSG_questions'][ $qst->ID() ]['ans_obj'] = $answer; |
|
895 | - // leave responses as-is, don't convert stuff into html entities please! |
|
896 | - $questions[ $qg->ID() ]['QSG_questions'][ $qst->ID() ]['htmlentities'] = false; |
|
897 | - if ($qst->type() == 'RADIO_BTN' || $qst->type() == 'CHECKBOX' || $qst->type() == 'DROPDOWN') { |
|
898 | - $QSOs = $qst->options(true, $answer->value()); |
|
899 | - if (is_array($QSOs)) { |
|
900 | - foreach ($QSOs as $QSO_ID => $QSO) { |
|
901 | - $questions[ $qg->ID() ]['QSG_questions'][ $qst->ID() ]['QST_options'][ $QSO_ID ] = $QSO->model_field_array(); |
|
902 | - } |
|
903 | - } |
|
904 | - } |
|
905 | - } |
|
906 | - } |
|
907 | - } |
|
908 | - return $questions; |
|
909 | - } |
|
910 | - |
|
911 | - |
|
912 | - /** |
|
913 | - * @param mixed $cols_n_values either an array of where each key is the name of a field, and the value is its value |
|
914 | - * or an stdClass where each property is the name of a column, |
|
915 | - * @return EE_Base_Class |
|
916 | - * @throws \EE_Error |
|
917 | - */ |
|
918 | - public function instantiate_class_from_array_or_object($cols_n_values) |
|
919 | - { |
|
920 | - $classInstance = parent::instantiate_class_from_array_or_object($cols_n_values); |
|
921 | - if ($classInstance instanceof EE_Event) { |
|
922 | - // events have their timezone defined in the DB, so use it immediately |
|
923 | - $this->set_timezone($classInstance->get_timezone()); |
|
924 | - } |
|
925 | - return $classInstance; |
|
926 | - } |
|
18 | + /** |
|
19 | + * constant used by status(), indicating that no more tickets can be purchased for any of the datetimes for the |
|
20 | + * event |
|
21 | + */ |
|
22 | + const sold_out = 'sold_out'; |
|
23 | + |
|
24 | + /** |
|
25 | + * constant used by status(), indicating that upcoming event dates have been postponed (may be pushed to a later |
|
26 | + * date) |
|
27 | + */ |
|
28 | + const postponed = 'postponed'; |
|
29 | + |
|
30 | + /** |
|
31 | + * constant used by status(), indicating that the event will no longer occur |
|
32 | + */ |
|
33 | + const cancelled = 'cancelled'; |
|
34 | + |
|
35 | + |
|
36 | + /** |
|
37 | + * @var string |
|
38 | + */ |
|
39 | + protected static $_default_reg_status; |
|
40 | + |
|
41 | + |
|
42 | + /** |
|
43 | + * This is the default for the additional limit field. |
|
44 | + * @var int |
|
45 | + */ |
|
46 | + protected static $_default_additional_limit = 10; |
|
47 | + |
|
48 | + |
|
49 | + /** |
|
50 | + * private instance of the Event object |
|
51 | + * |
|
52 | + * @var EEM_Event |
|
53 | + */ |
|
54 | + protected static $_instance; |
|
55 | + |
|
56 | + |
|
57 | + |
|
58 | + |
|
59 | + /** |
|
60 | + * Adds a relationship to Term_Taxonomy for each CPT_Base |
|
61 | + * |
|
62 | + * @param string $timezone |
|
63 | + * @throws \EE_Error |
|
64 | + */ |
|
65 | + protected function __construct($timezone = null) |
|
66 | + { |
|
67 | + EE_Registry::instance()->load_model('Registration'); |
|
68 | + $this->singular_item = esc_html__('Event', 'event_espresso'); |
|
69 | + $this->plural_item = esc_html__('Events', 'event_espresso'); |
|
70 | + // to remove Cancelled events from the frontend, copy the following filter to your functions.php file |
|
71 | + // add_filter( 'AFEE__EEM_Event__construct___custom_stati__cancelled__Public', '__return_false' ); |
|
72 | + // to remove Postponed events from the frontend, copy the following filter to your functions.php file |
|
73 | + // add_filter( 'AFEE__EEM_Event__construct___custom_stati__postponed__Public', '__return_false' ); |
|
74 | + // to remove Sold Out events from the frontend, copy the following filter to your functions.php file |
|
75 | + // add_filter( 'AFEE__EEM_Event__construct___custom_stati__sold_out__Public', '__return_false' ); |
|
76 | + $this->_custom_stati = apply_filters( |
|
77 | + 'AFEE__EEM_Event__construct___custom_stati', |
|
78 | + array( |
|
79 | + EEM_Event::cancelled => array( |
|
80 | + 'label' => esc_html__('Cancelled', 'event_espresso'), |
|
81 | + 'public' => apply_filters('AFEE__EEM_Event__construct___custom_stati__cancelled__Public', true), |
|
82 | + ), |
|
83 | + EEM_Event::postponed => array( |
|
84 | + 'label' => esc_html__('Postponed', 'event_espresso'), |
|
85 | + 'public' => apply_filters('AFEE__EEM_Event__construct___custom_stati__postponed__Public', true), |
|
86 | + ), |
|
87 | + EEM_Event::sold_out => array( |
|
88 | + 'label' => esc_html__('Sold Out', 'event_espresso'), |
|
89 | + 'public' => apply_filters('AFEE__EEM_Event__construct___custom_stati__sold_out__Public', true), |
|
90 | + ), |
|
91 | + ) |
|
92 | + ); |
|
93 | + self::$_default_reg_status = empty(self::$_default_reg_status) ? EEM_Registration::status_id_pending_payment |
|
94 | + : self::$_default_reg_status; |
|
95 | + $this->_tables = array( |
|
96 | + 'Event_CPT' => new EE_Primary_Table('posts', 'ID'), |
|
97 | + 'Event_Meta' => new EE_Secondary_Table('esp_event_meta', 'EVTM_ID', 'EVT_ID'), |
|
98 | + ); |
|
99 | + $this->_fields = array( |
|
100 | + 'Event_CPT' => array( |
|
101 | + 'EVT_ID' => new EE_Primary_Key_Int_Field( |
|
102 | + 'ID', |
|
103 | + esc_html__('Post ID for Event', 'event_espresso') |
|
104 | + ), |
|
105 | + 'EVT_name' => new EE_Plain_Text_Field( |
|
106 | + 'post_title', |
|
107 | + esc_html__('Event Name', 'event_espresso'), |
|
108 | + false, |
|
109 | + '' |
|
110 | + ), |
|
111 | + 'EVT_desc' => new EE_Post_Content_Field( |
|
112 | + 'post_content', |
|
113 | + esc_html__('Event Description', 'event_espresso'), |
|
114 | + false, |
|
115 | + '' |
|
116 | + ), |
|
117 | + 'EVT_slug' => new EE_Slug_Field( |
|
118 | + 'post_name', |
|
119 | + esc_html__('Event Slug', 'event_espresso'), |
|
120 | + false, |
|
121 | + '' |
|
122 | + ), |
|
123 | + 'EVT_created' => new EE_Datetime_Field( |
|
124 | + 'post_date', |
|
125 | + esc_html__('Date/Time Event Created', 'event_espresso'), |
|
126 | + false, |
|
127 | + EE_Datetime_Field::now |
|
128 | + ), |
|
129 | + 'EVT_short_desc' => new EE_Simple_HTML_Field( |
|
130 | + 'post_excerpt', |
|
131 | + esc_html__('Event Short Description', 'event_espresso'), |
|
132 | + false, |
|
133 | + '' |
|
134 | + ), |
|
135 | + 'EVT_modified' => new EE_Datetime_Field( |
|
136 | + 'post_modified', |
|
137 | + esc_html__('Date/Time Event Modified', 'event_espresso'), |
|
138 | + false, |
|
139 | + EE_Datetime_Field::now |
|
140 | + ), |
|
141 | + 'EVT_wp_user' => new EE_WP_User_Field( |
|
142 | + 'post_author', |
|
143 | + esc_html__('Event Creator ID', 'event_espresso'), |
|
144 | + false |
|
145 | + ), |
|
146 | + 'parent' => new EE_Integer_Field( |
|
147 | + 'post_parent', |
|
148 | + esc_html__('Event Parent ID', 'event_espresso'), |
|
149 | + false, |
|
150 | + 0 |
|
151 | + ), |
|
152 | + 'EVT_order' => new EE_Integer_Field( |
|
153 | + 'menu_order', |
|
154 | + esc_html__('Event Menu Order', 'event_espresso'), |
|
155 | + false, |
|
156 | + 1 |
|
157 | + ), |
|
158 | + 'post_type' => new EE_WP_Post_Type_Field('espresso_events'), |
|
159 | + // EE_Plain_Text_Field( 'post_type', esc_html__( 'Event Post Type', 'event_espresso' ), FALSE, 'espresso_events' ), |
|
160 | + 'status' => new EE_WP_Post_Status_Field( |
|
161 | + 'post_status', |
|
162 | + esc_html__('Event Status', 'event_espresso'), |
|
163 | + false, |
|
164 | + 'draft', |
|
165 | + $this->_custom_stati |
|
166 | + ), |
|
167 | + 'password' => new EE_Password_Field( |
|
168 | + 'post_password', |
|
169 | + __('Password', 'event_espresso'), |
|
170 | + false, |
|
171 | + '', |
|
172 | + array( |
|
173 | + 'EVT_desc', |
|
174 | + 'EVT_short_desc', |
|
175 | + 'EVT_display_desc', |
|
176 | + 'EVT_display_ticket_selector', |
|
177 | + 'EVT_visible_on', |
|
178 | + 'EVT_additional_limit', |
|
179 | + 'EVT_default_registration_status', |
|
180 | + 'EVT_member_only', |
|
181 | + 'EVT_phone', |
|
182 | + 'EVT_allow_overflow', |
|
183 | + 'EVT_timezone_string', |
|
184 | + 'EVT_external_URL', |
|
185 | + 'EVT_donations' |
|
186 | + ) |
|
187 | + ) |
|
188 | + ), |
|
189 | + 'Event_Meta' => array( |
|
190 | + 'EVTM_ID' => new EE_DB_Only_Float_Field( |
|
191 | + 'EVTM_ID', |
|
192 | + esc_html__('Event Meta Row ID', 'event_espresso'), |
|
193 | + false |
|
194 | + ), |
|
195 | + 'EVT_ID_fk' => new EE_DB_Only_Int_Field( |
|
196 | + 'EVT_ID', |
|
197 | + esc_html__('Foreign key to Event ID from Event Meta table', 'event_espresso'), |
|
198 | + false |
|
199 | + ), |
|
200 | + 'EVT_display_desc' => new EE_Boolean_Field( |
|
201 | + 'EVT_display_desc', |
|
202 | + esc_html__('Display Description Flag', 'event_espresso'), |
|
203 | + false, |
|
204 | + true |
|
205 | + ), |
|
206 | + 'EVT_display_ticket_selector' => new EE_Boolean_Field( |
|
207 | + 'EVT_display_ticket_selector', |
|
208 | + esc_html__('Display Ticket Selector Flag', 'event_espresso'), |
|
209 | + false, |
|
210 | + true |
|
211 | + ), |
|
212 | + 'EVT_visible_on' => new EE_Datetime_Field( |
|
213 | + 'EVT_visible_on', |
|
214 | + esc_html__('Event Visible Date', 'event_espresso'), |
|
215 | + true, |
|
216 | + EE_Datetime_Field::now |
|
217 | + ), |
|
218 | + 'EVT_additional_limit' => new EE_Integer_Field( |
|
219 | + 'EVT_additional_limit', |
|
220 | + esc_html__('Limit of Additional Registrations on Same Transaction', 'event_espresso'), |
|
221 | + true, |
|
222 | + self::$_default_additional_limit |
|
223 | + ), |
|
224 | + 'EVT_default_registration_status' => new EE_Enum_Text_Field( |
|
225 | + 'EVT_default_registration_status', |
|
226 | + esc_html__('Default Registration Status on this Event', 'event_espresso'), |
|
227 | + false, |
|
228 | + EEM_Event::$_default_reg_status, |
|
229 | + EEM_Registration::reg_status_array() |
|
230 | + ), |
|
231 | + 'EVT_member_only' => new EE_Boolean_Field( |
|
232 | + 'EVT_member_only', |
|
233 | + esc_html__('Member-Only Event Flag', 'event_espresso'), |
|
234 | + false, |
|
235 | + false |
|
236 | + ), |
|
237 | + 'EVT_phone' => new EE_Plain_Text_Field( |
|
238 | + 'EVT_phone', |
|
239 | + esc_html__('Event Phone Number', 'event_espresso'), |
|
240 | + false, |
|
241 | + '' |
|
242 | + ), |
|
243 | + 'EVT_allow_overflow' => new EE_Boolean_Field( |
|
244 | + 'EVT_allow_overflow', |
|
245 | + esc_html__('Allow Overflow on Event', 'event_espresso'), |
|
246 | + false, |
|
247 | + false |
|
248 | + ), |
|
249 | + 'EVT_timezone_string' => new EE_Plain_Text_Field( |
|
250 | + 'EVT_timezone_string', |
|
251 | + esc_html__('Timezone (name) for Event times', 'event_espresso'), |
|
252 | + false, |
|
253 | + '' |
|
254 | + ), |
|
255 | + 'EVT_external_URL' => new EE_Plain_Text_Field( |
|
256 | + 'EVT_external_URL', |
|
257 | + esc_html__('URL of Event Page if hosted elsewhere', 'event_espresso'), |
|
258 | + true |
|
259 | + ), |
|
260 | + 'EVT_donations' => new EE_Boolean_Field( |
|
261 | + 'EVT_donations', |
|
262 | + esc_html__('Accept Donations?', 'event_espresso'), |
|
263 | + false, |
|
264 | + false |
|
265 | + ), |
|
266 | + ), |
|
267 | + ); |
|
268 | + $this->_model_relations = array( |
|
269 | + 'Registration' => new EE_Has_Many_Relation(), |
|
270 | + 'Datetime' => new EE_Has_Many_Relation(), |
|
271 | + 'Question_Group' => new EE_HABTM_Relation('Event_Question_Group'), |
|
272 | + 'Event_Question_Group' => new EE_Has_Many_Relation(), |
|
273 | + 'Venue' => new EE_HABTM_Relation('Event_Venue'), |
|
274 | + 'Term_Relationship' => new EE_Has_Many_Relation(), |
|
275 | + 'Term_Taxonomy' => new EE_HABTM_Relation('Term_Relationship'), |
|
276 | + 'Message_Template_Group' => new EE_HABTM_Relation('Event_Message_Template'), |
|
277 | + 'Attendee' => new EE_HABTM_Relation('Registration'), |
|
278 | + 'WP_User' => new EE_Belongs_To_Relation(), |
|
279 | + ); |
|
280 | + // this model is generally available for reading |
|
281 | + $this->_cap_restriction_generators[ EEM_Base::caps_read ] = new EE_Restriction_Generator_Public(); |
|
282 | + $this->model_chain_to_password = ''; |
|
283 | + parent::__construct($timezone); |
|
284 | + } |
|
285 | + |
|
286 | + |
|
287 | + |
|
288 | + /** |
|
289 | + * @param string $default_reg_status |
|
290 | + */ |
|
291 | + public static function set_default_reg_status($default_reg_status) |
|
292 | + { |
|
293 | + self::$_default_reg_status = $default_reg_status; |
|
294 | + // if EEM_Event has already been instantiated, |
|
295 | + // then we need to reset the `EVT_default_reg_status` field to use the new default. |
|
296 | + if (self::$_instance instanceof EEM_Event) { |
|
297 | + $default_reg_status = new EE_Enum_Text_Field( |
|
298 | + 'EVT_default_registration_status', |
|
299 | + esc_html__('Default Registration Status on this Event', 'event_espresso'), |
|
300 | + false, |
|
301 | + $default_reg_status, |
|
302 | + EEM_Registration::reg_status_array() |
|
303 | + ); |
|
304 | + $default_reg_status->_construct_finalize( |
|
305 | + 'Event_Meta', |
|
306 | + 'EVT_default_registration_status', |
|
307 | + 'EEM_Event' |
|
308 | + ); |
|
309 | + self::$_instance->_fields['Event_Meta']['EVT_default_registration_status'] = $default_reg_status; |
|
310 | + } |
|
311 | + } |
|
312 | + |
|
313 | + |
|
314 | + /** |
|
315 | + * Used to override the default for the additional limit field. |
|
316 | + * @param $additional_limit |
|
317 | + */ |
|
318 | + public static function set_default_additional_limit($additional_limit) |
|
319 | + { |
|
320 | + self::$_default_additional_limit = (int) $additional_limit; |
|
321 | + if (self::$_instance instanceof EEM_Event) { |
|
322 | + self::$_instance->_fields['Event_Meta']['EVT_additional_limit'] = new EE_Integer_Field( |
|
323 | + 'EVT_additional_limit', |
|
324 | + __('Limit of Additional Registrations on Same Transaction', 'event_espresso'), |
|
325 | + true, |
|
326 | + self::$_default_additional_limit |
|
327 | + ); |
|
328 | + self::$_instance->_fields['Event_Meta']['EVT_additional_limit']->_construct_finalize( |
|
329 | + 'Event_Meta', |
|
330 | + 'EVT_additional_limit', |
|
331 | + 'EEM_Event' |
|
332 | + ); |
|
333 | + } |
|
334 | + } |
|
335 | + |
|
336 | + |
|
337 | + /** |
|
338 | + * Return what is currently set as the default additional limit for the event. |
|
339 | + * @return int |
|
340 | + */ |
|
341 | + public static function get_default_additional_limit() |
|
342 | + { |
|
343 | + return apply_filters('FHEE__EEM_Event__get_default_additional_limit', self::$_default_additional_limit); |
|
344 | + } |
|
345 | + |
|
346 | + |
|
347 | + /** |
|
348 | + * get_question_groups |
|
349 | + * |
|
350 | + * @return array |
|
351 | + * @throws \EE_Error |
|
352 | + */ |
|
353 | + public function get_all_question_groups() |
|
354 | + { |
|
355 | + return EE_Registry::instance()->load_model('Question_Group')->get_all( |
|
356 | + array( |
|
357 | + array('QSG_deleted' => false), |
|
358 | + 'order_by' => array('QSG_order' => 'ASC'), |
|
359 | + ) |
|
360 | + ); |
|
361 | + } |
|
362 | + |
|
363 | + |
|
364 | + |
|
365 | + /** |
|
366 | + * get_question_groups |
|
367 | + * |
|
368 | + * @param int $EVT_ID |
|
369 | + * @return array|bool |
|
370 | + * @throws \EE_Error |
|
371 | + */ |
|
372 | + public function get_all_event_question_groups($EVT_ID = 0) |
|
373 | + { |
|
374 | + if (! isset($EVT_ID) || ! absint($EVT_ID)) { |
|
375 | + EE_Error::add_error( |
|
376 | + esc_html__( |
|
377 | + 'An error occurred. No Event Question Groups could be retrieved because an Event ID was not received.', |
|
378 | + 'event_espresso' |
|
379 | + ), |
|
380 | + __FILE__, |
|
381 | + __FUNCTION__, |
|
382 | + __LINE__ |
|
383 | + ); |
|
384 | + return false; |
|
385 | + } |
|
386 | + return EE_Registry::instance()->load_model('Event_Question_Group')->get_all( |
|
387 | + array( |
|
388 | + array('EVT_ID' => $EVT_ID), |
|
389 | + ) |
|
390 | + ); |
|
391 | + } |
|
392 | + |
|
393 | + |
|
394 | + /** |
|
395 | + * get_question_groups |
|
396 | + * |
|
397 | + * @param int $EVT_ID |
|
398 | + * @param boolean $for_primary_attendee |
|
399 | + * @return array|bool |
|
400 | + * @throws EE_Error |
|
401 | + * @throws InvalidArgumentException |
|
402 | + * @throws ReflectionException |
|
403 | + * @throws InvalidDataTypeException |
|
404 | + * @throws InvalidInterfaceException |
|
405 | + */ |
|
406 | + public function get_event_question_groups($EVT_ID = 0, $for_primary_attendee = true) |
|
407 | + { |
|
408 | + if (! isset($EVT_ID) || ! absint($EVT_ID)) { |
|
409 | + EE_Error::add_error( |
|
410 | + esc_html__( |
|
411 | + // @codingStandardsIgnoreStart |
|
412 | + 'An error occurred. No Event Question Groups could be retrieved because an Event ID was not received.', |
|
413 | + // @codingStandardsIgnoreEnd |
|
414 | + 'event_espresso' |
|
415 | + ), |
|
416 | + __FILE__, |
|
417 | + __FUNCTION__, |
|
418 | + __LINE__ |
|
419 | + ); |
|
420 | + return false; |
|
421 | + } |
|
422 | + $query_params = [ |
|
423 | + [ |
|
424 | + 'EVT_ID' => $EVT_ID, |
|
425 | + EEM_Event_Question_Group::instance()->fieldNameForContext($for_primary_attendee) => true |
|
426 | + ] |
|
427 | + ]; |
|
428 | + if ($for_primary_attendee) { |
|
429 | + $query_params[0]['EQG_primary'] = true; |
|
430 | + } else { |
|
431 | + $query_params[0]['EQG_additional'] = true; |
|
432 | + } |
|
433 | + return EE_Registry::instance()->load_model('Event_Question_Group')->get_all($query_params); |
|
434 | + } |
|
435 | + |
|
436 | + |
|
437 | + /** |
|
438 | + * get_question_groups |
|
439 | + * |
|
440 | + * @param int $EVT_ID |
|
441 | + * @param EE_Registration $registration |
|
442 | + * @return array|bool |
|
443 | + * @throws EE_Error |
|
444 | + * @throws InvalidArgumentException |
|
445 | + * @throws InvalidDataTypeException |
|
446 | + * @throws InvalidInterfaceException |
|
447 | + * @throws ReflectionException |
|
448 | + */ |
|
449 | + public function get_question_groups_for_event($EVT_ID = 0, EE_Registration $registration) |
|
450 | + { |
|
451 | + if (! isset($EVT_ID) || ! absint($EVT_ID)) { |
|
452 | + EE_Error::add_error( |
|
453 | + esc_html__( |
|
454 | + 'An error occurred. No Question Groups could be retrieved because an Event ID was not received.', |
|
455 | + 'event_espresso' |
|
456 | + ), |
|
457 | + __FILE__, |
|
458 | + __FUNCTION__, |
|
459 | + __LINE__ |
|
460 | + ); |
|
461 | + return false; |
|
462 | + } |
|
463 | + return EE_Registry::instance()->load_model('Question_Group')->get_all( |
|
464 | + [ |
|
465 | + [ |
|
466 | + 'Event_Question_Group.EVT_ID' => $EVT_ID, |
|
467 | + 'Event_Question_Group.' |
|
468 | + . EEM_Event_Question_Group::instance()->fieldNameForContext( |
|
469 | + $registration->is_primary_registrant() |
|
470 | + ) => true |
|
471 | + ], |
|
472 | + 'order_by' => ['QSG_order' => 'ASC'], |
|
473 | + ] |
|
474 | + ); |
|
475 | + } |
|
476 | + |
|
477 | + |
|
478 | + |
|
479 | + /** |
|
480 | + * get_question_target_db_column |
|
481 | + * |
|
482 | + * @param string $QSG_IDs csv list of $QSG IDs |
|
483 | + * @return array|bool |
|
484 | + * @throws \EE_Error |
|
485 | + */ |
|
486 | + public function get_questions_in_groups($QSG_IDs = '') |
|
487 | + { |
|
488 | + if (empty($QSG_IDs)) { |
|
489 | + EE_Error::add_error( |
|
490 | + esc_html__('An error occurred. No Question Group IDs were received.', 'event_espresso'), |
|
491 | + __FILE__, |
|
492 | + __FUNCTION__, |
|
493 | + __LINE__ |
|
494 | + ); |
|
495 | + return false; |
|
496 | + } |
|
497 | + return EE_Registry::instance()->load_model('Question')->get_all( |
|
498 | + array( |
|
499 | + array( |
|
500 | + 'Question_Group.QSG_ID' => array('IN', $QSG_IDs), |
|
501 | + 'QST_deleted' => false, |
|
502 | + 'QST_admin_only' => is_admin(), |
|
503 | + ), |
|
504 | + 'order_by' => 'QST_order', |
|
505 | + ) |
|
506 | + ); |
|
507 | + } |
|
508 | + |
|
509 | + |
|
510 | + |
|
511 | + /** |
|
512 | + * get_options_for_question |
|
513 | + * |
|
514 | + * @param string $QST_IDs csv list of $QST IDs |
|
515 | + * @return array|bool |
|
516 | + * @throws \EE_Error |
|
517 | + */ |
|
518 | + public function get_options_for_question($QST_IDs) |
|
519 | + { |
|
520 | + if (empty($QST_IDs)) { |
|
521 | + EE_Error::add_error( |
|
522 | + esc_html__('An error occurred. No Question IDs were received.', 'event_espresso'), |
|
523 | + __FILE__, |
|
524 | + __FUNCTION__, |
|
525 | + __LINE__ |
|
526 | + ); |
|
527 | + return false; |
|
528 | + } |
|
529 | + return EE_Registry::instance()->load_model('Question_Option')->get_all( |
|
530 | + array( |
|
531 | + array( |
|
532 | + 'Question.QST_ID' => array('IN', $QST_IDs), |
|
533 | + 'QSO_deleted' => false, |
|
534 | + ), |
|
535 | + 'order_by' => 'QSO_ID', |
|
536 | + ) |
|
537 | + ); |
|
538 | + } |
|
539 | + |
|
540 | + |
|
541 | + |
|
542 | + |
|
543 | + |
|
544 | + |
|
545 | + |
|
546 | + /** |
|
547 | + * Gets all events that are published |
|
548 | + * and have event start time earlier than now and an event end time later than now |
|
549 | + * |
|
550 | + * @param array $query_params An array of query params to further filter on |
|
551 | + * (note that status and DTT_EVT_start and DTT_EVT_end will be overridden) |
|
552 | + * @param bool $count whether to return the count or not (default FALSE) |
|
553 | + * @return EE_Event[]|int |
|
554 | + * @throws \EE_Error |
|
555 | + */ |
|
556 | + public function get_active_events($query_params, $count = false) |
|
557 | + { |
|
558 | + if (array_key_exists(0, $query_params)) { |
|
559 | + $where_params = $query_params[0]; |
|
560 | + unset($query_params[0]); |
|
561 | + } else { |
|
562 | + $where_params = array(); |
|
563 | + } |
|
564 | + // if we have count make sure we don't include group by |
|
565 | + if ($count && isset($query_params['group_by'])) { |
|
566 | + unset($query_params['group_by']); |
|
567 | + } |
|
568 | + // let's add specific query_params for active_events |
|
569 | + // keep in mind this will override any sent status in the query AND any date queries. |
|
570 | + $where_params['status'] = array('IN', array('publish', EEM_Event::sold_out)); |
|
571 | + // if already have where params for DTT_EVT_start or DTT_EVT_end then append these conditions |
|
572 | + if (isset($where_params['Datetime.DTT_EVT_start'])) { |
|
573 | + $where_params['Datetime.DTT_EVT_start******'] = array( |
|
574 | + '<', |
|
575 | + EEM_Datetime::instance()->current_time_for_query('DTT_EVT_start'), |
|
576 | + ); |
|
577 | + } else { |
|
578 | + $where_params['Datetime.DTT_EVT_start'] = array( |
|
579 | + '<', |
|
580 | + EEM_Datetime::instance()->current_time_for_query('DTT_EVT_start'), |
|
581 | + ); |
|
582 | + } |
|
583 | + if (isset($where_params['Datetime.DTT_EVT_end'])) { |
|
584 | + $where_params['Datetime.DTT_EVT_end*****'] = array( |
|
585 | + '>', |
|
586 | + EEM_Datetime::instance()->current_time_for_query('DTT_EVT_end'), |
|
587 | + ); |
|
588 | + } else { |
|
589 | + $where_params['Datetime.DTT_EVT_end'] = array( |
|
590 | + '>', |
|
591 | + EEM_Datetime::instance()->current_time_for_query('DTT_EVT_end'), |
|
592 | + ); |
|
593 | + } |
|
594 | + $query_params[0] = $where_params; |
|
595 | + // don't use $query_params with count() |
|
596 | + // because we don't want to include additional query clauses like "GROUP BY" |
|
597 | + return $count |
|
598 | + ? $this->count(array($where_params), 'EVT_ID', true) |
|
599 | + : $this->get_all($query_params); |
|
600 | + } |
|
601 | + |
|
602 | + |
|
603 | + |
|
604 | + /** |
|
605 | + * get all events that are published and have an event start time later than now |
|
606 | + * |
|
607 | + * @param array $query_params An array of query params to further filter on |
|
608 | + * (Note that status and DTT_EVT_start will be overridden) |
|
609 | + * @param bool $count whether to return the count or not (default FALSE) |
|
610 | + * @return EE_Event[]|int |
|
611 | + * @throws \EE_Error |
|
612 | + */ |
|
613 | + public function get_upcoming_events($query_params, $count = false) |
|
614 | + { |
|
615 | + if (array_key_exists(0, $query_params)) { |
|
616 | + $where_params = $query_params[0]; |
|
617 | + unset($query_params[0]); |
|
618 | + } else { |
|
619 | + $where_params = array(); |
|
620 | + } |
|
621 | + // if we have count make sure we don't include group by |
|
622 | + if ($count && isset($query_params['group_by'])) { |
|
623 | + unset($query_params['group_by']); |
|
624 | + } |
|
625 | + // let's add specific query_params for active_events |
|
626 | + // keep in mind this will override any sent status in the query AND any date queries. |
|
627 | + $where_params['status'] = array('IN', array('publish', EEM_Event::sold_out)); |
|
628 | + // if there are already query_params matching DTT_EVT_start then we need to modify that to add them. |
|
629 | + if (isset($where_params['Datetime.DTT_EVT_start'])) { |
|
630 | + $where_params['Datetime.DTT_EVT_start*****'] = array( |
|
631 | + '>', |
|
632 | + EEM_Datetime::instance()->current_time_for_query('DTT_EVT_start'), |
|
633 | + ); |
|
634 | + } else { |
|
635 | + $where_params['Datetime.DTT_EVT_start'] = array( |
|
636 | + '>', |
|
637 | + EEM_Datetime::instance()->current_time_for_query('DTT_EVT_start'), |
|
638 | + ); |
|
639 | + } |
|
640 | + $query_params[0] = $where_params; |
|
641 | + // don't use $query_params with count() |
|
642 | + // because we don't want to include additional query clauses like "GROUP BY" |
|
643 | + return $count |
|
644 | + ? $this->count(array($where_params), 'EVT_ID', true) |
|
645 | + : $this->get_all($query_params); |
|
646 | + } |
|
647 | + |
|
648 | + |
|
649 | + |
|
650 | + /** |
|
651 | + * Gets all events that are published |
|
652 | + * and have an event end time later than now |
|
653 | + * |
|
654 | + * @param array $query_params An array of query params to further filter on |
|
655 | + * (note that status and DTT_EVT_end will be overridden) |
|
656 | + * @param bool $count whether to return the count or not (default FALSE) |
|
657 | + * @return EE_Event[]|int |
|
658 | + * @throws \EE_Error |
|
659 | + */ |
|
660 | + public function get_active_and_upcoming_events($query_params, $count = false) |
|
661 | + { |
|
662 | + if (array_key_exists(0, $query_params)) { |
|
663 | + $where_params = $query_params[0]; |
|
664 | + unset($query_params[0]); |
|
665 | + } else { |
|
666 | + $where_params = array(); |
|
667 | + } |
|
668 | + // if we have count make sure we don't include group by |
|
669 | + if ($count && isset($query_params['group_by'])) { |
|
670 | + unset($query_params['group_by']); |
|
671 | + } |
|
672 | + // let's add specific query_params for active_events |
|
673 | + // keep in mind this will override any sent status in the query AND any date queries. |
|
674 | + $where_params['status'] = array('IN', array('publish', EEM_Event::sold_out)); |
|
675 | + // add where params for DTT_EVT_end |
|
676 | + if (isset($where_params['Datetime.DTT_EVT_end'])) { |
|
677 | + $where_params['Datetime.DTT_EVT_end*****'] = array( |
|
678 | + '>', |
|
679 | + EEM_Datetime::instance()->current_time_for_query('DTT_EVT_end'), |
|
680 | + ); |
|
681 | + } else { |
|
682 | + $where_params['Datetime.DTT_EVT_end'] = array( |
|
683 | + '>', |
|
684 | + EEM_Datetime::instance()->current_time_for_query('DTT_EVT_end'), |
|
685 | + ); |
|
686 | + } |
|
687 | + $query_params[0] = $where_params; |
|
688 | + // don't use $query_params with count() |
|
689 | + // because we don't want to include additional query clauses like "GROUP BY" |
|
690 | + return $count |
|
691 | + ? $this->count(array($where_params), 'EVT_ID', true) |
|
692 | + : $this->get_all($query_params); |
|
693 | + } |
|
694 | + |
|
695 | + |
|
696 | + |
|
697 | + /** |
|
698 | + * This only returns events that are expired. |
|
699 | + * They may still be published but all their datetimes have expired. |
|
700 | + * |
|
701 | + * @param array $query_params An array of query params to further filter on |
|
702 | + * (note that status and DTT_EVT_end will be overridden) |
|
703 | + * @param bool $count whether to return the count or not (default FALSE) |
|
704 | + * @return EE_Event[]|int |
|
705 | + * @throws \EE_Error |
|
706 | + */ |
|
707 | + public function get_expired_events($query_params, $count = false) |
|
708 | + { |
|
709 | + $where_params = isset($query_params[0]) ? $query_params[0] : array(); |
|
710 | + // if we have count make sure we don't include group by |
|
711 | + if ($count && isset($query_params['group_by'])) { |
|
712 | + unset($query_params['group_by']); |
|
713 | + } |
|
714 | + // let's add specific query_params for active_events |
|
715 | + // keep in mind this will override any sent status in the query AND any date queries. |
|
716 | + if (isset($where_params['status'])) { |
|
717 | + unset($where_params['status']); |
|
718 | + } |
|
719 | + $exclude_query = $query_params; |
|
720 | + if (isset($exclude_query[0])) { |
|
721 | + unset($exclude_query[0]); |
|
722 | + } |
|
723 | + $exclude_query[0] = array( |
|
724 | + 'Datetime.DTT_EVT_end' => array( |
|
725 | + '>', |
|
726 | + EEM_Datetime::instance()->current_time_for_query('DTT_EVT_end'), |
|
727 | + ), |
|
728 | + ); |
|
729 | + // first get all events that have datetimes where its not expired. |
|
730 | + $event_ids = $this->_get_all_wpdb_results($exclude_query, OBJECT_K, 'Event_CPT.ID'); |
|
731 | + $event_ids = array_keys($event_ids); |
|
732 | + // if we have any additional query_params, let's add them to the 'AND' condition |
|
733 | + $and_condition = array( |
|
734 | + 'Datetime.DTT_EVT_end' => array('<', EEM_Datetime::instance()->current_time_for_query('DTT_EVT_end')), |
|
735 | + 'EVT_ID' => array('NOT IN', $event_ids), |
|
736 | + ); |
|
737 | + if (isset($where_params['OR'])) { |
|
738 | + $and_condition['OR'] = $where_params['OR']; |
|
739 | + unset($where_params['OR']); |
|
740 | + } |
|
741 | + if (isset($where_params['Datetime.DTT_EVT_end'])) { |
|
742 | + $and_condition['Datetime.DTT_EVT_end****'] = $where_params['Datetime.DTT_EVT_end']; |
|
743 | + unset($where_params['Datetime.DTT_EVT_end']); |
|
744 | + } |
|
745 | + if (isset($where_params['Datetime.DTT_EVT_start'])) { |
|
746 | + $and_condition['Datetime.DTT_EVT_start'] = $where_params['Datetime.DTT_EVT_start']; |
|
747 | + unset($where_params['Datetime.DTT_EVT_start']); |
|
748 | + } |
|
749 | + // merge remaining $where params with the and conditions. |
|
750 | + $where_params['AND'] = array_merge($and_condition, $where_params); |
|
751 | + $query_params[0] = $where_params; |
|
752 | + // don't use $query_params with count() |
|
753 | + // because we don't want to include additional query clauses like "GROUP BY" |
|
754 | + return $count |
|
755 | + ? $this->count(array($where_params), 'EVT_ID', true) |
|
756 | + : $this->get_all($query_params); |
|
757 | + } |
|
758 | + |
|
759 | + |
|
760 | + |
|
761 | + /** |
|
762 | + * This basically just returns the events that do not have the publish status. |
|
763 | + * |
|
764 | + * @param array $query_params An array of query params to further filter on |
|
765 | + * (note that status will be overwritten) |
|
766 | + * @param boolean $count whether to return the count or not (default FALSE) |
|
767 | + * @return EE_Event[]|int |
|
768 | + * @throws \EE_Error |
|
769 | + */ |
|
770 | + public function get_inactive_events($query_params, $count = false) |
|
771 | + { |
|
772 | + $where_params = isset($query_params[0]) ? $query_params[0] : array(); |
|
773 | + // let's add in specific query_params for inactive events. |
|
774 | + if (isset($where_params['status'])) { |
|
775 | + unset($where_params['status']); |
|
776 | + } |
|
777 | + // if we have count make sure we don't include group by |
|
778 | + if ($count && isset($query_params['group_by'])) { |
|
779 | + unset($query_params['group_by']); |
|
780 | + } |
|
781 | + // if we have any additional query_params, let's add them to the 'AND' condition |
|
782 | + $where_params['AND']['status'] = array('!=', 'publish'); |
|
783 | + if (isset($where_params['OR'])) { |
|
784 | + $where_params['AND']['OR'] = $where_params['OR']; |
|
785 | + unset($where_params['OR']); |
|
786 | + } |
|
787 | + if (isset($where_params['Datetime.DTT_EVT_end'])) { |
|
788 | + $where_params['AND']['Datetime.DTT_EVT_end****'] = $where_params['Datetime.DTT_EVT_end']; |
|
789 | + unset($where_params['Datetime.DTT_EVT_end']); |
|
790 | + } |
|
791 | + if (isset($where_params['Datetime.DTT_EVT_start'])) { |
|
792 | + $where_params['AND']['Datetime.DTT_EVT_start'] = $where_params['Datetime.DTT_EVT_start']; |
|
793 | + unset($where_params['Datetime.DTT_EVT_start']); |
|
794 | + } |
|
795 | + $query_params[0] = $where_params; |
|
796 | + // don't use $query_params with count() |
|
797 | + // because we don't want to include additional query clauses like "GROUP BY" |
|
798 | + return $count |
|
799 | + ? $this->count(array($where_params), 'EVT_ID', true) |
|
800 | + : $this->get_all($query_params); |
|
801 | + } |
|
802 | + |
|
803 | + |
|
804 | + |
|
805 | + /** |
|
806 | + * This is just injecting into the parent add_relationship_to so we do special handling on price relationships |
|
807 | + * because we don't want to override any existing global default prices but instead insert NEW prices that get |
|
808 | + * attached to the event. See parent for param descriptions |
|
809 | + * |
|
810 | + * @param $id_or_obj |
|
811 | + * @param $other_model_id_or_obj |
|
812 | + * @param string $relationName |
|
813 | + * @param array $where_query |
|
814 | + * @return EE_Base_Class |
|
815 | + * @throws EE_Error |
|
816 | + */ |
|
817 | + public function add_relationship_to($id_or_obj, $other_model_id_or_obj, $relationName, $where_query = array()) |
|
818 | + { |
|
819 | + if ($relationName === 'Price') { |
|
820 | + // let's get the PRC object for the given ID to make sure that we aren't dealing with a default |
|
821 | + $prc_chk = $this->get_related_model_obj($relationName)->ensure_is_obj($other_model_id_or_obj); |
|
822 | + // if EVT_ID = 0, then this is a default |
|
823 | + if ((int) $prc_chk->get('EVT_ID') === 0) { |
|
824 | + // let's set the prc_id as 0 so we force an insert on the add_relation_to carried out by relation |
|
825 | + $prc_chk->set('PRC_ID', 0); |
|
826 | + } |
|
827 | + // run parent |
|
828 | + return parent::add_relationship_to($id_or_obj, $prc_chk, $relationName, $where_query); |
|
829 | + } |
|
830 | + // otherwise carry on as normal |
|
831 | + return parent::add_relationship_to($id_or_obj, $other_model_id_or_obj, $relationName, $where_query); |
|
832 | + } |
|
833 | + |
|
834 | + |
|
835 | + |
|
836 | + /******************** DEPRECATED METHODS ********************/ |
|
837 | + |
|
838 | + |
|
839 | + |
|
840 | + /** |
|
841 | + * _get_question_target_db_column |
|
842 | + * |
|
843 | + * @deprecated as of 4.8.32.rc.001. Instead consider using |
|
844 | + * EE_Registration_Custom_Questions_Form located in |
|
845 | + * admin_pages/registrations/form_sections/EE_Registration_Custom_Questions_Form.form.php |
|
846 | + * @access public |
|
847 | + * @param EE_Registration $registration (so existing answers for registration are included) |
|
848 | + * @param int $EVT_ID so all question groups are included for event (not just answers from |
|
849 | + * registration). |
|
850 | + * @throws EE_Error |
|
851 | + * @return array |
|
852 | + */ |
|
853 | + public function assemble_array_of_groups_questions_and_options(EE_Registration $registration, $EVT_ID = 0) |
|
854 | + { |
|
855 | + if (empty($EVT_ID)) { |
|
856 | + throw new EE_Error(__( |
|
857 | + 'An error occurred. No EVT_ID is included. Needed to know which question groups to retrieve.', |
|
858 | + 'event_espresso' |
|
859 | + )); |
|
860 | + } |
|
861 | + $questions = array(); |
|
862 | + // get all question groups for event |
|
863 | + $qgs = $this->get_question_groups_for_event($EVT_ID, $registration); |
|
864 | + if (! empty($qgs)) { |
|
865 | + foreach ($qgs as $qg) { |
|
866 | + $qsts = $qg->questions(); |
|
867 | + $questions[ $qg->ID() ] = $qg->model_field_array(); |
|
868 | + $questions[ $qg->ID() ]['QSG_questions'] = array(); |
|
869 | + foreach ($qsts as $qst) { |
|
870 | + if ($qst->is_system_question()) { |
|
871 | + continue; |
|
872 | + } |
|
873 | + $answer = EEM_Answer::instance()->get_one(array( |
|
874 | + array( |
|
875 | + 'QST_ID' => $qst->ID(), |
|
876 | + 'REG_ID' => $registration->ID(), |
|
877 | + ), |
|
878 | + )); |
|
879 | + $answer = $answer instanceof EE_Answer ? $answer : EEM_Answer::instance()->create_default_object(); |
|
880 | + $qst_name = $qstn_id = $qst->ID(); |
|
881 | + $ans_id = $answer->ID(); |
|
882 | + $qst_name = ! empty($ans_id) ? '[' . $qst_name . '][' . $ans_id . ']' : '[' . $qst_name . ']'; |
|
883 | + $input_name = ''; |
|
884 | + $input_id = sanitize_key($qst->display_text()); |
|
885 | + $input_class = ''; |
|
886 | + $questions[ $qg->ID() ]['QSG_questions'][ $qst->ID() ] = $qst->model_field_array(); |
|
887 | + $questions[ $qg->ID() ]['QSG_questions'][ $qst->ID() ]['QST_input_name'] = 'qstn' |
|
888 | + . $input_name |
|
889 | + . $qst_name; |
|
890 | + $questions[ $qg->ID() ]['QSG_questions'][ $qst->ID() ]['QST_input_id'] = $input_id . '-' . $qstn_id; |
|
891 | + $questions[ $qg->ID() ]['QSG_questions'][ $qst->ID() ]['QST_input_class'] = $input_class; |
|
892 | + $questions[ $qg->ID() ]['QSG_questions'][ $qst->ID() ]['QST_options'] = array(); |
|
893 | + $questions[ $qg->ID() ]['QSG_questions'][ $qst->ID() ]['qst_obj'] = $qst; |
|
894 | + $questions[ $qg->ID() ]['QSG_questions'][ $qst->ID() ]['ans_obj'] = $answer; |
|
895 | + // leave responses as-is, don't convert stuff into html entities please! |
|
896 | + $questions[ $qg->ID() ]['QSG_questions'][ $qst->ID() ]['htmlentities'] = false; |
|
897 | + if ($qst->type() == 'RADIO_BTN' || $qst->type() == 'CHECKBOX' || $qst->type() == 'DROPDOWN') { |
|
898 | + $QSOs = $qst->options(true, $answer->value()); |
|
899 | + if (is_array($QSOs)) { |
|
900 | + foreach ($QSOs as $QSO_ID => $QSO) { |
|
901 | + $questions[ $qg->ID() ]['QSG_questions'][ $qst->ID() ]['QST_options'][ $QSO_ID ] = $QSO->model_field_array(); |
|
902 | + } |
|
903 | + } |
|
904 | + } |
|
905 | + } |
|
906 | + } |
|
907 | + } |
|
908 | + return $questions; |
|
909 | + } |
|
910 | + |
|
911 | + |
|
912 | + /** |
|
913 | + * @param mixed $cols_n_values either an array of where each key is the name of a field, and the value is its value |
|
914 | + * or an stdClass where each property is the name of a column, |
|
915 | + * @return EE_Base_Class |
|
916 | + * @throws \EE_Error |
|
917 | + */ |
|
918 | + public function instantiate_class_from_array_or_object($cols_n_values) |
|
919 | + { |
|
920 | + $classInstance = parent::instantiate_class_from_array_or_object($cols_n_values); |
|
921 | + if ($classInstance instanceof EE_Event) { |
|
922 | + // events have their timezone defined in the DB, so use it immediately |
|
923 | + $this->set_timezone($classInstance->get_timezone()); |
|
924 | + } |
|
925 | + return $classInstance; |
|
926 | + } |
|
927 | 927 | } |
@@ -16,15 +16,13 @@ discard block |
||
16 | 16 | 'EQG_primary'=>new EE_Boolean_Field('EQG_primary', __('Flag indicating question is only for primary attendees','event_espresso'), false, false) |
17 | 17 | ) |
18 | 18 | ); |
19 | - |
|
20 | - |
|
21 | 19 | * |
22 | 20 | */ |
23 | 21 | class EE_DMS_4_1_0_event_question_group extends EE_Data_Migration_Script_Stage_Table |
24 | 22 | { |
25 | - private $_new_table; |
|
26 | - public function _migrate_old_row($old_row) |
|
27 | - { |
|
23 | + private $_new_table; |
|
24 | + public function _migrate_old_row($old_row) |
|
25 | + { |
|
28 | 26 | // $txn_id = $this->get_migration_script()->get_mapping_new_pk($this->_old_table, $old_row['id'], $this->_new_transaction_table); |
29 | 27 | // if ( ! $txn_id ){ |
30 | 28 | // $this->add_error(sprintf(__("Could not find the transaction for the 3.1 attendee %d from row %s", "event_espresso"),$old_row['id'],$this->_json_encode($old_row))); |
@@ -34,8 +32,8 @@ discard block |
||
34 | 32 | // $new_line_items = $this->_insert_new_line_items($txn,$old_row); |
35 | 33 | // $this->get_migration_script()->set_mapping($this->_old_table,$old_row['id'],$this->_new_line_table,$new_line_items); |
36 | 34 | |
37 | - $this->_insert_new_event_question_groups($old_row); |
|
38 | - } |
|
35 | + $this->_insert_new_event_question_groups($old_row); |
|
36 | + } |
|
39 | 37 | // function _migration_step($num_items=50){ |
40 | 38 | // global $wpdb; |
41 | 39 | // $start_at_record = $this->count_records_migrated(); |
@@ -55,109 +53,109 @@ discard block |
||
55 | 53 | // $count = $wpdb->get_var("SELECT COUNT(id) FROM ".$this->_old_table); |
56 | 54 | // return $count; |
57 | 55 | // } |
58 | - public function __construct() |
|
59 | - { |
|
60 | - global $wpdb; |
|
61 | - $this->_old_table = $wpdb->prefix."events_detail"; |
|
62 | - $this->_extra_where_sql = 'WHERE event_status!="D"'; |
|
63 | - $this->_new_table = $wpdb->prefix."esp_event_question_group"; |
|
64 | - $this->_pretty_name = __("Question Groups in each Event", "event_espresso"); |
|
65 | - parent::__construct(); |
|
66 | - } |
|
56 | + public function __construct() |
|
57 | + { |
|
58 | + global $wpdb; |
|
59 | + $this->_old_table = $wpdb->prefix."events_detail"; |
|
60 | + $this->_extra_where_sql = 'WHERE event_status!="D"'; |
|
61 | + $this->_new_table = $wpdb->prefix."esp_event_question_group"; |
|
62 | + $this->_pretty_name = __("Question Groups in each Event", "event_espresso"); |
|
63 | + parent::__construct(); |
|
64 | + } |
|
67 | 65 | |
68 | - /** |
|
69 | - * Attempts to insert a new question group inthe new format given an old one |
|
70 | - * @global type $wpdb |
|
71 | - * @param array $old_event |
|
72 | - * @return void |
|
73 | - */ |
|
74 | - private function _insert_new_event_question_groups($old_event) |
|
75 | - { |
|
76 | - $new_event_question_group_ids = array(); |
|
77 | - $question_groups_for_primary = maybe_unserialize($old_event['question_groups']); |
|
78 | - if (is_array($question_groups_for_primary)) { |
|
79 | - foreach ($question_groups_for_primary as $old_question_group_id) { |
|
80 | - $new_id = $this->_insert_event_question_group($old_event, $old_question_group_id, true); |
|
81 | - if ($new_id) { |
|
82 | - $new_event_question_group_ids[] = $new_id; |
|
83 | - } |
|
84 | - } |
|
85 | - } |
|
86 | - $event_meta = maybe_unserialize($old_event['event_meta']); |
|
87 | - if (isset($event_meta['add_attendee_question_groups'])) { |
|
88 | - if (is_array($event_meta['add_attendee_question_groups'])) { |
|
89 | - foreach ($event_meta['add_attendee_question_groups'] as $old_question_group_id) { |
|
90 | - $new_id = $this->_insert_event_question_group($old_event, $old_question_group_id, false); |
|
91 | - if ($new_id) { |
|
92 | - $new_event_question_group_ids[] = $new_id; |
|
93 | - } |
|
94 | - } |
|
95 | - } |
|
96 | - } |
|
66 | + /** |
|
67 | + * Attempts to insert a new question group inthe new format given an old one |
|
68 | + * @global type $wpdb |
|
69 | + * @param array $old_event |
|
70 | + * @return void |
|
71 | + */ |
|
72 | + private function _insert_new_event_question_groups($old_event) |
|
73 | + { |
|
74 | + $new_event_question_group_ids = array(); |
|
75 | + $question_groups_for_primary = maybe_unserialize($old_event['question_groups']); |
|
76 | + if (is_array($question_groups_for_primary)) { |
|
77 | + foreach ($question_groups_for_primary as $old_question_group_id) { |
|
78 | + $new_id = $this->_insert_event_question_group($old_event, $old_question_group_id, true); |
|
79 | + if ($new_id) { |
|
80 | + $new_event_question_group_ids[] = $new_id; |
|
81 | + } |
|
82 | + } |
|
83 | + } |
|
84 | + $event_meta = maybe_unserialize($old_event['event_meta']); |
|
85 | + if (isset($event_meta['add_attendee_question_groups'])) { |
|
86 | + if (is_array($event_meta['add_attendee_question_groups'])) { |
|
87 | + foreach ($event_meta['add_attendee_question_groups'] as $old_question_group_id) { |
|
88 | + $new_id = $this->_insert_event_question_group($old_event, $old_question_group_id, false); |
|
89 | + if ($new_id) { |
|
90 | + $new_event_question_group_ids[] = $new_id; |
|
91 | + } |
|
92 | + } |
|
93 | + } |
|
94 | + } |
|
97 | 95 | |
98 | 96 | |
99 | - $this->get_migration_script()->set_mapping($this->_old_table, $old_event['id'], $this->_new_table, $new_event_question_group_ids); |
|
100 | - } |
|
97 | + $this->get_migration_script()->set_mapping($this->_old_table, $old_event['id'], $this->_new_table, $new_event_question_group_ids); |
|
98 | + } |
|
101 | 99 | |
102 | - private function _insert_event_question_group($old_event, $old_question_group_id, $primary) |
|
103 | - { |
|
104 | - global $wpdb; |
|
105 | - $new_question_group_id = $this->get_migration_script()->get_mapping_new_pk( |
|
106 | - $wpdb->prefix . "events_qst_group", |
|
107 | - intval($old_question_group_id), |
|
108 | - $wpdb->prefix . "esp_question_group" |
|
109 | - ); |
|
100 | + private function _insert_event_question_group($old_event, $old_question_group_id, $primary) |
|
101 | + { |
|
102 | + global $wpdb; |
|
103 | + $new_question_group_id = $this->get_migration_script()->get_mapping_new_pk( |
|
104 | + $wpdb->prefix . "events_qst_group", |
|
105 | + intval($old_question_group_id), |
|
106 | + $wpdb->prefix . "esp_question_group" |
|
107 | + ); |
|
110 | 108 | |
111 | - if (! $new_question_group_id) { |
|
112 | - $this->add_error( |
|
113 | - sprintf( |
|
114 | - // translators: %s question ID, %s event ID |
|
115 | - __("Could not find 4.1 question ID for 3.1 question id #%s on event $%s", "event_espresso"), |
|
116 | - $old_question_group_id, |
|
117 | - $old_event['id'] |
|
118 | - ) |
|
119 | - ); |
|
120 | - return 0; |
|
121 | - } |
|
122 | - $new_event_id = $this->get_migration_script()->get_mapping_new_pk( |
|
123 | - $wpdb->prefix . "events_detail", |
|
124 | - intval($old_event['id']), |
|
125 | - $wpdb->posts |
|
126 | - ); |
|
127 | - if (! $new_question_group_id) { |
|
128 | - $this->add_error( |
|
129 | - sprintf( |
|
130 | - // translators: %s event ID |
|
131 | - __("Could not find 4.1 event 3.1 event id #%s", "event_espresso"), |
|
132 | - $old_event['id'] |
|
133 | - ) |
|
134 | - ); |
|
135 | - return 0; |
|
136 | - } |
|
137 | - $cols_n_values = array( |
|
138 | - 'EVT_ID'=>$new_event_id, |
|
139 | - 'QSG_ID'=>$new_question_group_id, |
|
140 | - 'EQG_primary' => $primary |
|
141 | - ); |
|
109 | + if (! $new_question_group_id) { |
|
110 | + $this->add_error( |
|
111 | + sprintf( |
|
112 | + // translators: %s question ID, %s event ID |
|
113 | + __("Could not find 4.1 question ID for 3.1 question id #%s on event $%s", "event_espresso"), |
|
114 | + $old_question_group_id, |
|
115 | + $old_event['id'] |
|
116 | + ) |
|
117 | + ); |
|
118 | + return 0; |
|
119 | + } |
|
120 | + $new_event_id = $this->get_migration_script()->get_mapping_new_pk( |
|
121 | + $wpdb->prefix . "events_detail", |
|
122 | + intval($old_event['id']), |
|
123 | + $wpdb->posts |
|
124 | + ); |
|
125 | + if (! $new_question_group_id) { |
|
126 | + $this->add_error( |
|
127 | + sprintf( |
|
128 | + // translators: %s event ID |
|
129 | + __("Could not find 4.1 event 3.1 event id #%s", "event_espresso"), |
|
130 | + $old_event['id'] |
|
131 | + ) |
|
132 | + ); |
|
133 | + return 0; |
|
134 | + } |
|
135 | + $cols_n_values = array( |
|
136 | + 'EVT_ID'=>$new_event_id, |
|
137 | + 'QSG_ID'=>$new_question_group_id, |
|
138 | + 'EQG_primary' => $primary |
|
139 | + ); |
|
142 | 140 | |
143 | - $datatypes = array( |
|
144 | - '%d',// EVT_ID |
|
145 | - '%d',// QSG_ID |
|
146 | - '%d',// EQG_primary |
|
147 | - ); |
|
148 | - $success = $wpdb->insert($this->_new_table, $cols_n_values, $datatypes); |
|
149 | - if (! $success) { |
|
150 | - $this->add_error( |
|
151 | - $this->get_migration_script()->_create_error_message_for_db_insertion( |
|
152 | - $this->_old_table, |
|
153 | - $old_event, |
|
154 | - $this->_new_table, |
|
155 | - $cols_n_values, |
|
156 | - $datatypes |
|
157 | - ) |
|
158 | - ); |
|
159 | - return 0; |
|
160 | - } |
|
161 | - return $wpdb->insert_id; |
|
162 | - } |
|
141 | + $datatypes = array( |
|
142 | + '%d',// EVT_ID |
|
143 | + '%d',// QSG_ID |
|
144 | + '%d',// EQG_primary |
|
145 | + ); |
|
146 | + $success = $wpdb->insert($this->_new_table, $cols_n_values, $datatypes); |
|
147 | + if (! $success) { |
|
148 | + $this->add_error( |
|
149 | + $this->get_migration_script()->_create_error_message_for_db_insertion( |
|
150 | + $this->_old_table, |
|
151 | + $old_event, |
|
152 | + $this->_new_table, |
|
153 | + $cols_n_values, |
|
154 | + $datatypes |
|
155 | + ) |
|
156 | + ); |
|
157 | + return 0; |
|
158 | + } |
|
159 | + return $wpdb->insert_id; |
|
160 | + } |
|
163 | 161 | } |
@@ -103,12 +103,12 @@ discard block |
||
103 | 103 | { |
104 | 104 | global $wpdb; |
105 | 105 | $new_question_group_id = $this->get_migration_script()->get_mapping_new_pk( |
106 | - $wpdb->prefix . "events_qst_group", |
|
106 | + $wpdb->prefix."events_qst_group", |
|
107 | 107 | intval($old_question_group_id), |
108 | - $wpdb->prefix . "esp_question_group" |
|
108 | + $wpdb->prefix."esp_question_group" |
|
109 | 109 | ); |
110 | 110 | |
111 | - if (! $new_question_group_id) { |
|
111 | + if ( ! $new_question_group_id) { |
|
112 | 112 | $this->add_error( |
113 | 113 | sprintf( |
114 | 114 | // translators: %s question ID, %s event ID |
@@ -120,11 +120,11 @@ discard block |
||
120 | 120 | return 0; |
121 | 121 | } |
122 | 122 | $new_event_id = $this->get_migration_script()->get_mapping_new_pk( |
123 | - $wpdb->prefix . "events_detail", |
|
123 | + $wpdb->prefix."events_detail", |
|
124 | 124 | intval($old_event['id']), |
125 | 125 | $wpdb->posts |
126 | 126 | ); |
127 | - if (! $new_question_group_id) { |
|
127 | + if ( ! $new_question_group_id) { |
|
128 | 128 | $this->add_error( |
129 | 129 | sprintf( |
130 | 130 | // translators: %s event ID |
@@ -141,12 +141,12 @@ discard block |
||
141 | 141 | ); |
142 | 142 | |
143 | 143 | $datatypes = array( |
144 | - '%d',// EVT_ID |
|
145 | - '%d',// QSG_ID |
|
146 | - '%d',// EQG_primary |
|
144 | + '%d', // EVT_ID |
|
145 | + '%d', // QSG_ID |
|
146 | + '%d', // EQG_primary |
|
147 | 147 | ); |
148 | 148 | $success = $wpdb->insert($this->_new_table, $cols_n_values, $datatypes); |
149 | - if (! $success) { |
|
149 | + if ( ! $success) { |
|
150 | 150 | $this->add_error( |
151 | 151 | $this->get_migration_script()->_create_error_message_for_db_insertion( |
152 | 152 | $this->_old_table, |