SNS Count Cache - Version 0.3.0

Version Description

  • Added: Pocket was included as one of cache targets.
  • Added: function to modify target SNS that share count is cached
  • Added: function to modify term considering posted content as new content in the rush cache.
  • Added: page to display share count for specified all tagets.
  • Added: function to query pages and posts based on SNS share count using specific custom fields in WP_Query and so on.

=

Download this release

Release Info

Developer marubon
Plugin Icon SNS Count Cache
Version 0.3.0
Comparing to
See all releases

Code changes from version 0.2.0 to 0.3.0

admin.php DELETED
@@ -1,252 +0,0 @@
1
- <?php
2
- /*
3
- admin.php
4
-
5
- Description: Option page implementation
6
- Version: 0.2.0
7
- Author: Daisuke Maruyama
8
- Author URI: http://marubon.info/
9
- License: GPL2 or later
10
- License URI: http://www.gnu.org/licenses/gpl-2.0.txt
11
- */
12
-
13
- /*
14
-
15
- Copyright (C) 2014 Daisuke Maruyama
16
-
17
- This program is free software; you can redistribute it and/or
18
- modify it under the terms of the GNU General Public License
19
- as published by the Free Software Foundation; either version 2
20
- of the License, or (at your option) any later version.
21
-
22
- This program is distributed in the hope that it will be useful,
23
- but WITHOUT ANY WARRANTY; without even the implied warranty of
24
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25
- GNU General Public License for more details.
26
-
27
- You should have received a copy of the GNU General Public License
28
- along with this program; if not, write to the Free Software
29
- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
30
-
31
- */
32
-
33
- $count = 1;
34
- $query_args = array(
35
- 'post_type' => array('post', 'page'),
36
- 'post_status' => 'publish',
37
- 'nopaging' => true,
38
- 'update_post_term_cache' => false,
39
- 'update_post_meta_cache' => false
40
- );
41
-
42
- $posts_query = new WP_Query($query_args);
43
- ?>
44
- <div class="wrap">
45
- <h2>SNS Count Cache</h2>
46
- <div class="sns-cnt-cache">
47
- <ul class="tab">
48
- <li class="select"><?php _e('Cache Status', self::DOMAIN) ?></li>
49
- <li><?php _e('Setting', self::DOMAIN) ?></li>
50
- <li><?php _e('Help', self::DOMAIN) ?></li>
51
- </ul>
52
- <ul class="content">
53
- <li>
54
- <table class="view-table">
55
- <thead>
56
- <tr>
57
- <th>No.</th>
58
- <th><?php _e('Target Content', self::DOMAIN) ?></th>
59
- <th><?php _e('Cache Status', self::DOMAIN) ?></th>
60
- </tr>
61
- </thead>
62
- <tbody>
63
-
64
- <?php
65
- if($posts_query->have_posts()) {
66
- while($posts_query->have_posts()){
67
- $posts_query->the_post();
68
- ?>
69
- <tr>
70
- <td><?php echo $count; ?></td>
71
- <td><?php echo get_permalink(get_the_ID()); ?></td>
72
- <?php
73
- $transient_id = self::OPT_BASE_TRANSIENT_PREFIX . get_the_ID();
74
-
75
- if (false === ($sns_counts = get_transient($transient_id))) {
76
- echo '<td class="not-cached">';
77
- _e('not cached', self::DOMAIN);
78
- echo '</td>';
79
- } else {
80
- echo '<td class="cached">';
81
- _e('cached', self::DOMAIN);
82
- echo '</td>';
83
- }
84
-
85
- ?>
86
- </tr>
87
-
88
- <?php
89
- $count++;
90
-
91
- }
92
- }
93
- wp_reset_postdata();
94
- ?>
95
- </tbody>
96
- </table>
97
- </li>
98
- <li class="hide">
99
- <h3>Setting</h3>
100
- <?php
101
- if(isset($_POST["action"]) && $_POST["action"]==='register'){
102
-
103
- $check_interval = $_POST["check_interval"];
104
- $posts_per_check = $_POST["posts_per_check"];
105
- $dynamic_cache = $_POST["dynamic_cache"];
106
-
107
- if(isset($check_interval) && $check_interval && is_numeric($check_interval)){
108
- update_option(self::DB_CHECK_INTERVAL,$check_interval);
109
- }
110
- if(isset($posts_per_check) && $posts_per_check && is_numeric($posts_per_check)){
111
- update_option(self::DB_POSTS_PER_CHECK,$posts_per_check);
112
- }
113
- if(isset($dynamic_cache)){
114
- update_option(self::DB_DYNAMIC_CACHE, $dynamic_cache);
115
- }
116
-
117
- $this->reactivate_plugin();
118
- }
119
-
120
- $check_interval = get_option(self::DB_CHECK_INTERVAL);
121
- $posts_per_check = get_option(self::DB_POSTS_PER_CHECK);
122
- $dynamic_cache = get_option(self::DB_DYNAMIC_CACHE);
123
-
124
- $check_interval = !empty($check_interval) ? intval($check_interval) : self::OPT_BASE_CHECK_INTERVAL;
125
- $posts_per_check = !empty($posts_per_check) ? intval($posts_per_check) : self::OPT_BASE_POSTS_PER_CHECK;
126
- $dynamic_cache = isset($dynamic_cache) ? intval($dynamic_cache) : self::OPT_ACCESS_BASED_CACHE_NONE;
127
-
128
- ?>
129
- <h4><?php _e('Current Parameter', self::DOMAIN) ?></h4>
130
- <p><?php _e('The following describes registered parameters.', self::DOMAIN) ?></p>
131
- <table class="view-table">
132
- <thead>
133
- <tr>
134
- <th><?php _e('Function', self::DOMAIN) ?></th>
135
- <th><?php _e('Parameter', self::DOMAIN) ?></th>
136
- <th><?php _e('Value', self::DOMAIN) ?></th>
137
- </tr>
138
- </thead>
139
- <tbody>
140
- <tr>
141
- <td><?php _e('Base Cache', self::DOMAIN) ?></td><td><?php _e('Interval cheking and caching SNS share count (sec)', self::DOMAIN) ?></td><td><?php echo $check_interval ?></td>
142
- </tr>
143
- <tr>
144
- <td><?php _e('Base Cache', self::DOMAIN) ?></td><td><?php _e('Number of posts to check at a time (posts)', self::DOMAIN) ?></td><td><?php echo $posts_per_check ?></td>
145
- </tr>
146
- <tr>
147
- <td><?php _e('Dynamic Cache', self::DOMAIN) ?></td><td><?php _e('Dynamic caching based on user access', self::DOMAIN) ?></td><td>
148
- <?php
149
- switch($dynamic_cache){
150
- case SNSCountCache::OPT_ACCESS_BASED_CACHE_NONE:
151
- _e('disabled', self::DOMAIN);
152
- break;
153
- case SNSCountCache::OPT_ACCESS_BASED_SYNC_CACHE:
154
- _e('enabled (Synchronous Cache)', self::DOMAIN);
155
- break;
156
- case SNSCountCache::OPT_ACCESS_BASED_ASYNC_CACHE:
157
- _e('enabled (Asynchronous Cache)', self::DOMAIN);
158
- break;
159
- }
160
- ?>
161
- </td>
162
- </tr>
163
- </tbody>
164
- </table>
165
- <h4><?php _e('Register New Parameter', self::DOMAIN) ?></h4>
166
- <p><?php _e('You can register or modify required parameters at the following form.', self::DOMAIN) ?></p>
167
- <form action="" method="post">
168
- <table class="form-table">
169
- <tr>
170
- <th><label><?php _e('Interval cheking and caching SNS share count (sec)', self::DOMAIN) ?></label></th>
171
- <td><input type="text" class="text" name="check_interval" size="60" value="" /></td>
172
- </tr>
173
- <tr>
174
- <th><label><?php _e('Number of posts to check at a time (posts)', self::DOMAIN) ?></label></th>
175
- <td><input type="text" class="text" name="posts_per_check" size="60" value="" /></td>
176
- </tr>
177
- <tr>
178
- <th><label><?php _e('Dynamic caching based on user access', self::DOMAIN) ?></label></th>
179
- <td>
180
- <select name="dynamic cache">
181
- <option value="0"<?php if ($this->dynamic_cache == 0) echo ' selected="selected"'; ?>><?php _e('None', self::DOMAIN) ?></option>
182
- <option value="1"<?php if ($this->dynamic_cache == 1) echo ' selected="selected"'; ?>><?php _e('Synchronous Cache', self::DOMAIN) ?></option>
183
- <option value="2"<?php if ($this->dynamic_cache == 2) echo ' selected="selected"'; ?>><?php _e('Asynchronous Cache', self::DOMAIN) ?></option>
184
- </select>
185
- </td>
186
- </tr>
187
- </table>
188
- <input type="hidden" class="text" name="action" value="register" />
189
- <div class="submit-button">
190
- <input type="submit" class="button button-primary" value="<?php _e('Update Options', self::DOMAIN) ?>" />
191
- </div>
192
- </form>
193
-
194
- </li>
195
- <li class="hide">
196
- <div>
197
- <h3><?php _e('What is SNS Cout Cache?', self::DOMAIN) ?></h3>
198
- <p><?php _e('SNS Count Cache gets share count for Twitter and Facebook, Google Plus, Hatena Bookmark and caches these count in the background. This plugin may help you to shorten page loading time because the share count can be retrieved not through network but through the cache using given functions.', self::DOMAIN) ?></p>
199
- <h3><?php _e('How often does this plugin get and cache share count?', self::DOMAIN) ?></h3>
200
- <p><?php _e('Although this plugin gets share count of 20 posts at a time every 10 minutes by default, you can modify the setting in the "Setting" tab in the setting page.', self::DOMAIN) ?></p>
201
- <h3><?php _e('How can I know whether share cout of each post is cached or not?', self::DOMAIN) ?></h3>
202
- <p><?php _e('Cache status is described in the "Cache Status" tab in the setting page.', self::DOMAIN) ?></p>
203
- <h3><?php _e('How can I get share count from the cache?', self::DOMAIN) ?></h3>
204
- <p><?php _e('The share cout is retrieved from the cache using the following functions in the WordPress loop such as query_posts(), get_posts() and WP_Query().', self::DOMAIN) ?></p>
205
- <table class="view-table">
206
- <thead>
207
- <tr>
208
- <th><?php _e('Function', self::DOMAIN) ?></th>
209
- <th><?php _e('Description', self::DOMAIN) ?></th>
210
- </tr>
211
- </thead>
212
- <tbody>
213
- <tr><td>get_scc_twitter()</td><td><?php _e('Twitter share count is returned from cache.', self::DOMAIN) ?></td></tr>
214
- <tr><td>get_scc_facebook()</td><td><?php _e('Facebook share count is returned from cache.', self::DOMAIN) ?></td></tr>
215
- <tr><td>get_scc_gplus()</td><td><?php _e('Google Plus share count is returned from cache.', self::DOMAIN) ?></td></tr>
216
- <tr><td>get_scc_hatebu()</td><td><?php _e('Hatena Bookmark share count is returned from cache.', self::DOMAIN) ?></td></tr>
217
- </tbody>
218
- </table>
219
- <h3><?php _e('Example Code', self::DOMAIN) ?></h3>
220
- <?php _e('The code below describes a simple example which displays share count of Twitter, Facebook, Google Plus for each post.', self::DOMAIN) ?>
221
- <pre class="prettyprint">&lt;?php
222
- $query_args = array(
223
- &#039;post_type&#039; =&gt; &#039;post&#039;,
224
- &#039;post_status&#039; =&gt; &#039;publish&#039;,
225
- &#039;posts_per_page&#039; =&gt; 5
226
- );
227
-
228
- $posts_query = new WP_Query($query_args);
229
-
230
- if($posts_query-&gt;have_posts()) {
231
- while($posts_query-&gt;have_posts()){
232
- $posts_query-&gt;the_post();
233
- ?&gt;
234
-
235
- &lt;!--
236
- In WordPress loop, you can use the given function
237
- in order to get share count for current post.
238
- --&gt;
239
- &lt;p&gt;Twitter: &lt;?php echo get_scc_twitter(); ?&gt;&lt;/p&gt;
240
- &lt;p&gt;Facebook: &lt;?php echo get_scc_facebook(); ?&gt;&lt;/p&gt;
241
- &lt;p&gt;Google Plus: &lt;?php echo get_scc_gplus(); ?&gt;&lt;/p&gt;
242
-
243
- &lt;?php
244
- }
245
- }
246
- wp_reset_postdata();
247
- ?&gt;</pre>
248
- </div>
249
- </li>
250
- </ul>
251
- </div>
252
- </div>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
css/sns-count-cache.css CHANGED
@@ -1,7 +1,6 @@
1
  .sns-cnt-cache .view-table {
2
  border: 1px solid #000;
3
- margin: 20px 0 0 0;
4
- margin: 0px;
5
  padding: 0px;
6
  width: 100%;
7
  border-collapse: collapse;
@@ -24,10 +23,15 @@
24
  color: #fff;
25
  }
26
 
 
 
 
 
 
27
  .sns-cnt-cache .view-table td {
28
  vertical-align: middle;
29
- padding: 4px;
30
- text-align: left;
31
  }
32
 
33
  .sns-cnt-cache .cached {
@@ -39,6 +43,10 @@
39
  color: #ccc;
40
  }
41
 
 
 
 
 
42
  .sns-cnt-cache .tab {
43
  overflow: hidden;
44
  padding: 0;
@@ -93,6 +101,25 @@
93
  }
94
 
95
  .sns-cnt-cache .submit-button {
96
- margin: 15px 0 0 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97
  }
98
 
 
 
 
1
  .sns-cnt-cache .view-table {
2
  border: 1px solid #000;
3
+ margin: 20px 0;
 
4
  padding: 0px;
5
  width: 100%;
6
  border-collapse: collapse;
23
  color: #fff;
24
  }
25
 
26
+ .sns-cnt-cache .form-table .section-label {
27
+ font-weight: 600;
28
+ font-size: 1.3em;
29
+ }
30
+
31
  .sns-cnt-cache .view-table td {
32
  vertical-align: middle;
33
+ padding: 2px 4px;
34
+ border: 1px dotted #dfdfdf;
35
  }
36
 
37
  .sns-cnt-cache .cached {
43
  color: #ccc;
44
  }
45
 
46
+ .sns-cnt-cache .share-count {
47
+ text-align: right;
48
+ }
49
+
50
  .sns-cnt-cache .tab {
51
  overflow: hidden;
52
  padding: 0;
101
  }
102
 
103
  .sns-cnt-cache .submit-button {
104
+ margin: 20px 0;
105
+ }
106
+
107
+ .sns-cnt-cache h3 {
108
+ font-size: 1.5em;
109
+ }
110
+
111
+ .sns-cnt-cache a {
112
+ text-decoration: none;
113
+ color: #444;
114
+ -webkit-transition-property: border,background,color;
115
+ transition-property: border,background,color;
116
+ -webkit-transition-duration: .05s;
117
+ transition-duration: .05s;
118
+ -webkit-transition-timing-function: ease-in-out;
119
+ transition-timing-function: ease-in-out;
120
+ text-decoration: none;
121
  }
122
 
