Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sitegen: Trigger sitemap pages once preview step is done #440

Merged
merged 3 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"newfold-labs/wp-module-onboarding-data": "^0.1.0",
"newfold-labs/wp-module-patterns": "^0.1.12",
"newfold-labs/wp-module-install-checker": "^1.0",
"newfold-labs/wp-module-facebook": "1.0.0"
"newfold-labs/wp-module-facebook": "^1.0.2"
},
"require-dev": {
"wp-phpunit/wp-phpunit": "^6.2",
Expand Down
14 changes: 7 additions & 7 deletions composer.lock

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

66 changes: 59 additions & 7 deletions includes/RestApi/SiteGenController.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,17 @@ public function register_routes() {
'permission_callback' => array( Permissions::class, 'rest_is_authorized_admin' ),
)
);

\register_rest_route(
$this->namespace,
$this->rest_base . '/pages/sitemap',
array(
'methods' => \WP_REST_Server::CREATABLE,
'callback' => array( $this, 'publish_sitemap_pages' ),
'permission_callback' => array( Permissions::class, 'rest_is_authorized_admin' ),
'args' => $this->get_publish_sitemap_pages_args(),
)
);
}

/**
Expand All @@ -113,7 +124,7 @@ public function sitegen_meta_args() {
}

/**
* Gets the arguments for the 'get-homepages' endpoint.
* Gets the arguments for the homepages endpoint.
*
* @return array The array of arguments.
*/
Expand All @@ -127,6 +138,21 @@ public function get_homepages_args() {
);
}

/**
* Gets the arguments for the '/pages/sitemap' endpoint.
*
* @return array The array of arguments.
*/
public function get_publish_sitemap_pages_args() {
return array(
'site_description' => array(
'required' => true,
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
),
);
}

/**
* Gets the arguments for the 'get-homepages' endpoint.
*
Expand Down Expand Up @@ -264,24 +290,50 @@ public function regenerate_homepage( \WP_REST_Request $request ) {
}

/**
* Generate Sitegen Site Details meta data.
* Publish the pages in the sitemap.
*
* @param \WP_REST_Request $request Request model.
* @param \WP_REST_Request $request The incoming request
* @return \WP_REST_Response|\WP_Error
*/
public function publish_sitemap_pages( \WP_REST_Request $request ) {
$site_description = $request->get_param( 'site_description' );
$site_info = array( 'site_description' => $site_description );

$target_audience = SiteGenService::instantiate_site_meta( $site_info, 'target_audience' );
if ( is_wp_error( $target_audience ) ) {
return $target_audience;
}

$content_style = SiteGenService::instantiate_site_meta( $site_info, 'content_tones' );
if ( is_wp_error( $content_style ) ) {
return $content_style;
}

$sitemap = SiteGenService::instantiate_site_meta( $site_info, 'sitemap' );
if ( is_wp_error( $sitemap ) ) {
return $sitemap;
}

SiteGenService::publish_sitemap_pages( $site_description, $content_style, $target_audience, $sitemap );

return new \WP_REST_Response( array(), 201 );
}

