Added User API endpoints.

This commit is contained in:
dcooperdalrymple
2024-09-12 17:30:01 -05:00
parent aab8db5400
commit d2ff1e0303
3 changed files with 71 additions and 0 deletions

View File

@@ -0,0 +1,57 @@
<?php
/**
* @package CleverOgre
* @subpackage OgreIssuu
* @version 0.1.0
* @since 0.1.0
*/
namespace OgreIssuu\Api;
if (!defined('ABSPATH')) exit;
use stdClass;
use lasselehtinen\Issuu\Issuu;
class User {
private Issuu $issuu;
public function __construct(Issuu $issuu) {
$this->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);
}
}

View File

@@ -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;

View File

@@ -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',
]);
}