SNS Count Cache - Version 0.4.0

Version Description

  • Added: admin page was totally improved.
  • Added: function to sort contents based on share count was added to admin page of share count.
  • Added: content of custom post type was added as share count cache target.
  • Added: number of Feedly follower was included as one of cache targets.
  • Added: function to export share count data was added.
  • Added: cache logic was improved.

=

Download this release

Release Info

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

Code changes from version 0.3.0 to 0.4.0

css/sns-count-cache.css CHANGED
@@ -1,3 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  .sns-cnt-cache .view-table {
2
  border: 1px solid #000;
3
  margin: 20px 0;
@@ -34,11 +47,24 @@
34
  border: 1px dotted #dfdfdf;
35
  }
36
 
37
- .sns-cnt-cache .cached {
38
  color: #468847;
 
 
 
 
 
 
39
  /* background-color: #dff0d8 */;
40
  }
41
 
 
 
 
 
 
 
 
42
  .sns-cnt-cache .not-cached {
43
  color: #ccc;
44
  }
@@ -47,67 +73,10 @@
47
  text-align: right;
48
  }
49
 
50
- .sns-cnt-cache .tab {
51
- overflow: hidden;
52
- padding: 0;
53
- margin: 30px 0 0 0;
54
- }
55
-
56
- .sns-cnt-cache .tab li {
57
- float: left;
58
- margin: 0;
59
- padding: 0 20px;
60
- height: 31px;
61
- line-height: 31px;
62
- border: 1px solid #DFDFDF;
63
- margin-bottom: -1px;
64
- overflow: hidden;
65
- position: relative;
66
- background: #F5F5F5;
67
- background-image: -webkit-gradient(linear,left top,left bottom,from(#fff),to(#F5F5F5));
68
- background-image: -webkit-linear-gradient(center top,#fff,#F5F5F5);
69
- background-image: -moz-linear-gradient(center top,#fff,#F5F5F5);
70
- background-image: -ms-linear-gradient(center top,#fff,#F5F5F5);
71
- background-image: -o-linear-gradient(center top,#fff,#F5F5F5);
72
- background-image: linear-gradient(center top,#fff,#F5F5F5);
73
- }
74
-
75
- .sns-cnt-cache .tab li.select {
76
- background: #fff;
77
- text-decoration: none;
78
- color: #000;
79
- display: block;
80
- padding: 0 20px;
81
- outline: none;
82
- }
83
-
84
- .sns-cnt-cache ul.content {
85
- padding: 10px;
86
- background: #fff;
87
- border: 1px solid #DFDFDF;
88
- border-top: none;
89
- overflow: hidden;
90
- background: #fff;
91
- margin: 0;
92
- }
93
-
94
- .sns-cnt-cache .content li {
95
- padding: 0;
96
- margin: 0;
97
- }
98
-
99
- .sns-cnt-cache .hide {
100
- display: none;
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;
@@ -122,4 +91,53 @@
122
 
123
  .sns-cnt-cache a:hover {
124
  color: #0074a2;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
125
  }
1
+ .wrap h2 a {
2
+ color: #222;
3
+ text-decoration: none;
4
+ }
5
+
6
+ .sns-cnt-cache h3.hndle {
7
+ font-size: 16px !important;
8
+ }
9
+
10
+ .sns-cnt-cache .postbox .inside {
11
+ padding: 0 10px;
12
+ }
13
+
14
  .sns-cnt-cache .view-table {
15
  border: 1px solid #000;
16
  margin: 20px 0;
47
  border: 1px dotted #dfdfdf;
48
  }
49
 
50
+ .sns-cnt-cache .full-cache {
51
  color: #468847;
52
+ /* color: #5cb85c; */
53
+ /* background-color: #dff0d8 */;
54
+ }
55
+
56
+ .sns-cnt-cache .partial-cache {
57
+ color: #f0ad4e;
58
  /* background-color: #dff0d8 */;
59
  }
60
 
61
+ .sns-cnt-cache .no-cache {
62
+ /* color: #d9534f; */
63
+ /* background-color: #dff0d8 */;
64
+ color: #ccc;
65
+ }
66
+
67
+
68
  .sns-cnt-cache .not-cached {
69
  color: #ccc;
70
  }
73
  text-align: right;
74
  }
75
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
  .sns-cnt-cache .submit-button {
77
  margin: 20px 0;
78
  }
79
 
 
 
 
 
80
  .sns-cnt-cache a {
81
  text-decoration: none;
82
  color: #444;
91
 
92
  .sns-cnt-cache a:hover {
93
  color: #0074a2;
94
+ }
95
+
96
+ .sns-cnt-cache .view-table th a {
97
+ color: #fff;
98
+ }
99
+
100
+ .sns-cnt-cache h4 {
101
+ font-size: 15px;
102
+ }
103
+
104
+ .sns-cnt-cache a.sort-exec-key {
105
+ text-decoration: underline;
106
+ }
107
+
108
+ .pagination span, .pagination a {
109
+ display: inline-block;
110
+ padding: 2px 10px;
111
+ }
112
+
113
+ .pagination a {
114
+ background: #555;
115
+ color: #fff;
116
+ }
117
+
118
+ .pagination a:hover {
119
+ background: #3279bb;
120
+ color: #fff;
121
+ }
122
+
123
+ .pagination span.page-num {
124
+ margin-right: 10px;
125
+ padding: 0;
126
+ }
127
+
128
+ .pagination span.dots {
129
+ padding: 0;
130
+ color: gainsboro;
131
+ }
132
+
133
+ .pagination span.current {
134
+ background-color: #ccc;
135
+ color: #fff;
136
+ }
137
+
138
+ .sns-cnt-cache a.button {
139
+ font-size: 12px;
140
+ padding: 1px 5px;
141
+ height: 20px;
142
+ line-height: 15px;
143
  }
data/.htaccess ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <Files "*">
2
+ <IfModule mod_access.c>
3
+ Deny from all
4
+ </IfModule>
5
+ <IfModule !mod_access_compat>
6
+ <IfModule mod_authz_host.c>
7
+ Deny from all
8
+ </IfModule>
9
+ </IfModule>
10
+ <IfModule mod_access_compat>
11
+ Deny from all
12
+ </IfModule>
13
+ </Files>
data/index.php ADDED
@@ -0,0 +1,3 @@
 
 
 
1
+ <?php
2
+ header( $_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found' );
3
+ header( 'Status: 404 Not Found' );
includes/admin-cache-status.php ADDED
@@ -0,0 +1,238 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /*
3
+ admin-cache-status.php
4
+
5
+ Description: Option page implementation
6
+ Version: 0.4.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
+ $posts_per_page = 50;
34
+
35
+ $paged = 1;
36
+
37
+ if ( isset( $_GET['paged'] ) && is_numeric( $_GET['paged'] ) && $_GET['paged'] > 0 ) {
38
+ $paged = $_GET['paged'];
39
+ } else {
40
+ $paged = 1;
41
+ }
42
+
43
+ if ( isset( $_GET['_wpnonce'] ) && $_GET['_wpnonce'] ) {
44
+ $nonce = $_GET['_wpnonce'];
45
+
46
+ if ( wp_verify_nonce( $nonce, __FILE__ ) ) {
47
+ if( isset( $_GET["action"] ) && $_GET["action"] === 'cache' ) {
48
+
49
+ if( isset( $_GET["post_id"] ) ) {
50
+ $post_id = $_GET["post_id"];
51
+
52
+ $this->retrieve_share_cache( $post_id, true );
53
+ }
54
+
55
+ }
56
+ }
57
+ }
58
+
59
+ /*
60
+ $query_args = array(
61
+ 'post_type' => $this->share_base_cache_post_types,
62
+ 'post_status' => 'publish',
63
+ 'nopaging' => true,
64
+ 'update_post_term_cache' => false,
65
+ 'update_post_meta_cache' => false
66
+ );
67
+ */
68
+
69
+ $query_args = array(
70
+ 'post_type' => $this->share_base_cache_post_types,
71
+ 'post_status' => 'publish',
72
+ 'posts_per_page' => $posts_per_page,
73
+ 'paged' => $paged,
74
+ 'update_post_term_cache' => false
75
+ );
76
+
77
+ $posts_query = new WP_Query( $query_args );
78
+
79
+ ?>
80
+ <div class="wrap">
81
+ <h2><a href="admin.php?page=scc-cache-status"><?php _e( 'SNS Count Cache', self::DOMAIN ) ?></a></h2>
82
+ <div class="sns-cnt-cache">
83
+ <h3 class="nav-tab-wrapper">
84
+ <a class="nav-tab" href="admin.php?page=scc-dashboard">Dashboard</a>
85
+ <a class="nav-tab nav-tab-active" href="admin.php?page=scc-cache-status">Cache Status</a>
86
+ <a class="nav-tab" href="admin.php?page=scc-share-count">Share Count</a>
87
+ <a class="nav-tab" href="admin.php?page=scc-setting">Setting</a>
88
+ <a class="nav-tab" href="admin.php?page=scc-help">Help</a>
89
+ </h3>
90
+
91
+ <div class="metabox-holder">
92
+ <div id="share-site-summary" class="postbox">
93
+ <div class="handlediv" title="Click to toggle"><br></div>
94
+ <h3 class="hndle"><span><?php _e( 'Cache Status', self::DOMAIN ) ?></span></h3>
95
+ <div class="inside">
96
+
97
+ <table class="view-table">
98
+ <thead>
99
+ <tr>
100
+ <th>No.</th>
101
+ <th><?php _e( 'Target Content', self::DOMAIN ) ?></th>
102
+ <th><?php _e( 'Primary Cache', self::DOMAIN ) ?></th>
103
+ <th><?php _e( 'Secondary Cache', self::DOMAIN ) ?></th>
104
+ <th><?php _e( 'Crawl Date', self::DOMAIN ) ?></th>
105
+ <th></th>
106
+ </tr>
107
+ </thead>
108
+ <tbody>
109
+
110
+ <?php
111
+
112
+ $count = ($paged - 1) * $posts_per_page + 1;
113
+
114
+ $share_base_cache_target = $this->share_base_cache_target ;
115
+ unset( $share_base_cache_target[self::REF_CRAWL_DATE] );
116
+
117
+ if ( $posts_query->have_posts() ) {
118
+ while ( $posts_query->have_posts() ) {
119
+ $posts_query->the_post();
120
+ ?>
121
+ <tr>
122
+ <td><?php echo $count; ?></td>
123
+ <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>
124
+ <?php
125
+ $transient_id = self::OPT_SHARE_BASE_TRANSIENT_PREFIX . get_the_ID();
126
+
127
+ if ( false === ( $sns_counts = get_transient( $transient_id ) ) ) {
128
+ echo '<td class="no-cache">';
129
+ _e( 'no cache', self::DOMAIN );
130
+ echo '</td>';
131
+ } else {
132
+ $full_cache_flag = true;
133
+ $partial_cache_flag = false;
134
+
135
+ foreach ( $share_base_cache_target as $key => $value ) {
136
+ if ( $value ) {
137
+ if ( isset( $sns_counts[$key] ) && $sns_counts[$key] >= 0 ) {
138
+ $partial_cache_flag = true;
139
+ } else {
140
+ $full_cache_flag = false;
141
+ }
142
+ }
143
+ }
144
+
145
+ if ( $partial_cache_flag && $full_cache_flag ) {
146
+ echo '<td class="full-cache">';
147
+ _e( 'full cache', self::DOMAIN );
148
+ echo '</td>';
149
+ } else if ( $partial_cache_flag && ! $full_cache_flag ) {
150
+ echo '<td class="partial-cache">';
151
+ _e( 'partial cache', self::DOMAIN );
152
+ echo '</td>';
153
+ } else {
154
+ echo '<td class="no-cache">';
155
+ _e( 'no cache', self::DOMAIN );
156
+ echo '</td>';
157
+ }
158
+ }
159
+
160
+ $full_cache_flag = true;
161
+ $partial_cache_flag = false;
162
+
163
+ foreach ( $share_base_cache_target as $key => $value ) {
164
+ if ( $value ) {
165
+
166
+ $meta_key = self::OPT_SHARE_2ND_META_KEY_PREFIX . strtolower( $key );
167
+ $sns_count = get_post_meta( get_the_ID(), $meta_key, true );
168
+
169
+ if ( isset( $sns_count ) && $sns_count >= 0 ) {
170
+ $partial_cache_flag = true;
171
+ } else {
172
+ $full_cache_flag = false;
173
+ }
174
+
175
+ }
176
+ }
177
+
178
+ if ( $partial_cache_flag && $full_cache_flag ) {
179
+ echo '<td class="full-cache">';
180
+ _e( 'full cache', self::DOMAIN );
181
+ echo '</td>';
182
+ } else if ( $partial_cache_flag && ! $full_cache_flag ) {
183
+ echo '<td class="partial-cache">';
184
+ _e( 'partial cache', self::DOMAIN );
185
+ echo '</td>';
186
+ } else {
187
+ echo '<td class="no-cache">';
188
+ _e( 'no cache', self::DOMAIN );
189
+ echo '</td>';
190
+ }
191
+
192
+ if ( isset( $sns_counts[self::REF_CRAWL_DATE] ) ) {
193
+ echo '<td class="full-cache">';
194
+ echo $sns_counts[self::REF_CRAWL_DATE];
195
+ echo '</td>';
196
+ } else {
197
+
198
+ $meta_key = self::OPT_SHARE_2ND_META_KEY_PREFIX . strtolower( self::REF_CRAWL_DATE );
199
+ $crawl_date = get_post_meta( get_the_ID(), $meta_key, true );
200
+
201
+ if ( isset( $crawl_date ) && $crawl_date != -1 ) {
202
+ echo '<td class="full-cache">';
203
+ echo $crawl_date;
204
+ echo '</td>';
205
+ } else {
206
+ echo '<td class="no-cache">';
207
+ _e( 'no data', self::DOMAIN );
208
+ echo '</td>';
209
+ }
210
+
211
+ }
212
+
213
+ $nonce = wp_create_nonce( __FILE__ );
214
+ $cache_url = esc_url( 'admin.php?page=scc-cache-status&action=cache&post_id=' . get_the_ID() . '&_wpnonce=' . $nonce . '&paged=' . $paged );
215
+
216
+ ?>
217
+ <td><a class="button" href="<?php echo $cache_url ?>">Cache</a></td>
218
+ </tr>
219
+
220
+ <?php
221
+ $count++;
222
+
223
+ }
224
+ }
225
+ ?>
226
+ </tbody>
227
+ </table>
228
+ <?php
229
+ $this->pagination( $posts_query->max_num_pages, '', $paged, false );
230
+
231
+ wp_reset_postdata();
232
+ ?>
233
+ </div>
234
+ </div>
235
+ </div>
236
+
237
+ </div>
238
+ </div>
includes/admin-dashboard.php ADDED
@@ -0,0 +1,471 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /*
3
+ admin-dashboard.php
4
+
5
+ Description: Option page implementation
6
+ Version: 0.4.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
+ $share_base_custom_post_types = get_option( self::DB_SHARE_CUSTOM_POST_TYPES );
34
+
35
+ if ( ! $share_base_custom_post_types ) {
36
+ $share_base_custom_post_types = array();
37
+ }
38
+
39
+ $share_base_check_interval = get_option( self::DB_SHARE_CHECK_INTERVAL );
40
+ $share_base_check_interval = ! empty( $share_base_check_interval ) ? intval( $share_base_check_interval ) : self::OPT_SHARE_BASE_CHECK_INTERVAL;
41
+
42
+ $share_base_posts_per_check = get_option( self::DB_SHARE_POSTS_PER_CHECK );
43
+ $share_base_posts_per_check = ! empty( $share_base_posts_per_check ) ? intval( $share_base_posts_per_check ) : self::OPT_SHARE_BASE_POSTS_PER_CHECK;
44
+
45
+ $dynamic_cache = get_option( self::DB_COMMON_DYNAMIC_CACHE );
46
+ $dynamic_cache = isset( $dynamic_cache ) ? intval( $dynamic_cache ) : self::OPT_ACCESS_BASED_CACHE_NONE;
47
+
48
+ $share_rush_new_content_term = get_option( self::DB_SHARE_NEW_CONTENT_TERM );
49
+ $share_rush_new_content_term = ! empty( $share_rush_new_content_term ) ? intval( $share_rush_new_content_term ) : self::OPT_SHARE_RUSH_NEW_CONTENT_TERM;
50
+
51
+ $share_base_cache_target = get_option( self::DB_SHARE_CACHE_TARGET );
52
+
53
+ $follow_base_cache_target = get_option( self::DB_FOLLOW_CACHE_TARGET );
54
+
55
+ $follow_base_check_interval = get_option( self::DB_FOLLOW_CHECK_INTERVAL );
56
+ $follow_base_check_interval = ! empty( $follow_base_check_interval ) ? intval( $follow_base_check_interval ) : self::OPT_FOLLOW_BASE_CHECK_INTERVAL;
57
+
58
+ $data_export = get_option( self::DB_COMMON_DATA_EXPORT );
59
+ $data_export = isset( $data_export ) ? intval( $data_export ) : self::OPT_COMMON_DATA_EXPORT_MANUAL;
60
+
61
+ $data_export_interval = get_option( self::DB_COMMON_DATA_EXPORT_INTERVAL );
62
+ $data_export_interval = ! empty( $data_export_interval ) ? intval( $data_export_interval ) : self::OPT_COMMON_DATA_EXPORT_INTERVAL;
63
+
64
+ if ( ! $share_base_cache_target ) {
65
+ $share_base_cache_target[self::REF_SHARE_TWITTER] = true;
66
+ $share_base_cache_target[self::REF_SHARE_FACEBOOK] = true;
67
+ $share_base_cache_target[self::REF_SHARE_GPLUS] = true;
68
+ if ( extension_loaded( 'xml' ) && extension_loaded( 'xmlreader' ) && extension_loaded( 'xmlwriter' )) {
69
+ $share_base_cache_target[self::REF_SHARE_POCKET] = true;
70
+ }
71
+ $share_base_cache_target[self::REF_SHARE_HATEBU] = true;
72
+ }
73
+
74
+ $share_base_cache_target[self::REF_SHARE_TOTAL] = true;
75
+
76
+ if ( ! $follow_base_cache_target ) {
77
+ $follow_base_cache_target[self::REF_FOLLOW_FEEDLY] = true;
78
+ }
79
+
80
+ $query_args = array(
81
+ 'post_type' => $this->share_base_cache_post_types,
82
+ 'post_status' => 'publish',
83
+ 'nopaging' => true,
84
+ 'update_post_term_cache' => false,
85
+ 'update_post_meta_cache' => false
86
+ );
87
+
88
+ $site_query = new WP_Query( $query_args );
89
+
90
+ ?>
91
+ <div class="wrap">
92
+ <h2><a href="admin.php?page=scc-dashboard"><?php _e( 'SNS Count Cache', self::DOMAIN ) ?></a></h2>
93
+ <div class="sns-cnt-cache">
94
+ <h3 class="nav-tab-wrapper">
95
+ <a class="nav-tab nav-tab-active" href="admin.php?page=scc-dashboard">Dashboard</a>
96
+ <a class="nav-tab" href="admin.php?page=scc-cache-status">Cache Status</a>
97
+ <a class="nav-tab" href="admin.php?page=scc-share-count">Share Count</a>
98
+ <a class="nav-tab" href="admin.php?page=scc-setting">Setting</a>
99
+ <a class="nav-tab" href="admin.php?page=scc-help">Help</a>
100
+ </h3>
101
+ <div class="metabox-holder">
102
+ <div id="current-parameter" class="postbox">
103
+ <div class="handlediv" title="Click to toggle"><br></div>
104
+ <h3 class="hndle"><span><?php _e( 'Current Setting', self::DOMAIN ) ?></span></h3>
105
+ <div class="inside">
106
+ <p><?php _e( 'The following describes registered parameters.', self::DOMAIN ) ?></p>
107
+ <table class="view-table">
108
+ <thead>
109
+ <tr>
110
+ <th><?php _e( 'Function', self::DOMAIN ) ?></th>
111
+ <th><?php _e( 'Parameter', self::DOMAIN ) ?></th>
112
+ <th><?php _e( 'Value', self::DOMAIN ) ?></th>
113
+ </tr>
114
+ </thead>
115
+ <tbody>
116
+ <tr>
117
+ <td><?php _e( 'Share Base Cache', self::DOMAIN) ?></td><td><?php _e('Target SNS', self::DOMAIN ) ?></td>
118
+ <td>
119
+ <?php
120
+ $target_sns = array();
121
+ if ( isset( $share_base_cache_target[self::REF_SHARE_TWITTER] ) && $share_base_cache_target[self::REF_SHARE_TWITTER] ) {
122
+ $target_sns[] = 'Twitter';
123
+ }
124
+ if ( isset( $share_base_cache_target[self::REF_SHARE_FACEBOOK] ) && $share_base_cache_target[self::REF_SHARE_FACEBOOK] ) {
125
+ $target_sns[] = 'Facebook';
126
+ }
127
+ if ( isset( $share_base_cache_target[self::REF_SHARE_GPLUS] ) && $share_base_cache_target[self::REF_SHARE_GPLUS] ) {
128
+ $target_sns[] = 'Google+';
129
+ }
130
+ if ( isset( $share_base_cache_target[self::REF_SHARE_POCKET] ) && $share_base_cache_target[self::REF_SHARE_POCKET] ) {
131
+ $target_sns[] = 'Pocket';
132
+ }
133
+ if ( isset( $share_base_cache_target[self::REF_SHARE_HATEBU] ) && $share_base_cache_target[self::REF_SHARE_HATEBU] ) {
134
+ $target_sns[] = 'Hatena Bookmark';
135
+ }
136
+ echo implode( ", ", $target_sns );
137
+ ?>
138
+ </td>
139
+ </tr>
140
+ <tr>
141
+ <td><?php _e( 'Share Base Cache', self::DOMAIN ) ?></td>
142
+ <td><?php _e( 'Additional custom post types', self::DOMAIN ) ?></td>
143
+ <td><?php
144
+ if ( ! empty( $share_base_custom_post_types ) && $share_base_custom_post_types ) {
145
+ echo implode( ',', $share_base_custom_post_types );
146
+ } else {
147
+ _e( 'N/A', self::DOMAIN );
148
+ }
149
+ ?>
150
+ </td>
151
+ </tr>
152
+ <tr>
153
+ <td><?php _e( 'Share Base Cache', self::DOMAIN ) ?></td>
154
+ <td><?php _e( 'Interval cheking and caching share count', self::DOMAIN ) ?></td>
155
+ <td><?php echo $share_base_check_interval . ' seconds'; ?></td>
156
+ </tr>
157
+ <tr>
158
+ <td><?php _e( 'Share Base Cache', self::DOMAIN ) ?></td>
159
+ <td><?php _e( 'Number of posts to check at a time', self::DOMAIN ) ?></td>
160
+ <td><?php echo $share_base_posts_per_check . ' posts'; ?></td>
161
+ </tr>
162
+ <tr>
163
+ <td><?php _e( 'Share Rush Cache', self::DOMAIN ) ?></td>
164
+ <td><?php _e( 'Term considering posted content as new content', self::DOMAIN ) ?></td>
165
+ <td>
166
+ <?php
167
+ if ( $share_rush_new_content_term == 1 ) {
168
+ echo $share_rush_new_content_term . ' day';
169
+ } else if ( $share_rush_new_content_term > 1 ) {
170
+ echo $share_rush_new_content_term . ' days';
171
+ }
172
+ ?>
173
+ </td>
174
+ </tr>
175
+ <tr>
176
+ <td><?php _e( 'Follow Base Cache', self::DOMAIN) ?></td><td><?php _e('Target SNS', self::DOMAIN ) ?></td>
177
+ <td>
178
+ <?php
179
+ $target_sns = array();
180
+ if ( isset( $follow_base_cache_target[self::REF_FOLLOW_FEEDLY] ) && $follow_base_cache_target[self::REF_FOLLOW_FEEDLY] ) {
181
+ $target_sns[] = 'Feedly';
182
+ }
183
+ echo implode( ", ", $target_sns );
184
+ ?>
185
+ </td>
186
+ </tr>
187
+ <tr>
188
+ <td><?php _e( 'Follow Base Cache', self::DOMAIN ) ?></td>
189
+ <td><?php _e( 'Interval cheking and caching follower count', self::DOMAIN ) ?></td>
190
+ <td><?php echo $follow_base_check_interval . ' seconds'; ?></td>
191
+ </tr>
192
+ <tr>
193
+ <td><?php _e( 'Dynamic Cache', self::DOMAIN) ?></td><td><?php _e( 'Dynamic caching based on user access', self::DOMAIN ) ?></td><td>
194
+ <?php
195
+ switch ( $dynamic_cache ) {
196
+ case self::OPT_ACCESS_BASED_CACHE_NONE:
197
+ _e( 'disabled', self::DOMAIN );
198
+ break;
199
+ case self::OPT_ACCESS_BASED_SYNC_CACHE:
200
+ _e( 'enabled (Synchronous Cache)', self::DOMAIN );
201
+ break;
202
+ case self::OPT_ACCESS_BASED_ASYNC_CACHE:
203
+ _e( 'enabled (Asynchronous Cache)', self::DOMAIN );
204
+ case self::OPT_ACCESS_BASED_2ND_CACHE:
205
+ _e( 'enabled (Asynchronous 2nd Cache)', self::DOMAIN );
206
+ break;
207
+ }
208
+ ?>
209
+ </td>
210
+ </tr>
211
+ <tr>
212
+ <td><?php _e( 'Data Export', self::DOMAIN) ?></td><td><?php _e( 'Method of data export', self::DOMAIN ) ?></td><td>
213
+ <?php
214
+ switch ( $data_export ) {
215
+ case self::OPT_COMMON_DATA_EXPORT_MANUAL:
216
+ _e( 'Manual', self::DOMAIN );
217
+ break;
218
+ case self::OPT_COMMON_DATA_EXPORT_AUTO:
219
+ _e( 'Auto', self::DOMAIN );
220
+ break;
221
+ }
222
+ ?>
223
+ </td>
224
+ </tr>
225
+ <?php
226
+ if ( $data_export == self::OPT_COMMON_DATA_EXPORT_AUTO ) {
227
+ ?>
228
+ <tr>
229
+ <td><?php _e( 'Data Export', self::DOMAIN ) ?></td>
230
+ <td><?php _e( 'Interval exporting share count to a csv file', self::DOMAIN ) ?></td>
231
+ <td><?php echo $data_export_interval / 3600 . ' hours'; ?></td>
232
+ </tr>
233
+ <?php
234
+ }
235
+ ?>
236
+ </tbody>
237
+ </table>
238
+ </div>
239
+ </div>
240
+ </div>
241
+
242
+ <div class="metabox-holder">
243
+ <div id="share-site-summary" class="postbox">
244
+ <div class="handlediv" title="Click to toggle"><br></div>
245
+ <h3 class="hndle"><span><?php _e( 'Cache Status', self::DOMAIN ) ?></span></h3>
246
+ <div class="inside">
247
+
248
+ <table class="view-table">
249
+ <thead>
250
+ <tr>
251
+ <th><?php _e( 'Cache Type', self::DOMAIN ) ?></th>
252
+ <th><?php _e( 'Status Overview', self::DOMAIN ) ?></th>
253
+ <th><?php _e( 'Total Content', self::DOMAIN ) ?></th>
254
+ <th><?php _e( 'State - Full Cache', self::DOMAIN ) ?></th>
255
+ <th><?php _e( 'State - Partial Cache', self::DOMAIN ) ?></th>
256
+ <th><?php _e( 'State - No Cache', self::DOMAIN ) ?></th>
257
+ </tr>
258
+ </thead>
259
+ <tbody>
260
+
261
+ <?php
262
+
263
+ $share_base_cache_target = $this->share_base_cache_target ;
264
+ unset( $share_base_cache_target[self::REF_CRAWL_DATE] );
265
+
266
+ $count = 1;
267
+
268
+ $posts_count = 0;
269
+ $primary_full_cache_count = 0;
270
+ $primary_partial_cache_count = 0;
271
+ $primary_no_cache_count = 0;
272
+
273
+ $secondary_full_cache_count = 0;
274
+ $secondary_partial_cache_count = 0;
275
+ $secondary_no_cache_count = 0;
276
+
277
+ if ( $site_query->have_posts() ) {
278
+ while ( $site_query->have_posts() ) {
279
+ $site_query->the_post();
280
+
281
+ $posts_count++;
282
+
283
+ $transient_id = self::OPT_SHARE_BASE_TRANSIENT_PREFIX . get_the_ID();
284
+
285
+ if ( false === ( $sns_counts = get_transient( $transient_id ) ) ) {
286
+ $primary_no_cache_count++;
287
+ } else {
288
+ $full_cache_flag = true;
289
+ $partial_cache_flag = false;
290
+
291
+ foreach ( $share_base_cache_target as $key => $value ) {
292
+ if ( $value ) {
293
+ if ( isset( $sns_counts[$key] ) && $sns_counts[$key] >= 0 ) {
294
+ $partial_cache_flag = true;
295
+ } else {
296
+ $full_cache_flag = false;
297
+ }
298
+ }
299
+ }
300
+
301
+ if ( $partial_cache_flag && $full_cache_flag ) {
302
+ $primary_full_cache_count++;
303
+ } else if ( $partial_cache_flag && ! $full_cache_flag ) {
304
+ $primary_partial_cache_count++;
305
+ } else {
306
+ $primary_no_cache_count++;
307
+ }
308
+ }
309
+
310
+ $full_cache_flag = true;
311
+ $partial_cache_flag = false;
312
+ foreach ( $share_base_cache_target as $key => $value ) {
313
+ if ( $value ) {
314
+
315
+ $meta_key = self::OPT_SHARE_2ND_META_KEY_PREFIX . strtolower( $key );
316
+ $sns_count = get_post_meta( get_the_ID(), $meta_key, true );
317
+
318
+ if ( isset( $sns_count ) && $sns_count >= 0 ) {
319
+ $partial_cache_flag = true;
320
+ } else {
321
+ $full_cache_flag = false;
322
+ }
323
+
324
+ }
325
+ }
326
+ if ( $partial_cache_flag && $full_cache_flag ) {
327
+ $secondary_full_cache_count++;
328
+ } else if ( $partial_cache_flag && ! $full_cache_flag ) {
329
+ $secondary_partial_cache_count++;
330
+ } else {
331
+ $secondary_no_cache_count++;
332
+ }
333
+ }
334
+ wp_reset_postdata();
335
+ }
336
+
337
+ ?>
338
+
339
+ <tr>
340
+ <td><?php _e( 'Primary Cache', self::DOMAIN ); ?></td>
341
+ <td>
342
+ <?php
343
+ if ( $primary_full_cache_count == $posts_count ) {
344
+ _e( 'Completed', self::DOMAIN );
345
+ } else if ( ( $primary_full_cache_count + $primary_partial_cache_count ) == $posts_count ) {
346
+ _e( 'Partially Completed', self::DOMAIN );
347
+ } else {
348
+ _e( 'Ongoing', self::DOMAIN );
349
+ }
350
+ ?>
351
+ </td>
352
+ <td class="share-count"><?php echo $posts_count; ?></td>
353
+ <td class="share-count full-cache"><?php echo $primary_full_cache_count ; ?></td>
354
+ <td class="share-count partial-cache"><?php echo $primary_partial_cache_count ; ?></td>
355
+ <td class="share-count no-cache"><?php echo $primary_no_cache_count; ?></td>
356
+ </tr>
357
+ <tr>
358
+ <td><?php _e( 'Secondary Cache', self::DOMAIN ); ?></td>
359
+ <td>
360
+ <?php
361
+ if ( $secondary_full_cache_count == $posts_count ) {
362
+ _e( 'Completed', self::DOMAIN );
363
+ } else if ( ( $secondary_full_cache_count + $secondary_partial_cache_count ) == $posts_count ) {
364
+ _e( 'Partially Completed', self::DOMAIN );
365
+ } else {
366
+ _e( 'Ongoing', self::DOMAIN );
367
+ }
368
+ ?>
369
+ </td>
370
+ <td class="share-count"><?php echo $posts_count; ?></td>
371
+ <td class="share-count full-cache"><?php echo $secondary_full_cache_count; ?></td>
372
+ <td class="share-count partial-cache"><?php echo $secondary_partial_cache_count; ?></td>
373
+ <td class="share-count no-cache"><?php echo $secondary_no_cache_count; ?></td>
374
+ </tr>
375
+
376
+ </tbody>
377
+ </table>
378
+ </div>
379
+ </div>
380
+ </div>
381
+
382
+ <div class="metabox-holder">
383
+ <div id="share-site-summary" class="postbox">
384
+ <div class="handlediv" title="Click to toggle"><br></div>
385
+ <h3 class="hndle"><span><?php _e( 'Share Count', self::DOMAIN ) ?></span></h3>
386
+ <div class="inside">
387
+
388
+ <table class="view-table">
389
+ <thead>
390
+ <tr>
391
+ <?php
392
+
393
+ foreach ( $share_base_cache_target as $key => $value ){
394
+ if ( $value ) {
395
+ echo '<th>' . $key . '</th>';
396
+ }
397
+ }
398
+ ?>
399
+ </tr>
400
+ </thead>
401
+ <tbody>
402
+ <tr>
403
+ <?php
404
+
405
+ $sum = array();
406
+ foreach ( $share_base_cache_target as $key => $value ) {
407
+ if( $value ){
408
+ $sum[$key] = 0;
409
+ }
410
+ }
411
+
412
+ if ( $site_query->have_posts() ) {
413
+ while ( $site_query->have_posts() ) {
414
+ $site_query->the_post();
415
+
416
+ $transient_id = self::OPT_SHARE_BASE_TRANSIENT_PREFIX . get_the_ID();
417
+
418
+ if ( false === ( $sns_counts = get_transient( $transient_id ) ) ) {
419
+
420
+ foreach ( $share_base_cache_target as $key => $value ) {
421
+ if( $value ){
422
+ /**
423
+ echo '<td class="not-cached share-count">';
424
+ _e( 'N/A', self::DOMAIN );
425
+ echo '</td>';
426
+ */
427
+
428
+ $meta_key = self::OPT_SHARE_2ND_META_KEY_PREFIX . strtolower( $key );
429
+ $sns_counts[$key] = get_post_meta( get_the_ID(), $meta_key, true );
430
+
431
+ if ( isset( $sns_counts[$key] ) && $sns_counts[$key] >= 0 ) {
432
+ $sum[$key] = $sum[$key] + $sns_counts[$key];
433
+ }
434
+ }
435
+ }
436
+
437
+ } else {
438
+
439
+ foreach ( $share_base_cache_target as $key => $value ) {
440
+ if ( $value ) {
441
+ if ( isset( $sns_counts[$key] ) && $sns_counts[$key] >= 0 ) {
442
+ $sum[$key] = $sum[$key] + $sns_counts[$key];
443
+ }
444
+ }
445
+ }
446
+
447
+ }
448
+ }
449
+ }
450
+
451
+ foreach ( $share_base_cache_target as $key => $value ) {
452
+ if ( $value ) {
453
+ echo '<td class="share-count">';
454
+ echo number_format( $sum[$key] );
455
+ echo '</td>';
456
+ }
457
+ }
458
+
459
+ wp_reset_postdata();
460
+
461
+ ?>
462
+ </tr>
463
+ </tbody>
464
+ </table>
465
+
466
+ </div>
467
+ </div>
468
+ </div>
469
+
470
+ </div>
471
+ </div>
includes/admin-help.php ADDED
@@ -0,0 +1,159 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /*
3
+ admin-help.php
4
+
5
+ Description: Option page implementation
6
+ Version: 0.4.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
+ <div class="wrap">
36
+ <h2><a href="admin.php?page=scc-help"><?php _e( 'SNS Count Cache', self::DOMAIN ) ?></a></h2>
37
+ <div class="sns-cnt-cache">
38
+
39
+ <h3 class="nav-tab-wrapper">
40
+ <a class="nav-tab" href="admin.php?page=scc-dashboard">Dashboard</a>
41
+ <a class="nav-tab" href="admin.php?page=scc-cache-status">Cache Status</a>
42
+ <a class="nav-tab" href="admin.php?page=scc-share-count">Share Count</a>
43
+ <a class="nav-tab" href="admin.php?page=scc-setting">Setting</a>
44
+ <a class="nav-tab nav-tab-active" href="admin.php?page=scc-help">Help</a>
45
+ </h3>
46
+
47
+ <div class="metabox-holder">
48
+ <div id="share-site-summary" class="postbox">
49
+ <div class="handlediv" title="Click to toggle"><br></div>
50
+ <h3 class="hndle"><span><?php _e( 'Help', self::DOMAIN ) ?></span></h3>
51
+ <div class="inside">
52
+
53
+
54
+ <h4><?php _e( 'What is SNS Cout Cache?', self::DOMAIN ) ?></h4>
55
+ <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>
56
+ <h4><?php _e( 'How often does this plugin get and cache share count?', self::DOMAIN) ?></h4>
57
+ <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>
58
+ <h4><?php _e( 'How can I know whether share count of each post is cached or not?', self::DOMAIN) ?></h4>
59
+ <p><?php _e( 'Cache status is described in the "Cache Status" tab in the setting page.', self::DOMAIN ) ?></p>
60
+ <h4><?php _e( 'How can I get share count from the cache?', self::DOMAIN) ?></h4>
61
+ <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>
62
+ <table class="view-table">
63
+ <thead>
64
+ <tr>
65
+ <th><?php _e( 'Function', self::DOMAIN ) ?></th>
66
+ <th><?php _e( 'Description', self::DOMAIN ) ?></th>
67
+ </tr>
68
+ </thead>
69
+ <tbody>
70
+ <tr><td>scc_get_share_twitter()</td><td><?php _e( 'Twitter share count is returned from cache.', self::DOMAIN ) ?></td></tr>
71
+ <tr><td>scc_get_share_facebook()</td><td><?php _e( 'Facebook share count is returned from cache.', self::DOMAIN ) ?></td></tr>
72
+ <tr><td>scc_get_share_gplus()</td><td><?php _e( 'Google Plus share count is returned from cache.', self::DOMAIN ) ?></td></tr>
73
+ <tr><td>scc_get_share_hatebu()</td><td><?php _e( 'Hatena Bookmark share count is returned from cache.', self::DOMAIN ) ?></td></tr>
74
+ <tr><td>scc_get_share_pocket()</td><td><?php _e( 'Pocket share count is returned from cache.', self::DOMAIN ) ?></td></tr>
75
+ <tr><td>scc_get_share_total()</td><td><?php _e( 'Total share count of selected SNS is returned from cache.', self::DOMAIN ) ?></td></tr>
76
+ </tbody>
77
+ </table>
78
+ <h4><?php _e( 'How can I get follow count from the cache?', self::DOMAIN) ?></h4>
79
+ <p><?php _e( 'The follow count is retrieved from the cache using the following functions.', self::DOMAIN ) ?></p>
80
+ <table class="view-table">
81
+ <thead>
82
+ <tr>
83
+ <th><?php _e( 'Function', self::DOMAIN ) ?></th>
84
+ <th><?php _e( 'Description', self::DOMAIN ) ?></th>
85
+ </tr>
86
+ </thead>
87
+ <tbody>
88
+ <tr><td>scc_get_follow_feedly()</td><td><?php _e( 'Feedly follow count is returned from cache.', self::DOMAIN ) ?></td></tr>
89
+ </tbody>
90
+ </table>
91
+ <h4><?php _e( 'Example Code', self::DOMAIN ) ?></h4>
92
+ <?php _e( 'The code below describes a simple example which displays share count and follower count using the above functions for each post.', self::DOMAIN ) ?>
93
+ <pre class="prettyprint">&lt;?php
94
+ $query_args = array(
95
+ &#039;post_type&#039; =&gt; &#039;post&#039;,
96
+ &#039;post_status&#039; =&gt; &#039;publish&#039;,
97
+ &#039;posts_per_page&#039; =&gt; 5
98
+ );
99
+
100
+ $posts_query = new WP_Query( $query_args );
101
+
102
+ if ( $posts_query-&gt;have_posts() ) {
103
+ while ( $posts_query-&gt;have_posts() ){
104
+ $posts_query-&gt;the_post();
105
+
106
+ if(function_exists('scc_get_share_twitter') &amp;&amp;
107
+ function_exists('scc_get_share_facebook') &amp;&amp;
108
+ function_exists('scc_get_share_gplus') &amp;&amp;
109
+ function_exists('scc_get_share_pocket') &amp;&amp;
110
+ function_exists('scc_get_share_total') &amp;&amp;
111
+ function_exists('scc_get_follow_feedly')
112
+ ){
113
+
114
+ ?&gt;
115
+
116
+ &lt;!--
117
+ In WordPress loop, you can use the given function
118
+ in order to get share count for current post and follower count.
119
+ --&gt;
120
+
121
+ &lt;p&gt;Twitter: &lt;?php echo scc_get_share_twitter(); ?&gt;&lt;/p&gt;
122
+ &lt;p&gt;Facebook: &lt;?php echo scc_get_share_facebook(); ?&gt;&lt;/p&gt;
123
+ &lt;p&gt;Google Plus: &lt;?php echo scc_get_share_gplus(); ?&gt;&lt;/p&gt;
124
+ &lt;p&gt;Pocket: &lt;?php echo scc_get_share_pocket(); ?&gt;&lt;/p&gt;
125
+ &lt;p&gt;Total: &lt;?php echo scc_get_share_total(); ?&gt;&lt;/p&gt;
126
+ &lt;p&gt;Feedly: &lt;?php echo scc_get_follow_feedly(); ?&gt;&lt;/p&gt;
127
+
128
+ &lt;?php
129
+ }
130
+ }
131
+ }
132
+ wp_reset_postdata();
133
+ ?&gt;</pre>
134
+ <h4><?php _e( 'How can I access specific custom field containing each share count?', self::DOMAIN) ?></h4>
135
+ <p><?php _e( 'The custom field including share count is accessed using the following meta keys.', self::DOMAIN ) ?></p>
136
+ <table class="view-table">
137
+ <thead>
138
+ <tr>
139
+ <th><?php _e( 'Meta Key', self::DOMAIN ) ?></th>
140
+ <th><?php _e( 'Description', self::DOMAIN ) ?></th>
141
+ </tr>
142
+ </thead>
143
+ <tbody>
144
+ <tr><td>scc_share_count_twitter</td><td><?php _e( 'A meta key for Twitter share count', self::DOMAIN ) ?></td></tr>
145
+ <tr><td>scc_share_count_facebook</td><td><?php _e( 'A meta key for Facebook share count', self::DOMAIN ) ?></td></tr>
146
+ <tr><td>scc_share_count_google+</td><td><?php _e( 'A meta key for Google Plus share count', self::DOMAIN ) ?></td></tr>
147
+ <tr><td>scc_share_count_hatebu</td><td><?php _e( 'A meta key for Hatena Bookmark share count', self::DOMAIN ) ?></td></tr>
148
+ <tr><td>scc_share_count_pocket</td><td><?php _e( 'A meta key for Pocket share count', self::DOMAIN ) ?></td></tr>
149
+ <tr><td>scc_share_count_total</td><td><?php _e( 'A meta key for total share count', self::DOMAIN ) ?></td></tr>
150
+ </tbody>
151
+ </table>
152
+ </div>
153
+ </div>
154
+ </div>
155
+
156
+ </div>
157
+ </div>
158
+
159
+
includes/admin-setting.php ADDED
@@ -0,0 +1,646 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /*
3
+ admin-setting.php
4
+
5
+ Description: Option page implementation
6
+ Version: 0.4.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
+ if ( isset( $_POST['_wpnonce'] ) && $_POST['_wpnonce'] ) {
34
+ if( check_admin_referer( __FILE__, '_wpnonce' ) ) {
35
+
36
+ if( isset( $_POST["update_all_options"] ) && $_POST["update_all_options"] === 'Update All Options' ) {
37
+
38
+ $share_base_custom_post_types = $_POST["share_base_custom_post_types"];
39
+ $share_base_check_interval = $_POST["share_base_check_interval"];
40
+ $share_base_posts_per_check = $_POST["share_base_posts_per_check"];
41
+ $dynamic_cache = $_POST["dynamic_cache"];
42
+ $share_rush_new_content_term = $_POST["share_rush_new_content_term"];
43
+
44
+ $share_base_cache_target_twitter = $_POST["share_base_cache_target_twitter"];
45
+ $share_base_cache_target_facebook = $_POST["share_base_cache_target_facebook"];
46
+ $share_base_cache_target_gplus = $_POST["share_base_cache_target_gplus"];
47
+ $share_base_cache_target_pocket = $_POST["share_base_cache_target_pocket"];
48
+ $share_base_cache_target_hatebu = $_POST["share_base_cache_target_hatebu"];
49
+
50
+ $data_export = $_POST["data_export"];
51
+ $data_export_interval = $_POST["data_export_interval"];
52
+
53
+ $share_base_cache_target = array();
54
+ $follow_base_cache_target = array();
55
+
56
+ if ( isset( $share_base_custom_post_types ) && $share_base_custom_post_types ) {
57
+ $share_base_custom_post_types = explode( ',', $share_base_custom_post_types );
58
+ update_option( self::DB_SHARE_CUSTOM_POST_TYPES, $share_base_custom_post_types );
59
+ } else {
60
+ update_option( self::DB_SHARE_CUSTOM_POST_TYPES, $share_base_custom_post_types );
61
+ }
62
+
63
+ if ( isset( $share_base_check_interval ) && $share_base_check_interval && is_numeric( $share_base_check_interval ) ) {
64
+ update_option( self::DB_SHARE_CHECK_INTERVAL, $share_base_check_interval );
65
+ }
66
+
67
+ if ( isset( $share_base_posts_per_check ) && $share_base_posts_per_check && is_numeric( $share_base_posts_per_check ) ) {
68
+ update_option( self::DB_SHARE_POSTS_PER_CHECK, $share_base_posts_per_check );
69
+ }
70
+
71
+ if ( isset( $dynamic_cache ) ) {
72
+ update_option( self::DB_COMMON_DYNAMIC_CACHE, $dynamic_cache );
73
+ }
74
+
75
+ if ( isset( $share_rush_new_content_term ) && $share_rush_new_content_term && is_numeric( $share_rush_new_content_term ) ) {
76
+ update_option( self::DB_SHARE_NEW_CONTENT_TERM, $share_rush_new_content_term );
77
+ }
78
+
79
+ if ( isset( $data_export ) ) {
80
+ update_option( self::DB_COMMON_DATA_EXPORT, $data_export );
81
+ }
82
+
83
+ if ( isset( $data_export_interval ) && $data_export_interval && is_numeric( $data_export_interval ) ) {
84
+ update_option( self::DB_COMMON_DATA_EXPORT_INTERVAL, $data_export_interval );
85
+ }
86
+
87
+ if ( isset( $share_base_cache_target_twitter ) && $share_base_cache_target_twitter ) {
88
+ $share_base_cache_target[self::REF_SHARE_TWITTER] = true;
89
+ } else {
90
+ $share_base_cache_target[self::REF_SHARE_TWITTER] = false;
91
+ }
92
+
93
+ if ( isset( $share_base_cache_target_facebook ) && $share_base_cache_target_facebook ) {
94
+ if ( Common_Util::extension_loaded_php_xml() ) {
95
+ $share_base_cache_target[self::REF_SHARE_FACEBOOK] = true;
96
+ } else {
97
+ $share_base_cache_target[self::REF_SHARE_FACEBOOK] = false;
98
+ }
99
+ } else {
100
+ $share_base_cache_target[self::REF_SHARE_FACEBOOK] = false;
101
+ }
102
+
103
+ if ( isset( $share_base_cache_target_gplus ) && $share_base_cache_target_gplus ) {
104
+ $share_base_cache_target[self::REF_SHARE_GPLUS] = true;
105
+ } else {
106
+ $share_base_cache_target[self::REF_SHARE_GPLUS] = false;
107
+ }
108
+
109
+ if ( isset( $share_base_cache_target_pocket ) && $share_base_cache_target_pocket ) {
110
+ if ( Common_Util::extension_loaded_php_xml() ) {
111
+ $share_base_cache_target[self::REF_SHARE_POCKET] = true;
112
+ } else {
113
+ $share_base_cache_target[self::REF_SHARE_POCKET] = false;
114
+ }
115
+ } else {
116
+ $share_base_cache_target[self::REF_SHARE_POCKET] = false;
117
+ }
118
+
119
+ if ( isset( $share_base_cache_target_hatebu ) && $share_base_cache_target_hatebu ) {
120
+ $share_base_cache_target[self::REF_SHARE_HATEBU] = true;
121
+ } else {
122
+ $share_base_cache_target[self::REF_SHARE_HATEBU] = false;
123
+ }
124
+
125
+ if ( ! empty( $share_base_cache_target ) ) {
126
+ update_option( self::DB_SHARE_CACHE_TARGET, $share_base_cache_target );
127
+ }
128
+
129
+ $follow_base_check_interval = $_POST["follow_base_check_interval"];
130
+
131
+ if ( isset( $follow_base_check_interval ) && $follow_base_check_interval && is_numeric( $follow_base_check_interval ) ) {
132
+ update_option(self::DB_FOLLOW_CHECK_INTERVAL, $follow_base_check_interval);
133
+ }
134
+
135
+ $follow_base_cache_target_feedly = $_POST["follow_base_cache_target_feedly"];
136
+
137
+ if ( isset( $follow_base_cache_target_feedly ) && $follow_base_cache_target_feedly ) {
138
+ $follow_base_cache_target[self::REF_FOLLOW_FEEDLY] = true;
139
+ } else {
140
+ $follow_base_cache_target[self::REF_FOLLOW_FEEDLY] = false;
141
+ }
142
+
143
+ if ( ! empty( $follow_base_cache_target ) ) {
144
+ update_option( self::DB_FOLLOW_CACHE_TARGET, $follow_base_cache_target );
145
+ }
146
+
147
+ $this->reactivate_plugin();
148
+
149
+ }
150
+
151
+ if( isset( $_POST["reset_data"] ) && $_POST["reset_data"] === 'Reset' ) {
152
+ Common_Util::log( '[' . __METHOD__ . '] reset' );
153
+ $this->export_engines[self::REF_COMMON_EXPORT]->reset_export();
154
+ }
155
+
156
+ if( isset( $_POST["export_data"] ) && $_POST["export_data"] === 'Export' ) {
157
+ Common_Util::log( '[' . __METHOD__ . '] export' );
158
+ $this->export_engines[self::REF_COMMON_EXPORT]->execute_export( NULL );
159
+ }
160
+
161
+ if( isset( $_POST["clear_share_base_cache"] ) && $_POST["clear_share_base_cache"] === 'Clear Cache' ) {
162
+ Common_Util::log( '[' . __METHOD__ . '] clear cache' );
163
+ $this->cache_engines[self::REF_SHARE_BASE]->clear_cache();
164
+ //$this->cache_engines[self::REF_SHARE_2ND]->clear_cache();
165
+ $this->cache_engines[self::REF_SHARE_2ND]->initialize_cache();
166
+ }
167
+
168
+ if( isset( $_POST["clear_follow_base_cache"] ) && $_POST["clear_follow_base_cache"] === 'Clear Cache' ) {
169
+ Common_Util::log( '[' . __METHOD__ . '] clear cache' );
170
+ $this->cache_engines[self::REF_FOLLOW_BASE]->clear_cache();
171
+ //$this->cache_engines[self::REF_FOLLOW_2ND]->clear_cache();
172
+ $this->cache_engines[self::REF_FOLLOW_2ND]->initialize_cache();
173
+ }
174
+ }
175
+
176
+ }
177
+
178
+ $share_base_custom_post_types = get_option( self::DB_SHARE_CUSTOM_POST_TYPES );
179
+
180
+ if ( ! $share_base_custom_post_types ) {
181
+ $share_base_custom_post_types = array();
182
+ }
183
+
184
+ $share_base_check_interval = get_option( self::DB_SHARE_CHECK_INTERVAL );
185
+ $share_base_check_interval = ! empty( $share_base_check_interval ) ? intval( $share_base_check_interval ) : self::OPT_SHARE_BASE_CHECK_INTERVAL;
186
+
187
+ $share_base_posts_per_check = get_option( self::DB_SHARE_POSTS_PER_CHECK );
188
+ $share_base_posts_per_check = ! empty( $share_base_posts_per_check ) ? intval( $share_base_posts_per_check ) : self::OPT_SHARE_BASE_POSTS_PER_CHECK;
189
+
190
+ $dynamic_cache = get_option( self::DB_COMMON_DYNAMIC_CACHE );
191
+ $dynamic_cache = isset( $dynamic_cache ) ? intval( $dynamic_cache ) : self::OPT_ACCESS_BASED_CACHE_NONE;
192
+
193
+ $share_rush_new_content_term = get_option( self::DB_SHARE_NEW_CONTENT_TERM );
194
+ $share_rush_new_content_term = ! empty( $share_rush_new_content_term ) ? intval( $share_rush_new_content_term ) : self::OPT_SHARE_RUSH_NEW_CONTENT_TERM;
195
+
196
+ $share_base_cache_target = get_option( self::DB_SHARE_CACHE_TARGET );
197
+
198
+ $follow_base_cache_target = get_option( self::DB_FOLLOW_CACHE_TARGET );
199
+
200
+ $follow_base_check_interval = get_option( self::DB_FOLLOW_CHECK_INTERVAL );
201
+ $follow_base_check_interval = ! empty( $follow_base_check_interval ) ? intval( $follow_base_check_interval ) : self::OPT_FOLLOW_BASE_CHECK_INTERVAL;
202
+
203
+ $data_export = get_option( self::DB_COMMON_DATA_EXPORT );
204
+ $data_export = isset( $data_export ) ? intval( $data_export ) : self::OPT_COMMON_DATA_EXPORT_MANUAL;
205
+
206
+ $data_export_interval = get_option( self::DB_COMMON_DATA_EXPORT_INTERVAL );
207
+ $data_export_interval = ! empty( $data_export_interval ) ? intval( $data_export_interval ) : self::OPT_COMMON_DATA_EXPORT_INTERVAL;
208
+
209
+ if ( ! $share_base_cache_target ) {
210
+ $share_base_cache_target[self::REF_SHARE_TWITTER] = true;
211
+ $share_base_cache_target[self::REF_SHARE_FACEBOOK] = true;
212
+ $share_base_cache_target[self::REF_SHARE_GPLUS] = true;
213
+ if ( extension_loaded( 'xml' ) && extension_loaded( 'xmlreader' ) && extension_loaded( 'xmlwriter' )) {
214
+ $share_base_cache_target[self::REF_SHARE_POCKET] = true;
215
+ }
216
+ $share_base_cache_target[self::REF_SHARE_HATEBU] = true;
217
+ }
218
+
219
+ $share_base_cache_target[self::REF_SHARE_TOTAL] = true;
220
+
221
+ if ( ! $follow_base_cache_target ) {
222
+ $follow_base_cache_target[self::REF_FOLLOW_FEEDLY] = true;
223
+ }
224
+
225
+ ?>
226
+ <div class="wrap">
227
+ <h2><a href="admin.php?page=scc-setting"><?php _e( 'SNS Count Cache', self::DOMAIN ) ?></a></h2>
228
+ <div class="sns-cnt-cache">
229
+
230
+ <h3 class="nav-tab-wrapper">
231
+ <a class="nav-tab" href="admin.php?page=scc-dashboard">Dashboard</a>
232
+ <a class="nav-tab" href="admin.php?page=scc-cache-status">Cache Status</a>
233
+ <a class="nav-tab" href="admin.php?page=scc-share-count">Share Count</a>
234
+ <a class="nav-tab nav-tab-active" href="admin.php?page=scc-setting">Setting</a>
235
+ <a class="nav-tab" href="admin.php?page=scc-help">Help</a>
236
+ </h3>
237
+
238
+ <div class="metabox-holder">
239
+ <div id="current-parameter" class="postbox">
240
+ <div class="handlediv" title="Click to toggle"><br></div>
241
+ <h3 class="hndle"><span><?php _e( 'Current Setting', self::DOMAIN ) ?></span></h3>
242
+ <div class="inside">
243
+ <p><?php _e( 'The following describes registered parameters.', self::DOMAIN ) ?></p>
244
+ <table class="view-table">
245
+ <thead>
246
+ <tr>
247
+ <th><?php _e( 'Function', self::DOMAIN ) ?></th>
248
+ <th><?php _e( 'Parameter', self::DOMAIN ) ?></th>
249
+ <th><?php _e( 'Value', self::DOMAIN ) ?></th>
250
+ </tr>
251
+ </thead>
252
+ <tbody>
253
+ <tr>
254
+ <td><?php _e( 'Share Base Cache', self::DOMAIN) ?></td><td><?php _e('Target SNS', self::DOMAIN ) ?></td>
255
+ <td>
256
+ <?php
257
+ $target_sns = array();
258
+ if ( isset( $share_base_cache_target[self::REF_SHARE_TWITTER] ) && $share_base_cache_target[self::REF_SHARE_TWITTER] ) {
259
+ $target_sns[] = 'Twitter';
260
+ }
261
+ if ( isset( $share_base_cache_target[self::REF_SHARE_FACEBOOK] ) && $share_base_cache_target[self::REF_SHARE_FACEBOOK] ) {
262
+ $target_sns[] = 'Facebook';
263
+ }
264
+ if ( isset( $share_base_cache_target[self::REF_SHARE_GPLUS] ) && $share_base_cache_target[self::REF_SHARE_GPLUS] ) {
265
+ $target_sns[] = 'Google+';
266
+ }
267
+ if ( isset( $share_base_cache_target[self::REF_SHARE_POCKET] ) && $share_base_cache_target[self::REF_SHARE_POCKET] ) {
268
+ $target_sns[] = 'Pocket';
269
+ }
270
+ if ( isset( $share_base_cache_target[self::REF_SHARE_HATEBU] ) && $share_base_cache_target[self::REF_SHARE_HATEBU] ) {
271
+ $target_sns[] = 'Hatena Bookmark';
272
+ }
273
+ echo implode( ", ", $target_sns );
274
+ ?>
275
+ </td>
276
+ </tr>
277
+ <tr>
278
+ <td><?php _e( 'Share Base Cache', self::DOMAIN ) ?></td>
279
+ <td><?php _e( 'Additional custom post types', self::DOMAIN ) ?></td>
280
+ <td><?php
281
+ if ( ! empty( $share_base_custom_post_types ) && $share_base_custom_post_types ) {
282
+ echo implode( ',', $share_base_custom_post_types );
283
+ } else {
284
+ _e( 'N/A', self::DOMAIN );
285
+ }
286
+ ?>
287
+ </td>
288
+ </tr>
289
+ <tr>
290
+ <td><?php _e( 'Share Base Cache', self::DOMAIN ) ?></td>
291
+ <td><?php _e( 'Interval cheking and caching share count', self::DOMAIN ) ?></td>
292
+ <td><?php echo $share_base_check_interval . ' seconds'; ?></td>
293
+ </tr>
294
+ <tr>
295
+ <td><?php _e( 'Share Base Cache', self::DOMAIN ) ?></td>
296
+ <td><?php _e( 'Number of posts to check at a time', self::DOMAIN ) ?></td>
297
+ <td><?php echo $share_base_posts_per_check . ' posts'; ?></td>
298
+ </tr>
299
+ <tr>
300
+ <td><?php _e( 'Share Rush Cache', self::DOMAIN ) ?></td>
301
+ <td><?php _e( 'Term considering posted content as new content', self::DOMAIN ) ?></td>
302
+ <td>
303
+ <?php
304
+ if ( $share_rush_new_content_term == 1 ) {
305
+ echo $share_rush_new_content_term . ' day';
306
+ } else if ( $share_rush_new_content_term > 1 ) {
307
+ echo $share_rush_new_content_term . ' days';
308
+ }
309
+ ?>
310
+ </td>
311
+ </tr>
312
+ <tr>
313
+ <td><?php _e( 'Follow Base Cache', self::DOMAIN) ?></td><td><?php _e('Target SNS', self::DOMAIN ) ?></td>
314
+ <td>
315
+ <?php
316
+ $target_sns = array();
317
+ if ( isset( $follow_base_cache_target[self::REF_FOLLOW_FEEDLY] ) && $follow_base_cache_target[self::REF_FOLLOW_FEEDLY] ) {
318
+ $target_sns[] = 'Feedly';
319
+ }
320
+ echo implode( ", ", $target_sns );
321
+ ?>
322
+ </td>
323
+ </tr>
324
+ <tr>
325
+ <td><?php _e( 'Follow Base Cache', self::DOMAIN ) ?></td>
326
+ <td><?php _e( 'Interval cheking and caching follower count', self::DOMAIN ) ?></td>
327
+ <td><?php echo $follow_base_check_interval . ' seconds'; ?></td>
328
+ </tr>
329
+ <tr>
330
+ <td><?php _e( 'Dynamic Cache', self::DOMAIN) ?></td><td><?php _e( 'Dynamic caching based on user access', self::DOMAIN ) ?></td><td>
331
+ <?php
332
+ switch ( $dynamic_cache ) {
333
+ case self::OPT_ACCESS_BASED_CACHE_NONE:
334
+ _e( 'disabled', self::DOMAIN );
335
+ break;
336
+ case self::OPT_ACCESS_BASED_SYNC_CACHE:
337
+ _e( 'enabled (Synchronous Cache)', self::DOMAIN );
338
+ break;
339
+ case self::OPT_ACCESS_BASED_ASYNC_CACHE:
340
+ _e( 'enabled (Asynchronous Cache)', self::DOMAIN );
341
+ case self::OPT_ACCESS_BASED_2ND_CACHE:
342
+ _e( 'enabled (Asynchronous 2nd Cache)', self::DOMAIN );
343
+ break;
344
+ }
345
+ ?>
346
+ </td>
347
+ </tr>
348
+ <tr>
349
+ <td><?php _e( 'Data Export', self::DOMAIN) ?></td><td><?php _e( 'Method of data export', self::DOMAIN ) ?></td><td>
350
+ <?php
351
+ switch ( $data_export ) {
352
+ case self::OPT_COMMON_DATA_EXPORT_MANUAL:
353
+ _e( 'Manual', self::DOMAIN );
354
+ break;
355
+ case self::OPT_COMMON_DATA_EXPORT_AUTO:
356
+ _e( 'Auto', self::DOMAIN );
357
+ break;
358
+ }
359
+ ?>
360
+ </td>
361
+ </tr>
362
+ <?php
363
+ if ( $data_export == self::OPT_COMMON_DATA_EXPORT_AUTO ) {
364
+ ?>
365
+ <tr>
366
+ <td><?php _e( 'Data Export', self::DOMAIN ) ?></td>
367
+ <td><?php _e( 'Interval exporting share count to a csv file', self::DOMAIN ) ?></td>
368
+ <td><?php echo $data_export_interval / 3600 . ' hours'; ?></td>
369
+ </tr>
370
+ <?php
371
+ }
372
+ ?>
373
+ </tbody>
374
+ </table>
375
+ </div>
376
+ </div>
377
+ </div>
378
+ <div class="metabox-holder">
379
+ <form action="admin.php?page=scc-setting" method="post">
380
+ <?php wp_nonce_field( __FILE__, '_wpnonce' ); ?>
381
+ <div id="share-base-cache" class="postbox">
382
+ <div class="handlediv" title="Click to toggle"><br></div>
383
+ <h3 class="hndle"><span><?php _e('Share Base Cache', self::DOMAIN) ?></span></h3>
384
+ <div class="inside">
385
+ <table class="form-table">
386
+ <tr>
387
+ <th><label><?php _e( 'Target SNS', self::DOMAIN ) ?></label></th>
388
+ <td>
389
+ <div class="sns-check">
390
+ <input type="checkbox" value="1" name="share_base_cache_target_twitter"<?php if ( $share_base_cache_target[self::REF_SHARE_TWITTER] ) echo ' checked="checked"'; ?> />
391
+ <label><?php _e( 'Twitter', self::DOMAIN ) ?></label>
392
+ </div>
393
+ <div class="sns-check">
394
+ <input type="checkbox" value="1" name="share_base_cache_target_facebook"<?php if ( $share_base_cache_target[self::REF_SHARE_FACEBOOK] ) echo ' checked="checked"'; ?> />
395
+ <label><?php _e( 'Facebook', self::DOMAIN ) ?></label>
396
+ </div>
397
+ <div class="sns-check">
398
+ <input type="checkbox" value="1" name="share_base_cache_target_gplus"<?php if ( $share_base_cache_target[self::REF_SHARE_GPLUS] ) echo ' checked="checked"'; ?> />
399
+ <label><?php _e( 'Google+', self::DOMAIN ) ?></label>
400
+ </div>
401
+ <div class="sns-check">
402
+ <input type="checkbox" value="1" name="share_base_cache_target_pocket"<?php if ( $share_base_cache_target[self::REF_SHARE_POCKET] ) echo ' checked="checked"'; ?> />
403
+ <label><?php _e( 'Pocket', self::DOMAIN ) ?></label>
404
+ </div>
405
+ <div class="sns-check">
406
+ <input type="checkbox" value="1" name="share_base_cache_target_hatebu"<?php if ( $share_base_cache_target[self::REF_SHARE_HATEBU] ) echo ' checked="checked"'; ?> />
407
+ <label><?php _e( 'Hatena Bookmark', self::DOMAIN ) ?></label>
408
+ </div>
409
+ </td>
410
+ </tr>
411
+ <tr>
412
+ <th><label><?php _e( 'Additional custom post types', self::DOMAIN ) ?></label></th>
413
+ <td>
414
+ <input type="text" class="text" name="share_base_custom_post_types" size="60" value="" />
415
+ <br>
416
+ <label><?php _e( 'e.g. aaa, bbb, ccc (comma-delimited)', self::DOMAIN ) ?></label>
417
+ </td>
418
+ </tr>
419
+ <tr>
420
+ <th><label><?php _e( 'Interval cheking and caching share count (sec)', self::DOMAIN ) ?></label></th>
421
+ <td>
422
+ <input type="text" class="text" name="share_base_check_interval" size="20" value="" />
423
+ <label><?php _e( 'Default: 600', self::DOMAIN ) ?></label>
424
+ </td>
425
+ </tr>
426
+ <tr>
427
+ <th><label><?php _e( 'Number of posts to check at a time (posts)', self::DOMAIN ) ?></label></th>
428
+ <td>
429
+ <input type="text" class="text" name="share_base_posts_per_check" size="20" value="" />
430
+ <label><?php _e( 'Default: 20', self::DOMAIN ) ?></label>
431
+ </td>
432
+ </tr>
433
+ </table>
434
+ <div class="submit-button">
435
+ <input type="submit" class="button button-primary" name="update_all_options" value="<?php _e( 'Update All Options', self::DOMAIN ) ?>" />
436
+ <input type="submit" class="button" name="clear_share_base_cache" value="<?php _e( 'Clear Cache', self::DOMAIN ) ?>">
437
+ </div>
438
+ </div>
439
+ </div>
440
+ <div id="share-rush-cache" class="postbox">
441
+ <div class="handlediv" title="Click to toggle"><br></div>
442
+ <h3 class="hndle"><span><?php _e('Share Rush Cache', self::DOMAIN) ?></span></h3>
443
+ <div class="inside">
444
+ <table class="form-table">
445
+ <tr>
446
+ <th><label><?php _e( 'Term considering posted content as new content', self::DOMAIN ) ?></label></th>
447
+ <td>
448
+ <select name="share_rush_new_content_term">
449
+ <option value="1"<?php if ( $share_rush_new_content_term == 1 ) echo ' selected="selected"'; ?>>1 day</option>
450
+ <option value="2"<?php if ( $share_rush_new_content_term == 2 ) echo ' selected="selected"'; ?>>2 days</option>
451
+ <option value="3"<?php if ( $share_rush_new_content_term == 3 ) echo ' selected="selected"'; ?>>3 days</option>
452
+ <option value="4"<?php if ( $share_rush_new_content_term == 4 ) echo ' selected="selected"'; ?>>4 days</option>
453
+ <option value="5"<?php if ( $share_rush_new_content_term == 5 ) echo ' selected="selected"'; ?>>5 days</option>
454
+ <option value="6"<?php if ( $share_rush_new_content_term == 6 ) echo ' selected="selected"'; ?>>6 days</option>
455
+ <option value="7"<?php if ( $share_rush_new_content_term == 7 ) echo ' selected="selected"'; ?>>7 days</option>
456
+ <option value="8"<?php if ( $share_rush_new_content_term == 8 ) echo ' selected="selected"'; ?>>8 days</option>
457
+ <option value="9"<?php if ( $share_rush_new_content_term == 9 ) echo ' selected="selected"'; ?>>9 days</option>
458
+ <option value="10"<?php if ( $share_rush_new_content_term == 10 ) echo ' selected="selected"'; ?>>10 days</option>
459
+ <option value="11"<?php if ( $share_rush_new_content_term == 11 ) echo ' selected="selected"'; ?>>11 days</option>
460
+ <option value="12"<?php if ( $share_rush_new_content_term == 12 ) echo ' selected="selected"'; ?>>12 days</option>
461
+ <option value="13"<?php if ( $share_rush_new_content_term == 13 ) echo ' selected="selected"'; ?>>13 days</option>
462
+ <option value="14"<?php if ( $share_rush_new_content_term == 14 ) echo ' selected="selected"'; ?>>14 days</option>
463
+ </select>
464
+ <label><?php _e( 'Default: 3 days', self::DOMAIN ) ?></label>
465
+ </td>
466
+ </tr>
467
+ </table>
468
+ <div class="submit-button">
469
+ <input type="submit" class="button button-primary" name="update_all_options" value="<?php _e( 'Update All Options', self::DOMAIN ) ?>" />
470
+ </div>
471
+ </div>
472
+ </div>
473
+ <div id="follow-base-cache" class="postbox">
474
+ <div class="handlediv" title="Click to toggle"><br></div>
475
+ <h3 class="hndle"><span><?php _e('Follow Base Cache', self::DOMAIN) ?></span></h3>
476
+ <div class="inside">
477
+ <table class="form-table">
478
+ <tr>
479
+ <th><label><?php _e( 'Target SNS', self::DOMAIN ) ?></label></th>
480
+ <td>
481
+ <div class="sns-check">
482
+ <input type="checkbox" value="1" name="follow_base_cache_target_feedly"<?php if ( $follow_base_cache_target[self::REF_FOLLOW_FEEDLY] ) echo ' checked="checked"'; ?> />
483
+ <label><?php _e( 'Feedly', self::DOMAIN ) ?></label>
484
+ </div>
485
+ </td>
486
+ </tr>
487
+ <tr>
488
+ <th><label><?php _e( 'Interval cheking and caching follower count (sec)', self::DOMAIN ) ?></label></th>
489
+ <td>
490
+ <input type="text" class="text" name="follow_base_check_interval" size="20" value="" />
491
+ <label><?php _e( 'Default: 1800', self::DOMAIN ) ?></label>
492
+ </td>
493
+ </tr>
494
+ </table>
495
+ <div class="submit-button">
496
+ <input type="submit" class="button button-primary" name="update_all_options" value="<?php _e( 'Update All Options', self::DOMAIN ) ?>" />
497
+ <input type="submit" class="button" name="clear_follow_base_cache" value="<?php _e( 'Clear Cache', self::DOMAIN ) ?>">
498
+ </div>
499
+ </div>
500
+ </div>
501
+ <div id="common-dynamic-cache" class="postbox">
502
+ <div class="handlediv" title="Click to toggle"><br></div>
503
+ <h3 class="hndle"><span><?php _e( 'Dynamic Cache', self::DOMAIN ) ?></span></h3>
504
+ <div class="inside">
505
+ <table class="form-table">
506
+ <tr>
507
+ <th><label><?php _e( 'Dynamic caching based on user access', self::DOMAIN ) ?></label></th>
508
+ <td>
509
+ <select name="dynamic cache">
510
+ <option value="0"<?php if ( $dynamic_cache == 0 ) echo ' selected="selected"'; ?>><?php _e( 'None', self::DOMAIN ) ?></option>
511
+ <option value="1"<?php if ( $dynamic_cache == 1 ) echo ' selected="selected"'; ?>><?php _e( 'Synchronous Cache', self::DOMAIN ) ?></option>
512
+ <option value="2"<?php if ( $dynamic_cache == 2 ) echo ' selected="selected"'; ?>><?php _e( 'Asynchronous Cache', self::DOMAIN ) ?></option>
513
+ <option value="3"<?php if ( $dynamic_cache == 3 ) echo ' selected="selected"'; ?>><?php _e( 'Asynchronous 2nd Cache', self::DOMAIN ) ?></option>
514
+ </select>
515
+ <label><?php _e('Default: None', self::DOMAIN) ?></label>
516
+ </td>
517
+ </tr>
518
+ </table>
519
+ <div class="submit-button">
520
+ <input type="submit" class="button button-primary" name="update_all_options" value="<?php _e( 'Update All Options', self::DOMAIN ) ?>" />
521
+ </div>
522
+ </div>
523
+ </div>
524
+ <div id="common-data-export" class="postbox">
525
+ <div class="handlediv" title="Click to toggle"><br></div>
526
+ <h3 class="hndle"><span><?php _e('Data Export', self::DOMAIN) ?></span></h3>
527
+ <div class="inside">
528
+ <table class="form-table">
529
+ <tr>
530
+ <th><label><?php _e( 'Method of data export', self::DOMAIN ) ?></label></th>
531
+ <td>
532
+ <select name="data_export">
533
+ <option value="0"<?php if ( $data_export == 0 ) echo ' selected="selected"'; ?>><?php _e( 'Manual', self::DOMAIN ) ?></option>
534
+ <option value="1" disabled<?php if ( $data_export == 1 ) echo ' selected="selected"'; ?>><?php _e( 'Auto', self::DOMAIN ) ?></option>
535
+ </select>
536
+ <label><?php _e('Default: Manual', self::DOMAIN) ?></label>
537
+ </td>
538
+ </tr>
539
+ <?php
540
+ if ( $data_export == 1 ) {
541
+ ?>
542
+
543
+ <tr>
544
+ <th><label><?php _e( 'Interval exporting share count to a csv file', self::DOMAIN ) ?></label></th>
545
+ <td>
546
+ <select name="data_export_interval">
547
+ <option value="10800"<?php if ( $data_export_interval == 10800 ) echo ' selected="selected"'; ?>>3 hours</option>
548
+ <option value="21600"<?php if ( $data_export_interval == 21600 ) echo ' selected="selected"'; ?>>6 hours</option>
549
+ <option value="43200"<?php if ( $data_export_interval == 43200 ) echo ' selected="selected"'; ?>>12 hours</option>
550
+ <option value="86400"<?php if ( $data_export_interval == 86400 ) echo ' selected="selected"'; ?>>24 hours</option>
551
+ </select>
552
+ <label><?php _e( 'Default: 12 hours', self::DOMAIN ) ?></label>
553
+ </td>
554
+ </tr>
555
+ <?php
556
+ }
557
+ ?>
558
+ </table>
559
+ <div class="submit-button">
560
+ <input type="submit" class="button button-primary" name="update_all_options" value="<?php _e( 'Update All Options', self::DOMAIN ) ?>" />
561
+ </div>
562
+ </div>
563
+ </div>
564
+ </form>
565
+ </div>
566
+ <div class="metabox-holder">
567
+ <div id="exported-file" class="postbox">
568
+ <div class="handlediv" title="Click to toggle"><br></div>
569
+ <h3 class="hndle"><span><?php _e('Exported File', self::DOMAIN) ?></span></h3>
570
+ <div class="inside">
571
+ <table class="form-table">
572
+ <tbody>
573
+ <tr>
574
+ <th><?php _e( 'Disk usage of exported file', self::DOMAIN ) ?></th>
575
+ <td>
576
+ <?php
577
+ $abs_path = WP_PLUGIN_DIR . '/sns-count-cache/data/sns-count-cache-data.csv';
578
+ $file_size = Common_Util::get_file_size( $abs_path );
579
+
580
+ if ( isset( $file_size ) ) {
581
+ echo $file_size;
582
+ } else {
583
+ _e('No exported file', self::DOMAIN);
584
+ }
585
+ ?>
586
+ </td>
587
+ </tr>
588
+ </tbody>
589
+ </table>
590
+ <form action="admin.php?page=scc-setting" method="post">
591
+ <?php wp_nonce_field( __FILE__, '_wpnonce' ); ?>
592
+ <table class="form-table">
593
+ <tbody>
594
+ <tr>
595
+ <th><?php _e( 'Manual export', self::DOMAIN ) ?></th>
596
+ <td>
597
+ <input type="submit" class="button" name="export_data" value="<?php _e( 'Export', self::DOMAIN ) ?>" />
598
+ <br>
599
+ <span class="description">Export share count to a csv file.</span>
600
+ </td>
601
+ </tr>
602
+ </tbody>
603
+ </table>
604
+ </form>
605
+ <?php
606
+ if ( file_exists( $abs_path ) ) {
607
+ ?>
608
+ <form action="admin.php?page=scc-setting" method="post">
609
+ <?php wp_nonce_field( __FILE__, '_wpnonce' ); ?>
610
+ <table class="form-table">
611
+ <tbody>
612
+ <tr>
613
+ <th>Reset of exported file</th>
614
+ <td>
615
+ <input type="submit" class="button" name="reset_data" value="<?php _e( 'Reset', self::DOMAIN ) ?>" />
616
+ <br>
617
+ <span class="description">Clear exported csv file.</span>
618
+ </td>
619
+ </tr>
620
+ </tbody>
621
+ </table>
622
+ </form>
623
+ <form action="<?php echo plugins_url(); ?>/sns-count-cache/includes/download.php" method="post">
624
+ <?php wp_nonce_field( 'mynonce', '_wpnonce' ); ?>
625
+ <table class="form-table">
626
+ <tbody>
627
+ <tr>
628
+ <th>Download of exported file</th>
629
+ <td>
630
+ <input type="submit" class="button" name="download_data" value="<?php _e( 'Download', self::DOMAIN ) ?>" />
631
+ <br>
632
+ <span class="description">Download the exported csv file.</span>
633
+ </td>
634
+ </tr>
635
+ </tbody>
636
+ </table>
637
+ </form>
638
+ <?php
639
+ }
640
+ ?>
641
+ </div>
642
+ </div>
643
+ </div>
644
+
645
+ </div>
646
+ </div>
includes/admin-share-count.php ADDED
@@ -0,0 +1,283 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /*
3
+ admin-share-count.php
4
+
5
+ Description: Option page implementation
6
+ Version: 0.4.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
+ $posts_per_page = 50;
34
+
35
+ $sort_mode = false;
36
+ $sort_exec_key = '';
37
+ /*
38
+ if ( isset( $_GET['_wpnonce'] ) && $_GET['_wpnonce'] ) {
39
+ $nonce = $_GET['_wpnonce'];
40
+
41
+ if ( wp_verify_nonce( $nonce, __FILE__ ) ) {
42
+ if( isset( $_GET["action"] ) && $_GET["action"] === 'sort' ) {
43
+
44
+ if( isset( $_GET["key"] ) ) {
45
+ $sort_mode = true;
46
+ $key = $_GET["key"];
47
+
48
+ if ( $key === 'Google' ) {
49
+ $key = $key . '+';
50
+ }
51
+
52
+ $sort_exec_key = $key;
53
+ $meta_key = self::OPT_SHARE_2ND_META_KEY_PREFIX . strtolower( $key );
54
+ }
55
+
56
+ }
57
+ }
58
+ }
59
+ */
60
+
61
+ if( isset( $_GET["action"] ) && $_GET["action"] === 'sort' ) {
62
+
63
+ if( isset( $_GET["key"] ) ) {
64
+ $sort_mode = true;
65
+ $key = $_GET["key"];
66
+
67
+ if ( $key === 'Google' ) {
68
+ $key = $key . '+';
69
+ }
70
+
71
+ $sort_exec_key = $key;
72
+ $meta_key = self::OPT_SHARE_2ND_META_KEY_PREFIX . strtolower( $key );
73
+ }
74
+
75
+ }
76
+
77
+ $paged = 1;
78
+
79
+ if ( isset( $_GET['paged'] ) && is_numeric( $_GET['paged'] ) && $_GET['paged'] > 0 ) {
80
+ $paged = $_GET['paged'];
81
+ } else {
82
+ $paged = 1;
83
+ }
84
+
85
+ $share_base_cache_target = $this->share_base_cache_target ;
86
+ unset( $share_base_cache_target[self::REF_CRAWL_DATE] );
87
+
88
+ ?>
89
+ <div class="wrap">
90
+ <h2><a href="admin.php?page=scc-share-count"><?php _e( 'SNS Count Cache', self::DOMAIN ) ?></a></h2>
91
+ <div class="sns-cnt-cache">
92
+ <h3 class="nav-tab-wrapper">
93
+ <a class="nav-tab" href="admin.php?page=scc-dashboard">Dashboard</a>
94
+ <a class="nav-tab" href="admin.php?page=scc-cache-status">Cache Status</a>
95
+ <a class="nav-tab nav-tab-active" href="admin.php?page=scc-share-count">Share Count</a>
96
+ <a class="nav-tab" href="admin.php?page=scc-setting">Setting</a>
97
+ <a class="nav-tab" href="admin.php?page=scc-help">Help</a>
98
+ </h3>
99
+
100
+ <div class="metabox-holder">
101
+ <div id="share-each-content" class="postbox">
102
+ <div class="handlediv" title="Click to toggle"><br></div>
103
+ <h3 class="hndle"><span><?php _e( 'Share Count', self::DOMAIN ) ?></span></h3>
104
+ <div class="inside">
105
+
106
+
107
+ <table class="view-table">
108
+ <thead>
109
+ <tr>
110
+ <th>No.</th>
111
+ <th><?php _e( 'Content', self::DOMAIN ) ?></th>
112
+ <?php
113
+
114
+ foreach ( $share_base_cache_target as $key => $value ){
115
+
116
+ if ( $value ) {
117
+
118
+ $nonce = wp_create_nonce( __FILE__ );
119
+
120
+ $sort_key = $key;
121
+
122
+ if ( $sort_key === self::REF_SHARE_GPLUS ) {
123
+ $sort_key = str_replace( '+', '', $sort_key );
124
+ }
125
+
126
+ //$sort_url = esc_url( 'admin.php?page=scc-share-count&_wpnonce=' . $nonce . '&action=sort&key=' . $sort_key );
127
+ $sort_url = esc_url( 'admin.php?page=scc-share-count&action=sort&key=' . $sort_key );
128
+
129
+ if ( $key === $sort_exec_key ){
130
+ echo '<th><a class="sort-exec-key" href="' . $sort_url . '">' . $key . '</th>';
131
+ } else {
132
+ echo '<th><a href="' . $sort_url . '">' . $key . '</th>';
133
+ }
134
+ }
135
+
136
+ }
137
+
138
+ /*
139
+ if ( function_exists( 'wpp_get_views' ) ) {
140
+ echo '<th>Views</th>';
141
+ }
142
+ */
143
+
144
+ ?>
145
+ </tr>
146
+ </thead>
147
+ <tbody>
148
+
149
+ <?php
150
+
151
+ if( $sort_mode ) {
152
+ $query_args = array(
153
+ 'post_type' => $this->share_base_cache_post_types,
154
+ 'post_status' => 'publish',
155
+ 'posts_per_page' => $posts_per_page,
156
+ 'paged' => $paged,
157
+ 'meta_key' => $meta_key,
158
+ 'orderby' => 'meta_value_num',
159
+ 'update_post_term_cache' => false,
160
+ 'order' => 'DESC'
161
+ );
162
+
163
+ } else {
164
+
165
+ $query_args = array(
166
+ 'post_type' => $this->share_base_cache_post_types,
167
+ 'post_status' => 'publish',
168
+ 'posts_per_page' => $posts_per_page,
169
+ 'paged' => $paged,
170
+ 'update_post_term_cache' => false
171
+ );
172
+ }
173
+
174
+ $posts_query = new WP_Query( $query_args );
175
+
176
+ $count = ($paged - 1) * $posts_per_page + 1;
177
+
178
+ if ( $posts_query->have_posts() ) {
179
+ while ( $posts_query->have_posts() ) {
180
+ $posts_query->the_post();
181
+ ?>
182
+ <tr>
183
+ <td><?php echo $count; ?></td>
184
+ <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>
185
+ <?php
186
+ $transient_id = self::OPT_SHARE_BASE_TRANSIENT_PREFIX . get_the_ID();
187
+
188
+ if ( $sort_mode || false === ( $sns_counts = get_transient( $transient_id ) ) ) {
189
+
190
+ foreach ( $share_base_cache_target as $key => $value ) {
191
+ if( $value ){
192
+ /**
193
+ echo '<td class="not-cached share-count">';
194
+ _e( 'N/A', self::DOMAIN );
195
+ echo '</td>';
196
+ */
197
+
198
+ $meta_key = self::OPT_SHARE_2ND_META_KEY_PREFIX . strtolower( $key );
199
+ $sns_counts[$key] = get_post_meta( get_the_ID(), $meta_key, true );
200
+
201
+ if ( isset( $sns_counts[$key] ) && $sns_counts[$key] >= 0 ) {
202
+ echo '<td class="share-count">';
203
+ echo number_format( $sns_counts[$key] );
204
+ echo '</td>';
205
+ } else {
206
+ echo '<td class="not-cached share-count">';
207
+ _e( 'N/A', self::DOMAIN );
208
+ echo '</td>';
209
+ }
210
+ }
211
+ }
212
+
213
+ /*
214
+ // WordPress Popular Posts
215
+ if ( function_exists( 'wpp_get_views' ) ) {
216
+ $views = wpp_get_views( get_the_ID(), 'all', true);
217
+
218
+ if ( $views >= 0 ) {
219
+ echo '<td class="share-count">';
220
+ echo $views;
221
+ echo '</td>';
222
+ } else {
223
+ echo '<td class="not-cached share-count">';
224
+ _e( 'N/A', self::DOMAIN );
225
+ echo '</td>';
226
+ }
227
+ }
228
+ */
229
+ } else {
230
+
231
+ foreach ( $share_base_cache_target as $key => $value ) {
232
+ if ( $value ) {
233
+ if ( isset( $sns_counts[$key] ) && $sns_counts[$key] >= 0 ) {
234
+ echo '<td class="share-count">';
235
+ echo number_format( $sns_counts[$key] );
236
+ echo '</td>';
237
+ } else {
238
+ echo '<td class="not-cached share-count">';
239
+ _e( 'N/A', self::DOMAIN );
240
+ echo '</td>';
241
+ }
242
+ }
243
+ }
244
+ /*
245
+ // WordPress Popular Posts
246
+ if ( function_exists( 'wpp_get_views' ) ) {
247
+ $views = wpp_get_views( get_the_ID(), 'all', true );
248
+
249
+ if ( $views >= 0 ) {
250
+ echo '<td class="share-count">';
251
+ echo $views;
252
+ echo '</td>';
253
+ } else {
254
+ echo '<td class="not-cached share-count">';
255
+ _e( 'N/A', self::DOMAIN );
256
+ echo '</td>';
257
+ }
258
+ }
259
+ */
260
+
261
+ }
262
+ ?>
263
+ </tr>
264
+
265
+ <?php
266
+ $count++;
267
+
268
+ }
269
+ }
270
+ ?>
271
+ </tbody>
272
+ </table>
273
+ <?php
274
+ $this->pagination( $posts_query->max_num_pages, '', $paged, true );
275
+
276
+ wp_reset_postdata();
277
+ ?>
278
+
279
+ </div>
280
+ </div>
281
+ </div>
282
+ </div>
283
+ </div>
includes/admin.php DELETED
@@ -1,524 +0,0 @@
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-cache-engine.php ADDED
@@ -0,0 +1,162 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /*
3
+ class-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.4.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
+ abstract class Cache_Engine {
35
+
36
+ /**
37
+ * Prefix of cache ID
38
+ */
39
+ protected $transient_prefix = NULL;
40
+
41
+ /**
42
+ * Cron name to schedule cache processing
43
+ */
44
+ protected $prime_cron = NULL;
45
+
46
+ /**
47
+ * Cron name to execute cache processing
48
+ */
49
+ protected $execute_cron = NULL;
50
+
51
+ /**
52
+ * Schedule name for cache processing
53
+ */
54
+ protected $event_schedule = NULL;
55
+
56
+ /**
57
+ * Schedule description for cache processing
58
+ */
59
+ protected $event_description = NULL;
60
+
61
+ /**
62
+ * Instance
63
+ */
64
+ private static $instance = array();
65
+
66
+ /**
67
+ * Class constarctor
68
+ * Hook onto all of the actions and filters needed by the plugin.
69
+ *
70
+ */
71
+ protected function __construct() {
72
+ Common_Util::log('[' . __METHOD__ . '] (line='. __LINE__ . ')');
73
+ }
74
+
75
+ /**
76
+ * Get instance
77
+ *
78
+ * @since 0.1.1
79
+ */
80
+ public static function get_instance() {
81
+
82
+ $class_name = get_called_class();
83
+ if ( ! isset( self::$instance[$class_name] ) ) {
84
+ self::$instance[$class_name] = new $class_name();
85
+ //self::$instance[$class_name]->initialize($crawler, $options=array());
86
+ }
87
+
88
+ return self::$instance[$class_name];
89
+ }
90
+
91
+ /**
92
+ * Initialization
93
+ *
94
+ * @since 0.1.1
95
+ */
96
+ abstract public function initialize( $options = array() );
97
+
98
+ /**
99
+ * Register base schedule for this engine
100
+ *
101
+ * @since 0.1.0
102
+ */
103
+ public function register_schedule() {
104
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
105
+
106
+ if ( ! wp_next_scheduled( $this->prime_cron ) ) {
107
+ wp_schedule_event( time(), $this->event_schedule, $this->prime_cron );
108
+ }
109
+ }
110
+
111
+ /**
112
+ * Unregister base schedule for this engine
113
+ *
114
+ * @since 0.1.0
115
+ */
116
+ public function unregister_schedule() {
117
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
118
+
119
+ wp_clear_scheduled_hook( $this->prime_cron );
120
+ WP_Cron_Util::clear_scheduled_hook( $this->execute_cron );
121
+ }
122
+
123
+ /**
124
+ * Get cache expiration based on current number of total post and page
125
+ *
126
+ * @since 0.1.1
127
+ */
128
+ abstract protected function get_cache_expiration();
129
+
130
+ /**
131
+ * Get and cache data for a given post
132
+ *
133
+ * @since 0.1.1
134
+ */
135
+ abstract public function cache( $post_ID, $target_sns, $cache_expiration );
136
+
137
+ /**
138
+ * Initialize cache
139
+ *
140
+ * @since 0.3.0
141
+ */
142
+ abstract public function initialize_cache();
143
+
144
+ /**
145
+ * Clear cache
146
+ *
147
+ * @since 0.3.0
148
+ */
149
+ abstract public function clear_cache();
150
+
151
+ /**
152
+ * Get share transient ID
153
+ *
154
+ * @since 0.1.1
155
+ */
156
+ protected function get_transient_ID( $suffix ) {
157
+ return $this->transient_prefix . $suffix;
158
+ }
159
+
160
+ }
161
+
162
+ ?>
includes/class-common-data-export-engine.php ADDED
@@ -0,0 +1,329 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /*
3
+ class-common-data-export.php
4
+
5
+ Description: This class is a data export engine whitch exports cached data using wp-cron at regular intervals
6
+ Version: 0.4.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
+ class Common_Data_Export_Engine extends Export_Engine {
35
+
36
+ /**
37
+ * Meta key for share second cache
38
+ */
39
+ const DEF_SHARE_META_KEY_PREFIX = 'scc_share_count_';
40
+
41
+ /**
42
+ * Meta key for follow second cache
43
+ */
44
+ const DEF_FOLLOW_META_KEY_PREFIX = 'scc_follow_count_';
45
+
46
+ /**
47
+ * Cron name to schedule cache processing
48
+ */
49
+ const DEF_PRIME_CRON = 'scc_common_dataexport_prime';
50
+
51
+ /**
52
+ * Cron name to execute cache processing
53
+ */
54
+ const DEF_EXECUTE_CRON = 'scc_common_dataexport_exec';
55
+
56
+ /**
57
+ * Schedule name for cache processing
58
+ */
59
+ const DEF_EVENT_SCHEDULE = 'common_data_export_event';
60
+
61
+ /**
62
+ * Schedule description for cache processing
63
+ */
64
+ const DEF_EVENT_DESCRIPTION = '[SCC] Common Data Export Interval';
65
+
66
+ /**
67
+ * Option flag of data export
68
+ */
69
+ private $export_activation = 0;
70
+
71
+ /**
72
+ * Interval for data export
73
+ */
74
+ private $export_interval = 43200;
75
+
76
+ /**
77
+ * Excluded keys of data export
78
+ */
79
+ private $export_exclude_keys = array();
80
+
81
+ /**
82
+ * File name of data export
83
+ */
84
+ private $export_file_name = 'sns-count-cache-data.csv';
85
+
86
+ /**
87
+ * Cache post types
88
+ */
89
+ private $post_types = array( 'post', 'page' );
90
+
91
+ /**
92
+ * Cache target of share count
93
+ */
94
+ private $share_target_sns = array();
95
+
96
+ /**
97
+ * Cache target of follow count
98
+ */
99
+ private $follow_target_sns = array();
100
+
101
+ /**
102
+ * Class constarctor
103
+ * Hook onto all of the actions and filters needed by the plugin.
104
+ *
105
+ */
106
+ protected function __construct() {
107
+ Common_Util::log('[' . __METHOD__ . '] (line='. __LINE__ . ')');
108
+ }
109
+
110
+ /**
111
+ * Initialization
112
+ *
113
+ * @since 0.1.1
114
+ */
115
+ public function initialize( $options = array() ) {
116
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
117
+
118
+ $this->share_meta_key_prefix = self::DEF_SHARE_META_KEY_PREFIX;
119
+ $this->follow_meta_key_prefix = self::DEF_FOLLOW_META_KEY_PREFIX;
120
+ $this->prime_cron = self::DEF_PRIME_CRON;
121
+ $this->execute_cron = self::DEF_EXECUTE_CRON;
122
+ $this->event_schedule = self::DEF_EVENT_SCHEDULE;
123
+ $this->event_description = self::DEF_EVENT_DESCRIPTION;
124
+
125
+ if ( isset( $options['export_activation'] ) ) $this->export_activation = $options['export_activation'];
126
+ if ( isset( $options['export_interval'] ) ) $this->export_interval = $options['export_interval'];
127
+ if ( isset( $options['prime_cron'] ) ) $this->prime_cron = $options['prime_cron'];
128
+ if ( isset( $options['execute_cron'] ) ) $this->execute_cron = $options['execute_cron'];
129
+ if ( isset( $options['event_schedule'] ) ) $this->event_schedule = $options['event_schedule'];
130
+ if ( isset( $options['event_description'] ) ) $this->event_description = $options['event_description'];
131
+ if ( isset( $options['export_exclude_keys'] ) ) $this->export_exclude_keys = $options['export_exclude_keys'];
132
+ if ( isset( $options['export_file_name'] ) ) $this->export_file_name = $options['export_file_name'];
133
+ if ( isset( $options['post_types'] ) ) $this->post_types = $options['post_types'];
134
+ if ( isset( $options['share_meta_key_prefix'] ) ) $this->share_meta_key_prefix = $options['share_meta_key_prefix'];
135
+ if ( isset( $options['follow_meta_key_prefix'] ) ) $this->follow_meta_key_prefix = $options['follow_meta_key_prefix'];
136
+ if ( isset( $options['share_target_sns'] ) ) $this->share_target_sns = $options['share_target_sns'];
137
+ if ( isset( $options['follow_target_sns'] ) ) $this->follow_target_sns = $options['follow_target_sns'];
138
+
139
+ add_filter( 'cron_schedules', array( $this, 'schedule_export_interval' ) );
140
+ add_action( $this->prime_cron, array( $this, 'prime_export' ) );
141
+ add_action( $this->execute_cron, array( $this, 'execute_export' ), 10, 1 );
142
+ }
143
+
144
+ /**
145
+ * Register event schedule for this engine
146
+ *
147
+ * @since 0.1.0
148
+ */
149
+ public function schedule_export_interval( $schedules ) {
150
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
151
+
152
+ $schedules[$this->event_schedule] = array(
153
+ 'interval' => $this->export_interval,
154
+ 'display' => $this->event_description
155
+ );
156
+
157
+ return $schedules;
158
+ }
159
+
160
+ /**
161
+ * Prime data export
162
+ *
163
+ * @since 0.4.0
164
+ */
165
+ public function prime_export() {
166
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
167
+
168
+ $next_exec_time = time() + $this->export_interval;
169
+
170
+ //$next_exec_time = time() + 5;
171
+
172
+ Common_Util::log( '[' . __METHOD__ . '] check_interval: ' . $this->export_interval );
173
+ Common_Util::log( '[' . __METHOD__ . '] next_exec_time: ' . $next_exec_time );
174
+
175
+ /*
176
+ if( ! WP_Cron_Util::is_scheduled_hook( $this->execute_cron ) ) {
177
+ wp_schedule_single_event( WP_Cron_Util::next_exec_time( '0 0 * * *' ), $this->execute_cron, array( Common_Util::short_hash( $next_exec_time ) ) );
178
+ }
179
+ */
180
+
181
+ wp_schedule_single_event( $next_exec_time, $this->execute_cron, array( Common_Util::short_hash( $next_exec_time ) ) );
182
+ }
183
+
184
+
185
+ /**
186
+ * Execute data export
187
+ *
188
+ * @since 0.4.0
189
+ */
190
+ public function execute_export( $hash ) {
191
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
192
+
193
+ $base_dir = WP_PLUGIN_DIR . '/sns-count-cache/data/';
194
+
195
+ $current_date = date_i18n( 'Y/m/d H:i:s' );
196
+
197
+ if( file_exists( $base_dir ) ) {
198
+ $abs_path = $base_dir . $this->export_file_name;
199
+
200
+
201
+ if ( ! file_exists( $abs_path ) ) {
202
+
203
+ if ( touch( $abs_path ) ) {
204
+ Common_Util::log( '[' . __METHOD__ . '] export file creation succeeded: ' . $abs_path );
205
+ } else {
206
+ Common_Util::log( '[' . __METHOD__ . '] export file creation failed: ' . $abs_path );
207
+ }
208
+
209
+ if ( file_exists( $abs_path ) ) {
210
+ Common_Util::log( '[' . __METHOD__ . '] file exists: ' . $abs_path );
211
+ $fp = fopen( $abs_path, 'a' );
212
+
213
+ $header = '"Retrieval Date","Post ID","Post Type","Post Title","Permalink","Post Date","SNS","Data Type","Data Value",' . "\r\n";
214
+
215
+ if ( fwrite( $fp, mb_convert_encoding( $header, "SJIS", "UTF-8" ) ) ) {
216
+ Common_Util::log( '[' . __METHOD__ . '] file write succeeded: ' . $header );
217
+ } else {
218
+ Common_Util::log( '[' . __METHOD__ . '] file wrote failed: ' . $header );
219
+ }
220
+
221
+ if ( fclose($fp) ) {
222
+ Common_Util::log( '[' . __METHOD__ . '] file close succeeded: ' . $abs_path );
223
+ } else {
224
+ Common_Util::log( '[' . __METHOD__ . '] file close failed: ' . $abs_path );
225
+ }
226
+ }
227
+ }
228
+
229
+ if ( file_exists( $abs_path ) ) {
230
+ Common_Util::log( '[' . __METHOD__ . '] file exists: ' . $abs_path );
231
+
232
+ $fp = fopen( $abs_path, 'a' );
233
+
234
+ $query_args = array(
235
+ 'post_type' => $this->post_types,
236
+ 'post_status' => 'publish',
237
+ 'nopaging' => true,
238
+ 'update_post_term_cache' => false,
239
+ 'update_post_meta_cache' => false
240
+ );
241
+
242
+ $posts_query = new WP_Query( $query_args );
243
+
244
+ if ( $posts_query->have_posts() ) {
245
+ while ( $posts_query->have_posts() ) {
246
+ $posts_query->the_post();
247
+
248
+ $post_ID = get_the_ID();
249
+
250
+ $content = '"' . $current_date . '","' . $post_ID . '","' . get_post_type( $post_ID ) . '","' . get_the_title( $post_ID ) . '","' . get_permalink( $post_ID ) . '","' . get_post_time( 'Y/m/d H:i', false, $post_ID );
251
+
252
+ foreach ( $this->share_target_sns as $key => $value ) {
253
+
254
+ if ( ! in_array( $key, $this->export_exclude_keys ) ) {
255
+
256
+ $meta_key = $this->share_meta_key_prefix . strtolower( $key );
257
+
258
+ if ( $value ) {
259
+
260
+ $data_value = get_post_meta( $post_ID, $meta_key, true );
261
+
262
+ if ( $data_value == -1 ){
263
+ $data_value = '';
264
+ }
265
+
266
+ $data = $content . '","' . $key . '","' . 'Share' . '","' . $data_value . '",' . "\r\n";
267
+ }
268
+ if ( fwrite( $fp, mb_convert_encoding( $data, "SJIS", "UTF-8" ) ) ) {
269
+ Common_Util::log( '[' . __METHOD__ . '] file write succeeded: ' . $data );
270
+ } else {
271
+ Common_Util::log( '[' . __METHOD__ . '] file wrote failed: ' . $data );
272
+ }
273
+ }
274
+ }
275
+
276
+ /*
277
+ foreach ( $this->follow_target_sns as $key => $value ) {
278
+ $meta_key = $this->follow_meta_key_prefix . strtolower( $key );
279
+
280
+ if ( $value ) {
281
+
282
+ $data_value = get_post_meta( $post_ID, $meta_key, true );
283
+
284
+ if ( $data_value == -1 ){
285
+ $data_value = '';
286
+ }
287
+
288
+ $data = $content . '","' . $key . '","' . 'Follow' . '","' . $data_value . '",' . "\r\n";
289
+ }
290
+ if ( fwrite( $fp, mb_convert_encoding( $data, "SJIS", "UTF-8" ) ) ) {
291
+ Common_Util::log( '[' . __METHOD__ . '] file write succeeded: ' . $data );
292
+ } else {
293
+ Common_Util::log( '[' . __METHOD__ . '] file wrote failed: ' . $data );
294
+ }
295
+
296
+ }
297
+ */
298
+
299
+ }
300
+ }
301
+ wp_reset_postdata();
302
+
303
+ if ( fclose($fp) ) {
304
+ Common_Util::log( '[' . __METHOD__ . '] file close succeeded: ' . $abs_path );
305
+ } else {
306
+ Common_Util::log( '[' . __METHOD__ . '] file close failed: ' . $abs_path );
307
+ }
308
+ }
309
+ }
310
+ }
311
+
312
+ /**
313
+ * Reset exported file
314
+ *
315
+ * @since 0.4.0
316
+ */
317
+ public function reset_export() {
318
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
319
+
320
+ $base_dir = WP_PLUGIN_DIR . '/sns-count-cache/data/';
321
+ $abs_path = $base_dir . $this->export_file_name;
322
+
323
+ if ( file_exists( $abs_path ) ) {
324
+ unlink( $abs_path );
325
+ }
326
+ }
327
+ }
328
+
329
+ ?>
includes/class-common-util.php CHANGED
@@ -2,7 +2,7 @@
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/
@@ -57,6 +57,76 @@ class Common_Util {
57
  }
58
  }
59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
 
61
  }
