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1.2 6.2.3 3.2.0 6.3.0 3.2.1 6.3.0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / src / Internal / ProductDownloads / ApprovedDirectories / Synchronize.php
woocommerce / src / Internal / ProductDownloads / ApprovedDirectories Last commit date
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