findBy(['deletedAt' => null]);
echo "
Pick a form to export!
";
echo "";
foreach ($forms as $form) {
/** @var FormEntity $form */
$name = $form->getName() ?: '(no name)';
$exportUrl = menu_page_url('export-forms', false) . '&exportId=' . $form->getId();
echo "- $name (ID: {$form->getId()})
";
}
echo "
";
}
function mailpoetExportForm(int $id) {
/** @var FormEntity $form */
$form = mailpoetGetFormsRepository()->findOneById($id);
if (!$form) {
die('Meh! Wrong id!');
}
$template = file_get_contents(__DIR__ . '/Template.php', false);
$template = str_replace('TEMPLATE_BODY', mailpoetVarExport($form->getBody()), $template);
$settings = $form->getSettings();
$settings['success_message'] = '';
$settings['segments'] = [];
$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_STYLES', $form->getStyles(), $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 = mailpoetAddStringTranslations($template);
list($template, $assetUrls) = mailpoetProcessAssets($template);
$template = htmlspecialchars($template);
echo "";
if (!$assetUrls) {
die;
}
echo "Assets to download
";
echo "";
foreach ($assetUrls as $url) {
echo "- $url
";
}
echo "";
die;
}
function mailpoetGetFormsRepository(): FormsRepository {
if (!class_exists(ContainerWrapper::class)) {
die('MailPoet plugin must be active!');
}
return ContainerWrapper::getInstance()->get(FormsRepository::class);
}
function mailpoetAddStringTranslations(string $template): string {
// Replace label translations
$matches = [];
preg_match_all("/'label' => '(.+)'/u", $template, $matches);
foreach ($matches[0] as $key => $fullMatch) {
$stringToTranslate = $matches[1][$key];
$template = str_replace($fullMatch, "'label' => _x('$stringToTranslate', 'Form label', 'mailpoet')", $template);
}
// Replace paragraph and heading contents with translations
$matches = [];
preg_match_all("/'content' => '(.+)'/u", $template, $matches);
foreach ($matches[0] as $key => $fullMatch) {
$stringToTranslate = $matches[1][$key];
$template = str_replace($fullMatch, "'content' => _x('$stringToTranslate', 'Text in a web form. Keep HTML tags!', 'mailpoet')", $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
*/
function mailpoetVarExport($var, $indent=" "): string {
switch (gettype($var)) {
case 'string':
return '\'' . addcslashes($var, "\\\$'\r\n\t\v\f") . '\'';
case 'array':
$indexed = array_keys($var) === range(0, count($var) - 1);
$r = [];
foreach ($var as $key => $value) {
$r[] = "$indent "
. ($indexed ? "" : mailpoetVarExport($key) . " => ")
. mailpoetVarExport($value, "$indent ");
}
if (count($r) === 0) {
return '[]';
}
return "[\n" . implode(",\n", $r) . ",\n" . $indent . "]";
case 'boolean':
return $var ? 'true' : 'false';
default:
return var_export($var, true);
}
}