-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathchartbeat.php
679 lines (591 loc) · 23.6 KB
/
chartbeat.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
<?php
/*
Plugin Name: Chartbeat
Plugin URI: http://chartbeat.com/wordpress/
Description: Adds Chartbeat pinging to Wordpress.
Version: 2.0.7
Author: Chartbeat
Author URI: http://chartbeat.com/
*/
/*
Copyright 2009-2017 Chartbeat Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
function chartbeat_menu() {
add_options_page('chartbeat plugin options', 'Chartbeat', 'manage_options', 'chartbeat-options', 'chartbeat_options_page');
add_menu_page( 'Chartbeat Console', 'Chartbeat', 'edit_posts', 'chartbeat_console', 'chartbeat_console', plugins_url('media/chartbeat.png', __FILE__) );
}
add_action('admin_menu', 'chartbeat_menu');
function check_chartbeat_accountid_error() {
if (!get_option('chartbeat_userid') ) {
add_action( 'admin_notices', 'display_chartbeat_accountid_error' );
}
function display_chartbeat_accountid_error() {
$class = 'notice notice-error';
$message = 'You need to set your Chartbeat <a href="'.esc_url(admin_url('options-general.php?page=chartbeat-options')).'">Account ID</a> in the Chartbeat options page';
printf( '<div class="%1$s"><p>%2$s</p></div>', $class, $message );
}
}
add_action( 'admin_init', 'check_chartbeat_accountid_error', 1 );
function chartbeat_console() {
if (!current_user_can('edit_posts')) {
wp_die( __('You do not have sufficient permissions to access this page.') );
}
$domain = apply_filters( 'chartbeat_config_domain', chartbeat_get_display_url (get_option('home')) );
?>
<style type="text/css">
#wpbody-content { height:100%; }
#chartbeat-iframe {
padding: 10px 0;
min-height: 640px;
}
</style>
<?php
if (!get_option('chartbeat_enable_newsbeat')) {
$iframe_url = add_query_arg( array(
'url' => chartbeat_get_display_url( $domain ),
'k' => get_option( 'chartbeat_apikey' ),
'slim' => 1,
), '//chartbeat.com/dashboard/' );
?>
<iframe id="chartbeat-iframe" width="100%" height="100%" src="<?php echo esc_url( $iframe_url ); ?>"></iframe>
<?php
} else {
$iframe_url = add_query_arg( array(
'url' => chartbeat_get_display_url( $domain ),
'k' => get_option( 'chartbeat_apikey' ),
'slim' => 1,
), '//chartbeat.com/publishing/dashboard/' );
?>
<iframe id="chartbeat-iframe"width="100%" height="100%" src="<?php echo esc_url( $iframe_url ); ?>"></iframe>
<?php
}
}
function chartbeat_options_page() {
$domain = apply_filters( 'chartbeat_config_domain', chartbeat_get_display_url (get_option('home')) );
?>
<div class="wrap">
<h2>Chartbeat</h2>
<form method="post" action="options.php" onsubmit="buildOptions()">
<?php
// outputs all of the hidden fields that options.php will check, including the nonce
wp_nonce_field('update-options');
settings_fields('chartbeat-options'); ?>
<script>
function showSettings() {
window.open('//chartbeat.com/wordpress/?site=' + encodeURIComponent(window.location.host));
}
</script>
To enable tracking, you must enter your chartbeat account id. <a href="#" onclick="showSettings()">Find yours.</a> <br />
<table class="form-table">
<tr>
<th scope="row">Account ID</th>
<td><input size="30" type="text" name="chartbeat_userid"
value="<?php echo esc_attr( get_option('chartbeat_userid') ); ?>" />
</td>
</tr>
<tr>
<th scope="row"><?php _e('Track visits by Site Admins?','chartbeat'); ?><br />
<small>Administrators must be logged in to avoid tracking.</small>
</th>
<td>
<input type="radio" name="chartbeat_trackadmins" value="1" <?php checked( get_option('chartbeat_trackadmins'), 1 ); ?> />
Yes
<input type="radio" name="chartbeat_trackadmins" value="0" <?php checked( get_option('chartbeat_trackadmins'), 0 ); ?> />
No
</td>
</tr>
<tr>
<th scope="row"><?php _e('Enable Chartbeat Publishing?','chartbeat'); ?><br /> <small>Sign
up for <a href="http://chartbeat.com/publishing/">Chartbeat Publishing</a>.
</small></th>
<td>
<input type="radio" name="chartbeat_enable_newsbeat" value="1" <?php checked( get_option('chartbeat_enable_newsbeat'), 1 ); ?> />
Yes
<input type="radio" name="chartbeat_enable_newsbeat" value="0" <?php checked( get_option('chartbeat_enable_newsbeat'), 0 ); ?> />
No
</td>
</tr>
<tr>
<th scope="row">
<?php _e('Enable Headline Testing?','chartbeat'); ?><br />
<small>Sign up for <a href="https://chartbeat.com/publishing/headline-optimization/">Headline Testing</a></small>
</th>
<td>
<input type="radio" name="chartbeat_enable_headline_testing" value="1" <?php checked( get_option('chartbeat_enable_headline_testing'), 1 ); ?> />
Yes
<input type="radio" name="chartbeat_enable_headline_testing" value="0" <?php checked( get_option('chartbeat_enable_headline_testing'), 0 ); ?> />
No
</td>
</tr>
<tr>
<th scope="row">API Key<br/>
<?php if (get_option('chartbeat_enable_newsbeat')) { ?>
<small>Get API key <a href="https://chartbeat.com/newsbeat/settings/api-keys/">here</a></small>
<?php } else { ?>
<small>Get API key <a href="https://chartbeat.com/apikeys/">here</a></small>
<?php } ?>
</th>
<td><input size="30" type="text" name="chartbeat_apikey" value="<?php echo esc_attr( get_option('chartbeat_apikey') ); ?>" />
</td>
</tr>
</table>
<br /> <br />
<script src="<?php echo plugins_url('media/topwidget.compiled.js', __FILE__); ?>" type="text/javascript"></script>
<script type="text/javascript">
var themes = { 'doe': { 'bgcolor': '', 'border': '#dde7d4', 'text': '#555' },
'gray': { 'bgcolor': '#e3e3e3', 'border': '#333333', 'text': '#555', 'header_bgcolor': '#999999', 'header_color': '#fff' },
'red': { 'bgcolor': '#ffffff', 'border': '#cc3300', 'text': '#555', 'header_bgcolor': '#f5c5be', 'header_color': '#fff' },
'blue': { 'bgcolor': '#e0ecff', 'border': '#3a5db0' },
'green': { 'bgcolor': '#c9edcc', 'border': '#69c17d', 'text': '#555' } };
var theme = 'doe';
var limit = 10;
function changeTheme(select) {
theme = select.options[select.selectedIndex].value;
renderWidget();
}
function changeLimit(select) {
limit = select.options[select.selectedIndex].value;
renderWidget();
}
function renderWidget() {
new CBTopPagesWidget( '<?php echo esc_js(get_option('chartbeat_apikey')) ?>',
{ 'host': '<?php echo esc_js( chartbeat_get_display_url( $domain ) ); ?>',
'background': themes[theme]['bgcolor'],
'border': themes[theme]['border'],
'header_bgcolor': themes[theme]['header_bgcolor'],
'header_color': themes[theme]['header_color'],
'text': themes[theme]['text'],
'limit': limit });
}
function addOption(array, key, val) {
array.push("'" + key + "': '" + val + "'");
}
function buildOptions() {
var options = [];
addOption(options, 'background', themes[theme]['bgcolor']);
addOption(options, 'border', themes[theme]['border']);
addOption(options, 'header_bgcolor', themes[theme]['header_bgcolor']);
addOption(options, 'header_color', themes[theme]['header_color']);
addOption(options, 'text', themes[theme]['text']);
addOption(options, 'limit', limit);
options = '{' + options.join(',') + '}';
document.getElementById('chartbeat_widgetconfig').value = options;
console.debug("options:" + options);
}
renderWidget();
</script>
If your theme supports it, you can also add a widget under
<tt>Appearance > Widgets</tt>
to show where users currently are on your site. <br> <br>
<table cellspacing="10">
<tr>
<td valign="top">Number of pages to show
<select name="metric" id="toplimit" onChange="changeLimit(this);">
<option value="5">5</option>
<option value="10" selected="selected">10</option>
<option value="20">20</option>
<option value="30">30</option>
</select>
<br /> <br />
Color scheme
<select name="theme" id="toptheme" onChange="changeTheme(this);">
<option value="doe">John Doe</option>
<option value="gray">Dorian Gray</option>
<option value="red">Red Rum</option>
<option value="blue">Blue Moon</option>
<option value="green">Green Giant</option>
</select>
</td>
<td> </td>
<td>
Sample:
<br> <br>
<div id="cb_top_pages"></div>
</td>
</tr>
</table>
<input type="hidden" id="chartbeat_widgetconfig" name="chartbeat_widgetconfig" value="{}" />
<p class="submit">
<input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
</p>
</form>
</div>
<?php
}
// Function to register settings and sanitize output. To be called later in add_action
function chartbeat_register_settings() {
register_setting('chartbeat-options','chartbeat_userid', 'intval');
register_setting('chartbeat-options','chartbeat_apikey','chartbeat_is_validmd5');
register_setting('chartbeat-options','chartbeat_widgetconfig','chartbeat_is_validjson');
register_setting('chartbeat-options','chartbeat_trackadmins','intval'); // add trackadmin setting
register_setting('chartbeat-options','chartbeat_enable_newsbeat','intval');
register_setting('chartbeat-options','chartbeat_enable_headline_testing','intval');
}
function chartbeat_is_validmd5($md5) {
if( !empty($md5) && preg_match('/^[a-f0-9]{32}$/', $md5) ) {
return $md5;
} else {
add_settings_error( 'chartbeat_apikey','invalid_apikey','API Key is not correct, please check again','error');
return 777;
}
}
function chartbeat_is_validjson($json_str) {
if( is_null(json_decode($json_str)) ) {
return $json_str;
} else {
add_settings_error( 'chartbeat_widgetconfig','invalid_widgetconfig','Widget config is malformed','error');
}
}
function chartbeat_configs() {
$domain = apply_filters( 'chartbeat_config_domain', chartbeat_get_display_url (get_option('home')) );
$domain = preg_replace('#^www\.(.+\.)#i', '$1', $domain);
$cb_configs['domain'] = $domain;
$user_id = get_option('chartbeat_userid');
$cb_configs['uid'] = $user_id;
// Get Author and Sections
// Only add these values on blog posts use the queried object in case there
// are multiple Loops on the page.
if (is_single()) {
$post = get_queried_object();
// Use the author's display name
$author = get_the_author_meta('display_name', $post->post_author);
$cb_configs['author'] = apply_filters( 'chartbeat_config_author', $author );
// Use the post's categories as sections
$cats = get_the_terms($post->ID, 'category');
if ($cats) {
$cat_names = array();
foreach ( $cats as $cat ) {
$cat_names[] = $cat->name;
}
}
$sections = (array)apply_filters( 'chartbeat_config_sections', $cat_names );
$cb_configs['sections'] = implode( ", ", $sections);
}
if ( apply_filters( 'chartbeat_config_use_canonical', true ) )
$cb_configs['use_canonical'] = 'true';
else
$cb_configs['use_canonical'] = 'false';
return $cb_configs;
}
function add_chartbeat_head() {
echo "\n<script type=\"text/javascript\">var _sf_startpt=(new Date()).getTime()</script>\n";
if( 1 == get_option( 'chartbeat_enable_headline_testing' ) ) :
$cb_configs = chartbeat_configs();
?>
<script type="text/javascript">
var _sf_async_config = _sf_async_config || {};
_sf_async_config.uid = <?php echo esc_js($cb_configs["uid"]); ?>;
_sf_async_config.domain = "<?php echo esc_js($cb_configs["domain"]); ?>";
_sf_async_config.useCanonical = <?php echo esc_js($cb_configs["use_canonical"]); ?>;
</script>
<script src="//static.chartbeat.com/js/chartbeat_mab.js"></script>
<?php
endif;
}
function add_chartbeat_config(){
$cb_configs = chartbeat_configs();
if( 1 != get_option( 'chartbeat_enable_headline_testing' ) ) :
?>
var _sf_async_config={};
_sf_async_config.uid = <?php echo esc_js($cb_configs["uid"]); ?>;
_sf_async_config.domain = "<?php echo esc_js($cb_configs["domain"]); ?>";
_sf_async_config.useCanonical = <?php echo esc_js($cb_configs["use_canonical"]); ?>;
<?php
endif;
$enable_newsbeat = get_option('chartbeat_enable_newsbeat');
if ($enable_newsbeat) { ?>
_sf_async_config.authors = "<?php echo esc_js($cb_configs["author"]); ?>";
_sf_async_config.sections = "<?php echo esc_js($cb_configs["sections"]); ?>";
<?php }
}
function add_chartbeat_footer() {
$user_id = get_option('chartbeat_userid');
if ($user_id) {
// if visitor is admin AND tracking is off, do not load chartbeat
if ( current_user_can( 'manage_options') && get_option('chartbeat_trackadmins') == 0)
return $analytics ;
?>
<script type="text/javascript">
<?php
echo add_chartbeat_config();
$default_chartbeat_url = "//static.chartbeat.com/js/chartbeat.js";
$chartbeat_url = apply_filters( 'chartbeat_url', $default_chartbeat_url );
?>
(function(){
function loadChartbeat() {
window._sf_endpt=(new Date()).getTime();
var e = document.createElement('script');
e.setAttribute('language', 'javascript');
e.setAttribute('type', 'text/javascript');
e.setAttribute('src', '<?php echo $chartbeat_url; ?>');
document.body.appendChild(e);
}
var oldonload = window.onload;
window.onload = (typeof window.onload != 'function') ?
loadChartbeat : function() { try { oldonload(); } catch (e) { loadChartbeat(); throw e} loadChartbeat(); };
})();
</script>
<?php
}
}
function chartbeat_amp_add_analytics( $analytics ) {
if ( ! is_array( $analytics ) ) {
$analytics = array();
}
$user_id = get_option('chartbeat_userid');
if ($user_id) {
// if visitor is admin AND tracking is off, do not load chartbeat
if ( current_user_can( 'manage_options') && get_option('chartbeat_trackadmins') == 0)
return $analytics ;
$cb_configs = chartbeat_configs();
$analytics['chartbeat'] = array(
'type' => 'chartbeat',
'attributes' => array(),
'config_data' => array(
'vars' => array(
'uid' => esc_js($cb_configs["uid"]),
'domain' => esc_js($cb_configs["domain"]),
)
),
);
$enable_newsbeat = get_option('chartbeat_enable_newsbeat');
if ($enable_newsbeat) {
$analytics['chartbeat']['config_data']['vars']['authors'] = esc_js($cb_configs['author']);
$analytics['chartbeat']['config_data']['vars']['sections'] = esc_js($cb_configs['sections']);
}
}
return $analytics;
}
add_filter( 'amp_post_template_analytics', 'chartbeat_amp_add_analytics' );
function chartbeat_fbia_analytics( $analytics ) {
$user_id = get_option('chartbeat_userid');
if ($user_id) {
// if visitor is admin AND tracking is off, do not load chartbeat
if ( current_user_can( 'manage_options') && get_option('chartbeat_trackadmins') == 0)
return $analytics ;
?>
<figure class="op-tracker">
<iframe>
<script type="text/javascript">
<?php echo add_chartbeat_config(); ?>
window._sf_endpt=(new Date()).getTime();
</script>
<script defer src="//static.chartbeat.com/js/chartbeat_fia.js"></script>
</iframe>
</figure>
<?php
}
}
add_action( 'instant_articles_article_header', 'chartbeat_fbia_analytics' );
class Chartbeat_Widget extends WP_Widget {
function __construct() {
parent::__construct('chartbeat_widget', 'Chartbeat Widget',array( 'description' => __('Display your site\'s top pages')));
if ( is_active_widget(false,false,$this->id_base,true) || is_admin() ) {
wp_enqueue_script( 'chartbeat_topwidget', plugins_url('media/topwidget.compiled.js', __FILE__) );
wp_localize_script( 'chartbeat_topwidget', 'cbproxy', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ),
'cbnonce' => wp_create_nonce( 'cbproxy-nonce' ) ) );
}
}
function widget( $args, $instance ) {
extract( $args );
echo $before_widget;
$api_key = get_option( 'chartbeat_apikey' );
$widget_config = get_option('chartbeat_widgetconfig');
if ( $api_key ) : ?>
<div id="cb_top_pages"></div>
<script type="text/javascript">
var options = { };
new CBTopPagesWidget( <?php echo json_encode(get_option('chartbeat_widgetconfig')); ?> );
</script>
<?php
endif;
echo $after_widget;
}
}
// Add proxy for Chartbeat API requests
add_action( 'wp_ajax_nopriv_cbproxy-submit', 'cbproxy_submit' );
add_action( 'wp_ajax_cbproxy-submit', 'cbproxy_submit' );
function cbproxy_submit() {
// check nonce
$nonce = $_GET['cbnonce'];
if ( ! wp_verify_nonce( $nonce, 'cbproxy-nonce' ) ) die ( 'cbproxy-nonce failed!');
$domain = apply_filters( 'chartbeat_config_domain', chartbeat_get_display_url (get_option('home')) );
$url = 'https://api.chartbeat.com';
$url .= $_GET['url'];
$url .= '&host=' . chartbeat_get_display_url(esc_js($domain)) .'&apikey=' . urlencode(get_option('chartbeat_apikey'));
$transient = 'cbproxy_' . md5($url);
header( 'Content-Type: application/jsonp' );
$response = get_transient( $transient );
if ( !$response ) {
$get = wp_remote_get( $url , array( 'timeout' => 3 ) );
if( is_wp_error( $response ) ) {
$response = json_encode( array( 'error' => $get->get_error_message() ) );
} else {
$response = wp_remote_retrieve_body($get);
}
set_transient($transient,$response,5);
}
echo htmlspecialchars_decode( esc_js($response) );
exit;
}
// Dashboard Widget
function chartbeat_widget_init() {
register_widget( 'Chartbeat_Widget' );
}
add_action('widgets_init', 'chartbeat_widget_init');
function chartbeat_get_display_url( $url ){
return strtok(preg_replace("/(https?:\/\/)?(www\.)?/i","",$url),"/");
}
function chartbeat_dashboard_widget_function() {
?>
<div id="chartbeatGauge"></div>
<div id="chartbeatRefsTable" class="chartbeatWidget">
<table id="chartbeatLinks" class="chartbeatTable">
<thead>
<tr>
<th colspan=2 class="chartbeatLabel">Top Referrers</th>
</tr>
</thead>
<tbody></tbody>
</table>
<table id="chartbeatSearch" class="chartbeatTable">
<thead>
<tr>
<th colspan=2 class="chartbeatLabel">Top Search</th>
</tr>
</thead>
<tbody></tbody>
</table>
<div class="clear"></div>
</div>
<div id="chartbeatGraph" class="chartbeatWidget clear">
<div class="chartbeatLabel">Visits - Last 3 Day</div>
<div id="chartbeatHist">
<div id="annotations"></div>
</div>
</div>
<script type="text/javascript">
<?php add_filter( 'posts_where', 'chartbeat_filter_where_last_three_days' ); ?>
var events = [];
// Get published post Events
<?php
$args = array( 'post_type' => array( 'post' ),'post_status' => 'publish', 'orderby' => 'date', 'order' => 'ASC' );
$the_query = new WP_Query( $args );
$domain = apply_filters( 'chartbeat_config_domain', chartbeat_get_display_url (get_option('home')) );
while ( $the_query->have_posts() ) : $the_query->the_post();
$tstamp = get_the_time('Y,n-1,j,G,i');
if ($tstamps[$tstamp])
continue;
$tstamps[$tstamp] = true;
$category = get_the_category();
if($category[0])
$category_link = get_category_link($category[0]->cat_ID );
?>var ev = {domain:'<?php echo esc_js( chartbeat_get_display_url( $domain ) );?>',title:'<?php echo esc_js( get_the_title() ); ?>',
value:'<?php echo esc_js( chartbeat_get_display_url( $category_link ) ); ?>', group_name:'<?php echo esc_js( chartbeat_get_display_url( get_page_link() ) ); ?>',
t: new Date(<?php echo the_time('Y,n-1,j,G,i'); ?>).getTime()/1000,group_type:'page',num_referrers:10,id:'<?php echo esc_js( get_the_ID() ); ?>',type:'wp',data:{action_type:"create"}};
events.push(ev);
<?php
endwhile;
wp_reset_postdata();?>
// Get revisions
<?php
$args = array( 'post_type' => array( 'revision' ), 'post_status' => 'inherit', 'orderby' => 'date', 'order' => 'ASC' );
$the_query = new WP_Query( $args );
while( $the_query->have_posts() ) : $the_query->the_post();
$tstamp = get_the_time('Y,n-1,j,G,i');
if ($tstamps[$tstamp])
continue;
$tstamps[$tstamp] = true;
$category = get_the_category();
if($category[0])
$category_link = get_category_link($category[0]->cat_ID );
?>var ev = {domain:'<?php echo esc_js( chartbeat_get_display_url( $domain ) ); ?>',title:'<?php echo esc_js( get_the_title() ); ?>',
value:'<?php echo esc_js( chartbeat_get_display_url( $category_link ) ); ?>',group_name:'<?php echo esc_js( chartbeat_get_display_url( get_page_link() ) ); ?>',
t: new Date(<?php echo the_time('Y,n-1,j,G,i'); ?>).getTime()/1000,group_type:'page',num_referrers:10,id:'<?php echo esc_js( get_the_ID() ); ?>',type:'wp',data:{action_type:"update"}};
events.push(ev);
<?php
endwhile;
wp_reset_postdata();
remove_filter( 'posts_where', 'chartbeat_filter_where_last_three_days' );
?>
function loadChartBeatWidgets(){
new CBDashboard('chartbeatGauge','chartbeatRefsTable','chartbeatHist',200,"<?php echo esc_js( chartbeat_get_display_url( $domain ) ); ?>","<?php echo esc_js( get_option('chartbeat_apikey') ); ?>",events);
};
var currOnload = window.onload;
window.onload = (typeof window.onload != 'function') ? loadChartBeatWidgets : function() { oldonload(); loadChartBeatWidgets(); };
</script>
<?php
}
// Create a new filtering function that will add our where clause to the query
function chartbeat_filter_where_last_three_days( $where = '' ) {
global $wpdb;
$where .= $wpdb->prepare( " AND $wpdb->posts.post_modified > %s", date( 'Y-m-d', strtotime( '-3 days' ) ) );
return $where;
}
function chartbeat_add_dashboard_widgets() {
// Don't add widgets if we haven't set up Chartbeat yet
if ( ! get_option('chartbeat_userid') )
return;
wp_enqueue_style( 'cbplugin_css' );
wp_enqueue_script( 'cbdashboard' );
wp_add_dashboard_widget('chartbeat_dashboard_widget', 'Chartbeat', 'chartbeat_dashboard_widget_function');
}
function chartbeat_plugin_admin_init() {
wp_register_style('cbplugin_css',plugins_url('media/cb_plugin.css', __FILE__));
wp_register_script( 'cbdashboard',plugins_url('media/cbdashboard.compiled.js', __FILE__));
}
add_action('wp_dashboard_setup', 'chartbeat_add_dashboard_widgets' );
add_action( 'admin_init', 'chartbeat_plugin_admin_init' );
// Add Column to Posts Manager
add_filter('manage_posts_columns', 'chartbeat_columns');
function chartbeat_columns($defaults) {
if ( ! get_option('chartbeat_userid') )
return $defaults;
$defaults['cb_visits'] = __('Active Visits');
return $defaults;
}
add_action('manage_posts_custom_column', 'chartbeat_custom_columns', 10, 2);
function chartbeat_custom_columns($column_name, $id) {
$domain = apply_filters( 'chartbeat_config_domain', chartbeat_get_display_url (get_option('home')) );
if( $column_name == 'cb_visits' ) {
$post_url = parse_url(get_permalink( $id ));
$json_url = add_query_arg( array(
'host' => chartbeat_get_display_url( $domain ),
'apikey' => get_option('chartbeat_apikey'),
'path' => urlencode( $post_url["path"] ),
), '//api.chartbeat.com/live/quickstats/' );
?>
<script type="text/javascript">
jQuery.getJSON('<?php echo esc_js( $json_url ); ?>',
function(data) {
if ( !data.visits ) data.visits = 0;
jQuery('#post-<?php echo absint( $id ); ?> .cb_visits').append(data.visits);
}
);
</script>
<?php
}
}
// Returns URL for asset on the static.chartbeat domain
function chartbeat_get_static_asset_url( $path = '' ) {
$domain = (is_ssl() ? 'https' : 'http') . '://static.chartbeat.com/';
return $domain . ltrim( $path, '/' );
}
// If admin register settings on page that have been saved
// if not, add content to wp_head and wp_footer.
if ( is_admin() ){
add_action( 'admin_init', 'chartbeat_register_settings' );
}else {
add_action('wp_head', 'add_chartbeat_head');
add_action('wp_footer', 'add_chartbeat_footer');
}
?>