render_scorecards();
echo '
';
$this->render_chart_card();
$this->render_setup_card();
echo '
';
$this->render_top_contents();
ACSCW_Admin::page_footer();
}
/*
* -----------------------------------------------------------------------
* スコアカード
* -----------------------------------------------------------------------
*/
/**
* 期間つきスコアカードを描画する。
*
* @return void
*/
private function render_scorecards() {
$cards = array();
foreach ( array( 'today', '7days', '30days' ) as $preset ) {
$range = ACSCW_Stats::get_range( $preset );
$summary = ACSCW_Stats::get_summary( $range['from'], $range['to'] );
$note = '';
$previous = ACSCW_Stats::get_previous_range( $preset );
if ( $previous ) {
$before = ACSCW_Stats::get_summary( $previous['from'], $previous['to'] );
$delta = ACSCW_Admin::delta_label( $summary['total'], $before['total'] );
if ( '' !== $delta ) {
$note = esc_html__( 'vs previous period', 'acs-content-for-woocommerce' ) . ' ' . $delta;
}
}
$cards[] = array(
'period' => $range['label'],
'rows' => array(
array(
'label' => __( 'Revenue', 'acs-content-for-woocommerce' ),
'value' => ACSCW_Admin::price( $summary['total'] ),
),
array(
'label' => __( 'Article sales', 'acs-content-for-woocommerce' ),
'value' => esc_html( $this->count_label( $summary['sales'] ) ),
'small' => true,
),
array(
'label' => __( 'Tips', 'acs-content-for-woocommerce' ),
'value' => esc_html( $this->count_label( $summary['tip_count'] ) ),
'small' => true,
'gold' => true,
),
),
'note' => $note,
);
}
$all = ACSCW_Stats::get_summary( '', '' );
$first = ACSCW_Stats::get_first_sale_date();
$repeat_rate = ( $all['buyers'] > 0 )
? number_format_i18n( ( $all['repeat_buyers'] / $all['buyers'] ) * 100, 1 ) . '%'
: '—';
$cards[] = array(
'period' => __( 'All time', 'acs-content-for-woocommerce' ),
'rows' => array(
array(
'label' => __( 'Revenue', 'acs-content-for-woocommerce' ),
'value' => ACSCW_Admin::price( $all['total'] ),
),
array(
'label' => __( 'Buyers (unique)', 'acs-content-for-woocommerce' ),
'value' => esc_html( number_format_i18n( $all['buyers'] ) ),
'small' => true,
),
array(
'label' => __( 'Repeat rate', 'acs-content-for-woocommerce' ),
'value' => esc_html( $repeat_rate ),
'small' => true,
'gold' => true,
),
),
'note' => ( '' === $first )
? esc_html__( 'No sales yet', 'acs-content-for-woocommerce' )
: esc_html(
sprintf(
/* translators: %s: date */
__( 'First sale %s', 'acs-content-for-woocommerce' ),
mysql2date( 'Y-m-d', $first )
)
),
);
ACSCW_Admin::render_scorecards( $cards );
}
/**
* 件数のラベル。
*
* @param int $count 件数。
* @return string
*/
private function count_label( $count ) {
return sprintf(
/* translators: %s: count. */
_n( '%s item', '%s items', (int) $count, 'acs-content-for-woocommerce' ),
number_format_i18n( (int) $count )
);
}
/*
* -----------------------------------------------------------------------
* グラフ
* -----------------------------------------------------------------------
*/
/**
* 売上推移カードを描画する。
*
* @return void
*/
private function render_chart_card() {
$range = ACSCW_Stats::get_range( '30days' );
$series = ACSCW_Stats::get_series( 'day', $range['from'], $range['to'] );
$link = sprintf(
'%2$s',
esc_url( ACSCW_Admin::page_url( ACSCW_Reports_Page::PAGE_SLUG ) ),
esc_html__( 'Open reports', 'acs-content-for-woocommerce' )
);
ACSCW_Admin::card_open( __( 'Revenue trend (last 30 days)', 'acs-content-for-woocommerce' ), $link );
ACSCW_Admin::render_bar_chart(
$series,
__( 'Daily revenue over the last 30 days', 'acs-content-for-woocommerce' )
);
ACSCW_Admin::card_close();
}
/*
* -----------------------------------------------------------------------
* セットアップ進捗
* -----------------------------------------------------------------------
*/
/**
* セットアップ進捗カードを描画する。
*
* @return void
*/
private function render_setup_card() {
$steps = $this->get_setup_steps();
$done = 0;
foreach ( $steps as $step ) {
if ( $step['done'] ) {
++$done;
}
}
ACSCW_Admin::card_open( __( 'Setup', 'acs-content-for-woocommerce' ) );
ACSCW_Admin::render_progress( $done, count( $steps ) );
echo '';
$index = 0;
foreach ( $steps as $step ) {
++$index;
// 完了マークは CSS の content で描く(PHP に記号を書かない)。
printf(
'- %2$s%3$s%4$s%5$s
',
$step['done'] ? 'is-done' : 'is-todo',
$step['done'] ? '' : absint( $index ),
esc_html( $step['title'] ),
esc_html( $step['done'] ? __( 'Done', 'acs-content-for-woocommerce' ) : __( 'Not done', 'acs-content-for-woocommerce' ) ),
wp_kses_post( $step['description'] )
);
}
echo '
';
ACSCW_Admin::card_close();
}
/**
* セットアップの5ステップを返す。
*
* @return array[] 各要素 array{done:bool,title:string,description:string}
*/
public function get_setup_steps() {
$plugin = ACSCW_Plugin::instance();
$summary = ACSCW_Stats::get_summary( '', '' );
$items = ACSCW_Items_Page::count_items(
array(
'kind' => 'paywall',
'status' => 'publish',
)
);
$gateways = $this->get_enabled_gateway_titles();
return array(
array(
'done' => $plugin->is_woocommerce_active(),
'title' => __( 'WooCommerce is active', 'acs-content-for-woocommerce' ),
'description' => defined( 'WC_VERSION' )
? esc_html( sprintf( /* translators: %s: version */ __( 'Version %s', 'acs-content-for-woocommerce' ), WC_VERSION ) )
: esc_html__( 'Install and activate WooCommerce.', 'acs-content-for-woocommerce' ),
),
array(
'done' => ! empty( $gateways ),
'title' => __( 'At least one payment method is enabled', 'acs-content-for-woocommerce' ),
'description' => empty( $gateways )
? sprintf(
'%2$s',
esc_url( admin_url( 'admin.php?page=wc-settings&tab=checkout' ) ),
esc_html__( 'Open WooCommerce payment settings', 'acs-content-for-woocommerce' )
)
: esc_html( implode( ' / ', $gateways ) ),
),
array(
'done' => ACSCW_Cart_Context::is_simplification_enabled(),
'title' => __( 'Checkout simplification', 'acs-content-for-woocommerce' ),
'description' => ACSCW_Cart_Context::is_simplification_enabled()
? esc_html__( 'Applied to paid-content-only purchases.', 'acs-content-for-woocommerce' )
: sprintf(
'%2$s',
esc_url( ACSCW_Admin::page_url( ACSCW_Settings::PAGE_SLUG, array( 'tab' => 'checkout' ) ) ),
esc_html__( 'Enable it in Settings › Checkout', 'acs-content-for-woocommerce' )
),
),
array(
'done' => ( $items > 0 ),
'title' => __( 'Publish your first paid content', 'acs-content-for-woocommerce' ),
'description' => ( $items > 0 )
? esc_html( sprintf( /* translators: %d: count */ __( '%d paid item published.', 'acs-content-for-woocommerce' ), $items ) )
: esc_html__( 'Insert a "Paid Area" block into a post and everything after it becomes paid.', 'acs-content-for-woocommerce' ),
),
array(
'done' => ( $summary['sales'] > 0 ),
'title' => __( 'First purchase', 'acs-content-for-woocommerce' ),
'description' => ( $summary['sales'] > 0 )
? esc_html( sprintf( /* translators: %d: count */ __( '%d purchases so far.', 'acs-content-for-woocommerce' ), $summary['sales'] ) )
: esc_html__( 'Once a purchase happens, it shows up here and in the sales report.', 'acs-content-for-woocommerce' ),
),
);
}
/**
* 有効な決済方法の名前を返す。
*
* WooCommerce の設定は読むだけで、書き換えない。
*
* @return string[]
*/
private function get_enabled_gateway_titles() {
if ( ! function_exists( 'WC' ) ) {
return array();
}
$wc = WC();
if ( ! is_object( $wc ) || ! method_exists( $wc, 'payment_gateways' ) ) {
return array();
}
$gateways = $wc->payment_gateways();
if ( ! is_object( $gateways ) || ! method_exists( $gateways, 'get_available_payment_gateways' ) ) {
return array();
}
$titles = array();
foreach ( (array) $gateways->get_available_payment_gateways() as $gateway ) {
if ( is_object( $gateway ) && isset( $gateway->title ) ) {
$titles[] = wp_strip_all_tags( (string) $gateway->title );
}
}
return $titles;
}
/*
* -----------------------------------------------------------------------
* 売れているコンテンツ
* -----------------------------------------------------------------------
*/
/**
* 売れているコンテンツの表を描画する。
*
* @return void
*/
private function render_top_contents() {
$totals = ACSCW_Stats::get_content_totals( 0 );
uasort(
$totals,
static function ( $a, $b ) {
$left = $a['revenue'] + $a['tip'];
$right = $b['revenue'] + $b['tip'];
if ( $left === $right ) {
return 0;
}
return ( $left < $right ) ? 1 : -1;
}
);
$link = sprintf(
'%2$s',
esc_url( ACSCW_Admin::page_url( ACSCW_Items_Page::PAGE_SLUG ) ),
esc_html__( 'View all', 'acs-content-for-woocommerce' )
);
ACSCW_Admin::card_open( __( 'Top selling paid content', 'acs-content-for-woocommerce' ), $link );
if ( empty( $totals ) ) {
ACSCW_Admin::empty_state( __( 'No sales recorded yet.', 'acs-content-for-woocommerce' ) );
ACSCW_Admin::card_close();
return;
}
echo '';
printf( '| %s | ', esc_html__( 'Title', 'acs-content-for-woocommerce' ) );
printf( '%s | ', esc_html__( 'Price', 'acs-content-for-woocommerce' ) );
printf( '%s | ', esc_html__( 'Sales', 'acs-content-for-woocommerce' ) );
printf( '%s | ', esc_html__( 'Revenue', 'acs-content-for-woocommerce' ) );
printf( '%s | ', esc_html__( 'Tips', 'acs-content-for-woocommerce' ) );
echo '
';
foreach ( array_slice( $totals, 0, 10, true ) as $post_id => $total ) {
$title = get_the_title( (int) $post_id );
printf(
'| %2$s | %3$s | %4$s | %5$s | %6$s |
',
esc_url( (string) get_edit_post_link( (int) $post_id ) ),
esc_html( ( '' === $title ) ? ( '#' . (int) $post_id ) : $title ),
wp_kses_post( ACSCW_Admin::price( ACSCW_Products::get_content_price( (int) $post_id ) ) ),
esc_html( number_format_i18n( $total['sales'] ) ),
wp_kses_post( ACSCW_Admin::price( $total['revenue'] ) ),
( $total['tip'] > 0 ) ? wp_kses_post( ACSCW_Admin::price( $total['tip'] ) ) : wp_kses_post( '—' )
);
}
echo '
';
printf(
'%s
',
esc_html__( 'Revenue is the amount actually paid, recorded at the time of purchase. Changing a price does not move past figures. All aggregation happens on your own site, and nothing is sent off-site.', 'acs-content-for-woocommerce' )
);
ACSCW_Admin::card_close();
}
}