66Version: {release version}
77Author: ぴいた
88License: GPL2
9+ 参考記事:https://webcake.stars.ne.jp/wp-posts-date-alert.html
10+ https://techmemo.biz/wordpress/wp-posts-date-alert/
11+ https://blog.kamata-net.com/archives/6867.html
912*/
1013
1114/**
12- * CSS・Javascript読み込み設定
15+ * CSS・JavaScript読み込み設定
1316 */
1417add_action ('wp_enqueue_scripts ' , 'jcm_load_scripts ' );
1518function jcm_load_scripts () {
16- if (is_single () || is_page ()) {
19+ // 設定項目にチェックのある投稿タイプだけ表示
20+ if (get_option ('jcm_option_post_type_single ' ) === false ) {
21+ $ jcm_is_single = true ;
22+ } elseif (get_option ('jcm_option_post_type_single ' ) === 'single ' ) {
23+ $ jcm_is_single = is_single ();
24+ } else {
25+ $ jcm_is_single = false ;
26+ }
27+ if (get_option ('jcm_option_post_type_page ' ) === false ) {
28+ $ jcm_is_page = true ;
29+ } elseif (get_option ('jcm_option_post_type_page ' ) === 'page ' ) {
30+ $ jcm_is_page = is_page ();
31+ } else {
32+ $ jcm_is_page = false ;
33+ }
34+
35+ // CSS・JavaScript読み込み
36+ if ($ jcm_is_single || $ jcm_is_page ) {
1737 $ plugin_url = plugin_dir_url ( __FILE__ );
18- wp_enqueue_style ('jcm-css ' , $ plugin_url .'dist/css/jcm.css ' );
38+ if (get_option ( 'jcm_option_css ' ) !== 'custom ' ) {
39+ wp_enqueue_style ('jcm-css ' , $ plugin_url .'dist/css/jcm.css ' );
40+ }
1941 wp_enqueue_script ('jcm-js ' , $ plugin_url .'dist/js/jcm.js ' ,'' ,'' ,true );
2042 }
2143}
@@ -27,19 +49,131 @@ function jcm_load_scripts() {
2749 * %day%:記事と現在の日差
2850 */
2951function jcm_add_content ($ content ) {
30- // 設定
31- $ content_time = get_the_date ('Y/m/d ' ); // 投稿日
32- // $content_time = get_the_modified_date('Y/m/d'); // 更新日
33- $ reference_date = '1 ' ;
34- $ reference_type = 'year ' ;
35- $ message_text = 'この記事は%year%年以上前に書かれたものです。<br>情報が古い可能性があります。 ' ;
52+ // 比較基準タイプ設定
53+ if (get_option ('jcm_option_reference ' ) === 'modified_date ' ) {
54+ $ content_time = get_the_modified_date ('Y/m/d ' ); // 更新日
55+ } else {
56+ $ content_time = get_the_date ('Y/m/d ' ); // 投稿日
57+ }
58+ // 比較基準日数設定
59+ if (get_option ('jcm_reference_date ' ) === false ) {
60+ $ reference_date = '1 ' ;
61+ } else {
62+ $ reference_date = get_option ('jcm_reference_date ' );
63+ }
64+ // 比較基準日or年設定
65+ if (get_option ('jcm_reference_type ' )) {
66+ $ reference_type = get_option ('jcm_reference_type ' );
67+ } else {
68+ $ reference_type = 'year ' ;
69+ }
70+ // 表示メッセージ設定
71+ if (get_option ( 'jcm_option_message ' )) {
72+ $ message_text = get_option ( 'jcm_option_message ' );
73+ } else {
74+ $ message_text = 'この記事は%year%年以上前に書かれたものです。<br>情報が古い可能性があります。 ' ;
75+ }
3676
3777 // DOM構築
3878 $ message = '<input type="hidden" id="jcm_content_time" value=" ' .$ content_time .'" style="display:none;"> ' ;
3979 $ message .= '<input type="hidden" id="jcm_reference_date" value=" ' .$ reference_date .'" style="display:none;"> ' ;
4080 $ message .= '<input type="hidden" id="jcm_reference_type" value=" ' .$ reference_type .'" style="display:none;"> ' ;
4181 $ message .= '<div id="jcm_content_message" style="display:none;"> ' .$ message_text .'</div> ' ;
82+ if (get_option ('jcm_option_css ' ) === 'custom ' ) {
83+ $ message .= '<style> ' .get_option ('jcm_option_css_custom ' ).'</style> ' ;
84+ }
4285
4386 return $ message .$ content ;
4487}
4588add_filter ('the_content ' , 'jcm_add_content ' ,'10 ' );
89+
90+ /**
91+ * 管理画面に設定項目追加
92+ * 参考:https://www.nxworld.net/wordpress/wp-add-settings-field.html
93+ * https://qiita.com/diconran/items/bfdc093b083a2ee530c9
94+ */
95+ function jcm_field () {
96+ /* 管理画面に項目追加 */
97+ add_settings_section ( 'jcm_option_section ' , 'JavaScript-Content-Message ' , function (){echo '<p>投稿・固定ページに表示させるメッセージの設定を行います。</p> ' ;}, 'writing ' );
98+ // 適用ページ
99+ add_settings_field ( 'jcm_option_post_type ' , '適用ページ ' , 'jcm_option_post_type ' , 'writing ' , 'jcm_option_section ' );
100+ register_setting ( 'writing ' , 'jcm_option_post_type_single ' );
101+ register_setting ( 'writing ' , 'jcm_option_post_type_page ' );
102+ // 比較基準日
103+ add_settings_field ( 'jcm_option_reference ' , '比較基準日 ' , 'jcm_option_reference ' , 'writing ' , 'jcm_option_section ' );
104+ register_setting ( 'writing ' , 'jcm_option_reference ' );
105+ register_setting ( 'writing ' , 'jcm_reference_date ' );
106+ register_setting ( 'writing ' , 'jcm_reference_type ' );
107+ // 表示メッセージ
108+ add_settings_field ( 'jcm_option_message ' , '表示メッセージ ' , 'jcm_option_message ' , 'writing ' , 'jcm_option_section ' );
109+ register_setting ( 'writing ' , 'jcm_option_message ' );
110+ // CSS設定
111+ add_settings_field ( 'jcm_option_css ' , 'CSS設定 ' , 'jcm_option_css ' , 'writing ' , 'jcm_option_section ' );
112+ register_setting ( 'writing ' , 'jcm_option_css ' );
113+ register_setting ( 'writing ' , 'jcm_option_css_custom ' );
114+ }
115+ add_filter ( 'admin_init ' , 'jcm_field ' );
116+ /* 管理画面設定項目DOM */
117+ function jcm_option_post_type () {
118+ // 適用ページ
119+ ?>
120+ <fieldset>
121+ <label><input name="jcm_option_post_type_single" type="checkbox" value="single" <?php echo get_option ( 'jcm_option_post_type_single ' ) === false ? 'checked="checked" ' : checked ( 'single ' , get_option ( 'jcm_option_post_type_single ' ) ); ?> />投稿</label>
122+ <label><input name="jcm_option_post_type_page" type="checkbox" value="page" <?php echo get_option ( 'jcm_option_post_type_page ' ) === false ? 'checked="checked" ' : checked ( 'page ' , get_option ( 'jcm_option_post_type_page ' ) ); ?> />固定ページ</label><br>
123+ </fieldset>
124+ <?php
125+ }
126+ function jcm_option_reference () {
127+ // 比較基準日
128+ ?>
129+ <fieldset>
130+ <label><input name="jcm_option_reference" type="radio" value="posted_date" <?php echo get_option ( 'jcm_option_reference ' ) === false ? 'checked="checked" ' : checked ( 'posted_date ' , get_option ( 'jcm_option_reference ' ) ); ?> />投稿日</label>
131+ <label><input name="jcm_option_reference" type="radio" value="modified_date" <?php checked ( 'modified_date ' , get_option ( 'jcm_option_reference ' ) ); ?> />更新日</label><br>
132+ <input type="number" name="jcm_reference_date" min="0" max="9999" value="<?php echo get_option ( 'jcm_reference_date ' ) === false ? '1 ' : get_option ( 'jcm_reference_date ' ); ?> ">
133+ <select name="jcm_reference_type">
134+ <option value="year" <?php echo get_option ( 'jcm_reference_type ' ) === false ? 'selected ' : selected ( 'year ' , get_option ( 'jcm_reference_type ' ) ); ?> >年</option>
135+ <option value="day" <?php selected ( 'day ' , get_option ( 'jcm_reference_type ' ) ); ?> >日</option>
136+ </select> 以上経過した記事にメッセージを表示
137+ </fieldset>
138+ <?php
139+ }
140+ function jcm_option_message () {
141+ // 表示メッセージ
142+ ?>
143+ <fieldset>
144+ <p>表示させたいメッセージを入力してください。(HTML使用可能)</p>
145+ <p>例)<?php echo esc_html ('この記事は%year%年以上前に書かれたものです。<br>情報が古い可能性があります。 ' ); ?> </p>
146+ <textarea name="jcm_option_message" class="large-text code" rows="3"><?php echo get_option ( 'jcm_option_message ' ) === false ? 'この記事は%year%年以上前に書かれたものです。<br>情報が古い可能性があります。 ' : get_option ( 'jcm_option_message ' ); ?> </textarea>
147+ <p>利用可能なタグ</p>
148+ <ul>
149+ <li><code>%year%</code>:記事と現在の年差</li>
150+ <li><code>%monthnum%</code>:記事と現在の月差(1ヶ月あたり30日で計算のため多少ブレあり)</li>
151+ <li><code>%day%</code>:記事と現在の日差</li>
152+ </ul>
153+ </fieldset>
154+ <?php
155+ }
156+ function jcm_option_css () {
157+ // CSS設定
158+ ?>
159+ <style>
160+ .jcm_option_css_custom {
161+ display: none;
162+ }
163+ #jcm_option_css_custom:checked ~ .jcm_option_css_custom {
164+ display: block;
165+ }
166+ </style>
167+ <fieldset>
168+ <input id="jcm_option_css_default" name="jcm_option_css" type="radio" value="default" <?php echo get_option ( 'jcm_option_css ' ) === false ? 'checked="checked" ' : checked ( 'default ' , get_option ( 'jcm_option_css ' ) ); ?> />
169+ <label for="jcm_option_css_default">プラグイン付属のCSSを使う</label>
170+ <input id="jcm_option_css_custom" name="jcm_option_css" type="radio" value="custom" <?php checked ( 'custom ' , get_option ( 'jcm_option_css ' ) ); ?> />
171+ <label for="jcm_option_css_custom">オリジナルCSSを使う</label>
172+ <div class="jcm_option_css_custom">
173+ <p>表示されるメッセージは<code>#jcm_content_message</code>でラップされています。</p>
174+ <p>記述されたCSSはstyleタグに括られて出力されます。</p>
175+ <textarea name="jcm_option_css_custom" class="large-text code" rows="3"><?php echo get_option ( 'jcm_option_css_custom ' ) === false ? '#jcm_content_message{} ' : get_option ( 'jcm_option_css_custom ' ); ?> </textarea>
176+ </div>
177+ </fieldset>
178+ <?php
179+ }
0 commit comments