WordPress.org

Make WordPress Core

Changeset 43002


Ignore:
Timestamp:
04/25/2018 03:54:29 PM (2 years ago)
Author:
iandunn
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.
See #43850.

Location:
trunk
Files:
3 added
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/link-template.php

    r42995 r43002  
    42834283 */
    42844284function get_privacy_policy_url() {
     4285    $url            = '';
    42854286    $policy_page_id = (int) get_option( 'wp_page_for_privacy_policy' );
    42864287
    42874288    if ( ! empty( $policy_page_id ) && get_post_status( $policy_page_id ) === 'publish' ) {
    4288         return get_permalink( $policy_page_id );
     4289        $url = (string) get_permalink( $policy_page_id );
     4290    }
     4291
     4292    /**
     4293     * Filters the URL of the privacy policy page.
     4294     *
     4295     * @since 4.9.6
     4296     *
     4297     * @param string $url            The URL to the privacy policy page. Empty string
     4298     *                               if it doesn't exist.
     4299     * @param int    $policy_page_id The ID of privacy policy page.
     4300     */
     4301    return apply_filters( 'privacy_policy_url', $url, $policy_page_id );
     4302}
     4303
     4304/**
     4305 * Displays the privacy policy link with formatting, when applicable.
     4306 *
     4307 * @since 4.9.6
     4308 *
     4309 * @param string $before Optional. Display before privacy policy link. Default empty.
     4310 * @param string $after  Optional. Display after privacy policy link. Default empty.
     4311 */
     4312function the_privacy_policy_link( $before = '', $after = '' ) {
     4313    echo get_the_privacy_policy_link( $before, $after );
     4314}
     4315
     4316/**
     4317 * Returns the privacy policy link with formatting, when applicable.
     4318 *
     4319 * @since 4.9.6
     4320 *
     4321 * @param string $before Optional. Display before privacy policy link. Default empty.
     4322 * @param string $after  Optional. Display after privacy policy link. Default empty.
     4323 *
     4324 * @return string Markup for the link and surrounding elements. Empty string if it
     4325 *                doesn't exist.
     4326 */
     4327function get_the_privacy_policy_link( $before = '', $after = '' ) {
     4328    $link               = '';
     4329    $privacy_policy_url = get_privacy_policy_url();
     4330
     4331    if ( $privacy_policy_url ) {
     4332        $link = sprintf(
     4333            '<a class="privacy-policy-link" href="%s">%s</a>',
     4334            esc_url( $privacy_policy_url ),
     4335            __( 'Privacy Policy' )
     4336        );
     4337    }
     4338
     4339    /**
     4340     * Filters the privacy policy link.
     4341     *
     4342     * @since 4.9.6
     4343     *
     4344     * @param string $link               The privacy policy link. Empty string if it
     4345     *                                   doesn't exist.
     4346     * @param string $privacy_policy_url The URL of the privacy policy. Empty string
     4347     *                                   if it doesn't exist.
     4348     */
     4349    $link = apply_filters( 'the_privacy_policy_link', $link, $privacy_policy_url );
     4350
     4351    if ( $link ) {
     4352        return $before . $link . $after;
    42894353    }
    42904354
Note: See TracChangeset for help on using the changeset viewer.