'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 '
'; settings_fields( 'acscw_' . $tab ); if ( 'general' === $tab ) { $this->render_general_tab(); } elseif ( 'design' === $tab ) { $this->render_design_tab(); } else { $this->render_checkout_tab(); } submit_button( __( 'Save changes', 'acs-content-for-woocommerce' ) ); 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( '

%1$s %3$s %5$s

', 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 */ private function get_tab_labels() { return array( 'general' => __( 'General', 'acs-content-for-woocommerce' ), 'design' => __( 'Design', 'acs-content-for-woocommerce' ), 'checkout' => __( 'Checkout', 'acs-content-for-woocommerce' ), 'data' => __( 'Data', 'acs-content-for-woocommerce' ), ); } /** * タブを描画する。 * * @param string $current 現在のタブ。 * @return void */ private function render_tabs( $current ) { echo ''; } /** * 処理結果の通知を描画する。 * * @return void */ private function render_flash() { // phpcs:ignore WordPress.Security.NonceVerification.Recommended $message = isset( $_GET['acscw_message'] ) ? sanitize_key( wp_unslash( $_GET['acscw_message'] ) ) : ''; if ( '' === $message ) { return; } // phpcs:ignore WordPress.Security.NonceVerification.Recommended $cleaned = isset( $_GET['acscw_cleaned'] ) ? absint( wp_unslash( $_GET['acscw_cleaned'] ) ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended $count = isset( $_GET['acscw_count'] ) ? absint( wp_unslash( $_GET['acscw_count'] ) ) : 0; $type = 'success'; $text = ''; if ( 'orphans' === $message ) { $text = sprintf( /* translators: %d: number of products deleted */ _n( 'Deleted %d unused product.', 'Deleted %d unused products.', $cleaned, 'acs-content-for-woocommerce' ), $cleaned ); } elseif ( 'imported' === $message ) { $summary = ACSCW_Importer::get_summary(); $text = sprintf( /* translators: 1: imported count, 2: skipped count, 3: remaining count */ __( 'Imported %1$d old records (rows skipped as duplicates or invalid: %2$d / remaining: %3$d).', 'acs-content-for-woocommerce' ), $count, absint( $summary['skipped'] ), absint( $summary['remaining'] ) ); } elseif ( 'deleted' === $message ) { $text = sprintf( /* translators: %d: number of old records deleted */ __( 'Deleted %d old records.', 'acs-content-for-woocommerce' ), $count ); } elseif ( 'delete_blocked' === $message ) { $type = 'error'; $text = __( 'The old data could not be deleted. Finish the import, tick the confirmation box, and try again.', 'acs-content-for-woocommerce' ); } elseif ( 'converted' === $message ) { $text = sprintf( /* translators: %d: number of posts converted */ __( 'Replaced old blocks in %d posts.', 'acs-content-for-woocommerce' ), $count ); } else { return; } printf( '

%2$s

', esc_attr( $type ), esc_html( $text ) ); } /** * 全般タブ。 * * @return void */ private function render_general_tab() { $price = ACSCW_Products::normalize_amount( get_option( 'acscw_default_price', 0 ) ); $selected = ACSCW_Products::get_supported_post_types(); $amounts = ACSCW_Products::get_tip_amounts(); $days = get_option( 'acscw_cookie_days', 90 ); $post_types = self::get_selectable_post_types(); ACSCW_Admin::card_open( __( 'Basics', 'acs-content-for-woocommerce' ) ); echo ''; // 既定価格。 echo ''; // 対象投稿タイプ。 echo ''; // 投げ銭金額。 echo ''; // アクセス保持日数。 echo ''; echo ''; ACSCW_Admin::card_close(); } /** * デザインタブ。 * * @return void */ private function render_design_tab() { ACSCW_Admin::card_open( __( 'Paywall appearance (site default)', 'acs-content-for-woocommerce' ) ); echo ''; echo ''; echo ''; ACSCW_Admin::card_close(); } /** * 購入体験タブ。 * * @return void */ private function render_checkout_tab() { $simplify = ( 'no' !== get_option( 'acscw_simplify_checkout', 'yes' ) ); $mode = ( 'mixed' === get_option( 'acscw_cart_mode', 'exclusive' ) ) ? 'mixed' : 'exclusive'; printf( '

%s

