| @@ -1,0 +1,49 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +declare(strict_types=1); | |
| 4 | + | |
| 5 | +namespace Metricool\Support\Helpers; | |
| 6 | + | |
| 7 | +use Metricool\Bootstrap\App; | |
| 8 | +use Metricool\Support\Helpers\Storages\EnvironmentConfig; | |
| 9 | + | |
| 10 | +class MetricoolUrl | |
| 11 | +{ | |
| 12 | + /** | |
| 13 | + * Add required query args to a Metricool admin url | |
| 14 | + * @param string $url External url to a Metricool admin page | |
| 15 | + * @return string The url with the required query args | |
| 16 | + */ | |
| 17 | + public static function adminUrl(string $url): string | |
| 18 | + { | |
| 19 | + $queryArgs = array_filter([ | |
| 20 | + 'blogId' => get_option('metricool_blog_id', ''), | |
| 21 | + 'userId' => get_option('metricool_user_id', ''), | |
| 22 | + ]); | |
| 23 | + | |
| 24 | + return add_query_arg($queryArgs, $url); | |
| 25 | + } | |
| 26 | + | |
| 27 | + | |
| 28 | + /** | |
| 29 | + * Returns the Metricool create post URL for the given content and media | |
| 30 | + * This is a deeplink to the create-post screen of the Metricool web app | |
| 31 | + * @param string|false|null $mediaUrl Optional media URL to be included | |
| 32 | + * in the post | |
| 33 | + */ | |
| 34 | + public static function createPostUrl(string $content, $mediaUrl = null): string | |
| 35 | + { | |
| 36 | + $queryArgs = [ | |
| 37 | + 'blogId' => get_option('metricool_blog_id', ''), | |
| 38 | + 'userId' => get_option('metricool_user_id', ''), | |
| 39 | + 'post.content' => $content, | |
| 40 | + ]; | |
| 41 | + | |
| 42 | + if ($mediaUrl) { | |
| 43 | + $queryArgs['post.media'] = $mediaUrl; | |
| 44 | + } | |
| 45 | + | |
| 46 | + $env = App::getInstance()->get(EnvironmentConfig::class); | |
| 47 | + return add_query_arg(array_filter($queryArgs), $env->getUrl('metricool.create_post_url')); | |
| 48 | + } | |
| 49 | +} | |