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