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

1,132 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 の設定', //ページタイトル
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' ? '<br/> <strong>' . __('Set the audience as a private account.','codoc') . '</strong>' : ''));
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(__('認証�
675 報を取得しました。設定を完了させるために確認ボタンを押下してください。','codoc')) . '<br />');
676 echo sprintf('<input type="button" class="button button-primary" value="%s" onClick="%s"> ',esc_html(__('認証を確認','codoc')),esc_attr($confirm_url));
677 } else {
678
679 echo (esc_html(__('Authentication is required to use codoc on WordPress.','codoc')) . '<br />');
680 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 />');
681 echo sprintf('<input type="button" class="button button-primary" value="%s" onClick="%s"> ',esc_attr(__('Authenticate by direct input','codoc')),esc_attr($direct_url));
682 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));
683 }
684 if (!$this->util->health_check()) {
685 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>');
686 }
687
688 },
689 'codoc',
690 'setting_section_id'
691 );
692 }
693 // ない場合かつ自分で認証する場合
694 if (!$CODOC_AUTHINFO and (
695 (isset($_GET['auth_by_myself']) and $_GET['auth_by_myself']) or
696 (isset($_POST['auth_by_myself']) and $_POST['auth_by_myself'])
697 )) {
698 register_setting(
699 'codoc_option_group', // option group
700 CODOC_USERCODE_OPTION_NAME
701 );
702 register_setting(
703 'codoc_option_group', // option group
704 CODOC_TOKEN_OPTION_NAME
705 );
706 add_settings_field(
707 'codoc_usercode',
708 'ユーザーコード',
709 function() {
710 global $CODOC_USERCODE;
711 echo '<input type="hidden" name="auth_by_myself" value="1">';
712 echo sprintf('<input type="text" name="%s" value="%s">',esc_attr(CODOC_USERCODE_OPTION_NAME),esc_attr($CODOC_USERCODE));
713 },
714 'codoc',
715 'setting_section_id'
716 );
717 add_settings_field(
718 'codoc_token',
719 'APIトークン',
720 function() {
721 global $CODOC_TOKEN;
722 echo sprintf('<input type="text" name="%s" value="%s">',esc_attr(CODOC_TOKEN_OPTION_NAME),esc_attr($CODOC_TOKEN));
723 },
724 'codoc',
725 'setting_section_id'
726 );
727 }
728
729 // 認証�
730 報を保存
731 add_action( 'update_option_' . CODOC_USERCODE_OPTION_NAME, function( $old_value, $new_value ) {
732 global $CODOC_RE_AUTHORIZE;
733 $CODOC_RE_AUTHORIZE = 1;
734 },9,2); // $hook, $function_to_add, $priority, $accepted_args
735 add_action( 'update_option_' . CODOC_TOKEN_OPTION_NAME, function( $old_value, $new_value ) {
736 global $CODOC_RE_AUTHORIZE;
737 $CODOC_RE_AUTHORIZE = 1;
738 },10,2);
739
740 add_action('update_option_' . CODOC_SETTINGS_OPTION_NAME,function(){
741 $CODOC_SETTINGS = get_option(CODOC_SETTINGS_OPTION_NAME);
742 if (isset($CODOC_SETTINGS['always_show_support']) and $CODOC_SETTINGS['always_show_support']) {
743 $data = $this->util->get_support_entry([],[
744 "usercode" => get_option(CODOC_USERCODE_OPTION_NAME),
745 "token" => get_option(CODOC_TOKEN_OPTION_NAME),
746 ]);
747 if ($data and $data->status and $entry = $data->entry) {
748 update_option(CODOC_SUPPORT_ENTRYCODE_OPTION_NAME,$entry->code);
749 } else {
750 update_option(CODOC_SUPPORT_ENTRYCODE_OPTION_NAME,'');
751 }
752 } elseif(isset($CODOC_SETTINGS['always_show_support']) and !$CODOC_SETTINGS['always_show_support']) {
753 update_option(CODOC_SUPPORT_ENTRYCODE_OPTION_NAME,'');
754 }
755 });
756
757 add_action('updated_option',function(){
758 global $CODOC_RE_AUTHORIZE;
759 if ($CODOC_RE_AUTHORIZE) {
760 //$data = $this->callAPI('GET','');
761 $data = $this->util->get_user_info([],[
762 "usercode" => get_option(CODOC_USERCODE_OPTION_NAME),
763 "token" => get_option(CODOC_TOKEN_OPTION_NAME),
764 ]);
765 if ($data->status and $user = $data->user) {
766 $this->update_codoc_authinfo($user);
767 } else {
768 update_option(CODOC_AUTHINFO_OPTION_NAME,'');
769 }
770 }
771 });
772
773 });
774 // プラグイン一覧に設定のリンクをいれる
775 add_filter( 'plugin_action_links_' . plugin_basename( plugin_dir_path( __FILE__ ) . 'codoc' . '.php' ),
776 function( $links ) {
777 $setting_link = sprintf( '<a href="%s">%s</a>', esc_url( add_query_arg( 'page', 'codoc', admin_url( 'options-general.php' ) ) ), esc_html( '設定' ) );
778 array_unshift( $links, $setting_link );
779
780 return $links;
781 }
782 );
783
784 }
785 public function add_mce() {
786 add_action( 'admin_enqueue_scripts', function() {
787 if ( ! current_user_can( 'edit_posts' ) && ! current_user_can( 'edit_pages' ) ) {
788 return;
789 }
790 $current_screen = get_current_screen();
791 if ( ( method_exists( $current_screen, 'is_block_editor' ) && $current_screen->is_block_editor() ) || ( function_exists( 'is_gutenberg_page' ) && is_gutenberg_page() ) ) {
792 // Gutenberg Editor
793 // なにもしない
794 } else {
795 // tinymce
796 if ( get_user_option( 'rich_editing' ) == 'true' ) {
797 // プラグイン�
798 で使うCSRF を生成
799 $path = plugins_url( 'codoc/src_mce/codoc-editor-onload.js', __DIR__ );
800 wp_enqueue_script( 'codoc-editor-onload', $path, array('jquery'), '', true );
801 // I just want to insert this global variables but i don't know how i can do it..
802 $auth_info = get_option(CODOC_AUTHINFO_OPTION_NAME);
803 wp_localize_script(
804 'codoc-editor-onload',
805 'CODOCEDITOR',
806 array(
807 'action' => 'codoc_shortcodes',
808 'nonce' => wp_create_nonce( 'codoc_shortcodes' ),
809
810 'codoc_url' => $this->get_codoc_url(),
811 'codoc_usercode' => get_option(CODOC_USERCODE_OPTION_NAME),
812 'codoc_plugin_version' => CODOC_PLUGIN_VERSION,
813 'codoc_sdk_path' => CODOC_SDK_PATH,
814 'codoc_account_is_pro' => isset($auth_info['account_is_pro']) ? $auth_info['account_is_pro'] : 0,
815 'codoc_currency_code' => isset($auth_info['currency_code']) ? $auth_info['currency_code'] : 0,
816 'codoc_currency_decimal_places' => isset($auth_info['currency_decimal_places']) ? $auth_info['currency_decimal_places'] : 0,
817 )
818 );
819
820 // ここでtinymceのプラグイン追加
821 //wp_enqueue_style( 'codoc-admin-style', plugins_url( 'codoc/src_mce/codoc-admin.css', __DIR__ ) );
822 //add_editor_style( plugins_url( 'codoc/src_mce/codoc-admin.css', __DIR__ ) );
823 wp_enqueue_style( 'codoc-admin-style', $this->get_codoc_url() . CODOC_SDK_PATH . '.tinymce.css?c=' . date('Ymd') );
824 add_editor_style( $this->get_codoc_url() . CODOC_SDK_PATH . '.tinymce.css' );
825
826 add_filter( 'mce_external_plugins', function( $plugin_array ) {
827 //$plugin_array['codoc'] = plugins_url( 'codoc/src_mce/codoc-editor.js', __DIR__ );
828 $plugin_array['codoc'] = $this->get_codoc_url() . CODOC_SDK_PATH . '.tinymce.js?c=' . date('Ymd');
829 return $plugin_array;
830 } );
831 add_filter( 'mce_buttons', function( $buttons ) {
832 array_push( $buttons, "|", "codoc" );
833 return $buttons;
834 } );
835 // 非SSL環境の場合、なぜかhttps -> httpsになってしまうので対策
836 // コメントタグが除去されることがあるらしいのでvalid_elementsに�
837 要なタグを追加 (EXPERIMENTAL)
838 add_filter( 'tiny_mce_before_init', function($settings) {
839 $settings['external_plugins'] = preg_replace('/"codoc":"http:/','"codoc":"https:',$settings['external_plugins']);
840 foreach (['valid_elements','extended_valid_elements'] as $valid_elements) {
841 if (isset($settings[$valid_elements])) {
842 $settings[$valid_elements] = $settings[$valid_elements] . ',div[*],p[*],img[*],--[*]';
843 }
844 }
845 $CODOC_SETTINGS = get_option(CODOC_SETTINGS_OPTION_NAME);
846 if (isset($CODOC_SETTINGS["debug_params"])) {
847 $params_decoded = json_decode($CODOC_SETTINGS["debug_params"],true);
848 if (isset($params_decoded["mce_valid_elements"])) {
849 $settings['valid_elements'] = $params_decoded["mce_valid_elements"];
850 $settings['extended_valid_elements'] = $params_decoded["mce_valid_elements"];
851 }
852 }
853
854 return $settings;
855 },1000000);
856 }
857 }
858 } );
859 }
860 function update_codoc_authinfo($user) {
861 global $CODOC_SETTINGS;
862 global $CODOC_AUTHINFO;
863 if (property_exists($user,'connect_code') and $user->connect_code) {
864 $CODOC_SETTINGS = get_option(CODOC_SETTINGS_OPTION_NAME);
865 $CODOC_SETTINGS['codoc_connect_code'] = $user->connect_code;
866 $CODOC_SETTINGS['codoc_connect_registration_mode'] = $user->connect_has_permission_dedicated_account ? 'dedicated' : '';
867 update_option(CODOC_SETTINGS_OPTION_NAME,$CODOC_SETTINGS);
868 }
869 return update_option(CODOC_AUTHINFO_OPTION_NAME,[
870 'email' => $user->email,
871 'name' => $user->name,
872 'profile_image_url' => $user->profile_image_url,
873 'cover_image_url' => $user->cover_image_url,
874 'connect_code' => property_exists($user,'connect_code') ? $user->connect_code : '',
875 'connect_image_url' => property_exists($user,'connect_image_url') ? $user->connect_image_url : '',
876 'account_is_pro' => property_exists($user,'account_is_pro') ? $user->account_is_pro : '',
877 'created_at' => property_exists($user,'created_at') ? $user->created_at : 0,
878 'country' => property_exists($user,'country') ? $user->country : '',
879 'currency_code' => property_exists($user,'currency_code') ? $user->currency_code : '',
880 'currency_decimal_places' => property_exists($user,'currency_decimal_places') ? $user->currency_decimal_places : '',
881 ]);
882 $CODOC_AUTHINFO = get_option(CODOC_AUTHINFO_OPTION_NAME);
883 return $CODOC_AUTHINFO;
884 }
885 // 保存用コンテンツにフィルターを実施する
886 public function get_filtered_content($post_emulated) {
887 // 記事ページであることをエミュレートしてフィルター実行
888 $post_backuped = null;
889 if (isset($GLOBALS['post'])) {
890 $post_backuped = $GLOBALS['post'];
891 }
892 $GLOBALS['post'] = $post_emulated;
893
894 $wp_query_backuped = null;
895 if (isset($GLOBALS['wp_query'])) {
896 $wp_query_backuped = $GLOBALS['wp_query'];
897 $wp_query = $GLOBALS['wp_query'];
898 $wp_query->is_single = true;
899 $wp_query->is_singular = true;
900 # in_the_loop は記事ページでは通常有効っぽいので true指定 ex: wp_ulike
901 $wp_query->in_the_loop = true;
902 # これも追加しておく
903 $wp_query->is_main_query = true;
904
905 $wp_query->post = $post_emulated;
906 $wp_query->queried_object = $post_emulated;
907 $wp_query->queried_object_id = $post_emulated->ID;
908
909 $GLOBALS['wp_query'] = $wp_query;
910 }
911 // $this->the_content でオリジナルを返してもらうため
912 $this->do_not_filter_the_content = true;
913 $content = preg_replace(
914 '/ (\/)?wp:codoc\/codoc-block /',' \\1wptmp:codoc/codoc-block ',
915 $post_emulated->post_content
916 );
917 // post_metaに�
918 容を保存し、無料パートにショートコードの退避をおこなう
919 $codoc_settings = get_option(CODOC_SETTINGS_OPTION_NAME);
920 if (isset($codoc_settings['shortcode_evacuation']) and $codoc_settings['shortcode_evacuation']) {
921 $splited = preg_split('/\/wptmp:codoc\/codoc-block/s',$content);
922 if (isset($splited[0]) and isset($splited[1])) {
923 // すべてのショートコードを $match_all にいれる
924 preg_match_all('/\[[^\[\]]+?\](.+\[\/[^\[\]]+?\])?/',$splited[1],$match_all);
925 // ショートコードをURLエンコードしてdivタグの中にいれておく(後でmetaの中のショートコードと付け合せ)
926 $replaced = preg_replace_callback('/\[[^\[\]]+?\](.+\[\/[^\[\]]+?\])?/',function($matches) {
927 return sprintf ('<div class="codoc-evacuation-dests" data-shortcode="%s"></div>', urlencode($matches[0]));
928 },$splited[1]);
929 // 0番目に�
930 �列で�
931 �っているのでそこを退避対象の�
932 �列とする
933 $evacuations = $match_all[0];
934 // ショートコードの中身をタグに退避
935 $content = $splited[0] . '/wptmp:codoc/codoc-block' . $replaced;
936 // 無料パートでショートコードを実行できるようにする
937 update_post_meta($post_emulated->ID,'codoc_shortcode_evacuations',join('**codoc**',$evacuations));
938 } else {
939 update_post_meta($post_emulated->id,'codoc_shortcode_evacuations',"");
940 }
941 }
942 #$content = do_shortcode( $content );
943 $content_filtered = apply_filters( 'the_content', $content);
944 $this->do_not_filter_the_content = false;
945
946 $GLOBALS['post'] = $post_backuped;
947
948 if ($wp_query_backuped) {
949 $GLOBALS['wp_query'] = $wp_query_backuped;
950 }
951
952 $content_filtered = preg_replace(
953 '/ (\/)?wptmp:codoc\/codoc-block /',' \\1wp:codoc/codoc-block ',
954 $content_filtered
955 );
956 return $content_filtered;
957 }
958
959 public function save_post($post_ID,$post,$update) {
960 if (preg_match('/^(auto-draft|inherit)$/',$post->post_status)) {
961 return;
962 }
963 $entryCode = get_post_meta($post_ID,'codoc_entry_code',true);
964 $postId = get_post_meta($post_ID,'codoc_saved_post_id',true);
965 // 保存されているIDと違う場合は破棄(duplicate pageなどでmeta�
966 報をコピーされた可能性がある)
967 if ($postId and $postId != $post_ID) {
968 $entryCode = '';
969 }
970 $codoc_settings = get_option(CODOC_SETTINGS_OPTION_NAME);
971 $post_content = (isset($codoc_settings['do_not_filter_the_content']) and $codoc_settings['do_not_filter_the_content']) ?
972 $post->post_content : $this->get_filtered_content($post);
973 $res = $this->util->sync_entry([
974 "post_title" => $post->post_title,
975 "post_content" => $post_content,
976 // password が設定されてる場合は限定�
977 �開にする
978 "post_status" => $post->post_status == 'publish' ? ($post->post_password ? 2 : 1) : 0,
979 "post_permalink" => get_permalink($post_ID),
980 "codoc_entry_code" => $entryCode,
981 "codoc_settings" => $codoc_settings,
982 ],[
983 "usercode" => get_option(CODOC_USERCODE_OPTION_NAME),
984 "token" => get_option(CODOC_TOKEN_OPTION_NAME),
985 ]);
986 $prudent_update_post_meta_entry_code = null;
987 if (isset($codoc_settings["debug_params"])) {
988 $params_decoded = json_decode($codoc_settings["debug_params"],true);
989 if (isset($params_decoded["prudent_update_post_meta_entry_code"])) {
990 $prudent_update_post_meta_entry_code = $params_decoded["prudent_update_post_meta_entry_code"];
991 }
992 }
993 if ($prudent_update_post_meta_entry_code == 2) {
994 error_log('CODOC:: ' . sprintf ("%s : %s : %s : %s", $post_ID, (is_object($res) ? "1" : 0),$res->status,$entryCode));
995 }
996 if (is_object($res) and $res->status and !$entryCode) {
997 // 20230222 https://stackoverflow.com/questions/26640785/update-post-meta-not-work-only-when-save-data-not-for-update
998 if ($prudent_update_post_meta_entry_code) {
999 add_post_meta($post_ID,'codoc_entry_code',$res->entry->code);
1000 add_post_meta($post_ID,'codoc_saved_post_id',$post_ID);
1001 } else {
1002 update_post_meta($post_ID,'codoc_entry_code',$res->entry->code);
1003 update_post_meta($post_ID,'codoc_saved_post_id',$post_ID);
1004 }
1005 }
1006 return true;
1007 }
1008 function updated_post_meta( $meta_ID, $post_ID, $meta_key ) {
1009 // スルーされてる場合は他のメタ�
1010 報更新のタイミングにサムネイルをアップロード
1011 $has_to_resend = get_post_meta($post_ID,'codoc_post_thumbnail_invoking_entry_code',true);
1012 if ($has_to_resend != 1 and $meta_key != '_thumbnail_id') {
1013 return;
1014 }
1015 if ( has_post_thumbnail($post_ID) ) {
1016 // 新規投稿の場合、タイミングによってはcodocEntryCodeが取得できないので一旦スルーする
1017 if (!get_post_meta($post_ID,'codoc_entry_code',true)) {
1018 update_post_meta($post_ID,'codoc_post_thumbnail_invoking_entry_code',1);
1019 return;
1020 } else {
1021 update_post_meta($post_ID,'codoc_post_thumbnail_invoking_entry_code',0);
1022 }
1023 $attachment = wp_get_attachment_metadata( get_post_thumbnail_id($post_ID));
1024 $upload_dir = wp_upload_dir();
1025 $file_path = sprintf ('%s/%s',$upload_dir['basedir'],$attachment['file']);
1026
1027 return $this->util->post_thumbnail([
1028 "file_path" => $file_path,
1029 "boundary" => wp_generate_password(24),
1030 "codoc_entry_code" => get_post_meta($post_ID,'codoc_entry_code',true),
1031 ],[
1032 "usercode" => get_option(CODOC_USERCODE_OPTION_NAME),
1033 "token" => get_option(CODOC_TOKEN_OPTION_NAME),
1034 ]);
1035 }
1036 }
1037 function deleted_post_meta( $meta_ids, $post_ID, $meta_key,$meta_value ) {
1038 if ($meta_key != '_thumbnail_id') {
1039 return;
1040 }
1041 return $this->util->reset_thumbnail(
1042 ["codoc_entry_code" => get_post_meta($post_ID,'codoc_entry_code',true)],
1043 [
1044 "usercode" => get_option(CODOC_USERCODE_OPTION_NAME),
1045 "token" => get_option(CODOC_TOKEN_OPTION_NAME),
1046 ]
1047 );
1048 }
1049 // 退避されてるかどうかをmetaを使って確認し、無料エリアの一番最後にショートコードを追加
1050 function the_content_shortcode_evacuations( $post_content ) {
1051 if ($this->do_not_filter_the_content) {
1052 return $post_content;
1053 }
1054 $post = get_post();
1055 if (!$post) {
1056 return $post_content;
1057 }
1058 $evacuations_meta = get_post_meta($post->ID,'codoc_shortcode_evacuations',true);
1059 $evacuations = preg_split('/\*\*codoc\*\*/',$evacuations_meta);
1060 foreach ($evacuations as $evacuation) {
1061 $post_content = sprintf('<div class="codoc-evacuations" style="display:none;" data-shortcode="%s">%s</div>',
1062 urlencode($evacuation),$evacuation) . $post_content;
1063 }
1064 return $post_content;
1065 }
1066 function the_content( $post_content ) {
1067 if ( is_single() && in_the_loop() && is_main_query() ) {
1068 }
1069 // get_filtered_content から do_not_filter_the_contentを有効にされるパターン
1070 // オプションの設定値の意味合い(他のフィルタを無視)とは違うため注意
1071 if ($this->do_not_filter_the_content) {
1072 return $post_content;
1073 }
1074 $post = get_post();
1075 // 20211215 null になる場合がある
1076 if (!$post) {
1077 return $post_content;
1078 }
1079 # is_amp で実�
1080 しているテンプレート用
1081 $is_amp_endpoint = (function_exists('is_amp_endpoint') && is_amp_endpoint()) ? true :
1082 ((function_exists('is_amp') && is_amp()) ? true : false);
1083 return $this->util->filter_content([
1084 "post_content" => $post_content,
1085 "preview" => is_preview(),
1086 "codoc_entry_code" => get_post_meta($post->ID,'codoc_entry_code',true),
1087 "codoc_settings" => get_option(CODOC_SETTINGS_OPTION_NAME),
1088 "is_amp_endpoint" => $is_amp_endpoint,
1089 "post_permalink" => get_permalink($post->ID),
1090 "codoc_support_entry_code" => get_option(CODOC_SUPPORT_ENTRYCODE_OPTION_NAME),
1091 ]);
1092 }
1093
1094 function excerpt_allowed_blocks ($allowed_blocks) {
1095 if (is_array($allowed_blocks)) {
1096 array_push($allowed_blocks,'codoc/codoc-block');
1097 }
1098 return $allowed_blocks;
1099 }
1100
1101 function show_tadv_notice() {
1102 $name = 'Advanced Editor Tools';
1103 $name_escaped = preg_replace('/ /','+',$name);
1104 $install_url = network_admin_url( "plugin-install.php?tab=search&s=" . $name );
1105 $options_url = network_admin_url( "options-general.php?page=tinymce-advanced" );
1106
1107 //<a href=\"%s\">%s</a> を有効にし、設定画面にて &quot;Keep paragraph tags in the Classic block and the Classic Editor&quot; を有効にしてください。
1108 //<a href=\"%s\">%s</a> の設定画面にて &quot;Keep paragraph tags in the Classic block and the Classic Editor&quot; を有効にしてください。
1109 // Translators: %s is a link to the installation page with the plugin name.
1110 $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));
1111 // Translators: %s is a link to the options settings page with the plugin name.
1112 $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));
1113 // クラシックエディタで advanced editor tools を�
1114 �れてない場合は notice
1115 if (is_plugin_active('classic-editor/classic-editor.php') and
1116 !is_plugin_active('tinymce-advanced/tinymce-advanced.php')) {
1117 echo "<div class=\"notice notice-warning is-dismissible\"><p>" . esc_html($tadv_message_for_install) . "</p></div>";
1118 }
1119
1120 // advanced editor tools の設定を調査
1121 $no_autop = false;
1122 $tadv_admin_settings = get_option( 'tadv_admin_settings', false );
1123 if (isset($tadv_admin_settings['options']) and preg_match('/no_autop/',$tadv_admin_settings['options'])) {
1124 $no_autop = true;
1125 }
1126 // advanced editor tools が有効で no_autop (Keep paragraph tags in the Classic block and the Classic Editor) が無効
1127 if (is_plugin_active('tinymce-advanced/tinymce-advanced.php') and !$no_autop) {
1128 echo "<div class=\"notice notice-warning is-dismissible\"><p>" . esc_html($tadv_message_for_options) . "</p></div>";
1129 }
1130 }
1131 }
1132