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

Single landing page scripts #9695

Merged
merged 10 commits into from
Feb 4, 2022
68 changes: 68 additions & 0 deletions app/bundles/PageBundle/Entity/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,16 @@ class Page extends FormEntity implements TranslationEntityInterface, VariantEnti
*/
private $metaDescription;

/**
* @var string
*/
private $headScript;

/**
* @var string
*/
private $footerScript;

/**
* @var string
*/
Expand Down Expand Up @@ -188,6 +198,16 @@ public static function loadMetadata(ORM\ClassMetadata $metadata)
->nullable()
->build();

$builder->createField('headScript', 'text')
->columnName('head_script')
->nullable()
->build();

$builder->createField('footerScript', 'text')
->columnName('footer_script')
->nullable()
->build();

$builder->createField('redirectType', 'string')
->columnName('redirect_type')
->nullable()
Expand Down Expand Up @@ -521,6 +541,54 @@ public function getMetaDescription()
return $this->metaDescription;
}

/**
* Set headScript.
*
* @param string $headScript
*
* @return Page
*/
public function setHeadScript($headScript)
{
$this->headScript = $headScript;

return $this;
}

/**
* Get headScript.
*
* @return string
*/
public function getHeadScript()
{
return $this->headScript;
}

/**
* Set footerScript.
*
* @param string $footerScript
*
* @return Page
*/
public function setFooterScript($footerScript)
{
$this->footerScript = $footerScript;

return $this;
}

/**
* Get footerScript.
*
* @return string
*/
public function getFooterScript()
{
return $this->footerScript;
}

/**
* Set redirectType.
*
Expand Down
10 changes: 10 additions & 0 deletions app/bundles/PageBundle/EventListener/BuilderSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,16 @@ public function onPageDisplay(Events\PageDisplayEvent $event)
$content = str_ireplace(array_keys($tokens), $tokens, $content);
}

$headCloseScripts = $page->getHeadScript();
if ($headCloseScripts) {
$content = str_ireplace('</head>', $headCloseScripts."\n</head>", $content);
}

$bodyCloseScripts = $page->getFooterScript();
if ($bodyCloseScripts) {
$content = str_ireplace('</body>', $bodyCloseScripts."\n</body>", $content);
}

$event->setContent($content);
}

Expand Down
30 changes: 29 additions & 1 deletion app/bundles/PageBundle/Form/Type/PageType.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function __construct(
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->addEventSubscriber(new CleanFormSubscriber(['content' => 'html', 'customHtml' => 'html', 'redirectUrl' => 'url']));
$builder->addEventSubscriber(new CleanFormSubscriber(['content' => 'html', 'customHtml' => 'html', 'redirectUrl' => 'url', 'headScript' => 'html', 'footerScript' => 'html']));
$builder->addEventSubscriber(new FormExitSubscriber('page.page', $options));

$builder->add(
Expand Down Expand Up @@ -261,6 +261,34 @@ function (FormEvent $event) use ($formModifier) {
]
);

$builder->add(
'headScript',
TextareaType::class,
[
'label' => 'mautic.page.form.headscript',
'label_attr' => ['class' => 'control-label'],
'attr' => [
'class' => 'form-control',
'rows' => '8',
],
'required' => false,
]
);

$builder->add(
'footerScript',
TextareaType::class,
[
'label' => 'mautic.page.form.footerscript',
'label_attr' => ['class' => 'control-label'],
'attr' => [
'class' => 'form-control',
'rows' => '8',
],
'required' => false,
]
);

$builder->add(
'redirectType',
RedirectListType::class,
Expand Down
36 changes: 36 additions & 0 deletions app/bundles/PageBundle/Tests/Entity/PageTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
<?php

declare(strict_types=1);
/*
* @copyright 2021 Mautic Contributors. All rights reserved
* @author Mautic, Inc.
*
* @link https://mautic.org
*
* @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
*/

namespace Mautic\PageBundle\Tests\Entity;

Expand Down Expand Up @@ -52,4 +60,32 @@ public function setNoIndexDataProvider(): iterable
yield [0, false, ['noIndex' => [null, false]]];
yield ['string', true, ['noIndex' => [null, true]]];
}

/**
* Test setHeadScript and getHeadScript.
*
* @return void
*/
public function testSetHeadScript()
{
$script = '<script>console.log("test")';
$page = new Page();
$page->setHeadScript($script);

$this->assertEquals($script, $page->getHeadScript());
}

/**
* Test setFooterScript and getFooterScript.
*
* @return void
*/
public function testSetFooterScript()
{
$script = '<script>console.log("test")';
$page = new Page();
$page->setFooterScript($script);

$this->assertEquals($script, $page->getFooterScript());
}
}
2 changes: 2 additions & 0 deletions app/bundles/PageBundle/Translations/en_US/messages.ini
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ mautic.page.form.customhtml="Custom HTML Content"
mautic.page.form.customhtml.help="Design the entire landing page using custom HTML"
mautic.page.form.language.help="Translate the content into multiple languages and link them together by assigning them to a parent page. Language selection links will become available on the related landing pages for customers to choose the language they want to see."
mautic.page.form.metadescription="Meta Description"
mautic.page.form.headscript="Script inside Head"
mautic.page.form.footerscript="Script inside Footer"
mautic.page.form.template.help="Choose the theme that will give the landing page it's look and feel. Then use the Page Builder to fill in the contents. WARNING: Changing the theme after building the page may cause content to not display if the two themes do not use the same placeholders."
mautic.page.form.redirecttype="Redirect type"
mautic.page.form.redirecttype.help="Choose the redirect type (301 permanent, 302 temporary, none)"
Expand Down
7 changes: 6 additions & 1 deletion app/bundles/PageBundle/Views/Page/form.html.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,12 @@
<div class="template-fields<?php echo (!$template) ? ' hide"' : ''; ?>">
<?php echo $view['form']->row($form['metaDescription']); ?>
</div>

<div class="template-fields<?php echo (!$template) ? ' hide"' : ''; ?>">
<?php echo $view['form']->row($form['headScript']); ?>
</div>
<div class="template-fields<?php echo (!$template) ? ' hide"' : ''; ?>">
<?php echo $view['form']->row($form['footerScript']); ?>
</div>
<div class="hide">
<?php echo $view['form']->rest($form); ?>
</div>
Expand Down
36 changes: 36 additions & 0 deletions app/migrations/Version20210203143600.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

/*
* @copyright 2021 Mautic Contributors. All rights reserved.
* @author CTMobi
* @link https://mautic.org
* @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
*/

namespace Mautic\Migrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\Exception\SkipMigration;
use Mautic\CoreBundle\Doctrine\AbstractMauticMigration;

final class Version20210203143600 extends AbstractMauticMigration
{
/**
* @throws SkipMigration
*/
public function preUp(Schema $schema): void
{
$pagesTable = $schema->getTable(MAUTIC_TABLE_PREFIX.'pages');
if ($pagesTable->hasColumn('head_script') && $pagesTable->hasColumn('footer_script')) {
throw new SkipMigration('Schema includes this migration');
}
}

public function up(Schema $schema): void
{
$this->addSql("ALTER TABLE {$this->prefix}pages ADD head_script LONGTEXT DEFAULT NULL");
$this->addSql("ALTER TABLE {$this->prefix}pages ADD footer_script LONGTEXT DEFAULT NULL");
}
}