| @@ -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) { |
| @@ -109,8 +138,9 @@ | ||
| 109 | 138 | 'limited_count' => isset($codoc_info['limitedCount']) ? $codoc_info['limitedCount'] : 1, |
| 110 | 139 | 'affiliate_mode' => isset($codoc_info['affiliateMode']) ? ($codoc_info['affiliateMode'] ? 1 : 0) : 0, |
| 111 | 140 | 'affiliate_rate' => isset($codoc_info['affiliateRate']) ? $codoc_info['affiliateRate'] : '0.0500', |
| 112 | 141 | 'show_support' => isset($codoc_info['showSupport']) ? ($codoc_info['showSupport'] ? 1 : 0) : 0, |
| 142 | + 'show_paywalled_support' => isset($codoc_info['showPaywalledSupport']) ? ($codoc_info['showPaywalledSupport'] ? 1 : 0) : 0, | |
| 113 | 143 | 'subscriptions' => isset($codoc_info['subscriptions']) ? array_keys($codoc_info['subscriptions']) : [], |
| 114 | 144 | ]; |
| 115 | 145 | $entryCode = $params['codoc_entry_code']; |
| 116 | 146 | $res = null; |
| @@ -125,9 +155,12 @@ | ||
| 125 | 155 | function post_thumbnail( $params = [ |
| 126 | 156 | "file_path" => null, |
| 127 | 157 | "boundary" => null, |
| 128 | 158 | "codoc_entry_code" => null, |
| 129 | - ] ) { | |
| 159 | + ], $auth_info = ["usercode" => null, "token" => null] ) { | |
| 160 | + | |
| 161 | + $this->setAuthInfo($auth_info); | |
| 162 | + | |
| 130 | 163 | $file_path = $params['file_path']; |
| 131 | 164 | $boundary = $params['boundary']; # ランダム変数24桁 wp_generate_password(24); |
| 132 | 165 | $res = null; |
| 133 | 166 | if (is_readable($file_path)) { |
| @@ -157,9 +190,12 @@ | ||
| 157 | 190 | } |
| 158 | 191 | |
| 159 | 192 | function reset_thumbnail( $params = [ |
| 160 | 193 | "codoc_entry_code" => null, |
| 161 | - ]) { | |
| 194 | + ],$auth_info = ["usercode" => null, "token" => null] ) { | |
| 195 | + | |
| 196 | + $this->setAuthInfo($auth_info); | |
| 197 | + | |
| 162 | 198 | $entryCode = $params['codoc_entry_code']; |
| 163 | 199 | $res = $this->callAPI( |
| 164 | 200 | 'POST', |
| 165 | 201 | '/entries/' . $entryCode . '/thumbnail', |
| @@ -171,16 +207,54 @@ | ||
| 171 | 207 | function filter_content( $params = [ |
| 172 | 208 | "post_content" => null, |
| 173 | 209 | "preview" => null, |
| 174 | 210 | "codoc_entry_code" => null, |
| 211 | + "codoc_settings" => null, | |
| 212 | + "is_amp_endpoint" => null, | |
| 213 | + "post_permalink" => null, | |
| 214 | + "codoc_support_entry_code" => null, | |
| 175 | 215 | ]) { |
| 216 | + $CODOC_SETTINGS = $params['codoc_settings']; | |
| 217 | + | |
| 218 | + $has_codoc_tag = '/(<(span|div)[^>]+data-id="codoc-tag"(?:[^>]+|)>(?:.+|)<\/(?:div|span)>)/'; | |
| 176 | 219 | $post_content = $params['post_content']; |
| 177 | 220 | // codoc タグがあるかどうか (202001:span -> div に変更) |
| 178 | - preg_match('/(<(span|div)[^>]+data-id="codoc-tag"(?:[^>]+|)>(?:.+|)<\/(?:div|span)>)/',$post_content,$matches); | |
| 221 | + preg_match($has_codoc_tag,$post_content,$matches); | |
| 222 | + | |
| 223 | + $show_support_message = ''; | |
| 224 | + if (isset($CODOC_SETTINGS['show_support_message'])) { | |
| 225 | + $show_support_message = $CODOC_SETTINGS['show_support_message']; | |
| 226 | + } | |
| 227 | + $show_support_categories = ''; | |
| 228 | + if (isset($CODOC_SETTINGS['show_support_categories'])) { | |
| 229 | + $show_support_categories = $CODOC_SETTINGS['show_support_categories']; | |
| 230 | + } | |
| 231 | + $show_support_location = 'bottom'; | |
| 232 | + if (isset($CODOC_SETTINGS['show_support_location'])) { | |
| 233 | + $show_support_location = $CODOC_SETTINGS['show_support_location']; | |
| 234 | + } | |
| 235 | + | |
| 236 | + $show_support_entry = (is_single() && preg_grep("/(?:$show_support_categories)/",array_map( function($c) { return $c->name; },get_the_category(get_post()->ID)))); | |
| 237 | + if (!$matches && ($support_entry_code = $params['codoc_support_entry_code']) && $show_support_entry) { | |
| 238 | + $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>', | |
| 240 | + htmlspecialchars($show_support_message,ENT_QUOTES), | |
| 241 | + $support_entry_code, | |
| 242 | + $CODOC_SETTINGS['support_button_text'], | |
| 243 | + $CODOC_SETTINGS['show_like'], | |
| 244 | + $CODOC_SETTINGS['show_about_codoc'], | |
| 245 | + $CODOC_SETTINGS['show_powered_by'] | |
| 246 | + ); | |
| 247 | + $post_content = $show_support_location == 'bottom' ? | |
| 248 | + $post_content . $support_tag : | |
| 249 | + $support_tag . $post_content ; | |
| 250 | + } | |
| 251 | + | |
| 179 | 252 | // data-wp-plugin-ver が無いタグは分割しない |
| 180 | 253 | if (!$matches) { |
| 181 | 254 | return $post_content; |
| 182 | 255 | } |
| 256 | + | |
| 183 | 257 | // codocタグで分割 (202001: gutenbergで直前のdivタグを削除) |
| 184 | 258 | $tag_regex = '/(?:<div(?:[^>]+|)>|)<(?:span|div)[^>]+data-id="codoc-tag"(?:[^>]+|)>(?:.+|)<\/(?:span|div)>(?:<\/div>|)/'; |
| 185 | 259 | if ( $params['preview'] ) { |
| 186 | 260 | $post_content = preg_replace($tag_regex,'<div class="codoc-continue">ここから上は無料で表示されます</div>',$post_content); |
| @@ -188,18 +262,54 @@ | ||
| 188 | 262 | } |
| 189 | 263 | // codoc タグの前後で分ける |
| 190 | 264 | $splited = preg_split($tag_regex,$post_content); |
| 191 | 265 | // 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]); | |
| 266 | + $entryCodeFormated = sprintf('"codoc-entry-%s" ',$params['codoc_entry_code']); | |
| 267 | + | |
| 268 | + // 文言系の設定をつける | |
| 269 | + $tagAttributes = ''; | |
| 270 | + if (isset($CODOC_SETTINGS['show_like']) and $CODOC_SETTINGS['show_like'] != 1) { | |
| 271 | + $tagAttributes = $tagAttributes . sprintf(' data-show-like="%s"',$CODOC_SETTINGS['show_like']); | |
| 272 | + } | |
| 273 | + if (isset($CODOC_SETTINGS['show_about_codoc']) and $CODOC_SETTINGS['show_about_codoc'] != 1) { | |
| 274 | + $tagAttributes = $tagAttributes . sprintf(' data-show-about-codoc="%s"',$CODOC_SETTINGS['show_about_codoc']); | |
| 275 | + } | |
| 276 | + if (isset($CODOC_SETTINGS['show_powered_by']) and $CODOC_SETTINGS['show_powered_by'] != 1) { | |
| 277 | + $tagAttributes = $tagAttributes . sprintf(' data-show-powered-by="%s"',$CODOC_SETTINGS['show_powered_by']); | |
| 278 | + } | |
| 279 | + if ($CODOC_SETTINGS['entry_button_text']) { | |
| 280 | + $tagAttributes = $tagAttributes . sprintf(' data-entry-button-text="%s"',$CODOC_SETTINGS['entry_button_text']); | |
| 281 | + } | |
| 282 | + if ($CODOC_SETTINGS['subscription_button_text']) { | |
| 283 | + $tagAttributes = $tagAttributes . sprintf(' data-subscription-button-text="%s"',$CODOC_SETTINGS['subscription_button_text']); | |
| 284 | + } | |
| 285 | + if ($CODOC_SETTINGS['support_button_text']) { | |
| 286 | + $tagAttributes = $tagAttributes . sprintf(' data-support-button-text="%s"',$CODOC_SETTINGS['support_button_text']); | |
| 287 | + } | |
| 288 | + if ($CODOC_SETTINGS['subscription_message']) { | |
| 289 | + $tagAttributes = $tagAttributes . sprintf(' data-subscription-message="%s"',$CODOC_SETTINGS['subscription_message']); | |
| 290 | + } | |
| 291 | + if ($CODOC_SETTINGS['support_message']) { | |
| 292 | + $tagAttributes = $tagAttributes . sprintf(' data-support-message="%s"',$CODOC_SETTINGS['support_message']); | |
| 293 | + } | |
| 294 | + if ($CODOC_SETTINGS['codoc_tag_attributes']) { | |
| 295 | + $tagAttributes = $tagAttributes . sprintf(' %s',$CODOC_SETTINGS['codoc_tag_attributes']); | |
| 296 | + } | |
| 297 | + | |
| 298 | + $codoc_tag = preg_replace('/<((?:span|div)[^>]+)data-id="[^"]+"((?:[^>]+|))>/','<${1} ${2} data-without-body="1" id=' . $entryCodeFormated . $tagAttributes . ">", $matches[1]); | |
| 194 | 299 | # THE THOR の page-lp.php が " " をけずるための対策 |
| 195 | 300 | # "div class" -> "div class" |
| 196 | 301 | $codoc_tag = preg_replace('/div +class/','div class',$codoc_tag); |
| 197 | - | |
| 302 | + | |
| 303 | + if ($params['is_amp_endpoint']) { | |
| 304 | + $format = preg_replace_callback('/(<(?:span|div)[^>]+>)((?:[^<>]+|))(<\/(?:span|div)>)/',function($matches) { | |
| 305 | + $format = $matches[1] . '<a href="%%s">%s</a>' . $matches[3]; | |
| 306 | + return sprintf($format,($matches[2] ? $matches[2] : '続きを読む')); | |
| 307 | + },$codoc_tag); | |
| 308 | + $codoc_tag = sprintf($format,$params["post_permalink"]); | |
| 309 | + } | |
| 310 | + | |
| 198 | 311 | # タグの前後にHTMLを挿入 |
| 199 | - $CODOC_SETTINGS = $params['codoc_settings']; | |
| 200 | - $before = ''; | |
| 201 | - $after = ''; | |
| 202 | 312 | if (!isset($CODOC_SETTINGS['str_before_codoc_tag'])) { |
| 203 | 313 | $CODOC_SETTINGS['str_before_codoc_tag'] = ''; |
| 204 | 314 | } |
| 205 | 315 | if (!isset($CODOC_SETTINGS['str_after_codoc_tag'])) { |
| @@ -204,13 +314,16 @@ | ||
| 204 | 314 | } |
| 205 | 315 | if (!isset($CODOC_SETTINGS['str_after_codoc_tag'])) { |
| 206 | 316 | $CODOC_SETTINGS['str_after_codoc_tag'] = ''; |
| 207 | 317 | } |
| 208 | - $before = $CODOC_SETTINGS['str_before_codoc_tag'] or ""; | |
| 209 | - $after = $CODOC_SETTINGS['str_after_codoc_tag'] or ""; | |
| 210 | - | |
| 211 | 318 | // 無料部分のみ表示 |
| 212 | - return $splited[0] . sprintf('%s<div class="wp-block-codoc-codoc-block">%s</div>%s',$before,$codoc_tag,$after); | |
| 319 | + $post_content = $splited[0] . sprintf( | |
| 320 | + '%s<div class="wp-block-codoc-codoc-block">%s</div>%s', | |
| 321 | + $CODOC_SETTINGS['str_before_codoc_tag'], | |
| 322 | + $codoc_tag, | |
| 323 | + $CODOC_SETTINGS['str_after_codoc_tag'] | |
| 324 | + ); | |
| 325 | + return $post_content; | |
| 213 | 326 | } |
| 214 | 327 | |
| 215 | 328 | |
| 216 | 329 | } |