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