Default page on install + Setting::setValue() dot notation + cleanup
This commit is contained in:
@@ -5,6 +5,7 @@ use MailPoet\Models;
|
|||||||
use MailPoet\Cron\Supervisor;
|
use MailPoet\Cron\Supervisor;
|
||||||
use MailPoet\Router;
|
use MailPoet\Router;
|
||||||
use MailPoet\Models\Setting;
|
use MailPoet\Models\Setting;
|
||||||
|
use MailPoet\Settings\Pages;
|
||||||
|
|
||||||
if(!defined('ABSPATH')) exit;
|
if(!defined('ABSPATH')) exit;
|
||||||
|
|
||||||
@@ -31,6 +32,7 @@ class Initializer {
|
|||||||
$this->runQueueSupervisor();
|
$this->runQueueSupervisor();
|
||||||
$this->setupShortcodes();
|
$this->setupShortcodes();
|
||||||
$this->setupHooks();
|
$this->setupHooks();
|
||||||
|
$this->setupPages();
|
||||||
$this->setupImages();
|
$this->setupImages();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -127,6 +129,11 @@ class Initializer {
|
|||||||
$changelog->init();
|
$changelog->init();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function setupPages() {
|
||||||
|
$pages = new Pages();
|
||||||
|
$pages->init();
|
||||||
|
}
|
||||||
|
|
||||||
function setupShortcodes() {
|
function setupShortcodes() {
|
||||||
$shortcodes = new Shortcodes();
|
$shortcodes = new Shortcodes();
|
||||||
$shortcodes->init();
|
$shortcodes->init();
|
||||||
|
@@ -8,6 +8,7 @@ use MailPoet\Config\PopulatorData\Templates\PostNotificationsBlankTemplate;
|
|||||||
use \MailPoet\Models\Segment;
|
use \MailPoet\Models\Segment;
|
||||||
use \MailPoet\Segments\WP;
|
use \MailPoet\Segments\WP;
|
||||||
use \MailPoet\Models\Setting;
|
use \MailPoet\Models\Setting;
|
||||||
|
use \MailPoet\Settings\Pages;
|
||||||
|
|
||||||
if (!defined('ABSPATH')) exit;
|
if (!defined('ABSPATH')) exit;
|
||||||
|
|
||||||
@@ -48,6 +49,29 @@ class Populator {
|
|||||||
|
|
||||||
$this->createDefaultSegments();
|
$this->createDefaultSegments();
|
||||||
$this->createDefaultSettings();
|
$this->createDefaultSettings();
|
||||||
|
$this->createMailPoetPage();
|
||||||
|
}
|
||||||
|
|
||||||
|
private function createMailPoetPage() {
|
||||||
|
$pages = get_posts(array(
|
||||||
|
'posts_per_page' => 1,
|
||||||
|
'orderby' => 'date',
|
||||||
|
'order' => 'DESC',
|
||||||
|
'post_type' => 'mailpoet_page'
|
||||||
|
));
|
||||||
|
|
||||||
|
$page = null;
|
||||||
|
if(!empty($pages)) {
|
||||||
|
$page = array_shift($pages);
|
||||||
|
if(strpos($page->post_content, '[mailpoet_page]') === false) {
|
||||||
|
$page = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if($page === null) {
|
||||||
|
$mailpoet_page_id = Pages::createMailPoetPage();
|
||||||
|
Setting::setValue('subscription.page', $mailpoet_page_id);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function createDefaultSettings() {
|
private function createDefaultSettings() {
|
||||||
@@ -74,9 +98,7 @@ class Populator {
|
|||||||
));
|
));
|
||||||
|
|
||||||
// enable signup confirmation by default
|
// enable signup confirmation by default
|
||||||
Setting::setValue('signup_confirmation', array(
|
Setting::setValue('signup_confirmation.enabled', true);
|
||||||
'enabled' => true
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function createDefaultSegments() {
|
private function createDefaultSegments() {
|
||||||
|
@@ -194,96 +194,4 @@ class Widget extends \WP_Widget {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// set the content filter to replace the shortcode
|
|
||||||
if(isset($_GET['mailpoet_page']) && strlen(trim($_GET['mailpoet_page'])) > 0) {
|
|
||||||
switch($_GET['mailpoet_page']) {
|
|
||||||
|
|
||||||
case 'mailpoet_form_iframe':
|
|
||||||
$id = (isset($_GET['mailpoet_form']) && (int)$_GET['mailpoet_form'] > 0) ? (int)$_GET['mailpoet_form'] : null;
|
|
||||||
$form = Form::findOne($id);
|
|
||||||
|
|
||||||
if($form !== false) {
|
|
||||||
// render form
|
|
||||||
$output = Util\Export::get('html', $form->asArray());
|
|
||||||
// $output = do_shortcode($output);
|
|
||||||
print $output;
|
|
||||||
exit;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
// add_filter('wp_title', 'mailpoet_meta_page_title'));
|
|
||||||
add_filter('the_title', 'mailpoet_page_title', 10, 2);
|
|
||||||
add_filter('the_content', 'mailpoet_page_content', 98, 1);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function mailpoet_page_title($title = '', $id = null) {
|
|
||||||
// get signup confirmation page id
|
|
||||||
$signup_confirmation = Setting::getValue('signup_confirmation');
|
|
||||||
$page_id = $signup_confirmation['page'];
|
|
||||||
|
|
||||||
// check if we're on the signup confirmation page
|
|
||||||
if((int)$page_id === (int)$id) {
|
|
||||||
global $post;
|
|
||||||
|
|
||||||
// disable comments
|
|
||||||
$post->comment_status = 'close';
|
|
||||||
// disable password
|
|
||||||
$post->post_password = '';
|
|
||||||
|
|
||||||
$subscriber = null;
|
|
||||||
|
|
||||||
// get subscriber key from url
|
|
||||||
$subscriber_digest = (isset($_GET['mailpoet_key']) && strlen(trim($_GET['mailpoet_key'])) === 32) ? trim($_GET['mailpoet_key']) : null;
|
|
||||||
|
|
||||||
if($subscriber_digest !== null) {
|
|
||||||
// get subscriber
|
|
||||||
// TODO: change select() to selectOne() once it's implemented
|
|
||||||
$subscribers = $mailpoet->subscribers()->select(array(
|
|
||||||
'filter' => array(
|
|
||||||
'subscriber_digest' => $subscriber_digest
|
|
||||||
),
|
|
||||||
'limit' => 1
|
|
||||||
));
|
|
||||||
|
|
||||||
if(!empty($subscribers)) {
|
|
||||||
$subscriber = array_shift($subscribers);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// check if we have a subscriber record
|
|
||||||
if($subscriber === null) {
|
|
||||||
return __('Your confirmation link expired, please subscribe again.');
|
|
||||||
} else {
|
|
||||||
// we have a subscriber, let's check its state
|
|
||||||
switch($subscriber['subscriber_state']) {
|
|
||||||
case MailPoetSubscribers::STATE_UNCONFIRMED:
|
|
||||||
case MailPoetSubscribers::STATE_UNSUBSCRIBED:
|
|
||||||
// set subscriber state as confirmed
|
|
||||||
$mailpoet->subscribers()->update(array(
|
|
||||||
'subscriber' => $subscriber['subscriber'],
|
|
||||||
'subscriber_state' => MailPoetSubscribers::STATE_SUBSCRIBED,
|
|
||||||
'subscriber_confirmed_at' => time()
|
|
||||||
));
|
|
||||||
return __("You've subscribed");
|
|
||||||
break;
|
|
||||||
case MailPoetSubscribers::STATE_SUBSCRIBED:
|
|
||||||
return __("You've already subscribed");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return $title;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function mailpoet_page_content($content = '') {
|
|
||||||
if(strpos($content, '[mailpoet_page]') !== FALSE) {
|
|
||||||
$content = str_replace('[mailpoet_page]', '', $content);
|
|
||||||
}
|
|
||||||
return $content;
|
|
||||||
}
|
}
|
@@ -48,14 +48,44 @@ class Setting extends Model {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static function setValue($key, $value) {
|
public static function setValue($key, $value) {
|
||||||
if(is_array($value)) {
|
$keys = explode('.', $key);
|
||||||
$value = serialize($value);
|
|
||||||
}
|
|
||||||
|
|
||||||
return Setting::createOrUpdate(array(
|
if(count($keys) === 1) {
|
||||||
'name' => $key,
|
|
||||||
'value' => $value
|
if(is_array($value)) {
|
||||||
));
|
$value = serialize($value);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Setting::createOrUpdate(array(
|
||||||
|
'name' => $key,
|
||||||
|
'value' => $value
|
||||||
|
));
|
||||||
|
} else {
|
||||||
|
$main_key = array_shift($keys);
|
||||||
|
|
||||||
|
$setting_value = static::getValue($main_key, array());
|
||||||
|
$current_value = &$setting_value;
|
||||||
|
$last_key = array_pop($keys);
|
||||||
|
|
||||||
|
for($i = 0, $count = count($keys); $i < $count; $i++) {
|
||||||
|
if($i < $count) {
|
||||||
|
if(!is_array($current_value)) {
|
||||||
|
$current_value = array();
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!array_key_exists($keys[$i], $current_value)) {
|
||||||
|
$current_value = array($keys[$i] => array());
|
||||||
|
}
|
||||||
|
$current_value =& $current_value[$keys[$i]];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(is_scalar($current_value)) {
|
||||||
|
$current_value = array();
|
||||||
|
}
|
||||||
|
$current_value[$last_key] = $value;
|
||||||
|
|
||||||
|
return static::setValue($main_key, $setting_value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function getAll() {
|
public static function getAll() {
|
||||||
|
@@ -2,22 +2,71 @@
|
|||||||
namespace MailPoet\Settings;
|
namespace MailPoet\Settings;
|
||||||
|
|
||||||
class Pages {
|
class Pages {
|
||||||
|
function __construct() {
|
||||||
|
}
|
||||||
|
|
||||||
static function getAll() {
|
function init() {
|
||||||
$mailpoet_pages = \get_posts(array(
|
register_post_type('mailpoet_page', array(
|
||||||
|
'labels' => array(
|
||||||
|
'name' => __('MailPoet Page'),
|
||||||
|
'singular_name' => __('MailPoet Page')
|
||||||
|
),
|
||||||
|
'public' => true,
|
||||||
|
'has_archive' => false,
|
||||||
|
'show_ui' => true,
|
||||||
|
'show_in_menu' => false,
|
||||||
|
'rewrite' => false,
|
||||||
|
'show_in_nav_menus'=>false,
|
||||||
|
'can_export'=>false,
|
||||||
|
'publicly_queryable'=>true,
|
||||||
|
'exclude_from_search'=>true
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
static function createMailPoetPage() {
|
||||||
|
remove_all_actions('pre_post_update');
|
||||||
|
remove_all_actions('save_post');
|
||||||
|
remove_all_actions('wp_insert_post');
|
||||||
|
|
||||||
|
$id = wp_insert_post(array(
|
||||||
|
'post_status' => 'publish',
|
||||||
|
'post_type' => 'mailpoet_page',
|
||||||
|
'post_author' => 1,
|
||||||
|
'post_content' => '[mailpoet_page]',
|
||||||
|
'post_title' => __('MailPoet Page'),
|
||||||
|
'post_name' => 'subscriptions'
|
||||||
|
));
|
||||||
|
flush_rewrite_rules();
|
||||||
|
|
||||||
|
return ((int)$id > 0) ? (int)$id : false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static function getMailPoetPages() {
|
||||||
|
return get_posts(array(
|
||||||
'post_type' => 'mailpoet_page'
|
'post_type' => 'mailpoet_page'
|
||||||
));
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
static function getAll() {
|
||||||
|
$all_pages = array_merge(
|
||||||
|
static::getMailPoetPages(),
|
||||||
|
get_pages()
|
||||||
|
);
|
||||||
|
|
||||||
$pages = array();
|
$pages = array();
|
||||||
foreach(array_merge($mailpoet_pages, \get_pages()) as $page) {
|
foreach($all_pages as $page) {
|
||||||
$pages[] = array(
|
$pages[] = static::getPageData($page);
|
||||||
'id' => $page->ID,
|
|
||||||
'title' => $page->post_title,
|
|
||||||
'preview_url' => \get_permalink($page->ID),
|
|
||||||
'edit_url' => \get_edit_post_link($page->ID)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $pages;
|
return $pages;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static function getPageData($page) {
|
||||||
|
return array(
|
||||||
|
'id' => $page->ID,
|
||||||
|
'title' => $page->post_title,
|
||||||
|
'preview_url' => get_permalink($page->ID),
|
||||||
|
'edit_url' => get_edit_post_link($page->ID)
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user