Skip to content

Commit 6e7e541

Browse files
committed
🎉 Live Preview feature support added
1 parent 27f7f31 commit 6e7e541

File tree

4 files changed

+54
-8
lines changed

4 files changed

+54
-8
lines changed

src/Contentstack.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ abstract class Contentstack
4646
public static function Stack($api_*** = '',
4747
$access_token = '',
4848
$environment = '',
49-
$config = array('region'=> '')
49+
$config = array('region'=> '', 'live_preview' => array('enable' => false))
5050
) {
5151
return new Stack($api_***, $access_token, $environment, $config);
5252
}

src/Stack/Stack.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ public function __construct(
5555
$api_*** = '',
5656
$delivery_token = '',
5757
$environment = '',
58-
$config = array('region'=> '')
58+
$config = array('region'=> '', 'live_preview' => array())
5959
) {
6060

61-
if ($config && $config !== "undefined" && $config['region'] !== "undefined" && $config['region'] =="eu" ) {
61+
if ($config && $config !== "undefined" && array_***_exists('region', $config) && $config['region'] !== "undefined" && $config['region'] =="eu" ) {
6262
$this->host = $config['region'].'-cdn.contentstack.com';
6363
}
6464
$this->header = Utility::validateInput(
@@ -69,6 +69,7 @@ public function __construct(
6969
);
7070
$this->environment = $this->header['environment'];
7171
unset($this->header['environment']);
72+
$this->live_preview = $config['live_preview'] ?? array();
7273
return $this;
7374
}
7475

@@ -136,6 +137,10 @@ public function ImageTrasform($url, $parameters)
136137
}
137138

138139

140+
public function LivePreviewQuery($parameters) {
141+
$this->live_preview['hash'] = $parameters['hash'] ?? 'init';
142+
$this->live_preview['content_type_uid'] = $parameters['content_type_uid'];
143+
}
139144

140145
/**
141146
* To get the last_activity information of the

src/Support/Utility.php

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,13 @@ public static function validateInput($type = '', $input = array())
9898
throw new \Exception($e->getMessage());
9999
}
100100
}
101+
102+
public static function isLivePreview($query) {
103+
if ($query && isset($query->contentType)) {
104+
return ($query->contentType->stack->live_preview['enable'] == true && array_***_exists('content_type_uid', $query->contentType->stack->live_preview) && $query->contentType->uid == $query->contentType->stack->live_preview['content_type_uid']);
105+
}
106+
return false;
107+
}
101108
/**
102109
* Get the domain from the current object
103110
*
@@ -109,16 +116,20 @@ public static function getDomain($query)
109116
{
110117
$stack = $query;
111118
if ($query && isset($query->contentType)) {
112-
$stack = $query->contentType->stack;
119+
$stack = $query->contentType->stack;
113120
}
114121
if ($query && isset($query->stack)) {
115122
$stack = $query->stack;
116123
}
117124
if ($query && isset($query->assets)) {
118125
$stack = $query->assets->stack;
119126
}
127+
$host = $stack->getHost();
128+
if (Utility::isLivePreview($query)) {
129+
$host = $stack->live_preview['host'];
130+
}
120131
return $stack->getProtocol()
121-
.'://'.$stack->getHost()
132+
.'://'.$host
122133
.':'
123134
.$stack->getPort().VERSION;
124135
}
@@ -136,8 +147,10 @@ public static function contentstackUrl($queryObject = '', $type = '')
136147
$URL = '';
137148
switch ($type) {
138149
case 'set_environment':
139-
$URL = Utility::getDomain($queryObject).ENVIRONMENTS.''
140-
.$queryObject->contentType->stack->getEnvironment();
150+
if (!Utility::isLivePreview($queryObject)) {
151+
$URL = Utility::getDomain($queryObject).ENVIRONMENTS.''
152+
.$queryObject->contentType->stack->getEnvironment();
153+
}
141154
break;
142155
case 'get_last_activites':
143156
$URL = Utility::getDomain($queryObject).CONTENT_TYPES;
@@ -374,7 +387,12 @@ public static function contentstackRequest($queryObject = '', $type = '')
374387
$request_headers = array();
375388
$request_headers[] = 'x-user-agent: contentstack-php/1.6.1';
376389
$request_headers[] = 'api_***: '.$Headers["api_***"];
377-
$request_headers[] = 'access_token: '.$Headers["access_token"];
390+
if (Utility::isLivePreview($queryObject)) {
391+
$request_headers[] = 'authorization: '.$queryObject->contentType->stack->live_preview['authorization'] ;
392+
$request_headers[] = 'hash: '.($queryObject->contentType->stack->live_preview['hash'] ?? 'init');
393+
}else {
394+
$request_headers[] = 'access_token: '.$Headers["access_token"];
395+
}
378396
curl_setopt($http, CURLOPT_HTTPHEADER, $request_headers);
379397

380398
curl_setopt($http, CURLOPT_HEADER, false);

test/EntriesTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
class EntriesTest extends TestCase {
1313
public static $rest;
1414
public static $Stack;
15+
public static $LivePreviewStack;
1516
public static $_uid;
1617
/*
1718
* Setup before the test suites executes
@@ -23,6 +24,13 @@ public static function setUpBeforeClass() : void {
2324
if (self::$rest->getHost() !== NULL) {
2425
self::$Stack->setHost(self::$rest->getHost());
2526
}
27+
28+
self::$LivePreviewStack = Contentstack::Stack(self::$rest->getAPIKEY(), self::$rest->getAccessToken(), self::$rest->getEnvironmentName(), array('live_preview' => array(
29+
'enable'=> true,
30+
'host' => 'preview.contentstack.com',
31+
'authorization' => 'token'
32+
)));
33+
2634
}
2735
/*
2836
* Tear Down after the test suites executes
@@ -49,6 +57,21 @@ public function testFind() {
4957
}
5058
}
5159
}
60+
public function testLivePreviewEntry () {
61+
$_entry = self::$LivePreviewStack->ContentType(CT_ContentType)->Entry(self::$_uid)->toJSON()->fetch();
62+
63+
$this->assertEquals($_entry['title'], 'CB1-10');
64+
}
65+
66+
public function testLivePreviewEntrywithQuery () {
67+
try {
68+
self::$LivePreviewStack->livePreviewQuery(array('content_type_uid' => CT_ContentType));
69+
$_entry = self::$LivePreviewStack->ContentType(CT_ContentType)->Entry(self::$_uid)->toJSON()->fetch();
70+
71+
} catch (Exception $e) {
72+
$this->assertTrue(true);
73+
}
74+
}
5275

5376
public function testFetch() {
5477
$_entry = self::$Stack->ContentType(CT_ContentType)->Entry(self::$_uid)->toJSON()->fetch();

0 commit comments

Comments
 (0)