SNS Count Cache - Version 0.2.0

Version Description

  • Added: function to modify check interval of SNS share count and its number of target posts and pages at a time
  • Added: function to cache SNS share count for latest posts and pages preferentially
  • Added: function to cache SNS share count based on user access dynamically

=

Download this release

Release Info

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

Code changes from version 0.1.0 to 0.2.0

admin.php CHANGED
@@ -3,7 +3,7 @@
3
  admin.php
4
 
5
  Description: Option page implementation
6
- Version: 0.1.0
7
  Author: Daisuke Maruyama
8
  Author URI: http://marubon.info/
9
  License: GPL2 or later
@@ -32,7 +32,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
32
 
33
  $count = 1;
34
  $query_args = array(
35
- 'post_type' => 'post',
36
  'post_status' => 'publish',
37
  'nopaging' => true,
38
  'update_post_term_cache' => false,
@@ -46,64 +46,163 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
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('Help', self::DOMAIN) ?></li>
50
  </ul>
51
  <ul class="content">
52
  <li>
53
- <table>
54
  <thead>
55
  <tr>
56
  <th>No.</th>
57
- <th><?php _e('Post', self::DOMAIN) ?></th>
58
  <th><?php _e('Cache Status', self::DOMAIN) ?></th>
59
  </tr>
60
  </thead>
61
  <tbody>
62
 
63
- <?php
64
- if($posts_query->have_posts()) {
65
- while($posts_query->have_posts()){
66
- $posts_query->the_post();
67
- ?>
68
  <tr>
69
  <td><?php echo $count; ?></td>
70
  <td><?php echo get_permalink(get_the_ID()); ?></td>
71
- <?php
72
- $transient_id = self::TRANSIENT_PREFIX . get_the_ID();
73
- if (false === ($sns_counts = get_transient($transient_id))) {
74
- echo '<td class="not-cached">';
75
- _e('not cached', self::DOMAIN);
76
- echo '</td>';
77
- } else {
78
- echo '<td class="cached">';
79
- _e('cached', self::DOMAIN);
80
- echo '</td>';
81
- }
 
82
 
83
- ?>
84
  </tr>
85
 
86
- <?php
87
- $count++;
88
 
89
- }
90
- }
91
- wp_reset_postdata();
92
- ?>
93
  </tbody>
94
  </table>
95
  </li>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96
  <li class="hide">
97
  <div>
98
- <h3>What is SNS Cout Cache?</h3>
99
- <p>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.</p>
100
- <h3>How often does this plugin get and cache share count?</h3>
101
- <p>This plugin gets share count of 20 posts at a time every 10 minutes.</p>
102
- <h3>How can I know whether share cout of each post is cached or not?</h3>
103
- <p>Cache status is described in the "Cache Status" tab in the setting page.</p>
104
- <h3>How can I get share count from the cache?</h3>
105
- <p>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().</p>
106
- <table>
107
  <thead>
108
  <tr>
109
  <th><?php _e('Function', self::DOMAIN) ?></th>
@@ -117,8 +216,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
117
  <tr><td>get_scc_hatebu()</td><td><?php _e('Hatena Bookmark share count is returned from cache.', self::DOMAIN) ?></td></tr>
118
  </tbody>
119
  </table>
120
- <h3>Example Code</h3>
121
- The code below describes a simple example which displays share count of Twitter, Facebook, Google Plus for each post.
122
  <pre class="prettyprint">&lt;?php
123
  $query_args = array(
124
  &#039;post_type&#039; =&gt; &#039;post&#039;,
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
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,
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>
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;,
css/sns-count-cache.css CHANGED
@@ -1,4 +1,4 @@
1
- .sns-cnt-cache table {
2
  border: 1px solid #000;
3
  margin: 20px 0 0 0;
4
  margin: 0px;
@@ -8,15 +8,15 @@
8
  border-spacing: 0;
9
  }
10
 
11
- .sns-cnt-cache tr:nth-child(odd) {
12
  background-color: #ffffff;
13
  }
14
 
15
- .sns-cnt-cache tr:nth-child(even) {
16
  background-color: #f9f9f9;
17
  }
18
 
19
- .sns-cnt-cache th {
20
  vertical-align: middle;
21
  padding: 3px;
22
  text-align: left;
@@ -24,7 +24,7 @@
24
  color: #fff;
25
  }
26
 
27
- .sns-cnt-cache td {
28
  vertical-align: middle;
29
  padding: 4px;
30
  text-align: left;
@@ -92,3 +92,7 @@
92
  display: none;
93
  }
94
 
 
 
 
 
1
+ .sns-cnt-cache .view-table {
2
  border: 1px solid #000;
3
  margin: 20px 0 0 0;
4
  margin: 0px;
8
  border-spacing: 0;
9
  }
10
 
11
+ .sns-cnt-cache .view-table tr:nth-child(odd) {
12
  background-color: #ffffff;
13
  }
14
 
15
+ .sns-cnt-cache .view-table tr:nth-child(even) {
16
  background-color: #f9f9f9;
17
  }
18
 
19
+ .sns-cnt-cache .view-table th {
20
  vertical-align: middle;
21
  padding: 3px;
22
  text-align: left;
24
  color: #fff;
25
  }
26
 
27
+ .sns-cnt-cache .view-table td {
28
  vertical-align: middle;
29
  padding: 4px;
30
  text-align: left;
92
  display: none;
93
  }
94
 
95
+ .sns-cnt-cache .submit-button {
96
+ margin: 15px 0 0 0;
97
+ }
98
+
module/data-cache-engine.php CHANGED
@@ -3,7 +3,7 @@
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.1.0
7
  Author: Daisuke Maruyama
8
  Author URI: http://marubon.info/
9
  License: GPL2 or later
@@ -40,150 +40,289 @@ class DataCacheEngine {
40
  /**
41
  * Interval cheking and caching target data
42
  */
43
- private $check_interval = 600;
44
 
45
  /**
46
  * Number of posts to check at a time
47
  */
48
- private $posts_per_check = 20;
49
 
50
  /**
51
  * Prefix of cache ID
52
  */
53
- private $transient_prefix = 'data_cache';
54
 
55
  /**
56
  * Cron name to schedule cache processing
57
  */
58
- private $cron_cache_prime = 'data_cache_prime';
59
 
60
  /**
61
  * Cron name to execute cache processing
62
  */
63
- private $cron_cache_execute = 'data_cache_exec';
64
 
65
  /**
66
  * Schedule name for cache processing
67
  */
68
- private $event_schedule = 'cache_event';
69
 
70
  /**
71
  * Schedule description for cache processing
72
  */
73
- private $event_description = 'cache event';
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
 
75
  /**
76
  * Class constarctor
77
  * Hook onto all of the actions and filters needed by the plugin.
78
  *
79
  */
80
- function __construct($crawler, $options=array()) {
 
81
 
82
- $this->crawler = $crawler;
83
-
84
- if(isset($options['check_interval'])) $this->check_interval = $options['check_interval'];
85
- if(isset($options['posts_per_check'])) $this->posts_per_check = $options['posts_per_check'];
86
- if(isset($options['transient_prefix'])) $this->transient_prefix = $options['transient_prefix'];
87
- if(isset($options['cron_cache_prime'])) $this->cron_cache_prime = $options['cron_cache_prime'];
88
- if(isset($options['cron_cache_execute'])) $this->cron_cache_execute = $options['cron_cache_execute'];
89
- if(isset($options['event_schedule'])) $this->event_schedule = $options['event_schedule'];
90
- if(isset($options['event_description'])) $this->event_description = $options['event_description'];
91
-
92
- add_filter('cron_schedules', array($this, 'schedule_check_interval'));
93
- add_action($this->cron_cache_prime, array($this, 'prime_data_cache'));
94
- add_action($this->cron_cache_execute, array($this, 'execute_data_cache'),10,1);
 
95
 
 
96
  }
97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98
  /**
99
  * Register base schedule for this engine
100
  *
 
101
  */
102
  public function register_base_schedule(){
 
103
 
104
- if (!wp_next_scheduled($this->cron_cache_prime)) {
105
- wp_schedule_event( time(), $this->event_schedule, $this->cron_cache_prime);
 
 
 
106
  }
107
  }
108
 
109
  /**
110
  * Unregister base schedule for this engine
111
  *
 
112
  */
113
  public function unregister_base_schedule(){
114
- wp_clear_scheduled_hook($this->cron_cache_prime);
115
- }
 
 
116
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117
  /**
118
  * Register event schedule for this engine
119
  *
 
120
  */
121
- public function schedule_check_interval($schedules) {
 
122
 
123
- $schedules[$this->event_schedule] = array(
124
- 'interval' => $this->check_interval,
125
- 'display' => $this->event_description
126
  );
127
  return $schedules;
128
  }
129
-
130
  /**
131
  * Schedule data retrieval and cache processing
132
  *
 
133
  */
134
- public function prime_data_cache(){
 
135
 
136
- $next_exec_time = time() + $this->check_interval;
137
- $posts_total = $this->get_posts_total();
138
 
139
- $this->log('[prime_data_cache] check_interval: ' . $this->check_interval);
140
- $this->log('[prime_data_cache] next_exec_time: ' . $next_exec_time);
141
- $this->log('[prime_data_cache] posts_total: ' . $posts_total);
142
 
143
- $transient_id = $this->transient_prefix . 'offset';
144
-
145
- if (false === ($posts_offset = get_transient($transient_id))) {
146
  $posts_offset = 0;
147
  }
148
 
149
- $this->log('[prime_data_cache] posts_offset: ' . $posts_offset);
150
 
151
- wp_schedule_single_event($next_exec_time, $this->cron_cache_execute, array($posts_offset));
152
 
153
- $this->log('[prime_data_cache] posts_per_check: ' . $this->posts_per_check);
154
 
155
- $posts_offset = $posts_offset + $this->posts_per_check;
156
 
157
  if($posts_offset > $posts_total){
158
  $posts_offset = 0;
159
  }
160
 
161
- delete_transient($transient_id);
162
- set_transient($transient_id, $posts_offset, $this->check_interval + $this->check_interval);
163
 
164
  }
165
-
166
  /**
167
- * Get and cache data of each published post
168
  *
 
169
  */
170
- public function execute_data_cache($posts_offset){
 
171
 
172
- $this->log('[execute_data_cache] posts_offset: ' . $posts_offset);
173
- $this->log('[execute_data_cache] posts_per_check: ' . $this->posts_per_check);
174
- $this->log('[execute_data_cache] check_interval: ' . $this->check_interval);
175
 
176
- $posts_total = $this->get_posts_total();
177
- $cache_expiration = (ceil($posts_total / $this->posts_per_check) * $this->check_interval) + 2 * $this->check_interval;
178
-
179
- $this->log('[execute_data_cache] cache_expiration: ' . $cache_expiration);
180
- $this->log('[execute_data_cache] posts_total: ' . $posts_total);
181
 
182
  $query_args = array(
183
- 'post_type' => 'post',
184
  'post_status' => 'publish',
185
  'offset' => $posts_offset,
186
- 'posts_per_page' => $this->posts_per_check,
187
  'no_found_rows' => true,
188
  'update_post_term_cache' => false,
189
  'update_post_meta_cache' => false
@@ -194,37 +333,183 @@ class DataCacheEngine {
194
  if($posts_query->have_posts()) {
195
  while($posts_query->have_posts()){
196
  $posts_query->the_post();
197
-
198
- $this->log('[execute_data_cache] post_id: ' . get_the_ID());
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
199
 
200
- $transient_id = $this->transient_prefix . get_the_ID();
201
-
202
- $url = get_permalink(get_the_ID());
203
-
204
- $this->crawler->set_url($url);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
205
 
206
- $data = $this->crawler->get_data();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
207
 
208
- $this->log($data);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
209
 
210
- delete_transient($transient_id);
211
- set_transient($transient_id, $data, $cache_expiration);
212
-
213
  }
214
  }
215
  wp_reset_postdata();
216
  }
217
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
218
  /**
219
- * Get total count of current published posts
220
  *
 
221
  */
222
- private function get_posts_total(){
 
223
 
224
  $query_args = array(
225
- 'post_type' => 'post',
226
  'post_status' => 'publish',
 
 
 
 
227
  'nopaging' => true,
 
228
  'update_post_term_cache' => false,
229
  'update_post_meta_cache' => false
230
  );
@@ -234,9 +519,121 @@ class DataCacheEngine {
234
  return $posts_query->found_posts;
235
  }
236
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
237
  /**
238
  * Output log message according to WP_DEBUG setting
239
  *
 
240
  */
241
  private function log($message) {
242
  if (WP_DEBUG === true) {
@@ -247,6 +644,7 @@ class DataCacheEngine {
247
  }
248
  }
249
  }
 
250
  }
251
 
252
  ?>
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
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
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
  );
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) {
644
  }
645
  }
646
  }
647
+
648
  }