62
 
2
  /*
3
  class-common-util.php
4
 
5
+ Description: This class is a common utility
6
  Version: 0.3.0
7
  Author: Daisuke Maruyama
8
  Author URI: http://marubon.info/
57
  }
58
  }
59
 
60
+ /**
61
+ * Get short hash code
62
+ *
63
+ * @since 0.2.0
64
+ */
65
+ public static function short_hash( $data, $algo = 'CRC32' ) {
66
+ return strtr( rtrim( base64_encode( pack('H*', $algo($data) ) ), '=' ), '+/', '-_' );
67
+ }
68
+
69
+ /**
70
+ * Get file size of given file
71
+ *
72
+ * @since 0.4.0
73
+ */
74
+ public static function get_file_size( $file ) {
75
+
76
+ if ( file_exists( $file ) && is_file( $file ) ) {
77
+ $filesize = filesize( $file );
78
+ $s = array( 'B', 'KB', 'MB', 'GB', 'TB', 'PB' );
79
+ $e = floor( log( $filesize ) / log( 1024 ) );
80
+
81
+ if( $e == 0 || $e == 1 ) {
82
+ $format = '%d ';
83
+ } else {
84
+ $format = '%.1f ';
85
+ }
86
+
87
+ $filesize = sprintf( $format . $s[$e], ( $filesize / pow( 1024, floor( $e ) ) ) );
88
+
89
+ return $filesize;
90
+ } else {
91
+ return null;
92
+ }
93
+ }
94
+
95
+ /**
96
+ * Get custom post types
97
+ *
98
+ * @since 0.4.0
99
+ */
100
+ public static function get_custom_post_types() {
101
+
102
+ global $wpdb;
103
+
104
+ $custom_post_types = array();
105
+
106
+ $builtin_post_types = get_post_types( array( '_builtin' => true ) );
107
+
108
+ $exclude_post_types = "'";
109
+ $exclude_post_types .= implode( "','", $builtin_post_types );
110
+ $exclude_post_types .= "'";
111
+
112
+ $sql = 'SELECT DISTINCT post_type FROM ' . $wpdb->posts . ' WHERE post_type NOT IN ( ' . $exclude_post_types . ' )';
113
+
114
+ $results = $wpdb->get_results( $sql );
115
+
116
+ foreach ($results as $value) {
117
+ $custom_post_types[] = $value->post_type;
118
+ }
119
+
120
+ return $custom_post_types;
121
+ }
122
+
123
+ public static function extension_loaded_php_xml() {
124
+ if ( extension_loaded( 'xml' ) && extension_loaded( 'xmlreader' ) && extension_loaded( 'xmlwriter' ) ) {
125
+ return true;
126
+ } else {
127
+ return false;
128
+ }
129
+ }
130
 
