| @@ -3,8 +3,12 @@ | ||
| 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 { |
| @@ -9,13 +13,12 @@ | ||
| 9 | 13 | |
| 10 | 14 | final class CodocUtil { |
| 11 | 15 | |
| 12 | 16 | 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'); | |
| 17 | + $this->setAuthInfo($params); | |
| 15 | 18 | return $this; |
| 16 | 19 | } |
| 17 | - | |
| 20 | + | |
| 18 | 21 | public function callAPI($method,$path,$body = [],$headers = []) { |
| 19 | 22 | $usercode = $this->usercode; |
| 20 | 23 | $token = $this->token; |
| 21 | 24 | |
| @@ -56,9 +59,25 @@ | ||
| 56 | 59 | } else { |
| 57 | 60 | return null; |
| 58 | 61 | } |
| 59 | 62 | } |
| 60 | - | |
| 63 | + public function setAuthInfo($params = [ "usercode" => null, "token" => null ]) { | |
| 64 | + if ($params["usercode"]) { | |
| 65 | + $this->usercode = $params["usercode"]; | |
| 66 | + } | |
| 67 | + if ($params["token"]) { | |
| 68 | + $this->token = $params["token"]; | |
| 69 | + } | |
| 70 | + return true; | |
| 71 | + } | |
| 72 | + public function get_token($params = ["fetch_token_key" => null],$auth_info = ["usercode" => null, "token" => null]) { | |
| 73 | + $this->setAuthInfo($auth_info); | |
| 74 | + return $this->callAPI('GET','/token',[ "fetch_token_key" => $params["fetch_token_key"] ]); | |
| 75 | + } | |
| 76 | + public function get_user_info($params = [] ,$auth_info = ["usercode" => null, "token" => null]) { | |
| 77 | + $this->setAuthInfo($auth_info); | |
| 78 | + return $this->callAPI('GET',''); | |
| 79 | + } | |
| 61 | 80 | public function sync_entry($params = [ |
| 62 | 81 | "post_title" => null, |
| 63 | 82 | "post_content" => null, |
| 64 | 83 | "post_status" => null, # 0, 1 |
| @@ -63,9 +82,12 @@ | ||
| 63 | 82 | "post_content" => null, |
| 64 | 83 | "post_status" => null, # 0, 1 |
| 65 | 84 | "post_permalink" => null, |
| 66 | 85 | "codoc_entry_code" => null, |
| 67 | - ]) { | |
| 86 | + ],$auth_info = ["usercode" => null, "token" => null]) { | |
| 87 | + | |
| 88 | + $this->setAuthInfo($auth_info); | |
| 89 | + | |
| 68 | 90 | $post_content = $params['post_content']; |
| 69 | 91 | preg_match('/wp:codoc\/codoc-block +?({.*})/',$post_content,$matches); |
| 70 | 92 | // GUTENBERG で指定されたjson(記事情報)がない場合 |
| 71 | 93 | if (!$matches) { |
| @@ -125,9 +147,12 @@ | ||
| 125 | 147 | function post_thumbnail( $params = [ |
| 126 | 148 | "file_path" => null, |
| 127 | 149 | "boundary" => null, |
| 128 | 150 | "codoc_entry_code" => null, |
| 129 | - ] ) { | |
| 151 | + ], $auth_info = ["usercode" => null, "token" => null] ) { | |
| 152 | + | |
| 153 | + $this->setAuthInfo($auth_info); | |
| 154 | + | |
| 130 | 155 | $file_path = $params['file_path']; |
| 131 | 156 | $boundary = $params['boundary']; # ランダム変数24桁 wp_generate_password(24); |
| 132 | 157 | $res = null; |
| 133 | 158 | if (is_readable($file_path)) { |
| @@ -157,9 +182,12 @@ | ||
| 157 | 182 | } |
| 158 | 183 | |
| 159 | 184 | function reset_thumbnail( $params = [ |
| 160 | 185 | "codoc_entry_code" => null, |
| 161 | - ]) { | |
| 186 | + ],$auth_info = ["usercode" => null, "token" => null] ) { | |
| 187 | + | |
| 188 | + $this->setAuthInfo($auth_info); | |
| 189 | + | |
| 162 | 190 | $entryCode = $params['codoc_entry_code']; |
| 163 | 191 | $res = $this->callAPI( |
| 164 | 192 | 'POST', |
| 165 | 193 | '/entries/' . $entryCode . '/thumbnail', |
| @@ -171,8 +199,9 @@ | ||
| 171 | 199 | function filter_content( $params = [ |
| 172 | 200 | "post_content" => null, |
| 173 | 201 | "preview" => null, |
| 174 | 202 | "codoc_entry_code" => null, |
| 203 | + "codoc_settings" => null, | |
| 175 | 204 | ]) { |
| 176 | 205 | $post_content = $params['post_content']; |
| 177 | 206 | // codoc タグがあるかどうか (202001:span -> div に変更) |
| 178 | 207 | preg_match('/(<(span|div)[^>]+data-id="codoc-tag"(?:[^>]+|)>(?:.+|)<\/(?:div|span)>)/',$post_content,$matches); |
| @@ -188,10 +217,10 @@ | ||
| 188 | 217 | } |
| 189 | 218 | // codoc タグの前後で分ける |
| 190 | 219 | $splited = preg_split($tag_regex,$post_content); |
| 191 | 220 | // 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]); | |
| 221 | + $entryCodeFormated = sprintf('"codoc-entry-%s" ',$params['codoc_entry_code']); | |
| 222 | + $codoc_tag = preg_replace('/<((?:span|div)[^>]+)data-id="[^"]+"((?:[^>]+|))>/','<${1} ${2} data-without-body="1" id=' . $entryCodeFormated . ">", $matches[1]); | |
| 194 | 223 | # THE THOR の page-lp.php が " " をけずるための対策 |
| 195 | 224 | # "div class" -> "div class" |
| 196 | 225 | $codoc_tag = preg_replace('/div +class/','div class',$codoc_tag); |
| 197 | 226 | |
| @@ -196,10 +225,8 @@ | ||
| 196 | 225 | $codoc_tag = preg_replace('/div +class/','div class',$codoc_tag); |
| 197 | 226 | |
| 198 | 227 | # タグの前後にHTMLを挿入 |
| 199 | 228 | $CODOC_SETTINGS = $params['codoc_settings']; |
| 200 | - $before = ''; | |
| 201 | - $after = ''; | |
| 202 | 229 | if (!isset($CODOC_SETTINGS['str_before_codoc_tag'])) { |
| 203 | 230 | $CODOC_SETTINGS['str_before_codoc_tag'] = ''; |
| 204 | 231 | } |
| 205 | 232 | if (!isset($CODOC_SETTINGS['str_after_codoc_tag'])) { |
| @@ -204,13 +231,16 @@ | ||
| 204 | 231 | } |
| 205 | 232 | if (!isset($CODOC_SETTINGS['str_after_codoc_tag'])) { |
| 206 | 233 | $CODOC_SETTINGS['str_after_codoc_tag'] = ''; |
| 207 | 234 | } |
| 208 | - $before = $CODOC_SETTINGS['str_before_codoc_tag'] or ""; | |
| 209 | - $after = $CODOC_SETTINGS['str_after_codoc_tag'] or ""; | |
| 210 | - | |
| 211 | 235 | // 無料部分のみ表示 |
| 212 | - return $splited[0] . sprintf('%s<div class="wp-block-codoc-codoc-block">%s</div>%s',$before,$codoc_tag,$after); | |
| 236 | + $post_content = $splited[0] . sprintf( | |
| 237 | + '%s<div class="wp-block-codoc-codoc-block">%s</div>%s', | |
| 238 | + $CODOC_SETTINGS['str_before_codoc_tag'], | |
| 239 | + $codoc_tag, | |
| 240 | + $CODOC_SETTINGS['str_after_codoc_tag'] | |
| 241 | + ); | |
| 242 | + return $post_content; | |
| 213 | 243 | } |
| 214 | 244 | |
| 215 | 245 | |
| 216 | 246 | } |