PluginProbe
codoc / 0.9.3
codoc v0.9.3
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 +200 -242 0.30.9.3 View file →
@@ -1,6 +1,6 @@
1 1 <?php
2 -
2 +require_once(plugin_dir_path( __FILE__ ) .'class-codoc-util.php');
3 3 final class Codoc {
4 4
5 5 public function __construct() {
6 6 if (is_admin()) {
@@ -6,45 +6,51 @@
6 6 if (is_admin()) {
7 7 // setting
8 8 $this->add_settings();
9 9 }
10 +
11 + // usercode, token はadminページのみDBから取得する
12 + $this->util = new CodocUtil();
13 +
14 + // paywall用の本文非表示化とcodocタグへの属性追加
15 + add_filter('the_content',[$this,'the_content']);
16 +
10 17 $auth_info = get_option(CODOC_AUTHINFO_OPTION_NAME);
11 - if (!$auth_info) {
12 - return $this;
13 - }
14 18
15 - // データ同期 (save_postはis_adminで実行されないケースがある)
16 - add_action( 'save_post', [$this,'save_post'], 20, 3 );
17 - add_action( 'added_post_meta', [$this,'updated_post_meta'], 10, 3 );
18 - add_action( 'updated_post_meta',[$this,'updated_post_meta'], 10, 3 );
19 - add_action( 'deleted_post_meta',[$this,'deleted_post_meta'], 10, 4 );
20 -
21 - if (is_admin()) {
22 - // gutenberg
23 - require_once 'src/init.php';
19 + if ($auth_info) {
20 + // データ同期 (save_postはis_adminで実行されないケースがある)
21 + add_action( 'save_post', [$this,'save_post'], 20, 3 );
22 + add_action( 'added_post_meta', [$this,'updated_post_meta'], 10, 3 );
23 + add_action( 'updated_post_meta',[$this,'updated_post_meta'], 10, 3 );
24 + add_action( 'deleted_post_meta',[$this,'deleted_post_meta'], 10, 4 );
25 + }
26 + if (is_admin() and $auth_info) { //管理画面
27 + // Gutenberg のサポートがない場合は何もしない
28 + if ( function_exists( 'register_block_type' ) ) {
29 + // gutenberg
30 + require_once 'src/init.php';
31 + add_action('wp_loaded', function() {
32 + wp_localize_script('wordpress-cgb-block-js', 'OPTIONS', array(
33 + 'codoc_url' => CODOC_URL,
34 + 'codoc_usercode' => get_option(CODOC_USERCODE_OPTION_NAME),
35 + 'codoc_plugin_version' => CODOC_PLUGIN_VERSION,
36 + 'codoc_sdk_path' => CODOC_SDK_PATH,
37 + ));
38 + });
39 + }
24 40 // tinymce
25 41 $this->add_mce();
26 - add_action('wp_loaded', function() {
27 - wp_localize_script('wordpress-cgb-block-js', 'OPTIONS', array(
28 - 'codoc_url' => CODOC_URL,
29 - 'codoc_usercode' => get_option(CODOC_USERCODE_OPTION_NAME)
30 - ));
31 - });
32 - return $this;
33 - }
34 -
35 - // codocのJS登録
36 - add_action( 'wp_enqueue_scripts', function() {
37 - wp_enqueue_script( 'codoc-injector-js', CODOC_URL . '/js/cms.js' );
38 - });
39 - // 登録したscriptタグに属性をつける
40 - add_filter('script_loader_tag', [$this,'modifier_script_tag'],10,2);
41 - // tinymce用のショートコード
42 - add_shortcode('codoc', [$this, 'injector_shortcode']);
43 -
44 - // paywall用の本文非表示化とcodocタグへの属性追加
45 - add_filter('the_content',[$this,'the_content']);
46 - return $this;
42 + } else { // ブログ画面
43 + // codocのJS登録
44 + add_action( 'wp_enqueue_scripts', function() {
45 + wp_enqueue_script( 'codoc-injector-js', CODOC_URL . '/js/cms.js' );
46 + });
47 + // 登録したscriptタグに属性をつける
48 + add_filter('script_loader_tag', [$this,'modifier_script_tag'],10,2);
49 + // tinymce用のショートコード
50 + add_shortcode('codoc', [$this, 'injector_shortcode']);
51 + }
52 + return $this;
47 53 }
48 54
49 55 public function modifier_script_tag($tag,$handle) {
50 56 if($handle !== 'codoc-injector-js') {
@@ -54,9 +60,10 @@
54 60 $data_css = '';
55 61 if ($css_path = $CODOC_SETTINGS['css_path']) {
56 62 $data_css = sprintf(' data-css="%s" ',$css_path);
57 63 }
58 - return str_replace(' src=', $data_css . ' defer src=', $tag);
64 + //return str_replace(' src=', $data_css . ' defer src=', $tag);
65 + return preg_replace('/(src=[^>]+)/',' ${1} ' . $data_css . ' defer',$tag);
59 66 }
60 67 public function injector_shortcode($atts) {
61 68 global $post;
62 69 ob_start();
@@ -62,50 +69,8 @@
62 69 ob_start();
63 70 require 'views/codoc-injector.php';
64 71 return ob_get_clean();
65 72 }
66 - public function callAPI($method,$path,$body = [],$headers = []) {
67 - $usercode = get_option(CODOC_USERCODE_OPTION_NAME);
68 - $token = get_option(CODOC_TOKEN_OPTION_NAME);
69 -
70 - $headers['X-CodocToken'] = $token;
71 - $host = CODOC_URL;
72 - $sslverify = true;
73 -
74 - if (preg_match('/local/',CODOC_URL)) {
75 - $sslverify = false;
76 - $host = 'https://host.docker.internal';
77 - }
78 - $url = sprintf("%s/api/v1/cms/%s%s",$host,$usercode,$path);
79 - $http = new WP_Http();
80 - try {
81 - $response = $http->request(
82 - $url,
83 - [
84 - 'sslverify' => $sslverify,
85 - 'method' => $method,
86 - 'timeout' => 10,
87 - 'headers' => $headers,
88 - 'body' => $body,
89 - ]
90 - );
91 - if ( is_wp_error($response) || $response['response']['code'] != 200 ) {
92 - //何もしない
93 - //var_dump( $response );
94 - }
95 - } catch(Exception $e) {
96 - }
97 -
98 - if (!is_wp_error($response) and
99 - isset($response['body']) and
100 - is_string($response['body']) and
101 - is_array(json_decode($response['body'], true)) and
102 - (json_last_error() == JSON_ERROR_NONE)) {
103 - return json_decode($response['body']);
104 - } else {
105 - return null;
106 - }
107 - }
108 73 # settings / 設定関連
109 74 public function add_settings() {
110 75 global $CODOC_SETTINGS;
111 76 add_action( 'admin_menu' ,function(){
@@ -119,9 +84,9 @@
119 84 echo '<form method="post" action="options.php">';
120 85 settings_fields( 'codoc_option_group' );
121 86 do_settings_sections( 'codoc' );
122 87 submit_button(); // 送信ボタン
123 - echo '</div>';
88 + echo '</form></div>';
124 89 }
125 90 );
126 91 });
127 92
@@ -130,12 +95,14 @@
130 95 if (isset($_GET['page']) and $_GET['page'] == 'codoc' and isset($_GET['fetch_token_key'])) {
131 96 $key = sanitize_text_field($_GET['fetch_token_key']);
132 97 $usercode = sanitize_text_field($_GET['usercode']);
133 98 update_option(CODOC_USERCODE_OPTION_NAME,$usercode);
134 - $data = $this->callAPI('GET','/token',[ "fetch_token_key" => $key ]);
99 + //$data = $this->callAPI('GET','/token',[ "fetch_token_key" => $key ]);
100 + $data = $this->util->get_token([ "fetch_token_key" => $key ],["usercode" => $usercode, "token" => "1"]);
135 101 if ($data->status and $token = $data->token) {
136 102 update_option(CODOC_TOKEN_OPTION_NAME,$token);
137 - $data = $this->callAPI('GET','');
103 + //$data = $this->callAPI('GET','');
104 + $data = $this->util->get_user_info([],["usercode" => $usercode, "token" => $token]);
138 105 if ($data->status and $user = $data->user) {
139 106 update_option(CODOC_AUTHINFO_OPTION_NAME,[
140 107 'email' => $user->email,
141 108 'name' => $user->name,
@@ -142,9 +109,9 @@
142 109 ]);
143 110 }
144 111 #$current_url = (empty($_SERVER['HTTPS']) ? 'http://' : 'https://').$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
145 112 #$current_url = preg_replace('/(.*)fetch_token_key.*/','${1}&codoc_auth_finished=1',$current_url);
146 - add_settings_error( 'general', 'settings_updated', __( 'OKKK' ), 'success' );
113 + //add_settings_error( 'general', 'settings_updated', __( 'OK' ), 'success' );
147 114 $current_url = admin_url('options-general.php') . '?page=codoc&codoc_auth_finished=1';
148 115
149 116 wp_redirect( $current_url);
150 117 exit;
@@ -165,8 +132,20 @@
165 132 'css_path' => '',
166 133 );
167 134 update_option( CODOC_SETTINGS_OPTION_NAME, $CODOC_SETTINGS );
168 135 }
136 + if (!isset($CODOC_SETTINGS['str_replace_binded_url_from'])) {
137 + $CODOC_SETTINGS['str_replace_binded_url_from'] = '';
138 + }
139 + if (!isset($CODOC_SETTINGS['str_replace_binded_url_to'])) {
140 + $CODOC_SETTINGS['str_replace_binded_url_to'] = '';
141 + }
142 + if (!isset($CODOC_SETTINGS['str_before_codoc_tag'])) {
143 + $CODOC_SETTINGS['str_before_codoc_tag'] = '';
144 + }
145 + if (!isset($CODOC_SETTINGS['str_after_codoc_tag'])) {
146 + $CODOC_SETTINGS['str_after_codoc_tag'] = '';
147 + }
169 148
170 149 add_settings_section(
171 150 'setting_section_id', // id
172 151 'codoc 設定ページ', // title
@@ -180,9 +159,9 @@
180 159 CODOC_SETTINGS_OPTION_NAME // option name(DB)
181 160 );
182 161 add_settings_field(
183 162 'css_path', // id
184 - 'カスタマイズ用CSSパス:', // title
163 + 'カスタマイズ用CSSパス', // title
185 164 function() {
186 165 global $CODOC_SETTINGS;
187 166 echo sprintf('<input type="text" name="%s[css_path]" value="%s">',CODOC_SETTINGS_OPTION_NAME,$CODOC_SETTINGS['css_path']);
188 167 },
@@ -188,9 +167,37 @@
188 167 },
189 168 'codoc', //page
190 169 'setting_section_id' //Section
191 170 );
171 + add_settings_field(
172 + 'str_replace_binded_url', // id
173 + 'codoc側に登録するパーマリンクを置換', // title
174 + function() {
175 + global $CODOC_SETTINGS;
176 + 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']);
177 + echo ('を');
178 + 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']);
179 + echo ('に変換');
180 + },
181 + 'codoc', //page
182 + 'setting_section_id' //Section
183 + );
192 184
185 + add_settings_field(
186 + 'str_around_codoc_tag', // id
187 + 'codocタグの前後にHTMLを挿入', // title
188 + function() {
189 + global $CODOC_SETTINGS;
190 + echo sprintf('<textarea name="%s[str_before_codoc_tag]" cols="40">%s</textarea>',CODOC_SETTINGS_OPTION_NAME,$CODOC_SETTINGS['str_before_codoc_tag']);
191 + echo ('を前に挿入<br />');
192 + echo sprintf('<textarea name="%s[str_after_codoc_tag]" cols="40">%s</textarea>',CODOC_SETTINGS_OPTION_NAME,$CODOC_SETTINGS['str_after_codoc_tag']);
193 + echo ('を後に挿入');
194 +
195 + },
196 + 'codoc', //page
197 + 'setting_section_id' //Section
198 + );
199 +
193 200 register_setting(
194 201 'codoc_option_group', // option group
195 202 CODOC_USERCODE_OPTION_NAME
196 203 );
@@ -209,9 +216,9 @@
209 216 function() {
210 217 global $CODOC_USERCODE;
211 218 global $CODOC_TOKEN;
212 219 global $CODOC_AUTHINFO;
213 - $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';";
220 + $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';";
214 221
215 222 echo sprintf('<p><font color="green" id="codoc-auth-message"><strong>%s</strong> で認証済</font></p>',$CODOC_AUTHINFO['email']);
216 223
217 224 echo sprintf('<input id="codoc-usercode" type="hidden" name="%s" value="%s">',CODOC_USERCODE_OPTION_NAME,$CODOC_USERCODE);
@@ -232,10 +239,10 @@
232 239 function() {
233 240
234 241 //$current_url = (empty($_SERVER['HTTPS']) ? 'http://' : 'https://').$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
235 242 $current_url = admin_url('options-general.php') . '?page=codoc';
236 - $login_url = sprintf("location.href='%s'",CODOC_URL . '/me/token?from_wp=1&return_url=' . urlencode($current_url));
237 - $register_url = sprintf("location.href='%s'",CODOC_URL . '/register?from_wp=1&return_url=' . urlencode($current_url));
243 + $login_url = sprintf("location.href='%s'",CODOC_URL . '/me/token?from=wp&return_url=' . urlencode($current_url));
244 + $register_url = sprintf("location.href='%s'",CODOC_URL . '/register?from=wp&return_url=' . urlencode($current_url));
238 245 // submitを消しておく
239 246 echo ('<script type="text/javascript">window.onload=function(){document.getElementById(\'submit\').style.display = \'none\'}</script>');
240 247 echo ('codocをWordPressでご利用いただくには認証が必要です。<br />');
241 248 echo sprintf('ユーザーコードとAPIトークンを<a href="%s&auth_by_myself=1">直接入力</a>することでも認証できます。<br /><br />',$current_url);
@@ -247,27 +254,31 @@
247 254 'setting_section_id'
248 255 );
249 256 }
250 257 // ない場合かつ自分で認証する場合
251 - if (!$CODOC_AUTHINFO and (isset($_GET['auth_by_myself']) and $_GET['auth_by_myself'])) {
258 + if (!$CODOC_AUTHINFO and (
259 + (isset($_GET['auth_by_myself']) and $_GET['auth_by_myself']) or
260 + (isset($_POST['auth_by_myself']) and $_POST['auth_by_myself'])
261 + )) {
252 262 register_setting(
253 263 'codoc_option_group', // option group
254 264 CODOC_USERCODE_OPTION_NAME
255 265 );
266 + register_setting(
267 + 'codoc_option_group', // option group
268 + CODOC_TOKEN_OPTION_NAME
269 + );
256 270 add_settings_field(
257 271 'codoc_usercode',
258 272 'ユーザーコード',
259 273 function() {
260 274 global $CODOC_USERCODE;
275 + echo '<input type="hidden" name="auth_by_myself" value="1">';
261 276 echo sprintf('<input type="text" name="%s" value="%s">',CODOC_USERCODE_OPTION_NAME,$CODOC_USERCODE);
262 277 },
263 278 'codoc',
264 279 'setting_section_id'
265 280 );
266 - register_setting(
267 - 'codoc_option_group', // option group
268 - CODOC_TOKEN_OPTION_NAME
269 - );
270 281 add_settings_field(
271 282 'codoc_token',
272 283 'APIトークン',
273 284 function() {
@@ -290,9 +301,13 @@
290 301 },10,2);
291 302 add_action('updated_option',function(){
292 303 global $CODOC_RE_AUTHORIZE;
293 304 if ($CODOC_RE_AUTHORIZE) {
294 - $data = $this->callAPI('GET','');
305 + //$data = $this->callAPI('GET','');
306 + $data = $this->util->get_user_info([],[
307 + "usercode" => get_option(CODOC_USERCODE_OPTION_NAME),
308 + "token" => get_option(CODOC_TOKEN_OPTION_NAME),
309 + ]);
295 310 if ($data->status and $user = $data->user) {
296 311 update_option(CODOC_AUTHINFO_OPTION_NAME,[
297 312 'email' => $user->email,
298 313 'name' => $user->name,
@@ -303,64 +318,67 @@
303 318 }
304 319 });
305 320
306 321 });
322 + // プラグイン一覧に設定のリンクをいれる
323 + add_filter( 'plugin_action_links_' . plugin_basename( plugin_dir_path( __FILE__ ) . 'codoc' . '.php' ),
324 + function( $links ) {
325 + $setting_link = sprintf( '<a href="%s">%s</a>', esc_url( add_query_arg( 'page', 'codoc', admin_url( 'options-general.php' ) ) ), esc_html( '設定' ) );
326 + array_unshift( $links, $setting_link );
327 +
328 + return $links;
329 + }
330 + );
331 +
307 332 }
308 333 public function add_mce() {
309 - // Add Shortcode options in the WordPres visual editors
310 - // wp_enqueue_script( 'codoc-editor-script', plugins_url( 'codoc/src_mce/codoc-editor.js', __DIR__ ));
311 - add_action( 'init', function() {
334 + add_action( 'admin_enqueue_scripts', function() {
312 335 if ( ! current_user_can( 'edit_posts' ) && ! current_user_can( 'edit_pages' ) ) {
313 336 return;
314 337 }
315 - if ( get_user_option( 'rich_editing' ) == 'true' ) {
316 - // プラグイン内で使うCSRF を生成
317 - $path = plugins_url( 'codoc/src_mce/codoc-editor-onload.js', __DIR__ );
318 - wp_enqueue_script( 'codoc-editor-onload', $path, array('jquery'), '', true );
319 - // I just want to insert this global variables but i don't know how i can do it..
320 - wp_localize_script(
321 - 'codoc-editor-onload',
322 - 'CODOCEDITOR',
323 - array(
324 - 'action' => 'codoc_shortcodes',
325 - 'nonce' => wp_create_nonce( 'codoc_shortcodes' )
326 - )
327 - );
328 - // ここでtinymceのプラグイン追加
329 - wp_enqueue_style( 'codoc-admin-style', plugins_url( 'codoc/src_mce/codoc-admin.css', __DIR__ ) );
330 - add_filter( 'mce_external_plugins', function( $plugin_array ) {
331 - $plugin_array['codoc'] = plugins_url( 'codoc/src_mce/codoc-editor.js', __DIR__ );
332 - return $plugin_array;
333 - } );
334 - add_filter( 'mce_buttons', function( $buttons ) {
335 - array_push( $buttons, "|", "codoc" );
336 - return $buttons;
337 - } );
338 - add_action( 'wp_ajax_codoc_shortcodes', function(){
339 - check_ajax_referer( 'codoc_shortcodes' );
340 - if ($usercode = sanitize_text_field($_GET['usercode'])) {
341 - update_option('codoc_usercode',$usercode);
342 - }
343 - $usercode = get_option(CODOC_USERCODE_OPTION_NAME) ? get_option(CODOC_USERCODE_OPTION_NAME) : 'nocode';
344 - $uri = CODOC_URL . '/api/v1/paywall/' . $usercode . '/entries';
345 - if ($entrycode = sanitize_text_field($_GET['entrycode'])) {
346 - $uri = $uri . '/' . $entrycode;
347 - }
348 - $args = preg_match('/local/',CODOC_URL) ? ['sslverify' => false ] : [];
349 - $response = wp_remote_request( $uri, $args );
350 -
351 - if ($response instanceof WP_Error) {
352 - exit;
353 - }
354 -
355 - $response['body'] = json_decode($response['body'], true);
356 - $entries = $response['body']['entries'];
338 + $current_screen = get_current_screen();
339 + if ( ( method_exists( $current_screen, 'is_block_editor' ) && $current_screen->is_block_editor() ) || ( function_exists( 'is_gutenberg_page' ) && is_gutenberg_page() ) ) {
340 + // Gutenberg Editor
341 + // なにもしない
342 + } else {
343 + // tinymce
344 + if ( get_user_option( 'rich_editing' ) == 'true' ) {
345 + // プラグイン内で使うCSRF を生成
346 + $path = plugins_url( 'codoc/src_mce/codoc-editor-onload.js', __DIR__ );
347 + wp_enqueue_script( 'codoc-editor-onload', $path, array('jquery'), '', true );
348 + // I just want to insert this global variables but i don't know how i can do it..
349 + wp_localize_script(
350 + 'codoc-editor-onload',
351 + 'CODOCEDITOR',
352 + array(
353 + 'action' => 'codoc_shortcodes',
354 + 'nonce' => wp_create_nonce( 'codoc_shortcodes' ),
355 +
356 + 'codoc_url' => CODOC_URL,
357 + 'codoc_usercode' => get_option(CODOC_USERCODE_OPTION_NAME),
358 + 'codoc_plugin_version' => CODOC_PLUGIN_VERSION,
359 + 'codoc_sdk_path' => CODOC_SDK_PATH,
360 + )
361 + );
357 362
358 - require 'views/codoc-shortcodes-list.php';
359 -
360 - wp_die();
361 - } );
362 - }
363 + // ここでtinymceのプラグイン追加
364 + //wp_enqueue_style( 'codoc-admin-style', plugins_url( 'codoc/src_mce/codoc-admin.css', __DIR__ ) );
365 + //add_editor_style( plugins_url( 'codoc/src_mce/codoc-admin.css', __DIR__ ) );
366 + wp_enqueue_style( 'codoc-admin-style', CODOC_URL . CODOC_SDK_PATH . '.tinymce.css?c=' . date('Ymd') );
367 + add_editor_style( CODOC_URL . CODOC_SDK_PATH . '.tinymce.css' );
368 +
369 + add_filter( 'mce_external_plugins', function( $plugin_array ) {
370 + //$plugin_array['codoc'] = plugins_url( 'codoc/src_mce/codoc-editor.js', __DIR__ );
371 + $plugin_array['codoc'] = CODOC_URL . CODOC_SDK_PATH . '.tinymce.js?c=' . date('Ymd');
372 + return $plugin_array;
373 + } );
374 + add_filter( 'mce_buttons', function( $buttons ) {
375 + array_push( $buttons, "|", "codoc" );
376 + return $buttons;
377 + } );
378 +
379 + }
380 + }
363 381 } );
364 382 }
365 383 public function save_post($post_ID,$post,$update) {
366 384 if (preg_match('/^(auto-draft|inherit)$/',$post->post_status)) {
@@ -365,55 +383,25 @@
365 383 public function save_post($post_ID,$post,$update) {
366 384 if (preg_match('/^(auto-draft|inherit)$/',$post->post_status)) {
367 385 return;
368 386 }
369 -
370 - $post_content = $post->post_content;
371 - preg_match('/wp:codoc\/codoc-block +?({.*})/',$post_content,$matches);
372 - // GUTENBERG で指定されたjson(記事情報)がない場合
373 - if (!$matches) {
374 - return;
375 - }
376 - // GUTENBERG で保存している内容を取得
377 - $codoc_info = json_decode($matches[1],true);
378 - // codoc タグがない場合はなにもしない
379 - preg_match('/(<span +data-id="codoc-tag"[^>]+>(?:.+|)<\/span>)/',$post_content,$matches);
380 - if (!$matches) {
381 - return;
382 - }
383 - // TODO: split or html parse? HTMLがつながってる場合はおかしくなる
384 - $splited = preg_split('/<\!-- +wp:codoc\/codoc-block .*<\!-- +\/wp:codoc\/codoc-block +-->/s',$post_content);
385 - $status = $post->post_status == 'publish' ? 1 : 0;
386 - # $body_free = '';
387 - # $body_paywalled = '';
388 - # if (count($splited) >= 2) {
389 - #
390 - # }
391 -
392 - $params = [
393 - 'title' => $post->post_title,
394 - 'body_free' => $splited[0],
395 - 'body_paywalled' => $splited[1],
396 - 'status' => $status,
397 - 'binded_url' => get_permalink($post_ID),
398 - 'show_price' => isset($codoc_info['showPrice']) ? ($codoc_info['showPrice'] ? 1 : 0) : 0,
399 - 'price' => isset($codoc_info['price']) ? $codoc_info['price'] : 100,
400 - 'limited' => isset($codoc_info['limited']) ? ($codoc_info['limited'] ? 1 : 0) : 0,
401 - 'limited_count' => isset($codoc_info['limitedCount']) ? $codoc_info['limitedCount'] : 1,
402 - 'affiliate_mode' => isset($codoc_info['affiliateMode']) ? ($codoc_info['affiliateMode'] ? 1 : 0) : 0,
403 - 'affiliate_rate' => isset($codoc_info['affiliateRate']) ? $codoc_info['affiliateRate'] : '0.0500',
404 - 'show_support' => isset($codoc_info['showSupport']) ? ($codoc_info['showSupport'] ? 1 : 0) : 0,
405 - 'subscriptions' => isset($codoc_info['subscriptions']) ? array_keys($codoc_info['subscriptions']) : [],
406 - ];
387 +
407 388 $entryCode = get_post_meta($post_ID,'codoc_entry_code',true);
408 - if ($entryCode) {
409 - $res = $this->callAPI('PUT','/entries/' . $entryCode ,$params);
410 - } else {
411 - $res = $this->callAPI('POST', '/entries', $params);
412 - if (is_object($res) and $res->status) {
413 - update_post_meta($post_ID,'codoc_entry_code',$res->entry->code);
414 - }
389 + $res = $this->util->sync_entry([
390 + "post_title" => $post->post_title,
391 + "post_content" => $post->post_content,
392 + "post_status" => ($post->post_status == 'publish' ? 1 : 0),
393 + "post_permalink" => get_permalink($post_ID),
394 + "codoc_entry_code" => $entryCode,
395 + ],[
396 + "usercode" => get_option(CODOC_USERCODE_OPTION_NAME),
397 + "token" => get_option(CODOC_TOKEN_OPTION_NAME),
398 + ]);
399 +
400 + if (is_object($res) and $res->status and !$entryCode) {
401 + update_post_meta($post_ID,'codoc_entry_code',$res->entry->code);
415 402 }
403 +
416 404 return true;
417 405 }
418 406 function updated_post_meta( $meta_ID, $post_ID, $meta_key ) {
419 407 if ($meta_key != '_thumbnail_id') {
@@ -422,35 +410,17 @@
422 410 if ( has_post_thumbnail($post_ID) ) {
423 411 $attachment = wp_get_attachment_metadata( get_post_thumbnail_id($post_ID));
424 412 $upload_dir = wp_upload_dir();
425 413 $file_path = sprintf ('%s/%s',$upload_dir['basedir'],$attachment['file']);
426 -
427 - if (is_readable($file_path)) {
428 - $name = 'file';
429 -
430 - $boundary = wp_generate_password(24);
431 -
432 - $payload = '';
433 - $payload .= '--' . $boundary;
434 - $payload .= "\r\n";
435 - $payload .= 'Content-Disposition: form-data; name="' . $name . '"; filename="' . basename( $file_path ) . '"' . "\r\n";
436 - $payload .= "Content-Type: application/octet-stream\r\n";
437 - $payload .= "Content-Transfer-Encoding: binary\r\n";
438 - $payload .= "\r\n";
439 - $payload .= file_get_contents( $file_path );
440 - $payload .= "\r\n";
441 -
442 - $payload .= '--' . $boundary . '--';
443 -
444 -
445 - $entryCode = get_post_meta($post_ID,'codoc_entry_code',true);
446 - $res = $this->callAPI(
447 - 'POST',
448 - '/entries/' . $entryCode . '/thumbnail',
449 - $payload,
450 - ['content-type' => 'multipart/form-data; boundary=' . $boundary]
451 - );
452 - }
414 +
415 + return $this->util->post_thumbnail([
416 + "file_path" => $file_path,
417 + "boundary" => wp_generate_password(24),
418 + "codoc_entry_code" => get_post_meta($post_ID,'codoc_entry_code',true),
419 + ],[
420 + "usercode" => get_option(CODOC_USERCODE_OPTION_NAME),
421 + "token" => get_option(CODOC_TOKEN_OPTION_NAME),
422 + ]);
453 423 }
454 424 }
455 425 function deleted_post_meta( $meta_ids, $post_ID, $meta_key,$meta_value ) {
456 426 if ($meta_key != '_thumbnail_id') {
@@ -455,39 +425,27 @@
455 425 function deleted_post_meta( $meta_ids, $post_ID, $meta_key,$meta_value ) {
456 426 if ($meta_key != '_thumbnail_id') {
457 427 return;
458 428 }
459 - $entryCode = get_post_meta($post_ID,'codoc_entry_code',true);
460 - $res = $this->callAPI(
461 - 'POST',
462 - '/entries/' . $entryCode . '/thumbnail',
463 - [ "reset" => 1 ]
464 - );
465 - }
429 + return $this->util->reset_thumbnail(
430 + ["codoc_entry_code" => get_post_meta($post_ID,'codoc_entry_code',true)],
431 + [
432 + "usercode" => get_option(CODOC_USERCODE_OPTION_NAME),
433 + "token" => get_option(CODOC_TOKEN_OPTION_NAME),
434 + ]
435 + );
436 + }
437 +
466 438 function the_content( $post_content ) {
467 - if ( is_single() && in_the_loop() && is_main_query() ) {
468 - }
469 - // codoc タグがあるかどうか
470 - preg_match('/(<span +data-id="codoc-tag[^>]+>(?:.+|)<\/span>)/',$post_content,$matches);
471 - // data-wp-plugin-ver が無いタグは分割しない
472 - if (!$matches) {
473 - return $post_content;
474 - }
475 - if ( is_preview() ) {
476 - return $post_content;
477 - }
478 - // codoc タグの前後で分ける
479 - $splited = preg_split('/<div><span data-id="codoc-tag" class="codoc-entries">(?:.+|)<\/span><\/div>/',$post_content);
480 - //$splited = preg_split('/<div +class="wp-block-codoc-codoc-block">.*<\/div>/s',$post_content);
481 - // codco タグにentrycodeをつける
439 + if ( is_single() && in_the_loop() && is_main_query() ) {
440 + }
482 441 $post = get_post();
483 - $entryCode = sprintf('"codoc-entry-%s" ',get_post_meta($post->ID,'codoc_entry_code',true));
484 - $codoc_tag = str_replace('span','span id=' . $entryCode, $matches[1]);
485 - // codoc タグに属性をつける
486 - $codoc_tag = str_replace('span','span data-without-body="1" ',$codoc_tag);
487 - // 無料部分のみ表示
488 - return $splited[0] . sprintf('<div class="wp-block-codoc-codoc-block">%s</div>',$codoc_tag);
442 + return $this->util->filter_content([
443 + "post_content" => $post_content,
444 + "preview" => is_preview(),
445 + "codoc_entry_code" => get_post_meta($post->ID,'codoc_entry_code',true),
446 + "codoc_settings" => get_option(CODOC_SETTINGS_OPTION_NAME),
447 + ]);
489 448 }
490 -
491 449
492 450 }
493 451