-
Notifications
You must be signed in to change notification settings - Fork 30
/
geodirectory_hooks_actions.php
3174 lines (2606 loc) · 121 KB
/
geodirectory_hooks_actions.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
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Hook and filter actions used by the plugin
*
* @since 1.0.0
* @package GeoDirectory
* @global array $geodir_addon_list List of active GeoDirectory extensions.
* @global string $plugin_file_name Plugin main file name. 'geodirectory/geodirectory.php'.
*/
/**
* Return the GeoDirectory ajax specific url.
*
* This is used to run GeoDirectory specific functions via ajax.
*
* @since 1.0.0
* @package GeoDirectory
* @return string The GeoDirectory ajax URL.
*/
function geodir_get_ajax_url()
{
return admin_url('admin-ajax.php?action=geodir_ajax_action');
}
/////////////////////
/* ON INIT ACTIONS */
/////////////////////
add_action('init', 'geodir_on_init', 1);
add_action('init', 'geodir_add_post_filters');
//add_action('init', 'geodir_init_defaults');
add_action('init', 'geodir_allow_post_type_frontend');
add_action('init', 'geodir_register_taxonomies', 1);
add_action('init', 'geodir_register_post_types', 2);
add_filter('geodir_post_type_args', 'geodir_post_type_args_modify', 0, 2);
//add_action( 'init', 'geodir_flush_rewrite_rules', 99 );
add_action('init', 'geodir_custom_post_status');
add_action('widgets_init', 'geodir_register_sidebar'); // Takes care of widgets
global $geodir_addon_list;
/**
* Build an array of installed addons.
*
* This filter builds an array of installed addons which can be used to check what exactly is installed.
*
* @since 1.0.0
* @package GeoDirectory
* @param array $geodir_addon_list The array of installed plugins $geodir_addon_list['geodir_location_manager'].
*/
apply_filters('geodir_build_addon_list', $geodir_addon_list);
add_action('wp_ajax_geodir_ajax_action', "geodir_ajax_handler");
add_action('wp_ajax_nopriv_geodir_ajax_action', 'geodir_ajax_handler');
/* Pluploader */
add_action('wp_ajax_plupload_action', "geodir_plupload_action");
add_action('wp_ajax_nopriv_plupload_action', 'geodir_plupload_action'); // call for not logged in ajax
////////////////////////
/* Widget Initialization */
////////////////////////
add_action('widgets_init', 'register_geodir_widgets');
////////////////////////
/* REWRITE RULES */
////////////////////////
add_filter('rewrite_rules_array', 'geodir_listing_rewrite_rules');
////////////////////////
/* QUERY VARS */
////////////////////////
add_filter('query_vars', 'geodir_add_location_var');
add_filter('query_vars', 'geodir_add_geodir_page_var');
add_action('wp', 'geodir_add_page_id_in_query_var'); // problem fix in wordpress 3.8
if (get_option('permalink_structure') != '')
add_filter('parse_request', 'geodir_set_location_var_in_session_in_core');
add_filter('parse_query', 'geodir_modified_query');
////////////////////////
/* ON WP LOAD ACTIONS */
////////////////////////
//add_action( 'wp_loaded','geodir_flush_rewrite_rules' );
add_action('wp_loaded', 'geodir_on_wp_loaded', 10);
add_action('wp', 'geodir_on_wp', 10);
/////////////////////////////
/* ON WP HEAD ACTIONS */
/////////////////////////////
add_action('wp_head', 'geodir_header_scripts');
// add_action('admin_head', 'geodir_header_scripts'); // Removed since 1.5.0
add_action('wp_head', 'geodir_init_map_jason'); // Related to MAP
add_action('wp_head', 'geodir_init_map_canvas_array'); // Related to MAP
add_action('wp_head', 'geodir_restrict_widget'); // Related to widgets
//////////////////////////////
/* ENQUEUE SCRIPTS AND STYLES */
//////////////////////////////
add_action('wp_enqueue_scripts', 'geodir_templates_scripts');
add_action('wp_enqueue_scripts', 'geodir_templates_styles', 8);
////////////////////////
/* ON MAIN NAVIGATION */
////////////////////////
add_filter('wp_nav_menu_items', 'geodir_menu_items', 100, 2);
add_filter('wp_page_menu', 'geodir_pagemenu_items', 100, 2);
/////////////////////////
/* ON TEMPLATE INCLUDE */
/////////////////////////
add_filter('template_include', 'geodir_template_loader',9);
/////////////////////////
/* CATEGORY / TAXONOMY / CUSTOM POST ACTIONS */
/////////////////////////
//add_action('edited_term','geodir_update_markers_oncatedit',10,3);
add_filter('term_link', 'geodir_get_term_link', 10, 3);
add_filter('post_type_archive_link', 'geodir_get_posttype_link', 10, 2);
add_filter('post_type_link', 'geodir_listing_permalink_structure', 10, 4);
////////////////////////
/* POST AND LOOP ACTIONS */
////////////////////////
if (!is_admin()) {
add_action('pre_get_posts', 'geodir_exclude_page', 100); /// Will help to exclude virtual page from everywhere
add_filter('wp_list_pages_excludes', 'exclude_from_wp_list_pages', 100);
/** Exclude Virtual Pages From Pages List **/
add_action('pre_get_posts', 'set_listing_request', 0);
add_action('pre_get_posts', 'geodir_listing_loop_filter', 1);
add_filter('excerpt_more', 'geodir_excerpt_more', 1000);
add_filter('excerpt_length', 'geodir_excerpt_length', 1000);
add_action('the_post', 'create_marker_jason_of_posts'); // Add marker in json array, Map related filter
}
add_action('set_object_terms', 'geodir_set_post_terms', 10, 4);
add_action('transition_post_status', 'geodir_update_poststatus', 10, 3);
add_action('before_delete_post', 'geodir_delete_listing_info', 10, 1);
////////////////////////
/* WP REVIEW COUNT ACTIONS */
////////////////////////
add_action('geodir_update_postrating', 'geodir_term_review_count_force_update_single_post', 100,1);
//add_action('geodir_update_postrating', 'geodir_term_review_count_force_update', 100);
add_action('transition_post_status', 'geodir_term_review_count_force_update', 100,3);
//add_action('created_term', 'geodir_term_review_count_force_update', 100);
add_action('edited_term', 'geodir_term_review_count_force_update', 100);
//add_action('delete_term', 'geodir_term_review_count_force_update', 100); /*causes memory timeout on delete bulk tags*/
////////////////////////
/* WP CAT META UPDATE ACTIONS */
////////////////////////
add_action('gd_tax_meta_updated', 'geodir_get_term_icon_rebuild', 5000);
////////////////////////
/* WP FOOTER ACTIONS */
////////////////////////
add_action('wp_footer', 'geodir_footer_scripts'); /* Footer Scripts loader */
add_action('wp_footer', 'send_marker_jason_to_js'); // Show map for listings with markers
add_action('admin_footer', 'geodir_localize_all_js_msg');
add_action('wp_footer', 'geodir_localize_all_js_msg');
add_action('admin_head-media-upload-popup', 'geodir_localize_all_js_msg');
add_action('customize_controls_print_footer_scripts', 'geodir_localize_all_js_msg');
add_action('wp_head', 'geodir_add_meta_keywords');
add_action('wp_head', 'geodir_google_analytics_tracking_code');
/* Sharelocation scripts */
//global $geodir_addon_list;
//if(!empty($geodir_addon_list) && array_key_exists('geodir_sharelocation_manager', $geodir_addon_list) && $geodir_addon_list['geodir_sharelocation_manager'] == 'yes') {
add_action('wp_footer', 'geodir_add_sharelocation_scripts');
//}
/**
* Save and update GeoDirectory navigation settings per theme.
*
* @since 1.0.0
* @package GeoDirectory
* @param string $newname The theme name.
* @ignore
*/
function geodir_unset_prev_theme_nav_location($newname)
{
$geodir_theme_location = get_option('geodir_theme_location_nav_' . $newname);
if ($geodir_theme_location) {
update_option('geodir_theme_location_nav', $geodir_theme_location);
} else {
update_option('geodir_theme_location_nav', '');
}
}
/// add action for theme switch to blank previous theme navigation location setting
add_action("switch_theme", "geodir_unset_prev_theme_nav_location", 10, 2);
/**
* Contains functions/hooks for setting up the CPT and taxonomies for the plugin.
*
* @since 1.0.0
*/
require_once('geodirectory-functions/custom_taxonomy_hooks_actions.php');
/**
* Includes the file that adds filters/functions to change the database queries.
*
* @since 1.0.0
* @package GeoDirectory
*/
function geodir_add_post_filters()
{
/**
* Contains all function for filtering listing.
*
* @since 1.0.0
* @package GeoDirectory
*/
include_once('geodirectory-functions/listing_filters.php');
// Theme My Login compatibility fix
if ( isset( $_REQUEST['geodir_search'] ) && class_exists( 'Theme_My_Login' ) ) {
remove_action( 'pre_get_posts', array( Theme_My_Login::get_object(), 'pre_get_posts' ) );
}
if ( isset( $_REQUEST['geodir_search'] ) ) {
add_filter( 'geodir_filter_widget_listings_fields', 'geodir_search_widget_location_filter_fields', 100, 3 );
add_filter( 'geodir_filter_widget_listings_orderby', 'geodir_search_widget_location_filter_orderby', 100, 3 );
}
}
if (!function_exists('geodir_init_defaults')) {
/**
* Calls the function to register the GeoDirectory default CPT and taxonomies.
*
* @since 1.0.0
* @package GeoDirectory
*/
function geodir_init_defaults()
{
if (function_exists('geodir_register_defaults')) {
geodir_register_defaults();
}
}
}
/* Sidebar */
add_action('geodir_sidebar', 'geodir_get_sidebar', 10);
/* Pagination in loop-store */
add_action('geodir_pagination', 'geodir_pagination', 10);
/** Add Custom Menu Items **/
/** Replaces "Post" in the update messages for custom post types on the "Edit" post screen. **/
add_filter('post_updated_messages', 'geodir_custom_update_messages');
// CALLED ON 'sidebars_widgets' FILTER
if (!function_exists('geodir_restrict_widget')) {
/**
* Sets global values to be able to tell if the current page is a GeoDirectory listing page or a GeoDirectory details page.
*
* @global bool $is_listing Sets the global value to true if on a GD category page. False if not.
* @global bool $is_single_place Sets the global value to true if on a GD details (post) page. False if not.
* @since 1.0.0
* @package GeoDirectory
*/
function geodir_restrict_widget()
{
global $is_listing, $is_single_place;
// set is listing
(geodir_is_page('listing')) ? $is_listing = true : $is_listing = false;
// set is single place
(geodir_is_page('place')) ? $is_single_place = true : $is_single_place = false;
}
}
/////// GEO DIRECOTORY CUSTOM HOOKS ///
add_action('geodir_before_tab_content', 'geodir_before_tab_content');// this function is in custom_functions.php and it is used to wrap detail page tab content
add_action('geodir_after_tab_content', 'geodir_after_tab_content');// this function is in custom_functions.php and it is used to wrap detail page tab content
// Detail page sidebar content
add_action('geodir_detail_page_sidebar', 'geodir_detail_page_sidebar_content_sorting', 1);
/**
* Builds an array of elements for the details (post) page sidebar.
*
* Builds an array of functions to be called in the details page (post) sidebar, this array can be changed via hook or filter.
*
* @since 1.0.0
* @package GeoDirectory
*/
function geodir_detail_page_sidebar_content_sorting()
{
$arr_detail_page_sidebar_content =
/**
* An array of functions to be called to be displayed on the details (post) page sidebar.
*
* This filter can be used to remove sections of the details page sidebar,
* add new sections or rearrange the order of the sections.
*
* @param array array('geodir_social_sharing_buttons','geodir_share_this_button','geodir_detail_page_google_analytics','geodir_edit_post_link','geodir_detail_page_review_rating','geodir_detail_page_more_info') The array of functions that will be called.
* @since 1.0.0
*/
apply_filters('geodir_detail_page_sidebar_content',
array('geodir_social_sharing_buttons',
'geodir_detail_page_google_analytics',
'geodir_edit_post_link',
'geodir_detail_page_review_rating',
'geodir_detail_page_more_info'
) // end of array
); // end of apply filter
if (!empty($arr_detail_page_sidebar_content)) {
foreach ($arr_detail_page_sidebar_content as $content_function) {
if (function_exists($content_function)) {
add_action('geodir_detail_page_sidebar', $content_function);
}
}
}
}
add_action('geodir_after_edit_post_link', 'geodir_add_to_favourite_link', 1);
/**
* Outputs the add to favourite line for the current post if not add listing preview page.
*
* @global object $post The current post object.
* @global bool $preview True if the current page is add listing preview page. False if not.
* since 1.0.0
* @package GeoDirectory
*/
function geodir_add_to_favourite_link()
{
global $post, $preview;
if (!$preview && geodir_is_page('detail')) {
?>
<p class="edit_link">
<?php geodir_favourite_html($post->post_author, $post->ID); ?>
</p>
<?php
}
}
/**
* Outputs social buttons.
*
* Outputs social sharing buttons twitter,facebook and google plus into a containing div if not on the add listing preview page.
*
* @global bool $preview True if the current page is add listing preview page. False if not.
* @since 1.0.0
* @package GeoDirectory
*/
function geodir_social_sharing_buttons()
{
global $preview;
ob_start(); // Start buffering;
/**
* This action is called before the social buttons twitter,facebook and google plus are output in a containing div.
*
* @since 1.0.0
*/
do_action('geodir_before_social_sharing_buttons');
if (!$preview) {
?>
<div class="likethis">
<?php geodir_twitter_tweet_button(); ?>
<?php geodir_fb_like_button(); ?>
<?php geodir_google_plus_button(); ?>
</div>
<?php
}// end of if, if its a preview or not
/**
* This action is called after the social buttons twitter,facebook and google plus are output in a containing div.
*
* @since 1.0.0
*/
do_action('geodir_after_social_sharing_buttons');
$content_html = ob_get_clean();
if (trim($content_html) != '')
$content_html = '<div class="geodir-company_info geodir-details-sidebar-social-sharing">' . $content_html . '</div>';
if ((int)get_option('geodir_disable_tfg_buttons_section') != 1) {
/**
* Filter the geodir_social_sharing_buttons() function content.
*
* @param string $content_html The output html of the geodir_social_sharing_buttons() function.
*/
echo $content_html = apply_filters('geodir_social_sharing_buttons_html', $content_html);
}
}
/**
* Outputs the edit post link.
*
* Outputs the edit post link if the current logged in user owns the post.
*
* @global bool $preview True if the current page is add listing preview page. False if not.
* @global WP_Post|null $post The current post, if available.
* @since 1.0.0
* @package GeoDirectory
*/
function geodir_edit_post_link()
{
global $post, $preview;
ob_start(); // Start buffering;
/**
* This is called before the edit post link html in the function geodir_edit_post_link()
*
* @since 1.0.0
*/
do_action('geodir_before_edit_post_link');
if (!$preview) {
$is_current_user_owner = geodir_listing_belong_to_current_user();
if ($is_current_user_owner) {
$post_id = $post->ID;
if (isset($_REQUEST['pid']) && $_REQUEST['pid'] != '') {
$post_id = (int)$_REQUEST['pid'];
}
$postlink = get_permalink(geodir_add_listing_page_id());
$editlink = geodir_getlink($postlink, array('pid' => $post_id), false);
echo ' <p class="edit_link"><i class="fas fa-pencil-alt"></i> <a href="' . esc_url($editlink) . '">' . __('Edit this Post', 'geodirectory') . '</a></p>';
}
}// end of if, if its a preview or not
/**
* This is called after the edit post link html in the function geodir_edit_post_link()
*
* @since 1.0.0
*/
do_action('geodir_after_edit_post_link');
$content_html = ob_get_clean();
if (trim($content_html) != '')
$content_html = '<div class="geodir-company_info geodir-details-sidebar-user-links">' . $content_html . '</div>';
if ((int)get_option('geodir_disable_user_links_section') != 1) {
/**
* Filter the geodir_edit_post_link() function content.
*
* @param string $content_html The output html of the geodir_edit_post_link() function.
*/
echo $content_html = apply_filters('geodir_edit_post_link_html', $content_html);
}
}
/**
* Outputs the google analytics section on details page.
*
* Outputs the google analytics html if the current logged in user owns the post.
*
* @global WP_Post|null $post The current post, if available.
* @since 1.0.0
* @package GeoDirectory
*/
function geodir_detail_page_google_analytics()
{
if ( ! get_option( 'geodir_ga_stats' ) ) {
return;
}
global $post,$preview;
if($preview){return '';}
$package_info = array();
$package_info = geodir_post_package_info($package_info, $post);
$id = trim(get_option('geodir_ga_account_id'));
if (!$id) {
return; //if no Google Analytics ID then bail.
}
ob_start(); // Start buffering;
/**
* This is called before the edit post link html in the function geodir_detail_page_google_analytics()
*
* @since 1.0.0
*/
do_action('geodir_before_google_analytics');
$refresh_time = get_option('geodir_ga_refresh_time', 5);
/**
* Filter the time interval to check & refresh new users results.
*
* @since 1.5.9
*
* @param int $refresh_time Time interval to check & refresh new users results.
*/
$refresh_time = apply_filters('geodir_google_analytics_refresh_time', $refresh_time);
$refresh_time = absint($refresh_time * 1000);
$hide_refresh = get_option('geodir_ga_auto_refresh');
$auto_refresh = $hide_refresh && $refresh_time && $refresh_time > 0 ? 1 : 0;
if (is_user_logged_in() && (isset($package_info->google_analytics) && $package_info->google_analytics == '1') && (get_current_user_id()==$post->post_author || current_user_can( 'manage_options' )) ) {
$page_url = urlencode($_SERVER['REQUEST_URI']);
?>
<script type="text/javascript">
var gd_gaTimeOut;
var gd_gaTime = parseInt('<?php echo $refresh_time;?>');
var gd_gaHideRefresh = <?php echo (int)$hide_refresh;?>;
var gd_gaAutoRefresh = <?php echo $auto_refresh;?>;
ga_data1 = false;
ga_data2 = false;
ga_data3 = false;
ga_data4 = false;
ga_data5 = false;
ga_data6 = false;
ga_au = 0;
jQuery(document).ready(function() {
// Set some global Chart.js defaults.
Chart.defaults.global.animationSteps = 60;
Chart.defaults.global.animationEasing = 'easeInOutQuart';
Chart.defaults.global.responsive = true;
Chart.defaults.global.maintainAspectRatio = false;
jQuery('.gdga-show-analytics').click(function(e){
jQuery(this).hide();
jQuery('.gdga-analytics-box').show();
gdga_weekVSweek();
gdga_realtime(true);
});
if (gd_gaAutoRefresh !== 1) {
jQuery('.fa#gdga-loader-icon').click(function(e){
gdga_refresh();
clearTimeout(gd_gaTimeOut);
gdga_realtime();
});
}
});
function gdga_weekVSweek() {
jQuery.ajax({url: "<?php echo admin_url('admin-ajax.php?action=gdga&ga_page='.$page_url.'&ga_type=thisweek'); ?>", success: function(result){
ga_data1 = jQuery.parseJSON(result);
if(ga_data1.error){jQuery('#ga_stats').html(result);return;}
gd_renderWeekOverWeekChart();
}});
jQuery.ajax({url: "<?php echo admin_url('admin-ajax.php?action=gdga&ga_page='.$page_url.'&ga_type=lastweek'); ?>", success: function(result){
ga_data2 = jQuery.parseJSON(result);
gd_renderWeekOverWeekChart();
}});
}
function gdga_yearVSyear() {
jQuery.ajax({url: "<?php echo admin_url('admin-ajax.php?action=gdga&ga_page='.$page_url.'&ga_type=thisyear'); ?>", success: function(result){
ga_data3 = jQuery.parseJSON(result);
if(ga_data3.error){jQuery('#ga_stats').html(result);return;}
gd_renderYearOverYearChart()
}});
jQuery.ajax({url: "<?php echo admin_url('admin-ajax.php?action=gdga&ga_page='.$page_url.'&ga_type=lastyear'); ?>", success: function(result){
ga_data4 = jQuery.parseJSON(result);
gd_renderYearOverYearChart()
}});
}
function gdga_country() {
jQuery.ajax({url: "<?php echo admin_url('admin-ajax.php?action=gdga&ga_page='.$page_url.'&ga_type=country'); ?>", success: function(result){
ga_data5 = jQuery.parseJSON(result);
if(ga_data5.error){jQuery('#ga_stats').html(result);return;}
gd_renderTopCountriesChart();
}});
}
function gdga_realtime(dom_ready) {
jQuery.ajax({url: "<?php echo admin_url('admin-ajax.php?action=gdga&ga_page='.$page_url.'&ga_type=realtime'); ?>", success: function(result) {
ga_data6 = jQuery.parseJSON(result);
if (ga_data6.error) {
jQuery('#ga_stats').html(result);
return;
}
gd_renderRealTime(dom_ready);
}});
}
function gd_renderRealTime(dom_ready) {
if (typeof dom_ready === 'undefined') {
gdga_refresh(true);
}
ga_au_old = ga_au;
ga_au = ga_data6.totalsForAllResults["rt:activeUsers"];
if (ga_au>ga_au_old) {
jQuery('.gd-ActiveUsers').addClass( "is-increasing");
}
if (ga_au<ga_au_old) {
jQuery('.gd-ActiveUsers').addClass( "is-decreasing");
}
jQuery('.gd-ActiveUsers-value').html(ga_au);
if (gd_gaTime > 0 && gd_gaAutoRefresh === 1) {
// check for new users every 5 seconds
gd_gaTimeOut = setTimeout(function() {
jQuery('.gd-ActiveUsers').removeClass("is-increasing is-decreasing");
gdga_realtime();
}, gd_gaTime);
}
}
/**
* Draw the a chart.js doughnut chart with data from the specified view that
* compares sessions from mobile, desktop, and tablet over the past two
* weeks.
*/
function gd_renderTopCountriesChart() {
if (ga_data5) {
response = ga_data5;
ga_data5 = false;
} else {
return;
}
jQuery('#gdga-chart-container').show();
jQuery('#gdga-legend-container').show();
gdga_refresh(true);
jQuery('#gdga-select-analytic').show();
var data = [];
var colors = ['#4D5360','#949FB1','#D4CCC5','#E2EAE9','#F7464A'];
if (response.rows) {
response.rows.forEach(function (row, i) {
data.push({
label: row[0],
value: +row[1],
color: colors[i]
});
});
new Chart(makeCanvas('gdga-chart-container')).Doughnut(data);
generateLegend('gdga-legend-container', data);
} else {
gdga_noResults();
}
}
function gdga_noResults() {
jQuery('#gdga-chart-container').html('<?php _e('No results available','geodirectory');?>');
jQuery('#gdga-legend-container').html('');
}
/**
* Draw the a chart.js bar chart with data from the specified view that
* overlays session data for the current year over session data for the
* previous year, grouped by month.
*/
function gd_renderYearOverYearChart() {
if (ga_data3 && ga_data4) {
thisYear = ga_data3;
lastYear = ga_data4;
ga_data3 = false;
ga_data4 = false;
} else {
return;
}
jQuery('#gdga-chart-container').show();
jQuery('#gdga-legend-container').show();
gdga_refresh(true);
jQuery('#gdga-select-analytic').show();
// Adjust `now` to experiment with different days, for testing only...
var now = moment(); // .subtract(3, 'day');
Promise.all([thisYear, lastYear]).then(function(results) {
var data1 = results[0].rows.map(function(row) { return +row[2]; });
var data2 = results[1].rows.map(function(row) { return +row[2]; });
//var labelsN = results[0].rows.map(function(row) { return +row[1]; });
var labels = ['<?php _e('Jan', 'geodirectory');?>',
'<?php _e('Feb', 'geodirectory');?>',
'<?php _e('Mar', 'geodirectory');?>',
'<?php _e('Apr', 'geodirectory');?>',
'<?php _e('May', 'geodirectory');?>',
'<?php _e('Jun', 'geodirectory');?>',
'<?php _e('Jul', 'geodirectory');?>',
'<?php _e('Aug', 'geodirectory');?>',
'<?php _e('Sep', 'geodirectory');?>',
'<?php _e('Oct', 'geodirectory');?>',
'<?php _e('Nov', 'geodirectory');?>',
'<?php _e('Dec', 'geodirectory');?>'];
// Ensure the data arrays are at least as long as the labels array.
// Chart.js bar charts don't (yet) accept sparse datasets.
for (var i = 0, len = labels.length; i < len; i++) {
if (data1[i] === undefined) data1[i] = null;
if (data2[i] === undefined) data2[i] = null;
}
var data = {
labels : labels,
datasets : [
{
label: '<?php _e('Last Year', 'geodirectory');?>',
fillColor : "rgba(220,220,220,0.5)",
strokeColor : "rgba(220,220,220,1)",
data : data2
},
{
label: '<?php _e('This Year', 'geodirectory');?>',
fillColor : "rgba(151,187,205,0.5)",
strokeColor : "rgba(151,187,205,1)",
data : data1
}
]
};
new Chart(makeCanvas('gdga-chart-container')).Bar(data);
generateLegend('gdga-legend-container', data.datasets);
}).catch(function(err) {
console.error(err.stack);
})
}
/**
* Draw the a chart.js line chart with data from the specified view that
* overlays session data for the current week over session data for the
* previous week.
*/
function gd_renderWeekOverWeekChart() {
if(ga_data1 && ga_data2){
thisWeek = ga_data1;
lastWeek = ga_data2;
ga_data1 = false;
ga_data2 = false;
}else{
return;
}
jQuery('#gdga-chart-container').show();
jQuery('#gdga-legend-container').show();
gdga_refresh(true);
jQuery('#gdga-select-analytic').show();
// Adjust `now` to experiment with different days, for testing only...
var now = moment();
Promise.all([thisWeek, lastWeek]).then(function(results) {
var data1 = results[0].rows.map(function(row) { return +row[2]; });
var data2 = results[1].rows.map(function(row) { return +row[2]; });
var labels = results[1].rows.map(function(row) { return +row[0]; });
<?php
// Here we list the shorthand days of the week so it can be used in translation.
__("Mon",'geodirectory');
__("Tue",'geodirectory');
__("Wed",'geodirectory');
__("Thu",'geodirectory');
__("Fri",'geodirectory');
__("Sat",'geodirectory');
__("Sun",'geodirectory');
?>
labels = [
"<?php _e(date('D', strtotime("+1 day")),'geodirectory'); ?>",
"<?php _e(date('D', strtotime("+2 day")),'geodirectory'); ?>",
"<?php _e(date('D', strtotime("+3 day")),'geodirectory'); ?>",
"<?php _e(date('D', strtotime("+4 day")),'geodirectory'); ?>",
"<?php _e(date('D', strtotime("+5 day")),'geodirectory'); ?>",
"<?php _e(date('D', strtotime("+6 day")),'geodirectory'); ?>",
"<?php _e(date('D', strtotime("+7 day")),'geodirectory'); ?>"
];
var data = {
labels : labels,
datasets : [
{
label: '<?php _e('Last Week', 'geodirectory');?>',
fillColor : "rgba(220,220,220,0.5)",
strokeColor : "rgba(220,220,220,1)",
pointColor : "rgba(220,220,220,1)",
pointStrokeColor : "#fff",
data : data2
},
{
label: '<?php _e('This Week', 'geodirectory');?>',
fillColor : "rgba(151,187,205,0.5)",
strokeColor : "rgba(151,187,205,1)",
pointColor : "rgba(151,187,205,1)",
pointStrokeColor : "#fff",
data : data1
}
]
};
new Chart(makeCanvas('gdga-chart-container')).Line(data);
generateLegend('gdga-legend-container', data.datasets);
});
}
/**
* Create a new canvas inside the specified element. Set it to be the width
* and height of its container.
* @param {string} id The id attribute of the element to host the canvas.
* @return {RenderingContext} The 2D canvas context.
*/
function makeCanvas(id) {
var container = document.getElementById(id);
var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
container.innerHTML = '';
canvas.width = container.offsetWidth;
canvas.height = container.offsetHeight;
container.appendChild(canvas);
return ctx;
}
/**
* Create a visual legend inside the specified element based off of a
* Chart.js dataset.
* @param {string} id The id attribute of the element to host the legend.
* @param {Array.<Object>} items A list of labels and colors for the legend.
*/
function generateLegend(id, items) {
var legend = document.getElementById(id);
legend.innerHTML = items.map(function(item) {
var color = item.color || item.fillColor;
var label = item.label;
return '<li><i style="background:' + color + '"></i>' + label + '</li>';
}).join('');
}
function gdga_select_option() {
jQuery('#gdga-select-analytic').hide();
gdga_refresh();
gaType = jQuery('#gdga-select-analytic').val();
if (gaType=='weeks') {
gdga_weekVSweek();
} else if (gaType=='years') {
gdga_yearVSyear();
} else if(gaType=='country') {
gdga_country();
}
}
function gdga_refresh(stop) {
if (typeof stop !== 'undefined' && stop) {
if (gd_gaAutoRefresh === 1 || gd_gaHideRefresh == 1) {
jQuery('#gdga-loader-icon').hide();
} else {
jQuery('#gdga-loader-icon').removeClass('fa-spin');
}
} else {
if (gd_gaAutoRefresh === 1 || gd_gaHideRefresh == 1) {
jQuery('#gdga-loader-icon').show();
} else {
if (!jQuery('#gdga-loader-icon').hasClass('fa-spin')) {
jQuery('#gdga-loader-icon').addClass('fa-spin');
}
}
}
}
</script>
<style>
.geodir-details-sidebar-google-analytics {
min-height: 60px;
}
#ga_stats #gd-active-users-container {
float: right;
margin: 0 0 10px;
}
#gdga-select-analytic {
clear: both;
}
#ga_stats #ga-analytics-title{
float: left;
font-weight: bold;
}
#ga_stats #gd-active-users-container{
float: right;
}
.Chartjs {
font-size: .85em
}
.Chartjs-figure {
height: 200px;
width: 100%;
display: none;
}
.Chartjs-legend {
list-style: none;
margin: 0;
padding: 1em 0 0;
text-align: center;
width: 100%;
display: none;
}
.Chartjs-legend>li {
display: inline-block;
padding: .25em .5em
}
.Chartjs-legend>li>svg {
display: inline-block;
height: 1em;
margin-right: .5em;
vertical-align: -.1em;
width: 1em
}
@media (min-width: 570px) {
.Chartjs-figure {
margin-right:1.5em
}
}
.gd-ActiveUsers {
background: #f3f2f0;
border: 1px solid #d4d2d0;
border-radius: 4px;
font-weight: 300;
padding: .5em 1.5em;
white-space: nowrap
}
.gd-ActiveUsers-value {
display: inline-block;
font-weight: 600;
margin-right: -.25em
}
.gd-ActiveUsers.is-increasing {
-webkit-animation: increase 3s;
animation: increase 3s
}
.gd-ActiveUsers.is-decreasing {
-webkit-animation: decrease 3s;
animation: decrease 3s
}