| @@ -8,27 +8,23 @@ | ||
| 8 | 8 | $this->add_settings(); |
| 9 | 9 | } |
| 10 | 10 | |
| 11 | 11 | // usercode, token はadminページのみDBから取得する |
| 12 | - $this->util = new CodocUtil(["usercode" => 1, "token" => 1 ]); | |
| 13 | - | |
| 14 | - $auth_info = get_option(CODOC_AUTHINFO_OPTION_NAME); | |
| 15 | - if (!$auth_info) { | |
| 16 | - return $this; | |
| 17 | - } | |
| 18 | - $this->util->usercode = get_option(CODOC_USERCODE_OPTION_NAME); | |
| 19 | - $this->util->token = get_option(CODOC_TOKEN_OPTION_NAME); | |
| 20 | - | |
| 12 | + $this->util = new CodocUtil(); | |
| 21 | 13 | |
| 22 | - // データ同期 (save_postはis_adminで実行されないケースがある) | |
| 23 | - add_action( 'save_post', [$this,'save_post'], 20, 3 ); | |
| 24 | - add_action( 'added_post_meta', [$this,'updated_post_meta'], 10, 3 ); | |
| 25 | - add_action( 'updated_post_meta',[$this,'updated_post_meta'], 10, 3 ); | |
| 26 | - add_action( 'deleted_post_meta',[$this,'deleted_post_meta'], 10, 4 ); | |
| 27 | 14 | // paywall用の本文非表示化とcodocタグへの属性追加 |
| 28 | 15 | add_filter('the_content',[$this,'the_content']); |
| 29 | - | |
| 30 | - if (is_admin()) { | |
| 16 | + | |
| 17 | + $auth_info = get_option(CODOC_AUTHINFO_OPTION_NAME); | |
| 18 | + | |
| 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) { //管理画面 | |
| 31 | 27 | // Gutenberg のサポートがない場合は何もしない |
| 32 | 28 | if ( function_exists( 'register_block_type' ) ) { |
| 33 | 29 | // gutenberg |
| 34 | 30 | require_once 'src/init.php'; |
| @@ -42,23 +38,19 @@ | ||
| 42 | 38 | }); |
| 43 | 39 | } |
| 44 | 40 | // tinymce |
| 45 | 41 | $this->add_mce(); |
| 46 | - | |
| 47 | - return $this; | |
| 48 | - } | |
| 49 | - | |
| 50 | - // codocのJS登録 | |
| 51 | - | |
| 52 | - add_action( 'wp_enqueue_scripts', function() { | |
| 53 | - wp_enqueue_script( 'codoc-injector-js', CODOC_URL . '/js/cms.js' ); | |
| 54 | - }); | |
| 55 | - // 登録したscriptタグに属性をつける | |
| 56 | - add_filter('script_loader_tag', [$this,'modifier_script_tag'],10,2); | |
| 57 | - // tinymce用のショートコード | |
| 58 | - add_shortcode('codoc', [$this, 'injector_shortcode']); | |
| 59 | - | |
| 60 | - 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; | |
| 61 | 53 | } |
| 62 | 54 | |
| 63 | 55 | public function modifier_script_tag($tag,$handle) { |
| 64 | 56 | if($handle !== 'codoc-injector-js') { |
| @@ -77,50 +69,8 @@ | ||
| 77 | 69 | ob_start(); |
| 78 | 70 | require 'views/codoc-injector.php'; |
| 79 | 71 | return ob_get_clean(); |
| 80 | 72 | } |
| 81 | - public function callAPI($method,$path,$body = [],$headers = []) { | |
| 82 | - $usercode = get_option(CODOC_USERCODE_OPTION_NAME); | |
| 83 | - $token = get_option(CODOC_TOKEN_OPTION_NAME); | |
| 84 | - | |
| 85 | - $headers['X-CodocToken'] = $token; | |
| 86 | - $host = CODOC_URL; | |
| 87 | - $sslverify = true; | |
| 88 | - | |
| 89 | - if (preg_match('/local/',CODOC_URL)) { | |
| 90 | - $sslverify = false; | |
| 91 | - $host = 'https://host.docker.internal'; | |
| 92 | - } | |
| 93 | - $url = sprintf("%s/api/v1/cms/%s%s",$host,$usercode,$path); | |
| 94 | - $http = new WP_Http(); | |
| 95 | - try { | |
| 96 | - $response = $http->request( | |
| 97 | - $url, | |
| 98 | - [ | |
| 99 | - 'sslverify' => $sslverify, | |
| 100 | - 'method' => $method, | |
| 101 | - 'timeout' => 10, | |
| 102 | - 'headers' => $headers, | |
| 103 | - 'body' => $body, | |
| 104 | - ] | |
| 105 | - ); | |
| 106 | - if ( is_wp_error($response) || $response['response']['code'] != 200 ) { | |
| 107 | - //何もしない | |
| 108 | - //var_dump( $response ); | |
| 109 | - } | |
| 110 | - } catch(Exception $e) { | |
| 111 | - } | |
| 112 | - | |
| 113 | - if (!is_wp_error($response) and | |
| 114 | - isset($response['body']) and | |
| 115 | - is_string($response['body']) and | |
| 116 | - is_array(json_decode($response['body'], true)) and | |
| 117 | - (json_last_error() == JSON_ERROR_NONE)) { | |
| 118 | - return json_decode($response['body']); | |
| 119 | - } else { | |
| 120 | - return null; | |
| 121 | - } | |
| 122 | - } | |
| 123 | 73 | # settings / 設定関連 |
| 124 | 74 | public function add_settings() { |
| 125 | 75 | global $CODOC_SETTINGS; |
| 126 | 76 | add_action( 'admin_menu' ,function(){ |
| @@ -134,9 +84,9 @@ | ||
| 134 | 84 | echo '<form method="post" action="options.php">'; |
| 135 | 85 | settings_fields( 'codoc_option_group' ); |
| 136 | 86 | do_settings_sections( 'codoc' ); |
| 137 | 87 | submit_button(); // 送信ボタン |
| 138 | - echo '</div>'; | |
| 88 | + echo '</form></div>'; | |
| 139 | 89 | } |
| 140 | 90 | ); |
| 141 | 91 | }); |
| 142 | 92 | |
| @@ -145,12 +95,14 @@ | ||
| 145 | 95 | if (isset($_GET['page']) and $_GET['page'] == 'codoc' and isset($_GET['fetch_token_key'])) { |
| 146 | 96 | $key = sanitize_text_field($_GET['fetch_token_key']); |
| 147 | 97 | $usercode = sanitize_text_field($_GET['usercode']); |
| 148 | 98 | update_option(CODOC_USERCODE_OPTION_NAME,$usercode); |
| 149 | - $data = $this->util->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"]); | |
| 150 | 101 | if ($data->status and $token = $data->token) { |
| 151 | 102 | update_option(CODOC_TOKEN_OPTION_NAME,$token); |
| 152 | - $data = $this->util->callAPI('GET',''); | |
| 103 | + //$data = $this->callAPI('GET',''); | |
| 104 | + $data = $this->util->get_user_info([],["usercode" => $usercode, "token" => $token]); | |
| 153 | 105 | if ($data->status and $user = $data->user) { |
| 154 | 106 | update_option(CODOC_AUTHINFO_OPTION_NAME,[ |
| 155 | 107 | 'email' => $user->email, |
| 156 | 108 | 'name' => $user->name, |
| @@ -264,9 +216,9 @@ | ||
| 264 | 216 | function() { |
| 265 | 217 | global $CODOC_USERCODE; |
| 266 | 218 | global $CODOC_TOKEN; |
| 267 | 219 | global $CODOC_AUTHINFO; |
| 268 | - $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';"; | |
| 269 | 221 | |
| 270 | 222 | echo sprintf('<p><font color="green" id="codoc-auth-message"><strong>%s</strong> で認証済</font></p>',$CODOC_AUTHINFO['email']); |
| 271 | 223 | |
| 272 | 224 | echo sprintf('<input id="codoc-usercode" type="hidden" name="%s" value="%s">',CODOC_USERCODE_OPTION_NAME,$CODOC_USERCODE); |
| @@ -302,27 +254,31 @@ | ||
| 302 | 254 | 'setting_section_id' |
| 303 | 255 | ); |
| 304 | 256 | } |
| 305 | 257 | // ない場合かつ自分で認証する場合 |
| 306 | - 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 | + )) { | |
| 307 | 262 | register_setting( |
| 308 | 263 | 'codoc_option_group', // option group |
| 309 | 264 | CODOC_USERCODE_OPTION_NAME |
| 310 | 265 | ); |
| 266 | + register_setting( | |
| 267 | + 'codoc_option_group', // option group | |
| 268 | + CODOC_TOKEN_OPTION_NAME | |
| 269 | + ); | |
| 311 | 270 | add_settings_field( |
| 312 | 271 | 'codoc_usercode', |
| 313 | 272 | 'ユーザーコード', |
| 314 | 273 | function() { |
| 315 | 274 | global $CODOC_USERCODE; |
| 275 | + echo '<input type="hidden" name="auth_by_myself" value="1">'; | |
| 316 | 276 | echo sprintf('<input type="text" name="%s" value="%s">',CODOC_USERCODE_OPTION_NAME,$CODOC_USERCODE); |
| 317 | 277 | }, |
| 318 | 278 | 'codoc', |
| 319 | 279 | 'setting_section_id' |
| 320 | 280 | ); |
| 321 | - register_setting( | |
| 322 | - 'codoc_option_group', // option group | |
| 323 | - CODOC_TOKEN_OPTION_NAME | |
| 324 | - ); | |
| 325 | 281 | add_settings_field( |
| 326 | 282 | 'codoc_token', |
| 327 | 283 | 'APIトークン', |
| 328 | 284 | function() { |
| @@ -345,9 +301,13 @@ | ||
| 345 | 301 | },10,2); |
| 346 | 302 | add_action('updated_option',function(){ |
| 347 | 303 | global $CODOC_RE_AUTHORIZE; |
| 348 | 304 | if ($CODOC_RE_AUTHORIZE) { |
| 349 | - $data = $this->util->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 | + ]); | |
| 350 | 310 | if ($data->status and $user = $data->user) { |
| 351 | 311 | update_option(CODOC_AUTHINFO_OPTION_NAME,[ |
| 352 | 312 | 'email' => $user->email, |
| 353 | 313 | 'name' => $user->name, |
| @@ -370,51 +330,55 @@ | ||
| 370 | 330 | ); |
| 371 | 331 | |
| 372 | 332 | } |
| 373 | 333 | public function add_mce() { |
| 374 | - // Add Shortcode options in the WordPres visual editors | |
| 375 | - //wp_enqueue_script( 'codoc-editor-script', CODOC_URL . 'codoc/src_mce/codoc-editor.js'); | |
| 376 | - | |
| 377 | - add_action( 'init', function() { | |
| 334 | + add_action( 'admin_enqueue_scripts', function() { | |
| 378 | 335 | if ( ! current_user_can( 'edit_posts' ) && ! current_user_can( 'edit_pages' ) ) { |
| 379 | 336 | return; |
| 380 | 337 | } |
| 381 | - if ( get_user_option( 'rich_editing' ) == 'true' ) { | |
| 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' ), | |
| 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' ), | |
| 392 | 355 | |
| 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 | - ); | |
| 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 | + ); | |
| 399 | 362 | |
| 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 | - } | |
| 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 | + } | |
| 417 | 381 | } ); |
| 418 | 382 | } |
| 419 | 383 | public function save_post($post_ID,$post,$update) { |
| 420 | 384 | if (preg_match('/^(auto-draft|inherit)$/',$post->post_status)) { |
| @@ -419,19 +383,25 @@ | ||
| 419 | 383 | public function save_post($post_ID,$post,$update) { |
| 420 | 384 | if (preg_match('/^(auto-draft|inherit)$/',$post->post_status)) { |
| 421 | 385 | return; |
| 422 | 386 | } |
| 387 | + | |
| 423 | 388 | $entryCode = get_post_meta($post_ID,'codoc_entry_code',true); |
| 424 | 389 | $res = $this->util->sync_entry([ |
| 425 | 390 | "post_title" => $post->post_title, |
| 426 | 391 | "post_content" => $post->post_content, |
| 427 | - "post_status" => ($post->post_status == 'publish' ? 1 : 0), # 0, 1 | |
| 392 | + "post_status" => ($post->post_status == 'publish' ? 1 : 0), | |
| 428 | 393 | "post_permalink" => get_permalink($post_ID), |
| 429 | 394 | "codoc_entry_code" => $entryCode, |
| 395 | + ],[ | |
| 396 | + "usercode" => get_option(CODOC_USERCODE_OPTION_NAME), | |
| 397 | + "token" => get_option(CODOC_TOKEN_OPTION_NAME), | |
| 430 | 398 | ]); |
| 399 | + | |
| 431 | 400 | if (is_object($res) and $res->status and !$entryCode) { |
| 432 | 401 | update_post_meta($post_ID,'codoc_entry_code',$res->entry->code); |
| 433 | 402 | } |
| 403 | + | |
| 434 | 404 | return true; |
| 435 | 405 | } |
| 436 | 406 | function updated_post_meta( $meta_ID, $post_ID, $meta_key ) { |
| 437 | 407 | if ($meta_key != '_thumbnail_id') { |
| @@ -440,12 +410,16 @@ | ||
| 440 | 410 | if ( has_post_thumbnail($post_ID) ) { |
| 441 | 411 | $attachment = wp_get_attachment_metadata( get_post_thumbnail_id($post_ID)); |
| 442 | 412 | $upload_dir = wp_upload_dir(); |
| 443 | 413 | $file_path = sprintf ('%s/%s',$upload_dir['basedir'],$attachment['file']); |
| 414 | + | |
| 444 | 415 | return $this->util->post_thumbnail([ |
| 445 | 416 | "file_path" => $file_path, |
| 446 | 417 | "boundary" => wp_generate_password(24), |
| 447 | 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), | |
| 448 | 422 | ]); |
| 449 | 423 | } |
| 450 | 424 | } |
| 451 | 425 | function deleted_post_meta( $meta_ids, $post_ID, $meta_key,$meta_value ) { |
| @@ -451,18 +425,25 @@ | ||
| 451 | 425 | function deleted_post_meta( $meta_ids, $post_ID, $meta_key,$meta_value ) { |
| 452 | 426 | if ($meta_key != '_thumbnail_id') { |
| 453 | 427 | return; |
| 454 | 428 | } |
| 455 | - return $this->util->reset_thumbnail(["codoc_entry_code" => get_post_meta($post_ID,'codoc_entry_code',true)]); | |
| 456 | - } | |
| 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 | + | |
| 457 | 438 | function the_content( $post_content ) { |
| 458 | - if ( is_single() && in_the_loop() && is_main_query() ) { | |
| 459 | - } | |
| 439 | + if ( is_single() && in_the_loop() && is_main_query() ) { | |
| 440 | + } | |
| 460 | 441 | $post = get_post(); |
| 461 | 442 | return $this->util->filter_content([ |
| 462 | 443 | "post_content" => $post_content, |
| 463 | 444 | "preview" => is_preview(), |
| 464 | - "codoc_entry_code" => sprintf('"codoc-entry-%s" ',get_post_meta($post->ID,'codoc_entry_code',true)), | |
| 445 | + "codoc_entry_code" => get_post_meta($post->ID,'codoc_entry_code',true), | |
| 465 | 446 | "codoc_settings" => get_option(CODOC_SETTINGS_OPTION_NAME), |
| 466 | 447 | ]); |
| 467 | 448 | } |
| 468 | 449 | |