[wordpress插件] Cookie Connector For Themer前者的Cookie连接器

wordpress 插件 文章 2020-04-02 05:40 579 0 全屏看文

AI助手支持GPT4.0

评分
0
描述

Cookie Connector for (Beaver) Themer

(海狸)主题的Cookie连接器

Plugin description

插件说明

Cookie Connector for Beaver Themer is an unofficial addon-plugin for the popular Beaver Themer plugin.

用于Beaver Themer的Cookie连接器是流行的Beaver Themer插件的非官方插件。

It allows you to read cookie-values using the Beaver Themer Connector and use Conditional Logic to hide/show seperate nodes (modules, columns and/or rows) using cookie values.

它允许您使用Beaver Themer连接器读取cookie值,并使用条件逻辑使用cookie值隐藏/显示单独的节点(模块,列和/或行)。

Cookie Connector also allows you to create cookies using an AJAX call.

Cookie Connector还允许您使用AJAX调用创建cookie。

For security measures you will need to write the AJAX yourself, but an example file on how to do that can be found here, in the example folder:

为了安全起见,您需要自己编写AJAX,但是可以在以下示例文件夹中找到有关如何执行此操作的示例文件:

https://github.com/badabingbreda/cookie-connector-for-themer

https://github.com/badabingbreda/cookie-connector-for-themer

/

/

Using the Cookie Connector

使用Cookie连接器

You can display the Cookie Connector wherever you’d normally insert it as a string, using either the connect or insert button.

您可以使用 connect insert 按钮将Cookie连接器显示在通常以字符串形式插入的任何位置。

Using the Conditional Logic filter

使用条件逻辑过滤器

Because cookies have a certain validity, it can’t return a value when the cookie isn’t there or has become invalid.

由于cookie具有一定的有效性,因此当cookie不存在或变为无效时,它将无法返回值。

The Conditional Logic filter has an extra parameter to set a default value for whenever it doesn’t return anything.

条件逻辑过滤器有一个额外的参数,可以在不返回任何内容时设置默认值。

Setting this parameter return that value whenever it doesn’t exist.

设置此参数会在不存在该值时返回该值。

Writing cookies values

编写Cookie值

Cookies are written before any headers are written to the visitor’s browser.

在将任何标题写入访问者的浏览器之前,先写入Cookie。

The downside to that is that cookies can only be read when the visitor’s sends a request, since they are sent over WITH the request.

这样做的缺点是,仅在访问者发送请求时才能读取Cookie,因为它们是随请求一起发送的。

This means that you can’t write a cookie’s value and immediately read that cookies new value, within a single run of a page-call.

这意味着您无法在一次页面调用中就写入Cookie的值并立即读取该cookie的新值。

To work around that, cookies can be written using an AJAX call.

要解决此问题,可以使用AJAX调用编写cookie。

“Cookie Connector for Themer” enqueues a script that registers the cookieConnector object, which handles the security and sending of requests.

“用于Themer的Cookie连接器”使注册 cookieConnector 对象的脚本排队,该对象处理安全性和请求发送。

Let’s consider the following html-code that is used in a HTML module:

让我们考虑一下HTML模块中使用的以下html代码:

Set cookie value

  

设置cookie值

p>

>

Unset cookie value

未设置Cookie值

The first parameter is the actionname that is going to be called in the PHP script, so in this example (see below) the part after ‘wp_ajax_’ and ‘wp_ajax_nopriv_’.

第一个参数是将在PHP脚本中调用的 actionname ,因此在此示例中(见下文),在“ wp_ajax_”和“ wp_ajax_nopriv_”之后。

You can also add an extra, optional, third parameter debug that will add a console.log dump of the response, should you need to know what’s being answered:

您还可以添加一个额外的,可选的第三个参数 debug ,如果您需要知道要回答什么,它将添加响应的console.log转储:

Set cookie value

  

设置cookie值

Unset cookie value

未设置cookie值

Adding the PHP code to create/change/delete the cookie

添加PHP代码以创建/更改/删除cookie

Clicking the link wil trigger an AJAX call that will set a cookie on the visitor’s device, if their browser allows it.

单击链接将触发AJAX调用,如果访问者的浏览器允许,它将在访问者的设备上设置Cookie。

On the server-side, you will need to add one or two ajax callbacks, depending if the call can be made without being logged in.

在服务器端,您将需要添加一个或两个ajax回调,具体取决于是否可以在不登录的情况下进行调用。

  <?php    

add_action( 'wp_ajax_setmycookie' , 'callback_setmycookie' );    

add_action('wp_ajax_setmycookie','callback_setmycookie');    

add_action( 'wp_ajax_nopriv_setmycookie' , 'callback_setmycookie' );    

add_action('wp_ajax_nopriv_setmycookie','callback_setmycookie');    

add_action( 'wp_ajax_unsetmycookie' , 'callback_setmycookie' );    

add_action('wp_ajax_unsetmycookie','callback_setmycookie');    

add_action( 'wp_ajax_nopriv_unsetmycookie' , 'callback_setmycookie' );    

add_action('wp_ajax_nopriv_unsetmycookie','callback_setmycookie');    

