page = $page; parent::__construct( array( 'singular' => 'acscw_buyer', 'plural' => 'acscw_buyers', 'ajax' => false, ) ); } /** * 列定義。 * * @return array */ public function get_columns() { return array( 'granted_at' => __( 'Purchased', 'acs-content-for-woocommerce' ), 'email' => __( 'Buyers', 'acs-content-for-woocommerce' ), 'content' => __( 'Content', 'acs-content-for-woocommerce' ), 'order' => __( 'Order', 'acs-content-for-woocommerce' ), 'amount' => __( 'Amount paid', 'acs-content-for-woocommerce' ), 'status' => __( 'Status', 'acs-content-for-woocommerce' ), 'actions' => __( 'Actions', 'acs-content-for-woocommerce' ), ); } /** * データを用意する。 * * @return void */ public function prepare_items() { $access = ACSCW_Plugin::instance()->get( 'access' ); if ( ! $access instanceof ACSCW_Access ) { $this->items = array(); return; } $per_page = ACSCW_Buyers_Page::PER_PAGE; $paged = max( 1, (int) $this->get_pagenum() ); $args = $this->page->read_query_args(); $total = $access->count_records( $args ); $this->items = $access->get_records( array_merge( $args, array( 'number' => $per_page, 'offset' => ( $paged - 1 ) * $per_page, 'orderby' => 'granted_at', 'order' => 'DESC', ) ) ); $this->_column_headers = array( $this->get_columns(), array(), array() ); $this->set_pagination_args( array( 'total_items' => $total, 'per_page' => $per_page, 'total_pages' => (int) ceil( $total / $per_page ), ) ); } /** * 絞り込みバーと CSV ボタンを描画する。 * * @return void */ public function render_filters() { // phpcs:disable WordPress.Security.NonceVerification.Recommended $post_id = isset( $_GET['acscw_post'] ) ? absint( wp_unslash( $_GET['acscw_post'] ) ) : 0; $state = isset( $_GET['acscw_state'] ) ? sanitize_key( wp_unslash( $_GET['acscw_state'] ) ) : ''; // phpcs:enable WordPress.Security.NonceVerification.Recommended echo '
'; printf( ''; $states = array( '' => __( 'All statuses', 'acs-content-for-woocommerce' ), ACSCW_Access::STATUS_ACTIVE => __( 'Active', 'acs-content-for-woocommerce' ), ACSCW_Access::STATUS_REVOKED => __( 'Revoked', 'acs-content-for-woocommerce' ), ); printf( ''; printf( '', esc_html__( 'Filter', 'acs-content-for-woocommerce' ) ); printf( '%2$s', esc_url( wp_nonce_url( add_query_arg( array( 'action' => ACSCW_Buyers_Page::ACTION_EXPORT, 'acscw_post' => $post_id, ), admin_url( 'admin-post.php' ) ), ACSCW_Buyers_Page::ACTION_EXPORT ) ), esc_html__( 'Download CSV', 'acs-content-for-woocommerce' ) ); echo '
'; } /** * データが無いときの表示。 * * @return void */ public function no_items() { echo ''; esc_html_e( 'No matching buyers.', 'acs-content-for-woocommerce' ); echo ''; } /** * 購入日時列。 * * @param array $item 行データ。 * @return string */ public function column_granted_at( $item ) { return esc_html( mysql2date( 'Y-m-d H:i', $item['granted_at'] ) ); } /** * 購入者列(マスク表示)。 * * @param array $item 行データ。 * @return string */ public function column_email( $item ) { return '' . esc_html( ACSCW_Access::mask_email( $item['email'] ) ) . ''; } /** * コンテンツ列。 * * @param array $item 行データ。 * @return string */ public function column_content( $item ) { $post_id = (int) $item['post_id']; if ( 0 === $post_id ) { return '' . esc_html__( 'Site-wide', 'acs-content-for-woocommerce' ) . ''; } $title = get_the_title( $post_id ); if ( '' === $title ) { return '#' . absint( $post_id ) . ''; } return sprintf( '%2$s', esc_url( (string) get_edit_post_link( $post_id ) ), esc_html( $title ) ); } /** * 注文列。 * * @param array $item 行データ。 * @return string */ public function column_order( $item ) { $order_id = (int) $item['order_id']; if ( 0 === $order_id ) { return '' . esc_html__( 'Manual grant', 'acs-content-for-woocommerce' ) . ''; } $url = function_exists( 'wc_get_order' ) ? $this->get_order_url( $order_id ) : ''; if ( '' === $url ) { return '#' . absint( $order_id ); } return sprintf( '#%2$d', esc_url( $url ), absint( $order_id ) ); } /** * 注文の編集URLを返す(HPOS でも壊れないように注文オブジェクトから取る)。 * * @param int $order_id 注文ID。 * @return string */ private function get_order_url( $order_id ) { $order = wc_get_order( $order_id ); if ( ! $order || ! method_exists( $order, 'get_edit_order_url' ) ) { return ''; } return (string) $order->get_edit_order_url(); } /** * 支払額列。 * * @param array $item 行データ。 * @return string */ public function column_amount( $item ) { $amount = (float) $item['amount']; if ( $amount <= 0 ) { return '—'; } return ACSCW_Admin::price( $amount ); } /** * 状態列。 * * @param array $item 行データ。 * @return string */ public function column_status( $item ) { if ( ACSCW_Access::STATUS_ACTIVE === $item['status'] ) { return '' . esc_html__( 'Active', 'acs-content-for-woocommerce' ) . ''; } $label = ( ACSCW_Access::SOURCE_REFUND_REVOKED === $item['source'] ) ? __( 'Refunded', 'acs-content-for-woocommerce' ) : __( 'Revoked', 'acs-content-for-woocommerce' ); return '' . esc_html( $label ) . ''; } /** * 操作列。 * * @param array $item 行データ。 * @return string */ public function column_actions( $item ) { $id = (int) $item['id']; if ( ACSCW_Access::STATUS_ACTIVE !== $item['status'] ) { return '—'; } $resend = wp_nonce_url( add_query_arg( array( 'action' => ACSCW_Buyers_Page::ACTION_RESEND, 'id' => $id, ), admin_url( 'admin-post.php' ) ), ACSCW_Buyers_Page::ACTION_RESEND . '_' . $id ); $revoke = wp_nonce_url( add_query_arg( array( 'action' => ACSCW_Buyers_Page::ACTION_REVOKE, 'id' => $id, ), admin_url( 'admin-post.php' ) ), ACSCW_Buyers_Page::ACTION_REVOKE . '_' . $id ); return sprintf( '%2$s %4$s', esc_url( $resend ), esc_html__( 'Resend', 'acs-content-for-woocommerce' ), esc_url( $revoke ), esc_html__( 'Revoke', 'acs-content-for-woocommerce' ) ); } /** * 定義の無い列。 * * @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' ); } }