-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathAdminServiceProvider.php
More file actions
56 lines (46 loc) · 1.86 KB
/
AdminServiceProvider.php
File metadata and controls
56 lines (46 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
namespace OWC\OpenPub\Base\Admin;
use OWC\OpenPub\Base\Foundation\ServiceProvider;
use OWC\OpenPub\Base\Models\Item;
use WP_Post;
use WP_REST_Response;
class AdminServiceProvider extends ServiceProvider
{
public function register(): void
{
$this->plugin->loader->addFilter('post_type_link', $this, 'filterPostLink', 10, 4);
$this->plugin->loader->addFilter('preview_post_link', $this, 'filterPreviewLink', 10, 2);
$this->plugin->loader->addAction('rest_prepare_openpub-item', $this, 'filterPreviewInNewTabLink', 10, 2);
}
/**
* Change the url for preview of published posts in the portal.
*/
public function filterPostLink(string $link, WP_Post $post, bool $leavename, $sample): string
{
if ($post->post_type !== 'openpub-item' || ! $this->plugin->settings->isPortalSlugValid()) {
return $link;
}
return sprintf('%s%s', Item::makeFrom($post)->getBasePortalURL(), ($leavename ? '%postname%' : $post->post_name));
}
/**
* Change the url for preview of draft posts in the portal.
*/
public function filterPreviewLink(string $link, WP_Post $post): string
{
if ($post->post_type !== 'openpub-item' || ! $this->plugin->settings->isPortalSlugValid()) {
return $link;
}
return sprintf('%s%s', Item::makeFrom($post)->getPortalURL(), '?draft-preview=true');
}
/**
* Change the url of "preview in new tab" button for preview in the portal.
*/
public function filterPreviewInNewTabLink(WP_REST_Response $response, WP_Post $post): WP_REST_Response
{
if ($post->post_status === 'publish' || ! $this->plugin->settings->isPortalSlugValid()) {
return $response;
}
$response->data['link'] = Item::makeFrom($post)->getPortalURL() . "?draft-preview=true";
return $response;
}
}