PluginProbe
codoc / 0.9.12.7
codoc v0.9.12.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.12.7, at class-codoc.php

916 lines 51.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
5 public function __construct() {
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 add_filter('the_content',[$this,'the_content'],$priority);
29
30 // 2020-07-25 excerpt対応
31 add_filter('excerpt_allowed_blocks',[$this,'excerpt_allowed_blocks']);
32
33 $auth_info = get_option(CODOC_AUTHINFO_OPTION_NAME);
34
35 if ($auth_info) {
36 // データ同期 (save_postはis_adminで実行されないケースがある)
37 add_action( 'save_post', [$this,'save_post'], 20, 3 );
38 add_action( 'added_post_meta', [$this,'updated_post_meta'], 10, 3 );
39 add_action( 'updated_post_meta',[$this,'updated_post_meta'], 10, 3 );
40 add_action( 'deleted_post_meta',[$this,'deleted_post_meta'], 10, 4 );
41 }
42 if (is_admin() and $auth_info) { //管理画面
43 // Gutenberg のサポートがない場合は何もしない
44 if ( function_exists( 'register_block_type' ) ) {
45 // gutenberg
46 require_once 'src/init.php';
47 add_action('wp_loaded', function() {
48 wp_localize_script('wordpress-cgb-block-js', 'OPTIONS', array(
49 'codoc_url' => $this->get_codoc_url(),
50 'codoc_usercode' => get_option(CODOC_USERCODE_OPTION_NAME),
51 'codoc_plugin_version' => CODOC_PLUGIN_VERSION,
52 'codoc_sdk_path' => CODOC_SDK_PATH,
53 ));
54 });
55 }
56 // tinymce
57 $this->add_mce();
58 } else { // ブログ画面
59 // codocのJS登録
60 add_action( 'wp_enqueue_scripts', function() {
61 wp_enqueue_script( 'codoc-injector-js', $this->get_codoc_url() . '/js/cms.js' );
62 });
63 //登録したscriptタグに属性をつける
64 add_filter('script_loader_tag', [$this,'modifier_script_tag'],10,2);
65 // tinymce用のショートコード
66 add_shortcode('codoc', [$this, 'injector_shortcode']);
67 // テーマをbodyにも反映
68 add_filter('body_class', function($classes) {
69 $CODOC_SETTINGS = get_option(CODOC_SETTINGS_OPTION_NAME);
70 $css_path = 'blue';
71 if (isset($CODOC_SETTINGS["css_path"]) and $CODOC_SETTINGS["css_path"]) {
72 $css_path = $CODOC_SETTINGS["css_path"];
73 }
74 array_push($classes,sprintf( "codoc-theme-%s",$css_path));
75 return $classes;
76 });
77
78 }
79 return $this;
80 }
81 public function get_codoc_url() {
82 $CODOC_SETTINGS = get_option(CODOC_SETTINGS_OPTION_NAME);
83 if (isset($CODOC_SETTINGS["debug_params"])) {
84 $params_decoded = json_decode($CODOC_SETTINGS["debug_params"],true);
85 if (isset($params_decoded["codoc_url"])) {
86 return $params_decoded["codoc_url"];
87 }
88 }
89 return CODOC_URL;
90 }
91 public function modifier_script_tag($tag,$handle) {
92 if($handle !== 'codoc-injector-js') {
93 return $tag;
94 }
95 $CODOC_SETTINGS = get_option(CODOC_SETTINGS_OPTION_NAME);
96 $data_css = '';
97 if ($css_path = $CODOC_SETTINGS['css_path']) {
98 $data_css = sprintf(' data-css="%s" ',$css_path);
99 }
100 // connect用パラメータ
101 $connect_attributes = '';
102 if (isset($CODOC_SETTINGS["codoc_connect_code"])) {
103 $connect_code = $CODOC_SETTINGS["codoc_connect_code"];
104 $connect_attributes = sprintf(' data-connect-code="%s"',$connect_code);
105 $tag = preg_replace('/cms\.js/','cms-connect.js',$tag);
106 }
107 if (isset($CODOC_SETTINGS["codoc_connect_registration_mode"]) and $CODOC_SETTINGS["codoc_connect_registration_mode"]) {
108 $connect_attributes = $connect_attributes .
109 sprintf(' data-connect-registration-mode="%s"',$CODOC_SETTINGS["codoc_connect_registration_mode"]);
110 }
111
112 $usercode_attributes = '';
113 $user_code = get_option(CODOC_USERCODE_OPTION_NAME);
114 if ($user_code) {
115 $usercode_attributes = sprintf(' data-usercode="%s"',$user_code);
116 }
117 //return str_replace(' src=', $data_css . ' defer src=', $tag);
118 return preg_replace('/(src=[^>]+)/',' ${1} ' . $data_css . $connect_attributes . $usercode_attributes . ' defer',$tag);
119 }
120 public function injector_shortcode($atts) {
121 global $post;
122 ob_start();
123 require 'views/codoc-injector.php';
124 return ob_get_clean();
125 }
126 # settings / 設定関連
127 public function add_settings() {
128 global $CODOC_SETTINGS;
129 add_action( 'admin_menu' ,function(){
130 add_options_page(
131 'codoc の設定', //ページタイトル
132 'codoc', //設定メニューに表示されるメニュータイトル
133 'edit_users', //権限
134 'codoc', //設定ページのURL。options-general.php?page=codoc
135 function() {
136 echo '<div class="codoc-settings">';
137 echo '<form method="post" action="options.php">';
138 settings_fields( 'codoc_option_group' );
139 do_settings_sections( 'codoc' );
140 submit_button(); // 送信ボタン
141 echo '</form></div>';
142 }
143 );
144 });
145
146 add_action('admin_print_styles', 'codoc_admin_styles');
147 function codoc_admin_styles($hook) {
148 wp_enqueue_style('codoc-options-style', plugins_url( 'codoc/css/codoc-options.css', __DIR__ ));
149 }
150 add_action( "admin_init", function() {
151 // codocからの認証データ処理
152 if (isset($_GET['page']) and $_GET['page'] == 'codoc' and isset($_GET['fetch_token_key'])) {
153 $key = sanitize_text_field($_GET['fetch_token_key']);
154 $usercode = sanitize_text_field($_GET['usercode']);
155 update_option(CODOC_USERCODE_OPTION_NAME,$usercode);
156 //$data = $this->callAPI('GET','/token',[ "fetch_token_key" => $key ]);
157 $data = $this->util->get_token([ "fetch_token_key" => $key ],["usercode" => $usercode, "token" => "1"]);
158 if ($data->status and $token = $data->token) {
159 update_option(CODOC_TOKEN_OPTION_NAME,$token);
160 //$data = $this->callAPI('GET','');
161 $data = $this->util->get_user_info([],["usercode" => $usercode, "token" => $token]);
162 if ($data->status and $user = $data->user) {
163 $this->update_codoc_authinfo($user);
164 }
165 #$current_url = (empty($_SERVER['HTTPS']) ? 'http://' : 'https://').$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
166 #$current_url = preg_replace('/(.*)fetch_token_key.*/','${1}&codoc_auth_finished=1',$current_url);
167 //add_settings_error( 'general', 'settings_updated', __( 'OK' ), 'success' );
168 $current_url = admin_url('options-general.php') . '?page=codoc&codoc_auth_finished=1';
169
170 wp_redirect( $current_url);
171 exit;
172 }
173 }
174 global $CODOC_USERCODE;
175 global $CODOC_SETTINGS;
176 global $CODOC_TOKEN;
177 global $CODOC_AUTHINFO;
178 $CODOC_USERCODE = get_option(CODOC_USERCODE_OPTION_NAME);
179 $CODOC_SETTINGS = get_option(CODOC_SETTINGS_OPTION_NAME);
180 $CODOC_TOKEN = get_option(CODOC_TOKEN_OPTION_NAME);
181 $CODOC_AUTHINFO = get_option(CODOC_AUTHINFO_OPTION_NAME);
182 if( !$CODOC_SETTINGS ) {
183 //デフォルト値
184 $CODOC_SETTINGS = array(
185 'css_path' => '',
186 );
187 update_option( CODOC_SETTINGS_OPTION_NAME, $CODOC_SETTINGS );
188 }
189 if (!isset($CODOC_SETTINGS['str_replace_binded_url_from'])) {
190 $CODOC_SETTINGS['str_replace_binded_url_from'] = '';
191 }
192 if (!isset($CODOC_SETTINGS['str_replace_binded_url_to'])) {
193 $CODOC_SETTINGS['str_replace_binded_url_to'] = '';
194 }
195 if (!isset($CODOC_SETTINGS['str_before_codoc_tag'])) {
196 $CODOC_SETTINGS['str_before_codoc_tag'] = '';
197 }
198 if (!isset($CODOC_SETTINGS['str_after_codoc_tag'])) {
199 $CODOC_SETTINGS['str_after_codoc_tag'] = '';
200 }
201
202 if (!isset($CODOC_SETTINGS['always_show_support'])) {
203 $CODOC_SETTINGS['always_show_support'] = '0';
204 }
205 if (!isset($CODOC_SETTINGS['show_support_message'])) {
206 $CODOC_SETTINGS['show_support_message'] = '';
207 }
208 if (!isset($CODOC_SETTINGS['show_support_categories'])) {
209 $CODOC_SETTINGS['show_support_categories'] = '';
210 }
211 if (!isset($CODOC_SETTINGS['show_support_location'])) {
212 $CODOC_SETTINGS['show_support_location'] = 'bottom';
213 }
214 if (!isset($CODOC_SETTINGS['do_not_filter_the_content'])) {
215 $CODOC_SETTINGS['do_not_filter_the_content'] = '0';
216 }
217
218 if (!isset($CODOC_SETTINGS['entry_button_text'])) {
219 $CODOC_SETTINGS['entry_button_text'] = '';
220 }
221 if (!isset($CODOC_SETTINGS['subscription_button_text'])) {
222 $CODOC_SETTINGS['subscription_button_text'] = '';
223 }
224 if (!isset($CODOC_SETTINGS['support_button_text'])) {
225 $CODOC_SETTINGS['support_button_text'] = '';
226 }
227 if (!isset($CODOC_SETTINGS['subscription_message'])) {
228 $CODOC_SETTINGS['subscription_message'] = '';
229 }
230 if (!isset($CODOC_SETTINGS['support_message'])) {
231 $CODOC_SETTINGS['support_message'] = '';
232 }
233 if (!isset($CODOC_SETTINGS['show_like'])) {
234 $CODOC_SETTINGS['show_like'] = '1';
235 }
236 if (!isset($CODOC_SETTINGS['show_about_codoc'])) {
237 $CODOC_SETTINGS['show_about_codoc'] = '1';
238 }
239 if (!isset($CODOC_SETTINGS['show_powered_by'])) {
240 $CODOC_SETTINGS['show_powered_by'] = '1';
241 }
242
243 if (!isset($CODOC_SETTINGS['codoc_tag_attributes'])) {
244 $CODOC_SETTINGS['codoc_tag_attributes'] = '';
245 }
246 if (!isset($CODOC_SETTINGS['codoc_connect_code'])) {
247 $CODOC_SETTINGS['codoc_connect_code'] = '';
248 }
249 if (!isset($CODOC_SETTINGS['codoc_connect_registration_mode'])) {
250 $CODOC_SETTINGS['codoc_connect_registration_mode'] = '';
251 }
252 if (!isset($CODOC_SETTINGS['debug_params'])) {
253 $CODOC_SETTINGS['debug_params'] = '';
254 }
255 add_settings_section(
256 'setting_section_id', // id
257 'codoc 設定ページ', // title
258 function (){}, // callback
259 'codoc' // page
260 );
261 // 認証がある場合
262 if ($CODOC_AUTHINFO) {
263 // クリエイター�
264 報だけ更新 (POST時に同期をとる)
265 if (isset($_GET['page']) and $_GET['page'] == 'codoc' and isset($_GET['settings-updated'])) {
266 $data = $this->util->get_user_info([],[
267 "usercode" => get_option(CODOC_USERCODE_OPTION_NAME),
268 "token" => get_option(CODOC_TOKEN_OPTION_NAME),
269 ]);
270 if ($data->status and $user = $data->user) {
271 $this->update_codoc_authinfo($user);
272 }
273 }
274
275 register_setting(
276 'codoc_option_group', // option group
277 CODOC_SETTINGS_OPTION_NAME // option name(DB)
278 );
279 add_settings_field(
280 'css_path', // id
281 'テーマ', // title
282 function() {
283 global $CODOC_SETTINGS;
284 echo '<div class="excerpt">テーマを指定してペイウォールのデザインを変えることができます。<br>パスを指定を選択することでCSSを直接指定するこもと可能です。</div>';
285 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']);
286 echo '' .
287 '<script type="text/javascript"> ' .
288 ' function gen_css_path(select) { ' .
289 ' if (select.value == "dark") { ' .
290 ' document.getElementById("darkmode-caution").style.display="block";' .
291 ' } else { ' .
292 ' if (document.getElementById("darkmode-caution")) { ' .
293 ' document.getElementById("darkmode-caution").style.display="none";' .
294 ' } ' .
295 ' } ' .
296 ' if (select.value == "path") { ' .
297 ' document.getElementById("codoc_css_path").readOnly=false; ' .
298 ' if (!document.getElementById("codoc_css_path").value.match(/\//g)) {' .
299 ' document.getElementById("codoc_css_path").value=""; ' .
300 ' } ' .
301 ' } else { ' .
302 ' document.getElementById("codoc_css_path").readOnly=true; ' .
303 ' document.getElementById("codoc_css_path").value=select.value; ' .
304 ' } ' .
305 ' } ' .
306 '</script> ' ;
307
308 echo sprintf('<select name="theme_name" onChange="gen_css_path(this)" id="codoc_theme_select">');
309 foreach (["blue","red","green","black","dark","blue-square","red-square","green-square","black-square","dark-square"] as $theme) {
310 if ($theme == "blue") {
311 echo sprintf('<option value="%s" %s>青</option>', $theme, ($CODOC_SETTINGS["css_path"] == "blue" ? "selected" : ""));
312 }
313 if ($theme == "red") {
314 echo sprintf('<option value="%s" %s>赤</option>', $theme, ($CODOC_SETTINGS["css_path"] == "red" ? "selected" : ""));
315 }
316 if ($theme == "green") {
317 echo sprintf('<option value="%s" %s>緑</option>', $theme, ($CODOC_SETTINGS["css_path"] == "green" ? "selected" : ""));
318 }
319 if ($theme == "black") {
320 echo sprintf('<option value="%s" %s>黒</option>', $theme, ($CODOC_SETTINGS["css_path"] == "black" ? "selected" : ""));
321 }
322 if ($theme == "dark") {
323 echo sprintf('<option value="%s" %s>ダークモード</option>', $theme, ($CODOC_SETTINGS["css_path"] == "dark" ? "selected" : ""));
324 }
325 if ($theme == "blue-square") {
326 echo sprintf('<option value="%s" %s>青(四角)</option>', $theme, ($CODOC_SETTINGS["css_path"] == "blue-square" ? "selected" : ""));
327 }
328 if ($theme == "red-square") {
329 echo sprintf('<option value="%s" %s>赤(四角)</option>', $theme, ($CODOC_SETTINGS["css_path"] == "red-square" ? "selected" : ""));
330 }
331 if ($theme == "green-square") {
332 echo sprintf('<option value="%s" %s>緑(四角)</option>', $theme, ($CODOC_SETTINGS["css_path"] == "green-square" ? "selected" : ""));
333 }
334 if ($theme == "black-square") {
335 echo sprintf('<option value="%s" %s>黒(四角)</option>', $theme, ($CODOC_SETTINGS["css_path"] == "black-square" ? "selected" : ""));
336 }
337 if ($theme == "dark-square") {
338 echo sprintf('<option value="%s" %s>ダークモード(四角)</option>', $theme, ($CODOC_SETTINGS["css_path"] == "dark-square" ? "selected" : ""));
339 }
340 }
341 echo sprintf('<option value="path" %s>パス指定</option>', (preg_match("/\//",$CODOC_SETTINGS["css_path"]) ? "selected" : ""));
342 echo '<script type="text/javascript">gen_css_path(document.getElementById(\'codoc_theme_select\'))</script>';
343 echo sprintf('</select></div>');
344 echo '<p id="darkmode-caution" style="display:none">ダークモード向けテーマは文字色が白となり、背景色によっては文字が見えない状�
345 �となるためご注意ください。</p>';
346 },
347 'codoc', //page
348 'setting_section_id' //Section
349 );
350 add_settings_field(
351 'paywall_text', // id
352 '各種文言', // title
353 function() {
354 global $CODOC_SETTINGS;
355 echo '<div class="excerpt">ペイウォール�
356 の一部文言を変更できます。</div>';
357 echo '<table class="innerTable"><tbody>';
358 echo '<tr><th>いいねの表示</th><td>';
359 echo sprintf('<input type="radio" value="1" name="%s[show_like]" id="show_like_on" %s><label for="show_like_on">する</label> ',CODOC_SETTINGS_OPTION_NAME,($CODOC_SETTINGS['show_like'] == '1' ? "checked" : ""));
360 echo sprintf('<input type="radio" value="0" name="%s[show_like]" id="show_like_off" %s><label for="show_like_off">しない</label> <br / >',CODOC_SETTINGS_OPTION_NAME,($CODOC_SETTINGS['show_like'] == '0' ? "checked" : ""));
361 echo '</td></tr>';
362
363 echo '<tr><th>codocについての表示</th><td>';
364 echo sprintf('<input type="radio" value="1" name="%s[show_about_codoc]" id="show_about_codoc_on" %s><label for="show_about_codoc_on">する</label> ',CODOC_SETTINGS_OPTION_NAME,($CODOC_SETTINGS['show_about_codoc'] == '1' ? "checked" : ""));
365 echo sprintf('<input type="radio" value="0" name="%s[show_about_codoc]" id="show_about_codoc_off" %s><label for="show_about_codoc_off">しない</label> <br / >',CODOC_SETTINGS_OPTION_NAME,($CODOC_SETTINGS['show_about_codoc'] == '0' ? "checked" : ""));
366 echo '</td></tr>';
367
368 echo '<tr><th>PoweredByの表示</th><td>';
369 echo sprintf('<input type="radio" value="1" name="%s[show_powered_by]" id="show_powered_by_on" %s><label for="show_powered_by_on">する</label> ',CODOC_SETTINGS_OPTION_NAME,($CODOC_SETTINGS['show_powered_by'] == '1' ? "checked" : ""));
370 echo sprintf('<input type="radio" value="0" name="%s[show_powered_by]" id="show_powered_by_off" %s><label for="show_powered_by_off">しない</label> <br / >',CODOC_SETTINGS_OPTION_NAME,($CODOC_SETTINGS['show_powered_by'] == '0' ? "checked" : ""));
371 echo '</td></tr>';
372
373 echo '<tr><th>記事購�
374 �ボタンのテキスト</th><td>';
375 echo sprintf('<input type="text" name="%s[entry_button_text]" value="%s">',CODOC_SETTINGS_OPTION_NAME,esc_html($CODOC_SETTINGS['entry_button_text']));
376 echo '</td></tr>';
377
378 echo '<tr><th>サブスクリプション購�
379 �ボタンのテキスト</th><td>';
380 echo sprintf('<input type="text" name="%s[subscription_button_text]" value="%s">',CODOC_SETTINGS_OPTION_NAME,esc_html($CODOC_SETTINGS['subscription_button_text']));
381 echo '</td></tr>';
382
383 echo '<tr><th>サポートボタンのテキスト</th><td>';
384 echo sprintf('<input type="text" name="%s[support_button_text]" value="%s">',CODOC_SETTINGS_OPTION_NAME,esc_html($CODOC_SETTINGS['support_button_text']));
385 echo '</td></tr>';
386
387 echo '<tr><th>「サブスクリプションに含まれる記事」のテキスト</th><td>';
388 echo sprintf('<input type="text" name="%s[subscription_message]" value="%s">',CODOC_SETTINGS_OPTION_NAME,esc_html($CODOC_SETTINGS['subscription_message']));
389 echo '</td></tr>';
390
391 echo '<tr><th>サポート説明のテキスト</th><td>';
392 echo sprintf('<input type="text" name="%s[support_message]" value="%s">',CODOC_SETTINGS_OPTION_NAME,esc_html($CODOC_SETTINGS['support_message']));
393 echo '</td></tr>';
394
395 echo '</tbody></table>';
396 },
397 'codoc', //page
398 'setting_section_id' //Section
399 );
400
401 add_settings_field(
402 'codoc_tag_attributes', // id
403 'codocタグの属性', // title
404 function() {
405 global $CODOC_SETTINGS;
406 echo '<div class="excerpt">codocタグに特定の属性を追加することができます。</div>';
407 echo sprintf('<input type="text" name="%s[codoc_tag_attributes]" value="%s">',CODOC_SETTINGS_OPTION_NAME,htmlspecialchars($CODOC_SETTINGS['codoc_tag_attributes']));
408 },
409 'codoc', //page
410 'setting_section_id' //Section
411 );
412 add_settings_field(
413 'str_replace_binded_url', // id
414 'パーマリンク', // title
415 function() {
416 global $CODOC_SETTINGS;
417 echo '<div class="excerpt">codoc側に登録するパーマリンクを置換することができます。</div>';
418 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']));
419 echo ('<span class="suptext"></span>');
420 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']));
421 echo ('に変換');
422 },
423 'codoc', //page
424 'setting_section_id' //Section
425 );
426
427 add_settings_field(
428 'str_around_codoc_tag', // id
429 'HTMLの挿�
430 �', // title
431 function() {
432 global $CODOC_SETTINGS;
433 echo '<div class="excerpt">codocタグの前後にHTMLを挿�
434 �することができます。</div>';
435 echo sprintf('<div class="inputRow"><textarea name="%s[str_before_codoc_tag]" cols="40">%s</textarea>',CODOC_SETTINGS_OPTION_NAME,esc_html($CODOC_SETTINGS['str_before_codoc_tag']));
436 echo ('を前に挿�
437 �</div>');
438 echo sprintf('<div class="inputRow"><textarea name="%s[str_after_codoc_tag]" cols="40">%s</textarea>',CODOC_SETTINGS_OPTION_NAME,esc_html($CODOC_SETTINGS['str_after_codoc_tag']));
439 echo ('を後に挿�
440 �</div>');
441
442 },
443 'codoc', //page
444 'setting_section_id' //Section
445 );
446
447 add_settings_field(
448 'always_show_support_tag', // id
449 'サポートの自動挿�
450 �', // title
451 function() {
452 global $CODOC_SETTINGS;
453 echo '<div class="excerpt">codocブロックを挿�
454 �することなく投稿にサポート機能を追加します</div>';
455 echo '<table class="innerTable"><tbody>';
456 echo '<tr><th>自動挿�
457 �</th><td>';
458 echo sprintf('<input type="radio" value="1" name="%s[always_show_support]" id="always_show_support_on" %s><label for="always_show_support_on">する</label> ',CODOC_SETTINGS_OPTION_NAME,($CODOC_SETTINGS['always_show_support'] == '1' ? "checked" : ""));
459 echo sprintf('<input type="radio" value="0" name="%s[always_show_support]" id="always_show_support_off" %s><label for="always_show_support_off">しない</label> <br / >',CODOC_SETTINGS_OPTION_NAME,($CODOC_SETTINGS['always_show_support'] == '0' ? "checked" : ""));
460 echo '</td></tr>';
461 echo '<tr><th>表示位置</th><td>';
462 echo sprintf('<input type="radio" value="top" name="%s[show_support_location]" id="show_support_location_top" %s><label for="show_support_location_top">冒頭</label> ',CODOC_SETTINGS_OPTION_NAME,($CODOC_SETTINGS['show_support_location'] == 'top' ? "checked" : ""));
463 echo sprintf('<input type="radio" value="bottom" name="%s[show_support_location]" id="show_support_location_bottom" %s><label for="show_support_location_bottom">末尾</label> <br / >',CODOC_SETTINGS_OPTION_NAME,($CODOC_SETTINGS['show_support_location'] == 'bottom' ? "checked" : ""));
464 echo '</td></tr>';
465 echo '<tr><th>説明文</th><td>';
466 echo sprintf('<input type="text" placeholder="サポートの説明文をカスタマイズできます" size="50%%" name="%s[show_support_message]" value="%s">',CODOC_SETTINGS_OPTION_NAME,esc_html($CODOC_SETTINGS['show_support_message']));
467 echo '</td></tr>';
468 echo '<tr><th>対象カテゴリ名</th><td>';
469 echo sprintf('<input placeholder="&quot;|&quot;区切りで複数指定できます" type="text" size="50%%" name="%s[show_support_categories]" value="%s"><br />',CODOC_SETTINGS_OPTION_NAME,esc_html($CODOC_SETTINGS['show_support_categories']));
470 echo '</td></tr>';
471 echo '</tbody></table>';
472
473 },
474 'codoc', //page
475 'setting_section_id' //Section
476 );
477 add_settings_field(
478 'do_not_filter_the_content', // id
479 '支障が出る処理を無効化', // title
480 function() {
481 global $CODOC_SETTINGS;
482 echo '<div class="excerpt">codocの動作に支障が出る他のプラグインによる<strong>フィルター処理</strong>を無効化できます。codocが上手く動かない場合のみ利用してください。</div>';
483 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">する</label> ',CODOC_SETTINGS_OPTION_NAME,($CODOC_SETTINGS['do_not_filter_the_content'] == '1' ? "checked" : ""));
484 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">しない</label> ',CODOC_SETTINGS_OPTION_NAME,($CODOC_SETTINGS['do_not_filter_the_content'] == '0' ? "checked" : ""));
485 },
486 'codoc', //page
487 'setting_section_id' //Section
488 );
489 add_settings_field(
490 'debug_params', // id
491 'デバック用', // title
492 function() {
493 global $CODOC_SETTINGS;
494 echo '<div class="excerpt">デバック用のパラメーターを指定できます。codocサポートから依頼がある場合のみ利用してください。</div>';
495 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']));
496 },
497 'codoc', //page
498 'setting_section_id' //Section
499 );
500
501 add_settings_field(
502 'cretor_info', // id
503 'クリエイター�
504 ', // title
505 function() {
506 global $CODOC_SETTINGS;
507 global $CODOC_AUTHINFO;
508 echo sprintf('<p><font id="codoc-update-creator-info-message"></font></p>',"");
509 // 変更を保存時に常に同期するのでこの表示は�
510 要ないがUI上保持しておく
511 $script = "document.getElementById('codoc-update-creator-info-message').innerText='�
512 報を更新するには変更を保存してください';document.getElementById('codoc-update-creator-info-message').color='red';";
513 if (isset($CODOC_AUTHINFO['profile_image_url'])) {
514 echo sprintf('<img src="%s" width="40" height="40" />',$CODOC_AUTHINFO['profile_image_url']);
515 }
516 echo sprintf('<p>%s</p>',$CODOC_AUTHINFO['name']);
517 if ($connect_code = $CODOC_SETTINGS['codoc_connect_code']) {
518 echo sprintf('<p>%s外部サービス連携済(連携コード:%s)</p>',
519 ($CODOC_SETTINGS['codoc_connect_registration_mode'] == 'dedicated' ? '専用ユーザーモードで' : ''),
520 $connect_code);
521 }
522 echo sprintf('<p>�
523 報を<a href="javascript:void(0);" onClick="' . $script . '">更新する</p>');
524 },
525 'codoc', //page
526 'setting_section_id' //Section
527 );
528
529 register_setting(
530 'codoc_option_group', // option group
531 CODOC_USERCODE_OPTION_NAME
532 );
533 register_setting(
534 'codoc_option_group', // option group
535 CODOC_TOKEN_OPTION_NAME
536 );
537
538 if (isset($_GET['codoc_auth_finished']) and $_GET['codoc_auth_finished']) {
539 add_settings_error( 'general', 'settings_updated', __( 'codocの認証が完了しました' ), 'success' );
540 }
541
542 add_settings_field(
543 'codoc_auth',
544 '認証',
545 function() {
546 global $CODOC_USERCODE;
547 global $CODOC_TOKEN;
548 global $CODOC_AUTHINFO;
549 $script = "javascript:document.getElementById('codoc-usercode').value='-';document.getElementById('codoc-token').value='-';document.getElementById('codoc-auth-message').innerText='解除を完了するには変更を保存してください';document.getElementById('codoc-auth-message').color='red';";
550
551 echo sprintf('<p><font color="green" id="codoc-auth-message"><strong>%s</strong> で認証済</font></p>',$CODOC_AUTHINFO['email']);
552
553 echo sprintf('<input id="codoc-usercode" type="hidden" name="%s" value="%s">',CODOC_USERCODE_OPTION_NAME,$CODOC_USERCODE);
554 echo sprintf('<input id="codoc-token" type="hidden" name="%s" value="%s">',CODOC_TOKEN_OPTION_NAME,$CODOC_TOKEN);
555 echo sprintf('<p>認証を<a href="javascript:void(0);" onClick="' . $script . '">解除する</p>');
556 },
557 'codoc',
558 'setting_section_id'
559 );
560
561 }
562
563 // ない場合
564 if (!$CODOC_AUTHINFO and !isset($_GET['auth_by_myself'])) {
565 add_settings_field(
566 'codoc_auth',
567 '認証',
568 function() {
569 //$current_url = (empty($_SERVER['HTTPS']) ? 'http://' : 'https://').$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
570 $current_url = admin_url('options-general.php') . '?page=codoc';
571 $login_url = sprintf("location.href='%s'",$this->get_codoc_url() . '/me/token?from=wp&return_url=' . urlencode($current_url));
572 $register_url = sprintf("location.href='%s'",$this->get_codoc_url() . '/register?from=wp&return_url=' . urlencode($current_url));
573 // submitを消しておく
574 echo ('<script type="text/javascript">window.onload=function(){document.getElementById(\'submit\').style.display = \'none\'}</script>');
575 echo ('codocをWordPressでご利用いただくには認証が�
576 要です。<br />');
577 echo sprintf('ユーザーコードとAPIトークンを<a href="%s&auth_by_myself=1">直接�
578 �力</a>することでも認証できます。<br /><br />',$current_url);
579 echo sprintf('<input type="button" class="button button-primary" value="ログインして認証" onClick="%s"> <input type="button" class="button button-primary" value="登録して認証" onClick="%s"><br /><br />',$login_url,$register_url);
580
581
582 },
583 'codoc',
584 'setting_section_id'
585 );
586 }
587 // ない場合かつ自分で認証する場合
588 if (!$CODOC_AUTHINFO and (
589 (isset($_GET['auth_by_myself']) and $_GET['auth_by_myself']) or
590 (isset($_POST['auth_by_myself']) and $_POST['auth_by_myself'])
591 )) {
592 register_setting(
593 'codoc_option_group', // option group
594 CODOC_USERCODE_OPTION_NAME
595 );
596 register_setting(
597 'codoc_option_group', // option group
598 CODOC_TOKEN_OPTION_NAME
599 );
600 add_settings_field(
601 'codoc_usercode',
602 'ユーザーコード',
603 function() {
604 global $CODOC_USERCODE;
605 echo '<input type="hidden" name="auth_by_myself" value="1">';
606 echo sprintf('<input type="text" name="%s" value="%s">',CODOC_USERCODE_OPTION_NAME,$CODOC_USERCODE);
607 },
608 'codoc',
609 'setting_section_id'
610 );
611 add_settings_field(
612 'codoc_token',
613 'APIトークン',
614 function() {
615 global $CODOC_TOKEN;
616 echo sprintf('<input type="text" name="%s" value="%s">',CODOC_TOKEN_OPTION_NAME,$CODOC_TOKEN);
617 },
618 'codoc',
619 'setting_section_id'
620 );
621 }
622
623 // 認証�
624 報を保存
625 add_action( 'update_option_' . CODOC_USERCODE_OPTION_NAME, function( $old_value, $new_value ) {
626 global $CODOC_RE_AUTHORIZE;
627 $CODOC_RE_AUTHORIZE = 1;
628 },9,2); // $hook, $function_to_add, $priority, $accepted_args
629 add_action( 'update_option_' . CODOC_TOKEN_OPTION_NAME, function( $old_value, $new_value ) {
630 global $CODOC_RE_AUTHORIZE;
631 $CODOC_RE_AUTHORIZE = 1;
632 },10,2);
633
634 add_action('update_option_' . CODOC_SETTINGS_OPTION_NAME,function(){
635 $CODOC_SETTINGS = get_option(CODOC_SETTINGS_OPTION_NAME);
636 if (isset($CODOC_SETTINGS['always_show_support']) and $CODOC_SETTINGS['always_show_support']) {
637 $data = $this->util->get_support_entry([],[
638 "usercode" => get_option(CODOC_USERCODE_OPTION_NAME),
639 "token" => get_option(CODOC_TOKEN_OPTION_NAME),
640 ]);
641 if ($data->status and $entry = $data->entry) {
642 update_option(CODOC_SUPPORT_ENTRYCODE_OPTION_NAME,$entry->code);
643 } else {
644 update_option(CODOC_SUPPORT_ENTRYCODE_OPTION_NAME,'');
645 }
646 } elseif(isset($CODOC_SETTINGS['always_show_support']) and !$CODOC_SETTINGS['always_show_support']) {
647 update_option(CODOC_SUPPORT_ENTRYCODE_OPTION_NAME,'');
648 }
649 });
650
651 add_action('updated_option',function(){
652 global $CODOC_RE_AUTHORIZE;
653 if ($CODOC_RE_AUTHORIZE) {
654 //$data = $this->callAPI('GET','');
655 $data = $this->util->get_user_info([],[
656 "usercode" => get_option(CODOC_USERCODE_OPTION_NAME),
657 "token" => get_option(CODOC_TOKEN_OPTION_NAME),
658 ]);
659 if ($data->status and $user = $data->user) {
660 $this->update_codoc_authinfo($user);
661 } else {
662 update_option(CODOC_AUTHINFO_OPTION_NAME,'');
663 }
664 }
665 });
666
667 });
668 // プラグイン一覧に設定のリンクをいれる
669 add_filter( 'plugin_action_links_' . plugin_basename( plugin_dir_path( __FILE__ ) . 'codoc' . '.php' ),
670 function( $links ) {
671 $setting_link = sprintf( '<a href="%s">%s</a>', esc_url( add_query_arg( 'page', 'codoc', admin_url( 'options-general.php' ) ) ), esc_html( '設定' ) );
672 array_unshift( $links, $setting_link );
673
674 return $links;
675 }
676 );
677
678 }
679 public function add_mce() {
680 add_action( 'admin_enqueue_scripts', function() {
681 if ( ! current_user_can( 'edit_posts' ) && ! current_user_can( 'edit_pages' ) ) {
682 return;
683 }
684 $current_screen = get_current_screen();
685 if ( ( method_exists( $current_screen, 'is_block_editor' ) && $current_screen->is_block_editor() ) || ( function_exists( 'is_gutenberg_page' ) && is_gutenberg_page() ) ) {
686 // Gutenberg Editor
687 // なにもしない
688 } else {
689 // tinymce
690 if ( get_user_option( 'rich_editing' ) == 'true' ) {
691 // プラグイン�
692 で使うCSRF を生成
693 $path = plugins_url( 'codoc/src_mce/codoc-editor-onload.js', __DIR__ );
694 wp_enqueue_script( 'codoc-editor-onload', $path, array('jquery'), '', true );
695 // I just want to insert this global variables but i don't know how i can do it..
696 wp_localize_script(
697 'codoc-editor-onload',
698 'CODOCEDITOR',
699 array(
700 'action' => 'codoc_shortcodes',
701 'nonce' => wp_create_nonce( 'codoc_shortcodes' ),
702
703 'codoc_url' => $this->get_codoc_url(),
704 'codoc_usercode' => get_option(CODOC_USERCODE_OPTION_NAME),
705 'codoc_plugin_version' => CODOC_PLUGIN_VERSION,
706 'codoc_sdk_path' => CODOC_SDK_PATH,
707 )
708 );
709
710 // ここでtinymceのプラグイン追加
711 //wp_enqueue_style( 'codoc-admin-style', plugins_url( 'codoc/src_mce/codoc-admin.css', __DIR__ ) );
712 //add_editor_style( plugins_url( 'codoc/src_mce/codoc-admin.css', __DIR__ ) );
713 wp_enqueue_style( 'codoc-admin-style', $this->get_codoc_url() . CODOC_SDK_PATH . '.tinymce.css?c=' . date('Ymd') );
714 add_editor_style( $this->get_codoc_url() . CODOC_SDK_PATH . '.tinymce.css' );
715
716 add_filter( 'mce_external_plugins', function( $plugin_array ) {
717 //$plugin_array['codoc'] = plugins_url( 'codoc/src_mce/codoc-editor.js', __DIR__ );
718 $plugin_array['codoc'] = $this->get_codoc_url() . CODOC_SDK_PATH . '.tinymce.js?c=' . date('Ymd');
719 return $plugin_array;
720 } );
721 add_filter( 'mce_buttons', function( $buttons ) {
722 array_push( $buttons, "|", "codoc" );
723 return $buttons;
724 } );
725 // 非SSL環境の場合、なぜかhttps -> httpsになってしまうので対策
726 // コメントタグが除去されることがあるらしいのでvalid_elementsに�
727 要なタグを追加 (EXPERIMENTAL)
728 add_filter( 'tiny_mce_before_init', function($settings) {
729 $settings['external_plugins'] = preg_replace('/"codoc":"http:/','"codoc":"https:',$settings['external_plugins']);
730 foreach (['valid_elements','extended_valid_elements'] as $valid_elements) {
731 if (isset($settings[$valid_elements])) {
732 $settings[$valid_elements] = $settings[$valid_elements] . ',div[*],p[*],img[*],--[*]';
733 }
734 }
735 $CODOC_SETTINGS = get_option(CODOC_SETTINGS_OPTION_NAME);
736 if (isset($CODOC_SETTINGS["debug_params"])) {
737 $params_decoded = json_decode($CODOC_SETTINGS["debug_params"],true);
738 if (isset($params_decoded["mce_valid_elements"])) {
739 $settings['valid_elements'] = $params_decoded["mce_valid_elements"];
740 $settings['extended_valid_elements'] = $params_decoded["mce_valid_elements"];
741 }
742 }
743
744 return $settings;
745 },1000000);
746 }
747 }
748 } );
749 }
750 function update_codoc_authinfo($user) {
751 global $CODOC_SETTINGS;
752 global $CODOC_AUTHINFO;
753 if ($user->connect_code) {
754 $CODOC_SETTINGS = get_option(CODOC_SETTINGS_OPTION_NAME);
755 $CODOC_SETTINGS['codoc_connect_code'] = $user->connect_code;
756 $CODOC_SETTINGS['codoc_connect_registration_mode'] = $user->connect_has_permission_dedicated_account ? 'dedicated' : '';
757 update_option(CODOC_SETTINGS_OPTION_NAME,$CODOC_SETTINGS);
758 }
759 return update_option(CODOC_AUTHINFO_OPTION_NAME,[
760 'email' => $user->email,
761 'name' => $user->name,
762 'profile_image_url' => $user->profile_image_url,
763 'cover_image_url' => $user->cover_image_url,
764 'connect_code' => $user->connect_code ? $user->connect_code : '',
765 'connect_image_url' => $user->connect_image_url ? $user->connect_image_url : '',
766 ]);
767 $CODOC_AUTHINFO = get_option(CODOC_AUTHINFO_OPTION_NAME);
768 }
769 public function get_filtered_content($post_emulated) {
770 // 記事ページであることをエミュレートしてフィルター実行
771 $post_backuped = null;
772 if (isset($GLOBALS['post'])) {
773 $post_backuped = $GLOBALS['post'];
774 }
775 $GLOBALS['post'] = $post_emulated;
776
777 $wp_query_backuped = null;
778 if (isset($GLOBALS['wp_query'])) {
779 $wp_query_backuped = $GLOBALS['wp_query'];
780 $wp_query = $GLOBALS['wp_query'];
781 $wp_query->is_single = true;
782 $wp_query->is_singular = true;
783 # in_the_loop は記事ページでは通常有効っぽいので true指定 ex: wp_ulike
784 $wp_query->in_the_loop = true;
785 # これも追加しておく
786 $wp_query->is_main_query = true;
787
788 $wp_query->post = $post_emulated;
789 $wp_query->queried_object = $post_emulated;
790 $wp_query->queried_object_id = $post_emulated->id;
791
792 $GLOBALS['wp_query'] = $wp_query;
793 }
794 // $this->the_content でオリジナルを返してもらうため
795 $this->do_not_filter_the_content = true;
796 $content = preg_replace(
797 '/ (\/)?wp:codoc\/codoc-block /',' \\1wptmp:codoc/codoc-block ',
798 $post_emulated->post_content
799 );
800 #$content = do_shortcode( $content );
801 $content_filtered = apply_filters( 'the_content', $content);
802 $this->do_not_filter_the_content = false;
803
804 $GLOBALS['post'] = $post_backuped;
805
806 if ($wp_query_backuped) {
807 $GLOBALS['wp_query'] = $wp_query_backuped;
808 }
809
810 $content_filtered = preg_replace(
811 '/ (\/)?wptmp:codoc\/codoc-block /',' \\1wp:codoc/codoc-block ',
812 $content_filtered
813 );
814 return $content_filtered;
815 }
816
817 public function save_post($post_ID,$post,$update) {
818 if (preg_match('/^(auto-draft|inherit)$/',$post->post_status)) {
819 return;
820 }
821 $entryCode = get_post_meta($post_ID,'codoc_entry_code',true);
822 $codoc_settings = get_option(CODOC_SETTINGS_OPTION_NAME);
823 $post_content = (isset($codoc_settings['do_not_filter_the_content']) and $codoc_settings['do_not_filter_the_content']) ?
824 $post->post_content : $this->get_filtered_content($post);
825 $res = $this->util->sync_entry([
826 "post_title" => $post->post_title,
827 "post_content" => $post_content,
828 "post_status" => ($post->post_status == 'publish' ? 1 : 0),
829 "post_permalink" => get_permalink($post_ID),
830 "codoc_entry_code" => $entryCode,
831 "codoc_settings" => $codoc_settings,
832 ],[
833 "usercode" => get_option(CODOC_USERCODE_OPTION_NAME),
834 "token" => get_option(CODOC_TOKEN_OPTION_NAME),
835 ]);
836
837 if (is_object($res) and $res->status and !$entryCode) {
838 update_post_meta($post_ID,'codoc_entry_code',$res->entry->code);
839 }
840 return true;
841 }
842 function updated_post_meta( $meta_ID, $post_ID, $meta_key ) {
843 // スルーされてる場合は他のメタ�
844 報更新のタイミングにサムネイルをアップロード
845 $has_to_resend = get_post_meta($post_ID,'codoc_post_thumbnail_invoking_entry_code',true);
846 $has_to_resendi = get_post_meta($post_ID,'icodoc_post_thumbnail_invoking_entry_code',true);
847 if ($has_to_resend != 1 and $meta_key != '_thumbnail_id') {
848 return;
849 }
850 if ( has_post_thumbnail($post_ID) ) {
851 // 新規投稿の場合、タイミングによってはcodocEntryCodeが取得できないので一旦スルーする
852 if (!get_post_meta($post_ID,'codoc_entry_code',true)) {
853 update_post_meta($post_ID,'codoc_post_thumbnail_invoking_entry_code',1);
854 return;
855 } else {
856 update_post_meta($post_ID,'codoc_post_thumbnail_invoking_entry_code',0);
857 }
858 $attachment = wp_get_attachment_metadata( get_post_thumbnail_id($post_ID));
859 $upload_dir = wp_upload_dir();
860 $file_path = sprintf ('%s/%s',$upload_dir['basedir'],$attachment['file']);
861
862 return $this->util->post_thumbnail([
863 "file_path" => $file_path,
864 "boundary" => wp_generate_password(24),
865 "codoc_entry_code" => get_post_meta($post_ID,'codoc_entry_code',true),
866 ],[
867 "usercode" => get_option(CODOC_USERCODE_OPTION_NAME),
868 "token" => get_option(CODOC_TOKEN_OPTION_NAME),
869 ]);
870 }
871 }
872 function deleted_post_meta( $meta_ids, $post_ID, $meta_key,$meta_value ) {
873 if ($meta_key != '_thumbnail_id') {
874 return;
875 }
876 return $this->util->reset_thumbnail(
877 ["codoc_entry_code" => get_post_meta($post_ID,'codoc_entry_code',true)],
878 [
879 "usercode" => get_option(CODOC_USERCODE_OPTION_NAME),
880 "token" => get_option(CODOC_TOKEN_OPTION_NAME),
881 ]
882 );
883 }
884
885 function the_content( $post_content ) {
886 if ( is_single() && in_the_loop() && is_main_query() ) {
887 }
888 // get_filtered_content から do_not_filter_the_contentを有効にされるパターン
889 // オプションの設定値の意味合い(他のフィルタを無視)とは違うため注意
890 if ($this->do_not_filter_the_content) {
891 return $post_content;
892 }
893 $post = get_post();
894 # is_amp で実�
895 しているテンプレート用
896 $is_amp_endpoint = (function_exists('is_amp_endpoint') && is_amp_endpoint()) ? true :
897 ((function_exists('is_amp') && is_amp()) ? true : false);
898 return $this->util->filter_content([
899 "post_content" => $post_content,
900 "preview" => is_preview(),
901 "codoc_entry_code" => get_post_meta($post->ID,'codoc_entry_code',true),
902 "codoc_settings" => get_option(CODOC_SETTINGS_OPTION_NAME),
903 "is_amp_endpoint" => $is_amp_endpoint,
904 "post_permalink" => get_permalink($post->ID),
905 "codoc_support_entry_code" => get_option(CODOC_SUPPORT_ENTRYCODE_OPTION_NAME),
906 ]);
907 }
908
909 function excerpt_allowed_blocks ($allowed_blocks) {
910 if (is_array($allowed_blocks)) {
911 array_push($allowed_blocks,'codoc/codoc-block');
912 }
913 return $allowed_blocks;
914 }
915 }
916