get( 'access' ); if ( ! $access instanceof ACSCW_Access || 0 === $post_id ) { $this->redirect( 'grant_failed' ); } $result = $access->grant( array( 'post_id' => $post_id, 'email' => $email, 'source' => ACSCW_Access::SOURCE_MANUAL, 'note' => __( 'Granted manually from the admin', 'acs-content-for-woocommerce' ), ) ); if ( is_wp_error( $result ) ) { $this->redirect( 'grant_failed' ); } if ( $notify ) { $token = ACSCW_Plugin::instance()->get( 'token' ); if ( $token instanceof ACSCW_Access_Token ) { $token->request_access_link( $email, $post_id ); } } $this->redirect( 'granted' ); } /** * アクセスを取り消す。 * * @return void */ public function handle_revoke() { ACSCW_Admin::require_capability(); $id = isset( $_REQUEST['id'] ) ? absint( wp_unslash( $_REQUEST['id'] ) ) : 0; check_admin_referer( self::ACTION_REVOKE . '_' . $id ); $access = ACSCW_Plugin::instance()->get( 'access' ); if ( $access instanceof ACSCW_Access && $id > 0 ) { $access->revoke_by_id( $id, ACSCW_Access::SOURCE_MANUAL_REVOKED, __( 'Revoked manually from the admin', 'acs-content-for-woocommerce' ) ); ACSCW_Stats::flush(); } $this->redirect( 'revoked' ); } /** * アクセスリンクを再送する。 * * @return void */ public function handle_resend() { ACSCW_Admin::require_capability(); $id = isset( $_REQUEST['id'] ) ? absint( wp_unslash( $_REQUEST['id'] ) ) : 0; check_admin_referer( self::ACTION_RESEND . '_' . $id ); $access = ACSCW_Plugin::instance()->get( 'access' ); $token = ACSCW_Plugin::instance()->get( 'token' ); $record = ( $access instanceof ACSCW_Access ) ? $access->get_record( $id ) : null; if ( ! $record || ! $token instanceof ACSCW_Access_Token ) { $this->redirect( 'resend_failed' ); } $result = $token->request_access_link( $record['email'], (int) $record['post_id'] ); $this->redirect( is_wp_error( $result ) ? 'resend_failed' : 'resent' ); } /** * 購入者一覧を CSV で出力する。 * * ここだけは生のメールアドレスを出す。manage_woocommerce を持つ人だけが実行できる。 * * @return void */ public function handle_export() { ACSCW_Admin::require_capability(); check_admin_referer( self::ACTION_EXPORT ); $access = ACSCW_Plugin::instance()->get( 'access' ); if ( ! $access instanceof ACSCW_Access ) { $this->redirect( 'export_failed' ); } $rows = $access->get_records( array_merge( $this->read_query_args(), array( 'number' => 0, 'orderby' => 'granted_at', 'order' => 'DESC', ) ) ); $this->send_csv( 'acscw-buyers-' . gmdate( 'Ymd-His' ) . '.csv', array( __( 'Purchased', 'acs-content-for-woocommerce' ), __( 'Email address', 'acs-content-for-woocommerce' ), __( 'Content ID', 'acs-content-for-woocommerce' ), __( 'Content', 'acs-content-for-woocommerce' ), __( 'Order ID', 'acs-content-for-woocommerce' ), __( 'Amount paid', 'acs-content-for-woocommerce' ), __( 'Currency', 'acs-content-for-woocommerce' ), __( 'Source', 'acs-content-for-woocommerce' ), __( 'Status', 'acs-content-for-woocommerce' ), ), array_map( static function ( $row ) { return array( $row['granted_at'], $row['email'], $row['post_id'], get_the_title( (int) $row['post_id'] ), $row['order_id'], $row['amount'], $row['currency'], $row['source'], $row['status'], ); }, $rows ) ); } /** * CSV を送出して終了する。 * * ここで開くのは実ファイルではなく `php://output`(HTTP レスポンス本体)で、 * ファイルシステムには一切触れない。WP_Filesystem はファイルの読み書き用の * 抽象化であり、レスポンスストリームへの直接出力は扱えないため使えない。 * よって fwrite() / fclose() をそのまま使い、phpcs は行単位で抑制する。 * * @param string $filename ファイル名。 * @param array $headers 見出し行。 * @param array[] $rows データ行。 * @return void */ public static function send_csv( $filename, $headers, $rows ) { nocache_headers(); header( 'Content-Type: text/csv; charset=UTF-8' ); header( 'Content-Disposition: attachment; filename=' . $filename ); $handle = fopen( 'php://output', 'w' ); if ( false === $handle ) { exit; } // Excel が UTF-8 と判定できるように BOM を付ける。 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fwrite -- CSV stream to php://output, not filesystem. fwrite( $handle, "\xEF\xBB\xBF" ); fputcsv( $handle, $headers ); foreach ( $rows as $row ) { fputcsv( $handle, array_map( array( __CLASS__, 'escape_csv_cell' ), (array) $row ) ); } // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose -- CSV stream to php://output, not filesystem. fclose( $handle ); exit; } /** * 表計算ソフトで数式として解釈されうるセルを無害化する(CSV インジェクション対策)。 * * 投稿タイトルや購入者のメールアドレスは第三者が決められる値なので、 * 先頭が = + - @ タブ 改行 の文字列には ' を前置して文字列として扱わせる。 * * @param mixed $value セルの値。 * @return mixed */ private static function escape_csv_cell( $value ) { if ( ! is_string( $value ) || '' === $value ) { return $value; } if ( false !== strpos( "=+-@\t\r", $value[0] ) ) { return "'" . $value; } return $value; } /** * 処理後に一覧へ戻す。 * * @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 array ACSCW_Access::get_records() に渡せる形。 */ public function read_query_args() { // phpcs:disable WordPress.Security.NonceVerification.Recommended $search = isset( $_GET['s'] ) ? sanitize_text_field( wp_unslash( $_GET['s'] ) ) : ''; $post_id = isset( $_GET['acscw_post'] ) ? absint( wp_unslash( $_GET['acscw_post'] ) ) : 0; $status = isset( $_GET['acscw_state'] ) ? sanitize_key( wp_unslash( $_GET['acscw_state'] ) ) : ''; $order_id = 0; // phpcs:enable WordPress.Security.NonceVerification.Recommended // 「#20481」「20481」のような入力は注文番号として扱う。 if ( '' !== $search && preg_match( '/^#?\d+$/', $search ) ) { $order_id = absint( ltrim( $search, '#' ) ); $search = ''; } return array( 'post_id' => $post_id, 'order_id' => $order_id, 'search' => $search, 'status' => in_array( $status, array( ACSCW_Access::STATUS_ACTIVE, ACSCW_Access::STATUS_REVOKED ), true ) ? $status : '', ); } /** * 購入者画面を描画する。 * * @return void */ public function render() { ACSCW_Admin::require_capability(); require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php'; $table = new ACSCW_Buyers_Table( $this ); $table->prepare_items(); ACSCW_Admin::page_header( __( 'Buyers', 'acs-content-for-woocommerce' ), __( 'Who bought what, and where you restore or revoke access.', 'acs-content-for-woocommerce' ) ); $this->render_flash(); echo '
'; printf( '', esc_attr( self::PAGE_SLUG ) ); $table->render_filters(); $table->search_box( __( 'Email address / order number', 'acs-content-for-woocommerce' ), 'acscw-buyers' ); $table->display(); echo '
'; printf( '

%s

', esc_html__( 'Email addresses are always shown masked. Export the CSV if you need to see them in full.', 'acs-content-for-woocommerce' ) ); $this->render_grant_form(); $this->render_recovery_card(); 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( 'granted' => array( 'success', __( 'Access granted.', 'acs-content-for-woocommerce' ) ), 'grant_failed' => array( 'error', __( 'Access could not be granted. Check the email address and the content.', 'acs-content-for-woocommerce' ) ), 'revoked' => array( 'success', __( 'Access revoked.', 'acs-content-for-woocommerce' ) ), 'resent' => array( 'success', __( 'Access link sent.', 'acs-content-for-woocommerce' ) ), 'resend_failed' => array( 'error', __( 'The access link could not be sent. Please try again later.', 'acs-content-for-woocommerce' ) ), 'export_failed' => array( 'error', __( 'The CSV could not be exported.', '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_grant_form() { ACSCW_Admin::card_open( __( 'Grant access manually', 'acs-content-for-woocommerce' ) ); echo '
'; wp_nonce_field( self::ACTION_GRANT ); printf( '', esc_attr( self::ACTION_GRANT ) ); echo ''; echo ''; echo ''; echo ''; echo ''; printf( '

', esc_html__( 'Grant access', 'acs-content-for-woocommerce' ) ); echo '
'; ACSCW_Admin::card_close(); } /** * 読者側の復旧手段を説明するカードを描画する。 * * @return void */ private function render_recovery_card() { $token = ACSCW_Plugin::instance()->get( 'token' ); $cookie = ( $token instanceof ACSCW_Access_Token ) ? $token->get_cookie_lifetime() : 0; $one_time = ( $token instanceof ACSCW_Access_Token ) ? $token->get_token_lifetime() : 0; ACSCW_Admin::card_open( __( 'Reader-side access recovery', 'acs-content-for-woocommerce' ) ); printf( '

%s

', esc_html__( 'When a reader opens the article on another device, entering the email address used at checkout sends them a one-time access link. They can sort it out on their own, without any help from you.', 'acs-content-for-woocommerce' ) ); echo ''; printf( '', esc_html__( 'Access link validity', 'acs-content-for-woocommerce' ), esc_html( sprintf( /* translators: %d: number of hours */ __( '%d hours', 'acs-content-for-woocommerce' ), (int) round( $one_time / HOUR_IN_SECONDS ) ) ) ); printf( '', esc_html__( 'Access retention period', 'acs-content-for-woocommerce' ), esc_html( sprintf( /* translators: %d: number of days */ __( '%d days (signed cookie, per device)', 'acs-content-for-woocommerce' ), (int) round( $cookie / DAY_IN_SECONDS ) ) ) ); echo ''; printf( '

%s

', esc_html__( 'Version 2 used PHP sessions, so access was lost the moment the browser was closed.', 'acs-content-for-woocommerce' ) ); ACSCW_Admin::card_close(); } }