-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathun-click-counter.php
66 lines (58 loc) · 2.21 KB
/
un-click-counter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
/*
Plugin Name: Un-click counter
Plugin URI: https://kodabra.unchikov.ru/file-download-counter/
Description: Счетчик скачиваний файлов (шорткод [un_click_counter], у ссылки id="download"; или [un_click_counter id="id_ссылки"])
Author: Elena Unchikova
Version: 1.0.0
Author URI: https://kodabra.unchikov.ru/
License: GPL2
*/
// Выход при прямом доступе.
if ( ! defined( 'ABSPATH' ) ) { exit; }
add_shortcode( 'un_click_counter', 'un_click_counterf' ); // шорткод [un_click_counter]
function un_click_counterf($attrs) {
ob_start();
$params=shortcode_atts( array('id' => 'download'),$attrs);
$idlink=$params['id'];
// *******************************************************
?>
<link rel='stylesheet' href="<?php echo plugin_dir_url( __FILE__ ) . 'un-click-counter-style.css'; ?>" type='text/css' media='all' />
<span id="<?php echo $idlink . 'clc';?>" class="uncnt"></span>
<script type="text/javascript">
jQuery(document).ready(function($) {
var dnl = document.getElementById('<?php echo $idlink;?>');
var hrf = dnl.href;
var dnld_name = hrf.split("/").pop();
var dnld_href = dnld_name.slice(0, -4);
var dnld_href_txt = dnld_href+'.txt';
var dnld_hrefn_txt = "<?php echo plugin_dir_url( __FILE__ ) . 'calc/'; ?>"+dnld_href_txt;
$.ajaxSetup({cache: false});
$.ajax({ url: dnld_hrefn_txt, success: function(file_content) {
document.getElementById("<?php echo $idlink . 'clc';?>").innerHTML="(Скачиваний: "+file_content+")";
}
});
dnl.addEventListener('click', function () {
var data = "dnld_href_txt=" + encodeURIComponent(dnld_href_txt);
var xhr = new XMLHttpRequest();
xhr.open("POST", "<?php echo plugin_dir_url( __FILE__ ) . 'un-click-counter-post.php'; ?>" , true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send(data);
xhr.onreadystatechange = function()
{
if (xhr.readyState == 4)
{
if(xhr.status == 200)
{
document.getElementById("<?php echo $idlink . 'clc';?>").innerHTML="(Скачиваний: "+xhr.responseText+")";
}
}
}
})
});
</script>
<?php
// *******************************************************
return ob_get_clean();
}
?>