131
  }
132
 
includes/class-data-cache-engine.php DELETED
@@ -1,945 +0,0 @@
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
- ?>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
includes/class-data-crawler.php CHANGED
@@ -77,20 +77,6 @@ abstract class Data_Crawler {
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
- }
94
  }
95
 
96
  ?>
77
  */
78
  abstract public function get_data( $cache_target, $url );
79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
  }
81
 
82
  ?>
includes/class-export-engine.php ADDED
@@ -0,0 +1,130 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /*
3
+ class-export-engine.php
4
+
5
+ Description: This class is a data export engine whitch exports cached data using wp-cron at regular intervals
6
+ Version: 0.4.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
+ abstract class Export_Engine {
35
+
36
+ /**
37
+ * Meta key for share second cache
38
+ */
39
+ protected $share_meta_key_prefix = NULL;
40
+
41
+ /**
42
+ * Meta key for follow second cache
43
+ */
44
+ protected $follow_meta_key_prefix = NULL;
45
+
46
+ /**
47
+ * Cron name to schedule cache processing
48
+ */
49
+ protected $prime_cron = NULL;
50
+
51
+ /**
52
+ * Cron name to execute cache processing
53
+ */
54
+ protected $execute_cron = NULL;
55
+
56
+ /**
57
+ * Schedule name for cache processing
58
+ */
59
+ protected $event_schedule = NULL;
60
+
61
+ /**
62
+ * Schedule description for cache processing
63
+ */
64
+ protected $event_description = NULL;
65
+
66
+ /**
67
+ * Instance
68
+ */
69
+ private static $instance = array();
70
+
71
+ /**
72
+ * Class constarctor
73
+ * Hook onto all of the actions and filters needed by the plugin.
74
+ *
75
+ */
76
+ protected function __construct() {
77
+ Common_Util::log('[' . __METHOD__ . '] (line='. __LINE__ . ')');
78
+ }
79
+
80
+ /**
81
+ * Get instance
82
+ *
83
+ * @since 0.1.1
84
+ */
85
+ public static function get_instance() {
86
+
87
+ $class_name = get_called_class();
88
+ if ( ! isset( self::$instance[$class_name] ) ) {
89
+ self::$instance[$class_name] = new $class_name();
90
+ //self::$instance[$class_name]->initialize($crawler, $options=array());
91
+ }
92
+
93
+ return self::$instance[$class_name];
94
+ }
95
+
96
+ /**
97
+ * Initialization
98
+ *
99
+ * @since 0.1.1
100
+ */
101
+ abstract public function initialize( $options = array() );
102
+
103
+ /**
104
+ * Register base schedule for this engine
105
+ *
106
+ * @since 0.1.0
107
+ */
108
+ public function register_schedule() {
109
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
110
+
111
+ if ( ! wp_next_scheduled( $this->prime_cron ) ) {
112
+ wp_schedule_event( time(), $this->event_schedule, $this->prime_cron );
113
+ }
114
+ }
115
+
116
+ /**
117
+ * Unregister base schedule for this engine
118
+ *
119
+ * @since 0.1.0
120
+ */
121
+ public function unregister_schedule() {
122
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
123
+
124
+ wp_clear_scheduled_hook( $this->prime_cron );
125
+ WP_Cron_Util::clear_scheduled_hook( $this->execute_cron );
126
+ }
127
+
128
+ }
129
+
130
+ ?>
includes/class-follow-base-cache-engine.php ADDED
@@ -0,0 +1,253 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /*
3
+ class-follow-base-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.4.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
+ class Follow_Base_Cache_Engine extends Cache_Engine {
35
+
36
+ /**
37
+ * Prefix of cache ID
38
+ */
39
+ const DEF_TRANSIENT_PREFIX = 'scc_follow_count_';
40
+
41
+ /**
42
+ * Cron name to schedule cache processing
43
+ */
44
+ const DEF_PRIME_CRON = 'scc_follow_basecache_prime';
45
+
46
+ /**
47
+ * Cron name to execute cache processing
48
+ */
49
+ const DEF_EXECUTE_CRON = 'scc_follow_basecache_exec';
50
+
51
+ /**
52
+ * Schedule name for cache processing
53
+ */
54
+ const DEF_EVENT_SCHEDULE = 'follow_base_cache_event';
55
+
56
+ /**
57
+ * Schedule description for cache processing
58
+ */
59
+ const DEF_EVENT_DESCRIPTION = '[SCC] Follow Base Cache Interval';
60
+
61
+
62
+ private $crawler = NULL;
63
+
64
+ /**
65
+ * Interval cheking and caching target data
66
+ */
67
+ private $check_interval = 600;
68
+
69
+ /**
70
+ * Offset suffix
71
+ */
72
+ private $offset_suffix = 'follow_base_cache_offset';
73
+
74
+ /**
75
+ * Cache target
76
+ */
77
+ private $target_sns = array();
78
+
79
+ /**
80
+ * Cache post types
81
+ */
82
+ private $post_types = array( 'post', 'page' );
83
+
84
+ /**
85
+ * Class constarctor
86
+ * Hook onto all of the actions and filters needed by the plugin.
87
+ *
88
+ */
89
+ protected function __construct() {
90
+ Common_Util::log('[' . __METHOD__ . '] (line='. __LINE__ . ')');
91
+ }
92
+
93
+ /**
94
+ * Initialization
95
+ *
96
+ * @since 0.1.1
97
+ */
98
+ public function initialize( $options = array() ) {
99
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
100
+
101
+ $this->transient_prefix = self::DEF_TRANSIENT_PREFIX;
102
+ $this->prime_cron = self::DEF_PRIME_CRON;
103
+ $this->execute_cron = self::DEF_EXECUTE_CRON;
104
+ $this->event_schedule = self::DEF_EVENT_SCHEDULE;
105
+ $this->event_description = self::DEF_EVENT_DESCRIPTION;
106
+
107
+ if ( isset( $options['crawler'] ) ) $this->crawler = $options['crawler'];
108
+ if ( isset( $options['target_sns'] ) ) $this->target_sns = $options['target_sns'];
109
+ if ( isset( $options['check_interval'] ) ) $this->check_interval = $options['check_interval'];
110
+ if ( isset( $options['posts_per_check'] ) ) $this->posts_per_check = $options['posts_per_check'];
111
+ if ( isset( $options['transient_prefix'] ) ) $this->transient_prefix = $options['transient_prefix'];
112
+ if ( isset( $options['prime_cron'] ) ) $this->prime_cron = $options['prime_cron'];
113
+ if ( isset( $options['execute_cron'] ) ) $this->execute_cron = $options['execute_cron'];
114
+ if ( isset( $options['event_schedule'] ) ) $this->event_schedule = $options['event_schedule'];
115
+ if ( isset( $options['event_description'] ) ) $this->event_description = $options['event_description'];
116
+ if ( isset( $options['post_types'] ) ) $this->post_types = $options['post_types'];
117
+
118
+ add_filter( 'cron_schedules', array( $this, 'schedule_check_interval' ) );
119
+ add_action( $this->prime_cron, array( $this, 'prime_cache' ) );
120
+ add_action( $this->execute_cron, array( $this, 'execute_cache' ), 10, 1 );
121
+
122
+ }
123
+
124
+ /**
125
+ * Register event schedule for this engine
126
+ *
127
+ * @since 0.1.0
128
+ */
129
+ public function schedule_check_interval( $schedules ) {
130
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
131
+
132
+ $schedules[$this->event_schedule] = array(
133
+ 'interval' => $this->check_interval,
134
+ 'display' => $this->event_description
135
+ );
136
+
137
+ return $schedules;
138
+ }
139
+
140
+ /**
141
+ * Schedule data retrieval and cache processing
142
+ *
143
+ * @since 0.4.0
144
+ */
145
+ public function prime_cache() {
146
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
147
+
148
+ $next_exec_time = time() + $this->check_interval;
149
+
150
+ Common_Util::log( '[' . __METHOD__ . '] check_interval: ' . $this->check_interval );
151
+ Common_Util::log( '[' . __METHOD__ . '] next_exec_time: ' . $next_exec_time );
152
+
153
+ wp_schedule_single_event( $next_exec_time, $this->execute_cron, array( Common_Util::short_hash( $next_exec_time ) ) );
154
+ }
155
+
156
+ /**
157
+ * Get and cache data of each published post and page
158
+ *
159
+ * @since 0.1.0
160
+ */
161
+ public function execute_cache( $hash ) {
162
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
163
+
164
+ Common_Util::log( '[' . __METHOD__ . '] check_interval: ' . $this->check_interval );
165
+
166
+ $cache_expiration = $this->get_cache_expiration();
167
+
168
+ Common_Util::log( '[' . __METHOD__ . '] cache_expiration: ' . $cache_expiration );
169
+
170
+ $this->cache( NULL, $this->target_sns, $cache_expiration );
171
+
172
+ //$this->prime_follow_second_cache_once();
173
+ }
174
+
175
+ /**
176
+ * Get and cache data for a given post
177
+ *
178
+ * @since 0.4.0
179
+ */
180
+ public function direct_cache() {
181
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
182
+
183
+ $cache_expiration = $this->get_cache_expiration();
184
+
185
+ Common_Util::log( '[' . __METHOD__ . '] cache_expiration: ' . $cache_expiration );
186
+
187
+ return $this->cache( $this->target_sns, $cache_expiration );
188
+ }
189
+
190
+ /**
191
+ * Get cache expiration based on current number of total post and page
192
+ *
193
+ * @since 0.4.0
194
+ */
195
+ protected function get_cache_expiration() {
196
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
197
+
198
+ return 3 * $this->check_interval;
199
+ }
200
+
201
+ /**
202
+ * Get and cache data for a given post
203
+ *
204
+ * @since 0.4.0
205
+ */
206
+ public function cache( $post_ID, $target_sns, $cache_expiration ) {
207
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
208
+
209
+ $url = get_feed_link();
210
+
211
+ Common_Util::log( '[' . __METHOD__ . '] feed: ' . $url );
212
+
213
+ $transient_ID = $this->get_transient_ID( 'follow' );
214
+
215
+ $data = $this->crawler->get_data( $target_sns, $url );
216
+
217
+ Common_Util::log( $data );
218
+
219
+ if ( $data ) {
220
+ $result = set_transient( $transient_ID, $data, $cache_expiration );
221
+
222
+ Common_Util::log( '[' . __METHOD__ . '] set_transient result: ' . $result );
223
+ }
224
+
225
+ return $data;
226
+ }
227
+
228
+ /**
229
+ * Initialize meta key for ranking
230
+ *
231
+ * @since 0.3.0
232
+ */
233
+ public function initialize_cache() {
234
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
235
+
236
+ }
237
+
238
+ /**
239
+ * Clear meta key for ranking
240
+ *
241
+ * @since 0.3.0
242
+ */
243
+ public function clear_cache() {
244
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
245
+
246
+ $transient_ID = $this->get_transient_ID( 'follow' );
247
+
248
+ delete_transient( $transient_ID );
249
+ }
250
+
251
+ }
252
+
253
+ ?>
includes/class-follow-crawler.php ADDED
@@ -0,0 +1,120 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /*
3
+ class-sns-follow-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 Follow_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
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
42
+
43
+ $this->url = rawurlencode( $url );
44
+ $this->timeout = $timeout;
45
+ }
46
+
47
+ public function set_timeout( $timeout ) {
48
+ Common_Util::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
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
60
+
61
+ $url = rawurlencode( $url );
62
+
63
+ $sns_counts = array();
64
+
65
+ if ( isset( $cache_target[SNS_Count_Cache::REF_FOLLOW_FEEDLY] ) && $cache_target[SNS_Count_Cache::REF_FOLLOW_FEEDLY] ) {
66
+ $sns_counts[SNS_Count_Cache::REF_FOLLOW_FEEDLY] = $this->get_feedly_follow( $url );
67
+ }
68
+
69
+ return $sns_counts;
70
+ }
71
+
72
+ /**
73
+ * Get share count for Twitter
74
+ *
75
+ * @since 0.1.0
76
+ */
77
+ public function get_feedly_follow( $url ) {
78
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
79
+
80
+ $query = 'http://cloud.feedly.com/v3/feeds/feed%2F' . $url;
81
+
82
+ $json = $this->remote_get( $query );
83
+
84
+ $feedly = json_decode( $json, true );
85
+
86
+ return isset( $feedly['subscribers'] ) ? intval( $feedly['subscribers'] ) : null;
87
+ }
88
+
89
+ /**
90
+ * Get content from given URL using cURL
91
+ *
92
+ * @since 0.1.0
93
+ */
94
+ private function remote_get( $url ) {
95
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
96
+
97
+ $curl = curl_init();
98
+
99
+ curl_setopt( $curl, CURLOPT_URL, $url );
100
+ curl_setopt( $curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT'] );
101
+ //curl_setopt( $curl, CURLOPT_FAILONERROR, true );
102
+ curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, true );
103
+ curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
104
+ curl_setopt( $curl, CURLOPT_TIMEOUT, $this->timeout );
105
+
106
+ $curl_results = curl_exec( $curl );
107
+
108
+ if ( curl_error( $curl ) ) {
109
+ Common_Util::log( '[' . __METHOD__ . '] curl_error: ' + curl_error( $curl ) );
110
+ die( curl_error( $curl ) );
111
+ }
112
+
113
+ curl_close( $curl );
114
+
115
+ return $curl_results;
116
+ }
117
+
118
+ }
119
+
120
+ ?>
includes/class-follow-lazy-cache-engine.php ADDED
@@ -0,0 +1,215 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /*
3
+ class-follow-lazy-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.4.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
+ class Follow_Lazy_Cache_Engine extends Cache_Engine {
35
+
36
+ /**
37
+ * Prefix of cache ID
38
+ */
39
+ const DEF_TRANSIENT_PREFIX = 'scc_follow_count_';
40
+
41
+ /**
42
+ * Cron name to schedule cache processing
43
+ */
44
+ const DEF_PRIME_CRON = 'scc_follow_lazycache_prime';
45
+
46
+ /**
47
+ * Cron name to execute cache processing
48
+ */
49
+ const DEF_EXECUTE_CRON = 'scc_follow_lazycache_exec';
50
+
51
+ /**
52
+ * Schedule name for cache processing
53
+ */
54
+ const DEF_EVENT_SCHEDULE = 'follow_lazy_cache_event';
55
+
56
+ /**
57
+ * Schedule description for cache processing
58
+ */
59
+ const DEF_EVENT_DESCRIPTION = '[SCC] Follow Lazy Cache Interval';
60
+
61
+
62
+ private $crawler = NULL;
63
+
64
+ /**
65
+ * Interval cheking and caching target data
66
+ */
67
+ private $check_interval = 600;
68
+
69
+ /**
70
+ * Latency suffix
71
+ */
72
+ private $check_latency = 10;
73
+
74
+ /**
75
+ * Cache target
76
+ */
77
+ private $target_sns = array();
78
+
79
+ /**
80
+ * Class constarctor
81
+ * Hook onto all of the actions and filters needed by the plugin.
82
+ *
83
+ */
84
+ protected function __construct() {
85
+ Common_Util::log('[' . __METHOD__ . '] (line='. __LINE__ . ')');
86
+ }
87
+
88
+ /**
89
+ * Initialization
90
+ *
91
+ * @since 0.1.1
92
+ */
93
+ public function initialize( $options = array() ) {
94
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
95
+
96
+ $this->transient_prefix = self::DEF_TRANSIENT_PREFIX;
97
+ $this->prime_cron = self::DEF_PRIME_CRON;
98
+ $this->execute_cron = self::DEF_EXECUTE_CRON;
99
+ $this->event_schedule = self::DEF_EVENT_SCHEDULE;
100
+ $this->event_description = self::DEF_EVENT_DESCRIPTION;
101
+
102
+ if ( isset( $options['crawler'] ) ) $this->crawler = $options['crawler'];
103
+ if ( isset( $options['target_sns'] ) ) $this->target_sns = $options['target_sns'];
104
+ if ( isset( $options['check_interval'] ) ) $this->check_interval = $options['check_interval'];
105
+ if ( isset( $options['transient_prefix'] ) ) $this->transient_prefix = $options['transient_prefix'];
106
+ if ( isset( $options['execute_cron'] ) ) $this->execute_cron = $options['execute_cron'];
107
+ if ( isset( $options['check_latency'] ) ) $this->check_latency = $options['check_latency'];
108
+ if ( isset( $options['cache_post_types'] ) ) $this->cache_post_types = $options['cache_post_types'];
109
+
110
+ add_action( $this->execute_cron, array( $this, 'execute_cache' ), 10, 1 );
111
+
112
+ }
113
+
114
+ /**
115
+ * Register base schedule for this engine
116
+ *
117
+ * @since 0.1.0
118
+ */
119
+ public function register_schedule() {
120
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
121
+
122
+ }
123
+
124
+ /**
125
+ * Schedule data retrieval and cache processing
126
+ *
127
+ * @since 0.4.0
128
+ */
129
+ public function prime_cache() {
130
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
131
+
132
+ $next_exec_time = time() + $this->check_latency;
133
+
134
+ Common_Util::log( '[' . __METHOD__ . '] check_latency: ' . $this->check_latency );
135
+ Common_Util::log( '[' . __METHOD__ . '] next_exec_time: ' . $next_exec_time );
136
+
137
+ wp_schedule_single_event( $next_exec_time, $this->execute_cron, array( Common_Util::short_hash( $next_exec_time ) ) );
138
+ }
139
+
140
+ /**
141
+ * Get and cache data of each published post
142
+ *
143
+ * @since 0.4.0
144
+ */
145
+ public function execute_cache( $hash ) {
146
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
147
+
148
+ $cache_expiration = $this->get_cache_expiration();
149
+
150
+ Common_Util::log( '[' . __METHOD__ . '] cache_expiration: ' . $cache_expiration );
151
+
152
+ $this->cache( NULL, $this->target_sns, $cache_expiration );
153
+ }
154
+
155
+ /**
156
+ * Get cache expiration based on current number of total post and page
157
+ *
158
+ * @since 0.4.0
159
+ */
160
+ protected function get_cache_expiration() {
161
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
162
+
163
+ return 3 * $this->check_interval;
164
+ }
165
+
166
+ /**
167
+ * Get and cache data for a given post
168
+ *
169
+ * @since 0.4.0
170
+ */
171
+ public function cache( $post_ID, $target_sns, $cache_expiration ) {
172
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
173
+
174
+ $url = get_feed_link();
175
+
176
+ Common_Util::log( '[' . __METHOD__ . '] feed: ' . $url );
177
+
178
+ $transient_ID = $this->get_transient_ID( 'follow' );
179
+
180
+ $data = $this->crawler->get_data( $target_sns, $url );
181
+
182
+ Common_Util::log( $data );
183
+
184
+ if ( $data ) {
185
+ $result = set_transient( $transient_ID, $data, $cache_expiration );
186
+
187
+ Common_Util::log( '[' . __METHOD__ . '] set_transient result: ' . $result );
188
+ }
189
+
190
+ return $data;
191
+ }
192
+
193
+ /**
194
+ * Initialize meta key for ranking
195
+ *
196
+ * @since 0.3.0
197
+ */
198
+ public function initialize_cache() {
199
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
200
+
201
+ }
202
+
203
+ /**
204
+ * Clear meta key for ranking
205
+ *
206
+ * @since 0.3.0
207
+ */
208
+ public function clear_cache() {
209
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
210
+
211
+ }
212
+
213
+ }
214
+
215
+ ?>
includes/class-follow-second-cache-engine.php ADDED
@@ -0,0 +1,298 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /*
3
+ class-follow-second-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.4.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
+ class Follow_Second_Cache_Engine extends Cache_Engine {
35
+
36
+ /**
37
+ * Prefix of cache ID
38
+ */
39
+ const DEF_TRANSIENT_PREFIX = 'scc_follow_count_';
40
+
41
+ /**
42
+ * Cron name to schedule cache processing
43
+ */
44
+ const DEF_PRIME_CRON = 'scc_follow_2ndcache_prime';
45
+
46
+ /**
47
+ * Cron name to execute cache processing
48
+ */
49
+ const DEF_EXECUTE_CRON = 'scc_follow_2ndcache_exec';
50
+
51
+ /**
52
+ * Schedule name for cache processing
53
+ */
54
+ const DEF_EVENT_SCHEDULE = 'follow_second_cache_event';
55
+
56
+ /**
57
+ * Schedule description for cache processing
58
+ */
59
+ const DEF_EVENT_DESCRIPTION = '[SCC] Follow Second Cache Interval';
60
+ /**
61
+ * Interval cheking and caching target data
62
+ */
63
+ private $check_interval = 600;
64
+
65
+ /**
66
+ * Prefix of cache ID
67
+ */
68
+ private $meta_key_prefix = 'scc_follow_count_';
69
+
70
+ /**
71
+ * Cache target
72
+ */
73
+ private $target_sns = array();
74
+
75
+ /**
76
+ * Cache post types
77
+ */
78
+ private $post_types = array( 'post', 'page' );
79
+
80
+ /**
81
+ * Class constarctor
82
+ * Hook onto all of the actions and filters needed by the plugin.
83
+ *
84
+ */
85
+ protected function __construct() {
86
+ Common_Util::log('[' . __METHOD__ . '] (line='. __LINE__ . ')');
87
+ }
88
+
89
+ /**
90
+ * Initialization
91
+ *
92
+ * @since 0.1.1
93
+ */
94
+ public function initialize( $options = array() ) {
95
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
96
+
97
+ $this->transient_prefix = self::DEF_TRANSIENT_PREFIX;
98
+ $this->prime_cron = self::DEF_PRIME_CRON;
99
+ $this->execute_cron = self::DEF_EXECUTE_CRON;
100
+ $this->event_schedule = self::DEF_EVENT_SCHEDULE;
101
+ $this->event_description = self::DEF_EVENT_DESCRIPTION;
102
+
103
+ if ( isset( $options['target_sns'] ) ) $this->target_sns = $options['target_sns'];
104
+ if ( isset( $options['check_interval'] ) ) $this->check_interval = $options['check_interval'];
105
+ if ( isset( $options['transient_prefix'] ) ) $this->transient_prefix = $options['transient_prefix'];
106
+ if ( isset( $options['prime_cron'] ) ) $this->prime_cron = $options['prime_cron'];
107
+ if ( isset( $options['execute_cron'] ) ) $this->execute_cron = $options['execute_cron'];
108
+ if ( isset( $options['event_schedule'] ) ) $this->event_schedule = $options['event_schedule'];
109
+ if ( isset( $options['event_description'] ) ) $this->event_description = $options['event_description'];
110
+ if ( isset( $options['meta_key_prefix'] ) ) $this->meta_key_prefix = $options['meta_key_prefix'];
111
+ if ( isset( $options['post_types'] ) ) $this->post_types = $options['post_types'];
112
+
113
+ add_filter( 'cron_schedules', array( $this, 'schedule_check_interval' ) );
114
+ add_action( $this->prime_cron, array( $this, 'prime_cache' ) );
115
+ add_action( $this->execute_cron, array( $this, 'execute_cache' ), 10, 0 );
116
+
117
+ }
118
+
119
+ /**
120
+ * Register event schedule for this engine
121
+ *
122
+ * @since 0.1.0
123
+ */
124
+ public function schedule_check_interval( $schedules ) {
125
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
126
+
127
+ $schedules[$this->event_schedule] = array(
128
+ 'interval' => $this->check_interval,
129
+ 'display' => $this->event_description
130
+ );
131
+
132
+ return $schedules;
133
+ }
134
+
135
+ /**
136
+ * Schedule data retrieval and cache processing
137
+ *
138
+ * @since 0.3.0
139
+ */
140
+ public function prime_cache() {
141
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
142
+
143
+ $next_exec_time = time() + $this->check_interval;
144
+
145
+ Common_Util::log( '[' . __METHOD__ . '] check_interval: ' . $this->check_interval );
146
+ Common_Util::log( '[' . __METHOD__ . '] next_exec_time: ' . $next_exec_time );
147
+
148
+ wp_schedule_single_event( $next_exec_time, $this->execute_cron );
149
+ }
150
+
151
+ /**
152
+ * Get and cache data of each published post and page
153
+ *
154
+ * @since 0.4.0
155
+ */
156
+ public function execute_cache() {
157
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
158
+
159
+ $query_args = array(
160
+ 'post_type' => $this->post_types,
161
+ 'post_status' => 'publish',
162
+ 'nopaging' => true,
163
+ 'update_post_term_cache' => false,
164
+ 'update_post_meta_cache' => false
165
+ );
166
+
167
+ $posts_query = new WP_Query( $query_args );
168
+
169
+ if ( $posts_query->have_posts() ) {
170
+ while ( $posts_query->have_posts() ) {
171
+ $posts_query->the_post();
172
+
173
+ $post_ID = get_the_ID();
174
+
175
+ $this->cache( $post_ID, $this->target_sns, 0 );
176
+
177
+ }
178
+ }
179
+ wp_reset_postdata();
180
+ }
181
+
182
+ /**
183
+ * Get and cache data for a given post
184
+ *
185
+ * @since 0.1.1
186
+ */
187
+ public function cache( $post_ID, $target_sns, $cache_expiration ) {
188
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
189
+
190
+ $transient_ID = $this->get_transient_ID( 'follow' );
191
+
192
+ if ( false !== ( $sns_followers = get_transient( $transient_ID ) ) ) {
193
+
194
+ foreach ( $target_sns as $key => $value ) {
195
+
196
+ $meta_key = $this->meta_key_prefix . strtolower( $key );
197
+
198
+ if ( $value ) {
199
+ if ( isset( $sns_followers[$key] ) && $sns_followers[$key] >= 0 ) {
200
+ Common_Util::log( '[' . __METHOD__ . '] meta_key: ' . $meta_key . ' SNS: ' . $key . ' post_ID: ' . $post_ID . ' - ' . $sns_followers[$key] );
201
+
202
+ update_post_meta($post_ID, $meta_key, $sns_followers[$key]);
203
+ //$data[$key][$post_ID] = $sns_counts[$key];
204
+ }
205
+ }
206
+ }
207
+ }
208
+
209
+ }
210
+
211
+ /**
212
+ * Get cache expiration based on current number of total post and page
213
+ *
214
+ * @since 0.2.0
215
+ */
216
+ protected function get_cache_expiration() {
217
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
218
+
219
+ return 0;
220
+ }
221
+
222
+ /**
223
+ * Initialize meta key for ranking
224
+ *
225
+ * @since 0.3.0
226
+ */
227
+ public function initialize_cache() {
228
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
229
+
230
+ $query_args = array(
231
+ 'post_type' => $this->post_types,
232
+ 'post_status' => 'publish',
233
+ 'nopaging' => true,
234
+ 'update_post_term_cache' => false,
235
+ 'update_post_meta_cache' => false
236
+ );
237
+
238
+ $posts_query = new WP_Query( $query_args );
239
+
240
+ if ( $posts_query->have_posts() ) {
241
+ while ( $posts_query->have_posts() ) {
242
+ $posts_query->the_post();
243
+
244
+ $post_ID = get_the_ID();
245
+
246
+ foreach ( $this->target_sns as $key => $value ) {
247
+
248
+ $meta_key = $this->meta_key_prefix . strtolower( $key );
249
+
250
+ if ( $value ) {
251
+ update_post_meta($post_ID, $meta_key, -1);
252
+ }
253
+ }
254
+ }
255
+ }
256
+ wp_reset_postdata();
257
+ }
258
+
259
+ /**
260
+ * Clear meta key for ranking
261
+ *
262
+ * @since 0.3.0
263
+ */
264
+ public function clear_cache() {
265
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
266
+
267
+ $query_args = array(
268
+ 'post_type' => $this->post_types,
269
+ 'post_status' => 'publish',
270
+ 'nopaging' => true,
271
+ 'update_post_term_cache' => false,
272
+ 'update_post_meta_cache' => false
273
+ );
274
+
275
+ $posts_query = new WP_Query( $query_args );
276
+
277
+ if ( $posts_query->have_posts() ) {
278
+ while ( $posts_query->have_posts() ) {
279
+ $posts_query->the_post();
280
+
281
+ $post_ID = get_the_ID();
282
+
283
+ foreach ( $this->target_sns as $key => $value ) {
284
+
285
+ $meta_key = $this->meta_key_prefix . strtolower( $key );
286
+
287
+ if ( $value ) {
288
+ delete_post_meta($post_ID, $meta_key);
289
+ }
290
+ }
291
+ }
292
+ }
293
+ wp_reset_postdata();
294
+ }
295
+
296
+ }
297
+
298
+ ?>
includes/class-share-base-cache-engine.php ADDED
@@ -0,0 +1,377 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /*
3
+ class-share-base-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.4.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
+ class Share_Base_Cache_Engine extends Cache_Engine {
35
+
36
+ /**
37
+ * Prefix of cache ID
38
+ */
39
+ const DEF_TRANSIENT_PREFIX = 'scc_share_count_';
40
+
41
+ /**
42
+ * Cron name to schedule cache processing
43
+ */
44
+ const DEF_PRIME_CRON = 'scc_share_basecache_prime';
45
+
46
+ /**
47
+ * Cron name to execute cache processing
48
+ */
49
+ const DEF_EXECUTE_CRON = 'scc_share_basecache_exec';
50
+
51
+ /**
52
+ * Schedule name for cache processing
53
+ */
54
+ const DEF_EVENT_SCHEDULE = 'share_base_cache_event';
55
+
56
+ /**
57
+ * Schedule description for cache processing
58
+ */
59
+ const DEF_EVENT_DESCRIPTION = '[SCC] Share Base Cache Interval';
60
+
61
+
62
+ private $crawler = NULL;
63
+
64
+ /**
65
+ * Interval cheking and caching target data
66
+ */
67
+ private $check_interval = 600;
68
+
69
+ /**
70
+ * Number of posts to check at a time
71
+ */
72
+ private $posts_per_check = 20;
73
+
74
+ /**
75
+ * Offset suffix
76
+ */
77
+ private $offset_suffix = 'base_offset';
78
+
79
+ /**
80
+ * Cache target
81
+ */
82
+ private $target_sns = array();
83
+
84
+ /**
85
+ * Cache post types
86
+ */
87
+ private $post_types = array( 'post', 'page' );
88
+
89
+ /**
90
+ * instance for delegation
91
+ */
92
+ private $delegate = NULL;
93
+
94
+ /**
95
+ * Class constarctor
96
+ * Hook onto all of the actions and filters needed by the plugin.
97
+ *
98
+ */
99
+ protected function __construct() {
100
+ Common_Util::log('[' . __METHOD__ . '] (line='. __LINE__ . ')');
101
+ }
102
+
103
+ /**
104
+ * Initialization
105
+ *
106
+ * @since 0.1.1
107
+ */
108
+ public function initialize( $options = array() ) {
109
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
110
+
111
+ $this->transient_prefix = self::DEF_TRANSIENT_PREFIX;
112
+ $this->prime_cron = self::DEF_PRIME_CRON;
113
+ $this->execute_cron = self::DEF_EXECUTE_CRON;
114
+ $this->event_schedule = self::DEF_EVENT_SCHEDULE;
115
+ $this->event_description = self::DEF_EVENT_DESCRIPTION;
116
+
117
+ if ( isset( $options['delegate'] ) ) $this->delegate = $options['delegate'];
118
+ if ( isset( $options['crawler'] ) ) $this->crawler = $options['crawler'];
119
+ if ( isset( $options['target_sns'] ) ) $this->target_sns = $options['target_sns'];
120
+ if ( isset( $options['check_interval'] ) ) $this->check_interval = $options['check_interval'];
121
+ if ( isset( $options['posts_per_check'] ) ) $this->posts_per_check = $options['posts_per_check'];
122
+ if ( isset( $options['transient_prefix'] ) ) $this->transient_prefix = $options['transient_prefix'];
123
+ if ( isset( $options['prime_cron'] ) ) $this->prime_cron = $options['prime_cron'];
124
+ if ( isset( $options['execute_cron'] ) ) $this->execute_cron = $options['execute_cron'];
125
+ if ( isset( $options['event_schedule'] ) ) $this->event_schedule = $options['event_schedule'];
126
+ if ( isset( $options['event_description'] ) ) $this->event_description = $options['event_description'];
127
+ if ( isset( $options['post_types'] ) ) $this->post_types = $options['post_types'];
128
+
129
+ add_filter( 'cron_schedules', array( $this, 'schedule_check_interval' ) );
130
+ add_action( $this->prime_cron, array( $this, 'prime_cache' ) );
131
+ add_action( $this->execute_cron, array( $this, 'execute_cache' ), 10, 1 );
132
+
133
+ }
134
+
135
+ /**
136
+ * Register event schedule for this engine
137
+ *
138
+ * @since 0.1.0
139
+ */
140
+ public function schedule_check_interval( $schedules ) {
141
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
142
+
143
+ $schedules[$this->event_schedule] = array(
144
+ 'interval' => $this->check_interval,
145
+ 'display' => $this->event_description
146
+ );
147
+
148
+ return $schedules;
149
+ }
150
+
151
+ /**
152
+ * Schedule data retrieval and cache processing
153
+ *
154
+ * @since 0.1.0
155
+ */
156
+ public function prime_cache() {
157
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
158
+
159
+ $next_exec_time = time() + $this->check_interval;
160
+ $posts_total = $this->get_posts_total();
161
+
162
+ Common_Util::log( '[' . __METHOD__ . '] check_interval: ' . $this->check_interval );
163
+ Common_Util::log( '[' . __METHOD__ . '] posts_per_check: ' . $this->posts_per_check );
164
+ Common_Util::log( '[' . __METHOD__ . '] next_exec_time: ' . $next_exec_time );
165
+ Common_Util::log( '[' . __METHOD__ . '] posts_total: ' . $posts_total );
166
+
167
+ $transient_ID = $this->get_transient_ID( $this->offset_suffix );
168
+
169
+ /*
170
+ if ( false === ( $posts_offset = get_transient( $transient_ID ) ) ) {
171
+ $posts_offset = 0;
172
+ }
173
+ */
174
+
175
+ if ( false === ( $posts_offset = get_option( $transient_ID ) ) ) {
176
+ $posts_offset = 0;
177
+ }
178
+
179
+ Common_Util::log( '[' . __METHOD__ . '] posts_offset: ' . $posts_offset );
180
+
181
+ wp_schedule_single_event( $next_exec_time, $this->execute_cron, array( $posts_offset ) );
182
+
183
+ $posts_offset = $posts_offset + $this->posts_per_check;
184
+
185
+ if ( $posts_offset > $posts_total ) {
186
+ $posts_offset = 0;
187
+ }
188
+
189
+ //set_transient( $transient_ID, $posts_offset, $this->check_interval + $this->check_interval );
190
+ update_option( $transient_ID, $posts_offset );
191
+ }
192
+
193
+ /**
194
+ * Get and cache data of each published post and page
195
+ *
196
+ * @since 0.1.0
197
+ */
198
+ public function execute_cache( $posts_offset ) {
199
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
200
+
201
+ Common_Util::log( '[' . __METHOD__ . '] posts_offset: ' . $posts_offset );
202
+ Common_Util::log( '[' . __METHOD__ . '] check_interval: ' . $this->check_interval );
203
+ Common_Util::log( '[' . __METHOD__ . '] posts_per_check: ' . $this->posts_per_check );
204
+
205
+ $cache_expiration = $this->get_cache_expiration();
206
+
207
+ Common_Util::log( '[' . __METHOD__ . '] cache_expiration: ' . $cache_expiration );
208
+
209
+ $query_args = array(
210
+ 'post_type' => $this->post_types,
211
+ 'post_status' => 'publish',
212
+ 'offset' => $posts_offset,
213
+ 'posts_per_page' => $this->posts_per_check,
214
+ 'no_found_rows' => true,
215
+ 'update_post_term_cache' => false,
216
+ 'update_post_meta_cache' => false
217
+ );
218
+
219
+ $posts_query = new WP_Query( $query_args );
220
+
221
+ if ( $posts_query->have_posts() ) {
222
+ while ( $posts_query->have_posts() ) {
223
+ $posts_query->the_post();
224
+
225
+ $post_ID = get_the_ID();
226
+
227
+ Common_Util::log( '[' . __METHOD__ . '] post_id: ' . $post_ID );
228
+
229
+ $this->cache( $post_ID, $this->target_sns, $cache_expiration );
230
+
231
+ if ( ! is_null( $this->delegate ) && method_exists( $this->delegate, 'order_cache' ) ) {
232
+ $options = array( 'post_id' => $post_ID );
233
+ $this->delegate->order_cache( $this, $options );
234
+ }
235
+ }
236
+ }
237
+ wp_reset_postdata();
238
+
239
+ }
240
+
241
+ /**
242
+ * Get and cache data for a given post
243
+ *
244
+ * @since 0.1.1
245
+ */
246
+ public function direct_cache( $post_ID, $second_sync = false ) {
247
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
248
+
249
+ $cache_expiration = $this->get_cache_expiration();
250
+
251
+ Common_Util::log( '[' . __METHOD__ . '] cache_expiration: ' . $cache_expiration );
252
+
253
+ $result = $this->cache( $post_ID, $this->target_sns, $cache_expiration );
254
+
255
+ if ( $second_sync ) {
256
+ if ( ! is_null( $this->delegate ) && method_exists( $this->delegate, 'order_cache' ) ) {
257
+ $options = array( 'post_id' => $post_ID );
258
+ $this->delegate->order_cache( $this, $options );
259
+ }
260
+ }
261
+
262
+ return $result;
263
+ }
264
+
265
+ /**
266
+ * Get cache expiration based on current number of total post and page
267
+ *
268
+ * @since 0.1.1
269
+ */
270
+ protected function get_cache_expiration() {
271
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
272
+
273
+ $posts_total = $this->get_posts_total();
274
+
275
+ Common_Util::log( '[' . __METHOD__ . '] posts_total: ' . $posts_total );
276
+
277
+ return ceil( $posts_total / $this->posts_per_check ) * $this->check_interval * 3;
278
+ }
279
+
280
+ /**
281
+ * Get and cache data for a given post
282
+ *
283
+ * @since 0.1.1
284
+ */
285
+ public function cache( $post_ID, $target_sns, $cache_expiration ) {
286
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
287
+
288
+ Common_Util::log( '[' . __METHOD__ . '] post_id: ' . $post_ID );
289
+
290
+ $transient_ID = $this->get_transient_ID( $post_ID );
291
+
292
+ $url = get_permalink( $post_ID );
293
+
294
+ $data = $this->crawler->get_data( $target_sns, $url );
295
+
296
+ Common_Util::log( $data );
297
+
298
+ if ( $data ) {
299
+ $result = set_transient( $transient_ID, $data, $cache_expiration );
300
+
301
+ Common_Util::log( '[' . __METHOD__ . '] set_transient result: ' . $result );
302
+ }
303
+
304
+ return $data;
305
+ }
306
+
307
+ /**
308
+ * Initialize meta key for ranking
309
+ *
310
+ * @since 0.3.0
311
+ */
312
+ public function initialize_cache() {
313
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
314
+
315
+ }
316
+
317
+ /**
318
+ * Clear meta key for ranking
319
+ *
320
+ * @since 0.3.0
321
+ */
322
+ public function clear_cache() {
323
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
324
+
325
+ $query_args = array(
326
+ 'post_type' => $this->post_types,
327
+ 'post_status' => 'publish',
328
+ 'nopaging' => true,
329
+ 'update_post_term_cache' => false,
330
+ 'update_post_meta_cache' => false
331
+ );
332
+
333
+ $posts_query = new WP_Query( $query_args );
334
+
335
+ if ( $posts_query->have_posts() ) {
336
+ while ( $posts_query->have_posts() ) {
337
+ $posts_query->the_post();
338
+
339
+ $post_ID = get_the_ID();
340
+
341
+ $transient_ID = $this->get_transient_ID( $post_ID );
342
+
343
+ delete_transient( $transient_ID );
344
+ }
345
+ }
346
+ wp_reset_postdata();
347
+
348
+ $transient_ID = $this->get_transient_ID( $this->offset_suffix );
349
+
350
+ delete_option( $transient_ID );
351
+
352
+ }
353
+
354
+ /**
355
+ * Get total count of current published post and page
356
+ *
357
+ * @since 0.1.0
358
+ */
359
+ private function get_posts_total() {
360
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
361
+
362
+ $query_args = array(
363
+ 'post_type' => $this->post_types,
364
+ 'post_status' => 'publish',
365
+ 'nopaging' => true,
366
+ 'update_post_term_cache' => false,
367
+ 'update_post_meta_cache' => false
368
+ );
369
+
370
+ $posts_query = new WP_Query( $query_args );
371
+
372
+ return $posts_query->found_posts;
373
+ }
374
+
375
+ }
376
+
377
+ ?>
includes/class-share-crawler.php ADDED
@@ -0,0 +1,277 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 Share_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
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
42
+
43
+ $this->url = rawurlencode( $url );
44
+ $this->timeout = $timeout;
45
+ }
46
+
47
+ public function set_timeout( $timeout ) {
48
+ Common_Util::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
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
60
+
61
+ $url = rawurlencode( $url );
62
+
63
+ $sns_counts = array();
64
+
65
+ if ( isset( $cache_target[SNS_Count_Cache::REF_SHARE_HATEBU] ) && $cache_target[SNS_Count_Cache::REF_SHARE_HATEBU] ) {
66
+ $sns_counts[SNS_Count_Cache::REF_SHARE_HATEBU] = $this->get_hatebu_count( $url );
67
+ }
68
+
69
+ if ( isset( $cache_target[SNS_Count_Cache::REF_SHARE_TWITTER] ) && $cache_target[SNS_Count_Cache::REF_SHARE_TWITTER] ) {
70
+ $sns_counts[SNS_Count_Cache::REF_SHARE_TWITTER] = $this->get_twitter_count( $url );
71
+ }
72
+
73
+ if ( isset( $cache_target[SNS_Count_Cache::REF_SHARE_FACEBOOK] ) && $cache_target[SNS_Count_Cache::REF_SHARE_FACEBOOK] ) {
74
+ if ( Common_Util::extension_loaded_php_xml() ) {
75
+ $sns_counts[SNS_Count_Cache::REF_SHARE_FACEBOOK] = $this->get_facebook_count( $url );
76
+ } else {
77
+ Common_Util::log( '[' . __METHOD__ . '] php-xml is not installed.' );
78
+ }
79
+
80
+ }
81
+
82
+ if ( isset( $cache_target[SNS_Count_Cache::REF_SHARE_GPLUS] ) && $cache_target[SNS_Count_Cache::REF_SHARE_GPLUS] ) {
83
+ $sns_counts[SNS_Count_Cache::REF_SHARE_GPLUS] = $this->get_gplus_count( $url );
84
+ }
85
+
86
+ if ( isset( $cache_target[SNS_Count_Cache::REF_SHARE_POCKET] ) && $cache_target[SNS_Count_Cache::REF_SHARE_POCKET] ) {
87
+ if ( Common_Util::extension_loaded_php_xml() ) {
88
+ $sns_counts[SNS_Count_Cache::REF_SHARE_POCKET] = $this->get_pocket_count( $url );
89
+ } else {
90
+ Common_Util::log( '[' . __METHOD__ . '] php-xml is not installed.' );
91
+ }
92
+ }
93
+
94
+ if ( isset( $cache_target[SNS_Count_Cache::REF_SHARE_TOTAL] ) && $cache_target[SNS_Count_Cache::REF_SHARE_TOTAL] ) {
95
+
96
+ $total = 0;
97
+
98
+ foreach ( $sns_counts as $key => $value ) {
99
+ if ( isset( $value) && $value >= 0 ) {
100
+ $total = $total + $value;
101
+ }
102
+ }
103
+
104
+ $sns_counts[SNS_Count_Cache::REF_SHARE_TOTAL] = $total;
105
+ }
106
+
107
+ if ( isset( $cache_target[SNS_Count_Cache::REF_CRAWL_DATE] ) && $cache_target[SNS_Count_Cache::REF_CRAWL_DATE] ) {
108
+ $sns_counts[SNS_Count_Cache::REF_CRAWL_DATE] = date_i18n( 'Y/m/d H:i:s' );
109
+ }
110
+
111
+ return $sns_counts;
112
+ }
113
+
114
+ /**
115
+ * Get share count for Hatena Bookmark
116
+ *
117
+ * @since 0.1.0
118
+ */
119
+ public function get_hatebu_count( $url ) {
120
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
121
+
122
+ $query = 'http://api.b.st-hatena.com/entry.count?url=' . $url;
123
+
124
+ $hatebu = $this->remote_get( $query );
125
+
126
+ return isset( $hatebu ) ? intval($hatebu) : -1;
127
+ }
128
+
129
+ /**
130
+ * Get share count for Twitter
131
+ *
132
+ * @since 0.1.0
133
+ */
134
+ public function get_twitter_count( $url ) {
135
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
136
+
137
+ $query = 'http://urls.api.twitter.com/1/urls/count.json?url=' . $url;
138
+
139
+ $json = $this->remote_get( $query );
140
+
141
+ $twitter = json_decode( $json, true );
142
+
143
+ return isset( $twitter['count'] ) ? intval( $twitter['count'] ) : -1;
144
+ }
145
+
146
+ /**
147
+ * Get share count for Facebook
148
+ *
149
+ * @since 0.1.0
150
+ */
151
+ public function get_facebook_count( $url ) {
152
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
153
+
154
+ /*
155
+ $query = 'http://graph.facebook.com/' . $url;
156
+
157
+ $json = $this->remote_get( $query );
158
+
159
+ $facebook = json_decode( $json, true );
160
+
161
+ return isset( $facebook['shares'] ) ? intval( $facebook['shares'] ) : 0;
162
+ */
163
+
164
+ $query = 'http://www.facebook.com/plugins/like.php?href=' . $url . '&width&layout=standard&action=like&show_faces=false&share=false&height=35&locale=ja_JP';
165
+
166
+ Common_Util::log( '[' . __METHOD__ . '] facebookquery: ' . $query );
167
+
168
+ $html = $this->remote_get( $query );
169
+
170
+ //$html = wp_remote_get( $query );
171
+
172
+ //Common_Util::log( $html );
173
+ //Common_Util::log( '[' . __METHOD__ . '] facebookhtml: ' . $html['body'] );
174
+
175
+ $dom = new DOMDocument( '1.0', 'UTF-8' );
176
+ $dom->preserveWhiteSpace = false;
177
+ $dom->formatOutput = true;
178
+
179
+ $dom->loadHTML( $html );
180
+ //$dom->loadXml( $html );
181
+
182
+ //$dom->loadHTML( $html['body'] );
183
+
184
+ $xpath = new DOMXPath( $dom );
185
+
186
+ $result = $xpath->query( '//*[@id="u_0_2"]' )->item(0);
187
+
188
+ Common_Util::log( '[' . __METHOD__ . '] count element: ' . $result->nodeValue );
189
+
190
+ if ( preg_match( '/([0-9.]+)/', $result->nodeValue, $match ) ) {
191
+ $count = str_replace( ',', '', $match[1] );
192
+ Common_Util::log( '[' . __METHOD__ . '] count: ' . $count );
193
+ } else {
194
+ $count = 0;
195
+ }
196
+
197
+ return isset( $count ) ? intval( $count ) : -1;
198
+
199
+ }
200
+
201
+ /**
202
+ * Get share count for Google Plus
203
+ *
204
+ * @since 0.1.0
205
+ */
206
+ public function get_gplus_count( $url ) {
207
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
208
+
209
+ $query = 'https://apis.google.com/_/+1/fastbutton?url=' . $url;
210
+
211
+ $html = $this->remote_get( $query );
212
+
213
+ preg_match( '/\[2,([0-9.]+),\[/', $html, $count );
214
+
215
+ return isset( $count[1] ) ? intval( $count[1] ) : -1;
216
+ }
217
+
218
+ /**
219
+ * Get share count for Pocket
220
+ *
221
+ * @since 0.1.0
222
+ */
223
+ public function get_pocket_count( $url ) {
224
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
225
+
226
+ $query = 'http://widgets.getpocket.com/v1/button?v=1&count=horizontal&url=' . $url;
227
+ $html = $this->remote_get( $query );
228
+
229
+ $dom = new DOMDocument( '1.0', 'UTF-8' );
230
+ $dom->preserveWhiteSpace = false;
231
+ $dom->formatOutput = true;
232
+ $dom->loadXml( $html );
233
+
234
+ $xpath = new DOMXPath( $dom );
235
+
236
+ $result = $xpath->query( '//em[@id = "cnt"]' )->item(0);
237
+
238
+ Common_Util::log( '[' . __METHOD__ . '] count: ' . $result->nodeValue );
239
+
240
+ return isset( $result->nodeValue ) ? intval( $result->nodeValue ) : -1;
241
+ }
242
+
243
+ /**
244
+ * Get content from given URL using cURL
245
+ *
246
+ * @since 0.1.0
247
+ */
248
+ private function remote_get( $url ) {
249
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
250
+
251
+ global $wp_version;
252
+
253
+ $curl = curl_init();
254
+
255
+ curl_setopt( $curl, CURLOPT_URL, $url );
256
+ //curl_setopt( $curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT'] );
257
+ curl_setopt( $curl, CURLOPT_USERAGENT, 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' ) );
258
+ //curl_setopt( $curl, CURLOPT_FAILONERROR, true );
259
+ curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, true );
260
+ curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
261
+ curl_setopt( $curl, CURLOPT_TIMEOUT, $this->timeout );
262
+
263
+ $curl_results = curl_exec( $curl );
264
+
265
+ if ( curl_error( $curl ) ) {
266
+ Common_Util::log( '[' . __METHOD__ . '] curl_error: ' + curl_error( $curl ) );
267
+ die( curl_error( $curl ) );
268
+ }
269
+
270
+ curl_close( $curl );
271
+
272
+ return $curl_results;
273
+ }
274
+
275
+ }
276
+
277
+ ?>
includes/class-share-lazy-cache-engine.php ADDED
@@ -0,0 +1,273 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /*
3
+ class-share-lazy-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.4.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
+ class Share_Lazy_Cache_Engine extends Cache_Engine {
35
+
36
+ /**
37
+ * Prefix of cache ID
38
+ */
39
+ const DEF_TRANSIENT_PREFIX = 'scc_share_count_';
40
+
41
+ /**
42
+ * Cron name to schedule cache processing
43
+ */
44
+ const DEF_PRIME_CRON = 'scc_share_lazycache_prime';
45
+
46
+ /**
47
+ * Cron name to execute cache processing
48
+ */
49
+ const DEF_EXECUTE_CRON = 'scc_share_lazycache_exec';
50
+
51
+ /**
52
+ * Schedule name for cache processing
53
+ */
54
+ const DEF_EVENT_SCHEDULE = 'share_lazy_cache_event';
55
+
56
+ /**
57
+ * Schedule description for cache processing
58
+ */
59
+ const DEF_EVENT_DESCRIPTION = '[SCC] Share Lazy Cache Interval';
60
+
61
+
62
+ private $crawler = NULL;
63
+
64
+ /**
65
+ * Interval cheking and caching target data
66
+ */
67
+ private $check_interval = 600;
68
+
69
+ /**
70
+ * Number of posts to check at a time
71
+ */
72
+ private $posts_per_check = 20;
73
+
74
+ /**
75
+ * Latency suffix
76
+ */
77
+ private $check_latency = 10;
78
+
79
+ /**
80
+ * Cache target
81
+ */
82
+ private $target_sns = array();
83
+
84
+ /**
85
+ * Cache post types
86
+ */
87
+ private $post_types = array( 'post', 'page' );
88
+
89
+ /**
90
+ * instance for delegation
91
+ */
92
+ private $delegate = NULL;
93
+
94
+ /**
95
+ * Class constarctor
96
+ * Hook onto all of the actions and filters needed by the plugin.
97
+ *
98
+ */
99
+ protected function __construct() {
100
+ Common_Util::log('[' . __METHOD__ . '] (line='. __LINE__ . ')');
101
+ }
102
+
103
+ /**
104
+ * Initialization
105
+ *
106
+ * @since 0.1.1
107
+ */
108
+ public function initialize( $options = array() ) {
109
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
110
+
111
+ $this->transient_prefix = self::DEF_TRANSIENT_PREFIX;
112
+ $this->prime_cron = self::DEF_PRIME_CRON;
113
+ $this->execute_cron = self::DEF_EXECUTE_CRON;
114
+ $this->event_schedule = self::DEF_EVENT_SCHEDULE;
115
+ $this->event_description = self::DEF_EVENT_DESCRIPTION;
116
+
117
+ if ( isset( $options['delegate'] ) ) $this->delegate = $options['delegate'];
118
+ if ( isset( $options['crawler'] ) ) $this->crawler = $options['crawler'];
119
+ if ( isset( $options['target_sns'] ) ) $this->target_sns = $options['target_sns'];
120
+ if ( isset( $options['check_interval'] ) ) $this->check_interval = $options['check_interval'];
121
+ if ( isset( $options['posts_per_check'] ) ) $this->posts_per_check = $options['posts_per_check'];
122
+ if ( isset( $options['transient_prefix'] ) ) $this->transient_prefix = $options['transient_prefix'];
123
+ if ( isset( $options['execute_cron'] ) ) $this->execute_cron = $options['execute_cron'];
124
+ if ( isset( $options['check_latency'] ) ) $this->check_latency = $options['check_latency'];
125
+ if ( isset( $options['post_types'] ) ) $this->post_types = $options['post_types'];
126
+
127
+ add_action( $this->execute_cron, array( $this, 'execute_cache' ), 10, 1 );
128
+
129
+ }
130
+
131
+ /**
132
+ * Register base schedule for this engine
133
+ *
134
+ * @since 0.1.0
135
+ */
136
+ public function register_schedule() {
137
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
138
+
139
+ }
140
+
141
+ /**
142
+ * Unregister base schedule for this engine
143
+ *
144
+ * @since 0.1.0
145
+ */
146
+ public function unregister_schedule() {
147
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
148
+
149
+ WP_Cron_Util::clear_scheduled_hook( $this->execute_cron );
150
+ }
151
+
152
+ /**
153
+ * Schedule data retrieval and cache processing
154
+ *
155
+ * @since 0.2.0
156
+ */
157
+ public function prime_cache( $post_ID ) {
158
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
159
+
160
+ $next_exec_time = time() + $this->check_latency;
161
+
162
+ Common_Util::log( '[' . __METHOD__ . '] check_latency: ' . $this->check_latency );
163
+ Common_Util::log( '[' . __METHOD__ . '] next_exec_time: ' . $next_exec_time );
164
+
165
+ wp_schedule_single_event( $next_exec_time, $this->execute_cron, array( $post_ID ) );
166
+ }
167
+
168
+ /**
169
+ * Get and cache data of each published post
170
+ *
171
+ * @since 0.2.0
172
+ */
173
+ public function execute_cache( $post_ID ) {
174
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
175
+
176
+ $cache_expiration = $this->get_cache_expiration();
177
+
178
+ Common_Util::log( '[' . __METHOD__ . '] cache_expiration: ' . $cache_expiration );
179
+
180
+ $this->cache( $post_ID, $this->target_sns, $cache_expiration );
181
+
182
+ if ( ! is_null( $this->delegate ) && method_exists( $this->delegate, 'order_cache' ) ) {
183
+ $options = array( 'post_id' => $post_ID );
184
+ $this->delegate->order_cache( $this, $options );
185
+ }
186
+ }
187
+
188
+ /**
189
+ * Get cache expiration based on current number of total post and page
190
+ *
191
+ * @since 0.1.1
192
+ */
193
+ protected function get_cache_expiration() {
194
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
195
+
196
+ $posts_total = $this->get_posts_total();
197
+
198
+ Common_Util::log( '[' . __METHOD__ . '] posts_total: ' . $posts_total );
199
+
200
+ return ceil( $posts_total / $this->posts_per_check ) * $this->check_interval * 3;
201
+ }
202
+
203
+ /**
204
+ * Get total count of current published post and page
205
+ *
206
+ * @since 0.1.0
207
+ */
208
+ private function get_posts_total() {
209
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
210
+
211
+ $query_args = array(
212
+ 'post_type' => $this->post_types,
213
+ 'post_status' => 'publish',
214
+ 'nopaging' => true,
215
+ 'update_post_term_cache' => false,
216
+ 'update_post_meta_cache' => false
217
+ );
218
+
219
+ $posts_query = new WP_Query( $query_args );
220
+
221
+ return $posts_query->found_posts;
222
+ }
223
+
224
+ /**
225
+ * Get and cache data for a given post
226
+ *
227
+ * @since 0.1.1
228
+ */
229
+ public function cache( $post_ID, $target_sns, $cache_expiration ) {
230
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
231
+
232
+ Common_Util::log( '[' . __METHOD__ . '] post_id: ' . $post_ID );
233
+
234
+ $transient_ID = $this->get_transient_ID( $post_ID );
235
+
236
+ $url = get_permalink( $post_ID );
237
+
238
+ $data = $this->crawler->get_data( $target_sns, $url );
239
+
240
+ Common_Util::log( $data );
241
+
242
+ if ( $data ) {
243
+ $result = set_transient( $transient_ID, $data, $cache_expiration );
244
+
245
+ Common_Util::log( '[' . __METHOD__ . '] set_transient result: ' . $result );
246
+ }
247
+
248
+ return $data;
249
+ }
250
+
251
+ /**
252
+ * Initialize meta key for ranking
253
+ *
254
+ * @since 0.3.0
255
+ */
256
+ public function initialize_cache() {
257
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
258
+
259
+ }
260
+
261
+ /**
262
+ * Clear meta key for ranking
263
+ *
264
+ * @since 0.3.0
265
+ */
266
+ public function clear_cache() {
267
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
268
+
269
+ }
270
+
271
+ }
272
+
273
+ ?>
includes/class-share-rescue-cache-engine.php ADDED
@@ -0,0 +1,395 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /*
3
+ class-share-rescue-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.4.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
+ class Share_Rescue_Cache_Engine extends Cache_Engine {
35
+
36
+ /**
37
+ * Prefix of cache ID
38
+ */
39
+ const DEF_TRANSIENT_PREFIX = 'scc_share_count_';
40
+
41
+ /**
42
+ * Cron name to schedule cache processing
43
+ */
44
+ const DEF_PRIME_CRON = 'scc_share_rescuecache_prime';
45
+
46
+ /**
47
+ * Cron name to execute cache processing
48
+ */
49
+ const DEF_EXECUTE_CRON = 'scc_share_rescuecache_exec';
50
+
51
+ /**
52
+ * Schedule name for cache processing
53
+ */
54
+ const DEF_EVENT_SCHEDULE = 'share_rescue_cache_event';
55
+
56
+ /**
57
+ * Schedule description for cache processing
58
+ */
59
+ const DEF_EVENT_DESCRIPTION = '[SCC] Share Rescue Cache Interval';
60
+
61
+
62
+ private $crawler = NULL;
63
+
64
+ /**
65
+ * Interval cheking and caching target data
66
+ */
67
+ private $check_interval = 600;
68
+
69
+ /**
70
+ * Number of posts to check at a time
71
+ */
72
+ private $posts_per_check = 20;
73
+
74
+ /**
75
+ * Cache target
76
+ */
77
+ private $target_sns = array();
78
+
79
+ /**
80
+ * Prefix of cache ID
81
+ */
82
+ private $meta_key_prefix = 'scc_share_count_';
83
+
84
+ /**
85
+ * Cache post types
86
+ */
87
+ private $post_types = array( 'post', 'page' );
88
+
89
+ /**
90
+ * instance for delegation
91
+ */
92
+ private $delegate = NULL;
93
+
94
+ /**
95
+ * Class constarctor
96
+ * Hook onto all of the actions and filters needed by the plugin.
97
+ *
98
+ */
99
+ protected function __construct() {
100
+ Common_Util::log('[' . __METHOD__ . '] (line='. __LINE__ . ')');
101
+ }
102
+
103
+ /**
104
+ * Initialization
105
+ *
106
+ * @since 0.1.1
107
+ */
108
+ public function initialize( $options = array() ) {
109
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
110
+
111
+ $this->transient_prefix = self::DEF_TRANSIENT_PREFIX;
112
+ $this->prime_cron = self::DEF_PRIME_CRON;
113
+ $this->execute_cron = self::DEF_EXECUTE_CRON;
114
+ $this->event_schedule = self::DEF_EVENT_SCHEDULE;
115
+ $this->event_description = self::DEF_EVENT_DESCRIPTION;
116
+
117
+ if ( isset( $options['delegate'] ) ) $this->delegate = $options['delegate'];
118
+ if ( isset( $options['crawler'] ) ) $this->crawler = $options['crawler'];
119
+ if ( isset( $options['target_sns'] ) ) $this->target_sns = $options['target_sns'];
120
+ if ( isset( $options['check_interval'] ) ) $this->check_interval = $options['check_interval'];
121
+ if ( isset( $options['posts_per_check'] ) ) $this->posts_per_check = $options['posts_per_check'];
122
+ if ( isset( $options['transient_prefix'] ) ) $this->transient_prefix = $options['transient_prefix'];
123
+ if ( isset( $options['prime_cron'] ) ) $this->prime_cron = $options['prime_cron'];
124
+ if ( isset( $options['execute_cron'] ) ) $this->execute_cron = $options['execute_cron'];
125
+ if ( isset( $options['event_schedule'] ) ) $this->event_schedule = $options['event_schedule'];
126
+ if ( isset( $options['event_description'] ) ) $this->event_description = $options['event_description'];
127
+ if ( isset( $options['post_types'] ) ) $this->post_types = $options['post_types'];
128
+ if ( isset( $options['meta_key_prefix'] ) ) $this->meta_key_prefix = $options['meta_key_prefix'];
129
+
130
+ add_filter( 'cron_schedules', array( $this, 'schedule_check_interval' ) );
131
+ add_action( $this->prime_cron, array( $this, 'prime_cache' ) );
132
+ add_action( $this->execute_cron, array( $this, 'execute_cache' ), 10, 1 );
133
+
134
+ }
135
+
136
+ /**
137
+ * Register event schedule for this engine
138
+ *
139
+ * @since 0.1.0
140
+ */
141
+ public function schedule_check_interval( $schedules ) {
142
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
143
+
144
+ $schedules[$this->event_schedule] = array(
145
+ 'interval' => $this->check_interval,
146
+ 'display' => $this->event_description
147
+ );
148
+
149
+ return $schedules;
150
+ }
151
+
152
+ /**
153
+ * Schedule data retrieval and cache processing
154
+ *
155
+ * @since 0.1.0
156
+ */
157
+ public function prime_cache() {
158
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
159
+
160
+ $next_exec_time = time() + $this->check_interval;
161
+ $posts_total = $this->get_posts_total();
162
+
163
+ Common_Util::log( '[' . __METHOD__ . '] check_interval: ' . $this->check_interval );
164
+ Common_Util::log( '[' . __METHOD__ . '] posts_per_check: ' . $this->posts_per_check );
165
+ Common_Util::log( '[' . __METHOD__ . '] next_exec_time: ' . $next_exec_time );
166
+ Common_Util::log( '[' . __METHOD__ . '] posts_total: ' . $posts_total );
167
+
168
+ wp_schedule_single_event( $next_exec_time, $this->execute_cron, array( Common_Util::short_hash( $next_exec_time ) ) );
169
+
170
+ }
171
+
172
+ /**
173
+ * Get and cache data of each published post and page
174
+ *
175
+ * @since 0.1.0
176
+ */
177
+ public function execute_cache( $hash ) {
178
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
179
+
180
+ Common_Util::log( '[' . __METHOD__ . '] posts_offset: ' . $posts_offset );
181
+ Common_Util::log( '[' . __METHOD__ . '] check_interval: ' . $this->check_interval );
182
+ Common_Util::log( '[' . __METHOD__ . '] posts_per_check: ' . $this->posts_per_check );
183
+
184
+ $no_cache_post_IDs = array();
185
+
186
+ $cache_expiration = $this->get_cache_expiration();
187
+
188
+ Common_Util::log( '[' . __METHOD__ . '] cache_expiration: ' . $cache_expiration );
189
+
190
+ $check_range_min = 0;
191
+ $check_range_max = 0;
192
+
193
+ $crons = WP_Cron_Util::get_scheduled_hook( 'scc_share_basecache_exec' );
194
+
195
+ foreach ( $crons as $key => $cron ) {
196
+ $hook = $cron['hook'];
197
+ $timestamp = $cron['timestamp'];
198
+ $offset = $cron['args'][0];
199
+
200
+ Common_Util::log( '[' . __METHOD__ . '] hook: ' . $hook . ' offset: ' . $offset . ' timestamp: ' . $timestamp );
201
+
202
+ //if ( time() + 300 < $timestamp ) {
203
+ $check_range_min = $offset;
204
+ $check_range_max = $offset + $this->posts_per_check;
205
+ //}
206
+ }
207
+
208
+ $query_args = array(
209
+ 'post_type' => $this->post_types,
210
+ 'post_status' => 'publish',
211
+ 'nopaging' => true,
212
+ 'update_post_term_cache' => false,
213
+ 'update_post_meta_cache' => false
214
+ );
215
+
216
+ $posts_query = new WP_Query( $query_args );
217
+
218
+ if ( $posts_query->have_posts() ) {
219
+ while ( $posts_query->have_posts() ) {
220
+ $posts_query->the_post();
221
+
222
+ $post_ID = get_the_ID();
223
+
224
+ $full_cache_flag = true;
225
+ $partial_cache_flag = false;
226
+
227
+ foreach ( $target_sns as $key => $value ) {
228
+
229
+ if ( $value ) {
230
+
231
+ $meta_key = $this->meta_key_prefix . strtolower( $key );
232
+
233
+ $sns_count = get_post_meta( get_the_ID(), $meta_key, true );
234
+
235
+ if ( isset( $sns_count ) && $sns_count >= 0 ) {
236
+ $partial_cache_flag = true;
237
+ } else {
238
+ $full_cache_flag = false;
239
+ }
240
+
241
+ }
242
+ }
243
+
244
+ if ( $partial_cache_flag && $full_cache_flag ) {
245
+ //full cache
246
+ $transient_ID = $this->get_transient_ID( $post_ID );
247
+
248
+ if ( false === ( $sns_counts = get_transient( $transient_ID ) ) ) {
249
+ if ( $post_ID < $check_range_min || $post_ID > $check_range_max ) {
250
+ $no_cache_post_IDs[$post_ID] = 1;
251
+ }
252
+ }
253
+ } else if ( $partial_cache_flag && ! $full_cache_flag ) {
254
+ //partial cache
255
+ $transient_ID = $this->get_transient_ID( $post_ID );
256
+
257
+ if ( false === ( $sns_counts = get_transient( $transient_ID ) ) ) {
258
+ if ( $post_ID < $check_range_min || $post_ID > $check_range_max ) {
259
+ $no_cache_post_IDs[$post_ID] = 2;
260
+ }
261
+ }
262
+ } else {
263
+ if ( $post_ID < $check_range_min || $post_ID > $check_range_max ) {
264
+ $no_cache_post_IDs[$post_ID] = 3;
265
+ }
266
+ }
267
+
268
+ }
269
+ }
270
+ wp_reset_postdata();
271
+
272
+ Common_Util::log( '[' . __METHOD__ . '] no cache post IDs:');
273
+
274
+ Common_Util::log( $no_cache_post_IDs );
275
+
276
+ arsort( $no_cache_post_IDs, SORT_NUMERIC );
277
+
278
+ $rescue_post_IDs = array_slice( $no_cache_post_IDs, 0, $this->posts_per_check, true );
279
+
280
+ Common_Util::log( $rescue_post_IDs );
281
+
282
+ foreach ( $rescue_post_IDs as $post_ID => $priority ) {
283
+ Common_Util::log( '[' . __METHOD__ . '] post_id: ' . $post_ID );
284
+
285
+ $this->cache( $post_ID, $this->target_sns, $cache_expiration );
286
+
287
+ if ( ! is_null( $this->delegate ) && method_exists( $this->delegate, 'order_cache' ) ) {
288
+ $options = array( 'post_id' => $post_ID );
289
+ $this->delegate->order_cache( $this, $options );
290
+ }
291
+ }
292
+
293
+ }
294
+
295
+ /**
296
+ * Get and cache data for a given post
297
+ *
298
+ * @since 0.1.1
299
+ */
300
+ public function direct_cache( $post_ID ) {
301
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
302
+
303
+ $cache_expiration = $this->get_cache_expiration();
304
+
305
+ Common_Util::log( '[' . __METHOD__ . '] cache_expiration: ' . $cache_expiration );
306
+
307
+ return $this->cache( $post_ID, $this->target_sns, $cache_expiration );
308
+ }
309
+
310
+ /**
311
+ * Get cache expiration based on current number of total post and page
312
+ *
313
+ * @since 0.1.1
314
+ */
315
+ protected function get_cache_expiration() {
316
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
317
+
318
+ $posts_total = $this->get_posts_total();
319
+
320
+ Common_Util::log( '[' . __METHOD__ . '] posts_total: ' . $posts_total );
321
+
322
+ return ( ( ceil( $posts_total / $this->posts_per_check ) + 2 ) * $this->check_interval ) + 2 * $this->check_interval;
323
+ }
324
+
325
+ /**
326
+ * Get and cache data for a given post
327
+ *
328
+ * @since 0.1.1
329
+ */
330
+ public function cache( $post_ID, $target_sns, $cache_expiration ) {
331
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
332
+
333
+ Common_Util::log( '[' . __METHOD__ . '] post_id: ' . $post_ID );
334
+
335
+ $transient_ID = $this->get_transient_ID( $post_ID );
336
+
337
+ $url = get_permalink( $post_ID );
338
+
339
+ $data = $this->crawler->get_data( $target_sns, $url );
340
+
341
+ Common_Util::log( $data );
342
+
343
+ if ( $data ) {
344
+ $result = set_transient( $transient_ID, $data, $cache_expiration );
345
+
346
+ Common_Util::log( '[' . __METHOD__ . '] set_transient result: ' . $result );
347
+ }
348
+
349
+ return $data;
350
+ }
351
+
352
+ /**
353
+ * Initialize meta key for ranking
354
+ *
355
+ * @since 0.3.0
356
+ */
357
+ public function initialize_cache() {
358
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
359
+
360
+ }
361
+
362
+ /**
363
+ * Clear meta key for ranking
364
+ *
365
+ * @since 0.3.0
366
+ */
367
+ public function clear_cache() {
368
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
369
+
370
+ }
371
+
372
+ /**
373
+ * Get total count of current published post and page
374
+ *
375
+ * @since 0.1.0
376
+ */
377
+ private function get_posts_total() {
378
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
379
+
380
+ $query_args = array(
381
+ 'post_type' => $this->post_types,
382
+ 'post_status' => 'publish',
383
+ 'nopaging' => true,
384
+ 'update_post_term_cache' => false,
385
+ 'update_post_meta_cache' => false
386
+ );
387
+
388
+ $posts_query = new WP_Query( $query_args );
389
+
390
+ return $posts_query->found_posts;
391
+ }
392
+
393
+ }
394
+
395
+ ?>
includes/class-share-rush-cache-engine.php ADDED
@@ -0,0 +1,371 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /*
3
+ class-share-rush-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.4.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
+ class Share_Rush_Cache_Engine extends Cache_Engine {
35
+
36
+ /**
37
+ * Prefix of cache ID
38
+ */
39
+ const DEF_TRANSIENT_PREFIX = 'scc_share_count_';
40
+
41
+ /**
42
+ * Cron name to schedule cache processing
43
+ */
44
+ const DEF_PRIME_CRON = 'scc_share_rushcache_prime';
45
+
46
+ /**
47
+ * Cron name to execute cache processing
48
+ */
49
+ const DEF_EXECUTE_CRON = 'scc_share_rushcache_exec';
50
+
51
+ /**
52
+ * Schedule name for cache processing
53
+ */
54
+ const DEF_EVENT_SCHEDULE = 'share_rush_cache_event';
55
+
56
+ /**
57
+ * Schedule description for cache processing
58
+ */
59
+ const DEF_EVENT_DESCRIPTION = '[SCC] Share Rush Cache Interval';
60
+
61
+
62
+ private $crawler = NULL;
63
+
64
+ /**
65
+ * Interval cheking and caching target data
66
+ */
67
+ private $check_interval = 600;
68
+
69
+ /**
70
+ * Number of posts to check at a time
71
+ */
72
+ private $posts_per_check = 20;
73
+
74
+ /**
75
+ * Offset suffix
76
+ */
77
+ private $offset_suffix = 'rush_offset';
78
+
79
+ /**
80
+ * Term considered as new content
81
+ */
82
+ private $new_content_term = 3;
83
+
84
+ /**
85
+ * Cache target
86
+ */
87
+ private $target_sns = array();
88
+
89
+ /**
90
+ * Cache post types
91
+ */
92
+ private $post_types = array( 'post', 'page' );
93
+
94
+ /**
95
+ * instance for delegation
96
+ */
97
+ private $delegate = NULL;
98
+
99
+ /**
100
+ * Class constarctor
101
+ * Hook onto all of the actions and filters needed by the plugin.
102
+ *
103
+ */
104
+ protected function __construct() {
105
+ Common_Util::log('[' . __METHOD__ . '] (line='. __LINE__ . ')');
106
+ }
107
+
108
+ /**
109
+ * Initialization
110
+ *
111
+ * @since 0.1.1
112
+ */
113
+ public function initialize( $options = array() ) {
114
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
115
+
116
+ $this->transient_prefix = self::DEF_TRANSIENT_PREFIX;
117
+ $this->prime_cron = self::DEF_PRIME_CRON;
118
+ $this->execute_cron = self::DEF_EXECUTE_CRON;
119
+ $this->event_schedule = self::DEF_EVENT_SCHEDULE;
120
+ $this->event_description = self::DEF_EVENT_DESCRIPTION;
121
+
122
+ if ( isset( $options['delegate'] ) ) $this->delegate = $options['delegate'];
123
+ if ( isset( $options['crawler'] ) ) $this->crawler = $options['crawler'];
124
+ if ( isset( $options['target_sns'] ) ) $this->target_sns = $options['target_sns'];
125
+ if ( isset( $options['check_interval'] ) ) $this->check_interval = $options['check_interval'];
126
+ if ( isset( $options['posts_per_check'] ) ) $this->posts_per_check = $options['posts_per_check'];
127
+ if ( isset( $options['transient_prefix'] ) ) $this->transient_prefix = $options['transient_prefix'];
128
+ if ( isset( $options['prime_cron'] ) ) $this->prime_cron = $options['prime_cron'];
129
+ if ( isset( $options['execute_cron'] ) ) $this->execute_cron = $options['execute_cron'];
130
+ if ( isset( $options['event_schedule'] ) ) $this->event_schedule = $options['event_schedule'];
131
+ if ( isset( $options['event_description'] ) ) $this->event_description = $options['event_description'];
132
+ if ( isset( $options['post_types'] ) ) $this->post_types = $options['post_types'];
133
+ if ( isset( $options['new_content_term'] ) ) $this->new_content_term = $options['new_content_term'];
134
+
135
+ add_filter( 'cron_schedules', array( $this, 'schedule_check_interval' ) );
136
+ add_action( $this->prime_cron, array( $this, 'prime_cache' ) );
137
+ add_action( $this->execute_cron, array( $this, 'execute_cache' ), 10, 2 );
138
+
139
+ }
140
+
141
+ /**
142
+ * Register event schedule for this engine
143
+ *
144
+ * @since 0.1.0
145
+ */
146
+ public function schedule_check_interval( $schedules ) {
147
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
148
+
149
+ $schedules[$this->event_schedule] = array(
150
+ 'interval' => $this->check_interval,
151
+ 'display' => $this->event_description
152
+ );
153
+
154
+ return $schedules;
155
+ }
156
+
157
+ /**
158
+ * Schedule data retrieval and cache processing
159
+ *
160
+ * @since 0.2.0
161
+ */
162
+ public function prime_cache() {
163
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
164
+
165
+ $next_exec_time = time() + $this->check_interval;
166
+ $posts_total = $this->get_posts_total();
167
+
168
+ Common_Util::log( '[' . __METHOD__ . '] check_interval: ' . $this->check_interval );
169
+ Common_Util::log( '[' . __METHOD__ . '] posts_per_check: ' . $this->posts_per_check );
170
+ Common_Util::log( '[' . __METHOD__ . '] next_exec_time: ' . $next_exec_time );
171
+ Common_Util::log( '[' . __METHOD__ . '] posts_total: ' . $posts_total );
172
+
173
+ $transient_ID = $this->get_transient_ID($this->offset_suffix);
174
+
175
+ /*
176
+ if ( false === ( $posts_offset = get_transient( $transient_ID ) ) ) {
177
+ $posts_offset = 0;
178
+ }
179
+ */
180
+
181
+ if ( false === ( $posts_offset = get_option( $transient_ID ) ) ) {
182
+ $posts_offset = 0;
183
+ }
184
+
185
+ Common_Util::log( '[' . __METHOD__ . '] posts_offset: ' . $posts_offset );
186
+
187
+ wp_schedule_single_event( $next_exec_time, $this->execute_cron, array( $posts_offset, Common_Util::short_hash( $next_exec_time ) ) );
188
+
189
+ $posts_offset = $posts_offset + $this->posts_per_check;
190
+
191
+ if ( $posts_offset > $posts_total ) {
192
+ $posts_offset = 0;
193
+ }
194
+
195
+ //set_transient( $transient_ID, $posts_offset, 3 * $this->check_interval );
196
+ update_option( $transient_ID, $posts_offset );
197
+
198
+
199
+ }
200
+
201
+ /**
202
+ * Get and cache data of each published post and page
203
+ *
204
+ * @since 0.2.0
205
+ */
206
+ public function execute_cache( $posts_offset, $hash ) {
207
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
208
+
209
+ Common_Util::log( '[' . __METHOD__ . '] posts_offset: ' . $posts_offset );
210
+ Common_Util::log( '[' . __METHOD__ . '] check_interval: ' . $this->check_interval );
211
+ Common_Util::log( '[' . __METHOD__ . '] posts_per_check: ' . $this->posts_per_check );
212
+
213
+ $cache_expiration = $this->get_cache_expiration();
214
+
215
+ $term_threshold = $this->get_new_contet_term();
216
+
217
+ Common_Util::log( '[' . __METHOD__ . '] cache_expiration: ' . $cache_expiration );
218
+
219
+ Common_Util::log( '[' . __METHOD__ . '] term_threshold: ' . $term_threshold );
220
+
221
+ $query_args = array(
222
+ 'post_type' => $this->post_types,
223
+ 'post_status' => 'publish',
224
+ 'offset' => $posts_offset,
225
+ 'posts_per_page' => $this->posts_per_check,
226
+ 'date_query' => array(
227
+ 'column' => 'post_date_gmt',
228
+ 'after' => $term_threshold
229
+ ),
230
+ 'no_found_rows' => true,
231
+ 'update_post_term_cache' => false,
232
+ 'update_post_meta_cache' => false
233
+ );
234
+
235
+ $posts_query = new WP_Query( $query_args );
236
+
237
+ if ( $posts_query->have_posts() ) {
238
+ while ( $posts_query->have_posts() ) {
239
+ $posts_query->the_post();
240
+
241
+ $post_ID = get_the_ID();
242
+
243
+ Common_Util::log( '[' . __METHOD__ . '] post_id: ' . $post_ID );
244
+
245
+ $this->cache( $post_ID, $this->target_sns, $cache_expiration );
246
+
247
+ if ( ! is_null( $this->delegate ) && method_exists( $this->delegate, 'order_cache' ) ) {
248
+ $options = array( 'post_id' => $post_ID );
249
+ $this->delegate->order_cache( $this, $options );
250
+ }
251
+ }
252
+ }
253
+ wp_reset_postdata();
254
+ }
255
+
256
+ /**
257
+ * Get term considered as new content
258
+ *
259
+ * @since 0.4.0
260
+ */
261
+ private function get_new_contet_term() {
262
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
263
+
264
+ $term_threshold = '3 days ago';
265
+
266
+ if ( $this->new_content_term > 1 ) {
267
+ $term_threshold = $this->new_content_term . ' days ago';
268
+ } else if ( $this->new_content_term == 1 ) {
269
+ $term_threshold = $this->new_content_term . ' day ago';
270
+ }
271
+
272
+ return $term_threshold;
273
+ }
274
+
275
+ /**
276
+ * Get cache expiration based on current number of total post and page
277
+ *
278
+ * @since 0.2.0
279
+ */
280
+ protected function get_cache_expiration() {
281
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
282
+
283
+ $posts_total = $this->get_posts_total();
284
+
285
+ Common_Util::log( '[' . __METHOD__ . '] posts_total: ' . $posts_total );
286
+
287
+ return ceil( $posts_total / $this->posts_per_check ) * $this->check_interval * 3;
288
+ }
289
+
290
+ /**
291
+ * Get total count of current published post and page
292
+ *
293
+ * @since 0.2.0
294
+ */
295
+ private function get_posts_total() {
296
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
297
+
298
+ $term_threshold = $this->get_new_contet_term();
299
+
300
+ $query_args = array(
301
+ 'post_type' => $this->post_types,
302
+ 'post_status' => 'publish',
303
+ 'date_query' => array(
304
+ 'column' => 'post_date_gmt',
305
+ 'after' => $term_threshold
306
+ ),
307
+ 'nopaging' => true,
308
+ 'no_found_rows' => true,
309
+ 'update_post_term_cache' => false,
310
+ 'update_post_meta_cache' => false
311
+ );
312
+
313
+ $posts_query = new WP_Query( $query_args );
314
+
315
+ return $posts_query->found_posts;
316
+ }
317
+
318
+ /**
319
+ * Get and cache data for a given post
320
+ *
321
+ * @since 0.1.1
322
+ */
323
+ public function cache( $post_ID, $target_sns, $cache_expiration ) {
324
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
325
+
326
+ Common_Util::log( '[' . __METHOD__ . '] post_id: ' . $post_ID );
327
+
328
+ $transient_ID = $this->get_transient_ID( $post_ID );
329
+
330
+ $url = get_permalink( $post_ID );
331
+
332
+ $data = $this->crawler->get_data( $target_sns, $url );
333
+
334
+ Common_Util::log( $data );
335
+
336
+ if ( $data ) {
337
+ $result = set_transient( $transient_ID, $data, $cache_expiration );
338
+
339
+ Common_Util::log( '[' . __METHOD__ . '] set_transient result: ' . $result );
340
+ }
341
+
342
+ return $data;
343
+ }
344
+
345
+ /**
346
+ * Initialize meta key for ranking
347
+ *
348
+ * @since 0.3.0
349
+ */
350
+ public function initialize_cache() {
351
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
352
+
353
+ }
354
+
355
+ /**
356
+ * Clear meta key for ranking
357
+ *
358
+ * @since 0.3.0
359
+ */
360
+ public function clear_cache() {
361
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
362
+
363
+ $transient_ID = $this->get_transient_ID( $this->offset_suffix );
364
+
365
+ delete_option( $transient_ID );
366
+
367
+ }
368
+
369
+ }
370
+
371
+ ?>
includes/class-share-second-cache-engine.php ADDED
@@ -0,0 +1,307 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /*
3
+ class-share-second-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.4.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
+ class Share_Second_Cache_Engine extends Cache_Engine {
35
+
36
+ /**
37
+ * Prefix of cache ID
38
+ */
39
+ const DEF_TRANSIENT_PREFIX = 'scc_share_count_';
40
+
41
+ /**
42
+ * Cron name to schedule cache processing
43
+ */
44
+ const DEF_PRIME_CRON = 'scc_share_2ndcache_prime';
45
+
46
+ /**
47
+ * Cron name to execute cache processing
48
+ */
49
+ const DEF_EXECUTE_CRON = 'scc_share_2ndcache_exec';
50
+
51
+ /**
52
+ * Schedule name for cache processing
53
+ */
54
+ const DEF_EVENT_SCHEDULE = 'share_second_cache_event';
55
+
56
+ /**
57
+ * Schedule description for cache processing
58
+ */
59
+ const DEF_EVENT_DESCRIPTION = '[SCC] Share Second Cache Interval';
60
+
61
+ /**
62
+ * Interval cheking and caching target data
63
+ */
64
+ private $check_interval = 600;
65
+
66
+ /**
67
+ * Number of posts to check at a time
68
+ */
69
+ private $posts_per_check = 20;
70
+
71
+ /**
72
+ * Prefix of cache ID
73
+ */
74
+ private $meta_key_prefix = 'scc_share_count_';
75
+
76
+ /**
77
+ * Cache target
78
+ */
79
+ private $target_sns = array();
80
+
81
+ /**
82
+ * Cache post types
83
+ */
84
+ private $post_types = array( 'post', 'page' );
85
+
86
+ /**
87
+ * Class constarctor
88
+ * Hook onto all of the actions and filters needed by the plugin.
89
+ *
90
+ */
91
+ protected function __construct() {
92
+ Common_Util::log('[' . __METHOD__ . '] (line='. __LINE__ . ')');
93
+ }
94
+
95
+ /**
96
+ * Initialization
97
+ *
98
+ * @since 0.1.1
99
+ */
100
+ public function initialize( $options = array() ) {
101
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
102
+
103
+ $this->transient_prefix = self::DEF_TRANSIENT_PREFIX;
104
+ $this->prime_cron = self::DEF_PRIME_CRON;
105
+ $this->execute_cron = self::DEF_EXECUTE_CRON;
106
+ $this->event_schedule = self::DEF_EVENT_SCHEDULE;
107
+ $this->event_description = self::DEF_EVENT_DESCRIPTION;
108
+
109
+ if ( isset( $options['target_sns'] ) ) $this->target_sns = $options['target_sns'];
110
+ if ( isset( $options['check_interval'] ) ) $this->check_interval = $options['check_interval'];
111
+ if ( isset( $options['posts_per_check'] ) ) $this->posts_per_check = $options['posts_per_check'];
112
+ if ( isset( $options['transient_prefix'] ) ) $this->transient_prefix = $options['transient_prefix'];
113
+ if ( isset( $options['prime_cron'] ) ) $this->prime_cron = $options['prime_cron'];
114
+ if ( isset( $options['execute_cron'] ) ) $this->execute_cron = $options['execute_cron'];
115
+ if ( isset( $options['event_schedule'] ) ) $this->event_schedule = $options['event_schedule'];
116
+ if ( isset( $options['event_description'] ) ) $this->event_description = $options['event_description'];
117
+ if ( isset( $options['post_types'] ) ) $this->post_types = $options['post_types'];
118
+ if ( isset( $options['meta_key_prefix'] ) ) $this->meta_key_prefix = $options['meta_key_prefix'];
119
+
120
+ add_filter( 'cron_schedules', array( $this, 'schedule_check_interval' ) );
121
+ add_action( $this->prime_cron, array( $this, 'prime_cache' ) );
122
+ add_action( $this->execute_cron, array( $this, 'execute_cache' ), 10, 0 );
123
+
124
+ }
125
+
126
+ /**
127
+ * Register event schedule for this engine
128
+ *
129
+ * @since 0.1.0
130
+ */
131
+ public function schedule_check_interval( $schedules ) {
132
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
133
+
134
+ $schedules[$this->event_schedule] = array(
135
+ 'interval' => $this->check_interval,
136
+ 'display' => $this->event_description
137
+ );
138
+
139
+ return $schedules;
140
+ }
141
+
142
+ /**
143
+ * Schedule data retrieval and cache processing
144
+ *
145
+ * @since 0.3.0
146
+ */
147
+ public function prime_cache() {
148
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
149
+
150
+ $next_exec_time = time() + $this->check_interval;
151
+
152
+ Common_Util::log( '[' . __METHOD__ . '] check_interval: ' . $this->check_interval );
153
+ Common_Util::log( '[' . __METHOD__ . '] next_exec_time: ' . $next_exec_time );
154
+
155
+ wp_schedule_single_event( $next_exec_time, $this->execute_cron );
156
+ }
157
+
158
+ /**
159
+ * Get and cache data of each published post and page
160
+ *
161
+ * @since 0.3.0
162
+ */
163
+ public function execute_cache() {
164
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
165
+
166
+ $query_args = array(
167
+ 'post_type' => $this->post_types,
168
+ 'post_status' => 'publish',
169
+ 'nopaging' => true,
170
+ 'update_post_term_cache' => false,
171
+ 'update_post_meta_cache' => false
172
+ );
173
+
174
+ $posts_query = new WP_Query( $query_args );
175
+
176
+ if ( $posts_query->have_posts() ) {
177
+ while ( $posts_query->have_posts() ) {
178
+ $posts_query->the_post();
179
+
180
+ $post_ID = get_the_ID();
181
+
182
+ $this->cache( $post_ID, $this->target_sns, 0 );
183
+
184
+ }
185
+ }
186
+ wp_reset_postdata();
187
+
188
+ }
189
+
190
+ /**
191
+ * Get and cache data for a given post
192
+ *
193
+ * @since 0.1.1
194
+ */
195
+ public function cache( $post_ID, $target_sns, $cache_expiration ) {
196
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
197
+
198
+ $transient_ID = $this->get_transient_ID( $post_ID );
199
+
200
+ if ( false !== ( $sns_counts = get_transient( $transient_ID ) ) ) {
201
+ foreach ( $target_sns as $key => $value ) {
202
+
203
+ if ( $value ) {
204
+
205
+ $meta_key = $this->meta_key_prefix . strtolower( $key );
206
+
207
+ if ( ! is_null( $sns_counts[$key] ) && $sns_counts[$key] >= 0 ) {
208
+ Common_Util::log( '[' . __METHOD__ . '] meta_key: ' . $meta_key . ' SNS: ' . $key . ' post_ID: ' . $post_ID . ' - ' . $sns_counts[$key] );
209
+
210
+ update_post_meta($post_ID, $meta_key, $sns_counts[$key]);
211
+ //$data[$key][$post_ID] = $sns_counts[$key];
212
+ }
213
+ }
214
+ }
215
+
216
+ }
217
+
218
+ }
219
+
220
+ /**
221
+ * Get cache expiration based on current number of total post and page
222
+ *
223
+ * @since 0.2.0
224
+ */
225
+ protected function get_cache_expiration() {
226
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
227
+
228
+ return 0;
229
+ }
230
+
231
+ /**
232
+ * Initialize meta key for ranking
233
+ *
234
+ * @since 0.3.0
235
+ */
236
+ public function initialize_cache() {
237
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
238
+
239
+ $query_args = array(
240
+ 'post_type' => $this->post_types,
241
+ 'post_status' => 'publish',
242
+ 'nopaging' => true,
243
+ 'update_post_term_cache' => false,
244
+ 'update_post_meta_cache' => false
245
+ );
246
+
247
+ $posts_query = new WP_Query( $query_args );
248
+
249
+ if ( $posts_query->have_posts() ) {
250
+ while ( $posts_query->have_posts() ) {
251
+ $posts_query->the_post();
252
+
253
+ $post_ID = get_the_ID();
254
+
255
+ foreach ( $this->target_sns as $key => $value ) {
256
+
257
+ $meta_key = $this->meta_key_prefix . strtolower( $key );
258
+
259
+ if ( $value ) {
260
+ update_post_meta($post_ID, $meta_key, -1);
261
+ }
262
+ }
263
+ }
264
+ }
265
+ wp_reset_postdata();
266
+ }
267
+
268
+ /**
269
+ * Clear meta key for ranking
270
+ *
271
+ * @since 0.3.0
272
+ */
273
+ public function clear_cache() {
274
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
275
+
276
+ $query_args = array(
277
+ 'post_type' => $this->post_types,
278
+ 'post_status' => 'publish',
279
+ 'nopaging' => true,
280
+ 'update_post_term_cache' => false,
281
+ 'update_post_meta_cache' => false
282
+ );
283
+
284
+ $posts_query = new WP_Query( $query_args );
285
+
286
+ if ( $posts_query->have_posts() ) {
287
+ while ( $posts_query->have_posts() ) {
288
+ $posts_query->the_post();
289
+
290
+ $post_ID = get_the_ID();
291
+
292
+ foreach ( $this->target_sns as $key => $value ) {
293
+
294
+ $meta_key = $this->meta_key_prefix . strtolower( $key );
295
+
296
+ if ( $value ) {
297
+ delete_post_meta($post_ID, $meta_key);
298
+ }
299
+ }
300
+ }
301
+ }
302
+ wp_reset_postdata();
303
+ }
304
+
305
+ }
306
+
307
+ ?>
includes/class-sns-count-crawler.php DELETED
@@ -1,224 +0,0 @@
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
- ?>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
includes/class-wp-cron-util.php ADDED
@@ -0,0 +1,121 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /*
3
+ class-wp-cron-util.php
4
+
5
+ Description: This class is a utility for WP-Cron
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
+ class WP_Cron_Util {
35
+
36
+ /**
37
+ * Class constarctor
38
+ * Hook onto all of the actions and filters needed by the plugin.
39
+ *
40
+ */
41
+ protected function __construct() {
42
+
43
+ }
44
+
45
+ /**
46
+ * Clear scheduled hook based related to specified hook name
47
+ *
48
+ * @since 0.1.1
49
+ */
50
+ public static function clear_scheduled_hook( $hook ) {
51
+ $crons = _get_cron_array();
52
+
53
+ if ( empty( $crons ) ) return;
54
+
55
+ foreach( $crons as $timestamp => $cron ) {
56
+ if( isset( $cron[$hook] ) ) {
57
+ foreach ( $cron[$hook] as $signature => $data ) {
58
+ wp_unschedule_event( $timestamp, $hook, $data['args'] );
59
+ }
60
+ }
61
+ }
62
+ }
63
+
64
+ /**
65
+ * Return if there is the given hook or not
66
+ *
67
+ * @since 0.1.1
68
+ */
69
+ public static function is_scheduled_hook( $hook ) {
70
+ $crons = _get_cron_array();
71
+
72
+ if ( empty( $crons ) ) return false;
73
+
74
+ foreach( $crons as $timestamp => $cron ) {
75
+ if( isset( $cron[$hook] ) ) {
76
+ return true;
77
+ }
78
+ }
79
+
80
+ return false;
81
+ }
82
+
83
+ /**
84
+ * Get scheduled hook related to specified hook name
85
+ *
86
+ * @since 0.1.1
87
+ */
88
+ public static function get_scheduled_hook( $hook ) {
89
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
90
+
91
+ $crons = _get_cron_array();
92
+
93
+ $info = array();
94
+ $index = 0;
95
+
96
+ if ( empty( $crons ) ) return;
97
+
98
+ foreach( $crons as $timestamp => $cron ) {
99
+ if( isset( $cron[$hook] ) ) {
100
+ Common_Util::log( $cron[$hook] );
101
+ foreach ( $cron[$hook] as $signature => $data ) {
102
+
103
+ Common_Util::log( '[' . __METHOD__ . '] hook: ' . $hook . ' offset: ' . $data['args'][0]. ' timestamp: ' . $timestamp );
104
+
105
+ $info[$index]['hook'] = $hook;
106
+ $info[$index]['timestamp'] = $timestamp;
107
+ $info[$index]['args'] = $data['args'];
108
+ //wp_unschedule_event( $timestamp, $hook, $data['args'] );
109
+ }
110
+ $index++;
111
+ }
112
+ }
113
+
114
+ return $info;
115
+ }
116
+
117
+ }
118
+
119
+
120
+
121
+ ?>
includes/download.php ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /*
3
+ download.php
4
+
5
+ Description: Download page implementation
6
+ Version: 0.4.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
+ require_once( '../../../../wp-blog-header.php' );
34
+
35
+ $abs_path = WP_PLUGIN_DIR . '/sns-count-cache/data/sns-count-cache-data.csv';
36
+
37
+ if ( isset( $_POST['_wpnonce'] ) && $_POST['_wpnonce'] ) {
38
+ if ( check_admin_referer( 'mynonce', '_wpnonce' ) ) {
39
+
40
+ if ( isset( $_POST["download_data"] ) && $_POST["download_data"] === 'Download' ) {
41
+
42
+ if ( file_exists( $abs_path ) ) {
43
+
44
+ $file_name = "sns-count-cache_data_" . date_i18n( 'YmdHis' ) . ".csv";
45
+
46
+ header( 'Content-Type: application/octet-stream' );
47
+ header( 'Content-Disposition: attachment; filename=' . $file_name );
48
+
49
+ while ( ob_get_level() > 0 ) {
50
+ ob_end_clean();
51
+ }
52
+
53
+ ob_start();
54
+
55
+ if ( $fp = fopen( $abs_path, 'rb' ) ) {
56
+
57
+ while( ! feof( $fp ) && ( connection_status() == 0 ) ) {
58
+ echo fread( $fp, 8192 );
59
+ ob_flush();
60
+ }
61
+
62
+ ob_flush();
63
+ fclose($fp);
64
+
65
+ }
66
+
67
+ ob_end_clean();
68
+ die();
69
+
70
+ } else {
71
+ echo 'There is no exported file.';
72
+ }
73
+ }
74
+ }
75
+ }
76
+
77
+ ?>
includes/interface-cache-order.php ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /*
3
+ interface-cache-order.php
4
+
5
+ Description: This is a interface
6
+ Version: 0.4.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
+ interface Cache_Order {
34
+ public function order_cache( Cache_Engine $engine, $options = array() );
35
+ }
36
+
37
+ ?>
readme.txt CHANGED
@@ -3,15 +3,13 @@ Contributors: marubon
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.
@@ -19,11 +17,16 @@ This plugin may help you to shorten page loading time because the share count ca
19
 
20
  The following shows functions to get share count from the cache:
21
 
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
 
@@ -32,6 +35,7 @@ The following describes meta keys to get share count from custom field.
32
  * scc_share_count_google+
33
  * scc_share_count_pocket
34
  * scc_share_count_hatebu
 
35
 
36
  == Installation ==
37
 
@@ -48,10 +52,11 @@ The following describes meta keys to get share count from custom field.
48
  There are no questions.
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
 
@@ -70,9 +75,23 @@ There are no questions.
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
 
76
- == Arbitrary section ==
 
 
 
 
77
 
 
 
78
 
3
  Donate link:
4
  Tags: performance, SNS, social, cache
5
  Requires at least: 3.7
6
+ Tested up to: 4.1
7
+ Stable tag: 0.4.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
  == Description ==
14
 
15
  SNS Count Cache gets share count for Twitter and Facebook, Google Plus, Pocket, Hatena Bookmark and caches these count in the background.
17
 
18
  The following shows functions to get share count from the cache:
19
 
20
+ * scc_get_share_twitter()
21
+ * scc_get_share_facebook()
22
+ * scc_get_share_gplus()
23
+ * scc_get_share_pocket()
24
+ * scc_get_share_hatebu()
25
+ * scc_get_share_total()
26
+
27
+ The following shows function to get follower count from the cache:
28
+
29
+ * scc_get_follow_feedly()
30
 
31
  The following describes meta keys to get share count from custom field.
32
 
35
  * scc_share_count_google+
36
  * scc_share_count_pocket
37
  * scc_share_count_hatebu
38
+ * scc_share_count_total
39
 
40
  == Installation ==
41
 
52
  There are no questions.
53
 
54
  == Screenshots ==
55
+ 1. Dashboard to overview cache status and share count
56
+ 2. Cache status is described in setting page
57
+ 3. Share count for each post can be verviewed
58
+ 4. Described parameters can be modified in this page
59
+ 5. Help page shows available functions to access the cache
60
 
61
  == Changelog ==
62
 
75
  * Added: page to display share count for specified all tagets.
76
  * Added: function to query pages and posts based on SNS share count using specific custom fields in WP_Query and so on.
77
 
78
+ = 0.4.0 =
79
+ * Added: admin page was totally improved.
80
+ * Added: function to sort contents based on share count was added to admin page of share count.
81
+ * Added: content of custom post type was added as share count cache target.
82
+ * Added: number of Feedly follower was included as one of cache targets.
83
+ * Added: function to export share count data was added.
84
+ * Added: cache logic was improved.
85
+
86
  == Upgrade Notice ==
87
+ The following functions are deprecated.
88
 
89
+ * get_scc_twitter()
90
+ * get_scc_facebook()
91
+ * get_scc_gplus()
92
+ * get_scc_pocket()
93
+ * get_scc_hatebu()
94
 
95
+ == Arbitrary section ==
96
+ * PHP Version: 5.3+
97
 
screenshot-1.png CHANGED
Binary file
screenshot-2.png CHANGED
Binary file
screenshot-3.png CHANGED
Binary file
screenshot-4.png CHANGED
Binary file
screenshot-5.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, 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,139 +29,107 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
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
48
- */
49
- private $crawler = NULL;
50
-
51
- /**
52
- * Instance of module class of schedule and cache processing
53
- */
54
- private $cache_engine = NULL;
55
-
56
- /**
57
- * Slug of the plugin screen
58
- */
59
- private $plugin_screen_hook_suffix = NULL;
60
 
