# shopbuilder/2.1.0/app/Helpers/Installation.php

ShopBuilder – WooCommerce Builder For Elementor, version 2.1.0. 103 lines.

- Page: https://pluginprobe.com/plugins/shopbuilder/2.1.0/code/app/Helpers/Installation.php
- Raw: https://pluginprobe.com/plugins/shopbuilder/2.1.0/raw/app/Helpers/Installation.php
- Modified: 2024-01-05T06:10:40+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/shopbuilder/2.1.0/code/app/Helpers/Installation.php#L10-L20`.

```php
<?php

namespace RadiusTheme\SB\Helpers;

// Do not allow directly accessing this file.
use RadiusTheme\SB\Models\ExtraSettings;
use RadiusTheme\SB\Models\Settings;

if ( ! defined( 'ABSPATH' ) ) {
	exit( 'This script cannot be accessed directly.' );
}

class Installation {

    public static function init(){
        add_action( 'init', [ __CLASS__, 'check_version' ], 5 );
    }

    public static function check_version() {
		$version =  ExtraSettings::instance()->get_option( 'rtsb_version' ) ?? get_option( 'rtsb_version' );
        if ( version_compare( $version, RTSB_VERSION, '<' ) ) {
	        if ( ! $version ) {
		        self::set_default_options();
		        if ( ! Fns::get_product() ) {
			        return self::create_wc_product();
		        }
	        }
	        self::update_rtsb_version();
        }
    }

	public static function activate() {
        if ( ! is_blog_installed() ) {
            return;
        }

		if( ExtraSettings::instance()->get_option( 'first_instilled_version', false ) ){
			ExtraSettings::instance()->set_option( 'first_instilled_version', RTSB_VERSION );
		}

        // Check if we are not already running this routine.
        if ( 'yes' === get_transient( 'rtsb_installing' ) ) {
            return;
        }

        // If we made it till here nothing is running yet, lets set the transient now.
        set_transient( 'rtsb_installing', 'yes', MINUTE_IN_SECONDS * 10 );
		delete_transient( 'rtsb_installing' );
	}

    private static function update_rtsb_version() {
		// update_option( 'rtsb_version', RTSB_VERSION );
	    ExtraSettings::instance()->set_option( 'rtsb_version', RTSB_VERSION );
    }

	public static function deactivation() { }

    public static function set_default_options(){
        $sections = Settings::instance()->get_sections();
        foreach ( $sections as $sections_id  => $section ) {
            $rawOptions = [];
            if ( ! empty( $section['list'] ) ) {
                foreach ( $section['list'] as $blocks_id  => $blocks) {
					if( ! rtsb()->has_pro() && 'free' !== ( $blocks['package'] ?? '' ) ){
						continue;
					}
					$rawOptions['active'] = $blocks['active'] ?? null;
	                foreach ( $blocks['fields'] as $field_id  => $field ) {
                        $type = 'array' === gettype( $field['value'] ) ? [] : '' ;
                        $rawOptions[$field_id] = ! empty( $field['value'] ) ? $field['value'] : $type;
                    }
                    Fns::set_options( $sections_id, $blocks_id, $rawOptions );
                }
            }
        }
    }

	/**
	 * @return int
	 * @throws \WC_Data_Exception
	 */
	public static function create_wc_product() {

		$product = new \WC_Product_Simple();
		$product->set_name( 'Confirm at least one product is created before deleting' );
		$product->set_description( 'This is a ShopBuilder demo preview product' );
		$product->set_short_description( 'This is a ShopBuilder product' );
		$product->set_status( 'draft' );

		$product->set_regular_price( 50 );
		$product->set_sale_price( 49 );
		$product->set_price( 49 );

		$product->set_sku( 'SAMPLE-001' );

		$product->set_manage_stock( false );
		$product->set_stock_status( 'instock' );

		return $product->save();
	}


}
```