function callback_setmycookie() {        

函数callback_setmycookie(){        

// first check if this ajax call is        

//首先检查此ajax调用是否为        

// done using the script belonging to the installation        

//使用属于安装的脚本完成        

//        

//        

if( ! check_ajax_referer( 'cookie-security-nonce' , 'security' ) ) {            

if(!check_ajax_referer('cookie-security-nonce','security')){            

wp_send_json_error( 'Invalid security token sent.' );            

wp_send_json_error('无效的安全令牌已发送。');            

wp_die();        

wp_die();        

}        

}        

// Check if visitor has accepted the cookies from Cookie Notice plugin        

//检查访问者是否接受了Cookie Notice插件中的cookie        

// If you're using another plugin, you should write your own check here        

//如果您使用的是其他插件,则应在此处编写自己的支票        

if ( !function_exists('cn_cookies_accepted') || !cn_cookies_accepted() ) {            

如果(!function_exists('cn_cookies_accepted')||!cn_cookies_accepted()){            

wp_die();        

wp_die();        

}        

}        

if ( !defined( 'DOING_AJAX' ) ) define( 'DOING_AJAX' , TRUE );        

如果(!defined('DOING_AJAX'))define('DOING_AJAX',TRUE);        

// set your cookie name here        

//在此处设置您的Cookie名称        

$cookie_name = 'mylanguage';        

$ cookie_name ='mylanguage';        

$default_validity = 60 * 60;

$ default_validity = 60 * 60;

// = 60 minutes        

// = 60分钟        

// try to get the cookie_value        

//尝试获取cookie_value        

$cookie_value = isset( $_GET['cv'] ) ?

$ cookie_value = isset($ _GET ['cv'])吗?

$_GET['cv'] : false;        

$ _GET ['cv']:false;        

// try to get the cookie validity period.

//尝试获取Cookie的有效期。

If not default to default validity        

如果未默认为默认有效性        

$cookie_valid = isset( $_GET['valid'] ) ?

$ cookie_valid = isset($ _GET ['valid'])吗?

$_GET['valid'] : $default_validity;        

$ _GET ['valid']:$ default_validity;        

// if the action is setmycookie we need a cookie_value (cv) because else it will fail.

//如果该操作是setmycookie,则我们需要一个cookie_value(cv),因为否则它将失败。

Check for it and return an error if there isn't one.        

检查它,如果没有,则返回错误。        

if ( ! $cookie_value && $_GET['action'] == 'setmycookie' ) {            

如果(!$ cookie_value && $ _GET ['action'] =='setmycookie'){            

wp_send_json_error( array( 'success' => false, 'error' => '402', 'message' => 'cookie not set, no value given. ( cv )' ) );        

wp_send_json_error(array('success'=> false,'error'=>'402','message'=>'未设置cookie,没有给出值。(cv)'));        

}        

}        

// check action parameter        

//检查动作参数        

// UNSET mycookie        

//取消设置mycookie        

if ( 'unsetmycookie' == $_GET['action'] ) {            

如果('unsetmycookie'== $ _GET ['action']){            

setcookie( $cookie_name , 'unset value' , time() - 1 , COOKIEPATH, COOKIE_DOMAIN , isset($_SERVER["HTTPS"]) );            

setcookie($ cookie_name,'unset value',time()-1,COOKIEPATH,COOKIE_DOMAIN,isset($ _ SERVER [“ HTTPS”])));            

wp_send_json( array( 'success' => true, 'message' => "Done unsetting cookie '{$cookie_name}'." ) );        

wp_send_json(array('success'=> true,'message'=>“完成未设置cookie'{$ cookie_name}'。”));        

} else if ( 'setmycookie' == $_GET['action'] ) {            

} else if('setmycookie'== $ _GET ['action']){            

setcookie( $cookie_name , $cookie_value , time() + $cookie_valid , COOKIEPATH, COOKIE_DOMAIN , isset($_SERVER["HTTPS"]), true );            

setcookie($ cookie_name,$ cookie_value,time()+ $ cookie_valid,COOKIEPATH,COOKIE_DOMAIN,isset($ _ SERVER [“ HTTPS”]),true);            

wp_send_json( array( 'success' => true, 'message' => "Done setting cookie '{$cookie_name}' to value '{$cookie_value}' with validity $cookie_valid seconds." ) );        

wp_send_json(array('success'=> true,'message'=>“将cookie'{$ cookie_name}'设置为值'{$ cookie_value}'的有效期为$ cookie_valid秒。”));;        

}        

}        

wp_die();    

wp_die();    

}

}

It is advised to use wp_send_json with a 'success' parameter (either true or false) so that you check it in your javascript and add actions after sending the cookieConnector() command, for instance a reload of the page or forwarding to

建议将wp_send_json与“ success”参数(真或假)一起使用,以便在JavaScript中检查它并在发送cookieConnector()命令后添加操作,例如重新加载页面或转发到

an url that you received from the server based on the click.

您根据点击次数从服务器收到的网址。

For instance, the javascript below will try to create the cookie.

例如,下面的javascript将尝试创建cookie。

When successful it will display an alert on the browser and reload the page after 1.5 seconds.

成功后,它将在浏览器上显示警报,并在1.5秒后重新加载页面。

version history

版本历史记录

1.0.1 Updated example code

1.0.1 更新了示例代码

1.0.0 Initial release (January 18th, 2019)

1.0.0 初始版本(2019年1月18日)

下载地址
https://downloads.wordpress.org/plugin/cookie-connector-for-themer.zip
-EOF-

AI助手支持GPT4.0