PluginProbe
codoc / 0.9.3
codoc v0.9.3
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 -314 trunk0.9.3 View file →
@@ -6,24 +6,16 @@
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 - 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 +
16 + public function __construct($params = ['usercode' => null, 'token' => null]) {
17 + $this->setAuthInfo($params);
26 18 return $this;
27 19 }
28 20
29 21 public function callAPI($method,$path,$body = [],$headers = []) {
@@ -30,12 +22,12 @@
30 22 $usercode = $this->usercode;
31 23 $token = $this->token;
32 24
33 25 $headers['X-CodocToken'] = $token;
34 - $host = $this->codoc_url;
26 + $host = CODOC_URL;
35 27 $sslverify = true;
36 28
37 - if (preg_match('/local/',$this->codoc_url)) {
29 + if (preg_match('/local/',CODOC_URL)) {
38 30 $sslverify = false;
39 31 $host = 'https://host.docker.internal';
40 32 }
41 33 $url = sprintf("%s/api/v1/cms/%s%s",$host,$usercode,$path);
@@ -50,14 +42,8 @@
50 42 'headers' => $headers,
51 43 'body' => $body,
52 44 ]
53 45 );
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 46 if ( is_wp_error($response) || $response['response']['code'] != 200 ) {
61 47 //何もしない
62 48 //var_dump( $response );
63 49 }
@@ -73,49 +59,8 @@
73 59 } else {
74 60 return null;
75 61 }
76 62 }
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 63 public function setAuthInfo($params = [ "usercode" => null, "token" => null ]) {
119 64 if ($params["usercode"]) {
120 65 $this->usercode = $params["usercode"];
121 66 }
@@ -131,42 +76,8 @@
131 76 public function get_user_info($params = [] ,$auth_info = ["usercode" => null, "token" => null]) {
132 77 $this->setAuthInfo($auth_info);
133 78 return $this->callAPI('GET','');
134 79 }
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 - }
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 80 public function sync_entry($params = [
170 81 "post_title" => null,
171 82 "post_content" => null,
172 83 "post_status" => null, # 0, 1
@@ -191,17 +102,10 @@
191 102 return;
192 103 }
193 104 // TODO: split or html parse? HTMLがつながってる場合はおかしくなる
194 105 // 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 106 $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 - }
107 + $status = $params['post_status'];
204 108 # $body_free = '';
205 109 # $body_paywalled = '';
206 110 # if (count($splited) >= 2) {
207 111 #
@@ -227,17 +131,10 @@
227 131 'limited_count' => isset($codoc_info['limitedCount']) ? $codoc_info['limitedCount'] : 1,
228 132 'affiliate_mode' => isset($codoc_info['affiliateMode']) ? ($codoc_info['affiliateMode'] ? 1 : 0) : 0,
229 133 'affiliate_rate' => isset($codoc_info['affiliateRate']) ? $codoc_info['affiliateRate'] : '0.0500',
230 134 'show_support' => isset($codoc_info['showSupport']) ? ($codoc_info['showSupport'] ? 1 : 0) : 0,
231 - 'show_paywalled_support' => isset($codoc_info['showPaywalledSupport']) ? ($codoc_info['showPaywalledSupport'] ? 1 : 0) : 0,
232 135 'subscriptions' => isset($codoc_info['subscriptions']) ? array_keys($codoc_info['subscriptions']) : [],
233 136 ];
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 137 $entryCode = $params['codoc_entry_code'];
241 138 $res = null;
242 139 if ($entryCode) {
243 140 $res = $this->callAPI('PUT','/entries/' . $entryCode ,$api_params);
@@ -303,137 +200,33 @@
303 200 "post_content" => null,
304 201 "preview" => null,
305 202 "codoc_entry_code" => null,
306 203 "codoc_settings" => null,
307 - "is_amp_endpoint" => null,
308 - "post_permalink" => null,
309 - "codoc_support_entry_code" => null,
310 204 ]) {
311 - $CODOC_SETTINGS = $params['codoc_settings'];
312 -
313 - $has_codoc_tag = '/(<(span|div)[^>]+data-id="codoc-tag"(?:[^>]+|)>(?:.+|)<\/(?:div|span)>)/';
314 205 $post_content = $params['post_content'];
315 206 // codoc タグがあるかどうか (202001:span -> div に変更)
316 - preg_match($has_codoc_tag,$post_content,$matches);
317 -
318 - $show_support_message = '';
319 - if (isset($CODOC_SETTINGS['show_support_message'])) {
320 - $show_support_message = $CODOC_SETTINGS['show_support_message'];
321 - }
322 - $show_support_categories = '';
323 - if (isset($CODOC_SETTINGS['show_support_categories'])) {
324 - $show_support_categories = $CODOC_SETTINGS['show_support_categories'];
325 - }
326 - $show_support_location = 'bottom';
327 - if (isset($CODOC_SETTINGS['show_support_location'])) {
328 - $show_support_location = $CODOC_SETTINGS['show_support_location'];
329 - }
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 - }
349 - $show_support_entry = (is_single() && preg_grep("/(?:$show_support_categories)/",array_map( function($c) { return $c->name; },get_the_category(get_post()->ID))));
350 - if (!$matches && ($support_entry_code = $params['codoc_support_entry_code']) && $show_support_entry) {
351 - $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>',
353 - htmlspecialchars($show_support_message,ENT_QUOTES),
354 - $support_entry_code,
355 - $CODOC_SETTINGS['support_button_text'],
356 - $CODOC_SETTINGS['show_like'],
357 - $CODOC_SETTINGS['show_about_codoc'],
358 - $CODOC_SETTINGS['show_powered_by'],
359 - $CODOC_SETTINGS['show_created_by'],
360 - $CODOC_SETTINGS['show_copyright']
361 - );
362 - $post_content = $show_support_location == 'bottom' ?
363 - $post_content . $support_tag :
364 - $support_tag . $post_content ;
365 - }
366 -
207 + preg_match('/(<(span|div)[^>]+data-id="codoc-tag"(?:[^>]+|)>(?:.+|)<\/(?:div|span)>)/',$post_content,$matches);
367 208 // data-wp-plugin-ver が無いタグは分割しない
368 209 if (!$matches) {
369 210 return $post_content;
370 211 }
371 -
372 212 // codocタグで分割 (202001: gutenbergで直前のdivタグを削除)
373 213 $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 214 if ( $params['preview'] ) {
376 215 $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 216 return $post_content;
379 217 }
380 218 // codoc タグの前後で分ける
381 - $before_splited = preg_split($end_tag_regex,$post_content);
382 - $splited = preg_split($tag_regex,$before_splited[0]);
219 + $splited = preg_split($tag_regex,$post_content);
383 220 // codoc タグにID属性のentrycodeとdata-without-body(無料分を非表示)をつける
384 221 $entryCodeFormated = sprintf('"codoc-entry-%s" ',$params['codoc_entry_code']);
385 -
386 - // 文言系の設定をつける
387 - $tagAttributes = '';
388 - if (isset($CODOC_SETTINGS['show_like']) and $CODOC_SETTINGS['show_like'] != 1) {
389 - $tagAttributes = $tagAttributes . sprintf(' data-show-like="%s"',$CODOC_SETTINGS['show_like']);
390 - }
391 - if (isset($CODOC_SETTINGS['show_about_codoc']) and $CODOC_SETTINGS['show_about_codoc'] != 1) {
392 - $tagAttributes = $tagAttributes . sprintf(' data-show-about-codoc="%s"',$CODOC_SETTINGS['show_about_codoc']);
393 - }
394 - if (isset($CODOC_SETTINGS['show_powered_by']) and $CODOC_SETTINGS['show_powered_by'] != 1) {
395 - $tagAttributes = $tagAttributes . sprintf(' data-show-powered-by="%s"',$CODOC_SETTINGS['show_powered_by']);
396 - }
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'] != '') {
404 - $tagAttributes = $tagAttributes . sprintf(' data-entry-button-text="%s"',$CODOC_SETTINGS['entry_button_text']);
405 - }
406 - if (isset($CODOC_SETTINGS['subscription_button_text']) and $CODOC_SETTINGS['subscription_button_text'] != '') {
407 - $tagAttributes = $tagAttributes . sprintf(' data-subscription-button-text="%s"',$CODOC_SETTINGS['subscription_button_text']);
408 - }
409 - if (isset($CODOC_SETTINGS['support_button_text']) and $CODOC_SETTINGS['support_button_text'] != '') {
410 - $tagAttributes = $tagAttributes . sprintf(' data-support-button-text="%s"',$CODOC_SETTINGS['support_button_text']);
411 - }
412 - if (isset($CODOC_SETTINGS['subscription_message']) and $CODOC_SETTINGS['subscription_message'] != '') {
413 - $tagAttributes = $tagAttributes . sprintf(' data-subscription-message="%s"',$CODOC_SETTINGS['subscription_message']);
414 - }
415 - if (isset($CODOC_SETTINGS['support_message']) and $CODOC_SETTINGS['support_message'] != '') {
416 - $tagAttributes = $tagAttributes . sprintf(' data-support-message="%s"',$CODOC_SETTINGS['support_message']);
417 - }
418 - if (isset($CODOC_SETTINGS['codoc_tag_attributes']) and $CODOC_SETTINGS['codoc_tag_attributes'] != '') {
419 - $tagAttributes = $tagAttributes . sprintf(' %s',$CODOC_SETTINGS['codoc_tag_attributes']);
420 - }
421 -
422 - $codoc_tag = preg_replace('/<((?:span|div)[^>]+)data-id="[^"]+"((?:[^>]+|))>/','<${1} ${2} data-without-body="1" id=' . $entryCodeFormated . $tagAttributes . ">", $matches[1]);
222 + $codoc_tag = preg_replace('/<((?:span|div)[^>]+)data-id="[^"]+"((?:[^>]+|))>/','<${1} ${2} data-without-body="1" id=' . $entryCodeFormated . ">", $matches[1]);
423 223 # THE THOR の page-lp.php が " " をけずるための対策
424 224 # "div class" -> "div class"
425 225 $codoc_tag = preg_replace('/div +class/','div class',$codoc_tag);
426 -
427 - if ($params['is_amp_endpoint']) {
428 - $format = preg_replace_callback('/(<(?:span|div)[^>]+>)((?:[^<>]+|))(<\/(?:span|div)>)/',function($matches) {
429 - $format = $matches[1] . '<a href="%%s">%s</a>' . $matches[3];
430 - return sprintf($format,($matches[2] ? $matches[2] : '続きを読む'));
431 - },$codoc_tag);
432 - $codoc_tag = sprintf($format,$params["post_permalink"]);
433 - }
434 -
226 +
435 227 # タグの前後にHTMLを挿入
228 + $CODOC_SETTINGS = $params['codoc_settings'];
436 229 if (!isset($CODOC_SETTINGS['str_before_codoc_tag'])) {
437 230 $CODOC_SETTINGS['str_before_codoc_tag'] = '';
438 231 }
439 232 if (!isset($CODOC_SETTINGS['str_after_codoc_tag'])) {
@@ -445,104 +238,10 @@
445 238 $CODOC_SETTINGS['str_before_codoc_tag'],
446 239 $codoc_tag,
447 240 $CODOC_SETTINGS['str_after_codoc_tag']
448 241 );
449 - // ::CODOC_WP_END_PAYWALL:: の後を足す
450 - if (isset($before_splited[1]) and $before_splited[1]) {
451 - $post_content .= $before_splited[1];
452 - }
453 242 return $post_content;
454 243 }
455 - /*
456 244
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 - }
245 +
547 246 }
548 247