123
+ .sns-cnt-cache a:hover {
124
+ color: #0074a2;
125
+ }
includes/admin.php ADDED
@@ -0,0 +1,524 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /*
3
+ admin.php
4
+
5
+ Description: Option page implementation
6
+ Version: 0.3.0
7
+ Author: Daisuke Maruyama
8
+ Author URI: http://marubon.info/
9
+ License: GPL2 or later
10
+ License URI: http://www.gnu.org/licenses/gpl-2.0.txt
11
+ */
12
+
13
+ /*
14
+
15
+ Copyright (C) 2014 Daisuke Maruyama
16
+
17
+ This program is free software; you can redistribute it and/or
18
+ modify it under the terms of the GNU General Public License
19
+ as published by the Free Software Foundation; either version 2
20
+ of the License, or (at your option) any later version.
21
+
22
+ This program is distributed in the hope that it will be useful,
23
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
24
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25
+ GNU General Public License for more details.
26
+
27
+ You should have received a copy of the GNU General Public License
28
+ along with this program; if not, write to the Free Software
29
+ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
30
+
31
+ */
32
+
33
+
34
+
35
+ if( isset( $_POST["action"] ) && $_POST["action"] === 'register' ) {
36
+ $check_interval = $_POST["check_interval"];
37
+ $posts_per_check = $_POST["posts_per_check"];
38
+ $dynamic_cache = $_POST["dynamic_cache"];
39
+ $new_content_term = $_POST["new_content_term"];
40
+
41
+ $base_cache_target_twitter = $_POST["base_cache_target_twitter"];
42
+ $base_cache_target_facebook = $_POST["base_cache_target_facebook"];
43
+ $base_cache_target_gplus = $_POST["base_cache_target_gplus"];
44
+ $base_cache_target_pocket = $_POST["base_cache_target_pocket"];
45
+ $base_cache_target_hatebu = $_POST["base_cache_target_hatebu"];
46
+
47
+ if ( isset( $check_interval ) && $check_interval && is_numeric( $check_interval ) ) {
48
+ update_option(self::DB_CHECK_INTERVAL, $check_interval);
49
+ }
50
+ if ( isset( $posts_per_check ) && $posts_per_check && is_numeric( $posts_per_check ) ) {
51
+ update_option(self::DB_POSTS_PER_CHECK, $posts_per_check);
52
+ }
53
+ if ( isset( $dynamic_cache ) ) {
54
+ update_option(self::DB_DYNAMIC_CACHE, $dynamic_cache);
55
+ }
56
+ if ( isset( $new_content_term ) && $new_content_term && is_numeric( $new_content_term ) ) {
57
+ update_option(self::DB_NEW_CONTENT_TERM, $new_content_term);
58
+ }
59
+
60
+ if (isset($base_cache_target_twitter) && $base_cache_target_twitter) {
61
+ $base_cache_target[self::REF_TWITTER] = true;
62
+ }
63
+ if ( isset( $base_cache_target_facebook ) && $base_cache_target_facebook ) {
64
+ $base_cache_target[self::REF_FACEBOOK] = true;
65
+ }
66
+ if ( isset( $base_cache_target_gplus ) && $base_cache_target_gplus ) {
67
+ $base_cache_target[self::REF_GPLUS] = true;
68
+ }
69
+ if ( isset( $base_cache_target_pocket ) && $base_cache_target_pocket ) {
70
+ if ( extension_loaded( 'xml' ) ) {
71
+ $base_cache_target[self::REF_POCKET] = true;
72
+ }
73
+ }
74
+ if ( isset( $base_cache_target_hatebu ) && $base_cache_target_hatebu ) {
75
+ $base_cache_target[self::REF_HATEBU] = true;
76
+ }
77
+
78
+ if ( ! empty( $base_cache_target ) ) {
79
+ update_option(self::DB_CACHE_TARGET,$base_cache_target);
80
+ }
81
+
82
+ $this->reactivate_plugin();
83
+ }
84
+
85
+ $check_interval = get_option( self::DB_CHECK_INTERVAL );
86
+ $posts_per_check = get_option( self::DB_POSTS_PER_CHECK );
87
+ $dynamic_cache = get_option( self::DB_DYNAMIC_CACHE );
88
+ $new_content_term = get_option( self::DB_NEW_CONTENT_TERM );
89
+ $base_cache_target = get_option( self::DB_CACHE_TARGET );
90
+
91
+ $check_interval = ! empty( $check_interval ) ? intval( $check_interval ) : self::OPT_BASE_CHECK_INTERVAL;
92
+ $posts_per_check = ! empty( $posts_per_check ) ? intval( $posts_per_check ) : self::OPT_BASE_POSTS_PER_CHECK;
93
+ $dynamic_cache = isset( $dynamic_cache ) ? intval( $dynamic_cache ) : self::OPT_ACCESS_BASED_CACHE_NONE;
94
+ $new_content_term = ! empty( $new_content_term ) ? intval( $new_content_term ) : self::OPT_RUSH_NEW_CONTENT_TERM;
95
+
96
+ if ( empty( $base_cache_target ) ) {
97
+ $base_cache_target[self::REF_TWITTER] = true;
98
+ $base_cache_target[self::REF_FACEBOOK] = true;
99
+ $base_cache_target[self::REF_GPLUS] = true;
100
+ $base_cache_target[self::REF_POCKET] = true;
101
+ $base_cache_target[self::REF_HATEBU] = true;
102
+ }
103
+
104
+ $count = 1;
105
+ $query_args = array(
106
+ 'post_type' => array( 'post', 'page' ),
107
+ 'post_status' => 'publish',
108
+ 'nopaging' => true,
109
+ 'update_post_term_cache' => false,
110
+ 'update_post_meta_cache' => false
111
+ );
112
+
113
+ $posts_query = new WP_Query( $query_args );
114
+ ?>
115
+ <div class="wrap">
116
+ <h2>SNS Count Cache</h2>
117
+ <div class="sns-cnt-cache">
118
+ <ul class="tab">
119
+ <li class="select"><?php _e( 'Cache Status', self::DOMAIN ) ?></li>
120
+ <li><?php _e( 'Share Count', self::DOMAIN ) ?></li>
121
+ <li><?php _e( 'Setting', self::DOMAIN ) ?></li>
122
+ <li><?php _e( 'Help', self::DOMAIN ) ?></li>
123
+ </ul>
124
+ <ul class="content">
125
+ <li>
126
+ <table class="view-table">
127
+ <thead>
128
+ <tr>
129
+ <th>No.</th>
130
+ <th><?php _e( 'Target Content', self::DOMAIN ) ?></th>
131
+ <th><?php _e( 'Primary Cache', self::DOMAIN ) ?></th>
132
+ <th><?php _e( 'Secondary Cache', self::DOMAIN ) ?></th>
133
+
134
+ </tr>
135
+ </thead>
136
+ <tbody>
137
+
138
+ <?php
139
+ if ( $posts_query->have_posts() ) {
140
+ while ( $posts_query->have_posts() ) {
141
+ $posts_query->the_post();
142
+ ?>
143
+ <tr>
144
+ <td><?php echo $count; ?></td>
145
+ <td><a href="<?php echo esc_url( get_permalink( get_the_ID() ) ); ?>" target="_blank"><?php echo esc_html( get_permalink( get_the_ID() ) ); ?></a></td>
146
+ <?php
147
+ $transient_id = self::OPT_BASE_TRANSIENT_PREFIX . get_the_ID();
148
+
149
+ if ( false === ( $sns_counts = get_transient( $transient_id ) ) ) {
150
+ echo '<td class="not-cached">';
151
+ _e( 'not cached', self::DOMAIN );
152
+ echo '</td>';
153
+ } else {
154
+ echo '<td class="cached">';
155
+ _e( 'cached', self::DOMAIN );
156
+ echo '</td>';
157
+ }
158
+
159
+ $second_cache_flag = true;
160
+ foreach ( $base_cache_target as $key => $value ) {
161
+ if ( $value ) {
162
+
163
+ $meta_key = SNS_Count_Cache::OPT_2ND_META_KEY_PREFIX . strtolower( $key );
164
+ $sns_count = get_post_meta( get_the_ID(), $meta_key, true );
165
+
166
+ if ( $sns_count == -1 ) {
167
+ $second_cache_flag = false;
168
+ }
169
+ }
170
+ }
171
+ if ( $second_cache_flag ) {
172
+ echo '<td class="cached">';
173
+ _e( 'cached', self::DOMAIN );
174
+ echo '</td>';
175
+ } else {
176
+ echo '<td class="not-cached">';
177
+ _e( 'not cached', self::DOMAIN );
178
+ echo '</td>';
179
+ }
180
+
181
+ ?>
182
+ </tr>
183
+
184
+ <?php
185
+ $count++;
186
+
187
+ }
188
+ }
189
+ wp_reset_postdata();
190
+ ?>
191
+ </tbody>
192
+ </table>
193
+ </li>
194
+ <li class="hide">
195
+ <table class="view-table">
196
+ <thead>
197
+ <tr>
198
+ <th>No.</th>
199
+ <th><?php _e( 'Content', self::DOMAIN ) ?></th>
200
+ <?php
201
+
202
+ foreach ( $base_cache_target as $key => $value ){
203
+ if ( $value ) {
204
+ echo '<th>' . $key . '</th>';
205
+ }
206
+ }
207
+
208
+ ?>
209
+ </tr>
210
+ </thead>
211
+ <tbody>
212
+
213
+ <?php
214
+ $count = 1;
215
+ if ( $posts_query->have_posts() ) {
216
+ while ( $posts_query->have_posts() ) {
217
+ $posts_query->the_post();
218
+ ?>
219
+ <tr>
220
+ <td><?php echo $count; ?></td>
221
+ <td><a href="<?php echo esc_url( get_permalink( get_the_ID() ) ); ?>" target="_blank"><?php echo esc_html( get_the_title( get_the_ID() ) ); ?></a></td>
222
+ <?php
223
+ $transient_id = self::OPT_BASE_TRANSIENT_PREFIX . get_the_ID();
224
+
225
+ if ( false === ( $sns_counts = get_transient( $transient_id ) ) ) {
226
+
227
+ foreach ( $base_cache_target as $key => $value ) {
228
+ if( $value ){
229
+ /**
230
+ echo '<td class="not-cached share-count">';
231
+ _e( 'N/A', self::DOMAIN );
232
+ echo '</td>';
233
+ */
234
+
235
+ $meta_key = SNS_Count_Cache::OPT_2ND_META_KEY_PREFIX . strtolower( $key );
236
+ $sns_counts[$key] = get_post_meta( get_the_ID(), $meta_key, true );
237
+
238
+ if ( isset( $sns_counts[$key] ) && $sns_counts[$key] >= 0 ) {
239
+ echo '<td class="share-count">';
240
+ echo $sns_counts[$key];
241
+ echo '</td>';
242
+ } else {
243
+ echo '<td class="not-cached share-count">';
244
+ _e( 'N/A', self::DOMAIN );
245
+ echo '</td>';
246
+ }
247
+ }
248
+ }
249
+
250
+ } else {
251
+
252
+ foreach ( $base_cache_target as $key => $value ) {
253
+ if ( $value ) {
254
+ if ( isset( $sns_counts[$key] ) ) {
255
+ echo '<td class="share-count">';
256
+ echo $sns_counts[$key];
257
+ echo '</td>';
258
+ } else {
259
+ echo '<td class="not-cached share-count">';
260
+ _e( 'N/A', self::DOMAIN );
261
+ echo '</td>';
262
+ }
263
+ }
264
+ }
265
+
266
+ }
267
+ ?>
268
+ </tr>
269
+
270
+ <?php
271
+ $count++;
272
+
273
+ }
274
+ }
275
+ wp_reset_postdata();
276
+ ?>
277
+ </tbody>
278
+ </table>
279
+ </li>
280
+ <li class="hide">
281
+ <h3><?php _e( 'Current Parameter', self::DOMAIN ) ?></h3>
282
+ <p><?php _e( 'The following describes registered parameters.', self::DOMAIN ) ?></p>
283
+ <table class="view-table">
284
+ <thead>
285
+ <tr>
286
+ <th><?php _e( 'Function', self::DOMAIN ) ?></th>
287
+ <th><?php _e( 'Parameter', self::DOMAIN ) ?></th>
288
+ <th><?php _e( 'Value', self::DOMAIN ) ?></th>
289
+ </tr>
290
+ </thead>
291
+ <tbody>
292
+ <tr>
293
+ <td><?php _e( 'Base Cache', self::DOMAIN) ?></td><td><?php _e('Target SNS', self::DOMAIN ) ?></td>
294
+ <td>
295
+ <?php
296
+ $target_sns = array();
297
+ if ( isset( $base_cache_target[self::REF_TWITTER] ) && $base_cache_target[self::REF_TWITTER] ) {
298
+ $target_sns[] = 'Twitter';
299
+ }
300
+ if ( isset( $base_cache_target[self::REF_FACEBOOK] ) && $base_cache_target[self::REF_FACEBOOK] ) {
301
+ $target_sns[] = 'Facebook';
302
+ }
303
+ if ( isset( $base_cache_target[self::REF_GPLUS] ) && $base_cache_target[self::REF_GPLUS] ) {
304
+ $target_sns[] = 'Google+';
305
+ }
306
+ if ( isset( $base_cache_target[self::REF_POCKET] ) && $base_cache_target[self::REF_POCKET] ) {
307
+ $target_sns[] = 'Pocket';
308
+ }
309
+ if ( isset( $base_cache_target[self::REF_HATEBU] ) && $base_cache_target[self::REF_HATEBU] ) {
310
+ $target_sns[] = 'Hatena Bookmark';
311
+ }
312
+ echo implode( ", ", $target_sns );
313
+ ?>
314
+ </td>
315
+ </tr>
316
+ <tr>
317
+ <td><?php _e( 'Base Cache', self::DOMAIN ) ?></td>
318
+ <td><?php _e( 'Interval cheking and caching SNS share count', self::DOMAIN ) ?></td>
319
+ <td><?php echo $check_interval . ' seconds'; ?></td>
320
+ </tr>
321
+ <tr>
322
+ <td><?php _e( 'Base Cache', self::DOMAIN ) ?></td>
323
+ <td><?php _e( 'Number of posts to check at a time', self::DOMAIN ) ?></td>
324
+ <td><?php echo $posts_per_check . ' posts'; ?></td>
325
+ </tr>
326
+ <tr>
327
+ <td><?php _e( 'Rush Cache', self::DOMAIN ) ?></td>
328
+ <td><?php _e( 'Term considering posted content as new content', self::DOMAIN ) ?></td>
329
+ <td>
330
+ <?php
331
+ if ( $new_content_term == 1 ) {
332
+ echo $new_content_term . ' day';
333
+ } else if ( $new_content_term > 1 ) {
334
+ echo $new_content_term . ' days';
335
+ }
336
+ ?>
337
+ </td>
338
+ </tr>
339
+ <tr>
340
+ <td><?php _e( 'Dynamic Cache', self::DOMAIN) ?></td><td><?php _e( 'Dynamic caching based on user access', self::DOMAIN ) ?></td><td>
341
+ <?php
342
+ switch ( $dynamic_cache ) {
343
+ case SNS_Count_Cache::OPT_ACCESS_BASED_CACHE_NONE:
344
+ _e( 'disabled', self::DOMAIN );
345
+ break;
346
+ case SNS_Count_Cache::OPT_ACCESS_BASED_SYNC_CACHE:
347
+ _e( 'enabled (Synchronous Cache)', self::DOMAIN );
348
+ break;
349
+ case SNS_Count_Cache::OPT_ACCESS_BASED_ASYNC_CACHE:
350
+ _e( 'enabled (Asynchronous Cache)', self::DOMAIN );
351
+ case SNS_Count_Cache::OPT_ACCESS_BASED_2ND_CACHE:
352
+ _e( 'enabled (Asynchronous 2nd Cache)', self::DOMAIN );
353
+ break;
354
+ }
355
+ ?>
356
+ </td>
357
+ </tr>
358
+ </tbody>
359
+ </table>
360
+ <h3><?php _e( 'Register New Parameter', self::DOMAIN ) ?></h3>
361
+ <p><?php _e( 'You can register or modify required parameters at the following form.', self::DOMAIN ) ?></p>
362
+ <form action="" method="post">
363
+ <table class="form-table">
364
+ <tr><th class="section-label"><?php _e('Base Cache', self::DOMAIN) ?></th></tr>
365
+ <tr>
366
+ <th><label><?php _e( 'Target SNS', self::DOMAIN ) ?></label></th>
367
+ <td>
368
+ <div class="sns-check">
369
+ <input type="checkbox" value="1" name="base_cache_target_twitter"<?php if ( $base_cache_target[self::REF_TWITTER] ) echo ' checked="checked"'; ?> />
370
+ <label><?php _e( 'Twitter', self::DOMAIN ) ?></label>
371
+ </div>
372
+ <div class="sns-check">
373
+ <input type="checkbox" value="1" name="base_cache_target_facebook"<?php if ( $base_cache_target[self::REF_FACEBOOK] ) echo ' checked="checked"'; ?> />
374
+ <label><?php _e( 'Facebook', self::DOMAIN ) ?></label>
375
+ </div>
376
+ <div class="sns-check">
377
+ <input type="checkbox" value="1" name="base_cache_target_gplus"<?php if ( $base_cache_target[self::REF_GPLUS] ) echo ' checked="checked"'; ?> />
378
+ <label><?php _e( 'Google+', self::DOMAIN ) ?></label>
379
+ </div>
380
+ <div class="sns-check">
381
+ <input type="checkbox" value="1" name="base_cache_target_pocket"<?php if ( $base_cache_target[self::REF_POCKET] ) echo ' checked="checked"'; ?> />
382
+ <label><?php _e( 'Pocket', self::DOMAIN ) ?></label>
383
+ </div>
384
+ <div class="sns-check">
385
+ <input type="checkbox" value="1" name="base_cache_target_hatebu"<?php if ( $base_cache_target[self::REF_HATEBU] ) echo ' checked="checked"'; ?> />
386
+ <label><?php _e( 'Hatena Bookmark', self::DOMAIN ) ?></label>
387
+ </div>
388
+ </td>
389
+ </tr>
390
+ <tr>
391
+ <th><label><?php _e( 'Interval cheking and caching SNS share count (sec)', self::DOMAIN ) ?></label></th>
392
+ <td>
393
+ <input type="text" class="text" name="check_interval" size="20" value="" />
394
+ <label><?php _e( 'Default: 600', self::DOMAIN ) ?></label>
395
+ </td>
396
+ </tr>
397
+ <tr>
398
+ <th><label><?php _e( 'Number of posts to check at a time (posts)', self::DOMAIN ) ?></label></th>
399
+ <td>
400
+ <input type="text" class="text" name="posts_per_check" size="20" value="" />
401
+ <label><?php _e( 'Default: 20', self::DOMAIN ) ?></label>
402
+ </td>
403
+ </tr>
404
+ <tr><th class="section-label"><?php _e('Rush Cache', self::DOMAIN) ?></th></tr>
405
+ <tr>
406
+ <th><label><?php _e( 'Term considering posted content as new content', self::DOMAIN ) ?></label></th>
407
+ <td>
408
+ <select name="new_content_term">
409
+ <option value="1"<?php if ( $new_content_term == 1 ) echo ' selected="selected"'; ?>>1 day</option>
410
+ <option value="2"<?php if ( $new_content_term == 2 ) echo ' selected="selected"'; ?>>2 days</option>
411
+ <option value="3"<?php if ( $new_content_term == 3 ) echo ' selected="selected"'; ?>>3 days</option>
412
+ <option value="4"<?php if ( $new_content_term == 4 ) echo ' selected="selected"'; ?>>4 days</option>
413
+ <option value="5"<?php if ( $new_content_term == 5 ) echo ' selected="selected"'; ?>>5 days</option>
414
+ <option value="6"<?php if ( $new_content_term == 6 ) echo ' selected="selected"'; ?>>6 days</option>
415
+ <option value="7"<?php if ( $new_content_term == 7 ) echo ' selected="selected"'; ?>>7 days</option>
416
+ <option value="8"<?php if ( $new_content_term == 8 ) echo ' selected="selected"'; ?>>8 days</option>
417
+ <option value="9"<?php if ( $new_content_term == 9 ) echo ' selected="selected"'; ?>>9 days</option>
418
+ <option value="10"<?php if ( $new_content_term == 10 ) echo ' selected="selected"'; ?>>10 days</option>
419
+ <option value="11"<?php if ( $new_content_term == 11 ) echo ' selected="selected"'; ?>>11 days</option>
420
+ <option value="12"<?php if ( $new_content_term == 12 ) echo ' selected="selected"'; ?>>12 days</option>
421
+ <option value="13"<?php if ( $new_content_term == 13 ) echo ' selected="selected"'; ?>>13 days</option>
422
+ <option value="14"<?php if ( $new_content_term == 14 ) echo ' selected="selected"'; ?>>14 days</option>
423
+ </select>
424
+ <label><?php _e( 'Default: 3 days', self::DOMAIN ) ?></label>
425
+ </td>
426
+ </tr>
427
+ <tr><th class="section-label"><?php _e( 'Dynamic Cache', self::DOMAIN ) ?></th></tr>
428
+ <tr>
429
+ <th><label><?php _e( 'Dynamic caching based on user access', self::DOMAIN ) ?></label></th>
430
+ <td>
431
+ <select name="dynamic cache">
432
+ <option value="0"<?php if ( $dynamic_cache == 0 ) echo ' selected="selected"'; ?>><?php _e( 'None', self::DOMAIN ) ?></option>
433
+ <option value="1"<?php if ( $dynamic_cache == 1 ) echo ' selected="selected"'; ?>><?php _e( 'Synchronous Cache', self::DOMAIN ) ?></option>
434
+ <option value="2"<?php if ( $dynamic_cache == 2 ) echo ' selected="selected"'; ?>><?php _e( 'Asynchronous Cache', self::DOMAIN ) ?></option>
435
+ <option value="3"<?php if ( $dynamic_cache == 3 ) echo ' selected="selected"'; ?>><?php _e( 'Asynchronous 2nd Cache', self::DOMAIN ) ?></option>
436
+ </select>
437
+ <label><?php _e('Default: None', self::DOMAIN) ?></label>
438
+ </td>
439
+ </tr>
440
+ </table>
441
+ <input type="hidden" class="text" name="action" value="register" />
442
+ <div class="submit-button">
443
+ <input type="submit" class="button button-primary" value="<?php _e( 'Update Options', self::DOMAIN ) ?>" />
444
+ </div>
445
+ </form>
446
+
447
+ </li>
448
+ <li class="hide">
449
+ <div>
450
+ <h3><?php _e( 'What is SNS Cout Cache?', self::DOMAIN ) ?></h3>
451
+ <p><?php _e( 'SNS Count Cache gets share count for Twitter and Facebook, Google Plus, Hatena Bookmark and caches these count in the background. This plugin may help you to shorten page loading time because the share count can be retrieved not through network but through the cache using given functions.', self::DOMAIN ) ?></p>
452
+ <h3><?php _e( 'How often does this plugin get and cache share count?', self::DOMAIN) ?></h3>
453
+ <p><?php _e( 'Although this plugin gets share count of 20 posts at a time every 10 minutes by default, you can modify the setting in the "Setting" tab in the setting page.', self::DOMAIN ) ?></p>
454
+ <h3><?php _e( 'How can I know whether share cout of each post is cached or not?', self::DOMAIN) ?></h3>
455
+ <p><?php _e( 'Cache status is described in the "Cache Status" tab in the setting page.', self::DOMAIN ) ?></p>
456
+ <h3><?php _e( 'How can I get share count from the cache?', self::DOMAIN) ?></h3>
457
+ <p><?php _e( 'The share count is retrieved from the cache using the following functions in the WordPress loop such as query_posts(), get_posts() and WP_Query().', self::DOMAIN ) ?></p>
458
+ <table class="view-table">
459
+ <thead>
460
+ <tr>
461
+ <th><?php _e( 'Function', self::DOMAIN ) ?></th>
462
+ <th><?php _e( 'Description', self::DOMAIN ) ?></th>
463
+ </tr>
464
+ </thead>
465
+ <tbody>
466
+ <tr><td>get_scc_twitter()</td><td><?php _e( 'Twitter share count is returned from cache.', self::DOMAIN ) ?></td></tr>
467
+ <tr><td>get_scc_facebook()</td><td><?php _e( 'Facebook share count is returned from cache.', self::DOMAIN ) ?></td></tr>
468
+ <tr><td>get_scc_gplus()</td><td><?php _e( 'Google Plus share count is returned from cache.', self::DOMAIN ) ?></td></tr>
469
+ <tr><td>get_scc_hatebu()</td><td><?php _e( 'Hatena Bookmark share count is returned from cache.', self::DOMAIN ) ?></td></tr>
470
+ <tr><td>get_scc_pocket()</td><td><?php _e( 'Pocket share count is returned from cache.', self::DOMAIN ) ?></td></tr>
471
+ </tbody>
472
+ </table>
473
+ <h3><?php _e( 'Example Code', self::DOMAIN ) ?></h3>
474
+ <?php _e( 'The code below describes a simple example which displays share count of Twitter, Facebook, Google Plus for each post.', self::DOMAIN ) ?>
475
+ <pre class="prettyprint">&lt;?php
476
+ $query_args = array(
477
+ &#039;post_type&#039; =&gt; &#039;post&#039;,
478
+ &#039;post_status&#039; =&gt; &#039;publish&#039;,
479
+ &#039;posts_per_page&#039; =&gt; 5
480
+ );
481
+
482
+ $posts_query = new WP_Query( $query_args );
483
+
484
+ if ( $posts_query-&gt;have_posts() ) {
485
+ while ( $posts_query-&gt;have_posts() ){
486
+ $posts_query-&gt;the_post();
487
+ ?&gt;
488
+
489
+ &lt;!--
490
+ In WordPress loop, you can use the given function
491
+ in order to get share count for current post.
492
+ --&gt;
493
+ &lt;p&gt;Twitter: &lt;?php echo get_scc_twitter(); ?&gt;&lt;/p&gt;
494
+ &lt;p&gt;Facebook: &lt;?php echo get_scc_facebook(); ?&gt;&lt;/p&gt;
495
+ &lt;p&gt;Google Plus: &lt;?php echo get_scc_gplus(); ?&gt;&lt;/p&gt;
496
+ &lt;p&gt;Pocket: &lt;?php echo get_scc_pocket(); ?&gt;&lt;/p&gt;
497
+
498
+ &lt;?php
499
+ }
500
+ }
501
+ wp_reset_postdata();
502
+ ?&gt;</pre>
503
+ <h3><?php _e( 'How can I access specific custom field containing each share count?', self::DOMAIN) ?></h3>
504
+ <p><?php _e( 'The custom field including share count is accessed using the following meta keys.', self::DOMAIN ) ?></p>
505
+ <table class="view-table">
506
+ <thead>
507
+ <tr>
508
+ <th><?php _e( 'Meta Key', self::DOMAIN ) ?></th>
509
+ <th><?php _e( 'Description', self::DOMAIN ) ?></th>
510
+ </tr>
511
+ </thead>
512
+ <tbody>
513
+ <tr><td>scc_share_count_twitter</td><td><?php _e( 'A meta key for Twitter share count', self::DOMAIN ) ?></td></tr>
514
+ <tr><td>scc_share_count_facebook</td><td><?php _e( 'A meta key for Facebook share count', self::DOMAIN ) ?></td></tr>
515
+ <tr><td>scc_share_count_google+</td><td><?php _e( 'A meta key for Google Plus share count', self::DOMAIN ) ?></td></tr>
516
+ <tr><td>scc_share_count_hatebu</td><td><?php _e( 'A meta key for Hatena Bookmark share count', self::DOMAIN ) ?></td></tr>
517
+ <tr><td>scc_share_count_pocket</td><td><?php _e( 'A meta key for Pocket share count', self::DOMAIN ) ?></td></tr>
518
+ </tbody>
519
+ </table>
520
+ </div>
521
+ </li>
522
+ </ul>
523
+ </div>
524
+ </div>
includes/class-common-util.php ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /*
3
+ class-common-util.php
4
+
5
+ Description: This class is a data cache engine whitch get and cache data using wp-cron at regular intervals
6
+ Version: 0.3.0
7
+ Author: Daisuke Maruyama
8
+ Author URI: http://marubon.info/
9
+ License: GPL2 or later
10
+ License URI: http://www.gnu.org/licenses/gpl-2.0.txt
11
+ */
12
+
13
+ /*
14
+
15
+ Copyright (C) 2014 Daisuke Maruyama
16
+
17
+ This program is free software; you can redistribute it and/or
18
+ modify it under the terms of the GNU General Public License
19
+ as published by the Free Software Foundation; either version 2
20
+ of the License, or (at your option) any later version.
21
+
22
+ This program is distributed in the hope that it will be useful,
23
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
24
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25
+ GNU General Public License for more details.
26
+
27
+ You should have received a copy of the GNU General Public License
28
+ along with this program; if not, write to the Free Software
29
+ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
30
+
31
+ */
32
+
33
+ class Common_Util {
34
+
35
+ /**
36
+ * Class constarctor
37
+ * Hook onto all of the actions and filters needed by the plugin.
38
+ *
39
+ */
40
+ protected function __construct() {
41
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
42
+
43
+ }
44
+
45
+ /**
46
+ * Output log message according to WP_DEBUG setting
47
+ *
48
+ * @since 0.1.0
49
+ */
50
+ public static function log( $message ) {
51
+ if ( WP_DEBUG === true ) {
52
+ if ( is_array( $message ) || is_object( $message ) ) {
53
+ error_log( print_r( $message, true ) );
54
+ } else {
55
+ error_log( $message );
56
+ }
57
+ }
58
+ }
59
+
60
+
61
+ }
62
+
63
+ ?>
includes/class-data-cache-engine.php ADDED
@@ -0,0 +1,945 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /*
3
+ class-data-cache-engine.php
4
+
5
+ Description: This class is a data cache engine whitch get and cache data using wp-cron at regular intervals
6
+ Version: 0.3.0
7
+ Author: Daisuke Maruyama
8
+ Author URI: http://marubon.info/
9
+ License: GPL2 or later
10
+ License URI: http://www.gnu.org/licenses/gpl-2.0.txt
11
+ */
12
+
13
+ /*
14
+
15
+ Copyright (C) 2014 Daisuke Maruyama
16
+
17
+ This program is free software; you can redistribute it and/or
18
+ modify it under the terms of the GNU General Public License
19
+ as published by the Free Software Foundation; either version 2
20
+ of the License, or (at your option) any later version.
21
+
22
+ This program is distributed in the hope that it will be useful,
23
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
24
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25
+ GNU General Public License for more details.
26
+
27
+ You should have received a copy of the GNU General Public License
28
+ along with this program; if not, write to the Free Software
29
+ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
30
+
31
+ */
32
+
33
+ class Data_Cache_Engine {
34
+
35
+ /**
36
+ * Instance of crawler to get data
37
+ */
38
+ private $crawler = NULL;
39
+
40
+ /**
41
+ * Interval cheking and caching target data
42
+ */
43
+ private $base_check_interval = 600;
44
+
45
+ /**
46
+ * Number of posts to check at a time
47
+ */
48
+ private $base_posts_per_check = 20;
49
+
50
+ /**
51
+ * Prefix of cache ID
52
+ */
53
+ private $base_transient_prefix = 'base_data_cache';
54
+
55
+ /**
56
+ * Cron name to schedule cache processing
57
+ */
58
+ private $base_cache_prime_cron = 'base_data_cache_prime';
59
+
60
+ /**
61
+ * Cron name to execute cache processing
62
+ */
63
+ private $base_cache_execute_cron = 'base_data_cache_exec';
64
+
65
+ /**
66
+ * Schedule name for cache processing
67
+ */
68
+ private $base_event_schedule = 'base_cache_event';
69
+
70
+ /**
71
+ * Schedule description for cache processing
72
+ */
73
+ private $base_event_description = 'base cache event';
74
+
75
+ /**
76
+ * Cache target
77
+ */
78
+ private $base_cache_target = array();
79
+
80
+ /**
81
+ * Offset suffix
82
+ */
83
+ private $base_offset_suffix = 'base_offset';
84
+
85
+ /**
86
+ * Interval cheking and caching target data
87
+ */
88
+ private $rush_check_interval = 300;
89
+
90
+ /**
91
+ * Number of posts to check at a time
92
+ */
93
+ private $rush_posts_per_check = 20;
94
+
95
+ /**
96
+ * Prefix of cache ID
97
+ */
98
+ private $rush_transient_prefix = 'rush_data_cache';
99
+
100
+ /**
101
+ * Cron name to schedule cache processing
102
+ */
103
+ private $rush_cache_prime_cron = 'rush_data_cache_prime';
104
+
105
+ /**
106
+ * Cron name to execute cache processing
107
+ */
108
+ private $rush_cache_execute_cron = 'rush_data_cache_exec';
109
+
110
+ /**
111
+ * Schedule name for cache processing
112
+ */
113
+ private $rush_event_schedule = 'rush_cache_event';
114
+
115
+ /**
116
+ * Schedule description for cache processing
117
+ */
118
+ private $rush_event_description = 'rush cache event';
119
+
120
+ /**
121
+ * Offset suffix
122
+ */
123
+ private $rush_offset_suffix = 'rush_offset';
124
+
125
+ /**
126
+ * Term considered as new content
127
+ */
128
+ private $rush_new_content_term = 3;
129
+
130
+ /**
131
+ * Latency suffix
132
+ */
133
+ private $lazy_check_latency = 10;
134
+
135
+ /**
136
+ * Cron name to execute cache processing
137
+ */
138
+ private $lazy_cache_execute_cron = 'lazy_data_cache_exec';
139
+
140
+ /**
141
+ * Interval checking ranking
142
+ */
143
+ private $second_check_interval = 600;
144
+
145
+ /**
146
+ * Prefix of cache ID
147
+ */
148
+ private $second_meta_key_prefix = 'second_cache_processing';
149
+
150
+ /**
151
+ * Cron name to schedule rank processing
152
+ */
153
+ private $second_cache_prime_cron = 'second_cache_prime';
154
+
155
+ /**
156
+ * Cron name to execute rank processing
157
+ */
158
+ private $second_cache_execute_cron = 'second_cache_exec';
159
+
160
+ /**
161
+ * Schedule name for rank processing
162
+ */
163
+ private $second_event_schedule = 'second cache event';
164
+
165
+ /**
166
+ * Schedule description for rank processing
167
+ */
168
+ private $second_event_description = 'second cache event';
169
+
170
+
171
+ /**
172
+ * Instance
173
+ */
174
+ private static $instance = array();
175
+
176
+ /**
177
+ * Class constarctor
178
+ * Hook onto all of the actions and filters needed by the plugin.
179
+ *
180
+ */
181
+ protected function __construct() {
182
+ $this->log('[' . __METHOD__ . '] (line='. __LINE__ . ')');
183
+
184
+ }
185
+
186
+ /**
187
+ * Get instance
188
+ *
189
+ * @since 0.1.1
190
+ */
191
+ public static function get_instance() {
192
+
193
+ $class_name = get_called_class();
194
+ if ( ! isset( self::$instance[$class_name] ) ) {
195
+ self::$instance[$class_name] = new $class_name();
196
+ //self::$instance[$class_name]->initialize($crawler, $options=array());
197
+ }
198
+
199
+ return self::$instance[$class_name];
200
+ }
201
+
202
+ /**
203
+ * Initialization
204
+ *
205
+ * @since 0.1.1
206
+ */
207
+ public function initialize( $crawler, $options = array() ) {
208
+ $this->log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
209
+
210
+ $this->crawler = $crawler;
211
+
212
+ if ( isset( $options['base_cache_target'] ) ) $this->base_cache_target = $options['base_cache_target'];
213
+ if ( isset( $options['base_check_interval'] ) ) $this->base_check_interval = $options['base_check_interval'];
214
+ if ( isset( $options['base_posts_per_check'] ) ) $this->base_posts_per_check = $options['base_posts_per_check'];
215
+ if ( isset( $options['base_transient_prefix'] ) ) $this->base_transient_prefix = $options['base_transient_prefix'];
216
+ if ( isset( $options['base_cache_prime_cron'] ) ) $this->base_cache_prime_cron = $options['base_cache_prime_cron'];
217
+ if ( isset( $options['base_cache_execute_cron'] ) ) $this->base_cache_execute_cron = $options['base_cache_execute_cron'];
218
+ if ( isset( $options['base_event_schedule'] ) ) $this->base_event_schedule = $options['base_event_schedule'];
219
+ if ( isset( $options['base_event_description'] ) ) $this->base_event_description = $options['base_event_description'];
220
+
221
+ add_filter( 'cron_schedules', array( $this, 'schedule_base_check_interval' ) );
222
+ add_action( $this->base_cache_prime_cron, array( $this, 'prime_base_data_cache' ) );
223
+ add_action( $this->base_cache_execute_cron, array( $this, 'execute_base_data_cache' ), 10, 1 );
224
+
225
+ if ( isset( $options['rush_check_interval'] ) ) $this->rush_check_interval = $options['rush_check_interval'];
226
+ if ( isset( $options['rush_posts_per_check'] ) ) $this->rush_posts_per_check = $options['rush_posts_per_check'];
227
+ if ( isset( $options['rush_cache_prime_cron'] ) ) $this->rush_cache_prime_cron = $options['rush_cache_prime_cron'];
228
+ if ( isset( $options['rush_cache_execute_cron'] ) ) $this->rush_cache_execute_cron = $options['rush_cache_execute_cron'];
229
+ if ( isset( $options['rush_event_schedule'] ) ) $this->rush_event_schedule = $options['rush_event_schedule'];
230
+ if ( isset( $options['rush_event_description'] ) ) $this->rush_event_description = $options['rush_event_description'];
231
+ if ( isset( $options['rush_new_content_term'] ) ) $this->rush_new_content_term = $options['rush_new_content_term'];
232
+
233
+ add_filter( 'cron_schedules', array( $this, 'schedule_rush_check_interval' ) );
234
+ add_action( $this->rush_cache_prime_cron, array( $this, 'prime_rush_data_cache' ) );
235
+ add_action( $this->rush_cache_execute_cron, array( $this, 'execute_rush_data_cache' ), 10, 2 );
236
+
237
+ if ( isset( $options['lazy_cache_execute_cron'] ) ) $this->lazy_cache_execute_cron = $options['lazy_cache_execute_cron'];
238
+
239
+ //add_action($this->lazy_cache_execute_cron, array($this, 'execute_lazy_data_cache'),10,2);
240
+ add_action( $this->lazy_cache_execute_cron, array( $this, 'execute_lazy_data_cache' ), 10, 1 );
241
+
242
+ if ( isset( $options['second_check_interval'] ) ) $this->second_check_interval = $options['second_check_interval'];
243
+ if ( isset( $options['second_cache_prime_cron'] ) ) $this->second_cache_prime_cron = $options['second_cache_prime_cron'];
244
+ if ( isset( $options['second_cache_execute_cron'] ) ) $this->second_cache_execute_cron = $options['second_cache_execute_cron'];
245
+ if ( isset( $options['second_meta_key_prefix'] ) ) $this->second_meta_key_prefix = $options['second_meta_key_prefix'];
246
+ if ( isset( $options['second_event_schedule'] ) ) $this->second_event_schedule = $options['second_event_schedule'];
247
+ if ( isset( $options['second_event_description'] ) ) $this->second_event_description = $options['second_event_description'];
248
+
249
+ add_filter( 'cron_schedules', array( $this, 'schedule_second_check_interval' ) );
250
+ add_action( $this->second_cache_prime_cron, array( $this, 'prime_second_data_cache' ) );
251
+ add_action( $this->second_cache_execute_cron, array( $this, 'execute_second_data_cache' ), 10, 0 );
252
+
253
+ }
254
+
255
+
256
+ /**
257
+ * Register base schedule for this engine
258
+ *
259
+ * @since 0.1.0
260
+ */
261
+ public function register_base_schedule() {
262
+ $this->log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
263
+
264
+ if ( ! wp_next_scheduled( $this->base_cache_prime_cron ) ) {
265
+ wp_schedule_event( time(), $this->base_event_schedule, $this->base_cache_prime_cron );
266
+ }
267
+ if ( ! wp_next_scheduled( $this->rush_cache_prime_cron ) ) {
268
+ wp_schedule_event( time(), $this->rush_event_schedule, $this->rush_cache_prime_cron );
269
+ }
270
+ if ( ! wp_next_scheduled( $this->second_cache_prime_cron ) ) {
271
+ $this->initialize_second_cache();
272
+ wp_schedule_event( time(), $this->second_event_schedule, $this->second_cache_prime_cron );
273
+ }
274
+
275
+ }
276
+
277
+ /**
278
+ * Unregister base schedule for this engine
279
+ *
280
+ * @since 0.1.0
281
+ */
282
+ public function unregister_base_schedule() {
283
+ $this->log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
284
+
285
+ wp_clear_scheduled_hook( $this->base_cache_prime_cron );
286
+ $this->clear_scheduled_hook( $this->base_cache_execute_cron );
287
+
288
+ wp_clear_scheduled_hook( $this->rush_cache_prime_cron );
289
+ $this->clear_scheduled_hook( $this->rush_cache_execute_cron );
290
+
291
+ wp_clear_scheduled_hook( $this->second_cache_prime_cron );
292
+ $this->clear_scheduled_hook( $this->second_cache_execute_cron );
293
+ $this->clear_second_cache();
294
+ }
295
+
296
+ /**
297
+ * Clear scheduled hook based related to specified hook name
298
+ *
299
+ * @since 0.1.1
300
+ */
301
+ private function clear_scheduled_hook( $hook ) {
302
+ $crons = _get_cron_array();
303
+
304
+ if ( empty( $crons ) ) return;
305
+
306
+ foreach( $crons as $timestamp => $cron ) {
307
+ if( isset( $cron[$hook] ) ) {
308
+ foreach ( $cron[$hook] as $signature => $data ) {
309
+ wp_unschedule_event( $timestamp, $hook, $data['args'] );
310
+ }
311
+ }
312
+ }
313
+
314
+ }
315
+
316
+ /**
317
+ * Initialize meta key for ranking
318
+ *
319
+ * @since 0.3.0
320
+ */
321
+ private function initialize_second_cache() {
322
+ $query_args = array(
323
+ 'post_type' => array( 'post', 'page' ),
324
+ 'post_status' => 'publish',
325
+ 'nopaging' => true,
326
+ 'update_post_term_cache' => false,
327
+ 'update_post_meta_cache' => false
328
+ );
329
+
330
+ $posts_query = new WP_Query( $query_args );
331
+
332
+ if ( $posts_query->have_posts() ) {
333
+ while ( $posts_query->have_posts() ) {
334
+ $posts_query->the_post();
335
+
336
+ $post_ID = get_the_ID();
337
+
338
+ foreach ( $this->base_cache_target as $key => $value ) {
339
+
340
+ $meta_key = $this->second_meta_key_prefix . strtolower( $key );
341
+
342
+ if ( $value ) {
343
+ update_post_meta($post_ID, $meta_key, -1);
344
+ }
345
+ }
346
+ }
347
+ }
348
+ wp_reset_postdata();
349
+
350
+ }
351
+
352
+ /**
353
+ * Clear meta key for ranking
354
+ *
355
+ * @since 0.3.0
356
+ */
357
+ private function clear_second_cache() {
358
+ $query_args = array(
359
+ 'post_type' => array( 'post', 'page' ),
360
+ 'post_status' => 'publish',
361
+ 'nopaging' => true,
362
+ 'update_post_term_cache' => false,
363
+ 'update_post_meta_cache' => false
364
+ );
365
+
366
+ $posts_query = new WP_Query( $query_args );
367
+
368
+ if ( $posts_query->have_posts() ) {
369
+ while ( $posts_query->have_posts() ) {
370
+ $posts_query->the_post();
371
+
372
+ $post_ID = get_the_ID();
373
+
374
+ foreach ( $this->base_cache_target as $key => $value ) {
375
+
376
+ $meta_key = $this->second_meta_key_prefix . strtolower( $key );
377
+
378
+ if ( $value ) {
379
+ delete_post_meta($post_ID, $meta_key);
380
+ }
381
+ }
382
+ }
383
+ }
384
+ wp_reset_postdata();
385
+
386
+ }
387
+
388
+ /**
389
+ * Register event schedule for this engine
390
+ *
391
+ * @since 0.1.0
392
+ */
393
+ public function schedule_base_check_interval( $schedules ) {
394
+ $this->log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
395
+
396
+ $schedules[$this->base_event_schedule] = array(
397
+ 'interval' => $this->base_check_interval,
398
+ 'display' => $this->base_event_description
399
+ );
400
+ return $schedules;
401
+ }
402
+
403
+ /**
404
+ * Schedule data retrieval and cache processing
405
+ *
406
+ * @since 0.1.0
407
+ */
408
+ public function prime_base_data_cache() {
409
+ $this->log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
410
+
411
+ $next_exec_time = time() + $this->base_check_interval;
412
+ $posts_total = $this->get_base_posts_total();
413
+
414
+ $this->log( '[' . __METHOD__ . '] check_interval: ' . $this->base_check_interval );
415
+ $this->log( '[' . __METHOD__ . '] next_exec_time: ' . $next_exec_time );
416
+ $this->log( '[' . __METHOD__ . '] posts_total: ' . $posts_total );
417
+
418
+ $transient_ID = $this->get_transient_ID( $this->base_offset_suffix );
419
+
420
+ if ( false === ( $posts_offset = get_transient( $transient_ID ) ) ) {
421
+ $posts_offset = 0;
422
+ }
423
+
424
+ $this->log( '[' . __METHOD__ . '] posts_offset: ' . $posts_offset );
425
+
426
+ wp_schedule_single_event( $next_exec_time, $this->base_cache_execute_cron, array( $posts_offset ) );
427
+
428
+ $this->log( '[' . __METHOD__ . '] posts_per_check: ' . $this->base_posts_per_check );
429
+
430
+ $posts_offset = $posts_offset + $this->base_posts_per_check;
431
+
432
+ if ( $posts_offset > $posts_total ) {
433
+ $posts_offset = 0;
434
+ }
435
+
436
+ set_transient( $transient_ID, $posts_offset, $this->base_check_interval + $this->base_check_interval );
437
+
438
+ }
439
+
440
+ /**
441
+ * Get and cache data of each published post and page
442
+ *
443
+ * @since 0.1.0
444
+ */
445
+ public function execute_base_data_cache( $posts_offset ) {
446
+ $this->log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
447
+
448
+ $this->log( '[' . __METHOD__ . '] posts_offset: ' . $posts_offset );
449
+ $this->log( '[' . __METHOD__ . '] posts_per_check: ' . $this->base_posts_per_check );
450
+ $this->log( '[' . __METHOD__ . '] check_interval: ' . $this->base_check_interval );
451
+
452
+ $cache_expiration = $this->get_base_cache_expiration();
453
+
454
+ $this->log( '[' . __METHOD__ . '] cache_expiration: ' . $cache_expiration );
455
+
456
+ $query_args = array(
457
+ 'post_type' => array( 'post', 'page' ),
458
+ 'post_status' => 'publish',
459
+ 'offset' => $posts_offset,
460
+ 'posts_per_page' => $this->base_posts_per_check,
461
+ 'no_found_rows' => true,
462
+ 'update_post_term_cache' => false,
463
+ 'update_post_meta_cache' => false
464
+ );
465
+
466
+ $posts_query = new WP_Query( $query_args );
467
+
468
+ if ( $posts_query->have_posts() ) {
469
+ while ( $posts_query->have_posts() ) {
470
+ $posts_query->the_post();
471
+
472
+ $post_ID = get_the_ID();
473
+
474
+ $this->log( '[' . __METHOD__ . '] post_id: ' . $post_ID );
475
+
476
+ $this->cache_data( $post_ID, $this->base_cache_target, $cache_expiration );
477
+ }
478
+ }
479
+ wp_reset_postdata();
480
+ }
481
+
482
+ /**
483
+ * Get cache expiration based on current number of total post and page
484
+ *
485
+ * @since 0.1.1
486
+ */
487
+ private function get_base_cache_expiration() {
488
+ $this->log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
489
+
490
+ $posts_total = $this->get_base_posts_total();
491
+
492
+ $this->log( '[' . __METHOD__ . '] posts_total: ' . $posts_total );
493
+
494
+ return ( ( ceil( $posts_total / $this->base_posts_per_check ) + 2 ) * $this->base_check_interval ) + 2 * $this->base_check_interval;
495
+ }
496
+
497
+ /**
498
+ * Get total count of current published post and page
499
+ *
500
+ * @since 0.1.0
501
+ */
502
+ private function get_base_posts_total() {
503
+ $this->log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
504
+
505
+ $query_args = array(
506
+ 'post_type' => array( 'post', 'page' ),
507
+ 'post_status' => 'publish',
508
+ 'nopaging' => true,
509
+ 'update_post_term_cache' => false,
510
+ 'update_post_meta_cache' => false
511
+ );
512
+
513
+ $posts_query = new WP_Query( $query_args );
514
+
515
+ return $posts_query->found_posts;
516
+ }
517
+
518
+ /**
519
+ * Register event schedule for this engine
520
+ *
521
+ * @since 0.2.0
522
+ */
523
+ public function schedule_rush_check_interval( $schedules ) {
524
+ $this->log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
525
+
526
+ $schedules[$this->rush_event_schedule] = array(
527
+ 'interval' => $this->rush_check_interval,
528
+ 'display' => $this->rush_event_description
529
+ );
530
+ return $schedules;
531
+ }
532
+
533
+ /**
534
+ * Schedule data retrieval and cache processing
535
+ *
536
+ * @since 0.2.0
537
+ */
538
+ public function prime_rush_data_cache() {
539
+ $this->log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
540
+
541
+ $next_exec_time = time() + $this->rush_check_interval;
542
+ $posts_total = $this->get_rush_posts_total();
543
+
544
+ $this->log( '[' . __METHOD__ . '] check_interval: ' . $this->rush_check_interval );
545
+ $this->log( '[' . __METHOD__ . '] next_exec_time: ' . $next_exec_time );
546
+ $this->log( '[' . __METHOD__ . '] posts_total: ' . $posts_total );
547
+
548
+ $transient_ID = $this->get_transient_ID($this->rush_offset_suffix);
549
+
550
+ if ( false === ( $posts_offset = get_transient( $transient_ID ) ) ) {
551
+ $posts_offset = 0;
552
+ }
553
+
554
+ $this->log( '[' . __METHOD__ . '] posts_offset: ' . $posts_offset );
555
+
556
+ wp_schedule_single_event( $next_exec_time, $this->rush_cache_execute_cron, array( $posts_offset, $this->short_hash( $next_exec_time ) ) );
557
+
558
+ $this->log( '[' . __METHOD__ . '] posts_per_check: ' . $this->rush_posts_per_check );
559
+
560
+ $posts_offset = $posts_offset + $this->rush_posts_per_check;
561
+
562
+ if ( $posts_offset > $posts_total ) {
563
+ $posts_offset = 0;
564
+ }
565
+
566
+ //delete_transient($transient_id);
567
+ set_transient( $transient_ID, $posts_offset, $this->rush_check_interval + $this->rush_check_interval );
568
+ }
569
+
570
+ /**
571
+ * Get and cache data of each published post and page
572
+ *
573
+ * @since 0.2.0
574
+ */
575
+ public function execute_rush_data_cache( $posts_offset, $hash ) {
576
+ $this->log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
577
+
578
+ $this->log( '[' . __METHOD__ . '] posts_offset: ' . $posts_offset );
579
+ $this->log( '[' . __METHOD__ . '] posts_per_check: ' . $this->rush_posts_per_check );
580
+ $this->log( '[' . __METHOD__ . '] check_interval: ' . $this->rush_check_interval );
581
+
582
+ $cache_expiration = $this->get_rush_cache_expiration();
583
+
584
+ $this->log( '[' . __METHOD__ . '] cache_expiration: ' . $cache_expiration );
585
+
586
+ $term_threshold = '3 days ago';
587
+
588
+ if ( $this->rush_new_content_term > 1 ) {
589
+ $term_threshold = $this->rush_new_content_term . ' days ago';
590
+ } else if ( $this->rush_new_content_term == 1 ) {
591
+ $term_threshold = $this->rush_new_content_term . ' day ago';
592
+ }
593
+
594
+ $this->log( '[' . __METHOD__ . '] term_threshold: ' . $term_threshold );
595
+
596
+ $query_args = array(
597
+ 'post_type' => array( 'post', 'page' ),
598
+ 'post_status' => 'publish',
599
+ 'offset' => $posts_offset,
600
+ 'posts_per_page' => $this->rush_posts_per_check,
601
+ 'date_query' => array(
602
+ 'column' => 'post_date_gmt',
603
+ 'after' => $term_threshold
604
+ ),
605
+ 'no_found_rows' => true,
606
+ 'update_post_term_cache' => false,
607
+ 'update_post_meta_cache' => false
608
+ );
609
+
610
+ $posts_query = new WP_Query( $query_args );
611
+
612
+ if ( $posts_query->have_posts() ) {
613
+ while ( $posts_query->have_posts() ) {
614
+ $posts_query->the_post();
615
+
616
+ $post_ID = get_the_ID();
617
+
618
+ $this->log( '[' . __METHOD__ . '] post_id: ' . $post_ID );
619
+
620
+ $this->cache_data( $post_ID, $this->base_cache_target, $cache_expiration );
621
+ }
622
+ }
623
+ wp_reset_postdata();
624
+ }
625
+
626
+ /**
627
+ * Get cache expiration based on current number of total post and page
628
+ *
629
+ * @since 0.2.0
630
+ */
631
+ private function get_rush_cache_expiration() {
632
+ $this->log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
633
+
634
+ $posts_total = $this->get_rush_posts_total();
635
+
636
+ $this->log( '[' . __METHOD__ . '] posts_total: ' . $posts_total );
637
+
638
+ return ( ( ceil( $posts_total / $this->rush_posts_per_check ) + 2 ) * $this->rush_check_interval ) + 2 * $this->rush_check_interval;
639
+ }
640
+
641
+ /**
642
+ * Get total count of current published post and page
643
+ *
644
+ * @since 0.2.0
645
+ */
646
+ private function get_rush_posts_total() {
647
+ $this->log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
648
+
649
+ $term_threshold = '3 days ago';
650
+
651
+ if ( $this->rush_new_content_term > 1 ) {
652
+ $term_threshold = $this->rush_new_content_term . ' days ago';
653
+ } else if ( $this->rush_new_content_term == 1 ) {
654
+ $term_threshold = $this->rush_new_content_term . ' day ago';
655
+ }
656
+
657
+ $query_args = array(
658
+ 'post_type' => array( 'post', 'page' ),
659
+ 'post_status' => 'publish',
660
+ 'date_query' => array(
661
+ 'column' => 'post_date_gmt',
662
+ 'after' => $term_threshold
663
+ ),
664
+ 'nopaging' => true,
665
+ 'no_found_rows' => true,
666
+ 'update_post_term_cache' => false,
667
+ 'update_post_meta_cache' => false
668
+ );
669
+
670
+ $posts_query = new WP_Query( $query_args );
671
+
672
+ return $posts_query->found_posts;
673
+ }
674
+
675
+ /**
676
+ * Schedule data retrieval and cache processing
677
+ *
678
+ * @since 0.2.0
679
+ */
680
+ public function prime_lazy_data_cache( $post_ID ) {
681
+ $this->log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
682
+
683
+ $next_exec_time = time() + $this->lazy_check_latency;
684
+
685
+ $this->log( '[' . __METHOD__ . '] check_latency: ' . $this->lazy_check_latency );
686
+ $this->log( '[' . __METHOD__ . '] next_exec_time: ' . $next_exec_time );
687
+
688
+ //wp_schedule_single_event($next_exec_time, $this->lazy_cache_execute_cron, array($post_ID, $this->short_hash($next_exec_time)));
689
+ wp_schedule_single_event( $next_exec_time, $this->lazy_cache_execute_cron, array( $post_ID ) );
690
+
691
+ }
692
+
693
+ /**
694
+ * Get and cache data of each published post
695
+ *
696
+ * @since 0.2.0
697
+ */
698
+ public function execute_lazy_data_cache( $post_ID ) {
699
+ $this->log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
700
+
701
+ $cache_expiration = $this->get_base_cache_expiration();
702
+
703
+ $this->log( '[' . __METHOD__ . '] cache_expiration: ' . $cache_expiration );
704
+
705
+ $this->cache_data( $post_ID, $this->base_cache_target, $cache_expiration );
706
+ }
707
+
708
+ /**
709
+ * Get and cache data of each published post
710
+ *
711
+ * @since 0.2.0
712
+ */
713
+ /*
714
+ public function execute_lazy_data_cache($post_ID, $hash){
715
+ $this->log('[' . __METHOD__ . '] (line='. __LINE__ . ')');
716
+
717
+ $cache_expiration = $this->get_base_cache_expiration();
718
+
719
+ $this->log('[' . __METHOD__ . '] cache_expiration: ' . $cache_expiration);
720
+
721
+ $this->cache_data($post_ID, $cache_expiration);
722
+ }
723
+ */
724
+
725
+ /**
726
+ * Get and cache data for a given post
727
+ *
728
+ * @since 0.1.1
729
+ */
730
+ public function execute_direct_data_cache( $post_ID ) {
731
+ $this->log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
732
+
733
+ $this->log( '[' . __METHOD__ . '] posts_per_check: ' . $this->base_posts_per_check );
734
+ $this->log( '[' . __METHOD__ . '] check_interval: ' . $this->base_check_interval );
735
+
736
+ $cache_expiration = $this->get_base_cache_expiration();
737
+
738
+ $this->log( '[' . __METHOD__ . '] cache_expiration: ' . $cache_expiration );
739
+
740
+ return $this->cache_data( $post_ID, $this->base_cache_target, $cache_expiration );
741
+ }
742
+
743
+ /**
744
+ * Get and cache data for a given post
745
+ *
746
+ * @since 0.1.1
747
+ */
748
+ private function cache_data( $post_ID, $cache_target, $cache_expiration ) {
749
+ $this->log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
750
+
751
+ $this->log( '[' . __METHOD__ . '] post_id: ' . $post_ID );
752
+
753
+ $transient_ID = $this->get_transient_ID( $post_ID );
754
+
755
+ $url = get_permalink( $post_ID );
756
+
757
+ $data = $this->crawler->get_data( $cache_target, $url );
758
+
759
+ $this->log( $data );
760
+
761
+ if ( $data ) {
762
+ $result = set_transient( $transient_ID, $data, $cache_expiration );
763
+
764
+ $this->log( '[' . __METHOD__ . '] set_transient result: ' . $result );
765
+ }
766
+
767
+ return $data;
768
+ }
769
+
770
+
771
+ /**
772
+ * Register event schedule for this engine
773
+ *
774
+ * @since 0.3.0
775
+ */
776
+ public function schedule_second_check_interval( $schedules ) {
777
+ $this->log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
778
+
779
+ $schedules[$this->second_event_schedule] = array(
780
+ 'interval' => $this->second_check_interval,
781
+ 'display' => $this->second_event_description
782
+ );
783
+ return $schedules;
784
+ }
785
+
786
+ /**
787
+ * Schedule data retrieval and cache processing
788
+ *
789
+ * @since 0.3.0
790
+ */
791
+ public function prime_second_data_cache() {
792
+ $this->log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
793
+
794
+ $next_exec_time = time() + $this->second_check_interval;
795
+ $posts_total = $this->get_base_posts_total();
796
+
797
+ $this->log( '[' . __METHOD__ . '] check_interval: ' . $this->second_check_interval );
798
+ $this->log( '[' . __METHOD__ . '] next_exec_time: ' . $next_exec_time );
799
+ $this->log( '[' . __METHOD__ . '] posts_total: ' . $posts_total );
800
+
801
+ wp_schedule_single_event( $next_exec_time, $this->second_cache_execute_cron);
802
+
803
+ }
804
+
805
+ /**
806
+ * Get and cache data of each published post and page
807
+ *
808
+ * @since 0.3.0
809
+ */
810
+ public function execute_second_data_cache() {
811
+ $this->log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
812
+
813
+ //$data = array();
814
+
815
+ $query_args = array(
816
+ 'post_type' => array( 'post', 'page' ),
817
+ 'post_status' => 'publish',
818
+ 'nopaging' => true,
819
+ 'update_post_term_cache' => false,
820
+ 'update_post_meta_cache' => false
821
+ );
822
+
823
+ $posts_query = new WP_Query( $query_args );
824
+
825
+ if ( $posts_query->have_posts() ) {
826
+ while ( $posts_query->have_posts() ) {
827
+ $posts_query->the_post();
828
+
829
+ $post_ID = get_the_ID();
830
+
831
+ $transient_ID = $this->base_transient_prefix . $post_ID;
832
+
833
+ if ( false !== ( $sns_counts = get_transient( $transient_ID ) ) ) {
834
+
835
+ foreach ( $this->base_cache_target as $key => $value ) {
836
+
837
+ $meta_key = $this->second_meta_key_prefix . strtolower( $key );
838
+
839
+ if ( $value ) {
840
+ $this->log( '[' . __METHOD__ . '] meta_key: ' . $meta_key . ' SNS: ' . $key . ' post_ID: ' . $post_ID . ' - ' . $sns_counts[$key] );
841
+ update_post_meta($post_ID, $meta_key, $sns_counts[$key]);
842
+ //$data[$key][$post_ID] = $sns_counts[$key];
843
+ }
844
+ }
845
+
846
+ }
847
+ /*
848
+ else {
849
+ foreach ( $this->base_cache_target as $key => $value ) {
850
+ if ( $value ) {
851
+ update_post_meta($post_ID, $meta_key, 0);
852
+ }
853
+ }
854
+ }
855
+ */
856
+
857
+ }
858
+ }
859
+ wp_reset_postdata();
860
+
861
+
862
+ /*
863
+ if ( $posts_query->have_posts() ) {
864
+ while( $posts_query->have_posts() ) {
865
+ $posts_query->the_post();
866
+
867
+ $post_ID = get_the_ID();
868
+
869
+ $transient_ID = $this->base_transient_prefix . $post_ID;
870
+
871
+ if ( false !== ( $sns_counts = get_transient( $transient_ID ) ) ) {
872
+
873
+ foreach ( $this->base_cache_target as $key => $value ) {
874
+ if ( $value ) {
875
+ $data[$key][$post_ID] = $sns_counts[$key];
876
+ }
877
+ }
878
+
879
+ } else {
880
+ foreach ( $this->base_cache_target as $key => $value ) {
881
+ if ( $value ) {
882
+ $data[$key][$post_ID] = 0;
883
+ }
884
+ }
885
+ }
886
+
887
+ }
888
+ }
889
+ wp_reset_postdata();
890
+
891
+ foreach ( $this->base_cache_target as $key => $value ) {
892
+ if ( $value ) {
893
+ arsort( $data[$key] );
894
+
895
+ $meta_key = $this->second_meta_key_prefix . strtolower( $key );
896
+ //update_post_meta($post_ID, $meta_key, );
897
+ $rank = 1;
898
+ foreach ( $data[$key] as $post_ID => $num ) {
899
+ $this->log( '[' . __METHOD__ . '] meta_key: ' . $meta_key . ' SNS: ' . $key . ' post_ID: ' . $post_ID . ' - ' . $rank );
900
+ update_post_meta($post_ID, $meta_key, $rank);
901
+ $rank++;
902
+ }
903
+ }
904
+ }
905
+ */
906
+
907
+ }
908
+
909
+
910
+ /**
911
+ * Get transient ID
912
+ *
913
+ * @since 0.1.1
914
+ */
915
+ private function get_transient_ID( $suffix ) {
916
+ return $this->base_transient_prefix . $suffix;
917
+ }
918
+
919
+ /**
920
+ * Get short hash code
921
+ *
922
+ * @since 0.2.0
923
+ */
924
+ private function short_hash( $data, $algo = 'CRC32' ) {
925
+ return strtr( rtrim( base64_encode( pack('H*', $algo($data) ) ), '=' ), '+/', '-_' );
926
+ }
927
+
928
+ /**
929
+ * Output log message according to WP_DEBUG setting
930
+ *
931
+ * @since 0.1.0
932
+ */
933
+ private function log( $message ) {
934
+ if ( WP_DEBUG === true ) {
935
+ if ( is_array( $message ) || is_object( $message ) ) {
936
+ error_log( print_r( $message, true ) );
937
+ } else {
938
+ error_log( $message );
939
+ }
940
+ }
941
+ }
942
+
943
+ }
944
+
945
+ ?>
module/data-crawler.php → includes/class-data-crawler.php RENAMED
@@ -1,9 +1,9 @@
1
  <?php
