*/ private $totals = array(); /** * 現在の絞り込み。 * * @var array */ private $filters = array(); /** * 表を初期化する。 */ public function __construct() { parent::__construct( array( 'singular' => 'acscw_item', 'plural' => 'acscw_items', 'ajax' => false, ) ); } /** * 列定義。 * * @return array */ public function get_columns() { return array( 'title' => __( 'Title', 'acs-content-for-woocommerce' ), 'kind' => __( 'Type', 'acs-content-for-woocommerce' ), 'status' => __( 'Status', 'acs-content-for-woocommerce' ), 'price' => __( 'Price', 'acs-content-for-woocommerce' ), 'sales' => __( 'Sales', 'acs-content-for-woocommerce' ), 'revenue' => __( 'Revenue', 'acs-content-for-woocommerce' ), 'tip' => __( 'Tips', 'acs-content-for-woocommerce' ), 'actions' => __( 'Actions', 'acs-content-for-woocommerce' ), ); } /** * 並び替えできる列。 * * @return array */ public function get_sortable_columns() { return array( 'title' => array( 'post_title', false ), ); } /** * データを用意する。 * * @return void */ public function prepare_items() { $this->filters = $this->read_filters(); $per_page = ACSCW_Items_Page::PER_PAGE; $paged = max( 1, (int) $this->get_pagenum() ); $args = array( 'kind' => $this->filters['kind'], 'post_type' => $this->filters['post_type'], 'status' => $this->filters['status'], 'search' => $this->filters['s'], 'orderby' => $this->filters['orderby'], 'order' => $this->filters['order'], ); $total = ACSCW_Items_Page::count_items( $args ); $args['number'] = 0; $all = ACSCW_Items_Page::query_items( $args ); $this->items = array_slice( $all, ( $paged - 1 ) * $per_page, $per_page ); $this->totals = ACSCW_Stats::get_content_totals( 0 ); $this->_column_headers = array( $this->get_columns(), array(), $this->get_sortable_columns() ); $this->set_pagination_args( array( 'total_items' => $total, 'per_page' => $per_page, 'total_pages' => (int) ceil( $total / $per_page ), ) ); } /** * リクエストから絞り込みを読む。 * * @return array */ private function read_filters() { // phpcs:disable WordPress.Security.NonceVerification.Recommended return array( 'kind' => isset( $_GET['kind'] ) ? sanitize_key( wp_unslash( $_GET['kind'] ) ) : 'all', 'post_type' => isset( $_GET['acscw_post_type'] ) ? sanitize_key( wp_unslash( $_GET['acscw_post_type'] ) ) : '', 'status' => isset( $_GET['acscw_status'] ) ? sanitize_key( wp_unslash( $_GET['acscw_status'] ) ) : '', 's' => isset( $_GET['s'] ) ? sanitize_text_field( wp_unslash( $_GET['s'] ) ) : '', 'orderby' => isset( $_GET['orderby'] ) ? sanitize_key( wp_unslash( $_GET['orderby'] ) ) : 'post_date', 'order' => isset( $_GET['order'] ) ? sanitize_key( wp_unslash( $_GET['order'] ) ) : 'DESC', ); // phpcs:enable WordPress.Security.NonceVerification.Recommended } /** * 絞り込みバーを描画する。 * * @return void */ public function render_filters() { $filters = empty( $this->filters ) ? $this->read_filters() : $this->filters; echo '
'; $kinds = array( 'all' => __( 'All types', 'acs-content-for-woocommerce' ), 'paywall' => __( 'Paid Area', 'acs-content-for-woocommerce' ), 'tip' => __( 'Tips', 'acs-content-for-woocommerce' ), 'both' => __( 'Both', 'acs-content-for-woocommerce' ), ); $this->render_select( 'kind', $kinds, $filters['kind'], __( 'Type', 'acs-content-for-woocommerce' ) ); $types = array( '' => __( 'All post types', 'acs-content-for-woocommerce' ) ); foreach ( ACSCW_Products::get_supported_post_types() as $type ) { $object = get_post_type_object( $type ); $types[ $type ] = ( $object && isset( $object->labels->name ) ) ? $object->labels->name : $type; } $this->render_select( 'acscw_post_type', $types, $filters['post_type'], __( 'Post type', 'acs-content-for-woocommerce' ) ); $statuses = array( '' => __( 'All statuses', 'acs-content-for-woocommerce' ) ) + ACSCW_Items_Page::get_statuses(); $this->render_select( 'acscw_status', $statuses, $filters['status'], __( 'Status', 'acs-content-for-woocommerce' ) ); printf( '', esc_html__( 'Filter', 'acs-content-for-woocommerce' ) ); echo '
'; } /** * セレクトボックスを描画する。 * * @param string $name フィールド名。 * @param string[] $options 選択肢。 * @param string $current 現在値。 * @param string $label ラベル。 * @return void */ private function render_select( $name, $options, $current, $label ) { printf( ''; } /** * データが無いときの表示。 * * @return void */ public function no_items() { echo ''; esc_html_e( 'No content carries a Paid Area or Tip Jar block yet.', 'acs-content-for-woocommerce' ); echo ''; } /** * タイトル列。 * * @param array $item 行データ。 * @return string */ public function column_title( $item ) { $object = get_post_type_object( $item['post_type'] ); $type = ( $object && isset( $object->labels->singular_name ) ) ? $object->labels->singular_name : $item['post_type']; $title = ( '' === trim( $item['title'] ) ) ? __( '(no title)', 'acs-content-for-woocommerce' ) : $item['title']; return sprintf( '%2$s
%3$s', esc_url( (string) get_edit_post_link( $item['id'] ) ), esc_html( $title ), esc_html( $type . ' ・ ' . mysql2date( 'Y-m-d', $item['date'] ) ) ); } /** * 種別列。 * * @param array $item 行データ。 * @return string */ public function column_kind( $item ) { $badges = array(); if ( in_array( $item['kind'], array( 'paywall', 'both' ), true ) ) { $badges[] = '' . esc_html__( 'Paid Area', 'acs-content-for-woocommerce' ) . ''; } if ( in_array( $item['kind'], array( 'tip', 'both' ), true ) ) { $badges[] = '' . esc_html__( 'Tips', 'acs-content-for-woocommerce' ) . ''; } return implode( ' ', $badges ); } /** * 状態列。 * * @param array $item 行データ。 * @return string */ public function column_status( $item ) { $statuses = ACSCW_Items_Page::get_statuses(); $label = isset( $statuses[ $item['status'] ] ) ? $statuses[ $item['status'] ] : $item['status']; $class = ( 'publish' === $item['status'] ) ? 'acscw-badge is-live' : 'acscw-badge is-draft'; return sprintf( '%2$s', esc_attr( $class ), esc_html( $label ) ); } /** * 価格列(インライン編集)。 * * @param array $item 行データ。 * @return string */ public function column_price( $item ) { if ( ! in_array( $item['kind'], array( 'paywall', 'both' ), true ) ) { return '—'; } return sprintf( ' ', esc_attr( (string) ACSCW_Products::get_content_price( $item['id'] ) ), absint( $item['id'] ), esc_attr__( 'Price', 'acs-content-for-woocommerce' ) ); } /** * 販売数列。 * * @param array $item 行データ。 * @return string */ public function column_sales( $item ) { $total = $this->get_total( $item['id'] ); return ( $total['sales'] > 0 ) ? esc_html( number_format_i18n( $total['sales'] ) ) : '—'; } /** * 売上列。 * * @param array $item 行データ。 * @return string */ public function column_revenue( $item ) { $total = $this->get_total( $item['id'] ); return ( $total['revenue'] > 0 ) ? ACSCW_Admin::price( $total['revenue'] ) : '—'; } /** * 投げ銭列。 * * @param array $item 行データ。 * @return string */ public function column_tip( $item ) { $total = $this->get_total( $item['id'] ); return ( $total['tip'] > 0 ) ? ACSCW_Admin::price( $total['tip'] ) : '—'; } /** * 操作列。 * * @param array $item 行データ。 * @return string */ public function column_actions( $item ) { $links = array( sprintf( '%2$s', esc_url( (string) get_edit_post_link( $item['id'] ) ), esc_html__( 'Edit', 'acs-content-for-woocommerce' ) ), ); if ( in_array( $item['kind'], array( 'paywall', 'both' ), true ) ) { $links[] = sprintf( '%2$s', esc_url( ACSCW_Admin::page_url( ACSCW_Buyers_Page::PAGE_SLUG, array( 'acscw_post' => $item['id'] ) ) ), esc_html__( 'Buyers', 'acs-content-for-woocommerce' ) ); } return implode( ' ', $links ); } /** * 定義の無い列。 * * @param array $item 行データ。 * @param string $column_name 列名。 * @return string */ public function column_default( $item, $column_name ) { return isset( $item[ $column_name ] ) ? esc_html( (string) $item[ $column_name ] ) : ''; } /** * 行の追加クラス。 * * @return string */ protected function get_table_classes() { return array( 'widefat', 'striped', 'wp-list-table', 'acscw-table' ); } /** * コンテンツ別の実績を取り出す。 * * @param int $post_id コンテンツID。 * @return array{sales:int,revenue:float,tip:float,tip_count:int} */ private function get_total( $post_id ) { $post_id = (int) $post_id; if ( isset( $this->totals[ $post_id ] ) ) { return $this->totals[ $post_id ]; } return array( 'sales' => 0, 'revenue' => 0.0, 'tip' => 0.0, 'tip_count' => 0, ); } }