set_description($result['description']); $product->save(); return $result; } /** * @param array $input { product_id: int, tone: string } * @return string|WP_Error */ public function execute_generate_short_description( array $input ) { $result = AI_Content_Generator_For_WooCommerce_Helper::api_generate_short_description( absint( $input['product_id'] ), isset( $input['tone'] ) ? sanitize_text_field( $input['tone'] ) : 'formal' ); $product = wc_get_product( absint( $input['product_id'] ) ); $product->set_short_description($result); $product->save(); return $result; } /** * @param array $input { product_id: int } * @return array|WP_Error */ public function execute_generate_image( array $input ) { $product_id = absint( $input['product_id'] ); $product = wc_get_product( $product_id ); if ( ! $product ) { return new WP_Error( 'invalid_product', 'Invalid product ID.' ); } $result = AI_Content_Generator_For_WooCommerce_Helper::api_generate_images( $product_id ); if ( is_wp_error( $result ) ) { return $result; } $image_urls = $result['image_urls'] ?? array(); if ( empty( $image_urls ) ) { return new WP_Error( 'no_images', 'No images were generated.' ); } $has_featured = (bool) $product->get_image_id(); foreach ( $image_urls as $url ) { // First image becomes featured only when no featured image exists yet. // Everything else — including all images when a featured already exists — goes to gallery. $attachment_id = AI_Content_Generator_For_WooCommerce_Helper::attach_image_to_product( $product_id, $url, $has_featured ); if ( ! is_wp_error( $attachment_id ) ) { $has_featured = true; } } return $result; } /** * @param array $input { product_id: int } * @return array|WP_Error */ public function execute_generate_brands( array $input ) { $post_id = absint( $input['product_id'] ); $result = AI_Content_Generator_For_WooCommerce_Helper::api_generate_brands( $post_id ); if ( is_wp_error( $result ) ) { return $result; } $brand_names = $result['brands'] ?? array(); $brand_ids = array(); foreach ( $brand_names as $name ) { $term = get_term_by( 'name', $name, 'product_brand' ); if ( $term ) { $brand_ids[] = $term->term_id; } else { $inserted = wp_insert_term( $name, 'product_brand' ); if ( ! is_wp_error( $inserted ) ) { $brand_ids[] = $inserted['term_id']; } } } if ( ! empty( $brand_ids ) ) { $existing = wp_get_object_terms( $post_id, 'product_brand', array( 'fields' => 'ids' ) ); $all_ids = array_unique( array_merge( is_wp_error( $existing ) ? array() : $existing, $brand_ids ) ); wp_set_object_terms( $post_id, $all_ids, 'product_brand' ); } return $result; } /** * @param array $input { product_id: int } * @return array|WP_Error */ public function execute_generate_tags( array $input ) { $post_id = absint( $input['product_id'] ); $result = AI_Content_Generator_For_WooCommerce_Helper::api_generate_tags( $post_id ); if ( is_wp_error( $result ) ) { return $result; } $tag_names = $result['tags'] ?? array(); $tag_ids = array(); foreach ( $tag_names as $name ) { $term = get_term_by( 'name', $name, 'product_tag' ); if ( $term ) { $tag_ids[] = $term->term_id; } else { $inserted = wp_insert_term( $name, 'product_tag' ); if ( ! is_wp_error( $inserted ) ) { $tag_ids[] = $inserted['term_id']; } } } if ( ! empty( $tag_ids ) ) { $existing = wp_get_object_terms( $post_id, 'product_tag', array( 'fields' => 'ids' ) ); $all_ids = array_unique( array_merge( is_wp_error( $existing ) ? array() : $existing, $tag_ids ) ); wp_set_object_terms( $post_id, $all_ids, 'product_tag' ); } return $result; } /** * @param array $input { product_id: int, marked: bool } * @return bool|WP_Error */ public function execute_mark_ai_generated( array $input ) { $post_id = absint( $input['product_id'] ); $marked = isset( $input['marked'] ) ? (bool) $input['marked'] : true; if ( ! $post_id || ! wc_get_product( $post_id ) ) { return new WP_Error( 'invalid_product', __( 'Invalid product ID.', 'ai-content-generator-for-woocommerce' ) ); } if ( $marked ) { update_post_meta( $post_id, '_wtofe_ai_generated', '1' ); } else { delete_post_meta( $post_id, '_wtofe_ai_generated' ); } return true; } /** * Creates a product saved as draft. * * @param array $input Full product field map (see product_input_schema). * @return array */ public function execute_create_product_draft( array $input ) { return AI_Content_Generator_For_WooCommerce_Helper::create_product( $input, 'draft' ); } /** * Creates a product and immediately publishes it. * * @param array $input Full product field map (see product_input_schema). * @return array */ public function execute_create_product_published( array $input ) { return AI_Content_Generator_For_WooCommerce_Helper::create_product( $input, 'publish' ); } /** * Publishes an existing draft product. * * @param array $input { product_id: int } * @return array */ public function execute_publish_product_draft( array $input ) { return AI_Content_Generator_For_WooCommerce_Helper::publish_product( absint( $input['product_id'] ) ); } /** * Creates a WooCommerce coupon (MCP / Abilities API). * * @param array $input See register_create_shop_coupon input_schema. * @return array */ public function execute_create_shop_coupon( array $input ) { return AI_Content_Generator_For_WooCommerce_Helper::create_coupon( $input ); } /** * Executes searching for WooCommerce products by name to retrieve their IDs. * Uses WooCommerce's native wc_get_products() for accurate querying. * * @param array $input Contains search_term. * @return array|WP_Error Array of product details or WP_Error. */ public function execute_search_product_id( array $input ) { return AI_Content_Generator_For_WooCommerce_Helper::search_product_id( $input ); } /** * @param array $input { product_id: int, ...fields } * @return array */ public function execute_update_product( array $input ) { $product_id = absint( $input['product_id'] ); unset( $input['product_id'] ); return AI_Content_Generator_For_WooCommerce_Helper::update_product( $product_id, $input ); } /** * @param array $input { product_id: int, force_delete: bool } * @return array */ public function execute_delete_product( array $input ) { $force = isset( $input['force_delete'] ) && (bool) $input['force_delete']; return AI_Content_Generator_For_WooCommerce_Helper::delete_product( absint( $input['product_id'] ), $force ); } /** * @param array $input { order_id: int, new_status: string, note: string } * @return array */ public function execute_update_order_status( array $input ) { return AI_Content_Generator_For_WooCommerce_Helper::update_order_status( absint( $input['order_id'] ), sanitize_text_field( $input['new_status'] ), isset( $input['note'] ) ? sanitize_textarea_field( $input['note'] ) : '' ); } // ------------------------------------------------------------------------- // Permission callback (shared) // ------------------------------------------------------------------------- /** * Only users who can manage WooCommerce may execute these abilities. * * @return bool */ public function permission_manage_woocommerce() { return current_user_can( 'manage_woocommerce' ); } }