Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
68.00% covered (warning)
68.00%
17 / 25
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
Twitter
68.00% covered (warning)
68.00%
17 / 25
66.67% covered (warning)
66.67%
2 / 3
5.82
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 addMetadata
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 addTwitterSiteHandleTag
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2/**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 *
17 * @file
18 */
19
20declare( strict_types=1 );
21
22namespace MediaWiki\Extension\WikiSEO\Generator\Plugins;
23
24/**
25 * Twitter metadata generator
26 *
27 * @package MediaWiki\Extension\WikiSEO\Generator\Plugins
28 */
29class Twitter extends OpenGraph {
30    /**
31     * Twitter constructor.
32     * Updates some tag name conversions
33     */
34    public function __construct() {
35        $this->tags[] = 'twitter_site';
36        $this->tags[] = 'twitter_card';
37
38        $this->conversions = array_merge(
39            $this->conversions, [
40                'twitter_site' => 'twitter:site',
41                'twitter_card' => 'twitter:card'
42            ]
43        );
44    }
45
46    /**
47     * Add the metadata to the OutputPage
48     *
49     * @return void
50     */
51    public function addMetadata(): void {
52        $this->addTwitterSiteHandleTag();
53
54        parent::addMetadata();
55
56        $twitterCardType = $this->getConfigValue( 'TwitterCardType' ) ?? 'summary_large_image';
57        if ( !empty( $this->metadata['twitter_card'] ) ) {
58            $twitterCardType = $this->metadata['twitter_card'];
59        }
60
61        $this->outputPage->addMeta( 'twitter:card', $twitterCardType );
62    }
63
64    /**
65     * Add the global twitter site handle from $wgTwitterSiteHandle to the meta tags
66     * If $wgTwitterSiteHandle is not null setting the handle via tag or hook is ignored
67     */
68    private function addTwitterSiteHandleTag(): void {
69        $twitterSiteHandle = $this->getConfigValue( 'TwitterSiteHandle' ) ??
70            $this->metadata['twitter_site'] ??
71            null;
72
73        if ( $twitterSiteHandle === null ) {
74            return;
75        }
76
77        unset(
78            $this->metadata['twitter_site'],
79            $this->tags['twitter_site'],
80            $this->conversions['twitter_site']
81        );
82
83        $this->outputPage->addMeta( 'twitter:site', $twitterSiteHandle );
84    }
85}