SyncUI.php
123 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Automattic\WooCommerce\Internal\ProductDownloads\ApprovedDirectories\Admin; |
| 4 | |
| 5 | use Automattic\WooCommerce\Internal\ProductDownloads\ApprovedDirectories\Register; |
| 6 | use Automattic\WooCommerce\Internal\ProductDownloads\ApprovedDirectories\Synchronize; |
| 7 | use Automattic\WooCommerce\Internal\Utilities\Users; |
| 8 | |
| 9 | /** |
| 10 | * Adds tools to the Status > Tools page that can be used to (re-)initiate or stop a synchronization process |
| 11 | * for Approved Download Directories. |
| 12 | */ |
| 13 | class SyncUI { |
| 14 | /** |
| 15 | * The active register of approved directories. |
| 16 | * |
| 17 | * @var Register |
| 18 | */ |
| 19 | private $register; |
| 20 | |
| 21 | /** |
| 22 | * Sets up UI controls for product download URLs. |
| 23 | * |
| 24 | * @internal |
| 25 | * |
| 26 | * @param Register $register Register of approved directories. |
| 27 | */ |
| 28 | final public function init( Register $register ) { |
| 29 | $this->register = $register; |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Performs any work needed to add hooks and otherwise integrate with the wider system, |
| 34 | * except in the case where the current user is not a site administrator, no hooks will |
| 35 | * be initialized. |
| 36 | */ |
| 37 | final public function init_hooks() { |
| 38 | if ( ! Users::is_site_administrator() ) { |
| 39 | return; |
| 40 | } |
| 41 | |
| 42 | add_filter( 'woocommerce_debug_tools', array( $this, 'add_tools' ) ); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Adds Approved Directory list-related entries to the tools page. |
| 47 | * |
| 48 | * @param array $tools Admin tool definitions. |
| 49 | * |
| 50 | * @return array |
| 51 | */ |
| 52 | public function add_tools( array $tools ): array { |
| 53 | $sync = wc_get_container()->get( Synchronize::class ); |
| 54 | |
| 55 | if ( ! $sync->in_progress() ) { |
| 56 | // Provide tools to trigger a fresh scan (migration) and to clear the Approved Directories list. |
| 57 | $tools['approved_directories_sync'] = array( |
| 58 | 'name' => __( 'Synchronize approved download directories', 'woocommerce' ), |
| 59 | 'desc' => __( 'Updates the list of Approved Product Download Directories. Note that triggering this tool does not impact whether the Approved Download Directories list is enabled or not.', 'woocommerce' ), |
| 60 | 'button' => __( 'Update', 'woocommerce' ), |
| 61 | 'callback' => array( $this, 'trigger_sync' ), |
| 62 | 'requires_refresh' => true, |
| 63 | ); |
| 64 | |
| 65 | $tools['approved_directories_clear'] = array( |
| 66 | 'name' => __( 'Empty the approved download directories list', 'woocommerce' ), |
| 67 | 'desc' => __( 'Removes all existing entries from the Approved Product Download Directories list.', 'woocommerce' ), |
| 68 | 'button' => __( 'Clear', 'woocommerce' ), |
| 69 | 'callback' => array( $this, 'clear_existing_entries' ), |
| 70 | 'requires_refresh' => true, |
| 71 | ); |
| 72 | } else { |
| 73 | // Or if a scan (migration) is already in progress, offer a means of cancelling it. |
| 74 | $tools['cancel_directories_scan'] = array( |
| 75 | 'name' => __( 'Cancel synchronization of approved directories', 'woocommerce' ), |
| 76 | 'desc' => sprintf( |
| 77 | /* translators: %d is an integer between 0-100 representing the percentage complete of the current scan. */ |
| 78 | __( 'The Approved Product Download Directories list is currently being synchronized with the product catalog (%d%% complete). If you need to, you can cancel it.', 'woocommerce' ), |
| 79 | $sync->get_progress() |
| 80 | ), |
| 81 | 'button' => __( 'Cancel', 'woocommerce' ), |
| 82 | 'callback' => array( $this, 'cancel_sync' ), |
| 83 | ); |
| 84 | } |
| 85 | |
| 86 | return $tools; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Triggers a new migration. |
| 91 | */ |
| 92 | public function trigger_sync() { |
| 93 | $this->security_check(); |
| 94 | wc_get_container()->get( Synchronize::class )->start(); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Clears all existing rules from the Approved Directories list. |
| 99 | */ |
| 100 | public function clear_existing_entries() { |
| 101 | $this->security_check(); |
| 102 | $this->register->delete_all(); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * If a migration is in progress, this will attempt to cancel it. |
| 107 | */ |
| 108 | public function cancel_sync() { |
| 109 | $this->security_check(); |
| 110 | wc_get_logger()->log( 'info', __( 'Approved Download Directories sync: scan has been cancelled.', 'woocommerce' ) ); |
| 111 | wc_get_container()->get( Synchronize::class )->stop(); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Makes sure the user has appropriate permissions and that we have a valid nonce. |
| 116 | */ |
| 117 | private function security_check() { |
| 118 | if ( ! Users::is_site_administrator() ) { |
| 119 | wp_die( esc_html__( 'You do not have permission to modify the list of approved directories for product downloads.', 'woocommerce' ) ); |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 |