61
- /**
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
  */
74
- const OPT_BASE_TRANSIENT_PREFIX = 'sns_count_cache_';
75
 
76
- /**
77
- * Cron name to schedule cache processing
78
- */
79
- const OPT_BASE_CACHE_PRIME_CRON = 'scc_basecache_prime';
80
-
81
- /**
82
- * Cron name to execute cache processing
83
- */
84
- const OPT_BASE_CACHE_EXECUTE_CRON = 'scc_basecache_exec';
85
-
86
- /**
87
- * Schedule name for cache processing
88
- */
89
- const OPT_BASE_EVENT_SCHEDULE = 'base_cache_event';
90
-
91
  /**
92
- * Schedule description for cache processing
93
  */
94
- const OPT_BASE_EVENT_DESCRIPTION = '[SCC] Share Count Cache Basic Interval';
95
-
96
  /**
97
- * Interval cheking and caching target data
98
  */
99
- const OPT_BASE_CHECK_INTERVAL = 600;
100
 
101
  /**
102
- * Number of posts to check at a time
103
  */
104
- const OPT_BASE_POSTS_PER_CHECK = 20;
105
 
106
  /**
107
- * Cron name to schedule cache processing
108
- */
109
- const OPT_RUSH_CACHE_PRIME_CRON = 'scc_rushcache_prime';
110
 
