Skip to content

Commit

Permalink
Merge pull request #215 from newfold-labs/add/PRESS2-785-pass-nf_dc_p…
Browse files Browse the repository at this point in the history
…age-when-injecting-site-pages

Add nf_dc_page postmeta to pages published by Onboarding
  • Loading branch information
abhijitb authored Apr 20, 2023
2 parents 0f3cd30 + 133ce86 commit e467956
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 15 deletions.
55 changes: 49 additions & 6 deletions includes/Data/Patterns.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,33 @@ protected static function get_theme_step_patterns() {
);
}

/**
* Get post metadata for a pattern. Ref: SitePagesController.php
*
* @return array
*/
public static function get_theme_patterns_meta() {
return array(
'yith-wonder' => array(
'company-page' => array(
'nf_dc_page' => 'about',
),
'contact-us' => array(
'nf_dc_page' => 'contact',
),
'homepage-1' => array(
'nf_dc_page' => 'home',
),
'homepage-2' => array(
'nf_dc_page' => 'home',
),
'homepage-3' => array(
'nf_dc_page' => 'home',
),
),
);
}

/**
* Sanitize the content by cleaning wp_grammar.
*
Expand All @@ -144,6 +171,23 @@ private static function cleanup_wp_grammar( $content ) {
return $content;
}

/**
* Get the post meta for a given pattern slug.
*
* @param string $pattern_slug The pattern slug (theme/kebab-cased-name).
* @return array|boolean
*/
public static function get_meta_from_pattern_slug( $pattern_slug ) {
$theme_pattern = explode( '/', $pattern_slug );
if ( ! isset( $theme_pattern[0] ) || ! isset( $theme_pattern[1] ) ) {
return false;
}
$theme_patterns_meta = self::get_theme_patterns_meta();
return isset( $theme_patterns_meta[ $theme_pattern [0] ][ $theme_pattern [1] ] )
? $theme_patterns_meta[ $theme_pattern [0] ][ $theme_pattern [1] ]
: false;
}

/**
* Retrieve pattern from slug.
*
Expand All @@ -152,14 +196,14 @@ private static function cleanup_wp_grammar( $content ) {
* @return array|boolean
*/
public static function get_pattern_from_slug( $pattern_slug ) {

$block_patterns_registry = \WP_Block_Patterns_Registry::get_instance();
if ( $block_patterns_registry->is_registered( $pattern_slug ) ) {
$pattern = $block_patterns_registry->get_registered( $pattern_slug );
return array(
'title' => $pattern['title'],
'content' => self::cleanup_wp_grammar( $pattern['content'] ),
'name' => $pattern['name'],
'meta' => self::get_meta_from_pattern_slug( $pattern_slug ),
);
}

Expand Down Expand Up @@ -222,11 +266,10 @@ public static function get_theme_step_patterns_from_step( $step, $squash = false
return false;
}

$styles_to_check_for_header_menu = self::get_theme_step_patterns()[ $active_theme ]['styles-for-header-menu-check'];
$pattern_slugs = self::get_theme_step_patterns()[ $active_theme ][ $step ];
$block_patterns_registry = \WP_Block_Patterns_Registry::get_instance();
$block_patterns = array();
$block_patterns_squashed = '';
$pattern_slugs = self::get_theme_step_patterns()[ $active_theme ][ $step ];
$block_patterns_registry = \WP_Block_Patterns_Registry::get_instance();
$block_patterns = array();
$block_patterns_squashed = '';

// fetch the selected header menu slug from DB
$flow_data = \get_option( Options::get_option_name( 'flow' ) );
Expand Down
21 changes: 12 additions & 9 deletions includes/RestApi/SitePagesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace NewfoldLabs\WP\Module\Onboarding\RestApi;

use NewfoldLabs\WP\Module\Onboarding\Permissions;
use NewfoldLabs\WP\Module\Onboarding\Mustache\Mustache;
use NewfoldLabs\WP\Module\Onboarding\Data\Options;
use NewfoldLabs\WP\Module\Onboarding\Data\Patterns;

Expand Down Expand Up @@ -89,7 +88,7 @@ private function set_homepage( $homepage_pattern_slug ) {
return true;
}

$pattern_data = Patterns::get_pattern_from_slug( $homepage_pattern_slug );
$pattern_data = Patterns::get_pattern_from_slug( $homepage_pattern_slug );
if ( ! $pattern_data ) {
return new \WP_Error(
'nfd_onboarding_error',
Expand All @@ -98,14 +97,14 @@ private function set_homepage( $homepage_pattern_slug ) {
);
}

$show_pages_on_front = \get_option( Options::get_option_name( 'show_on_front', false ) );
$show_pages_on_front = \get_option( Options::get_option_name( 'show_on_front', false ) );

// Check if default homepage is posts
// Check if default homepage is posts
if ( 'posts' === $show_pages_on_front ) {
\update_option( Options::get_option_name( 'show_on_front', false ), 'page' );
\update_option( Options::get_option_name( 'show_on_front', false ), 'page' );
}

$post_id = $this->publish_page( 'Homepage', $pattern_data['content'], true );
$post_id = $this->publish_page( 'Homepage', $pattern_data['content'], true, $pattern_data['meta'] );
if ( is_wp_error( $post_id ) ) {
return $post_id;
}
Expand Down Expand Up @@ -137,7 +136,7 @@ private function set_site_pages( $site_pages_pattern_slugs ) {
array( 'status' => 500 )
);
}
$page_data = $this->publish_page( $site_page['title'], $pattern_data['content'] );
$page_data = $this->publish_page( $site_page['title'], $pattern_data['content'], false, $pattern_data['meta'] );
if ( is_wp_error( $page_data ) ) {
return $page_data;
}
Expand All @@ -151,18 +150,22 @@ private function set_site_pages( $site_pages_pattern_slugs ) {
* @param string $title Site Page Title
* @param string $content Pattern Content
* @param boolean $is_template_no_title Check for Title
* @param array $meta The page post_meta.
*
* @return int|\WP_Error
*/
private function publish_page( $title, $content, $is_template_no_title = false ) {

private function publish_page( $title, $content, $is_template_no_title = false, $meta = false ) {
$post = array(
'post_title' => $title,
'post_status' => 'publish',
'post_content' => $content,
'post_type' => 'page',
);

if ( $meta ) {
$post['meta_input'] = $meta;
}

if ( $is_template_no_title ) {
$post['page_template'] = 'no-title';
}
Expand Down

0 comments on commit e467956

Please sign in to comment.