| @@ -1,1241 +1,493 @@ | ||
| 1 | 1 | <?php |
| 2 | -require_once(plugin_dir_path( __FILE__ ) .'class-codoc-util.php'); | |
| 2 | + | |
| 3 | 3 | final class Codoc { |
| 4 | - public function __construct() { | |
| 5 | - add_action('plugins_loaded', [$this,'codoc_load_textdomain']); | |
| 6 | - if (is_admin()) { | |
| 7 | - // setting | |
| 8 | - $this->add_settings(); | |
| 9 | - } | |
| 10 | 4 | |
| 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; | |
| 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 | + } | |
| 16 | 14 | |
| 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 | - // ショーココードの退避 | |
| 28 | - if (isset($CODOC_SETTINGS['shortcode_evacuation']) and $CODOC_SETTINGS['shortcode_evacuation']) { | |
| 29 | - add_filter('the_content',[$this,'the_content_shortcode_evacuations'],0); | |
| 30 | - } | |
| 31 | - add_filter('the_content',[$this,'the_content'],$priority); | |
| 32 | - // 2021-09-24 the_content を利用しない場合は個別に呼び出ししてもらう | |
| 33 | - add_filter('codoc_the_content',[$this,'the_content'],$priority); | |
| 34 | - | |
| 35 | - // 2020-07-25 excerpt対応 | |
| 36 | - add_filter('excerpt_allowed_blocks',[$this,'excerpt_allowed_blocks']); | |
| 15 | + // データ同期 (save_postはis_adminで実行されないケースがある) | |
| 16 | + add_action( 'save_post', [$this,'save_post'], 20, 3 ); | |
| 17 | + add_action( 'added_post_meta', [$this,'updated_post_meta'], 10, 3 ); | |
| 18 | + add_action( 'updated_post_meta',[$this,'updated_post_meta'], 10, 3 ); | |
| 19 | + add_action( 'deleted_post_meta',[$this,'deleted_post_meta'], 10, 4 ); | |
| 20 | + | |
| 21 | + if (is_admin()) { | |
| 22 | + // gutenberg | |
| 23 | + require_once 'src/init.php'; | |
| 24 | + // tinymce | |
| 25 | + $this->add_mce(); | |
| 26 | + add_action('wp_loaded', function() { | |
| 27 | + wp_localize_script('wordpress-cgb-block-js', 'OPTIONS', array( | |
| 28 | + 'codoc_url' => CODOC_URL, | |
| 29 | + 'codoc_usercode' => get_option(CODOC_USERCODE_OPTION_NAME) | |
| 30 | + )); | |
| 31 | + }); | |
| 32 | + return $this; | |
| 33 | + } | |
| 37 | 34 | |
| 38 | - // テーマが allowed_block_types_all で制限している場合に codoc ブロックを許可リストに追加 | |
| 39 | - add_filter('allowed_block_types_all', [$this,'allowed_block_types_all'], 100, 2); | |
| 40 | - | |
| 41 | - $auth_info = get_option(CODOC_AUTHINFO_OPTION_NAME); | |
| 35 | + // codocのJS登録 | |
| 36 | + add_action( 'wp_enqueue_scripts', function() { | |
| 37 | + wp_enqueue_script( 'codoc-injector-js', CODOC_URL . '/js/cms.js' ); | |
| 38 | + }); | |
| 39 | + // 登録したscriptタグに属性をつける | |
| 40 | + add_filter('script_loader_tag', [$this,'modifier_script_tag'],10,2); | |
| 41 | + // tinymce用のショートコード | |
| 42 | + add_shortcode('codoc', [$this, 'injector_shortcode']); | |
| 42 | 43 | |
| 43 | - if ($auth_info) { | |
| 44 | - // データ同期 (save_postはis_adminで実行されないケースがある) | |
| 45 | - add_action( 'save_post', [$this,'save_post'], 20, 3 ); | |
| 46 | - add_action( 'added_post_meta', [$this,'updated_post_meta'], 10, 3 ); | |
| 47 | - add_action( 'updated_post_meta',[$this,'updated_post_meta'], 10, 3 ); | |
| 48 | - add_action( 'deleted_post_meta',[$this,'deleted_post_meta'], 10, 4 ); | |
| 49 | - // 文字数バリデーションエラーの管理画面通知 | |
| 50 | - add_action( 'admin_notices', [$this,'show_validation_notices'] ); | |
| 51 | - } | |
| 52 | - global $pagenow; | |
| 53 | - //管理画面でcodoc認証があり投稿画面の場合 | |
| 54 | - if (is_admin() and $auth_info and preg_match('/^post/',$pagenow)) { | |
| 55 | - // Gutenberg のサポートがない場合は何もしない | |
| 56 | - if ( function_exists( 'register_block_type' ) ) { | |
| 57 | - // gutenberg - init.php handles all block registration and script enqueuing | |
| 58 | - require_once 'src/init.php'; | |
| 59 | - } | |
| 60 | - // tinymce | |
| 61 | - $this->add_mce(); | |
| 62 | - } elseif(is_admin() and $auth_info and preg_match('/^edit/',$pagenow)) { | |
| 63 | - // 記事編集ページ | |
| 64 | - } elseif(!is_admin()) { // ブログ画面 | |
| 65 | - // codocのJS登録 | |
| 66 | - add_action( 'wp_enqueue_scripts', function() { | |
| 67 | - $CODOC_SETTINGS = get_option(CODOC_SETTINGS_OPTION_NAME); | |
| 68 | - $load_script_condition = ''; | |
| 69 | - if (isset($CODOC_SETTINGS["debug_params"]) and | |
| 70 | - $params_decoded = json_decode($CODOC_SETTINGS["debug_params"],true) and | |
| 71 | - isset($params_decoded["load_script_condition"]) | |
| 72 | - ) { | |
| 73 | - $load_script_condition = $params_decoded["load_script_condition"]; | |
| 74 | - } | |
| 75 | - if ($load_script_condition == 'not_list_page') { | |
| 76 | - // not_list_pageの場合はトップページ等でスクリプトをロードしない | |
| 77 | - if (!(is_archive() or is_category() or is_home() or is_front_page())) { | |
| 78 | - wp_enqueue_script( 'codoc-injector-js', $this->get_codoc_url() . '/js/cms.js' ); | |
| 79 | - } | |
| 80 | - } else { | |
| 81 | - wp_enqueue_script( 'codoc-injector-js', $this->get_codoc_url() . '/js/cms.js' ); | |
| 82 | - } | |
| 83 | - }); | |
| 84 | - //登録したscriptタグに属性をつける | |
| 85 | - add_filter('script_loader_tag', [$this,'modifier_script_tag'],10,2); | |
| 86 | - // tinymce用のショートコード | |
| 87 | - add_shortcode('codoc', [$this, 'injector_shortcode']); | |
| 88 | - // テーマをbodyにも反映 | |
| 89 | - add_filter('body_class', function($classes) { | |
| 90 | - $CODOC_SETTINGS = get_option(CODOC_SETTINGS_OPTION_NAME); | |
| 91 | - // デフォルトを変更 | |
| 92 | - $css_path = 'rainbow-square'; | |
| 93 | - if (isset($CODOC_SETTINGS["css_path"]) and $CODOC_SETTINGS["css_path"]) { | |
| 94 | - $css_path = $CODOC_SETTINGS["css_path"]; | |
| 95 | - } | |
| 96 | - array_push($classes,sprintf( "codoc-theme-%s",$css_path)); | |
| 97 | - return $classes; | |
| 98 | - }); | |
| 99 | - | |
| 100 | - } | |
| 101 | - return $this; | |
| 102 | - } | |
| 103 | - function codoc_load_textdomain() { | |
| 104 | - load_plugin_textdomain('codoc', false, dirname(plugin_basename( __FILE__ )) . '/languages/'); | |
| 105 | - } | |
| 106 | - public function get_codoc_url() { | |
| 107 | - $CODOC_SETTINGS = get_option(CODOC_SETTINGS_OPTION_NAME); | |
| 108 | - if (isset($CODOC_SETTINGS["debug_params"])) { | |
| 109 | - $params_decoded = json_decode($CODOC_SETTINGS["debug_params"],true); | |
| 110 | - if (isset($params_decoded["codoc_url"])) { | |
| 111 | - return $params_decoded["codoc_url"]; | |
| 112 | - } | |
| 113 | - } | |
| 114 | - return CODOC_URL; | |
| 115 | - } | |
| 116 | - public function modifier_script_tag($tag,$handle) { | |
| 117 | - if($handle !== 'codoc-injector-js') { | |
| 118 | - return $tag; | |
| 119 | - } | |
| 120 | - $CODOC_SETTINGS = get_option(CODOC_SETTINGS_OPTION_NAME); | |
| 121 | - $data_css = ''; | |
| 122 | - if ($css_path = $CODOC_SETTINGS['css_path']) { | |
| 123 | - $data_css = sprintf(' data-css="%s" ',$css_path); | |
| 124 | - } | |
| 125 | - // connect用パラメータ | |
| 126 | - $connect_attributes = ''; | |
| 127 | - if (isset($CODOC_SETTINGS["codoc_connect_code"])) { | |
| 128 | - $connect_code = $CODOC_SETTINGS["codoc_connect_code"]; | |
| 129 | - $connect_attributes = sprintf(' data-connect-code="%s"',$connect_code); | |
| 130 | - $tag = preg_replace('/cms\.js/','cms-connect.js',$tag); | |
| 131 | - } | |
| 132 | - if (isset($CODOC_SETTINGS["codoc_connect_registration_mode"]) and $CODOC_SETTINGS["codoc_connect_registration_mode"]) { | |
| 133 | - $connect_attributes = $connect_attributes . | |
| 134 | - sprintf(' data-connect-registration-mode="%s"',$CODOC_SETTINGS["codoc_connect_registration_mode"]); | |
| 135 | - } | |
| 44 | + // paywall用の本文非表示化とcodocタグへの属性追加 | |
| 45 | + add_filter('the_content',[$this,'the_content']); | |
| 46 | + return $this; | |
| 47 | + } | |
| 48 | + | |
| 49 | + public function modifier_script_tag($tag,$handle) { | |
| 50 | + if($handle !== 'codoc-injector-js') { | |
| 51 | + return $tag; | |
| 52 | + } | |
| 53 | + $CODOC_SETTINGS = get_option(CODOC_SETTINGS_OPTION_NAME); | |
| 54 | + $data_css = ''; | |
| 55 | + if ($css_path = $CODOC_SETTINGS['css_path']) { | |
| 56 | + $data_css = sprintf(' data-css="%s" ',$css_path); | |
| 57 | + } | |
| 58 | + return str_replace(' src=', $data_css . ' defer src=', $tag); | |
| 59 | + } | |
| 60 | + public function injector_shortcode($atts) { | |
| 61 | + global $post; | |
| 62 | + ob_start(); | |
| 63 | + require 'views/codoc-injector.php'; | |
| 64 | + return ob_get_clean(); | |
| 65 | + } | |
| 66 | + public function callAPI($method,$path,$body = [],$headers = []) { | |
| 67 | + $usercode = get_option(CODOC_USERCODE_OPTION_NAME); | |
| 68 | + $token = get_option(CODOC_TOKEN_OPTION_NAME); | |
| 136 | 69 | |
| 137 | - $usercode_attributes = ''; | |
| 138 | - $user_code = get_option(CODOC_USERCODE_OPTION_NAME); | |
| 139 | - if ($user_code) { | |
| 140 | - $usercode_attributes = sprintf(' data-usercode="%s"',$user_code); | |
| 141 | - } | |
| 70 | + $headers['X-CodocToken'] = $token; | |
| 71 | + $host = CODOC_URL; | |
| 72 | + $sslverify = true; | |
| 142 | 73 | |
| 143 | - $setting_attributes = ''; | |
| 144 | - if (isset($CODOC_SETTINGS['codoc_script_tag_attributes']) and $CODOC_SETTINGS['codoc_script_tag_attributes']) { | |
| 145 | - $setting_attributes = $CODOC_SETTINGS['codoc_script_tag_attributes']; | |
| 146 | - } | |
| 147 | - //return str_replace(' src=', $data_css . ' defer src=', $tag); | |
| 148 | - return preg_replace('/(src=[^>]+)/',' ${1} ' . $data_css . $connect_attributes . $usercode_attributes . $setting_attributes . ' defer',$tag); | |
| 149 | - } | |
| 150 | - public function injector_shortcode($atts) { | |
| 151 | - global $post; | |
| 152 | - ob_start(); | |
| 153 | - require 'views/codoc-injector.php'; | |
| 154 | - return ob_get_clean(); | |
| 155 | - } | |
| 156 | - # settings / 設定関連 | |
| 157 | - public function add_settings() { | |
| 158 | - global $CODOC_SETTINGS; | |
| 159 | - add_action( 'admin_menu' ,function(){ | |
| 160 | - add_options_page( | |
| 161 | - __('codoc Settings','codoc'), //ページタイトル | |
| 162 | - 'codoc', //設定メニューに表示されるメニュータイトル | |
| 163 | - 'edit_users', //権限 | |
| 164 | - 'codoc', //設定ページのURL。options-general.php?page=codoc | |
| 165 | - function() { | |
| 166 | - echo '<div class="codoc-settings">'; | |
| 167 | - echo '<form method="post" action="options.php">'; | |
| 168 | - settings_fields( 'codoc_option_group' ); | |
| 169 | - do_settings_sections( 'codoc' ); | |
| 170 | - submit_button(); // 送信ボタン | |
| 171 | - echo '</form></div>'; | |
| 172 | - } | |
| 173 | - ); | |
| 174 | - }); | |
| 74 | + if (preg_match('/local/',CODOC_URL)) { | |
| 75 | + $sslverify = false; | |
| 76 | + $host = 'https://host.docker.internal'; | |
| 77 | + } | |
| 78 | + $url = sprintf("%s/api/v1/cms/%s%s",$host,$usercode,$path); | |
| 79 | + $http = new WP_Http(); | |
| 80 | + try { | |
| 81 | + $response = $http->request( | |
| 82 | + $url, | |
| 83 | + [ | |
| 84 | + 'sslverify' => $sslverify, | |
| 85 | + 'method' => $method, | |
| 86 | + 'timeout' => 10, | |
| 87 | + 'headers' => $headers, | |
| 88 | + 'body' => $body, | |
| 89 | + ] | |
| 90 | + ); | |
| 91 | + if ( is_wp_error($response) || $response['response']['code'] != 200 ) { | |
| 92 | + //何もしない | |
| 93 | + //var_dump( $response ); | |
| 94 | + } | |
| 95 | + } catch(Exception $e) { | |
| 96 | + } | |
| 97 | + | |
| 98 | + if (!is_wp_error($response) and | |
| 99 | + isset($response['body']) and | |
| 100 | + is_string($response['body']) and | |
| 101 | + is_array(json_decode($response['body'], true)) and | |
| 102 | + (json_last_error() == JSON_ERROR_NONE)) { | |
| 103 | + return json_decode($response['body']); | |
| 104 | + } else { | |
| 105 | + return null; | |
| 106 | + } | |
| 107 | + } | |
| 108 | + # settings / 設定関連 | |
| 109 | + public function add_settings() { | |
| 110 | + global $CODOC_SETTINGS; | |
| 111 | + add_action( 'admin_menu' ,function(){ | |
| 112 | + add_options_page( | |
| 113 | + 'codoc の設定', //ページタイトル | |
| 114 | + 'codoc', //設定メニューに表示されるメニュータイトル | |
| 115 | + 'edit_users', //権限 | |
| 116 | + 'codoc', //設定ページのURL。options-general.php?page=codoc | |
| 117 | + function() { | |
| 118 | + echo '<div clas="wrap">'; | |
| 119 | + echo '<form method="post" action="options.php">'; | |
| 120 | + settings_fields( 'codoc_option_group' ); | |
| 121 | + do_settings_sections( 'codoc' ); | |
| 122 | + submit_button(); // 送信ボタン | |
| 123 | + echo '</div>'; | |
| 124 | + } | |
| 125 | + ); | |
| 126 | + }); | |
| 175 | 127 | |
| 176 | - add_action('admin_print_styles', 'codoc_admin_styles'); | |
| 177 | - function codoc_admin_styles($hook) { | |
| 178 | - wp_enqueue_style('codoc-options-style', plugins_url( 'codoc/css/codoc-options.css', __DIR__ )); | |
| 179 | - } | |
| 128 | + add_action( "admin_init", function() { | |
| 129 | + // codocからの認証データ処理 | |
| 130 | + if (isset($_GET['page']) and $_GET['page'] == 'codoc' and isset($_GET['fetch_token_key'])) { | |
| 131 | + $key = sanitize_text_field($_GET['fetch_token_key']); | |
| 132 | + $usercode = sanitize_text_field($_GET['usercode']); | |
| 133 | + update_option(CODOC_USERCODE_OPTION_NAME,$usercode); | |
| 134 | + $data = $this->callAPI('GET','/token',[ "fetch_token_key" => $key ]); | |
| 135 | + if ($data->status and $token = $data->token) { | |
| 136 | + update_option(CODOC_TOKEN_OPTION_NAME,$token); | |
| 137 | + $data = $this->callAPI('GET',''); | |
| 138 | + if ($data->status and $user = $data->user) { | |
| 139 | + update_option(CODOC_AUTHINFO_OPTION_NAME,[ | |
| 140 | + 'email' => $user->email, | |
| 141 | + 'name' => $user->name, | |
| 142 | + ]); | |
| 143 | + } | |
| 144 | + #$current_url = (empty($_SERVER['HTTPS']) ? 'http://' : 'https://').$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; | |
| 145 | + #$current_url = preg_replace('/(.*)fetch_token_key.*/','${1}&codoc_auth_finished=1',$current_url); | |
| 146 | + add_settings_error( 'general', 'settings_updated', __( 'OKKK' ), 'success' ); | |
| 147 | + $current_url = admin_url('options-general.php') . '?page=codoc&codoc_auth_finished=1'; | |
| 148 | + | |
| 149 | + wp_redirect( $current_url); | |
| 150 | + exit; | |
| 151 | + } | |
| 152 | + } | |
| 180 | 153 | |
| 181 | - // notification を削除する Ajax 処理 | |
| 182 | - add_action('wp_ajax_dismiss_codoc_notification', function() { | |
| 183 | - delete_transient('codoc_api_notification'); | |
| 184 | - wp_die(); | |
| 185 | - }); | |
| 154 | + global $CODOC_USERCODE; | |
| 155 | + global $CODOC_SETTINGS; | |
| 156 | + global $CODOC_TOKEN; | |
| 157 | + global $CODOC_AUTHINFO; | |
| 158 | + $CODOC_USERCODE = get_option(CODOC_USERCODE_OPTION_NAME); | |
| 159 | + $CODOC_SETTINGS = get_option(CODOC_SETTINGS_OPTION_NAME); | |
| 160 | + $CODOC_TOKEN = get_option(CODOC_TOKEN_OPTION_NAME); | |
| 161 | + $CODOC_AUTHINFO = get_option(CODOC_AUTHINFO_OPTION_NAME); | |
| 162 | + if( !$CODOC_SETTINGS ) { | |
| 163 | + //デフォルト値 | |
| 164 | + $CODOC_SETTINGS = array( | |
| 165 | + 'css_path' => '', | |
| 166 | + ); | |
| 167 | + update_option( CODOC_SETTINGS_OPTION_NAME, $CODOC_SETTINGS ); | |
| 168 | + } | |
| 169 | + | |
| 170 | + add_settings_section( | |
| 171 | + 'setting_section_id', // id | |
| 172 | + 'codoc 設定ページ', // title | |
| 173 | + function (){}, // callback | |
| 174 | + 'codoc' // page | |
| 175 | + ); | |
| 176 | + // 認証がある場合 | |
| 177 | + if ($CODOC_AUTHINFO) { | |
| 178 | + register_setting( | |
| 179 | + 'codoc_option_group', // option group | |
| 180 | + CODOC_SETTINGS_OPTION_NAME // option name(DB) | |
| 181 | + ); | |
| 182 | + add_settings_field( | |
| 183 | + 'css_path', // id | |
| 184 | + 'カスタマイズ用CSSパス:', // title | |
| 185 | + function() { | |
| 186 | + global $CODOC_SETTINGS; | |
| 187 | + echo sprintf('<input type="text" name="%s[css_path]" value="%s">',CODOC_SETTINGS_OPTION_NAME,$CODOC_SETTINGS['css_path']); | |
| 188 | + }, | |
| 189 | + 'codoc', //page | |
| 190 | + 'setting_section_id' //Section | |
| 191 | + ); | |
| 186 | 192 | |
| 187 | - // 管理画面で notification の dismiss を処理する JavaScript | |
| 188 | - add_action('admin_footer', function() { | |
| 189 | - ?> | |
| 190 | - <script type="text/javascript"> | |
| 191 | - jQuery(document).ready(function($) { | |
| 192 | - // codoc notification の×ボタンがクリックされたときの処理 | |
| 193 | - $(document).on('click', '.codoc-notification .notice-dismiss', function() { | |
| 194 | - $.post(ajaxurl, { | |
| 195 | - action: 'dismiss_codoc_notification' | |
| 196 | - }); | |
| 197 | - }); | |
| 198 | - }); | |
| 199 | - </script> | |
| 200 | - <?php | |
| 201 | - }); | |
| 193 | + register_setting( | |
| 194 | + 'codoc_option_group', // option group | |
| 195 | + CODOC_USERCODE_OPTION_NAME | |
| 196 | + ); | |
| 197 | + register_setting( | |
| 198 | + 'codoc_option_group', // option group | |
| 199 | + CODOC_TOKEN_OPTION_NAME | |
| 200 | + ); | |
| 201 | + | |
| 202 | + if (isset($_GET['codoc_auth_finished']) and $_GET['codoc_auth_finished']) { | |
| 203 | + add_settings_error( 'general', 'settings_updated', __( 'codocの認証が完了しました' ), 'success' ); | |
| 204 | + } | |
| 205 | + | |
| 206 | + add_settings_field( | |
| 207 | + 'codoc_auth', | |
| 208 | + '認証', | |
| 209 | + function() { | |
| 210 | + global $CODOC_USERCODE; | |
| 211 | + global $CODOC_TOKEN; | |
| 212 | + global $CODOC_AUTHINFO; | |
| 213 | + $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';"; | |
| 202 | 214 | |
| 203 | - add_action( "admin_init", function() { | |
| 204 | - global $CODOC_USERCODE; | |
| 205 | - global $CODOC_SETTINGS; | |
| 206 | - global $CODOC_TOKEN; | |
| 207 | - global $CODOC_AUTHINFO; | |
| 208 | - // codocからの認証データ処理 | |
| 209 | - if (current_user_can('edit_posts') && !isset($_GET['confirm_token']) and isset($_GET['page']) and $_GET['page'] == 'codoc' and isset($_GET['fetch_token_key'])) { | |
| 210 | - $current_url = admin_url('options-general.php') . '?page=codoc'; | |
| 211 | - $key = sanitize_text_field($_GET['fetch_token_key']); | |
| 212 | - $usercode = sanitize_text_field($_GET['usercode']); | |
| 215 | + echo sprintf('<p><font color="green" id="codoc-auth-message"><strong>%s</strong> で認証済</font></p>',$CODOC_AUTHINFO['email']); | |
| 213 | 216 | |
| 214 | - // 確認画面の表示 | |
| 215 | - if ( | |
| 216 | - !isset($_GET['_wpnonce']) or | |
| 217 | - (isset($_GET['_wpnonce']) and !wp_verify_nonce($_GET['_wpnonce'],'fetch_token_nonce')) | |
| 218 | - ) { | |
| 219 | - wp_redirect($current_url . | |
| 220 | - sprintf("&confirm_token=1&fetch_token_key=%s&usercode=%s",$key,$usercode)); | |
| 221 | - exit; | |
| 222 | - } | |
| 223 | - update_option(CODOC_USERCODE_OPTION_NAME,$usercode); | |
| 224 | - //$data = $this->callAPI('GET','/token',[ "fetch_token_key" => $key ]); | |
| 225 | - $data = $this->util->get_token([ "fetch_token_key" => $key ],["usercode" => $usercode, "token" => "1"]); | |
| 226 | - if ($data->status and property_exists($data,'token') and $token = $data->token) { | |
| 227 | - update_option(CODOC_TOKEN_OPTION_NAME,$token); | |
| 228 | - //$data = $this->callAPI('GET',''); | |
| 229 | - $data = $this->util->get_user_info([],["usercode" => $usercode, "token" => $token]); | |
| 230 | - | |
| 231 | - if ($data->status and $user = $data->user) { | |
| 232 | - $this->update_codoc_authinfo($user); | |
| 233 | - } | |
| 234 | - #$current_url = (empty($_SERVER['HTTPS']) ? 'http://' : 'https://').$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; | |
| 235 | - #$current_url = preg_replace('/(.*)fetch_token_key.*/','${1}&codoc_auth_finished=1',$current_url); | |
| 236 | - //add_settings_error( 'general', 'settings_updated', __( 'OK' ), 'success' ); | |
| 217 | + echo sprintf('<input id="codoc-usercode" type="hidden" name="%s" value="%s">',CODOC_USERCODE_OPTION_NAME,$CODOC_USERCODE); | |
| 218 | + echo sprintf('<input id="codoc-token" type="hidden" name="%s" value="%s">',CODOC_TOKEN_OPTION_NAME,$CODOC_TOKEN); | |
| 219 | + echo sprintf('<p>認証を<a href="javascript:void(0);" onClick="' . $script . '">解除する</p>'); | |
| 220 | + }, | |
| 221 | + 'codoc', | |
| 222 | + 'setting_section_id' | |
| 223 | + ); | |
| 224 | + | |
| 225 | + } | |
| 237 | 226 | |
| 238 | - $current_url = $current_url . '&codoc_auth_finished=1'; | |
| 239 | - wp_redirect( $current_url); | |
| 240 | - exit; | |
| 241 | - } | |
| 242 | - } | |
| 243 | - $CODOC_USERCODE = get_option(CODOC_USERCODE_OPTION_NAME); | |
| 244 | - $CODOC_SETTINGS = get_option(CODOC_SETTINGS_OPTION_NAME); | |
| 245 | - $CODOC_TOKEN = get_option(CODOC_TOKEN_OPTION_NAME); | |
| 246 | - $CODOC_AUTHINFO = get_option(CODOC_AUTHINFO_OPTION_NAME); | |
| 247 | - if( !$CODOC_SETTINGS ) { | |
| 248 | - //デフォルト値 | |
| 249 | - $CODOC_SETTINGS = array( | |
| 250 | - 'css_path' => '', | |
| 251 | - ); | |
| 252 | - update_option( CODOC_SETTINGS_OPTION_NAME, $CODOC_SETTINGS ); | |
| 253 | - } | |
| 254 | - if (!isset($CODOC_SETTINGS['str_replace_binded_url_from'])) { | |
| 255 | - $CODOC_SETTINGS['str_replace_binded_url_from'] = ''; | |
| 256 | - } | |
| 257 | - if (!isset($CODOC_SETTINGS['str_replace_binded_url_to'])) { | |
| 258 | - $CODOC_SETTINGS['str_replace_binded_url_to'] = ''; | |
| 259 | - } | |
| 260 | - if (!isset($CODOC_SETTINGS['str_before_codoc_tag'])) { | |
| 261 | - $CODOC_SETTINGS['str_before_codoc_tag'] = ''; | |
| 262 | - } | |
| 263 | - if (!isset($CODOC_SETTINGS['str_after_codoc_tag'])) { | |
| 264 | - $CODOC_SETTINGS['str_after_codoc_tag'] = ''; | |
| 265 | - } | |
| 227 | + // ない場合 | |
| 228 | + if (!$CODOC_AUTHINFO and !isset($_GET['auth_by_myself'])) { | |
| 229 | + add_settings_field( | |
| 230 | + 'codoc_auth', | |
| 231 | + '認証', | |
| 232 | + function() { | |
| 233 | + | |
| 234 | + //$current_url = (empty($_SERVER['HTTPS']) ? 'http://' : 'https://').$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; | |
| 235 | + $current_url = admin_url('options-general.php') . '?page=codoc'; | |
| 236 | + $login_url = sprintf("location.href='%s'",CODOC_URL . '/me/token?from_wp=1&return_url=' . urlencode($current_url)); | |
| 237 | + $register_url = sprintf("location.href='%s'",CODOC_URL . '/register?from_wp=1&return_url=' . urlencode($current_url)); | |
| 238 | + // submitを消しておく | |
| 239 | + echo ('<script type="text/javascript">window.onload=function(){document.getElementById(\'submit\').style.display = \'none\'}</script>'); | |
| 240 | + echo ('codocをWordPressでご利用いただくには認証が必要です。<br />'); | |
| 241 | + echo sprintf('ユーザーコードとAPIトークンを<a href="%s&auth_by_myself=1">直接入力</a>することでも認証できます。<br /><br />',$current_url); | |
| 242 | + 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); | |
| 243 | + | |
| 266 | 244 | |
| 267 | - if (!isset($CODOC_SETTINGS['always_show_support'])) { | |
| 268 | - $CODOC_SETTINGS['always_show_support'] = '0'; | |
| 269 | - } | |
| 270 | - if (!isset($CODOC_SETTINGS['show_support_message'])) { | |
| 271 | - $CODOC_SETTINGS['show_support_message'] = ''; | |
| 272 | - } | |
| 273 | - if (!isset($CODOC_SETTINGS['show_support_categories'])) { | |
| 274 | - $CODOC_SETTINGS['show_support_categories'] = ''; | |
| 275 | - } | |
| 276 | - if (!isset($CODOC_SETTINGS['show_support_location'])) { | |
| 277 | - $CODOC_SETTINGS['show_support_location'] = 'bottom'; | |
| 278 | - } | |
| 279 | - if (!isset($CODOC_SETTINGS['do_not_filter_the_content'])) { | |
| 280 | - $CODOC_SETTINGS['do_not_filter_the_content'] = '0'; | |
| 281 | - } | |
| 282 | - if (!isset($CODOC_SETTINGS['shortcode_evacuation'])) { | |
| 283 | - $CODOC_SETTINGS['shortcode_evacuation'] = '0'; | |
| 284 | - } | |
| 245 | + }, | |
| 246 | + 'codoc', | |
| 247 | + 'setting_section_id' | |
| 248 | + ); | |
| 249 | + } | |
| 250 | + // ない場合かつ自分で認証する場合 | |
| 251 | + if (!$CODOC_AUTHINFO and (isset($_GET['auth_by_myself']) and $_GET['auth_by_myself'])) { | |
| 252 | + register_setting( | |
| 253 | + 'codoc_option_group', // option group | |
| 254 | + CODOC_USERCODE_OPTION_NAME | |
| 255 | + ); | |
| 256 | + add_settings_field( | |
| 257 | + 'codoc_usercode', | |
| 258 | + 'ユーザーコード', | |
| 259 | + function() { | |
| 260 | + global $CODOC_USERCODE; | |
| 261 | + echo sprintf('<input type="text" name="%s" value="%s">',CODOC_USERCODE_OPTION_NAME,$CODOC_USERCODE); | |
| 262 | + }, | |
| 263 | + 'codoc', | |
| 264 | + 'setting_section_id' | |
| 265 | + ); | |
| 266 | + register_setting( | |
| 267 | + 'codoc_option_group', // option group | |
| 268 | + CODOC_TOKEN_OPTION_NAME | |
| 269 | + ); | |
| 270 | + add_settings_field( | |
| 271 | + 'codoc_token', | |
| 272 | + 'APIトークン', | |
| 273 | + function() { | |
| 274 | + global $CODOC_TOKEN; | |
| 275 | + echo sprintf('<input type="text" name="%s" value="%s">',CODOC_TOKEN_OPTION_NAME,$CODOC_TOKEN); | |
| 276 | + }, | |
| 277 | + 'codoc', | |
| 278 | + 'setting_section_id' | |
| 279 | + ); | |
| 280 | + } | |
| 281 | + | |
| 282 | + // 認証情報を保存 | |
| 283 | + add_action( 'update_option_' . CODOC_USERCODE_OPTION_NAME, function( $old_value, $new_value ) { | |
| 284 | + global $CODOC_RE_AUTHORIZE; | |
| 285 | + $CODOC_RE_AUTHORIZE = 1; | |
| 286 | + },9,2); // $hook, $function_to_add, $priority, $accepted_args | |
| 287 | + add_action( 'update_option_' . CODOC_TOKEN_OPTION_NAME, function( $old_value, $new_value ) { | |
| 288 | + global $CODOC_RE_AUTHORIZE; | |
| 289 | + $CODOC_RE_AUTHORIZE = 1; | |
| 290 | + },10,2); | |
| 291 | + add_action('updated_option',function(){ | |
| 292 | + global $CODOC_RE_AUTHORIZE; | |
| 293 | + if ($CODOC_RE_AUTHORIZE) { | |
| 294 | + $data = $this->callAPI('GET',''); | |
| 295 | + if ($data->status and $user = $data->user) { | |
| 296 | + update_option(CODOC_AUTHINFO_OPTION_NAME,[ | |
| 297 | + 'email' => $user->email, | |
| 298 | + 'name' => $user->name, | |
| 299 | + ]); | |
| 300 | + } else { | |
| 301 | + update_option(CODOC_AUTHINFO_OPTION_NAME,''); | |
| 302 | + } | |
| 303 | + } | |
| 304 | + }); | |
| 305 | + | |
| 306 | + }); | |
| 307 | + } | |
| 308 | + public function add_mce() { | |
| 309 | + // Add Shortcode options in the WordPres visual editors | |
| 310 | + // wp_enqueue_script( 'codoc-editor-script', plugins_url( 'codoc/src_mce/codoc-editor.js', __DIR__ )); | |
| 311 | + add_action( 'init', function() { | |
| 312 | + if ( ! current_user_can( 'edit_posts' ) && ! current_user_can( 'edit_pages' ) ) { | |
| 313 | + return; | |
| 314 | + } | |
| 315 | + if ( get_user_option( 'rich_editing' ) == 'true' ) { | |
| 316 | + // プラグイン内で使うCSRF を生成 | |
| 317 | + $path = plugins_url( 'codoc/src_mce/codoc-editor-onload.js', __DIR__ ); | |
| 318 | + wp_enqueue_script( 'codoc-editor-onload', $path, array('jquery'), '', true ); | |
| 319 | + // I just want to insert this global variables but i don't know how i can do it.. | |
| 320 | + wp_localize_script( | |
| 321 | + 'codoc-editor-onload', | |
| 322 | + 'CODOCEDITOR', | |
| 323 | + array( | |
| 324 | + 'action' => 'codoc_shortcodes', | |
| 325 | + 'nonce' => wp_create_nonce( 'codoc_shortcodes' ) | |
| 326 | + ) | |
| 327 | + ); | |
| 328 | + // ここでtinymceのプラグイン追加 | |
| 329 | + wp_enqueue_style( 'codoc-admin-style', plugins_url( 'codoc/src_mce/codoc-admin.css', __DIR__ ) ); | |
| 330 | + add_filter( 'mce_external_plugins', function( $plugin_array ) { | |
| 331 | + $plugin_array['codoc'] = plugins_url( 'codoc/src_mce/codoc-editor.js', __DIR__ ); | |
| 332 | + return $plugin_array; | |
| 333 | + } ); | |
| 334 | + add_filter( 'mce_buttons', function( $buttons ) { | |
| 335 | + array_push( $buttons, "|", "codoc" ); | |
| 336 | + return $buttons; | |
| 337 | + } ); | |
| 338 | + add_action( 'wp_ajax_codoc_shortcodes', function(){ | |
| 339 | + check_ajax_referer( 'codoc_shortcodes' ); | |
| 340 | + if ($usercode = sanitize_text_field($_GET['usercode'])) { | |
| 341 | + update_option('codoc_usercode',$usercode); | |
| 342 | + } | |
| 343 | + $usercode = get_option(CODOC_USERCODE_OPTION_NAME) ? get_option(CODOC_USERCODE_OPTION_NAME) : 'nocode'; | |
| 344 | + $uri = CODOC_URL . '/api/v1/paywall/' . $usercode . '/entries'; | |
| 345 | + if ($entrycode = sanitize_text_field($_GET['entrycode'])) { | |
| 346 | + $uri = $uri . '/' . $entrycode; | |
| 347 | + } | |
| 348 | + $args = preg_match('/local/',CODOC_URL) ? ['sslverify' => false ] : []; | |
| 349 | + $response = wp_remote_request( $uri, $args ); | |
| 350 | + | |
| 351 | + if ($response instanceof WP_Error) { | |
| 352 | + exit; | |
| 353 | + } | |
| 354 | + | |
| 355 | + $response['body'] = json_decode($response['body'], true); | |
| 356 | + $entries = $response['body']['entries']; | |
| 285 | 357 | |
| 286 | - if (!isset($CODOC_SETTINGS['entry_button_text'])) { | |
| 287 | - $CODOC_SETTINGS['entry_button_text'] = ''; | |
| 288 | - } | |
| 289 | - if (!isset($CODOC_SETTINGS['subscription_button_text'])) { | |
| 290 | - $CODOC_SETTINGS['subscription_button_text'] = ''; | |
| 291 | - } | |
| 292 | - if (!isset($CODOC_SETTINGS['support_button_text'])) { | |
| 293 | - $CODOC_SETTINGS['support_button_text'] = ''; | |
| 294 | - } | |
| 295 | - if (!isset($CODOC_SETTINGS['subscription_message'])) { | |
| 296 | - $CODOC_SETTINGS['subscription_message'] = ''; | |
| 297 | - } | |
| 298 | - if (!isset($CODOC_SETTINGS['support_message'])) { | |
| 299 | - $CODOC_SETTINGS['support_message'] = ''; | |
| 300 | - } | |
| 301 | - if (!isset($CODOC_SETTINGS['show_like'])) { | |
| 302 | - $CODOC_SETTINGS['show_like'] = '1'; | |
| 303 | - } | |
| 304 | - if (!isset($CODOC_SETTINGS['show_about_codoc'])) { | |
| 305 | - $CODOC_SETTINGS['show_about_codoc'] = '1'; | |
| 306 | - } | |
| 307 | - if (!isset($CODOC_SETTINGS['show_powered_by'])) { | |
| 308 | - $CODOC_SETTINGS['show_powered_by'] = '1'; | |
| 309 | - } | |
| 310 | - if (!isset($CODOC_SETTINGS['show_created_by'])) { | |
| 311 | - $CODOC_SETTINGS['show_created_by'] = '1'; | |
| 312 | - } | |
| 313 | - if (!isset($CODOC_SETTINGS['show_copyright'])) { | |
| 314 | - $CODOC_SETTINGS['show_copyright'] = '1'; | |
| 315 | - } | |
| 316 | - if (!isset($CODOC_SETTINGS['codoc_tag_attributes'])) { | |
| 317 | - $CODOC_SETTINGS['codoc_tag_attributes'] = ''; | |
| 318 | - } | |
| 319 | - if (!isset($CODOC_SETTINGS['codoc_script_tag_attributes'])) { | |
| 320 | - $CODOC_SETTINGS['codoc_script_tag_attributes'] = ''; | |
| 321 | - } | |
| 322 | - if (!isset($CODOC_SETTINGS['codoc_connect_code'])) { | |
| 323 | - $CODOC_SETTINGS['codoc_connect_code'] = ''; | |
| 324 | - } | |
| 325 | - if (!isset($CODOC_SETTINGS['codoc_connect_registration_mode'])) { | |
| 326 | - $CODOC_SETTINGS['codoc_connect_registration_mode'] = ''; | |
| 327 | - } | |
| 328 | - if (!isset($CODOC_SETTINGS['debug_params'])) { | |
| 329 | - $CODOC_SETTINGS['debug_params'] = ''; | |
| 330 | - } | |
| 331 | - if (!isset($CODOC_SETTINGS['block_defaults'])) { | |
| 332 | - $CODOC_SETTINGS['block_defaults'] = ''; | |
| 333 | - } | |
| 334 | - add_settings_section( | |
| 335 | - 'setting_section_id', // id | |
| 336 | - __('codoc Settings','codoc'), // title | |
| 337 | - [$this,'show_tadv_notice'], | |
| 338 | - 'codoc' // page | |
| 339 | - ); | |
| 358 | + require 'views/codoc-shortcodes-list.php'; | |
| 340 | 359 | |
| 341 | - // 認証がある場合 | |
| 342 | - if ($CODOC_AUTHINFO) { | |
| 343 | - // クリエイター情報だけ更新 (POST時に同期をとる) | |
| 344 | - if (isset($_GET['page']) and $_GET['page'] == 'codoc' and isset($_GET['settings-updated'])) { | |
| 345 | - $data = $this->util->get_user_info([],[ | |
| 346 | - "usercode" => get_option(CODOC_USERCODE_OPTION_NAME), | |
| 347 | - "token" => get_option(CODOC_TOKEN_OPTION_NAME), | |
| 348 | - ]); | |
| 349 | - if ($data->status and $user = $data->user) { | |
| 350 | - $this->update_codoc_authinfo($user); | |
| 351 | - // 一度get_optionしてるのでリロードする | |
| 352 | - $CODOC_AUTHINFO = get_option(CODOC_AUTHINFO_OPTION_NAME); | |
| 353 | - } | |
| 354 | - } | |
| 360 | + wp_die(); | |
| 361 | + } ); | |
| 362 | + } | |
| 363 | + } ); | |
| 364 | + } | |
| 365 | + public function save_post($post_ID,$post,$update) { | |
| 366 | + if (preg_match('/^(auto-draft|inherit)$/',$post->post_status)) { | |
| 367 | + return; | |
| 368 | + } | |
| 369 | + | |
| 370 | + $post_content = $post->post_content; | |
| 371 | + preg_match('/wp:codoc\/codoc-block +?({.*})/',$post_content,$matches); | |
| 372 | + // GUTENBERG で指定されたjson(記事情報)がない場合 | |
| 373 | + if (!$matches) { | |
| 374 | + return; | |
| 375 | + } | |
| 376 | + // GUTENBERG で保存している内容を取得 | |
| 377 | + $codoc_info = json_decode($matches[1],true); | |
| 378 | + // codoc タグがない場合はなにもしない | |
| 379 | + preg_match('/(<span +data-id="codoc-tag"[^>]+>(?:.+|)<\/span>)/',$post_content,$matches); | |
| 380 | + if (!$matches) { | |
| 381 | + return; | |
| 382 | + } | |
| 383 | + // TODO: split or html parse? HTMLがつながってる場合はおかしくなる | |
| 384 | + $splited = preg_split('/<\!-- +wp:codoc\/codoc-block .*<\!-- +\/wp:codoc\/codoc-block +-->/s',$post_content); | |
| 385 | + $status = $post->post_status == 'publish' ? 1 : 0; | |
| 386 | + # $body_free = ''; | |
| 387 | + # $body_paywalled = ''; | |
| 388 | + # if (count($splited) >= 2) { | |
| 389 | + # | |
| 390 | + # } | |
| 355 | 391 | |
| 356 | - register_setting( | |
| 357 | - 'codoc_option_group', // option group | |
| 358 | - CODOC_SETTINGS_OPTION_NAME, // option name(DB) | |
| 359 | - array('sanitize_callback' => function($input) { | |
| 360 | - if (isset($input['block_defaults']) && $input['block_defaults'] !== '') { | |
| 361 | - if (json_decode($input['block_defaults'], true) === null && $input['block_defaults'] !== 'null') { | |
| 362 | - $old = get_option(CODOC_SETTINGS_OPTION_NAME); | |
| 363 | - $input['block_defaults'] = isset($old['block_defaults']) ? $old['block_defaults'] : ''; | |
| 364 | - add_settings_error('block_defaults', 'invalid_json', esc_html(__('Block Default Values: Invalid JSON format. The value was not saved.','codoc')), 'error'); | |
| 365 | - } | |
| 366 | - } | |
| 367 | - return $input; | |
| 368 | - }) | |
| 369 | - ); | |
| 370 | - add_settings_field( | |
| 371 | - 'css_path', // id | |
| 372 | - __('Theme','codoc'), // title | |
| 373 | - function() { | |
| 374 | - global $CODOC_SETTINGS; | |
| 375 | - echo '<div class="excerpt">' . esc_html(__('You can change the design of the paywall by specifying a theme.<br />You can also specify CSS directly by selecting "Path Specification"','codoc')) . '</div>'; | |
| 376 | - echo sprintf('<div class="inputGroup"><input type="text" name="%s[css_path]" value="%s" id="codoc_css_path" readonly>',esc_attr(CODOC_SETTINGS_OPTION_NAME),esc_attr($CODOC_SETTINGS['css_path'])); | |
| 377 | - echo '' . | |
| 378 | - '<script type="text/javascript"> ' . | |
| 379 | - ' function gen_css_path(select) { ' . | |
| 380 | - ' if (select.value == "dark" || select.value == "dark-square") { ' . | |
| 381 | - ' document.getElementById("darkmode-caution").style.display="block";' . | |
| 382 | - ' } else { ' . | |
| 383 | - ' if (document.getElementById("darkmode-caution")) { ' . | |
| 384 | - ' document.getElementById("darkmode-caution").style.display="none";' . | |
| 385 | - ' } ' . | |
| 386 | - ' } ' . | |
| 387 | - ' if (select.value == "path") { ' . | |
| 388 | - ' document.getElementById("codoc_css_path").readOnly=false; ' . | |
| 389 | - ' if (!document.getElementById("codoc_css_path").value.match(/\//g)) {' . | |
| 390 | - ' document.getElementById("codoc_css_path").value=""; ' . | |
| 391 | - ' } ' . | |
| 392 | - ' } else { ' . | |
| 393 | - ' document.getElementById("codoc_css_path").readOnly=true; ' . | |
| 394 | - ' document.getElementById("codoc_css_path").value=select.value; ' . | |
| 395 | - ' } ' . | |
| 396 | - ' } ' . | |
| 397 | - '</script> ' ; | |
| 392 | + $params = [ | |
| 393 | + 'title' => $post->post_title, | |
| 394 | + 'body_free' => $splited[0], | |
| 395 | + 'body_paywalled' => $splited[1], | |
| 396 | + 'status' => $status, | |
| 397 | + 'binded_url' => get_permalink($post_ID), | |
| 398 | + 'show_price' => isset($codoc_info['showPrice']) ? ($codoc_info['showPrice'] ? 1 : 0) : 0, | |
| 399 | + 'price' => isset($codoc_info['price']) ? $codoc_info['price'] : 100, | |
| 400 | + 'limited' => isset($codoc_info['limited']) ? ($codoc_info['limited'] ? 1 : 0) : 0, | |
| 401 | + 'limited_count' => isset($codoc_info['limitedCount']) ? $codoc_info['limitedCount'] : 1, | |
| 402 | + 'affiliate_mode' => isset($codoc_info['affiliateMode']) ? ($codoc_info['affiliateMode'] ? 1 : 0) : 0, | |
| 403 | + 'affiliate_rate' => isset($codoc_info['affiliateRate']) ? $codoc_info['affiliateRate'] : '0.0500', | |
| 404 | + 'show_support' => isset($codoc_info['showSupport']) ? ($codoc_info['showSupport'] ? 1 : 0) : 0, | |
| 405 | + 'subscriptions' => isset($codoc_info['subscriptions']) ? array_keys($codoc_info['subscriptions']) : [], | |
| 406 | + ]; | |
| 407 | + $entryCode = get_post_meta($post_ID,'codoc_entry_code',true); | |
| 408 | + if ($entryCode) { | |
| 409 | + $res = $this->callAPI('PUT','/entries/' . $entryCode ,$params); | |
| 410 | + } else { | |
| 411 | + $res = $this->callAPI('POST', '/entries', $params); | |
| 412 | + if (is_object($res) and $res->status) { | |
| 413 | + update_post_meta($post_ID,'codoc_entry_code',$res->entry->code); | |
| 414 | + } | |
| 415 | + } | |
| 416 | + return true; | |
| 417 | + } | |
| 418 | + function updated_post_meta( $meta_ID, $post_ID, $meta_key ) { | |
| 419 | + if ($meta_key != '_thumbnail_id') { | |
| 420 | + return; | |
| 421 | + } | |
| 422 | + if ( has_post_thumbnail($post_ID) ) { | |
| 423 | + $attachment = wp_get_attachment_metadata( get_post_thumbnail_id($post_ID)); | |
| 424 | + $upload_dir = wp_upload_dir(); | |
| 425 | + $file_path = sprintf ('%s/%s',$upload_dir['basedir'],$attachment['file']); | |
| 426 | + | |
| 427 | + if (is_readable($file_path)) { | |
| 428 | + $name = 'file'; | |
| 398 | 429 | |
| 399 | - echo sprintf('<select name="theme_name" onChange="gen_css_path(this)" id="codoc_theme_select">'); | |
| 400 | - foreach (["rainbow","blue","red","green","black","dark","rainbow-square","blue-square","red-square","green-square","black-square","dark-square"] as $theme) { | |
| 401 | - if ($theme == "rainbow") { | |
| 402 | - echo sprintf('<option value="%s" %s>%s</option>', esc_attr($theme), esc_attr($CODOC_SETTINGS["css_path"] == "rainbow" ? "selected" : ""),esc_attr(__('Rainbow colors','codoc'))); | |
| 403 | - } | |
| 404 | - if ($theme == "blue") { | |
| 405 | - echo sprintf('<option value="%s" %s>%s</option>', esc_attr($theme), esc_attr($CODOC_SETTINGS["css_path"] == "blue" ? "selected" : ""),esc_attr(__('Blue','codoc'))); | |
| 406 | - } | |
| 407 | - if ($theme == "red") { | |
| 408 | - echo sprintf('<option value="%s" %s>%s</option>', esc_attr($theme), esc_attr($CODOC_SETTINGS["css_path"] == "red" ? "selected" : ""),esc_attr(__('Red','codoc'))); | |
| 409 | - } | |
| 410 | - if ($theme == "green") { | |
| 411 | - echo sprintf('<option value="%s" %s>%s</option>', esc_attr($theme), esc_attr($CODOC_SETTINGS["css_path"] == "green" ? "selected" : ""),esc_attr(__('Green','codoc'))); | |
| 412 | - } | |
| 413 | - if ($theme == "black") { | |
| 414 | - echo sprintf('<option value="%s" %s>%s</option>', esc_attr($theme), esc_attr($CODOC_SETTINGS["css_path"] == "black" ? "selected" : ""),esc_attr(__('Black','codoc'))); | |
| 415 | - } | |
| 416 | - if ($theme == "dark") { | |
| 417 | - echo sprintf('<option value="%s" %s>%s</option>', esc_attr($theme), esc_attr($CODOC_SETTINGS["css_path"] == "dark" ? "selected" : ""),esc_attr(__('Dark mode','codoc'))); | |
| 418 | - } | |
| 419 | - if ($theme == "rainbow-square") { | |
| 420 | - echo sprintf('<option value="%s" %s>%s</option>', esc_attr($theme), esc_attr($CODOC_SETTINGS["css_path"] == "rainbow-square" ? "selected" : ""),esc_attr(__("Rainbow Colors / Square design",'codoc'))); | |
| 421 | - } | |
| 422 | - if ($theme == "blue-square") { | |
| 423 | - echo sprintf('<option value="%s" %s>%s</option>', esc_attr($theme), esc_attr($CODOC_SETTINGS["css_path"] == "blue-square" ? "selected" : ""),esc_attr(__('Blue / Square design','codoc'))); | |
| 424 | - } | |
| 425 | - if ($theme == "red-square") { | |
| 426 | - echo sprintf('<option value="%s" %s>%s/option>', esc_attr($theme), esc_attr($CODOC_SETTINGS["css_path"] == "red-square" ? "selected" : ""),esc_attr(__('Red / Square design','codoc'))); | |
| 427 | - } | |
| 428 | - if ($theme == "green-square") { | |
| 429 | - echo sprintf('<option value="%s" %s>%s</option>', esc_attr($theme), esc_attr($CODOC_SETTINGS["css_path"] == "green-square" ? "selected" : ""),esc_attr(__('Green / Square design','codoc'))); | |
| 430 | - } | |
| 431 | - if ($theme == "black-square") { | |
| 432 | - echo sprintf('<option value="%s" %s>%s</option>', esc_attr($theme), esc_attr($CODOC_SETTINGS["css_path"] == "black-square" ? "selected" : ""),esc_attr(__('Black / Square design','codoc'))); | |
| 433 | - } | |
| 434 | - if ($theme == "dark-square") { | |
| 435 | - echo sprintf('<option value="%s" %s>%s</option>', esc_attr($theme), esc_attr($CODOC_SETTINGS["css_path"] == "dark-square" ? "selected" : ""),esc_attr(__('Dark mode / Square design','codoc'))); | |
| 436 | - } | |
| 437 | - } | |
| 438 | - echo sprintf('<option value="path" %s>%s</option>', esc_attr(preg_match("/\//",$CODOC_SETTINGS["css_path"]) ? "selected" : ""),esc_attr(__('Path Specification','codoc'))); | |
| 439 | - echo '<script type="text/javascript">gen_css_path(document.getElementById(\'codoc_theme_select\'))</script>'; | |
| 440 | - echo sprintf('</select></div>'); | |
| 441 | - echo '<p id="darkmode-caution" style="display:none">' . esc_attr(__('Please note that the theme for dark mode has white text color, and depending on the background color, the text may not be visible.','codoc')). '</p>'; | |
| 442 | - }, | |
| 443 | - 'codoc', //page | |
| 444 | - 'setting_section_id' //Section | |
| 445 | - ); | |
| 446 | - add_settings_field( | |
| 447 | - 'paywall_text', // id | |
| 448 | - __('Texts in paywall','codoc'), // title | |
| 449 | - function() { | |
| 450 | - global $CODOC_SETTINGS; | |
| 451 | - global $CODOC_AUTHINFO; | |
| 452 | - echo '<div class="excerpt">' . esc_html(__('You can change, show and hide texts in your paywall.','codoc')) . '</div>'; | |
| 453 | - echo '<table class="innerTable"><tbody>'; | |
| 454 | - echo '<tr><th>' . esc_html(__('Display of likes','codoc')) . '</th><td>'; | |
| 455 | - echo sprintf('<input type="radio" value="1" name="%s[show_like]" id="show_like_on" %s><label for="show_like_on">' . esc_html(__('Enable','codoc')) . '</label> ',esc_attr(CODOC_SETTINGS_OPTION_NAME),esc_attr($CODOC_SETTINGS['show_like'] == '1' ? "checked" : "")); | |
| 456 | - echo sprintf('<input type="radio" value="0" name="%s[show_like]" id="show_like_off" %s><label for="show_like_off">' . esc_html(__('Disable','codoc')) . '</label> <br / >',esc_attr(CODOC_SETTINGS_OPTION_NAME),esc_attr($CODOC_SETTINGS['show_like'] == '0' ? "checked" : "")); | |
| 457 | - echo '</td></tr>'; | |
| 430 | + $boundary = wp_generate_password(24); | |
| 458 | 431 | |
| 459 | - echo '<tr><th>' . esc_html(__('Display of PoweredBy','codoc')) . '</th><td>'; | |
| 460 | - echo sprintf('<input type="hidden" value="1" name="%s[show_about_codoc]">',esc_attr(CODOC_SETTINGS_OPTION_NAME)); | |
| 461 | - echo sprintf('<input type="hidden" value="1" name="%s[show_created_by]">',esc_attr(CODOC_SETTINGS_OPTION_NAME)); | |
| 462 | - echo sprintf('<input type="hidden" value="1" name="%s[show_powered_by]">',esc_attr(CODOC_SETTINGS_OPTION_NAME)); | |
| 463 | - if (isset($CODOC_AUTHINFO['account_is_pro']) and $CODOC_AUTHINFO['account_is_pro']) { | |
| 464 | - echo sprintf('<input type="radio" value="1" name="%s[show_copyright]" id="show_copyright_on" %s><label for="show_copyright_on">' . esc_html(__('Enable','codoc')) . '</label> ',esc_attr(CODOC_SETTINGS_OPTION_NAME),esc_attr($CODOC_SETTINGS['show_copyright'] == '1' ? "checked" : "")); | |
| 465 | - echo sprintf('<input type="radio" value="0" name="%s[show_copyright]" id="show_copyright_off" %s><label for="show_copyright_off">' . esc_html(__('Disable','codoc')) . '</label> <br / >',esc_attr(CODOC_SETTINGS_OPTION_NAME),esc_attr($CODOC_SETTINGS['show_copyright'] == '0' ? "checked" : "")); | |
| 466 | - } else { | |
| 467 | - echo esc_html(__('Only PRO accounts can be disabled.','codoc')); | |
| 468 | - } | |
| 469 | - echo '</td></tr>'; | |
| 470 | - | |
| 471 | - echo '<tr><th>' . esc_html(__('"Purchase Article" button text','codoc')) . '</th><td>'; | |
| 472 | - echo sprintf('<input type="text" name="%s[entry_button_text]" value="%s">',esc_attr(CODOC_SETTINGS_OPTION_NAME),esc_attr($CODOC_SETTINGS['entry_button_text'])); | |
| 473 | - echo '</td></tr>'; | |
| 474 | - | |
| 475 | - echo '<tr><th>' . esc_html(__('"Purchase Subscription" button text.','codoc')) . '</th><td>'; | |
| 476 | - echo sprintf('<input type="text" name="%s[subscription_button_text]" value="%s">',esc_attr(CODOC_SETTINGS_OPTION_NAME),esc_attr($CODOC_SETTINGS['subscription_button_text'])); | |
| 477 | - echo '</td></tr>'; | |
| 432 | + $payload = ''; | |
| 433 | + $payload .= '--' . $boundary; | |
| 434 | + $payload .= "\r\n"; | |
| 435 | + $payload .= 'Content-Disposition: form-data; name="' . $name . '"; filename="' . basename( $file_path ) . '"' . "\r\n"; | |
| 436 | + $payload .= "Content-Type: application/octet-stream\r\n"; | |
| 437 | + $payload .= "Content-Transfer-Encoding: binary\r\n"; | |
| 438 | + $payload .= "\r\n"; | |
| 439 | + $payload .= file_get_contents( $file_path ); | |
| 440 | + $payload .= "\r\n"; | |
| 441 | + | |
| 442 | + $payload .= '--' . $boundary . '--'; | |
| 443 | + | |
| 444 | + | |
| 445 | + $entryCode = get_post_meta($post_ID,'codoc_entry_code',true); | |
| 446 | + $res = $this->callAPI( | |
| 447 | + 'POST', | |
| 448 | + '/entries/' . $entryCode . '/thumbnail', | |
| 449 | + $payload, | |
| 450 | + ['content-type' => 'multipart/form-data; boundary=' . $boundary] | |
| 451 | + ); | |
| 452 | + } | |
| 453 | + } | |
| 454 | + } | |
| 455 | + function deleted_post_meta( $meta_ids, $post_ID, $meta_key,$meta_value ) { | |
| 456 | + if ($meta_key != '_thumbnail_id') { | |
| 457 | + return; | |
| 458 | + } | |
| 459 | + $entryCode = get_post_meta($post_ID,'codoc_entry_code',true); | |
| 460 | + $res = $this->callAPI( | |
| 461 | + 'POST', | |
| 462 | + '/entries/' . $entryCode . '/thumbnail', | |
| 463 | + [ "reset" => 1 ] | |
| 464 | + ); | |
| 465 | + } | |
| 466 | + function the_content( $post_content ) { | |
| 467 | + if ( is_single() && in_the_loop() && is_main_query() ) { | |
| 468 | + } | |
| 469 | + // codoc タグがあるかどうか | |
| 470 | + preg_match('/(<span +data-id="codoc-tag[^>]+>(?:.+|)<\/span>)/',$post_content,$matches); | |
| 471 | + // data-wp-plugin-ver が無いタグは分割しない | |
| 472 | + if (!$matches) { | |
| 473 | + return $post_content; | |
| 474 | + } | |
| 475 | + if ( is_preview() ) { | |
| 476 | + return $post_content; | |
| 477 | + } | |
| 478 | + // codoc タグの前後で分ける | |
| 479 | + $splited = preg_split('/<div><span data-id="codoc-tag" class="codoc-entries">(?:.+|)<\/span><\/div>/',$post_content); | |
| 480 | + //$splited = preg_split('/<div +class="wp-block-codoc-codoc-block">.*<\/div>/s',$post_content); | |
| 481 | + // codco タグにentrycodeをつける | |
| 482 | + $post = get_post(); | |
| 483 | + $entryCode = sprintf('"codoc-entry-%s" ',get_post_meta($post->ID,'codoc_entry_code',true)); | |
| 484 | + $codoc_tag = str_replace('span','span id=' . $entryCode, $matches[1]); | |
| 485 | + // codoc タグに属性をつける | |
| 486 | + $codoc_tag = str_replace('span','span data-without-body="1" ',$codoc_tag); | |
| 487 | + // 無料部分のみ表示 | |
| 488 | + return $splited[0] . sprintf('<div class="wp-block-codoc-codoc-block">%s</div>',$codoc_tag); | |
| 489 | + } | |
| 478 | 490 | |
| 479 | - echo '<tr><th>' . esc_html(__('Support button text','codoc')) . '</th><td>'; | |
| 480 | - echo sprintf('<input type="text" name="%s[support_button_text]" value="%s">',esc_attr(CODOC_SETTINGS_OPTION_NAME),esc_attr($CODOC_SETTINGS['support_button_text'])); | |
| 481 | - echo '</td></tr>'; | |
| 482 | - | |
| 483 | - echo '<tr><th>' . esc_html(__('Text for "Articles Included in Subscription".','codoc')) . '</th><td>'; | |
| 484 | - echo sprintf('<input type="text" name="%s[subscription_message]" value="%s">',esc_attr(CODOC_SETTINGS_OPTION_NAME),esc_attr($CODOC_SETTINGS['subscription_message'])); | |
| 485 | - echo '</td></tr>'; | |
| 486 | - | |
| 487 | - echo '<tr><th>' . esc_html(__('Text for support description','codoc')) . '</th><td>'; | |
| 488 | - echo sprintf('<input type="text" name="%s[support_message]" value="%s">',esc_attr(CODOC_SETTINGS_OPTION_NAME),esc_attr($CODOC_SETTINGS['support_message'])); | |
| 489 | - echo '</td></tr>'; | |
| 490 | - | |
| 491 | - echo '</tbody></table>'; | |
| 492 | - }, | |
| 493 | - 'codoc', //page | |
| 494 | - 'setting_section_id' //Section | |
| 495 | - ); | |
| 496 | - | |
| 497 | - add_settings_field( | |
| 498 | - 'codoc_tag_attributes', // id | |
| 499 | - __('Attributes for codoc tag','codoc'), // title | |
| 500 | - function() { | |
| 501 | - global $CODOC_SETTINGS; | |
| 502 | - echo '<div class="excerpt">' . esc_html(__('You can add specific attributes to codoc tags.','codoc')) . '</div>'; | |
| 503 | - echo sprintf('<input type="text" name="%s[codoc_tag_attributes]" value="%s">',esc_attr(CODOC_SETTINGS_OPTION_NAME),esc_attr($CODOC_SETTINGS['codoc_tag_attributes'])); | |
| 504 | - }, | |
| 505 | - 'codoc', //page | |
| 506 | - 'setting_section_id' //Section | |
| 507 | - ); | |
| 508 | - add_settings_field( | |
| 509 | - 'codoc_script_tag_attributes', // id | |
| 510 | - __('Attributes for codoc script tag','codoc'), // title | |
| 511 | - function() { | |
| 512 | - global $CODOC_SETTINGS; | |
| 513 | - echo '<div class="excerpt">' . esc_html(__('You can add specific attributes to codoc script tags.','codoc')) . '</div>'; | |
| 514 | - echo sprintf('<input type="text" name="%s[codoc_script_tag_attributes]" value="%s">',esc_attr(CODOC_SETTINGS_OPTION_NAME),esc_attr($CODOC_SETTINGS['codoc_script_tag_attributes'])); | |
| 515 | - }, | |
| 516 | - 'codoc', //page | |
| 517 | - 'setting_section_id' //Section | |
| 518 | - ); | |
| 519 | - add_settings_field( | |
| 520 | - 'str_replace_binded_url', // id | |
| 521 | - __('Permalink','codoc'), // title | |
| 522 | - function() { | |
| 523 | - global $CODOC_SETTINGS; | |
| 524 | - echo '<div class="excerpt">' . esc_html(__('You can replace the part of permalink registered on the codoc.','codoc')) . '</div>'; | |
| 525 | - echo sprintf('<input type="text" name="%s[str_replace_binded_url_from]" value="%s">',esc_attr(CODOC_SETTINGS_OPTION_NAME),esc_attr($CODOC_SETTINGS['str_replace_binded_url_from'])); | |
| 526 | - echo ('<span class="suptext">→</span>'); | |
| 527 | - echo sprintf('<input type="text" name="%s[str_replace_binded_url_to]" value="%s">',esc_attr(CODOC_SETTINGS_OPTION_NAME),esc_attr($CODOC_SETTINGS['str_replace_binded_url_to'])); | |
| 491 | + | |
| 492 | +} | |
| 528 | 493 | |
| 529 | - }, | |
| 530 | - 'codoc', //page | |
| 531 | - 'setting_section_id' //Section | |
| 532 | - ); | |
| 533 | - | |
| 534 | - add_settings_field( | |
| 535 | - 'str_around_codoc_tag', // id | |
| 536 | - __('HTML insertion','codoc'), // title | |
| 537 | - function() { | |
| 538 | - global $CODOC_SETTINGS; | |
| 539 | - echo '<div class="excerpt">' . esc_html(__('You can insert HTML before and after the codoc tag.','codoc')) . '</div>'; | |
| 540 | - echo sprintf('<div class="inputRow"><p>%s</p><textarea name="%s[str_before_codoc_tag]" cols="40">%s</textarea></div>',esc_html(__('Before HTML','codoc')),esc_attr(CODOC_SETTINGS_OPTION_NAME),esc_html($CODOC_SETTINGS['str_before_codoc_tag'])); | |
| 541 | - echo sprintf('<div class="inputRow"><p>%s</p><textarea name="%s[str_after_codoc_tag]" cols="40">%s</textarea></div>',esc_html(__('After HTML','codoc')),esc_attr(CODOC_SETTINGS_OPTION_NAME),esc_html($CODOC_SETTINGS['str_after_codoc_tag'])); | |
| 542 | - | |
| 543 | - }, | |
| 544 | - 'codoc', //page | |
| 545 | - 'setting_section_id' //Section | |
| 546 | - ); | |
| 547 | - | |
| 548 | - add_settings_field( | |
| 549 | - 'always_show_support_tag', // id | |
| 550 | - __('Automatic support insertion','codoc'), // title | |
| 551 | - function() { | |
| 552 | - global $CODOC_SETTINGS; | |
| 553 | - echo '<div class="excerpt">' . esc_html(__('It adds support functions to the post without inserting a codoc block.','codoc')) . '</div>'; | |
| 554 | - echo '<table class="innerTable"><tbody>'; | |
| 555 | - echo '<tr><th>' . esc_html(__('Automatic insertion','codoc')) . '</th><td>'; | |
| 556 | - echo sprintf('<input type="radio" value="1" name="%s[always_show_support]" id="always_show_support_on" %s><label for="always_show_support_on">' . esc_html(__('Enable','codoc')) . '</label> ',esc_attr(CODOC_SETTINGS_OPTION_NAME),esc_attr($CODOC_SETTINGS['always_show_support'] == '1' ? "checked" : "")); | |
| 557 | - echo sprintf('<input type="radio" value="0" name="%s[always_show_support]" id="always_show_support_off" %s><label for="always_show_support_off">' . esc_html(__('Disable','codoc')) . '</label> <br / >',esc_attr(CODOC_SETTINGS_OPTION_NAME),esc_attr($CODOC_SETTINGS['always_show_support'] == '0' ? "checked" : "")); | |
| 558 | - echo '</td></tr>'; | |
| 559 | - echo '<tr><th>'. esc_html(__('Position','codoc')) . '</th><td>'; | |
| 560 | - echo sprintf('<input type="radio" value="top" name="%s[show_support_location]" id="show_support_location_top" %s><label for="show_support_location_top">' . esc_html(__('TOP','codoc')) . '</label> ',esc_attr(CODOC_SETTINGS_OPTION_NAME),esc_attr($CODOC_SETTINGS['show_support_location'] == 'top' ? "checked" : "")); | |
| 561 | - echo sprintf('<input type="radio" value="bottom" name="%s[show_support_location]" id="show_support_location_bottom" %s><label for="show_support_location_bottom">' . esc_html(__('Bottom','codoc')) . '</label> <br / >',esc_attr(CODOC_SETTINGS_OPTION_NAME),esc_attr($CODOC_SETTINGS['show_support_location'] == 'bottom' ? "checked" : "")); | |
| 562 | - echo '</td></tr>'; | |
| 563 | - echo '<tr><th>' . esc_html(__('Description','codoc')) . '</th><td>'; | |
| 564 | - echo sprintf('<input type="text" placeholder="' . esc_html(__('You can customize the description of the support.','codoc')) . '" size="50%%" name="%s[show_support_message]" value="%s">',esc_attr(CODOC_SETTINGS_OPTION_NAME),esc_html($CODOC_SETTINGS['show_support_message'])); | |
| 565 | - echo '</td></tr>'; | |
| 566 | - echo '<tr><th>' . esc_html(__('Category names','codoc')) . '</th><td>'; | |
| 567 | - echo sprintf('<input placeholder="' . esc_html(__('Inserted into articles of categories, divided by "|"','codoc')) . '" type="text" size="50%%" name="%s[show_support_categories]" value="%s"><br />',esc_attr(CODOC_SETTINGS_OPTION_NAME),esc_html($CODOC_SETTINGS['show_support_categories'])); | |
| 568 | - echo '</td></tr>'; | |
| 569 | - echo '</tbody></table>'; | |
| 570 | - | |
| 571 | - }, | |
| 572 | - 'codoc', //page | |
| 573 | - 'setting_section_id' //Section | |
| 574 | - ); | |
| 575 | - add_settings_field( | |
| 576 | - 'do_not_filter_the_content', // id | |
| 577 | - __('Disable plugin filter','codoc'), // title | |
| 578 | - function() { | |
| 579 | - global $CODOC_SETTINGS; | |
| 580 | - echo '<div class="excerpt">' . esc_html(__('You can disable filter processing that includes shortcodes from other plugins that interfere with the operation of codoc. Please use this only if codoc is not functioning properly.','codoc')) . '</div>'; | |
| 581 | - 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">' . esc_html(__('Enable','codoc')) .'</label> ',esc_attr(CODOC_SETTINGS_OPTION_NAME),esc_attr($CODOC_SETTINGS['do_not_filter_the_content'] == '1' ? "checked" : "")); | |
| 582 | - 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">' . esc_html(__('Disable','codoc')) . '</label> ',esc_attr(CODOC_SETTINGS_OPTION_NAME),esc_attr($CODOC_SETTINGS['do_not_filter_the_content'] == '0' ? "checked" : "")); | |
| 583 | - }, | |
| 584 | - 'codoc', //page | |
| 585 | - 'setting_section_id' //Section | |
| 586 | - ); | |
| 587 | - | |
| 588 | - add_settings_field( | |
| 589 | - 'shortcode_evacuation', // id | |
| 590 | - __('Shortcode evacuation','codoc'), // title | |
| 591 | - function() { | |
| 592 | - global $CODOC_SETTINGS; | |
| 593 | - echo '<div class="excerpt">' . esc_html(__('The execution results of shortcodes written in the paid part are moved to the free part, and are used and displayed on the HTML during viewing of the paid part. Please use this if the shortcode does not work as expected in the paid part. Please note that the execution results of shortcodes in the paid part will be written in the source code.','codoc')). '</div>'; | |
| 594 | - echo sprintf('<input type="radio" value="1" name="%s[shortcode_evacuation]" id="shortcode_evacuation_on" %s><label for="shortcode_evacuation_on">' . esc_html(__('Enable','codoc')) . '</label> ',esc_attr(CODOC_SETTINGS_OPTION_NAME),esc_attr($CODOC_SETTINGS['shortcode_evacuation'] == '1' ? "checked" : "")); | |
| 595 | - echo sprintf('<input type="radio" value="0" name="%s[shortcode_evacuation]" id="shortcode_evacuation_off" %s><label for="shortcode_evacuation_off">' . esc_html(__('Disable','codoc')) . '</label> ',esc_attr(CODOC_SETTINGS_OPTION_NAME),esc_attr($CODOC_SETTINGS['shortcode_evacuation'] == '0' ? "checked" : "")); | |
| 596 | - }, | |
| 597 | - 'codoc', //page | |
| 598 | - 'setting_section_id' //Section | |
| 599 | - ); | |
| 600 | - | |
| 601 | - add_settings_field( | |
| 602 | - 'debug_params', // id | |
| 603 | - __('Debug Parameters','codoc'), // title | |
| 604 | - function() { | |
| 605 | - global $CODOC_SETTINGS; | |
| 606 | - echo '<div class="excerpt">' . esc_html(__('You can specify parameters for debugging. Please use this only upon request from codoc support.','codoc')) . '</div>'; | |
| 607 | - echo sprintf('<input placeholder="" type="text" size="50%%" name="%s[debug_params]" value="%s"><br />',esc_attr(CODOC_SETTINGS_OPTION_NAME),esc_html($CODOC_SETTINGS['debug_params'])); | |
| 608 | - }, | |
| 609 | - 'codoc', //page | |
| 610 | - 'setting_section_id' //Section | |
| 611 | - ); | |
| 612 | - | |
| 613 | - add_settings_field( | |
| 614 | - 'block_defaults', // id | |
| 615 | - __('Block Default Values','codoc'), // title | |
| 616 | - function() { | |
| 617 | - global $CODOC_SETTINGS; | |
| 618 | - echo '<div class="excerpt">' . esc_html(__('You can specify default values for new codoc blocks in JSON format.','codoc')) . '</div>'; | |
| 619 | - echo sprintf('<textarea placeholder=\'{"showPrice":false,"price":300}\' rows="3" cols="50" name="%s[block_defaults]">%s</textarea>',esc_attr(CODOC_SETTINGS_OPTION_NAME),esc_html($CODOC_SETTINGS['block_defaults'])); | |
| 620 | - echo '<p class="description">' | |
| 621 | - . 'showPrice (bool: true), ' | |
| 622 | - . 'price (int: 500), ' | |
| 623 | - . 'limited (bool: false), ' | |
| 624 | - . 'limitedCount (int: 10), ' | |
| 625 | - . 'affiliateMode (bool: false), ' | |
| 626 | - . 'affiliateRate (string: "0.0500"), ' | |
| 627 | - . 'showSupport (bool: false), ' | |
| 628 | - . 'showPaywalledSupport (bool: false), ' | |
| 629 | - . 'statusLimited (bool: false), ' | |
| 630 | - . 'subscriptions (object: {})' | |
| 631 | - . '</p>'; | |
| 632 | - }, | |
| 633 | - 'codoc', //page | |
| 634 | - 'setting_section_id' //Section | |
| 635 | - ); | |
| 636 | - | |
| 637 | - add_settings_field( | |
| 638 | - 'cretor_info', // id | |
| 639 | - __('Creator\'s Information','codoc'), // title | |
| 640 | - function() { | |
| 641 | - global $CODOC_SETTINGS; | |
| 642 | - global $CODOC_AUTHINFO; | |
| 643 | - echo sprintf('<p><font id="codoc-update-creator-info-message"></font></p>',""); | |
| 644 | - // 変更を保存時に常に同期するのでこの表示は必要ないがUI上保持しておく | |
| 645 | - $script = "document.getElementById('codoc-update-creator-info-message').innerText='" . __("Please save changes to update the information.","codoc") . "';document.getElementById('codoc-update-creator-info-message').color='red';"; | |
| 646 | - if (isset($CODOC_AUTHINFO['profile_image_url'])) { | |
| 647 | - echo sprintf('<img src="%s" width="40" height="40" />',esc_attr($CODOC_AUTHINFO['profile_image_url'])); | |
| 648 | - } | |
| 649 | - echo sprintf('<p>%s %s %s</p>',esc_html($CODOC_AUTHINFO['name']),esc_html((isset($CODOC_AUTHINFO['account_is_pro']) and $CODOC_AUTHINFO['account_is_pro']) ? ' [PRO]' : ''),esc_html(isset($CODOC_AUTHINFO['country']) ? $CODOC_AUTHINFO['country'] : '' )); | |
| 650 | - if ($connect_code = $CODOC_SETTINGS['codoc_connect_code']) { | |
| 651 | - // Translators: %s is the integration code, and %s is additional information based on the registration mode. | |
| 652 | - echo sprintf('<p>' . esc_html(__('Extensions integration completed (Extensions code: %1$s) %2$s','codoc')) . '</p>', | |
| 653 | - esc_html($connect_code), | |
| 654 | - esc_html($CODOC_SETTINGS['codoc_connect_registration_mode'] == 'dedicated' ? __('Set the audience as a private account.','codoc') : '')); | |
| 655 | - } | |
| 656 | - echo sprintf('<p><a href="javascript:void(0);" onClick="' . esc_attr($script) . '">' . esc_html(__('Update creator\'s Information','codoc')) . '</a></p>'); | |
| 657 | - echo ('<p>' . esc_html(__('Please update each time if you change the logo or cover image on the codoc side.','codoc')) . '</p>'); | |
| 658 | - }, | |
| 659 | - 'codoc', //page | |
| 660 | - 'setting_section_id' //Section | |
| 661 | - ); | |
| 662 | - | |
| 663 | - register_setting( | |
| 664 | - 'codoc_option_group', // option group | |
| 665 | - CODOC_USERCODE_OPTION_NAME | |
| 666 | - ); | |
| 667 | - register_setting( | |
| 668 | - 'codoc_option_group', // option group | |
| 669 | - CODOC_TOKEN_OPTION_NAME | |
| 670 | - ); | |
| 671 | - | |
| 672 | - if (isset($_GET['codoc_auth_finished']) and $_GET['codoc_auth_finished']) { | |
| 673 | - add_settings_error( 'general', 'settings_updated', esc_html(__( 'codoc authentication has been completed.' ,'codoc'), 'success' )); | |
| 674 | - } | |
| 675 | - | |
| 676 | - add_settings_field( | |
| 677 | - 'codoc_auth', | |
| 678 | - __('Authentication','codoc'), | |
| 679 | - function() { | |
| 680 | - global $CODOC_USERCODE; | |
| 681 | - global $CODOC_TOKEN; | |
| 682 | - global $CODOC_AUTHINFO; | |
| 683 | - $script = "javascript:document.getElementById('codoc-usercode').value='-';document.getElementById('codoc-token').value='-';document.getElementById('codoc-auth-message').innerText='" . __('Please save changes to complete the unbinding.','codoc') . "';document.getElementById('codoc-auth-message').color='red';"; | |
| 684 | - | |
| 685 | - // Translators: %s is the email address. | |
| 686 | - echo sprintf('<p><font color="green" id="codoc-auth-message">' . esc_html(__('Authorized as %s','codoc')) . '</font></p>',esc_attr($CODOC_AUTHINFO['email'])); | |
| 687 | - | |
| 688 | - echo sprintf('<input id="codoc-usercode" type="hidden" name="%s" value="%s">',esc_attr(CODOC_USERCODE_OPTION_NAME),esc_attr($CODOC_USERCODE)); | |
| 689 | - echo sprintf('<input id="codoc-token" type="hidden" name="%s" value="%s">',esc_attr(CODOC_TOKEN_OPTION_NAME),esc_attr($CODOC_TOKEN)); | |
| 690 | - echo sprintf('<p><a href="javascript:void(0);" onClick="' . esc_attr($script) . '">' . esc_html(__('Unbind authorization','codoc')) . '</p>'); | |
| 691 | - }, | |
| 692 | - 'codoc', | |
| 693 | - 'setting_section_id' | |
| 694 | - ); | |
| 695 | - | |
| 696 | - } | |
| 697 | - | |
| 698 | - // ない場合 | |
| 699 | - if (!$CODOC_AUTHINFO and !isset($_GET['auth_by_myself'])) { | |
| 700 | - add_settings_field( | |
| 701 | - 'codoc_auth', | |
| 702 | - __('Authentication','codoc'), | |
| 703 | - function() { | |
| 704 | - $theme = wp_get_theme(); | |
| 705 | - $from = 'wp'; | |
| 706 | - if ($theme->get('Name') === 'codoc') { | |
| 707 | - $from = 'wp_codoc'; | |
| 708 | - } | |
| 709 | - //$current_url = (empty($_SERVER['HTTPS']) ? 'http://' : 'https://').$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; | |
| 710 | - $current_url = admin_url('options-general.php') . '?page=codoc'; | |
| 711 | - $direct_url = sprintf("location.href='%s&auth_by_myself=1'",$current_url); | |
| 712 | - $login_url = sprintf("location.href='%s'",$this->get_codoc_url() . '/me/token?from=' . $from . '&return_url=' . urlencode($current_url)); | |
| 713 | - $register_url = sprintf("location.href='%s'",$this->get_codoc_url() . '/register?from=' . $from . '&return_url=' . urlencode($current_url)); | |
| 714 | - // submitを消しておく | |
| 715 | - echo ('<script type="text/javascript">window.onload=function(){document.getElementById(\'submit\').style.display = \'none\'}</script>'); | |
| 716 | - if (isset($_GET['confirm_token'])) { | |
| 717 | - $confirm_url = sprintf("location.href='%s&_wpnonce=%s&fetch_token_key=%s&usercode=%s'",$current_url, | |
| 718 | - wp_create_nonce('fetch_token_nonce'), | |
| 719 | - esc_attr($_GET['fetch_token_key']), | |
| 720 | - esc_attr($_GET['usercode'])); | |
| 721 | - echo (esc_html(__('The authentication information has been obtained. Please click the confirmation button to complete the setup.','codoc')) . '<br />'); | |
| 722 | - echo sprintf('<input type="button" class="button button-primary" value="%s" onClick="%s"> ',esc_html(__('Confirm authentication','codoc')),esc_attr($confirm_url)); | |
| 723 | - } else { | |
| 724 | - | |
| 725 | - echo (esc_html(__('Authentication is required to use codoc on WordPress.','codoc')) . '<br />'); | |
| 726 | - echo (esc_html(__('You can authenticate by directly entering the user code and API token, or by logging in and registering with codoc.','codoc')) . '<br /><br />'); | |
| 727 | - echo sprintf('<input type="button" class="button button-primary" value="%s" onClick="%s"> ',esc_attr(__('Authenticate by direct input','codoc')),esc_attr($direct_url)); | |
| 728 | - echo sprintf('<input type="button" class="button button-primary" value="%s" onClick="%s"> <input type="button" class="button button-primary" value="%s" onClick="%s"><br /><br />',esc_attr(__('Login and authenticate','codoc')),esc_attr($login_url),esc_attr(__('Register and authenticate','codoc')),esc_attr($register_url)); | |
| 729 | - } | |
| 730 | - if (!$this->util->health_check()) { | |
| 731 | - echo sprintf('<p style="color: red;">Cannot communicate with the codoc server. Please allow communication to https://codoc.jp in your firewall settings on the server or WordPress side.</p>'); | |
| 732 | - } | |
| 733 | - | |
| 734 | - }, | |
| 735 | - 'codoc', | |
| 736 | - 'setting_section_id' | |
| 737 | - ); | |
| 738 | - } | |
| 739 | - // ない場合かつ自分で認証する場合 | |
| 740 | - if (!$CODOC_AUTHINFO and ( | |
| 741 | - (isset($_GET['auth_by_myself']) and $_GET['auth_by_myself']) or | |
| 742 | - (isset($_POST['auth_by_myself']) and $_POST['auth_by_myself']) | |
| 743 | - )) { | |
| 744 | - register_setting( | |
| 745 | - 'codoc_option_group', // option group | |
| 746 | - CODOC_USERCODE_OPTION_NAME | |
| 747 | - ); | |
| 748 | - register_setting( | |
| 749 | - 'codoc_option_group', // option group | |
| 750 | - CODOC_TOKEN_OPTION_NAME | |
| 751 | - ); | |
| 752 | - add_settings_field( | |
| 753 | - 'codoc_usercode', | |
| 754 | - 'ユーザーコード', | |
| 755 | - function() { | |
| 756 | - global $CODOC_USERCODE; | |
| 757 | - echo '<input type="hidden" name="auth_by_myself" value="1">'; | |
| 758 | - echo sprintf('<input type="text" name="%s" value="%s">',esc_attr(CODOC_USERCODE_OPTION_NAME),esc_attr($CODOC_USERCODE)); | |
| 759 | - }, | |
| 760 | - 'codoc', | |
| 761 | - 'setting_section_id' | |
| 762 | - ); | |
| 763 | - add_settings_field( | |
| 764 | - 'codoc_token', | |
| 765 | - 'APIトークン', | |
| 766 | - function() { | |
| 767 | - global $CODOC_TOKEN; | |
| 768 | - echo sprintf('<input type="text" name="%s" value="%s">',esc_attr(CODOC_TOKEN_OPTION_NAME),esc_attr($CODOC_TOKEN)); | |
| 769 | - }, | |
| 770 | - 'codoc', | |
| 771 | - 'setting_section_id' | |
| 772 | - ); | |
| 773 | - } | |
| 774 | - | |
| 775 | - // 認証情報を保存 | |
| 776 | - add_action( 'update_option_' . CODOC_USERCODE_OPTION_NAME, function( $old_value, $new_value ) { | |
| 777 | - global $CODOC_RE_AUTHORIZE; | |
| 778 | - $CODOC_RE_AUTHORIZE = 1; | |
| 779 | - },9,2); // $hook, $function_to_add, $priority, $accepted_args | |
| 780 | - add_action( 'update_option_' . CODOC_TOKEN_OPTION_NAME, function( $old_value, $new_value ) { | |
| 781 | - global $CODOC_RE_AUTHORIZE; | |
| 782 | - $CODOC_RE_AUTHORIZE = 1; | |
| 783 | - },10,2); | |
| 784 | - | |
| 785 | - add_action('update_option_' . CODOC_SETTINGS_OPTION_NAME,function(){ | |
| 786 | - $CODOC_SETTINGS = get_option(CODOC_SETTINGS_OPTION_NAME); | |
| 787 | - if (isset($CODOC_SETTINGS['always_show_support']) and $CODOC_SETTINGS['always_show_support']) { | |
| 788 | - $data = $this->util->get_support_entry([],[ | |
| 789 | - "usercode" => get_option(CODOC_USERCODE_OPTION_NAME), | |
| 790 | - "token" => get_option(CODOC_TOKEN_OPTION_NAME), | |
| 791 | - ]); | |
| 792 | - if ($data and $data->status and $entry = $data->entry) { | |
| 793 | - update_option(CODOC_SUPPORT_ENTRYCODE_OPTION_NAME,$entry->code); | |
| 794 | - } else { | |
| 795 | - update_option(CODOC_SUPPORT_ENTRYCODE_OPTION_NAME,''); | |
| 796 | - } | |
| 797 | - } elseif(isset($CODOC_SETTINGS['always_show_support']) and !$CODOC_SETTINGS['always_show_support']) { | |
| 798 | - update_option(CODOC_SUPPORT_ENTRYCODE_OPTION_NAME,''); | |
| 799 | - } | |
| 800 | - }); | |
| 801 | - | |
| 802 | - add_action('updated_option',function(){ | |
| 803 | - global $CODOC_RE_AUTHORIZE; | |
| 804 | - if ($CODOC_RE_AUTHORIZE) { | |
| 805 | - //$data = $this->callAPI('GET',''); | |
| 806 | - $data = $this->util->get_user_info([],[ | |
| 807 | - "usercode" => get_option(CODOC_USERCODE_OPTION_NAME), | |
| 808 | - "token" => get_option(CODOC_TOKEN_OPTION_NAME), | |
| 809 | - ]); | |
| 810 | - if ($data->status and $user = $data->user) { | |
| 811 | - $this->update_codoc_authinfo($user); | |
| 812 | - } else { | |
| 813 | - update_option(CODOC_AUTHINFO_OPTION_NAME,''); | |
| 814 | - } | |
| 815 | - } | |
| 816 | - }); | |
| 817 | - | |
| 818 | - }); | |
| 819 | - // プラグイン一覧に設定のリンクをいれる | |
| 820 | - add_filter( 'plugin_action_links_' . plugin_basename( plugin_dir_path( __FILE__ ) . 'codoc' . '.php' ), | |
| 821 | - function( $links ) { | |
| 822 | - $setting_link = sprintf( '<a href="%s">%s</a>', esc_url( add_query_arg( 'page', 'codoc', admin_url( 'options-general.php' ) ) ), esc_html( __('Settings') ) ); | |
| 823 | - array_unshift( $links, $setting_link ); | |
| 824 | - | |
| 825 | - return $links; | |
| 826 | - } | |
| 827 | - ); | |
| 828 | - | |
| 829 | - } | |
| 830 | - public function add_mce() { | |
| 831 | - add_action( 'admin_enqueue_scripts', function() { | |
| 832 | - if ( ! current_user_can( 'edit_posts' ) && ! current_user_can( 'edit_pages' ) ) { | |
| 833 | - return; | |
| 834 | - } | |
| 835 | - $current_screen = get_current_screen(); | |
| 836 | - if ( ( method_exists( $current_screen, 'is_block_editor' ) && $current_screen->is_block_editor() ) || ( function_exists( 'is_gutenberg_page' ) && is_gutenberg_page() ) ) { | |
| 837 | - // Gutenberg Editor | |
| 838 | - // なにもしない | |
| 839 | - } else { | |
| 840 | - // tinymce | |
| 841 | - if ( get_user_option( 'rich_editing' ) == 'true' ) { | |
| 842 | - // プラグイン内で使うCSRF を生成 | |
| 843 | - $path = plugins_url( 'codoc/src_mce/codoc-editor-onload.js', __DIR__ ); | |
| 844 | - wp_enqueue_script( 'codoc-editor-onload', $path, array('jquery'), '', true ); | |
| 845 | - // I just want to insert this global variables but i don't know how i can do it.. | |
| 846 | - $auth_info = get_option(CODOC_AUTHINFO_OPTION_NAME); | |
| 847 | - $codoc_settings = get_option(CODOC_SETTINGS_OPTION_NAME); | |
| 848 | - $block_defaults_raw = isset($codoc_settings['block_defaults']) ? $codoc_settings['block_defaults'] : ''; | |
| 849 | - $block_defaults_decoded = json_decode($block_defaults_raw, true); | |
| 850 | - $block_defaults = is_array($block_defaults_decoded) ? $block_defaults_decoded : new stdClass(); | |
| 851 | - wp_localize_script( | |
| 852 | - 'codoc-editor-onload', | |
| 853 | - 'CODOCEDITOR', | |
| 854 | - array( | |
| 855 | - 'action' => 'codoc_shortcodes', | |
| 856 | - 'nonce' => wp_create_nonce( 'codoc_shortcodes' ), | |
| 857 | - | |
| 858 | - 'codoc_url' => $this->get_codoc_url(), | |
| 859 | - 'codoc_usercode' => get_option(CODOC_USERCODE_OPTION_NAME), | |
| 860 | - 'codoc_plugin_version' => CODOC_PLUGIN_VERSION, | |
| 861 | - 'codoc_sdk_path' => CODOC_SDK_PATH, | |
| 862 | - 'codoc_account_is_pro' => isset($auth_info['account_is_pro']) ? $auth_info['account_is_pro'] : 0, | |
| 863 | - 'codoc_currency_code' => isset($auth_info['currency_code']) ? $auth_info['currency_code'] : 0, | |
| 864 | - 'codoc_currency_decimal_places' => isset($auth_info['currency_decimal_places']) ? $auth_info['currency_decimal_places'] : 0, | |
| 865 | - 'codoc_block_defaults' => $block_defaults, | |
| 866 | - ) | |
| 867 | - ); | |
| 868 | - | |
| 869 | - // ここでtinymceのプラグイン追加 | |
| 870 | - //wp_enqueue_style( 'codoc-admin-style', plugins_url( 'codoc/src_mce/codoc-admin.css', __DIR__ ) ); | |
| 871 | - //add_editor_style( plugins_url( 'codoc/src_mce/codoc-admin.css', __DIR__ ) ); | |
| 872 | - wp_enqueue_style( 'codoc-admin-style', $this->get_codoc_url() . CODOC_SDK_PATH . '.tinymce.css?c=' . date('Ymd') ); | |
| 873 | - add_editor_style( $this->get_codoc_url() . CODOC_SDK_PATH . '.tinymce.css' ); | |
| 874 | - | |
| 875 | - add_filter( 'mce_external_plugins', function( $plugin_array ) { | |
| 876 | - //$plugin_array['codoc'] = plugins_url( 'codoc/src_mce/codoc-editor.js', __DIR__ ); | |
| 877 | - $plugin_array['codoc'] = $this->get_codoc_url() . CODOC_SDK_PATH . '.tinymce.js?c=' . date('Ymd'); | |
| 878 | - return $plugin_array; | |
| 879 | - } ); | |
| 880 | - add_filter( 'mce_buttons', function( $buttons ) { | |
| 881 | - array_push( $buttons, "|", "codoc" ); | |
| 882 | - return $buttons; | |
| 883 | - } ); | |
| 884 | - // 非SSL環境の場合、なぜかhttps -> httpsになってしまうので対策 | |
| 885 | - // コメントタグが除去されることがあるらしいのでvalid_elementsに必要なタグを追加 (EXPERIMENTAL) | |
| 886 | - add_filter( 'tiny_mce_before_init', function($settings) { | |
| 887 | - $settings['external_plugins'] = preg_replace('/"codoc":"http:/','"codoc":"https:',$settings['external_plugins']); | |
| 888 | - foreach (['valid_elements','extended_valid_elements'] as $valid_elements) { | |
| 889 | - if (isset($settings[$valid_elements])) { | |
| 890 | - $settings[$valid_elements] = $settings[$valid_elements] . ',div[*],p[*],img[*],--[*]'; | |
| 891 | - } | |
| 892 | - } | |
| 893 | - $CODOC_SETTINGS = get_option(CODOC_SETTINGS_OPTION_NAME); | |
| 894 | - if (isset($CODOC_SETTINGS["debug_params"])) { | |
| 895 | - $params_decoded = json_decode($CODOC_SETTINGS["debug_params"],true); | |
| 896 | - if (isset($params_decoded["mce_valid_elements"])) { | |
| 897 | - $settings['valid_elements'] = $params_decoded["mce_valid_elements"]; | |
| 898 | - $settings['extended_valid_elements'] = $params_decoded["mce_valid_elements"]; | |
| 899 | - } | |
| 900 | - } | |
| 901 | - | |
| 902 | - return $settings; | |
| 903 | - },1000000); | |
| 904 | - } | |
| 905 | - } | |
| 906 | - } ); | |
| 907 | - } | |
| 908 | - function update_codoc_authinfo($user) { | |
| 909 | - global $CODOC_SETTINGS; | |
| 910 | - global $CODOC_AUTHINFO; | |
| 911 | - if (property_exists($user,'connect_code') and $user->connect_code) { | |
| 912 | - $CODOC_SETTINGS = get_option(CODOC_SETTINGS_OPTION_NAME); | |
| 913 | - $CODOC_SETTINGS['codoc_connect_code'] = $user->connect_code; | |
| 914 | - $CODOC_SETTINGS['codoc_connect_registration_mode'] = $user->connect_has_permission_dedicated_account ? 'dedicated' : ''; | |
| 915 | - update_option(CODOC_SETTINGS_OPTION_NAME,$CODOC_SETTINGS); | |
| 916 | - } | |
| 917 | - return update_option(CODOC_AUTHINFO_OPTION_NAME,[ | |
| 918 | - 'email' => $user->email, | |
| 919 | - 'name' => $user->name, | |
| 920 | - 'profile_image_url' => $user->profile_image_url, | |
| 921 | - 'cover_image_url' => $user->cover_image_url, | |
| 922 | - 'connect_code' => property_exists($user,'connect_code') ? $user->connect_code : '', | |
| 923 | - 'connect_image_url' => property_exists($user,'connect_image_url') ? $user->connect_image_url : '', | |
| 924 | - 'account_is_pro' => property_exists($user,'account_is_pro') ? $user->account_is_pro : '', | |
| 925 | - 'created_at' => property_exists($user,'created_at') ? $user->created_at : 0, | |
| 926 | - 'country' => property_exists($user,'country') ? $user->country : '', | |
| 927 | - 'currency_code' => property_exists($user,'currency_code') ? $user->currency_code : '', | |
| 928 | - 'currency_decimal_places' => property_exists($user,'currency_decimal_places') ? $user->currency_decimal_places : '', | |
| 929 | - ]); | |
| 930 | - $CODOC_AUTHINFO = get_option(CODOC_AUTHINFO_OPTION_NAME); | |
| 931 | - return $CODOC_AUTHINFO; | |
| 932 | - } | |
| 933 | - // 保存用コンテンツにフィルターを実施する | |
| 934 | - public function get_filtered_content($post_emulated) { | |
| 935 | - // 記事ページであることをエミュレートしてフィルター実行 | |
| 936 | - $post_backuped = null; | |
| 937 | - if (isset($GLOBALS['post'])) { | |
| 938 | - $post_backuped = $GLOBALS['post']; | |
| 939 | - } | |
| 940 | - $GLOBALS['post'] = $post_emulated; | |
| 941 | - | |
| 942 | - $wp_query_backuped = null; | |
| 943 | - if (isset($GLOBALS['wp_query'])) { | |
| 944 | - $wp_query_backuped = $GLOBALS['wp_query']; | |
| 945 | - $wp_query = $GLOBALS['wp_query']; | |
| 946 | - $wp_query->is_single = true; | |
| 947 | - $wp_query->is_singular = true; | |
| 948 | - # in_the_loop は記事ページでは通常有効っぽいので true指定 ex: wp_ulike | |
| 949 | - $wp_query->in_the_loop = true; | |
| 950 | - # これも追加しておく | |
| 951 | - $wp_query->is_main_query = true; | |
| 952 | - | |
| 953 | - $wp_query->post = $post_emulated; | |
| 954 | - $wp_query->queried_object = $post_emulated; | |
| 955 | - $wp_query->queried_object_id = $post_emulated->ID; | |
| 956 | - | |
| 957 | - $GLOBALS['wp_query'] = $wp_query; | |
| 958 | - } | |
| 959 | - // $this->the_content でオリジナルを返してもらうため | |
| 960 | - $this->do_not_filter_the_content = true; | |
| 961 | - $content = preg_replace( | |
| 962 | - '/ (\/)?wp:codoc\/codoc-block /',' \\1wptmp:codoc/codoc-block ', | |
| 963 | - $post_emulated->post_content | |
| 964 | - ); | |
| 965 | - // post_metaに内容を保存し、無料パートにショートコードの退避をおこなう | |
| 966 | - $codoc_settings = get_option(CODOC_SETTINGS_OPTION_NAME); | |
| 967 | - if (isset($codoc_settings['shortcode_evacuation']) and $codoc_settings['shortcode_evacuation']) { | |
| 968 | - $splited = preg_split('/\/wptmp:codoc\/codoc-block/s',$content); | |
| 969 | - if (isset($splited[0]) and isset($splited[1])) { | |
| 970 | - // すべてのショートコードを $match_all にいれる | |
| 971 | - preg_match_all('/\[[^\[\]]+?\](.+\[\/[^\[\]]+?\])?/',$splited[1],$match_all); | |
| 972 | - // ショートコードをURLエンコードしてdivタグの中にいれておく(後でmetaの中のショートコードと付け合せ) | |
| 973 | - $replaced = preg_replace_callback('/\[[^\[\]]+?\](.+\[\/[^\[\]]+?\])?/',function($matches) { | |
| 974 | - return sprintf ('<div class="codoc-evacuation-dests" data-shortcode="%s"></div>', urlencode($matches[0])); | |
| 975 | - },$splited[1]); | |
| 976 | - // 0番目に配列で入っているのでそこを退避対象の配列とする | |
| 977 | - $evacuations = $match_all[0]; | |
| 978 | - // ショートコードの中身をタグに退避 | |
| 979 | - $content = $splited[0] . '/wptmp:codoc/codoc-block' . $replaced; | |
| 980 | - // 無料パートでショートコードを実行できるようにする | |
| 981 | - update_post_meta($post_emulated->ID,'codoc_shortcode_evacuations',join('**codoc**',$evacuations)); | |
| 982 | - } else { | |
| 983 | - update_post_meta($post_emulated->id,'codoc_shortcode_evacuations',""); | |
| 984 | - } | |
| 985 | - } | |
| 986 | - #$content = do_shortcode( $content ); | |
| 987 | - $content_filtered = apply_filters( 'the_content', $content); | |
| 988 | - $this->do_not_filter_the_content = false; | |
| 989 | - | |
| 990 | - $GLOBALS['post'] = $post_backuped; | |
| 991 | - | |
| 992 | - if ($wp_query_backuped) { | |
| 993 | - $GLOBALS['wp_query'] = $wp_query_backuped; | |
| 994 | - } | |
| 995 | - | |
| 996 | - $content_filtered = preg_replace( | |
| 997 | - '/ (\/)?wptmp:codoc\/codoc-block /',' \\1wp:codoc/codoc-block ', | |
| 998 | - $content_filtered | |
| 999 | - ); | |
| 1000 | - return $content_filtered; | |
| 1001 | - } | |
| 1002 | - | |
| 1003 | - public function save_post($post_ID,$post,$update) { | |
| 1004 | - if (preg_match('/^(auto-draft|inherit)$/',$post->post_status)) { | |
| 1005 | - return; | |
| 1006 | - } | |
| 1007 | - $entryCode = get_post_meta($post_ID,'codoc_entry_code',true); | |
| 1008 | - $postId = get_post_meta($post_ID,'codoc_saved_post_id',true); | |
| 1009 | - // 保存されているIDと違う場合は破棄(duplicate pageなどでmeta情報をコピーされた可能性がある) | |
| 1010 | - if ($postId and $postId != $post_ID) { | |
| 1011 | - $entryCode = ''; | |
| 1012 | - } | |
| 1013 | - $codoc_settings = get_option(CODOC_SETTINGS_OPTION_NAME); | |
| 1014 | - $post_content = (isset($codoc_settings['do_not_filter_the_content']) and $codoc_settings['do_not_filter_the_content']) ? | |
| 1015 | - $post->post_content : $this->get_filtered_content($post); | |
| 1016 | - | |
| 1017 | - // codoc タグ(span/div または Gutenberg ブロック)が含まれていれば API側と統一した文字数検証を行う | |
| 1018 | - $has_codoc_tag = preg_match('/<(?:span|div)[^>]+data-id="codoc-tag"/', $post_content) | |
| 1019 | - || preg_match('/wp:codoc\/codoc-block/', $post_content); | |
| 1020 | - if ($has_codoc_tag) { | |
| 1021 | - // codoc タグ位置で free / paywalled を分割 | |
| 1022 | - $gutenberg_split = '/(?:<(?:div|p)(?:[^>]+|)>|)<\!-- +wp:codoc\/codoc-block .*<\!-- +\/wp:codoc\/codoc-block +-->(?:<\/(?:div|p)>|)/s'; | |
| 1023 | - $tag_split = '/<(?:span|div)[^>]+data-id="codoc-tag"(?:[^>]+|)>(?:.+|)<\/(?:span|div)>/'; | |
| 1024 | - $end_tag_regex = '/<(?:div|p)>::CODOC_WP_END_PAYWALL::<\/(?:div|p)>/'; | |
| 1025 | - $for_split = preg_split($end_tag_regex, $post_content); | |
| 1026 | - $for_split = $for_split[0]; | |
| 1027 | - if (preg_match('/wp:codoc\/codoc-block/', $for_split)) { | |
| 1028 | - $splited = preg_split($gutenberg_split, $for_split); | |
| 1029 | - } else { | |
| 1030 | - $splited = preg_split($tag_split, $for_split); | |
| 1031 | - } | |
| 1032 | - $body_free = isset($splited[0]) ? $splited[0] : ''; | |
| 1033 | - $body_paywalled = isset($splited[1]) ? $splited[1] : ''; | |
| 1034 | - $errors = $this->util->validate_entry_lengths([ | |
| 1035 | - 'title' => $post->post_title, | |
| 1036 | - 'body_free' => $body_free, | |
| 1037 | - 'body_paywalled' => $body_paywalled, | |
| 1038 | - 'binded_url' => get_permalink($post_ID), | |
| 1039 | - ]); | |
| 1040 | - if ($errors) { | |
| 1041 | - set_transient('codoc_entry_validation_errors', $errors, MINUTE_IN_SECONDS * 5); | |
| 1042 | - // 同期はスキップ | |
| 1043 | - return true; | |
| 1044 | - } | |
| 1045 | - } | |
| 1046 | - | |
| 1047 | - $res = $this->util->sync_entry([ | |
| 1048 | - "post_title" => $post->post_title, | |
| 1049 | - "post_content" => $post_content, | |
| 1050 | - // password が設定されてる場合は限定公開にする | |
| 1051 | - "post_status" => $post->post_status == 'publish' ? ($post->post_password ? 2 : 1) : 0, | |
| 1052 | - "post_permalink" => get_permalink($post_ID), | |
| 1053 | - "codoc_entry_code" => $entryCode, | |
| 1054 | - "codoc_settings" => $codoc_settings, | |
| 1055 | - ],[ | |
| 1056 | - "usercode" => get_option(CODOC_USERCODE_OPTION_NAME), | |
| 1057 | - "token" => get_option(CODOC_TOKEN_OPTION_NAME), | |
| 1058 | - ]); | |
| 1059 | - $prudent_update_post_meta_entry_code = null; | |
| 1060 | - if (isset($codoc_settings["debug_params"])) { | |
| 1061 | - $params_decoded = json_decode($codoc_settings["debug_params"],true); | |
| 1062 | - if (isset($params_decoded["prudent_update_post_meta_entry_code"])) { | |
| 1063 | - $prudent_update_post_meta_entry_code = $params_decoded["prudent_update_post_meta_entry_code"]; | |
| 1064 | - } | |
| 1065 | - } | |
| 1066 | - if ($prudent_update_post_meta_entry_code == 2) { | |
| 1067 | - error_log('CODOC:: ' . sprintf ("%s : %s : %s : %s", $post_ID, (is_object($res) ? "1" : 0),$res->status,$entryCode)); | |
| 1068 | - } | |
| 1069 | - if (is_object($res) and $res->status and !$entryCode) { | |
| 1070 | - // 20230222 https://stackoverflow.com/questions/26640785/update-post-meta-not-work-only-when-save-data-not-for-update | |
| 1071 | - if ($prudent_update_post_meta_entry_code) { | |
| 1072 | - add_post_meta($post_ID,'codoc_entry_code',$res->entry->code); | |
| 1073 | - add_post_meta($post_ID,'codoc_saved_post_id',$post_ID); | |
| 1074 | - } else { | |
| 1075 | - update_post_meta($post_ID,'codoc_entry_code',$res->entry->code); | |
| 1076 | - update_post_meta($post_ID,'codoc_saved_post_id',$post_ID); | |
| 1077 | - } | |
| 1078 | - } | |
| 1079 | - return true; | |
| 1080 | - } | |
| 1081 | - function updated_post_meta( $meta_ID, $post_ID, $meta_key ) { | |
| 1082 | - // スルーされてる場合は他のメタ情報更新のタイミングにサムネイルをアップロード | |
| 1083 | - $has_to_resend = get_post_meta($post_ID,'codoc_post_thumbnail_invoking_entry_code',true); | |
| 1084 | - if ($has_to_resend != 1 and $meta_key != '_thumbnail_id') { | |
| 1085 | - return; | |
| 1086 | - } | |
| 1087 | - if ( has_post_thumbnail($post_ID) ) { | |
| 1088 | - // 新規投稿の場合、タイミングによってはcodocEntryCodeが取得できないので一旦スルーする | |
| 1089 | - if (!get_post_meta($post_ID,'codoc_entry_code',true)) { | |
| 1090 | - update_post_meta($post_ID,'codoc_post_thumbnail_invoking_entry_code',1); | |
| 1091 | - return; | |
| 1092 | - } else { | |
| 1093 | - update_post_meta($post_ID,'codoc_post_thumbnail_invoking_entry_code',0); | |
| 1094 | - } | |
| 1095 | - $attachment = wp_get_attachment_metadata( get_post_thumbnail_id($post_ID)); | |
| 1096 | - $upload_dir = wp_upload_dir(); | |
| 1097 | - $file_path = sprintf ('%s/%s',$upload_dir['basedir'],$attachment['file']); | |
| 1098 | - | |
| 1099 | - return $this->util->post_thumbnail([ | |
| 1100 | - "file_path" => $file_path, | |
| 1101 | - "boundary" => wp_generate_password(24), | |
| 1102 | - "codoc_entry_code" => get_post_meta($post_ID,'codoc_entry_code',true), | |
| 1103 | - ],[ | |
| 1104 | - "usercode" => get_option(CODOC_USERCODE_OPTION_NAME), | |
| 1105 | - "token" => get_option(CODOC_TOKEN_OPTION_NAME), | |
| 1106 | - ]); | |
| 1107 | - } | |
| 1108 | - } | |
| 1109 | - function deleted_post_meta( $meta_ids, $post_ID, $meta_key,$meta_value ) { | |
| 1110 | - if ($meta_key != '_thumbnail_id') { | |
| 1111 | - return; | |
| 1112 | - } | |
| 1113 | - return $this->util->reset_thumbnail( | |
| 1114 | - ["codoc_entry_code" => get_post_meta($post_ID,'codoc_entry_code',true)], | |
| 1115 | - [ | |
| 1116 | - "usercode" => get_option(CODOC_USERCODE_OPTION_NAME), | |
| 1117 | - "token" => get_option(CODOC_TOKEN_OPTION_NAME), | |
| 1118 | - ] | |
| 1119 | - ); | |
| 1120 | - } | |
| 1121 | - // 退避されてるかどうかをmetaを使って確認し、無料エリアの一番最後にショートコードを追加 | |
| 1122 | - function the_content_shortcode_evacuations( $post_content ) { | |
| 1123 | - if ($this->do_not_filter_the_content) { | |
| 1124 | - return $post_content; | |
| 1125 | - } | |
| 1126 | - $post = get_post(); | |
| 1127 | - if (!$post) { | |
| 1128 | - return $post_content; | |
| 1129 | - } | |
| 1130 | - $evacuations_meta = get_post_meta($post->ID,'codoc_shortcode_evacuations',true); | |
| 1131 | - $evacuations = preg_split('/\*\*codoc\*\*/',$evacuations_meta); | |
| 1132 | - foreach ($evacuations as $evacuation) { | |
| 1133 | - $post_content = sprintf('<div class="codoc-evacuations" style="display:none;" data-shortcode="%s">%s</div>', | |
| 1134 | - urlencode($evacuation),$evacuation) . $post_content; | |
| 1135 | - } | |
| 1136 | - return $post_content; | |
| 1137 | - } | |
| 1138 | - function the_content( $post_content ) { | |
| 1139 | - if ( is_single() && in_the_loop() && is_main_query() ) { | |
| 1140 | - } | |
| 1141 | - // get_filtered_content から do_not_filter_the_contentを有効にされるパターン | |
| 1142 | - // オプションの設定値の意味合い(他のフィルタを無視)とは違うため注意 | |
| 1143 | - if ($this->do_not_filter_the_content) { | |
| 1144 | - return $post_content; | |
| 1145 | - } | |
| 1146 | - $post = get_post(); | |
| 1147 | - // 20211215 null になる場合がある | |
| 1148 | - if (!$post) { | |
| 1149 | - return $post_content; | |
| 1150 | - } | |
| 1151 | - # is_amp で実装しているテンプレート用 | |
| 1152 | - $is_amp_endpoint = (function_exists('is_amp_endpoint') && is_amp_endpoint()) ? true : | |
| 1153 | - ((function_exists('is_amp') && is_amp()) ? true : false); | |
| 1154 | - return $this->util->filter_content([ | |
| 1155 | - "post_content" => $post_content, | |
| 1156 | - "preview" => is_preview(), | |
| 1157 | - "codoc_entry_code" => get_post_meta($post->ID,'codoc_entry_code',true), | |
| 1158 | - "codoc_settings" => get_option(CODOC_SETTINGS_OPTION_NAME), | |
| 1159 | - "is_amp_endpoint" => $is_amp_endpoint, | |
| 1160 | - "post_permalink" => get_permalink($post->ID), | |
| 1161 | - "codoc_support_entry_code" => get_option(CODOC_SUPPORT_ENTRYCODE_OPTION_NAME), | |
| 1162 | - ]); | |
| 1163 | - } | |
| 1164 | - | |
| 1165 | - function excerpt_allowed_blocks ($allowed_blocks) { | |
| 1166 | - if (is_array($allowed_blocks)) { | |
| 1167 | - array_push($allowed_blocks,'codoc/codoc-block'); | |
| 1168 | - } | |
| 1169 | - return $allowed_blocks; | |
| 1170 | - } | |
| 1171 | - | |
| 1172 | - function allowed_block_types_all ($allowed_block_types, $block_editor_context) { | |
| 1173 | - // テーマ等がブロックを制限していない場合(true = 全ブロック許可)はそのまま返す | |
| 1174 | - if ($allowed_block_types === true) { | |
| 1175 | - return $allowed_block_types; | |
| 1176 | - } | |
| 1177 | - // 配列で制限されている場合、codoc ブロックを追加 | |
| 1178 | - if (is_array($allowed_block_types) && !in_array('codoc/codoc-block', $allowed_block_types)) { | |
| 1179 | - $allowed_block_types[] = 'codoc/codoc-block'; | |
| 1180 | - } | |
| 1181 | - return $allowed_block_types; | |
| 1182 | - } | |
| 1183 | - | |
| 1184 | - function show_validation_notices() { | |
| 1185 | - $errors = get_transient('codoc_entry_validation_errors'); | |
| 1186 | - if (!$errors || !is_array($errors)) { | |
| 1187 | - return; | |
| 1188 | - } | |
| 1189 | - delete_transient('codoc_entry_validation_errors'); | |
| 1190 | - $header = __('codoc could not sync this entry due to length limits:', 'codoc'); | |
| 1191 | - echo '<div class="notice notice-error is-dismissible"><p><strong>' . esc_html($header) . '</strong></p><ul>'; | |
| 1192 | - foreach ($errors as $message) { | |
| 1193 | - echo '<li>' . esc_html($message) . '</li>'; | |
| 1194 | - } | |
| 1195 | - echo '</ul></div>'; | |
| 1196 | - } | |
| 1197 | - | |
| 1198 | - function show_tadv_notice() { | |
| 1199 | - // API からの notification を表示 | |
| 1200 | - $api_notifications = get_transient('codoc_api_notification'); | |
| 1201 | - if ($api_notifications && is_array($api_notifications)) { | |
| 1202 | - // notification 配列のキーと値を表示 | |
| 1203 | - foreach ($api_notifications as $key => $value) { | |
| 1204 | - // 値が配列やオブジェクトの場合は json_encode で文字列化 | |
| 1205 | - if (is_array($value) || is_object($value)) { | |
| 1206 | - $value = json_encode($value, JSON_UNESCAPED_UNICODE); | |
| 1207 | - } | |
| 1208 | - echo "<div class=\"notice notice-warning is-dismissible codoc-notification\"><p>" . esc_html($value) . "</p></div>"; | |
| 1209 | - } | |
| 1210 | - //delete_transient('codoc_api_notification'); | |
| 1211 | - } | |
| 1212 | - | |
| 1213 | - $name = 'Advanced Editor Tools'; | |
| 1214 | - $name_escaped = preg_replace('/ /','+',$name); | |
| 1215 | - $install_url = network_admin_url( "plugin-install.php?tab=search&s=" . $name ); | |
| 1216 | - $options_url = network_admin_url( "options-general.php?page=tinymce-advanced" ); | |
| 1217 | - | |
| 1218 | - //<a href=\"%s\">%s</a> を有効にし、設定画面にて "Keep paragraph tags in the Classic block and the Classic Editor" を有効にしてください。 | |
| 1219 | - //<a href=\"%s\">%s</a> の設定画面にて "Keep paragraph tags in the Classic block and the Classic Editor" を有効にしてください。 | |
| 1220 | - // Translators: %s is a link to the installation page with the plugin name. | |
| 1221 | - $tadv_message_for_install = sprintf(__("Please enable %s and enable "Keep paragraph tags in the Classic block and the Classic Editor" on the settings screen.","codoc"),sprintf("<a href=\"%s\">%s</a>",$install_url,$name)); | |
| 1222 | - // Translators: %s is a link to the options settings page with the plugin name. | |
| 1223 | - $tadv_message_for_options = sprintf(__("Please enable "Keep paragraph tags in the Classic block and the Classic Editor" on the %s settings screen.","codoc"),sprintf("<a href=\"%s\">%s</a>",$options_url,$name)); | |
| 1224 | - // クラシックエディタで advanced editor tools を入れてない場合は notice | |
| 1225 | - if (is_plugin_active('classic-editor/classic-editor.php') and | |
| 1226 | - !is_plugin_active('tinymce-advanced/tinymce-advanced.php')) { | |
| 1227 | - echo "<div class=\"notice notice-warning is-dismissible\"><p>" . esc_html($tadv_message_for_install) . "</p></div>"; | |
| 1228 | - } | |
| 1229 | - | |
| 1230 | - // advanced editor tools の設定を調査 | |
| 1231 | - $no_autop = false; | |
| 1232 | - $tadv_admin_settings = get_option( 'tadv_admin_settings', false ); | |
| 1233 | - if (isset($tadv_admin_settings['options']) and preg_match('/no_autop/',$tadv_admin_settings['options'])) { | |
| 1234 | - $no_autop = true; | |
| 1235 | - } | |
| 1236 | - // advanced editor tools が有効で no_autop (Keep paragraph tags in the Classic block and the Classic Editor) が無効 | |
| 1237 | - if (is_plugin_active('tinymce-advanced/tinymce-advanced.php') and !$no_autop) { | |
| 1238 | - echo "<div class=\"notice notice-warning is-dismissible\"><p>" . esc_html($tadv_message_for_options) . "</p></div>"; | |
| 1239 | - } | |
| 1240 | - } | |
| 1241 | -} | |