summaryrefslogtreecommitdiff
blob: d8bccd46feab24b2dc4218a70525d362463c6c92 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php

/**
 * An SEO expert walks into a bar, bars, pub, public house, Irish pub, drinks, beer, wine, liquor, Grey Goose, Cristal...
 */
class Jetpack_SEO {
	public function __construct() {
		add_action( 'init', array( $this, 'init' ) );
	}

	public function init() {
		/**
		 * Can be used to prevent SEO tools from inserting custom meta tags.
		 *
		 * @module seo-tools
		 *
		 * @since 4.4.0
		 *
		 * @param bool true Should Jetpack's SEO Meta Tags be enabled. Defaults to true.
		 */
		if ( apply_filters( 'jetpack_seo_meta_tags_enabled', true ) ) {
			add_action( 'wp_head', array( $this, 'meta_tags' ) );

			// Add support for editing page excerpts in pages, regardless of theme support.
			add_post_type_support( 'page', 'excerpt' );
		}

		/**
		 * Can be used to prevent SEO tools form modifying site titles.
		 *
		 * @module seo-tools
		 *
		 * @since 4.4.0
		 *
		 * @param bool true Should Jetpack SEO modify site titles. Defaults to true.
		 */
		if ( apply_filters( 'jetpack_seo_custom_titles', true ) ) {
			// Overwrite page title with custom SEO meta title for themes that support title-tag.
			add_filter( 'pre_get_document_title', array( 'Jetpack_SEO_Titles', 'get_custom_title' ) );

			// Add overwrite support for themes that don't support title-tag.
			add_filter( 'wp_title', array( 'Jetpack_SEO_Titles', 'get_custom_title' ) );
		}

		add_filter( 'jetpack_open_graph_tags', array( $this, 'set_custom_og_tags' ) );
	}

	private function get_authors() {
		global $wp_query;

		$authors = array();

		foreach ( $wp_query->posts as $post ) {
			$authors[] = get_the_author_meta( 'display_name', (int) $post->post_author );
		}

		$authors = array_unique( $authors );

		return $authors;
	}

	public function set_custom_og_tags( $tags ) {
		$custom_title = Jetpack_SEO_Titles::get_custom_title();

		if ( ! empty( $custom_title ) ) {
			$tags['og:title'] = $custom_title;
		}

		$post_custom_description = Jetpack_SEO_Posts::get_post_custom_description( get_post() );
		$front_page_meta = Jetpack_SEO_Utils::get_front_page_meta_description();

		if ( is_front_page() && ! empty( $front_page_meta ) ) {
			$tags['og:description'] = $front_page_meta;
		} else {
			if ( ! empty( $post_custom_description ) ) {
				$tags['og:description'] = $post_custom_description;
			}
		}

		return $tags;
	}

	public function meta_tags() {
		global $wp_query;

		$period   = '';
		$template = '';
		$meta     = array();

		/**
		 * Can be used to specify a list of themes that set their own meta tags.
		 *
		 * If current site is using one of the themes listed as conflicting, inserting Jetpack SEO
		 * meta tags will be prevented.
		 *
		 * @module seo-tools
		 *
		 * @since 4.4.0
		 *
		 * @param array List of conflicted theme names. Defaults to empty array.
		 */
		$conflicted_themes = apply_filters( 'jetpack_seo_meta_tags_conflicted_themes', array() );

		if ( isset( $conflicted_themes[ get_option( 'template' ) ] ) ) {
			return;
		}

		$front_page_meta     = Jetpack_SEO_Utils::get_front_page_meta_description();
		$description         = $front_page_meta ? $front_page_meta : get_bloginfo( 'description' );
		$meta['description'] = trim( $description );

		// Try to target things if we're on a "specific" page of any kind.
		if ( is_singular() ) {
			// Business users can overwrite the description.
			if ( ! ( is_front_page() && Jetpack_SEO_Utils::get_front_page_meta_description() ) ) {
				$description = Jetpack_SEO_Posts::get_post_description( get_post() );

				if ( $description ) {
					$description = wp_trim_words( strip_shortcodes( wp_kses( $description, array() ) ) );
					$meta['description'] = $description;
				}
			}

		} elseif ( is_author() ) {
			$obj = get_queried_object();

			$meta['description'] = sprintf(
				_x( 'Read all of the posts by %1$s on %2$s', 'Read all of the posts by Author Name on Blog Title', 'jetpack' ),
				$obj->display_name,
				get_bloginfo( 'title' )
			);
		} elseif ( is_tag() || is_category() || is_tax() ) {
			$obj = get_queried_object();

			$description = get_term_field( 'description', $obj->term_id, $obj->taxonomy, 'raw' );

			if ( ! is_wp_error( $description ) && '' != $description ) {
				$meta['description'] = wp_trim_words( $description );
			} else {
				$authors = $this->get_authors();

				$meta['description'] = wp_sprintf(
					_x( 'Posts about %1$s written by %2$l', 'Posts about Category written by John and Bob', 'jetpack' ),
					single_term_title( '', false ),
					$authors
				);
			}
		} elseif ( is_date() ) {
			if ( is_year() ) {
				$period   = get_query_var( 'year' );

				$template = _nx(
					'%1$s post published by %2$l in the year %3$s', // singular
					'%1$s posts published by %2$l in the year %3$s', // plural
					count( $wp_query->posts ), // number
					'10 posts published by John in the year 2012', // context
					'jetpack'
				);
			} elseif ( is_month() ) {
				$period   = date( 'F Y', mktime( 0, 0, 0, get_query_var( 'monthnum' ), 1, get_query_var( 'year' ) ) );

				$template = _nx(
					'%1$s post published by %2$l during %3$s', // singular
					'%1$s posts published by %2$l during %3$s', // plural
					count( $wp_query->posts ), // number
					'10 posts publishes by John during May 2012', // context
					'jetpack'
				);
			} elseif ( is_day() ) {
				$period   = date(
					'F j, Y',
					mktime( 0, 0, 0, get_query_var( 'monthnum' ), get_query_var( 'day' ), get_query_var( 'year' ) )
				);

				$template = _nx(
					'%1$s post published by %2$l on %3$s', // singular
					'%1$s posts published by %2$l on %3$s', // plural
					count( $wp_query->posts ), // number
					'10 posts published by John on May 30, 2012', // context
					'jetpack'
				);
			}

			$authors             = $this->get_authors();
			$meta['description'] = wp_sprintf( $template, count( $wp_query->posts ), $authors, $period );
		}

		/**
		 * Can be used to edit the default SEO tools meta tags.
		 *
		 * @module seo-tools
		 *
		 * @since 4.4.0
		 *
		 * @param array Array that consists of meta name and meta content pairs.
		 */
		$meta = apply_filters( 'jetpack_seo_meta_tags', $meta );

		// Output them
		foreach ( $meta as $name => $content ) {
			if ( ! empty( $content ) ) {
				echo '<meta name="' . esc_attr( $name ) . '" content="' . esc_attr( $content ) . '" />' . "\n";
			}
		}
	}
}