Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure view data compatibility across Statamic Antlers versions #168

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Parsers/PageDataParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public static function getSettingsBlueprintWithValues($ctx, $type, $blueprint_cl
public static function generatePageTitle($data, $ctx)
{
if ($data->get('meta_title') && $data->get('meta_title')->raw()) {
return Parse::template($data->get('meta_title'), $ctx);
return Parse::template($data->get('meta_title'), [], $ctx);
}

if ($data->get('response_code') === 404) {
Expand Down
114 changes: 103 additions & 11 deletions src/Tags/AardvarkSeoTags.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,41 @@ class AardvarkSeoTags extends Tags
{
protected static $handle = 'aardvark-seo';

// The $ctx property is used to store the context as a collection.
// It's initialised and set on the first method call to avoid subsequent calls.
protected $ctx = null;

// The $ctxParsed property holds the processed data obtained from the PageDataParser.
// It's initialised and set on the first method call to avoid subsequent calls.
protected $ctxParsed = null;

/**
* Return the <head /> tag content required for on-page SEO
*
* @return string
*/
public function head()
{
$data = PageDataParser::getData(collect($this->context));

// Check if $this->ctx is not set. If not, initialise it with the current context.
if (!$this->ctx) {
$this->ctx = $this->context;
}

// Check if $this->ctxParsed is not set. If not, parse $this->ctx using PageDataParser
// and store the result in $this->ctxParsed for reuse.
if (!$this->ctxParsed) {
$this->ctxParsed = PageDataParser::getData($this->ctx);
}

$data = $this->ctxParsed;

// Check the version of Statamic Antlers. If it's 'regex', use the Laravel view helper,
// else use the Statamic View facade. Also, ensure $data is an array when passing it to the view.
if (config('statamic.antlers.version') == 'regex') {
$view = view('aardvark-seo::tags.head', $data);
$view = $data instanceof \Illuminate\Support\Collection ?
view('aardvark-seo::tags.head', $data->all()) :
view('aardvark-seo::tags.head', $data);
} else {
$view = View::make('aardvark-seo::tags.head', $data->all());
}
Expand All @@ -51,10 +75,26 @@ public function head()
*/
public function body()
{
$data = PageDataParser::getData(collect($this->context));

// Check if $this->ctx is not set. If not, initialise it with the current context.
if (!$this->ctx) {
$this->ctx = collect($this->context);
}

// Check if $this->ctxParsed is not set. If not, parse $this->ctx using PageDataParser
// and store the result in $this->ctxParsed for reuse.
if (!$this->ctxParsed) {
$this->ctxParsed = PageDataParser::getData($this->ctx);
}

$data = $this->ctxParsed;

// Check the version of Statamic Antlers. If it's 'regex', use the Laravel view helper,
// else use the Statamic View facade. Also, ensure $data is an array when passing it to the view.
if (config('statamic.antlers.version') == 'regex') {
$view = view('aardvark-seo::tags.body', $data);
$view = $data instanceof \Illuminate\Support\Collection ?
view('aardvark-seo::tags.body', $data->all()) :
view('aardvark-seo::tags.body', $data);
} else {
$view = View::make('aardvark-seo::tags.body', $data->all());
}
Expand All @@ -67,10 +107,26 @@ public function body()
*/
public function footer()
{
$data = PageDataParser::getData(collect($this->context));

// Check if $this->ctx is not set. If not, initialise it with the current context.
if (!$this->ctx) {
$this->ctx = collect($this->context);
}

// Check if $this->ctxParsed is not set. If not, parse $this->ctx using PageDataParser
// and store the result in $this->ctxParsed for reuse.
if (!$this->ctxParsed) {
$this->ctxParsed = PageDataParser::getData($this->ctx);
}

$data = $this->ctxParsed;

// Check the version of Statamic Antlers. If it's 'regex', use the Laravel view helper,
// else use the Statamic View facade. Also, ensure $data is an array when passing it to the view.
if (config('statamic.antlers.version') == 'regex') {
$view = view('aardvark-seo::tags.footer', $data);
$view = $data instanceof \Illuminate\Support\Collection ?
view('aardvark-seo::tags.footer', $data->all()) :
view('aardvark-seo::tags.footer', $data);
} else {
$view = View::make('aardvark-seo::tags.footer', $data->all());
}
Expand All @@ -83,7 +139,13 @@ public function footer()
*/
public function hreflang()
{
$ctx = collect($this->context);

// Check if $this->ctx is not set. If not, initialise it with the current context.
if (!$this->ctx) {
$this->ctx = collect($this->context);
}

$ctx = $this->ctx;

$id = $ctx->get('id');

Expand Down Expand Up @@ -134,7 +196,13 @@ public function hreflang()
*/
public function graph()
{
$ctx = collect($this->context);

// Check if $this->ctx is not set. If not, initialise it with the current context.
if (!$this->ctx) {
$this->ctx = collect($this->context);
}

$ctx = $this->ctx;
$graph = new SchemaGraph($ctx);
return $graph->build();
}
Expand All @@ -146,7 +214,19 @@ public function graph()
*/
public function socials()
{
$data = PageDataParser::getData(collect($this->context));

// Check if $this->ctx is not set. If not, initialise it with the current context.
if (!$this->ctx) {
$this->ctx = collect($this->context);
}

// Check if $this->ctxParsed is not set. If not, parse $this->ctx using PageDataParser
// and store the result in $this->ctxParsed for reuse.
if (!$this->ctxParsed) {
$this->ctxParsed = PageDataParser::getData($this->ctx);
}

$data = $this->ctxParsed;
$socials = $data->get('aardvark_social_settings')->get('social_links');
if ($socials->raw()) {
return $this->parseLoop($socials->raw());
Expand All @@ -162,7 +242,13 @@ public function socials()
*/
public function robotsTag()
{
$ctx = collect($this->context);

// Check if $this->ctx is not set. If not, initialise it with the current context.
if (!$this->ctx) {
$this->ctx = collect($this->context);
}

$ctx = $this->ctx;
$attrs = [];

$global_no_index = $ctx->get('aardvark_general_settings')['no_index_site'];
Expand Down Expand Up @@ -191,7 +277,13 @@ public function robotsTag()
*/
public function generatedCanonical()
{
$data = collect($this->context);

// Check if $this->ctx is not set. If not, initialise it with the current context.
if (!$this->ctx) {
$this->ctx = collect($this->context);
}

$data = $this->ctx;
$vars = $data->get('get');
$current_url = $data->get('permalink');
if ($vars && $page = collect($vars)->get('page')) {
Expand Down