themes = new NewsletterThemes('emails');
parent::__construct('emails', '1.1.5');
add_action('wp_loaded', array($this, 'hook_wp_loaded'));
if (is_admin()) {
add_action('wp_ajax_tnpc_render', array($this, 'tnpc_render_callback'));
add_action('wp_ajax_tnpc_preview', array($this, 'tnpc_preview_callback'));
add_action('wp_ajax_tnpc_css', array($this, 'tnpc_css_callback'));
add_action('wp_ajax_tnpc_options', array($this, 'hook_wp_ajax_tnpc_options'));
add_action('wp_ajax_tnpc_presets', array($this, 'hook_wp_ajax_tnpc_presets'));
// Thank you to plugins which add the WP editor on other admin plugin pages...
if (isset($_GET['page']) && $_GET['page'] == 'newsletter_emails_edit') {
global $wp_actions;
$wp_actions['wp_enqueue_editor'] = 1;
}
}
}
function options_decode($options) {
// Start compatibility
if (is_string($options) && strpos($options, 'options[') !== false) {
$opts = array();
parse_str($options, $opts);
$options = $opts['options'];
}
// End compatibility
if (is_array($options)) {
return $options;
}
$tmp = json_decode($options, true);
if (is_null($tmp)) {
return json_decode(base64_decode($options), true);
} else {
return $tmp;
}
}
/**
*
* @param array $options Options array
*/
function options_encode($options) {
return base64_encode(json_encode($options, JSON_HEX_TAG | JSON_HEX_AMP));
}
function hook_wp_ajax_tnpc_options() {
global $wpdb;
// TODO: Uniform to use id everywhere
if (!isset($_REQUEST['id']))
$_REQUEST['id'] = $_REQUEST['b'];
$block = $this->get_block($_REQUEST['id']);
if (!$block) {
die('Block not found with id ' . esc_html($_REQUEST['id']));
}
if (!class_exists('NewsletterControls')) {
include NEWSLETTER_INCLUDES_DIR . '/controls.php';
}
$options = $this->options_decode(stripslashes_deep($_REQUEST['options']));
$context = array('type'=>'');
if (isset($_REQUEST['context_type'])) $context['type'] = $_REQUEST['context_type'];
// $defaults = array(
// 'block_padding_top' => 15,
// 'block_padding_bottom' => 15,
// 'block_padding_right' => 0,
// 'block_padding_left' => 0,
// 'block_background' => '#ffffff'
// );
//
// $options = array_merge($defaults, $options);
$controls = new NewsletterControls($options);
$fields = new NewsletterFields($controls);
$controls->init();
echo '';
echo '';
ob_start();
include $block['dir'] . '/options.php';
$content = ob_get_clean();
echo "
", esc_html($block["name"]), "
";
echo $content;
wp_die();
}
/**
* Retrieves the presets list (no id in GET) or a specific preset id in GET)
*
* @return string
*/
function hook_wp_ajax_tnpc_presets() {
$content = "";
if (!empty($_REQUEST['id'])) {
// Preset render
$preset = $this->get_preset($_REQUEST['id']);
foreach ($preset->blocks as $item) {
$this->render_block($item->block, true, (array) $item->options);
}
} else {
$content = "