PluginProbe
codoc / 0.9.7
codoc v0.9.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.7, at class-codoc.php

621 lines 32.6 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();
13
14 # the_content フィルタを実行しない状�
15
16 $this->do_not_filter_the_content = false;
17
18 // paywall用の本文非表示化とcodocタグへの属性追加
19 add_filter('the_content',[$this,'the_content']);
20
21 $auth_info = get_option(CODOC_AUTHINFO_OPTION_NAME);
22
23 if ($auth_info) {
24 // データ同期 (save_postはis_adminで実行されないケースがある)
25 add_action( 'save_post', [$this,'save_post'], 20, 3 );
26 add_action( 'added_post_meta', [$this,'updated_post_meta'], 10, 3 );
27 add_action( 'updated_post_meta',[$this,'updated_post_meta'], 10, 3 );
28 add_action( 'deleted_post_meta',[$this,'deleted_post_meta'], 10, 4 );
29 }
30 if (is_admin() and $auth_info) { //管理画面
31 // Gutenberg のサポートがない場合は何もしない
32 if ( function_exists( 'register_block_type' ) ) {
33 // gutenberg
34 require_once 'src/init.php';
35 add_action('wp_loaded', function() {
36 wp_localize_script('wordpress-cgb-block-js', 'OPTIONS', array(
37 'codoc_url' => CODOC_URL,
38 'codoc_usercode' => get_option(CODOC_USERCODE_OPTION_NAME),
39 'codoc_plugin_version' => CODOC_PLUGIN_VERSION,
40 'codoc_sdk_path' => CODOC_SDK_PATH,
41 ));
42 });
43 }
44 // tinymce
45 $this->add_mce();
46 } else { // ブログ画面
47 // codocのJS登録
48 add_action( 'wp_enqueue_scripts', function() {
49 wp_enqueue_script( 'codoc-injector-js', CODOC_URL . '/js/cms.js' );
50 });
51 // 登録したscriptタグに属性をつける
52 add_filter('script_loader_tag', [$this,'modifier_script_tag'],10,2);
53 // tinymce用のショートコード
54 add_shortcode('codoc', [$this, 'injector_shortcode']);
55 }
56 return $this;
57 }
58
59 public function modifier_script_tag($tag,$handle) {
60 if($handle !== 'codoc-injector-js') {
61 return $tag;
62 }
63 $CODOC_SETTINGS = get_option(CODOC_SETTINGS_OPTION_NAME);
64 $data_css = '';
65 if ($css_path = $CODOC_SETTINGS['css_path']) {
66 $data_css = sprintf(' data-css="%s" ',$css_path);
67 }
68 //return str_replace(' src=', $data_css . ' defer src=', $tag);
69 return preg_replace('/(src=[^>]+)/',' ${1} ' . $data_css . ' defer',$tag);
70 }
71 public function injector_shortcode($atts) {
72 global $post;
73 ob_start();
74 require 'views/codoc-injector.php';
75 return ob_get_clean();
76 }
77 # settings / 設定関連
78 public function add_settings() {
79 global $CODOC_SETTINGS;
80 add_action( 'admin_menu' ,function(){
81 add_options_page(
82 'codoc の設定', //ページタイトル
83 'codoc', //設定メニューに表示されるメニュータイトル
84 'edit_users', //権限
85 'codoc', //設定ページのURL。options-general.php?page=codoc
86 function() {
87 echo '<div clas="wrap">';
88 echo '<form method="post" action="options.php">';
89 settings_fields( 'codoc_option_group' );
90 do_settings_sections( 'codoc' );
91 submit_button(); // 送信ボタン
92 echo '</form></div>';
93 }
94 );
95 });
96
97 add_action( "admin_init", function() {
98 // codocからの認証データ処理
99 if (isset($_GET['page']) and $_GET['page'] == 'codoc' and isset($_GET['fetch_token_key'])) {
100 $key = sanitize_text_field($_GET['fetch_token_key']);
101 $usercode = sanitize_text_field($_GET['usercode']);
102 update_option(CODOC_USERCODE_OPTION_NAME,$usercode);
103 //$data = $this->callAPI('GET','/token',[ "fetch_token_key" => $key ]);
104 $data = $this->util->get_token([ "fetch_token_key" => $key ],["usercode" => $usercode, "token" => "1"]);
105 if ($data->status and $token = $data->token) {
106 update_option(CODOC_TOKEN_OPTION_NAME,$token);
107 //$data = $this->callAPI('GET','');
108 $data = $this->util->get_user_info([],["usercode" => $usercode, "token" => $token]);
109 if ($data->status and $user = $data->user) {
110 update_option(CODOC_AUTHINFO_OPTION_NAME,[
111 'email' => $user->email,
112 'name' => $user->name,
113 ]);
114 }
115 #$current_url = (empty($_SERVER['HTTPS']) ? 'http://' : 'https://').$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
116 #$current_url = preg_replace('/(.*)fetch_token_key.*/','${1}&codoc_auth_finished=1',$current_url);
117 //add_settings_error( 'general', 'settings_updated', __( 'OK' ), 'success' );
118 $current_url = admin_url('options-general.php') . '?page=codoc&codoc_auth_finished=1';
119
120 wp_redirect( $current_url);
121 exit;
122 }
123 }
124 global $CODOC_USERCODE;
125 global $CODOC_SETTINGS;
126 global $CODOC_TOKEN;
127 global $CODOC_AUTHINFO;
128 $CODOC_USERCODE = get_option(CODOC_USERCODE_OPTION_NAME);
129 $CODOC_SETTINGS = get_option(CODOC_SETTINGS_OPTION_NAME);
130 $CODOC_TOKEN = get_option(CODOC_TOKEN_OPTION_NAME);
131 $CODOC_AUTHINFO = get_option(CODOC_AUTHINFO_OPTION_NAME);
132 if( !$CODOC_SETTINGS ) {
133 //デフォルト値
134 $CODOC_SETTINGS = array(
135 'css_path' => '',
136 );
137 update_option( CODOC_SETTINGS_OPTION_NAME, $CODOC_SETTINGS );
138 }
139 if (!isset($CODOC_SETTINGS['str_replace_binded_url_from'])) {
140 $CODOC_SETTINGS['str_replace_binded_url_from'] = '';
141 }
142 if (!isset($CODOC_SETTINGS['str_replace_binded_url_to'])) {
143 $CODOC_SETTINGS['str_replace_binded_url_to'] = '';
144 }
145 if (!isset($CODOC_SETTINGS['str_before_codoc_tag'])) {
146 $CODOC_SETTINGS['str_before_codoc_tag'] = '';
147 }
148 if (!isset($CODOC_SETTINGS['str_after_codoc_tag'])) {
149 $CODOC_SETTINGS['str_after_codoc_tag'] = '';
150 }
151
152 if (!isset($CODOC_SETTINGS['always_show_support'])) {
153 $CODOC_SETTINGS['always_show_support'] = '0';
154 }
155 if (!isset($CODOC_SETTINGS['show_support_message'])) {
156 $CODOC_SETTINGS['show_support_message'] = '';
157 }
158 if (!isset($CODOC_SETTINGS['show_support_categories'])) {
159 $CODOC_SETTINGS['show_support_categories'] = '';
160 }
161 if (!isset($CODOC_SETTINGS['show_support_location'])) {
162 $CODOC_SETTINGS['show_support_location'] = 'bottom';
163 }
164 if (!isset($CODOC_SETTINGS['do_not_filter_the_content'])) {
165 $CODOC_SETTINGS['do_not_filter_the_content'] = '0';
166 }
167 add_settings_section(
168 'setting_section_id', // id
169 'codoc 設定ページ', // title
170 function (){}, // callback
171 'codoc' // page
172 );
173 // 認証がある場合
174 if ($CODOC_AUTHINFO) {
175 register_setting(
176 'codoc_option_group', // option group
177 CODOC_SETTINGS_OPTION_NAME // option name(DB)
178 );
179 add_settings_field(
180 'css_path', // id
181 'テーマ', // title
182 function() {
183 global $CODOC_SETTINGS;
184 echo sprintf('<input type="text" name="%s[css_path]" value="%s" id="codoc_css_path" readonly>',CODOC_SETTINGS_OPTION_NAME,$CODOC_SETTINGS['css_path']);
185 echo '' .
186 '<script type="text/javascript"> ' .
187 ' function gen_css_path(select) { ' .
188 ' if (select.value == "path") { ' .
189 ' document.getElementById("codoc_css_path").readOnly=false; ' .
190 ' if (!document.getElementById("codoc_css_path").value.match(/\//g)) {' .
191 ' document.getElementById("codoc_css_path").value=""; ' .
192 ' } ' .
193 ' } else { ' .
194 ' document.getElementById("codoc_css_path").readOnly=true; ' .
195 ' document.getElementById("codoc_css_path").value=select.value; ' .
196 ' } ' .
197 ' } ' .
198 '</script> ' ;
199
200 echo sprintf('<select name="theme_name" onChange="gen_css_path(this)" id="codoc_theme_select">');
201 foreach (["blue","red","green","black","dark"] as $theme) {
202 if ($theme == "blue") {
203 echo sprintf('<option value="%s" %s>青</option>', $theme, ($CODOC_SETTINGS["css_path"] == "blue" ? "selected" : ""));
204 }
205 if ($theme == "red") {
206 echo sprintf('<option value="%s" %s>赤</option>', $theme, ($CODOC_SETTINGS["css_path"] == "red" ? "selected" : ""));
207 }
208 if ($theme == "green") {
209 echo sprintf('<option value="%s" %s>緑</option>', $theme, ($CODOC_SETTINGS["css_path"] == "green" ? "selected" : ""));
210 }
211 if ($theme == "black") {
212 echo sprintf('<option value="%s" %s>黒</option>', $theme, ($CODOC_SETTINGS["css_path"] == "black" ? "selected" : ""));
213 }
214 if ($theme == "dark") {
215 echo sprintf('<option value="%s" %s>ダークモード</option>', $theme, ($CODOC_SETTINGS["css_path"] == "dark" ? "selected" : ""));
216 }
217 }
218 echo sprintf('<option value="path" %s>パス指定</option>', (preg_match("/\//",$CODOC_SETTINGS["css_path"]) ? "selected" : ""));
219 echo '<script type="text/javascript">gen_css_path(document.getElementById(\'codoc_theme_select\'))</script>';
220 echo sprintf('</select>');
221 echo '<p>パスを指定を選択することでCSSを直接指定するこもと可能です</p>';
222 },
223 'codoc', //page
224 'setting_section_id' //Section
225 );
226 add_settings_field(
227 'str_replace_binded_url', // id
228 'codoc側に登録するパーマリンクを置換', // title
229 function() {
230 global $CODOC_SETTINGS;
231 echo sprintf('<input type="text" name="%s[str_replace_binded_url_from]" value="%s">',CODOC_SETTINGS_OPTION_NAME,$CODOC_SETTINGS['str_replace_binded_url_from']);
232 echo ('');
233 echo sprintf('<input type="text" name="%s[str_replace_binded_url_to]" value="%s">',CODOC_SETTINGS_OPTION_NAME,$CODOC_SETTINGS['str_replace_binded_url_to']);
234 echo ('に変換');
235 },
236 'codoc', //page
237 'setting_section_id' //Section
238 );
239
240 add_settings_field(
241 'str_around_codoc_tag', // id
242 'codocタグの前後にHTMLを挿�
243 �', // title
244 function() {
245 global $CODOC_SETTINGS;
246 echo sprintf('<textarea name="%s[str_before_codoc_tag]" cols="40">%s</textarea>',CODOC_SETTINGS_OPTION_NAME,$CODOC_SETTINGS['str_before_codoc_tag']);
247 echo ('を前に挿�
248 �<br />');
249 echo sprintf('<textarea name="%s[str_after_codoc_tag]" cols="40">%s</textarea>',CODOC_SETTINGS_OPTION_NAME,$CODOC_SETTINGS['str_after_codoc_tag']);
250 echo ('を後に挿�
251 �');
252
253 },
254 'codoc', //page
255 'setting_section_id' //Section
256 );
257
258 add_settings_field(
259 'always_show_support_tag', // id
260 'サポート用のボタンを記事ページに自動挿�
261 �', // title
262 function() {
263 global $CODOC_SETTINGS;
264 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" : ""));
265 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" : ""));
266 echo ('表示位置 ');
267 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" : ""));
268 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" : ""));
269
270 echo sprintf('説明文 <input type="text" placeholder="サポートの説明文をカスタマイズできます" size="50%%" name="%s[show_support_message]" value="%s"><br />',CODOC_SETTINGS_OPTION_NAME,($CODOC_SETTINGS['show_support_message']));
271 echo sprintf('対象カテゴリ名 <input placeholder="&quot;|&quot;区切りで複数指定できます" type="text" size="50%%" name="%s[show_support_categories]" value="%s"><br />',CODOC_SETTINGS_OPTION_NAME,($CODOC_SETTINGS['show_support_categories']));
272
273 },
274 'codoc', //page
275 'setting_section_id' //Section
276 );
277 add_settings_field(
278 'do_not_filter_the_content', // id
279 'コンテンツに他のプラグインによるフィルター処理をしない', // title
280 function() {
281 global $CODOC_SETTINGS;
282 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" : ""));
283 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> <br / >',CODOC_SETTINGS_OPTION_NAME,($CODOC_SETTINGS['do_not_filter_the_content'] == '0' ? "checked" : ""));
284 },
285 'codoc', //page
286 'setting_section_id' //Section
287 );
288
289
290 register_setting(
291 'codoc_option_group', // option group
292 CODOC_USERCODE_OPTION_NAME
293 );
294 register_setting(
295 'codoc_option_group', // option group
296 CODOC_TOKEN_OPTION_NAME
297 );
298
299 if (isset($_GET['codoc_auth_finished']) and $_GET['codoc_auth_finished']) {
300 add_settings_error( 'general', 'settings_updated', __( 'codocの認証が完了しました' ), 'success' );
301 }
302
303 add_settings_field(
304 'codoc_auth',
305 '認証',
306 function() {
307 global $CODOC_USERCODE;
308 global $CODOC_TOKEN;
309 global $CODOC_AUTHINFO;
310 $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';";
311
312 echo sprintf('<p><font color="green" id="codoc-auth-message"><strong>%s</strong> で認証済</font></p>',$CODOC_AUTHINFO['email']);
313
314 echo sprintf('<input id="codoc-usercode" type="hidden" name="%s" value="%s">',CODOC_USERCODE_OPTION_NAME,$CODOC_USERCODE);
315 echo sprintf('<input id="codoc-token" type="hidden" name="%s" value="%s">',CODOC_TOKEN_OPTION_NAME,$CODOC_TOKEN);
316 echo sprintf('<p>認証を<a href="javascript:void(0);" onClick="' . $script . '">解除する</p>');
317 },
318 'codoc',
319 'setting_section_id'
320 );
321
322 }
323
324 // ない場合
325 if (!$CODOC_AUTHINFO and !isset($_GET['auth_by_myself'])) {
326 add_settings_field(
327 'codoc_auth',
328 '認証',
329 function() {
330
331 //$current_url = (empty($_SERVER['HTTPS']) ? 'http://' : 'https://').$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
332 $current_url = admin_url('options-general.php') . '?page=codoc';
333 $login_url = sprintf("location.href='%s'",CODOC_URL . '/me/token?from=wp&return_url=' . urlencode($current_url));
334 $register_url = sprintf("location.href='%s'",CODOC_URL . '/register?from=wp&return_url=' . urlencode($current_url));
335 // submitを消しておく
336 echo ('<script type="text/javascript">window.onload=function(){document.getElementById(\'submit\').style.display = \'none\'}</script>');
337 echo ('codocをWordPressでご利用いただくには認証が�
338 要です。<br />');
339 echo sprintf('ユーザーコードとAPIトークンを<a href="%s&auth_by_myself=1">直接�
340 �力</a>することでも認証できます。<br /><br />',$current_url);
341 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);
342
343
344 },
345 'codoc',
346 'setting_section_id'
347 );
348 }
349 // ない場合かつ自分で認証する場合
350 if (!$CODOC_AUTHINFO and (
351 (isset($_GET['auth_by_myself']) and $_GET['auth_by_myself']) or
352 (isset($_POST['auth_by_myself']) and $_POST['auth_by_myself'])
353 )) {
354 register_setting(
355 'codoc_option_group', // option group
356 CODOC_USERCODE_OPTION_NAME
357 );
358 register_setting(
359 'codoc_option_group', // option group
360 CODOC_TOKEN_OPTION_NAME
361 );
362 add_settings_field(
363 'codoc_usercode',
364 'ユーザーコード',
365 function() {
366 global $CODOC_USERCODE;
367 echo '<input type="hidden" name="auth_by_myself" value="1">';
368 echo sprintf('<input type="text" name="%s" value="%s">',CODOC_USERCODE_OPTION_NAME,$CODOC_USERCODE);
369 },
370 'codoc',
371 'setting_section_id'
372 );
373 add_settings_field(
374 'codoc_token',
375 'APIトークン',
376 function() {
377 global $CODOC_TOKEN;
378 echo sprintf('<input type="text" name="%s" value="%s">',CODOC_TOKEN_OPTION_NAME,$CODOC_TOKEN);
379 },
380 'codoc',
381 'setting_section_id'
382 );
383 }
384
385 // 認証�
386 報を保存
387 add_action( 'update_option_' . CODOC_USERCODE_OPTION_NAME, function( $old_value, $new_value ) {
388 global $CODOC_RE_AUTHORIZE;
389 $CODOC_RE_AUTHORIZE = 1;
390 },9,2); // $hook, $function_to_add, $priority, $accepted_args
391 add_action( 'update_option_' . CODOC_TOKEN_OPTION_NAME, function( $old_value, $new_value ) {
392 global $CODOC_RE_AUTHORIZE;
393 $CODOC_RE_AUTHORIZE = 1;
394 },10,2);
395
396 add_action('update_option_' . CODOC_SETTINGS_OPTION_NAME,function(){
397 $CODOC_SETTINGS = get_option(CODOC_SETTINGS_OPTION_NAME);
398 if (isset($CODOC_SETTINGS['always_show_support']) and $CODOC_SETTINGS['always_show_support']) {
399 $data = $this->util->get_support_entry([],[
400 "usercode" => get_option(CODOC_USERCODE_OPTION_NAME),
401 "token" => get_option(CODOC_TOKEN_OPTION_NAME),
402 ]);
403 if ($data->status and $entry = $data->entry) {
404 update_option(CODOC_SUPPORT_ENTRYCODE_OPTION_NAME,$entry->code);
405 } else {
406 update_option(CODOC_SUPPORT_ENTRYCODE_OPTION_NAME,'');
407 }
408 } elseif(isset($CODOC_SETTINGS['always_show_support']) and !$CODOC_SETTINGS['always_show_support']) {
409 update_option(CODOC_SUPPORT_ENTRYCODE_OPTION_NAME,'');
410 }
411 });
412
413 add_action('updated_option',function(){
414 global $CODOC_RE_AUTHORIZE;
415 if ($CODOC_RE_AUTHORIZE) {
416 //$data = $this->callAPI('GET','');
417 $data = $this->util->get_user_info([],[
418 "usercode" => get_option(CODOC_USERCODE_OPTION_NAME),
419 "token" => get_option(CODOC_TOKEN_OPTION_NAME),
420 ]);
421 if ($data->status and $user = $data->user) {
422 update_option(CODOC_AUTHINFO_OPTION_NAME,[
423 'email' => $user->email,
424 'name' => $user->name,
425 ]);
426 } else {
427 update_option(CODOC_AUTHINFO_OPTION_NAME,'');
428 }
429 }
430 });
431
432 });
433 // プラグイン一覧に設定のリンクをいれる
434 add_filter( 'plugin_action_links_' . plugin_basename( plugin_dir_path( __FILE__ ) . 'codoc' . '.php' ),
435 function( $links ) {
436 $setting_link = sprintf( '<a href="%s">%s</a>', esc_url( add_query_arg( 'page', 'codoc', admin_url( 'options-general.php' ) ) ), esc_html( '設定' ) );
437 array_unshift( $links, $setting_link );
438
439 return $links;
440 }
441 );
442
443 }
444 public function add_mce() {
445 add_action( 'admin_enqueue_scripts', function() {
446 if ( ! current_user_can( 'edit_posts' ) && ! current_user_can( 'edit_pages' ) ) {
447 return;
448 }
449 $current_screen = get_current_screen();
450 if ( ( method_exists( $current_screen, 'is_block_editor' ) && $current_screen->is_block_editor() ) || ( function_exists( 'is_gutenberg_page' ) && is_gutenberg_page() ) ) {
451 // Gutenberg Editor
452 // なにもしない
453 } else {
454 // tinymce
455 if ( get_user_option( 'rich_editing' ) == 'true' ) {
456 // プラグイン�
457 で使うCSRF を生成
458 $path = plugins_url( 'codoc/src_mce/codoc-editor-onload.js', __DIR__ );
459 wp_enqueue_script( 'codoc-editor-onload', $path, array('jquery'), '', true );
460 // I just want to insert this global variables but i don't know how i can do it..
461 wp_localize_script(
462 'codoc-editor-onload',
463 'CODOCEDITOR',
464 array(
465 'action' => 'codoc_shortcodes',
466 'nonce' => wp_create_nonce( 'codoc_shortcodes' ),
467
468 'codoc_url' => CODOC_URL,
469 'codoc_usercode' => get_option(CODOC_USERCODE_OPTION_NAME),
470 'codoc_plugin_version' => CODOC_PLUGIN_VERSION,
471 'codoc_sdk_path' => CODOC_SDK_PATH,
472 )
473 );
474
475 // ここでtinymceのプラグイン追加
476 //wp_enqueue_style( 'codoc-admin-style', plugins_url( 'codoc/src_mce/codoc-admin.css', __DIR__ ) );
477 //add_editor_style( plugins_url( 'codoc/src_mce/codoc-admin.css', __DIR__ ) );
478 wp_enqueue_style( 'codoc-admin-style', CODOC_URL . CODOC_SDK_PATH . '.tinymce.css?c=' . date('Ymd') );
479 add_editor_style( CODOC_URL . CODOC_SDK_PATH . '.tinymce.css' );
480
481 add_filter( 'mce_external_plugins', function( $plugin_array ) {
482 //$plugin_array['codoc'] = plugins_url( 'codoc/src_mce/codoc-editor.js', __DIR__ );
483 $plugin_array['codoc'] = CODOC_URL . CODOC_SDK_PATH . '.tinymce.js?c=' . date('Ymd');
484 return $plugin_array;
485 } );
486 add_filter( 'mce_buttons', function( $buttons ) {
487 array_push( $buttons, "|", "codoc" );
488 return $buttons;
489 } );
490
491 }
492 }
493 } );
494 }
495 public function get_filtered_content($post_emulated) {
496 // 記事ページであることをエミュレートしてフィルター実行
497 $post_backuped = null;
498 if (isset($GLOBALS['post'])) {
499 $post_backuped = $GLOBALS['post'];
500 }
501 $GLOBALS['post'] = $post_emulated;
502
503 $wp_query_backuped = null;
504 if (isset($GLOBALS['wp_query'])) {
505 $wp_query_backuped = $GLOBALS['wp_query'];
506 $wp_query = $GLOBALS['wp_query'];
507 $is_single_backuped = $wp_query->is_single;
508 $wp_query->is_single = true;
509 $wp_query->post = $post_emulated;
510 $wp_query->queried_object = $post_emulated;
511 $wp_query->queried_object_id = $post_emulated->id;
512
513 $GLOBALS['wp_query'] = $wp_query;
514 }
515
516 $this->do_not_filter_the_content = true;
517 $content = preg_replace(
518 '/ (\/)?wp:codoc\/codoc-block /',' \\1wptmp:codoc/codoc-block ',
519 $post_emulated->post_content
520 );
521 #$content = do_shortcode( $content );
522 $content_filtered = apply_filters( 'the_content', $content);
523 $this->do_not_filter_the_content = false;
524
525 $GLOBALS['post'] = $post_backuped;
526
527 if ($wp_query_backuped) {
528 $GLOBALS['wp_query'] = $wp_query_backuped;
529 }
530
531 $content_filtered = preg_replace(
532 '/ (\/)?wptmp:codoc\/codoc-block /',' \\1wp:codoc/codoc-block ',
533 $content_filtered
534 );
535
536 return $content_filtered;
537 }
538
539 public function save_post($post_ID,$post,$update) {
540 if (preg_match('/^(auto-draft|inherit)$/',$post->post_status)) {
541 return;
542 }
543 $entryCode = get_post_meta($post_ID,'codoc_entry_code',true);
544 $codoc_settings = get_option(CODOC_SETTINGS_OPTION_NAME);
545 $post_content = (isset($codoc_settings['do_not_filter_the_content']) and $codoc_settings['do_not_filter_the_content']) ?
546 $post->post_content : $this->get_filtered_content($post);
547 $res = $this->util->sync_entry([
548 "post_title" => $post->post_title,
549 "post_content" => $post_content,
550 "post_status" => ($post->post_status == 'publish' ? 1 : 0),
551 "post_permalink" => get_permalink($post_ID),
552 "codoc_entry_code" => $entryCode,
553 "codoc_settings" => $codoc_settings,
554 ],[
555 "usercode" => get_option(CODOC_USERCODE_OPTION_NAME),
556 "token" => get_option(CODOC_TOKEN_OPTION_NAME),
557 ]);
558
559 if (is_object($res) and $res->status and !$entryCode) {
560 update_post_meta($post_ID,'codoc_entry_code',$res->entry->code);
561 }
562
563 return true;
564 }
565 function updated_post_meta( $meta_ID, $post_ID, $meta_key ) {
566 if ($meta_key != '_thumbnail_id') {
567 return;
568 }
569 if ( has_post_thumbnail($post_ID) ) {
570 $attachment = wp_get_attachment_metadata( get_post_thumbnail_id($post_ID));
571 $upload_dir = wp_upload_dir();
572 $file_path = sprintf ('%s/%s',$upload_dir['basedir'],$attachment['file']);
573
574 return $this->util->post_thumbnail([
575 "file_path" => $file_path,
576 "boundary" => wp_generate_password(24),
577 "codoc_entry_code" => get_post_meta($post_ID,'codoc_entry_code',true),
578 ],[
579 "usercode" => get_option(CODOC_USERCODE_OPTION_NAME),
580 "token" => get_option(CODOC_TOKEN_OPTION_NAME),
581 ]);
582 }
583 }
584 function deleted_post_meta( $meta_ids, $post_ID, $meta_key,$meta_value ) {
585 if ($meta_key != '_thumbnail_id') {
586 return;
587 }
588 return $this->util->reset_thumbnail(
589 ["codoc_entry_code" => get_post_meta($post_ID,'codoc_entry_code',true)],
590 [
591 "usercode" => get_option(CODOC_USERCODE_OPTION_NAME),
592 "token" => get_option(CODOC_TOKEN_OPTION_NAME),
593 ]
594 );
595 }
596
597 function the_content( $post_content ) {
598 if ( is_single() && in_the_loop() && is_main_query() ) {
599 }
600 if ($this->do_not_filter_the_content) {
601 return $post_content;
602 }
603 $post = get_post();
604 # is_amp で実�
605 しているテンプレート用
606 $is_amp_endpoint = (function_exists('is_amp_endpoint') && is_amp_endpoint()) ? true :
607 ((function_exists('is_amp') && is_amp()) ? true : false);
608 return $this->util->filter_content([
609 "post_content" => $post_content,
610 "preview" => is_preview(),
611 "codoc_entry_code" => get_post_meta($post->ID,'codoc_entry_code',true),
612 "codoc_settings" => get_option(CODOC_SETTINGS_OPTION_NAME),
613 "is_amp_endpoint" => $is_amp_endpoint,
614 "post_permalink" => get_permalink($post->ID),
615 "codoc_support_entry_code" => get_option(CODOC_SUPPORT_ENTRYCODE_OPTION_NAME),
616 ]);
617 }
618
619 }
620
621