Ticket #48224: general-template.patch
File general-template.patch, 18.4 KB (added by , 16 months ago) |
---|
-
general-template.php
1022 1022 * 1023 1023 * @global int $page Page number of a single post. 1024 1024 * @global int $paged Page number of a list of posts. 1025 * 1025 * 1026 * @param string $sep 1026 1027 * @return string Tag with the document title. 1027 1028 */ 1028 function wp_get_document_title( ) {1029 function wp_get_document_title($sep = '-') { 1029 1030 1030 1031 /** 1031 1032 * Filters the document title before it is generated. … … 1116 1117 * 1117 1118 * @param string $sep Document title separator. Default '-'. 1118 1119 */ 1119 $sep = apply_filters( 'document_title_separator', '-');1120 $sep = apply_filters( 'document_title_separator', $sep ); 1120 1121 1121 1122 /** 1122 1123 * Filters the parts of the document title. … … 3970 3971 } 3971 3972 3972 3973 /** 3973 * Retrieve paginated link for archive post pages.3974 * Get default values based on the current URL 3974 3975 * 3975 * Technically, the function can be used to create paginated link list for any 3976 * area. The 'base' argument is used to reference the url, which will be used to 3977 * create the paginated links. The 'format' argument is then used for replacing 3978 * the page number. It is however, most likely and by default, to be used on the 3979 * archive post pages. 3976 * @param bool $get_all Return a full modified array of $url_parts 3977 * @return string|array 3978 */ 3979 function default_paginate_base( bool $get_all = false ) { 3980 // Setting up default values based on the current URL. 3981 $base = html_entity_decode( get_pagenum_link() ); 3982 $url_parts = explode( '?', $base ); 3983 $url_parts[0] = trailingslashit( $url_parts[0] ) . '%_%'; 3984 3985 // Append the format placeholder to the base URL. 3986 return $get_all ? $url_parts : $url_parts[0]; 3987 } 3988 3989 /** 3990 * Get URL base depends on permalink settings 3980 3991 * 3981 * The 'type' argument controls format of the returned value. The default is 3982 * 'plain', which is just a string with the links separated by a newline 3983 * character. The other possible values are either 'array' or 'list'. The 3984 * 'array' value will return an array of the paginated link list to offer full 3985 * control of display. The 'list' value will place all of the paginated links in 3986 * an unordered HTML list. 3992 * @return string 3993 */ 3994 function default_paginate_format():string { 3995 global $wp_rewrite; 3996 3997 $format = $wp_rewrite->using_index_permalinks() && ! strpos( $base, 'index.php' ) ? 'index.php/' : ''; 3998 $format .= $wp_rewrite->using_permalinks() ? user_trailingslashit( $wp_rewrite->pagination_base . '/%#%', 'paged' ) : '?paged=%#%'; 3999 4000 return $format; 4001 } 4002 4003 /** 4004 * Return a proper URL from given base, format, current page. 3987 4005 * 4006 * @param int $page 4007 * @param string $format 4008 * @param string $base 4009 * @param array $args 4010 * @param string $fragment 4011 * @return string 4012 */ 4013 function format_paginate_link( int $page, $base = null, $format = null, array $args = [], string $fragment = '' ):string { 4014 $base = $base ?: default_paginate_base(); 4015 $format = $format ?: default_paginate_format(); 4016 4017 $link = str_replace( '%_%', $format, $base); 4018 $link = str_replace( '%#%', $page, $link ); 4019 if ( $args ) { 4020 $link = add_query_arg( $args, $link ); 4021 } 4022 $link .= $fragment; 4023 4024 return esc_url( apply_filters( 'paginate_links', $link ) ); 4025 } 4026 4027 /** 4028 * Retrieve paginated links in array for archive post pages. 4029 * 4030 * Technically, the function can be used to create paginated links in array for 4031 * any area. The 'base' argument is used to reference the url, which will be 4032 * used to create the paginated links. The 'format' argument is then used for 4033 * replacing the page number. It is however, most likely and by default, to be 4034 * used on the archive post pages. 4035 * 3988 4036 * The 'total' argument is the total amount of pages and is an integer. The 3989 4037 * 'current' argument is the current page number and is also an integer. 3990 4038 * … … 4009 4057 * It is possible to add query vars to the link by using the 'add_args' argument 4010 4058 * and see add_query_arg() for more information. 4011 4059 * 4012 * The 'before_page_number' and 'after_page_number' arguments allow users to4013 * augment the links themselves. Typically this might be to add context to the4014 * numbered links so that screen reader users understand what the links are for.4015 * The text strings are added before and after the page number - within the4016 * anchor tag.4017 *4018 * @since 2.1.04019 * @since 4.9.0 Added the `aria_current` argument.4020 *4021 4060 * @global WP_Query $wp_query WordPress Query object. 4022 4061 * @global WP_Rewrite $wp_rewrite WordPress rewrite component. 4023 4062 * 4024 * @param string|array $args{4063 * @param array $args { 4025 4064 * Optional. Array or string of arguments for generating paginated links for archives. 4026 4065 * 4027 4066 * @type string $base Base of the paginated url. Default empty. … … 4029 4068 * @type int $total The total amount of pages. Default is the value WP_Query's 4030 4069 * `max_num_pages` or 1. 4031 4070 * @type int $current The current page number. Default is 'paged' query var or 1. 4032 * @type string $aria_current The value for the aria-current attribute. Possible values are 'page',4033 4071 * 'step', 'location', 'date', 'time', 'true', 'false'. Default is 'page'. 4034 4072 * @type bool $show_all Whether to show all pages. Default false. 4035 4073 * @type int $end_size How many numbers on either the start and the end list edges. … … 4036 4074 * Default 1. 4037 4075 * @type int $mid_size How many numbers to either side of the current pages. Default 2. 4038 4076 * @type bool $prev_next Whether to include the previous and next links in the list. Default true. 4039 * @type bool $prev_text The previous page text. Default '« Previous'.4040 * @type bool $next_text The next page text. Default 'Next »'.4041 * @type string $type Controls format of the returned value. Possible values are 'plain',4042 * 'array' and 'list'. Default is 'plain'.4043 4077 * @type array $add_args An array of query args to add. Default false. 4044 4078 * @type string $add_fragment A string to append to each link. Default empty. 4045 * @type string $before_page_number A string to appear before the page number. Default empty.4046 * @type string $after_page_number A string to append after the page number. Default empty.4047 4079 * } 4048 * @return string|array|void String of page links or array of page links.4080 * @return array|void Void value is return if total is less than 2. Otherwise, an informative array is returned. 4049 4081 */ 4050 function paginate_links( $args = '' ) { 4051 global $wp_query, $wp_rewrite; 4082 function paginate_array( array $args = [] ) { 4083 if ( empty( $args['base'] ) ) : 4084 $url_parts = default_paginate_base(true); 4085 $base = $url_parts[0]; 4086 else: 4087 $base = $args['base']; 4088 endif; 4052 4089 4053 // Setting up default values based on the current URL. 4054 $pagenum_link = html_entity_decode( get_pagenum_link() ); 4055 $url_parts = explode( '?', $pagenum_link ); 4090 if ( empty( $args['total'] ) ) : 4091 global $wp_query; 4092 $total = isset( $wp_query->max_num_pages ) ? $wp_query->max_num_pages : 1; 4093 else: 4094 $total = $args['total']; 4095 endif; 4056 4096 4057 // Get max pages and current page out of the current query, if available. 4058 $total = isset( $wp_query->max_num_pages ) ? $wp_query->max_num_pages : 1; 4059 $current = get_query_var( 'paged' ) ? intval( get_query_var( 'paged' ) ) : 1; 4097 if ( empty( $args['current'] ) ) : 4098 global $wp_query; 4099 $current = get_query_var( 'paged' ) ? intval( get_query_var( 'paged' ) ) : 1; 4100 else: 4101 $current = $args['current']; 4102 endif; 4060 4103 4061 // Append the format placeholder to the base URL. 4062 $pagenum_link = trailingslashit( $url_parts[0] ) . '%_%'; 4063 4064 // URL base depends on permalink settings. 4065 $format = $wp_rewrite->using_index_permalinks() && ! strpos( $pagenum_link, 'index.php' ) ? 'index.php/' : ''; 4066 $format .= $wp_rewrite->using_permalinks() ? user_trailingslashit( $wp_rewrite->pagination_base . '/%#%', 'paged' ) : '?paged=%#%'; 4067 4068 $defaults = array( 4069 'base' => $pagenum_link, // http://example.com/all_posts.php%_% : %_% is replaced by format (below) 4070 'format' => $format, // ?page=%#% : %#% is replaced by the page number 4104 $defaults = [ 4105 'base' => $base, 4106 'format' => empty( $args['format'] ) ? default_paginate_format() : $args['format'], 4071 4107 'total' => $total, 4072 4108 'current' => $current, 4073 'aria_current' => 'page',4074 4109 'show_all' => false, 4075 4110 'prev_next' => true, 4076 'prev_text' => __( '« Previous' ),4077 'next_text' => __( 'Next »' ),4078 4111 'end_size' => 1, 4079 4112 'mid_size' => 2, 4080 'type' => 'plain', 4081 'add_args' => array(), // array of query args to add 4082 'add_fragment' => '', 4083 'before_page_number' => '', 4084 'after_page_number' => '', 4085 ); 4113 'add_args' => [], 4114 'add_fragment' => '' 4115 ]; 4116 unset($args['base'], $args['format'], $args['total'], $args['current']); 4086 4117 4087 4118 $args = wp_parse_args( $args, $defaults ); 4088 4119 4089 4120 if ( ! is_array( $args['add_args'] ) ) { 4090 $args['add_args'] = array();4121 $args['add_args'] = []; 4091 4122 } 4092 4123 4093 4124 // Merge additional query vars found in the original URL into 'add_args' array. … … 4122 4153 if ( $mid_size < 0 ) { 4123 4154 $mid_size = 2; 4124 4155 } 4125 4126 $add_args = $args['add_args'];4127 $r = '';4128 $page_links = array();4129 4156 $dots = false; 4130 4157 4158 $page_array = [ 4159 'prev' => null, 4160 'pages' => [], 4161 'next' => null 4162 ]; 4163 4131 4164 if ( $args['prev_next'] && $current && 1 < $current ) : 4132 $link = str_replace( '%_%', 2 == $current ? '' : $args['format'], $args['base'] ); 4133 $link = str_replace( '%#%', $current - 1, $link ); 4134 if ( $add_args ) { 4135 $link = add_query_arg( $add_args, $link ); 4136 } 4137 $link .= $args['add_fragment']; 4165 $link = format_paginate_link( 4166 $current - 1, 4167 $args['base'], 4168 2 == $current ? '' : $args['format'], 4169 $args['add_args'], 4170 $args['add_fragment'] 4171 ); 4138 4172 4139 $page_links[] = sprintf( 4140 '<a class="prev page-numbers" href="%s">%s</a>', 4141 /** 4142 * Filters the paginated links for the given archive pages. 4143 * 4144 * @since 3.0.0 4145 * 4146 * @param string $link The paginated link URL. 4147 */ 4148 esc_url( apply_filters( 'paginate_links', $link ) ), 4149 $args['prev_text'] 4150 ); 4173 $page_array['prev'] = [ 4174 'url' => $link, 4175 'class' => 'prev page-numbers' 4176 ]; 4151 4177 endif; 4152 4153 4178 for ( $n = 1; $n <= $total; $n++ ) : 4154 if ( $n == $current ) : 4155 $page_links[] = sprintf( 4156 '<span aria-current="%s" class="page-numbers current">%s</span>', 4157 esc_attr( $args['aria_current'] ), 4158 $args['before_page_number'] . number_format_i18n( $n ) . $args['after_page_number'] 4159 ); 4179 $current_stage = $current && $n >= $current - $mid_size && $n <= $current + $mid_size; 4180 $cond = $args['show_all'] || 4181 ( 4182 $n <= $end_size || 4183 $current_stage || 4184 $n > $total - $end_size 4185 ); 4186 if ( $cond ): 4187 $link = null; 4188 if ($n !== $current): 4189 $link = format_paginate_link( 4190 $n, 4191 $args['base'], 4192 1 == $n ? '' : $args['format'], 4193 $args['add_args'], 4194 $args['add_fragment'] 4195 ); 4196 endif; 4160 4197 4198 $page_array['pages'][] = [ 4199 'type' => 'page', 4200 'url' => $link ?: null, 4201 'label' => $n, 4202 'class' => 'page-numbers' 4203 ]; 4161 4204 $dots = true; 4162 else : 4163 if ( $args['show_all'] || ( $n <= $end_size || ( $current && $n >= $current - $mid_size && $n <= $current + $mid_size ) || $n > $total - $end_size ) ) : 4164 $link = str_replace( '%_%', 1 == $n ? '' : $args['format'], $args['base'] ); 4165 $link = str_replace( '%#%', $n, $link ); 4166 if ( $add_args ) { 4167 $link = add_query_arg( $add_args, $link ); 4168 } 4169 $link .= $args['add_fragment']; 4205 elseif ( $dots && !$args['show_all'] ) : 4206 $page_array['pages'][] = [ 4207 'type' => 'etc', 4208 'label' => __( '…' ), 4209 'class' => 'page-numbers dots' 4210 ]; 4211 $dots = false; 4212 endif; 4213 endfor; 4214 if ( $args['prev_next'] && $current && $current < $total ) : 4215 $link = format_paginate_link( 4216 $current + 1, 4217 $args['base'], 4218 $args['format'], 4219 $args['add_args'], 4220 $args['add_fragment'] 4221 ); 4170 4222 4171 $page_links[] = sprintf( 4172 '<a class="page-numbers" href="%s">%s</a>', 4173 /** This filter is documented in wp-includes/general-template.php */ 4174 esc_url( apply_filters( 'paginate_links', $link ) ), 4175 $args['before_page_number'] . number_format_i18n( $n ) . $args['after_page_number'] 4176 ); 4223 $page_array['next'] = [ 4224 'url' => $link, 4225 'class' => 'next page-numbers' 4226 ]; 4227 endif; 4177 4228 4178 $dots = true; 4179 elseif ( $dots && ! $args['show_all'] ) : 4180 $page_links[] = '<span class="page-numbers dots">' . __( '…' ) . '</span>'; 4229 return $page_array; 4230 } 4181 4231 4182 $dots = false; 4183 endif; 4184 endif; 4185 endfor; 4232 /** 4233 * Retrieve paginated link for archive post pages. 4234 * 4235 * Technically, the function can be used to create paginated link list for any 4236 * area. The 'base' argument is used to reference the url, which will be used to 4237 * create the paginated links. The 'format' argument is then used for replacing 4238 * the page number. It is however, most likely and by default, to be used on the 4239 * archive post pages. 4240 * 4241 * The 'type' argument controls format of the returned value. The default is 4242 * 'plain', which is just a string with the links separated by a newline 4243 * character. The other possible values are either 'array' or 'list'. The 4244 * 'array' value will return an array of the paginated link list to offer full 4245 * control of display. The 'list' value will place all of the paginated links in 4246 * an unordered HTML list. 4247 * 4248 * The 'before_page_number' and 'after_page_number' arguments allow users to 4249 * augment the links themselves. Typically this might be to add context to the 4250 * numbered links so that screen reader users understand what the links are for. 4251 * The text strings are added before and after the page number - within the 4252 * anchor tag. 4253 * 4254 * @since 2.1.0 4255 * @since 4.9.0 Added the `aria_current` argument. 4256 * 4257 * @param string|array $args { 4258 * Optional. Array or string of arguments for generating paginated links for archives. 4259 * 4260 * @type string $base Base of the paginated url. Default empty. 4261 * @type string $format Format for the pagination structure. Default empty. 4262 * @type int $total The total amount of pages. Default is the value WP_Query's 4263 * `max_num_pages` or 1. 4264 * @type int $current The current page number. Default is 'paged' query var or 1. 4265 * @type string $aria_current The value for the aria-current attribute. Possible values are 'page', 4266 * 'step', 'location', 'date', 'time', 'true', 'false'. Default is 'page'. 4267 * @type bool $show_all Whether to show all pages. Default false. 4268 * @type int $end_size How many numbers on either the start and the end list edges. 4269 * Default 1. 4270 * @type int $mid_size How many numbers to either side of the current pages. Default 2. 4271 * @type bool $prev_next Whether to include the previous and next links in the list. Default true. 4272 * @type bool $prev_text The previous page text. Default '« Previous'. 4273 * @type bool $next_text The next page text. Default 'Next »'. 4274 * @type string $type Controls format of the returned value. Possible values are 'plain', 4275 * 'array' and 'list'. Default is 'plain'. 4276 * @type array $add_args An array of query args to add. Default false. 4277 * @type string $add_fragment A string to append to each link. Default empty. 4278 * @type string $before_page_number A string to appear before the page number. Default empty. 4279 * @type string $after_page_number A string to append after the page number. Default empty. 4280 * } 4281 * @return string|array|void String of page links or array of page links. 4282 */ 4283 function paginate_links( $args = '' ) { 4284 $defaults = [ 4285 'aria_current' => 'page', 4286 'prev_text' => __( '« Previous' ), 4287 'next_text' => __( 'Next »' ), 4288 'type' => 'plain', 4289 'before_page_number' => '', 4290 'after_page_number' => '' 4291 ]; 4186 4292 4187 if ( $args['prev_next'] && $current && $current < $total ) : 4188 $link = str_replace( '%_%', $args['format'], $args['base'] ); 4189 $link = str_replace( '%#%', $current + 1, $link ); 4190 if ( $add_args ) { 4191 $link = add_query_arg( $add_args, $link ); 4192 } 4193 $link .= $args['add_fragment']; 4293 $paginate_array = paginate_array( wp_parse_args( $args, $defaults ) ); 4294 $page_links = []; 4194 4295 4195 $page_links[] = sprintf( 4196 '<a class="next page-numbers" href="%s">%s</a>', 4197 /** This filter is documented in wp-includes/general-template.php */ 4198 esc_url( apply_filters( 'paginate_links', $link ) ), 4199 $args['next_text'] 4200 ); 4296 4297 if ( isset($paginate_array['prev']) ) : 4298 $prev = $paginate_array['prev']; 4299 $page_links[] = "<a class='{$prev['class']}' href='{$prev['url']}'>{$args['prev_text']}</a>"; 4201 4300 endif; 4202 4301 4302 foreach ( $paginate_array['pages'] as $item ) : 4303 if ( $item['type'] === 'etc' ) : 4304 $page_links[] = "<a class='{$item['class']}'>{$item['label']}</a>"; 4305 elseif ( !$item['url'] && $item['type'] === 'page' ) : 4306 $pos = $args['before_page_number'] . number_format_i18n( $item['label'] ) . $args['after_page_number']; 4307 $page_links[] = "<a class='{$item['class']} current' aria-current='{$args['aria_current']}'>{$pos}</a>"; 4308 else: 4309 $pos = $args['before_page_number'] . number_format_i18n( $item['label'] ) . $args['after_page_number']; 4310 $page_links[] = "<a class='{$item['class']}' href='{$item['url']}'>{$pos}</a>"; 4311 endif; 4312 endforeach; 4313 4314 if ( isset($paginate_array['next']) ) : 4315 $next = $paginate_array['next']; 4316 $page_links[] = "<a class='{$next['class']}' href='{$next['url']}'>{$args['next_text']}</a>"; 4317 endif; 4318 4319 4203 4320 switch ( $args['type'] ) { 4204 4321 case 'array': 4205 4322 return $page_links; 4206 4323 4207 4324 case 'list': 4208 $r .= "<ul class='page-numbers'>\n\t<li>"; 4209 $r .= join( "</li>\n\t<li>", $page_links ); 4210 $r .= "</li>\n</ul>\n"; 4211 break; 4325 return "<ul class='page-numbers'>\n\t<li>". 4326 join( "</li>\n\t<li>", $page_links ). 4327 "</li>\n</ul>\n"; 4212 4328 4213 4329 default: 4214 $r = join( "\n", $page_links ); 4215 break; 4330 return join( "\n", $page_links ); 4216 4331 } 4217 4218 return $r;4219 4332 } 4220 4333 4221 4334 /**