PluginProbe
codoc / 0.9
codoc v0.9
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 +122 -145 0.30.9 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,12 +6,19 @@
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(["usercode" => 1, "token" => 1 ]);
13 +
10 14 $auth_info = get_option(CODOC_AUTHINFO_OPTION_NAME);
11 15 if (!$auth_info) {
12 16 return $this;
13 17 }
18 + $this->util->usercode = get_option(CODOC_USERCODE_OPTION_NAME);
19 + $this->util->token = get_option(CODOC_TOKEN_OPTION_NAME);
20 +
14 21
15 22 // データ同期 (save_postはis_adminで実行されないケースがある)
16 23 add_action( 'save_post', [$this,'save_post'], 20, 3 );
17 24 add_action( 'added_post_meta', [$this,'updated_post_meta'], 10, 3 );
@@ -16,24 +23,33 @@
16 23 add_action( 'save_post', [$this,'save_post'], 20, 3 );
17 24 add_action( 'added_post_meta', [$this,'updated_post_meta'], 10, 3 );
18 25 add_action( 'updated_post_meta',[$this,'updated_post_meta'], 10, 3 );
19 26 add_action( 'deleted_post_meta',[$this,'deleted_post_meta'], 10, 4 );
27 + // paywall用の本文非表示化とcodocタグへの属性追加
28 + add_filter('the_content',[$this,'the_content']);
20 29
21 30 if (is_admin()) {
22 - // gutenberg
23 - require_once 'src/init.php';
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 + }
24 44 // tinymce
25 45 $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 - });
46 +
32 47 return $this;
33 48 }
49 +
50 + // codocのJS登録
34 51
35 - // codocのJS登録
36 52 add_action( 'wp_enqueue_scripts', function() {
37 53 wp_enqueue_script( 'codoc-injector-js', CODOC_URL . '/js/cms.js' );
38 54 });
39 55 // 登録したscriptタグに属性をつける
@@ -40,10 +56,8 @@
40 56 add_filter('script_loader_tag', [$this,'modifier_script_tag'],10,2);
41 57 // tinymce用のショートコード
42 58 add_shortcode('codoc', [$this, 'injector_shortcode']);
43 59
44 - // paywall用の本文非表示化とcodocタグへの属性追加
45 - add_filter('the_content',[$this,'the_content']);
46 60 return $this;
47 61 }
48 62
49 63 public function modifier_script_tag($tag,$handle) {
@@ -54,9 +68,10 @@
54 68 $data_css = '';
55 69 if ($css_path = $CODOC_SETTINGS['css_path']) {
56 70 $data_css = sprintf(' data-css="%s" ',$css_path);
57 71 }
58 - return str_replace(' src=', $data_css . ' defer src=', $tag);
72 + //return str_replace(' src=', $data_css . ' defer src=', $tag);
73 + return preg_replace('/(src=[^>]+)/',' ${1} ' . $data_css . ' defer',$tag);
59 74 }
60 75 public function injector_shortcode($atts) {
61 76 global $post;
62 77 ob_start();
@@ -130,12 +145,12 @@
130 145 if (isset($_GET['page']) and $_GET['page'] == 'codoc' and isset($_GET['fetch_token_key'])) {
131 146 $key = sanitize_text_field($_GET['fetch_token_key']);
132 147 $usercode = sanitize_text_field($_GET['usercode']);
133 148 update_option(CODOC_USERCODE_OPTION_NAME,$usercode);
134 - $data = $this->callAPI('GET','/token',[ "fetch_token_key" => $key ]);
149 + $data = $this->util->callAPI('GET','/token',[ "fetch_token_key" => $key ]);
135 150 if ($data->status and $token = $data->token) {
136 151 update_option(CODOC_TOKEN_OPTION_NAME,$token);
137 - $data = $this->callAPI('GET','');
152 + $data = $this->util->callAPI('GET','');
138 153 if ($data->status and $user = $data->user) {
139 154 update_option(CODOC_AUTHINFO_OPTION_NAME,[
140 155 'email' => $user->email,
141 156 'name' => $user->name,
@@ -142,9 +157,9 @@
142 157 ]);
143 158 }
144 159 #$current_url = (empty($_SERVER['HTTPS']) ? 'http://' : 'https://').$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
145 160 #$current_url = preg_replace('/(.*)fetch_token_key.*/','${1}&codoc_auth_finished=1',$current_url);
146 - add_settings_error( 'general', 'settings_updated', __( 'OKKK' ), 'success' );
161 + //add_settings_error( 'general', 'settings_updated', __( 'OK' ), 'success' );
147 162 $current_url = admin_url('options-general.php') . '?page=codoc&codoc_auth_finished=1';
148 163
149 164 wp_redirect( $current_url);
150 165 exit;
@@ -165,8 +180,20 @@
165 180 'css_path' => '',
166 181 );
167 182 update_option( CODOC_SETTINGS_OPTION_NAME, $CODOC_SETTINGS );
168 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 + }
169 196
170 197 add_settings_section(
171 198 'setting_section_id', // id
172 199 'codoc 設定ページ', // title
@@ -180,9 +207,9 @@
180 207 CODOC_SETTINGS_OPTION_NAME // option name(DB)
181 208 );
182 209 add_settings_field(
183 210 'css_path', // id
184 - 'カスタマイズ用CSSパス:', // title
211 + 'カスタマイズ用CSSパス', // title
185 212 function() {
186 213 global $CODOC_SETTINGS;
187 214 echo sprintf('<input type="text" name="%s[css_path]" value="%s">',CODOC_SETTINGS_OPTION_NAME,$CODOC_SETTINGS['css_path']);
188 215 },
@@ -188,9 +215,37 @@
188 215 },
189 216 'codoc', //page
190 217 'setting_section_id' //Section
191 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 + );
192 232
233 + add_settings_field(
234 + 'str_around_codoc_tag', // id
235 + 'codocタグの前後にHTMLを挿入', // title
236 + function() {
237 + global $CODOC_SETTINGS;
238 + echo sprintf('<textarea name="%s[str_before_codoc_tag]" cols="40">%s</textarea>',CODOC_SETTINGS_OPTION_NAME,$CODOC_SETTINGS['str_before_codoc_tag']);
239 + echo ('を前に挿入<br />');
240 + echo sprintf('<textarea name="%s[str_after_codoc_tag]" cols="40">%s</textarea>',CODOC_SETTINGS_OPTION_NAME,$CODOC_SETTINGS['str_after_codoc_tag']);
241 + echo ('を後に挿入');
242 +
243 + },
244 + 'codoc', //page
245 + 'setting_section_id' //Section
246 + );
247 +
193 248 register_setting(
194 249 'codoc_option_group', // option group
195 250 CODOC_USERCODE_OPTION_NAME
196 251 );
@@ -232,10 +287,10 @@
232 287 function() {
233 288
234 289 //$current_url = (empty($_SERVER['HTTPS']) ? 'http://' : 'https://').$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
235 290 $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));
291 + $login_url = sprintf("location.href='%s'",CODOC_URL . '/me/token?from=wp&return_url=' . urlencode($current_url));
292 + $register_url = sprintf("location.href='%s'",CODOC_URL . '/register?from=wp&return_url=' . urlencode($current_url));
238 293 // submitを消しておく
239 294 echo ('<script type="text/javascript">window.onload=function(){document.getElementById(\'submit\').style.display = \'none\'}</script>');
240 295 echo ('codocをWordPressでご利用いただくには認証が必要です。<br />');
241 296 echo sprintf('ユーザーコードとAPIトークンを<a href="%s&auth_by_myself=1">直接入力</a>することでも認証できます。<br /><br />',$current_url);
@@ -290,9 +345,9 @@
290 345 },10,2);
291 346 add_action('updated_option',function(){
292 347 global $CODOC_RE_AUTHORIZE;
293 348 if ($CODOC_RE_AUTHORIZE) {
294 - $data = $this->callAPI('GET','');
349 + $data = $this->util->callAPI('GET','');
295 350 if ($data->status and $user = $data->user) {
296 351 update_option(CODOC_AUTHINFO_OPTION_NAME,[
297 352 'email' => $user->email,
298 353 'name' => $user->name,
@@ -303,12 +358,23 @@
303 358 }
304 359 });
305 360
306 361 });
362 + // プラグイン一覧に設定のリンクをいれる
363 + add_filter( 'plugin_action_links_' . plugin_basename( plugin_dir_path( __FILE__ ) . 'codoc' . '.php' ),
364 + function( $links ) {
365 + $setting_link = sprintf( '<a href="%s">%s</a>', esc_url( add_query_arg( 'page', 'codoc', admin_url( 'options-general.php' ) ) ), esc_html( '設定' ) );
366 + array_unshift( $links, $setting_link );
367 +
368 + return $links;
369 + }
370 + );
371 +
307 372 }
308 373 public function add_mce() {
309 374 // Add Shortcode options in the WordPres visual editors
310 - // wp_enqueue_script( 'codoc-editor-script', plugins_url( 'codoc/src_mce/codoc-editor.js', __DIR__ ));
375 + //wp_enqueue_script( 'codoc-editor-script', CODOC_URL . 'codoc/src_mce/codoc-editor.js');
376 +
311 377 add_action( 'init', function() {
312 378 if ( ! current_user_can( 'edit_posts' ) && ! current_user_can( 'edit_pages' ) ) {
313 379 return;
314 380 }
@@ -321,15 +387,26 @@
321 387 'codoc-editor-onload',
322 388 'CODOCEDITOR',
323 389 array(
324 390 'action' => 'codoc_shortcodes',
325 - 'nonce' => wp_create_nonce( 'codoc_shortcodes' )
391 + 'nonce' => wp_create_nonce( 'codoc_shortcodes' ),
392 +
393 + 'codoc_url' => CODOC_URL,
394 + 'codoc_usercode' => get_option(CODOC_USERCODE_OPTION_NAME),
395 + 'codoc_plugin_version' => CODOC_PLUGIN_VERSION,
396 + 'codoc_sdk_path' => CODOC_SDK_PATH,
326 397 )
327 398 );
399 +
328 400 // ここでtinymceのプラグイン追加
329 - wp_enqueue_style( 'codoc-admin-style', plugins_url( 'codoc/src_mce/codoc-admin.css', __DIR__ ) );
401 + //wp_enqueue_style( 'codoc-admin-style', plugins_url( 'codoc/src_mce/codoc-admin.css', __DIR__ ) );
402 + //add_editor_style( plugins_url( 'codoc/src_mce/codoc-admin.css', __DIR__ ) );
403 + wp_enqueue_style( 'codoc-admin-style', CODOC_URL . CODOC_SDK_PATH . '.tinymce.css?c=' . date('Ymd') );
404 + add_editor_style( CODOC_URL . CODOC_SDK_PATH . '.tinymce.css' );
405 +
330 406 add_filter( 'mce_external_plugins', function( $plugin_array ) {
331 - $plugin_array['codoc'] = plugins_url( 'codoc/src_mce/codoc-editor.js', __DIR__ );
407 + //$plugin_array['codoc'] = plugins_url( 'codoc/src_mce/codoc-editor.js', __DIR__ );
408 + $plugin_array['codoc'] = CODOC_URL . CODOC_SDK_PATH . '.tinymce.js?c=' . date('Ymd');
332 409 return $plugin_array;
333 410 } );
334 411 add_filter( 'mce_buttons', function( $buttons ) {
335 412 array_push( $buttons, "|", "codoc" );
@@ -334,32 +411,9 @@
334 411 add_filter( 'mce_buttons', function( $buttons ) {
335 412 array_push( $buttons, "|", "codoc" );
336 413 return $buttons;
337 414 } );
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'];
357 415
358 - require 'views/codoc-shortcodes-list.php';
359 -
360 - wp_die();
361 - } );
362 416 }
363 417 } );
364 418 }
365 419 public function save_post($post_ID,$post,$update) {
@@ -365,54 +419,18 @@
365 419 public function save_post($post_ID,$post,$update) {
366 420 if (preg_match('/^(auto-draft|inherit)$/',$post->post_status)) {
367 421 return;
368 422 }
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 - ];
407 423 $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 - }
424 + $res = $this->util->sync_entry([
425 + "post_title" => $post->post_title,
426 + "post_content" => $post->post_content,
427 + "post_status" => ($post->post_status == 'publish' ? 1 : 0), # 0, 1
428 + "post_permalink" => get_permalink($post_ID),
429 + "codoc_entry_code" => $entryCode,
430 + ]);
431 + if (is_object($res) and $res->status and !$entryCode) {
432 + update_post_meta($post_ID,'codoc_entry_code',$res->entry->code);
415 433 }
416 434 return true;
417 435 }
418 436 function updated_post_meta( $meta_ID, $post_ID, $meta_key ) {
@@ -422,35 +440,13 @@
422 440 if ( has_post_thumbnail($post_ID) ) {
423 441 $attachment = wp_get_attachment_metadata( get_post_thumbnail_id($post_ID));
424 442 $upload_dir = wp_upload_dir();
425 443 $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 - }
444 + return $this->util->post_thumbnail([
445 + "file_path" => $file_path,
446 + "boundary" => wp_generate_password(24),
447 + "codoc_entry_code" => get_post_meta($post_ID,'codoc_entry_code',true),
448 + ]);
453 449 }
454 450 }
455 451 function deleted_post_meta( $meta_ids, $post_ID, $meta_key,$meta_value ) {
456 452 if ($meta_key != '_thumbnail_id') {
@@ -455,39 +451,20 @@
455 451 function deleted_post_meta( $meta_ids, $post_ID, $meta_key,$meta_value ) {
456 452 if ($meta_key != '_thumbnail_id') {
457 453 return;
458 454 }
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 - );
455 + return $this->util->reset_thumbnail(["codoc_entry_code" => get_post_meta($post_ID,'codoc_entry_code',true)]);
465 456 }
466 457 function the_content( $post_content ) {
467 458 if ( is_single() && in_the_loop() && is_main_query() ) {
468 459 }
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をつける
482 460 $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);
461 + return $this->util->filter_content([
462 + "post_content" => $post_content,
463 + "preview" => is_preview(),
464 + "codoc_entry_code" => sprintf('"codoc-entry-%s" ',get_post_meta($post->ID,'codoc_entry_code',true)),
465 + "codoc_settings" => get_option(CODOC_SETTINGS_OPTION_NAME),
466 + ]);
489 467 }
490 -
491 468
492 469 }
493 470