helsingborg-stad/recaptcha-integration

Google ReCAPTCHA v3 集成,适用于 Municipio 和插件

1.1.2 2021-03-24 10:38 UTC

This package is auto-updated.

Last update: 2024-08-24 17:59:05 UTC


README

Google ReCAPTCHA v3 集成,适用于 Municipio 和插件

使用 Composer 安装

composer require helsingborg-stad/recaptcha-integration

如何使用

开始使用所需的基本功能。

前端标记(HTML)

将隐藏的输入添加到您的表单中。Google 将用哈希字符串填充输入。

<input type="hidden" class="g-recaptcha-response" id="g-recaptcha-response"
       name="g-recaptcha-response" value=""/>

后端密钥(PHP)

在 Google reCAPTCHA 网站上注册您的网站域名并获取所需的密钥。https://developers.google.com/recaptcha

注意:本包适用于版本 3。提交后,Google 将提供以下两个信息。

  • 网站密钥

  • 密钥

将密钥添加到您的配置或 functions.php 文件中。替换:YOUR-RECAPTCHA-SITE-KEYYOUR-RECAPTCHA-SECRET-KEY

<?php

    define('G_RECAPTCHA_KEY', 'YOUR-RECAPTCHA-SITE-KEY');
    define('G_RECAPTCHA_SECRET', 'YOUR-RECAPTCHA-SECRET-KEY');

?>

如果使用 functions.php 的基本 WordPress 示例

将以下代码片段添加到您的 functions.php 文件中。

<?php

    use \HelsingborgStad\RecaptchaIntegration as Captcha;
    
?>    

Google reCaptcha v3 JavaScript。

此 PHP 代码将包含所有必要的 JavaScript。

<?php

    add_action('wp_enqueue_scripts', 'getScripts', 999);
    
    function getScripts(){
        Captcha::initScripts();
    }

?>    

PHP 函数验证

此函数将在发布前运行验证。在这个例子中,它在评论发布前运行。

<?php 

    add_action('pre_comment_on_post', 'reCaptchaValidation');
    
    function reCaptchaValidation() {
        
        if (is_user_logged_in()) {
            return;
        }

        Captcha::initCaptcha();
    }

?>    

就是这样...

基于类的 WordPress 示例

如果您喜欢 PHP 类,这是一个简单的例子。

前端

<?php

    namespace YourTheme\YourCommentLogicNameSpace;
    use \HelsingborgStad\RecaptchaIntegration as Captcha;
    
   /**
    * Class CommentsFrontEnd
    * @package YourTheme\YourCommentLogicNameSpace
    */
    class CommentsFrontEnd
    {
       /**
        * CommentFrontEnd constructor.
        */
        public function __construct()
        {
            add_action('wp_enqueue_scripts', array($this, 'getScripts'), 999);
        } 
        
       /**
        * Enqueue Google Captcha javaScripts
        */
        public static function getScripts(){
            Captcha::initScripts();
        }
    }    
        
?>    

后端

<?php

    namespace YourTheme\YourCommentLogicNameSpace;
    use \HelsingborgStad\RecaptchaIntegration as Captcha;

   /**
    * Class CommentsBackEnd
    * @package YourTheme\YourCommentLogicNameSpace
    */
    class CommentsBackEnd
    {
        /**
         * CommentsBackEnd constructor.
         */
        public function __construct()
        {
            add_action('pre_comment_on_post', array($this, 'reCaptchaValidation'));
        }
        
        /**
         * Validate reCaptcha
         */
        public function reCaptchaValidation()
        {
            if (is_user_logged_in()) {
                return;
            }
    
            Captcha::initCaptcha();
        }
    }    
?>    

更多关于 Google reCaptcha 的工作原理。

https://en.wikipedia.org/wiki/ReCAPTCHA

https://developers.google.com/recaptcha/docs/v3

就是这样 folks :-)