get_header( 'X-API-Key' ); if ( '' === $provided ) { return false; } $ok = hash_equals( aiso_get_api_key(), $provided ); // Remember that the dashboard has connected, so the setup notice can stop. if ( $ok && ! get_option( 'aiso_connected' ) ) { add_option( 'aiso_connected', time(), '', false ); } return $ok; } /* * REST routes (namespace: aiso/v1). */ add_action( 'rest_api_init', function () { $auth = 'aiso_check_auth'; $routes = array( array( '/products', 'GET', 'aiso_get_products' ), array( '/pages', 'GET', 'aiso_get_pages' ), array( '/posts', 'GET', 'aiso_get_posts' ), array( '/get-meta/(?P\d+)', 'GET', 'aiso_get_meta_endpoint' ), array( '/update-meta', 'POST', 'aiso_update_meta' ), array( '/update-product', 'POST', 'aiso_update_product' ), array( '/add-product-image', 'POST', 'aiso_add_product_image' ), array( '/categories', 'GET', 'aiso_get_categories' ), array( '/update-category', 'POST', 'aiso_update_category' ), array( '/bulk-update-meta', 'POST', 'aiso_bulk_update_meta' ), array( '/images-missing-alt', 'GET', 'aiso_images_missing_alt' ), array( '/update-image-alt', 'POST', 'aiso_update_image_alt' ), array( '/seo-health', 'GET', 'aiso_seo_health' ), array( '/mark-optimised', 'POST', 'aiso_mark_optimised' ), array( '/create-post', 'POST', 'aiso_create_post' ), ); // Public: tells the dashboard the connector is installed (no store data). register_rest_route( 'aiso/v1', '/test', array( 'methods' => 'GET', 'callback' => 'aiso_test', 'permission_callback' => '__return_true', ) ); foreach ( $routes as $r ) { register_rest_route( 'aiso/v1', $r[0], array( 'methods' => $r[1], 'callback' => $r[2], 'permission_callback' => $auth, ) ); } } ); /* * Endpoints. */ /** * Connection check. Public, so it only says what is installed. * * @return array */ function aiso_test() { return array( 'status' => 'AI Store Optimiser Connector is working', 'version' => AISO_CONNECTOR_VERSION, 'site' => get_bloginfo( 'name' ), 'site_url' => home_url(), 'woocommerce' => class_exists( 'WooCommerce' ), 'yoast' => defined( 'WPSEO_VERSION' ), 'rankmath' => class_exists( 'RankMath' ), 'studio_images' => true, ); } /** * Published WooCommerce products, paged. * * @param WP_REST_Request $request The request. * @return array */ function aiso_get_products( $request ) { if ( ! class_exists( 'WooCommerce' ) ) { return new WP_Error( 'no_woo', 'WooCommerce is not installed on this site.', array( 'status' => 400 ) ); } $per_page = min( 100, max( 1, absint( $request->get_param( 'per_page' ) ? $request->get_param( 'per_page' ) : 50 ) ) ); $page = max( 1, absint( $request->get_param( 'page' ) ? $request->get_param( 'page' ) : 1 ) ); $products = get_posts( array( 'post_type' => 'product', 'posts_per_page' => $per_page, 'paged' => $page, 'post_status' => 'publish', 'has_password' => false, ) ); return array( 'page' => $page, 'per_page' => $per_page, 'items' => array_map( function ( $post ) { $data = aiso_format_post( $post ); $wc = wc_get_product( $post->ID ); if ( $wc ) { $data['sku'] = $wc->get_sku(); $data['price'] = $wc->get_price(); $data['short_description'] = $wc->get_short_description(); $data['categories'] = wp_get_post_terms( $post->ID, 'product_cat', array( 'fields' => 'names' ) ); $data['image'] = wp_get_attachment_url( $wc->get_image_id() ); } return $data; }, $products ), ); } /** * Published pages. * * @return array */ function aiso_get_pages() { return array_map( 'aiso_format_post', get_posts( array( 'post_type' => 'page', 'posts_per_page' => 500, 'post_status' => 'publish', 'has_password' => false, ) ) ); } /** * Published blog posts. * * @return array */ function aiso_get_posts() { return array_map( 'aiso_format_post', get_posts( array( 'post_type' => 'post', 'posts_per_page' => 500, 'post_status' => 'publish', 'has_password' => false, ) ) ); } /** * Shape of one post, page or product in API responses. * * @param WP_Post $post The post. * @return array */ function aiso_format_post( $post ) { $last = get_post_meta( $post->ID, '_aiso_last_update', true ); return array( 'id' => $post->ID, 'title' => $post->post_title, 'slug' => $post->post_name, 'url' => get_permalink( $post->ID ), 'type' => $post->post_type, 'content' => $post->post_content, 'excerpt' => $post->post_excerpt, 'modified' => $post->post_modified, 'featured_image' => get_the_post_thumbnail_url( $post->ID, 'full' ), 'seo' => aiso_get_seo_meta( $post->ID ), 'last_optimised' => $last ? $last : null, ); } /** * Read SEO meta from whichever plugin is active (Yoast SEO or Rank Math). * * @param int $post_id Post ID. * @return array */ function aiso_get_seo_meta( $post_id ) { $meta = array( 'title' => '', 'description' => '', 'focus_keyword' => '', 'source' => 'none', ); if ( defined( 'WPSEO_VERSION' ) ) { $meta['title'] = get_post_meta( $post_id, '_yoast_wpseo_title', true ); $meta['description'] = get_post_meta( $post_id, '_yoast_wpseo_metadesc', true ); $meta['focus_keyword'] = get_post_meta( $post_id, '_yoast_wpseo_focuskw', true ); if ( $meta['title'] || $meta['description'] ) { $meta['source'] = 'yoast'; } } if ( class_exists( 'RankMath' ) && ! $meta['title'] && ! $meta['description'] ) { $meta['title'] = get_post_meta( $post_id, 'rank_math_title', true ); $meta['description'] = get_post_meta( $post_id, 'rank_math_description', true ); $meta['focus_keyword'] = get_post_meta( $post_id, 'rank_math_focus_keyword', true ); if ( $meta['title'] || $meta['description'] ) { $meta['source'] = 'rankmath'; } } return $meta; } /** * Write SEO meta to Yoast SEO and Rank Math (whichever exists). * * @param int $post_id Post ID. * @param string $title Meta title (already sanitised). * @param string $description Meta description (already sanitised). * @param string $keyword Focus keyword (already sanitised). * @return array Which fields were written. */ function aiso_write_seo_meta( $post_id, $title, $description, $keyword ) { $done = array(); if ( defined( 'WPSEO_VERSION' ) ) { if ( $title ) { update_post_meta( $post_id, '_yoast_wpseo_title', $title ); $done[] = 'yoast_title'; } if ( $description ) { update_post_meta( $post_id, '_yoast_wpseo_metadesc', $description ); $done[] = 'yoast_desc'; } if ( $keyword ) { update_post_meta( $post_id, '_yoast_wpseo_focuskw', $keyword ); $done[] = 'yoast_kw'; } } if ( class_exists( 'RankMath' ) ) { if ( $title ) { update_post_meta( $post_id, 'rank_math_title', $title ); $done[] = 'rankmath_title'; } if ( $description ) { update_post_meta( $post_id, 'rank_math_description', $description ); $done[] = 'rankmath_desc'; } if ( $keyword ) { update_post_meta( $post_id, 'rank_math_focus_keyword', $keyword ); $done[] = 'rankmath_kw'; } } return $done; } /* * WooCommerce product categories (read + write description and SEO). * Uses WordPress core functions and never changes a name or slug. */ /** * All product categories. * * @return array */ function aiso_get_categories() { if ( ! taxonomy_exists( 'product_cat' ) ) { return array( 'error' => 'WooCommerce product categories were not found on this site.' ); } $terms = get_terms( array( 'taxonomy' => 'product_cat', 'hide_empty' => false, ) ); if ( is_wp_error( $terms ) ) { return array( 'items' => array() ); } return array( 'items' => array_map( function ( $t ) { $img_id = get_term_meta( $t->term_id, 'thumbnail_id', true ); return array( 'id' => $t->term_id, 'name' => $t->name, 'slug' => $t->slug, 'description' => $t->description, 'count' => $t->count, 'image' => $img_id ? wp_get_attachment_url( $img_id ) : null, 'seo' => aiso_get_term_seo( $t->term_id ), ); }, $terms ), ); } /** * SEO meta of a product category. * * @param int $term_id Term ID. * @return array */ function aiso_get_term_seo( $term_id ) { $meta = array( 'title' => '', 'description' => '', 'focus_keyword' => '', ); if ( class_exists( 'RankMath' ) ) { $meta['title'] = (string) get_term_meta( $term_id, 'rank_math_title', true ); $meta['description'] = (string) get_term_meta( $term_id, 'rank_math_description', true ); } if ( defined( 'WPSEO_VERSION' ) ) { $tax = get_option( 'wpseo_taxonomy_meta' ); if ( ! empty( $tax['product_cat'][ $term_id ] ) ) { $y = $tax['product_cat'][ $term_id ]; if ( ! $meta['title'] && ! empty( $y['wpseo_title'] ) ) { $meta['title'] = $y['wpseo_title']; } if ( ! $meta['description'] && ! empty( $y['wpseo_desc'] ) ) { $meta['description'] = $y['wpseo_desc']; } } } return $meta; } /** * Update a category's description and SEO meta (never its name or slug). * * @param WP_REST_Request $request The request. * @return array */ function aiso_update_category( $request ) { $id = absint( $request->get_param( 'category_id' ) ); if ( ! $id ) { return array( 'success' => false, 'error' => 'category_id is required', ); } $term = get_term( $id, 'product_cat' ); if ( ! $term || is_wp_error( $term ) ) { return array( 'success' => false, 'error' => 'category not found', ); } $description = $request->get_param( 'description' ); if ( null !== $description && '' !== $description ) { wp_update_term( $id, 'product_cat', array( 'description' => aiso_term_description( (string) $description ) ) ); } aiso_write_term_seo( $id, sanitize_text_field( (string) $request->get_param( 'meta_title' ) ), sanitize_textarea_field( (string) $request->get_param( 'meta_description' ) ) ); update_term_meta( $id, '_aiso_last_update', current_time( 'mysql' ) ); return array( 'success' => true, 'category_id' => $id, ); } /** * WordPress core only keeps simple inline tags in term descriptions, so * paragraphs, line breaks and list items become line breaks, which themes turn * back into paragraphs when the category page is shown. * * @param string $html Description HTML from the dashboard. * @return string */ function aiso_term_description( $html ) { $text = preg_replace( '#<(script|style)\b[^>]*>.*?#is', '', $html ); // Drop code, not just its tags. $text = wp_kses_post( $text ); $text = preg_replace( '##i', "\n", $text ); $text = preg_replace( '##i', "\n\n", $text ); $text = preg_replace( '#]*>#i', '- ', $text ); $text = preg_replace( '#<(p|h[1-6]|ul|ol|blockquote)(\s[^>]*)?>#i', '', $text ); return trim( preg_replace( "#\n{3,}#", "\n\n", $text ) ); } /** * Write a category's SEO meta to Rank Math and Yoast SEO (whichever exists). * * @param int $term_id Term ID. * @param string $title Meta title (already sanitised). * @param string $description Meta description (already sanitised). */ function aiso_write_term_seo( $term_id, $title, $description ) { if ( class_exists( 'RankMath' ) ) { if ( $title ) { update_term_meta( $term_id, 'rank_math_title', $title ); } if ( $description ) { update_term_meta( $term_id, 'rank_math_description', $description ); } } if ( defined( 'WPSEO_VERSION' ) ) { $tax = get_option( 'wpseo_taxonomy_meta' ); if ( ! is_array( $tax ) ) { $tax = array(); } if ( empty( $tax['product_cat'] ) ) { $tax['product_cat'] = array(); } if ( empty( $tax['product_cat'][ $term_id ] ) ) { $tax['product_cat'][ $term_id ] = array(); } if ( $title ) { $tax['product_cat'][ $term_id ]['wpseo_title'] = $title; } if ( $description ) { $tax['product_cat'][ $term_id ]['wpseo_desc'] = $description; } update_option( 'wpseo_taxonomy_meta', $tax ); } } /** * SEO meta of one post, page or product. * * @param WP_REST_Request $request The request. * @return array|WP_Error */ function aiso_get_meta_endpoint( $request ) { $id = absint( $request->get_param( 'id' ) ); $post = get_post( $id ); if ( ! $post ) { return new WP_Error( 'not_found', 'Not found', array( 'status' => 404 ) ); } return array( 'id' => $id, 'title' => $post->post_title, 'url' => get_permalink( $id ), 'seo' => aiso_get_seo_meta( $id ), ); } /** * Update the SEO meta of one post, page or product. * * @param WP_REST_Request $request The request. * @return array|WP_Error */ function aiso_update_meta( $request ) { $id = absint( $request->get_param( 'post_id' ) ); if ( ! $id || ! get_post( $id ) ) { return new WP_Error( 'not_found', 'Post not found', array( 'status' => 404 ) ); } if ( ! defined( 'WPSEO_VERSION' ) && ! class_exists( 'RankMath' ) ) { return new WP_Error( 'no_seo_plugin', 'No SEO plugin found. Install Yoast SEO or Rank Math so meta titles and descriptions have somewhere to be saved.', array( 'status' => 422 ) ); } $done = aiso_write_seo_meta( $id, sanitize_text_field( (string) $request->get_param( 'meta_title' ) ), sanitize_textarea_field( (string) $request->get_param( 'meta_description' ) ), sanitize_text_field( (string) $request->get_param( 'focus_keyword' ) ) ); clean_post_cache( $id ); update_post_meta( $id, '_aiso_last_update', current_time( 'mysql' ) ); return array( 'success' => true, 'post_id' => $id, 'updated' => $done, ); } /** * Update a product's description, short description and SEO meta. * * @param WP_REST_Request $request The request. * @return array|WP_Error */ function aiso_update_product( $request ) { if ( ! class_exists( 'WooCommerce' ) ) { return new WP_Error( 'no_woo', 'WooCommerce not installed', array( 'status' => 400 ) ); } $id = absint( $request->get_param( 'product_id' ) ); $product = wc_get_product( $id ); if ( ! $product ) { return new WP_Error( 'not_found', 'Product not found', array( 'status' => 404 ) ); } $done = array(); $desc = $request->get_param( 'description' ); $short_desc = $request->get_param( 'short_description' ); if ( null !== $desc && '' !== $desc ) { $product->set_description( wp_kses_post( (string) $desc ) ); $done[] = 'description'; } if ( null !== $short_desc && '' !== $short_desc ) { $product->set_short_description( wp_kses_post( (string) $short_desc ) ); $done[] = 'short_description'; } $product->save(); $done = array_merge( $done, aiso_write_seo_meta( $id, sanitize_text_field( (string) $request->get_param( 'meta_title' ) ), sanitize_textarea_field( (string) $request->get_param( 'meta_description' ) ), sanitize_text_field( (string) $request->get_param( 'focus_keyword' ) ) ) ); clean_post_cache( $id ); update_post_meta( $id, '_aiso_last_update', current_time( 'mysql' ) ); return array( 'success' => true, 'product_id' => $id, 'updated' => $done, ); } /** * Product Studio: save an AI image to the media library and set it as the * product's main image (set_featured) or add it to the gallery. The image * arrives as base64 data (PNG, JPEG or WebP) because it comes from our server. * * @param WP_REST_Request $request The request. * @return array|WP_Error */ function aiso_add_product_image( $request ) { if ( ! class_exists( 'WooCommerce' ) ) { return new WP_Error( 'no_woo', 'WooCommerce not installed', array( 'status' => 400 ) ); } $id = absint( $request->get_param( 'product_id' ) ); $product = wc_get_product( $id ); if ( ! $product ) { return new WP_Error( 'not_found', 'Product not found', array( 'status' => 404 ) ); } $b64 = (string) $request->get_param( 'image_base64' ); if ( '' === $b64 ) { return new WP_Error( 'no_image', 'image_base64 required', array( 'status' => 400 ) ); } $b64 = preg_replace( '#^data:image/\w+;base64,#i', '', $b64 ); $bytes = base64_decode( $b64, true ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode -- image upload payload, not code. if ( false === $bytes || strlen( $bytes ) < 100 ) { return new WP_Error( 'bad_image', 'Invalid image data', array( 'status' => 400 ) ); } if ( strlen( $bytes ) > 6 * 1024 * 1024 ) { return new WP_Error( 'too_big', 'Image too large', array( 'status' => 400 ) ); } // Only real PNG, JPEG or WebP images are accepted (checked by their first bytes). if ( "\x89PNG\r\n\x1a\n" === substr( $bytes, 0, 8 ) ) { $ext = 'png'; $mime = 'image/png'; } elseif ( "\xFF\xD8\xFF" === substr( $bytes, 0, 3 ) ) { $ext = 'jpg'; $mime = 'image/jpeg'; } elseif ( 'RIFF' === substr( $bytes, 0, 4 ) && 'WEBP' === substr( $bytes, 8, 4 ) ) { $ext = 'webp'; $mime = 'image/webp'; } else { return new WP_Error( 'bad_image', 'Only PNG, JPEG or WebP images are accepted', array( 'status' => 400 ) ); } $alt = sanitize_text_field( (string) $request->get_param( 'alt' ) ); $type = sanitize_key( (string) $request->get_param( 'type' ) ); $filename = 'aiso-' . $id . '-' . ( $type ? $type : 'studio' ) . '-' . time() . '.' . $ext; $upload = wp_upload_bits( $filename, null, $bytes ); if ( ! empty( $upload['error'] ) ) { return new WP_Error( 'upload_failed', $upload['error'], array( 'status' => 500 ) ); } $attach_id = wp_insert_attachment( array( 'post_mime_type' => $mime, 'post_title' => $alt ? $alt : get_the_title( $id ), 'post_content' => '', 'post_status' => 'inherit', ), $upload['file'], $id ); if ( is_wp_error( $attach_id ) || ! $attach_id ) { return new WP_Error( 'attach_failed', 'Could not create attachment', array( 'status' => 500 ) ); } // REST requests do not load the admin image functions; load them only when needed and use them straight away. if ( ! function_exists( 'wp_generate_attachment_metadata' ) ) { require_once ABSPATH . 'wp-admin/includes/image.php'; } wp_update_attachment_metadata( $attach_id, wp_generate_attachment_metadata( $attach_id, $upload['file'] ) ); if ( $alt ) { update_post_meta( $attach_id, '_wp_attachment_image_alt', $alt ); } $sf = $request->get_param( 'set_featured' ); $set_featured = ( true === $sf || 'true' === $sf || 1 === $sf || '1' === $sf ); if ( $set_featured ) { $old = $product->get_image_id(); set_post_thumbnail( $id, $attach_id ); if ( $old ) { $gallery = $product->get_gallery_image_ids(); if ( ! in_array( (int) $old, array_map( 'intval', $gallery ), true ) ) { $gallery[] = $old; $product->set_gallery_image_ids( $gallery ); } } } else { $gallery = $product->get_gallery_image_ids(); $gallery[] = $attach_id; $product->set_gallery_image_ids( $gallery ); } $product->save(); clean_post_cache( $id ); update_post_meta( $id, '_aiso_last_update', current_time( 'mysql' ) ); return array( 'success' => true, 'product_id' => $id, 'attachment_id' => $attach_id, 'url' => wp_get_attachment_url( $attach_id ), 'featured' => $set_featured, ); } /** * Update SEO meta for many posts at once. * * @param WP_REST_Request $request The request. * @return array|WP_Error */ function aiso_bulk_update_meta( $request ) { $items = $request->get_param( 'items' ); if ( ! is_array( $items ) ) { return new WP_Error( 'invalid', 'items array required', array( 'status' => 400 ) ); } $results = array(); foreach ( array_slice( $items, 0, 200 ) as $item ) { $id = isset( $item['post_id'] ) ? absint( $item['post_id'] ) : 0; if ( ! $id || ! get_post( $id ) ) { continue; } aiso_write_seo_meta( $id, sanitize_text_field( isset( $item['meta_title'] ) ? (string) $item['meta_title'] : '' ), sanitize_textarea_field( isset( $item['meta_description'] ) ? (string) $item['meta_description'] : '' ), sanitize_text_field( isset( $item['focus_keyword'] ) ? (string) $item['focus_keyword'] : '' ) ); clean_post_cache( $id ); update_post_meta( $id, '_aiso_last_update', current_time( 'mysql' ) ); $results[] = $id; } return array( 'success' => true, 'updated' => count( $results ), 'ids' => $results, ); } /** * Up to 100 images in the media library that have no alt text. * * @return array */ function aiso_images_missing_alt() { $ids = get_posts( array( 'post_type' => 'attachment', 'post_mime_type' => 'image', 'post_status' => 'inherit', 'posts_per_page' => 100, 'orderby' => 'ID', 'order' => 'DESC', 'fields' => 'ids', 'meta_query' => array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query -- one capped admin-side lookup. 'relation' => 'OR', array( 'key' => '_wp_attachment_image_alt', 'compare' => 'NOT EXISTS', ), array( 'key' => '_wp_attachment_image_alt', 'value' => '', ), ), ) ); return array( 'total' => count( $ids ), 'images' => array_map( function ( $image_id ) { $url = wp_get_attachment_url( $image_id ); return array( 'id' => $image_id, 'url' => $url, 'filename' => $url ? wp_basename( $url ) : '', 'title' => get_the_title( $image_id ), ); }, $ids ), ); } /** * Set the alt text of one image. * * @param WP_REST_Request $request The request. * @return array|WP_Error */ function aiso_update_image_alt( $request ) { $id = absint( $request->get_param( 'image_id' ) ); $image = get_post( $id ); if ( ! $image || 'attachment' !== $image->post_type ) { return new WP_Error( 'not_found', 'Image not found', array( 'status' => 404 ) ); } $alt = sanitize_text_field( (string) $request->get_param( 'alt_text' ) ); update_post_meta( $id, '_wp_attachment_image_alt', $alt ); return array( 'success' => true, 'image_id' => $id, 'alt_text' => $alt, ); } /** * Site-wide SEO summary (how many items have a meta description). * * @return array */ function aiso_seo_health() { $types = array( 'page', 'post' ); if ( class_exists( 'WooCommerce' ) ) { $types[] = 'product'; } $ids = get_posts( array( 'post_type' => $types, 'posts_per_page' => 2000, 'post_status' => 'publish', 'fields' => 'ids', ) ); update_postmeta_cache( $ids ); // One query for all meta instead of one per item. $total = count( $ids ); $with_desc = 0; $never = 0; foreach ( $ids as $post_id ) { $seo = aiso_get_seo_meta( $post_id ); if ( ! empty( $seo['description'] ) ) { ++$with_desc; } if ( ! get_post_meta( $post_id, '_aiso_last_update', true ) ) { ++$never; } } return array( 'generated_at' => current_time( 'mysql' ), 'total_items' => $total, 'with_meta_desc' => $with_desc, 'missing_meta_desc' => $total - $with_desc, 'never_optimised' => $never, 'meta_desc_percent' => $total ? round( ( $with_desc / $total ) * 100 ) : 0, ); } /** * Record that an item was optimised (shown in the dashboard). * * @param WP_REST_Request $request The request. * @return array|WP_Error */ function aiso_mark_optimised( $request ) { $id = absint( $request->get_param( 'post_id' ) ); if ( ! $id || ! get_post( $id ) ) { return new WP_Error( 'not_found', 'Post not found', array( 'status' => 404 ) ); } update_post_meta( $id, '_aiso_last_update', current_time( 'mysql' ) ); return array( 'success' => true, 'post_id' => $id, 'optimised_at' => current_time( 'mysql' ), ); } /** * Blog Engine: create a blog post. Saved as a draft unless the request asks * to publish. WordPress makes the slug from the title, so no URL is ever set * by hand. * * @param WP_REST_Request $request The request. * @return array|WP_Error */ function aiso_create_post( $request ) { $title = sanitize_text_field( (string) $request->get_param( 'title' ) ); $content = wp_kses_post( (string) $request->get_param( 'content' ) ); if ( '' === $title || '' === $content ) { return new WP_Error( 'bad_request', 'A title and content are required.', array( 'status' => 400 ) ); } $status = 'publish' === $request->get_param( 'status' ) ? 'publish' : 'draft'; $excerpt = sanitize_textarea_field( (string) $request->get_param( 'excerpt' ) ); $tags = array_filter( array_map( 'sanitize_text_field', array_map( 'trim', explode( ',', (string) $request->get_param( 'tags' ) ) ) ) ); // Requests come from our server with no logged-in user, so credit the post to // the site's first administrator instead of leaving the author empty. $admins = get_users( array( 'role' => 'administrator', 'number' => 1, 'orderby' => 'ID', 'fields' => 'ID', ) ); $post_id = wp_insert_post( array( 'post_author' => $admins ? (int) $admins[0] : 0, 'post_title' => $title, 'post_content' => $content, 'post_excerpt' => $excerpt, 'post_status' => $status, 'post_type' => 'post', 'tags_input' => $tags, ), true ); if ( is_wp_error( $post_id ) ) { return $post_id; } update_post_meta( $post_id, '_aiso_generated', current_time( 'mysql' ) ); return array( 'success' => true, 'id' => $post_id, 'url' => get_permalink( $post_id ), 'status' => $status, ); } /* * Admin settings page: shows the store owner the two values to paste into * the AI Store Optimiser dashboard. */ add_action( 'admin_menu', function () { add_options_page( __( 'AI Store Optimiser', 'ai-store-optimiser-connector' ), __( 'AI Store Optimiser', 'ai-store-optimiser-connector' ), 'manage_options', 'aiso-connector', 'aiso_settings_page' ); } ); add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), function ( $links ) { array_unshift( $links, '' . esc_html__( 'Connect', 'ai-store-optimiser-connector' ) . '' ); return $links; } ); /* Small behaviours for the settings page only (select the key on click, confirm a new key). */ add_action( 'admin_enqueue_scripts', function ( $hook ) { if ( 'settings_page_aiso-connector' !== $hook ) { return; } wp_register_script( 'aiso-settings', '', array(), AISO_CONNECTOR_VERSION, true ); wp_enqueue_script( 'aiso-settings' ); wp_add_inline_script( 'aiso-settings', sprintf( '(function(){var k=document.getElementById("aiso-key");if(k){k.addEventListener("click",function(){k.select();});}var f=document.getElementById("aiso-regen-form");if(f){f.addEventListener("submit",function(e){if(!window.confirm(%s)){e.preventDefault();}});}})();', wp_json_encode( __( 'Generate a new key? You will need to update it in your AI Store Optimiser dashboard.', 'ai-store-optimiser-connector' ) ) ) ); } ); /** * Render the settings page. */ function aiso_settings_page() { if ( ! current_user_can( 'manage_options' ) ) { return; } if ( isset( $_POST['aiso_regen'] ) && check_admin_referer( 'aiso_regen_key' ) ) { update_option( 'aiso_api_key', wp_generate_password( 40, false, false ), false ); echo '

' . esc_html__( 'A new key was generated. Update it in your AI Store Optimiser dashboard.', 'ai-store-optimiser-connector' ) . '

'; } $key = aiso_get_api_key(); $url = home_url(); ?>

  1. storeoptimiser.ai' ); ?>

id ) { $link = admin_url( 'options-general.php?page=aiso-connector' ); printf( '

%1$s %2$s %4$s

', esc_html__( 'AI Store Optimiser:', 'ai-store-optimiser-connector' ), esc_html__( 'active.', 'ai-store-optimiser-connector' ), esc_url( $link ), esc_html__( 'Get your connection key', 'ai-store-optimiser-connector' ) ); } } );