Yandex Metrica - Version 1.5

Version Description

  • Metrica API upgraded, fixes api related problems
    • sslverify parameter set to true
    • UI improvements
    • Informer widget address update
    • Widget: showing visitors fix. Props
    • requires at least WordPress 3.7
Download this release

Release Info

Developer m_uysl
Plugin Icon 128x128 Yandex Metrica
Version 1.5
Comparing to
See all releases

Code changes from version 1.4.3 to 1.5

README.md CHANGED
@@ -1,12 +1,12 @@
1
  # Yandex Metrica #
2
 
3
- Contributors: m_uysl
4
- Tags: yandex,metrica,stats,statistics,tools,analytics,analytics tool,metrika
5
- Requires at least: 3.0
6
- Tested up to: 4.7
7
- Stable tag: 1.4.3
8
- License: GPLv2 (or later)
9
- License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
11
 
12
  Easy way to use Yandex Metrica in your WordPress site.
@@ -63,6 +63,14 @@ Extract the zip file and just drop the contents in the `wp-content/plugins/` dir
63
 
64
  ## Changelog ##
65
 
 
 
 
 
 
 
 
 
66
  ### 1.4.3 ###
67
  - minor bug fix about HTTP request
68
 
@@ -132,3 +140,7 @@ Extract the zip file and just drop the contents in the `wp-content/plugins/` dir
132
  ### 1.0 ###
133
 
134
  Including major changes, recommended update!
 
 
 
 
1
  # Yandex Metrica #
2
 
3
+ Contributors: m_uysl
4
+ Tags: yandex,metrica,stats,statistics,tools,analytics,analytics tool,metrika
5
+ Requires at least: 3.7
6
+ Tested up to: 4.7
7
+ Stable tag: 1.5
8
+ License: GPLv2 (or later)
9
+ License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
11
 
12
  Easy way to use Yandex Metrica in your WordPress site.
63
 
64
  ## Changelog ##
65
 
66
+ ### 1.5 ###
67
+ - Metrica API upgraded, fixes api related problems
68
+ - `sslverify` parameter set to true
69
+ - UI improvements
70
+ - Informer widget address update
71
+ - Widget: showing visitors fix. Props Эльвира Капитонова
72
+ - requires at least WordPress 3.7
73
+
74
  ### 1.4.3 ###
75
  - minor bug fix about HTTP request
76
 
140
  ### 1.0 ###
141
 
142
  Including major changes, recommended update!
143
+
144
+ ### 1.5 ###
145
+
146
+ Metrica api upgraded, if you get authentication related problems reset plugin settings and connect to API again (that will generate new token)
libs/Yandex_Metrica.php CHANGED
@@ -1,74 +1,89 @@
1
  <?php
 
 
 
2
 
