| 1 |
<?php |
| 2 |
|
| 3 |
namespace MailPoet\Form; |
| 4 |
|
| 5 |
if (!defined('ABSPATH')) exit; |
| 6 |
|
| 7 |
|
| 8 |
use MailPoet\API\JSON\API; |
| 9 |
use MailPoet\Config\Renderer as ConfigRenderer; |
| 10 |
use MailPoet\DI\ContainerWrapper; |
| 11 |
use MailPoet\Entities\FormEntity; |
| 12 |
use MailPoet\Form\Renderer as FormRenderer; |
| 13 |
use MailPoet\Models\Form; |
| 14 |
use MailPoet\Settings\SettingsController; |
| 15 |
use MailPoet\Util\Security; |
| 16 |
use MailPoet\WP\Functions as WPFunctions; |
| 17 |
|
| 18 |
class Widget extends \WP_Widget { |
| 19 |
private $renderer; |
| 20 |
private $wp; |
| 21 |
|
| 22 |
/** @var AssetsController */ |
| 23 |
private $assetsController; |
| 24 |
|
| 25 |
/** @var FormRenderer */ |
| 26 |
private $formRenderer; |
| 27 |
|
| 28 |
public function __construct() { |
| 29 |
parent::__construct( |
| 30 |
'mailpoet_form', |
| 31 |
WPFunctions::get()->__('MailPoet 3 Form', 'mailpoet'), |
| 32 |
['description' => WPFunctions::get()->__('Add a newsletter subscription form', 'mailpoet')] |
| 33 |
); |
| 34 |
$this->wp = new WPFunctions; |
| 35 |
$this->renderer = new \MailPoet\Config\Renderer(!WP_DEBUG, !WP_DEBUG); |
| 36 |
$this->assetsController = new AssetsController($this->wp, $this->renderer, SettingsController::getInstance()); |
| 37 |
$this->formRenderer = ContainerWrapper::getInstance()->get(FormRenderer::class); |
| 38 |
if (!is_admin()) { |
| 39 |
$this->setupIframe(); |
| 40 |
} else { |
| 41 |
WPFunctions::get()->addAction('widgets_admin_page', [ |
| 42 |
$this->assetsController, |
| 43 |
'setupAdminWidgetPageDependencies', |
| 44 |
]); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
public function setupIframe() { |
| 49 |
$formId = (isset($_GET['mailpoet_form_iframe']) ? (int)$_GET['mailpoet_form_iframe'] : 0); |
| 50 |
if (!$formId || !Form::findOne($formId)) return; |
| 51 |
|
| 52 |
$formHtml = $this->widget( |
| 53 |
[ |
| 54 |
'form' => $formId, |
| 55 |
'form_type' => 'iframe', |
| 56 |
] |
| 57 |
); |
| 58 |
|
| 59 |
$scripts = $this->assetsController->printScripts(); |
| 60 |
|
| 61 |
// language attributes |
| 62 |
$languageAttributes = []; |
| 63 |
$isRtl = (bool)(function_exists('is_rtl') && WPFunctions::get()->isRtl()); |
| 64 |
|
| 65 |
if ($isRtl) { |
| 66 |
$languageAttributes[] = 'dir="rtl"'; |
| 67 |
} |
| 68 |
|
| 69 |
if (get_option('html_type') === 'text/html') { |
| 70 |
$languageAttributes[] = sprintf('lang="%s"', WPFunctions::get()->getBloginfo('language')); |
| 71 |
} |
| 72 |
|
| 73 |
$languageAttributes = WPFunctions::get()->applyFilters( |
| 74 |
'language_attributes', implode(' ', $languageAttributes) |
| 75 |
); |
| 76 |
|
| 77 |
$data = [ |
| 78 |
'language_attributes' => $languageAttributes, |
| 79 |
'scripts' => $scripts, |
| 80 |
'form' => $formHtml, |
| 81 |
'mailpoet_form' => [ |
| 82 |
'ajax_url' => WPFunctions::get()->adminUrl('admin-ajax.php', 'absolute'), |
| 83 |
'is_rtl' => $isRtl, |
| 84 |
], |
| 85 |
]; |
| 86 |
|
| 87 |
try { |
| 88 |
echo $this->renderer->render('form/iframe.html', $data); |
| 89 |
} catch (\Exception $e) { |
| 90 |
echo $e->getMessage(); |
| 91 |
} |
| 92 |
|
| 93 |
exit(); |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Save the new widget's title. |
| 98 |
*/ |
| 99 |
public function update($newInstance, $oldInstance) { |
| 100 |
$instance = $oldInstance; |
| 101 |
$instance['title'] = strip_tags($newInstance['title']); |
| 102 |
$instance['form'] = (int)$newInstance['form']; |
| 103 |
return $instance; |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Output the widget's option form. |
| 108 |
*/ |
| 109 |
public function form($instance) { |
| 110 |
$instance = WPFunctions::get()->wpParseArgs( |
| 111 |
(array)$instance, |
| 112 |
[ |
| 113 |
'title' => WPFunctions::get()->__('Subscribe to Our Newsletter', 'mailpoet'), |
| 114 |
] |
| 115 |
); |
| 116 |
|
| 117 |
$formEditUrl = WPFunctions::get()->adminUrl('admin.php?page=mailpoet-form-editor&id='); |
| 118 |
|
| 119 |
// set title |
| 120 |
$title = isset($instance['title']) ? strip_tags($instance['title']) : ''; |
| 121 |
|
| 122 |
// set form |
| 123 |
$selectedForm = isset($instance['form']) ? (int)($instance['form']) : 0; |
| 124 |
|
| 125 |
// get forms list |
| 126 |
$forms = Form::getPublished()->orderByAsc('name')->findArray(); |
| 127 |
?><p> |
| 128 |
<label for="<?php $this->get_field_id( 'title' ) ?>"><?php WPFunctions::get()->_e('Title:', 'mailpoet'); ?></label> |
| 129 |
<input |
| 130 |
type="text" |
| 131 |
class="widefat" |
| 132 |
id="<?php echo $this->get_field_id('title') ?>" |
| 133 |
name="<?php echo $this->get_field_name('title'); ?>" |
| 134 |
value="<?php echo WPFunctions::get()->escAttr($title); ?>" |
| 135 |
/> |
| 136 |
</p> |
| 137 |
<p> |
| 138 |
<select class="widefat" id="<?php echo $this->get_field_id('form') ?>" name="<?php echo $this->get_field_name('form'); ?>"> |
| 139 |
<?php |
| 140 |
foreach ($forms as $form) { |
| 141 |
$isSelected = ($selectedForm === (int)$form['id']) ? 'selected="selected"' : ''; |
| 142 |
$formName = $form['name'] ? $this->wp->escHtml($form['name']) : "({$this->wp->_x('no name', 'fallback for forms without a name in a form list')})" |
| 143 |
?> |
| 144 |
<option value="<?php echo (int)$form['id']; ?>" <?php echo $isSelected; ?>><?php echo $formName; ?></option> |
| 145 |
<?php } ?> |
| 146 |
</select> |
| 147 |
</p> |
| 148 |
<p> |
| 149 |
<a href="javascript:;" onClick="createSubscriptionForm()" class="mailpoet_form_new"><?php WPFunctions::get()->_e('Create a new form', 'mailpoet'); ?></a> |
| 150 |
</p> |
| 151 |
<script type="text/javascript"> |
| 152 |
function createSubscriptionForm() { |
| 153 |
MailPoet.Ajax.post({ |
| 154 |
endpoint: 'forms', |
| 155 |
action: 'create', |
| 156 |
api_version: window.mailpoet_api_version |
| 157 |
}).done(function(response) { |
| 158 |
if (response.data && response.data.id) { |
| 159 |
window.location = |
| 160 |
"<?php echo $formEditUrl; ?>" + response.data.id; |
| 161 |
} |
| 162 |
}).fail((response) => { |
| 163 |
if (response.errors.length > 0) { |
| 164 |
MailPoet.Notice.error( |
| 165 |
response.errors.map((error) => { return error.message; }), |
| 166 |
{ scroll: true } |
| 167 |
); |
| 168 |
} |
| 169 |
}); |
| 170 |
return false; |
| 171 |
} |
| 172 |
</script> |
| 173 |
<?php |
| 174 |
return ''; |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Output the widget itself. |
| 179 |
*/ |
| 180 |
public function widget($args, $instance = null) { |
| 181 |
$this->assetsController->setupFrontEndDependencies(); |
| 182 |
|
| 183 |
$beforeWidget = !empty($args['before_widget']) ? $args['before_widget'] : ''; |
| 184 |
$afterWidget = !empty($args['after_widget']) ? $args['after_widget'] : ''; |
| 185 |
$beforeTitle = !empty($args['before_title']) ? $args['before_title'] : ''; |
| 186 |
$afterTitle = !empty($args['after_title']) ? $args['after_title'] : ''; |
| 187 |
|
| 188 |
if ($instance === null) { |
| 189 |
$instance = $args; |
| 190 |
} |
| 191 |
|
| 192 |
$title = $this->wp->applyFilters( |
| 193 |
'widget_title', |
| 194 |
!empty($instance['title']) ? $instance['title'] : '', |
| 195 |
$instance, |
| 196 |
$this->id_base // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 197 |
); |
| 198 |
|
| 199 |
// get form |
| 200 |
$form = Form::getPublished()->findOne($instance['form']); |
| 201 |
if (!$form) return ''; |
| 202 |
if ($form->status !== FormEntity::STATUS_ENABLED) return ''; |
| 203 |
|
| 204 |
$form = $form->asArray(); |
| 205 |
$formType = 'widget'; |
| 206 |
if (isset($instance['form_type']) && in_array( |
| 207 |
$instance['form_type'], |
| 208 |
[ |
| 209 |
'html', |
| 210 |
'php', |
| 211 |
'iframe', |
| 212 |
'shortcode', |
| 213 |
] |
| 214 |
)) { |
| 215 |
$formType = $instance['form_type']; |
| 216 |
} |
| 217 |
|
| 218 |
$body = (isset($form['body']) ? $form['body'] : []); |
| 219 |
$output = ''; |
| 220 |
|
| 221 |
if (!empty($body) && isset($form['settings']) && is_array($form['settings'])) { |
| 222 |
$formId = $this->id_base . '_' . $form['id']; // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 223 |
$data = [ |
| 224 |
'form_html_id' => $formId, |
| 225 |
'form_id' => $form['id'], |
| 226 |
'form_type' => $formType, |
| 227 |
'form_success_message' => $form['settings']['success_message'], |
| 228 |
'title' => $title, |
| 229 |
'styles' => $this->formRenderer->renderStyles($form, '#' . $formId, FormEntity::DISPLAY_TYPE_OTHERS), |
| 230 |
'html' => $this->formRenderer->renderHTML($form), |
| 231 |
'before_widget' => $beforeWidget, |
| 232 |
'after_widget' => $afterWidget, |
| 233 |
'before_title' => $beforeTitle, |
| 234 |
'after_title' => $afterTitle, |
| 235 |
]; |
| 236 |
|
| 237 |
// (POST) non ajax success/error variables |
| 238 |
$data['success'] = ( |
| 239 |
(isset($_GET['mailpoet_success'])) |
| 240 |
&& |
| 241 |
((int)$_GET['mailpoet_success'] === (int)$form['id']) |
| 242 |
); |
| 243 |
$data['error'] = ( |
| 244 |
(isset($_GET['mailpoet_error'])) |
| 245 |
&& |
| 246 |
((int)$_GET['mailpoet_error'] === (int)$form['id']) |
| 247 |
); |
| 248 |
|
| 249 |
// generate security token |
| 250 |
$data['token'] = Security::generateToken(); |
| 251 |
|
| 252 |
// add API version |
| 253 |
$data['api_version'] = API::CURRENT_VERSION; |
| 254 |
|
| 255 |
// render form |
| 256 |
$renderer = new ConfigRenderer(); |
| 257 |
try { |
| 258 |
$output = $renderer->render('form/front_end_form.html', $data); |
| 259 |
$output = WPFunctions::get()->doShortcode($output); |
| 260 |
$output = $this->wp->applyFilters('mailpoet_form_widget_post_process', $output); |
| 261 |
} catch (\Exception $e) { |
| 262 |
$output = $e->getMessage(); |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
if ($formType === 'widget') { |
| 267 |
echo $output; |
| 268 |
} else { |
| 269 |
return $output; |
| 270 |
} |
| 271 |
} |
| 272 |
} |
| 273 |
|