mediawiki-extensions-Cookie.../includes/CookieWarning.hooks.php
Florian bc100e294d Show CookieWarning for mobile users and save setting to user
Enable the CookieWarning to be visible on a mobile device (bottom, instead of top).

Instead of only rely on cookies to save, that a user accepted that we use cookies,
save it as a user preference, too.

Change-Id: Ib03d5eafd4392d14315115c158b547b9e26a173c
2015-08-16 11:49:05 +02:00

71 lines
1.8 KiB
PHP

<?php
class CookieWarningHooks {
public static function onSkinTemplateOutputPageBeforeExec( SkinTemplate &$sk, &$tpl ) {
if ( self::showWarning( $sk ) ) {
$tpl->data['headelement'] .= Html::openElement(
'div',
array( 'class' => 'mw-cookiewarning-container' )
) .
Html::openElement(
'div',
array( 'class' => 'mw-cookiewarning-text' )
) .
Html::element(
'span',
array(),
$sk->msg( 'cookiewarning-info' )->text()
) .
Html::element(
'a',
array( 'href' => 'https://www.droidwiki.de/DroidWiki:Impressum#Verwendung_von_Cookies' ),
'Mehr Informationen'
) .
Html::element(
'a',
array( 'class' => 'mw-cookiewarning-dismiss' ),
'OK'
) .
Html::closeElement( 'div' ) .
Html::closeElement( 'div' );
}
}
public static function onBeforePageDisplay( OutputPage $out ) {
if ( !$out->getRequest()->getCookie( 'cookiewarning_dismissed' ) ) {
$out->addModuleStyles( array( 'ext.CookieWarning.styles' ) );
$out->addModules( array( 'ext.CookieWarning' ) );
}
}
private static function showWarning( IContextSource $context ) {
$user = $context->getUser();
$conf = ConfigFactory::getDefaultInstance()->makeConfig( 'cookiewarning' );
if (
$conf->get( 'CookieWarningEnabled' ) &&
!$user->getBoolOption( 'cookiewarning_dismissed', false ) &&
!$context->getRequest()->getCookie( 'cookiewarning_dismissed' )
) {
return true;
}
return false;
}
/**
* GetPreferences hook handler
*
* @see https://www.mediawiki.org/wiki/Manual:Hooks/GetPreferences
*
* @param User $user
* @param array $defaultPreferences
* @return bool
*/
public static function onGetPreferences( User $user, &$defaultPreferences ) {
$defaultPreferences['cookiewarning_dismissed'] = array(
'type' => 'api',
'default' => '0',
);
return true;
}
}