]+|)>|)<(?:span|div)[^>]+data-id="codoc-tag"(?:[^>]+|)>(?:.+|)<\/(?:span|div)>(?:<\/div>|)/';
$end_tag_regex = '/<(?:div|p)>::CODOC_WP_END_PAYWALL::<\/(?:div|p)>/';
if ( $params['preview'] ) {
$post_content = preg_replace($tag_regex,'
ここから上は無料で表示されます
',$post_content);
$post_content = preg_replace($end_tag_regex,'
ここから下は無料で表示されます
',$post_content);
return $post_content;
}
// codoc タグの前後で分ける
$before_splited = preg_split($end_tag_regex,$post_content);
$splited = preg_split($tag_regex,$before_splited[0]);
// codoc タグにID属性のentrycodeとdata-without-body(無料分を非表示)をつける
$entryCodeFormated = sprintf('"codoc-entry-%s" ',$params['codoc_entry_code']);
// 文言系の設定をつける
$tagAttributes = '';
if (isset($CODOC_SETTINGS['show_like']) and $CODOC_SETTINGS['show_like'] != 1) {
$tagAttributes = $tagAttributes . sprintf(' data-show-like="%s"',$CODOC_SETTINGS['show_like']);
}
if (isset($CODOC_SETTINGS['show_about_codoc']) and $CODOC_SETTINGS['show_about_codoc'] != 1) {
$tagAttributes = $tagAttributes . sprintf(' data-show-about-codoc="%s"',$CODOC_SETTINGS['show_about_codoc']);
}
if (isset($CODOC_SETTINGS['show_powered_by']) and $CODOC_SETTINGS['show_powered_by'] != 1) {
$tagAttributes = $tagAttributes . sprintf(' data-show-powered-by="%s"',$CODOC_SETTINGS['show_powered_by']);
}
if (isset($CODOC_SETTINGS['show_created_by']) and $CODOC_SETTINGS['show_created_by'] != 1) {
$tagAttributes = $tagAttributes . sprintf(' data-show-created-by="%s"',$CODOC_SETTINGS['show_created_by']);
}
if (isset($CODOC_SETTINGS['show_copyright']) and $CODOC_SETTINGS['show_copyright'] != 1) {
$tagAttributes = $tagAttributes . sprintf(' data-show-copyright="%s"',$CODOC_SETTINGS['show_copyright']);
}
if (isset($CODOC_SETTINGS['entry_button_text']) and $CODOC_SETTINGS['entry_button_text'] != '') {
$tagAttributes = $tagAttributes . sprintf(' data-entry-button-text="%s"',$CODOC_SETTINGS['entry_button_text']);
}
if (isset($CODOC_SETTINGS['subscription_button_text']) and $CODOC_SETTINGS['subscription_button_text'] != '') {
$tagAttributes = $tagAttributes . sprintf(' data-subscription-button-text="%s"',$CODOC_SETTINGS['subscription_button_text']);
}
if (isset($CODOC_SETTINGS['support_button_text']) and $CODOC_SETTINGS['support_button_text'] != '') {
$tagAttributes = $tagAttributes . sprintf(' data-support-button-text="%s"',$CODOC_SETTINGS['support_button_text']);
}
if (isset($CODOC_SETTINGS['subscription_message']) and $CODOC_SETTINGS['subscription_message'] != '') {
$tagAttributes = $tagAttributes . sprintf(' data-subscription-message="%s"',$CODOC_SETTINGS['subscription_message']);
}
if (isset($CODOC_SETTINGS['support_message']) and $CODOC_SETTINGS['support_message'] != '') {
$tagAttributes = $tagAttributes . sprintf(' data-support-message="%s"',$CODOC_SETTINGS['support_message']);
}
if (isset($CODOC_SETTINGS['codoc_tag_attributes']) and $CODOC_SETTINGS['codoc_tag_attributes'] != '') {
$tagAttributes = $tagAttributes . sprintf(' %s',$CODOC_SETTINGS['codoc_tag_attributes']);
}
$codoc_tag = preg_replace('/<((?:span|div)[^>]+)data-id="[^"]+"((?:[^>]+|))>/','<${1} ${2} data-without-body="1" id=' . $entryCodeFormated . $tagAttributes . ">", $matches[1]);
# THE THOR の page-lp.php が " " をけずるための対策
# "div class" -> "div class"
$codoc_tag = preg_replace('/div +class/','div class',$codoc_tag);
if ($params['is_amp_endpoint']) {
$format = preg_replace_callback('/(<(?:span|div)[^>]+>)((?:[^<>]+|))(<\/(?:span|div)>)/',function($matches) {
$format = $matches[1] . '
%s' . $matches[3];
return sprintf($format,($matches[2] ? $matches[2] : '続きを読む'));
},$codoc_tag);
$codoc_tag = sprintf($format,$params["post_permalink"]);
}
# タグの前後にHTMLを挿入
if (!isset($CODOC_SETTINGS['str_before_codoc_tag'])) {
$CODOC_SETTINGS['str_before_codoc_tag'] = '';
}
if (!isset($CODOC_SETTINGS['str_after_codoc_tag'])) {
$CODOC_SETTINGS['str_after_codoc_tag'] = '';
}
// 無料部分のみ表示
$post_content = $splited[0] . sprintf(
'%s
%s
%s',
$CODOC_SETTINGS['str_before_codoc_tag'],
$codoc_tag,
$CODOC_SETTINGS['str_after_codoc_tag']
);
// ::CODOC_WP_END_PAYWALL:: の後を足す
if (isset($before_splited[1]) and $before_splited[1]) {
$post_content .= $before_splited[1];
}
return $post_content;
}
/*
// Cookieの中のトークンを取得
$_CODOC->util->get_paywall_token_code();
*/
function get_paywall_token_code() {
if (isset($_COOKIE['codocTokenCode']) and $codocTokenCode = $_COOKIE['codocTokenCode']) {
return $codocTokenCode;
}
return null;
}
/*
$_CODOC->util->check_login($mode); // cookie / strict
*/
function check_login($mode = 'cookie') {
if ($codocTokenCode = $this->get_paywall_token_code()) {
if ($mode == 'cookie') {
// cookieの中にtokenがあればOK
return true;
}
if ($mode == 'strict') {
// ログイン状態を問い合わせ
$res = $this->callPaywallAPI('GET','/users',["paywall_token_code" => $codocTokenCode ]);
if ($res->status) {
return true;
} else {
return false;
}
}
}
return false;
}
/*
$_CODOC->util->check_owned($entry_code);
*/
function check_owned($entry_code = null) {
if (!$entry_code) {
return false;
}
$res = $this->callPaywallAPI('GET','/entries/' . $entry_code, ["paywall_token_code" => $this->get_paywall_token_code(), "check_owned" => 1 ]);
if ($res->status) {
return true;
}
return false;
}
/*
$_CODOC->util->health_check();
*/
function health_check() {
$sslverify = true;
$host = $this->codoc_url;
if (preg_match('/local/',$this->codoc_url)) {
$sslverify = false;
$host = 'https://host.docker.internal';
}
$url = sprintf("%s/codoc_health_check",$host);
$http = new WP_Http();
try {
$response = $http->request(
$url,
[
'sslverify' => $sslverify,
'method' => 'GET',
'timeout' => 10,
'headers' => [],
'body' => [],
]
);
} catch(Exception $e) {
}
if ($response and $response['response']['code'] == '200') {
return true;
} else {
return null;
}
}
}