PluginProbe
codoc / 0.9.51.7
codoc v0.9.51.7
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.51.7, at class-codoc.php

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