@@ -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_key_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_key: ' .$ Headers ["api_key " ];
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 );
0 commit comments