| 1 |
<?php |
| 2 |
namespace Imagify\Bulk; |
| 3 |
|
| 4 |
use Imagify_Filesystem; |
| 5 |
|
| 6 |
/** |
| 7 |
* Abstract class to use for bulk. |
| 8 |
* |
| 9 |
* @since 1.9 |
| 10 |
*/ |
| 11 |
abstract class AbstractBulk implements BulkInterface { |
| 12 |
/** |
| 13 |
* Filesystem object. |
| 14 |
* |
| 15 |
* @var Imagify_Filesystem |
| 16 |
* @since 1.9 |
| 17 |
*/ |
| 18 |
protected $filesystem; |
| 19 |
|
| 20 |
/** |
| 21 |
* The constructor. |
| 22 |
* |
| 23 |
* @since 1.9 |
| 24 |
*/ |
| 25 |
public function __construct() { |
| 26 |
$this->filesystem = Imagify_Filesystem::get_instance(); |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Format context data (stats). |
| 31 |
* |
| 32 |
* @since 1.9 |
| 33 |
* |
| 34 |
* @param array $data { |
| 35 |
* The data to format. |
| 36 |
* |
| 37 |
* @type int $count-optimized Number of media optimized. |
| 38 |
* @type int $count-errors Number of media having an optimization error. |
| 39 |
* @type int $optimized-size Optimized filesize. |
| 40 |
* @type int $original-size Original filesize. |
| 41 |
* @type string $errors_url URL to the page listing the optimization errors. |
| 42 |
* } |
| 43 |
* @return array { |
| 44 |
* The formated data. |
| 45 |
* |
| 46 |
* @type string $count-optimized Number of media optimized. |
| 47 |
* @type string $count-errors Number of media having an optimization error, with a link to the page listing the optimization errors. |
| 48 |
* @type string $optimized-size Optimized filesize. |
| 49 |
* @type string $original-size Original filesize. |
| 50 |
* } |
| 51 |
*/ |
| 52 |
protected function format_context_data( $data ) { |
| 53 |
$defaults = [ |
| 54 |
'count-optimized' => '', |
| 55 |
'count-errors' => '', |
| 56 |
'optimized-size' => '', |
| 57 |
'original-size' => '', |
| 58 |
]; |
| 59 |
|
| 60 |
$data = wp_parse_args( $data, $defaults ); |
| 61 |
|
| 62 |
$data = array_map( function( $item ) { |
| 63 |
return empty( $item ) ? '' : $item; |
| 64 |
}, $data ); |
| 65 |
|
| 66 |
if ( ! empty( $data['count-optimized'] ) ) { |
| 67 |
// translators: %s is a formatted number, dont use %d. |
| 68 |
$data['count-optimized'] = sprintf( _n( '%s Media File Optimized', '%s Media Files Optimized', $data['count-optimized'], 'imagify' ), '<span>' . number_format_i18n( $data['count-optimized'] ) . '</span>' ); |
| 69 |
|
| 70 |
} |
| 71 |
|
| 72 |
if ( ! empty( $data['count-errors'] ) ) { |
| 73 |
/* translators: %s is a formatted number, dont use %d. */ |
| 74 |
$data['count-errors'] = sprintf( _n( '%s Error', '%s Errors', $data['count-errors'], 'imagify' ), '<span>' . number_format_i18n( $data['count-errors'] ) . '</span>' ); |
| 75 |
$data['count-errors'] .= ' <a href="' . esc_url( $data['errors_url'] ) . '">' . __( 'View Errors', 'imagify' ) . '</a>'; |
| 76 |
} |
| 77 |
|
| 78 |
if ( ! empty( $data['optimized-size'] ) ) { |
| 79 |
$data['optimized-size'] = '<span class="imagify-cell-label">' . __( 'Optimized Filesize', 'imagify' ) . '</span> ' . imagify_size_format( $data['optimized-size'], 2 ); |
| 80 |
} |
| 81 |
|
| 82 |
if ( ! empty( $data['original-size'] ) ) { |
| 83 |
$data['original-size'] = '<span class="imagify-cell-label">' . __( 'Original Filesize', 'imagify' ) . '</span> ' . imagify_size_format( $data['original-size'], 2 ); |
| 84 |
} |
| 85 |
|
| 86 |
unset( $data['errors_url'] ); |
| 87 |
|
| 88 |
return $data; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Attempts to set no limit to the PHP timeout for time intensive processes. |
| 93 |
* |
| 94 |
* @return void |
| 95 |
*/ |
| 96 |
protected function set_no_time_limit() { |
| 97 |
if ( |
| 98 |
function_exists( 'set_time_limit' ) |
| 99 |
&& |
| 100 |
false === strpos( ini_get( 'disable_functions' ), 'set_time_limit' ) |
| 101 |
&& ! ini_get( 'safe_mode' ) // phpcs:ignore PHPCompatibility.IniDirectives.RemovedIniDirectives.safe_modeDeprecatedRemoved |
| 102 |
) { |
| 103 |
@set_time_limit( 0 ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Tell if there are optimized media without WebP versions. |
| 109 |
* |
| 110 |
* @since 1.9 |
| 111 |
* |
| 112 |
* @return int The number of media. |
| 113 |
*/ |
| 114 |
public function has_optimized_media_without_webp() { |
| 115 |
return count( $this->get_optimized_media_ids_without_webp()['ids'] ); |
| 116 |
} |
| 117 |
} |
| 118 |
|