widgets.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. /**
  3. * Author: ExactMetrics team
  4. * Author URI: https://exactmetrics.com
  5. * Copyright 2018 ExactMetrics team
  6. * License: GPLv2 or later
  7. * License URI: http://www.gnu.org/licenses/gpl-2.0.html
  8. */
  9. // Exit if accessed directly
  10. if ( ! defined( 'ABSPATH' ) )
  11. exit();
  12. final class GADWP_Frontend_Widget extends WP_Widget {
  13. private $gadwp;
  14. public function __construct() {
  15. $this->gadwp = GADWP();
  16. parent::__construct( 'gadwp-frontwidget-report', __( 'Google Analytics Dashboard', 'google-analytics-dashboard-for-wp' ), array( 'description' => __( "Will display your google analytics stats in a widget", 'google-analytics-dashboard-for-wp' ) ) );
  17. // Frontend Styles
  18. if ( is_active_widget( false, false, $this->id_base, true ) ) {
  19. add_action( 'wp_enqueue_scripts', array( $this, 'load_styles_scripts' ) );
  20. }
  21. }
  22. public function load_styles_scripts() {
  23. $lang = get_bloginfo( 'language' );
  24. $lang = explode( '-', $lang );
  25. $lang = $lang[0];
  26. wp_enqueue_style( 'gadwp-front-widget', GADWP_URL . 'front/css/widgets.css', null, GADWP_CURRENT_VERSION );
  27. wp_enqueue_script( 'gadwp-front-widget', GADWP_URL . 'front/js/widgets.js', array( 'jquery' ), GADWP_CURRENT_VERSION );
  28. wp_enqueue_script( 'googlecharts', 'https://www.gstatic.com/charts/loader.js', array(), null );
  29. }
  30. public function widget( $args, $instance ) {
  31. $widget_title = apply_filters( 'widget_title', $instance['title'] );
  32. $title = __( "Sessions", 'google-analytics-dashboard-for-wp' );
  33. echo "\n<!-- BEGIN GADWP v" . GADWP_CURRENT_VERSION . " Widget - https://exactmetrics.com/ -->\n";
  34. echo $args['before_widget'];
  35. if ( ! empty( $widget_title ) ) {
  36. echo $args['before_title'] . $widget_title . $args['after_title'];
  37. }
  38. if ( isset( $this->gadwp->config->options['theme_color'] ) ) {
  39. $css = "colors:['" . $this->gadwp->config->options['theme_color'] . "','" . GADWP_Tools::colourVariator( $this->gadwp->config->options['theme_color'], - 20 ) . "'],";
  40. $color = $this->gadwp->config->options['theme_color'];
  41. } else {
  42. $css = "";
  43. $color = "#3366CC";
  44. }
  45. ob_start();
  46. if ( $instance['anonim'] ) {
  47. $formater = "var formatter = new google.visualization.NumberFormat({
  48. suffix: '%',
  49. fractionDigits: 2
  50. });
  51. formatter.format(data, 1);";
  52. } else {
  53. $formater = '';
  54. }
  55. $periodtext = "";
  56. switch ( $instance['period'] ) {
  57. case '7daysAgo' :
  58. $periodtext = sprintf( __( 'Last %d Days', 'google-analytics-dashboard-for-wp' ), 7 );
  59. break;
  60. case '14daysAgo' :
  61. $periodtext = sprintf( __( 'Last %d Days', 'google-analytics-dashboard-for-wp' ), 14 );
  62. break;
  63. case '30daysAgo' :
  64. $periodtext = sprintf( __( 'Last %d Days', 'google-analytics-dashboard-for-wp' ), 30 );
  65. break;
  66. default :
  67. $periodtext = "";
  68. break;
  69. }
  70. switch ( $instance['display'] ) {
  71. case '1' :
  72. echo '<div id="gadwp-widget"><div id="gadwp-widgetchart"></div><div id="gadwp-widgettotals"></div></div>';
  73. break;
  74. case '2' :
  75. echo '<div id="gadwp-widget"><div id="gadwp-widgetchart"></div></div>';
  76. break;
  77. case '3' :
  78. echo '<div id="gadwp-widget"><div id="gadwp-widgettotals"></div></div>';
  79. break;
  80. }
  81. ?>
  82. <script type="text/javascript">
  83. google.charts.load('current', {'packages':['corechart']});
  84. google.charts.setOnLoadCallback( GADWPWidgetLoad );
  85. function GADWPWidgetLoad (){
  86. jQuery.post("<?php echo admin_url( 'admin-ajax.php' ); ?>", {action: "ajax_frontwidget_report", gadwp_number: "<?php echo $this->number; ?>", gadwp_optionname: "<?php echo $this->option_name; ?>" }, function(response){
  87. if (!jQuery.isNumeric(response) && jQuery.isArray(response)){
  88. if (jQuery("#gadwp-widgetchart")[0]){
  89. gadwpFrontWidgetData = response[0];
  90. gadwp_drawFrontWidgetChart(gadwpFrontWidgetData);
  91. }
  92. if (jQuery("#gadwp-widgettotals")[0]){
  93. gadwp_drawFrontWidgetTotals(response[1]);
  94. }
  95. }else{
  96. jQuery("#gadwp-widgetchart").css({"background-color":"#F7F7F7","height":"auto","padding-top":"50px","padding-bottom":"50px","color":"#000","text-align":"center"});
  97. jQuery("#gadwp-widgetchart").html("<?php __( "This report is unavailable", 'google-analytics-dashboard-for-wp' ); ?> ("+response+")");
  98. }
  99. });
  100. }
  101. function gadwp_drawFrontWidgetChart(response) {
  102. var data = google.visualization.arrayToDataTable(response);
  103. var options = {
  104. legend: { position: "none" },
  105. pointSize: "3",
  106. <?php echo $css; ?>
  107. title: "<?php echo $title; ?>",
  108. titlePosition: "in",
  109. chartArea: { width: "95%", height: "75%" },
  110. hAxis: { textPosition: "none"},
  111. vAxis: { textPosition: "none", minValue: 0, gridlines: { color: "transparent" }, baselineColor: "transparent"}
  112. }
  113. var chart = new google.visualization.AreaChart(document.getElementById("gadwp-widgetchart"));
  114. <?php echo $formater; ?>
  115. chart.draw(data, options);
  116. }
  117. function gadwp_drawFrontWidgetTotals(response) {
  118. if ( null == response ){
  119. response = 0;
  120. }
  121. jQuery("#gadwp-widgettotals").html('<div class="gadwp-left"><?php _e( "Period:", 'google-analytics-dashboard-for-wp' ); ?></div> <div class="gadwp-right"><?php echo $periodtext; ?> </div><div class="gadwp-left"><?php _e( "Sessions:", 'google-analytics-dashboard-for-wp' ); ?></div> <div class="gadwp-right">'+response+'</div>');
  122. }
  123. </script>
  124. <?php
  125. if ( 1 == $instance['give_credits'] ) :
  126. ?>
  127. <div style="text-align: right; width: 100%; font-size: 0.8em; clear: both; margin-right: 5px;"><?php _e( 'generated by', 'google-analytics-dashboard-for-wp' ); ?> <a href="https://exactmetrics.com/?utm_source=gadwp_report&utm_medium=link&utm_content=front_widget&utm_campaign=gadwp" rel="nofollow" style="text-decoration: none; font-size: 1em;">GADWP</a>&nbsp;
  128. </div>
  129. <?php
  130. endif;
  131. $widget_content = ob_get_contents();
  132. if ( ob_get_length() ) {
  133. ob_end_clean();
  134. }
  135. echo $widget_content;
  136. echo $args['after_widget'];
  137. echo "\n<!-- END GADWP Widget -->\n";
  138. }
  139. public function form( $instance ) {
  140. $widget_title = ( isset( $instance['title'] ) ? $instance['title'] : __( "Google Analytics Stats", 'google-analytics-dashboard-for-wp' ) );
  141. $period = ( isset( $instance['period'] ) ? $instance['period'] : '7daysAgo' );
  142. $display = ( isset( $instance['display'] ) ? $instance['display'] : 1 );
  143. $give_credits = ( isset( $instance['give_credits'] ) ? $instance['give_credits'] : 1 );
  144. $anonim = ( isset( $instance['anonim'] ) ? $instance['anonim'] : 0 );
  145. /* @formatter:off */
  146. ?>
  147. <p>
  148. <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( "Title:",'google-analytics-dashboard-for-wp' ); ?></label> <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $widget_title ); ?>">
  149. </p>
  150. <p>
  151. <label for="<?php echo $this->get_field_id( 'display' ); ?>"><?php _e( "Display:",'google-analytics-dashboard-for-wp' ); ?></label> <select id="<?php echo $this->get_field_id('display'); ?>" class="widefat" name="<?php echo $this->get_field_name( 'display' ); ?>">
  152. <option value="1" <?php selected( $display, 1 ); ?>><?php _e('Chart & Totals', 'google-analytics-dashboard-for-wp');?></option>
  153. <option value="2" <?php selected( $display, 2 ); ?>><?php _e('Chart', 'google-analytics-dashboard-for-wp');?></option>
  154. <option value="3" <?php selected( $display, 3 ); ?>><?php _e('Totals', 'google-analytics-dashboard-for-wp');?></option>
  155. </select>
  156. </p>
  157. <p>
  158. <label for="<?php echo $this->get_field_id( 'anonim' ); ?>"><?php _e( "Anonymize stats:",'google-analytics-dashboard-for-wp' ); ?></label> <input class="widefat" id="<?php echo $this->get_field_id( 'anonim' ); ?>" name="<?php echo $this->get_field_name( 'anonim' ); ?>" type="checkbox" <?php checked( $anonim, 1 ); ?> value="1">
  159. </p>
  160. <p>
  161. <label for="<?php echo $this->get_field_id( 'period' ); ?>"><?php _e( "Stats for:",'google-analytics-dashboard-for-wp' ); ?></label> <select id="<?php echo $this->get_field_id('period'); ?>" class="widefat" name="<?php echo $this->get_field_name( 'period' ); ?>">
  162. <option value="7daysAgo" <?php selected( $period, '7daysAgo' ); ?>><?php printf( __('Last %d Days', 'google-analytics-dashboard-for-wp'), 7 );?></option>
  163. <option value="14daysAgo" <?php selected( $period, '14daysAgo' ); ?>><?php printf( __('Last %d Days', 'google-analytics-dashboard-for-wp'), 14 );?></option>
  164. <option value="30daysAgo" <?php selected( $period, '30daysAgo' ); ?>><?php printf( __('Last %d Days', 'google-analytics-dashboard-for-wp'), 30 );?></option>
  165. </select>
  166. </p>
  167. <p>
  168. <label for="<?php echo $this->get_field_id( 'give_credits' ); ?>"><?php _e( "Give credits:",'google-analytics-dashboard-for-wp' ); ?></label> <input class="widefat" id="<?php echo $this->get_field_id( 'give_credits' ); ?>" name="<?php echo $this->get_field_name( 'give_credits' ); ?>" type="checkbox" <?php checked( $give_credits, 1 ); ?> value="1">
  169. </p>
  170. <?php
  171. /* @formatter:on */
  172. }
  173. public function update( $new_instance, $old_instance ) {
  174. $instance = array();
  175. $instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : 'Analytics Stats';
  176. $instance['period'] = ( ! empty( $new_instance['period'] ) ) ? strip_tags( $new_instance['period'] ) : '7daysAgo';
  177. $instance['display'] = ( ! empty( $new_instance['display'] ) ) ? strip_tags( $new_instance['display'] ) : 1;
  178. $instance['give_credits'] = ( ! empty( $new_instance['give_credits'] ) ) ? strip_tags( $new_instance['give_credits'] ) : 0;
  179. $instance['anonim'] = ( ! empty( $new_instance['anonim'] ) ) ? strip_tags( $new_instance['anonim'] ) : 0;
  180. return $instance;
  181. }
  182. }