Change line endings from DOS CRLF to Unix LF
This commit is contained in:
@ -1,197 +1,197 @@
|
||||
<?php
|
||||
namespace MailPoet\Config;
|
||||
|
||||
use MailPoet\Models;
|
||||
use MailPoet\Cron\Supervisor;
|
||||
use MailPoet\Router;
|
||||
|
||||
if(!defined('ABSPATH')) exit;
|
||||
|
||||
require_once(ABSPATH . 'wp-admin/includes/plugin.php');
|
||||
|
||||
class Initializer {
|
||||
function __construct($params = array(
|
||||
'file' => '',
|
||||
'version' => '1.0.0'
|
||||
)) {
|
||||
Env::init($params['file'], $params['version']);
|
||||
}
|
||||
|
||||
function init() {
|
||||
$this->setupDB();
|
||||
|
||||
register_activation_hook(Env::$file, array($this, 'runMigrator'));
|
||||
register_activation_hook(Env::$file, array($this, 'runPopulator'));
|
||||
|
||||
add_action('plugins_loaded', array($this, 'setup'));
|
||||
add_action('init', array($this, 'onInit'));
|
||||
add_action('widgets_init', array($this, 'setupWidget'));
|
||||
}
|
||||
|
||||
function setup() {
|
||||
try {
|
||||
$this->setupRenderer();
|
||||
$this->setupLocalizer();
|
||||
$this->setupMenu();
|
||||
$this->setupPermissions();
|
||||
$this->setupAnalytics();
|
||||
$this->setupChangelog();
|
||||
$this->setupShortcodes();
|
||||
$this->setupHooks();
|
||||
$this->setupImages();
|
||||
$this->runQueueSupervisor();
|
||||
} catch(\Exception $e) {
|
||||
// if anything goes wrong during init
|
||||
// automatically deactivate the plugin
|
||||
deactivate_plugins(Env::$file);
|
||||
}
|
||||
}
|
||||
|
||||
function onInit() {
|
||||
$this->setupRouter();
|
||||
$this->setupPublicAPI();
|
||||
$this->setupPages();
|
||||
}
|
||||
|
||||
function setupDB() {
|
||||
\ORM::configure(Env::$db_source_name);
|
||||
\ORM::configure('username', Env::$db_username);
|
||||
\ORM::configure('password', Env::$db_password);
|
||||
\ORM::configure('logging', WP_DEBUG);
|
||||
\ORM::configure('logger', function($query, $time) {
|
||||
// error_log("\n".$query."\n");
|
||||
});
|
||||
|
||||
\ORM::configure('driver_options', array(
|
||||
\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
|
||||
\PDO::MYSQL_ATTR_INIT_COMMAND =>
|
||||
'SET TIME_ZONE = "' . Env::$db_timezone_offset. '"'
|
||||
));
|
||||
|
||||
$settings = Env::$db_prefix . 'settings';
|
||||
$segments = Env::$db_prefix . 'segments';
|
||||
$forms = Env::$db_prefix . 'forms';
|
||||
$custom_fields = Env::$db_prefix . 'custom_fields';
|
||||
$subscribers = Env::$db_prefix . 'subscribers';
|
||||
$subscriber_segment = Env::$db_prefix . 'subscriber_segment';
|
||||
$subscriber_custom_field = Env::$db_prefix . 'subscriber_custom_field';
|
||||
$newsletter_segment = Env::$db_prefix . 'newsletter_segment';
|
||||
$sending_queues = Env::$db_prefix . 'sending_queues';
|
||||
$newsletters = Env::$db_prefix . 'newsletters';
|
||||
$newsletter_templates = Env::$db_prefix . 'newsletter_templates';
|
||||
$newsletter_option_fields = Env::$db_prefix . 'newsletter_option_fields';
|
||||
$newsletter_option = Env::$db_prefix . 'newsletter_option';
|
||||
$newsletter_links = Env::$db_prefix . 'newsletter_links';
|
||||
$newsletter_posts = Env::$db_prefix . 'newsletter_posts';
|
||||
$statistics_newsletters = Env::$db_prefix . 'statistics_newsletters';
|
||||
$statistics_clicks = Env::$db_prefix . 'statistics_clicks';
|
||||
$statistics_opens = Env::$db_prefix . 'statistics_opens';
|
||||
$statistics_unsubscribes = Env::$db_prefix . 'statistics_unsubscribes';
|
||||
$statistics_forms = Env::$db_prefix . 'statistics_forms';
|
||||
|
||||
define('MP_SETTINGS_TABLE', $settings);
|
||||
define('MP_SEGMENTS_TABLE', $segments);
|
||||
define('MP_FORMS_TABLE', $forms);
|
||||
define('MP_CUSTOM_FIELDS_TABLE', $custom_fields);
|
||||
define('MP_SUBSCRIBERS_TABLE', $subscribers);
|
||||
define('MP_SUBSCRIBER_SEGMENT_TABLE', $subscriber_segment);
|
||||
define('MP_SUBSCRIBER_CUSTOM_FIELD_TABLE', $subscriber_custom_field);
|
||||
define('MP_SENDING_QUEUES_TABLE', $sending_queues);
|
||||
define('MP_NEWSLETTERS_TABLE', $newsletters);
|
||||
define('MP_NEWSLETTER_TEMPLATES_TABLE', $newsletter_templates);
|
||||
define('MP_NEWSLETTER_SEGMENT_TABLE', $newsletter_segment);
|
||||
define('MP_NEWSLETTER_OPTION_FIELDS_TABLE', $newsletter_option_fields);
|
||||
define('MP_NEWSLETTER_LINKS_TABLE', $newsletter_links);
|
||||
define('MP_NEWSLETTER_POSTS_TABLE', $newsletter_posts);
|
||||
define('MP_NEWSLETTER_OPTION_TABLE', $newsletter_option);
|
||||
define('MP_STATISTICS_NEWSLETTERS_TABLE', $statistics_newsletters);
|
||||
define('MP_STATISTICS_CLICKS_TABLE', $statistics_clicks);
|
||||
define('MP_STATISTICS_OPENS_TABLE', $statistics_opens);
|
||||
define('MP_STATISTICS_UNSUBSCRIBES_TABLE', $statistics_unsubscribes);
|
||||
define('MP_STATISTICS_FORMS_TABLE', $statistics_forms);
|
||||
}
|
||||
|
||||
function runMigrator() {
|
||||
$migrator = new Migrator();
|
||||
$migrator->up();
|
||||
}
|
||||
|
||||
function runPopulator() {
|
||||
$populator = new Populator();
|
||||
$populator->up();
|
||||
}
|
||||
|
||||
function setupRenderer() {
|
||||
$renderer = new Renderer();
|
||||
$this->renderer = $renderer->init();
|
||||
}
|
||||
|
||||
function setupLocalizer() {
|
||||
$localizer = new Localizer($this->renderer);
|
||||
$localizer->init();
|
||||
}
|
||||
|
||||
function setupMenu() {
|
||||
$menu = new Menu($this->renderer, Env::$assets_url);
|
||||
$menu->init();
|
||||
}
|
||||
|
||||
function setupRouter() {
|
||||
$router = new Router\Router();
|
||||
$router->init();
|
||||
}
|
||||
|
||||
function setupWidget() {
|
||||
$widget = new Widget($this->renderer);
|
||||
$widget->init();
|
||||
}
|
||||
|
||||
function setupAnalytics() {
|
||||
$widget = new Analytics();
|
||||
$widget->init();
|
||||
}
|
||||
|
||||
function setupPermissions() {
|
||||
$permissions = new Permissions();
|
||||
$permissions->init();
|
||||
}
|
||||
|
||||
function setupChangelog() {
|
||||
$changelog = new Changelog();
|
||||
$changelog->init();
|
||||
}
|
||||
|
||||
function setupPages() {
|
||||
$pages = new \MailPoet\Settings\Pages();
|
||||
$pages->init();
|
||||
}
|
||||
|
||||
function setupShortcodes() {
|
||||
$shortcodes = new Shortcodes();
|
||||
$shortcodes->init();
|
||||
}
|
||||
|
||||
function setupHooks() {
|
||||
$hooks = new Hooks();
|
||||
$hooks->init();
|
||||
}
|
||||
|
||||
function setupPublicAPI() {
|
||||
$publicAPI = new PublicAPI();
|
||||
$publicAPI->init();
|
||||
}
|
||||
|
||||
function runQueueSupervisor() {
|
||||
if(php_sapi_name() === 'cli') return;
|
||||
try {
|
||||
$supervisor = new Supervisor();
|
||||
$supervisor->checkDaemon();
|
||||
} catch(\Exception $e) {
|
||||
// Prevent Daemon exceptions from breaking out and breaking UI
|
||||
}
|
||||
}
|
||||
|
||||
function setupImages() {
|
||||
add_image_size('mailpoet_newsletter_max', 1320);
|
||||
}
|
||||
}
|
||||
<?php
|
||||
namespace MailPoet\Config;
|
||||
|
||||
use MailPoet\Models;
|
||||
use MailPoet\Cron\Supervisor;
|
||||
use MailPoet\Router;
|
||||
|
||||
if(!defined('ABSPATH')) exit;
|
||||
|
||||
require_once(ABSPATH . 'wp-admin/includes/plugin.php');
|
||||
|
||||
class Initializer {
|
||||
function __construct($params = array(
|
||||
'file' => '',
|
||||
'version' => '1.0.0'
|
||||
)) {
|
||||
Env::init($params['file'], $params['version']);
|
||||
}
|
||||
|
||||
function init() {
|
||||
$this->setupDB();
|
||||
|
||||
register_activation_hook(Env::$file, array($this, 'runMigrator'));
|
||||
register_activation_hook(Env::$file, array($this, 'runPopulator'));
|
||||
|
||||
add_action('plugins_loaded', array($this, 'setup'));
|
||||
add_action('init', array($this, 'onInit'));
|
||||
add_action('widgets_init', array($this, 'setupWidget'));
|
||||
}
|
||||
|
||||
function setup() {
|
||||
try {
|
||||
$this->setupRenderer();
|
||||
$this->setupLocalizer();
|
||||
$this->setupMenu();
|
||||
$this->setupPermissions();
|
||||
$this->setupAnalytics();
|
||||
$this->setupChangelog();
|
||||
$this->setupShortcodes();
|
||||
$this->setupHooks();
|
||||
$this->setupImages();
|
||||
$this->runQueueSupervisor();
|
||||
} catch(\Exception $e) {
|
||||
// if anything goes wrong during init
|
||||
// automatically deactivate the plugin
|
||||
deactivate_plugins(Env::$file);
|
||||
}
|
||||
}
|
||||
|
||||
function onInit() {
|
||||
$this->setupRouter();
|
||||
$this->setupPublicAPI();
|
||||
$this->setupPages();
|
||||
}
|
||||
|
||||
function setupDB() {
|
||||
\ORM::configure(Env::$db_source_name);
|
||||
\ORM::configure('username', Env::$db_username);
|
||||
\ORM::configure('password', Env::$db_password);
|
||||
\ORM::configure('logging', WP_DEBUG);
|
||||
\ORM::configure('logger', function($query, $time) {
|
||||
// error_log("\n".$query."\n");
|
||||
});
|
||||
|
||||
\ORM::configure('driver_options', array(
|
||||
\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
|
||||
\PDO::MYSQL_ATTR_INIT_COMMAND =>
|
||||
'SET TIME_ZONE = "' . Env::$db_timezone_offset. '"'
|
||||
));
|
||||
|
||||
$settings = Env::$db_prefix . 'settings';
|
||||
$segments = Env::$db_prefix . 'segments';
|
||||
$forms = Env::$db_prefix . 'forms';
|
||||
$custom_fields = Env::$db_prefix . 'custom_fields';
|
||||
$subscribers = Env::$db_prefix . 'subscribers';
|
||||
$subscriber_segment = Env::$db_prefix . 'subscriber_segment';
|
||||
$subscriber_custom_field = Env::$db_prefix . 'subscriber_custom_field';
|
||||
$newsletter_segment = Env::$db_prefix . 'newsletter_segment';
|
||||
$sending_queues = Env::$db_prefix . 'sending_queues';
|
||||
$newsletters = Env::$db_prefix . 'newsletters';
|
||||
$newsletter_templates = Env::$db_prefix . 'newsletter_templates';
|
||||
$newsletter_option_fields = Env::$db_prefix . 'newsletter_option_fields';
|
||||
$newsletter_option = Env::$db_prefix . 'newsletter_option';
|
||||
$newsletter_links = Env::$db_prefix . 'newsletter_links';
|
||||
$newsletter_posts = Env::$db_prefix . 'newsletter_posts';
|
||||
$statistics_newsletters = Env::$db_prefix . 'statistics_newsletters';
|
||||
$statistics_clicks = Env::$db_prefix . 'statistics_clicks';
|
||||
$statistics_opens = Env::$db_prefix . 'statistics_opens';
|
||||
$statistics_unsubscribes = Env::$db_prefix . 'statistics_unsubscribes';
|
||||
$statistics_forms = Env::$db_prefix . 'statistics_forms';
|
||||
|
||||
define('MP_SETTINGS_TABLE', $settings);
|
||||
define('MP_SEGMENTS_TABLE', $segments);
|
||||
define('MP_FORMS_TABLE', $forms);
|
||||
define('MP_CUSTOM_FIELDS_TABLE', $custom_fields);
|
||||
define('MP_SUBSCRIBERS_TABLE', $subscribers);
|
||||
define('MP_SUBSCRIBER_SEGMENT_TABLE', $subscriber_segment);
|
||||
define('MP_SUBSCRIBER_CUSTOM_FIELD_TABLE', $subscriber_custom_field);
|
||||
define('MP_SENDING_QUEUES_TABLE', $sending_queues);
|
||||
define('MP_NEWSLETTERS_TABLE', $newsletters);
|
||||
define('MP_NEWSLETTER_TEMPLATES_TABLE', $newsletter_templates);
|
||||
define('MP_NEWSLETTER_SEGMENT_TABLE', $newsletter_segment);
|
||||
define('MP_NEWSLETTER_OPTION_FIELDS_TABLE', $newsletter_option_fields);
|
||||
define('MP_NEWSLETTER_LINKS_TABLE', $newsletter_links);
|
||||
define('MP_NEWSLETTER_POSTS_TABLE', $newsletter_posts);
|
||||
define('MP_NEWSLETTER_OPTION_TABLE', $newsletter_option);
|
||||
define('MP_STATISTICS_NEWSLETTERS_TABLE', $statistics_newsletters);
|
||||
define('MP_STATISTICS_CLICKS_TABLE', $statistics_clicks);
|
||||
define('MP_STATISTICS_OPENS_TABLE', $statistics_opens);
|
||||
define('MP_STATISTICS_UNSUBSCRIBES_TABLE', $statistics_unsubscribes);
|
||||
define('MP_STATISTICS_FORMS_TABLE', $statistics_forms);
|
||||
}
|
||||
|
||||
function runMigrator() {
|
||||
$migrator = new Migrator();
|
||||
$migrator->up();
|
||||
}
|
||||
|
||||
function runPopulator() {
|
||||
$populator = new Populator();
|
||||
$populator->up();
|
||||
}
|
||||
|
||||
function setupRenderer() {
|
||||
$renderer = new Renderer();
|
||||
$this->renderer = $renderer->init();
|
||||
}
|
||||
|
||||
function setupLocalizer() {
|
||||
$localizer = new Localizer($this->renderer);
|
||||
$localizer->init();
|
||||
}
|
||||
|
||||
function setupMenu() {
|
||||
$menu = new Menu($this->renderer, Env::$assets_url);
|
||||
$menu->init();
|
||||
}
|
||||
|
||||
function setupRouter() {
|
||||
$router = new Router\Router();
|
||||
$router->init();
|
||||
}
|
||||
|
||||
function setupWidget() {
|
||||
$widget = new Widget($this->renderer);
|
||||
$widget->init();
|
||||
}
|
||||
|
||||
function setupAnalytics() {
|
||||
$widget = new Analytics();
|
||||
$widget->init();
|
||||
}
|
||||
|
||||
function setupPermissions() {
|
||||
$permissions = new Permissions();
|
||||
$permissions->init();
|
||||
}
|
||||
|
||||
function setupChangelog() {
|
||||
$changelog = new Changelog();
|
||||
$changelog->init();
|
||||
}
|
||||
|
||||
function setupPages() {
|
||||
$pages = new \MailPoet\Settings\Pages();
|
||||
$pages->init();
|
||||
}
|
||||
|
||||
function setupShortcodes() {
|
||||
$shortcodes = new Shortcodes();
|
||||
$shortcodes->init();
|
||||
}
|
||||
|
||||
function setupHooks() {
|
||||
$hooks = new Hooks();
|
||||
$hooks->init();
|
||||
}
|
||||
|
||||
function setupPublicAPI() {
|
||||
$publicAPI = new PublicAPI();
|
||||
$publicAPI->init();
|
||||
}
|
||||
|
||||
function runQueueSupervisor() {
|
||||
if(php_sapi_name() === 'cli') return;
|
||||
try {
|
||||
$supervisor = new Supervisor();
|
||||
$supervisor->checkDaemon();
|
||||
} catch(\Exception $e) {
|
||||
// Prevent Daemon exceptions from breaking out and breaking UI
|
||||
}
|
||||
}
|
||||
|
||||
function setupImages() {
|
||||
add_image_size('mailpoet_newsletter_max', 1320);
|
||||
}
|
||||
}
|
||||
|
@ -1,360 +1,360 @@
|
||||
<?php
|
||||
namespace MailPoet\Config;
|
||||
|
||||
use MailPoet\Models\Subscriber;
|
||||
use MailPoet\Models\Newsletter;
|
||||
|
||||
if(!defined('ABSPATH')) exit;
|
||||
|
||||
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
|
||||
|
||||
class Migrator {
|
||||
function __construct() {
|
||||
$this->prefix = Env::$db_prefix;
|
||||
$this->charset = Env::$db_charset;
|
||||
$this->models = array(
|
||||
'segments',
|
||||
'settings',
|
||||
'custom_fields',
|
||||
'sending_queues',
|
||||
'subscribers',
|
||||
'subscriber_segment',
|
||||
'subscriber_custom_field',
|
||||
'newsletters',
|
||||
'newsletter_templates',
|
||||
'newsletter_option_fields',
|
||||
'newsletter_option',
|
||||
'newsletter_segment',
|
||||
'newsletter_links',
|
||||
'newsletter_posts',
|
||||
'forms',
|
||||
'statistics_newsletters',
|
||||
'statistics_clicks',
|
||||
'statistics_opens',
|
||||
'statistics_unsubscribes',
|
||||
'statistics_forms'
|
||||
);
|
||||
}
|
||||
|
||||
function up() {
|
||||
global $wpdb;
|
||||
|
||||
$_this = $this;
|
||||
$migrate = function($model) use($_this) {
|
||||
dbDelta($_this->$model());
|
||||
};
|
||||
|
||||
array_map($migrate, $this->models);
|
||||
}
|
||||
|
||||
function down() {
|
||||
global $wpdb;
|
||||
|
||||
$drop_table = function($model) use($wpdb) {
|
||||
$table = $this->prefix . $model;
|
||||
$wpdb->query("DROP TABLE {$table}");
|
||||
};
|
||||
|
||||
array_map($drop_table, $this->models);
|
||||
}
|
||||
|
||||
function segments() {
|
||||
$attributes = array(
|
||||
'id mediumint(9) NOT NULL AUTO_INCREMENT,',
|
||||
'name varchar(90) NOT NULL,',
|
||||
'type varchar(90) NOT NULL DEFAULT "default",',
|
||||
'description varchar(250) NOT NULL DEFAULT "",',
|
||||
'created_at TIMESTAMP NULL,',
|
||||
'updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,',
|
||||
'deleted_at TIMESTAMP NULL,',
|
||||
'PRIMARY KEY (id),',
|
||||
'UNIQUE KEY name (name)'
|
||||
);
|
||||
return $this->sqlify(__FUNCTION__, $attributes);
|
||||
}
|
||||
|
||||
function settings() {
|
||||
$attributes = array(
|
||||
'id mediumint(9) NOT NULL AUTO_INCREMENT,',
|
||||
'name varchar(20) NOT NULL,',
|
||||
'value longtext,',
|
||||
'created_at TIMESTAMP NULL,',
|
||||
'updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,',
|
||||
'PRIMARY KEY (id),',
|
||||
'UNIQUE KEY name (name)'
|
||||
);
|
||||
return $this->sqlify(__FUNCTION__, $attributes);
|
||||
}
|
||||
|
||||
function custom_fields() {
|
||||
$attributes = array(
|
||||
'id mediumint(9) NOT NULL AUTO_INCREMENT,',
|
||||
'name varchar(90) NOT NULL,',
|
||||
'type varchar(90) NOT NULL,',
|
||||
'params longtext NOT NULL,',
|
||||
'created_at TIMESTAMP NULL,',
|
||||
'updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,',
|
||||
'PRIMARY KEY (id),',
|
||||
'UNIQUE KEY name (name)'
|
||||
);
|
||||
return $this->sqlify(__FUNCTION__, $attributes);
|
||||
}
|
||||
|
||||
function sending_queues() {
|
||||
$attributes = array(
|
||||
'id mediumint(9) NOT NULL AUTO_INCREMENT,',
|
||||
'newsletter_id mediumint(9) NOT NULL,',
|
||||
'newsletter_rendered_body longtext,',
|
||||
'newsletter_rendered_subject varchar(250) NULL DEFAULT NULL,',
|
||||
'subscribers longtext,',
|
||||
'status varchar(12) NULL DEFAULT NULL,',
|
||||
'priority mediumint(9) NOT NULL DEFAULT 0,',
|
||||
'count_total mediumint(9) NOT NULL DEFAULT 0,',
|
||||
'count_processed mediumint(9) NOT NULL DEFAULT 0,',
|
||||
'count_to_process mediumint(9) NOT NULL DEFAULT 0,',
|
||||
'count_failed mediumint(9) NOT NULL DEFAULT 0,',
|
||||
'scheduled_at TIMESTAMP NULL,',
|
||||
'processed_at TIMESTAMP NULL,',
|
||||
'created_at TIMESTAMP NULL,',
|
||||
'updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,',
|
||||
'deleted_at TIMESTAMP NULL,',
|
||||
'PRIMARY KEY (id)',
|
||||
);
|
||||
return $this->sqlify(__FUNCTION__, $attributes);
|
||||
}
|
||||
|
||||
function subscribers() {
|
||||
$attributes = array(
|
||||
'id mediumint(9) NOT NULL AUTO_INCREMENT,',
|
||||
'wp_user_id bigint(20) NULL,',
|
||||
'first_name tinytext NOT NULL DEFAULT "",',
|
||||
'last_name tinytext NOT NULL DEFAULT "",',
|
||||
'email varchar(150) NOT NULL,',
|
||||
'status varchar(12) NOT NULL DEFAULT "' . Subscriber::STATUS_UNCONFIRMED . '",',
|
||||
'created_at TIMESTAMP NULL,',
|
||||
'updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,',
|
||||
'deleted_at TIMESTAMP NULL,',
|
||||
'PRIMARY KEY (id),',
|
||||
'UNIQUE KEY email (email)'
|
||||
);
|
||||
return $this->sqlify(__FUNCTION__, $attributes);
|
||||
}
|
||||
|
||||
function subscriber_segment() {
|
||||
$attributes = array(
|
||||
'id mediumint(9) NOT NULL AUTO_INCREMENT,',
|
||||
'subscriber_id mediumint(9) NOT NULL,',
|
||||
'segment_id mediumint(9) NOT NULL,',
|
||||
'status varchar(12) NOT NULL DEFAULT "' . Subscriber::STATUS_SUBSCRIBED . '",',
|
||||
'created_at TIMESTAMP NULL,',
|
||||
'updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,',
|
||||
'PRIMARY KEY (id),',
|
||||
'UNIQUE KEY subscriber_segment (subscriber_id,segment_id)'
|
||||
);
|
||||
return $this->sqlify(__FUNCTION__, $attributes);
|
||||
}
|
||||
|
||||
function subscriber_custom_field() {
|
||||
$attributes = array(
|
||||
'id mediumint(9) NOT NULL AUTO_INCREMENT,',
|
||||
'subscriber_id mediumint(9) NOT NULL,',
|
||||
'custom_field_id mediumint(9) NOT NULL,',
|
||||
'value varchar(255) NOT NULL DEFAULT "",',
|
||||
'created_at TIMESTAMP NULL,',
|
||||
'updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,',
|
||||
'PRIMARY KEY (id),',
|
||||
'UNIQUE KEY subscriber_id_custom_field_id (subscriber_id,custom_field_id)'
|
||||
);
|
||||
return $this->sqlify(__FUNCTION__, $attributes);
|
||||
}
|
||||
|
||||
function newsletters() {
|
||||
$attributes = array(
|
||||
'id mediumint(9) NOT NULL AUTO_INCREMENT,',
|
||||
'subject varchar(250) NOT NULL DEFAULT "",',
|
||||
'type varchar(20) NOT NULL DEFAULT "standard",',
|
||||
'sender_address varchar(150) NOT NULL DEFAULT "",',
|
||||
'sender_name varchar(150) NOT NULL DEFAULT "",',
|
||||
'status varchar(20) NOT NULL DEFAULT "'.Newsletter::STATUS_DRAFT.'",',
|
||||
'reply_to_address varchar(150) NOT NULL DEFAULT "",',
|
||||
'reply_to_name varchar(150) NOT NULL DEFAULT "",',
|
||||
'preheader varchar(250) NOT NULL DEFAULT "",',
|
||||
'body longtext,',
|
||||
'created_at TIMESTAMP NULL,',
|
||||
'updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,',
|
||||
'deleted_at TIMESTAMP NULL,',
|
||||
'PRIMARY KEY (id)'
|
||||
);
|
||||
return $this->sqlify(__FUNCTION__, $attributes);
|
||||
}
|
||||
|
||||
function newsletter_templates() {
|
||||
$attributes = array(
|
||||
'id mediumint(9) NOT NULL AUTO_INCREMENT,',
|
||||
'name varchar(250) NOT NULL,',
|
||||
'description varchar(250) NOT NULL,',
|
||||
'body LONGTEXT,',
|
||||
'thumbnail LONGTEXT,',
|
||||
'readonly TINYINT(1) DEFAULT 0,',
|
||||
'created_at TIMESTAMP NULL,',
|
||||
'updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,',
|
||||
'PRIMARY KEY (id)'
|
||||
);
|
||||
return $this->sqlify(__FUNCTION__, $attributes);
|
||||
}
|
||||
|
||||
function newsletter_option_fields() {
|
||||
$attributes = array(
|
||||
'id mediumint(9) NOT NULL AUTO_INCREMENT,',
|
||||
'name varchar(90) NOT NULL,',
|
||||
'newsletter_type varchar(90) NOT NULL,',
|
||||
'created_at TIMESTAMP NULL,',
|
||||
'updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,',
|
||||
'PRIMARY KEY (id),',
|
||||
'UNIQUE KEY name_newsletter_type (newsletter_type,name)'
|
||||
);
|
||||
return $this->sqlify(__FUNCTION__, $attributes);
|
||||
}
|
||||
|
||||
function newsletter_option() {
|
||||
$attributes = array(
|
||||
'id mediumint(9) NOT NULL AUTO_INCREMENT,',
|
||||
'newsletter_id mediumint(9) NOT NULL,',
|
||||
'option_field_id mediumint(9) NOT NULL,',
|
||||
'value varchar(255) NOT NULL DEFAULT "",',
|
||||
'created_at TIMESTAMP NULL,',
|
||||
'updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,',
|
||||
'PRIMARY KEY (id),',
|
||||
'UNIQUE KEY newsletter_id_option_field_id (newsletter_id,option_field_id)'
|
||||
);
|
||||
return $this->sqlify(__FUNCTION__, $attributes);
|
||||
}
|
||||
|
||||
function newsletter_segment() {
|
||||
$attributes = array(
|
||||
'id mediumint(9) NOT NULL AUTO_INCREMENT,',
|
||||
'newsletter_id mediumint(9) NOT NULL,',
|
||||
'segment_id mediumint(9) NOT NULL,',
|
||||
'created_at TIMESTAMP NULL,',
|
||||
'updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,',
|
||||
'PRIMARY KEY (id),',
|
||||
'UNIQUE KEY newsletter_segment (newsletter_id,segment_id)'
|
||||
);
|
||||
return $this->sqlify(__FUNCTION__, $attributes);
|
||||
}
|
||||
|
||||
function newsletter_links() {
|
||||
$attributes = array(
|
||||
'id mediumint(9) NOT NULL AUTO_INCREMENT,',
|
||||
'newsletter_id mediumint(9) NOT NULL,',
|
||||
'queue_id mediumint(9) NOT NULL,',
|
||||
'url varchar(255) NOT NULL,',
|
||||
'hash varchar(20) NOT NULL,',
|
||||
'created_at TIMESTAMP NULL,',
|
||||
'updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,',
|
||||
'PRIMARY KEY (id)',
|
||||
);
|
||||
return $this->sqlify(__FUNCTION__, $attributes);
|
||||
}
|
||||
|
||||
function newsletter_posts() {
|
||||
$attributes = array(
|
||||
'id mediumint(9) NOT NULL AUTO_INCREMENT,',
|
||||
'newsletter_id mediumint(9) NOT NULL,',
|
||||
'post_id mediumint(9) NOT NULL,',
|
||||
'created_at TIMESTAMP NULL,',
|
||||
'updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,',
|
||||
'PRIMARY KEY (id)',
|
||||
);
|
||||
return $this->sqlify(__FUNCTION__, $attributes);
|
||||
}
|
||||
|
||||
function forms() {
|
||||
$attributes = array(
|
||||
'id mediumint(9) NOT NULL AUTO_INCREMENT,',
|
||||
'name varchar(90) NOT NULL,',
|
||||
'body longtext,',
|
||||
'settings longtext,',
|
||||
'styles longtext,',
|
||||
'created_at TIMESTAMP NULL,',
|
||||
'updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,',
|
||||
'deleted_at TIMESTAMP NULL,',
|
||||
'PRIMARY KEY (id)'
|
||||
);
|
||||
return $this->sqlify(__FUNCTION__, $attributes);
|
||||
}
|
||||
|
||||
function statistics_newsletters() {
|
||||
$attributes = array(
|
||||
'id mediumint(9) NOT NULL AUTO_INCREMENT,',
|
||||
'newsletter_id mediumint(9) NOT NULL,',
|
||||
'subscriber_id mediumint(9) NOT NULL,',
|
||||
'queue_id mediumint(9) NOT NULL,',
|
||||
'sent_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,',
|
||||
'PRIMARY KEY (id)',
|
||||
);
|
||||
return $this->sqlify(__FUNCTION__, $attributes);
|
||||
}
|
||||
|
||||
function statistics_clicks() {
|
||||
$attributes = array(
|
||||
'id mediumint(9) NOT NULL AUTO_INCREMENT,',
|
||||
'newsletter_id mediumint(9) NOT NULL,',
|
||||
'subscriber_id mediumint(9) NOT NULL,',
|
||||
'queue_id mediumint(9) NOT NULL,',
|
||||
'link_id mediumint(9) NOT NULL,',
|
||||
'count mediumint(9) NOT NULL,',
|
||||
'created_at TIMESTAMP NULL,',
|
||||
'updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,',
|
||||
'PRIMARY KEY (id)',
|
||||
);
|
||||
return $this->sqlify(__FUNCTION__, $attributes);
|
||||
}
|
||||
|
||||
function statistics_opens() {
|
||||
$attributes = array(
|
||||
'id mediumint(9) NOT NULL AUTO_INCREMENT,',
|
||||
'newsletter_id mediumint(9) NOT NULL,',
|
||||
'subscriber_id mediumint(9) NOT NULL,',
|
||||
'queue_id mediumint(9) NOT NULL,',
|
||||
'created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,',
|
||||
'PRIMARY KEY (id)',
|
||||
);
|
||||
return $this->sqlify(__FUNCTION__, $attributes);
|
||||
}
|
||||
|
||||
function statistics_unsubscribes() {
|
||||
$attributes = array(
|
||||
'id mediumint(9) NOT NULL AUTO_INCREMENT,',
|
||||
'newsletter_id mediumint(9) NOT NULL,',
|
||||
'subscriber_id mediumint(9) NOT NULL,',
|
||||
'queue_id mediumint(9) NOT NULL,',
|
||||
'created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,',
|
||||
'PRIMARY KEY (id)',
|
||||
);
|
||||
return $this->sqlify(__FUNCTION__, $attributes);
|
||||
}
|
||||
|
||||
function statistics_forms() {
|
||||
$attributes = array(
|
||||
'id mediumint(9) NOT NULL AUTO_INCREMENT,',
|
||||
'form_id mediumint(9) NOT NULL,',
|
||||
'subscriber_id mediumint(9) NOT NULL,',
|
||||
'created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,',
|
||||
'PRIMARY KEY (id),',
|
||||
'UNIQUE KEY form_subscriber (form_id,subscriber_id)'
|
||||
);
|
||||
return $this->sqlify(__FUNCTION__, $attributes);
|
||||
}
|
||||
|
||||
private function sqlify($model, $attributes) {
|
||||
$table = $this->prefix . $model;
|
||||
|
||||
$sql = array();
|
||||
$sql[] = "CREATE TABLE " . $table . " (";
|
||||
$sql = array_merge($sql, $attributes);
|
||||
$sql[] = ") " . $this->charset . ";";
|
||||
|
||||
return implode("\n", $sql);
|
||||
}
|
||||
}
|
||||
<?php
|
||||
namespace MailPoet\Config;
|
||||
|
||||
use MailPoet\Models\Subscriber;
|
||||
use MailPoet\Models\Newsletter;
|
||||
|
||||
if(!defined('ABSPATH')) exit;
|
||||
|
||||
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
|
||||
|
||||
class Migrator {
|
||||
function __construct() {
|
||||
$this->prefix = Env::$db_prefix;
|
||||
$this->charset = Env::$db_charset;
|
||||
$this->models = array(
|
||||
'segments',
|
||||
'settings',
|
||||
'custom_fields',
|
||||
'sending_queues',
|
||||
'subscribers',
|
||||
'subscriber_segment',
|
||||
'subscriber_custom_field',
|
||||
'newsletters',
|
||||
'newsletter_templates',
|
||||
'newsletter_option_fields',
|
||||
'newsletter_option',
|
||||
'newsletter_segment',
|
||||
'newsletter_links',
|
||||
'newsletter_posts',
|
||||
'forms',
|
||||
'statistics_newsletters',
|
||||
'statistics_clicks',
|
||||
'statistics_opens',
|
||||
'statistics_unsubscribes',
|
||||
'statistics_forms'
|
||||
);
|
||||
}
|
||||
|
||||
function up() {
|
||||
global $wpdb;
|
||||
|
||||
$_this = $this;
|
||||
$migrate = function($model) use($_this) {
|
||||
dbDelta($_this->$model());
|
||||
};
|
||||
|
||||
array_map($migrate, $this->models);
|
||||
}
|
||||
|
||||
function down() {
|
||||
global $wpdb;
|
||||
|
||||
$drop_table = function($model) use($wpdb) {
|
||||
$table = $this->prefix . $model;
|
||||
$wpdb->query("DROP TABLE {$table}");
|
||||
};
|
||||
|
||||
array_map($drop_table, $this->models);
|
||||
}
|
||||
|
||||
function segments() {
|
||||
$attributes = array(
|
||||
'id mediumint(9) NOT NULL AUTO_INCREMENT,',
|
||||
'name varchar(90) NOT NULL,',
|
||||
'type varchar(90) NOT NULL DEFAULT "default",',
|
||||
'description varchar(250) NOT NULL DEFAULT "",',
|
||||
'created_at TIMESTAMP NULL,',
|
||||
'updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,',
|
||||
'deleted_at TIMESTAMP NULL,',
|
||||
'PRIMARY KEY (id),',
|
||||
'UNIQUE KEY name (name)'
|
||||
);
|
||||
return $this->sqlify(__FUNCTION__, $attributes);
|
||||
}
|
||||
|
||||
function settings() {
|
||||
$attributes = array(
|
||||
'id mediumint(9) NOT NULL AUTO_INCREMENT,',
|
||||
'name varchar(20) NOT NULL,',
|
||||
'value longtext,',
|
||||
'created_at TIMESTAMP NULL,',
|
||||
'updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,',
|
||||
'PRIMARY KEY (id),',
|
||||
'UNIQUE KEY name (name)'
|
||||
);
|
||||
return $this->sqlify(__FUNCTION__, $attributes);
|
||||
}
|
||||
|
||||
function custom_fields() {
|
||||
$attributes = array(
|
||||
'id mediumint(9) NOT NULL AUTO_INCREMENT,',
|
||||
'name varchar(90) NOT NULL,',
|
||||
'type varchar(90) NOT NULL,',
|
||||
'params longtext NOT NULL,',
|
||||
'created_at TIMESTAMP NULL,',
|
||||
'updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,',
|
||||
'PRIMARY KEY (id),',
|
||||
'UNIQUE KEY name (name)'
|
||||
);
|
||||
return $this->sqlify(__FUNCTION__, $attributes);
|
||||
}
|
||||
|
||||
function sending_queues() {
|
||||
$attributes = array(
|
||||
'id mediumint(9) NOT NULL AUTO_INCREMENT,',
|
||||
'newsletter_id mediumint(9) NOT NULL,',
|
||||
'newsletter_rendered_body longtext,',
|
||||
'newsletter_rendered_subject varchar(250) NULL DEFAULT NULL,',
|
||||
'subscribers longtext,',
|
||||
'status varchar(12) NULL DEFAULT NULL,',
|
||||
'priority mediumint(9) NOT NULL DEFAULT 0,',
|
||||
'count_total mediumint(9) NOT NULL DEFAULT 0,',
|
||||
'count_processed mediumint(9) NOT NULL DEFAULT 0,',
|
||||
'count_to_process mediumint(9) NOT NULL DEFAULT 0,',
|
||||
'count_failed mediumint(9) NOT NULL DEFAULT 0,',
|
||||
'scheduled_at TIMESTAMP NULL,',
|
||||
'processed_at TIMESTAMP NULL,',
|
||||
'created_at TIMESTAMP NULL,',
|
||||
'updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,',
|
||||
'deleted_at TIMESTAMP NULL,',
|
||||
'PRIMARY KEY (id)',
|
||||
);
|
||||
return $this->sqlify(__FUNCTION__, $attributes);
|
||||
}
|
||||
|
||||
function subscribers() {
|
||||
$attributes = array(
|
||||
'id mediumint(9) NOT NULL AUTO_INCREMENT,',
|
||||
'wp_user_id bigint(20) NULL,',
|
||||
'first_name tinytext NOT NULL DEFAULT "",',
|
||||
'last_name tinytext NOT NULL DEFAULT "",',
|
||||
'email varchar(150) NOT NULL,',
|
||||
'status varchar(12) NOT NULL DEFAULT "' . Subscriber::STATUS_UNCONFIRMED . '",',
|
||||
'created_at TIMESTAMP NULL,',
|
||||
'updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,',
|
||||
'deleted_at TIMESTAMP NULL,',
|
||||
'PRIMARY KEY (id),',
|
||||
'UNIQUE KEY email (email)'
|
||||
);
|
||||
return $this->sqlify(__FUNCTION__, $attributes);
|
||||
}
|
||||
|
||||
function subscriber_segment() {
|
||||
$attributes = array(
|
||||
'id mediumint(9) NOT NULL AUTO_INCREMENT,',
|
||||
'subscriber_id mediumint(9) NOT NULL,',
|
||||
'segment_id mediumint(9) NOT NULL,',
|
||||
'status varchar(12) NOT NULL DEFAULT "' . Subscriber::STATUS_SUBSCRIBED . '",',
|
||||
'created_at TIMESTAMP NULL,',
|
||||
'updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,',
|
||||
'PRIMARY KEY (id),',
|
||||
'UNIQUE KEY subscriber_segment (subscriber_id,segment_id)'
|
||||
);
|
||||
return $this->sqlify(__FUNCTION__, $attributes);
|
||||
}
|
||||
|
||||
function subscriber_custom_field() {
|
||||
$attributes = array(
|
||||
'id mediumint(9) NOT NULL AUTO_INCREMENT,',
|
||||
'subscriber_id mediumint(9) NOT NULL,',
|
||||
'custom_field_id mediumint(9) NOT NULL,',
|
||||
'value varchar(255) NOT NULL DEFAULT "",',
|
||||
'created_at TIMESTAMP NULL,',
|
||||
'updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,',
|
||||
'PRIMARY KEY (id),',
|
||||
'UNIQUE KEY subscriber_id_custom_field_id (subscriber_id,custom_field_id)'
|
||||
);
|
||||
return $this->sqlify(__FUNCTION__, $attributes);
|
||||
}
|
||||
|
||||
function newsletters() {
|
||||
$attributes = array(
|
||||
'id mediumint(9) NOT NULL AUTO_INCREMENT,',
|
||||
'subject varchar(250) NOT NULL DEFAULT "",',
|
||||
'type varchar(20) NOT NULL DEFAULT "standard",',
|
||||
'sender_address varchar(150) NOT NULL DEFAULT "",',
|
||||
'sender_name varchar(150) NOT NULL DEFAULT "",',
|
||||
'status varchar(20) NOT NULL DEFAULT "'.Newsletter::STATUS_DRAFT.'",',
|
||||
'reply_to_address varchar(150) NOT NULL DEFAULT "",',
|
||||
'reply_to_name varchar(150) NOT NULL DEFAULT "",',
|
||||
'preheader varchar(250) NOT NULL DEFAULT "",',
|
||||
'body longtext,',
|
||||
'created_at TIMESTAMP NULL,',
|
||||
'updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,',
|
||||
'deleted_at TIMESTAMP NULL,',
|
||||
'PRIMARY KEY (id)'
|
||||
);
|
||||
return $this->sqlify(__FUNCTION__, $attributes);
|
||||
}
|
||||
|
||||
function newsletter_templates() {
|
||||
$attributes = array(
|
||||
'id mediumint(9) NOT NULL AUTO_INCREMENT,',
|
||||
'name varchar(250) NOT NULL,',
|
||||
'description varchar(250) NOT NULL,',
|
||||
'body LONGTEXT,',
|
||||
'thumbnail LONGTEXT,',
|
||||
'readonly TINYINT(1) DEFAULT 0,',
|
||||
'created_at TIMESTAMP NULL,',
|
||||
'updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,',
|
||||
'PRIMARY KEY (id)'
|
||||
);
|
||||
return $this->sqlify(__FUNCTION__, $attributes);
|
||||
}
|
||||
|
||||
function newsletter_option_fields() {
|
||||
$attributes = array(
|
||||
'id mediumint(9) NOT NULL AUTO_INCREMENT,',
|
||||
'name varchar(90) NOT NULL,',
|
||||
'newsletter_type varchar(90) NOT NULL,',
|
||||
'created_at TIMESTAMP NULL,',
|
||||
'updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,',
|
||||
'PRIMARY KEY (id),',
|
||||
'UNIQUE KEY name_newsletter_type (newsletter_type,name)'
|
||||
);
|
||||
return $this->sqlify(__FUNCTION__, $attributes);
|
||||
}
|
||||
|
||||
function newsletter_option() {
|
||||
$attributes = array(
|
||||
'id mediumint(9) NOT NULL AUTO_INCREMENT,',
|
||||
'newsletter_id mediumint(9) NOT NULL,',
|
||||
'option_field_id mediumint(9) NOT NULL,',
|
||||
'value varchar(255) NOT NULL DEFAULT "",',
|
||||
'created_at TIMESTAMP NULL,',
|
||||
'updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,',
|
||||
'PRIMARY KEY (id),',
|
||||
'UNIQUE KEY newsletter_id_option_field_id (newsletter_id,option_field_id)'
|
||||
);
|
||||
return $this->sqlify(__FUNCTION__, $attributes);
|
||||
}
|
||||
|
||||
function newsletter_segment() {
|
||||
$attributes = array(
|
||||
'id mediumint(9) NOT NULL AUTO_INCREMENT,',
|
||||
'newsletter_id mediumint(9) NOT NULL,',
|
||||
'segment_id mediumint(9) NOT NULL,',
|
||||
'created_at TIMESTAMP NULL,',
|
||||
'updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,',
|
||||
'PRIMARY KEY (id),',
|
||||
'UNIQUE KEY newsletter_segment (newsletter_id,segment_id)'
|
||||
);
|
||||
return $this->sqlify(__FUNCTION__, $attributes);
|
||||
}
|
||||
|
||||
function newsletter_links() {
|
||||
$attributes = array(
|
||||
'id mediumint(9) NOT NULL AUTO_INCREMENT,',
|
||||
'newsletter_id mediumint(9) NOT NULL,',
|
||||
'queue_id mediumint(9) NOT NULL,',
|
||||
'url varchar(255) NOT NULL,',
|
||||
'hash varchar(20) NOT NULL,',
|
||||
'created_at TIMESTAMP NULL,',
|
||||
'updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,',
|
||||
'PRIMARY KEY (id)',
|
||||
);
|
||||
return $this->sqlify(__FUNCTION__, $attributes);
|
||||
}
|
||||
|
||||
function newsletter_posts() {
|
||||
$attributes = array(
|
||||
'id mediumint(9) NOT NULL AUTO_INCREMENT,',
|
||||
'newsletter_id mediumint(9) NOT NULL,',
|
||||
'post_id mediumint(9) NOT NULL,',
|
||||
'created_at TIMESTAMP NULL,',
|
||||
'updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,',
|
||||
'PRIMARY KEY (id)',
|
||||
);
|
||||
return $this->sqlify(__FUNCTION__, $attributes);
|
||||
}
|
||||
|
||||
function forms() {
|
||||
$attributes = array(
|
||||
'id mediumint(9) NOT NULL AUTO_INCREMENT,',
|
||||
'name varchar(90) NOT NULL,',
|
||||
'body longtext,',
|
||||
'settings longtext,',
|
||||
'styles longtext,',
|
||||
'created_at TIMESTAMP NULL,',
|
||||
'updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,',
|
||||
'deleted_at TIMESTAMP NULL,',
|
||||
'PRIMARY KEY (id)'
|
||||
);
|
||||
return $this->sqlify(__FUNCTION__, $attributes);
|
||||
}
|
||||
|
||||
function statistics_newsletters() {
|
||||
$attributes = array(
|
||||
'id mediumint(9) NOT NULL AUTO_INCREMENT,',
|
||||
'newsletter_id mediumint(9) NOT NULL,',
|
||||
'subscriber_id mediumint(9) NOT NULL,',
|
||||
'queue_id mediumint(9) NOT NULL,',
|
||||
'sent_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,',
|
||||
'PRIMARY KEY (id)',
|
||||
);
|
||||
return $this->sqlify(__FUNCTION__, $attributes);
|
||||
}
|
||||
|
||||
function statistics_clicks() {
|
||||
$attributes = array(
|
||||
'id mediumint(9) NOT NULL AUTO_INCREMENT,',
|
||||
'newsletter_id mediumint(9) NOT NULL,',
|
||||
'subscriber_id mediumint(9) NOT NULL,',
|
||||
'queue_id mediumint(9) NOT NULL,',
|
||||
'link_id mediumint(9) NOT NULL,',
|
||||
'count mediumint(9) NOT NULL,',
|
||||
'created_at TIMESTAMP NULL,',
|
||||
'updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,',
|
||||
'PRIMARY KEY (id)',
|
||||
);
|
||||
return $this->sqlify(__FUNCTION__, $attributes);
|
||||
}
|
||||
|
||||
function statistics_opens() {
|
||||
$attributes = array(
|
||||
'id mediumint(9) NOT NULL AUTO_INCREMENT,',
|
||||
'newsletter_id mediumint(9) NOT NULL,',
|
||||
'subscriber_id mediumint(9) NOT NULL,',
|
||||
'queue_id mediumint(9) NOT NULL,',
|
||||
'created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,',
|
||||
'PRIMARY KEY (id)',
|
||||
);
|
||||
return $this->sqlify(__FUNCTION__, $attributes);
|
||||
}
|
||||
|
||||
function statistics_unsubscribes() {
|
||||
$attributes = array(
|
||||
'id mediumint(9) NOT NULL AUTO_INCREMENT,',
|
||||
'newsletter_id mediumint(9) NOT NULL,',
|
||||
'subscriber_id mediumint(9) NOT NULL,',
|
||||
'queue_id mediumint(9) NOT NULL,',
|
||||
'created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,',
|
||||
'PRIMARY KEY (id)',
|
||||
);
|
||||
return $this->sqlify(__FUNCTION__, $attributes);
|
||||
}
|
||||
|
||||
function statistics_forms() {
|
||||
$attributes = array(
|
||||
'id mediumint(9) NOT NULL AUTO_INCREMENT,',
|
||||
'form_id mediumint(9) NOT NULL,',
|
||||
'subscriber_id mediumint(9) NOT NULL,',
|
||||
'created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,',
|
||||
'PRIMARY KEY (id),',
|
||||
'UNIQUE KEY form_subscriber (form_id,subscriber_id)'
|
||||
);
|
||||
return $this->sqlify(__FUNCTION__, $attributes);
|
||||
}
|
||||
|
||||
private function sqlify($model, $attributes) {
|
||||
$table = $this->prefix . $model;
|
||||
|
||||
$sql = array();
|
||||
$sql[] = "CREATE TABLE " . $table . " (";
|
||||
$sql = array_merge($sql, $attributes);
|
||||
$sql[] = ") " . $this->charset . ";";
|
||||
|
||||
return implode("\n", $sql);
|
||||
}
|
||||
}
|
||||
|
@ -1,160 +1,160 @@
|
||||
<?php
|
||||
namespace MailPoet\Config;
|
||||
use \MailPoet\Util\Security;
|
||||
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 {
|
||||
$this->setupAdminDependencies();
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
'token' => Security::generateToken()
|
||||
)
|
||||
);
|
||||
|
||||
echo $this->renderer->render('form/iframe.html', $data);
|
||||
}
|
||||
exit();
|
||||
}
|
||||
}
|
||||
|
||||
function registerWidget() {
|
||||
register_widget('\MailPoet\Form\Widget');
|
||||
}
|
||||
|
||||
function setupDependencies() {
|
||||
wp_enqueue_style('mailpoet_public', Env::$assets_url.'/css/public.css');
|
||||
|
||||
wp_enqueue_script('mailpoet_vendor',
|
||||
Env::$assets_url.'/js/vendor.js',
|
||||
array(),
|
||||
Env::$version,
|
||||
true
|
||||
);
|
||||
|
||||
wp_enqueue_script('mailpoet_public',
|
||||
Env::$assets_url.'/js/public.js',
|
||||
array(),
|
||||
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),
|
||||
'token' => Security::generateToken()
|
||||
));
|
||||
}
|
||||
|
||||
function setupAdminDependencies() {
|
||||
if(
|
||||
empty($_GET['page'])
|
||||
or
|
||||
isset($_GET['page']) && strpos($_GET['page'], 'mailpoet') === false
|
||||
) {
|
||||
wp_enqueue_script('mailpoet_vendor',
|
||||
Env::$assets_url.'/js/vendor.js',
|
||||
array(),
|
||||
Env::$version,
|
||||
true
|
||||
);
|
||||
|
||||
wp_enqueue_script('mailpoet_admin',
|
||||
Env::$assets_url.'/js/mailpoet.js',
|
||||
array(),
|
||||
Env::$version,
|
||||
true
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: extract this method into an Initializer
|
||||
// - the "ajax" part might probably be useless
|
||||
// - the "post" (non-ajax) part needs to be redone properly
|
||||
function setupActions() {
|
||||
// ajax requests
|
||||
add_action(
|
||||
'wp_ajax_mailpoet_form_subscribe',
|
||||
'mailpoet_form_subscribe'
|
||||
);
|
||||
add_action(
|
||||
'wp_ajax_nopriv_mailpoet_form_subscribe',
|
||||
'mailpoet_form_subscribe'
|
||||
);
|
||||
// post request
|
||||
add_action(
|
||||
'admin_post_nopriv_mailpoet_form_subscribe',
|
||||
'mailpoet_form_subscribe'
|
||||
);
|
||||
add_action(
|
||||
'admin_post_mailpoet_form_subscribe',
|
||||
'mailpoet_form_subscribe'
|
||||
);
|
||||
add_action(
|
||||
'init',
|
||||
'mailpoet_form_subscribe'
|
||||
);
|
||||
}
|
||||
}
|
||||
<?php
|
||||
namespace MailPoet\Config;
|
||||
use \MailPoet\Util\Security;
|
||||
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 {
|
||||
$this->setupAdminDependencies();
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
'token' => Security::generateToken()
|
||||
)
|
||||
);
|
||||
|
||||
echo $this->renderer->render('form/iframe.html', $data);
|
||||
}
|
||||
exit();
|
||||
}
|
||||
}
|
||||
|
||||
function registerWidget() {
|
||||
register_widget('\MailPoet\Form\Widget');
|
||||
}
|
||||
|
||||
function setupDependencies() {
|
||||
wp_enqueue_style('mailpoet_public', Env::$assets_url.'/css/public.css');
|
||||
|
||||
wp_enqueue_script('mailpoet_vendor',
|
||||
Env::$assets_url.'/js/vendor.js',
|
||||
array(),
|
||||
Env::$version,
|
||||
true
|
||||
);
|
||||
|
||||
wp_enqueue_script('mailpoet_public',
|
||||
Env::$assets_url.'/js/public.js',
|
||||
array(),
|
||||
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),
|
||||
'token' => Security::generateToken()
|
||||
));
|
||||
}
|
||||
|
||||
function setupAdminDependencies() {
|
||||
if(
|
||||
empty($_GET['page'])
|
||||
or
|
||||
isset($_GET['page']) && strpos($_GET['page'], 'mailpoet') === false
|
||||
) {
|
||||
wp_enqueue_script('mailpoet_vendor',
|
||||
Env::$assets_url.'/js/vendor.js',
|
||||
array(),
|
||||
Env::$version,
|
||||
true
|
||||
);
|
||||
|
||||
wp_enqueue_script('mailpoet_admin',
|
||||
Env::$assets_url.'/js/mailpoet.js',
|
||||
array(),
|
||||
Env::$version,
|
||||
true
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: extract this method into an Initializer
|
||||
// - the "ajax" part might probably be useless
|
||||
// - the "post" (non-ajax) part needs to be redone properly
|
||||
function setupActions() {
|
||||
// ajax requests
|
||||
add_action(
|
||||
'wp_ajax_mailpoet_form_subscribe',
|
||||
'mailpoet_form_subscribe'
|
||||
);
|
||||
add_action(
|
||||
'wp_ajax_nopriv_mailpoet_form_subscribe',
|
||||
'mailpoet_form_subscribe'
|
||||
);
|
||||
// post request
|
||||
add_action(
|
||||
'admin_post_nopriv_mailpoet_form_subscribe',
|
||||
'mailpoet_form_subscribe'
|
||||
);
|
||||
add_action(
|
||||
'admin_post_mailpoet_form_subscribe',
|
||||
'mailpoet_form_subscribe'
|
||||
);
|
||||
add_action(
|
||||
'init',
|
||||
'mailpoet_form_subscribe'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -1,143 +1,143 @@
|
||||
<?php
|
||||
namespace MailPoet\Mailer;
|
||||
|
||||
use MailPoet\Models\Setting;
|
||||
|
||||
require_once(ABSPATH . 'wp-includes/pluggable.php');
|
||||
|
||||
if(!defined('ABSPATH')) exit;
|
||||
|
||||
class Mailer {
|
||||
public $mailer;
|
||||
public $sender;
|
||||
public $reply_to;
|
||||
public $mailer_instance;
|
||||
|
||||
function __construct($mailer = false, $sender = false, $reply_to = false) {
|
||||
$this->mailer = $this->getMailer($mailer);
|
||||
$this->sender = $this->getSender($sender);
|
||||
$this->reply_to = $this->getReplyTo($reply_to);
|
||||
$this->mailer_instance = $this->buildMailer();
|
||||
}
|
||||
|
||||
function send($newsletter, $subscriber) {
|
||||
$subscriber = $this->transformSubscriber($subscriber);
|
||||
return $this->mailer_instance->send($newsletter, $subscriber);
|
||||
}
|
||||
|
||||
function buildMailer() {
|
||||
switch($this->mailer['method']) {
|
||||
case 'AmazonSES':
|
||||
$mailer_instance = new $this->mailer['class'](
|
||||
$this->mailer['region'],
|
||||
$this->mailer['access_key'],
|
||||
$this->mailer['secret_key'],
|
||||
$this->sender,
|
||||
$this->reply_to
|
||||
);
|
||||
break;
|
||||
case 'ElasticEmail':
|
||||
$mailer_instance = new $this->mailer['class'](
|
||||
$this->mailer['api_key'],
|
||||
$this->sender,
|
||||
$this->reply_to
|
||||
);
|
||||
break;
|
||||
case 'MailGun':
|
||||
$mailer_instance = new $this->mailer['class'](
|
||||
$this->mailer['domain'],
|
||||
$this->mailer['api_key'],
|
||||
$this->sender,
|
||||
$this->reply_to
|
||||
);
|
||||
break;
|
||||
case 'MailPoet':
|
||||
$mailer_instance = new $this->mailer['class'](
|
||||
$this->mailer['mailpoet_api_key'],
|
||||
$this->sender,
|
||||
$this->reply_to
|
||||
);
|
||||
break;
|
||||
case 'SendGrid':
|
||||
$mailer_instance = new $this->mailer['class'](
|
||||
$this->mailer['api_key'],
|
||||
$this->sender,
|
||||
$this->reply_to
|
||||
);
|
||||
break;
|
||||
case 'PHPMail':
|
||||
$mailer_instance = new $this->mailer['class'](
|
||||
$this->sender,
|
||||
$this->reply_to
|
||||
);
|
||||
break;
|
||||
case 'SMTP':
|
||||
$mailer_instance = new $this->mailer['class'](
|
||||
$this->mailer['host'],
|
||||
$this->mailer['port'],
|
||||
$this->mailer['authentication'],
|
||||
$this->mailer['login'],
|
||||
$this->mailer['password'],
|
||||
$this->mailer['encryption'],
|
||||
$this->sender,
|
||||
$this->reply_to
|
||||
);
|
||||
break;
|
||||
default:
|
||||
throw new \Exception(__('Mailing method does not exist.'));
|
||||
}
|
||||
return $mailer_instance;
|
||||
}
|
||||
|
||||
function getMailer($mailer = false) {
|
||||
if(!$mailer) {
|
||||
$mailer = Setting::getValue('mta');
|
||||
if(!$mailer || !isset($mailer['method'])) throw new \Exception(__('Mailer is not configured.'));
|
||||
}
|
||||
$mailer['class'] = 'MailPoet\\Mailer\\Methods\\' . $mailer['method'];
|
||||
return $mailer;
|
||||
}
|
||||
|
||||
function getSender($sender = false) {
|
||||
if(empty($sender)) {
|
||||
$sender = Setting::getValue('sender', array());
|
||||
if(empty($sender['address'])) throw new \Exception(__('Sender name and email are not configured.'));
|
||||
}
|
||||
return array(
|
||||
'from_name' => $sender['name'],
|
||||
'from_email' => $sender['address'],
|
||||
'from_name_email' => sprintf('%s <%s>', $sender['name'], $sender['address'])
|
||||
);
|
||||
}
|
||||
|
||||
function getReplyTo($reply_to = false) {
|
||||
if(!$reply_to) {
|
||||
$reply_to = Setting::getValue('reply_to', null);
|
||||
if(!$reply_to) {
|
||||
$reply_to = array(
|
||||
'name' => $this->sender['from_name'],
|
||||
'address' => $this->sender['from_email']
|
||||
);
|
||||
}
|
||||
}
|
||||
if(empty($reply_to['address'])) {
|
||||
$reply_to['address'] = $this->sender['from_email'];
|
||||
}
|
||||
return array(
|
||||
'reply_to_name' => $reply_to['name'],
|
||||
'reply_to_email' => $reply_to['address'],
|
||||
'reply_to_name_email' => sprintf('%s <%s>', $reply_to['name'], $reply_to['address'])
|
||||
);
|
||||
}
|
||||
|
||||
function transformSubscriber($subscriber) {
|
||||
if(!is_array($subscriber)) return $subscriber;
|
||||
if(isset($subscriber['address'])) $subscriber['email'] = $subscriber['address'];
|
||||
$first_name = (isset($subscriber['first_name'])) ? $subscriber['first_name'] : '';
|
||||
$last_name = (isset($subscriber['last_name'])) ? $subscriber['last_name'] : '';
|
||||
if(!$first_name && !$last_name) return $subscriber['email'];
|
||||
$subscriber = sprintf('%s %s <%s>', $first_name, $last_name, $subscriber['email']);
|
||||
$subscriber = trim(preg_replace('!\s\s+!', ' ', $subscriber));
|
||||
return $subscriber;
|
||||
}
|
||||
}
|
||||
<?php
|
||||
namespace MailPoet\Mailer;
|
||||
|
||||
use MailPoet\Models\Setting;
|
||||
|
||||
require_once(ABSPATH . 'wp-includes/pluggable.php');
|
||||
|
||||
if(!defined('ABSPATH')) exit;
|
||||
|
||||
class Mailer {
|
||||
public $mailer;
|
||||
public $sender;
|
||||
public $reply_to;
|
||||
public $mailer_instance;
|
||||
|
||||
function __construct($mailer = false, $sender = false, $reply_to = false) {
|
||||
$this->mailer = $this->getMailer($mailer);
|
||||
$this->sender = $this->getSender($sender);
|
||||
$this->reply_to = $this->getReplyTo($reply_to);
|
||||
$this->mailer_instance = $this->buildMailer();
|
||||
}
|
||||
|
||||
function send($newsletter, $subscriber) {
|
||||
$subscriber = $this->transformSubscriber($subscriber);
|
||||
return $this->mailer_instance->send($newsletter, $subscriber);
|
||||
}
|
||||
|
||||
function buildMailer() {
|
||||
switch($this->mailer['method']) {
|
||||
case 'AmazonSES':
|
||||
$mailer_instance = new $this->mailer['class'](
|
||||
$this->mailer['region'],
|
||||
$this->mailer['access_key'],
|
||||
$this->mailer['secret_key'],
|
||||
$this->sender,
|
||||
$this->reply_to
|
||||
);
|
||||
break;
|
||||
case 'ElasticEmail':
|
||||
$mailer_instance = new $this->mailer['class'](
|
||||
$this->mailer['api_key'],
|
||||
$this->sender,
|
||||
$this->reply_to
|
||||
);
|
||||
break;
|
||||
case 'MailGun':
|
||||
$mailer_instance = new $this->mailer['class'](
|
||||
$this->mailer['domain'],
|
||||
$this->mailer['api_key'],
|
||||
$this->sender,
|
||||
$this->reply_to
|
||||
);
|
||||
break;
|
||||
case 'MailPoet':
|
||||
$mailer_instance = new $this->mailer['class'](
|
||||
$this->mailer['mailpoet_api_key'],
|
||||
$this->sender,
|
||||
$this->reply_to
|
||||
);
|
||||
break;
|
||||
case 'SendGrid':
|
||||
$mailer_instance = new $this->mailer['class'](
|
||||
$this->mailer['api_key'],
|
||||
$this->sender,
|
||||
$this->reply_to
|
||||
);
|
||||
break;
|
||||
case 'PHPMail':
|
||||
$mailer_instance = new $this->mailer['class'](
|
||||
$this->sender,
|
||||
$this->reply_to
|
||||
);
|
||||
break;
|
||||
case 'SMTP':
|
||||
$mailer_instance = new $this->mailer['class'](
|
||||
$this->mailer['host'],
|
||||
$this->mailer['port'],
|
||||
$this->mailer['authentication'],
|
||||
$this->mailer['login'],
|
||||
$this->mailer['password'],
|
||||
$this->mailer['encryption'],
|
||||
$this->sender,
|
||||
$this->reply_to
|
||||
);
|
||||
break;
|
||||
default:
|
||||
throw new \Exception(__('Mailing method does not exist.'));
|
||||
}
|
||||
return $mailer_instance;
|
||||
}
|
||||
|
||||
function getMailer($mailer = false) {
|
||||
if(!$mailer) {
|
||||
$mailer = Setting::getValue('mta');
|
||||
if(!$mailer || !isset($mailer['method'])) throw new \Exception(__('Mailer is not configured.'));
|
||||
}
|
||||
$mailer['class'] = 'MailPoet\\Mailer\\Methods\\' . $mailer['method'];
|
||||
return $mailer;
|
||||
}
|
||||
|
||||
function getSender($sender = false) {
|
||||
if(empty($sender)) {
|
||||
$sender = Setting::getValue('sender', array());
|
||||
if(empty($sender['address'])) throw new \Exception(__('Sender name and email are not configured.'));
|
||||
}
|
||||
return array(
|
||||
'from_name' => $sender['name'],
|
||||
'from_email' => $sender['address'],
|
||||
'from_name_email' => sprintf('%s <%s>', $sender['name'], $sender['address'])
|
||||
);
|
||||
}
|
||||
|
||||
function getReplyTo($reply_to = false) {
|
||||
if(!$reply_to) {
|
||||
$reply_to = Setting::getValue('reply_to', null);
|
||||
if(!$reply_to) {
|
||||
$reply_to = array(
|
||||
'name' => $this->sender['from_name'],
|
||||
'address' => $this->sender['from_email']
|
||||
);
|
||||
}
|
||||
}
|
||||
if(empty($reply_to['address'])) {
|
||||
$reply_to['address'] = $this->sender['from_email'];
|
||||
}
|
||||
return array(
|
||||
'reply_to_name' => $reply_to['name'],
|
||||
'reply_to_email' => $reply_to['address'],
|
||||
'reply_to_name_email' => sprintf('%s <%s>', $reply_to['name'], $reply_to['address'])
|
||||
);
|
||||
}
|
||||
|
||||
function transformSubscriber($subscriber) {
|
||||
if(!is_array($subscriber)) return $subscriber;
|
||||
if(isset($subscriber['address'])) $subscriber['email'] = $subscriber['address'];
|
||||
$first_name = (isset($subscriber['first_name'])) ? $subscriber['first_name'] : '';
|
||||
$last_name = (isset($subscriber['last_name'])) ? $subscriber['last_name'] : '';
|
||||
if(!$first_name && !$last_name) return $subscriber['email'];
|
||||
$subscriber = sprintf('%s %s <%s>', $first_name, $last_name, $subscriber['email']);
|
||||
$subscriber = trim(preg_replace('!\s\s+!', ' ', $subscriber));
|
||||
return $subscriber;
|
||||
}
|
||||
}
|
||||
|
@ -1,160 +1,160 @@
|
||||
<?php
|
||||
namespace MailPoet\Models;
|
||||
|
||||
if(!defined('ABSPATH')) exit;
|
||||
|
||||
class Model extends \Sudzy\ValidModel {
|
||||
protected $_errors;
|
||||
|
||||
function __construct() {
|
||||
$this->_errors = array();
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
static function create() {
|
||||
return parent::create();
|
||||
}
|
||||
|
||||
function getErrors() {
|
||||
if(empty($this->_errors)) {
|
||||
return false;
|
||||
} else {
|
||||
return $this->_errors;
|
||||
}
|
||||
}
|
||||
|
||||
function setError($error = '') {
|
||||
if(!empty($error)) {
|
||||
if(is_array($error)) {
|
||||
$this->_errors = array_merge($this->_errors, $error);
|
||||
$this->_errors = array_unique($this->_errors);
|
||||
} else {
|
||||
$this->_errors[] = $error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function save() {
|
||||
$this->setTimestamp();
|
||||
try {
|
||||
parent::save();
|
||||
} catch(\Sudzy\ValidationException $e) {
|
||||
$this->setError($e->getValidationErrors());
|
||||
} catch(\PDOException $e) {
|
||||
switch($e->getCode()) {
|
||||
case 23000:
|
||||
preg_match("/for key \'(.*?)\'/i", $e->getMessage(), $matches);
|
||||
if(isset($matches[1])) {
|
||||
$column = $matches[1];
|
||||
$this->setError(
|
||||
sprintf(
|
||||
__('Another record already exists. Please specify a different "%1$s".'),
|
||||
$column
|
||||
)
|
||||
);
|
||||
} else {
|
||||
$this->setError($e->getMessage());
|
||||
}
|
||||
break;
|
||||
default:
|
||||
$this->setError($e->getMessage());
|
||||
}
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
function trash() {
|
||||
return $this->set_expr('deleted_at', 'NOW()')->save();
|
||||
}
|
||||
|
||||
static function bulkTrash($orm) {
|
||||
$model = get_called_class();
|
||||
return self::bulkAction($orm, function($ids) use($model) {
|
||||
self::rawExecute(join(' ', array(
|
||||
'UPDATE `'.$model::$_table.'`',
|
||||
'SET `deleted_at` = NOW()',
|
||||
'WHERE `id` IN ('.rtrim(str_repeat('?,', count($ids)), ',').')'
|
||||
)),
|
||||
$ids
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
static function bulkDelete($orm) {
|
||||
$model = get_called_class();
|
||||
return self::bulkAction($orm, function($ids) use($model) {
|
||||
$model::whereIn('id', $ids)->deleteMany();
|
||||
});
|
||||
}
|
||||
|
||||
function restore() {
|
||||
return $this->set_expr('deleted_at', 'NULL')->save();
|
||||
}
|
||||
|
||||
static function bulkRestore($orm) {
|
||||
$model = get_called_class();
|
||||
return self::bulkAction($orm, function($ids) use($model) {
|
||||
self::rawExecute(join(' ', array(
|
||||
'UPDATE `'.$model::$_table.'`',
|
||||
'SET `deleted_at` = NULL',
|
||||
'WHERE `id` IN ('.rtrim(str_repeat('?,', count($ids)), ',').')'
|
||||
)),
|
||||
$ids
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
static function bulkAction($orm, $callback = false) {
|
||||
$total = $orm->count();
|
||||
|
||||
if($total === 0) return false;
|
||||
|
||||
$rows = $orm->select(static::$_table.'.id')
|
||||
->offset(null)
|
||||
->limit(null)
|
||||
->findArray();
|
||||
|
||||
$ids = array_map(function($model) {
|
||||
return (int)$model['id'];
|
||||
}, $rows);
|
||||
|
||||
if($callback !== false) {
|
||||
$callback($ids);
|
||||
}
|
||||
|
||||
// get number of affected rows
|
||||
return $orm->get_last_statement()->rowCount();
|
||||
}
|
||||
|
||||
function duplicate($data = array()) {
|
||||
$model = get_called_class();
|
||||
$model_data = array_merge($this->asArray(), $data);
|
||||
unset($model_data['id']);
|
||||
|
||||
$duplicate = $model::create();
|
||||
$duplicate->hydrate($model_data);
|
||||
$duplicate->set_expr('created_at', 'NOW()');
|
||||
$duplicate->set_expr('updated_at', 'NOW()');
|
||||
$duplicate->set_expr('deleted_at', 'NULL');
|
||||
|
||||
if($duplicate->save()) {
|
||||
return $duplicate;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private function setTimestamp() {
|
||||
if($this->created_at === null) {
|
||||
$this->set_expr('created_at', 'NOW()');
|
||||
}
|
||||
}
|
||||
|
||||
static function getPublished() {
|
||||
return static::whereNull('deleted_at');
|
||||
}
|
||||
|
||||
static function getTrashed() {
|
||||
return static::whereNotNull('deleted_at');
|
||||
}
|
||||
}
|
||||
<?php
|
||||
namespace MailPoet\Models;
|
||||
|
||||
if(!defined('ABSPATH')) exit;
|
||||
|
||||
class Model extends \Sudzy\ValidModel {
|
||||
protected $_errors;
|
||||
|
||||
function __construct() {
|
||||
$this->_errors = array();
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
static function create() {
|
||||
return parent::create();
|
||||
}
|
||||
|
||||
function getErrors() {
|
||||
if(empty($this->_errors)) {
|
||||
return false;
|
||||
} else {
|
||||
return $this->_errors;
|
||||
}
|
||||
}
|
||||
|
||||
function setError($error = '') {
|
||||
if(!empty($error)) {
|
||||
if(is_array($error)) {
|
||||
$this->_errors = array_merge($this->_errors, $error);
|
||||
$this->_errors = array_unique($this->_errors);
|
||||
} else {
|
||||
$this->_errors[] = $error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function save() {
|
||||
$this->setTimestamp();
|
||||
try {
|
||||
parent::save();
|
||||
} catch(\Sudzy\ValidationException $e) {
|
||||
$this->setError($e->getValidationErrors());
|
||||
} catch(\PDOException $e) {
|
||||
switch($e->getCode()) {
|
||||
case 23000:
|
||||
preg_match("/for key \'(.*?)\'/i", $e->getMessage(), $matches);
|
||||
if(isset($matches[1])) {
|
||||
$column = $matches[1];
|
||||
$this->setError(
|
||||
sprintf(
|
||||
__('Another record already exists. Please specify a different "%1$s".'),
|
||||
$column
|
||||
)
|
||||
);
|
||||
} else {
|
||||
$this->setError($e->getMessage());
|
||||
}
|
||||
break;
|
||||
default:
|
||||
$this->setError($e->getMessage());
|
||||
}
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
function trash() {
|
||||
return $this->set_expr('deleted_at', 'NOW()')->save();
|
||||
}
|
||||
|
||||
static function bulkTrash($orm) {
|
||||
$model = get_called_class();
|
||||
return self::bulkAction($orm, function($ids) use($model) {
|
||||
self::rawExecute(join(' ', array(
|
||||
'UPDATE `'.$model::$_table.'`',
|
||||
'SET `deleted_at` = NOW()',
|
||||
'WHERE `id` IN ('.rtrim(str_repeat('?,', count($ids)), ',').')'
|
||||
)),
|
||||
$ids
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
static function bulkDelete($orm) {
|
||||
$model = get_called_class();
|
||||
return self::bulkAction($orm, function($ids) use($model) {
|
||||
$model::whereIn('id', $ids)->deleteMany();
|
||||
});
|
||||
}
|
||||
|
||||
function restore() {
|
||||
return $this->set_expr('deleted_at', 'NULL')->save();
|
||||
}
|
||||
|
||||
static function bulkRestore($orm) {
|
||||
$model = get_called_class();
|
||||
return self::bulkAction($orm, function($ids) use($model) {
|
||||
self::rawExecute(join(' ', array(
|
||||
'UPDATE `'.$model::$_table.'`',
|
||||
'SET `deleted_at` = NULL',
|
||||
'WHERE `id` IN ('.rtrim(str_repeat('?,', count($ids)), ',').')'
|
||||
)),
|
||||
$ids
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
static function bulkAction($orm, $callback = false) {
|
||||
$total = $orm->count();
|
||||
|
||||
if($total === 0) return false;
|
||||
|
||||
$rows = $orm->select(static::$_table.'.id')
|
||||
->offset(null)
|
||||
->limit(null)
|
||||
->findArray();
|
||||
|
||||
$ids = array_map(function($model) {
|
||||
return (int)$model['id'];
|
||||
}, $rows);
|
||||
|
||||
if($callback !== false) {
|
||||
$callback($ids);
|
||||
}
|
||||
|
||||
// get number of affected rows
|
||||
return $orm->get_last_statement()->rowCount();
|
||||
}
|
||||
|
||||
function duplicate($data = array()) {
|
||||
$model = get_called_class();
|
||||
$model_data = array_merge($this->asArray(), $data);
|
||||
unset($model_data['id']);
|
||||
|
||||
$duplicate = $model::create();
|
||||
$duplicate->hydrate($model_data);
|
||||
$duplicate->set_expr('created_at', 'NOW()');
|
||||
$duplicate->set_expr('updated_at', 'NOW()');
|
||||
$duplicate->set_expr('deleted_at', 'NULL');
|
||||
|
||||
if($duplicate->save()) {
|
||||
return $duplicate;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private function setTimestamp() {
|
||||
if($this->created_at === null) {
|
||||
$this->set_expr('created_at', 'NOW()');
|
||||
}
|
||||
}
|
||||
|
||||
static function getPublished() {
|
||||
return static::whereNull('deleted_at');
|
||||
}
|
||||
|
||||
static function getTrashed() {
|
||||
return static::whereNotNull('deleted_at');
|
||||
}
|
||||
}
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -1,46 +1,46 @@
|
||||
<?php
|
||||
namespace MailPoet\Models;
|
||||
|
||||
if(!defined('ABSPATH')) exit;
|
||||
|
||||
class NewsletterTemplate extends Model {
|
||||
public static $_table = MP_NEWSLETTER_TEMPLATES_TABLE;
|
||||
|
||||
function __construct() {
|
||||
parent::__construct();
|
||||
|
||||
$this->addValidations('name', array(
|
||||
'required' => __('Please specify a name.')
|
||||
));
|
||||
$this->addValidations('body', array(
|
||||
'required' => __('The template body cannot be empty.')
|
||||
));
|
||||
}
|
||||
|
||||
function asArray() {
|
||||
$template = parent::asArray();
|
||||
if(isset($template['body'])) {
|
||||
$template['body'] = json_decode($template['body'], true);
|
||||
}
|
||||
return $template;
|
||||
}
|
||||
|
||||
static function createOrUpdate($data = array()) {
|
||||
$template = false;
|
||||
|
||||
if(isset($data['id']) && (int)$data['id'] > 0) {
|
||||
$template = self::findOne((int)$data['id']);
|
||||
}
|
||||
|
||||
if($template === false) {
|
||||
$template = self::create();
|
||||
$template->hydrate($data);
|
||||
} else {
|
||||
unset($data['id']);
|
||||
$template->set($data);
|
||||
}
|
||||
|
||||
$template->save();
|
||||
return $template;
|
||||
}
|
||||
}
|
||||
<?php
|
||||
namespace MailPoet\Models;
|
||||
|
||||
if(!defined('ABSPATH')) exit;
|
||||
|
||||
class NewsletterTemplate extends Model {
|
||||
public static $_table = MP_NEWSLETTER_TEMPLATES_TABLE;
|
||||
|
||||
function __construct() {
|
||||
parent::__construct();
|
||||
|
||||
$this->addValidations('name', array(
|
||||
'required' => __('Please specify a name.')
|
||||
));
|
||||
$this->addValidations('body', array(
|
||||
'required' => __('The template body cannot be empty.')
|
||||
));
|
||||
}
|
||||
|
||||
function asArray() {
|
||||
$template = parent::asArray();
|
||||
if(isset($template['body'])) {
|
||||
$template['body'] = json_decode($template['body'], true);
|
||||
}
|
||||
return $template;
|
||||
}
|
||||
|
||||
static function createOrUpdate($data = array()) {
|
||||
$template = false;
|
||||
|
||||
if(isset($data['id']) && (int)$data['id'] > 0) {
|
||||
$template = self::findOne((int)$data['id']);
|
||||
}
|
||||
|
||||
if($template === false) {
|
||||
$template = self::create();
|
||||
$template->hydrate($data);
|
||||
} else {
|
||||
unset($data['id']);
|
||||
$template->set($data);
|
||||
}
|
||||
|
||||
$template->save();
|
||||
return $template;
|
||||
}
|
||||
}
|
||||
|
@ -1,160 +1,160 @@
|
||||
<?php
|
||||
namespace MailPoet\Models;
|
||||
|
||||
if(!defined('ABSPATH')) exit;
|
||||
|
||||
class Setting extends Model {
|
||||
public static $_table = MP_SETTINGS_TABLE;
|
||||
|
||||
public static $defaults = null;
|
||||
|
||||
const DEFAULT_SENDING_METHOD_GROUP = 'website';
|
||||
const DEFAULT_SENDING_METHOD = 'PHPMail';
|
||||
const DEFAULT_SENDING_FREQUENCY_EMAILS = 25;
|
||||
const DEFAULT_SENDING_FREQUENCY_INTERVAL = 5; // in minutes
|
||||
|
||||
function __construct() {
|
||||
parent::__construct();
|
||||
|
||||
$this->addValidations('name', array(
|
||||
'required' => __('Please specify a name.')
|
||||
));
|
||||
}
|
||||
|
||||
public static function getDefaults() {
|
||||
if(self::$defaults === null) {
|
||||
self::loadDefaults();
|
||||
}
|
||||
return self::$defaults;
|
||||
}
|
||||
|
||||
public static function loadDefaults() {
|
||||
self::$defaults = array(
|
||||
'mta_group' => self::DEFAULT_SENDING_METHOD_GROUP,
|
||||
'mta' => array(
|
||||
'method' => self::DEFAULT_SENDING_METHOD,
|
||||
'frequency' => array(
|
||||
'emails' => self::DEFAULT_SENDING_FREQUENCY_EMAILS,
|
||||
'interval' => self::DEFAULT_SENDING_FREQUENCY_INTERVAL
|
||||
)
|
||||
),
|
||||
'signup_confirmation' => array(
|
||||
'enabled' => true,
|
||||
'subject' => sprintf(__('Confirm your subscription to %1$s'), get_option('blogname')),
|
||||
'body' => __("Hello!\n\nHurray! You've subscribed to our site.\n\nPlease confirm your subscription to the list(s): [lists_to_confirm] by clicking the link below: \n\n[activation_link]Click here to confirm your subscription.[/activation_link]\n\nThank you,\n\nThe Team")
|
||||
),
|
||||
'tracking' => array(
|
||||
'enabled' => true
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public static function getValue($key, $default = null) {
|
||||
$keys = explode('.', $key);
|
||||
$defaults = self::getDefaults();
|
||||
|
||||
if(count($keys) === 1) {
|
||||
$setting = Setting::where('name', $key)->findOne();
|
||||
if($setting === false) {
|
||||
if($default === null && array_key_exists($key, $defaults)) {
|
||||
return $defaults[$key];
|
||||
} else {
|
||||
return $default;
|
||||
}
|
||||
} else {
|
||||
if(is_serialized($setting->value)) {
|
||||
$value = unserialize($setting->value);
|
||||
} else {
|
||||
$value = $setting->value;
|
||||
}
|
||||
if(is_array($value) && array_key_exists($key, $defaults)) {
|
||||
return array_replace_recursive($defaults[$key], $value);
|
||||
} else {
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$main_key = array_shift($keys);
|
||||
|
||||
$setting = static::getValue($main_key, $default);
|
||||
|
||||
if($setting !== $default) {
|
||||
for($i = 0, $count = count($keys); $i < $count; $i++) {
|
||||
if(!is_array($setting)) {
|
||||
$setting = array();
|
||||
}
|
||||
if(array_key_exists($keys[$i], $setting)) {
|
||||
$setting = $setting[$keys[$i]];
|
||||
} else {
|
||||
return $default;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $setting;
|
||||
}
|
||||
}
|
||||
|
||||
public static function setValue($key, $value) {
|
||||
$keys = explode('.', $key);
|
||||
|
||||
if(count($keys) === 1) {
|
||||
if(is_array($value)) {
|
||||
$value = serialize($value);
|
||||
}
|
||||
|
||||
$setting = Setting::createOrUpdate(array(
|
||||
'name' => $key,
|
||||
'value' => $value
|
||||
));
|
||||
return ($setting->id() > 0 && $setting->getErrors() === false);
|
||||
} else {
|
||||
$main_key = array_shift($keys);
|
||||
|
||||
$setting_value = static::getValue($main_key, array());
|
||||
$current_value = &$setting_value;
|
||||
$last_key = array_pop($keys);
|
||||
|
||||
foreach($keys as $key) {
|
||||
$current_value =& $current_value[$key];
|
||||
}
|
||||
if(is_scalar($current_value)) {
|
||||
$current_value = array();
|
||||
}
|
||||
$current_value[$last_key] = $value;
|
||||
|
||||
return static::setValue($main_key, $setting_value);
|
||||
}
|
||||
}
|
||||
|
||||
public static function getAll() {
|
||||
$settingsCollection = self::findMany();
|
||||
$settings = array();
|
||||
if(!empty($settingsCollection)) {
|
||||
foreach($settingsCollection as $setting) {
|
||||
$value = (is_serialized($setting->value)
|
||||
? unserialize($setting->value)
|
||||
: $setting->value
|
||||
);
|
||||
$settings[$setting->name] = $value;
|
||||
}
|
||||
}
|
||||
return array_replace_recursive(self::getDefaults(), $settings);
|
||||
}
|
||||
|
||||
public static function createOrUpdate($data = array()) {
|
||||
$setting = false;
|
||||
|
||||
if(isset($data['name'])) {
|
||||
$setting = self::where('name', $data['name'])->findOne();
|
||||
}
|
||||
|
||||
if($setting === false) {
|
||||
$setting = self::create();
|
||||
$setting->hydrate($data);
|
||||
} else {
|
||||
$setting->value = $data['value'];
|
||||
}
|
||||
|
||||
return $setting->save();
|
||||
}
|
||||
}
|
||||
<?php
|
||||
namespace MailPoet\Models;
|
||||
|
||||
if(!defined('ABSPATH')) exit;
|
||||
|
||||
class Setting extends Model {
|
||||
public static $_table = MP_SETTINGS_TABLE;
|
||||
|
||||
public static $defaults = null;
|
||||
|
||||
const DEFAULT_SENDING_METHOD_GROUP = 'website';
|
||||
const DEFAULT_SENDING_METHOD = 'PHPMail';
|
||||
const DEFAULT_SENDING_FREQUENCY_EMAILS = 25;
|
||||
const DEFAULT_SENDING_FREQUENCY_INTERVAL = 5; // in minutes
|
||||
|
||||
function __construct() {
|
||||
parent::__construct();
|
||||
|
||||
$this->addValidations('name', array(
|
||||
'required' => __('Please specify a name.')
|
||||
));
|
||||
}
|
||||
|
||||
public static function getDefaults() {
|
||||
if(self::$defaults === null) {
|
||||
self::loadDefaults();
|
||||
}
|
||||
return self::$defaults;
|
||||
}
|
||||
|
||||
public static function loadDefaults() {
|
||||
self::$defaults = array(
|
||||
'mta_group' => self::DEFAULT_SENDING_METHOD_GROUP,
|
||||
'mta' => array(
|
||||
'method' => self::DEFAULT_SENDING_METHOD,
|
||||
'frequency' => array(
|
||||
'emails' => self::DEFAULT_SENDING_FREQUENCY_EMAILS,
|
||||
'interval' => self::DEFAULT_SENDING_FREQUENCY_INTERVAL
|
||||
)
|
||||
),
|
||||
'signup_confirmation' => array(
|
||||
'enabled' => true,
|
||||
'subject' => sprintf(__('Confirm your subscription to %1$s'), get_option('blogname')),
|
||||
'body' => __("Hello!\n\nHurray! You've subscribed to our site.\n\nPlease confirm your subscription to the list(s): [lists_to_confirm] by clicking the link below: \n\n[activation_link]Click here to confirm your subscription.[/activation_link]\n\nThank you,\n\nThe Team")
|
||||
),
|
||||
'tracking' => array(
|
||||
'enabled' => true
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public static function getValue($key, $default = null) {
|
||||
$keys = explode('.', $key);
|
||||
$defaults = self::getDefaults();
|
||||
|
||||
if(count($keys) === 1) {
|
||||
$setting = Setting::where('name', $key)->findOne();
|
||||
if($setting === false) {
|
||||
if($default === null && array_key_exists($key, $defaults)) {
|
||||
return $defaults[$key];
|
||||
} else {
|
||||
return $default;
|
||||
}
|
||||
} else {
|
||||
if(is_serialized($setting->value)) {
|
||||
$value = unserialize($setting->value);
|
||||
} else {
|
||||
$value = $setting->value;
|
||||
}
|
||||
if(is_array($value) && array_key_exists($key, $defaults)) {
|
||||
return array_replace_recursive($defaults[$key], $value);
|
||||
} else {
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$main_key = array_shift($keys);
|
||||
|
||||
$setting = static::getValue($main_key, $default);
|
||||
|
||||
if($setting !== $default) {
|
||||
for($i = 0, $count = count($keys); $i < $count; $i++) {
|
||||
if(!is_array($setting)) {
|
||||
$setting = array();
|
||||
}
|
||||
if(array_key_exists($keys[$i], $setting)) {
|
||||
$setting = $setting[$keys[$i]];
|
||||
} else {
|
||||
return $default;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $setting;
|
||||
}
|
||||
}
|
||||
|
||||
public static function setValue($key, $value) {
|
||||
$keys = explode('.', $key);
|
||||
|
||||
if(count($keys) === 1) {
|
||||
if(is_array($value)) {
|
||||
$value = serialize($value);
|
||||
}
|
||||
|
||||
$setting = Setting::createOrUpdate(array(
|
||||
'name' => $key,
|
||||
'value' => $value
|
||||
));
|
||||
return ($setting->id() > 0 && $setting->getErrors() === false);
|
||||
} else {
|
||||
$main_key = array_shift($keys);
|
||||
|
||||
$setting_value = static::getValue($main_key, array());
|
||||
$current_value = &$setting_value;
|
||||
$last_key = array_pop($keys);
|
||||
|
||||
foreach($keys as $key) {
|
||||
$current_value =& $current_value[$key];
|
||||
}
|
||||
if(is_scalar($current_value)) {
|
||||
$current_value = array();
|
||||
}
|
||||
$current_value[$last_key] = $value;
|
||||
|
||||
return static::setValue($main_key, $setting_value);
|
||||
}
|
||||
}
|
||||
|
||||
public static function getAll() {
|
||||
$settingsCollection = self::findMany();
|
||||
$settings = array();
|
||||
if(!empty($settingsCollection)) {
|
||||
foreach($settingsCollection as $setting) {
|
||||
$value = (is_serialized($setting->value)
|
||||
? unserialize($setting->value)
|
||||
: $setting->value
|
||||
);
|
||||
$settings[$setting->name] = $value;
|
||||
}
|
||||
}
|
||||
return array_replace_recursive(self::getDefaults(), $settings);
|
||||
}
|
||||
|
||||
public static function createOrUpdate($data = array()) {
|
||||
$setting = false;
|
||||
|
||||
if(isset($data['name'])) {
|
||||
$setting = self::where('name', $data['name'])->findOne();
|
||||
}
|
||||
|
||||
if($setting === false) {
|
||||
$setting = self::create();
|
||||
$setting->hydrate($data);
|
||||
} else {
|
||||
$setting->value = $data['value'];
|
||||
}
|
||||
|
||||
return $setting->save();
|
||||
}
|
||||
}
|
||||
|
@ -1,132 +1,132 @@
|
||||
<?php
|
||||
namespace MailPoet\Newsletter\Links;
|
||||
|
||||
use MailPoet\Models\NewsletterLink;
|
||||
use MailPoet\Newsletter\Shortcodes\Shortcodes;
|
||||
use MailPoet\Util\Helpers;
|
||||
use MailPoet\Util\Security;
|
||||
|
||||
class Links {
|
||||
const DATA_TAG = '[mailpoet_data]';
|
||||
const HASH_LENGTH = 5;
|
||||
|
||||
static function extract($content) {
|
||||
$extracted_links = array();
|
||||
// adopted from WP's wp_extract_urls() function & modified to work on hrefs
|
||||
# match href=' or href="
|
||||
$regex = '#(?:href.*?=.*?)(["\']?)('
|
||||
# match http://
|
||||
. '(?:([\w-]+:)?//?)'
|
||||
# match everything except for special characters # until .
|
||||
. '[^\s()<>]+'
|
||||
. '[.]'
|
||||
# conditionally match everything except for special characters after .
|
||||
. '(?:'
|
||||
. '\([\w\d]+\)|'
|
||||
. '(?:'
|
||||
. '[^`!()\[\]{}:;\'".,<>«»“”‘’\s]|'
|
||||
. '(?:[:]\d+)?/?'
|
||||
. ')+'
|
||||
. ')'
|
||||
. ')\\1#';
|
||||
// extract shortcodes with [link:*] format
|
||||
$shortcodes = new Shortcodes();
|
||||
$shortcodes = $shortcodes->extract($content, $categories = array('link'));
|
||||
$extracted_links = array_map(function($shortcode) {
|
||||
return array(
|
||||
'html' => $shortcode,
|
||||
'link' => $shortcode
|
||||
);
|
||||
}, $shortcodes);
|
||||
// extract urls with href="url" format
|
||||
preg_match_all($regex, $content, $matched_urls);
|
||||
$matched_urls_count = count($matched_urls[0]);
|
||||
if($matched_urls_count) {
|
||||
for($index = 0; $index < $matched_urls_count; $index++) {
|
||||
$extracted_links[] = array(
|
||||
'html' => $matched_urls[0][$index],
|
||||
'link' => $matched_urls[2][$index]
|
||||
);
|
||||
}
|
||||
}
|
||||
return array_unique($extracted_links, SORT_REGULAR);
|
||||
}
|
||||
|
||||
static function process($content) {
|
||||
$extracted_links = self::extract($content);
|
||||
$processed_links = array();
|
||||
foreach($extracted_links as $extracted_link) {
|
||||
$hash = Security::generateRandomString(self::HASH_LENGTH);
|
||||
$processed_links[] = array(
|
||||
'hash' => $hash,
|
||||
'url' => $extracted_link['link']
|
||||
);
|
||||
$params = array(
|
||||
'mailpoet' => '',
|
||||
'endpoint' => 'track',
|
||||
'action' => 'click',
|
||||
'data' => self::DATA_TAG . '-' . $hash
|
||||
);
|
||||
$tracked_link = add_query_arg($params, home_url());
|
||||
// first, replace URL in the extracted HTML source with encoded link
|
||||
$tracked_link_html_source = str_replace(
|
||||
$extracted_link['link'], $tracked_link,
|
||||
$extracted_link['html']
|
||||
);
|
||||
// second, replace original extracted HTML source with tracked URL source
|
||||
$content = str_replace(
|
||||
$extracted_link['html'], $tracked_link_html_source, $content
|
||||
);
|
||||
// third, replace text version URL with tracked link: [description](url)
|
||||
// regex is used to avoid replacing description URLs that are wrapped in round brackets
|
||||
// i.e., <a href="http://google.com">(http://google.com)</a> => [(http://google.com)](http://tracked_link)
|
||||
$regex_escaped_extracted_link = preg_quote($extracted_link['link'], '/');
|
||||
$content = preg_replace(
|
||||
'/\[(' . $regex_escaped_extracted_link . ')\](\(' . $regex_escaped_extracted_link . '\))/',
|
||||
'[$1](' . $tracked_link . ')',
|
||||
$content
|
||||
);
|
||||
}
|
||||
return array(
|
||||
$content,
|
||||
$processed_links
|
||||
);
|
||||
}
|
||||
|
||||
static function replaceSubscriberData(
|
||||
$newsletter_id,
|
||||
$subscriber_id,
|
||||
$queue_id,
|
||||
$content
|
||||
) {
|
||||
$regex = sprintf('/data=(%s(?:-\w+)?)/', preg_quote(self::DATA_TAG));
|
||||
preg_match_all($regex, $content, $links);
|
||||
foreach($links[1] as $link) {
|
||||
$hash = null;
|
||||
if(preg_match('/-/', $link)) {
|
||||
list(, $hash) = explode('-', $link);
|
||||
}
|
||||
$data = array(
|
||||
'newsletter' => $newsletter_id,
|
||||
'subscriber' => $subscriber_id,
|
||||
'queue' => $queue_id,
|
||||
'hash' => $hash
|
||||
);
|
||||
$data = rtrim(base64_encode(serialize($data)), '=');
|
||||
$content = str_replace($link, $data, $content);
|
||||
}
|
||||
return $content;
|
||||
}
|
||||
|
||||
static function save(array $links, $newsletter_id, $queue_id) {
|
||||
foreach($links as $link) {
|
||||
if(empty($link['hash'] || empty($link['url']))) continue;
|
||||
$newsletter_link = NewsletterLink::create();
|
||||
$newsletter_link->newsletter_id = $newsletter_id;
|
||||
$newsletter_link->queue_id = $queue_id;
|
||||
$newsletter_link->hash = $link['hash'];
|
||||
$newsletter_link->url = $link['url'];
|
||||
$newsletter_link->save();
|
||||
}
|
||||
}
|
||||
}
|
||||
<?php
|
||||
namespace MailPoet\Newsletter\Links;
|
||||
|
||||
use MailPoet\Models\NewsletterLink;
|
||||
use MailPoet\Newsletter\Shortcodes\Shortcodes;
|
||||
use MailPoet\Util\Helpers;
|
||||
use MailPoet\Util\Security;
|
||||
|
||||
class Links {
|
||||
const DATA_TAG = '[mailpoet_data]';
|
||||
const HASH_LENGTH = 5;
|
||||
|
||||
static function extract($content) {
|
||||
$extracted_links = array();
|
||||
// adopted from WP's wp_extract_urls() function & modified to work on hrefs
|
||||
# match href=' or href="
|
||||
$regex = '#(?:href.*?=.*?)(["\']?)('
|
||||
# match http://
|
||||
. '(?:([\w-]+:)?//?)'
|
||||
# match everything except for special characters # until .
|
||||
. '[^\s()<>]+'
|
||||
. '[.]'
|
||||
# conditionally match everything except for special characters after .
|
||||
. '(?:'
|
||||
. '\([\w\d]+\)|'
|
||||
. '(?:'
|
||||
. '[^`!()\[\]{}:;\'".,<>«»“”‘’\s]|'
|
||||
. '(?:[:]\d+)?/?'
|
||||
. ')+'
|
||||
. ')'
|
||||
. ')\\1#';
|
||||
// extract shortcodes with [link:*] format
|
||||
$shortcodes = new Shortcodes();
|
||||
$shortcodes = $shortcodes->extract($content, $categories = array('link'));
|
||||
$extracted_links = array_map(function($shortcode) {
|
||||
return array(
|
||||
'html' => $shortcode,
|
||||
'link' => $shortcode
|
||||
);
|
||||
}, $shortcodes);
|
||||
// extract urls with href="url" format
|
||||
preg_match_all($regex, $content, $matched_urls);
|
||||
$matched_urls_count = count($matched_urls[0]);
|
||||
if($matched_urls_count) {
|
||||
for($index = 0; $index < $matched_urls_count; $index++) {
|
||||
$extracted_links[] = array(
|
||||
'html' => $matched_urls[0][$index],
|
||||
'link' => $matched_urls[2][$index]
|
||||
);
|
||||
}
|
||||
}
|
||||
return array_unique($extracted_links, SORT_REGULAR);
|
||||
}
|
||||
|
||||
static function process($content) {
|
||||
$extracted_links = self::extract($content);
|
||||
$processed_links = array();
|
||||
foreach($extracted_links as $extracted_link) {
|
||||
$hash = Security::generateRandomString(self::HASH_LENGTH);
|
||||
$processed_links[] = array(
|
||||
'hash' => $hash,
|
||||
'url' => $extracted_link['link']
|
||||
);
|
||||
$params = array(
|
||||
'mailpoet' => '',
|
||||
'endpoint' => 'track',
|
||||
'action' => 'click',
|
||||
'data' => self::DATA_TAG . '-' . $hash
|
||||
);
|
||||
$tracked_link = add_query_arg($params, home_url());
|
||||
// first, replace URL in the extracted HTML source with encoded link
|
||||
$tracked_link_html_source = str_replace(
|
||||
$extracted_link['link'], $tracked_link,
|
||||
$extracted_link['html']
|
||||
);
|
||||
// second, replace original extracted HTML source with tracked URL source
|
||||
$content = str_replace(
|
||||
$extracted_link['html'], $tracked_link_html_source, $content
|
||||
);
|
||||
// third, replace text version URL with tracked link: [description](url)
|
||||
// regex is used to avoid replacing description URLs that are wrapped in round brackets
|
||||
// i.e., <a href="http://google.com">(http://google.com)</a> => [(http://google.com)](http://tracked_link)
|
||||
$regex_escaped_extracted_link = preg_quote($extracted_link['link'], '/');
|
||||
$content = preg_replace(
|
||||
'/\[(' . $regex_escaped_extracted_link . ')\](\(' . $regex_escaped_extracted_link . '\))/',
|
||||
'[$1](' . $tracked_link . ')',
|
||||
$content
|
||||
);
|
||||
}
|
||||
return array(
|
||||
$content,
|
||||
$processed_links
|
||||
);
|
||||
}
|
||||
|
||||
static function replaceSubscriberData(
|
||||
$newsletter_id,
|
||||
$subscriber_id,
|
||||
$queue_id,
|
||||
$content
|
||||
) {
|
||||
$regex = sprintf('/data=(%s(?:-\w+)?)/', preg_quote(self::DATA_TAG));
|
||||
preg_match_all($regex, $content, $links);
|
||||
foreach($links[1] as $link) {
|
||||
$hash = null;
|
||||
if(preg_match('/-/', $link)) {
|
||||
list(, $hash) = explode('-', $link);
|
||||
}
|
||||
$data = array(
|
||||
'newsletter' => $newsletter_id,
|
||||
'subscriber' => $subscriber_id,
|
||||
'queue' => $queue_id,
|
||||
'hash' => $hash
|
||||
);
|
||||
$data = rtrim(base64_encode(serialize($data)), '=');
|
||||
$content = str_replace($link, $data, $content);
|
||||
}
|
||||
return $content;
|
||||
}
|
||||
|
||||
static function save(array $links, $newsletter_id, $queue_id) {
|
||||
foreach($links as $link) {
|
||||
if(empty($link['hash'] || empty($link['url']))) continue;
|
||||
$newsletter_link = NewsletterLink::create();
|
||||
$newsletter_link->newsletter_id = $newsletter_id;
|
||||
$newsletter_link->queue_id = $queue_id;
|
||||
$newsletter_link->hash = $link['hash'];
|
||||
$newsletter_link->url = $link['url'];
|
||||
$newsletter_link->save();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,27 +1,27 @@
|
||||
<?php
|
||||
namespace MailPoet\Newsletter\Renderer\PostProcess;
|
||||
|
||||
use MailPoet\Newsletter\Links\Links;
|
||||
use MailPoet\Newsletter\Renderer\Renderer;
|
||||
|
||||
class OpenTracking {
|
||||
static function process($template) {
|
||||
$DOM = new \pQuery();
|
||||
$DOM = $DOM->parseStr($template);
|
||||
$template = $DOM->query('body');
|
||||
$open_tracking_image = sprintf(
|
||||
'<img alt="" class="" src="%s/%s"/>',
|
||||
home_url(),
|
||||
esc_attr('?mailpoet&endpoint=track&action=open&data=' . Links::DATA_TAG)
|
||||
);
|
||||
$template->html($template->html() . $open_tracking_image);
|
||||
return $DOM->__toString();
|
||||
}
|
||||
|
||||
static function addTrackingImage() {
|
||||
add_filter(Renderer::POST_PROCESS_FILTER, function ($template) {
|
||||
return OpenTracking::process($template);
|
||||
});
|
||||
return true;
|
||||
}
|
||||
}
|
||||
<?php
|
||||
namespace MailPoet\Newsletter\Renderer\PostProcess;
|
||||
|
||||
use MailPoet\Newsletter\Links\Links;
|
||||
use MailPoet\Newsletter\Renderer\Renderer;
|
||||
|
||||
class OpenTracking {
|
||||
static function process($template) {
|
||||
$DOM = new \pQuery();
|
||||
$DOM = $DOM->parseStr($template);
|
||||
$template = $DOM->query('body');
|
||||
$open_tracking_image = sprintf(
|
||||
'<img alt="" class="" src="%s/%s"/>',
|
||||
home_url(),
|
||||
esc_attr('?mailpoet&endpoint=track&action=open&data=' . Links::DATA_TAG)
|
||||
);
|
||||
$template->html($template->html() . $open_tracking_image);
|
||||
return $DOM->__toString();
|
||||
}
|
||||
|
||||
static function addTrackingImage() {
|
||||
add_filter(Renderer::POST_PROCESS_FILTER, function ($template) {
|
||||
return OpenTracking::process($template);
|
||||
});
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -1,17 +1,17 @@
|
||||
<?php
|
||||
namespace MailPoet\Newsletter\Shortcodes\Categories;
|
||||
|
||||
class Date {
|
||||
static function process($action) {
|
||||
$date = new \DateTime('now');
|
||||
$actions = array(
|
||||
'd' => $date->format('d'),
|
||||
'dordinal' => $date->format('dS'),
|
||||
'dtext' => $date->format('D'),
|
||||
'm' => $date->format('m'),
|
||||
'mtext' => $date->format('F'),
|
||||
'y' => $date->format('Y')
|
||||
);
|
||||
return (isset($actions[$action])) ? $actions[$action] : false;
|
||||
}
|
||||
}
|
||||
<?php
|
||||
namespace MailPoet\Newsletter\Shortcodes\Categories;
|
||||
|
||||
class Date {
|
||||
static function process($action) {
|
||||
$date = new \DateTime('now');
|
||||
$actions = array(
|
||||
'd' => $date->format('d'),
|
||||
'dordinal' => $date->format('dS'),
|
||||
'dtext' => $date->format('D'),
|
||||
'm' => $date->format('m'),
|
||||
'mtext' => $date->format('F'),
|
||||
'y' => $date->format('Y')
|
||||
);
|
||||
return (isset($actions[$action])) ? $actions[$action] : false;
|
||||
}
|
||||
}
|
||||
|
@ -1,57 +1,57 @@
|
||||
<?php
|
||||
namespace MailPoet\Newsletter\Shortcodes\Categories;
|
||||
|
||||
use MailPoet\Models\SendingQueue;
|
||||
use MailPoet\Newsletter\Shortcodes\ShortcodesHelper;
|
||||
|
||||
require_once( ABSPATH . "wp-includes/pluggable.php" );
|
||||
|
||||
class Newsletter {
|
||||
static function process($action,
|
||||
$default_value = false,
|
||||
$newsletter,
|
||||
$subscriber,
|
||||
$queue = false,
|
||||
$content
|
||||
) {
|
||||
switch($action) {
|
||||
case 'subject':
|
||||
return ($newsletter) ? $newsletter['subject'] : false;
|
||||
|
||||
case 'total':
|
||||
return substr_count($content, 'data-post-id');
|
||||
|
||||
case 'post_title':
|
||||
preg_match_all('/data-post-id="(\d+)"/ism', $content, $posts);
|
||||
$post_ids = array_unique($posts[1]);
|
||||
$latest_post = self::getLatestWPPost($post_ids);
|
||||
return ($latest_post) ? $latest_post['post_title'] : false;
|
||||
|
||||
case 'number':
|
||||
if($newsletter['type'] !== 'notification') return false;
|
||||
$sent_newsletters =
|
||||
SendingQueue::where('newsletter_id', $newsletter['id'])
|
||||
->where('status', 'completed')
|
||||
->count();
|
||||
return ++$sent_newsletters;
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static function getLatestWPPost($post_ids) {
|
||||
$posts = new \WP_Query(
|
||||
array(
|
||||
'post__in' => $post_ids,
|
||||
'posts_per_page' => 1,
|
||||
'ignore_sticky_posts' => true,
|
||||
'orderby' => 'post_date',
|
||||
'order' => 'DESC'
|
||||
)
|
||||
);
|
||||
return (count($posts)) ?
|
||||
$posts->posts[0]->to_array() :
|
||||
false;
|
||||
}
|
||||
}
|
||||
<?php
|
||||
namespace MailPoet\Newsletter\Shortcodes\Categories;
|
||||
|
||||
use MailPoet\Models\SendingQueue;
|
||||
use MailPoet\Newsletter\Shortcodes\ShortcodesHelper;
|
||||
|
||||
require_once( ABSPATH . "wp-includes/pluggable.php" );
|
||||
|
||||
class Newsletter {
|
||||
static function process($action,
|
||||
$default_value = false,
|
||||
$newsletter,
|
||||
$subscriber,
|
||||
$queue = false,
|
||||
$content
|
||||
) {
|
||||
switch($action) {
|
||||
case 'subject':
|
||||
return ($newsletter) ? $newsletter['subject'] : false;
|
||||
|
||||
case 'total':
|
||||
return substr_count($content, 'data-post-id');
|
||||
|
||||
case 'post_title':
|
||||
preg_match_all('/data-post-id="(\d+)"/ism', $content, $posts);
|
||||
$post_ids = array_unique($posts[1]);
|
||||
$latest_post = self::getLatestWPPost($post_ids);
|
||||
return ($latest_post) ? $latest_post['post_title'] : false;
|
||||
|
||||
case 'number':
|
||||
if($newsletter['type'] !== 'notification') return false;
|
||||
$sent_newsletters =
|
||||
SendingQueue::where('newsletter_id', $newsletter['id'])
|
||||
->where('status', 'completed')
|
||||
->count();
|
||||
return ++$sent_newsletters;
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static function getLatestWPPost($post_ids) {
|
||||
$posts = new \WP_Query(
|
||||
array(
|
||||
'post__in' => $post_ids,
|
||||
'posts_per_page' => 1,
|
||||
'ignore_sticky_posts' => true,
|
||||
'orderby' => 'post_date',
|
||||
'order' => 'DESC'
|
||||
)
|
||||
);
|
||||
return (count($posts)) ?
|
||||
$posts->posts[0]->to_array() :
|
||||
false;
|
||||
}
|
||||
}
|
||||
|
@ -1,97 +1,97 @@
|
||||
<?php
|
||||
namespace MailPoet\Newsletter\Shortcodes;
|
||||
|
||||
class Shortcodes {
|
||||
public $newsletter;
|
||||
public $subscriber;
|
||||
public $queue;
|
||||
const SHORTCODE_CATEGORY_NAMESPACE = 'MailPoet\Newsletter\Shortcodes\Categories\\';
|
||||
|
||||
function __construct(
|
||||
$newsletter = false,
|
||||
$subscriber = false,
|
||||
$queue = false
|
||||
) {
|
||||
$this->newsletter = (is_object($newsletter)) ?
|
||||
$newsletter->asArray() :
|
||||
$newsletter;
|
||||
$this->subscriber = (is_object($subscriber)) ?
|
||||
$subscriber->asArray() :
|
||||
$subscriber;
|
||||
$this->queue = (is_object($queue)) ?
|
||||
$queue->asArray() :
|
||||
$queue;
|
||||
}
|
||||
|
||||
function extract($content, $categories = false) {
|
||||
$categories = (is_array($categories)) ? implode('|', $categories) : false;
|
||||
$regex = sprintf(
|
||||
'/\[%s:.*?\]/ism',
|
||||
($categories) ? '(?:' . $categories . ')' : '(?:\w+)'
|
||||
);
|
||||
preg_match_all($regex, $content, $shortcodes);
|
||||
$shortcodes = $shortcodes[0];
|
||||
return (count($shortcodes)) ?
|
||||
array_unique($shortcodes) :
|
||||
false;
|
||||
}
|
||||
|
||||
function match($shortcode) {
|
||||
preg_match(
|
||||
'/\[(?P<category>\w+)?:(?P<action>\w+)(?:.*?\|.*?default:(?P<default>.*?))?\]/',
|
||||
$shortcode,
|
||||
$match
|
||||
);
|
||||
return $match;
|
||||
}
|
||||
|
||||
function process($shortcodes, $content = false) {
|
||||
$processed_shortcodes = array_map(
|
||||
function($shortcode) use ($content) {
|
||||
$shortcode_details = $this->match($shortcode);
|
||||
$shortcode_category = !empty($shortcode_details['category']) ?
|
||||
ucfirst($shortcode_details['category']) :
|
||||
false;
|
||||
$shortcode_action = !empty($shortcode_details['action']) ?
|
||||
$shortcode_details['action'] :
|
||||
false;
|
||||
$shortcode_class =
|
||||
self::SHORTCODE_CATEGORY_NAMESPACE . $shortcode_category;
|
||||
$shortcode_default_value = !empty($shortcode_details['default']) ?
|
||||
$shortcode_details['default'] :
|
||||
false;
|
||||
if(!class_exists($shortcode_class)) {
|
||||
$custom_shortcode = apply_filters(
|
||||
'mailpoet_newsletter_shortcode',
|
||||
$shortcode,
|
||||
$this->newsletter,
|
||||
$this->subscriber,
|
||||
$this->queue,
|
||||
$content
|
||||
);
|
||||
return ($custom_shortcode === $shortcode) ?
|
||||
false :
|
||||
$custom_shortcode;
|
||||
}
|
||||
return $shortcode_class::process(
|
||||
$shortcode_action,
|
||||
$shortcode_default_value,
|
||||
$this->newsletter,
|
||||
$this->subscriber,
|
||||
$this->queue,
|
||||
$content
|
||||
);
|
||||
}, $shortcodes);
|
||||
return $processed_shortcodes;
|
||||
}
|
||||
|
||||
function replace($content, $categories = false) {
|
||||
$shortcodes = $this->extract($content, $categories);
|
||||
if(!$shortcodes) {
|
||||
return $content;
|
||||
}
|
||||
$processed_shortcodes = $this->process($shortcodes, $content);
|
||||
$shortcodes = array_intersect_key($shortcodes, $processed_shortcodes);
|
||||
return str_replace($shortcodes, $processed_shortcodes, $content);
|
||||
}
|
||||
}
|
||||
<?php
|
||||
namespace MailPoet\Newsletter\Shortcodes;
|
||||
|
||||
class Shortcodes {
|
||||
public $newsletter;
|
||||
public $subscriber;
|
||||
public $queue;
|
||||
const SHORTCODE_CATEGORY_NAMESPACE = 'MailPoet\Newsletter\Shortcodes\Categories\\';
|
||||
|
||||
function __construct(
|
||||
$newsletter = false,
|
||||
$subscriber = false,
|
||||
$queue = false
|
||||
) {
|
||||
$this->newsletter = (is_object($newsletter)) ?
|
||||
$newsletter->asArray() :
|
||||
$newsletter;
|
||||
$this->subscriber = (is_object($subscriber)) ?
|
||||
$subscriber->asArray() :
|
||||
$subscriber;
|
||||
$this->queue = (is_object($queue)) ?
|
||||
$queue->asArray() :
|
||||
$queue;
|
||||
}
|
||||
|
||||
function extract($content, $categories = false) {
|
||||
$categories = (is_array($categories)) ? implode('|', $categories) : false;
|
||||
$regex = sprintf(
|
||||
'/\[%s:.*?\]/ism',
|
||||
($categories) ? '(?:' . $categories . ')' : '(?:\w+)'
|
||||
);
|
||||
preg_match_all($regex, $content, $shortcodes);
|
||||
$shortcodes = $shortcodes[0];
|
||||
return (count($shortcodes)) ?
|
||||
array_unique($shortcodes) :
|
||||
false;
|
||||
}
|
||||
|
||||
function match($shortcode) {
|
||||
preg_match(
|
||||
'/\[(?P<category>\w+)?:(?P<action>\w+)(?:.*?\|.*?default:(?P<default>.*?))?\]/',
|
||||
$shortcode,
|
||||
$match
|
||||
);
|
||||
return $match;
|
||||
}
|
||||
|
||||
function process($shortcodes, $content = false) {
|
||||
$processed_shortcodes = array_map(
|
||||
function($shortcode) use ($content) {
|
||||
$shortcode_details = $this->match($shortcode);
|
||||
$shortcode_category = !empty($shortcode_details['category']) ?
|
||||
ucfirst($shortcode_details['category']) :
|
||||
false;
|
||||
$shortcode_action = !empty($shortcode_details['action']) ?
|
||||
$shortcode_details['action'] :
|
||||
false;
|
||||
$shortcode_class =
|
||||
self::SHORTCODE_CATEGORY_NAMESPACE . $shortcode_category;
|
||||
$shortcode_default_value = !empty($shortcode_details['default']) ?
|
||||
$shortcode_details['default'] :
|
||||
false;
|
||||
if(!class_exists($shortcode_class)) {
|
||||
$custom_shortcode = apply_filters(
|
||||
'mailpoet_newsletter_shortcode',
|
||||
$shortcode,
|
||||
$this->newsletter,
|
||||
$this->subscriber,
|
||||
$this->queue,
|
||||
$content
|
||||
);
|
||||
return ($custom_shortcode === $shortcode) ?
|
||||
false :
|
||||
$custom_shortcode;
|
||||
}
|
||||
return $shortcode_class::process(
|
||||
$shortcode_action,
|
||||
$shortcode_default_value,
|
||||
$this->newsletter,
|
||||
$this->subscriber,
|
||||
$this->queue,
|
||||
$content
|
||||
);
|
||||
}, $shortcodes);
|
||||
return $processed_shortcodes;
|
||||
}
|
||||
|
||||
function replace($content, $categories = false) {
|
||||
$shortcodes = $this->extract($content, $categories);
|
||||
if(!$shortcodes) {
|
||||
return $content;
|
||||
}
|
||||
$processed_shortcodes = $this->process($shortcodes, $content);
|
||||
$shortcodes = array_intersect_key($shortcodes, $processed_shortcodes);
|
||||
return str_replace($shortcodes, $processed_shortcodes, $content);
|
||||
}
|
||||
}
|
||||
|
@ -1,103 +1,103 @@
|
||||
<?php
|
||||
namespace MailPoet\Router;
|
||||
use \MailPoet\Util\Security;
|
||||
|
||||
if(!defined('ABSPATH')) exit;
|
||||
|
||||
class Router {
|
||||
function __construct() {
|
||||
}
|
||||
|
||||
function init() {
|
||||
// security token
|
||||
add_action(
|
||||
'admin_head',
|
||||
array($this, 'setToken')
|
||||
);
|
||||
|
||||
// Admin API (Ajax only)
|
||||
add_action(
|
||||
'wp_ajax_mailpoet',
|
||||
array($this, 'setupAdmin')
|
||||
);
|
||||
|
||||
// Public API (Ajax)
|
||||
add_action(
|
||||
'wp_ajax_nopriv_mailpoet',
|
||||
array($this, 'setupPublic')
|
||||
);
|
||||
// Public API (Post)
|
||||
add_action(
|
||||
'admin_post_nopriv_mailpoet',
|
||||
array($this, 'setupPublic')
|
||||
);
|
||||
}
|
||||
|
||||
function setupAdmin() {
|
||||
$this->verifyToken();
|
||||
$this->checkPermissions();
|
||||
return $this->processRoute();
|
||||
}
|
||||
|
||||
function setupPublic() {
|
||||
$this->verifyToken();
|
||||
return $this->processRoute();
|
||||
}
|
||||
|
||||
function processRoute() {
|
||||
$class = ucfirst($_POST['endpoint']);
|
||||
$endpoint = __NAMESPACE__ . "\\" . $class;
|
||||
$method = $_POST['method'];
|
||||
|
||||
$doing_ajax = (bool)(defined('DOING_AJAX') && DOING_AJAX);
|
||||
|
||||
if($doing_ajax) {
|
||||
$data = isset($_POST['data']) ? stripslashes_deep($_POST['data']) : array();
|
||||
} else {
|
||||
$data = $_POST;
|
||||
}
|
||||
|
||||
if(is_array($data) && !empty($data)) {
|
||||
// filter out reserved keywords from data
|
||||
$reserved_keywords = array(
|
||||
'token',
|
||||
'endpoint',
|
||||
'method',
|
||||
'mailpoet_redirect'
|
||||
);
|
||||
$data = array_diff_key($data, array_flip($reserved_keywords));
|
||||
}
|
||||
|
||||
try {
|
||||
$endpoint = new $endpoint();
|
||||
$response = $endpoint->$method($data);
|
||||
wp_send_json($response);
|
||||
} catch(\Exception $e) {
|
||||
error_log($e->getMessage());
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
function setToken() {
|
||||
$global = '<script type="text/javascript">';
|
||||
$global .= 'var mailpoet_token = "'.Security::generateToken().'";';
|
||||
$global .= '</script>';
|
||||
echo $global;
|
||||
}
|
||||
|
||||
function checkPermissions() {
|
||||
if(!current_user_can('manage_options')) {
|
||||
die();
|
||||
}
|
||||
}
|
||||
|
||||
function verifyToken() {
|
||||
if(
|
||||
empty($_POST['token'])
|
||||
||
|
||||
!wp_verify_nonce($_POST['token'], 'mailpoet_token')
|
||||
) {
|
||||
die();
|
||||
}
|
||||
}
|
||||
}
|
||||
<?php
|
||||
namespace MailPoet\Router;
|
||||
use \MailPoet\Util\Security;
|
||||
|
||||
if(!defined('ABSPATH')) exit;
|
||||
|
||||
class Router {
|
||||
function __construct() {
|
||||
}
|
||||
|
||||
function init() {
|
||||
// security token
|
||||
add_action(
|
||||
'admin_head',
|
||||
array($this, 'setToken')
|
||||
);
|
||||
|
||||
// Admin API (Ajax only)
|
||||
add_action(
|
||||
'wp_ajax_mailpoet',
|
||||
array($this, 'setupAdmin')
|
||||
);
|
||||
|
||||
// Public API (Ajax)
|
||||
add_action(
|
||||
'wp_ajax_nopriv_mailpoet',
|
||||
array($this, 'setupPublic')
|
||||
);
|
||||
// Public API (Post)
|
||||
add_action(
|
||||
'admin_post_nopriv_mailpoet',
|
||||
array($this, 'setupPublic')
|
||||
);
|
||||
}
|
||||
|
||||
function setupAdmin() {
|
||||
$this->verifyToken();
|
||||
$this->checkPermissions();
|
||||
return $this->processRoute();
|
||||
}
|
||||
|
||||
function setupPublic() {
|
||||
$this->verifyToken();
|
||||
return $this->processRoute();
|
||||
}
|
||||
|
||||
function processRoute() {
|
||||
$class = ucfirst($_POST['endpoint']);
|
||||
$endpoint = __NAMESPACE__ . "\\" . $class;
|
||||
$method = $_POST['method'];
|
||||
|
||||
$doing_ajax = (bool)(defined('DOING_AJAX') && DOING_AJAX);
|
||||
|
||||
if($doing_ajax) {
|
||||
$data = isset($_POST['data']) ? stripslashes_deep($_POST['data']) : array();
|
||||
} else {
|
||||
$data = $_POST;
|
||||
}
|
||||
|
||||
if(is_array($data) && !empty($data)) {
|
||||
// filter out reserved keywords from data
|
||||
$reserved_keywords = array(
|
||||
'token',
|
||||
'endpoint',
|
||||
'method',
|
||||
'mailpoet_redirect'
|
||||
);
|
||||
$data = array_diff_key($data, array_flip($reserved_keywords));
|
||||
}
|
||||
|
||||
try {
|
||||
$endpoint = new $endpoint();
|
||||
$response = $endpoint->$method($data);
|
||||
wp_send_json($response);
|
||||
} catch(\Exception $e) {
|
||||
error_log($e->getMessage());
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
function setToken() {
|
||||
$global = '<script type="text/javascript">';
|
||||
$global .= 'var mailpoet_token = "'.Security::generateToken().'";';
|
||||
$global .= '</script>';
|
||||
echo $global;
|
||||
}
|
||||
|
||||
function checkPermissions() {
|
||||
if(!current_user_can('manage_options')) {
|
||||
die();
|
||||
}
|
||||
}
|
||||
|
||||
function verifyToken() {
|
||||
if(
|
||||
empty($_POST['token'])
|
||||
||
|
||||
!wp_verify_nonce($_POST['token'], 'mailpoet_token')
|
||||
) {
|
||||
die();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,26 +1,26 @@
|
||||
<?php
|
||||
namespace MailPoet\Router;
|
||||
use \MailPoet\Models\Setting;
|
||||
|
||||
if(!defined('ABSPATH')) exit;
|
||||
|
||||
class Settings {
|
||||
function __construct() {
|
||||
}
|
||||
|
||||
function get() {
|
||||
$settings = Setting::getAll();
|
||||
return $settings;
|
||||
}
|
||||
|
||||
function set($settings = array()) {
|
||||
if(empty($settings)) {
|
||||
return false;
|
||||
} else {
|
||||
foreach($settings as $name => $value) {
|
||||
Setting::setValue($name, $value);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
<?php
|
||||
namespace MailPoet\Router;
|
||||
use \MailPoet\Models\Setting;
|
||||
|
||||
if(!defined('ABSPATH')) exit;
|
||||
|
||||
class Settings {
|
||||
function __construct() {
|
||||
}
|
||||
|
||||
function get() {
|
||||
$settings = Setting::getAll();
|
||||
return $settings;
|
||||
}
|
||||
|
||||
function set($settings = array()) {
|
||||
if(empty($settings)) {
|
||||
return false;
|
||||
} else {
|
||||
foreach($settings as $name => $value) {
|
||||
Setting::setValue($name, $value);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user