Skip to content

Commit

Permalink
Version 1.0.10
Browse files Browse the repository at this point in the history
  • Loading branch information
khalwat committed Jan 12, 2016
1 parent 7e500e7 commit a7fbf4a
Show file tree
Hide file tree
Showing 11 changed files with 162 additions and 50 deletions.
31 changes: 29 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -257,13 +257,15 @@ You can use any Craft `environmentVariables` in these fields in addition to stat

This is my {baseUrl}

In addition to being able to hold custom data that you enter manually, you can also set the Source that **SEO Title**, **SEO Description**, **SEO Keywords**, and **SEO Image** SEOmatic Meta fields to pull data from to an existing field in your Entry.
In addition to being able to hold custom data that you enter manually, you can also set the Source that **SEO Title**, **SEO Description**, **SEO Keywords**, and **SEO Image** SEOmatic Meta fields to pull data from to an existing field in your Entry.

**SEO Image** only can pull from an existing Assets field, while **SEO Title**, **SEO Description**, and **SEO Keywords** can pull from Text, Rich Text, and Matrix fields. If you pull from a Matrix field, SEOmatic goes through and concatenates all of the Text & Rich Text fields together (this is useful for **SEO Keywords** generation).

The **SEO Keywords** field also allows you to extract keywords automatically from an existing field in your Entry via the `Keywords From Field` Source option.

## Dynamic Twig SEO Meta

All this SEO is great, but what if you want to generate dynamic SEO in an Twig template, for example on a Blog page where each blog entry should have different SEO Meta? SEOmatic makes it easy.
All this SEO is great, but what if you want to generate dynamic SEO in an Twig template, with custom or specific requirements that the SEOmatic FieldType can't handle? SEOmatic makes it easy.

SEOmatic populates your templates with the following global variables for SEO Meta:

Expand Down Expand Up @@ -303,6 +305,16 @@ You'll also get variables that are used to generate your Facebook Open Graph tag

When SEOmatic goes to render the `twitter` and `og` tags, it iterates through the respective arrays, so you can add, remove, or change any of the key-value pairs in the array, and they will be rendered. Just ensure that the `key` is a valid schema type for the respective meta tags.

You can even do fun things like:

{% set seomaticMeta = seomaticMeta | merge({'og':
{
'image': ['one.jpg', 'two.jpg', 'three.jpg']
}
}) %}

...and SEOmatic will output 3 `og:image` tags, one for each image in the array.

