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 +87 -162 0.80.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,32 +23,31 @@
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 - 'codoc_plugin_version' => CODOC_PLUGIN_VERSION,
31 - ));
32 -
33 - wp_localize_script('codoc-editor.js', 'OPTIONS', array(
34 - 'codoc_url' => CODOC_URL,
35 - 'codoc_usercode' => get_option(CODOC_USERCODE_OPTION_NAME),
36 - 'codoc_plugin_version' => CODOC_PLUGIN_VERSION,
37 - ));
38 - // tinymce 用
39 - //wp_enqueue_script( 'codoc-integration-js', CODOC_URL . '/js/integration.js', true );
40 - });
46 +
41 47 return $this;
42 48 }
43 -
49 +
44 50 // codocのJS登録
45 51
46 52 add_action( 'wp_enqueue_scripts', function() {
47 53 wp_enqueue_script( 'codoc-injector-js', CODOC_URL . '/js/cms.js' );
@@ -50,10 +56,8 @@
50 56 add_filter('script_loader_tag', [$this,'modifier_script_tag'],10,2);
51 57 // tinymce用のショートコード
52 58 add_shortcode('codoc', [$this, 'injector_shortcode']);
53 59
54 - // paywall用の本文非表示化とcodocタグへの属性追加
55 - add_filter('the_content',[$this,'the_content']);
56 60 return $this;
57 61 }
58 62
59 63 public function modifier_script_tag($tag,$handle) {
@@ -64,9 +68,10 @@
64 68 $data_css = '';
65 69 if ($css_path = $CODOC_SETTINGS['css_path']) {
66 70 $data_css = sprintf(' data-css="%s" ',$css_path);
67 71 }
68 - 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);
69 74 }
70 75 public function injector_shortcode($atts) {
71 76 global $post;
72 77 ob_start();
@@ -140,12 +145,12 @@
140 145 if (isset($_GET['page']) and $_GET['page'] == 'codoc' and isset($_GET['fetch_token_key'])) {
141 146 $key = sanitize_text_field($_GET['fetch_token_key']);
142 147 $usercode = sanitize_text_field($_GET['usercode']);
143 148 update_option(CODOC_USERCODE_OPTION_NAME,$usercode);
144 - $data = $this->callAPI('GET','/token',[ "fetch_token_key" => $key ]);
149 + $data = $this->util->callAPI('GET','/token',[ "fetch_token_key" => $key ]);
145 150 if ($data->status and $token = $data->token) {
146 151 update_option(CODOC_TOKEN_OPTION_NAME,$token);
147 - $data = $this->callAPI('GET','');
152 + $data = $this->util->callAPI('GET','');
148 153 if ($data->status and $user = $data->user) {
149 154 update_option(CODOC_AUTHINFO_OPTION_NAME,[
150 155 'email' => $user->email,
151 156 'name' => $user->name,
@@ -181,8 +186,14 @@
181 186 }
182 187 if (!isset($CODOC_SETTINGS['str_replace_binded_url_to'])) {
183 188 $CODOC_SETTINGS['str_replace_binded_url_to'] = '';
184 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 + }
185 196
186 197 add_settings_section(
187 198 'setting_section_id', // id
188 199 'codoc 設定ページ', // title
@@ -218,8 +229,23 @@
218 229 'codoc', //page
219 230 'setting_section_id' //Section
220 231 );
221 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 +
222 248 register_setting(
223 249 'codoc_option_group', // option group
224 250 CODOC_USERCODE_OPTION_NAME
225 251 );
@@ -319,9 +345,9 @@
319 345 },10,2);
320 346 add_action('updated_option',function(){
321 347 global $CODOC_RE_AUTHORIZE;
322 348 if ($CODOC_RE_AUTHORIZE) {
323 - $data = $this->callAPI('GET','');
349 + $data = $this->util->callAPI('GET','');
324 350 if ($data->status and $user = $data->user) {
325 351 update_option(CODOC_AUTHINFO_OPTION_NAME,[
326 352 'email' => $user->email,
327 353 'name' => $user->name,
@@ -345,9 +371,10 @@
345 371
346 372 }
347 373 public function add_mce() {
348 374 // Add Shortcode options in the WordPres visual editors
349 - // 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 +
350 377 add_action( 'init', function() {
351 378 if ( ! current_user_can( 'edit_posts' ) && ! current_user_can( 'edit_pages' ) ) {
352 379 return;
353 380 }
@@ -360,17 +387,26 @@
360 387 'codoc-editor-onload',
361 388 'CODOCEDITOR',
362 389 array(
363 390 'action' => 'codoc_shortcodes',
364 - '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,
365 397 )
366 398 );
399 +
367 400 // ここでtinymceのプラグイン追加
368 - wp_enqueue_style( 'codoc-admin-style', plugins_url( 'codoc/src_mce/codoc-admin.css', __DIR__ ) );
369 - add_editor_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' );
370 405
371 406 add_filter( 'mce_external_plugins', function( $plugin_array ) {
372 - $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');
373 409 return $plugin_array;
374 410 } );
375 411 add_filter( 'mce_buttons', function( $buttons ) {
376 412 array_push( $buttons, "|", "codoc" );
@@ -375,32 +411,9 @@
375 411 add_filter( 'mce_buttons', function( $buttons ) {
376 412 array_push( $buttons, "|", "codoc" );
377 413 return $buttons;
378 414 } );
379 - add_action( 'wp_ajax_codoc_shortcodes', function(){
380 - check_ajax_referer( 'codoc_shortcodes' );
381 - if ($usercode = sanitize_text_field($_GET['usercode'])) {
382 - update_option('codoc_usercode',$usercode);
383 - }
384 - $usercode = get_option(CODOC_USERCODE_OPTION_NAME) ? get_option(CODOC_USERCODE_OPTION_NAME) : 'nocode';
385 - $uri = CODOC_URL . '/api/v1/paywall/' . $usercode . '/entries';
386 - if ($entrycode = sanitize_text_field($_GET['entrycode'])) {
387 - $uri = $uri . '/' . $entrycode;
388 - }
389 - $args = preg_match('/local/',CODOC_URL) ? ['sslverify' => false ] : [];
390 - $response = wp_remote_request( $uri, $args );
391 -
392 - if ($response instanceof WP_Error) {
393 - exit;
394 - }
395 -
396 - $response['body'] = json_decode($response['body'], true);
397 - $entries = $response['body']['entries'];
398 415
399 - require 'views/codoc-shortcodes-list.php';
400 -
401 - wp_die();
402 - } );
403 416 }
404 417 } );
405 418 }
406 419 public function save_post($post_ID,$post,$update) {
@@ -406,65 +419,18 @@
406 419 public function save_post($post_ID,$post,$update) {
407 420 if (preg_match('/^(auto-draft|inherit)$/',$post->post_status)) {
408 421 return;
409 422 }
410 -
411 - $post_content = $post->post_content;
412 - preg_match('/wp:codoc\/codoc-block +?({.*})/',$post_content,$matches);
413 - // GUTENBERG で指定されたjson(記事情報)がない場合
414 - if (!$matches) {
415 - return;
416 - }
417 - // GUTENBERG で保存している内容を取得
418 - $codoc_info = json_decode($matches[1],true);
419 - // codoc タグがない場合はなにもしない
420 - //preg_match('/(<span +data-id="codoc-tag"[^>]+>(?:.+|)<\/span>)/',$post_content,$matches);
421 - preg_match('/(<(?:span|div)[^>]+data-id="codoc-tag"(?:[^>]+|)>(?:.+|)<\/(?:span|div)>)/',$post_content,$matches);
422 - if (!$matches) {
423 - return;
424 - }
425 - // TODO: split or html parse? HTMLがつながってる場合はおかしくなる
426 - // classic(tinymce)版の場合p、もしくはdivにかこまれている
427 - $splited = preg_split('/(?:<(?:div|p)(?:[^>]+|)>|)<\!-- +wp:codoc\/codoc-block .*<\!-- +\/wp:codoc\/codoc-block +-->(?:<\/(?:div|p)>|)/s',$post_content);
428 - $status = $post->post_status == 'publish' ? 1 : 0;
429 - # $body_free = '';
430 - # $body_paywalled = '';
431 - # if (count($splited) >= 2) {
432 - #
433 - # }
434 - $binded_url = get_permalink($post_ID);
435 - $CODOC_SETTINGS = get_option(CODOC_SETTINGS_OPTION_NAME);
436 - if (isset($CODOC_SETTINGS['str_replace_binded_url_from']) and
437 - isset($CODOC_SETTINGS['str_replace_binded_url_to']) and
438 - $CODOC_SETTINGS['str_replace_binded_url_from']) {
439 - $binded_url = str_replace(sanitize_text_field($CODOC_SETTINGS['str_replace_binded_url_from']),
440 - sanitize_text_field($CODOC_SETTINGS['str_replace_binded_url_to']),
441 - $binded_url);
442 - }
443 -
444 - $params = [
445 - 'title' => $post->post_title,
446 - 'body_free' => $splited[0],
447 - 'body_paywalled' => $splited[1],
448 - 'status' => $status,
449 - 'binded_url' => $binded_url,
450 - 'show_price' => isset($codoc_info['showPrice']) ? ($codoc_info['showPrice'] ? 1 : 0) : 0,
451 - 'price' => isset($codoc_info['price']) ? $codoc_info['price'] : 100,
452 - 'limited' => isset($codoc_info['limited']) ? ($codoc_info['limited'] ? 1 : 0) : 0,
453 - 'limited_count' => isset($codoc_info['limitedCount']) ? $codoc_info['limitedCount'] : 1,
454 - 'affiliate_mode' => isset($codoc_info['affiliateMode']) ? ($codoc_info['affiliateMode'] ? 1 : 0) : 0,
455 - 'affiliate_rate' => isset($codoc_info['affiliateRate']) ? $codoc_info['affiliateRate'] : '0.0500',
456 - 'show_support' => isset($codoc_info['showSupport']) ? ($codoc_info['showSupport'] ? 1 : 0) : 0,
457 - 'subscriptions' => isset($codoc_info['subscriptions']) ? array_keys($codoc_info['subscriptions']) : [],
458 - ];
459 423 $entryCode = get_post_meta($post_ID,'codoc_entry_code',true);
460 - if ($entryCode) {
461 - $res = $this->callAPI('PUT','/entries/' . $entryCode ,$params);
462 - } else {
463 - $res = $this->callAPI('POST', '/entries', $params);
464 - if (is_object($res) and $res->status) {
465 - update_post_meta($post_ID,'codoc_entry_code',$res->entry->code);
466 - }
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);
467 433 }
468 434 return true;
469 435 }
470 436 function updated_post_meta( $meta_ID, $post_ID, $meta_key ) {
@@ -474,35 +440,13 @@
474 440 if ( has_post_thumbnail($post_ID) ) {
475 441 $attachment = wp_get_attachment_metadata( get_post_thumbnail_id($post_ID));
476 442 $upload_dir = wp_upload_dir();
477 443 $file_path = sprintf ('%s/%s',$upload_dir['basedir'],$attachment['file']);
478 -
479 - if (is_readable($file_path)) {
480 - $name = 'file';
481 -
482 - $boundary = wp_generate_password(24);
483 -
484 - $payload = '';
485 - $payload .= '--' . $boundary;
486 - $payload .= "\r\n";
487 - $payload .= 'Content-Disposition: form-data; name="' . $name . '"; filename="' . basename( $file_path ) . '"' . "\r\n";
488 - $payload .= "Content-Type: application/octet-stream\r\n";
489 - $payload .= "Content-Transfer-Encoding: binary\r\n";
490 - $payload .= "\r\n";
491 - $payload .= file_get_contents( $file_path );
492 - $payload .= "\r\n";
493 -
494 - $payload .= '--' . $boundary . '--';
495 -
496 -
497 - $entryCode = get_post_meta($post_ID,'codoc_entry_code',true);
498 - $res = $this->callAPI(
499 - 'POST',
500 - '/entries/' . $entryCode . '/thumbnail',
501 - $payload,
502 - ['content-type' => 'multipart/form-data; boundary=' . $boundary]
503 - );
504 - }
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 + ]);
505 449 }
506 450 }
507 451 function deleted_post_meta( $meta_ids, $post_ID, $meta_key,$meta_value ) {
508 452 if ($meta_key != '_thumbnail_id') {
@@ -507,39 +451,20 @@
507 451 function deleted_post_meta( $meta_ids, $post_ID, $meta_key,$meta_value ) {
508 452 if ($meta_key != '_thumbnail_id') {
509 453 return;
510 454 }
511 - $entryCode = get_post_meta($post_ID,'codoc_entry_code',true);
512 - $res = $this->callAPI(
513 - 'POST',
514 - '/entries/' . $entryCode . '/thumbnail',
515 - [ "reset" => 1 ]
516 - );
455 + return $this->util->reset_thumbnail(["codoc_entry_code" => get_post_meta($post_ID,'codoc_entry_code',true)]);
517 456 }
518 457 function the_content( $post_content ) {
519 458 if ( is_single() && in_the_loop() && is_main_query() ) {
520 459 }
521 - // codoc タグがあるかどうか (202001:span -> div に変更)
522 - preg_match('/(<(span|div)[^>]+data-id="codoc-tag"(?:[^>]+|)>(?:.+|)<\/(?:div|span)>)/',$post_content,$matches);
523 - // data-wp-plugin-ver が無いタグは分割しない
524 - if (!$matches) {
525 - return $post_content;
526 - }
527 - // codocタグで分割 (202001: gutenbergで直前のdivタグを削除)
528 - $tag_regex = '/(?:<div(?:[^>]+|)>|)<(?:span|div)[^>]+data-id="codoc-tag"(?:[^>]+|)>(?:.+|)<\/(?:span|div)>(?:<\/div>|)/';
529 - if ( is_preview() ) {
530 - $post_content = preg_replace($tag_regex,'<div class="codoc-continue">ここから上は無料で表示されます</div>',$post_content);
531 - return $post_content;
532 - }
533 - // codoc タグの前後で分ける
534 - $splited = preg_split($tag_regex,$post_content);
535 - // codoc タグにID属性のentrycodeとdata-without-body(無料分を非表示)をつける
536 460 $post = get_post();
537 - $entryCode = sprintf('"codoc-entry-%s" ',get_post_meta($post->ID,'codoc_entry_code',true));
538 - $codoc_tag = preg_replace('/<((?:span|div)[^>]+)data-id="[^"]+"((?:[^>]+|))>/','<${1} ${2} data-without-body="1" id=' . $entryCode . ">", $matches[1]);
539 - // 無料部分のみ表示
540 - 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 + ]);
541 467 }
542 -
543 468
544 469 }
545 470