Skip to content

Commit

Permalink
Merge pull request #31 from newfold-labs/enhance/contact-pages
Browse files Browse the repository at this point in the history
Added support for contact page templates
  • Loading branch information
abhijitb authored Aug 28, 2024
2 parents f72cb1b + 0c68f2b commit 23e0c46
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 3 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
}
],
"require": {
"newfold-labs/wp-module-data": "^2.4.18"
"newfold-labs/wp-module-data": "^2.4.18",
"newfold-labs/wp-module-installer": "^1.1.4"
},
"require-dev": {
"newfold-labs/wp-php-standards": "^1.2"
Expand Down
44 changes: 43 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 63 additions & 1 deletion includes/SiteGen/SiteGen.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use NewfoldLabs\WP\Module\Data\HiiveConnection;
use NewfoldLabs\WP\Module\Data\SiteCapabilities;
use NewfoldLabs\WP\Module\AI\Patterns;
use NewfoldLabs\WP\Module\Installer\Services\PluginInstaller;
use NewfoldLabs\WP\Module\Installer\Data\Plugins;

/**
* The class to generate different parts of the site gen object.
Expand Down Expand Up @@ -140,7 +142,7 @@ private static function get_patterns_for_category( $category, $site_classificati

if ( wp_remote_retrieve_response_code( $response ) !== 200 ) {
return array(
'error' => __( 'We are unable to process the request at this moment' ),
'error' => __( 'We are unable to process the request at this moment', 'wp-module-ai' ),
);
}

Expand All @@ -154,6 +156,48 @@ private static function get_patterns_for_category( $category, $site_classificati
return $processed_patterns;
}

/**
* Get the templates for a particular category.
*
* @param string $category The category to get templates for.
* @param array $site_classification site classification as determined by AI.
*/
private static function get_templates_for_category( $category, $site_classification = array() ) {
$primary_sitetype = isset( $site_classification['primaryType'] ) ? $site_classification['primaryType'] : null;
$secondary_sitetype = isset( $site_classification['slug'] ) ? $site_classification['slug'] : null;
$args = array(
'category' => $category,
'primary_type' => $primary_sitetype,
'secondary_type' => $secondary_sitetype,
);
$api_url = NFD_PATTERNS_BASE . 'templates?' . http_build_query( $args );

$response = wp_remote_get(
$api_url,
array(
'headers' => array(
'Content-Type' => 'application/json',
),
'timeout' => 60,
)
);

if ( wp_remote_retrieve_response_code( $response ) !== 200 ) {
return array(
'error' => __( 'We are unable to process the request at this moment', 'wp-module-ai' ),
);
}

$templates = json_decode( wp_remote_retrieve_body( $response ), true );
$processed_templates = array();

foreach ( $templates['data'] as $template ) {
$processed_templates[ $template['slug'] ] = $template;
}

return $processed_templates;
}

/**
* Function to generate all possible patterns for the current generation.
*
Expand Down Expand Up @@ -565,6 +609,24 @@ function ( $key ) use ( $menu_patterns_slugs ) {
}
}

// if contact page then pick from contact page templates
// also make sure the jetpack plugin is installed and active
// then activate "contact-form" module since some templates use jetpack forms
if ( 'contact' === $page ) {
$contact_page_templates = self::get_templates_for_category( $page, $site_classification );
// templates fetched successfully
if ( ! isset( $contact_page_templates['error'] ) ) {
$random_contact_page_template_slug = array_rand( $contact_page_templates, 1 );
$contact_page_content = $contact_page_templates[ $random_contact_page_template_slug ]['content'];

// install and activate the Jetpack plugin and enable the "contact-form" module
if ( PluginInstaller::install( 'jetpack', true ) && Plugins::toggle_jetpack_module( 'contact-form', true ) ) {
// return contact page
return $contact_page_content;
}
}
}

$response = wp_remote_post(
NFD_AI_BASE . 'generatePageContent',
array(
Expand Down

0 comments on commit 23e0c46

Please sign in to comment.