$empty_only && self::isTextOccupied( (string) $product->get_name() ), 'description' => $empty_only && self::isHtmlOccupied( (string) $product->get_description() ), 'short_desc' => $empty_only && self::isHtmlOccupied( (string) $product->get_short_description() ), 'tags' => $empty_only && self::hasTags( $product_id ), 'faq' => $empty_only && self::hasFaq( $product_id ), ); } /** * Whether an image's alt text is left as it is: a text equal to the stored * one is a no-op in both modes, and under "only fill empty" any stored * text is kept. * * @param string $current_alt The stored alt text. * @param string $alt_text The generated alt text, sanitized. * @param string $overwrite 'replace' | 'empty_only'. * @return bool */ public static function keepsImageAlt( string $current_alt, string $alt_text, string $overwrite ): bool { return $current_alt === $alt_text || ( 'empty_only' === $overwrite && '' !== trim( $current_alt ) ); } /** * "empty_only" occupancy test for a plain-text field (title): any * non-whitespace content counts as already filled. * * @param string $current Live current value. * @return bool */ public static function isTextOccupied( string $current ): bool { return '' !== trim( $current ); } /** * "empty_only" occupancy test for an HTML-bearing field (description, * short description): the STRIPPED text must have non-whitespace * content, so empty markup (e.g. "
") still counts as empty, * mirroring DescriptionApplyAdapter's identical empty_only check on the * content template engine's own apply path. * * @param string $current Live current value. * @return bool */ public static function isHtmlOccupied( string $current ): bool { return '' !== trim( wp_strip_all_tags( $current ) ); } /** * "empty_only" occupancy test for tags: the whole field is kept once the * product has at least one product_tag term, never a partial merge of * new tags alongside existing ones, mirroring * DescriptionApplyAdapter::planTags()'s identical whole-field check. * * @param int $product_id Product id. * @return bool */ public static function hasTags( int $product_id ): bool { $existing = function_exists( 'wp_get_post_terms' ) ? wp_get_post_terms( $product_id, 'product_tag', array( 'fields' => 'ids' ) ) : array(); return is_array( $existing ) && ! empty( $existing ); } /** * "empty_only" occupancy test for FAQ: whether the product already has * at least one stored FAQ entry, mirroring FaqApplyAdapter's identical * check on the content template engine's own apply path. * * @param int $product_id Product id. * @return bool */ public static function hasFaq( int $product_id ): bool { $raw = function_exists( 'get_post_meta' ) ? get_post_meta( $product_id, '_aipt_faq_data', true ) : ''; if ( ! is_string( $raw ) || '' === $raw ) { return false; } $decoded = json_decode( $raw, true ); return is_array( $decoded ) && ! empty( $decoded ); } /** * Current SEO meta for the empty_only per-field check, or an empty array * when the mode is 'replace' (never needs it) or no SEO plugin is active * (nothing to compare against; the SEO write itself already no-ops with * no plugin detected). * * @param int $product_id Product id. * @param string $overwrite 'replace' | 'empty_only'. * @return array */ public static function currentSeoInfo( int $product_id, string $overwrite ): array { if ( 'empty_only' !== $overwrite ) { return array(); } $detected_plugin = SeoPluginDetector::detect(); if ( null === $detected_plugin || empty( $detected_plugin['plugin_slug'] ) ) { return array(); } return SeoPluginDetector::get_seo_info( $product_id, $detected_plugin, false ); } /** * Drop any SEO field the store already has, for an empty_only job, per * field and not the whole result, mirroring SeoMetaApplyAdapter's * identical per-field check on the content template engine's own apply * path. A plain decision function (no I/O) for testability: the caller * resolves $current_seo_info once via currentSeoInfo(). * * @param array $seo_meta Generated SEO fields (meta_title / meta_description / focus_keyword). * @param array $current_seo_info SeoPluginDetector::get_seo_info() shape, or []. * @param string $overwrite 'replace' | 'empty_only'. * @return array Same shape as $seo_meta, with already-filled fields removed under empty_only. */ public static function filterSeoMeta( array $seo_meta, array $current_seo_info, string $overwrite ): array { if ( 'empty_only' !== $overwrite ) { return $seo_meta; } foreach ( array( 'meta_title', 'meta_description', 'focus_keyword' ) as $seo_meta_key ) { if ( isset( $seo_meta[ $seo_meta_key ] ) && self::keepsSeoKey( $seo_meta_key, $current_seo_info, $overwrite ) ) { unset( $seo_meta[ $seo_meta_key ] ); } } return $seo_meta; } /** * Whether "only fill empty" keeps one SEO field: the store already has a * value for it. The pre-call check asks it per field, with no generated * value to compare. * * @param string $seo_meta_key meta_title, meta_description or focus_keyword. * @param array $current_seo_info SeoPluginDetector::get_seo_info() shape, or []. * @param string $overwrite 'replace' | 'empty_only'. * @return bool */ public static function keepsSeoKey( string $seo_meta_key, array $current_seo_info, string $overwrite ): bool { if ( 'empty_only' !== $overwrite ) { return false; } return '' !== trim( SeoFieldValueNormalizer::flatten( $current_seo_info[ $seo_meta_key ] ?? '' ) ); } /** * Whether "only fill empty" keeps an image's stored alt text, whatever the * generated one will be. The pre-call check cannot use keepsImageAlt(): * its "equal to the stored text" rule needs the generated value. * * @param string $current_alt The stored alt text. * @param string $overwrite 'replace' | 'empty_only'. * @return bool */ public static function keepsStoredImageAlt( string $current_alt, string $overwrite ): bool { return 'empty_only' === $overwrite && '' !== trim( $current_alt ); } }