我意识到基本上有人发表了我的评论,并补充了我的回答。这是一个使用内置REST API 多样性。在经过身份验证的请求上查找停靠点有点棘手,但这应该是一个偶然的机会。
// send authorization info
beforeSend: function(xhr) {
// attach REST nonce for logged in user requests -- otherwise will end up no-priv
xhr.setRequestHeader (\'X-WP-Nonce\', \'<?php echo wp_create_nonce( \'wp_rest\' ); ?>\');
},
单击外部链接时,会对自定义REST路由进行ajax调用。然后将链接添加到users元中的URL数组中,并递增计数数据。
在几次外部单击之后,您可以检查用户的元数据,并查看所有单击和计数。
<?php
add_action( \'rest_api_init\', function() {
// namespace of url
$namespace = \'v2\';
// route
$base = \'/link/count/\';
// register request
register_rest_route( $namespace, $base, array (
// hide from discovery
\'show_in_index\' => false,
// only accept POST methods
\'methods\' => \'POST\',
// handler
\'callback\' => \'add_link_count\',
// validate args
\'args\' => array(
\'link\' => array(
\'validate_callback\' => function($param, $request, $key) {
return ! empty( $param );
}
),
),
// check for logged in users
\'permission_callback\' => function () {
return is_user_logged_in();
}
) );
} );
// only add late in the footer
add_action( \'wp_footer\', function() {
// only add for logged in users
if ( ! get_current_user_id() ) {
return;
}
?>
<script>
(function($) {
// only search for links with data
$ (\'a[href^="http"], a[href^="//"]\').each (function() {
// excluded specific domains
var excludes = [
\'example.com\'
];
for (i = 0; i < excludes.length; i++) {
if (this.href.indexOf (excludes[i]) != -1) {
return true; // continue each() with next link
}
}
// filter by domain -- only track external links
if (this.href.indexOf (location.hostname) == -1) {
// attach click event
$ (this).click (function(e) {
// do ajax call
$.ajax ({
// send request to REST URL
url: \'<?php echo esc_js( get_rest_url( null, \'/v2/link/count/\' ) ); ?>\',
// use the accepted method - POST
method: \'POST\',
// send authorization info
beforeSend: function(xhr) {
// attach REST nonce for logged in user requests -- otherwise will end up no-priv
xhr.setRequestHeader (\'X-WP-Nonce\', \'<?php echo wp_create_nonce( \'wp_rest\' ); ?>\');
},
data: {
// link to track click count
\'link\': this.href
}
}).done (function(response) {
// show response in the console
// console.log (response);
});
// block click -- for testing
// e.preventDefault();
});
}
})
} (jQuery));
</script>
<?php
}, 30 );
// handle the REST url
function add_link_count( $request ) {
// get the current user
$user_id = get_current_user_id();
// check for valid user id > 0
if ( ! $user_id || $user_id < 1 ) {
return new WP_REST_Response( \'Unauthorized\', 403 );
}
// get the link to log
$link = $request->get_param( \'link\' );
// validate the request
if( empty($link)) return new WP_REST_Response( \'Bad Request\', 400 );
// pull the previous click data
$prev = get_user_meta($user_id, \'link_clicks\', true);
// generate if it doesn\'t already exist
if( ! $prev || ! is_array($prev)) $prev = array();
// make sure the value is numeric
if( ! is_numeric($prev[$link]) ){
$prev[$link] = 0;
}
// increment based on the click
$prev[$link]++;
// update the users metadata
update_user_meta( $user_id, \'link_clicks\', $prev, null );
// return success response
return new WP_REST_Response( \'OK\', 200 );
}
要读取以单击返回,请使用单击元数据查询用户:
function get_clicks_by_user() {
$args = array (
\'meta_query\' => array (
array (
\'key\' => \'link_clicks\',
\'compare\' => \'EXISTS\',
),
),
);
$user_query = new WP_User_Query( $args );
$users = $user_query->get_results();
$clicks_by_user = array ();
foreach ( $users as $user ) {
$clicks_by_user[ $user->data->user_login ] = get_user_meta( $user->ID, \'link_clicks\', true );
}
return $clicks_by_user;
}
并显示结果:
echo "<pre>";
print_r( get_clicks_by_user() );