| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | <?php |
| 2 | - | |
| 2 | +require_once(plugin_dir_path( __FILE__ ) .'class-codoc-util.php'); | |
| 3 | 3 | final class Codoc { |
| 4 | 4 | |
| 5 | 5 | public function __construct() { |
| 6 | 6 | if (is_admin()) { |
| @@ -6,50 +6,51 @@ | ||
| 6 | 6 | if (is_admin()) { |
| 7 | 7 | // setting |
| 8 | 8 | $this->add_settings(); |
| 9 | 9 | } |
| 10 | + | |
| 11 | + // usercode, token はadminページのみDBから取得する | |
| 12 | + $this->util = new CodocUtil(); | |
| 13 | + | |
| 14 | + // paywall用の本文非表示化とcodocタグへの属性追加 | |
| 15 | + add_filter('the_content',[$this,'the_content']); | |
| 16 | + | |
| 10 | 17 | $auth_info = get_option(CODOC_AUTHINFO_OPTION_NAME); |
| 11 | - if (!$auth_info) { | |
| 12 | - return $this; | |
| 13 | - } | |
| 14 | 18 | |
| 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 | - require_once 'src/init.php'; | |
| 19 | + if ($auth_info) { | |
| 20 | + // データ同期 (save_postはis_adminで実行されないケースがある) | |
| 21 | + add_action( 'save_post', [$this,'save_post'], 20, 3 ); | |
| 22 | + add_action( 'added_post_meta', [$this,'updated_post_meta'], 10, 3 ); | |
| 23 | + add_action( 'updated_post_meta',[$this,'updated_post_meta'], 10, 3 ); | |
| 24 | + add_action( 'deleted_post_meta',[$this,'deleted_post_meta'], 10, 4 ); | |
| 25 | + } | |
| 26 | + if (is_admin() and $auth_info) { //管理画面 | |
| 27 | + // Gutenberg のサポートがない場合は何もしない | |
| 28 | + if ( function_exists( 'register_block_type' ) ) { | |
| 29 | + // gutenberg | |
| 30 | + require_once 'src/init.php'; | |
| 31 | + add_action('wp_loaded', function() { | |
| 32 | + wp_localize_script('wordpress-cgb-block-js', 'OPTIONS', array( | |
| 33 | + 'codoc_url' => CODOC_URL, | |
| 34 | + 'codoc_usercode' => get_option(CODOC_USERCODE_OPTION_NAME), | |
| 35 | + 'codoc_plugin_version' => CODOC_PLUGIN_VERSION, | |
| 36 | + 'codoc_sdk_path' => CODOC_SDK_PATH, | |
| 37 | + )); | |
| 38 | + }); | |
| 39 | + } | |
| 24 | 40 | // tinymce |
| 25 | 41 | $this->add_mce(); |
| 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 | - )); | |
| 32 | - | |
| 33 | - // tinymce 用 | |
| 34 | - //wp_enqueue_script( 'codoc-integration-js', CODOC_URL . '/js/integration.js', true ); | |
| 35 | - }); | |
| 36 | - return $this; | |
| 37 | - } | |
| 38 | - | |
| 39 | - // codocのJS登録 | |
| 40 | - | |
| 41 | - add_action( 'wp_enqueue_scripts', function() { | |
| 42 | - wp_enqueue_script( 'codoc-injector-js', CODOC_URL . '/js/cms.js' ); | |
| 43 | - }); | |
| 44 | - // 登録したscriptタグに属性をつける | |
| 45 | - add_filter('script_loader_tag', [$this,'modifier_script_tag'],10,2); | |
| 46 | - // tinymce用のショートコード | |
| 47 | - add_shortcode('codoc', [$this, 'injector_shortcode']); | |
| 48 | - | |
| 49 | - // paywall用の本文非表示化とcodocタグへの属性追加 | |
| 50 | - add_filter('the_content',[$this,'the_content']); | |
| 51 | - return $this; | |
| 42 | + } else { // ブログ画面 | |
| 43 | + // codocのJS登録 | |
| 44 | + add_action( 'wp_enqueue_scripts', function() { | |
| 45 | + wp_enqueue_script( 'codoc-injector-js', CODOC_URL . '/js/cms.js' ); | |
| 46 | + }); | |
| 47 | + // 登録したscriptタグに属性をつける | |
| 48 | + add_filter('script_loader_tag', [$this,'modifier_script_tag'],10,2); | |
| 49 | + // tinymce用のショートコード | |
| 50 | + add_shortcode('codoc', [$this, 'injector_shortcode']); | |
| 51 | + } | |
| 52 | + return $this; | |
| 52 | 53 | } |
| 53 | 54 | |
| 54 | 55 | public function modifier_script_tag($tag,$handle) { |
| 55 | 56 | if($handle !== 'codoc-injector-js') { |
| @@ -59,9 +60,10 @@ | ||
| 59 | 60 | $data_css = ''; |
| 60 | 61 | if ($css_path = $CODOC_SETTINGS['css_path']) { |
| 61 | 62 | $data_css = sprintf(' data-css="%s" ',$css_path); |
| 62 | 63 | } |
| 63 | - return str_replace(' src=', $data_css . ' defer src=', $tag); | |
| 64 | + //return str_replace(' src=', $data_css . ' defer src=', $tag); | |
| 65 | + return preg_replace('/(src=[^>]+)/',' ${1} ' . $data_css . ' defer',$tag); | |
| 64 | 66 | } |
| 65 | 67 | public function injector_shortcode($atts) { |
| 66 | 68 | global $post; |
| 67 | 69 | ob_start(); |
| @@ -67,50 +69,8 @@ | ||
| 67 | 69 | ob_start(); |
| 68 | 70 | require 'views/codoc-injector.php'; |
| 69 | 71 | return ob_get_clean(); |
| 70 | 72 | } |
| 71 | - public function callAPI($method,$path,$body = [],$headers = []) { | |
| 72 | - $usercode = get_option(CODOC_USERCODE_OPTION_NAME); | |
| 73 | - $token = get_option(CODOC_TOKEN_OPTION_NAME); | |
| 74 | - | |
| 75 | - $headers['X-CodocToken'] = $token; | |
| 76 | - $host = CODOC_URL; | |
| 77 | - $sslverify = true; | |
| 78 | - | |
| 79 | - if (preg_match('/local/',CODOC_URL)) { | |
| 80 | - $sslverify = false; | |
| 81 | - $host = 'https://host.docker.internal'; | |
| 82 | - } | |
| 83 | - $url = sprintf("%s/api/v1/cms/%s%s",$host,$usercode,$path); | |
| 84 | - $http = new WP_Http(); | |
| 85 | - try { | |
| 86 | - $response = $http->request( | |
| 87 | - $url, | |
| 88 | - [ | |
| 89 | - 'sslverify' => $sslverify, | |
| 90 | - 'method' => $method, | |
| 91 | - 'timeout' => 10, | |
| 92 | - 'headers' => $headers, | |
| 93 | - 'body' => $body, | |
| 94 | - ] | |
| 95 | - ); | |
| 96 | - if ( is_wp_error($response) || $response['response']['code'] != 200 ) { | |
| 97 | - //何もしない | |
| 98 | - //var_dump( $response ); | |
| 99 | - } | |
| 100 | - } catch(Exception $e) { | |
| 101 | - } | |
| 102 | - | |
| 103 | - if (!is_wp_error($response) and | |
| 104 | - isset($response['body']) and | |
| 105 | - is_string($response['body']) and | |
| 106 | - is_array(json_decode($response['body'], true)) and | |
| 107 | - (json_last_error() == JSON_ERROR_NONE)) { | |
| 108 | - return json_decode($response['body']); | |
| 109 | - } else { | |
| 110 | - return null; | |
| 111 | - } | |
| 112 | - } | |
| 113 | 73 | # settings / 設定関連 |
| 114 | 74 | public function add_settings() { |
| 115 | 75 | global $CODOC_SETTINGS; |
| 116 | 76 | add_action( 'admin_menu' ,function(){ |
| @@ -124,9 +84,9 @@ | ||
| 124 | 84 | echo '<form method="post" action="options.php">'; |
| 125 | 85 | settings_fields( 'codoc_option_group' ); |
| 126 | 86 | do_settings_sections( 'codoc' ); |
| 127 | 87 | submit_button(); // 送信ボタン |
| 128 | - echo '</div>'; | |
| 88 | + echo '</form></div>'; | |
| 129 | 89 | } |
| 130 | 90 | ); |
| 131 | 91 | }); |
| 132 | 92 | |
| @@ -135,12 +95,14 @@ | ||
| 135 | 95 | if (isset($_GET['page']) and $_GET['page'] == 'codoc' and isset($_GET['fetch_token_key'])) { |
| 136 | 96 | $key = sanitize_text_field($_GET['fetch_token_key']); |
| 137 | 97 | $usercode = sanitize_text_field($_GET['usercode']); |
| 138 | 98 | update_option(CODOC_USERCODE_OPTION_NAME,$usercode); |
| 139 | - $data = $this->callAPI('GET','/token',[ "fetch_token_key" => $key ]); | |
| 99 | + //$data = $this->callAPI('GET','/token',[ "fetch_token_key" => $key ]); | |
| 100 | + $data = $this->util->get_token([ "fetch_token_key" => $key ],["usercode" => $usercode, "token" => "1"]); | |
| 140 | 101 | if ($data->status and $token = $data->token) { |
| 141 | 102 | update_option(CODOC_TOKEN_OPTION_NAME,$token); |
| 142 | - $data = $this->callAPI('GET',''); | |
| 103 | + //$data = $this->callAPI('GET',''); | |
| 104 | + $data = $this->util->get_user_info([],["usercode" => $usercode, "token" => $token]); | |
| 143 | 105 | if ($data->status and $user = $data->user) { |
| 144 | 106 | update_option(CODOC_AUTHINFO_OPTION_NAME,[ |
| 145 | 107 | 'email' => $user->email, |
| 146 | 108 | 'name' => $user->name, |
| @@ -176,8 +138,14 @@ | ||
| 176 | 138 | } |
| 177 | 139 | if (!isset($CODOC_SETTINGS['str_replace_binded_url_to'])) { |
| 178 | 140 | $CODOC_SETTINGS['str_replace_binded_url_to'] = ''; |
| 179 | 141 | } |
| 142 | + if (!isset($CODOC_SETTINGS['str_before_codoc_tag'])) { | |
| 143 | + $CODOC_SETTINGS['str_before_codoc_tag'] = ''; | |
| 144 | + } | |
| 145 | + if (!isset($CODOC_SETTINGS['str_after_codoc_tag'])) { | |
| 146 | + $CODOC_SETTINGS['str_after_codoc_tag'] = ''; | |
| 147 | + } | |
| 180 | 148 | |
| 181 | 149 | add_settings_section( |
| 182 | 150 | 'setting_section_id', // id |
| 183 | 151 | 'codoc 設定ページ', // title |
| @@ -213,8 +181,23 @@ | ||
| 213 | 181 | 'codoc', //page |
| 214 | 182 | 'setting_section_id' //Section |
| 215 | 183 | ); |
| 216 | 184 | |
| 185 | + add_settings_field( | |
| 186 | + 'str_around_codoc_tag', // id | |
| 187 | + 'codocタグの前後にHTMLを挿入', // title | |
| 188 | + function() { | |
| 189 | + global $CODOC_SETTINGS; | |
| 190 | + echo sprintf('<textarea name="%s[str_before_codoc_tag]" cols="40">%s</textarea>',CODOC_SETTINGS_OPTION_NAME,$CODOC_SETTINGS['str_before_codoc_tag']); | |
| 191 | + echo ('を前に挿入<br />'); | |
| 192 | + echo sprintf('<textarea name="%s[str_after_codoc_tag]" cols="40">%s</textarea>',CODOC_SETTINGS_OPTION_NAME,$CODOC_SETTINGS['str_after_codoc_tag']); | |
| 193 | + echo ('を後に挿入'); | |
| 194 | + | |
| 195 | + }, | |
| 196 | + 'codoc', //page | |
| 197 | + 'setting_section_id' //Section | |
| 198 | + ); | |
| 199 | + | |
| 217 | 200 | register_setting( |
| 218 | 201 | 'codoc_option_group', // option group |
| 219 | 202 | CODOC_USERCODE_OPTION_NAME |
| 220 | 203 | ); |
| @@ -233,9 +216,9 @@ | ||
| 233 | 216 | function() { |
| 234 | 217 | global $CODOC_USERCODE; |
| 235 | 218 | global $CODOC_TOKEN; |
| 236 | 219 | global $CODOC_AUTHINFO; |
| 237 | - $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';"; | |
| 220 | + $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';"; | |
| 238 | 221 | |
| 239 | 222 | echo sprintf('<p><font color="green" id="codoc-auth-message"><strong>%s</strong> で認証済</font></p>',$CODOC_AUTHINFO['email']); |
| 240 | 223 | |
| 241 | 224 | echo sprintf('<input id="codoc-usercode" type="hidden" name="%s" value="%s">',CODOC_USERCODE_OPTION_NAME,$CODOC_USERCODE); |
| @@ -271,27 +254,31 @@ | ||
| 271 | 254 | 'setting_section_id' |
| 272 | 255 | ); |
| 273 | 256 | } |
| 274 | 257 | // ない場合かつ自分で認証する場合 |
| 275 | - if (!$CODOC_AUTHINFO and (isset($_GET['auth_by_myself']) and $_GET['auth_by_myself'])) { | |
| 258 | + if (!$CODOC_AUTHINFO and ( | |
| 259 | + (isset($_GET['auth_by_myself']) and $_GET['auth_by_myself']) or | |
| 260 | + (isset($_POST['auth_by_myself']) and $_POST['auth_by_myself']) | |
| 261 | + )) { | |
| 276 | 262 | register_setting( |
| 277 | 263 | 'codoc_option_group', // option group |
| 278 | 264 | CODOC_USERCODE_OPTION_NAME |
| 279 | 265 | ); |
| 266 | + register_setting( | |
| 267 | + 'codoc_option_group', // option group | |
| 268 | + CODOC_TOKEN_OPTION_NAME | |
| 269 | + ); | |
| 280 | 270 | add_settings_field( |
| 281 | 271 | 'codoc_usercode', |
| 282 | 272 | 'ユーザーコード', |
| 283 | 273 | function() { |
| 284 | 274 | global $CODOC_USERCODE; |
| 275 | + echo '<input type="hidden" name="auth_by_myself" value="1">'; | |
| 285 | 276 | echo sprintf('<input type="text" name="%s" value="%s">',CODOC_USERCODE_OPTION_NAME,$CODOC_USERCODE); |
| 286 | 277 | }, |
| 287 | 278 | 'codoc', |
| 288 | 279 | 'setting_section_id' |
| 289 | 280 | ); |
| 290 | - register_setting( | |
| 291 | - 'codoc_option_group', // option group | |
| 292 | - CODOC_TOKEN_OPTION_NAME | |
| 293 | - ); | |
| 294 | 281 | add_settings_field( |
| 295 | 282 | 'codoc_token', |
| 296 | 283 | 'APIトークン', |
| 297 | 284 | function() { |
| @@ -314,9 +301,13 @@ | ||
| 314 | 301 | },10,2); |
| 315 | 302 | add_action('updated_option',function(){ |
| 316 | 303 | global $CODOC_RE_AUTHORIZE; |
| 317 | 304 | if ($CODOC_RE_AUTHORIZE) { |
| 318 | - $data = $this->callAPI('GET',''); | |
| 305 | + //$data = $this->callAPI('GET',''); | |
| 306 | + $data = $this->util->get_user_info([],[ | |
| 307 | + "usercode" => get_option(CODOC_USERCODE_OPTION_NAME), | |
| 308 | + "token" => get_option(CODOC_TOKEN_OPTION_NAME), | |
| 309 | + ]); | |
| 319 | 310 | if ($data->status and $user = $data->user) { |
| 320 | 311 | update_option(CODOC_AUTHINFO_OPTION_NAME,[ |
| 321 | 312 | 'email' => $user->email, |
| 322 | 313 | 'name' => $user->name, |
| @@ -339,48 +330,55 @@ | ||
| 339 | 330 | ); |
| 340 | 331 | |
| 341 | 332 | } |
| 342 | 333 | public function add_mce() { |
| 343 | - // Add Shortcode options in the WordPres visual editors | |
| 344 | - //wp_enqueue_script( 'codoc-editor-script', CODOC_URL . 'codoc/src_mce/codoc-editor.js'); | |
| 345 | - | |
| 346 | - add_action( 'init', function() { | |
| 334 | + add_action( 'admin_enqueue_scripts', function() { | |
| 347 | 335 | if ( ! current_user_can( 'edit_posts' ) && ! current_user_can( 'edit_pages' ) ) { |
| 348 | 336 | return; |
| 349 | 337 | } |
| 350 | - if ( get_user_option( 'rich_editing' ) == 'true' ) { | |
| 351 | - // プラグイン内で使うCSRF を生成 | |
| 352 | - $path = plugins_url( 'codoc/src_mce/codoc-editor-onload.js', __DIR__ ); | |
| 353 | - wp_enqueue_script( 'codoc-editor-onload', $path, array('jquery'), '', true ); | |
| 354 | - // I just want to insert this global variables but i don't know how i can do it.. | |
| 355 | - wp_localize_script( | |
| 356 | - 'codoc-editor-onload', | |
| 357 | - 'CODOCEDITOR', | |
| 358 | - array( | |
| 359 | - 'action' => 'codoc_shortcodes', | |
| 360 | - 'nonce' => wp_create_nonce( 'codoc_shortcodes' ), | |
| 338 | + $current_screen = get_current_screen(); | |
| 339 | + if ( ( method_exists( $current_screen, 'is_block_editor' ) && $current_screen->is_block_editor() ) || ( function_exists( 'is_gutenberg_page' ) && is_gutenberg_page() ) ) { | |
| 340 | + // Gutenberg Editor | |
| 341 | + // なにもしない | |
| 342 | + } else { | |
| 343 | + // tinymce | |
| 344 | + if ( get_user_option( 'rich_editing' ) == 'true' ) { | |
| 345 | + // プラグイン内で使うCSRF を生成 | |
| 346 | + $path = plugins_url( 'codoc/src_mce/codoc-editor-onload.js', __DIR__ ); | |
| 347 | + wp_enqueue_script( 'codoc-editor-onload', $path, array('jquery'), '', true ); | |
| 348 | + // I just want to insert this global variables but i don't know how i can do it.. | |
| 349 | + wp_localize_script( | |
| 350 | + 'codoc-editor-onload', | |
| 351 | + 'CODOCEDITOR', | |
| 352 | + array( | |
| 353 | + 'action' => 'codoc_shortcodes', | |
| 354 | + 'nonce' => wp_create_nonce( 'codoc_shortcodes' ), | |
| 361 | 355 | |
| 362 | - 'codoc_url' => CODOC_URL, | |
| 363 | - 'codoc_usercode' => get_option(CODOC_USERCODE_OPTION_NAME), | |
| 364 | - 'codoc_plugin_version' => CODOC_PLUGIN_VERSION, | |
| 365 | - | |
| 366 | - ) | |
| 367 | - ); | |
| 356 | + 'codoc_url' => CODOC_URL, | |
| 357 | + 'codoc_usercode' => get_option(CODOC_USERCODE_OPTION_NAME), | |
| 358 | + 'codoc_plugin_version' => CODOC_PLUGIN_VERSION, | |
| 359 | + 'codoc_sdk_path' => CODOC_SDK_PATH, | |
| 360 | + ) | |
| 361 | + ); | |
| 368 | 362 | |
| 369 | - // ここでtinymceのプラグイン追加 | |
| 370 | - wp_enqueue_style( 'codoc-admin-style', plugins_url( 'codoc/src_mce/codoc-admin.css', __DIR__ ) ); | |
| 371 | - add_editor_style( plugins_url( 'codoc/src_mce/codoc-admin.css', __DIR__ ) ); | |
| 372 | - | |
| 373 | - add_filter( 'mce_external_plugins', function( $plugin_array ) { | |
| 374 | - $plugin_array['codoc'] = plugins_url( 'codoc/src_mce/codoc-editor.js', __DIR__ ); | |
| 375 | - return $plugin_array; | |
| 376 | - } ); | |
| 377 | - add_filter( 'mce_buttons', function( $buttons ) { | |
| 378 | - array_push( $buttons, "|", "codoc" ); | |
| 379 | - return $buttons; | |
| 380 | - } ); | |
| 381 | - | |
| 382 | - } | |
| 363 | + // ここでtinymceのプラグイン追加 | |
| 364 | + //wp_enqueue_style( 'codoc-admin-style', plugins_url( 'codoc/src_mce/codoc-admin.css', __DIR__ ) ); | |
| 365 | + //add_editor_style( plugins_url( 'codoc/src_mce/codoc-admin.css', __DIR__ ) ); | |
| 366 | + wp_enqueue_style( 'codoc-admin-style', CODOC_URL . CODOC_SDK_PATH . '.tinymce.css?c=' . date('Ymd') ); | |
| 367 | + add_editor_style( CODOC_URL . CODOC_SDK_PATH . '.tinymce.css' ); | |
| 368 | + | |
| 369 | + add_filter( 'mce_external_plugins', function( $plugin_array ) { | |
| 370 | + //$plugin_array['codoc'] = plugins_url( 'codoc/src_mce/codoc-editor.js', __DIR__ ); | |
| 371 | + $plugin_array['codoc'] = CODOC_URL . CODOC_SDK_PATH . '.tinymce.js?c=' . date('Ymd'); | |
| 372 | + return $plugin_array; | |
| 373 | + } ); | |
| 374 | + add_filter( 'mce_buttons', function( $buttons ) { | |
| 375 | + array_push( $buttons, "|", "codoc" ); | |
| 376 | + return $buttons; | |
| 377 | + } ); | |
| 378 | + | |
| 379 | + } | |
| 380 | + } | |
| 383 | 381 | } ); |
| 384 | 382 | } |
| 385 | 383 | public function save_post($post_ID,$post,$update) { |
| 386 | 384 | if (preg_match('/^(auto-draft|inherit)$/',$post->post_status)) { |
| @@ -385,66 +383,25 @@ | ||
| 385 | 383 | public function save_post($post_ID,$post,$update) { |
| 386 | 384 | if (preg_match('/^(auto-draft|inherit)$/',$post->post_status)) { |
| 387 | 385 | return; |
| 388 | 386 | } |
| 389 | - | |
| 390 | - $post_content = $post->post_content; | |
| 391 | - preg_match('/wp:codoc\/codoc-block +?({.*})/',$post_content,$matches); | |
| 392 | - // GUTENBERG で指定されたjson(記事情報)がない場合 | |
| 393 | - if (!$matches) { | |
| 394 | - return; | |
| 395 | - } | |
| 396 | - // GUTENBERG で保存している内容を取得 | |
| 397 | - $codoc_info = json_decode($matches[1],true); | |
| 398 | - // codoc タグがない場合はなにもしない | |
| 399 | - //preg_match('/(<span +data-id="codoc-tag"[^>]+>(?:.+|)<\/span>)/',$post_content,$matches); | |
| 400 | - preg_match('/(<(?:span|div)[^>]+data-id="codoc-tag"(?:[^>]+|)>(?:.+|)<\/(?:span|div)>)/',$post_content,$matches); | |
| 401 | - if (!$matches) { | |
| 402 | - return; | |
| 403 | - } | |
| 404 | - // TODO: split or html parse? HTMLがつながってる場合はおかしくなる | |
| 405 | - // classic(tinymce)版の場合p、もしくはdivにかこまれている | |
| 406 | - $splited = preg_split('/(?:<(?:div|p)(?:[^>]+|)>|)<\!-- +wp:codoc\/codoc-block .*<\!-- +\/wp:codoc\/codoc-block +-->(?:<\/(?:div|p)>|)/s',$post_content); | |
| 407 | - $status = $post->post_status == 'publish' ? 1 : 0; | |
| 408 | - # $body_free = ''; | |
| 409 | - # $body_paywalled = ''; | |
| 410 | - # if (count($splited) >= 2) { | |
| 411 | - # | |
| 412 | - # } | |
| 413 | - $binded_url = get_permalink($post_ID); | |
| 414 | - $CODOC_SETTINGS = get_option(CODOC_SETTINGS_OPTION_NAME); | |
| 415 | - if (isset($CODOC_SETTINGS['str_replace_binded_url_from']) and | |
| 416 | - isset($CODOC_SETTINGS['str_replace_binded_url_to']) and | |
| 417 | - $CODOC_SETTINGS['str_replace_binded_url_from']) { | |
| 418 | - $binded_url = str_replace(sanitize_text_field($CODOC_SETTINGS['str_replace_binded_url_from']), | |
| 419 | - sanitize_text_field($CODOC_SETTINGS['str_replace_binded_url_to']), | |
| 420 | - $binded_url); | |
| 421 | - } | |
| 422 | - | |
| 423 | - $params = [ | |
| 424 | - 'title' => $post->post_title, | |
| 425 | - 'body_free' => $splited[0], | |
| 426 | - 'body_paywalled' => $splited[1], | |
| 427 | - 'status' => $status, | |
| 428 | - 'binded_url' => $binded_url, | |
| 429 | - 'show_price' => isset($codoc_info['showPrice']) ? ($codoc_info['showPrice'] ? 1 : 0) : 0, | |
| 430 | - 'price' => isset($codoc_info['price']) ? $codoc_info['price'] : 100, | |
| 431 | - 'limited' => isset($codoc_info['limited']) ? ($codoc_info['limited'] ? 1 : 0) : 0, | |
| 432 | - 'limited_count' => isset($codoc_info['limitedCount']) ? $codoc_info['limitedCount'] : 1, | |
| 433 | - 'affiliate_mode' => isset($codoc_info['affiliateMode']) ? ($codoc_info['affiliateMode'] ? 1 : 0) : 0, | |
| 434 | - 'affiliate_rate' => isset($codoc_info['affiliateRate']) ? $codoc_info['affiliateRate'] : '0.0500', | |
| 435 | - 'show_support' => isset($codoc_info['showSupport']) ? ($codoc_info['showSupport'] ? 1 : 0) : 0, | |
| 436 | - 'subscriptions' => isset($codoc_info['subscriptions']) ? array_keys($codoc_info['subscriptions']) : [], | |
| 437 | - ]; | |
| 387 | + | |
| 438 | 388 | $entryCode = get_post_meta($post_ID,'codoc_entry_code',true); |
| 439 | - if ($entryCode) { | |
| 440 | - $res = $this->callAPI('PUT','/entries/' . $entryCode ,$params); | |
| 441 | - } else { | |
| 442 | - $res = $this->callAPI('POST', '/entries', $params); | |
| 443 | - if (is_object($res) and $res->status) { | |
| 444 | - update_post_meta($post_ID,'codoc_entry_code',$res->entry->code); | |
| 445 | - } | |
| 389 | + $res = $this->util->sync_entry([ | |
| 390 | + "post_title" => $post->post_title, | |
| 391 | + "post_content" => $post->post_content, | |
| 392 | + "post_status" => ($post->post_status == 'publish' ? 1 : 0), | |
| 393 | + "post_permalink" => get_permalink($post_ID), | |
| 394 | + "codoc_entry_code" => $entryCode, | |
| 395 | + ],[ | |
| 396 | + "usercode" => get_option(CODOC_USERCODE_OPTION_NAME), | |
| 397 | + "token" => get_option(CODOC_TOKEN_OPTION_NAME), | |
| 398 | + ]); | |
| 399 | + | |
| 400 | + if (is_object($res) and $res->status and !$entryCode) { | |
| 401 | + update_post_meta($post_ID,'codoc_entry_code',$res->entry->code); | |
| 446 | 402 | } |
| 403 | + | |
| 447 | 404 | return true; |
| 448 | 405 | } |
| 449 | 406 | function updated_post_meta( $meta_ID, $post_ID, $meta_key ) { |
| 450 | 407 | if ($meta_key != '_thumbnail_id') { |
| @@ -453,35 +410,17 @@ | ||
| 453 | 410 | if ( has_post_thumbnail($post_ID) ) { |
| 454 | 411 | $attachment = wp_get_attachment_metadata( get_post_thumbnail_id($post_ID)); |
| 455 | 412 | $upload_dir = wp_upload_dir(); |
| 456 | 413 | $file_path = sprintf ('%s/%s',$upload_dir['basedir'],$attachment['file']); |
| 457 | - | |
| 458 | - if (is_readable($file_path)) { | |
| 459 | - $name = 'file'; | |
| 460 | - | |
| 461 | - $boundary = wp_generate_password(24); | |
| 462 | - | |
| 463 | - $payload = ''; | |
| 464 | - $payload .= '--' . $boundary; | |
| 465 | - $payload .= "\r\n"; | |
| 466 | - $payload .= 'Content-Disposition: form-data; name="' . $name . '"; filename="' . basename( $file_path ) . '"' . "\r\n"; | |
| 467 | - $payload .= "Content-Type: application/octet-stream\r\n"; | |
| 468 | - $payload .= "Content-Transfer-Encoding: binary\r\n"; | |
| 469 | - $payload .= "\r\n"; | |
| 470 | - $payload .= file_get_contents( $file_path ); | |
| 471 | - $payload .= "\r\n"; | |
| 472 | - | |
| 473 | - $payload .= '--' . $boundary . '--'; | |
| 474 | - | |
| 475 | - | |
| 476 | - $entryCode = get_post_meta($post_ID,'codoc_entry_code',true); | |
| 477 | - $res = $this->callAPI( | |
| 478 | - 'POST', | |
| 479 | - '/entries/' . $entryCode . '/thumbnail', | |
| 480 | - $payload, | |
| 481 | - ['content-type' => 'multipart/form-data; boundary=' . $boundary] | |
| 482 | - ); | |
| 483 | - } | |
| 414 | + | |
| 415 | + return $this->util->post_thumbnail([ | |
| 416 | + "file_path" => $file_path, | |
| 417 | + "boundary" => wp_generate_password(24), | |
| 418 | + "codoc_entry_code" => get_post_meta($post_ID,'codoc_entry_code',true), | |
| 419 | + ],[ | |
| 420 | + "usercode" => get_option(CODOC_USERCODE_OPTION_NAME), | |
| 421 | + "token" => get_option(CODOC_TOKEN_OPTION_NAME), | |
| 422 | + ]); | |
| 484 | 423 | } |
| 485 | 424 | } |
| 486 | 425 | function deleted_post_meta( $meta_ids, $post_ID, $meta_key,$meta_value ) { |
| 487 | 426 | if ($meta_key != '_thumbnail_id') { |
| @@ -486,39 +425,27 @@ | ||
| 486 | 425 | function deleted_post_meta( $meta_ids, $post_ID, $meta_key,$meta_value ) { |
| 487 | 426 | if ($meta_key != '_thumbnail_id') { |
| 488 | 427 | return; |
| 489 | 428 | } |
| 490 | - $entryCode = get_post_meta($post_ID,'codoc_entry_code',true); | |
| 491 | - $res = $this->callAPI( | |
| 492 | - 'POST', | |
| 493 | - '/entries/' . $entryCode . '/thumbnail', | |
| 494 | - [ "reset" => 1 ] | |
| 495 | - ); | |
| 496 | - } | |
| 429 | + return $this->util->reset_thumbnail( | |
| 430 | + ["codoc_entry_code" => get_post_meta($post_ID,'codoc_entry_code',true)], | |
| 431 | + [ | |
| 432 | + "usercode" => get_option(CODOC_USERCODE_OPTION_NAME), | |
| 433 | + "token" => get_option(CODOC_TOKEN_OPTION_NAME), | |
| 434 | + ] | |
| 435 | + ); | |
| 436 | + } | |
| 437 | + | |
| 497 | 438 | function the_content( $post_content ) { |
| 498 | - if ( is_single() && in_the_loop() && is_main_query() ) { | |
| 499 | - } | |
| 500 | - // codoc タグがあるかどうか (202001:span -> div に変更) | |
| 501 | - preg_match('/(<(span|div)[^>]+data-id="codoc-tag"(?:[^>]+|)>(?:.+|)<\/(?:div|span)>)/',$post_content,$matches); | |
| 502 | - // data-wp-plugin-ver が無いタグは分割しない | |
| 503 | - if (!$matches) { | |
| 504 | - return $post_content; | |
| 505 | - } | |
| 506 | - // codocタグで分割 (202001: gutenbergで直前のdivタグを削除) | |
| 507 | - $tag_regex = '/(?:<div(?:[^>]+|)>|)<(?:span|div)[^>]+data-id="codoc-tag"(?:[^>]+|)>(?:.+|)<\/(?:span|div)>(?:<\/div>|)/'; | |
| 508 | - if ( is_preview() ) { | |
| 509 | - $post_content = preg_replace($tag_regex,'<div class="codoc-continue">ここから上は無料で表示されます</div>',$post_content); | |
| 510 | - return $post_content; | |
| 511 | - } | |
| 512 | - // codoc タグの前後で分ける | |
| 513 | - $splited = preg_split($tag_regex,$post_content); | |
| 514 | - // codoc タグにID属性のentrycodeとdata-without-body(無料分を非表示)をつける | |
| 439 | + if ( is_single() && in_the_loop() && is_main_query() ) { | |
| 440 | + } | |
| 515 | 441 | $post = get_post(); |
| 516 | - $entryCode = sprintf('"codoc-entry-%s" ',get_post_meta($post->ID,'codoc_entry_code',true)); | |
| 517 | - $codoc_tag = preg_replace('/<((?:span|div)[^>]+)data-id="[^"]+"((?:[^>]+|))>/','<${1} ${2} data-without-body="1" id=' . $entryCode . ">", $matches[1]); | |
| 518 | - // 無料部分のみ表示 | |
| 519 | - return $splited[0] . sprintf('<div class="wp-block-codoc-codoc-block">%s</div>',$codoc_tag); | |
| 442 | + return $this->util->filter_content([ | |
| 443 | + "post_content" => $post_content, | |
| 444 | + "preview" => is_preview(), | |
| 445 | + "codoc_entry_code" => get_post_meta($post->ID,'codoc_entry_code',true), | |
| 446 | + "codoc_settings" => get_option(CODOC_SETTINGS_OPTION_NAME), | |
| 447 | + ]); | |
| 520 | 448 | } |
| 521 | - | |
| 522 | 449 | |
| 523 | 450 | } |
| 524 | 451 | |