Skip to content

Commit

Permalink
fix homepages generation
Browse files Browse the repository at this point in the history
add wrapper APIs
  • Loading branch information
amartya-dev committed Nov 17, 2023
1 parent 6f72164 commit 09c7784
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 11 deletions.
5 changes: 2 additions & 3 deletions bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,16 @@ function () {
}

register(
[
array(
'name' => 'ai',
'label' => __( 'ai', 'newfold-ai-module' ),
'callback' => function ( Container $container ) {
return new AI( $container );
},
'isActive' => true,
'isHidden' => true,
]
)
);

}
);

Expand Down
82 changes: 82 additions & 0 deletions includes/RestApi/AISearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 );
}

/**
Expand Down
29 changes: 21 additions & 8 deletions includes/SiteGen/SiteGen.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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'
Expand All @@ -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 );
}
}
Expand Down

0 comments on commit 09c7784

Please sign in to comment.