PluginProbe
codoc / 0.9.51
codoc v0.9.51
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 +1019 -471 0.30.9.51 View file →
@@ -1,493 +1,1041 @@
1 1 <?php
2 +require_once(plugin_dir_path( __FILE__ ) .'class-codoc-util.php');
3 +final class Codoc {
4 + public function __construct() {
5 + add_action('plugins_loaded', [$this,'codoc_load_textdomain']);
6 + if (is_admin()) {
7 + // setting
8 + $this->add_settings();
9 + }
2 10
3 -final class Codoc {
11 + // usercode, token はadminページのみDBから取得する
12 + $this->util = new CodocUtil([ 'usercode' => null, 'token' => null, 'codoc_url' => $this->get_codoc_url() ]);
13 +
14 + # the_content フィルタを実行しない状態
15 + $this->do_not_filter_the_content = false;
4 16
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 - }
17 + // paywall用の本文非表示化とcodocタグへの属性追加
18 + #$priority = 999999999;
19 + $priority = 100000; # フィルターは最後に実施する default 10
20 + $CODOC_SETTINGS = get_option(CODOC_SETTINGS_OPTION_NAME);
21 + if (isset($CODOC_SETTINGS["debug_params"])) {
22 + $params_decoded = json_decode($CODOC_SETTINGS["debug_params"],true);
23 + if (isset($params_decoded["the_content_filter_priority"])) {
24 + $priority = $params_decoded["the_content_filter_priority"];
25 + }
26 + }
27 + // ショーココードの退避
28 + if (isset($CODOC_SETTINGS['shortcode_evacuation']) and $CODOC_SETTINGS['shortcode_evacuation']) {
29 + add_filter('the_content',[$this,'the_content_shortcode_evacuations'],0);
30 + }
31 + add_filter('the_content',[$this,'the_content'],$priority);
32 + // 2021-09-24 the_content を利用しない場合は個別に呼び出ししてもらう
33 + add_filter('codoc_the_content',[$this,'the_content'],$priority);
34 +
35 + // 2020-07-25 excerpt対応
36 + add_filter('excerpt_allowed_blocks',[$this,'excerpt_allowed_blocks']);
37 +
38 + $auth_info = get_option(CODOC_AUTHINFO_OPTION_NAME);
14 39
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 - }
40 + if ($auth_info) {
41 + // データ同期 (save_postはis_adminで実行されないケースがある)
42 + add_action( 'save_post', [$this,'save_post'], 20, 3 );
43 + add_action( 'added_post_meta', [$this,'updated_post_meta'], 10, 3 );
44 + add_action( 'updated_post_meta',[$this,'updated_post_meta'], 10, 3 );
45 + add_action( 'deleted_post_meta',[$this,'deleted_post_meta'], 10, 4 );
46 + }
47 + global $pagenow;
48 + //管理画面でcodoc認証があり投稿画面の場合
49 + if (is_admin() and $auth_info and preg_match('/^post/',$pagenow)) {
50 + // Gutenberg のサポートがない場合は何もしない
51 + if ( function_exists( 'register_block_type' ) ) {
52 + // WPに登録ブロックとして認識させる(cgbは登録しないっぽい)
53 + \WP_Block_Type_Registry::get_instance()->register('codoc/codoc-block');
54 + // gutenberg
55 + require_once 'src/init.php';
56 + add_action('wp_loaded', function() {
57 + wp_localize_script('codoc-block-js', 'OPTIONS', array(
58 + 'codoc_url' => $this->get_codoc_url(),
59 + 'codoc_usercode' => get_option(CODOC_USERCODE_OPTION_NAME),
60 + 'codoc_plugin_version' => CODOC_PLUGIN_VERSION,
61 + 'codoc_sdk_path' => CODOC_SDK_PATH,
62 + ));
63 + wp_set_script_translations('codoc-block-js', 'codoc',plugin_dir_path( __FILE__ ) . 'languages');
64 + });
65 + }
66 + // tinymce
67 + $this->add_mce();
68 + } elseif(is_admin() and $auth_info and preg_match('/^edit/',$pagenow)) {
69 + // 記事編集ページ
70 + } elseif(!is_admin()) { // ブログ画面
71 + // codocのJS登録
72 + add_action( 'wp_enqueue_scripts', function() {
73 + $CODOC_SETTINGS = get_option(CODOC_SETTINGS_OPTION_NAME);
74 + $load_script_condition = '';
75 + if (isset($CODOC_SETTINGS["debug_params"]) and
76 + $params_decoded = json_decode($CODOC_SETTINGS["debug_params"],true) and
77 + isset($params_decoded["load_script_condition"])
78 + ) {
79 + $load_script_condition = $params_decoded["load_script_condition"];
80 + }
81 + if ($load_script_condition == 'not_list_page') {
82 + // not_list_pageの場合はトップページ等でスクリプトをロードしない
83 + if (!(is_archive() or is_category() or is_home() or is_front_page())) {
84 + wp_enqueue_script( 'codoc-injector-js', $this->get_codoc_url() . '/js/cms.js' );
85 + }
86 + } else {
87 + wp_enqueue_script( 'codoc-injector-js', $this->get_codoc_url() . '/js/cms.js' );
88 + }
89 + });
90 + //登録したscriptタグに属性をつける
91 + add_filter('script_loader_tag', [$this,'modifier_script_tag'],10,2);
92 + // tinymce用のショートコード
93 + add_shortcode('codoc', [$this, 'injector_shortcode']);
94 + // テーマをbodyにも反映
95 + add_filter('body_class', function($classes) {
96 + $CODOC_SETTINGS = get_option(CODOC_SETTINGS_OPTION_NAME);
97 + // デフォルトを変更
98 + $css_path = 'rainbow-square';
99 + if (isset($CODOC_SETTINGS["css_path"]) and $CODOC_SETTINGS["css_path"]) {
100 + $css_path = $CODOC_SETTINGS["css_path"];
101 + }
102 + array_push($classes,sprintf( "codoc-theme-%s",$css_path));
103 + return $classes;
104 + });
105 +
106 + }
107 + return $this;
108 + }
109 + function codoc_load_textdomain() {
110 + load_plugin_textdomain('codoc', false, dirname(plugin_basename( __FILE__ )) . '/languages/');
111 + }
112 + public function get_codoc_url() {
113 + $CODOC_SETTINGS = get_option(CODOC_SETTINGS_OPTION_NAME);
114 + if (isset($CODOC_SETTINGS["debug_params"])) {
115 + $params_decoded = json_decode($CODOC_SETTINGS["debug_params"],true);
116 + if (isset($params_decoded["codoc_url"])) {
117 + return $params_decoded["codoc_url"];
118 + }
119 + }
120 + return CODOC_URL;
121 + }
122 + public function modifier_script_tag($tag,$handle) {
123 + if($handle !== 'codoc-injector-js') {
124 + return $tag;
125 + }
126 + $CODOC_SETTINGS = get_option(CODOC_SETTINGS_OPTION_NAME);
127 + $data_css = '';
128 + if ($css_path = $CODOC_SETTINGS['css_path']) {
129 + $data_css = sprintf(' data-css="%s" ',$css_path);
130 + }
131 + // connect用パラメータ
132 + $connect_attributes = '';
133 + if (isset($CODOC_SETTINGS["codoc_connect_code"])) {
134 + $connect_code = $CODOC_SETTINGS["codoc_connect_code"];
135 + $connect_attributes = sprintf(' data-connect-code="%s"',$connect_code);
136 + $tag = preg_replace('/cms\.js/','cms-connect.js',$tag);
137 + }
138 + if (isset($CODOC_SETTINGS["codoc_connect_registration_mode"]) and $CODOC_SETTINGS["codoc_connect_registration_mode"]) {
139 + $connect_attributes = $connect_attributes .
140 + sprintf(' data-connect-registration-mode="%s"',$CODOC_SETTINGS["codoc_connect_registration_mode"]);
141 + }
34 142
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']);
143 + $usercode_attributes = '';
144 + $user_code = get_option(CODOC_USERCODE_OPTION_NAME);
145 + if ($user_code) {
146 + $usercode_attributes = sprintf(' data-usercode="%s"',$user_code);
147 + }
43 148
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);
149 + $setting_attributes = '';
150 + if (isset($CODOC_SETTINGS['codoc_script_tag_attributes']) and $CODOC_SETTINGS['codoc_script_tag_attributes']) {
151 + $setting_attributes = $CODOC_SETTINGS['codoc_script_tag_attributes'];
152 + }
153 + //return str_replace(' src=', $data_css . ' defer src=', $tag);
154 + return preg_replace('/(src=[^>]+)/',' ${1} ' . $data_css . $connect_attributes . $usercode_attributes . $setting_attributes . ' defer',$tag);
155 + }
156 + public function injector_shortcode($atts) {
157 + global $post;
158 + ob_start();
159 + require 'views/codoc-injector.php';
160 + return ob_get_clean();
161 + }
162 + # settings / 設定関連
163 + public function add_settings() {
164 + global $CODOC_SETTINGS;
165 + add_action( 'admin_menu' ,function(){
166 + add_options_page(
167 + 'codoc の設定', //ページタイトル
168 + 'codoc', //設定メニューに表示されるメニュータイトル
169 + 'edit_users', //権限
170 + 'codoc', //設定ページのURL。options-general.php?page=codoc
171 + function() {
172 + echo '<div class="codoc-settings">';
173 + echo '<form method="post" action="options.php">';
174 + settings_fields( 'codoc_option_group' );
175 + do_settings_sections( 'codoc' );
176 + submit_button(); // 送信ボタン
177 + echo '</form></div>';
178 + }
179 + );
180 + });
69 181
70 - $headers['X-CodocToken'] = $token;
71 - $host = CODOC_URL;
72 - $sslverify = true;
182 + add_action('admin_print_styles', 'codoc_admin_styles');
183 + function codoc_admin_styles($hook) {
184 + wp_enqueue_style('codoc-options-style', plugins_url( 'codoc/css/codoc-options.css', __DIR__ ));
185 + }
186 + add_action( "admin_init", function() {
187 + global $CODOC_USERCODE;
188 + global $CODOC_SETTINGS;
189 + global $CODOC_TOKEN;
190 + global $CODOC_AUTHINFO;
191 + // codocからの認証データ処理
192 + if (isset($_GET['page']) and $_GET['page'] == 'codoc' and isset($_GET['fetch_token_key'])) {
193 + $key = sanitize_text_field($_GET['fetch_token_key']);
194 + $usercode = sanitize_text_field($_GET['usercode']);
195 + update_option(CODOC_USERCODE_OPTION_NAME,$usercode);
196 + //$data = $this->callAPI('GET','/token',[ "fetch_token_key" => $key ]);
197 + $data = $this->util->get_token([ "fetch_token_key" => $key ],["usercode" => $usercode, "token" => "1"]);
198 + if ($data->status and $token = $data->token) {
199 + update_option(CODOC_TOKEN_OPTION_NAME,$token);
200 + //$data = $this->callAPI('GET','');
201 + $data = $this->util->get_user_info([],["usercode" => $usercode, "token" => $token]);
202 + if ($data->status and $user = $data->user) {
203 + $this->update_codoc_authinfo($user);
204 + }
205 + #$current_url = (empty($_SERVER['HTTPS']) ? 'http://' : 'https://').$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
206 + #$current_url = preg_replace('/(.*)fetch_token_key.*/','${1}&codoc_auth_finished=1',$current_url);
207 + //add_settings_error( 'general', 'settings_updated', __( 'OK' ), 'success' );
208 + $current_url = admin_url('options-general.php') . '?page=codoc&codoc_auth_finished=1';
73 209
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 - });
210 + wp_redirect( $current_url);
211 + exit;
212 + }
213 + }
214 + $CODOC_USERCODE = get_option(CODOC_USERCODE_OPTION_NAME);
215 + $CODOC_SETTINGS = get_option(CODOC_SETTINGS_OPTION_NAME);
216 + $CODOC_TOKEN = get_option(CODOC_TOKEN_OPTION_NAME);
217 + $CODOC_AUTHINFO = get_option(CODOC_AUTHINFO_OPTION_NAME);
218 + if( !$CODOC_SETTINGS ) {
219 + //デフォルト値
220 + $CODOC_SETTINGS = array(
221 + 'css_path' => '',
222 + );
223 + update_option( CODOC_SETTINGS_OPTION_NAME, $CODOC_SETTINGS );
224 + }
225 + if (!isset($CODOC_SETTINGS['str_replace_binded_url_from'])) {
226 + $CODOC_SETTINGS['str_replace_binded_url_from'] = '';
227 + }
228 + if (!isset($CODOC_SETTINGS['str_replace_binded_url_to'])) {
229 + $CODOC_SETTINGS['str_replace_binded_url_to'] = '';
230 + }
231 + if (!isset($CODOC_SETTINGS['str_before_codoc_tag'])) {
232 + $CODOC_SETTINGS['str_before_codoc_tag'] = '';
233 + }
234 + if (!isset($CODOC_SETTINGS['str_after_codoc_tag'])) {
235 + $CODOC_SETTINGS['str_after_codoc_tag'] = '';
236 + }
127 237
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 - }
238 + if (!isset($CODOC_SETTINGS['always_show_support'])) {
239 + $CODOC_SETTINGS['always_show_support'] = '0';
240 + }
241 + if (!isset($CODOC_SETTINGS['show_support_message'])) {
242 + $CODOC_SETTINGS['show_support_message'] = '';
243 + }
244 + if (!isset($CODOC_SETTINGS['show_support_categories'])) {
245 + $CODOC_SETTINGS['show_support_categories'] = '';
246 + }
247 + if (!isset($CODOC_SETTINGS['show_support_location'])) {
248 + $CODOC_SETTINGS['show_support_location'] = 'bottom';
249 + }
250 + if (!isset($CODOC_SETTINGS['do_not_filter_the_content'])) {
251 + $CODOC_SETTINGS['do_not_filter_the_content'] = '0';
252 + }
253 + if (!isset($CODOC_SETTINGS['shortcode_evacuation'])) {
254 + $CODOC_SETTINGS['shortcode_evacuation'] = '0';
255 + }
153 256
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 - );
257 + if (!isset($CODOC_SETTINGS['entry_button_text'])) {
258 + $CODOC_SETTINGS['entry_button_text'] = '';
259 + }
260 + if (!isset($CODOC_SETTINGS['subscription_button_text'])) {
261 + $CODOC_SETTINGS['subscription_button_text'] = '';
262 + }
263 + if (!isset($CODOC_SETTINGS['support_button_text'])) {
264 + $CODOC_SETTINGS['support_button_text'] = '';
265 + }
266 + if (!isset($CODOC_SETTINGS['subscription_message'])) {
267 + $CODOC_SETTINGS['subscription_message'] = '';
268 + }
269 + if (!isset($CODOC_SETTINGS['support_message'])) {
270 + $CODOC_SETTINGS['support_message'] = '';
271 + }
272 + if (!isset($CODOC_SETTINGS['show_like'])) {
273 + $CODOC_SETTINGS['show_like'] = '1';
274 + }
275 + if (!isset($CODOC_SETTINGS['show_about_codoc'])) {
276 + $CODOC_SETTINGS['show_about_codoc'] = '1';
277 + }
278 + if (!isset($CODOC_SETTINGS['show_powered_by'])) {
279 + $CODOC_SETTINGS['show_powered_by'] = '1';
280 + }
281 + if (!isset($CODOC_SETTINGS['show_created_by'])) {
282 + $CODOC_SETTINGS['show_created_by'] = '1';
283 + }
284 + if (!isset($CODOC_SETTINGS['show_copyright'])) {
285 + $CODOC_SETTINGS['show_copyright'] = '1';
286 + }
287 + if (!isset($CODOC_SETTINGS['codoc_tag_attributes'])) {
288 + $CODOC_SETTINGS['codoc_tag_attributes'] = '';
289 + }
290 + if (!isset($CODOC_SETTINGS['codoc_script_tag_attributes'])) {
291 + $CODOC_SETTINGS['codoc_script_tag_attributes'] = '';
292 + }
293 + if (!isset($CODOC_SETTINGS['codoc_connect_code'])) {
294 + $CODOC_SETTINGS['codoc_connect_code'] = '';
295 + }
296 + if (!isset($CODOC_SETTINGS['codoc_connect_registration_mode'])) {
297 + $CODOC_SETTINGS['codoc_connect_registration_mode'] = '';
298 + }
299 + if (!isset($CODOC_SETTINGS['debug_params'])) {
300 + $CODOC_SETTINGS['debug_params'] = '';
301 + }
302 + add_settings_section(
303 + 'setting_section_id', // id
304 + __('codoc Settings','codoc'), // title
305 + function (){}, // callback
306 + 'codoc' // page
307 + );
308 + // 認証がある場合
309 + if ($CODOC_AUTHINFO) {
310 + // クリエイター情報だけ更新 (POST時に同期をとる)
311 + if (isset($_GET['page']) and $_GET['page'] == 'codoc' and isset($_GET['settings-updated'])) {
312 + $data = $this->util->get_user_info([],[
313 + "usercode" => get_option(CODOC_USERCODE_OPTION_NAME),
314 + "token" => get_option(CODOC_TOKEN_OPTION_NAME),
315 + ]);
316 + if ($data->status and $user = $data->user) {
317 + $this->update_codoc_authinfo($user);
318 + // 一度get_optionしてるのでリロードする
319 + $CODOC_AUTHINFO = get_option(CODOC_AUTHINFO_OPTION_NAME);
320 + }
321 + }
192 322
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';";
323 + register_setting(
324 + 'codoc_option_group', // option group
325 + CODOC_SETTINGS_OPTION_NAME // option name(DB)
326 + );
327 + add_settings_field(
328 + 'css_path', // id
329 + __('Theme','codoc'), // title
330 + function() {
331 + global $CODOC_SETTINGS;
332 + echo '<div class="excerpt">' . __('You can change the design of the paywall by specifying a theme.<br />You can also specify CSS directly by selecting "Path Specification"','codoc') . '</div>';
333 + echo sprintf('<div class="inputGroup"><input type="text" name="%s[css_path]" value="%s" id="codoc_css_path" readonly>',CODOC_SETTINGS_OPTION_NAME,$CODOC_SETTINGS['css_path']);
334 + echo '' .
335 + '<script type="text/javascript"> ' .
336 + ' function gen_css_path(select) { ' .
337 + ' if (select.value == "dark" || select.value == "dark-square") { ' .
338 + ' document.getElementById("darkmode-caution").style.display="block";' .
339 + ' } else { ' .
340 + ' if (document.getElementById("darkmode-caution")) { ' .
341 + ' document.getElementById("darkmode-caution").style.display="none";' .
342 + ' } ' .
343 + ' } ' .
344 + ' if (select.value == "path") { ' .
345 + ' document.getElementById("codoc_css_path").readOnly=false; ' .
346 + ' if (!document.getElementById("codoc_css_path").value.match(/\//g)) {' .
347 + ' document.getElementById("codoc_css_path").value=""; ' .
348 + ' } ' .
349 + ' } else { ' .
350 + ' document.getElementById("codoc_css_path").readOnly=true; ' .
351 + ' document.getElementById("codoc_css_path").value=select.value; ' .
352 + ' } ' .
353 + ' } ' .
354 + '</script> ' ;
214 355
215 - echo sprintf('<p><font color="green" id="codoc-auth-message"><strong>%s</strong> で認証済</font></p>',$CODOC_AUTHINFO['email']);
356 + echo sprintf('<select name="theme_name" onChange="gen_css_path(this)" id="codoc_theme_select">');
357 + foreach (["rainbow","blue","red","green","black","dark","rainbow-square","blue-square","red-square","green-square","black-square","dark-square"] as $theme) {
358 + if ($theme == "rainbow") {
359 + echo sprintf('<option value="%s" %s>%s</option>', $theme, ($CODOC_SETTINGS["css_path"] == "rainbow" ? "selected" : ""),__('Rainbow colors','codoc'));
360 + }
361 + if ($theme == "blue") {
362 + echo sprintf('<option value="%s" %s>%s</option>', $theme, ($CODOC_SETTINGS["css_path"] == "blue" ? "selected" : ""),__('Blue','codoc'),);
363 + }
364 + if ($theme == "red") {
365 + echo sprintf('<option value="%s" %s>%s</option>', $theme, ($CODOC_SETTINGS["css_path"] == "red" ? "selected" : ""),__('Red','codoc'));
366 + }
367 + if ($theme == "green") {
368 + echo sprintf('<option value="%s" %s>%s</option>', $theme, ($CODOC_SETTINGS["css_path"] == "green" ? "selected" : ""),__('Green','codoc'));
369 + }
370 + if ($theme == "black") {
371 + echo sprintf('<option value="%s" %s>%s</option>', $theme, ($CODOC_SETTINGS["css_path"] == "black" ? "selected" : ""),__('Black','codoc'));
372 + }
373 + if ($theme == "dark") {
374 + echo sprintf('<option value="%s" %s>%s</option>', $theme, ($CODOC_SETTINGS["css_path"] == "dark" ? "selected" : ""),__('Dark mode','codoc'));
375 + }
376 + if ($theme == "rainbow-square") {
377 + echo sprintf('<option value="%s" %s>%s</option>', $theme, ($CODOC_SETTINGS["css_path"] == "rainbow-square" ? "selected" : ""),__("Rainbow Colors / Square design",'codoc'));
378 + }
379 + if ($theme == "blue-square") {
380 + echo sprintf('<option value="%s" %s>%s</option>', $theme, ($CODOC_SETTINGS["css_path"] == "blue-square" ? "selected" : ""),__('Blue / Square design','codoc'));
381 + }
382 + if ($theme == "red-square") {
383 + echo sprintf('<option value="%s" %s>%s/option>', $theme, ($CODOC_SETTINGS["css_path"] == "red-square" ? "selected" : ""),__('Red / Square design','codoc'));
384 + }
385 + if ($theme == "green-square") {
386 + echo sprintf('<option value="%s" %s>%s</option>', $theme, ($CODOC_SETTINGS["css_path"] == "green-square" ? "selected" : ""),__('Green / Square design','codoc'));
387 + }
388 + if ($theme == "black-square") {
389 + echo sprintf('<option value="%s" %s>%s</option>', $theme, ($CODOC_SETTINGS["css_path"] == "black-square" ? "selected" : ""),__('Black / Square design','codoc'));
390 + }
391 + if ($theme == "dark-square") {
392 + echo sprintf('<option value="%s" %s>%s</option>', $theme, ($CODOC_SETTINGS["css_path"] == "dark-square" ? "selected" : ""),__('Dark mode / Square design','codoc'));
393 + }
394 + }
395 + echo sprintf('<option value="path" %s>%s</option>', (preg_match("/\//",$CODOC_SETTINGS["css_path"]) ? "selected" : ""),__('Path Specification','codoc'));
396 + echo '<script type="text/javascript">gen_css_path(document.getElementById(\'codoc_theme_select\'))</script>';
397 + echo sprintf('</select></div>');
398 + echo '<p id="darkmode-caution" style="display:none">' . __('Please note that the theme for dark mode has white text color, and depending on the background color, the text may not be visible.','codoc'). '</p>';
399 + },
400 + 'codoc', //page
401 + 'setting_section_id' //Section
402 + );
403 + add_settings_field(
404 + 'paywall_text', // id
405 + __('Texts in paywall','codoc'), // title
406 + function() {
407 + global $CODOC_SETTINGS;
408 + global $CODOC_AUTHINFO;
409 + echo '<div class="excerpt">' . __('You can change, show and hide texts in your paywall.','codoc') . '</div>';
410 + echo '<table class="innerTable"><tbody>';
411 + echo '<tr><th>' . __('Display of likes','codoc') . '</th><td>';
412 + echo sprintf('<input type="radio" value="1" name="%s[show_like]" id="show_like_on" %s><label for="show_like_on">' . __('Enable','codoc') . '</label> ',CODOC_SETTINGS_OPTION_NAME,($CODOC_SETTINGS['show_like'] == '1' ? "checked" : ""));
413 + echo sprintf('<input type="radio" value="0" name="%s[show_like]" id="show_like_off" %s><label for="show_like_off">' . __('Disable','codoc') . '</label> <br / >',CODOC_SETTINGS_OPTION_NAME,($CODOC_SETTINGS['show_like'] == '0' ? "checked" : ""));
414 + echo '</td></tr>';
216 415
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 - }
416 + echo '<tr><th>' . __('Display of PoweredBy','codoc') . '</th><td>';
417 + echo sprintf('<input type="hidden" value="1" name="%s[show_about_codoc]">',CODOC_SETTINGS_OPTION_NAME);
418 + echo sprintf('<input type="hidden" value="1" name="%s[show_created_by]">',CODOC_SETTINGS_OPTION_NAME);
419 + echo sprintf('<input type="hidden" value="1" name="%s[show_powered_by]">',CODOC_SETTINGS_OPTION_NAME);
420 + if (isset($CODOC_AUTHINFO['account_is_pro']) and $CODOC_AUTHINFO['account_is_pro']) {
421 + echo sprintf('<input type="radio" value="1" name="%s[show_copyright]" id="show_copyright_on" %s><label for="show_copyright_on">' . __('Enable','codoc') . '</label> ',CODOC_SETTINGS_OPTION_NAME,($CODOC_SETTINGS['show_copyright'] == '1' ? "checked" : ""));
422 + echo sprintf('<input type="radio" value="0" name="%s[show_copyright]" id="show_copyright_off" %s><label for="show_copyright_off">' . __('Disable','codoc') . '</label> <br / >',CODOC_SETTINGS_OPTION_NAME,($CODOC_SETTINGS['show_copyright'] == '0' ? "checked" : ""));
423 + } else {
424 + echo __('Only PRO accounts can be disabled.','codoc');
425 + }
426 + echo '</td></tr>';
427 +
428 + echo '<tr><th>' . __('"Purchase Article" button text','codoc') . '</th><td>';
429 + echo sprintf('<input type="text" name="%s[entry_button_text]" value="%s">',CODOC_SETTINGS_OPTION_NAME,esc_html($CODOC_SETTINGS['entry_button_text']));
430 + echo '</td></tr>';
431 +
432 + echo '<tr><th>' . __('"Purchase Subscription" button text.','codoc') . '</th><td>';
433 + echo sprintf('<input type="text" name="%s[subscription_button_text]" value="%s">',CODOC_SETTINGS_OPTION_NAME,esc_html($CODOC_SETTINGS['subscription_button_text']));
434 + echo '</td></tr>';
226 435
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 -
436 + echo '<tr><th>' . __('Support button text','codoc') . '</th><td>';
437 + echo sprintf('<input type="text" name="%s[support_button_text]" value="%s">',CODOC_SETTINGS_OPTION_NAME,esc_html($CODOC_SETTINGS['support_button_text']));
438 + echo '</td></tr>';
439 +
440 + echo '<tr><th>' . __('Text for "Articles Included in Subscription".','codoc') . '</th><td>';
441 + echo sprintf('<input type="text" name="%s[subscription_message]" value="%s">',CODOC_SETTINGS_OPTION_NAME,esc_html($CODOC_SETTINGS['subscription_message']));
442 + echo '</td></tr>';
443 +
444 + echo '<tr><th>' . __('Text for support description','codoc') . '</th><td>';
445 + echo sprintf('<input type="text" name="%s[support_message]" value="%s">',CODOC_SETTINGS_OPTION_NAME,esc_html($CODOC_SETTINGS['support_message']));
446 + echo '</td></tr>';
447 +
448 + echo '</tbody></table>';
449 + },
450 + 'codoc', //page
451 + 'setting_section_id' //Section
452 + );
453 +
454 + add_settings_field(
455 + 'codoc_tag_attributes', // id
456 + __('Attributes for codoc tag','codoc'), // title
457 + function() {
458 + global $CODOC_SETTINGS;
459 + echo '<div class="excerpt">' . __('You can add specific attributes to codoc tags.','codoc') . '</div>';
460 + echo sprintf('<input type="text" name="%s[codoc_tag_attributes]" value="%s">',CODOC_SETTINGS_OPTION_NAME,htmlspecialchars($CODOC_SETTINGS['codoc_tag_attributes']));
461 + },
462 + 'codoc', //page
463 + 'setting_section_id' //Section
464 + );
465 + add_settings_field(
466 + 'codoc_script_tag_attributes', // id
467 + __('Attributes for codoc script tag','codoc'), // title
468 + function() {
469 + global $CODOC_SETTINGS;
470 + echo '<div class="excerpt">' . __('You can add specific attributes to codoc script tags.','codoc') . '</div>';
471 + echo sprintf('<input type="text" name="%s[codoc_script_tag_attributes]" value="%s">',CODOC_SETTINGS_OPTION_NAME,htmlspecialchars($CODOC_SETTINGS['codoc_script_tag_attributes']));
472 + },
473 + 'codoc', //page
474 + 'setting_section_id' //Section
475 + );
476 + add_settings_field(
477 + 'str_replace_binded_url', // id
478 + __('Permalink','codoc'), // title
479 + function() {
480 + global $CODOC_SETTINGS;
481 + echo '<div class="excerpt">' . __('You can replace the part of permalink registered on the codoc.','codoc') . '</div>';
482 + echo sprintf('<input type="text" name="%s[str_replace_binded_url_from]" value="%s">',CODOC_SETTINGS_OPTION_NAME,esc_html($CODOC_SETTINGS['str_replace_binded_url_from']));
483 + echo ('<span class="suptext">→</span>');
484 + echo sprintf('<input type="text" name="%s[str_replace_binded_url_to]" value="%s">',CODOC_SETTINGS_OPTION_NAME,esc_html($CODOC_SETTINGS['str_replace_binded_url_to']));
244 485
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'];
486 + },
487 + 'codoc', //page
488 + 'setting_section_id' //Section
489 + );
357 490
358 - require 'views/codoc-shortcodes-list.php';
491 + add_settings_field(
492 + 'str_around_codoc_tag', // id
493 + __('HTML insertion','codoc'), // title
494 + function() {
495 + global $CODOC_SETTINGS;
496 + echo '<div class="excerpt">' . __('You can insert HTML before and after the codoc tag.','codoc') . '</div>';
497 + echo sprintf('<div class="inputRow"><p>%s</p><textarea name="%s[str_before_codoc_tag]" cols="40">%s</textarea></div>',__('Before HTML','codoc'),CODOC_SETTINGS_OPTION_NAME,esc_html($CODOC_SETTINGS['str_before_codoc_tag']));
498 + echo sprintf('<div class="inputRow"><p>%s</p><textarea name="%s[str_after_codoc_tag]" cols="40">%s</textarea></div>',__('After HTML','codoc'),CODOC_SETTINGS_OPTION_NAME,esc_html($CODOC_SETTINGS['str_after_codoc_tag']));
359 499
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 - # }
500 + },
501 + 'codoc', //page
502 + 'setting_section_id' //Section
503 + );
391 504
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';
505 + add_settings_field(
506 + 'always_show_support_tag', // id
507 + __('Automatic support insertion','codoc'), // title
508 + function() {
509 + global $CODOC_SETTINGS;
510 + echo '<div class="excerpt">' . __('It adds support functions to the post without inserting a codoc block.','codoc') . '</div>';
511 + echo '<table class="innerTable"><tbody>';
512 + echo '<tr><th>' . __('Automatic insertion','codoc') . '</th><td>';
513 + echo sprintf('<input type="radio" value="1" name="%s[always_show_support]" id="always_show_support_on" %s><label for="always_show_support_on">' . __('Enable','codoc') . '</label> ',CODOC_SETTINGS_OPTION_NAME,($CODOC_SETTINGS['always_show_support'] == '1' ? "checked" : ""));
514 + echo sprintf('<input type="radio" value="0" name="%s[always_show_support]" id="always_show_support_off" %s><label for="always_show_support_off">' . __('Disable','codoc') . '</label> <br / >',CODOC_SETTINGS_OPTION_NAME,($CODOC_SETTINGS['always_show_support'] == '0' ? "checked" : ""));
515 + echo '</td></tr>';
516 + echo '<tr><th>'. __('Position','codoc') . '</th><td>';
517 + echo sprintf('<input type="radio" value="top" name="%s[show_support_location]" id="show_support_location_top" %s><label for="show_support_location_top">' . __('TOP','codoc') . '</label> ',CODOC_SETTINGS_OPTION_NAME,($CODOC_SETTINGS['show_support_location'] == 'top' ? "checked" : ""));
518 + echo sprintf('<input type="radio" value="bottom" name="%s[show_support_location]" id="show_support_location_bottom" %s><label for="show_support_location_bottom">' . __('Bottom','codoc') . '</label> <br / >',CODOC_SETTINGS_OPTION_NAME,($CODOC_SETTINGS['show_support_location'] == 'bottom' ? "checked" : ""));
519 + echo '</td></tr>';
520 + echo '<tr><th>' . __('Description','codoc') . '</th><td>';
521 + echo sprintf('<input type="text" placeholder="' . __('You can customize the description of the support.','codoc') . '" size="50%%" name="%s[show_support_message]" value="%s">',CODOC_SETTINGS_OPTION_NAME,esc_html($CODOC_SETTINGS['show_support_message']));
522 + echo '</td></tr>';
523 + echo '<tr><th>' . __('Category names','codoc') . '</th><td>';
524 + echo sprintf('<input placeholder="' . __('Inserted into articles of categories, divided by &quot;|&quot;','codoc') . '" type="text" size="50%%" name="%s[show_support_categories]" value="%s"><br />',CODOC_SETTINGS_OPTION_NAME,esc_html($CODOC_SETTINGS['show_support_categories']));
525 + echo '</td></tr>';
526 + echo '</tbody></table>';
429 527
430 - $boundary = wp_generate_password(24);
528 + },
529 + 'codoc', //page
530 + 'setting_section_id' //Section
531 + );
532 + add_settings_field(
533 + 'do_not_filter_the_content', // id
534 + __('Disable plugin filter','codoc'), // title
535 + function() {
536 + global $CODOC_SETTINGS;
537 + echo '<div class="excerpt">' . __('You can disable filter processing that includes shortcodes from other plugins that interfere with the operation of codoc. Please use this only if codoc is not functioning properly.','codoc') . '</div>';
538 + echo sprintf('<input type="radio" value="1" name="%s[do_not_filter_the_content]" id="do_not_filter_the_content_on" %s><label for="do_not_filter_the_content_on">' . __('Enable','codoc') .'</label> ',CODOC_SETTINGS_OPTION_NAME,($CODOC_SETTINGS['do_not_filter_the_content'] == '1' ? "checked" : ""));
539 + echo sprintf('<input type="radio" value="0" name="%s[do_not_filter_the_content]" id="do_not_filter_the_content_off" %s><label for="do_not_filter_the_content_off">' . __('Disable','codoc') . '</label> ',CODOC_SETTINGS_OPTION_NAME,($CODOC_SETTINGS['do_not_filter_the_content'] == '0' ? "checked" : ""));
540 + },
541 + 'codoc', //page
542 + 'setting_section_id' //Section
543 + );
431 544
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 - }
545 + add_settings_field(
546 + 'shortcode_evacuation', // id
547 + __('Shortcode evacuation','codoc'), // title
548 + function() {
549 + global $CODOC_SETTINGS;
550 + echo '<div class="excerpt">' . __('The execution results of shortcodes written in the paid part are moved to the free part, and are used and displayed on the HTML during viewing of the paid part. Please use this if the shortcode does not work as expected in the paid part. Please note that the execution results of shortcodes in the paid part will be written in the source code.','codoc'). '</div>';
551 + echo sprintf('<input type="radio" value="1" name="%s[shortcode_evacuation]" id="shortcode_evacuation_on" %s><label for="shortcode_evacuation_on">' . __('Enable','codoc') . '</label> ',CODOC_SETTINGS_OPTION_NAME,($CODOC_SETTINGS['shortcode_evacuation'] == '1' ? "checked" : ""));
552 + echo sprintf('<input type="radio" value="0" name="%s[shortcode_evacuation]" id="shortcode_evacuation_off" %s><label for="shortcode_evacuation_off">' . __('Disable','codoc') . '</label> ',CODOC_SETTINGS_OPTION_NAME,($CODOC_SETTINGS['shortcode_evacuation'] == '0' ? "checked" : ""));
553 + },
554 + 'codoc', //page
555 + 'setting_section_id' //Section
556 + );
490 557
491 -
558 + add_settings_field(
559 + 'debug_params', // id
560 + __('Debug Parameters','codoc'), // title
561 + function() {
562 + global $CODOC_SETTINGS;
563 + echo '<div class="excerpt">' . __('You can specify parameters for debugging. Please use this only upon request from codoc support.','codoc') . '</div>';
564 + echo sprintf('<input placeholder="" type="text" size="50%%" name="%s[debug_params]" value="%s"><br />',CODOC_SETTINGS_OPTION_NAME,esc_html($CODOC_SETTINGS['debug_params']));
565 + },
566 + 'codoc', //page
567 + 'setting_section_id' //Section
568 + );
569 +
570 + add_settings_field(
571 + 'cretor_info', // id
572 + __('Creator\'s Information','codoc'), // title
573 + function() {
574 + global $CODOC_SETTINGS;
575 + global $CODOC_AUTHINFO;
576 + echo sprintf('<p><font id="codoc-update-creator-info-message"></font></p>',"");
577 + // 変更を保存時に常に同期するのでこの表示は必要ないがUI上保持しておく
578 + $script = "document.getElementById('codoc-update-creator-info-message').innerText='" . __("Please save changes to update the information.","codoc") . "';document.getElementById('codoc-update-creator-info-message').color='red';";
579 + if (isset($CODOC_AUTHINFO['profile_image_url'])) {
580 + echo sprintf('<img src="%s" width="40" height="40" />',$CODOC_AUTHINFO['profile_image_url']);
581 + }
582 + echo sprintf('<p>%s%s</p>',$CODOC_AUTHINFO['name'],((isset($CODOC_AUTHINFO['account_is_pro']) and $CODOC_AUTHINFO['account_is_pro']) ? ' [PRO] ' : ''));
583 + if ($connect_code = $CODOC_SETTINGS['codoc_connect_code']) {
584 +
585 + echo sprintf('<p>' . __('External service integration completed (Integration code: %s) %s','codoc') . '</p>',
586 + $connect_code,
587 + ($CODOC_SETTINGS['codoc_connect_registration_mode'] == 'dedicated' ? '<br/> <strong>' . __('Set the audience as a private account.','codoc') . '</strong>' : ''));
588 + }
589 + echo sprintf('<p><a href="javascript:void(0);" onClick="' . $script . '">' . __('Update creator\'s Information','codoc') . '</a></p>');
590 + echo ('<p>' . __('Please update each time if you change the logo or cover image on the codoc side.','codoc') . '</p>');
591 + },
592 + 'codoc', //page
593 + 'setting_section_id' //Section
594 + );
595 +
596 + register_setting(
597 + 'codoc_option_group', // option group
598 + CODOC_USERCODE_OPTION_NAME
599 + );
600 + register_setting(
601 + 'codoc_option_group', // option group
602 + CODOC_TOKEN_OPTION_NAME
603 + );
604 +
605 + if (isset($_GET['codoc_auth_finished']) and $_GET['codoc_auth_finished']) {
606 + add_settings_error( 'general', 'settings_updated', __( 'codoc authentication has been completed.' ,'codoc'), 'success' );
607 + }
608 +
609 + add_settings_field(
610 + 'codoc_auth',
611 + __('Authentication','codoc'),
612 + function() {
613 + global $CODOC_USERCODE;
614 + global $CODOC_TOKEN;
615 + global $CODOC_AUTHINFO;
616 + $script = "javascript:document.getElementById('codoc-usercode').value='-';document.getElementById('codoc-token').value='-';document.getElementById('codoc-auth-message').innerText='" . __('Please save changes to complete the unbinding.','codoc') . "';document.getElementById('codoc-auth-message').color='red';";
617 +
618 + echo sprintf('<p><font color="green" id="codoc-auth-message">' . __('Authorized as <strong>%s</strong>','codoc') . '</font></p>',$CODOC_AUTHINFO['email']);
619 +
620 + echo sprintf('<input id="codoc-usercode" type="hidden" name="%s" value="%s">',CODOC_USERCODE_OPTION_NAME,$CODOC_USERCODE);
621 + echo sprintf('<input id="codoc-token" type="hidden" name="%s" value="%s">',CODOC_TOKEN_OPTION_NAME,$CODOC_TOKEN);
622 + echo sprintf('<p><a href="javascript:void(0);" onClick="' . $script . '">' . __('Unbind authorization','codoc') . '</p>');
623 + },
624 + 'codoc',
625 + 'setting_section_id'
626 + );
627 +
628 + }
629 +
630 + // ない場合
631 + if (!$CODOC_AUTHINFO and !isset($_GET['auth_by_myself'])) {
632 + add_settings_field(
633 + 'codoc_auth',
634 + '認証',
635 + function() {
636 + $theme = wp_get_theme();
637 + $from = 'wp';
638 + if ($theme->get('Name') === 'codoc') {
639 + $from = 'wp_codoc';
640 + }
641 + //$current_url = (empty($_SERVER['HTTPS']) ? 'http://' : 'https://').$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
642 + $current_url = admin_url('options-general.php') . '?page=codoc';
643 + $login_url = sprintf("location.href='%s'",$this->get_codoc_url() . '/me/token?from=' . $from . '&return_url=' . urlencode($current_url));
644 + $register_url = sprintf("location.href='%s'",$this->get_codoc_url() . '/register?from=' . $from . '&return_url=' . urlencode($current_url));
645 + // submitを消しておく
646 + echo ('<script type="text/javascript">window.onload=function(){document.getElementById(\'submit\').style.display = \'none\'}</script>');
647 + echo ('codocをWordPressでご利用いただくには認証が必要です。<br />');
648 + echo sprintf('ユーザーコードとAPIトークンを<a href="%s&auth_by_myself=1">直接入力</a>することでも認証できます。<br /><br />',$current_url);
649 + 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);
650 + if (!$this->util->health_check()) {
651 + echo sprintf('<p style="color: red;">codocサーバーと通信ができません。サーバーやWordPress側でのファイヤーウォール設定にて https://codoc.jp への通信を許可してください。</p>');
652 + }
653 +
654 + },
655 + 'codoc',
656 + 'setting_section_id'
657 + );
658 + }
659 + // ない場合かつ自分で認証する場合
660 + if (!$CODOC_AUTHINFO and (
661 + (isset($_GET['auth_by_myself']) and $_GET['auth_by_myself']) or
662 + (isset($_POST['auth_by_myself']) and $_POST['auth_by_myself'])
663 + )) {
664 + register_setting(
665 + 'codoc_option_group', // option group
666 + CODOC_USERCODE_OPTION_NAME
667 + );
668 + register_setting(
669 + 'codoc_option_group', // option group
670 + CODOC_TOKEN_OPTION_NAME
671 + );
672 + add_settings_field(
673 + 'codoc_usercode',
674 + 'ユーザーコード',
675 + function() {
676 + global $CODOC_USERCODE;
677 + echo '<input type="hidden" name="auth_by_myself" value="1">';
678 + echo sprintf('<input type="text" name="%s" value="%s">',CODOC_USERCODE_OPTION_NAME,$CODOC_USERCODE);
679 + },
680 + 'codoc',
681 + 'setting_section_id'
682 + );
683 + add_settings_field(
684 + 'codoc_token',
685 + 'APIトークン',
686 + function() {
687 + global $CODOC_TOKEN;
688 + echo sprintf('<input type="text" name="%s" value="%s">',CODOC_TOKEN_OPTION_NAME,$CODOC_TOKEN);
689 + },
690 + 'codoc',
691 + 'setting_section_id'
692 + );
693 + }
694 +
695 + // 認証情報を保存
696 + add_action( 'update_option_' . CODOC_USERCODE_OPTION_NAME, function( $old_value, $new_value ) {
697 + global $CODOC_RE_AUTHORIZE;
698 + $CODOC_RE_AUTHORIZE = 1;
699 + },9,2); // $hook, $function_to_add, $priority, $accepted_args
700 + add_action( 'update_option_' . CODOC_TOKEN_OPTION_NAME, function( $old_value, $new_value ) {
701 + global $CODOC_RE_AUTHORIZE;
702 + $CODOC_RE_AUTHORIZE = 1;
703 + },10,2);
704 +
705 + add_action('update_option_' . CODOC_SETTINGS_OPTION_NAME,function(){
706 + $CODOC_SETTINGS = get_option(CODOC_SETTINGS_OPTION_NAME);
707 + if (isset($CODOC_SETTINGS['always_show_support']) and $CODOC_SETTINGS['always_show_support']) {
708 + $data = $this->util->get_support_entry([],[
709 + "usercode" => get_option(CODOC_USERCODE_OPTION_NAME),
710 + "token" => get_option(CODOC_TOKEN_OPTION_NAME),
711 + ]);
712 + if ($data and $data->status and $entry = $data->entry) {
713 + update_option(CODOC_SUPPORT_ENTRYCODE_OPTION_NAME,$entry->code);
714 + } else {
715 + update_option(CODOC_SUPPORT_ENTRYCODE_OPTION_NAME,'');
716 + }
717 + } elseif(isset($CODOC_SETTINGS['always_show_support']) and !$CODOC_SETTINGS['always_show_support']) {
718 + update_option(CODOC_SUPPORT_ENTRYCODE_OPTION_NAME,'');
719 + }
720 + });
721 +
722 + add_action('updated_option',function(){
723 + global $CODOC_RE_AUTHORIZE;
724 + if ($CODOC_RE_AUTHORIZE) {
725 + //$data = $this->callAPI('GET','');
726 + $data = $this->util->get_user_info([],[
727 + "usercode" => get_option(CODOC_USERCODE_OPTION_NAME),
728 + "token" => get_option(CODOC_TOKEN_OPTION_NAME),
729 + ]);
730 + if ($data->status and $user = $data->user) {
731 + $this->update_codoc_authinfo($user);
732 + } else {
733 + update_option(CODOC_AUTHINFO_OPTION_NAME,'');
734 + }
735 + }
736 + });
737 +
738 + });
739 + // プラグイン一覧に設定のリンクをいれる
740 + add_filter( 'plugin_action_links_' . plugin_basename( plugin_dir_path( __FILE__ ) . 'codoc' . '.php' ),
741 + function( $links ) {
742 + $setting_link = sprintf( '<a href="%s">%s</a>', esc_url( add_query_arg( 'page', 'codoc', admin_url( 'options-general.php' ) ) ), esc_html( '設定' ) );
743 + array_unshift( $links, $setting_link );
744 +
745 + return $links;
746 + }
747 + );
748 +
749 + }
750 + public function add_mce() {
751 + add_action( 'admin_enqueue_scripts', function() {
752 + if ( ! current_user_can( 'edit_posts' ) && ! current_user_can( 'edit_pages' ) ) {
753 + return;
754 + }
755 + $current_screen = get_current_screen();
756 + if ( ( method_exists( $current_screen, 'is_block_editor' ) && $current_screen->is_block_editor() ) || ( function_exists( 'is_gutenberg_page' ) && is_gutenberg_page() ) ) {
757 + // Gutenberg Editor
758 + // なにもしない
759 + } else {
760 + // tinymce
761 + if ( get_user_option( 'rich_editing' ) == 'true' ) {
762 + // プラグイン内で使うCSRF を生成
763 + $path = plugins_url( 'codoc/src_mce/codoc-editor-onload.js', __DIR__ );
764 + wp_enqueue_script( 'codoc-editor-onload', $path, array('jquery'), '', true );
765 + // I just want to insert this global variables but i don't know how i can do it..
766 + wp_localize_script(
767 + 'codoc-editor-onload',
768 + 'CODOCEDITOR',
769 + array(
770 + 'action' => 'codoc_shortcodes',
771 + 'nonce' => wp_create_nonce( 'codoc_shortcodes' ),
772 +
773 + 'codoc_url' => $this->get_codoc_url(),
774 + 'codoc_usercode' => get_option(CODOC_USERCODE_OPTION_NAME),
775 + 'codoc_plugin_version' => CODOC_PLUGIN_VERSION,
776 + 'codoc_sdk_path' => CODOC_SDK_PATH,
777 + )
778 + );
779 +
780 + // ここでtinymceのプラグイン追加
781 + //wp_enqueue_style( 'codoc-admin-style', plugins_url( 'codoc/src_mce/codoc-admin.css', __DIR__ ) );
782 + //add_editor_style( plugins_url( 'codoc/src_mce/codoc-admin.css', __DIR__ ) );
783 + wp_enqueue_style( 'codoc-admin-style', $this->get_codoc_url() . CODOC_SDK_PATH . '.tinymce.css?c=' . date('Ymd') );
784 + add_editor_style( $this->get_codoc_url() . CODOC_SDK_PATH . '.tinymce.css' );
785 +
786 + add_filter( 'mce_external_plugins', function( $plugin_array ) {
787 + //$plugin_array['codoc'] = plugins_url( 'codoc/src_mce/codoc-editor.js', __DIR__ );
788 + $plugin_array['codoc'] = $this->get_codoc_url() . CODOC_SDK_PATH . '.tinymce.js?c=' . date('Ymd');
789 + return $plugin_array;
790 + } );
791 + add_filter( 'mce_buttons', function( $buttons ) {
792 + array_push( $buttons, "|", "codoc" );
793 + return $buttons;
794 + } );
795 + // 非SSL環境の場合、なぜかhttps -> httpsになってしまうので対策
796 + // コメントタグが除去されることがあるらしいのでvalid_elementsに必要なタグを追加 (EXPERIMENTAL)
797 + add_filter( 'tiny_mce_before_init', function($settings) {
798 + $settings['external_plugins'] = preg_replace('/"codoc":"http:/','"codoc":"https:',$settings['external_plugins']);
799 + foreach (['valid_elements','extended_valid_elements'] as $valid_elements) {
800 + if (isset($settings[$valid_elements])) {
801 + $settings[$valid_elements] = $settings[$valid_elements] . ',div[*],p[*],img[*],--[*]';
802 + }
803 + }
804 + $CODOC_SETTINGS = get_option(CODOC_SETTINGS_OPTION_NAME);
805 + if (isset($CODOC_SETTINGS["debug_params"])) {
806 + $params_decoded = json_decode($CODOC_SETTINGS["debug_params"],true);
807 + if (isset($params_decoded["mce_valid_elements"])) {
808 + $settings['valid_elements'] = $params_decoded["mce_valid_elements"];
809 + $settings['extended_valid_elements'] = $params_decoded["mce_valid_elements"];
810 + }
811 + }
812 +
813 + return $settings;
814 + },1000000);
815 + }
816 + }
817 + } );
818 + }
819 + function update_codoc_authinfo($user) {
820 + global $CODOC_SETTINGS;
821 + global $CODOC_AUTHINFO;
822 + if (property_exists($user,'connect_code') and $user->connect_code) {
823 + $CODOC_SETTINGS = get_option(CODOC_SETTINGS_OPTION_NAME);
824 + $CODOC_SETTINGS['codoc_connect_code'] = $user->connect_code;
825 + $CODOC_SETTINGS['codoc_connect_registration_mode'] = $user->connect_has_permission_dedicated_account ? 'dedicated' : '';
826 + update_option(CODOC_SETTINGS_OPTION_NAME,$CODOC_SETTINGS);
827 + }
828 + return update_option(CODOC_AUTHINFO_OPTION_NAME,[
829 + 'email' => $user->email,
830 + 'name' => $user->name,
831 + 'profile_image_url' => $user->profile_image_url,
832 + 'cover_image_url' => $user->cover_image_url,
833 + 'connect_code' => property_exists($user,'connect_code') ? $user->connect_code : '',
834 + 'connect_image_url' => property_exists($user,'connect_image_url') ? $user->connect_image_url : '',
835 + 'account_is_pro' => property_exists($user,'account_is_pro') ? $user->account_is_pro : '',
836 + 'created_at' => property_exists($user,'created_at') ? $user->created_at : 0,
837 + ]);
838 + $CODOC_AUTHINFO = get_option(CODOC_AUTHINFO_OPTION_NAME);
839 + return $CODOC_AUTHINFO;
840 + }
841 + // 保存用コンテンツにフィルターを実施する
842 + public function get_filtered_content($post_emulated) {
843 + // 記事ページであることをエミュレートしてフィルター実行
844 + $post_backuped = null;
845 + if (isset($GLOBALS['post'])) {
846 + $post_backuped = $GLOBALS['post'];
847 + }
848 + $GLOBALS['post'] = $post_emulated;
849 +
850 + $wp_query_backuped = null;
851 + if (isset($GLOBALS['wp_query'])) {
852 + $wp_query_backuped = $GLOBALS['wp_query'];
853 + $wp_query = $GLOBALS['wp_query'];
854 + $wp_query->is_single = true;
855 + $wp_query->is_singular = true;
856 + # in_the_loop は記事ページでは通常有効っぽいので true指定 ex: wp_ulike
857 + $wp_query->in_the_loop = true;
858 + # これも追加しておく
859 + $wp_query->is_main_query = true;
860 +
861 + $wp_query->post = $post_emulated;
862 + $wp_query->queried_object = $post_emulated;
863 + $wp_query->queried_object_id = $post_emulated->ID;
864 +
865 + $GLOBALS['wp_query'] = $wp_query;
866 + }
867 + // $this->the_content でオリジナルを返してもらうため
868 + $this->do_not_filter_the_content = true;
869 + $content = preg_replace(
870 + '/ (\/)?wp:codoc\/codoc-block /',' \\1wptmp:codoc/codoc-block ',
871 + $post_emulated->post_content
872 + );
873 + // post_metaに内容を保存し、無料パートにショートコードの退避をおこなう
874 + $codoc_settings = get_option(CODOC_SETTINGS_OPTION_NAME);
875 + if (isset($codoc_settings['shortcode_evacuation']) and $codoc_settings['shortcode_evacuation']) {
876 + $splited = preg_split('/\/wptmp:codoc\/codoc-block/s',$content);
877 + if (isset($splited[0]) and isset($splited[1])) {
878 + // すべてのショートコードを $match_all にいれる
879 + preg_match_all('/\[[^\[\]]+?\](.+\[\/[^\[\]]+?\])?/',$splited[1],$match_all);
880 + // ショートコードをURLエンコードしてdivタグの中にいれておく(後でmetaの中のショートコードと付け合せ)
881 + $replaced = preg_replace_callback('/\[[^\[\]]+?\](.+\[\/[^\[\]]+?\])?/',function($matches) {
882 + return sprintf ('<div class="codoc-evacuation-dests" data-shortcode="%s"></div>', urlencode($matches[0]));
883 + },$splited[1]);
884 + // 0番目に配列で入っているのでそこを退避対象の配列とする
885 + $evacuations = $match_all[0];
886 + // ショートコードの中身をタグに退避
887 + $content = $splited[0] . '/wptmp:codoc/codoc-block' . $replaced;
888 + // 無料パートでショートコードを実行できるようにする
889 + update_post_meta($post_emulated->ID,'codoc_shortcode_evacuations',join('**codoc**',$evacuations));
890 + } else {
891 + update_post_meta($post_emulated->id,'codoc_shortcode_evacuations',"");
892 + }
893 + }
894 + #$content = do_shortcode( $content );
895 + $content_filtered = apply_filters( 'the_content', $content);
896 + $this->do_not_filter_the_content = false;
897 +
898 + $GLOBALS['post'] = $post_backuped;
899 +
900 + if ($wp_query_backuped) {
901 + $GLOBALS['wp_query'] = $wp_query_backuped;
902 + }
903 +
904 + $content_filtered = preg_replace(
905 + '/ (\/)?wptmp:codoc\/codoc-block /',' \\1wp:codoc/codoc-block ',
906 + $content_filtered
907 + );
908 + return $content_filtered;
909 + }
910 +
911 + public function save_post($post_ID,$post,$update) {
912 + if (preg_match('/^(auto-draft|inherit)$/',$post->post_status)) {
913 + return;
914 + }
915 + $entryCode = get_post_meta($post_ID,'codoc_entry_code',true);
916 + $codoc_settings = get_option(CODOC_SETTINGS_OPTION_NAME);
917 + $post_content = (isset($codoc_settings['do_not_filter_the_content']) and $codoc_settings['do_not_filter_the_content']) ?
918 + $post->post_content : $this->get_filtered_content($post);
919 + $res = $this->util->sync_entry([
920 + "post_title" => $post->post_title,
921 + "post_content" => $post_content,
922 + // password が設定されてる場合は限定公開にする
923 + "post_status" => $post->post_status == 'publish' ? ($post->post_password ? 2 : 1) : 0,
924 + "post_permalink" => get_permalink($post_ID),
925 + "codoc_entry_code" => $entryCode,
926 + "codoc_settings" => $codoc_settings,
927 + ],[
928 + "usercode" => get_option(CODOC_USERCODE_OPTION_NAME),
929 + "token" => get_option(CODOC_TOKEN_OPTION_NAME),
930 + ]);
931 + $prudent_update_post_meta_entry_code = null;
932 + if (isset($codoc_settings["debug_params"])) {
933 + $params_decoded = json_decode($codoc_settings["debug_params"],true);
934 + if (isset($params_decoded["prudent_update_post_meta_entry_code"])) {
935 + $prudent_update_post_meta_entry_code = $params_decoded["prudent_update_post_meta_entry_code"];
936 + }
937 + }
938 + if ($prudent_update_post_meta_entry_code == 2) {
939 + error_log('CODOC:: ' . sprintf ("%s : %s : %s : %s", $post_ID, (is_object($res) ? "1" : 0),$res->status,$entryCode));
940 + }
941 + if (is_object($res) and $res->status and !$entryCode) {
942 + // 20230222 https://stackoverflow.com/questions/26640785/update-post-meta-not-work-only-when-save-data-not-for-update
943 + if ($prudent_update_post_meta_entry_code) {
944 + add_post_meta($post_ID,'codoc_entry_code',$res->entry->code);
945 + } else {
946 + update_post_meta($post_ID,'codoc_entry_code',$res->entry->code);
947 + }
948 + }
949 + return true;
950 + }
951 + function updated_post_meta( $meta_ID, $post_ID, $meta_key ) {
952 + // スルーされてる場合は他のメタ情報更新のタイミングにサムネイルをアップロード
953 + $has_to_resend = get_post_meta($post_ID,'codoc_post_thumbnail_invoking_entry_code',true);
954 + if ($has_to_resend != 1 and $meta_key != '_thumbnail_id') {
955 + return;
956 + }
957 + if ( has_post_thumbnail($post_ID) ) {
958 + // 新規投稿の場合、タイミングによってはcodocEntryCodeが取得できないので一旦スルーする
959 + if (!get_post_meta($post_ID,'codoc_entry_code',true)) {
960 + update_post_meta($post_ID,'codoc_post_thumbnail_invoking_entry_code',1);
961 + return;
962 + } else {
963 + update_post_meta($post_ID,'codoc_post_thumbnail_invoking_entry_code',0);
964 + }
965 + $attachment = wp_get_attachment_metadata( get_post_thumbnail_id($post_ID));
966 + $upload_dir = wp_upload_dir();
967 + $file_path = sprintf ('%s/%s',$upload_dir['basedir'],$attachment['file']);
968 +
969 + return $this->util->post_thumbnail([
970 + "file_path" => $file_path,
971 + "boundary" => wp_generate_password(24),
972 + "codoc_entry_code" => get_post_meta($post_ID,'codoc_entry_code',true),
973 + ],[
974 + "usercode" => get_option(CODOC_USERCODE_OPTION_NAME),
975 + "token" => get_option(CODOC_TOKEN_OPTION_NAME),
976 + ]);
977 + }
978 + }
979 + function deleted_post_meta( $meta_ids, $post_ID, $meta_key,$meta_value ) {
980 + if ($meta_key != '_thumbnail_id') {
981 + return;
982 + }
983 + return $this->util->reset_thumbnail(
984 + ["codoc_entry_code" => get_post_meta($post_ID,'codoc_entry_code',true)],
985 + [
986 + "usercode" => get_option(CODOC_USERCODE_OPTION_NAME),
987 + "token" => get_option(CODOC_TOKEN_OPTION_NAME),
988 + ]
989 + );
990 + }
991 + // 退避されてるかどうかをmetaを使って確認し、無料エリアの一番最後にショートコードを追加
992 + function the_content_shortcode_evacuations( $post_content ) {
993 + if ($this->do_not_filter_the_content) {
994 + return $post_content;
995 + }
996 + $post = get_post();
997 + if (!$post) {
998 + return $post_content;
999 + }
1000 + $evacuations_meta = get_post_meta($post->ID,'codoc_shortcode_evacuations',true);
1001 + $evacuations = preg_split('/\*\*codoc\*\*/',$evacuations_meta);
1002 + foreach ($evacuations as $evacuation) {
1003 + $post_content = sprintf('<div class="codoc-evacuations" style="display:none;" data-shortcode="%s">%s</div>',
1004 + urlencode($evacuation),$evacuation) . $post_content;
1005 + }
1006 + return $post_content;
1007 + }
1008 + function the_content( $post_content ) {
1009 + if ( is_single() && in_the_loop() && is_main_query() ) {
1010 + }
1011 + // get_filtered_content から do_not_filter_the_contentを有効にされるパターン
1012 + // オプションの設定値の意味合い(他のフィルタを無視)とは違うため注意
1013 + if ($this->do_not_filter_the_content) {
1014 + return $post_content;
1015 + }
1016 + $post = get_post();
1017 + // 20211215 null になる場合がある
1018 + if (!$post) {
1019 + return $post_content;
1020 + }
1021 + # is_amp で実装しているテンプレート用
1022 + $is_amp_endpoint = (function_exists('is_amp_endpoint') && is_amp_endpoint()) ? true :
1023 + ((function_exists('is_amp') && is_amp()) ? true : false);
1024 + return $this->util->filter_content([
1025 + "post_content" => $post_content,
1026 + "preview" => is_preview(),
1027 + "codoc_entry_code" => get_post_meta($post->ID,'codoc_entry_code',true),
1028 + "codoc_settings" => get_option(CODOC_SETTINGS_OPTION_NAME),
1029 + "is_amp_endpoint" => $is_amp_endpoint,
1030 + "post_permalink" => get_permalink($post->ID),
1031 + "codoc_support_entry_code" => get_option(CODOC_SUPPORT_ENTRYCODE_OPTION_NAME),
1032 + ]);
1033 + }
1034 +
1035 + function excerpt_allowed_blocks ($allowed_blocks) {
1036 + if (is_array($allowed_blocks)) {
1037 + array_push($allowed_blocks,'codoc/codoc-block');
1038 + }
1039 + return $allowed_blocks;
1040 + }
492 1041 }
493 -