| @@ -3,28 +3,34 @@ | ||
| 3 | 3 | if (!defined('WPINC')) { |
| 4 | 4 | define( 'WPINC', 'wp-includes' ); |
| 5 | 5 | } |
| 6 | 6 | |
| 7 | +if (!defined('CODOC_URL')) { | |
| 8 | + define( 'CODOC_URL', 'https://codoc.jp' ); | |
| 9 | +} | |
| 10 | + | |
| 7 | 11 | require_once( ABSPATH . WPINC . '/class-http.php'); |
| 8 | 12 | require_once( ABSPATH . WPINC . '/class-wp-error.php'); |
| 9 | 13 | |
| 10 | 14 | 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'); | |
| 15 | + public $codoc_url = CODOC_URL; | |
| 16 | + public function __construct($params = ['usercode' => null, 'token' => null,'codoc_url' => null]) { | |
| 17 | + $this->setAuthInfo(['usercode' => $params['usercode'], 'token' => $params['token']]); | |
| 18 | + if (isset($params['codoc_url'])) { | |
| 19 | + $this->codoc_url = $params['codoc_url']; | |
| 20 | + } | |
| 15 | 21 | return $this; |
| 16 | 22 | } |
| 17 | - | |
| 23 | + | |
| 18 | 24 | public function callAPI($method,$path,$body = [],$headers = []) { |
| 19 | 25 | $usercode = $this->usercode; |
| 20 | 26 | $token = $this->token; |
| 21 | 27 | |
| 22 | 28 | $headers['X-CodocToken'] = $token; |
| 23 | - $host = CODOC_URL; | |
| 29 | + $host = $this->codoc_url; | |
| 24 | 30 | $sslverify = true; |
| 25 | 31 | |
| 26 | - if (preg_match('/local/',CODOC_URL)) { | |
| 32 | + if (preg_match('/local/',$this->codoc_url)) { | |
| 27 | 33 | $sslverify = false; |
| 28 | 34 | $host = 'https://host.docker.internal'; |
| 29 | 35 | } |
| 30 | 36 | $url = sprintf("%s/api/v1/cms/%s%s",$host,$usercode,$path); |
| @@ -56,9 +62,29 @@ | ||
| 56 | 62 | } else { |
| 57 | 63 | return null; |
| 58 | 64 | } |
| 59 | 65 | } |
| 60 | - | |
| 66 | + public function setAuthInfo($params = [ "usercode" => null, "token" => null ]) { | |
| 67 | + if ($params["usercode"]) { | |
| 68 | + $this->usercode = $params["usercode"]; | |
| 69 | + } | |
| 70 | + if ($params["token"]) { | |
| 71 | + $this->token = $params["token"]; | |
| 72 | + } | |
| 73 | + return true; | |
| 74 | + } | |
| 75 | + public function get_token($params = ["fetch_token_key" => null],$auth_info = ["usercode" => null, "token" => null]) { | |
| 76 | + $this->setAuthInfo($auth_info); | |
| 77 | + return $this->callAPI('GET','/token',[ "fetch_token_key" => $params["fetch_token_key"] ]); | |
| 78 | + } | |
| 79 | + public function get_user_info($params = [] ,$auth_info = ["usercode" => null, "token" => null]) { | |
| 80 | + $this->setAuthInfo($auth_info); | |
| 81 | + return $this->callAPI('GET',''); | |
| 82 | + } | |
| 83 | + public function get_support_entry($params = [] ,$auth_info = ["usercode" => null, "token" => null]) { | |
| 84 | + $this->setAuthInfo($auth_info); | |
| 85 | + return $this->callAPI('GET','/support_entry'); | |
| 86 | + } | |
| 61 | 87 | public function sync_entry($params = [ |
| 62 | 88 | "post_title" => null, |
| 63 | 89 | "post_content" => null, |
| 64 | 90 | "post_status" => null, # 0, 1 |
| @@ -63,9 +89,12 @@ | ||
| 63 | 89 | "post_content" => null, |
| 64 | 90 | "post_status" => null, # 0, 1 |
| 65 | 91 | "post_permalink" => null, |
| 66 | 92 | "codoc_entry_code" => null, |
| 67 | - ]) { | |
| 93 | + ],$auth_info = ["usercode" => null, "token" => null]) { | |
| 94 | + | |
| 95 | + $this->setAuthInfo($auth_info); | |
| 96 | + | |
| 68 | 97 | $post_content = $params['post_content']; |
| 69 | 98 | preg_match('/wp:codoc\/codoc-block +?({.*})/',$post_content,$matches); |
| 70 | 99 | // GUTENBERG で指定されたjson(記事情報)がない場合 |
| 71 | 100 | if (!$matches) { |
| @@ -80,10 +109,17 @@ | ||
| 80 | 109 | return; |
| 81 | 110 | } |
| 82 | 111 | // TODO: split or html parse? HTMLがつながってる場合はおかしくなる |
| 83 | 112 | // classic(tinymce)版の場合p、もしくはdivにかこまれている |
| 113 | + $end_tag_regex = '/<(?:div|p)>::CODOC_WP_END_PAYWALL::<\/(?:div|p)>/'; | |
| 114 | + $before_splited = preg_split($end_tag_regex,$post_content); | |
| 115 | + $post_content = $before_splited[0]; | |
| 84 | 116 | $splited = preg_split('/(?:<(?:div|p)(?:[^>]+|)>|)<\!-- +wp:codoc\/codoc-block .*<\!-- +\/wp:codoc\/codoc-block +-->(?:<\/(?:div|p)>|)/s',$post_content); |
| 85 | - $status = $params['post_status']; | |
| 117 | + $status = $params['post_status']; # 0: 非公開 1: 公開 2:限定公開(パスワードの場合は限定公開で同期する) | |
| 118 | + // codoc設定で限定公開を有効で、公開の場合は 限定公開にする | |
| 119 | + if ($status == 1 and isset($codoc_info['statusLimited']) and $codoc_info['statusLimited']) { | |
| 120 | + $status = 2; | |
| 121 | + } | |
| 86 | 122 | # $body_free = ''; |
| 87 | 123 | # $body_paywalled = ''; |
| 88 | 124 | # if (count($splited) >= 2) { |
| 89 | 125 | # |
| @@ -109,8 +145,9 @@ | ||
| 109 | 145 | 'limited_count' => isset($codoc_info['limitedCount']) ? $codoc_info['limitedCount'] : 1, |
| 110 | 146 | 'affiliate_mode' => isset($codoc_info['affiliateMode']) ? ($codoc_info['affiliateMode'] ? 1 : 0) : 0, |
| 111 | 147 | 'affiliate_rate' => isset($codoc_info['affiliateRate']) ? $codoc_info['affiliateRate'] : '0.0500', |
| 112 | 148 | 'show_support' => isset($codoc_info['showSupport']) ? ($codoc_info['showSupport'] ? 1 : 0) : 0, |
| 149 | + 'show_paywalled_support' => isset($codoc_info['showPaywalledSupport']) ? ($codoc_info['showPaywalledSupport'] ? 1 : 0) : 0, | |
| 113 | 150 | 'subscriptions' => isset($codoc_info['subscriptions']) ? array_keys($codoc_info['subscriptions']) : [], |
| 114 | 151 | ]; |
| 115 | 152 | $entryCode = $params['codoc_entry_code']; |
| 116 | 153 | $res = null; |
| @@ -125,9 +162,12 @@ | ||
| 125 | 162 | function post_thumbnail( $params = [ |
| 126 | 163 | "file_path" => null, |
| 127 | 164 | "boundary" => null, |
| 128 | 165 | "codoc_entry_code" => null, |
| 129 | - ] ) { | |
| 166 | + ], $auth_info = ["usercode" => null, "token" => null] ) { | |
| 167 | + | |
| 168 | + $this->setAuthInfo($auth_info); | |
| 169 | + | |
| 130 | 170 | $file_path = $params['file_path']; |
| 131 | 171 | $boundary = $params['boundary']; # ランダム変数24桁 wp_generate_password(24); |
| 132 | 172 | $res = null; |
| 133 | 173 | if (is_readable($file_path)) { |
| @@ -157,9 +197,12 @@ | ||
| 157 | 197 | } |
| 158 | 198 | |
| 159 | 199 | function reset_thumbnail( $params = [ |
| 160 | 200 | "codoc_entry_code" => null, |
| 161 | - ]) { | |
| 201 | + ],$auth_info = ["usercode" => null, "token" => null] ) { | |
| 202 | + | |
| 203 | + $this->setAuthInfo($auth_info); | |
| 204 | + | |
| 162 | 205 | $entryCode = $params['codoc_entry_code']; |
| 163 | 206 | $res = $this->callAPI( |
| 164 | 207 | 'POST', |
| 165 | 208 | '/entries/' . $entryCode . '/thumbnail', |
| @@ -171,35 +214,112 @@ | ||
| 171 | 214 | function filter_content( $params = [ |
| 172 | 215 | "post_content" => null, |
| 173 | 216 | "preview" => null, |
| 174 | 217 | "codoc_entry_code" => null, |
| 218 | + "codoc_settings" => null, | |
| 219 | + "is_amp_endpoint" => null, | |
| 220 | + "post_permalink" => null, | |
| 221 | + "codoc_support_entry_code" => null, | |
| 175 | 222 | ]) { |
| 223 | + $CODOC_SETTINGS = $params['codoc_settings']; | |
| 224 | + | |
| 225 | + $has_codoc_tag = '/(<(span|div)[^>]+data-id="codoc-tag"(?:[^>]+|)>(?:.+|)<\/(?:div|span)>)/'; | |
| 176 | 226 | $post_content = $params['post_content']; |
| 177 | 227 | // codoc タグがあるかどうか (202001:span -> div に変更) |
| 178 | - preg_match('/(<(span|div)[^>]+data-id="codoc-tag"(?:[^>]+|)>(?:.+|)<\/(?:div|span)>)/',$post_content,$matches); | |
| 228 | + preg_match($has_codoc_tag,$post_content,$matches); | |
| 229 | + | |
| 230 | + $show_support_message = ''; | |
| 231 | + if (isset($CODOC_SETTINGS['show_support_message'])) { | |
| 232 | + $show_support_message = $CODOC_SETTINGS['show_support_message']; | |
| 233 | + } | |
| 234 | + $show_support_categories = ''; | |
| 235 | + if (isset($CODOC_SETTINGS['show_support_categories'])) { | |
| 236 | + $show_support_categories = $CODOC_SETTINGS['show_support_categories']; | |
| 237 | + } | |
| 238 | + $show_support_location = 'bottom'; | |
| 239 | + if (isset($CODOC_SETTINGS['show_support_location'])) { | |
| 240 | + $show_support_location = $CODOC_SETTINGS['show_support_location']; | |
| 241 | + } | |
| 242 | + | |
| 243 | + $show_support_entry = (is_single() && preg_grep("/(?:$show_support_categories)/",array_map( function($c) { return $c->name; },get_the_category(get_post()->ID)))); | |
| 244 | + if (!$matches && ($support_entry_code = $params['codoc_support_entry_code']) && $show_support_entry) { | |
| 245 | + $support_tag = sprintf( | |
| 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>', | |
| 247 | + htmlspecialchars($show_support_message,ENT_QUOTES), | |
| 248 | + $support_entry_code, | |
| 249 | + $CODOC_SETTINGS['support_button_text'], | |
| 250 | + $CODOC_SETTINGS['show_like'], | |
| 251 | + $CODOC_SETTINGS['show_about_codoc'], | |
| 252 | + $CODOC_SETTINGS['show_powered_by'] | |
| 253 | + ); | |
| 254 | + $post_content = $show_support_location == 'bottom' ? | |
| 255 | + $post_content . $support_tag : | |
| 256 | + $support_tag . $post_content ; | |
| 257 | + } | |
| 258 | + | |
| 179 | 259 | // data-wp-plugin-ver が無いタグは分割しない |
| 180 | 260 | if (!$matches) { |
| 181 | 261 | return $post_content; |
| 182 | 262 | } |
| 263 | + | |
| 183 | 264 | // codocタグで分割 (202001: gutenbergで直前のdivタグを削除) |
| 184 | 265 | $tag_regex = '/(?:<div(?:[^>]+|)>|)<(?:span|div)[^>]+data-id="codoc-tag"(?:[^>]+|)>(?:.+|)<\/(?:span|div)>(?:<\/div>|)/'; |
| 266 | + $end_tag_regex = '/<(?:div|p)>::CODOC_WP_END_PAYWALL::<\/(?:div|p)>/'; | |
| 185 | 267 | if ( $params['preview'] ) { |
| 186 | 268 | $post_content = preg_replace($tag_regex,'<div class="codoc-continue">ここから上は無料で表示されます</div>',$post_content); |
| 269 | + $post_content = preg_replace($end_tag_regex,'<div class="codoc-continue">ここから下は無料で表示されます</div>',$post_content); | |
| 187 | 270 | return $post_content; |
| 188 | 271 | } |
| 189 | 272 | // codoc タグの前後で分ける |
| 190 | - $splited = preg_split($tag_regex,$post_content); | |
| 273 | + $before_splited = preg_split($end_tag_regex,$post_content); | |
| 274 | + $splited = preg_split($tag_regex,$before_splited[0]); | |
| 191 | 275 | // 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]); | |
| 276 | + $entryCodeFormated = sprintf('"codoc-entry-%s" ',$params['codoc_entry_code']); | |
| 277 | + | |
| 278 | + // 文言系の設定をつける | |
| 279 | + $tagAttributes = ''; | |
| 280 | + if (isset($CODOC_SETTINGS['show_like']) and $CODOC_SETTINGS['show_like'] != 1) { | |
| 281 | + $tagAttributes = $tagAttributes . sprintf(' data-show-like="%s"',$CODOC_SETTINGS['show_like']); | |
| 282 | + } | |
| 283 | + if (isset($CODOC_SETTINGS['show_about_codoc']) and $CODOC_SETTINGS['show_about_codoc'] != 1) { | |
| 284 | + $tagAttributes = $tagAttributes . sprintf(' data-show-about-codoc="%s"',$CODOC_SETTINGS['show_about_codoc']); | |
| 285 | + } | |
| 286 | + if (isset($CODOC_SETTINGS['show_powered_by']) and $CODOC_SETTINGS['show_powered_by'] != 1) { | |
| 287 | + $tagAttributes = $tagAttributes . sprintf(' data-show-powered-by="%s"',$CODOC_SETTINGS['show_powered_by']); | |
| 288 | + } | |
| 289 | + if ($CODOC_SETTINGS['entry_button_text']) { | |
| 290 | + $tagAttributes = $tagAttributes . sprintf(' data-entry-button-text="%s"',$CODOC_SETTINGS['entry_button_text']); | |
| 291 | + } | |
| 292 | + if ($CODOC_SETTINGS['subscription_button_text']) { | |
| 293 | + $tagAttributes = $tagAttributes . sprintf(' data-subscription-button-text="%s"',$CODOC_SETTINGS['subscription_button_text']); | |
| 294 | + } | |
| 295 | + if ($CODOC_SETTINGS['support_button_text']) { | |
| 296 | + $tagAttributes = $tagAttributes . sprintf(' data-support-button-text="%s"',$CODOC_SETTINGS['support_button_text']); | |
| 297 | + } | |
| 298 | + if ($CODOC_SETTINGS['subscription_message']) { | |
| 299 | + $tagAttributes = $tagAttributes . sprintf(' data-subscription-message="%s"',$CODOC_SETTINGS['subscription_message']); | |
| 300 | + } | |
| 301 | + if ($CODOC_SETTINGS['support_message']) { | |
| 302 | + $tagAttributes = $tagAttributes . sprintf(' data-support-message="%s"',$CODOC_SETTINGS['support_message']); | |
| 303 | + } | |
| 304 | + if ($CODOC_SETTINGS['codoc_tag_attributes']) { | |
| 305 | + $tagAttributes = $tagAttributes . sprintf(' %s',$CODOC_SETTINGS['codoc_tag_attributes']); | |
| 306 | + } | |
| 307 | + | |
| 308 | + $codoc_tag = preg_replace('/<((?:span|div)[^>]+)data-id="[^"]+"((?:[^>]+|))>/','<${1} ${2} data-without-body="1" id=' . $entryCodeFormated . $tagAttributes . ">", $matches[1]); | |
| 194 | 309 | # THE THOR の page-lp.php が " " をけずるための対策 |
| 195 | 310 | # "div class" -> "div class" |
| 196 | 311 | $codoc_tag = preg_replace('/div +class/','div class',$codoc_tag); |
| 197 | - | |
| 312 | + | |
| 313 | + if ($params['is_amp_endpoint']) { | |
| 314 | + $format = preg_replace_callback('/(<(?:span|div)[^>]+>)((?:[^<>]+|))(<\/(?:span|div)>)/',function($matches) { | |
| 315 | + $format = $matches[1] . '<a href="%%s">%s</a>' . $matches[3]; | |
| 316 | + return sprintf($format,($matches[2] ? $matches[2] : '続きを読む')); | |
| 317 | + },$codoc_tag); | |
| 318 | + $codoc_tag = sprintf($format,$params["post_permalink"]); | |
| 319 | + } | |
| 320 | + | |
| 198 | 321 | # タグの前後にHTMLを挿入 |
| 199 | - $CODOC_SETTINGS = $params['codoc_settings']; | |
| 200 | - $before = ''; | |
| 201 | - $after = ''; | |
| 202 | 322 | if (!isset($CODOC_SETTINGS['str_before_codoc_tag'])) { |
| 203 | 323 | $CODOC_SETTINGS['str_before_codoc_tag'] = ''; |
| 204 | 324 | } |
| 205 | 325 | if (!isset($CODOC_SETTINGS['str_after_codoc_tag'])) { |
| @@ -204,13 +324,20 @@ | ||
| 204 | 324 | } |
| 205 | 325 | if (!isset($CODOC_SETTINGS['str_after_codoc_tag'])) { |
| 206 | 326 | $CODOC_SETTINGS['str_after_codoc_tag'] = ''; |
| 207 | 327 | } |
| 208 | - $before = $CODOC_SETTINGS['str_before_codoc_tag'] or ""; | |
| 209 | - $after = $CODOC_SETTINGS['str_after_codoc_tag'] or ""; | |
| 210 | - | |
| 211 | 328 | // 無料部分のみ表示 |
| 212 | - return $splited[0] . sprintf('%s<div class="wp-block-codoc-codoc-block">%s</div>%s',$before,$codoc_tag,$after); | |
| 329 | + $post_content = $splited[0] . sprintf( | |
| 330 | + '%s<div class="wp-block-codoc-codoc-block">%s</div>%s', | |
| 331 | + $CODOC_SETTINGS['str_before_codoc_tag'], | |
| 332 | + $codoc_tag, | |
| 333 | + $CODOC_SETTINGS['str_after_codoc_tag'] | |
| 334 | + ); | |
| 335 | + // ::CODOC_WP_END_PAYWALL:: の後を足す | |
| 336 | + if (isset($before_splited[1]) and $before_splited[1]) { | |
| 337 | + $post_content .= $before_splited[1]; | |
| 338 | + } | |
| 339 | + return $post_content; | |
| 213 | 340 | } |
| 214 | 341 | |
| 215 | 342 | |
| 216 | 343 | } |