Skip to content

Commit 4089f27

Browse files
authored
Add mastodon sharer with instance popup (#808)
1 parent 98ed902 commit 4089f27

File tree

11 files changed

+327
-72
lines changed

11 files changed

+327
-72
lines changed

assets/js/frontend.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ jQuery( function () {
3333

3434
jQuery( '.show-search' ).on( 'click', function () {
3535
jQuery( '.topmenu .search input' ).toggleClass( 'active' );
36-
jQuery( '.topmenu .search input' ).focus();
36+
jQuery( '.topmenu .search input' ).trigger( 'focus' );
3737
} );
3838

3939
jQuery( '.show-contrast' ).on( 'click', function () {
@@ -220,4 +220,5 @@ jQuery( function () {
220220
}
221221
} );
222222
} );
223+
223224
/* eslint-enable no-undef */

assets/js/mastodon.js

+140
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/* eslint-disable no-undef */
2+
'use strict';
3+
4+
// based on https://github.com/Aly-ve/Mastodon-share-button
5+
6+
const COOKIE_NAME = 'mastodon-instance-address';
7+
const URL_REGEX = /^(https?:\/\/)?([\da-z.-]+)\.([a-z.]{2,6})([\/\w .-]*)*\/?$/;
8+
9+
const msbConfig = {
10+
openModal() {
11+
jQuery( '#mastodonShareModal' ).modal( 'show' );
12+
},
13+
closeModal() {
14+
jQuery( '#mastodonShareModal' ).modal( 'hide' );
15+
},
16+
addressFieldSelector: '#msb-address',
17+
buttonModalSelector: '#msb-share',
18+
memorizeFieldId: 'msb-memorize-instance',
19+
};
20+
21+
/* Mastodon Share Modal */
22+
jQuery( '#mastodonShareModal' ).on( 'shown.bs.modal', function () {
23+
jQuery( '#msb-address' ).trigger( 'focus' );
24+
} );
25+
26+
function msbShareButtonAction( name, target ) {
27+
let msbInstanceAddress = '';
28+
29+
msbInstanceAddress = msbGetCookie( 'mastodon-instance-address' );
30+
if ( msbInstanceAddress.length > 0 ) {
31+
window.open(
32+
`${ msbInstanceAddress }/share?text=${ name }%20${ target }`,
33+
`__blank`
34+
);
35+
} else if (
36+
msbConfig &&
37+
msbConfig.openModal &&
38+
msbConfig.addressFieldSelector
39+
) {
40+
if ( document.querySelector( msbConfig.buttonModalSelector ) ) {
41+
const bms = document.querySelector( msbConfig.buttonModalSelector );
42+
bms.data = { target, name };
43+
bms.addEventListener( 'click', () => msbOnShare(), false );
44+
}
45+
msbConfig.openModal( name, target );
46+
}
47+
}
48+
49+
function msbOnShare( _name, _target ) {
50+
if (
51+
msbConfig &&
52+
msbConfig.addressFieldSelector &&
53+
msbConfig.buttonModalSelector
54+
) {
55+
const name = !! _name
56+
? _name
57+
: document.querySelector( msbConfig.buttonModalSelector ).data.name;
58+
const target = !! _target
59+
? _target
60+
: document.querySelector( msbConfig.buttonModalSelector ).data
61+
.target;
62+
let msbInstanceAddress = document.querySelector(
63+
`${ msbConfig.addressFieldSelector }`
64+
).value;
65+
66+
if ( ! msbInstanceAddress.startsWith( 'http' ) ) {
67+
msbInstanceAddress = 'https://' + msbInstanceAddress;
68+
}
69+
if ( msbInstanceAddress.match( URL_REGEX ) ) {
70+
if ( msbConfig.memorizeFieldId ) {
71+
const msbMemorizeIsChecked = document.querySelector(
72+
`#${ msbConfig.memorizeFieldId }`
73+
).checked;
74+
if (
75+
msbConfig.memorizeFieldId &&
76+
! msbGetCookie( COOKIE_NAME ).length > 0 &&
77+
msbMemorizeIsChecked
78+
) {
79+
msbSetCookie( COOKIE_NAME, msbInstanceAddress, 7 );
80+
}
81+
}
82+
83+
window.open(
84+
`${ msbInstanceAddress }/share?text=${ name }%20${ target }`,
85+
`__blank`
86+
);
87+
if ( msbConfig && msbConfig.openModal && msbConfig.closeModal ) {
88+
msbConfig.closeModal();
89+
}
90+
}
91+
}
92+
}
93+
94+
function msbGetCookie( cname ) {
95+
const name = cname + '=';
96+
const ca = document.cookie.split( ';' );
97+
for ( let i = 0; i < ca.length; i++ ) {
98+
let c = ca[ i ];
99+
while ( c.charAt( 0 ) === ' ' ) {
100+
c = c.substring( 1 );
101+
}
102+
if ( c.indexOf( name ) === 0 ) {
103+
return c.substring( name.length, c.length );
104+
}
105+
}
106+
return '';
107+
}
108+
109+
function msbSetCookie( name, value, days ) {
110+
const d = new Date();
111+
d.setTime( d.getTime() + days * 86400000 );
112+
const expires = 'expires=' + d.toUTCString();
113+
document.cookie = `${ name }=${ value }; ${ expires }; path=/`;
114+
}
115+
116+
( function () {
117+
const msbButtons = document.querySelectorAll( '.mastodon-share-button' );
118+
119+
for ( let i = 0; i < msbButtons.length; i++ ) {
120+
( function ( j ) {
121+
const msbTarget = msbButtons[ j ].dataset.target;
122+
let msbName = msbButtons[ j ].dataset.name;
123+
124+
// Replace hashtab by html code
125+
msbName = msbName.replace( /#/g, '%23' );
126+
127+
/**
128+
* Set the listener in each button
129+
*/
130+
msbButtons[ j ].addEventListener(
131+
'click',
132+
() => {
133+
msbShareButtonAction( msbName, msbTarget );
134+
},
135+
true
136+
);
137+
} )( i );
138+
}
139+
} )();
140+
/* eslint-enable no-undef */

functions/options/class-sunflowersocialmediasettingspage.php

+9
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,15 @@ public function sunflower_social_media_page_init(): void {
127127
array( 'sunflower_sharer_whatsapp', __( 'WhatsApp', 'sunflower' ) )
128128
);
129129

130+
add_settings_field(
131+
'sunflower_sharer_mastodon',
132+
__( 'Mastodon', 'sunflower' ),
133+
$this->sunflower_checkbox_callback( ... ),
134+
'sunflower-setting-social-media-options',
135+
'sunflower_social_media_sharers',
136+
array( 'sunflower_sharer_mastodon', __( 'Mastodon', 'sunflower' ) )
137+
);
138+
130139
add_settings_field(
131140
'sunflower_sharer_mail',
132141
__( 'mail', 'sunflower' ),

functions/s.php

+10-9
Original file line numberDiff line numberDiff line change
@@ -136,17 +136,9 @@ function sunflower_scripts() {
136136
wp_enqueue_script( 'comment-reply' );
137137
}
138138

139-
wp_enqueue_script(
140-
'popper',
141-
get_template_directory_uri() . '/assets/vndr/@popperjs/core/dist/umd/popper.min.js',
142-
array(),
143-
SUNFLOWER_VERSION,
144-
true
145-
);
146-
147139
wp_enqueue_script(
148140
'bootstrap',
149-
get_template_directory_uri() . '/assets/vndr/bootstrap/dist/js/bootstrap.min.js',
141+
get_template_directory_uri() . '/assets/vndr/bootstrap/dist/js/bootstrap.bundle.min.js',
150142
array( 'jquery' ),
151143
SUNFLOWER_VERSION,
152144
true
@@ -201,6 +193,15 @@ function sunflower_scripts() {
201193
wp_enqueue_style( 'lightbox', get_template_directory_uri() . '/assets/vndr/lightbox2/dist/css/lightbox.min.css', array(), '4.3.0' );
202194
wp_enqueue_script( 'lightbox', get_template_directory_uri() . '/assets/vndr/lightbox2/dist/js/lightbox.min.js', array( 'jquery' ), '3.3.0', true );
203195
wp_enqueue_script( 'masonry', get_template_directory_uri() . '/assets/vndr/masonry-layout/dist/masonry.pkgd.min.js', array( 'masorny' ), '4.2.2', true );
196+
if ( sunflower_get_setting( 'sunflower_sharer_mastodon' ) ) {
197+
wp_enqueue_script(
198+
'mastodon',
199+
get_template_directory_uri() . '/assets/js/mastodon.js',
200+
null,
201+
SUNFLOWER_VERSION,
202+
true
203+
);
204+
}
204205
}
205206

206207
add_action( 'wp_enqueue_scripts', 'sunflower_scripts' );

inc/template-tags.php

+17
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,23 @@ function sunflower_the_social_media_sharers() {
129129
);
130130
}
131131

132+
if ( sunflower_get_setting( 'sunflower_sharer_mastodon' ) ) {
133+
$sharer[] = sprintf(
134+
'<div
135+
class="mastodon-share-button sharer"
136+
data-target="%1$s"
137+
data-name="%2$s"
138+
data-buttonstyle="fab fa-mastodon"
139+
data-text="%3$s"
140+
title="%3$s"
141+
>%4$s</div>',
142+
get_permalink(),
143+
rawurlencode( (string) get_the_title() ),
144+
__( 'Share on Mastodon ', 'sunflower' ),
145+
'<i class="fab fa-mastodon"></i>',
146+
);
147+
}
148+
132149
if ( sunflower_get_setting( 'sunflower_sharer_mail' ) ) {
133150
$sharer[] = sprintf(
134151
'<a href="MAILTO:?subject=%s&body=%s" target="_blank" title="%s" class="sharer"><i class="fas fa-envelope"></i></a>',

languages/de_DE.mo

380 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)