| 1 |
<?php |
| 2 |
/** |
| 3 |
* PropertyHive Updates |
| 4 |
* |
| 5 |
* Functions for updating data during an update. |
| 6 |
* |
| 7 |
* @author PropertyHive |
| 8 |
* @category Core |
| 9 |
* @package PropertyHive/Functions |
| 10 |
* @version 1.0.0 |
| 11 |
*/ |
| 12 |
|
| 13 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 14 |
|
| 15 |
/** |
| 16 |
* Update on_market_change_dates for 1.4.68 |
| 17 |
* |
| 18 |
* @return void |
| 19 |
*/ |
| 20 |
function propertyhive_update_1468_on_market_change_dates() { |
| 21 |
global $wpdb; |
| 22 |
|
| 23 |
$args = array( |
| 24 |
'post_type' => 'property', |
| 25 |
'fields' => 'ids', |
| 26 |
'post_status' => 'publish', |
| 27 |
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query -- This maintenance backfill must select records missing the destination metadata; removing the predicate would overwrite existing values. |
| 28 |
'meta_query' => array( |
| 29 |
array( |
| 30 |
'key' => '_on_market', |
| 31 |
'value' => 'yes' |
| 32 |
), |
| 33 |
array( |
| 34 |
'key' => '_on_market_change_date', |
| 35 |
'compare' => 'NOT EXISTS' |
| 36 |
) |
| 37 |
), |
| 38 |
'nopaging' => true, |
| 39 |
// phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.SuppressFilters_suppress_filters -- Maintenance must cover every language; front-end language filters would omit records from this backfill. |
| 40 |
'suppress_filters' => true, |
| 41 |
); |
| 42 |
$property_query = new WP_Query($args); |
| 43 |
|
| 44 |
if ( $property_query->have_posts() ) |
| 45 |
{ |
| 46 |
while ( $property_query->have_posts() ) |
| 47 |
{ |
| 48 |
$property_query->the_post(); |
| 49 |
|
| 50 |
$date_post_written = get_the_date( 'Y-m-d H:i:s' ); |
| 51 |
add_post_meta( get_the_ID(), '_on_market_change_date', $date_post_written ); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
wp_reset_postdata(); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Record the fact we've updated to version 2 from an older version and which add ons were installed at the time of update |
| 60 |
* |
| 61 |
* @return void |
| 62 |
*/ |
| 63 |
function propertyhive_update_200_pre_pro_record_installed_plugins() |
| 64 |
{ |
| 65 |
$installed_plugins = array(); |
| 66 |
|
| 67 |
$features = get_ph_pro_features(); |
| 68 |
|
| 69 |
foreach ( $features as $feature ) |
| 70 |
{ |
| 71 |
$slug = explode("/", $feature['wordpress_plugin_file']); |
| 72 |
$slug = $slug[0]; |
| 73 |
|
| 74 |
if ( is_dir( WP_PLUGIN_DIR . '/' . $slug ) ) |
| 75 |
{ |
| 76 |
$installed_plugins[] = array( |
| 77 |
'slug' => $slug, |
| 78 |
'plugin' => $feature['wordpress_plugin_file'] |
| 79 |
); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
update_option( 'propertyhive_pre_pro_add_ons', $installed_plugins ); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Deactivate Template Assistant add on after moving it's functionality into core |
| 88 |
* |
| 89 |
* @return void |
| 90 |
*/ |
| 91 |
function propertyhive_deactivate_template_assistant() |
| 92 |
{ |
| 93 |
if ( ! function_exists( 'is_plugin_active' ) ) |
| 94 |
{ |
| 95 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 96 |
} |
| 97 |
|
| 98 |
$ta_plugin = 'propertyhive-template-assistant/propertyhive-template-assistant.php'; |
| 99 |
|
| 100 |
// Single site. |
| 101 |
if ( ! is_multisite() ) |
| 102 |
{ |
| 103 |
if ( is_plugin_active( $ta_plugin ) ) |
| 104 |
{ |
| 105 |
deactivate_plugins( $ta_plugin, true, false ); |
| 106 |
|
| 107 |
$auto_deactivated = get_option( 'propertyhive_template_assistant_auto_deactivated', null ); |
| 108 |
|
| 109 |
if ( null === $auto_deactivated ) |
| 110 |
{ |
| 111 |
update_option( |
| 112 |
'propertyhive_template_assistant_auto_deactivated', |
| 113 |
current_time( 'mysql' ), |
| 114 |
false |
| 115 |
); |
| 116 |
} |
| 117 |
|
| 118 |
$existing_ta_settings = get_option( 'propertyhive_template_assistant', null ); |
| 119 |
$existing_ta_settings_backup = get_option( 'propertyhive_template_assistant_backup', null ); |
| 120 |
|
| 121 |
if ( null !== $existing_ta_settings && null === $existing_ta_settings_backup ) |
| 122 |
{ |
| 123 |
update_option( |
| 124 |
'propertyhive_template_assistant_backup', |
| 125 |
array( |
| 126 |
'backed_up_at' => current_time( 'mysql' ), |
| 127 |
'settings' => $existing_ta_settings, |
| 128 |
), |
| 129 |
false |
| 130 |
); |
| 131 |
} |
| 132 |
} |
| 133 |
else |
| 134 |
{ |
| 135 |
// TA plugin not active. Take backup of settings if exists and empty current settings |
| 136 |
$existing_ta_settings = get_option( 'propertyhive_template_assistant', null ); |
| 137 |
$existing_ta_settings_backup = get_option( 'propertyhive_template_assistant_backup', null ); |
| 138 |
|
| 139 |
if ( null !== $existing_ta_settings && null === $existing_ta_settings_backup ) |
| 140 |
{ |
| 141 |
update_option( |
| 142 |
'propertyhive_template_assistant_backup', |
| 143 |
array( |
| 144 |
'backed_up_at' => current_time( 'mysql' ), |
| 145 |
'settings' => $existing_ta_settings, |
| 146 |
), |
| 147 |
false |
| 148 |
); |
| 149 |
|
| 150 |
delete_option( 'propertyhive_template_assistant' ); |
| 151 |
} |
| 152 |
|
| 153 |
} |
| 154 |
|
| 155 |
return; |
| 156 |
} |
| 157 |
|
| 158 |
$was_network_active = false; |
| 159 |
|
| 160 |
// Multisite: handle network-active first. |
| 161 |
if ( is_plugin_active_for_network( $ta_plugin ) ) |
| 162 |
{ |
| 163 |
$was_network_active = true; |
| 164 |
|
| 165 |
deactivate_plugins( $ta_plugin, true, true ); |
| 166 |
|
| 167 |
$auto_deactivated = get_site_option( 'propertyhive_template_assistant_auto_deactivated', null ); |
| 168 |
|
| 169 |
if ( null === $auto_deactivated ) |
| 170 |
{ |
| 171 |
update_site_option( |
| 172 |
'propertyhive_template_assistant_auto_deactivated', |
| 173 |
current_time( 'mysql' ) |
| 174 |
); |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
// Multisite: handle each site individually. |
| 179 |
$sites = get_sites( |
| 180 |
array( |
| 181 |
'fields' => 'ids', |
| 182 |
'number' => 0, |
| 183 |
) |
| 184 |
); |
| 185 |
|
| 186 |
foreach ( $sites as $blog_id ) |
| 187 |
{ |
| 188 |
switch_to_blog( $blog_id ); |
| 189 |
|
| 190 |
if ( $was_network_active || is_plugin_active( $ta_plugin ) ) |
| 191 |
{ |
| 192 |
deactivate_plugins( $ta_plugin, true, false ); |
| 193 |
|
| 194 |
$auto_deactivated = get_option( 'propertyhive_template_assistant_auto_deactivated', null ); |
| 195 |
|
| 196 |
if ( null === $auto_deactivated ) |
| 197 |
{ |
| 198 |
update_option( |
| 199 |
'propertyhive_template_assistant_auto_deactivated', |
| 200 |
current_time( 'mysql' ), |
| 201 |
false |
| 202 |
); |
| 203 |
} |
| 204 |
|
| 205 |
$existing_ta_settings = get_option( 'propertyhive_template_assistant', null ); |
| 206 |
$existing_ta_settings_backup = get_option( 'propertyhive_template_assistant_backup', null ); |
| 207 |
|
| 208 |
if ( null !== $existing_ta_settings && null === $existing_ta_settings_backup ) { |
| 209 |
update_option( |
| 210 |
'propertyhive_template_assistant_backup', |
| 211 |
array( |
| 212 |
'backed_up_at' => current_time( 'mysql' ), |
| 213 |
'settings' => $existing_ta_settings, |
| 214 |
), |
| 215 |
false |
| 216 |
); |
| 217 |
} |
| 218 |
} |
| 219 |
else |
| 220 |
{ |
| 221 |
// TA plugin not active. Take backup of settings if exists and empty current settings |
| 222 |
$existing_ta_settings = get_option( 'propertyhive_template_assistant', null ); |
| 223 |
$existing_ta_settings_backup = get_option( 'propertyhive_template_assistant_backup', null ); |
| 224 |
|
| 225 |
if ( null !== $existing_ta_settings && null === $existing_ta_settings_backup ) |
| 226 |
{ |
| 227 |
update_option( |
| 228 |
'propertyhive_template_assistant_backup', |
| 229 |
array( |
| 230 |
'backed_up_at' => current_time( 'mysql' ), |
| 231 |
'settings' => $existing_ta_settings, |
| 232 |
), |
| 233 |
false |
| 234 |
); |
| 235 |
|
| 236 |
delete_option( 'propertyhive_template_assistant' ); |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
restore_current_blog(); |
| 241 |
} |
| 242 |
} |