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 );
// Convert content to Markdown
$markdownContent = $this -> convert_to_markdown ( $content );
// Append TULLU license
$license = " \n \n --- \n \n " . " This work is licensed under the TULLU rule. For more information, visit https://open-educational-resources.de/oer-tullu-regel/ " ;
$markdownContent .= $license ;
// 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' ])
);
return $link ;
}
return '' ;
}
private function convert_to_markdown ( $content ) {
$parsedown = new Parsedown ();
$html = $parsedown -> text ( $content );
$converter = new HtmlConverter ();
$markdown = $converter -> convert ( $html );
2024-05-23 18:17:07 +00:00
$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 ();