Completed
Push — develop ( 8dee00...a73902 )
by David
03:37
created

Wordlift_Page_Service::handler()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
cc 1
eloc 8
c 2
b 0
f 2
nc 1
nop 1
dl 0
loc 17
rs 9.4285
1
<?php
2
3
/**
4
 * The Wordlift_Page_Service alters the page output to add schema.org markup in order to provide sharing services
5
 * such as Google+ the correct page title (otherwise they would read the first entity name).
6
 *
7
 * See https://github.com/insideout10/wordlift-plugin/issues/262
8
 *
9
 * @since 3.5.3
10
 */
11
class Wordlift_Page_Service {
12
13
	/**
14
	 * Hook to wp_head and create a response buffer.
15
	 *
16
	 * @since 3.5.3
17
	 */
18
	public function wp_head() {
19
20
		// When the buffer is flushed, have the handler markup the content.
21
		ob_start( array( $this, 'handler' ) );
22
23
	}
24
25
	/**
26
	 * Hook to wp_footer and flush the response buffer.
27
	 *
28
	 * @since 3.5.3
29
	 */
30
	public function wp_footer() {
31
32
		ob_end_flush();
33
34
	}
35
36
	/**
37
	 * Handle the buffer, by inserting schema.org microdata markup with itemscope/itemtype/itemprop.
38
	 *
39
	 * @since 3.5.3
40
	 *
41
	 * @param string $buffer The output buffer.
42
	 *
43
	 * @return string The processed output buffer.
44
	 */
45
	public function handler( $buffer ) {
46
47
		// Look for the following regexs.
48
		$regexs = array(
49
			'/<article ([^>]*)class="([^"]*)type-post([^"]*)"([^>]*)/i',
50
			'/<h1 ([^>]*)class="([^"]*)entry-title([^"]*)"([^>]*)/i',
51
		);
52
53
		// Replacements.
54
		$replacements = array(
55
			'<article itemscope itemtype="http://schema.org/Article" ${1}class="${2}type-post${3}"${4}',
56
			'<h1 itemprop="name" ${1}class="${2}entry-title${3}"${4}',
57
		);
58
59
		// Perform the replacements in the buffer.
60
		return preg_replace( $regexs, $replacements, $buffer );
61
	}
62
63
}
64