2022-11-25 21:34:46 +01:00
|
|
|
<?php
|
|
|
|
/*
|
2022-11-29 22:17:34 +01:00
|
|
|
Plugin Name: Include Mastodon Feed
|
|
|
|
Plugin URI: https://www.wolfgang.lol/code/include-include-mastodon-feed-wordpress-plugin
|
|
|
|
Description: Plugin providing [include-mastodon-feed] shortcode
|
2022-12-02 11:22:04 +01:00
|
|
|
Version: 1.0.1
|
2022-11-25 21:34:46 +01:00
|
|
|
Author: wolfgang.lol
|
|
|
|
Author URI: https://www.wolfgang.lol
|
|
|
|
*/
|
|
|
|
|
|
|
|
// load user config if available
|
2022-12-02 11:22:04 +01:00
|
|
|
if(file_exists( plugin_dir_path( __FILE__ ) . DIRECTORY_SEPARATOR . 'config.php' )) {
|
|
|
|
require_once( plugin_dir_path( __FILE__ ) . DIRECTORY_SEPARATOR . 'config.php' );
|
2022-11-25 21:34:46 +01:00
|
|
|
}
|
2022-11-25 23:25:21 +01:00
|
|
|
// set defaults
|
|
|
|
$constants = [
|
|
|
|
[
|
2022-11-29 22:17:34 +01:00
|
|
|
'key' => 'INCLUDE_MASTODON_FEED_DEBUG',
|
2022-11-25 23:25:21 +01:00
|
|
|
'value' => false,
|
|
|
|
],
|
|
|
|
[
|
2022-11-29 22:17:34 +01:00
|
|
|
'key' => 'INCLUDE_MASTODON_FEED_DEFAULT_INSTANCE',
|
|
|
|
'value' => false,
|
2022-11-25 23:25:21 +01:00
|
|
|
],
|
|
|
|
[
|
2022-11-29 22:17:34 +01:00
|
|
|
'key' => 'INCLUDE_MASTODON_FEED_STYLE_BG_LIGHT_COLOR',
|
2022-11-25 23:25:21 +01:00
|
|
|
'value' => 'rgba(100, 100, 100, 0.15)',
|
|
|
|
],
|
|
|
|
[
|
2022-11-29 22:17:34 +01:00
|
|
|
'key' => 'INCLUDE_MASTODON_FEED_STYLE_BG_DARK_COLOR',
|
2022-11-25 23:25:21 +01:00
|
|
|
'value' => 'rgba(155, 155, 155, 0.15)',
|
|
|
|
],
|
|
|
|
[
|
2022-11-29 22:17:34 +01:00
|
|
|
'key' => 'INCLUDE_MASTODON_FEED_STYLE_ACCENT_COLOR',
|
2022-11-25 23:25:21 +01:00
|
|
|
'value' => 'rgb(99, 100, 255)',
|
|
|
|
],
|
|
|
|
[
|
2022-11-29 22:17:34 +01:00
|
|
|
'key' => 'INCLUDE_MASTODON_FEED_STYLE_ACCENT_FONT_COLOR',
|
2022-11-25 23:25:21 +01:00
|
|
|
'value' => 'rgb(255, 255, 255)',
|
|
|
|
],
|
|
|
|
[
|
2022-11-29 22:17:34 +01:00
|
|
|
'key' => 'INCLUDE_MASTODON_FEED_STYLE_BORDER_RADIUS',
|
2022-11-25 23:25:21 +01:00
|
|
|
'value' => '0.25rem',
|
|
|
|
],
|
|
|
|
];
|
|
|
|
foreach($constants as $constant) {
|
|
|
|
if(!defined($constant['key'])) {
|
|
|
|
define($constant['key'], $constant['value']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
unset($constants);
|
2022-11-25 21:34:46 +01:00
|
|
|
|
2022-11-29 22:17:34 +01:00
|
|
|
function include_mastodon_feed_error($msg) {
|
|
|
|
return '[include-mastodon-feed] ' . $msg;
|
2022-11-25 21:34:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-29 22:17:34 +01:00
|
|
|
function include_mastodon_feed_init_styles() {
|
2022-11-25 21:34:46 +01:00
|
|
|
ob_start();
|
|
|
|
?>
|
|
|
|
<style>
|
|
|
|
:root {
|
2022-12-02 11:22:04 +01:00
|
|
|
--include-mastodon-feed-bg-light: <?php echo esc_attr( INCLUDE_MASTODON_FEED_STYLE_BG_LIGHT_COLOR ); ?>;
|
|
|
|
--include-mastodon-feed-bg-dark: <?php echo esc_attr( INCLUDE_MASTODON_FEED_STYLE_BG_DARK_COLOR ); ?>;
|
|
|
|
--include-mastodon-feed-accent-color: <?php echo esc_attr( INCLUDE_MASTODON_FEED_STYLE_ACCENT_COLOR ); ?>;
|
|
|
|
--include-mastodon-feed-accent-font-color: <?php echo esc_attr( INCLUDE_MASTODON_FEED_STYLE_ACCENT_FONT_COLOR ); ?>;
|
|
|
|
--include-mastodon-feed-border-radius: <?php echo esc_attr( INCLUDE_MASTODON_FEED_STYLE_BORDER_RADIUS ); ?>;
|
2022-11-25 21:34:46 +01:00
|
|
|
}
|
|
|
|
|
2022-11-29 22:17:34 +01:00
|
|
|
.include-mastodon-feed .status {
|
2022-11-25 21:34:46 +01:00
|
|
|
margin: 0.5rem 0 1.5rem;
|
2022-11-29 22:17:34 +01:00
|
|
|
border-radius: var(--include-mastodon-feed-border-radius);
|
2022-11-25 21:34:46 +01:00
|
|
|
padding: 0.5rem;
|
2022-11-29 22:17:34 +01:00
|
|
|
background: var(--include-mastodon-feed-bg-light);
|
2022-11-25 21:34:46 +01:00
|
|
|
}
|
2022-11-29 22:17:34 +01:00
|
|
|
.include-mastodon-feed .status a {
|
|
|
|
color: var(--include-mastodon-feed-accent-color);
|
2022-11-25 21:34:46 +01:00
|
|
|
text-decoration: none;
|
|
|
|
}
|
2022-11-29 22:17:34 +01:00
|
|
|
.include-mastodon-feed .status a:hover {
|
2022-11-25 21:34:46 +01:00
|
|
|
text-decoration: underline;
|
|
|
|
}
|
2022-11-29 22:17:34 +01:00
|
|
|
.include-mastodon-feed .account .permalink {
|
2022-11-25 21:34:46 +01:00
|
|
|
float: right;
|
|
|
|
}
|
2022-11-29 22:17:34 +01:00
|
|
|
.include-mastodon-feed .avatar {
|
2022-11-25 21:34:46 +01:00
|
|
|
height: 1.25rem;
|
2022-11-29 22:17:34 +01:00
|
|
|
border-radius: var(--include-mastodon-feed-border-radius);
|
2022-11-25 21:34:46 +01:00
|
|
|
vertical-align: top;
|
|
|
|
}
|
2022-11-29 22:17:34 +01:00
|
|
|
.include-mastodon-feed .account {
|
2022-11-25 21:34:46 +01:00
|
|
|
font-size: 0.8rem;
|
|
|
|
}
|
2022-11-29 22:17:34 +01:00
|
|
|
.include-mastodon-feed .account a {
|
2022-11-25 21:34:46 +01:00
|
|
|
display: inline-block;
|
|
|
|
}
|
2022-11-29 22:17:34 +01:00
|
|
|
.include-mastodon-feed .account .booster {
|
2022-11-25 21:34:46 +01:00
|
|
|
float: right;
|
|
|
|
font-style: italic;
|
|
|
|
}
|
2022-11-29 22:17:34 +01:00
|
|
|
.include-mastodon-feed .boosted .account a:nth-child(2),
|
|
|
|
.include-mastodon-feed .contentWarning a {
|
|
|
|
border-radius: var(--include-mastodon-feed-border-radius);
|
2022-11-25 21:34:46 +01:00
|
|
|
padding: 0.15rem 0.5rem;
|
2022-11-29 22:17:34 +01:00
|
|
|
background: var(--include-mastodon-feed-accent-color);
|
|
|
|
color: var(--include-mastodon-feed-accent-font-color);
|
2022-11-25 21:34:46 +01:00
|
|
|
}
|
2022-11-29 22:17:34 +01:00
|
|
|
.include-mastodon-feed .boosted .account a:nth-child(2):hover,
|
|
|
|
.include-mastodon-feed .contentWarning a:hover {
|
|
|
|
border-radius: var(--include-mastodon-feed-border-radius);
|
2022-11-25 21:34:46 +01:00
|
|
|
padding: 0.15rem 0.5rem;
|
2022-11-29 22:17:34 +01:00
|
|
|
background: var(--include-mastodon-feed-accent-font-color);
|
|
|
|
color: var(--include-mastodon-feed-accent-color);
|
2022-11-25 21:34:46 +01:00
|
|
|
text-decoration: none;
|
|
|
|
}
|
2022-11-29 22:17:34 +01:00
|
|
|
.include-mastodon-feed .contentWrapper.boosted {
|
2022-11-25 21:34:46 +01:00
|
|
|
margin: 0.5rem 0;
|
|
|
|
padding: 0.5rem;
|
2022-11-29 22:17:34 +01:00
|
|
|
background: var(--include-mastodon-feed-bg-light);
|
2022-11-25 21:34:46 +01:00
|
|
|
}
|
2022-11-29 22:17:34 +01:00
|
|
|
.include-mastodon-feed .contentWarning {
|
2022-11-25 21:34:46 +01:00
|
|
|
text-align: center;
|
|
|
|
margin: 1rem;
|
|
|
|
padding: 1rem;
|
|
|
|
}
|
2022-11-29 22:17:34 +01:00
|
|
|
.include-mastodon-feed .contentWarning .title {
|
2022-11-25 21:34:46 +01:00
|
|
|
font-weight: bold;
|
|
|
|
}
|
2022-11-29 22:17:34 +01:00
|
|
|
.include-mastodon-feed.content .emoji {
|
2022-11-26 20:13:26 +01:00
|
|
|
height: 1rem;
|
|
|
|
}
|
2022-11-29 22:17:34 +01:00
|
|
|
.include-mastodon-feed .media {
|
2022-11-25 21:34:46 +01:00
|
|
|
display: flex;
|
|
|
|
justify-content: space-around;
|
|
|
|
align-items: center;
|
|
|
|
flex-wrap: wrap;
|
|
|
|
gap: 0.5rem;
|
|
|
|
margin: 1rem;
|
|
|
|
}
|
2022-11-29 22:17:34 +01:00
|
|
|
.include-mastodon-feed .media .image {
|
2022-11-25 21:34:46 +01:00
|
|
|
font-size: 0.8rem;
|
|
|
|
font-weight: bold;
|
|
|
|
text-align: center;
|
|
|
|
flex-basis: calc(50% - 0.5rem);
|
|
|
|
flex-grow: 1;
|
|
|
|
}
|
2022-11-29 22:17:34 +01:00
|
|
|
.include-mastodon-feed .media .image img {
|
|
|
|
border-radius: var(--include-mastodon-feed-border-radius);
|
2022-11-25 21:34:46 +01:00
|
|
|
max-width: 100%;
|
|
|
|
}
|
2022-11-29 22:17:34 +01:00
|
|
|
.include-mastodon-feed .card {
|
|
|
|
border-radius: var(--include-mastodon-feed-border-radius);
|
2022-11-25 21:34:46 +01:00
|
|
|
margin: 1rem 0.5rem;
|
|
|
|
}
|
2022-11-29 22:17:34 +01:00
|
|
|
.include-mastodon-feed .card iframe {
|
|
|
|
border-radius: var(--include-mastodon-feed-border-radius);
|
2022-11-25 21:34:46 +01:00
|
|
|
width: 100%;
|
|
|
|
height: 100%;
|
|
|
|
aspect-ratio: 2 / 1.25;
|
|
|
|
}
|
2022-11-29 22:17:34 +01:00
|
|
|
.include-mastodon-feed .card a {
|
|
|
|
border-radius: var(--include-mastodon-feed-border-radius);
|
2022-11-25 21:34:46 +01:00
|
|
|
display: block;
|
|
|
|
text-decoration: none;
|
|
|
|
color: #000;
|
|
|
|
}
|
2022-11-29 22:17:34 +01:00
|
|
|
.include-mastodon-feed.dark .card a {
|
2022-11-25 23:10:55 +01:00
|
|
|
color: #fff;
|
|
|
|
}
|
2022-11-29 22:17:34 +01:00
|
|
|
.include-mastodon-feed .card a:hover {
|
2022-11-25 21:34:46 +01:00
|
|
|
text-decoration: none;
|
2022-11-29 22:17:34 +01:00
|
|
|
background: var(--include-mastodon-feed-accent-color);
|
|
|
|
color: var(--include-mastodon-feed-accent-font-color);
|
2022-11-25 21:34:46 +01:00
|
|
|
}
|
2022-11-29 22:17:34 +01:00
|
|
|
.include-mastodon-feed .card .meta {
|
|
|
|
background: var(--include-mastodon-feed-bg-light);
|
2022-11-25 21:34:46 +01:00
|
|
|
font-size: 0.8rem;
|
|
|
|
padding: 1rem;
|
|
|
|
}
|
2022-11-29 22:17:34 +01:00
|
|
|
.include-mastodon-feed .card .image {
|
2022-11-25 21:34:46 +01:00
|
|
|
margin-bottom: 0.5rem;
|
|
|
|
text-align: center;
|
|
|
|
}
|
2022-11-29 22:17:34 +01:00
|
|
|
.include-mastodon-feed .card .image img {
|
2022-11-25 21:34:46 +01:00
|
|
|
max-width: 75%;
|
|
|
|
}
|
2022-11-29 22:17:34 +01:00
|
|
|
.include-mastodon-feed .card .title {
|
2022-11-25 21:34:46 +01:00
|
|
|
font-weight: bold;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-11-29 22:17:34 +01:00
|
|
|
.include-mastodon-feed.dark .status,
|
|
|
|
.include-mastodon-feed.dark .contentWrapper.boosted,
|
|
|
|
.include-mastodon-feed.dark .card {
|
|
|
|
background: var(--include-mastodon-feed-bg-dark);
|
2022-11-25 21:34:46 +01:00
|
|
|
}
|
|
|
|
</style>
|
|
|
|
<?php
|
|
|
|
echo ob_get_clean();
|
|
|
|
}
|
2022-11-29 22:17:34 +01:00
|
|
|
add_action('wp_head', 'include_mastodon_feed_init_styles', 7);
|
2022-11-25 21:34:46 +01:00
|
|
|
|
2022-11-29 22:17:34 +01:00
|
|
|
function include_mastodon_feed_init_scripts() {
|
2022-11-25 21:34:46 +01:00
|
|
|
ob_start();
|
|
|
|
?>
|
|
|
|
<script>
|
|
|
|
|
|
|
|
const mastodonFeedCreateElement = function(type, className = null) {
|
|
|
|
let element = document.createElement(type);
|
|
|
|
if(null !== className) {
|
|
|
|
element.className = className;
|
|
|
|
}
|
|
|
|
return element;
|
|
|
|
}
|
|
|
|
|
2022-11-26 19:48:46 +01:00
|
|
|
const mastodonFeedCreateElementAccountLink = function(account) {
|
2022-11-25 21:34:46 +01:00
|
|
|
let accountLinkElem = mastodonFeedCreateElement('a');
|
|
|
|
accountLinkElem.href = account.url;
|
|
|
|
|
|
|
|
let accountImageElem = mastodonFeedCreateElement('img', 'avatar');
|
|
|
|
accountImageElem.src = account.avatar;
|
|
|
|
|
|
|
|
accountLinkElem.appendChild(accountImageElem);
|
2022-11-26 20:13:26 +01:00
|
|
|
// inject emojis
|
|
|
|
let displayName = account.display_name;
|
|
|
|
if(account.emojis.length > 0) {
|
|
|
|
account.emojis.forEach(function(emoji) {
|
|
|
|
displayName = mastodonFeedInjectEmoji(displayName, emoji);
|
|
|
|
});
|
|
|
|
}
|
2022-11-26 20:15:24 +01:00
|
|
|
accountLinkElem.innerHTML += ' ' + displayName;
|
2022-11-25 21:34:46 +01:00
|
|
|
return accountLinkElem;
|
|
|
|
}
|
|
|
|
|
2022-11-26 19:48:46 +01:00
|
|
|
const mastodonFeedCreateElementPermalink = function(status) {
|
|
|
|
let linkElem = mastodonFeedCreateElement('a');
|
|
|
|
linkElem.href = status.url;
|
|
|
|
linkElem.appendChild(document.createTextNode('view on instance'));
|
|
|
|
return linkElem;
|
|
|
|
}
|
|
|
|
|
|
|
|
const mastodonFeedCreateElementMediaAttachments = function(attachments) {
|
|
|
|
let mediaWrapperElem = mastodonFeedCreateElement('div', 'media');
|
|
|
|
for(let mediaIndex = 0; mediaIndex < attachments.length; mediaIndex++) {
|
|
|
|
let media = attachments[mediaIndex];
|
|
|
|
let mediaElem = mastodonFeedCreateElement('div', 'image');
|
|
|
|
if('image' == media.type) {
|
|
|
|
let mediaElemImg = mastodonFeedCreateElement('img');
|
|
|
|
if(null === media.remote_url) {
|
|
|
|
mediaElemImg.src = media.preview_url;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mediaElemImg.src = media.remote_url;
|
|
|
|
}
|
|
|
|
if(null !== media.description) {
|
|
|
|
mediaElemImg.title = media.description;
|
|
|
|
}
|
|
|
|
mediaElem.appendChild(mediaElemImg);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// TODO implement support for other media types
|
|
|
|
// currently only image support implemented
|
|
|
|
mediaElem.innerHTML = 'Stripped ' + media.type + ' - only available on instance<br />';
|
|
|
|
mediaElem.appendChild(permalinkElem);
|
|
|
|
}
|
|
|
|
mediaWrapperElem.appendChild(mediaElem);
|
|
|
|
}
|
|
|
|
return mediaWrapperElem;
|
|
|
|
}
|
|
|
|
|
|
|
|
const mastodonFeedCreateElementPreviewCard = function(card) {
|
|
|
|
let cardElem = mastodonFeedCreateElement('div', 'card');
|
|
|
|
|
|
|
|
if(null === card.html || card.html.length < 1) {
|
|
|
|
let cardElemMeta = mastodonFeedCreateElement('div', 'meta');
|
|
|
|
|
|
|
|
if(null !== card.image) {
|
|
|
|
let cardElemImageWrapper = mastodonFeedCreateElement('div', 'image');
|
|
|
|
let cardElemImage = mastodonFeedCreateElement('img');
|
|
|
|
cardElemImage.src = card.image;
|
|
|
|
cardElemImageWrapper.appendChild(cardElemImage);
|
|
|
|
cardElemMeta.appendChild(cardElemImageWrapper);
|
|
|
|
}
|
|
|
|
|
|
|
|
let cardElemTitle = mastodonFeedCreateElement('div', 'title');
|
|
|
|
cardElemTitle.innerHTML = card.title;
|
|
|
|
cardElemMeta.appendChild(cardElemTitle);
|
|
|
|
|
|
|
|
let cardElemDescription = mastodonFeedCreateElement('div', 'description');
|
|
|
|
cardElemDescription.innerHTML = card.description;
|
|
|
|
cardElemMeta.appendChild(cardElemDescription);
|
|
|
|
|
|
|
|
if(card.url === null) {
|
|
|
|
cardElem.appendChild(cardElemMeta);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
let cardElemLink = mastodonFeedCreateElement('a');
|
|
|
|
cardElemLink.href = card.url;
|
|
|
|
cardElemLink.appendChild(cardElemMeta);
|
|
|
|
cardElem.appendChild(cardElemLink);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
cardElem.innerHTML = card.html;
|
|
|
|
}
|
|
|
|
return cardElem;
|
|
|
|
}
|
|
|
|
|
2022-11-25 21:34:46 +01:00
|
|
|
const mastodonFeedCreateTimeinfo = function(status) {
|
|
|
|
let createdInfo = document.createTextNode(' on ' + new Date(status.created_at).toLocaleDateString('en-US'));
|
|
|
|
createdInfo.textContent += ' ' + new Date(status.created_at).toLocaleTimeString('en-US');
|
|
|
|
if(null !== status.edited_at) {
|
|
|
|
createdInfo.textContent += ' (edited)';
|
|
|
|
}
|
|
|
|
return createdInfo;
|
|
|
|
}
|
|
|
|
|
2022-11-26 20:13:26 +01:00
|
|
|
const mastodonFeedInjectEmoji = function(string, emoji) {
|
|
|
|
return string.replace(':' + emoji.shortcode + ':', '<img class="emoji" src="' + emoji.url + '" title="' + emoji.shortcode + '" />');
|
|
|
|
}
|
|
|
|
|
2022-11-25 21:34:46 +01:00
|
|
|
const mastodonFeedRenderStatuses = function(statuses, rootElem) {
|
|
|
|
for(let i = 0; i < statuses.length; i++) {
|
|
|
|
let status = statuses[i];
|
|
|
|
let isEdited = (null === status.edited_at ? true : false);
|
|
|
|
let isReblog = (null === status.reblog ? false : true);
|
|
|
|
|
|
|
|
let statusElem = mastodonFeedCreateElement('div', 'status');
|
|
|
|
|
|
|
|
// add account meta info
|
|
|
|
let accountElem = mastodonFeedCreateElement('div', 'account');
|
|
|
|
|
|
|
|
if(isReblog) {
|
|
|
|
let boosterElem = mastodonFeedCreateElement('span', 'booster');
|
|
|
|
boosterElem.appendChild(document.createTextNode('boosted 🚀'));
|
|
|
|
accountElem.appendChild(boosterElem);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
let origPermalinkElem = mastodonFeedCreateElement('span', 'permalink');
|
2022-11-26 19:48:46 +01:00
|
|
|
origPermalinkElem.appendChild(mastodonFeedCreateElementPermalink(status));
|
2022-11-25 21:34:46 +01:00
|
|
|
accountElem.appendChild(origPermalinkElem);
|
|
|
|
}
|
2022-11-26 19:48:46 +01:00
|
|
|
accountElem.appendChild(mastodonFeedCreateElementAccountLink(status.account));
|
2022-11-25 21:34:46 +01:00
|
|
|
accountElem.appendChild(mastodonFeedCreateTimeinfo(status));
|
|
|
|
|
|
|
|
statusElem.appendChild(accountElem);
|
|
|
|
|
|
|
|
// prepare content rendering
|
|
|
|
let showStatus = status;
|
|
|
|
if(isReblog) {
|
|
|
|
showStatus = status.reblog;
|
|
|
|
}
|
|
|
|
let contentWrapperElem = mastodonFeedCreateElement('div', 'contentWrapper' + (isReblog ? ' boosted' : ''));
|
|
|
|
let permalinkElem = mastodonFeedCreateElement('span', 'permalink');
|
2022-11-26 19:48:46 +01:00
|
|
|
permalinkElem.appendChild(mastodonFeedCreateElementPermalink(showStatus));
|
2022-11-25 21:34:46 +01:00
|
|
|
|
|
|
|
// add boosted post meta info
|
|
|
|
if(isReblog) {
|
|
|
|
let boostElem = mastodonFeedCreateElement('div', 'account');
|
|
|
|
boostElem.appendChild(permalinkElem);
|
2022-11-26 19:48:46 +01:00
|
|
|
let boostAccountLink = mastodonFeedCreateElementAccountLink(showStatus.account);
|
2022-11-25 21:34:46 +01:00
|
|
|
boostElem.appendChild(boostAccountLink);
|
|
|
|
boostElem.appendChild(mastodonFeedCreateTimeinfo(showStatus));
|
|
|
|
|
|
|
|
contentWrapperElem.appendChild(boostElem);
|
|
|
|
}
|
|
|
|
|
|
|
|
let contentElem = mastodonFeedCreateElement('div', 'content');
|
|
|
|
|
|
|
|
// handle content warnings
|
|
|
|
if(showStatus.sensitive || showStatus.spoiler_text.length > 0) {
|
|
|
|
let cwElem = mastodonFeedCreateElement('div', 'contentWarning');
|
|
|
|
|
|
|
|
if(showStatus.spoiler_text.length > 0) {
|
|
|
|
let cwTitleElem = mastodonFeedCreateElement('div', 'title');
|
|
|
|
cwTitleElem.innerHTML = showStatus.spoiler_text;
|
|
|
|
cwElem.appendChild(cwTitleElem);
|
|
|
|
}
|
|
|
|
|
|
|
|
let cwLinkElem = mastodonFeedCreateElement('a');
|
|
|
|
cwLinkElem.href = '#';
|
|
|
|
cwLinkElem.onclick = function() {
|
|
|
|
this.parentElement.style = 'display: none;';
|
|
|
|
this.parentElement.nextSibling.style = 'display: block;';
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
cwLinkElem.innerHTML = 'Show content';
|
|
|
|
cwElem.appendChild(cwLinkElem);
|
|
|
|
|
|
|
|
contentWrapperElem.appendChild(cwElem);
|
|
|
|
contentElem.style = 'display: none;';
|
|
|
|
}
|
|
|
|
|
|
|
|
// add regular content
|
2022-11-26 20:13:26 +01:00
|
|
|
let renderContent = showStatus.content;
|
|
|
|
// inject emojis
|
|
|
|
if(showStatus.emojis.length > 0) {
|
|
|
|
showStatus.emojis.forEach(function(emoji) {
|
|
|
|
renderContent = mastodonFeedInjectEmoji(renderContent, emoji);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
contentElem.innerHTML += renderContent;
|
2022-11-25 21:34:46 +01:00
|
|
|
|
|
|
|
// handle media attachments
|
|
|
|
if(showStatus.media_attachments.length > 0) {
|
2022-11-26 19:48:46 +01:00
|
|
|
let mediaAttachmentsElem = mastodonFeedCreateElementMediaAttachments(showStatus.media_attachments);
|
|
|
|
contentElem.appendChild(mediaAttachmentsElem);
|
2022-11-25 21:34:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// handle preview card
|
|
|
|
if(showStatus.card != null) {
|
2022-11-26 19:48:46 +01:00
|
|
|
let cardElem = mastodonFeedCreateElementPreviewCard(showStatus.card);
|
2022-11-25 21:34:46 +01:00
|
|
|
contentElem.appendChild(cardElem);
|
|
|
|
}
|
|
|
|
|
|
|
|
contentWrapperElem.appendChild(contentElem);
|
|
|
|
statusElem.appendChild(contentWrapperElem);
|
|
|
|
rootElem.appendChild(statusElem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const mastodonFeedLoad = function(url, elementId) {
|
|
|
|
const xhr = new XMLHttpRequest();
|
|
|
|
xhr.open('GET', url, true);
|
|
|
|
xhr.responseType = 'json';
|
|
|
|
xhr.onload = function() {
|
|
|
|
const statuses = xhr.response;
|
|
|
|
const rootElem = document.getElementById(elementId);
|
|
|
|
rootElem.innerHTML = '';
|
|
|
|
if (xhr.status === 200) {
|
2022-11-29 22:17:34 +01:00
|
|
|
<?php if(true === INCLUDE_MASTODON_FEED_DEBUG) : ?>
|
2022-11-25 21:34:46 +01:00
|
|
|
console.log(xhr.response);
|
|
|
|
<?php endif; ?>
|
|
|
|
mastodonFeedRenderStatuses(statuses, rootElem);
|
|
|
|
}
|
|
|
|
else {
|
2022-11-29 22:17:34 +01:00
|
|
|
<?php if(true === INCLUDE_MASTODON_FEED_DEBUG) : ?>
|
2022-11-25 21:34:46 +01:00
|
|
|
console.log(xhr);
|
|
|
|
<?php endif; ?>
|
|
|
|
rootElem.appendChild(document.createTextNode(xhr.response.error));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
xhr.send();
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
<?php
|
|
|
|
echo ob_get_clean();
|
|
|
|
}
|
2022-11-29 22:17:34 +01:00
|
|
|
add_action('wp_footer', 'include_mastodon_feed_init_scripts');
|
2022-11-25 21:34:46 +01:00
|
|
|
|
2022-11-29 22:17:34 +01:00
|
|
|
function include_mastodon_feed_display_feed($atts) {
|
2022-11-25 21:34:46 +01:00
|
|
|
$atts = shortcode_atts(
|
|
|
|
array(
|
2022-11-29 22:17:34 +01:00
|
|
|
'instance' => false,
|
2022-11-25 21:34:46 +01:00
|
|
|
'account' => false,
|
|
|
|
'loading' => null,
|
|
|
|
'darkmode' => false,
|
|
|
|
), $atts
|
|
|
|
);
|
2022-11-29 22:17:34 +01:00
|
|
|
if(false === $atts['instance']) {
|
|
|
|
return include_mastodon_feed_error('missing configuration: instance');
|
|
|
|
}
|
2022-11-25 21:34:46 +01:00
|
|
|
if(false === $atts['account']) {
|
2022-11-29 22:17:34 +01:00
|
|
|
return include_mastodon_feed_error('missing configuration: account id');
|
2022-11-25 21:34:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$apiUrl = 'https://'.urlencode($atts['instance']).'/api/v1/accounts/'.urlencode($atts['account']).'/statuses';
|
2022-11-29 22:17:34 +01:00
|
|
|
$elemId = uniqid('include-mastodon-feed-');
|
2022-11-25 21:34:46 +01:00
|
|
|
ob_start();
|
|
|
|
?>
|
|
|
|
<script>
|
|
|
|
window.addEventListener("load", () => {
|
2022-12-02 11:22:04 +01:00
|
|
|
mastodonFeedLoad("<?php echo esc_url( $apiUrl ); ?>", "<?php echo esc_js( $elemId ); ?>");
|
2022-11-25 21:34:46 +01:00
|
|
|
});
|
|
|
|
</script>
|
2022-12-02 11:22:04 +01:00
|
|
|
<div class="include-mastodon-feed<?php echo ('true' == $atts['darkmode'] ? ' dark' : ''); ?>" id="<?php echo esc_attr( $elemId ); ?>"><?php echo (null === $atts['loading'] ? 'Loading Mastodon feed...' : esc_html( $atts['loading']) ); ?></div>
|
2022-11-25 21:34:46 +01:00
|
|
|
<?php
|
|
|
|
return ob_get_clean();
|
|
|
|
}
|
2022-11-29 22:17:34 +01:00
|
|
|
add_shortcode('include-mastodon-feed', 'include_mastodon_feed_display_feed');
|