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