3
  class Yandex_Metrica {
4
 
5
  private $access_token;
6
  public $error;
 
7
 
8
  /**
9
- * Fetch data via WordPress http api
 
 
 
 
 
 
 
 
 
 
10
  *
11
  * @param $url
12
  *
13
  * @return mixed
14
  */
15
  public function fetch_data( $url ) {
16
- $data = wp_remote_get( $url, array( 'timeout' => 7, 'httpversion' => '1.1', 'sslverify' => false ) );
17
 
18
- if ( $data instanceof WP_Error ) {
19
  $this->error = "HTTP Request problem";
20
 
21
  return false;
22
  }
23
 
24
- if ( isset( $data['response']['message'] ) && 'OK' != $data['response']['message'] ) {
25
  $this->error = "API connectivity problem.";
26
 
27
  return false;
28
  }
29
 
30
- return $data["body"];
31
- }
32
-
33
-
34
- public function __construct( $access_token ) {
35
- if ( empty( $access_token ) ) {
36
- $this->error = "Empty access token!";
37
- }
38
- $this->access_token = $access_token;
39
  }
40
 
41
 
42
  /**
43
- * Fetch all results
44
- * since 1.0
 
45
  * @return array|mixed counters
46
  */
47
  public function get_all_counters() {
48
- $url = 'http://api-metrika.yandex.com/counters.json?pretty=1&oauth_token=' . $this->access_token;
49
- $data = $this->fetch_data( $url );
50
- $results = json_decode( $data, true );
 
 
 
 
51
 
52
  return $results;
53
  }
54
 
55
  /**
56
  * How many counters are there?
 
57
  * @return mixed
58
  * @since 1.0
59
  */
60
  public function count_counters() {
61
  $counters = $this->get_all_counters();
 
62
  return $counters["rows"];
63
  }
64
 
65
  /**
66
  * Get all counters
 
67
  * @return mixed | array
68
  * @since 1.0
69
  */
70
  public function get_counters() {
71
  $results = $this->get_all_counters();
 
72
  return $results["counters"];
73
  }
74
 
@@ -84,14 +99,14 @@ class Yandex_Metrica {
84
 
85
  if ( $current_counter["counter"]["code_status"] == "CS_OK" ) {
86
  return true;
87
- }
88
- else {
89
  return false;
90
  }
91
  }
92
 
93
  /**
94
  * Fetch counter' all data
 
95
  * @see http://api.yandex.com/metrika/doc/ref/reference/get-counter.xml
96
  *
97
  * @param $counter_id
@@ -100,13 +115,13 @@ class Yandex_Metrica {
100
  * @since 1.0
101
  */
102
  public function fetch_counter( $counter_id ) {
103
- $results = get_transient( 'metrica_counter_' . $counter_id );
104
 
105
  if ( ! $results ) {
106
- $counter_url = 'http://api-metrika.yandex.com/counter/' . $counter_id . '.json?pretty=1&oauth_token=' . $this->access_token;
107
  $data = $this->fetch_data( $counter_url );
108
  $results = json_decode( $data, true );
109
- set_transient( 'metrica_counter_' . $counter_id, $results, 720 );
110
  }
111
 
112
  return $results;
@@ -115,33 +130,103 @@ class Yandex_Metrica {
115
 
116
  public function get_counter_name( $counter_id ) {
117
  $current_counter = $this->fetch_counter( $counter_id );
 
118
  return $current_counter["counter"]["name"];
119
  }
120
 
121
 
122
- public function get_counter_statistics( $counter_id, $start_date, $end_date, $value = null ) {
123
- $statistics = get_transient( 'counter_statistics_' . $counter_id . '_' . $start_date . $end_date );
124
-
125
- if ( ! $statistics ) {
126
- $stats_url = 'http://api-metrika.yandex.com/stat/traffic/summary.json?id=' . $counter_id . '&pretty=1&date1=' . $start_date . '&date2=' . $end_date . '&oauth_token=' . $this->access_token;
127
- $statistics = json_decode( $this->fetch_data( $stats_url ), true );
128
- set_transient( 'counter_statistics_' . $counter_id . '_' . $start_date . $end_date, $statistics, 3600 );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
129
  }
130
 
131
- if ( is_null( $value ) ) {
132
- return $statistics;
 
133
  }
134
- return $statistics[$value];
 
135
  }
136
 
137
 
138
  public function get_popular_content( $counter_id, $start_date, $end_date, $per_page = 5 ) {
139
- $popular_content = get_transient( 'popular_content_' . $counter_id . '_' . $start_date . $end_date );
140
 
141
  if ( ! $popular_content ) {
142
- $content_url = 'http://api-metrika.yandex.com/stat/content/popular.json?id=' . $counter_id . '&pretty=1&date1=' . $start_date . '&date2=' . $end_date . '&per_page=' . $per_page . '&oauth_token=' . $this->access_token;
143
- $popular_content = json_decode( $this->fetch_data( $content_url ), true );
144
- set_transient( 'popular_content_' . $counter_id . '_' . $start_date . $end_date, $popular_content, 3600 );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
145
  }
146
 
147
  return $popular_content;
@@ -149,27 +234,78 @@ class Yandex_Metrica {
149
 
150
 
151
  public function get_referal_sites( $counter_id, $start_date, $end_date, $per_page = 5 ) {
152
- $top_referrers = get_transient( 'top_referrers_' . $counter_id . '_' . $start_date . $end_date );
153
 
154
  if ( ! $top_referrers ) {
155
- $referral_url = 'http://api-metrika.yandex.com/stat/sources/sites.json?id=' . $counter_id . '&pretty=1&date1=' . $start_date . '&date2=' . $end_date . '&per_page=' . $per_page . '&oauth_token=' . $this->access_token;
156
- $top_referrers = json_decode( $this->fetch_data( $referral_url ), true );
157
- set_transient( 'top_referrers_' . $counter_id . '_' . $start_date . $end_date, $top_referrers, 3600 );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
158
  }
159
 
 
160
  return $top_referrers;
161
  }
162
 
163
 
164
  public function get_search_terms( $counter_id, $start_date, $end_date, $per_page = 5 ) {
165
- $top_searches = get_transient( 'top_searches_' . $counter_id . '_' . $start_date . $end_date );
 
166
 
167
  if ( ! $top_searches ) {
168
- $phrases_url = 'http://api-metrika.yandex.com/stat/sources/phrases.json?id=' . $counter_id . '&pretty=1&date1=' . $start_date . '&date2=' . $end_date . '&per_page=' . $per_page . '&oauth_token=' . $this->access_token;
169
- $top_searches = json_decode( $this->fetch_data( $phrases_url ), true );
170
- set_transient( 'top_searches_' . $counter_id . '_' . $start_date . $end_date, $top_searches, 3600 );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
171
  }
172
 
 
173
  return $top_searches;
174
  }
175
 
1
  <?php
2
+ if ( ! defined( 'ABSPATH' ) ) {
3
+ exit; // Exit if accessed directly.
4
+ }
5
 
6
  class Yandex_Metrica {
7
 
8
  private $access_token;
9
  public $error;
10
+ const TRANSIENT_VERSION = '1.5';
11
 
12
  /**
13
+ * @param string $access_token
14
+ */
15
+ public function __construct( $access_token ) {
16
+ if ( empty( $access_token ) ) {
17
+ $this->error = "Empty access token!";
18
+ }
19
+ $this->access_token = $access_token;
20
+ }
21
+
22
+ /**
23
+ * Fetch data via WordPress HTTP api
24
  *
25
  * @param $url
26
  *
27
  * @return mixed
28
  */
29
  public function fetch_data( $url ) {
30
+ $response = wp_remote_get( $url, array( 'timeout' => 7, 'httpversion' => '1.1', 'sslverify' => true ) );
31
 
32
+ if ( is_wp_error( $response ) ) {
33
  $this->error = "HTTP Request problem";
34
 
35
  return false;
36
  }
37
 
38
+ if ( isset( $response['response']['message'] ) && 'OK' != $response['response']['message'] ) {
39
  $this->error = "API connectivity problem.";
40
 
41
  return false;
42
  }
43
 
44
+ return $response["body"];
 
 
 
 
 
 
 
 
45
  }
46
 
47
 
48
  /**
49
+ * Fetch counter lists
50
+ *
51
+ * @since 1.0
52
  * @return array|mixed counters
53
  */
54
  public function get_all_counters() {
55
+
56
+ $url = esc_url_raw( add_query_arg( array(
57
+ 'oauth_token' => $this->access_token,
58
+ ), 'https://api-metrika.yandex.com/management/v1/counters' ) );
59
+
60
+ $response = $this->fetch_data( $url );
61
+ $results = json_decode( $response, true );
62
 
63
  return $results;
64
  }
65
 
66
  /**
67
  * How many counters are there?
68
+ *
69
  * @return mixed
70
  * @since 1.0
71
  */
72
  public function count_counters() {
73
  $counters = $this->get_all_counters();
74
+
75
  return $counters["rows"];
76
  }
77
 
78
  /**
79
  * Get all counters
80
+ *
81
  * @return mixed | array
82
  * @since 1.0
83
  */
84
  public function get_counters() {
85
  $results = $this->get_all_counters();
86
+
87
  return $results["counters"];
88
  }
89
 
99
 
100
  if ( $current_counter["counter"]["code_status"] == "CS_OK" ) {
101
  return true;
102
+ } else {
 
103
  return false;
104
  }
105
  }
106
 
107
  /**
108
  * Fetch counter' all data
109
+ *
110
  * @see http://api.yandex.com/metrika/doc/ref/reference/get-counter.xml
111
  *
112
  * @param $counter_id
115
  * @since 1.0
116
  */
117
  public function fetch_counter( $counter_id ) {
118
+ $results = get_transient( 'metrica_counter_' . self::TRANSIENT_VERSION . '_' . $counter_id );
119
 
120
  if ( ! $results ) {
121
+ $counter_url = 'https://api-metrika.yandex.com/management/v1/counter/' . $counter_id . '?oauth_token=' . $this->access_token;
122
  $data = $this->fetch_data( $counter_url );
123
  $results = json_decode( $data, true );
124
+ set_transient( 'metrica_counter_' . self::TRANSIENT_VERSION . '_' . $counter_id, $results, 720 );
125
  }
126
 
127
  return $results;
130
 
131
  public function get_counter_name( $counter_id ) {
132
  $current_counter = $this->fetch_counter( $counter_id );
133
+
134
  return $current_counter["counter"]["name"];
135
  }
136
 
137
 
138
+ public function get_counter_statistics( $counter_id, $start_date, $end_date, $stats_type = null ) {
139
+ $stats = get_transient( 'counter_statistics_' . self::TRANSIENT_VERSION . '_' . $counter_id . '_' . $start_date . $end_date );
140
+
141
+ if ( ! $stats ) {
142
+ $stats_url = esc_url_raw( add_query_arg( array(
143
+ 'date1' => $start_date,
144
+ 'date2' => $end_date,
145
+ 'metrics' => 'ym:s:pageviews,ym:s:visits,ym:s:users,ym:s:percentNewVisitors,ym:s:pageDepth,ym:s:avgVisitDurationSeconds',
146
+ 'group' => 'day',
147
+ 'ids' => $counter_id,
148
+ 'oauth_token' => $this->access_token,
149
+ ), 'https://api-metrika.yandex.com/stat/v1/data/bytime' ) );
150
+
151
+
152
+ $response = json_decode( $this->fetch_data( $stats_url ), true );
153
+ $daily = array();
154
+
155
+ $pageviews = $response['data'][0]['metrics'][0];
156
+ $visits = $response['data'][0]['metrics'][1];
157
+ $users = $response['data'][0]['metrics'][2];
158
+ $new_visitors = $response['data'][0]['metrics'][3];
159
+ $page_depth = $response['data'][0]['metrics'][4];
160
+ $duration = $response['data'][0]['metrics'][5];
161
+
162
+ foreach ( $response['time_intervals'] as $key => $time ) {
163
+ $daily[ $time[0] ] = array(
164
+ 'pageviews' => $pageviews[ $key ],
165
+ 'visits' => $visits[ $key ],
166
+ 'visitors' => $users[ $key ],
167
+ 'new_visitors' => $new_visitors[ $key ],
168
+ 'page_depth' => $page_depth[ $key ],
169
+ 'duration' => $duration[ $key ],
170
+ );
171
+ }
172
+
173
+ $total = array(
174
+ 'pageviews' => $response['totals'][0][0],
175
+ 'visits' => $response['totals'][0][1],
176
+ 'visitors' => $response['totals'][0][2],
177
+ 'new_visitors' => $response['totals'][0][3],
178
+ 'page_depth' => $response['totals'][0][4],
179
+ 'duration' => $response['totals'][0][5],
180
+ );
181
+
182
+ $stats = array(
183
+ 'daily' => $daily,
184
+ 'total' => $total,
185
+ );
186
+
187
+ set_transient( 'counter_statistics_' . self::TRANSIENT_VERSION . '_' . $counter_id . '_' . $start_date . $end_date, $stats, 3600 );
188
  }
189
 
190
+
191
+ if ( ! is_null( $stats_type ) && array_key_exists( $stats_type, $stats ) ) {
192
+ return $stats[ $stats_type ];
193
  }
194
+
195
+ return $stats;
196
  }
197
 
198
 
199
  public function get_popular_content( $counter_id, $start_date, $end_date, $per_page = 5 ) {
200
+ $popular_content = get_transient( 'popular_content_' . self::TRANSIENT_VERSION . '_' . $counter_id . '_' . $start_date . $end_date );
201
 
202
  if ( ! $popular_content ) {
203
+
204
+ $stats_url = esc_url_raw( add_query_arg( array(
205
+ 'date1' => $start_date,
206
+ 'date2' => $end_date,
207
+ 'dimensions' => 'ym:pv:URL',
208
+ 'metrics' => 'ym:pv:pageviews',
209
+ 'group' => 'day',
210
+ 'ids' => $counter_id,
211
+ 'limit' => $per_page, // @todo this parameter is not works, take a look later
212
+ 'oauth_token' => $this->access_token,
213
+ ), 'https://api-metrika.yandex.com/stat/v1/data/bytime' ) );
214
+
215
+
216
+ $popular_content_result = json_decode( $this->fetch_data( $stats_url ), true );
217
+
218
+
219
+ $popular_content = array();
220
+
221
+ foreach ( $popular_content_result['data'] as $data_key => $data ) {
222
+ $popular_content[ $data_key ]['url'] = $data['dimensions'][0]['name'];
223
+ $popular_content[ $data_key ]['pageviews'] = $popular_content_result['totals'][0][ $data_key ];
224
+ if ( count( $popular_content ) >= $per_page ) {
225
+ break;
226
+ }
227
+ }
228
+
229
+ set_transient( 'popular_content_' . self::TRANSIENT_VERSION . '_' . $counter_id . '_' . $start_date . $end_date, $popular_content, 3600 );
230
  }
231
 
232
  return $popular_content;
234
 
235
 
236
  public function get_referal_sites( $counter_id, $start_date, $end_date, $per_page = 5 ) {
237
+ $top_referrers = get_transient( 'top_referrers_' . self::TRANSIENT_VERSION . '_' . $counter_id . '_' . $start_date . $end_date );
238
 
239
  if ( ! $top_referrers ) {
240
+
241
+ $stats_url = esc_url_raw( add_query_arg( array(
242
+ 'date1' => $start_date,
243
+ 'date2' => $end_date,
244
+ 'dimensions' => 'ym:s:referer',
245
+ 'metrics' => 'ym:s:visits',
246
+ 'group' => 'day',
247
+ 'ids' => $counter_id,
248
+ 'limit' => $per_page,
249
+ 'oauth_token' => $this->access_token,
250
+ ), 'https://api-metrika.yandex.com/stat/v1/data/bytime' ) );
251
+
252
+
253
+ $top_referrers_result = json_decode( $this->fetch_data( $stats_url ), true );
254
+
255
+
256
+ $top_referrers = array();
257
+
258
+ foreach ( $top_referrers_result['data'] as $data_key => $data ) {
259
+ $top_referrers[ $data_key ]['url'] = $data['dimensions'][0]['name'];
260
+ $top_referrers[ $data_key ]['visits'] = $top_referrers_result['totals'][0][ $data_key ];
261
+ if ( count( $top_referrers ) >= $per_page ) {
262
+ break;
263
+ }
264
+ }
265
+
266
+ set_transient( 'top_referrers_' . self::TRANSIENT_VERSION . '_' . $counter_id . '_' . $start_date . $end_date, $top_referrers, 3600 );
267
  }
268
 
269
+
270
  return $top_referrers;
271
  }
272
 
273
 
274
  public function get_search_terms( $counter_id, $start_date, $end_date, $per_page = 5 ) {
275
+
276
+ $top_searches = get_transient( 'top_searches_' . self::TRANSIENT_VERSION . '_' . $counter_id . '_' . $start_date . $end_date );
277
 
278
  if ( ! $top_searches ) {
279
+
280
+ $stats_url = add_query_arg( array(
281
+ 'date1' => $start_date,
282
+ 'date2' => $end_date,
283
+ 'dimensions' => 'ym:s:<attribution>SearchPhrase',
284
+ 'metrics' => 'ym:s:visits',
285
+ 'group' => 'day',
286
+ 'ids' => $counter_id,
287
+ 'limit' => $per_page,
288
+ 'oauth_token' => $this->access_token,
289
+ ), 'https://api-metrika.yandex.com/stat/v1/data/bytime' );
290
+
291
+
292
+ $top_searches_result = json_decode( $this->fetch_data( $stats_url ), true );
293
+
294
+ $top_searches = array();
295
+
296
+ foreach ( $top_searches_result['data'] as $data_key => $data ) {
297
+ $top_searches[ $data_key ]['name'] = $data['dimensions'][0]['name'];
298
+ $top_searches[ $data_key ]['url'] = $data['dimensions'][0]['url'];
299
+ $top_searches[ $data_key ]['visits'] = $top_searches_result['totals'][0][ $data_key ];
300
+ if ( count( $top_searches ) >= $per_page ) {
301
+ break;
302
+ }
303
+ }
304
+
305
+ set_transient( 'top_searches_' . self::TRANSIENT_VERSION . '_' . $counter_id . '_' . $start_date . $end_date, $top_searches, 3600 );
306
  }
307
 
308
+
309
  return $top_searches;
310
  }
311
 
libs/Yandex_Oauth.php CHANGED
@@ -1,5 +1,8 @@
1
  <?php
2
 
 
 
 
3
 
4
  class Yandex_Oauth {
5
 
@@ -8,6 +11,7 @@ class Yandex_Oauth {
8
  private $app_secret;
9
  public $error;
10
  private $access_token;
 
11
 
12
 
13
  /**
@@ -28,49 +32,44 @@ class Yandex_Oauth {
28
  * @return bool
29
  */
30
  public function connect_oauth_server( $confirmation_code ) {
31
- $param = 'grant_type=authorization_code&code=' . $confirmation_code . '&client_id=' . $this->app_id . '&client_secret=' . $this->app_secret . '';
32
- $url = "https://oauth.yandex.com/token";
33
 
34
- $header = array(
35
- 'POST /token HTTP/1.1',
36
- 'Host: oauth.yandex.com',
37
- 'Content-type: application/json',
38
- 'Content-Length: ' . strlen( $param ),
39
- );
40
-
41
- $data = wp_remote_post( $url, array(
42
  'method' => 'POST',
43
  'timeout' => 45,
44
  'httpversion' => '1.1',
45
- 'header' => $header,
46
- 'body' => $param,
47
- 'sslverify' => false
48
- )
49
- );
50
-
51
- if ( is_wp_error( $data ) ) {
 
 
 
52
  return false;
53
  }
54
 
55
- $http_code = $data["response"]["code"];
56
 
57
  if ( ( $http_code != '200' ) && ( $http_code != '400' ) ) {
58
  $this->error = $http_code;
 
59
  return false;
60
  }
61
 
62
- $result = json_decode( $data["body"], true );
63
 
64
  if ( empty( $result['error'] ) ) {
65
  $this->access_token = $result['access_token'];
 
66
  return true;
67
  }
68
- else {
69
- $this->error = $result['error'];
70
- return false;
71
- }
72
 
73
 
 
 
 
74
  }
75
 
76
  /**
@@ -79,15 +78,16 @@ class Yandex_Oauth {
79
  public function check_access() {
80
  if ( ! empty( $this->access_token ) && ( empty( $this->error ) ) ) {
81
  return true;
82
- }
83
- else {
84
  $this->error = 'expired_token';
 
85
  return false;
86
  }
87
  }
88
 
89
  /**
90
  * What is the problem?
 
91
  * @return string
92
  */
93
  public function get_error() {
1
  <?php
2
 
3
+ if ( ! defined( 'ABSPATH' ) ) {
4
+ exit; // Exit if accessed directly.
5
+ }
6
 
7
  class Yandex_Oauth {
8
 
11
  private $app_secret;
12
  public $error;
13
  private $access_token;
14
+ const OAUTH_SERVER = 'https://oauth.yandex.com/token';
15
 
16
 
17
  /**
32
  * @return bool
33
  */
34
  public function connect_oauth_server( $confirmation_code ) {
 
 
35
 
36
+ $response = wp_remote_post( self::OAUTH_SERVER, array(
 
 
 
 
 
 
 
37
  'method' => 'POST',
38
  'timeout' => 45,
39
  'httpversion' => '1.1',
40
+ 'body' => array(
41
+ 'grant_type' => 'authorization_code',
42
+ 'code' => $confirmation_code,
43
+ 'client_id' => $this->app_id,
44
+ 'client_secret' => $this->app_secret,
45
+ ),
46
+ 'sslverify' => true,
47
+ ) );
48
+
49
+ if ( is_wp_error( $response ) ) {
50
  return false;
51
  }
52
 
53
+ $http_code = $response["response"]["code"];
54
 
55
  if ( ( $http_code != '200' ) && ( $http_code != '400' ) ) {
56
  $this->error = $http_code;
57
+
58
  return false;
59
  }
60
 
61
+ $result = json_decode( $response["body"], true );
62
 
63
  if ( empty( $result['error'] ) ) {
64
  $this->access_token = $result['access_token'];
65
+
66
  return true;
67
  }
 
 
 
 
68
 
69
 
70
+ $this->error = $result['error'];
71
+
72
+ return false;
73
  }
74
 
75
  /**
78
  public function check_access() {
79
  if ( ! empty( $this->access_token ) && ( empty( $this->error ) ) ) {
80
  return true;
81
+ } else {
 
82
  $this->error = 'expired_token';
83
+
84
  return false;
85
  }
86
  }
87
 
88
  /**
89
  * What is the problem?
90
+ *
91
  * @return string
92
  */
93
  public function get_error() {
libs/widget.php CHANGED
@@ -1,10 +1,8 @@
1
- <?php if ( ! defined( 'ABSPATH' ) ) die(); ?>
2
  <?php
3
- /**
4
- * @package : WordPress
5
- * @subpackage: Yandex Metrica
6
- */
7
 
 
 
 
8
 
9
  class Metrica_Widget extends WP_Widget {
10
  protected $defaults;
@@ -55,19 +53,19 @@ class Metrica_Widget extends WP_Widget {
55
  printf( __( '<h3>Metrica statistics for %s </h3>', 'yandex-metrica' ), __( ucfirst( $instance["time"] ), 'yandex-metrica' ) );
56
  $main_options = get_option( 'metrica_options' );
57
  $metrica_api = new Yandex_Metrica( $main_options['access_token'] );
58
- $results = $metrica_api->get_counter_statistics( $main_options['counter_id'], $start_date, $end_date, "totals" );
59
 
60
- $instance["page_views"] = $results["page_views"];
61
  $instance["visits"] = $results["visits"];
62
  $instance["visitors"] = $results["visitors"];
63
 
64
 
65
- if ( $instance['show_page_views'] === true && ( ! empty( $instance["page_views"] ) ) )
66
- printf( __( 'Page Views: <b>%d</b> <br/>', 'yandex-metrica' ), $instance['page_views'] );
67
  if ( $instance['show_visits'] === true && ! empty( $instance["visits"] ) )
68
  printf( __( 'Visits: <b>%d</b> </br>', 'yandex-metrica' ), $instance['visits'] );
69
- if ( $instance['show_visitors'] === true && ! empty( $instance["visits"] ) )
70
- printf( __( 'Visitors: <b>%d</b> </br>', 'yandex-metrica' ), $instance['visits'] );
71
 
72
  echo $args['after_widget'];
73
  }
 
1
  <?php
 
 
 
 
2
 
3
+ if ( ! defined( 'ABSPATH' ) ) {
4
+ exit; // Exit if accessed directly.
5
+ }
6
 
7
  class Metrica_Widget extends WP_Widget {
8
  protected $defaults;
53
  printf( __( '<h3>Metrica statistics for %s </h3>', 'yandex-metrica' ), __( ucfirst( $instance["time"] ), 'yandex-metrica' ) );
54
  $main_options = get_option( 'metrica_options' );
55
  $metrica_api = new Yandex_Metrica( $main_options['access_token'] );
56
+ $results = $metrica_api->get_counter_statistics( $main_options['counter_id'], $start_date, $end_date, "total" );
57
 
58
+ $instance["pageviews"] = $results["pageviews"];
59
  $instance["visits"] = $results["visits"];
60
  $instance["visitors"] = $results["visitors"];
61
 
62
 
63
+ if ( $instance['show_page_views'] === true && ( ! empty( $instance["pageviews"] ) ) )
64
+ printf( __( 'Page Views: <b>%d</b> <br/>', 'yandex-metrica' ), $instance['pageviews'] );
65
  if ( $instance['show_visits'] === true && ! empty( $instance["visits"] ) )
66
  printf( __( 'Visits: <b>%d</b> </br>', 'yandex-metrica' ), $instance['visits'] );
67
+ if ( $instance['show_visitors'] === true && ! empty( $instance["visitors"] ) )
68
+ printf( __( 'Visitors: <b>%d</b> </br>', 'yandex-metrica' ), $instance['visitors'] );
69
 
70
  echo $args['after_widget'];
71
  }
readme.txt CHANGED
@@ -2,9 +2,9 @@
2
 
3
  Contributors: m_uysl
4
  Tags: yandex,metrica,stats,statistics,tools,analytics,analytics tool,metrika
5
- Requires at least: 3.0
6
  Tested up to: 4.7
7
- Stable tag: 1.4.3
8
  License: GPLv2 (or later)
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
@@ -63,6 +63,14 @@ Extract the zip file and just drop the contents in the `wp-content/plugins/` dir
63
 
64
  == Changelog ==
65
 
 
 
 
 
 
 
 
 
66
  = 1.4.3 =
67
  - minor bug fix about HTTP request
68
 
@@ -131,4 +139,8 @@ Extract the zip file and just drop the contents in the `wp-content/plugins/` dir
131
 
132
  = 1.0 =
133
 
134
- Including major changes, recommended update!
 
 
 
 
2
 
3
  Contributors: m_uysl
4
  Tags: yandex,metrica,stats,statistics,tools,analytics,analytics tool,metrika
5
+ Requires at least: 3.7
6
  Tested up to: 4.7
7
+ Stable tag: 1.5
8
  License: GPLv2 (or later)
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
63
 
64
  == Changelog ==
65
 
66
+ = 1.5 =
67
+ - Metrica API upgraded, fixes api related problems
68
+ - `sslverify` parameter set to true
69
+ - UI improvements
70
+ - Informer widget address update
71
+ - Widget: showing visitors fix. Props Эльвира Капитонова
72
+ - requires at least WordPress 3.7
73
+
74
  = 1.4.3 =
75
  - minor bug fix about HTTP request
76
 
139
 
140
  = 1.0 =
141
 
142
+ Including major changes, recommended update!
143
+
144
+ = 1.5 =
145
+
146
+ Metrica api upgraded, if you get authentication related problems reset plugin settings and connect to API again (that will generate new token)
templates/dashboard-charts-js.php CHANGED
@@ -23,15 +23,11 @@
23
  categories: [
24
  <?php
25
  // use WordPress' date function date_i18n instead of the php's date. Because localization matters...
26
- if($this->period != "monthly"){
27
- foreach( $statical_data as $item){
28
- echo "'" .date_i18n('D', strtotime($item["date"])). "',";
29
- };
30
- } else {
31
- foreach( $statical_data as $item){
32
- echo "'" .date_i18n('d M', strtotime($item["date"])). "',";
33
- };
34
- }
35
  ?>
36
  ]
37
 
@@ -51,6 +47,14 @@
51
  },
52
 
53
  series: [
 
 
 
 
 
 
 
 
54
  {
55
  name: '<?php echo __('Visits','yandex-metrica');?>',
56
  data: [
@@ -70,23 +74,17 @@
70
  ]
71
  });
72
 
73
- $( "#toggle-popular-pages" ).click(function() {
74
- $( ".metrica-popular-pages" ).toggle( function() {
75
-
76
- });
77
- });
78
-
79
- $( "#toggle-top-referrers" ).click(function() {
80
- $( ".metrica-top-referrers" ).toggle( function() {
81
-
82
- });
83
- });
84
 
85
- $( "#toggle-top-searches" ).click(function() {
86
- $( ".metrica-top-searches" ).toggle( function() {
 
87
 
88
- });
89
- });
 
90
 
91
  <?php } ?>
92
 
23
  categories: [
24
  <?php
25
  // use WordPress' date function date_i18n instead of the php's date. Because localization matters...
26
+ $date_format = ( $this->period != "monthly" ? 'D' : 'd M' );
27
+ foreach( $statical_data as $date => $stats_item ){
28
+ echo "'" .date_i18n($date_format, strtotime( $date ) ). "',";
29
+ }
30
+
 
 
 
 
31
  ?>
32
  ]
33
 
47
  },
48
 
49
  series: [
50
+ {
51
+ name: '<?php echo __('Pageviews','yandex-metrica');?>',
52
+ data: [
53
+ <?php foreach( $statical_data as $item){
54
+ echo $item["pageviews"].",";
55
+ };?>
56
+ ]
57
+ },
58
  {
59
  name: '<?php echo __('Visits','yandex-metrica');?>',
60
  data: [
74
  ]
75
  });
76
 
77
+ $("#toggle-metrica-popular-pages").click(function () {
78
+ $(".metrica-popular-pages").toggle();
79
+ });
 
 
 
 
 
 
 
 
80
 
81
+ $("#toggle-metrica-top-referrers").click(function () {
82
+ $(".metrica-top-referrers").toggle();
83
+ });
84
 
85
+ $("#toggle-metrica-top-searches").click(function () {
86
+ $(".metrica-top-searches").toggle();
87
+ });
88
 
89
  <?php } ?>
90
 
templates/dashboard-widget.php CHANGED
@@ -1,17 +1,22 @@
1
  <?php if ( ! defined( 'ABSPATH' )) {
2
  die();
3
  } ?>
4
- <select name="period" id="period">
5
- <option <?php selected( $this->period, "daily" ); ?> value="daily"><?php _e( 'Daily', 'yandex-metrica' ); ?></option>
6
- <option <?php selected( $this->period, "weekly" ); ?> <?php if (empty ( $this->period )) {
7
- echo "selected";
8
- } ?> value="weekly"><?php _e( 'Weekly', 'yandex-metrica' ); ?></option>
9
- <option <?php selected( $this->period, "monthly" ); ?> value="monthly"><?php _e( 'Monthly', 'yandex-metrica' ); ?></option>
10
- </select>
 
 
 
11
  <span id="metricaloading"></span>
12
- <?php if ( ! empty( $total_values["visits"] ) || ! empty( $total_values["visitors"] )): ?>
 
13
  <div id="metrica-graph" style="width:100%; height: 400px;"></div>
14
  <?php endif; ?>
 
15
  <div id="metrica-widget-data">
16
  <h3><?php _e( 'Site Usage', 'yandex-metrica' ); ?></h3>
17
  <table width="100%">
@@ -33,8 +38,8 @@
33
  </td>
34
  <td width="20%">
35
  <?php
36
- if ( ! empty( $total_values["new_visitors_perc"] )) {
37
- echo '%'.$total_values["new_visitors_perc"] * 100;
38
  } else {
39
  _e( 'None', 'yandex-metrica' );
40
  }
@@ -48,8 +53,8 @@
48
  <b><?php _e( 'Page Views', 'yandex-metrica' ); ?>:</b>
49
  </td>
50
  <td>
51
- <?php if ( ! empty( $total_values["page_views"] )) {
52
- echo $total_values["page_views"];
53
  } else {
54
  _e( 'None', 'yandex-metrica' );
55
  }
@@ -59,8 +64,8 @@
59
  <b><?php _e( 'Session depth', 'yandex-metrica' ); ?>:</b>
60
  </td>
61
  <td>
62
- <?php if ( ! empty( $total_values["depth"] )) {
63
- echo round( $total_values["depth"], 1 );
64
  } else {
65
  _e( 'None', 'yandex-metrica' );
66
  }
@@ -86,8 +91,8 @@
86
  </td>
87
  <td>
88
  <?php
89
- if ( ! empty( $total_values["visit_time"] )) {
90
- echo gmdate( "H:i:s", $total_values["visit_time"] );
91
  } else {
92
  _e( 'None', 'yandex-metrica' );
93
  }
@@ -99,71 +104,90 @@
99
  </table>
100
 
101
 
102
- <div id="popular-posts" class="postbox">
103
- <div class="handlediv" id="toggle-popular-pages"><br /></div>
 
 
104
 
105
- <h3><?php _e( 'Popular Pages', 'yandex-metrica' ); ?></h3>
106
 
107
- <ol class="metrica-popular-pages">
 
108
 
109
- <?php if ( ! empty( $popular_posts["data"] )): ?>
110
- <?php foreach ($popular_posts["data"] as $post): ?>
111
- <li>
112
- <a href="<?php echo esc_url( $post["url"] ); ?>"><?php echo esc_url( $post["url"] ); ?></a> -
113
- <?php echo sprintf( _n( '%d View', '%d Views', $post["page_views"], 'yandex-metrica' ), $post["page_views"] ); ?>
114
- </li>
115
- <?php endforeach; ?>
116
- <?php else: ?>
117
- <?php _e( 'None', 'yandex-metrica' ); ?>
118
- <?php endif; ?>
119
 
120
- </ol>
 
121
  </div>
122
 
123
 
124
- <div id="metrica-incoming" class="postbox">
125
- <div class="handlediv" id="toggle-top-referrers"><br /></div>
 
 
126
 
127
- <h3><?php _e( 'Top Referrers', 'yandex-metrica' ); ?></h3>
128
 
129
- <ol class="metrica-top-referrers" style="display:none">
130
 
131
- <?php if ( ! empty( $top_referrers["data"] )): ?>
132
- <?php foreach ($top_referrers["data"] as $referrer): ?>
133
- <li>
134
- <a href="<?php echo esc_url( $referrer["url"] ); ?>"><?php echo esc_url( $referrer["url"] ); ?></a> -
135
- <?php echo sprintf( _n( '%d Visit', '%d Visits', $referrer["visits"], 'yandex-metrica' ), $referrer["visits"] ); ?>
136
- </li>
137
- <?php endforeach; ?>
138
- <?php else: ?>
139
- <?php _e( 'None', 'yandex-metrica' ); ?>
140
- <?php endif; ?>
141
 
142
- </ol>
 
 
 
 
 
 
 
 
 
143
 
 
 
144
  </div>
145
 
146
 
147
- <div id="top-searches" class="postbox">
148
- <div class="handlediv" id="toggle-top-searches"><br /></div>
 
 
 
 
 
 
 
149
 
150
- <h3><?php _e( 'Search Terms', 'yandex-metrica' ); ?></h3>
151
- <ol class="metrica-top-searches" style="display:none">
 
 
 
 
 
 
 
 
152
 
153
- <?php if ( ! empty( $top_searches["data"] )) : ?>
154
- <?php foreach ($top_searches["data"] as $search_term): ?>
155
- <li>
156
- <strong><?php echo $search_term["phrase"]; ?></strong> -
157
- <?php echo sprintf( _n( '%d Visit', '%d Visits', $search_term["visits"], 'yandex-metrica' ), $search_term["visits"] ); ?>
158
- </li>
159
- <?php endforeach ?>
160
- <?php else: ?>
161
- <?php _e( 'None', 'yandex-metrica' ); ?>
162
- <?php endif; ?>
163
 
164
- </ol>
165
  </div>
166
 
167
  </div>
168
 
 
 
 
 
 
169
 
1
  <?php if ( ! defined( 'ABSPATH' )) {
2
  die();
3
  } ?>
4
+ <label>
5
+ <select name="period" id="period">
6
+ <option <?php selected( $this->period, "daily" ); ?> value="daily"><?php _e( 'Daily', 'yandex-metrica' ); ?></option>
7
+ <option <?php selected( $this->period, "weekly" ); ?> <?php if ( empty ( $this->period ) ) {
8
+ echo "selected";
9
+ } ?> value="weekly"><?php _e( 'Weekly', 'yandex-metrica' ); ?></option>
10
+ <option <?php selected( $this->period, "monthly" ); ?> value="monthly"><?php _e( 'Monthly', 'yandex-metrica' ); ?></option>
11
+ </select>
12
+ </label>
13
+
14
  <span id="metricaloading"></span>
15
+
16
+ <?php if ( ! empty( $total_values["visits"] ) || ! empty( $total_values["visitors"] ) ): ?>
17
  <div id="metrica-graph" style="width:100%; height: 400px;"></div>
18
  <?php endif; ?>
19
+
20
  <div id="metrica-widget-data">
21
  <h3><?php _e( 'Site Usage', 'yandex-metrica' ); ?></h3>
22
  <table width="100%">
38
  </td>
39
  <td width="20%">
40
  <?php
41
+ if ( ! empty( $total_values["new_visitors"] )) {
42
+ echo '% '.round($total_values["new_visitors"],2);
43
  } else {
44
  _e( 'None', 'yandex-metrica' );
45
  }
53
  <b><?php _e( 'Page Views', 'yandex-metrica' ); ?>:</b>
54
  </td>
55
  <td>
56
+ <?php if ( ! empty( $total_values["pageviews"] )) {
57
+ echo $total_values["pageviews"];
58
  } else {
59
  _e( 'None', 'yandex-metrica' );
60
  }
64
  <b><?php _e( 'Session depth', 'yandex-metrica' ); ?>:</b>
65
  </td>
66
  <td>
67
+ <?php if ( ! empty( $total_values["page_depth"] )) {
68
+ echo round( $total_values["page_depth"], 1 );
69
  } else {
70
  _e( 'None', 'yandex-metrica' );
71
  }
91
  </td>
92
  <td>
93
  <?php
94
+ if ( ! empty( $total_values["duration"] )) {
95
+ echo gmdate( "H:i:s", $total_values["duration"] );
96
  } else {
97
  _e( 'None', 'yandex-metrica' );
98
  }
104
  </table>
105
 
106
 
107
+ <div id="popular-posts" class="postbox <?php echo postbox_classes('popular-posts', 'dashboard');?>">
108
+ <button type="button" class="handlediv button-link" >
109
+ <span class="toggle-indicator" id="toggle-metrica-popular-pages"></span>
110
+ </button>
111
 
112
+ <h2 class="hndle"><?php _e( 'Popular Pages', 'yandex-metrica' ); ?></h2>
113
 
114
+ <div class="metrica-inside">
115
+ <ol class="metrica-popular-pages <?php echo postbox_classes('popular-posts', 'dashboard');?>">
116
 
117
+ <?php if ( ! empty( $popular_posts ) ): ?>
118
+ <?php foreach ( $popular_posts as $post ): ?>
119
+ <li>
120
+ <a href="<?php echo esc_url( $post["url"] ); ?>"><?php echo esc_url( $post["url"] ); ?></a> -
121
+ <?php echo sprintf( _n( '%d View', '%d Views', $post["pageviews"], 'yandex-metrica' ), $post["pageviews"] ); ?>
122
+ </li>
123
+ <?php endforeach; ?>
124
+ <?php else: ?>
125
+ <?php _e( 'None', 'yandex-metrica' ); ?>
126
+ <?php endif; ?>
127
 
128
+ </ol>
129
+ </div>
130
  </div>
131
 
132
 
133
+ <div id="metrica-incoming" class="postbox <?php echo postbox_classes('metrica-incoming', 'dashboard');?>">
134
+ <button type="button" class="handlediv button-link" >
135
+ <span class="toggle-indicator" id="toggle-metrica-top-referrers"></span>
136
+ </button>
137
 
 
138
 
139
+ <h2 class="hndle"><?php _e( 'Top Referrers', 'yandex-metrica' ); ?></h2>
140
 
141
+ <div class="metrica-inside">
142
+ <ol class="metrica-top-referrers <?php echo postbox_classes('metrica-incoming', 'dashboard');?>">
 
 
 
 
 
 
 
 
143
 
144
+ <?php if ( ! empty( $top_referrers ) ): ?>
145
+ <?php foreach ( $top_referrers as $referrer ): ?>
146
+ <li>
147
+ <a href="<?php echo esc_url( $referrer["url"] ); ?>"><?php echo esc_url( $referrer["url"] ); ?></a> -
148
+ <?php echo sprintf( _n( '%d Visit', '%d Visits', $referrer["visits"], 'yandex-metrica' ), $referrer["visits"] ); ?>
149
+ </li>
150
+ <?php endforeach; ?>
151
+ <?php else: ?>
152
+ <?php _e( 'None', 'yandex-metrica' ); ?>
153
+ <?php endif; ?>
154
 
155
+ </ol>
156
+ </div>
157
  </div>
158
 
159
 
160
+ <div id="top-searches" class="postbox <?php echo postbox_classes('top-searches', 'dashboard');?>">
161
+ <button type="button" class="handlediv button-link">
162
+ <span class="toggle-indicator" id="toggle-metrica-top-searches"></span>
163
+ </button>
164
+
165
+ <h2 class="hndle"><?php _e( 'Search Terms', 'yandex-metrica' ); ?></h2>
166
+
167
+ <div class="metrica-inside">
168
+ <ol class="metrica-top-searches <?php echo postbox_classes('top-searches', 'dashboard');?>">
169
 
170
+ <?php if ( ! empty( $top_searches ) ) : ?>
171
+ <?php foreach ( $top_searches as $search_term ): ?>
172
+ <li>
173
+ <strong><?php echo $search_term["name"]; ?></strong> -
174
+ <?php echo sprintf( _n( '%d Visit', '%d Visits', $search_term["visits"], 'yandex-metrica' ), $search_term["visits"] ); ?>
175
+ </li>
176
+ <?php endforeach ?>
177
+ <?php else: ?>
178
+ <?php _e( 'None', 'yandex-metrica' ); ?>
179
+ <?php endif; ?>
180
 
181
+ </ol>
 
 
 
 
 
 
 
 
 
182
 
183
+ </div>
184
  </div>
185
 
186
  </div>
187
 
188
+ <style>
189
+ .metrica-inside .closed{
190
+ display: none;
191
+ }
192
+ </style>
193
 
yandex-metrica.php CHANGED
@@ -4,7 +4,7 @@ Plugin Name: Yandex Metrica
4
  Plugin URI: http://uysalmustafa.com/plugins/yandex-metrica
5
  Description: Best metrica plugin for the use Yandex Metrica in your WordPress site.
6
  Author: Mustafa Uysal
7
- Version: 1.4.3
8
  Text Domain: yandex-metrica
9
  Domain Path: /languages/
10
  Author URI: http://uysalmustafa.com
@@ -135,7 +135,10 @@ class WP_Yandex_Metrica extends WP_Stack_Plugin {
135
  */
136
  if ( self::$metrica_api->is_valid_counter( $this->options["counter_id"] ) ) {
137
 
138
- $this->hook( 'admin_head', 'dashboard_chart_js' ); // add neccessary jsc
 
 
 
139
  wp_add_dashboard_widget( 'yandex_metrica_widget', __( 'Metrica Statistics', 'yandex-metrica' ), array( $this, 'metrica_dashboard_widget' ) );
140
  }
141
  else {
@@ -156,7 +159,7 @@ class WP_Yandex_Metrica extends WP_Stack_Plugin {
156
 
157
 
158
  public function metrica_dashboard_widget() {
159
- $total_values = self::$metrica_api->get_counter_statistics( $this->options["counter_id"], $this->start_date, $this->end_date, "totals" );
160
  $popular_posts = self::$metrica_api->get_popular_content( $this->options["counter_id"], $this->start_date, $this->end_date );
161
  $top_referrers = self::$metrica_api->get_referal_sites( $this->options["counter_id"], $this->start_date, $this->end_date );
162
  $top_searches = self::$metrica_api->get_search_terms( $this->options["counter_id"], $this->start_date, $this->end_date );
@@ -204,15 +207,7 @@ class WP_Yandex_Metrica extends WP_Stack_Plugin {
204
 
205
  public function dashboard_chart_js() {
206
  wp_enqueue_script( 'jquery' );
207
- $statical_data = self::$metrica_api->get_counter_statistics( $this->options["counter_id"], $this->start_date, $this->end_date, "data" );
208
-
209
- if ( is_array( $statical_data ) ) {
210
- $days = array();
211
- foreach ( $statical_data as $key => $row ) {
212
- $days[ $key ] = $row['date'];
213
- }
214
- array_multisort( $days, SORT_ASC, $statical_data );
215
- }
216
 
217
  include( dirname( __FILE__ ) . '/templates/dashboard-charts-js.php' );
218
  }
@@ -256,16 +251,16 @@ class WP_Yandex_Metrica extends WP_Stack_Plugin {
256
 
257
  switch ( $period ) {
258
  case "monthly":
259
- $this->start_date = date( 'Ymd', strtotime( "-1 month" ) );
260
- $this->end_date = date( 'Ymd' );
261
  break;
262
  case "weekly":
263
- $this->start_date = date( 'Ymd', strtotime( "-6 days" ) );
264
- $this->end_date = date( 'Ymd' );
265
  break;
266
  case "daily":
267
- $this->start_date = date( 'Ymd' );
268
- $this->end_date = date( 'Ymd' );
269
  break;
270
  }
271
 
@@ -308,7 +303,7 @@ class WP_Yandex_Metrica extends WP_Stack_Plugin {
308
 
309
 
310
  public function metrica_informer() {
311
- echo '<img src="http://bs.yandex.ru/informer/' . $this->options['counter_id'] . '/3_1_FFFFFFFF_EFEFEFFF_0_pageviews" style="width:80px; height:31px; border:0;" />';
312
  }
313
 
314
  /**
@@ -323,7 +318,7 @@ class WP_Yandex_Metrica extends WP_Stack_Plugin {
323
  wp_register_sidebar_widget(
324
  'metrica_informer',
325
  'Metrica Informer',
326
- array( &$this, 'metrica_informer' ),
327
  array(
328
  'description' => 'Add metrica Informer to your sidebar, share daily statistics'
329
  )
4
  Plugin URI: http://uysalmustafa.com/plugins/yandex-metrica
5
  Description: Best metrica plugin for the use Yandex Metrica in your WordPress site.
6
  Author: Mustafa Uysal
7
+ Version: 1.5
8
  Text Domain: yandex-metrica
9
  Domain Path: /languages/
10
  Author URI: http://uysalmustafa.com
135
  */
136
  if ( self::$metrica_api->is_valid_counter( $this->options["counter_id"] ) ) {
137
 
138
+ /**
139
+ * add inline chart js
140
+ */
141
+ $this->hook( 'admin_head', 'dashboard_chart_js' );
142
  wp_add_dashboard_widget( 'yandex_metrica_widget', __( 'Metrica Statistics', 'yandex-metrica' ), array( $this, 'metrica_dashboard_widget' ) );
143
  }
144
  else {
159
 
160
 
161
  public function metrica_dashboard_widget() {
162
+ $total_values = self::$metrica_api->get_counter_statistics( $this->options["counter_id"], $this->start_date, $this->end_date, 'total' );
163
  $popular_posts = self::$metrica_api->get_popular_content( $this->options["counter_id"], $this->start_date, $this->end_date );
164
  $top_referrers = self::$metrica_api->get_referal_sites( $this->options["counter_id"], $this->start_date, $this->end_date );
165
  $top_searches = self::$metrica_api->get_search_terms( $this->options["counter_id"], $this->start_date, $this->end_date );
207
 
208
  public function dashboard_chart_js() {
209
  wp_enqueue_script( 'jquery' );
210
+ $statical_data = self::$metrica_api->get_counter_statistics( $this->options["counter_id"], $this->start_date, $this->end_date, "daily" );
 
 
 
 
 
 
 
 
211
 
212
  include( dirname( __FILE__ ) . '/templates/dashboard-charts-js.php' );
213
  }
251
 
252
  switch ( $period ) {
253
  case "monthly":
254
+ $this->start_date = date( 'Y-m-d', strtotime( "-1 month" ) );
255
+ $this->end_date = date( 'Y-m-d' );
256
  break;
257
  case "weekly":
258
+ $this->start_date = date( 'Y-m-d', strtotime( "-6 days" ) );
259
+ $this->end_date = date( 'Y-m-d' );
260
  break;
261
  case "daily":
262
+ $this->start_date = date( 'Y-m-d' );
263
+ $this->end_date = date( 'Y-m-d' );
264
  break;
265
  }
266
 
303
 
304
 
305
  public function metrica_informer() {
306
+ echo '<img src="https://informer.yandex.ru/informer/' . esc_attr( $this->options['counter_id'] ) . '/3_1_FFFFFFFF_EFEFEFFF_0_pageviews" class="ym-advanced-informer" data-cid="' . esc_attr( $this->options['counter_id'] ) . '" style="width:80px; height:31px; border:0;" />';
307
  }
308
 
309
  /**
318
  wp_register_sidebar_widget(
319
  'metrica_informer',
320
  'Metrica Informer',
321
+ array( $this, 'metrica_informer' ),
322
  array(
323
  'description' => 'Add metrica Informer to your sidebar, share daily statistics'
324
  )