1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Riclep\Storyblok; |
5
|
|
|
|
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
use Riclep\Storyblok\Traits\ProcessesBlocks; |
8
|
|
|
|
9
|
|
|
abstract class Page |
10
|
|
|
{ |
11
|
|
|
use ProcessesBlocks; |
|
|
|
|
12
|
|
|
|
13
|
|
|
private $processedJson; |
14
|
|
|
private $content; |
15
|
|
|
private $seo; |
16
|
|
|
protected $title; |
17
|
|
|
|
18
|
|
|
public function __construct($rawStory) |
19
|
|
|
{ |
20
|
|
|
$this->processedJson = $rawStory; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Performs any actions on the Storyblok content before it is parsed into Block classes |
26
|
|
|
* Move SEO plugin out of content to the root of the page’s response |
27
|
|
|
*/ |
28
|
|
|
public function preprocess() { |
29
|
|
|
if (array_key_exists('seo', $this->processedJson['content'])) { |
30
|
|
|
$this->processedJson['seo'] = $this->processedJson['content']['seo']; |
31
|
|
|
unset($this->processedJson['content']['seo']); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
return $this; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Processes the page’s meta content |
39
|
|
|
*/ |
40
|
|
|
public function process() { |
41
|
|
|
$this->seo = array_key_exists('seo', $this->processedJson) ? $this->processedJson['seo'] : null; |
42
|
|
|
|
43
|
|
|
return $this; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function getBlocks() { |
47
|
|
|
$this->content = $this->processBlock($this->processedJson['content'], 'root'); |
48
|
|
|
|
49
|
|
|
return $this; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return array |
54
|
|
|
*/ |
55
|
|
|
protected function view() { |
56
|
|
|
$views = []; |
57
|
|
|
|
58
|
|
|
$viewFile = strtolower(subStr((new \ReflectionClass($this))->getShortName(), 0, -4)); |
59
|
|
|
|
60
|
|
|
if ($viewFile !== 'default') { |
61
|
|
|
$views[] = config('storyblok.view_path') . 'pages.' . $viewFile; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
$segments = explode('/', rtrim($this->slug(), '/')); |
66
|
|
|
// creates an array of dot paths for each path segment |
67
|
|
|
// site.com/this/that/them becomes: |
68
|
|
|
// this.that.them |
69
|
|
|
// this.that |
70
|
|
|
// this |
71
|
|
|
while (count($segments) >= 1) { |
72
|
|
|
$views[] = 'storyblok.pages.' . implode('.', $segments); |
73
|
|
|
|
74
|
|
|
if (!in_array($singular = 'storyblok.pages.' . Str::singular(implode('.', $segments)), $views)) { |
75
|
|
|
$views[] = $singular; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
array_pop($segments); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$views[] = 'storyblok.pages.default'; |
82
|
|
|
|
83
|
|
|
return $views; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Reads the story |
88
|
|
|
* |
89
|
|
|
* @return array |
90
|
|
|
*/ |
91
|
|
|
public function render() { |
92
|
|
|
return view()->first( |
93
|
|
|
$this->view(), |
94
|
|
|
[ |
95
|
|
|
'title' => $this->title(), |
96
|
|
|
'meta_description' => $this->metaDescription(), |
97
|
|
|
'content' => $this->content(), |
98
|
|
|
'seo' => $this->seo, |
99
|
|
|
] |
100
|
|
|
); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function title() { |
104
|
|
|
if ($this->seo) { |
105
|
|
|
return $this->seo['title']; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $this->processedJson['name']; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function metaDescription() { |
112
|
|
|
if ($this->seo) { |
113
|
|
|
return $this->seo['description']; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return config('seo.default-description'); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function content() { |
120
|
|
|
return $this->content; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function slug() |
124
|
|
|
{ |
125
|
|
|
return $this->processedJson['full_slug']; |
126
|
|
|
} |
127
|
|
|
} |