$amount ); if ( absint( $post_id ) > 0 ) { $args[ self::QUERY_FROM ] = absint( $post_id ); } $url = add_query_arg( $args, home_url( '/' ) ); $url = wp_nonce_url( $url, self::purchase_nonce_action( 'tip', $amount, $post_id ), self::QUERY_NONCE ); /** * 投げ銭導線のURL。 * * @since 3.0.0 * * @param string $url URL。 * @param float $amount 金額。 * @param int $post_id 戻り先のコンテンツID。 */ return (string) apply_filters( 'acscw_tip_url', $url, $amount, $post_id ); } /** * 購入・投げ銭リクエストを処理してチェックアウトへ送る。 * * この関数は template_redirect からしか呼ばれない。$_GET を読むのは * ここだけで、関数の外(ファイル読み込み時)では一切見ない。 * * 検証の順番: * 1. クエリ変数を読む(この時点では何も書き換えない) * 2. wp_verify_nonce() で検証する。失敗したらカートに触れずに戻す * 3. リファラがあり、かつ他ホストなら同じく戻す(多層防御) * 4. ここではじめてカートを書き換える * * current_user_can() は使わない。有料コンテンツの購入と投げ銭は * 未ログインの訪問者にも開かれた操作で、要求すべき権限が存在しない * (権限で絞ると購入自体ができなくなる)。守るべきは「訪問者本人が * 意図してクリックしたか」であり、それは nonce が担う。 * * @return void */ public function maybe_handle_purchase_request() { // 配列を渡された場合に備え、必ず sanitize_text_field(配列なら空文字を返す)を通してから数値化する。 $buy = isset( $_GET[ self::QUERY_BUY ] ) ? absint( sanitize_text_field( wp_unslash( $_GET[ self::QUERY_BUY ] ) ) ) : 0; $tip = isset( $_GET[ self::QUERY_TIP ] ) ? ACSCW_Products::normalize_amount( sanitize_text_field( wp_unslash( $_GET[ self::QUERY_TIP ] ) ) ) : 0.0; $from = isset( $_GET[ self::QUERY_FROM ] ) ? absint( sanitize_text_field( wp_unslash( $_GET[ self::QUERY_FROM ] ) ) ) : 0; $nonce = isset( $_GET[ self::QUERY_NONCE ] ) ? sanitize_text_field( wp_unslash( $_GET[ self::QUERY_NONCE ] ) ) : ''; if ( 0 === $buy && $tip <= 0 ) { return; } /* * ここから先はカートを書き換えうる。 * nonce(+リファラ)を通らないリクエストは、カートに一切触れずに帰す。 */ $action = ( $buy > 0 ) ? self::purchase_nonce_action( 'buy', $buy ) : self::purchase_nonce_action( 'tip', $tip, $from ); if ( ! wp_verify_nonce( $nonce, $action ) || ! $this->referer_is_same_site() ) { $this->redirect_with_notice( ( $buy > 0 ) ? $buy : $from, 'expired' ); return; } if ( ! ACSCW_Cart_Context::cart_available() ) { return; } $products = ACSCW_Plugin::instance()->get( 'products' ); if ( ! $products instanceof ACSCW_Products ) { return; } if ( $buy > 0 ) { $product_id = $products->get_paywall_product_id( $buy, true ); $return_id = $buy; } else { $product_id = $products->get_tip_product_id( $tip, true ); $return_id = $from; } if ( $product_id <= 0 ) { $this->redirect_with_notice( $return_id, 'unavailable' ); return; } if ( ! $this->prepare_cart( $product_id ) ) { $this->redirect_with_notice( $return_id, 'cart_failed' ); return; } $added = WC()->cart->add_to_cart( $product_id, 1, 0, array(), array( self::CART_ITEM_POST_ID => $return_id ) ); if ( ! $added ) { $this->redirect_with_notice( $return_id, 'cart_failed' ); return; } /** * 購入導線からカートへ入れた直後に発火する。 * * @since 3.0.0 * * @param int $product_id 商品ID。 * @param int $return_id 戻り先のコンテンツID。 */ do_action( 'acscw_added_to_cart', $product_id, $return_id ); $checkout_url = function_exists( 'wc_get_checkout_url' ) ? wc_get_checkout_url() : home_url( '/' ); /** * カート投入後の遷移先。 * * @since 3.0.0 * * @param string $checkout_url チェックアウトURL。 * @param int $product_id 商品ID。 */ $checkout_url = apply_filters( 'acscw_checkout_redirect_url', $checkout_url, $product_id ); wp_safe_redirect( $checkout_url ); exit; } /** * リファラが自サイトのものか(無い場合は許す)。 * * nonce の上に重ねる多層防御。リファラを送らないブラウザ・ * プライバシー設定・一部のメールアプリが実在するため、 * 「無い」ことを理由に購入を止めることはしない。 * 「有るのに他ホスト」=外部サイトからの誘導だけを弾く。 * * @return bool */ private function referer_is_same_site() { $referer = isset( $_SERVER['HTTP_REFERER'] ) ? esc_url_raw( wp_unslash( $_SERVER['HTTP_REFERER'] ) ) : ''; if ( '' === $referer ) { return true; } $referer_host = wp_parse_url( $referer, PHP_URL_HOST ); $home_host = wp_parse_url( home_url( '/' ), PHP_URL_HOST ); if ( ! $referer_host || ! $home_host ) { return true; } return 0 === strcasecmp( (string) $referer_host, (string) $home_host ); } /** * カートを購入できる状態にする。 * * 既定(acscw_cart_mode = 'exclusive')では、他店舗商品が入っていれば * それを取り除いて「有料コンテンツだけの単独チェックアウト」にする。 * 'mixed' を選んだサイトでは何もせず、そのまま一緒に購入させる * (その場合、簡略化は cart_is_acscw_only() が false になるため自動的に効かない)。 * * @param int $product_id これから入れる商品ID。 * @return bool 続行してよいかどうか。 */ private function prepare_cart( $product_id ) { unset( $product_id ); if ( 'mixed' === $this->get_cart_mode() ) { return true; } $foreign = ACSCW_Cart_Context::get_foreign_cart_keys(); if ( empty( $foreign ) ) { return true; } foreach ( $foreign as $cart_key ) { WC()->cart->remove_cart_item( $cart_key ); } if ( function_exists( 'wc_add_notice' ) ) { wc_add_notice( __( 'Paid content is bought on its own, so the other products in your cart have been removed for now.', 'acs-content-for-woocommerce' ), 'notice' ); } /** * 単独チェックアウトのためにカートから外した商品。 * * @since 3.0.0 * * @param string[] $foreign 外したカート項目のキー。 */ do_action( 'acscw_foreign_cart_items_removed', $foreign ); return true; } /** * カートの扱い方('exclusive' | 'mixed')。 * * @return string */ private function get_cart_mode() { $mode = (string) get_option( 'acscw_cart_mode', 'exclusive' ); return ( 'mixed' === $mode ) ? 'mixed' : 'exclusive'; } /** * 失敗時にコンテンツへ戻す。 * * @param int $post_id 戻り先のコンテンツID。 * @param string $notice 通知コード。 * @return void */ private function redirect_with_notice( $post_id, $notice ) { $base = ( absint( $post_id ) > 0 ) ? get_permalink( absint( $post_id ) ) : home_url( '/' ); if ( ! $base ) { $base = home_url( '/' ); } wp_safe_redirect( add_query_arg( 'acscw_notice', rawurlencode( $notice ), $base ) ); exit; } /* * ----------------------------------------------------------------------- * カート項目 * ----------------------------------------------------------------------- */ /** * カート項目に「どのコンテンツか」と「投入時点の価格」を持たせる。 * * 価格をここで固定するのは、カートに入れたあとに管理者が値上げしても * 読者が見せられた金額で買えるようにするため。 * * @param array $cart_item_data カート項目の追加データ。 * @param int $product_id 商品ID。 * @param int $variation_id バリエーションID。 * @return array */ public function add_cart_item_data( $cart_item_data, $product_id, $variation_id = 0 ) { unset( $variation_id ); $type = ACSCW_Products::get_product_type( $product_id ); if ( '' === $type ) { return $cart_item_data; } if ( ACSCW_Products::TYPE_PAYWALL === $type ) { $post_id = ACSCW_Products::get_linked_post_id( $product_id ); if ( $post_id > 0 ) { $cart_item_data[ self::CART_ITEM_POST_ID ] = $post_id; $cart_item_data[ self::CART_ITEM_PRICE ] = ACSCW_Products::get_content_price( $post_id ); } return $cart_item_data; } // 投げ銭。金額は商品側の値をそのまま使う(記事に依存しない)。 if ( empty( $cart_item_data[ self::CART_ITEM_POST_ID ] ) ) { $cart_item_data[ self::CART_ITEM_POST_ID ] = 0; } return $cart_item_data; } /** * カート計算時に、コンテンツの現在価格を反映する。 * * 商品レコードの価格を毎回書き換える v2 の方式は採らない * (表示のたびに update_post_meta していたのが v2 の問題②の原因)。 * * @param WC_Cart $cart カート。 * @return void */ public function apply_cart_item_price( $cart ) { if ( ! is_object( $cart ) || ! method_exists( $cart, 'get_cart' ) ) { return; } // set_price() は絶対値の代入なので、同一リクエストで複数回走っても結果は同じ。 foreach ( $cart->get_cart() as $item ) { if ( empty( $item[ self::CART_ITEM_PRICE ] ) || ! isset( $item['data'] ) ) { continue; } if ( ! is_object( $item['data'] ) || ! method_exists( $item['data'], 'set_price' ) ) { continue; } $price = ACSCW_Products::normalize_amount( $item[ self::CART_ITEM_PRICE ] ); if ( $price > 0 ) { $item['data']->set_price( (string) $price ); } } } /** * 注文明細にコンテンツIDを残す。 * * 商品メタからも辿れるが、投げ銭は「どの記事から送られたか」が * 商品側に無いため、注文明細に持たせる必要がある。 * * @param WC_Order_Item_Product $item 注文明細。 * @param string $cart_item_key カート項目キー。 * @param array $values カート項目。 * @return void */ public function add_order_item_meta( $item, $cart_item_key, $values ) { unset( $cart_item_key ); if ( ! isset( $values[ self::CART_ITEM_POST_ID ] ) || ! is_object( $item ) ) { return; } if ( ! method_exists( $item, 'add_meta_data' ) ) { return; } $item->add_meta_data( self::ORDER_ITEM_POST_ID, absint( $values[ self::CART_ITEM_POST_ID ] ), true ); } /* * ----------------------------------------------------------------------- * 条件付き簡略化(クラシック版チェックアウト) * ----------------------------------------------------------------------- */ /** * 請求先を氏名+メールだけにし、配送先を外す。 * * v2 の🔴B との違いは1点だけだが決定的: * **カートの中身を見てから触る**。 * * @param array $fields チェックアウトのフィールド定義。 * @return array 介入しない場合は引数そのまま。 */ public function simplify_checkout_fields( $fields ) { if ( ! is_array( $fields ) || ! ACSCW_Cart_Context::should_simplify_checkout() ) { return $fields; } if ( self::is_blocks_checkout() ) { return $fields; } $keep = array( 'billing_first_name', 'billing_last_name', 'billing_email' ); /** * 簡略化後に残す請求先フィールド。 * * 決済方法によっては住所が必須のことがあるため、サイト側で足せるようにする。 * * @since 3.0.0 * * @param string[] $keep 残すフィールドキー。 */ $keep = (array) apply_filters( 'acscw_checkout_billing_fields', $keep ); if ( isset( $fields['billing'] ) && is_array( $fields['billing'] ) ) { foreach ( array_keys( $fields['billing'] ) as $key ) { if ( ! in_array( $key, $keep, true ) ) { unset( $fields['billing'][ $key ] ); } } if ( isset( $fields['billing']['billing_email'] ) ) { $fields['billing']['billing_email']['required'] = true; $fields['billing']['billing_email']['priority'] = 10; } } // 仮想商品しか入っていないので配送先は不要。 unset( $fields['shipping'] ); return $fields; } /** * 配送を不要にする。 * * @param bool $needs 判定結果。 * @return bool 介入しない場合は引数そのまま。 */ public function filter_needs_shipping( $needs ) { if ( ! ACSCW_Cart_Context::should_simplify_checkout() ) { return $needs; } return false; } /** * アカウント登録を必須にしない(ゲスト購入を許可する)。 * * v2 は update_option('woocommerce_enable_guest_checkout','yes') を毎リクエスト * 実行していた。これはサイト全体の設定を書き換える行為で、 * 「顧客アカウント登録」を無効化されたショップが実際に壊れた(🔴A)。 * このフィルタはリクエスト内でしか効かず、DBには何も書かない。 * * ブロック版チェックアウトでも Store API 経由で参照されるため、両版で有効。 * * @param bool $required 判定結果。 * @return bool 介入しない場合は引数そのまま。 */ public function filter_registration_required( $required ) { if ( ! ACSCW_Cart_Context::should_simplify_checkout() ) { return $required; } return false; } /** * チェックアウト上のアカウント作成欄を出さない。 * * @param bool $enabled 判定結果。 * @return bool 介入しない場合は引数そのまま。 */ public function filter_registration_enabled( $enabled ) { if ( ! ACSCW_Cart_Context::should_simplify_checkout() ) { return $enabled; } return false; } /** * 「別の住所へ配送する」を初期状態でオフにする。 * * @param bool $checked 判定結果。 * @return bool 介入しない場合は引数そのまま。 */ public function filter_ship_to_different( $checked ) { if ( ! ACSCW_Cart_Context::should_simplify_checkout() ) { return $checked; } return false; } /** * 注文メモ欄を出さない(クラシック版のみ)。 * * @param bool $enabled 判定結果。 * @return bool 介入しない場合は引数そのまま。 */ public function filter_order_notes( $enabled ) { if ( ! ACSCW_Cart_Context::should_simplify_checkout() || self::is_blocks_checkout() ) { return $enabled; } return false; } /** * クラシック版チェックアウト上部のログイン誘導を隠す。 * * remove_action はこのリクエストの描画にしか効かない。 * WooCommerce の設定(ログインリマインダー)は書き換えない。 * * クーポン入力欄は**外さない**。割引はWooCommerce標準に任せる方針(再設計案 §4)で、 * ここで隠すとクーポンを配った読者が使えなくなる。 * * @return void */ public function maybe_hide_login_form() { if ( ! ACSCW_Cart_Context::should_simplify_checkout() ) { return; } remove_action( 'woocommerce_before_checkout_form', 'woocommerce_checkout_login_form', 10 ); } /* * ----------------------------------------------------------------------- * ブロック版チェックアウトの判定 * ----------------------------------------------------------------------- */ /** * 現在のチェックアウトがブロック版かどうか。 * * 判定順: * 1. Store API のリクエストならブロック版(実処理はここを通る) * 2. WooCommerce Blocks のユーティリティがあればそれに従う * 3. チェックアウト固定ページに woocommerce/checkout ブロックがあるか * * @return bool */ public static function is_blocks_checkout() { if ( ACSCW_Cart_Context::is_store_api_request() ) { return true; } $utils = '\Automattic\WooCommerce\Blocks\Utils\CartCheckoutUtils'; if ( class_exists( $utils ) && method_exists( $utils, 'is_checkout_block_default' ) ) { return (bool) call_user_func( array( $utils, 'is_checkout_block_default' ) ); } if ( ! function_exists( 'wc_get_page_id' ) || ! function_exists( 'has_block' ) ) { return false; } $page_id = (int) wc_get_page_id( 'checkout' ); if ( $page_id <= 0 ) { return false; } return (bool) has_block( 'woocommerce/checkout', $page_id ); } }