Privacy: Add template tags for building link to privacy policy page.

This introduces the `get_the_privacy_policy_link()` and `the_privacy_policy_link()` functions, as well as the `privacy_policy_url` filter.

A new `tests/url/` folder was added to better organize tests related to `get_*_url()` functions. Previously, those tests were placed in `tests/url.php` and `tests/link/`, but neither of those locations are optimal. Placing tests in `tests/url.php` violates the guideline of creating separate files/classes for each function under test, and using `tests/link/` conflates two distinct -- albeit related -- groups of functions. Over time, URL-related tests can be migrated to the new folder.

Props birgire, xkon, azaozz, iandunn.
See #43850.


git-svn-id: https://develop.svn.wordpress.org/trunk@43002 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Ian Dunn 2018-04-25 15:54:29 +00:00
parent 50e9639f22
commit d336475bf5
3 changed files with 314 additions and 1 deletions

View File

@ -4282,10 +4282,74 @@ function get_parent_theme_file_path( $file = '' ) {
* @return string The URL to the privacy policy page. Empty string if it doesn't exist.
*/
function get_privacy_policy_url() {
$url = '';
$policy_page_id = (int) get_option( 'wp_page_for_privacy_policy' );
if ( ! empty( $policy_page_id ) && get_post_status( $policy_page_id ) === 'publish' ) {
return get_permalink( $policy_page_id );
$url = (string) get_permalink( $policy_page_id );
}
/**
* Filters the URL of the privacy policy page.
*
* @since 4.9.6
*
* @param string $url The URL to the privacy policy page. Empty string
* if it doesn't exist.
* @param int $policy_page_id The ID of privacy policy page.
*/
return apply_filters( 'privacy_policy_url', $url, $policy_page_id );
}
/**
* Displays the privacy policy link with formatting, when applicable.
*
* @since 4.9.6
*
* @param string $before Optional. Display before privacy policy link. Default empty.
* @param string $after Optional. Display after privacy policy link. Default empty.
*/
function the_privacy_policy_link( $before = '', $after = '' ) {
echo get_the_privacy_policy_link( $before, $after );
}
/**
* Returns the privacy policy link with formatting, when applicable.
*
* @since 4.9.6
*
* @param string $before Optional. Display before privacy policy link. Default empty.
* @param string $after Optional. Display after privacy policy link. Default empty.
*
* @return string Markup for the link and surrounding elements. Empty string if it
* doesn't exist.
*/
function get_the_privacy_policy_link( $before = '', $after = '' ) {
$link = '';
$privacy_policy_url = get_privacy_policy_url();
if ( $privacy_policy_url ) {
$link = sprintf(
'<a class="privacy-policy-link" href="%s">%s</a>',
esc_url( $privacy_policy_url ),
__( 'Privacy Policy' )
);
}
/**
* Filters the privacy policy link.
*
* @since 4.9.6
*
* @param string $link The privacy policy link. Empty string if it
* doesn't exist.
* @param string $privacy_policy_url The URL of the privacy policy. Empty string
* if it doesn't exist.
*/
$link = apply_filters( 'the_privacy_policy_link', $link, $privacy_policy_url );
if ( $link ) {
return $before . $link . $after;
}
return '';

View File

@ -0,0 +1,143 @@
<?php
/**
* Define a class to test the `get_the_privacy_policy_link()` function.
*
* @package WordPress
* @subpackage UnitTests
* @since 4.9.6
*/
/**
* Test cases for the `get_the_privacy_policy_link()` function.
*
* @group link
* @group privacy
* @covers get_the_privacy_policy_link
*
* @since 4.9.6
*/
class Tests_Link_GetThePrivacyPolicyLink extends WP_UnitTestCase {
/**
* The ID of the Privacy Policy page.
*
* @since 4.9.6
* @var int $privacy_policy_page_id
*/
protected static $privacy_policy_page_id;
/**
* The URL of the Privacy Policy page.
*
* @since 4.9.6
* @var string $privacy_policy_url
*/
protected static $privacy_policy_url;
/**
* The text that gets prepended to the `get_the_privacy_policy_link()` output.
*
* @since 4.9.6
* @var string $before
*/
protected static $before;
/**
* The text that gets appended to the `get_the_privacy_policy_link()` output.
*
* @since 4.9.6
* @var string $after
*/
protected static $after;
/**
* Create fixtures that are shared by multiple test cases.
*
* @param WP_UnitTest_Factory $factory The base factory object.
*/
public static function wpSetUpBeforeClass( $factory ) {
self::$privacy_policy_page_id = $factory->post->create(
array(
'post_type' => 'page',
'post_title' => WP_TESTS_DOMAIN . ' Privacy Policy',
)
);
// `esc_url()` is added for consistency with `get_the_privacy_policy_link()`.
self::$privacy_policy_url = esc_url( get_permalink( self::$privacy_policy_page_id ) );
self::$before = '<span class="privacy-policy-link-wrapper">';
self::$after = '</span>';
}
/**
* The function should return a valid link if a privacy policy page has been
* created and set as the `wp_page_for_privacy_policy`.
*/
public function test_get_the_privacy_policy_link_should_return_valid_link_when_privacy_page_set() {
update_option( 'wp_page_for_privacy_policy', self::$privacy_policy_page_id );
$actual_link = get_the_privacy_policy_link();
$this->assertStringStartsWith( '<a', $actual_link );
$this->assertContains( self::$privacy_policy_url, $actual_link );
$this->assertStringEndsWith( '</a>', $actual_link );
}
/**
* The function should prepend the supplied `$before` markup and append the
* supplied `$after` markup when the `wp_page_for_privacy_policy` is configured.
*/
public function test_get_the_privacy_policy_link_should_prepend_and_append_supplied_markup_when_privacy_page_set() {
update_option( 'wp_page_for_privacy_policy', self::$privacy_policy_page_id );
$actual_link = get_the_privacy_policy_link( self::$before, self::$after );
$this->assertStringStartsWith( self::$before . '<a', $actual_link );
$this->assertContains( self::$privacy_policy_url, $actual_link );
$this->assertStringEndsWith( '</a>' . self::$after, $actual_link );
}
/**
* The function should _not_ prepend the supplied `$before` markup and append
* the supplied `$after` markup when the `wp_page_for_privacy_policy` is _not_ configured.
*/
public function test_get_the_privacy_policy_link_should_not_prepend_and_append_supplied_markup_when_privacy_page_not_set() {
$actual_link = get_the_privacy_policy_link( self::$before, self::$after );
$this->assertSame( '', $actual_link );
}
/**
* The function should return an empty string when `wp_page_for_privacy_policy` is _not_ configured.
*/
public function test_get_the_privacy_policy_link_should_return_empty_string_when_privacy_page_not_set() {
$this->assertSame( '', get_the_privacy_policy_link() );
}
/**
* The output of the get_the_privacy_policy_link() function should be filterable with the 'privacy_policy_link' filter.
*/
public function test_get_the_privacy_policy_link_should_be_filterable() {
update_option( 'wp_page_for_privacy_policy', self::$privacy_policy_page_id );
$expected_url = get_privacy_policy_url();
$this->assertNotEmpty( $expected_url );
add_filter( 'the_privacy_policy_link', array( $this, 'modify_link_markup' ), 10, 2 );
$this->assertSame( 'Policy: ' . $expected_url, get_the_privacy_policy_link() );
remove_filter( 'the_privacy_policy_link', array( $this, 'modify_link_markup' ), 10 );
}
/**
* Return modified `the_privacy_policy_link` content in order to test the filter.
*
* @param string $link The privacy policy link. Empty string if it
* doesn't exist.
* @param string $privacy_policy_url The URL of the privacy policy. Empty string
* if it doesn't exist.
* @return string
*/
public static function modify_link_markup( $link, $privacy_policy_url ) {
return 'Policy: ' . $privacy_policy_url;
}
}

View File

@ -0,0 +1,106 @@
<?php
/**
* Define a class to test `get_privacy_policy_url()`.
*
* @package WordPress
* @subpackage UnitTests
* @since 4.9.6
*/
/**
* Test cases for `get_privacy_policy_url()`.
*
* @group url
* @group privacy
* @covers get_privacy_policy_url
*
* @since 4.9.6
*/
class Tests_Url_GetPrivacyPolicyUrl extends WP_UnitTestCase {
/**
* The ID of the Privacy Policy page.
*
* @since 4.9.6
* @var int $privacy_policy_page_id
*/
protected static $privacy_policy_page_id;
/**
* The URL of the Privacy Policy page.
*
* @since 4.9.6
* @var string $privacy_policy_url
*/
protected static $privacy_policy_url;
/**
* Create fixtures that are shared by multiple test cases.
*
* @param WP_UnitTest_Factory $factory The base factory object.
*/
public static function wpSetUpBeforeClass( $factory ) {
self::$privacy_policy_page_id = $factory->post->create(
array(
'post_type' => 'page',
'post_title' => WP_TESTS_DOMAIN . ' Privacy Policy',
)
);
self::$privacy_policy_url = get_permalink( self::$privacy_policy_page_id );
}
/**
* The function should return an empty string when `wp_page_for_privacy_policy` is _not_ set.
*/
public function test_get_privacy_policy_url_should_return_empty_string_when_policy_page_not_set() {
$this->assertSame( '', get_privacy_policy_url() );
}
/**
* The function should return the privacy policy URL when `wp_page_for_privacy_policy` is set.
*/
public function test_get_privacy_policy_url_should_return_valid_url_when_policy_page_set() {
update_option( 'wp_page_for_privacy_policy', self::$privacy_policy_page_id );
$this->assertSame( self::$privacy_policy_url, get_privacy_policy_url() );
}
/**
* The function should return an empty string when `wp_page_for_privacy_policy` is _not_ set.
*/
public function test_get_privacy_policy_url_should_return_empty_when_privacy_policy_page_not_set() {
$this->assertSame( '', get_privacy_policy_url() );
}
/**
* The function should return an empty string for an invalid `wp_page_for_privacy_policy` value.
*/
public function test_get_privacy_policy_url_should_return_empty_for_non_existing_page() {
update_option( 'wp_page_for_privacy_policy', PHP_INT_MAX );
$this->assertSame( '', get_privacy_policy_url() );
}
/**
* The output of `get_privacy_policy_url()` should be filterable with the 'privacy_policy_url' filter.
*/
public function test_get_privacy_policy_url_should_be_filterable() {
update_option( 'wp_page_for_privacy_policy', self::$privacy_policy_page_id );
add_filter( 'privacy_policy_url', array( $this, 'modify_policy_url' ), 10, 2 );
$this->assertSame( 'Page ID: ' . self::$privacy_policy_page_id, get_privacy_policy_url() );
remove_filter( 'privacy_policy_url', array( $this, 'modify_policy_url' ), 10 );
}
/**
* Return modified `privacy_policy_url` content in order to test the filter.
*
* @param string $url The URL to the privacy policy page. Empty string
* if it doesn't exist.
* @param int $policy_page_id The ID of privacy policy page.
* @return string
*/
public static function modify_policy_url( $url, $policy_page_id ) {
return 'Page ID: ' . $policy_page_id;
}
}