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