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

altered a few access modifiers and removed unused imports #148

Merged
merged 12 commits into from
Jan 25, 2023
1 change: 1 addition & 0 deletions includes/Data/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ final class Options {
'page_on_front' => 'page_on_front',
'theme_settings' => 'theme_settings',
'flow_preset' => 'flow_preset',
'wpseo_social' => 'wpseo_social',
);

/**
Expand Down
47 changes: 41 additions & 6 deletions includes/Data/Patterns.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,16 @@

use NewfoldLabs\WP\Module\Onboarding\Data\Options;

/**
* Class Patterns
*/
final class Patterns {

/**
* Retrieve Patterns for Theme Step.
*
* @return array
*/
protected static function get_theme_step_patterns() {
return array(
'yith-wonder' => array(
Expand Down Expand Up @@ -96,12 +104,19 @@ protected static function get_theme_step_patterns() {
'shown' => true,
),
),
'site-features' => SiteFeatures::get_site_features()[Data::current_flow()]
'site-features' => SiteFeatures::get_site_features()[ Data::current_flow() ],
),
);
}

public static function cleanup_wp_grammar( $content ) {
/**
* Sanitize the content by cleaning wp_grammar.
*
* @param string $content Data to clean
*
* @return string
*/
private static function cleanup_wp_grammar( $content ) {

// Remove template-part if that exists
$content = preg_replace( '/^<!-- wp:template-part .* \/-->$/m', '', $content );
Expand All @@ -115,6 +130,13 @@ public static function cleanup_wp_grammar( $content ) {
return $content;
}

/**
* Retrieve pattern from slug.
*
* @param array $pattern_slug Pattern Slug Data
*
* @return array|boolean
*/
public static function get_pattern_from_slug( $pattern_slug ) {

$block_patterns_registry = \WP_Block_Patterns_Registry::get_instance();
Expand All @@ -130,6 +152,14 @@ public static function get_pattern_from_slug( $pattern_slug ) {
return false;
}

/**
* Retrieve Theme Step Patterns from chosen Theme in Previous Step
*
* @param string $step Step from which Theme Step Pattern is required
* @param boolean $squash Flag set to retrieve the block pattern
*
* @return array|string
*/
public static function get_theme_step_patterns_from_step( $step, $squash = false ) {
$active_theme = ( \wp_get_theme() )->get( 'TextDomain' );

Expand All @@ -142,7 +172,7 @@ public static function get_theme_step_patterns_from_step( $step, $squash = false
$block_patterns = array();
$block_patterns_squashed = '';
foreach ( array_keys( $pattern_slugs ) as $pattern_slug ) {
if ( $pattern_slugs[ $pattern_slug ]['active'] === true ) {
if ( true === $pattern_slugs[ $pattern_slug ]['active'] ) {
$pattern_name = $active_theme . '/' . $pattern_slug;
if ( $block_patterns_registry->is_registered( $pattern_name ) ) {
$pattern = $block_patterns_registry->get_registered( $pattern_name );
Expand All @@ -166,6 +196,11 @@ public static function get_theme_step_patterns_from_step( $step, $squash = false
return $squash ? $block_patterns_squashed : $block_patterns;
}

/**
* Retrieve Pattern Count.
*
* @return array
*/
public static function get_count_of_patterns() {
$active_theme = ( \wp_get_theme() )->get( 'TextDomain' );
$theme_step_patterns = self::get_theme_step_patterns();
Expand All @@ -176,10 +211,10 @@ public static function get_count_of_patterns() {
$theme_step_count = 0;
$combine_styles = 1;
foreach ( $patterns as $pattern => $pattern_data ) {
if ( isset( $pattern_data['shown'] ) && $pattern_data['shown'] === true ) {
$theme_step_count += 1;
if ( isset( $pattern_data['shown'] ) && true === $pattern_data['shown'] ) {
++$theme_step_count;
}
if ( isset( $pattern_data['combine'] ) && $pattern_data['combine'] === true ) {
if ( isset( $pattern_data['combine'] ) && true === $pattern_data['combine'] ) {
$combine_styles = count( \WP_Theme_JSON_Resolver::get_style_variations() ) + 1;
}
}
Expand Down
29 changes: 22 additions & 7 deletions includes/Data/SiteFeatures.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
<?php

namespace NewfoldLabs\WP\Module\Onboarding\Data;
use \__;
use NewfoldLabs\WP\Module\Onboarding\Data\Options;

/**
* Class SiteFeatures
*/
final class SiteFeatures {

public static function get_site_features()
{

/**
* Retrieve Site Features.
*
* @return array
*/
public static function get_site_features() {
return array(
'wp-setup' => array(),
'wp-setup' => array(),
'ecommerce' => array(
'jetpack' => array(
'slug' => 'jetpack',
Expand Down Expand Up @@ -105,7 +110,12 @@ public static function get_site_features()
);
}

public static function mark_initial_plugins() {
/**
* Based on the flow type initial site features are marked.
*
* @return array
*/
private static function mark_initial_plugins() {
arunshenoy99 marked this conversation as resolved.
Show resolved Hide resolved
$flow = Data::current_flow();
$installed_plugins = Plugins::get_init();

Expand All @@ -121,6 +131,11 @@ public static function mark_initial_plugins() {
return $site_features_marked;
}

/**
* Retrieve marked site features
*
* @return array
*/
public static function get() {
return self::mark_initial_plugins();
}
Expand Down
66 changes: 46 additions & 20 deletions includes/RestApi/FlowController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@
class FlowController {

/**
* @var string
* This is the REST API namespace that will be used for our custom API
*
* @var string
*/
protected $namespace = 'newfold-onboarding/v1';

/**
* @var string
* This is the REST endpoint
*
* @var string
*/
protected $rest_base = '/flow';

Expand Down Expand Up @@ -68,7 +70,8 @@ public function register_routes() {
*/
public function get_onboarding_flow_data( \WP_REST_Request $request ) {
// check if data is available in the database if not then fetch the default dataset
if ( ! ( $result = $this->read_details_from_wp_options() ) ) {
$result = $this->read_details_from_wp_options();
if ( ! $result ) {
$result = Flows::get_data();
$result['createdAt'] = time();
// update default data if flow type is ecommerce
Expand Down Expand Up @@ -101,7 +104,8 @@ public function save_onboarding_flow_data( \WP_REST_Request $request ) {
);
}

if ( ! ( $flow_data = $this->read_details_from_wp_options() ) ) {
$flow_data = $this->read_details_from_wp_options();
if ( ! $flow_data ) {
$flow_data = Flows::get_data();
$flow_data['createdAt'] = time();
// update default data if flow type is ecommerce
Expand All @@ -110,7 +114,8 @@ public function save_onboarding_flow_data( \WP_REST_Request $request ) {
}

foreach ( $params as $key => $param ) {
if ( $value = $this->array_search_key( $key, $flow_data ) === false ) {
$value = $this->array_search_key( $key, $flow_data );
if ( false === $value ) {
return new \WP_Error(
'wrong_param_provided',
"Wrong Parameter Provided : $key",
Expand Down Expand Up @@ -157,16 +162,16 @@ public function save_onboarding_flow_data( \WP_REST_Request $request ) {
}

/**
* check the current flow type and update default data if flowtype is ecommerce.
* Check the current flow type and update default data if flowtype is ecommerce.
*
* @param default flow data.
* @param array $data default blueprint flow data.
*
* @return array
*/
public function update_default_data_for_ecommerce( $data ) {
private function update_default_data_for_ecommerce( $data ) {
// get current flow type
$flow_type = Data::current_flow();
if ( $flow_type == 'ecommerce' ) {
if ( 'ecommerce' === $flow_type ) {
// update default data with ecommerce data
$data['data']['topPriority']['priority1'] = 'selling';
$data['data']['siteType'] = array(
Expand All @@ -177,44 +182,65 @@ public function update_default_data_for_ecommerce( $data ) {
return $data;
}

/*
/**
* Read onboarding flow options from database
*
* @return array
*/
public function read_details_from_wp_options() {
return \get_option( Options::get_option_name( 'flow' ) );
}

/*
* add onboarding flow options
/**
* Add onboarding flow options
*
* @param array $data default blueprint flow data.
*
* @return array
*/
public function save_details_to_wp_options( $data ) {
private function save_details_to_wp_options( $data ) {
return \add_option( Options::get_option_name( 'flow' ), $data );
}

/*
* update onboarding flow options
/**
* Update onboarding flow options
*
* @param array $data default blueprint flow data.
*
* @return array
*/
public function update_wp_options_data_in_database( $data ) {
private function update_wp_options_data_in_database( $data ) {
return \update_option( Options::get_option_name( 'flow' ), $data );
}

/*
* function to search for key in array recursively with case sensitive exact match
/**
* Function to search for key in array recursively with case sensitive exact match
*
* @param array $needle_key specific key in flow data.
* @param array $array WP Options Data.
*
* @return boolean
*/
public function array_search_key( $needle_key, $array ) {
private function array_search_key( $needle_key, $array ) {
foreach ( $array as $key => $value ) {
if ( strcmp( $key, $needle_key ) === 0 ) {
return true;
}
if ( is_array( $value ) ) {
if ( ( $result = $this->array_search_key( $needle_key, $value ) ) !== false ) {
$result = $this->array_search_key( $needle_key, $value );
if ( false !== $result ) {
return $result;
}
}
}
return false;
}

/**
* Flow completion API for child theme generation, verify child theme and publish site pages
*
* @return \WP_REST_Response
*/
public function complete() {
$site_pages_publish_request = new \WP_REST_Request(
'POST',
Expand Down
Loading