improve publishing
This commit is contained in:
+62
-15
@@ -26,13 +26,15 @@ class Page implements PageContract
|
||||
public function setData($data, $fullOverwrite = true)
|
||||
{
|
||||
// if page builder data is set, try to decode json
|
||||
if (isset($data['data']) && is_string($data['data'])) {
|
||||
$data['data'] = json_decode($data['data'], true);
|
||||
if (isset($data["data"]) && is_string($data["data"])) {
|
||||
$data["data"] = json_decode($data["data"], true);
|
||||
}
|
||||
if ($fullOverwrite) {
|
||||
$this->attributes = $data;
|
||||
} elseif (is_array($data)) {
|
||||
$this->attributes = is_null($this->attributes) ? [] : $this->attributes;
|
||||
} elseif (is_array($data)) {
|
||||
$this->attributes = is_null($this->attributes)
|
||||
? []
|
||||
: $this->attributes;
|
||||
foreach ($data as $key => $value) {
|
||||
$this->attributes[$key] = $value;
|
||||
}
|
||||
@@ -66,7 +68,7 @@ class Page implements PageContract
|
||||
*/
|
||||
public function getBuilderData()
|
||||
{
|
||||
return $this->attributes['data'] ?? [];
|
||||
return $this->attributes["data"] ?? [];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -76,7 +78,7 @@ class Page implements PageContract
|
||||
*/
|
||||
public function getId()
|
||||
{
|
||||
return $this->get('id');
|
||||
return $this->get("id");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -86,7 +88,7 @@ class Page implements PageContract
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return $this->get('name');
|
||||
return $this->get("name");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -96,7 +98,7 @@ class Page implements PageContract
|
||||
*/
|
||||
public function getLayout()
|
||||
{
|
||||
return $this->get('layout');
|
||||
return $this->get("layout");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -107,10 +109,18 @@ class Page implements PageContract
|
||||
public function getTranslations()
|
||||
{
|
||||
if (is_null($this->translations)) {
|
||||
$records = (new PageTranslationRepository)->findWhere(phpb_config('page.translation.foreign_key'), $this->getId());
|
||||
$records = new PageTranslationRepository()->findWhere(
|
||||
phpb_config("page.translation.foreign_key"),
|
||||
$this->getId(),
|
||||
);
|
||||
$translations = [];
|
||||
foreach ($records as $record) {
|
||||
if (in_array($record->locale, array_keys(phpb_active_languages()))) {
|
||||
if (
|
||||
in_array(
|
||||
$record->locale,
|
||||
array_keys(phpb_active_languages()),
|
||||
)
|
||||
) {
|
||||
$translations[$record->locale] = (array) $record;
|
||||
}
|
||||
}
|
||||
@@ -129,7 +139,11 @@ class Page implements PageContract
|
||||
public function getTranslation(string $setting, ?string $locale = null)
|
||||
{
|
||||
$translations = $this->getTranslations();
|
||||
return (new PageTranslationService())->resolveTranslation($translations, $setting, $locale);
|
||||
return new PageTranslationService()->resolveTranslation(
|
||||
$translations,
|
||||
$setting,
|
||||
$locale,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -140,13 +154,46 @@ class Page implements PageContract
|
||||
*/
|
||||
public function getRoute(?string $locale = null)
|
||||
{
|
||||
$routeTranslation = $this->getTranslation('route', $locale);
|
||||
$routeTranslation = $this->getTranslation("route", $locale);
|
||||
foreach (phpb_route_parameters() as $routeParameter => $value) {
|
||||
$routeTranslation = str_replace('{' . $routeParameter . '}', $value, $routeTranslation);
|
||||
$routeTranslation = str_replace(
|
||||
"{" . $routeParameter . "}",
|
||||
$value,
|
||||
$routeTranslation,
|
||||
);
|
||||
}
|
||||
return $routeTranslation;
|
||||
}
|
||||
|
||||
protected function getPreviewToken(): string
|
||||
{
|
||||
if (
|
||||
!isset($_SESSION["phpb_preview_token"]) ||
|
||||
!is_string($_SESSION["phpb_preview_token"]) ||
|
||||
$_SESSION["phpb_preview_token"] === ""
|
||||
) {
|
||||
$_SESSION["phpb_preview_token"] = bin2hex(random_bytes(32));
|
||||
}
|
||||
|
||||
return $_SESSION["phpb_preview_token"];
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the route of this page.
|
||||
*
|
||||
* @param string|null $locale
|
||||
* @return mixed|string|null
|
||||
*/
|
||||
public function getPreviewRoute(?string $locale = null)
|
||||
{
|
||||
$previewUrl = $this->getRoute($locale);
|
||||
$previewUrl .=
|
||||
(str_contains($previewUrl, "?") ? "&" : "?") .
|
||||
"previewToken=" .
|
||||
rawurlencode($this->getPreviewToken());
|
||||
return $previewUrl;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value of the given property of this Page.
|
||||
*
|
||||
@@ -172,9 +219,9 @@ class Page implements PageContract
|
||||
public function invalidateCache()
|
||||
{
|
||||
$routes = array_map(function ($translation) {
|
||||
return $translation['route'] ?? null;
|
||||
return $translation["route"] ?? null;
|
||||
}, $this->getTranslations());
|
||||
|
||||
(new PageTranslationService())->invalidateRoutes($routes);
|
||||
new PageTranslationService()->invalidateRoutes($routes);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user