| 1 |
<?php |
| 2 |
|
| 3 |
namespace MasterAddons\Upgrades; |
| 4 |
|
| 5 |
defined('ABSPATH') || exit; |
| 6 |
|
| 7 |
/** |
| 8 |
* Template Conditions Upgrader Class |
| 9 |
* |
| 10 |
* Handles migration of existing template conditions to new repeater format |
| 11 |
* Adds "Include >" prefix to existing conditions and converts legacy data |
| 12 |
* |
| 13 |
* @since 1.9.1 |
| 14 |
*/ |
| 15 |
class Template_Conditions_Upgrader |
| 16 |
{ |
| 17 |
/** |
| 18 |
* @var string Version key for tracking upgrade completion |
| 19 |
*/ |
| 20 |
private $upgrade_version_key = 'jltma_conditions_upgrade_v1'; |
| 21 |
|
| 22 |
/** |
| 23 |
* @var array Mapping of legacy condition values to new format |
| 24 |
*/ |
| 25 |
private $condition_mapping = [ |
| 26 |
'entire_site' => 'entire_site', |
| 27 |
'singular' => 'singular', |
| 28 |
'archive' => 'archive', |
| 29 |
'search' => 'search', |
| 30 |
'404' => '404', |
| 31 |
'front_page' => 'front_page', |
| 32 |
'post_types' => 'post_types', |
| 33 |
'product' => 'product', |
| 34 |
'product_archive' => 'product_archive' |
| 35 |
]; |
| 36 |
|
| 37 |
/** |
| 38 |
* Constructor |
| 39 |
*/ |
| 40 |
public function __construct() |
| 41 |
{ |
| 42 |
add_action('admin_init', [$this, 'maybe_run_upgrade']); |
| 43 |
add_action('wp_ajax_jltma_force_conditions_upgrade', [$this, 'force_upgrade_ajax']); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Check if upgrade needs to run and execute it |
| 48 |
*/ |
| 49 |
public function maybe_run_upgrade() |
| 50 |
{ |
| 51 |
// Only run in admin |
| 52 |
if (!is_admin()) { |
| 53 |
return; |
| 54 |
} |
| 55 |
|
| 56 |
// Check if upgrade has already been completed |
| 57 |
if (get_option($this->upgrade_version_key, false)) { |
| 58 |
return; |
| 59 |
} |
| 60 |
|
| 61 |
// Check if we have any master_template posts that need upgrading |
| 62 |
if ($this->has_templates_to_upgrade()) { |
| 63 |
$this->run_upgrade(); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Check if there are templates that need upgrading |
| 69 |
* |
| 70 |
* @return bool |
| 71 |
*/ |
| 72 |
private function has_templates_to_upgrade() |
| 73 |
{ |
| 74 |
global $wpdb; |
| 75 |
|
| 76 |
$query = $wpdb->prepare(" |
| 77 |
SELECT COUNT(p.ID) |
| 78 |
FROM {$wpdb->posts} p |
| 79 |
LEFT JOIN {$wpdb->postmeta} pm ON p.ID = pm.post_id |
| 80 |
AND pm.meta_key = 'master_template_conditions_data' |
| 81 |
WHERE p.post_type = %s |
| 82 |
AND p.post_status = 'publish' |
| 83 |
AND pm.meta_value IS NULL |
| 84 |
", 'master_template'); |
| 85 |
|
| 86 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.PreparedSQL.NotPrepared -- $query built via $wpdb->prepare() above; direct query required for upgrade check |
| 87 |
$count = $wpdb->get_var( $query ); |
| 88 |
return $count > 0; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Run the upgrade process |
| 93 |
*/ |
| 94 |
public function run_upgrade() |
| 95 |
{ |
| 96 |
global $wpdb; |
| 97 |
|
| 98 |
// Start transaction for data integrity |
| 99 |
$wpdb->query( 'START TRANSACTION' ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- transaction control; no caching applicable |
| 100 |
|
| 101 |
try { |
| 102 |
$upgraded_count = 0; |
| 103 |
$templates = $this->get_templates_to_upgrade(); |
| 104 |
|
| 105 |
foreach ($templates as $template) { |
| 106 |
if ($this->upgrade_template_conditions($template->ID)) { |
| 107 |
$upgraded_count++; |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
// Commit transaction |
| 112 |
$wpdb->query( 'COMMIT' ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- transaction control; no caching applicable |
| 113 |
|
| 114 |
// Mark upgrade as completed |
| 115 |
update_option($this->upgrade_version_key, true); |
| 116 |
|
| 117 |
// Log upgrade completion |
| 118 |
// error_log("JLTMA Conditions Upgrade: Successfully upgraded {$upgraded_count} templates"); |
| 119 |
|
| 120 |
// Show admin notice |
| 121 |
add_action('admin_notices', function() use ($upgraded_count) { |
| 122 |
echo '<div class="notice notice-success is-dismissible">'; |
| 123 |
echo '<p><strong>Master Addons:</strong> Successfully upgraded ' . absint( $upgraded_count ) . ' template conditions to new format.</p>'; |
| 124 |
echo '</div>'; |
| 125 |
}); |
| 126 |
|
| 127 |
} catch (Exception $e) { |
| 128 |
// Rollback on error |
| 129 |
$wpdb->query( 'ROLLBACK' ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- transaction control; no caching applicable |
| 130 |
// error_log("JLTMA Conditions Upgrade Error: " . $e->getMessage()); |
| 131 |
|
| 132 |
// Show error notice |
| 133 |
add_action('admin_notices', function() use ($e) { |
| 134 |
echo '<div class="notice notice-error is-dismissible">'; |
| 135 |
echo '<p><strong>Master Addons:</strong> Failed to upgrade template conditions. Error: ' . esc_html($e->getMessage()) . '</p>'; |
| 136 |
echo '</div>'; |
| 137 |
}); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Get templates that need upgrading |
| 143 |
* |
| 144 |
* @return array |
| 145 |
*/ |
| 146 |
private function get_templates_to_upgrade() |
| 147 |
{ |
| 148 |
global $wpdb; |
| 149 |
|
| 150 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- direct query required for upgrade migration; no equivalent WP_Query for meta-absent check |
| 151 |
return $wpdb->get_results( $wpdb->prepare( " |
| 152 |
SELECT p.ID, p.post_title |
| 153 |
FROM {$wpdb->posts} p |
| 154 |
LEFT JOIN {$wpdb->postmeta} pm ON p.ID = pm.post_id |
| 155 |
AND pm.meta_key = 'master_template_conditions_data' |
| 156 |
WHERE p.post_type = %s |
| 157 |
AND p.post_status = 'publish' |
| 158 |
AND pm.meta_value IS NULL |
| 159 |
ORDER BY p.ID ASC |
| 160 |
", 'master_template' ) ); |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Upgrade individual template conditions |
| 165 |
* |
| 166 |
* @param int $template_id |
| 167 |
* @return bool |
| 168 |
*/ |
| 169 |
private function upgrade_template_conditions($template_id) |
| 170 |
{ |
| 171 |
// Get existing legacy condition data |
| 172 |
$legacy_conditions = [ |
| 173 |
'jltma_hf_conditions' => get_post_meta($template_id, 'master_template_jltma_hf_conditions', true), |
| 174 |
'jltma_hfc_singular' => get_post_meta($template_id, 'master_template_jltma_hfc_singular', true), |
| 175 |
'jltma_hfc_singular_id' => get_post_meta($template_id, 'master_template_jltma_hfc_singular_id', true), |
| 176 |
'jltma_hfc_post_types_id' => get_post_meta($template_id, 'master_template_jltma_hfc_post_types_id', true), |
| 177 |
]; |
| 178 |
|
| 179 |
// Convert to new repeater format |
| 180 |
$new_conditions = $this->convert_legacy_to_repeater($legacy_conditions); |
| 181 |
|
| 182 |
// Save new conditions data |
| 183 |
if (!empty($new_conditions)) { |
| 184 |
update_post_meta($template_id, 'master_template_conditions_data', $new_conditions); |
| 185 |
return true; |
| 186 |
} |
| 187 |
|
| 188 |
return false; |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Convert legacy conditions to new repeater format |
| 193 |
* |
| 194 |
* @param array $legacy_conditions |
| 195 |
* @return array |
| 196 |
*/ |
| 197 |
private function convert_legacy_to_repeater($legacy_conditions) |
| 198 |
{ |
| 199 |
$conditions_data = []; |
| 200 |
|
| 201 |
// Get the main condition |
| 202 |
$main_condition = $legacy_conditions['jltma_hf_conditions']; |
| 203 |
|
| 204 |
// If no main condition, set default |
| 205 |
if (empty($main_condition)) { |
| 206 |
$conditions_data[] = [ |
| 207 |
'type' => 'include', |
| 208 |
'rule' => 'entire_site', |
| 209 |
'specific' => '' |
| 210 |
]; |
| 211 |
return $conditions_data; |
| 212 |
} |
| 213 |
|
| 214 |
// Map legacy condition to new format |
| 215 |
$rule = isset($this->condition_mapping[$main_condition]) |
| 216 |
? $this->condition_mapping[$main_condition] |
| 217 |
: $main_condition; |
| 218 |
|
| 219 |
$condition = [ |
| 220 |
'type' => 'include', // Always default to include for existing conditions |
| 221 |
'rule' => $rule, |
| 222 |
'specific' => '' |
| 223 |
]; |
| 224 |
|
| 225 |
// Handle singular conditions with specific targeting |
| 226 |
if ($main_condition === 'singular') { |
| 227 |
$singular_type = $legacy_conditions['jltma_hfc_singular']; |
| 228 |
$singular_ids = $legacy_conditions['jltma_hfc_singular_id']; |
| 229 |
|
| 230 |
if ($singular_type === 'selective' && !empty($singular_ids)) { |
| 231 |
$condition['specific'] = $singular_ids; |
| 232 |
} elseif (!empty($singular_type) && $singular_type !== 'all') { |
| 233 |
// Handle other singular types like 'front_page', 'all_posts', etc. |
| 234 |
$condition['rule'] = $singular_type; |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
// Handle post types conditions |
| 239 |
if ($main_condition === 'post_types') { |
| 240 |
$post_types = $legacy_conditions['jltma_hfc_post_types_id']; |
| 241 |
if (!empty($post_types)) { |
| 242 |
$condition['specific'] = is_array($post_types) ? implode(', ', $post_types) : $post_types; |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
$conditions_data[] = $condition; |
| 247 |
|
| 248 |
return $conditions_data; |
| 249 |
} |
| 250 |
|
| 251 |
/** |
| 252 |
* Force upgrade via AJAX (for admin manual trigger) |
| 253 |
*/ |
| 254 |
public function force_upgrade_ajax() |
| 255 |
{ |
| 256 |
// Verify nonce and permissions |
| 257 |
if (!check_ajax_referer('jltma_force_upgrade', 'nonce', false) || !current_user_can('manage_options')) { |
| 258 |
wp_send_json_error(['message' => 'Permission denied']); |
| 259 |
} |
| 260 |
|
| 261 |
// Reset upgrade flag and run upgrade |
| 262 |
delete_option($this->upgrade_version_key); |
| 263 |
|
| 264 |
if ($this->has_templates_to_upgrade()) { |
| 265 |
$this->run_upgrade(); |
| 266 |
wp_send_json_success(['message' => 'Upgrade completed successfully']); |
| 267 |
} else { |
| 268 |
wp_send_json_success(['message' => 'No templates to upgrade']); |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
/** |
| 273 |
* Get upgrade status for admin display |
| 274 |
* |
| 275 |
* @return array |
| 276 |
*/ |
| 277 |
public function get_upgrade_status() |
| 278 |
{ |
| 279 |
return [ |
| 280 |
'completed' => get_option($this->upgrade_version_key, false), |
| 281 |
'templates_to_upgrade' => $this->has_templates_to_upgrade() ? $this->count_templates_to_upgrade() : 0 |
| 282 |
]; |
| 283 |
} |
| 284 |
|
| 285 |
/** |
| 286 |
* Count templates that need upgrading |
| 287 |
* |
| 288 |
* @return int |
| 289 |
*/ |
| 290 |
private function count_templates_to_upgrade() |
| 291 |
{ |
| 292 |
global $wpdb; |
| 293 |
|
| 294 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- direct query required for upgrade count; no equivalent WP_Query for meta-absent check |
| 295 |
return (int) $wpdb->get_var( $wpdb->prepare( " |
| 296 |
SELECT COUNT(p.ID) |
| 297 |
FROM {$wpdb->posts} p |
| 298 |
LEFT JOIN {$wpdb->postmeta} pm ON p.ID = pm.post_id |
| 299 |
AND pm.meta_key = 'master_template_conditions_data' |
| 300 |
WHERE p.post_type = %s |
| 301 |
AND p.post_status = 'publish' |
| 302 |
AND pm.meta_value IS NULL |
| 303 |
", 'master_template' ) ); |
| 304 |
} |
| 305 |
|
| 306 |
/** |
| 307 |
* Add admin menu for upgrade management (optional) |
| 308 |
*/ |
| 309 |
public function add_upgrade_admin_page() |
| 310 |
{ |
| 311 |
add_submenu_page( |
| 312 |
'edit.php?post_type=master_template', |
| 313 |
'Template Conditions Upgrade', |
| 314 |
'Upgrade Conditions', |
| 315 |
'manage_options', |
| 316 |
'jltma-upgrade-conditions', |
| 317 |
[$this, 'render_upgrade_page'] |
| 318 |
); |
| 319 |
} |
| 320 |
|
| 321 |
/** |
| 322 |
* Render upgrade admin page |
| 323 |
*/ |
| 324 |
public function render_upgrade_page() |
| 325 |
{ |
| 326 |
$status = $this->get_upgrade_status(); |
| 327 |
?> |
| 328 |
<div class="wrap"> |
| 329 |
<h1>Template Conditions Upgrade</h1> |
| 330 |
|
| 331 |
<?php if ($status['completed']): ?> |
| 332 |
<div class="notice notice-success"> |
| 333 |
<p><strong>Upgrade Completed:</strong> All template conditions have been upgraded to the new format.</p> |
| 334 |
</div> |
| 335 |
<?php else: ?> |
| 336 |
<div class="notice notice-warning"> |
| 337 |
<p><strong>Upgrade Pending:</strong> Found <?php echo absint( $status['templates_to_upgrade'] ); ?> templates that need upgrading.</p> |
| 338 |
</div> |
| 339 |
<?php endif; ?> |
| 340 |
|
| 341 |
<div class="card"> |
| 342 |
<h2>About This Upgrade</h2> |
| 343 |
<p>This upgrade converts existing template conditions to the new repeater format with the following improvements:</p> |
| 344 |
<ul> |
| 345 |
<li>� |
| 346 |
Adds "Include >" prefix to all existing conditions</li> |
| 347 |
<li>� |
| 348 |
Converts legacy condition data to new repeater format</li> |
| 349 |
<li>� |
| 350 |
Maintains backward compatibility with existing templates</li> |
| 351 |
<li>� |
| 352 |
Improves condition display in admin columns</li> |
| 353 |
</ul> |
| 354 |
|
| 355 |
<?php if (!$status['completed'] && $status['templates_to_upgrade'] > 0): ?> |
| 356 |
<button id="force-upgrade" class="button button-primary"> |
| 357 |
Force Run Upgrade Now |
| 358 |
</button> |
| 359 |
<?php endif; ?> |
| 360 |
</div> |
| 361 |
</div> |
| 362 |
|
| 363 |
<script> |
| 364 |
jQuery(document).ready(function($) { |
| 365 |
$('#force-upgrade').on('click', function() { |
| 366 |
var btn = $(this); |
| 367 |
btn.prop('disabled', true).text('Running Upgrade...'); |
| 368 |
|
| 369 |
$.ajax({ |
| 370 |
url: ajaxurl, |
| 371 |
type: 'POST', |
| 372 |
data: { |
| 373 |
action: 'jltma_force_conditions_upgrade', |
| 374 |
nonce: '<?php echo esc_js( wp_create_nonce('jltma_force_upgrade') ); ?>' |
| 375 |
}, |
| 376 |
success: function(response) { |
| 377 |
if (response.success) { |
| 378 |
location.reload(); |
| 379 |
} else { |
| 380 |
alert('Upgrade failed: ' + response.data.message); |
| 381 |
btn.prop('disabled', false).text('Force Run Upgrade Now'); |
| 382 |
} |
| 383 |
}, |
| 384 |
error: function() { |
| 385 |
alert('Upgrade failed due to network error'); |
| 386 |
btn.prop('disabled', false).text('Force Run Upgrade Now'); |
| 387 |
} |
| 388 |
}); |
| 389 |
}); |
| 390 |
}); |
| 391 |
</script> |
| 392 |
<?php |
| 393 |
} |
| 394 |
|
| 395 |
/** |
| 396 |
* Static method to get instance |
| 397 |
* |
| 398 |
* @return Template_Conditions_Upgrader |
| 399 |
*/ |
| 400 |
public static function instance() |
| 401 |
{ |
| 402 |
static $instance = null; |
| 403 |
if (is_null($instance)) { |
| 404 |
$instance = new self(); |
| 405 |
} |
| 406 |
return $instance; |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
// Initialize the upgrader |
| 411 |
Template_Conditions_Upgrader::instance(); |