Update plugin for form exports to support assets
[MAILPOET-3076]
This commit is contained in:
committed by
Veljko V
parent
d5a840aa2f
commit
3adb20dd23
@@ -7,6 +7,9 @@ use MailPoet\Form\Templates\FormTemplate;
|
|||||||
class Template extends FormTemplate {
|
class Template extends FormTemplate {
|
||||||
const ID = 'TEMPLATE_ID';
|
const ID = 'TEMPLATE_ID';
|
||||||
|
|
||||||
|
/** @var string */
|
||||||
|
protected $assetsDirectory = 'TEMPLATE_ASSETS_DIR';
|
||||||
|
|
||||||
public function getName(): string {
|
public function getName(): string {
|
||||||
return 'TEMPLATE_NAME';
|
return 'TEMPLATE_NAME';
|
||||||
}
|
}
|
||||||
|
@@ -32,7 +32,7 @@ function mailpoetExportForms() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function mailpoetRenderFormList() {
|
function mailpoetRenderFormList() {
|
||||||
$forms = mailpoetGetFormsRepository()->findAll();
|
$forms = mailpoetGetFormsRepository()->findBy(['deletedAt' => null]);
|
||||||
echo "<h1>Pick a form to export!</h1>";
|
echo "<h1>Pick a form to export!</h1>";
|
||||||
echo "<ul>";
|
echo "<ul>";
|
||||||
foreach ($forms as $form) {
|
foreach ($forms as $form) {
|
||||||
@@ -56,15 +56,26 @@ function mailpoetExportForm(int $id) {
|
|||||||
$settings['success_message'] = '';
|
$settings['success_message'] = '';
|
||||||
$settings['segments'] = [];
|
$settings['segments'] = [];
|
||||||
$template = str_replace('TEMPLATE_ID', strtolower(preg_replace("/[^A-Za-z0-9]/", '_', $form->getName())), $template);
|
$template = str_replace('TEMPLATE_ID', strtolower(preg_replace("/[^A-Za-z0-9]/", '_', $form->getName())), $template);
|
||||||
|
$template = str_replace('TEMPLATE_ASSETS_DIR', strtolower(preg_replace("/[^A-Za-z0-9]/", '-', $form->getName())), $template);
|
||||||
$template = str_replace('TEMPLATE_SETTINGS', mailpoetVarExport($settings), $template);
|
$template = str_replace('TEMPLATE_SETTINGS', mailpoetVarExport($settings), $template);
|
||||||
$template = str_replace('TEMPLATE_STYLES', $form->getStyles(), $template);
|
$template = str_replace('TEMPLATE_STYLES', $form->getStyles(), $template);
|
||||||
$template = str_replace('TEMPLATE_NAME', $form->getName(), $template);
|
$template = str_replace('TEMPLATE_NAME', $form->getName(), $template);
|
||||||
$template = str_replace('class Template', 'class ' . preg_replace("/[^A-Za-z0-9]/", '', $form->getName()), $template);
|
$template = str_replace('class Template', 'class ' . preg_replace("/[^A-Za-z0-9]/", '', $form->getName()), $template);
|
||||||
|
|
||||||
$template = mailpoetAddStringTranslations($template);
|
$template = mailpoetAddStringTranslations($template);
|
||||||
|
list($template, $assetUrls) = mailpoetProcessAssets($template);
|
||||||
|
|
||||||
$template = htmlspecialchars($template);
|
$template = htmlspecialchars($template);
|
||||||
echo "<textarea style=\"width:90%;height:90vh;\">$template</textarea>";
|
echo "<textarea style=\"width:90%;height:80vh;\">$template</textarea>";
|
||||||
|
if (!$assetUrls) {
|
||||||
|
die;
|
||||||
|
}
|
||||||
|
echo "<h3>Assets to download</h3>";
|
||||||
|
echo "<ul style=\"width:90%;height:10vh;\">";
|
||||||
|
foreach ($assetUrls as $url) {
|
||||||
|
echo "<li><a href='$url' target='_blank'>$url</a></li>";
|
||||||
|
}
|
||||||
|
echo "</url>";
|
||||||
die;
|
die;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -93,6 +104,48 @@ function mailpoetAddStringTranslations(string $template): string {
|
|||||||
return $template;
|
return $template;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function mailpoetProcessAssets(string $template): array {
|
||||||
|
$assetUrls = [];
|
||||||
|
// background image urls
|
||||||
|
$matches = [];
|
||||||
|
preg_match_all("/'background_image_url' => '(.+)'/u", $template, $matches);
|
||||||
|
foreach ($matches[0] as $key => $fullMatch) {
|
||||||
|
list($assetCode, $url) = mailpoetReplaceAssetUrl($matches[1][$key]);
|
||||||
|
if (!$assetCode) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$assetUrls[] = $url;
|
||||||
|
$template = str_replace($fullMatch, "'background_image_url' => $assetCode", $template);
|
||||||
|
}
|
||||||
|
// Urls in url property (e.g. image block url)
|
||||||
|
$matches = [];
|
||||||
|
preg_match_all("/'url' => '(.+)'/u", $template, $matches);
|
||||||
|
foreach ($matches[0] as $key => $fullMatch) {
|
||||||
|
list($assetCode, $url) = mailpoetReplaceAssetUrl($matches[1][$key]);
|
||||||
|
if (!$assetCode) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$assetUrls[] = $url;
|
||||||
|
$template = str_replace($fullMatch, "'url' => $assetCode", $template);
|
||||||
|
}
|
||||||
|
return [$template, $assetUrls];
|
||||||
|
}
|
||||||
|
|
||||||
|
function mailpoetReplaceAssetUrl(string $url): array {
|
||||||
|
$assetFile = basename(parse_url($url, PHP_URL_PATH));
|
||||||
|
$siteUrl = get_site_url();
|
||||||
|
// Don't touch urls from different site
|
||||||
|
if (strpos($url, $siteUrl) === false) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
// Don't touch url with non-image file extension
|
||||||
|
$ext = strtolower(pathinfo($assetFile, PATHINFO_EXTENSION));
|
||||||
|
if (in_array($ext, ['gif', 'jpg', 'jpeg', 'png']) === false) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
return ['$this->getAssetUrl(\'' . $assetFile . '\')', $url];
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @see https://stackoverflow.com/questions/24316347/how-to-format-var-export-to-php5-4-array-syntax
|
* @see https://stackoverflow.com/questions/24316347/how-to-format-var-export-to-php5-4-array-syntax
|
||||||
*/
|
*/
|
||||||
|
Reference in New Issue
Block a user