| 1 |
<?php |
| 2 |
|
| 3 |
final class Codoc { |
| 4 |
|
| 5 |
public function __construct() { |
| 6 |
if (is_admin()) { |
| 7 |
// setting |
| 8 |
$this->add_settings(); |
| 9 |
} |
| 10 |
$auth_info = get_option(CODOC_AUTHINFO_OPTION_NAME); |
| 11 |
if (!$auth_info) { |
| 12 |
return $this; |
| 13 |
} |
| 14 |
|
| 15 |
// データ同期 (save_postはis_adminで実行されないケースがある) |
| 16 |
add_action( 'save_post', [$this,'save_post'], 20, 3 ); |
| 17 |
add_action( 'added_post_meta', [$this,'updated_post_meta'], 10, 3 ); |
| 18 |
add_action( 'updated_post_meta',[$this,'updated_post_meta'], 10, 3 ); |
| 19 |
add_action( 'deleted_post_meta',[$this,'deleted_post_meta'], 10, 4 ); |
| 20 |
|
| 21 |
if (is_admin()) { |
| 22 |
// Gutenberg のサポートがない場合は何もしない |
| 23 |
if ( function_exists( 'register_block_type' ) ) { |
| 24 |
// gutenberg |
| 25 |
require_once 'src/init.php'; |
| 26 |
add_action('wp_loaded', function() { |
| 27 |
wp_localize_script('wordpress-cgb-block-js', 'OPTIONS', array( |
| 28 |
'codoc_url' => CODOC_URL, |
| 29 |
'codoc_usercode' => get_option(CODOC_USERCODE_OPTION_NAME), |
| 30 |
'codoc_plugin_version' => CODOC_PLUGIN_VERSION, |
| 31 |
'codoc_sdk_path' => CODOC_SDK_PATH, |
| 32 |
)); |
| 33 |
}); |
| 34 |
} |
| 35 |
// tinymce |
| 36 |
$this->add_mce(); |
| 37 |
|
| 38 |
return $this; |
| 39 |
} |
| 40 |
|
| 41 |
// codocのJS登録 |
| 42 |
|
| 43 |
add_action( 'wp_enqueue_scripts', function() { |
| 44 |
wp_enqueue_script( 'codoc-injector-js', CODOC_URL . '/js/cms.js' ); |
| 45 |
}); |
| 46 |
// 登録したscriptタグに属性をつける |
| 47 |
add_filter('script_loader_tag', [$this,'modifier_script_tag'],10,2); |
| 48 |
// tinymce用のショートコード |
| 49 |
add_shortcode('codoc', [$this, 'injector_shortcode']); |
| 50 |
|
| 51 |
// paywall用の本文非表示化とcodocタグへの属性追加 |
| 52 |
add_filter('the_content',[$this,'the_content']); |
| 53 |
return $this; |
| 54 |
} |
| 55 |
|
| 56 |
public function modifier_script_tag($tag,$handle) { |
| 57 |
if($handle !== 'codoc-injector-js') { |
| 58 |
return $tag; |
| 59 |
} |
| 60 |
$CODOC_SETTINGS = get_option(CODOC_SETTINGS_OPTION_NAME); |
| 61 |
$data_css = ''; |
| 62 |
if ($css_path = $CODOC_SETTINGS['css_path']) { |
| 63 |
$data_css = sprintf(' data-css="%s" ',$css_path); |
| 64 |
} |
| 65 |
//return str_replace(' src=', $data_css . ' defer src=', $tag); |
| 66 |
return preg_replace('/(src=[^>]+)/',' ${1} ' . $data_css . ' defer',$tag); |
| 67 |
} |
| 68 |
public function injector_shortcode($atts) { |
| 69 |
global $post; |
| 70 |
ob_start(); |
| 71 |
require 'views/codoc-injector.php'; |
| 72 |
return ob_get_clean(); |
| 73 |
} |
| 74 |
public function callAPI($method,$path,$body = [],$headers = []) { |
| 75 |
$usercode = get_option(CODOC_USERCODE_OPTION_NAME); |
| 76 |
$token = get_option(CODOC_TOKEN_OPTION_NAME); |
| 77 |
|
| 78 |
$headers['X-CodocToken'] = $token; |
| 79 |
$host = CODOC_URL; |
| 80 |
$sslverify = true; |
| 81 |
|
| 82 |
if (preg_match('/local/',CODOC_URL)) { |
| 83 |
$sslverify = false; |
| 84 |
$host = 'https://host.docker.internal'; |
| 85 |
} |
| 86 |
$url = sprintf("%s/api/v1/cms/%s%s",$host,$usercode,$path); |
| 87 |
$http = new WP_Http(); |
| 88 |
try { |
| 89 |
$response = $http->request( |
| 90 |
$url, |
| 91 |
[ |
| 92 |
'sslverify' => $sslverify, |
| 93 |
'method' => $method, |
| 94 |
'timeout' => 10, |
| 95 |
'headers' => $headers, |
| 96 |
'body' => $body, |
| 97 |
] |
| 98 |
); |
| 99 |
if ( is_wp_error($response) || $response['response']['code'] != 200 ) { |
| 100 |
//何もしない |
| 101 |
//var_dump( $response ); |
| 102 |
} |
| 103 |
} catch(Exception $e) { |
| 104 |
} |
| 105 |
|
| 106 |
if (!is_wp_error($response) and |
| 107 |
isset($response['body']) and |
| 108 |
is_string($response['body']) and |
| 109 |
is_array(json_decode($response['body'], true)) and |
| 110 |
(json_last_error() == JSON_ERROR_NONE)) { |
| 111 |
return json_decode($response['body']); |
| 112 |
} else { |
| 113 |
return null; |
| 114 |
} |
| 115 |
} |
| 116 |
# settings / 設定関連 |
| 117 |
public function add_settings() { |
| 118 |
global $CODOC_SETTINGS; |
| 119 |
add_action( 'admin_menu' ,function(){ |
| 120 |
add_options_page( |
| 121 |
'codoc の設定', //ページタイトル |
| 122 |
'codoc', //設定メニューに表示されるメニュータイトル |
| 123 |
'edit_users', //権限 |
| 124 |
'codoc', //設定ページのURL。options-general.php?page=codoc |
| 125 |
function() { |
| 126 |
echo '<div clas="wrap">'; |
| 127 |
echo '<form method="post" action="options.php">'; |
| 128 |
settings_fields( 'codoc_option_group' ); |
| 129 |
do_settings_sections( 'codoc' ); |
| 130 |
submit_button(); // 送信ボタン |
| 131 |
echo '</div>'; |
| 132 |
} |
| 133 |
); |
| 134 |
}); |
| 135 |
|
| 136 |
add_action( "admin_init", function() { |
| 137 |
// codocからの認証データ処理 |
| 138 |
if (isset($_GET['page']) and $_GET['page'] == 'codoc' and isset($_GET['fetch_token_key'])) { |
| 139 |
$key = sanitize_text_field($_GET['fetch_token_key']); |
| 140 |
$usercode = sanitize_text_field($_GET['usercode']); |
| 141 |
update_option(CODOC_USERCODE_OPTION_NAME,$usercode); |
| 142 |
$data = $this->callAPI('GET','/token',[ "fetch_token_key" => $key ]); |
| 143 |
if ($data->status and $token = $data->token) { |
| 144 |
update_option(CODOC_TOKEN_OPTION_NAME,$token); |
| 145 |
$data = $this->callAPI('GET',''); |
| 146 |
if ($data->status and $user = $data->user) { |
| 147 |
update_option(CODOC_AUTHINFO_OPTION_NAME,[ |
| 148 |
'email' => $user->email, |
| 149 |
'name' => $user->name, |
| 150 |
]); |
| 151 |
} |
| 152 |
#$current_url = (empty($_SERVER['HTTPS']) ? 'http://' : 'https://').$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; |
| 153 |
#$current_url = preg_replace('/(.*)fetch_token_key.*/','${1}&codoc_auth_finished=1',$current_url); |
| 154 |
//add_settings_error( 'general', 'settings_updated', __( 'OK' ), 'success' ); |
| 155 |
$current_url = admin_url('options-general.php') . '?page=codoc&codoc_auth_finished=1'; |
| 156 |
|
| 157 |
wp_redirect( $current_url); |
| 158 |
exit; |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
global $CODOC_USERCODE; |
| 163 |
global $CODOC_SETTINGS; |
| 164 |
global $CODOC_TOKEN; |
| 165 |
global $CODOC_AUTHINFO; |
| 166 |
$CODOC_USERCODE = get_option(CODOC_USERCODE_OPTION_NAME); |
| 167 |
$CODOC_SETTINGS = get_option(CODOC_SETTINGS_OPTION_NAME); |
| 168 |
$CODOC_TOKEN = get_option(CODOC_TOKEN_OPTION_NAME); |
| 169 |
$CODOC_AUTHINFO = get_option(CODOC_AUTHINFO_OPTION_NAME); |
| 170 |
if( !$CODOC_SETTINGS ) { |
| 171 |
//デフォルト値 |
| 172 |
$CODOC_SETTINGS = array( |
| 173 |
'css_path' => '', |
| 174 |
); |
| 175 |
update_option( CODOC_SETTINGS_OPTION_NAME, $CODOC_SETTINGS ); |
| 176 |
} |
| 177 |
if (!isset($CODOC_SETTINGS['str_replace_binded_url_from'])) { |
| 178 |
$CODOC_SETTINGS['str_replace_binded_url_from'] = ''; |
| 179 |
} |
| 180 |
if (!isset($CODOC_SETTINGS['str_replace_binded_url_to'])) { |
| 181 |
$CODOC_SETTINGS['str_replace_binded_url_to'] = ''; |
| 182 |
} |
| 183 |
if (!isset($CODOC_SETTINGS['str_before_codoc_tag'])) { |
| 184 |
$CODOC_SETTINGS['str_before_codoc_tag'] = ''; |
| 185 |
} |
| 186 |
if (!isset($CODOC_SETTINGS['str_after_codoc_tag'])) { |
| 187 |
$CODOC_SETTINGS['str_after_codoc_tag'] = ''; |
| 188 |
} |
| 189 |
|
| 190 |
add_settings_section( |
| 191 |
'setting_section_id', // id |
| 192 |
'codoc 設定ページ', // title |
| 193 |
function (){}, // callback |
| 194 |
'codoc' // page |
| 195 |
); |
| 196 |
// 認証がある場合 |
| 197 |
if ($CODOC_AUTHINFO) { |
| 198 |
register_setting( |
| 199 |
'codoc_option_group', // option group |
| 200 |
CODOC_SETTINGS_OPTION_NAME // option name(DB) |
| 201 |
); |
| 202 |
add_settings_field( |
| 203 |
'css_path', // id |
| 204 |
'カスタマイズ用CSSパス', // title |
| 205 |
function() { |
| 206 |
global $CODOC_SETTINGS; |
| 207 |
echo sprintf('<input type="text" name="%s[css_path]" value="%s">',CODOC_SETTINGS_OPTION_NAME,$CODOC_SETTINGS['css_path']); |
| 208 |
}, |
| 209 |
'codoc', //page |
| 210 |
'setting_section_id' //Section |
| 211 |
); |
| 212 |
add_settings_field( |
| 213 |
'str_replace_binded_url', // id |
| 214 |
'codoc側に登録するパーマリンクを置換', // title |
| 215 |
function() { |
| 216 |
global $CODOC_SETTINGS; |
| 217 |
echo sprintf('<input type="text" name="%s[str_replace_binded_url_from]" value="%s">',CODOC_SETTINGS_OPTION_NAME,$CODOC_SETTINGS['str_replace_binded_url_from']); |
| 218 |
echo ('を'); |
| 219 |
echo sprintf('<input type="text" name="%s[str_replace_binded_url_to]" value="%s">',CODOC_SETTINGS_OPTION_NAME,$CODOC_SETTINGS['str_replace_binded_url_to']); |
| 220 |
echo ('に変換'); |
| 221 |
}, |
| 222 |
'codoc', //page |
| 223 |
'setting_section_id' //Section |
| 224 |
); |
| 225 |
|
| 226 |
add_settings_field( |
| 227 |
'str_around_codoc_tag', // id |
| 228 |
'codocタグの前後にHTMLを挿� |
| 229 |
�', // title |
| 230 |
function() { |
| 231 |
global $CODOC_SETTINGS; |
| 232 |
echo sprintf('<textarea name="%s[str_before_codoc_tag]" cols="40">%s</textarea>',CODOC_SETTINGS_OPTION_NAME,$CODOC_SETTINGS['str_before_codoc_tag']); |
| 233 |
echo ('を前に挿� |
| 234 |
�<br />'); |
| 235 |
echo sprintf('<textarea name="%s[str_after_codoc_tag]" cols="40">%s</textarea>',CODOC_SETTINGS_OPTION_NAME,$CODOC_SETTINGS['str_after_codoc_tag']); |
| 236 |
echo ('を後に挿� |
| 237 |
�'); |
| 238 |
|
| 239 |
}, |
| 240 |
'codoc', //page |
| 241 |
'setting_section_id' //Section |
| 242 |
); |
| 243 |
|
| 244 |
register_setting( |
| 245 |
'codoc_option_group', // option group |
| 246 |
CODOC_USERCODE_OPTION_NAME |
| 247 |
); |
| 248 |
register_setting( |
| 249 |
'codoc_option_group', // option group |
| 250 |
CODOC_TOKEN_OPTION_NAME |
| 251 |
); |
| 252 |
|
| 253 |
if (isset($_GET['codoc_auth_finished']) and $_GET['codoc_auth_finished']) { |
| 254 |
add_settings_error( 'general', 'settings_updated', __( 'codocの認証が完了しました' ), 'success' ); |
| 255 |
} |
| 256 |
|
| 257 |
add_settings_field( |
| 258 |
'codoc_auth', |
| 259 |
'認証', |
| 260 |
function() { |
| 261 |
global $CODOC_USERCODE; |
| 262 |
global $CODOC_TOKEN; |
| 263 |
global $CODOC_AUTHINFO; |
| 264 |
$script = "javascript:document.getElementById('codoc-usercode').value='';document.getElementById('codoc-token').value=' ';document.getElementById('codoc-auth-message').innerText='解除を完了するには変更を保存してください';document.getElementById('codoc-auth-message').color='red';"; |
| 265 |
|
| 266 |
echo sprintf('<p><font color="green" id="codoc-auth-message"><strong>%s</strong> で認証済</font></p>',$CODOC_AUTHINFO['email']); |
| 267 |
|
| 268 |
echo sprintf('<input id="codoc-usercode" type="hidden" name="%s" value="%s">',CODOC_USERCODE_OPTION_NAME,$CODOC_USERCODE); |
| 269 |
echo sprintf('<input id="codoc-token" type="hidden" name="%s" value="%s">',CODOC_TOKEN_OPTION_NAME,$CODOC_TOKEN); |
| 270 |
echo sprintf('<p>認証を<a href="javascript:void(0);" onClick="' . $script . '">解除する</p>'); |
| 271 |
}, |
| 272 |
'codoc', |
| 273 |
'setting_section_id' |
| 274 |
); |
| 275 |
|
| 276 |
} |
| 277 |
|
| 278 |
// ない場合 |
| 279 |
if (!$CODOC_AUTHINFO and !isset($_GET['auth_by_myself'])) { |
| 280 |
add_settings_field( |
| 281 |
'codoc_auth', |
| 282 |
'認証', |
| 283 |
function() { |
| 284 |
|
| 285 |
//$current_url = (empty($_SERVER['HTTPS']) ? 'http://' : 'https://').$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; |
| 286 |
$current_url = admin_url('options-general.php') . '?page=codoc'; |
| 287 |
$login_url = sprintf("location.href='%s'",CODOC_URL . '/me/token?from=wp&return_url=' . urlencode($current_url)); |
| 288 |
$register_url = sprintf("location.href='%s'",CODOC_URL . '/register?from=wp&return_url=' . urlencode($current_url)); |
| 289 |
// submitを消しておく |
| 290 |
echo ('<script type="text/javascript">window.onload=function(){document.getElementById(\'submit\').style.display = \'none\'}</script>'); |
| 291 |
echo ('codocをWordPressでご利用いただくには認証が� |
| 292 |
要です。<br />'); |
| 293 |
echo sprintf('ユーザーコードとAPIトークンを<a href="%s&auth_by_myself=1">直接� |
| 294 |
�力</a>することでも認証できます。<br /><br />',$current_url); |
| 295 |
echo sprintf('<input type="button" class="button button-primary" value="ログインして認証" onClick="%s"> <input type="button" class="button button-primary" value="登録して認証" onClick="%s"><br /><br />',$login_url,$register_url); |
| 296 |
|
| 297 |
|
| 298 |
}, |
| 299 |
'codoc', |
| 300 |
'setting_section_id' |
| 301 |
); |
| 302 |
} |
| 303 |
// ない場合かつ自分で認証する場合 |
| 304 |
if (!$CODOC_AUTHINFO and (isset($_GET['auth_by_myself']) and $_GET['auth_by_myself'])) { |
| 305 |
register_setting( |
| 306 |
'codoc_option_group', // option group |
| 307 |
CODOC_USERCODE_OPTION_NAME |
| 308 |
); |
| 309 |
add_settings_field( |
| 310 |
'codoc_usercode', |
| 311 |
'ユーザーコード', |
| 312 |
function() { |
| 313 |
global $CODOC_USERCODE; |
| 314 |
echo sprintf('<input type="text" name="%s" value="%s">',CODOC_USERCODE_OPTION_NAME,$CODOC_USERCODE); |
| 315 |
}, |
| 316 |
'codoc', |
| 317 |
'setting_section_id' |
| 318 |
); |
| 319 |
register_setting( |
| 320 |
'codoc_option_group', // option group |
| 321 |
CODOC_TOKEN_OPTION_NAME |
| 322 |
); |
| 323 |
add_settings_field( |
| 324 |
'codoc_token', |
| 325 |
'APIトークン', |
| 326 |
function() { |
| 327 |
global $CODOC_TOKEN; |
| 328 |
echo sprintf('<input type="text" name="%s" value="%s">',CODOC_TOKEN_OPTION_NAME,$CODOC_TOKEN); |
| 329 |
}, |
| 330 |
'codoc', |
| 331 |
'setting_section_id' |
| 332 |
); |
| 333 |
} |
| 334 |
|
| 335 |
// 認証� |
| 336 |
報を保存 |
| 337 |
add_action( 'update_option_' . CODOC_USERCODE_OPTION_NAME, function( $old_value, $new_value ) { |
| 338 |
global $CODOC_RE_AUTHORIZE; |
| 339 |
$CODOC_RE_AUTHORIZE = 1; |
| 340 |
},9,2); // $hook, $function_to_add, $priority, $accepted_args |
| 341 |
add_action( 'update_option_' . CODOC_TOKEN_OPTION_NAME, function( $old_value, $new_value ) { |
| 342 |
global $CODOC_RE_AUTHORIZE; |
| 343 |
$CODOC_RE_AUTHORIZE = 1; |
| 344 |
},10,2); |
| 345 |
add_action('updated_option',function(){ |
| 346 |
global $CODOC_RE_AUTHORIZE; |
| 347 |
if ($CODOC_RE_AUTHORIZE) { |
| 348 |
$data = $this->callAPI('GET',''); |
| 349 |
if ($data->status and $user = $data->user) { |
| 350 |
update_option(CODOC_AUTHINFO_OPTION_NAME,[ |
| 351 |
'email' => $user->email, |
| 352 |
'name' => $user->name, |
| 353 |
]); |
| 354 |
} else { |
| 355 |
update_option(CODOC_AUTHINFO_OPTION_NAME,''); |
| 356 |
} |
| 357 |
} |
| 358 |
}); |
| 359 |
|
| 360 |
}); |
| 361 |
// プラグイン一覧に設定のリンクをいれる |
| 362 |
add_filter( 'plugin_action_links_' . plugin_basename( plugin_dir_path( __FILE__ ) . 'codoc' . '.php' ), |
| 363 |
function( $links ) { |
| 364 |
$setting_link = sprintf( '<a href="%s">%s</a>', esc_url( add_query_arg( 'page', 'codoc', admin_url( 'options-general.php' ) ) ), esc_html( '設定' ) ); |
| 365 |
array_unshift( $links, $setting_link ); |
| 366 |
|
| 367 |
return $links; |
| 368 |
} |
| 369 |
); |
| 370 |
|
| 371 |
} |
| 372 |
public function add_mce() { |
| 373 |
// Add Shortcode options in the WordPres visual editors |
| 374 |
//wp_enqueue_script( 'codoc-editor-script', CODOC_URL . 'codoc/src_mce/codoc-editor.js'); |
| 375 |
|
| 376 |
add_action( 'init', function() { |
| 377 |
if ( ! current_user_can( 'edit_posts' ) && ! current_user_can( 'edit_pages' ) ) { |
| 378 |
return; |
| 379 |
} |
| 380 |
if ( get_user_option( 'rich_editing' ) == 'true' ) { |
| 381 |
// プラグイン� |
| 382 |
で使うCSRF を生成 |
| 383 |
$path = plugins_url( 'codoc/src_mce/codoc-editor-onload.js', __DIR__ ); |
| 384 |
wp_enqueue_script( 'codoc-editor-onload', $path, array('jquery'), '', true ); |
| 385 |
// I just want to insert this global variables but i don't know how i can do it.. |
| 386 |
wp_localize_script( |
| 387 |
'codoc-editor-onload', |
| 388 |
'CODOCEDITOR', |
| 389 |
array( |
| 390 |
'action' => 'codoc_shortcodes', |
| 391 |
'nonce' => wp_create_nonce( 'codoc_shortcodes' ), |
| 392 |
|
| 393 |
'codoc_url' => CODOC_URL, |
| 394 |
'codoc_usercode' => get_option(CODOC_USERCODE_OPTION_NAME), |
| 395 |
'codoc_plugin_version' => CODOC_PLUGIN_VERSION, |
| 396 |
'codoc_sdk_path' => CODOC_SDK_PATH, |
| 397 |
) |
| 398 |
); |
| 399 |
|
| 400 |
// ここでtinymceのプラグイン追加 |
| 401 |
//wp_enqueue_style( 'codoc-admin-style', plugins_url( 'codoc/src_mce/codoc-admin.css', __DIR__ ) ); |
| 402 |
//add_editor_style( plugins_url( 'codoc/src_mce/codoc-admin.css', __DIR__ ) ); |
| 403 |
wp_enqueue_style( 'codoc-admin-style', CODOC_URL . CODOC_SDK_PATH . '.tinymce.css?c=' . date('Ymd') ); |
| 404 |
add_editor_style( CODOC_URL . CODOC_SDK_PATH . '.tinymce.css' ); |
| 405 |
|
| 406 |
add_filter( 'mce_external_plugins', function( $plugin_array ) { |
| 407 |
//$plugin_array['codoc'] = plugins_url( 'codoc/src_mce/codoc-editor.js', __DIR__ ); |
| 408 |
$plugin_array['codoc'] = CODOC_URL . CODOC_SDK_PATH . '.tinymce.js?c=' . date('Ymd'); |
| 409 |
return $plugin_array; |
| 410 |
} ); |
| 411 |
add_filter( 'mce_buttons', function( $buttons ) { |
| 412 |
array_push( $buttons, "|", "codoc" ); |
| 413 |
return $buttons; |
| 414 |
} ); |
| 415 |
|
| 416 |
} |
| 417 |
} ); |
| 418 |
} |
| 419 |
public function save_post($post_ID,$post,$update) { |
| 420 |
if (preg_match('/^(auto-draft|inherit)$/',$post->post_status)) { |
| 421 |
return; |
| 422 |
} |
| 423 |
|
| 424 |
$post_content = $post->post_content; |
| 425 |
preg_match('/wp:codoc\/codoc-block +?({.*})/',$post_content,$matches); |
| 426 |
// GUTENBERG で指定されたjson(記事� |
| 427 |
報)がない場合 |
| 428 |
if (!$matches) { |
| 429 |
return; |
| 430 |
} |
| 431 |
// GUTENBERG で保存している� |
| 432 |
容を取得 |
| 433 |
$codoc_info = json_decode($matches[1],true); |
| 434 |
// codoc タグがない場合はなにもしない |
| 435 |
//preg_match('/(<span +data-id="codoc-tag"[^>]+>(?:.+|)<\/span>)/',$post_content,$matches); |
| 436 |
preg_match('/(<(?:span|div)[^>]+data-id="codoc-tag"(?:[^>]+|)>(?:.+|)<\/(?:span|div)>)/',$post_content,$matches); |
| 437 |
if (!$matches) { |
| 438 |
return; |
| 439 |
} |
| 440 |
// TODO: split or html parse? HTMLがつながってる場合はおかしくなる |
| 441 |
// classic(tinymce)版の場合p、もしくはdivにかこまれている |
| 442 |
$splited = preg_split('/(?:<(?:div|p)(?:[^>]+|)>|)<\!-- +wp:codoc\/codoc-block .*<\!-- +\/wp:codoc\/codoc-block +-->(?:<\/(?:div|p)>|)/s',$post_content); |
| 443 |
$status = $post->post_status == 'publish' ? 1 : 0; |
| 444 |
# $body_free = ''; |
| 445 |
# $body_paywalled = ''; |
| 446 |
# if (count($splited) >= 2) { |
| 447 |
# |
| 448 |
# } |
| 449 |
$binded_url = get_permalink($post_ID); |
| 450 |
$CODOC_SETTINGS = get_option(CODOC_SETTINGS_OPTION_NAME); |
| 451 |
if (isset($CODOC_SETTINGS['str_replace_binded_url_from']) and |
| 452 |
isset($CODOC_SETTINGS['str_replace_binded_url_to']) and |
| 453 |
$CODOC_SETTINGS['str_replace_binded_url_from']) { |
| 454 |
$binded_url = str_replace(sanitize_text_field($CODOC_SETTINGS['str_replace_binded_url_from']), |
| 455 |
sanitize_text_field($CODOC_SETTINGS['str_replace_binded_url_to']), |
| 456 |
$binded_url); |
| 457 |
} |
| 458 |
|
| 459 |
$params = [ |
| 460 |
'title' => $post->post_title, |
| 461 |
'body_free' => $splited[0], |
| 462 |
'body_paywalled' => $splited[1], |
| 463 |
'status' => $status, |
| 464 |
'binded_url' => $binded_url, |
| 465 |
'show_price' => isset($codoc_info['showPrice']) ? ($codoc_info['showPrice'] ? 1 : 0) : 0, |
| 466 |
'price' => isset($codoc_info['price']) ? $codoc_info['price'] : 100, |
| 467 |
'limited' => isset($codoc_info['limited']) ? ($codoc_info['limited'] ? 1 : 0) : 0, |
| 468 |
'limited_count' => isset($codoc_info['limitedCount']) ? $codoc_info['limitedCount'] : 1, |
| 469 |
'affiliate_mode' => isset($codoc_info['affiliateMode']) ? ($codoc_info['affiliateMode'] ? 1 : 0) : 0, |
| 470 |
'affiliate_rate' => isset($codoc_info['affiliateRate']) ? $codoc_info['affiliateRate'] : '0.0500', |
| 471 |
'show_support' => isset($codoc_info['showSupport']) ? ($codoc_info['showSupport'] ? 1 : 0) : 0, |
| 472 |
'subscriptions' => isset($codoc_info['subscriptions']) ? array_keys($codoc_info['subscriptions']) : [], |
| 473 |
]; |
| 474 |
$entryCode = get_post_meta($post_ID,'codoc_entry_code',true); |
| 475 |
if ($entryCode) { |
| 476 |
$res = $this->callAPI('PUT','/entries/' . $entryCode ,$params); |
| 477 |
} else { |
| 478 |
$res = $this->callAPI('POST', '/entries', $params); |
| 479 |
if (is_object($res) and $res->status) { |
| 480 |
update_post_meta($post_ID,'codoc_entry_code',$res->entry->code); |
| 481 |
} |
| 482 |
} |
| 483 |
return true; |
| 484 |
} |
| 485 |
function updated_post_meta( $meta_ID, $post_ID, $meta_key ) { |
| 486 |
if ($meta_key != '_thumbnail_id') { |
| 487 |
return; |
| 488 |
} |
| 489 |
if ( has_post_thumbnail($post_ID) ) { |
| 490 |
$attachment = wp_get_attachment_metadata( get_post_thumbnail_id($post_ID)); |
| 491 |
$upload_dir = wp_upload_dir(); |
| 492 |
$file_path = sprintf ('%s/%s',$upload_dir['basedir'],$attachment['file']); |
| 493 |
|
| 494 |
if (is_readable($file_path)) { |
| 495 |
$name = 'file'; |
| 496 |
|
| 497 |
$boundary = wp_generate_password(24); |
| 498 |
|
| 499 |
$payload = ''; |
| 500 |
$payload .= '--' . $boundary; |
| 501 |
$payload .= "\r\n"; |
| 502 |
$payload .= 'Content-Disposition: form-data; name="' . $name . '"; filename="' . basename( $file_path ) . '"' . "\r\n"; |
| 503 |
$payload .= "Content-Type: application/octet-stream\r\n"; |
| 504 |
$payload .= "Content-Transfer-Encoding: binary\r\n"; |
| 505 |
$payload .= "\r\n"; |
| 506 |
$payload .= file_get_contents( $file_path ); |
| 507 |
$payload .= "\r\n"; |
| 508 |
|
| 509 |
$payload .= '--' . $boundary . '--'; |
| 510 |
|
| 511 |
|
| 512 |
$entryCode = get_post_meta($post_ID,'codoc_entry_code',true); |
| 513 |
$res = $this->callAPI( |
| 514 |
'POST', |
| 515 |
'/entries/' . $entryCode . '/thumbnail', |
| 516 |
$payload, |
| 517 |
['content-type' => 'multipart/form-data; boundary=' . $boundary] |
| 518 |
); |
| 519 |
} |
| 520 |
} |
| 521 |
} |
| 522 |
function deleted_post_meta( $meta_ids, $post_ID, $meta_key,$meta_value ) { |
| 523 |
if ($meta_key != '_thumbnail_id') { |
| 524 |
return; |
| 525 |
} |
| 526 |
$entryCode = get_post_meta($post_ID,'codoc_entry_code',true); |
| 527 |
$res = $this->callAPI( |
| 528 |
'POST', |
| 529 |
'/entries/' . $entryCode . '/thumbnail', |
| 530 |
[ "reset" => 1 ] |
| 531 |
); |
| 532 |
} |
| 533 |
function the_content( $post_content ) { |
| 534 |
if ( is_single() && in_the_loop() && is_main_query() ) { |
| 535 |
} |
| 536 |
// codoc タグがあるかどうか (202001:span -> div に変更) |
| 537 |
preg_match('/(<(span|div)[^>]+data-id="codoc-tag"(?:[^>]+|)>(?:.+|)<\/(?:div|span)>)/',$post_content,$matches); |
| 538 |
// data-wp-plugin-ver が無いタグは分割しない |
| 539 |
if (!$matches) { |
| 540 |
return $post_content; |
| 541 |
} |
| 542 |
// codocタグで分割 (202001: gutenbergで直前のdivタグを削除) |
| 543 |
$tag_regex = '/(?:<div(?:[^>]+|)>|)<(?:span|div)[^>]+data-id="codoc-tag"(?:[^>]+|)>(?:.+|)<\/(?:span|div)>(?:<\/div>|)/'; |
| 544 |
if ( is_preview() ) { |
| 545 |
$post_content = preg_replace($tag_regex,'<div class="codoc-continue">ここから上は無料で表示されます</div>',$post_content); |
| 546 |
return $post_content; |
| 547 |
} |
| 548 |
// codoc タグの前後で分ける |
| 549 |
$splited = preg_split($tag_regex,$post_content); |
| 550 |
// codoc タグにID属性のentrycodeとdata-without-body(無料分を非表示)をつける |
| 551 |
$post = get_post(); |
| 552 |
$entryCode = sprintf('"codoc-entry-%s" ',get_post_meta($post->ID,'codoc_entry_code',true)); |
| 553 |
$codoc_tag = preg_replace('/<((?:span|div)[^>]+)data-id="[^"]+"((?:[^>]+|))>/','<${1} ${2} data-without-body="1" id=' . $entryCode . ">", $matches[1]); |
| 554 |
|
| 555 |
# THE THOR の page-lp.php が " " をけずるための対策 |
| 556 |
# "div class" -> "div class" |
| 557 |
$codoc_tag = preg_replace('/div +class/','div class',$codoc_tag); |
| 558 |
|
| 559 |
$CODOC_SETTINGS = get_option(CODOC_SETTINGS_OPTION_NAME); |
| 560 |
$before = $CODOC_SETTINGS['str_before_codoc_tag'] or ""; |
| 561 |
$after = $CODOC_SETTINGS['str_after_codoc_tag'] or ""; |
| 562 |
|
| 563 |
// 無料部分のみ表示 |
| 564 |
return $splited[0] . sprintf('%s<div class="wp-block-codoc-codoc-block">%s</div>%s',$before,$codoc_tag,$after); |
| 565 |
} |
| 566 |
|
| 567 |
|
| 568 |
} |
| 569 |
|
| 570 |
|