From d2ff1e0303e8172d84c25cfe262341f433c91ec4 Mon Sep 17 00:00:00 2001 From: dcooperdalrymple Date: Thu, 12 Sep 2024 17:30:01 -0500 Subject: [PATCH] Added User API endpoints. --- includes/api/class-user.php | 57 +++++++++++++++++++++++++++++++++++++ includes/class-api.php | 13 +++++++++ ogre-issuu.php | 1 + 3 files changed, 71 insertions(+) create mode 100644 includes/api/class-user.php diff --git a/includes/api/class-user.php b/includes/api/class-user.php new file mode 100644 index 0000000..c8a768c --- /dev/null +++ b/includes/api/class-user.php @@ -0,0 +1,57 @@ +issuu = $issuu; + } + + private static function parse_args(array $args, array $defaults):array { + return array_filter( + wp_parse_args($args, $defaults), + function ($value, $key) use ($defaults) { + return $value !== $defaults[$key]; + }, + ARRAY_FILTER_USE_BOTH + ); + } + + public function getUserProfile():stdClass { + return $this->issuu->getResponse(method: 'GET', endpoint: "user/me"); + } + + public function getUserFeatures():stdClass { + return $this->issuu->getResponse(method: 'GET', endpoint: "user/features"); + } + + public function getPublicationEmbedBySlug(string $slug, array $args = []):stdClass { + $args = self::parse_args($args, [ + 'responsive' => true, + 'width' => '100%', + 'height' => '100%', + 'hideIssuuLogo' => false, + 'hideShareButton' => false, + 'showOtherPublications' => false, + 'bgColor' => '', + 'fullScreenShareBgColor' => '', + ]); + + return $this->issuu->getResponse(method: 'GET', endpoint: "publications/{$slug}/embed", queryParameters: $args); + } + +} diff --git a/includes/class-api.php b/includes/class-api.php index 6aeec18..11ed1e2 100644 --- a/includes/class-api.php +++ b/includes/class-api.php @@ -15,6 +15,7 @@ use Exception; use lasselehtinen\Issuu\Issuu; use lasselehtinen\Issuu\Publications; use OgreIssuu\Api\Publications as ApiPublications; +use OgreIssuu\Api\User; class Api { use Singleton; @@ -26,6 +27,18 @@ class Api { $this->client = new Issuu(Settings::get('token')); } + public function get_user():?stdClass { + if (!isset($this->client)) return null; + + $user = new User($this->client); + try { + return $user->getUserProfile(); + } catch (Exception $e) { + error_log($e->getMessage()); + return null; + } + } + public function get_publications(array $args = []):?stdClass { if (!isset($this->client)) return null; diff --git a/ogre-issuu.php b/ogre-issuu.php index d5dc2a2..362be01 100644 --- a/ogre-issuu.php +++ b/ogre-issuu.php @@ -37,6 +37,7 @@ class Plugin extends PluginBase { 'vendor/autoload.php', 'includes/class-settings.php', 'includes/api/class-publications.php', + 'includes/api/class-user.php', 'includes/class-api.php', ]); }