__( 'Paid content purchase records', 'acs-content-for-woocommerce' ), 'callback' => array( $this, 'export' ), ); return $exporters; } /** * 消去ハンドラを登録する。 * * @param array $erasers 既存の消去ハンドラ。 * @return array */ public function register_eraser( $erasers ) { $erasers['acs-content-for-woocommerce'] = array( 'eraser_friendly_name' => __( 'Paid content purchase records', 'acs-content-for-woocommerce' ), 'callback' => array( $this, 'erase' ), ); return $erasers; } /* * ----------------------------------------------------------------------- * エクスポート * ----------------------------------------------------------------------- */ /** * 個人データをエクスポートする。 * * @param string $email_address 対象のメールアドレス。 * @param int $page 1始まりのページ番号。 * @return array{data:array,done:bool} */ public function export( $email_address, $page = 1 ) { $page = max( 1, (int) $page ); $access = ACSCW_Plugin::instance()->get( 'access' ); if ( ! $access instanceof ACSCW_Access ) { return array( 'data' => array(), 'done' => true, ); } $records = $access->get_records( array( 'email' => $email_address, 'number' => self::BATCH_SIZE, 'offset' => ( $page - 1 ) * self::BATCH_SIZE, 'orderby' => 'id', 'order' => 'ASC', ) ); $data = array(); foreach ( $records as $record ) { $data[] = array( 'group_id' => self::GROUP_ID, 'group_label' => __( 'Paid content purchase records', 'acs-content-for-woocommerce' ), 'item_id' => 'acscw-access-' . absint( $record['id'] ), 'data' => $this->build_export_items( $record ), ); } return array( 'data' => $data, 'done' => count( $records ) < self::BATCH_SIZE, ); } /** * 1行分のエクスポート項目を組み立てる。 * * @param array $record acscw_access の1行。 * @return array[] */ private function build_export_items( $record ) { $post_id = absint( $record['post_id'] ); $title = ( $post_id > 0 ) ? get_the_title( $post_id ) : __( 'Site-wide', 'acs-content-for-woocommerce' ); if ( '' === (string) $title ) { $title = __( '(deleted content)', 'acs-content-for-woocommerce' ); } $items = array( array( 'name' => __( 'Content', 'acs-content-for-woocommerce' ), 'value' => $title, ), array( 'name' => __( 'Email address', 'acs-content-for-woocommerce' ), 'value' => (string) $record['email'], ), array( 'name' => __( 'Purchased', 'acs-content-for-woocommerce' ), 'value' => (string) $record['granted_at'], ), array( 'name' => __( 'Amount paid', 'acs-content-for-woocommerce' ), 'value' => $this->format_amount( $record['amount'], $record['currency'] ), ), array( 'name' => __( 'Source', 'acs-content-for-woocommerce' ), 'value' => $this->label_source( $record['source'] ), ), array( 'name' => __( 'Status', 'acs-content-for-woocommerce' ), 'value' => ( ACSCW_Access::STATUS_REVOKED === $record['status'] ) ? __( 'Revoked', 'acs-content-for-woocommerce' ) : __( 'Active', 'acs-content-for-woocommerce' ), ), ); if ( absint( $record['order_id'] ) > 0 ) { $items[] = array( 'name' => __( 'Order number', 'acs-content-for-woocommerce' ), 'value' => absint( $record['order_id'] ), ); } if ( ! empty( $record['expires_at'] ) ) { $items[] = array( 'name' => __( 'Expires', 'acs-content-for-woocommerce' ), 'value' => (string) $record['expires_at'], ); } return $items; } /* * ----------------------------------------------------------------------- * 消去 * ----------------------------------------------------------------------- */ /** * 個人データを消去する。 * * メールアドレスと管理者メモを空にし、行は売上記録として残す。 * * @param string $email_address 対象のメールアドレス。 * @param int $page 1始まりのページ番号。 * @return array{items_removed:bool,items_retained:bool,messages:string[],done:bool} */ public function erase( $email_address, $page = 1 ) { global $wpdb; unset( $page ); $response = array( 'items_removed' => false, 'items_retained' => false, 'messages' => array(), 'done' => true, ); $email = ACSCW_Access::normalize_email( $email_address ); if ( '' === $email ) { return $response; } $table = ACSCW_Install::get_access_table(); // phpcs:disable PluginCheck.Security.DirectDB.UnescapedDBParameter, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table name is a fixed value from ACSCW_Install, not user input; every value is bound through $wpdb->prepare(). Direct query is required and results are intentionally uncached. $ids = $wpdb->get_col( $wpdb->prepare( "SELECT id FROM `{$table}` WHERE email = %s ORDER BY id ASC LIMIT %d", $email, self::BATCH_SIZE ) ); // phpcs:enable PluginCheck.Security.DirectDB.UnescapedDBParameter, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared $ids = array_map( 'absint', (array) $ids ); if ( empty( $ids ) ) { return $response; } $placeholders = implode( ',', array_fill( 0, count( $ids ), '%d' ) ); // phpcs:disable PluginCheck.Security.DirectDB.UnescapedDBParameter, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare -- Table name is a fixed value from ACSCW_Install, not user input; every value is bound through $wpdb->prepare(). Direct query is required and results are intentionally uncached. $updated = $wpdb->query( $wpdb->prepare( "UPDATE `{$table}` SET email = '', note = '' WHERE id IN ({$placeholders})", $ids ) ); // phpcs:enable PluginCheck.Security.DirectDB.UnescapedDBParameter, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare if ( $updated > 0 ) { $response['items_removed'] = true; $response['items_retained'] = true; $response['messages'][] = __( 'The buyer email address has been removed. The amount and the date are kept as a sales record that does not identify the buyer.', 'acs-content-for-woocommerce' ); } // 取得件数が上限に達していたら、まだ残りがある可能性がある。 $response['done'] = ( count( $ids ) < self::BATCH_SIZE ); return $response; } /* * ----------------------------------------------------------------------- * 表示補助 * ----------------------------------------------------------------------- */ /** * 金額を通貨つきの文字列にする。 * * @param string|float $amount 金額。 * @param string $currency 通貨コード。 * @return string */ private function format_amount( $amount, $currency ) { $amount = (float) $amount; $currency = (string) $currency; if ( function_exists( 'wc_price' ) ) { return wp_strip_all_tags( wc_price( $amount, array( 'currency' => $currency ) ) ); } return trim( $currency . ' ' . number_format_i18n( $amount, 2 ) ); } /** * 付与元の表示名。 * * @param string $source 付与元。 * @return string */ private function label_source( $source ) { $labels = array( ACSCW_Access::SOURCE_PURCHASE => __( 'Purchase', 'acs-content-for-woocommerce' ), ACSCW_Access::SOURCE_MANUAL => __( 'Granted by an administrator', 'acs-content-for-woocommerce' ), ACSCW_Access::SOURCE_IMPORT => __( 'Migration from the previous version', 'acs-content-for-woocommerce' ), ACSCW_Access::SOURCE_REFUND_REVOKED => __( 'Revoked after a refund', 'acs-content-for-woocommerce' ), ACSCW_Access::SOURCE_MANUAL_REVOKED => __( 'Revoked by an administrator', 'acs-content-for-woocommerce' ), ); return isset( $labels[ $source ] ) ? $labels[ $source ] : (string) $source; } }