From 4c9c39aa52a8891b4c6bfaa8ed620d0e847e967c Mon Sep 17 00:00:00 2001 From: dcooperdalrymple Date: Thu, 12 Sep 2024 17:30:57 -0500 Subject: [PATCH] Added `publication-embed` shortcode. --- includes/shortcodes/abstract-shortcode.php | 47 +++++++++++++++++++ .../shortcodes/class-publication-embed.php | 44 +++++++++++++++++ ogre-issuu.php | 2 + 3 files changed, 93 insertions(+) create mode 100644 includes/shortcodes/abstract-shortcode.php create mode 100644 includes/shortcodes/class-publication-embed.php diff --git a/includes/shortcodes/abstract-shortcode.php b/includes/shortcodes/abstract-shortcode.php new file mode 100644 index 0000000..77768fa --- /dev/null +++ b/includes/shortcodes/abstract-shortcode.php @@ -0,0 +1,47 @@ +register(); + } + + protected function get_tag():string { + $parts = explode('\\', get_called_class()); + return sanitize_key(preg_replace('/([a-z])([A-Z])/', '$1-$2', array_pop($parts))); + } + + protected function register():void { + add_shortcode($this->get_tag(), [$this, 'callback']); + } + + public function get_default_attributes():array { + return []; + } + + public function get_attributes(array $atts = []):array { + return shortcode_atts($this->get_default_attributes(), $atts, $this->get_tag()); + } + + public function callback(array $atts, ?string $content = null) { + ob_start(); + $this->render($this->get_attributes($atts), (!is_null($content) ? $content : '')); + return ob_get_clean(); + } + + public abstract function render(array $atts, string $content):void; + +} diff --git a/includes/shortcodes/class-publication-embed.php b/includes/shortcodes/class-publication-embed.php new file mode 100644 index 0000000..0cb2a0e --- /dev/null +++ b/includes/shortcodes/class-publication-embed.php @@ -0,0 +1,44 @@ + '', + 'responsive' => true, + 'width' => '100%', + 'height' => '100%', + 'hideIssuuLogo' => false, + 'hideShareButton' => false, + 'showOtherPublications' => false, + 'bgColor' => '', + 'fullScreenShareBgColor' => '', + ]; + } + + public function render(array $atts, string $content):void { + $slug = $atts['slug']; + unset($atts['slug']); + if (empty($slug)) return; + + $embed = Api::instance()->get_publication_embed($slug, $atts); + if (is_null($embed) || !property_exists($embed, 'embed')) return; + + echo $embed->embed; + } + +} + +PublicationEmbed::instance(); diff --git a/ogre-issuu.php b/ogre-issuu.php index 362be01..706fae7 100644 --- a/ogre-issuu.php +++ b/ogre-issuu.php @@ -39,6 +39,8 @@ class Plugin extends PluginBase { 'includes/api/class-publications.php', 'includes/api/class-user.php', 'includes/class-api.php', + 'includes/shortcodes/abstract-shortcode.php', + 'includes/shortcodes/class-publication-embed.php', ]); }