Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/woocomme.../src/Internal/Transien...
File: TransientFilesEngine.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace Automattic\WooCommerce\Internal\TransientFiles;
[2] Fix | Delete
[3] Fix | Delete
use \DateTime;
[4] Fix | Delete
use \Exception;
[5] Fix | Delete
use \InvalidArgumentException;
[6] Fix | Delete
use Automattic\WooCommerce\Internal\RegisterHooksInterface;
[7] Fix | Delete
use Automattic\WooCommerce\Proxies\LegacyProxy;
[8] Fix | Delete
use Automattic\WooCommerce\Utilities\TimeUtil;
[9] Fix | Delete
[10] Fix | Delete
/**
[11] Fix | Delete
* Transient files engine class.
[12] Fix | Delete
*
[13] Fix | Delete
* This class contains methods that allow creating files that have an expiration date.
[14] Fix | Delete
*
[15] Fix | Delete
* A transient file is created by invoking the create_transient_file method, which accepts the file contents
[16] Fix | Delete
* and the expiration date as arguments. Transient file names are composed by concatenating the expiration date
[17] Fix | Delete
* encoded in hexadecimal (3 digits for the year, 1 for the month and 2 for the day) and a random string
[18] Fix | Delete
* of hexadecimal digits.
[19] Fix | Delete
*
[20] Fix | Delete
* Transient files are stored in a directory whose default route is
[21] Fix | Delete
* wp-content/uploads/woocommerce_transient_files/yyyy-mm-dd, where "yyyy-mm-dd" is the expiration date
[22] Fix | Delete
* (year, month and day). The base route (minus the expiration date part) can be changed via a dedicated hook.
[23] Fix | Delete
*
[24] Fix | Delete
* Transient files that haven't expired (the expiration date is today or in the future) can be obtained remotely
[25] Fix | Delete
* via a dedicated URL, <server root>/wc/file/transient/<file name>. This URL is public (no authentication is required).
[26] Fix | Delete
* The content type of the response will always be "text/html".
[27] Fix | Delete
*
[28] Fix | Delete
* Cleanup of expired files is handled by the delete_expired_files method, which can be invoked manually
[29] Fix | Delete
* but there's a dedicated scheduled action that will invoke it that can be started and stopped via a dedicated tool
[30] Fix | Delete
* available in the WooCommerce tools page. The action runs once per day but this can be customized
[31] Fix | Delete
* via a dedicated hook.
[32] Fix | Delete
*/
[33] Fix | Delete
class TransientFilesEngine implements RegisterHooksInterface {
[34] Fix | Delete
[35] Fix | Delete
private const CLEANUP_ACTION_NAME = 'woocommerce_expired_transient_files_cleanup';
[36] Fix | Delete
private const CLEANUP_ACTION_GROUP = 'wc_batch_processes';
[37] Fix | Delete
[38] Fix | Delete
/**
[39] Fix | Delete
* The instance of LegacyProxy to use.
[40] Fix | Delete
*
[41] Fix | Delete
* @var LegacyProxy
[42] Fix | Delete
*/
[43] Fix | Delete
private $legacy_proxy;
[44] Fix | Delete
[45] Fix | Delete
/**
[46] Fix | Delete
* Register hooks.
[47] Fix | Delete
*/
[48] Fix | Delete
public function register() {
[49] Fix | Delete
add_action( self::CLEANUP_ACTION_NAME, array( $this, 'handle_expired_files_cleanup_action' ) );
[50] Fix | Delete
add_filter( 'woocommerce_debug_tools', array( $this, 'add_debug_tools_entries' ), 999, 1 );
[51] Fix | Delete
[52] Fix | Delete
add_action( 'init', array( $this, 'add_endpoint' ), 0 );
[53] Fix | Delete
add_filter( 'query_vars', array( $this, 'handle_query_vars' ), 0 );
[54] Fix | Delete
add_action( 'parse_request', array( $this, 'handle_parse_request' ), 0 );
[55] Fix | Delete
}
[56] Fix | Delete
[57] Fix | Delete
/**
[58] Fix | Delete
* Class initialization, to be executed when the class is resolved by the container.
[59] Fix | Delete
*
[60] Fix | Delete
* @internal
[61] Fix | Delete
*
[62] Fix | Delete
* @param LegacyProxy $legacy_proxy The instance of LegacyProxy to use.
[63] Fix | Delete
*/
[64] Fix | Delete
final public function init( LegacyProxy $legacy_proxy ) {
[65] Fix | Delete
$this->legacy_proxy = $legacy_proxy;
[66] Fix | Delete
}
[67] Fix | Delete
[68] Fix | Delete
/**
[69] Fix | Delete
* Get the base directory where transient files are stored.
[70] Fix | Delete
*
[71] Fix | Delete
* The default base directory is the WordPress uploads directory plus "woocommerce_transient_files". This can
[72] Fix | Delete
* be changed by using the woocommerce_transient_files_directory filter.
[73] Fix | Delete
*
[74] Fix | Delete
* If the woocommerce_transient_files_directory filter is not used and the default base directory
[75] Fix | Delete
* doesn't exist, it will be created. If the filter is used it's the responsibility of the caller
[76] Fix | Delete
* to ensure that the custom directory exists, otherwise an exception will be thrown.
[77] Fix | Delete
*
[78] Fix | Delete
* The actual directory for each existing file will be the base directory plus the expiration date
[79] Fix | Delete
* of the file formatted as 'yyyy-mm-dd'.
[80] Fix | Delete
*
[81] Fix | Delete
* @return string Effective base directory where transient files are stored.
[82] Fix | Delete
* @throws Exception The custom base directory (as specified via filter) doesn't exist, or the default base directory can't be created.
[83] Fix | Delete
*/
[84] Fix | Delete
public function get_transient_files_directory(): string {
[85] Fix | Delete
$upload_dir_info = $this->legacy_proxy->call_function( 'wp_upload_dir' );
[86] Fix | Delete
$default_transient_files_directory = untrailingslashit( $upload_dir_info['basedir'] ) . '/woocommerce_transient_files';
[87] Fix | Delete
[88] Fix | Delete
/**
[89] Fix | Delete
* Filters the directory where transient files are stored.
[90] Fix | Delete
*
[91] Fix | Delete
* Note that this is used for both creating new files (with create_file_by_rendering_template)
[92] Fix | Delete
* and retrieving existing files (with get_file_by_*).
[93] Fix | Delete
*
[94] Fix | Delete
* @param string $transient_files_directory The default directory for transient files.
[95] Fix | Delete
* @return string The actual directory to use for storing transient files.
[96] Fix | Delete
*
[97] Fix | Delete
* @since 8.5.0
[98] Fix | Delete
*/
[99] Fix | Delete
$transient_files_directory = apply_filters( 'woocommerce_transient_files_directory', $default_transient_files_directory );
[100] Fix | Delete
[101] Fix | Delete
$realpathed_transient_files_directory = $this->legacy_proxy->call_function( 'realpath', $transient_files_directory );
[102] Fix | Delete
if ( false === $realpathed_transient_files_directory ) {
[103] Fix | Delete
if ( $transient_files_directory === $default_transient_files_directory ) {
[104] Fix | Delete
if ( ! $this->legacy_proxy->call_function( 'wp_mkdir_p', $transient_files_directory ) ) {
[105] Fix | Delete
throw new Exception( "Can't create directory: $transient_files_directory" );
[106] Fix | Delete
}
[107] Fix | Delete
[108] Fix | Delete
// Create infrastructure to prevent listing the contents of the transient files directory.
[109] Fix | Delete
require_once ABSPATH . 'wp-admin/includes/file.php';
[110] Fix | Delete
\WP_Filesystem();
[111] Fix | Delete
$wp_filesystem = $this->legacy_proxy->get_global( 'wp_filesystem' );
[112] Fix | Delete
$wp_filesystem->put_contents( $transient_files_directory . '/.htaccess', 'deny from all' );
[113] Fix | Delete
$wp_filesystem->put_contents( $transient_files_directory . '/index.html', '' );
[114] Fix | Delete
[115] Fix | Delete
$realpathed_transient_files_directory = $this->legacy_proxy->call_function( 'realpath', $transient_files_directory );
[116] Fix | Delete
} else {
[117] Fix | Delete
throw new Exception( "The base transient files directory doesn't exist: $transient_files_directory" );
[118] Fix | Delete
}
[119] Fix | Delete
}
[120] Fix | Delete
[121] Fix | Delete
return untrailingslashit( $realpathed_transient_files_directory );
[122] Fix | Delete
}
[123] Fix | Delete
[124] Fix | Delete
/**
[125] Fix | Delete
* Create a transient file.
[126] Fix | Delete
*
[127] Fix | Delete
* @param string $file_contents The contents of the file.
[128] Fix | Delete
* @param string|int $expiration_date A string representing the expiration date formatted as "yyyy-mm-dd", or a number representing the expiration date as a timestamp (the time of day part will be ignored).
[129] Fix | Delete
* @return string The name of the transient file created (without path information).
[130] Fix | Delete
* @throws \InvalidArgumentException Invalid expiration date (wrongly formatted, or it's a date in the past).
[131] Fix | Delete
* @throws \Exception The directory to store the file doesn't exist and can't be created.
[132] Fix | Delete
*/
[133] Fix | Delete
public function create_transient_file( string $file_contents, $expiration_date ): string {
[134] Fix | Delete
if ( is_numeric( $expiration_date ) ) {
[135] Fix | Delete
$expiration_date = gmdate( 'Y-m-d', $expiration_date );
[136] Fix | Delete
} elseif ( ! is_string( $expiration_date ) || ! TimeUtil::is_valid_date( $expiration_date, 'Y-m-d' ) ) {
[137] Fix | Delete
$expiration_date = is_scalar( $expiration_date ) ? $expiration_date : gettype( $expiration_date );
[138] Fix | Delete
throw new InvalidArgumentException( "$expiration_date is not a valid date, expected format: YYYY-MM-DD" );
[139] Fix | Delete
}
[140] Fix | Delete
[141] Fix | Delete
$expiration_date_object = DateTime::createFromFormat( 'Y-m-d', $expiration_date, TimeUtil::get_utc_date_time_zone() );
[142] Fix | Delete
$today_date_object = new DateTime( $this->legacy_proxy->call_function( 'gmdate', 'Y-m-d' ), TimeUtil::get_utc_date_time_zone() );
[143] Fix | Delete
[144] Fix | Delete
if ( $expiration_date_object < $today_date_object ) {
[145] Fix | Delete
throw new InvalidArgumentException( "The supplied expiration date, $expiration_date, is in the past" );
[146] Fix | Delete
}
[147] Fix | Delete
[148] Fix | Delete
$filename = bin2hex( $this->legacy_proxy->call_function( 'random_bytes', 16 ) );
[149] Fix | Delete
[150] Fix | Delete
$transient_files_directory = $this->get_transient_files_directory();
[151] Fix | Delete
$transient_files_directory .= '/' . $expiration_date_object->format( 'Y-m-d' );
[152] Fix | Delete
if ( ! $this->legacy_proxy->call_function( 'is_dir', $transient_files_directory ) ) {
[153] Fix | Delete
if ( ! $this->legacy_proxy->call_function( 'wp_mkdir_p', $transient_files_directory ) ) {
[154] Fix | Delete
throw new Exception( "Can't create directory: $transient_files_directory" );
[155] Fix | Delete
}
[156] Fix | Delete
}
[157] Fix | Delete
$filepath = $transient_files_directory . '/' . $filename;
[158] Fix | Delete
[159] Fix | Delete
require_once ABSPATH . 'wp-admin/includes/file.php';
[160] Fix | Delete
\WP_Filesystem();
[161] Fix | Delete
$wp_filesystem = $this->legacy_proxy->get_global( 'wp_filesystem' );
[162] Fix | Delete
if ( false === $wp_filesystem->put_contents( $filepath, $file_contents ) ) {
[163] Fix | Delete
throw new Exception( "Can't create file: $filepath" );
[164] Fix | Delete
}
[165] Fix | Delete
[166] Fix | Delete
return sprintf(
[167] Fix | Delete
'%03x%01x%02x%s',
[168] Fix | Delete
$expiration_date_object->format( 'Y' ),
[169] Fix | Delete
$expiration_date_object->format( 'm' ),
[170] Fix | Delete
$expiration_date_object->format( 'd' ),
[171] Fix | Delete
$filename
[172] Fix | Delete
);
[173] Fix | Delete
}
[174] Fix | Delete
[175] Fix | Delete
/**
[176] Fix | Delete
* Get the full physical path of a transient file given its name.
[177] Fix | Delete
*
[178] Fix | Delete
* @param string $filename The name of the transient file to locate.
[179] Fix | Delete
* @return string|null The full physical path of the file, or null if the files doesn't exist.
[180] Fix | Delete
*/
[181] Fix | Delete
public function get_transient_file_path( string $filename ): ?string {
[182] Fix | Delete
$expiration_date = self::get_expiration_date( $filename );
[183] Fix | Delete
if ( is_null( $expiration_date ) ) {
[184] Fix | Delete
return null;
[185] Fix | Delete
}
[186] Fix | Delete
[187] Fix | Delete
$file_path = $this->get_transient_files_directory() . '/' . $expiration_date . '/' . substr( $filename, 6 );
[188] Fix | Delete
[189] Fix | Delete
return is_file( $file_path ) ? $file_path : null;
[190] Fix | Delete
}
[191] Fix | Delete
[192] Fix | Delete
/**
[193] Fix | Delete
* Get the expiration date of a transient file based on its file name. The actual existence of the file is NOT checked.
[194] Fix | Delete
*
[195] Fix | Delete
* @param string $filename The name of the transient file to get the expiration date for.
[196] Fix | Delete
* @return string|null Expiration date formatted as Y-m-d, null if the file name isn't encoding a proper date.
[197] Fix | Delete
*/
[198] Fix | Delete
public static function get_expiration_date( string $filename ) : ?string {
[199] Fix | Delete
if ( strlen( $filename ) < 7 || ! ctype_xdigit( $filename ) ) {
[200] Fix | Delete
return null;
[201] Fix | Delete
}
[202] Fix | Delete
[203] Fix | Delete
$expiration_date = sprintf(
[204] Fix | Delete
'%04d-%02d-%02d',
[205] Fix | Delete
hexdec( substr( $filename, 0, 3 ) ),
[206] Fix | Delete
hexdec( substr( $filename, 3, 1 ) ),
[207] Fix | Delete
hexdec( substr( $filename, 4, 2 ) )
[208] Fix | Delete
);
[209] Fix | Delete
[210] Fix | Delete
return TimeUtil::is_valid_date( $expiration_date, 'Y-m-d' ) ? $expiration_date : null;
[211] Fix | Delete
}
[212] Fix | Delete
[213] Fix | Delete
/**
[214] Fix | Delete
* Get the public URL of a transient file. The file name is NOT checked for validity or actual existence.
[215] Fix | Delete
*
[216] Fix | Delete
* @param string $filename The name of the transient file to get the public URL for.
[217] Fix | Delete
* @return string The public URL of the file.
[218] Fix | Delete
*/
[219] Fix | Delete
public function get_public_url( string $filename ) {
[220] Fix | Delete
return $this->legacy_proxy->call_function( 'get_site_url', null, '/wc/file/transient/' . $filename );
[221] Fix | Delete
}
[222] Fix | Delete
[223] Fix | Delete
/**
[224] Fix | Delete
* Verify if a file has expired, given its full physical file path.
[225] Fix | Delete
*
[226] Fix | Delete
* Given a file name returned by 'create_transient_file', the procedure to check if it has expired is as follows:
[227] Fix | Delete
*
[228] Fix | Delete
* 1. Use 'get_transient_file_path' to obtain the full file path.
[229] Fix | Delete
* 2. If the above returns null, the file doesn't exist anymore (likely it expired and was deleted by the cleanup process).
[230] Fix | Delete
* 3. Otherwise, use 'file_has_expired' passing the obtained full file path.
[231] Fix | Delete
*
[232] Fix | Delete
* @param string $file_path The full file path to check.
[233] Fix | Delete
* @return bool True if the file has expired, false otherwise.
[234] Fix | Delete
* @throws \Exception Thrown by DateTime if a wrong file path is passed.
[235] Fix | Delete
*/
[236] Fix | Delete
public function file_has_expired( string $file_path ): bool {
[237] Fix | Delete
$dirname = dirname( $file_path );
[238] Fix | Delete
$expiration_date = basename( $dirname );
[239] Fix | Delete
$expiration_date_object = new DateTime( $expiration_date, TimeUtil::get_utc_date_time_zone() );
[240] Fix | Delete
$today_date_object = new DateTime( $this->legacy_proxy->call_function( 'gmdate', 'Y-m-d' ), TimeUtil::get_utc_date_time_zone() );
[241] Fix | Delete
return $expiration_date_object < $today_date_object;
[242] Fix | Delete
}
[243] Fix | Delete
[244] Fix | Delete
/**
[245] Fix | Delete
* Delete an existing transient file.
[246] Fix | Delete
*
[247] Fix | Delete
* @param string $filename The name of the file to delete.
[248] Fix | Delete
* @return bool True if the file has been deleted, false otherwise (the file didn't exist).
[249] Fix | Delete
*/
[250] Fix | Delete
public function delete_transient_file( string $filename ): bool {
[251] Fix | Delete
$file_path = $this->get_transient_file_path( $filename );
[252] Fix | Delete
if ( is_null( $file_path ) ) {
[253] Fix | Delete
return false;
[254] Fix | Delete
}
[255] Fix | Delete
[256] Fix | Delete
$dirname = dirname( $file_path );
[257] Fix | Delete
wp_delete_file( $file_path );
[258] Fix | Delete
$this->delete_directory_if_not_empty( $dirname );
[259] Fix | Delete
[260] Fix | Delete
return true;
[261] Fix | Delete
}
[262] Fix | Delete
[263] Fix | Delete
/**
[264] Fix | Delete
* Delete expired transient files from the filesystem.
[265] Fix | Delete
*
[266] Fix | Delete
* @param int $limit Maximum number of files to delete.
[267] Fix | Delete
* @return array "deleted_count" with the number of files actually deleted, "files_remain" that will be true if there are still files left to delete.
[268] Fix | Delete
* @throws Exception The base directory for transient files (possibly changed via filter) doesn't exist.
[269] Fix | Delete
*/
[270] Fix | Delete
public function delete_expired_files( int $limit = 1000 ): array {
[271] Fix | Delete
$expiration_date_gmt = $this->legacy_proxy->call_function( 'gmdate', 'Y-m-d' );
[272] Fix | Delete
$base_dir = $this->get_transient_files_directory();
[273] Fix | Delete
$subdirs = glob( $base_dir . '/[2-9][0-9][0-9][0-9]-[01][0-9]-[0-3][0-9]', GLOB_ONLYDIR );
[274] Fix | Delete
if ( false === $subdirs ) {
[275] Fix | Delete
throw new Exception( "Error when getting the list of subdirectories of $base_dir" );
[276] Fix | Delete
}
[277] Fix | Delete
[278] Fix | Delete
$subdirs = array_map( fn( $name ) => substr( $name, strlen( $name ) - 10, 10 ), $subdirs );
[279] Fix | Delete
$expired_subdirs = array_filter( $subdirs, fn( $name ) => $name < $expiration_date_gmt );
[280] Fix | Delete
asort( $subdirs ); // We want to delete files starting with the oldest expiration month.
[281] Fix | Delete
[282] Fix | Delete
$remaining_limit = $limit;
[283] Fix | Delete
$limit_reached = false;
[284] Fix | Delete
foreach ( $expired_subdirs as $subdir ) {
[285] Fix | Delete
$full_dir_path = $base_dir . '/' . $subdir;
[286] Fix | Delete
$files_to_delete = glob( $full_dir_path . '/*' );
[287] Fix | Delete
if ( count( $files_to_delete ) > $remaining_limit ) {
[288] Fix | Delete
$limit_reached = true;
[289] Fix | Delete
$files_to_delete = array_slice( $files_to_delete, 0, $remaining_limit );
[290] Fix | Delete
}
[291] Fix | Delete
array_map( 'wp_delete_file', $files_to_delete );
[292] Fix | Delete
$remaining_limit -= count( $files_to_delete );
[293] Fix | Delete
$this->delete_directory_if_not_empty( $full_dir_path );
[294] Fix | Delete
[295] Fix | Delete
if ( $limit_reached ) {
[296] Fix | Delete
break;
[297] Fix | Delete
}
[298] Fix | Delete
}
[299] Fix | Delete
[300] Fix | Delete
return array(
[301] Fix | Delete
'deleted_count' => $limit - $remaining_limit,
[302] Fix | Delete
'files_remain' => $limit_reached,
[303] Fix | Delete
);
[304] Fix | Delete
}
[305] Fix | Delete
[306] Fix | Delete
/**
[307] Fix | Delete
* Is the expired files cleanup action currently scheduled?
[308] Fix | Delete
*
[309] Fix | Delete
* @return bool True if the expired files cleanup action is currently scheduled, false otherwise.
[310] Fix | Delete
*/
[311] Fix | Delete
public function expired_files_cleanup_is_scheduled(): bool {
[312] Fix | Delete
return as_has_scheduled_action( self::CLEANUP_ACTION_NAME, array(), self::CLEANUP_ACTION_GROUP );
[313] Fix | Delete
}
[314] Fix | Delete
[315] Fix | Delete
/**
[316] Fix | Delete
* Schedule an action that will do one round of expired files cleanup.
[317] Fix | Delete
* The action is scheduled to run immediately. If a previous pending action exists, it's unscheduled first.
[318] Fix | Delete
*/
[319] Fix | Delete
public function schedule_expired_files_cleanup(): void {
[320] Fix | Delete
$this->unschedule_expired_files_cleanup();
[321] Fix | Delete
as_schedule_single_action( time() + 1, self::CLEANUP_ACTION_NAME, array(), self::CLEANUP_ACTION_GROUP );
[322] Fix | Delete
}
[323] Fix | Delete
[324] Fix | Delete
/**
[325] Fix | Delete
* Remove the scheduled action that does the expired files cleanup, if it's scheduled.
[326] Fix | Delete
*/
[327] Fix | Delete
public function unschedule_expired_files_cleanup(): void {
[328] Fix | Delete
if ( $this->expired_files_cleanup_is_scheduled() ) {
[329] Fix | Delete
as_unschedule_action( self::CLEANUP_ACTION_NAME, array(), self::CLEANUP_ACTION_GROUP );
[330] Fix | Delete
}
[331] Fix | Delete
}
[332] Fix | Delete
[333] Fix | Delete
/**
[334] Fix | Delete
* Run the expired files cleanup action and schedule a new one.
[335] Fix | Delete
*
[336] Fix | Delete
* If files are actually deleted then we assume that more files are pending deletion and schedule the next
[337] Fix | Delete
* action to run immediately. Otherwise (nothing was deleted) we schedule the next action for one day later
[338] Fix | Delete
* (but this can be changed via the 'woocommerce_delete_expired_transient_files_interval' filter).
[339] Fix | Delete
*
[340] Fix | Delete
* If the actual deletion process fails the next action is scheduled anyway for one day later
[341] Fix | Delete
* or for the interval given by the filter.
[342] Fix | Delete
*
[343] Fix | Delete
* NOTE: If the default interval is changed to something different from DAY_IN_SECONDS, please adjust the
[344] Fix | Delete
* "every 24h" text in add_debug_tools_entries too.
[345] Fix | Delete
*
[346] Fix | Delete
* @internal For exclusive usage of WooCommerce core, backwards compatibility not guaranteed.
[347] Fix | Delete
*/
[348] Fix | Delete
public function handle_expired_files_cleanup_action(): void {
[349] Fix | Delete
$new_interval = null;
[350] Fix | Delete
[351] Fix | Delete
try {
[352] Fix | Delete
$result = $this->delete_expired_files();
[353] Fix | Delete
if ( $result['deleted_count'] > 0 ) {
[354] Fix | Delete
$new_interval = 1;
[355] Fix | Delete
}
[356] Fix | Delete
} finally {
[357] Fix | Delete
if ( is_null( $new_interval ) ) {
[358] Fix | Delete
[359] Fix | Delete
/**
[360] Fix | Delete
* Filter to alter the interval between the actions that delete expired transient files.
[361] Fix | Delete
*
[362] Fix | Delete
* @param int $interval The default time before the next action run, in seconds.
[363] Fix | Delete
* @return int The time to actually wait before the next action run, in seconds.
[364] Fix | Delete
*
[365] Fix | Delete
* @since 8.5.0
[366] Fix | Delete
*/
[367] Fix | Delete
$new_interval = apply_filters( 'woocommerce_delete_expired_transient_files_interval', DAY_IN_SECONDS );
[368] Fix | Delete
}
[369] Fix | Delete
[370] Fix | Delete
$next_time = $this->legacy_proxy->call_function( 'time' ) + $new_interval;
[371] Fix | Delete
$this->legacy_proxy->call_function( 'as_schedule_single_action', $next_time, self::CLEANUP_ACTION_NAME, array(), self::CLEANUP_ACTION_GROUP );
[372] Fix | Delete
}
[373] Fix | Delete
}
[374] Fix | Delete
[375] Fix | Delete
/**
[376] Fix | Delete
* Add the tools to (re)schedule and un-schedule the expired files cleanup actions in the WooCommerce debug tools page.
[377] Fix | Delete
*
[378] Fix | Delete
* @param array $tools_array Original debug tools array.
[379] Fix | Delete
* @return array Updated debug tools array
[380] Fix | Delete
*
[381] Fix | Delete
* @internal For exclusive usage of WooCommerce core, backwards compatibility not guaranteed.
[382] Fix | Delete
*/
[383] Fix | Delete
public function add_debug_tools_entries( array $tools_array ): array {
[384] Fix | Delete
$cleanup_is_scheduled = $this->expired_files_cleanup_is_scheduled();
[385] Fix | Delete
[386] Fix | Delete
$tools_array['schedule_expired_transient_files_cleanup'] = array(
[387] Fix | Delete
'name' => $cleanup_is_scheduled ?
[388] Fix | Delete
__( 'Re-schedule expired transient files cleanup', 'woocommerce' ) :
[389] Fix | Delete
__( 'Schedule expired transient files cleanup', 'woocommerce' ),
[390] Fix | Delete
'desc' => $cleanup_is_scheduled ?
[391] Fix | Delete
__( 'Remove the currently scheduled action to delete expired transient files, then schedule it again for running immediately. Subsequent actions will run once every 24h.', 'woocommerce' ) :
[392] Fix | Delete
__( 'Schedule the action to delete expired transient files for running immediately. Subsequent actions will run once every 24h.', 'woocommerce' ),
[393] Fix | Delete
'button' => $cleanup_is_scheduled ?
[394] Fix | Delete
__( 'Re-schedule', 'woocommerce' ) :
[395] Fix | Delete
__( 'Schedule', 'woocommerce' ),
[396] Fix | Delete
'requires_refresh' => true,
[397] Fix | Delete
'callback' => array( $this, 'schedule_expired_files_cleanup' ),
[398] Fix | Delete
);
[399] Fix | Delete
[400] Fix | Delete
if ( $cleanup_is_scheduled ) {
[401] Fix | Delete
$tools_array['unschedule_expired_transient_files_cleanup'] = array(
[402] Fix | Delete
'name' => __( 'Un-schedule expired transient files cleanup', 'woocommerce' ),
[403] Fix | Delete
'desc' => __( "Remove the currently scheduled action to delete expired transient files. Expired files won't be automatically deleted until the 'Schedule expired transient files cleanup' tool is run again.", 'woocommerce' ),
[404] Fix | Delete
'button' => __( 'Un-schedule', 'woocommerce' ),
[405] Fix | Delete
'requires_refresh' => true,
[406] Fix | Delete
'callback' => array( $this, 'unschedule_expired_files_cleanup' ),
[407] Fix | Delete
);
[408] Fix | Delete
}
[409] Fix | Delete
[410] Fix | Delete
return $tools_array;
[411] Fix | Delete
}
[412] Fix | Delete
[413] Fix | Delete
/**
[414] Fix | Delete
* Delete a directory if it isn't empty.
[415] Fix | Delete
*
[416] Fix | Delete
* @param string $directory Full directory path.
[417] Fix | Delete
*/
[418] Fix | Delete
private function delete_directory_if_not_empty( string $directory ) {
[419] Fix | Delete
if ( ! ( new \FilesystemIterator( $directory ) )->valid() ) {
[420] Fix | Delete
rmdir( $directory );
[421] Fix | Delete
}
[422] Fix | Delete
}
[423] Fix | Delete
[424] Fix | Delete
/**
[425] Fix | Delete
* Handle the "init" action, add rewrite rules for the "wc/file" endpoint.
[426] Fix | Delete
*
[427] Fix | Delete
* @internal For exclusive usage of WooCommerce core, backwards compatibility not guaranteed.
[428] Fix | Delete
*/
[429] Fix | Delete
public static function add_endpoint() {
[430] Fix | Delete
add_rewrite_rule( '^wc/file/transient/?$', 'index.php?wc-transient-file-name=', 'top' );
[431] Fix | Delete
add_rewrite_rule( '^wc/file/transient/(.+)$', 'index.php?wc-transient-file-name=$matches[1]', 'top' );
[432] Fix | Delete
add_rewrite_endpoint( 'wc/file/transient', EP_ALL );
[433] Fix | Delete
}
[434] Fix | Delete
[435] Fix | Delete
/**
[436] Fix | Delete
* Handle the "query_vars" action, add the "wc-transient-file-name" variable for the "wc/file/transient" endpoint.
[437] Fix | Delete
*
[438] Fix | Delete
* @param array $vars The original query variables.
[439] Fix | Delete
* @return array The updated query variables.
[440] Fix | Delete
*
[441] Fix | Delete
* @internal For exclusive usage of WooCommerce core, backwards compatibility not guaranteed.
[442] Fix | Delete
*/
[443] Fix | Delete
public function handle_query_vars( $vars ) {
[444] Fix | Delete
$vars[] = 'wc-transient-file-name';
[445] Fix | Delete
return $vars;
[446] Fix | Delete
}
[447] Fix | Delete
[448] Fix | Delete
// phpcs:disable Squiz.Commenting.FunctionCommentThrowTag.Missing, WordPress.WP.AlternativeFunctions
[449] Fix | Delete
[450] Fix | Delete
/**
[451] Fix | Delete
* Handle the "parse_request" action for the "wc/file/transient" endpoint.
[452] Fix | Delete
*
[453] Fix | Delete
* If the request is not for "/wc/file/transient/<filename>" or "index.php?wc-transient-file-name=filename",
[454] Fix | Delete
* it returns without doing anything. Otherwise, it will serve the contents of the file with the provided name
[455] Fix | Delete
* if it exists, is public and has not expired; or will return a "Not found" status otherwise.
[456] Fix | Delete
*
[457] Fix | Delete
* The file will be served with a content type header of "text/html".
[458] Fix | Delete
*
[459] Fix | Delete
* @internal For exclusive usage of WooCommerce core, backwards compatibility not guaranteed.
[460] Fix | Delete
*/
[461] Fix | Delete
public function handle_parse_request() {
[462] Fix | Delete
global $wp;
[463] Fix | Delete
[464] Fix | Delete
// phpcs:ignore WordPress.Security
[465] Fix | Delete
$query_arg = wp_unslash( $_GET['wc-transient-file-name'] ?? null );
[466] Fix | Delete
if ( ! is_null( $query_arg ) ) {
[467] Fix | Delete
$wp->query_vars['wc-transient-file-name'] = $query_arg;
[468] Fix | Delete
}
[469] Fix | Delete
[470] Fix | Delete
if ( is_null( $wp->query_vars['wc-transient-file-name'] ?? null ) ) {
[471] Fix | Delete
return;
[472] Fix | Delete
}
[473] Fix | Delete
[474] Fix | Delete
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput
[475] Fix | Delete
if ( 'GET' !== ( $_SERVER['REQUEST_METHOD'] ?? null ) ) {
[476] Fix | Delete
status_header( 405 );
[477] Fix | Delete
exit();
[478] Fix | Delete
}
[479] Fix | Delete
[480] Fix | Delete
$this->serve_file_contents( $wp->query_vars['wc-transient-file-name'] );
[481] Fix | Delete
}
[482] Fix | Delete
[483] Fix | Delete
/**
[484] Fix | Delete
* Core method to serve the contents of a transient file.
[485] Fix | Delete
*
[486] Fix | Delete
* @param string $file_name Transient file id or filename.
[487] Fix | Delete
*/
[488] Fix | Delete
private function serve_file_contents( string $file_name ) {
[489] Fix | Delete
$legacy_proxy = wc_get_container()->get( LegacyProxy::class );
[490] Fix | Delete
[491] Fix | Delete
try {
[492] Fix | Delete
$file_path = $this->get_transient_file_path( $file_name );
[493] Fix | Delete
if ( is_null( $file_path ) ) {
[494] Fix | Delete
$legacy_proxy->call_function( 'status_header', 404 );
[495] Fix | Delete
$legacy_proxy->exit();
[496] Fix | Delete
}
[497] Fix | Delete
[498] Fix | Delete
if ( $this->file_has_expired( $file_path ) ) {
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function