Skip to content

Commit

Permalink
Privacy: Introduce Privacy Policy page helpers:
Browse files Browse the repository at this point in the history
* `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
  • Loading branch information
SergeyBiryukov committed Mar 21, 2019
1 parent fac9f5f commit 7d74080
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 5 deletions.
1 change: 1 addition & 0 deletions wp-admin/includes/file.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
'single.php' => __( 'Single Post' ),
'page.php' => __( 'Single Page' ),
'front-page.php' => __( 'Homepage' ),
'privacy-policy.php' => __( 'Privacy Policy Page' ),
// Attachments
'attachment.php' => __( 'Attachment Template' ),
'image.php' => __( 'Image Attachment Template' ),
Expand Down
38 changes: 38 additions & 0 deletions wp-includes/class-wp-query.php
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,14 @@ class WP_Query {
*/
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.
*
Expand Down Expand Up @@ -463,6 +471,7 @@ private function init_query_flags() {
$this->is_comment_feed = false;
$this->is_trackback = false;
$this->is_home = false;
$this->is_privacy_policy = false;
$this->is_404 = false;
$this->is_paged = false;
$this->is_admin = false;
Expand Down Expand Up @@ -998,6 +1007,10 @@ public function parse_query( $query = '' ) {
$this->is_home = 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'] ) {
Expand All @@ -1006,6 +1019,10 @@ public function parse_query( $query = '' ) {
$this->is_home = 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'] ) ) {
Expand Down Expand Up @@ -3877,6 +3894,27 @@ public function 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?
*
Expand Down
10 changes: 8 additions & 2 deletions wp-includes/nav-menu-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -361,8 +361,9 @@ function _wp_menu_item_classes_by_context( &$menu_items ) {

$possible_object_parents = array_filter( $possible_object_parents );

$front_page_url = home_url();
$front_page_id = (int) get_option( 'page_on_front' );
$front_page_url = home_url();
$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 ) {

Expand All @@ -378,6 +379,11 @@ function _wp_menu_item_classes_by_context( &$menu_items ) {
$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 ( $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;
Expand Down
3 changes: 3 additions & 0 deletions wp-includes/post-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,9 @@ function get_body_class( $class = '' ) {
if ( is_home() ) {
$classes[] = 'blog';
}
if ( is_privacy_policy() ) {
$classes[] = 'privacy-policy';
}
if ( is_archive() ) {
$classes[] = 'archive';
}
Expand Down
30 changes: 30 additions & 0 deletions wp-includes/query.php
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,36 @@ function 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.
*
Expand Down
1 change: 1 addition & 0 deletions wp-includes/template-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
elseif ( is_search() && $template = get_search_template() ) :
elseif ( is_front_page() && $template = get_front_page_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_tax() && $template = get_taxonomy_template() ) :
elseif ( is_attachment() && $template = get_attachment_template() ) :
Expand Down
22 changes: 20 additions & 2 deletions wp-includes/template.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
* 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
*
Expand All @@ -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.
*
* 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 4.8.0 The `$type` and `$templates` parameters were added.
Expand Down Expand Up @@ -376,6 +376,24 @@ function get_front_page_template() {
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.
*
Expand Down
2 changes: 1 addition & 1 deletion wp-includes/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*
* @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.
Expand Down

0 comments on commit 7d74080

Please sign in to comment.