Skip to content

Commit

Permalink
(feat): live preview
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonvanWijhe committed Jul 14, 2021
1 parent 8d7c46e commit fa87abf
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 15 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Version [2.0.10]

### Feat

- Add preview parameter for retrieving drafts
- Purge Varnish on save_post

### Fix

- Add addHighlightedParameters to active items
Expand Down
9 changes: 6 additions & 3 deletions config/core.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
return [

/**
* Service Providers.
*/
* Service Providers.
*/
'providers' => [
/**
/**
* Global providers.
*/
OWC\OpenPub\Base\PostType\PostTypeServiceProvider::class,
Expand All @@ -15,6 +15,9 @@
OWC\OpenPub\Base\Metabox\MetaboxServiceProvider::class,
OWC\OpenPub\Base\RestAPI\RestAPIServiceProvider::class,
OWC\OpenPub\Base\ElasticPress\ElasticPressServiceProvider::class,
OWC\OpenPub\Base\Admin\AdminServiceProvider::class,
OWC\OpenPub\Base\Varnish\VarnishServiceProvider::class,

/**
* Providers specific to the admin.
*/
Expand Down
38 changes: 38 additions & 0 deletions src/Base/Admin/AdminServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace OWC\OpenPub\Base\Admin;

use OWC\OpenPub\Base\Foundation\ServiceProvider;
use OWC\OpenPub\Base\Models\Item;

class AdminServiceProvider extends ServiceProvider
{
public function register()
{
$this->plugin->loader->addAction('preview_post_link', $this, 'filterPostLink', 10, 10, 2);
$this->plugin->loader->addAction('rest_prepare_pdc-item', $this, 'restPrepareResponseLink', 10, 2);
}

/**
* Changes the url user for live preview to the portal url.
* Works in the old editor (not gutenberg)
*/
public function filterPostLink($link, \WP_Post $post): string
{
$itemModel = Item::makeFrom($post);
$url = $itemModel->getPortalURL();
return $url . "?preview=true";
}

/**
* Changes the url used for live preview to the portal url.
* Works in the gutenberg editor.
*/
public function restPrepareResponseLink(\WP_REST_Response $response, \WP_Post $post): \WP_REST_Response
{
$itemModel = Item::makeFrom($post);
$response->data['link'] = $itemModel->getPortalURL() ?? '';

return $response;
}
}
13 changes: 7 additions & 6 deletions src/Base/Repositories/AbstractRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,12 +227,13 @@ public static function getGlobalFields(): array
public function transform(WP_Post $post)
{
$data = [
'id' => $post->ID,
'title' => $post->post_title,
'content' => apply_filters('the_content', $post->post_content),
'excerpt' => $post->post_excerpt,
'date' => $post->post_date,
'slug' => $post->post_name
'id' => $post->ID,
'title' => $post->post_title,
'content' => apply_filters('the_content', $post->post_content),
'excerpt' => $post->post_excerpt,
'date' => $post->post_date,
'slug' => $post->post_name,
'post_status' => $post->post_status
];

$data = $this->assignFields($data, $post);
Expand Down
26 changes: 20 additions & 6 deletions src/Base/RestAPI/Controllers/ItemController.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ public function getItem(WP_REST_Request $request)
{
$id = (int) $request->get_param('id');

$item = (new Item)
->query(apply_filters('owc/openpub/rest-api/items/query/single', []))
->find($id);
$item = $this->singleItemQueryBuilder($request);

$item = $item->find($id);

if (!$item) {
return new WP_Error('no_item_found', sprintf('Item with ID "%d" not found (anymore)', $id), [
Expand All @@ -89,9 +89,9 @@ public function getItemBySlug(WP_REST_Request $request)
{
$slug = $request->get_param('slug');

$item = (new Item)
->query(apply_filters('owc/openpub/rest-api/items/query/single', []))
->findBySlug($slug);
$item = $this->singleItemQueryBuilder($request);

$item = $item->findBySlug($slug);

if (!$item) {
return new WP_Error('no_item_found', sprintf('Item with slug "%d" not found', $slug), [
Expand All @@ -104,6 +104,20 @@ public function getItemBySlug(WP_REST_Request $request)
return $item;
}

public function singleItemQueryBuilder(WP_REST_Request $request): Item
{
$item = (new Item)
->query(apply_filters('owc/openpub/rest-api/items/query/single', []));

$preview = filter_var($request->get_param('preview'), FILTER_VALIDATE_BOOLEAN);

if (true === $preview) {
$item->query(['post_status' => ['publish', 'draft']]);
}

return $item;
}

/**
* Get related items
*/
Expand Down
33 changes: 33 additions & 0 deletions src/Base/Varnish/VarnishServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace OWC\OpenPub\Base\Varnish;

use OWC\OpenPub\Base\Foundation\ServiceProvider;

class VarnishServiceProvider extends ServiceProvider
{
public function register(): void
{
$this->plugin->loader->addAction('save_post', $this, 'purgeVarnishCache', 10, 2);
}

public function purgeVarnishCache(int $postID, \WP_Post $post): void
{
if ( ! function_exists('curl_init')){
return;
}

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'http://127.0.0.1/.*');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PURGE');

$headers = [];
$headers[] = 'Host: '. \parse_url(get_site_url(), PHP_URL_HOST);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

curl_exec($ch);
curl_close($ch);
}
}

0 comments on commit fa87abf

Please sign in to comment.