$today . ' 00:00:00', 'to' => $to, 'label' => __( 'Today', 'acs-content-for-woocommerce' ), ); case '7days': return array( 'from' => gmdate( 'Y-m-d', $now - 6 * DAY_IN_SECONDS ) . ' 00:00:00', 'to' => $to, 'label' => __( 'Last 7 days', 'acs-content-for-woocommerce' ), ); case '30days': return array( 'from' => gmdate( 'Y-m-d', $now - 29 * DAY_IN_SECONDS ) . ' 00:00:00', 'to' => $to, 'label' => __( 'Last 30 days', 'acs-content-for-woocommerce' ), ); } return array( 'from' => '', 'to' => '', 'label' => __( 'All time', 'acs-content-for-woocommerce' ), ); } /** * プリセットの1つ前の期間を返す(前日比・前週比の分母)。 * * @param string $preset 'today' / '7days' / '30days'。 * @return array{from:string,to:string}|null 累計には前期間が無いので null。 */ public static function get_previous_range( $preset ) { $now = self::now(); $days = array( 'today' => 1, '7days' => 7, '30days' => 30, ); if ( ! isset( $days[ $preset ] ) ) { return null; } $span = $days[ $preset ]; return array( 'from' => gmdate( 'Y-m-d', $now - ( 2 * $span - 1 ) * DAY_IN_SECONDS ) . ' 00:00:00', 'to' => gmdate( 'Y-m-d', $now - $span * DAY_IN_SECONDS ) . ' 23:59:59', ); } /* * ----------------------------------------------------------------------- * 合計 * ----------------------------------------------------------------------- */ /** * 期間内の合計を返す。 * * @param string $from 開始日時 'Y-m-d H:i:s'。空で下限なし。 * @param string $to 終了日時 'Y-m-d H:i:s'。空で上限なし。 * @return array{sales:int,revenue:float,refunded:int,refunded_amount:float,buyers:int,repeat_buyers:int,tip_count:int,tip_amount:float,total:float} */ public static function get_summary( $from = '', $to = '' ) { $cache_key = 'summary|' . $from . '|' . $to; $cached = self::cache_get( $cache_key ); if ( is_array( $cached ) ) { return $cached; } $access = self::get_access(); $summary = array( 'sales' => 0, 'revenue' => 0.0, 'refunded' => 0, 'refunded_amount' => 0.0, 'buyers' => 0, 'repeat_buyers' => 0, ); if ( $access ) { $summary = $access->get_totals( array( 'date_from' => $from, 'date_to' => $to, ) ); } $tips = self::sum_tips( self::get_tip_records( $from, $to ) ); $summary['tip_count'] = $tips['count']; $summary['tip_amount'] = $tips['amount']; $summary['total'] = round( $summary['revenue'] + $tips['amount'], 4 ); self::cache_set( $cache_key, $summary ); return $summary; } /** * 期間区切りの推移を返す(グラフと明細表の共通データ)。 * * @param string $unit 'day' / 'month' / 'year'。 * @param string $from 開始日時。 * @param string $to 終了日時。 * @return array[] 各要素 array{key:string,label:string,count:int,article:float,tip:float,total:float} */ public static function get_series( $unit, $from, $to ) { $unit = self::sanitize_unit( $unit ); $cache_key = 'series|' . $unit . '|' . $from . '|' . $to; $cached = self::cache_get( $cache_key ); if ( is_array( $cached ) ) { return $cached; } $access = self::get_access(); $article = $access ? $access->get_period_totals( $unit, array( 'status' => ACSCW_Access::STATUS_ACTIVE, 'date_from' => $from, 'date_to' => $to, ) ) : array(); $tips = array(); foreach ( self::get_tip_records( $from, $to ) as $record ) { if ( $record['refunded'] ) { continue; } $key = self::period_key( $record['date'], $unit ); if ( ! isset( $tips[ $key ] ) ) { $tips[ $key ] = 0.0; } $tips[ $key ] += $record['amount']; } $series = array(); foreach ( self::build_period_keys( $unit, $from, $to, array_keys( $article ), array_keys( $tips ) ) as $key ) { $count = isset( $article[ $key ] ) ? (int) $article[ $key ]['count'] : 0; $article_sum = isset( $article[ $key ] ) ? (float) $article[ $key ]['amount'] : 0.0; $tip_sum = isset( $tips[ $key ] ) ? (float) $tips[ $key ] : 0.0; $series[] = array( 'key' => $key, 'label' => self::format_period_label( $key, $unit ), 'count' => $count, 'article' => round( $article_sum, 4 ), 'tip' => round( $tip_sum, 4 ), 'total' => round( $article_sum + $tip_sum, 4 ), ); } self::cache_set( $cache_key, $series ); return $series; } /** * コンテンツ別の実績を返す。 * * @param int $limit 上限件数。0 で無制限。 * @return array キーは post_id。 */ public static function get_content_totals( $limit = 0 ) { $cache_key = 'content|' . (int) $limit; $cached = self::cache_get( $cache_key ); if ( is_array( $cached ) ) { return $cached; } $access = self::get_access(); $rows = $access ? $access->get_post_totals( array( 'status' => ACSCW_Access::STATUS_ACTIVE, 'number' => (int) $limit, ) ) : array(); $totals = array(); foreach ( $rows as $post_id => $row ) { $totals[ (int) $post_id ] = array( 'sales' => (int) $row['count'], 'revenue' => (float) $row['amount'], 'tip' => 0.0, 'tip_count' => 0, ); } foreach ( self::get_tip_records( '', '' ) as $record ) { $post_id = (int) $record['post_id']; if ( $post_id <= 0 || $record['refunded'] ) { continue; } if ( ! isset( $totals[ $post_id ] ) ) { $totals[ $post_id ] = array( 'sales' => 0, 'revenue' => 0.0, 'tip' => 0.0, 'tip_count' => 0, ); } $totals[ $post_id ]['tip'] += $record['amount']; ++$totals[ $post_id ]['tip_count']; } self::cache_set( $cache_key, $totals ); return $totals; } /** * 1コンテンツの実績を返す(ブロックエディタの「この記事の実績」パネル)。 * * @param int $post_id コンテンツID。 * @return array{sales:int,revenue:float,tip:float,tip_count:int} */ public static function get_post_stats( $post_id ) { $post_id = absint( $post_id ); $totals = self::get_content_totals( 0 ); if ( isset( $totals[ $post_id ] ) ) { return $totals[ $post_id ]; } return array( 'sales' => 0, 'revenue' => 0.0, 'tip' => 0.0, 'tip_count' => 0, ); } /** * 最初の販売日。 * * @return string 'Y-m-d H:i:s'。無ければ空文字。 */ public static function get_first_sale_date() { $access = self::get_access(); return $access ? $access->get_first_granted_at() : ''; } /* * ----------------------------------------------------------------------- * 投げ銭(注文側から集計する) * ----------------------------------------------------------------------- */ /** * 期間内の投げ銭明細を返す。 * * 投げ銭はアクセス表に記録しない(フェーズ3の判断)。 * したがってここだけは WooCommerce の注文を読む。 * HPOS 環境でも動くよう、テーブルを直接叩かず wc_get_orders() を使う。 * * @param string $from 開始日時。 * @param string $to 終了日時。 * @return array[] 各要素 array{date:string,amount:float,post_id:int,order_id:int,refunded:bool} */ public static function get_tip_records( $from = '', $to = '' ) { $cache_key = 'tips|' . $from . '|' . $to; $cached = self::cache_get( $cache_key ); if ( is_array( $cached ) ) { return $cached; } $records = self::fetch_tip_records( $from, $to ); self::cache_set( $cache_key, $records ); return $records; } /** * 投げ銭明細を注文から取り出す。 * * @param string $from 開始日時。 * @param string $to 終了日時。 * @return array[] */ private static function fetch_tip_records( $from, $to ) { if ( ! function_exists( 'wc_get_orders' ) ) { return array(); } /** * 1回の集計で読む注文の上限。 * * 大規模ストアでメモリを使い切らないための安全弁。 * * @since 3.0.0 * * @param int $limit 既定 2000。 */ $limit = (int) apply_filters( 'acscw_stats_order_limit', 2000 ); $args = array( 'limit' => $limit, 'type' => 'shop_order', 'status' => array( 'processing', 'completed', 'on-hold', 'refunded' ), 'orderby' => 'date', 'order' => 'DESC', 'return' => 'objects', ); $date_query = self::build_date_query( $from, $to ); if ( '' !== $date_query ) { $args['date_created'] = $date_query; } $orders = wc_get_orders( $args ); $records = array(); foreach ( (array) $orders as $order ) { if ( ! is_object( $order ) || ! method_exists( $order, 'get_items' ) ) { continue; } $created = method_exists( $order, 'get_date_created' ) ? $order->get_date_created() : null; $date = ( $created && method_exists( $created, 'date' ) ) ? $created->date( 'Y-m-d H:i:s' ) : ''; if ( '' === $date ) { continue; } $refunded = ( 'refunded' === (string) $order->get_status() ); foreach ( $order->get_items() as $item ) { if ( ! is_object( $item ) || ! method_exists( $item, 'get_product_id' ) ) { continue; } if ( ACSCW_Products::TYPE_TIP !== ACSCW_Products::get_product_type( $item->get_product_id() ) ) { continue; } $total = method_exists( $item, 'get_total' ) ? (float) $item->get_total() : 0.0; $tax = method_exists( $item, 'get_total_tax' ) ? (float) $item->get_total_tax() : 0.0; $records[] = array( 'date' => $date, 'amount' => round( $total + $tax, 4 ), 'post_id' => method_exists( $item, 'get_meta' ) ? absint( $item->get_meta( ACSCW_Checkout::ORDER_ITEM_POST_ID ) ) : 0, 'order_id' => (int) $order->get_id(), 'refunded' => $refunded, ); } } return $records; } /** * wc_get_orders() に渡す date_created の条件を作る。 * * @param string $from 開始日時。 * @param string $to 終了日時。 * @return string 条件が無ければ空文字。 */ private static function build_date_query( $from, $to ) { $from_date = ( '' === $from ) ? '' : substr( $from, 0, 10 ); $to_date = ( '' === $to ) ? '' : substr( $to, 0, 10 ); if ( '' !== $from_date && '' !== $to_date ) { return $from_date . '...' . $to_date; } if ( '' !== $from_date ) { return '>=' . $from_date; } if ( '' !== $to_date ) { return '<=' . $to_date; } return ''; } /** * 投げ銭明細を合計する。 * * @param array[] $records 明細。 * @return array{count:int,amount:float} */ private static function sum_tips( $records ) { $count = 0; $amount = 0.0; foreach ( (array) $records as $record ) { if ( $record['refunded'] ) { continue; } ++$count; $amount += (float) $record['amount']; } return array( 'count' => $count, 'amount' => round( $amount, 4 ), ); } /* * ----------------------------------------------------------------------- * 期間ラベル * ----------------------------------------------------------------------- */ /** * 単位名を許可リストで丸める。 * * @param string $unit 単位。 * @return string 'day' / 'month' / 'year'。 */ public static function sanitize_unit( $unit ) { return in_array( $unit, array( 'day', 'month', 'year' ), true ) ? $unit : 'day'; } /** * 日時から期間キーを作る。 * * @param string $datetime 'Y-m-d H:i:s'。 * @param string $unit 単位。 * @return string */ public static function period_key( $datetime, $unit ) { $length = array( 'day' => 10, 'month' => 7, 'year' => 4, ); $unit = self::sanitize_unit( $unit ); return substr( (string) $datetime, 0, $length[ $unit ] ); } /** * 期間キーを表示用ラベルにする。 * * v2 は日別でも `strtotime( $key . '-01' )` を呼んでいたため * '2026-08-23-01' を解釈させることになり、日別表の期間列が全行同じ値になっていた * (再設計案 §2-1⑥)。単位ごとに補う文字列を変える。 * * @param string $key 期間キー。 * @param string $unit 単位。 * @return string */ public static function format_period_label( $key, $unit ) { $key = (string) $key; $unit = self::sanitize_unit( $unit ); if ( 'year' === $unit ) { return $key; } $suffix = ( 'month' === $unit ) ? '-01 00:00:00' : ' 00:00:00'; $timestamp = strtotime( $key . $suffix ); if ( false === $timestamp ) { return $key; } if ( 'month' === $unit ) { return date_i18n( 'Y-m', $timestamp ); } return date_i18n( 'Y-m-d', $timestamp ); } /** * グラフに並べる期間キーの一覧を作る。 * * 空の期間も0として並べないと、棒グラフの間隔が実際の日付とずれる。 * * @param string $unit 単位。 * @param string $from 開始日時。 * @param string $to 終了日時。 * @param string[] $keys_a データ側のキー1。 * @param string[] $keys_b データ側のキー2。 * @return string[] 昇順。 */ private static function build_period_keys( $unit, $from, $to, $keys_a, $keys_b ) { $existing = array_unique( array_merge( (array) $keys_a, (array) $keys_b ) ); sort( $existing ); if ( '' === $from || '' === $to ) { return $existing; } $start = strtotime( self::period_key( $from, $unit ) . ( 'day' === $unit ? '' : ( 'month' === $unit ? '-01' : '-01-01' ) ) ); $end = strtotime( self::period_key( $to, $unit ) . ( 'day' === $unit ? '' : ( 'month' === $unit ? '-01' : '-01-01' ) ) ); if ( false === $start || false === $end || $start > $end ) { return $existing; } $keys = array(); $cursor = $start; $guard = 0; while ( $cursor <= $end && $guard < 400 ) { $keys[] = self::period_key( gmdate( 'Y-m-d H:i:s', $cursor ), $unit ); ++$guard; if ( 'day' === $unit ) { $cursor += DAY_IN_SECONDS; } elseif ( 'month' === $unit ) { $cursor = strtotime( '+1 month', $cursor ); } else { $cursor = strtotime( '+1 year', $cursor ); } } // 上限に達した場合でも、データのある期間は必ず含める。 return array_values( array_unique( array_merge( $keys, $existing ) ) ); } /* * ----------------------------------------------------------------------- * 内部 * ----------------------------------------------------------------------- */ /** * アクセス記録コンポーネントを取得する。 * * @return ACSCW_Access|null */ private static function get_access() { $access = ACSCW_Plugin::instance()->get( 'access' ); return ( $access instanceof ACSCW_Access ) ? $access : null; } }