| 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 class="codoc-settings">'; |
| 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_print_styles', 'admin_styles'); |
| 98 |
function admin_styles($hook) { |
| 99 |
wp_enqueue_style('codoc-options-style', plugins_url( 'codoc/css/codoc-options.css', __DIR__ )); |
| 100 |
} |
| 101 |
|
| 102 |
add_action( "admin_init", function() { |
| 103 |
// codocからの認証データ処理 |
| 104 |
if (isset($_GET['page']) and $_GET['page'] == 'codoc' and isset($_GET['fetch_token_key'])) { |
| 105 |
$key = sanitize_text_field($_GET['fetch_token_key']); |
| 106 |
$usercode = sanitize_text_field($_GET['usercode']); |
| 107 |
update_option(CODOC_USERCODE_OPTION_NAME,$usercode); |
| 108 |
//$data = $this->callAPI('GET','/token',[ "fetch_token_key" => $key ]); |
| 109 |
$data = $this->util->get_token([ "fetch_token_key" => $key ],["usercode" => $usercode, "token" => "1"]); |
| 110 |
if ($data->status and $token = $data->token) { |
| 111 |
update_option(CODOC_TOKEN_OPTION_NAME,$token); |
| 112 |
//$data = $this->callAPI('GET',''); |
| 113 |
$data = $this->util->get_user_info([],["usercode" => $usercode, "token" => $token]); |
| 114 |
if ($data->status and $user = $data->user) { |
| 115 |
update_option(CODOC_AUTHINFO_OPTION_NAME,[ |
| 116 |
'email' => $user->email, |
| 117 |
'name' => $user->name, |
| 118 |
]); |
| 119 |
} |
| 120 |
#$current_url = (empty($_SERVER['HTTPS']) ? 'http://' : 'https://').$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; |
| 121 |
#$current_url = preg_replace('/(.*)fetch_token_key.*/','${1}&codoc_auth_finished=1',$current_url); |
| 122 |
//add_settings_error( 'general', 'settings_updated', __( 'OK' ), 'success' ); |
| 123 |
$current_url = admin_url('options-general.php') . '?page=codoc&codoc_auth_finished=1'; |
| 124 |
|
| 125 |
wp_redirect( $current_url); |
| 126 |
exit; |
| 127 |
} |
| 128 |
} |
| 129 |
global $CODOC_USERCODE; |
| 130 |
global $CODOC_SETTINGS; |
| 131 |
global $CODOC_TOKEN; |
| 132 |
global $CODOC_AUTHINFO; |
| 133 |
$CODOC_USERCODE = get_option(CODOC_USERCODE_OPTION_NAME); |
| 134 |
$CODOC_SETTINGS = get_option(CODOC_SETTINGS_OPTION_NAME); |
| 135 |
$CODOC_TOKEN = get_option(CODOC_TOKEN_OPTION_NAME); |
| 136 |
$CODOC_AUTHINFO = get_option(CODOC_AUTHINFO_OPTION_NAME); |
| 137 |
if( !$CODOC_SETTINGS ) { |
| 138 |
//デフォルト値 |
| 139 |
$CODOC_SETTINGS = array( |
| 140 |
'css_path' => '', |
| 141 |
); |
| 142 |
update_option( CODOC_SETTINGS_OPTION_NAME, $CODOC_SETTINGS ); |
| 143 |
} |
| 144 |
if (!isset($CODOC_SETTINGS['str_replace_binded_url_from'])) { |
| 145 |
$CODOC_SETTINGS['str_replace_binded_url_from'] = ''; |
| 146 |
} |
| 147 |
if (!isset($CODOC_SETTINGS['str_replace_binded_url_to'])) { |
| 148 |
$CODOC_SETTINGS['str_replace_binded_url_to'] = ''; |
| 149 |
} |
| 150 |
if (!isset($CODOC_SETTINGS['str_before_codoc_tag'])) { |
| 151 |
$CODOC_SETTINGS['str_before_codoc_tag'] = ''; |
| 152 |
} |
| 153 |
if (!isset($CODOC_SETTINGS['str_after_codoc_tag'])) { |
| 154 |
$CODOC_SETTINGS['str_after_codoc_tag'] = ''; |
| 155 |
} |
| 156 |
|
| 157 |
if (!isset($CODOC_SETTINGS['always_show_support'])) { |
| 158 |
$CODOC_SETTINGS['always_show_support'] = '0'; |
| 159 |
} |
| 160 |
if (!isset($CODOC_SETTINGS['show_support_message'])) { |
| 161 |
$CODOC_SETTINGS['show_support_message'] = ''; |
| 162 |
} |
| 163 |
if (!isset($CODOC_SETTINGS['show_support_categories'])) { |
| 164 |
$CODOC_SETTINGS['show_support_categories'] = ''; |
| 165 |
} |
| 166 |
if (!isset($CODOC_SETTINGS['show_support_location'])) { |
| 167 |
$CODOC_SETTINGS['show_support_location'] = 'bottom'; |
| 168 |
} |
| 169 |
if (!isset($CODOC_SETTINGS['do_not_filter_the_content'])) { |
| 170 |
$CODOC_SETTINGS['do_not_filter_the_content'] = '0'; |
| 171 |
} |
| 172 |
|
| 173 |
if (!isset($CODOC_SETTINGS['entry_button_text'])) { |
| 174 |
$CODOC_SETTINGS['entry_button_text'] = ''; |
| 175 |
} |
| 176 |
if (!isset($CODOC_SETTINGS['subscription_button_text'])) { |
| 177 |
$CODOC_SETTINGS['subscription_button_text'] = ''; |
| 178 |
} |
| 179 |
if (!isset($CODOC_SETTINGS['support_button_text'])) { |
| 180 |
$CODOC_SETTINGS['support_button_text'] = ''; |
| 181 |
} |
| 182 |
if (!isset($CODOC_SETTINGS['subscription_message'])) { |
| 183 |
$CODOC_SETTINGS['subscription_message'] = ''; |
| 184 |
} |
| 185 |
if (!isset($CODOC_SETTINGS['support_message'])) { |
| 186 |
$CODOC_SETTINGS['support_message'] = ''; |
| 187 |
} |
| 188 |
if (!isset($CODOC_SETTINGS['show_like'])) { |
| 189 |
$CODOC_SETTINGS['show_like'] = '1'; |
| 190 |
} |
| 191 |
if (!isset($CODOC_SETTINGS['show_powered_by'])) { |
| 192 |
$CODOC_SETTINGS['show_powered_by'] = '1'; |
| 193 |
} |
| 194 |
|
| 195 |
if (!isset($CODOC_SETTINGS['codoc_tag_attributes'])) { |
| 196 |
$CODOC_SETTINGS['codoc_tag_attributes'] = ''; |
| 197 |
} |
| 198 |
|
| 199 |
add_settings_section( |
| 200 |
'setting_section_id', // id |
| 201 |
'codoc 設定ページ', // title |
| 202 |
function (){}, // callback |
| 203 |
'codoc' // page |
| 204 |
); |
| 205 |
// 認証がある場合 |
| 206 |
if ($CODOC_AUTHINFO) { |
| 207 |
register_setting( |
| 208 |
'codoc_option_group', // option group |
| 209 |
CODOC_SETTINGS_OPTION_NAME // option name(DB) |
| 210 |
); |
| 211 |
add_settings_field( |
| 212 |
'css_path', // id |
| 213 |
'テーマ', // title |
| 214 |
function() { |
| 215 |
global $CODOC_SETTINGS; |
| 216 |
echo '<div class="excerpt">テーマを指定してペイウォールのデザインを変えることができます。<br>パスを指定を選択することでCSSを直接指定するこもと可能です。</div>'; |
| 217 |
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']); |
| 218 |
echo '' . |
| 219 |
'<script type="text/javascript"> ' . |
| 220 |
' function gen_css_path(select) { ' . |
| 221 |
' if (select.value == "path") { ' . |
| 222 |
' document.getElementById("codoc_css_path").readOnly=false; ' . |
| 223 |
' if (!document.getElementById("codoc_css_path").value.match(/\//g)) {' . |
| 224 |
' document.getElementById("codoc_css_path").value=""; ' . |
| 225 |
' } ' . |
| 226 |
' } else { ' . |
| 227 |
' document.getElementById("codoc_css_path").readOnly=true; ' . |
| 228 |
' document.getElementById("codoc_css_path").value=select.value; ' . |
| 229 |
' } ' . |
| 230 |
' } ' . |
| 231 |
'</script> ' ; |
| 232 |
|
| 233 |
echo sprintf('<select name="theme_name" onChange="gen_css_path(this)" id="codoc_theme_select">'); |
| 234 |
foreach (["blue","red","green","black","dark"] as $theme) { |
| 235 |
if ($theme == "blue") { |
| 236 |
echo sprintf('<option value="%s" %s>青</option>', $theme, ($CODOC_SETTINGS["css_path"] == "blue" ? "selected" : "")); |
| 237 |
} |
| 238 |
if ($theme == "red") { |
| 239 |
echo sprintf('<option value="%s" %s>赤</option>', $theme, ($CODOC_SETTINGS["css_path"] == "red" ? "selected" : "")); |
| 240 |
} |
| 241 |
if ($theme == "green") { |
| 242 |
echo sprintf('<option value="%s" %s>緑</option>', $theme, ($CODOC_SETTINGS["css_path"] == "green" ? "selected" : "")); |
| 243 |
} |
| 244 |
if ($theme == "black") { |
| 245 |
echo sprintf('<option value="%s" %s>黒</option>', $theme, ($CODOC_SETTINGS["css_path"] == "black" ? "selected" : "")); |
| 246 |
} |
| 247 |
if ($theme == "dark") { |
| 248 |
echo sprintf('<option value="%s" %s>ダークモード</option>', $theme, ($CODOC_SETTINGS["css_path"] == "dark" ? "selected" : "")); |
| 249 |
} |
| 250 |
} |
| 251 |
echo sprintf('<option value="path" %s>パス指定</option>', (preg_match("/\//",$CODOC_SETTINGS["css_path"]) ? "selected" : "")); |
| 252 |
echo '<script type="text/javascript">gen_css_path(document.getElementById(\'codoc_theme_select\'))</script>'; |
| 253 |
echo sprintf('</select></div>'); |
| 254 |
}, |
| 255 |
'codoc', //page |
| 256 |
'setting_section_id' //Section |
| 257 |
); |
| 258 |
add_settings_field( |
| 259 |
'paywall_text', // id |
| 260 |
'各種文言', // title |
| 261 |
function() { |
| 262 |
global $CODOC_SETTINGS; |
| 263 |
echo '<div class="excerpt">ペイウォール� |
| 264 |
の一部文言を変更できます。</div>'; |
| 265 |
echo '<table class="innerTable"><tbody>'; |
| 266 |
echo '<tr><th>いいねの表示</th><td>'; |
| 267 |
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" : "")); |
| 268 |
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" : "")); |
| 269 |
echo '</td></tr>'; |
| 270 |
echo '<tr><th>PoweredByの表示</th><td>'; |
| 271 |
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" : "")); |
| 272 |
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" : "")); |
| 273 |
echo '</td></tr>'; |
| 274 |
|
| 275 |
echo '<tr><th>記事購� |
| 276 |
�ボタンのテキスト</th><td>'; |
| 277 |
echo sprintf('<input type="text" name="%s[entry_button_text]" value="%s">',CODOC_SETTINGS_OPTION_NAME,$CODOC_SETTINGS['entry_button_text']); |
| 278 |
echo '</td></tr>'; |
| 279 |
|
| 280 |
echo '<tr><th>サブスクリプション購� |
| 281 |
�ボタンのテキスト</th><td>'; |
| 282 |
echo sprintf('<input type="text" name="%s[subscription_button_text]" value="%s">',CODOC_SETTINGS_OPTION_NAME,$CODOC_SETTINGS['subscription_button_text']); |
| 283 |
echo '</td></tr>'; |
| 284 |
|
| 285 |
echo '<tr><th>サポートボタンのテキスト</th><td>'; |
| 286 |
echo sprintf('<input type="text" name="%s[support_button_text]" value="%s">',CODOC_SETTINGS_OPTION_NAME,$CODOC_SETTINGS['support_button_text']); |
| 287 |
echo '</td></tr>'; |
| 288 |
|
| 289 |
echo '<tr><th>「サブスクリプションに含まれる記事」のテキスト</th><td>'; |
| 290 |
echo sprintf('<input type="text" name="%s[subscription_message]" value="%s">',CODOC_SETTINGS_OPTION_NAME,$CODOC_SETTINGS['subscription_message']); |
| 291 |
echo '</td></tr>'; |
| 292 |
|
| 293 |
echo '<tr><th>サポート説明のテキスト</th><td>'; |
| 294 |
echo sprintf('<input type="text" name="%s[support_message]" value="%s">',CODOC_SETTINGS_OPTION_NAME,$CODOC_SETTINGS['support_message']); |
| 295 |
echo '</td></tr>'; |
| 296 |
|
| 297 |
echo '</tbody></table>'; |
| 298 |
}, |
| 299 |
'codoc', //page |
| 300 |
'setting_section_id' //Section |
| 301 |
); |
| 302 |
|
| 303 |
add_settings_field( |
| 304 |
'codoc_tag_attributes', // id |
| 305 |
'codocタグの属性', // title |
| 306 |
function() { |
| 307 |
global $CODOC_SETTINGS; |
| 308 |
echo '<div class="excerpt">codocタグに特定の属性を追加することができます。</div>'; |
| 309 |
echo sprintf('<input type="text" name="%s[codoc_tag_attributes]" value="%s">',CODOC_SETTINGS_OPTION_NAME,htmlspecialchars($CODOC_SETTINGS['codoc_tag_attributes'])); |
| 310 |
}, |
| 311 |
'codoc', //page |
| 312 |
'setting_section_id' //Section |
| 313 |
); |
| 314 |
|
| 315 |
add_settings_field( |
| 316 |
'str_replace_binded_url', // id |
| 317 |
'パーマリンク', // title |
| 318 |
function() { |
| 319 |
global $CODOC_SETTINGS; |
| 320 |
echo '<div class="excerpt">codoc側に登録するパーマリンクを置換することができます。</div>'; |
| 321 |
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']); |
| 322 |
echo ('<span class="suptext">を</span>'); |
| 323 |
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']); |
| 324 |
echo ('に変換'); |
| 325 |
}, |
| 326 |
'codoc', //page |
| 327 |
'setting_section_id' //Section |
| 328 |
); |
| 329 |
|
| 330 |
add_settings_field( |
| 331 |
'str_around_codoc_tag', // id |
| 332 |
'HTMLの挿� |
| 333 |
�', // title |
| 334 |
function() { |
| 335 |
global $CODOC_SETTINGS; |
| 336 |
echo '<div class="excerpt">codocタグの前後にHTMLを挿� |
| 337 |
�することができます。</div>'; |
| 338 |
echo sprintf('<div class="inputRow"><textarea name="%s[str_before_codoc_tag]" cols="40">%s</textarea>',CODOC_SETTINGS_OPTION_NAME,$CODOC_SETTINGS['str_before_codoc_tag']); |
| 339 |
echo ('を前に挿� |
| 340 |
�</div>'); |
| 341 |
echo sprintf('<div class="inputRow"><textarea name="%s[str_after_codoc_tag]" cols="40">%s</textarea>',CODOC_SETTINGS_OPTION_NAME,$CODOC_SETTINGS['str_after_codoc_tag']); |
| 342 |
echo ('を後に挿� |
| 343 |
�</div>'); |
| 344 |
|
| 345 |
}, |
| 346 |
'codoc', //page |
| 347 |
'setting_section_id' //Section |
| 348 |
); |
| 349 |
|
| 350 |
add_settings_field( |
| 351 |
'always_show_support_tag', // id |
| 352 |
'サポートの自動挿� |
| 353 |
�', // title |
| 354 |
function() { |
| 355 |
global $CODOC_SETTINGS; |
| 356 |
echo '<div class="excerpt">codocブロックを挿� |
| 357 |
�することなく投稿にサポート機能を追加します</div>'; |
| 358 |
echo '<table class="innerTable"><tbody>'; |
| 359 |
echo '<tr><th>自動挿� |
| 360 |
�</th><td>'; |
| 361 |
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" : "")); |
| 362 |
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" : "")); |
| 363 |
echo '</td></tr>'; |
| 364 |
echo '<tr><th>表示位置</th><td>'; |
| 365 |
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" : "")); |
| 366 |
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" : "")); |
| 367 |
echo '</td></tr>'; |
| 368 |
echo '<tr><th>説明文</th><td>'; |
| 369 |
echo sprintf('<input type="text" placeholder="サポートの説明文をカスタマイズできます" size="50%%" name="%s[show_support_message]" value="%s">',CODOC_SETTINGS_OPTION_NAME,($CODOC_SETTINGS['show_support_message'])); |
| 370 |
echo '</td></tr>'; |
| 371 |
echo '<tr><th>対象カテゴリ名</th><td>'; |
| 372 |
echo sprintf('<input placeholder=""|"区切りで複数指定できます" type="text" size="50%%" name="%s[show_support_categories]" value="%s"><br />',CODOC_SETTINGS_OPTION_NAME,($CODOC_SETTINGS['show_support_categories'])); |
| 373 |
echo '</td></tr>'; |
| 374 |
echo '</tbody></table>'; |
| 375 |
|
| 376 |
}, |
| 377 |
'codoc', //page |
| 378 |
'setting_section_id' //Section |
| 379 |
); |
| 380 |
add_settings_field( |
| 381 |
'do_not_filter_the_content', // id |
| 382 |
'支障が出る処理を無効化', // title |
| 383 |
function() { |
| 384 |
global $CODOC_SETTINGS; |
| 385 |
echo '<div class="excerpt">codocの動作に支障が出る他のプラグインによる<strong>フィルター処理</strong>を無効化できます。codocが上手く動かない場合のみ利用してください。</div>'; |
| 386 |
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" : "")); |
| 387 |
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" : "")); |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
}, |
| 394 |
'codoc', //page |
| 395 |
'setting_section_id' //Section |
| 396 |
); |
| 397 |
|
| 398 |
|
| 399 |
register_setting( |
| 400 |
'codoc_option_group', // option group |
| 401 |
CODOC_USERCODE_OPTION_NAME |
| 402 |
); |
| 403 |
register_setting( |
| 404 |
'codoc_option_group', // option group |
| 405 |
CODOC_TOKEN_OPTION_NAME |
| 406 |
); |
| 407 |
|
| 408 |
if (isset($_GET['codoc_auth_finished']) and $_GET['codoc_auth_finished']) { |
| 409 |
add_settings_error( 'general', 'settings_updated', __( 'codocの認証が完了しました' ), 'success' ); |
| 410 |
} |
| 411 |
|
| 412 |
add_settings_field( |
| 413 |
'codoc_auth', |
| 414 |
'認証', |
| 415 |
function() { |
| 416 |
global $CODOC_USERCODE; |
| 417 |
global $CODOC_TOKEN; |
| 418 |
global $CODOC_AUTHINFO; |
| 419 |
$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';"; |
| 420 |
|
| 421 |
echo sprintf('<p><font color="green" id="codoc-auth-message"><strong>%s</strong> で認証済</font></p>',$CODOC_AUTHINFO['email']); |
| 422 |
|
| 423 |
echo sprintf('<input id="codoc-usercode" type="hidden" name="%s" value="%s">',CODOC_USERCODE_OPTION_NAME,$CODOC_USERCODE); |
| 424 |
echo sprintf('<input id="codoc-token" type="hidden" name="%s" value="%s">',CODOC_TOKEN_OPTION_NAME,$CODOC_TOKEN); |
| 425 |
echo sprintf('<p>認証を<a href="javascript:void(0);" onClick="' . $script . '">解除する</p>'); |
| 426 |
}, |
| 427 |
'codoc', |
| 428 |
'setting_section_id' |
| 429 |
); |
| 430 |
|
| 431 |
} |
| 432 |
|
| 433 |
// ない場合 |
| 434 |
if (!$CODOC_AUTHINFO and !isset($_GET['auth_by_myself'])) { |
| 435 |
add_settings_field( |
| 436 |
'codoc_auth', |
| 437 |
'認証', |
| 438 |
function() { |
| 439 |
|
| 440 |
//$current_url = (empty($_SERVER['HTTPS']) ? 'http://' : 'https://').$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; |
| 441 |
$current_url = admin_url('options-general.php') . '?page=codoc'; |
| 442 |
$login_url = sprintf("location.href='%s'",CODOC_URL . '/me/token?from=wp&return_url=' . urlencode($current_url)); |
| 443 |
$register_url = sprintf("location.href='%s'",CODOC_URL . '/register?from=wp&return_url=' . urlencode($current_url)); |
| 444 |
// submitを消しておく |
| 445 |
echo ('<script type="text/javascript">window.onload=function(){document.getElementById(\'submit\').style.display = \'none\'}</script>'); |
| 446 |
echo ('codocをWordPressでご利用いただくには認証が� |
| 447 |
要です。<br />'); |
| 448 |
echo sprintf('ユーザーコードとAPIトークンを<a href="%s&auth_by_myself=1">直接� |
| 449 |
�力</a>することでも認証できます。<br /><br />',$current_url); |
| 450 |
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); |
| 451 |
|
| 452 |
|
| 453 |
}, |
| 454 |
'codoc', |
| 455 |
'setting_section_id' |
| 456 |
); |
| 457 |
} |
| 458 |
// ない場合かつ自分で認証する場合 |
| 459 |
if (!$CODOC_AUTHINFO and ( |
| 460 |
(isset($_GET['auth_by_myself']) and $_GET['auth_by_myself']) or |
| 461 |
(isset($_POST['auth_by_myself']) and $_POST['auth_by_myself']) |
| 462 |
)) { |
| 463 |
register_setting( |
| 464 |
'codoc_option_group', // option group |
| 465 |
CODOC_USERCODE_OPTION_NAME |
| 466 |
); |
| 467 |
register_setting( |
| 468 |
'codoc_option_group', // option group |
| 469 |
CODOC_TOKEN_OPTION_NAME |
| 470 |
); |
| 471 |
add_settings_field( |
| 472 |
'codoc_usercode', |
| 473 |
'ユーザーコード', |
| 474 |
function() { |
| 475 |
global $CODOC_USERCODE; |
| 476 |
echo '<input type="hidden" name="auth_by_myself" value="1">'; |
| 477 |
echo sprintf('<input type="text" name="%s" value="%s">',CODOC_USERCODE_OPTION_NAME,$CODOC_USERCODE); |
| 478 |
}, |
| 479 |
'codoc', |
| 480 |
'setting_section_id' |
| 481 |
); |
| 482 |
add_settings_field( |
| 483 |
'codoc_token', |
| 484 |
'APIトークン', |
| 485 |
function() { |
| 486 |
global $CODOC_TOKEN; |
| 487 |
echo sprintf('<input type="text" name="%s" value="%s">',CODOC_TOKEN_OPTION_NAME,$CODOC_TOKEN); |
| 488 |
}, |
| 489 |
'codoc', |
| 490 |
'setting_section_id' |
| 491 |
); |
| 492 |
} |
| 493 |
|
| 494 |
// 認証� |
| 495 |
報を保存 |
| 496 |
add_action( 'update_option_' . CODOC_USERCODE_OPTION_NAME, function( $old_value, $new_value ) { |
| 497 |
global $CODOC_RE_AUTHORIZE; |
| 498 |
$CODOC_RE_AUTHORIZE = 1; |
| 499 |
},9,2); // $hook, $function_to_add, $priority, $accepted_args |
| 500 |
add_action( 'update_option_' . CODOC_TOKEN_OPTION_NAME, function( $old_value, $new_value ) { |
| 501 |
global $CODOC_RE_AUTHORIZE; |
| 502 |
$CODOC_RE_AUTHORIZE = 1; |
| 503 |
},10,2); |
| 504 |
|
| 505 |
add_action('update_option_' . CODOC_SETTINGS_OPTION_NAME,function(){ |
| 506 |
$CODOC_SETTINGS = get_option(CODOC_SETTINGS_OPTION_NAME); |
| 507 |
if (isset($CODOC_SETTINGS['always_show_support']) and $CODOC_SETTINGS['always_show_support']) { |
| 508 |
$data = $this->util->get_support_entry([],[ |
| 509 |
"usercode" => get_option(CODOC_USERCODE_OPTION_NAME), |
| 510 |
"token" => get_option(CODOC_TOKEN_OPTION_NAME), |
| 511 |
]); |
| 512 |
if ($data->status and $entry = $data->entry) { |
| 513 |
update_option(CODOC_SUPPORT_ENTRYCODE_OPTION_NAME,$entry->code); |
| 514 |
} else { |
| 515 |
update_option(CODOC_SUPPORT_ENTRYCODE_OPTION_NAME,''); |
| 516 |
} |
| 517 |
} elseif(isset($CODOC_SETTINGS['always_show_support']) and !$CODOC_SETTINGS['always_show_support']) { |
| 518 |
update_option(CODOC_SUPPORT_ENTRYCODE_OPTION_NAME,''); |
| 519 |
} |
| 520 |
}); |
| 521 |
|
| 522 |
add_action('updated_option',function(){ |
| 523 |
global $CODOC_RE_AUTHORIZE; |
| 524 |
if ($CODOC_RE_AUTHORIZE) { |
| 525 |
//$data = $this->callAPI('GET',''); |
| 526 |
$data = $this->util->get_user_info([],[ |
| 527 |
"usercode" => get_option(CODOC_USERCODE_OPTION_NAME), |
| 528 |
"token" => get_option(CODOC_TOKEN_OPTION_NAME), |
| 529 |
]); |
| 530 |
if ($data->status and $user = $data->user) { |
| 531 |
update_option(CODOC_AUTHINFO_OPTION_NAME,[ |
| 532 |
'email' => $user->email, |
| 533 |
'name' => $user->name, |
| 534 |
]); |
| 535 |
} else { |
| 536 |
update_option(CODOC_AUTHINFO_OPTION_NAME,''); |
| 537 |
} |
| 538 |
} |
| 539 |
}); |
| 540 |
|
| 541 |
}); |
| 542 |
// プラグイン一覧に設定のリンクをいれる |
| 543 |
add_filter( 'plugin_action_links_' . plugin_basename( plugin_dir_path( __FILE__ ) . 'codoc' . '.php' ), |
| 544 |
function( $links ) { |
| 545 |
$setting_link = sprintf( '<a href="%s">%s</a>', esc_url( add_query_arg( 'page', 'codoc', admin_url( 'options-general.php' ) ) ), esc_html( '設定' ) ); |
| 546 |
array_unshift( $links, $setting_link ); |
| 547 |
|
| 548 |
return $links; |
| 549 |
} |
| 550 |
); |
| 551 |
|
| 552 |
} |
| 553 |
public function add_mce() { |
| 554 |
add_action( 'admin_enqueue_scripts', function() { |
| 555 |
if ( ! current_user_can( 'edit_posts' ) && ! current_user_can( 'edit_pages' ) ) { |
| 556 |
return; |
| 557 |
} |
| 558 |
$current_screen = get_current_screen(); |
| 559 |
if ( ( method_exists( $current_screen, 'is_block_editor' ) && $current_screen->is_block_editor() ) || ( function_exists( 'is_gutenberg_page' ) && is_gutenberg_page() ) ) { |
| 560 |
// Gutenberg Editor |
| 561 |
// なにもしない |
| 562 |
} else { |
| 563 |
// tinymce |
| 564 |
if ( get_user_option( 'rich_editing' ) == 'true' ) { |
| 565 |
// プラグイン� |
| 566 |
で使うCSRF を生成 |
| 567 |
$path = plugins_url( 'codoc/src_mce/codoc-editor-onload.js', __DIR__ ); |
| 568 |
wp_enqueue_script( 'codoc-editor-onload', $path, array('jquery'), '', true ); |
| 569 |
// I just want to insert this global variables but i don't know how i can do it.. |
| 570 |
wp_localize_script( |
| 571 |
'codoc-editor-onload', |
| 572 |
'CODOCEDITOR', |
| 573 |
array( |
| 574 |
'action' => 'codoc_shortcodes', |
| 575 |
'nonce' => wp_create_nonce( 'codoc_shortcodes' ), |
| 576 |
|
| 577 |
'codoc_url' => CODOC_URL, |
| 578 |
'codoc_usercode' => get_option(CODOC_USERCODE_OPTION_NAME), |
| 579 |
'codoc_plugin_version' => CODOC_PLUGIN_VERSION, |
| 580 |
'codoc_sdk_path' => CODOC_SDK_PATH, |
| 581 |
) |
| 582 |
); |
| 583 |
|
| 584 |
// ここでtinymceのプラグイン追加 |
| 585 |
//wp_enqueue_style( 'codoc-admin-style', plugins_url( 'codoc/src_mce/codoc-admin.css', __DIR__ ) ); |
| 586 |
//add_editor_style( plugins_url( 'codoc/src_mce/codoc-admin.css', __DIR__ ) ); |
| 587 |
wp_enqueue_style( 'codoc-admin-style', CODOC_URL . CODOC_SDK_PATH . '.tinymce.css?c=' . date('Ymd') ); |
| 588 |
add_editor_style( CODOC_URL . CODOC_SDK_PATH . '.tinymce.css' ); |
| 589 |
|
| 590 |
add_filter( 'mce_external_plugins', function( $plugin_array ) { |
| 591 |
//$plugin_array['codoc'] = plugins_url( 'codoc/src_mce/codoc-editor.js', __DIR__ ); |
| 592 |
$plugin_array['codoc'] = CODOC_URL . CODOC_SDK_PATH . '.tinymce.js?c=' . date('Ymd'); |
| 593 |
return $plugin_array; |
| 594 |
} ); |
| 595 |
add_filter( 'mce_buttons', function( $buttons ) { |
| 596 |
array_push( $buttons, "|", "codoc" ); |
| 597 |
return $buttons; |
| 598 |
} ); |
| 599 |
|
| 600 |
} |
| 601 |
} |
| 602 |
} ); |
| 603 |
} |
| 604 |
public function get_filtered_content($post_emulated) { |
| 605 |
// 記事ページであることをエミュレートしてフィルター実行 |
| 606 |
$post_backuped = null; |
| 607 |
if (isset($GLOBALS['post'])) { |
| 608 |
$post_backuped = $GLOBALS['post']; |
| 609 |
} |
| 610 |
$GLOBALS['post'] = $post_emulated; |
| 611 |
|
| 612 |
$wp_query_backuped = null; |
| 613 |
if (isset($GLOBALS['wp_query'])) { |
| 614 |
$wp_query_backuped = $GLOBALS['wp_query']; |
| 615 |
$wp_query = $GLOBALS['wp_query']; |
| 616 |
$is_single_backuped = $wp_query->is_single; |
| 617 |
$wp_query->is_single = true; |
| 618 |
$wp_query->post = $post_emulated; |
| 619 |
$wp_query->queried_object = $post_emulated; |
| 620 |
$wp_query->queried_object_id = $post_emulated->id; |
| 621 |
|
| 622 |
$GLOBALS['wp_query'] = $wp_query; |
| 623 |
} |
| 624 |
// $this->the_content でオリジナルを返してもらうため |
| 625 |
$this->do_not_filter_the_content = true; |
| 626 |
$content = preg_replace( |
| 627 |
'/ (\/)?wp:codoc\/codoc-block /',' \\1wptmp:codoc/codoc-block ', |
| 628 |
$post_emulated->post_content |
| 629 |
); |
| 630 |
#$content = do_shortcode( $content ); |
| 631 |
$content_filtered = apply_filters( 'the_content', $content); |
| 632 |
$this->do_not_filter_the_content = false; |
| 633 |
|
| 634 |
$GLOBALS['post'] = $post_backuped; |
| 635 |
|
| 636 |
if ($wp_query_backuped) { |
| 637 |
$GLOBALS['wp_query'] = $wp_query_backuped; |
| 638 |
} |
| 639 |
|
| 640 |
$content_filtered = preg_replace( |
| 641 |
'/ (\/)?wptmp:codoc\/codoc-block /',' \\1wp:codoc/codoc-block ', |
| 642 |
$content_filtered |
| 643 |
); |
| 644 |
|
| 645 |
return $content_filtered; |
| 646 |
} |
| 647 |
|
| 648 |
public function save_post($post_ID,$post,$update) { |
| 649 |
if (preg_match('/^(auto-draft|inherit)$/',$post->post_status)) { |
| 650 |
return; |
| 651 |
} |
| 652 |
$entryCode = get_post_meta($post_ID,'codoc_entry_code',true); |
| 653 |
$codoc_settings = get_option(CODOC_SETTINGS_OPTION_NAME); |
| 654 |
$post_content = (isset($codoc_settings['do_not_filter_the_content']) and $codoc_settings['do_not_filter_the_content']) ? |
| 655 |
$post->post_content : $this->get_filtered_content($post); |
| 656 |
$res = $this->util->sync_entry([ |
| 657 |
"post_title" => $post->post_title, |
| 658 |
"post_content" => $post_content, |
| 659 |
"post_status" => ($post->post_status == 'publish' ? 1 : 0), |
| 660 |
"post_permalink" => get_permalink($post_ID), |
| 661 |
"codoc_entry_code" => $entryCode, |
| 662 |
"codoc_settings" => $codoc_settings, |
| 663 |
],[ |
| 664 |
"usercode" => get_option(CODOC_USERCODE_OPTION_NAME), |
| 665 |
"token" => get_option(CODOC_TOKEN_OPTION_NAME), |
| 666 |
]); |
| 667 |
|
| 668 |
if (is_object($res) and $res->status and !$entryCode) { |
| 669 |
update_post_meta($post_ID,'codoc_entry_code',$res->entry->code); |
| 670 |
} |
| 671 |
|
| 672 |
return true; |
| 673 |
} |
| 674 |
function updated_post_meta( $meta_ID, $post_ID, $meta_key ) { |
| 675 |
if ($meta_key != '_thumbnail_id') { |
| 676 |
return; |
| 677 |
} |
| 678 |
if ( has_post_thumbnail($post_ID) ) { |
| 679 |
$attachment = wp_get_attachment_metadata( get_post_thumbnail_id($post_ID)); |
| 680 |
$upload_dir = wp_upload_dir(); |
| 681 |
$file_path = sprintf ('%s/%s',$upload_dir['basedir'],$attachment['file']); |
| 682 |
|
| 683 |
return $this->util->post_thumbnail([ |
| 684 |
"file_path" => $file_path, |
| 685 |
"boundary" => wp_generate_password(24), |
| 686 |
"codoc_entry_code" => get_post_meta($post_ID,'codoc_entry_code',true), |
| 687 |
],[ |
| 688 |
"usercode" => get_option(CODOC_USERCODE_OPTION_NAME), |
| 689 |
"token" => get_option(CODOC_TOKEN_OPTION_NAME), |
| 690 |
]); |
| 691 |
} |
| 692 |
} |
| 693 |
function deleted_post_meta( $meta_ids, $post_ID, $meta_key,$meta_value ) { |
| 694 |
if ($meta_key != '_thumbnail_id') { |
| 695 |
return; |
| 696 |
} |
| 697 |
return $this->util->reset_thumbnail( |
| 698 |
["codoc_entry_code" => get_post_meta($post_ID,'codoc_entry_code',true)], |
| 699 |
[ |
| 700 |
"usercode" => get_option(CODOC_USERCODE_OPTION_NAME), |
| 701 |
"token" => get_option(CODOC_TOKEN_OPTION_NAME), |
| 702 |
] |
| 703 |
); |
| 704 |
} |
| 705 |
|
| 706 |
function the_content( $post_content ) { |
| 707 |
if ( is_single() && in_the_loop() && is_main_query() ) { |
| 708 |
} |
| 709 |
// get_filtered_content から do_not_filter_the_contentを有効にされるパターン |
| 710 |
// オプションの設定値の意味合い(他のフィルタを無視)とは違うため注意 |
| 711 |
if ($this->do_not_filter_the_content) { |
| 712 |
return $post_content; |
| 713 |
} |
| 714 |
$post = get_post(); |
| 715 |
# is_amp で実� |
| 716 |
しているテンプレート用 |
| 717 |
$is_amp_endpoint = (function_exists('is_amp_endpoint') && is_amp_endpoint()) ? true : |
| 718 |
((function_exists('is_amp') && is_amp()) ? true : false); |
| 719 |
return $this->util->filter_content([ |
| 720 |
"post_content" => $post_content, |
| 721 |
"preview" => is_preview(), |
| 722 |
"codoc_entry_code" => get_post_meta($post->ID,'codoc_entry_code',true), |
| 723 |
"codoc_settings" => get_option(CODOC_SETTINGS_OPTION_NAME), |
| 724 |
"is_amp_endpoint" => $is_amp_endpoint, |
| 725 |
"post_permalink" => get_permalink($post->ID), |
| 726 |
"codoc_support_entry_code" => get_option(CODOC_SUPPORT_ENTRYCODE_OPTION_NAME), |
| 727 |
]); |
| 728 |
} |
| 729 |
|
| 730 |
} |
| 731 |
|