| @@ -1,25 +1,45 @@ | ||
| 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 | + } | |
| 4 | 10 | |
| 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 | - } | |
| 11 | + // usercode, token はadminページのみDBから取得する | |
| 12 | + $this->util = new CodocUtil([ 'usercode' => null, 'token' => null, 'codoc_url' => $this->get_codoc_url() ]); | |
| 13 | + | |
| 14 | + # the_content フィルタを実行しない状態 | |
| 15 | + $this->do_not_filter_the_content = false; | |
| 14 | 16 | |
| 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()) { | |
| 17 | + // paywall用の本文非表示化とcodocタグへの属性追加 | |
| 18 | + #$priority = 999999999; | |
| 19 | + $priority = 100000; # フィルターは最後に実施する default 10 | |
| 20 | + $CODOC_SETTINGS = get_option(CODOC_SETTINGS_OPTION_NAME); | |
| 21 | + if (isset($CODOC_SETTINGS["debug_params"])) { | |
| 22 | + $params_decoded = json_decode($CODOC_SETTINGS["debug_params"],true); | |
| 23 | + if (isset($params_decoded["the_content_filter_priority"])) { | |
| 24 | + $priority = $params_decoded["the_content_filter_priority"]; | |
| 25 | + } | |
| 26 | + } | |
| 27 | + add_filter('the_content',[$this,'the_content'],$priority); | |
| 28 | + | |
| 29 | + // 2020-07-25 excerpt対応 | |
| 30 | + add_filter('excerpt_allowed_blocks',[$this,'excerpt_allowed_blocks']); | |
| 31 | + | |
| 32 | + $auth_info = get_option(CODOC_AUTHINFO_OPTION_NAME); | |
| 33 | + | |
| 34 | + if ($auth_info) { | |
| 35 | + // データ同期 (save_postはis_adminで実行されないケースがある) | |
| 36 | + add_action( 'save_post', [$this,'save_post'], 20, 3 ); | |
| 37 | + add_action( 'added_post_meta', [$this,'updated_post_meta'], 10, 3 ); | |
| 38 | + add_action( 'updated_post_meta',[$this,'updated_post_meta'], 10, 3 ); | |
| 39 | + add_action( 'deleted_post_meta',[$this,'deleted_post_meta'], 10, 4 ); | |
| 40 | + } | |
| 41 | + if (is_admin() and $auth_info) { //管理画面 | |
| 22 | 42 | // Gutenberg のサポートがない場合は何もしない |
| 23 | 43 | if ( function_exists( 'register_block_type' ) ) { |
| 24 | 44 | // gutenberg |
| 25 | 45 | require_once 'src/init.php'; |
| @@ -24,9 +44,9 @@ | ||
| 24 | 44 | // gutenberg |
| 25 | 45 | require_once 'src/init.php'; |
| 26 | 46 | add_action('wp_loaded', function() { |
| 27 | 47 | wp_localize_script('wordpress-cgb-block-js', 'OPTIONS', array( |
| 28 | - 'codoc_url' => CODOC_URL, | |
| 48 | + 'codoc_url' => $this->get_codoc_url(), | |
| 29 | 49 | 'codoc_usercode' => get_option(CODOC_USERCODE_OPTION_NAME), |
| 30 | 50 | 'codoc_plugin_version' => CODOC_PLUGIN_VERSION, |
| 31 | 51 | 'codoc_sdk_path' => CODOC_SDK_PATH, |
| 32 | 52 | )); |
| @@ -31,530 +51,882 @@ | ||
| 31 | 51 | 'codoc_sdk_path' => CODOC_SDK_PATH, |
| 32 | 52 | )); |
| 33 | 53 | }); |
| 34 | 54 | } |
| 35 | - // tinymce | |
| 36 | - $this->add_mce(); | |
| 37 | - | |
| 38 | - return $this; | |
| 39 | - } | |
| 40 | - | |
| 41 | - // codocのJS登録 | |
| 55 | + // tinymce | |
| 56 | + $this->add_mce(); | |
| 57 | + } else { // ブログ画面 | |
| 58 | + // codocのJS登録 | |
| 59 | + add_action( 'wp_enqueue_scripts', function() { | |
| 60 | + $CODOC_SETTINGS = get_option(CODOC_SETTINGS_OPTION_NAME); | |
| 61 | + $load_script_condition = ''; | |
| 62 | + if (isset($CODOC_SETTINGS["debug_params"]) and | |
| 63 | + $params_decoded = json_decode($CODOC_SETTINGS["debug_params"],true) and | |
| 64 | + isset($params_decoded["load_script_condition"]) | |
| 65 | + ) { | |
| 66 | + $load_script_condition = $params_decoded["load_script_condition"]; | |
| 67 | + } | |
| 68 | + if ($load_script_condition == 'not_list_page') { | |
| 69 | + // not_list_pageの場合はトップページ等でスクリプトをロードしない | |
| 70 | + if (!(is_archive() or is_category() or is_home() or is_front_page())) { | |
| 71 | + wp_enqueue_script( 'codoc-injector-js', $this->get_codoc_url() . '/js/cms.js' ); | |
| 72 | + } | |
| 73 | + } else { | |
| 74 | + wp_enqueue_script( 'codoc-injector-js', $this->get_codoc_url() . '/js/cms.js' ); | |
| 75 | + } | |
| 76 | + }); | |
| 77 | + //登録したscriptタグに属性をつける | |
| 78 | + add_filter('script_loader_tag', [$this,'modifier_script_tag'],10,2); | |
| 79 | + // tinymce用のショートコード | |
| 80 | + add_shortcode('codoc', [$this, 'injector_shortcode']); | |
| 81 | + // テーマをbodyにも反映 | |
| 82 | + add_filter('body_class', function($classes) { | |
| 83 | + $CODOC_SETTINGS = get_option(CODOC_SETTINGS_OPTION_NAME); | |
| 84 | + $css_path = 'blue'; | |
| 85 | + if (isset($CODOC_SETTINGS["css_path"]) and $CODOC_SETTINGS["css_path"]) { | |
| 86 | + $css_path = $CODOC_SETTINGS["css_path"]; | |
| 87 | + } | |
| 88 | + array_push($classes,sprintf( "codoc-theme-%s",$css_path)); | |
| 89 | + return $classes; | |
| 90 | + }); | |
| 91 | + | |
| 92 | + } | |
| 93 | + return $this; | |
| 94 | + } | |
| 95 | + public function get_codoc_url() { | |
| 96 | + $CODOC_SETTINGS = get_option(CODOC_SETTINGS_OPTION_NAME); | |
| 97 | + if (isset($CODOC_SETTINGS["debug_params"])) { | |
| 98 | + $params_decoded = json_decode($CODOC_SETTINGS["debug_params"],true); | |
| 99 | + if (isset($params_decoded["codoc_url"])) { | |
| 100 | + return $params_decoded["codoc_url"]; | |
| 101 | + } | |
| 102 | + } | |
| 103 | + return CODOC_URL; | |
| 104 | + } | |
| 105 | + public function modifier_script_tag($tag,$handle) { | |
| 106 | + if($handle !== 'codoc-injector-js') { | |
| 107 | + return $tag; | |
| 108 | + } | |
| 109 | + $CODOC_SETTINGS = get_option(CODOC_SETTINGS_OPTION_NAME); | |
| 110 | + $data_css = ''; | |
| 111 | + if ($css_path = $CODOC_SETTINGS['css_path']) { | |
| 112 | + $data_css = sprintf(' data-css="%s" ',$css_path); | |
| 113 | + } | |
| 114 | + // connect用パラメータ | |
| 115 | + $connect_attributes = ''; | |
| 116 | + if (isset($CODOC_SETTINGS["codoc_connect_code"])) { | |
| 117 | + $connect_code = $CODOC_SETTINGS["codoc_connect_code"]; | |
| 118 | + $connect_attributes = sprintf(' data-connect-code="%s"',$connect_code); | |
| 119 | + $tag = preg_replace('/cms\.js/','cms-connect.js',$tag); | |
| 120 | + } | |
| 121 | + if (isset($CODOC_SETTINGS["codoc_connect_registration_mode"]) and $CODOC_SETTINGS["codoc_connect_registration_mode"]) { | |
| 122 | + $connect_attributes = $connect_attributes . | |
| 123 | + sprintf(' data-connect-registration-mode="%s"',$CODOC_SETTINGS["codoc_connect_registration_mode"]); | |
| 124 | + } | |
| 42 | 125 | |
| 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']); | |
| 126 | + $usercode_attributes = ''; | |
| 127 | + $user_code = get_option(CODOC_USERCODE_OPTION_NAME); | |
| 128 | + if ($user_code) { | |
| 129 | + $usercode_attributes = sprintf(' data-usercode="%s"',$user_code); | |
| 130 | + } | |
| 50 | 131 | |
| 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 | - } | |
| 132 | + $setting_attributes = ''; | |
| 133 | + if ($CODOC_SETTINGS['codoc_script_tag_attributes']) { | |
| 134 | + $setting_attributes = $CODOC_SETTINGS['codoc_script_tag_attributes']; | |
| 135 | + } | |
| 65 | 136 | //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); | |
| 137 | + return preg_replace('/(src=[^>]+)/',' ${1} ' . $data_css . $connect_attributes . $usercode_attributes . $setting_attributes . ' defer',$tag); | |
| 138 | + } | |
| 139 | + public function injector_shortcode($atts) { | |
| 140 | + global $post; | |
| 141 | + ob_start(); | |
| 142 | + require 'views/codoc-injector.php'; | |
| 143 | + return ob_get_clean(); | |
| 144 | + } | |
| 145 | + # settings / 設定関連 | |
| 146 | + public function add_settings() { | |
| 147 | + global $CODOC_SETTINGS; | |
| 148 | + add_action( 'admin_menu' ,function(){ | |
| 149 | + add_options_page( | |
| 150 | + 'codoc の設定', //ページタイトル | |
| 151 | + 'codoc', //設定メニューに表示されるメニュータイトル | |
| 152 | + 'edit_users', //権限 | |
| 153 | + 'codoc', //設定ページのURL。options-general.php?page=codoc | |
| 154 | + function() { | |
| 155 | + echo '<div class="codoc-settings">'; | |
| 156 | + echo '<form method="post" action="options.php">'; | |
| 157 | + settings_fields( 'codoc_option_group' ); | |
| 158 | + do_settings_sections( 'codoc' ); | |
| 159 | + submit_button(); // 送信ボタン | |
| 160 | + echo '</form></div>'; | |
| 161 | + } | |
| 162 | + ); | |
| 163 | + }); | |
| 77 | 164 | |
| 78 | - $headers['X-CodocToken'] = $token; | |
| 79 | - $host = CODOC_URL; | |
| 80 | - $sslverify = true; | |
| 165 | + add_action('admin_print_styles', 'codoc_admin_styles'); | |
| 166 | + function codoc_admin_styles($hook) { | |
| 167 | + wp_enqueue_style('codoc-options-style', plugins_url( 'codoc/css/codoc-options.css', __DIR__ )); | |
| 168 | + } | |
| 169 | + add_action( "admin_init", function() { | |
| 170 | + // codocからの認証データ処理 | |
| 171 | + if (isset($_GET['page']) and $_GET['page'] == 'codoc' and isset($_GET['fetch_token_key'])) { | |
| 172 | + $key = sanitize_text_field($_GET['fetch_token_key']); | |
| 173 | + $usercode = sanitize_text_field($_GET['usercode']); | |
| 174 | + update_option(CODOC_USERCODE_OPTION_NAME,$usercode); | |
| 175 | + //$data = $this->callAPI('GET','/token',[ "fetch_token_key" => $key ]); | |
| 176 | + $data = $this->util->get_token([ "fetch_token_key" => $key ],["usercode" => $usercode, "token" => "1"]); | |
| 177 | + if ($data->status and $token = $data->token) { | |
| 178 | + update_option(CODOC_TOKEN_OPTION_NAME,$token); | |
| 179 | + //$data = $this->callAPI('GET',''); | |
| 180 | + $data = $this->util->get_user_info([],["usercode" => $usercode, "token" => $token]); | |
| 181 | + if ($data->status and $user = $data->user) { | |
| 182 | + $this->update_codoc_authinfo($user); | |
| 183 | + } | |
| 184 | + #$current_url = (empty($_SERVER['HTTPS']) ? 'http://' : 'https://').$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; | |
| 185 | + #$current_url = preg_replace('/(.*)fetch_token_key.*/','${1}&codoc_auth_finished=1',$current_url); | |
| 186 | + //add_settings_error( 'general', 'settings_updated', __( 'OK' ), 'success' ); | |
| 187 | + $current_url = admin_url('options-general.php') . '?page=codoc&codoc_auth_finished=1'; | |
| 81 | 188 | |
| 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 | - }); | |
| 189 | + wp_redirect( $current_url); | |
| 190 | + exit; | |
| 191 | + } | |
| 192 | + } | |
| 193 | + global $CODOC_USERCODE; | |
| 194 | + global $CODOC_SETTINGS; | |
| 195 | + global $CODOC_TOKEN; | |
| 196 | + global $CODOC_AUTHINFO; | |
| 197 | + $CODOC_USERCODE = get_option(CODOC_USERCODE_OPTION_NAME); | |
| 198 | + $CODOC_SETTINGS = get_option(CODOC_SETTINGS_OPTION_NAME); | |
| 199 | + $CODOC_TOKEN = get_option(CODOC_TOKEN_OPTION_NAME); | |
| 200 | + $CODOC_AUTHINFO = get_option(CODOC_AUTHINFO_OPTION_NAME); | |
| 201 | + if( !$CODOC_SETTINGS ) { | |
| 202 | + //デフォルト値 | |
| 203 | + $CODOC_SETTINGS = array( | |
| 204 | + 'css_path' => '', | |
| 205 | + ); | |
| 206 | + update_option( CODOC_SETTINGS_OPTION_NAME, $CODOC_SETTINGS ); | |
| 207 | + } | |
| 208 | + if (!isset($CODOC_SETTINGS['str_replace_binded_url_from'])) { | |
| 209 | + $CODOC_SETTINGS['str_replace_binded_url_from'] = ''; | |
| 210 | + } | |
| 211 | + if (!isset($CODOC_SETTINGS['str_replace_binded_url_to'])) { | |
| 212 | + $CODOC_SETTINGS['str_replace_binded_url_to'] = ''; | |
| 213 | + } | |
| 214 | + if (!isset($CODOC_SETTINGS['str_before_codoc_tag'])) { | |
| 215 | + $CODOC_SETTINGS['str_before_codoc_tag'] = ''; | |
| 216 | + } | |
| 217 | + if (!isset($CODOC_SETTINGS['str_after_codoc_tag'])) { | |
| 218 | + $CODOC_SETTINGS['str_after_codoc_tag'] = ''; | |
| 219 | + } | |
| 135 | 220 | |
| 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 | - } | |
| 221 | + if (!isset($CODOC_SETTINGS['always_show_support'])) { | |
| 222 | + $CODOC_SETTINGS['always_show_support'] = '0'; | |
| 223 | + } | |
| 224 | + if (!isset($CODOC_SETTINGS['show_support_message'])) { | |
| 225 | + $CODOC_SETTINGS['show_support_message'] = ''; | |
| 226 | + } | |
| 227 | + if (!isset($CODOC_SETTINGS['show_support_categories'])) { | |
| 228 | + $CODOC_SETTINGS['show_support_categories'] = ''; | |
| 229 | + } | |
| 230 | + if (!isset($CODOC_SETTINGS['show_support_location'])) { | |
| 231 | + $CODOC_SETTINGS['show_support_location'] = 'bottom'; | |
| 232 | + } | |
| 233 | + if (!isset($CODOC_SETTINGS['do_not_filter_the_content'])) { | |
| 234 | + $CODOC_SETTINGS['do_not_filter_the_content'] = '0'; | |
| 235 | + } | |
| 161 | 236 | |
| 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 | - ); | |
| 237 | + if (!isset($CODOC_SETTINGS['entry_button_text'])) { | |
| 238 | + $CODOC_SETTINGS['entry_button_text'] = ''; | |
| 239 | + } | |
| 240 | + if (!isset($CODOC_SETTINGS['subscription_button_text'])) { | |
| 241 | + $CODOC_SETTINGS['subscription_button_text'] = ''; | |
| 242 | + } | |
| 243 | + if (!isset($CODOC_SETTINGS['support_button_text'])) { | |
| 244 | + $CODOC_SETTINGS['support_button_text'] = ''; | |
| 245 | + } | |
| 246 | + if (!isset($CODOC_SETTINGS['subscription_message'])) { | |
| 247 | + $CODOC_SETTINGS['subscription_message'] = ''; | |
| 248 | + } | |
| 249 | + if (!isset($CODOC_SETTINGS['support_message'])) { | |
| 250 | + $CODOC_SETTINGS['support_message'] = ''; | |
| 251 | + } | |
| 252 | + if (!isset($CODOC_SETTINGS['show_like'])) { | |
| 253 | + $CODOC_SETTINGS['show_like'] = '1'; | |
| 254 | + } | |
| 255 | + if (!isset($CODOC_SETTINGS['show_about_codoc'])) { | |
| 256 | + $CODOC_SETTINGS['show_about_codoc'] = '1'; | |
| 257 | + } | |
| 258 | + if (!isset($CODOC_SETTINGS['show_powered_by'])) { | |
| 259 | + $CODOC_SETTINGS['show_powered_by'] = '1'; | |
| 260 | + } | |
| 225 | 261 | |
| 226 | - add_settings_field( | |
| 227 | - 'str_around_codoc_tag', // id | |
| 228 | - 'codocタグの前後にHTMLを挿入', // title | |
| 229 | - function() { | |
| 230 | - global $CODOC_SETTINGS; | |
| 231 | - echo sprintf('<textarea name="%s[str_before_codoc_tag]" cols="40">%s</textarea>',CODOC_SETTINGS_OPTION_NAME,$CODOC_SETTINGS['str_before_codoc_tag']); | |
| 232 | - echo ('を前に挿入<br />'); | |
| 233 | - echo sprintf('<textarea name="%s[str_after_codoc_tag]" cols="40">%s</textarea>',CODOC_SETTINGS_OPTION_NAME,$CODOC_SETTINGS['str_after_codoc_tag']); | |
| 234 | - echo ('を後に挿入'); | |
| 235 | - | |
| 236 | - }, | |
| 237 | - 'codoc', //page | |
| 238 | - 'setting_section_id' //Section | |
| 239 | - ); | |
| 262 | + if (!isset($CODOC_SETTINGS['codoc_tag_attributes'])) { | |
| 263 | + $CODOC_SETTINGS['codoc_tag_attributes'] = ''; | |
| 264 | + } | |
| 265 | + if (!isset($CODOC_SETTINGS['codoc_script_tag_attributes'])) { | |
| 266 | + $CODOC_SETTINGS['codoc_script_tag_attributes'] = ''; | |
| 267 | + } | |
| 268 | + if (!isset($CODOC_SETTINGS['codoc_connect_code'])) { | |
| 269 | + $CODOC_SETTINGS['codoc_connect_code'] = ''; | |
| 270 | + } | |
| 271 | + if (!isset($CODOC_SETTINGS['codoc_connect_registration_mode'])) { | |
| 272 | + $CODOC_SETTINGS['codoc_connect_registration_mode'] = ''; | |
| 273 | + } | |
| 274 | + if (!isset($CODOC_SETTINGS['debug_params'])) { | |
| 275 | + $CODOC_SETTINGS['debug_params'] = ''; | |
| 276 | + } | |
| 277 | + add_settings_section( | |
| 278 | + 'setting_section_id', // id | |
| 279 | + 'codoc 設定ページ', // title | |
| 280 | + function (){}, // callback | |
| 281 | + 'codoc' // page | |
| 282 | + ); | |
| 283 | + // 認証がある場合 | |
| 284 | + if ($CODOC_AUTHINFO) { | |
| 285 | + // クリエイター情報だけ更新 (POST時に同期をとる) | |
| 286 | + if (isset($_GET['page']) and $_GET['page'] == 'codoc' and isset($_GET['settings-updated'])) { | |
| 287 | + $data = $this->util->get_user_info([],[ | |
| 288 | + "usercode" => get_option(CODOC_USERCODE_OPTION_NAME), | |
| 289 | + "token" => get_option(CODOC_TOKEN_OPTION_NAME), | |
| 290 | + ]); | |
| 291 | + if ($data->status and $user = $data->user) { | |
| 292 | + $this->update_codoc_authinfo($user); | |
| 293 | + } | |
| 294 | + } | |
| 295 | + | |
| 296 | + register_setting( | |
| 297 | + 'codoc_option_group', // option group | |
| 298 | + CODOC_SETTINGS_OPTION_NAME // option name(DB) | |
| 299 | + ); | |
| 300 | + add_settings_field( | |
| 301 | + 'css_path', // id | |
| 302 | + 'テーマ', // title | |
| 303 | + function() { | |
| 304 | + global $CODOC_SETTINGS; | |
| 305 | + echo '<div class="excerpt">テーマを指定してペイウォールのデザインを変えることができます。<br>パスを指定を選択することでCSSを直接指定するこもと可能です。</div>'; | |
| 306 | + echo sprintf('<div class="inputGroup"><input type="text" name="%s[css_path]" value="%s" id="codoc_css_path" readonly>',CODOC_SETTINGS_OPTION_NAME,$CODOC_SETTINGS['css_path']); | |
| 307 | + echo '' . | |
| 308 | + '<script type="text/javascript"> ' . | |
| 309 | + ' function gen_css_path(select) { ' . | |
| 310 | + ' if (select.value == "dark") { ' . | |
| 311 | + ' document.getElementById("darkmode-caution").style.display="block";' . | |
| 312 | + ' } else { ' . | |
| 313 | + ' if (document.getElementById("darkmode-caution")) { ' . | |
| 314 | + ' document.getElementById("darkmode-caution").style.display="none";' . | |
| 315 | + ' } ' . | |
| 316 | + ' } ' . | |
| 317 | + ' if (select.value == "path") { ' . | |
| 318 | + ' document.getElementById("codoc_css_path").readOnly=false; ' . | |
| 319 | + ' if (!document.getElementById("codoc_css_path").value.match(/\//g)) {' . | |
| 320 | + ' document.getElementById("codoc_css_path").value=""; ' . | |
| 321 | + ' } ' . | |
| 322 | + ' } else { ' . | |
| 323 | + ' document.getElementById("codoc_css_path").readOnly=true; ' . | |
| 324 | + ' document.getElementById("codoc_css_path").value=select.value; ' . | |
| 325 | + ' } ' . | |
| 326 | + ' } ' . | |
| 327 | + '</script> ' ; | |
| 328 | + | |
| 329 | + echo sprintf('<select name="theme_name" onChange="gen_css_path(this)" id="codoc_theme_select">'); | |
| 330 | + foreach (["blue","red","green","black","dark","blue-square","red-square","green-square","black-square","dark-square"] as $theme) { | |
| 331 | + if ($theme == "blue") { | |
| 332 | + echo sprintf('<option value="%s" %s>青</option>', $theme, ($CODOC_SETTINGS["css_path"] == "blue" ? "selected" : "")); | |
| 333 | + } | |
| 334 | + if ($theme == "red") { | |
| 335 | + echo sprintf('<option value="%s" %s>赤</option>', $theme, ($CODOC_SETTINGS["css_path"] == "red" ? "selected" : "")); | |
| 336 | + } | |
| 337 | + if ($theme == "green") { | |
| 338 | + echo sprintf('<option value="%s" %s>緑</option>', $theme, ($CODOC_SETTINGS["css_path"] == "green" ? "selected" : "")); | |
| 339 | + } | |
| 340 | + if ($theme == "black") { | |
| 341 | + echo sprintf('<option value="%s" %s>黒</option>', $theme, ($CODOC_SETTINGS["css_path"] == "black" ? "selected" : "")); | |
| 342 | + } | |
| 343 | + if ($theme == "dark") { | |
| 344 | + echo sprintf('<option value="%s" %s>ダークモード</option>', $theme, ($CODOC_SETTINGS["css_path"] == "dark" ? "selected" : "")); | |
| 345 | + } | |
| 346 | + if ($theme == "blue-square") { | |
| 347 | + echo sprintf('<option value="%s" %s>青(四角)</option>', $theme, ($CODOC_SETTINGS["css_path"] == "blue-square" ? "selected" : "")); | |
| 348 | + } | |
| 349 | + if ($theme == "red-square") { | |
| 350 | + echo sprintf('<option value="%s" %s>赤(四角)</option>', $theme, ($CODOC_SETTINGS["css_path"] == "red-square" ? "selected" : "")); | |
| 351 | + } | |
| 352 | + if ($theme == "green-square") { | |
| 353 | + echo sprintf('<option value="%s" %s>緑(四角)</option>', $theme, ($CODOC_SETTINGS["css_path"] == "green-square" ? "selected" : "")); | |
| 354 | + } | |
| 355 | + if ($theme == "black-square") { | |
| 356 | + echo sprintf('<option value="%s" %s>黒(四角)</option>', $theme, ($CODOC_SETTINGS["css_path"] == "black-square" ? "selected" : "")); | |
| 357 | + } | |
| 358 | + if ($theme == "dark-square") { | |
| 359 | + echo sprintf('<option value="%s" %s>ダークモード(四角)</option>', $theme, ($CODOC_SETTINGS["css_path"] == "dark-square" ? "selected" : "")); | |
| 360 | + } | |
| 361 | + } | |
| 362 | + echo sprintf('<option value="path" %s>パス指定</option>', (preg_match("/\//",$CODOC_SETTINGS["css_path"]) ? "selected" : "")); | |
| 363 | + echo '<script type="text/javascript">gen_css_path(document.getElementById(\'codoc_theme_select\'))</script>'; | |
| 364 | + echo sprintf('</select></div>'); | |
| 365 | + echo '<p id="darkmode-caution" style="display:none">ダークモード向けテーマは文字色が白となり、背景色によっては文字が見えない状態となるためご注意ください。</p>'; | |
| 366 | + }, | |
| 367 | + 'codoc', //page | |
| 368 | + 'setting_section_id' //Section | |
| 369 | + ); | |
| 370 | + add_settings_field( | |
| 371 | + 'paywall_text', // id | |
| 372 | + '各種文言', // title | |
| 373 | + function() { | |
| 374 | + global $CODOC_SETTINGS; | |
| 375 | + echo '<div class="excerpt">ペイウォール内の一部文言を変更できます。</div>'; | |
| 376 | + echo '<table class="innerTable"><tbody>'; | |
| 377 | + echo '<tr><th>いいねの表示</th><td>'; | |
| 378 | + echo sprintf('<input type="radio" value="1" name="%s[show_like]" id="show_like_on" %s><label for="show_like_on">する</label> ',CODOC_SETTINGS_OPTION_NAME,($CODOC_SETTINGS['show_like'] == '1' ? "checked" : "")); | |
| 379 | + echo sprintf('<input type="radio" value="0" name="%s[show_like]" id="show_like_off" %s><label for="show_like_off">しない</label> <br / >',CODOC_SETTINGS_OPTION_NAME,($CODOC_SETTINGS['show_like'] == '0' ? "checked" : "")); | |
| 380 | + echo '</td></tr>'; | |
| 381 | + | |
| 382 | + echo '<tr><th>codocについての表示</th><td>'; | |
| 383 | + echo sprintf('<input type="radio" value="1" name="%s[show_about_codoc]" id="show_about_codoc_on" %s><label for="show_about_codoc_on">する</label> ',CODOC_SETTINGS_OPTION_NAME,($CODOC_SETTINGS['show_about_codoc'] == '1' ? "checked" : "")); | |
| 384 | + echo sprintf('<input type="radio" value="0" name="%s[show_about_codoc]" id="show_about_codoc_off" %s><label for="show_about_codoc_off">しない</label> <br / >',CODOC_SETTINGS_OPTION_NAME,($CODOC_SETTINGS['show_about_codoc'] == '0' ? "checked" : "")); | |
| 385 | + echo '</td></tr>'; | |
| 386 | + | |
| 387 | + echo '<tr><th>PoweredByの表示</th><td>'; | |
| 388 | + echo sprintf('<input type="radio" value="1" name="%s[show_powered_by]" id="show_powered_by_on" %s><label for="show_powered_by_on">する</label> ',CODOC_SETTINGS_OPTION_NAME,($CODOC_SETTINGS['show_powered_by'] == '1' ? "checked" : "")); | |
| 389 | + echo sprintf('<input type="radio" value="0" name="%s[show_powered_by]" id="show_powered_by_off" %s><label for="show_powered_by_off">しない</label> <br / >',CODOC_SETTINGS_OPTION_NAME,($CODOC_SETTINGS['show_powered_by'] == '0' ? "checked" : "")); | |
| 390 | + echo '</td></tr>'; | |
| 391 | + | |
| 392 | + echo '<tr><th>記事購入ボタンのテキスト</th><td>'; | |
| 393 | + echo sprintf('<input type="text" name="%s[entry_button_text]" value="%s">',CODOC_SETTINGS_OPTION_NAME,esc_html($CODOC_SETTINGS['entry_button_text'])); | |
| 394 | + echo '</td></tr>'; | |
| 395 | + | |
| 396 | + echo '<tr><th>サブスクリプション購入ボタンのテキスト</th><td>'; | |
| 397 | + echo sprintf('<input type="text" name="%s[subscription_button_text]" value="%s">',CODOC_SETTINGS_OPTION_NAME,esc_html($CODOC_SETTINGS['subscription_button_text'])); | |
| 398 | + echo '</td></tr>'; | |
| 399 | + | |
| 400 | + echo '<tr><th>サポートボタンのテキスト</th><td>'; | |
| 401 | + echo sprintf('<input type="text" name="%s[support_button_text]" value="%s">',CODOC_SETTINGS_OPTION_NAME,esc_html($CODOC_SETTINGS['support_button_text'])); | |
| 402 | + echo '</td></tr>'; | |
| 403 | + | |
| 404 | + echo '<tr><th>「サブスクリプションに含まれる記事」のテキスト</th><td>'; | |
| 405 | + echo sprintf('<input type="text" name="%s[subscription_message]" value="%s">',CODOC_SETTINGS_OPTION_NAME,esc_html($CODOC_SETTINGS['subscription_message'])); | |
| 406 | + echo '</td></tr>'; | |
| 407 | + | |
| 408 | + echo '<tr><th>サポート説明のテキスト</th><td>'; | |
| 409 | + echo sprintf('<input type="text" name="%s[support_message]" value="%s">',CODOC_SETTINGS_OPTION_NAME,esc_html($CODOC_SETTINGS['support_message'])); | |
| 410 | + echo '</td></tr>'; | |
| 411 | + | |
| 412 | + echo '</tbody></table>'; | |
| 413 | + }, | |
| 414 | + 'codoc', //page | |
| 415 | + 'setting_section_id' //Section | |
| 416 | + ); | |
| 240 | 417 | |
| 241 | - register_setting( | |
| 242 | - 'codoc_option_group', // option group | |
| 243 | - CODOC_USERCODE_OPTION_NAME | |
| 244 | - ); | |
| 245 | - register_setting( | |
| 246 | - 'codoc_option_group', // option group | |
| 247 | - CODOC_TOKEN_OPTION_NAME | |
| 248 | - ); | |
| 249 | - | |
| 250 | - if (isset($_GET['codoc_auth_finished']) and $_GET['codoc_auth_finished']) { | |
| 251 | - add_settings_error( 'general', 'settings_updated', __( 'codocの認証が完了しました' ), 'success' ); | |
| 252 | - } | |
| 253 | - | |
| 254 | - add_settings_field( | |
| 255 | - 'codoc_auth', | |
| 256 | - '認証', | |
| 257 | - function() { | |
| 258 | - global $CODOC_USERCODE; | |
| 259 | - global $CODOC_TOKEN; | |
| 260 | - global $CODOC_AUTHINFO; | |
| 261 | - $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';"; | |
| 418 | + add_settings_field( | |
| 419 | + 'codoc_tag_attributes', // id | |
| 420 | + 'codocタグの属性', // title | |
| 421 | + function() { | |
| 422 | + global $CODOC_SETTINGS; | |
| 423 | + echo '<div class="excerpt">codocタグに特定の属性を追加することができます。</div>'; | |
| 424 | + echo sprintf('<input type="text" name="%s[codoc_tag_attributes]" value="%s">',CODOC_SETTINGS_OPTION_NAME,htmlspecialchars($CODOC_SETTINGS['codoc_tag_attributes'])); | |
| 425 | + }, | |
| 426 | + 'codoc', //page | |
| 427 | + 'setting_section_id' //Section | |
| 428 | + ); | |
| 429 | + add_settings_field( | |
| 430 | + 'codoc_script_tag_attributes', // id | |
| 431 | + 'codoc scriptタグの属性', // title | |
| 432 | + function() { | |
| 433 | + global $CODOC_SETTINGS; | |
| 434 | + echo '<div class="excerpt">codoc scriptタグに特定の属性を追加することができます。</div>'; | |
| 435 | + echo sprintf('<input type="text" name="%s[codoc_script_tag_attributes]" value="%s">',CODOC_SETTINGS_OPTION_NAME,htmlspecialchars($CODOC_SETTINGS['codoc_script_tag_attributes'])); | |
| 436 | + }, | |
| 437 | + 'codoc', //page | |
| 438 | + 'setting_section_id' //Section | |
| 439 | + ); | |
| 440 | + add_settings_field( | |
| 441 | + 'str_replace_binded_url', // id | |
| 442 | + 'パーマリンク', // title | |
| 443 | + function() { | |
| 444 | + global $CODOC_SETTINGS; | |
| 445 | + echo '<div class="excerpt">codoc側に登録するパーマリンクを置換することができます。</div>'; | |
| 446 | + echo sprintf('<input type="text" name="%s[str_replace_binded_url_from]" value="%s">',CODOC_SETTINGS_OPTION_NAME,esc_html($CODOC_SETTINGS['str_replace_binded_url_from'])); | |
| 447 | + echo ('<span class="suptext">を</span>'); | |
| 448 | + echo sprintf('<input type="text" name="%s[str_replace_binded_url_to]" value="%s">',CODOC_SETTINGS_OPTION_NAME,esc_html($CODOC_SETTINGS['str_replace_binded_url_to'])); | |
| 449 | + echo ('に変換'); | |
| 450 | + }, | |
| 451 | + 'codoc', //page | |
| 452 | + 'setting_section_id' //Section | |
| 453 | + ); | |
| 262 | 454 | |
| 263 | - echo sprintf('<p><font color="green" id="codoc-auth-message"><strong>%s</strong> で認証済</font></p>',$CODOC_AUTHINFO['email']); | |
| 455 | + add_settings_field( | |
| 456 | + 'str_around_codoc_tag', // id | |
| 457 | + 'HTMLの挿入', // title | |
| 458 | + function() { | |
| 459 | + global $CODOC_SETTINGS; | |
| 460 | + echo '<div class="excerpt">codocタグの前後にHTMLを挿入することができます。</div>'; | |
| 461 | + echo sprintf('<div class="inputRow"><textarea name="%s[str_before_codoc_tag]" cols="40">%s</textarea>',CODOC_SETTINGS_OPTION_NAME,esc_html($CODOC_SETTINGS['str_before_codoc_tag'])); | |
| 462 | + echo ('を前に挿入</div>'); | |
| 463 | + echo sprintf('<div class="inputRow"><textarea name="%s[str_after_codoc_tag]" cols="40">%s</textarea>',CODOC_SETTINGS_OPTION_NAME,esc_html($CODOC_SETTINGS['str_after_codoc_tag'])); | |
| 464 | + echo ('を後に挿入</div>'); | |
| 264 | 465 | |
| 265 | - echo sprintf('<input id="codoc-usercode" type="hidden" name="%s" value="%s">',CODOC_USERCODE_OPTION_NAME,$CODOC_USERCODE); | |
| 266 | - echo sprintf('<input id="codoc-token" type="hidden" name="%s" value="%s">',CODOC_TOKEN_OPTION_NAME,$CODOC_TOKEN); | |
| 267 | - echo sprintf('<p>認証を<a href="javascript:void(0);" onClick="' . $script . '">解除する</p>'); | |
| 268 | - }, | |
| 269 | - 'codoc', | |
| 270 | - 'setting_section_id' | |
| 271 | - ); | |
| 272 | - | |
| 273 | - } | |
| 466 | + }, | |
| 467 | + 'codoc', //page | |
| 468 | + 'setting_section_id' //Section | |
| 469 | + ); | |
| 274 | 470 | |
| 275 | - // ない場合 | |
| 276 | - if (!$CODOC_AUTHINFO and !isset($_GET['auth_by_myself'])) { | |
| 277 | - add_settings_field( | |
| 278 | - 'codoc_auth', | |
| 279 | - '認証', | |
| 280 | - function() { | |
| 281 | - | |
| 282 | - //$current_url = (empty($_SERVER['HTTPS']) ? 'http://' : 'https://').$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; | |
| 283 | - $current_url = admin_url('options-general.php') . '?page=codoc'; | |
| 284 | - $login_url = sprintf("location.href='%s'",CODOC_URL . '/me/token?from=wp&return_url=' . urlencode($current_url)); | |
| 285 | - $register_url = sprintf("location.href='%s'",CODOC_URL . '/register?from=wp&return_url=' . urlencode($current_url)); | |
| 286 | - // submitを消しておく | |
| 287 | - echo ('<script type="text/javascript">window.onload=function(){document.getElementById(\'submit\').style.display = \'none\'}</script>'); | |
| 288 | - echo ('codocをWordPressでご利用いただくには認証が必要です。<br />'); | |
| 289 | - echo sprintf('ユーザーコードとAPIトークンを<a href="%s&auth_by_myself=1">直接入力</a>することでも認証できます。<br /><br />',$current_url); | |
| 290 | - 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); | |
| 291 | - | |
| 471 | + add_settings_field( | |
| 472 | + 'always_show_support_tag', // id | |
| 473 | + 'サポートの自動挿入', // title | |
| 474 | + function() { | |
| 475 | + global $CODOC_SETTINGS; | |
| 476 | + echo '<div class="excerpt">codocブロックを挿入することなく投稿にサポート機能を追加します</div>'; | |
| 477 | + echo '<table class="innerTable"><tbody>'; | |
| 478 | + echo '<tr><th>自動挿入</th><td>'; | |
| 479 | + echo sprintf('<input type="radio" value="1" name="%s[always_show_support]" id="always_show_support_on" %s><label for="always_show_support_on">する</label> ',CODOC_SETTINGS_OPTION_NAME,($CODOC_SETTINGS['always_show_support'] == '1' ? "checked" : "")); | |
| 480 | + echo sprintf('<input type="radio" value="0" name="%s[always_show_support]" id="always_show_support_off" %s><label for="always_show_support_off">しない</label> <br / >',CODOC_SETTINGS_OPTION_NAME,($CODOC_SETTINGS['always_show_support'] == '0' ? "checked" : "")); | |
| 481 | + echo '</td></tr>'; | |
| 482 | + echo '<tr><th>表示位置</th><td>'; | |
| 483 | + echo sprintf('<input type="radio" value="top" name="%s[show_support_location]" id="show_support_location_top" %s><label for="show_support_location_top">冒頭</label> ',CODOC_SETTINGS_OPTION_NAME,($CODOC_SETTINGS['show_support_location'] == 'top' ? "checked" : "")); | |
| 484 | + echo sprintf('<input type="radio" value="bottom" name="%s[show_support_location]" id="show_support_location_bottom" %s><label for="show_support_location_bottom">末尾</label> <br / >',CODOC_SETTINGS_OPTION_NAME,($CODOC_SETTINGS['show_support_location'] == 'bottom' ? "checked" : "")); | |
| 485 | + echo '</td></tr>'; | |
| 486 | + echo '<tr><th>説明文</th><td>'; | |
| 487 | + echo sprintf('<input type="text" placeholder="サポートの説明文をカスタマイズできます" size="50%%" name="%s[show_support_message]" value="%s">',CODOC_SETTINGS_OPTION_NAME,esc_html($CODOC_SETTINGS['show_support_message'])); | |
| 488 | + echo '</td></tr>'; | |
| 489 | + echo '<tr><th>対象カテゴリ名</th><td>'; | |
| 490 | + echo sprintf('<input placeholder=""|"区切りで複数指定できます" type="text" size="50%%" name="%s[show_support_categories]" value="%s"><br />',CODOC_SETTINGS_OPTION_NAME,esc_html($CODOC_SETTINGS['show_support_categories'])); | |
| 491 | + echo '</td></tr>'; | |
| 492 | + echo '</tbody></table>'; | |
| 292 | 493 | |
| 293 | - }, | |
| 294 | - 'codoc', | |
| 295 | - 'setting_section_id' | |
| 296 | - ); | |
| 297 | - } | |
| 298 | - // ない場合かつ自分で認証する場合 | |
| 299 | - if (!$CODOC_AUTHINFO and (isset($_GET['auth_by_myself']) and $_GET['auth_by_myself'])) { | |
| 300 | - register_setting( | |
| 301 | - 'codoc_option_group', // option group | |
| 302 | - CODOC_USERCODE_OPTION_NAME | |
| 303 | - ); | |
| 304 | - add_settings_field( | |
| 305 | - 'codoc_usercode', | |
| 306 | - 'ユーザーコード', | |
| 307 | - function() { | |
| 308 | - global $CODOC_USERCODE; | |
| 309 | - echo sprintf('<input type="text" name="%s" value="%s">',CODOC_USERCODE_OPTION_NAME,$CODOC_USERCODE); | |
| 310 | - }, | |
| 311 | - 'codoc', | |
| 312 | - 'setting_section_id' | |
| 313 | - ); | |
| 314 | - register_setting( | |
| 315 | - 'codoc_option_group', // option group | |
| 316 | - CODOC_TOKEN_OPTION_NAME | |
| 317 | - ); | |
| 318 | - add_settings_field( | |
| 319 | - 'codoc_token', | |
| 320 | - 'APIトークン', | |
| 321 | - function() { | |
| 322 | - global $CODOC_TOKEN; | |
| 323 | - echo sprintf('<input type="text" name="%s" value="%s">',CODOC_TOKEN_OPTION_NAME,$CODOC_TOKEN); | |
| 324 | - }, | |
| 325 | - 'codoc', | |
| 326 | - 'setting_section_id' | |
| 327 | - ); | |
| 328 | - } | |
| 329 | - | |
| 330 | - // 認証情報を保存 | |
| 331 | - add_action( 'update_option_' . CODOC_USERCODE_OPTION_NAME, function( $old_value, $new_value ) { | |
| 332 | - global $CODOC_RE_AUTHORIZE; | |
| 333 | - $CODOC_RE_AUTHORIZE = 1; | |
| 334 | - },9,2); // $hook, $function_to_add, $priority, $accepted_args | |
| 335 | - add_action( 'update_option_' . CODOC_TOKEN_OPTION_NAME, function( $old_value, $new_value ) { | |
| 336 | - global $CODOC_RE_AUTHORIZE; | |
| 337 | - $CODOC_RE_AUTHORIZE = 1; | |
| 338 | - },10,2); | |
| 339 | - add_action('updated_option',function(){ | |
| 340 | - global $CODOC_RE_AUTHORIZE; | |
| 341 | - if ($CODOC_RE_AUTHORIZE) { | |
| 342 | - $data = $this->callAPI('GET',''); | |
| 343 | - if ($data->status and $user = $data->user) { | |
| 344 | - update_option(CODOC_AUTHINFO_OPTION_NAME,[ | |
| 345 | - 'email' => $user->email, | |
| 346 | - 'name' => $user->name, | |
| 347 | - ]); | |
| 348 | - } else { | |
| 349 | - update_option(CODOC_AUTHINFO_OPTION_NAME,''); | |
| 350 | - } | |
| 351 | - } | |
| 352 | - }); | |
| 353 | - | |
| 354 | - }); | |
| 355 | - // プラグイン一覧に設定のリンクをいれる | |
| 356 | - add_filter( 'plugin_action_links_' . plugin_basename( plugin_dir_path( __FILE__ ) . 'codoc' . '.php' ), | |
| 357 | - function( $links ) { | |
| 358 | - $setting_link = sprintf( '<a href="%s">%s</a>', esc_url( add_query_arg( 'page', 'codoc', admin_url( 'options-general.php' ) ) ), esc_html( '設定' ) ); | |
| 359 | - array_unshift( $links, $setting_link ); | |
| 360 | - | |
| 361 | - return $links; | |
| 362 | - } | |
| 363 | - ); | |
| 364 | - | |
| 365 | - } | |
| 366 | - public function add_mce() { | |
| 367 | - // Add Shortcode options in the WordPres visual editors | |
| 368 | - //wp_enqueue_script( 'codoc-editor-script', CODOC_URL . 'codoc/src_mce/codoc-editor.js'); | |
| 369 | - | |
| 370 | - add_action( 'init', function() { | |
| 371 | - if ( ! current_user_can( 'edit_posts' ) && ! current_user_can( 'edit_pages' ) ) { | |
| 372 | - return; | |
| 373 | - } | |
| 374 | - if ( get_user_option( 'rich_editing' ) == 'true' ) { | |
| 375 | - // プラグイン内で使うCSRF を生成 | |
| 376 | - $path = plugins_url( 'codoc/src_mce/codoc-editor-onload.js', __DIR__ ); | |
| 377 | - wp_enqueue_script( 'codoc-editor-onload', $path, array('jquery'), '', true ); | |
| 378 | - // I just want to insert this global variables but i don't know how i can do it.. | |
| 379 | - wp_localize_script( | |
| 380 | - 'codoc-editor-onload', | |
| 381 | - 'CODOCEDITOR', | |
| 382 | - array( | |
| 383 | - 'action' => 'codoc_shortcodes', | |
| 384 | - 'nonce' => wp_create_nonce( 'codoc_shortcodes' ), | |
| 385 | - | |
| 386 | - 'codoc_url' => CODOC_URL, | |
| 387 | - 'codoc_usercode' => get_option(CODOC_USERCODE_OPTION_NAME), | |
| 388 | - 'codoc_plugin_version' => CODOC_PLUGIN_VERSION, | |
| 389 | - 'codoc_sdk_path' => CODOC_SDK_PATH, | |
| 390 | - ) | |
| 391 | - ); | |
| 494 | + }, | |
| 495 | + 'codoc', //page | |
| 496 | + 'setting_section_id' //Section | |
| 497 | + ); | |
| 498 | + add_settings_field( | |
| 499 | + 'do_not_filter_the_content', // id | |
| 500 | + '支障が出る処理を無効化', // title | |
| 501 | + function() { | |
| 502 | + global $CODOC_SETTINGS; | |
| 503 | + echo '<div class="excerpt">codocの動作に支障が出る他のプラグインによる<strong>フィルター処理</strong>を無効化できます。codocが上手く動かない場合のみ利用してください。</div>'; | |
| 504 | + echo sprintf('<input type="radio" value="1" name="%s[do_not_filter_the_content]" id="do_not_filter_the_content_on" %s><label for="do_not_filter_the_content_on">する</label> ',CODOC_SETTINGS_OPTION_NAME,($CODOC_SETTINGS['do_not_filter_the_content'] == '1' ? "checked" : "")); | |
| 505 | + echo sprintf('<input type="radio" value="0" name="%s[do_not_filter_the_content]" id="do_not_filter_the_content_off" %s><label for="do_not_filter_the_content_off">しない</label> ',CODOC_SETTINGS_OPTION_NAME,($CODOC_SETTINGS['do_not_filter_the_content'] == '0' ? "checked" : "")); | |
| 506 | + }, | |
| 507 | + 'codoc', //page | |
| 508 | + 'setting_section_id' //Section | |
| 509 | + ); | |
| 510 | + add_settings_field( | |
| 511 | + 'debug_params', // id | |
| 512 | + 'デバック用', // title | |
| 513 | + function() { | |
| 514 | + global $CODOC_SETTINGS; | |
| 515 | + echo '<div class="excerpt">デバック用のパラメーターを指定できます。codocサポートから依頼がある場合のみ利用してください。</div>'; | |
| 516 | + echo sprintf('<input placeholder="" type="text" size="50%%" name="%s[debug_params]" value="%s"><br />',CODOC_SETTINGS_OPTION_NAME,esc_html($CODOC_SETTINGS['debug_params'])); | |
| 517 | + }, | |
| 518 | + 'codoc', //page | |
| 519 | + 'setting_section_id' //Section | |
| 520 | + ); | |
| 392 | 521 | |
| 393 | - // ここでtinymceのプラグイン追加 | |
| 394 | - //wp_enqueue_style( 'codoc-admin-style', plugins_url( 'codoc/src_mce/codoc-admin.css', __DIR__ ) ); | |
| 395 | - //add_editor_style( plugins_url( 'codoc/src_mce/codoc-admin.css', __DIR__ ) ); | |
| 396 | - wp_enqueue_style( 'codoc-admin-style', CODOC_URL . CODOC_SDK_PATH . '.tinymce.css?c=' . date('Ymd') ); | |
| 397 | - add_editor_style( CODOC_URL . CODOC_SDK_PATH . '.tinymce.css' ); | |
| 522 | + add_settings_field( | |
| 523 | + 'cretor_info', // id | |
| 524 | + 'クリエイター情報', // title | |
| 525 | + function() { | |
| 526 | + global $CODOC_SETTINGS; | |
| 527 | + global $CODOC_AUTHINFO; | |
| 528 | + echo sprintf('<p><font id="codoc-update-creator-info-message"></font></p>',""); | |
| 529 | + // 変更を保存時に常に同期するのでこの表示は必要ないがUI上保持しておく | |
| 530 | + $script = "document.getElementById('codoc-update-creator-info-message').innerText='情報を更新するには変更を保存してください';document.getElementById('codoc-update-creator-info-message').color='red';"; | |
| 531 | + if (isset($CODOC_AUTHINFO['profile_image_url'])) { | |
| 532 | + echo sprintf('<img src="%s" width="40" height="40" />',$CODOC_AUTHINFO['profile_image_url']); | |
| 533 | + } | |
| 534 | + echo sprintf('<p>%s</p>',$CODOC_AUTHINFO['name']); | |
| 535 | + if ($connect_code = $CODOC_SETTINGS['codoc_connect_code']) { | |
| 536 | + echo sprintf('<p>%s外部サービス連携済(連携コード:%s)</p>', | |
| 537 | + ($CODOC_SETTINGS['codoc_connect_registration_mode'] == 'dedicated' ? '専用ユーザーモードで' : ''), | |
| 538 | + $connect_code); | |
| 539 | + } | |
| 540 | + echo sprintf('<p>情報を<a href="javascript:void(0);" onClick="' . $script . '">更新する</a></p>'); | |
| 541 | + echo ('<p>codoc側でロゴやカバー画像を変更した場合は都度更新を行ってください</p>'); | |
| 542 | + }, | |
| 543 | + 'codoc', //page | |
| 544 | + 'setting_section_id' //Section | |
| 545 | + ); | |
| 398 | 546 | |
| 399 | - add_filter( 'mce_external_plugins', function( $plugin_array ) { | |
| 400 | - //$plugin_array['codoc'] = plugins_url( 'codoc/src_mce/codoc-editor.js', __DIR__ ); | |
| 401 | - $plugin_array['codoc'] = CODOC_URL . CODOC_SDK_PATH . '.tinymce.js?c=' . date('Ymd'); | |
| 402 | - return $plugin_array; | |
| 403 | - } ); | |
| 404 | - add_filter( 'mce_buttons', function( $buttons ) { | |
| 405 | - array_push( $buttons, "|", "codoc" ); | |
| 406 | - return $buttons; | |
| 407 | - } ); | |
| 547 | + register_setting( | |
| 548 | + 'codoc_option_group', // option group | |
| 549 | + CODOC_USERCODE_OPTION_NAME | |
| 550 | + ); | |
| 551 | + register_setting( | |
| 552 | + 'codoc_option_group', // option group | |
| 553 | + CODOC_TOKEN_OPTION_NAME | |
| 554 | + ); | |
| 408 | 555 | |
| 409 | - } | |
| 410 | - } ); | |
| 411 | - } | |
| 412 | - public function save_post($post_ID,$post,$update) { | |
| 413 | - if (preg_match('/^(auto-draft|inherit)$/',$post->post_status)) { | |
| 414 | - return; | |
| 415 | - } | |
| 416 | - | |
| 417 | - $post_content = $post->post_content; | |
| 418 | - preg_match('/wp:codoc\/codoc-block +?({.*})/',$post_content,$matches); | |
| 419 | - // GUTENBERG で指定されたjson(記事情報)がない場合 | |
| 420 | - if (!$matches) { | |
| 421 | - return; | |
| 422 | - } | |
| 423 | - // GUTENBERG で保存している内容を取得 | |
| 424 | - $codoc_info = json_decode($matches[1],true); | |
| 425 | - // codoc タグがない場合はなにもしない | |
| 426 | - //preg_match('/(<span +data-id="codoc-tag"[^>]+>(?:.+|)<\/span>)/',$post_content,$matches); | |
| 427 | - preg_match('/(<(?:span|div)[^>]+data-id="codoc-tag"(?:[^>]+|)>(?:.+|)<\/(?:span|div)>)/',$post_content,$matches); | |
| 428 | - if (!$matches) { | |
| 429 | - return; | |
| 430 | - } | |
| 431 | - // TODO: split or html parse? HTMLがつながってる場合はおかしくなる | |
| 432 | - // classic(tinymce)版の場合p、もしくはdivにかこまれている | |
| 433 | - $splited = preg_split('/(?:<(?:div|p)(?:[^>]+|)>|)<\!-- +wp:codoc\/codoc-block .*<\!-- +\/wp:codoc\/codoc-block +-->(?:<\/(?:div|p)>|)/s',$post_content); | |
| 434 | - $status = $post->post_status == 'publish' ? 1 : 0; | |
| 435 | - # $body_free = ''; | |
| 436 | - # $body_paywalled = ''; | |
| 437 | - # if (count($splited) >= 2) { | |
| 438 | - # | |
| 439 | - # } | |
| 440 | - $binded_url = get_permalink($post_ID); | |
| 441 | - $CODOC_SETTINGS = get_option(CODOC_SETTINGS_OPTION_NAME); | |
| 442 | - if (isset($CODOC_SETTINGS['str_replace_binded_url_from']) and | |
| 443 | - isset($CODOC_SETTINGS['str_replace_binded_url_to']) and | |
| 444 | - $CODOC_SETTINGS['str_replace_binded_url_from']) { | |
| 445 | - $binded_url = str_replace(sanitize_text_field($CODOC_SETTINGS['str_replace_binded_url_from']), | |
| 446 | - sanitize_text_field($CODOC_SETTINGS['str_replace_binded_url_to']), | |
| 447 | - $binded_url); | |
| 448 | - } | |
| 449 | - | |
| 450 | - $params = [ | |
| 451 | - 'title' => $post->post_title, | |
| 452 | - 'body_free' => $splited[0], | |
| 453 | - 'body_paywalled' => $splited[1], | |
| 454 | - 'status' => $status, | |
| 455 | - 'binded_url' => $binded_url, | |
| 456 | - 'show_price' => isset($codoc_info['showPrice']) ? ($codoc_info['showPrice'] ? 1 : 0) : 0, | |
| 457 | - 'price' => isset($codoc_info['price']) ? $codoc_info['price'] : 100, | |
| 458 | - 'limited' => isset($codoc_info['limited']) ? ($codoc_info['limited'] ? 1 : 0) : 0, | |
| 459 | - 'limited_count' => isset($codoc_info['limitedCount']) ? $codoc_info['limitedCount'] : 1, | |
| 460 | - 'affiliate_mode' => isset($codoc_info['affiliateMode']) ? ($codoc_info['affiliateMode'] ? 1 : 0) : 0, | |
| 461 | - 'affiliate_rate' => isset($codoc_info['affiliateRate']) ? $codoc_info['affiliateRate'] : '0.0500', | |
| 462 | - 'show_support' => isset($codoc_info['showSupport']) ? ($codoc_info['showSupport'] ? 1 : 0) : 0, | |
| 463 | - 'subscriptions' => isset($codoc_info['subscriptions']) ? array_keys($codoc_info['subscriptions']) : [], | |
| 464 | - ]; | |
| 465 | - $entryCode = get_post_meta($post_ID,'codoc_entry_code',true); | |
| 466 | - if ($entryCode) { | |
| 467 | - $res = $this->callAPI('PUT','/entries/' . $entryCode ,$params); | |
| 468 | - } else { | |
| 469 | - $res = $this->callAPI('POST', '/entries', $params); | |
| 470 | - if (is_object($res) and $res->status) { | |
| 471 | - update_post_meta($post_ID,'codoc_entry_code',$res->entry->code); | |
| 472 | - } | |
| 473 | - } | |
| 474 | - return true; | |
| 475 | - } | |
| 476 | - function updated_post_meta( $meta_ID, $post_ID, $meta_key ) { | |
| 477 | - if ($meta_key != '_thumbnail_id') { | |
| 478 | - return; | |
| 479 | - } | |
| 480 | - if ( has_post_thumbnail($post_ID) ) { | |
| 481 | - $attachment = wp_get_attachment_metadata( get_post_thumbnail_id($post_ID)); | |
| 482 | - $upload_dir = wp_upload_dir(); | |
| 483 | - $file_path = sprintf ('%s/%s',$upload_dir['basedir'],$attachment['file']); | |
| 484 | - | |
| 485 | - if (is_readable($file_path)) { | |
| 486 | - $name = 'file'; | |
| 556 | + if (isset($_GET['codoc_auth_finished']) and $_GET['codoc_auth_finished']) { | |
| 557 | + add_settings_error( 'general', 'settings_updated', __( 'codocの認証が完了しました' ), 'success' ); | |
| 558 | + } | |
| 487 | 559 | |
| 488 | - $boundary = wp_generate_password(24); | |
| 560 | + add_settings_field( | |
| 561 | + 'codoc_auth', | |
| 562 | + '認証', | |
| 563 | + function() { | |
| 564 | + global $CODOC_USERCODE; | |
| 565 | + global $CODOC_TOKEN; | |
| 566 | + global $CODOC_AUTHINFO; | |
| 567 | + $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';"; | |
| 489 | 568 | |
| 490 | - $payload = ''; | |
| 491 | - $payload .= '--' . $boundary; | |
| 492 | - $payload .= "\r\n"; | |
| 493 | - $payload .= 'Content-Disposition: form-data; name="' . $name . '"; filename="' . basename( $file_path ) . '"' . "\r\n"; | |
| 494 | - $payload .= "Content-Type: application/octet-stream\r\n"; | |
| 495 | - $payload .= "Content-Transfer-Encoding: binary\r\n"; | |
| 496 | - $payload .= "\r\n"; | |
| 497 | - $payload .= file_get_contents( $file_path ); | |
| 498 | - $payload .= "\r\n"; | |
| 499 | - | |
| 500 | - $payload .= '--' . $boundary . '--'; | |
| 501 | - | |
| 502 | - | |
| 503 | - $entryCode = get_post_meta($post_ID,'codoc_entry_code',true); | |
| 504 | - $res = $this->callAPI( | |
| 505 | - 'POST', | |
| 506 | - '/entries/' . $entryCode . '/thumbnail', | |
| 507 | - $payload, | |
| 508 | - ['content-type' => 'multipart/form-data; boundary=' . $boundary] | |
| 509 | - ); | |
| 510 | - } | |
| 511 | - } | |
| 512 | - } | |
| 513 | - function deleted_post_meta( $meta_ids, $post_ID, $meta_key,$meta_value ) { | |
| 514 | - if ($meta_key != '_thumbnail_id') { | |
| 515 | - return; | |
| 516 | - } | |
| 517 | - $entryCode = get_post_meta($post_ID,'codoc_entry_code',true); | |
| 518 | - $res = $this->callAPI( | |
| 519 | - 'POST', | |
| 520 | - '/entries/' . $entryCode . '/thumbnail', | |
| 521 | - [ "reset" => 1 ] | |
| 522 | - ); | |
| 523 | - } | |
| 524 | - function the_content( $post_content ) { | |
| 525 | - if ( is_single() && in_the_loop() && is_main_query() ) { | |
| 526 | - } | |
| 527 | - // codoc タグがあるかどうか (202001:span -> div に変更) | |
| 528 | - preg_match('/(<(span|div)[^>]+data-id="codoc-tag"(?:[^>]+|)>(?:.+|)<\/(?:div|span)>)/',$post_content,$matches); | |
| 529 | - // data-wp-plugin-ver が無いタグは分割しない | |
| 530 | - if (!$matches) { | |
| 531 | - return $post_content; | |
| 532 | - } | |
| 533 | - // codocタグで分割 (202001: gutenbergで直前のdivタグを削除) | |
| 534 | - $tag_regex = '/(?:<div(?:[^>]+|)>|)<(?:span|div)[^>]+data-id="codoc-tag"(?:[^>]+|)>(?:.+|)<\/(?:span|div)>(?:<\/div>|)/'; | |
| 535 | - if ( is_preview() ) { | |
| 536 | - $post_content = preg_replace($tag_regex,'<div class="codoc-continue">ここから上は無料で表示されます</div>',$post_content); | |
| 537 | - return $post_content; | |
| 538 | - } | |
| 539 | - // codoc タグの前後で分ける | |
| 540 | - $splited = preg_split($tag_regex,$post_content); | |
| 541 | - // codoc タグにID属性のentrycodeとdata-without-body(無料分を非表示)をつける | |
| 542 | - $post = get_post(); | |
| 543 | - $entryCode = sprintf('"codoc-entry-%s" ',get_post_meta($post->ID,'codoc_entry_code',true)); | |
| 544 | - $codoc_tag = preg_replace('/<((?:span|div)[^>]+)data-id="[^"]+"((?:[^>]+|))>/','<${1} ${2} data-without-body="1" id=' . $entryCode . ">", $matches[1]); | |
| 569 | + echo sprintf('<p><font color="green" id="codoc-auth-message"><strong>%s</strong> で認証済</font></p>',$CODOC_AUTHINFO['email']); | |
| 545 | 570 | |
| 546 | - # THE THOR の page-lp.php が " " をけずるための対策 | |
| 547 | - # "div class" -> "div class" | |
| 548 | - $codoc_tag = preg_replace('/div +class/','div class',$codoc_tag); | |
| 571 | + echo sprintf('<input id="codoc-usercode" type="hidden" name="%s" value="%s">',CODOC_USERCODE_OPTION_NAME,$CODOC_USERCODE); | |
| 572 | + echo sprintf('<input id="codoc-token" type="hidden" name="%s" value="%s">',CODOC_TOKEN_OPTION_NAME,$CODOC_TOKEN); | |
| 573 | + echo sprintf('<p>認証を<a href="javascript:void(0);" onClick="' . $script . '">解除する</p>'); | |
| 574 | + }, | |
| 575 | + 'codoc', | |
| 576 | + 'setting_section_id' | |
| 577 | + ); | |
| 549 | 578 | |
| 550 | - $CODOC_SETTINGS = get_option(CODOC_SETTINGS_OPTION_NAME); | |
| 551 | - $before = $CODOC_SETTINGS['str_before_codoc_tag'] or ""; | |
| 552 | - $after = $CODOC_SETTINGS['str_after_codoc_tag'] or ""; | |
| 579 | + } | |
| 553 | 580 | |
| 554 | - // 無料部分のみ表示 | |
| 555 | - return $splited[0] . sprintf('%s<div class="wp-block-codoc-codoc-block">%s</div>%s',$before,$codoc_tag,$after); | |
| 556 | - } | |
| 581 | + // ない場合 | |
| 582 | + if (!$CODOC_AUTHINFO and !isset($_GET['auth_by_myself'])) { | |
| 583 | + add_settings_field( | |
| 584 | + 'codoc_auth', | |
| 585 | + '認証', | |
| 586 | + function() { | |
| 587 | + $theme = wp_get_theme(); | |
| 588 | + $from = 'wp'; | |
| 589 | + if ($theme->get('Name') === 'codoc') { | |
| 590 | + $from = 'wp_codoc'; | |
| 591 | + } | |
| 592 | + //$current_url = (empty($_SERVER['HTTPS']) ? 'http://' : 'https://').$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; | |
| 593 | + $current_url = admin_url('options-general.php') . '?page=codoc'; | |
| 594 | + $login_url = sprintf("location.href='%s'",$this->get_codoc_url() . '/me/token?from=' . $from . '&return_url=' . urlencode($current_url)); | |
| 595 | + $register_url = sprintf("location.href='%s'",$this->get_codoc_url() . '/register?from=' . $from . '&return_url=' . urlencode($current_url)); | |
| 596 | + // submitを消しておく | |
| 597 | + echo ('<script type="text/javascript">window.onload=function(){document.getElementById(\'submit\').style.display = \'none\'}</script>'); | |
| 598 | + echo ('codocをWordPressでご利用いただくには認証が必要です。<br />'); | |
| 599 | + echo sprintf('ユーザーコードとAPIトークンを<a href="%s&auth_by_myself=1">直接入力</a>することでも認証できます。<br /><br />',$current_url); | |
| 600 | + 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); | |
| 557 | 601 | |
| 558 | - | |
| 602 | + | |
| 603 | + }, | |
| 604 | + 'codoc', | |
| 605 | + 'setting_section_id' | |
| 606 | + ); | |
| 607 | + } | |
| 608 | + // ない場合かつ自分で認証する場合 | |
| 609 | + if (!$CODOC_AUTHINFO and ( | |
| 610 | + (isset($_GET['auth_by_myself']) and $_GET['auth_by_myself']) or | |
| 611 | + (isset($_POST['auth_by_myself']) and $_POST['auth_by_myself']) | |
| 612 | + )) { | |
| 613 | + register_setting( | |
| 614 | + 'codoc_option_group', // option group | |
| 615 | + CODOC_USERCODE_OPTION_NAME | |
| 616 | + ); | |
| 617 | + register_setting( | |
| 618 | + 'codoc_option_group', // option group | |
| 619 | + CODOC_TOKEN_OPTION_NAME | |
| 620 | + ); | |
| 621 | + add_settings_field( | |
| 622 | + 'codoc_usercode', | |
| 623 | + 'ユーザーコード', | |
| 624 | + function() { | |
| 625 | + global $CODOC_USERCODE; | |
| 626 | + echo '<input type="hidden" name="auth_by_myself" value="1">'; | |
| 627 | + echo sprintf('<input type="text" name="%s" value="%s">',CODOC_USERCODE_OPTION_NAME,$CODOC_USERCODE); | |
| 628 | + }, | |
| 629 | + 'codoc', | |
| 630 | + 'setting_section_id' | |
| 631 | + ); | |
| 632 | + add_settings_field( | |
| 633 | + 'codoc_token', | |
| 634 | + 'APIトークン', | |
| 635 | + function() { | |
| 636 | + global $CODOC_TOKEN; | |
| 637 | + echo sprintf('<input type="text" name="%s" value="%s">',CODOC_TOKEN_OPTION_NAME,$CODOC_TOKEN); | |
| 638 | + }, | |
| 639 | + 'codoc', | |
| 640 | + 'setting_section_id' | |
| 641 | + ); | |
| 642 | + } | |
| 643 | + | |
| 644 | + // 認証情報を保存 | |
| 645 | + add_action( 'update_option_' . CODOC_USERCODE_OPTION_NAME, function( $old_value, $new_value ) { | |
| 646 | + global $CODOC_RE_AUTHORIZE; | |
| 647 | + $CODOC_RE_AUTHORIZE = 1; | |
| 648 | + },9,2); // $hook, $function_to_add, $priority, $accepted_args | |
| 649 | + add_action( 'update_option_' . CODOC_TOKEN_OPTION_NAME, function( $old_value, $new_value ) { | |
| 650 | + global $CODOC_RE_AUTHORIZE; | |
| 651 | + $CODOC_RE_AUTHORIZE = 1; | |
| 652 | + },10,2); | |
| 653 | + | |
| 654 | + add_action('update_option_' . CODOC_SETTINGS_OPTION_NAME,function(){ | |
| 655 | + $CODOC_SETTINGS = get_option(CODOC_SETTINGS_OPTION_NAME); | |
| 656 | + if (isset($CODOC_SETTINGS['always_show_support']) and $CODOC_SETTINGS['always_show_support']) { | |
| 657 | + $data = $this->util->get_support_entry([],[ | |
| 658 | + "usercode" => get_option(CODOC_USERCODE_OPTION_NAME), | |
| 659 | + "token" => get_option(CODOC_TOKEN_OPTION_NAME), | |
| 660 | + ]); | |
| 661 | + if ($data->status and $entry = $data->entry) { | |
| 662 | + update_option(CODOC_SUPPORT_ENTRYCODE_OPTION_NAME,$entry->code); | |
| 663 | + } else { | |
| 664 | + update_option(CODOC_SUPPORT_ENTRYCODE_OPTION_NAME,''); | |
| 665 | + } | |
| 666 | + } elseif(isset($CODOC_SETTINGS['always_show_support']) and !$CODOC_SETTINGS['always_show_support']) { | |
| 667 | + update_option(CODOC_SUPPORT_ENTRYCODE_OPTION_NAME,''); | |
| 668 | + } | |
| 669 | + }); | |
| 670 | + | |
| 671 | + add_action('updated_option',function(){ | |
| 672 | + global $CODOC_RE_AUTHORIZE; | |
| 673 | + if ($CODOC_RE_AUTHORIZE) { | |
| 674 | + //$data = $this->callAPI('GET',''); | |
| 675 | + $data = $this->util->get_user_info([],[ | |
| 676 | + "usercode" => get_option(CODOC_USERCODE_OPTION_NAME), | |
| 677 | + "token" => get_option(CODOC_TOKEN_OPTION_NAME), | |
| 678 | + ]); | |
| 679 | + if ($data->status and $user = $data->user) { | |
| 680 | + $this->update_codoc_authinfo($user); | |
| 681 | + } else { | |
| 682 | + update_option(CODOC_AUTHINFO_OPTION_NAME,''); | |
| 683 | + } | |
| 684 | + } | |
| 685 | + }); | |
| 686 | + | |
| 687 | + }); | |
| 688 | + // プラグイン一覧に設定のリンクをいれる | |
| 689 | + add_filter( 'plugin_action_links_' . plugin_basename( plugin_dir_path( __FILE__ ) . 'codoc' . '.php' ), | |
| 690 | + function( $links ) { | |
| 691 | + $setting_link = sprintf( '<a href="%s">%s</a>', esc_url( add_query_arg( 'page', 'codoc', admin_url( 'options-general.php' ) ) ), esc_html( '設定' ) ); | |
| 692 | + array_unshift( $links, $setting_link ); | |
| 693 | + | |
| 694 | + return $links; | |
| 695 | + } | |
| 696 | + ); | |
| 697 | + | |
| 698 | + } | |
| 699 | + public function add_mce() { | |
| 700 | + add_action( 'admin_enqueue_scripts', function() { | |
| 701 | + if ( ! current_user_can( 'edit_posts' ) && ! current_user_can( 'edit_pages' ) ) { | |
| 702 | + return; | |
| 703 | + } | |
| 704 | + $current_screen = get_current_screen(); | |
| 705 | + if ( ( method_exists( $current_screen, 'is_block_editor' ) && $current_screen->is_block_editor() ) || ( function_exists( 'is_gutenberg_page' ) && is_gutenberg_page() ) ) { | |
| 706 | + // Gutenberg Editor | |
| 707 | + // なにもしない | |
| 708 | + } else { | |
| 709 | + // tinymce | |
| 710 | + if ( get_user_option( 'rich_editing' ) == 'true' ) { | |
| 711 | + // プラグイン内で使うCSRF を生成 | |
| 712 | + $path = plugins_url( 'codoc/src_mce/codoc-editor-onload.js', __DIR__ ); | |
| 713 | + wp_enqueue_script( 'codoc-editor-onload', $path, array('jquery'), '', true ); | |
| 714 | + // I just want to insert this global variables but i don't know how i can do it.. | |
| 715 | + wp_localize_script( | |
| 716 | + 'codoc-editor-onload', | |
| 717 | + 'CODOCEDITOR', | |
| 718 | + array( | |
| 719 | + 'action' => 'codoc_shortcodes', | |
| 720 | + 'nonce' => wp_create_nonce( 'codoc_shortcodes' ), | |
| 721 | + | |
| 722 | + 'codoc_url' => $this->get_codoc_url(), | |
| 723 | + 'codoc_usercode' => get_option(CODOC_USERCODE_OPTION_NAME), | |
| 724 | + 'codoc_plugin_version' => CODOC_PLUGIN_VERSION, | |
| 725 | + 'codoc_sdk_path' => CODOC_SDK_PATH, | |
| 726 | + ) | |
| 727 | + ); | |
| 728 | + | |
| 729 | + // ここでtinymceのプラグイン追加 | |
| 730 | + //wp_enqueue_style( 'codoc-admin-style', plugins_url( 'codoc/src_mce/codoc-admin.css', __DIR__ ) ); | |
| 731 | + //add_editor_style( plugins_url( 'codoc/src_mce/codoc-admin.css', __DIR__ ) ); | |
| 732 | + wp_enqueue_style( 'codoc-admin-style', $this->get_codoc_url() . CODOC_SDK_PATH . '.tinymce.css?c=' . date('Ymd') ); | |
| 733 | + add_editor_style( $this->get_codoc_url() . CODOC_SDK_PATH . '.tinymce.css' ); | |
| 734 | + | |
| 735 | + add_filter( 'mce_external_plugins', function( $plugin_array ) { | |
| 736 | + //$plugin_array['codoc'] = plugins_url( 'codoc/src_mce/codoc-editor.js', __DIR__ ); | |
| 737 | + $plugin_array['codoc'] = $this->get_codoc_url() . CODOC_SDK_PATH . '.tinymce.js?c=' . date('Ymd'); | |
| 738 | + return $plugin_array; | |
| 739 | + } ); | |
| 740 | + add_filter( 'mce_buttons', function( $buttons ) { | |
| 741 | + array_push( $buttons, "|", "codoc" ); | |
| 742 | + return $buttons; | |
| 743 | + } ); | |
| 744 | + // 非SSL環境の場合、なぜかhttps -> httpsになってしまうので対策 | |
| 745 | + // コメントタグが除去されることがあるらしいのでvalid_elementsに必要なタグを追加 (EXPERIMENTAL) | |
| 746 | + add_filter( 'tiny_mce_before_init', function($settings) { | |
| 747 | + $settings['external_plugins'] = preg_replace('/"codoc":"http:/','"codoc":"https:',$settings['external_plugins']); | |
| 748 | + foreach (['valid_elements','extended_valid_elements'] as $valid_elements) { | |
| 749 | + if (isset($settings[$valid_elements])) { | |
| 750 | + $settings[$valid_elements] = $settings[$valid_elements] . ',div[*],p[*],img[*],--[*]'; | |
| 751 | + } | |
| 752 | + } | |
| 753 | + $CODOC_SETTINGS = get_option(CODOC_SETTINGS_OPTION_NAME); | |
| 754 | + if (isset($CODOC_SETTINGS["debug_params"])) { | |
| 755 | + $params_decoded = json_decode($CODOC_SETTINGS["debug_params"],true); | |
| 756 | + if (isset($params_decoded["mce_valid_elements"])) { | |
| 757 | + $settings['valid_elements'] = $params_decoded["mce_valid_elements"]; | |
| 758 | + $settings['extended_valid_elements'] = $params_decoded["mce_valid_elements"]; | |
| 759 | + } | |
| 760 | + } | |
| 761 | + | |
| 762 | + return $settings; | |
| 763 | + },1000000); | |
| 764 | + } | |
| 765 | + } | |
| 766 | + } ); | |
| 767 | + } | |
| 768 | + function update_codoc_authinfo($user) { | |
| 769 | + global $CODOC_SETTINGS; | |
| 770 | + global $CODOC_AUTHINFO; | |
| 771 | + if (property_exists($user,'connect_code') and $user->connect_code) { | |
| 772 | + $CODOC_SETTINGS = get_option(CODOC_SETTINGS_OPTION_NAME); | |
| 773 | + $CODOC_SETTINGS['codoc_connect_code'] = $user->connect_code; | |
| 774 | + $CODOC_SETTINGS['codoc_connect_registration_mode'] = $user->connect_has_permission_dedicated_account ? 'dedicated' : ''; | |
| 775 | + update_option(CODOC_SETTINGS_OPTION_NAME,$CODOC_SETTINGS); | |
| 776 | + } | |
| 777 | + return update_option(CODOC_AUTHINFO_OPTION_NAME,[ | |
| 778 | + 'email' => $user->email, | |
| 779 | + 'name' => $user->name, | |
| 780 | + 'profile_image_url' => $user->profile_image_url, | |
| 781 | + 'cover_image_url' => $user->cover_image_url, | |
| 782 | + 'connect_code' => property_exists($user,'connect_code') ? $user->connect_code : '', | |
| 783 | + 'connect_image_url' => property_exists($user,'connect_image_url') ? $user->connect_image_url : '', | |
| 784 | + ]); | |
| 785 | + $CODOC_AUTHINFO = get_option(CODOC_AUTHINFO_OPTION_NAME); | |
| 786 | + } | |
| 787 | + public function get_filtered_content($post_emulated) { | |
| 788 | + // 記事ページであることをエミュレートしてフィルター実行 | |
| 789 | + $post_backuped = null; | |
| 790 | + if (isset($GLOBALS['post'])) { | |
| 791 | + $post_backuped = $GLOBALS['post']; | |
| 792 | + } | |
| 793 | + $GLOBALS['post'] = $post_emulated; | |
| 794 | + | |
| 795 | + $wp_query_backuped = null; | |
| 796 | + if (isset($GLOBALS['wp_query'])) { | |
| 797 | + $wp_query_backuped = $GLOBALS['wp_query']; | |
| 798 | + $wp_query = $GLOBALS['wp_query']; | |
| 799 | + $wp_query->is_single = true; | |
| 800 | + $wp_query->is_singular = true; | |
| 801 | + # in_the_loop は記事ページでは通常有効っぽいので true指定 ex: wp_ulike | |
| 802 | + $wp_query->in_the_loop = true; | |
| 803 | + # これも追加しておく | |
| 804 | + $wp_query->is_main_query = true; | |
| 805 | + | |
| 806 | + $wp_query->post = $post_emulated; | |
| 807 | + $wp_query->queried_object = $post_emulated; | |
| 808 | + $wp_query->queried_object_id = $post_emulated->id; | |
| 809 | + | |
| 810 | + $GLOBALS['wp_query'] = $wp_query; | |
| 811 | + } | |
| 812 | + // $this->the_content でオリジナルを返してもらうため | |
| 813 | + $this->do_not_filter_the_content = true; | |
| 814 | + $content = preg_replace( | |
| 815 | + '/ (\/)?wp:codoc\/codoc-block /',' \\1wptmp:codoc/codoc-block ', | |
| 816 | + $post_emulated->post_content | |
| 817 | + ); | |
| 818 | + #$content = do_shortcode( $content ); | |
| 819 | + $content_filtered = apply_filters( 'the_content', $content); | |
| 820 | + $this->do_not_filter_the_content = false; | |
| 821 | + | |
| 822 | + $GLOBALS['post'] = $post_backuped; | |
| 823 | + | |
| 824 | + if ($wp_query_backuped) { | |
| 825 | + $GLOBALS['wp_query'] = $wp_query_backuped; | |
| 826 | + } | |
| 827 | + | |
| 828 | + $content_filtered = preg_replace( | |
| 829 | + '/ (\/)?wptmp:codoc\/codoc-block /',' \\1wp:codoc/codoc-block ', | |
| 830 | + $content_filtered | |
| 831 | + ); | |
| 832 | + return $content_filtered; | |
| 833 | + } | |
| 834 | + | |
| 835 | + public function save_post($post_ID,$post,$update) { | |
| 836 | + if (preg_match('/^(auto-draft|inherit)$/',$post->post_status)) { | |
| 837 | + return; | |
| 838 | + } | |
| 839 | + $entryCode = get_post_meta($post_ID,'codoc_entry_code',true); | |
| 840 | + $codoc_settings = get_option(CODOC_SETTINGS_OPTION_NAME); | |
| 841 | + $post_content = (isset($codoc_settings['do_not_filter_the_content']) and $codoc_settings['do_not_filter_the_content']) ? | |
| 842 | + $post->post_content : $this->get_filtered_content($post); | |
| 843 | + $res = $this->util->sync_entry([ | |
| 844 | + "post_title" => $post->post_title, | |
| 845 | + "post_content" => $post_content, | |
| 846 | + // password が設定されてる場合は限定公開にする | |
| 847 | + "post_status" => $post->post_status == 'publish' ? ($post->post_password ? 2 : 1) : 0, | |
| 848 | + "post_permalink" => get_permalink($post_ID), | |
| 849 | + "codoc_entry_code" => $entryCode, | |
| 850 | + "codoc_settings" => $codoc_settings, | |
| 851 | + ],[ | |
| 852 | + "usercode" => get_option(CODOC_USERCODE_OPTION_NAME), | |
| 853 | + "token" => get_option(CODOC_TOKEN_OPTION_NAME), | |
| 854 | + ]); | |
| 855 | + | |
| 856 | + if (is_object($res) and $res->status and !$entryCode) { | |
| 857 | + update_post_meta($post_ID,'codoc_entry_code',$res->entry->code); | |
| 858 | + } | |
| 859 | + return true; | |
| 860 | + } | |
| 861 | + function updated_post_meta( $meta_ID, $post_ID, $meta_key ) { | |
| 862 | + // スルーされてる場合は他のメタ情報更新のタイミングにサムネイルをアップロード | |
| 863 | + $has_to_resend = get_post_meta($post_ID,'codoc_post_thumbnail_invoking_entry_code',true); | |
| 864 | + $has_to_resendi = get_post_meta($post_ID,'icodoc_post_thumbnail_invoking_entry_code',true); | |
| 865 | + if ($has_to_resend != 1 and $meta_key != '_thumbnail_id') { | |
| 866 | + return; | |
| 867 | + } | |
| 868 | + if ( has_post_thumbnail($post_ID) ) { | |
| 869 | + // 新規投稿の場合、タイミングによってはcodocEntryCodeが取得できないので一旦スルーする | |
| 870 | + if (!get_post_meta($post_ID,'codoc_entry_code',true)) { | |
| 871 | + update_post_meta($post_ID,'codoc_post_thumbnail_invoking_entry_code',1); | |
| 872 | + return; | |
| 873 | + } else { | |
| 874 | + update_post_meta($post_ID,'codoc_post_thumbnail_invoking_entry_code',0); | |
| 875 | + } | |
| 876 | + $attachment = wp_get_attachment_metadata( get_post_thumbnail_id($post_ID)); | |
| 877 | + $upload_dir = wp_upload_dir(); | |
| 878 | + $file_path = sprintf ('%s/%s',$upload_dir['basedir'],$attachment['file']); | |
| 879 | + | |
| 880 | + return $this->util->post_thumbnail([ | |
| 881 | + "file_path" => $file_path, | |
| 882 | + "boundary" => wp_generate_password(24), | |
| 883 | + "codoc_entry_code" => get_post_meta($post_ID,'codoc_entry_code',true), | |
| 884 | + ],[ | |
| 885 | + "usercode" => get_option(CODOC_USERCODE_OPTION_NAME), | |
| 886 | + "token" => get_option(CODOC_TOKEN_OPTION_NAME), | |
| 887 | + ]); | |
| 888 | + } | |
| 889 | + } | |
| 890 | + function deleted_post_meta( $meta_ids, $post_ID, $meta_key,$meta_value ) { | |
| 891 | + if ($meta_key != '_thumbnail_id') { | |
| 892 | + return; | |
| 893 | + } | |
| 894 | + return $this->util->reset_thumbnail( | |
| 895 | + ["codoc_entry_code" => get_post_meta($post_ID,'codoc_entry_code',true)], | |
| 896 | + [ | |
| 897 | + "usercode" => get_option(CODOC_USERCODE_OPTION_NAME), | |
| 898 | + "token" => get_option(CODOC_TOKEN_OPTION_NAME), | |
| 899 | + ] | |
| 900 | + ); | |
| 901 | + } | |
| 902 | + | |
| 903 | + function the_content( $post_content ) { | |
| 904 | + if ( is_single() && in_the_loop() && is_main_query() ) { | |
| 905 | + } | |
| 906 | + // get_filtered_content から do_not_filter_the_contentを有効にされるパターン | |
| 907 | + // オプションの設定値の意味合い(他のフィルタを無視)とは違うため注意 | |
| 908 | + if ($this->do_not_filter_the_content) { | |
| 909 | + return $post_content; | |
| 910 | + } | |
| 911 | + $post = get_post(); | |
| 912 | + # is_amp で実装しているテンプレート用 | |
| 913 | + $is_amp_endpoint = (function_exists('is_amp_endpoint') && is_amp_endpoint()) ? true : | |
| 914 | + ((function_exists('is_amp') && is_amp()) ? true : false); | |
| 915 | + return $this->util->filter_content([ | |
| 916 | + "post_content" => $post_content, | |
| 917 | + "preview" => is_preview(), | |
| 918 | + "codoc_entry_code" => get_post_meta($post->ID,'codoc_entry_code',true), | |
| 919 | + "codoc_settings" => get_option(CODOC_SETTINGS_OPTION_NAME), | |
| 920 | + "is_amp_endpoint" => $is_amp_endpoint, | |
| 921 | + "post_permalink" => get_permalink($post->ID), | |
| 922 | + "codoc_support_entry_code" => get_option(CODOC_SUPPORT_ENTRYCODE_OPTION_NAME), | |
| 923 | + ]); | |
| 924 | + } | |
| 925 | + | |
| 926 | + function excerpt_allowed_blocks ($allowed_blocks) { | |
| 927 | + if (is_array($allowed_blocks)) { | |
| 928 | + array_push($allowed_blocks,'codoc/codoc-block'); | |
| 929 | + } | |
| 930 | + return $allowed_blocks; | |
| 931 | + } | |
| 559 | 932 | } |
| 560 | - | |