2
  /*
3
- data-crawler.php
4
 
5
  Description: This class is abstract class of a data crawler
6
- Version: 0.2.0
7
  Author: Daisuke Maruyama
8
  Author URI: http://marubon.info/
9
  License: GPL2 or later
@@ -30,7 +30,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
30
 
31
  */
32
 
33
- abstract class DataCrawler{
34
 
35
  /**
36
  * URL for data crawling
@@ -51,7 +51,7 @@ abstract class DataCrawler{
51
 
52
  $class_name = get_called_class();
53
 
54
- if(!isset( self::$instance[$class_name])) {
55
  self::$instance[$class_name] = new $class_name();
56
  //self::$instance[ $c ]->init($crawler, $options=array());
57
  }
@@ -64,10 +64,10 @@ abstract class DataCrawler{
64
  *
65
  * @since 0.1.0
66
  */
67
- public function set_url($url){
68
- $this->log('[' . __METHOD__ . '] (line='. __LINE__ . ')');
69
 
70
- $this->url = rawurlencode($url);
71
  }
72
 
73
  /**
@@ -75,19 +75,19 @@ abstract class DataCrawler{
75
  *
76
  * @since 0.1.1
77
  */
78
- abstract public function get_data($url);
79
 
80
  /**
81
  * Output log message according to WP_DEBUG setting
82
  *
83
  * @since 0.1.0
84
  */
85
- protected function log($message) {
86
- if (WP_DEBUG === true) {
87
- if (is_array($message) || is_object($message)) {
88
- error_log(print_r($message, true));
89
  } else {
90
- error_log($message);
91
  }
92
  }
93
  }
1
  <?php
2
  /*
3
+ class-data-crawler.php
4
 
5
  Description: This class is abstract class of a data crawler
6
+ Version: 0.3.0
7
  Author: Daisuke Maruyama
8
  Author URI: http://marubon.info/
9
  License: GPL2 or later
30
 
31
  */
32
 
33
+ abstract class Data_Crawler {
34
 
35
  /**
36
  * URL for data crawling
51
 
52
  $class_name = get_called_class();
53
 
54
+ if( ! isset( self::$instance[$class_name] ) ) {
55
  self::$instance[$class_name] = new $class_name();
56
  //self::$instance[ $c ]->init($crawler, $options=array());
57
  }
64
  *
65
  * @since 0.1.0
66
  */
67
+ public function set_url( $url ) {
68
+ $this->log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
69
 
70
+ $this->url = rawurlencode( $url );
71
  }
72
 
73
  /**
75
  *
76
  * @since 0.1.1
77
  */
78
+ abstract public function get_data( $cache_target, $url );
79
 
80
  /**
81
  * Output log message according to WP_DEBUG setting
82
  *
83
  * @since 0.1.0
84
  */
85
+ protected function log( $message ) {
86
+ if ( WP_DEBUG === true ) {
87
+ if ( is_array( $message ) || is_object( $message ) ) {
88
+ error_log( print_r( $message, true ) );
89
  } else {
90
+ error_log( $message );
91
  }
92
  }
93
  }
includes/class-sns-count-crawler.php ADDED
@@ -0,0 +1,224 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /*
3
+ class-sns-count-crawler.php
4
+
5
+ Description: This class is a data crawler whitch get share count using given API and cURL
6
+ Version: 0.3.0
7
+ Author: Daisuke Maruyama
8
+ Author URI: http://marubon.info/
9
+ License: GPL2 or later
10
+ License URI: http://www.gnu.org/licenses/gpl-2.0.txt
11
+ */
12
+
13
+ /*
14
+
15
+ Copyright (C) 2014 Daisuke Maruyama
16
+
17
+ This program is free software; you can redistribute it and/or
18
+ modify it under the terms of the GNU General Public License
19
+ as published by the Free Software Foundation; either version 2
20
+ of the License, or (at your option) any later version.
21
+
22
+ This program is distributed in the hope that it will be useful,
23
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
24
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25
+ GNU General Public License for more details.
26
+
27
+ You should have received a copy of the GNU General Public License
28
+ along with this program; if not, write to the Free Software
29
+ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
30
+
31
+ */
32
+
33
+ class SNS_Count_Crawler extends Data_Crawler {
34
+
35
+ /**
36
+ * Timeout for cURL data retrieval
37
+ */
38
+ private $timeout = 10;
39
+
40
+ protected function __construct( $url='', $timeout=10 ) {
41
+ $this->log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
42
+
43
+ $this->url = rawurlencode( $url );
44
+ $this->timeout = $timeout;
45
+ }
46
+
47
+ public function set_timeout( $timeout ) {
48
+ $this->log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
49
+
50
+ $this->timeout = $timeout;
51
+ }
52
+
53
+ /**
54
+ * Implementation of abstract method. this method gets each share count
55
+ *
56
+ * @since 0.1.1
57
+ */
58
+ public function get_data( $cache_target, $url ) {
59
+ $this->log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
60
+
61
+ $url = rawurlencode( $url );
62
+
63
+ $sns_counts = array();
64
+
65
+ if ( isset( $cache_target[SNS_Count_Cache::REF_HATEBU] ) && $cache_target[SNS_Count_Cache::REF_HATEBU] ) {
66
+ $sns_counts[SNS_Count_Cache::REF_HATEBU] = $this->get_hatebu_count( $url );
67
+ }
68
+
69
+ if ( isset( $cache_target[SNS_Count_Cache::REF_TWITTER] ) && $cache_target[SNS_Count_Cache::REF_TWITTER] ) {
70
+ $sns_counts[SNS_Count_Cache::REF_TWITTER] = $this->get_twitter_count( $url );
71
+ }
72
+
73
+ if ( isset( $cache_target[SNS_Count_Cache::REF_FACEBOOK] ) && $cache_target[SNS_Count_Cache::REF_FACEBOOK] ) {
74
+ $sns_counts[SNS_Count_Cache::REF_FACEBOOK] = $this->get_facebook_count( $url );
75
+ }
76
+
77
+ if ( isset( $cache_target[SNS_Count_Cache::REF_GPLUS] ) && $cache_target[SNS_Count_Cache::REF_GPLUS] ) {
78
+ $sns_counts[SNS_Count_Cache::REF_GPLUS] = $this->get_gplus_count( $url );
79
+ }
80
+
81
+ if ( isset( $cache_target[SNS_Count_Cache::REF_POCKET] ) && $cache_target[SNS_Count_Cache::REF_POCKET] ) {
82
+ $sns_counts[SNS_Count_Cache::REF_POCKET] = $this->get_pocket_count( $url );
83
+ }
84
+
85
+ return $sns_counts;
86
+ }
87
+
88
+ /**
89
+ * Get share count for Hatena Bookmark
90
+ *
91
+ * @since 0.1.0
92
+ */
93
+ public function get_hatebu_count( $url ) {
94
+ $this->log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
95
+
96
+ $query = 'http://api.b.st-hatena.com/entry.count?url=' . $url;
97
+
98
+ $hatebu = $this->remote_get( $query );
99
+
100
+ return isset( $hatebu ) ? intval($hatebu) : 0;
101
+ }
102
+
103
+ /**
104
+ * Get share count for Twitter
105
+ *
106
+ * @since 0.1.0
107
+ */
108
+ public function get_twitter_count( $url ) {
109
+ $this->log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
110
+
111
+ $query = 'http://urls.api.twitter.com/1/urls/count.json?url=' . $url;
112
+
113
+ $json = $this->remote_get( $query );
114
+
115
+ $twitter = json_decode( $json, true );
116
+
117
+ return isset( $twitter['count'] ) ? intval( $twitter['count'] ) : 0;
118
+ }
119
+
120
+ /**
121
+ * Get share count for Facebook
122
+ *
123
+ * @since 0.1.0
124
+ */
125
+ public function get_facebook_count( $url ) {
126
+ $this->log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
127
+
128
+ $query = 'http://graph.facebook.com/' . $url;
129
+
130
+ $json = $this->remote_get( $query );
131
+
132
+ $facebook = json_decode( $json, true );
133
+
134
+ return isset( $facebook['shares'] ) ? intval( $facebook['shares'] ) : 0;
135
+ }
136
+
137
+ /**
138
+ * Get share count for Google Plus
139
+ *
140
+ * @since 0.1.0
141
+ */
142
+ public function get_gplus_count( $url ) {
143
+ $this->log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
144
+
145
+ $curl = curl_init();
146
+
147
+ curl_setopt( $curl, CURLOPT_URL, "https://clients6.google.com/rpc" );
148
+ curl_setopt( $curl, CURLOPT_POST, true );
149
+ curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, false );
150
+ curl_setopt( $curl, CURLOPT_POSTFIELDS, '[{"method":"pos.plusones.get","id":"p","params":{"nolog":true,"id":"' . rawurldecode( $url ) . '","source":"widget","userId":"@viewer","groupId":"@self"},"jsonrpc":"2.0","key":"p","apiVersion":"v1"}]' );
151
+ curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
152
+ curl_setopt( $curl, CURLOPT_HTTPHEADER, array( 'Content-type: application/json' ) );
153
+
154
+ $curl_results = curl_exec ( $curl );
155
+
156
+ if ( curl_error( $curl ) ) {
157
+ $this->log( '[' . __METHOD__ . '] curl_error: ' + curl_error( $curl ) );
158
+ die( curl_error( $curl ) ) ;
159
+ }
160
+
161
+ curl_close( $curl );
162
+
163
+ $json = json_decode( $curl_results, true );
164
+
165
+ return isset( $json[0]['result']['metadata']['globalCounts']['count'] ) ? intval( $json[0]['result']['metadata']['globalCounts']['count'] ) : 0;
166
+ }
167
+
168
+ /**
169
+ * Get share count for Pocket
170
+ *
171
+ * @since 0.1.0
172
+ */
173
+ public function get_pocket_count( $url ) {
174
+ $this->log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
175
+
176
+ $query = 'http://widgets.getpocket.com/v1/button?v=1&count=horizontal&url=' . $url;
177
+ $html = $this->remote_get( $query );
178
+
179
+ $dom = new DOMDocument( '1.0', 'UTF-8' );
180
+ $dom->preserveWhiteSpace = false;
181
+ $dom->formatOutput = true;
182
+ $dom->loadXml( $html );
183
+
184
+ $xpath = new DOMXPath( $dom );
185
+
186
+ $result = $xpath->query( '//em[@id = "cnt"]' )->item(0);
187
+
188
+ $this->log( '[' . __METHOD__ . '] count: ' . $result->nodeValue );
189
+
190
+ return isset( $result->nodeValue ) ? intval( $result->nodeValue ) : 0;
191
+ }
192
+
193
+ /**
194
+ * Get content from given URL using cURL
195
+ *
196
+ * @since 0.1.0
197
+ */
198
+ private function remote_get( $url ) {
199
+ $this->log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
200
+
201
+ $curl = curl_init();
202
+
203
+ curl_setopt( $curl, CURLOPT_URL, $url );
204
+ curl_setopt( $curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT'] );
205
+ //curl_setopt( $curl, CURLOPT_FAILONERROR, true );
206
+ curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, true );
207
+ curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
208
+ curl_setopt( $curl, CURLOPT_TIMEOUT, $this->timeout );
209
+
210
+ $curl_results = curl_exec( $curl );
211
+
212
+ if ( curl_error( $curl ) ) {
213
+ $this->log( '[' . __METHOD__ . '] curl_error: ' + curl_error( $curl ) );
214
+ die( curl_error( $curl ) );
215
+ }
216
+
217
+ curl_close( $curl );
218
+
219
+ return $curl_results;
220
+ }
221
+
222
+ }
223
+
224
+ ?>
module/data-cache-engine.php DELETED
@@ -1,650 +0,0 @@
1
- <?php
2
- /*
3
- data-cache-engine.php
4
-
5
- Description: This class is a data cache engine whitch get and cache data using wp-cron at regular intervals
6
- Version: 0.2.0
7
- Author: Daisuke Maruyama
8
- Author URI: http://marubon.info/
9
- License: GPL2 or later
10
- License URI: http://www.gnu.org/licenses/gpl-2.0.txt
11
- */
12
-
13
- /*
14
-
15
- Copyright (C) 2014 Daisuke Maruyama
16
-
17
- This program is free software; you can redistribute it and/or
18
- modify it under the terms of the GNU General Public License
19
- as published by the Free Software Foundation; either version 2
20
- of the License, or (at your option) any later version.
21
-
22
- This program is distributed in the hope that it will be useful,
23
- but WITHOUT ANY WARRANTY; without even the implied warranty of
24
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25
- GNU General Public License for more details.
26
-
27
- You should have received a copy of the GNU General Public License
28
- along with this program; if not, write to the Free Software
29
- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
30
-
31
- */
32
-
33
- class DataCacheEngine {
34
-
35
- /**
36
- * Instance of crawler to get data
37
- */
38
- private $crawler = NULL;
39
-
40
- /**
41
- * Interval cheking and caching target data
42
- */
43
- private $base_check_interval = 600;
44
-
45
- /**
46
- * Number of posts to check at a time
47
- */
48
- private $base_posts_per_check = 20;
49
-
50
- /**
51
- * Prefix of cache ID
52
- */
53
- private $base_transient_prefix = 'base_data_cache';
54
-
55
- /**
56
- * Cron name to schedule cache processing
57
- */
58
- private $base_cache_prime_cron = 'base_data_cache_prime';
59
-
60
- /**
61
- * Cron name to execute cache processing
62
- */
63
- private $base_cache_execute_cron = 'base_data_cache_exec';
64
-
65
- /**
66
- * Schedule name for cache processing
67
- */
68
- private $base_event_schedule = 'base_cache_event';
69
-
70
- /**
71
- * Schedule description for cache processing
72
- */
73
- private $base_event_description = 'base cache event';
74
-
75
- /**
76
- * Offset suffix
77
- */
78
- private $base_offset_suffix = 'base_offset';
79
-
80
- /**
81
- * Interval cheking and caching target data
82
- */
83
- private $rush_check_interval = 300;
84
-
85
- /**
86
- * Number of posts to check at a time
87
- */
88
- private $rush_posts_per_check = 20;
89
-
90
- /**
91
- * Prefix of cache ID
92
- */
93
- private $rush_transient_prefix = 'rush_data_cache';
94
-
95
- /**
96
- * Cron name to schedule cache processing
97
- */
98
- private $rush_cache_prime_cron = 'rush_data_cache_prime';
99
-
100
- /**
101
- * Cron name to execute cache processing
102
- */
103
- private $rush_cache_execute_cron = 'rush_data_cache_exec';
104
-
105
- /**
106
- * Schedule name for cache processing
107
- */
108
- private $rush_event_schedule = 'rush_cache_event';
109
-
110
- /**
111
- * Schedule description for cache processing
112
- */
113
- private $rush_event_description = 'rush cache event';
114
-
115
- /**
116
- * Offset suffix
117
- */
118
- private $rush_offset_suffix = 'rush_offset';
119
-
120
- /**
121
- * Latency suffix
122
- */
123
- private $lazy_check_latency = 10;
124
-
125
- /**
126
- * Cron name to execute cache processing
127
- */
128
- private $lazy_cache_execute_cron = 'lazy_data_cache_exec';
129
-
130
- /**
131
- * Instance
132
- */
133
- private static $instance = array();
134
-
135
- /**
136
- * Class constarctor
137
- * Hook onto all of the actions and filters needed by the plugin.
138
- *
139
- */
140
- protected function __construct() {
141
- $this->log('[' . __METHOD__ . '] (line='. __LINE__ . ')');
142
-
143
- }
144
-
145
- /**
146
- * Get instance
147
- *
148
- * @since 0.1.1
149
- */
150
- public static function get_instance() {
151
-
152
- $class_name = get_called_class();
153
- if( !isset( self::$instance[$class_name] ) ) {
154
- self::$instance[$class_name] = new $class_name();
155
- //self::$instance[$class_name]->initialize($crawler, $options=array());
156
- }
157
-
158
- return self::$instance[$class_name];
159
- }
160
-
161
- /**
162
- * Initialization
163
- *
164
- * @since 0.1.1
165
- */
166
- public function initialize($crawler, $options=array()){
167
- $this->log('[' . __METHOD__ . '] (line='. __LINE__ . ')');
168
-
169
- $this->crawler = $crawler;
170
-
171
- if(isset($options['base_check_interval'])) $this->base_check_interval = $options['base_check_interval'];
172
- if(isset($options['base_posts_per_check'])) $this->base_posts_per_check = $options['base_posts_per_check'];
173
- if(isset($options['base_transient_prefix'])) $this->base_transient_prefix = $options['base_transient_prefix'];
174
- if(isset($options['base_cache_prime_cron'])) $this->base_cache_prime_cron = $options['base_cache_prime_cron'];
175
- if(isset($options['base_cache_execute_cron'])) $this->base_cache_execute_cron = $options['base_cache_execute_cron'];
176
- if(isset($options['base_event_schedule'])) $this->base_event_schedule = $options['base_event_schedule'];
177
- if(isset($options['base_event_description'])) $this->base_event_description = $options['base_event_description'];
178
-
179
- add_filter('cron_schedules', array($this, 'schedule_base_check_interval'));
180
- add_action($this->base_cache_prime_cron, array($this, 'prime_base_data_cache'));
181
- add_action($this->base_cache_execute_cron, array($this, 'execute_base_data_cache'),10,1);
182
-
183
- if(isset($options['rush_check_interval'])) $this->rush_check_interval = $options['rush_check_interval'];
184
- if(isset($options['rush_posts_per_check'])) $this->rush_posts_per_check = $options['rush_posts_per_check'];
185
- if(isset($options['rush_cache_prime_cron'])) $this->rush_cache_prime_cron = $options['rush_cache_prime_cron'];
186
- if(isset($options['rush_cache_execute_cron'])) $this->rush_cache_execute_cron = $options['rush_cache_execute_cron'];
187
- if(isset($options['rush_event_schedule'])) $this->rush_event_schedule = $options['rush_event_schedule'];
188
- if(isset($options['rush_event_description'])) $this->rush_event_description = $options['rush_event_description'];
189
-
190
- add_filter('cron_schedules', array($this, 'schedule_rush_check_interval'));
191
- add_action($this->rush_cache_prime_cron, array($this, 'prime_rush_data_cache'));
192
- add_action($this->rush_cache_execute_cron, array($this, 'execute_rush_data_cache'),10,2);
193
-
194
- if(isset($options['lazy_cache_execute_cron'])) $this->lazy_cache_execute_cron = $options['lazy_cache_execute_cron'];
195
-
196
- //add_action($this->lazy_cache_execute_cron, array($this, 'execute_lazy_data_cache'),10,2);
197
- add_action($this->lazy_cache_execute_cron, array($this, 'execute_lazy_data_cache'),10,1);
198
- }
199
-
200
-
201
- /**
202
- * Register base schedule for this engine
203
- *
204
- * @since 0.1.0
205
- */
206
- public function register_base_schedule(){
207
- $this->log('[' . __METHOD__ . '] (line='. __LINE__ . ')');
208
-
209
- if (!wp_next_scheduled($this->base_cache_prime_cron)) {
210
- wp_schedule_event( time(), $this->base_event_schedule, $this->base_cache_prime_cron);
211
- }
212
- if (!wp_next_scheduled($this->rush_cache_prime_cron)) {
213
- wp_schedule_event( time(), $this->rush_event_schedule, $this->rush_cache_prime_cron);
214
- }
215
- }
216
-
217
- /**
218
- * Unregister base schedule for this engine
219
- *
220
- * @since 0.1.0
221
- */
222
- public function unregister_base_schedule(){
223
- $this->log('[' . __METHOD__ . '] (line='. __LINE__ . ')');
224
-
225
- wp_clear_scheduled_hook($this->base_cache_prime_cron);
226
- $this->clear_scheduled_hook($this->base_cache_execute_cron);
227
-
228
- wp_clear_scheduled_hook($this->rush_cache_prime_cron);
229
- $this->clear_scheduled_hook($this->rush_cache_execute_cron);
230
-
231
- }
232
-
233
- /**
234
- * Clear scheduled hook based related to specified hook name
235
- *
236
- * @since 0.1.1
237
- */
238
- private function clear_scheduled_hook($hook){
239
- $crons = _get_cron_array();
240
-
241
- if(empty($crons)) return;
242
-
243
- foreach($crons as $timestamp => $cron) {
244
- if(isset($cron[$hook])){
245
- foreach ($cron[$hook] as $signature => $data){
246
- wp_unschedule_event( $timestamp, $hook, $data['args']);
247
- }
248
- }
249
- }
250
-
251
- }
252
-
253
- /**
254
- * Register event schedule for this engine
255
- *
256
- * @since 0.1.0
257
- */
258
- public function schedule_base_check_interval($schedules) {
259
- $this->log('[' . __METHOD__ . '] (line='. __LINE__ . ')');
260
-
261
- $schedules[$this->base_event_schedule] = array(
262
- 'interval' => $this->base_check_interval,
263
- 'display' => $this->base_event_description
264
- );
265
- return $schedules;
266
- }
267
-
268
- /**
269
- * Schedule data retrieval and cache processing
270
- *
271
- * @since 0.1.0
272
- */
273
- public function prime_base_data_cache(){
274
- $this->log('[' . __METHOD__ . '] (line='. __LINE__ . ')');
275
-
276
- $next_exec_time = time() + $this->base_check_interval;
277
- $posts_total = $this->get_base_posts_total();
278
-
279
- $this->log('[' . __METHOD__ . '] check_interval: ' . $this->base_check_interval);
280
- $this->log('[' . __METHOD__ . '] next_exec_time: ' . $next_exec_time);
281
- $this->log('[' . __METHOD__ . '] posts_total: ' . $posts_total);
282
-
283
- $transient_ID = $this->get_transient_ID($this->base_offset_suffix);
284
-
285
- if (false === ($posts_offset = get_transient($transient_ID))) {
286
- $posts_offset = 0;
287
- }
288
-
289
- $this->log('[' . __METHOD__ . '] posts_offset: ' . $posts_offset);
290
-
291
- wp_schedule_single_event($next_exec_time, $this->base_cache_execute_cron, array($posts_offset));
292
-
293
- $this->log('[' . __METHOD__ . '] posts_per_check: ' . $this->base_posts_per_check);
294
-
295
- $posts_offset = $posts_offset + $this->base_posts_per_check;
296
-
297
- if($posts_offset > $posts_total){
298
- $posts_offset = 0;
299
- }
300
-
301
- set_transient($transient_ID, $posts_offset, $this->base_check_interval + $this->base_check_interval);
302
-
303
- }
304
-
305
- /**
306
- * Get and cache data of each published post and page
307
- *
308
- * @since 0.1.0
309
- */
310
- public function execute_base_data_cache($posts_offset){
311
- $this->log('[' . __METHOD__ . '] (line='. __LINE__ . ')');
312
-
313
- $this->log('[' . __METHOD__ . '] posts_offset: ' . $posts_offset);
314
- $this->log('[' . __METHOD__ . '] posts_per_check: ' . $this->base_posts_per_check);
315
- $this->log('[' . __METHOD__ . '] check_interval: ' . $this->base_check_interval);
316
-
317
- $cache_expiration = $this->get_base_cache_expiration();
318
-
319
- $this->log('[' . __METHOD__ . '] cache_expiration: ' . $cache_expiration);
320
-
321
- $query_args = array(
322
- 'post_type' => array('post', 'page'),
323
- 'post_status' => 'publish',
324
- 'offset' => $posts_offset,
325
- 'posts_per_page' => $this->base_posts_per_check,
326
- 'no_found_rows' => true,
327
- 'update_post_term_cache' => false,
328
- 'update_post_meta_cache' => false
329
- );
330
-
331
- $posts_query = new WP_Query($query_args);
332
-
333
- if($posts_query->have_posts()) {
334
- while($posts_query->have_posts()){
335
- $posts_query->the_post();
336
-
337
- $post_ID = get_the_ID();
338
-
339
- $this->log('[' . __METHOD__ . '] post_id: ' . $post_ID);
340
-
341
- $this->cache_data($post_ID, $cache_expiration);
342
- }
343
- }
344
- wp_reset_postdata();
345
- }
346
-
347
- /**
348
- * Get cache expiration based on current number of total post and page
349
- *
350
- * @since 0.1.1
351
- */
352
- private function get_base_cache_expiration(){
353
- $this->log('[' . __METHOD__ . '] (line='. __LINE__ . ')');
354
-
355
- $posts_total = $this->get_base_posts_total();
356
-
357
- $this->log('[' . __METHOD__ . '] posts_total: ' . $posts_total);
358
-
359
- return ((ceil($posts_total / $this->base_posts_per_check) + 2) * $this->base_check_interval) + 2 * $this->base_check_interval;
360
- }
361
-
362
- /**
363
- * Get total count of current published post and page
364
- *
365
- * @since 0.1.0
366
- */
367
- private function get_base_posts_total(){
368
- $this->log('[' . __METHOD__ . '] (line='. __LINE__ . ')');
369
-
370
- $query_args = array(
371
- 'post_type' => array('post', 'page'),
372
- 'post_status' => 'publish',
373
- 'nopaging' => true,
374
- 'update_post_term_cache' => false,
375
- 'update_post_meta_cache' => false
376
- );
377
-
378
- $posts_query = new WP_Query($query_args);
379
-
380
- return $posts_query->found_posts;
381
- }
382
-
383
- /**
384
- * Register event schedule for this engine
385
- *
386
- * @since 0.2.0
387
- */
388
- public function schedule_rush_check_interval($schedules) {
389
- $this->log('[' . __METHOD__ . '] (line='. __LINE__ . ')');
390
-
391
- $schedules[$this->rush_event_schedule] = array(
392
- 'interval' => $this->rush_check_interval,
393
- 'display' => $this->rush_event_description
394
- );
395
- return $schedules;
396
- }
397
-
398
- /**
399
- * Schedule data retrieval and cache processing
400
- *
401
- * @since 0.2.0
402
- */
403
- public function prime_rush_data_cache(){
404
- $this->log('[' . __METHOD__ . '] (line='. __LINE__ . ')');
405
-
406
- $next_exec_time = time() + $this->rush_check_interval;
407
- $posts_total = $this->get_rush_posts_total();
408
-
409
- $this->log('[' . __METHOD__ . '] check_interval: ' . $this->rush_check_interval);
410
- $this->log('[' . __METHOD__ . '] next_exec_time: ' . $next_exec_time);
411
- $this->log('[' . __METHOD__ . '] posts_total: ' . $posts_total);
412
-
413
- $transient_ID = $this->get_transient_ID($this->rush_offset_suffix);
414
-
415
- if (false === ($posts_offset = get_transient($transient_ID))) {
416
- $posts_offset = 0;
417
- }
418
-
419
- $this->log('[' . __METHOD__ . '] posts_offset: ' . $posts_offset);
420
-
421
- wp_schedule_single_event($next_exec_time, $this->rush_cache_execute_cron, array($posts_offset, $this->short_hash($next_exec_time)));
422
-
423
- $this->log('[' . __METHOD__ . '] posts_per_check: ' . $this->rush_posts_per_check);
424
-
425
- $posts_offset = $posts_offset + $this->rush_posts_per_check;
426
-
427
- if($posts_offset > $posts_total){
428
- $posts_offset = 0;
429
- }
430
-
431
- //delete_transient($transient_id);
432
- set_transient($transient_ID, $posts_offset, $this->rush_check_interval + $this->rush_check_interval);
433
- }
434
-
435
- /**
436
- * Get and cache data of each published post and page
437
- *
438
- * @since 0.2.0
439
- */
440
- public function execute_rush_data_cache($posts_offset, $hash){
441
- $this->log('[' . __METHOD__ . '] (line='. __LINE__ . ')');
442
-
443
- $this->log('[' . __METHOD__ . '] posts_offset: ' . $posts_offset);
444
- $this->log('[' . __METHOD__ . '] posts_per_check: ' . $this->rush_posts_per_check);
445
- $this->log('[' . __METHOD__ . '] check_interval: ' . $this->rush_check_interval);
446
-
447
- $cache_expiration = $this->get_rush_cache_expiration();
448
-
449
- $this->log('[' . __METHOD__ . '] cache_expiration: ' . $cache_expiration);
450
-
451
- $query_args = array(
452
- 'post_type' => array('post', 'page'),
453
- 'post_status' => 'publish',
454
- 'offset' => $posts_offset,
455
- 'posts_per_page' => $this->rush_posts_per_check,
456
- 'date_query' => array(
457
- 'column' => 'post_date_gmt',
458
- 'after' => '3 days ago'
459
- ),
460
- 'no_found_rows' => true,
461
- 'update_post_term_cache' => false,
462
- 'update_post_meta_cache' => false
463
- );
464
-
465
- $posts_query = new WP_Query($query_args);
466
-
467
- if($posts_query->have_posts()) {
468
- while($posts_query->have_posts()){
469
- $posts_query->the_post();
470
-
471
- $post_ID = get_the_ID();
472
-
473
- $this->log('[' . __METHOD__ . '] post_id: ' . $post_ID);
474
-
475
- $this->cache_data($post_ID, $cache_expiration);
476
- }
477
- }
478
- wp_reset_postdata();
479
- }
480
-
481
- /**
482
- * Get cache expiration based on current number of total post and page
483
- *
484
- * @since 0.2.0
485
- */
486
- private function get_rush_cache_expiration(){
487
- $this->log('[' . __METHOD__ . '] (line='. __LINE__ . ')');
488
-
489
- $posts_total = $this->get_rush_posts_total();
490
-
491
- $this->log('[' . __METHOD__ . '] posts_total: ' . $posts_total);
492
-
493
- return ((ceil($posts_total / $this->rush_posts_per_check) + 2) * $this->rush_check_interval) + 2 * $this->rush_check_interval;
494
- }
495
-
496
- /**
497
- * Get total count of current published post and page
498
- *
499
- * @since 0.2.0
500
- */
501
- private function get_rush_posts_total(){
502
- $this->log('[' . __METHOD__ . '] (line='. __LINE__ . ')');
503
-
504
- $query_args = array(
505
- 'post_type' => array('post', 'page'),
506
- 'post_status' => 'publish',
507
- 'date_query' => array(
508
- 'column' => 'post_date_gmt',
509
- 'after' => '3 days ago'
510
- ),
511
- 'nopaging' => true,
512
- 'no_found_rows' => true,
513
- 'update_post_term_cache' => false,
514
- 'update_post_meta_cache' => false
515
- );
516
-
517
- $posts_query = new WP_Query($query_args);
518
-
519
- return $posts_query->found_posts;
520
- }
521
-
522
- /**
523
- * Schedule data retrieval and cache processing
524
- *
525
- * @since 0.2.0
526
- */
527
- public function prime_lazy_data_cache($post_ID){
528
- $this->log('[' . __METHOD__ . '] (line='. __LINE__ . ')');
529
-
530
- $next_exec_time = time() + $this->lazy_check_latency;
531
-
532
- $this->log('[' . __METHOD__ . '] check_latency: ' . $this->lazy_check_latency);
533
- $this->log('[' . __METHOD__ . '] next_exec_time: ' . $next_exec_time);
534
-
535
- //wp_schedule_single_event($next_exec_time, $this->lazy_cache_execute_cron, array($post_ID, $this->short_hash($next_exec_time)));
536
- wp_schedule_single_event($next_exec_time, $this->lazy_cache_execute_cron, array($post_ID));
537
-
538
- }
539
-
540
- /**
541
- * Get and cache data of each published post
542
- *
543
- * @since 0.2.0
544
- */
545
- public function execute_lazy_data_cache($post_ID){
546
- $this->log('[' . __METHOD__ . '] (line='. __LINE__ . ')');
547
-
548
- $cache_expiration = $this->get_base_cache_expiration();
549
-
550
- $this->log('[' . __METHOD__ . '] cache_expiration: ' . $cache_expiration);
551
-
552
- $this->cache_data($post_ID, $cache_expiration);
553
- }
554
-
555
- /**
556
- * Get and cache data of each published post
557
- *
558
- * @since 0.2.0
559
- */
560
- /*
561
- public function execute_lazy_data_cache($post_ID, $hash){
562
- $this->log('[' . __METHOD__ . '] (line='. __LINE__ . ')');
563
-
564
- $cache_expiration = $this->get_base_cache_expiration();
565
-
566
- $this->log('[' . __METHOD__ . '] cache_expiration: ' . $cache_expiration);
567
-
568
- $this->cache_data($post_ID, $cache_expiration);
569
- }
570
- */
571
-
572
- /**
573
- * Get and cache data for a given post
574
- *
575
- * @since 0.1.1
576
- */
577
- public function execute_direct_data_cache($post_ID){
578
- $this->log('[' . __METHOD__ . '] (line='. __LINE__ . ')');
579
-
580
- $this->log('[' . __METHOD__ . '] posts_per_check: ' . $this->base_posts_per_check);
581
- $this->log('[' . __METHOD__ . '] check_interval: ' . $this->base_check_interval);
582
-
583
- $cache_expiration = $this->get_base_cache_expiration();
584
-
585
- $this->log('[' . __METHOD__ . '] cache_expiration: ' . $cache_expiration);
586
-
587
- return $this->cache_data($post_ID, $cache_expiration);
588
- }
589
-
590
- /**
591
- * Get and cache data for a given post
592
- *
593
- * @since 0.1.1
594
- */
595
- private function cache_data($post_ID, $cache_expiration){
596
- $this->log('[' . __METHOD__ . '] (line='. __LINE__ . ')');
597
-
598
- $this->log('[' . __METHOD__ . '] post_id: ' . $post_ID);
599
-
600
- $transient_ID = $this->get_transient_ID($post_ID);
601
-
602
- $url = get_permalink($post_ID);
603
-
604
- $data = $this->crawler->get_data($url);
605
-
606
- $this->log($data);
607
-
608
- $result = set_transient($transient_ID, $data, $cache_expiration);
609
-
610
- $this->log('[' . __METHOD__ . '] set_transient result: ' . $result);
611
-
612
- return $data;
613
- }
614
-
615
- /**
616
- * Get transient ID
617
- *
618
- * @since 0.1.1
619
- */
620
- private function get_transient_ID($suffix){
621
- return $this->base_transient_prefix . $suffix;
622
- }
623
-
624
- /**
625
- * Get short hash code
626
- *
627
- * @since 0.2.0
628
- */
629
- private function short_hash($data, $algo = 'CRC32') {
630
- return strtr(rtrim(base64_encode(pack('H*', $algo($data))), '='), '+/', '-_');
631
- }
632
-
633
- /**
634
- * Output log message according to WP_DEBUG setting
635
- *
636
- * @since 0.1.0
637
- */
638
- private function log($message) {
639
- if (WP_DEBUG === true) {
640
- if (is_array($message) || is_object($message)) {
641
- error_log(print_r($message, true));
642
- } else {
643
- error_log($message);
644
- }
645
- }
646
- }
647
-
648
- }
649
-
650
- ?>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
module/sns-count-crawler.php DELETED
@@ -1,202 +0,0 @@
1
- <?php
2
- /*
3
- sns-count-crawler.php
4
-
5
- Description: This class is a data crawler whitch get share count using given API and cURL
6
- Version: 0.2.0
7
- Author: Daisuke Maruyama
8
- Author URI: http://marubon.info/
9
- License: GPL2 or later
10
- License URI: http://www.gnu.org/licenses/gpl-2.0.txt
11
- */
12
-
13
- /*
14
-
15
- Copyright (C) 2014 Daisuke Maruyama
16
-
17
- This program is free software; you can redistribute it and/or
18
- modify it under the terms of the GNU General Public License
19
- as published by the Free Software Foundation; either version 2
20
- of the License, or (at your option) any later version.
21
-
22
- This program is distributed in the hope that it will be useful,
23
- but WITHOUT ANY WARRANTY; without even the implied warranty of
24
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25
- GNU General Public License for more details.
26
-
27
- You should have received a copy of the GNU General Public License
28
- along with this program; if not, write to the Free Software
29
- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
30
-
31
- */
32
-
33
- class SNSCountCrawler extends DataCrawler{
34
-
35
- /**
36
- * Timeout for cURL data retrieval
37
- */
38
- private $timeout = 10;
39
-
40
- protected function __construct($url='',$timeout=10) {
41
- $this->log('[' . __METHOD__ . '] (line='. __LINE__ . ')');
42
-
43
- $this->url=rawurlencode($url);
44
- $this->timeout=$timeout;
45
- }
46
-
47
- public function set_timeout($timeout){
48
- $this->log('[' . __METHOD__ . '] (line='. __LINE__ . ')');
49
-
50
- $this->timeout = $timeout;
51
- }
52
-
53
- /**
54
- * Implementation of abstract method. this method gets each share count
55
- *
56
- * @since 0.1.0
57
- */
58
- /*
59
- public function get_data(){
60
- $this->log('[' . __METHOD__ . '] (line='. __LINE__ . ')');
61
-
62
- $sns_counts = array();
63
-
64
- $sns_counts[SNSCountCache::REF_HATEBU] = $this->get_hatebu_count();
65
- $sns_counts[SNSCountCache::REF_TWITTER] = $this->get_twitter_count();
66
- $sns_counts[SNSCountCache::REF_FACEBOOK] = $this->get_facebook_count();
67
- $sns_counts[SNSCountCache::REF_GPLUS] = $this->get_gplus_count();
68
-
69
- return $sns_counts;
70
- }
71
- */
72
-
73
- /**
74
- * Implementation of abstract method. this method gets each share count
75
- *
76
- * @since 0.1.1
77
- */
78
- public function get_data($url){
79
- $this->log('[' . __METHOD__ . '] (line='. __LINE__ . ')');
80
-
81
- $url=rawurlencode($url);
82
-
83
- $sns_counts = array();
84
-
85
- $sns_counts[SNSCountCache::REF_HATEBU] = $this->get_hatebu_count($url);
86
- $sns_counts[SNSCountCache::REF_TWITTER] = $this->get_twitter_count($url);
87
- $sns_counts[SNSCountCache::REF_FACEBOOK] = $this->get_facebook_count($url);
88
- $sns_counts[SNSCountCache::REF_GPLUS] = $this->get_gplus_count($url);
89
-
90
- return $sns_counts;
91
- }
92
-
93
- /**
94
- * Get share count for Hatena Bookmark
95
- *
96
- * @since 0.1.0
97
- */
98
- public function get_hatebu_count($url) {
99
- $this->log('[' . __METHOD__ . '] (line='. __LINE__ . ')');
100
-
101
- $query = 'http://api.b.st-hatena.com/entry.count?url=' . $url;
102
-
103
- $hatebu = $this->file_get_contents($query);
104
-
105
- return isset($hatebu) ? intval($hatebu) : 0;
106
- }
107
-
108
- /**
109
- * Get share count for Twitter
110
- *
111
- * @since 0.1.0
112
- */
113
- public function get_twitter_count($url) {
114
- $this->log('[' . __METHOD__ . '] (line='. __LINE__ . ')');
115
-
116
- $query = 'http://urls.api.twitter.com/1/urls/count.json?url=' . $url;
117
-
118
- $json = $this->file_get_contents($query);
119
-
120
- $twitter = json_decode($json, true);
121
-
122
- return isset($twitter['count']) ? intval($twitter['count']) : 0;
123
- }
124
-
125
- /**
126
- * Get share count for Facebook
127
- *
128
- * @since 0.1.0
129
- */
130
- public function get_facebook_count($url) {
131
- $this->log('[' . __METHOD__ . '] (line='. __LINE__ . ')');
132
-
133
- $query = 'http://graph.facebook.com/' . $url;
134
-
135
- $json = $this->file_get_contents($query);
136
-
137
- $facebook = json_decode($json, true);
138
-
139
- return isset($facebook['shares']) ? intval($facebook['shares']) : 0;
140
- }
141
-
142
- /**
143
- * Get share count for Google Plus
144
- *
145
- * @since 0.1.0
146
- */
147
- public function get_gplus_count($url) {
148
- $this->log('[' . __METHOD__ . '] (line='. __LINE__ . ')');
149
-
150
- $curl = curl_init();
151
-
152
- curl_setopt($curl, CURLOPT_URL, "https://clients6.google.com/rpc");
153
- curl_setopt($curl, CURLOPT_POST, true);
154
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
155
- curl_setopt($curl, CURLOPT_POSTFIELDS, '[{"method":"pos.plusones.get","id":"p","params":{"nolog":true,"id":"' . rawurldecode($url) . '","source":"widget","userId":"@viewer","groupId":"@self"},"jsonrpc":"2.0","key":"p","apiVersion":"v1"}]');
156
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
157
- curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
158
-
159
- $curl_results = curl_exec ($curl);
160
-
161
- if(curl_error($curl)) {
162
- die(curl_error($curl));
163
- }
164
-
165
- curl_close ($curl);
166
-
167
- $json = json_decode($curl_results, true);
168
-
169
- return isset($json[0]['result']['metadata']['globalCounts']['count']) ? intval( $json[0]['result']['metadata']['globalCounts']['count'] ) : 0;
170
- }
171
-
172
- /**
173
- * Get content from given URL using cURL
174
- *
175
- * @since 0.1.0
176
- */
177
- private function file_get_contents($url){
178
- $this->log('[' . __METHOD__ . '] (line='. __LINE__ . ')');
179
-
180
- $curl = curl_init();
181
-
182
- curl_setopt($curl, CURLOPT_URL, $url);
183
- curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
184
- curl_setopt($curl, CURLOPT_FAILONERROR, true);
185
- curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
186
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
187
- curl_setopt($curl, CURLOPT_TIMEOUT, $this->timeout);
188
-
189
- $curl_results = curl_exec($curl);
190
-
191
- if(curl_error($curl)) {
192
- die(curl_error($curl));
193
- }
194
-
195
- curl_close ($curl);
196
-
197
- return $curl_results;
198
- }
199
-
200
- }
201
-
202
- ?>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
readme.txt CHANGED
@@ -3,17 +3,18 @@ Contributors: marubon
3
  Donate link:
4
  Tags: performance, SNS, social, cache
5
  Requires at least: 3.7
6
- Tested up to: 3.9.2
7
- Stable tag: 0.2.0
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
 
10
 
11
  This plugin gets and caches SNS share count in the background, and provides functions to access the cache.
12
 
13
 
14
  == Description ==
15
 
16
- SNS Count Cache gets share count for Twitter and Facebook, Google Plus, Hatena Bookmark and caches these count in the background.
17
  This plugin may help you to shorten page loading time because the share count can be retrieved not through network but through the cache using given functions.
18
 
19
  The following shows functions to get share count from the cache:
@@ -21,8 +22,17 @@ The following shows functions to get share count from the cache:
21
  * get_scc_twitter()
22
  * get_scc_facebook()
23
  * get_scc_gplus()
 
24
  * get_scc_hatebu()
25
 
 
 
 
 
 
 
 
 
26
  == Installation ==
27
 
28
  1. Download zip archive file from this repository.
@@ -39,8 +49,9 @@ There are no questions.
39
 
40
  == Screenshots ==
41
  1. Cache status is described in setting page
42
- 2. Described parameters can be modified in this page
43
- 3. Help page shows available functions to access the cache
 
44
 
45
  == Changelog ==
46
 
@@ -52,6 +63,13 @@ There are no questions.
52
  * Added: function to cache SNS share count for latest posts and pages preferentially
53
  * Added: function to cache SNS share count based on user access dynamically
54
 
 
 
 
 
 
 
 
55
  == Upgrade Notice ==
56
  There is no upgrade notice.
57
 
3
  Donate link:
4
  Tags: performance, SNS, social, cache
5
  Requires at least: 3.7
6
+ Tested up to: 4.0
7
+ Stable tag: 0.3.0
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
+ PHP Version: 5.3+
11
 
12
  This plugin gets and caches SNS share count in the background, and provides functions to access the cache.
13
 
14
 
15
  == Description ==
16
 
17
+ SNS Count Cache gets share count for Twitter and Facebook, Google Plus, Pocket, Hatena Bookmark and caches these count in the background.
18
  This plugin may help you to shorten page loading time because the share count can be retrieved not through network but through the cache using given functions.
19
 
20
  The following shows functions to get share count from the cache:
22
  * get_scc_twitter()
23
  * get_scc_facebook()
24
  * get_scc_gplus()
25
+ * get_scc_pocket()
26
  * get_scc_hatebu()
27
 
28
+ The following describes meta keys to get share count from custom field.
29
+
30
+ * scc_share_count_twitter
31
+ * scc_share_count_facebook
32
+ * scc_share_count_google+
33
+ * scc_share_count_pocket
34
+ * scc_share_count_hatebu
35
+
36
  == Installation ==
37
 
38
  1. Download zip archive file from this repository.
49
 
50
  == Screenshots ==
51
  1. Cache status is described in setting page
52
+ 2. Share count for each post can be verviewed
53
+ 3. Described parameters can be modified in this page
54
+ 4. Help page shows available functions to access the cache
55
 
56
  == Changelog ==
57
 
63
  * Added: function to cache SNS share count for latest posts and pages preferentially
64
  * Added: function to cache SNS share count based on user access dynamically
65
 
66
+ = 0.3.0 =
67
+ * Added: Pocket was included as one of cache targets.
68
+ * Added: function to modify target SNS that share count is cached
69
+ * Added: function to modify term considering posted content as new content in the rush cache.
70
+ * Added: page to display share count for specified all tagets.
71
+ * Added: function to query pages and posts based on SNS share count using specific custom fields in WP_Query and so on.
72
+
73
  == Upgrade Notice ==
74
  There is no upgrade notice.
75
 
screenshot-1.png CHANGED
Binary file
screenshot-2.png CHANGED
Binary file
screenshot-3.png CHANGED
Binary file
screenshot-4.png ADDED
Binary file
sns-count-cache.php CHANGED
@@ -1,8 +1,8 @@
1
  <?php
2
  /*
3
  Plugin Name: SNS Count Cache
4
- Description: SNS Count Cache gets share count for Twitter and Facebook, Google Plus, Hatena Bookmark and caches these count in the background. This plugin may help you to shorten page loading time because the share count can be retrieved not through network but through the cache using given functions.
5
- Version: 0.2.0
6
  Author: Daisuke Maruyama
7
  Author URI: http://marubon.info/
8
  License: GPL2 or later
@@ -29,18 +29,19 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
29
 
30
  */
31
 
32
- require_once (dirname(__FILE__) . '/module/data-cache-engine.php');
33
- require_once (dirname(__FILE__) . '/module/data-crawler.php');
34
- require_once (dirname(__FILE__) . '/module/sns-count-crawler.php');
 
35
 
36
- if (!class_exists('SNSCountCache')){
37
 
38
- class SNSCountCache {
39
 
40
  /**
41
  * Plugin version, used for cache-busting of style and script file references.
42
  */
43
- private $version = '0.2.0';
44
 
45
  /**
46
  * Instance of module class of crawler
@@ -61,7 +62,12 @@ class SNSCountCache {
61
  * Option flag of dynamic cache processing
62
  */
63
  private $dynamic_cache = 0;
64
-
 
 
 
 
 
65
  /**
66
  * Prefix of cache ID
67
  */
@@ -117,6 +123,41 @@ class SNSCountCache {
117
  */
118
  const OPT_RUSH_EVENT_DESCRIPTION = '[SCC] Share Count Cache Rush Interval';
119
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
120
  /**
121
  * Cron name to execute cache processing
122
  */
@@ -137,6 +178,11 @@ class SNSCountCache {
137
  */
138
  const OPT_ACCESS_BASED_ASYNC_CACHE = 2;
139
 
 
 
 
 
 
140
  /**
141
  * Option key for interval cheking and caching target data
142
  */
@@ -151,6 +197,16 @@ class SNSCountCache {
151
  * Option key for dynamic cache processing
152
  */
153
  const DB_DYNAMIC_CACHE = 'scc_dynamic_cache';
 
 
 
 
 
 
 
 
 
 
154
 
155
  /**
156
  * Slug of the plugin
@@ -160,22 +216,27 @@ class SNSCountCache {
160
  /**
161
  * ID of share count (Twitter)
162
  */
163
- const REF_TWITTER = 'twitter';
164
 
165
  /**
166
  * ID of share count (Facebook)
167
  */
168
- const REF_FACEBOOK = 'facebook';
169
 
170
  /**
171
  * ID of share count (Google Plus)
172
  */
173
- const REF_GPLUS = 'gplus';
174
 
175
  /**
176
  * ID of share count (Hatena Bookmark)
177
  */
178
- const REF_HATEBU = 'hatebu';
 
 
 
 
 
179
 
180
  /**
181
  * Instance
@@ -187,19 +248,19 @@ class SNSCountCache {
187
  * Hook onto all of the actions and filters needed by the plugin.
188
  */
189
  private function __construct() {
190
- $this->log('[' . __METHOD__ . '] (line='. __LINE__ . ')');
191
 
192
  //load_plugin_textdomain(self::DOMAIN, false, basename(dirname( __FILE__ )) . '/languages');
193
 
194
- register_activation_hook( __FILE__, array($this, 'activate_plugin'));
195
- register_deactivation_hook(__FILE__, array($this, 'deactivate_plugin'));
196
 
197
- add_action('admin_menu', array($this, 'action_admin_menu'));
198
 
199
- add_action('admin_print_styles', array($this, 'register_admin_styles'));
200
- add_action('admin_enqueue_scripts', array($this, 'register_admin_scripts'));
201
 
202
- add_action('plugins_loaded', array($this,'initialize'));
203
  }
204
 
205
  /**
@@ -210,7 +271,7 @@ class SNSCountCache {
210
  public static function get_instance() {
211
 
212
  $class_name = get_called_class();
213
- if(!self::$instance) {
214
  self::$instance = new $class_name();
215
  }
216
 
@@ -222,24 +283,40 @@ class SNSCountCache {
222
  *
223
  * @since 0.1.1
224
  */
225
- public function initialize(){
226
- $this->log('[' . __METHOD__ . '] (line='. __LINE__ . ')');
 
 
 
 
 
 
227
 
228
- $check_interval = get_option(self::DB_CHECK_INTERVAL);
229
- $posts_per_check = get_option(self::DB_POSTS_PER_CHECK);
230
 
231
- $check_interval = !empty($check_interval) ? intval($check_interval) : self::OPT_BASE_CHECK_INTERVAL;
232
- $posts_per_check = !empty($posts_per_check) ? intval($posts_per_check) : self::OPT_BASE_POSTS_PER_CHECK;
233
 
234
- $dynamic_cache = get_option(self::DB_DYNAMIC_CACHE);
235
- $this->dynamic_cache = !empty($dynamic_cache) ? $dynamic_cache : false;
236
 
237
- $this->log('[' . __METHOD__ . '] check_interval: ' . $check_interval);
238
- $this->log('[' . __METHOD__ . '] posts_per_check: ' . $posts_per_check);
 
 
 
 
 
 
 
239
 
240
- $this->crawler = SNSCountCrawler::get_instance();
 
 
 
241
 
242
  $options = array(
 
243
  'base_check_interval' => $check_interval,
244
  'base_posts_per_check' => $posts_per_check,
245
  'base_transient_prefix' => self::OPT_BASE_TRANSIENT_PREFIX,
@@ -251,11 +328,18 @@ class SNSCountCache {
251
  'rush_cache_execute_cron' => self::OPT_RUSH_CACHE_EXECUTE_CRON,
252
  'rush_event_schedule' => self::OPT_RUSH_EVENT_SCHEDULE,
253
  'rush_event_description' => self::OPT_RUSH_EVENT_DESCRIPTION,
254
- 'lazy_cache_execute_cron' => self::OPT_LAZY_CACHE_EXECUTE_CRON
 
 
 
 
 
 
 
255
  );
256
 
257
- $this->cache_engine = DataCacheEngine::get_instance();
258
- $this->cache_engine->initialize($this->crawler, $options);
259
 
260
  }
261
 
@@ -265,17 +349,17 @@ class SNSCountCache {
265
  * @since 0.1.0
266
  */
267
  public function register_admin_styles() {
268
- $this->log('[' . __METHOD__ . '] (line='. __LINE__ . ')');
269
 
270
- if (!isset($this->plugin_screen_hook_suffix)) {
271
  return;
272
  }
273
 
274
  $screen = get_current_screen();
275
 
276
- if ($screen->id == $this->plugin_screen_hook_suffix) {
277
- wp_enqueue_style(self::DOMAIN .'-admin-style-1' , plugins_url(ltrim('/css/sns-count-cache.css', '/'), __FILE__));
278
- wp_enqueue_style(self::DOMAIN .'-admin-style-2' , plugins_url(ltrim('/css/prettify.css', '/'), __FILE__));
279
  }
280
 
281
  }
@@ -286,17 +370,17 @@ class SNSCountCache {
286
  * @since 0.1.0
287
  */
288
  public function register_admin_scripts() {
289
- $this->log('[' . __METHOD__ . '] (line='. __LINE__ . ')');
290
 
291
- if (!isset( $this->plugin_screen_hook_suffix)) {
292
  return;
293
  }
294
 
295
  $screen = get_current_screen();
296
 
297
- if ($screen->id == $this->plugin_screen_hook_suffix) {
298
- wp_enqueue_script(self::DOMAIN . '-admin-script-1' , plugins_url(ltrim('/js/jquery.sns-count-cache.js', '/') , __FILE__ ), array( 'jquery' ));
299
- wp_enqueue_script(self::DOMAIN . '-admin-script-2' , plugins_url(ltrim('/js/prettify.js', '/') , __FILE__ ), array( 'jquery' ));
300
  }
301
 
302
  }
@@ -306,8 +390,8 @@ class SNSCountCache {
306
  *
307
  * @since 0.1.1
308
  */
309
- function activate_plugin(){
310
- $this->log('[' . __METHOD__ . '] (line='. __LINE__ . ')');
311
 
312
  $this->initialize();
313
  $this->cache_engine->register_base_schedule();
@@ -318,8 +402,8 @@ class SNSCountCache {
318
  *
319
  * @since 0.1.1
320
  */
321
- function deactivate_plugin(){
322
- $this->log('[' . __METHOD__ . '] (line='. __LINE__ . ')');
323
 
324
  $this->cache_engine->unregister_base_schedule();
325
  }
@@ -330,7 +414,7 @@ class SNSCountCache {
330
  * @since 0.1.1
331
  */
332
  function reactivate_plugin() {
333
- $this->log('[' . __METHOD__ . '] (line='. __LINE__ . ')');
334
 
335
  $this->cache_engine->unregister_base_schedule();
336
  $this->initialize();
@@ -345,7 +429,7 @@ class SNSCountCache {
345
  * @since 0.1.0
346
  */
347
  public function action_admin_menu() {
348
- $this->plugin_screen_hook_suffix = add_options_page('SNS Count Cache', 'SNS Count Cache', 8, 'sns_count_cache_options_page',array($this, 'option_page'));
349
  }
350
 
351
  /**
@@ -353,8 +437,8 @@ class SNSCountCache {
353
  *
354
  * @since 0.1.0
355
  */
356
- public function option_page(){
357
- include_once(dirname(__FILE__) . '/admin.php');
358
  }
359
 
360
  /**
@@ -362,12 +446,12 @@ class SNSCountCache {
362
  *
363
  * @since 0.1.0
364
  */
365
- private function log($message) {
366
  if (WP_DEBUG === true) {
367
- if (is_array($message) || is_object($message)) {
368
- error_log(print_r($message, true));
369
  } else {
370
- error_log($message);
371
  }
372
  }
373
  }
@@ -377,10 +461,10 @@ class SNSCountCache {
377
  *
378
  * @since 0.2.0
379
  */
380
- public function get_dynamic_cache_type(){
381
- $this->log('[' . __METHOD__ . '] (line='. __LINE__ . ')');
382
 
383
- $this->log('[' . __METHOD__ . '] dynamic cache: ' . $this->dynamic_cache);
384
 
385
  return $this->dynamic_cache;
386
  }
@@ -391,10 +475,10 @@ class SNSCountCache {
391
  *
392
  * @since 0.2.0
393
  */
394
- public function retrieve_count_cache($post_ID){
395
- $this->log('[' . __METHOD__ . '] (line='. __LINE__ . ')');
396
 
397
- return $this->cache_engine->execute_direct_data_cache($post_ID);
398
  }
399
 
400
  /**
@@ -402,51 +486,120 @@ class SNSCountCache {
402
  *
403
  * @since 0.2.0
404
  */
405
- public function reserve_count_cache($post_ID){
406
- $this->log('[' . __METHOD__ . '] (line='. __LINE__ . ')');
407
 
408
- $this->cache_engine->prime_lazy_data_cache($post_ID);
409
  }
 
 
 
 
 
 
 
 
 
 
 
 
410
 
411
  }
412
 
413
- SNSCountCache::get_instance();
414
-
415
  /**
416
- * Get share count from cache (Hatena Bookmark).
417
  *
418
  * @since 0.1.0
419
- */
420
- function get_scc_hatebu($post_ID='') {
421
  $transient_ID ='';
 
422
  $sns_counts = array();
423
-
424
- if(empty($post_ID)){
 
 
425
  $post_ID = get_the_ID();
426
  }
427
-
428
- $transient_ID = SNSCountCache::OPT_BASE_TRANSIENT_PREFIX . $post_ID;
429
 
430
- if(false !== ($sns_counts = get_transient($transient_ID))){
431
- return $sns_counts[SNSCountCache::REF_HATEBU];
 
 
 
 
 
 
 
 
 
 
432
  } else {
433
- $sns_count_cache = SNSCountCache::get_instance();
434
-
435
- switch($sns_count_cache->get_dynamic_cache_type()){
436
- case SNSCountCache::OPT_ACCESS_BASED_CACHE_NONE:
437
- return $sns_counts[SNSCountCache::REF_HATEBU];
 
 
 
 
 
 
 
 
 
 
 
 
438
  break;
439
- case SNSCountCache::OPT_ACCESS_BASED_SYNC_CACHE:
440
- $sns_counts = $sns_count_cache->retrieve_count_cache($post_ID);
441
- return $sns_counts[SNSCountCache::REF_HATEBU];
 
 
 
 
442
  break;
443
- case SNSCountCache::OPT_ACCESS_BASED_ASYNC_CACHE:
444
- $sns_count_cache->reserve_count_cache($post_ID);
445
- return $sns_counts[SNSCountCache::REF_HATEBU];
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
446
  break;
447
  }
448
- }
 
449
 
 
 
 
 
 
 
 
 
 
 
450
  }
451
 
452
  /**
@@ -454,35 +607,10 @@ function get_scc_hatebu($post_ID='') {
454
  *
455
  * @since 0.1.0
456
  */
457
- function get_scc_twitter($post_ID='') {
458
- $transient_ID ='';
459
- $sns_counts = array();
460
 
461
- if(empty($post_ID)){
462
- $post_ID = get_the_ID();
463
- }
464
-
465
- $transient_ID = SNSCountCache::OPT_BASE_TRANSIENT_PREFIX . $post_ID;
466
-
467
- if(false !== ($sns_counts = get_transient($transient_ID))){
468
- return $sns_counts[SNSCountCache::REF_TWITTER];
469
- } else {
470
- $sns_count_cache = SNSCountCache::get_instance();
471
-
472
- switch($sns_count_cache->get_dynamic_cache_type()){
473
- case SNSCountCache::OPT_ACCESS_BASED_CACHE_NONE:
474
- return $sns_counts[SNSCountCache::REF_TWITTER];
475
- break;
476
- case SNSCountCache::OPT_ACCESS_BASED_SYNC_CACHE:
477
- $sns_counts = $sns_count_cache->retrieve_count_cache($post_ID);
478
- return $sns_counts[SNSCountCache::REF_TWITTER];
479
- break;
480
- case SNSCountCache::OPT_ACCESS_BASED_ASYNC_CACHE:
481
- $sns_count_cache->reserve_count_cache($post_ID);
482
- return $sns_counts[SNSCountCache::REF_TWITTER];
483
- break;
484
- }
485
- }
486
 
487
  }
488
 
@@ -491,35 +619,10 @@ function get_scc_twitter($post_ID='') {
491
  *
492
  * @since 0.1.0
493
  */
494
- function get_scc_facebook($post_ID='') {
495
- $transient_ID ='';
496
- $sns_counts = array();
497
 
498
- if(empty($post_ID)){
499
- $post_ID = get_the_ID();
500
- }
501
-
502
- $transient_ID = SNSCountCache::OPT_BASE_TRANSIENT_PREFIX . $post_ID;
503
-
504
- if(false !== ($sns_counts = get_transient($transient_ID))){
505
- return $sns_counts[SNSCountCache::REF_FACEBOOK];
506
- } else {
507
- $sns_count_cache = SNSCountCache::get_instance();
508
-
509
- switch($sns_count_cache->get_dynamic_cache_type()){
510
- case SNSCountCache::OPT_ACCESS_BASED_CACHE_NONE:
511
- return $sns_counts[SNSCountCache::REF_FACEBOOK];
512
- break;
513
- case SNSCountCache::OPT_ACCESS_BASED_SYNC_CACHE:
514
- $sns_counts = $sns_count_cache->retrieve_count_cache($post_ID);
515
- return $sns_counts[SNSCountCache::REF_FACEBOOK];
516
- break;
517
- case SNSCountCache::OPT_ACCESS_BASED_ASYNC_CACHE:
518
- $sns_count_cache->reserve_count_cache($post_ID);
519
- return $sns_counts[SNSCountCache::REF_FACEBOOK];
520
- break;
521
- }
522
- }
523
 
524
  }
525
 
@@ -528,72 +631,23 @@ function get_scc_facebook($post_ID='') {
528
  *
529
  * @since 0.1.0
530
  */
531
- function get_scc_gplus($post_ID='') {
532
- $transient_ID ='';
533
- $sns_counts = array();
534
-
535
- if(empty($post_ID)){
536
- $post_ID = get_the_ID();
537
- }
538
-
539
- $transient_ID = SNSCountCache::OPT_BASE_TRANSIENT_PREFIX . $post_ID;
540
-
541
- if(false !== ($sns_counts = get_transient($transient_ID))){
542
- return $sns_counts[SNSCountCache::REF_GPLUS];
543
- } else {
544
- $sns_count_cache = SNSCountCache::get_instance();
545
-
546
- switch($sns_count_cache->get_dynamic_cache_type()){
547
- case SNSCountCache::OPT_ACCESS_BASED_CACHE_NONE:
548
- return $sns_counts[SNSCountCache::REF_GPLUS];
549
- break;
550
- case SNSCountCache::OPT_ACCESS_BASED_SYNC_CACHE:
551
- $sns_counts = $sns_count_cache->retrieve_count_cache($post_ID);
552
- return $sns_counts[SNSCountCache::REF_GPLUS];
553
- break;
554
- case SNSCountCache::OPT_ACCESS_BASED_ASYNC_CACHE:
555
- $sns_count_cache->reserve_count_cache($post_ID);
556
- return $sns_counts[SNSCountCache::REF_GPLUS];
557
- break;
558
- }
559
- }
560
 
 
 
 
561
  }
562
 
563
  /**
564
- * Get share count from cache
565
  *
566
- * @since 0.1.0
567
  */
568
- function get_scc($post_ID='') {
569
- $transient_ID ='';
570
- $sns_counts = array();
571
-
572
- if(empty($post_ID)){
573
- $post_ID = get_the_ID();
574
- }
575
-
576
- $transient_ID = SNSCountCache::OPT_BASE_TRANSIENT_PREFIX . $post_ID;
577
 
578
- if(false !== ($sns_counts = get_transient($transient_ID))){
579
- return $sns_counts;
580
- } else {
581
- $sns_count_cache = SNSCountCache::get_instance();
582
-
583
- switch($sns_count_cache->get_dynamic_cache_type()){
584
- case SNSCountCache::OPT_ACCESS_BASED_CACHE_NONE:
585
- return $sns_counts;
586
- break;
587
- case SNSCountCache::OPT_ACCESS_BASED_SYNC_CACHE:
588
- $sns_counts = $sns_count_cache->retrieve_count_cache($post_ID);
589
- return $sns_counts;
590
- break;
591
- case SNSCountCache::OPT_ACCESS_BASED_ASYNC_CACHE:
592
- $sns_count_cache->reserve_count_cache($post_ID);
593
- return $sns_counts;
594
- break;
595
- }
596
- }
597
  }
598
 
599
  }
1
  <?php
2
  /*
3
  Plugin Name: SNS Count Cache
4
+ Description: SNS Count Cache gets share count for Twitter and Facebook, Google Plus, Pocket, Hatena Bookmark and caches these count in the background. This plugin may help you to shorten page loading time because the share count can be retrieved not through network but through the cache using given functions.
5
+ Version: 0.3.0
6
  Author: Daisuke Maruyama
7
  Author URI: http://marubon.info/
8
  License: GPL2 or later
29
 
30
  */
31
 
32
+ require_once ( dirname( __FILE__ ) . '/includes/class-data-cache-engine.php' );
33
+ require_once ( dirname( __FILE__ ) . '/includes/class-data-crawler.php' );
34
+ require_once ( dirname( __FILE__ ) . '/includes/class-sns-count-crawler.php' );
35
+ require_once ( dirname( __FILE__ ) . '/includes/class-common-util.php' );
36
 
37
+ if ( ! class_exists( 'SNS_Count_Cache' ) ) {
38
 
39
+ class SNS_Count_Cache {
40
 
41
  /**
42
  * Plugin version, used for cache-busting of style and script file references.
43
  */
44
+ private $version = '0.3.0';
45
 
46
  /**
47
  * Instance of module class of crawler
62
  * Option flag of dynamic cache processing
63
  */
64
  private $dynamic_cache = 0;
65
+
66
+ /**
67
+ * Option flag of dynamic cache processing
68
+ */
69
+ private $base_cache_target = array();
70
+
71
  /**
72
  * Prefix of cache ID
73
  */
123
  */
124
  const OPT_RUSH_EVENT_DESCRIPTION = '[SCC] Share Count Cache Rush Interval';
125
 
126
+ /**
127
+ * Type of dynamic cache processing
128
+ */
129
+ const OPT_RUSH_NEW_CONTENT_TERM = 3;
130
+
131
+ /**
132
+ * Interval ranking
133
+ */
134
+ const OPT_2ND_CHECK_INTERVAL = 600;
135
+
136
+ /**
137
+ * Cron name to schedule rank processing
138
+ */
139
+ const OPT_2ND_CACHE_PRIME_CRON = 'scc_2ndcache_prime';
140
+
141
+ /**
142
+ * Cron name to execute rank processing
143
+ */
144
+ const OPT_2ND_CACHE_EXECUTE_CRON = 'scc_2ndcache_exec';
145
+
146
+ /**
147
+ * Schedule name for rank processing
148
+ */
149
+ const OPT_2ND_EVENT_SCHEDULE = '2nd_cache_event';
150
+
151
+ /**
152
+ * Schedule description for rank processing
153
+ */
154
+ const OPT_2ND_EVENT_DESCRIPTION = '[SCC] Share Count Cache 2nd Cache Interval';
155
+
156
+ /**
157
+ * Meta key for rank processing
158
+ */
159
+ const OPT_2ND_META_KEY_PREFIX = 'scc_share_count_';
160
+
161
  /**
162
  * Cron name to execute cache processing
163
  */
178
  */
179
  const OPT_ACCESS_BASED_ASYNC_CACHE = 2;
180
 
181
+ /**
182
+ * Type of dynamic cache processing
183
+ */
184
+ const OPT_ACCESS_BASED_2ND_CACHE = 3;
185
+
186
  /**
187
  * Option key for interval cheking and caching target data
188
  */
197
  * Option key for dynamic cache processing
198
  */
199
  const DB_DYNAMIC_CACHE = 'scc_dynamic_cache';
200
+
201
+ /**
202
+ * Option key for dynamic cache processing
203
+ */
204
+ const DB_NEW_CONTENT_TERM = 'scc_new_content_term';
205
+
206
+ /**
207
+ * Option key of cache target
208
+ */
209
+ const DB_CACHE_TARGET = 'scc_cache_target';
210
 
211
  /**
212
  * Slug of the plugin
216
  /**
217
  * ID of share count (Twitter)
218
  */
219
+ const REF_TWITTER = 'Twitter';
220
 
221
  /**
222
  * ID of share count (Facebook)
223
  */
224
+ const REF_FACEBOOK = 'Facebook';
225
 
226
  /**
227
  * ID of share count (Google Plus)
228
  */
229
+ const REF_GPLUS = 'Google+';
230
 
231
  /**
232
  * ID of share count (Hatena Bookmark)
233
  */
234
+ const REF_HATEBU = 'Hatebu';
235
+
236
+ /**
237
+ * ID of share count (Pocket)
238
+ */
239
+ const REF_POCKET = 'Pocket';
240
 
241
  /**
242
  * Instance
248
  * Hook onto all of the actions and filters needed by the plugin.
249
  */
250
  private function __construct() {
251
+ $this->log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
252
 
253
  //load_plugin_textdomain(self::DOMAIN, false, basename(dirname( __FILE__ )) . '/languages');
254
 
255
+ register_activation_hook( __FILE__, array( $this, 'activate_plugin' ) );
256
+ register_deactivation_hook( __FILE__, array( $this, 'deactivate_plugin' ) );
257
 
258
+ add_action( 'admin_menu', array($this, 'action_admin_menu' ) );
259
 
260
+ add_action( 'admin_print_styles', array( $this, 'register_admin_styles' ) );
261
+ add_action( 'admin_enqueue_scripts', array($this, 'register_admin_scripts' ) );
262
 
263
+ add_action( 'plugins_loaded', array( $this, 'initialize' ) );
264
  }
265
 
266
  /**
271
  public static function get_instance() {
272
 
273
  $class_name = get_called_class();
274
+ if ( ! self::$instance ) {
275
  self::$instance = new $class_name();
276
  }
277
 
283
  *
284
  * @since 0.1.1
285
  */
286
+ public function initialize() {
287
+ $this->log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
288
+
289
+ $check_interval = get_option( self::DB_CHECK_INTERVAL );
290
+ $posts_per_check = get_option( self::DB_POSTS_PER_CHECK );
291
+
292
+ $check_interval = ! empty( $check_interval ) ? intval( $check_interval ) : self::OPT_BASE_CHECK_INTERVAL;
293
+ $posts_per_check = ! empty( $posts_per_check ) ? intval( $posts_per_check ) : self::OPT_BASE_POSTS_PER_CHECK;
294
 
295
+ $dynamic_cache = get_option( self::DB_DYNAMIC_CACHE );
296
+ $this->dynamic_cache = ! empty( $dynamic_cache ) ? $dynamic_cache : false;
297
 
298
+ $new_content_term = get_option( self::DB_NEW_CONTENT_TERM );
299
+ $new_content_term = ! empty( $new_content_term ) ? intval( $new_content_term ) : self::OPT_RUSH_NEW_CONTENT_TERM;
300
 
301
+ $this->base_cache_target = get_option( self::DB_CACHE_TARGET );
 
302
 
303
+ if ( empty( $this->base_cache_target ) ) {
304
+ $this->base_cache_target[self::REF_TWITTER] = true;
305
+ $this->base_cache_target[self::REF_FACEBOOK] = true;
306
+ $this->base_cache_target[self::REF_GPLUS] = true;
307
+ if ( extension_loaded( 'xml' ) ) {
308
+ $this->base_cache_target[self::REF_POCKET] = true;
309
+ }
310
+ $this->base_cache_target[self::REF_HATEBU] = true;
311
+ }
312
 
313
+ $this->log( '[' . __METHOD__ . '] check_interval: ' . $check_interval );
314
+ $this->log( '[' . __METHOD__ . '] posts_per_check: ' . $posts_per_check );
315
+
316
+ $this->crawler = SNS_Count_Crawler::get_instance();
317
 
318
  $options = array(
319
+ 'base_cache_target' => $this->base_cache_target,
320
  'base_check_interval' => $check_interval,
321
  'base_posts_per_check' => $posts_per_check,
322
  'base_transient_prefix' => self::OPT_BASE_TRANSIENT_PREFIX,
328
  'rush_cache_execute_cron' => self::OPT_RUSH_CACHE_EXECUTE_CRON,
329
  'rush_event_schedule' => self::OPT_RUSH_EVENT_SCHEDULE,
330
  'rush_event_description' => self::OPT_RUSH_EVENT_DESCRIPTION,
331
+ 'rush_new_content_term' => $new_content_term,
332
+ 'lazy_cache_execute_cron' => self::OPT_LAZY_CACHE_EXECUTE_CRON,
333
+ 'second_check_interval' => self::OPT_2ND_CHECK_INTERVAL,
334
+ 'second_cache_prime_cron' => self::OPT_2ND_CACHE_PRIME_CRON,
335
+ 'second_cache_execute_cron' => self::OPT_2ND_CACHE_EXECUTE_CRON,
336
+ 'second_event_schedule' => self::OPT_2ND_EVENT_SCHEDULE,
337
+ 'second_event_description' => self::OPT_2ND_EVENT_DESCRIPTION,
338
+ 'second_meta_key_prefix' => self::OPT_2ND_META_KEY_PREFIX
339
  );
340
 
341
+ $this->cache_engine = Data_Cache_Engine::get_instance();
342
+ $this->cache_engine->initialize( $this->crawler, $options );
343
 
344
  }
345
 
349
  * @since 0.1.0
350
  */
351
  public function register_admin_styles() {
352
+ $this->log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
353
 
354
+ if ( ! isset( $this->plugin_screen_hook_suffix ) ) {
355
  return;
356
  }
357
 
358
  $screen = get_current_screen();
359
 
360
+ if ( $screen->id == $this->plugin_screen_hook_suffix ) {
361
+ wp_enqueue_style( self::DOMAIN .'-admin-style-1' , plugins_url( ltrim( '/css/sns-count-cache.css', '/' ), __FILE__) );
362
+ wp_enqueue_style( self::DOMAIN .'-admin-style-2' , plugins_url( ltrim( '/css/prettify.css', '/' ), __FILE__ ) );
363
  }
364
 
365
  }
370
  * @since 0.1.0
371
  */
372
  public function register_admin_scripts() {
373
+ $this->log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
374
 
375
+ if ( ! isset( $this->plugin_screen_hook_suffix ) ) {
376
  return;
377
  }
378
 
379
  $screen = get_current_screen();
380
 
381
+ if ( $screen->id == $this->plugin_screen_hook_suffix ) {
382
+ wp_enqueue_script( self::DOMAIN . '-admin-script-1' , plugins_url( ltrim( '/js/jquery.sns-count-cache.js', '/' ) , __FILE__ ), array( 'jquery' ) );
383
+ wp_enqueue_script( self::DOMAIN . '-admin-script-2' , plugins_url( ltrim( '/js/prettify.js', '/' ) , __FILE__ ), array( 'jquery' ) );
384
  }
385
 
386
  }
390
  *
391
  * @since 0.1.1
392
  */
393
+ function activate_plugin() {
394
+ $this->log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
395
 
396
  $this->initialize();
397
  $this->cache_engine->register_base_schedule();
402
  *
403
  * @since 0.1.1
404
  */
405
+ function deactivate_plugin() {
406
+ $this->log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
407
 
408
  $this->cache_engine->unregister_base_schedule();
409
  }
414
  * @since 0.1.1
415
  */
416
  function reactivate_plugin() {
417
+ $this->log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
418
 
419
  $this->cache_engine->unregister_base_schedule();
420
  $this->initialize();
429
  * @since 0.1.0
430
  */
431
  public function action_admin_menu() {
432
+ $this->plugin_screen_hook_suffix = add_options_page( 'SNS Count Cache', 'SNS Count Cache', 8, 'sns_count_cache_options_page', array( $this, 'option_page' ) );
433
  }
434
 
435
  /**
437
  *
438
  * @since 0.1.0
439
  */
440
+ public function option_page() {
441
+ include_once( dirname( __FILE__ ) . '/includes/admin.php' );
442
  }
443
 
444
  /**
446
  *
447
  * @since 0.1.0
448
  */
449
+ private function log( $message ) {
450
  if (WP_DEBUG === true) {
451
+ if ( is_array( $message ) || is_object( $message ) ) {
452
+ error_log( print_r( $message, true ) );
453
  } else {
454
+ error_log( $message );
455
  }
456
  }
457
  }
461
  *
462
  * @since 0.2.0
463
  */
464
+ public function get_dynamic_cache_type() {
465
+ $this->log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
466
 
467
+ $this->log( '[' . __METHOD__ . '] dynamic cache: ' . $this->dynamic_cache );
468
 
469
  return $this->dynamic_cache;
470
  }
475
  *
476
  * @since 0.2.0
477
  */
478
+ public function retrieve_count_cache( $post_ID ) {
479
+ $this->log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
480
 
481
+ return $this->cache_engine->execute_direct_data_cache( $post_ID );
482
  }
483
 
484
  /**
486
  *
487
  * @since 0.2.0
488
  */
489
+ public function reserve_count_cache( $post_ID ) {
490
+ $this->log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
491
 
492
+ $this->cache_engine->prime_lazy_data_cache( $post_ID );
493
  }
494
+
495
+ /**
496
+ * Return base cache target
497
+ *
498
+ * @since 0.2.0
499
+ */
500
+ public function get_base_cache_target() {
501
+ $this->log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
502
+
503
+ return $this->base_cache_target;
504
+ }
505
+
506
 
507
  }
508
 
509
+ SNS_Count_Cache::get_instance();
510
+
511
  /**
512
+ * Get share count from cache
513
  *
514
  * @since 0.1.0
515
+ */
516
+ function get_scc( $options = array( 'id' => '', 'url' => '', 'sns' => '' ) ) {
517
  $transient_ID ='';
518
+ $sns_key = '';
519
  $sns_counts = array();
520
+
521
+ if ( $options['id'] ) {
522
+ $post_ID = $options['id'];
523
+ } else {
524
  $post_ID = get_the_ID();
525
  }
 
 
526
 
527
+ if ( $options['sns'] ) {
528
+ $sns_key = $options['sns'];
529
+ }
530
+
531
+ $transient_ID = SNS_Count_Cache::OPT_BASE_TRANSIENT_PREFIX . $post_ID;
532
+
533
+ if ( false !== ( $sns_counts = get_transient( $transient_ID ) ) ) {
534
+ if ( $sns_key ) {
535
+ return $sns_counts[$sns_key];
536
+ } else {
537
+ return $sns_counts;
538
+ }
539
  } else {
540
+ $sns_count_cache = SNS_Count_Cache::get_instance();
541
+
542
+ switch ( $sns_count_cache->get_dynamic_cache_type() ) {
543
+ case SNS_Count_Cache::OPT_ACCESS_BASED_CACHE_NONE:
544
+ if ( $sns_key ) {
545
+ return $sns_counts[$sns_key];
546
+ } else {
547
+ return $sns_counts;
548
+ }
549
+ break;
550
+ case SNS_Count_Cache::OPT_ACCESS_BASED_SYNC_CACHE:
551
+ $sns_counts = $sns_count_cache->retrieve_count_cache( $post_ID );
552
+ if ( $sns_key ) {
553
+ return $sns_counts[$sns_key];
554
+ } else {
555
+ return $sns_counts;
556
+ }
557
  break;
558
+ case SNS_Count_Cache::OPT_ACCESS_BASED_ASYNC_CACHE:
559
+ $sns_count_cache->reserve_count_cache( $post_ID );
560
+ if ( $sns_key ) {
561
+ return $sns_counts[$sns_key];
562
+ } else {
563
+ return $sns_counts;
564
+ }
565
  break;
566
+ case SNS_Count_Cache::OPT_ACCESS_BASED_2ND_CACHE:
567
+ $sns_count_cache->reserve_count_cache( $post_ID );
568
+ if ( $sns_key ) {
569
+ $meta_key = SNS_Count_Cache::OPT_2ND_META_KEY_PREFIX . strtolower( $sns_key );
570
+ $sns_counts[$sns_key] = get_post_meta( $post_ID, $meta_key, true );
571
+ return $sns_counts[$sns_key];
572
+ } else {
573
+ $base_cache_target = $sns_count_cache->get_base_cache_target();
574
+
575
+ foreach ( $base_cache_target as $key => $value ) {
576
+ if ( $value ) {
577
+ $meta_key = SNS_Count_Cache::OPT_2ND_META_KEY_PREFIX . strtolower( $key );
578
+
579
+ $sns_count = get_post_meta( $post_ID, $meta_key, true );
580
+
581
+ if ( isset( $sns_count ) && $sns_count >= 0 ) {
582
+ $sns_counts[$key] = get_post_meta( $post_ID, $meta_key, true );
583
+ }
584
+ }
585
+ }
586
+ return $sns_counts;
587
+ }
588
  break;
589
  }
590
+ }
591
+ }
592
 
593
+ /**
594
+ * Get share count from cache (Hatena Bookmark).
595
+ *
596
+ * @since 0.1.0
597
+ */
598
+ function get_scc_hatebu( $options = array( 'id' => '', 'url' => '' ) ) {
599
+
600
+ $options['sns'] = SNS_Count_Cache::REF_HATEBU;
601
+ return get_scc( $options );
602
+
603
  }
604
 
605
  /**
607
  *
608
  * @since 0.1.0
609
  */
610
+ function get_scc_twitter( $options = array( 'id' => '', 'url' => '' ) ) {
 
 
611
 
612
+ $options['sns'] = SNS_Count_Cache::REF_TWITTER;
613
+ return get_scc( $options );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
614
 
615
  }
616
 
619
  *
620
  * @since 0.1.0
621
  */
622
+ function get_scc_facebook( $options = array( 'id' => '', 'url' => '' ) ) {
 
 
623
 
624
+ $options['sns'] = SNS_Count_Cache::REF_FACEBOOK;
625
+ return get_scc( $options );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
626
 
627
  }
628
 
631
  *
632
  * @since 0.1.0
633
  */
634
+ function get_scc_gplus( $options = array( 'id' => '', 'url' => '' ) ) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
635
 
636
+ $options['sns'] = SNS_Count_Cache::REF_GPLUS;
637
+ return get_scc( $options );
638
+
639
  }
640
 
641
  /**
642
+ * Get share count from cache (Pocket)
643
  *
644
+ * @since 0.2.1
645
  */
646
+ function get_scc_pocket( $options = array( 'id' => '', 'url' => '' ) ) {
 
 
 
 
 
 
 
 
647
 
648
+ $options['sns'] = SNS_Count_Cache::REF_POCKET;
649
+ return get_scc( $options );
650
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
651
  }
652
 
653
  }
uninstall.php CHANGED
@@ -5,12 +5,16 @@ if(!defined('WP_UNINSTALL_PLUGIN')) exit();
5
  $db_check_interval = 'scc_check_interval';
6
  $db_posts_per_check = 'scc_posts_per_check';
7
  $db_dynamic_cache = 'scc_dynamic_cache';
8
-
 
 
9
  // For Single site
10
  if(!is_multisite()){
11
  delete_option($db_check_interval);
12
  delete_option($db_posts_per_check);
13
  delete_option($db_dynamic_cache);
 
 
14
  }
15
  // For Multisite
16
  else {
@@ -24,6 +28,8 @@ else {
24
  delete_option($db_check_interval);
25
  delete_option($db_posts_per_check);
26
  delete_option($db_dynamic_cache);
 
 
27
  }
28
  switch_to_blog($original_blog_id);
29
  }
5
  $db_check_interval = 'scc_check_interval';
6
  $db_posts_per_check = 'scc_posts_per_check';
7
  $db_dynamic_cache = 'scc_dynamic_cache';
8
+ $db_new_content_term = 'scc_new_content_term';
9
+ $db_cache_target = 'scc_cache_target';
10
+
11
  // For Single site
12
  if(!is_multisite()){
13
  delete_option($db_check_interval);
14
  delete_option($db_posts_per_check);
15
  delete_option($db_dynamic_cache);
16
+ delete_option($db_new_content_term);
17
+ delete_option($db_cache_target);
18
  }
19
  // For Multisite
20
  else {
28
  delete_option($db_check_interval);
29
  delete_option($db_posts_per_check);
30
  delete_option($db_dynamic_cache);
31
+ delete_option($db_new_content_term);
32
+ delete_option($db_cache_target);
33
  }
34
  switch_to_blog($original_blog_id);
35
  }