ai = $ai; } /** * {@inheritDoc} */ public function get_id() { return 'description_ai'; } /** * {@inheritDoc} */ public function get_label() { return __( 'Rewrite description (AI)', 'agentictrack-agent-readiness-for-woocommerce' ); } /** * {@inheritDoc} */ public function get_description() { return __( 'Rewrites thin or marketing-heavy descriptions into factual, spec-rich copy that agents can read. You approve each rewrite before it is saved.', 'agentictrack-agent-readiness-for-woocommerce' ); } /** * {@inheritDoc} * * Preview does NOT call the AI (that would spend budget on a non-approved * action). It only reports whether a rewrite is worthwhile. */ public function preview( WC_Product $product ) { $text = trim( wp_strip_all_tags( (string) $product->get_description() ) ); $words = ( '' === $text ) ? 0 : count( preg_split( '/\s+/', $text, -1, PREG_SPLIT_NO_EMPTY ) ); // Already substantial — nothing to do. if ( $words >= 50 ) { return array(); } // Too little to work with: the AI won't invent specs, so a "rewrite" here // would just pad a placeholder. The free "Description" fix offers an in-app // manual editor for this case, so we show nothing here. if ( $words < self::MIN_WORDS ) { return array(); } // Thin but workable (MIN_WORDS–49 words): offer an AI rewrite the merchant // generates and reviews before it is saved (generate => two-phase flow). return array( array( 'field' => 'description', 'label' => __( 'Long description', 'agentictrack-agent-readiness-for-woocommerce' ), 'before' => wp_trim_words( $text, 30, '…' ), 'after' => '', 'needs_input' => false, 'auto' => false, 'generate' => true, 'note' => __( 'Generate a factual AI rewrite to review — and edit — before it is saved.', 'agentictrack-agent-readiness-for-woocommerce' ), ), ); } /** * Generate a proposed rewrite for review, WITHOUT writing anything. * * @param WC_Product $product Product. * @return array|WP_Error Review payload, or WP_Error. */ public function generate( WC_Product $product ) { if ( ! $this->ai instanceof ARWC_OpenRouter ) { return new WP_Error( 'arwc_ai_unavailable', __( 'AI fixes are unavailable.', 'agentictrack-agent-readiness-for-woocommerce' ) ); } $before = trim( wp_strip_all_tags( (string) $product->get_description() ) ); $before_words = ( '' === $before ) ? 0 : count( preg_split( '/\s+/', $before, -1, PREG_SPLIT_NO_EMPTY ) ); if ( $before_words < self::MIN_WORDS ) { return new WP_Error( 'arwc_desc_too_thin', __( 'This product has almost no description to rewrite. Use the free Description fix to add a few real details first.', 'agentictrack-agent-readiness-for-woocommerce' ) ); } $generated = $this->ai->complete( $this->system_prompt(), $this->user_prompt( $product, $before ) ); if ( is_wp_error( $generated ) ) { return $generated; } $generated = sanitize_textarea_field( $generated ); if ( '' === $generated ) { return new WP_Error( 'arwc_ai_empty', __( 'AI returned no usable text.', 'agentictrack-agent-readiness-for-woocommerce' ) ); } return array( 'field' => 'description', 'label' => __( 'Long description', 'agentictrack-agent-readiness-for-woocommerce' ), 'before' => wp_trim_words( $before, 30, '…' ), 'after' => $generated, 'editable' => true, 'apply_field' => 'description', 'note' => __( 'Review and edit if needed, then apply to save.', 'agentictrack-agent-readiness-for-woocommerce' ), ); } /** * {@inheritDoc} */ public function apply( WC_Product $product, array $input ) { $before = trim( wp_strip_all_tags( (string) $product->get_description() ) ); // Preferred path: the merchant reviewed a generated rewrite and approved it // (possibly after editing). Write exactly that — no second AI call. if ( isset( $input['description'] ) && '' !== trim( (string) $input['description'] ) ) { $reviewed = sanitize_textarea_field( wp_unslash( $input['description'] ) ); $result = $this->result(); $product->set_description( $reviewed ); $product->save(); $result->add_change( 'description', __( 'Long description', 'agentictrack-agent-readiness-for-woocommerce' ), '' === $before ? __( 'Empty', 'agentictrack-agent-readiness-for-woocommerce' ) : wp_trim_words( $before, 20, '…' ), wp_trim_words( $reviewed, 20, '…' ) ); return $result; } // Fallback path (e.g. a direct apply without a prior generate): generate and // write in one step, with the same "must have real content" guard. if ( ! $this->ai instanceof ARWC_OpenRouter ) { return new WP_Error( 'arwc_ai_unavailable', __( 'AI fixes are unavailable.', 'agentictrack-agent-readiness-for-woocommerce' ) ); } $before_words = ( '' === $before ) ? 0 : count( preg_split( '/\s+/', $before, -1, PREG_SPLIT_NO_EMPTY ) ); if ( $before_words < self::MIN_WORDS ) { return new WP_Error( 'arwc_desc_too_thin', __( 'This product has almost no description to rewrite. Add a few real details first — the AI won\'t invent specifications.', 'agentictrack-agent-readiness-for-woocommerce' ) ); } $generated = $this->ai->complete( $this->system_prompt(), $this->user_prompt( $product, $before ) ); if ( is_wp_error( $generated ) ) { return $generated; } $generated = sanitize_textarea_field( $generated ); if ( '' === $generated ) { return new WP_Error( 'arwc_ai_empty', __( 'AI returned no usable text.', 'agentictrack-agent-readiness-for-woocommerce' ) ); } $result = $this->result(); $product->set_description( $generated ); $product->save(); $result->add_change( 'description', __( 'Long description', 'agentictrack-agent-readiness-for-woocommerce' ), $before, $generated ); return $result; } /** * System prompt for the rewrite. * * @return string */ protected function system_prompt() { return 'You are a product-data editor for an e-commerce catalog. Rewrite the product description so AI shopping agents can extract facts. Output plain text only: concise, factual, specification-led sentences. Do NOT invent specifications, measurements, materials, or claims that are not given. Avoid marketing superlatives. Keep it under 120 words.'; } /** * User prompt for the rewrite. * * @param WC_Product $product Product. * @param string $before Current plain-text description. * @return string */ protected function user_prompt( WC_Product $product, $before ) { return sprintf( "Product title: %s\n\nCurrent description (may be empty or thin):\n%s", $product->get_name(), '' === $before ? '(none)' : $before ); } }