PluginProbe
codoc / trunk
codoc vtrunk
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
codoc / class-codoc.php

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

1,261 lines 77.0 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')) . '</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 . 'statusLimited (bool: false), '
632 . 'subscriptions (object: {})'
633 . '</p>';
634 },
635 'codoc', //page
636 'setting_section_id' //Section
637 );
638
639 add_settings_field(
640 'cretor_info', // id
641 __('Creator\'s Information','codoc'), // title
642 function() {
643 global $CODOC_SETTINGS;
644 global $CODOC_AUTHINFO;
645 echo sprintf('<p><font id="codoc-update-creator-info-message"></font></p>',"");
646 // 変更を保存時に常に同期するのでこの表示は�
647 要ないがUI上保持しておく
648 $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';";
649 if (isset($CODOC_AUTHINFO['profile_image_url'])) {
650 echo sprintf('<img src="%s" width="40" height="40" />',esc_attr($CODOC_AUTHINFO['profile_image_url']));
651 }
652 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'] : '' ));
653 if ($connect_code = $CODOC_SETTINGS['codoc_connect_code']) {
654 // Translators: %s is the integration code, and %s is additional information based on the registration mode.
655 echo sprintf('<p>' . esc_html(__('Extensions integration completed (Extensions code: %1$s) %2$s','codoc')) . '</p>',
656 esc_html($connect_code),
657 esc_html($CODOC_SETTINGS['codoc_connect_registration_mode'] == 'dedicated' ? __('Set the audience as a private account.','codoc') : ''));
658 }
659 echo sprintf('<p><a href="javascript:void(0);" onClick="' . esc_attr($script) . '">' . esc_html(__('Update creator\'s Information','codoc')) . '</a></p>');
660 echo ('<p>' . esc_html(__('Please update each time if you change the logo or cover image on the codoc side.','codoc')) . '</p>');
661 },
662 'codoc', //page
663 'setting_section_id' //Section
664 );
665
666 register_setting(
667 'codoc_option_group', // option group
668 CODOC_USERCODE_OPTION_NAME
669 );
670 register_setting(
671 'codoc_option_group', // option group
672 CODOC_TOKEN_OPTION_NAME
673 );
674
675 if (isset($_GET['codoc_auth_finished']) and $_GET['codoc_auth_finished']) {
676 add_settings_error( 'general', 'settings_updated', esc_html(__( 'codoc authentication has been completed.' ,'codoc'), 'success' ));
677 }
678
679 add_settings_field(
680 'codoc_auth',
681 __('Authentication','codoc'),
682 function() {
683 global $CODOC_USERCODE;
684 global $CODOC_TOKEN;
685 global $CODOC_AUTHINFO;
686 $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';";
687
688 // Translators: %s is the email address.
689 echo sprintf('<p><font color="green" id="codoc-auth-message">' . esc_html(__('Authorized as %s','codoc')) . '</font></p>',esc_attr($CODOC_AUTHINFO['email']));
690
691 echo sprintf('<input id="codoc-usercode" type="hidden" name="%s" value="%s">',esc_attr(CODOC_USERCODE_OPTION_NAME),esc_attr($CODOC_USERCODE));
692 echo sprintf('<input id="codoc-token" type="hidden" name="%s" value="%s">',esc_attr(CODOC_TOKEN_OPTION_NAME),esc_attr($CODOC_TOKEN));
693 echo sprintf('<p><a href="javascript:void(0);" onClick="' . esc_attr($script) . '">' . esc_html(__('Unbind authorization','codoc')) . '</p>');
694 },
695 'codoc',
696 'setting_section_id'
697 );
698
699 }
700
701 // ない場合
702 if (!$CODOC_AUTHINFO and !isset($_GET['auth_by_myself'])) {
703 add_settings_field(
704 'codoc_auth',
705 __('Authentication','codoc'),
706 function() {
707 $theme = wp_get_theme();
708 $from = 'wp';
709 if ($theme->get('Name') === 'codoc') {
710 $from = 'wp_codoc';
711 }
712 //$current_url = (empty($_SERVER['HTTPS']) ? 'http://' : 'https://').$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
713 $current_url = admin_url('options-general.php') . '?page=codoc';
714 $direct_url = sprintf("location.href='%s&auth_by_myself=1'",$current_url);
715 $login_url = sprintf("location.href='%s'",$this->get_codoc_url() . '/me/token?from=' . $from . '&return_url=' . urlencode($current_url));
716 $register_url = sprintf("location.href='%s'",$this->get_codoc_url() . '/register?from=' . $from . '&return_url=' . urlencode($current_url));
717 // submitを消しておく
718 echo ('<script type="text/javascript">window.onload=function(){document.getElementById(\'submit\').style.display = \'none\'}</script>');
719 if (isset($_GET['confirm_token'])) {
720 $confirm_url = sprintf("location.href='%s&_wpnonce=%s&fetch_token_key=%s&usercode=%s'",$current_url,
721 wp_create_nonce('fetch_token_nonce'),
722 esc_attr($_GET['fetch_token_key']),
723 esc_attr($_GET['usercode']));
724 echo (esc_html(__('The authentication information has been obtained. Please click the confirmation button to complete the setup.','codoc')) . '<br />');
725 echo sprintf('<input type="button" class="button button-primary" value="%s" onClick="%s"> ',esc_html(__('Confirm authentication','codoc')),esc_attr($confirm_url));
726 } else {
727
728 echo (esc_html(__('Authentication is required to use codoc on WordPress.','codoc')) . '<br />');
729 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 />');
730 echo sprintf('<input type="button" class="button button-primary" value="%s" onClick="%s"> ',esc_attr(__('Authenticate by direct input','codoc')),esc_attr($direct_url));
731 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));
732 }
733 if (!$this->util->health_check()) {
734 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>');
735 }
736
737 },
738 'codoc',
739 'setting_section_id'
740 );
741 }
742 // ない場合かつ自分で認証する場合
743 if (!$CODOC_AUTHINFO and (
744 (isset($_GET['auth_by_myself']) and $_GET['auth_by_myself']) or
745 (isset($_POST['auth_by_myself']) and $_POST['auth_by_myself'])
746 )) {
747 register_setting(
748 'codoc_option_group', // option group
749 CODOC_USERCODE_OPTION_NAME
750 );
751 register_setting(
752 'codoc_option_group', // option group
753 CODOC_TOKEN_OPTION_NAME
754 );
755 add_settings_field(
756 'codoc_usercode',
757 'ユーザーコード',
758 function() {
759 global $CODOC_USERCODE;
760 echo '<input type="hidden" name="auth_by_myself" value="1">';
761 echo sprintf('<input type="text" name="%s" value="%s">',esc_attr(CODOC_USERCODE_OPTION_NAME),esc_attr($CODOC_USERCODE));
762 },
763 'codoc',
764 'setting_section_id'
765 );
766 add_settings_field(
767 'codoc_token',
768 'APIトークン',
769 function() {
770 global $CODOC_TOKEN;
771 echo sprintf('<input type="text" name="%s" value="%s">',esc_attr(CODOC_TOKEN_OPTION_NAME),esc_attr($CODOC_TOKEN));
772 },
773 'codoc',
774 'setting_section_id'
775 );
776 }
777
778 // 認証�
779 報を保存
780 add_action( 'update_option_' . CODOC_USERCODE_OPTION_NAME, function( $old_value, $new_value ) {
781 global $CODOC_RE_AUTHORIZE;
782 $CODOC_RE_AUTHORIZE = 1;
783 },9,2); // $hook, $function_to_add, $priority, $accepted_args
784 add_action( 'update_option_' . CODOC_TOKEN_OPTION_NAME, function( $old_value, $new_value ) {
785 global $CODOC_RE_AUTHORIZE;
786 $CODOC_RE_AUTHORIZE = 1;
787 },10,2);
788
789 add_action('update_option_' . CODOC_SETTINGS_OPTION_NAME,function(){
790 $CODOC_SETTINGS = get_option(CODOC_SETTINGS_OPTION_NAME);
791 if (isset($CODOC_SETTINGS['always_show_support']) and $CODOC_SETTINGS['always_show_support']) {
792 $data = $this->util->get_support_entry([],[
793 "usercode" => get_option(CODOC_USERCODE_OPTION_NAME),
794 "token" => get_option(CODOC_TOKEN_OPTION_NAME),
795 ]);
796 if ($data and $data->status and $entry = $data->entry) {
797 update_option(CODOC_SUPPORT_ENTRYCODE_OPTION_NAME,$entry->code);
798 } else {
799 update_option(CODOC_SUPPORT_ENTRYCODE_OPTION_NAME,'');
800 }
801 } elseif(isset($CODOC_SETTINGS['always_show_support']) and !$CODOC_SETTINGS['always_show_support']) {
802 update_option(CODOC_SUPPORT_ENTRYCODE_OPTION_NAME,'');
803 }
804 });
805
806 add_action('updated_option',function(){
807 global $CODOC_RE_AUTHORIZE;
808 if ($CODOC_RE_AUTHORIZE) {
809 //$data = $this->callAPI('GET','');
810 $data = $this->util->get_user_info([],[
811 "usercode" => get_option(CODOC_USERCODE_OPTION_NAME),
812 "token" => get_option(CODOC_TOKEN_OPTION_NAME),
813 ]);
814 if ($data->status and $user = $data->user) {
815 $this->update_codoc_authinfo($user);
816 } else {
817 update_option(CODOC_AUTHINFO_OPTION_NAME,'');
818 }
819 }
820 });
821
822 });
823 // プラグイン一覧に設定のリンクをいれる
824 add_filter( 'plugin_action_links_' . plugin_basename( plugin_dir_path( __FILE__ ) . 'codoc' . '.php' ),
825 function( $links ) {
826 $setting_link = sprintf( '<a href="%s">%s</a>', esc_url( add_query_arg( 'page', 'codoc', admin_url( 'options-general.php' ) ) ), esc_html( __('Settings') ) );
827 array_unshift( $links, $setting_link );
828
829 return $links;
830 }
831 );
832
833 }
834 public function add_mce() {
835 add_action( 'admin_enqueue_scripts', function() {
836 if ( ! current_user_can( 'edit_posts' ) && ! current_user_can( 'edit_pages' ) ) {
837 return;
838 }
839 $current_screen = get_current_screen();
840 if ( ( method_exists( $current_screen, 'is_block_editor' ) && $current_screen->is_block_editor() ) || ( function_exists( 'is_gutenberg_page' ) && is_gutenberg_page() ) ) {
841 // Gutenberg Editor
842 // なにもしない
843 } else {
844 // tinymce
845 if ( get_user_option( 'rich_editing' ) == 'true' ) {
846 // プラグイン�
847 で使うCSRF を生成
848 $path = plugins_url( 'codoc/src_mce/codoc-editor-onload.js', __DIR__ );
849 wp_enqueue_script( 'codoc-editor-onload', $path, array('jquery'), '', true );
850 // I just want to insert this global variables but i don't know how i can do it..
851 $auth_info = get_option(CODOC_AUTHINFO_OPTION_NAME);
852 $codoc_settings = get_option(CODOC_SETTINGS_OPTION_NAME);
853 $block_defaults_raw = isset($codoc_settings['block_defaults']) ? $codoc_settings['block_defaults'] : '';
854 $block_defaults_decoded = json_decode($block_defaults_raw, true);
855 $block_defaults = is_array($block_defaults_decoded) ? $block_defaults_decoded : new stdClass();
856 wp_localize_script(
857 'codoc-editor-onload',
858 'CODOCEDITOR',
859 array(
860 'action' => 'codoc_shortcodes',
861 'nonce' => wp_create_nonce( 'codoc_shortcodes' ),
862
863 'codoc_url' => $this->get_codoc_url(),
864 'codoc_usercode' => get_option(CODOC_USERCODE_OPTION_NAME),
865 'codoc_plugin_version' => CODOC_PLUGIN_VERSION,
866 'codoc_sdk_path' => CODOC_SDK_PATH,
867 'codoc_account_is_pro' => isset($auth_info['account_is_pro']) ? $auth_info['account_is_pro'] : 0,
868 'codoc_currency_code' => isset($auth_info['currency_code']) ? $auth_info['currency_code'] : 0,
869 'codoc_currency_decimal_places' => isset($auth_info['currency_decimal_places']) ? $auth_info['currency_decimal_places'] : 0,
870 'codoc_block_defaults' => $block_defaults,
871 )
872 );
873
874 // ここでtinymceのプラグイン追加
875 //wp_enqueue_style( 'codoc-admin-style', plugins_url( 'codoc/src_mce/codoc-admin.css', __DIR__ ) );
876 //add_editor_style( plugins_url( 'codoc/src_mce/codoc-admin.css', __DIR__ ) );
877 wp_enqueue_style( 'codoc-admin-style', $this->get_codoc_url() . CODOC_SDK_PATH . '.tinymce.css?c=' . date('Ymd') );
878 add_editor_style( $this->get_codoc_url() . CODOC_SDK_PATH . '.tinymce.css' );
879
880 add_filter( 'mce_external_plugins', function( $plugin_array ) {
881 //$plugin_array['codoc'] = plugins_url( 'codoc/src_mce/codoc-editor.js', __DIR__ );
882 $plugin_array['codoc'] = $this->get_codoc_url() . CODOC_SDK_PATH . '.tinymce.js?c=' . date('Ymd');
883 return $plugin_array;
884 } );
885 add_filter( 'mce_buttons', function( $buttons ) {
886 array_push( $buttons, "|", "codoc" );
887 return $buttons;
888 } );
889 // 非SSL環境の場合、なぜかhttps -> httpsになってしまうので対策
890 // コメントタグが除去されることがあるらしいのでvalid_elementsに�
891 要なタグを追加 (EXPERIMENTAL)
892 add_filter( 'tiny_mce_before_init', function($settings) {
893 $settings['external_plugins'] = preg_replace('/"codoc":"http:/','"codoc":"https:',$settings['external_plugins']);
894 foreach (['valid_elements','extended_valid_elements'] as $valid_elements) {
895 if (isset($settings[$valid_elements])) {
896 $settings[$valid_elements] = $settings[$valid_elements] . ',div[*],p[*],img[*],--[*]';
897 }
898 }
899 $CODOC_SETTINGS = get_option(CODOC_SETTINGS_OPTION_NAME);
900 if (isset($CODOC_SETTINGS["debug_params"])) {
901 $params_decoded = json_decode($CODOC_SETTINGS["debug_params"],true);
902 if (isset($params_decoded["mce_valid_elements"])) {
903 $settings['valid_elements'] = $params_decoded["mce_valid_elements"];
904 $settings['extended_valid_elements'] = $params_decoded["mce_valid_elements"];
905 }
906 }
907
908 return $settings;
909 },1000000);
910 }
911 }
912 } );
913 }
914 function update_codoc_authinfo($user) {
915 global $CODOC_SETTINGS;
916 global $CODOC_AUTHINFO;
917 if (property_exists($user,'connect_code') and $user->connect_code) {
918 $CODOC_SETTINGS = get_option(CODOC_SETTINGS_OPTION_NAME);
919 $CODOC_SETTINGS['codoc_connect_code'] = $user->connect_code;
920 $CODOC_SETTINGS['codoc_connect_registration_mode'] = $user->connect_has_permission_dedicated_account ? 'dedicated' : '';
921 update_option(CODOC_SETTINGS_OPTION_NAME,$CODOC_SETTINGS);
922 }
923 return update_option(CODOC_AUTHINFO_OPTION_NAME,[
924 'email' => $user->email,
925 'name' => $user->name,
926 'profile_image_url' => $user->profile_image_url,
927 'cover_image_url' => $user->cover_image_url,
928 'connect_code' => property_exists($user,'connect_code') ? $user->connect_code : '',
929 'connect_image_url' => property_exists($user,'connect_image_url') ? $user->connect_image_url : '',
930 'account_is_pro' => property_exists($user,'account_is_pro') ? $user->account_is_pro : '',
931 'created_at' => property_exists($user,'created_at') ? $user->created_at : 0,
932 'country' => property_exists($user,'country') ? $user->country : '',
933 'currency_code' => property_exists($user,'currency_code') ? $user->currency_code : '',
934 'currency_decimal_places' => property_exists($user,'currency_decimal_places') ? $user->currency_decimal_places : '',
935 ]);
936 $CODOC_AUTHINFO = get_option(CODOC_AUTHINFO_OPTION_NAME);
937 return $CODOC_AUTHINFO;
938 }
939 // 保存用コンテンツにフィルターを実施する
940 public function get_filtered_content($post_emulated) {
941 // 記事ページであることをエミュレートしてフィルター実行
942 $post_backuped = null;
943 if (isset($GLOBALS['post'])) {
944 $post_backuped = $GLOBALS['post'];
945 }
946 $GLOBALS['post'] = $post_emulated;
947
948 $wp_query_backuped = null;
949 if (isset($GLOBALS['wp_query'])) {
950 $wp_query_backuped = $GLOBALS['wp_query'];
951 $wp_query = $GLOBALS['wp_query'];
952 $wp_query->is_single = true;
953 $wp_query->is_singular = true;
954 # in_the_loop は記事ページでは通常有効っぽいので true指定 ex: wp_ulike
955 $wp_query->in_the_loop = true;
956 # これも追加しておく
957 $wp_query->is_main_query = true;
958
959 $wp_query->post = $post_emulated;
960 $wp_query->queried_object = $post_emulated;
961 $wp_query->queried_object_id = $post_emulated->ID;
962
963 $GLOBALS['wp_query'] = $wp_query;
964 }
965 // $this->the_content でオリジナルを返してもらうため
966 $this->do_not_filter_the_content = true;
967 $content = preg_replace(
968 '/ (\/)?wp:codoc\/codoc-block /',' \\1wptmp:codoc/codoc-block ',
969 $post_emulated->post_content
970 );
971 // post_metaに�
972 容を保存し、無料パートにショートコードの退避をおこなう
973 $codoc_settings = get_option(CODOC_SETTINGS_OPTION_NAME);
974 if (isset($codoc_settings['shortcode_evacuation']) and $codoc_settings['shortcode_evacuation']) {
975 $splited = preg_split('/\/wptmp:codoc\/codoc-block/s',$content);
976 if (isset($splited[0]) and isset($splited[1])) {
977 // すべてのショートコードを $match_all にいれる
978 preg_match_all('/\[[^\[\]]+?\](.+\[\/[^\[\]]+?\])?/',$splited[1],$match_all);
979 // ショートコードをURLエンコードしてdivタグの中にいれておく(後でmetaの中のショートコードと付け合せ)
980 $replaced = preg_replace_callback('/\[[^\[\]]+?\](.+\[\/[^\[\]]+?\])?/',function($matches) {
981 return sprintf ('<div class="codoc-evacuation-dests" data-shortcode="%s"></div>', urlencode($matches[0]));
982 },$splited[1]);
983 // 0番目に�
984 �列で�
985 �っているのでそこを退避対象の�
986 �列とする
987 $evacuations = $match_all[0];
988 // ショートコードの中身をタグに退避
989 $content = $splited[0] . '/wptmp:codoc/codoc-block' . $replaced;
990 // 無料パートでショートコードを実行できるようにする
991 update_post_meta($post_emulated->ID,'codoc_shortcode_evacuations',join('**codoc**',$evacuations));
992 } else {
993 update_post_meta($post_emulated->id,'codoc_shortcode_evacuations',"");
994 }
995 }
996 #$content = do_shortcode( $content );
997 $content_filtered = apply_filters( 'the_content', $content);
998 $this->do_not_filter_the_content = false;
999
1000 $GLOBALS['post'] = $post_backuped;
1001
1002 if ($wp_query_backuped) {
1003 $GLOBALS['wp_query'] = $wp_query_backuped;
1004 }
1005
1006 $content_filtered = preg_replace(
1007 '/ (\/)?wptmp:codoc\/codoc-block /',' \\1wp:codoc/codoc-block ',
1008 $content_filtered
1009 );
1010 return $content_filtered;
1011 }
1012
1013 public function save_post($post_ID,$post,$update) {
1014 if (preg_match('/^(auto-draft|inherit)$/',$post->post_status)) {
1015 return;
1016 }
1017 $entryCode = get_post_meta($post_ID,'codoc_entry_code',true);
1018 $postId = get_post_meta($post_ID,'codoc_saved_post_id',true);
1019 // 保存されているIDと違う場合は破棄(duplicate pageなどでmeta�
1020 報をコピーされた可能性がある)
1021 if ($postId and $postId != $post_ID) {
1022 $entryCode = '';
1023 }
1024 $codoc_settings = get_option(CODOC_SETTINGS_OPTION_NAME);
1025 $post_content = (isset($codoc_settings['do_not_filter_the_content']) and $codoc_settings['do_not_filter_the_content']) ?
1026 $post->post_content : $this->get_filtered_content($post);
1027
1028 // codoc タグ(span/div または Gutenberg ブロック)が含まれていれば API側と統一した文字数検証を行う
1029 $has_codoc_tag = preg_match('/<(?:span|div)[^>]+data-id="codoc-tag"/', $post_content)
1030 || preg_match('/wp:codoc\/codoc-block/', $post_content);
1031 if ($has_codoc_tag) {
1032 // codoc タグ位置で free / paywalled を分割
1033 $gutenberg_split = '/(?:<(?:div|p)(?:[^>]+|)>|)<\!-- +wp:codoc\/codoc-block .*<\!-- +\/wp:codoc\/codoc-block +-->(?:<\/(?:div|p)>|)/s';
1034 $tag_split = '/<(?:span|div)[^>]+data-id="codoc-tag"(?:[^>]+|)>(?:.+|)<\/(?:span|div)>/';
1035 $end_tag_regex = '/<(?:div|p)>::CODOC_WP_END_PAYWALL::<\/(?:div|p)>/';
1036 $for_split = preg_split($end_tag_regex, $post_content);
1037 $for_split = $for_split[0];
1038 if (preg_match('/wp:codoc\/codoc-block/', $for_split)) {
1039 $splited = preg_split($gutenberg_split, $for_split);
1040 } else {
1041 $splited = preg_split($tag_split, $for_split);
1042 }
1043 $body_free = isset($splited[0]) ? $splited[0] : '';
1044 $body_paywalled = isset($splited[1]) ? $splited[1] : '';
1045 $errors = $this->util->validate_entry_lengths([
1046 'title' => $post->post_title,
1047 'body_free' => $body_free,
1048 'body_paywalled' => $body_paywalled,
1049 'binded_url' => get_permalink($post_ID),
1050 ]);
1051 if ($errors) {
1052 set_transient('codoc_entry_validation_errors', $errors, MINUTE_IN_SECONDS * 5);
1053 // 同期はスキップ
1054 return true;
1055 }
1056 }
1057
1058 $res = $this->util->sync_entry([
1059 "post_title" => $post->post_title,
1060 "post_content" => $post_content,
1061 // password が設定されてる場合は限定�
1062 �開にする
1063 "post_status" => $post->post_status == 'publish' ? ($post->post_password ? 2 : 1) : 0,
1064 "post_permalink" => get_permalink($post_ID),
1065 "codoc_entry_code" => $entryCode,
1066 "codoc_settings" => $codoc_settings,
1067 ],[
1068 "usercode" => get_option(CODOC_USERCODE_OPTION_NAME),
1069 "token" => get_option(CODOC_TOKEN_OPTION_NAME),
1070 ]);
1071 $prudent_update_post_meta_entry_code = null;
1072 if (isset($codoc_settings["debug_params"])) {
1073 $params_decoded = json_decode($codoc_settings["debug_params"],true);
1074 if (isset($params_decoded["prudent_update_post_meta_entry_code"])) {
1075 $prudent_update_post_meta_entry_code = $params_decoded["prudent_update_post_meta_entry_code"];
1076 }
1077 }
1078 if ($prudent_update_post_meta_entry_code == 2) {
1079 error_log('CODOC:: ' . sprintf ("%s : %s : %s : %s", $post_ID, (is_object($res) ? "1" : 0),$res->status,$entryCode));
1080 }
1081 if (is_object($res) and $res->status and !$entryCode) {
1082 // 20230222 https://stackoverflow.com/questions/26640785/update-post-meta-not-work-only-when-save-data-not-for-update
1083 if ($prudent_update_post_meta_entry_code) {
1084 add_post_meta($post_ID,'codoc_entry_code',$res->entry->code);
1085 add_post_meta($post_ID,'codoc_saved_post_id',$post_ID);
1086 } else {
1087 update_post_meta($post_ID,'codoc_entry_code',$res->entry->code);
1088 update_post_meta($post_ID,'codoc_saved_post_id',$post_ID);
1089 }
1090 }
1091 return true;
1092 }
1093 function updated_post_meta( $meta_ID, $post_ID, $meta_key ) {
1094 // スルーされてる場合は他のメタ�
1095 報更新のタイミングにサムネイルをアップロード
1096 $has_to_resend = get_post_meta($post_ID,'codoc_post_thumbnail_invoking_entry_code',true);
1097 if ($has_to_resend != 1 and $meta_key != '_thumbnail_id') {
1098 return;
1099 }
1100 if ( has_post_thumbnail($post_ID) ) {
1101 // 新規投稿の場合、タイミングによってはcodocEntryCodeが取得できないので一旦スルーする
1102 if (!get_post_meta($post_ID,'codoc_entry_code',true)) {
1103 update_post_meta($post_ID,'codoc_post_thumbnail_invoking_entry_code',1);
1104 return;
1105 } else {
1106 update_post_meta($post_ID,'codoc_post_thumbnail_invoking_entry_code',0);
1107 }
1108 $attachment = wp_get_attachment_metadata( get_post_thumbnail_id($post_ID));
1109 $upload_dir = wp_upload_dir();
1110 $file_path = sprintf ('%s/%s',$upload_dir['basedir'],$attachment['file']);
1111
1112 return $this->util->post_thumbnail([
1113 "file_path" => $file_path,
1114 "boundary" => wp_generate_password(24),
1115 "codoc_entry_code" => get_post_meta($post_ID,'codoc_entry_code',true),
1116 ],[
1117 "usercode" => get_option(CODOC_USERCODE_OPTION_NAME),
1118 "token" => get_option(CODOC_TOKEN_OPTION_NAME),
1119 ]);
1120 }
1121 }
1122 function deleted_post_meta( $meta_ids, $post_ID, $meta_key,$meta_value ) {
1123 if ($meta_key != '_thumbnail_id') {
1124 return;
1125 }
1126 return $this->util->reset_thumbnail(
1127 ["codoc_entry_code" => get_post_meta($post_ID,'codoc_entry_code',true)],
1128 [
1129 "usercode" => get_option(CODOC_USERCODE_OPTION_NAME),
1130 "token" => get_option(CODOC_TOKEN_OPTION_NAME),
1131 ]
1132 );
1133 }
1134 // 退避されてるかどうかをmetaを使って確認し、無料エリアの一番最後にショートコードを追加
1135 function the_content_shortcode_evacuations( $post_content ) {
1136 if ($this->do_not_filter_the_content) {
1137 return $post_content;
1138 }
1139 $post = get_post();
1140 if (!$post) {
1141 return $post_content;
1142 }
1143 $evacuations_meta = get_post_meta($post->ID,'codoc_shortcode_evacuations',true);
1144 $evacuations = preg_split('/\*\*codoc\*\*/',$evacuations_meta);
1145 foreach ($evacuations as $evacuation) {
1146 $post_content = sprintf('<div class="codoc-evacuations" style="display:none;" data-shortcode="%s">%s</div>',
1147 urlencode($evacuation),$evacuation) . $post_content;
1148 }
1149 return $post_content;
1150 }
1151 function the_content( $post_content ) {
1152 if ( is_single() && in_the_loop() && is_main_query() ) {
1153 }
1154 // get_filtered_content から do_not_filter_the_contentを有効にされるパターン
1155 // オプションの設定値の意味合い(他のフィルタを無視)とは違うため注意
1156 if ($this->do_not_filter_the_content) {
1157 return $post_content;
1158 }
1159 $post = get_post();
1160 // 20211215 null になる場合がある
1161 if (!$post) {
1162 return $post_content;
1163 }
1164 # is_amp で実�
1165 しているテンプレート用
1166 $is_amp_endpoint = (function_exists('is_amp_endpoint') && is_amp_endpoint()) ? true :
1167 ((function_exists('is_amp') && is_amp()) ? true : false);
1168 return $this->util->filter_content([
1169 "post_content" => $post_content,
1170 "preview" => is_preview(),
1171 "codoc_entry_code" => get_post_meta($post->ID,'codoc_entry_code',true),
1172 "codoc_settings" => get_option(CODOC_SETTINGS_OPTION_NAME),
1173 "is_amp_endpoint" => $is_amp_endpoint,
1174 "post_permalink" => get_permalink($post->ID),
1175 "codoc_support_entry_code" => get_option(CODOC_SUPPORT_ENTRYCODE_OPTION_NAME),
1176 ]);
1177 }
1178
1179 function excerpt_allowed_blocks ($allowed_blocks) {
1180 if (is_array($allowed_blocks)) {
1181 array_push($allowed_blocks,'codoc/codoc-block');
1182 }
1183 return $allowed_blocks;
1184 }
1185
1186 function allowed_block_types_all ($allowed_block_types, $block_editor_context) {
1187 // テーマ等がブロックを制限していない場合(true = �
1188 �ブロック許可)はそのまま返す
1189 if ($allowed_block_types === true) {
1190 return $allowed_block_types;
1191 }
1192 // �
1193 �列で制限されている場合、codoc ブロックを追加
1194 if (is_array($allowed_block_types) && !in_array('codoc/codoc-block', $allowed_block_types)) {
1195 $allowed_block_types[] = 'codoc/codoc-block';
1196 }
1197 return $allowed_block_types;
1198 }
1199
1200 function show_validation_notices() {
1201 $errors = get_transient('codoc_entry_validation_errors');
1202 if (!$errors || !is_array($errors)) {
1203 return;
1204 }
1205 delete_transient('codoc_entry_validation_errors');
1206 $header = __('codoc could not sync this entry due to length limits:', 'codoc');
1207 echo '<div class="notice notice-error is-dismissible"><p><strong>' . esc_html($header) . '</strong></p><ul>';
1208 foreach ($errors as $message) {
1209 echo '<li>' . esc_html($message) . '</li>';
1210 }
1211 echo '</ul></div>';
1212 }
1213
1214 function show_tadv_notice() {
1215 // API からの notification を表示
1216 $api_notifications = get_transient('codoc_api_notification');
1217 if ($api_notifications && is_array($api_notifications)) {
1218 // notification �
1219 �列のキーと値を表示
1220 foreach ($api_notifications as $key => $value) {
1221 // 値が�
1222 �列やオブジェクトの場合は json_encode で文字列化
1223 if (is_array($value) || is_object($value)) {
1224 $value = json_encode($value, JSON_UNESCAPED_UNICODE);
1225 }
1226 echo "<div class=\"notice notice-warning is-dismissible codoc-notification\"><p>" . esc_html($value) . "</p></div>";
1227 }
1228 //delete_transient('codoc_api_notification');
1229 }
1230
1231 $name = 'Advanced Editor Tools';
1232 $name_escaped = preg_replace('/ /','+',$name);
1233 $install_url = network_admin_url( "plugin-install.php?tab=search&s=" . $name );
1234 $options_url = network_admin_url( "options-general.php?page=tinymce-advanced" );
1235
1236 //<a href=\"%s\">%s</a> を有効にし、設定画面にて &quot;Keep paragraph tags in the Classic block and the Classic Editor&quot; を有効にしてください。
1237 //<a href=\"%s\">%s</a> の設定画面にて &quot;Keep paragraph tags in the Classic block and the Classic Editor&quot; を有効にしてください。
1238 // Translators: %s is a link to the installation page with the plugin name.
1239 $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));
1240 // Translators: %s is a link to the options settings page with the plugin name.
1241 $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));
1242 // クラシックエディタで advanced editor tools を�
1243 �れてない場合は notice
1244 if (is_plugin_active('classic-editor/classic-editor.php') and
1245 !is_plugin_active('tinymce-advanced/tinymce-advanced.php')) {
1246 echo "<div class=\"notice notice-warning is-dismissible\"><p>" . esc_html($tadv_message_for_install) . "</p></div>";
1247 }
1248
1249 // advanced editor tools の設定を調査
1250 $no_autop = false;
1251 $tadv_admin_settings = get_option( 'tadv_admin_settings', false );
1252 if (isset($tadv_admin_settings['options']) and preg_match('/no_autop/',$tadv_admin_settings['options'])) {
1253 $no_autop = true;
1254 }
1255 // advanced editor tools が有効で no_autop (Keep paragraph tags in the Classic block and the Classic Editor) が無効
1256 if (is_plugin_active('tinymce-advanced/tinymce-advanced.php') and !$no_autop) {
1257 echo "<div class=\"notice notice-warning is-dismissible\"><p>" . esc_html($tadv_message_for_options) . "</p></div>";
1258 }
1259 }
1260 }
1261