', esc_html__( 'The simplification enabled here applies only when the cart holds nothing but paid content. Ordinary shop purchases are left exactly as they are, and WooCommerce\'s own settings are never rewritten.', 'acs-content-for-woocommerce' ) ); ACSCW_Admin::card_open( __( 'Checkout simplification', 'acs-content-for-woocommerce' ) ); echo ''; echo ''; echo ''; echo ''; // 適用範囲の明示。何がどちらのチェックアウトで効くかを隠さない。 echo ''; printf( '', esc_html__( 'Feature', 'acs-content-for-woocommerce' ) ); printf( '', esc_html__( 'Classic (shortcode)', 'acs-content-for-woocommerce' ) ); printf( '', esc_html__( 'Block checkout', 'acs-content-for-woocommerce' ) ); echo ''; $rows = array( array( __( 'Reduce billing fields to name and email', 'acs-content-for-woocommerce' ), __( 'Applies', 'acs-content-for-woocommerce' ), __( 'Does not apply (block checkout defines its own fields)', 'acs-content-for-woocommerce' ), ), array( __( 'Do not require a shipping address', 'acs-content-for-woocommerce' ), __( 'Applies', 'acs-content-for-woocommerce' ), __( 'Applies', 'acs-content-for-woocommerce' ), ), array( __( 'No login required (guest checkout)', 'acs-content-for-woocommerce' ), __( 'Applies', 'acs-content-for-woocommerce' ), __( 'Applies', 'acs-content-for-woocommerce' ), ), array( __( 'Hide the login form', 'acs-content-for-woocommerce' ), __( 'Applies', 'acs-content-for-woocommerce' ), __( 'Does not apply', 'acs-content-for-woocommerce' ), ), ); foreach ( $rows as $row ) { printf( '', esc_html( $row[0] ), esc_html( $row[1] ), esc_html( $row[2] ) ); } echo '
%s%s%s
%1$s%2$s%3$s
'; // WooCommerce 側の現在値は読むだけ。書き換えない。 printf( '

%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 ''; echo '
'; wp_nonce_field( self::ACTION_CLEAN_ORPHANS ); printf( '', esc_attr( self::ACTION_CLEAN_ORPHANS ) ); printf( '

', esc_html( sprintf( /* translators: %d: count */ __( 'Delete %d unused products', 'acs-content-for-woocommerce' ), count( $orphans ) ) ) ); echo '
'; } printf( '

%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 '
'; settings_fields( 'acscw_data' ); ACSCW_Admin::card_open( __( 'On uninstall', 'acs-content-for-woocommerce' ) ); echo ''; echo ''; ACSCW_Admin::card_close(); submit_button( __( 'Save changes', 'acs-content-for-woocommerce' ) ); 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

', esc_html( sprintf( /* translators: 1: table name, 2: number of old records, 3: number already imported */ __( 'Data from the previous version (ACS WooCommerce Content Pro) was found. %1$s holds %2$d purchase records, %3$d of which have been imported.', 'acs-content-for-woocommerce' ), $legacy_table, $legacy_count, $imported ) ) ); echo '
'; wp_nonce_field( ACSCW_Importer::ACTION_IMPORT ); printf( '', esc_attr( ACSCW_Importer::ACTION_IMPORT ) ); printf( '

', esc_html( $completed ? __( 'Import again', 'acs-content-for-woocommerce' ) : sprintf( /* translators: %d: maximum number of records imported per batch */ __( 'Import old data (up to %d at a time)', 'acs-content-for-woocommerce' ), ACSCW_Importer::BATCH_SIZE ) ) ); echo '
'; printf( '

%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 '
'; wp_nonce_field( ACSCW_Importer::ACTION_DELETE ); printf( '', esc_attr( ACSCW_Importer::ACTION_DELETE ) ); printf( '

', esc_html( sprintf( /* translators: %s: table name */ __( 'I confirm that the old data (%s) will be deleted. This cannot be undone.', 'acs-content-for-woocommerce' ), $legacy_table ) ) ); printf( '

', esc_html__( 'Delete the old data', '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 ''; echo '
'; wp_nonce_field( ACSCW_Legacy_Blocks::ACTION_CONVERT ); printf( '', esc_attr( ACSCW_Legacy_Blocks::ACTION_CONVERT ) ); printf( '

', esc_html( sprintf( /* translators: %d: number of posts */ __( 'Replace blocks in %d posts', 'acs-content-for-woocommerce' ), count( $post_ids ) ) ) ); echo '
'; printf( '

%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 キーは投稿タイプ名、値はラベル。 */ public static function get_selectable_post_types() { $types = get_post_types( array( 'public' => true ), 'objects' ); $list = array(); foreach ( (array) $types as $type ) { if ( ! is_object( $type ) || in_array( $type->name, array( 'attachment', 'product' ), true ) ) { continue; } $label = isset( $type->labels->name ) ? $type->labels->name : $type->name; $list[ $type->name ] = (string) $label; } return $list; } }