findAll(); echo "

Pick a form to export!

"; 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_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); $template = htmlspecialchars($template); 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; } /** * @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); } }