649
 
650
  ?>
module/data-crawler.php CHANGED
@@ -3,7 +3,7 @@
3
  data-crawler.php
4
 
5
  Description: This class is abstract class of a data crawler
6
- Version: 0.1.0
7
  Author: Daisuke Maruyama
8
  Author URI: http://marubon.info/
9
  License: GPL2 or later
@@ -37,18 +37,60 @@ abstract class DataCrawler{
37
  */
38
  protected $url = '';
39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  /**
41
  * Set URL for data crawling
 
 
42
  */
43
  public function set_url($url){
 
 
44
  $this->url = rawurlencode($url);
45
  }
46
 
47
  /**
48
  * Abstract method for data crawling
49
  *
 
50
  */
51
- abstract public function get_data();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  }
53
 
54
  ?>
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
37
  */
38
  protected $url = '';
39
 
40
+ /**
41
+ * Instance
42
+ */
43
+ private static $instance = array();
44
+
45
+ /**
46
+ * Get instance
47
+ *
48
+ * @since 0.1.1
49
+ */
50
+ public static function get_instance() {
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
+ }
58
+
59
+ return self::$instance[$class_name];
60
+ }
61
+
62
  /**
63
  * Set URL for data crawling
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
  /**
74
  * Abstract method for data crawling
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
+ }
94
  }
95
 
96
  ?>
module/sns-count-crawler.php CHANGED
@@ -3,7 +3,7 @@
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.1.0
7
  Author: Daisuke Maruyama
8
  Author URI: http://marubon.info/
9
  License: GPL2 or later
@@ -36,37 +36,69 @@ class SNSCountCrawler extends DataCrawler{
36
  * Timeout for cURL data retrieval
37
  */
