PluginProbe
codoc / trunk
codoc vtrunk
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
codoc / class-codoc-util.php

class-codoc-util.php in codoc trunk, at class-codoc-util.php

562 lines 24.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if (!defined('WPINC')) {
4 define( 'WPINC', 'wp-includes' );
5 }
6
7 if (!defined('CODOC_URL')) {
8 define( 'CODOC_URL', 'https://codoc.jp' );
9 }
10 global $wp_version;
11 if (version_compare( $wp_version,'5.9') >= 0 ) {
12 require_once( ABSPATH . WPINC . '/class-wp-http.php');
13 } else {
14 require_once( ABSPATH . WPINC . '/class-http.php');
15 }
16
17 require_once( ABSPATH . WPINC . '/class-wp-error.php');
18
19 final class CodocUtil {
20 public $codoc_url = CODOC_URL;
21 public function __construct($params = ['usercode' => null, 'token' => null,'codoc_url' => null]) {
22 $this->setAuthInfo(['usercode' => $params['usercode'], 'token' => $params['token']]);
23 if (isset($params['codoc_url'])) {
24 $this->codoc_url = $params['codoc_url'];
25 }
26 return $this;
27 }
28
29 public function callAPI($method,$path,$body = [],$headers = []) {
30 $usercode = $this->usercode;
31 $token = $this->token;
32
33 $headers['X-CodocToken'] = $token;
34 $host = $this->codoc_url;
35 $sslverify = true;
36
37 if (preg_match('/local/',$this->codoc_url)) {
38 $sslverify = false;
39 $host = 'https://host.docker.internal';
40 }
41 $url = sprintf("%s/api/v1/cms/%s%s",$host,$usercode,$path);
42 $http = new WP_Http();
43 try {
44 $response = $http->request(
45 $url,
46 [
47 'sslverify' => $sslverify,
48 'method' => $method,
49 'timeout' => 10,
50 'headers' => $headers,
51 'body' => $body,
52 ]
53 );
54 // notification を処理
55 $body = json_decode($response['body'],true);
56 if (isset($body['notification']) && !empty($body['notification'])) {
57 // notification をトランジェントに保存(24時間保持)
58 set_transient('codoc_api_notification', $body['notification'], DAY_IN_SECONDS);
59 }
60 if ( is_wp_error($response) || $response['response']['code'] != 200 ) {
61 //何もしない
62 //var_dump( $response );
63 }
64 } catch(Exception $e) {
65
66 }
67 if (!is_wp_error($response) and
68 isset($response['body']) and
69 is_string($response['body']) and
70 is_array(json_decode($response['body'], true)) and
71 (json_last_error() == JSON_ERROR_NONE)) {
72 return json_decode($response['body']);
73 } else {
74 return null;
75 }
76 }
77 public function callPaywallAPI($method,$path,$body = [],$headers = []) {
78 $site_usercode = get_option(CODOC_USERCODE_OPTION_NAME);
79 $paywall_token = $this->get_paywall_token_code();
80
81 $host = $this->codoc_url;
82 $sslverify = true;
83
84 if (preg_match('/local/',$this->codoc_url)) {
85 $sslverify = false;
86 $host = 'https://host.docker.internal';
87 }
88 $url = sprintf("%s/api/v1/paywall/%s%s",$host,$site_usercode,$path);
89 $http = new WP_Http();
90 try {
91 $response = $http->request(
92 $url,
93 [
94 'sslverify' => $sslverify,
95 'method' => $method,
96 'timeout' => 10,
97 'headers' => $headers,
98 'body' => $body,
99 ]
100 );
101 if ( is_wp_error($response) || $response['response']['code'] != 200 ) {
102 //何もしない
103 //var_dump( $response );
104 }
105 } catch(Exception $e) {
106
107 }
108 if (!is_wp_error($response) and
109 isset($response['body']) and
110 is_string($response['body']) and
111 is_array(json_decode($response['body'], true)) and
112 (json_last_error() == JSON_ERROR_NONE)) {
113 return json_decode($response['body']);
114 } else {
115 return null;
116 }
117 }
118 public function setAuthInfo($params = [ "usercode" => null, "token" => null ]) {
119 if ($params["usercode"]) {
120 $this->usercode = $params["usercode"];
121 }
122 if ($params["token"]) {
123 $this->token = $params["token"];
124 }
125 return true;
126 }
127 public function get_token($params = ["fetch_token_key" => null],$auth_info = ["usercode" => null, "token" => null]) {
128 $this->setAuthInfo($auth_info);
129 return $this->callAPI('GET','/token',[ "fetch_token_key" => $params["fetch_token_key"] ]);
130 }
131 public function get_user_info($params = [] ,$auth_info = ["usercode" => null, "token" => null]) {
132 $this->setAuthInfo($auth_info);
133 return $this->callAPI('GET','');
134 }
135 public function get_support_entry($params = [] ,$auth_info = ["usercode" => null, "token" => null]) {
136 $this->setAuthInfo($auth_info);
137 return $this->callAPI('GET','/support_entry');
138 }
139 // API側と統一した文字数上限のバリデーション。エラーメッセージの�
140 �列を返す (エラー無しなら空�
141 �列)
142 public function validate_entry_lengths($api_params) {
143 $errors = [];
144 $title = isset($api_params['title']) ? $api_params['title'] : '';
145 $body_free = isset($api_params['body_free']) ? $api_params['body_free'] : '';
146 $body_paywalled = isset($api_params['body_paywalled']) ? $api_params['body_paywalled'] : '';
147 $binded_url = isset($api_params['binded_url']) ? $api_params['binded_url'] : '';
148
149 if (mb_strlen($title) > CODOC_MAX_TITLE_LENGTH) {
150 // Translators: %1$d is the max length, %2$d is the current length.
151 $errors[] = sprintf(__('Title exceeds the maximum length of %1$d characters (current: %2$d).', 'codoc'), CODOC_MAX_TITLE_LENGTH, mb_strlen($title));
152 }
153 $len_body_free = mb_strlen($body_free);
154 $len_body_paywalled = mb_strlen($body_paywalled);
155 if ($len_body_free > CODOC_MAX_BODY_FREE_LENGTH) {
156 $errors[] = sprintf(__('Free area exceeds the maximum length of %1$d characters (current: %2$d).', 'codoc'), CODOC_MAX_BODY_FREE_LENGTH, $len_body_free);
157 }
158 if ($len_body_paywalled > CODOC_MAX_BODY_PAYWALLED_LENGTH) {
159 $errors[] = sprintf(__('Paid area exceeds the maximum length of %1$d characters (current: %2$d).', 'codoc'), CODOC_MAX_BODY_PAYWALLED_LENGTH, $len_body_paywalled);
160 }
161 $len_body_total = $len_body_free + $len_body_paywalled;
162 if ($len_body_total > CODOC_MAX_BODY_LENGTH) {
163 $errors[] = sprintf(__('Total body exceeds the maximum length of %1$d characters (current: %2$d).', 'codoc'), CODOC_MAX_BODY_LENGTH, $len_body_total);
164 }
165 if (mb_strlen($binded_url) > CODOC_MAX_BINDED_URL_LENGTH) {
166 $errors[] = sprintf(__('URL exceeds the maximum length of %1$d characters (current: %2$d).', 'codoc'), CODOC_MAX_BINDED_URL_LENGTH, mb_strlen($binded_url));
167 }
168 return $errors;
169 }
170
171 public function sync_entry($params = [
172 "post_title" => null,
173 "post_content" => null,
174 "post_status" => null, # 0, 1
175 "post_permalink" => null,
176 "codoc_entry_code" => null,
177 ],$auth_info = ["usercode" => null, "token" => null]) {
178
179 $this->setAuthInfo($auth_info);
180
181 $post_content = $params['post_content'];
182 preg_match('/wp:codoc\/codoc-block +?({.*})/',$post_content,$matches);
183 // GUTENBERG で指定されたjson(記事�
184 )がない場合
185 if (!$matches) {
186 return;
187 }
188 // GUTENBERG で保存している�
189 容を取得
190 $codoc_info = json_decode($matches[1],true);
191 // codoc タグがない場合はなにもしない
192 //preg_match('/(<span +data-id="codoc-tag"[^>]+>(?:.+|)<\/span>)/',$post_content,$matches);
193 preg_match('/(<(?:span|div)[^>]+data-id="codoc-tag"(?:[^>]+|)>(?:.+|)<\/(?:span|div)>)/',$post_content,$matches);
194 if (!$matches) {
195 return;
196 }
197 // TODO: split or html parse? HTMLがつながってる場合はおかしくなる
198 // classic(tinymce)版の場合p、もしくはdivにかこまれている
199 $end_tag_regex = '/<(?:div|p)>::CODOC_WP_END_PAYWALL::<\/(?:div|p)>/';
200 $before_splited = preg_split($end_tag_regex,$post_content);
201 $post_content = $before_splited[0];
202 $splited = preg_split('/(?:<(?:div|p)(?:[^>]+|)>|)<\!-- +wp:codoc\/codoc-block .*<\!-- +\/wp:codoc\/codoc-block +-->(?:<\/(?:div|p)>|)/s',$post_content);
203 $status = $params['post_status']; # 0: 非�
204 �開 1: �
205 �開 2:限定�
206 �開(パスワードの場合は限定�
207 �開で同期する)
208 // codoc設定で限定�
209 �開を有効で、�
210 �開の場合は 限定�
211 �開にする
212 if ($status == 1 and isset($codoc_info['statusLimited']) and $codoc_info['statusLimited']) {
213 $status = 2;
214 }
215 # $body_free = '';
216 # $body_paywalled = '';
217 # if (count($splited) >= 2) {
218 #
219 # }
220 $binded_url = $params['post_permalink'];
221 $CODOC_SETTINGS = get_option(CODOC_SETTINGS_OPTION_NAME);
222 if (isset($CODOC_SETTINGS['str_replace_binded_url_from']) and
223 isset($CODOC_SETTINGS['str_replace_binded_url_to']) and
224 $CODOC_SETTINGS['str_replace_binded_url_from']) {
225 $binded_url = str_replace(sanitize_text_field($CODOC_SETTINGS['str_replace_binded_url_from']),
226 sanitize_text_field($CODOC_SETTINGS['str_replace_binded_url_to']),
227 $binded_url);
228 }
229 $api_params = [
230 'title' => $params['post_title'],
231 'body_free' => $splited[0],
232 'body_paywalled' => $splited[1],
233 'status' => $status,
234 'binded_url' => $binded_url,
235 'show_price' => isset($codoc_info['showPrice']) ? ($codoc_info['showPrice'] ? 1 : 0) : 0,
236 'price' => isset($codoc_info['price']) ? $codoc_info['price'] : 100,
237 'limited' => isset($codoc_info['limited']) ? ($codoc_info['limited'] ? 1 : 0) : 0,
238 'limited_count' => isset($codoc_info['limitedCount']) ? $codoc_info['limitedCount'] : 1,
239 'affiliate_mode' => isset($codoc_info['affiliateMode']) ? ($codoc_info['affiliateMode'] ? 1 : 0) : 0,
240 'affiliate_rate' => isset($codoc_info['affiliateRate']) ? $codoc_info['affiliateRate'] : '0.0500',
241 'show_support' => isset($codoc_info['showSupport']) ? ($codoc_info['showSupport'] ? 1 : 0) : 0,
242 'show_paywalled_support' => isset($codoc_info['showPaywalledSupport']) ? ($codoc_info['showPaywalledSupport'] ? 1 : 0) : 0,
243 'subscriptions' => isset($codoc_info['subscriptions']) ? array_keys($codoc_info['subscriptions']) : [],
244 ];
245 // API側と統一した文字数バリデーション
246 $errors = $this->validate_entry_lengths($api_params);
247 if ($errors) {
248 set_transient('codoc_entry_validation_errors', $errors, MINUTE_IN_SECONDS * 5);
249 return null;
250 }
251 $entryCode = $params['codoc_entry_code'];
252 $res = null;
253 if ($entryCode) {
254 $res = $this->callAPI('PUT','/entries/' . $entryCode ,$api_params);
255 } else {
256 $res = $this->callAPI('POST', '/entries', $api_params);
257 }
258 return $res;
259 }
260
261 function post_thumbnail( $params = [
262 "file_path" => null,
263 "boundary" => null,
264 "codoc_entry_code" => null,
265 ], $auth_info = ["usercode" => null, "token" => null] ) {
266
267 $this->setAuthInfo($auth_info);
268
269 $file_path = $params['file_path'];
270 $boundary = $params['boundary']; # ランダム変数24桁 wp_generate_password(24);
271 $res = null;
272 if (is_readable($file_path)) {
273 $name = 'file';
274
275 $payload = '';
276 $payload .= '--' . $boundary;
277 $payload .= "\r\n";
278 $payload .= 'Content-Disposition: form-data; name="' . $name . '"; filename="' . basename( $file_path ) . '"' . "\r\n";
279 $payload .= "Content-Type: application/octet-stream\r\n";
280 $payload .= "Content-Transfer-Encoding: binary\r\n";
281 $payload .= "\r\n";
282 $payload .= file_get_contents( $file_path );
283 $payload .= "\r\n";
284
285 $payload .= '--' . $boundary . '--';
286
287 $entryCode = $params['codoc_entry_code'];
288 $res = $this->callAPI(
289 'POST',
290 '/entries/' . $entryCode . '/thumbnail',
291 $payload,
292 ['content-type' => 'multipart/form-data; boundary=' . $boundary]
293 );
294 }
295 return $res;
296 }
297
298 function reset_thumbnail( $params = [
299 "codoc_entry_code" => null,
300 ],$auth_info = ["usercode" => null, "token" => null] ) {
301
302 $this->setAuthInfo($auth_info);
303
304 $entryCode = $params['codoc_entry_code'];
305 $res = $this->callAPI(
306 'POST',
307 '/entries/' . $entryCode . '/thumbnail',
308 [ "reset" => 1 ]
309 );
310 return $res;
311 }
312
313 function filter_content( $params = [
314 "post_content" => null,
315 "preview" => null,
316 "codoc_entry_code" => null,
317 "codoc_settings" => null,
318 "is_amp_endpoint" => null,
319 "post_permalink" => null,
320 "codoc_support_entry_code" => null,
321 ]) {
322 $CODOC_SETTINGS = $params['codoc_settings'];
323
324 $has_codoc_tag = '/(<(span|div)[^>]+data-id="codoc-tag"(?:[^>]+|)>(?:.+|)<\/(?:div|span)>)/';
325 $post_content = $params['post_content'];
326 // codoc タグがあるかどうか (202001:span -> div に変更)
327 preg_match($has_codoc_tag,$post_content,$matches);
328
329 $show_support_message = '';
330 if (isset($CODOC_SETTINGS['show_support_message'])) {
331 $show_support_message = $CODOC_SETTINGS['show_support_message'];
332 }
333 $show_support_categories = '';
334 if (isset($CODOC_SETTINGS['show_support_categories'])) {
335 $show_support_categories = $CODOC_SETTINGS['show_support_categories'];
336 }
337 $show_support_location = 'bottom';
338 if (isset($CODOC_SETTINGS['show_support_location'])) {
339 $show_support_location = $CODOC_SETTINGS['show_support_location'];
340 }
341 // デフォルト値をうめておく
342 if (!isset($CODOC_SETTINGS['support_button_text'])) {
343 $CODOC_SETTINGS['support_button_text'] = 'サポートする';
344 }
345 if (!isset($CODOC_SETTINGS['show_like'])) {
346 $CODOC_SETTINGS['show_like'] = 1;
347 }
348 if (!isset($CODOC_SETTINGS['show_about_codoc'])) {
349 $CODOC_SETTINGS['show_about_codoc'] = 1;
350 }
351 if (!isset($CODOC_SETTINGS['show_powered_by'])) {
352 $CODOC_SETTINGS['show_powered_by'] = 1;
353 }
354 if (!isset($CODOC_SETTINGS['show_created_by'])) {
355 $CODOC_SETTINGS['show_created_by'] = 1;
356 }
357 if (!isset($CODOC_SETTINGS['show_copyright'])) {
358 $CODOC_SETTINGS['show_copyright'] = 1;
359 }
360 $show_support_entry = (is_single() && preg_grep("/(?:$show_support_categories)/",array_map( function($c) { return $c->name; },get_the_category(get_post()->ID))));
361 if (!$matches && ($support_entry_code = $params['codoc_support_entry_code']) && $show_support_entry) {
362 $support_tag = sprintf(
363 '<div class="codoc-entries" data-without-body="1" data-support-message="%s" id="codoc-entry-%s" data-support-button-text="%s" data-show-like="%s" data-show-about-codoc="%s" data-show-powered-by="%s" data-show-created-by="%s" data-show-copyright="%s"></div>',
364 htmlspecialchars($show_support_message,ENT_QUOTES),
365 $support_entry_code,
366 $CODOC_SETTINGS['support_button_text'],
367 $CODOC_SETTINGS['show_like'],
368 $CODOC_SETTINGS['show_about_codoc'],
369 $CODOC_SETTINGS['show_powered_by'],
370 $CODOC_SETTINGS['show_created_by'],
371 $CODOC_SETTINGS['show_copyright']
372 );
373 $post_content = $show_support_location == 'bottom' ?
374 $post_content . $support_tag :
375 $support_tag . $post_content ;
376 }
377
378 // data-wp-plugin-ver が無いタグは分割しない
379 if (!$matches) {
380 return $post_content;
381 }
382
383 // codocタグで分割 (202001: gutenbergで直前のdivタグを削除)
384 $tag_regex = '/(?:<div(?:[^>]+|)>|)<(?:span|div)[^>]+data-id="codoc-tag"(?:[^>]+|)>(?:.+|)<\/(?:span|div)>(?:<\/div>|)/';
385 $end_tag_regex = '/<(?:div|p)>::CODOC_WP_END_PAYWALL::<\/(?:div|p)>/';
386 if ( $params['preview'] ) {
387 $post_content = preg_replace($tag_regex,'<div class="codoc-continue">ここから上は無料で表示されます</div>',$post_content);
388 $post_content = preg_replace($end_tag_regex,'<div class="codoc-continue">ここから下は無料で表示されます</div>',$post_content);
389 return $post_content;
390 }
391 // codoc タグの前後で分ける
392 $before_splited = preg_split($end_tag_regex,$post_content);
393 $splited = preg_split($tag_regex,$before_splited[0]);
394 // codoc タグにID属性のentrycodeとdata-without-body(無料分を非表示)をつける
395 $entryCodeFormated = sprintf('"codoc-entry-%s" ',$params['codoc_entry_code']);
396
397 // 文言系の設定をつける
398 $tagAttributes = '';
399 if (isset($CODOC_SETTINGS['show_like']) and $CODOC_SETTINGS['show_like'] != 1) {
400 $tagAttributes = $tagAttributes . sprintf(' data-show-like="%s"',$CODOC_SETTINGS['show_like']);
401 }
402 if (isset($CODOC_SETTINGS['show_about_codoc']) and $CODOC_SETTINGS['show_about_codoc'] != 1) {
403 $tagAttributes = $tagAttributes . sprintf(' data-show-about-codoc="%s"',$CODOC_SETTINGS['show_about_codoc']);
404 }
405 if (isset($CODOC_SETTINGS['show_powered_by']) and $CODOC_SETTINGS['show_powered_by'] != 1) {
406 $tagAttributes = $tagAttributes . sprintf(' data-show-powered-by="%s"',$CODOC_SETTINGS['show_powered_by']);
407 }
408 if (isset($CODOC_SETTINGS['show_created_by']) and $CODOC_SETTINGS['show_created_by'] != 1) {
409 $tagAttributes = $tagAttributes . sprintf(' data-show-created-by="%s"',$CODOC_SETTINGS['show_created_by']);
410 }
411 if (isset($CODOC_SETTINGS['show_copyright']) and $CODOC_SETTINGS['show_copyright'] != 1) {
412 $tagAttributes = $tagAttributes . sprintf(' data-show-copyright="%s"',$CODOC_SETTINGS['show_copyright']);
413 }
414 if (isset($CODOC_SETTINGS['entry_button_text']) and $CODOC_SETTINGS['entry_button_text'] != '') {
415 $tagAttributes = $tagAttributes . sprintf(' data-entry-button-text="%s"',$CODOC_SETTINGS['entry_button_text']);
416 }
417 if (isset($CODOC_SETTINGS['subscription_button_text']) and $CODOC_SETTINGS['subscription_button_text'] != '') {
418 $tagAttributes = $tagAttributes . sprintf(' data-subscription-button-text="%s"',$CODOC_SETTINGS['subscription_button_text']);
419 }
420 if (isset($CODOC_SETTINGS['support_button_text']) and $CODOC_SETTINGS['support_button_text'] != '') {
421 $tagAttributes = $tagAttributes . sprintf(' data-support-button-text="%s"',$CODOC_SETTINGS['support_button_text']);
422 }
423 if (isset($CODOC_SETTINGS['subscription_message']) and $CODOC_SETTINGS['subscription_message'] != '') {
424 $tagAttributes = $tagAttributes . sprintf(' data-subscription-message="%s"',$CODOC_SETTINGS['subscription_message']);
425 }
426 if (isset($CODOC_SETTINGS['support_message']) and $CODOC_SETTINGS['support_message'] != '') {
427 $tagAttributes = $tagAttributes . sprintf(' data-support-message="%s"',$CODOC_SETTINGS['support_message']);
428 }
429 if (isset($CODOC_SETTINGS['codoc_tag_attributes']) and $CODOC_SETTINGS['codoc_tag_attributes'] != '') {
430 $tagAttributes = $tagAttributes . sprintf(' %s',$CODOC_SETTINGS['codoc_tag_attributes']);
431 }
432
433 $codoc_tag = preg_replace('/<((?:span|div)[^>]+)data-id="[^"]+"((?:[^>]+|))>/','<${1} ${2} data-without-body="1" id=' . $entryCodeFormated . $tagAttributes . ">", $matches[1]);
434 # THE THOR の page-lp.php が " " をけずるための対策
435 # "div class" -> "div class"
436 $codoc_tag = preg_replace('/div +class/','div class',$codoc_tag);
437
438 if ($params['is_amp_endpoint']) {
439 $format = preg_replace_callback('/(<(?:span|div)[^>]+>)((?:[^<>]+|))(<\/(?:span|div)>)/',function($matches) {
440 $format = $matches[1] . '<a href="%%s">%s</a>' . $matches[3];
441 return sprintf($format,($matches[2] ? $matches[2] : '続きを読む'));
442 },$codoc_tag);
443 $codoc_tag = sprintf($format,$params["post_permalink"]);
444 }
445
446 # タグの前後にHTMLを挿�
447
448 if (!isset($CODOC_SETTINGS['str_before_codoc_tag'])) {
449 $CODOC_SETTINGS['str_before_codoc_tag'] = '';
450 }
451 if (!isset($CODOC_SETTINGS['str_after_codoc_tag'])) {
452 $CODOC_SETTINGS['str_after_codoc_tag'] = '';
453 }
454 // 無料部分のみ表示
455 $post_content = $splited[0] . sprintf(
456 '%s<div class="wp-block-codoc-codoc-block">%s</div>%s',
457 $CODOC_SETTINGS['str_before_codoc_tag'],
458 $codoc_tag,
459 $CODOC_SETTINGS['str_after_codoc_tag']
460 );
461 // ::CODOC_WP_END_PAYWALL:: の後を足す
462 if (isset($before_splited[1]) and $before_splited[1]) {
463 $post_content .= $before_splited[1];
464 }
465 return $post_content;
466 }
467 /*
468
469 // Cookieの中のトークンを取得
470 $_CODOC->util->get_paywall_token_code();
471
472 */
473 function get_paywall_token_code() {
474 if (isset($_COOKIE['codocTokenCode']) and $codocTokenCode = $_COOKIE['codocTokenCode']) {
475 return $codocTokenCode;
476 }
477 return null;
478 }
479 /*
480
481 $_CODOC->util->check_login($mode); // cookie / strict
482
483 $response = $_CODOC->util->check_login('strict_with_data');
484 if ($response->status and $response->user) {
485 echo $reponse->user->code;
486 }
487
488 */
489 function check_login($mode = 'cookie') {
490 if ($codocTokenCode = $this->get_paywall_token_code()) {
491 if ($mode == 'cookie') {
492 // cookieの中にtokenがあればOK
493 return true;
494 }
495 if ($mode == 'strict' or $mode == 'strict_with_data') {
496 // ログイン状�
497 �を問い合わせ
498 $res = $this->callPaywallAPI('GET','/users',["paywall_token_code" => $codocTokenCode ]);
499 if ($res->status) {
500 if ($mode == 'strict_with_data') {
501 return $res;
502 } else {
503 return true;
504 }
505 } else {
506 return false;
507 }
508 }
509 }
510 return false;
511 }
512 /*
513
514 $_CODOC->util->check_owned($entry_code);
515
516 */
517 function check_owned($entry_code = null) {
518 if (!$entry_code) {
519 return false;
520 }
521 $res = $this->callPaywallAPI('GET','/entries/' . $entry_code, ["paywall_token_code" => $this->get_paywall_token_code(), "check_owned" => 1 ]);
522 if ($res->status) {
523 return true;
524 }
525 return false;
526 }
527
528 /*
529 $_CODOC->util->health_check();
530 */
531 function health_check() {
532 $sslverify = true;
533 $host = $this->codoc_url;
534 if (preg_match('/local/',$this->codoc_url)) {
535 $sslverify = false;
536 $host = 'https://host.docker.internal';
537 }
538 $url = sprintf("%s/codoc_health_check",$host);
539 $http = new WP_Http();
540 try {
541 $response = $http->request(
542 $url,
543 [
544 'sslverify' => $sslverify,
545 'method' => 'GET',
546 'timeout' => 10,
547 'headers' => [],
548 'body' => [],
549 ]
550 );
551 } catch(Exception $e) {
552
553 }
554 if ($response and $response['response']['code'] == '200') {
555 return true;
556 } else {
557 return null;
558 }
559 }
560 }
561
562