'number', 'sanitize_callback' => array( $this, 'sanitize_price' ), 'default' => 0, ) ); register_setting( 'acscw_general', 'acscw_post_types', array( 'type' => 'array', 'sanitize_callback' => array( $this, 'sanitize_post_types' ), 'default' => array( 'post', 'page' ), ) ); register_setting( 'acscw_general', 'acscw_tip_amounts', array( 'type' => 'array', 'sanitize_callback' => array( $this, 'sanitize_tip_amounts' ), 'default' => array( 100, 500, 1000, 3000 ), ) ); register_setting( 'acscw_general', 'acscw_cookie_days', array( 'type' => 'integer', 'sanitize_callback' => array( $this, 'sanitize_cookie_days' ), 'default' => 90, ) ); // --- デザイン ----------------------------------------------------- register_setting( 'acscw_design', 'acscw_button_text', array( 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', 'default' => '', ) ); // --- 購入体験 ----------------------------------------------------- register_setting( 'acscw_checkout', 'acscw_simplify_checkout', array( 'type' => 'string', 'sanitize_callback' => array( $this, 'sanitize_yes_no' ), 'default' => 'yes', ) ); register_setting( 'acscw_checkout', 'acscw_cart_mode', array( 'type' => 'string', 'sanitize_callback' => array( $this, 'sanitize_cart_mode' ), 'default' => 'exclusive', ) ); // --- データ ------------------------------------------------------- register_setting( 'acscw_data', 'acscw_delete_data_on_uninstall', array( 'type' => 'string', 'sanitize_callback' => array( $this, 'sanitize_yes_no' ), 'default' => 'no', ) ); } /* * ----------------------------------------------------------------------- * サニタイズ * ----------------------------------------------------------------------- */ /** * 既定価格を整える。 * * @param mixed $value 入力値。 * @return float 0 以上。 */ public function sanitize_price( $value ) { return ACSCW_Products::normalize_amount( $value ); } /** * 対象投稿タイプを整える。 * * 実在し、かつ公開クエリ可能な投稿タイプだけを通す。 * 'product'(WooCommerce 商品)は自分が生成する商品と衝突するため除外する。 * * @param mixed $value 入力値。 * @return string[] */ public function sanitize_post_types( $value ) { $allowed = self::get_selectable_post_types(); $clean = array(); foreach ( (array) $value as $type ) { $type = sanitize_key( $type ); if ( isset( $allowed[ $type ] ) ) { $clean[] = $type; } } $clean = array_values( array_unique( $clean ) ); return empty( $clean ) ? array( 'post' ) : $clean; } /** * 投げ銭の金額セットを整える。 * * 保存後に商品を用意し直す。金額を増やしたのに商品が無い、という * 状態を管理者が気づけないまま作らないため。 * * @param mixed $value 入力値(カンマ区切り文字列または配列)。 * @return float[] */ public function sanitize_tip_amounts( $value ) { if ( ! is_array( $value ) ) { $value = explode( ',', (string) $value ); } $amounts = array(); foreach ( $value as $item ) { $amount = ACSCW_Products::normalize_amount( trim( (string) $item ) ); if ( $amount > 0 ) { $amounts[ (string) $amount ] = $amount; } } $amounts = array_values( $amounts ); sort( $amounts ); if ( empty( $amounts ) ) { $amounts = array( 100, 500, 1000, 3000 ); } // 上限を設けるのは、金額ごとに商品を1つ作るため(無制限に増やさせない)。 $amounts = array_slice( $amounts, 0, 8 ); add_action( 'shutdown', array( $this, 'ensure_tip_products' ) ); return $amounts; } /** * 保存された金額セットに対応する投げ銭商品を用意する。 * * サニタイズの中で作らないのは、値がまだ保存されていないため。 * * @return void */ public function ensure_tip_products() { $products = ACSCW_Plugin::instance()->get( 'products' ); if ( $products instanceof ACSCW_Products ) { $products->ensure_default_tip_products(); } } /** * アクセス保持日数を整える。 * * @param mixed $value 入力値。 * @return int 0〜3650。 */ public function sanitize_cookie_days( $value ) { return min( 3650, absint( $value ) ); } /** * yes / no に丸める。 * * @param mixed $value 入力値。 * @return string */ public function sanitize_yes_no( $value ) { return ( 'yes' === $value || '1' === (string) $value ) ? 'yes' : 'no'; } /** * カートの扱い方を丸める。 * * @param mixed $value 入力値。 * @return string */ public function sanitize_cart_mode( $value ) { return ( 'mixed' === $value ) ? 'mixed' : 'exclusive'; } /* * ----------------------------------------------------------------------- * 設定値 → フロントの挙動 * ----------------------------------------------------------------------- */ /** * アクセス Cookie の有効期間を設定値から決める。 * * @param int $lifetime 既定値(秒)。 * @return int */ public function filter_cookie_lifetime( $lifetime ) { $days = get_option( 'acscw_cookie_days', '' ); if ( '' === $days || null === $days ) { return $lifetime; } $days = absint( $days ); return ( $days > 0 ) ? $days * DAY_IN_SECONDS : $lifetime; } /* * ----------------------------------------------------------------------- * 孤児商品の掃除 * ----------------------------------------------------------------------- */ /** * 孤児商品を削除する。 * * 検出は ACSCW_Products が行い、削除するかどうかは必ず管理者が決める。 * アンインストールでは自動化しない(注文履歴を壊しうるため)。 * * @return void */ public function handle_clean_orphans() { ACSCW_Admin::require_capability(); check_admin_referer( self::ACTION_CLEAN_ORPHANS ); $products = ACSCW_Plugin::instance()->get( 'products' ); $deleted = 0; if ( $products instanceof ACSCW_Products ) { foreach ( $products->get_orphan_product_ids() as $product_id ) { if ( wp_delete_post( (int) $product_id, true ) ) { ++$deleted; } } } wp_safe_redirect( ACSCW_Admin::page_url( self::PAGE_SLUG, array( 'tab' => 'data', 'acscw_cleaned' => absint( $deleted ), 'acscw_message' => 'orphans', ) ) ); exit; } /* * ----------------------------------------------------------------------- * 描画 * ----------------------------------------------------------------------- */ /** * 設定画面を描画する。 * * @return void */ public function render() { ACSCW_Admin::require_capability(); $tab = $this->get_current_tab(); ACSCW_Admin::page_header( __( 'Settings', 'acs-content-for-woocommerce' ), __( 'Site-wide defaults. Each article can override them from the block.', 'acs-content-for-woocommerce' ) ); $this->render_tabs( $tab ); $this->render_flash(); $this->render_review_request(); if ( 'data' === $tab ) { $this->render_data_tab(); } else { echo '
'; } ACSCW_Admin::page_footer(); } /** * 「閉じる」リンクを処理する。一度閉じたら二度と表示しない。 * * @return void */ public function maybe_dismiss_review_notice() { if ( ! isset( $_GET['acscw_dismiss_review'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- 直後に check_admin_referer で検証する。 return; } if ( ! current_user_can( 'manage_options' ) ) { return; } check_admin_referer( 'acscw_dismiss_review' ); update_option( self::REVIEW_DISMISSED_OPTION, 1, false ); wp_safe_redirect( ACSCW_Admin::page_url( self::PAGE_SLUG ) ); exit; } /** * WordPress.org のレビュー依頼を1行だけ描画する。 * * 表示条件は「この設定画面」「manage_options」「まだ閉じていない」 * 「初回表示から7日以上経過」の4つ。外部への送信は一切行わない。 * * @return void */ private function render_review_request() { if ( ! current_user_can( 'manage_options' ) ) { return; } if ( get_option( self::REVIEW_DISMISSED_OPTION ) ) { return; } $since = (int) get_option( self::REVIEW_SINCE_OPTION, 0 ); if ( ! $since ) { $since = time(); update_option( self::REVIEW_SINCE_OPTION, $since, false ); } if ( ( time() - $since ) < self::REVIEW_DELAY_DAYS * DAY_IN_SECONDS ) { return; } $review_url = 'https://wordpress.org/support/plugin/acs-content-for-woocommerce/reviews/#new-post'; $dismiss_url = wp_nonce_url( ACSCW_Admin::page_url( self::PAGE_SLUG, array( 'acscw_dismiss_review' => '1' ) ), 'acscw_dismiss_review' ); printf( '', esc_html__( 'If this plugin is useful to you, a review on WordPress.org would help a lot.', 'acs-content-for-woocommerce' ), esc_url( $review_url ), esc_html__( 'Write a review', 'acs-content-for-woocommerce' ), esc_url( $dismiss_url ), esc_html__( 'Dismiss', 'acs-content-for-woocommerce' ) ); } /** * 現在のタブを返す。 * * @return string */ private function get_current_tab() { // phpcs:ignore WordPress.Security.NonceVerification.Recommended $tab = isset( $_GET['tab'] ) ? sanitize_key( wp_unslash( $_GET['tab'] ) ) : 'general'; return in_array( $tab, self::TABS, true ) ? $tab : 'general'; } /** * タブのラベル。 * * 翻訳関数には必ずリテラルを渡す(.pot に拾わせるため)。 * * @return array%2$s
| ';
printf(
' ',
esc_attr( (string) $price )
);
printf( '%s', esc_html( get_woocommerce_currency() ) );
printf(
' %s ', esc_html__( 'The starting value when a new Paid Area block is inserted. The currency follows your WooCommerce settings.', 'acs-content-for-woocommerce' ) ); echo ' | |
| ' . esc_html__( 'Enabled post types', 'acs-content-for-woocommerce' ) . ' | ';
printf(
' %s ', esc_html__( 'Version 2 was locked to posts only. Changes take effect on the next page load; reload any editor screen you have open.', 'acs-content-for-woocommerce' ) ); echo ' |
| ';
printf(
'',
esc_attr( implode( ', ', array_map( 'strval', $amounts ) ) )
);
printf(
' %s ', esc_html__( 'Comma separated, up to 8 amounts. On save, products are prepared for these amounts only; no product is created for an amount that is not listed here.', 'acs-content-for-woocommerce' ) ); echo ' | |
| ';
printf(
' ',
esc_attr( (string) absint( $days ) )
);
echo esc_html__( 'days', 'acs-content-for-woocommerce' );
printf(
' %s ', esc_html__( 'How many days access is remembered on the reader\'s device. Set it to 0 to require an access link every time.', 'acs-content-for-woocommerce' ) ); echo ' |
| ';
printf(
'',
esc_attr( (string) get_option( 'acscw_button_text', '' ) ),
esc_attr__( 'Read on (price)', 'acs-content-for-woocommerce' )
);
printf(
' %s ', esc_html__( 'If left blank, "Read on (price)" is used. Each article can override it from the block.', 'acs-content-for-woocommerce' ) ); echo ' |
%s
| ' . esc_html__( 'Simplification', 'acs-content-for-woocommerce' ) . ' | ';
printf(
' %s ', esc_html__( 'Shipping is dropped as well, since the article is a digital product. Turn this off to fall back entirely to the WooCommerce default behaviour.', 'acs-content-for-woocommerce' ) ); echo ' |
|---|---|
| ' . esc_html__( 'Cart handling', 'acs-content-for-woocommerce' ) . ' |
| %s | ', esc_html__( 'Feature', 'acs-content-for-woocommerce' ) ); printf( '%s | ', esc_html__( 'Classic (shortcode)', 'acs-content-for-woocommerce' ) ); printf( '%s | ', esc_html__( 'Block checkout', 'acs-content-for-woocommerce' ) ); echo '
|---|---|---|
| %1$s | %2$s | %3$s |
%s %s
', esc_html__( 'For reference, the WooCommerce "Allow guest checkout" setting is currently', 'acs-content-for-woocommerce' ), esc_html( ( 'yes' === get_option( 'woocommerce_enable_guest_checkout', 'no' ) ) ? __( 'enabled (this plugin has not changed it).', 'acs-content-for-woocommerce' ) : __( 'disabled. Guest checkout is allowed temporarily, and only while paid content is being bought.', 'acs-content-for-woocommerce' ) ) ); ACSCW_Admin::card_close(); } /** * データタブ。 * * @return void */ private function render_data_tab() { $this->render_import_card(); $this->render_legacy_blocks_card(); // --- 未使用商品の掃除 ------------------------------------------------ $products = ACSCW_Plugin::instance()->get( 'products' ); $orphans = ( $products instanceof ACSCW_Products ) ? $products->get_orphan_product_ids() : array(); ACSCW_Admin::card_open( __( 'Unused products', 'acs-content-for-woocommerce' ) ); if ( empty( $orphans ) ) { ACSCW_Admin::empty_state( __( 'There are no unused products to delete.', 'acs-content-for-woocommerce' ) ); } else { echo '%s
', esc_html__( 'Only paid content products whose source content has been deleted, and tip products no longer in the amount set, are listed here, and only when no order uses them.', 'acs-content-for-woocommerce' ) ); ACSCW_Admin::card_close(); // --- アンインストール時 ---------------------------------------------- echo ''; } /** * データタブ: 旧バージョンの購入履歴の取り込み。 * * 取り込みは手動1クリック。何度押しても同じ行は増えない(ACSCW_Importer の冪等設計)。 * 旧データは取り込みでは消さない。削除は「取り込み完了」かつ「確認チェック」の * 両方がそろったときだけ、別のボタンで行う。 * * @return void */ private function render_import_card() { $legacy_found = ACSCW_Legacy::table_exists(); $legacy_table = ACSCW_Legacy::get_table(); $legacy_count = $legacy_found ? ACSCW_Legacy::count_rows() : 0; $imported = ACSCW_Importer::count_imported(); $completed = ACSCW_Importer::is_completed(); ACSCW_Admin::card_open( __( 'Migration from the previous version', 'acs-content-for-woocommerce' ) ); if ( ! $legacy_found ) { ACSCW_Admin::empty_state( __( 'No data from the previous version was found. There is nothing to migrate.', 'acs-content-for-woocommerce' ) ); ACSCW_Admin::card_close(); return; } printf( '%s
%s
', esc_html( sprintf( /* translators: %d: maximum number of records imported per batch */ __( 'Imports up to %d records at a time. If any remain, press the same button again. Rows already imported are skipped rather than duplicated, and the old data is never modified or deleted by this operation.', 'acs-content-for-woocommerce' ), ACSCW_Importer::BATCH_SIZE ) ) ); if ( $completed ) { printf( '%s
', esc_html__( 'The import is complete. Once you no longer need the old data, tick the box below to delete it.', 'acs-content-for-woocommerce' ) ); echo ''; } ACSCW_Admin::card_close(); } /** * データタブ: 旧ブロックの置き換え。 * * @return void */ private function render_legacy_blocks_card() { $post_ids = ACSCW_Legacy_Blocks::get_legacy_post_ids(); ACSCW_Admin::card_open( __( 'Replacing old blocks', 'acs-content-for-woocommerce' ) ); if ( empty( $post_ids ) ) { ACSCW_Admin::empty_state( __( 'No posts use blocks from the previous version.', 'acs-content-for-woocommerce' ) ); ACSCW_Admin::card_close(); return; } echo '%s
', esc_html__( 'Even left as they are, old blocks are rendered for readers by the new implementation. Replacing them lets the editor treat them as ordinary blocks and keeps the price in a single place.', 'acs-content-for-woocommerce' ) ); ACSCW_Admin::card_close(); } /* * ----------------------------------------------------------------------- * 補助 * ----------------------------------------------------------------------- */ /** * 選択できる投稿タイプの一覧。 * * @return array