}
/**
- * Display or retrieve the current post title with optional content.
+ * Display or retrieve the current post title with optional markup.
*
* @since 0.71
*
- * @param string $before Optional. Content to prepend to the title.
- * @param string $after Optional. Content to append to the title.
- * @param bool $echo Optional, default to true.Whether to display or return.
- * @return string|void String if $echo parameter is false.
+ * @param string $before Optional. Markup to prepend to the title. Default empty.
+ * @param string $after Optional. Markup to append to the title. Default empty.
+ * @param bool $echo Optional. Whether to echo or return the title. Default true for echo.
+ * @return string|void Current post title if $echo is false.
*/
function the_title( $before = '', $after = '', $echo = true ) {
$title = get_the_title();
*
* @since 0.71
*
- * @global int $page
- * @global int $more
- * @global bool $preview
- * @global array $pages
- * @global int $multipage
+ * @global int $page Page number of a single post/page.
+ * @global int $more Boolean indicator for whether single post/page is being viewed.
+ * @global bool $preview Whether post/page is in preview mode.
+ * @global array $pages Array of all pages in post/page. Each array element contains part of the content separated by the <!--nextpage--> tag.
+ * @global int $multipage Boolean indicator for whether multiple pages are in play.
*
* @param string $more_link_text Optional. Content for when there is more text.
* @param bool $strip_teaser Optional. Strip teaser content before the more text. Default is false.
if ( is_404() )
$classes[] = 'error404';
- if ( is_single() ) {
+ if ( is_singular() ) {
$post_id = $wp_query->get_queried_object_id();
$post = $wp_query->get_queried_object();
+ $post_type = $post->post_type;
- $classes[] = 'single';
- if ( isset( $post->post_type ) ) {
- $classes[] = 'single-' . sanitize_html_class($post->post_type, $post_id);
- $classes[] = 'postid-' . $post_id;
+ if ( is_page_template() ) {
+ $classes[] = "{$post_type}-template";
- // Post Format
- if ( post_type_supports( $post->post_type, 'post-formats' ) ) {
- $post_format = get_post_format( $post->ID );
+ $template_slug = get_page_template_slug( $post_id );
+ $template_parts = explode( '/', $template_slug );
- if ( $post_format && !is_wp_error($post_format) )
- $classes[] = 'single-format-' . sanitize_html_class( $post_format );
- else
- $classes[] = 'single-format-standard';
+ foreach ( $template_parts as $part ) {
+ $classes[] = "{$post_type}-template-" . sanitize_html_class( str_replace( array( '.', '/' ), '-', basename( $part, '.php' ) ) );
+ }
+ $classes[] = "{$post_type}-template-" . sanitize_html_class( str_replace( '.', '-', $template_slug ) );
+ } else {
+ $classes[] = "{$post_type}-template-default";
+ }
+
+ if ( is_single() ) {
+ $classes[] = 'single';
+ if ( isset( $post->post_type ) ) {
+ $classes[] = 'single-' . sanitize_html_class( $post->post_type, $post_id );
+ $classes[] = 'postid-' . $post_id;
+
+ // Post Format
+ if ( post_type_supports( $post->post_type, 'post-formats' ) ) {
+ $post_format = get_post_format( $post->ID );
+
+ if ( $post_format && !is_wp_error($post_format) )
+ $classes[] = 'single-format-' . sanitize_html_class( $post_format );
+ else
+ $classes[] = 'single-format-standard';
+ }
}
}
$mime_prefix = array( 'application/', 'image/', 'text/', 'audio/', 'video/', 'music/' );
$classes[] = 'attachmentid-' . $post_id;
$classes[] = 'attachment-' . str_replace( $mime_prefix, '', $mime_type );
+ } elseif ( is_page() ) {
+ $classes[] = 'page';
+
+ $page_id = $wp_query->get_queried_object_id();
+
+ $post = get_post($page_id);
+
+ $classes[] = 'page-id-' . $page_id;
+
+ if ( get_pages( array( 'parent' => $page_id, 'number' => 1 ) ) ) {
+ $classes[] = 'page-parent';
+ }
+
+ if ( $post->post_parent ) {
+ $classes[] = 'page-child';
+ $classes[] = 'parent-pageid-' . $post->post_parent;
+ }
}
} elseif ( is_archive() ) {
if ( is_post_type_archive() ) {
$classes[] = 'term-' . $term->term_id;
}
}
- } elseif ( is_page() ) {
- $classes[] = 'page';
-
- $page_id = $wp_query->get_queried_object_id();
-
- $post = get_post($page_id);
-
- $classes[] = 'page-id-' . $page_id;
-
- if ( get_pages( array( 'parent' => $page_id, 'number' => 1 ) ) ) {
- $classes[] = 'page-parent';
- }
-
- if ( $post->post_parent ) {
- $classes[] = 'page-child';
- $classes[] = 'parent-pageid-' . $post->post_parent;
- }
- if ( is_page_template() ) {
- $classes[] = 'page-template';
-
- $template_slug = get_page_template_slug( $page_id );
- $template_parts = explode( '/', $template_slug );
-
- foreach ( $template_parts as $part ) {
- $classes[] = 'page-template-' . sanitize_html_class( str_replace( array( '.', '/' ), '-', basename( $part, '.php' ) ) );
- }
- $classes[] = 'page-template-' . sanitize_html_class( str_replace( '.', '-', $template_slug ) );
- } else {
- $classes[] = 'page-template-default';
- }
}
if ( is_user_logged_in() )
function post_password_required( $post = null ) {
$post = get_post($post);
- if ( empty( $post->post_password ) )
- return false;
+ if ( empty( $post->post_password ) ) {
+ /** This filter is documented in wp-includes/post.php */
+ return apply_filters( 'post_password_required', false, $post );
+ }
- if ( ! isset( $_COOKIE['wp-postpass_' . COOKIEHASH] ) )
- return true;
+ if ( ! isset( $_COOKIE[ 'wp-postpass_' . COOKIEHASH ] ) ) {
+ /** This filter is documented in wp-includes/post.php */
+ return apply_filters( 'post_password_required', true, $post );
+ }
- require_once ABSPATH . WPINC . '/class-phpass.php';
$hasher = new PasswordHash( 8, true );
$hash = wp_unslash( $_COOKIE[ 'wp-postpass_' . COOKIEHASH ] );
- if ( 0 !== strpos( $hash, '$P$B' ) )
- return true;
+ if ( 0 !== strpos( $hash, '$P$B' ) ) {
+ $required = true;
+ } else {
+ $required = ! $hasher->CheckPassword( $post->post_password, $hash );
+ }
- return ! $hasher->CheckPassword( $post->post_password, $hash );
+ /**
+ * Filters whether a post requires the user to supply a password.
+ *
+ * @since 4.7.0
+ *
+ * @param bool $required Whether the user needs to supply a password. True if password has not been
+ * provided or is incorrect, false if password has been supplied or is not required.
+ * @param WP_Post $post Post data.
+ */
+ return apply_filters( 'post_password_required', $required, $post );
}
//
* Retrieve or display list of pages in list (li) format.
*
* @since 1.5.0
+ * @since 4.7.0 Added the `item_spacing` argument.
*
* @see get_pages()
*
* @param array|string $args {
* Array or string of arguments. Optional.
*
- * @type int $child_of Display only the sub-pages of a single page by ID. Default 0 (all pages).
- * @type string $authors Comma-separated list of author IDs. Default empty (all authors).
- * @type string $date_format PHP date format to use for the listed pages. Relies on the 'show_date' parameter.
- * Default is the value of 'date_format' option.
- * @type int $depth Number of levels in the hierarchy of pages to include in the generated list.
- * Accepts -1 (any depth), 0 (all pages), 1 (top-level pages only), and n (pages to
- * the given n depth). Default 0.
- * @type bool $echo Whether or not to echo the list of pages. Default true.
- * @type string $exclude Comma-separated list of page IDs to exclude. Default empty.
- * @type array $include Comma-separated list of page IDs to include. Default empty.
- * @type string $link_after Text or HTML to follow the page link label. Default null.
- * @type string $link_before Text or HTML to precede the page link label. Default null.
- * @type string $post_type Post type to query for. Default 'page'.
- * @type string $post_status Comma-separated list of post statuses to include. Default 'publish'.
- * @type string $show_date Whether to display the page publish or modified date for each page. Accepts
- * 'modified' or any other value. An empty value hides the date. Default empty.
- * @type string $sort_column Comma-separated list of column names to sort the pages by. Accepts 'post_author',
- * 'post_date', 'post_title', 'post_name', 'post_modified', 'post_modified_gmt',
- * 'menu_order', 'post_parent', 'ID', 'rand', or 'comment_count'. Default 'post_title'.
- * @type string $title_li List heading. Passing a null or empty value will result in no heading, and the list
- * will not be wrapped with unordered list `<ul>` tags. Default 'Pages'.
- * @type Walker $walker Walker instance to use for listing pages. Default empty (Walker_Page).
+ * @type int $child_of Display only the sub-pages of a single page by ID. Default 0 (all pages).
+ * @type string $authors Comma-separated list of author IDs. Default empty (all authors).
+ * @type string $date_format PHP date format to use for the listed pages. Relies on the 'show_date' parameter.
+ * Default is the value of 'date_format' option.
+ * @type int $depth Number of levels in the hierarchy of pages to include in the generated list.
+ * Accepts -1 (any depth), 0 (all pages), 1 (top-level pages only), and n (pages to
+ * the given n depth). Default 0.
+ * @type bool $echo Whether or not to echo the list of pages. Default true.
+ * @type string $exclude Comma-separated list of page IDs to exclude. Default empty.
+ * @type array $include Comma-separated list of page IDs to include. Default empty.
+ * @type string $link_after Text or HTML to follow the page link label. Default null.
+ * @type string $link_before Text or HTML to precede the page link label. Default null.
+ * @type string $post_type Post type to query for. Default 'page'.
+ * @type string|array $post_status Comma-separated list or array of post statuses to include. Default 'publish'.
+ * @type string $show_date Whether to display the page publish or modified date for each page. Accepts
+ * 'modified' or any other value. An empty value hides the date. Default empty.
+ * @type string $sort_column Comma-separated list of column names to sort the pages by. Accepts 'post_author',
+ * 'post_date', 'post_title', 'post_name', 'post_modified', 'post_modified_gmt',
+ * 'menu_order', 'post_parent', 'ID', 'rand', or 'comment_count'. Default 'post_title'.
+ * @type string $title_li List heading. Passing a null or empty value will result in no heading, and the list
+ * will not be wrapped with unordered list `<ul>` tags. Default 'Pages'.
+ * @type string $item_spacing Whether to preserve whitespace within the menu's HTML. Accepts 'preserve' or 'discard'.
+ * Default 'preserve'.
+ * @type Walker $walker Walker instance to use for listing pages. Default empty (Walker_Page).
* }
* @return string|void HTML list of pages.
*/
function wp_list_pages( $args = '' ) {
$defaults = array(
- 'depth' => 0, 'show_date' => '',
- 'date_format' => get_option( 'date_format' ),
- 'child_of' => 0, 'exclude' => '',
- 'title_li' => __( 'Pages' ), 'echo' => 1,
- 'authors' => '', 'sort_column' => 'menu_order, post_title',
- 'link_before' => '', 'link_after' => '', 'walker' => '',
+ 'depth' => 0,
+ 'show_date' => '',
+ 'date_format' => get_option( 'date_format' ),
+ 'child_of' => 0,
+ 'exclude' => '',
+ 'title_li' => __( 'Pages' ),
+ 'echo' => 1,
+ 'authors' => '',
+ 'sort_column' => 'menu_order, post_title',
+ 'link_before' => '',
+ 'link_after' => '',
+ 'item_spacing' => 'preserve',
+ 'walker' => '',
);
$r = wp_parse_args( $args, $defaults );
+ if ( ! in_array( $r['item_spacing'], array( 'preserve', 'discard' ), true ) ) {
+ // invalid value, fall back to default.
+ $r['item_spacing'] = $defaults['item_spacing'];
+ }
+
$output = '';
$current_page = 0;
*
* @since 2.7.0
* @since 4.4.0 Added `menu_id`, `container`, `before`, `after`, and `walker` arguments.
+ * @since 4.7.0 Added the `item_spacing` argument.
*
* @param array|string $args {
* Optional. Arguments to generate a page menu. See wp_list_pages() for additional arguments.
*
- * @type string $sort_column How to short the list of pages. Accepts post column names.
- * Default 'menu_order, post_title'.
- * @type string $menu_id ID for the div containing the page list. Default is empty string.
- * @type string $menu_class Class to use for the element containing the page list. Default 'menu'.
- * @type string $container Element to use for the element containing the page list. Default 'div'.
- * @type bool $echo Whether to echo the list or return it. Accepts true (echo) or false (return).
- * Default true.
- * @type int|bool|string $show_home Whether to display the link to the home page. Can just enter the text
- * you'd like shown for the home link. 1|true defaults to 'Home'.
- * @type string $link_before The HTML or text to prepend to $show_home text. Default empty.
- * @type string $link_after The HTML or text to append to $show_home text. Default empty.
- * @type string $before The HTML or text to prepend to the menu. Default is '<ul>'.
- * @type string $after The HTML or text to append to the menu. Default is '</ul>'.
- * @type Walker $walker Walker instance to use for listing pages. Default empty (Walker_Page).
+ * @type string $sort_column How to short the list of pages. Accepts post column names.
+ * Default 'menu_order, post_title'.
+ * @type string $menu_id ID for the div containing the page list. Default is empty string.
+ * @type string $menu_class Class to use for the element containing the page list. Default 'menu'.
+ * @type string $container Element to use for the element containing the page list. Default 'div'.
+ * @type bool $echo Whether to echo the list or return it. Accepts true (echo) or false (return).
+ * Default true.
+ * @type int|bool|string $show_home Whether to display the link to the home page. Can just enter the text
+ * you'd like shown for the home link. 1|true defaults to 'Home'.
+ * @type string $link_before The HTML or text to prepend to $show_home text. Default empty.
+ * @type string $link_after The HTML or text to append to $show_home text. Default empty.
+ * @type string $before The HTML or text to prepend to the menu. Default is '<ul>'.
+ * @type string $after The HTML or text to append to the menu. Default is '</ul>'.
+ * @type string $item_spacing Whether to preserve whitespace within the menu's HTML. Accepts 'preserve' or 'discard'. Default 'discard'.
+ * @type Walker $walker Walker instance to use for listing pages. Default empty (Walker_Page).
* }
* @return string|void HTML menu
*/
function wp_page_menu( $args = array() ) {
$defaults = array(
- 'sort_column' => 'menu_order, post_title',
- 'menu_id' => '',
- 'menu_class' => 'menu',
- 'container' => 'div',
- 'echo' => true,
- 'link_before' => '',
- 'link_after' => '',
- 'before' => '<ul>',
- 'after' => '</ul>',
- 'walker' => '',
+ 'sort_column' => 'menu_order, post_title',
+ 'menu_id' => '',
+ 'menu_class' => 'menu',
+ 'container' => 'div',
+ 'echo' => true,
+ 'link_before' => '',
+ 'link_after' => '',
+ 'before' => '<ul>',
+ 'after' => '</ul>',
+ 'item_spacing' => 'discard',
+ 'walker' => '',
);
$args = wp_parse_args( $args, $defaults );
+ if ( ! in_array( $args['item_spacing'], array( 'preserve', 'discard' ) ) ) {
+ // invalid value, fall back to default.
+ $args['item_spacing'] = $defaults['item_spacing'];
+ }
+
+ if ( 'preserve' === $args['item_spacing'] ) {
+ $t = "\t";
+ $n = "\n";
+ } else {
+ $t = '';
+ $n = '';
+ }
+
/**
* Filters the arguments used to generate a page-based menu.
*
$list_args['echo'] = false;
$list_args['title_li'] = '';
- $menu .= str_replace( array( "\r", "\n", "\t" ), '', wp_list_pages($list_args) );
+ $menu .= wp_list_pages( $list_args );
$container = sanitize_text_field( $args['container'] );
if ( isset( $args['fallback_cb'] ) &&
'wp_page_menu' === $args['fallback_cb'] &&
'ul' !== $container ) {
- $args['before'] = '<ul>';
+ $args['before'] = "<ul>{$n}";
$args['after'] = '</ul>';
}
$attrs .= ' class="' . esc_attr( $args['menu_class'] ) . '"';
}
- $menu = "<{$container}{$attrs}>" . $menu . "</{$container}>\n";
+ $menu = "<{$container}{$attrs}>" . $menu . "</{$container}>{$n}";
/**
* Filters the HTML output of a page-based menu.
function wp_get_attachment_link( $id = 0, $size = 'thumbnail', $permalink = false, $icon = false, $text = false, $attr = '' ) {
$_post = get_post( $id );
- if ( empty( $_post ) || ( 'attachment' != $_post->post_type ) || ! $url = wp_get_attachment_url( $_post->ID ) )
+ if ( empty( $_post ) || ( 'attachment' !== $_post->post_type ) || ! $url = wp_get_attachment_url( $_post->ID ) ) {
return __( 'Missing Attachment' );
+ }
- if ( $permalink )
+ if ( $permalink ) {
$url = get_attachment_link( $_post->ID );
+ }
if ( $text ) {
$link_text = $text;
$link_text = '';
}
- if ( trim( $link_text ) == '' )
+ if ( '' === trim( $link_text ) ) {
$link_text = $_post->post_title;
+ }
+ if ( '' === trim( $link_text ) ) {
+ $link_text = esc_html( pathinfo( get_attached_file( $_post->ID ), PATHINFO_FILENAME ) );
+ }
/**
* Filters a retrieved attachment page link.
*
*
* @since 2.5.0
* @since 4.2.0 The `$template` parameter was changed to also accept an array of page templates.
+ * @since 4.7.0 Now works with any post type, not just pages.
*
* @param string|array $template The specific template name or array of templates to match.
* @return bool True on success, false on failure.
*/
function is_page_template( $template = '' ) {
- if ( ! is_page() )
- return false;
-
$page_template = get_page_template_slug( get_queried_object_id() );
if ( empty( $template ) )
}
/**
- * Get the specific template name for a page.
+ * Get the specific template name for a given post.
*
* @since 3.4.0
+ * @since 4.7.0 Now works with any post type, not just pages.
*
- * @param int $post_id Optional. The page ID to check. Defaults to the current post, when used in the loop.
+ * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post.
* @return string|false Page template filename. Returns an empty string when the default page template
- * is in use. Returns false if the post is not a page.
+ * is in use. Returns false if the post does not exist.
*/
-function get_page_template_slug( $post_id = null ) {
- $post = get_post( $post_id );
- if ( ! $post || 'page' != $post->post_type )
+function get_page_template_slug( $post = null ) {
+ $post = get_post( $post );
+
+ if ( ! $post ) {
return false;
+ }
+
$template = get_post_meta( $post->ID, '_wp_page_template', true );
- if ( ! $template || 'default' == $template )
+
+ if ( ! $template || 'default' == $template ) {
return '';
+ }
+
return $template;
}
/* translators: revision date format, see https://secure.php.net/date */
$datef = _x( 'F j, Y @ H:i:s', 'revision date format' );
- /* translators: 1: date */
- $autosavef = _x( '%1$s [Autosave]', 'post revision title extra' );
- /* translators: 1: date */
- $currentf = _x( '%1$s [Current Revision]', 'post revision title extra' );
+ /* translators: %s: revision date */
+ $autosavef = __( '%s [Autosave]' );
+ /* translators: %s: revision date */
+ $currentf = __( '%s [Current Revision]' );
$date = date_i18n( $datef, strtotime( $revision->post_modified ) );
if ( $link && current_user_can( 'edit_post', $revision->ID ) && $link = get_edit_post_link( $revision->ID ) )
$revision_date_author = sprintf(
/* translators: post revision title: 1: author avatar, 2: author name, 3: time ago, 4: date */
- _x( '%1$s %2$s, %3$s ago (%4$s)', 'post revision title' ),
+ __( '%1$s %2$s, %3$s ago (%4$s)' ),
$gravatar,
$author,
human_time_diff( strtotime( $revision->post_modified ), current_time( 'timestamp' ) ),
$date
);
- $autosavef = __( '%1$s [Autosave]' );
- $currentf = __( '%1$s [Current Revision]' );
+ /* translators: %s: revision date with author avatar */
+ $autosavef = __( '%s [Autosave]' );
+ /* translators: %s: revision date with author avatar */
+ $currentf = __( '%s [Current Revision]' );
if ( !wp_is_post_revision( $revision ) )
$revision_date_author = sprintf( $currentf, $revision_date_author );