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