get_meta( self::META_MANAGED ) ) { return $valid; } if ( ! ACSCW_Products::is_acscw_product( $product ) ) { return false; } $post_ids = self::get_coupon_post_ids( $coupon ); if ( empty( $post_ids ) ) { // 「有料コンテンツすべて」を対象にしたクーポン。 // カート単位の型(fixed_cart)では WooCommerce が false を渡してくる // (商品単位の判定をしない型のため)。is_valid_for_cart() で商品単位の // 配分に切り替えているので、有料コンテンツの商品はここで対象にする。 if ( method_exists( $coupon, 'is_type' ) && function_exists( 'wc_get_product_coupon_types' ) && ! $coupon->is_type( wc_get_product_coupon_types() ) ) { return true; } return $valid; } $post_id = ACSCW_Products::get_linked_post_id( $product ); return ( $post_id > 0 && in_array( $post_id, $post_ids, true ) ); } /** * このプラグインが管理するクーポンかどうか。 * * @param WC_Coupon $coupon クーポン。 * @return bool */ public static function is_managed_coupon( $coupon ) { return is_object( $coupon ) && method_exists( $coupon, 'get_meta' ) && 'yes' === (string) $coupon->get_meta( self::META_MANAGED ); } /** * 管理クーポンをカート単位のクーポンとして扱わせない。 * * WooCommerce は、カート単位のクーポン(fixed_cart)なら商品単位の判定 * (woocommerce_coupon_is_valid_for_product)を見ずに全商品へ割引を配分する。 * そのままでは「有料コンテンツ専用」のクーポンが一般の商品にも効いてしまう。 * false を返すと、割引は is_valid_for_product() を通った商品にだけ配分される。 * * @param bool $valid これまでの判定。 * @param WC_Coupon $coupon クーポン。 * @return bool */ public function is_valid_for_cart( $valid, $coupon ) { return self::is_managed_coupon( $coupon ) ? false : $valid; } /** * 管理クーポンの対象になる商品がカートに無ければ、クーポンを無効にする。 * * @param bool $valid これまでの判定。 * @param WC_Coupon $coupon クーポン。 * @param WC_Discounts $discounts 割引の計算対象。 * @return bool */ public function is_valid_for_items( $valid, $coupon, $discounts = null ) { if ( ! $valid || ! self::is_managed_coupon( $coupon ) || ! is_object( $discounts ) || ! method_exists( $discounts, 'get_items' ) ) { return $valid; } foreach ( (array) $discounts->get_items() as $item ) { if ( isset( $item->product ) && $this->is_valid_for_product( true, $item->product, $coupon ) ) { return true; } } return false; } /** * クーポンの対象コンテンツIDを返す。 * * @param WC_Coupon $coupon クーポン。 * @return int[] 空なら「有料コンテンツすべて」。 */ public static function get_coupon_post_ids( $coupon ) { if ( ! is_object( $coupon ) || ! method_exists( $coupon, 'get_meta' ) ) { return array(); } $stored = $coupon->get_meta( self::META_POST_IDS ); if ( ! is_array( $stored ) ) { $stored = ( '' === (string) $stored ) ? array() : explode( ',', (string) $stored ); } $ids = array(); foreach ( $stored as $id ) { $id = absint( $id ); if ( $id > 0 ) { $ids[] = $id; } } return array_values( array_unique( $ids ) ); } /* * ----------------------------------------------------------------------- * 作成 * ----------------------------------------------------------------------- */ /** * クーポンを作成する。 * * 独自のクーポン機構は作らない。WooCommerce のクーポン(shop_coupon)として作り、 * WooCommerce のクーポン画面からも編集できる状態にする。 * * @return void */ public function handle_create() { ACSCW_Admin::require_capability(); check_admin_referer( self::ACTION_CREATE ); if ( ! class_exists( 'WC_Coupon' ) ) { $this->redirect( 'coupon_failed' ); } $code = isset( $_POST['acscw_code'] ) ? sanitize_text_field( wp_unslash( $_POST['acscw_code'] ) ) : ''; $code = strtolower( preg_replace( '/[^A-Za-z0-9_-]/', '', $code ) ); if ( '' === $code ) { $this->redirect( 'coupon_failed' ); } if ( function_exists( 'wc_get_coupon_id_by_code' ) && wc_get_coupon_id_by_code( $code ) > 0 ) { $this->redirect( 'coupon_duplicate' ); } $type = isset( $_POST['acscw_discount_type'] ) ? sanitize_key( wp_unslash( $_POST['acscw_discount_type'] ) ) : 'percent'; $type = ( 'fixed_cart' === $type ) ? 'fixed_cart' : 'percent'; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- ACSCW_Products::normalize_amount() casts to float and clamps the value, which is stricter than any text sanitizer. $amount = isset( $_POST['acscw_amount'] ) ? ACSCW_Products::normalize_amount( wp_unslash( $_POST['acscw_amount'] ) ) : 0; $expires = isset( $_POST['acscw_expires'] ) ? sanitize_text_field( wp_unslash( $_POST['acscw_expires'] ) ) : ''; $limit = isset( $_POST['acscw_usage_limit'] ) ? absint( wp_unslash( $_POST['acscw_usage_limit'] ) ) : 0; $scope = isset( $_POST['acscw_scope'] ) ? sanitize_key( wp_unslash( $_POST['acscw_scope'] ) ) : 'all'; $post_ids = array(); if ( 'selected' === $scope && isset( $_POST['acscw_post_ids'] ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Every element is passed through absint() on the next line before it is kept. foreach ( (array) wp_unslash( $_POST['acscw_post_ids'] ) as $id ) { $id = absint( $id ); if ( $id > 0 ) { $post_ids[] = $id; } } } if ( 'percent' === $type ) { $amount = min( 100, $amount ); } if ( $amount <= 0 ) { $this->redirect( 'coupon_failed' ); } $coupon = new WC_Coupon(); $coupon->set_code( $code ); $coupon->set_discount_type( $type ); $coupon->set_amount( $amount ); $coupon->set_individual_use( true ); if ( $limit > 0 ) { $coupon->set_usage_limit( $limit ); } if ( '' !== $expires && false !== strtotime( $expires ) ) { $coupon->set_date_expires( $expires ); } $coupon->set_description( __( 'Paid content coupon for ACS Content for WooCommerce', 'acs-content-for-woocommerce' ) ); $coupon->update_meta_data( self::META_MANAGED, 'yes' ); $coupon->update_meta_data( self::META_POST_IDS, array_values( array_unique( $post_ids ) ) ); $coupon->save(); $this->redirect( 'coupon_created' ); } /** * 処理後にクーポン画面へ戻す。 * * @param string $message 通知コード。 * @return void */ private function redirect( $message ) { wp_safe_redirect( ACSCW_Admin::page_url( self::PAGE_SLUG, array( 'acscw_message' => $message ) ) ); exit; } /* * ----------------------------------------------------------------------- * 一覧 * ----------------------------------------------------------------------- */ /** * このプラグインが管理しているクーポンを返す。 * * @return int[] クーポンID。 */ public static function get_managed_coupon_ids() { // phpcs:disable WordPress.DB.SlowDBQuery.slow_db_query_meta_key, WordPress.DB.SlowDBQuery.slow_db_query_meta_value -- Coupons created by this plugin are identifiable only by this meta pair; the query is capped at 100 rows and runs on the plugin's own admin screen. $ids = get_posts( array( 'post_type' => 'shop_coupon', 'post_status' => array( 'publish', 'draft', 'pending' ), 'posts_per_page' => 100, 'fields' => 'ids', 'no_found_rows' => true, 'update_post_term_cache' => false, 'meta_key' => self::META_MANAGED, 'meta_value' => 'yes', ) ); // phpcs:enable WordPress.DB.SlowDBQuery.slow_db_query_meta_key, WordPress.DB.SlowDBQuery.slow_db_query_meta_value return array_map( 'absint', (array) $ids ); } /* * ----------------------------------------------------------------------- * 描画 * ----------------------------------------------------------------------- */ /** * クーポン画面を描画する。 * * @return void */ public function render() { ACSCW_Admin::require_capability(); ACSCW_Admin::page_header( __( 'Coupons', 'acs-content-for-woocommerce' ), __( 'Discount codes for your paid content only. They are created as WooCommerce coupons.', 'acs-content-for-woocommerce' ) ); $this->render_flash(); $this->render_list(); $this->render_form(); ACSCW_Admin::page_footer(); } /** * 処理結果の通知を描画する。 * * @return void */ private function render_flash() { // phpcs:ignore WordPress.Security.NonceVerification.Recommended $code = isset( $_GET['acscw_message'] ) ? sanitize_key( wp_unslash( $_GET['acscw_message'] ) ) : ''; $messages = array( 'coupon_created' => array( 'success', __( 'Coupon created.', 'acs-content-for-woocommerce' ) ), 'coupon_duplicate' => array( 'error', __( 'That coupon code is already in use.', 'acs-content-for-woocommerce' ) ), 'coupon_failed' => array( 'error', __( 'The coupon could not be created. Check the code and the discount amount.', 'acs-content-for-woocommerce' ) ), ); if ( ! isset( $messages[ $code ] ) ) { return; } printf( '

%2$s

', esc_attr( $messages[ $code ][0] ), esc_html( $messages[ $code ][1] ) ); } /** * クーポン一覧を描画する。 * * @return void */ private function render_list() { $ids = self::get_managed_coupon_ids(); ACSCW_Admin::card_open( __( 'Issued coupons', 'acs-content-for-woocommerce' ) ); if ( empty( $ids ) || ! class_exists( 'WC_Coupon' ) ) { ACSCW_Admin::empty_state( __( 'No coupons created yet.', 'acs-content-for-woocommerce' ) ); ACSCW_Admin::card_close(); return; } echo ''; printf( '', esc_html__( 'Code', 'acs-content-for-woocommerce' ) ); printf( '', esc_html__( 'Discount', 'acs-content-for-woocommerce' ) ); printf( '', esc_html__( 'Scope', 'acs-content-for-woocommerce' ) ); printf( '', esc_html__( 'Expires', 'acs-content-for-woocommerce' ) ); printf( '', esc_html__( 'Used', 'acs-content-for-woocommerce' ) ); printf( '', esc_html__( 'Actions', 'acs-content-for-woocommerce' ) ); echo ''; foreach ( $ids as $id ) { $coupon = new WC_Coupon( $id ); printf( '', esc_html( strtoupper( (string) $coupon->get_code() ) ), wp_kses_post( $this->format_discount( $coupon ) ), esc_html( $this->format_target( $coupon ) ), esc_html( $this->format_expiry( $coupon ) ), esc_html( $this->format_usage( $coupon ) ), sprintf( '%2$s', esc_url( (string) get_edit_post_link( $id ) ), esc_html__( 'Edit', 'acs-content-for-woocommerce' ) ) ); } echo '
%s%s%s%s%s%s
%1$s%2$s%3$s%4$s%5$s%6$s
'; printf( '

%s

', esc_html__( 'These never apply to ordinary shop products. A coupon with target content set works only for purchases of those articles.', 'acs-content-for-woocommerce' ) ); ACSCW_Admin::card_close(); } /** * 割引の表示。 * * @param WC_Coupon $coupon クーポン。 * @return string */ private function format_discount( $coupon ) { $amount = (float) $coupon->get_amount(); if ( 'percent' === $coupon->get_discount_type() ) { return esc_html( sprintf( /* translators: %s: percentage */ __( '%s%% off', 'acs-content-for-woocommerce' ), number_format_i18n( $amount ) ) ); } return ACSCW_Admin::price( $amount ) . esc_html__( ' off', 'acs-content-for-woocommerce' ); } /** * 対象の表示。 * * @param WC_Coupon $coupon クーポン。 * @return string */ private function format_target( $coupon ) { $ids = self::get_coupon_post_ids( $coupon ); if ( empty( $ids ) ) { return __( 'All paid content', 'acs-content-for-woocommerce' ); } $titles = array(); foreach ( $ids as $id ) { $title = get_the_title( $id ); $titles[] = ( '' === $title ) ? ( '#' . $id ) : $title; } return implode( ' / ', $titles ); } /** * 有効期限の表示。 * * @param WC_Coupon $coupon クーポン。 * @return string */ private function format_expiry( $coupon ) { $expires = $coupon->get_date_expires(); if ( ! $expires || ! method_exists( $expires, 'date' ) ) { return __( 'No expiry', 'acs-content-for-woocommerce' ); } return $expires->date( 'Y-m-d' ); } /** * 利用状況の表示。 * * @param WC_Coupon $coupon クーポン。 * @return string */ private function format_usage( $coupon ) { $used = (int) $coupon->get_usage_count(); $limit = (int) $coupon->get_usage_limit(); if ( $limit <= 0 ) { return sprintf( /* translators: %s: number of uses */ __( '%s / unlimited', 'acs-content-for-woocommerce' ), number_format_i18n( $used ) ); } return number_format_i18n( $used ) . ' / ' . number_format_i18n( $limit ); } /** * 新規作成フォームを描画する。 * * @return void */ private function render_form() { ACSCW_Admin::card_open( __( 'New coupon', 'acs-content-for-woocommerce' ) ); if ( ! class_exists( 'WC_Coupon' ) ) { ACSCW_Admin::empty_state( __( 'Coupons cannot be created because WooCommerce is not active.', 'acs-content-for-woocommerce' ) ); ACSCW_Admin::card_close(); return; } echo '
'; wp_nonce_field( self::ACTION_CREATE ); printf( '', esc_attr( self::ACTION_CREATE ) ); echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; printf( '

', esc_html__( 'Create coupon', 'acs-content-for-woocommerce' ) ); echo '
'; ACSCW_Admin::card_close(); } }