| @@ -3,28 +3,39 @@ | ||
| 3 | 3 | if (!defined('WPINC')) { |
| 4 | 4 | define( 'WPINC', 'wp-includes' ); |
| 5 | 5 | } |
| 6 | 6 | |
| 7 | -require_once( ABSPATH . WPINC . '/class-http.php'); | |
| 7 | +if (!defined('CODOC_URL')) { | |
| 8 | + define( 'CODOC_URL', 'https://codoc.jp' ); | |
| 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 | + | |
| 8 | 17 | require_once( ABSPATH . WPINC . '/class-wp-error.php'); |
| 9 | 18 | |
| 10 | 19 | final class CodocUtil { |
| 11 | - | |
| 12 | - public function __construct($params = ['usercode' => null, 'token' => null]) { | |
| 13 | - $this->usercode = $params['usercode'] or die('no usercode'); | |
| 14 | - $this->token = $params['token'] or die('no token'); | |
| 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 | 26 | return $this; |
| 16 | 27 | } |
| 17 | - | |
| 28 | + | |
| 18 | 29 | public function callAPI($method,$path,$body = [],$headers = []) { |
| 19 | 30 | $usercode = $this->usercode; |
| 20 | 31 | $token = $this->token; |
| 21 | 32 | |
| 22 | 33 | $headers['X-CodocToken'] = $token; |
| 23 | - $host = CODOC_URL; | |
| 34 | + $host = $this->codoc_url; | |
| 24 | 35 | $sslverify = true; |
| 25 | 36 | |
| 26 | - if (preg_match('/local/',CODOC_URL)) { | |
| 37 | + if (preg_match('/local/',$this->codoc_url)) { | |
| 27 | 38 | $sslverify = false; |
| 28 | 39 | $host = 'https://host.docker.internal'; |
| 29 | 40 | } |
| 30 | 41 | $url = sprintf("%s/api/v1/cms/%s%s",$host,$usercode,$path); |
| @@ -39,8 +50,14 @@ | ||
| 39 | 50 | 'headers' => $headers, |
| 40 | 51 | 'body' => $body, |
| 41 | 52 | ] |
| 42 | 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 | + } | |
| 43 | 60 | if ( is_wp_error($response) || $response['response']['code'] != 200 ) { |
| 44 | 61 | //何もしない |
| 45 | 62 | //var_dump( $response ); |
| 46 | 63 | } |
| @@ -56,9 +73,100 @@ | ||
| 56 | 73 | } else { |
| 57 | 74 | return null; |
| 58 | 75 | } |
| 59 | 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(); | |
| 60 | 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 | + public function setAuthInfo($params = [ "usercode" => null, "token" => null ]) { | |
| 119 | + if ($params["usercode"]) { | |
| 120 | + $this->usercode = $params["usercode"]; | |
| 121 | + } | |
| 122 | + if ($params["token"]) { | |
| 123 | + $this->token = $params["token"]; | |
| 124 | + } | |
| 125 | + return true; | |
| 126 | + } | |
| 127 | + public function get_token($params = ["fetch_token_key" => null],$auth_info = ["usercode" => null, "token" => null]) { | |
| 128 | + $this->setAuthInfo($auth_info); | |
| 129 | + return $this->callAPI('GET','/token',[ "fetch_token_key" => $params["fetch_token_key"] ]); | |
| 130 | + } | |
| 131 | + public function get_user_info($params = [] ,$auth_info = ["usercode" => null, "token" => null]) { | |
| 132 | + $this->setAuthInfo($auth_info); | |
| 133 | + return $this->callAPI('GET',''); | |
| 134 | + } | |
| 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 | + | |
| 61 | 169 | public function sync_entry($params = [ |
| 62 | 170 | "post_title" => null, |
| 63 | 171 | "post_content" => null, |
| 64 | 172 | "post_status" => null, # 0, 1 |
| @@ -63,9 +171,12 @@ | ||
| 63 | 171 | "post_content" => null, |
| 64 | 172 | "post_status" => null, # 0, 1 |
| 65 | 173 | "post_permalink" => null, |
| 66 | 174 | "codoc_entry_code" => null, |
| 67 | - ]) { | |
| 175 | + ],$auth_info = ["usercode" => null, "token" => null]) { | |
| 176 | + | |
| 177 | + $this->setAuthInfo($auth_info); | |
| 178 | + | |
| 68 | 179 | $post_content = $params['post_content']; |
| 69 | 180 | preg_match('/wp:codoc\/codoc-block +?({.*})/',$post_content,$matches); |
| 70 | 181 | // GUTENBERG で指定されたjson(記事情報)がない場合 |
| 71 | 182 | if (!$matches) { |
| @@ -80,10 +191,17 @@ | ||
| 80 | 191 | return; |
| 81 | 192 | } |
| 82 | 193 | // TODO: split or html parse? HTMLがつながってる場合はおかしくなる |
| 83 | 194 | // 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]; | |
| 84 | 198 | $splited = preg_split('/(?:<(?:div|p)(?:[^>]+|)>|)<\!-- +wp:codoc\/codoc-block .*<\!-- +\/wp:codoc\/codoc-block +-->(?:<\/(?:div|p)>|)/s',$post_content); |
| 85 | - $status = $params['post_status']; | |
| 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 | + } | |
| 86 | 204 | # $body_free = ''; |
| 87 | 205 | # $body_paywalled = ''; |
| 88 | 206 | # if (count($splited) >= 2) { |
| 89 | 207 | # |
| @@ -109,10 +227,21 @@ | ||
| 109 | 227 | 'limited_count' => isset($codoc_info['limitedCount']) ? $codoc_info['limitedCount'] : 1, |
| 110 | 228 | 'affiliate_mode' => isset($codoc_info['affiliateMode']) ? ($codoc_info['affiliateMode'] ? 1 : 0) : 0, |
| 111 | 229 | 'affiliate_rate' => isset($codoc_info['affiliateRate']) ? $codoc_info['affiliateRate'] : '0.0500', |
| 112 | 230 | '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, | |
| 113 | 232 | 'subscriptions' => isset($codoc_info['subscriptions']) ? array_keys($codoc_info['subscriptions']) : [], |
| 114 | 233 | ]; |
| 234 | + // ゲスト購入の可否 (未設定 = 旧バージョンのブロックの場合は送信せず本体側の既存値を維持) | |
| 235 | + if (isset($codoc_info['showPayWithoutAccountButton'])) { | |
| 236 | + $api_params['show_pay_without_account_button'] = $codoc_info['showPayWithoutAccountButton'] ? 1 : 0; | |
| 237 | + } | |
| 238 | + // API側と統一した文字数バリデーション | |
| 239 | + $errors = $this->validate_entry_lengths($api_params); | |
| 240 | + if ($errors) { | |
| 241 | + set_transient('codoc_entry_validation_errors', $errors, MINUTE_IN_SECONDS * 5); | |
| 242 | + return null; | |
| 243 | + } | |
| 115 | 244 | $entryCode = $params['codoc_entry_code']; |
| 116 | 245 | $res = null; |
| 117 | 246 | if ($entryCode) { |
| 118 | 247 | $res = $this->callAPI('PUT','/entries/' . $entryCode ,$api_params); |
| @@ -125,9 +254,12 @@ | ||
| 125 | 254 | function post_thumbnail( $params = [ |
| 126 | 255 | "file_path" => null, |
| 127 | 256 | "boundary" => null, |
| 128 | 257 | "codoc_entry_code" => null, |
| 129 | - ] ) { | |
| 258 | + ], $auth_info = ["usercode" => null, "token" => null] ) { | |
| 259 | + | |
| 260 | + $this->setAuthInfo($auth_info); | |
| 261 | + | |
| 130 | 262 | $file_path = $params['file_path']; |
| 131 | 263 | $boundary = $params['boundary']; # ランダム変数24桁 wp_generate_password(24); |
| 132 | 264 | $res = null; |
| 133 | 265 | if (is_readable($file_path)) { |
| @@ -157,9 +289,12 @@ | ||
| 157 | 289 | } |
| 158 | 290 | |
| 159 | 291 | function reset_thumbnail( $params = [ |
| 160 | 292 | "codoc_entry_code" => null, |
| 161 | - ]) { | |
| 293 | + ],$auth_info = ["usercode" => null, "token" => null] ) { | |
| 294 | + | |
| 295 | + $this->setAuthInfo($auth_info); | |
| 296 | + | |
| 162 | 297 | $entryCode = $params['codoc_entry_code']; |
| 163 | 298 | $res = $this->callAPI( |
| 164 | 299 | 'POST', |
| 165 | 300 | '/entries/' . $entryCode . '/thumbnail', |
| @@ -171,35 +306,138 @@ | ||
| 171 | 306 | function filter_content( $params = [ |
| 172 | 307 | "post_content" => null, |
| 173 | 308 | "preview" => null, |
| 174 | 309 | "codoc_entry_code" => null, |
| 310 | + "codoc_settings" => null, | |
| 311 | + "is_amp_endpoint" => null, | |
| 312 | + "post_permalink" => null, | |
| 313 | + "codoc_support_entry_code" => null, | |
| 175 | 314 | ]) { |
| 315 | + $CODOC_SETTINGS = $params['codoc_settings']; | |
| 316 | + | |
| 317 | + $has_codoc_tag = '/(<(span|div)[^>]+data-id="codoc-tag"(?:[^>]+|)>(?:.+|)<\/(?:div|span)>)/'; | |
| 176 | 318 | $post_content = $params['post_content']; |
| 177 | 319 | // codoc タグがあるかどうか (202001:span -> div に変更) |
| 178 | - preg_match('/(<(span|div)[^>]+data-id="codoc-tag"(?:[^>]+|)>(?:.+|)<\/(?:div|span)>)/',$post_content,$matches); | |
| 320 | + preg_match($has_codoc_tag,$post_content,$matches); | |
| 321 | + | |
| 322 | + $show_support_message = ''; | |
| 323 | + if (isset($CODOC_SETTINGS['show_support_message'])) { | |
| 324 | + $show_support_message = $CODOC_SETTINGS['show_support_message']; | |
| 325 | + } | |
| 326 | + $show_support_categories = ''; | |
| 327 | + if (isset($CODOC_SETTINGS['show_support_categories'])) { | |
| 328 | + $show_support_categories = $CODOC_SETTINGS['show_support_categories']; | |
| 329 | + } | |
| 330 | + $show_support_location = 'bottom'; | |
| 331 | + if (isset($CODOC_SETTINGS['show_support_location'])) { | |
| 332 | + $show_support_location = $CODOC_SETTINGS['show_support_location']; | |
| 333 | + } | |
| 334 | + // デフォルト値をうめておく | |
| 335 | + if (!isset($CODOC_SETTINGS['support_button_text'])) { | |
| 336 | + $CODOC_SETTINGS['support_button_text'] = 'サポートする'; | |
| 337 | + } | |
| 338 | + if (!isset($CODOC_SETTINGS['show_like'])) { | |
| 339 | + $CODOC_SETTINGS['show_like'] = 1; | |
| 340 | + } | |
| 341 | + if (!isset($CODOC_SETTINGS['show_about_codoc'])) { | |
| 342 | + $CODOC_SETTINGS['show_about_codoc'] = 1; | |
| 343 | + } | |
| 344 | + if (!isset($CODOC_SETTINGS['show_powered_by'])) { | |
| 345 | + $CODOC_SETTINGS['show_powered_by'] = 1; | |
| 346 | + } | |
| 347 | + if (!isset($CODOC_SETTINGS['show_created_by'])) { | |
| 348 | + $CODOC_SETTINGS['show_created_by'] = 1; | |
| 349 | + } | |
| 350 | + if (!isset($CODOC_SETTINGS['show_copyright'])) { | |
| 351 | + $CODOC_SETTINGS['show_copyright'] = 1; | |
| 352 | + } | |
| 353 | + $show_support_entry = (is_single() && preg_grep("/(?:$show_support_categories)/",array_map( function($c) { return $c->name; },get_the_category(get_post()->ID)))); | |
| 354 | + if (!$matches && ($support_entry_code = $params['codoc_support_entry_code']) && $show_support_entry) { | |
| 355 | + $support_tag = sprintf( | |
| 356 | + '<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>', | |
| 357 | + htmlspecialchars($show_support_message,ENT_QUOTES), | |
| 358 | + $support_entry_code, | |
| 359 | + $CODOC_SETTINGS['support_button_text'], | |
| 360 | + $CODOC_SETTINGS['show_like'], | |
| 361 | + $CODOC_SETTINGS['show_about_codoc'], | |
| 362 | + $CODOC_SETTINGS['show_powered_by'], | |
| 363 | + $CODOC_SETTINGS['show_created_by'], | |
| 364 | + $CODOC_SETTINGS['show_copyright'] | |
| 365 | + ); | |
| 366 | + $post_content = $show_support_location == 'bottom' ? | |
| 367 | + $post_content . $support_tag : | |
| 368 | + $support_tag . $post_content ; | |
| 369 | + } | |
| 370 | + | |
| 179 | 371 | // data-wp-plugin-ver が無いタグは分割しない |
| 180 | 372 | if (!$matches) { |
| 181 | 373 | return $post_content; |
| 182 | 374 | } |
| 375 | + | |
| 183 | 376 | // codocタグで分割 (202001: gutenbergで直前のdivタグを削除) |
| 184 | 377 | $tag_regex = '/(?:<div(?:[^>]+|)>|)<(?:span|div)[^>]+data-id="codoc-tag"(?:[^>]+|)>(?:.+|)<\/(?:span|div)>(?:<\/div>|)/'; |
| 378 | + $end_tag_regex = '/<(?:div|p)>::CODOC_WP_END_PAYWALL::<\/(?:div|p)>/'; | |
| 185 | 379 | if ( $params['preview'] ) { |
| 186 | 380 | $post_content = preg_replace($tag_regex,'<div class="codoc-continue">ここから上は無料で表示されます</div>',$post_content); |
| 381 | + $post_content = preg_replace($end_tag_regex,'<div class="codoc-continue">ここから下は無料で表示されます</div>',$post_content); | |
| 187 | 382 | return $post_content; |
| 188 | 383 | } |
| 189 | 384 | // codoc タグの前後で分ける |
| 190 | - $splited = preg_split($tag_regex,$post_content); | |
| 385 | + $before_splited = preg_split($end_tag_regex,$post_content); | |
| 386 | + $splited = preg_split($tag_regex,$before_splited[0]); | |
| 191 | 387 | // codoc タグにID属性のentrycodeとdata-without-body(無料分を非表示)をつける |
| 192 | - $entryCode = $params['codoc_entry_code']; | |
| 193 | - $codoc_tag = preg_replace('/<((?:span|div)[^>]+)data-id="[^"]+"((?:[^>]+|))>/','<${1} ${2} data-without-body="1" id=' . $entryCode . ">", $matches[1]); | |
| 388 | + $entryCodeFormated = sprintf('"codoc-entry-%s" ',$params['codoc_entry_code']); | |
| 389 | + | |
| 390 | + // 文言系の設定をつける | |
| 391 | + $tagAttributes = ''; | |
| 392 | + if (isset($CODOC_SETTINGS['show_like']) and $CODOC_SETTINGS['show_like'] != 1) { | |
| 393 | + $tagAttributes = $tagAttributes . sprintf(' data-show-like="%s"',$CODOC_SETTINGS['show_like']); | |
| 394 | + } | |
| 395 | + if (isset($CODOC_SETTINGS['show_about_codoc']) and $CODOC_SETTINGS['show_about_codoc'] != 1) { | |
| 396 | + $tagAttributes = $tagAttributes . sprintf(' data-show-about-codoc="%s"',$CODOC_SETTINGS['show_about_codoc']); | |
| 397 | + } | |
| 398 | + if (isset($CODOC_SETTINGS['show_powered_by']) and $CODOC_SETTINGS['show_powered_by'] != 1) { | |
| 399 | + $tagAttributes = $tagAttributes . sprintf(' data-show-powered-by="%s"',$CODOC_SETTINGS['show_powered_by']); | |
| 400 | + } | |
| 401 | + if (isset($CODOC_SETTINGS['show_created_by']) and $CODOC_SETTINGS['show_created_by'] != 1) { | |
| 402 | + $tagAttributes = $tagAttributes . sprintf(' data-show-created-by="%s"',$CODOC_SETTINGS['show_created_by']); | |
| 403 | + } | |
| 404 | + if (isset($CODOC_SETTINGS['show_copyright']) and $CODOC_SETTINGS['show_copyright'] != 1) { | |
| 405 | + $tagAttributes = $tagAttributes . sprintf(' data-show-copyright="%s"',$CODOC_SETTINGS['show_copyright']); | |
| 406 | + } | |
| 407 | + if (isset($CODOC_SETTINGS['entry_button_text']) and $CODOC_SETTINGS['entry_button_text'] != '') { | |
| 408 | + $tagAttributes = $tagAttributes . sprintf(' data-entry-button-text="%s"',$CODOC_SETTINGS['entry_button_text']); | |
| 409 | + } | |
| 410 | + if (isset($CODOC_SETTINGS['subscription_button_text']) and $CODOC_SETTINGS['subscription_button_text'] != '') { | |
| 411 | + $tagAttributes = $tagAttributes . sprintf(' data-subscription-button-text="%s"',$CODOC_SETTINGS['subscription_button_text']); | |
| 412 | + } | |
| 413 | + if (isset($CODOC_SETTINGS['support_button_text']) and $CODOC_SETTINGS['support_button_text'] != '') { | |
| 414 | + $tagAttributes = $tagAttributes . sprintf(' data-support-button-text="%s"',$CODOC_SETTINGS['support_button_text']); | |
| 415 | + } | |
| 416 | + if (isset($CODOC_SETTINGS['subscription_message']) and $CODOC_SETTINGS['subscription_message'] != '') { | |
| 417 | + $tagAttributes = $tagAttributes . sprintf(' data-subscription-message="%s"',$CODOC_SETTINGS['subscription_message']); | |
| 418 | + } | |
| 419 | + if (isset($CODOC_SETTINGS['support_message']) and $CODOC_SETTINGS['support_message'] != '') { | |
| 420 | + $tagAttributes = $tagAttributes . sprintf(' data-support-message="%s"',$CODOC_SETTINGS['support_message']); | |
| 421 | + } | |
| 422 | + if (isset($CODOC_SETTINGS['codoc_tag_attributes']) and $CODOC_SETTINGS['codoc_tag_attributes'] != '') { | |
| 423 | + $tagAttributes = $tagAttributes . sprintf(' %s',$CODOC_SETTINGS['codoc_tag_attributes']); | |
| 424 | + } | |
| 425 | + | |
| 426 | + $codoc_tag = preg_replace('/<((?:span|div)[^>]+)data-id="[^"]+"((?:[^>]+|))>/','<${1} ${2} data-without-body="1" id=' . $entryCodeFormated . $tagAttributes . ">", $matches[1]); | |
| 194 | 427 | # THE THOR の page-lp.php が " " をけずるための対策 |
| 195 | 428 | # "div class" -> "div class" |
| 196 | 429 | $codoc_tag = preg_replace('/div +class/','div class',$codoc_tag); |
| 197 | - | |
| 430 | + | |
| 431 | + if ($params['is_amp_endpoint']) { | |
| 432 | + $format = preg_replace_callback('/(<(?:span|div)[^>]+>)((?:[^<>]+|))(<\/(?:span|div)>)/',function($matches) { | |
| 433 | + $format = $matches[1] . '<a href="%%s">%s</a>' . $matches[3]; | |
| 434 | + return sprintf($format,($matches[2] ? $matches[2] : '続きを読む')); | |
| 435 | + },$codoc_tag); | |
| 436 | + $codoc_tag = sprintf($format,$params["post_permalink"]); | |
| 437 | + } | |
| 438 | + | |
| 198 | 439 | # タグの前後にHTMLを挿入 |
| 199 | - $CODOC_SETTINGS = $params['codoc_settings']; | |
| 200 | - $before = ''; | |
| 201 | - $after = ''; | |
| 202 | 440 | if (!isset($CODOC_SETTINGS['str_before_codoc_tag'])) { |
| 203 | 441 | $CODOC_SETTINGS['str_before_codoc_tag'] = ''; |
| 204 | 442 | } |
| 205 | 443 | if (!isset($CODOC_SETTINGS['str_after_codoc_tag'])) { |
| @@ -204,14 +442,111 @@ | ||
| 204 | 442 | } |
| 205 | 443 | if (!isset($CODOC_SETTINGS['str_after_codoc_tag'])) { |
| 206 | 444 | $CODOC_SETTINGS['str_after_codoc_tag'] = ''; |
| 207 | 445 | } |
| 208 | - $before = $CODOC_SETTINGS['str_before_codoc_tag'] or ""; | |
| 209 | - $after = $CODOC_SETTINGS['str_after_codoc_tag'] or ""; | |
| 210 | - | |
| 211 | 446 | // 無料部分のみ表示 |
| 212 | - return $splited[0] . sprintf('%s<div class="wp-block-codoc-codoc-block">%s</div>%s',$before,$codoc_tag,$after); | |
| 447 | + $post_content = $splited[0] . sprintf( | |
| 448 | + '%s<div class="wp-block-codoc-codoc-block">%s</div>%s', | |
| 449 | + $CODOC_SETTINGS['str_before_codoc_tag'], | |
| 450 | + $codoc_tag, | |
| 451 | + $CODOC_SETTINGS['str_after_codoc_tag'] | |
| 452 | + ); | |
| 453 | + // ::CODOC_WP_END_PAYWALL:: の後を足す | |
| 454 | + if (isset($before_splited[1]) and $before_splited[1]) { | |
| 455 | + $post_content .= $before_splited[1]; | |
| 456 | + } | |
| 457 | + return $post_content; | |
| 213 | 458 | } |
| 459 | + /* | |
| 214 | 460 | |
| 215 | - | |
| 461 | + // Cookieの中のトークンを取得 | |
| 462 | + $_CODOC->util->get_paywall_token_code(); | |
| 463 | + | |
| 464 | + */ | |
| 465 | + function get_paywall_token_code() { | |
| 466 | + if (isset($_COOKIE['codocTokenCode']) and $codocTokenCode = $_COOKIE['codocTokenCode']) { | |
| 467 | + return $codocTokenCode; | |
| 468 | + } | |
| 469 | + return null; | |
| 470 | + } | |
| 471 | + /* | |
| 472 | + | |
| 473 | + $_CODOC->util->check_login($mode); // cookie / strict | |
| 474 | + | |
| 475 | + $response = $_CODOC->util->check_login('strict_with_data'); | |
| 476 | + if ($response->status and $response->user) { | |
| 477 | + echo $reponse->user->code; | |
| 478 | + } | |
| 479 | + | |
| 480 | + */ | |
| 481 | + function check_login($mode = 'cookie') { | |
| 482 | + if ($codocTokenCode = $this->get_paywall_token_code()) { | |
| 483 | + if ($mode == 'cookie') { | |
| 484 | + // cookieの中にtokenがあればOK | |
| 485 | + return true; | |
| 486 | + } | |
| 487 | + if ($mode == 'strict' or $mode == 'strict_with_data') { | |
| 488 | + // ログイン状態を問い合わせ | |
| 489 | + $res = $this->callPaywallAPI('GET','/users',["paywall_token_code" => $codocTokenCode ]); | |
| 490 | + if ($res->status) { | |
| 491 | + if ($mode == 'strict_with_data') { | |
| 492 | + return $res; | |
| 493 | + } else { | |
| 494 | + return true; | |
| 495 | + } | |
| 496 | + } else { | |
| 497 | + return false; | |
| 498 | + } | |
| 499 | + } | |
| 500 | + } | |
| 501 | + return false; | |
| 502 | + } | |
| 503 | + /* | |
| 504 | + | |
| 505 | + $_CODOC->util->check_owned($entry_code); | |
| 506 | + | |
| 507 | + */ | |
| 508 | + function check_owned($entry_code = null) { | |
| 509 | + if (!$entry_code) { | |
| 510 | + return false; | |
| 511 | + } | |
| 512 | + $res = $this->callPaywallAPI('GET','/entries/' . $entry_code, ["paywall_token_code" => $this->get_paywall_token_code(), "check_owned" => 1 ]); | |
| 513 | + if ($res->status) { | |
| 514 | + return true; | |
| 515 | + } | |
| 516 | + return false; | |
| 517 | + } | |
| 518 | + | |
| 519 | + /* | |
| 520 | + $_CODOC->util->health_check(); | |
| 521 | + */ | |
| 522 | + function health_check() { | |
| 523 | + $sslverify = true; | |
| 524 | + $host = $this->codoc_url; | |
| 525 | + if (preg_match('/local/',$this->codoc_url)) { | |
| 526 | + $sslverify = false; | |
| 527 | + $host = 'https://host.docker.internal'; | |
| 528 | + } | |
| 529 | + $url = sprintf("%s/codoc_health_check",$host); | |
| 530 | + $http = new WP_Http(); | |
| 531 | + try { | |
| 532 | + $response = $http->request( | |
| 533 | + $url, | |
| 534 | + [ | |
| 535 | + 'sslverify' => $sslverify, | |
| 536 | + 'method' => 'GET', | |
| 537 | + 'timeout' => 10, | |
| 538 | + 'headers' => [], | |
| 539 | + 'body' => [], | |
| 540 | + ] | |
| 541 | + ); | |
| 542 | + } catch(Exception $e) { | |
| 543 | + | |
| 544 | + } | |
| 545 | + if ($response and $response['response']['code'] == '200') { | |
| 546 | + return true; | |
| 547 | + } else { | |
| 548 | + return null; | |
| 549 | + } | |
| 550 | + } | |
| 216 | 551 | } |
| 217 | 552 | |