| @@ -1,96 +1,451 @@ | ||
| 1 | 1 | <?php |
| 2 | +require_once(plugin_dir_path( __FILE__ ) .'class-codoc-util.php'); | |
| 3 | +final class Codoc { | |
| 2 | 4 | |
| 3 | -final class Codoc { | |
| 5 | + public function __construct() { | |
| 6 | + if (is_admin()) { | |
| 7 | + // setting | |
| 8 | + $this->add_settings(); | |
| 9 | + } | |
| 10 | + | |
| 11 | + // usercode, token はadminページのみDBから取得する | |
| 12 | + $this->util = new CodocUtil(); | |
| 4 | 13 | |
| 5 | - public function register() { | |
| 14 | + // paywall用の本文非表示化とcodocタグへの属性追加 | |
| 15 | + add_filter('the_content',[$this,'the_content']); | |
| 16 | + | |
| 17 | + $auth_info = get_option(CODOC_AUTHINFO_OPTION_NAME); | |
| 6 | 18 | |
| 7 | - if ( is_admin() ) { | |
| 8 | - return $this; | |
| 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 ); | |
| 9 | 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 | + } | |
| 40 | + // tinymce | |
| 41 | + $this->add_mce(); | |
| 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; | |
| 53 | + } | |
| 54 | + | |
| 55 | + public function modifier_script_tag($tag,$handle) { | |
| 56 | + if($handle !== 'codoc-injector-js') { | |
| 57 | + return $tag; | |
| 58 | + } | |
| 59 | + $CODOC_SETTINGS = get_option(CODOC_SETTINGS_OPTION_NAME); | |
| 60 | + $data_css = ''; | |
| 61 | + if ($css_path = $CODOC_SETTINGS['css_path']) { | |
| 62 | + $data_css = sprintf(' data-css="%s" ',$css_path); | |
| 63 | + } | |
| 64 | + //return str_replace(' src=', $data_css . ' defer src=', $tag); | |
| 65 | + return preg_replace('/(src=[^>]+)/',' ${1} ' . $data_css . ' defer',$tag); | |
| 66 | + } | |
| 67 | + public function injector_shortcode($atts) { | |
| 68 | + global $post; | |
| 69 | + ob_start(); | |
| 70 | + require 'views/codoc-injector.php'; | |
| 71 | + return ob_get_clean(); | |
| 72 | + } | |
| 73 | + # settings / 設定関連 | |
| 74 | + public function add_settings() { | |
| 75 | + global $CODOC_SETTINGS; | |
| 76 | + add_action( 'admin_menu' ,function(){ | |
| 77 | + add_options_page( | |
| 78 | + 'codoc の設定', //ページタイトル | |
| 79 | + 'codoc', //設定メニューに表示されるメニュータイトル | |
| 80 | + 'edit_users', //権限 | |
| 81 | + 'codoc', //設定ページのURL。options-general.php?page=codoc | |
| 82 | + function() { | |
| 83 | + echo '<div clas="wrap">'; | |
| 84 | + echo '<form method="post" action="options.php">'; | |
| 85 | + settings_fields( 'codoc_option_group' ); | |
| 86 | + do_settings_sections( 'codoc' ); | |
| 87 | + submit_button(); // 送信ボタン | |
| 88 | + echo '</form></div>'; | |
| 89 | + } | |
| 90 | + ); | |
| 91 | + }); | |
| 10 | 92 | |
| 11 | - add_action( 'wp_enqueue_scripts', function() { | |
| 12 | - wp_enqueue_script( 'codoc-injector-js', CODOC_URL . '/js/paywall.js' ); | |
| 13 | - }); | |
| 14 | - add_filter('script_loader_tag', function($tag, $handle) { | |
| 15 | - if($handle !== 'codoc-injector-js') { | |
| 16 | - return $tag; | |
| 17 | - } | |
| 18 | - return str_replace(' src=', ' defer src=', $tag); | |
| 19 | - }, 10, 2); | |
| 20 | - add_shortcode('codoc', [$this, 'injector_shortcode']); | |
| 93 | + add_action( "admin_init", function() { | |
| 94 | + // codocからの認証データ処理 | |
| 95 | + if (isset($_GET['page']) and $_GET['page'] == 'codoc' and isset($_GET['fetch_token_key'])) { | |
| 96 | + $key = sanitize_text_field($_GET['fetch_token_key']); | |
| 97 | + $usercode = sanitize_text_field($_GET['usercode']); | |
| 98 | + update_option(CODOC_USERCODE_OPTION_NAME,$usercode); | |
| 99 | + //$data = $this->callAPI('GET','/token',[ "fetch_token_key" => $key ]); | |
| 100 | + $data = $this->util->get_token([ "fetch_token_key" => $key ],["usercode" => $usercode, "token" => "1"]); | |
| 101 | + if ($data->status and $token = $data->token) { | |
| 102 | + update_option(CODOC_TOKEN_OPTION_NAME,$token); | |
| 103 | + //$data = $this->callAPI('GET',''); | |
| 104 | + $data = $this->util->get_user_info([],["usercode" => $usercode, "token" => $token]); | |
| 105 | + if ($data->status and $user = $data->user) { | |
| 106 | + update_option(CODOC_AUTHINFO_OPTION_NAME,[ | |
| 107 | + 'email' => $user->email, | |
| 108 | + 'name' => $user->name, | |
| 109 | + ]); | |
| 110 | + } | |
| 111 | + #$current_url = (empty($_SERVER['HTTPS']) ? 'http://' : 'https://').$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; | |
| 112 | + #$current_url = preg_replace('/(.*)fetch_token_key.*/','${1}&codoc_auth_finished=1',$current_url); | |
| 113 | + //add_settings_error( 'general', 'settings_updated', __( 'OK' ), 'success' ); | |
| 114 | + $current_url = admin_url('options-general.php') . '?page=codoc&codoc_auth_finished=1'; | |
| 115 | + | |
| 116 | + wp_redirect( $current_url); | |
| 117 | + exit; | |
| 118 | + } | |
| 119 | + } | |
| 21 | 120 | |
| 22 | - return $this; | |
| 23 | - } | |
| 121 | + global $CODOC_USERCODE; | |
| 122 | + global $CODOC_SETTINGS; | |
| 123 | + global $CODOC_TOKEN; | |
| 124 | + global $CODOC_AUTHINFO; | |
| 125 | + $CODOC_USERCODE = get_option(CODOC_USERCODE_OPTION_NAME); | |
| 126 | + $CODOC_SETTINGS = get_option(CODOC_SETTINGS_OPTION_NAME); | |
| 127 | + $CODOC_TOKEN = get_option(CODOC_TOKEN_OPTION_NAME); | |
| 128 | + $CODOC_AUTHINFO = get_option(CODOC_AUTHINFO_OPTION_NAME); | |
| 129 | + if( !$CODOC_SETTINGS ) { | |
| 130 | + //デフォルト値 | |
| 131 | + $CODOC_SETTINGS = array( | |
| 132 | + 'css_path' => '', | |
| 133 | + ); | |
| 134 | + update_option( CODOC_SETTINGS_OPTION_NAME, $CODOC_SETTINGS ); | |
| 135 | + } | |
| 136 | + if (!isset($CODOC_SETTINGS['str_replace_binded_url_from'])) { | |
| 137 | + $CODOC_SETTINGS['str_replace_binded_url_from'] = ''; | |
| 138 | + } | |
| 139 | + if (!isset($CODOC_SETTINGS['str_replace_binded_url_to'])) { | |
| 140 | + $CODOC_SETTINGS['str_replace_binded_url_to'] = ''; | |
| 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 | + } | |
| 148 | + | |
| 149 | + add_settings_section( | |
| 150 | + 'setting_section_id', // id | |
| 151 | + 'codoc 設定ページ', // title | |
| 152 | + function (){}, // callback | |
| 153 | + 'codoc' // page | |
| 154 | + ); | |
| 155 | + // 認証がある場合 | |
| 156 | + if ($CODOC_AUTHINFO) { | |
| 157 | + register_setting( | |
| 158 | + 'codoc_option_group', // option group | |
| 159 | + CODOC_SETTINGS_OPTION_NAME // option name(DB) | |
| 160 | + ); | |
| 161 | + add_settings_field( | |
| 162 | + 'css_path', // id | |
| 163 | + 'カスタマイズ用CSSパス', // title | |
| 164 | + function() { | |
| 165 | + global $CODOC_SETTINGS; | |
| 166 | + echo sprintf('<input type="text" name="%s[css_path]" value="%s">',CODOC_SETTINGS_OPTION_NAME,$CODOC_SETTINGS['css_path']); | |
| 167 | + }, | |
| 168 | + 'codoc', //page | |
| 169 | + 'setting_section_id' //Section | |
| 170 | + ); | |
| 171 | + add_settings_field( | |
| 172 | + 'str_replace_binded_url', // id | |
| 173 | + 'codoc側に登録するパーマリンクを置換', // title | |
| 174 | + function() { | |
| 175 | + global $CODOC_SETTINGS; | |
| 176 | + 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']); | |
| 177 | + echo ('を'); | |
| 178 | + 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']); | |
| 179 | + echo ('に変換'); | |
| 180 | + }, | |
| 181 | + 'codoc', //page | |
| 182 | + 'setting_section_id' //Section | |
| 183 | + ); | |
| 24 | 184 | |
| 25 | - public function injector_shortcode($atts) { | |
| 26 | - global $post; | |
| 27 | - ob_start(); | |
| 28 | - require 'views/codoc-injector.php'; | |
| 29 | - return ob_get_clean(); | |
| 30 | - } | |
| 31 | - | |
| 32 | - public function add_mce() { | |
| 33 | - // Add Shortcode options in the WordPres visual editors | |
| 34 | - //wp_enqueue_script( 'codoc-editor-script', plugins_url( 'codoc/src_mce/codoc-editor.js', __DIR__ )); | |
| 35 | - add_action( 'init', function() { | |
| 36 | - if ( ! current_user_can( 'edit_posts' ) && ! current_user_can( 'edit_pages' ) ) { | |
| 37 | - return; | |
| 38 | - } | |
| 39 | - if ( get_user_option( 'rich_editing' ) == 'true' ) { | |
| 40 | - // プラグイン内で使うCSRF を生成 | |
| 41 | - $path = plugins_url( 'codoc/src_mce/codoc-editor-onload.js', __DIR__ ); | |
| 42 | - wp_enqueue_script( 'codoc-editor-onload', $path, array('jquery'), '', true ); | |
| 43 | - // I just want to insert this global variables but i don't know how i can do it.. | |
| 44 | - wp_localize_script( | |
| 45 | - 'codoc-editor-onload', | |
| 46 | - 'CODOCEDITOR', | |
| 47 | - array( | |
| 48 | - 'action' => 'codoc_shortcodes', | |
| 49 | - 'nonce' => wp_create_nonce( 'codoc_shortcodes' ) | |
| 50 | - ) | |
| 51 | - ); | |
| 52 | - // ここでtinymceのプラグイン追加 | |
| 53 | - wp_enqueue_style( 'codoc-admin-style', plugins_url( 'codoc/src_mce/codoc-admin.css', __DIR__ ) ); | |
| 54 | - add_filter( 'mce_external_plugins', function( $plugin_array ) { | |
| 55 | - $plugin_array['codoc'] = plugins_url( 'codoc/src_mce/codoc-editor.js', __DIR__ ); | |
| 56 | - return $plugin_array; | |
| 57 | - } ); | |
| 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 | + ); | |
| 58 | 199 | |
| 59 | - add_filter( 'mce_buttons', function( $buttons ) { | |
| 60 | - array_push( $buttons, "|", "codoc" ); | |
| 200 | + register_setting( | |
| 201 | + 'codoc_option_group', // option group | |
| 202 | + CODOC_USERCODE_OPTION_NAME | |
| 203 | + ); | |
| 204 | + register_setting( | |
| 205 | + 'codoc_option_group', // option group | |
| 206 | + CODOC_TOKEN_OPTION_NAME | |
| 207 | + ); | |
| 208 | + | |
| 209 | + if (isset($_GET['codoc_auth_finished']) and $_GET['codoc_auth_finished']) { | |
| 210 | + add_settings_error( 'general', 'settings_updated', __( 'codocの認証が完了しました' ), 'success' ); | |
| 211 | + } | |
| 212 | + | |
| 213 | + add_settings_field( | |
| 214 | + 'codoc_auth', | |
| 215 | + '認証', | |
| 216 | + function() { | |
| 217 | + global $CODOC_USERCODE; | |
| 218 | + global $CODOC_TOKEN; | |
| 219 | + global $CODOC_AUTHINFO; | |
| 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';"; | |
| 61 | 221 | |
| 62 | - return $buttons; | |
| 63 | - } ); | |
| 64 | - | |
| 65 | - add_action( 'wp_ajax_codoc_shortcodes', function(){ | |
| 66 | - check_ajax_referer( 'codoc_shortcodes' ); | |
| 67 | - if ($usercode = sanitize_text_field($_GET['usercode'])) { | |
| 68 | - update_option('codoc_usercode',$usercode); | |
| 69 | - } | |
| 70 | - $usercode = get_option(CODOC_USERCODE_OPTION_NAME) ? get_option(CODOC_USERCODE_OPTION_NAME) : 'nocode'; | |
| 71 | - $uri = CODOC_URL . '/api/v1/paywall/' . $usercode . '/entries'; | |
| 222 | + echo sprintf('<p><font color="green" id="codoc-auth-message"><strong>%s</strong> で認証済</font></p>',$CODOC_AUTHINFO['email']); | |
| 223 | + | |
| 224 | + echo sprintf('<input id="codoc-usercode" type="hidden" name="%s" value="%s">',CODOC_USERCODE_OPTION_NAME,$CODOC_USERCODE); | |
| 225 | + echo sprintf('<input id="codoc-token" type="hidden" name="%s" value="%s">',CODOC_TOKEN_OPTION_NAME,$CODOC_TOKEN); | |
| 226 | + echo sprintf('<p>認証を<a href="javascript:void(0);" onClick="' . $script . '">解除する</p>'); | |
| 227 | + }, | |
| 228 | + 'codoc', | |
| 229 | + 'setting_section_id' | |
| 230 | + ); | |
| 231 | + | |
| 232 | + } | |
| 233 | + | |
| 234 | + // ない場合 | |
| 235 | + if (!$CODOC_AUTHINFO and !isset($_GET['auth_by_myself'])) { | |
| 236 | + add_settings_field( | |
| 237 | + 'codoc_auth', | |
| 238 | + '認証', | |
| 239 | + function() { | |
| 240 | + | |
| 241 | + //$current_url = (empty($_SERVER['HTTPS']) ? 'http://' : 'https://').$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; | |
| 242 | + $current_url = admin_url('options-general.php') . '?page=codoc'; | |
| 243 | + $login_url = sprintf("location.href='%s'",CODOC_URL . '/me/token?from=wp&return_url=' . urlencode($current_url)); | |
| 244 | + $register_url = sprintf("location.href='%s'",CODOC_URL . '/register?from=wp&return_url=' . urlencode($current_url)); | |
| 245 | + // submitを消しておく | |
| 246 | + echo ('<script type="text/javascript">window.onload=function(){document.getElementById(\'submit\').style.display = \'none\'}</script>'); | |
| 247 | + echo ('codocをWordPressでご利用いただくには認証が必要です。<br />'); | |
| 248 | + echo sprintf('ユーザーコードとAPIトークンを<a href="%s&auth_by_myself=1">直接入力</a>することでも認証できます。<br /><br />',$current_url); | |
| 249 | + 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); | |
| 250 | + | |
| 251 | + | |
| 252 | + }, | |
| 253 | + 'codoc', | |
| 254 | + 'setting_section_id' | |
| 255 | + ); | |
| 256 | + } | |
| 257 | + // ない場合かつ自分で認証する場合 | |
| 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 | + )) { | |
| 262 | + register_setting( | |
| 263 | + 'codoc_option_group', // option group | |
| 264 | + CODOC_USERCODE_OPTION_NAME | |
| 265 | + ); | |
| 266 | + register_setting( | |
| 267 | + 'codoc_option_group', // option group | |
| 268 | + CODOC_TOKEN_OPTION_NAME | |
| 269 | + ); | |
| 270 | + add_settings_field( | |
| 271 | + 'codoc_usercode', | |
| 272 | + 'ユーザーコード', | |
| 273 | + function() { | |
| 274 | + global $CODOC_USERCODE; | |
| 275 | + echo '<input type="hidden" name="auth_by_myself" value="1">'; | |
| 276 | + echo sprintf('<input type="text" name="%s" value="%s">',CODOC_USERCODE_OPTION_NAME,$CODOC_USERCODE); | |
| 277 | + }, | |
| 278 | + 'codoc', | |
| 279 | + 'setting_section_id' | |
| 280 | + ); | |
| 281 | + add_settings_field( | |
| 282 | + 'codoc_token', | |
| 283 | + 'APIトークン', | |
| 284 | + function() { | |
| 285 | + global $CODOC_TOKEN; | |
| 286 | + echo sprintf('<input type="text" name="%s" value="%s">',CODOC_TOKEN_OPTION_NAME,$CODOC_TOKEN); | |
| 287 | + }, | |
| 288 | + 'codoc', | |
| 289 | + 'setting_section_id' | |
| 290 | + ); | |
| 291 | + } | |
| 292 | + | |
| 293 | + // 認証情報を保存 | |
| 294 | + add_action( 'update_option_' . CODOC_USERCODE_OPTION_NAME, function( $old_value, $new_value ) { | |
| 295 | + global $CODOC_RE_AUTHORIZE; | |
| 296 | + $CODOC_RE_AUTHORIZE = 1; | |
| 297 | + },9,2); // $hook, $function_to_add, $priority, $accepted_args | |
| 298 | + add_action( 'update_option_' . CODOC_TOKEN_OPTION_NAME, function( $old_value, $new_value ) { | |
| 299 | + global $CODOC_RE_AUTHORIZE; | |
| 300 | + $CODOC_RE_AUTHORIZE = 1; | |
| 301 | + },10,2); | |
| 302 | + add_action('updated_option',function(){ | |
| 303 | + global $CODOC_RE_AUTHORIZE; | |
| 304 | + if ($CODOC_RE_AUTHORIZE) { | |
| 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 | + ]); | |
| 310 | + if ($data->status and $user = $data->user) { | |
| 311 | + update_option(CODOC_AUTHINFO_OPTION_NAME,[ | |
| 312 | + 'email' => $user->email, | |
| 313 | + 'name' => $user->name, | |
| 314 | + ]); | |
| 315 | + } else { | |
| 316 | + update_option(CODOC_AUTHINFO_OPTION_NAME,''); | |
| 317 | + } | |
| 318 | + } | |
| 319 | + }); | |
| 320 | + | |
| 321 | + }); | |
| 322 | + // プラグイン一覧に設定のリンクをいれる | |
| 323 | + add_filter( 'plugin_action_links_' . plugin_basename( plugin_dir_path( __FILE__ ) . 'codoc' . '.php' ), | |
| 324 | + function( $links ) { | |
| 325 | + $setting_link = sprintf( '<a href="%s">%s</a>', esc_url( add_query_arg( 'page', 'codoc', admin_url( 'options-general.php' ) ) ), esc_html( '設定' ) ); | |
| 326 | + array_unshift( $links, $setting_link ); | |
| 327 | + | |
| 328 | + return $links; | |
| 329 | + } | |
| 330 | + ); | |
| 331 | + | |
| 332 | + } | |
| 333 | + public function add_mce() { | |
| 334 | + add_action( 'admin_enqueue_scripts', function() { | |
| 335 | + if ( ! current_user_can( 'edit_posts' ) && ! current_user_can( 'edit_pages' ) ) { | |
| 336 | + return; | |
| 337 | + } | |
| 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' ), | |
| 355 | + | |
| 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 | + ); | |
| 362 | + | |
| 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' ); | |
| 72 | 368 | |
| 73 | - if ($entrycode = sanitize_text_field($_GET['entrycode'])) { | |
| 74 | - $uri = $uri . '/' . $entrycode; | |
| 75 | - } | |
| 76 | - $args = preg_match('/localhost/',CODOC_URL) ? ['sslverify' => false ] : []; | |
| 77 | - $response = wp_remote_request( $uri, $args ); | |
| 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 | + } ); | |
| 78 | 378 | |
| 79 | - if ($response instanceof WP_Error) { | |
| 80 | - exit; | |
| 81 | - } | |
| 82 | - | |
| 83 | - $response['body'] = json_decode($response['body'], true); | |
| 84 | - $entries = $response['body']['entries']; | |
| 85 | - | |
| 86 | - require 'views/codoc-shortcodes-list.php'; | |
| 87 | - | |
| 88 | - wp_die(); | |
| 89 | - } ); | |
| 379 | + } | |
| 90 | 380 | } |
| 91 | - } ); | |
| 381 | + } ); | |
| 382 | + } | |
| 383 | + public function save_post($post_ID,$post,$update) { | |
| 384 | + if (preg_match('/^(auto-draft|inherit)$/',$post->post_status)) { | |
| 385 | + return; | |
| 386 | + } | |
| 387 | + | |
| 388 | + $entryCode = get_post_meta($post_ID,'codoc_entry_code',true); | |
| 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); | |
| 402 | + } | |
| 403 | + | |
| 404 | + return true; | |
| 405 | + } | |
| 406 | + function updated_post_meta( $meta_ID, $post_ID, $meta_key ) { | |
| 407 | + if ($meta_key != '_thumbnail_id') { | |
| 408 | + return; | |
| 409 | + } | |
| 410 | + if ( has_post_thumbnail($post_ID) ) { | |
| 411 | + $attachment = wp_get_attachment_metadata( get_post_thumbnail_id($post_ID)); | |
| 412 | + $upload_dir = wp_upload_dir(); | |
| 413 | + $file_path = sprintf ('%s/%s',$upload_dir['basedir'],$attachment['file']); | |
| 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 | + ]); | |
| 423 | + } | |
| 424 | + } | |
| 425 | + function deleted_post_meta( $meta_ids, $post_ID, $meta_key,$meta_value ) { | |
| 426 | + if ($meta_key != '_thumbnail_id') { | |
| 427 | + return; | |
| 428 | + } | |
| 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 | + } | |
| 92 | 437 | |
| 438 | + function the_content( $post_content ) { | |
| 439 | + if ( is_single() && in_the_loop() && is_main_query() ) { | |
| 440 | + } | |
| 441 | + $post = get_post(); | |
| 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 | + ]); | |
| 448 | + } | |
| 449 | + | |
| 450 | +} | |
| 93 | 451 | |
| 94 | - } | |
| 95 | - | |
| 96 | -} | |