From d28a0fba2f8b58b50224145a90376b028787a417 Mon Sep 17 00:00:00 2001 From: Sidsector9 Date: Tue, 10 Oct 2017 18:54:40 +0530 Subject: [PATCH 1/9] GH#36 Add sort feature by field type and orderby --- inc/class-command.php | 49 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/inc/class-command.php b/inc/class-command.php index f2bf32f..d2bf3d6 100644 --- a/inc/class-command.php +++ b/inc/class-command.php @@ -70,6 +70,13 @@ class Command { * * [--format=] * : Render output in a particular format. + * + * [--order=] + * : Ascending or Descending order. ASC|DESC. + * + * [--orderby=] + * : Order by fields. + * * --- * default: table * options: @@ -86,6 +93,13 @@ class Command { $focus = Utils\get_flag_value( $assoc_args, 'all', isset( $args[0] ) ? $args[0] : null ); + $order = Utils\get_flag_value( $assoc_args, 'order', 'ASC' ); + $orderby = Utils\get_flag_value( $assoc_args, 'orderby' ); + + if ( Utils\get_flag_value( $assoc_args, 'fields' ) ) { + $set_fields = explode( ',', Utils\get_flag_value( $assoc_args, 'fields' ) ); + } + $valid_stages = array( 'bootstrap', 'main_query', 'template' ); if ( $focus && ( true !== $focus && ! in_array( $focus, $valid_stages, true ) ) ) { WP_CLI::error( 'Invalid stage. Must be one of ' . implode( ', ', $valid_stages ) . ', or use --all.' ); @@ -132,9 +146,44 @@ class Command { if ( Utils\get_flag_value( $assoc_args, 'spotlight' ) ) { $loggers = self::shine_spotlight( $loggers, $metrics ); } + + if ( $orderby ) { + usort( $loggers, function( $a, $b ) use ( $order, $orderby ) { + if ( 'ASC' === $order ) { + if ( is_numeric( $a->$orderby ) && is_numeric( $b->$orderby ) ) { + return $this->compare_float( $a->$orderby, $b->$orderby ); + } else { + return strcmp( $a->$orderby, $b->$orderby ); + } + } elseif ( 'DESC' === $order ) { + if ( is_numeric( $b->$orderby ) && is_numeric( $a->$orderby ) ) { + return $this->compare_float( $b->$orderby, $a->$orderby ); + } else { + return strcmp( $b->$orderby, $a->$orderby ); + } + } + }); + } + $formatter->display_items( $loggers ); } + /** + * Function to compare floats. + * + * @param double $a Floating number. + * @param double $b Floating number. + */ + public function compare_float( $a, $b ) { + if ( abs( $a - $b ) < 0.00000001 ) { + return 0; + } elseif ( ( $a - $b ) < 0 ) { + return -1; + } else { + return 1; + } + } + /** * Profile key metrics for WordPress hooks (actions and filters). * From 806ea097788f92fa19b19a789aa6f1754803a719 Mon Sep 17 00:00:00 2001 From: Sidsector9 Date: Tue, 10 Oct 2017 19:23:01 +0530 Subject: [PATCH 2/9] GH#36 Changed compare_float() from public to private --- inc/class-command.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/class-command.php b/inc/class-command.php index d2bf3d6..e7e67b1 100644 --- a/inc/class-command.php +++ b/inc/class-command.php @@ -174,7 +174,7 @@ class Command { * @param double $a Floating number. * @param double $b Floating number. */ - public function compare_float( $a, $b ) { + private function compare_float( $a, $b ) { if ( abs( $a - $b ) < 0.00000001 ) { return 0; } elseif ( ( $a - $b ) < 0 ) { From cb164727b5d907fb3c34108566fb8e435d7a60a2 Mon Sep 17 00:00:00 2001 From: Sidsector9 Date: Wed, 11 Oct 2017 13:27:14 +0530 Subject: [PATCH 3/9] GH#36 Moved function to correct file --- features/profile.feature | 4 +-- inc/class-command.php | 60 +++++++++++----------------------------- inc/class-formatter.php | 43 ++++++++++++++++++++++++++-- 3 files changed, 58 insertions(+), 49 deletions(-) diff --git a/features/profile.feature b/features/profile.feature index 29544e9..ba0d013 100644 --- a/features/profile.feature +++ b/features/profile.feature @@ -8,8 +8,8 @@ Feature: Basic profile usage """ usage: wp profile eval [--hook[=]] [--fields=] [--format=] or: wp profile eval-file [--hook[=]] [--fields=] [--format=] - or: wp profile hook [] [--all] [--spotlight] [--url=] [--fields=] [--format=] - or: wp profile stage [] [--all] [--spotlight] [--url=] [--fields=] [--format=] + or: wp profile hook [] [--all] [--spotlight] [--url=] [--fields=] [--format=] [--order=] [--orderby=] + or: wp profile stage [] [--all] [--spotlight] [--url=] [--fields=] [--format=] [--order=] [--orderby=] See 'wp help profile ' for more information on a specific command. """ diff --git a/inc/class-command.php b/inc/class-command.php index e7e67b1..40c6958 100644 --- a/inc/class-command.php +++ b/inc/class-command.php @@ -91,15 +91,11 @@ class Command { public function stage( $args, $assoc_args ) { global $wpdb; - $focus = Utils\get_flag_value( $assoc_args, 'all', isset( $args[0] ) ? $args[0] : null ); + $focus = Utils\get_flag_value( $assoc_args, 'all', isset( $args[0] ) ? $args[0] : null ); + + $order = Utils\get_flag_value( $assoc_args, 'order', 'ASC' ); + $orderby = Utils\get_flag_value( $assoc_args, 'orderby', null ); - $order = Utils\get_flag_value( $assoc_args, 'order', 'ASC' ); - $orderby = Utils\get_flag_value( $assoc_args, 'orderby' ); - - if ( Utils\get_flag_value( $assoc_args, 'fields' ) ) { - $set_fields = explode( ',', Utils\get_flag_value( $assoc_args, 'fields' ) ); - } - $valid_stages = array( 'bootstrap', 'main_query', 'template' ); if ( $focus && ( true !== $focus && ! in_array( $focus, $valid_stages, true ) ) ) { WP_CLI::error( 'Invalid stage. Must be one of ' . implode( ', ', $valid_stages ) . ', or use --all.' ); @@ -147,41 +143,7 @@ class Command { $loggers = self::shine_spotlight( $loggers, $metrics ); } - if ( $orderby ) { - usort( $loggers, function( $a, $b ) use ( $order, $orderby ) { - if ( 'ASC' === $order ) { - if ( is_numeric( $a->$orderby ) && is_numeric( $b->$orderby ) ) { - return $this->compare_float( $a->$orderby, $b->$orderby ); - } else { - return strcmp( $a->$orderby, $b->$orderby ); - } - } elseif ( 'DESC' === $order ) { - if ( is_numeric( $b->$orderby ) && is_numeric( $a->$orderby ) ) { - return $this->compare_float( $b->$orderby, $a->$orderby ); - } else { - return strcmp( $b->$orderby, $a->$orderby ); - } - } - }); - } - - $formatter->display_items( $loggers ); - } - - /** - * Function to compare floats. - * - * @param double $a Floating number. - * @param double $b Floating number. - */ - private function compare_float( $a, $b ) { - if ( abs( $a - $b ) < 0.00000001 ) { - return 0; - } elseif ( ( $a - $b ) < 0 ) { - return -1; - } else { - return 1; - } + $formatter->display_items( $order, $orderby, $loggers ); } /** @@ -209,6 +171,13 @@ class Command { * * [--format=] * : Render output in a particular format. + * + * [--order=] + * : Ascending or Descending order. ASC|DESC. + * + * [--orderby=] + * : Order by fields. + * * --- * default: table * options: @@ -224,6 +193,9 @@ class Command { $focus = Utils\get_flag_value( $assoc_args, 'all', isset( $args[0] ) ? $args[0] : null ); + $order = Utils\get_flag_value( $assoc_args, 'order', 'ASC' ); + $orderby = Utils\get_flag_value( $assoc_args, 'orderby', null ); + $profiler = new Profiler( 'hook', $focus ); $profiler->run(); @@ -255,7 +227,7 @@ class Command { if ( Utils\get_flag_value( $assoc_args, 'spotlight' ) ) { $loggers = self::shine_spotlight( $loggers, $metrics ); } - $formatter->display_items( $loggers ); + $formatter->display_items( $order, $orderby, $loggers ); } /** diff --git a/inc/class-formatter.php b/inc/class-formatter.php index 6a73659..2a45fa5 100644 --- a/inc/class-formatter.php +++ b/inc/class-formatter.php @@ -42,21 +42,39 @@ class Formatter { * * @param array $items */ - public function display_items( $items, $include_total = true ) { + public function display_items( $order, $orderby, $items, $include_total = true ) { if ( 'table' === $this->args['format'] && empty( $this->args['field'] ) ) { - $this->show_table( $items, $this->args['fields'], $include_total ); + $this->show_table( $order, $orderby, $items, $this->args['fields'], $include_total ); } else { $this->formatter->display_items( $items ); } } + /** + * Function to compare floats. + * + * @param double $a Floating number. + * @param double $b Floating number. + */ + private function compare_float( $a, $b ) { + $a = number_format( $a, 4 ); + $b = number_format( $b, 4 ); + if ( 0 === $a - $b ) { + return 0; + } else if ( $a - $b < 0 ) { + return -1; + } else { + return 1; + } + } + /** * Show items in a \cli\Table. * * @param array $items * @param array $fields */ - private function show_table( $items, $fields, $include_total ) { + private function show_table( $order, $orderby, $items, $fields, $include_total ) { $table = new \cli\Table(); $enabled = \cli\Colors::shouldColorize(); @@ -70,6 +88,25 @@ class Formatter { if ( ! is_null( $this->total_cell_index ) ) { $totals[ $this->total_cell_index ] = 'total (' . count( $items ) . ')'; } + + if ( $orderby ) { + usort( $items, function( $a, $b ) use ( $order, $orderby ) { + if ( 'ASC' === $order ) { + if ( is_numeric( $a->$orderby ) && is_numeric( $b->$orderby ) ) { + return $this->compare_float( $a->$orderby, $b->$orderby ); + } else { + return strcmp( $a->$orderby, $b->$orderby ); + } + } elseif ( 'DESC' === $order ) { + if ( is_numeric( $b->$orderby ) && is_numeric( $a->$orderby ) ) { + return $this->compare_float( $b->$orderby, $a->$orderby ); + } else { + return strcmp( $b->$orderby, $a->$orderby ); + } + } + }); + } + $location_index = array_search( 'location', $fields ); foreach ( $items as $item ) { $values = array_values( \WP_CLI\Utils\pick_fields( $item, $fields ) ); From 292a4566f1d4898d7ac423a717f7f8f9678e30e3 Mon Sep 17 00:00:00 2001 From: Sidsector9 Date: Wed, 11 Oct 2017 14:39:22 +0530 Subject: [PATCH 4/9] GH#36 Added feature for eval and file-eval --- features/profile.feature | 4 ++-- inc/class-command.php | 26 ++++++++++++++++++++++++-- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/features/profile.feature b/features/profile.feature index ba0d013..8714d1a 100644 --- a/features/profile.feature +++ b/features/profile.feature @@ -6,8 +6,8 @@ Feature: Basic profile usage When I run `wp profile` Then STDOUT should be: """ - usage: wp profile eval [--hook[=]] [--fields=] [--format=] - or: wp profile eval-file [--hook[=]] [--fields=] [--format=] + usage: wp profile eval [--hook[=]] [--fields=] [--format=] [--order=] [--orderby=] + or: wp profile eval-file [--hook[=]] [--fields=] [--format=] [--order=] [--orderby=] or: wp profile hook [] [--all] [--spotlight] [--url=] [--fields=] [--format=] [--order=] [--orderby=] or: wp profile stage [] [--all] [--spotlight] [--url=] [--fields=] [--format=] [--order=] [--orderby=] diff --git a/inc/class-command.php b/inc/class-command.php index 40c6958..e948d71 100644 --- a/inc/class-command.php +++ b/inc/class-command.php @@ -250,6 +250,13 @@ class Command { * * [--format=] * : Render output in a particular format. + * + * [--order=] + * : Ascending or Descending order. ASC|DESC. + * + * [--orderby=] + * : Order by fields. + * * --- * default: table * options: @@ -263,7 +270,11 @@ class Command { */ public function eval_( $args, $assoc_args ) { $statement = $args[0]; - self::profile_eval_ish( $assoc_args, function() use ( $statement ) { + + $order = Utils\get_flag_value( $assoc_args, 'order', 'ASC' ); + $orderby = Utils\get_flag_value( $assoc_args, 'orderby', null ); + + self::profile_eval_ish( $order, $orderby, $assoc_args, function() use ( $statement ) { eval( $statement ); }); } @@ -288,6 +299,13 @@ class Command { * * [--format=] * : Render output in a particular format. + * + * [--order=] + * : Ascending or Descending order. ASC|DESC. + * + * [--orderby=] + * : Order by fields. + * * --- * default: table * options: @@ -302,6 +320,10 @@ class Command { public function eval_file( $args, $assoc_args ) { $file = $args[0]; + + $order = Utils\get_flag_value( $assoc_args, 'order', 'ASC' ); + $orderby = Utils\get_flag_value( $assoc_args, 'orderby', null ); + if ( ! file_exists( $file ) ) { WP_CLI::error( "'$file' does not exist." ); } @@ -351,7 +373,7 @@ class Command { 'request_count', ) ); $formatter = new Formatter( $assoc_args, $fields ); - $formatter->display_items( $loggers, false ); + $formatter->display_items( $order, $orderby, $loggers, false ); } /** From 70d062deb21ca7e8c509039038d48d1981d10352 Mon Sep 17 00:00:00 2001 From: Sidsector9 Date: Wed, 11 Oct 2017 14:49:42 +0530 Subject: [PATCH 5/9] GH#36 Fix argument sequence --- inc/class-command.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/inc/class-command.php b/inc/class-command.php index e948d71..de2199d 100644 --- a/inc/class-command.php +++ b/inc/class-command.php @@ -274,9 +274,9 @@ class Command { $order = Utils\get_flag_value( $assoc_args, 'order', 'ASC' ); $orderby = Utils\get_flag_value( $assoc_args, 'orderby', null ); - self::profile_eval_ish( $order, $orderby, $assoc_args, function() use ( $statement ) { + self::profile_eval_ish( $assoc_args, function() use ( $statement ) { eval( $statement ); - }); + }, $order, $orderby ); } /** From 7c8a48b74d2f00be755b1aff540279a03851b552 Mon Sep 17 00:00:00 2001 From: Sidsector9 Date: Wed, 11 Oct 2017 14:59:13 +0530 Subject: [PATCH 6/9] GH#36 Fixed arguments --- inc/class-command.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/class-command.php b/inc/class-command.php index de2199d..559f9be 100644 --- a/inc/class-command.php +++ b/inc/class-command.php @@ -330,7 +330,7 @@ class Command { self::profile_eval_ish( $assoc_args, function() use ( $file ) { self::include_file( $file ); - }); + }, $order, $orderby ); } /** From 60bd8d5cd09d888125339ce677f9666aacabc318 Mon Sep 17 00:00:00 2001 From: Sidsector9 Date: Wed, 11 Oct 2017 15:44:08 +0530 Subject: [PATCH 7/9] GH#36 Added functional test --- features/profile-stage.feature | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/features/profile-stage.feature b/features/profile-stage.feature index a91085e..d37b0db 100644 --- a/features/profile-stage.feature +++ b/features/profile-stage.feature @@ -65,6 +65,25 @@ Feature: Profile the template render stage | wp_footer:after | | total (13) | + When I run `wp profile stage template --fields=hook --orderby=hook --order=DESC` + Then STDOUT should be a table containing rows: + | hook | + | wp_head:before | + | wp_head | + | wp_footer:before | + | wp_footer:after | + | wp_footer | + | template_redirect:before | + | template_redirect | + | template_include:before | + | template_include | + | loop_start:before | + | loop_start | + | loop_end:before | + | loop_end | + | total (13) | + + Scenario: Use --all flag to profile all stages Given a WP install From bd0543ccd67c8d0c7d126ec9ddc521796c96635c Mon Sep 17 00:00:00 2001 From: Sidsector9 Date: Fri, 13 Oct 2017 12:14:03 +0530 Subject: [PATCH 8/9] GH#36 Corrected argument sequence and updated documentation --- inc/class-command.php | 39 +++++++++++++++++++++++++++++++-------- inc/class-formatter.php | 2 +- 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/inc/class-command.php b/inc/class-command.php index 559f9be..9f2e316 100644 --- a/inc/class-command.php +++ b/inc/class-command.php @@ -72,7 +72,13 @@ class Command { * : Render output in a particular format. * * [--order=] - * : Ascending or Descending order. ASC|DESC. + * : Ascending or Descending order. + * --- + * default: ASC + * options: + * - ASC + * - DESC + * --- * * [--orderby=] * : Order by fields. @@ -143,7 +149,7 @@ class Command { $loggers = self::shine_spotlight( $loggers, $metrics ); } - $formatter->display_items( $order, $orderby, $loggers ); + $formatter->display_items( $loggers, true, $order, $orderby ); } /** @@ -173,12 +179,17 @@ class Command { * : Render output in a particular format. * * [--order=] - * : Ascending or Descending order. ASC|DESC. + * : Ascending or Descending order. + * --- + * default: ASC + * options: + * - ASC + * - DESC + * --- * * [--orderby=] * : Order by fields. * - * --- * default: table * options: * - table @@ -227,7 +238,7 @@ class Command { if ( Utils\get_flag_value( $assoc_args, 'spotlight' ) ) { $loggers = self::shine_spotlight( $loggers, $metrics ); } - $formatter->display_items( $order, $orderby, $loggers ); + $formatter->display_items( $loggers, true, $order, $orderby ); } /** @@ -252,7 +263,13 @@ class Command { * : Render output in a particular format. * * [--order=] - * : Ascending or Descending order. ASC|DESC. + * : Ascending or Descending order. + * --- + * default: ASC + * options: + * - ASC + * - DESC + * --- * * [--orderby=] * : Order by fields. @@ -301,7 +318,13 @@ class Command { * : Render output in a particular format. * * [--order=] - * : Ascending or Descending order. ASC|DESC. + * : Ascending or Descending order. + * --- + * default: ASC + * options: + * - ASC + * - DESC + * --- * * [--orderby=] * : Order by fields. @@ -373,7 +396,7 @@ class Command { 'request_count', ) ); $formatter = new Formatter( $assoc_args, $fields ); - $formatter->display_items( $order, $orderby, $loggers, false ); + $formatter->display_items( $loggers, false, $order, $orderby ); } /** diff --git a/inc/class-formatter.php b/inc/class-formatter.php index 2a45fa5..d817392 100644 --- a/inc/class-formatter.php +++ b/inc/class-formatter.php @@ -42,7 +42,7 @@ class Formatter { * * @param array $items */ - public function display_items( $order, $orderby, $items, $include_total = true ) { + public function display_items( $items, $include_total = true, $order, $orderby ) { if ( 'table' === $this->args['format'] && empty( $this->args['field'] ) ) { $this->show_table( $order, $orderby, $items, $this->args['fields'], $include_total ); } else { From 9773f8bf5e13027db7466d009d58eb4b050e2fbb Mon Sep 17 00:00:00 2001 From: Sidsector9 Date: Wed, 25 Oct 2017 08:30:34 +0530 Subject: [PATCH 9/9] GH#36 Improvised logic --- inc/class-formatter.php | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/inc/class-formatter.php b/inc/class-formatter.php index d817392..fa0d5c6 100644 --- a/inc/class-formatter.php +++ b/inc/class-formatter.php @@ -91,19 +91,13 @@ class Formatter { if ( $orderby ) { usort( $items, function( $a, $b ) use ( $order, $orderby ) { - if ( 'ASC' === $order ) { - if ( is_numeric( $a->$orderby ) && is_numeric( $b->$orderby ) ) { - return $this->compare_float( $a->$orderby, $b->$orderby ); - } else { - return strcmp( $a->$orderby, $b->$orderby ); - } - } elseif ( 'DESC' === $order ) { - if ( is_numeric( $b->$orderby ) && is_numeric( $a->$orderby ) ) { - return $this->compare_float( $b->$orderby, $a->$orderby ); - } else { - return strcmp( $b->$orderby, $a->$orderby ); - } + list( $first, $second ) = 'ASC' === $order ? array( $a, $b ) : array( $b, $a ); + + if ( is_numeric( $first->$orderby ) && is_numeric( $second->$orderby ) ) { + return $this->compare_float( $first->$orderby, $second->$orderby ); } + + return strcmp( $first->$orderby, $second->$orderby ); }); }