diff --git a/bootstrap.php b/bootstrap.php index 1db2362..a85a724 100644 --- a/bootstrap.php +++ b/bootstrap.php @@ -23,7 +23,7 @@ function () { } register( - [ + array( 'name' => 'ai', 'label' => __( 'ai', 'newfold-ai-module' ), 'callback' => function ( Container $container ) { @@ -31,9 +31,8 @@ function () { }, 'isActive' => true, 'isHidden' => true, - ] + ) ); - } ); diff --git a/includes/RestApi/AISearchController.php b/includes/RestApi/AISearchController.php index 4a113f5..4120b99 100644 --- a/includes/RestApi/AISearchController.php +++ b/includes/RestApi/AISearchController.php @@ -2,6 +2,7 @@ namespace NewfoldLabs\WP\Module\AI\RestApi; +use NewfoldLabs\WP\Module\AI\SiteGen\SiteGen; use NewfoldLabs\WP\Module\AI\Utils\AISearchUtil; use NewfoldLabs\WP\Module\Data\HiiveConnection; @@ -64,6 +65,87 @@ public function register_routes() { ), ) ); + + register_rest_route( + $this->namespace, + '/' . $this->rest_base . '/sitegen', + array( + array( + 'methods' => \WP_REST_Server::CREATABLE, + 'callback' => array( $this, 'sitegen' ), + 'args' => array( + 'site_info' => array( + 'required' => true, + 'type' => 'object', + ), + 'identifier' => array( + 'required' => true, + 'type' => 'string', + ), + ), + 'permission_callback' => array( $this, 'check_permission' ), + ), + ) + ); + + register_rest_route( + $this->namespace, + '/' . $this->rest_base . '/homepages', + array( + array( + 'methods' => \WP_REST_Server::CREATABLE, + 'callback' => array( $this, 'homepages' ), + 'args' => array( + 'site_description' => array( + 'required' => true, + 'type' => 'string', + ), + 'content_style' => array( + 'required' => true, + 'type' => 'object', + ), + 'target_audience' => array( + 'required' => true, + 'type' => 'object', + ), + ), + 'permission_callback' => array( $this, 'check_permission' ), + ), + ) + ); + } + + /** + * Proxy to the AI service to get the responses. + * + * @param \WP_REST_Request $request Request object + * + * @returns \WP_REST_Response|\WP_Error + */ + public function sitegen( \WP_REST_Request $request ) { + $site_info = $request['site_info']; + $identifier = $request['identifier']; + + $response = SiteGen::generate_site_meta( $site_info, $identifier ); + + return new \WP_REST_Response( $response, 200 ); + } + + /** + * Proxy to the AI service to get the responses. + * + * @param \WP_REST_Request $request Request object + * + * @returns \WP_REST_Response|\WP_Error + */ + public function homepages( \WP_REST_Request $request ) { + $site_description = $request['site_description']; + $content_style = $request['content_style']; + $target_audience = $request['target_audience']; + + $response = SiteGen::get_home_pages( $site_description, $content_style, $target_audience ); + + return new \WP_REST_Response( $response, 200 ); } /** diff --git a/includes/SiteGen/SiteGen.php b/includes/SiteGen/SiteGen.php index 858a13d..37798ae 100644 --- a/includes/SiteGen/SiteGen.php +++ b/includes/SiteGen/SiteGen.php @@ -81,11 +81,12 @@ private static function validate_site_info( $site_info, $identifier ) { */ private static function get_sitegen_from_cache( $identifier ) { $site_gen = get_option( NFD_SITEGEN_OPTION, null ); - if ( $site_gen && in_array( $identifier, $site_gen, true ) ) { - return $site_gen[ $identifier ]; - } else { + if ( ! $site_gen ) { update_option( NFD_SITEGEN_OPTION, array() ); } + if ( $site_gen && array_key_exists( $identifier, $site_gen ) ) { + return $site_gen[ $identifier ]; + } return null; } @@ -169,7 +170,13 @@ private static function generate_pattern_content( } } - $keywords = self::generate_site_meta( array( 'site_description' => $site_description ), 'keywords' ); + $keywords = self::generate_site_meta( + array( + 'site_description' => $site_description, + 'content_style' => $content_style, + ), + 'keywords' + ); $unique_categories = array(); foreach ( $content_structure as $homepage => $structure ) { @@ -230,6 +237,8 @@ private static function generate_pattern_content( // Store the categories self::cache_sitegen_response( 'contentRegenerate', $category_pattern_map ); + + return $category_pattern_map; } /** @@ -311,7 +320,7 @@ public static function generate_site_meta( $site_info, $identifier, $skip_cache * @param array $target_audience Generated target audience. * @param boolean $regenerate If we need to regenerate. */ - public static function get_home_pages( $site_description, $content_style, $target_audience, $regenerate = false ) { + public static function get_home_pages( $site_description, $content_style, $target_audience, $regenerate = true ) { $generated_content_structures = self::generate_site_meta( array( 'site_description' => $site_description ), 'contentstructure' @@ -333,11 +342,15 @@ public static function get_home_pages( $site_description, $content_style, $targe ); // Choose random categories for the generated patterns and return - foreach ( $random_homepages as $slug => $structure ) { - array_push( $generated_homepages, array( $slug => array() ) ); - foreach ( $structure as $pattern_category ) { + foreach ( $random_homepages as $slug ) { + $generated_homepages[ $slug ] = array(); + foreach ( $generated_content_structures[ $slug ] as $pattern_category ) { + if ( ! $generated_patterns[ $pattern_category ] ) { + continue; + } // Get a random pattern for the category. $random_pattern = array_rand( $generated_patterns[ $pattern_category ] ); + $random_pattern = $generated_patterns[ $random_pattern ]; array_push( $generated_homepages[ $slug ], $random_pattern ); } }