| 1 | <?php |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | defined( 'ZEROBSCRM_PATH' ) || exit( 0 ); |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | global $zeroBSCRM_migrations; $zeroBSCRM_migrations = array( |
| 20 | '288', |
| 21 | '2963', |
| 22 | '29999', |
| 23 | '411', |
| 24 | '50', |
| 25 | '53', |
| 26 | '54', |
| 27 | '543', |
| 28 | '544', |
| 29 | '55', |
| 30 | '55a', |
| 31 | '551', |
| 32 | '560', |
| 33 | 'task_offset_fix', |
| 34 | 'refresh_user_roles', |
| 35 | 'regenerate_tag_slugs', |
| 36 | 'create_workflows_table', |
| 37 | 'invoice_language_fixes', |
| 38 | 'gh3465_increase_city_field_size', |
| 39 | 'tax_rate_precision_fix', |
| 40 | 'ensure_notifications_table', |
| 41 | ); |
| 42 | |
| 43 | global $zeroBSCRM_migrations_requirements; |
| 44 | $zeroBSCRM_migrations_requirements = array( |
| 45 | '288' => array( 'postsettings' ), |
| 46 | '53' => array( 'postsettings' ), |
| 47 | '5402' => array( 'postsettings' ), |
| 48 | '55a' => array( 'wp_loaded' ), |
| 49 | ); |
| 50 | |
| 51 | |
| 52 | function zeroBSCRM_migrations_markComplete( $migrationKey = -1, $logObj = false ) { |
| 53 | |
| 54 | global $zeroBSCRM_migrations; |
| 55 | |
| 56 | if ( ! empty( $migrationKey ) && in_array( $migrationKey, $zeroBSCRM_migrations ) ) { |
| 57 | |
| 58 | $completedMigrations = zeroBSCRM_migrations_getCompleted(); |
| 59 | $completedMigrations[] = $migrationKey; |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | update_option( 'zbsmigrations', $completedMigrations, false ); |
| 66 | |
| 67 | |
| 68 | update_option( |
| 69 | 'zbsmigration' . $migrationKey, |
| 70 | array( |
| 71 | 'completed' => time(), |
| 72 | 'meta' => $logObj, |
| 73 | ), |
| 74 | false |
| 75 | ); |
| 76 | |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | |
| 81 | function zeroBSCRM_migrations_getCompleted() { |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | global $zbs; |
| 91 | $migrations = $zbs->settings->get( 'migrations' ); if ( isset( $migrations ) && is_array( $migrations ) && count( $migrations ) > 0 ) { |
| 92 | |
| 93 | $existingMigrationsMigration = get_option( 'zbsmigrationsdal', -1 ); |
| 94 | |
| 95 | if ( $existingMigrationsMigration == -1 ) { |
| 96 | |
| 97 | |
| 98 | update_option( 'zbsmigrations', $migrations, false ); |
| 99 | update_option( 'zbsmigrationsdal', 2, false ); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | |
| 104 | return get_option( 'zbsmigrations', array() ); |
| 105 | } |
| 106 | |
| 107 | |
| 108 | function jpcrm_migrations_get_migration( $migrationKey = '' ) { |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | $finished = false; |
| 115 | $migrations = zeroBSCRM_migrations_getCompleted(); |
| 116 | if ( in_array( $migrationKey, $migrations ) ) { |
| 117 | $finished = true; |
| 118 | } |
| 119 | |
| 120 | return array( $finished, get_option( 'zbsmigration' . $migrationKey, false ) ); |
| 121 | } |
| 122 | |
| 123 | function zeroBSCRM_migrations_run( $settingsArr = false, $run_at = 'init' ) { |
| 124 | |
| 125 | global $zeroBSCRM_migrations, $zeroBSCRM_migrations_requirements; |
| 126 | |
| 127 | |
| 128 | if ( jpcrm_perms_manage_options() && isset( $_GET['resetmigrationblock'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ?? '' ) ), 'resetmigrationblock' ) ) { |
| 129 | |
| 130 | |
| 131 | delete_option( 'zbsmigrationpreloadcatch' ); |
| 132 | delete_option( 'zbsmigrationblockerrors' ); |
| 133 | |
| 134 | |
| 135 | $migrationBlocksRemoved = true; |
| 136 | } |
| 137 | |
| 138 | |
| 139 | $blockingErrs = get_option( 'zbsmigrationblockerrors', false ); |
| 140 | if ( $blockingErrs !== false && ! empty( $blockingErrs ) ) { |
| 141 | return false; |
| 142 | } |
| 143 | |
| 144 | |
| 145 | $migratedAlreadyArr = zeroBSCRM_migrations_getCompleted(); |
| 146 | |
| 147 | |
| 148 | $migrationRunCount = 0; |
| 149 | |
| 150 | |
| 151 | if ( count( $zeroBSCRM_migrations ) > 0 ) { |
| 152 | foreach ( $zeroBSCRM_migrations as $migration ) { |
| 153 | |
| 154 | if ( ! in_array( $migration, $migratedAlreadyArr ) && function_exists( 'zeroBSCRM_migration_' . $migration ) ) { |
| 155 | |
| 156 | $run = true; |
| 157 | |
| 158 | |
| 159 | if ( isset( $zeroBSCRM_migrations_requirements[ $migration ] ) ) { |
| 160 | |
| 161 | |
| 162 | |
| 163 | if ( in_array( 'preload', $zeroBSCRM_migrations_requirements[ $migration ] ) && $migrationRunCount > 0 ) { |
| 164 | |
| 165 | |
| 166 | $previousAttempts = get_option( 'zbsmigrationpreloadcatch', array() ); |
| 167 | if ( ! is_array( $previousAttempts ) ) { |
| 168 | $previousAttempts = array(); |
| 169 | } |
| 170 | if ( ! isset( $previousAttempts[ $migration ] ) ) { |
| 171 | $previousAttempts[ $migration ] = 1; |
| 172 | } |
| 173 | if ( $previousAttempts[ $migration ] < 5 ) { |
| 174 | |
| 175 | |
| 176 | ++$previousAttempts[ $migration ]; |
| 177 | update_option( 'zbsmigrationpreloadcatch', $previousAttempts, false ); |
| 178 | |
| 179 | |
| 180 | header( 'Refresh:0' ); |
| 181 | exit( 0 ); |
| 182 | |
| 183 | } else { |
| 184 | |
| 185 | |
| 186 | update_option( 'zbsmigrationblockerrors', $migration, false ); |
| 187 | |
| 188 | |
| 189 | add_action( 'after-zerobscrm-admin-init', 'zeroBSCRM_adminNotices_majorMigrationError' ); |
| 190 | add_action( 'admin_notices', 'zeroBSCRM_adminNotices_majorMigrationError' ); |
| 191 | |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | |
| 196 | foreach ( $zeroBSCRM_migrations_requirements[ $migration ] as $check ) { |
| 197 | |
| 198 | |
| 199 | |
| 200 | if ( $check !== 'preload' && $check !== 'wp_loaded' ) { |
| 201 | |
| 202 | $checkFuncName = 'zeroBSCRM_migrations_checks_' . $check; |
| 203 | if ( ! call_user_func( $checkFuncName ) ) { |
| 204 | $run = false; |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | |
| 210 | if ( in_array( 'wp_loaded', $zeroBSCRM_migrations_requirements[ $migration ] ) ) { |
| 211 | |
| 212 | $run = false; |
| 213 | |
| 214 | if ( $run_at == 'wp_loaded' ) { |
| 215 | $run = true; |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | |
| 221 | if ( $run ) { |
| 222 | |
| 223 | |
| 224 | call_user_func( 'zeroBSCRM_migration_' . $migration ); |
| 225 | |
| 226 | |
| 227 | ++$migrationRunCount; |
| 228 | |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | function zeroBSCRM_migrations_checks_postsettings() { |
| 236 | |
| 237 | global $zbs; |
| 238 | |
| 239 | |
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | try { |
| 246 | $potentialDBSetting = $zbs->DAL->getSetting( |
| 247 | array( |
| 248 | 'key' => 'settingsinstalled', |
| 249 | 'fullDetails' => false, |
| 250 | ) |
| 251 | ); |
| 252 | |
| 253 | if ( isset( $potentialDBSetting ) && $potentialDBSetting > 0 ) { |
| 254 | |
| 255 | return true; |
| 256 | |
| 257 | } |
| 258 | } catch ( Exception $e ) { |
| 259 | |
| 260 | } |
| 261 | |
| 262 | return false; |
| 263 | } |
| 264 | |
| 265 | |
| 266 | function zeroBSCRM_adminNotices_majorMigrationError() { |
| 267 | |
| 268 | |
| 269 | if ( get_current_user_id() > 0 ) { |
| 270 | |
| 271 | |
| 272 | $msgSent = get_transient( 'zbs-migration-general-errors' ); |
| 273 | if ( ! $msgSent ) { |
| 274 | |
| 275 | zeroBSCRM_notifyme_insert_notification( get_current_user_id(), -999, -1, 'migration.blocked.errors', 'migration.blocked.errors' ); |
| 276 | set_transient( 'zbs-migration-general-errors', 20, 24 * 7 * HOUR_IN_SECONDS ); |
| 277 | |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | |
| 283 | |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | |
| 289 | |
| 290 | |
| 291 | |
| 292 | |
| 293 | |
| 294 | |
| 295 | function zeroBSCRM_migration_288() { |
| 296 | |
| 297 | global $zbs; |
| 298 | |
| 299 | zeroBSCRM_portal_checkCreatePage(); |
| 300 | |
| 301 | zeroBSCRM_migrations_markComplete( '288', array( 'updated' => '1' ) ); |
| 302 | } |
| 303 | |
| 304 | |
| 305 | |
| 306 | |
| 307 | |
| 308 | |
| 309 | |
| 310 | function zeroBSCRM_migration_2963() { |
| 311 | |
| 312 | global $zbs, $wpdb, $ZBSCRM_t; |
| 313 | |
| 314 | |
| 315 | zeroBSCRM_checkTablesExist(); |
| 316 | |
| 317 | |
| 318 | zeroBSCRM_populateEmailTemplateList(); |
| 319 | |
| 320 | |
| 321 | |
| 322 | |
| 323 | |
| 324 | $from_name = zeroBSCRM_mailDelivery_defaultFromname(); |
| 325 | |
| 326 | |
| 327 | |
| 328 | |
| 329 | |
| 330 | |
| 331 | $deliveryMethod = zeroBSCRM_getMailDeliveryDefault(); |
| 332 | |
| 333 | $ID = 6; |
| 334 | $reply_to = ''; |
| 335 | $cc = ''; |
| 336 | $bcc = ''; |
| 337 | |
| 338 | |
| 339 | $subject = __( 'Your Client Portal Password', 'zero-bs-crm' ); |
| 340 | $content = zeroBSCRM_mail_retrieveDefaultBodyTemplate( 'clientportalpwreset' ); |
| 341 | $active = 1; |
| 342 | if ( zeroBSCRM_mailTemplate_exists( $ID ) == 0 ) { |
| 343 | $content = zeroBSCRM_mailTemplate_processEmailHTML( $content ); |
| 344 | |
| 345 | zeroBSCRM_insertEmailTemplate( $ID, $deliveryMethod, $bcc, $subject, $content, $active ); |
| 346 | } |
| 347 | |
| 348 | |
| 349 | |
| 350 | |
| 351 | |
| 352 | |
| 353 | |
| 354 | $from_name = zeroBSCRM_mailDelivery_defaultFromname(); |
| 355 | |
| 356 | |
| 357 | |
| 358 | |
| 359 | |
| 360 | |
| 361 | $deliveryMethod = zeroBSCRM_getMailDeliveryDefault(); |
| 362 | |
| 363 | $ID = 6; |
| 364 | $reply_to = ''; |
| 365 | $cc = ''; |
| 366 | $bcc = ''; |
| 367 | |
| 368 | |
| 369 | $wpdb->delete( $ZBSCRM_t['system_mail_templates'], array( 'zbsmail_id' => $ID ) ); |
| 370 | |
| 371 | |
| 372 | $subject = __( 'Your Client Portal Password', 'zero-bs-crm' ); |
| 373 | $content = zeroBSCRM_mail_retrieveDefaultBodyTemplate( 'clientportalpwreset' ); |
| 374 | |
| 375 | $active = 1; |
| 376 | if ( zeroBSCRM_mailTemplate_exists( $ID ) == 0 ) { |
| 377 | $content = zeroBSCRM_mailTemplate_processEmailHTML( $content ); |
| 378 | |
| 379 | zeroBSCRM_insertEmailTemplate( $ID, $deliveryMethod, $bcc, $subject, $content, $active ); |
| 380 | } |
| 381 | |
| 382 | |
| 383 | |
| 384 | |
| 385 | |
| 386 | |
| 387 | |
| 388 | $from_name = zeroBSCRM_mailDelivery_defaultFromname(); |
| 389 | |
| 390 | |
| 391 | |
| 392 | |
| 393 | |
| 394 | |
| 395 | $deliveryMethod = zeroBSCRM_getMailDeliveryDefault(); |
| 396 | |
| 397 | $ID = 7; |
| 398 | $reply_to = ''; |
| 399 | $cc = ''; |
| 400 | $bcc = ''; |
| 401 | |
| 402 | |
| 403 | $subject = __( 'Your Statement', 'zero-bs-crm' ); |
| 404 | $content = zeroBSCRM_mail_retrieveDefaultBodyTemplate( 'invoicestatementsent' ); |
| 405 | |
| 406 | |
| 407 | $wpdb->delete( $ZBSCRM_t['system_mail_templates'], array( 'zbsmail_id' => $ID ) ); |
| 408 | |
| 409 | $active = 1; |
| 410 | if ( zeroBSCRM_mailTemplate_exists( $ID ) == 0 ) { |
| 411 | $content = zeroBSCRM_mailTemplate_processEmailHTML( $content ); |
| 412 | |
| 413 | zeroBSCRM_insertEmailTemplate( $ID, $deliveryMethod, $bcc, $subject, $content, $active ); |
| 414 | } |
| 415 | |
| 416 | |
| 417 | |
| 418 | |
| 419 | |
| 420 | |
| 421 | for ( $i = 0; $i <= 7; $i++ ) { |
| 422 | |
| 423 | |
| 424 | $sql = $wpdb->prepare( 'SELECT ID FROM ' . $ZBSCRM_t['system_mail_templates'] . ' WHERE zbsmail_id = %d GROUP BY ID ORDER BY zbsmail_id DESC, zbsmail_lastupdated DESC', $i ); |
| 425 | $r = $wpdb->get_results( $sql, ARRAY_A ); |
| 426 | |
| 427 | |
| 428 | if ( is_array( $r ) && count( $r ) > 1 ) { |
| 429 | |
| 430 | $count = 0; |
| 431 | |
| 432 | |
| 433 | foreach ( $r as $x ) { |
| 434 | |
| 435 | |
| 436 | if ( $count > 0 ) { |
| 437 | |
| 438 | |
| 439 | $wpdb->delete( $ZBSCRM_t['system_mail_templates'], array( 'ID' => $x['ID'] ) ); |
| 440 | |
| 441 | } |
| 442 | |
| 443 | ++$count; |
| 444 | |
| 445 | } |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | |
| 450 | |
| 451 | |
| 452 | |
| 453 | |
| 454 | $existingTemplate = $wpdb->get_var( 'SELECT zbsmail_body FROM ' . $ZBSCRM_t['system_mail_templates'] . ' WHERE ID = 6' ); |
| 455 | |
| 456 | |
| 457 | $newTemplate = zeroBSCRM_mail_retrieveDefaultBodyTemplate( 'tasknotification' ); |
| 458 | |
| 459 | |
| 460 | if ( $existingTemplate !== $newTemplate ) { |
| 461 | update_option( 'jpcrm_tasknotificationtemplate', $existingTemplate, false ); |
| 462 | } |
| 463 | |
| 464 | |
| 465 | $sql = 'UPDATE ' . $ZBSCRM_t['system_mail_templates'] . ' SET zbsmail_body = %s WHERE ID = 6'; |
| 466 | $q = $wpdb->prepare( $sql, array( $newTemplate ) ); |
| 467 | $wpdb->query( $q ); |
| 468 | |
| 469 | |
| 470 | |
| 471 | |
| 472 | |
| 473 | if ( $zbs->DAL->invoices->getFullCount() > 0 ) { |
| 474 | |
| 475 | $zbs->settings->update( 'reftype', 'manual' ); |
| 476 | } |
| 477 | |
| 478 | |
| 479 | global $ZBSCRM_t, $wpdb; |
| 480 | |
| 481 | |
| 482 | $existingTemplate = $wpdb->get_var( 'SELECT zbsmail_body FROM ' . $ZBSCRM_t['system_mail_templates'] . ' WHERE ID = 4' ); |
| 483 | |
| 484 | |
| 485 | $newTemplate = zeroBSCRM_mail_retrieveDefaultBodyTemplate( 'invoicesent' ); |
| 486 | |
| 487 | |
| 488 | if ( $existingTemplate !== $newTemplate ) { |
| 489 | update_option( 'jpcrm_invnotificationtemplate', $existingTemplate, false ); |
| 490 | } |
| 491 | |
| 492 | |
| 493 | $sql = 'UPDATE ' . $ZBSCRM_t['system_mail_templates'] . ' SET zbsmail_body = %s WHERE ID = 4'; |
| 494 | $q = $wpdb->prepare( $sql, array( $newTemplate ) ); |
| 495 | $wpdb->query( $q ); |
| 496 | |
| 497 | |
| 498 | |
| 499 | zeroBSCRM_migrations_markComplete( '2963', array( 'updated' => '1' ) ); |
| 500 | } |
| 501 | |
| 502 | |
| 503 | |
| 504 | |
| 505 | function zeroBSCRM_migration_29999() { |
| 506 | |
| 507 | |
| 508 | |
| 509 | jpcrm_flag_for_flush_rewrite(); |
| 510 | |
| 511 | |
| 512 | zeroBSCRM_migrations_markComplete( '29999', array( 'updated' => 1 ) ); |
| 513 | } |
| 514 | |
| 515 | |
| 516 | |
| 517 | |
| 518 | |
| 519 | |
| 520 | |
| 521 | function zeroBSCRM_migration_411() { |
| 522 | |
| 523 | $wp_uploads_dir = wp_upload_dir(); |
| 524 | |
| 525 | |
| 526 | |
| 527 | $directories = array( |
| 528 | |
| 529 | ZEROBSCRM_PATH . 'templates/', |
| 530 | ZEROBSCRM_PATH . 'templates/emails/', |
| 531 | ZEROBSCRM_PATH . 'templates/invoices/', |
| 532 | ZEROBSCRM_PATH . 'templates/quotes/', |
| 533 | |
| 534 | $wp_uploads_dir['basedir'] . '/' . 'zbscrm-store/_wip/', |
| 535 | |
| 536 | ); |
| 537 | |
| 538 | |
| 539 | foreach ( $directories as $directory ) { |
| 540 | jpcrm_create_and_secure_dir_from_external_access( $directory, true ); |
| 541 | } |
| 542 | |
| 543 | jpcrm_create_and_secure_dir_from_external_access( $wp_uploads_dir['basedir'] . '/' . 'zbscrm-store/', false ); |
| 544 | |
| 545 | |
| 546 | zeroBSCRM_migrations_markComplete( '411', array( 'updated' => 1 ) ); |
| 547 | } |
| 548 | |
| 549 | |
| 550 | |
| 551 | |
| 552 | function zeroBSCRM_migration_50() { |
| 553 | |
| 554 | global $zbs, $wpdb, $ZBSCRM_t; |
| 555 | |
| 556 | |
| 557 | if ( ! zeroBSCRM_migration_tableHasColumn( $ZBSCRM_t['externalsources'], 'zbss_origin' ) ) { |
| 558 | |
| 559 | $sql = 'ALTER TABLE ' . $ZBSCRM_t['externalsources'] . ' ADD COLUMN `zbss_origin` VARCHAR(400) NULL DEFAULT NULL AFTER `zbss_uid`, ADD INDEX (zbss_origin);'; |
| 560 | $wpdb->query( $sql ); |
| 561 | |
| 562 | } |
| 563 | |
| 564 | |
| 565 | |
| 566 | |
| 567 | $transaction_statuses = zeroBSCRM_getTransactionsStatuses( true ); |
| 568 | $deleted_string = __( 'Deleted', 'zero-bs-crm' ); |
| 569 | if ( ! in_array( $deleted_string, $transaction_statuses ) ) { |
| 570 | $transaction_statuses[] = $deleted_string; |
| 571 | } |
| 572 | $transaction_statuses_str = implode( ',', $transaction_statuses ); |
| 573 | |
| 574 | |
| 575 | $customisedFields = $zbs->settings->get( 'customisedfields' ); |
| 576 | $customisedFields['transactions']['status'][1] = $transaction_statuses_str; |
| 577 | $zbs->settings->update( 'customisedfields', $customisedFields ); |
| 578 | |
| 579 | |
| 580 | zeroBSCRM_migrations_markComplete( '50', array( 'updated' => 1 ) ); |
| 581 | } |
| 582 | |
| 583 | |
| 584 | |
| 585 | |
| 586 | function zeroBSCRM_migration_53() { |
| 587 | |
| 588 | global $zbs; |
| 589 | |
| 590 | |
| 591 | |
| 592 | |
| 593 | if ( ! function_exists( 'zeroBSCRM_encrypt' ) ) { |
| 594 | require ZEROBSCRM_INCLUDE_PATH . 'ZeroBSCRM.Encryption.php'; |
| 595 | } |
| 596 | |
| 597 | |
| 598 | $zbs->load_encryption(); |
| 599 | |
| 600 | |
| 601 | $successful_recryptions = 0; |
| 602 | |
| 603 | |
| 604 | |
| 605 | |
| 606 | $decryption_key = hex2bin( zeroBSCRM_getSetting( 'smtpkey' ) ); |
| 607 | |
| 608 | |
| 609 | $existing_mail_delivery_methods = zeroBSCRM_getSetting( 'smtpaccs' ); |
| 610 | if ( ! is_array( $existing_mail_delivery_methods ) ) { |
| 611 | $existing_mail_delivery_methods = array(); |
| 612 | } |
| 613 | |
| 614 | |
| 615 | $replacement_delivery_methods = array(); |
| 616 | foreach ( $existing_mail_delivery_methods as $method_key => $method_array ) { |
| 617 | |
| 618 | $updated_method_array = $method_array; |
| 619 | |
| 620 | if ( isset( $method_array['pass'] ) ) { |
| 621 | |
| 622 | |
| 623 | $password = zeroBSCRM_encryption_unsafe_process( 'decrypt', $method_array['pass'], $decryption_key, zeroBSCRM_get_iv( true ), true ); |
| 624 | |
| 625 | |
| 626 | |
| 627 | if ( ! $password ) { |
| 628 | $password = zeroBSCRM_encryption_unsafe_process( 'decrypt', $method_array['pass'], $decryption_key, $decryption_key, true ); |
| 629 | } |
| 630 | |
| 631 | if ( $password ) { |
| 632 | |
| 633 | |
| 634 | $updated_method_array['pass'] = $zbs->encryption->encrypt( $password, 'smtp' ); |
| 635 | |
| 636 | } else { |
| 637 | |
| 638 | |
| 639 | $updated_method_array['pass'] = $method_array['pass']; |
| 640 | |
| 641 | } |
| 642 | |
| 643 | ++$successful_recryptions; |
| 644 | |
| 645 | } |
| 646 | |
| 647 | $replacement_delivery_methods[ $method_key ] = $updated_method_array; |
| 648 | |
| 649 | } |
| 650 | |
| 651 | |
| 652 | $zbs->settings->update( 'smtpaccs', $replacement_delivery_methods ); |
| 653 | |
| 654 | |
| 655 | |
| 656 | |
| 657 | |
| 658 | $api_secret = $zbs->DAL->setting( 'api_secret' ); |
| 659 | if ( str_starts_with( $api_secret, 'zbscrm_' ) ) { |
| 660 | $hashed_api_secret = $zbs->encryption->hash( $api_secret ); |
| 661 | $zbs->DAL->updateSetting( 'api_secret', $hashed_api_secret ); |
| 662 | } |
| 663 | |
| 664 | global $wpdb, $ZBSCRM_t; |
| 665 | |
| 666 | if ( jpcrm_database_server_has_ability( 'fulltext_index' ) && ! jpcrm_migration_table_has_index( $ZBSCRM_t['customfields'], 'search' ) ) { |
| 667 | $sql = 'ALTER TABLE ' . $ZBSCRM_t['customfields'] . ' ADD FULLTEXT INDEX `search` (`zbscf_objval`);'; |
| 668 | $wpdb->query( $sql ); |
| 669 | } |
| 670 | if ( ! jpcrm_migration_table_has_index( $ZBSCRM_t['taglinks'], 'zbstl_tagid+zbstl_objtype' ) ) { |
| 671 | $sql = 'ALTER TABLE ' . $ZBSCRM_t['taglinks'] . ' ADD INDEX `zbstl_tagid+zbstl_objtype` (`zbstl_tagid`,`zbstl_objtype`) USING BTREE;'; |
| 672 | $wpdb->query( $sql ); |
| 673 | } |
| 674 | if ( ! jpcrm_migration_table_has_index( $ZBSCRM_t['externalsources'], 'zbss_uid+zbss_source+zbss_objtype' ) ) { |
| 675 | $sql = 'ALTER TABLE ' . $ZBSCRM_t['externalsources'] . ' ADD INDEX `zbss_uid+zbss_source+zbss_objtype` (`zbss_uid`,`zbss_source`,`zbss_objtype`) USING BTREE;'; |
| 676 | $wpdb->query( $sql ); |
| 677 | } |
| 678 | if ( ! jpcrm_migration_table_has_index( $ZBSCRM_t['meta'], 'zbsm_objid+zbsm_key+zbsm_objtype' ) ) { |
| 679 | $sql = 'ALTER TABLE ' . $ZBSCRM_t['meta'] . ' ADD INDEX `zbsm_objid+zbsm_key+zbsm_objtype` (`zbsm_objid`,`zbsm_key`,`zbsm_objtype`) USING BTREE;'; |
| 680 | $wpdb->query( $sql ); |
| 681 | } |
| 682 | if ( ! jpcrm_migration_table_has_index( $ZBSCRM_t['logs'], 'zbsl_created' ) ) { |
| 683 | $sql = 'ALTER TABLE ' . $ZBSCRM_t['logs'] . ' ADD INDEX `zbsl_created` (`zbsl_created`) USING BTREE;'; |
| 684 | $wpdb->query( $sql ); |
| 685 | } |
| 686 | if ( ! jpcrm_migration_table_has_index( $ZBSCRM_t['contacts'], 'zbsc_status' ) ) { |
| 687 | $sql = 'ALTER TABLE ' . $ZBSCRM_t['contacts'] . ' ADD INDEX `zbsc_status` (`zbsc_status`) USING BTREE;'; |
| 688 | $wpdb->query( $sql ); |
| 689 | } |
| 690 | |
| 691 | |
| 692 | $wp_uploads_dir = wp_upload_dir(); |
| 693 | $errant_htaccess = $wp_uploads_dir['basedir'] . '/' . 'zbscrm-store/.htaccess'; |
| 694 | if ( file_exists( $errant_htaccess ) ) { |
| 695 | unlink( $errant_htaccess ); |
| 696 | } |
| 697 | |
| 698 | zeroBSCRM_migrations_markComplete( '53', array( 'updated' => $successful_recryptions ) ); |
| 699 | } |
| 700 | |
| 701 | |
| 702 | |
| 703 | |
| 704 | |
| 705 | |
| 706 | function zeroBSCRM_migration_54() { |
| 707 | |
| 708 | global $zbs, $wpdb, $ZBSCRM_t; |
| 709 | |
| 710 | |
| 711 | if ( ! zeroBSCRM_migration_tableHasColumn( $ZBSCRM_t['logs'], 'zbsl_pinned' ) ) { |
| 712 | $sql = 'ALTER TABLE ' . $ZBSCRM_t['logs'] . ' ADD `zbsl_pinned` int(1) NULL AFTER `zbsl_longdesc`;'; |
| 713 | $wpdb->query( $sql ); |
| 714 | } |
| 715 | |
| 716 | |
| 717 | $outdated_log_meta_count = (int) $wpdb->get_var( 'SELECT COUNT(ID) FROM ' . $ZBSCRM_t['meta'] . ' WHERE zbsm_objtype = 8 AND zbsm_key = "logmeta"' ); |
| 718 | |
| 719 | if ( $outdated_log_meta_count > 0 ) { |
| 720 | |
| 721 | |
| 722 | $outdated_log_meta_records = $wpdb->get_results( 'SELECT * FROM ' . $ZBSCRM_t['meta'] . ' WHERE zbsm_objtype = 8 AND zbsm_key = "logmeta" ORDER BY ID DESC LIMIT 5000' ); |
| 723 | |
| 724 | foreach ( $outdated_log_meta_records as $log_record ) { |
| 725 | |
| 726 | |
| 727 | $log_meta = $zbs->DAL->decodeIfJSON( $zbs->DAL->stripSlashes( $log_record->zbsm_val ) ); |
| 728 | |
| 729 | |
| 730 | if ( is_array( $log_meta ) ) { |
| 731 | |
| 732 | foreach ( $log_meta as $key => $value ) { |
| 733 | |
| 734 | $zbs->DAL->updateMeta( ZBS_TYPE_LOG, $log_record->zbsm_objid, $zbs->DAL->makeSlug( $key ), $value ); |
| 735 | |
| 736 | } |
| 737 | } |
| 738 | |
| 739 | |
| 740 | zeroBSCRM_db2_deleteGeneric( $log_record->ID, 'meta' ); |
| 741 | |
| 742 | } |
| 743 | |
| 744 | |
| 745 | $outdated_log_meta_count = (int) $wpdb->get_var( 'SELECT COUNT(ID) FROM ' . $ZBSCRM_t['meta'] . ' WHERE zbsm_objtype = 8 AND zbsm_key = "logmeta"' ); |
| 746 | |
| 747 | } |
| 748 | |
| 749 | if ( $outdated_log_meta_count == 0 ) { |
| 750 | |
| 751 | |
| 752 | zeroBSCRM_migrations_markComplete( '54', array( 'updated' => 1 ) ); |
| 753 | |
| 754 | } |
| 755 | } |
| 756 | |
| 757 | |
| 758 | |
| 759 | |
| 760 | |
| 761 | function zeroBSCRM_migration_543() { |
| 762 | |
| 763 | $root_storage = zeroBSCRM_privatisedDirCheck(); |
| 764 | if ( $root_storage !== false ) { |
| 765 | $recursive_file_iterator = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $root_storage['path'] ) ); |
| 766 | $htaccess_files = array(); |
| 767 | |
| 768 | foreach ( $recursive_file_iterator as $file ) { |
| 769 | if ( $file->isDir() || $file->getBasename() != '.htaccess' ) { |
| 770 | continue; |
| 771 | } |
| 772 | $htaccess_files[] = $file->getPathname(); |
| 773 | } |
| 774 | |
| 775 | foreach ( $htaccess_files as $errant_htaccess ) { |
| 776 | if ( is_file( $errant_htaccess ) ) { |
| 777 | unlink( $errant_htaccess ); |
| 778 | } |
| 779 | } |
| 780 | } |
| 781 | |
| 782 | |
| 783 | zeroBSCRM_migrations_markComplete( '543', array( 'updated' => 1 ) ); |
| 784 | } |
| 785 | |
| 786 | |
| 787 | |
| 788 | |
| 789 | |
| 790 | function zeroBSCRM_migration_544() { |
| 791 | |
| 792 | global $zbs; |
| 793 | |
| 794 | |
| 795 | $shouldBeInstalled = zeroBSCRM_getSetting( 'feat_pdfinv' ); |
| 796 | if ( $shouldBeInstalled == '1' ) { |
| 797 | |
| 798 | |
| 799 | $fonts = $zbs->get_fonts(); |
| 800 | if ( ! $fonts->extract_and_install_default_fonts() ) { |
| 801 | return false; |
| 802 | } |
| 803 | } |
| 804 | |
| 805 | |
| 806 | zeroBSCRM_migrations_markComplete( '544', array( 'updated' => 1 ) ); |
| 807 | } |
| 808 | |
| 809 | |
| 810 | |
| 811 | |
| 812 | |
| 813 | function zeroBSCRM_migration_55() { |
| 814 | |
| 815 | global $zbs, $wpdb, $ZBSCRM_t; |
| 816 | |
| 817 | |
| 818 | $wpdb->query( |
| 819 | ' DELETE FROM ' . $ZBSCRM_t['objlinks'] |
| 820 | . ' WHERE ' |
| 821 | . ' zbsol_objtype_to = ' . ZBS_TYPE_INVOICE |
| 822 | . ' AND zbsol_objid_to NOT IN ( SELECT ID from ' . $ZBSCRM_t['invoices'] . ' ) ' |
| 823 | ); |
| 824 | |
| 825 | |
| 826 | $wpdb->query( |
| 827 | ' DELETE FROM ' . $ZBSCRM_t['objlinks'] |
| 828 | . ' WHERE ' |
| 829 | . ' zbsol_objtype_from = ' . ZBS_TYPE_INVOICE |
| 830 | . ' AND zbsol_objid_from NOT IN ( SELECT ID from ' . $ZBSCRM_t['invoices'] . ' ) ' |
| 831 | ); |
| 832 | |
| 833 | |
| 834 | $wpdb->query( |
| 835 | ' DELETE FROM ' . $ZBSCRM_t['lineitems'] . ' WHERE ID NOT IN' |
| 836 | . ' (' |
| 837 | . ' SELECT zbsol_objid_from FROM ' . $ZBSCRM_t['objlinks'] |
| 838 | . ' WHERE ' |
| 839 | . ' zbsol_objtype_from = ' . ZBS_TYPE_LINEITEM |
| 840 | . ' )' |
| 841 | ); |
| 842 | |
| 843 | |
| 844 | zeroBSCRM_migrations_markComplete( '55', array( 'updated' => 1 ) ); |
| 845 | } |
| 846 | |
| 847 | |
| 848 | |
| 849 | |
| 850 | |
| 851 | function zeroBSCRM_migration_55a() { |
| 852 | |
| 853 | global $zbs; |
| 854 | |
| 855 | |
| 856 | $zbs->DAL->segments->compile_all_segments(); |
| 857 | |
| 858 | |
| 859 | zeroBSCRM_migrations_markComplete( '55a', array( 'updated' => 1 ) ); |
| 860 | } |
| 861 | |
| 862 | |
| 863 | |
| 864 | |
| 865 | |
| 866 | function zeroBSCRM_migration_551() { |
| 867 | |
| 868 | global $zbs, $wpdb, $ZBSCRM_t; |
| 869 | |
| 870 | |
| 871 | $wpdb->query( |
| 872 | 'DELETE FROM ' . $ZBSCRM_t['aka'] |
| 873 | . ' WHERE aka_type = ' . ZBS_TYPE_CONTACT . ' AND aka_id NOT IN' |
| 874 | . ' (SELECT id FROM ' . $ZBSCRM_t['contacts'] . ')' |
| 875 | ); |
| 876 | |
| 877 | |
| 878 | zeroBSCRM_migrations_markComplete( '551', array( 'updated' => 1 ) ); |
| 879 | } |
| 880 | |
| 881 | |
| 882 | |
| 883 | |
| 884 | |
| 885 | |
| 886 | |
| 887 | |
| 888 | |
| 889 | |
| 890 | |
| 891 | function zeroBSCRM_migration_560_move_custom_file_upload_box( $meta_row ) { |
| 892 | global $wpdb, $ZBSCRM_t; |
| 893 | jpcrm_migration_load_wp_filesystem_direct(); |
| 894 | |
| 895 | |
| 896 | |
| 897 | if ( $meta_row->zbsm_objtype !== ZBS_TYPE_CONTACT ) { |
| 898 | return; |
| 899 | } |
| 900 | $file_path = $meta_row->zbsm_val; |
| 901 | |
| 902 | if ( ! file_exists( $file_path ) ) { |
| 903 | error_log( sprintf( 'JPCRM migration error while searching for upload box file %s', $file_path ) ); |
| 904 | return; |
| 905 | } |
| 906 | $new_dir = jpcrm_storage_dir_info_for_contact( $meta_row->zbsm_objid ); |
| 907 | |
| 908 | if ( $new_dir === false || ! isset( $new_dir['files'] ) ) { |
| 909 | error_log( sprintf( 'JPCRM migration error missing subfolder files for contact ID %s', $meta_row->zbsm_objid ) ); |
| 910 | return; |
| 911 | } |
| 912 | $new_dir_info = $new_dir['files']; |
| 913 | $upload_folder_exists = jpcrm_create_and_secure_dir_from_external_access( $new_dir_info['path'], false ); |
| 914 | if ( $upload_folder_exists === false ) { |
| 915 | |
| 916 | error_log( sprintf( 'JPCRM migration error while creating upload box folder %s ', $new_dir_info['path'] ) ); |
| 917 | return; |
| 918 | } |
| 919 | $file_name = basename( $file_path ); |
| 920 | $new_file_path = $new_dir_info['path'] . '/' . $file_name; |
| 921 | $wp_filesystem_direct = new WP_Filesystem_Direct( false ); |
| 922 | |
| 923 | if ( ! $wp_filesystem_direct->move( $file_path, $new_file_path, true ) ) { |
| 924 | |
| 925 | error_log( sprintf( 'JPCRM migration error while moving upload box %s to %s', $file_path, $new_file_path ) ); |
| 926 | return; |
| 927 | } |
| 928 | |
| 929 | |
| 930 | $update_result = $wpdb->update( |
| 931 | $ZBSCRM_t['meta'], |
| 932 | array( |
| 933 | 'zbsm_val' => $new_file_path, |
| 934 | 'zbsm_lastupdated' => time(), |
| 935 | ), |
| 936 | array( 'ID' => $meta_row->ID ), |
| 937 | array( |
| 938 | '%s', |
| 939 | '%d', |
| 940 | ), |
| 941 | array( |
| 942 | '%d', |
| 943 | ) |
| 944 | ); |
| 945 | |
| 946 | if ( $update_result === false ) { |
| 947 | error_log( sprintf( 'JPCRM migration error while updating upload box meta %s to %s', $meta_row->ID ) ); |
| 948 | } |
| 949 | } |
| 950 | |
| 951 | |
| 952 | |
| 953 | |
| 954 | |
| 955 | |
| 956 | |
| 957 | |
| 958 | |
| 959 | |
| 960 | function zeroBSCRM_migration_560_move_file_array( $meta_row ) { |
| 961 | global $wpdb, $ZBSCRM_t; |
| 962 | jpcrm_migration_load_wp_filesystem_direct(); |
| 963 | |
| 964 | |
| 965 | |
| 966 | |
| 967 | $new_dir = false; |
| 968 | switch ( $meta_row->zbsm_objtype ) { |
| 969 | case ZBS_TYPE_CONTACT: |
| 970 | $new_dir = jpcrm_storage_dir_info_for_contact( $meta_row->zbsm_objid ); |
| 971 | break; |
| 972 | |
| 973 | case ZBS_TYPE_COMPANY: |
| 974 | $new_dir = jpcrm_storage_dir_info_for_company( $meta_row->zbsm_objid ); |
| 975 | break; |
| 976 | |
| 977 | case ZBS_TYPE_QUOTE: |
| 978 | $new_dir = jpcrm_storage_dir_info_for_quotes( $meta_row->zbsm_objid ); |
| 979 | break; |
| 980 | |
| 981 | case ZBS_TYPE_INVOICE: |
| 982 | $new_dir = jpcrm_storage_dir_info_for_invoices( $meta_row->zbsm_objid ); |
| 983 | break; |
| 984 | } |
| 985 | |
| 986 | |
| 987 | if ( $new_dir === false || ! isset( $new_dir['files'] ) ) { |
| 988 | return; |
| 989 | } |
| 990 | |
| 991 | $new_dir_info = $new_dir['files']; |
| 992 | $outdated_file_array = json_decode( $meta_row->zbsm_val, true ); |
| 993 | |
| 994 | |
| 995 | if ( $outdated_file_array === null ) { |
| 996 | return; |
| 997 | } |
| 998 | |
| 999 | |
| 1000 | $previous_folder = 'zbscrm-store'; |
| 1001 | $new_file_array = array(); |
| 1002 | foreach ( $outdated_file_array as $outdated_file_meta ) { |
| 1003 | |
| 1004 | |
| 1005 | |
| 1006 | if ( |
| 1007 | ! isset( $outdated_file_meta['file'] ) |
| 1008 | || ! str_contains( $outdated_file_meta['file'], "/$previous_folder/" ) |
| 1009 | || ! file_exists( $outdated_file_meta['file'] ) |
| 1010 | ) { |
| 1011 | $new_file_array[] = $outdated_file_meta; |
| 1012 | continue; |
| 1013 | } |
| 1014 | |
| 1015 | $upload_folder_exists = jpcrm_create_and_secure_dir_from_external_access( $new_dir_info['path'], false ); |
| 1016 | if ( $upload_folder_exists === false ) { |
| 1017 | |
| 1018 | error_log( sprintf( 'JPCRM migration error while creating folder %s ', $new_dir_info['path'] ) ); |
| 1019 | $new_file_array[] = $outdated_file_meta; |
| 1020 | return; |
| 1021 | } |
| 1022 | |
| 1023 | $file_name = basename( $outdated_file_meta['file'] ); |
| 1024 | $new_file_path = $new_dir_info['path'] . '/' . $file_name; |
| 1025 | $wp_filesystem_direct = new WP_Filesystem_Direct( false ); |
| 1026 | |
| 1027 | if ( ! $wp_filesystem_direct->move( $outdated_file_meta['file'], $new_file_path, true ) ) { |
| 1028 | |
| 1029 | error_log( sprintf( 'JPCRM migration error while moving %s to %s', $outdated_file_meta['file'], $new_file_path ) ); |
| 1030 | $new_file_array[] = $outdated_file_meta; |
| 1031 | continue; |
| 1032 | } |
| 1033 | |
| 1034 | |
| 1035 | $outdated_file_meta['file'] = $new_file_path; |
| 1036 | $outdated_file_meta['url'] = $new_dir_info['url'] . '/' . $file_name; |
| 1037 | $new_file_array[] = $outdated_file_meta; |
| 1038 | } |
| 1039 | |
| 1040 | $update_result = $wpdb->update( |
| 1041 | $ZBSCRM_t['meta'], |
| 1042 | array( |
| 1043 | 'zbsm_val' => wp_json_encode( $new_file_array, JSON_UNESCAPED_SLASHES ), |
| 1044 | 'zbsm_lastupdated' => time(), |
| 1045 | ), |
| 1046 | array( 'ID' => $meta_row->ID ), |
| 1047 | array( |
| 1048 | '%s', |
| 1049 | '%d', |
| 1050 | ), |
| 1051 | array( |
| 1052 | '%d', |
| 1053 | ) |
| 1054 | ); |
| 1055 | |
| 1056 | if ( $update_result === false ) { |
| 1057 | error_log( sprintf( 'JPCRM migration error while updating file array meta %s to %s', $meta_row->ID ) ); |
| 1058 | } |
| 1059 | } |
| 1060 | |
| 1061 | |
| 1062 | |
| 1063 | |
| 1064 | |
| 1065 | |
| 1066 | |
| 1067 | function zeroBSCRM_migration_560() { |
| 1068 | global $wpdb, $ZBSCRM_t; |
| 1069 | |
| 1070 | |
| 1071 | $previous_folder = 'zbscrm-store'; |
| 1072 | |
| 1073 | $query = sprintf( "SELECT * FROM `%s` WHERE `zbsm_val` LIKE '%s'", $ZBSCRM_t['meta'], '%%%s%%' ); |
| 1074 | $outdated_rows = $wpdb->get_results( $wpdb->prepare( $query, $previous_folder ) ); |
| 1075 | |
| 1076 | if ( is_array( $outdated_rows ) ) { |
| 1077 | foreach ( $outdated_rows as $outdated_row ) { |
| 1078 | |
| 1079 | |
| 1080 | if ( $outdated_row->zbsm_key === 'files' ) { |
| 1081 | zeroBSCRM_migration_560_move_file_array( $outdated_row ); |
| 1082 | } else { |
| 1083 | zeroBSCRM_migration_560_move_custom_file_upload_box( $outdated_row ); |
| 1084 | } |
| 1085 | } |
| 1086 | } |
| 1087 | |
| 1088 | |
| 1089 | zeroBSCRM_migrations_markComplete( '560', array( 'updated' => 1 ) ); |
| 1090 | } |
| 1091 | |
| 1092 | |
| 1093 | |
| 1094 | |
| 1095 | |
| 1096 | |
| 1097 | |
| 1098 | |
| 1099 | |
| 1100 | function zeroBSCRM_migration_create_workflows_table() { |
| 1101 | |
| 1102 | zeroBSCRM_checkTablesExist(); |
| 1103 | |
| 1104 | |
| 1105 | zeroBSCRM_migrations_markComplete( 'create_workflows_table', array( 'updated' => 1 ) ); |
| 1106 | } |
| 1107 | |
| 1108 | |
| 1109 | |
| 1110 | |
| 1111 | function zeroBSCRM_migration_task_offset_fix() { |
| 1112 | global $wpdb, $ZBSCRM_t; |
| 1113 | |
| 1114 | $timezone_offset_in_secs = jpcrm_get_wp_timezone_offset_in_seconds(); |
| 1115 | |
| 1116 | if ( ! empty( $timezone_offset_in_secs ) ) { |
| 1117 | |
| 1118 | $sql = sprintf( 'UPDATE %s SET zbse_start = zbse_start - %d;', $ZBSCRM_t['events'], $timezone_offset_in_secs ); |
| 1119 | $wpdb->query( $sql ); |
| 1120 | $sql = sprintf( 'UPDATE %s SET zbse_end = zbse_end - %d;', $ZBSCRM_t['events'], $timezone_offset_in_secs ); |
| 1121 | $wpdb->query( $sql ); |
| 1122 | } |
| 1123 | |
| 1124 | zeroBSCRM_migrations_markComplete( 'task_offset_fix', array( 'updated' => 1 ) ); |
| 1125 | } |
| 1126 | |
| 1127 | |
| 1128 | |
| 1129 | |
| 1130 | function zeroBSCRM_migration_refresh_user_roles() { |
| 1131 | |
| 1132 | zeroBSCRM_clearUserRoles(); |
| 1133 | |
| 1134 | |
| 1135 | zeroBSCRM_addUserRoles(); |
| 1136 | |
| 1137 | zeroBSCRM_migrations_markComplete( 'refresh_user_roles', array( 'updated' => 1 ) ); |
| 1138 | } |
| 1139 | |
| 1140 | |
| 1141 | |
| 1142 | |
| 1143 | function zeroBSCRM_migration_regenerate_tag_slugs() { |
| 1144 | $obj_ids = array( ZBS_TYPE_CONTACT, ZBS_TYPE_COMPANY, ZBS_TYPE_QUOTE, ZBS_TYPE_INVOICE, ZBS_TYPE_TRANSACTION, ZBS_TYPE_TASK, ZBS_TYPE_FORM ); |
| 1145 | foreach ( $obj_ids as $obj_id ) { |
| 1146 | jpcrm_migration_regenerate_tag_slugs_for_obj_type( $obj_id ); |
| 1147 | } |
| 1148 | zeroBSCRM_migrations_markComplete( 'regenerate_tag_slugs', array( 'updated' => 1 ) ); |
| 1149 | } |
| 1150 | |
| 1151 | |
| 1152 | |
| 1153 | |
| 1154 | function zeroBSCRM_migration_invoice_language_fixes() { |
| 1155 | |
| 1156 | |
| 1157 | $cur_locale = get_locale(); |
| 1158 | if ( $cur_locale === 'en_US' ) { |
| 1159 | zeroBSCRM_migrations_markComplete( 'invoice_language_fixes', array( 'updated' => 1 ) ); |
| 1160 | return; |
| 1161 | } |
| 1162 | |
| 1163 | global $zbs, $wpdb, $ZBSCRM_t; |
| 1164 | |
| 1165 | $invoice_statuses = array( |
| 1166 | 'Draft' => __( 'Draft', 'zero-bs-crm' ), |
| 1167 | 'Unpaid' => __( 'Unpaid', 'zero-bs-crm' ), |
| 1168 | 'Paid' => __( 'Paid', 'zero-bs-crm' ), |
| 1169 | 'Overdue' => __( 'Overdue', 'zero-bs-crm' ), |
| 1170 | 'Deleted' => __( 'Deleted', 'zero-bs-crm' ), |
| 1171 | ); |
| 1172 | |
| 1173 | |
| 1174 | $woosync_settings = $zbs->settings->get( 'zbscrm_dmz_ext_woosync' ); |
| 1175 | |
| 1176 | $settings_to_update = array(); |
| 1177 | |
| 1178 | foreach ( $invoice_statuses as $invoice_status => $translated_status ) { |
| 1179 | |
| 1180 | |
| 1181 | if ( $translated_status === $invoice_status ) { |
| 1182 | continue; |
| 1183 | } |
| 1184 | |
| 1185 | |
| 1186 | if ( $woosync_settings ) { |
| 1187 | |
| 1188 | foreach ( $woosync_settings as $setting => $value ) { |
| 1189 | |
| 1190 | if ( ! str_starts_with( $setting, 'order_invoice_map_' ) ) { |
| 1191 | continue; |
| 1192 | } |
| 1193 | |
| 1194 | |
| 1195 | if ( $value !== $translated_status ) { |
| 1196 | continue; |
| 1197 | } |
| 1198 | |
| 1199 | |
| 1200 | $settings_to_update[ $setting ] = $invoice_status; |
| 1201 | } |
| 1202 | } |
| 1203 | |
| 1204 | |
| 1205 | $query = $wpdb->prepare( 'SELECT COUNT(ID) FROM ' . $ZBSCRM_t['invoices'] . ' WHERE zbsi_status=%s', $translated_status ); |
| 1206 | $count = $wpdb->get_var( $query ); |
| 1207 | |
| 1208 | |
| 1209 | if ( $count === 0 ) { |
| 1210 | continue; |
| 1211 | } |
| 1212 | |
| 1213 | |
| 1214 | $query = $wpdb->prepare( 'UPDATE ' . $ZBSCRM_t['invoices'] . ' SET zbsi_status=%s WHERE zbsi_status=%s', $invoice_status, $translated_status ); |
| 1215 | $wpdb->query( $query ); |
| 1216 | } |
| 1217 | |
| 1218 | |
| 1219 | if ( $woosync_settings && ! empty( $settings_to_update ) ) { |
| 1220 | |
| 1221 | |
| 1222 | $zbs->settings->update( 'zbscrm_dmz_ext_woosync.bak', $woosync_settings ); |
| 1223 | |
| 1224 | |
| 1225 | $updated_woosync_settings = array_merge( $woosync_settings, $settings_to_update ); |
| 1226 | $zbs->settings->update( 'zbscrm_dmz_ext_woosync', $updated_woosync_settings ); |
| 1227 | } |
| 1228 | |
| 1229 | zeroBSCRM_migrations_markComplete( 'invoice_language_fixes', array( 'updated' => 1 ) ); |
| 1230 | } |
| 1231 | |
| 1232 | |
| 1233 | |
| 1234 | |
| 1235 | function zeroBSCRM_migration_gh3465_increase_city_field_size() { |
| 1236 | |
| 1237 | |
| 1238 | $db_engine = jpcrm_database_engine(); |
| 1239 | if ( $db_engine === 'sqlite' ) { |
| 1240 | return; |
| 1241 | } |
| 1242 | |
| 1243 | global $wpdb, $ZBSCRM_t; |
| 1244 | |
| 1245 | $sql = 'ALTER TABLE ' . $ZBSCRM_t['contacts'] . ' MODIFY COLUMN `zbsc_city` VARCHAR(200);'; |
| 1246 | $wpdb->query( $sql ); |
| 1247 | |
| 1248 | $sql = 'ALTER TABLE ' . $ZBSCRM_t['contacts'] . ' MODIFY COLUMN `zbsc_seccity` VARCHAR(200);'; |
| 1249 | $wpdb->query( $sql ); |
| 1250 | |
| 1251 | $sql = 'ALTER TABLE ' . $ZBSCRM_t['companies'] . ' MODIFY COLUMN `zbsco_city` VARCHAR(200);'; |
| 1252 | $wpdb->query( $sql ); |
| 1253 | |
| 1254 | $sql = 'ALTER TABLE ' . $ZBSCRM_t['companies'] . ' MODIFY COLUMN `zbsco_seccity` VARCHAR(200);'; |
| 1255 | $wpdb->query( $sql ); |
| 1256 | |
| 1257 | zeroBSCRM_migrations_markComplete( 'gh3465_increase_city_field_size', array( 'updated' => 1 ) ); |
| 1258 | } |
| 1259 | |
| 1260 | |
| 1261 | |
| 1262 | |
| 1263 | function zeroBSCRM_migration_tax_rate_precision_fix() { |
| 1264 | |
| 1265 | global $wpdb, $ZBSCRM_t; |
| 1266 | |
| 1267 | $db_engine = jpcrm_database_engine(); |
| 1268 | |
| 1269 | if ( $db_engine === 'sqlite' ) { |
| 1270 | |
| 1271 | |
| 1272 | zeroBSCRM_migrations_markComplete( |
| 1273 | 'tax_rate_precision_fix', |
| 1274 | array( |
| 1275 | 'updated' => 0, |
| 1276 | 'note' => 'SQLite dynamic typing - no schema change needed', |
| 1277 | ) |
| 1278 | ); |
| 1279 | return; |
| 1280 | } |
| 1281 | |
| 1282 | |
| 1283 | $column_type = zeraBSCRM_migration_get_column_data_type( $ZBSCRM_t['tax'], 'zbsc_rate' ); |
| 1284 | if ( $column_type && strpos( $column_type, 'decimal(20,10)' ) === false ) { |
| 1285 | |
| 1286 | $wpdb->query( 'ALTER TABLE `' . esc_sql( $ZBSCRM_t['tax'] ) . '` MODIFY zbsc_rate DECIMAL(20,10) NOT NULL DEFAULT 0.0000000000' ); |
| 1287 | } |
| 1288 | |
| 1289 | zeroBSCRM_migrations_markComplete( 'tax_rate_precision_fix', array( 'updated' => 1 ) ); |
| 1290 | } |
| 1291 | |
| 1292 | |
| 1293 | |
| 1294 | |
| 1295 | |
| 1296 | |
| 1297 | |
| 1298 | function zeroBSCRM_migration_ensure_notifications_table() { |
| 1299 | global $wpdb; |
| 1300 | |
| 1301 | |
| 1302 | if ( ! function_exists( 'jpcrm_create_notifications_table' ) ) { |
| 1303 | require_once ZEROBSCRM_INCLUDE_PATH . 'ZeroBSCRM.Database.php'; |
| 1304 | } |
| 1305 | |
| 1306 | |
| 1307 | $table_name = $wpdb->prefix . 'zbs_notifications'; |
| 1308 | |
| 1309 | |
| 1310 | $table_exists = $wpdb->get_var( "SHOW TABLES LIKE '$table_name'" ) === $table_name; |
| 1311 | |
| 1312 | |
| 1313 | if ( ! $table_exists ) { |
| 1314 | |
| 1315 | $result = jpcrm_create_notifications_table(); |
| 1316 | |
| 1317 | if ( $result ) { |
| 1318 | zeroBSCRM_migrations_markComplete( |
| 1319 | 'ensure_notifications_table', |
| 1320 | array( |
| 1321 | 'updated' => 1, |
| 1322 | 'note' => 'Successfully created notifications table', |
| 1323 | ) |
| 1324 | ); |
| 1325 | return true; |
| 1326 | } else { |
| 1327 | |
| 1328 | zeroBSCRM_migrations_markComplete( |
| 1329 | 'ensure_notifications_table', |
| 1330 | array( |
| 1331 | 'updated' => 0, |
| 1332 | 'note' => 'Failed to create notifications table', |
| 1333 | 'error' => true, |
| 1334 | ) |
| 1335 | ); |
| 1336 | return false; |
| 1337 | } |
| 1338 | } |
| 1339 | |
| 1340 | |
| 1341 | zeroBSCRM_migrations_markComplete( |
| 1342 | 'ensure_notifications_table', |
| 1343 | array( |
| 1344 | 'updated' => 0, |
| 1345 | 'note' => 'Notifications table already exists', |
| 1346 | ) |
| 1347 | ); |
| 1348 | return true; |
| 1349 | } |
| 1350 | |
| 1351 | |
| 1352 | |
| 1353 | |
| 1354 | |
| 1355 | |
| 1356 | |
| 1357 | |
| 1358 | |
| 1359 | |
| 1360 | |
| 1361 | |
| 1362 | function zeroBSCRM_migration_addErrToStack( $err = array(), $errKey = '' ) { |
| 1363 | |
| 1364 | if ( $errKey !== '' ) { |
| 1365 | |
| 1366 | $existing = get_option( $errKey, array() ); |
| 1367 | |
| 1368 | |
| 1369 | if ( ! is_array( $existing ) ) { |
| 1370 | $existing = array(); |
| 1371 | } |
| 1372 | |
| 1373 | |
| 1374 | $existing[] = $err; |
| 1375 | update_option( $errKey, $existing, false ); |
| 1376 | |
| 1377 | return true; |
| 1378 | |
| 1379 | } |
| 1380 | |
| 1381 | return false; |
| 1382 | } |
| 1383 | |
| 1384 | |
| 1385 | |
| 1386 | function zeroBSCRM_migration_tableHasColumn( $table_name, $column_name ) { |
| 1387 | |
| 1388 | global $wpdb; |
| 1389 | |
| 1390 | if ( ! empty( $table_name ) && ! empty( $column_name ) ) { |
| 1391 | |
| 1392 | $query = $wpdb->prepare( 'SHOW COLUMNS FROM ' . $table_name . ' LIKE %s', $column_name ); |
| 1393 | |
| 1394 | $row = $wpdb->get_results( $query ); |
| 1395 | |
| 1396 | if ( is_array( $row ) && count( $row ) > 0 ) { |
| 1397 | |
| 1398 | return true; |
| 1399 | |
| 1400 | } |
| 1401 | } |
| 1402 | |
| 1403 | return false; |
| 1404 | } |
| 1405 | |
| 1406 | |
| 1407 | |
| 1408 | |
| 1409 | function jpcrm_migration_table_has_index( $table_name, $index_name ) { |
| 1410 | |
| 1411 | global $wpdb; |
| 1412 | |
| 1413 | $query = $wpdb->prepare( 'SHOW INDEX FROM ' . $table_name . ' WHERE Key_name = %s', $index_name ); |
| 1414 | $row = $wpdb->get_results( $query ); |
| 1415 | |
| 1416 | if ( is_array( $row ) && count( $row ) > 0 ) { |
| 1417 | |
| 1418 | return true; |
| 1419 | |
| 1420 | } |
| 1421 | |
| 1422 | return false; |
| 1423 | } |
| 1424 | |
| 1425 | |
| 1426 | |
| 1427 | |
| 1428 | |
| 1429 | |
| 1430 | |
| 1431 | |
| 1432 | |
| 1433 | |
| 1434 | |
| 1435 | function zeraBSCRM_migration_get_column_data_type( $table_name, $column_name ) { |
| 1436 | global $wpdb; |
| 1437 | |
| 1438 | $column = $wpdb->get_row( |
| 1439 | $wpdb->prepare( |
| 1440 | "SHOW COLUMNS FROM $table_name LIKE %s", |
| 1441 | $column_name |
| 1442 | ) |
| 1443 | ); |
| 1444 | return empty( $column ) ? false : $column->Type; |
| 1445 | } |
| 1446 | |
| 1447 | |
| 1448 | |
| 1449 | |
| 1450 | |
| 1451 | |
| 1452 | function jpcrm_migration_load_wp_filesystem_direct() { |
| 1453 | require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php'; |
| 1454 | require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php'; |
| 1455 | } |
| 1456 | |
| 1457 | |
| 1458 | |
| 1459 | |
| 1460 | |
| 1461 | |
| 1462 | function jpcrm_migration_regenerate_tag_slugs_for_obj_type( int $obj_type_id ) { |
| 1463 | global $zbs; |
| 1464 | |
| 1465 | |
| 1466 | $tags = $zbs->DAL->getTagsForObjType( |
| 1467 | array( |
| 1468 | 'objtypeid' => $obj_type_id, |
| 1469 | 'excludeEmpty' => false, |
| 1470 | ) |
| 1471 | ); |
| 1472 | |
| 1473 | |
| 1474 | $used_slugs = array(); |
| 1475 | |
| 1476 | foreach ( $tags as $tag ) { |
| 1477 | |
| 1478 | |
| 1479 | $potential_slug = sanitize_key( $tag['name'] ); |
| 1480 | |
| 1481 | |
| 1482 | if ( empty( $potential_slug ) ) { |
| 1483 | if ( preg_match( '/^tag-\d+$/', $tag['slug'] ) && ! in_array( $tag['slug'], $used_slugs, true ) ) { |
| 1484 | |
| 1485 | $potential_slug = $tag['slug']; |
| 1486 | } else { |
| 1487 | |
| 1488 | $potential_slug = $zbs->DAL->get_new_tag_slug( $obj_type_id, 'tag', true ); |
| 1489 | } |
| 1490 | } elseif ( in_array( $potential_slug, $used_slugs, true ) ) { |
| 1491 | |
| 1492 | if ( preg_match( '/^' . $potential_slug . '-\d+$/', $tag['slug'] ) && ! in_array( $tag['slug'], $used_slugs, true ) ) { |
| 1493 | |
| 1494 | $potential_slug = $tag['slug']; |
| 1495 | } else { |
| 1496 | |
| 1497 | $potential_slug = $zbs->DAL->get_new_tag_slug( $obj_type_id, $potential_slug ); |
| 1498 | } |
| 1499 | } |
| 1500 | |
| 1501 | |
| 1502 | if ( $potential_slug !== $tag['slug'] ) { |
| 1503 | $zbs->DAL->addUpdateTag( |
| 1504 | array( |
| 1505 | 'id' => $tag['id'], |
| 1506 | 'data' => array( |
| 1507 | 'objtype' => $obj_type_id, |
| 1508 | 'name' => $tag['name'], |
| 1509 | 'slug' => $potential_slug, |
| 1510 | ), |
| 1511 | ) |
| 1512 | ); |
| 1513 | } |
| 1514 | |
| 1515 | |
| 1516 | $used_slugs[] = $potential_slug; |
| 1517 | } |
| 1518 | } |
| 1519 | |
| 1520 | |
| 1521 | |
| 1522 | |
| 1523 | |