JsonFileFeed.php
309 lines
| 1 | <?php |
| 2 | /** |
| 3 | * JSON File Feed class. |
| 4 | * |
| 5 | * @package Automattic\WooCommerce\Internal\ProductFeed |
| 6 | */ |
| 7 | |
| 8 | declare(strict_types=1); |
| 9 | |
| 10 | namespace Automattic\WooCommerce\Internal\ProductFeed\Storage; |
| 11 | |
| 12 | use Automattic\WooCommerce\Internal\Utilities\FilesystemUtil; |
| 13 | use Automattic\WooCommerce\Internal\ProductFeed\Feed\FeedInterface; |
| 14 | use Exception; |
| 15 | |
| 16 | // This file works directly with local files. That's fine. |
| 17 | // phpcs:disable WordPress.WP.AlternativeFunctions |
| 18 | |
| 19 | /** |
| 20 | * File-backed JSON feed storage. |
| 21 | * |
| 22 | * This class writes JSON directly to a file, entry by entry, without keeping everything in memory. |
| 23 | * |
| 24 | * @since 10.5.0 |
| 25 | */ |
| 26 | class JsonFileFeed implements FeedInterface { |
| 27 | public const UPLOAD_DIR = 'product-feeds'; |
| 28 | |
| 29 | /** |
| 30 | * The number of entries added to the feed. |
| 31 | * |
| 32 | * @var int |
| 33 | */ |
| 34 | private $entry_count = 0; |
| 35 | |
| 36 | /** |
| 37 | * The base name of the feed file. |
| 38 | * |
| 39 | * @var string |
| 40 | */ |
| 41 | private $base_name; |
| 42 | |
| 43 | /** |
| 44 | * The name of the feed file, no directory. |
| 45 | * |
| 46 | * @var string |
| 47 | */ |
| 48 | private $file_name; |
| 49 | |
| 50 | /** |
| 51 | * The path to the feed file. |
| 52 | * |
| 53 | * @var string |
| 54 | */ |
| 55 | private $file_path; |
| 56 | |
| 57 | /** |
| 58 | * The file handle. |
| 59 | * |
| 60 | * @var resource|false|null |
| 61 | */ |
| 62 | private $file_handle = null; |
| 63 | |
| 64 | /** |
| 65 | * Indicates if the feed file has been completed. |
| 66 | * |
| 67 | * @var bool |
| 68 | */ |
| 69 | private $file_completed = false; |
| 70 | |
| 71 | /** |
| 72 | * The URL of the feed file. |
| 73 | * |
| 74 | * @var string|null |
| 75 | */ |
| 76 | private $file_url = null; |
| 77 | |
| 78 | /** |
| 79 | * Indicates if the feed file is in a temp directory. |
| 80 | * |
| 81 | * @var bool |
| 82 | */ |
| 83 | private $is_temp_filepath = false; |
| 84 | |
| 85 | /** |
| 86 | * Constructor. |
| 87 | * |
| 88 | * @param string $base_name The base name of the feed file. |
| 89 | */ |
| 90 | public function __construct( string $base_name ) { |
| 91 | $this->base_name = $base_name; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Start the feed. |
| 96 | * |
| 97 | * @return void |
| 98 | * @throws Exception If the feed directory cannot be created. |
| 99 | */ |
| 100 | public function start(): void { |
| 101 | $this->entry_count = 0; |
| 102 | $this->file_completed = false; |
| 103 | $this->file_url = null; |
| 104 | |
| 105 | /** |
| 106 | * Allows the current time to be overridden before a feed is stored. |
| 107 | * |
| 108 | * @param int $time The current time. |
| 109 | * @param FeedInterface $feed The feed instance. |
| 110 | * @return int The current time. |
| 111 | * @since 10.5.0 |
| 112 | */ |
| 113 | $current_time = apply_filters( 'woocommerce_product_feed_time', time(), $this ); |
| 114 | $hash_data = $this->base_name . gmdate( 'r', $current_time ); |
| 115 | $this->file_name = sprintf( |
| 116 | '%s-%s-%s.json', |
| 117 | $this->base_name, |
| 118 | gmdate( 'Y-m-d', $current_time ), |
| 119 | wp_hash( $hash_data ) |
| 120 | ); |
| 121 | |
| 122 | // Start by trying to use a temp directory to generate the feed. |
| 123 | $this->file_path = get_temp_dir() . DIRECTORY_SEPARATOR . $this->file_name; |
| 124 | $this->file_handle = fopen( $this->file_path, 'w' ); |
| 125 | if ( false === $this->file_handle ) { |
| 126 | // Fall back to immediately using the upload directory for generation. |
| 127 | $upload_dir = $this->get_upload_dir(); |
| 128 | $this->file_path = $upload_dir['path'] . $this->file_name; |
| 129 | $this->file_handle = fopen( $this->file_path, 'w' ); |
| 130 | } else { |
| 131 | $this->is_temp_filepath = true; |
| 132 | } |
| 133 | |
| 134 | if ( false === $this->file_handle ) { |
| 135 | throw new Exception( |
| 136 | esc_html( |
| 137 | sprintf( |
| 138 | /* translators: %s: directory path */ |
| 139 | __( 'Unable to open feed file for writing: %s', 'woocommerce' ), |
| 140 | $this->file_path |
| 141 | ) |
| 142 | ) |
| 143 | ); |
| 144 | } |
| 145 | |
| 146 | // Open the array. |
| 147 | fwrite( $this->file_handle, '[' ); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Add an entry to the feed. |
| 152 | * |
| 153 | * @param array $entry The entry to add. |
| 154 | * @return void |
| 155 | */ |
| 156 | public function add_entry( array $entry ): void { |
| 157 | if ( ! is_resource( $this->file_handle ) ) { |
| 158 | return; |
| 159 | } |
| 160 | |
| 161 | $json = wp_json_encode( $entry ); |
| 162 | if ( false === $json ) { |
| 163 | return; |
| 164 | } |
| 165 | |
| 166 | if ( $this->entry_count > 0 ) { |
| 167 | fwrite( $this->file_handle, ',' ); |
| 168 | } |
| 169 | |
| 170 | fwrite( $this->file_handle, $json ); |
| 171 | ++$this->entry_count; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * End the feed. |
| 176 | * |
| 177 | * @return void |
| 178 | */ |
| 179 | public function end(): void { |
| 180 | if ( ! is_resource( $this->file_handle ) ) { |
| 181 | return; |
| 182 | } |
| 183 | |
| 184 | // Close the array and the file. |
| 185 | fwrite( $this->file_handle, ']' ); |
| 186 | fclose( $this->file_handle ); |
| 187 | |
| 188 | // Indicate that we have a complete file. |
| 189 | $this->file_completed = true; |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Get the number of entries that have been added to the feed. |
| 194 | * |
| 195 | * This reflects the rows actually written to the feed, which may be fewer |
| 196 | * than the number of products iterated by `ProductWalker` because the |
| 197 | * validator can silently drop entries before they reach `add_entry()`. |
| 198 | * |
| 199 | * @since 10.9.0 |
| 200 | * @return int Number of entries added to the feed. |
| 201 | */ |
| 202 | public function get_entry_count(): int { |
| 203 | return $this->entry_count; |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * {@inheritDoc} |
| 208 | */ |
| 209 | public function get_file_path(): ?string { |
| 210 | if ( ! $this->file_completed ) { |
| 211 | return null; |
| 212 | } |
| 213 | |
| 214 | return $this->file_path; |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * {@inheritDoc} |
| 219 | * |
| 220 | * @throws Exception If the feed file cannot be moved to the upload directory. |
| 221 | */ |
| 222 | public function get_file_url(): ?string { |
| 223 | if ( ! $this->file_completed ) { |
| 224 | return null; |
| 225 | } |
| 226 | |
| 227 | $upload_dir = $this->get_upload_dir(); |
| 228 | |
| 229 | // Move the file to the upload directory if it is in temp. |
| 230 | if ( $this->is_temp_filepath ) { |
| 231 | $tmp_path = $this->file_path; |
| 232 | $this->file_path = $upload_dir['path'] . $this->file_name; |
| 233 | // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
| 234 | if ( ! @copy( $tmp_path, $this->file_path ) ) { |
| 235 | $error = error_get_last(); |
| 236 | $error_message = is_array( $error ) ? $error['message'] : 'Unknown error'; |
| 237 | throw new Exception( |
| 238 | esc_html( |
| 239 | sprintf( |
| 240 | /* translators: %1$s: file path, %2$s: error message */ |
| 241 | __( 'Unable to move feed file %1$s to upload directory: %2$s', 'woocommerce' ), |
| 242 | $this->file_path, |
| 243 | $error_message |
| 244 | ) |
| 245 | ) |
| 246 | ); |
| 247 | } |
| 248 | |
| 249 | unlink( $tmp_path ); |
| 250 | |
| 251 | $this->is_temp_filepath = false; |
| 252 | } |
| 253 | |
| 254 | // Generate the URL. |
| 255 | $this->file_url = $upload_dir['url'] . $this->file_name; |
| 256 | |
| 257 | return $this->file_url; |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Get the upload directory for the feed. |
| 262 | * |
| 263 | * @return array { |
| 264 | * The upload directory for the feed. Both fields end with the right trailing slash. |
| 265 | * |
| 266 | * @type string $path The path to the upload directory. |
| 267 | * @type string $url The URL to the upload directory. |
| 268 | * } |
| 269 | * @throws Exception If the upload directory cannot be created. |
| 270 | */ |
| 271 | private function get_upload_dir(): array { |
| 272 | // Only generate everything once. |
| 273 | static $prepared; |
| 274 | if ( isset( $prepared ) ) { |
| 275 | return $prepared; |
| 276 | } |
| 277 | |
| 278 | $upload_dir = wp_upload_dir( null, true ); |
| 279 | $directory_path = $upload_dir['basedir'] . DIRECTORY_SEPARATOR . self::UPLOAD_DIR . DIRECTORY_SEPARATOR; |
| 280 | |
| 281 | // Try to create the directory if it does not exist. |
| 282 | if ( ! is_dir( $directory_path ) ) { |
| 283 | FilesystemUtil::mkdir_p_not_indexable( $directory_path ); |
| 284 | } |
| 285 | |
| 286 | // `mkdir_p_not_indexable()` returns `void`, we have to check again. |
| 287 | if ( ! is_dir( $directory_path ) ) { |
| 288 | throw new Exception( |
| 289 | esc_html( |
| 290 | sprintf( |
| 291 | /* translators: %s: directory path */ |
| 292 | __( 'Unable to create feed directory: %s', 'woocommerce' ), |
| 293 | $directory_path |
| 294 | ) |
| 295 | ) |
| 296 | ); |
| 297 | } |
| 298 | |
| 299 | $directory_url = $upload_dir['baseurl'] . '/' . self::UPLOAD_DIR . '/'; |
| 300 | |
| 301 | // Follow the format, returned by `wp_upload_dir()`. |
| 302 | $prepared = array( |
| 303 | 'path' => $directory_path, |
| 304 | 'url' => $directory_url, |
| 305 | ); |
| 306 | return $prepared; |
| 307 | } |
| 308 | } |
| 309 |