| @@ -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 |
| @@ -230,14 +148,8 @@ | ||
| 230 | 148 | 'show_support' => isset($codoc_info['showSupport']) ? ($codoc_info['showSupport'] ? 1 : 0) : 0, |
| 231 | 149 | 'show_paywalled_support' => isset($codoc_info['showPaywalledSupport']) ? ($codoc_info['showPaywalledSupport'] ? 1 : 0) : 0, |
| 232 | 150 | 'subscriptions' => isset($codoc_info['subscriptions']) ? array_keys($codoc_info['subscriptions']) : [], |
| 233 | 151 | ]; |
| 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 | 152 | $entryCode = $params['codoc_entry_code']; |
| 241 | 153 | $res = null; |
| 242 | 154 | if ($entryCode) { |
| 243 | 155 | $res = $this->callAPI('PUT','/entries/' . $entryCode ,$api_params); |
| @@ -326,39 +238,19 @@ | ||
| 326 | 238 | $show_support_location = 'bottom'; |
| 327 | 239 | if (isset($CODOC_SETTINGS['show_support_location'])) { |
| 328 | 240 | $show_support_location = $CODOC_SETTINGS['show_support_location']; |
| 329 | 241 | } |
| 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 | - } | |
| 242 | + | |
| 349 | 243 | $show_support_entry = (is_single() && preg_grep("/(?:$show_support_categories)/",array_map( function($c) { return $c->name; },get_the_category(get_post()->ID)))); |
| 350 | 244 | if (!$matches && ($support_entry_code = $params['codoc_support_entry_code']) && $show_support_entry) { |
| 351 | 245 | $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>', | |
| 246 | + '<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 | 247 | htmlspecialchars($show_support_message,ENT_QUOTES), |
| 354 | 248 | $support_entry_code, |
| 355 | 249 | $CODOC_SETTINGS['support_button_text'], |
| 356 | 250 | $CODOC_SETTINGS['show_like'], |
| 357 | 251 | $CODOC_SETTINGS['show_about_codoc'], |
| 358 | - $CODOC_SETTINGS['show_powered_by'], | |
| 359 | - $CODOC_SETTINGS['show_created_by'], | |
| 360 | - $CODOC_SETTINGS['show_copyright'] | |
| 252 | + $CODOC_SETTINGS['show_powered_by'] | |
| 361 | 253 | ); |
| 362 | 254 | $post_content = $show_support_location == 'bottom' ? |
| 363 | 255 | $post_content . $support_tag : |
| 364 | 256 | $support_tag . $post_content ; |
| @@ -393,30 +285,24 @@ | ||
| 393 | 285 | } |
| 394 | 286 | if (isset($CODOC_SETTINGS['show_powered_by']) and $CODOC_SETTINGS['show_powered_by'] != 1) { |
| 395 | 287 | $tagAttributes = $tagAttributes . sprintf(' data-show-powered-by="%s"',$CODOC_SETTINGS['show_powered_by']); |
| 396 | 288 | } |
| 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'] != '') { | |
| 289 | + if ($CODOC_SETTINGS['entry_button_text']) { | |
| 404 | 290 | $tagAttributes = $tagAttributes . sprintf(' data-entry-button-text="%s"',$CODOC_SETTINGS['entry_button_text']); |
| 405 | 291 | } |
| 406 | - if (isset($CODOC_SETTINGS['subscription_button_text']) and $CODOC_SETTINGS['subscription_button_text'] != '') { | |
| 292 | + if ($CODOC_SETTINGS['subscription_button_text']) { | |
| 407 | 293 | $tagAttributes = $tagAttributes . sprintf(' data-subscription-button-text="%s"',$CODOC_SETTINGS['subscription_button_text']); |
| 408 | 294 | } |
| 409 | - if (isset($CODOC_SETTINGS['support_button_text']) and $CODOC_SETTINGS['support_button_text'] != '') { | |
| 295 | + if ($CODOC_SETTINGS['support_button_text']) { | |
| 410 | 296 | $tagAttributes = $tagAttributes . sprintf(' data-support-button-text="%s"',$CODOC_SETTINGS['support_button_text']); |
| 411 | 297 | } |
| 412 | - if (isset($CODOC_SETTINGS['subscription_message']) and $CODOC_SETTINGS['subscription_message'] != '') { | |
| 298 | + if ($CODOC_SETTINGS['subscription_message']) { | |
| 413 | 299 | $tagAttributes = $tagAttributes . sprintf(' data-subscription-message="%s"',$CODOC_SETTINGS['subscription_message']); |
| 414 | 300 | } |
| 415 | - if (isset($CODOC_SETTINGS['support_message']) and $CODOC_SETTINGS['support_message'] != '') { | |
| 301 | + if ($CODOC_SETTINGS['support_message']) { | |
| 416 | 302 | $tagAttributes = $tagAttributes . sprintf(' data-support-message="%s"',$CODOC_SETTINGS['support_message']); |
| 417 | 303 | } |
| 418 | - if (isset($CODOC_SETTINGS['codoc_tag_attributes']) and $CODOC_SETTINGS['codoc_tag_attributes'] != '') { | |
| 304 | + if ($CODOC_SETTINGS['codoc_tag_attributes']) { | |
| 419 | 305 | $tagAttributes = $tagAttributes . sprintf(' %s',$CODOC_SETTINGS['codoc_tag_attributes']); |
| 420 | 306 | } |
| 421 | 307 | |
| 422 | 308 | $codoc_tag = preg_replace('/<((?:span|div)[^>]+)data-id="[^"]+"((?:[^>]+|))>/','<${1} ${2} data-without-body="1" id=' . $entryCodeFormated . $tagAttributes . ">", $matches[1]); |
| @@ -451,98 +337,8 @@ | ||
| 451 | 337 | $post_content .= $before_splited[1]; |
| 452 | 338 | } |
| 453 | 339 | return $post_content; |
| 454 | 340 | } |
| 455 | - /* | |
| 456 | 341 | |
| 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 | - } | |
| 342 | + | |
| 547 | 343 | } |
| 548 | 344 | |