This repository has been archived by the owner on Dec 2, 2023. It is now read-only.
forked from MoeMoeFish/MoegirlAD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MoegirlAD.hooks.php
123 lines (97 loc) · 3.61 KB
/
MoegirlAD.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
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
<?php
/*
* Static class for hooks handle by MoegirlAD.
*
* @file MoegirlAD.hooks.php
*
* @license Apache-2.0+
* @author Fish Thirteen < [email protected] >
* @author Baskice
* @author Bingxing Wang (The Little Moe New LLC) <[email protected]>
*
*/
final class MoegirlADHooks {
public static function BeforePageDisplay( OutputPage &$out, Skin &$skin ) {
global $wgMoegirlADHeaderscriptDesktop;
$isMobileView = MoegirlADHooks::isMobileView();
if (!$isMobileView) {
$out->addHeadItem('ADScriptforDesktop',$wgMoegirlADHeaderscriptDesktop);
}
}
public static function BeforePageDisplayMobile( OutputPage &$out, Skin &$skin ) {
global $wgMoegirlADHeaderscriptMobile;
$out->addHeadItem('ADScriptforMobile',$wgMoegirlADHeaderscriptMobile);
}
public static function onSkinAfterContent(&$data, $skin) {
global $wgMoegirlADBottomADCode;
if (MoegirlADHooks::shouldShowADs()) {
$data .= $wgMoegirlADBottomADCode;
}
return true;
}
public static function onSiteNoticeAfter(&$siteNotice, $skin) {
global $wgMoegirlADTopADCode, $wgMoegirlADMobileTopADCode;
$isMobileView = MoegirlADHooks::isMobileView();
if (MoegirlADHooks::shouldShowADs()) {
// Determine the availability: If MobileFrontend exists and mobile view is enabled, present mobile ad
if ($isMobileView) {
$siteNotice = $wgMoegirlADMobileTopADCode;
} else {
$siteNotice = $wgMoegirlADTopADCode . $siteNotice;
}
} else if ($isMobileView) {
// Fix by case: Since MobileFrontend will display SiteNotice for some users, clear site notice if we are in mobile view.
$siteNotice = '';
}
return true;
}
public static function onSkinAfterBottomScripts( $skin, &$text ) {
global $wgMoegirlADFooterEnabled, $wgMoegirlADFooterADCode;
if (MoegirlADHooks::shouldShowADs() && $wgMoegirlADFooterEnabled) {
$text .= $wgMoegirlADFooterADCode;
}
return true;
}
public static function onSkinBuildSidebar( Skin $skin, &$bar ) {
global $wgMoegirlADSideBarEnabled, $wgMoegirlADSideBarADName, $wgMoegirlADSideBarADCode;
if (MoegirlADHooks::shouldShowADs() && $wgMoegirlADSideBarEnabled) {
$bar[$wgMoegirlADSideBarADName] = $wgMoegirlADSideBarADCode;
}
return true;
}
/**
* @param array $ids
*/
public static function onGetDoubleUnderscoreIDs( array &$ids ) {
$ids[] = 'noad';
}
/**
* Check if the advertice should be display
*
* @return boolean
*/
public static function shouldShowADs() {
global $wgMoegirlADEnabled, $wgMoegirlADEditCountQualification;
if (!$wgMoegirlADEnabled) return false;
$currentReqContext = RequestContext::getMain();
$currentTitle = $currentReqContext->getTitle();
// Special namespace has NSID -1
$isSpecialPage = $currentTitle->getNamespace() === -1;
// Ignore advertisements for all special pages on mobile device
if (MoegirlADHooks::isMobileView() && $isSpecialPage) return false;
if (!$isSpecialPage) {
// Ignore advertisements if page attribute is set
$pageProps = PageProps::getInstance();
// See the magic word definition
$suppressAdAttributes = $pageProps->getProperties($currentTitle, 'noad');
// Suppress advertisement if attribute is set
if (!empty($suppressAdAttributes)) return false;
}
// Show advertisements for users that have given edits (default: 5) or less / guests
$currentUser = $currentReqContext->getUser();
return !( $currentUser->getEditCount() > $wgMoegirlADEditCountQualification);
}
public static function isMobileView() {
return class_exists('MobileContext') && MobileContext::singleton()->shouldDisplayMobileView();
}
}