Privacy: Introduce Privacy Policy page helpers:

* `is_privacy_policy()` template tag
* `privacy-policy.php` template
* `.privacy-policy` body class
* `.menu-item-privacy-policy` menu item class

Props garrett-eclipse, birgire, xkon, Clorith.
Fixes #44005.
Built from https://develop.svn.wordpress.org/trunk@44966


git-svn-id: http://core.svn.wordpress.org/trunk@44797 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Sergey Biryukov 2019-03-21 19:48:50 +00:00
parent fac9f5f9fb
commit 7d74080b80
8 changed files with 102 additions and 5 deletions

View File

@ -36,6 +36,7 @@ $wp_file_descriptions = array(
'single.php' => __( 'Single Post' ), 'single.php' => __( 'Single Post' ),
'page.php' => __( 'Single Page' ), 'page.php' => __( 'Single Page' ),
'front-page.php' => __( 'Homepage' ), 'front-page.php' => __( 'Homepage' ),
'privacy-policy.php' => __( 'Privacy Policy Page' ),
// Attachments // Attachments
'attachment.php' => __( 'Attachment Template' ), 'attachment.php' => __( 'Attachment Template' ),
'image.php' => __( 'Image Attachment Template' ), 'image.php' => __( 'Image Attachment Template' ),

View File

@ -325,6 +325,14 @@ class WP_Query {
*/ */
public $is_home = false; public $is_home = false;
/**
* Signifies whether the current query is for the Privacy Policy page.
*
* @since 5.2.0
* @var bool
*/
public $is_privacy_policy = false;
/** /**
* Signifies whether the current query couldn't find anything. * Signifies whether the current query couldn't find anything.
* *
@ -463,6 +471,7 @@ class WP_Query {
$this->is_comment_feed = false; $this->is_comment_feed = false;
$this->is_trackback = false; $this->is_trackback = false;
$this->is_home = false; $this->is_home = false;
$this->is_privacy_policy = false;
$this->is_404 = false; $this->is_404 = false;
$this->is_paged = false; $this->is_paged = false;
$this->is_admin = false; $this->is_admin = false;
@ -998,6 +1007,10 @@ class WP_Query {
$this->is_home = true; $this->is_home = true;
$this->is_posts_page = true; $this->is_posts_page = true;
} }
if ( isset( $this->queried_object_id ) && $this->queried_object_id == get_option( 'wp_page_for_privacy_policy' ) ) {
$this->is_privacy_policy = true;
}
} }
if ( $qv['page_id'] ) { if ( $qv['page_id'] ) {
@ -1006,6 +1019,10 @@ class WP_Query {
$this->is_home = true; $this->is_home = true;
$this->is_posts_page = true; $this->is_posts_page = true;
} }
if ( $qv['page_id'] == get_option( 'wp_page_for_privacy_policy' ) ) {
$this->is_privacy_policy = true;
}
} }
if ( ! empty( $qv['post_type'] ) ) { if ( ! empty( $qv['post_type'] ) ) {
@ -3877,6 +3894,27 @@ class WP_Query {
return (bool) $this->is_home; return (bool) $this->is_home;
} }
/**
* Is the query for the Privacy Policy page?
*
* This is the page which shows the Privacy Policy content of your site.
*
* Depends on the site's "Change your Privacy Policy page" Privacy Settings 'wp_page_for_privacy_policy'.
*
* This function will return true only on the page you set as the "Privacy Policy page".
*
* @since 5.2.0
*
* @return bool True, if Privacy Policy page.
*/
public function is_privacy_policy() {
if ( get_option( 'wp_page_for_privacy_policy' ) && $this->is_page( get_option( 'wp_page_for_privacy_policy' ) ) ) {
return true;
} else {
return false;
}
}
/** /**
* Is the query for an existing month archive? * Is the query for an existing month archive?
* *

View File

@ -361,8 +361,9 @@ function _wp_menu_item_classes_by_context( &$menu_items ) {
$possible_object_parents = array_filter( $possible_object_parents ); $possible_object_parents = array_filter( $possible_object_parents );
$front_page_url = home_url(); $front_page_url = home_url();
$front_page_id = (int) get_option( 'page_on_front' ); $front_page_id = (int) get_option( 'page_on_front' );
$privacy_policy_page_id = (int) get_option( 'wp_page_for_privacy_policy' );
foreach ( (array) $menu_items as $key => $menu_item ) { foreach ( (array) $menu_items as $key => $menu_item ) {
@ -378,6 +379,11 @@ function _wp_menu_item_classes_by_context( &$menu_items ) {
$classes[] = 'menu-item-home'; $classes[] = 'menu-item-home';
} }
// This menu item is set as the 'Privacy Policy Page'.
if ( 'post_type' === $menu_item->type && $privacy_policy_page_id === (int) $menu_item->object_id ) {
$classes[] = 'menu-item-privacy-policy';
}
// if the menu item corresponds to a taxonomy term for the currently-queried non-hierarchical post object // if the menu item corresponds to a taxonomy term for the currently-queried non-hierarchical post object
if ( $wp_query->is_singular && 'taxonomy' == $menu_item->type && in_array( $menu_item->object_id, $possible_object_parents ) ) { if ( $wp_query->is_singular && 'taxonomy' == $menu_item->type && in_array( $menu_item->object_id, $possible_object_parents ) ) {
$active_parent_object_ids[] = (int) $menu_item->object_id; $active_parent_object_ids[] = (int) $menu_item->object_id;

View File

@ -621,6 +621,9 @@ function get_body_class( $class = '' ) {
if ( is_home() ) { if ( is_home() ) {
$classes[] = 'blog'; $classes[] = 'blog';
} }
if ( is_privacy_policy() ) {
$classes[] = 'privacy-policy';
}
if ( is_archive() ) { if ( is_archive() ) {
$classes[] = 'archive'; $classes[] = 'archive';
} }

View File

@ -489,6 +489,36 @@ function is_home() {
return $wp_query->is_home(); return $wp_query->is_home();
} }
/**
* Determines whether the query is for the Privacy Policy page.
*
* The Privacy Policy page is the page that shows the Privacy Policy content of the site.
*
* is_privacy_policy() is dependent on the site's "Change your Privacy Policy page" Privacy Settings 'wp_page_for_privacy_policy'.
*
* This function will return true only on the page you set as the "Privacy Policy page".
*
* For more information on this and similar theme functions, check out
* the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
* Conditional Tags} article in the Theme Developer Handbook.
*
* @since 5.2.0
*
* @global WP_Query $wp_query Global WP_Query instance.
*
* @return bool
*/
function is_privacy_policy() {
global $wp_query;
if ( ! isset( $wp_query ) ) {
_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
return false;
}
return $wp_query->is_privacy_policy();
}
/** /**
* Determines whether the query is for an existing month archive. * Determines whether the query is for an existing month archive.
* *

View File

@ -51,6 +51,7 @@ if ( wp_using_themes() ) :
elseif ( is_search() && $template = get_search_template() ) : elseif ( is_search() && $template = get_search_template() ) :
elseif ( is_front_page() && $template = get_front_page_template() ) : elseif ( is_front_page() && $template = get_front_page_template() ) :
elseif ( is_home() && $template = get_home_template() ) : elseif ( is_home() && $template = get_home_template() ) :
elseif ( is_privacy_policy() && $template = get_privacy_policy_template() ) :
elseif ( is_post_type_archive() && $template = get_post_type_archive_template() ) : elseif ( is_post_type_archive() && $template = get_post_type_archive_template() ) :
elseif ( is_tax() && $template = get_taxonomy_template() ) : elseif ( is_tax() && $template = get_taxonomy_template() ) :
elseif ( is_attachment() && $template = get_attachment_template() ) : elseif ( is_attachment() && $template = get_attachment_template() ) :

View File

@ -33,7 +33,7 @@ function get_query_template( $type, $templates = array() ) {
* The last element in the array should always be the fallback template for this query type. * The last element in the array should always be the fallback template for this query type.
* *
* Possible values for `$type` include: 'index', '404', 'archive', 'author', 'category', 'tag', 'taxonomy', 'date', * Possible values for `$type` include: 'index', '404', 'archive', 'author', 'category', 'tag', 'taxonomy', 'date',
* 'embed', 'home', 'frontpage', 'page', 'paged', 'search', 'single', 'singular', and 'attachment'. * 'embed', 'home', 'frontpage', 'privacypolicy', 'page', 'paged', 'search', 'single', 'singular', and 'attachment'.
* *
* @since 4.7.0 * @since 4.7.0
* *
@ -51,7 +51,7 @@ function get_query_template( $type, $templates = array() ) {
* This hook also applies to various types of files loaded as part of the Template Hierarchy. * This hook also applies to various types of files loaded as part of the Template Hierarchy.
* *
* Possible values for `$type` include: 'index', '404', 'archive', 'author', 'category', 'tag', 'taxonomy', 'date', * Possible values for `$type` include: 'index', '404', 'archive', 'author', 'category', 'tag', 'taxonomy', 'date',
* 'embed', 'home', 'frontpage', 'page', 'paged', 'search', 'single', 'singular', and 'attachment'. * 'embed', 'home', 'frontpage', 'privacypolicy', 'page', 'paged', 'search', 'single', 'singular', and 'attachment'.
* *
* @since 1.5.0 * @since 1.5.0
* @since 4.8.0 The `$type` and `$templates` parameters were added. * @since 4.8.0 The `$type` and `$templates` parameters were added.
@ -376,6 +376,24 @@ function get_front_page_template() {
return get_query_template( 'front_page', $templates ); return get_query_template( 'front_page', $templates );
} }
/**
* Retrieve path of Privacy Policy page template in current or parent template.
*
* The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
* and {@see '$type_template'} dynamic hooks, where `$type` is 'privacypolicy'.
*
* @since 5.2.0
*
* @see get_query_template()
*
* @return string Full path to front page template file.
*/
function get_privacy_policy_template() {
$templates = array( 'privacy-policy.php' );
return get_query_template( 'privacy_policy', $templates );
}
/** /**
* Retrieve path of page template in current or parent template. * Retrieve path of page template in current or parent template.
* *

View File

@ -13,7 +13,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '5.2-alpha-44965'; $wp_version = '5.2-alpha-44966';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.