Making WordPress.org

Changeset 14952


Ignore:
Timestamp:
06/08/2026 04:08:52 PM (5 months ago)
Author:
bor0
Message:

Well, someone added captcha and these stuff, so just committing

Location:
sites/trunk/jobs.wordpress.net/public_html/wp-content/plugins/jobswp
Files:
2 added
2 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/jobs.wordpress.net/public_html/wp-content/plugins/jobswp/jobswp-captcha.php

    r10000 r14952  
    11<?php
    22/**
    3  * Plugin Name: JobsWP reCaptcha
    4  * Version:     1.1
    5  * Plugin URI:  https://jobs.wordpress.net/
    6  * Author:      Scott Reilly
    7  * Description: Adds reCaptcha field to job posting form for jobs.wordpress.net.
    8  *
    93 * Site must define JOBSWP_RECAPTCHA_SITE_KEY and JOBSWP_RECAPTCHA_SECRET_KEY as
    104 * obtained from Google reCaptcha.
    … …  
    4539
    4640        /**
    47          * Performs setup reCaptcha use, namely enqueuing JS.
     41         * Performs setup for reCaptcha, namely enqueuing JS.
    4842         *
    4943         * @access protected
    … …  
    5145        protected static function setup() {
    5246                wp_enqueue_script( 'recaptcha-api', 'https://www.google.com/recaptcha/api.js', array(), '1' );
     47        }
     48
     49        /**
     50         * Determines if reCaptcha is configured.
     51         *
     52         * Captcha is configured if the site key and secret key are defined.
     53         *
     54         * @access protected
     55         *
     56         * @return bool True if reCaptcha is configured, false otherwise.
     57         */
     58        protected static function is_configured() {
     59                return self::get_site_key() && self::get_secret_key();
     60        }
     61
     62        /**
     63         * Determines if reCaptcha is required.
     64         *
     65         * @access protected
     66         *
     67         * @return bool True if reCaptcha is required, false otherwise.
     68         */
     69        protected static function is_required() {
     70                /**
     71                 * Filters whether reCaptcha is required for form submissions that would otherwise require it.
     72                 *
     73                 * @param bool True if reCaptcha is required, false otherwise. Default is true if the environment is production.
     74                 */
     75                return (bool) apply_filters( 'jobswp_require_captcha', wp_get_environment_type() === 'production' );
    5376        }
    5477
    … …  
    93116                // Only inject the captcha field on the verification page.
    94117                if ( self::do_captcha() ) {
    95                         self::setup();
    96                         echo '<div class="g-recaptcha" data-sitekey="' . self::get_site_key() . '"></div>' . "\n";
     118                        self::output_recaptcha_field();
    97119                }
     120        }
     121
     122        /**
     123         * Outputs reCAPTCHA field unless it is not required.
     124         */
     125        public static function output_recaptcha_field() {
     126                if ( ! self::is_required() ) {
     127                        return;
     128                }
     129                self::setup();
     130                echo '<div class="g-recaptcha" data-sitekey="' . esc_attr( self::get_site_key() ) . '"></div>' . "\n";
     131        }
     132
     133        /**
     134         * Verifies a submitted reCAPTCHA response with Google's API.
     135         *
     136         * If reCAPTCHA is not configured (missing keys), verification will fail.
     137         *
     138         * @return string|false Error message, or false if verification succeeded or was skipped.
     139         */
     140        public static function verify_recaptcha_token() {
     141                if ( ! self::is_required() ) {
     142                        return false;
     143                }
     144
     145                if ( empty( $_POST['g-recaptcha-response'] ) ) {
     146                        return __( 'The captcha needs to be provided.', 'jobswp' );
     147                }
     148
     149                $verify = array(
     150                        'secret'   => self::get_secret_key(),
     151                        'remoteip' => isset( $_SERVER['REMOTE_ADDR'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ) : '',
     152                        'response' => sanitize_text_field( wp_unslash( $_POST['g-recaptcha-response'] ) ),
     153                );
     154
     155                $response = wp_remote_post( 'https://www.google.com/recaptcha/api/siteverify', array( 'body' => $verify ) );
     156
     157                if ( ! is_wp_error( $response ) && 200 === (int) wp_remote_retrieve_response_code( $response ) ) {
     158                        $result = json_decode( wp_remote_retrieve_body( $response ), true );
     159                        if ( empty( $result['success'] ) ) {
     160                                return __( 'The captcha was incorrect.', 'jobswp' );
     161                        }
     162                        return false;
     163                }
     164
     165                return __( 'Unable to verify captcha at this time. Try again in a few minutes.', 'jobswp' );
    98166        }
    99167
    … …  
    107175                // Only proceed if no error was already thrown.
    108176                if ( self::do_captcha() && ! $errors && $_POST ) {
    109                         if ( empty( $_POST['g-recaptcha-response'] ) ) {
    110                                 $errors = __( 'The captcha needs to be provided.', 'jobswp' );
    111                         } else {
    112                                 self::setup();
    113 
    114                                 $verify = array(
    115                                         'secret'    => self::get_secret_key(),
    116                                         'remoteip'      => $_SERVER['REMOTE_ADDR'],
    117                                         'response'  => $_POST['g-recaptcha-response'],
    118                                 );
    119 
    120                                 $response = wp_remote_post( 'https://www.google.com/recaptcha/api/siteverify', array( 'body' => $verify ) );
    121 
    122                                 if ( ! is_wp_error( $response ) && 200 == wp_remote_retrieve_response_code( $response ) ) {
    123                                         $result = json_decode( wp_remote_retrieve_body( $response ), true );
    124                                         if ( ! $result['success'] ) {
    125                                                 $errors = __( 'The captcha was incorrect.', 'jobswp' );
    126                                         }
    127                                 } else {
    128                                         $errors = __( 'Unable to verify captcha at this time. Try again in a few minutes.', 'jobswp' );
    129                                 }
     177                        $captcha_error = self::verify_recaptcha_token();
     178                        if ( $captcha_error ) {
     179                                $errors = $captcha_error;
    130180                        }
    131181                }
  • sites/trunk/jobs.wordpress.net/public_html/wp-content/plugins/jobswp/jobswp.php

    r14842 r14952  
    1111
    1212require_once( dirname( __FILE__ ) . '/jobswp-captcha.php' );
     13require_once( dirname( __FILE__ ) . '/jobswp-contact-form.php' );
     14require_once( dirname( __FILE__ ) . '/jobswp-moderation.php' );
    1315require_once( dirname( __FILE__ ) . '/jobswp-template.php' );
    1416require_once( dirname( __FILE__ ) . '/jobswp-walker.php' );
Note: See TracChangeset for help on using the changeset viewer.