You can also change them all at once like this using the Twig [set](http://twig.sensiolabs.org/doc/tags/set.html) syntax:

{% set seomaticMeta = {
Expand Down Expand Up @@ -659,6 +671,21 @@ All three of these methods accomplish the same thing:

You shouldn't need to use truncateStringOnWord() for SEO Meta like `seoTitle` & `seoDescription` that have character limitations, because SEOmatic will truncate them for you automatically. However you may find this function handy for other purposes.

### extractTextFromMatrix()

All three of these methods accomplish the same thing:

{# Extract all and concatenate all of the text fields from a Matrix block using the 'extractTextFromMatrix' function #}
{{ extractTextFromMatrix( THEMATRIXBLOCK ) }}

{# Extract all and concatenate all of the text fields from a Matrix block using the 'extractTextFromMatrix' filter #}
{{ THEMATRIXBLOCK | extractTextFromMatrix() }}

{# Extract all and concatenate all of the text fields from a Matrix block using the 'extractTextFromMatrix' variable #}
{% do craft.seomatic. extractTextFromMatrix( THEMATRIXBLOCK ) %}

**THEMATRIXBLOCK** is the Matrix block to extract text from. It iterates through all of the 'Text' and 'Rich Text' fields in a Matrix block, and concatenates the text together for you. This is a useful precursor for the `extractKeywords()` function.

### encodeEmailAddress()

All three of these methods accomplish the same thing:
Expand Down
2 changes: 1 addition & 1 deletion SeomaticPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function getReleaseFeedUrl()

public function getVersion()
{
return '1.0.9';
return '1.0.10';
}

public function getSchemaVersion()
Expand Down
1 change: 1 addition & 0 deletions fieldtypes/Seomatic_MetaFieldType.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ public function getInputHtml($name, $value)
if ($field->fieldType->name == "Plain Text"
|| $field->fieldType->name == "Rich Text"
|| $field->fieldType->name == "Rich Text (Redactor I)"
|| $field->fieldType->name == "Matrix"
)
$fieldList[$field->handle] = $field->name;
if ($field->fieldType->name == "Assets")
Expand Down
14 changes: 13 additions & 1 deletion releases.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
[
{
"version": "1.0.10",
"downloadUrl": "https://github.com/khalwat/seomatic/archive/master.zip",
"date": "2016-01-12T11:00:00-05:00",
"notes": [
"[Added] You can now set Matrix blocks to be a Source for SEO FieldType Meta fields; it iterates through all text and rich text fields",
"[Added] Added a extractTextFromMatrix() Twig filter/function/variable for templating use",
"[Improved] The truncateStringOnWord() function does a much better job on comma-delimited keywords now",
"[Fixed] Fixed a regression that caused the 'New Template Meta' button to be broken",
"[Improved] Updated the README.md"
]
},
{
"version": "1.0.9",
"downloadUrl": "https://github.com/khalwat/seomatic/archive/master.zip",
"date": "2016-01-10T11:00:00-05:00",
"date": "2016-01-11T11:00:00-05:00",
"notes": [
"[Added] Added tokenized input fields for the keywords for the Site Meta, Template Meta, and Field Meta",
"[Added] You can now specify the position of the SEO Site Title relative to the Title",
Expand Down
6 changes: 0 additions & 6 deletions resources/js/meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,4 @@
$(function () {
$('#seoKeywords').tokenfield();

// Behavior for "New Meta" button
$('#new-meta').on('click', function () {
// Redirect
window.location = Craft.getUrl('seomatic/meta/new');
});

});
11 changes: 11 additions & 0 deletions resources/js/metalist.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// LOAD EVENTS

$(function () {

// Behavior for "New Meta" button
$('#new-meta').on('click', function () {
// Redirect
window.location = Craft.getUrl('seomatic/meta/new');
});

});
53 changes: 48 additions & 5 deletions services/SeomaticService.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ public function getMetaFromElement($element)

$entryMeta = null;
$entryMetaUrl = "";
if (isset($element) && $element)
if (isset($element) && $element && $element->getElementType() == ElementType::Entry)
{
$attributes = $element->content->attributes;
foreach ($attributes as $key => $value)
Expand All @@ -280,7 +280,11 @@ public function getMetaFromElement($element)
case 'field':
if (isset($element[$entryMeta['seoTitleSourceField']]))
{
$entryMeta['seoTitle'] = strip_tags($element[$entryMeta['seoTitleSourceField']]);
$srcField = $element[$entryMeta['seoTitleSourceField']];
if (isset($srcField->elementType) && $srcField->elementType->classHandle == ElementType::MatrixBlock)
$entryMeta['seoTitle'] = $this->extractTextFromMatrix($srcField);
else
$entryMeta['seoTitle'] = strip_tags($element[$entryMeta['seoTitleSourceField']]);
}
break;
}
Expand All @@ -290,7 +294,11 @@ public function getMetaFromElement($element)
case 'field':
if (isset($element[$entryMeta['seoDescriptionSourceField']]))
{
$entryMeta['seoDescription'] = strip_tags($element[$entryMeta['seoDescriptionSourceField']]);
$srcField = $element[$entryMeta['seoDescriptionSourceField']];
if (isset($srcField->elementType) && $srcField->elementType->classHandle == ElementType::MatrixBlock)
$entryMeta['seoDescription'] = $this->extractTextFromMatrix($srcField);
else
$entryMeta['seoDescription'] = strip_tags($element[$entryMeta['seoDescriptionSourceField']]);
}
break;
}
Expand All @@ -300,14 +308,22 @@ public function getMetaFromElement($element)
case 'field':
if (isset($element[$entryMeta['seoKeywordsSourceField']]))
{
$entryMeta['seoKeywords'] = strip_tags($element[$entryMeta['seoKeywordsSourceField']]);
$srcField = $element[$entryMeta['seoKeywordsSourceField']];
if (isset($srcField->elementType) && $srcField->elementType->classHandle == ElementType::MatrixBlock)
$entryMeta['seoKeywords'] = $this->extractTextFromMatrix($srcField);
else
$entryMeta['seoKeywords'] = strip_tags($element[$entryMeta['seoKeywordsSourceField']]);
}
break;

case 'keywords':
if (isset($element[$entryMeta['seoKeywordsSourceField']]))
{
$entryMeta['seoKeywords'] = craft()->seomatic->extractKeywords(strip_tags($element[$entryMeta['seoKeywordsSourceField']]));
$srcField = $element[$entryMeta['seoKeywordsSourceField']];
if (isset($srcField->elementType) && $srcField->elementType->classHandle == ElementType::MatrixBlock)
$entryMeta['seoKeywords'] = craft()->seomatic->extractKeywords($this->extractTextFromMatrix($srcField));
else
$entryMeta['seoKeywords'] = craft()->seomatic->extractKeywords(strip_tags($element[$entryMeta['seoKeywordsSourceField']]));
}
break;
}
Expand All @@ -329,6 +345,33 @@ public function getMetaFromElement($element)
return $entryMeta;
} /* -- getMetaFromElement */

/* --------------------------------------------------------------------------------
Extract text from a matrix field
-------------------------------------------------------------------------------- */

public function extractTextFromMatrix($matrixBlocks)
{
$result = "";
foreach ($matrixBlocks as $block)
{
$matrixBlockTypeModel = $block->getType();
$fields = $matrixBlockTypeModel->getFields();

foreach ($fields as $field)
{
if ($field->fieldType->name == "Plain Text"
|| $field->fieldType->name == "Rich Text"
|| $field->fieldType->name == "Rich Text (Redactor I)"
)
{
$result .= strip_tags($block[$field->handle]) . " ";
}
}

}
return $result;
} /* -- extractTextFromMatrix */

/* --------------------------------------------------------------------------------
Set the entry-level meta
-------------------------------------------------------------------------------- */
Expand Down
68 changes: 34 additions & 34 deletions templates/_facebookDisplayPreview.html
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
{% if seomaticMeta.og is defined and seomaticMeta.og %}
<div class="Facebook-container">
<div class="">
<a href="{{ seomaticMeta.canonicalUrl }}" target="_blank">
<div class="Facebook-image-wrap">
<div id="facebook-post-image" class="Facebook-post-image" style="width:470px;height:246px;">
</div>
</div>
</a>
</div>
<div class="facebook-text-wrap">
<div class="Facebook-text-container">
<div class="Facebook-headline">
<a class="Facebook-link" href="{{ seomaticMeta.canonicalUrl }}" target="_blank">{{ seomaticMeta.og.title |raw }}</a>
</div>
<div class="Facebook-text">
{{ seomaticMeta.og.description |raw }}
</div>
<div class="Facebook-site-url-wrapper">
<div>
<div class="Facebook-site-url">
{{ siteUrl | replace({'http://': ''}) | replace({'https://': ''}) | replace({'/': ''}) }}
</div>
</div>
</div>
</div>
</div>
<div class="">
<a href="{{ seomaticMeta.canonicalUrl }}" target="_blank">
<div class="Facebook-image-wrap">
<div id="facebook-post-image" class="Facebook-post-image" style="width:470px;height:246px;">
</div>
</div>
</a>
</div>
<div class="facebook-text-wrap">
<div class="Facebook-text-container">
<div class="Facebook-headline">
<a class="Facebook-link" href="{{ seomaticMeta.canonicalUrl }}" target="_blank">{{ seomaticMeta.og.title |raw }}</a>
</div>
<div class="Facebook-text">
{{ seomaticMeta.og.description |raw }}
</div>
<div class="Facebook-site-url-wrapper">
<div>
<div class="Facebook-site-url">
{{ siteUrl | replace({'http://': ''}) | replace({'https://': ''}) | replace({'/': ''}) }}
</div>
</div>
</div>
</div>
</div>
</div>

<script type="text/javascript">
var x = document.getElementsByClassName("Facebook-post-image");
var i;
for (i = 0; i < x.length; i++) {
x[i].style.backgroundImage = "url(" + "{{ resourceUrl('seomatic/images/no_image_set.png') }}" + ")";
{% if seomaticMeta.og.image %}
x[i].style.backgroundImage = "url(" + "{{ seomaticMeta.seoImage }}" + ")";
{% endif %}
}
var x = document.getElementsByClassName("Facebook-post-image");
var i;
for (i = 0; i < x.length; i++) {
x[i].style.backgroundImage = "url(" + "{{ resourceUrl('seomatic/images/no_image_set.png') }}" + ")";
{% if seomaticMeta.og.image %}
x[i].style.backgroundImage = "url(" + "{{ seomaticMeta.seoImage }}" + ")";
{% endif %}
}
</script>
{% else %}
<p>No Facebook Profile ID has been set</p>
Expand Down
2 changes: 1 addition & 1 deletion templates/meta/index.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{% extends '_layouts/elementindex' %}

{% includeCssResource "seomatic/css/style.css" %}
{% includeJsResource "seomatic/js/meta.js" %}
{% includeJsResource "seomatic/js/metalist.js" %}

{% set title = "SEOmatic"|t %}
{% set docsUrl = "https://github.com/khalwat/seomatic/blob/master/README.md" %}
Expand Down
13 changes: 13 additions & 0 deletions twigextensions/SeomaticTwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public function getFilters()
'extractSummary' => new \Twig_Filter_Method($this, 'extractSummary'),
'truncateStringOnWord' => new \Twig_Filter_Method($this, 'truncateStringOnWord'),
'encodeEmailAddress' => new \Twig_Filter_Method($this, 'encodeEmailAddress'),
'extractTextFromMatrix' => new \Twig_Filter_Method($this, 'extractTextFromMatrix'),
);
}

Expand All @@ -66,6 +67,7 @@ public function getFunctions()
'extractSummary' => new \Twig_Function_Method($this, 'extractSummary'),
'truncateStringOnWord' => new \Twig_Function_Method($this, 'truncateStringOnWord'),
'encodeEmailAddress' => new \Twig_Function_Method($this, 'encodeEmailAddress'),
'extractTextFromMatrix' => new \Twig_Function_Method($this, 'extractTextFromMatrix'),
);
}

Expand Down Expand Up @@ -156,6 +158,17 @@ public function encodeEmailAddress($emailAddress)
return $result;
} /* -- encodeEmailAddress */

/* --------------------------------------------------------------------------------
Extract all of the text and rich text from the fields in MatrixBlockModels
-------------------------------------------------------------------------------- */

public function extractTextFromMatrix($matrixBlocks)
{
$result = craft()->seomatic->extractTextFromMatrix($matrixBlocks);

return $result;
} /* -- extractTextFromMatrix */

/* --------------------------------------------------------------------------------
Get the current template path
-------------------------------------------------------------------------------- */
Expand Down
11 changes: 11 additions & 0 deletions variables/SeomaticVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,17 @@ public function encodeEmailAddress($emailAddress)
return $result;
} /* -- encodeEmailAddress */

/* --------------------------------------------------------------------------------
Extract all of the text and rich text from the fields in MatrixBlockModels
-------------------------------------------------------------------------------- */

public function extractTextFromMatrix($matrixBlocks)
{
$result = craft()->seomatic->extractTextFromMatrix($matrixBlocks);

return $result;
} /* -- extractTextFromMatrix */

/* ================================================================================
INTERNAL methods for SEOmatic use
================================================================================ */
Expand Down

0 comments on commit a7fbf4a

Please sign in to comment.