111
  /**
112
- * Cron name to execute cache processing
113
  */
114
- const OPT_RUSH_CACHE_EXECUTE_CRON = 'scc_rushcache_exec';
115
-
116
- /**
117
- * Schedule name for cache processing
118
- */
119
- const OPT_RUSH_EVENT_SCHEDULE = 'rush_cache_event';
120
-
121
- /**
122
- * Schedule description for cache processing
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
- */
164
- const OPT_LAZY_CACHE_EXECUTE_CRON = 'scc_lazycache_exec';
165
 
166
  /**
167
  * Type of dynamic cache processing
@@ -182,62 +150,207 @@ class SNS_Count_Cache {
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
  */
189
- const DB_CHECK_INTERVAL = 'scc_check_interval';
190
 
191
  /**
192
- * Option key for Number of posts to check at a time
 
 
 
 
 
193
  */
194
- const DB_POSTS_PER_CHECK = 'scc_posts_per_check';
195
 
196
  /**
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
213
  */
214
  const DOMAIN = 'sns-count-cache';
215
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
243
  */
@@ -248,7 +361,7 @@ class SNS_Count_Cache {
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
 
@@ -284,63 +397,178 @@ class SNS_Count_Cache {
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,
323
- 'base_cache_prime_cron' => self::OPT_BASE_CACHE_PRIME_CRON,
324
- 'base_cache_execute_cron' => self::OPT_BASE_CACHE_EXECUTE_CRON,
325
- 'base_event_schedule' => self::OPT_BASE_EVENT_SCHEDULE,
326
- 'base_event_description' => self::OPT_BASE_EVENT_DESCRIPTION,
327
- 'rush_cache_prime_cron' => self::OPT_RUSH_CACHE_PRIME_CRON,
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
 
346
  /**
@@ -349,7 +577,7 @@ class SNS_Count_Cache {
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;
@@ -357,11 +585,10 @@ class SNS_Count_Cache {
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
  }
366
 
367
  /**
@@ -370,7 +597,7 @@ class SNS_Count_Cache {
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;
@@ -378,34 +605,46 @@ class SNS_Count_Cache {
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
  }
387
 
388
  /**
389
- * Activate cache engine (base schedule cron)
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();
 
 
 
 
 
 
 
 
398
  }
399
 
400
  /**
401
- * Deactivate cache engine (base schedule cron)
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
  }
410
 
411
  /**
@@ -414,11 +653,25 @@ class SNS_Count_Cache {
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();
421
- $this->cache_engine->register_base_schedule();
 
 
 
 
 
 
 
 
422
  }
423
 
424
  /**
@@ -429,80 +682,241 @@ class SNS_Count_Cache {
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
- /**
436
  * Option page implementation
437
  *
438
  * @since 0.1.0
439
- */
440
- public function option_page() {
441
- include_once( dirname( __FILE__ ) . '/includes/admin.php' );
442
- }
443
 
444
- /**
445
- * Output log message according to WP_DEBUG setting
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
  }
458
-
 
 
 
 
 
 
 
 
 
459
  /**
460
  * Return type of dynamic cache processing
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
  }
471
 
472
-
473
  /**
474
- * Get and cache data for a given post ID
 
 
 
 
 
 
 
 
 
 
 
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
  /**
485
- * Reserve cache processing for a given post ID
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
 
@@ -511,9 +925,9 @@ SNS_Count_Cache::get_instance();
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();
@@ -528,12 +942,30 @@ function get_scc( $options = array( 'id' => '', 'url' => '', '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 {
@@ -542,13 +974,21 @@ function get_scc( $options = array( 'id' => '', 'url' => '', 'sns' => '' ) ) {
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 {
@@ -556,31 +996,49 @@ function get_scc( $options = array( 'id' => '', 'url' => '', 'sns' => '' ) ) {
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;
@@ -588,68 +1046,272 @@ function get_scc( $options = array( 'id' => '', 'url' => '', 'sns' => '' ) ) {
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
  /**
606
  * Get share count from cache (Twitter)
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
 
617
  /**
618
  * Get share count from cache (Facebook)
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
 
629
  /**
630
  * Get share count from cache (Google Plus)
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
  }
654
 
655
  ?>
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.4.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-cache-engine.php' );
33
+
34
+ require_once ( dirname( __FILE__ ) . '/includes/class-share-base-cache-engine.php' );
35
+ require_once ( dirname( __FILE__ ) . '/includes/class-share-rush-cache-engine.php' );
36
+ require_once ( dirname( __FILE__ ) . '/includes/class-share-lazy-cache-engine.php' );
37
+ require_once ( dirname( __FILE__ ) . '/includes/class-share-second-cache-engine.php' );
38
+ require_once ( dirname( __FILE__ ) . '/includes/class-share-rescue-cache-engine.php' );
39
+
40
+ require_once ( dirname( __FILE__ ) . '/includes/class-follow-base-cache-engine.php' );
41
+ require_once ( dirname( __FILE__ ) . '/includes/class-follow-lazy-cache-engine.php' );
42
+ require_once ( dirname( __FILE__ ) . '/includes/class-follow-second-cache-engine.php' );
43
+
44
  require_once ( dirname( __FILE__ ) . '/includes/class-data-crawler.php' );
45
+ require_once ( dirname( __FILE__ ) . '/includes/class-share-crawler.php' );
46
+ require_once ( dirname( __FILE__ ) . '/includes/class-follow-crawler.php' );
47
  require_once ( dirname( __FILE__ ) . '/includes/class-common-util.php' );
48
+ require_once ( dirname( __FILE__ ) . '/includes/class-wp-cron-util.php' );
49
 
50
+ require_once ( dirname( __FILE__ ) . '/includes/class-export-engine.php' );
51
+ require_once ( dirname( __FILE__ ) . '/includes/class-common-data-export-engine.php' );
52
 
53
+ require_once ( dirname( __FILE__ ) . '/includes/interface-cache-order.php' );
54
 
55
+ if ( ! class_exists( 'SNS_Count_Cache' ) ) {
56
+
57
+ class SNS_Count_Cache implements Cache_Order {
 
58
 
59
  /**
60
+ * Prefix of share cache ID
61
+ */
62
+ const OPT_SHARE_BASE_TRANSIENT_PREFIX = 'scc_share_count_';
 
 
 
 
 
 
 
 
 
 
63
 
64
+ /**
65
+ * Meta key for share second cache
66
+ */
67
+ const OPT_SHARE_2ND_META_KEY_PREFIX = 'scc_share_count_';
 
 
 
 
 
68
 
69
  /**
70
+ * Prefix of follow cache ID
71
  */
72
+ const OPT_FOLLOW_BASE_TRANSIENT_PREFIX = 'scc_follow_count_';
73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
  /**
75
+ * Meta key for follow second cache
76
  */
77
+ const OPT_FOLLOW_2ND_META_KEY_PREFIX = 'scc_follow_count_';
78
+
79
  /**
80
+ * Interval cheking and caching share count for share base cache
81
  */
82
+ const OPT_SHARE_BASE_CHECK_INTERVAL = 600;
83
 
84
  /**
85
+ * Number of posts to check at a time for share base cache
86
  */
87
+ const OPT_SHARE_BASE_POSTS_PER_CHECK = 20;
88
 
89
  /**
90
+ * Interval cheking and caching share count for share rush cache
91
+ */
92
+ const OPT_SHARE_RUSH_CHECK_INTERVAL = 300;
93
 
94
  /**
95
+ * Term that a content is considered as new content in share rush cache
96
  */
97
+ const OPT_SHARE_RUSH_NEW_CONTENT_TERM = 3;
 
 
 
 
 
 
 
 
 
 
98
 
99
  /**
100
+ * Interval for share second cache
101
+ */
102
+ const OPT_SHARE_2ND_CHECK_INTERVAL = 600;
103
+
104
  /**
105
+ * Interval cheking and caching share count for follow base cache
106
  */
107
+ const OPT_FOLLOW_BASE_CHECK_INTERVAL = 1800;
108
 
109
  /**
110
+ * Interval for follow second cache
111
+ */
112
+ const OPT_FOLLOW_2ND_CHECK_INTERVAL = 600;
113
 
114
  /**
115
+ * Type of data export
116
  */
117
+ const OPT_COMMON_DATA_EXPORT_MANUAL = 0;
118
 
119
  /**
120
+ * Type of data export
121
+ */
122
+ const OPT_COMMON_DATA_EXPORT_AUTO = 1;
123
 
124
+ /**
125
+ * File name of data export
126
+ */
127
+ const OPT_COMMON_DATA_EXPORT_FILE_NAME = 'sns-count-cache-data.csv';
128
 
 
 
 
 
 
129
  /**
130
+ * Data export interval
131
+ */
132
+ const OPT_COMMON_DATA_EXPORT_INTERVAL = 43200;
133
 
134
  /**
135
  * Type of dynamic cache processing
150
  * Type of dynamic cache processing
151
  */
152
  const OPT_ACCESS_BASED_2ND_CACHE = 3;
153
+
154
+ /**
155
+ * Option key for custom post types for share base cache
156
+ */
157
+ const DB_SHARE_CUSTOM_POST_TYPES = 'scc_custom_post_types';
158
 
159
  /**
160
+ * Option key for check interval of share base cache
161
  */
162
+ const DB_SHARE_CHECK_INTERVAL = 'scc_check_interval';
163
 
164
  /**
165
+ * Option key for number of posts to check at a time for share base cache
166
+ */
167
+ const DB_SHARE_POSTS_PER_CHECK = 'scc_posts_per_check';
168
+
169
+ /**
170
+ * Option key for dynamic cache
171
  */
172
+ const DB_COMMON_DYNAMIC_CACHE = 'scc_dynamic_cache';
173
 
174
  /**
175
+ * Option key for new content term for share rush cache
176
  */
177
+ const DB_SHARE_NEW_CONTENT_TERM = 'scc_new_content_term';
178
 
179
  /**
180
+ * Option key of cache target for share base cache
181
  */
182
+ const DB_SHARE_CACHE_TARGET = 'scc_cache_target';
183
 
184
  /**
185
+ * Option key of cache target for follow base cache
186
  */
187
+ const DB_FOLLOW_CACHE_TARGET = 'scc_follow_cache_target';
188
+
189
+ /**
190
+ * Option key of checking interval for follow base cache
191
+ */
192
+ const DB_FOLLOW_CHECK_INTERVAL = 'scc_follow_check_interval';
193
+
194
+ /**
195
+ * Option key of data export
196
+ */
197
+ const DB_COMMON_DATA_EXPORT = 'scc_data_export';
198
+
199
+ /**
200
+ * Option key of data export interval
201
+ */
202
+ const DB_COMMON_DATA_EXPORT_INTERVAL = 'scc_data_export_interval';
203
 
204
  /**
205
  * Slug of the plugin
206
  */
207
  const DOMAIN = 'sns-count-cache';
208
 
209
+ /**
210
+ * ID of share base cache
211
+ */
212
+ const REF_SHARE_BASE = 'share-base';
213
+
214
+ /**
215
+ * ID of share rush cache
216
+ */
217
+ const REF_SHARE_RUSH = 'share-rush';
218
+
219
+ /**
220
+ * ID of share lazy cache
221
+ */
222
+ const REF_SHARE_LAZY = 'share-lazy';
223
+
224
+ /**
225
+ * ID of share second cache
226
+ */
227
+ const REF_SHARE_2ND = 'share-second';
228
+
229
+ /**
230
+ * ID of share second cache
231
+ */
232
+ const REF_SHARE_RESCUE = 'share-rescue';
233
+
234
+ /**
235
+ * ID of follow base cache
236
+ */
237
+ const REF_FOLLOW_BASE = 'follow-base';
238
+
239
+ /**
240
+ * ID of follow lazy cache
241
+ */
242
+ const REF_FOLLOW_LAZY = 'follow-lazy';
243
+
244
+ /**
245
+ * ID of follow second cache
246
+ */
247
+ const REF_FOLLOW_2ND = 'follow-second';
248
+
249
+ /**
250
+ * ID of common data export
251
+ */
252
+ const REF_COMMON_EXPORT = 'common-export';
253
+
254
+ /**
255
+ * ID of share
256
+ */
257
+ const REF_SHARE = 'share';
258
+
259
+ /**
260
+ * ID of follow
261
+ */
262
+ const REF_FOLLOW = 'follow';
263
+
264
  /**
265
  * ID of share count (Twitter)
266
  */
267
+ const REF_SHARE_TWITTER = 'Twitter';
268
 
269
  /**
270
  * ID of share count (Facebook)
271
  */
272
+ const REF_SHARE_FACEBOOK = 'Facebook';
273
 
274
  /**
275
  * ID of share count (Google Plus)
276
  */
277
+ const REF_SHARE_GPLUS = 'Google+';
278
 
279
  /**
280
  * ID of share count (Hatena Bookmark)
281
  */
282
+ const REF_SHARE_HATEBU = 'Hatebu';
283
 
284
  /**
285
  * ID of share count (Pocket)
286
  */
287
+ const REF_SHARE_POCKET = 'Pocket';
288
 
289
+ /**
290
+ * ID of share count (Total)
291
+ */
292
+ const REF_SHARE_TOTAL = 'Total';
293
+
294
+ /**
295
+ * ID of follow count (Feedly)
296
+ */
297
+ const REF_FOLLOW_FEEDLY = 'Feedly';
298
+
299
+ /**
300
+ * ID of crawl date
301
+ */
302
+ const REF_CRAWL_DATE = 'CrawlDate';
303
+
304
+ /**
305
+ * Plugin version, used for cache-busting of style and script file references.
306
+ */
307
+ private $version = '0.4.0';
308
+
309
+ /**
310
+ * Instances of crawler
311
+ */
312
+ private $crawlers = array();
313
+
314
+ /**
315
+ * Instance of cache engine
316
+ */
317
+ private $cache_engines = array();
318
+
319
+ /**
320
+ * Instance of export engine
321
+ */
322
+ private $export_engines = array();
323
+
324
+ /**
325
+ * Slug of the plugin screen
326
+ */
327
+ private $plugin_screen_hook_suffix = array();
328
+
329
+ /**
330
+ * Cache target for share base cache
331
+ */
332
+ private $share_base_cache_target = array();
333
+
334
+ /**
335
+ * Cache target for follow base cache
336
+ */
337
+ private $follow_base_cache_target = array();
338
+
339
+ /**
340
+ * Post types to be cached
341
+ */
342
+ private $share_base_cache_post_types = array( 'post', 'page' );
343
+
344
+ /**
345
+ * Option flag of dynamic cache
346
+ */
347
+ private $dynamic_cache = 0;
348
+
349
+ /**
350
+ * Option flag of data export
351
+ */
352
+ private $data_export = 0;
353
+
354
  /**
355
  * Instance
356
  */
361
  * Hook onto all of the actions and filters needed by the plugin.
362
  */
363
  private function __construct() {
364
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
365
 
366
  //load_plugin_textdomain(self::DOMAIN, false, basename(dirname( __FILE__ )) . '/languages');
367
 
397
  * @since 0.1.1
398
  */
399
  public function initialize() {
400
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
401
 
402
+ $share_base_check_interval = get_option( self::DB_SHARE_CHECK_INTERVAL );
403
+ $share_base_check_interval = ! empty( $share_base_check_interval ) ? intval( $share_base_check_interval ) : self::OPT_SHARE_BASE_CHECK_INTERVAL;
404
 
405
+ $share_base_posts_per_check = get_option( self::DB_SHARE_POSTS_PER_CHECK );
406
+ $share_base_posts_per_check = ! empty( $share_base_posts_per_check ) ? intval( $share_base_posts_per_check ) : self::OPT_SHARE_BASE_POSTS_PER_CHECK;
407
 
408
+ $follow_base_check_interval = get_option( self::DB_FOLLOW_CHECK_INTERVAL );
409
+ $follow_base_check_interval = ! empty( $follow_base_check_interval ) ? intval( $follow_base_check_interval ) : self::OPT_FOLLOW_BASE_CHECK_INTERVAL;
410
+
411
+ $dynamic_cache = get_option( self::DB_COMMON_DYNAMIC_CACHE );
412
  $this->dynamic_cache = ! empty( $dynamic_cache ) ? $dynamic_cache : false;
413
 
414
+ $share_rush_new_content_term = get_option( self::DB_SHARE_NEW_CONTENT_TERM );
415
+ $share_rush_new_content_term = ! empty( $share_rush_new_content_term ) ? intval( $share_rush_new_content_term ) : self::OPT_SHARE_RUSH_NEW_CONTENT_TERM;
416
 
417
+ $this->share_base_cache_target = get_option( self::DB_SHARE_CACHE_TARGET );
418
+ $this->follow_base_cache_target = get_option( self::DB_FOLLOW_CACHE_TARGET );
419
 
420
+ $data_export = get_option( self::DB_COMMON_DATA_EXPORT );
421
+ $this->data_export = isset( $data_export ) ? intval( $data_export ) : self::OPT_COMMON_DATA_EXPORT_MANUAL;
422
+
423
+ $data_export_interval = get_option( self::DB_COMMON_DATA_EXPORT_INTERVAL );
424
+ $data_export_interval = ! empty( $data_export_interval ) ? intval( $data_export_interval ) : self::OPT_COMMON_DATA_EXPORT_INTERVAL;
425
+
426
+ if ( ! $this->share_base_cache_target ) {
427
+ $this->share_base_cache_target[self::REF_SHARE_TWITTER] = true;
428
+ $this->share_base_cache_target[self::REF_SHARE_GPLUS] = true;
429
+ if ( Common_Util::extension_loaded_php_xml() ) {
430
+ $this->share_base_cache_target[self::REF_SHARE_FACEBOOK] = true;
431
+ $this->share_base_cache_target[self::REF_SHARE_POCKET] = true;
432
  }
433
+ $this->share_base_cache_target[self::REF_SHARE_HATEBU] = true;
434
+ }
435
 
436
+ $this->share_base_cache_target[self::REF_CRAWL_DATE] = true;
437
+ $this->share_base_cache_target[self::REF_SHARE_TOTAL] = true;
438
+
439
+ if ( ! $this->follow_base_cache_target ) {
440
+ $this->follow_base_cache_target[self::REF_FOLLOW_FEEDLY] = true;
441
+ }
442
+
443
+ $share_base_custom_post_types = get_option( self::DB_SHARE_CUSTOM_POST_TYPES );
444
+
445
+ if ( ! $share_base_custom_post_types ) {
446
+ $share_base_custom_post_types = array();
447
+ }
448
+
449
+ //$custom_post_types = Common_Util::get_custom_post_types();
450
+ $this->share_base_cache_post_types = array_merge( $this->share_base_cache_post_types, $share_base_custom_post_types );
451
+ //$custom_post_types = get_post_types( array( 'public' => true, '_builtin' => false ) );
452
+ //$this->share_base_cache_post_types = array_merge( $this->share_base_cache_post_types, $share_base_custom_post_types );
453
+
454
+ // Crawler
455
+ $this->crawlers[self::REF_SHARE] = Share_Crawler::get_instance();
456
+ $this->crawlers[self::REF_FOLLOW] = Follow_Crawler::get_instance();
457
+
458
+ // Share base cache engine
459
+ $options = array(
460
+ 'delegate' => $this,
461
+ 'crawler' => $this->crawlers[self::REF_SHARE],
462
+ 'target_sns' => $this->share_base_cache_target,
463
+ 'check_interval' => $share_base_check_interval,
464
+ 'posts_per_check' => $share_base_posts_per_check,
465
+ 'post_types' => $this->share_base_cache_post_types
466
+ );
467
 
468
+ $this->cache_engines[self::REF_SHARE_BASE] = Share_Base_Cache_Engine::get_instance();
469
+ $this->cache_engines[self::REF_SHARE_BASE]->initialize( $options );
470
 
471
+ // Share rush cache engine
472
  $options = array(
473
+ 'delegate' => $this,
474
+ 'crawler' => $this->crawlers[self::REF_SHARE],
475
+ 'target_sns' => $this->share_base_cache_target,
476
+ 'check_interval' => self::OPT_SHARE_RUSH_CHECK_INTERVAL,
477
+ 'posts_per_check' => $share_base_posts_per_check,
478
+ 'new_content_term' => $share_rush_new_content_term,
479
+ 'post_types' => $this->share_base_cache_post_types
 
 
 
 
 
 
 
 
 
 
 
 
 
480
  );
481
+
482
+ $this->cache_engines[self::REF_SHARE_RUSH] = Share_Rush_Cache_Engine::get_instance();
483
+ $this->cache_engines[self::REF_SHARE_RUSH]->initialize( $options );
484
 
485
+ // Share lazy cache engine
486
+ $options = array(
487
+ 'delegate' => $this,
488
+ 'crawler' => $this->crawlers[self::REF_SHARE],
489
+ 'target_sns' => $this->share_base_cache_target,
490
+ 'check_interval' => $share_base_check_interval,
491
+ 'posts_per_check' => $share_base_posts_per_check,
492
+ 'post_types' => $this->share_base_cache_post_types
493
+ );
494
+
495
+ $this->cache_engines[self::REF_SHARE_LAZY] = Share_Lazy_Cache_Engine::get_instance();
496
+ $this->cache_engines[self::REF_SHARE_LAZY]->initialize( $options );
497
+
498
+ // Share second cache engine
499
+ $options = array(
500
+ 'target_sns' => $this->share_base_cache_target,
501
+ 'check_interval' => self::OPT_SHARE_2ND_CHECK_INTERVAL,
502
+ 'post_types' => $this->share_base_cache_post_types,
503
+ 'meta_key_prefix' => self::OPT_SHARE_2ND_META_KEY_PREFIX
504
+ );
505
+
506
+ $this->cache_engines[self::REF_SHARE_2ND] = Share_Second_Cache_Engine::get_instance();
507
+ $this->cache_engines[self::REF_SHARE_2ND]->initialize( $options );
508
+
509
+ // Share rescue cache engine
510
+ $options = array(
511
+ 'delegate' => $this,
512
+ 'crawler' => $this->crawlers[self::REF_SHARE],
513
+ 'target_sns' => $this->share_base_cache_target,
514
+ 'check_interval' => $share_base_check_interval,
515
+ 'posts_per_check' => $share_base_posts_per_check,
516
+ 'post_types' => $this->share_base_cache_post_types
517
+ );
518
+
519
+ $this->cache_engines[self::REF_SHARE_RESCUE] = Share_Rescue_Cache_Engine::get_instance();
520
+ $this->cache_engines[self::REF_SHARE_RESCUE]->initialize( $options );
521
+
522
+ // Follow base cache engine
523
+ $options = array(
524
+ 'crawler' => $this->crawlers[self::REF_FOLLOW],
525
+ 'target_sns' => $this->follow_base_cache_target,
526
+ 'check_interval' => $follow_base_check_interval,
527
+ 'post_types' => $this->share_base_cache_post_types
528
+ );
529
+
530
+ $this->cache_engines[self::REF_FOLLOW_BASE] = Follow_Base_Cache_Engine::get_instance();
531
+ $this->cache_engines[self::REF_FOLLOW_BASE]->initialize( $options );
532
+
533
+ // Follow lazy cache engine
534
+ $options = array(
535
+ 'crawler' => $this->crawlers[self::REF_FOLLOW],
536
+ 'target_sns' => $this->follow_base_cache_target,
537
+ 'check_interval' => $follow_base_check_interval,
538
+ );
539
+
540
+ $this->cache_engines[self::REF_FOLLOW_LAZY] = Follow_Lazy_Cache_Engine::get_instance();
541
+ $this->cache_engines[self::REF_FOLLOW_LAZY]->initialize( $options );
542
+
543
+ // Follow second cache engine
544
+ $options = array(
545
+ 'crawler' => $this->crawlers[self::REF_FOLLOW],
546
+ 'target_sns' => $this->follow_base_cache_target,
547
+ 'check_interval' => self::OPT_FOLLOW_2ND_CHECK_INTERVAL,
548
+ 'meta_key_prefix' => self::OPT_FOLLOW_2ND_META_KEY_PREFIX
549
+ );
550
+
551
+ $this->cache_engines[self::REF_FOLLOW_2ND] = Follow_Second_Cache_Engine::get_instance();
552
+ $this->cache_engines[self::REF_FOLLOW_2ND]->initialize( $options );
553
+
554
+ // Data export engine
555
+ $options = array(
556
+ 'export_activation' => $this->data_export,
557
+ 'export_interval' => $data_export_interval,
558
+ 'share_target_sns' => $this->share_base_cache_target,
559
+ 'follow_target_sns' => $this->follow_base_cache_target,
560
+ 'export_file_name' => self::OPT_COMMON_DATA_EXPORT_FILE_NAME,
561
+ 'export_exclude_keys' => array( self::REF_SHARE_TOTAL, self::REF_CRAWL_DATE ),
562
+ 'post_types' => $this->share_base_cache_post_types
563
+ );
564
+
565
+ $this->export_engines[self::REF_COMMON_EXPORT] = Common_Data_Export_Engine::get_instance();
566
+ $this->export_engines[self::REF_COMMON_EXPORT]->initialize( $options );
567
+
568
+ // delete old hooks
569
+ WP_Cron_Util::clear_scheduled_hook( 'scc_basecache_prime' );
570
+ WP_Cron_Util::clear_scheduled_hook( 'scc_rushcache_prime' );
571
+ WP_Cron_Util::clear_scheduled_hook( 'scc_2ndcache_prime' );
572
  }
573
 
574
  /**
577
  * @since 0.1.0
578
  */
579
  public function register_admin_styles() {
580
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
581
 
582
  if ( ! isset( $this->plugin_screen_hook_suffix ) ) {
583
  return;
585
 
586
  $screen = get_current_screen();
587
 
588
+ if ( in_array( $screen->id, $this->plugin_screen_hook_suffix ) ) {
589
  wp_enqueue_style( self::DOMAIN .'-admin-style-1' , plugins_url( ltrim( '/css/sns-count-cache.css', '/' ), __FILE__) );
590
  wp_enqueue_style( self::DOMAIN .'-admin-style-2' , plugins_url( ltrim( '/css/prettify.css', '/' ), __FILE__ ) );
591
+ }
 
592
  }
593
 
594
  /**
597
  * @since 0.1.0
598
  */
599
  public function register_admin_scripts() {
600
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
601
 
602
  if ( ! isset( $this->plugin_screen_hook_suffix ) ) {
603
  return;
605
 
606
  $screen = get_current_screen();
607
 
608
+ if ( in_array( $screen->id, $this->plugin_screen_hook_suffix ) ) {
609
  wp_enqueue_script( self::DOMAIN . '-admin-script-1' , plugins_url( ltrim( '/js/jquery.sns-count-cache.js', '/' ) , __FILE__ ), array( 'jquery' ) );
610
  wp_enqueue_script( self::DOMAIN . '-admin-script-2' , plugins_url( ltrim( '/js/prettify.js', '/' ) , __FILE__ ), array( 'jquery' ) );
611
  }
 
612
  }
613
 
614
  /**
615
+ * Activate cache engine (schedule cron)
616
  *
617
  * @since 0.1.1
618
  */
619
  function activate_plugin() {
620
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
621
 
622
  $this->initialize();
623
+
624
+ foreach ( $this->cache_engines as $key => $cache_engine ) {
625
+ $cache_engine->initialize_cache();
626
+ $cache_engine->register_schedule();
627
+ }
628
+
629
+ if ( $this->data_export ) {
630
+ $this->export_engines[self::REF_COMMON_EXPORT]->register_schedule();
631
+ }
632
  }
633
 
634
  /**
635
+ * Deactivate cache engine (schedule cron)
636
  *
637
  * @since 0.1.1
638
  */
639
  function deactivate_plugin() {
640
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
641
+
642
+ foreach ( $this->cache_engines as $key => $cache_engine ) {
643
+ $cache_engine->unregister_schedule();
644
+ $cache_engine->clear_cache();
645
+ }
646
+
647
+ $this->export_engines[self::REF_COMMON_EXPORT]->unregister_schedule();
648
  }
649
 
650
  /**
653
  * @since 0.1.1
654
  */
655
  function reactivate_plugin() {
656
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
657
+
658
+ foreach ( $this->cache_engines as $key => $cache_engine ) {
659
+ $cache_engine->unregister_schedule();
660
+ $cache_engine->clear_cache();
661
+ }
662
+
663
+ $this->export_engines[self::REF_COMMON_EXPORT]->unregister_schedule();
664
 
 
665
  $this->initialize();
666
+
667
+ foreach ( $this->cache_engines as $key => $cache_engine ) {
668
+ $cache_engine->initialize_cache();
669
+ $cache_engine->register_schedule();
670
+ }
671
+
672
+ if ( $this->data_export ) {
673
+ $this->export_engines[self::REF_COMMON_EXPORT]->register_schedule();
674
+ }
675
  }
676
 
677
  /**
682
  * @since 0.1.0
683
  */
684
  public function action_admin_menu() {
685
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
686
+
687
+ $this->plugin_screen_hook_suffix[] = add_menu_page( 'SNS Count Cache', 'SNS Count Cache', 8, 'scc-dashboard', array( $this, 'dashboard_page' ), 'dashicons-share' );
688
+ $this->plugin_screen_hook_suffix[] = add_submenu_page( 'scc-dashboard', 'Dashboard', 'Dashboard', 8, 'scc-dashboard', array( $this, 'dashboard_page' ) );
689
+ $this->plugin_screen_hook_suffix[] = add_submenu_page( 'scc-dashboard', 'Cache Status', 'Cache Status', 8, 'scc-cache-status', array( $this, 'cache_status_page' ) );
690
+ $this->plugin_screen_hook_suffix[] = add_submenu_page( 'scc-dashboard', 'Share Count', 'Share Count', 8, 'scc-share-count', array( $this, 'share_count_page' ) );
691
+ $this->plugin_screen_hook_suffix[] = add_submenu_page( 'scc-dashboard', 'Setting', 'Setting', 8, 'scc-setting', array( $this, 'setting_page' ) );
692
+ $this->plugin_screen_hook_suffix[] = add_submenu_page( 'scc-dashboard', 'Help', 'Help', 8, 'scc-help', array( $this, 'help_page' ) );
693
  }
694
+
695
+ /**
696
  * Option page implementation
697
  *
698
  * @since 0.1.0
699
+ */
700
+ public function dashboard_page() {
701
+ include_once( dirname( __FILE__ ) . '/includes/admin-dashboard.php' );
702
+ }
703
 
704
+
705
+ /**
706
+ * Option page implementation
707
+ *
708
+ * @since 0.1.0
709
+ */
710
+ public function cache_status_page() {
711
+ include_once( dirname( __FILE__ ) . '/includes/admin-cache-status.php' );
712
+ }
713
+
714
+ /**
715
+ * Option page implementation
716
+ *
717
+ * @since 0.1.0
718
+ */
719
+ public function share_count_page() {
720
+ include_once( dirname( __FILE__ ) . '/includes/admin-share-count.php' );
721
+ }
722
+
723
+ /**
724
+ * Option page implementation
725
+ *
726
  * @since 0.1.0
727
  */
728
+ public function setting_page() {
729
+ include_once( dirname( __FILE__ ) . '/includes/admin-setting.php' );
 
 
 
 
 
 
730
  }
731
+
732
+ /**
733
+ * Option page implementation
734
+ *
735
+ * @since 0.1.0
736
+ */
737
+ public function help_page() {
738
+ include_once( dirname( __FILE__ ) . '/includes/admin-help.php' );
739
+ }
740
+
741
  /**
742
  * Return type of dynamic cache processing
743
  *
744
  * @since 0.2.0
745
  */
746
  public function get_dynamic_cache_type() {
747
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
748
+
 
 
749
  return $this->dynamic_cache;
750
  }
751
 
 
752
  /**
753
+ * Cache share count for a given post ID
754
+ *
755
+ * @since 0.2.0
756
+ */
757
+ public function retrieve_share_cache( $post_ID ) {
758
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
759
+
760
+ return $this->cache_engines[self::REF_SHARE_BASE]->direct_cache( $post_ID, false );
761
+ }
762
+
763
+ /**
764
+ * Cache follow count
765
  *
766
  * @since 0.2.0
767
  */
768
+ public function retrieve_follow_cache() {
769
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
770
 
771
+ return $this->cache_engines[self::REF_FOLLOW_BASE]->direct_cache();
772
  }
773
 
774
  /**
775
+ * Reserve cache processing of share count for a given post ID
776
  *
777
  * @since 0.2.0
778
  */
779
+ public function reserve_share_cache( $post_ID ) {
780
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
781
 
782
+ $this->cache_engines[self::REF_SHARE_LAZY]->prime_cache( $post_ID );
783
  }
784
 
785
  /**
786
+ * Reserve cache processing of follow count
787
+ *
788
+ * @since 0.4.0
789
+ */
790
+ public function reserve_follow_cache() {
791
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
792
+
793
+ $this->cache_engines[self::REF_FOLLOW_LAZY]->prime_cache();
794
+ }
795
+
796
+ /**
797
+ * Return cache target of share count
798
  *
799
  * @since 0.2.0
800
  */
801
+ public function get_share_base_cache_target() {
802
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
803
+
804
+ return $this->share_base_cache_target;
805
+ }
806
+
807
+ /**
808
+ * Return cache target of follow count
809
+ *
810
+ * @since 0.4.0
811
+ */
812
+ public function get_follow_base_cache_target() {
813
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
814
 
815
+ return $this->follow_base_cache_target;
816
+ }
817
+
818
+ /**
819
+ * Method call between one cache engine and another
820
+ *
821
+ * @since 0.4.0
822
+ */
823
+ public function order_cache( Cache_Engine $engine, $options = array() ) {
824
+ Common_Util::log( '[' . __METHOD__ . '] (line='. __LINE__ . ')' );
825
+
826
+ switch ( get_class( $engine ) ) {
827
+ case 'Share_Lazy_Cache_Engine':
828
+ $this->cache_engines[self::REF_SHARE_2ND]->cache( $options['post_id'], $this->share_base_cache_target, NULL );
829
+ break;
830
+ case 'Share_Rescue_Cache_Engine':
831
+ $this->cache_engines[self::REF_SHARE_2ND]->cache( $options['post_id'], $this->share_base_cache_target, NULL );
832
+ break;
833
+ case 'Share_Base_Cache_Engine':
834
+ $this->cache_engines[self::REF_SHARE_2ND]->cache( $options['post_id'], $this->share_base_cache_target, NULL );
835
+ break;
836
+ case 'Share_Rush_Cache_Engine':
837
+ $this->cache_engines[self::REF_SHARE_2ND]->cache( $options['post_id'], $this->share_base_cache_target, NULL );
838
+ break;
839
+ }
840
+
841
  }
842
+
843
+ private function pagination( $numpages = '', $pagerange = '', $paged='', $inherit_param = true ) {
844
+
845
+ if ( empty( $pagerange ) ) {
846
+ $pagerange = 2;
847
+ }
848
+
849
+ if ( $paged == '' ) {
850
+ global $paged;
851
+
852
+ if ( empty( $paged ) ) {
853
+ $paged = 1;
854
+ }
855
+ }
856
+
857
+ if ( $numpages == '' ) {
858
+ global $wp_query;
859
+
860
+ $numpages = $wp_query->max_num_pages;
861
+
862
+ if( ! $numpages ) {
863
+ $numpages = 1;
864
+ }
865
+ }
866
+
867
+ if ( $inherit_param ) {
868
+ $pagination_args = array(
869
+ 'base' => get_pagenum_link(1) . '%_%',
870
+ 'format' => '&paged=%#%',
871
+ 'total' => $numpages,
872
+ 'current' => $paged,
873
+ 'show_all' => False,
874
+ 'end_size' => 1,
875
+ 'mid_size' => $pagerange,
876
+ 'prev_next' => True,
877
+ 'prev_text' => __('&laquo;'),
878
+ 'next_text' => __('&raquo;'),
879
+ 'type' => 'plain',
880
+ 'add_args' => false,
881
+ 'add_fragment' => ''
882
+ );
883
+ } else {
884
+
885
+ $url = parse_url( get_pagenum_link(1) );
886
+ $base_url = $url['scheme'] . '://' . $url['host'] . $url['path'];
887
+
888
+ parse_str ( $url['query'], $query );
889
+
890
+ $base_url = $base_url . '?page=' . $query['page'];
891
 
892
+ $pagination_args = array(
893
+ 'base' => $base_url . '%_%',
894
+ 'format' => '&paged=%#%',
895
+ 'total' => $numpages,
896
+ 'current' => $paged,
897
+ 'show_all' => False,
898
+ 'end_size' => 1,
899
+ 'mid_size' => $pagerange,
900
+ 'prev_next' => True,
901
+ 'prev_text' => __('&laquo;'),
902
+ 'next_text' => __('&raquo;'),
903
+ 'type' => 'plain',
904
+ 'add_args' => false,
905
+ 'add_fragment' => ''
906
+ );
907
+
908
+ }
909
+
910
+ $paginate_links = paginate_links($pagination_args);
911
+
912
+ if ( $paginate_links ) {
913
+ echo "<nav class='pagination'>";
914
+ echo "<span class='page-numbers page-num'>Page " . $paged . " of " . $numpages . "</span> ";
915
+ echo $paginate_links;
916
+ echo "</nav>";
917
+ }
918
+
919
+ }
920
 
921
  }
922
 
925
  /**
926
  * Get share count from cache
927
  *
928
+ * @since 0.4.0
929
  */
930
+ function scc_get_share( $options = array( 'id' => '', 'url' => '', 'sns' => '' ) ) {
931
  $transient_ID ='';
932
  $sns_key = '';
933
  $sns_counts = array();
942
  $sns_key = $options['sns'];
943
  }
944
 
945
+ $transient_ID = SNS_Count_Cache::OPT_SHARE_BASE_TRANSIENT_PREFIX . $post_ID;
946
 
947
  if ( false !== ( $sns_counts = get_transient( $transient_ID ) ) ) {
948
  if ( $sns_key ) {
949
+
950
+ $sns_count = $sns_counts[$sns_key];
951
+
952
+ if ( isset( $sns_count ) && $sns_count >= 0 ) {
953
+ $sns_counts[$sns_key] = $sns_count;
954
+ } else {
955
+ $sns_counts[$sns_key] = 0;
956
+ }
957
+
958
  return $sns_counts[$sns_key];
959
  } else {
960
+
961
+ foreach ( $sns_counts as $key => $value ) {
962
+ if ( isset( $value ) && $value >= 0 ) {
963
+ $sns_counts[$key] = $value;
964
+ } else {
965
+ $sns_counts[$key] = 0;
966
+ }
967
+ }
968
+
969
  return $sns_counts;
970
  }
971
  } else {
974
  switch ( $sns_count_cache->get_dynamic_cache_type() ) {
975
  case SNS_Count_Cache::OPT_ACCESS_BASED_CACHE_NONE:
976
  if ( $sns_key ) {
977
+ $sns_counts[$sns_key] = 0;
978
  return $sns_counts[$sns_key];
979
+ } else {
980
+ $base_cache_target = $sns_count_cache->get_share_base_cache_target();
981
+
982
+ foreach ( $base_cache_target as $key => $value ) {
983
+ if ( $value ) {
984
+ $sns_counts[$key] = 0;
985
+ }
986
+ }
987
+ return $sns_counts;
988
  }
989
  break;
990
  case SNS_Count_Cache::OPT_ACCESS_BASED_SYNC_CACHE:
991
+ $sns_counts = $sns_count_cache->retrieve_share_cache( $post_ID );
992
  if ( $sns_key ) {
993
  return $sns_counts[$sns_key];
994
  } else {
996
  }
997
  break;
998
  case SNS_Count_Cache::OPT_ACCESS_BASED_ASYNC_CACHE:
999
+ $sns_count_cache->reserve_share_cache( $post_ID );
1000
  if ( $sns_key ) {
1001
+ $sns_counts[$sns_key] = 0;
1002
  return $sns_counts[$sns_key];
1003
+ } else {
1004
+ $base_cache_target = $sns_count_cache->get_share_base_cache_target();
1005
+
1006
+ foreach ( $base_cache_target as $key => $value ) {
1007
+ if ( $value ) {
1008
+ $sns_counts[$key] = 0;
1009
+ }
1010
+ }
1011
+ return $sns_counts;
1012
+ }
1013
  break;
1014
  case SNS_Count_Cache::OPT_ACCESS_BASED_2ND_CACHE:
1015
+ $sns_count_cache->reserve_share_cache( $post_ID );
1016
  if ( $sns_key ) {
1017
+ $meta_key = SNS_Count_Cache::OPT_SHARE_2ND_META_KEY_PREFIX . strtolower( $sns_key );
1018
+
1019
+ $sns_count = get_post_meta( $post_ID, $meta_key, true );
1020
+
1021
+ if ( isset( $sns_count ) && $sns_count >= 0) {
1022
+ $sns_counts[$sns_key] = $sns_count;
1023
+ } else {
1024
+ $sns_counts[$sns_key] = 0;
1025
+ }
1026
+
1027
  return $sns_counts[$sns_key];
1028
  } else {
1029
+ $base_cache_target = $sns_count_cache->get_share_base_cache_target();
1030
 
1031
  foreach ( $base_cache_target as $key => $value ) {
1032
  if ( $value ) {
1033
+ $meta_key = SNS_Count_Cache::OPT_SHARE_2ND_META_KEY_PREFIX . strtolower( $key );
1034
 
1035
  $sns_count = get_post_meta( $post_ID, $meta_key, true );
1036
 
1037
  if ( isset( $sns_count ) && $sns_count >= 0 ) {
1038
+ $sns_counts[$key] = $sns_count;
1039
+ } else {
1040
+ $sns_counts[$key] = 0;
1041
+ }
1042
  }
1043
  }
1044
  return $sns_counts;
1046
  break;
1047
  }
1048
  }
1049
+ }
1050
+
1051
+
1052
+ /**
1053
+ * Get follow count from cache
1054
+ *
1055
+ * @since 0.4.0
1056
+ */
1057
+ function scc_get_follow( $options = array( 'id' => '', 'sns' => '' ) ) {
1058
+ $transient_ID ='';
1059
+ $sns_key = '';
1060
+ $sns_followers = array();
1061
+
1062
+ if ( $options['id'] ) {
1063
+ $post_ID = $options['id'];
1064
+ } else {
1065
+ $post_ID = get_the_ID();
1066
+ }
1067
+
1068
+ if ( $options['sns'] ) {
1069
+ $sns_key = $options['sns'];
1070
+ }
1071
+
1072
+ $transient_ID = SNS_Count_Cache::OPT_FOLLOW_BASE_TRANSIENT_PREFIX . 'follow';
1073
+
1074
+ if ( false !== ( $sns_followers = get_transient( $transient_ID ) ) ) {
1075
+ if ( $sns_key ) {
1076
+ $sns_follower = $sns_followers[$sns_key];
1077
+
1078
+ if ( isset( $sns_follower ) && $sns_follower >= 0 ){
1079
+ $sns_followers[$sns_key] = $sns_follower;
1080
+ } else {
1081
+ $sns_followers[$sns_key] = 0;
1082
+ }
1083
+
1084
+ return $sns_followers[$sns_key];
1085
+ } else {
1086
+ foreach ( $sns_followers as $key => $value ) {
1087
+ if ( isset( $value ) && $value >= 0 ) {
1088
+ $sns_followers[$key] = $value;
1089
+ } else {
1090
+ $sns_followers[$key] = 0;
1091
+ }
1092
+ }
1093
+
1094
+ return $sns_followers;
1095
+ }
1096
+ } else {
1097
+ $sns_count_cache = SNS_Count_Cache::get_instance();
1098
+
1099
+ switch ( $sns_count_cache->get_dynamic_cache_type() ) {
1100
+ case SNS_Count_Cache::OPT_ACCESS_BASED_CACHE_NONE:
1101
+ if ( $sns_key ) {
1102
+ $sns_followers[$sns_key] = 0;
1103
+ return $sns_followers[$sns_key];
1104
+ } else {
1105
+ $base_cache_target = $sns_count_cache->get_follow_base_cache_target();
1106
+
1107
+ foreach ( $base_cache_target as $key => $value ) {
1108
+ if ( $value ) {
1109
+ $sns_followers[$key] = 0;
1110
+ }
1111
+ }
1112
+
1113
+ return $sns_followers;
1114
+ }
1115
+ break;
1116
+ case SNS_Count_Cache::OPT_ACCESS_BASED_SYNC_CACHE:
1117
+ $sns_followers = $sns_count_cache->retrieve_follow_cache();
1118
+ if ( $sns_key ) {
1119
+ return $sns_followers[$sns_key];
1120
+ } else {
1121
+ return $sns_followers;
1122
+ }
1123
+ break;
1124
+ case SNS_Count_Cache::OPT_ACCESS_BASED_ASYNC_CACHE:
1125
+ $sns_count_cache->reserve_follow_cache();
1126
+ if ( $sns_key ) {
1127
+ $sns_followers[$sns_key] = 0;
1128
+ return $sns_followers[$sns_key];
1129
+ } else {
1130
+ $base_cache_target = $sns_count_cache->get_follow_base_cache_target();
1131
+
1132
+ foreach ( $base_cache_target as $key => $value ) {
1133
+ if ( $value ) {
1134
+ $sns_followers[$key] = 0;
1135
+ }
1136
+ }
1137
+ return $sns_followers;
1138
+ }
1139
+ break;
1140
+ case SNS_Count_Cache::OPT_ACCESS_BASED_2ND_CACHE:
1141
+ $sns_count_cache->reserve_follow_cache();
1142
+
1143
+ if ( $sns_key ) {
1144
+ $meta_key = SNS_Count_Cache::OPT_FOLLOW_2ND_META_KEY_PREFIX . strtolower( $sns_key );
1145
+
1146
+ $sns_follower = get_post_meta( $post_ID, $meta_key, true );
1147
+
1148
+ if ( isset( $sns_follower ) && $sns_follower >= 0 ) {
1149
+ $sns_followers[$sns_key] = $sns_follower;
1150
+ } else {
1151
+ $sns_followers[$sns_key] = 0;
1152
+ }
1153
+
1154
+ return $sns_followers[$sns_key];
1155
+ } else {
1156
+ $base_cache_target = $sns_count_cache->get_follow_base_cache_target();
1157
+
1158
+ foreach ( $base_cache_target as $key => $value ) {
1159
+ if ( $value ) {
1160
+ $meta_key = SNS_Count_Cache::OPT_FOLLOW_2ND_META_KEY_PREFIX . strtolower( $key );
1161
+
1162
+ $sns_follower = get_post_meta( $post_ID, $meta_key, true );
1163
+
1164
+ if ( isset( $sns_follower ) && $sns_follower >= 0 ) {
1165
+ $sns_followers[$key] = $sns_follower;
1166
+ } else {
1167
+ $sns_followers[$key] = 0;
1168
+ }
1169
+ }
1170
+ }
1171
+ return $sns_followers;
1172
+ }
1173
+ break;
1174
+ }
1175
+ }
1176
  }
1177
+
1178
+
1179
+ /**
1180
+ * Get share count from cache (Hatena Bookmark).
1181
+ *
1182
+ * @since 0.4.0
1183
+ */
1184
+ function scc_get_share_hatebu( $options = array( 'id' => '', 'url' => '' ) ) {
1185
+
1186
+ $options['sns'] = SNS_Count_Cache::REF_SHARE_HATEBU;
1187
+ return scc_get_share( $options );
1188
+ }
1189
+
1190
+ /**
1191
+ * Get share count from cache (Twitter)
1192
+ *
1193
+ * @since 0.4.0
1194
+ */
1195
+ function scc_get_share_twitter( $options = array( 'id' => '', 'url' => '' ) ) {
1196
+
1197
+ $options['sns'] = SNS_Count_Cache::REF_SHARE_TWITTER;
1198
+ return scc_get_share( $options );
1199
+ }
1200
+
1201
+ /**
1202
+ * Get share count from cache (Facebook)
1203
+ *
1204
+ * @since 0.4.0
1205
+ */
1206
+ function scc_get_share_facebook( $options = array( 'id' => '', 'url' => '' ) ) {
1207
+
1208
+ $options['sns'] = SNS_Count_Cache::REF_SHARE_FACEBOOK;
1209
+ return scc_get_share( $options );
1210
+ }
1211
+
1212
+ /**
1213
+ * Get share count from cache (Google Plus)
1214
+ *
1215
+ * @since 0.4.0
1216
+ */
1217
+ function scc_get_share_gplus( $options = array( 'id' => '', 'url' => '' ) ) {
1218
+
1219
+ $options['sns'] = SNS_Count_Cache::REF_SHARE_GPLUS;
1220
+ return scc_get_share( $options );
1221
+ }
1222
+
1223
+ /**
1224
+ * Get share count from cache (Pocket)
1225
+ *
1226
+ * @since 0.4.0
1227
+ */
1228
+ function scc_get_share_pocket( $options = array( 'id' => '', 'url' => '' ) ) {
1229
+
1230
+ $options['sns'] = SNS_Count_Cache::REF_SHARE_POCKET;
1231
+ return scc_get_share( $options );
1232
+
1233
+ }
1234
+
1235
+ /**
1236
+ * Get share count from cache (Pocket)
1237
+ *
1238
+ * @since 0.4.0
1239
+ */
1240
+ function scc_get_share_total( $options = array( 'id' => '', 'url' => '' ) ) {
1241
+
1242
+ $options['sns'] = SNS_Count_Cache::REF_SHARE_TOTAL;
1243
+ return scc_get_share( $options );
1244
+ }
1245
 
1246
  /**
1247
  * Get share count from cache (Hatena Bookmark).
1248
  *
1249
  * @since 0.1.0
1250
+ * @deprecated Function deprecated in Release 0.4.0
1251
  */
1252
  function get_scc_hatebu( $options = array( 'id' => '', 'url' => '' ) ) {
1253
 
1254
+ return scc_get_share_hatebu( $options );
 
 
1255
  }
1256
 
1257
  /**
1258
  * Get share count from cache (Twitter)
1259
  *
1260
  * @since 0.1.0
1261
+ * @deprecated Function deprecated in Release 0.4.0
1262
  */
1263
  function get_scc_twitter( $options = array( 'id' => '', 'url' => '' ) ) {
1264
 
1265
+ return scc_get_share_twitter( $options );
 
 
1266
  }
1267
 
1268
  /**
1269
  * Get share count from cache (Facebook)
1270
  *
1271
  * @since 0.1.0
1272
+ * @deprecated Function deprecated in Release 0.4.0
1273
  */
1274
  function get_scc_facebook( $options = array( 'id' => '', 'url' => '' ) ) {
1275
 
1276
+ return scc_get_share_facebook( $options );
 
 
1277
  }
1278
 
1279
  /**
1280
  * Get share count from cache (Google Plus)
1281
  *
1282
  * @since 0.1.0
1283
+ * @deprecated Function deprecated in Release 0.4.0
1284
  */
1285
  function get_scc_gplus( $options = array( 'id' => '', 'url' => '' ) ) {
1286
 
1287
+ return scc_get_share_gplus( $options );
 
 
1288
  }
1289
 
1290
  /**
1291
  * Get share count from cache (Pocket)
1292
  *
1293
  * @since 0.2.1
1294
+ * @deprecated Function deprecated in Release 0.4.0
1295
  */
1296
  function get_scc_pocket( $options = array( 'id' => '', 'url' => '' ) ) {
1297
 
1298
+ return scc_get_share_pocket( $options );
 
 
1299
  }
1300
+
1301
+ /**
1302
+ * Get follow count from cache (Feedly)
1303
+ *
1304
+ * @since 0.2.1
1305
+ */
1306
+ function scc_get_follow_feedly() {
1307
+
1308
+ $options['sns'] = SNS_Count_Cache::REF_FOLLOW_FEEDLY;
1309
+ return scc_get_follow( $options );
1310
+ }
1311
+
1312
+
1313
+
1314
+
1315
  }
1316
 
1317
  ?>
uninstall.php CHANGED
@@ -3,18 +3,28 @@
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
  $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 {
@@ -25,11 +35,16 @@ else {
25
  foreach($blog_ids as $blog_id){
26
  switch_to_blog($blog_id);
27
 
 
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
  }
3
  if(!defined('WP_UNINSTALL_PLUGIN')) exit();
4
 
5
  $db_check_interval = 'scc_check_interval';
6
+ $db_follow_check_interval = 'scc_follow_check_interval';
7
  $db_posts_per_check = 'scc_posts_per_check';
8
  $db_dynamic_cache = 'scc_dynamic_cache';
9
  $db_new_content_term = 'scc_new_content_term';
10
  $db_cache_target = 'scc_cache_target';
11
+ $db_data_export = 'scc_data_export';
12
+ $db_data_export_interval = 'scc_data_export_interval';
13
+ $db_custom_post_types = 'scc_custom_post_types';
14
+ $db_follow_cache_target = 'scc_follow_cache_target';
15
+
16
  // For Single site
17
  if(!is_multisite()){
18
+ delete_option($db_custom_post_types);
19
  delete_option($db_check_interval);
20
+ delete_option($db_follow_check_interval);
21
  delete_option($db_posts_per_check);
22
  delete_option($db_dynamic_cache);
23
  delete_option($db_new_content_term);
24
  delete_option($db_cache_target);
25
+ delete_option($db_data_export);
26
+ delete_option($db_data_export_interval);
27
+ delete_option($db_follow_cache_target);
28
  }
29
  // For Multisite
30
  else {
35
  foreach($blog_ids as $blog_id){
36
  switch_to_blog($blog_id);
37
 
38
+ delete_option($db_custom_post_types);
39
  delete_option($db_check_interval);
40
+ delete_option($db_follow_check_interval);
41
  delete_option($db_posts_per_check);
42
  delete_option($db_dynamic_cache);
43
  delete_option($db_new_content_term);
44
+ delete_option($db_cache_target);
45
+ delete_option($db_data_export);
46
+ delete_option($db_data_export_interval);
47
+ delete_option($db_follow_cache_target);
48
  }
49
  switch_to_blog($original_blog_id);
50
  }