| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; // Exit if accessed directly! |
| 4 |
} |
| 5 |
|
| 6 |
if ( ! class_exists( 'WPSC_SC_Upgrade' ) ) : |
| 7 |
|
| 8 |
final class WPSC_SC_Upgrade { |
| 9 |
|
| 10 |
/** |
| 11 |
* Update attachment file paths in database |
| 12 |
* |
| 13 |
* @param WPSC_Scheduled_Task $task - task object. |
| 14 |
* @version 3.1.0 |
| 15 |
* @return void |
| 16 |
*/ |
| 17 |
public static function update_ticket_attachment_path( $task ) { |
| 18 |
|
| 19 |
$upload_dir = wp_upload_dir(); |
| 20 |
|
| 21 |
$attachments = WPSC_Attachment::find( |
| 22 |
array( |
| 23 |
'items_per_page' => 100, |
| 24 |
'meta_query' => array( |
| 25 |
'relation' => 'AND', |
| 26 |
array( |
| 27 |
'slug' => 'file_path', |
| 28 |
'compare' => 'LIKE', |
| 29 |
'val' => $upload_dir['basedir'], |
| 30 |
), |
| 31 |
), |
| 32 |
) |
| 33 |
); |
| 34 |
|
| 35 |
if ( $attachments['total_items'] === 0 ) { |
| 36 |
WPSC_Scheduled_Task::destroy( $task ); |
| 37 |
return; |
| 38 |
} |
| 39 |
|
| 40 |
// save remaining pages of the task. it will be used in ui section. |
| 41 |
$task->pages = $attachments['total_pages'] - 1; |
| 42 |
$task->save(); |
| 43 |
|
| 44 |
foreach ( $attachments['results'] as $attachment ) { |
| 45 |
$attachment->file_path = str_replace( $upload_dir['basedir'], '', $attachment->file_path ); |
| 46 |
$attachment->save(); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Upgrade setting conditions |
| 52 |
* |
| 53 |
* @param WPSC_Scheduled_Task $task - task object. |
| 54 |
* @version 3.1.5 |
| 55 |
* @return void |
| 56 |
*/ |
| 57 |
public static function upgrade_setting_conditions( $task ) { |
| 58 |
|
| 59 |
// Visibility conditions. |
| 60 |
$tff = get_option( 'wpsc-tff' ); |
| 61 |
foreach ( $tff as $slug => $properties ) { |
| 62 |
if ( ! $properties['visibility'] ) { |
| 63 |
continue; |
| 64 |
} |
| 65 |
$tff[ $slug ]['visibility'] = self::upgrade_condition( $properties['visibility'], $properties['relation'] ); |
| 66 |
} |
| 67 |
update_option( 'wpsc-tff', $tff ); |
| 68 |
|
| 69 |
// Email notifications. |
| 70 |
$email_templates = get_option( 'wpsc-email-templates' ); |
| 71 |
foreach ( $email_templates as $index => $properties ) { |
| 72 |
if ( ! $properties['conditions'] ) { |
| 73 |
continue; |
| 74 |
} |
| 75 |
$email_templates[ $index ]['conditions'] = self::upgrade_condition( $properties['conditions'], $properties['relation'] ); |
| 76 |
} |
| 77 |
update_option( 'wpsc-email-templates', $email_templates ); |
| 78 |
|
| 79 |
// Default agent filters. |
| 80 |
$atl_filters = get_option( 'wpsc-atl-default-filters' ); |
| 81 |
foreach ( $atl_filters as $slug => $properties ) { |
| 82 |
if ( ! is_numeric( $slug ) ) { |
| 83 |
continue; |
| 84 |
} |
| 85 |
$atl_filters[ $slug ]['filters'] = self::upgrade_condition( $properties['filters'], 'AND' ); |
| 86 |
} |
| 87 |
update_option( 'wpsc-atl-default-filters', $atl_filters ); |
| 88 |
|
| 89 |
// Default customer filters. |
| 90 |
$ctl_filters = get_option( 'wpsc-ctl-default-filters' ); |
| 91 |
foreach ( $ctl_filters as $slug => $properties ) { |
| 92 |
if ( ! is_numeric( $slug ) ) { |
| 93 |
continue; |
| 94 |
} |
| 95 |
$ctl_filters[ $slug ]['filters'] = self::upgrade_condition( $properties['filters'], 'AND' ); |
| 96 |
} |
| 97 |
update_option( 'wpsc-ctl-default-filters', $ctl_filters ); |
| 98 |
|
| 99 |
// Assigned agent rules filters. |
| 100 |
if ( class_exists( 'WPSC_AAR' ) ) { |
| 101 |
$rules = get_option( 'wpsc-aar-rules', array() ); |
| 102 |
foreach ( $rules as $index => $properties ) { |
| 103 |
$rules[ $index ]['conditions'] = self::upgrade_condition( $properties['conditions'], $properties['relation'] ); |
| 104 |
} |
| 105 |
update_option( 'wpsc-aar-rules', $rules ); |
| 106 |
} |
| 107 |
|
| 108 |
// SLA policy filters. |
| 109 |
if ( class_exists( 'WPSC_SLA' ) ) { |
| 110 |
$policies = get_option( 'wpsc-sla-policies', array() ); |
| 111 |
foreach ( $policies as $index => $properties ) { |
| 112 |
$policies[ $index ]['conditions'] = self::upgrade_condition( $properties['conditions'], $properties['relation'] ); |
| 113 |
} |
| 114 |
update_option( 'wpsc-sla-policies', $policies ); |
| 115 |
} |
| 116 |
|
| 117 |
WPSC_Scheduled_Task::destroy( $task ); |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Upgrade saved filter conditions for all customers |
| 122 |
* |
| 123 |
* @param WPSC_Scheduled_Task $task - task object. |
| 124 |
* @version 3.1.5 |
| 125 |
* @return void |
| 126 |
*/ |
| 127 |
public static function upgrade_saved_filter_conditions( $task ) { |
| 128 |
|
| 129 |
global $wpdb; |
| 130 |
|
| 131 |
$current_page = get_transient( 'wpsc_upgrade_saved_filter_conditions_cursor' ); |
| 132 |
if ( false === $current_page ) { |
| 133 |
|
| 134 |
$total_items = $wpdb->get_var( "SELECT COUNT(id) FROM {$wpdb->prefix}psmsc_customers" ); |
| 135 |
$total_pages = ceil( $total_items / 50 ); |
| 136 |
$task->pages = $total_pages; |
| 137 |
$task->save(); |
| 138 |
|
| 139 |
$current_page = 0; |
| 140 |
set_transient( 'wpsc_upgrade_saved_filter_conditions_cursor', $current_page, MINUTE_IN_SECONDS * 60 * 48 ); |
| 141 |
} |
| 142 |
|
| 143 |
++$current_page; |
| 144 |
|
| 145 |
$customers = WPSC_Customer::find( |
| 146 |
array( |
| 147 |
'items_per_page' => 50, |
| 148 |
'page_no' => $current_page, |
| 149 |
'meta_query' => array( |
| 150 |
'relation' => 'AND', |
| 151 |
array( |
| 152 |
'slug' => 'custom_query', |
| 153 |
'compare' => '=', |
| 154 |
'val' => 'c.user>0', |
| 155 |
), |
| 156 |
), |
| 157 |
) |
| 158 |
); |
| 159 |
|
| 160 |
if ( $customers['results'] ) { |
| 161 |
|
| 162 |
foreach ( $customers['results'] as $customer ) { |
| 163 |
|
| 164 |
$saved_filters = get_user_meta( $customer->user->ID, get_current_blog_id() . '-wpsc-tl-saved-filters', true ); |
| 165 |
if ( ! $saved_filters ) { |
| 166 |
continue; |
| 167 |
} |
| 168 |
|
| 169 |
foreach ( $saved_filters as $key => $filter ) { |
| 170 |
$filters = self::upgrade_condition( str_replace( '^^', '\n', $filter['filters'] ), 'AND' ); |
| 171 |
$saved_filters[ $key ]['filters'] = str_replace( '\n', PHP_EOL, $filters ); |
| 172 |
} |
| 173 |
update_user_meta( $customer->user->ID, get_current_blog_id() . '-wpsc-tl-saved-filters', $saved_filters ); |
| 174 |
} |
| 175 |
} else { |
| 176 |
|
| 177 |
WPSC_Scheduled_Task::destroy( $task ); |
| 178 |
delete_transient( 'wpsc_upgrade_saved_filter_conditions_cursor' ); |
| 179 |
} |
| 180 |
|
| 181 |
if ( $current_page == $task->pages ) { |
| 182 |
|
| 183 |
WPSC_Scheduled_Task::destroy( $task ); |
| 184 |
delete_transient( 'wpsc_upgrade_saved_filter_conditions_cursor' ); |
| 185 |
|
| 186 |
} else { |
| 187 |
|
| 188 |
set_transient( 'wpsc_upgrade_saved_filter_conditions_cursor', $current_page, MINUTE_IN_SECONDS * 60 * 48 ); |
| 189 |
return; |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Upgrade and return conditions json |
| 195 |
* |
| 196 |
* @param string $conditions - conditions json string. |
| 197 |
* @param string $relation - AND or OR. |
| 198 |
* @version 3.1.5 |
| 199 |
* @return string |
| 200 |
*/ |
| 201 |
public static function upgrade_condition( $conditions, $relation ) { |
| 202 |
|
| 203 |
if ( ! $conditions || $conditions == '[]' || $conditions == '{}' ) { |
| 204 |
return '[]'; |
| 205 |
} |
| 206 |
|
| 207 |
$conditions = json_decode( html_entity_decode( $conditions ), true ); |
| 208 |
$slug_arr = array_keys( $conditions ); |
| 209 |
if ( is_numeric( $slug_arr[0] ) ) { |
| 210 |
return wp_json_encode( $conditions ); |
| 211 |
} |
| 212 |
|
| 213 |
$and_conditions = array(); |
| 214 |
$or_conditions = array(); |
| 215 |
|
| 216 |
foreach ( $conditions as $slug => $condition ) { |
| 217 |
if ( preg_match( '/^cf_\w+$/', $slug ) ) { |
| 218 |
$slug = str_replace( 'cf_', '', $slug ); |
| 219 |
} |
| 220 |
$temp = array_merge( array( 'slug' => $slug ), $condition ); |
| 221 |
if ( $relation == 'AND' ) { |
| 222 |
$and_conditions[] = array( $temp ); |
| 223 |
} else { |
| 224 |
$or_conditions[] = $temp; |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
if ( $relation == 'OR' && $or_conditions ) { |
| 229 |
$and_conditions[] = $or_conditions; |
| 230 |
} |
| 231 |
|
| 232 |
return wp_json_encode( $and_conditions ); |
| 233 |
} |
| 234 |
|
| 235 |
/** |
| 236 |
* Repaire setting conditions impacted in v3.1.5 |
| 237 |
* |
| 238 |
* @param WPSC_Scheduled_Task $task - task object. |
| 239 |
* @version 3.1.6 |
| 240 |
* @return void |
| 241 |
*/ |
| 242 |
public static function repaire_setting_conditions( $task ) { |
| 243 |
|
| 244 |
// Visibility conditions. |
| 245 |
$tff = get_option( 'wpsc-tff' ); |
| 246 |
foreach ( $tff as $slug => $properties ) { |
| 247 |
|
| 248 |
$relation = isset( $properties['relation'] ) ? $properties['relation'] : null; |
| 249 |
$tff[ $slug ]['visibility'] = self::convert_repair_conditions( $properties['visibility'], $relation ); |
| 250 |
} |
| 251 |
update_option( 'wpsc-tff', $tff ); |
| 252 |
|
| 253 |
// Email notifications. |
| 254 |
$email_templates = get_option( 'wpsc-email-templates' ); |
| 255 |
foreach ( $email_templates as $index => $properties ) { |
| 256 |
$relation = isset( $properties['relation'] ) ? $properties['relation'] : null; |
| 257 |
$email_templates[ $index ]['conditions'] = self::convert_repair_conditions( $properties['conditions'], $relation ); |
| 258 |
} |
| 259 |
update_option( 'wpsc-email-templates', $email_templates ); |
| 260 |
|
| 261 |
// Default agent filters. |
| 262 |
$atl_filters = get_option( 'wpsc-atl-default-filters' ); |
| 263 |
foreach ( $atl_filters as $slug => $properties ) { |
| 264 |
if ( ! is_numeric( $slug ) ) { |
| 265 |
continue; |
| 266 |
} |
| 267 |
$atl_filters[ $slug ]['filters'] = self::convert_repair_conditions( $properties['filters'] ); |
| 268 |
} |
| 269 |
update_option( 'wpsc-atl-default-filters', $atl_filters ); |
| 270 |
|
| 271 |
// Default customer filters. |
| 272 |
$ctl_filters = get_option( 'wpsc-ctl-default-filters' ); |
| 273 |
foreach ( $ctl_filters as $slug => $properties ) { |
| 274 |
if ( ! is_numeric( $slug ) ) { |
| 275 |
continue; |
| 276 |
} |
| 277 |
$ctl_filters[ $slug ]['filters'] = self::convert_repair_conditions( $properties['filters'] ); |
| 278 |
} |
| 279 |
update_option( 'wpsc-ctl-default-filters', $ctl_filters ); |
| 280 |
|
| 281 |
// Assigned agent rules filters. |
| 282 |
if ( class_exists( 'WPSC_AAR' ) ) { |
| 283 |
$rules = get_option( 'wpsc-aar-rules', array() ); |
| 284 |
foreach ( $rules as $index => $properties ) { |
| 285 |
$relation = isset( $properties['relation'] ) ? $properties['relation'] : null; |
| 286 |
$rules[ $index ]['conditions'] = self::convert_repair_conditions( $properties['conditions'], $relation ); |
| 287 |
} |
| 288 |
update_option( 'wpsc-aar-rules', $rules ); |
| 289 |
} |
| 290 |
|
| 291 |
// SLA policy filters. |
| 292 |
if ( class_exists( 'WPSC_SLA' ) ) { |
| 293 |
$policies = get_option( 'wpsc-sla-policies', array() ); |
| 294 |
foreach ( $policies as $index => $properties ) { |
| 295 |
$relation = isset( $properties['relation'] ) ? $properties['relation'] : null; |
| 296 |
$policies[ $index ]['conditions'] = self::convert_repair_conditions( $properties['conditions'], $relation ); |
| 297 |
} |
| 298 |
update_option( 'wpsc-sla-policies', $policies ); |
| 299 |
} |
| 300 |
|
| 301 |
WPSC_Scheduled_Task::destroy( $task ); |
| 302 |
} |
| 303 |
|
| 304 |
/** |
| 305 |
* Repair saved filter conditions for all customers |
| 306 |
* |
| 307 |
* @param WPSC_Scheduled_Task $task - task object. |
| 308 |
* @version 3.1.6 |
| 309 |
* @return void |
| 310 |
*/ |
| 311 |
public static function repaire_saved_filter_conditions( $task ) { |
| 312 |
|
| 313 |
global $wpdb; |
| 314 |
|
| 315 |
$current_page = get_transient( 'wpsc_repair_saved_filter_conditions_cursor' ); |
| 316 |
if ( false === $current_page ) { |
| 317 |
|
| 318 |
$total_items = $wpdb->get_var( "SELECT COUNT(id) FROM {$wpdb->prefix}psmsc_customers" ); |
| 319 |
$total_pages = ceil( $total_items / 50 ); |
| 320 |
$task->pages = $total_pages; |
| 321 |
$task->save(); |
| 322 |
|
| 323 |
$current_page = 0; |
| 324 |
set_transient( 'wpsc_repair_saved_filter_conditions_cursor', $current_page, MINUTE_IN_SECONDS * 60 * 48 ); |
| 325 |
} |
| 326 |
|
| 327 |
++$current_page; |
| 328 |
|
| 329 |
$customers = WPSC_Customer::find( |
| 330 |
array( |
| 331 |
'items_per_page' => 50, |
| 332 |
'page_no' => $current_page, |
| 333 |
'meta_query' => array( |
| 334 |
'relation' => 'AND', |
| 335 |
array( |
| 336 |
'slug' => 'custom_query', |
| 337 |
'compare' => '=', |
| 338 |
'val' => 'c.user>0', |
| 339 |
), |
| 340 |
), |
| 341 |
) |
| 342 |
); |
| 343 |
|
| 344 |
if ( $customers['results'] ) { |
| 345 |
|
| 346 |
foreach ( $customers['results'] as $customer ) { |
| 347 |
|
| 348 |
$saved_filters = get_user_meta( $customer->user->ID, get_current_blog_id() . '-wpsc-tl-saved-filters', true ); |
| 349 |
if ( ! $saved_filters ) { |
| 350 |
continue; |
| 351 |
} |
| 352 |
|
| 353 |
foreach ( $saved_filters as $key => $filter ) { |
| 354 |
$filters = self::convert_repair_conditions( str_replace( PHP_EOL, '\n', $filter['filters'] ) ); |
| 355 |
$saved_filters[ $key ]['filters'] = str_replace( '\n', PHP_EOL, $filters ); |
| 356 |
} |
| 357 |
update_user_meta( $customer->user->ID, get_current_blog_id() . '-wpsc-tl-saved-filters', $saved_filters ); |
| 358 |
} |
| 359 |
} else { |
| 360 |
|
| 361 |
WPSC_Scheduled_Task::destroy( $task ); |
| 362 |
delete_transient( 'wpsc_repair_saved_filter_conditions_cursor' ); |
| 363 |
} |
| 364 |
|
| 365 |
if ( $current_page == $task->pages ) { |
| 366 |
|
| 367 |
WPSC_Scheduled_Task::destroy( $task ); |
| 368 |
delete_transient( 'wpsc_repair_saved_filter_conditions_cursor' ); |
| 369 |
|
| 370 |
} else { |
| 371 |
|
| 372 |
set_transient( 'wpsc_repair_saved_filter_conditions_cursor', $current_page, MINUTE_IN_SECONDS * 60 * 48 ); |
| 373 |
return; |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
/** |
| 378 |
* Convert repaired conditions to valid format depending on relation |
| 379 |
* |
| 380 |
* @param string $conditions - json string for conditions. |
| 381 |
* @param string $relation - AND or OR relation. |
| 382 |
* @return string |
| 383 |
*/ |
| 384 |
public static function convert_repair_conditions( $conditions, $relation = 'AND' ) { |
| 385 |
|
| 386 |
if ( ! $conditions || $conditions == '[]' ) { |
| 387 |
return '[]'; |
| 388 |
} |
| 389 |
|
| 390 |
$conditions = json_decode( html_entity_decode( $conditions ), true ); |
| 391 |
$slug_arr = array_keys( $conditions ); |
| 392 |
if ( $relation === null && is_numeric( $slug_arr[0] ) ) { |
| 393 |
return wp_json_encode( $conditions ); |
| 394 |
} |
| 395 |
|
| 396 |
$temp = self::repair_conditions( $conditions ); |
| 397 |
$and_conditions = array(); |
| 398 |
$or_conditions = array(); |
| 399 |
foreach ( $temp as $con ) { |
| 400 |
if ( $relation == 'AND' ) { |
| 401 |
$and_conditions[] = array( $con ); |
| 402 |
} else { |
| 403 |
$or_conditions[] = $con; |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
if ( $relation == 'OR' && $or_conditions ) { |
| 408 |
$and_conditions[] = $or_conditions; |
| 409 |
} |
| 410 |
|
| 411 |
return wp_json_encode( $and_conditions ); |
| 412 |
} |
| 413 |
|
| 414 |
/** |
| 415 |
* Repair conditions changed in v3.1.5 |
| 416 |
* |
| 417 |
* @param string $conditions - conditions json string. |
| 418 |
* @version 3.1.6 |
| 419 |
* @return array |
| 420 |
*/ |
| 421 |
public static function repair_conditions( $conditions ) { |
| 422 |
|
| 423 |
if ( ! is_array( $conditions ) ) { |
| 424 |
return array(); |
| 425 |
} |
| 426 |
|
| 427 |
$temp = array(); |
| 428 |
foreach ( $conditions as $key => $con ) { |
| 429 |
|
| 430 |
if ( isset( $con['slug'] ) && ! is_numeric( $con['slug'] ) ) { |
| 431 |
|
| 432 |
$temp[] = $con; |
| 433 |
|
| 434 |
} elseif ( |
| 435 |
! isset( $con['slug'] ) || |
| 436 |
( isset( $con['slug'] ) && is_numeric( $con['slug'] ) ) |
| 437 |
) { |
| 438 |
|
| 439 |
$temp1 = self::repair_conditions( $con ); |
| 440 |
if ( is_array( $temp1 ) && $temp1 ) { |
| 441 |
foreach ( $temp1 as $con1 ) { |
| 442 |
if ( isset( $con1['slug'] ) && ! is_numeric( $con1['slug'] ) ) { |
| 443 |
$temp[] = $con1; |
| 444 |
} |
| 445 |
} |
| 446 |
} |
| 447 |
} |
| 448 |
} |
| 449 |
return $temp; |
| 450 |
} |
| 451 |
|
| 452 |
/** |
| 453 |
* Update attachment status in database |
| 454 |
* |
| 455 |
* @param WPSC_Scheduled_Task $task - task object. |
| 456 |
* @version 3.1.9 |
| 457 |
* @return void |
| 458 |
*/ |
| 459 |
public static function update_ticket_attachment_status( $task ) { |
| 460 |
|
| 461 |
$items_per_page = 100; |
| 462 |
|
| 463 |
if ( ! $task->pages ) { |
| 464 |
$attachments = WPSC_Attachment::find( |
| 465 |
array( |
| 466 |
'items_per_page' => $items_per_page, |
| 467 |
'page_no' => 1, |
| 468 |
) |
| 469 |
); |
| 470 |
|
| 471 |
if ( $attachments['total_items'] === 0 ) { |
| 472 |
WPSC_Scheduled_Task::destroy( $task ); |
| 473 |
return; |
| 474 |
} |
| 475 |
|
| 476 |
$task->pages = $attachments['total_pages']; |
| 477 |
$task->save(); |
| 478 |
} |
| 479 |
|
| 480 |
$attachments = WPSC_Attachment::find( |
| 481 |
array( |
| 482 |
'items_per_page' => $items_per_page, |
| 483 |
'page_no' => $task->pages, |
| 484 |
) |
| 485 |
); |
| 486 |
|
| 487 |
// save remaining pages of the task. it will be used in ui section. |
| 488 |
$task->pages = $task->pages - 1; |
| 489 |
$task->save(); |
| 490 |
|
| 491 |
foreach ( $attachments['results'] as $attachment ) { |
| 492 |
|
| 493 |
if ( ! $attachment->ticket_id && ! $attachment->customer_id ) { |
| 494 |
|
| 495 |
$attachment->is_active = 0; |
| 496 |
|
| 497 |
} elseif ( $attachment->ticket_id ) { |
| 498 |
|
| 499 |
$ticket = new WPSC_Ticket( $attachment->ticket_id ); |
| 500 |
if ( $ticket->id ) { |
| 501 |
$attachment->is_active = 1; |
| 502 |
} else { |
| 503 |
$attachment->is_active = 0; |
| 504 |
} |
| 505 |
} elseif ( $attachment->customer_id ) { |
| 506 |
|
| 507 |
$customer = new WPSC_Customer( $attachment->customer_id ); |
| 508 |
if ( $customer->id ) { |
| 509 |
$attachment->is_active = 1; |
| 510 |
} else { |
| 511 |
$attachment->is_active = 0; |
| 512 |
} |
| 513 |
} |
| 514 |
|
| 515 |
$attachment->save(); |
| 516 |
} |
| 517 |
|
| 518 |
if ( ! $task->pages ) { |
| 519 |
WPSC_Scheduled_Task::destroy( $task ); |
| 520 |
return; |
| 521 |
} |
| 522 |
} |
| 523 |
|
| 524 |
/** |
| 525 |
* Update image editor attachment path in thread body |
| 526 |
* |
| 527 |
* @param WPSC_Scheduled_Task $task - task object. |
| 528 |
* @version 3.1.9 |
| 529 |
* @return void |
| 530 |
*/ |
| 531 |
public static function upgrade_ie_attachment_path( $task ) { |
| 532 |
|
| 533 |
// Check if task has pages. |
| 534 |
if ( ! $task->pages ) { |
| 535 |
|
| 536 |
$attachments = WPSC_Attachment::find( |
| 537 |
array( |
| 538 |
'items_per_page' => 50, |
| 539 |
'page_no' => 1, |
| 540 |
'meta_query' => array( |
| 541 |
'relation' => 'AND', |
| 542 |
array( |
| 543 |
'slug' => 'source', |
| 544 |
'compare' => '=', |
| 545 |
'val' => 'img_editor', |
| 546 |
), |
| 547 |
array( |
| 548 |
'slug' => 'is_active', |
| 549 |
'compare' => '=', |
| 550 |
'val' => '1', |
| 551 |
), |
| 552 |
), |
| 553 |
) |
| 554 |
); |
| 555 |
|
| 556 |
// If no attachments found then destroy the task. |
| 557 |
if ( $attachments['total_items'] === 0 ) { |
| 558 |
WPSC_Scheduled_Task::destroy( $task ); |
| 559 |
return; |
| 560 |
} |
| 561 |
|
| 562 |
// Save total pages of the task. |
| 563 |
$task->pages = $attachments['total_pages']; |
| 564 |
$task->save(); |
| 565 |
} |
| 566 |
|
| 567 |
$attachments = WPSC_Attachment::find( |
| 568 |
array( |
| 569 |
'items_per_page' => 50, |
| 570 |
'page_no' => $task->pages, |
| 571 |
'meta_query' => array( |
| 572 |
'relation' => 'AND', |
| 573 |
array( |
| 574 |
'slug' => 'source', |
| 575 |
'compare' => '=', |
| 576 |
'val' => 'img_editor', |
| 577 |
), |
| 578 |
array( |
| 579 |
'slug' => 'is_active', |
| 580 |
'compare' => '=', |
| 581 |
'val' => '1', |
| 582 |
), |
| 583 |
), |
| 584 |
) |
| 585 |
); |
| 586 |
|
| 587 |
// Save remaining pages of the task. it will be used in ui section. |
| 588 |
$task->pages = $task->pages - 1; |
| 589 |
$task->save(); |
| 590 |
foreach ( $attachments['results'] as $attachment ) { |
| 591 |
|
| 592 |
// Check if valid ticket. |
| 593 |
$ticket = new WPSC_Ticket( $attachment->ticket_id ); |
| 594 |
if ( ! $ticket->id ) { |
| 595 |
continue; |
| 596 |
} |
| 597 |
|
| 598 |
// Check if valid thread. |
| 599 |
$thread = new WPSC_Thread( $attachment->source_id ); |
| 600 |
if ( ! $thread->id ) { |
| 601 |
continue; |
| 602 |
} |
| 603 |
|
| 604 |
// Check if wpsc_attachment is present and auth_code is not present. |
| 605 |
if ( preg_match_all( '/' . preg_quote( home_url( '/' ), '/' ) . '\?wpsc_attachment=(\d+)(?!&auth_code=)([^"]*)/', $thread->body, $matches ) ) { |
| 606 |
|
| 607 |
// Update the attachment url with auth_code and save the thread. |
| 608 |
foreach ( $matches[0] as $url ) { |
| 609 |
|
| 610 |
if ( strpos( $url, 'auth_code=' ) === false ) { |
| 611 |
$new_url = add_query_arg( 'auth_code', $ticket->auth_code, $url ); |
| 612 |
$thread->body = str_replace( $url, $new_url, $thread->body ); |
| 613 |
$thread->save(); |
| 614 |
} |
| 615 |
} |
| 616 |
} |
| 617 |
} |
| 618 |
// If no pages left then destroy the task. |
| 619 |
if ( ! $task->pages ) { |
| 620 |
WPSC_Scheduled_Task::destroy( $task ); |
| 621 |
return; |
| 622 |
} |
| 623 |
} |
| 624 |
} |
| 625 |
endif; |
| 626 |
|