| 1 |
<?php |
| 2 |
|
| 3 |
final class Codoc { |
| 4 |
|
| 5 |
public function register() { |
| 6 |
|
| 7 |
if ( is_admin() ) { |
| 8 |
return $this; |
| 9 |
} |
| 10 |
|
| 11 |
add_action( 'wp_enqueue_scripts', function() { |
| 12 |
wp_enqueue_script( 'codoc-injector-js', CODOC_URL . '/js/paywall.js' ); |
| 13 |
}); |
| 14 |
add_filter('script_loader_tag', function($tag, $handle) { |
| 15 |
if($handle !== 'codoc-injector-js') { |
| 16 |
return $tag; |
| 17 |
} |
| 18 |
return str_replace(' src=', ' defer src=', $tag); |
| 19 |
}, 10, 2); |
| 20 |
add_shortcode('codoc', [$this, 'injector_shortcode']); |
| 21 |
|
| 22 |
return $this; |
| 23 |
} |
| 24 |
|
| 25 |
public function injector_shortcode($atts) { |
| 26 |
global $post; |
| 27 |
ob_start(); |
| 28 |
require 'views/codoc-injector.php'; |
| 29 |
return ob_get_clean(); |
| 30 |
} |
| 31 |
|
| 32 |
public function add_mce() { |
| 33 |
// Add Shortcode options in the WordPres visual editors |
| 34 |
//wp_enqueue_script( 'codoc-editor-script', plugins_url( 'codoc/src_mce/codoc-editor.js', __DIR__ )); |
| 35 |
add_action( 'init', function() { |
| 36 |
if ( ! current_user_can( 'edit_posts' ) && ! current_user_can( 'edit_pages' ) ) { |
| 37 |
return; |
| 38 |
} |
| 39 |
if ( get_user_option( 'rich_editing' ) == 'true' ) { |
| 40 |
// プラグイン� |
| 41 |
で使うCSRF を生成 |
| 42 |
$path = plugins_url( 'codoc/src_mce/codoc-editor-onload.js', __DIR__ ); |
| 43 |
wp_enqueue_script( 'codoc-editor-onload', $path, array('jquery'), '', true ); |
| 44 |
// I just want to insert this global variables but i don't know how i can do it.. |
| 45 |
wp_localize_script( |
| 46 |
'codoc-editor-onload', |
| 47 |
'CODOCEDITOR', |
| 48 |
array( |
| 49 |
'action' => 'codoc_shortcodes', |
| 50 |
'nonce' => wp_create_nonce( 'codoc_shortcodes' ) |
| 51 |
) |
| 52 |
); |
| 53 |
// ここでtinymceのプラグイン追加 |
| 54 |
wp_enqueue_style( 'codoc-admin-style', plugins_url( 'codoc/src_mce/codoc-admin.css', __DIR__ ) ); |
| 55 |
add_filter( 'mce_external_plugins', function( $plugin_array ) { |
| 56 |
$plugin_array['codoc'] = plugins_url( 'codoc/src_mce/codoc-editor.js', __DIR__ ); |
| 57 |
return $plugin_array; |
| 58 |
} ); |
| 59 |
|
| 60 |
add_filter( 'mce_buttons', function( $buttons ) { |
| 61 |
array_push( $buttons, "|", "codoc" ); |
| 62 |
|
| 63 |
return $buttons; |
| 64 |
} ); |
| 65 |
|
| 66 |
add_action( 'wp_ajax_codoc_shortcodes', function(){ |
| 67 |
check_ajax_referer( 'codoc_shortcodes' ); |
| 68 |
if ($usercode = sanitize_text_field($_GET['usercode'])) { |
| 69 |
update_option('codoc_usercode',$usercode); |
| 70 |
} |
| 71 |
$usercode = get_option(CODOC_USERCODE_OPTION_NAME) ? get_option(CODOC_USERCODE_OPTION_NAME) : 'nocode'; |
| 72 |
$uri = CODOC_URL . '/api/v1/paywall/' . $usercode . '/entries'; |
| 73 |
|
| 74 |
if ($entrycode = sanitize_text_field($_GET['entrycode'])) { |
| 75 |
$uri = $uri . '/' . $entrycode; |
| 76 |
} |
| 77 |
$args = preg_match('/localhost/',CODOC_URL) ? ['sslverify' => false ] : []; |
| 78 |
$response = wp_remote_request( $uri, $args ); |
| 79 |
|
| 80 |
if ($response instanceof WP_Error) { |
| 81 |
exit; |
| 82 |
} |
| 83 |
|
| 84 |
$response['body'] = json_decode($response['body'], true); |
| 85 |
$entries = $response['body']['entries']; |
| 86 |
|
| 87 |
require 'views/codoc-shortcodes-list.php'; |
| 88 |
|
| 89 |
wp_die(); |
| 90 |
} ); |
| 91 |
} |
| 92 |
} ); |
| 93 |
|
| 94 |
|
| 95 |
} |
| 96 |
|
| 97 |
} |
| 98 |
|