| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('WPINC')) { |
| 4 |
define( 'WPINC', 'wp-includes' ); |
| 5 |
} |
| 6 |
|
| 7 |
require_once( ABSPATH . WPINC . '/class-http.php'); |
| 8 |
require_once( ABSPATH . WPINC . '/class-wp-error.php'); |
| 9 |
|
| 10 |
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 |
return $this; |
| 16 |
} |
| 17 |
|
| 18 |
public function callAPI($method,$path,$body = [],$headers = []) { |
| 19 |
$usercode = $this->usercode; |
| 20 |
$token = $this->token; |
| 21 |
|
| 22 |
$headers['X-CodocToken'] = $token; |
| 23 |
$host = CODOC_URL; |
| 24 |
$sslverify = true; |
| 25 |
|
| 26 |
if (preg_match('/local/',CODOC_URL)) { |
| 27 |
$sslverify = false; |
| 28 |
$host = 'https://host.docker.internal'; |
| 29 |
} |
| 30 |
$url = sprintf("%s/api/v1/cms/%s%s",$host,$usercode,$path); |
| 31 |
$http = new WP_Http(); |
| 32 |
try { |
| 33 |
$response = $http->request( |
| 34 |
$url, |
| 35 |
[ |
| 36 |
'sslverify' => $sslverify, |
| 37 |
'method' => $method, |
| 38 |
'timeout' => 10, |
| 39 |
'headers' => $headers, |
| 40 |
'body' => $body, |
| 41 |
] |
| 42 |
); |
| 43 |
if ( is_wp_error($response) || $response['response']['code'] != 200 ) { |
| 44 |
//何もしない |
| 45 |
//var_dump( $response ); |
| 46 |
} |
| 47 |
} catch(Exception $e) { |
| 48 |
|
| 49 |
} |
| 50 |
if (!is_wp_error($response) and |
| 51 |
isset($response['body']) and |
| 52 |
is_string($response['body']) and |
| 53 |
is_array(json_decode($response['body'], true)) and |
| 54 |
(json_last_error() == JSON_ERROR_NONE)) { |
| 55 |
return json_decode($response['body']); |
| 56 |
} else { |
| 57 |
return null; |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
public function sync_entry($params = [ |
| 62 |
"post_title" => null, |
| 63 |
"post_content" => null, |
| 64 |
"post_status" => null, # 0, 1 |
| 65 |
"post_permalink" => null, |
| 66 |
"codoc_entry_code" => null, |
| 67 |
]) { |
| 68 |
$post_content = $params['post_content']; |
| 69 |
preg_match('/wp:codoc\/codoc-block +?({.*})/',$post_content,$matches); |
| 70 |
// GUTENBERG で指定されたjson(記事� |
| 71 |
報)がない場合 |
| 72 |
if (!$matches) { |
| 73 |
return; |
| 74 |
} |
| 75 |
// GUTENBERG で保存している� |
| 76 |
容を取得 |
| 77 |
$codoc_info = json_decode($matches[1],true); |
| 78 |
// codoc タグがない場合はなにもしない |
| 79 |
//preg_match('/(<span +data-id="codoc-tag"[^>]+>(?:.+|)<\/span>)/',$post_content,$matches); |
| 80 |
preg_match('/(<(?:span|div)[^>]+data-id="codoc-tag"(?:[^>]+|)>(?:.+|)<\/(?:span|div)>)/',$post_content,$matches); |
| 81 |
if (!$matches) { |
| 82 |
return; |
| 83 |
} |
| 84 |
// TODO: split or html parse? HTMLがつながってる場合はおかしくなる |
| 85 |
// classic(tinymce)版の場合p、もしくはdivにかこまれている |
| 86 |
$splited = preg_split('/(?:<(?:div|p)(?:[^>]+|)>|)<\!-- +wp:codoc\/codoc-block .*<\!-- +\/wp:codoc\/codoc-block +-->(?:<\/(?:div|p)>|)/s',$post_content); |
| 87 |
$status = $params['post_status']; |
| 88 |
# $body_free = ''; |
| 89 |
# $body_paywalled = ''; |
| 90 |
# if (count($splited) >= 2) { |
| 91 |
# |
| 92 |
# } |
| 93 |
$binded_url = $params['post_permalink']; |
| 94 |
$CODOC_SETTINGS = get_option(CODOC_SETTINGS_OPTION_NAME); |
| 95 |
if (isset($CODOC_SETTINGS['str_replace_binded_url_from']) and |
| 96 |
isset($CODOC_SETTINGS['str_replace_binded_url_to']) and |
| 97 |
$CODOC_SETTINGS['str_replace_binded_url_from']) { |
| 98 |
$binded_url = str_replace(sanitize_text_field($CODOC_SETTINGS['str_replace_binded_url_from']), |
| 99 |
sanitize_text_field($CODOC_SETTINGS['str_replace_binded_url_to']), |
| 100 |
$binded_url); |
| 101 |
} |
| 102 |
$api_params = [ |
| 103 |
'title' => $params['post_title'], |
| 104 |
'body_free' => $splited[0], |
| 105 |
'body_paywalled' => $splited[1], |
| 106 |
'status' => $status, |
| 107 |
'binded_url' => $binded_url, |
| 108 |
'show_price' => isset($codoc_info['showPrice']) ? ($codoc_info['showPrice'] ? 1 : 0) : 0, |
| 109 |
'price' => isset($codoc_info['price']) ? $codoc_info['price'] : 100, |
| 110 |
'limited' => isset($codoc_info['limited']) ? ($codoc_info['limited'] ? 1 : 0) : 0, |
| 111 |
'limited_count' => isset($codoc_info['limitedCount']) ? $codoc_info['limitedCount'] : 1, |
| 112 |
'affiliate_mode' => isset($codoc_info['affiliateMode']) ? ($codoc_info['affiliateMode'] ? 1 : 0) : 0, |
| 113 |
'affiliate_rate' => isset($codoc_info['affiliateRate']) ? $codoc_info['affiliateRate'] : '0.0500', |
| 114 |
'show_support' => isset($codoc_info['showSupport']) ? ($codoc_info['showSupport'] ? 1 : 0) : 0, |
| 115 |
'subscriptions' => isset($codoc_info['subscriptions']) ? array_keys($codoc_info['subscriptions']) : [], |
| 116 |
]; |
| 117 |
$entryCode = $params['codoc_entry_code']; |
| 118 |
$res = null; |
| 119 |
if ($entryCode) { |
| 120 |
$res = $this->callAPI('PUT','/entries/' . $entryCode ,$api_params); |
| 121 |
} else { |
| 122 |
$res = $this->callAPI('POST', '/entries', $api_params); |
| 123 |
} |
| 124 |
return $res; |
| 125 |
} |
| 126 |
|
| 127 |
function post_thumbnail( $params = [ |
| 128 |
"file_path" => null, |
| 129 |
"boundary" => null, |
| 130 |
"codoc_entry_code" => null, |
| 131 |
] ) { |
| 132 |
$file_path = $params['file_path']; |
| 133 |
$boundary = $params['boundary']; # ランダム変数24桁 wp_generate_password(24); |
| 134 |
$res = null; |
| 135 |
if (is_readable($file_path)) { |
| 136 |
$name = 'file'; |
| 137 |
|
| 138 |
$payload = ''; |
| 139 |
$payload .= '--' . $boundary; |
| 140 |
$payload .= "\r\n"; |
| 141 |
$payload .= 'Content-Disposition: form-data; name="' . $name . '"; filename="' . basename( $file_path ) . '"' . "\r\n"; |
| 142 |
$payload .= "Content-Type: application/octet-stream\r\n"; |
| 143 |
$payload .= "Content-Transfer-Encoding: binary\r\n"; |
| 144 |
$payload .= "\r\n"; |
| 145 |
$payload .= file_get_contents( $file_path ); |
| 146 |
$payload .= "\r\n"; |
| 147 |
|
| 148 |
$payload .= '--' . $boundary . '--'; |
| 149 |
|
| 150 |
$entryCode = $params['codoc_entry_code']; |
| 151 |
$res = $this->callAPI( |
| 152 |
'POST', |
| 153 |
'/entries/' . $entryCode . '/thumbnail', |
| 154 |
$payload, |
| 155 |
['content-type' => 'multipart/form-data; boundary=' . $boundary] |
| 156 |
); |
| 157 |
} |
| 158 |
return $res; |
| 159 |
} |
| 160 |
|
| 161 |
function reset_thumbnail( $params = [ |
| 162 |
"codoc_entry_code" => null, |
| 163 |
]) { |
| 164 |
$entryCode = $params['codoc_entry_code']; |
| 165 |
$res = $this->callAPI( |
| 166 |
'POST', |
| 167 |
'/entries/' . $entryCode . '/thumbnail', |
| 168 |
[ "reset" => 1 ] |
| 169 |
); |
| 170 |
return $res; |
| 171 |
} |
| 172 |
|
| 173 |
function filter_content( $params = [ |
| 174 |
"post_content" => null, |
| 175 |
"preview" => null, |
| 176 |
"codoc_entry_code" => null, |
| 177 |
]) { |
| 178 |
$post_content = $params['post_content']; |
| 179 |
// codoc タグがあるかどうか (202001:span -> div に変更) |
| 180 |
preg_match('/(<(span|div)[^>]+data-id="codoc-tag"(?:[^>]+|)>(?:.+|)<\/(?:div|span)>)/',$post_content,$matches); |
| 181 |
// data-wp-plugin-ver が無いタグは分割しない |
| 182 |
if (!$matches) { |
| 183 |
return $post_content; |
| 184 |
} |
| 185 |
// codocタグで分割 (202001: gutenbergで直前のdivタグを削除) |
| 186 |
$tag_regex = '/(?:<div(?:[^>]+|)>|)<(?:span|div)[^>]+data-id="codoc-tag"(?:[^>]+|)>(?:.+|)<\/(?:span|div)>(?:<\/div>|)/'; |
| 187 |
if ( $params['preview'] ) { |
| 188 |
$post_content = preg_replace($tag_regex,'<div class="codoc-continue">ここから上は無料で表示されます</div>',$post_content); |
| 189 |
return $post_content; |
| 190 |
} |
| 191 |
// codoc タグの前後で分ける |
| 192 |
$splited = preg_split($tag_regex,$post_content); |
| 193 |
// codoc タグにID属性のentrycodeとdata-without-body(無料分を非表示)をつける |
| 194 |
$entryCode = $params['codoc_entry_code']; |
| 195 |
$codoc_tag = preg_replace('/<((?:span|div)[^>]+)data-id="[^"]+"((?:[^>]+|))>/','<${1} ${2} data-without-body="1" id=' . $entryCode . ">", $matches[1]); |
| 196 |
# THE THOR の page-lp.php が " " をけずるための対策 |
| 197 |
# "div class" -> "div class" |
| 198 |
$codoc_tag = preg_replace('/div +class/','div class',$codoc_tag); |
| 199 |
|
| 200 |
# タグの前後にHTMLを挿� |
| 201 |
� |
| 202 |
$CODOC_SETTINGS = $params['codoc_settings']; |
| 203 |
$before = ''; |
| 204 |
$after = ''; |
| 205 |
if (!isset($CODOC_SETTINGS['str_before_codoc_tag'])) { |
| 206 |
$CODOC_SETTINGS['str_before_codoc_tag'] = ''; |
| 207 |
} |
| 208 |
if (!isset($CODOC_SETTINGS['str_after_codoc_tag'])) { |
| 209 |
$CODOC_SETTINGS['str_after_codoc_tag'] = ''; |
| 210 |
} |
| 211 |
$before = $CODOC_SETTINGS['str_before_codoc_tag'] or ""; |
| 212 |
$after = $CODOC_SETTINGS['str_after_codoc_tag'] or ""; |
| 213 |
|
| 214 |
// 無料部分のみ表示 |
| 215 |
return $splited[0] . sprintf('%s<div class="wp-block-codoc-codoc-block">%s</div>%s',$before,$codoc_tag,$after); |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
} |
| 220 |
|
| 221 |
|