@@ -30,7 +30,9 @@ function extract_from_phar( $path ) {
30
30
31
31
register_shutdown_function (
32
32
function () use ( $ tmp_path ) {
33
- @unlink ( $ tmp_path );
33
+ if ( file_exists ( $ tmp_path ) ) {
34
+ unlink ( $ tmp_path );
35
+ }
34
36
}
35
37
);
36
38
@@ -58,7 +60,7 @@ function load_dependencies() {
58
60
}
59
61
60
62
if ( ! $ has_autoload ) {
61
- fputs ( STDERR , "Internal error: Can't find Composer autoloader. \nTry running: composer install \n" );
63
+ fwrite ( STDERR , "Internal error: Can't find Composer autoloader. \nTry running: composer install \n" );
62
64
exit ( 3 );
63
65
}
64
66
}
@@ -138,7 +140,7 @@ function find_file_upward( $files, $dir = null, $stop_check = null ) {
138
140
if ( is_null ( $ dir ) ) {
139
141
$ dir = getcwd ();
140
142
}
141
- while ( @ is_readable ( $ dir ) ) {
143
+ while ( is_readable ( $ dir ) ) {
142
144
// Stop walking up when the supplied callable returns true being passed the $dir
143
145
if ( is_callable ( $ stop_check ) && call_user_func ( $ stop_check , $ dir ) ) {
144
146
return null ;
@@ -166,7 +168,7 @@ function is_path_absolute( $path ) {
166
168
return true ;
167
169
}
168
170
169
- return $ path [ 0 ] === ' / ' ;
171
+ return ' / ' === $ path [ 0 ] ;
170
172
}
171
173
172
174
/**
@@ -227,12 +229,12 @@ function locate_wp_config() {
227
229
static $ path ;
228
230
229
231
if ( null === $ path ) {
232
+ $ path = false ;
233
+
230
234
if ( file_exists ( ABSPATH . 'wp-config.php ' ) ) {
231
235
$ path = ABSPATH . 'wp-config.php ' ;
232
236
} elseif ( file_exists ( ABSPATH . '../wp-config.php ' ) && ! file_exists ( ABSPATH . '/../wp-settings.php ' ) ) {
233
237
$ path = ABSPATH . '../wp-config.php ' ;
234
- } else {
235
- $ path = false ;
236
238
}
237
239
238
240
if ( $ path ) {
@@ -244,7 +246,9 @@ function locate_wp_config() {
244
246
}
245
247
246
248
function wp_version_compare ( $ since , $ operator ) {
247
- return version_compare ( str_replace ( array ( '-src ' ), '' , $ GLOBALS ['wp_version ' ] ), $ since , $ operator );
249
+ $ wp_version = str_replace ( '-src ' , '' , $ GLOBALS ['wp_version ' ] );
250
+ $ since = str_replace ( '-src ' , '' , $ since );
251
+ return version_compare ( $ wp_version , $ since , $ operator );
248
252
}
249
253
250
254
/**
@@ -358,9 +362,9 @@ function launch_editor_for_input( $input, $filename = 'WP-CLI' ) {
358
362
do {
359
363
$ tmpfile = basename ( $ filename );
360
364
$ tmpfile = preg_replace ( '|\.[^.]*$| ' , '' , $ tmpfile );
361
- $ tmpfile .= '- ' . substr ( md5 ( rand () ), 0 , 6 );
365
+ $ tmpfile .= '- ' . substr ( md5 ( mt_rand () ), 0 , 6 );
362
366
$ tmpfile = $ tmpdir . $ tmpfile . '.tmp ' ;
363
- $ fp = @ fopen ( $ tmpfile , 'x ' );
367
+ $ fp = fopen ( $ tmpfile , 'xb ' );
364
368
if ( ! $ fp && is_writable ( $ tmpdir ) && file_exists ( $ tmpfile ) ) {
365
369
$ tmpfile = '' ;
366
370
continue ;
@@ -379,10 +383,10 @@ function launch_editor_for_input( $input, $filename = 'WP-CLI' ) {
379
383
380
384
$ editor = getenv ( 'EDITOR ' );
381
385
if ( ! $ editor ) {
386
+ $ editor = 'vi ' ;
387
+
382
388
if ( isset ( $ _SERVER ['OS ' ] ) && false !== strpos ( $ _SERVER ['OS ' ], 'indows ' ) ) {
383
389
$ editor = 'notepad ' ;
384
- } else {
385
- $ editor = 'vi ' ;
386
390
}
387
391
}
388
392
@@ -416,9 +420,9 @@ function mysql_host_to_cli_args( $raw_host ) {
416
420
list ( $ assoc_args ['host ' ], $ extra ) = $ host_parts ;
417
421
$ extra = trim ( $ extra );
418
422
if ( is_numeric ( $ extra ) ) {
419
- $ assoc_args ['port ' ] = intval ( $ extra ) ;
423
+ $ assoc_args ['port ' ] = ( int ) $ extra ;
420
424
$ assoc_args ['protocol ' ] = 'tcp ' ;
421
- } elseif ( $ extra !== '' ) {
425
+ } elseif ( '' !== $ extra ) {
422
426
$ assoc_args ['socket ' ] = $ extra ;
423
427
}
424
428
} else {
@@ -436,7 +440,9 @@ function run_mysql_command( $cmd, $assoc_args, $descriptors = null ) {
436
440
}
437
441
438
442
if ( isset ( $ assoc_args ['host ' ] ) ) {
443
+ //@codingStandardsIgnoreStart
439
444
$ assoc_args = array_merge ( $ assoc_args , mysql_host_to_cli_args ( $ assoc_args ['host ' ] ) );
445
+ //@codingStandardsIgnoreEnd
440
446
}
441
447
442
448
$ pass = $ assoc_args ['pass ' ];
@@ -581,6 +587,7 @@ function replace_path_consts( $source, $path ) {
581
587
function http_request ( $ method , $ url , $ data = null , $ headers = array (), $ options = array () ) {
582
588
583
589
$ cert_path = '/rmccue/requests/library/Requests/Transport/cacert.pem ' ;
590
+ $ halt_on_error = ! isset ( $ options ['halt_on_error ' ] ) || (bool ) $ options ['halt_on_error ' ];
584
591
if ( inside_phar () ) {
585
592
// cURL can't read Phar archives
586
593
$ options ['verify ' ] = extract_from_phar (
@@ -594,25 +601,36 @@ function http_request( $method, $url, $data = null, $headers = array(), $options
594
601
}
595
602
}
596
603
if ( empty ( $ options ['verify ' ] ) ) {
597
- WP_CLI ::error ( 'Cannot find SSL certificate. ' );
604
+ $ error_msg = 'Cannot find SSL certificate. ' ;
605
+ if ( $ halt_on_error ) {
606
+ WP_CLI ::error ( $ error_msg );
607
+ }
608
+ throw new \RuntimeException ( $ error_msg );
598
609
}
599
610
}
600
611
601
612
try {
602
- $ request = \Requests::request ( $ url , $ headers , $ data , $ method , $ options );
603
- return $ request ;
613
+ return \Requests::request ( $ url , $ headers , $ data , $ method , $ options );
604
614
} catch ( \Requests_Exception $ ex ) {
605
615
// CURLE_SSL_CACERT_BADFILE only defined for PHP >= 7.
606
616
if ( 'curlerror ' !== $ ex ->getType () || ! in_array ( curl_errno ( $ ex ->getData () ), array ( CURLE_SSL_CONNECT_ERROR , CURLE_SSL_CERTPROBLEM , 77 /*CURLE_SSL_CACERT_BADFILE*/ ), true ) ) {
607
- \WP_CLI ::error ( sprintf ( "Failed to get url '%s': %s. " , $ url , $ ex ->getMessage () ) );
617
+ $ error_msg = sprintf ( "Failed to get url '%s': %s. " , $ url , $ ex ->getMessage () );
618
+ if ( $ halt_on_error ) {
619
+ WP_CLI ::error ( $ error_msg );
620
+ }
621
+ throw new \RuntimeException ( $ error_msg , null , $ ex );
608
622
}
609
623
// Handle SSL certificate issues gracefully
610
624
\WP_CLI ::warning ( sprintf ( "Re-trying without verify after failing to get verified url '%s' %s. " , $ url , $ ex ->getMessage () ) );
611
625
$ options ['verify ' ] = false ;
612
626
try {
613
627
return \Requests::request ( $ url , $ headers , $ data , $ method , $ options );
614
628
} catch ( \Requests_Exception $ ex ) {
615
- \WP_CLI ::error ( sprintf ( "Failed to get non-verified url '%s' %s. " , $ url , $ ex ->getMessage () ) );
629
+ $ error_msg = sprintf ( "Failed to get non-verified url '%s' %s. " , $ url , $ ex ->getMessage () );
630
+ if ( $ halt_on_error ) {
631
+ WP_CLI ::error ( $ error_msg );
632
+ }
633
+ throw new \RuntimeException ( $ error_msg , null , $ ex );
616
634
}
617
635
}
618
636
}
@@ -698,11 +716,13 @@ function get_named_sem_ver( $new_version, $original_version ) {
698
716
699
717
if ( ! is_null ( $ minor ) && Semver::satisfies ( $ new_version , "{$ major }. {$ minor }.x " ) ) {
700
718
return 'patch ' ;
701
- } elseif ( Semver::satisfies ( $ new_version , "{$ major }.x.x " ) ) {
719
+ }
720
+
721
+ if ( Semver::satisfies ( $ new_version , "{$ major }.x.x " ) ) {
702
722
return 'minor ' ;
703
- } else {
704
- return 'major ' ;
705
723
}
724
+
725
+ return 'major ' ;
706
726
}
707
727
708
728
/**
@@ -770,16 +790,16 @@ function get_temp_dir() {
770
790
return $ temp ;
771
791
}
772
792
793
+ $ temp = '/tmp/ ' ;
794
+
773
795
// `sys_get_temp_dir()` introduced PHP 5.2.1.
774
796
if ( $ try = sys_get_temp_dir () ) {
775
797
$ temp = trailingslashit ( $ try );
776
798
} elseif ( $ try = ini_get ( 'upload_tmp_dir ' ) ) {
777
799
$ temp = trailingslashit ( $ try );
778
- } else {
779
- $ temp = '/tmp/ ' ;
780
800
}
781
801
782
- if ( ! @ is_writable ( $ temp ) ) {
802
+ if ( ! is_writable ( $ temp ) ) {
783
803
\WP_CLI ::warning ( "Temp directory isn't writable: {$ temp }" );
784
804
}
785
805
@@ -835,25 +855,28 @@ function parse_ssh_url( $url, $component = -1 ) {
835
855
* @access public
836
856
* @category Input
837
857
*
838
- * @param string $noun Resource being affected (e.g. plugin)
839
- * @param string $verb Type of action happening to the noun (e.g. activate)
840
- * @param integer $total Total number of resource being affected.
841
- * @param integer $successes Number of successful operations.
842
- * @param integer $failures Number of failures.
858
+ * @param string $noun Resource being affected (e.g. plugin)
859
+ * @param string $verb Type of action happening to the noun (e.g. activate)
860
+ * @param integer $total Total number of resource being affected.
861
+ * @param integer $successes Number of successful operations.
862
+ * @param integer $failures Number of failures.
863
+ * @param null|integer $skips Optional. Number of skipped operations. Default null (don't show skips).
843
864
*/
844
- function report_batch_operation_results ( $ noun , $ verb , $ total , $ successes , $ failures ) {
865
+ function report_batch_operation_results ( $ noun , $ verb , $ total , $ successes , $ failures, $ skips = null ) {
845
866
$ plural_noun = $ noun . 's ' ;
846
867
$ past_tense_verb = past_tense_verb ( $ verb );
847
868
$ past_tense_verb_upper = ucfirst ( $ past_tense_verb );
848
869
if ( $ failures ) {
870
+ $ failed_skipped_message = null === $ skips ? '' : " ( {$ failures } failed " . ( $ skips ? ", {$ skips } skipped " : '' ) . ') ' ;
849
871
if ( $ successes ) {
850
- WP_CLI ::error ( "Only {$ past_tense_verb } {$ successes } of {$ total } {$ plural_noun }. " );
872
+ WP_CLI ::error ( "Only {$ past_tense_verb } {$ successes } of {$ total } {$ plural_noun }{ $ failed_skipped_message } . " );
851
873
} else {
852
- WP_CLI ::error ( "No {$ plural_noun } {$ past_tense_verb }. " );
874
+ WP_CLI ::error ( "No {$ plural_noun } {$ past_tense_verb }{ $ failed_skipped_message } . " );
853
875
}
854
876
} else {
855
- if ( $ successes ) {
856
- WP_CLI ::success ( "{$ past_tense_verb_upper } {$ successes } of {$ total } {$ plural_noun }. " );
877
+ $ skipped_message = $ skips ? " ( {$ skips } skipped) " : '' ;
878
+ if ( $ successes || $ skips ) {
879
+ WP_CLI ::success ( "{$ past_tense_verb_upper } {$ successes } of {$ total } {$ plural_noun }{$ skipped_message }. " );
857
880
} else {
858
881
$ message = $ total > 1 ? ucfirst ( $ plural_noun ) : ucfirst ( $ noun );
859
882
WP_CLI ::success ( "{$ message } already {$ past_tense_verb }. " );
@@ -876,7 +899,7 @@ function parse_str_to_argv( $arguments ) {
876
899
$ argv = array_map (
877
900
function ( $ arg ) {
878
901
foreach ( array ( '" ' , "' " ) as $ char ) {
879
- if ( $ char === substr ( $ arg , 0 , 1 ) && $ char === substr ( $ arg , -1 ) ) {
902
+ if ( substr ( $ arg , 0 , 1 ) === $ char && substr ( $ arg , -1 ) === $ char ) {
880
903
$ arg = substr ( $ arg , 1 , -1 );
881
904
break ;
882
905
}
@@ -916,14 +939,15 @@ function basename( $path, $suffix = '' ) {
916
939
*
917
940
* @return bool
918
941
*/
942
+ // @codingStandardsIgnoreLine
919
943
function isPiped () {
920
944
$ shellPipe = getenv ( 'SHELL_PIPE ' );
921
945
922
- if ( $ shellPipe !== false ) {
946
+ if ( false !== $ shellPipe ) {
923
947
return filter_var ( $ shellPipe , FILTER_VALIDATE_BOOLEAN );
924
- } else {
925
- return (function_exists ( 'posix_isatty ' ) && ! posix_isatty ( STDOUT ));
926
948
}
949
+
950
+ return (function_exists ( 'posix_isatty ' ) && ! posix_isatty ( STDOUT ));
927
951
}
928
952
929
953
/**
@@ -992,9 +1016,11 @@ function glob_brace( $pattern, $dummy_flags = null ) {
992
1016
}
993
1017
$ current ++;
994
1018
} else {
995
- if ( ( '} ' === $ pattern [ $ current ] && $ depth -- === 0 ) || ( ', ' === $ pattern [ $ current ] && 0 === $ depth ) ) {
1019
+ if ( ( '} ' === $ pattern [ $ current ] && 0 === $ depth -- ) || ( ', ' === $ pattern [ $ current ] && 0 === $ depth ) ) {
996
1020
break ;
997
- } elseif ( '{ ' === $ pattern [ $ current ++ ] ) {
1021
+ }
1022
+
1023
+ if ( '{ ' === $ pattern [ $ current ++ ] ) {
998
1024
$ depth ++;
999
1025
}
1000
1026
}
@@ -1038,7 +1064,7 @@ function glob_brace( $pattern, $dummy_flags = null ) {
1038
1064
. substr ( $ pattern , $ p , $ next - $ p )
1039
1065
. substr ( $ pattern , $ rest + 1 );
1040
1066
1041
- if ( ( $ result = glob_brace ( $ subpattern ) ) ) {
1067
+ if ( $ result = glob_brace ( $ subpattern ) ) {
1042
1068
$ paths = array_merge ( $ paths , $ result );
1043
1069
}
1044
1070
@@ -1070,6 +1096,31 @@ function glob_brace( $pattern, $dummy_flags = null ) {
1070
1096
* @return string
1071
1097
*/
1072
1098
function get_suggestion ( $ target , array $ options , $ threshold = 2 ) {
1099
+
1100
+ $ suggestion_map = array (
1101
+ 'check ' => 'check-update ' ,
1102
+ 'clear ' => 'flush ' ,
1103
+ 'decrement ' => 'decr ' ,
1104
+ 'del ' => 'delete ' ,
1105
+ 'directory ' => 'dir ' ,
1106
+ 'exec ' => 'eval ' ,
1107
+ 'exec-file ' => 'eval-file ' ,
1108
+ 'increment ' => 'incr ' ,
1109
+ 'language ' => 'locale ' ,
1110
+ 'lang ' => 'locale ' ,
1111
+ 'new ' => 'create ' ,
1112
+ 'number ' => 'count ' ,
1113
+ 'remove ' => 'delete ' ,
1114
+ 'regen ' => 'regenerate ' ,
1115
+ 'rep ' => 'replace ' ,
1116
+ 'repl ' => 'replace ' ,
1117
+ 'v ' => 'version ' ,
1118
+ );
1119
+
1120
+ if ( array_key_exists ( $ target , $ suggestion_map ) ) {
1121
+ return $ suggestion_map [ $ target ];
1122
+ }
1123
+
1073
1124
if ( empty ( $ options ) ) {
1074
1125
return '' ;
1075
1126
}
0 commit comments