| 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(["usercode" => 1, "token" => 1 ]); |
| 13 |
|
| 14 |
$auth_info = get_option(CODOC_AUTHINFO_OPTION_NAME); |
| 15 |
if (!$auth_info) { |
| 16 |
return $this; |
| 17 |
} |
| 18 |
$this->util->usercode = get_option(CODOC_USERCODE_OPTION_NAME); |
| 19 |
$this->util->token = get_option(CODOC_TOKEN_OPTION_NAME); |
| 20 |
|
| 21 |
|
| 22 |
// データ同期 (save_postはis_adminで実行されないケースがある) |
| 23 |
add_action( 'save_post', [$this,'save_post'], 20, 3 ); |
| 24 |
add_action( 'added_post_meta', [$this,'updated_post_meta'], 10, 3 ); |
| 25 |
add_action( 'updated_post_meta',[$this,'updated_post_meta'], 10, 3 ); |
| 26 |
add_action( 'deleted_post_meta',[$this,'deleted_post_meta'], 10, 4 ); |
| 27 |
// paywall用の本文非表示化とcodocタグへの属性追加 |
| 28 |
add_filter('the_content',[$this,'the_content']); |
| 29 |
|
| 30 |
if (is_admin()) { |
| 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 |
|
| 47 |
return $this; |
| 48 |
} |
| 49 |
|
| 50 |
// codocのJS登録 |
| 51 |
|
| 52 |
add_action( 'wp_enqueue_scripts', function() { |
| 53 |
wp_enqueue_script( 'codoc-injector-js', CODOC_URL . '/js/cms.js' ); |
| 54 |
}); |
| 55 |
// 登録したscriptタグに属性をつける |
| 56 |
add_filter('script_loader_tag', [$this,'modifier_script_tag'],10,2); |
| 57 |
// tinymce用のショートコード |
| 58 |
add_shortcode('codoc', [$this, 'injector_shortcode']); |
| 59 |
|
| 60 |
return $this; |
| 61 |
} |
| 62 |
|
| 63 |
public function modifier_script_tag($tag,$handle) { |
| 64 |
if($handle !== 'codoc-injector-js') { |
| 65 |
return $tag; |
| 66 |
} |
| 67 |
$CODOC_SETTINGS = get_option(CODOC_SETTINGS_OPTION_NAME); |
| 68 |
$data_css = ''; |
| 69 |
if ($css_path = $CODOC_SETTINGS['css_path']) { |
| 70 |
$data_css = sprintf(' data-css="%s" ',$css_path); |
| 71 |
} |
| 72 |
//return str_replace(' src=', $data_css . ' defer src=', $tag); |
| 73 |
return preg_replace('/(src=[^>]+)/',' ${1} ' . $data_css . ' defer',$tag); |
| 74 |
} |
| 75 |
public function injector_shortcode($atts) { |
| 76 |
global $post; |
| 77 |
ob_start(); |
| 78 |
require 'views/codoc-injector.php'; |
| 79 |
return ob_get_clean(); |
| 80 |
} |
| 81 |
public function callAPI($method,$path,$body = [],$headers = []) { |
| 82 |
$usercode = get_option(CODOC_USERCODE_OPTION_NAME); |
| 83 |
$token = get_option(CODOC_TOKEN_OPTION_NAME); |
| 84 |
|
| 85 |
$headers['X-CodocToken'] = $token; |
| 86 |
$host = CODOC_URL; |
| 87 |
$sslverify = true; |
| 88 |
|
| 89 |
if (preg_match('/local/',CODOC_URL)) { |
| 90 |
$sslverify = false; |
| 91 |
$host = 'https://host.docker.internal'; |
| 92 |
} |
| 93 |
$url = sprintf("%s/api/v1/cms/%s%s",$host,$usercode,$path); |
| 94 |
$http = new WP_Http(); |
| 95 |
try { |
| 96 |
$response = $http->request( |
| 97 |
$url, |
| 98 |
[ |
| 99 |
'sslverify' => $sslverify, |
| 100 |
'method' => $method, |
| 101 |
'timeout' => 10, |
| 102 |
'headers' => $headers, |
| 103 |
'body' => $body, |
| 104 |
] |
| 105 |
); |
| 106 |
if ( is_wp_error($response) || $response['response']['code'] != 200 ) { |
| 107 |
//何もしない |
| 108 |
//var_dump( $response ); |
| 109 |
} |
| 110 |
} catch(Exception $e) { |
| 111 |
} |
| 112 |
|
| 113 |
if (!is_wp_error($response) and |
| 114 |
isset($response['body']) and |
| 115 |
is_string($response['body']) and |
| 116 |
is_array(json_decode($response['body'], true)) and |
| 117 |
(json_last_error() == JSON_ERROR_NONE)) { |
| 118 |
return json_decode($response['body']); |
| 119 |
} else { |
| 120 |
return null; |
| 121 |
} |
| 122 |
} |
| 123 |
# settings / 設定関連 |
| 124 |
public function add_settings() { |
| 125 |
global $CODOC_SETTINGS; |
| 126 |
add_action( 'admin_menu' ,function(){ |
| 127 |
add_options_page( |
| 128 |
'codoc の設定', //ページタイトル |
| 129 |
'codoc', //設定メニューに表示されるメニュータイトル |
| 130 |
'edit_users', //権限 |
| 131 |
'codoc', //設定ページのURL。options-general.php?page=codoc |
| 132 |
function() { |
| 133 |
echo '<div clas="wrap">'; |
| 134 |
echo '<form method="post" action="options.php">'; |
| 135 |
settings_fields( 'codoc_option_group' ); |
| 136 |
do_settings_sections( 'codoc' ); |
| 137 |
submit_button(); // 送信ボタン |
| 138 |
echo '</div>'; |
| 139 |
} |
| 140 |
); |
| 141 |
}); |
| 142 |
|
| 143 |
add_action( "admin_init", function() { |
| 144 |
// codocからの認証データ処理 |
| 145 |
if (isset($_GET['page']) and $_GET['page'] == 'codoc' and isset($_GET['fetch_token_key'])) { |
| 146 |
$key = sanitize_text_field($_GET['fetch_token_key']); |
| 147 |
$usercode = sanitize_text_field($_GET['usercode']); |
| 148 |
update_option(CODOC_USERCODE_OPTION_NAME,$usercode); |
| 149 |
$data = $this->callAPI('GET','/token',[ "fetch_token_key" => $key ]); |
| 150 |
if ($data->status and $token = $data->token) { |
| 151 |
update_option(CODOC_TOKEN_OPTION_NAME,$token); |
| 152 |
$data = $this->callAPI('GET',''); |
| 153 |
if ($data->status and $user = $data->user) { |
| 154 |
update_option(CODOC_AUTHINFO_OPTION_NAME,[ |
| 155 |
'email' => $user->email, |
| 156 |
'name' => $user->name, |
| 157 |
]); |
| 158 |
} |
| 159 |
#$current_url = (empty($_SERVER['HTTPS']) ? 'http://' : 'https://').$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; |
| 160 |
#$current_url = preg_replace('/(.*)fetch_token_key.*/','${1}&codoc_auth_finished=1',$current_url); |
| 161 |
//add_settings_error( 'general', 'settings_updated', __( 'OK' ), 'success' ); |
| 162 |
$current_url = admin_url('options-general.php') . '?page=codoc&codoc_auth_finished=1'; |
| 163 |
|
| 164 |
wp_redirect( $current_url); |
| 165 |
exit; |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
global $CODOC_USERCODE; |
| 170 |
global $CODOC_SETTINGS; |
| 171 |
global $CODOC_TOKEN; |
| 172 |
global $CODOC_AUTHINFO; |
| 173 |
$CODOC_USERCODE = get_option(CODOC_USERCODE_OPTION_NAME); |
| 174 |
$CODOC_SETTINGS = get_option(CODOC_SETTINGS_OPTION_NAME); |
| 175 |
$CODOC_TOKEN = get_option(CODOC_TOKEN_OPTION_NAME); |
| 176 |
$CODOC_AUTHINFO = get_option(CODOC_AUTHINFO_OPTION_NAME); |
| 177 |
if( !$CODOC_SETTINGS ) { |
| 178 |
//デフォルト値 |
| 179 |
$CODOC_SETTINGS = array( |
| 180 |
'css_path' => '', |
| 181 |
); |
| 182 |
update_option( CODOC_SETTINGS_OPTION_NAME, $CODOC_SETTINGS ); |
| 183 |
} |
| 184 |
if (!isset($CODOC_SETTINGS['str_replace_binded_url_from'])) { |
| 185 |
$CODOC_SETTINGS['str_replace_binded_url_from'] = ''; |
| 186 |
} |
| 187 |
if (!isset($CODOC_SETTINGS['str_replace_binded_url_to'])) { |
| 188 |
$CODOC_SETTINGS['str_replace_binded_url_to'] = ''; |
| 189 |
} |
| 190 |
if (!isset($CODOC_SETTINGS['str_before_codoc_tag'])) { |
| 191 |
$CODOC_SETTINGS['str_before_codoc_tag'] = ''; |
| 192 |
} |
| 193 |
if (!isset($CODOC_SETTINGS['str_after_codoc_tag'])) { |
| 194 |
$CODOC_SETTINGS['str_after_codoc_tag'] = ''; |
| 195 |
} |
| 196 |
|
| 197 |
add_settings_section( |
| 198 |
'setting_section_id', // id |
| 199 |
'codoc 設定ページ', // title |
| 200 |
function (){}, // callback |
| 201 |
'codoc' // page |
| 202 |
); |
| 203 |
// 認証がある場合 |
| 204 |
if ($CODOC_AUTHINFO) { |
| 205 |
register_setting( |
| 206 |
'codoc_option_group', // option group |
| 207 |
CODOC_SETTINGS_OPTION_NAME // option name(DB) |
| 208 |
); |
| 209 |
add_settings_field( |
| 210 |
'css_path', // id |
| 211 |
'カスタマイズ用CSSパス', // title |
| 212 |
function() { |
| 213 |
global $CODOC_SETTINGS; |
| 214 |
echo sprintf('<input type="text" name="%s[css_path]" value="%s">',CODOC_SETTINGS_OPTION_NAME,$CODOC_SETTINGS['css_path']); |
| 215 |
}, |
| 216 |
'codoc', //page |
| 217 |
'setting_section_id' //Section |
| 218 |
); |
| 219 |
add_settings_field( |
| 220 |
'str_replace_binded_url', // id |
| 221 |
'codoc側に登録するパーマリンクを置換', // title |
| 222 |
function() { |
| 223 |
global $CODOC_SETTINGS; |
| 224 |
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']); |
| 225 |
echo ('を'); |
| 226 |
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']); |
| 227 |
echo ('に変換'); |
| 228 |
}, |
| 229 |
'codoc', //page |
| 230 |
'setting_section_id' //Section |
| 231 |
); |
| 232 |
|
| 233 |
add_settings_field( |
| 234 |
'str_around_codoc_tag', // id |
| 235 |
'codocタグの前後にHTMLを挿� |
| 236 |
�', // title |
| 237 |
function() { |
| 238 |
global $CODOC_SETTINGS; |
| 239 |
echo sprintf('<textarea name="%s[str_before_codoc_tag]" cols="40">%s</textarea>',CODOC_SETTINGS_OPTION_NAME,$CODOC_SETTINGS['str_before_codoc_tag']); |
| 240 |
echo ('を前に挿� |
| 241 |
�<br />'); |
| 242 |
echo sprintf('<textarea name="%s[str_after_codoc_tag]" cols="40">%s</textarea>',CODOC_SETTINGS_OPTION_NAME,$CODOC_SETTINGS['str_after_codoc_tag']); |
| 243 |
echo ('を後に挿� |
| 244 |
�'); |
| 245 |
|
| 246 |
}, |
| 247 |
'codoc', //page |
| 248 |
'setting_section_id' //Section |
| 249 |
); |
| 250 |
|
| 251 |
register_setting( |
| 252 |
'codoc_option_group', // option group |
| 253 |
CODOC_USERCODE_OPTION_NAME |
| 254 |
); |
| 255 |
register_setting( |
| 256 |
'codoc_option_group', // option group |
| 257 |
CODOC_TOKEN_OPTION_NAME |
| 258 |
); |
| 259 |
|
| 260 |
if (isset($_GET['codoc_auth_finished']) and $_GET['codoc_auth_finished']) { |
| 261 |
add_settings_error( 'general', 'settings_updated', __( 'codocの認証が完了しました' ), 'success' ); |
| 262 |
} |
| 263 |
|
| 264 |
add_settings_field( |
| 265 |
'codoc_auth', |
| 266 |
'認証', |
| 267 |
function() { |
| 268 |
global $CODOC_USERCODE; |
| 269 |
global $CODOC_TOKEN; |
| 270 |
global $CODOC_AUTHINFO; |
| 271 |
$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';"; |
| 272 |
|
| 273 |
echo sprintf('<p><font color="green" id="codoc-auth-message"><strong>%s</strong> で認証済</font></p>',$CODOC_AUTHINFO['email']); |
| 274 |
|
| 275 |
echo sprintf('<input id="codoc-usercode" type="hidden" name="%s" value="%s">',CODOC_USERCODE_OPTION_NAME,$CODOC_USERCODE); |
| 276 |
echo sprintf('<input id="codoc-token" type="hidden" name="%s" value="%s">',CODOC_TOKEN_OPTION_NAME,$CODOC_TOKEN); |
| 277 |
echo sprintf('<p>認証を<a href="javascript:void(0);" onClick="' . $script . '">解除する</p>'); |
| 278 |
}, |
| 279 |
'codoc', |
| 280 |
'setting_section_id' |
| 281 |
); |
| 282 |
|
| 283 |
} |
| 284 |
|
| 285 |
// ない場合 |
| 286 |
if (!$CODOC_AUTHINFO and !isset($_GET['auth_by_myself'])) { |
| 287 |
add_settings_field( |
| 288 |
'codoc_auth', |
| 289 |
'認証', |
| 290 |
function() { |
| 291 |
|
| 292 |
//$current_url = (empty($_SERVER['HTTPS']) ? 'http://' : 'https://').$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; |
| 293 |
$current_url = admin_url('options-general.php') . '?page=codoc'; |
| 294 |
$login_url = sprintf("location.href='%s'",CODOC_URL . '/me/token?from=wp&return_url=' . urlencode($current_url)); |
| 295 |
$register_url = sprintf("location.href='%s'",CODOC_URL . '/register?from=wp&return_url=' . urlencode($current_url)); |
| 296 |
// submitを消しておく |
| 297 |
echo ('<script type="text/javascript">window.onload=function(){document.getElementById(\'submit\').style.display = \'none\'}</script>'); |
| 298 |
echo ('codocをWordPressでご利用いただくには認証が� |
| 299 |
要です。<br />'); |
| 300 |
echo sprintf('ユーザーコードとAPIトークンを<a href="%s&auth_by_myself=1">直接� |
| 301 |
�力</a>することでも認証できます。<br /><br />',$current_url); |
| 302 |
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); |
| 303 |
|
| 304 |
|
| 305 |
}, |
| 306 |
'codoc', |
| 307 |
'setting_section_id' |
| 308 |
); |
| 309 |
} |
| 310 |
// ない場合かつ自分で認証する場合 |
| 311 |
if (!$CODOC_AUTHINFO and (isset($_GET['auth_by_myself']) and $_GET['auth_by_myself'])) { |
| 312 |
register_setting( |
| 313 |
'codoc_option_group', // option group |
| 314 |
CODOC_USERCODE_OPTION_NAME |
| 315 |
); |
| 316 |
add_settings_field( |
| 317 |
'codoc_usercode', |
| 318 |
'ユーザーコード', |
| 319 |
function() { |
| 320 |
global $CODOC_USERCODE; |
| 321 |
echo sprintf('<input type="text" name="%s" value="%s">',CODOC_USERCODE_OPTION_NAME,$CODOC_USERCODE); |
| 322 |
}, |
| 323 |
'codoc', |
| 324 |
'setting_section_id' |
| 325 |
); |
| 326 |
register_setting( |
| 327 |
'codoc_option_group', // option group |
| 328 |
CODOC_TOKEN_OPTION_NAME |
| 329 |
); |
| 330 |
add_settings_field( |
| 331 |
'codoc_token', |
| 332 |
'APIトークン', |
| 333 |
function() { |
| 334 |
global $CODOC_TOKEN; |
| 335 |
echo sprintf('<input type="text" name="%s" value="%s">',CODOC_TOKEN_OPTION_NAME,$CODOC_TOKEN); |
| 336 |
}, |
| 337 |
'codoc', |
| 338 |
'setting_section_id' |
| 339 |
); |
| 340 |
} |
| 341 |
|
| 342 |
// 認証� |
| 343 |
報を保存 |
| 344 |
add_action( 'update_option_' . CODOC_USERCODE_OPTION_NAME, function( $old_value, $new_value ) { |
| 345 |
global $CODOC_RE_AUTHORIZE; |
| 346 |
$CODOC_RE_AUTHORIZE = 1; |
| 347 |
},9,2); // $hook, $function_to_add, $priority, $accepted_args |
| 348 |
add_action( 'update_option_' . CODOC_TOKEN_OPTION_NAME, function( $old_value, $new_value ) { |
| 349 |
global $CODOC_RE_AUTHORIZE; |
| 350 |
$CODOC_RE_AUTHORIZE = 1; |
| 351 |
},10,2); |
| 352 |
add_action('updated_option',function(){ |
| 353 |
global $CODOC_RE_AUTHORIZE; |
| 354 |
if ($CODOC_RE_AUTHORIZE) { |
| 355 |
$data = $this->callAPI('GET',''); |
| 356 |
if ($data->status and $user = $data->user) { |
| 357 |
update_option(CODOC_AUTHINFO_OPTION_NAME,[ |
| 358 |
'email' => $user->email, |
| 359 |
'name' => $user->name, |
| 360 |
]); |
| 361 |
} else { |
| 362 |
update_option(CODOC_AUTHINFO_OPTION_NAME,''); |
| 363 |
} |
| 364 |
} |
| 365 |
}); |
| 366 |
|
| 367 |
}); |
| 368 |
// プラグイン一覧に設定のリンクをいれる |
| 369 |
add_filter( 'plugin_action_links_' . plugin_basename( plugin_dir_path( __FILE__ ) . 'codoc' . '.php' ), |
| 370 |
function( $links ) { |
| 371 |
$setting_link = sprintf( '<a href="%s">%s</a>', esc_url( add_query_arg( 'page', 'codoc', admin_url( 'options-general.php' ) ) ), esc_html( '設定' ) ); |
| 372 |
array_unshift( $links, $setting_link ); |
| 373 |
|
| 374 |
return $links; |
| 375 |
} |
| 376 |
); |
| 377 |
|
| 378 |
} |
| 379 |
public function add_mce() { |
| 380 |
// Add Shortcode options in the WordPres visual editors |
| 381 |
//wp_enqueue_script( 'codoc-editor-script', CODOC_URL . 'codoc/src_mce/codoc-editor.js'); |
| 382 |
|
| 383 |
add_action( 'init', function() { |
| 384 |
if ( ! current_user_can( 'edit_posts' ) && ! current_user_can( 'edit_pages' ) ) { |
| 385 |
return; |
| 386 |
} |
| 387 |
if ( get_user_option( 'rich_editing' ) == 'true' ) { |
| 388 |
// プラグイン� |
| 389 |
で使うCSRF を生成 |
| 390 |
$path = plugins_url( 'codoc/src_mce/codoc-editor-onload.js', __DIR__ ); |
| 391 |
wp_enqueue_script( 'codoc-editor-onload', $path, array('jquery'), '', true ); |
| 392 |
// I just want to insert this global variables but i don't know how i can do it.. |
| 393 |
wp_localize_script( |
| 394 |
'codoc-editor-onload', |
| 395 |
'CODOCEDITOR', |
| 396 |
array( |
| 397 |
'action' => 'codoc_shortcodes', |
| 398 |
'nonce' => wp_create_nonce( 'codoc_shortcodes' ), |
| 399 |
|
| 400 |
'codoc_url' => CODOC_URL, |
| 401 |
'codoc_usercode' => get_option(CODOC_USERCODE_OPTION_NAME), |
| 402 |
'codoc_plugin_version' => CODOC_PLUGIN_VERSION, |
| 403 |
'codoc_sdk_path' => CODOC_SDK_PATH, |
| 404 |
) |
| 405 |
); |
| 406 |
|
| 407 |
// ここでtinymceのプラグイン追加 |
| 408 |
//wp_enqueue_style( 'codoc-admin-style', plugins_url( 'codoc/src_mce/codoc-admin.css', __DIR__ ) ); |
| 409 |
//add_editor_style( plugins_url( 'codoc/src_mce/codoc-admin.css', __DIR__ ) ); |
| 410 |
wp_enqueue_style( 'codoc-admin-style', CODOC_URL . CODOC_SDK_PATH . '.tinymce.css?c=' . date('Ymd') ); |
| 411 |
add_editor_style( CODOC_URL . CODOC_SDK_PATH . '.tinymce.css' ); |
| 412 |
|
| 413 |
add_filter( 'mce_external_plugins', function( $plugin_array ) { |
| 414 |
//$plugin_array['codoc'] = plugins_url( 'codoc/src_mce/codoc-editor.js', __DIR__ ); |
| 415 |
$plugin_array['codoc'] = CODOC_URL . CODOC_SDK_PATH . '.tinymce.js?c=' . date('Ymd'); |
| 416 |
return $plugin_array; |
| 417 |
} ); |
| 418 |
add_filter( 'mce_buttons', function( $buttons ) { |
| 419 |
array_push( $buttons, "|", "codoc" ); |
| 420 |
return $buttons; |
| 421 |
} ); |
| 422 |
|
| 423 |
} |
| 424 |
} ); |
| 425 |
} |
| 426 |
public function save_post($post_ID,$post,$update) { |
| 427 |
if (preg_match('/^(auto-draft|inherit)$/',$post->post_status)) { |
| 428 |
return; |
| 429 |
} |
| 430 |
$entryCode = get_post_meta($post_ID,'codoc_entry_code',true); |
| 431 |
$res = $this->util->sync_entry([ |
| 432 |
"post_title" => $post->post_title, |
| 433 |
"post_content" => $post->post_content, |
| 434 |
"post_status" => ($post->post_status == 'publish' ? 1 : 0), # 0, 1 |
| 435 |
"post_permalink" => get_permalink($post_ID), |
| 436 |
"codoc_entry_code" => $entryCode, |
| 437 |
]); |
| 438 |
if (is_object($res) and $res->status and !$entryCode) { |
| 439 |
update_post_meta($post_ID,'codoc_entry_code',$res->entry->code); |
| 440 |
} |
| 441 |
return true; |
| 442 |
} |
| 443 |
function updated_post_meta( $meta_ID, $post_ID, $meta_key ) { |
| 444 |
if ($meta_key != '_thumbnail_id') { |
| 445 |
return; |
| 446 |
} |
| 447 |
if ( has_post_thumbnail($post_ID) ) { |
| 448 |
$attachment = wp_get_attachment_metadata( get_post_thumbnail_id($post_ID)); |
| 449 |
$upload_dir = wp_upload_dir(); |
| 450 |
$file_path = sprintf ('%s/%s',$upload_dir['basedir'],$attachment['file']); |
| 451 |
return $this->util->post_thumbnail([ |
| 452 |
"file_path" => $file_path, |
| 453 |
"boundary" => wp_generate_password(24), |
| 454 |
"codoc_entry_code" => get_post_meta($post_ID,'codoc_entry_code',true), |
| 455 |
]); |
| 456 |
} |
| 457 |
} |
| 458 |
function deleted_post_meta( $meta_ids, $post_ID, $meta_key,$meta_value ) { |
| 459 |
if ($meta_key != '_thumbnail_id') { |
| 460 |
return; |
| 461 |
} |
| 462 |
return $this->util->reset_thumbnail(["codoc_entry_code" => get_post_meta($post_ID,'codoc_entry_code',true)]); |
| 463 |
} |
| 464 |
function the_content( $post_content ) { |
| 465 |
if ( is_single() && in_the_loop() && is_main_query() ) { |
| 466 |
} |
| 467 |
$post = get_post(); |
| 468 |
return $this->util->filter_content([ |
| 469 |
"post_content" => $post_content, |
| 470 |
"preview" => is_preview(), |
| 471 |
"codoc_entry_code" => sprintf('"codoc-entry-%s" ',get_post_meta($post->ID,'codoc_entry_code',true)), |
| 472 |
"codoc_settings" => get_option(CODOC_SETTINGS_OPTION_NAME), |
| 473 |
]); |
| 474 |
} |
| 475 |
|
| 476 |
} |
| 477 |
|
| 478 |
|