<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[43002] trunk: Privacy: Add template tags for building link to privacy policy page.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/43002">43002</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/43002","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>iandunn</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2018-04-25 15:54:29 +0000 (Wed, 25 Apr 2018)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>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 <a href="https://core.trac.wordpress.org/ticket/43850">#43850</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludeslinktemplatephp">trunk/src/wp-includes/link-template.php</a></li>
</ul>
<h3>Added Paths</h3>
<ul>
<li><a href="#trunktestsphpunittestslinkgetThePrivacyPolicyLinkphp">trunk/tests/phpunit/tests/link/getThePrivacyPolicyLink.php</a></li>
<li>trunk/tests/phpunit/tests/url/</li>
<li><a href="#trunktestsphpunittestsurlgetPrivacyPolicyUrlphp">trunk/tests/phpunit/tests/url/getPrivacyPolicyUrl.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludeslinktemplatephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/link-template.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/link-template.php 2018-04-25 13:05:48 UTC (rev 43001)
+++ trunk/src/wp-includes/link-template.php 2018-04-25 15:54:29 UTC (rev 43002)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -4282,11 +4282,75 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @return string The URL to the privacy policy page. Empty string if it doesn't exist.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> function get_privacy_policy_url() {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $url = '';
</ins><span class="cx" style="display: block; padding: 0 10px"> $policy_page_id = (int) get_option( 'wp_page_for_privacy_policy' );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! empty( $policy_page_id ) && get_post_status( $policy_page_id ) === 'publish' ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- return get_permalink( $policy_page_id );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $url = (string) get_permalink( $policy_page_id );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+ * 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;
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> return '';
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre></div>
<a id="trunktestsphpunittestslinkgetThePrivacyPolicyLinkphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/link/getThePrivacyPolicyLink.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/link/getThePrivacyPolicyLink.php (rev 0)
+++ trunk/tests/phpunit/tests/link/getThePrivacyPolicyLink.php 2018-04-25 15:54:29 UTC (rev 43002)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,143 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?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;
+ }
+}
</ins></span></pre></div>
<a id="trunktestsphpunittestsurlgetPrivacyPolicyUrlphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/url/getPrivacyPolicyUrl.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/url/getPrivacyPolicyUrl.php (rev 0)
+++ trunk/tests/phpunit/tests/url/getPrivacyPolicyUrl.php 2018-04-25 15:54:29 UTC (rev 43002)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,106 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?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;
+ }
+}
</ins></span></pre>
</div>
</div>
</body>
</html>