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