' . esc_html__( 'Settings saved successfully.', 'ai-product-content-generator' ) . '

'; } $openai_key = get_option( 'aipc_openai_key', '' ); $tone = get_option( 'aipc_tone', 'professional' ); $language = get_option( 'aipc_language', 'English' ); ?>

post_type !== 'product' ) return; wp_enqueue_script( 'aipc-admin', AIPC_PLUGIN_URL . 'assets/admin.js', array( 'jquery' ), AIPC_VERSION, true ); wp_localize_script( 'aipc-admin', 'aipc_ajax', array( 'ajax_url' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'aipc_generate' ), )); } // ───────────────────────────────────────────── // 3. META BOX ON PRODUCT EDIT PAGE // ───────────────────────────────────────────── add_action( 'add_meta_boxes', 'aipc_add_meta_box' ); function aipc_add_meta_box() { add_meta_box( 'aipc_generator', esc_html__( '✨ AI Content Generator', 'ai-product-content-generator' ), 'aipc_meta_box_html', 'product', 'normal', 'high' ); } function aipc_meta_box_html( $post ) { $has_key = (bool) get_option( 'aipc_openai_key', '' ); ?>
⚠️
30, 'headers' => array( 'Authorization' => 'Bearer ' . $api_key, 'Content-Type' => 'application/json', ), 'body' => wp_json_encode( array( 'model' => 'gpt-4o-mini', 'messages' => array( array( 'role' => 'user', 'content' => $prompt ) ), 'temperature' => 0.7, 'max_tokens' => 800, )), )); if ( is_wp_error( $response ) ) { wp_send_json_error( 'Connection error: ' . sanitize_text_field( $response->get_error_message() ) ); } $body = json_decode( wp_remote_retrieve_body( $response ), true ); if ( isset( $body['error'] ) ) { $msg = isset( $body['error']['message'] ) ? sanitize_text_field( $body['error']['message'] ) : 'OpenAI API error.'; wp_send_json_error( $msg ); } $raw_content = $body['choices'][0]['message']['content'] ?? ''; $raw_content = preg_replace( '/^```json\s*/i', '', trim( $raw_content ) ); $raw_content = preg_replace( '/```$/', '', trim( $raw_content ) ); $content = json_decode( trim( $raw_content ), true ); if ( ! $content || ! isset( $content['description'] ) ) { wp_send_json_error( 'Could not parse AI response. Please try again.' ); } wp_send_json_success( array( 'description' => wp_kses_post( $content['description'] ), 'seo_title' => sanitize_text_field( $content['seo_title'] ), 'meta_description' => sanitize_text_field( $content['meta_description'] ), )); } // ───────────────────────────────────────────── // 5. AJAX HANDLER — APPLY CONTENT TO PRODUCT // ───────────────────────────────────────────── add_action( 'wp_ajax_aipc_apply', 'aipc_ajax_apply' ); function aipc_ajax_apply() { check_ajax_referer( 'aipc_generate', 'nonce' ); $product_id = intval( $_POST['product_id'] ?? 0 ); $field = sanitize_text_field( $_POST['field'] ?? '' ); if ( ! $product_id || ! current_user_can( 'edit_post', $product_id ) ) { wp_send_json_error( 'Permission denied.' ); } // Use appropriate sanitization per field type. if ( 'description' === $field ) { $value = wp_kses_post( $_POST['value'] ?? '' ); } else { $value = sanitize_text_field( $_POST['value'] ?? '' ); } switch ( $field ) { case 'description': wp_update_post( array( 'ID' => $product_id, 'post_content' => $value ) ); break; case 'seo_title': update_post_meta( $product_id, '_yoast_wpseo_title', $value ); update_post_meta( $product_id, 'rank_math_title', $value ); update_post_meta( $product_id, '_aipc_seo_title', $value ); break; case 'meta_description': update_post_meta( $product_id, '_yoast_wpseo_metadesc', $value ); update_post_meta( $product_id, 'rank_math_description', $value ); update_post_meta( $product_id, '_aipc_meta_description', $value ); break; } wp_send_json_success( 'Applied.' ); } // ───────────────────────────────────────────── // 6. PLUGIN ACTION LINKS // ───────────────────────────────────────────── add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'aipc_action_links' ); function aipc_action_links( $links ) { $settings_link = '' . esc_html__( 'Settings', 'ai-product-content-generator' ) . ''; array_unshift( $links, $settings_link ); return $links; }