| 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\TemplateSettings; |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit( 'This script cannot be accessed directly.' ); |
| 11 |
} |
| 12 |
|
| 13 |
class Migration { |
| 14 |
private static $first_instilled; |
| 15 |
|
| 16 |
public static function init() { |
| 17 |
self::$first_instilled = ExtraSettings::instance()->get_option( 'first_instilled_version', false ); |
| 18 |
add_action( 'init', [ __CLASS__, 'the_migration' ], 5 ); |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* @return void |
| 23 |
*/ |
| 24 |
public static function the_migration() { |
| 25 |
|
| 26 |
if ( self::$first_instilled && version_compare( self::$first_instilled, RTSB_VERSION, '==' ) ) { |
| 27 |
return; |
| 28 |
} |
| 29 |
$v_2_0_4 = ExtraSettings::instance()->get_option( 'settings_migration_v_2_0_4', false ); |
| 30 |
if ( ! $v_2_0_4 ) { |
| 31 |
self::settings_migration_plugin_v_2_0_4(); |
| 32 |
ExtraSettings::instance()->set_option( 'settings_migration_v_2_0_4', RTSB_VERSION ); |
| 33 |
return; |
| 34 |
} |
| 35 |
|
| 36 |
$v_2_1_16 = ExtraSettings::instance()->get_option( 'template_settings_migration_v_2_1_16', false ); |
| 37 |
if ( ! $v_2_1_16 ) { |
| 38 |
self::template_settings_migration_plugin_v_2_1_16(); |
| 39 |
ExtraSettings::instance()->set_option( 'template_settings_migration_v_2_1_16', RTSB_VERSION ); |
| 40 |
return; |
| 41 |
} |
| 42 |
// return; |
| 43 |
} |
| 44 |
/** |
| 45 |
* @return void |
| 46 |
*/ |
| 47 |
private static function remove_option_for_missing_builder_v_2_0_4( $name ) { |
| 48 |
$array = explode( '_', $name ); |
| 49 |
$post_id = end( $array ); |
| 50 |
if ( ! is_numeric( $post_id ) ) { |
| 51 |
return; |
| 52 |
} |
| 53 |
if ( get_post_status( $post_id ) ) { |
| 54 |
return; |
| 55 |
} |
| 56 |
TemplateSettings::instance()->delete_option( $name ); |
| 57 |
} |
| 58 |
/** |
| 59 |
* @return void |
| 60 |
*/ |
| 61 |
public static function settings_migration_plugin_v_2_0_4() { |
| 62 |
global $wpdb; |
| 63 |
// Prefixes to search for. |
| 64 |
$prefixes = [ |
| 65 |
'rtsb_tb_template', |
| 66 |
'rtsb_template_specific', |
| 67 |
'rtsb_template_for', |
| 68 |
]; |
| 69 |
|
| 70 |
// Array to store matching options. |
| 71 |
// Loop through each prefix. |
| 72 |
foreach ( $prefixes as $prefix ) { |
| 73 |
// Run a direct database query to fetch option names and values. |
| 74 |
$query = $wpdb->prepare( "SELECT option_name, option_value FROM {$wpdb->options} WHERE option_name LIKE %s", $wpdb->esc_like( $prefix ) . '%' ); |
| 75 |
$options_with_prefix = $wpdb->get_results( $query, OBJECT ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 76 |
if ( $options_with_prefix ) { |
| 77 |
foreach ( $options_with_prefix as $option ) { |
| 78 |
$value = maybe_unserialize( $option->option_value ); |
| 79 |
if ( ! empty( $value ) ) { |
| 80 |
TemplateSettings::instance()->set_option( $option->option_name, $value ); |
| 81 |
} |
| 82 |
delete_option( $option->option_name ); |
| 83 |
self::remove_option_for_missing_builder_v_2_0_4( $option->option_name ); |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
// Extra Setings. |
| 89 |
$extra_prefixes = [ |
| 90 |
'rtsb_version', |
| 91 |
'rtsb_wishlist_db_version', |
| 92 |
'rtsb_compare_db_version', |
| 93 |
'rtsbpro_version', |
| 94 |
]; |
| 95 |
// Array to store matching options. |
| 96 |
// Loop through each prefix. |
| 97 |
foreach ( $extra_prefixes as $prefix ) { |
| 98 |
// Run a direct database query to fetch option names and values. |
| 99 |
$query = $wpdb->prepare( "SELECT option_name, option_value FROM {$wpdb->options} WHERE option_name LIKE %s", $wpdb->esc_like( $prefix ) . '%' ); |
| 100 |
$options_with_prefix = $wpdb->get_results( $query, OBJECT ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 101 |
if ( $options_with_prefix ) { |
| 102 |
foreach ( $options_with_prefix as $option ) { |
| 103 |
$value = maybe_unserialize( $option->option_value ); |
| 104 |
if ( ! empty( $value ) ) { |
| 105 |
ExtraSettings::instance()->set_option( $option->option_name, $value ); |
| 106 |
} |
| 107 |
delete_option( $option->option_name ); |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* @return void |
| 115 |
*/ |
| 116 |
private static function template_settings_migration_plugin_v_2_1_16() { |
| 117 |
$args = [ |
| 118 |
'post_type' => BuilderFns::$post_type_tb, |
| 119 |
'numberposts' => -1, |
| 120 |
'fields' => 'ids', |
| 121 |
]; |
| 122 |
$posts_id = get_posts( $args ); |
| 123 |
if ( ! empty( $posts_id ) ) { |
| 124 |
foreach ( $posts_id as $id ) { |
| 125 |
$template_type = BuilderFns::builder_type( $id ); |
| 126 |
$options_name = BuilderFns::option_name_by_template_id( $id ); |
| 127 |
$product_ids = TemplateSettings::instance()->get_option( $options_name ); |
| 128 |
if ( 'product' === $template_type && ! empty( $product_ids ) ) { |
| 129 |
update_post_meta( $id, '_is_product_page_template_for', 'specific_products' ); |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
|