PluginProbe
Analytify – Google Analytics Dashboard For WordPress (GA4 analytics tracking) / trunk
Analytify – Google Analytics Dashboard For WordPress (GA4 analytics tracking) vtrunk
9.1.2 9.1.1 9.1.0 9.0.2 9.0.1 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.1.0 1.1.1 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.3.0 1.3.1 1.3.2 All 153 releases
wp-analytify / inc / analytify-widgets.php

analytify-widgets.php in Analytify – Google Analytics Dashboard For WordPress (GA4 analytics tracking) trunk, at inc/analytify-widgets.php

156 lines 4.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName -- File naming is acceptable
2 /**
3 * Analytify Real-Time Widget
4 *
5 * @package WP_Analytify
6 */
7
8 /**
9 * Analytify Real-Time Widget Class
10 *
11 * @extends WP_Widget<array<string, mixed>>
12 */
13 class ANALYTIFY_WIDGET_REALTIME extends WP_Widget {
14
15 /**
16 * Constructor
17 *
18 * @return void
19 */
20 public function __construct() {
21
22 parent::__construct(
23 'analytify_live_stats', // Base ID.
24 esc_html_e( 'Analytify Live Stats', 'wp-analytify' ), // Name.
25 array( 'description' => esc_html_e( 'It shows Live Stats of your site.', 'wp-analytify' ) ) // Args.
26 );
27 }
28
29 /**
30 * Widget form creation
31 *
32 * @param array<string, mixed> $instance Widget instance.
33 * @return void
34 */
35 public function form( $instance ) {
36
37 // Check values.
38 if ( $instance ) {
39 $title = esc_attr( $instance['title'] );
40 $description = esc_textarea( $instance['description'] );
41 } else {
42 $title = '';
43 $description = '';
44 }
45 ?>
46
47 <p>
48 <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title', 'wp-analytify' ); ?></label>
49 <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
50 </p>
51
52 <p>
53 <label for="<?php echo esc_attr( $this->get_field_id( 'description' ) ); ?>"><?php esc_html_e( 'Description:', 'wp-analytify' ); ?></label>
54 <textarea class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'description' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'description' ) ); ?>"><?php echo esc_textarea( $description ); ?></textarea>
55 </p>
56
57 <?php
58 }
59
60 /**
61 * Update widget
62 *
63 * @param array<string, mixed> $new_instance New instance.
64 * @param array<string, mixed> $old_instance Old instance.
65 * @return array<string, mixed>
66 */
67 public function update( $new_instance, $old_instance ) {
68
69 $instance = $old_instance;
70 // Fields.
71 $instance['title'] = wp_strip_all_tags( $new_instance['title'] );
72 $instance['description'] = wp_strip_all_tags( $new_instance['description'] );
73
74 return $instance;
75 }
76
77 /**
78 * Display widget
79 *
80 * @param array<string, mixed> $args Widget arguments.
81 * @param array<string, mixed> $instance Widget instance.
82 * @return void
83 */
84 public function widget( $args, $instance ) {
85
86 // phpcs:ignore WordPress.PHP.DontExtract.extract_extract -- Required for WordPress widget compatibility.
87 extract( $args );
88 // These are the widget options.
89 $title = apply_filters( 'widget_title', $instance['title'] );
90 $description = $instance['description'];
91
92 // Ensure widget variables are defined.
93 $before_widget = isset( $before_widget ) ? $before_widget : '<div class="widget">';
94 $after_widget = isset( $after_widget ) ? $after_widget : '</div>';
95 $before_title = isset( $before_title ) ? $before_title : '<h3 class="widget-title">';
96 $after_title = isset( $after_title ) ? $after_title : '</h3>';
97
98 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Widget output is controlled by theme.
99 echo $before_widget;
100 // Display the widget.
101 echo '<div class="analytify-widget-realtime">';
102
103 // Check if title is set.
104 if ( $title ) {
105 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Widget output is controlled by theme.
106 echo $before_title . esc_html( $title ) . $after_title;
107 }
108
109 // Check if description is set.
110 if ( $description ) {
111 echo '<p class="analytify-widget-desc">' . esc_html( $description ) . '</p>';
112 }
113
114 echo '<div class="analytify-widget-realtime-visitors">0</div>';
115
116 echo '</div>';
117 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Widget output is controlled by theme.
118 echo $after_widget;
119
120 ?>
121 <script>
122 jQuery(document).ready(function ($) {
123
124 function analytify_realtime_widget(){
125
126 jQuery.post(
127 ajax_object.ajax_url, {
128 action: "pa_get_online_data",
129 pa_security: "<?php echo esc_attr( wp_create_nonce( 'pa_get_online_data' ) ); ?>"
130 },
131 function(response){
132 var data = jQuery.parseJSON(response);
133 $('.analytify-widget-realtime-visitors').html(data["totalsForAllResults"]["ga:activeVisitors"]);
134 }
135 );
136
137 }
138
139 analytify_realtime_widget();
140 setInterval(analytify_realtime_widget, 5000);
141
142 });
143 </script>
144 <?php
145 }
146 }
147
148 // Register widget.
149 add_action(
150 'widgets_init',
151 function () {
152 return register_widget( 'ANALYTIFY_WIDGET_REALTIME' );
153 }
154 );
155
156