@@ -23,336 +23,336 @@ |
||
23 | 23 | class EED_Batch extends EED_Module |
24 | 24 | { |
25 | 25 | |
26 | - /** |
|
27 | - * Possibly value for $_REQUEST[ 'batch' ]. Indicates to run a job that |
|
28 | - * processes data only |
|
29 | - */ |
|
30 | - const batch_job = 'job'; |
|
31 | - /** |
|
32 | - * Possibly value for $_REQUEST[ 'batch' ]. Indicates to run a job that |
|
33 | - * produces a file for download |
|
34 | - */ |
|
35 | - const batch_file_job = 'file'; |
|
36 | - /** |
|
37 | - * Possibly value for $_REQUEST[ 'batch' ]. Indicates this request is NOT |
|
38 | - * for a batch job. It's the same as not providing the $_REQUEST[ 'batch' ] |
|
39 | - * at all |
|
40 | - */ |
|
41 | - const batch_not_job = 'none'; |
|
26 | + /** |
|
27 | + * Possibly value for $_REQUEST[ 'batch' ]. Indicates to run a job that |
|
28 | + * processes data only |
|
29 | + */ |
|
30 | + const batch_job = 'job'; |
|
31 | + /** |
|
32 | + * Possibly value for $_REQUEST[ 'batch' ]. Indicates to run a job that |
|
33 | + * produces a file for download |
|
34 | + */ |
|
35 | + const batch_file_job = 'file'; |
|
36 | + /** |
|
37 | + * Possibly value for $_REQUEST[ 'batch' ]. Indicates this request is NOT |
|
38 | + * for a batch job. It's the same as not providing the $_REQUEST[ 'batch' ] |
|
39 | + * at all |
|
40 | + */ |
|
41 | + const batch_not_job = 'none'; |
|
42 | 42 | |
43 | - /** |
|
44 | - * |
|
45 | - * @var string 'file', or 'job', or false to indicate its not a batch request at all |
|
46 | - */ |
|
47 | - protected $_batch_request_type = null; |
|
43 | + /** |
|
44 | + * |
|
45 | + * @var string 'file', or 'job', or false to indicate its not a batch request at all |
|
46 | + */ |
|
47 | + protected $_batch_request_type = null; |
|
48 | 48 | |
49 | - /** |
|
50 | - * Because we want to use the response in both the localized JS and in the body |
|
51 | - * we need to make this response available between method calls |
|
52 | - * |
|
53 | - * @var \EventEspressoBatchRequest\Helpers\JobStepResponse |
|
54 | - */ |
|
55 | - protected $_job_step_response = null; |
|
49 | + /** |
|
50 | + * Because we want to use the response in both the localized JS and in the body |
|
51 | + * we need to make this response available between method calls |
|
52 | + * |
|
53 | + * @var \EventEspressoBatchRequest\Helpers\JobStepResponse |
|
54 | + */ |
|
55 | + protected $_job_step_response = null; |
|
56 | 56 | |
57 | - /** |
|
58 | - * Gets the batch instance |
|
59 | - * |
|
60 | - * @return EED_Batch |
|
61 | - */ |
|
62 | - public static function instance() |
|
63 | - { |
|
64 | - return self::get_instance(); |
|
65 | - } |
|
57 | + /** |
|
58 | + * Gets the batch instance |
|
59 | + * |
|
60 | + * @return EED_Batch |
|
61 | + */ |
|
62 | + public static function instance() |
|
63 | + { |
|
64 | + return self::get_instance(); |
|
65 | + } |
|
66 | 66 | |
67 | - /** |
|
68 | - * Sets hooks to enable batch jobs on the frontend. Disabled by default |
|
69 | - * because it's an attack vector and there are currently no implementations |
|
70 | - */ |
|
71 | - public static function set_hooks() |
|
72 | - { |
|
73 | - // because this is a possibel attack vector, let's have this disabled until |
|
74 | - // we at least have a real use for it on the frontend |
|
75 | - if (apply_filters('FHEE__EED_Batch__set_hooks__enable_frontend_batch', false)) { |
|
76 | - add_action('wp_enqueue_scripts', array(self::instance(), 'enqueue_scripts')); |
|
77 | - add_filter('template_include', array(self::instance(), 'override_template'), 99); |
|
78 | - } |
|
79 | - } |
|
67 | + /** |
|
68 | + * Sets hooks to enable batch jobs on the frontend. Disabled by default |
|
69 | + * because it's an attack vector and there are currently no implementations |
|
70 | + */ |
|
71 | + public static function set_hooks() |
|
72 | + { |
|
73 | + // because this is a possibel attack vector, let's have this disabled until |
|
74 | + // we at least have a real use for it on the frontend |
|
75 | + if (apply_filters('FHEE__EED_Batch__set_hooks__enable_frontend_batch', false)) { |
|
76 | + add_action('wp_enqueue_scripts', array(self::instance(), 'enqueue_scripts')); |
|
77 | + add_filter('template_include', array(self::instance(), 'override_template'), 99); |
|
78 | + } |
|
79 | + } |
|
80 | 80 | |
81 | - /** |
|
82 | - * Initializes some hooks for the admin in order to run batch jobs |
|
83 | - */ |
|
84 | - public static function set_hooks_admin() |
|
85 | - { |
|
86 | - add_action('admin_menu', array(self::instance(), 'register_admin_pages')); |
|
87 | - add_action('admin_enqueue_scripts', array(self::instance(), 'enqueue_scripts')); |
|
81 | + /** |
|
82 | + * Initializes some hooks for the admin in order to run batch jobs |
|
83 | + */ |
|
84 | + public static function set_hooks_admin() |
|
85 | + { |
|
86 | + add_action('admin_menu', array(self::instance(), 'register_admin_pages')); |
|
87 | + add_action('admin_enqueue_scripts', array(self::instance(), 'enqueue_scripts')); |
|
88 | 88 | |
89 | - // ajax |
|
90 | - add_action('wp_ajax_espresso_batch_continue', array(self::instance(), 'batch_continue')); |
|
91 | - add_action('wp_ajax_espresso_batch_cleanup', array(self::instance(), 'batch_cleanup')); |
|
92 | - add_action('wp_ajax_nopriv_espresso_batch_continue', array(self::instance(), 'batch_continue')); |
|
93 | - add_action('wp_ajax_nopriv_espresso_batch_cleanup', array(self::instance(), 'batch_cleanup')); |
|
94 | - } |
|
89 | + // ajax |
|
90 | + add_action('wp_ajax_espresso_batch_continue', array(self::instance(), 'batch_continue')); |
|
91 | + add_action('wp_ajax_espresso_batch_cleanup', array(self::instance(), 'batch_cleanup')); |
|
92 | + add_action('wp_ajax_nopriv_espresso_batch_continue', array(self::instance(), 'batch_continue')); |
|
93 | + add_action('wp_ajax_nopriv_espresso_batch_cleanup', array(self::instance(), 'batch_cleanup')); |
|
94 | + } |
|
95 | 95 | |
96 | - /** |
|
97 | - * Enqueues batch scripts on the frontend or admin, and creates a job |
|
98 | - */ |
|
99 | - public function enqueue_scripts() |
|
100 | - { |
|
101 | - if (isset($_REQUEST['espresso_batch']) |
|
102 | - || |
|
103 | - ( |
|
104 | - isset($_REQUEST['page']) |
|
105 | - && $_REQUEST['page'] == 'espresso_batch' |
|
106 | - ) |
|
107 | - ) { |
|
108 | - switch ($this->batch_request_type()) { |
|
109 | - case self::batch_job: |
|
110 | - $this->enqueue_scripts_styles_batch_create(); |
|
111 | - break; |
|
112 | - case self::batch_file_job: |
|
113 | - $this->enqueue_scripts_styles_batch_file_create(); |
|
114 | - break; |
|
115 | - } |
|
116 | - } |
|
117 | - } |
|
96 | + /** |
|
97 | + * Enqueues batch scripts on the frontend or admin, and creates a job |
|
98 | + */ |
|
99 | + public function enqueue_scripts() |
|
100 | + { |
|
101 | + if (isset($_REQUEST['espresso_batch']) |
|
102 | + || |
|
103 | + ( |
|
104 | + isset($_REQUEST['page']) |
|
105 | + && $_REQUEST['page'] == 'espresso_batch' |
|
106 | + ) |
|
107 | + ) { |
|
108 | + switch ($this->batch_request_type()) { |
|
109 | + case self::batch_job: |
|
110 | + $this->enqueue_scripts_styles_batch_create(); |
|
111 | + break; |
|
112 | + case self::batch_file_job: |
|
113 | + $this->enqueue_scripts_styles_batch_file_create(); |
|
114 | + break; |
|
115 | + } |
|
116 | + } |
|
117 | + } |
|
118 | 118 | |
119 | - /** |
|
120 | - * Create a batch job, enqueues a script to run it, and localizes some data for it |
|
121 | - */ |
|
122 | - public function enqueue_scripts_styles_batch_create() |
|
123 | - { |
|
124 | - $job_response = $this->_enqueue_batch_job_scripts_and_styles_and_start_job(); |
|
125 | - wp_enqueue_script( |
|
126 | - 'batch_runner_init', |
|
127 | - BATCH_URL . 'assets/batch_runner_init.js', |
|
128 | - array('batch_runner'), |
|
129 | - EVENT_ESPRESSO_VERSION, |
|
130 | - true |
|
131 | - ); |
|
132 | - wp_localize_script('batch_runner_init', 'ee_job_response', $job_response->to_array()); |
|
133 | - wp_localize_script( |
|
134 | - 'batch_runner_init', |
|
135 | - 'ee_job_i18n', |
|
136 | - array( |
|
137 | - 'return_url' => $_REQUEST['return_url'], |
|
138 | - ) |
|
139 | - ); |
|
140 | - } |
|
119 | + /** |
|
120 | + * Create a batch job, enqueues a script to run it, and localizes some data for it |
|
121 | + */ |
|
122 | + public function enqueue_scripts_styles_batch_create() |
|
123 | + { |
|
124 | + $job_response = $this->_enqueue_batch_job_scripts_and_styles_and_start_job(); |
|
125 | + wp_enqueue_script( |
|
126 | + 'batch_runner_init', |
|
127 | + BATCH_URL . 'assets/batch_runner_init.js', |
|
128 | + array('batch_runner'), |
|
129 | + EVENT_ESPRESSO_VERSION, |
|
130 | + true |
|
131 | + ); |
|
132 | + wp_localize_script('batch_runner_init', 'ee_job_response', $job_response->to_array()); |
|
133 | + wp_localize_script( |
|
134 | + 'batch_runner_init', |
|
135 | + 'ee_job_i18n', |
|
136 | + array( |
|
137 | + 'return_url' => $_REQUEST['return_url'], |
|
138 | + ) |
|
139 | + ); |
|
140 | + } |
|
141 | 141 | |
142 | - /** |
|
143 | - * Creates a batch job which will download a file, enqueues a script to run the job, and localizes some data for it |
|
144 | - */ |
|
145 | - public function enqueue_scripts_styles_batch_file_create() |
|
146 | - { |
|
147 | - // creates a job based on the request variable |
|
148 | - $job_response = $this->_enqueue_batch_job_scripts_and_styles_and_start_job(); |
|
149 | - wp_enqueue_script( |
|
150 | - 'batch_file_runner_init', |
|
151 | - BATCH_URL . 'assets/batch_file_runner_init.js', |
|
152 | - array('batch_runner'), |
|
153 | - EVENT_ESPRESSO_VERSION, |
|
154 | - true |
|
155 | - ); |
|
156 | - wp_localize_script('batch_file_runner_init', 'ee_job_response', $job_response->to_array()); |
|
157 | - wp_localize_script( |
|
158 | - 'batch_file_runner_init', |
|
159 | - 'ee_job_i18n', |
|
160 | - array( |
|
161 | - 'download_and_redirecting' => sprintf( |
|
162 | - __('File Generation complete. Downloading, and %1$sredirecting%2$s...', 'event_espresso'), |
|
163 | - '<a href="' . $_REQUEST['return_url'] . '">', |
|
164 | - '</a>' |
|
165 | - ), |
|
166 | - 'return_url' => $_REQUEST['return_url'], |
|
167 | - ) |
|
168 | - ); |
|
169 | - } |
|
142 | + /** |
|
143 | + * Creates a batch job which will download a file, enqueues a script to run the job, and localizes some data for it |
|
144 | + */ |
|
145 | + public function enqueue_scripts_styles_batch_file_create() |
|
146 | + { |
|
147 | + // creates a job based on the request variable |
|
148 | + $job_response = $this->_enqueue_batch_job_scripts_and_styles_and_start_job(); |
|
149 | + wp_enqueue_script( |
|
150 | + 'batch_file_runner_init', |
|
151 | + BATCH_URL . 'assets/batch_file_runner_init.js', |
|
152 | + array('batch_runner'), |
|
153 | + EVENT_ESPRESSO_VERSION, |
|
154 | + true |
|
155 | + ); |
|
156 | + wp_localize_script('batch_file_runner_init', 'ee_job_response', $job_response->to_array()); |
|
157 | + wp_localize_script( |
|
158 | + 'batch_file_runner_init', |
|
159 | + 'ee_job_i18n', |
|
160 | + array( |
|
161 | + 'download_and_redirecting' => sprintf( |
|
162 | + __('File Generation complete. Downloading, and %1$sredirecting%2$s...', 'event_espresso'), |
|
163 | + '<a href="' . $_REQUEST['return_url'] . '">', |
|
164 | + '</a>' |
|
165 | + ), |
|
166 | + 'return_url' => $_REQUEST['return_url'], |
|
167 | + ) |
|
168 | + ); |
|
169 | + } |
|
170 | 170 | |
171 | - /** |
|
172 | - * Enqueues scripts and styles common to any batch job, and creates |
|
173 | - * a job from the request data, and stores the response in the |
|
174 | - * $this->_job_step_response property |
|
175 | - * |
|
176 | - * @return \EventEspressoBatchRequest\Helpers\JobStepResponse |
|
177 | - */ |
|
178 | - protected function _enqueue_batch_job_scripts_and_styles_and_start_job() |
|
179 | - { |
|
180 | - wp_register_script( |
|
181 | - 'progress_bar', |
|
182 | - EE_PLUGIN_DIR_URL . 'core/libraries/batch/Assets/progress_bar.js', |
|
183 | - array('jquery') |
|
184 | - ); |
|
185 | - wp_enqueue_style( |
|
186 | - 'progress_bar', |
|
187 | - EE_PLUGIN_DIR_URL . 'core/libraries/batch/Assets/progress_bar.css', |
|
188 | - array(), |
|
189 | - EVENT_ESPRESSO_VERSION |
|
190 | - ); |
|
191 | - wp_enqueue_script( |
|
192 | - 'batch_runner', |
|
193 | - EE_PLUGIN_DIR_URL . 'core/libraries/batch/Assets/batch_runner.js', |
|
194 | - array('progress_bar') |
|
195 | - ); |
|
196 | - // just copy the bits of EE admin's eei18n that we need in the JS |
|
197 | - wp_localize_script( |
|
198 | - 'batch_runner', |
|
199 | - 'eei18n', |
|
200 | - array( |
|
201 | - 'ajax_url' => WP_AJAX_URL, |
|
202 | - 'is_admin' => (bool) is_admin(), |
|
203 | - 'error_message' => esc_html__('An error occurred and the job has been stopped. Please refresh the page to try again.', 'event_espresso'), |
|
204 | - ) |
|
205 | - ); |
|
206 | - $job_handler_classname = stripslashes($_GET['job_handler']); |
|
207 | - $request_data = array_diff_key( |
|
208 | - $_REQUEST, |
|
209 | - array_flip(array('action', 'page', 'ee', 'batch')) |
|
210 | - ); |
|
211 | - $batch_runner = new EventEspressoBatchRequest\BatchRequestProcessor(); |
|
212 | - // eg 'EventEspressoBatchRequest\JobHandlers\RegistrationsReport' |
|
213 | - $job_response = $batch_runner->create_job($job_handler_classname, $request_data); |
|
214 | - // remember the response for later. We need it to display the page body |
|
215 | - $this->_job_step_response = $job_response; |
|
216 | - return $job_response; |
|
217 | - } |
|
171 | + /** |
|
172 | + * Enqueues scripts and styles common to any batch job, and creates |
|
173 | + * a job from the request data, and stores the response in the |
|
174 | + * $this->_job_step_response property |
|
175 | + * |
|
176 | + * @return \EventEspressoBatchRequest\Helpers\JobStepResponse |
|
177 | + */ |
|
178 | + protected function _enqueue_batch_job_scripts_and_styles_and_start_job() |
|
179 | + { |
|
180 | + wp_register_script( |
|
181 | + 'progress_bar', |
|
182 | + EE_PLUGIN_DIR_URL . 'core/libraries/batch/Assets/progress_bar.js', |
|
183 | + array('jquery') |
|
184 | + ); |
|
185 | + wp_enqueue_style( |
|
186 | + 'progress_bar', |
|
187 | + EE_PLUGIN_DIR_URL . 'core/libraries/batch/Assets/progress_bar.css', |
|
188 | + array(), |
|
189 | + EVENT_ESPRESSO_VERSION |
|
190 | + ); |
|
191 | + wp_enqueue_script( |
|
192 | + 'batch_runner', |
|
193 | + EE_PLUGIN_DIR_URL . 'core/libraries/batch/Assets/batch_runner.js', |
|
194 | + array('progress_bar') |
|
195 | + ); |
|
196 | + // just copy the bits of EE admin's eei18n that we need in the JS |
|
197 | + wp_localize_script( |
|
198 | + 'batch_runner', |
|
199 | + 'eei18n', |
|
200 | + array( |
|
201 | + 'ajax_url' => WP_AJAX_URL, |
|
202 | + 'is_admin' => (bool) is_admin(), |
|
203 | + 'error_message' => esc_html__('An error occurred and the job has been stopped. Please refresh the page to try again.', 'event_espresso'), |
|
204 | + ) |
|
205 | + ); |
|
206 | + $job_handler_classname = stripslashes($_GET['job_handler']); |
|
207 | + $request_data = array_diff_key( |
|
208 | + $_REQUEST, |
|
209 | + array_flip(array('action', 'page', 'ee', 'batch')) |
|
210 | + ); |
|
211 | + $batch_runner = new EventEspressoBatchRequest\BatchRequestProcessor(); |
|
212 | + // eg 'EventEspressoBatchRequest\JobHandlers\RegistrationsReport' |
|
213 | + $job_response = $batch_runner->create_job($job_handler_classname, $request_data); |
|
214 | + // remember the response for later. We need it to display the page body |
|
215 | + $this->_job_step_response = $job_response; |
|
216 | + return $job_response; |
|
217 | + } |
|
218 | 218 | |
219 | - /** |
|
220 | - * If we are doing a frontend batch job, this makes it so WP shows our template's HTML |
|
221 | - * |
|
222 | - * @param string $template |
|
223 | - * @return string |
|
224 | - */ |
|
225 | - public function override_template($template) |
|
226 | - { |
|
227 | - if (isset($_REQUEST['espresso_batch']) && isset($_REQUEST['batch'])) { |
|
228 | - return EE_MODULES . 'batch' . DS . 'templates' . DS . 'batch_frontend_wrapper.template.html'; |
|
229 | - } |
|
230 | - return $template; |
|
231 | - } |
|
219 | + /** |
|
220 | + * If we are doing a frontend batch job, this makes it so WP shows our template's HTML |
|
221 | + * |
|
222 | + * @param string $template |
|
223 | + * @return string |
|
224 | + */ |
|
225 | + public function override_template($template) |
|
226 | + { |
|
227 | + if (isset($_REQUEST['espresso_batch']) && isset($_REQUEST['batch'])) { |
|
228 | + return EE_MODULES . 'batch' . DS . 'templates' . DS . 'batch_frontend_wrapper.template.html'; |
|
229 | + } |
|
230 | + return $template; |
|
231 | + } |
|
232 | 232 | |
233 | - /** |
|
234 | - * Adds an admin page which doesn't appear in the admin menu |
|
235 | - */ |
|
236 | - public function register_admin_pages() |
|
237 | - { |
|
238 | - add_submenu_page( |
|
239 | - '', // parent slug. we don't want this to actually appear in the menu |
|
240 | - __('Batch Job', 'event_espresso'), // page title |
|
241 | - 'n/a', // menu title |
|
242 | - 'read', // we want this page to actually be accessible to anyone, |
|
243 | - 'espresso_batch', // menu slug |
|
244 | - array(self::instance(), 'show_admin_page') |
|
245 | - ); |
|
246 | - } |
|
233 | + /** |
|
234 | + * Adds an admin page which doesn't appear in the admin menu |
|
235 | + */ |
|
236 | + public function register_admin_pages() |
|
237 | + { |
|
238 | + add_submenu_page( |
|
239 | + '', // parent slug. we don't want this to actually appear in the menu |
|
240 | + __('Batch Job', 'event_espresso'), // page title |
|
241 | + 'n/a', // menu title |
|
242 | + 'read', // we want this page to actually be accessible to anyone, |
|
243 | + 'espresso_batch', // menu slug |
|
244 | + array(self::instance(), 'show_admin_page') |
|
245 | + ); |
|
246 | + } |
|
247 | 247 | |
248 | - /** |
|
249 | - * Renders the admin page, after most of the work was already done during enqueuing scripts |
|
250 | - * of creating the job and localizing some data |
|
251 | - */ |
|
252 | - public function show_admin_page() |
|
253 | - { |
|
254 | - echo EEH_Template::locate_template( |
|
255 | - EE_MODULES . 'batch' . DS . 'templates' . DS . 'batch_wrapper.template.html', |
|
256 | - array('batch_request_type' => $this->batch_request_type()) |
|
257 | - ); |
|
258 | - } |
|
248 | + /** |
|
249 | + * Renders the admin page, after most of the work was already done during enqueuing scripts |
|
250 | + * of creating the job and localizing some data |
|
251 | + */ |
|
252 | + public function show_admin_page() |
|
253 | + { |
|
254 | + echo EEH_Template::locate_template( |
|
255 | + EE_MODULES . 'batch' . DS . 'templates' . DS . 'batch_wrapper.template.html', |
|
256 | + array('batch_request_type' => $this->batch_request_type()) |
|
257 | + ); |
|
258 | + } |
|
259 | 259 | |
260 | - /** |
|
261 | - * Receives ajax calls for continuing a job |
|
262 | - */ |
|
263 | - public function batch_continue() |
|
264 | - { |
|
265 | - $job_id = sanitize_text_field($_REQUEST['job_id']); |
|
266 | - $batch_runner = new EventEspressoBatchRequest\BatchRequestProcessor(); |
|
267 | - $response_obj = $batch_runner->continue_job($job_id); |
|
268 | - $this->_return_json($response_obj->to_array()); |
|
269 | - } |
|
260 | + /** |
|
261 | + * Receives ajax calls for continuing a job |
|
262 | + */ |
|
263 | + public function batch_continue() |
|
264 | + { |
|
265 | + $job_id = sanitize_text_field($_REQUEST['job_id']); |
|
266 | + $batch_runner = new EventEspressoBatchRequest\BatchRequestProcessor(); |
|
267 | + $response_obj = $batch_runner->continue_job($job_id); |
|
268 | + $this->_return_json($response_obj->to_array()); |
|
269 | + } |
|
270 | 270 | |
271 | - /** |
|
272 | - * Receives the ajax call to cleanup a job |
|
273 | - * |
|
274 | - * @return type |
|
275 | - */ |
|
276 | - public function batch_cleanup() |
|
277 | - { |
|
278 | - $job_id = sanitize_text_field($_REQUEST['job_id']); |
|
279 | - $batch_runner = new EventEspressoBatchRequest\BatchRequestProcessor(); |
|
280 | - $response_obj = $batch_runner->cleanup_job($job_id); |
|
281 | - $this->_return_json($response_obj->to_array()); |
|
282 | - } |
|
271 | + /** |
|
272 | + * Receives the ajax call to cleanup a job |
|
273 | + * |
|
274 | + * @return type |
|
275 | + */ |
|
276 | + public function batch_cleanup() |
|
277 | + { |
|
278 | + $job_id = sanitize_text_field($_REQUEST['job_id']); |
|
279 | + $batch_runner = new EventEspressoBatchRequest\BatchRequestProcessor(); |
|
280 | + $response_obj = $batch_runner->cleanup_job($job_id); |
|
281 | + $this->_return_json($response_obj->to_array()); |
|
282 | + } |
|
283 | 283 | |
284 | 284 | |
285 | - /** |
|
286 | - * Returns a json response |
|
287 | - * |
|
288 | - * @param array $data The data we want to send echo via in the JSON response's "data" element |
|
289 | - * |
|
290 | - * The returned json object is created from an array in the following format: |
|
291 | - * array( |
|
292 | - * 'notices' => '', // - contains any EE_Error formatted notices |
|
293 | - * 'data' => array() //this can be any key/value pairs that a method returns for later json parsing by the js. |
|
294 | - * We're also going to include the template args with every package (so js can pick out any specific template |
|
295 | - * args that might be included in here) |
|
296 | - * 'isEEajax' => true,//indicates this is a response from EE |
|
297 | - * ) |
|
298 | - */ |
|
299 | - protected function _return_json($data) |
|
300 | - { |
|
301 | - $json = array( |
|
302 | - 'notices' => EE_Error::get_notices(), |
|
303 | - 'data' => $data, |
|
304 | - 'isEEajax' => true |
|
305 | - // special flag so any ajax.Success methods in js can identify this return package as a EEajax package. |
|
306 | - ); |
|
285 | + /** |
|
286 | + * Returns a json response |
|
287 | + * |
|
288 | + * @param array $data The data we want to send echo via in the JSON response's "data" element |
|
289 | + * |
|
290 | + * The returned json object is created from an array in the following format: |
|
291 | + * array( |
|
292 | + * 'notices' => '', // - contains any EE_Error formatted notices |
|
293 | + * 'data' => array() //this can be any key/value pairs that a method returns for later json parsing by the js. |
|
294 | + * We're also going to include the template args with every package (so js can pick out any specific template |
|
295 | + * args that might be included in here) |
|
296 | + * 'isEEajax' => true,//indicates this is a response from EE |
|
297 | + * ) |
|
298 | + */ |
|
299 | + protected function _return_json($data) |
|
300 | + { |
|
301 | + $json = array( |
|
302 | + 'notices' => EE_Error::get_notices(), |
|
303 | + 'data' => $data, |
|
304 | + 'isEEajax' => true |
|
305 | + // special flag so any ajax.Success methods in js can identify this return package as a EEajax package. |
|
306 | + ); |
|
307 | 307 | |
308 | 308 | |
309 | - // make sure there are no php errors or headers_sent. Then we can set correct json header. |
|
310 | - if (null === error_get_last() || ! headers_sent()) { |
|
311 | - header('Content-Type: application/json; charset=UTF-8'); |
|
312 | - } |
|
313 | - echo wp_json_encode($json); |
|
314 | - exit(); |
|
315 | - } |
|
309 | + // make sure there are no php errors or headers_sent. Then we can set correct json header. |
|
310 | + if (null === error_get_last() || ! headers_sent()) { |
|
311 | + header('Content-Type: application/json; charset=UTF-8'); |
|
312 | + } |
|
313 | + echo wp_json_encode($json); |
|
314 | + exit(); |
|
315 | + } |
|
316 | 316 | |
317 | - /** |
|
318 | - * Gets the job step response which was done during the enqueuing of scripts |
|
319 | - * |
|
320 | - * @return \EventEspressoBatchRequest\Helpers\JobStepResponse |
|
321 | - */ |
|
322 | - public function job_step_response() |
|
323 | - { |
|
324 | - return $this->_job_step_response; |
|
325 | - } |
|
317 | + /** |
|
318 | + * Gets the job step response which was done during the enqueuing of scripts |
|
319 | + * |
|
320 | + * @return \EventEspressoBatchRequest\Helpers\JobStepResponse |
|
321 | + */ |
|
322 | + public function job_step_response() |
|
323 | + { |
|
324 | + return $this->_job_step_response; |
|
325 | + } |
|
326 | 326 | |
327 | - /** |
|
328 | - * Gets the batch request type indicated in the $_REQUEST |
|
329 | - * |
|
330 | - * @return string: EED_Batch::batch_job, EED_Batch::batch_file_job, EED_Batch::batch_not_job |
|
331 | - */ |
|
332 | - public function batch_request_type() |
|
333 | - { |
|
334 | - if ($this->_batch_request_type === null) { |
|
335 | - if (isset($_GET['batch'])) { |
|
336 | - if ($_GET['batch'] == self::batch_job) { |
|
337 | - $this->_batch_request_type = self::batch_job; |
|
338 | - } elseif ($_GET['batch'] == self::batch_file_job) { |
|
339 | - $this->_batch_request_type = self::batch_file_job; |
|
340 | - } |
|
341 | - } |
|
342 | - // if we didn't find that it was a batch request, indicate it wasn't |
|
343 | - if ($this->_batch_request_type === null) { |
|
344 | - $this->_batch_request_type = self::batch_not_job; |
|
345 | - } |
|
346 | - } |
|
347 | - return $this->_batch_request_type; |
|
348 | - } |
|
327 | + /** |
|
328 | + * Gets the batch request type indicated in the $_REQUEST |
|
329 | + * |
|
330 | + * @return string: EED_Batch::batch_job, EED_Batch::batch_file_job, EED_Batch::batch_not_job |
|
331 | + */ |
|
332 | + public function batch_request_type() |
|
333 | + { |
|
334 | + if ($this->_batch_request_type === null) { |
|
335 | + if (isset($_GET['batch'])) { |
|
336 | + if ($_GET['batch'] == self::batch_job) { |
|
337 | + $this->_batch_request_type = self::batch_job; |
|
338 | + } elseif ($_GET['batch'] == self::batch_file_job) { |
|
339 | + $this->_batch_request_type = self::batch_file_job; |
|
340 | + } |
|
341 | + } |
|
342 | + // if we didn't find that it was a batch request, indicate it wasn't |
|
343 | + if ($this->_batch_request_type === null) { |
|
344 | + $this->_batch_request_type = self::batch_not_job; |
|
345 | + } |
|
346 | + } |
|
347 | + return $this->_batch_request_type; |
|
348 | + } |
|
349 | 349 | |
350 | - /** |
|
351 | - * Unnecessary |
|
352 | - * |
|
353 | - * @param type $WP |
|
354 | - */ |
|
355 | - public function run($WP) |
|
356 | - { |
|
357 | - } |
|
350 | + /** |
|
351 | + * Unnecessary |
|
352 | + * |
|
353 | + * @param type $WP |
|
354 | + */ |
|
355 | + public function run($WP) |
|
356 | + { |
|
357 | + } |
|
358 | 358 | } |
@@ -18,332 +18,332 @@ |
||
18 | 18 | class EED_Recaptcha_Invisible extends EED_Module |
19 | 19 | { |
20 | 20 | |
21 | - /** |
|
22 | - * @var EE_Registration_Config $config |
|
23 | - */ |
|
24 | - private static $config; |
|
25 | - |
|
26 | - |
|
27 | - /** |
|
28 | - * @return EED_Module|EED_Recaptcha |
|
29 | - */ |
|
30 | - public static function instance() |
|
31 | - { |
|
32 | - return parent::get_instance(__CLASS__); |
|
33 | - } |
|
34 | - |
|
35 | - |
|
36 | - /** |
|
37 | - * @return void |
|
38 | - * @throws InvalidInterfaceException |
|
39 | - * @throws InvalidDataTypeException |
|
40 | - * @throws InvalidArgumentException |
|
41 | - */ |
|
42 | - public static function set_hooks() |
|
43 | - { |
|
44 | - EED_Recaptcha_Invisible::setProperties(); |
|
45 | - if (EED_Recaptcha_Invisible::useInvisibleRecaptcha()) { |
|
46 | - if (EED_Recaptcha_Invisible::protectForm('ticket_selector')) { |
|
47 | - // ticket selection |
|
48 | - add_filter( |
|
49 | - 'FHEE__EE_Ticket_Selector__after_ticket_selector_submit', |
|
50 | - array('EED_Recaptcha_Invisible', 'ticketSelectorForm'), |
|
51 | - 10, |
|
52 | - 3 |
|
53 | - ); |
|
54 | - add_action( |
|
55 | - 'EED_Ticket_Selector__process_ticket_selections__before', |
|
56 | - array('EED_Recaptcha_Invisible', 'processTicketSelectorForm') |
|
57 | - ); |
|
58 | - } |
|
59 | - if (EED_Recaptcha_Invisible::protectForm('registration_form')) { |
|
60 | - // checkout |
|
61 | - add_action( |
|
62 | - 'AHEE__EE_SPCO_Reg_Step__display_reg_form__reg_form', |
|
63 | - array('EED_Recaptcha_Invisible', 'spcoRegStepForm') |
|
64 | - ); |
|
65 | - add_filter( |
|
66 | - 'FHEE__EE_Form_Section_Proper__receive_form_submission__req_data', |
|
67 | - array('EED_Recaptcha_Invisible', 'receiveSpcoRegStepForm'), |
|
68 | - 10, |
|
69 | - 2 |
|
70 | - ); |
|
71 | - } |
|
72 | - add_action('loop_end', array('EED_Recaptcha_Invisible', 'localizeScriptVars')); |
|
73 | - } |
|
74 | - } |
|
75 | - |
|
76 | - |
|
77 | - /** |
|
78 | - * @return void |
|
79 | - * @throws InvalidInterfaceException |
|
80 | - * @throws InvalidDataTypeException |
|
81 | - * @throws InvalidArgumentException |
|
82 | - */ |
|
83 | - public static function set_hooks_admin() |
|
84 | - { |
|
85 | - EED_Recaptcha_Invisible::setProperties(); |
|
86 | - if (EED_Recaptcha_Invisible::protectForm('ticket_selector')) { |
|
87 | - add_action( |
|
88 | - 'EED_Ticket_Selector__process_ticket_selections__before', |
|
89 | - array('EED_Recaptcha_Invisible', 'processTicketSelectorForm') |
|
90 | - ); |
|
91 | - } |
|
92 | - if (EED_Recaptcha_Invisible::protectForm('registration_form')) { |
|
93 | - add_filter( |
|
94 | - 'FHEE__EE_Form_Section_Proper__receive_form_submission__req_data', |
|
95 | - array('EED_Recaptcha_Invisible', 'receiveSpcoRegStepForm'), |
|
96 | - 10, |
|
97 | - 2 |
|
98 | - ); |
|
99 | - } |
|
100 | - // admin settings |
|
101 | - add_action( |
|
102 | - 'AHEE__Extend_Registration_Form_Admin_Page___reg_form_settings_template', |
|
103 | - array('EED_Recaptcha_Invisible', 'adminSettings') |
|
104 | - ); |
|
105 | - add_filter( |
|
106 | - 'FHEE__Extend_Registration_Form_Admin_Page___update_reg_form_settings__CFG_registration', |
|
107 | - array('EED_Recaptcha_Invisible', 'updateAdminSettings') |
|
108 | - ); |
|
109 | - } |
|
110 | - |
|
111 | - |
|
112 | - /** |
|
113 | - * @return void |
|
114 | - * @throws InvalidInterfaceException |
|
115 | - * @throws InvalidDataTypeException |
|
116 | - * @throws InvalidArgumentException |
|
117 | - */ |
|
118 | - public static function setProperties() |
|
119 | - { |
|
120 | - |
|
121 | - EED_Recaptcha_Invisible::$config = EE_Registry::instance()->CFG->registration; |
|
122 | - } |
|
123 | - |
|
124 | - |
|
125 | - /** |
|
126 | - * @return boolean |
|
127 | - */ |
|
128 | - public static function useInvisibleRecaptcha() |
|
129 | - { |
|
130 | - return EED_Recaptcha_Invisible::$config->use_captcha |
|
131 | - && EED_Recaptcha_Invisible::$config->recaptcha_theme === 'invisible'; |
|
132 | - } |
|
133 | - |
|
134 | - |
|
135 | - /** |
|
136 | - * @param string $form |
|
137 | - * @return boolean |
|
138 | - */ |
|
139 | - public static function protectForm($form) |
|
140 | - { |
|
141 | - return is_array(EED_Recaptcha_Invisible::$config->recaptcha_protected_forms) |
|
142 | - && in_array($form, EED_Recaptcha_Invisible::$config->recaptcha_protected_forms, true); |
|
143 | - } |
|
144 | - |
|
145 | - |
|
146 | - /** |
|
147 | - * @return void |
|
148 | - * @throws InvalidInterfaceException |
|
149 | - * @throws InvalidDataTypeException |
|
150 | - * @throws InvalidArgumentException |
|
151 | - */ |
|
152 | - public static function localizeScriptVars() |
|
153 | - { |
|
154 | - /** @var \EventEspresso\core\services\request\Request $request */ |
|
155 | - $request = LoaderFactory::getLoader()->getShared('EventEspresso\core\services\request\Request'); |
|
156 | - // Invisible Recaptcha is ONLY ever required for the frontend and admin |
|
157 | - // so we don't need to load any JS assets for other types of requests (like AJAX or API). |
|
158 | - if (! ($request->isAdmin() || $request->isFrontend())) { |
|
159 | - return; |
|
160 | - } |
|
161 | - wp_localize_script( |
|
162 | - EE_Invisible_Recaptcha_Input::SCRIPT_HANDLE_ESPRESSO_INVISIBLE_RECAPTCHA, |
|
163 | - 'eeRecaptcha', |
|
164 | - RecaptchaFactory::create()->getLocalizedVars() |
|
165 | - ); |
|
166 | - } |
|
167 | - |
|
168 | - |
|
169 | - /** |
|
170 | - * @return string |
|
171 | - */ |
|
172 | - public static function assetsUrl() |
|
173 | - { |
|
174 | - return plugin_dir_url(__FILE__) . 'assets' . DS; |
|
175 | - } |
|
176 | - |
|
177 | - |
|
178 | - /** |
|
179 | - * @param \WP $WP |
|
180 | - */ |
|
181 | - public function run($WP) |
|
182 | - { |
|
183 | - } |
|
184 | - |
|
185 | - |
|
186 | - /** |
|
187 | - * @param RequestInterface $request |
|
188 | - * @return bool |
|
189 | - * @throws InvalidArgumentException |
|
190 | - * @throws InvalidDataTypeException |
|
191 | - * @throws InvalidInterfaceException |
|
192 | - * @throws RuntimeException |
|
193 | - */ |
|
194 | - public static function verifyToken(RequestInterface $request) |
|
195 | - { |
|
196 | - return RecaptchaFactory::create()->verifyToken($request); |
|
197 | - } |
|
198 | - |
|
199 | - |
|
200 | - /** |
|
201 | - * @param EE_Form_Section_Proper $reg_form |
|
202 | - * @return void |
|
203 | - * @throws EE_Error |
|
204 | - * @throws InvalidArgumentException |
|
205 | - * @throws InvalidDataTypeException |
|
206 | - * @throws InvalidInterfaceException |
|
207 | - * @throws DomainException |
|
208 | - */ |
|
209 | - public static function spcoRegStepForm(EE_Form_Section_Proper $reg_form) |
|
210 | - { |
|
211 | - // do nothing if form isn't for a reg step or test has already been passed |
|
212 | - if (! EED_Recaptcha_Invisible::processSpcoRegStepForm($reg_form)) { |
|
213 | - return; |
|
214 | - } |
|
215 | - $default_hidden_inputs = $reg_form->get_subsection('default_hidden_inputs'); |
|
216 | - if ($default_hidden_inputs instanceof EE_Form_Section_Proper) { |
|
217 | - $invisible_recaptcha = RecaptchaFactory::create(); |
|
218 | - $invisible_recaptcha->addToFormSection($default_hidden_inputs); |
|
219 | - } |
|
220 | - } |
|
221 | - |
|
222 | - |
|
223 | - /** |
|
224 | - * @param EE_Form_Section_Proper $reg_form |
|
225 | - * @return bool |
|
226 | - * @throws InvalidDataTypeException |
|
227 | - * @throws InvalidInterfaceException |
|
228 | - * @throws EE_Error |
|
229 | - * @throws InvalidArgumentException |
|
230 | - */ |
|
231 | - public static function processSpcoRegStepForm(EE_Form_Section_Proper $reg_form) |
|
232 | - { |
|
233 | - return strpos($reg_form->name(), 'reg-step-form') !== false |
|
234 | - && ! RecaptchaFactory::create()->recaptchaPassed(); |
|
235 | - } |
|
236 | - |
|
237 | - |
|
238 | - /** |
|
239 | - * @param array|null $req_data |
|
240 | - * @param EE_Form_Section_Proper $reg_form |
|
241 | - * @return array |
|
242 | - * @throws EE_Error |
|
243 | - * @throws InvalidArgumentException |
|
244 | - * @throws InvalidDataTypeException |
|
245 | - * @throws InvalidInterfaceException |
|
246 | - * @throws RuntimeException |
|
247 | - */ |
|
248 | - public static function receiveSpcoRegStepForm($req_data = array(), EE_Form_Section_Proper $reg_form) |
|
249 | - { |
|
250 | - // do nothing if form isn't for a reg step or test has already been passed |
|
251 | - if (! EED_Recaptcha_Invisible::processSpcoRegStepForm($reg_form)) { |
|
252 | - return $req_data; |
|
253 | - } |
|
254 | - /** @var RequestInterface $request */ |
|
255 | - $request = LoaderFactory::getLoader()->getShared('EventEspresso\core\services\request\RequestInterface'); |
|
256 | - if (! EED_Recaptcha_Invisible::verifyToken($request)) { |
|
257 | - if ($request->isAjax()) { |
|
258 | - $json_response = new EE_SPCO_JSON_Response(); |
|
259 | - $json_response->echoAndExit(); |
|
260 | - } |
|
261 | - EEH_URL::safeRedirectAndExit( |
|
262 | - EE_Registry::instance()->CFG->core->reg_page_url() |
|
263 | - ); |
|
264 | - } |
|
265 | - return $req_data; |
|
266 | - } |
|
267 | - |
|
268 | - |
|
269 | - /** |
|
270 | - * @param string $html |
|
271 | - * @param EE_Event $event |
|
272 | - * @param bool $iframe |
|
273 | - * @return string |
|
274 | - * @throws EE_Error |
|
275 | - * @throws InvalidArgumentException |
|
276 | - * @throws InvalidDataTypeException |
|
277 | - * @throws InvalidInterfaceException |
|
278 | - * @throws ReflectionException |
|
279 | - * @throws DomainException |
|
280 | - */ |
|
281 | - public static function ticketSelectorForm($html = '', EE_Event $event, $iframe = false) |
|
282 | - { |
|
283 | - $recaptcha = RecaptchaFactory::create(); |
|
284 | - // do nothing if test has already been passed |
|
285 | - if ($recaptcha->recaptchaPassed()) { |
|
286 | - return $html; |
|
287 | - } |
|
288 | - $html .= $recaptcha->getInputHtml( |
|
289 | - array( |
|
290 | - 'recaptcha_id' => $event->ID(), |
|
291 | - 'iframe' => $iframe, |
|
292 | - 'localized_vars' => $recaptcha->getLocalizedVars(), |
|
293 | - ) |
|
294 | - ); |
|
295 | - return $html; |
|
296 | - } |
|
297 | - |
|
298 | - |
|
299 | - /** |
|
300 | - * @return void |
|
301 | - * @throws InvalidArgumentException |
|
302 | - * @throws InvalidInterfaceException |
|
303 | - * @throws InvalidDataTypeException |
|
304 | - * @throws RuntimeException |
|
305 | - */ |
|
306 | - public static function processTicketSelectorForm() |
|
307 | - { |
|
308 | - // do nothing if test has already been passed |
|
309 | - if (RecaptchaFactory::create()->recaptchaPassed()) { |
|
310 | - return; |
|
311 | - } |
|
312 | - /** @var RequestInterface $request */ |
|
313 | - $request = LoaderFactory::getLoader()->getShared('EventEspresso\core\services\request\RequestInterface'); |
|
314 | - if (! EED_Recaptcha_Invisible::verifyToken($request)) { |
|
315 | - $event_id = $request->getRequestParam('tkt-slctr-event-id'); |
|
316 | - $return_url = $request->requestParamIsSet("tkt-slctr-return-url-{$event_id}") |
|
317 | - ? $request->getRequestParam("tkt-slctr-return-url-{$event_id}") |
|
318 | - : get_permalink($event_id); |
|
319 | - EEH_URL::safeRedirectAndExit($return_url); |
|
320 | - } |
|
321 | - } |
|
322 | - |
|
323 | - |
|
324 | - /** |
|
325 | - * @throws EE_Error |
|
326 | - * @throws InvalidArgumentException |
|
327 | - * @throws InvalidDataTypeException |
|
328 | - * @throws InvalidInterfaceException |
|
329 | - */ |
|
330 | - public static function adminSettings() |
|
331 | - { |
|
332 | - RecaptchaFactory::getAdminModule()->adminSettings(); |
|
333 | - } |
|
334 | - |
|
335 | - |
|
336 | - /** |
|
337 | - * @param EE_Registration_Config $EE_Registration_Config |
|
338 | - * @return EE_Registration_Config |
|
339 | - * @throws EE_Error |
|
340 | - * @throws InvalidArgumentException |
|
341 | - * @throws InvalidDataTypeException |
|
342 | - * @throws InvalidInterfaceException |
|
343 | - * @throws ReflectionException |
|
344 | - */ |
|
345 | - public static function updateAdminSettings(EE_Registration_Config $EE_Registration_Config) |
|
346 | - { |
|
347 | - return RecaptchaFactory::getAdminModule()->updateAdminSettings($EE_Registration_Config); |
|
348 | - } |
|
21 | + /** |
|
22 | + * @var EE_Registration_Config $config |
|
23 | + */ |
|
24 | + private static $config; |
|
25 | + |
|
26 | + |
|
27 | + /** |
|
28 | + * @return EED_Module|EED_Recaptcha |
|
29 | + */ |
|
30 | + public static function instance() |
|
31 | + { |
|
32 | + return parent::get_instance(__CLASS__); |
|
33 | + } |
|
34 | + |
|
35 | + |
|
36 | + /** |
|
37 | + * @return void |
|
38 | + * @throws InvalidInterfaceException |
|
39 | + * @throws InvalidDataTypeException |
|
40 | + * @throws InvalidArgumentException |
|
41 | + */ |
|
42 | + public static function set_hooks() |
|
43 | + { |
|
44 | + EED_Recaptcha_Invisible::setProperties(); |
|
45 | + if (EED_Recaptcha_Invisible::useInvisibleRecaptcha()) { |
|
46 | + if (EED_Recaptcha_Invisible::protectForm('ticket_selector')) { |
|
47 | + // ticket selection |
|
48 | + add_filter( |
|
49 | + 'FHEE__EE_Ticket_Selector__after_ticket_selector_submit', |
|
50 | + array('EED_Recaptcha_Invisible', 'ticketSelectorForm'), |
|
51 | + 10, |
|
52 | + 3 |
|
53 | + ); |
|
54 | + add_action( |
|
55 | + 'EED_Ticket_Selector__process_ticket_selections__before', |
|
56 | + array('EED_Recaptcha_Invisible', 'processTicketSelectorForm') |
|
57 | + ); |
|
58 | + } |
|
59 | + if (EED_Recaptcha_Invisible::protectForm('registration_form')) { |
|
60 | + // checkout |
|
61 | + add_action( |
|
62 | + 'AHEE__EE_SPCO_Reg_Step__display_reg_form__reg_form', |
|
63 | + array('EED_Recaptcha_Invisible', 'spcoRegStepForm') |
|
64 | + ); |
|
65 | + add_filter( |
|
66 | + 'FHEE__EE_Form_Section_Proper__receive_form_submission__req_data', |
|
67 | + array('EED_Recaptcha_Invisible', 'receiveSpcoRegStepForm'), |
|
68 | + 10, |
|
69 | + 2 |
|
70 | + ); |
|
71 | + } |
|
72 | + add_action('loop_end', array('EED_Recaptcha_Invisible', 'localizeScriptVars')); |
|
73 | + } |
|
74 | + } |
|
75 | + |
|
76 | + |
|
77 | + /** |
|
78 | + * @return void |
|
79 | + * @throws InvalidInterfaceException |
|
80 | + * @throws InvalidDataTypeException |
|
81 | + * @throws InvalidArgumentException |
|
82 | + */ |
|
83 | + public static function set_hooks_admin() |
|
84 | + { |
|
85 | + EED_Recaptcha_Invisible::setProperties(); |
|
86 | + if (EED_Recaptcha_Invisible::protectForm('ticket_selector')) { |
|
87 | + add_action( |
|
88 | + 'EED_Ticket_Selector__process_ticket_selections__before', |
|
89 | + array('EED_Recaptcha_Invisible', 'processTicketSelectorForm') |
|
90 | + ); |
|
91 | + } |
|
92 | + if (EED_Recaptcha_Invisible::protectForm('registration_form')) { |
|
93 | + add_filter( |
|
94 | + 'FHEE__EE_Form_Section_Proper__receive_form_submission__req_data', |
|
95 | + array('EED_Recaptcha_Invisible', 'receiveSpcoRegStepForm'), |
|
96 | + 10, |
|
97 | + 2 |
|
98 | + ); |
|
99 | + } |
|
100 | + // admin settings |
|
101 | + add_action( |
|
102 | + 'AHEE__Extend_Registration_Form_Admin_Page___reg_form_settings_template', |
|
103 | + array('EED_Recaptcha_Invisible', 'adminSettings') |
|
104 | + ); |
|
105 | + add_filter( |
|
106 | + 'FHEE__Extend_Registration_Form_Admin_Page___update_reg_form_settings__CFG_registration', |
|
107 | + array('EED_Recaptcha_Invisible', 'updateAdminSettings') |
|
108 | + ); |
|
109 | + } |
|
110 | + |
|
111 | + |
|
112 | + /** |
|
113 | + * @return void |
|
114 | + * @throws InvalidInterfaceException |
|
115 | + * @throws InvalidDataTypeException |
|
116 | + * @throws InvalidArgumentException |
|
117 | + */ |
|
118 | + public static function setProperties() |
|
119 | + { |
|
120 | + |
|
121 | + EED_Recaptcha_Invisible::$config = EE_Registry::instance()->CFG->registration; |
|
122 | + } |
|
123 | + |
|
124 | + |
|
125 | + /** |
|
126 | + * @return boolean |
|
127 | + */ |
|
128 | + public static function useInvisibleRecaptcha() |
|
129 | + { |
|
130 | + return EED_Recaptcha_Invisible::$config->use_captcha |
|
131 | + && EED_Recaptcha_Invisible::$config->recaptcha_theme === 'invisible'; |
|
132 | + } |
|
133 | + |
|
134 | + |
|
135 | + /** |
|
136 | + * @param string $form |
|
137 | + * @return boolean |
|
138 | + */ |
|
139 | + public static function protectForm($form) |
|
140 | + { |
|
141 | + return is_array(EED_Recaptcha_Invisible::$config->recaptcha_protected_forms) |
|
142 | + && in_array($form, EED_Recaptcha_Invisible::$config->recaptcha_protected_forms, true); |
|
143 | + } |
|
144 | + |
|
145 | + |
|
146 | + /** |
|
147 | + * @return void |
|
148 | + * @throws InvalidInterfaceException |
|
149 | + * @throws InvalidDataTypeException |
|
150 | + * @throws InvalidArgumentException |
|
151 | + */ |
|
152 | + public static function localizeScriptVars() |
|
153 | + { |
|
154 | + /** @var \EventEspresso\core\services\request\Request $request */ |
|
155 | + $request = LoaderFactory::getLoader()->getShared('EventEspresso\core\services\request\Request'); |
|
156 | + // Invisible Recaptcha is ONLY ever required for the frontend and admin |
|
157 | + // so we don't need to load any JS assets for other types of requests (like AJAX or API). |
|
158 | + if (! ($request->isAdmin() || $request->isFrontend())) { |
|
159 | + return; |
|
160 | + } |
|
161 | + wp_localize_script( |
|
162 | + EE_Invisible_Recaptcha_Input::SCRIPT_HANDLE_ESPRESSO_INVISIBLE_RECAPTCHA, |
|
163 | + 'eeRecaptcha', |
|
164 | + RecaptchaFactory::create()->getLocalizedVars() |
|
165 | + ); |
|
166 | + } |
|
167 | + |
|
168 | + |
|
169 | + /** |
|
170 | + * @return string |
|
171 | + */ |
|
172 | + public static function assetsUrl() |
|
173 | + { |
|
174 | + return plugin_dir_url(__FILE__) . 'assets' . DS; |
|
175 | + } |
|
176 | + |
|
177 | + |
|
178 | + /** |
|
179 | + * @param \WP $WP |
|
180 | + */ |
|
181 | + public function run($WP) |
|
182 | + { |
|
183 | + } |
|
184 | + |
|
185 | + |
|
186 | + /** |
|
187 | + * @param RequestInterface $request |
|
188 | + * @return bool |
|
189 | + * @throws InvalidArgumentException |
|
190 | + * @throws InvalidDataTypeException |
|
191 | + * @throws InvalidInterfaceException |
|
192 | + * @throws RuntimeException |
|
193 | + */ |
|
194 | + public static function verifyToken(RequestInterface $request) |
|
195 | + { |
|
196 | + return RecaptchaFactory::create()->verifyToken($request); |
|
197 | + } |
|
198 | + |
|
199 | + |
|
200 | + /** |
|
201 | + * @param EE_Form_Section_Proper $reg_form |
|
202 | + * @return void |
|
203 | + * @throws EE_Error |
|
204 | + * @throws InvalidArgumentException |
|
205 | + * @throws InvalidDataTypeException |
|
206 | + * @throws InvalidInterfaceException |
|
207 | + * @throws DomainException |
|
208 | + */ |
|
209 | + public static function spcoRegStepForm(EE_Form_Section_Proper $reg_form) |
|
210 | + { |
|
211 | + // do nothing if form isn't for a reg step or test has already been passed |
|
212 | + if (! EED_Recaptcha_Invisible::processSpcoRegStepForm($reg_form)) { |
|
213 | + return; |
|
214 | + } |
|
215 | + $default_hidden_inputs = $reg_form->get_subsection('default_hidden_inputs'); |
|
216 | + if ($default_hidden_inputs instanceof EE_Form_Section_Proper) { |
|
217 | + $invisible_recaptcha = RecaptchaFactory::create(); |
|
218 | + $invisible_recaptcha->addToFormSection($default_hidden_inputs); |
|
219 | + } |
|
220 | + } |
|
221 | + |
|
222 | + |
|
223 | + /** |
|
224 | + * @param EE_Form_Section_Proper $reg_form |
|
225 | + * @return bool |
|
226 | + * @throws InvalidDataTypeException |
|
227 | + * @throws InvalidInterfaceException |
|
228 | + * @throws EE_Error |
|
229 | + * @throws InvalidArgumentException |
|
230 | + */ |
|
231 | + public static function processSpcoRegStepForm(EE_Form_Section_Proper $reg_form) |
|
232 | + { |
|
233 | + return strpos($reg_form->name(), 'reg-step-form') !== false |
|
234 | + && ! RecaptchaFactory::create()->recaptchaPassed(); |
|
235 | + } |
|
236 | + |
|
237 | + |
|
238 | + /** |
|
239 | + * @param array|null $req_data |
|
240 | + * @param EE_Form_Section_Proper $reg_form |
|
241 | + * @return array |
|
242 | + * @throws EE_Error |
|
243 | + * @throws InvalidArgumentException |
|
244 | + * @throws InvalidDataTypeException |
|
245 | + * @throws InvalidInterfaceException |
|
246 | + * @throws RuntimeException |
|
247 | + */ |
|
248 | + public static function receiveSpcoRegStepForm($req_data = array(), EE_Form_Section_Proper $reg_form) |
|
249 | + { |
|
250 | + // do nothing if form isn't for a reg step or test has already been passed |
|
251 | + if (! EED_Recaptcha_Invisible::processSpcoRegStepForm($reg_form)) { |
|
252 | + return $req_data; |
|
253 | + } |
|
254 | + /** @var RequestInterface $request */ |
|
255 | + $request = LoaderFactory::getLoader()->getShared('EventEspresso\core\services\request\RequestInterface'); |
|
256 | + if (! EED_Recaptcha_Invisible::verifyToken($request)) { |
|
257 | + if ($request->isAjax()) { |
|
258 | + $json_response = new EE_SPCO_JSON_Response(); |
|
259 | + $json_response->echoAndExit(); |
|
260 | + } |
|
261 | + EEH_URL::safeRedirectAndExit( |
|
262 | + EE_Registry::instance()->CFG->core->reg_page_url() |
|
263 | + ); |
|
264 | + } |
|
265 | + return $req_data; |
|
266 | + } |
|
267 | + |
|
268 | + |
|
269 | + /** |
|
270 | + * @param string $html |
|
271 | + * @param EE_Event $event |
|
272 | + * @param bool $iframe |
|
273 | + * @return string |
|
274 | + * @throws EE_Error |
|
275 | + * @throws InvalidArgumentException |
|
276 | + * @throws InvalidDataTypeException |
|
277 | + * @throws InvalidInterfaceException |
|
278 | + * @throws ReflectionException |
|
279 | + * @throws DomainException |
|
280 | + */ |
|
281 | + public static function ticketSelectorForm($html = '', EE_Event $event, $iframe = false) |
|
282 | + { |
|
283 | + $recaptcha = RecaptchaFactory::create(); |
|
284 | + // do nothing if test has already been passed |
|
285 | + if ($recaptcha->recaptchaPassed()) { |
|
286 | + return $html; |
|
287 | + } |
|
288 | + $html .= $recaptcha->getInputHtml( |
|
289 | + array( |
|
290 | + 'recaptcha_id' => $event->ID(), |
|
291 | + 'iframe' => $iframe, |
|
292 | + 'localized_vars' => $recaptcha->getLocalizedVars(), |
|
293 | + ) |
|
294 | + ); |
|
295 | + return $html; |
|
296 | + } |
|
297 | + |
|
298 | + |
|
299 | + /** |
|
300 | + * @return void |
|
301 | + * @throws InvalidArgumentException |
|
302 | + * @throws InvalidInterfaceException |
|
303 | + * @throws InvalidDataTypeException |
|
304 | + * @throws RuntimeException |
|
305 | + */ |
|
306 | + public static function processTicketSelectorForm() |
|
307 | + { |
|
308 | + // do nothing if test has already been passed |
|
309 | + if (RecaptchaFactory::create()->recaptchaPassed()) { |
|
310 | + return; |
|
311 | + } |
|
312 | + /** @var RequestInterface $request */ |
|
313 | + $request = LoaderFactory::getLoader()->getShared('EventEspresso\core\services\request\RequestInterface'); |
|
314 | + if (! EED_Recaptcha_Invisible::verifyToken($request)) { |
|
315 | + $event_id = $request->getRequestParam('tkt-slctr-event-id'); |
|
316 | + $return_url = $request->requestParamIsSet("tkt-slctr-return-url-{$event_id}") |
|
317 | + ? $request->getRequestParam("tkt-slctr-return-url-{$event_id}") |
|
318 | + : get_permalink($event_id); |
|
319 | + EEH_URL::safeRedirectAndExit($return_url); |
|
320 | + } |
|
321 | + } |
|
322 | + |
|
323 | + |
|
324 | + /** |
|
325 | + * @throws EE_Error |
|
326 | + * @throws InvalidArgumentException |
|
327 | + * @throws InvalidDataTypeException |
|
328 | + * @throws InvalidInterfaceException |
|
329 | + */ |
|
330 | + public static function adminSettings() |
|
331 | + { |
|
332 | + RecaptchaFactory::getAdminModule()->adminSettings(); |
|
333 | + } |
|
334 | + |
|
335 | + |
|
336 | + /** |
|
337 | + * @param EE_Registration_Config $EE_Registration_Config |
|
338 | + * @return EE_Registration_Config |
|
339 | + * @throws EE_Error |
|
340 | + * @throws InvalidArgumentException |
|
341 | + * @throws InvalidDataTypeException |
|
342 | + * @throws InvalidInterfaceException |
|
343 | + * @throws ReflectionException |
|
344 | + */ |
|
345 | + public static function updateAdminSettings(EE_Registration_Config $EE_Registration_Config) |
|
346 | + { |
|
347 | + return RecaptchaFactory::getAdminModule()->updateAdminSettings($EE_Registration_Config); |
|
348 | + } |
|
349 | 349 | } |
@@ -155,7 +155,7 @@ discard block |
||
155 | 155 | $request = LoaderFactory::getLoader()->getShared('EventEspresso\core\services\request\Request'); |
156 | 156 | // Invisible Recaptcha is ONLY ever required for the frontend and admin |
157 | 157 | // so we don't need to load any JS assets for other types of requests (like AJAX or API). |
158 | - if (! ($request->isAdmin() || $request->isFrontend())) { |
|
158 | + if ( ! ($request->isAdmin() || $request->isFrontend())) { |
|
159 | 159 | return; |
160 | 160 | } |
161 | 161 | wp_localize_script( |
@@ -171,7 +171,7 @@ discard block |
||
171 | 171 | */ |
172 | 172 | public static function assetsUrl() |
173 | 173 | { |
174 | - return plugin_dir_url(__FILE__) . 'assets' . DS; |
|
174 | + return plugin_dir_url(__FILE__).'assets'.DS; |
|
175 | 175 | } |
176 | 176 | |
177 | 177 | |
@@ -209,7 +209,7 @@ discard block |
||
209 | 209 | public static function spcoRegStepForm(EE_Form_Section_Proper $reg_form) |
210 | 210 | { |
211 | 211 | // do nothing if form isn't for a reg step or test has already been passed |
212 | - if (! EED_Recaptcha_Invisible::processSpcoRegStepForm($reg_form)) { |
|
212 | + if ( ! EED_Recaptcha_Invisible::processSpcoRegStepForm($reg_form)) { |
|
213 | 213 | return; |
214 | 214 | } |
215 | 215 | $default_hidden_inputs = $reg_form->get_subsection('default_hidden_inputs'); |
@@ -248,12 +248,12 @@ discard block |
||
248 | 248 | public static function receiveSpcoRegStepForm($req_data = array(), EE_Form_Section_Proper $reg_form) |
249 | 249 | { |
250 | 250 | // do nothing if form isn't for a reg step or test has already been passed |
251 | - if (! EED_Recaptcha_Invisible::processSpcoRegStepForm($reg_form)) { |
|
251 | + if ( ! EED_Recaptcha_Invisible::processSpcoRegStepForm($reg_form)) { |
|
252 | 252 | return $req_data; |
253 | 253 | } |
254 | 254 | /** @var RequestInterface $request */ |
255 | 255 | $request = LoaderFactory::getLoader()->getShared('EventEspresso\core\services\request\RequestInterface'); |
256 | - if (! EED_Recaptcha_Invisible::verifyToken($request)) { |
|
256 | + if ( ! EED_Recaptcha_Invisible::verifyToken($request)) { |
|
257 | 257 | if ($request->isAjax()) { |
258 | 258 | $json_response = new EE_SPCO_JSON_Response(); |
259 | 259 | $json_response->echoAndExit(); |
@@ -311,7 +311,7 @@ discard block |
||
311 | 311 | } |
312 | 312 | /** @var RequestInterface $request */ |
313 | 313 | $request = LoaderFactory::getLoader()->getShared('EventEspresso\core\services\request\RequestInterface'); |
314 | - if (! EED_Recaptcha_Invisible::verifyToken($request)) { |
|
314 | + if ( ! EED_Recaptcha_Invisible::verifyToken($request)) { |
|
315 | 315 | $event_id = $request->getRequestParam('tkt-slctr-event-id'); |
316 | 316 | $return_url = $request->requestParamIsSet("tkt-slctr-return-url-{$event_id}") |
317 | 317 | ? $request->getRequestParam("tkt-slctr-return-url-{$event_id}") |
@@ -27,258 +27,258 @@ |
||
27 | 27 | class InvisibleRecaptcha |
28 | 28 | { |
29 | 29 | |
30 | - const URL_GOOGLE_RECAPTCHA_API = 'https://www.google.com/recaptcha/api/siteverify'; |
|
30 | + const URL_GOOGLE_RECAPTCHA_API = 'https://www.google.com/recaptcha/api/siteverify'; |
|
31 | 31 | |
32 | - const SESSION_DATA_KEY_RECAPTCHA_PASSED = 'recaptcha_passed'; |
|
32 | + const SESSION_DATA_KEY_RECAPTCHA_PASSED = 'recaptcha_passed'; |
|
33 | 33 | |
34 | - /** |
|
35 | - * @var EE_Registration_Config $config |
|
36 | - */ |
|
37 | - private $config; |
|
34 | + /** |
|
35 | + * @var EE_Registration_Config $config |
|
36 | + */ |
|
37 | + private $config; |
|
38 | 38 | |
39 | - /** |
|
40 | - * @var EE_Session $session |
|
41 | - */ |
|
42 | - private $session; |
|
39 | + /** |
|
40 | + * @var EE_Session $session |
|
41 | + */ |
|
42 | + private $session; |
|
43 | 43 | |
44 | - /** |
|
45 | - * @var boolean $recaptcha_passed |
|
46 | - */ |
|
47 | - private $recaptcha_passed; |
|
44 | + /** |
|
45 | + * @var boolean $recaptcha_passed |
|
46 | + */ |
|
47 | + private $recaptcha_passed; |
|
48 | 48 | |
49 | 49 | |
50 | - /** |
|
51 | - * InvisibleRecaptcha constructor. |
|
52 | - * |
|
53 | - * @param EE_Registration_Config $registration_config |
|
54 | - * @param EE_Session $session |
|
55 | - */ |
|
56 | - public function __construct(EE_Registration_Config $registration_config, EE_Session $session) |
|
57 | - { |
|
58 | - $this->config = $registration_config; |
|
59 | - $this->session = $session; |
|
60 | - } |
|
50 | + /** |
|
51 | + * InvisibleRecaptcha constructor. |
|
52 | + * |
|
53 | + * @param EE_Registration_Config $registration_config |
|
54 | + * @param EE_Session $session |
|
55 | + */ |
|
56 | + public function __construct(EE_Registration_Config $registration_config, EE_Session $session) |
|
57 | + { |
|
58 | + $this->config = $registration_config; |
|
59 | + $this->session = $session; |
|
60 | + } |
|
61 | 61 | |
62 | 62 | |
63 | - /** |
|
64 | - * @return boolean |
|
65 | - */ |
|
66 | - public function useInvisibleRecaptcha() |
|
67 | - { |
|
68 | - return $this->config->use_captcha && $this->config->recaptcha_theme === 'invisible'; |
|
69 | - } |
|
63 | + /** |
|
64 | + * @return boolean |
|
65 | + */ |
|
66 | + public function useInvisibleRecaptcha() |
|
67 | + { |
|
68 | + return $this->config->use_captcha && $this->config->recaptcha_theme === 'invisible'; |
|
69 | + } |
|
70 | 70 | |
71 | 71 | |
72 | - /** |
|
73 | - * @param array $input_settings |
|
74 | - * @return EE_Invisible_Recaptcha_Input |
|
75 | - * @throws InvalidDataTypeException |
|
76 | - * @throws InvalidInterfaceException |
|
77 | - * @throws InvalidArgumentException |
|
78 | - * @throws DomainException |
|
79 | - */ |
|
80 | - public function getInput(array $input_settings = array()) |
|
81 | - { |
|
82 | - return new EE_Invisible_Recaptcha_Input( |
|
83 | - $input_settings, |
|
84 | - $this->config |
|
85 | - ); |
|
86 | - } |
|
72 | + /** |
|
73 | + * @param array $input_settings |
|
74 | + * @return EE_Invisible_Recaptcha_Input |
|
75 | + * @throws InvalidDataTypeException |
|
76 | + * @throws InvalidInterfaceException |
|
77 | + * @throws InvalidArgumentException |
|
78 | + * @throws DomainException |
|
79 | + */ |
|
80 | + public function getInput(array $input_settings = array()) |
|
81 | + { |
|
82 | + return new EE_Invisible_Recaptcha_Input( |
|
83 | + $input_settings, |
|
84 | + $this->config |
|
85 | + ); |
|
86 | + } |
|
87 | 87 | |
88 | 88 | |
89 | - /** |
|
90 | - * @param array $input_settings |
|
91 | - * @return string |
|
92 | - * @throws EE_Error |
|
93 | - * @throws InvalidDataTypeException |
|
94 | - * @throws InvalidInterfaceException |
|
95 | - * @throws InvalidArgumentException |
|
96 | - * @throws DomainException |
|
97 | - */ |
|
98 | - public function getInputHtml(array $input_settings = array()) |
|
99 | - { |
|
100 | - return $this->getInput($input_settings)->get_html_for_input(); |
|
101 | - } |
|
89 | + /** |
|
90 | + * @param array $input_settings |
|
91 | + * @return string |
|
92 | + * @throws EE_Error |
|
93 | + * @throws InvalidDataTypeException |
|
94 | + * @throws InvalidInterfaceException |
|
95 | + * @throws InvalidArgumentException |
|
96 | + * @throws DomainException |
|
97 | + */ |
|
98 | + public function getInputHtml(array $input_settings = array()) |
|
99 | + { |
|
100 | + return $this->getInput($input_settings)->get_html_for_input(); |
|
101 | + } |
|
102 | 102 | |
103 | 103 | |
104 | - /** |
|
105 | - * @param EE_Form_Section_Proper $form |
|
106 | - * @param array $input_settings |
|
107 | - * @throws EE_Error |
|
108 | - * @throws InvalidArgumentException |
|
109 | - * @throws InvalidDataTypeException |
|
110 | - * @throws InvalidInterfaceException |
|
111 | - * @throws DomainException |
|
112 | - */ |
|
113 | - public function addToFormSection(EE_Form_Section_Proper $form, array $input_settings = array()) |
|
114 | - { |
|
115 | - $form->add_subsections( |
|
116 | - array( |
|
117 | - 'espresso_recaptcha' => $this->getInput($input_settings), |
|
118 | - ), |
|
119 | - null, |
|
120 | - false |
|
121 | - ); |
|
122 | - } |
|
104 | + /** |
|
105 | + * @param EE_Form_Section_Proper $form |
|
106 | + * @param array $input_settings |
|
107 | + * @throws EE_Error |
|
108 | + * @throws InvalidArgumentException |
|
109 | + * @throws InvalidDataTypeException |
|
110 | + * @throws InvalidInterfaceException |
|
111 | + * @throws DomainException |
|
112 | + */ |
|
113 | + public function addToFormSection(EE_Form_Section_Proper $form, array $input_settings = array()) |
|
114 | + { |
|
115 | + $form->add_subsections( |
|
116 | + array( |
|
117 | + 'espresso_recaptcha' => $this->getInput($input_settings), |
|
118 | + ), |
|
119 | + null, |
|
120 | + false |
|
121 | + ); |
|
122 | + } |
|
123 | 123 | |
124 | 124 | |
125 | - /** |
|
126 | - * @param RequestInterface $request |
|
127 | - * @return boolean |
|
128 | - * @throws InvalidArgumentException |
|
129 | - * @throws InvalidDataTypeException |
|
130 | - * @throws InvalidInterfaceException |
|
131 | - * @throws RuntimeException |
|
132 | - */ |
|
133 | - public function verifyToken(RequestInterface $request) |
|
134 | - { |
|
135 | - static $previous_recaptcha_response = array(); |
|
136 | - $grecaptcha_response = $request->getRequestParam('g-recaptcha-response'); |
|
137 | - if ($grecaptcha_response === null) { |
|
138 | - $this->generateError( |
|
139 | - sprintf( |
|
140 | - esc_html__( |
|
141 | - 'The "%1$s" parameter is missing.', |
|
142 | - 'event_espresso' |
|
143 | - ), |
|
144 | - 'g-recaptcha-response' |
|
145 | - ), |
|
146 | - true |
|
147 | - ); |
|
148 | - return false; |
|
149 | - } |
|
150 | - // if this token has already been verified, then return previous response |
|
151 | - if (isset($previous_recaptcha_response[ $grecaptcha_response ])) { |
|
152 | - return $previous_recaptcha_response[ $grecaptcha_response ]; |
|
153 | - } |
|
154 | - // will update to true if everything passes |
|
155 | - $previous_recaptcha_response[ $grecaptcha_response ] = false; |
|
156 | - $response = wp_safe_remote_post( |
|
157 | - InvisibleRecaptcha::URL_GOOGLE_RECAPTCHA_API, |
|
158 | - array( |
|
159 | - 'body' => array( |
|
160 | - 'secret' => $this->config->recaptcha_privatekey, |
|
161 | - 'response' => $grecaptcha_response, |
|
162 | - 'remoteip' => $request->ipAddress(), |
|
163 | - ), |
|
164 | - ) |
|
165 | - ); |
|
166 | - if ($response instanceof WP_Error) { |
|
167 | - $this->generateError($response->get_error_messages()); |
|
168 | - return false; |
|
169 | - } |
|
170 | - $results = json_decode(wp_remote_retrieve_body($response), true); |
|
171 | - if (filter_var($results['success'], FILTER_VALIDATE_BOOLEAN) !== true) { |
|
172 | - $errors = array_map( |
|
173 | - array($this, 'getErrorCode'), |
|
174 | - $results['error-codes'] |
|
175 | - ); |
|
176 | - if (isset($results['challenge_ts'])) { |
|
177 | - $errors[] = 'challenge timestamp: ' . $results['challenge_ts'] . '.'; |
|
178 | - } |
|
179 | - $this->generateError(implode(' ', $errors), true); |
|
180 | - } |
|
181 | - $previous_recaptcha_response[ $grecaptcha_response ] = true; |
|
182 | - add_action('shutdown', array($this, 'setSessionData')); |
|
183 | - return true; |
|
184 | - } |
|
125 | + /** |
|
126 | + * @param RequestInterface $request |
|
127 | + * @return boolean |
|
128 | + * @throws InvalidArgumentException |
|
129 | + * @throws InvalidDataTypeException |
|
130 | + * @throws InvalidInterfaceException |
|
131 | + * @throws RuntimeException |
|
132 | + */ |
|
133 | + public function verifyToken(RequestInterface $request) |
|
134 | + { |
|
135 | + static $previous_recaptcha_response = array(); |
|
136 | + $grecaptcha_response = $request->getRequestParam('g-recaptcha-response'); |
|
137 | + if ($grecaptcha_response === null) { |
|
138 | + $this->generateError( |
|
139 | + sprintf( |
|
140 | + esc_html__( |
|
141 | + 'The "%1$s" parameter is missing.', |
|
142 | + 'event_espresso' |
|
143 | + ), |
|
144 | + 'g-recaptcha-response' |
|
145 | + ), |
|
146 | + true |
|
147 | + ); |
|
148 | + return false; |
|
149 | + } |
|
150 | + // if this token has already been verified, then return previous response |
|
151 | + if (isset($previous_recaptcha_response[ $grecaptcha_response ])) { |
|
152 | + return $previous_recaptcha_response[ $grecaptcha_response ]; |
|
153 | + } |
|
154 | + // will update to true if everything passes |
|
155 | + $previous_recaptcha_response[ $grecaptcha_response ] = false; |
|
156 | + $response = wp_safe_remote_post( |
|
157 | + InvisibleRecaptcha::URL_GOOGLE_RECAPTCHA_API, |
|
158 | + array( |
|
159 | + 'body' => array( |
|
160 | + 'secret' => $this->config->recaptcha_privatekey, |
|
161 | + 'response' => $grecaptcha_response, |
|
162 | + 'remoteip' => $request->ipAddress(), |
|
163 | + ), |
|
164 | + ) |
|
165 | + ); |
|
166 | + if ($response instanceof WP_Error) { |
|
167 | + $this->generateError($response->get_error_messages()); |
|
168 | + return false; |
|
169 | + } |
|
170 | + $results = json_decode(wp_remote_retrieve_body($response), true); |
|
171 | + if (filter_var($results['success'], FILTER_VALIDATE_BOOLEAN) !== true) { |
|
172 | + $errors = array_map( |
|
173 | + array($this, 'getErrorCode'), |
|
174 | + $results['error-codes'] |
|
175 | + ); |
|
176 | + if (isset($results['challenge_ts'])) { |
|
177 | + $errors[] = 'challenge timestamp: ' . $results['challenge_ts'] . '.'; |
|
178 | + } |
|
179 | + $this->generateError(implode(' ', $errors), true); |
|
180 | + } |
|
181 | + $previous_recaptcha_response[ $grecaptcha_response ] = true; |
|
182 | + add_action('shutdown', array($this, 'setSessionData')); |
|
183 | + return true; |
|
184 | + } |
|
185 | 185 | |
186 | 186 | |
187 | - /** |
|
188 | - * @param string $error_response |
|
189 | - * @param bool $show_errors |
|
190 | - * @return void |
|
191 | - * @throws RuntimeException |
|
192 | - */ |
|
193 | - public function generateError($error_response = '', $show_errors = false) |
|
194 | - { |
|
195 | - throw new RuntimeException( |
|
196 | - sprintf( |
|
197 | - esc_html__( |
|
198 | - 'We\'re sorry but an attempt to verify the form\'s reCAPTCHA has failed. %1$s %2$s Please try again.', |
|
199 | - 'event_espresso' |
|
200 | - ), |
|
201 | - '<br />', |
|
202 | - $show_errors || current_user_can('manage_options') ? $error_response : '' |
|
203 | - ) |
|
204 | - ); |
|
205 | - } |
|
187 | + /** |
|
188 | + * @param string $error_response |
|
189 | + * @param bool $show_errors |
|
190 | + * @return void |
|
191 | + * @throws RuntimeException |
|
192 | + */ |
|
193 | + public function generateError($error_response = '', $show_errors = false) |
|
194 | + { |
|
195 | + throw new RuntimeException( |
|
196 | + sprintf( |
|
197 | + esc_html__( |
|
198 | + 'We\'re sorry but an attempt to verify the form\'s reCAPTCHA has failed. %1$s %2$s Please try again.', |
|
199 | + 'event_espresso' |
|
200 | + ), |
|
201 | + '<br />', |
|
202 | + $show_errors || current_user_can('manage_options') ? $error_response : '' |
|
203 | + ) |
|
204 | + ); |
|
205 | + } |
|
206 | 206 | |
207 | 207 | |
208 | - /** |
|
209 | - * @param string $error_code |
|
210 | - * @return string |
|
211 | - */ |
|
212 | - public function getErrorCode(&$error_code) |
|
213 | - { |
|
214 | - $error_codes = array( |
|
215 | - 'missing-input-secret' => 'The secret parameter is missing.', |
|
216 | - 'invalid-input-secret' => 'The secret parameter is invalid or malformed.', |
|
217 | - 'missing-input-response' => 'The response parameter is missing.', |
|
218 | - 'invalid-input-response' => 'The response parameter is invalid or malformed.', |
|
219 | - 'bad-request' => 'The request is invalid or malformed.', |
|
220 | - 'timeout-or-duplicate' => 'The request took too long to be sent or was a duplicate of a previous request.', |
|
221 | - ); |
|
222 | - return isset($error_codes[ $error_code ]) ? $error_codes[ $error_code ] : ''; |
|
223 | - } |
|
208 | + /** |
|
209 | + * @param string $error_code |
|
210 | + * @return string |
|
211 | + */ |
|
212 | + public function getErrorCode(&$error_code) |
|
213 | + { |
|
214 | + $error_codes = array( |
|
215 | + 'missing-input-secret' => 'The secret parameter is missing.', |
|
216 | + 'invalid-input-secret' => 'The secret parameter is invalid or malformed.', |
|
217 | + 'missing-input-response' => 'The response parameter is missing.', |
|
218 | + 'invalid-input-response' => 'The response parameter is invalid or malformed.', |
|
219 | + 'bad-request' => 'The request is invalid or malformed.', |
|
220 | + 'timeout-or-duplicate' => 'The request took too long to be sent or was a duplicate of a previous request.', |
|
221 | + ); |
|
222 | + return isset($error_codes[ $error_code ]) ? $error_codes[ $error_code ] : ''; |
|
223 | + } |
|
224 | 224 | |
225 | 225 | |
226 | - /** |
|
227 | - * @return array |
|
228 | - * @throws InvalidInterfaceException |
|
229 | - * @throws InvalidDataTypeException |
|
230 | - * @throws InvalidArgumentException |
|
231 | - */ |
|
232 | - public function getLocalizedVars() |
|
233 | - { |
|
234 | - return (array) apply_filters( |
|
235 | - 'FHEE__EventEspresso_caffeinated_modules_recaptcha_invisible_InvisibleRecaptcha__getLocalizedVars__localized_vars', |
|
236 | - array( |
|
237 | - 'siteKey' => $this->config->recaptcha_publickey, |
|
238 | - 'recaptcha_passed' => $this->recaptchaPassed(), |
|
239 | - 'wp_debug' => WP_DEBUG, |
|
240 | - 'disable_submit' => defined('EE_EVENT_QUEUE_BASE_URL'), |
|
241 | - ) |
|
242 | - ); |
|
243 | - } |
|
226 | + /** |
|
227 | + * @return array |
|
228 | + * @throws InvalidInterfaceException |
|
229 | + * @throws InvalidDataTypeException |
|
230 | + * @throws InvalidArgumentException |
|
231 | + */ |
|
232 | + public function getLocalizedVars() |
|
233 | + { |
|
234 | + return (array) apply_filters( |
|
235 | + 'FHEE__EventEspresso_caffeinated_modules_recaptcha_invisible_InvisibleRecaptcha__getLocalizedVars__localized_vars', |
|
236 | + array( |
|
237 | + 'siteKey' => $this->config->recaptcha_publickey, |
|
238 | + 'recaptcha_passed' => $this->recaptchaPassed(), |
|
239 | + 'wp_debug' => WP_DEBUG, |
|
240 | + 'disable_submit' => defined('EE_EVENT_QUEUE_BASE_URL'), |
|
241 | + ) |
|
242 | + ); |
|
243 | + } |
|
244 | 244 | |
245 | 245 | |
246 | - /** |
|
247 | - * @return boolean |
|
248 | - * @throws InvalidInterfaceException |
|
249 | - * @throws InvalidDataTypeException |
|
250 | - * @throws InvalidArgumentException |
|
251 | - */ |
|
252 | - public function recaptchaPassed() |
|
253 | - { |
|
254 | - if ($this->recaptcha_passed !== null) { |
|
255 | - return $this->recaptcha_passed; |
|
256 | - } |
|
257 | - // logged in means you have already passed a turing test of sorts |
|
258 | - if ($this->useInvisibleRecaptcha() === false || is_user_logged_in()) { |
|
259 | - $this->recaptcha_passed = true; |
|
260 | - return $this->recaptcha_passed; |
|
261 | - } |
|
262 | - // was test already passed? |
|
263 | - $this->recaptcha_passed = filter_var( |
|
264 | - $this->session->get_session_data( |
|
265 | - InvisibleRecaptcha::SESSION_DATA_KEY_RECAPTCHA_PASSED |
|
266 | - ), |
|
267 | - FILTER_VALIDATE_BOOLEAN |
|
268 | - ); |
|
269 | - return $this->recaptcha_passed; |
|
270 | - } |
|
246 | + /** |
|
247 | + * @return boolean |
|
248 | + * @throws InvalidInterfaceException |
|
249 | + * @throws InvalidDataTypeException |
|
250 | + * @throws InvalidArgumentException |
|
251 | + */ |
|
252 | + public function recaptchaPassed() |
|
253 | + { |
|
254 | + if ($this->recaptcha_passed !== null) { |
|
255 | + return $this->recaptcha_passed; |
|
256 | + } |
|
257 | + // logged in means you have already passed a turing test of sorts |
|
258 | + if ($this->useInvisibleRecaptcha() === false || is_user_logged_in()) { |
|
259 | + $this->recaptcha_passed = true; |
|
260 | + return $this->recaptcha_passed; |
|
261 | + } |
|
262 | + // was test already passed? |
|
263 | + $this->recaptcha_passed = filter_var( |
|
264 | + $this->session->get_session_data( |
|
265 | + InvisibleRecaptcha::SESSION_DATA_KEY_RECAPTCHA_PASSED |
|
266 | + ), |
|
267 | + FILTER_VALIDATE_BOOLEAN |
|
268 | + ); |
|
269 | + return $this->recaptcha_passed; |
|
270 | + } |
|
271 | 271 | |
272 | 272 | |
273 | - /** |
|
274 | - * @throws InvalidArgumentException |
|
275 | - * @throws InvalidDataTypeException |
|
276 | - * @throws InvalidInterfaceException |
|
277 | - */ |
|
278 | - public function setSessionData() |
|
279 | - { |
|
280 | - $this->session->set_session_data( |
|
281 | - array(InvisibleRecaptcha::SESSION_DATA_KEY_RECAPTCHA_PASSED => true) |
|
282 | - ); |
|
283 | - } |
|
273 | + /** |
|
274 | + * @throws InvalidArgumentException |
|
275 | + * @throws InvalidDataTypeException |
|
276 | + * @throws InvalidInterfaceException |
|
277 | + */ |
|
278 | + public function setSessionData() |
|
279 | + { |
|
280 | + $this->session->set_session_data( |
|
281 | + array(InvisibleRecaptcha::SESSION_DATA_KEY_RECAPTCHA_PASSED => true) |
|
282 | + ); |
|
283 | + } |
|
284 | 284 | } |
@@ -148,11 +148,11 @@ discard block |
||
148 | 148 | return false; |
149 | 149 | } |
150 | 150 | // if this token has already been verified, then return previous response |
151 | - if (isset($previous_recaptcha_response[ $grecaptcha_response ])) { |
|
152 | - return $previous_recaptcha_response[ $grecaptcha_response ]; |
|
151 | + if (isset($previous_recaptcha_response[$grecaptcha_response])) { |
|
152 | + return $previous_recaptcha_response[$grecaptcha_response]; |
|
153 | 153 | } |
154 | 154 | // will update to true if everything passes |
155 | - $previous_recaptcha_response[ $grecaptcha_response ] = false; |
|
155 | + $previous_recaptcha_response[$grecaptcha_response] = false; |
|
156 | 156 | $response = wp_safe_remote_post( |
157 | 157 | InvisibleRecaptcha::URL_GOOGLE_RECAPTCHA_API, |
158 | 158 | array( |
@@ -169,16 +169,16 @@ discard block |
||
169 | 169 | } |
170 | 170 | $results = json_decode(wp_remote_retrieve_body($response), true); |
171 | 171 | if (filter_var($results['success'], FILTER_VALIDATE_BOOLEAN) !== true) { |
172 | - $errors = array_map( |
|
172 | + $errors = array_map( |
|
173 | 173 | array($this, 'getErrorCode'), |
174 | 174 | $results['error-codes'] |
175 | 175 | ); |
176 | 176 | if (isset($results['challenge_ts'])) { |
177 | - $errors[] = 'challenge timestamp: ' . $results['challenge_ts'] . '.'; |
|
177 | + $errors[] = 'challenge timestamp: '.$results['challenge_ts'].'.'; |
|
178 | 178 | } |
179 | 179 | $this->generateError(implode(' ', $errors), true); |
180 | 180 | } |
181 | - $previous_recaptcha_response[ $grecaptcha_response ] = true; |
|
181 | + $previous_recaptcha_response[$grecaptcha_response] = true; |
|
182 | 182 | add_action('shutdown', array($this, 'setSessionData')); |
183 | 183 | return true; |
184 | 184 | } |
@@ -219,7 +219,7 @@ discard block |
||
219 | 219 | 'bad-request' => 'The request is invalid or malformed.', |
220 | 220 | 'timeout-or-duplicate' => 'The request took too long to be sent or was a duplicate of a previous request.', |
221 | 221 | ); |
222 | - return isset($error_codes[ $error_code ]) ? $error_codes[ $error_code ] : ''; |
|
222 | + return isset($error_codes[$error_code]) ? $error_codes[$error_code] : ''; |
|
223 | 223 | } |
224 | 224 | |
225 | 225 |
@@ -13,339 +13,339 @@ |
||
13 | 13 | { |
14 | 14 | |
15 | 15 | |
16 | - /** |
|
17 | - * @return Messages_Admin_Page |
|
18 | - */ |
|
19 | - public function get_admin_page() |
|
20 | - { |
|
21 | - return $this->_admin_page; |
|
22 | - } |
|
23 | - |
|
24 | - |
|
25 | - /** |
|
26 | - * Setup data object |
|
27 | - */ |
|
28 | - protected function _setup_data() |
|
29 | - { |
|
30 | - $this->_data = $this->get_admin_page()->get_message_templates( |
|
31 | - $this->_per_page, |
|
32 | - $this->_view, |
|
33 | - false |
|
34 | - ); |
|
35 | - $this->_all_data_count = $this->get_admin_page()->get_message_templates( |
|
36 | - $this->_per_page, |
|
37 | - $this->_view, |
|
38 | - true, |
|
39 | - true |
|
40 | - ); |
|
41 | - } |
|
42 | - |
|
43 | - |
|
44 | - /** |
|
45 | - * Set internal properties |
|
46 | - */ |
|
47 | - protected function _set_properties() |
|
48 | - { |
|
49 | - $this->_wp_list_args = array( |
|
50 | - 'singular' => esc_html__('Message Template Group', 'event_espresso'), |
|
51 | - 'plural' => esc_html__('Message Template', 'event_espresso'), |
|
52 | - 'ajax' => true, // for now, |
|
53 | - 'screen' => $this->get_admin_page()->get_current_screen()->id, |
|
54 | - ); |
|
55 | - $this->_columns = array( |
|
56 | - // 'cb' => '<input type="checkbox" />', //no deleting default (global) templates! |
|
57 | - 'message_type' => esc_html__('Message Type', 'event_espresso'), |
|
58 | - 'messenger' => esc_html__('Messenger', 'event_espresso'), |
|
59 | - 'description' => esc_html__('Description', 'event_espresso'), |
|
60 | - ); |
|
61 | - |
|
62 | - $this->_sortable_columns = array( |
|
63 | - 'messenger' => array('MTP_messenger' => true), |
|
64 | - ); |
|
65 | - |
|
66 | - $this->_hidden_columns = array(); |
|
67 | - } |
|
68 | - |
|
69 | - |
|
70 | - /** |
|
71 | - * Overriding the single_row method from parent to verify whether the $item has an accessible |
|
72 | - * message_type or messenger object before generating the row. |
|
73 | - * |
|
74 | - * @param EE_Message_Template_Group $item |
|
75 | - * @return string |
|
76 | - * @throws EE_Error |
|
77 | - */ |
|
78 | - public function single_row($item) |
|
79 | - { |
|
80 | - $message_type = $item->message_type_obj(); |
|
81 | - $messenger = $item->messenger_obj(); |
|
82 | - |
|
83 | - if (! $message_type instanceof EE_message_type || ! $messenger instanceof EE_messenger) { |
|
84 | - echo ''; |
|
85 | - return; |
|
86 | - } |
|
87 | - |
|
88 | - parent::single_row($item); |
|
89 | - } |
|
90 | - |
|
91 | - |
|
92 | - /** |
|
93 | - * @return array |
|
94 | - * @throws EE_Error |
|
95 | - */ |
|
96 | - protected function _get_table_filters() |
|
97 | - { |
|
98 | - $filters = array(); |
|
99 | - |
|
100 | - // get select inputs |
|
101 | - $select_inputs = array( |
|
102 | - $this->_get_messengers_dropdown_filter(), |
|
103 | - $this->_get_message_types_dropdown_filter(), |
|
104 | - ); |
|
105 | - |
|
106 | - // set filters to select inputs if they aren't empty |
|
107 | - foreach ($select_inputs as $select_input) { |
|
108 | - if ($select_input) { |
|
109 | - $filters[] = $select_input; |
|
110 | - } |
|
111 | - } |
|
112 | - return $filters; |
|
113 | - } |
|
114 | - |
|
115 | - /** |
|
116 | - * We're just removing the search box for message templates, not needed. |
|
117 | - * |
|
118 | - * @param string $text |
|
119 | - * @param string $input_id |
|
120 | - * @return string ; |
|
121 | - */ |
|
122 | - public function search_box($text, $input_id) |
|
123 | - { |
|
124 | - return ''; |
|
125 | - } |
|
126 | - |
|
127 | - |
|
128 | - /** |
|
129 | - * Add counts to the _views property |
|
130 | - */ |
|
131 | - protected function _add_view_counts() |
|
132 | - { |
|
133 | - foreach ($this->_views as $view => $args) { |
|
134 | - $this->_views[ $view ]['count'] = $this->get_admin_page()->get_message_templates( |
|
135 | - $this->_per_page, |
|
136 | - $view, |
|
137 | - true, |
|
138 | - true |
|
139 | - ); |
|
140 | - } |
|
141 | - } |
|
142 | - |
|
143 | - |
|
144 | - /** |
|
145 | - * @param EE_Message_Template_Group $item |
|
146 | - * @return string |
|
147 | - */ |
|
148 | - public function column_cb($item) |
|
149 | - { |
|
150 | - return ''; |
|
151 | - } |
|
152 | - |
|
153 | - |
|
154 | - /** |
|
155 | - * @param EE_Message_Template_Group $item |
|
156 | - * @return string |
|
157 | - * @throws EE_Error |
|
158 | - */ |
|
159 | - public function column_description($item) |
|
160 | - { |
|
161 | - return '<p>' . $item->message_type_obj()->description . '</p>'; |
|
162 | - } |
|
163 | - |
|
164 | - |
|
165 | - /** |
|
166 | - * @param EE_Message_Template_Group $item |
|
167 | - * @return string |
|
168 | - * @throws EE_Error |
|
169 | - */ |
|
170 | - public function column_messenger($item) |
|
171 | - { |
|
172 | - // Return the name contents |
|
173 | - return sprintf( |
|
174 | - '%1$s <span style="color:silver">(id:%2$s)</span><br />%3$s', |
|
175 | - /* $1%s */ |
|
176 | - ucwords($item->messenger_obj()->label['singular']), |
|
177 | - /* $2%s */ |
|
178 | - $item->GRP_ID(), |
|
179 | - /* %4$s */ |
|
180 | - $this->_get_context_links($item) |
|
181 | - ); |
|
182 | - } |
|
183 | - |
|
184 | - /** |
|
185 | - * column_message_type |
|
186 | - * |
|
187 | - * @param EE_Message_Template_Group $item message info for the row |
|
188 | - * @return string message_type name |
|
189 | - * @throws EE_Error |
|
190 | - */ |
|
191 | - public function column_message_type($item) |
|
192 | - { |
|
193 | - return ucwords($item->message_type_obj()->label['singular']); |
|
194 | - } |
|
195 | - |
|
196 | - |
|
197 | - /** |
|
198 | - * Generate dropdown filter select input for messengers |
|
199 | - * |
|
200 | - * @param bool $global |
|
201 | - * @return string |
|
202 | - * @throws EE_Error |
|
203 | - */ |
|
204 | - protected function _get_messengers_dropdown_filter($global = true) |
|
205 | - { |
|
206 | - $messenger_options = array(); |
|
207 | - $active_message_template_groups_grouped_by_messenger = EEM_Message_Template_Group::instance()->get_all( |
|
208 | - array( |
|
209 | - array( |
|
210 | - 'MTP_is_active' => true, |
|
211 | - 'MTP_is_global' => $global, |
|
212 | - ), |
|
213 | - 'group_by' => 'MTP_messenger', |
|
214 | - ) |
|
215 | - ); |
|
216 | - |
|
217 | - foreach ($active_message_template_groups_grouped_by_messenger as $active_message_template_group) { |
|
218 | - if ($active_message_template_group instanceof EE_Message_Template_Group) { |
|
219 | - $messenger = $active_message_template_group->messenger_obj(); |
|
220 | - $messenger_label = $messenger instanceof EE_messenger |
|
221 | - ? $messenger->label['singular'] |
|
222 | - : $active_message_template_group->messenger(); |
|
223 | - $messenger_options[ $active_message_template_group->messenger() ] = ucwords($messenger_label); |
|
224 | - } |
|
225 | - } |
|
226 | - return $this->get_admin_page()->get_messengers_select_input($messenger_options); |
|
227 | - } |
|
228 | - |
|
229 | - |
|
230 | - /** |
|
231 | - * Generate dropdown filter select input for message types |
|
232 | - * |
|
233 | - * @param bool $global |
|
234 | - * @return string |
|
235 | - * @throws EE_Error |
|
236 | - */ |
|
237 | - protected function _get_message_types_dropdown_filter($global = true) |
|
238 | - { |
|
239 | - $message_type_options = array(); |
|
240 | - $active_message_template_groups_grouped_by_message_type = EEM_Message_Template_Group::instance()->get_all( |
|
241 | - array( |
|
242 | - array( |
|
243 | - 'MTP_is_active' => true, |
|
244 | - 'MTP_is_global' => true, |
|
245 | - ), |
|
246 | - 'group_by' => 'MTP_message_type', |
|
247 | - ) |
|
248 | - ); |
|
249 | - |
|
250 | - foreach ($active_message_template_groups_grouped_by_message_type as $active_message_template_group) { |
|
251 | - if ($active_message_template_group instanceof EE_Message_Template_Group) { |
|
252 | - $message_type = $active_message_template_group->message_type_obj(); |
|
253 | - $message_type_label = $message_type instanceof EE_message_type |
|
254 | - ? $message_type->label['singular'] |
|
255 | - : $active_message_template_group->message_type(); |
|
256 | - $message_type_options[ $active_message_template_group->message_type() ] = ucwords($message_type_label); |
|
257 | - } |
|
258 | - } |
|
259 | - return $this->get_admin_page()->get_message_types_select_input($message_type_options); |
|
260 | - } |
|
261 | - |
|
262 | - |
|
263 | - /** |
|
264 | - * Return the edit url for the message template group. |
|
265 | - * |
|
266 | - * @param EE_Message_Template_Group $item |
|
267 | - * @return string |
|
268 | - * @throws EE_Error |
|
269 | - */ |
|
270 | - protected function _get_edit_url(EE_Message_Template_Group $item) |
|
271 | - { |
|
272 | - $edit_url = ''; |
|
273 | - // edit link but only if item isn't trashed. |
|
274 | - if (! $item->get('MTP_deleted') |
|
275 | - && EE_Registry::instance()->CAP->current_user_can( |
|
276 | - 'ee_edit_message', |
|
277 | - 'espresso_messages_edit_message_template', |
|
278 | - $item->ID() |
|
279 | - )) { |
|
280 | - $edit_url = EE_Admin_Page::add_query_args_and_nonce( |
|
281 | - array( |
|
282 | - 'action' => 'edit_message_template', |
|
283 | - 'id' => $item->GRP_ID(), |
|
284 | - ), |
|
285 | - EE_MSG_ADMIN_URL |
|
286 | - ); |
|
287 | - } |
|
288 | - return $edit_url; |
|
289 | - } |
|
290 | - |
|
291 | - |
|
292 | - /** |
|
293 | - * Get the context link string for the messenger column. |
|
294 | - * |
|
295 | - * @param EE_Message_Template_Group $item |
|
296 | - * @return string |
|
297 | - * @throws EE_Error |
|
298 | - */ |
|
299 | - protected function _get_context_links(EE_Message_Template_Group $item) |
|
300 | - { |
|
301 | - // first check if we even show the context links or not. |
|
302 | - if (! EE_Registry::instance()->CAP->current_user_can( |
|
303 | - 'ee_edit_message', |
|
304 | - 'espresso_messages_edit_message_template', |
|
305 | - $item->ID() |
|
306 | - ) |
|
307 | - || $item->get('MTP_deleted') |
|
308 | - ) { |
|
309 | - return ''; |
|
310 | - } |
|
311 | - // we want to display the contexts in here so we need to set them up |
|
312 | - $c_label = $item->context_label(); |
|
313 | - $c_configs = $item->contexts_config(); |
|
314 | - $ctxt = array(); |
|
315 | - $context_templates = $item->context_templates(); |
|
316 | - foreach ($context_templates as $context => $template_fields) { |
|
317 | - $mtp_to = ! empty($context_templates[ $context ]['to']) |
|
318 | - && $context_templates[ $context ]['to'] instanceof EE_Message_Template |
|
319 | - ? $context_templates[ $context ]['to']->get('MTP_content') |
|
320 | - : null; |
|
321 | - $inactive_class = ( |
|
322 | - empty($mtp_to) |
|
323 | - && ! empty($context_templates[ $context ]['to']) |
|
324 | - ) |
|
325 | - || ! $item->is_context_active($context) |
|
326 | - ? ' mtp-inactive' |
|
327 | - : ''; |
|
328 | - $context_title = sprintf( |
|
329 | - /* translators: Placeholder represents the context label. Example "Edit Event Admin" */ |
|
330 | - esc_html__('Edit %1$s', 'event_espresso'), |
|
331 | - ucwords($c_configs[ $context ]['label']) |
|
332 | - ); |
|
333 | - $edit_link = EE_Admin_Page::add_query_args_and_nonce( |
|
334 | - array( |
|
335 | - 'action' => 'edit_message_template', |
|
336 | - 'id' => $item->GRP_ID(), |
|
337 | - 'context' => $context, |
|
338 | - ), |
|
339 | - EE_MSG_ADMIN_URL |
|
340 | - ); |
|
341 | - $ctxt[] = '<a' |
|
342 | - . ' href="' . $edit_link . '"' |
|
343 | - . ' class="' . $item->message_type() . '-' . $context . '-edit-link' . $inactive_class . '"' |
|
344 | - . ' title="' . esc_attr__('Edit Context', 'event_espresso') . '">' |
|
345 | - . $context_title |
|
346 | - . '</a>'; |
|
347 | - } |
|
348 | - |
|
349 | - return sprintf('<strong>%s:</strong> ', ucwords($c_label['plural'])) . implode(' | ', $ctxt); |
|
350 | - } |
|
16 | + /** |
|
17 | + * @return Messages_Admin_Page |
|
18 | + */ |
|
19 | + public function get_admin_page() |
|
20 | + { |
|
21 | + return $this->_admin_page; |
|
22 | + } |
|
23 | + |
|
24 | + |
|
25 | + /** |
|
26 | + * Setup data object |
|
27 | + */ |
|
28 | + protected function _setup_data() |
|
29 | + { |
|
30 | + $this->_data = $this->get_admin_page()->get_message_templates( |
|
31 | + $this->_per_page, |
|
32 | + $this->_view, |
|
33 | + false |
|
34 | + ); |
|
35 | + $this->_all_data_count = $this->get_admin_page()->get_message_templates( |
|
36 | + $this->_per_page, |
|
37 | + $this->_view, |
|
38 | + true, |
|
39 | + true |
|
40 | + ); |
|
41 | + } |
|
42 | + |
|
43 | + |
|
44 | + /** |
|
45 | + * Set internal properties |
|
46 | + */ |
|
47 | + protected function _set_properties() |
|
48 | + { |
|
49 | + $this->_wp_list_args = array( |
|
50 | + 'singular' => esc_html__('Message Template Group', 'event_espresso'), |
|
51 | + 'plural' => esc_html__('Message Template', 'event_espresso'), |
|
52 | + 'ajax' => true, // for now, |
|
53 | + 'screen' => $this->get_admin_page()->get_current_screen()->id, |
|
54 | + ); |
|
55 | + $this->_columns = array( |
|
56 | + // 'cb' => '<input type="checkbox" />', //no deleting default (global) templates! |
|
57 | + 'message_type' => esc_html__('Message Type', 'event_espresso'), |
|
58 | + 'messenger' => esc_html__('Messenger', 'event_espresso'), |
|
59 | + 'description' => esc_html__('Description', 'event_espresso'), |
|
60 | + ); |
|
61 | + |
|
62 | + $this->_sortable_columns = array( |
|
63 | + 'messenger' => array('MTP_messenger' => true), |
|
64 | + ); |
|
65 | + |
|
66 | + $this->_hidden_columns = array(); |
|
67 | + } |
|
68 | + |
|
69 | + |
|
70 | + /** |
|
71 | + * Overriding the single_row method from parent to verify whether the $item has an accessible |
|
72 | + * message_type or messenger object before generating the row. |
|
73 | + * |
|
74 | + * @param EE_Message_Template_Group $item |
|
75 | + * @return string |
|
76 | + * @throws EE_Error |
|
77 | + */ |
|
78 | + public function single_row($item) |
|
79 | + { |
|
80 | + $message_type = $item->message_type_obj(); |
|
81 | + $messenger = $item->messenger_obj(); |
|
82 | + |
|
83 | + if (! $message_type instanceof EE_message_type || ! $messenger instanceof EE_messenger) { |
|
84 | + echo ''; |
|
85 | + return; |
|
86 | + } |
|
87 | + |
|
88 | + parent::single_row($item); |
|
89 | + } |
|
90 | + |
|
91 | + |
|
92 | + /** |
|
93 | + * @return array |
|
94 | + * @throws EE_Error |
|
95 | + */ |
|
96 | + protected function _get_table_filters() |
|
97 | + { |
|
98 | + $filters = array(); |
|
99 | + |
|
100 | + // get select inputs |
|
101 | + $select_inputs = array( |
|
102 | + $this->_get_messengers_dropdown_filter(), |
|
103 | + $this->_get_message_types_dropdown_filter(), |
|
104 | + ); |
|
105 | + |
|
106 | + // set filters to select inputs if they aren't empty |
|
107 | + foreach ($select_inputs as $select_input) { |
|
108 | + if ($select_input) { |
|
109 | + $filters[] = $select_input; |
|
110 | + } |
|
111 | + } |
|
112 | + return $filters; |
|
113 | + } |
|
114 | + |
|
115 | + /** |
|
116 | + * We're just removing the search box for message templates, not needed. |
|
117 | + * |
|
118 | + * @param string $text |
|
119 | + * @param string $input_id |
|
120 | + * @return string ; |
|
121 | + */ |
|
122 | + public function search_box($text, $input_id) |
|
123 | + { |
|
124 | + return ''; |
|
125 | + } |
|
126 | + |
|
127 | + |
|
128 | + /** |
|
129 | + * Add counts to the _views property |
|
130 | + */ |
|
131 | + protected function _add_view_counts() |
|
132 | + { |
|
133 | + foreach ($this->_views as $view => $args) { |
|
134 | + $this->_views[ $view ]['count'] = $this->get_admin_page()->get_message_templates( |
|
135 | + $this->_per_page, |
|
136 | + $view, |
|
137 | + true, |
|
138 | + true |
|
139 | + ); |
|
140 | + } |
|
141 | + } |
|
142 | + |
|
143 | + |
|
144 | + /** |
|
145 | + * @param EE_Message_Template_Group $item |
|
146 | + * @return string |
|
147 | + */ |
|
148 | + public function column_cb($item) |
|
149 | + { |
|
150 | + return ''; |
|
151 | + } |
|
152 | + |
|
153 | + |
|
154 | + /** |
|
155 | + * @param EE_Message_Template_Group $item |
|
156 | + * @return string |
|
157 | + * @throws EE_Error |
|
158 | + */ |
|
159 | + public function column_description($item) |
|
160 | + { |
|
161 | + return '<p>' . $item->message_type_obj()->description . '</p>'; |
|
162 | + } |
|
163 | + |
|
164 | + |
|
165 | + /** |
|
166 | + * @param EE_Message_Template_Group $item |
|
167 | + * @return string |
|
168 | + * @throws EE_Error |
|
169 | + */ |
|
170 | + public function column_messenger($item) |
|
171 | + { |
|
172 | + // Return the name contents |
|
173 | + return sprintf( |
|
174 | + '%1$s <span style="color:silver">(id:%2$s)</span><br />%3$s', |
|
175 | + /* $1%s */ |
|
176 | + ucwords($item->messenger_obj()->label['singular']), |
|
177 | + /* $2%s */ |
|
178 | + $item->GRP_ID(), |
|
179 | + /* %4$s */ |
|
180 | + $this->_get_context_links($item) |
|
181 | + ); |
|
182 | + } |
|
183 | + |
|
184 | + /** |
|
185 | + * column_message_type |
|
186 | + * |
|
187 | + * @param EE_Message_Template_Group $item message info for the row |
|
188 | + * @return string message_type name |
|
189 | + * @throws EE_Error |
|
190 | + */ |
|
191 | + public function column_message_type($item) |
|
192 | + { |
|
193 | + return ucwords($item->message_type_obj()->label['singular']); |
|
194 | + } |
|
195 | + |
|
196 | + |
|
197 | + /** |
|
198 | + * Generate dropdown filter select input for messengers |
|
199 | + * |
|
200 | + * @param bool $global |
|
201 | + * @return string |
|
202 | + * @throws EE_Error |
|
203 | + */ |
|
204 | + protected function _get_messengers_dropdown_filter($global = true) |
|
205 | + { |
|
206 | + $messenger_options = array(); |
|
207 | + $active_message_template_groups_grouped_by_messenger = EEM_Message_Template_Group::instance()->get_all( |
|
208 | + array( |
|
209 | + array( |
|
210 | + 'MTP_is_active' => true, |
|
211 | + 'MTP_is_global' => $global, |
|
212 | + ), |
|
213 | + 'group_by' => 'MTP_messenger', |
|
214 | + ) |
|
215 | + ); |
|
216 | + |
|
217 | + foreach ($active_message_template_groups_grouped_by_messenger as $active_message_template_group) { |
|
218 | + if ($active_message_template_group instanceof EE_Message_Template_Group) { |
|
219 | + $messenger = $active_message_template_group->messenger_obj(); |
|
220 | + $messenger_label = $messenger instanceof EE_messenger |
|
221 | + ? $messenger->label['singular'] |
|
222 | + : $active_message_template_group->messenger(); |
|
223 | + $messenger_options[ $active_message_template_group->messenger() ] = ucwords($messenger_label); |
|
224 | + } |
|
225 | + } |
|
226 | + return $this->get_admin_page()->get_messengers_select_input($messenger_options); |
|
227 | + } |
|
228 | + |
|
229 | + |
|
230 | + /** |
|
231 | + * Generate dropdown filter select input for message types |
|
232 | + * |
|
233 | + * @param bool $global |
|
234 | + * @return string |
|
235 | + * @throws EE_Error |
|
236 | + */ |
|
237 | + protected function _get_message_types_dropdown_filter($global = true) |
|
238 | + { |
|
239 | + $message_type_options = array(); |
|
240 | + $active_message_template_groups_grouped_by_message_type = EEM_Message_Template_Group::instance()->get_all( |
|
241 | + array( |
|
242 | + array( |
|
243 | + 'MTP_is_active' => true, |
|
244 | + 'MTP_is_global' => true, |
|
245 | + ), |
|
246 | + 'group_by' => 'MTP_message_type', |
|
247 | + ) |
|
248 | + ); |
|
249 | + |
|
250 | + foreach ($active_message_template_groups_grouped_by_message_type as $active_message_template_group) { |
|
251 | + if ($active_message_template_group instanceof EE_Message_Template_Group) { |
|
252 | + $message_type = $active_message_template_group->message_type_obj(); |
|
253 | + $message_type_label = $message_type instanceof EE_message_type |
|
254 | + ? $message_type->label['singular'] |
|
255 | + : $active_message_template_group->message_type(); |
|
256 | + $message_type_options[ $active_message_template_group->message_type() ] = ucwords($message_type_label); |
|
257 | + } |
|
258 | + } |
|
259 | + return $this->get_admin_page()->get_message_types_select_input($message_type_options); |
|
260 | + } |
|
261 | + |
|
262 | + |
|
263 | + /** |
|
264 | + * Return the edit url for the message template group. |
|
265 | + * |
|
266 | + * @param EE_Message_Template_Group $item |
|
267 | + * @return string |
|
268 | + * @throws EE_Error |
|
269 | + */ |
|
270 | + protected function _get_edit_url(EE_Message_Template_Group $item) |
|
271 | + { |
|
272 | + $edit_url = ''; |
|
273 | + // edit link but only if item isn't trashed. |
|
274 | + if (! $item->get('MTP_deleted') |
|
275 | + && EE_Registry::instance()->CAP->current_user_can( |
|
276 | + 'ee_edit_message', |
|
277 | + 'espresso_messages_edit_message_template', |
|
278 | + $item->ID() |
|
279 | + )) { |
|
280 | + $edit_url = EE_Admin_Page::add_query_args_and_nonce( |
|
281 | + array( |
|
282 | + 'action' => 'edit_message_template', |
|
283 | + 'id' => $item->GRP_ID(), |
|
284 | + ), |
|
285 | + EE_MSG_ADMIN_URL |
|
286 | + ); |
|
287 | + } |
|
288 | + return $edit_url; |
|
289 | + } |
|
290 | + |
|
291 | + |
|
292 | + /** |
|
293 | + * Get the context link string for the messenger column. |
|
294 | + * |
|
295 | + * @param EE_Message_Template_Group $item |
|
296 | + * @return string |
|
297 | + * @throws EE_Error |
|
298 | + */ |
|
299 | + protected function _get_context_links(EE_Message_Template_Group $item) |
|
300 | + { |
|
301 | + // first check if we even show the context links or not. |
|
302 | + if (! EE_Registry::instance()->CAP->current_user_can( |
|
303 | + 'ee_edit_message', |
|
304 | + 'espresso_messages_edit_message_template', |
|
305 | + $item->ID() |
|
306 | + ) |
|
307 | + || $item->get('MTP_deleted') |
|
308 | + ) { |
|
309 | + return ''; |
|
310 | + } |
|
311 | + // we want to display the contexts in here so we need to set them up |
|
312 | + $c_label = $item->context_label(); |
|
313 | + $c_configs = $item->contexts_config(); |
|
314 | + $ctxt = array(); |
|
315 | + $context_templates = $item->context_templates(); |
|
316 | + foreach ($context_templates as $context => $template_fields) { |
|
317 | + $mtp_to = ! empty($context_templates[ $context ]['to']) |
|
318 | + && $context_templates[ $context ]['to'] instanceof EE_Message_Template |
|
319 | + ? $context_templates[ $context ]['to']->get('MTP_content') |
|
320 | + : null; |
|
321 | + $inactive_class = ( |
|
322 | + empty($mtp_to) |
|
323 | + && ! empty($context_templates[ $context ]['to']) |
|
324 | + ) |
|
325 | + || ! $item->is_context_active($context) |
|
326 | + ? ' mtp-inactive' |
|
327 | + : ''; |
|
328 | + $context_title = sprintf( |
|
329 | + /* translators: Placeholder represents the context label. Example "Edit Event Admin" */ |
|
330 | + esc_html__('Edit %1$s', 'event_espresso'), |
|
331 | + ucwords($c_configs[ $context ]['label']) |
|
332 | + ); |
|
333 | + $edit_link = EE_Admin_Page::add_query_args_and_nonce( |
|
334 | + array( |
|
335 | + 'action' => 'edit_message_template', |
|
336 | + 'id' => $item->GRP_ID(), |
|
337 | + 'context' => $context, |
|
338 | + ), |
|
339 | + EE_MSG_ADMIN_URL |
|
340 | + ); |
|
341 | + $ctxt[] = '<a' |
|
342 | + . ' href="' . $edit_link . '"' |
|
343 | + . ' class="' . $item->message_type() . '-' . $context . '-edit-link' . $inactive_class . '"' |
|
344 | + . ' title="' . esc_attr__('Edit Context', 'event_espresso') . '">' |
|
345 | + . $context_title |
|
346 | + . '</a>'; |
|
347 | + } |
|
348 | + |
|
349 | + return sprintf('<strong>%s:</strong> ', ucwords($c_label['plural'])) . implode(' | ', $ctxt); |
|
350 | + } |
|
351 | 351 | } |
@@ -80,7 +80,7 @@ discard block |
||
80 | 80 | $message_type = $item->message_type_obj(); |
81 | 81 | $messenger = $item->messenger_obj(); |
82 | 82 | |
83 | - if (! $message_type instanceof EE_message_type || ! $messenger instanceof EE_messenger) { |
|
83 | + if ( ! $message_type instanceof EE_message_type || ! $messenger instanceof EE_messenger) { |
|
84 | 84 | echo ''; |
85 | 85 | return; |
86 | 86 | } |
@@ -131,7 +131,7 @@ discard block |
||
131 | 131 | protected function _add_view_counts() |
132 | 132 | { |
133 | 133 | foreach ($this->_views as $view => $args) { |
134 | - $this->_views[ $view ]['count'] = $this->get_admin_page()->get_message_templates( |
|
134 | + $this->_views[$view]['count'] = $this->get_admin_page()->get_message_templates( |
|
135 | 135 | $this->_per_page, |
136 | 136 | $view, |
137 | 137 | true, |
@@ -158,7 +158,7 @@ discard block |
||
158 | 158 | */ |
159 | 159 | public function column_description($item) |
160 | 160 | { |
161 | - return '<p>' . $item->message_type_obj()->description . '</p>'; |
|
161 | + return '<p>'.$item->message_type_obj()->description.'</p>'; |
|
162 | 162 | } |
163 | 163 | |
164 | 164 | |
@@ -220,7 +220,7 @@ discard block |
||
220 | 220 | $messenger_label = $messenger instanceof EE_messenger |
221 | 221 | ? $messenger->label['singular'] |
222 | 222 | : $active_message_template_group->messenger(); |
223 | - $messenger_options[ $active_message_template_group->messenger() ] = ucwords($messenger_label); |
|
223 | + $messenger_options[$active_message_template_group->messenger()] = ucwords($messenger_label); |
|
224 | 224 | } |
225 | 225 | } |
226 | 226 | return $this->get_admin_page()->get_messengers_select_input($messenger_options); |
@@ -253,7 +253,7 @@ discard block |
||
253 | 253 | $message_type_label = $message_type instanceof EE_message_type |
254 | 254 | ? $message_type->label['singular'] |
255 | 255 | : $active_message_template_group->message_type(); |
256 | - $message_type_options[ $active_message_template_group->message_type() ] = ucwords($message_type_label); |
|
256 | + $message_type_options[$active_message_template_group->message_type()] = ucwords($message_type_label); |
|
257 | 257 | } |
258 | 258 | } |
259 | 259 | return $this->get_admin_page()->get_message_types_select_input($message_type_options); |
@@ -271,7 +271,7 @@ discard block |
||
271 | 271 | { |
272 | 272 | $edit_url = ''; |
273 | 273 | // edit link but only if item isn't trashed. |
274 | - if (! $item->get('MTP_deleted') |
|
274 | + if ( ! $item->get('MTP_deleted') |
|
275 | 275 | && EE_Registry::instance()->CAP->current_user_can( |
276 | 276 | 'ee_edit_message', |
277 | 277 | 'espresso_messages_edit_message_template', |
@@ -299,7 +299,7 @@ discard block |
||
299 | 299 | protected function _get_context_links(EE_Message_Template_Group $item) |
300 | 300 | { |
301 | 301 | // first check if we even show the context links or not. |
302 | - if (! EE_Registry::instance()->CAP->current_user_can( |
|
302 | + if ( ! EE_Registry::instance()->CAP->current_user_can( |
|
303 | 303 | 'ee_edit_message', |
304 | 304 | 'espresso_messages_edit_message_template', |
305 | 305 | $item->ID() |
@@ -314,13 +314,13 @@ discard block |
||
314 | 314 | $ctxt = array(); |
315 | 315 | $context_templates = $item->context_templates(); |
316 | 316 | foreach ($context_templates as $context => $template_fields) { |
317 | - $mtp_to = ! empty($context_templates[ $context ]['to']) |
|
318 | - && $context_templates[ $context ]['to'] instanceof EE_Message_Template |
|
319 | - ? $context_templates[ $context ]['to']->get('MTP_content') |
|
317 | + $mtp_to = ! empty($context_templates[$context]['to']) |
|
318 | + && $context_templates[$context]['to'] instanceof EE_Message_Template |
|
319 | + ? $context_templates[$context]['to']->get('MTP_content') |
|
320 | 320 | : null; |
321 | 321 | $inactive_class = ( |
322 | 322 | empty($mtp_to) |
323 | - && ! empty($context_templates[ $context ]['to']) |
|
323 | + && ! empty($context_templates[$context]['to']) |
|
324 | 324 | ) |
325 | 325 | || ! $item->is_context_active($context) |
326 | 326 | ? ' mtp-inactive' |
@@ -328,7 +328,7 @@ discard block |
||
328 | 328 | $context_title = sprintf( |
329 | 329 | /* translators: Placeholder represents the context label. Example "Edit Event Admin" */ |
330 | 330 | esc_html__('Edit %1$s', 'event_espresso'), |
331 | - ucwords($c_configs[ $context ]['label']) |
|
331 | + ucwords($c_configs[$context]['label']) |
|
332 | 332 | ); |
333 | 333 | $edit_link = EE_Admin_Page::add_query_args_and_nonce( |
334 | 334 | array( |
@@ -339,13 +339,13 @@ discard block |
||
339 | 339 | EE_MSG_ADMIN_URL |
340 | 340 | ); |
341 | 341 | $ctxt[] = '<a' |
342 | - . ' href="' . $edit_link . '"' |
|
343 | - . ' class="' . $item->message_type() . '-' . $context . '-edit-link' . $inactive_class . '"' |
|
344 | - . ' title="' . esc_attr__('Edit Context', 'event_espresso') . '">' |
|
342 | + . ' href="'.$edit_link.'"' |
|
343 | + . ' class="'.$item->message_type().'-'.$context.'-edit-link'.$inactive_class.'"' |
|
344 | + . ' title="'.esc_attr__('Edit Context', 'event_espresso').'">' |
|
345 | 345 | . $context_title |
346 | 346 | . '</a>'; |
347 | 347 | } |
348 | 348 | |
349 | - return sprintf('<strong>%s:</strong> ', ucwords($c_label['plural'])) . implode(' | ', $ctxt); |
|
349 | + return sprintf('<strong>%s:</strong> ', ucwords($c_label['plural'])).implode(' | ', $ctxt); |
|
350 | 350 | } |
351 | 351 | } |
@@ -9,261 +9,261 @@ |
||
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($all_fields)); |
|
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 | - } |
|
187 | - return $other_model_obj; |
|
188 | - } |
|
179 | + $existing_entry_in_join_table = $this->get_join_model()->get_one(array($all_fields)); |
|
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 | + } |
|
187 | + return $other_model_obj; |
|
188 | + } |
|
189 | 189 | |
190 | 190 | |
191 | - /** |
|
192 | - * Deletes any rows in the join table that have foreign keys matching the other model objects specified |
|
193 | - * |
|
194 | - * @param EE_Base_Class|int $this_obj_or_id |
|
195 | - * @param EE_Base_Class|int $other_obj_or_id |
|
196 | - * @param array $where_query col=>val pairs that are used as extra conditions for checking existing |
|
197 | - * values and for removing existing rows if exact matches exist. |
|
198 | - * @return EE_Base_Class |
|
199 | - * @throws \EE_Error |
|
200 | - */ |
|
201 | - public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array()) |
|
202 | - { |
|
203 | - $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
204 | - $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
205 | - // check if such a relationship already exists |
|
206 | - $join_model_fk_to_this_model = $this->get_join_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
207 | - $join_model_fk_to_other_model = $this->get_join_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
191 | + /** |
|
192 | + * Deletes any rows in the join table that have foreign keys matching the other model objects specified |
|
193 | + * |
|
194 | + * @param EE_Base_Class|int $this_obj_or_id |
|
195 | + * @param EE_Base_Class|int $other_obj_or_id |
|
196 | + * @param array $where_query col=>val pairs that are used as extra conditions for checking existing |
|
197 | + * values and for removing existing rows if exact matches exist. |
|
198 | + * @return EE_Base_Class |
|
199 | + * @throws \EE_Error |
|
200 | + */ |
|
201 | + public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array()) |
|
202 | + { |
|
203 | + $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
204 | + $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
205 | + // check if such a relationship already exists |
|
206 | + $join_model_fk_to_this_model = $this->get_join_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
207 | + $join_model_fk_to_other_model = $this->get_join_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
208 | 208 | |
209 | - $cols_n_values = array( |
|
210 | - $join_model_fk_to_this_model->get_name() => $this_model_obj->ID(), |
|
211 | - $join_model_fk_to_other_model->get_name() => $other_model_obj->ID(), |
|
212 | - ); |
|
209 | + $cols_n_values = array( |
|
210 | + $join_model_fk_to_this_model->get_name() => $this_model_obj->ID(), |
|
211 | + $join_model_fk_to_other_model->get_name() => $other_model_obj->ID(), |
|
212 | + ); |
|
213 | 213 | |
214 | - // if $where_query exists lets add them to the query_params. |
|
215 | - if (! empty($where_query)) { |
|
216 | - // 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) |
|
217 | - // make sure we strip THIS models name from the query param |
|
218 | - $parsed_query = array(); |
|
219 | - foreach ($where_query as $query_param => $val) { |
|
220 | - $query_param = str_replace( |
|
221 | - $this->get_join_model()->get_this_model_name() . ".", |
|
222 | - "", |
|
223 | - $query_param |
|
224 | - ); |
|
225 | - $parsed_query[ $query_param ] = $val; |
|
226 | - } |
|
227 | - $cols_n_values = array_merge($cols_n_values, $parsed_query); |
|
228 | - } |
|
214 | + // if $where_query exists lets add them to the query_params. |
|
215 | + if (! empty($where_query)) { |
|
216 | + // 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) |
|
217 | + // make sure we strip THIS models name from the query param |
|
218 | + $parsed_query = array(); |
|
219 | + foreach ($where_query as $query_param => $val) { |
|
220 | + $query_param = str_replace( |
|
221 | + $this->get_join_model()->get_this_model_name() . ".", |
|
222 | + "", |
|
223 | + $query_param |
|
224 | + ); |
|
225 | + $parsed_query[ $query_param ] = $val; |
|
226 | + } |
|
227 | + $cols_n_values = array_merge($cols_n_values, $parsed_query); |
|
228 | + } |
|
229 | 229 | |
230 | - $this->get_join_model()->delete(array($cols_n_values)); |
|
231 | - return $other_model_obj; |
|
232 | - } |
|
230 | + $this->get_join_model()->delete(array($cols_n_values)); |
|
231 | + return $other_model_obj; |
|
232 | + } |
|
233 | 233 | |
234 | - /** |
|
235 | - * Gets all the non-key fields (ie, not the primary key and not foreign keys) on the join model. |
|
236 | - * @since 4.9.76.p |
|
237 | - * @return EE_Model_Field_Base[] |
|
238 | - * @throws EE_Error |
|
239 | - */ |
|
240 | - public function getNonKeyFields() |
|
241 | - { |
|
242 | - // all fields besides the primary key and two foreign keys should be parameters |
|
243 | - $join_model = $this->get_join_model(); |
|
244 | - $standard_fields = array(); |
|
245 | - if ($join_model->has_primary_key_field()) { |
|
246 | - $standard_fields[] = $join_model->primary_key_name(); |
|
247 | - } |
|
248 | - if ($this->get_this_model()->has_primary_key_field()) { |
|
249 | - $standard_fields[] = $this->get_this_model()->primary_key_name(); |
|
250 | - } |
|
251 | - if ($this->get_other_model()->has_primary_key_field()) { |
|
252 | - $standard_fields[] = $this->get_other_model()->primary_key_name(); |
|
253 | - } |
|
254 | - return array_diff_key( |
|
255 | - $join_model->field_settings(), |
|
256 | - array_flip($standard_fields) |
|
257 | - ); |
|
258 | - } |
|
234 | + /** |
|
235 | + * Gets all the non-key fields (ie, not the primary key and not foreign keys) on the join model. |
|
236 | + * @since 4.9.76.p |
|
237 | + * @return EE_Model_Field_Base[] |
|
238 | + * @throws EE_Error |
|
239 | + */ |
|
240 | + public function getNonKeyFields() |
|
241 | + { |
|
242 | + // all fields besides the primary key and two foreign keys should be parameters |
|
243 | + $join_model = $this->get_join_model(); |
|
244 | + $standard_fields = array(); |
|
245 | + if ($join_model->has_primary_key_field()) { |
|
246 | + $standard_fields[] = $join_model->primary_key_name(); |
|
247 | + } |
|
248 | + if ($this->get_this_model()->has_primary_key_field()) { |
|
249 | + $standard_fields[] = $this->get_this_model()->primary_key_name(); |
|
250 | + } |
|
251 | + if ($this->get_other_model()->has_primary_key_field()) { |
|
252 | + $standard_fields[] = $this->get_other_model()->primary_key_name(); |
|
253 | + } |
|
254 | + return array_diff_key( |
|
255 | + $join_model->field_settings(), |
|
256 | + array_flip($standard_fields) |
|
257 | + ); |
|
258 | + } |
|
259 | 259 | |
260 | - /** |
|
261 | - * Returns true if the join model has non-key fields (ie, fields that aren't the primary key or foreign keys.) |
|
262 | - * @since 4.9.76.p |
|
263 | - * @return boolean |
|
264 | - */ |
|
265 | - public function hasNonKeyFields() |
|
266 | - { |
|
267 | - return count($this->get_join_model()->field_settings()) > 3; |
|
268 | - } |
|
260 | + /** |
|
261 | + * Returns true if the join model has non-key fields (ie, fields that aren't the primary key or foreign keys.) |
|
262 | + * @since 4.9.76.p |
|
263 | + * @return boolean |
|
264 | + */ |
|
265 | + public function hasNonKeyFields() |
|
266 | + { |
|
267 | + return count($this->get_join_model()->field_settings()) > 3; |
|
268 | + } |
|
269 | 269 | } |
@@ -39,573 +39,573 @@ |
||
39 | 39 | { |
40 | 40 | |
41 | 41 | |
42 | - public function __construct() |
|
43 | - { |
|
44 | - parent::__construct(); |
|
45 | - EE_Registry::instance()->load_helper('Inflector'); |
|
46 | - } |
|
42 | + public function __construct() |
|
43 | + { |
|
44 | + parent::__construct(); |
|
45 | + EE_Registry::instance()->load_helper('Inflector'); |
|
46 | + } |
|
47 | 47 | |
48 | 48 | |
49 | - /** |
|
50 | - * Handles requests to get all (or a filtered subset) of entities for a particular model |
|
51 | - * |
|
52 | - * @param WP_REST_Request $request |
|
53 | - * @param string $version |
|
54 | - * @param string $model_name |
|
55 | - * @return WP_REST_Response|\WP_Error |
|
56 | - */ |
|
57 | - public static function handleRequestInsert(WP_REST_Request $request, $version, $model_name) |
|
58 | - { |
|
59 | - $controller = new Write(); |
|
60 | - try { |
|
61 | - $controller->setRequestedVersion($version); |
|
62 | - return $controller->sendResponse( |
|
63 | - $controller->insert( |
|
64 | - $controller->getModelVersionInfo()->loadModel($model_name), |
|
65 | - $request |
|
66 | - ) |
|
67 | - ); |
|
68 | - } catch (Exception $e) { |
|
69 | - return $controller->sendResponse($e); |
|
70 | - } |
|
71 | - } |
|
49 | + /** |
|
50 | + * Handles requests to get all (or a filtered subset) of entities for a particular model |
|
51 | + * |
|
52 | + * @param WP_REST_Request $request |
|
53 | + * @param string $version |
|
54 | + * @param string $model_name |
|
55 | + * @return WP_REST_Response|\WP_Error |
|
56 | + */ |
|
57 | + public static function handleRequestInsert(WP_REST_Request $request, $version, $model_name) |
|
58 | + { |
|
59 | + $controller = new Write(); |
|
60 | + try { |
|
61 | + $controller->setRequestedVersion($version); |
|
62 | + return $controller->sendResponse( |
|
63 | + $controller->insert( |
|
64 | + $controller->getModelVersionInfo()->loadModel($model_name), |
|
65 | + $request |
|
66 | + ) |
|
67 | + ); |
|
68 | + } catch (Exception $e) { |
|
69 | + return $controller->sendResponse($e); |
|
70 | + } |
|
71 | + } |
|
72 | 72 | |
73 | 73 | |
74 | - /** |
|
75 | - * Handles a request from \WP_REST_Server to update an EE model |
|
76 | - * |
|
77 | - * @param WP_REST_Request $request |
|
78 | - * @param string $version |
|
79 | - * @param string $model_name |
|
80 | - * @return WP_REST_Response|\WP_Error |
|
81 | - */ |
|
82 | - public static function handleRequestUpdate(WP_REST_Request $request, $version, $model_name) |
|
83 | - { |
|
84 | - $controller = new Write(); |
|
85 | - try { |
|
86 | - $controller->setRequestedVersion($version); |
|
87 | - return $controller->sendResponse( |
|
88 | - $controller->update( |
|
89 | - $controller->getModelVersionInfo()->loadModel($model_name), |
|
90 | - $request |
|
91 | - ) |
|
92 | - ); |
|
93 | - } catch (Exception $e) { |
|
94 | - return $controller->sendResponse($e); |
|
95 | - } |
|
96 | - } |
|
74 | + /** |
|
75 | + * Handles a request from \WP_REST_Server to update an EE model |
|
76 | + * |
|
77 | + * @param WP_REST_Request $request |
|
78 | + * @param string $version |
|
79 | + * @param string $model_name |
|
80 | + * @return WP_REST_Response|\WP_Error |
|
81 | + */ |
|
82 | + public static function handleRequestUpdate(WP_REST_Request $request, $version, $model_name) |
|
83 | + { |
|
84 | + $controller = new Write(); |
|
85 | + try { |
|
86 | + $controller->setRequestedVersion($version); |
|
87 | + return $controller->sendResponse( |
|
88 | + $controller->update( |
|
89 | + $controller->getModelVersionInfo()->loadModel($model_name), |
|
90 | + $request |
|
91 | + ) |
|
92 | + ); |
|
93 | + } catch (Exception $e) { |
|
94 | + return $controller->sendResponse($e); |
|
95 | + } |
|
96 | + } |
|
97 | 97 | |
98 | 98 | |
99 | - /** |
|
100 | - * Deletes a single model object and returns it. Unless |
|
101 | - * |
|
102 | - * @param WP_REST_Request $request |
|
103 | - * @param string $version |
|
104 | - * @param string $model_name |
|
105 | - * @return WP_REST_Response|\WP_Error |
|
106 | - */ |
|
107 | - public static function handleRequestDelete(WP_REST_Request $request, $version, $model_name) |
|
108 | - { |
|
109 | - $controller = new Write(); |
|
110 | - try { |
|
111 | - $controller->setRequestedVersion($version); |
|
112 | - return $controller->sendResponse( |
|
113 | - $controller->delete( |
|
114 | - $controller->getModelVersionInfo()->loadModel($model_name), |
|
115 | - $request |
|
116 | - ) |
|
117 | - ); |
|
118 | - } catch (Exception $e) { |
|
119 | - return $controller->sendResponse($e); |
|
120 | - } |
|
121 | - } |
|
99 | + /** |
|
100 | + * Deletes a single model object and returns it. Unless |
|
101 | + * |
|
102 | + * @param WP_REST_Request $request |
|
103 | + * @param string $version |
|
104 | + * @param string $model_name |
|
105 | + * @return WP_REST_Response|\WP_Error |
|
106 | + */ |
|
107 | + public static function handleRequestDelete(WP_REST_Request $request, $version, $model_name) |
|
108 | + { |
|
109 | + $controller = new Write(); |
|
110 | + try { |
|
111 | + $controller->setRequestedVersion($version); |
|
112 | + return $controller->sendResponse( |
|
113 | + $controller->delete( |
|
114 | + $controller->getModelVersionInfo()->loadModel($model_name), |
|
115 | + $request |
|
116 | + ) |
|
117 | + ); |
|
118 | + } catch (Exception $e) { |
|
119 | + return $controller->sendResponse($e); |
|
120 | + } |
|
121 | + } |
|
122 | 122 | |
123 | 123 | |
124 | - /** |
|
125 | - * Inserts a new model object according to the $request |
|
126 | - * |
|
127 | - * @param EEM_Base $model |
|
128 | - * @param WP_REST_Request $request |
|
129 | - * @return array |
|
130 | - * @throws EE_Error |
|
131 | - * @throws RestException |
|
132 | - */ |
|
133 | - public function insert(EEM_Base $model, WP_REST_Request $request) |
|
134 | - { |
|
135 | - Capabilities::verifyAtLeastPartialAccessTo($model, EEM_Base::caps_edit, 'create'); |
|
136 | - $default_cap_to_check_for = EE_Restriction_Generator_Base::get_default_restrictions_cap(); |
|
137 | - if (! current_user_can($default_cap_to_check_for)) { |
|
138 | - throw new RestException( |
|
139 | - 'rest_cannot_create_' . EEH_Inflector::pluralize_and_lower(($model->get_this_model_name())), |
|
140 | - sprintf( |
|
141 | - esc_html__( |
|
142 | - // @codingStandardsIgnoreStart |
|
143 | - 'For now, only those with the admin capability to "%1$s" are allowed to use the REST API to insert data into Event Espresso.', |
|
144 | - // @codingStandardsIgnoreEnd |
|
145 | - 'event_espresso' |
|
146 | - ), |
|
147 | - $default_cap_to_check_for |
|
148 | - ), |
|
149 | - array('status' => 403) |
|
150 | - ); |
|
151 | - } |
|
152 | - $submitted_json_data = array_merge((array) $request->get_body_params(), (array) $request->get_json_params()); |
|
153 | - $model_data = ModelDataTranslator::prepareConditionsQueryParamsForModels( |
|
154 | - $submitted_json_data, |
|
155 | - $model, |
|
156 | - $this->getModelVersionInfo()->requestedVersion(), |
|
157 | - true |
|
158 | - ); |
|
159 | - $model_obj = EE_Registry::instance()->load_class( |
|
160 | - $model->get_this_model_name(), |
|
161 | - array($model_data, $model->get_timezone()), |
|
162 | - false, |
|
163 | - false |
|
164 | - ); |
|
165 | - $model_obj->save(); |
|
166 | - $new_id = $model_obj->ID(); |
|
167 | - if (! $new_id) { |
|
168 | - throw new RestException( |
|
169 | - 'rest_insertion_failed', |
|
170 | - sprintf(__('Could not insert new %1$s', 'event_espresso'), $model->get_this_model_name()) |
|
171 | - ); |
|
172 | - } |
|
173 | - return $this->returnModelObjAsJsonResponse($model_obj, $request); |
|
174 | - } |
|
124 | + /** |
|
125 | + * Inserts a new model object according to the $request |
|
126 | + * |
|
127 | + * @param EEM_Base $model |
|
128 | + * @param WP_REST_Request $request |
|
129 | + * @return array |
|
130 | + * @throws EE_Error |
|
131 | + * @throws RestException |
|
132 | + */ |
|
133 | + public function insert(EEM_Base $model, WP_REST_Request $request) |
|
134 | + { |
|
135 | + Capabilities::verifyAtLeastPartialAccessTo($model, EEM_Base::caps_edit, 'create'); |
|
136 | + $default_cap_to_check_for = EE_Restriction_Generator_Base::get_default_restrictions_cap(); |
|
137 | + if (! current_user_can($default_cap_to_check_for)) { |
|
138 | + throw new RestException( |
|
139 | + 'rest_cannot_create_' . EEH_Inflector::pluralize_and_lower(($model->get_this_model_name())), |
|
140 | + sprintf( |
|
141 | + esc_html__( |
|
142 | + // @codingStandardsIgnoreStart |
|
143 | + 'For now, only those with the admin capability to "%1$s" are allowed to use the REST API to insert data into Event Espresso.', |
|
144 | + // @codingStandardsIgnoreEnd |
|
145 | + 'event_espresso' |
|
146 | + ), |
|
147 | + $default_cap_to_check_for |
|
148 | + ), |
|
149 | + array('status' => 403) |
|
150 | + ); |
|
151 | + } |
|
152 | + $submitted_json_data = array_merge((array) $request->get_body_params(), (array) $request->get_json_params()); |
|
153 | + $model_data = ModelDataTranslator::prepareConditionsQueryParamsForModels( |
|
154 | + $submitted_json_data, |
|
155 | + $model, |
|
156 | + $this->getModelVersionInfo()->requestedVersion(), |
|
157 | + true |
|
158 | + ); |
|
159 | + $model_obj = EE_Registry::instance()->load_class( |
|
160 | + $model->get_this_model_name(), |
|
161 | + array($model_data, $model->get_timezone()), |
|
162 | + false, |
|
163 | + false |
|
164 | + ); |
|
165 | + $model_obj->save(); |
|
166 | + $new_id = $model_obj->ID(); |
|
167 | + if (! $new_id) { |
|
168 | + throw new RestException( |
|
169 | + 'rest_insertion_failed', |
|
170 | + sprintf(__('Could not insert new %1$s', 'event_espresso'), $model->get_this_model_name()) |
|
171 | + ); |
|
172 | + } |
|
173 | + return $this->returnModelObjAsJsonResponse($model_obj, $request); |
|
174 | + } |
|
175 | 175 | |
176 | 176 | |
177 | - /** |
|
178 | - * Updates an existing model object according to the $request |
|
179 | - * |
|
180 | - * @param EEM_Base $model |
|
181 | - * @param WP_REST_Request $request |
|
182 | - * @return array |
|
183 | - * @throws EE_Error |
|
184 | - */ |
|
185 | - public function update(EEM_Base $model, WP_REST_Request $request) |
|
186 | - { |
|
187 | - Capabilities::verifyAtLeastPartialAccessTo($model, EEM_Base::caps_edit, 'edit'); |
|
188 | - $default_cap_to_check_for = EE_Restriction_Generator_Base::get_default_restrictions_cap(); |
|
189 | - if (! current_user_can($default_cap_to_check_for)) { |
|
190 | - throw new RestException( |
|
191 | - 'rest_cannot_edit_' . EEH_Inflector::pluralize_and_lower(($model->get_this_model_name())), |
|
192 | - sprintf( |
|
193 | - esc_html__( |
|
194 | - // @codingStandardsIgnoreStart |
|
195 | - 'For now, only those with the admin capability to "%1$s" are allowed to use the REST API to update data into Event Espresso.', |
|
196 | - // @codingStandardsIgnoreEnd |
|
197 | - 'event_espresso' |
|
198 | - ), |
|
199 | - $default_cap_to_check_for |
|
200 | - ), |
|
201 | - array('status' => 403) |
|
202 | - ); |
|
203 | - } |
|
204 | - $obj_id = $request->get_param('id'); |
|
205 | - if (! $obj_id) { |
|
206 | - throw new RestException( |
|
207 | - 'rest_edit_failed', |
|
208 | - sprintf(__('Could not edit %1$s', 'event_espresso'), $model->get_this_model_name()) |
|
209 | - ); |
|
210 | - } |
|
211 | - $model_data = ModelDataTranslator::prepareConditionsQueryParamsForModels( |
|
212 | - $this->getBodyParams($request), |
|
213 | - $model, |
|
214 | - $this->getModelVersionInfo()->requestedVersion(), |
|
215 | - true |
|
216 | - ); |
|
217 | - $model_obj = $model->get_one_by_ID($obj_id); |
|
218 | - if (! $model_obj instanceof EE_Base_Class) { |
|
219 | - $lowercase_model_name = strtolower($model->get_this_model_name()); |
|
220 | - throw new RestException( |
|
221 | - sprintf('rest_%s_invalid_id', $lowercase_model_name), |
|
222 | - sprintf(__('Invalid %s ID.', 'event_espresso'), $lowercase_model_name), |
|
223 | - array('status' => 404) |
|
224 | - ); |
|
225 | - } |
|
226 | - $model_obj->save($model_data); |
|
227 | - return $this->returnModelObjAsJsonResponse($model_obj, $request); |
|
228 | - } |
|
177 | + /** |
|
178 | + * Updates an existing model object according to the $request |
|
179 | + * |
|
180 | + * @param EEM_Base $model |
|
181 | + * @param WP_REST_Request $request |
|
182 | + * @return array |
|
183 | + * @throws EE_Error |
|
184 | + */ |
|
185 | + public function update(EEM_Base $model, WP_REST_Request $request) |
|
186 | + { |
|
187 | + Capabilities::verifyAtLeastPartialAccessTo($model, EEM_Base::caps_edit, 'edit'); |
|
188 | + $default_cap_to_check_for = EE_Restriction_Generator_Base::get_default_restrictions_cap(); |
|
189 | + if (! current_user_can($default_cap_to_check_for)) { |
|
190 | + throw new RestException( |
|
191 | + 'rest_cannot_edit_' . EEH_Inflector::pluralize_and_lower(($model->get_this_model_name())), |
|
192 | + sprintf( |
|
193 | + esc_html__( |
|
194 | + // @codingStandardsIgnoreStart |
|
195 | + 'For now, only those with the admin capability to "%1$s" are allowed to use the REST API to update data into Event Espresso.', |
|
196 | + // @codingStandardsIgnoreEnd |
|
197 | + 'event_espresso' |
|
198 | + ), |
|
199 | + $default_cap_to_check_for |
|
200 | + ), |
|
201 | + array('status' => 403) |
|
202 | + ); |
|
203 | + } |
|
204 | + $obj_id = $request->get_param('id'); |
|
205 | + if (! $obj_id) { |
|
206 | + throw new RestException( |
|
207 | + 'rest_edit_failed', |
|
208 | + sprintf(__('Could not edit %1$s', 'event_espresso'), $model->get_this_model_name()) |
|
209 | + ); |
|
210 | + } |
|
211 | + $model_data = ModelDataTranslator::prepareConditionsQueryParamsForModels( |
|
212 | + $this->getBodyParams($request), |
|
213 | + $model, |
|
214 | + $this->getModelVersionInfo()->requestedVersion(), |
|
215 | + true |
|
216 | + ); |
|
217 | + $model_obj = $model->get_one_by_ID($obj_id); |
|
218 | + if (! $model_obj instanceof EE_Base_Class) { |
|
219 | + $lowercase_model_name = strtolower($model->get_this_model_name()); |
|
220 | + throw new RestException( |
|
221 | + sprintf('rest_%s_invalid_id', $lowercase_model_name), |
|
222 | + sprintf(__('Invalid %s ID.', 'event_espresso'), $lowercase_model_name), |
|
223 | + array('status' => 404) |
|
224 | + ); |
|
225 | + } |
|
226 | + $model_obj->save($model_data); |
|
227 | + return $this->returnModelObjAsJsonResponse($model_obj, $request); |
|
228 | + } |
|
229 | 229 | |
230 | 230 | |
231 | - /** |
|
232 | - * Updates an existing model object according to the $request |
|
233 | - * |
|
234 | - * @param EEM_Base $model |
|
235 | - * @param WP_REST_Request $request |
|
236 | - * @return array of either the soft-deleted item, or |
|
237 | - * @throws EE_Error |
|
238 | - */ |
|
239 | - public function delete(EEM_Base $model, WP_REST_Request $request) |
|
240 | - { |
|
241 | - Capabilities::verifyAtLeastPartialAccessTo($model, EEM_Base::caps_delete, 'delete'); |
|
242 | - $default_cap_to_check_for = EE_Restriction_Generator_Base::get_default_restrictions_cap(); |
|
243 | - if (! current_user_can($default_cap_to_check_for)) { |
|
244 | - throw new RestException( |
|
245 | - 'rest_cannot_delete_' . EEH_Inflector::pluralize_and_lower(($model->get_this_model_name())), |
|
246 | - sprintf( |
|
247 | - esc_html__( |
|
248 | - // @codingStandardsIgnoreStart |
|
249 | - 'For now, only those with the admin capability to "%1$s" are allowed to use the REST API to delete data into Event Espresso.', |
|
250 | - // @codingStandardsIgnoreEnd |
|
251 | - 'event_espresso' |
|
252 | - ), |
|
253 | - $default_cap_to_check_for |
|
254 | - ), |
|
255 | - array('status' => 403) |
|
256 | - ); |
|
257 | - } |
|
258 | - $obj_id = $request->get_param('id'); |
|
259 | - // this is where we would apply more fine-grained caps |
|
260 | - $model_obj = $model->get_one_by_ID($obj_id); |
|
261 | - if (! $model_obj instanceof EE_Base_Class) { |
|
262 | - $lowercase_model_name = strtolower($model->get_this_model_name()); |
|
263 | - throw new RestException( |
|
264 | - sprintf('rest_%s_invalid_id', $lowercase_model_name), |
|
265 | - sprintf(__('Invalid %s ID.', 'event_espresso'), $lowercase_model_name), |
|
266 | - array('status' => 404) |
|
267 | - ); |
|
268 | - } |
|
269 | - $requested_permanent_delete = filter_var($request->get_param('force'), FILTER_VALIDATE_BOOLEAN); |
|
270 | - $requested_allow_blocking = filter_var($request->get_param('allow_blocking'), FILTER_VALIDATE_BOOLEAN); |
|
271 | - if ($requested_permanent_delete) { |
|
272 | - $previous = $this->returnModelObjAsJsonResponse($model_obj, $request); |
|
273 | - $deleted = (bool) $model->delete_permanently_by_ID($obj_id, $requested_allow_blocking); |
|
274 | - return array( |
|
275 | - 'deleted' => $deleted, |
|
276 | - 'previous' => $previous, |
|
277 | - ); |
|
278 | - } else { |
|
279 | - if ($model instanceof EEM_Soft_Delete_Base) { |
|
280 | - $model->delete_by_ID($obj_id, $requested_allow_blocking); |
|
281 | - return $this->returnModelObjAsJsonResponse($model_obj, $request); |
|
282 | - } else { |
|
283 | - throw new RestException( |
|
284 | - 'rest_trash_not_supported', |
|
285 | - 501, |
|
286 | - sprintf( |
|
287 | - esc_html__('%1$s do not support trashing. Set force=1 to delete.', 'event_espresso'), |
|
288 | - EEH_Inflector::pluralize($model->get_this_model_name()) |
|
289 | - ) |
|
290 | - ); |
|
291 | - } |
|
292 | - } |
|
293 | - } |
|
231 | + /** |
|
232 | + * Updates an existing model object according to the $request |
|
233 | + * |
|
234 | + * @param EEM_Base $model |
|
235 | + * @param WP_REST_Request $request |
|
236 | + * @return array of either the soft-deleted item, or |
|
237 | + * @throws EE_Error |
|
238 | + */ |
|
239 | + public function delete(EEM_Base $model, WP_REST_Request $request) |
|
240 | + { |
|
241 | + Capabilities::verifyAtLeastPartialAccessTo($model, EEM_Base::caps_delete, 'delete'); |
|
242 | + $default_cap_to_check_for = EE_Restriction_Generator_Base::get_default_restrictions_cap(); |
|
243 | + if (! current_user_can($default_cap_to_check_for)) { |
|
244 | + throw new RestException( |
|
245 | + 'rest_cannot_delete_' . EEH_Inflector::pluralize_and_lower(($model->get_this_model_name())), |
|
246 | + sprintf( |
|
247 | + esc_html__( |
|
248 | + // @codingStandardsIgnoreStart |
|
249 | + 'For now, only those with the admin capability to "%1$s" are allowed to use the REST API to delete data into Event Espresso.', |
|
250 | + // @codingStandardsIgnoreEnd |
|
251 | + 'event_espresso' |
|
252 | + ), |
|
253 | + $default_cap_to_check_for |
|
254 | + ), |
|
255 | + array('status' => 403) |
|
256 | + ); |
|
257 | + } |
|
258 | + $obj_id = $request->get_param('id'); |
|
259 | + // this is where we would apply more fine-grained caps |
|
260 | + $model_obj = $model->get_one_by_ID($obj_id); |
|
261 | + if (! $model_obj instanceof EE_Base_Class) { |
|
262 | + $lowercase_model_name = strtolower($model->get_this_model_name()); |
|
263 | + throw new RestException( |
|
264 | + sprintf('rest_%s_invalid_id', $lowercase_model_name), |
|
265 | + sprintf(__('Invalid %s ID.', 'event_espresso'), $lowercase_model_name), |
|
266 | + array('status' => 404) |
|
267 | + ); |
|
268 | + } |
|
269 | + $requested_permanent_delete = filter_var($request->get_param('force'), FILTER_VALIDATE_BOOLEAN); |
|
270 | + $requested_allow_blocking = filter_var($request->get_param('allow_blocking'), FILTER_VALIDATE_BOOLEAN); |
|
271 | + if ($requested_permanent_delete) { |
|
272 | + $previous = $this->returnModelObjAsJsonResponse($model_obj, $request); |
|
273 | + $deleted = (bool) $model->delete_permanently_by_ID($obj_id, $requested_allow_blocking); |
|
274 | + return array( |
|
275 | + 'deleted' => $deleted, |
|
276 | + 'previous' => $previous, |
|
277 | + ); |
|
278 | + } else { |
|
279 | + if ($model instanceof EEM_Soft_Delete_Base) { |
|
280 | + $model->delete_by_ID($obj_id, $requested_allow_blocking); |
|
281 | + return $this->returnModelObjAsJsonResponse($model_obj, $request); |
|
282 | + } else { |
|
283 | + throw new RestException( |
|
284 | + 'rest_trash_not_supported', |
|
285 | + 501, |
|
286 | + sprintf( |
|
287 | + esc_html__('%1$s do not support trashing. Set force=1 to delete.', 'event_espresso'), |
|
288 | + EEH_Inflector::pluralize($model->get_this_model_name()) |
|
289 | + ) |
|
290 | + ); |
|
291 | + } |
|
292 | + } |
|
293 | + } |
|
294 | 294 | |
295 | 295 | |
296 | - /** |
|
297 | - * Returns an array ready to be converted into a JSON response, based solely on the model object |
|
298 | - * |
|
299 | - * @param EE_Base_Class $model_obj |
|
300 | - * @param WP_REST_Request $request |
|
301 | - * @return array ready for a response |
|
302 | - */ |
|
303 | - protected function returnModelObjAsJsonResponse(EE_Base_Class $model_obj, WP_REST_Request $request) |
|
304 | - { |
|
305 | - $model = $model_obj->get_model(); |
|
306 | - // create an array exactly like the wpdb results row, |
|
307 | - // so we can pass it to controllers/model/Read::create_entity_from_wpdb_result() |
|
308 | - $simulated_db_row = array(); |
|
309 | - foreach ($model->field_settings(true) as $field_name => $field_obj) { |
|
310 | - // we need to reconstruct the normal wpdb results, including the db-only fields |
|
311 | - // like a secondary table's primary key. The models expect those (but don't care what value they have) |
|
312 | - if ($field_obj instanceof EE_DB_Only_Field_Base) { |
|
313 | - $raw_value = true; |
|
314 | - } elseif ($field_obj instanceof EE_Datetime_Field) { |
|
315 | - $raw_value = $model_obj->get_DateTime_object($field_name); |
|
316 | - } else { |
|
317 | - $raw_value = $model_obj->get_raw($field_name); |
|
318 | - } |
|
319 | - $simulated_db_row[ $field_obj->get_qualified_column() ] = $field_obj->prepare_for_use_in_db($raw_value); |
|
320 | - } |
|
321 | - $read_controller = LoaderFactory::getLoader()->getNew('EventEspresso\core\libraries\rest_api\controllers\model\Read'); |
|
322 | - $read_controller->setRequestedVersion($this->getRequestedVersion()); |
|
323 | - // the simulates request really doesn't need any info downstream |
|
324 | - $simulated_request = new WP_REST_Request('GET'); |
|
325 | - // set the caps context on the simulated according to the original request. |
|
326 | - switch ($request->get_method()) { |
|
327 | - case 'POST': |
|
328 | - case 'PUT': |
|
329 | - $caps_context = EEM_Base::caps_edit; |
|
330 | - break; |
|
331 | - case 'DELETE': |
|
332 | - $caps_context = EEM_Base::caps_delete; |
|
333 | - break; |
|
334 | - default: |
|
335 | - $caps_context = EEM_Base::caps_read_admin; |
|
336 | - } |
|
337 | - $simulated_request->set_param('caps', $caps_context); |
|
338 | - return $read_controller->createEntityFromWpdbResult( |
|
339 | - $model_obj->get_model(), |
|
340 | - $simulated_db_row, |
|
341 | - $simulated_request |
|
342 | - ); |
|
343 | - } |
|
296 | + /** |
|
297 | + * Returns an array ready to be converted into a JSON response, based solely on the model object |
|
298 | + * |
|
299 | + * @param EE_Base_Class $model_obj |
|
300 | + * @param WP_REST_Request $request |
|
301 | + * @return array ready for a response |
|
302 | + */ |
|
303 | + protected function returnModelObjAsJsonResponse(EE_Base_Class $model_obj, WP_REST_Request $request) |
|
304 | + { |
|
305 | + $model = $model_obj->get_model(); |
|
306 | + // create an array exactly like the wpdb results row, |
|
307 | + // so we can pass it to controllers/model/Read::create_entity_from_wpdb_result() |
|
308 | + $simulated_db_row = array(); |
|
309 | + foreach ($model->field_settings(true) as $field_name => $field_obj) { |
|
310 | + // we need to reconstruct the normal wpdb results, including the db-only fields |
|
311 | + // like a secondary table's primary key. The models expect those (but don't care what value they have) |
|
312 | + if ($field_obj instanceof EE_DB_Only_Field_Base) { |
|
313 | + $raw_value = true; |
|
314 | + } elseif ($field_obj instanceof EE_Datetime_Field) { |
|
315 | + $raw_value = $model_obj->get_DateTime_object($field_name); |
|
316 | + } else { |
|
317 | + $raw_value = $model_obj->get_raw($field_name); |
|
318 | + } |
|
319 | + $simulated_db_row[ $field_obj->get_qualified_column() ] = $field_obj->prepare_for_use_in_db($raw_value); |
|
320 | + } |
|
321 | + $read_controller = LoaderFactory::getLoader()->getNew('EventEspresso\core\libraries\rest_api\controllers\model\Read'); |
|
322 | + $read_controller->setRequestedVersion($this->getRequestedVersion()); |
|
323 | + // the simulates request really doesn't need any info downstream |
|
324 | + $simulated_request = new WP_REST_Request('GET'); |
|
325 | + // set the caps context on the simulated according to the original request. |
|
326 | + switch ($request->get_method()) { |
|
327 | + case 'POST': |
|
328 | + case 'PUT': |
|
329 | + $caps_context = EEM_Base::caps_edit; |
|
330 | + break; |
|
331 | + case 'DELETE': |
|
332 | + $caps_context = EEM_Base::caps_delete; |
|
333 | + break; |
|
334 | + default: |
|
335 | + $caps_context = EEM_Base::caps_read_admin; |
|
336 | + } |
|
337 | + $simulated_request->set_param('caps', $caps_context); |
|
338 | + return $read_controller->createEntityFromWpdbResult( |
|
339 | + $model_obj->get_model(), |
|
340 | + $simulated_db_row, |
|
341 | + $simulated_request |
|
342 | + ); |
|
343 | + } |
|
344 | 344 | |
345 | 345 | |
346 | - /** |
|
347 | - * Gets the item affected by this request |
|
348 | - * |
|
349 | - * @param EEM_Base $model |
|
350 | - * @param WP_REST_Request $request |
|
351 | - * @param int|string $obj_id |
|
352 | - * @return \WP_Error|array |
|
353 | - */ |
|
354 | - protected function getOneBasedOnRequest(EEM_Base $model, WP_REST_Request $request, $obj_id) |
|
355 | - { |
|
356 | - $requested_version = $this->getRequestedVersion($request->get_route()); |
|
357 | - $get_request = new WP_REST_Request( |
|
358 | - 'GET', |
|
359 | - EED_Core_Rest_Api::ee_api_namespace |
|
360 | - . $requested_version |
|
361 | - . '/' |
|
362 | - . EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
|
363 | - . '/' |
|
364 | - . $obj_id |
|
365 | - ); |
|
366 | - $get_request->set_url_params( |
|
367 | - array( |
|
368 | - 'id' => $obj_id, |
|
369 | - 'include' => $request->get_param('include'), |
|
370 | - ) |
|
371 | - ); |
|
372 | - $read_controller = new Read(); |
|
373 | - $read_controller->setRequestedVersion($this->getRequestedVersion()); |
|
374 | - return $read_controller->getEntityFromModel($model, $get_request); |
|
375 | - } |
|
346 | + /** |
|
347 | + * Gets the item affected by this request |
|
348 | + * |
|
349 | + * @param EEM_Base $model |
|
350 | + * @param WP_REST_Request $request |
|
351 | + * @param int|string $obj_id |
|
352 | + * @return \WP_Error|array |
|
353 | + */ |
|
354 | + protected function getOneBasedOnRequest(EEM_Base $model, WP_REST_Request $request, $obj_id) |
|
355 | + { |
|
356 | + $requested_version = $this->getRequestedVersion($request->get_route()); |
|
357 | + $get_request = new WP_REST_Request( |
|
358 | + 'GET', |
|
359 | + EED_Core_Rest_Api::ee_api_namespace |
|
360 | + . $requested_version |
|
361 | + . '/' |
|
362 | + . EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
|
363 | + . '/' |
|
364 | + . $obj_id |
|
365 | + ); |
|
366 | + $get_request->set_url_params( |
|
367 | + array( |
|
368 | + 'id' => $obj_id, |
|
369 | + 'include' => $request->get_param('include'), |
|
370 | + ) |
|
371 | + ); |
|
372 | + $read_controller = new Read(); |
|
373 | + $read_controller->setRequestedVersion($this->getRequestedVersion()); |
|
374 | + return $read_controller->getEntityFromModel($model, $get_request); |
|
375 | + } |
|
376 | 376 | |
377 | - /** |
|
378 | - * Adds a relation between the specified models (if it doesn't already exist.) |
|
379 | - * @since 4.9.76.p |
|
380 | - * @param WP_REST_Request $request |
|
381 | - * @return WP_REST_Response |
|
382 | - */ |
|
383 | - public static function handleRequestAddRelation(WP_REST_Request $request, $version, $model_name, $related_model_name) |
|
384 | - { |
|
385 | - $controller = new Write(); |
|
386 | - try { |
|
387 | - $controller->setRequestedVersion($version); |
|
388 | - $main_model = $controller->validateModel($model_name); |
|
389 | - $controller->validateModel($related_model_name); |
|
390 | - return $controller->sendResponse( |
|
391 | - $controller->addRelation( |
|
392 | - $main_model, |
|
393 | - $main_model->related_settings_for($related_model_name), |
|
394 | - $request |
|
395 | - ) |
|
396 | - ); |
|
397 | - } catch (Exception $e) { |
|
398 | - return $controller->sendResponse($e); |
|
399 | - } |
|
400 | - } |
|
377 | + /** |
|
378 | + * Adds a relation between the specified models (if it doesn't already exist.) |
|
379 | + * @since 4.9.76.p |
|
380 | + * @param WP_REST_Request $request |
|
381 | + * @return WP_REST_Response |
|
382 | + */ |
|
383 | + public static function handleRequestAddRelation(WP_REST_Request $request, $version, $model_name, $related_model_name) |
|
384 | + { |
|
385 | + $controller = new Write(); |
|
386 | + try { |
|
387 | + $controller->setRequestedVersion($version); |
|
388 | + $main_model = $controller->validateModel($model_name); |
|
389 | + $controller->validateModel($related_model_name); |
|
390 | + return $controller->sendResponse( |
|
391 | + $controller->addRelation( |
|
392 | + $main_model, |
|
393 | + $main_model->related_settings_for($related_model_name), |
|
394 | + $request |
|
395 | + ) |
|
396 | + ); |
|
397 | + } catch (Exception $e) { |
|
398 | + return $controller->sendResponse($e); |
|
399 | + } |
|
400 | + } |
|
401 | 401 | |
402 | - /** |
|
403 | - * Adds a relation between the two model specified model objects. |
|
404 | - * @since 4.9.76.p |
|
405 | - * @param EEM_Base $model |
|
406 | - * @param EE_Model_Relation_Base $relation |
|
407 | - * @param WP_REST_Request $request |
|
408 | - * @return array |
|
409 | - * @throws EE_Error |
|
410 | - * @throws InvalidArgumentException |
|
411 | - * @throws InvalidDataTypeException |
|
412 | - * @throws InvalidInterfaceException |
|
413 | - * @throws RestException |
|
414 | - * @throws DomainException |
|
415 | - */ |
|
416 | - public function addRelation(EEM_Base $model, EE_Model_Relation_Base $relation, WP_REST_Request $request) |
|
417 | - { |
|
418 | - list($model_obj, $other_obj) = $this->getBothModelObjects($model, $relation, $request); |
|
419 | - $extra_params = array(); |
|
420 | - if ($relation instanceof EE_HABTM_Relation) { |
|
421 | - $extra_params = array_intersect_key( |
|
422 | - ModelDataTranslator::prepareConditionsQueryParamsForModels( |
|
423 | - $request->get_body_params(), |
|
424 | - $relation->get_join_model(), |
|
425 | - $this->getModelVersionInfo()->requestedVersion(), |
|
426 | - true |
|
427 | - ), |
|
428 | - $relation->getNonKeyFields() |
|
429 | - ); |
|
430 | - } |
|
431 | - // Add a relation. |
|
432 | - $related_obj = $model_obj->_add_relation_to( |
|
433 | - $other_obj, |
|
434 | - $relation->get_other_model()->get_this_model_name(), |
|
435 | - $extra_params |
|
436 | - ); |
|
437 | - $response = array( |
|
438 | - strtolower($model->get_this_model_name()) => $this->returnModelObjAsJsonResponse($model_obj, $request), |
|
439 | - strtolower($relation->get_other_model()->get_this_model_name()) => $this->returnModelObjAsJsonResponse($related_obj, $request), |
|
440 | - ); |
|
441 | - if ($relation instanceof EE_HABTM_Relation) { |
|
442 | - $join_model_obj = $relation->get_join_model()->get_one( |
|
443 | - array( |
|
444 | - array( |
|
445 | - $model->primary_key_name() => $model_obj->ID(), |
|
446 | - $relation->get_other_model()->primary_key_name() => $related_obj->ID() |
|
447 | - ) |
|
448 | - ) |
|
449 | - ); |
|
450 | - $response['join'][ strtolower($relation->get_join_model()->get_this_model_name()) ] = $this->returnModelObjAsJsonResponse($join_model_obj, $request); |
|
451 | - } |
|
452 | - return $response; |
|
453 | - } |
|
402 | + /** |
|
403 | + * Adds a relation between the two model specified model objects. |
|
404 | + * @since 4.9.76.p |
|
405 | + * @param EEM_Base $model |
|
406 | + * @param EE_Model_Relation_Base $relation |
|
407 | + * @param WP_REST_Request $request |
|
408 | + * @return array |
|
409 | + * @throws EE_Error |
|
410 | + * @throws InvalidArgumentException |
|
411 | + * @throws InvalidDataTypeException |
|
412 | + * @throws InvalidInterfaceException |
|
413 | + * @throws RestException |
|
414 | + * @throws DomainException |
|
415 | + */ |
|
416 | + public function addRelation(EEM_Base $model, EE_Model_Relation_Base $relation, WP_REST_Request $request) |
|
417 | + { |
|
418 | + list($model_obj, $other_obj) = $this->getBothModelObjects($model, $relation, $request); |
|
419 | + $extra_params = array(); |
|
420 | + if ($relation instanceof EE_HABTM_Relation) { |
|
421 | + $extra_params = array_intersect_key( |
|
422 | + ModelDataTranslator::prepareConditionsQueryParamsForModels( |
|
423 | + $request->get_body_params(), |
|
424 | + $relation->get_join_model(), |
|
425 | + $this->getModelVersionInfo()->requestedVersion(), |
|
426 | + true |
|
427 | + ), |
|
428 | + $relation->getNonKeyFields() |
|
429 | + ); |
|
430 | + } |
|
431 | + // Add a relation. |
|
432 | + $related_obj = $model_obj->_add_relation_to( |
|
433 | + $other_obj, |
|
434 | + $relation->get_other_model()->get_this_model_name(), |
|
435 | + $extra_params |
|
436 | + ); |
|
437 | + $response = array( |
|
438 | + strtolower($model->get_this_model_name()) => $this->returnModelObjAsJsonResponse($model_obj, $request), |
|
439 | + strtolower($relation->get_other_model()->get_this_model_name()) => $this->returnModelObjAsJsonResponse($related_obj, $request), |
|
440 | + ); |
|
441 | + if ($relation instanceof EE_HABTM_Relation) { |
|
442 | + $join_model_obj = $relation->get_join_model()->get_one( |
|
443 | + array( |
|
444 | + array( |
|
445 | + $model->primary_key_name() => $model_obj->ID(), |
|
446 | + $relation->get_other_model()->primary_key_name() => $related_obj->ID() |
|
447 | + ) |
|
448 | + ) |
|
449 | + ); |
|
450 | + $response['join'][ strtolower($relation->get_join_model()->get_this_model_name()) ] = $this->returnModelObjAsJsonResponse($join_model_obj, $request); |
|
451 | + } |
|
452 | + return $response; |
|
453 | + } |
|
454 | 454 | |
455 | 455 | |
456 | - /** |
|
457 | - * Removes the relation between the specified models (if it exists). |
|
458 | - * @since 4.9.76.p |
|
459 | - * @param WP_REST_Request $request |
|
460 | - * @return WP_REST_Response |
|
461 | - */ |
|
462 | - public static function handleRequestRemoveRelation(WP_REST_Request $request, $version, $model_name, $related_model_name) |
|
463 | - { |
|
464 | - $controller = new Write(); |
|
465 | - try { |
|
466 | - $controller->setRequestedVersion($version); |
|
467 | - $main_model = $controller->getModelVersionInfo()->loadModel($model_name); |
|
468 | - return $controller->sendResponse( |
|
469 | - $controller->removeRelation( |
|
470 | - $main_model, |
|
471 | - $main_model->related_settings_for($related_model_name), |
|
472 | - $request |
|
473 | - ) |
|
474 | - ); |
|
475 | - } catch (Exception $e) { |
|
476 | - return $controller->sendResponse($e); |
|
477 | - } |
|
478 | - } |
|
456 | + /** |
|
457 | + * Removes the relation between the specified models (if it exists). |
|
458 | + * @since 4.9.76.p |
|
459 | + * @param WP_REST_Request $request |
|
460 | + * @return WP_REST_Response |
|
461 | + */ |
|
462 | + public static function handleRequestRemoveRelation(WP_REST_Request $request, $version, $model_name, $related_model_name) |
|
463 | + { |
|
464 | + $controller = new Write(); |
|
465 | + try { |
|
466 | + $controller->setRequestedVersion($version); |
|
467 | + $main_model = $controller->getModelVersionInfo()->loadModel($model_name); |
|
468 | + return $controller->sendResponse( |
|
469 | + $controller->removeRelation( |
|
470 | + $main_model, |
|
471 | + $main_model->related_settings_for($related_model_name), |
|
472 | + $request |
|
473 | + ) |
|
474 | + ); |
|
475 | + } catch (Exception $e) { |
|
476 | + return $controller->sendResponse($e); |
|
477 | + } |
|
478 | + } |
|
479 | 479 | |
480 | - /** |
|
481 | - * Adds a relation between the two model specified model objects. |
|
482 | - * @since 4.9.76.p |
|
483 | - * @param EEM_Base $model |
|
484 | - * @param EE_Model_Relation_Base $relation |
|
485 | - * @param WP_REST_Request $request |
|
486 | - * @return array |
|
487 | - * @throws DomainException |
|
488 | - * @throws EE_Error |
|
489 | - * @throws InvalidArgumentException |
|
490 | - * @throws InvalidDataTypeException |
|
491 | - * @throws InvalidInterfaceException |
|
492 | - * @throws RestException |
|
493 | - */ |
|
494 | - public function removeRelation(EEM_Base $model, EE_Model_Relation_Base $relation, WP_REST_Request $request) |
|
495 | - { |
|
496 | - // This endpoint doesn't accept body parameters (it's understandable to think it might, so let developers know |
|
497 | - // up-front that it doesn't.) |
|
498 | - if (!empty($request->get_body_params())) { |
|
499 | - $body_params = $request->get_body_params(); |
|
500 | - throw new RestException( |
|
501 | - 'invalid_field', |
|
502 | - sprintf( |
|
503 | - esc_html__('This endpoint doesn\'t accept post body arguments, you sent in %1$s', 'event_espresso'), |
|
504 | - implode(array_keys($body_params)) |
|
505 | - ) |
|
506 | - ); |
|
507 | - } |
|
508 | - list($model_obj, $other_obj) = $this->getBothModelObjects($model, $relation, $request); |
|
509 | - // Remember the old relation, if it used a join entry. |
|
510 | - $join_model_obj = null; |
|
511 | - if ($relation instanceof EE_HABTM_Relation) { |
|
512 | - $join_model_obj = $relation->get_join_model()->get_one( |
|
513 | - array( |
|
514 | - array( |
|
515 | - $model->primary_key_name() => $model_obj->ID(), |
|
516 | - $relation->get_other_model()->primary_key_name() => $other_obj->ID() |
|
517 | - ) |
|
518 | - ) |
|
519 | - ); |
|
520 | - } |
|
521 | - // Remove the relation. |
|
522 | - $related_obj = $model_obj->_remove_relation_to( |
|
523 | - $other_obj, |
|
524 | - $relation->get_other_model()->get_this_model_name() |
|
525 | - ); |
|
526 | - $response = array( |
|
527 | - strtolower($model->get_this_model_name()) => $this->returnModelObjAsJsonResponse($model_obj, $request), |
|
528 | - strtolower($relation->get_other_model()->get_this_model_name()) => $this->returnModelObjAsJsonResponse($related_obj, $request), |
|
529 | - ); |
|
530 | - if ($relation instanceof EE_HABTM_Relation) { |
|
531 | - $join_model_obj_after_removal = $relation->get_join_model()->get_one( |
|
532 | - array( |
|
533 | - array( |
|
534 | - $model->primary_key_name() => $model_obj->ID(), |
|
535 | - $relation->get_other_model()->primary_key_name() => $other_obj->ID() |
|
536 | - ) |
|
537 | - ) |
|
538 | - ); |
|
539 | - if ($join_model_obj instanceof EE_Base_Class) { |
|
540 | - $response['join'][ strtolower($relation->get_join_model()->get_this_model_name()) ] = $this->returnModelObjAsJsonResponse($join_model_obj, $request); |
|
541 | - } else { |
|
542 | - $response['join'][ strtolower($relation->get_join_model()->get_this_model_name()) ] = null; |
|
543 | - } |
|
544 | - } |
|
545 | - return $response; |
|
546 | - } |
|
480 | + /** |
|
481 | + * Adds a relation between the two model specified model objects. |
|
482 | + * @since 4.9.76.p |
|
483 | + * @param EEM_Base $model |
|
484 | + * @param EE_Model_Relation_Base $relation |
|
485 | + * @param WP_REST_Request $request |
|
486 | + * @return array |
|
487 | + * @throws DomainException |
|
488 | + * @throws EE_Error |
|
489 | + * @throws InvalidArgumentException |
|
490 | + * @throws InvalidDataTypeException |
|
491 | + * @throws InvalidInterfaceException |
|
492 | + * @throws RestException |
|
493 | + */ |
|
494 | + public function removeRelation(EEM_Base $model, EE_Model_Relation_Base $relation, WP_REST_Request $request) |
|
495 | + { |
|
496 | + // This endpoint doesn't accept body parameters (it's understandable to think it might, so let developers know |
|
497 | + // up-front that it doesn't.) |
|
498 | + if (!empty($request->get_body_params())) { |
|
499 | + $body_params = $request->get_body_params(); |
|
500 | + throw new RestException( |
|
501 | + 'invalid_field', |
|
502 | + sprintf( |
|
503 | + esc_html__('This endpoint doesn\'t accept post body arguments, you sent in %1$s', 'event_espresso'), |
|
504 | + implode(array_keys($body_params)) |
|
505 | + ) |
|
506 | + ); |
|
507 | + } |
|
508 | + list($model_obj, $other_obj) = $this->getBothModelObjects($model, $relation, $request); |
|
509 | + // Remember the old relation, if it used a join entry. |
|
510 | + $join_model_obj = null; |
|
511 | + if ($relation instanceof EE_HABTM_Relation) { |
|
512 | + $join_model_obj = $relation->get_join_model()->get_one( |
|
513 | + array( |
|
514 | + array( |
|
515 | + $model->primary_key_name() => $model_obj->ID(), |
|
516 | + $relation->get_other_model()->primary_key_name() => $other_obj->ID() |
|
517 | + ) |
|
518 | + ) |
|
519 | + ); |
|
520 | + } |
|
521 | + // Remove the relation. |
|
522 | + $related_obj = $model_obj->_remove_relation_to( |
|
523 | + $other_obj, |
|
524 | + $relation->get_other_model()->get_this_model_name() |
|
525 | + ); |
|
526 | + $response = array( |
|
527 | + strtolower($model->get_this_model_name()) => $this->returnModelObjAsJsonResponse($model_obj, $request), |
|
528 | + strtolower($relation->get_other_model()->get_this_model_name()) => $this->returnModelObjAsJsonResponse($related_obj, $request), |
|
529 | + ); |
|
530 | + if ($relation instanceof EE_HABTM_Relation) { |
|
531 | + $join_model_obj_after_removal = $relation->get_join_model()->get_one( |
|
532 | + array( |
|
533 | + array( |
|
534 | + $model->primary_key_name() => $model_obj->ID(), |
|
535 | + $relation->get_other_model()->primary_key_name() => $other_obj->ID() |
|
536 | + ) |
|
537 | + ) |
|
538 | + ); |
|
539 | + if ($join_model_obj instanceof EE_Base_Class) { |
|
540 | + $response['join'][ strtolower($relation->get_join_model()->get_this_model_name()) ] = $this->returnModelObjAsJsonResponse($join_model_obj, $request); |
|
541 | + } else { |
|
542 | + $response['join'][ strtolower($relation->get_join_model()->get_this_model_name()) ] = null; |
|
543 | + } |
|
544 | + } |
|
545 | + return $response; |
|
546 | + } |
|
547 | 547 | |
548 | - /** |
|
549 | - * Gets the model objects indicated by the model, relation object, and request. |
|
550 | - * Throws an exception if the first object doesn't exist, and currently if the related object also doesn't exist. |
|
551 | - * However, this behaviour may change, as we may add support for simultaneously creating and relating data. |
|
552 | - * @since 4.9.76.p |
|
553 | - * @param EEM_Base $model |
|
554 | - * @param EE_Model_Relation_Base $relation |
|
555 | - * @param WP_REST_Request $request |
|
556 | - * @return array { |
|
557 | - * @type EE_Base_Class $model_obj |
|
558 | - * @type EE_Base_Class|null $other_model_obj |
|
559 | - * } |
|
560 | - * @throws RestException |
|
561 | - */ |
|
562 | - protected function getBothModelObjects(EEM_Base $model, EE_Model_Relation_Base $relation, WP_REST_Request $request) |
|
563 | - { |
|
564 | - // Check generic caps. For now, we're only allowing access to this endpoint to full admins. |
|
565 | - Capabilities::verifyAtLeastPartialAccessTo($model, EEM_Base::caps_edit, 'edit'); |
|
566 | - $default_cap_to_check_for = EE_Restriction_Generator_Base::get_default_restrictions_cap(); |
|
567 | - if (! current_user_can($default_cap_to_check_for)) { |
|
568 | - throw new RestException( |
|
569 | - 'rest_cannot_edit_' . EEH_Inflector::pluralize_and_lower(($model->get_this_model_name())), |
|
570 | - sprintf( |
|
571 | - esc_html__( |
|
572 | - // @codingStandardsIgnoreStart |
|
573 | - 'For now, only those with the admin capability to "%1$s" are allowed to use the REST API to add relations in Event Espresso.', |
|
574 | - // @codingStandardsIgnoreEnd |
|
575 | - 'event_espresso' |
|
576 | - ), |
|
577 | - $default_cap_to_check_for |
|
578 | - ), |
|
579 | - array('status' => 403) |
|
580 | - ); |
|
581 | - } |
|
582 | - // Get the main model object. |
|
583 | - $model_obj = $this->getOneOrThrowException($model, $request->get_param('id')); |
|
584 | - // For now, we require the other model object to exist too. This might be relaxed later. |
|
585 | - $other_obj = $this->getOneOrThrowException($relation->get_other_model(), $request->get_param('related_id')); |
|
586 | - return array($model_obj,$other_obj); |
|
587 | - } |
|
548 | + /** |
|
549 | + * Gets the model objects indicated by the model, relation object, and request. |
|
550 | + * Throws an exception if the first object doesn't exist, and currently if the related object also doesn't exist. |
|
551 | + * However, this behaviour may change, as we may add support for simultaneously creating and relating data. |
|
552 | + * @since 4.9.76.p |
|
553 | + * @param EEM_Base $model |
|
554 | + * @param EE_Model_Relation_Base $relation |
|
555 | + * @param WP_REST_Request $request |
|
556 | + * @return array { |
|
557 | + * @type EE_Base_Class $model_obj |
|
558 | + * @type EE_Base_Class|null $other_model_obj |
|
559 | + * } |
|
560 | + * @throws RestException |
|
561 | + */ |
|
562 | + protected function getBothModelObjects(EEM_Base $model, EE_Model_Relation_Base $relation, WP_REST_Request $request) |
|
563 | + { |
|
564 | + // Check generic caps. For now, we're only allowing access to this endpoint to full admins. |
|
565 | + Capabilities::verifyAtLeastPartialAccessTo($model, EEM_Base::caps_edit, 'edit'); |
|
566 | + $default_cap_to_check_for = EE_Restriction_Generator_Base::get_default_restrictions_cap(); |
|
567 | + if (! current_user_can($default_cap_to_check_for)) { |
|
568 | + throw new RestException( |
|
569 | + 'rest_cannot_edit_' . EEH_Inflector::pluralize_and_lower(($model->get_this_model_name())), |
|
570 | + sprintf( |
|
571 | + esc_html__( |
|
572 | + // @codingStandardsIgnoreStart |
|
573 | + 'For now, only those with the admin capability to "%1$s" are allowed to use the REST API to add relations in Event Espresso.', |
|
574 | + // @codingStandardsIgnoreEnd |
|
575 | + 'event_espresso' |
|
576 | + ), |
|
577 | + $default_cap_to_check_for |
|
578 | + ), |
|
579 | + array('status' => 403) |
|
580 | + ); |
|
581 | + } |
|
582 | + // Get the main model object. |
|
583 | + $model_obj = $this->getOneOrThrowException($model, $request->get_param('id')); |
|
584 | + // For now, we require the other model object to exist too. This might be relaxed later. |
|
585 | + $other_obj = $this->getOneOrThrowException($relation->get_other_model(), $request->get_param('related_id')); |
|
586 | + return array($model_obj,$other_obj); |
|
587 | + } |
|
588 | 588 | |
589 | - /** |
|
590 | - * Gets the model with that ID or throws a REST exception. |
|
591 | - * @since 4.9.76.p |
|
592 | - * @param EEM_Base $model |
|
593 | - * @param $id |
|
594 | - * @return EE_Base_Class |
|
595 | - * @throws RestException |
|
596 | - */ |
|
597 | - protected function getOneOrThrowException(EEM_Base $model, $id) |
|
598 | - { |
|
599 | - $model_obj = $model->get_one_by_ID($id); |
|
600 | - // @todo: check they can permission for it. For now unnecessary because only full admins can use this endpoint. |
|
601 | - if ($model_obj instanceof EE_Base_Class) { |
|
602 | - return $model_obj; |
|
603 | - } |
|
604 | - $lowercase_model_name = strtolower($model->get_this_model_name()); |
|
605 | - throw new RestException( |
|
606 | - sprintf('rest_%s_invalid_id', $lowercase_model_name), |
|
607 | - sprintf(__('Invalid %s ID.', 'event_espresso'), $lowercase_model_name), |
|
608 | - array('status' => 404) |
|
609 | - ); |
|
610 | - } |
|
589 | + /** |
|
590 | + * Gets the model with that ID or throws a REST exception. |
|
591 | + * @since 4.9.76.p |
|
592 | + * @param EEM_Base $model |
|
593 | + * @param $id |
|
594 | + * @return EE_Base_Class |
|
595 | + * @throws RestException |
|
596 | + */ |
|
597 | + protected function getOneOrThrowException(EEM_Base $model, $id) |
|
598 | + { |
|
599 | + $model_obj = $model->get_one_by_ID($id); |
|
600 | + // @todo: check they can permission for it. For now unnecessary because only full admins can use this endpoint. |
|
601 | + if ($model_obj instanceof EE_Base_Class) { |
|
602 | + return $model_obj; |
|
603 | + } |
|
604 | + $lowercase_model_name = strtolower($model->get_this_model_name()); |
|
605 | + throw new RestException( |
|
606 | + sprintf('rest_%s_invalid_id', $lowercase_model_name), |
|
607 | + sprintf(__('Invalid %s ID.', 'event_espresso'), $lowercase_model_name), |
|
608 | + array('status' => 404) |
|
609 | + ); |
|
610 | + } |
|
611 | 611 | } |
@@ -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 | } |