← All changes
|
modules/api-backups/classes/class-api-backups-utility.php
+53
-6
5.2.1
→
6.2.1
View file →
| @@ -7,12 +7,19 @@ | ||
| 7 | 7 | */ |
| 8 | 8 | |
| 9 | 9 | namespace MainWP\Dashboard\Module\ApiBackups; |
| 10 | 10 | |
| 11 | +use MainWP\Dashboard\MainWP_Credential_Render; | |
| 11 | 12 | use MainWP\Dashboard\MainWP_DB; |
| 12 | 13 | use MainWP\Dashboard\MainWP_Logger; |
| 13 | 14 | use MainWP\Dashboard\MainWP_System_Utility; |
| 14 | 15 | |
| 16 | +// Exit if accessed directly. | |
| 17 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 18 | + exit; | |
| 19 | +} | |
| 20 | + | |
| 21 | + | |
| 15 | 22 | /** |
| 16 | 23 | * Class Api_Backups_Utility |
| 17 | 24 | */ |
| 18 | 25 | class Api_Backups_Utility { //phpcs:ignore -- NOSONAR - multi methods. |
| @@ -214,9 +221,20 @@ | ||
| 214 | 221 | } |
| 215 | 222 | |
| 216 | 223 | $primaryBackup = MainWP_System_Utility::get_primary_backup(); |
| 217 | 224 | |
| 218 | - if ( 'module-api-backups' !== $primaryBackup ) { | |
| 225 | + $website = Api_Backups_Helper::get_website_by_id( $site_id ); | |
| 226 | + | |
| 227 | + $backup_method = ''; | |
| 228 | + if ( is_array( $website ) && isset( $website['primary_backup_method'] ) ) { | |
| 229 | + if ( '' === $website['primary_backup_method'] || 'global' === $website['primary_backup_method'] ) { | |
| 230 | + $backup_method = $primaryBackup; | |
| 231 | + } else { | |
| 232 | + $backup_method = $website['primary_backup_method']; | |
| 233 | + } | |
| 234 | + } | |
| 235 | + | |
| 236 | + if ( 'module-api-backups' !== $backup_method ) { | |
| 219 | 237 | return; |
| 220 | 238 | } |
| 221 | 239 | |
| 222 | 240 | $lasttime_backup = 0; |
| @@ -564,10 +582,11 @@ | ||
| 564 | 582 | * Encrypt data. |
| 565 | 583 | * |
| 566 | 584 | * @param string $name name data. |
| 567 | 585 | * @param string $def_val default data. |
| 586 | + * @param string $opt_name Option name. | |
| 568 | 587 | */ |
| 569 | - public function get_api_key( $name, $def_val = '' ) { | |
| 588 | + public function get_api_key( $name, $def_val = '', $opt_name = '' ) { | |
| 570 | 589 | |
| 571 | 590 | $names = array( |
| 572 | 591 | 'vultr', |
| 573 | 592 | 'gridpane', |
| @@ -582,9 +601,14 @@ | ||
| 582 | 601 | if ( ! in_array( $name, $names ) ) { |
| 583 | 602 | return $def_val; |
| 584 | 603 | } |
| 585 | 604 | |
| 586 | - $encrypted = get_option( 'mainwp_api_backups_' . $name . '_api_key' ); | |
| 605 | + if ( ! empty( $opt_name ) ) { | |
| 606 | + $encrypted = get_option( 'mainwp_api_backups_' . $name . '_' . (string) $opt_name ); | |
| 607 | + } else { | |
| 608 | + $encrypted = get_option( 'mainwp_api_backups_' . $name . '_api_key' ); | |
| 609 | + } | |
| 610 | + | |
| 587 | 611 | if ( is_array( $encrypted ) && ! empty( $encrypted['file_key'] ) ) { |
| 588 | 612 | $key = $this->decrypt_api_keys( $encrypted ); |
| 589 | 613 | if ( ! empty( $key ) && is_string( $key ) ) { |
| 590 | 614 | return $key; |
| @@ -599,10 +623,11 @@ | ||
| 599 | 623 | * Encrypt data. |
| 600 | 624 | * |
| 601 | 625 | * @param string $name name data. |
| 602 | 626 | * @param string $value value data. |
| 627 | + * @param string $opt_name Option name. | |
| 603 | 628 | */ |
| 604 | - public function update_api_key( $name, $value ) { | |
| 629 | + public function update_api_key( $name, $value, $opt_name = '' ) { | |
| 605 | 630 | |
| 606 | 631 | $names = array( |
| 607 | 632 | 'vultr', |
| 608 | 633 | 'gridpane', |
| @@ -617,9 +642,22 @@ | ||
| 617 | 642 | if ( ! in_array( $name, $names ) ) { |
| 618 | 643 | return false; |
| 619 | 644 | } |
| 620 | 645 | |
| 621 | - $opt_name = 'mainwp_api_backups_' . $name . '_api_key'; | |
| 646 | + // MWP-1543: settings forms render the sentinel ('••••••••') in the | |
| 647 | + // password input when a value is already stored. A submission that | |
| 648 | + // returns the sentinel unchanged means "leave existing value alone"; | |
| 649 | + // short-circuit before delete_option / encrypt so no overwrite happens. | |
| 650 | + if ( MainWP_Credential_Render::is_sentinel( $value ) ) { | |
| 651 | + return true; | |
| 652 | + } | |
| 653 | + | |
| 654 | + if ( ! empty( $opt_name ) ) { | |
| 655 | + $opt_name = 'mainwp_api_backups_' . $name . '_' . (string) $opt_name; | |
| 656 | + } else { | |
| 657 | + $opt_name = 'mainwp_api_backups_' . $name . '_api_key'; | |
| 658 | + } | |
| 659 | + | |
| 622 | 660 | $current = get_option( $opt_name ); |
| 623 | 661 | $key_file = ''; |
| 624 | 662 | |
| 625 | 663 | if ( is_array( $current ) && ! empty( $current['file_key'] ) ) { |
| @@ -711,8 +749,16 @@ | ||
| 711 | 749 | if ( ! in_array( $name, $names ) ) { |
| 712 | 750 | return false; |
| 713 | 751 | } |
| 714 | 752 | |
| 753 | + // MWP-1543: same sentinel short-circuit as update_api_key(); the | |
| 754 | + // per-site Edit Site form renders the sentinel placeholder when a | |
| 755 | + // value already exists, and an unchanged submission must not | |
| 756 | + // overwrite the encrypted option with the sentinel string. | |
| 757 | + if ( MainWP_Credential_Render::is_sentinel( $value ) ) { | |
| 758 | + return true; | |
| 759 | + } | |
| 760 | + | |
| 715 | 761 | $opt_name = 'mainwp_api_backups_' . $name . '_api_key'; |
| 716 | 762 | $opt_encoded = Api_Backups_Helper::get_website_options( $website_id, array( $opt_name ) ); |
| 717 | 763 | if ( array_key_exists( $opt_name, $opt_encoded ) ) { |
| 718 | 764 | $current = json_decode( $opt_encoded[ $opt_name ], true ); |
| @@ -725,9 +771,10 @@ | ||
| 725 | 771 | } |
| 726 | 772 | |
| 727 | 773 | // If post $value is empty, delete option. |
| 728 | 774 | if ( empty( $value ) ) { |
| 729 | - delete_option( $opt_name ); | |
| 775 | + // Delete the site serviceapi key. | |
| 776 | + Api_Backups_Helper::update_website_option( $website_id, $opt_name, '' ); | |
| 730 | 777 | if ( ! empty( $key_file ) ) { |
| 731 | 778 | do_action( 'mainwp_delete_key_file', $key_file ); |
| 732 | 779 | } |
| 733 | 780 | return true; |