Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Jetpack_Search often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Jetpack_Search, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 3 | class Jetpack_Search { |
||
| 4 | |||
| 5 | protected $found_posts = 0; |
||
| 6 | |||
| 7 | /** |
||
| 8 | * The maximum offset ('from' param), since deep pages get exponentially slower. |
||
| 9 | * |
||
| 10 | * @see https://www.elastic.co/guide/en/elasticsearch/guide/current/pagination.html |
||
| 11 | */ |
||
| 12 | protected $max_offset = 200; |
||
| 13 | |||
| 14 | protected $search_result; |
||
| 15 | |||
| 16 | protected $original_blog_id; |
||
| 17 | protected $jetpack_blog_id; |
||
| 18 | |||
| 19 | protected $aggregations = array(); |
||
| 20 | protected $max_aggregations_count = 100; |
||
| 21 | |||
| 22 | // used to output query meta into page |
||
| 23 | protected $last_query_info; |
||
| 24 | protected $last_query_failure_info; |
||
| 25 | |||
| 26 | protected static $instance; |
||
| 27 | |||
| 28 | //Languages with custom analyzers, other languages are supported, |
||
| 29 | // but are analyzed with the default analyzer. |
||
| 30 | public static $analyzed_langs = array( 'ar', 'bg', 'ca', 'cs', 'da', 'de', 'el', 'en', 'es', 'eu', 'fa', 'fi', 'fr', 'he', 'hi', 'hu', 'hy', 'id', 'it', 'ja', 'ko', 'nl', 'no', 'pt', 'ro', 'ru', 'sv', 'tr', 'zh' ); |
||
| 31 | |||
| 32 | protected function __construct() { |
||
| 35 | |||
| 36 | public function __clone() { |
||
| 39 | |||
| 40 | public function __wakeup() { |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Get singleton instance of Jetpack_Search |
||
| 46 | * |
||
| 47 | * Instantiates and sets up a new instance if needed, or returns the singleton |
||
| 48 | * |
||
| 49 | * @module search |
||
| 50 | * |
||
| 51 | * @return Jetpack_Search The Jetpack_Search singleton |
||
| 52 | */ |
||
| 53 | public static function instance() { |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Perform various setup tasks for the class |
||
| 65 | * |
||
| 66 | * Checks various pre-requisites and adds hooks |
||
| 67 | * |
||
| 68 | * @module search |
||
| 69 | */ |
||
| 70 | public function setup() { |
||
| 71 | if ( ! Jetpack::is_active() || ! Jetpack::active_plan_supports( 'search' ) ) { |
||
| 72 | return; |
||
| 73 | } |
||
| 74 | |||
| 75 | $this->jetpack_blog_id = Jetpack::get_option( 'id' ); |
||
| 76 | |||
| 77 | if ( ! $this->jetpack_blog_id ) { |
||
| 78 | return; |
||
| 79 | } |
||
| 80 | |||
| 81 | require_once( dirname( __FILE__ ) . '/class.jetpack-search-helpers.php' ); |
||
| 82 | |||
| 83 | $this->init_hooks(); |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Setup the various hooks needed for the plugin to take over Search duties |
||
| 88 | * |
||
| 89 | * @module search |
||
| 90 | */ |
||
| 91 | public function init_hooks() { |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Print query info as a HTML comment in the footer |
||
| 110 | */ |
||
| 111 | |||
| 112 | public function store_query_failure( $meta ) { |
||
| 116 | |||
| 117 | public function print_query_failure() { |
||
| 127 | |||
| 128 | public function store_query_success( $meta ) { |
||
| 132 | |||
| 133 | public function print_query_success() { |
||
| 142 | |||
| 143 | function are_filters_by_widget_disabled() { |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Retrives a list of known Jetpack search filters widget IDs, gets the filters for each widget, |
||
| 159 | * and applies those filters to this Jetpack_Search object. |
||
| 160 | * |
||
| 161 | * @since 5.7.0 |
||
| 162 | * |
||
| 163 | * @return void |
||
| 164 | */ |
||
| 165 | function set_filters_from_widgets() { |
||
| 176 | |||
| 177 | function maybe_add_post_type_as_var( $query ) { |
||
| 186 | |||
| 187 | /* |
||
| 188 | * Run a search on the WP.com public API. |
||
| 189 | * |
||
| 190 | * @module search |
||
| 191 | * |
||
| 192 | * @param array $es_args Args conforming to the WP.com /sites/<blog_id>/search endpoint |
||
| 193 | * |
||
| 194 | * @return object|WP_Error The response from the public api, or a WP_Error |
||
| 195 | */ |
||
| 196 | public function search( array $es_args ) { |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Bypass the normal Search query and offload it to Jetpack servers |
||
| 293 | * |
||
| 294 | * This is the main hook of the plugin and is responsible for returning the posts that match the search query |
||
| 295 | * |
||
| 296 | * @module search |
||
| 297 | * |
||
| 298 | * @param array $posts Current array of posts (still pre-query) |
||
| 299 | * @param WP_Query $query The WP_Query being filtered |
||
| 300 | * |
||
| 301 | * @return array Array of matching posts |
||
| 302 | */ |
||
| 303 | public function filter__posts_pre_query( $posts, $query ) { |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Build up the search, then run it against the Jetpack servers |
||
| 353 | * |
||
| 354 | * @param WP_Query $query The original WP_Query to use for the parameters of our search |
||
| 355 | */ |
||
| 356 | public function do_search( WP_Query $query ) { |
||
| 463 | |||
| 464 | /** |
||
| 465 | * If the query has already been run before filters have been updated, then we need to re-run the query |
||
| 466 | * to get the latest aggregations. |
||
| 467 | * |
||
| 468 | * This is especially useful for supporting widget management in the customizer. |
||
| 469 | * |
||
| 470 | * @return bool Whether the query was successful or not. |
||
| 471 | */ |
||
| 472 | public function update_search_results_aggregations() { |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Given a WP_Query, convert its WP_Tax_Query (if present) into the WP-style ES term arguments for the search |
||
| 489 | * |
||
| 490 | * @module search |
||
| 491 | * |
||
| 492 | * @param WP_Query $query The original WP_Query object for which to parse the taxonomy query |
||
| 493 | * |
||
| 494 | * @return array The new WP-style ES arguments (that will be converted into 'real' ES arguments) |
||
| 495 | */ |
||
| 496 | public function get_es_wp_query_terms_for_query( WP_Query $query ) { |
||
| 529 | |||
| 530 | /** |
||
| 531 | * Parse out the post type from a WP_Query |
||
| 532 | * |
||
| 533 | * Only allows post types that are not marked as 'exclude_from_search' |
||
| 534 | * |
||
| 535 | * @module search |
||
| 536 | * |
||
| 537 | * @param WP_Query $query Original WP_Query object |
||
| 538 | * |
||
| 539 | * @return array Array of searchable post types corresponding to the original query |
||
| 540 | */ |
||
| 541 | public function get_es_wp_query_post_type_for_query( WP_Query $query ) { |
||
| 575 | |||
| 576 | /** |
||
| 577 | * Initialze widgets for the Search module |
||
| 578 | * |
||
| 579 | * @module search |
||
| 580 | */ |
||
| 581 | public function action__widgets_init() { |
||
| 586 | |||
| 587 | /** |
||
| 588 | * Get the Elasticsearch result |
||
| 589 | * |
||
| 590 | * @module search |
||
| 591 | * |
||
| 592 | * @param bool $raw If true, does not check for WP_Error or return the 'results' array - the JSON decoded HTTP response |
||
| 593 | * |
||
| 594 | * @return array|bool The search results, or false if there was a failure |
||
| 595 | */ |
||
| 596 | public function get_search_result( $raw = false ) { |
||
| 603 | |||
| 604 | /** |
||
| 605 | * Add the date portion of a WP_Query onto the query args |
||
| 606 | * |
||
| 607 | * @param array $es_wp_query_args |
||
| 608 | * @param WP_Query $query The original WP_Query |
||
| 609 | * |
||
| 610 | * @return array The es wp query args, with date filters added (as needed) |
||
| 611 | */ |
||
| 612 | public function filter__add_date_filter_to_query( array $es_wp_query_args, WP_Query $query ) { |
||
| 644 | |||
| 645 | /** |
||
| 646 | * Converts WP_Query style args to ES args |
||
| 647 | * |
||
| 648 | * @module search |
||
| 649 | * |
||
| 650 | * @param array $args Array of WP_Query style arguments |
||
| 651 | * |
||
| 652 | * @return array Array of ES style query arguments |
||
| 653 | */ |
||
| 654 | function convert_wp_es_to_es_args( array $args ) { |
||
| 902 | |||
| 903 | /** |
||
| 904 | * Given an array of aggregations, parse and add them onto the Jetpack_WPES_Query_Builder object for use in ES |
||
| 905 | * |
||
| 906 | * @module search |
||
| 907 | * |
||
| 908 | * @param array $aggregations Array of Aggregations (filters) to add to the Jetpack_WPES_Query_Builder |
||
| 909 | * |
||
| 910 | * @param Jetpack_WPES_Query_Builder $builder The builder instance that is creating the ES query |
||
| 911 | */ |
||
| 912 | public function add_aggregations_to_es_query_builder( array $aggregations, Jetpack_WPES_Query_Builder $builder ) { |
||
| 932 | |||
| 933 | /** |
||
| 934 | * Given an individual taxonomy aggregation, add it to the Jetpack_WPES_Query_Builder object for use in ES |
||
| 935 | * |
||
| 936 | * @module search |
||
| 937 | * |
||
| 938 | * @param array $aggregation The aggregation to add to the query builder |
||
| 939 | * @param string $label The 'label' (unique id) for this aggregation |
||
| 940 | * @param Jetpack_WPES_Query_Builder $builder The builder instance that is creating the ES query |
||
| 941 | */ |
||
| 942 | public function add_taxonomy_aggregation_to_es_query_builder( array $aggregation, $label, Jetpack_WPES_Query_Builder $builder ) { |
||
| 966 | |||
| 967 | /** |
||
| 968 | * Given an individual post_type aggregation, add it to the Jetpack_WPES_Query_Builder object for use in ES |
||
| 969 | * |
||
| 970 | * @module search |
||
| 971 | * |
||
| 972 | * @param array $aggregation The aggregation to add to the query builder |
||
| 973 | * @param string $label The 'label' (unique id) for this aggregation |
||
| 974 | * @param Jetpack_WPES_Query_Builder $builder The builder instance that is creating the ES query |
||
| 975 | */ |
||
| 976 | public function add_post_type_aggregation_to_es_query_builder( array $aggregation, $label, Jetpack_WPES_Query_Builder $builder ) { |
||
| 984 | |||
| 985 | /** |
||
| 986 | * Given an individual date_histogram aggregation, add it to the Jetpack_WPES_Query_Builder object for use in ES |
||
| 987 | * |
||
| 988 | * @module search |
||
| 989 | * |
||
| 990 | * @param array $aggregation The aggregation to add to the query builder |
||
| 991 | * @param string $label The 'label' (unique id) for this aggregation |
||
| 992 | * @param Jetpack_WPES_Query_Builder $builder The builder instance that is creating the ES query |
||
| 993 | */ |
||
| 994 | public function add_date_histogram_aggregation_to_es_query_builder( array $aggregation, $label, Jetpack_WPES_Query_Builder $builder ) { |
||
| 1010 | |||
| 1011 | /** |
||
| 1012 | * And an existing filter object with a list of additional filters. |
||
| 1013 | * |
||
| 1014 | * Attempts to optimize the filters somewhat. |
||
| 1015 | * |
||
| 1016 | * @module search |
||
| 1017 | * |
||
| 1018 | * @param array $curr_filter The existing filters to build upon |
||
| 1019 | * @param array $filters The new filters to add |
||
| 1020 | * |
||
| 1021 | * @return array The resulting merged filters |
||
| 1022 | */ |
||
| 1023 | public static function and_es_filters( array $curr_filter, array $filters ) { |
||
| 1038 | |||
| 1039 | /** |
||
| 1040 | * Add a recency score to a given Jetpack_WPES_Query_Builder object, for emphasizing newer posts in results |
||
| 1041 | * |
||
| 1042 | * Internally uses a gauss decay function |
||
| 1043 | * |
||
| 1044 | * @module search |
||
| 1045 | * |
||
| 1046 | * @param Jetpack_WPES_Query_Builder $builder The Jetpack_WPES_Query_Builder to add the recency score to |
||
| 1047 | * |
||
| 1048 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html#function-decay |
||
| 1049 | */ |
||
| 1050 | public static function score_query_by_recency( Jetpack_WPES_Query_Builder &$builder ) { |
||
| 1064 | |||
| 1065 | /** |
||
| 1066 | * Set the available filters for the search |
||
| 1067 | * |
||
| 1068 | * These get rendered via the Jetpack_Search_Widget_Filters() widget |
||
| 1069 | * |
||
| 1070 | * Behind the scenes, these are implemented using Elasticsearch Aggregations. |
||
| 1071 | * |
||
| 1072 | * If you do not require counts of how many documents match each filter, please consider using regular WP Query |
||
| 1073 | * arguments instead, such as via the jetpack_search_es_wp_query_args filter |
||
| 1074 | * |
||
| 1075 | * @module search |
||
| 1076 | * |
||
| 1077 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations.html |
||
| 1078 | * |
||
| 1079 | * @param array $aggregations Array of filters (aggregations) to apply to the search |
||
| 1080 | */ |
||
| 1081 | public function set_filters( array $aggregations ) { |
||
| 1089 | |||
| 1090 | /** |
||
| 1091 | * Set the search's facets (deprecated) |
||
| 1092 | * |
||
| 1093 | * @module search |
||
| 1094 | * |
||
| 1095 | * @deprecated 5.0 Please use Jetpack_Search::set_filters() instead |
||
| 1096 | * |
||
| 1097 | * @see Jetpack_Search::set_filters() |
||
| 1098 | * |
||
| 1099 | * @param array $facets Array of facets to apply to the search |
||
| 1100 | */ |
||
| 1101 | public function set_facets( array $facets ) { |
||
| 1106 | |||
| 1107 | /** |
||
| 1108 | * Get the raw Aggregation results from the ES response |
||
| 1109 | * |
||
| 1110 | * @module search |
||
| 1111 | * |
||
| 1112 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations.html |
||
| 1113 | * |
||
| 1114 | * @return array Array of Aggregations performed on the search |
||
| 1115 | */ |
||
| 1116 | public function get_search_aggregations_results() { |
||
| 1127 | |||
| 1128 | /** |
||
| 1129 | * Get the raw Facet results from the ES response |
||
| 1130 | * |
||
| 1131 | * @module search |
||
| 1132 | * |
||
| 1133 | * @deprecated 5.0 Please use Jetpack_Search::get_search_aggregations_results() instead |
||
| 1134 | * |
||
| 1135 | * @see Jetpack_Search::get_search_aggregations_results() |
||
| 1136 | * |
||
| 1137 | * @return array Array of Facets performed on the search |
||
| 1138 | */ |
||
| 1139 | public function get_search_facets() { |
||
| 1144 | |||
| 1145 | /** |
||
| 1146 | * Get the results of the Filters performed, including the number of matching documents |
||
| 1147 | * |
||
| 1148 | * Returns an array of Filters (keyed by $label, as passed to Jetpack_Search::set_filters()), containing the Filter and all resulting |
||
| 1149 | * matching buckets, the url for applying/removing each bucket, etc. |
||
| 1150 | * |
||
| 1151 | * NOTE - if this is called before the search is performed, an empty array will be returned. Use the $aggregations class |
||
| 1152 | * member if you need to access the raw filters set in Jetpack_Search::set_filters() |
||
| 1153 | * |
||
| 1154 | * @module search |
||
| 1155 | * |
||
| 1156 | * @param WP_Query $query The optional original WP_Query to use for determining which filters are active. Defaults to the main query |
||
| 1157 | * |
||
| 1158 | * @return array Array of Filters applied and info about them |
||
| 1159 | */ |
||
| 1160 | public function get_filters( WP_Query $query = null ) { |
||
| 1161 | if ( ! $query instanceof WP_Query ) { |
||
| 1162 | global $wp_query; |
||
| 1163 | |||
| 1164 | $query = $wp_query; |
||
| 1165 | } |
||
| 1166 | |||
| 1167 | $aggregation_data = $this->aggregations; |
||
| 1168 | |||
| 1169 | if ( empty( $aggregation_data ) ) { |
||
| 1170 | return $aggregation_data; |
||
| 1171 | } |
||
| 1172 | |||
| 1173 | $aggregation_results = $this->get_search_aggregations_results(); |
||
| 1174 | |||
| 1175 | if ( ! $aggregation_results ) { |
||
| 1176 | return $aggregation_data; |
||
| 1177 | } |
||
| 1178 | |||
| 1179 | // NOTE - Looping over the _results_, not the original configured aggregations, so we get the 'real' data from ES |
||
| 1180 | foreach ( $aggregation_results as $label => $aggregation ) { |
||
| 1181 | if ( empty( $aggregation ) ) { |
||
| 1182 | continue; |
||
| 1183 | } |
||
| 1184 | |||
| 1185 | $type = $this->aggregations[ $label ]['type']; |
||
| 1186 | |||
| 1187 | $aggregation_data[ $label ]['buckets'] = array(); |
||
| 1188 | |||
| 1189 | $existing_term_slugs = array(); |
||
| 1190 | |||
| 1191 | $tax_query_var = null; |
||
| 1192 | |||
| 1193 | // Figure out which terms are active in the query, for this taxonomy |
||
| 1194 | if ( 'taxonomy' === $this->aggregations[ $label ]['type'] ) { |
||
| 1195 | $tax_query_var = $this->get_taxonomy_query_var( $this->aggregations[ $label ]['taxonomy'] ); |
||
| 1196 | |||
| 1197 | if ( ! empty( $query->tax_query ) && ! empty( $query->tax_query->queries ) && is_array( $query->tax_query->queries ) ) { |
||
| 1198 | foreach( $query->tax_query->queries as $tax_query ) { |
||
| 1199 | if ( is_array( $tax_query ) && $this->aggregations[ $label ]['taxonomy'] === $tax_query['taxonomy'] && |
||
| 1200 | 'slug' === $tax_query['field'] && |
||
| 1201 | is_array( $tax_query['terms'] ) ) { |
||
| 1202 | $existing_term_slugs = array_merge( $existing_term_slugs, $tax_query['terms'] ); |
||
| 1203 | } |
||
| 1204 | } |
||
| 1205 | } |
||
| 1206 | } |
||
| 1207 | |||
| 1208 | // Now take the resulting found aggregation items and generate the additional info about them, such as |
||
| 1209 | // activation/deactivation url, name, count, etc |
||
| 1210 | $buckets = array(); |
||
| 1211 | |||
| 1212 | if ( ! empty( $aggregation['buckets'] ) ) { |
||
| 1213 | $buckets = (array) $aggregation['buckets']; |
||
| 1214 | } |
||
| 1215 | |||
| 1216 | if ( 'date_histogram' == $type ) { |
||
| 1217 | //re-order newest to oldest |
||
| 1218 | $buckets = array_reverse( $buckets ); |
||
| 1219 | } |
||
| 1220 | |||
| 1221 | // Some aggregation types like date_histogram don't support the max results parameter |
||
| 1222 | if ( is_int( $this->aggregations[ $label ]['count'] ) && count( $buckets ) > $this->aggregations[ $label ]['count'] ) { |
||
| 1223 | $buckets = array_slice( $buckets, 0, $this->aggregations[ $label ]['count'] ); |
||
| 1224 | } |
||
| 1225 | |||
| 1226 | foreach ( $buckets as $item ) { |
||
| 1227 | $query_vars = array(); |
||
| 1228 | $active = false; |
||
| 1229 | $remove_url = null; |
||
| 1230 | $name = ''; |
||
| 1231 | |||
| 1232 | // What type was the original aggregation? |
||
| 1233 | switch ( $type ) { |
||
| 1234 | case 'taxonomy': |
||
| 1235 | $taxonomy = $this->aggregations[ $label ]['taxonomy']; |
||
| 1236 | |||
| 1237 | $term = get_term_by( 'slug', $item['key'], $taxonomy ); |
||
| 1238 | |||
| 1239 | if ( ! $term || ! $tax_query_var ) { |
||
| 1240 | continue 2; // switch() is considered a looping structure |
||
| 1241 | } |
||
| 1242 | |||
| 1243 | $query_vars = array( |
||
| 1244 | $tax_query_var => implode( '+', array_merge( $existing_term_slugs, array( $term->slug ) ) ), |
||
| 1245 | ); |
||
| 1246 | |||
| 1247 | $name = $term->name; |
||
| 1248 | |||
| 1249 | // Let's determine if this term is active or not |
||
| 1250 | |||
| 1251 | if ( in_array( $item['key'], $existing_term_slugs, true ) ) { |
||
| 1252 | $active = true; |
||
| 1253 | |||
| 1254 | $slug_count = count( $existing_term_slugs ); |
||
| 1255 | |||
| 1256 | if ( $slug_count > 1 ) { |
||
| 1257 | $remove_url = Jetpack_Search_Helpers::add_query_arg( |
||
| 1258 | $tax_query_var, |
||
| 1259 | urlencode( implode( '+', array_diff( $existing_term_slugs, array( $item['key'] ) ) ) ) |
||
| 1260 | ); |
||
| 1261 | } else { |
||
| 1262 | $remove_url = Jetpack_Search_Helpers::remove_query_arg( $tax_query_var ); |
||
| 1263 | } |
||
| 1264 | } |
||
| 1265 | |||
| 1266 | break; |
||
| 1267 | |||
| 1268 | case 'post_type': |
||
| 1269 | $post_type = get_post_type_object( $item['key'] ); |
||
| 1270 | |||
| 1271 | if ( ! $post_type || $post_type->exclude_from_search ) { |
||
| 1272 | continue 2; // switch() is considered a looping structure |
||
| 1273 | } |
||
| 1274 | |||
| 1275 | $query_vars = array( |
||
| 1276 | 'post_type' => $item['key'], |
||
| 1277 | ); |
||
| 1278 | |||
| 1279 | $name = $post_type->labels->singular_name; |
||
| 1280 | |||
| 1281 | // Is this post type active on this search? |
||
| 1282 | $post_types = $query->get( 'post_type' ); |
||
| 1283 | |||
| 1284 | if ( ! is_array( $post_types ) ) { |
||
| 1285 | $post_types = array( $post_types ); |
||
| 1286 | } |
||
| 1287 | |||
| 1288 | if ( in_array( $item['key'], $post_types ) ) { |
||
| 1289 | $active = true; |
||
| 1290 | |||
| 1291 | $post_type_count = count( $post_types ); |
||
| 1292 | |||
| 1293 | // For the right 'remove filter' url, we need to remove the post type from the array, or remove the param entirely if it's the only one |
||
| 1294 | if ( $post_type_count > 1 ) { |
||
| 1295 | $remove_url = Jetpack_Search_Helpers::add_query_arg( |
||
| 1296 | 'post_type', |
||
| 1297 | implode( ',', array_diff( $post_types, array( $item['key'] ) ) ) |
||
| 1298 | ); |
||
| 1299 | } else { |
||
| 1300 | $remove_url = Jetpack_Search_Helpers::remove_query_arg( 'post_type' ); |
||
| 1301 | } |
||
| 1302 | } |
||
| 1303 | |||
| 1304 | break; |
||
| 1305 | |||
| 1306 | case 'date_histogram': |
||
| 1307 | $timestamp = $item['key'] / 1000; |
||
| 1308 | |||
| 1309 | $current_year = $query->get( 'year' ); |
||
| 1310 | $current_month = $query->get( 'monthnum' ); |
||
| 1311 | $current_day = $query->get( 'day' ); |
||
| 1312 | |||
| 1313 | switch ( $this->aggregations[ $label ]['interval'] ) { |
||
| 1314 | case 'year': |
||
| 1315 | $year = (int) date( 'Y', $timestamp ); |
||
| 1316 | |||
| 1317 | $query_vars = array( |
||
| 1318 | 'year' => $year, |
||
| 1319 | 'monthnum' => false, |
||
| 1320 | 'day' => false, |
||
| 1321 | ); |
||
| 1322 | |||
| 1323 | $name = $year; |
||
| 1324 | |||
| 1325 | // Is this year currently selected? |
||
| 1326 | if ( ! empty( $current_year ) && (int) $current_year === $year ) { |
||
| 1327 | $active = true; |
||
| 1328 | |||
| 1329 | $remove_url = Jetpack_Search_Helpers::remove_query_arg( array( 'year', 'monthnum', 'day' ) ); |
||
| 1330 | } |
||
| 1331 | |||
| 1332 | break; |
||
| 1333 | |||
| 1334 | case 'month': |
||
| 1335 | $year = (int) date( 'Y', $timestamp ); |
||
| 1336 | $month = (int) date( 'n', $timestamp ); |
||
| 1337 | |||
| 1338 | $query_vars = array( |
||
| 1339 | 'year' => $year, |
||
| 1340 | 'monthnum' => $month, |
||
| 1341 | 'day' => false, |
||
| 1342 | ); |
||
| 1343 | |||
| 1344 | $name = date( 'F Y', $timestamp ); |
||
| 1345 | |||
| 1346 | // Is this month currently selected? |
||
| 1347 | if ( ! empty( $current_year ) && (int) $current_year === $year && |
||
| 1348 | ! empty( $current_month ) && (int) $current_month === $month ) { |
||
| 1349 | $active = true; |
||
| 1350 | |||
| 1351 | $remove_url = Jetpack_Search_Helpers::remove_query_arg( array( 'year', 'monthnum' ) ); |
||
| 1352 | } |
||
| 1353 | |||
| 1354 | break; |
||
| 1355 | |||
| 1356 | case 'day': |
||
| 1357 | $year = (int) date( 'Y', $timestamp ); |
||
| 1358 | $month = (int) date( 'n', $timestamp ); |
||
| 1359 | $day = (int) date( 'j', $timestamp ); |
||
| 1360 | |||
| 1361 | $query_vars = array( |
||
| 1362 | 'year' => $year, |
||
| 1363 | 'monthnum' => $month, |
||
| 1364 | 'day' => $day, |
||
| 1365 | ); |
||
| 1366 | |||
| 1367 | $name = date( 'F jS, Y', $timestamp ); |
||
| 1368 | |||
| 1369 | // Is this day currently selected? |
||
| 1370 | if ( ! empty( $current_year ) && (int) $current_year === $year && |
||
| 1371 | ! empty( $current_month ) && (int) $current_month === $month && |
||
| 1372 | ! empty( $current_day ) && (int) $current_day === $day ) { |
||
| 1373 | $active = true; |
||
| 1374 | |||
| 1375 | $remove_url = Jetpack_Search_Helpers::remove_query_arg( array( 'day' ) ); |
||
| 1376 | } |
||
| 1377 | |||
| 1378 | break; |
||
| 1379 | |||
| 1380 | default: |
||
| 1381 | continue 3; // switch() is considered a looping structure |
||
| 1382 | } // End switch(). |
||
| 1383 | |||
| 1384 | break; |
||
| 1385 | |||
| 1386 | default: |
||
| 1387 | //continue 2; // switch() is considered a looping structure |
||
| 1388 | } // End switch(). |
||
| 1389 | |||
| 1390 | // Need to urlencode param values since add_query_arg doesn't |
||
| 1391 | $url_params = urlencode_deep( $query_vars ); |
||
| 1392 | |||
| 1393 | $aggregation_data[ $label ]['buckets'][] = array( |
||
| 1394 | 'url' => Jetpack_Search_Helpers::add_query_arg( $url_params ), |
||
| 1395 | 'query_vars' => $query_vars, |
||
| 1396 | 'name' => $name, |
||
| 1397 | 'count' => $item['doc_count'], |
||
| 1398 | 'active' => $active, |
||
| 1399 | 'remove_url' => $remove_url, |
||
| 1400 | 'type' => $type, |
||
| 1401 | 'type_label' => $aggregation_data[ $label ]['name'], |
||
| 1402 | 'widget_id' => ! empty( $aggregation_data[ $label ]['widget_id'] ) ? $aggregation_data[ $label ]['widget_id'] : 0 |
||
| 1403 | ); |
||
| 1404 | } // End foreach(). |
||
| 1405 | } // End foreach(). |
||
| 1406 | |||
| 1407 | return $aggregation_data; |
||
| 1408 | } |
||
| 1409 | |||
| 1410 | /** |
||
| 1411 | * Get the results of the Facets performed |
||
| 1412 | * |
||
| 1413 | * @module search |
||
| 1414 | * |
||
| 1415 | * @deprecated 5.0 Please use Jetpack_Search::get_filters() instead |
||
| 1416 | * |
||
| 1417 | * @see Jetpack_Search::get_filters() |
||
| 1418 | * |
||
| 1419 | * @return array $facets Array of Facets applied and info about them |
||
| 1420 | */ |
||
| 1421 | public function get_search_facet_data() { |
||
| 1426 | |||
| 1427 | /** |
||
| 1428 | * Get the Filters that are currently applied to this search |
||
| 1429 | * |
||
| 1430 | * @module search |
||
| 1431 | * |
||
| 1432 | * @return array Array if Filters that were applied |
||
| 1433 | */ |
||
| 1434 | public function get_active_filter_buckets() { |
||
| 1455 | |||
| 1456 | /** |
||
| 1457 | * Get the Filters that are currently applied to this search |
||
| 1458 | * |
||
| 1459 | * @module search |
||
| 1460 | * |
||
| 1461 | * @return array Array if Filters that were applied |
||
| 1462 | */ |
||
| 1463 | public function get_current_filters() { |
||
| 1468 | |||
| 1469 | /** |
||
| 1470 | * Calculate the right query var to use for a given taxonomy |
||
| 1471 | * |
||
| 1472 | * Allows custom code to modify the GET var that is used to represent a given taxonomy, via the jetpack_search_taxonomy_query_var filter |
||
| 1473 | * |
||
| 1474 | * @module search |
||
| 1475 | * |
||
| 1476 | * @param string $taxonomy_name The name of the taxonomy for which to get the query var |
||
| 1477 | * |
||
| 1478 | * @return bool|string The query var to use for this taxonomy, or false if none found |
||
| 1479 | */ |
||
| 1480 | public function get_taxonomy_query_var( $taxonomy_name ) { |
||
| 1499 | |||
| 1500 | /** |
||
| 1501 | * Takes an array of aggregation results, and ensures the array key ordering matches the key order in $desired |
||
| 1502 | * which is the input order |
||
| 1503 | * |
||
| 1504 | * Necessary because ES does not always return Aggs in the same order that you pass them in, and it should be possible |
||
| 1505 | * to control the display order easily |
||
| 1506 | * |
||
| 1507 | * @module search |
||
| 1508 | * |
||
| 1509 | * @param array $aggregations Agg results to be reordered |
||
| 1510 | * @param array $desired Array with keys representing the desired ordering |
||
| 1511 | * |
||
| 1512 | * @return array A new array with reordered keys, matching those in $desired |
||
| 1513 | */ |
||
| 1514 | public function fix_aggregation_ordering( array $aggregations, array $desired ) { |
||
| 1529 | } |
||
| 1530 |
This check looks for improperly formatted assignments.
Every assignment must have exactly one space before and one space after the equals operator.
To illustrate:
will have no issues, while
will report issues in lines 1 and 2.