Skip to content

Commit

Permalink
Upgrade fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Tam committed Jan 21, 2019
1 parent 8434eff commit b9f06ca
Show file tree
Hide file tree
Showing 7 changed files with 236 additions and 138 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
## 3.5.1 - 2019-01-21

### Added
- Added console command to manually trigger the upgrade to the new data format
`./craft seo/upgrade/to-new-data-format`.

### Fixed
- Fixed issue when updating an SEO field without a suffix.
- Fixed readonly property error when running the upgrade task.

## 3.5.0 - 2019-01-21

> {warning} This update changes how SEO meta is stored. We **STRONGLY**
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "ether/seo",
"license": "MIT",
"description": "SEO utilities including a unique field type, sitemap, & redirect manager",
"version": "3.5.0",
"version": "3.5.1",
"type": "craft-plugin",
"minimum-stability": "dev",
"require": {
Expand Down
4 changes: 3 additions & 1 deletion src/Seo.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use ether\seo\services\RedirectsService;
use ether\seo\services\SeoService;
use ether\seo\services\SitemapService;
use ether\seo\services\UpgradeService;
use ether\seo\web\twig\Extension;
use ether\seo\web\twig\Variable;
use yii\base\Event;
Expand All @@ -33,6 +34,7 @@
* @property SeoService $seo
* @property SitemapService $sitemap
* @property RedirectsService $redirects
* @property UpgradeService $upgrade
*/
class Seo extends Plugin
{
Expand All @@ -43,7 +45,6 @@ class Seo extends Plugin
/** @var Seo */
public static $i;

public $controllerNamespace = 'ether\\seo\\controllers';
public $hasCpSection = true;
public $hasCpSettings = true;

Expand Down Expand Up @@ -73,6 +74,7 @@ public function init ()
'seo' => SeoService::class,
'sitemap' => SitemapService::class,
'redirects' => RedirectsService::class,
'upgrade' => UpgradeService::class,
]);

// Events
Expand Down
34 changes: 34 additions & 0 deletions src/console/controllers/UpgradeController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
/**
* SEO for Craft
*
* @link https://ethercreative.co.uk
* @copyright Copyright (c) 2019 Ether Creative
*/

namespace ether\seo\console\controllers;

use yii\console\Controller;
use yii\console\ExitCode;
use ether\seo\Seo;

/**
* Class UpgradeController
*
* @author Ether Creative
* @package ether\seo\console\controllers
*/
class UpgradeController extends Controller
{

/**
* Triggers the upgrade to the new data format
*/
public function actionToNewDataFormat ()
{
Seo::getInstance()->upgrade->toNewDataFormat();

return ExitCode::OK;
}

}
138 changes: 2 additions & 136 deletions src/migrations/m190114_152300_upgrade_to_new_data_format.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
namespace ether\seo\migrations;

use craft\db\Migration;
use craft\db\Query;
use ether\seo\fields\SeoField;
use ether\seo\jobs\UpgradeSeoDataJob;
use ether\seo\Seo;

/**
* Class m190114_152300_upgrade_to_new_data_format
Expand All @@ -24,144 +22,12 @@ class m190114_152300_upgrade_to_new_data_format extends Migration

public function safeUp ()
{
$craft = \Craft::$app;

// 1. For each SEO field, update the settings to the template
$fields = $this->_getAllSeoFields();

$fields = array_map(function ($field) use ($craft) {
return $craft->fields->createField($field);
}, $fields);
$suffixesByFieldHandle = [];

$siteName = $craft->sites->primarySite->name;

/** @var SeoField $field */
foreach ($fields as $field) if ($field->titleSuffix !== null)
{
// If `- Current Prefix` contains the site name, replace with `{{siteName}}`
$suffix = $field->titleSuffix;

if (strpos($suffix, $siteName))
$suffix = str_replace($siteName, '{{siteName}}', $suffix);

$field->title = [
[
'key' => '1',
'template' => '{title}',
'locked' => false,
]
];

if ($suffix !== '')
{
$field->title[] = [
'key' => '2',
'template' => ' ' . $suffix,
'locked' => true,
];

$suffixesByFieldHandle[$field->handle] = ' ' . $field->titleSuffix;
}
else
{
$suffixesByFieldHandle[$field->handle] = '';
}

// '[{title}] [- Current Prefix]' (or flipped if suffixAsPrefix is true)
if ($field->suffixAsPrefix && $suffix !== '')
{
$field->title = array_reverse($field->title, false);
$field->title[0]['key'] = '1';
$field->title[1]['key'] = '2';
$field->title[1]['template'] = $suffix . ' ';

$suffixesByFieldHandle[$field->handle] = $field->titleSuffix . ' ';
}

$craft->fields->saveField($field);
}

// 2. Queue re-save of all elements that have an SEO field
$layouts = $this->_getAllElementTypesAndIdsThatHaveSEOFields();

foreach ($layouts as $type => $data)
{
$craft->queue->push(new UpgradeSeoDataJob([
'elementType' => $type,
'criteria' => [
'id' => $data['ids']
],
'handle' => $data['handle'],
'suffix' => $suffixesByFieldHandle[$data['handle']],
]));
}
Seo::getInstance()->upgrade->toNewDataFormat();
}

public function safeDown ()
{
return false;
}

// Helpers
// =========================================================================

private function _getAllSeoFields ()
{
return (new Query())
->select(
[
'fields.id',
'fields.dateCreated',
'fields.dateUpdated',
'fields.groupId',
'fields.name',
'fields.handle',
'fields.context',
'fields.instructions',
'fields.translationMethod',
'fields.translationKeyFormat',
'fields.type',
'fields.settings',
]
)
->from(['{{%fields}} fields'])
->where(['type' => SeoField::class])
->orderBy(['fields.name' => SORT_ASC, 'fields.handle' => SORT_ASC])
->all();
}

private function _getAllElementTypesAndIdsThatHaveSEOFields ()
{
$results = (new Query())
->select(['el.type', 'el.id', 'fields.handle'])
->from(['{{%fields}} fields'])
->innerJoin(
'{{%fieldlayoutfields}} flf',
'[[flf.fieldId]] = [[fields.id]]'
)
->innerJoin(
'{{%fieldlayouts}} fl',
'[[fl.id]] = [[flf.layoutId]]'
)
->innerJoin(
'{{%elements}} el',
'[[el.fieldLayoutId]] = [[flf.layoutId]] AND [[el.type]] = [[fl.type]]'
)
->where(['fields.type' => SeoField::class])
->all();

return array_reduce($results, function ($a, $b) {
if (!array_key_exists($b['type'], $a))
$a[$b['type']] = [
'ids' => [],
'handle' => $b['handle'],
];

$a[$b['type']]['ids'][] = $b['id'];

return $a;
}, []);
}

}
9 changes: 9 additions & 0 deletions src/models/data/SeoData.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,15 @@ public function __construct (SeoField $seo = null, ElementInterface $element = n
unset($config['title']);
}

if (array_key_exists('title', $config))
{
$config['titleRaw'] = $config['title'];
unset($config['title']);
}

if (array_key_exists('titleRaw', $config) && is_string($config['titleRaw']))
$config['titleRaw'] = [$config['titleRaw']];

// Backwards compatibility for descriptions in SEO v3.4.x or lower
if (isset($config['description']))
{
Expand Down
Loading

0 comments on commit b9f06ca

Please sign in to comment.