Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
45.45% covered (danger)
45.45%
5 / 11
100.00% covered (success)
100.00%
1 / 1
CRAP
n/a
0 / 0
jetpack_canonical_urls_output_tag
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2/**
3 * Module Name: Canonical URLs
4 * Module Description: Add canonical URL tags to archive pages to prevent duplicate content in search engines.
5 * Sort Order: 36
6 * First Introduced: 15.6
7 * Requires Connection: No
8 * Requires User Connection: No
9 * Auto Activate: No
10 * Module Tags: Traffic
11 * Feature: Traffic
12 * Additional Search Queries: canonical, seo, duplicate content, woocommerce, archive
13 *
14 * @package automattic/jetpack
15 */
16
17if ( ! defined( 'ABSPATH' ) ) {
18    exit( 0 );
19}
20
21require_once __DIR__ . '/canonical-urls/canonical-urls-conflicts.php';
22
23/**
24 * Can be used to prevent the Canonical URLs module from outputting canonical tags.
25 *
26 * @module canonical-urls
27 *
28 * @since 15.6
29 *
30 * @param bool $disabled Whether canonical URL output is disabled. Defaults to false.
31 */
32if ( ! apply_filters( 'jetpack_disable_canonical_urls', false ) ) {
33    require_once __DIR__ . '/canonical-urls/class-jetpack-canonical-urls-resolver.php';
34    add_action( 'wp_head', 'jetpack_canonical_urls_output_tag' );
35}
36
37/**
38 * Output the canonical link tag for non-singular pages.
39 *
40 * WordPress core handles singular posts/pages via rel_canonical(),
41 * so this function only outputs canonical tags for archive pages.
42 *
43 * @since 15.6
44 */
45function jetpack_canonical_urls_output_tag() {
46    if ( is_singular() ) {
47        return;
48    }
49
50    $url = Jetpack_Canonical_Urls_Resolver::get_canonical_url();
51
52    if ( ! empty( $url ) ) {
53        echo '<link rel="canonical" href="' . esc_url( $url ) . '" />' . "\n";
54    }
55}