PluginProbe
codoc / 0.9.54.3
codoc v0.9.54.3
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 0.9.54.3, at class-codoc.php

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