PluginProbe
codoc / 0.9.20
codoc v0.9.20
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 +13 -231 trunk0.9.20 View file →
@@ -6,15 +6,10 @@
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 -}
16 10
11 +require_once( ABSPATH . WPINC . '/class-http.php');
17 12 require_once( ABSPATH . WPINC . '/class-wp-error.php');
18 13
19 14 final class CodocUtil {
20 15 public $codoc_url = CODOC_URL;
@@ -50,14 +45,8 @@
50 45 'headers' => $headers,
51 46 'body' => $body,
52 47 ]
53 48 );
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 - }
60 49 if ( is_wp_error($response) || $response['response']['code'] != 200 ) {
61 50 //何もしない
62 51 //var_dump( $response );
63 52 }
@@ -73,49 +62,8 @@
73 62 } else {
74 63 return null;
75 64 }
76 65 }
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 - }
118 66 public function setAuthInfo($params = [ "usercode" => null, "token" => null ]) {
119 67 if ($params["usercode"]) {
120 68 $this->usercode = $params["usercode"];
121 69 }
@@ -135,38 +83,8 @@
135 83 public function get_support_entry($params = [] ,$auth_info = ["usercode" => null, "token" => null]) {
136 84 $this->setAuthInfo($auth_info);
137 85 return $this->callAPI('GET','/support_entry');
138 86 }
139 - // API側と統一した文字数上限のバリデーション。エラーメッセージの配列を返す (エラー無しなら空配列)
140 - public function validate_entry_lengths($api_params) {
141 - $errors = [];
142 - $title = isset($api_params['title']) ? $api_params['title'] : '';
143 - $body_free = isset($api_params['body_free']) ? $api_params['body_free'] : '';
144 - $body_paywalled = isset($api_params['body_paywalled']) ? $api_params['body_paywalled'] : '';
145 - $binded_url = isset($api_params['binded_url']) ? $api_params['binded_url'] : '';
146 -
147 - if (mb_strlen($title) > CODOC_MAX_TITLE_LENGTH) {
148 - // Translators: %1$d is the max length, %2$d is the current length.
149 - $errors[] = sprintf(__('Title exceeds the maximum length of %1$d characters (current: %2$d).', 'codoc'), CODOC_MAX_TITLE_LENGTH, mb_strlen($title));
150 - }
151 - $len_body_free = mb_strlen($body_free);
152 - $len_body_paywalled = mb_strlen($body_paywalled);
153 - if ($len_body_free > CODOC_MAX_BODY_FREE_LENGTH) {
154 - $errors[] = sprintf(__('Free area exceeds the maximum length of %1$d characters (current: %2$d).', 'codoc'), CODOC_MAX_BODY_FREE_LENGTH, $len_body_free);
155 - }
156 - if ($len_body_paywalled > CODOC_MAX_BODY_PAYWALLED_LENGTH) {
157 - $errors[] = sprintf(__('Paid area exceeds the maximum length of %1$d characters (current: %2$d).', 'codoc'), CODOC_MAX_BODY_PAYWALLED_LENGTH, $len_body_paywalled);
158 - }
159 - $len_body_total = $len_body_free + $len_body_paywalled;
160 - if ($len_body_total > CODOC_MAX_BODY_LENGTH) {
161 - $errors[] = sprintf(__('Total body exceeds the maximum length of %1$d characters (current: %2$d).', 'codoc'), CODOC_MAX_BODY_LENGTH, $len_body_total);
162 - }
163 - if (mb_strlen($binded_url) > CODOC_MAX_BINDED_URL_LENGTH) {
164 - $errors[] = sprintf(__('URL exceeds the maximum length of %1$d characters (current: %2$d).', 'codoc'), CODOC_MAX_BINDED_URL_LENGTH, mb_strlen($binded_url));
165 - }
166 - return $errors;
167 - }
168 -
169 87 public function sync_entry($params = [
170 88 "post_title" => null,
171 89 "post_content" => null,
172 90 "post_status" => null, # 0, 1
@@ -191,17 +109,10 @@
191 109 return;
192 110 }
193 111 // TODO: split or html parse? HTMLがつながってる場合はおかしくなる
194 112 // classic(tinymce)版の場合p、もしくはdivにかこまれている
195 - $end_tag_regex = '/<(?:div|p)>::CODOC_WP_END_PAYWALL::<\/(?:div|p)>/';
196 - $before_splited = preg_split($end_tag_regex,$post_content);
197 - $post_content = $before_splited[0];
198 113 $splited = preg_split('/(?:<(?:div|p)(?:[^>]+|)>|)<\!-- +wp:codoc\/codoc-block .*<\!-- +\/wp:codoc\/codoc-block +-->(?:<\/(?:div|p)>|)/s',$post_content);
199 - $status = $params['post_status']; # 0: 非公開 1: 公開 2:限定公開(パスワードの場合は限定公開で同期する)
200 - // codoc設定で限定公開を有効で、公開の場合は 限定公開にする
201 - if ($status == 1 and isset($codoc_info['statusLimited']) and $codoc_info['statusLimited']) {
202 - $status = 2;
203 - }
114 + $status = $params['post_status'];
204 115 # $body_free = '';
205 116 # $body_paywalled = '';
206 117 # if (count($splited) >= 2) {
207 118 #
@@ -230,14 +141,8 @@
230 141 'show_support' => isset($codoc_info['showSupport']) ? ($codoc_info['showSupport'] ? 1 : 0) : 0,
231 142 'show_paywalled_support' => isset($codoc_info['showPaywalledSupport']) ? ($codoc_info['showPaywalledSupport'] ? 1 : 0) : 0,
232 143 'subscriptions' => isset($codoc_info['subscriptions']) ? array_keys($codoc_info['subscriptions']) : [],
233 144 ];
234 - // API側と統一した文字数バリデーション
235 - $errors = $this->validate_entry_lengths($api_params);
236 - if ($errors) {
237 - set_transient('codoc_entry_validation_errors', $errors, MINUTE_IN_SECONDS * 5);
238 - return null;
239 - }
240 145 $entryCode = $params['codoc_entry_code'];
241 146 $res = null;
242 147 if ($entryCode) {
243 148 $res = $this->callAPI('PUT','/entries/' . $entryCode ,$api_params);
@@ -326,39 +231,19 @@
326 231 $show_support_location = 'bottom';
327 232 if (isset($CODOC_SETTINGS['show_support_location'])) {
328 233 $show_support_location = $CODOC_SETTINGS['show_support_location'];
329 234 }
330 - // デフォルト値をうめておく
331 - if (!isset($CODOC_SETTINGS['support_button_text'])) {
332 - $CODOC_SETTINGS['support_button_text'] = 'サポートする';
333 - }
334 - if (!isset($CODOC_SETTINGS['show_like'])) {
335 - $CODOC_SETTINGS['show_like'] = 1;
336 - }
337 - if (!isset($CODOC_SETTINGS['show_about_codoc'])) {
338 - $CODOC_SETTINGS['show_about_codoc'] = 1;
339 - }
340 - if (!isset($CODOC_SETTINGS['show_powered_by'])) {
341 - $CODOC_SETTINGS['show_powered_by'] = 1;
342 - }
343 - if (!isset($CODOC_SETTINGS['show_created_by'])) {
344 - $CODOC_SETTINGS['show_created_by'] = 1;
345 - }
346 - if (!isset($CODOC_SETTINGS['show_copyright'])) {
347 - $CODOC_SETTINGS['show_copyright'] = 1;
348 - }
235 +
349 236 $show_support_entry = (is_single() && preg_grep("/(?:$show_support_categories)/",array_map( function($c) { return $c->name; },get_the_category(get_post()->ID))));
350 237 if (!$matches && ($support_entry_code = $params['codoc_support_entry_code']) && $show_support_entry) {
351 238 $support_tag = sprintf(
352 - '<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>',
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>',
353 240 htmlspecialchars($show_support_message,ENT_QUOTES),
354 241 $support_entry_code,
355 242 $CODOC_SETTINGS['support_button_text'],
356 243 $CODOC_SETTINGS['show_like'],
357 244 $CODOC_SETTINGS['show_about_codoc'],
358 - $CODOC_SETTINGS['show_powered_by'],
359 - $CODOC_SETTINGS['show_created_by'],
360 - $CODOC_SETTINGS['show_copyright']
245 + $CODOC_SETTINGS['show_powered_by']
361 246 );
362 247 $post_content = $show_support_location == 'bottom' ?
363 248 $post_content . $support_tag :
364 249 $support_tag . $post_content ;
@@ -370,17 +255,14 @@
370 255 }
371 256
372 257 // codocタグで分割 (202001: gutenbergで直前のdivタグを削除)
373 258 $tag_regex = '/(?:<div(?:[^>]+|)>|)<(?:span|div)[^>]+data-id="codoc-tag"(?:[^>]+|)>(?:.+|)<\/(?:span|div)>(?:<\/div>|)/';
374 - $end_tag_regex = '/<(?:div|p)>::CODOC_WP_END_PAYWALL::<\/(?:div|p)>/';
375 259 if ( $params['preview'] ) {
376 260 $post_content = preg_replace($tag_regex,'<div class="codoc-continue">ここから上は無料で表示されます</div>',$post_content);
377 - $post_content = preg_replace($end_tag_regex,'<div class="codoc-continue">ここから下は無料で表示されます</div>',$post_content);
378 261 return $post_content;
379 262 }
380 263 // codoc タグの前後で分ける
381 - $before_splited = preg_split($end_tag_regex,$post_content);
382 - $splited = preg_split($tag_regex,$before_splited[0]);
264 + $splited = preg_split($tag_regex,$post_content);
383 265 // codoc タグにID属性のentrycodeとdata-without-body(無料分を非表示)をつける
384 266 $entryCodeFormated = sprintf('"codoc-entry-%s" ',$params['codoc_entry_code']);
385 267
386 268 // 文言系の設定をつける
@@ -393,30 +275,24 @@
393 275 }
394 276 if (isset($CODOC_SETTINGS['show_powered_by']) and $CODOC_SETTINGS['show_powered_by'] != 1) {
395 277 $tagAttributes = $tagAttributes . sprintf(' data-show-powered-by="%s"',$CODOC_SETTINGS['show_powered_by']);
396 278 }
397 - if (isset($CODOC_SETTINGS['show_created_by']) and $CODOC_SETTINGS['show_created_by'] != 1) {
398 - $tagAttributes = $tagAttributes . sprintf(' data-show-created-by="%s"',$CODOC_SETTINGS['show_created_by']);
399 - }
400 - if (isset($CODOC_SETTINGS['show_copyright']) and $CODOC_SETTINGS['show_copyright'] != 1) {
401 - $tagAttributes = $tagAttributes . sprintf(' data-show-copyright="%s"',$CODOC_SETTINGS['show_copyright']);
402 - }
403 - if (isset($CODOC_SETTINGS['entry_button_text']) and $CODOC_SETTINGS['entry_button_text'] != '') {
279 + if ($CODOC_SETTINGS['entry_button_text']) {
404 280 $tagAttributes = $tagAttributes . sprintf(' data-entry-button-text="%s"',$CODOC_SETTINGS['entry_button_text']);
405 281 }
406 - if (isset($CODOC_SETTINGS['subscription_button_text']) and $CODOC_SETTINGS['subscription_button_text'] != '') {
282 + if ($CODOC_SETTINGS['subscription_button_text']) {
407 283 $tagAttributes = $tagAttributes . sprintf(' data-subscription-button-text="%s"',$CODOC_SETTINGS['subscription_button_text']);
408 284 }
409 - if (isset($CODOC_SETTINGS['support_button_text']) and $CODOC_SETTINGS['support_button_text'] != '') {
285 + if ($CODOC_SETTINGS['support_button_text']) {
410 286 $tagAttributes = $tagAttributes . sprintf(' data-support-button-text="%s"',$CODOC_SETTINGS['support_button_text']);
411 287 }
412 - if (isset($CODOC_SETTINGS['subscription_message']) and $CODOC_SETTINGS['subscription_message'] != '') {
288 + if ($CODOC_SETTINGS['subscription_message']) {
413 289 $tagAttributes = $tagAttributes . sprintf(' data-subscription-message="%s"',$CODOC_SETTINGS['subscription_message']);
414 290 }
415 - if (isset($CODOC_SETTINGS['support_message']) and $CODOC_SETTINGS['support_message'] != '') {
291 + if ($CODOC_SETTINGS['support_message']) {
416 292 $tagAttributes = $tagAttributes . sprintf(' data-support-message="%s"',$CODOC_SETTINGS['support_message']);
417 293 }
418 - if (isset($CODOC_SETTINGS['codoc_tag_attributes']) and $CODOC_SETTINGS['codoc_tag_attributes'] != '') {
294 + if ($CODOC_SETTINGS['codoc_tag_attributes']) {
419 295 $tagAttributes = $tagAttributes . sprintf(' %s',$CODOC_SETTINGS['codoc_tag_attributes']);
420 296 }
421 297
422 298 $codoc_tag = preg_replace('/<((?:span|div)[^>]+)data-id="[^"]+"((?:[^>]+|))>/','<${1} ${2} data-without-body="1" id=' . $entryCodeFormated . $tagAttributes . ">", $matches[1]);
@@ -445,104 +321,10 @@
445 321 $CODOC_SETTINGS['str_before_codoc_tag'],
446 322 $codoc_tag,
447 323 $CODOC_SETTINGS['str_after_codoc_tag']
448 324 );
449 - // ::CODOC_WP_END_PAYWALL:: の後を足す
450 - if (isset($before_splited[1]) and $before_splited[1]) {
451 - $post_content .= $before_splited[1];
452 - }
453 325 return $post_content;
454 326 }
455 - /*
456 327
457 - // Cookieの中のトークンを取得
458 - $_CODOC->util->get_paywall_token_code();
459 -
460 - */
461 - function get_paywall_token_code() {
462 - if (isset($_COOKIE['codocTokenCode']) and $codocTokenCode = $_COOKIE['codocTokenCode']) {
463 - return $codocTokenCode;
464 - }
465 - return null;
466 - }
467 - /*
468 -
469 - $_CODOC->util->check_login($mode); // cookie / strict
470 -
471 - $response = $_CODOC->util->check_login('strict_with_data');
472 - if ($response->status and $response->user) {
473 - echo $reponse->user->code;
474 - }
475 -
476 - */
477 - function check_login($mode = 'cookie') {
478 - if ($codocTokenCode = $this->get_paywall_token_code()) {
479 - if ($mode == 'cookie') {
480 - // cookieの中にtokenがあればOK
481 - return true;
482 - }
483 - if ($mode == 'strict' or $mode == 'strict_with_data') {
484 - // ログイン状態を問い合わせ
485 - $res = $this->callPaywallAPI('GET','/users',["paywall_token_code" => $codocTokenCode ]);
486 - if ($res->status) {
487 - if ($mode == 'strict_with_data') {
488 - return $res;
489 - } else {
490 - return true;
491 - }
492 - } else {
493 - return false;
494 - }
495 - }
496 - }
497 - return false;
498 - }
499 - /*
500 -
501 - $_CODOC->util->check_owned($entry_code);
502 -
503 - */
504 - function check_owned($entry_code = null) {
505 - if (!$entry_code) {
506 - return false;
507 - }
508 - $res = $this->callPaywallAPI('GET','/entries/' . $entry_code, ["paywall_token_code" => $this->get_paywall_token_code(), "check_owned" => 1 ]);
509 - if ($res->status) {
510 - return true;
511 - }
512 - return false;
513 - }
514 -
515 - /*
516 - $_CODOC->util->health_check();
517 - */
518 - function health_check() {
519 - $sslverify = true;
520 - $host = $this->codoc_url;
521 - if (preg_match('/local/',$this->codoc_url)) {
522 - $sslverify = false;
523 - $host = 'https://host.docker.internal';
524 - }
525 - $url = sprintf("%s/codoc_health_check",$host);
526 - $http = new WP_Http();
527 - try {
528 - $response = $http->request(
529 - $url,
530 - [
531 - 'sslverify' => $sslverify,
532 - 'method' => 'GET',
533 - 'timeout' => 10,
534 - 'headers' => [],
535 - 'body' => [],
536 - ]
537 - );
538 - } catch(Exception $e) {
539 -
540 - }
541 - if ($response and $response['response']['code'] == '200') {
542 - return true;
543 - } else {
544 - return null;
545 - }
546 - }
328 +
547 329 }
548 330