PluginProbe
codoc / 0.9.3.2
codoc v0.9.3.2
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
← All changes | class-codoc.php +162 -35 0.9.110.9.3.2 View file →
@@ -14,9 +14,10 @@
14 14 # the_content フィルタを実行しない状態
15 15 $this->do_not_filter_the_content = false;
16 16
17 17 // paywall用の本文非表示化とcodocタグへの属性追加
18 - $priority = 10;
18 + #$priority = 999999999;
19 + $priority = 100000; # フィルターは最後に実施する default 10
19 20 $CODOC_SETTINGS = get_option(CODOC_SETTINGS_OPTION_NAME);
20 21 if (isset($CODOC_SETTINGS["debug_params"])) {
21 22 $params_decoded = json_decode($CODOC_SETTINGS["debug_params"],true);
22 23 if (isset($params_decoded["the_content_filter_priority"])) {
@@ -23,8 +24,10 @@
23 24 $priority = $params_decoded["the_content_filter_priority"];
24 25 }
25 26 }
26 27 add_filter('the_content',[$this,'the_content'],$priority);
28 + // 2021-09-24 the_content を利用しない場合は個別に呼び出ししてもらう
29 + add_filter('codoc_the_content',[$this,'the_content'],$priority);
27 30
28 31 // 2020-07-25 excerpt対応
29 32 add_filter('excerpt_allowed_blocks',[$this,'excerpt_allowed_blocks']);
30 33
@@ -55,14 +58,40 @@
55 58 $this->add_mce();
56 59 } else { // ブログ画面
57 60 // codocのJS登録
58 61 add_action( 'wp_enqueue_scripts', function() {
59 - wp_enqueue_script( 'codoc-injector-js', $this->get_codoc_url() . '/js/cms.js' );
62 + $CODOC_SETTINGS = get_option(CODOC_SETTINGS_OPTION_NAME);
63 + $load_script_condition = '';
64 + if (isset($CODOC_SETTINGS["debug_params"]) and
65 + $params_decoded = json_decode($CODOC_SETTINGS["debug_params"],true) and
66 + isset($params_decoded["load_script_condition"])
67 + ) {
68 + $load_script_condition = $params_decoded["load_script_condition"];
69 + }
70 + if ($load_script_condition == 'not_list_page') {
71 + // not_list_pageの場合はトップページ等でスクリプトをロードしない
72 + if (!(is_archive() or is_category() or is_home() or is_front_page())) {
73 + wp_enqueue_script( 'codoc-injector-js', $this->get_codoc_url() . '/js/cms.js' );
74 + }
75 + } else {
76 + wp_enqueue_script( 'codoc-injector-js', $this->get_codoc_url() . '/js/cms.js' );
77 + }
60 78 });
61 - // 登録したscriptタグに属性をつける
79 + //登録したscriptタグに属性をつける
62 80 add_filter('script_loader_tag', [$this,'modifier_script_tag'],10,2);
63 81 // tinymce用のショートコード
64 82 add_shortcode('codoc', [$this, 'injector_shortcode']);
83 + // テーマをbodyにも反映
84 + add_filter('body_class', function($classes) {
85 + $CODOC_SETTINGS = get_option(CODOC_SETTINGS_OPTION_NAME);
86 + $css_path = 'blue';
87 + if (isset($CODOC_SETTINGS["css_path"]) and $CODOC_SETTINGS["css_path"]) {
88 + $css_path = $CODOC_SETTINGS["css_path"];
89 + }
90 + array_push($classes,sprintf( "codoc-theme-%s",$css_path));
91 + return $classes;
92 + });
93 +
65 94 }
66 95 return $this;
67 96 }
68 97 public function get_codoc_url() {
@@ -90,17 +119,25 @@
90 119 $connect_code = $CODOC_SETTINGS["codoc_connect_code"];
91 120 $connect_attributes = sprintf(' data-connect-code="%s"',$connect_code);
92 121 $tag = preg_replace('/cms\.js/','cms-connect.js',$tag);
93 122 }
94 - if (isset($CODOC_SETTINGS["debug_params"])) {
95 - $params_decoded = json_decode($CODOC_SETTINGS["debug_params"],true);
96 - if (isset($params_decoded["connect_registration_mode"])) {
97 - $connect_attributes = $connect_attributes . sprintf(' data-connect-registration-mode="%s"',$params_decoded["connect_registration_mode"]);
98 - }
123 + if (isset($CODOC_SETTINGS["codoc_connect_registration_mode"]) and $CODOC_SETTINGS["codoc_connect_registration_mode"]) {
124 + $connect_attributes = $connect_attributes .
125 + sprintf(' data-connect-registration-mode="%s"',$CODOC_SETTINGS["codoc_connect_registration_mode"]);
99 126 }
100 -
127 +
128 + $usercode_attributes = '';
129 + $user_code = get_option(CODOC_USERCODE_OPTION_NAME);
130 + if ($user_code) {
131 + $usercode_attributes = sprintf(' data-usercode="%s"',$user_code);
132 + }
133 +
134 + $setting_attributes = '';
135 + if (isset($CODOC_SETTINGS['codoc_script_tag_attributes']) and $CODOC_SETTINGS['codoc_script_tag_attributes']) {
136 + $setting_attributes = $CODOC_SETTINGS['codoc_script_tag_attributes'];
137 + }
101 138 //return str_replace(' src=', $data_css . ' defer src=', $tag);
102 - return preg_replace('/(src=[^>]+)/',' ${1} ' . $data_css . $connect_attributes . ' defer',$tag);
139 + return preg_replace('/(src=[^>]+)/',' ${1} ' . $data_css . $connect_attributes . $usercode_attributes . $setting_attributes . ' defer',$tag);
103 140 }
104 141 public function injector_shortcode($atts) {
105 142 global $post;
106 143 ob_start();
@@ -130,9 +167,8 @@
130 167 add_action('admin_print_styles', 'codoc_admin_styles');
131 168 function codoc_admin_styles($hook) {
132 169 wp_enqueue_style('codoc-options-style', plugins_url( 'codoc/css/codoc-options.css', __DIR__ ));
133 170 }
134 -
135 171 add_action( "admin_init", function() {
136 172 // codocからの認証データ処理
137 173 if (isset($_GET['page']) and $_GET['page'] == 'codoc' and isset($_GET['fetch_token_key'])) {
138 174 $key = sanitize_text_field($_GET['fetch_token_key']);
@@ -144,12 +180,9 @@
144 180 update_option(CODOC_TOKEN_OPTION_NAME,$token);
145 181 //$data = $this->callAPI('GET','');
146 182 $data = $this->util->get_user_info([],["usercode" => $usercode, "token" => $token]);
147 183 if ($data->status and $user = $data->user) {
148 - update_option(CODOC_AUTHINFO_OPTION_NAME,[
149 - 'email' => $user->email,
150 - 'name' => $user->name,
151 - ]);
184 + $this->update_codoc_authinfo($user);
152 185 }
153 186 #$current_url = (empty($_SERVER['HTTPS']) ? 'http://' : 'https://').$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
154 187 #$current_url = preg_replace('/(.*)fetch_token_key.*/','${1}&codoc_auth_finished=1',$current_url);
155 188 //add_settings_error( 'general', 'settings_updated', __( 'OK' ), 'success' );
@@ -230,15 +263,20 @@
230 263
231 264 if (!isset($CODOC_SETTINGS['codoc_tag_attributes'])) {
232 265 $CODOC_SETTINGS['codoc_tag_attributes'] = '';
233 266 }
267 + if (!isset($CODOC_SETTINGS['codoc_script_tag_attributes'])) {
268 + $CODOC_SETTINGS['codoc_script_tag_attributes'] = '';
269 + }
234 270 if (!isset($CODOC_SETTINGS['codoc_connect_code'])) {
235 271 $CODOC_SETTINGS['codoc_connect_code'] = '';
236 272 }
273 + if (!isset($CODOC_SETTINGS['codoc_connect_registration_mode'])) {
274 + $CODOC_SETTINGS['codoc_connect_registration_mode'] = '';
275 + }
237 276 if (!isset($CODOC_SETTINGS['debug_params'])) {
238 277 $CODOC_SETTINGS['debug_params'] = '';
239 278 }
240 -
241 279 add_settings_section(
242 280 'setting_section_id', // id
243 281 'codoc 設定ページ', // title
244 282 function (){}, // callback
@@ -245,8 +283,19 @@
245 283 'codoc' // page
246 284 );
247 285 // 認証がある場合
248 286 if ($CODOC_AUTHINFO) {
287 + // クリエイター情報だけ更新 (POST時に同期をとる)
288 + if (isset($_GET['page']) and $_GET['page'] == 'codoc' and isset($_GET['settings-updated'])) {
289 + $data = $this->util->get_user_info([],[
290 + "usercode" => get_option(CODOC_USERCODE_OPTION_NAME),
291 + "token" => get_option(CODOC_TOKEN_OPTION_NAME),
292 + ]);
293 + if ($data->status and $user = $data->user) {
294 + $this->update_codoc_authinfo($user);
295 + }
296 + }
297 +
249 298 register_setting(
250 299 'codoc_option_group', // option group
251 300 CODOC_SETTINGS_OPTION_NAME // option name(DB)
252 301 );
@@ -262,9 +311,11 @@
262 311 ' function gen_css_path(select) { ' .
263 312 ' if (select.value == "dark") { ' .
264 313 ' document.getElementById("darkmode-caution").style.display="block";' .
265 314 ' } else { ' .
266 - ' document.getElementById("darkmode-caution").style.display="none";' .
315 + ' if (document.getElementById("darkmode-caution")) { ' .
316 + ' document.getElementById("darkmode-caution").style.display="none";' .
317 + ' } ' .
267 318 ' } ' .
268 319 ' if (select.value == "path") { ' .
269 320 ' document.getElementById("codoc_css_path").readOnly=false; ' .
270 321 ' if (!document.getElementById("codoc_css_path").value.match(/\//g)) {' .
@@ -377,19 +428,18 @@
377 428 'codoc', //page
378 429 'setting_section_id' //Section
379 430 );
380 431 add_settings_field(
381 - 'codoc_connect_code', // id
382 - 'サービス連携コード', // title
432 + 'codoc_script_tag_attributes', // id
433 + 'codoc scriptタグの属性', // title
383 434 function() {
384 435 global $CODOC_SETTINGS;
385 - echo '<div class="excerpt">サービス連携ウィジェットを利用するためにはアカウント設定でサービス連携を有効化し、サービス連携コードを指定してください</div>';
386 - echo sprintf('<input type="text" name="%s[codoc_connect_code]" value="%s">',CODOC_SETTINGS_OPTION_NAME,esc_html($CODOC_SETTINGS['codoc_connect_code']));
436 + echo '<div class="excerpt">codoc scriptタグに特定の属性を追加することができます。</div>';
437 + echo sprintf('<input type="text" name="%s[codoc_script_tag_attributes]" value="%s">',CODOC_SETTINGS_OPTION_NAME,htmlspecialchars($CODOC_SETTINGS['codoc_script_tag_attributes']));
387 438 },
388 439 'codoc', //page
389 440 'setting_section_id' //Section
390 441 );
391 -
392 442 add_settings_field(
393 443 'str_replace_binded_url', // id
394 444 'パーマリンク', // title
395 445 function() {
@@ -470,8 +520,32 @@
470 520 'codoc', //page
471 521 'setting_section_id' //Section
472 522 );
473 523
524 + add_settings_field(
525 + 'cretor_info', // id
526 + 'クリエイター情報', // title
527 + function() {
528 + global $CODOC_SETTINGS;
529 + global $CODOC_AUTHINFO;
530 + echo sprintf('<p><font id="codoc-update-creator-info-message"></font></p>',"");
531 + // 変更を保存時に常に同期するのでこの表示は必要ないがUI上保持しておく
532 + $script = "document.getElementById('codoc-update-creator-info-message').innerText='情報を更新するには変更を保存してください';document.getElementById('codoc-update-creator-info-message').color='red';";
533 + if (isset($CODOC_AUTHINFO['profile_image_url'])) {
534 + echo sprintf('<img src="%s" width="40" height="40" />',$CODOC_AUTHINFO['profile_image_url']);
535 + }
536 + echo sprintf('<p>%s</p>',$CODOC_AUTHINFO['name']);
537 + if ($connect_code = $CODOC_SETTINGS['codoc_connect_code']) {
538 + echo sprintf('<p>%s外部サービス連携済(連携コード:%s)</p>',
539 + ($CODOC_SETTINGS['codoc_connect_registration_mode'] == 'dedicated' ? '専用ユーザーモードで' : ''),
540 + $connect_code);
541 + }
542 + echo sprintf('<p>情報を<a href="javascript:void(0);" onClick="' . $script . '">更新する</a></p>');
543 + echo ('<p>codoc側でロゴやカバー画像を変更した場合は都度更新を行ってください</p>');
544 + },
545 + 'codoc', //page
546 + 'setting_section_id' //Section
547 + );
474 548
475 549 register_setting(
476 550 'codoc_option_group', // option group
477 551 CODOC_USERCODE_OPTION_NAME
@@ -511,13 +585,17 @@
511 585 add_settings_field(
512 586 'codoc_auth',
513 587 '認証',
514 588 function() {
515 -
589 + $theme = wp_get_theme();
590 + $from = 'wp';
591 + if ($theme->get('Name') === 'codoc') {
592 + $from = 'wp_codoc';
593 + }
516 594 //$current_url = (empty($_SERVER['HTTPS']) ? 'http://' : 'https://').$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
517 595 $current_url = admin_url('options-general.php') . '?page=codoc';
518 - $login_url = sprintf("location.href='%s'",$this->get_codoc_url() . '/me/token?from=wp&return_url=' . urlencode($current_url));
519 - $register_url = sprintf("location.href='%s'",$this->get_codoc_url() . '/register?from=wp&return_url=' . urlencode($current_url));
596 + $login_url = sprintf("location.href='%s'",$this->get_codoc_url() . '/me/token?from=' . $from . '&return_url=' . urlencode($current_url));
597 + $register_url = sprintf("location.href='%s'",$this->get_codoc_url() . '/register?from=' . $from . '&return_url=' . urlencode($current_url));
520 598 // submitを消しておく
521 599 echo ('<script type="text/javascript">window.onload=function(){document.getElementById(\'submit\').style.display = \'none\'}</script>');
522 600 echo ('codocをWordPressでご利用いただくには認証が必要です。<br />');
523 601 echo sprintf('ユーザーコードとAPIトークンを<a href="%s&auth_by_myself=1">直接入力</a>することでも認証できます。<br /><br />',$current_url);
@@ -590,9 +668,9 @@
590 668 } elseif(isset($CODOC_SETTINGS['always_show_support']) and !$CODOC_SETTINGS['always_show_support']) {
591 669 update_option(CODOC_SUPPORT_ENTRYCODE_OPTION_NAME,'');
592 670 }
593 671 });
594 -
672 +
595 673 add_action('updated_option',function(){
596 674 global $CODOC_RE_AUTHORIZE;
597 675 if ($CODOC_RE_AUTHORIZE) {
598 676 //$data = $this->callAPI('GET','');
@@ -600,12 +678,9 @@
600 678 "usercode" => get_option(CODOC_USERCODE_OPTION_NAME),
601 679 "token" => get_option(CODOC_TOKEN_OPTION_NAME),
602 680 ]);
603 681 if ($data->status and $user = $data->user) {
604 - update_option(CODOC_AUTHINFO_OPTION_NAME,[
605 - 'email' => $user->email,
606 - 'name' => $user->name,
607 - ]);
682 + $this->update_codoc_authinfo($user);
608 683 } else {
609 684 update_option(CODOC_AUTHINFO_OPTION_NAME,'');
610 685 }
611 686 }
@@ -667,13 +742,51 @@
667 742 add_filter( 'mce_buttons', function( $buttons ) {
668 743 array_push( $buttons, "|", "codoc" );
669 744 return $buttons;
670 745 } );
746 + // 非SSL環境の場合、なぜかhttps -> httpsになってしまうので対策
747 + // コメントタグが除去されることがあるらしいのでvalid_elementsに必要なタグを追加 (EXPERIMENTAL)
748 + add_filter( 'tiny_mce_before_init', function($settings) {
749 + $settings['external_plugins'] = preg_replace('/"codoc":"http:/','"codoc":"https:',$settings['external_plugins']);
750 + foreach (['valid_elements','extended_valid_elements'] as $valid_elements) {
751 + if (isset($settings[$valid_elements])) {
752 + $settings[$valid_elements] = $settings[$valid_elements] . ',div[*],p[*],img[*],--[*]';
753 + }
754 + }
755 + $CODOC_SETTINGS = get_option(CODOC_SETTINGS_OPTION_NAME);
756 + if (isset($CODOC_SETTINGS["debug_params"])) {
757 + $params_decoded = json_decode($CODOC_SETTINGS["debug_params"],true);
758 + if (isset($params_decoded["mce_valid_elements"])) {
759 + $settings['valid_elements'] = $params_decoded["mce_valid_elements"];
760 + $settings['extended_valid_elements'] = $params_decoded["mce_valid_elements"];
761 + }
762 + }
671 763
764 + return $settings;
765 + },1000000);
672 766 }
673 767 }
674 768 } );
675 769 }
770 + function update_codoc_authinfo($user) {
771 + global $CODOC_SETTINGS;
772 + global $CODOC_AUTHINFO;
773 + if (property_exists($user,'connect_code') and $user->connect_code) {
774 + $CODOC_SETTINGS = get_option(CODOC_SETTINGS_OPTION_NAME);
775 + $CODOC_SETTINGS['codoc_connect_code'] = $user->connect_code;
776 + $CODOC_SETTINGS['codoc_connect_registration_mode'] = $user->connect_has_permission_dedicated_account ? 'dedicated' : '';
777 + update_option(CODOC_SETTINGS_OPTION_NAME,$CODOC_SETTINGS);
778 + }
779 + return update_option(CODOC_AUTHINFO_OPTION_NAME,[
780 + 'email' => $user->email,
781 + 'name' => $user->name,
782 + 'profile_image_url' => $user->profile_image_url,
783 + 'cover_image_url' => $user->cover_image_url,
784 + 'connect_code' => property_exists($user,'connect_code') ? $user->connect_code : '',
785 + 'connect_image_url' => property_exists($user,'connect_image_url') ? $user->connect_image_url : '',
786 + ]);
787 + $CODOC_AUTHINFO = get_option(CODOC_AUTHINFO_OPTION_NAME);
788 + }
676 789 public function get_filtered_content($post_emulated) {
677 790 // 記事ページであることをエミュレートしてフィルター実行
678 791 $post_backuped = null;
679 792 if (isset($GLOBALS['post'])) {
@@ -684,10 +797,15 @@
684 797 $wp_query_backuped = null;
685 798 if (isset($GLOBALS['wp_query'])) {
686 799 $wp_query_backuped = $GLOBALS['wp_query'];
687 800 $wp_query = $GLOBALS['wp_query'];
688 - $is_single_backuped = $wp_query->is_single;
689 801 $wp_query->is_single = true;
802 + $wp_query->is_singular = true;
803 + # in_the_loop は記事ページでは通常有効っぽいので true指定 ex: wp_ulike
804 + $wp_query->in_the_loop = true;
805 + # これも追加しておく
806 + $wp_query->is_main_query = true;
807 +
690 808 $wp_query->post = $post_emulated;
691 809 $wp_query->queried_object = $post_emulated;
692 810 $wp_query->queried_object_id = $post_emulated->id;
693 811
@@ -712,9 +830,8 @@
712 830 $content_filtered = preg_replace(
713 831 '/ (\/)?wptmp:codoc\/codoc-block /',' \\1wp:codoc/codoc-block ',
714 832 $content_filtered
715 833 );
716 -
717 834 return $content_filtered;
718 835 }
719 836
720 837 public function save_post($post_ID,$post,$update) {
@@ -727,9 +844,10 @@
727 844 $post->post_content : $this->get_filtered_content($post);
728 845 $res = $this->util->sync_entry([
729 846 "post_title" => $post->post_title,
730 847 "post_content" => $post_content,
731 - "post_status" => ($post->post_status == 'publish' ? 1 : 0),
848 + // password が設定されてる場合は限定公開にする
849 + "post_status" => $post->post_status == 'publish' ? ($post->post_password ? 2 : 1) : 0,
732 850 "post_permalink" => get_permalink($post_ID),
733 851 "codoc_entry_code" => $entryCode,
734 852 "codoc_settings" => $codoc_settings,
735 853 ],[
@@ -739,16 +857,25 @@
739 857
740 858 if (is_object($res) and $res->status and !$entryCode) {
741 859 update_post_meta($post_ID,'codoc_entry_code',$res->entry->code);
742 860 }
743 -
744 861 return true;
745 862 }
746 863 function updated_post_meta( $meta_ID, $post_ID, $meta_key ) {
747 - if ($meta_key != '_thumbnail_id') {
864 + // スルーされてる場合は他のメタ情報更新のタイミングにサムネイルをアップロード
865 + $has_to_resend = get_post_meta($post_ID,'codoc_post_thumbnail_invoking_entry_code',true);
866 + $has_to_resendi = get_post_meta($post_ID,'icodoc_post_thumbnail_invoking_entry_code',true);
867 + if ($has_to_resend != 1 and $meta_key != '_thumbnail_id') {
748 868 return;
749 869 }
750 870 if ( has_post_thumbnail($post_ID) ) {
871 + // 新規投稿の場合、タイミングによってはcodocEntryCodeが取得できないので一旦スルーする
872 + if (!get_post_meta($post_ID,'codoc_entry_code',true)) {
873 + update_post_meta($post_ID,'codoc_post_thumbnail_invoking_entry_code',1);
874 + return;
875 + } else {
876 + update_post_meta($post_ID,'codoc_post_thumbnail_invoking_entry_code',0);
877 + }
751 878 $attachment = wp_get_attachment_metadata( get_post_thumbnail_id($post_ID));
752 879 $upload_dir = wp_upload_dir();
753 880 $file_path = sprintf ('%s/%s',$upload_dir['basedir'],$attachment['file']);
754 881