| 1 |
<?php |
| 2 |
namespace BetterLinks\Traits; |
| 3 |
if ( ! defined( 'ABSPATH' ) ) { exit; } |
| 4 |
|
| 5 |
use BetterLinks\Admin\Cache; |
| 6 |
use BetterLinks\Helper; |
| 7 |
|
| 8 |
// phpcs:disable WordPress.DB.PreparedSQL, PluginCheck.Security.DirectDB |
| 9 |
|
| 10 |
trait DBMigrate { |
| 11 |
|
| 12 |
public function db_migration_1_1() { |
| 13 |
$table_name = $this->wpdb->prefix . 'betterlinks'; |
| 14 |
$betterlinks = $this->wpdb->get_row( "SELECT * FROM $table_name" ); |
| 15 |
// Add column if not present. |
| 16 |
if ( ! isset( $betterlinks->wildcards ) ) { |
| 17 |
$this->wpdb->query( "ALTER TABLE $table_name ADD wildcards BOOLEAN NOT NULL DEFAULT 0" ); |
| 18 |
} |
| 19 |
} |
| 20 |
public function db_migration_1_2() { |
| 21 |
$table_name = $this->wpdb->prefix . 'betterlinks'; |
| 22 |
$betterlinks = $this->wpdb->get_row( "SELECT * FROM $table_name" ); |
| 23 |
// Add column if not present. |
| 24 |
if ( ! isset( $betterlinks->expire ) ) { |
| 25 |
$this->wpdb->query( "ALTER TABLE $table_name ADD expire text default NULL" ); |
| 26 |
} |
| 27 |
} |
| 28 |
public function db_migration_1_4() { |
| 29 |
// links |
| 30 |
$betterlinks_table = $this->wpdb->prefix . 'betterlinks'; |
| 31 |
$betterlinks = $this->wpdb->get_row( "SELECT * FROM $betterlinks_table" ); |
| 32 |
// Add column if not present. |
| 33 |
if ( ! isset( $betterlinks->dynamic_redirect ) ) { |
| 34 |
$this->wpdb->query( "ALTER TABLE $betterlinks_table ADD dynamic_redirect text default NULL" ); |
| 35 |
} |
| 36 |
// clicks |
| 37 |
$betterlinks_clicks_table = $this->wpdb->prefix . 'betterlinks_clicks'; |
| 38 |
$betterlinks_clicks = $this->wpdb->get_row( "SELECT * FROM $betterlinks_clicks_table" ); |
| 39 |
// Add column if not present. |
| 40 |
if ( ! isset( $betterlinks_clicks->rotation_target_url ) ) { |
| 41 |
$this->wpdb->query( "ALTER TABLE $betterlinks_clicks_table ADD rotation_target_url varchar(255) NULL" ); |
| 42 |
} |
| 43 |
} |
| 44 |
public function update_fluent_settings() { |
| 45 |
if ( ! defined( 'FLUENT_BOARDS' ) ) { |
| 46 |
return; |
| 47 |
} |
| 48 |
$settings = Cache::get_json_settings(); |
| 49 |
|
| 50 |
// Ensure $settings is an array (fix for PHP 8+ scalar value error) |
| 51 |
if ( ! is_array( $settings ) ) { |
| 52 |
$settings = array(); |
| 53 |
} |
| 54 |
|
| 55 |
if ( empty( $settings['fbs']['enable_fbs'] ) ) { |
| 56 |
$args = array( |
| 57 |
'ID' => 0, |
| 58 |
'term_name' => 'Fluent Boards', |
| 59 |
'term_slug' => 'btl-fluent-boards', |
| 60 |
'term_type' => 'category', |
| 61 |
); |
| 62 |
$results = $this->create_term( $args ); |
| 63 |
$fbs_cat = ! empty( $results['ID'] ) ? $results['ID'] : 0; |
| 64 |
|
| 65 |
$settings['fbs'] = array(); |
| 66 |
$settings['fbs'] = array( |
| 67 |
'enable_fbs' => true, |
| 68 |
'cat_id' => $fbs_cat, |
| 69 |
'delete_on' => 'task_delete', |
| 70 |
); |
| 71 |
} |
| 72 |
if ( $settings ) { |
| 73 |
Helper::clear_query_cache(); |
| 74 |
$settings = wp_json_encode( $settings ); |
| 75 |
update_option( BETTERLINKS_LINKS_OPTION_NAME, $settings ); |
| 76 |
Cache::write_json_settings(); |
| 77 |
Helper::write_links_inside_json(); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
public function update_fluent_task_delete_settings() { |
| 82 |
if ( ! defined( 'FLUENT_BOARDS' ) ) { |
| 83 |
return; |
| 84 |
} |
| 85 |
|
| 86 |
$settings = Cache::get_json_settings(); |
| 87 |
|
| 88 |
// Ensure $settings is an array (fix for PHP 8+ scalar value error) |
| 89 |
if ( ! is_array( $settings ) ) { |
| 90 |
$settings = array(); |
| 91 |
} |
| 92 |
|
| 93 |
if ( ! empty( $settings['fbs']['delete_on'] ) ) { |
| 94 |
return; |
| 95 |
} |
| 96 |
$settings['fbs']['delete_on'] = 'task_delete'; |
| 97 |
|
| 98 |
if ( $settings ) { |
| 99 |
Helper::clear_query_cache(); |
| 100 |
$settings = wp_json_encode( $settings ); |
| 101 |
update_option( BETTERLINKS_LINKS_OPTION_NAME, $settings ); |
| 102 |
Cache::write_json_settings(); |
| 103 |
Helper::write_links_inside_json(); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
public function update_cle_category() { |
| 108 |
$settings = Cache::get_json_settings(); |
| 109 |
|
| 110 |
// Ensure $settings is an array (fix for PHP 8+ scalar value error) |
| 111 |
if ( ! is_array( $settings ) ) { |
| 112 |
$settings = array(); |
| 113 |
} |
| 114 |
|
| 115 |
if ( isset( $settings['cle'] ) && empty( $settings['cle']['category'] ) ) { |
| 116 |
$settings['cle']['category'] = '1'; |
| 117 |
|
| 118 |
if ( $settings ) { |
| 119 |
Helper::clear_query_cache(); |
| 120 |
$settings = wp_json_encode( $settings ); |
| 121 |
update_option( BETTERLINKS_LINKS_OPTION_NAME, $settings ); |
| 122 |
Cache::write_json_settings(); |
| 123 |
Helper::write_links_inside_json(); |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
public function update_settings() { |
| 130 |
$settings = Cache::get_json_settings(); |
| 131 |
|
| 132 |
// Ensure $settings is an array (fix for PHP 8+ scalar value error) |
| 133 |
if ( ! is_array( $settings ) ) { |
| 134 |
$settings = array(); |
| 135 |
} |
| 136 |
|
| 137 |
if ( empty( $settings['enable_custom_domain_menu'] ) ) { |
| 138 |
$settings['enable_custom_domain_menu'] = true; |
| 139 |
} |
| 140 |
$settings = json_encode( $settings ); |
| 141 |
delete_transient( BETTERLINKS_CACHE_LINKS_NAME ); |
| 142 |
if ( $settings ) { |
| 143 |
update_option( BETTERLINKS_LINKS_OPTION_NAME, $settings ); |
| 144 |
Cache::write_json_settings(); |
| 145 |
} |
| 146 |
if ( empty( get_option( BETTERLINKS_CUSTOM_DOMAIN_MENU, false ) ) ) { |
| 147 |
update_option( BETTERLINKS_CUSTOM_DOMAIN_MENU, true ); |
| 148 |
} |
| 149 |
// regenerate links for wildcards option update |
| 150 |
Helper::write_links_inside_json(); |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Migrate settings to ensure all default settings exist |
| 155 |
* This is crucial for backward compatibility with older plugin versions |
| 156 |
* |
| 157 |
* @since 2.6.1 |
| 158 |
*/ |
| 159 |
public function migrate_default_settings() { |
| 160 |
// Get current settings |
| 161 |
$current_settings = get_option( BETTERLINKS_LINKS_OPTION_NAME, '{}' ); |
| 162 |
|
| 163 |
// If it's a JSON string, decode it |
| 164 |
if ( is_string( $current_settings ) ) { |
| 165 |
$current_settings = json_decode( $current_settings, true ); |
| 166 |
} |
| 167 |
|
| 168 |
// If decoding failed or empty, initialize as array |
| 169 |
if ( ! is_array( $current_settings ) ) { |
| 170 |
$current_settings = array(); |
| 171 |
} |
| 172 |
|
| 173 |
// Define default settings - same as in save_settings() method |
| 174 |
$default_settings = array( |
| 175 |
'redirect_type' => '307', |
| 176 |
'nofollow' => true, |
| 177 |
'sponsored' => '', |
| 178 |
'track_me' => true, |
| 179 |
'param_forwarding' => '', |
| 180 |
'wildcards' => false, |
| 181 |
'disablebotclicks' => false, |
| 182 |
'is_allow_gutenberg' => true, |
| 183 |
'force_https' => false, |
| 184 |
'prefix' => 'go', |
| 185 |
'is_allow_qr' => false, |
| 186 |
'is_random_string' => false, |
| 187 |
'is_autolink_icon' => false, |
| 188 |
'is_autolink_headings' => true, |
| 189 |
'is_case_sensitive' => false, |
| 190 |
'enable_custom_domain_menu' => true, |
| 191 |
'enable_auto_title_suggestion' => true, |
| 192 |
'fbs' => array( |
| 193 |
'enable_fbs' => true, |
| 194 |
'cat_id' => 0, |
| 195 |
'delete_on' => 'task_delete', |
| 196 |
), |
| 197 |
); |
| 198 |
|
| 199 |
// Track if any changes were made |
| 200 |
$settings_updated = false; |
| 201 |
|
| 202 |
// Recheck each setting - only add if missing, never overwrite |
| 203 |
foreach ( $default_settings as $key => $default_value ) { |
| 204 |
if ( ! isset( $current_settings[ $key ] ) ) { |
| 205 |
// Key doesn't exist, add it with default value |
| 206 |
$current_settings[ $key ] = $default_value; |
| 207 |
$settings_updated = true; |
| 208 |
} elseif ( 'fbs' === $key && is_array( $default_value ) ) { |
| 209 |
// Special handling for nested 'fbs' array |
| 210 |
if ( ! is_array( $current_settings[ $key ] ) ) { |
| 211 |
$current_settings[ $key ] = $default_value; |
| 212 |
$settings_updated = true; |
| 213 |
} else { |
| 214 |
// Check each sub-key in fbs |
| 215 |
foreach ( $default_value as $sub_key => $sub_default_value ) { |
| 216 |
if ( ! isset( $current_settings[ $key ][ $sub_key ] ) ) { |
| 217 |
$current_settings[ $key ][ $sub_key ] = $sub_default_value; |
| 218 |
$settings_updated = true; |
| 219 |
} |
| 220 |
} |
| 221 |
} |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
// Only update if changes were made |
| 226 |
if ( $settings_updated ) { |
| 227 |
// Encode and save |
| 228 |
$settings_json = wp_json_encode( $current_settings ); |
| 229 |
update_option( BETTERLINKS_LINKS_OPTION_NAME, $settings_json ); |
| 230 |
|
| 231 |
// Update cache and regenerate links |
| 232 |
Cache::write_json_settings(); |
| 233 |
Helper::clear_query_cache(); |
| 234 |
Helper::write_links_inside_json(); |
| 235 |
} |
| 236 |
} |
| 237 |
} |
| 238 |
|