Completed
Push — master ( bd7ab6...f96a9e )
by mw
11:59 queued 10:10
created

addMetaPropertyMarkup()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
ccs 11
cts 11
cp 1
cc 2
eloc 9
nc 2
nop 2
crap 2
1
<?php
2
3
namespace SMT;
4
5
use OutputPage;
6
7
/**
8
 * @license GNU GPL v2+
9
 * @since 1.0
10
 *
11
 * @author mwjames
12
 */
13
class OutputPageHtmlTagsInserter {
14
15
	/**
16
	 * @var OutputPage
17
	 */
18
	private $outputPage;
19
20
	/**
21
	 * @var array
22
	 */
23
	private $metaTagsBlacklist = array();
24
25
	/**
26
	 * @var array
27
	 */
28
	private $metaPropertyPrefixes = array();
29
30
	/**
31
	 * @var boolean
32
	 */
33
	private $metaPropertyMarkup = false;
34
35
	/**
36
	 * @var string
37
	 */
38
	private $actionName = '';
39
40
	/**
41
	 * @since 1.0
42
	 *
43
	 * @param OutputPage $outputPage
44
	 */
45 12
	public function __construct( OutputPage $outputPage ) {
46 12
		$this->outputPage = $outputPage;
47 12
	}
48
49
	/**
50
	 * @since 1.0
51
	 *
52
	 * @param array $metaTagsBlacklist
53
	 */
54 3
	public function setMetaTagsBlacklist( array $metaTagsBlacklist ) {
55 3
		$this->metaTagsBlacklist = array_flip( $metaTagsBlacklist );
56 3
	}
57
58
	/**
59
	 * @since 1.4
60
	 *
61
	 * @param array $metaPropertyPrefixes
62
	 */
63 3
	public function setMetaPropertyPrefixes( array $metaPropertyPrefixes ) {
64 3
		$this->metaPropertyPrefixes = $metaPropertyPrefixes;
65 3
	}
66
67
	/**
68
	 * @since 1.0
69
	 *
70
	 * @param string $actionName
71
	 */
72 3
	public function setActionName( $actionName ) {
73 3
		$this->actionName = $actionName;
74 3
	}
75
76
	/**
77
	 * @since  1.0
78
	 *
79
	 * @return boolean
80
	 */
81 4
	public function canUseOutputPage() {
82
83 4
		if ( $this->outputPage->getTitle() === null || $this->outputPage->getTitle()->isSpecialPage() || $this->actionName !== 'view' ) {
84 2
			return false;
85
		}
86
87 2
		return true;
88
	}
89
90
	/**
91
	 * @since 1.0
92
	 *
93
	 * @param string $tag
94
	 * @param string $content
95
	 */
96 9
	public function addTagContentToOutputPage( $tag, $content ) {
97
98 9
		$tag = strtolower( htmlspecialchars( trim( $tag ) ) );
99 9
		$content = htmlspecialchars( $content );
100
101 9
		if ( isset( $this->metaTagsBlacklist[$tag] ) ) {
102 2
			return;
103
		}
104
105 8
		if ( $this->reqMetaPropertyMarkup( $tag ) ) {
106 2
			return $this->addMetaPropertyMarkup( $tag, $content );
107
		}
108
109 7
		$this->outputPage->addMeta( $tag, $content );
110 7
	}
111
112 2
	private function addMetaPropertyMarkup( $tag, $content ) {
113
114 2
		$comment = '';
115
116 2
		if ( !$this->metaPropertyMarkup ) {
117 2
			$comment .= '<!-- Semantic MetaTags -->' . "\n";
118 2
			$this->metaPropertyMarkup = true;
119 2
		}
120
121 2
		$content = $comment . \Html::element( 'meta', array(
122 2
			'property' => $tag,
123
			'content'  => $content
124 2
		) );
125
126 2
		$this->outputPage->addHeadItem( "meta:property:$tag", $content );
127 2
	}
128
129 8
	private function reqMetaPropertyMarkup( $tag ) {
130
131
		// If a tag contains a `og:` such as `og:title` it is expected to be a
132
		// OpenGraph protocol tag along with other prefixes maintained in
133
		// $GLOBALS['smtgMetaPropertyPrefixes']
134 8
		foreach ( $this->metaPropertyPrefixes as $prefix ) {
135 3
			if ( strpos( $tag, $prefix ) !== false ) {
136 2
				return true;
137
			}
138 7
		}
139
140 7
		return false;
141
	}
142
143
}
144