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 +195 -13 0.9.90.9.59 View file →
@@ -6,10 +6,15 @@
6 6
7 7 if (!defined('CODOC_URL')) {
8 8 define( 'CODOC_URL', 'https://codoc.jp' );
9 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 +}
10 16
11 -require_once( ABSPATH . WPINC . '/class-http.php');
12 17 require_once( ABSPATH . WPINC . '/class-wp-error.php');
13 18
14 19 final class CodocUtil {
15 20 public $codoc_url = CODOC_URL;
@@ -45,8 +50,14 @@
45 50 'headers' => $headers,
46 51 'body' => $body,
47 52 ]
48 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 + }
49 60 if ( is_wp_error($response) || $response['response']['code'] != 200 ) {
50 61 //何もしない
51 62 //var_dump( $response );
52 63 }
@@ -62,8 +73,49 @@
62 73 } else {
63 74 return null;
64 75 }
65 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();
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 + }
66 118 public function setAuthInfo($params = [ "usercode" => null, "token" => null ]) {
67 119 if ($params["usercode"]) {
68 120 $this->usercode = $params["usercode"];
69 121 }
@@ -109,10 +161,17 @@
109 161 return;
110 162 }
111 163 // TODO: split or html parse? HTMLがつながってる場合はおかしくなる
112 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];
113 168 $splited = preg_split('/(?:<(?:div|p)(?:[^>]+|)>|)<\!-- +wp:codoc\/codoc-block .*<\!-- +\/wp:codoc\/codoc-block +-->(?:<\/(?:div|p)>|)/s',$post_content);
114 - $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 + }
115 174 # $body_free = '';
116 175 # $body_paywalled = '';
117 176 # if (count($splited) >= 2) {
118 177 #
@@ -231,19 +290,39 @@
231 290 $show_support_location = 'bottom';
232 291 if (isset($CODOC_SETTINGS['show_support_location'])) {
233 292 $show_support_location = $CODOC_SETTINGS['show_support_location'];
234 293 }
235 -
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 + }
236 313 $show_support_entry = (is_single() && preg_grep("/(?:$show_support_categories)/",array_map( function($c) { return $c->name; },get_the_category(get_post()->ID))));
237 314 if (!$matches && ($support_entry_code = $params['codoc_support_entry_code']) && $show_support_entry) {
238 315 $support_tag = sprintf(
239 - '<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"></div>',
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>',
240 317 htmlspecialchars($show_support_message,ENT_QUOTES),
241 318 $support_entry_code,
242 319 $CODOC_SETTINGS['support_button_text'],
243 320 $CODOC_SETTINGS['show_like'],
244 321 $CODOC_SETTINGS['show_about_codoc'],
245 - $CODOC_SETTINGS['show_powered_by']
322 + $CODOC_SETTINGS['show_powered_by'],
323 + $CODOC_SETTINGS['show_created_by'],
324 + $CODOC_SETTINGS['show_copyright']
246 325 );
247 326 $post_content = $show_support_location == 'bottom' ?
248 327 $post_content . $support_tag :
249 328 $support_tag . $post_content ;
@@ -255,14 +334,17 @@
255 334 }
256 335
257 336 // codocタグで分割 (202001: gutenbergで直前のdivタグを削除)
258 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)>/';
259 339 if ( $params['preview'] ) {
260 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);
261 342 return $post_content;
262 343 }
263 344 // codoc タグの前後で分ける
264 - $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]);
265 347 // codoc タグにID属性のentrycodeとdata-without-body(無料分を非表示)をつける
266 348 $entryCodeFormated = sprintf('"codoc-entry-%s" ',$params['codoc_entry_code']);
267 349
268 350 // 文言系の設定をつける
@@ -275,24 +357,30 @@
275 357 }
276 358 if (isset($CODOC_SETTINGS['show_powered_by']) and $CODOC_SETTINGS['show_powered_by'] != 1) {
277 359 $tagAttributes = $tagAttributes . sprintf(' data-show-powered-by="%s"',$CODOC_SETTINGS['show_powered_by']);
278 360 }
279 - if ($CODOC_SETTINGS['entry_button_text']) {
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'] != '') {
280 368 $tagAttributes = $tagAttributes . sprintf(' data-entry-button-text="%s"',$CODOC_SETTINGS['entry_button_text']);
281 369 }
282 - if ($CODOC_SETTINGS['subscription_button_text']) {
370 + if (isset($CODOC_SETTINGS['subscription_button_text']) and $CODOC_SETTINGS['subscription_button_text'] != '') {
283 371 $tagAttributes = $tagAttributes . sprintf(' data-subscription-button-text="%s"',$CODOC_SETTINGS['subscription_button_text']);
284 372 }
285 - if ($CODOC_SETTINGS['support_button_text']) {
373 + if (isset($CODOC_SETTINGS['support_button_text']) and $CODOC_SETTINGS['support_button_text'] != '') {
286 374 $tagAttributes = $tagAttributes . sprintf(' data-support-button-text="%s"',$CODOC_SETTINGS['support_button_text']);
287 375 }
288 - if ($CODOC_SETTINGS['subscription_message']) {
376 + if (isset($CODOC_SETTINGS['subscription_message']) and $CODOC_SETTINGS['subscription_message'] != '') {
289 377 $tagAttributes = $tagAttributes . sprintf(' data-subscription-message="%s"',$CODOC_SETTINGS['subscription_message']);
290 378 }
291 - if ($CODOC_SETTINGS['support_message']) {
379 + if (isset($CODOC_SETTINGS['support_message']) and $CODOC_SETTINGS['support_message'] != '') {
292 380 $tagAttributes = $tagAttributes . sprintf(' data-support-message="%s"',$CODOC_SETTINGS['support_message']);
293 381 }
294 - if ($CODOC_SETTINGS['codoc_tag_attributes']) {
382 + if (isset($CODOC_SETTINGS['codoc_tag_attributes']) and $CODOC_SETTINGS['codoc_tag_attributes'] != '') {
295 383 $tagAttributes = $tagAttributes . sprintf(' %s',$CODOC_SETTINGS['codoc_tag_attributes']);
296 384 }
297 385
298 386 $codoc_tag = preg_replace('/<((?:span|div)[^>]+)data-id="[^"]+"((?:[^>]+|))>/','<${1} ${2} data-without-body="1" id=' . $entryCodeFormated . $tagAttributes . ">", $matches[1]);
@@ -321,10 +409,104 @@
321 409 $CODOC_SETTINGS['str_before_codoc_tag'],
322 410 $codoc_tag,
323 411 $CODOC_SETTINGS['str_after_codoc_tag']
324 412 );
413 + // ::CODOC_WP_END_PAYWALL:: の後を足す
414 + if (isset($before_splited[1]) and $before_splited[1]) {
415 + $post_content .= $before_splited[1];
416 + }
325 417 return $post_content;
326 418 }
419 + /*
327 420
328 -
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 + }
329 511 }
330 512