Admin
4 years ago
ApprovedDirectoriesException.php
4 years ago
Register.php
4 years ago
StoredUrl.php
4 years ago
Synchronize.php
1 year ago
Synchronize.php
267 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Automattic\WooCommerce\Internal\ProductDownloads\ApprovedDirectories; |
| 4 | |
| 5 | use Exception; |
| 6 | use Automattic\WooCommerce\Internal\Utilities\URL; |
| 7 | use WC_Admin_Notices; |
| 8 | use WC_Product; |
| 9 | use WC_Queue_Interface; |
| 10 | |
| 11 | /** |
| 12 | * Ensures that any downloadable files have a corresponding entry in the Approved Product |
| 13 | * Download Directories list. |
| 14 | */ |
| 15 | class Synchronize { |
| 16 | /** |
| 17 | * Scheduled action hook used to facilitate scanning the product catalog for downloadable products. |
| 18 | */ |
| 19 | public const SYNC_TASK = 'woocommerce_download_dir_sync'; |
| 20 | |
| 21 | /** |
| 22 | * The group under which synchronization tasks run (our standard 'woocommerce-db-updates' group). |
| 23 | */ |
| 24 | public const SYNC_TASK_GROUP = 'woocommerce-db-updates'; |
| 25 | |
| 26 | /** |
| 27 | * Used to track progress throughout the sync process. |
| 28 | */ |
| 29 | public const SYNC_TASK_PAGE = 'wc_product_download_dir_sync_page'; |
| 30 | |
| 31 | /** |
| 32 | * Used to record an estimation of progress on the current synchronization process. 0 means 0%, |
| 33 | * 100 means 100%. |
| 34 | * |
| 35 | * @param int |
| 36 | */ |
| 37 | public const SYNC_TASK_PROGRESS = 'wc_product_download_dir_sync_progress'; |
| 38 | |
| 39 | /** |
| 40 | * Number of downloadable products to be processed in each atomic sync task. |
| 41 | */ |
| 42 | public const SYNC_TASK_BATCH_SIZE = 20; |
| 43 | |
| 44 | /** |
| 45 | * WC Queue. |
| 46 | * |
| 47 | * @var WC_Queue_Interface |
| 48 | */ |
| 49 | private $queue; |
| 50 | |
| 51 | /** |
| 52 | * Register of approved directories. |
| 53 | * |
| 54 | * @var Register |
| 55 | */ |
| 56 | private $register; |
| 57 | |
| 58 | /** |
| 59 | * Sets up our checks and controls for downloadable asset URLs, as appropriate for |
| 60 | * the current approved download directory mode. |
| 61 | * |
| 62 | * @internal |
| 63 | * @throws Exception If the WC_Queue instance cannot be obtained. |
| 64 | * |
| 65 | * @param Register $register The active approved download directories instance in use. |
| 66 | */ |
| 67 | final public function init( Register $register ) { |
| 68 | $this->queue = WC()->get_instance_of( WC_Queue_Interface::class ); |
| 69 | $this->register = $register; |
| 70 | |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Performs any work needed to add hooks and otherwise integrate with the wider system. |
| 75 | */ |
| 76 | final public function init_hooks() { |
| 77 | add_action( self::SYNC_TASK, array( $this, 'run' ) ); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Initializes the Approved Download Directories feature, typically following an update or |
| 82 | * during initial installation. |
| 83 | * |
| 84 | * @param bool $synchronize Synchronize with existing product downloads. Not needed in a fresh installation. |
| 85 | * @param bool $enable_feature Enable (default) or disable the feature. |
| 86 | */ |
| 87 | public function init_feature( bool $synchronize = true, bool $enable_feature = true ) { |
| 88 | try { |
| 89 | $this->add_default_directories(); |
| 90 | |
| 91 | if ( $synchronize ) { |
| 92 | $this->start(); |
| 93 | } |
| 94 | } catch ( Exception $e ) { |
| 95 | wc_get_logger()->log( 'warning', __( 'It was not possible to synchronize download directories following the most recent update.', 'woocommerce' ) ); |
| 96 | } |
| 97 | |
| 98 | $this->register->set_mode( |
| 99 | $enable_feature ? Register::MODE_ENABLED : Register::MODE_DISABLED |
| 100 | ); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * By default we add the woocommerce_uploads directory (file path plus web URL) to the list |
| 105 | * of approved download directories. |
| 106 | * |
| 107 | * @throws Exception If the default directories cannot be added to the Approved List. |
| 108 | */ |
| 109 | public function add_default_directories() { |
| 110 | $upload_dir = wp_get_upload_dir(); |
| 111 | $this->register->add_approved_directory( $upload_dir['basedir'] . '/woocommerce_uploads' ); |
| 112 | $this->register->add_approved_directory( $upload_dir['baseurl'] . '/woocommerce_uploads' ); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Starts the synchronization process. |
| 117 | * |
| 118 | * @return bool |
| 119 | */ |
| 120 | public function start(): bool { |
| 121 | if ( null !== $this->queue->get_next( self::SYNC_TASK ) ) { |
| 122 | wc_get_logger()->log( 'warning', __( 'Synchronization of approved product download directories is already in progress.', 'woocommerce' ) ); |
| 123 | return false; |
| 124 | } |
| 125 | |
| 126 | update_option( self::SYNC_TASK_PAGE, 1 ); |
| 127 | $this->queue->schedule_single( time(), self::SYNC_TASK, array(), self::SYNC_TASK_GROUP ); |
| 128 | wc_get_logger()->log( 'info', __( 'Approved Download Directories sync: new scan scheduled.', 'woocommerce' ) ); |
| 129 | return true; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Runs the synchronization task. |
| 134 | */ |
| 135 | public function run() { |
| 136 | $products = $this->get_next_set_of_downloadable_products(); |
| 137 | |
| 138 | foreach ( $products as $product ) { |
| 139 | $this->process_product( $product ); |
| 140 | } |
| 141 | |
| 142 | // Detect if we have reached the end of the task. |
| 143 | if ( count( $products ) < self::SYNC_TASK_BATCH_SIZE ) { |
| 144 | wc_get_logger()->log( 'info', __( 'Approved Download Directories sync: scan is complete!', 'woocommerce' ) ); |
| 145 | $this->stop(); |
| 146 | } else { |
| 147 | wc_get_logger()->log( |
| 148 | 'info', |
| 149 | sprintf( |
| 150 | /* translators: %1$d is the current batch in the synchronization task, %2$d is the percent complete. */ |
| 151 | __( 'Approved Download Directories sync: completed batch %1$d (%2$d%% complete).', 'woocommerce' ), |
| 152 | (int) get_option( self::SYNC_TASK_PAGE, 2 ) - 1, |
| 153 | $this->get_progress() |
| 154 | ) |
| 155 | ); |
| 156 | $this->queue->schedule_single( time() + 1, self::SYNC_TASK, array(), self::SYNC_TASK_GROUP ); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Stops/cancels the current synchronization task. |
| 162 | */ |
| 163 | public function stop() { |
| 164 | WC_Admin_Notices::add_notice( 'download_directories_sync_complete', true ); |
| 165 | delete_option( self::SYNC_TASK_PAGE ); |
| 166 | delete_option( self::SYNC_TASK_PROGRESS ); |
| 167 | $this->queue->cancel( self::SYNC_TASK ); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Queries for the next batch of downloadable products, applying logic to ensure we only fetch those that actually |
| 172 | * have downloadable files (a downloadable product can be created that does not have downloadable files and/or |
| 173 | * downloadable files can be removed from existing downloadable products). |
| 174 | * |
| 175 | * @return array |
| 176 | */ |
| 177 | private function get_next_set_of_downloadable_products(): array { |
| 178 | $query_filter = function ( array $query ): array { |
| 179 | $query['meta_query'][] = array( |
| 180 | 'key' => '_downloadable_files', |
| 181 | 'compare' => 'EXISTS', |
| 182 | ); |
| 183 | |
| 184 | return $query; |
| 185 | }; |
| 186 | |
| 187 | $page = (int) get_option( self::SYNC_TASK_PAGE, 1 ); |
| 188 | add_filter( 'woocommerce_product_data_store_cpt_get_products_query', $query_filter ); |
| 189 | |
| 190 | $products = wc_get_products( |
| 191 | array( |
| 192 | 'limit' => self::SYNC_TASK_BATCH_SIZE, |
| 193 | 'page' => $page, |
| 194 | 'paginate' => true, |
| 195 | ) |
| 196 | ); |
| 197 | |
| 198 | remove_filter( 'woocommerce_product_data_store_cpt_get_products_query', $query_filter ); |
| 199 | $progress = $products->max_num_pages > 0 ? (int) ( ( $page / $products->max_num_pages ) * 100 ) : 1; |
| 200 | update_option( self::SYNC_TASK_PAGE, $page + 1 ); |
| 201 | update_option( self::SYNC_TASK_PROGRESS, $progress ); |
| 202 | |
| 203 | return $products->products; |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Processes an individual downloadable product, adding the parent paths for any downloadable files to the |
| 208 | * Approved Download Directories list. |
| 209 | * |
| 210 | * Any such paths will be added with the disabled flag set, because we want a site administrator to review |
| 211 | * and approve first. |
| 212 | * |
| 213 | * @param WC_Product $product The product we wish to examine for downloadable file paths. |
| 214 | */ |
| 215 | private function process_product( WC_Product $product ) { |
| 216 | $downloads = $product->get_downloads(); |
| 217 | |
| 218 | foreach ( $downloads as $downloadable ) { |
| 219 | $parent_url = _x( 'invalid URL', 'Approved product download URLs migration', 'woocommerce' ); |
| 220 | |
| 221 | try { |
| 222 | $download_file = $downloadable->get_file(); |
| 223 | |
| 224 | /** |
| 225 | * Controls whether shortcodes should be resolved and validated using the Approved Download Directory feature. |
| 226 | * |
| 227 | * @param bool $should_validate |
| 228 | */ |
| 229 | if ( apply_filters( 'woocommerce_product_downloads_approved_directory_validation_for_shortcodes', true ) && 'shortcode' === $downloadable->get_type_of_file_path() ) { |
| 230 | $download_file = do_shortcode( $download_file ); |
| 231 | } |
| 232 | |
| 233 | $parent_url = ( new URL( $download_file ) )->get_parent_url(); |
| 234 | $this->register->add_approved_directory( $parent_url, false ); |
| 235 | } catch ( Exception $e ) { |
| 236 | wc_get_logger()->log( |
| 237 | 'error', |
| 238 | sprintf( |
| 239 | /* translators: %s is a URL, %d is a product ID. */ |
| 240 | __( 'Product download migration: %1$s (for product %1$d) could not be added to the list of approved download directories.', 'woocommerce' ), |
| 241 | $parent_url, |
| 242 | $product->get_id() |
| 243 | ) |
| 244 | ); |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Indicates if a synchronization of product download directories is in progress. |
| 251 | * |
| 252 | * @return bool |
| 253 | */ |
| 254 | public function in_progress(): bool { |
| 255 | return (bool) get_option( self::SYNC_TASK_PAGE, false ); |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * Returns a value between 0 and 100 representing the percentage complete of the current sync. |
| 260 | * |
| 261 | * @return int |
| 262 | */ |
| 263 | public function get_progress(): int { |
| 264 | return min( 100, max( 0, (int) get_option( self::SYNC_TASK_PROGRESS, 0 ) ) ); |
| 265 | } |
| 266 | } |
| 267 |