Skip to content
This repository was archived by the owner on Jun 30, 2024. It is now read-only.

Commit 992ceeb

Browse files
committed
Add support for AddThis
1 parent 96b0637 commit 992ceeb

7 files changed

+134
-49
lines changed

extension.json

+4
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@
107107
"AchievementBadgesFacebookAppId": {
108108
"value": false,
109109
"description": "Facebook App id for SNS sharing."
110+
},
111+
"AchievementBadgesAddThisId": {
112+
"value": false,
113+
"description": "AddThis id for SNS sharing. it could include tool id in a form of array has keys 'pub' and 'tool'."
110114
}
111115
},
112116
"MessagesDirs": {

includes/Constants.php

+3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ final class Constants {
2626
/** @var string */
2727
public const CONFIG_KEY_FACEBOOK_APP_ID = 'AchievementBadgesFacebookAppId';
2828

29+
/** @var string */
30+
public const CONFIG_KEY_ADD_THIS_ID = 'AchievementBadgesAddThisId';
31+
2932
/** @var string */
3033
public const PREF_KEY_ACHIEVEMENT_ENABLE = 'achievementbadges-beta-feature-achievement-enable';
3134

includes/HookHandler/Main.php

+7-3
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,13 @@ public static function onBeforeEchoEventInsert( EchoEvent $event ) {
149149
* @inheritDoc
150150
*/
151151
public function onResourceLoaderGetConfigVars( array &$vars, $skin, Config $config ) : void {
152-
$vars['wg' . Constants::CONFIG_KEY_FACEBOOK_APP_ID] = MediaWikiServices::getInstance()
153-
->getMainConfig()
154-
->get( Constants::CONFIG_KEY_FACEBOOK_APP_ID );
152+
$config = MediaWikiServices::getInstance()->getMainConfig();
153+
$addThisId = $config->get( Constants::CONFIG_KEY_ADD_THIS_ID );
154+
if ( $addThisId ) {
155+
$vars['wg' . Constants::CONFIG_KEY_ADD_THIS_ID] = $addThisId;
156+
} else {
157+
$vars['wg' . Constants::CONFIG_KEY_FACEBOOK_APP_ID] = $config->get( Constants::CONFIG_KEY_FACEBOOK_APP_ID );
158+
}
155159
}
156160

157161
/**

includes/Special/SpecialShareAchievement.php

+57-15
Original file line numberDiff line numberDiff line change
@@ -119,17 +119,21 @@ public function execute( $subPage ) {
119119
$out->addWikiTextAsInterface( $this->msg( 'special-shareachievement-invalid' )->parse() );
120120
return;
121121
}
122-
$data = array_merge( $data, $this->getSnsShareData() );
122+
123+
$data['text-share-header'] = $this->msg( 'special-shareachievement-header-share',
124+
$this->obtainer->getName() );
125+
$data['data-add-this'] = $this->getAddThisData();
126+
if ( !$data['data-add-this'] ) {
127+
$data['data-share'] = $this->getSnsShareData();
128+
}
123129

124130
$betaConfigEnabled = $config->get( Constants::CONFIG_KEY_ENABLE_BETA_FEATURE );
125131
$userBetaEnabled = $betaConfigEnabled && BetaFeatures::isFeatureEnabled( $viewer,
126132
Constants::PREF_KEY_ACHIEVEMENT_ENABLE );
127133
if ( $viewer->isAnon() ) {
128-
$data['has-suggestion'] = true;
129134
$data['text-suggestion'] = $this->msg( 'special-shareachievement-suggestion-sign-up',
130135
$viewer->getName() )->parse();
131136
} elseif ( $betaConfigEnabled && !$userBetaEnabled ) {
132-
$data['has-suggestion'] = true;
133137
$data['text-suggestion'] = $this->msg( 'special-shareachievement-suggestion-beta',
134138
$viewer->getName() )->parse();
135139
}
@@ -186,16 +190,46 @@ private function getBadgeData() {
186190
];
187191
}
188192

193+
/** @return array|null */
194+
private function getAddThisData() {
195+
$config = $this->getConfig()->get( Constants::CONFIG_KEY_ADD_THIS_ID );
196+
if ( !$config ) {
197+
return null;
198+
}
199+
200+
$data = [];
201+
$classes = [ 'addthis_inline_share_toolbox' ];
202+
if ( is_array( $config ) ) {
203+
if ( isset( $config['tool'] ) ) {
204+
$classes[] = 'addthis_inline_share_toolbox_' . $config['tool'];
205+
}
206+
$data['text-pub-id'] = $config['pub'];
207+
} else {
208+
$data['text-pub-id'] = $config;
209+
}
210+
211+
$data['text-class'] = implode( ' ', $classes );
212+
$data['text-url-for-share'] = $this->getUrlForShare();
213+
214+
$obtainer = $this->obtainer;
215+
$viewer = $this->viewer;
216+
$tweet = ( $obtainer == $viewer ) ? 'special-shareachievement-tweet'
217+
: 'special-shareachievement-tweet-viewer';
218+
$tweet = $this->msg( $tweet )
219+
->plaintextParams( $obtainer->getName() )
220+
->plaintextParams( $this->achvNameMsg->text() )
221+
->plaintextParams( '' )
222+
->parse();
223+
$data['text-tweet'] = trim( $tweet );
224+
return $data;
225+
}
226+
189227
/** @return array */
190228
private function getSnsShareData() {
191229
$config = $this->getConfig();
192230
$obtainer = $this->obtainer;
193231

194-
// Use English title to avoid very long url which is build by urlencode()
195-
$titleText = NamespaceInfo::CANONICAL_NAMES[NS_SPECIAL] . ':' . self::PAGE_NAME . '/' . $this->base64subPage;
196-
$articlePath = $config->get( 'ArticlePath' );
197-
$localUrl = str_replace( '$1', $titleText, $articlePath );
198-
$titleUrl = wfExpandUrl( $localUrl );
232+
$titleUrl = $this->getUrlForShare();
199233

200234
$share = [];
201235
$facebookAppId = $config->get( Constants::CONFIG_KEY_FACEBOOK_APP_ID );
@@ -215,21 +249,29 @@ private function getSnsShareData() {
215249
: 'special-shareachievement-tweet-viewer';
216250

217251
$tweet = $this->msg( $tweet );
218-
$tweet = $tweet->plaintextParams( $this->obtainer->getName() )
252+
$tweet = $tweet->plaintextParams( $obtainer->getName() )
219253
->plaintextParams( $this->achvNameMsg->text() )
220-
->plaintextParams( $titleUrl );
254+
->plaintextParams( $titleUrl )
255+
->parse();
221256
$tweetUrl = 'https://twitter.com/intent/tweet?text=' . urlencode( $tweet );
222257
$share[] = [
223258
'text-id' => 'share-achievement-twitter',
224259
'text-text' => $this->msg( 'special-shareachievement-item-twitter' ),
225260
'text-url' => $tweetUrl,
226261
];
227262

228-
return [
229-
'text-share-header' => $this->msg( 'special-shareachievement-header-share',
230-
$this->obtainer->getName() ),
231-
'data-share' => $share
232-
];
263+
return $share;
264+
}
265+
266+
/**
267+
* Returns english url to avoid very long url which is built by urlencode() with other languages than english.
268+
* @return string
269+
*/
270+
private function getUrlForShare() {
271+
$titleText = NamespaceInfo::CANONICAL_NAMES[NS_SPECIAL] . ':' . self::PAGE_NAME . '/' . $this->base64subPage;
272+
$articlePath = $this->getConfig()->get( 'ArticlePath' );
273+
$localUrl = str_replace( '$1', $titleText, $articlePath );
274+
return wfExpandUrl( $localUrl );
233275
}
234276

235277
private function addMeta() {

includes/templates/SpecialShareAchievement.mustache

+10-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,15 @@
33
<img class="achievement-icon" src="{{text-icon}}" alt="">
44
<div class="achievement-description">{{{text-description}}}</div>
55
<h3 class="share-achievement">{{text-share-header}}</h2>
6+
{{^data-add-this}}
67
<ul class="share-media">
7-
{{#data-share}}<li><a id="{{text-id}}" href="{{text-url}}" target="_blank"><span>{{text-text}}</span></a></li>{{/data-share}}
8+
{{#data-share}}<li>
9+
<a id="{{text-id}}" href="{{text-url}}" target="_blank"><span>{{text-text}}</span></a>
10+
</li>{{/data-share}}
811
</ul>
9-
{{#has-suggestion}}<div class="achievement-suggestion">{{{text-suggestion}}}</div>{{/has-suggestion}}
12+
{{/data-add-this}}
13+
{{#data-add-this}}
14+
<div class="{{text-class}}" data-url-for-share="{{text-url-for-share}}" data-tweet="{{text-tweet}}"></div>
15+
<script type="text/javascript" src="//s7.addthis.com/js/300/addthis_widget.js#pubid={{text-pub-id}}"></script>
16+
{{/data-add-this}}
17+
{{#text-suggestion}}<div class="achievement-suggestion">{{{.}}}</div>{{/text-suggestion}}

modules/shareachievement.js

+49-29
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,56 @@
11
(function () {
22
'use strict';
33

4-
// https://developers.facebook.com/docs/plugins/share-button/
5-
// prettier-ignore
6-
(function(d, s, id) {
7-
var js, fjs = d.getElementsByTagName(s)[0];
8-
if (d.getElementById(id)) return;
9-
js = d.createElement(s); js.id = id;
10-
js.src = "https://connect.facebook.net/en_US/sdk.js#xfbml=1&version=v3.0";
11-
fjs.parentNode.insertBefore(js, fjs);
4+
var addThisId = mw.config.get('wgAchievementBadgesAddThisId');
5+
if (addThisId) {
6+
addthis_config = addthis_config || {};
7+
addthis_config['ui_language'] = mw.config.get('wgUserLanguage');
8+
addthis_share = addthis_share || {};
9+
var data = window.document.getElementsByClassName(
10+
'addthis_inline_share_toolbox'
11+
)[0].dataset;
12+
var url = data['urlForShare'];
13+
var tweet = data['tweet'];
14+
addthis_share = {
15+
url: url,
16+
passthrough: {
17+
twitter: {
18+
text: tweet,
19+
},
20+
},
21+
};
22+
} else {
23+
// https://developers.facebook.com/docs/plugins/share-button/
24+
// prettier-ignore
25+
(function(d, s, id) {
26+
var js, fjs = d.getElementsByTagName(s)[0];
27+
if (d.getElementById(id)) return;
28+
js = d.createElement(s); js.id = id;
29+
js.src = "https://connect.facebook.net/en_US/sdk.js#xfbml=1&version=v3.0";
30+
fjs.parentNode.insertBefore(js, fjs);
1231
}(document, 'script', 'facebook-jssdk'));
1332

14-
// Initialize Facebook SDK
15-
var facebookAppId = mw.config.get('wgAchievementBadgesFacebookAppId');
16-
window.fbAsyncInit = function () {
17-
FB.init({
18-
appId: facebookAppId,
19-
status: true,
20-
xfbml: true,
21-
version: 'v2.7',
22-
});
23-
};
33+
// Initialize Facebook SDK
34+
var facebookAppId = mw.config.get('wgAchievementBadgesFacebookAppId');
35+
window.fbAsyncInit = function () {
36+
FB.init({
37+
appId: facebookAppId,
38+
status: true,
39+
xfbml: true,
40+
version: 'v2.7',
41+
});
42+
};
2443

25-
var button = window.document.getElementById('share-achievement-facebook');
26-
button.addEventListener('click', function (e) {
27-
FB.ui(
28-
{
29-
method: 'share',
30-
href: window.location.href,
31-
},
32-
function (response) {}
33-
);
34-
e.preventDefault();
35-
});
44+
var button = window.document.getElementById('share-achievement-facebook');
45+
button.addEventListener('click', function (e) {
46+
FB.ui(
47+
{
48+
method: 'share',
49+
href: window.location.href,
50+
},
51+
function (response) {}
52+
);
53+
e.preventDefault();
54+
});
55+
}
3656
})();

modules/styles/spacial-shareachievement.less

+4
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@
5858
}
5959
}
6060

61+
.addthis_inline_share_toolbox {
62+
text-align: center;
63+
}
64+
6165
.achievement-suggestion {
6266
margin-top: 3em;
6367
text-align: center;

0 commit comments

Comments
 (0)