-
Notifications
You must be signed in to change notification settings - Fork 0
/
MetaDescriptionTag.hooks.php
51 lines (40 loc) · 1.41 KB
/
MetaDescriptionTag.hooks.php
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
<?php
class MetaDescriptionTagHooks {
/**
* Sets up the MetaDescriptionTag Parser hook and system messages
*
* @param Parser $parser
*
* @return bool true
*/
public static function onParserFirstCallInit( Parser &$parser ) {
$parser->setHook( 'metadesc', [ __CLASS__, 'renderMetaDescriptionTag' ] );
return true;
}
/**
* Renders the <metadesc> tag.
*
* @param String $text The description to output
* @param array $params Attributes specified for the tag. Should be an empty array
* @param Parser $parser Reference to currently running parser
*
* @return String Always empty (because we don't output anything to the text).
*/
public static function renderMetaDescriptionTag( $text, $params, Parser $parser, PPFrame $frame ) {
// Short-circuit with error message if content is not specified.
if ( !isset( $text ) ) {
$errorText = wfMessage( 'metadescriptiontag-missing-content' )->inContentLanguage(
)->text();
return Html::element( 'div', [ 'class' => 'errorbox' ], $errorText );
}
$parser->getOutput()->setExtensionData( 'metaDescription', trim( $text ) );
return '';
}
public static function onOutputPageParserOutput( OutputPage &$out, ParserOutput $parseroutput ) {
$metaDescription = $parseroutput->getExtensionData( 'metaDescription' );
if ( !empty( $metaDescription ) ) {
$out->addMeta( 'description', htmlspecialchars( $metaDescription ) );
}
return true;
}
}