teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

10

コードの修正

2018/09/09 08:10

投稿

MyQuestioner
MyQuestioner

スコア57

title CHANGED
File without changes
body CHANGED
@@ -174,7 +174,7 @@
174
174
  ```
175
175
  Notice: Undefined indexのエラー対策として3パターン編集してみました。
176
176
  ```ここに言語を入力
177
- if (isset($_GET['page'] && $_POST['page'] == "{$this->DIR}_reset_page")){
177
+ if (isset($_GET['page'] && $_GET['page'] == "{$this->DIR}_reset_page")){
178
178
  ```
179
179
  の場合、エラー内容
180
180
  Fatal error: Cannot use isset() on the result of an expression (you can use "null !== expression" instead)

9

文章の修正

2018/09/09 08:10

投稿

MyQuestioner
MyQuestioner

スコア57

title CHANGED
File without changes
body CHANGED
@@ -188,7 +188,7 @@
188
188
  ```ここに言語を入力
189
189
  if (!empty($_GET['page'] === "{$this->DIR}_reset_page")){
190
190
  ```
191
- 「Notice: Undefined index: page in」エラーのまま
191
+ の場合、「Notice: Undefined index: page in」エラーのまま
192
192
 
193
193
  その他、edumpというデバッグプラグインでは
194
194
  「Only variables should be passed by reference」というエラーが

8

コードの修正

2018/09/08 11:05

投稿

MyQuestioner
MyQuestioner

スコア57

title CHANGED
File without changes
body CHANGED
@@ -174,7 +174,7 @@
174
174
  ```
175
175
  Notice: Undefined indexのエラー対策として3パターン編集してみました。
176
176
  ```ここに言語を入力
177
- if (isset($_GET['page'] && $_POST['page'] == "{$this->DIR}_reset_page"){
177
+ if (isset($_GET['page'] && $_POST['page'] == "{$this->DIR}_reset_page")){
178
178
  ```
179
179
  の場合、エラー内容
180
180
  Fatal error: Cannot use isset() on the result of an expression (you can use "null !== expression" instead)

7

文章が見にくいので修正

2018/09/08 11:03

投稿

MyQuestioner
MyQuestioner

スコア57

title CHANGED
File without changes
body CHANGED
@@ -176,12 +176,14 @@
176
176
  ```ここに言語を入力
177
177
  if (isset($_GET['page'] && $_POST['page'] == "{$this->DIR}_reset_page"){
178
178
  ```
179
+ の場合、エラー内容
179
- の場合、エラー内容:Fatal error: Cannot use isset() on the result of an expression (you can use "null !== expression" instead)
180
+ Fatal error: Cannot use isset() on the result of an expression (you can use "null !== expression" instead)
180
181
 
181
182
  ```ここに言語を入力
182
183
  if (isset($_GET['page'] === "{$this->DIR}_reset_page")){
183
184
  ```
185
+ の場合、エラー内容
184
- の場合、エラー内容:Fatal error: Cannot use isset() on the result of an expression (you can use "null !== expression" instead)
186
+ Fatal error: Cannot use isset() on the result of an expression (you can use "null !== expression" instead)
185
187
 
186
188
  ```ここに言語を入力
187
189
  if (!empty($_GET['page'] === "{$this->DIR}_reset_page")){

6

エラー表示の追加

2018/09/08 10:58

投稿

MyQuestioner
MyQuestioner

スコア57

title CHANGED
File without changes
body CHANGED
@@ -164,24 +164,34 @@
164
164
  }
165
165
  }
166
166
  }
167
- function getreposrt($name = 'GoogleAnalyticsRankingData'){
167
+
168
- $data = get_option($name);
169
- $today = date('Y-m-d',strtotime('-1 hour'));
170
- $yesterday = date('Y-m-d',strtotime('-25 hour'));
171
- if(empty($data) || (is_array($data) && (empty($data[$today]) || !count($data[$today])))){
172
- $task_time = strtotime(date('2018-01-01 1:00:00'));
173
- if ( !wp_next_scheduled('get_ranking_cron')) {
174
- wp_schedule_event( $task_time, 'daily', 'get_ranking_cron');
175
- }
176
- if(!empty($data[$today])){
177
- return $data[$today];
178
- }elseif(!empty($data[$yesterday])){
179
- return $data[$yesterday];
180
- }else{
181
- return array();
182
- }
183
- }
184
- $GLOBALS['GoogleAnalyticsRanking'] = new GoogleAnalyticsRanking();
185
168
  ```
186
169
  ※追記です。
170
+ デバッグモードでプラグインを有効化すると
171
+ 「Notice: Undefined index: page in」で次の箇所にエラーが表示されます。
172
+ ```ここに言語を入力
173
+ if($_GET['page'] === "{$this->DIR}_reset_page")
174
+ ```
175
+ Notice: Undefined indexのエラー対策として3パターン編集してみました。
176
+ ```ここに言語を入力
187
- どうもアカウントと紐付けはできているけど、データをとってきていないようです。色々調べると配列の値が空という結果になりました。アナリティクスAPIを利用したサンプルから見つけるしかなさそうですが、一番シンプルなものはないですかねぇ…。
177
+ if (isset($_GET['page'] && $_POST['page'] == "{$this->DIR}_reset_page"){
178
+ ```
179
+ の場合、エラー内容:Fatal error: Cannot use isset() on the result of an expression (you can use "null !== expression" instead)
180
+
181
+ ```ここに言語を入力
182
+ if (isset($_GET['page'] === "{$this->DIR}_reset_page")){
183
+ ```
184
+ の場合、エラー内容:Fatal error: Cannot use isset() on the result of an expression (you can use "null !== expression" instead)
185
+
186
+ ```ここに言語を入力
187
+ if (!empty($_GET['page'] === "{$this->DIR}_reset_page")){
188
+ ```
189
+ 「Notice: Undefined index: page in」エラーのまま
190
+
191
+ その他、edumpというデバッグプラグインでは
192
+ 「Only variables should be passed by reference」というエラーが
193
+ ```ここに言語を入力
194
+ $this->DIR = @array_pop(explode("/",dirname(__FILE__)));
195
+ ```
196
+ に出ます。
197
+ こちらの対処方法も分からない状態です。

5

追記の変更

2018/09/08 10:56

投稿

MyQuestioner
MyQuestioner

スコア57

title CHANGED
File without changes
body CHANGED
@@ -184,9 +184,4 @@
184
184
  $GLOBALS['GoogleAnalyticsRanking'] = new GoogleAnalyticsRanking();
185
185
  ```
186
186
  ※追記です。
187
- edumpというデバッグプラグインを使用したところ、
188
- 「Only variables should be passed by reference~」というエラーが
189
- ```ここに言語を入力
190
- $this->DIR = @array_pop(explode("/",dirname(__FILE__)));
187
+ どうもアカウントと紐付けはできているけど、データをとってきていないようです。色々調べると配列の値が空という結果になりました。アナリティクスAPIを利用したサンプルから見つけるしかなさそうですが、一番シンプルなものはないですかねぇ…。
191
- ```
192
- の部分で出ていました。解決策がお分かりになりますでしょうか?

4

追記です

2018/09/07 13:37

投稿

MyQuestioner
MyQuestioner

スコア57

title CHANGED
File without changes
body CHANGED
@@ -183,4 +183,10 @@
183
183
  }
184
184
  $GLOBALS['GoogleAnalyticsRanking'] = new GoogleAnalyticsRanking();
185
185
  ```
186
+ ※追記です。
186
- ここが原因ではないかという箇所指摘頂ければ存じます
187
+ edumpというデバッグプラグイン使用したころ、
188
+ 「Only variables should be passed by reference~」というエラーが
189
+ ```ここに言語を入力
190
+ $this->DIR = @array_pop(explode("/",dirname(__FILE__)));
191
+ ```
192
+ の部分で出ていました。解決策がお分かりになりますでしょうか?

3

リンクの修正

2018/09/07 12:18

投稿

MyQuestioner
MyQuestioner

スコア57

title CHANGED
File without changes
body CHANGED
@@ -1,7 +1,7 @@
1
1
  アナリティクス APIを使用したWPプラグインを数年前に人に作って頂いたのですが、1週間ぐらい前から動作をしなくなりました。
2
2
  アナリティクスAPIと連携して、ランキング形式で表示させるものなのですが、操作をしてもページビュー数等が表示されなくなりました。
3
3
  このプラグインは「Google Analyticator」というプラグインを改良されて作られたらしく、APIライブラリは全く同じものでした。
4
- Google Analyticator](https://ja.wordpress.org/plugins/google-analyticator/Google Analytics)
4
+ [Google Analyticator](https://ja.wordpress.org/plugins/google-analyticator/Google Analytics)
5
5
  メインのphp
6
6
  ```ここに言語を入力
7
7
  <?php

2

コードの追加

2018/09/07 06:27

投稿

MyQuestioner
MyQuestioner

スコア57

title CHANGED
File without changes
body CHANGED
@@ -1,13 +1,186 @@
1
- お世話になります。
2
-
3
- Google アナリティクス APIを使用したWordpressプラグインを数年前に人に作って頂いたのですが、1週間ぐらい前から動作をしなくなりました。
1
+ アナリティクス APIを使用したWPプラグインを数年前に人に作って頂いたのですが、1週間ぐらい前から動作をしなくなりました。
4
- そのプラグインは、Googleアナリティクスと連携して、ページビューが多い順に並べてランキング形式で表示させるものなのですが、操作をしてもページビュー数等が表示されなくなりました。
2
+ アナリティクスAPIと連携して、ランキング形式で表示させるものなのですが、操作をしてもページビュー数等が表示されなくなりました。
5
-
6
- Wordpressをデバッグモードにしてみましたが、原因となるエラーは見つからず、プラグインに備わったデバッグモードでは数値が表示されるところに「FALSE」と表示されます。
7
-
8
- 色々調べてみるとこのプラグインは「Google Analyticator」というプラグインを改良されて作られたもののようで、APIライブラリは全く同じものでした。
3
+ このプラグインは「Google Analyticator」というプラグインを改良されて作られたらしく、APIライブラリは全く同じものでした。
9
- ただ、「Google Analyticator」は動くのですが、当方のプラグインは動きません。
10
- [Google Analyticator](https://ja.wordpress.org/plugins/google-analyticator/Google Analytics)
4
+ Google Analyticator](https://ja.wordpress.org/plugins/google-analyticator/Google Analytics)
11
-
5
+ メインのphp
6
+ ```ここに言語を入力
7
+ <?php
8
+ define('GARCODE',"GoogleAnalyticsRankingCode");
9
+ define('GARTOKEN',"GoogleAnalyticsRankingToken");
10
+ define('GARWPID',"GoogleAnalyticsRankingWebPropertyID");
11
+ define('GARVID',"GoogleAnalyticsRankingViewID");
12
+ define('HIDDEN_IDS',"GoogleAnalyticsRankingHiddenIDs");
13
+ function GoogleAnalyticsRanking_Uninstall(){
14
+ delete_option(GARCODE);
15
+ delete_option(GARTOKEN);
16
+ delete_option(GARWPID);
17
+ delete_option(GARVID);
18
+ delete_option(HIDDEN_IDS);
19
+ wp_clear_scheduled_hook('get_ranking_cron');
20
+ }
21
+ class GoogleAnalyticsRanking {
22
+ function __construct(){
23
+ if(!class_exists('Google_Client')){
24
+ require_once(dirname(__FILE__).'/google-api-php-client/src/Google_Client.php');
25
+ }
26
+ if(!class_exists('Google_AnalyticsService')){
27
+ require_once(dirname(__FILE__).'/google-api-php-client/src/contrib/Google_AnalyticsService.php');
28
+ }
29
+ foreach(json_decode(file_get_contents(dirname(__FILE__).'/client_id/client_id.json'),true) as $k => $v){
30
+ if(is_array($v)){
31
+ foreach($v as $k2 => $v2){
32
+ $this->$k2 = $v2;
33
+ }
34
+ }else{
35
+ $this->$k = $v;
36
+ }
37
+ }
38
+ $this->DIR = @array_pop(explode("/",dirname(__FILE__)));
39
+ $this->NONCE = md5(dirname(__FILE__));
40
+ if(function_exists('register_uninstall_hook'))register_uninstall_hook(__FILE__, 'GoogleAnalyticsRanking_Uninstall');
41
+ add_action('init', array($this,'init'));
42
+ add_action('wp_ajax_get_posts_googleanalyticsranking',array($this,'wp_ajax_get_posts'));
43
+ add_action('get_ranking_cron', array($this, 'get_ranking'));
44
+ }
45
+ function init(){
46
+ $this->client = new Google_Client();
47
+ $this->client->setApprovalPrompt('force');
48
+ $this->client->setAccessType('offline');
49
+ $this->client->setClientId($this->client_id);
50
+ $this->client->setClientSecret($this->client_secret);
51
+ $this->client->setRedirectUri($this->redirect_uris[0]);
52
+ $this->client->setScopes(array('https://www.googleapis.com/auth/analytics.readonly'));
53
+ $this->token = get_option(GARTOKEN);
54
+ $this->WebPropertyID = get_option(GARWPID);
55
+ $this->ViewID = get_option(GARVID);
56
+ if(!empty($this->token)){
57
+ $this->client->setAccessToken(json_encode($this->token));
58
+ $this->analytics = new Google_AnalyticsService($this->client);
59
+ }
60
+ add_action('admin_menu',array($this,'admin_menu'));
61
+ }
62
+ function wp_enqueue(){
63
+ wp_enqueue_script('googleclient','https://apis.google.com/js/client.js?onload=googleApiClientReady',array('jquery'),'',true);
64
+ wp_enqueue_script('option_page',plugins_url($this->DIR).'/assets/js/option_page.js',array('jquery','googleclient'),'',true);
65
+ }
66
+ function admin_menu(){
67
+ $hook = add_menu_page('ランキング','ランキング','edit_others_posts',"{$this->DIR}_options_page",array($this,'options_page'));
68
+ add_submenu_page("{$this->DIR}_options_page",'初期設定','初期設定','edit_others_posts',"{$this->DIR}_options_page",array($this,'options_page'));
69
+ add_action('load-'.$hook,array($this,'options_page_update'));
70
+ add_action('load-'.$hook,array($this,'wp_enqueue'));
71
+ if(empty($this->token)){
12
- 興味がある、またはアドバイスを頂ける方がおられましたら以下のリンクでDL出来ますので、大きな修正が必要なのか少しパラメータを変更するだけで動くものなのかお知らせ頂ければと存じます。
72
+ add_action('load-'.$hook,array($this,'redirect_client_setting_page'));
73
+ $hook = add_submenu_page("{$this->DIR}_options_page",'認証コード設定','認証コード設定','edit_others_posts',"{$this->DIR}_client_setting_page",array($this,'client_setting_page'));
74
+ add_action('load-'.$hook,array($this,'client_setting_page_update'));
75
+ }else{
76
+ remove_action('load-'.$hook,array($this,'redirect_client_setting_page'));
77
+ }
78
+ if($_GET['page'] === "{$this->DIR}_reset_page"){
79
+ delete_option('GoogleAnalyticsRankingData');
80
+ delete_option(GARCODE);
81
+ delete_option(GARTOKEN);
82
+ delete_option(GARWPID);
83
+ delete_option(GARVID);
84
+ wp_safe_redirect(admin_url("admin.php?page={$this->DIR}_client_setting_page&update=reset"));
85
+ exit;
86
+ }
87
+ function options_page(){
88
+ require_once(dirname(__FILE__).'/assets/views/options_page.php');
89
+ }
90
+ function options_page_update(){
91
+ if($_SERVER["REQUEST_METHOD"] == "POST"){
92
+ if(!empty($this->token)){
93
+ $my_nonce = filter_input(INPUT_POST,$this->NONCE)?filter_input(INPUT_POST,$this->NONCE):null;
94
+ if(wp_verify_nonce($my_nonce, wp_create_nonce($this->DIR))){
95
+ $webproperty_id = filter_input(INPUT_POST,'webproperty_id');
96
+ if(!empty($webproperty_id)){
97
+ update_option(GARWPID,$webproperty_id);
98
+ if($view_id = filter_input(INPUT_POST,'view_id')){
99
+ update_option(GARVID,$view_id);
100
+ }else{
101
+ list($pre, $account_id, $post) = explode('-',$webproperty_id);
102
+ $profiles = $this->analytics->management_profiles->listManagementProfiles($account_id,$webproperty_id);
103
+ update_option(GARVID,$profiles['items'][0]['id']);
104
+ }
105
+ delete_option('GoogleAnalyticsRankingData');
106
+ exec("nohup php -c '' '".dirname(__FILE__)."/background.php' > /dev/null &");
107
+ wp_safe_redirect(admin_url("admin.php?page={$_GET['page']}&update=ok"));
108
+ exit;
109
+ }else{
110
+ wp_safe_redirect(admin_url("admin.php?page={$_GET['page']}&update=error"));
111
+ exit;
112
+ }
113
+ }else{
114
+ wp_safe_redirect(admin_url("admin.php?page={$_GET['page']}&update=error"));
115
+ exit;
116
+ }
117
+ }else{
118
+ wp_safe_redirect(admin_url("admin.php?page={$this->DIR}_client_setting_page"));
119
+ exit;
120
+ }
121
+ }
122
+ }
123
+ function client_setting_page(){
13
- [プラグインのダウンロード(ギガファイル便)](https://33.gigafile.nu/0914-bd9919c3db49c0d58c39fbe3aeb5eb176)
124
+ require_once(dirname(__FILE__).'/assets/views/client_setting_page.php');
125
+ }
126
+ function redirect_client_setting_page(){
127
+ wp_safe_redirect(admin_url("admin.php?page={$this->DIR}_client_setting_page"));
128
+ exit;
129
+ }
130
+ function client_setting_page_update(){
131
+ if($_SERVER["REQUEST_METHOD"] == "POST"){
132
+ $my_nonce = filter_input(INPUT_POST,$this->NONCE)?filter_input(INPUT_POST,$this->NONCE):null;
133
+ if(wp_verify_nonce($my_nonce, wp_create_nonce($this->DIR))){
134
+ $code = filter_input(INPUT_POST,'token_key');
135
+ if(!empty($code)){
136
+ $res = file_get_contents($this->token_uri, false, stream_context_create(array('http' => array(
137
+ 'method' => 'POST',
138
+ 'content' => http_build_query(array(
139
+ 'code' => $code,
140
+ 'grant_type' => 'authorization_code',
141
+ 'redirect_uri' => $this->redirect_uris[0],
142
+ 'client_id' => $this->client_id,
143
+ 'client_secret' => $this->client_secret,
144
+ )),
145
+ ))));
146
+ $token = json_decode($res, true);
147
+ if(isset($token['error']) || empty($token)){
148
+ wp_safe_redirect(admin_url("admin.php?page={$this->DIR}_client_setting_page&update=error"));
149
+ exit;
150
+ }
151
+ delete_option(GARCODE);
152
+ delete_option(GARTOKEN);
153
+ if(!add_option(GARCODE,$code,'','no')){
154
+ wp_safe_redirect(admin_url("admin.php?page={$this->DIR}_client_setting_page&update=error"));
155
+ exit;
156
+ }
157
+ if(!add_option(GARTOKEN,$token,'','no')){
158
+ wp_safe_redirect(admin_url("admin.php?page={$this->DIR}_client_setting_page&update=error"));
159
+ exit;
160
+ }
161
+ }
162
+ wp_safe_redirect(admin_url("admin.php?page={$this->DIR}_options_page"));
163
+ exit;
164
+ }
165
+ }
166
+ }
167
+ function getreposrt($name = 'GoogleAnalyticsRankingData'){
168
+ $data = get_option($name);
169
+ $today = date('Y-m-d',strtotime('-1 hour'));
170
+ $yesterday = date('Y-m-d',strtotime('-25 hour'));
171
+ if(empty($data) || (is_array($data) && (empty($data[$today]) || !count($data[$today])))){
172
+ $task_time = strtotime(date('2018-01-01 1:00:00'));
173
+ if ( !wp_next_scheduled('get_ranking_cron')) {
174
+ wp_schedule_event( $task_time, 'daily', 'get_ranking_cron');
175
+ }
176
+ if(!empty($data[$today])){
177
+ return $data[$today];
178
+ }elseif(!empty($data[$yesterday])){
179
+ return $data[$yesterday];
180
+ }else{
181
+ return array();
182
+ }
183
+ }
184
+ $GLOBALS['GoogleAnalyticsRanking'] = new GoogleAnalyticsRanking();
185
+ ```
186
+ ここが原因ではないかという箇所を指摘頂ければと存じます

1

文章の編集

2018/09/07 06:25

投稿

MyQuestioner
MyQuestioner

スコア57

title CHANGED
File without changes
body CHANGED
@@ -10,4 +10,4 @@
10
10
  [Google Analyticator](https://ja.wordpress.org/plugins/google-analyticator/Google Analytics)
11
11
 
12
12
  興味がある、またはアドバイスを頂ける方がおられましたら以下のリンクでDL出来ますので、大きな修正が必要なのか少しパラメータを変更するだけで動くものなのかお知らせ頂ければと存じます。
13
- [プラグインのダウンロード(ギガファイル便)](https://38.gigafile.nu/0914-c0cd66c344a73d22fd87a5e7fed696608)
13
+ [プラグインのダウンロード(ギガファイル便)](https://33.gigafile.nu/0914-bd9919c3db49c0d58c39fbe3aeb5eb176)