From d311e880f47fe996e66d4db118859d30c2456578 Mon Sep 17 00:00:00 2001 From: Chris Tombleson Date: Wed, 12 Aug 2020 11:46:55 +1200 Subject: [PATCH 1/2] SilverStripe 4 support --- .editorconfig | 0 LICENSE.md | 0 _config.php | 1 - _config/config.yml | 5 +- changelog.md | 0 {images => client/images}/opengraph.png | Bin composer.json | 16 ++++- contributing.md | 0 .../Extensions}/OpenGraphMeta.php | 56 +++++++++++++----- .../OpenGraphSiteConfigExtension.php | 2 + .../Tasks}/MigrateSiteTreeOpenGraphTask.php | 12 +++- 11 files changed, 68 insertions(+), 24 deletions(-) mode change 100755 => 100644 .editorconfig mode change 100755 => 100644 LICENSE.md mode change 100755 => 100644 changelog.md rename {images => client/images}/opengraph.png (100%) mode change 100755 => 100644 contributing.md rename {code/extensions => src/Extensions}/OpenGraphMeta.php (68%) rename {code/extensions => src/Extensions}/OpenGraphSiteConfigExtension.php (96%) rename {code/tasks => src/Tasks}/MigrateSiteTreeOpenGraphTask.php (91%) diff --git a/.editorconfig b/.editorconfig old mode 100755 new mode 100644 diff --git a/LICENSE.md b/LICENSE.md old mode 100755 new mode 100644 diff --git a/_config.php b/_config.php index 31e7d5b..a4abe2d 100644 --- a/_config.php +++ b/_config.php @@ -1,3 +1,2 @@ addFieldToTab('Root.Main', ToggleCompositeField::create('OpenGraph', 'Open Graph', - [ - LiteralField::create('', '

   Open Graph Information

'), - TextField::create('OGTitle', '') - ->setAttribute('placeholder', 'e.g My Website') - ->setRightTitle('Page title goes here, automatically defaults to the page title'), - DropdownField::create('OGContent', 'Content Type', $this->owner->dbObject('OGContent')->enumValues()) - ->setRightTitle('Will default to website (the most common open graph object type'), - TextareaField::create('OGDescription', '') - ->setRightTitle('Page description goes here, automatically defaults to the content summary'), - UploadField::create('OGImage', 'Open Graph Image') - ->setDescription('Ideal size: 1200px * 630px') - ] - )); + $imageUrl = ModuleResourceLoader::singleton() + ->resolveUrl('toastnz/open-graph-meta:client/images/opengraph.png'); + + $headerHtml = sprintf( + '

   Open Graph Information

', + $imageUrl + ); + + $fields->addFieldToTab( + 'Root.Main', + ToggleCompositeField::create( + 'OpenGraph', + 'Open Graph', + [ + LiteralField::create('', $headerHtml), + TextField::create('OGTitle', '') + ->setAttribute('placeholder', 'e.g My Website') + ->setRightTitle('Page title goes here, automatically defaults to the page title'), + DropdownField::create( + 'OGContent', + 'Content Type', + $this->owner->dbObject('OGContent')->enumValues() + )->setRightTitle('Will default to website (the most common open graph object type'), + TextareaField::create('OGDescription', '') + ->setRightTitle( + 'Page description goes here, automatically defaults to the content summary' + ), + UploadField::create('OGImage', 'Open Graph Image') + ->setDescription('Ideal size: 1200px * 630px') + ] + ) + ); } public function FirstImage() { $pattern = ' /]+ src[\\s = \'"]'; $pattern .= '+([^"\'>\\s]+)/is'; + if (preg_match_all($pattern, $this->owner->Content, $match)) { $imageLink = preg_replace('/_resampled\/resizedimage[0-9]*-/', '', $match[1][0]); return (string)$imageLink; @@ -129,7 +150,10 @@ public function MetaTags(&$tags) if (trim($this->owner->OGDescription)) { $tags .= sprintf('', $this->owner->OGDescription) . "\n"; } elseif ($this->owner->Content) { - $tags .= sprintf('', $this->owner->dbObject('Content')->FirstParagraph()) . "\n"; + $tags .= sprintf( + '', + $this->owner->dbObject('Content')->FirstParagraph() + ) . "\n"; } // Image diff --git a/code/extensions/OpenGraphSiteConfigExtension.php b/src/Extensions/OpenGraphSiteConfigExtension.php similarity index 96% rename from code/extensions/OpenGraphSiteConfigExtension.php rename to src/Extensions/OpenGraphSiteConfigExtension.php index 06dc2dd..797ee1a 100644 --- a/code/extensions/OpenGraphSiteConfigExtension.php +++ b/src/Extensions/OpenGraphSiteConfigExtension.php @@ -1,5 +1,7 @@ Title . $this->eol; foreach ($this->fields_to_update as $fieldName) { - $oldData = DB::query("SELECT {$fieldName} FROM Page WHERE ID = {$id}")->column($fieldName); + $oldData = DB::query("SELECT {$fieldName} FROM Page WHERE ID = {$id}") + ->column($fieldName); - $newData = DB::query("SELECT {$fieldName} FROM SiteTree WHERE ID = {$id}")->column($fieldName); + $newData = DB::query("SELECT {$fieldName} FROM SiteTree WHERE ID = {$id}") + ->column($fieldName); if (!empty($oldData)) { // If new data has been saved and we don't want to overwrite, exit the loop if (!empty($newData) && !$overwrite) { continue; } + $data = Convert::raw2sql($oldData[0]); + DB::query("UPDATE SiteTree SET {$fieldName} = '{$data}' WHERE ID = {$id}"); DB::query("UPDATE SiteTree_Live SET {$fieldName} = '{$data}' WHERE ID = {$id}"); } else { From cca3ee020492bf01a036acbbc7a330c88e0765aa Mon Sep 17 00:00:00 2001 From: Chris Tombleson Date: Wed, 12 Aug 2020 12:50:06 +1200 Subject: [PATCH 2/2] Added missing use call --- src/Tasks/MigrateSiteTreeOpenGraphTask.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Tasks/MigrateSiteTreeOpenGraphTask.php b/src/Tasks/MigrateSiteTreeOpenGraphTask.php index d049ee3..4e3c7d8 100644 --- a/src/Tasks/MigrateSiteTreeOpenGraphTask.php +++ b/src/Tasks/MigrateSiteTreeOpenGraphTask.php @@ -6,6 +6,7 @@ use SilverStripe\ORM\DB; use SilverStripe\Core\Convert; use SilverStripe\Dev\BuildTask; +use Page; /** * Class MigrateSiteTreeOpenGraphTask