File manager - Edit - /home/verseaumee/middletvnew26/wp-content/plugins/shopbuild/includes/class-pure-wc-shopbuild-setup.php
Back
<?php use Automattic\WooCommerce\Admin\API\OnboardingTasks; if (!defined('ABSPATH')) { exit; } if (!class_exists('StoreBuild_Setup')) { class StoreBuild_Setup { public function __construct() { // Admin actions add_action('admin_init', array($this, 'maybe_redirect_to_setup')); add_action('admin_menu', array($this, 'register_setup_page')); add_action('admin_head', array($this, 'hide_admin_ui_for_setup_page')); add_filter('admin_body_class', array($this, 'add_fullscreen_body_class')); // Register REST API Route for setup add_action('rest_api_init', array($this, 'register_setup_rest_route')); } // Method to register the REST API route public function register_setup_rest_route() { register_rest_route('storebuild/v1', '/setup/', [ 'methods' => 'POST', 'callback' => array($this, 'storebuild_handle_setup'), 'permission_callback' => function () { return current_user_can('manage_options'); }, ]); } // Handle the setup process when the REST API is called public static function storebuild_handle_setup_2($request) { require_once ABSPATH . 'wp-admin/includes/plugin.php'; require_once ABSPATH . 'wp-admin/includes/file.php'; require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; $params = $request->get_json_params(); $homepage = sanitize_text_field($params['homepage']); $modules = array_map('sanitize_text_field', $params['modules']); $templates = array_map('sanitize_text_field', $params['templates']); $response = []; // 1. Install required plugins $plugins = [ 'woocommerce' => 'woocommerce/woocommerce.php', 'elementor' => 'elementor/elementor.php', ]; foreach ($plugins as $slug => $main_file) { if (!is_plugin_active($main_file)) { $api = plugins_api('plugin_information', ['slug' => $slug, 'fields' => ['sections' => false]]); if (is_wp_error($api)) continue; $upgrader = new Plugin_Upgrader(new WP_Ajax_Upgrader_Skin()); $upgrader->install($api->download_link); activate_plugin($main_file); $response[] = "Installed and activated $slug."; } else { $response[] = "$slug already installed."; } } // 2. Import products if empty if (!storebuild_has_existing_products()) { $csv_file = STOREBUILD_PATH . 'sample-data/sample_products.csv'; if (file_exists($csv_file)) { // include_once STOREBUILD_PATH . 'sample-data/import/import-products.php'; // you should create this importer // $import_result = storebuild_import_products_from_csv($csv_file); $response[] = "Demo products imported."; } else { $response[] = "CSV file for demo products not found."; } } else { $response[] = "Products already exist. Skipped demo import."; } // 3. Create homepage and set as front page if (!empty($homepage)) { add_action('elementor/init', function() use ($homepage) { $home_title = sanitize_text_field($homepage); $home_title = explode('/', $home_title); $home_title = end($home_title); $home_query = new WP_Query([ 'post_type' => 'page', 'post_status' => 'publish', 'title' => $home_title, 'posts_per_page' => 1, ]); if ($home_query->have_posts()) { $page_id = $home_query->posts[0]->ID; $response[] = "Homepage '$home_title' already exists."; } else { $page_id = wp_insert_post([ 'post_title' => $home_title, 'post_status' => 'publish', 'post_type' => 'page', ]); // Import Elementor data $json_file = "https://storebuild.shop/wp-json/storebuild/v2/template/?param={$homepage}"; // Use wp_remote_get to fetch the content from the remote JSON URL $response_data = wp_remote_get($json_file); if (!is_wp_error($response_data) && wp_remote_retrieve_response_code($response_data) === 200) { $elementor_data = wp_remote_retrieve_body($response_data); $home_content = json_decode($elementor_data, true); $home_content = $home_content['content'] ?? []; $content = \StoreBuild\StoreBuild_Template::get_instance()->import_export_content($home_content); update_post_meta($page_id, '_wp_page_template', 'elementor_header_footer'); update_post_meta($page_id, '_elementor_edit_mode', 'builder'); update_post_meta($page_id, '_elementor_data', $content); $response[] = "Elementor homepage content imported."; } else { $response[] = "Elementor JSON file not found or request failed for '{$homepage}'."; } $response[] = "Homepage '$home_title' created."; } update_option('page_on_front', $page_id); update_option('show_on_front', 'page'); }); } // 4. Setup Modules if (!empty($modules)) { $individual_options = []; $storebuild_modules = []; foreach ($modules as $module) { switch ($module) { case '_storebuild_sale_notification': $individual_options['_storebuild_sale_notification'] = [ 'storebuild_enable_sale_notification' => true, ]; $response[] = "Sale Notification module setup."; break; case '_storebuild_invoice_settings': $individual_options['_storebuild_invoice_settings'] = [ 'enable' => true, ]; $response[] = "Invoice Settings module setup."; break; case '_storebuild_quickview_settings': $individual_options['_storebuild_quickview_settings'] = [ 'enable' => true, ]; $response[] = "Quickview module setup."; break; case '_storebuild_backorder_settings': $individual_options['_storebuild_backorder_settings'] = [ 'enable' => true, ]; $response[] = "Backorder module setup."; break; case 'storebuild_add_to_cart_notification': case 'storebuild_sale_badge': case 'storebuild_flash_sale_countdown': case 'storebuild_wishlist': case 'storebuild_compare': $storebuild_modules[$module] = true; $label = ucwords(str_replace('_', ' ', str_replace('storebuild_', '', $module))); $response[] = "$label module setup."; break; default: $response[] = "Unknown module: $module"; } } // Update individual options foreach ($individual_options as $key => $value) { update_option($key, $value); } // Update _storebuild_modules once if (!empty($storebuild_modules)) { // Merge with existing settings to preserve previous modules $existing = get_option('_storebuild_modules', []); $merged = array_merge($existing, $storebuild_modules); update_option('_storebuild_modules', $merged); } } else { $response[] = "No modules selected."; } // 5. Setup WooCommerce templates if (!empty($templates)) { add_action('elementor/init', function() use ($templates) { $template_content_json_url = 'https://storebuild.shop/templates/contents/'; foreach ($templates as $template) { switch ($template) { case 'shop-top-filter': $template_content = file_get_contents($template_content_json_url . 'shop/shop-top-filter.json'); storebuild_setup_import_data('Shop', $template_content, 'shop', $template); $response[] = "Shop template setup."; break; case 'single-default': $template_content = file_get_contents($template_content_json_url . 'single/single-default.json'); storebuild_setup_import_data('Shop Details', $template_content, 'single', $template); $response[] = "Single template setup."; break; case 'cart-1': $template_content = file_get_contents($template_content_json_url . 'cart/cart-1.json'); storebuild_setup_import_data('Cart', $template_content, 'cart', $template); $response[] = "Cart template setup."; break; case 'checkout-1': $template_content = file_get_contents($template_content_json_url . 'checkout/checkout-1.json'); storebuild_setup_import_data('Checkout', $template_content, 'checkout', $template); $response[] = "Checkout template setup."; break; default: $response[] = "Unknown template: $template"; } } }); } else { $response[] = "No templates selected."; } // This is a simplified placeholder — you can improve this using custom Elementor templates or assign templates via hooks $response[] = "Custom WooCommerce templates applied (placeholder)."; // 5. Done $response[] = "Setup complete."; return new WP_REST_Response([ 'success' => true, 'messages' => $response, ], 200); } public static function storebuild_handle_setup($request) { $step = $request->get_param('step'); $response = []; // return new WP_REST_Response($step, 200); switch ($step) { case 'check_plugins': // Initial Stage: Start $response = ['success' => true, 'message' => 'Setup started.', 'next_stage' => 'install_plugins']; break; case 'install_plugins': // Stage 1: Install Plugins $response = self::install_plugins(); break; case 'check_products': // check is woocommerce products is empty if (!storebuild_has_existing_products()) { $response = ['success' => true, 'message' => 'No products in this store.', 'next_stage' => 'import_products']; }else{ $response = ['success' => true, 'message' => 'Products already exist. Skipped demo import.', 'next_stage' => 'create_homepage']; } break; case 'import_products': // Stage 2: Import Products $response = self::import_products(); break; case 'create_homepage': // Stage 3: Create Homepage $response = self::create_homepage($request->get_param('homepage') ?? ''); break; case 'apply_templates': // Stage 5: Setup Templates $response = self::setup_templates($request->get_param('templates') ?? []); break; case 'activate_modules': // Stage 4: Setup Modules $response = self::setup_modules($request->get_param('modules') ?? []); break; case 'complete': // Final Stage: Complete flush_rewrite_rules(); $response = ['success' => true, 'message' => 'Setup complete.']; break; default: $response = ['success' => false, 'message' => 'Unknown stage.']; break; } return new WP_REST_Response($response, 200); } public static function install_plugins() { // Install WooCommerce, Elementor, etc. // Logic for installing plugins require_once ABSPATH . 'wp-admin/includes/plugin.php'; require_once ABSPATH . 'wp-admin/includes/file.php'; require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; $response = []; $plugins = [ 'woocommerce' => 'woocommerce/woocommerce.php', 'elementor' => 'elementor/elementor.php', ]; foreach ($plugins as $slug => $main_file) { if (!is_plugin_active($main_file)) { $api = plugins_api('plugin_information', ['slug' => $slug, 'fields' => ['sections' => false]]); if (is_wp_error($api)) continue; $upgrader = new Plugin_Upgrader(new WP_Ajax_Upgrader_Skin()); $upgrader->install($api->download_link); activate_plugin($main_file); $response[] = "Installed and activated $slug."; } else { $response[] = "$slug already installed."; } } return ['success' => true, 'message' => $response, 'next_stage' => 'create_homepage']; } public static function import_products() { // Import demo products // Logic for importing products $response = []; $csv_file = STOREBUILD_PATH . 'sample-data/sample_products.csv'; $import_result = storebuild_import_woocommerce_products_from_csv($csv_file); $response[] = $import_result; return ['success' => true, 'message' => $response, 'next_stage' => 'activate_modules']; } public static function create_homepage( $homepage ) { // Create homepage // Logic for creating homepage $response = []; if (!empty($homepage)) { $home_title = sanitize_text_field($homepage); $home_title = explode('/', $home_title); $home_title = end($home_title); $home_query = new WP_Query([ 'post_type' => 'page', 'post_status' => 'publish', 'title' => $home_title, 'posts_per_page' => 1, ]); if ($home_query->have_posts()) { $page_id = $home_query->posts[0]->ID; $response[] = "Homepage '$home_title' already exists."; } else { $page_id = wp_insert_post([ 'post_title' => $home_title, 'post_status' => 'publish', 'post_type' => 'page', ]); // Import Elementor data $json_file = "https://storebuild.shop/wp-json/storebuild/v2/template/?param={$homepage}"; // Use wp_remote_get to fetch the content from the remote JSON URL $response_data = wp_remote_get($json_file); if (!is_wp_error($response_data) && wp_remote_retrieve_response_code($response_data) === 200) { $elementor_data = wp_remote_retrieve_body($response_data); $home_content = json_decode($elementor_data, true); $home_content = $home_content['content'] ?? []; $content = \StoreBuild\StoreBuild_Template::get_instance()->import_export_content($home_content); update_post_meta($page_id, '_wp_page_template', 'elementor_header_footer'); update_post_meta($page_id, '_elementor_edit_mode', 'builder'); update_post_meta($page_id, '_elementor_data', $content); $response[] = "Elementor homepage content imported."; } else { $response[] = "Elementor JSON file not found or request failed for '{$homepage}'."; } $response[] = "Homepage '$home_title' created."; } update_option('page_on_front', $page_id); update_option('show_on_front', 'page'); } return ['success' => true, 'message' => $response, 'next_stage' => 'apply_templates']; } public static function setup_modules($modules) { // Setup modules // Logic for setting up modules $response = []; $modules = json_decode( $modules, true ); if (!empty($modules) && is_array($modules)) { $individual_options = []; $storebuild_modules = []; foreach ($modules as $module) { // return ['success' => true, 'message' => $module, 'next_stage' => 'complete']; switch ($module) { case '_storebuild_sale_notification': $individual_options['_storebuild_sale_notification'] = [ 'storebuild_enable_sale_notification' => true, ]; $response[] = "Sale Notification module setup."; break; case '_storebuild_invoice_settings': $individual_options['_storebuild_invoice_settings'] = [ 'enable' => true, ]; $response[] = "Invoice Settings module setup."; break; case '_storebuild_quickview_settings': $individual_options['_storebuild_quickview_settings'] = [ 'enable' => true, ]; $response[] = "Quickview module setup."; break; case '_storebuild_backorder_settings': $individual_options['_storebuild_backorder_settings'] = [ 'enable' => true, ]; $response[] = "Backorder module setup."; break; case 'storebuild_add_to_cart_notification': case 'storebuild_sale_badge': case 'storebuild_flash_sale_countdown': case 'storebuild_wishlist': case 'storebuild_compare': $storebuild_modules[$module] = true; $label = ucwords(str_replace('_', ' ', str_replace('storebuild_', '', $module))); $response[] = "$label module setup."; break; default: $response[] = "Unknown module: $module"; } } // Update individual options foreach ($individual_options as $key => $value) { update_option($key, $value); } // Update _storebuild_modules once if (!empty($storebuild_modules)) { // Merge with existing settings to preserve previous modules $existing = get_option('_storebuild_modules', []); $merged = array_merge($existing, $storebuild_modules); update_option('_storebuild_modules', $merged); } } else { $response[] = "No modules selected."; } return ['success' => true, 'message' => $response, 'next_stage' => 'complete']; } public static function setup_templates($templates) { // Setup templates // Logic for setting up templates $response = []; $templates = json_decode( $templates, true ); // return ['success' => true, 'message' => var_dump($templates), 'next_stage' => 'check_products']; if (!empty($templates) && is_array($templates)) { $template_content_json_url = 'https://storebuild.shop/templates/contents/'; foreach ($templates as $template) { //return ['success' => true, 'message' => $template, 'next_stage' => 'check_products']; switch ($template) { case 'shop-top-filter': $template_content = file_get_contents($template_content_json_url . 'shop/shop-top-filter.json'); storebuild_setup_import_data('Shop', $template_content, 'shop', $template); $response[] = "Shop template setup."; break; case 'single-default': $template_content = file_get_contents($template_content_json_url . 'single/single-default.json'); storebuild_setup_import_data('Shop Details', $template_content, 'single', $template); $response[] = "Single template setup."; break; case 'cart-1': $template_content = file_get_contents($template_content_json_url . 'cart/cart-1.json'); storebuild_setup_import_data('Cart', $template_content, 'cart', $template); $response[] = "Cart template setup."; break; case 'checkout-1': $template_content = file_get_contents($template_content_json_url . 'checkout/checkout-1.json'); storebuild_setup_import_data('Checkout', $template_content, 'checkout', $template); $response[] = "Checkout template setup."; break; default: $response[] = "Unknown template: $template"; } } } else { $response[] = "No templates selected."; } return ['success' => true, 'message' => $response, 'next_stage' => 'check_products']; } public static function import_products_from_csv_2( $csv_file ) { if ( ! defined( 'WC_ABSPATH' ) ) { return new \WP_Error( 'woocommerce_rest_import_error', __( 'WooCommerce is not loaded.', 'shopbuild' ) ); } $wc_base = constant( 'WC_ABSPATH' ); include_once $wc_base . 'includes/import/class-wc-product-csv-importer.php'; $logger = function_exists( 'wc_get_logger' ) ? wc_get_logger() : null; $log_context = array( 'source' => 'shopbuild-setup-import' ); if ( ! file_exists( $csv_file ) || ! class_exists( 'WC_Product_CSV_Importer' ) ) { return new \WP_Error( 'woocommerce_rest_import_error', __( 'CSV file not found or importer class missing.', 'shopbuild' ) ); } $args = array( 'delimiter' => ',', ); $importer = new WC_Product_CSV_Importer( $csv_file, $args ); $headers = $importer->get_raw_keys(); if ( defined( 'WP_DEBUG' ) && WP_DEBUG && $logger ) { $logger->debug( 'Headers: ' . wp_json_encode( $headers ), $log_context ); } // Debug: show how many were parsed $parsed = $importer->get_parsed_data(); if ( defined( 'WP_DEBUG' ) && WP_DEBUG && $logger ) { $logger->debug( 'Parsed rows: ' . count( $parsed ), $log_context ); } // Import $result = $importer->import(); // Debug: raw data (optional) if ( defined( 'WP_DEBUG' ) && WP_DEBUG && $logger ) { $logger->debug( wp_json_encode( $importer->get_raw_data() ), $log_context ); } return $result; } /** * Header Mappings for CSV Import */ public static function get_header_mappings( $file ) { if ( ! defined( 'WC_ABSPATH' ) ) { return array(); } $wc_base = constant( 'WC_ABSPATH' ); include_once $wc_base . 'includes/admin/importers/mappings/mappings.php'; $importer_class = apply_filters( 'woocommerce_product_csv_importer_class', 'WC_Product_CSV_Importer' ); $importer = new $importer_class( $file, array() ); $raw_headers = $importer->get_raw_keys(); $default_columns = wc_importer_default_english_mappings( array() ); $special_columns = wc_importer_default_special_english_mappings( array() ); $headers = array(); foreach ( $raw_headers as $key => $field ) { $index = $field; $headers[ $index ] = $field; if ( isset( $default_columns[ $field ] ) ) { $headers[ $index ] = $default_columns[ $field ]; } else { foreach ( $special_columns as $regex => $special_key ) { if ( preg_match( self::sanitize_special_column_name_regex( $regex ), $field, $matches ) ) { $headers[ $index ] = $special_key . $matches[1]; break; } } } } return $headers; } /** * Sanitize special column name regex. * * @internal * @param string $value Raw special column name. * @return string */ public static function sanitize_special_column_name_regex( $value ) { return '/' . str_replace( array( '%d', '%s' ), '(.*)', trim( quotemeta( $value ) ) ) . '/'; } /** * Import Products By Category * @param string $category */ public static function import_products_from_csv( $csv_file ) { if ( ! defined( 'WC_ABSPATH' ) ) { return new \WP_Error( 'woocommerce_rest_import_error', __( 'WooCommerce is not loaded.', 'shopbuild' ) ); } $wc_base = constant( 'WC_ABSPATH' ); include_once $wc_base . 'includes/import/class-wc-product-csv-importer.php'; if ( file_exists( $csv_file ) && class_exists( 'WC_Product_CSV_Importer' ) ) { // Import using WC_Product_CSV_Importer $importer_class = apply_filters( 'woocommerce_product_csv_importer_class', 'WC_Product_CSV_Importer' ); $args = array( 'parse' => true, // 'delimiter' => ',', 'mapping' => self::get_header_mappings( $csv_file ), // 'update_existing' => true, ); $args = apply_filters( 'woocommerce_product_csv_importer_args', $args, $importer_class ); $importer = new $importer_class( $csv_file, $args ); $import = $importer->import(); return $import; } else { return new \WP_Error( 'woocommerce_rest_import_error', __( 'CSV file not found or importer class missing.', 'shopbuild' ) ); } } public function maybe_redirect_to_setup() { if (get_option('storebuild_do_activation_redirect', false)) { delete_option('storebuild_do_activation_redirect'); // phpcs:ignore WordPress.Security.NonceVerification.Recommended if ( ! isset( $_GET['activate-multi'] ) ) { wp_safe_redirect(admin_url('admin.php?page=storebuild-setup')); exit; } } } public function register_setup_page() { add_submenu_page( 'storebuild-main', // No parent = not shown in any menu 'StoreBuild Setup', 'StoreBuild Setup', 'manage_options', 'storebuild-setup', array($this, 'render_setup_page') ); } public function render_setup_page() { $woocommerce_active = $this->is_plugin_active('woocommerce/woocommerce.php'); $elementor_active = $this->is_plugin_active('elementor/elementor.php'); require_once STOREBUILD_PATH . 'admin/app/app.php'; } private function is_plugin_active($plugin_path) { include_once(ABSPATH . 'wp-admin/includes/plugin.php'); return is_plugin_active($plugin_path); } public function hide_admin_ui_for_setup_page() { // phpcs:ignore WordPress.Security.NonceVerification.Recommended if (!isset($_GET['page']) || sanitize_text_field( wp_unslash( $_GET['page'] ) ) !== 'storebuild-setup') { return; } echo '<style> #adminmenumain, #wpadminbar, #screen-meta, .update-nag, .notice, #screen-options-link-wrap, .notice-warning, .notice-error { display: none !important; } #wpcontent, #wpfooter { margin-left: 0; } body.storebuild-setup-page { background: #fff; } </style>'; } public function add_fullscreen_body_class($classes) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended if (isset($_GET['page']) && sanitize_text_field( wp_unslash( $_GET['page'] ) ) === 'storebuild-setup') { $classes .= ' storebuild-setup-page'; } return $classes; } } new StoreBuild_Setup(); }
| ver. 1.4 |
Github
|
.
| PHP 8.5.7 | Generation time: 0 |
proxy
|
phpinfo
|
Settings