38
  private $timeout = 10;
39
-
40
- function __construct($url='',$timeout=10) {
 
 
41
  $this->url=rawurlencode($url);
42
  $this->timeout=$timeout;
43
  }
44
-
45
- function set_timeout($timeout){
 
 
46
  $this->timeout = $timeout;
47
  }
48
-
49
  /**
50
  * Implementation of abstract method. this method gets each share count
51
  *
52
- */
 
 
53
  public function get_data(){
54
- $sns_counts = array();
55
 
 
 
56
  $sns_counts[SNSCountCache::REF_HATEBU] = $this->get_hatebu_count();
57
  $sns_counts[SNSCountCache::REF_TWITTER] = $this->get_twitter_count();
58
  $sns_counts[SNSCountCache::REF_FACEBOOK] = $this->get_facebook_count();
59
  $sns_counts[SNSCountCache::REF_GPLUS] = $this->get_gplus_count();
60
-
61
  return $sns_counts;
62
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
 
64
  /**
65
  * Get share count for Hatena Bookmark
66
  *
 
67
  */
68
- public function get_hatebu_count() {
69
- $query = 'http://api.b.st-hatena.com/entry.count?url=' . $this->url;
 
 
70
 
71
  $hatebu = $this->file_get_contents($query);
72
 
@@ -76,9 +108,12 @@ class SNSCountCrawler extends DataCrawler{
76
  /**
77
  * Get share count for Twitter
78
  *
 
79
  */
80
- public function get_twitter_count() {
81
- $query = 'http://urls.api.twitter.com/1/urls/count.json?url=' . $this->url;
 
 
82
 
83
  $json = $this->file_get_contents($query);
84
 
@@ -90,9 +125,12 @@ class SNSCountCrawler extends DataCrawler{
90
  /**
91
  * Get share count for Facebook
92
  *
 
93
  */
94
- public function get_facebook_count() {
95
- $query = 'http://graph.facebook.com/' . $this->url;
 
 
96
 
97
  $json = $this->file_get_contents($query);
98
 
@@ -104,14 +142,17 @@ class SNSCountCrawler extends DataCrawler{
104
  /**
105
  * Get share count for Google Plus
106
  *
 
107
  */
108
- public function get_gplus_count() {
 
 
109
  $curl = curl_init();
110
 
111
  curl_setopt($curl, CURLOPT_URL, "https://clients6.google.com/rpc");
112
  curl_setopt($curl, CURLOPT_POST, true);
113
  curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
114
- curl_setopt($curl, CURLOPT_POSTFIELDS, '[{"method":"pos.plusones.get","id":"p","params":{"nolog":true,"id":"' . rawurldecode($this->url) . '","source":"widget","userId":"@viewer","groupId":"@self"},"jsonrpc":"2.0","key":"p","apiVersion":"v1"}]');
115
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
116
  curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
117
 
@@ -131,8 +172,11 @@ class SNSCountCrawler extends DataCrawler{
131
  /**
132
  * Get content from given URL using cURL
133
  *
 
134
  */
135
  private function file_get_contents($url){
 
 
136
  $curl = curl_init();
137
 
138
  curl_setopt($curl, CURLOPT_URL, $url);
@@ -153,20 +197,6 @@ class SNSCountCrawler extends DataCrawler{
153
  return $curl_results;
154
  }
155
 
156
- /**
157
- * Output log message according to WP_DEBUG setting
158
- *
159
- */
160
- private function log($message) {
161
- if (WP_DEBUG === true) {
162
- if (is_array($message) || is_object($message)) {
163
- error_log(print_r($message, true));
164
- } else {
165
- error_log($message);
166
- }
167
- }
168
- }
169
-
170
  }
171
 
172
- ?>
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
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
 
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
 
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
 
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
 
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);
197
  return $curl_results;
198
  }
199
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
200
  }
201
 
202
+ ?>
readme.txt CHANGED
@@ -2,9 +2,9 @@
2
  Contributors: marubon
3
  Donate link:
4
  Tags: performance, SNS, social, cache
5
- Requires at least: 3.2
6
- Tested up to: 3.9.1
7
- Stable tag: 0.1.0
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
@@ -39,13 +39,19 @@ There are no questions.
39
 
40
  == Screenshots ==
41
  1. Cache status is described in setting page
42
- 2. Help page shows available functions to access the cache
 
43
 
44
  == Changelog ==
45
 
46
  = 0.1.0 =
47
  * Initial working version.
48
 
 
 
 
 
 
49
  == Upgrade Notice ==
50
  There is no upgrade notice.
51
 
2
  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
 
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
 
47
  = 0.1.0 =
48
  * Initial working version.
49
 
50
+ = 0.2.0 =
51
+ * Added: function to modify check interval of SNS share count and its number of target posts and pages at a time
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
 
screenshot-1.png CHANGED
Binary file
screenshot-2.png CHANGED
Binary file
screenshot-3.png ADDED
Binary file
sns-count-cache.php CHANGED
@@ -2,7 +2,7 @@
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.1.0
6
  Author: Daisuke Maruyama
7
  Author URI: http://marubon.info/
8
  License: GPL2 or later
@@ -29,6 +29,10 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
29
 
30
  */
31
 
 
 
 
 
32
  if (!class_exists('SNSCountCache')){
33
 
34
  class SNSCountCache {
@@ -36,8 +40,13 @@ class SNSCountCache {
36
  /**
37
  * Plugin version, used for cache-busting of style and script file references.
38
  */
39
- private $version = '0.1.0';
40
-
 
 
 
 
 
41
  /**
42
  * Instance of module class of schedule and cache processing
43
  */
@@ -47,12 +56,102 @@ class SNSCountCache {
47
  * Slug of the plugin screen
48
  */
49
  private $plugin_screen_hook_suffix = NULL;
 
 
 
 
 
50
 
51
  /**
52
  * Prefix of cache ID
53
  */
54
- const TRANSIENT_PREFIX = 'sns_count_cache_';
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
  /**
57
  * Slug of the plugin
58
  */
@@ -77,99 +176,174 @@ class SNSCountCache {
77
  * ID of share count (Hatena Bookmark)
78
  */
79
  const REF_HATEBU = 'hatebu';
80
-
 
 
 
 
 
81
  /**
82
  * Class constarctor
83
  * Hook onto all of the actions and filters needed by the plugin.
84
  */
85
- function __construct() {
86
-
87
- //load_plugin_textdomain(self::DOMAIN, false, basename(dirname( __FILE__ )) . '/languages');
88
-
89
- require_once (dirname(__FILE__) . '/module/data-cache-engine.php');
90
- require_once (dirname(__FILE__) . '/module/data-crawler.php');
91
- require_once (dirname(__FILE__) . '/module/sns-count-crawler.php');
92
-
93
- $crawler = new SNSCountCrawler();
94
 
95
- $options = array(
96
- 'check_interval' => 600,
97
- 'posts_per_check' => 20,
98
- 'transient_prefix' => self::TRANSIENT_PREFIX,
99
- 'cron_cache_prime' => 'scc_cntcache_prime',
100
- 'cron_cache_execute' => 'scc_cntcache_exec',
101
- 'event_schedule' => 'cache_event',
102
- 'event_description' => '[SCC] Share Count Cache Interval'
103
- );
104
-
105
- $this->cache_engine = new DataCacheEngine($crawler, $options);
106
 
107
  add_action('admin_menu', array($this, 'action_admin_menu'));
108
-
109
  add_action('admin_print_styles', array($this, 'register_admin_styles'));
110
  add_action('admin_enqueue_scripts', array($this, 'register_admin_scripts'));
111
-
112
- register_activation_hook( __FILE__, array($this, 'activate_plugin'));
113
- register_deactivation_hook(__FILE__, array($this, 'deactivate_plugin'));
114
-
115
  }
116
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117
  /**
118
  * Registers and enqueues admin-specific styles.
119
  *
 
120
  */
121
  public function register_admin_styles() {
 
 
122
  if (!isset($this->plugin_screen_hook_suffix)) {
123
  return;
124
  }
125
 
126
  $screen = get_current_screen();
 
127
  if ($screen->id == $this->plugin_screen_hook_suffix) {
128
  wp_enqueue_style(self::DOMAIN .'-admin-style-1' , plugins_url(ltrim('/css/sns-count-cache.css', '/'), __FILE__));
129
  wp_enqueue_style(self::DOMAIN .'-admin-style-2' , plugins_url(ltrim('/css/prettify.css', '/'), __FILE__));
130
  }
131
-
132
  }
133
 
134
  /**
135
  * Registers and enqueues admin-specific JavaScript.
136
  *
 
137
  */
138
  public function register_admin_scripts() {
139
-
 
140
  if (!isset( $this->plugin_screen_hook_suffix)) {
141
  return;
142
  }
143
 
144
  $screen = get_current_screen();
 
145
  if ($screen->id == $this->plugin_screen_hook_suffix) {
146
- wp_enqueue_script(self::DOMAIN . '-admin-script-1' ,plugins_url(ltrim('/js/jquery.sns-count-cache.js', '/') , __FILE__ ),array( 'jquery' ));
147
- wp_enqueue_script(self::DOMAIN . '-admin-script-2' ,plugins_url(ltrim('/js/prettify.js', '/') , __FILE__ ),array( 'jquery' ));
148
  }
149
-
150
  }
151
 
152
  /**
153
- * Activate base schedule cron
154
  *
 
155
  */
156
- function activate_plugin(){
157
- $this->cache_engine->register_base_schedule();
 
 
 
158
  }
159
 
160
  /**
161
- * Deactivate base schedule cron
162
  *
 
163
  */
164
  function deactivate_plugin(){
165
- $this->cache_engine->unregister_base_schedule();
 
 
166
  }
167
-
 
 
 
 
 
 
 
 
 
 
 
 
 
168
  /**
169
  * Adds options & management pages to the admin menu.
170
  *
171
  * Run using the 'admin_menu' action.
172
- */
 
 
173
  public function action_admin_menu() {
174
  $this->plugin_screen_hook_suffix = add_options_page('SNS Count Cache', 'SNS Count Cache', 8, 'sns_count_cache_options_page',array($this, 'option_page'));
175
  }
@@ -177,115 +351,251 @@ class SNSCountCache {
177
  /**
178
  * Option page implementation
179
  *
180
- */
 
181
  public function option_page(){
182
  include_once(dirname(__FILE__) . '/admin.php');
183
  }
184
-
185
- public static function init() {
186
-
187
- static $instance = null;
188
-
189
- if ( !$instance )
190
- $instance = new SNSCountCache;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
191
 
192
- return $instance;
 
 
 
193
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
194
  }
 
195
  }
196
 
197
- SNSCountCache::init();
198
 
199
  /**
200
  * Get share count from cache (Hatena Bookmark).
201
  *
202
- */
 
203
  function get_scc_hatebu($post_ID='') {
204
- $transient_id ='';
 
205
 
206
- if(!empty($post_ID)){
207
- $transient_id = SNSCountCache::TRANSIENT_PREFIX . $post_ID;
208
- }else{
209
- $transient_id = SNSCountCache::TRANSIENT_PREFIX . get_the_ID();
210
  }
211
-
212
- $sns_counts = get_transient($transient_id);
213
 
214
- return $sns_counts[SNSCountCache::REF_HATEBU];
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
215
  }
216
 
217
  /**
218
  * Get share count from cache (Twitter)
219
  *
220
- */
 
221
  function get_scc_twitter($post_ID='') {
222
- $transient_id ='';
 
223
 
224
- if(!empty($post_ID)){
225
- $transient_id = SNSCountCache::TRANSIENT_PREFIX . $post_ID;
226
- }else{
227
- $transient_id = SNSCountCache::TRANSIENT_PREFIX . get_the_ID();
228
  }
 
 
229
 
230
- $sns_counts = get_transient($transient_id);
 
 
 
231
 
232
- return $sns_counts[SNSCountCache::REF_TWITTER];
 
 
 
 
 
 
 
 
 
 
 
 
 
 
233
  }
234
 
235
  /**
236
  * Get share count from cache (Facebook)
237
  *
238
- */
 
239
  function get_scc_facebook($post_ID='') {
240
- $transient_id ='';
 
241
 
242
- if(!empty($post_ID)){
243
- $transient_id = SNSCountCache::TRANSIENT_PREFIX . $post_ID;
244
- }else{
245
- $transient_id = SNSCountCache::TRANSIENT_PREFIX . get_the_ID();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
246
  }
247
 
248
- $sns_counts = get_transient($transient_id);
249
-
250
- return $sns_counts[SNSCountCache::REF_FACEBOOK];
251
  }
252
 
253
  /**
254
  * Get share count from cache (Google Plus)
255
  *
256
- */
 
257
  function get_scc_gplus($post_ID='') {
258
- $transient_id ='';
 
259
 
260
- if(!empty($post_ID)){
261
- $transient_id = SNSCountCache::TRANSIENT_PREFIX . $post_ID;
262
- }else{
263
- $transient_id = SNSCountCache::TRANSIENT_PREFIX . get_the_ID();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
264
  }
265
 
266
- $sns_counts = get_transient($transient_id);
267
-
268
- return $sns_counts[SNSCountCache::REF_GPLUS];
269
  }
270
 
271
  /**
272
  * Get share count from cache
273
  *
274
- */
 
275
  function get_scc($post_ID='') {
276
- $transient_id ='';
 
277
 
278
- if(!empty($post_ID)){
279
- $transient_id = SNSCountCache::TRANSIENT_PREFIX . $post_ID;
280
- }else{
281
- $transient_id = SNSCountCache::TRANSIENT_PREFIX . get_the_ID();
282
  }
 
 
283
 
284
- $sns_counts = get_transient($transient_id);
285
-
286
- return $sns_counts;
287
- }
288
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
289
  }
290
 
291
  ?>
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
 
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 {
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
47
+ */
48
+ private $crawler = NULL;
49
+
50
  /**
51
  * Instance of module class of schedule and cache processing
52
  */
56
  * Slug of the plugin screen
57
  */
58
  private $plugin_screen_hook_suffix = NULL;
59
+
60
+ /**
61
+ * Option flag of dynamic cache processing
62
+ */
63
+ private $dynamic_cache = 0;
64
 
65
  /**
66
  * Prefix of cache ID
67
  */
68
+ const OPT_BASE_TRANSIENT_PREFIX = 'sns_count_cache_';
69
+
70
+ /**
71
+ * Cron name to schedule cache processing
72
+ */
73
+ const OPT_BASE_CACHE_PRIME_CRON = 'scc_basecache_prime';
74
+
75
+ /**
76
+ * Cron name to execute cache processing
77
+ */
78
+ const OPT_BASE_CACHE_EXECUTE_CRON = 'scc_basecache_exec';
79
+
80
+ /**
81
+ * Schedule name for cache processing
82
+ */
83
+ const OPT_BASE_EVENT_SCHEDULE = 'base_cache_event';
84
+
85
+ /**
86
+ * Schedule description for cache processing
87
+ */
88
+ const OPT_BASE_EVENT_DESCRIPTION = '[SCC] Share Count Cache Basic Interval';
89
+
90
+ /**
91
+ * Interval cheking and caching target data
92
+ */
93
+ const OPT_BASE_CHECK_INTERVAL = 600;
94
+
95
+ /**
96
+ * Number of posts to check at a time
97
+ */
98
+ const OPT_BASE_POSTS_PER_CHECK = 20;
99
+
100
+ /**
101
+ * Cron name to schedule cache processing
102
+ */
103
+ const OPT_RUSH_CACHE_PRIME_CRON = 'scc_rushcache_prime';
104
+
105
+ /**
106
+ * Cron name to execute cache processing
107
+ */
108
+ const OPT_RUSH_CACHE_EXECUTE_CRON = 'scc_rushcache_exec';
109
+
110
+ /**
111
+ * Schedule name for cache processing
112
+ */
113
+ const OPT_RUSH_EVENT_SCHEDULE = 'rush_cache_event';
114
+
115
+ /**
116
+ * Schedule description for cache processing
117
+ */
118
+ const OPT_RUSH_EVENT_DESCRIPTION = '[SCC] Share Count Cache Rush Interval';
119
+
120
+ /**
121
+ * Cron name to execute cache processing
122
+ */
123
+ const OPT_LAZY_CACHE_EXECUTE_CRON = 'scc_lazycache_exec';
124
+
125
+ /**
126
+ * Type of dynamic cache processing
127
+ */
128
+ const OPT_ACCESS_BASED_CACHE_NONE = 0;
129
+
130
+ /**
131
+ * Type of dynamic cache processing
132
+ */
133
+ const OPT_ACCESS_BASED_SYNC_CACHE = 1;
134
 
135
+ /**
136
+ * Type of dynamic cache processing
137
+ */
138
+ const OPT_ACCESS_BASED_ASYNC_CACHE = 2;
139
+
140
+ /**
141
+ * Option key for interval cheking and caching target data
142
+ */
143
+ const DB_CHECK_INTERVAL = 'scc_check_interval';
144
+
145
+ /**
146
+ * Option key for Number of posts to check at a time
147
+ */
148
+ const DB_POSTS_PER_CHECK = 'scc_posts_per_check';
149
+
150
+ /**
151
+ * Option key for dynamic cache processing
152
+ */
153
+ const DB_DYNAMIC_CACHE = 'scc_dynamic_cache';
154
+
155
  /**
156
  * Slug of the plugin
157
  */
176
  * ID of share count (Hatena Bookmark)
177
  */
178
  const REF_HATEBU = 'hatebu';
179
+
180
+ /**
181
+ * Instance
182
+ */
183
+ private static $instance = NULL;
184
+
185
  /**
186
  * Class constarctor
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
+ /**
206
+ * Get instance
207
+ *
208
+ * @since 0.1.1
209
+ */
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
+
217
+ return self::$instance;
218
+ }
219
+
220
+ /**
221
+ * Initialization
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,
246
+ 'base_cache_prime_cron' => self::OPT_BASE_CACHE_PRIME_CRON,
247
+ 'base_cache_execute_cron' => self::OPT_BASE_CACHE_EXECUTE_CRON,
248
+ 'base_event_schedule' => self::OPT_BASE_EVENT_SCHEDULE,
249
+ 'base_event_description' => self::OPT_BASE_EVENT_DESCRIPTION,
250
+ 'rush_cache_prime_cron' => self::OPT_RUSH_CACHE_PRIME_CRON,
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
+
262
  /**
263
  * Registers and enqueues admin-specific styles.
264
  *
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
  }
282
 
283
  /**
284
  * Registers and enqueues admin-specific JavaScript.
285
  *
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
  }
303
 
304
  /**
305
+ * Activate cache engine (base schedule cron)
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();
314
  }
315
 
316
  /**
317
+ * Deactivate cache engine (base schedule cron)
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
  }
326
+
327
+ /**
328
+ * Reactivate cache engine
329
+ *
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();
337
+ $this->cache_engine->register_base_schedule();
338
+ }
339
+
340
  /**
341
  * Adds options & management pages to the admin menu.
342
  *
343
  * Run using the 'admin_menu' action.
344
+ *
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
  }
351
  /**
352
  * Option page implementation
353
  *
354
+ * @since 0.1.0
355
+ */
356
  public function option_page(){
357
  include_once(dirname(__FILE__) . '/admin.php');
358
  }
359
+
360
+ /**
361
+ * Output log message according to WP_DEBUG setting
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
+ }
374
+
375
+ /**
376
+ * Return type of dynamic cache processing
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
+ }
387
 
388
+
389
+ /**
390
+ * Get and cache data for a given post ID
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
+ /**
401
+ * Reserve cache processing for a given post ID
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
  /**
453
  * Get share count from cache (Twitter)
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
 
489
  /**
490
  * Get share count from cache (Facebook)
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
 
526
  /**
527
  * Get share count from cache (Google Plus)
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
  }
600
 
601
  ?>
uninstall.php ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ //if uninstall not called from WordPress exit
3
+ if(!defined('WP_UNINSTALL_PLUGIN')) exit();
4
+
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 {
17
+ global $wpdb;
18
+ $blog_ids = $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs" );
19
+ $original_blog_id = get_current_blog_id();
20
+
21
+ foreach($blog_ids as $blog_id){
22
+ switch_to_blog($blog_id);
23
+
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
+ }
30
+ ?>