PluginProbe
codoc / 0.9.51
codoc v0.9.51
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 +180 -13 0.9.90.9.51 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;
@@ -62,8 +67,49 @@
62 67 } else {
63 68 return null;
64 69 }
65 70 }
71 + public function callPaywallAPI($method,$path,$body = [],$headers = []) {
72 + $site_usercode = get_option(CODOC_USERCODE_OPTION_NAME);
73 + $paywall_token = $this->get_paywall_token_code();
74 +
75 + $host = $this->codoc_url;
76 + $sslverify = true;
77 +
78 + if (preg_match('/local/',$this->codoc_url)) {
79 + $sslverify = false;
80 + $host = 'https://host.docker.internal';
81 + }
82 + $url = sprintf("%s/api/v1/paywall/%s%s",$host,$site_usercode,$path);
83 + $http = new WP_Http();
84 + try {
85 + $response = $http->request(
86 + $url,
87 + [
88 + 'sslverify' => $sslverify,
89 + 'method' => $method,
90 + 'timeout' => 10,
91 + 'headers' => $headers,
92 + 'body' => $body,
93 + ]
94 + );
95 + if ( is_wp_error($response) || $response['response']['code'] != 200 ) {
96 + //何もしない
97 + //var_dump( $response );
98 + }
99 + } catch(Exception $e) {
100 +
101 + }
102 + if (!is_wp_error($response) and
103 + isset($response['body']) and
104 + is_string($response['body']) and
105 + is_array(json_decode($response['body'], true)) and
106 + (json_last_error() == JSON_ERROR_NONE)) {
107 + return json_decode($response['body']);
108 + } else {
109 + return null;
110 + }
111 + }
66 112 public function setAuthInfo($params = [ "usercode" => null, "token" => null ]) {
67 113 if ($params["usercode"]) {
68 114 $this->usercode = $params["usercode"];
69 115 }
@@ -109,10 +155,17 @@
109 155 return;
110 156 }
111 157 // TODO: split or html parse? HTMLがつながってる場合はおかしくなる
112 158 // classic(tinymce)版の場合p、もしくはdivにかこまれている
159 + $end_tag_regex = '/<(?:div|p)>::CODOC_WP_END_PAYWALL::<\/(?:div|p)>/';
160 + $before_splited = preg_split($end_tag_regex,$post_content);
161 + $post_content = $before_splited[0];
113 162 $splited = preg_split('/(?:<(?:div|p)(?:[^>]+|)>|)<\!-- +wp:codoc\/codoc-block .*<\!-- +\/wp:codoc\/codoc-block +-->(?:<\/(?:div|p)>|)/s',$post_content);
114 - $status = $params['post_status'];
163 + $status = $params['post_status']; # 0: 非公開 1: 公開 2:限定公開(パスワードの場合は限定公開で同期する)
164 + // codoc設定で限定公開を有効で、公開の場合は 限定公開にする
165 + if ($status == 1 and isset($codoc_info['statusLimited']) and $codoc_info['statusLimited']) {
166 + $status = 2;
167 + }
115 168 # $body_free = '';
116 169 # $body_paywalled = '';
117 170 # if (count($splited) >= 2) {
118 171 #
@@ -231,19 +284,39 @@
231 284 $show_support_location = 'bottom';
232 285 if (isset($CODOC_SETTINGS['show_support_location'])) {
233 286 $show_support_location = $CODOC_SETTINGS['show_support_location'];
234 287 }
235 -
288 + // デフォルト値をうめておく
289 + if (!isset($CODOC_SETTINGS['support_button_text'])) {
290 + $CODOC_SETTINGS['support_button_text'] = 'サポートする';
291 + }
292 + if (!isset($CODOC_SETTINGS['show_like'])) {
293 + $CODOC_SETTINGS['show_like'] = 1;
294 + }
295 + if (!isset($CODOC_SETTINGS['show_about_codoc'])) {
296 + $CODOC_SETTINGS['show_about_codoc'] = 1;
297 + }
298 + if (!isset($CODOC_SETTINGS['show_powered_by'])) {
299 + $CODOC_SETTINGS['show_powered_by'] = 1;
300 + }
301 + if (!isset($CODOC_SETTINGS['show_created_by'])) {
302 + $CODOC_SETTINGS['show_created_by'] = 1;
303 + }
304 + if (!isset($CODOC_SETTINGS['show_copyright'])) {
305 + $CODOC_SETTINGS['show_copyright'] = 1;
306 + }
236 307 $show_support_entry = (is_single() && preg_grep("/(?:$show_support_categories)/",array_map( function($c) { return $c->name; },get_the_category(get_post()->ID))));
237 308 if (!$matches && ($support_entry_code = $params['codoc_support_entry_code']) && $show_support_entry) {
238 309 $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>',
310 + '<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 311 htmlspecialchars($show_support_message,ENT_QUOTES),
241 312 $support_entry_code,
242 313 $CODOC_SETTINGS['support_button_text'],
243 314 $CODOC_SETTINGS['show_like'],
244 315 $CODOC_SETTINGS['show_about_codoc'],
245 - $CODOC_SETTINGS['show_powered_by']
316 + $CODOC_SETTINGS['show_powered_by'],
317 + $CODOC_SETTINGS['show_created_by'],
318 + $CODOC_SETTINGS['show_copyright']
246 319 );
247 320 $post_content = $show_support_location == 'bottom' ?
248 321 $post_content . $support_tag :
249 322 $support_tag . $post_content ;
@@ -255,14 +328,17 @@
255 328 }
256 329
257 330 // codocタグで分割 (202001: gutenbergで直前のdivタグを削除)
258 331 $tag_regex = '/(?:<div(?:[^>]+|)>|)<(?:span|div)[^>]+data-id="codoc-tag"(?:[^>]+|)>(?:.+|)<\/(?:span|div)>(?:<\/div>|)/';
332 + $end_tag_regex = '/<(?:div|p)>::CODOC_WP_END_PAYWALL::<\/(?:div|p)>/';
259 333 if ( $params['preview'] ) {
260 334 $post_content = preg_replace($tag_regex,'<div class="codoc-continue">ここから上は無料で表示されます</div>',$post_content);
335 + $post_content = preg_replace($end_tag_regex,'<div class="codoc-continue">ここから下は無料で表示されます</div>',$post_content);
261 336 return $post_content;
262 337 }
263 338 // codoc タグの前後で分ける
264 - $splited = preg_split($tag_regex,$post_content);
339 + $before_splited = preg_split($end_tag_regex,$post_content);
340 + $splited = preg_split($tag_regex,$before_splited[0]);
265 341 // codoc タグにID属性のentrycodeとdata-without-body(無料分を非表示)をつける
266 342 $entryCodeFormated = sprintf('"codoc-entry-%s" ',$params['codoc_entry_code']);
267 343
268 344 // 文言系の設定をつける
@@ -275,24 +351,30 @@
275 351 }
276 352 if (isset($CODOC_SETTINGS['show_powered_by']) and $CODOC_SETTINGS['show_powered_by'] != 1) {
277 353 $tagAttributes = $tagAttributes . sprintf(' data-show-powered-by="%s"',$CODOC_SETTINGS['show_powered_by']);
278 354 }
279 - if ($CODOC_SETTINGS['entry_button_text']) {
355 + if (isset($CODOC_SETTINGS['show_created_by']) and $CODOC_SETTINGS['show_created_by'] != 1) {
356 + $tagAttributes = $tagAttributes . sprintf(' data-show-created-by="%s"',$CODOC_SETTINGS['show_created_by']);
357 + }
358 + if (isset($CODOC_SETTINGS['show_copyright']) and $CODOC_SETTINGS['show_copyright'] != 1) {
359 + $tagAttributes = $tagAttributes . sprintf(' data-show-copyright="%s"',$CODOC_SETTINGS['show_copyright']);
360 + }
361 + if (isset($CODOC_SETTINGS['entry_button_text']) and $CODOC_SETTINGS['entry_button_text'] != '') {
280 362 $tagAttributes = $tagAttributes . sprintf(' data-entry-button-text="%s"',$CODOC_SETTINGS['entry_button_text']);
281 363 }
282 - if ($CODOC_SETTINGS['subscription_button_text']) {
364 + if (isset($CODOC_SETTINGS['subscription_button_text']) and $CODOC_SETTINGS['subscription_button_text'] != '') {
283 365 $tagAttributes = $tagAttributes . sprintf(' data-subscription-button-text="%s"',$CODOC_SETTINGS['subscription_button_text']);
284 366 }
285 - if ($CODOC_SETTINGS['support_button_text']) {
367 + if (isset($CODOC_SETTINGS['support_button_text']) and $CODOC_SETTINGS['support_button_text'] != '') {
286 368 $tagAttributes = $tagAttributes . sprintf(' data-support-button-text="%s"',$CODOC_SETTINGS['support_button_text']);
287 369 }
288 - if ($CODOC_SETTINGS['subscription_message']) {
370 + if (isset($CODOC_SETTINGS['subscription_message']) and $CODOC_SETTINGS['subscription_message'] != '') {
289 371 $tagAttributes = $tagAttributes . sprintf(' data-subscription-message="%s"',$CODOC_SETTINGS['subscription_message']);
290 372 }
291 - if ($CODOC_SETTINGS['support_message']) {
373 + if (isset($CODOC_SETTINGS['support_message']) and $CODOC_SETTINGS['support_message'] != '') {
292 374 $tagAttributes = $tagAttributes . sprintf(' data-support-message="%s"',$CODOC_SETTINGS['support_message']);
293 375 }
294 - if ($CODOC_SETTINGS['codoc_tag_attributes']) {
376 + if (isset($CODOC_SETTINGS['codoc_tag_attributes']) and $CODOC_SETTINGS['codoc_tag_attributes'] != '') {
295 377 $tagAttributes = $tagAttributes . sprintf(' %s',$CODOC_SETTINGS['codoc_tag_attributes']);
296 378 }
297 379
298 380 $codoc_tag = preg_replace('/<((?:span|div)[^>]+)data-id="[^"]+"((?:[^>]+|))>/','<${1} ${2} data-without-body="1" id=' . $entryCodeFormated . $tagAttributes . ">", $matches[1]);
@@ -321,10 +403,95 @@
321 403 $CODOC_SETTINGS['str_before_codoc_tag'],
322 404 $codoc_tag,
323 405 $CODOC_SETTINGS['str_after_codoc_tag']
324 406 );
407 + // ::CODOC_WP_END_PAYWALL:: の後を足す
408 + if (isset($before_splited[1]) and $before_splited[1]) {
409 + $post_content .= $before_splited[1];
410 + }
325 411 return $post_content;
326 412 }
413 + /*
327 414
328 -
415 + // Cookieの中のトークンを取得
416 + $_CODOC->util->get_paywall_token_code();
417 +
418 + */
419 + function get_paywall_token_code() {
420 + if (isset($_COOKIE['codocTokenCode']) and $codocTokenCode = $_COOKIE['codocTokenCode']) {
421 + return $codocTokenCode;
422 + }
423 + return null;
424 + }
425 + /*
426 +
427 + $_CODOC->util->check_login($mode); // cookie / strict
428 +
429 + */
430 + function check_login($mode = 'cookie') {
431 + if ($codocTokenCode = $this->get_paywall_token_code()) {
432 + if ($mode == 'cookie') {
433 + // cookieの中にtokenがあればOK
434 + return true;
435 + }
436 + if ($mode == 'strict') {
437 + // ログイン状態を問い合わせ
438 + $res = $this->callPaywallAPI('GET','/users',["paywall_token_code" => $codocTokenCode ]);
439 + if ($res->status) {
440 + return true;
441 + } else {
442 + return false;
443 + }
444 + }
445 + }
446 + return false;
447 + }
448 + /*
449 +
450 + $_CODOC->util->check_owned($entry_code);
451 +
452 + */
453 + function check_owned($entry_code = null) {
454 + if (!$entry_code) {
455 + return false;
456 + }
457 + $res = $this->callPaywallAPI('GET','/entries/' . $entry_code, ["paywall_token_code" => $this->get_paywall_token_code(), "check_owned" => 1 ]);
458 + if ($res->status) {
459 + return true;
460 + }
461 + return false;
462 + }
463 +
464 + /*
465 + $_CODOC->util->health_check();
466 + */
467 + function health_check() {
468 + $sslverify = true;
469 + $host = $this->codoc_url;
470 + if (preg_match('/local/',$this->codoc_url)) {
471 + $sslverify = false;
472 + $host = 'https://host.docker.internal';
473 + }
474 + $url = sprintf("%s/codoc_health_check",$host);
475 + $http = new WP_Http();
476 + try {
477 + $response = $http->request(
478 + $url,
479 + [
480 + 'sslverify' => $sslverify,
481 + 'method' => 'GET',
482 + 'timeout' => 10,
483 + 'headers' => [],
484 + 'body' => [],
485 + ]
486 + );
487 + } catch(Exception $e) {
488 +
489 + }
490 + if ($response and $response['response']['code'] == '200') {
491 + return true;
492 + } else {
493 + return null;
494 + }
495 + }
329 496 }
330 497