Ticket #2847: 2847.3.diff
File 2847.3.diff, 7.3 KB (added by , 6 years ago) |
---|
-
functions.php
From ccecb903f3542f7d5d2350cc953b9c3f0166980b Mon Sep 17 00:00:00 2001 From: Paul Biron <paul@sparrowhawkcomputing.com> Date: Sun, 22 Jul 2018 09:40:04 -0600 Subject: [PATCH] support filtering @since archive by change type --- functions.php | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++ inc/template-tags.php | 62 +++++++++++++++++++++++++++++++++++++--- stylesheets/main.css | 4 +++ 3 files changed, 141 insertions(+), 4 deletions(-) diff --git a/functions.php b/functions.php index 71f0a16..ea62765 100644
a b if ( ! isset( $content_width ) ) { 108 108 add_action( 'init', __NAMESPACE__ . '\\init' ); 109 109 add_action( 'widgets_init', __NAMESPACE__ . '\\widgets_init' ); 110 110 111 /** 112 * @since 1.x.y Setup to filter wp-parser-since archives by change_type 113 */ 111 114 function init() { 112 115 113 116 register_nav_menus(); … … function init() { 132 135 add_filter( 'document_title_separator', __NAMESPACE__ . '\\theme_title_separator', 10, 2 ); 133 136 134 137 add_filter( 'syntaxhighlighter_htmlresult', __NAMESPACE__ . '\\syntaxhighlighter_htmlresult' ); 138 139 add_filter( 'query_vars', __NAMESPACE__ . '\\add_change_type_query_var' ); 140 add_filter( 'pre_get_posts', __NAMESPACE__ . '\\since_change_type_filter', 10 ); 135 141 } 136 142 137 143 /** … … function widgets_init() { 235 241 } 236 242 237 243 /** 244 * @since 1.x.y initialize change_type query_var 245 * 238 246 * @param \WP_Query $query 239 247 */ 240 248 function pre_get_posts( $query ) { … … function pre_get_posts( $query ) { 248 256 $query->set( 'wp-parser-source-file', str_replace( array( '.php', '/' ), array( '-php', '_' ), $query->query['wp-parser-source-file'] ) ); 249 257 } 250 258 259 if ( $query->is_main_query() && $query->is_tax() && $query->get( 'wp-parser-since' ) ) { 260 $query->set( 'orderby', 'type title' ); 261 $query->set( 'order', 'ASC' ); 262 $query->set( 'change_type', ! empty( $_REQUEST['change_type'] ) ? $_REQUEST['change_type'] : 'introduced' ); 263 } 264 251 265 // For search query modifications see DevHub_Search. 252 266 } 253 267 268 /** 269 * Add change_type to the public_query_vars. 270 * 271 * @since 1.x.y 272 * 273 * @param array $public_query_vars The array of whitelisted query variables. 274 * @return array $public_query_vars with 'change_type' added. 275 */ 276 function add_change_type_query_var( $public_query_vars ) { 277 $public_query_vars[] = 'change_type'; 278 279 return $public_query_vars; 280 } 281 282 /** 283 * Filter wp-parser-since archive by the type of change. 284 * 285 * @since 1.x.y 286 * 287 * @param WP_Query $query WP_Query object. 288 * @return void 289 */ 290 function since_change_type_filter( $query ) { 291 global $wpdb; 292 293 $version = $query->get( 'wp-parser-since' ); 294 $change_type = $query->get( 'change_type' ); 295 296 if ( ! ( $query->is_archive() && $query->is_tax() && ! empty( $version ) ) || 297 ( empty( $change_type ) || 'any' === $change_type ) ) { 298 // bail, no need to filter 299 return; 300 } 301 302 $version_term = get_term_by( 'name', $version, 'wp-parser-since' ); 303 $changes = get_term_meta( $version_term->term_id, '_wp_parser_changes', true ); 304 if ( empty( $changes ) ) { 305 // no changes, so force a "nothing found" 306 // @see @\todo in \DevHub\taxonomy_archive_filter() 307 $query->set( 'post__in', array( -1 ) ); 308 309 return; 310 } 311 312 $post_types = $query->get( 'post_type' ); 313 if ( empty( $post_types ) ) { 314 $post_types = get_parsed_post_types(); 315 } 316 317 $post__in = array(); 318 foreach ( (array) $post_types as $post_type ) { 319 if ( ! empty( $changes[ $change_type ][ $post_type ] ) ) { 320 $post__in = array_merge( $post__in, $changes[ $change_type ][ $post_type ] ); 321 } 322 } 323 324 if ( empty( $post__in ) ) { 325 // no changes, so force a "nothing found" 326 // @see @\todo in \DevHub\taxonomy_archive_filter() 327 $post__in = array( -1 ); 328 } 329 330 $query->set( 'post__in', $post__in ); 331 } 332 254 333 function register_nav_menus() { 255 334 256 335 \register_nav_menus( array( -
inc/template-tags.php
diff --git a/inc/template-tags.php b/inc/template-tags.php index 225dd81..1381469 100644
a b namespace DevHub { 559 559 } 560 560 561 561 /** 562 * Get an array of all change types. 563 * 564 * @since 1.x.y 565 * 566 * @param string $labels If set to 'labels' change types with their labels are returned. 567 * @return array 568 */ 569 function get_change_types( $labels = '' ) { 570 $change_types = array( 571 'introduced' => __( 'Introduced', 'wporg' ), 572 'modified' => __( 'Modified', 'wporg' ), 573 'deprecated' => __( 'Deprecated', 'wporg' ), 574 ); 575 576 if ( 'labels' !== $labels ) { 577 return array_keys( $post_types ); 578 } 579 580 return $change_types; 581 } 582 583 /** 562 584 * Checks if given post type is one of the parsed post types. 563 585 * 564 586 * @param null|string Optional. The post type. Default null. … … namespace DevHub { 1691 1713 $form .= "<input type='hidden' name='" . esc_attr( $taxonomy ) . "' value='" . esc_attr( $term ) . "'>"; 1692 1714 } 1693 1715 1694 $form .= "<label for='archive-filter'>"; 1695 $form .= __( 'Filter by type:', 'wporg' ) . ' '; 1696 $form .= '<select name="post_type[]" id="archive-filter">'; 1716 $form .= "<label for='post-type-archive-filter'>"; 1717 // @todo the "Filter by post type" wording is not ideal because most users 1718 // won't know that classes/methods/functions/hooks are represented as 1719 // as different CPTs, but with the addition of the "Filter by type of change" 1720 // dropdown I felt that the original "Filter by type" wording wasn't sufficient. 1721 // Very much open to suggestions for alternative wording! 1722 $form .= __( 'Filter by post type:', 'wporg' ) . ' '; 1723 $form .= '<select name="post_type[]" id="post-type-archive-filter">'; 1697 1724 $form .= $options . '</select></label>'; 1698 $form .= "<input class='shiny-blue' type='submit' value='Filter' /></form>";1699 1725 1726 if ( 'wp-parser-since' === $taxonomy ) { 1727 // @todo it might make for a better UX to detect whether there are 1728 // any changes of a given type and not output an option for that 1729 // change type, rather than having the user get the "Nothing Found" message 1730 // in those cases. 1731 // 1732 // This also applies to the post type filter, e.g., in cases like 1733 // https://developer.wordpress.org/reference/since/1.0.0/?post_type[]=wp-parser-class 1734 // 1735 // But I'll leave this for a future iteration. 1736 $change_types = get_change_types( 'labels' ); 1737 $change_types = array( 'any' => __( 'Any change', 'wporg' ) ) + $change_types; 1738 1739 $options = ''; 1740 $current_change_type = get_query_var( 'change_type' ); 1741 foreach ( $change_types as $change_type => $label ) { 1742 $selected = selected( $change_type, $current_change_type, false ); 1743 $options .= "<option value='$change_type'$selected>$label</option>"; 1744 } 1745 1746 $form .= "<label for='change-type-archive-filter'>"; 1747 $form .= __( 'Filter by type of change:', 'wporg' ) . ' '; 1748 $form .= '<select name="change_type" id="change-type-archive-filter">'; 1749 $form .= $options . '</select></label>'; 1750 } 1751 1752 $form .= "<input class='shiny-blue' type='submit' value='Filter' /></form>"; 1753 1700 1754 echo $form; 1701 1755 } 1702 1756 -
stylesheets/main.css
diff --git a/stylesheets/main.css b/stylesheets/main.css index 8ab9053..9a41739 100644
a b input { 1075 1075 font-size: 1.5rem; 1076 1076 } 1077 1077 1078 .devhub-wrap .archive-filter-form label[for="change-type-archive-filter"] { 1079 margin-left: 0.5em; 1080 } 1081 1078 1082 .devhub-wrap .archive-filter-form input[type="submit"] { 1079 1083 margin-left: .5em; 1080 1084 padding: 0.2em 0.5em;