2024-05-23 17:08:09 +00:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
Plugin Name: Post to LiaScript Shortcode Button
|
|
|
|
Description: Ermittelt den Content und Titel eines Beitrags, konvertiert den Inhalt in gut strukturiertes Markdown, fügt eine TULLU-Lizenz hinzu, komprimiert und kodiert den Inhalt und gibt einen LiaScript-Link über einen Shortcode aus.
|
|
|
|
Version: 1.0
|
|
|
|
Author: Joachim Happel
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!defined('ABSPATH')) {
|
|
|
|
exit; // Exit if accessed directly.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Include Composer autoload
|
|
|
|
require_once __DIR__ . '/vendor/autoload.php';
|
|
|
|
|
|
|
|
#use Parsedown;
|
|
|
|
use League\HTMLToMarkdown\HtmlConverter;
|
|
|
|
|
|
|
|
class PostToLiaScriptShortcodeButton {
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
add_shortcode('shareliascriptbtn', array($this, 'generate_lia_script_link'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function generate_lia_script_link($atts) {
|
|
|
|
$atts = shortcode_atts(array(
|
|
|
|
'class' => 'button',
|
|
|
|
'label' => 'Teilen'
|
|
|
|
), $atts, 'shareliascriptbtn');
|
|
|
|
|
|
|
|
if (is_singular()) {
|
|
|
|
global $post;
|
|
|
|
|
|
|
|
$title = get_the_title($post->ID);
|
|
|
|
$content = apply_filters('the_content', $post->post_content);
|
2024-05-23 22:11:49 +00:00
|
|
|
|
|
|
|
// Get the license terms
|
|
|
|
$terms = wp_get_post_terms($post->ID, 'license');
|
|
|
|
if (!empty($terms) && !is_wp_error($terms)) {
|
|
|
|
$term = $terms[0];
|
|
|
|
$license = $term->name;
|
|
|
|
$license_url = term_description($term->term_id);
|
|
|
|
$license_icon_url = "https://mirrors.creativecommons.org/presskit/buttons/88x31/png/" . str_replace("cc-", "", $term->slug) . ".png";
|
|
|
|
} else {
|
|
|
|
// Default values if no license term is found
|
|
|
|
$license = "No License";
|
|
|
|
$license_url = "#";
|
|
|
|
$license_icon_url = "#";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get author and site information
|
|
|
|
$post_author_id = $post->post_author;
|
|
|
|
$post_author_name = get_the_author_meta('display_name', $post_author_id);
|
|
|
|
$post_author_url = get_author_posts_url($post_author_id);
|
|
|
|
$home_url = home_url();
|
|
|
|
$blogname = get_bloginfo('name');
|
|
|
|
$post_url = get_permalink($post->ID);
|
|
|
|
|
|
|
|
// License HTML snippet
|
|
|
|
$license_html = "
|
|
|
|
<div class='oer-cc-licensebox'>
|
|
|
|
<a href='{$license_url}'>
|
|
|
|
<img decoding='async' src='{$license_icon_url}' alt='{$license}'>
|
|
|
|
</a>
|
|
|
|
<br>
|
|
|
|
Weiternutzung als OER ausdrücklich erlaubt: Dieses OER ist - sofern dies bei Teilinhalten nicht anders angegeben - lizenziert unter
|
|
|
|
<a href='{$license_url}' rel='license' target='_blank'>{$license}</a>.
|
|
|
|
Nennung gemäß <a href='https://open-educational-resources.de/oer-tullu-regel/'>TULLU-Regel</a> bitte wie folgt:<br>
|
|
|
|
<i>
|
|
|
|
<span xmlns:dct='http://purl.org/dc/terms/' property='dct:title'><a href='{$post_url}' xmlns:cc='http://creativecommons.org/ns#' rel='cc:attributionURL dct:creator'>{$title}</a> (<a href='{$home_url}' xmlns:cc='http://creativecommons.org/ns#' rel='cc:attributionURL dct:publisher'><em>{$blogname}</em></a>)</span>
|
|
|
|
von <span xmlns:cc='http://creativecommons.org/ns#' property='cc:attributionName dct:author'><a href='{$post_author_url}' rel='tag'>{$post_author_name}</a></span>,
|
|
|
|
Lizenz: <a href='{$license_url}' target='_blank'>{$license}</a>
|
|
|
|
</i>.
|
|
|
|
</div>";
|
|
|
|
|
|
|
|
$license_markdown = "\n\n---\n\n### OER-Lizenz\n\n".
|
|
|
|
"Dieses Werk ist - sofern Inhalte nicht anders gekennzeichnet - lizenziert unter".
|
|
|
|
"\n[$title]($post_url)\n[$license]($license_url) [$post_author_name]($post_author_url)/[$blogname]($home_url)";
|
2024-05-23 17:08:09 +00:00
|
|
|
|
|
|
|
// Convert content to Markdown
|
2024-05-23 22:11:49 +00:00
|
|
|
$markdownContent = $this->convert_to_markdown($content).$license_markdown;
|
2024-05-23 17:08:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Convert Markdown content to gzip Base64
|
|
|
|
$contentHash = $this->create_gzip_base64_data($markdownContent);
|
|
|
|
|
|
|
|
// Generate the link
|
|
|
|
$link = sprintf(
|
|
|
|
'<a href="https://liascript.github.io/LiveEditor/?/show/code/%s" class="%s" target="_blank">%s</a>',
|
|
|
|
$contentHash,
|
|
|
|
esc_attr($atts['class']),
|
|
|
|
esc_html($atts['label'])
|
|
|
|
);
|
|
|
|
|
2024-05-23 22:11:49 +00:00
|
|
|
return $license_html.$link;
|
2024-05-23 17:08:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
private function convert_to_markdown($content) {
|
2024-05-23 22:11:49 +00:00
|
|
|
#$parsedown = new Parsedown();
|
|
|
|
#$contenthtml = $parsedown->text($content);
|
|
|
|
$converter = new HtmlConverter(array('strip_tags' => true));
|
|
|
|
$markdown = $converter->convert($content);
|
|
|
|
# $markdown = strip_tags($markdown);
|
2024-05-23 17:08:09 +00:00
|
|
|
return $markdown;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function create_gzip_base64_data($data) {
|
|
|
|
$gzData = gzencode($data, 9);
|
|
|
|
return base64_encode($gzData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize the plugin
|
|
|
|
new PostToLiaScriptShortcodeButton();
|