PluginProbe
codoc / 0.9.59
codoc v0.9.59
0.9.8.8 0.9.8.9 0.9.9 0.9.9.1 trunk 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.8.1 0.8.2 0.8.3 0.8.4 0.8.6 0.8.7 0.8.8 0.8.9 0.9 0.9.1 0.9.10 0.9.11 All 114 releases
← All changes | class-codoc-util.php +320 -25 0.90.9.59 View file →
@@ -3,28 +3,39 @@
3 3 if (!defined('WPINC')) {
4 4 define( 'WPINC', 'wp-includes' );
5 5 }
6 6
7 -require_once( ABSPATH . WPINC . '/class-http.php');
7 +if (!defined('CODOC_URL')) {
8 + define( 'CODOC_URL', 'https://codoc.jp' );
9 +}
10 +global $wp_version;
11 +if (version_compare( $wp_version,'5.9') >= 0 ) {
12 + require_once( ABSPATH . WPINC . '/class-wp-http.php');
13 +} else {
14 + require_once( ABSPATH . WPINC . '/class-http.php');
15 +}
16 +
8 17 require_once( ABSPATH . WPINC . '/class-wp-error.php');
9 18
10 19 final class CodocUtil {
11 -
12 - public function __construct($params = ['usercode' => null, 'token' => null]) {
13 - $this->usercode = $params['usercode'] or die('no usercode');
14 - $this->token = $params['token'] or die('no token');
20 + public $codoc_url = CODOC_URL;
21 + public function __construct($params = ['usercode' => null, 'token' => null,'codoc_url' => null]) {
22 + $this->setAuthInfo(['usercode' => $params['usercode'], 'token' => $params['token']]);
23 + if (isset($params['codoc_url'])) {
24 + $this->codoc_url = $params['codoc_url'];
25 + }
15 26 return $this;
16 27 }
17 -
28 +
18 29 public function callAPI($method,$path,$body = [],$headers = []) {
19 30 $usercode = $this->usercode;
20 31 $token = $this->token;
21 32
22 33 $headers['X-CodocToken'] = $token;
23 - $host = CODOC_URL;
34 + $host = $this->codoc_url;
24 35 $sslverify = true;
25 36
26 - if (preg_match('/local/',CODOC_URL)) {
37 + if (preg_match('/local/',$this->codoc_url)) {
27 38 $sslverify = false;
28 39 $host = 'https://host.docker.internal';
29 40 }
30 41 $url = sprintf("%s/api/v1/cms/%s%s",$host,$usercode,$path);
@@ -39,8 +50,14 @@
39 50 'headers' => $headers,
40 51 'body' => $body,
41 52 ]
42 53 );
54 + // notification を処理
55 + $body = json_decode($response['body'],true);
56 + if (isset($body['notification']) && !empty($body['notification'])) {
57 + // notification をトランジェントに保存(24時間保持)
58 + set_transient('codoc_api_notification', $body['notification'], DAY_IN_SECONDS);
59 + }
43 60 if ( is_wp_error($response) || $response['response']['code'] != 200 ) {
44 61 //何もしない
45 62 //var_dump( $response );
46 63 }
@@ -56,9 +73,70 @@
56 73 } else {
57 74 return null;
58 75 }
59 76 }
77 + public function callPaywallAPI($method,$path,$body = [],$headers = []) {
78 + $site_usercode = get_option(CODOC_USERCODE_OPTION_NAME);
79 + $paywall_token = $this->get_paywall_token_code();
60 80
81 + $host = $this->codoc_url;
82 + $sslverify = true;
83 +
84 + if (preg_match('/local/',$this->codoc_url)) {
85 + $sslverify = false;
86 + $host = 'https://host.docker.internal';
87 + }
88 + $url = sprintf("%s/api/v1/paywall/%s%s",$host,$site_usercode,$path);
89 + $http = new WP_Http();
90 + try {
91 + $response = $http->request(
92 + $url,
93 + [
94 + 'sslverify' => $sslverify,
95 + 'method' => $method,
96 + 'timeout' => 10,
97 + 'headers' => $headers,
98 + 'body' => $body,
99 + ]
100 + );
101 + if ( is_wp_error($response) || $response['response']['code'] != 200 ) {
102 + //何もしない
103 + //var_dump( $response );
104 + }
105 + } catch(Exception $e) {
106 +
107 + }
108 + if (!is_wp_error($response) and
109 + isset($response['body']) and
110 + is_string($response['body']) and
111 + is_array(json_decode($response['body'], true)) and
112 + (json_last_error() == JSON_ERROR_NONE)) {
113 + return json_decode($response['body']);
114 + } else {
115 + return null;
116 + }
117 + }
118 + public function setAuthInfo($params = [ "usercode" => null, "token" => null ]) {
119 + if ($params["usercode"]) {
120 + $this->usercode = $params["usercode"];
121 + }
122 + if ($params["token"]) {
123 + $this->token = $params["token"];
124 + }
125 + return true;
126 + }
127 + public function get_token($params = ["fetch_token_key" => null],$auth_info = ["usercode" => null, "token" => null]) {
128 + $this->setAuthInfo($auth_info);
129 + return $this->callAPI('GET','/token',[ "fetch_token_key" => $params["fetch_token_key"] ]);
130 + }
131 + public function get_user_info($params = [] ,$auth_info = ["usercode" => null, "token" => null]) {
132 + $this->setAuthInfo($auth_info);
133 + return $this->callAPI('GET','');
134 + }
135 + public function get_support_entry($params = [] ,$auth_info = ["usercode" => null, "token" => null]) {
136 + $this->setAuthInfo($auth_info);
137 + return $this->callAPI('GET','/support_entry');
138 + }
61 139 public function sync_entry($params = [
62 140 "post_title" => null,
63 141 "post_content" => null,
64 142 "post_status" => null, # 0, 1
@@ -63,9 +141,12 @@
63 141 "post_content" => null,
64 142 "post_status" => null, # 0, 1
65 143 "post_permalink" => null,
66 144 "codoc_entry_code" => null,
67 - ]) {
145 + ],$auth_info = ["usercode" => null, "token" => null]) {
146 +
147 + $this->setAuthInfo($auth_info);
148 +
68 149 $post_content = $params['post_content'];
69 150 preg_match('/wp:codoc\/codoc-block +?({.*})/',$post_content,$matches);
70 151 // GUTENBERG で指定されたjson(記事情報)がない場合
71 152 if (!$matches) {
@@ -80,10 +161,17 @@
80 161 return;
81 162 }
82 163 // TODO: split or html parse? HTMLがつながってる場合はおかしくなる
83 164 // classic(tinymce)版の場合p、もしくはdivにかこまれている
165 + $end_tag_regex = '/<(?:div|p)>::CODOC_WP_END_PAYWALL::<\/(?:div|p)>/';
166 + $before_splited = preg_split($end_tag_regex,$post_content);
167 + $post_content = $before_splited[0];
84 168 $splited = preg_split('/(?:<(?:div|p)(?:[^>]+|)>|)<\!-- +wp:codoc\/codoc-block .*<\!-- +\/wp:codoc\/codoc-block +-->(?:<\/(?:div|p)>|)/s',$post_content);
85 - $status = $params['post_status'];
169 + $status = $params['post_status']; # 0: 非公開 1: 公開 2:限定公開(パスワードの場合は限定公開で同期する)
170 + // codoc設定で限定公開を有効で、公開の場合は 限定公開にする
171 + if ($status == 1 and isset($codoc_info['statusLimited']) and $codoc_info['statusLimited']) {
172 + $status = 2;
173 + }
86 174 # $body_free = '';
87 175 # $body_paywalled = '';
88 176 # if (count($splited) >= 2) {
89 177 #
@@ -109,8 +197,9 @@
109 197 'limited_count' => isset($codoc_info['limitedCount']) ? $codoc_info['limitedCount'] : 1,
110 198 'affiliate_mode' => isset($codoc_info['affiliateMode']) ? ($codoc_info['affiliateMode'] ? 1 : 0) : 0,
111 199 'affiliate_rate' => isset($codoc_info['affiliateRate']) ? $codoc_info['affiliateRate'] : '0.0500',
112 200 'show_support' => isset($codoc_info['showSupport']) ? ($codoc_info['showSupport'] ? 1 : 0) : 0,
201 + 'show_paywalled_support' => isset($codoc_info['showPaywalledSupport']) ? ($codoc_info['showPaywalledSupport'] ? 1 : 0) : 0,
113 202 'subscriptions' => isset($codoc_info['subscriptions']) ? array_keys($codoc_info['subscriptions']) : [],
114 203 ];
115 204 $entryCode = $params['codoc_entry_code'];
116 205 $res = null;
@@ -125,9 +214,12 @@
125 214 function post_thumbnail( $params = [
126 215 "file_path" => null,
127 216 "boundary" => null,
128 217 "codoc_entry_code" => null,
129 - ] ) {
218 + ], $auth_info = ["usercode" => null, "token" => null] ) {
219 +
220 + $this->setAuthInfo($auth_info);
221 +
130 222 $file_path = $params['file_path'];
131 223 $boundary = $params['boundary']; # ランダム変数24桁 wp_generate_password(24);
132 224 $res = null;
133 225 if (is_readable($file_path)) {
@@ -157,9 +249,12 @@
157 249 }
158 250
159 251 function reset_thumbnail( $params = [
160 252 "codoc_entry_code" => null,
161 - ]) {
253 + ],$auth_info = ["usercode" => null, "token" => null] ) {
254 +
255 + $this->setAuthInfo($auth_info);
256 +
162 257 $entryCode = $params['codoc_entry_code'];
163 258 $res = $this->callAPI(
164 259 'POST',
165 260 '/entries/' . $entryCode . '/thumbnail',
@@ -171,35 +266,138 @@
171 266 function filter_content( $params = [
172 267 "post_content" => null,
173 268 "preview" => null,
174 269 "codoc_entry_code" => null,
270 + "codoc_settings" => null,
271 + "is_amp_endpoint" => null,
272 + "post_permalink" => null,
273 + "codoc_support_entry_code" => null,
175 274 ]) {
275 + $CODOC_SETTINGS = $params['codoc_settings'];
276 +
277 + $has_codoc_tag = '/(<(span|div)[^>]+data-id="codoc-tag"(?:[^>]+|)>(?:.+|)<\/(?:div|span)>)/';
176 278 $post_content = $params['post_content'];
177 279 // codoc タグがあるかどうか (202001:span -> div に変更)
178 - preg_match('/(<(span|div)[^>]+data-id="codoc-tag"(?:[^>]+|)>(?:.+|)<\/(?:div|span)>)/',$post_content,$matches);
280 + preg_match($has_codoc_tag,$post_content,$matches);
281 +
282 + $show_support_message = '';
283 + if (isset($CODOC_SETTINGS['show_support_message'])) {
284 + $show_support_message = $CODOC_SETTINGS['show_support_message'];
285 + }
286 + $show_support_categories = '';
287 + if (isset($CODOC_SETTINGS['show_support_categories'])) {
288 + $show_support_categories = $CODOC_SETTINGS['show_support_categories'];
289 + }
290 + $show_support_location = 'bottom';
291 + if (isset($CODOC_SETTINGS['show_support_location'])) {
292 + $show_support_location = $CODOC_SETTINGS['show_support_location'];
293 + }
294 + // デフォルト値をうめておく
295 + if (!isset($CODOC_SETTINGS['support_button_text'])) {
296 + $CODOC_SETTINGS['support_button_text'] = 'サポートする';
297 + }
298 + if (!isset($CODOC_SETTINGS['show_like'])) {
299 + $CODOC_SETTINGS['show_like'] = 1;
300 + }
301 + if (!isset($CODOC_SETTINGS['show_about_codoc'])) {
302 + $CODOC_SETTINGS['show_about_codoc'] = 1;
303 + }
304 + if (!isset($CODOC_SETTINGS['show_powered_by'])) {
305 + $CODOC_SETTINGS['show_powered_by'] = 1;
306 + }
307 + if (!isset($CODOC_SETTINGS['show_created_by'])) {
308 + $CODOC_SETTINGS['show_created_by'] = 1;
309 + }
310 + if (!isset($CODOC_SETTINGS['show_copyright'])) {
311 + $CODOC_SETTINGS['show_copyright'] = 1;
312 + }
313 + $show_support_entry = (is_single() && preg_grep("/(?:$show_support_categories)/",array_map( function($c) { return $c->name; },get_the_category(get_post()->ID))));
314 + if (!$matches && ($support_entry_code = $params['codoc_support_entry_code']) && $show_support_entry) {
315 + $support_tag = sprintf(
316 + '<div class="codoc-entries" data-without-body="1" data-support-message="%s" id="codoc-entry-%s" data-support-button-text="%s" data-show-like="%s" data-show-about-codoc="%s" data-show-powered-by="%s" data-show-created-by="%s" data-show-copyright="%s"></div>',
317 + htmlspecialchars($show_support_message,ENT_QUOTES),
318 + $support_entry_code,
319 + $CODOC_SETTINGS['support_button_text'],
320 + $CODOC_SETTINGS['show_like'],
321 + $CODOC_SETTINGS['show_about_codoc'],
322 + $CODOC_SETTINGS['show_powered_by'],
323 + $CODOC_SETTINGS['show_created_by'],
324 + $CODOC_SETTINGS['show_copyright']
325 + );
326 + $post_content = $show_support_location == 'bottom' ?
327 + $post_content . $support_tag :
328 + $support_tag . $post_content ;
329 + }
330 +
179 331 // data-wp-plugin-ver が無いタグは分割しない
180 332 if (!$matches) {
181 333 return $post_content;
182 334 }
335 +
183 336 // codocタグで分割 (202001: gutenbergで直前のdivタグを削除)
184 337 $tag_regex = '/(?:<div(?:[^>]+|)>|)<(?:span|div)[^>]+data-id="codoc-tag"(?:[^>]+|)>(?:.+|)<\/(?:span|div)>(?:<\/div>|)/';
338 + $end_tag_regex = '/<(?:div|p)>::CODOC_WP_END_PAYWALL::<\/(?:div|p)>/';
185 339 if ( $params['preview'] ) {
186 340 $post_content = preg_replace($tag_regex,'<div class="codoc-continue">ここから上は無料で表示されます</div>',$post_content);
341 + $post_content = preg_replace($end_tag_regex,'<div class="codoc-continue">ここから下は無料で表示されます</div>',$post_content);
187 342 return $post_content;
188 343 }
189 344 // codoc タグの前後で分ける
190 - $splited = preg_split($tag_regex,$post_content);
345 + $before_splited = preg_split($end_tag_regex,$post_content);
346 + $splited = preg_split($tag_regex,$before_splited[0]);
191 347 // codoc タグにID属性のentrycodeとdata-without-body(無料分を非表示)をつける
192 - $entryCode = $params['codoc_entry_code'];
193 - $codoc_tag = preg_replace('/<((?:span|div)[^>]+)data-id="[^"]+"((?:[^>]+|))>/','<${1} ${2} data-without-body="1" id=' . $entryCode . ">", $matches[1]);
348 + $entryCodeFormated = sprintf('"codoc-entry-%s" ',$params['codoc_entry_code']);
349 +
350 + // 文言系の設定をつける
351 + $tagAttributes = '';
352 + if (isset($CODOC_SETTINGS['show_like']) and $CODOC_SETTINGS['show_like'] != 1) {
353 + $tagAttributes = $tagAttributes . sprintf(' data-show-like="%s"',$CODOC_SETTINGS['show_like']);
354 + }
355 + if (isset($CODOC_SETTINGS['show_about_codoc']) and $CODOC_SETTINGS['show_about_codoc'] != 1) {
356 + $tagAttributes = $tagAttributes . sprintf(' data-show-about-codoc="%s"',$CODOC_SETTINGS['show_about_codoc']);
357 + }
358 + if (isset($CODOC_SETTINGS['show_powered_by']) and $CODOC_SETTINGS['show_powered_by'] != 1) {
359 + $tagAttributes = $tagAttributes . sprintf(' data-show-powered-by="%s"',$CODOC_SETTINGS['show_powered_by']);
360 + }
361 + if (isset($CODOC_SETTINGS['show_created_by']) and $CODOC_SETTINGS['show_created_by'] != 1) {
362 + $tagAttributes = $tagAttributes . sprintf(' data-show-created-by="%s"',$CODOC_SETTINGS['show_created_by']);
363 + }
364 + if (isset($CODOC_SETTINGS['show_copyright']) and $CODOC_SETTINGS['show_copyright'] != 1) {
365 + $tagAttributes = $tagAttributes . sprintf(' data-show-copyright="%s"',$CODOC_SETTINGS['show_copyright']);
366 + }
367 + if (isset($CODOC_SETTINGS['entry_button_text']) and $CODOC_SETTINGS['entry_button_text'] != '') {
368 + $tagAttributes = $tagAttributes . sprintf(' data-entry-button-text="%s"',$CODOC_SETTINGS['entry_button_text']);
369 + }
370 + if (isset($CODOC_SETTINGS['subscription_button_text']) and $CODOC_SETTINGS['subscription_button_text'] != '') {
371 + $tagAttributes = $tagAttributes . sprintf(' data-subscription-button-text="%s"',$CODOC_SETTINGS['subscription_button_text']);
372 + }
373 + if (isset($CODOC_SETTINGS['support_button_text']) and $CODOC_SETTINGS['support_button_text'] != '') {
374 + $tagAttributes = $tagAttributes . sprintf(' data-support-button-text="%s"',$CODOC_SETTINGS['support_button_text']);
375 + }
376 + if (isset($CODOC_SETTINGS['subscription_message']) and $CODOC_SETTINGS['subscription_message'] != '') {
377 + $tagAttributes = $tagAttributes . sprintf(' data-subscription-message="%s"',$CODOC_SETTINGS['subscription_message']);
378 + }
379 + if (isset($CODOC_SETTINGS['support_message']) and $CODOC_SETTINGS['support_message'] != '') {
380 + $tagAttributes = $tagAttributes . sprintf(' data-support-message="%s"',$CODOC_SETTINGS['support_message']);
381 + }
382 + if (isset($CODOC_SETTINGS['codoc_tag_attributes']) and $CODOC_SETTINGS['codoc_tag_attributes'] != '') {
383 + $tagAttributes = $tagAttributes . sprintf(' %s',$CODOC_SETTINGS['codoc_tag_attributes']);
384 + }
385 +
386 + $codoc_tag = preg_replace('/<((?:span|div)[^>]+)data-id="[^"]+"((?:[^>]+|))>/','<${1} ${2} data-without-body="1" id=' . $entryCodeFormated . $tagAttributes . ">", $matches[1]);
194 387 # THE THOR の page-lp.php が " " をけずるための対策
195 388 # "div class" -> "div class"
196 389 $codoc_tag = preg_replace('/div +class/','div class',$codoc_tag);
197 -
390 +
391 + if ($params['is_amp_endpoint']) {
392 + $format = preg_replace_callback('/(<(?:span|div)[^>]+>)((?:[^<>]+|))(<\/(?:span|div)>)/',function($matches) {
393 + $format = $matches[1] . '<a href="%%s">%s</a>' . $matches[3];
394 + return sprintf($format,($matches[2] ? $matches[2] : '続きを読む'));
395 + },$codoc_tag);
396 + $codoc_tag = sprintf($format,$params["post_permalink"]);
397 + }
398 +
198 399 # タグの前後にHTMLを挿入
199 - $CODOC_SETTINGS = $params['codoc_settings'];
200 - $before = '';
201 - $after = '';
202 400 if (!isset($CODOC_SETTINGS['str_before_codoc_tag'])) {
203 401 $CODOC_SETTINGS['str_before_codoc_tag'] = '';
204 402 }
205 403 if (!isset($CODOC_SETTINGS['str_after_codoc_tag'])) {
@@ -204,14 +402,111 @@
204 402 }
205 403 if (!isset($CODOC_SETTINGS['str_after_codoc_tag'])) {
206 404 $CODOC_SETTINGS['str_after_codoc_tag'] = '';
207 405 }
208 - $before = $CODOC_SETTINGS['str_before_codoc_tag'] or "";
209 - $after = $CODOC_SETTINGS['str_after_codoc_tag'] or "";
210 -
211 406 // 無料部分のみ表示
212 - return $splited[0] . sprintf('%s<div class="wp-block-codoc-codoc-block">%s</div>%s',$before,$codoc_tag,$after);
407 + $post_content = $splited[0] . sprintf(
408 + '%s<div class="wp-block-codoc-codoc-block">%s</div>%s',
409 + $CODOC_SETTINGS['str_before_codoc_tag'],
410 + $codoc_tag,
411 + $CODOC_SETTINGS['str_after_codoc_tag']
412 + );
413 + // ::CODOC_WP_END_PAYWALL:: の後を足す
414 + if (isset($before_splited[1]) and $before_splited[1]) {
415 + $post_content .= $before_splited[1];
416 + }
417 + return $post_content;
213 418 }
419 + /*
214 420
215 -
421 + // Cookieの中のトークンを取得
422 + $_CODOC->util->get_paywall_token_code();
423 +
424 + */
425 + function get_paywall_token_code() {
426 + if (isset($_COOKIE['codocTokenCode']) and $codocTokenCode = $_COOKIE['codocTokenCode']) {
427 + return $codocTokenCode;
428 + }
429 + return null;
430 + }
431 + /*
432 +
433 + $_CODOC->util->check_login($mode); // cookie / strict
434 +
435 + $response = $_CODOC->util->check_login('strict_with_data');
436 + if ($response->status and $response->user) {
437 + echo $reponse->user->code;
438 + }
439 +
440 + */
441 + function check_login($mode = 'cookie') {
442 + if ($codocTokenCode = $this->get_paywall_token_code()) {
443 + if ($mode == 'cookie') {
444 + // cookieの中にtokenがあればOK
445 + return true;
446 + }
447 + if ($mode == 'strict' or $mode == 'strict_with_data') {
448 + // ログイン状態を問い合わせ
449 + $res = $this->callPaywallAPI('GET','/users',["paywall_token_code" => $codocTokenCode ]);
450 + if ($res->status) {
451 + if ($mode == 'strict_with_data') {
452 + return $res;
453 + } else {
454 + return true;
455 + }
456 + } else {
457 + return false;
458 + }
459 + }
460 + }
461 + return false;
462 + }
463 + /*
464 +
465 + $_CODOC->util->check_owned($entry_code);
466 +
467 + */
468 + function check_owned($entry_code = null) {
469 + if (!$entry_code) {
470 + return false;
471 + }
472 + $res = $this->callPaywallAPI('GET','/entries/' . $entry_code, ["paywall_token_code" => $this->get_paywall_token_code(), "check_owned" => 1 ]);
473 + if ($res->status) {
474 + return true;
475 + }
476 + return false;
477 + }
478 +
479 + /*
480 + $_CODOC->util->health_check();
481 + */
482 + function health_check() {
483 + $sslverify = true;
484 + $host = $this->codoc_url;
485 + if (preg_match('/local/',$this->codoc_url)) {
486 + $sslverify = false;
487 + $host = 'https://host.docker.internal';
488 + }
489 + $url = sprintf("%s/codoc_health_check",$host);
490 + $http = new WP_Http();
491 + try {
492 + $response = $http->request(
493 + $url,
494 + [
495 + 'sslverify' => $sslverify,
496 + 'method' => 'GET',
497 + 'timeout' => 10,
498 + 'headers' => [],
499 + 'body' => [],
500 + ]
501 + );
502 + } catch(Exception $e) {
503 +
504 + }
505 + if ($response and $response['response']['code'] == '200') {
506 + return true;
507 + } else {
508 + return null;
509 + }
510 + }
216 511 }
217 512