PluginProbe
codoc / 0.9.61
codoc v0.9.61
0.9.61 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 All 115 releases
codoc / class-codoc.php

class-codoc.php in codoc 0.9.61, at class-codoc.php

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