'categories', ); private $registry; private $wc_get_product; private $get_permalink; private $get_edit_post_link; private $wp_get_attachment_image_url; private $get_post_meta; public function __construct( FieldRegistry $registry, array $callables = array() ) { $this->registry = $registry; $this->wc_get_product = isset( $callables['wc_get_product'] ) ? $callables['wc_get_product'] : null; $this->get_permalink = isset( $callables['get_permalink'] ) ? $callables['get_permalink'] : null; $this->get_edit_post_link = isset( $callables['get_edit_post_link'] ) ? $callables['get_edit_post_link'] : null; $this->wp_get_attachment_image_url = isset( $callables['wp_get_attachment_image_url'] ) ? $callables['wp_get_attachment_image_url'] : null; $this->get_post_meta = isset( $callables['get_post_meta'] ) ? $callables['get_post_meta'] : null; } public function assemble_rows( array $product_ids, array $field_ids ): array { $products = array(); $attachment_ids = array(); foreach ( $product_ids as $product_id ) { $product_id = (int) $product_id; if ( $product_id <= 0 ) { continue; } $product = $this->call_wc_get_product( $product_id ); if ( ! $product instanceof \WC_Product ) { continue; } $products[] = $product; $attachment_ids = array_merge( $attachment_ids, $this->product_attachment_ids( $product ) ); } if ( ! empty( $attachment_ids ) && function_exists( 'update_meta_cache' ) ) { update_meta_cache( 'post', array_values( array_unique( $attachment_ids ) ) ); } $rows = array(); foreach ( $products as $product ) { $rows[] = $this->assemble_row( $product, $field_ids ); } return $rows; } public function assemble_response( array $builder_result, array $field_ids ): array { $product_ids = array(); foreach ( isset( $builder_result['products'] ) && is_array( $builder_result['products'] ) ? $builder_result['products'] : array() as $product ) { if ( is_array( $product ) && isset( $product['id'] ) ) { $product_ids[] = (int) $product['id']; continue; } if ( $product instanceof \WC_Product ) { $product_ids[] = (int) $product->get_id(); continue; } if ( is_object( $product ) && isset( $product->ID ) ) { $product_ids[] = (int) $product->ID; continue; } if ( ! is_scalar( $product ) ) { continue; } $product_ids[] = (int) $product; } return array( 'rows' => $this->assemble_rows( $product_ids, $field_ids ), 'rowCount' => isset( $builder_result['total'] ) ? (int) $builder_result['total'] : 0, 'pageCount' => isset( $builder_result['total_pages'] ) ? (int) $builder_result['total_pages'] : 0, ); } private function assemble_row( \WC_Product $product, array $field_ids ): array { $product_id = (int) $product->get_id(); $image_id = (int) $product->get_image_id(); $row = array( 'id' => $product_id, 'token' => ProductEditorConcurrencyToken::for_product( $product ), 'name' => $product->get_name(), 'status' => $product->get_status(), 'sku' => $product->get_sku(), 'productType' => $product->get_type(), 'permalink' => $this->call_get_permalink( $product_id ), 'adminEditUrl' => $this->call_get_edit_post_link( $product_id ), 'thumbnailUrl' => $image_id > 0 ? $this->call_wp_get_attachment_image_url( $image_id, 'thumbnail' ) : null, ); $registry_field_ids = $this->registry_field_ids( $field_ids ); if ( ! empty( $registry_field_ids ) ) { foreach ( $this->registry->read_fields( $product, $registry_field_ids ) as $field_id => $value ) { $row[ $this->row_key_for_field( (string) $field_id ) ] = $value; } } $derived_to_emit = array( 'validationIssues' => true ); foreach ( $field_ids as $field_id ) { if ( is_string( $field_id ) && in_array( $field_id, self::DERIVED_FIELDS, true ) ) { $derived_to_emit[ $field_id ] = true; } } if ( $this->requests_seo_field( $field_ids ) ) { $derived_to_emit['seoPlugin'] = true; } if ( $this->requests_media_field( $field_ids ) ) { $derived_to_emit['featuredImage'] = true; $derived_to_emit['gallery'] = true; } if ( in_array( 'aiStatus', $field_ids, true ) ) { $derived_to_emit['aiTypes'] = true; } foreach ( array_keys( $derived_to_emit ) as $field_id ) { $row[ $field_id ] = $this->read_derived_field( $product, $field_id ); } return $row; } private function requests_media_field( array $field_ids ): bool { $media = array( 'altText', 'featuredImage', 'gallerySummary', 'missingAltCount' ); foreach ( $field_ids as $field_id ) { if ( is_string( $field_id ) && in_array( $field_id, $media, true ) ) { return true; } } return false; } private function requests_seo_field( array $field_ids ): bool { $seo = array( 'seoTitle', 'metaDescription', 'focusKeyword' ); foreach ( $field_ids as $field_id ) { if ( is_string( $field_id ) && in_array( $field_id, $seo, true ) ) { return true; } } return false; } private function registry_field_ids( array $field_ids ): array { $ids = array(); foreach ( $field_ids as $field_id ) { if ( is_string( $field_id ) && ! in_array( $field_id, self::DERIVED_FIELDS, true ) ) { $ids[] = $field_id; } } return array_values( array_unique( $ids ) ); } private function row_key_for_field( string $field_id ): string { return isset( self::FIELD_KEY_MAP[ $field_id ] ) ? self::FIELD_KEY_MAP[ $field_id ] : $field_id; } private function read_derived_field( \WC_Product $product, string $field_id ) { switch ( $field_id ) { case 'featuredImage': return $this->read_featured_image( $product ); case 'gallery': return $this->read_gallery( $product ); case 'gallerySummary': return $this->read_gallery_summary( $product ); case 'missingAltCount': return $this->read_missing_alt_count( $product ); case 'aiStatus': $rollup = $this->call_get_post_meta( (int) $product->get_id(), MetaKeys::ROLLUP, true ); return '' !== $rollup ? 'generated' : 'notGenerated'; case 'aiTypes': return \AIPT\Core\ContentMarking\AiMarkReader::markedTypes( (int) $product->get_id(), $this->get_post_meta ); case 'lastGenerated': $rollup = $this->call_get_post_meta( (int) $product->get_id(), MetaKeys::ROLLUP, true ); return '' !== $rollup ? gmdate( 'Y-m-d\TH:i:s\Z', (int) $rollup ) : null; case 'reviewStatus': return 'none'; case 'validationIssues': return array(); case 'seoPlugin': $adapter = $this->registry->adapter_for( 'seoTitle' ); return is_object( $adapter ) && method_exists( $adapter, 'get_detected_plugin' ) ? $adapter->get_detected_plugin() : null; } return null; } private function read_featured_image( \WC_Product $product ) { $attachment_id = (int) $product->get_image_id(); if ( $attachment_id <= 0 ) { return null; } return $this->attachment_entry( $attachment_id, true ); } private function read_gallery( \WC_Product $product ): array { $gallery = array(); foreach ( (array) $product->get_gallery_image_ids() as $attachment_id ) { $attachment_id = (int) $attachment_id; if ( $attachment_id > 0 ) { $gallery[] = $this->attachment_entry( $attachment_id, false ); } } return $gallery; } private function read_gallery_summary( \WC_Product $product ): array { $gallery_ids = array_values( array_filter( array_map( 'intval', (array) $product->get_gallery_image_ids() ) ) ); return array( 'count' => count( $gallery_ids ), 'firstUrl' => ! empty( $gallery_ids ) ? $this->call_wp_get_attachment_image_url( $gallery_ids[0], 'thumbnail' ) : null, ); } private function read_missing_alt_count( \WC_Product $product ): int { $count = 0; foreach ( $this->product_attachment_ids( $product ) as $attachment_id ) { $alt = $this->call_get_post_meta( $attachment_id, self::ATTACHMENT_ALT_META_KEY, true ); if ( '' === trim( (string) $alt ) ) { $count++; } } return $count; } private function attachment_entry( int $attachment_id, bool $is_featured ): array { $alt = (string) $this->call_get_post_meta( $attachment_id, self::ATTACHMENT_ALT_META_KEY, true ); return array( 'attachmentId' => $attachment_id, 'url' => $this->call_wp_get_attachment_image_url( $attachment_id, 'thumbnail' ), 'alt' => $alt, 'previousAlt' => $alt, 'isFeatured' => $is_featured, ); } private function product_attachment_ids( \WC_Product $product ): array { $ids = array(); $featured_id = (int) $product->get_image_id(); if ( $featured_id > 0 ) { $ids[] = $featured_id; } foreach ( (array) $product->get_gallery_image_ids() as $attachment_id ) { $attachment_id = (int) $attachment_id; if ( $attachment_id > 0 ) { $ids[] = $attachment_id; } } return array_values( array_unique( $ids ) ); } private function call_wc_get_product( int $product_id ) { if ( is_callable( $this->wc_get_product ) ) { return call_user_func( $this->wc_get_product, $product_id ); } return \wc_get_product( $product_id ); } private function call_get_permalink( int $product_id ) { if ( is_callable( $this->get_permalink ) ) { return call_user_func( $this->get_permalink, $product_id ); } return \get_permalink( $product_id ); } private function call_get_edit_post_link( int $product_id ) { if ( is_callable( $this->get_edit_post_link ) ) { return call_user_func( $this->get_edit_post_link, $product_id ); } return \get_edit_post_link( $product_id, 'raw' ); } private function call_wp_get_attachment_image_url( int $attachment_id, string $size ) { if ( is_callable( $this->wp_get_attachment_image_url ) ) { return call_user_func( $this->wp_get_attachment_image_url, $attachment_id, $size ); } return \wp_get_attachment_image_url( $attachment_id, $size ); } private function call_get_post_meta( int $post_id, string $key, bool $single ) { if ( is_callable( $this->get_post_meta ) ) { return call_user_func( $this->get_post_meta, $post_id, $key, $single ); } return \get_post_meta( $post_id, $key, $single ); } }