Make WordPress Core

Changeset 43109


Ignore:
Timestamp:
05/02/2018 03:38:23 AM (7 years ago)
Author:
SergeyBiryukov
Message:

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.
Merges [43002] to the 4.9 branch.
See #43850.

Location:
branches/4.9
Files:
2 edited
2 copied

Legend:

Unmodified
Added
Removed
  • branches/4.9

  • branches/4.9/src/wp-includes/link-template.php

    r43102 r43109  
    41144114 */
    41154115function get_privacy_policy_url() {
     4116    $url            = '';
    41164117    $policy_page_id = (int) get_option( 'wp_page_for_privacy_policy' );
    41174118
    41184119    if ( ! empty( $policy_page_id ) && get_post_status( $policy_page_id ) === 'publish' ) {
    4119         return get_permalink( $policy_page_id );
     4120        $url = (string) get_permalink( $policy_page_id );
     4121    }
     4122
     4123    /**
     4124     * Filters the URL of the privacy policy page.
     4125     *
     4126     * @since 4.9.6
     4127     *
     4128     * @param string $url            The URL to the privacy policy page. Empty string
     4129     *                               if it doesn't exist.
     4130     * @param int    $policy_page_id The ID of privacy policy page.
     4131     */
     4132    return apply_filters( 'privacy_policy_url', $url, $policy_page_id );
     4133}
     4134
     4135/**
     4136 * Displays the privacy policy link with formatting, when applicable.
     4137 *
     4138 * @since 4.9.6
     4139 *
     4140 * @param string $before Optional. Display before privacy policy link. Default empty.
     4141 * @param string $after  Optional. Display after privacy policy link. Default empty.
     4142 */
     4143function the_privacy_policy_link( $before = '', $after = '' ) {
     4144    echo get_the_privacy_policy_link( $before, $after );
     4145}
     4146
     4147/**
     4148 * Returns the privacy policy link with formatting, when applicable.
     4149 *
     4150 * @since 4.9.6
     4151 *
     4152 * @param string $before Optional. Display before privacy policy link. Default empty.
     4153 * @param string $after  Optional. Display after privacy policy link. Default empty.
     4154 *
     4155 * @return string Markup for the link and surrounding elements. Empty string if it
     4156 *                doesn't exist.
     4157 */
     4158function get_the_privacy_policy_link( $before = '', $after = '' ) {
     4159    $link               = '';
     4160    $privacy_policy_url = get_privacy_policy_url();
     4161
     4162    if ( $privacy_policy_url ) {
     4163        $link = sprintf(
     4164            '<a class="privacy-policy-link" href="%s">%s</a>',
     4165            esc_url( $privacy_policy_url ),
     4166            __( 'Privacy Policy' )
     4167        );
     4168    }
     4169
     4170    /**
     4171     * Filters the privacy policy link.
     4172     *
     4173     * @since 4.9.6
     4174     *
     4175     * @param string $link               The privacy policy link. Empty string if it
     4176     *                                   doesn't exist.
     4177     * @param string $privacy_policy_url The URL of the privacy policy. Empty string
     4178     *                                   if it doesn't exist.
     4179     */
     4180    $link = apply_filters( 'the_privacy_policy_link', $link, $privacy_policy_url );
     4181
     4182    if ( $link ) {
     4183        return $before . $link . $after;
    41204184    }
    41214185
Note: See TracChangeset for help on using the changeset viewer.