/**
* Generate Sitegen Site Details meta data.
*
* @return array|WP_Error
*/
public function get_site_details_meta( \WP_REST_Request $request ) {
public function get_site_details_meta() {
return SiteGenData::get_site_details_questionnaire();
}

/**
* Get Sitegen Customize sidebar data.
*
* @param \WP_REST_Request $request Request model.
*
* @return array|WP_Error
*/
public function get_customize_sidebar_data( \WP_REST_Request $request ) {
public function get_customize_sidebar_data() {
return SiteGenService::get_customize_sidebar_data();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,11 @@
async function performSiteGenMetaGeneration(
siteInfo,
identifier,
skipCache,
retryCount = 1
) {
return new Promise( () =>
generateSiteGenMeta( siteInfo, identifier )
generateSiteGenMeta( siteInfo, identifier, skipCache )
.then( ( data ) => {
if ( data.body !== null ) {
currentData.sitegen.siteGenMetaStatus.currentStatus += 1;
Expand All @@ -77,6 +78,7 @@
performSiteGenMetaGeneration(
siteInfo,
identifier,
skipCache,
retryCount + 1
);
}
Expand Down Expand Up @@ -115,9 +117,11 @@
site_description: currentData.sitegen?.siteDetails?.prompt,
};

const skipCache = currentData.sitegen?.skipCache;

// Iterate over Identifiers and fire Requests!
identifiers.forEach( ( identifier ) => {
performSiteGenMetaGeneration( siteInfo, identifier );
performSiteGenMetaGeneration( siteInfo, identifier, skipCache );
} );
}

Expand All @@ -142,13 +146,13 @@
initializePlugins( pluginInstallHash );
setInterval( cronTrigger, 45000 );
}
}, [ initialize ] );

Check warning on line 149 in src/OnboardingSPA/components/NewfoldInterfaceSkeleton/SiteGen/index.js

View workflow job for this annotation

GitHub Actions / Run Lint Checks

React Hook useEffect has a missing dependency: 'pluginInstallHash'. Either include it or remove the dependency array

useEffect( () => {
syncStoreToDB();
generateSiteGenData();
handlePreviousStepTracking();
}, [ location.pathname ] );

Check warning on line 155 in src/OnboardingSPA/components/NewfoldInterfaceSkeleton/SiteGen/index.js

View workflow job for this annotation

GitHub Actions / Run Lint Checks

React Hook useEffect has missing dependencies: 'generateSiteGenData', 'handlePreviousStepTracking', and 'syncStoreToDB'. Either include them or remove the dependency array

useEffect( () => {
initializeThemes();
Expand Down
15 changes: 15 additions & 0 deletions src/OnboardingSPA/steps/SiteGen/Editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

// eslint-disable-next-line import/no-extraneous-dependencies
import { cloneDeep } from 'lodash';
import { publishSitemapPages } from '../../../utils/api/siteGen';

const StepSiteGenEditor = () => {
const [ homepage, setHomepage ] = useState( false );
Expand All @@ -20,6 +21,7 @@
setHeaderActiveView,
setDrawerActiveView,
setHideFooterNav,
setCurrentOnboardingData,
} = useDispatch( nfdOnboardingStore );

const { currentData } = useSelect( ( select ) => {
Expand All @@ -29,6 +31,18 @@
};
} );

const handleSitemapPagesGeneration = async () => {
if ( false === currentData?.sitegen?.sitemapPagesGenerated ) {
const sitemapPagesPublished = await publishSitemapPages(
currentData.sitegen.siteDetails.prompt
);
if ( ! sitemapPagesPublished.error ) {
currentData.sitegen.sitemapPagesGenerated = true;
setCurrentOnboardingData( currentData );
}
}
};

const loadData = async () => {
setHideFooterNav( true );
setIsHeaderEnabled( true );
Expand All @@ -45,7 +59,8 @@
setHeaderActiveView( HEADER_SITEGEN );
setDrawerActiveView( false );
loadData();
handleSitemapPagesGeneration();
}, [] );

Check warning on line 63 in src/OnboardingSPA/steps/SiteGen/Editor/index.js

View workflow job for this annotation

GitHub Actions / Run Lint Checks

React Hook useEffect has missing dependencies: 'handleSitemapPagesGeneration', 'loadData', 'setDrawerActiveView', 'setHeaderActiveView', and 'setIsHeaderEnabled'. Either include them or remove the dependency array

useEffect( () => {
if ( currentData?.sitegen?.homepages?.active ) {
Expand Down
4 changes: 2 additions & 2 deletions src/OnboardingSPA/steps/SiteGen/Preview/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
setHeaderActiveView( HEADER_SITEGEN );
setDrawerActiveView( false );
updateInitialize( true );
}, [ currentData ] );

Check warning on line 50 in src/OnboardingSPA/steps/SiteGen/Preview/index.js

View workflow job for this annotation

GitHub Actions / Run Lint Checks

React Hook useEffect has missing dependencies: 'setDrawerActiveView', 'setHeaderActiveView', 'setHideFooterNav', 'setIsHeaderEnabled', 'setSidebarActiveView', and 'updateInitialize'. Either include them or remove the dependency array

const loadHomepages = async () => {
setIsPreviewLoading( true );
Expand All @@ -62,8 +62,7 @@
}

const response = await getHomepages(
currentData.sitegen.siteDetails.prompt,
false
currentData.sitegen.siteDetails.prompt
);

if ( response.error ) {
Expand All @@ -89,13 +88,14 @@
useEffect( () => {
loadHomepages();
loadGlobalStyles();
}, [] );

Check warning on line 91 in src/OnboardingSPA/steps/SiteGen/Preview/index.js

View workflow job for this annotation

GitHub Actions / Run Lint Checks

React Hook useEffect has a missing dependency: 'loadHomepages'. Either include it or remove the dependency array

const handlePreview = ( slug ) => {
if ( ! ( slug in homepages ) ) {
return false;
}
currentData.sitegen.homepages.active = homepages[ slug ];
currentData.sitegen.skipCache = false;
setCurrentOnboardingData( currentData );
navigate( nextStep.path );
};
Expand Down
5 changes: 3 additions & 2 deletions src/OnboardingSPA/steps/SiteGen/SiteDetails/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,21 @@
return setCustomerInput( currentData.sitegen.siteDetails.prompt );
}
setIsFooterNavAllowed( false );
}, [] );

Check warning on line 48 in src/OnboardingSPA/steps/SiteGen/SiteDetails/index.js

View workflow job for this annotation

GitHub Actions / Run Lint Checks

React Hook useEffect has missing dependencies: 'currentData.sitegen.siteDetails.prompt', 'setDrawerActiveView', 'setHeaderActiveView', 'setHideFooterNav', 'setIsFooterNavAllowed', 'setIsHeaderEnabled', and 'setSidebarActiveView'. Either include them or remove the dependency array

useEffect( () => {
if (
currentData.sitegen.siteDetails.prompt !== undefined &&
customerInput !== undefined &&
customerInput !== '' &&
customerInput !== currentData.sitegen.siteDetails.prompt
) {
currentData.sitegen.siteDetails.prompt = customerInput?.trim();
currentData.sitegen.siteDetails.mode = 'simple';
currentData.sitegen.skipCache = true;
currentData.sitegen.sitemapPagesGenerated = false;
setCurrentOnboardingData( currentData );
}
setIsFooterNavAllowed( isValidInput );
}, [ customerInput ] );

Check warning on line 62 in src/OnboardingSPA/steps/SiteGen/SiteDetails/index.js

View workflow job for this annotation

GitHub Actions / Run Lint Checks

React Hook useEffect has missing dependencies: 'currentData', 'isValidInput', 'setCurrentOnboardingData', and 'setIsFooterNavAllowed'. Either include them or remove the dependency array

return (
<CommonLayout isCentered>
Expand Down
12 changes: 12 additions & 0 deletions src/OnboardingSPA/utils/api/siteGen.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,15 @@ export async function getCustomizeSidebarData() {
} ).then()
);
}

export async function publishSitemapPages( siteDescription ) {
return await resolve(
apiFetch( {
url: onboardingRestURL( 'sitegen/pages/sitemap' ),
method: 'POST',
data: {
site_description: siteDescription,
},
} ).then()
);
}
Loading