@@ -17,330 +17,330 @@ |
||
| 17 | 17 | class EED_Recaptcha_Invisible extends EED_Module |
| 18 | 18 | { |
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * @var EE_Registration_Config $config |
|
| 22 | - */ |
|
| 23 | - private static $config; |
|
| 24 | - |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * @return EED_Module|EED_Recaptcha |
|
| 28 | - */ |
|
| 29 | - public static function instance() |
|
| 30 | - { |
|
| 31 | - return parent::get_instance(__CLASS__); |
|
| 32 | - } |
|
| 33 | - |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * @return void |
|
| 37 | - * @throws InvalidInterfaceException |
|
| 38 | - * @throws InvalidDataTypeException |
|
| 39 | - * @throws InvalidArgumentException |
|
| 40 | - */ |
|
| 41 | - public static function set_hooks() |
|
| 42 | - { |
|
| 43 | - EED_Recaptcha_Invisible::setProperties(); |
|
| 44 | - if (EED_Recaptcha_Invisible::useInvisibleRecaptcha()) { |
|
| 45 | - if (EED_Recaptcha_Invisible::protectForm('ticket_selector')) { |
|
| 46 | - // ticket selection |
|
| 47 | - add_filter( |
|
| 48 | - 'FHEE__EE_Ticket_Selector__after_ticket_selector_submit', |
|
| 49 | - array('EED_Recaptcha_Invisible', 'ticketSelectorForm'), |
|
| 50 | - 10, |
|
| 51 | - 3 |
|
| 52 | - ); |
|
| 53 | - add_action( |
|
| 54 | - 'EED_Ticket_Selector__process_ticket_selections__before', |
|
| 55 | - array('EED_Recaptcha_Invisible', 'processTicketSelectorForm') |
|
| 56 | - ); |
|
| 57 | - } |
|
| 58 | - if (EED_Recaptcha_Invisible::protectForm('registration_form')) { |
|
| 59 | - // checkout |
|
| 60 | - add_action( |
|
| 61 | - 'AHEE__EE_SPCO_Reg_Step__display_reg_form__reg_form', |
|
| 62 | - array('EED_Recaptcha_Invisible', 'spcoRegStepForm') |
|
| 63 | - ); |
|
| 64 | - add_filter( |
|
| 65 | - 'FHEE__EE_Form_Section_Proper__receive_form_submission__req_data', |
|
| 66 | - array('EED_Recaptcha_Invisible', 'receiveSpcoRegStepForm'), |
|
| 67 | - 10, |
|
| 68 | - 2 |
|
| 69 | - ); |
|
| 70 | - } |
|
| 71 | - add_action('loop_end', array('EED_Recaptcha_Invisible', 'localizeScriptVars')); |
|
| 72 | - } |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * @return void |
|
| 78 | - * @throws InvalidInterfaceException |
|
| 79 | - * @throws InvalidDataTypeException |
|
| 80 | - * @throws InvalidArgumentException |
|
| 81 | - */ |
|
| 82 | - public static function set_hooks_admin() |
|
| 83 | - { |
|
| 84 | - EED_Recaptcha_Invisible::setProperties(); |
|
| 85 | - if (EED_Recaptcha_Invisible::protectForm('ticket_selector')) { |
|
| 86 | - add_action( |
|
| 87 | - 'EED_Ticket_Selector__process_ticket_selections__before', |
|
| 88 | - array('EED_Recaptcha_Invisible', 'processTicketSelectorForm') |
|
| 89 | - ); |
|
| 90 | - } |
|
| 91 | - if (EED_Recaptcha_Invisible::protectForm('registration_form')) { |
|
| 92 | - add_filter( |
|
| 93 | - 'FHEE__EE_Form_Section_Proper__receive_form_submission__req_data', |
|
| 94 | - array('EED_Recaptcha_Invisible', 'receiveSpcoRegStepForm'), |
|
| 95 | - 10, |
|
| 96 | - 2 |
|
| 97 | - ); |
|
| 98 | - } |
|
| 99 | - // admin settings |
|
| 100 | - add_action( |
|
| 101 | - 'AHEE__Extend_Registration_Form_Admin_Page___reg_form_settings_template', |
|
| 102 | - array('EED_Recaptcha_Invisible', 'adminSettings') |
|
| 103 | - ); |
|
| 104 | - add_filter( |
|
| 105 | - 'FHEE__Extend_Registration_Form_Admin_Page___update_reg_form_settings__CFG_registration', |
|
| 106 | - array('EED_Recaptcha_Invisible', 'updateAdminSettings') |
|
| 107 | - ); |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * @return void |
|
| 113 | - * @throws InvalidInterfaceException |
|
| 114 | - * @throws InvalidDataTypeException |
|
| 115 | - * @throws InvalidArgumentException |
|
| 116 | - */ |
|
| 117 | - public static function setProperties() |
|
| 118 | - { |
|
| 119 | - |
|
| 120 | - EED_Recaptcha_Invisible::$config = EE_Registry::instance()->CFG->registration; |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * @return boolean |
|
| 126 | - */ |
|
| 127 | - public static function useInvisibleRecaptcha() |
|
| 128 | - { |
|
| 129 | - return EED_Recaptcha_Invisible::$config->use_captcha |
|
| 130 | - && EED_Recaptcha_Invisible::$config->recaptcha_theme === 'invisible'; |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - |
|
| 134 | - /** |
|
| 135 | - * @param string $form |
|
| 136 | - * @return boolean |
|
| 137 | - */ |
|
| 138 | - public static function protectForm($form) |
|
| 139 | - { |
|
| 140 | - return is_array(EED_Recaptcha_Invisible::$config->recaptcha_protected_forms) |
|
| 141 | - && in_array($form, EED_Recaptcha_Invisible::$config->recaptcha_protected_forms, true); |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * @return void |
|
| 147 | - * @throws InvalidInterfaceException |
|
| 148 | - * @throws InvalidDataTypeException |
|
| 149 | - * @throws InvalidArgumentException |
|
| 150 | - */ |
|
| 151 | - public static function localizeScriptVars() |
|
| 152 | - { |
|
| 153 | - /** @var \EventEspresso\core\services\request\Request $request */ |
|
| 154 | - $request = LoaderFactory::getLoader()->getShared('EventEspresso\core\services\request\Request'); |
|
| 155 | - if (! ($request->isAdmin() || $request->isFrontend())) { |
|
| 156 | - return; |
|
| 157 | - } |
|
| 158 | - wp_localize_script( |
|
| 159 | - EE_Invisible_Recaptcha_Input::SCRIPT_HANDLE_ESPRESSO_INVISIBLE_RECAPTCHA, |
|
| 160 | - 'eeRecaptcha', |
|
| 161 | - RecaptchaFactory::create()->getLocalizedVars() |
|
| 162 | - ); |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - |
|
| 166 | - /** |
|
| 167 | - * @return string |
|
| 168 | - */ |
|
| 169 | - public static function assetsUrl() |
|
| 170 | - { |
|
| 171 | - return plugin_dir_url(__FILE__) . 'assets' . DS; |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - |
|
| 175 | - /** |
|
| 176 | - * @param \WP $WP |
|
| 177 | - */ |
|
| 178 | - public function run($WP) |
|
| 179 | - { |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - |
|
| 183 | - /** |
|
| 184 | - * @param EE_Request $request |
|
| 185 | - * @return bool |
|
| 186 | - * @throws InvalidArgumentException |
|
| 187 | - * @throws InvalidDataTypeException |
|
| 188 | - * @throws InvalidInterfaceException |
|
| 189 | - * @throws RuntimeException |
|
| 190 | - */ |
|
| 191 | - public static function verifyToken(EE_Request $request) |
|
| 192 | - { |
|
| 193 | - return RecaptchaFactory::create()->verifyToken($request); |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - |
|
| 197 | - /** |
|
| 198 | - * @param EE_Form_Section_Proper $reg_form |
|
| 199 | - * @return void |
|
| 200 | - * @throws EE_Error |
|
| 201 | - * @throws InvalidArgumentException |
|
| 202 | - * @throws InvalidDataTypeException |
|
| 203 | - * @throws InvalidInterfaceException |
|
| 204 | - * @throws DomainException |
|
| 205 | - */ |
|
| 206 | - public static function spcoRegStepForm(EE_Form_Section_Proper $reg_form) |
|
| 207 | - { |
|
| 208 | - // do nothing if form isn't for a reg step or test has already been passed |
|
| 209 | - if (! EED_Recaptcha_Invisible::processSpcoRegStepForm($reg_form)) { |
|
| 210 | - return; |
|
| 211 | - } |
|
| 212 | - $default_hidden_inputs = $reg_form->get_subsection('default_hidden_inputs'); |
|
| 213 | - if ($default_hidden_inputs instanceof EE_Form_Section_Proper) { |
|
| 214 | - $invisible_recaptcha = RecaptchaFactory::create(); |
|
| 215 | - $invisible_recaptcha->addToFormSection($default_hidden_inputs); |
|
| 216 | - } |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - |
|
| 220 | - /** |
|
| 221 | - * @param EE_Form_Section_Proper $reg_form |
|
| 222 | - * @return bool |
|
| 223 | - * @throws InvalidDataTypeException |
|
| 224 | - * @throws InvalidInterfaceException |
|
| 225 | - * @throws EE_Error |
|
| 226 | - * @throws InvalidArgumentException |
|
| 227 | - */ |
|
| 228 | - public static function processSpcoRegStepForm(EE_Form_Section_Proper $reg_form) |
|
| 229 | - { |
|
| 230 | - return strpos($reg_form->name(), 'reg-step-form') !== false |
|
| 231 | - && ! RecaptchaFactory::create()->recaptchaPassed(); |
|
| 232 | - } |
|
| 233 | - |
|
| 234 | - |
|
| 235 | - /** |
|
| 236 | - * @param array|null $req_data |
|
| 237 | - * @param EE_Form_Section_Proper $reg_form |
|
| 238 | - * @return array |
|
| 239 | - * @throws EE_Error |
|
| 240 | - * @throws InvalidArgumentException |
|
| 241 | - * @throws InvalidDataTypeException |
|
| 242 | - * @throws InvalidInterfaceException |
|
| 243 | - * @throws RuntimeException |
|
| 244 | - */ |
|
| 245 | - public static function receiveSpcoRegStepForm($req_data = array(), EE_Form_Section_Proper $reg_form) |
|
| 246 | - { |
|
| 247 | - // do nothing if form isn't for a reg step or test has already been passed |
|
| 248 | - if (! EED_Recaptcha_Invisible::processSpcoRegStepForm($reg_form)) { |
|
| 249 | - return $req_data; |
|
| 250 | - } |
|
| 251 | - /** @var EE_Request $request */ |
|
| 252 | - $request = LoaderFactory::getLoader()->getShared('EE_Request'); |
|
| 253 | - if (! EED_Recaptcha_Invisible::verifyToken($request)) { |
|
| 254 | - if ($request->isAjax()) { |
|
| 255 | - $json_response = new EE_SPCO_JSON_Response(); |
|
| 256 | - $json_response->echoAndExit(); |
|
| 257 | - } |
|
| 258 | - EEH_URL::safeRedirectAndExit( |
|
| 259 | - EE_Registry::instance()->CFG->core->reg_page_url() |
|
| 260 | - ); |
|
| 261 | - } |
|
| 262 | - return $req_data; |
|
| 263 | - } |
|
| 264 | - |
|
| 265 | - |
|
| 266 | - /** |
|
| 267 | - * @param string $html |
|
| 268 | - * @param EE_Event $event |
|
| 269 | - * @param bool $iframe |
|
| 270 | - * @return string |
|
| 271 | - * @throws EE_Error |
|
| 272 | - * @throws InvalidArgumentException |
|
| 273 | - * @throws InvalidDataTypeException |
|
| 274 | - * @throws InvalidInterfaceException |
|
| 275 | - * @throws ReflectionException |
|
| 276 | - * @throws DomainException |
|
| 277 | - */ |
|
| 278 | - public static function ticketSelectorForm($html = '', EE_Event $event, $iframe = false) |
|
| 279 | - { |
|
| 280 | - $recaptcha = RecaptchaFactory::create(); |
|
| 281 | - // do nothing if test has already been passed |
|
| 282 | - if ($recaptcha->recaptchaPassed()) { |
|
| 283 | - return $html; |
|
| 284 | - } |
|
| 285 | - $html .= $recaptcha->getInputHtml( |
|
| 286 | - array( |
|
| 287 | - 'recaptcha_id' => $event->ID(), |
|
| 288 | - 'iframe' => $iframe, |
|
| 289 | - 'localized_vars' => $recaptcha->getLocalizedVars(), |
|
| 290 | - ) |
|
| 291 | - ); |
|
| 292 | - return $html; |
|
| 293 | - } |
|
| 294 | - |
|
| 295 | - |
|
| 296 | - /** |
|
| 297 | - * @return void |
|
| 298 | - * @throws InvalidArgumentException |
|
| 299 | - * @throws InvalidInterfaceException |
|
| 300 | - * @throws InvalidDataTypeException |
|
| 301 | - * @throws RuntimeException |
|
| 302 | - */ |
|
| 303 | - public static function processTicketSelectorForm() |
|
| 304 | - { |
|
| 305 | - // do nothing if test has already been passed |
|
| 306 | - if (RecaptchaFactory::create()->recaptchaPassed()) { |
|
| 307 | - return; |
|
| 308 | - } |
|
| 309 | - /** @var EE_Request $request */ |
|
| 310 | - $request = LoaderFactory::getLoader()->getShared('EE_Request'); |
|
| 311 | - if (! EED_Recaptcha_Invisible::verifyToken($request)) { |
|
| 312 | - $event_id = $request->get('tkt-slctr-event-id'); |
|
| 313 | - $return_url = $request->is_set("tkt-slctr-return-url-{$event_id}") |
|
| 314 | - ? $request->get("tkt-slctr-return-url-{$event_id}") |
|
| 315 | - : get_permalink($event_id); |
|
| 316 | - EEH_URL::safeRedirectAndExit($return_url); |
|
| 317 | - } |
|
| 318 | - } |
|
| 319 | - |
|
| 320 | - |
|
| 321 | - /** |
|
| 322 | - * @throws EE_Error |
|
| 323 | - * @throws InvalidArgumentException |
|
| 324 | - * @throws InvalidDataTypeException |
|
| 325 | - * @throws InvalidInterfaceException |
|
| 326 | - */ |
|
| 327 | - public static function adminSettings() |
|
| 328 | - { |
|
| 329 | - RecaptchaFactory::getAdminModule()->adminSettings(); |
|
| 330 | - } |
|
| 331 | - |
|
| 332 | - |
|
| 333 | - /** |
|
| 334 | - * @param EE_Registration_Config $EE_Registration_Config |
|
| 335 | - * @return EE_Registration_Config |
|
| 336 | - * @throws EE_Error |
|
| 337 | - * @throws InvalidArgumentException |
|
| 338 | - * @throws InvalidDataTypeException |
|
| 339 | - * @throws InvalidInterfaceException |
|
| 340 | - * @throws ReflectionException |
|
| 341 | - */ |
|
| 342 | - public static function updateAdminSettings(EE_Registration_Config $EE_Registration_Config) |
|
| 343 | - { |
|
| 344 | - return RecaptchaFactory::getAdminModule()->updateAdminSettings($EE_Registration_Config); |
|
| 345 | - } |
|
| 20 | + /** |
|
| 21 | + * @var EE_Registration_Config $config |
|
| 22 | + */ |
|
| 23 | + private static $config; |
|
| 24 | + |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * @return EED_Module|EED_Recaptcha |
|
| 28 | + */ |
|
| 29 | + public static function instance() |
|
| 30 | + { |
|
| 31 | + return parent::get_instance(__CLASS__); |
|
| 32 | + } |
|
| 33 | + |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * @return void |
|
| 37 | + * @throws InvalidInterfaceException |
|
| 38 | + * @throws InvalidDataTypeException |
|
| 39 | + * @throws InvalidArgumentException |
|
| 40 | + */ |
|
| 41 | + public static function set_hooks() |
|
| 42 | + { |
|
| 43 | + EED_Recaptcha_Invisible::setProperties(); |
|
| 44 | + if (EED_Recaptcha_Invisible::useInvisibleRecaptcha()) { |
|
| 45 | + if (EED_Recaptcha_Invisible::protectForm('ticket_selector')) { |
|
| 46 | + // ticket selection |
|
| 47 | + add_filter( |
|
| 48 | + 'FHEE__EE_Ticket_Selector__after_ticket_selector_submit', |
|
| 49 | + array('EED_Recaptcha_Invisible', 'ticketSelectorForm'), |
|
| 50 | + 10, |
|
| 51 | + 3 |
|
| 52 | + ); |
|
| 53 | + add_action( |
|
| 54 | + 'EED_Ticket_Selector__process_ticket_selections__before', |
|
| 55 | + array('EED_Recaptcha_Invisible', 'processTicketSelectorForm') |
|
| 56 | + ); |
|
| 57 | + } |
|
| 58 | + if (EED_Recaptcha_Invisible::protectForm('registration_form')) { |
|
| 59 | + // checkout |
|
| 60 | + add_action( |
|
| 61 | + 'AHEE__EE_SPCO_Reg_Step__display_reg_form__reg_form', |
|
| 62 | + array('EED_Recaptcha_Invisible', 'spcoRegStepForm') |
|
| 63 | + ); |
|
| 64 | + add_filter( |
|
| 65 | + 'FHEE__EE_Form_Section_Proper__receive_form_submission__req_data', |
|
| 66 | + array('EED_Recaptcha_Invisible', 'receiveSpcoRegStepForm'), |
|
| 67 | + 10, |
|
| 68 | + 2 |
|
| 69 | + ); |
|
| 70 | + } |
|
| 71 | + add_action('loop_end', array('EED_Recaptcha_Invisible', 'localizeScriptVars')); |
|
| 72 | + } |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * @return void |
|
| 78 | + * @throws InvalidInterfaceException |
|
| 79 | + * @throws InvalidDataTypeException |
|
| 80 | + * @throws InvalidArgumentException |
|
| 81 | + */ |
|
| 82 | + public static function set_hooks_admin() |
|
| 83 | + { |
|
| 84 | + EED_Recaptcha_Invisible::setProperties(); |
|
| 85 | + if (EED_Recaptcha_Invisible::protectForm('ticket_selector')) { |
|
| 86 | + add_action( |
|
| 87 | + 'EED_Ticket_Selector__process_ticket_selections__before', |
|
| 88 | + array('EED_Recaptcha_Invisible', 'processTicketSelectorForm') |
|
| 89 | + ); |
|
| 90 | + } |
|
| 91 | + if (EED_Recaptcha_Invisible::protectForm('registration_form')) { |
|
| 92 | + add_filter( |
|
| 93 | + 'FHEE__EE_Form_Section_Proper__receive_form_submission__req_data', |
|
| 94 | + array('EED_Recaptcha_Invisible', 'receiveSpcoRegStepForm'), |
|
| 95 | + 10, |
|
| 96 | + 2 |
|
| 97 | + ); |
|
| 98 | + } |
|
| 99 | + // admin settings |
|
| 100 | + add_action( |
|
| 101 | + 'AHEE__Extend_Registration_Form_Admin_Page___reg_form_settings_template', |
|
| 102 | + array('EED_Recaptcha_Invisible', 'adminSettings') |
|
| 103 | + ); |
|
| 104 | + add_filter( |
|
| 105 | + 'FHEE__Extend_Registration_Form_Admin_Page___update_reg_form_settings__CFG_registration', |
|
| 106 | + array('EED_Recaptcha_Invisible', 'updateAdminSettings') |
|
| 107 | + ); |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * @return void |
|
| 113 | + * @throws InvalidInterfaceException |
|
| 114 | + * @throws InvalidDataTypeException |
|
| 115 | + * @throws InvalidArgumentException |
|
| 116 | + */ |
|
| 117 | + public static function setProperties() |
|
| 118 | + { |
|
| 119 | + |
|
| 120 | + EED_Recaptcha_Invisible::$config = EE_Registry::instance()->CFG->registration; |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * @return boolean |
|
| 126 | + */ |
|
| 127 | + public static function useInvisibleRecaptcha() |
|
| 128 | + { |
|
| 129 | + return EED_Recaptcha_Invisible::$config->use_captcha |
|
| 130 | + && EED_Recaptcha_Invisible::$config->recaptcha_theme === 'invisible'; |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + |
|
| 134 | + /** |
|
| 135 | + * @param string $form |
|
| 136 | + * @return boolean |
|
| 137 | + */ |
|
| 138 | + public static function protectForm($form) |
|
| 139 | + { |
|
| 140 | + return is_array(EED_Recaptcha_Invisible::$config->recaptcha_protected_forms) |
|
| 141 | + && in_array($form, EED_Recaptcha_Invisible::$config->recaptcha_protected_forms, true); |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + |
|
| 145 | + /** |
|
| 146 | + * @return void |
|
| 147 | + * @throws InvalidInterfaceException |
|
| 148 | + * @throws InvalidDataTypeException |
|
| 149 | + * @throws InvalidArgumentException |
|
| 150 | + */ |
|
| 151 | + public static function localizeScriptVars() |
|
| 152 | + { |
|
| 153 | + /** @var \EventEspresso\core\services\request\Request $request */ |
|
| 154 | + $request = LoaderFactory::getLoader()->getShared('EventEspresso\core\services\request\Request'); |
|
| 155 | + if (! ($request->isAdmin() || $request->isFrontend())) { |
|
| 156 | + return; |
|
| 157 | + } |
|
| 158 | + wp_localize_script( |
|
| 159 | + EE_Invisible_Recaptcha_Input::SCRIPT_HANDLE_ESPRESSO_INVISIBLE_RECAPTCHA, |
|
| 160 | + 'eeRecaptcha', |
|
| 161 | + RecaptchaFactory::create()->getLocalizedVars() |
|
| 162 | + ); |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + |
|
| 166 | + /** |
|
| 167 | + * @return string |
|
| 168 | + */ |
|
| 169 | + public static function assetsUrl() |
|
| 170 | + { |
|
| 171 | + return plugin_dir_url(__FILE__) . 'assets' . DS; |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + |
|
| 175 | + /** |
|
| 176 | + * @param \WP $WP |
|
| 177 | + */ |
|
| 178 | + public function run($WP) |
|
| 179 | + { |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + |
|
| 183 | + /** |
|
| 184 | + * @param EE_Request $request |
|
| 185 | + * @return bool |
|
| 186 | + * @throws InvalidArgumentException |
|
| 187 | + * @throws InvalidDataTypeException |
|
| 188 | + * @throws InvalidInterfaceException |
|
| 189 | + * @throws RuntimeException |
|
| 190 | + */ |
|
| 191 | + public static function verifyToken(EE_Request $request) |
|
| 192 | + { |
|
| 193 | + return RecaptchaFactory::create()->verifyToken($request); |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + |
|
| 197 | + /** |
|
| 198 | + * @param EE_Form_Section_Proper $reg_form |
|
| 199 | + * @return void |
|
| 200 | + * @throws EE_Error |
|
| 201 | + * @throws InvalidArgumentException |
|
| 202 | + * @throws InvalidDataTypeException |
|
| 203 | + * @throws InvalidInterfaceException |
|
| 204 | + * @throws DomainException |
|
| 205 | + */ |
|
| 206 | + public static function spcoRegStepForm(EE_Form_Section_Proper $reg_form) |
|
| 207 | + { |
|
| 208 | + // do nothing if form isn't for a reg step or test has already been passed |
|
| 209 | + if (! EED_Recaptcha_Invisible::processSpcoRegStepForm($reg_form)) { |
|
| 210 | + return; |
|
| 211 | + } |
|
| 212 | + $default_hidden_inputs = $reg_form->get_subsection('default_hidden_inputs'); |
|
| 213 | + if ($default_hidden_inputs instanceof EE_Form_Section_Proper) { |
|
| 214 | + $invisible_recaptcha = RecaptchaFactory::create(); |
|
| 215 | + $invisible_recaptcha->addToFormSection($default_hidden_inputs); |
|
| 216 | + } |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + |
|
| 220 | + /** |
|
| 221 | + * @param EE_Form_Section_Proper $reg_form |
|
| 222 | + * @return bool |
|
| 223 | + * @throws InvalidDataTypeException |
|
| 224 | + * @throws InvalidInterfaceException |
|
| 225 | + * @throws EE_Error |
|
| 226 | + * @throws InvalidArgumentException |
|
| 227 | + */ |
|
| 228 | + public static function processSpcoRegStepForm(EE_Form_Section_Proper $reg_form) |
|
| 229 | + { |
|
| 230 | + return strpos($reg_form->name(), 'reg-step-form') !== false |
|
| 231 | + && ! RecaptchaFactory::create()->recaptchaPassed(); |
|
| 232 | + } |
|
| 233 | + |
|
| 234 | + |
|
| 235 | + /** |
|
| 236 | + * @param array|null $req_data |
|
| 237 | + * @param EE_Form_Section_Proper $reg_form |
|
| 238 | + * @return array |
|
| 239 | + * @throws EE_Error |
|
| 240 | + * @throws InvalidArgumentException |
|
| 241 | + * @throws InvalidDataTypeException |
|
| 242 | + * @throws InvalidInterfaceException |
|
| 243 | + * @throws RuntimeException |
|
| 244 | + */ |
|
| 245 | + public static function receiveSpcoRegStepForm($req_data = array(), EE_Form_Section_Proper $reg_form) |
|
| 246 | + { |
|
| 247 | + // do nothing if form isn't for a reg step or test has already been passed |
|
| 248 | + if (! EED_Recaptcha_Invisible::processSpcoRegStepForm($reg_form)) { |
|
| 249 | + return $req_data; |
|
| 250 | + } |
|
| 251 | + /** @var EE_Request $request */ |
|
| 252 | + $request = LoaderFactory::getLoader()->getShared('EE_Request'); |
|
| 253 | + if (! EED_Recaptcha_Invisible::verifyToken($request)) { |
|
| 254 | + if ($request->isAjax()) { |
|
| 255 | + $json_response = new EE_SPCO_JSON_Response(); |
|
| 256 | + $json_response->echoAndExit(); |
|
| 257 | + } |
|
| 258 | + EEH_URL::safeRedirectAndExit( |
|
| 259 | + EE_Registry::instance()->CFG->core->reg_page_url() |
|
| 260 | + ); |
|
| 261 | + } |
|
| 262 | + return $req_data; |
|
| 263 | + } |
|
| 264 | + |
|
| 265 | + |
|
| 266 | + /** |
|
| 267 | + * @param string $html |
|
| 268 | + * @param EE_Event $event |
|
| 269 | + * @param bool $iframe |
|
| 270 | + * @return string |
|
| 271 | + * @throws EE_Error |
|
| 272 | + * @throws InvalidArgumentException |
|
| 273 | + * @throws InvalidDataTypeException |
|
| 274 | + * @throws InvalidInterfaceException |
|
| 275 | + * @throws ReflectionException |
|
| 276 | + * @throws DomainException |
|
| 277 | + */ |
|
| 278 | + public static function ticketSelectorForm($html = '', EE_Event $event, $iframe = false) |
|
| 279 | + { |
|
| 280 | + $recaptcha = RecaptchaFactory::create(); |
|
| 281 | + // do nothing if test has already been passed |
|
| 282 | + if ($recaptcha->recaptchaPassed()) { |
|
| 283 | + return $html; |
|
| 284 | + } |
|
| 285 | + $html .= $recaptcha->getInputHtml( |
|
| 286 | + array( |
|
| 287 | + 'recaptcha_id' => $event->ID(), |
|
| 288 | + 'iframe' => $iframe, |
|
| 289 | + 'localized_vars' => $recaptcha->getLocalizedVars(), |
|
| 290 | + ) |
|
| 291 | + ); |
|
| 292 | + return $html; |
|
| 293 | + } |
|
| 294 | + |
|
| 295 | + |
|
| 296 | + /** |
|
| 297 | + * @return void |
|
| 298 | + * @throws InvalidArgumentException |
|
| 299 | + * @throws InvalidInterfaceException |
|
| 300 | + * @throws InvalidDataTypeException |
|
| 301 | + * @throws RuntimeException |
|
| 302 | + */ |
|
| 303 | + public static function processTicketSelectorForm() |
|
| 304 | + { |
|
| 305 | + // do nothing if test has already been passed |
|
| 306 | + if (RecaptchaFactory::create()->recaptchaPassed()) { |
|
| 307 | + return; |
|
| 308 | + } |
|
| 309 | + /** @var EE_Request $request */ |
|
| 310 | + $request = LoaderFactory::getLoader()->getShared('EE_Request'); |
|
| 311 | + if (! EED_Recaptcha_Invisible::verifyToken($request)) { |
|
| 312 | + $event_id = $request->get('tkt-slctr-event-id'); |
|
| 313 | + $return_url = $request->is_set("tkt-slctr-return-url-{$event_id}") |
|
| 314 | + ? $request->get("tkt-slctr-return-url-{$event_id}") |
|
| 315 | + : get_permalink($event_id); |
|
| 316 | + EEH_URL::safeRedirectAndExit($return_url); |
|
| 317 | + } |
|
| 318 | + } |
|
| 319 | + |
|
| 320 | + |
|
| 321 | + /** |
|
| 322 | + * @throws EE_Error |
|
| 323 | + * @throws InvalidArgumentException |
|
| 324 | + * @throws InvalidDataTypeException |
|
| 325 | + * @throws InvalidInterfaceException |
|
| 326 | + */ |
|
| 327 | + public static function adminSettings() |
|
| 328 | + { |
|
| 329 | + RecaptchaFactory::getAdminModule()->adminSettings(); |
|
| 330 | + } |
|
| 331 | + |
|
| 332 | + |
|
| 333 | + /** |
|
| 334 | + * @param EE_Registration_Config $EE_Registration_Config |
|
| 335 | + * @return EE_Registration_Config |
|
| 336 | + * @throws EE_Error |
|
| 337 | + * @throws InvalidArgumentException |
|
| 338 | + * @throws InvalidDataTypeException |
|
| 339 | + * @throws InvalidInterfaceException |
|
| 340 | + * @throws ReflectionException |
|
| 341 | + */ |
|
| 342 | + public static function updateAdminSettings(EE_Registration_Config $EE_Registration_Config) |
|
| 343 | + { |
|
| 344 | + return RecaptchaFactory::getAdminModule()->updateAdminSettings($EE_Registration_Config); |
|
| 345 | + } |
|
| 346 | 346 | } |
@@ -152,7 +152,7 @@ discard block |
||
| 152 | 152 | { |
| 153 | 153 | /** @var \EventEspresso\core\services\request\Request $request */ |
| 154 | 154 | $request = LoaderFactory::getLoader()->getShared('EventEspresso\core\services\request\Request'); |
| 155 | - if (! ($request->isAdmin() || $request->isFrontend())) { |
|
| 155 | + if ( ! ($request->isAdmin() || $request->isFrontend())) { |
|
| 156 | 156 | return; |
| 157 | 157 | } |
| 158 | 158 | wp_localize_script( |
@@ -168,7 +168,7 @@ discard block |
||
| 168 | 168 | */ |
| 169 | 169 | public static function assetsUrl() |
| 170 | 170 | { |
| 171 | - return plugin_dir_url(__FILE__) . 'assets' . DS; |
|
| 171 | + return plugin_dir_url(__FILE__).'assets'.DS; |
|
| 172 | 172 | } |
| 173 | 173 | |
| 174 | 174 | |
@@ -206,7 +206,7 @@ discard block |
||
| 206 | 206 | public static function spcoRegStepForm(EE_Form_Section_Proper $reg_form) |
| 207 | 207 | { |
| 208 | 208 | // do nothing if form isn't for a reg step or test has already been passed |
| 209 | - if (! EED_Recaptcha_Invisible::processSpcoRegStepForm($reg_form)) { |
|
| 209 | + if ( ! EED_Recaptcha_Invisible::processSpcoRegStepForm($reg_form)) { |
|
| 210 | 210 | return; |
| 211 | 211 | } |
| 212 | 212 | $default_hidden_inputs = $reg_form->get_subsection('default_hidden_inputs'); |
@@ -245,12 +245,12 @@ discard block |
||
| 245 | 245 | public static function receiveSpcoRegStepForm($req_data = array(), EE_Form_Section_Proper $reg_form) |
| 246 | 246 | { |
| 247 | 247 | // do nothing if form isn't for a reg step or test has already been passed |
| 248 | - if (! EED_Recaptcha_Invisible::processSpcoRegStepForm($reg_form)) { |
|
| 248 | + if ( ! EED_Recaptcha_Invisible::processSpcoRegStepForm($reg_form)) { |
|
| 249 | 249 | return $req_data; |
| 250 | 250 | } |
| 251 | 251 | /** @var EE_Request $request */ |
| 252 | 252 | $request = LoaderFactory::getLoader()->getShared('EE_Request'); |
| 253 | - if (! EED_Recaptcha_Invisible::verifyToken($request)) { |
|
| 253 | + if ( ! EED_Recaptcha_Invisible::verifyToken($request)) { |
|
| 254 | 254 | if ($request->isAjax()) { |
| 255 | 255 | $json_response = new EE_SPCO_JSON_Response(); |
| 256 | 256 | $json_response->echoAndExit(); |
@@ -308,7 +308,7 @@ discard block |
||
| 308 | 308 | } |
| 309 | 309 | /** @var EE_Request $request */ |
| 310 | 310 | $request = LoaderFactory::getLoader()->getShared('EE_Request'); |
| 311 | - if (! EED_Recaptcha_Invisible::verifyToken($request)) { |
|
| 311 | + if ( ! EED_Recaptcha_Invisible::verifyToken($request)) { |
|
| 312 | 312 | $event_id = $request->get('tkt-slctr-event-id'); |
| 313 | 313 | $return_url = $request->is_set("tkt-slctr-return-url-{$event_id}") |
| 314 | 314 | ? $request->get("tkt-slctr-return-url-{$event_id}") |
@@ -13,124 +13,124 @@ |
||
| 13 | 13 | interface RequestTypeContextCheckerInterface |
| 14 | 14 | { |
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * true if the current request involves some form of activation |
|
| 18 | - * |
|
| 19 | - * @return bool |
|
| 20 | - */ |
|
| 21 | - public function isActivation(); |
|
| 16 | + /** |
|
| 17 | + * true if the current request involves some form of activation |
|
| 18 | + * |
|
| 19 | + * @return bool |
|
| 20 | + */ |
|
| 21 | + public function isActivation(); |
|
| 22 | 22 | |
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * @param $is_activation |
|
| 26 | - * @return bool |
|
| 27 | - */ |
|
| 28 | - public function setIsActivation($is_activation); |
|
| 24 | + /** |
|
| 25 | + * @param $is_activation |
|
| 26 | + * @return bool |
|
| 27 | + */ |
|
| 28 | + public function setIsActivation($is_activation); |
|
| 29 | 29 | |
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * true if the current request is for the admin and is not being made via AJAX |
|
| 33 | - * |
|
| 34 | - * @return bool |
|
| 35 | - */ |
|
| 36 | - public function isAdmin(); |
|
| 31 | + /** |
|
| 32 | + * true if the current request is for the admin and is not being made via AJAX |
|
| 33 | + * |
|
| 34 | + * @return bool |
|
| 35 | + */ |
|
| 36 | + public function isAdmin(); |
|
| 37 | 37 | |
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * true if the current request is for the admin AND is being made via AJAX |
|
| 41 | - * and the ajax request contains the request parameter "ee_admin_ajax" |
|
| 42 | - * |
|
| 43 | - * @return bool |
|
| 44 | - */ |
|
| 45 | - public function isAdminAjax(); |
|
| 39 | + /** |
|
| 40 | + * true if the current request is for the admin AND is being made via AJAX |
|
| 41 | + * and the ajax request contains the request parameter "ee_admin_ajax" |
|
| 42 | + * |
|
| 43 | + * @return bool |
|
| 44 | + */ |
|
| 45 | + public function isAdminAjax(); |
|
| 46 | 46 | |
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * true if the current request is being made via AJAX... any AJAX |
|
| 50 | - * |
|
| 51 | - * @return bool |
|
| 52 | - */ |
|
| 53 | - public function isAjax(); |
|
| 48 | + /** |
|
| 49 | + * true if the current request is being made via AJAX... any AJAX |
|
| 50 | + * |
|
| 51 | + * @return bool |
|
| 52 | + */ |
|
| 53 | + public function isAjax(); |
|
| 54 | 54 | |
| 55 | 55 | |
| 56 | - /** |
|
| 57 | - * true if the current request is for the EE REST API |
|
| 58 | - * |
|
| 59 | - * @return bool |
|
| 60 | - */ |
|
| 61 | - public function isApi(); |
|
| 56 | + /** |
|
| 57 | + * true if the current request is for the EE REST API |
|
| 58 | + * |
|
| 59 | + * @return bool |
|
| 60 | + */ |
|
| 61 | + public function isApi(); |
|
| 62 | 62 | |
| 63 | 63 | |
| 64 | - /** |
|
| 65 | - * true if the current request is from the command line |
|
| 66 | - * |
|
| 67 | - * @return bool |
|
| 68 | - */ |
|
| 69 | - public function isCli(); |
|
| 64 | + /** |
|
| 65 | + * true if the current request is from the command line |
|
| 66 | + * |
|
| 67 | + * @return bool |
|
| 68 | + */ |
|
| 69 | + public function isCli(); |
|
| 70 | 70 | |
| 71 | 71 | |
| 72 | - /** |
|
| 73 | - * true if the current request is for a WP_Cron |
|
| 74 | - * |
|
| 75 | - * @return bool |
|
| 76 | - */ |
|
| 77 | - public function isCron(); |
|
| 72 | + /** |
|
| 73 | + * true if the current request is for a WP_Cron |
|
| 74 | + * |
|
| 75 | + * @return bool |
|
| 76 | + */ |
|
| 77 | + public function isCron(); |
|
| 78 | 78 | |
| 79 | 79 | |
| 80 | - /** |
|
| 81 | - * true if the current request is for either the EE admin or EE frontend AND is being made via AJAX |
|
| 82 | - * |
|
| 83 | - * @return bool |
|
| 84 | - */ |
|
| 85 | - public function isEeAjax(); |
|
| 80 | + /** |
|
| 81 | + * true if the current request is for either the EE admin or EE frontend AND is being made via AJAX |
|
| 82 | + * |
|
| 83 | + * @return bool |
|
| 84 | + */ |
|
| 85 | + public function isEeAjax(); |
|
| 86 | 86 | |
| 87 | 87 | |
| 88 | - /** |
|
| 89 | - * true if the current request is for a feed (ie: RSS) |
|
| 90 | - * |
|
| 91 | - * @return bool |
|
| 92 | - */ |
|
| 93 | - public function isFeed(); |
|
| 88 | + /** |
|
| 89 | + * true if the current request is for a feed (ie: RSS) |
|
| 90 | + * |
|
| 91 | + * @return bool |
|
| 92 | + */ |
|
| 93 | + public function isFeed(); |
|
| 94 | 94 | |
| 95 | 95 | |
| 96 | - /** |
|
| 97 | - * true if the current request is for the frontend and is not being made via AJAX |
|
| 98 | - * |
|
| 99 | - * @return bool |
|
| 100 | - */ |
|
| 101 | - public function isFrontend(); |
|
| 96 | + /** |
|
| 97 | + * true if the current request is for the frontend and is not being made via AJAX |
|
| 98 | + * |
|
| 99 | + * @return bool |
|
| 100 | + */ |
|
| 101 | + public function isFrontend(); |
|
| 102 | 102 | |
| 103 | 103 | |
| 104 | - /** |
|
| 105 | - * @return bool |
|
| 106 | - */ |
|
| 107 | - public function isFrontAjax(); |
|
| 104 | + /** |
|
| 105 | + * @return bool |
|
| 106 | + */ |
|
| 107 | + public function isFrontAjax(); |
|
| 108 | 108 | |
| 109 | 109 | |
| 110 | - /** |
|
| 111 | - * @return bool |
|
| 112 | - */ |
|
| 113 | - public function isIframe(); |
|
| 110 | + /** |
|
| 111 | + * @return bool |
|
| 112 | + */ |
|
| 113 | + public function isIframe(); |
|
| 114 | 114 | |
| 115 | 115 | |
| 116 | - /** |
|
| 117 | - * true if the current request is being made via AJAX but is NOT for EE related logic |
|
| 118 | - * |
|
| 119 | - * @return bool |
|
| 120 | - */ |
|
| 121 | - public function isOtherAjax(); |
|
| 116 | + /** |
|
| 117 | + * true if the current request is being made via AJAX but is NOT for EE related logic |
|
| 118 | + * |
|
| 119 | + * @return bool |
|
| 120 | + */ |
|
| 121 | + public function isOtherAjax(); |
|
| 122 | 122 | |
| 123 | 123 | |
| 124 | - /** |
|
| 125 | - * true if the current request is a loopback sent from WP core to test for errors |
|
| 126 | - * |
|
| 127 | - * @return bool |
|
| 128 | - */ |
|
| 129 | - public function isWordPressScrape(); |
|
| 124 | + /** |
|
| 125 | + * true if the current request is a loopback sent from WP core to test for errors |
|
| 126 | + * |
|
| 127 | + * @return bool |
|
| 128 | + */ |
|
| 129 | + public function isWordPressScrape(); |
|
| 130 | 130 | |
| 131 | 131 | |
| 132 | - /** |
|
| 133 | - * @return string |
|
| 134 | - */ |
|
| 135 | - public function slug(); |
|
| 132 | + /** |
|
| 133 | + * @return string |
|
| 134 | + */ |
|
| 135 | + public function slug(); |
|
| 136 | 136 | } |
@@ -16,195 +16,195 @@ |
||
| 16 | 16 | class RequestTypeContextChecker extends ContextChecker implements RequestTypeContextCheckerInterface |
| 17 | 17 | { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * @var RequestTypeContext $request_type |
|
| 21 | - */ |
|
| 22 | - private $request_type; |
|
| 23 | - |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * RequestTypeContextChecker constructor. |
|
| 27 | - * |
|
| 28 | - * @param RequestTypeContext $request_type |
|
| 29 | - */ |
|
| 30 | - public function __construct(RequestTypeContext $request_type) |
|
| 31 | - { |
|
| 32 | - $this->request_type = $request_type; |
|
| 33 | - parent::__construct( |
|
| 34 | - 'RequestTypeContextChecker', |
|
| 35 | - $this->request_type->validRequestTypes() |
|
| 36 | - ); |
|
| 37 | - } |
|
| 38 | - |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * true if the current request involves some form of activation |
|
| 42 | - * |
|
| 43 | - * @return bool |
|
| 44 | - */ |
|
| 45 | - public function isActivation() |
|
| 46 | - { |
|
| 47 | - return $this->request_type->isActivation(); |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * @param $is_activation |
|
| 53 | - * @return bool |
|
| 54 | - */ |
|
| 55 | - public function setIsActivation($is_activation) |
|
| 56 | - { |
|
| 57 | - return $this->request_type->setIsActivation($is_activation); |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * true if the current request is for the admin and is not being made via AJAX |
|
| 63 | - * |
|
| 64 | - * @return bool |
|
| 65 | - */ |
|
| 66 | - public function isAdmin() |
|
| 67 | - { |
|
| 68 | - return $this->request_type->slug() === RequestTypeContext::ADMIN; |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * true if the current request is for the admin AND is being made via AJAX |
|
| 74 | - * |
|
| 75 | - * @return bool |
|
| 76 | - */ |
|
| 77 | - public function isAdminAjax() |
|
| 78 | - { |
|
| 79 | - return $this->request_type->slug() === RequestTypeContext::AJAX_ADMIN; |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * true if the current request is being made via AJAX... any AJAX |
|
| 85 | - * |
|
| 86 | - * @return bool |
|
| 87 | - */ |
|
| 88 | - public function isAjax() |
|
| 89 | - { |
|
| 90 | - return $this->isEeAjax() || $this->isOtherAjax(); |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * true if the current request is for either the EE admin or EE frontend AND is being made via AJAX |
|
| 96 | - * |
|
| 97 | - * @return bool |
|
| 98 | - */ |
|
| 99 | - public function isEeAjax() |
|
| 100 | - { |
|
| 101 | - return $this->isAdminAjax() || $this->isFrontAjax(); |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * true if the current request is being made via AJAX but is NOT for EE related logic |
|
| 107 | - * |
|
| 108 | - * @return bool |
|
| 109 | - */ |
|
| 110 | - public function isOtherAjax() |
|
| 111 | - { |
|
| 112 | - return $this->request_type->slug() === RequestTypeContext::AJAX_OTHER; |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - /** |
|
| 116 | - * true if the current request is for the EE REST API |
|
| 117 | - * |
|
| 118 | - * @return bool |
|
| 119 | - */ |
|
| 120 | - public function isApi() |
|
| 121 | - { |
|
| 122 | - return $this->request_type->slug() === RequestTypeContext::API; |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * true if the current request is from the command line |
|
| 128 | - * |
|
| 129 | - * @return bool |
|
| 130 | - */ |
|
| 131 | - public function isCli() |
|
| 132 | - { |
|
| 133 | - return $this->request_type->slug() === RequestTypeContext::CLI; |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - |
|
| 137 | - /** |
|
| 138 | - * true if the current request is for a WP_Cron |
|
| 139 | - * |
|
| 140 | - * @return bool |
|
| 141 | - */ |
|
| 142 | - public function isCron() |
|
| 143 | - { |
|
| 144 | - return $this->request_type->slug() === RequestTypeContext::CRON; |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - |
|
| 148 | - /** |
|
| 149 | - * true if the current request is for a feed (ie: RSS) |
|
| 150 | - * |
|
| 151 | - * @return bool |
|
| 152 | - */ |
|
| 153 | - public function isFeed() |
|
| 154 | - { |
|
| 155 | - return $this->request_type->slug() === RequestTypeContext::FEED; |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - |
|
| 159 | - /** |
|
| 160 | - * true if the current request is for the frontend and is not being made via AJAX |
|
| 161 | - * |
|
| 162 | - * @return bool |
|
| 163 | - */ |
|
| 164 | - public function isFrontend() |
|
| 165 | - { |
|
| 166 | - return $this->request_type->slug() === RequestTypeContext::FRONTEND; |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - |
|
| 170 | - /** |
|
| 171 | - * true if the current request is for the frontend AND is being made via AJAX |
|
| 172 | - * |
|
| 173 | - * @return bool |
|
| 174 | - */ |
|
| 175 | - public function isFrontAjax() |
|
| 176 | - { |
|
| 177 | - return $this->request_type->slug() === RequestTypeContext::AJAX_FRONT; |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - |
|
| 181 | - /** |
|
| 182 | - * true if the current request is for content that is to be displayed within an iframe |
|
| 183 | - * |
|
| 184 | - * @return bool |
|
| 185 | - */ |
|
| 186 | - public function isIframe() |
|
| 187 | - { |
|
| 188 | - return $this->request_type->slug() === RequestTypeContext::IFRAME; |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - |
|
| 192 | - /** |
|
| 193 | - * true if the current request is a loopback sent from WP core to test for errors |
|
| 194 | - * |
|
| 195 | - * @return bool |
|
| 196 | - */ |
|
| 197 | - public function isWordPressScrape() |
|
| 198 | - { |
|
| 199 | - return $this->request_type->slug() === RequestTypeContext::WP_SCRAPE; |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - |
|
| 203 | - /** |
|
| 204 | - * @return string |
|
| 205 | - */ |
|
| 206 | - public function slug() |
|
| 207 | - { |
|
| 208 | - return $this->request_type->slug(); |
|
| 209 | - } |
|
| 19 | + /** |
|
| 20 | + * @var RequestTypeContext $request_type |
|
| 21 | + */ |
|
| 22 | + private $request_type; |
|
| 23 | + |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * RequestTypeContextChecker constructor. |
|
| 27 | + * |
|
| 28 | + * @param RequestTypeContext $request_type |
|
| 29 | + */ |
|
| 30 | + public function __construct(RequestTypeContext $request_type) |
|
| 31 | + { |
|
| 32 | + $this->request_type = $request_type; |
|
| 33 | + parent::__construct( |
|
| 34 | + 'RequestTypeContextChecker', |
|
| 35 | + $this->request_type->validRequestTypes() |
|
| 36 | + ); |
|
| 37 | + } |
|
| 38 | + |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * true if the current request involves some form of activation |
|
| 42 | + * |
|
| 43 | + * @return bool |
|
| 44 | + */ |
|
| 45 | + public function isActivation() |
|
| 46 | + { |
|
| 47 | + return $this->request_type->isActivation(); |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * @param $is_activation |
|
| 53 | + * @return bool |
|
| 54 | + */ |
|
| 55 | + public function setIsActivation($is_activation) |
|
| 56 | + { |
|
| 57 | + return $this->request_type->setIsActivation($is_activation); |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * true if the current request is for the admin and is not being made via AJAX |
|
| 63 | + * |
|
| 64 | + * @return bool |
|
| 65 | + */ |
|
| 66 | + public function isAdmin() |
|
| 67 | + { |
|
| 68 | + return $this->request_type->slug() === RequestTypeContext::ADMIN; |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * true if the current request is for the admin AND is being made via AJAX |
|
| 74 | + * |
|
| 75 | + * @return bool |
|
| 76 | + */ |
|
| 77 | + public function isAdminAjax() |
|
| 78 | + { |
|
| 79 | + return $this->request_type->slug() === RequestTypeContext::AJAX_ADMIN; |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * true if the current request is being made via AJAX... any AJAX |
|
| 85 | + * |
|
| 86 | + * @return bool |
|
| 87 | + */ |
|
| 88 | + public function isAjax() |
|
| 89 | + { |
|
| 90 | + return $this->isEeAjax() || $this->isOtherAjax(); |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * true if the current request is for either the EE admin or EE frontend AND is being made via AJAX |
|
| 96 | + * |
|
| 97 | + * @return bool |
|
| 98 | + */ |
|
| 99 | + public function isEeAjax() |
|
| 100 | + { |
|
| 101 | + return $this->isAdminAjax() || $this->isFrontAjax(); |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * true if the current request is being made via AJAX but is NOT for EE related logic |
|
| 107 | + * |
|
| 108 | + * @return bool |
|
| 109 | + */ |
|
| 110 | + public function isOtherAjax() |
|
| 111 | + { |
|
| 112 | + return $this->request_type->slug() === RequestTypeContext::AJAX_OTHER; |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + /** |
|
| 116 | + * true if the current request is for the EE REST API |
|
| 117 | + * |
|
| 118 | + * @return bool |
|
| 119 | + */ |
|
| 120 | + public function isApi() |
|
| 121 | + { |
|
| 122 | + return $this->request_type->slug() === RequestTypeContext::API; |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * true if the current request is from the command line |
|
| 128 | + * |
|
| 129 | + * @return bool |
|
| 130 | + */ |
|
| 131 | + public function isCli() |
|
| 132 | + { |
|
| 133 | + return $this->request_type->slug() === RequestTypeContext::CLI; |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + |
|
| 137 | + /** |
|
| 138 | + * true if the current request is for a WP_Cron |
|
| 139 | + * |
|
| 140 | + * @return bool |
|
| 141 | + */ |
|
| 142 | + public function isCron() |
|
| 143 | + { |
|
| 144 | + return $this->request_type->slug() === RequestTypeContext::CRON; |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + |
|
| 148 | + /** |
|
| 149 | + * true if the current request is for a feed (ie: RSS) |
|
| 150 | + * |
|
| 151 | + * @return bool |
|
| 152 | + */ |
|
| 153 | + public function isFeed() |
|
| 154 | + { |
|
| 155 | + return $this->request_type->slug() === RequestTypeContext::FEED; |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + |
|
| 159 | + /** |
|
| 160 | + * true if the current request is for the frontend and is not being made via AJAX |
|
| 161 | + * |
|
| 162 | + * @return bool |
|
| 163 | + */ |
|
| 164 | + public function isFrontend() |
|
| 165 | + { |
|
| 166 | + return $this->request_type->slug() === RequestTypeContext::FRONTEND; |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + |
|
| 170 | + /** |
|
| 171 | + * true if the current request is for the frontend AND is being made via AJAX |
|
| 172 | + * |
|
| 173 | + * @return bool |
|
| 174 | + */ |
|
| 175 | + public function isFrontAjax() |
|
| 176 | + { |
|
| 177 | + return $this->request_type->slug() === RequestTypeContext::AJAX_FRONT; |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + |
|
| 181 | + /** |
|
| 182 | + * true if the current request is for content that is to be displayed within an iframe |
|
| 183 | + * |
|
| 184 | + * @return bool |
|
| 185 | + */ |
|
| 186 | + public function isIframe() |
|
| 187 | + { |
|
| 188 | + return $this->request_type->slug() === RequestTypeContext::IFRAME; |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + |
|
| 192 | + /** |
|
| 193 | + * true if the current request is a loopback sent from WP core to test for errors |
|
| 194 | + * |
|
| 195 | + * @return bool |
|
| 196 | + */ |
|
| 197 | + public function isWordPressScrape() |
|
| 198 | + { |
|
| 199 | + return $this->request_type->slug() === RequestTypeContext::WP_SCRAPE; |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + |
|
| 203 | + /** |
|
| 204 | + * @return string |
|
| 205 | + */ |
|
| 206 | + public function slug() |
|
| 207 | + { |
|
| 208 | + return $this->request_type->slug(); |
|
| 209 | + } |
|
| 210 | 210 | } |
@@ -19,137 +19,137 @@ |
||
| 19 | 19 | class RequestTypeContextDetector |
| 20 | 20 | { |
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * @var RequestTypeContextFactory $factory |
|
| 24 | - */ |
|
| 25 | - private $factory; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * @var RequestInterface $request |
|
| 29 | - */ |
|
| 30 | - private $request; |
|
| 31 | - |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * RequestTypeContextDetector constructor. |
|
| 35 | - * |
|
| 36 | - * @param RequestInterface $request |
|
| 37 | - * @param RequestTypeContextFactory $factory |
|
| 38 | - */ |
|
| 39 | - public function __construct(RequestInterface $request, RequestTypeContextFactory $factory) |
|
| 40 | - { |
|
| 41 | - $this->request = $request; |
|
| 42 | - $this->factory = $factory; |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * @return RequestTypeContext |
|
| 48 | - * @throws InvalidArgumentException |
|
| 49 | - */ |
|
| 50 | - public function detectRequestTypeContext() |
|
| 51 | - { |
|
| 52 | - // Detect error scrapes |
|
| 53 | - if ($this->request->getRequestParam('wp_scrape_key') !== null |
|
| 54 | - && $this->request->getRequestParam('wp_scrape_nonce') !== null |
|
| 55 | - ) { |
|
| 56 | - return $this->factory->create(RequestTypeContext::WP_SCRAPE); |
|
| 57 | - } |
|
| 58 | - // Detect EE REST API |
|
| 59 | - if ($this->isEspressoRestApiRequest()) { |
|
| 60 | - return $this->factory->create(RequestTypeContext::API); |
|
| 61 | - } |
|
| 62 | - // Detect AJAX |
|
| 63 | - if (defined('DOING_AJAX') && DOING_AJAX) { |
|
| 64 | - if (filter_var($this->request->getRequestParam('ee_front_ajax'), FILTER_VALIDATE_BOOLEAN)) { |
|
| 65 | - return $this->factory->create(RequestTypeContext::AJAX_FRONT); |
|
| 66 | - } |
|
| 67 | - if (filter_var($this->request->getRequestParam('ee_admin_ajax'), FILTER_VALIDATE_BOOLEAN)) { |
|
| 68 | - return $this->factory->create(RequestTypeContext::AJAX_ADMIN); |
|
| 69 | - } |
|
| 70 | - return $this->factory->create(RequestTypeContext::AJAX_OTHER); |
|
| 71 | - } |
|
| 72 | - // Detect WP_Cron |
|
| 73 | - if ($this->isCronRequest()) { |
|
| 74 | - return $this->factory->create(RequestTypeContext::CRON); |
|
| 75 | - } |
|
| 76 | - // Detect command line requests |
|
| 77 | - if (defined('WP_CLI') && WP_CLI) { |
|
| 78 | - return $this->factory->create(RequestTypeContext::CLI); |
|
| 79 | - } |
|
| 80 | - // detect WordPress admin (ie: "Dashboard") |
|
| 81 | - if (is_admin()) { |
|
| 82 | - return $this->factory->create(RequestTypeContext::ADMIN); |
|
| 83 | - } |
|
| 84 | - // Detect iFrames |
|
| 85 | - if ($this->isIframeRoute()) { |
|
| 86 | - return $this->factory->create(RequestTypeContext::IFRAME); |
|
| 87 | - } |
|
| 88 | - // Detect Feeds |
|
| 89 | - if ($this->isFeedRequest()) { |
|
| 90 | - return $this->factory->create(RequestTypeContext::FEED); |
|
| 91 | - } |
|
| 92 | - // and by process of elimination... |
|
| 93 | - return $this->factory->create(RequestTypeContext::FRONTEND); |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - |
|
| 97 | - /** |
|
| 98 | - * @return bool |
|
| 99 | - */ |
|
| 100 | - private function isEspressoRestApiRequest() |
|
| 101 | - { |
|
| 102 | - $ee_rest_url_prefix = RecommendedVersions::compareWordPressVersion('4.4.0') |
|
| 103 | - ? trim(rest_get_url_prefix(), '/') |
|
| 104 | - : 'wp-json'; |
|
| 105 | - $ee_rest_url_prefix .= '/' . Domain::API_NAMESPACE; |
|
| 106 | - return $this->uriPathMatches($ee_rest_url_prefix); |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * @return bool |
|
| 112 | - */ |
|
| 113 | - private function isCronRequest() |
|
| 114 | - { |
|
| 115 | - return $this->uriPathMatches('wp-cron.php'); |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * @return bool |
|
| 121 | - */ |
|
| 122 | - private function isFeedRequest() |
|
| 123 | - { |
|
| 124 | - return $this->uriPathMatches('feed'); |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - |
|
| 128 | - /** |
|
| 129 | - * @param string $component |
|
| 130 | - * @return bool |
|
| 131 | - */ |
|
| 132 | - private function uriPathMatches($component) |
|
| 133 | - { |
|
| 134 | - $request_uri = $this->request->requestUri(); |
|
| 135 | - $parts = explode('?', $request_uri); |
|
| 136 | - $path = trim(reset($parts), '/'); |
|
| 137 | - return strpos($path, $component) === 0; |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - |
|
| 141 | - /** |
|
| 142 | - * @return bool |
|
| 143 | - */ |
|
| 144 | - private function isIframeRoute() |
|
| 145 | - { |
|
| 146 | - $is_iframe_route = apply_filters( |
|
| 147 | - 'FHEE__EventEspresso_core_domain_services_contexts_RequestTypeContextDetector__isIframeRoute', |
|
| 148 | - $this->request->getRequestParam('event_list', '') === 'iframe' |
|
| 149 | - || $this->request->getRequestParam('ticket_selector', '') === 'iframe' |
|
| 150 | - || $this->request->getRequestParam('calendar', '') === 'iframe', |
|
| 151 | - $this |
|
| 152 | - ); |
|
| 153 | - return filter_var($is_iframe_route, FILTER_VALIDATE_BOOLEAN); |
|
| 154 | - } |
|
| 22 | + /** |
|
| 23 | + * @var RequestTypeContextFactory $factory |
|
| 24 | + */ |
|
| 25 | + private $factory; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * @var RequestInterface $request |
|
| 29 | + */ |
|
| 30 | + private $request; |
|
| 31 | + |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * RequestTypeContextDetector constructor. |
|
| 35 | + * |
|
| 36 | + * @param RequestInterface $request |
|
| 37 | + * @param RequestTypeContextFactory $factory |
|
| 38 | + */ |
|
| 39 | + public function __construct(RequestInterface $request, RequestTypeContextFactory $factory) |
|
| 40 | + { |
|
| 41 | + $this->request = $request; |
|
| 42 | + $this->factory = $factory; |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * @return RequestTypeContext |
|
| 48 | + * @throws InvalidArgumentException |
|
| 49 | + */ |
|
| 50 | + public function detectRequestTypeContext() |
|
| 51 | + { |
|
| 52 | + // Detect error scrapes |
|
| 53 | + if ($this->request->getRequestParam('wp_scrape_key') !== null |
|
| 54 | + && $this->request->getRequestParam('wp_scrape_nonce') !== null |
|
| 55 | + ) { |
|
| 56 | + return $this->factory->create(RequestTypeContext::WP_SCRAPE); |
|
| 57 | + } |
|
| 58 | + // Detect EE REST API |
|
| 59 | + if ($this->isEspressoRestApiRequest()) { |
|
| 60 | + return $this->factory->create(RequestTypeContext::API); |
|
| 61 | + } |
|
| 62 | + // Detect AJAX |
|
| 63 | + if (defined('DOING_AJAX') && DOING_AJAX) { |
|
| 64 | + if (filter_var($this->request->getRequestParam('ee_front_ajax'), FILTER_VALIDATE_BOOLEAN)) { |
|
| 65 | + return $this->factory->create(RequestTypeContext::AJAX_FRONT); |
|
| 66 | + } |
|
| 67 | + if (filter_var($this->request->getRequestParam('ee_admin_ajax'), FILTER_VALIDATE_BOOLEAN)) { |
|
| 68 | + return $this->factory->create(RequestTypeContext::AJAX_ADMIN); |
|
| 69 | + } |
|
| 70 | + return $this->factory->create(RequestTypeContext::AJAX_OTHER); |
|
| 71 | + } |
|
| 72 | + // Detect WP_Cron |
|
| 73 | + if ($this->isCronRequest()) { |
|
| 74 | + return $this->factory->create(RequestTypeContext::CRON); |
|
| 75 | + } |
|
| 76 | + // Detect command line requests |
|
| 77 | + if (defined('WP_CLI') && WP_CLI) { |
|
| 78 | + return $this->factory->create(RequestTypeContext::CLI); |
|
| 79 | + } |
|
| 80 | + // detect WordPress admin (ie: "Dashboard") |
|
| 81 | + if (is_admin()) { |
|
| 82 | + return $this->factory->create(RequestTypeContext::ADMIN); |
|
| 83 | + } |
|
| 84 | + // Detect iFrames |
|
| 85 | + if ($this->isIframeRoute()) { |
|
| 86 | + return $this->factory->create(RequestTypeContext::IFRAME); |
|
| 87 | + } |
|
| 88 | + // Detect Feeds |
|
| 89 | + if ($this->isFeedRequest()) { |
|
| 90 | + return $this->factory->create(RequestTypeContext::FEED); |
|
| 91 | + } |
|
| 92 | + // and by process of elimination... |
|
| 93 | + return $this->factory->create(RequestTypeContext::FRONTEND); |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + |
|
| 97 | + /** |
|
| 98 | + * @return bool |
|
| 99 | + */ |
|
| 100 | + private function isEspressoRestApiRequest() |
|
| 101 | + { |
|
| 102 | + $ee_rest_url_prefix = RecommendedVersions::compareWordPressVersion('4.4.0') |
|
| 103 | + ? trim(rest_get_url_prefix(), '/') |
|
| 104 | + : 'wp-json'; |
|
| 105 | + $ee_rest_url_prefix .= '/' . Domain::API_NAMESPACE; |
|
| 106 | + return $this->uriPathMatches($ee_rest_url_prefix); |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * @return bool |
|
| 112 | + */ |
|
| 113 | + private function isCronRequest() |
|
| 114 | + { |
|
| 115 | + return $this->uriPathMatches('wp-cron.php'); |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * @return bool |
|
| 121 | + */ |
|
| 122 | + private function isFeedRequest() |
|
| 123 | + { |
|
| 124 | + return $this->uriPathMatches('feed'); |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + |
|
| 128 | + /** |
|
| 129 | + * @param string $component |
|
| 130 | + * @return bool |
|
| 131 | + */ |
|
| 132 | + private function uriPathMatches($component) |
|
| 133 | + { |
|
| 134 | + $request_uri = $this->request->requestUri(); |
|
| 135 | + $parts = explode('?', $request_uri); |
|
| 136 | + $path = trim(reset($parts), '/'); |
|
| 137 | + return strpos($path, $component) === 0; |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + |
|
| 141 | + /** |
|
| 142 | + * @return bool |
|
| 143 | + */ |
|
| 144 | + private function isIframeRoute() |
|
| 145 | + { |
|
| 146 | + $is_iframe_route = apply_filters( |
|
| 147 | + 'FHEE__EventEspresso_core_domain_services_contexts_RequestTypeContextDetector__isIframeRoute', |
|
| 148 | + $this->request->getRequestParam('event_list', '') === 'iframe' |
|
| 149 | + || $this->request->getRequestParam('ticket_selector', '') === 'iframe' |
|
| 150 | + || $this->request->getRequestParam('calendar', '') === 'iframe', |
|
| 151 | + $this |
|
| 152 | + ); |
|
| 153 | + return filter_var($is_iframe_route, FILTER_VALIDATE_BOOLEAN); |
|
| 154 | + } |
|
| 155 | 155 | } |
@@ -16,143 +16,143 @@ |
||
| 16 | 16 | class RequestTypeContext extends Context |
| 17 | 17 | { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * indicates that the current request involves some form of activation |
|
| 21 | - */ |
|
| 22 | - const ACTIVATION = 'activation-request'; |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * indicates that the current request is for the admin but is not being made via AJAX |
|
| 26 | - */ |
|
| 27 | - const ADMIN = 'non-ajax-admin-request'; |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * indicates that the current request is for the admin AND is being made via AJAX |
|
| 31 | - */ |
|
| 32 | - const AJAX_ADMIN = 'admin-ajax-request'; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * indicates that the current request is for the frontend AND is being made via AJAX |
|
| 36 | - */ |
|
| 37 | - const AJAX_FRONT = 'frontend-ajax-request'; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * indicates that the current request is being made via AJAX, but is NOT for EE |
|
| 41 | - */ |
|
| 42 | - const AJAX_OTHER = 'other-ajax-request'; |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * indicates that the current request is for the EE REST API |
|
| 46 | - */ |
|
| 47 | - const API = 'rest-api'; |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * indicates that the current request is from the command line |
|
| 51 | - */ |
|
| 52 | - const CLI = 'command-line'; |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * indicates that the current request is for a WP_Cron |
|
| 56 | - */ |
|
| 57 | - const CRON = 'wp-cron'; |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * indicates that the current request is for a feed (ie: RSS) |
|
| 61 | - */ |
|
| 62 | - const FEED = 'feed-request'; |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * indicates that the current request is for the frontend but is not being made via AJAX |
|
| 66 | - */ |
|
| 67 | - const FRONTEND = 'non-ajax-frontend-request'; |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * indicates that the current request is for content that is to be displayed within an iframe |
|
| 71 | - */ |
|
| 72 | - const IFRAME = 'iframe-request'; |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * indicates that the current request is a loopback sent from WP core to test for errors |
|
| 76 | - */ |
|
| 77 | - const WP_SCRAPE = 'wordpress-scrape'; |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * @var boolean $is_activation |
|
| 81 | - */ |
|
| 82 | - private $is_activation = false; |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * @var array $valid_request_types |
|
| 86 | - */ |
|
| 87 | - private $valid_request_types = array(); |
|
| 88 | - |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * RequestTypeContext constructor. |
|
| 92 | - * |
|
| 93 | - * @param string $slug |
|
| 94 | - * @param string $description |
|
| 95 | - * @throws InvalidArgumentException |
|
| 96 | - */ |
|
| 97 | - public function __construct($slug, $description) |
|
| 98 | - { |
|
| 99 | - parent::__construct($slug, $description); |
|
| 100 | - if (! in_array($this->slug(), $this->validRequestTypes(), true)) { |
|
| 101 | - throw new InvalidArgumentException( |
|
| 102 | - sprintf( |
|
| 103 | - esc_html__( |
|
| 104 | - 'The RequestTypeContext slug must be one of the following values: %1$s %2$s', |
|
| 105 | - 'event_espresso' |
|
| 106 | - ), |
|
| 107 | - var_export($this->validRequestTypes(), true) |
|
| 108 | - ) |
|
| 109 | - ); |
|
| 110 | - } |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * @return array |
|
| 116 | - */ |
|
| 117 | - public function validRequestTypes() |
|
| 118 | - { |
|
| 119 | - if (empty($this->valid_request_types)) { |
|
| 120 | - $this->valid_request_types = apply_filters( |
|
| 121 | - 'FHEE__EventEspresso_core_domain_entities_contexts_RequestTypeContext__validRequestTypes', |
|
| 122 | - array( |
|
| 123 | - RequestTypeContext::ACTIVATION, |
|
| 124 | - RequestTypeContext::ADMIN, |
|
| 125 | - RequestTypeContext::AJAX_ADMIN, |
|
| 126 | - RequestTypeContext::AJAX_FRONT, |
|
| 127 | - RequestTypeContext::AJAX_OTHER, |
|
| 128 | - RequestTypeContext::API, |
|
| 129 | - RequestTypeContext::CLI, |
|
| 130 | - RequestTypeContext::CRON, |
|
| 131 | - RequestTypeContext::FEED, |
|
| 132 | - RequestTypeContext::FRONTEND, |
|
| 133 | - RequestTypeContext::IFRAME, |
|
| 134 | - RequestTypeContext::WP_SCRAPE, |
|
| 135 | - ) |
|
| 136 | - ); |
|
| 137 | - } |
|
| 138 | - return $this->valid_request_types; |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - |
|
| 142 | - /** |
|
| 143 | - * @return bool |
|
| 144 | - */ |
|
| 145 | - public function isActivation() |
|
| 146 | - { |
|
| 147 | - return $this->is_activation; |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - |
|
| 151 | - /** |
|
| 152 | - * @param bool $is_activation |
|
| 153 | - */ |
|
| 154 | - public function setIsActivation($is_activation) |
|
| 155 | - { |
|
| 156 | - $this->is_activation = filter_var($is_activation, FILTER_VALIDATE_BOOLEAN); |
|
| 157 | - } |
|
| 19 | + /** |
|
| 20 | + * indicates that the current request involves some form of activation |
|
| 21 | + */ |
|
| 22 | + const ACTIVATION = 'activation-request'; |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * indicates that the current request is for the admin but is not being made via AJAX |
|
| 26 | + */ |
|
| 27 | + const ADMIN = 'non-ajax-admin-request'; |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * indicates that the current request is for the admin AND is being made via AJAX |
|
| 31 | + */ |
|
| 32 | + const AJAX_ADMIN = 'admin-ajax-request'; |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * indicates that the current request is for the frontend AND is being made via AJAX |
|
| 36 | + */ |
|
| 37 | + const AJAX_FRONT = 'frontend-ajax-request'; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * indicates that the current request is being made via AJAX, but is NOT for EE |
|
| 41 | + */ |
|
| 42 | + const AJAX_OTHER = 'other-ajax-request'; |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * indicates that the current request is for the EE REST API |
|
| 46 | + */ |
|
| 47 | + const API = 'rest-api'; |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * indicates that the current request is from the command line |
|
| 51 | + */ |
|
| 52 | + const CLI = 'command-line'; |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * indicates that the current request is for a WP_Cron |
|
| 56 | + */ |
|
| 57 | + const CRON = 'wp-cron'; |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * indicates that the current request is for a feed (ie: RSS) |
|
| 61 | + */ |
|
| 62 | + const FEED = 'feed-request'; |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * indicates that the current request is for the frontend but is not being made via AJAX |
|
| 66 | + */ |
|
| 67 | + const FRONTEND = 'non-ajax-frontend-request'; |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * indicates that the current request is for content that is to be displayed within an iframe |
|
| 71 | + */ |
|
| 72 | + const IFRAME = 'iframe-request'; |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * indicates that the current request is a loopback sent from WP core to test for errors |
|
| 76 | + */ |
|
| 77 | + const WP_SCRAPE = 'wordpress-scrape'; |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * @var boolean $is_activation |
|
| 81 | + */ |
|
| 82 | + private $is_activation = false; |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * @var array $valid_request_types |
|
| 86 | + */ |
|
| 87 | + private $valid_request_types = array(); |
|
| 88 | + |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * RequestTypeContext constructor. |
|
| 92 | + * |
|
| 93 | + * @param string $slug |
|
| 94 | + * @param string $description |
|
| 95 | + * @throws InvalidArgumentException |
|
| 96 | + */ |
|
| 97 | + public function __construct($slug, $description) |
|
| 98 | + { |
|
| 99 | + parent::__construct($slug, $description); |
|
| 100 | + if (! in_array($this->slug(), $this->validRequestTypes(), true)) { |
|
| 101 | + throw new InvalidArgumentException( |
|
| 102 | + sprintf( |
|
| 103 | + esc_html__( |
|
| 104 | + 'The RequestTypeContext slug must be one of the following values: %1$s %2$s', |
|
| 105 | + 'event_espresso' |
|
| 106 | + ), |
|
| 107 | + var_export($this->validRequestTypes(), true) |
|
| 108 | + ) |
|
| 109 | + ); |
|
| 110 | + } |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * @return array |
|
| 116 | + */ |
|
| 117 | + public function validRequestTypes() |
|
| 118 | + { |
|
| 119 | + if (empty($this->valid_request_types)) { |
|
| 120 | + $this->valid_request_types = apply_filters( |
|
| 121 | + 'FHEE__EventEspresso_core_domain_entities_contexts_RequestTypeContext__validRequestTypes', |
|
| 122 | + array( |
|
| 123 | + RequestTypeContext::ACTIVATION, |
|
| 124 | + RequestTypeContext::ADMIN, |
|
| 125 | + RequestTypeContext::AJAX_ADMIN, |
|
| 126 | + RequestTypeContext::AJAX_FRONT, |
|
| 127 | + RequestTypeContext::AJAX_OTHER, |
|
| 128 | + RequestTypeContext::API, |
|
| 129 | + RequestTypeContext::CLI, |
|
| 130 | + RequestTypeContext::CRON, |
|
| 131 | + RequestTypeContext::FEED, |
|
| 132 | + RequestTypeContext::FRONTEND, |
|
| 133 | + RequestTypeContext::IFRAME, |
|
| 134 | + RequestTypeContext::WP_SCRAPE, |
|
| 135 | + ) |
|
| 136 | + ); |
|
| 137 | + } |
|
| 138 | + return $this->valid_request_types; |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + |
|
| 142 | + /** |
|
| 143 | + * @return bool |
|
| 144 | + */ |
|
| 145 | + public function isActivation() |
|
| 146 | + { |
|
| 147 | + return $this->is_activation; |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + |
|
| 151 | + /** |
|
| 152 | + * @param bool $is_activation |
|
| 153 | + */ |
|
| 154 | + public function setIsActivation($is_activation) |
|
| 155 | + { |
|
| 156 | + $this->is_activation = filter_var($is_activation, FILTER_VALIDATE_BOOLEAN); |
|
| 157 | + } |
|
| 158 | 158 | } |
@@ -17,21 +17,21 @@ |
||
| 17 | 17 | class DetectFileEditorRequest extends Middleware |
| 18 | 18 | { |
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * converts a Request to a Response |
|
| 22 | - * |
|
| 23 | - * @param RequestInterface $request |
|
| 24 | - * @param ResponseInterface $response |
|
| 25 | - * @return ResponseInterface |
|
| 26 | - */ |
|
| 27 | - public function handleRequest(RequestInterface $request, ResponseInterface $response) |
|
| 28 | - { |
|
| 29 | - $this->request = $request; |
|
| 30 | - $this->response = $response; |
|
| 31 | - if ($this->request->isWordPressScrape()) { |
|
| 32 | - add_filter('FHEE_load_EE_Session', '__return_false', 999); |
|
| 33 | - } |
|
| 34 | - $this->response = $this->processRequestStack($this->request, $this->response); |
|
| 35 | - return $this->response; |
|
| 36 | - } |
|
| 20 | + /** |
|
| 21 | + * converts a Request to a Response |
|
| 22 | + * |
|
| 23 | + * @param RequestInterface $request |
|
| 24 | + * @param ResponseInterface $response |
|
| 25 | + * @return ResponseInterface |
|
| 26 | + */ |
|
| 27 | + public function handleRequest(RequestInterface $request, ResponseInterface $response) |
|
| 28 | + { |
|
| 29 | + $this->request = $request; |
|
| 30 | + $this->response = $response; |
|
| 31 | + if ($this->request->isWordPressScrape()) { |
|
| 32 | + add_filter('FHEE_load_EE_Session', '__return_false', 999); |
|
| 33 | + } |
|
| 34 | + $this->response = $this->processRequestStack($this->request, $this->response); |
|
| 35 | + return $this->response; |
|
| 36 | + } |
|
| 37 | 37 | } |
@@ -17,586 +17,586 @@ |
||
| 17 | 17 | class Request implements InterminableInterface, RequestInterface, ReservedInstanceInterface |
| 18 | 18 | { |
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * $_GET parameters |
|
| 22 | - * |
|
| 23 | - * @var array $get |
|
| 24 | - */ |
|
| 25 | - private $get; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * $_POST parameters |
|
| 29 | - * |
|
| 30 | - * @var array $post |
|
| 31 | - */ |
|
| 32 | - private $post; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * $_COOKIE parameters |
|
| 36 | - * |
|
| 37 | - * @var array $cookie |
|
| 38 | - */ |
|
| 39 | - private $cookie; |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * $_SERVER parameters |
|
| 43 | - * |
|
| 44 | - * @var array $server |
|
| 45 | - */ |
|
| 46 | - private $server; |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * $_REQUEST parameters |
|
| 50 | - * |
|
| 51 | - * @var array $request |
|
| 52 | - */ |
|
| 53 | - private $request; |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * @var RequestTypeContextCheckerInterface |
|
| 57 | - */ |
|
| 58 | - private $request_type; |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * IP address for request |
|
| 62 | - * |
|
| 63 | - * @var string $ip_address |
|
| 64 | - */ |
|
| 65 | - private $ip_address; |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * @var string $user_agent |
|
| 69 | - */ |
|
| 70 | - private $user_agent; |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * true if current user appears to be some kind of bot |
|
| 74 | - * |
|
| 75 | - * @var bool $is_bot |
|
| 76 | - */ |
|
| 77 | - private $is_bot; |
|
| 78 | - |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * @param array $get |
|
| 82 | - * @param array $post |
|
| 83 | - * @param array $cookie |
|
| 84 | - * @param array $server |
|
| 85 | - */ |
|
| 86 | - public function __construct(array $get, array $post, array $cookie, array $server) |
|
| 87 | - { |
|
| 88 | - // grab request vars |
|
| 89 | - $this->get = $get; |
|
| 90 | - $this->post = $post; |
|
| 91 | - $this->cookie = $cookie; |
|
| 92 | - $this->server = $server; |
|
| 93 | - $this->request = array_merge($this->get, $this->post); |
|
| 94 | - $this->ip_address = $this->visitorIp(); |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * @param RequestTypeContextCheckerInterface $type |
|
| 100 | - */ |
|
| 101 | - public function setRequestTypeContextChecker(RequestTypeContextCheckerInterface $type) |
|
| 102 | - { |
|
| 103 | - $this->request_type = $type; |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * @return array |
|
| 109 | - */ |
|
| 110 | - public function getParams() |
|
| 111 | - { |
|
| 112 | - return $this->get; |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * @return array |
|
| 118 | - */ |
|
| 119 | - public function postParams() |
|
| 120 | - { |
|
| 121 | - return $this->post; |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * @return array |
|
| 127 | - */ |
|
| 128 | - public function cookieParams() |
|
| 129 | - { |
|
| 130 | - return $this->cookie; |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - |
|
| 134 | - /** |
|
| 135 | - * @return array |
|
| 136 | - */ |
|
| 137 | - public function serverParams() |
|
| 138 | - { |
|
| 139 | - return $this->server; |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - |
|
| 143 | - /** |
|
| 144 | - * returns contents of $_REQUEST |
|
| 145 | - * |
|
| 146 | - * @return array |
|
| 147 | - */ |
|
| 148 | - public function requestParams() |
|
| 149 | - { |
|
| 150 | - return $this->request; |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - |
|
| 154 | - /** |
|
| 155 | - * @param $key |
|
| 156 | - * @param $value |
|
| 157 | - * @param bool $override_ee |
|
| 158 | - * @return void |
|
| 159 | - */ |
|
| 160 | - public function setRequestParam($key, $value, $override_ee = false) |
|
| 161 | - { |
|
| 162 | - // don't allow "ee" to be overwritten unless explicitly instructed to do so |
|
| 163 | - if ($key !== 'ee' |
|
| 164 | - || ($key === 'ee' && empty($this->request['ee'])) |
|
| 165 | - || ($key === 'ee' && ! empty($this->request['ee']) && $override_ee) |
|
| 166 | - ) { |
|
| 167 | - $this->request[ $key ] = $value; |
|
| 168 | - } |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - |
|
| 172 | - /** |
|
| 173 | - * returns the value for a request param if the given key exists |
|
| 174 | - * |
|
| 175 | - * @param $key |
|
| 176 | - * @param null $default |
|
| 177 | - * @return mixed |
|
| 178 | - */ |
|
| 179 | - public function getRequestParam($key, $default = null) |
|
| 180 | - { |
|
| 181 | - return $this->requestParameterDrillDown($key, $default, 'get'); |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - |
|
| 185 | - /** |
|
| 186 | - * check if param exists |
|
| 187 | - * |
|
| 188 | - * @param $key |
|
| 189 | - * @return bool |
|
| 190 | - */ |
|
| 191 | - public function requestParamIsSet($key) |
|
| 192 | - { |
|
| 193 | - return $this->requestParameterDrillDown($key); |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - |
|
| 197 | - /** |
|
| 198 | - * check if a request parameter exists whose key that matches the supplied wildcard pattern |
|
| 199 | - * and return the value for the first match found |
|
| 200 | - * wildcards can be either of the following: |
|
| 201 | - * ? to represent a single character of any type |
|
| 202 | - * * to represent one or more characters of any type |
|
| 203 | - * |
|
| 204 | - * @param string $pattern |
|
| 205 | - * @param null|mixed $default |
|
| 206 | - * @return false|int |
|
| 207 | - */ |
|
| 208 | - public function getMatch($pattern, $default = null) |
|
| 209 | - { |
|
| 210 | - return $this->requestParameterDrillDown($pattern, $default, 'match'); |
|
| 211 | - } |
|
| 212 | - |
|
| 213 | - |
|
| 214 | - /** |
|
| 215 | - * check if a request parameter exists whose key matches the supplied wildcard pattern |
|
| 216 | - * wildcards can be either of the following: |
|
| 217 | - * ? to represent a single character of any type |
|
| 218 | - * * to represent one or more characters of any type |
|
| 219 | - * returns true if a match is found or false if not |
|
| 220 | - * |
|
| 221 | - * @param string $pattern |
|
| 222 | - * @return false|int |
|
| 223 | - */ |
|
| 224 | - public function matches($pattern) |
|
| 225 | - { |
|
| 226 | - return $this->requestParameterDrillDown($pattern, null, 'match') !== null; |
|
| 227 | - } |
|
| 228 | - |
|
| 229 | - |
|
| 230 | - /** |
|
| 231 | - * @see https://stackoverflow.com/questions/6163055/php-string-matching-with-wildcard |
|
| 232 | - * @param string $pattern A string including wildcards to be converted to a regex pattern |
|
| 233 | - * and used to search through the current request's parameter keys |
|
| 234 | - * @param array $request_params The array of request parameters to search through |
|
| 235 | - * @param mixed $default [optional] The value to be returned if no match is found. |
|
| 236 | - * Default is null |
|
| 237 | - * @param string $return [optional] Controls what kind of value is returned. |
|
| 238 | - * Options are: |
|
| 239 | - * 'bool' will return true or false if match is found or not |
|
| 240 | - * 'key' will return the first key found that matches the supplied pattern |
|
| 241 | - * 'value' will return the value for the first request parameter |
|
| 242 | - * whose key matches the supplied pattern |
|
| 243 | - * Default is 'value' |
|
| 244 | - * @return boolean|string |
|
| 245 | - */ |
|
| 246 | - private function match($pattern, array $request_params, $default = null, $return = 'value') |
|
| 247 | - { |
|
| 248 | - $return = in_array($return, array('bool', 'key', 'value'), true) |
|
| 249 | - ? $return |
|
| 250 | - : 'is_set'; |
|
| 251 | - // replace wildcard chars with regex chars |
|
| 252 | - $pattern = str_replace( |
|
| 253 | - array("\*", "\?"), |
|
| 254 | - array('.*', '.'), |
|
| 255 | - preg_quote($pattern, '/') |
|
| 256 | - ); |
|
| 257 | - foreach ($request_params as $key => $request_param) { |
|
| 258 | - if (preg_match('/^' . $pattern . '$/is', $key)) { |
|
| 259 | - // return value for request param |
|
| 260 | - if ($return === 'value') { |
|
| 261 | - return $request_params[ $key ]; |
|
| 262 | - } |
|
| 263 | - // or actual key or true just to indicate it was found |
|
| 264 | - return $return === 'key' ? $key : true; |
|
| 265 | - } |
|
| 266 | - } |
|
| 267 | - // match not found so return default value or false |
|
| 268 | - return $return === 'value' ? $default : false; |
|
| 269 | - } |
|
| 270 | - |
|
| 271 | - |
|
| 272 | - /** |
|
| 273 | - * the supplied key can be a simple string to represent a "top-level" request parameter |
|
| 274 | - * or represent a key for a request parameter that is nested deeper within the request parameter array, |
|
| 275 | - * by using square brackets to surround keys for deeper array elements. |
|
| 276 | - * For example : |
|
| 277 | - * if the supplied $key was: "first[second][third]" |
|
| 278 | - * then this will attempt to drill down into the request parameter array to find a value. |
|
| 279 | - * Given the following request parameters: |
|
| 280 | - * array( |
|
| 281 | - * 'first' => array( |
|
| 282 | - * 'second' => array( |
|
| 283 | - * 'third' => 'has a value' |
|
| 284 | - * ) |
|
| 285 | - * ) |
|
| 286 | - * ) |
|
| 287 | - * would return true if default parameters were set |
|
| 288 | - * |
|
| 289 | - * @param string $callback |
|
| 290 | - * @param $key |
|
| 291 | - * @param null $default |
|
| 292 | - * @param array $request_params |
|
| 293 | - * @return bool|mixed|null |
|
| 294 | - */ |
|
| 295 | - private function requestParameterDrillDown( |
|
| 296 | - $key, |
|
| 297 | - $default = null, |
|
| 298 | - $callback = 'is_set', |
|
| 299 | - array $request_params = array() |
|
| 300 | - ) { |
|
| 301 | - $callback = in_array($callback, array('is_set', 'get', 'match'), true) |
|
| 302 | - ? $callback |
|
| 303 | - : 'is_set'; |
|
| 304 | - $request_params = ! empty($request_params) |
|
| 305 | - ? $request_params |
|
| 306 | - : $this->request; |
|
| 307 | - // does incoming key represent an array like 'first[second][third]' ? |
|
| 308 | - if (strpos($key, '[') !== false) { |
|
| 309 | - // turn it into an actual array |
|
| 310 | - $key = str_replace(']', '', $key); |
|
| 311 | - $keys = explode('[', $key); |
|
| 312 | - $key = array_shift($keys); |
|
| 313 | - if ($callback === 'match') { |
|
| 314 | - $real_key = $this->match($key, $request_params, $default, 'key'); |
|
| 315 | - $key = $real_key ? $real_key : $key; |
|
| 316 | - } |
|
| 317 | - // check if top level key exists |
|
| 318 | - if (isset($request_params[ $key ])) { |
|
| 319 | - // build a new key to pass along like: 'second[third]' |
|
| 320 | - // or just 'second' depending on depth of keys |
|
| 321 | - $key_string = array_shift($keys); |
|
| 322 | - if (! empty($keys)) { |
|
| 323 | - $key_string .= '[' . implode('][', $keys) . ']'; |
|
| 324 | - } |
|
| 325 | - return $this->requestParameterDrillDown( |
|
| 326 | - $key_string, |
|
| 327 | - $default, |
|
| 328 | - $callback, |
|
| 329 | - $request_params[ $key ] |
|
| 330 | - ); |
|
| 331 | - } |
|
| 332 | - } |
|
| 333 | - if ($callback === 'is_set') { |
|
| 334 | - return isset($request_params[ $key ]); |
|
| 335 | - } |
|
| 336 | - if ($callback === 'match') { |
|
| 337 | - return $this->match($key, $request_params, $default); |
|
| 338 | - } |
|
| 339 | - return isset($request_params[ $key ]) |
|
| 340 | - ? $request_params[ $key ] |
|
| 341 | - : $default; |
|
| 342 | - } |
|
| 343 | - |
|
| 344 | - |
|
| 345 | - /** |
|
| 346 | - * remove param |
|
| 347 | - * |
|
| 348 | - * @param $key |
|
| 349 | - * @param bool $unset_from_global_too |
|
| 350 | - */ |
|
| 351 | - public function unSetRequestParam($key, $unset_from_global_too = false) |
|
| 352 | - { |
|
| 353 | - unset($this->request[ $key ]); |
|
| 354 | - if ($unset_from_global_too) { |
|
| 355 | - unset($_REQUEST[ $key ]); |
|
| 356 | - } |
|
| 357 | - } |
|
| 358 | - |
|
| 359 | - |
|
| 360 | - /** |
|
| 361 | - * @return string |
|
| 362 | - */ |
|
| 363 | - public function ipAddress() |
|
| 364 | - { |
|
| 365 | - return $this->ip_address; |
|
| 366 | - } |
|
| 367 | - |
|
| 368 | - |
|
| 369 | - /** |
|
| 370 | - * attempt to get IP address of current visitor from server |
|
| 371 | - * plz see: http://stackoverflow.com/a/2031935/1475279 |
|
| 372 | - * |
|
| 373 | - * @access public |
|
| 374 | - * @return string |
|
| 375 | - */ |
|
| 376 | - private function visitorIp() |
|
| 377 | - { |
|
| 378 | - $visitor_ip = '0.0.0.0'; |
|
| 379 | - $server_keys = array( |
|
| 380 | - 'HTTP_CLIENT_IP', |
|
| 381 | - 'HTTP_X_FORWARDED_FOR', |
|
| 382 | - 'HTTP_X_FORWARDED', |
|
| 383 | - 'HTTP_X_CLUSTER_CLIENT_IP', |
|
| 384 | - 'HTTP_FORWARDED_FOR', |
|
| 385 | - 'HTTP_FORWARDED', |
|
| 386 | - 'REMOTE_ADDR', |
|
| 387 | - ); |
|
| 388 | - foreach ($server_keys as $key) { |
|
| 389 | - if (isset($this->server[ $key ])) { |
|
| 390 | - foreach (array_map('trim', explode(',', $this->server[ $key ])) as $ip) { |
|
| 391 | - if ($ip === '127.0.0.1' || filter_var($ip, FILTER_VALIDATE_IP) !== false) { |
|
| 392 | - $visitor_ip = $ip; |
|
| 393 | - } |
|
| 394 | - } |
|
| 395 | - } |
|
| 396 | - } |
|
| 397 | - return $visitor_ip; |
|
| 398 | - } |
|
| 399 | - |
|
| 400 | - |
|
| 401 | - /** |
|
| 402 | - * @return string |
|
| 403 | - */ |
|
| 404 | - public function requestUri() |
|
| 405 | - { |
|
| 406 | - $request_uri = filter_input( |
|
| 407 | - INPUT_SERVER, |
|
| 408 | - 'REQUEST_URI', |
|
| 409 | - FILTER_SANITIZE_URL, |
|
| 410 | - FILTER_NULL_ON_FAILURE |
|
| 411 | - ); |
|
| 412 | - if (empty($request_uri)) { |
|
| 413 | - // fallback sanitization if the above fails |
|
| 414 | - $request_uri = wp_sanitize_redirect($this->server['REQUEST_URI']); |
|
| 415 | - } |
|
| 416 | - return $request_uri; |
|
| 417 | - } |
|
| 418 | - |
|
| 419 | - |
|
| 420 | - /** |
|
| 421 | - * @return string |
|
| 422 | - */ |
|
| 423 | - public function userAgent() |
|
| 424 | - { |
|
| 425 | - return $this->user_agent; |
|
| 426 | - } |
|
| 427 | - |
|
| 428 | - |
|
| 429 | - /** |
|
| 430 | - * @param string $user_agent |
|
| 431 | - */ |
|
| 432 | - public function setUserAgent($user_agent = '') |
|
| 433 | - { |
|
| 434 | - if ($user_agent === '' || ! is_string($user_agent)) { |
|
| 435 | - $user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? (string) esc_attr($_SERVER['HTTP_USER_AGENT']) : ''; |
|
| 436 | - } |
|
| 437 | - $this->user_agent = $user_agent; |
|
| 438 | - } |
|
| 439 | - |
|
| 440 | - |
|
| 441 | - /** |
|
| 442 | - * @return bool |
|
| 443 | - */ |
|
| 444 | - public function isBot() |
|
| 445 | - { |
|
| 446 | - return $this->is_bot; |
|
| 447 | - } |
|
| 448 | - |
|
| 449 | - |
|
| 450 | - /** |
|
| 451 | - * @param bool $is_bot |
|
| 452 | - */ |
|
| 453 | - public function setIsBot($is_bot) |
|
| 454 | - { |
|
| 455 | - $this->is_bot = filter_var($is_bot, FILTER_VALIDATE_BOOLEAN); |
|
| 456 | - } |
|
| 457 | - |
|
| 458 | - |
|
| 459 | - /** |
|
| 460 | - * @return bool |
|
| 461 | - */ |
|
| 462 | - public function isActivation() |
|
| 463 | - { |
|
| 464 | - return $this->request_type->isActivation(); |
|
| 465 | - } |
|
| 466 | - |
|
| 467 | - |
|
| 468 | - /** |
|
| 469 | - * @param $is_activation |
|
| 470 | - * @return bool |
|
| 471 | - */ |
|
| 472 | - public function setIsActivation($is_activation) |
|
| 473 | - { |
|
| 474 | - return $this->request_type->setIsActivation($is_activation); |
|
| 475 | - } |
|
| 476 | - |
|
| 477 | - |
|
| 478 | - /** |
|
| 479 | - * @return bool |
|
| 480 | - */ |
|
| 481 | - public function isAdmin() |
|
| 482 | - { |
|
| 483 | - return $this->request_type->isAdmin(); |
|
| 484 | - } |
|
| 485 | - |
|
| 486 | - |
|
| 487 | - /** |
|
| 488 | - * @return bool |
|
| 489 | - */ |
|
| 490 | - public function isAdminAjax() |
|
| 491 | - { |
|
| 492 | - return $this->request_type->isAdminAjax(); |
|
| 493 | - } |
|
| 494 | - |
|
| 495 | - |
|
| 496 | - /** |
|
| 497 | - * @return bool |
|
| 498 | - */ |
|
| 499 | - public function isAjax() |
|
| 500 | - { |
|
| 501 | - return $this->request_type->isAjax(); |
|
| 502 | - } |
|
| 503 | - |
|
| 504 | - |
|
| 505 | - /** |
|
| 506 | - * @return bool |
|
| 507 | - */ |
|
| 508 | - public function isEeAjax() |
|
| 509 | - { |
|
| 510 | - return $this->request_type->isEeAjax(); |
|
| 511 | - } |
|
| 512 | - |
|
| 513 | - |
|
| 514 | - /** |
|
| 515 | - * @return bool |
|
| 516 | - */ |
|
| 517 | - public function isOtherAjax() |
|
| 518 | - { |
|
| 519 | - return $this->request_type->isOtherAjax(); |
|
| 520 | - } |
|
| 521 | - |
|
| 522 | - |
|
| 523 | - /** |
|
| 524 | - * @return bool |
|
| 525 | - */ |
|
| 526 | - public function isApi() |
|
| 527 | - { |
|
| 528 | - return $this->request_type->isApi(); |
|
| 529 | - } |
|
| 530 | - |
|
| 531 | - |
|
| 532 | - /** |
|
| 533 | - * @return bool |
|
| 534 | - */ |
|
| 535 | - public function isCli() |
|
| 536 | - { |
|
| 537 | - return $this->request_type->isCli(); |
|
| 538 | - } |
|
| 539 | - |
|
| 540 | - |
|
| 541 | - /** |
|
| 542 | - * @return bool |
|
| 543 | - */ |
|
| 544 | - public function isCron() |
|
| 545 | - { |
|
| 546 | - return $this->request_type->isCron(); |
|
| 547 | - } |
|
| 548 | - |
|
| 549 | - |
|
| 550 | - /** |
|
| 551 | - * @return bool |
|
| 552 | - */ |
|
| 553 | - public function isFeed() |
|
| 554 | - { |
|
| 555 | - return $this->request_type->isFeed(); |
|
| 556 | - } |
|
| 557 | - |
|
| 558 | - |
|
| 559 | - /** |
|
| 560 | - * @return bool |
|
| 561 | - */ |
|
| 562 | - public function isFrontend() |
|
| 563 | - { |
|
| 564 | - return $this->request_type->isFrontend(); |
|
| 565 | - } |
|
| 566 | - |
|
| 567 | - |
|
| 568 | - /** |
|
| 569 | - * @return bool |
|
| 570 | - */ |
|
| 571 | - public function isFrontAjax() |
|
| 572 | - { |
|
| 573 | - return $this->request_type->isFrontAjax(); |
|
| 574 | - } |
|
| 575 | - |
|
| 576 | - |
|
| 577 | - /** |
|
| 578 | - * @return bool |
|
| 579 | - */ |
|
| 580 | - public function isIframe() |
|
| 581 | - { |
|
| 582 | - return $this->request_type->isIframe(); |
|
| 583 | - } |
|
| 584 | - |
|
| 585 | - |
|
| 586 | - /** |
|
| 587 | - * @return bool |
|
| 588 | - */ |
|
| 589 | - public function isWordPressScrape() |
|
| 590 | - { |
|
| 591 | - return $this->request_type->isWordPressScrape(); |
|
| 592 | - } |
|
| 593 | - |
|
| 594 | - |
|
| 595 | - /** |
|
| 596 | - * @return string |
|
| 597 | - */ |
|
| 598 | - public function slug() |
|
| 599 | - { |
|
| 600 | - return $this->request_type->slug(); |
|
| 601 | - } |
|
| 20 | + /** |
|
| 21 | + * $_GET parameters |
|
| 22 | + * |
|
| 23 | + * @var array $get |
|
| 24 | + */ |
|
| 25 | + private $get; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * $_POST parameters |
|
| 29 | + * |
|
| 30 | + * @var array $post |
|
| 31 | + */ |
|
| 32 | + private $post; |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * $_COOKIE parameters |
|
| 36 | + * |
|
| 37 | + * @var array $cookie |
|
| 38 | + */ |
|
| 39 | + private $cookie; |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * $_SERVER parameters |
|
| 43 | + * |
|
| 44 | + * @var array $server |
|
| 45 | + */ |
|
| 46 | + private $server; |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * $_REQUEST parameters |
|
| 50 | + * |
|
| 51 | + * @var array $request |
|
| 52 | + */ |
|
| 53 | + private $request; |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * @var RequestTypeContextCheckerInterface |
|
| 57 | + */ |
|
| 58 | + private $request_type; |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * IP address for request |
|
| 62 | + * |
|
| 63 | + * @var string $ip_address |
|
| 64 | + */ |
|
| 65 | + private $ip_address; |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * @var string $user_agent |
|
| 69 | + */ |
|
| 70 | + private $user_agent; |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * true if current user appears to be some kind of bot |
|
| 74 | + * |
|
| 75 | + * @var bool $is_bot |
|
| 76 | + */ |
|
| 77 | + private $is_bot; |
|
| 78 | + |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * @param array $get |
|
| 82 | + * @param array $post |
|
| 83 | + * @param array $cookie |
|
| 84 | + * @param array $server |
|
| 85 | + */ |
|
| 86 | + public function __construct(array $get, array $post, array $cookie, array $server) |
|
| 87 | + { |
|
| 88 | + // grab request vars |
|
| 89 | + $this->get = $get; |
|
| 90 | + $this->post = $post; |
|
| 91 | + $this->cookie = $cookie; |
|
| 92 | + $this->server = $server; |
|
| 93 | + $this->request = array_merge($this->get, $this->post); |
|
| 94 | + $this->ip_address = $this->visitorIp(); |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * @param RequestTypeContextCheckerInterface $type |
|
| 100 | + */ |
|
| 101 | + public function setRequestTypeContextChecker(RequestTypeContextCheckerInterface $type) |
|
| 102 | + { |
|
| 103 | + $this->request_type = $type; |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * @return array |
|
| 109 | + */ |
|
| 110 | + public function getParams() |
|
| 111 | + { |
|
| 112 | + return $this->get; |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + |
|
| 116 | + /** |
|
| 117 | + * @return array |
|
| 118 | + */ |
|
| 119 | + public function postParams() |
|
| 120 | + { |
|
| 121 | + return $this->post; |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * @return array |
|
| 127 | + */ |
|
| 128 | + public function cookieParams() |
|
| 129 | + { |
|
| 130 | + return $this->cookie; |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + |
|
| 134 | + /** |
|
| 135 | + * @return array |
|
| 136 | + */ |
|
| 137 | + public function serverParams() |
|
| 138 | + { |
|
| 139 | + return $this->server; |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + |
|
| 143 | + /** |
|
| 144 | + * returns contents of $_REQUEST |
|
| 145 | + * |
|
| 146 | + * @return array |
|
| 147 | + */ |
|
| 148 | + public function requestParams() |
|
| 149 | + { |
|
| 150 | + return $this->request; |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + |
|
| 154 | + /** |
|
| 155 | + * @param $key |
|
| 156 | + * @param $value |
|
| 157 | + * @param bool $override_ee |
|
| 158 | + * @return void |
|
| 159 | + */ |
|
| 160 | + public function setRequestParam($key, $value, $override_ee = false) |
|
| 161 | + { |
|
| 162 | + // don't allow "ee" to be overwritten unless explicitly instructed to do so |
|
| 163 | + if ($key !== 'ee' |
|
| 164 | + || ($key === 'ee' && empty($this->request['ee'])) |
|
| 165 | + || ($key === 'ee' && ! empty($this->request['ee']) && $override_ee) |
|
| 166 | + ) { |
|
| 167 | + $this->request[ $key ] = $value; |
|
| 168 | + } |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + |
|
| 172 | + /** |
|
| 173 | + * returns the value for a request param if the given key exists |
|
| 174 | + * |
|
| 175 | + * @param $key |
|
| 176 | + * @param null $default |
|
| 177 | + * @return mixed |
|
| 178 | + */ |
|
| 179 | + public function getRequestParam($key, $default = null) |
|
| 180 | + { |
|
| 181 | + return $this->requestParameterDrillDown($key, $default, 'get'); |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + |
|
| 185 | + /** |
|
| 186 | + * check if param exists |
|
| 187 | + * |
|
| 188 | + * @param $key |
|
| 189 | + * @return bool |
|
| 190 | + */ |
|
| 191 | + public function requestParamIsSet($key) |
|
| 192 | + { |
|
| 193 | + return $this->requestParameterDrillDown($key); |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + |
|
| 197 | + /** |
|
| 198 | + * check if a request parameter exists whose key that matches the supplied wildcard pattern |
|
| 199 | + * and return the value for the first match found |
|
| 200 | + * wildcards can be either of the following: |
|
| 201 | + * ? to represent a single character of any type |
|
| 202 | + * * to represent one or more characters of any type |
|
| 203 | + * |
|
| 204 | + * @param string $pattern |
|
| 205 | + * @param null|mixed $default |
|
| 206 | + * @return false|int |
|
| 207 | + */ |
|
| 208 | + public function getMatch($pattern, $default = null) |
|
| 209 | + { |
|
| 210 | + return $this->requestParameterDrillDown($pattern, $default, 'match'); |
|
| 211 | + } |
|
| 212 | + |
|
| 213 | + |
|
| 214 | + /** |
|
| 215 | + * check if a request parameter exists whose key matches the supplied wildcard pattern |
|
| 216 | + * wildcards can be either of the following: |
|
| 217 | + * ? to represent a single character of any type |
|
| 218 | + * * to represent one or more characters of any type |
|
| 219 | + * returns true if a match is found or false if not |
|
| 220 | + * |
|
| 221 | + * @param string $pattern |
|
| 222 | + * @return false|int |
|
| 223 | + */ |
|
| 224 | + public function matches($pattern) |
|
| 225 | + { |
|
| 226 | + return $this->requestParameterDrillDown($pattern, null, 'match') !== null; |
|
| 227 | + } |
|
| 228 | + |
|
| 229 | + |
|
| 230 | + /** |
|
| 231 | + * @see https://stackoverflow.com/questions/6163055/php-string-matching-with-wildcard |
|
| 232 | + * @param string $pattern A string including wildcards to be converted to a regex pattern |
|
| 233 | + * and used to search through the current request's parameter keys |
|
| 234 | + * @param array $request_params The array of request parameters to search through |
|
| 235 | + * @param mixed $default [optional] The value to be returned if no match is found. |
|
| 236 | + * Default is null |
|
| 237 | + * @param string $return [optional] Controls what kind of value is returned. |
|
| 238 | + * Options are: |
|
| 239 | + * 'bool' will return true or false if match is found or not |
|
| 240 | + * 'key' will return the first key found that matches the supplied pattern |
|
| 241 | + * 'value' will return the value for the first request parameter |
|
| 242 | + * whose key matches the supplied pattern |
|
| 243 | + * Default is 'value' |
|
| 244 | + * @return boolean|string |
|
| 245 | + */ |
|
| 246 | + private function match($pattern, array $request_params, $default = null, $return = 'value') |
|
| 247 | + { |
|
| 248 | + $return = in_array($return, array('bool', 'key', 'value'), true) |
|
| 249 | + ? $return |
|
| 250 | + : 'is_set'; |
|
| 251 | + // replace wildcard chars with regex chars |
|
| 252 | + $pattern = str_replace( |
|
| 253 | + array("\*", "\?"), |
|
| 254 | + array('.*', '.'), |
|
| 255 | + preg_quote($pattern, '/') |
|
| 256 | + ); |
|
| 257 | + foreach ($request_params as $key => $request_param) { |
|
| 258 | + if (preg_match('/^' . $pattern . '$/is', $key)) { |
|
| 259 | + // return value for request param |
|
| 260 | + if ($return === 'value') { |
|
| 261 | + return $request_params[ $key ]; |
|
| 262 | + } |
|
| 263 | + // or actual key or true just to indicate it was found |
|
| 264 | + return $return === 'key' ? $key : true; |
|
| 265 | + } |
|
| 266 | + } |
|
| 267 | + // match not found so return default value or false |
|
| 268 | + return $return === 'value' ? $default : false; |
|
| 269 | + } |
|
| 270 | + |
|
| 271 | + |
|
| 272 | + /** |
|
| 273 | + * the supplied key can be a simple string to represent a "top-level" request parameter |
|
| 274 | + * or represent a key for a request parameter that is nested deeper within the request parameter array, |
|
| 275 | + * by using square brackets to surround keys for deeper array elements. |
|
| 276 | + * For example : |
|
| 277 | + * if the supplied $key was: "first[second][third]" |
|
| 278 | + * then this will attempt to drill down into the request parameter array to find a value. |
|
| 279 | + * Given the following request parameters: |
|
| 280 | + * array( |
|
| 281 | + * 'first' => array( |
|
| 282 | + * 'second' => array( |
|
| 283 | + * 'third' => 'has a value' |
|
| 284 | + * ) |
|
| 285 | + * ) |
|
| 286 | + * ) |
|
| 287 | + * would return true if default parameters were set |
|
| 288 | + * |
|
| 289 | + * @param string $callback |
|
| 290 | + * @param $key |
|
| 291 | + * @param null $default |
|
| 292 | + * @param array $request_params |
|
| 293 | + * @return bool|mixed|null |
|
| 294 | + */ |
|
| 295 | + private function requestParameterDrillDown( |
|
| 296 | + $key, |
|
| 297 | + $default = null, |
|
| 298 | + $callback = 'is_set', |
|
| 299 | + array $request_params = array() |
|
| 300 | + ) { |
|
| 301 | + $callback = in_array($callback, array('is_set', 'get', 'match'), true) |
|
| 302 | + ? $callback |
|
| 303 | + : 'is_set'; |
|
| 304 | + $request_params = ! empty($request_params) |
|
| 305 | + ? $request_params |
|
| 306 | + : $this->request; |
|
| 307 | + // does incoming key represent an array like 'first[second][third]' ? |
|
| 308 | + if (strpos($key, '[') !== false) { |
|
| 309 | + // turn it into an actual array |
|
| 310 | + $key = str_replace(']', '', $key); |
|
| 311 | + $keys = explode('[', $key); |
|
| 312 | + $key = array_shift($keys); |
|
| 313 | + if ($callback === 'match') { |
|
| 314 | + $real_key = $this->match($key, $request_params, $default, 'key'); |
|
| 315 | + $key = $real_key ? $real_key : $key; |
|
| 316 | + } |
|
| 317 | + // check if top level key exists |
|
| 318 | + if (isset($request_params[ $key ])) { |
|
| 319 | + // build a new key to pass along like: 'second[third]' |
|
| 320 | + // or just 'second' depending on depth of keys |
|
| 321 | + $key_string = array_shift($keys); |
|
| 322 | + if (! empty($keys)) { |
|
| 323 | + $key_string .= '[' . implode('][', $keys) . ']'; |
|
| 324 | + } |
|
| 325 | + return $this->requestParameterDrillDown( |
|
| 326 | + $key_string, |
|
| 327 | + $default, |
|
| 328 | + $callback, |
|
| 329 | + $request_params[ $key ] |
|
| 330 | + ); |
|
| 331 | + } |
|
| 332 | + } |
|
| 333 | + if ($callback === 'is_set') { |
|
| 334 | + return isset($request_params[ $key ]); |
|
| 335 | + } |
|
| 336 | + if ($callback === 'match') { |
|
| 337 | + return $this->match($key, $request_params, $default); |
|
| 338 | + } |
|
| 339 | + return isset($request_params[ $key ]) |
|
| 340 | + ? $request_params[ $key ] |
|
| 341 | + : $default; |
|
| 342 | + } |
|
| 343 | + |
|
| 344 | + |
|
| 345 | + /** |
|
| 346 | + * remove param |
|
| 347 | + * |
|
| 348 | + * @param $key |
|
| 349 | + * @param bool $unset_from_global_too |
|
| 350 | + */ |
|
| 351 | + public function unSetRequestParam($key, $unset_from_global_too = false) |
|
| 352 | + { |
|
| 353 | + unset($this->request[ $key ]); |
|
| 354 | + if ($unset_from_global_too) { |
|
| 355 | + unset($_REQUEST[ $key ]); |
|
| 356 | + } |
|
| 357 | + } |
|
| 358 | + |
|
| 359 | + |
|
| 360 | + /** |
|
| 361 | + * @return string |
|
| 362 | + */ |
|
| 363 | + public function ipAddress() |
|
| 364 | + { |
|
| 365 | + return $this->ip_address; |
|
| 366 | + } |
|
| 367 | + |
|
| 368 | + |
|
| 369 | + /** |
|
| 370 | + * attempt to get IP address of current visitor from server |
|
| 371 | + * plz see: http://stackoverflow.com/a/2031935/1475279 |
|
| 372 | + * |
|
| 373 | + * @access public |
|
| 374 | + * @return string |
|
| 375 | + */ |
|
| 376 | + private function visitorIp() |
|
| 377 | + { |
|
| 378 | + $visitor_ip = '0.0.0.0'; |
|
| 379 | + $server_keys = array( |
|
| 380 | + 'HTTP_CLIENT_IP', |
|
| 381 | + 'HTTP_X_FORWARDED_FOR', |
|
| 382 | + 'HTTP_X_FORWARDED', |
|
| 383 | + 'HTTP_X_CLUSTER_CLIENT_IP', |
|
| 384 | + 'HTTP_FORWARDED_FOR', |
|
| 385 | + 'HTTP_FORWARDED', |
|
| 386 | + 'REMOTE_ADDR', |
|
| 387 | + ); |
|
| 388 | + foreach ($server_keys as $key) { |
|
| 389 | + if (isset($this->server[ $key ])) { |
|
| 390 | + foreach (array_map('trim', explode(',', $this->server[ $key ])) as $ip) { |
|
| 391 | + if ($ip === '127.0.0.1' || filter_var($ip, FILTER_VALIDATE_IP) !== false) { |
|
| 392 | + $visitor_ip = $ip; |
|
| 393 | + } |
|
| 394 | + } |
|
| 395 | + } |
|
| 396 | + } |
|
| 397 | + return $visitor_ip; |
|
| 398 | + } |
|
| 399 | + |
|
| 400 | + |
|
| 401 | + /** |
|
| 402 | + * @return string |
|
| 403 | + */ |
|
| 404 | + public function requestUri() |
|
| 405 | + { |
|
| 406 | + $request_uri = filter_input( |
|
| 407 | + INPUT_SERVER, |
|
| 408 | + 'REQUEST_URI', |
|
| 409 | + FILTER_SANITIZE_URL, |
|
| 410 | + FILTER_NULL_ON_FAILURE |
|
| 411 | + ); |
|
| 412 | + if (empty($request_uri)) { |
|
| 413 | + // fallback sanitization if the above fails |
|
| 414 | + $request_uri = wp_sanitize_redirect($this->server['REQUEST_URI']); |
|
| 415 | + } |
|
| 416 | + return $request_uri; |
|
| 417 | + } |
|
| 418 | + |
|
| 419 | + |
|
| 420 | + /** |
|
| 421 | + * @return string |
|
| 422 | + */ |
|
| 423 | + public function userAgent() |
|
| 424 | + { |
|
| 425 | + return $this->user_agent; |
|
| 426 | + } |
|
| 427 | + |
|
| 428 | + |
|
| 429 | + /** |
|
| 430 | + * @param string $user_agent |
|
| 431 | + */ |
|
| 432 | + public function setUserAgent($user_agent = '') |
|
| 433 | + { |
|
| 434 | + if ($user_agent === '' || ! is_string($user_agent)) { |
|
| 435 | + $user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? (string) esc_attr($_SERVER['HTTP_USER_AGENT']) : ''; |
|
| 436 | + } |
|
| 437 | + $this->user_agent = $user_agent; |
|
| 438 | + } |
|
| 439 | + |
|
| 440 | + |
|
| 441 | + /** |
|
| 442 | + * @return bool |
|
| 443 | + */ |
|
| 444 | + public function isBot() |
|
| 445 | + { |
|
| 446 | + return $this->is_bot; |
|
| 447 | + } |
|
| 448 | + |
|
| 449 | + |
|
| 450 | + /** |
|
| 451 | + * @param bool $is_bot |
|
| 452 | + */ |
|
| 453 | + public function setIsBot($is_bot) |
|
| 454 | + { |
|
| 455 | + $this->is_bot = filter_var($is_bot, FILTER_VALIDATE_BOOLEAN); |
|
| 456 | + } |
|
| 457 | + |
|
| 458 | + |
|
| 459 | + /** |
|
| 460 | + * @return bool |
|
| 461 | + */ |
|
| 462 | + public function isActivation() |
|
| 463 | + { |
|
| 464 | + return $this->request_type->isActivation(); |
|
| 465 | + } |
|
| 466 | + |
|
| 467 | + |
|
| 468 | + /** |
|
| 469 | + * @param $is_activation |
|
| 470 | + * @return bool |
|
| 471 | + */ |
|
| 472 | + public function setIsActivation($is_activation) |
|
| 473 | + { |
|
| 474 | + return $this->request_type->setIsActivation($is_activation); |
|
| 475 | + } |
|
| 476 | + |
|
| 477 | + |
|
| 478 | + /** |
|
| 479 | + * @return bool |
|
| 480 | + */ |
|
| 481 | + public function isAdmin() |
|
| 482 | + { |
|
| 483 | + return $this->request_type->isAdmin(); |
|
| 484 | + } |
|
| 485 | + |
|
| 486 | + |
|
| 487 | + /** |
|
| 488 | + * @return bool |
|
| 489 | + */ |
|
| 490 | + public function isAdminAjax() |
|
| 491 | + { |
|
| 492 | + return $this->request_type->isAdminAjax(); |
|
| 493 | + } |
|
| 494 | + |
|
| 495 | + |
|
| 496 | + /** |
|
| 497 | + * @return bool |
|
| 498 | + */ |
|
| 499 | + public function isAjax() |
|
| 500 | + { |
|
| 501 | + return $this->request_type->isAjax(); |
|
| 502 | + } |
|
| 503 | + |
|
| 504 | + |
|
| 505 | + /** |
|
| 506 | + * @return bool |
|
| 507 | + */ |
|
| 508 | + public function isEeAjax() |
|
| 509 | + { |
|
| 510 | + return $this->request_type->isEeAjax(); |
|
| 511 | + } |
|
| 512 | + |
|
| 513 | + |
|
| 514 | + /** |
|
| 515 | + * @return bool |
|
| 516 | + */ |
|
| 517 | + public function isOtherAjax() |
|
| 518 | + { |
|
| 519 | + return $this->request_type->isOtherAjax(); |
|
| 520 | + } |
|
| 521 | + |
|
| 522 | + |
|
| 523 | + /** |
|
| 524 | + * @return bool |
|
| 525 | + */ |
|
| 526 | + public function isApi() |
|
| 527 | + { |
|
| 528 | + return $this->request_type->isApi(); |
|
| 529 | + } |
|
| 530 | + |
|
| 531 | + |
|
| 532 | + /** |
|
| 533 | + * @return bool |
|
| 534 | + */ |
|
| 535 | + public function isCli() |
|
| 536 | + { |
|
| 537 | + return $this->request_type->isCli(); |
|
| 538 | + } |
|
| 539 | + |
|
| 540 | + |
|
| 541 | + /** |
|
| 542 | + * @return bool |
|
| 543 | + */ |
|
| 544 | + public function isCron() |
|
| 545 | + { |
|
| 546 | + return $this->request_type->isCron(); |
|
| 547 | + } |
|
| 548 | + |
|
| 549 | + |
|
| 550 | + /** |
|
| 551 | + * @return bool |
|
| 552 | + */ |
|
| 553 | + public function isFeed() |
|
| 554 | + { |
|
| 555 | + return $this->request_type->isFeed(); |
|
| 556 | + } |
|
| 557 | + |
|
| 558 | + |
|
| 559 | + /** |
|
| 560 | + * @return bool |
|
| 561 | + */ |
|
| 562 | + public function isFrontend() |
|
| 563 | + { |
|
| 564 | + return $this->request_type->isFrontend(); |
|
| 565 | + } |
|
| 566 | + |
|
| 567 | + |
|
| 568 | + /** |
|
| 569 | + * @return bool |
|
| 570 | + */ |
|
| 571 | + public function isFrontAjax() |
|
| 572 | + { |
|
| 573 | + return $this->request_type->isFrontAjax(); |
|
| 574 | + } |
|
| 575 | + |
|
| 576 | + |
|
| 577 | + /** |
|
| 578 | + * @return bool |
|
| 579 | + */ |
|
| 580 | + public function isIframe() |
|
| 581 | + { |
|
| 582 | + return $this->request_type->isIframe(); |
|
| 583 | + } |
|
| 584 | + |
|
| 585 | + |
|
| 586 | + /** |
|
| 587 | + * @return bool |
|
| 588 | + */ |
|
| 589 | + public function isWordPressScrape() |
|
| 590 | + { |
|
| 591 | + return $this->request_type->isWordPressScrape(); |
|
| 592 | + } |
|
| 593 | + |
|
| 594 | + |
|
| 595 | + /** |
|
| 596 | + * @return string |
|
| 597 | + */ |
|
| 598 | + public function slug() |
|
| 599 | + { |
|
| 600 | + return $this->request_type->slug(); |
|
| 601 | + } |
|
| 602 | 602 | } |