PluginProbe
codoc / 0.3
codoc v0.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 +477 -139 0.20.3 View file →
@@ -1,155 +1,493 @@
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 + ));
31 + });
32 + return $this;
33 + }
10 34
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']);
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']);
26 43
27 - return $this;
28 - }
44 + // paywall用の本文非表示化とcodocタグへの属性追加
45 + add_filter('the_content',[$this,'the_content']);
46 + return $this;
47 + }
48 +
49 + public function modifier_script_tag($tag,$handle) {
50 + if($handle !== 'codoc-injector-js') {
51 + return $tag;
52 + }
53 + $CODOC_SETTINGS = get_option(CODOC_SETTINGS_OPTION_NAME);
54 + $data_css = '';
55 + if ($css_path = $CODOC_SETTINGS['css_path']) {
56 + $data_css = sprintf(' data-css="%s" ',$css_path);
57 + }
58 + return str_replace(' src=', $data_css . ' defer src=', $tag);
59 + }
60 + public function injector_shortcode($atts) {
61 + global $post;
62 + ob_start();
63 + require 'views/codoc-injector.php';
64 + return ob_get_clean();
65 + }
66 + public function callAPI($method,$path,$body = [],$headers = []) {
67 + $usercode = get_option(CODOC_USERCODE_OPTION_NAME);
68 + $token = get_option(CODOC_TOKEN_OPTION_NAME);
29 69
30 - public function injector_shortcode($atts) {
31 - global $post;
32 - ob_start();
33 - require 'views/codoc-injector.php';
34 - return ob_get_clean();
35 - }
70 + $headers['X-CodocToken'] = $token;
71 + $host = CODOC_URL;
72 + $sslverify = true;
36 73
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 - });
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 + # settings / 設定関連
109 + public function add_settings() {
110 + global $CODOC_SETTINGS;
111 + add_action( 'admin_menu' ,function(){
112 + add_options_page(
113 + 'codoc の設定', //ページタイトル
114 + 'codoc', //設定メニューに表示されるメニュータイトル
115 + 'edit_users', //権限
116 + 'codoc', //設定ページのURL。options-general.php?page=codoc
117 + function() {
118 + echo '<div clas="wrap">';
119 + echo '<form method="post" action="options.php">';
120 + settings_fields( 'codoc_option_group' );
121 + do_settings_sections( 'codoc' );
122 + submit_button(); // 送信ボタン
123 + echo '</div>';
124 + }
125 + );
126 + });
88 127
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" );
128 + add_action( "admin_init", function() {
129 + // codocからの認証データ処理
130 + if (isset($_GET['page']) and $_GET['page'] == 'codoc' and isset($_GET['fetch_token_key'])) {
131 + $key = sanitize_text_field($_GET['fetch_token_key']);
132 + $usercode = sanitize_text_field($_GET['usercode']);
133 + update_option(CODOC_USERCODE_OPTION_NAME,$usercode);
134 + $data = $this->callAPI('GET','/token',[ "fetch_token_key" => $key ]);
135 + if ($data->status and $token = $data->token) {
136 + update_option(CODOC_TOKEN_OPTION_NAME,$token);
137 + $data = $this->callAPI('GET','');
138 + if ($data->status and $user = $data->user) {
139 + update_option(CODOC_AUTHINFO_OPTION_NAME,[
140 + 'email' => $user->email,
141 + 'name' => $user->name,
142 + ]);
143 + }
144 + #$current_url = (empty($_SERVER['HTTPS']) ? 'http://' : 'https://').$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
145 + #$current_url = preg_replace('/(.*)fetch_token_key.*/','${1}&codoc_auth_finished=1',$current_url);
146 + add_settings_error( 'general', 'settings_updated', __( 'OKKK' ), 'success' );
147 + $current_url = admin_url('options-general.php') . '?page=codoc&codoc_auth_finished=1';
148 +
149 + wp_redirect( $current_url);
150 + exit;
151 + }
152 + }
120 153
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'];
154 + global $CODOC_USERCODE;
155 + global $CODOC_SETTINGS;
156 + global $CODOC_TOKEN;
157 + global $CODOC_AUTHINFO;
158 + $CODOC_USERCODE = get_option(CODOC_USERCODE_OPTION_NAME);
159 + $CODOC_SETTINGS = get_option(CODOC_SETTINGS_OPTION_NAME);
160 + $CODOC_TOKEN = get_option(CODOC_TOKEN_OPTION_NAME);
161 + $CODOC_AUTHINFO = get_option(CODOC_AUTHINFO_OPTION_NAME);
162 + if( !$CODOC_SETTINGS ) {
163 + //デフォルト値
164 + $CODOC_SETTINGS = array(
165 + 'css_path' => '',
166 + );
167 + update_option( CODOC_SETTINGS_OPTION_NAME, $CODOC_SETTINGS );
168 + }
169 +
170 + add_settings_section(
171 + 'setting_section_id', // id
172 + 'codoc 設定ページ', // title
173 + function (){}, // callback
174 + 'codoc' // page
175 + );
176 + // 認証がある場合
177 + if ($CODOC_AUTHINFO) {
178 + register_setting(
179 + 'codoc_option_group', // option group
180 + CODOC_SETTINGS_OPTION_NAME // option name(DB)
181 + );
182 + add_settings_field(
183 + 'css_path', // id
184 + 'カスタマイズ用CSSパス:', // title
185 + function() {
186 + global $CODOC_SETTINGS;
187 + echo sprintf('<input type="text" name="%s[css_path]" value="%s">',CODOC_SETTINGS_OPTION_NAME,$CODOC_SETTINGS['css_path']);
188 + },
189 + 'codoc', //page
190 + 'setting_section_id' //Section
191 + );
144 192
145 - require 'views/codoc-shortcodes-list.php';
193 + register_setting(
194 + 'codoc_option_group', // option group
195 + CODOC_USERCODE_OPTION_NAME
196 + );
197 + register_setting(
198 + 'codoc_option_group', // option group
199 + CODOC_TOKEN_OPTION_NAME
200 + );
201 +
202 + if (isset($_GET['codoc_auth_finished']) and $_GET['codoc_auth_finished']) {
203 + add_settings_error( 'general', 'settings_updated', __( 'codocの認証が完了しました' ), 'success' );
204 + }
205 +
206 + add_settings_field(
207 + 'codoc_auth',
208 + '認証',
209 + function() {
210 + global $CODOC_USERCODE;
211 + global $CODOC_TOKEN;
212 + 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';";
146 214
147 - wp_die();
148 - } );
149 - }
150 - } );
151 -
215 + echo sprintf('<p><font color="green" id="codoc-auth-message"><strong>%s</strong> で認証済</font></p>',$CODOC_AUTHINFO['email']);
152 216
153 - }
217 + echo sprintf('<input id="codoc-usercode" type="hidden" name="%s" value="%s">',CODOC_USERCODE_OPTION_NAME,$CODOC_USERCODE);
218 + echo sprintf('<input id="codoc-token" type="hidden" name="%s" value="%s">',CODOC_TOKEN_OPTION_NAME,$CODOC_TOKEN);
219 + echo sprintf('<p>認証を<a href="javascript:void(0);" onClick="' . $script . '">解除する</p>');
220 + },
221 + 'codoc',
222 + 'setting_section_id'
223 + );
224 +
225 + }
154 226
227 + // ない場合
228 + if (!$CODOC_AUTHINFO and !isset($_GET['auth_by_myself'])) {
229 + add_settings_field(
230 + 'codoc_auth',
231 + '認証',
232 + function() {
233 +
234 + //$current_url = (empty($_SERVER['HTTPS']) ? 'http://' : 'https://').$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
235 + $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));
238 + // submitを消しておく
239 + echo ('<script type="text/javascript">window.onload=function(){document.getElementById(\'submit\').style.display = \'none\'}</script>');
240 + echo ('codocをWordPressでご利用いただくには認証が必要です。<br />');
241 + echo sprintf('ユーザーコードとAPIトークンを<a href="%s&auth_by_myself=1">直接入力</a>することでも認証できます。<br /><br />',$current_url);
242 + 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);
243 +
244 +
245 + },
246 + 'codoc',
247 + 'setting_section_id'
248 + );
249 + }
250 + // ない場合かつ自分で認証する場合
251 + if (!$CODOC_AUTHINFO and (isset($_GET['auth_by_myself']) and $_GET['auth_by_myself'])) {
252 + register_setting(
253 + 'codoc_option_group', // option group
254 + CODOC_USERCODE_OPTION_NAME
255 + );
256 + add_settings_field(
257 + 'codoc_usercode',
258 + 'ユーザーコード',
259 + function() {
260 + global $CODOC_USERCODE;
261 + echo sprintf('<input type="text" name="%s" value="%s">',CODOC_USERCODE_OPTION_NAME,$CODOC_USERCODE);
262 + },
263 + 'codoc',
264 + 'setting_section_id'
265 + );
266 + register_setting(
267 + 'codoc_option_group', // option group
268 + CODOC_TOKEN_OPTION_NAME
269 + );
270 + add_settings_field(
271 + 'codoc_token',
272 + 'APIトークン',
273 + function() {
274 + global $CODOC_TOKEN;
275 + echo sprintf('<input type="text" name="%s" value="%s">',CODOC_TOKEN_OPTION_NAME,$CODOC_TOKEN);
276 + },
277 + 'codoc',
278 + 'setting_section_id'
279 + );
280 + }
281 +
282 + // 認証情報を保存
283 + add_action( 'update_option_' . CODOC_USERCODE_OPTION_NAME, function( $old_value, $new_value ) {
284 + global $CODOC_RE_AUTHORIZE;
285 + $CODOC_RE_AUTHORIZE = 1;
286 + },9,2); // $hook, $function_to_add, $priority, $accepted_args
287 + add_action( 'update_option_' . CODOC_TOKEN_OPTION_NAME, function( $old_value, $new_value ) {
288 + global $CODOC_RE_AUTHORIZE;
289 + $CODOC_RE_AUTHORIZE = 1;
290 + },10,2);
291 + add_action('updated_option',function(){
292 + global $CODOC_RE_AUTHORIZE;
293 + if ($CODOC_RE_AUTHORIZE) {
294 + $data = $this->callAPI('GET','');
295 + if ($data->status and $user = $data->user) {
296 + update_option(CODOC_AUTHINFO_OPTION_NAME,[
297 + 'email' => $user->email,
298 + 'name' => $user->name,
299 + ]);
300 + } else {
301 + update_option(CODOC_AUTHINFO_OPTION_NAME,'');
302 + }
303 + }
304 + });
305 +
306 + });
307 + }
308 + 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() {
312 + if ( ! current_user_can( 'edit_posts' ) && ! current_user_can( 'edit_pages' ) ) {
313 + return;
314 + }
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'];
357 +
358 + require 'views/codoc-shortcodes-list.php';
359 +
360 + wp_die();
361 + } );
362 + }
363 + } );
364 + }
365 + public function save_post($post_ID,$post,$update) {
366 + if (preg_match('/^(auto-draft|inherit)$/',$post->post_status)) {
367 + return;
368 + }
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 + $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 + }
415 + }
416 + return true;
417 + }
418 + function updated_post_meta( $meta_ID, $post_ID, $meta_key ) {
419 + if ($meta_key != '_thumbnail_id') {
420 + return;
421 + }
422 + if ( has_post_thumbnail($post_ID) ) {
423 + $attachment = wp_get_attachment_metadata( get_post_thumbnail_id($post_ID));
424 + $upload_dir = wp_upload_dir();
425 + $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 + }
453 + }
454 + }
455 + function deleted_post_meta( $meta_ids, $post_ID, $meta_key,$meta_value ) {
456 + if ($meta_key != '_thumbnail_id') {
457 + return;
458 + }
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 + }
466 + 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をつける
482 + $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);
489 + }
490 +
491 +
155 492 }
493 +