Initial commit: Atomaste website
This commit is contained in:
@@ -0,0 +1,206 @@
|
||||
<?php
|
||||
namespace Hostinger\EasyOnboarding\Rest;
|
||||
|
||||
/**
|
||||
* Avoid possibility to get file accessed directly
|
||||
*/
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
die;
|
||||
}
|
||||
|
||||
/**
|
||||
* Class for handling Rest Api Routes
|
||||
*/
|
||||
class Routes {
|
||||
/**
|
||||
* @var WelcomeRoutes
|
||||
*/
|
||||
private WelcomeRoutes $welcome_routes;
|
||||
|
||||
/**
|
||||
* @var StepRoutes
|
||||
*/
|
||||
private StepRoutes $step_routes;
|
||||
|
||||
/**
|
||||
* @var WooRoutes
|
||||
*/
|
||||
private WooRoutes $woo_routes;
|
||||
|
||||
/**
|
||||
* @var TutorialRoutes
|
||||
*/
|
||||
private TutorialRoutes $tutorial_routes;
|
||||
|
||||
/**
|
||||
* @param WelcomeRoutes $welcome_routes
|
||||
* @param StepRoutes $step_routes
|
||||
* @param WooRoutes $woo_routes
|
||||
* @param TutorialRoutes $tutorial_routes
|
||||
*/
|
||||
public function __construct( WelcomeRoutes $welcome_routes, StepRoutes $step_routes, WooRoutes $woo_routes, TutorialRoutes $tutorial_routes ) {
|
||||
$this->welcome_routes = $welcome_routes;
|
||||
$this->step_routes = $step_routes;
|
||||
$this->woo_routes = $woo_routes;
|
||||
$this->tutorial_routes = $tutorial_routes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Init rest routes
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function init(): void {
|
||||
add_action( 'rest_api_init', array( $this, 'register_routes' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function register_routes(): void {
|
||||
$this->register_welcome_routes();
|
||||
$this->register_step_routes();
|
||||
$this->register_tutorial_routes();
|
||||
|
||||
if ( is_plugin_active( 'woocommerce/woocommerce.php' ) ) {
|
||||
$this->register_woo_routes();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param WP_REST_Request $request WordPress rest request.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function permission_check( $request ): bool {
|
||||
// Workaround if Rest Api endpoint cache is enabled.
|
||||
// We don't want to cache these requests.
|
||||
if( has_action('litespeed_control_set_nocache') ) {
|
||||
do_action(
|
||||
'litespeed_control_set_nocache',
|
||||
'Custom Rest API endpoint, not cacheable.'
|
||||
);
|
||||
}
|
||||
|
||||
if ( empty( is_user_logged_in() ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Implement custom capabilities when needed.
|
||||
return current_user_can( 'manage_options' );
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function register_welcome_routes(): void {
|
||||
// Return welcome status.
|
||||
register_rest_route(
|
||||
HOSTINGER_EASY_ONBOARDING_REST_API_BASE,
|
||||
'get-welcome-status',
|
||||
array(
|
||||
'methods' => 'GET',
|
||||
'callback' => array( $this->welcome_routes, 'get_welcome_status' ),
|
||||
'permission_callback' => array( $this, 'permission_check' ),
|
||||
)
|
||||
);
|
||||
|
||||
// Update welcome status.
|
||||
register_rest_route(
|
||||
HOSTINGER_EASY_ONBOARDING_REST_API_BASE,
|
||||
'update-welcome-status',
|
||||
array(
|
||||
'methods' => 'POST',
|
||||
'callback' => array( $this->welcome_routes, 'update_welcome_status' ),
|
||||
'permission_callback' => array( $this, 'permission_check' ),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
private function register_step_routes(): void {
|
||||
// Return steps.
|
||||
register_rest_route(
|
||||
HOSTINGER_EASY_ONBOARDING_REST_API_BASE,
|
||||
'get-steps',
|
||||
array(
|
||||
'methods' => 'GET',
|
||||
'callback' => array( $this->step_routes, 'get_steps' ),
|
||||
'permission_callback' => array( $this, 'permission_check' ),
|
||||
)
|
||||
);
|
||||
|
||||
// Complete step.
|
||||
register_rest_route(
|
||||
HOSTINGER_EASY_ONBOARDING_REST_API_BASE,
|
||||
'complete-step',
|
||||
array(
|
||||
'methods' => 'POST',
|
||||
'callback' => array( $this->step_routes, 'complete_step' ),
|
||||
'permission_callback' => array( $this, 'permission_check' ),
|
||||
)
|
||||
);
|
||||
|
||||
// Toggle list step.
|
||||
register_rest_route(
|
||||
HOSTINGER_EASY_ONBOARDING_REST_API_BASE,
|
||||
'toggle-list-visibility',
|
||||
array(
|
||||
'methods' => 'POST',
|
||||
'callback' => array( $this->step_routes, 'toggle_list_visibility' ),
|
||||
'permission_callback' => array( $this, 'permission_check' ),
|
||||
)
|
||||
);
|
||||
|
||||
// Activate plugin
|
||||
register_rest_route(
|
||||
HOSTINGER_EASY_ONBOARDING_REST_API_BASE, 'activate-plugin', [
|
||||
'methods' => 'POST',
|
||||
'callback' => [$this->step_routes, 'activate_plugin'],
|
||||
'permission_callback' => [$this, 'permission_check'],
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
private function register_woo_routes(): void {
|
||||
// Woo Setup
|
||||
register_rest_route(
|
||||
HOSTINGER_EASY_ONBOARDING_REST_API_BASE, 'woo-setup', [
|
||||
'methods' => 'POST',
|
||||
'callback' => [$this->woo_routes, 'woo_setup'],
|
||||
'permission_callback' => [$this, 'permission_check'],
|
||||
]
|
||||
);
|
||||
|
||||
// Plugins
|
||||
register_rest_route(
|
||||
HOSTINGER_EASY_ONBOARDING_REST_API_BASE, 'get-plugins', [
|
||||
'methods' => 'GET',
|
||||
'callback' => [$this->woo_routes, 'get_plugins'],
|
||||
'permission_callback' => [$this, 'permission_check'],
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
private function register_tutorial_routes(): void {
|
||||
// Return steps.
|
||||
register_rest_route(
|
||||
HOSTINGER_EASY_ONBOARDING_REST_API_BASE,
|
||||
'get-tutorials',
|
||||
array(
|
||||
'methods' => 'GET',
|
||||
'callback' => array( $this->tutorial_routes, 'get_tutorials' ),
|
||||
'permission_callback' => array( $this, 'permission_check' ),
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,186 @@
|
||||
<?php
|
||||
namespace Hostinger\EasyOnboarding\Rest;
|
||||
|
||||
use Hostinger\EasyOnboarding\Admin\Onboarding\Onboarding;
|
||||
use Hostinger\EasyOnboarding\Helper;
|
||||
|
||||
/**
|
||||
* Avoid possibility to get file accessed directly
|
||||
*/
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
die;
|
||||
}
|
||||
|
||||
/**
|
||||
* Class for handling Settings Rest API
|
||||
*/
|
||||
class StepRoutes {
|
||||
public const LIST_VISIBILITY_OPTION = 'hostinger_onboarding_list_visibility';
|
||||
/**
|
||||
* @param \WP_REST_Request $request
|
||||
*
|
||||
* @return \WP_REST_Response
|
||||
*/
|
||||
public function get_steps( \WP_REST_Request $request ): \WP_REST_Response {
|
||||
$parameters = $request->get_params();
|
||||
|
||||
$locale = !empty($parameters['locale']) ? sanitize_text_field($parameters['locale']) : '';
|
||||
$available_languages = get_available_languages();
|
||||
|
||||
if (!empty($locale) && in_array($locale, $available_languages)) {
|
||||
switch_to_locale($locale);
|
||||
}
|
||||
|
||||
$onboarding = new Onboarding();
|
||||
$onboarding->init();
|
||||
|
||||
$data = array(
|
||||
'data' => array(
|
||||
'steps' => $onboarding->get_step_categories(),
|
||||
)
|
||||
);
|
||||
|
||||
$response = new \WP_REST_Response( $data );
|
||||
|
||||
$response->set_headers( array( 'Cache-Control' => 'no-cache' ) );
|
||||
|
||||
$response->set_status( \WP_Http::OK );
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
|
||||
public function complete_step( \WP_REST_Request $request ): \WP_Error|\WP_REST_Response
|
||||
{
|
||||
$parameters = $request->get_params();
|
||||
|
||||
$errors = array();
|
||||
|
||||
if ( empty( $parameters['step_category_id'] ) ) {
|
||||
/* translators: %s field name that is missing */
|
||||
$errors['step_category_id'] = sprintf( __( '%s missing or empty', 'hostinger-easy-onboarding' ), 'step category id');
|
||||
}
|
||||
|
||||
if ( empty( $parameters['step_id'] ) ) {
|
||||
$errors['step_id'] = sprintf( __( '%s missing or empty', 'hostinger-easy-onboarding' ), 'step category id') ;
|
||||
}
|
||||
|
||||
if ( ! empty( $errors ) ) {
|
||||
return new \WP_Error(
|
||||
'data_invalid',
|
||||
__( 'Sorry, there are validation errors.', 'hostinger-easy-onboarding' ),
|
||||
array(
|
||||
'status' => \WP_Http::BAD_REQUEST,
|
||||
'errors' => $errors,
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
$step_category_id = sanitize_text_field( $parameters['step_category_id'] );
|
||||
$step_id = sanitize_text_field( $parameters['step_id'] );
|
||||
|
||||
$onboarding = new Onboarding();
|
||||
$onboarding->init();
|
||||
|
||||
$validate_step = $onboarding->validate_step( $step_category_id, $step_id );
|
||||
|
||||
if(empty($validate_step)) {
|
||||
return new \WP_Error(
|
||||
'data_invalid',
|
||||
__( 'Step category and/or step does not exist.', 'hostinger-easy-onboarding' ),
|
||||
array(
|
||||
'status' => \WP_Http::BAD_REQUEST
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
$data = array(
|
||||
'data' => array(
|
||||
'saved' => $onboarding->complete_step( $step_category_id, $step_id )
|
||||
)
|
||||
);
|
||||
|
||||
if ( has_action( 'litespeed_purge_all' ) ) {
|
||||
do_action( 'litespeed_purge_all' );
|
||||
}
|
||||
|
||||
$response = new \WP_REST_Response( $data );
|
||||
|
||||
$response->set_headers( array( 'Cache-Control' => 'no-cache' ) );
|
||||
|
||||
$response->set_status( \WP_Http::OK );
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
function toggle_list_visibility( \WP_REST_Request $request ) {
|
||||
$current_state = get_option( self::LIST_VISIBILITY_OPTION, 1 );
|
||||
|
||||
$new_state = !(bool)$current_state;
|
||||
|
||||
$update = update_option( self::LIST_VISIBILITY_OPTION, (int)$new_state );
|
||||
|
||||
return new \WP_REST_Response( array(
|
||||
'status' => $update,
|
||||
'new_state' => $new_state
|
||||
), 200 );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \WP_REST_Request $request
|
||||
*
|
||||
* @return \WP_REST_Response|\WP_Error
|
||||
*/
|
||||
public function activate_plugin( \WP_REST_Request $request ): \WP_REST_Response|\WP_Error {
|
||||
$parameters = $request->get_params();
|
||||
|
||||
$plugin = !empty($parameters['plugin']) ? sanitize_text_field($parameters['plugin']) : '';
|
||||
|
||||
$errors = array();
|
||||
|
||||
if(empty($plugin)) {
|
||||
$errors['plugin'] = sprintf( __( '%s missing or empty', 'hostinger-easy-onboarding' ), 'plugin' );
|
||||
}
|
||||
|
||||
$helper = new Helper();
|
||||
$plugin_path = $helper->get_plugin_main_file( $plugin );
|
||||
|
||||
if ( is_plugin_active( $plugin_path ) ) {
|
||||
/* translators: %s plugin slug */
|
||||
$errors['plugin'] = sprintf( __( '%s is already active', 'hostinger-easy-onboarding' ), 'plugin' );
|
||||
}
|
||||
|
||||
if ( ! empty( $errors ) ) {
|
||||
return new \WP_Error(
|
||||
'data_invalid',
|
||||
__( 'Sorry, there are validation errors.', 'hostinger-easy-onboarding' ),
|
||||
array(
|
||||
'status' => \WP_Http::BAD_REQUEST,
|
||||
'errors' => $errors,
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
$activated = activate_plugin( $plugin_path );
|
||||
|
||||
if ( is_wp_error( $activated ) ) {
|
||||
return new \WP_Error(
|
||||
'data_invalid',
|
||||
__( 'Sorry, there are activation errors.', 'hostinger-easy-onboarding' ),
|
||||
array(
|
||||
'status' => \WP_Http::BAD_REQUEST,
|
||||
'errors' => $activated->get_error_message(),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
$response = new \WP_REST_Response( array( 'data' => '' ) );
|
||||
|
||||
$response->set_headers( array( 'Cache-Control' => 'no-cache' ) );
|
||||
|
||||
$response->set_status( \WP_Http::OK );
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,156 @@
|
||||
<?php
|
||||
namespace Hostinger\EasyOnboarding\Rest;
|
||||
|
||||
/**
|
||||
* Avoid possibility to get file accessed directly
|
||||
*/
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
die;
|
||||
}
|
||||
|
||||
class TutorialRoutes {
|
||||
public function get_tutorials( \WP_REST_Request $request ): \WP_REST_Response {
|
||||
$parameters = $request->get_params();
|
||||
|
||||
$locale = sanitize_text_field( $parameters['locale'] );
|
||||
|
||||
$user_locale = !empty( $locale ) ? substr( $locale, 0, 2) : 'en';
|
||||
|
||||
$tutorials = array(
|
||||
'en' => array(
|
||||
array(
|
||||
'id' => 'F_53_baJe6Q',
|
||||
'title' => 'How to Make a Website (2024): Simple, Quick, & Easy Tutorial',
|
||||
'duration' => '17:47',
|
||||
),
|
||||
array(
|
||||
'id' => 'SU_DOsu9Llk',
|
||||
'title' => 'How to EASILY Manage Google Tools with Google Site Kit - Beginners Guide 2024',
|
||||
'duration' => '12:28',
|
||||
),
|
||||
array(
|
||||
'id' => 'YK-XO7iLyGQ',
|
||||
'title' => 'How to Import Images Into WordPress Website',
|
||||
'duration' => '1:44',
|
||||
),
|
||||
array(
|
||||
'id' => 'WHXtmEppbn8',
|
||||
'title' => 'How to Edit the Footer in WordPress',
|
||||
'duration' => '6:17',
|
||||
),
|
||||
),
|
||||
'pt' => array(
|
||||
array(
|
||||
'id' => 'Ck15HW4koWE',
|
||||
'title' => 'Como Alterar a sua Logo no WordPress (Rápido e Prático)',
|
||||
'duration' => '4:28',
|
||||
),
|
||||
array(
|
||||
'id' => 'OJH713cx-u4',
|
||||
'title' => 'Como Personalizar um Tema do WordPress',
|
||||
'duration' => '13:42',
|
||||
),
|
||||
array(
|
||||
'id' => 'X_04utuq750',
|
||||
'title' => 'Como Editar o Menu dos Temas do WordPress',
|
||||
'duration' => '4:53',
|
||||
),
|
||||
array(
|
||||
'id' => 'cMKPatPvSKk',
|
||||
'title' => 'Como Criar Categorias no WordPress',
|
||||
'duration' => '6:04',
|
||||
),
|
||||
),
|
||||
'es' => array(
|
||||
array(
|
||||
'id' => 'FKp0dvhEN8o',
|
||||
'title' => 'Cómo Personalizar WordPress (2023)',
|
||||
'duration' => '9:02',
|
||||
),
|
||||
array(
|
||||
'id' => '1tvYSsRSgNc',
|
||||
'title' => 'Cómo Crear una Galería de Fotos en WordPress | Fácil y Gratis',
|
||||
'duration' => '5:48',
|
||||
),
|
||||
array(
|
||||
'id' => 'A-yuq3g1KVs',
|
||||
'title' => 'Como Instalar Plugins y Temas en WordPress',
|
||||
'duration' => '4:54',
|
||||
),
|
||||
array(
|
||||
'id' => '_8Z0C6Os1CQ',
|
||||
'title' => 'Cómo Crear un Menú en WordPress (en Menos de 5 minutos)',
|
||||
'duration' => '4:52',
|
||||
),
|
||||
),
|
||||
'fr' => array(
|
||||
array(
|
||||
'id' => 'zpW8jliv45E',
|
||||
'title' => 'Hostinger WordPress : Le Guide Complet pour utiliser WordPress sur Hostinger',
|
||||
'duration' => '8:11',
|
||||
),
|
||||
array(
|
||||
'id' => 'X7ZA9pteqqQ',
|
||||
'title' => 'TUTO WORDPRESS (Débutant) : Créer un site WordPress pour les Nuls',
|
||||
'duration' => '12:56',
|
||||
),
|
||||
array(
|
||||
'id' => 'JIHy3Y6ek_s',
|
||||
'title' => 'Tuto WordPress Débutant (Hostinger hPanel) - Créer un Site par IA',
|
||||
'duration' => '7:21',
|
||||
),
|
||||
array(
|
||||
'id' => 'Te3fM7VuQKg',
|
||||
'title' => 'Installer un Thème WordPress (2023) | Rapide et Facile',
|
||||
'duration' => '2:58',
|
||||
),
|
||||
array(
|
||||
'id' => '2rPq1CiogDk',
|
||||
'title' => 'Google Analytics sur WordPress FACILEMENT avec Google Site Kit : Guide Complet (2023)',
|
||||
'duration' => '7:19',
|
||||
),
|
||||
),
|
||||
'hi' => array(
|
||||
array(
|
||||
'id' => '4wGytQfbmm4',
|
||||
'title' => 'How to Build a Website FAST Using AI in Just 10 Minutes',
|
||||
'duration' => '8:32',
|
||||
),
|
||||
array(
|
||||
'id' => 'AT73ExGMuVc',
|
||||
'title' => 'How to Edit Footer in WordPress in Hindi | Hostinger India',
|
||||
'duration' => '3:48',
|
||||
),
|
||||
array(
|
||||
'id' => 'OIGsBGIaZqM',
|
||||
'title' => 'How to Create a Menu in WordPress in Hindi | Hostinger India',
|
||||
'duration' => '2:38',
|
||||
),
|
||||
array(
|
||||
'id' => 'WFBoHv0xJ60',
|
||||
'title' => 'How to Install WordPress Themes | Hostinger India',
|
||||
'duration' => '2:52',
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
if ( empty( $tutorials[$user_locale] ) ) {
|
||||
$user_locale = 'en';
|
||||
}
|
||||
|
||||
$data = array(
|
||||
'data' => array(
|
||||
'tutorials' => $tutorials[$user_locale],
|
||||
)
|
||||
);
|
||||
|
||||
$response = new \WP_REST_Response( $data );
|
||||
|
||||
$response->set_headers( array( 'Cache-Control' => 'no-cache' ) );
|
||||
|
||||
$response->set_status( \WP_Http::OK );
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
namespace Hostinger\EasyOnboarding\Rest;
|
||||
|
||||
use Hostinger\EasyOnboarding\Admin\Onboarding\WelcomeCards;
|
||||
|
||||
/**
|
||||
* Avoid possibility to get file accessed directly
|
||||
*/
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
die;
|
||||
}
|
||||
|
||||
/**
|
||||
* Class for handling Settings Rest API
|
||||
*/
|
||||
class WelcomeRoutes {
|
||||
/**
|
||||
* Return welcome status
|
||||
*
|
||||
* @param WP_REST_Request $request WordPress rest request.
|
||||
*
|
||||
* @return \WP_REST_Response
|
||||
*/
|
||||
public function get_welcome_status( \WP_REST_Request $request ): \WP_REST_Response {
|
||||
$parameters = $request->get_params();
|
||||
|
||||
$locale = !empty($parameters['locale']) ? sanitize_text_field($parameters['locale']) : '';
|
||||
$available_languages = get_available_languages();
|
||||
|
||||
if (!empty($locale) && in_array($locale, $available_languages)) {
|
||||
switch_to_locale($locale);
|
||||
}
|
||||
|
||||
$welcome_card = new WelcomeCards();
|
||||
$welcome_cards = $welcome_card->get_welcome_cards();
|
||||
|
||||
$data = array(
|
||||
'data' => array(
|
||||
'welcome_cards' => $welcome_cards,
|
||||
'welcome_choice_done' => get_option( 'hostinger_onboarding_choice_done', false )
|
||||
)
|
||||
);
|
||||
|
||||
$response = new \WP_REST_Response( $data );
|
||||
|
||||
$response->set_headers( array( 'Cache-Control' => 'no-cache' ) );
|
||||
|
||||
$response->set_status( \WP_Http::OK );
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \WP_REST_Request $request
|
||||
*
|
||||
* @return \WP_Error|\WP_REST_Response
|
||||
*/
|
||||
public function update_welcome_status( \WP_REST_Request $request )
|
||||
{
|
||||
$parameters = $request->get_params();
|
||||
|
||||
if( empty( $parameters['choice'] ) ) {
|
||||
return new \WP_Error(
|
||||
'data_invalid',
|
||||
__( 'Choice parameter missing or empty', 'hostinger-easy-onboarding' ),
|
||||
array(
|
||||
'status' => \WP_Http::BAD_REQUEST
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
$choice = sanitize_text_field( $parameters['choice'] );
|
||||
|
||||
update_option( 'hostinger_onboarding_choice_done', $choice );
|
||||
|
||||
if ( has_action( 'litespeed_purge_all' ) ) {
|
||||
do_action( 'litespeed_purge_all' );
|
||||
}
|
||||
|
||||
$response = new \WP_REST_Response( array( 'data' => array() ) );
|
||||
|
||||
$response->set_headers(array('Cache-Control' => 'no-cache'));
|
||||
|
||||
$response->set_status( \WP_Http::OK );
|
||||
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,201 @@
|
||||
<?php
|
||||
namespace Hostinger\EasyOnboarding\Rest;
|
||||
|
||||
use Hostinger\EasyOnboarding\Admin\Onboarding\Onboarding;
|
||||
use Hostinger\EasyOnboarding\Admin\Onboarding\PluginManager;
|
||||
use Hostinger\EasyOnboarding\Helper;
|
||||
|
||||
/**
|
||||
* Avoid possibility to get file accessed directly
|
||||
*/
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
die;
|
||||
}
|
||||
|
||||
/**
|
||||
* Class for handling WooCommerce related routes
|
||||
*/
|
||||
class WooRoutes {
|
||||
/**
|
||||
* @param \WP_REST_Request $request
|
||||
*
|
||||
* @return \WP_REST_Response
|
||||
*/
|
||||
public function get_plugins( \WP_REST_Request $request ): \WP_REST_Response|\WP_Error {
|
||||
$parameters = $request->get_params();
|
||||
|
||||
$locale = !empty($parameters['locale']) ? sanitize_text_field($parameters['locale']) : '';
|
||||
$available_languages = get_available_languages();
|
||||
|
||||
if (!empty($locale) && in_array($locale, $available_languages)) {
|
||||
switch_to_locale($locale);
|
||||
}
|
||||
|
||||
$parameters = $request->get_params();
|
||||
|
||||
$type = !empty($parameters['type']) ? $this->filter_allowed_types($parameters['type']) : '';
|
||||
|
||||
$errors = array();
|
||||
|
||||
if(empty($type)) {
|
||||
$errors['type'] = sprintf( __( '%s missing or empty', 'hostinger-easy-onboarding' ), 'type' );
|
||||
}
|
||||
|
||||
$locale = get_option( 'woocommerce_default_country', false );
|
||||
|
||||
if(empty($locale)) {
|
||||
$errors['locale'] = __( 'Shop locale is empty, please setup store first', 'hostinger-easy-onboarding' );
|
||||
}
|
||||
|
||||
if ( ! empty( $errors ) ) {
|
||||
return new \WP_Error(
|
||||
'data_invalid',
|
||||
__( 'Sorry, there are validation errors.', 'hostinger-easy-onboarding' ),
|
||||
array(
|
||||
'status' => \WP_Http::BAD_REQUEST,
|
||||
'errors' => $errors,
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
$plugin_manager = new PluginManager();
|
||||
|
||||
$data = array(
|
||||
'plugins' => $plugin_manager->get_plugins_by_criteria( $type, $locale ),
|
||||
'locale' => get_option('woocommerce_default_country', '')
|
||||
);
|
||||
|
||||
$response = new \WP_REST_Response( array( 'data' => $data ) );
|
||||
|
||||
$response->set_headers( array( 'Cache-Control' => 'no-cache' ) );
|
||||
|
||||
$response->set_status( \WP_Http::OK );
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \WP_REST_Request $request
|
||||
*
|
||||
* @return \WP_Error|\WP_REST_Response
|
||||
*/
|
||||
public function woo_setup( \WP_REST_Request $request )
|
||||
{
|
||||
$parameters = $request->get_params();
|
||||
|
||||
$fields = array(
|
||||
'store_name',
|
||||
'industry',
|
||||
'store_location',
|
||||
'business_email',
|
||||
'is_agree_marketing',
|
||||
);
|
||||
|
||||
$boolean_fields = array(
|
||||
'is_agree_marketing'
|
||||
);
|
||||
|
||||
$errors = array();
|
||||
|
||||
foreach( $fields as $field ) {
|
||||
|
||||
$formatted_field = str_replace( '_', ' ', $field );
|
||||
|
||||
// Boolean field have bit different validation
|
||||
if ( in_array( $field, $boolean_fields ) ) {
|
||||
$field_is_valid = isset( $parameters[$field] );
|
||||
} else {
|
||||
$field_is_valid = !empty( $parameters[$field] );
|
||||
}
|
||||
|
||||
if ( !$field_is_valid ) {
|
||||
$errors[$field] = sprintf( __( '%s missing or empty', 'hostinger-easy-onboarding' ), $formatted_field );
|
||||
} else {
|
||||
$parameters[$field] = sanitize_text_field( $parameters[$field] );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
$locale_info = include WC()->plugin_path() . DIRECTORY_SEPARATOR . 'i18n' . DIRECTORY_SEPARATOR . 'locale-info.php';
|
||||
|
||||
if(str_contains($parameters['store_location'], ':')) {
|
||||
$store_location = explode( ':', $parameters['store_location'] );
|
||||
$store_location = $store_location[0];
|
||||
} else {
|
||||
$store_location = $parameters['store_location'];
|
||||
}
|
||||
|
||||
if ( empty( $locale_info[$store_location] ) ) {
|
||||
$errors['store_location'] = __( 'Store location locale not found', 'hostinger-easy-onboarding' );
|
||||
}
|
||||
|
||||
if ( ! empty( $errors ) ) {
|
||||
return new \WP_Error(
|
||||
'data_invalid',
|
||||
__( 'Sorry, there are validation errors.', 'hostinger-easy-onboarding' ),
|
||||
array(
|
||||
'status' => \WP_Http::BAD_REQUEST,
|
||||
'errors' => $errors,
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
$store_locale = $locale_info[$store_location];
|
||||
|
||||
// Default WooCommerce values.
|
||||
update_option('woocommerce_default_country', $parameters['store_location'], true);
|
||||
update_option('woocommerce_allowed_countries', 'all', true);
|
||||
update_option('woocommerce_all_except_countries', [], true);
|
||||
update_option('woocommerce_specific_allowed_countries', [], true);
|
||||
update_option('woocommerce_specific_ship_to_countries', [], true);
|
||||
update_option('woocommerce_default_customer_address', 'base', true);
|
||||
update_option('woocommerce_calc_taxes', 'no', true);
|
||||
update_option('woocommerce_enable_coupons', 'yes', true);
|
||||
update_option('woocommerce_calc_discounts_sequentially', 'no', true);
|
||||
update_option('woocommerce_currency', $store_locale['currency_code'], true);
|
||||
update_option('woocommerce_currency_pos', $store_locale['currency_pos'], true);
|
||||
update_option('woocommerce_price_thousand_sep', $store_locale['thousand_sep'], true);
|
||||
update_option('woocommerce_price_decimal_sep', $store_locale['decimal_sep'], true);
|
||||
update_option('woocommerce_price_num_decimals', $store_locale['num_decimals'], true);
|
||||
update_option('woocommerce_weight_unit', $store_locale['weight_unit'], true);
|
||||
update_option('woocommerce_dimension_unit', $store_locale['dimension_unit'], true);
|
||||
|
||||
$onboarding_profile = array();
|
||||
$onboarding_profile['is_store_country_set'] = true;
|
||||
$onboarding_profile['industry'] = array( $parameters['industry'] );
|
||||
$onboarding_profile['is_agree_marketing'] = $parameters['is_agree_marketing'];
|
||||
$onboarding_profile['store_email'] = $parameters['business_email'];
|
||||
$onboarding_profile['completed'] = true;
|
||||
$onboarding_profile['is_plugins_page_skipped'] = true;
|
||||
|
||||
update_option('woocommerce_onboarding_profile', $onboarding_profile, true);
|
||||
|
||||
$onboarding = new Onboarding();
|
||||
$onboarding->init();
|
||||
|
||||
$onboarding->complete_step( Onboarding::HOSTINGER_EASY_ONBOARDING_STORE_STEP_CATEGORY_ID, 'setup_store' );
|
||||
|
||||
if ( has_action( 'litespeed_purge_all' ) ) {
|
||||
do_action( 'litespeed_purge_all' );
|
||||
}
|
||||
|
||||
$response = new \WP_REST_Response( array( ) );
|
||||
|
||||
$response->set_headers(array('Cache-Control' => 'no-cache'));
|
||||
|
||||
$response->set_status( \WP_Http::OK );
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $type
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function filter_allowed_types( string $type ) {
|
||||
$allowed_types = array( 'shipping', 'payment' );
|
||||
|
||||
return in_array( $type, $allowed_types ) ? $type : '';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user