Centralizes widget logic in one place

Cleans up code
This commit is contained in:
Vlad
2017-12-09 19:43:29 -05:00
parent 1f9c956637
commit d13aa67a07
3 changed files with 193 additions and 218 deletions

View File

@ -115,8 +115,7 @@ class Initializer {
}
function setupWidget() {
$widget = new Widget($this->renderer);
$widget->init();
register_widget('\MailPoet\Form\Widget');
}
function initialize() {

View File

@ -1,153 +0,0 @@
<?php
namespace MailPoet\Config;
use MailPoet\Models\Form;
if(!defined('ABSPATH')) exit;
class Widget {
private $renderer = null;
function __construct($renderer = null) {
if($renderer !== null) {
$this->renderer = $renderer;
}
}
function init() {
$this->registerWidget();
if(!is_admin()) {
$this->setupDependencies();
$this->setupIframe();
} else {
add_action('widgets_admin_page', array($this, 'setupAdminWidgetPageDependencies'));
}
}
function setupIframe() {
$form_id = (isset($_GET['mailpoet_form_iframe']) ? (int)$_GET['mailpoet_form_iframe'] : 0);
if($form_id > 0) {
$form = Form::findOne($form_id);
if($form !== false) {
$form_widget = new \MailPoet\Form\Widget();
$form_html = $form_widget->widget(array(
'form' => $form_id,
'form_type' => 'iframe'
));
// capture javascripts
ob_start();
wp_print_scripts('jquery');
wp_print_scripts('mailpoet_vendor');
wp_print_scripts('mailpoet_public');
$scripts = ob_get_contents();
ob_end_clean();
// language attributes
$language_attributes = array();
$is_rtl = (bool)(function_exists('is_rtl') && is_rtl());
if($is_rtl) {
$language_attributes[] = 'dir="rtl"';
}
if($lang = get_bloginfo('language')) {
if(get_option('html_type') === 'text/html') {
$language_attributes[] = "lang=\"$lang\"";
}
}
$language_attributes = apply_filters(
'language_attributes', implode(' ', $language_attributes)
);
$data = array(
'language_attributes' => $language_attributes,
'scripts' => $scripts,
'form' => $form_html,
'mailpoet_form' => array(
'ajax_url' => admin_url('admin-ajax.php', 'absolute'),
'is_rtl' => $is_rtl
)
);
try {
echo $this->renderer->render('form/iframe.html', $data);
} catch(\Exception $e) {
echo $e->getMessage();
}
}
exit();
}
}
function registerWidget() {
register_widget('\MailPoet\Form\Widget');
}
function setupDependencies() {
wp_enqueue_style(
'mailpoet_public',
Env::$assets_url . '/css/' . $this->renderer->getCssAsset('public.css')
);
wp_enqueue_script(
'mailpoet_vendor',
Env::$assets_url . '/js/' . $this->renderer->getJsAsset('vendor.js'),
array(),
Env::$version,
true
);
wp_enqueue_script(
'mailpoet_public',
Env::$assets_url . '/js/' . $this->renderer->getJsAsset('public.js'),
array('jquery'),
Env::$version,
true
);
wp_localize_script('mailpoet_public', 'MailPoetForm', array(
'ajax_url' => admin_url('admin-ajax.php'),
'is_rtl' => (function_exists('is_rtl') ? (bool)is_rtl() : false)
));
$ajax_failed_error_message = __('An error has happened while performing a request, please try again later.');
$inline_script = <<<EOL
function initMailpoetTranslation() {
if(typeof MailPoet !== 'undefined') {
MailPoet.I18n.add('ajaxFailedErrorMessage', '%s')
} else {
setTimeout(initMailpoetTranslation, 250);
}
}
setTimeout(initMailpoetTranslation, 250);
EOL;
wp_add_inline_script(
'mailpoet_public',
sprintf($inline_script, $ajax_failed_error_message),
'after'
);
}
function setupAdminWidgetPageDependencies() {
wp_enqueue_script(
'mailpoet_vendor',
Env::$assets_url . '/js/' . $this->renderer->getJsAsset('vendor.js'),
array(),
Env::$version,
true
);
wp_enqueue_script(
'mailpoet_admin',
Env::$assets_url . '/js/' . $this->renderer->getJsAsset('mailpoet.js'),
array(),
Env::$version,
true
);
}
}

View File

@ -3,6 +3,7 @@
namespace MailPoet\Form;
use MailPoet\API\JSON\API;
use MailPoet\Config\Env;
use MailPoet\Config\Renderer as ConfigRenderer;
use MailPoet\Form\Renderer as FormRenderer;
use MailPoet\Models\Form;
@ -12,14 +13,143 @@ use MailPoet\WP\Hooks;
if(!defined('ABSPATH')) exit;
class Widget extends \WP_Widget {
private $renderer;
function __construct() {
return parent::__construct(
parent::__construct(
'mailpoet_form',
__('MailPoet Form', 'mailpoet'),
array('description' => __('Add a newsletter subscription form', 'mailpoet'))
);
$this->renderer = new \MailPoet\Config\Renderer(!WP_DEBUG, !WP_DEBUG);
if(!is_admin()) {
$this->setupDependencies();
$this->setupIframe();
} else {
add_action('widgets_admin_page', array(
$this,
'setupAdminWidgetPageDependencies'
));
}
}
function setupIframe() {
$form_id = (isset($_GET['mailpoet_form_iframe']) ? (int)$_GET['mailpoet_form_iframe'] : 0);
if(!$form_id || !Form::findOne($form_id)) return;
$form_html = $this->widget(
array(
'description' => __('Add a newsletter subscription form', 'mailpoet')
'form' => $form_id,
'form_type' => 'iframe'
)
);
ob_start();
wp_print_scripts('jquery');
wp_print_scripts('mailpoet_vendor');
wp_print_scripts('mailpoet_public');
$scripts = ob_get_contents();
ob_end_clean();
// language attributes
$language_attributes = array();
$is_rtl = (bool)(function_exists('is_rtl') && is_rtl());
if($is_rtl) {
$language_attributes[] = 'dir="rtl"';
}
if(get_option('html_type') === 'text/html') {
$language_attributes[] = sprintf('lang="%s"', get_bloginfo('language'));
}
$language_attributes = apply_filters(
'language_attributes', implode(' ', $language_attributes)
);
$data = array(
'language_attributes' => $language_attributes,
'scripts' => $scripts,
'form' => $form_html,
'mailpoet_form' => array(
'ajax_url' => admin_url('admin-ajax.php', 'absolute'),
'is_rtl' => $is_rtl
)
);
try {
echo $this->renderer->render('form/iframe.html', $data);
} catch(\Exception $e) {
echo $e->getMessage();
}
exit();
}
function setupDependencies() {
wp_enqueue_style(
'mailpoet_public',
Env::$assets_url . '/css/' . $this->renderer->getCssAsset('public.css')
);
wp_enqueue_script(
'mailpoet_vendor',
Env::$assets_url . '/js/' . $this->renderer->getJsAsset('vendor.js'),
array(),
Env::$version,
true
);
wp_enqueue_script(
'mailpoet_public',
Env::$assets_url . '/js/' . $this->renderer->getJsAsset('public.js'),
array('jquery'),
Env::$version,
true
);
wp_localize_script('mailpoet_public', 'MailPoetForm', array(
'ajax_url' => admin_url('admin-ajax.php'),
'is_rtl' => (function_exists('is_rtl') ? (bool)is_rtl() : false)
));
$ajax_failed_error_message = __('An error has happened while performing a request, please try again later.');
$inline_script = <<<EOL
function initMailpoetTranslation() {
if(typeof MailPoet !== 'undefined') {
MailPoet.I18n.add('ajaxFailedErrorMessage', '%s')
} else {
setTimeout(initMailpoetTranslation, 250);
}
}
setTimeout(initMailpoetTranslation, 250);
EOL;
wp_add_inline_script(
'mailpoet_public',
sprintf($inline_script, $ajax_failed_error_message),
'after'
);
}
function setupAdminWidgetPageDependencies() {
wp_enqueue_script(
'mailpoet_vendor',
Env::$assets_url . '/js/' . $this->renderer->getJsAsset('vendor.js'),
array(),
Env::$version,
true
);
wp_enqueue_script(
'mailpoet_admin',
Env::$assets_url . '/js/' . $this->renderer->getJsAsset('mailpoet.js'),
array(),
Env::$version,
true
);
}
/**
@ -36,7 +166,6 @@ class Widget extends \WP_Widget {
* Output the widget's option form.
*/
public function form($instance) {
$instance = wp_parse_args(
(array)$instance,
array(
@ -121,21 +250,22 @@ class Widget extends \WP_Widget {
// get form
$form = Form::getPublished()->findOne($instance['form']);
if(!$form) return '';
// if the form was not found, return nothing
if($form === false) {
return '';
} else {
$form = $form->asArray();
$form_type = 'widget';
if(isset($instance['form_type']) && in_array(
$instance['form_type'],
array('html', 'php', 'iframe', 'shortcode')
array(
'html',
'php',
'iframe',
'shortcode'
)
)) {
$form_type = $instance['form_type'];
}
$settings = (isset($form['settings']) ? $form['settings'] : array());
$body = (isset($form['body']) ? $form['body'] : array());
$output = '';
@ -191,4 +321,3 @@ class Widget extends \WP_Widget {
}
}
}
}