$position, 'selector' => $options['checkout_selector']]); } /** Pay for order: always right above its pay button. */ public static function print_for_woo_pay(): void { if (Selfcap_Plugin::protects('woo_checkout')) { // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped echo Selfcap_Plugin::widget(['position' => 'button']); } } /** Requests that never show the form (XML-RPC, REST, application passwords) are not checked. */ private static function is_form_post(): bool { if ((defined('XMLRPC_REQUEST') && XMLRPC_REQUEST) || (defined('REST_REQUEST') && REST_REQUEST)) { return false; } return isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'POST'; } /** * wp-login.php. The entry point decides, never a field the client sends, so no * request field can switch the check off. Logins started by other plugins' * own forms (no widget there) pass through. */ public static function check_login($user, $username, $password) { if (! self::is_form_post() || $username === '' || $username === null || ! Selfcap_Plugin::protects('login')) { return $user; } // login_init fires only on wp-login.php, also when a "custom login URL" // plugin serves it from another address; a client cannot trigger it. if (! did_action('login_init') && ($GLOBALS['pagenow'] ?? '') !== 'wp-login.php') { return $user; } $result = Selfcap_Plugin::verify(); return is_wp_error($result) ? $result : $user; } /** WooCommerce's own login check, run before it signs the user in, whatever nonce field it got. */ public static function check_woo_login($errors) { if (! Selfcap_Plugin::protects('login')) { return $errors; } $result = Selfcap_Plugin::verify(); if (is_wp_error($result) && is_wp_error($errors)) { $errors->add($result->get_error_code(), $result->get_error_message()); } return $errors; } public static function login_failed(): void { if (Selfcap_Plugin::is_enabled()) { Selfcap_Plugin::engine()->record('login_fail', Selfcap_Plugin::client_ip()); } } public static function print_for_signup($errors): void { if (Selfcap_Plugin::protects('register') && ! is_user_logged_in()) { if (is_wp_error($errors) && $errors->get_error_message('selfcap')) { echo '
'.esc_html($errors->get_error_message('selfcap')).'
'; } echo Selfcap_Plugin::widget(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } } /** Runs for visitors signing up on wp-signup.php; network admins adding users are left alone. */ public static function check_signup($result) { if (! Selfcap_Plugin::protects('register') || is_admin() || is_user_logged_in() || ! self::is_form_post() || ! isset($result['errors']) || ! is_wp_error($result['errors'])) { return $result; } $verified = Selfcap_Plugin::verify(); if (is_wp_error($verified)) { $result['errors']->add('selfcap', $verified->get_error_message()); } return $result; } public static function check_woo_pay($order): void { if (! Selfcap_Plugin::protects('woo_checkout')) { return; } $result = Selfcap_Plugin::verify(); // An error notice stops WooCommerce before it calls the payment gateway. if (is_wp_error($result) && function_exists('wc_add_notice')) { wc_add_notice($result->get_error_message(), 'error'); } } public static function xmlrpc_enabled($enabled) { return Selfcap_Plugin::protects('login') && ! empty(Selfcap_Plugin::options()['block_xmlrpc']) ? false : $enabled; } public static function check_register($errors) { if (Selfcap_Plugin::protects('register')) { $result = Selfcap_Plugin::verify(); if (is_wp_error($result)) { $errors->add($result->get_error_code(), $result->get_error_message()); } } return $errors; } /** wp-login.php and WooCommerce both end in retrieve_password(), which fires this. */ public static function check_lostpassword($errors): void { // An administrator sending a reset link from the Users screen is not a visitor. if (! Selfcap_Plugin::protects('lostpassword') || (is_user_logged_in() && current_user_can('edit_users'))) { return; } $result = Selfcap_Plugin::verify(); if (is_wp_error($result) && is_wp_error($errors)) { $errors->add($result->get_error_code(), $result->get_error_message()); } } public static function comment_field(string $submit_field): string { if (! Selfcap_Plugin::protects('comments') || current_user_can('moderate_comments')) { return $submit_field; } return Selfcap_Plugin::widget().$submit_field; } public static function check_comment(array $commentdata): array { $type = $commentdata['comment_type'] ?? ''; if (! Selfcap_Plugin::protects('comments') || current_user_can('moderate_comments') || in_array($type, ['pingback', 'trackback'], true) || ! self::is_form_post()) { return $commentdata; } $result = Selfcap_Plugin::verify(); if (is_wp_error($result)) { wp_die(esc_html($result->get_error_message()), '', ['response' => 403, 'back_link' => true]); } return $commentdata; } public static function check_woo_register($errors) { // Only the My Account form runs this filter; accounts created while placing // an order are covered by the checkout option. if (Selfcap_Plugin::protects('register')) { $result = Selfcap_Plugin::verify(); if (is_wp_error($result)) { $errors->add($result->get_error_code(), $result->get_error_message()); } } return $errors; } public static function check_woo_checkout($data, $errors): void { if (Selfcap_Plugin::protects('woo_checkout')) { $result = Selfcap_Plugin::verify(); if (is_wp_error($result)) { $errors->add($result->get_error_code(), $result->get_error_message()); } } } // ---- WooCommerce block checkout ------------------------------------- /** Lets the block checkout send the token along with the order request. */ public static function block_checkout_data(): void { if (! function_exists('woocommerce_store_api_register_endpoint_data')) { return; } woocommerce_store_api_register_endpoint_data([ 'endpoint' => 'checkout', 'namespace' => self::STORE_API_NAMESPACE, 'schema_callback' => static function (): array { return [ 'token' => [ 'description' => 'Selfcap response token', 'type' => 'string', 'context' => [], 'optional' => true, ], ]; }, ]); } /** Prints the widget right above the "Place order" button of the block checkout. */ public static function block_checkout_widget($content) { if (! Selfcap_Plugin::protects('woo_checkout')) { return $content; } // Its own class: the checkout block rebuilds this markup in the browser, // so forms.js renders the widget once the block is on the page. return str_replace('class="self-captcha"', 'class="self-captcha-block"', Selfcap_Plugin::widget()).$content; } public static function check_block_checkout($order, $request): void { // The checkout also sends PUT requests while the visitor edits the form // after a failed payment; only placing the order (POST) spends the token. if (! Selfcap_Plugin::protects('woo_checkout') || ! is_object($request) || ! method_exists($request, 'get_method') || $request->get_method() !== 'POST') { return; } $extensions = isset($request['extensions']) ? (array) $request['extensions'] : []; $data = isset($extensions[self::STORE_API_NAMESPACE]) ? (array) $extensions[self::STORE_API_NAMESPACE] : []; $token = isset($data['token']) && is_string($data['token']) ? sanitize_text_field($data['token']) : ''; $result = Selfcap_Plugin::verify($token); if (is_wp_error($result)) { throw new \Automattic\WooCommerce\StoreApi\Exceptions\RouteException( 'selfcap', // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped esc_html($result->get_error_message()), 400 ); } } // ---- Contact Form 7 ------------------------------------------------- public static function cf7_tag(): void { if (function_exists('wpcf7_add_form_tag')) { wpcf7_add_form_tag('selfcap', [Selfcap_Plugin::class, 'widget']); } } public static function check_cf7($spam, $submission = null) { if ($spam || ! Selfcap_Plugin::protects('cf7')) { return $spam; } $form = function_exists('wpcf7_get_current_contact_form') ? wpcf7_get_current_contact_form() : null; // Only forms that actually include the [selfcap] tag are checked. if (! $form || empty($form->scan_form_tags(['type' => 'selfcap']))) { return $spam; } return is_wp_error(Selfcap_Plugin::verify()); } }