Templates
3 years ago
FormTemplate.php
2 years ago
TemplateRepository.php
3 years ago
index.php
3 years ago
FormTemplate.php
169 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\Form\Templates; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Entities\FormEntity; |
| 9 | use MailPoet\Settings\SettingsController; |
| 10 | use MailPoet\Util\CdnAssetUrl; |
| 11 | use MailPoet\Util\Helpers; |
| 12 | use MailPoet\WP\Functions as WPFunctions; |
| 13 | |
| 14 | abstract class FormTemplate { |
| 15 | const DEFAULT_STYLES = <<<EOL |
| 16 | /* form */ |
| 17 | .mailpoet_form { |
| 18 | } |
| 19 | |
| 20 | /* columns */ |
| 21 | .mailpoet_column_with_background { |
| 22 | padding: 10px; |
| 23 | } |
| 24 | /* space between columns */ |
| 25 | .mailpoet_form_column:not(:first-child) { |
| 26 | margin-left: 20px; |
| 27 | } |
| 28 | |
| 29 | /* input wrapper (label + input) */ |
| 30 | .mailpoet_paragraph { |
| 31 | line-height:20px; |
| 32 | margin-bottom: 20px; |
| 33 | } |
| 34 | |
| 35 | /* labels */ |
| 36 | .mailpoet_segment_label, |
| 37 | .mailpoet_text_label, |
| 38 | .mailpoet_textarea_label, |
| 39 | .mailpoet_select_label, |
| 40 | .mailpoet_radio_label, |
| 41 | .mailpoet_checkbox_label, |
| 42 | .mailpoet_list_label, |
| 43 | .mailpoet_date_label { |
| 44 | display:block; |
| 45 | font-weight: normal; |
| 46 | } |
| 47 | |
| 48 | /* inputs */ |
| 49 | .mailpoet_text, |
| 50 | .mailpoet_textarea, |
| 51 | .mailpoet_select, |
| 52 | .mailpoet_date_month, |
| 53 | .mailpoet_date_day, |
| 54 | .mailpoet_date_year, |
| 55 | .mailpoet_date { |
| 56 | display:block; |
| 57 | } |
| 58 | |
| 59 | .mailpoet_text, |
| 60 | .mailpoet_textarea { |
| 61 | width: 200px; |
| 62 | } |
| 63 | |
| 64 | .mailpoet_checkbox { |
| 65 | } |
| 66 | |
| 67 | .mailpoet_submit { |
| 68 | } |
| 69 | |
| 70 | .mailpoet_divider { |
| 71 | } |
| 72 | |
| 73 | .mailpoet_message { |
| 74 | } |
| 75 | |
| 76 | .mailpoet_form_loading { |
| 77 | width: 30px; |
| 78 | text-align: center; |
| 79 | line-height: normal; |
| 80 | } |
| 81 | |
| 82 | .mailpoet_form_loading > span { |
| 83 | width: 5px; |
| 84 | height: 5px; |
| 85 | background-color: #5b5b5b; |
| 86 | } |
| 87 | EOL; |
| 88 | |
| 89 | /** @var CdnAssetUrl */ |
| 90 | protected $cdnAssetUrl; |
| 91 | |
| 92 | /** @var WPFunctions */ |
| 93 | protected $wp; |
| 94 | |
| 95 | /** @var string */ |
| 96 | protected $assetsDirectory = ''; |
| 97 | |
| 98 | /** @var SettingsController */ |
| 99 | private $settings; |
| 100 | |
| 101 | public function __construct( |
| 102 | CdnAssetUrl $cdnAssetUrl, |
| 103 | SettingsController $settings, |
| 104 | WPFunctions $wp |
| 105 | ) { |
| 106 | $this->cdnAssetUrl = $cdnAssetUrl; |
| 107 | $this->wp = $wp; |
| 108 | $this->settings = $settings; |
| 109 | } |
| 110 | |
| 111 | abstract public function getName(): string; |
| 112 | |
| 113 | abstract public function getBody(): array; |
| 114 | |
| 115 | abstract public function getThumbnailUrl(): string; |
| 116 | |
| 117 | public function getSettings(): array { |
| 118 | return [ |
| 119 | 'on_success' => 'message', |
| 120 | 'success_message' => '', |
| 121 | 'segments' => null, |
| 122 | 'segments_selected_by' => 'admin', |
| 123 | ]; |
| 124 | } |
| 125 | |
| 126 | public function getStyles(): string { |
| 127 | return self::DEFAULT_STYLES; |
| 128 | } |
| 129 | |
| 130 | public function toFormEntity(): FormEntity { |
| 131 | $formEntity = new FormEntity($this->getName()); |
| 132 | $formEntity->setBody($this->getBody()); |
| 133 | $formEntity->setSettings($this->getSettings()); |
| 134 | $formEntity->setStyles($this->getStyles()); |
| 135 | $settings = $formEntity->getSettings(); |
| 136 | if (!isset($settings['success_message']) || !($settings['success_message'])) { |
| 137 | $settings['success_message'] = $this->getDefaultSuccessMessage(); |
| 138 | $formEntity->setSettings($settings); |
| 139 | } |
| 140 | return $formEntity; |
| 141 | } |
| 142 | |
| 143 | private function getDefaultSuccessMessage() { |
| 144 | if ($this->settings->get('signup_confirmation.enabled')) { |
| 145 | return __('Check your inbox or spam folder to confirm your subscription.', 'mailpoet'); |
| 146 | } |
| 147 | return __('You’ve been successfully subscribed to our newsletter!', 'mailpoet'); |
| 148 | } |
| 149 | |
| 150 | protected function getAssetUrl(string $filename): string { |
| 151 | return $this->cdnAssetUrl->generateCdnUrl("form-templates/{$this->assetsDirectory}/$filename"); |
| 152 | } |
| 153 | |
| 154 | protected function replaceLinkTags($source, $link, $attributes = []): string { |
| 155 | return Helpers::replaceLinkTags($source, $link, $attributes); |
| 156 | } |
| 157 | |
| 158 | protected function replacePrivacyLinkTags($source, $link = '#'): string { |
| 159 | $privacyPolicyUrl = $this->wp->getPrivacyPolicyUrl(); |
| 160 | $attributes = []; |
| 161 | |
| 162 | if (!empty($privacyPolicyUrl)) { |
| 163 | $link = $this->wp->escUrl($privacyPolicyUrl); |
| 164 | $attributes = ['target' => '_blank']; |
| 165 | } |
| 166 | return $this->replaceLinkTags($source, $link, $attributes); |
| 167 | } |
| 168 | } |
| 169 |