PopulatorData
9 years ago
Activator.php
9 years ago
Analytics.php
9 years ago
Changelog.php
9 years ago
Database.php
9 years ago
Env.php
9 years ago
Hooks.php
9 years ago
Initializer.php
9 years ago
Localizer.php
9 years ago
Menu.php
9 years ago
Migrator.php
9 years ago
Populator.php
9 years ago
Renderer.php
9 years ago
RequirementsChecker.php
9 years ago
Shortcodes.php
9 years ago
Widget.php
9 years ago
index.php
9 years ago
Widget.php
157 lines
| 1 | <?php |
| 2 | namespace MailPoet\Config; |
| 3 | use \MailPoet\Util\Security; |
| 4 | use \MailPoet\Models\Form; |
| 5 | |
| 6 | if(!defined('ABSPATH')) exit; |
| 7 | |
| 8 | class Widget { |
| 9 | private $renderer = null; |
| 10 | |
| 11 | function __construct($renderer = null) { |
| 12 | if($renderer !== null) { |
| 13 | $this->renderer = $renderer; |
| 14 | } |
| 15 | } |
| 16 | |
| 17 | function init() { |
| 18 | $this->registerWidget(); |
| 19 | |
| 20 | if(!is_admin()) { |
| 21 | $this->setupDependencies(); |
| 22 | $this->setupIframe(); |
| 23 | } else { |
| 24 | add_action('widgets_admin_page', array($this, 'setupAdminWidgetPageDependencies')); |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | function setupIframe() { |
| 29 | $form_id = (isset($_GET['mailpoet_form_iframe']) ? (int)$_GET['mailpoet_form_iframe'] : 0); |
| 30 | if($form_id > 0) { |
| 31 | $form = Form::findOne($form_id); |
| 32 | |
| 33 | if($form !== false) { |
| 34 | $form_widget = new \MailPoet\Form\Widget(); |
| 35 | $form_html = $form_widget->widget(array( |
| 36 | 'form' => $form_id, |
| 37 | 'form_type' => 'iframe' |
| 38 | )); |
| 39 | |
| 40 | // capture javascripts |
| 41 | ob_start(); |
| 42 | wp_print_scripts('jquery'); |
| 43 | wp_print_scripts('mailpoet_vendor'); |
| 44 | wp_print_scripts('mailpoet_public'); |
| 45 | $scripts = ob_get_contents(); |
| 46 | ob_end_clean(); |
| 47 | |
| 48 | // language attributes |
| 49 | $language_attributes = array(); |
| 50 | $is_rtl = (bool)(function_exists('is_rtl') && is_rtl()); |
| 51 | |
| 52 | if($is_rtl) { |
| 53 | $language_attributes[] = 'dir="rtl"'; |
| 54 | } |
| 55 | |
| 56 | if($lang = get_bloginfo('language')) { |
| 57 | if(get_option('html_type') === 'text/html') { |
| 58 | $language_attributes[] = "lang=\"$lang\""; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | $language_attributes = apply_filters( |
| 63 | 'language_attributes', implode(' ', $language_attributes) |
| 64 | ); |
| 65 | |
| 66 | $data = array( |
| 67 | 'language_attributes' => $language_attributes, |
| 68 | 'scripts' => $scripts, |
| 69 | 'form' => $form_html, |
| 70 | 'mailpoet_form' => array( |
| 71 | 'ajax_url' => admin_url('admin-ajax.php', 'absolute'), |
| 72 | 'is_rtl' => $is_rtl |
| 73 | ) |
| 74 | ); |
| 75 | |
| 76 | try { |
| 77 | echo $this->renderer->render('form/iframe.html', $data); |
| 78 | } catch(\Exception $e) { |
| 79 | echo $e->getMessage(); |
| 80 | } |
| 81 | } |
| 82 | exit(); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | function registerWidget() { |
| 87 | register_widget('\MailPoet\Form\Widget'); |
| 88 | } |
| 89 | |
| 90 | function setupDependencies() { |
| 91 | wp_enqueue_style('mailpoet_public', Env::$assets_url.'/css/public.css'); |
| 92 | |
| 93 | wp_enqueue_script('mailpoet_vendor', |
| 94 | Env::$assets_url.'/js/vendor.js', |
| 95 | array(), |
| 96 | Env::$version, |
| 97 | true |
| 98 | ); |
| 99 | |
| 100 | wp_enqueue_script('mailpoet_public', |
| 101 | Env::$assets_url.'/js/public.js', |
| 102 | array(), |
| 103 | Env::$version, |
| 104 | true |
| 105 | ); |
| 106 | |
| 107 | wp_localize_script('mailpoet_public', 'MailPoetForm', array( |
| 108 | 'ajax_url' => admin_url('admin-ajax.php'), |
| 109 | 'is_rtl' => (function_exists('is_rtl') ? (bool)is_rtl() : false) |
| 110 | )); |
| 111 | } |
| 112 | |
| 113 | function setupAdminWidgetPageDependencies() { |
| 114 | wp_enqueue_script('mailpoet_vendor', |
| 115 | Env::$assets_url.'/js/vendor.js', |
| 116 | array(), |
| 117 | Env::$version, |
| 118 | true |
| 119 | ); |
| 120 | |
| 121 | wp_enqueue_script('mailpoet_admin', |
| 122 | Env::$assets_url.'/js/mailpoet.js', |
| 123 | array(), |
| 124 | Env::$version, |
| 125 | true |
| 126 | ); |
| 127 | } |
| 128 | |
| 129 | // TODO: extract this method into an Initializer |
| 130 | // - the "ajax" part might probably be useless |
| 131 | // - the "post" (non-ajax) part needs to be redone properly |
| 132 | function setupActions() { |
| 133 | // ajax requests |
| 134 | add_action( |
| 135 | 'wp_ajax_mailpoet_form_subscribe', |
| 136 | 'mailpoet_form_subscribe' |
| 137 | ); |
| 138 | add_action( |
| 139 | 'wp_ajax_nopriv_mailpoet_form_subscribe', |
| 140 | 'mailpoet_form_subscribe' |
| 141 | ); |
| 142 | // post request |
| 143 | add_action( |
| 144 | 'admin_post_nopriv_mailpoet_form_subscribe', |
| 145 | 'mailpoet_form_subscribe' |
| 146 | ); |
| 147 | add_action( |
| 148 | 'admin_post_mailpoet_form_subscribe', |
| 149 | 'mailpoet_form_subscribe' |
| 150 | ); |
| 151 | add_action( |
| 152 | 'init', |
| 153 | 'mailpoet_form_subscribe' |
| 154 | ); |
| 155 | } |
| 156 | } |
| 157 |