'order', 'plural' => 'orders', 'ajax' => true, ) ); } /** * Get table column definitions. * * @return array */ public function get_columns() { return array( 'cb' => '', 'id' => Messages::get( 'columns.id' ), 'batch_request_id' => Messages::get( 'columns.batch_request_id' ), 'status' => Messages::get( 'columns.status' ), 'type' => Messages::get( 'columns.type' ), 'total_expected' => Messages::get( 'columns.total_expected' ), 'batches_done' => Messages::get( 'columns.batches_done' ), 'created_at' => Messages::get( 'columns.created_at' ), 'updated_at' => Messages::get( 'columns.updated_at' ), ); } /** * Prepare table items and pagination. */ public function prepare_items() { // Define columns. $columns = $this->get_columns(); $hidden = array(); $sortable = $this->get_sortable_columns(); $this->_column_headers = array( $columns, $hidden, $sortable ); // Handle sorting. $orderby = $this->get_request_value( 'orderby' ) ?? 'created_at'; $order = $this->get_request_value( 'order' ) ?? 'desc'; $valid_columns = array_keys( $this->get_columns() ); if ( ! in_array( $orderby, $valid_columns, true ) ) { $orderby = 'created_at'; } if ( ! in_array( $order, array( 'asc', 'desc' ), true ) ) { $order = 'desc'; } // Get data. $per_page = $this->get_items_per_page( 'ai_content_by_parallax_batch_requests_per_page', $this->table_row_amount ); $current_page = $this->get_pagenum(); $offset = ( $current_page - 1 ) * $per_page; $args = array( 'page' => $current_page, 'per_page' => $per_page, 'orderby' => $orderby, 'order' => $order, ); // Filter at the query level so pagination, totals, and per-page counts // are type-aware instead of being stripped after the page slice. if ( '' !== $this->item_type ) { $args['types'] = array( $this->item_type ); } $pagination = BatchRequest::get_paginated( $args ); $this->items = $pagination['items']; $total_items = $pagination['total_items']; // Set pagination. $this->set_pagination_args( array( 'total_items' => $total_items, 'per_page' => $per_page, 'total_pages' => ceil( $total_items / $per_page ), ) ); } /** * Columns that support sorting. * * @return array */ public function get_sortable_columns() { return array( 'id' => array( 'id', false ), 'batch_request_id' => array( 'batch_request_id', false ), 'status' => array( 'status', false ), 'type' => array( 'type', false ), 'total_expected' => array( 'total_expected', false ), 'batches_done' => array( 'batches_done', false ), 'created_at' => array( 'created_at', true ), 'updated_at' => array( 'updated_at', false ), ); } /** * Default column output handler. * * @param array $item Row data. * @param string $column_name Column key. * @return string */ public function column_default( $item, $column_name ) { switch ( $column_name ) { case 'id': case 'batch_request_id': case 'status': case 'type': case 'total_expected': case 'batches_done': case 'created_at': case 'updated_at': return esc_html( $item[ $column_name ] ); default: return esc_html( wp_json_encode( $item ) ); } } /** * Checkbox column renderer. * * @param array $item Row data. * @return string */ public function column_cb( $item ) { return sprintf( '', esc_attr( $item['id'] ) ); } /** * Available bulk actions. * * @return array */ public function get_bulk_actions() { if ( ! $this->show_bulk_actions ) { return array(); } return array( 'approve' => Messages::get( 'columns.approve' ), 'delete' => Messages::get( 'columns.delete' ), ); } /** * Render ID column with action links. * * @param array $item Row data. * @return string */ public function column_id( $item ) { $actions = array( 'approve' => sprintf( '%s', esc_url( add_query_arg( array( 'action' => 'approve', 'order' => $item['id'], ) ) ), esc_html( Messages::get( 'columns.approve' ) ) ), ); return sprintf( '%1$s %2$s', esc_html( $item['id'] ), wp_kses_post( $this->row_actions( $actions ) ) ); } /** * Output extra controls above the table. * * @param string $which Top or bottom context. */ public function extra_tablenav( $which ) { if ( 'top' === $which ) { echo '
'; if ( $this->show_clear_button ) { $this->get_clear_batch_requests_button(); } echo '
'; $this->display_table_actions_right(); } } /** * Handle and render the clear batch requests button. */ private function get_clear_batch_requests_button() { // Handle clear batch requests action before rendering the table. // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only toggle for action link. $clear_request = isset( $_GET['clear_batch_requests'] ) ? sanitize_text_field( wp_unslash( $_GET['clear_batch_requests'] ) ) : ''; if ( '' !== $clear_request ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- nonce verified below. $nonce = isset( $_GET['_wpnonce'] ) ? sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ) : ''; if ( '' === $nonce || ! wp_verify_nonce( $nonce, 'ai_content_by_parallax_clear_batch_requests' ) ) { wp_die( esc_html( Messages::get( 'notices.security_failed' ) ) ); } BatchRequest::clear_all(); wp_safe_redirect( remove_query_arg( array( 'clear_batch_requests', '_wpnonce' ) ) ); exit; } $url = add_query_arg( 'clear_batch_requests', '1' ); $url = wp_nonce_url( $url, 'ai_content_by_parallax_clear_batch_requests' ); printf( '%s', esc_url( $url ), esc_html( Messages::get( 'columns.clear_batch' ) ) ); } /** * Build action link HTML for a batch request row. * * @param string|int $batch_request_id Batch request identifier. * @param string $type Request type. * @return string */ protected static function get_action_html( $batch_request_id, string $type = '' ): string { if ( empty( $batch_request_id ) ) { return esc_html( Messages::get( 'columns.no_batch_id' ) ); } // Normalize the type so JS can decide which modal to open. $type = '' !== $type ? (string) $type : 'product'; $common_attr = array( 'data-batch-request-id' => $batch_request_id, 'data-batch-type' => $type, ); $action_links = array( self::display_action_button_link( Messages::get( 'columns.view' ), '#open-batch-request', $common_attr, ), self::display_action_button_link( Messages::get( 'columns.import' ), '#import-all', $common_attr, 'button-primary button-small' ), self::display_action_button_link( Messages::get( 'columns.drop' ), '#delete-order', $common_attr, 'button-link-delete button-small button-danger' ), ); return implode( ' ', $action_links ); } /** * Default column output for approvals tables. * * @param array $item Row data. * @param string $column_name Column key. * @return string */ protected function column_default_approvals( $item, $column_name ) { $batch_request_id = $item['batch_request_id'] ?? null; if ( ! $batch_request_id ) { $batch_request_id = $item['order_id'] ?? null; } switch ( $column_name ) { case 'created_at': $created_at = $item['created_at'] ?? ''; if ( ! $created_at ) { return '—'; } // created_at is stored in the WP timezone (current_time('mysql')). // Convert to UTC so the browser can reformat in the visitor's local timezone. $dt = new \DateTimeImmutable( (string) $created_at, wp_timezone() ); $utc_iso = $dt->setTimezone( new \DateTimeZone( 'UTC' ) )->format( 'Y-m-d\TH:i:s\Z' ); $date_format = get_option( 'date_format', 'M j, Y' ); $time_format = get_option( 'time_format', 'g:i a' ); $date_label = date_i18n( $date_format, $dt->getTimestamp() ); $time_label = date_i18n( $time_format, $dt->getTimestamp() ); return sprintf( '', esc_attr( $utc_iso ), esc_html( $date_label ), esc_html( $time_label ) ); case 'status': // Handle empty batch request ID early. if ( empty( $batch_request_id ) ) { return sprintf( '%s', esc_attr( '' ), esc_html( 'Unknown' ) ); } // Read status from the cached DB row — no live API call on page render. $raw_status = (string) ( $item['status'] ?? '' ); $enum_case = BatchRequestStatus::from_api_status( $raw_status ); $status_label = $enum_case ? BatchRequestStatus::to_human_readable( $enum_case ) : esc_html( '' !== $raw_status ? $raw_status : 'Unknown' ); $refresh_link = sprintf( ' %s', esc_attr( (string) $batch_request_id ), esc_html( Messages::get( 'tables.refresh_status' ) ) ); return sprintf( '%s%s', esc_attr( $raw_status ), esc_html( $status_label ), wp_kses_post( $refresh_link ) ); case 'total_expected': return esc_html( (string) ( $batch_request_id ? MiddlewareRequest::get_total_count( $batch_request_id ) : 0 ) ); case 'batches_done': return esc_html( (string) ( $batch_request_id ? MiddlewareRequest::get_processed_count( $batch_request_id ) : 0 ) ); case 'failed': return esc_html( (string) ( $batch_request_id ? MiddlewareRequest::get_failed_count( $batch_request_id ) : 0 ) ); case 'remaining': return esc_html( (string) ( $batch_request_id ? MiddlewareRequest::get_remaining_count( $batch_request_id ) : 0 ) ); case 'view': $type = isset( $item['type'] ) ? (string) $item['type'] : ''; return wp_kses_post( self::get_action_html( $item['id'], $type ) ); default: return esc_html( wp_json_encode( $item ) ); } } /** * Format a status string/JSON into a readable tooltip. * * @param mixed $status Raw status (string or JSON). * @return string */ private function format_status_tooltip( $status ): string { if ( is_array( $status ) ) { return $this->flatten_tooltip_lines( $status ); } $status = (string) $status; // If it's JSON, decode and format nicely. $decoded = json_decode( $status, true ); if ( JSON_ERROR_NONE === json_last_error() && is_array( $decoded ) ) { return $this->flatten_tooltip_lines( $decoded ); } // Fallback: make a best-effort "readable" string. $status = preg_replace( '/\s+/', ' ', $status ) ?? $status; return trim( $status ); } /** * Turn an associative array into newline-separated "key: value" lines. * * @param array $data Tooltip data. * @return string */ private function flatten_tooltip_lines( array $data ): string { $lines = array(); foreach ( $data as $key => $value ) { if ( is_array( $value ) ) { $encoded = wp_json_encode( $value ); $value = ( false !== $encoded ) ? $encoded : '(unable to encode)'; } $lines[] = sprintf( '%s: %s', (string) $key, (string) $value ); } // Newlines are fine in title attributes; browser may render them as line breaks in tooltip. return implode( "\n", $lines ); } /** * Columns for the approvals view. * * @return array */ protected function get_columns_approvals() { return array( 'created_at' => Messages::get( 'columns.date' ), 'status' => Messages::get( 'columns.batch_status' ), 'total_expected' => Messages::format( 'columns.total_items', ucfirst( $this->item_type ) ), 'batches_done' => Messages::get( 'columns.processed' ), 'failed' => Messages::get( 'columns.failed' ), 'remaining' => Messages::get( 'columns.remaining' ), 'view' => '', ); } /** * CSS classes for the table element. * * @return array */ public function get_table_classes() { return array( 'widefat', 'fixed', 'striped', 'wc-parallax-batch-requests-table', 'wc-parallax-' . $this->item_type . '-batch-requests-table' ); } /** * Render the table output when batch requests exist. * * @param string|null $type Specific type to display. */ public static function output_table( ?string $type = null ): void { $table = new static(); $resolved_type = $type ?? $table->item_type; if ( '' === $resolved_type ) { return; } if ( ! BatchRequest::exists( $resolved_type ) ) { return; } $table->prepare_items(); if ( empty( $table->items ) ) { return; } echo '
'; $table->display_title(); $table->display(); echo '
'; } /** * Filter $this->_pagination_args['total_items'] so only relevant items are counted * when rendering pagination. * * @param string $which Top or bottom context. */ protected function pagination( $which = 'top' ) { if ( isset( $this->_pagination_args['total_items'] ) ) { $this->_pagination_args['total_items'] = count( $this->items ); } parent::pagination( $which ); } /** * Retrieve a request value from POST/GET or overrides. * * @param string $key Request key. * @return string|null */ private function get_request_value( string $key ): ?string { $allowlist = array( 'orderby' => 'key', 'order' => 'key', ); if ( ! isset( $allowlist[ $key ] ) ) { return null; } // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only table filter. if ( isset( $_GET[ $key ] ) && is_string( $_GET[ $key ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- read-only table filter; sanitized immediately below. $value = wp_unslash( $_GET[ $key ] ); if ( 'key' === $allowlist[ $key ] ) { return sanitize_key( $value ); } return sanitize_text_field( $value ); } $override = static::get_request_override( $key ); if ( is_string( $override ) ) { return $override; } return null; } }