| 1 |
<?php |
| 2 |
|
| 3 |
namespace RadiusTheme\SB\Helpers; |
| 4 |
|
| 5 |
// Do not allow directly accessing this file. |
| 6 |
use RadiusTheme\SB\Models\ExtraSettings; |
| 7 |
use RadiusTheme\SB\Models\Settings; |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit( 'This script cannot be accessed directly.' ); |
| 11 |
} |
| 12 |
|
| 13 |
class Installation { |
| 14 |
|
| 15 |
public static function init() { |
| 16 |
add_action( 'init', [ __CLASS__, 'check_version' ], 5 ); |
| 17 |
} |
| 18 |
|
| 19 |
public static function check_version() { |
| 20 |
$version = ExtraSettings::instance()->get_option( 'rtsb_version' ) ?? get_option( 'rtsb_version' ); |
| 21 |
if ( version_compare( $version, RTSB_VERSION, '<' ) ) { |
| 22 |
if ( ! $version ) { |
| 23 |
self::set_default_options(); |
| 24 |
if ( ! Fns::get_product() ) { |
| 25 |
return self::create_wc_product(); |
| 26 |
} |
| 27 |
} |
| 28 |
self::update_rtsb_version(); |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
public static function activate() { |
| 33 |
if ( ! is_blog_installed() ) { |
| 34 |
return; |
| 35 |
} |
| 36 |
// flush_rewrite_rules(); |
| 37 |
if ( ExtraSettings::instance()->get_option( 'first_instilled_version', false ) ) { |
| 38 |
ExtraSettings::instance()->set_option( 'first_instilled_version', RTSB_VERSION ); |
| 39 |
} |
| 40 |
|
| 41 |
if ( ! ExtraSettings::instance()->get_option( 'rtsb_plugin_activation_time', false ) ) { |
| 42 |
ExtraSettings::instance()->set_option( 'rtsb_plugin_activation_time', strtotime( 'now' ) ); |
| 43 |
} |
| 44 |
|
| 45 |
// Check if we are not already running this routine. |
| 46 |
if ( 'yes' === get_transient( 'rtsb_installing' ) ) { |
| 47 |
return; |
| 48 |
} |
| 49 |
|
| 50 |
// If we made it till here nothing is running yet, lets set the transient now. |
| 51 |
set_transient( 'rtsb_installing', 'yes', MINUTE_IN_SECONDS * 10 ); |
| 52 |
delete_transient( 'rtsb_installing' ); |
| 53 |
} |
| 54 |
|
| 55 |
private static function update_rtsb_version() { |
| 56 |
// update_option( 'rtsb_version', RTSB_VERSION ); |
| 57 |
ExtraSettings::instance()->set_option( 'rtsb_version', RTSB_VERSION ); |
| 58 |
} |
| 59 |
|
| 60 |
public static function deactivation() { |
| 61 |
delete_option( 'shopbuilder_permalinks_flushed' ); |
| 62 |
} |
| 63 |
|
| 64 |
public static function set_default_options() { |
| 65 |
$sections = Settings::instance()->get_sections(); |
| 66 |
foreach ( $sections as $sections_id => $section ) { |
| 67 |
$rawOptions = []; |
| 68 |
if ( ! empty( $section['list'] ) ) { |
| 69 |
foreach ( $section['list'] as $blocks_id => $blocks ) { |
| 70 |
if ( ! rtsb()->has_pro() && 'free' !== ( $blocks['package'] ?? '' ) ) { |
| 71 |
continue; |
| 72 |
} |
| 73 |
$rawOptions['active'] = $blocks['active'] ?? null; |
| 74 |
foreach ( $blocks['fields'] as $field_id => $field ) { |
| 75 |
$type = 'array' === gettype( $field['value'] ) ? [] : ''; |
| 76 |
$rawOptions[ $field_id ] = ! empty( $field['value'] ) ? $field['value'] : $type; |
| 77 |
} |
| 78 |
Fns::set_options( $sections_id, $blocks_id, $rawOptions ); |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* @return int |
| 86 |
* @throws \WC_Data_Exception |
| 87 |
*/ |
| 88 |
public static function create_wc_product() { |
| 89 |
|
| 90 |
$product = new \WC_Product_Simple(); |
| 91 |
$product->set_name( 'Confirm at least one product is created before deleting' ); |
| 92 |
$product->set_description( 'This is a ShopBuilder demo preview product' ); |
| 93 |
$product->set_short_description( 'This is a ShopBuilder product' ); |
| 94 |
$product->set_status( 'draft' ); |
| 95 |
|
| 96 |
$product->set_regular_price( 50 ); |
| 97 |
$product->set_sale_price( 49 ); |
| 98 |
$product->set_price( 49 ); |
| 99 |
|
| 100 |
$product->set_sku( 'SAMPLE-001' ); |
| 101 |
|
| 102 |
$product->set_manage_stock( false ); |
| 103 |
$product->set_stock_status( 'instock' ); |
| 104 |
|
| 105 |
return $product->save(); |
| 106 |
} |
| 107 |
} |
| 108 |
|