-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImage_Size_Generator.php
More file actions
142 lines (125 loc) · 4.4 KB
/
Copy pathImage_Size_Generator.php
File metadata and controls
142 lines (125 loc) · 4.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
declare( strict_types=1 );
namespace Tribe\Libs\Generators;
use function WP_CLI\Utils\get_flag_value;
class Image_Size_Generator extends Generator_Command {
protected function command() {
return 'generate image-size';
}
protected function description() {
return __( 'Adds an image size to the core plugin Image_Sizes class.', 'tribe' );
}
protected function arguments() {
return [
[
'type' => 'positional',
'name' => 'name',
'optional' => false,
'description' => __( 'The name of the image size. E.g., "core-medium"', 'tribe' ),
],
[
'type' => 'assoc',
'name' => 'width',
'optional' => true,
'description' => __( 'The width of the image size. Required if height is zero.', 'tribe' ),
'default' => 0,
],
[
'type' => 'assoc',
'name' => 'height',
'optional' => true,
'description' => __( 'The height of the image size. Required if width is zero.', 'tribe' ),
'default' => 0,
],
[
'type' => 'assoc',
'name' => 'ratio',
'optional' => true,
'description' => __( 'The ratio of the image size width to height, as a decimal value. Used to calculate width or height, if one has a zero value.', 'tribe' ),
'default' => 0,
],
[
'type' => 'assoc',
'name' => 'crop',
'optional' => true,
'description' => __( 'Whether to crop the image. Accepts true, false, or a comma-delimited x,y string such as "left,top"', 'tribe' ),
'default' => false,
],
[
'type' => 'flag',
'name' => 'dry-run',
'optional' => true,
'description' => __( 'During a dry-run, no files will be written', 'tribe' ),
'default' => false,
],
];
}
public function run_command( $args, $assoc_args ) {
// read the cli arguments
$dry_run = get_flag_value( $assoc_args, 'dry-run', false );
$name = sanitize_title( $args[0] );
$const = strtoupper( str_replace( '-', '_', $name ) );
$width = absint( $assoc_args['width'] ?? 0 );
$height = absint( $assoc_args['height'] ?? 0 );
$ratio = abs( $assoc_args['ratio'] ?? 0 );
$crop = $this->sanitize_crop( $assoc_args['crop'] ?? false );
if ( empty( $width ) && empty( $height ) ) {
\WP_CLI::error( 'Either width or height must be set' );
}
// use ratio to calculate missing size
if ( $ratio ) {
if ( $width && ! $height ) {
$height = round( $width * $ratio );
} elseif ( $height && ! $width ) {
$width = round( $height / $ratio );
}
}
// translate the given values into code strings
$const_string = sprintf( "\tpublic const %s = '%s';\n", $const, $name );
$crop_string = is_array( $crop ) ? sprintf( "[ '%s', '%s' ]", $crop[0], $crop[1] ) : ( $crop === true ? 'true' : 'false' );
$definition_string = sprintf(
"\t\tself::%s => [\n\t\t\t'width' => %d,\n\t\t\t'height' => %d,\n\t\t\t'crop' => %s,\n\t\t],\n",
$const,
$width,
$height,
$crop_string
);
\WP_CLI::log( sprintf( "Adding image size '%s'", $name ) );
\WP_CLI::debug( $const_string );
\WP_CLI::debug( $definition_string );
if ( $dry_run ) {
\WP_CLI::log( 'Dry run: no files will be written.' );
return;
}
// write to disk
$file_path = $this->src_path . 'Theme/Config/Image_Sizes.php';
$this->file_system->insert_into_existing_file( $file_path, $const_string, 'class Image_Sizes' );
$this->file_system->insert_into_existing_file( $file_path, $definition_string, 'private $sizes' );
\WP_CLI::success( sprintf( "Image size '%s' added to %s", $name, $file_path ) );
}
/**
* Sanitizes the crop argument into a value that is usable by WP image sizes.
* The value may be a boolean, or an array with two strings.
*
* @param bool|string $crop
*
* @return string[]|bool
* @see add_image_size()
*/
private function sanitize_crop( $crop ) {
$boolean = filter_var( $crop, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE );
if ( ! is_null( $boolean ) ) {
return $boolean;
}
if ( ! is_string( $crop ) ) {
return false;
}
$crop_array = array_map( 'trim', explode( ',', $crop ) );
$x_values = [ 'left', 'center', 'right' ];
$y_values = [ 'top', 'center', 'bottom' ];
if ( count( $crop_array ) !== 2 || ! in_array( $crop_array[0], $x_values, true ) || ! in_array( $crop_array[1], $y_values, true ) ) {
\WP_CLI::error( sprintf( 'Invalid crop value: %s', $crop ) );
}
return $crop_array;
}
}