label. * * @return array */ public static function definitions() { $columns = array( 'subscription_id' => 'subscription_id', 'status' => 'status', 'customer_id' => 'customer_id', 'customer_email' => 'customer_email', 'customer_name' => 'customer_name', 'currency' => 'currency', 'recurring_total' => 'recurring_total', 'billing_period' => 'billing_period', 'billing_interval' => 'billing_interval', 'renewal_type' => 'renewal_type', 'payment_method' => 'payment_method', 'payment_method_title' => 'payment_method_title', 'start_date' => 'start_date', 'next_payment_date' => 'next_payment_date', 'last_order_date' => 'last_order_date', 'trial_end_date' => 'trial_end_date', 'end_date' => 'end_date', 'parent_order_id' => 'parent_order_id', 'line_items' => 'line_items', 'product_ids' => 'product_ids', 'variation_ids' => 'variation_ids', 'tax_classes' => 'tax_classes', 'item_count' => 'item_count', 'admin_url' => 'admin_url', ); /** * Filters the export columns (key => header). * * @param array $columns Columns. */ return (array) apply_filters( 'recast_export_columns', $columns ); } /** * Header row. * * @return string[] */ public static function header() { return array_values( self::definitions() ); } /** * Builds one CSV row. * * @param \WC_Subscription $subscription Subscription. * @return string[] */ public static function row( \WC_Subscription $subscription ) { $row = RowFormatter::format( $subscription ); $values = array(); foreach ( array_keys( self::definitions() ) as $key ) { $values[] = self::guard( self::value( $key, $row, $subscription ) ); } return $values; } /** * Value for one column. * * @param string $key Column key. * @param array $row Formatted row. * @param \WC_Subscription $subscription Subscription. * @return string */ private static function value( $key, array $row, \WC_Subscription $subscription ) { switch ( $key ) { case 'subscription_id': return (string) $row['id']; case 'status': return $row['status']; case 'customer_id': return (string) $row['customer']['id']; case 'customer_email': return $row['customer']['email']; case 'customer_name': return $row['customer']['name']; case 'currency': return $row['currency']; case 'recurring_total': return $row['recurring_total']; case 'billing_period': return $row['billing']['period']; case 'billing_interval': return (string) $row['billing']['interval']; case 'renewal_type': return $row['manual'] ? 'manual' : 'auto'; case 'payment_method': return $row['gateway']['id']; case 'payment_method_title': return '' === $row['gateway']['id'] ? '' : $row['gateway']['title']; case 'start_date': return $row['start_date']; case 'next_payment_date': return $row['next_payment']; case 'last_order_date': return $row['last_order_date']; case 'trial_end_date': return $row['trial_end']; case 'end_date': return $row['end_date']; case 'parent_order_id': return (string) $subscription->get_parent_id(); case 'line_items': return implode( ' | ', array_map( static function ( $item ) { return $item['name'] . ' x ' . $item['qty']; }, $row['items'] ) ); case 'product_ids': return implode( ',', array_unique( wp_list_pluck( $row['items'], 'product_id' ) ) ); case 'variation_ids': return implode( ',', array_filter( array_unique( wp_list_pluck( $row['items'], 'variation_id' ) ) ) ); case 'tax_classes': return implode( ',', array_unique( array_map( static function ( $item ) { return '' === $item['tax_class'] ? 'standard' : $item['tax_class']; }, $row['items'] ) ) ); case 'item_count': return (string) count( $row['items'] ); case 'admin_url': return $row['edit_url']; } /** * Filters the value of a custom export column. * * @param string $value Value. * @param string $key Column key. * @param \WC_Subscription $subscription Subscription. */ return (string) apply_filters( 'recast_export_column_value', '', $key, $subscription ); } /** * Neutralises spreadsheet formula injection. * * A cell that starts with a formula character, a tab or a line break gets a leading quote. The * full-width forms count too, because some spreadsheet locales convert them to the ASCII ones. * * @param string $value Cell. * @return string */ public static function guard( $value ) { $value = (string) $value; if ( '' !== $value && preg_match( '/^(?:[=+\-@\t\r\n]|\x{FF1D}|\x{FF0B}|\x{FF0D}|\x{FF20})/u', $value ) ) { return "'" . $value; } return $value; } }