| 1 |
<?php |
| 2 |
/** |
| 3 |
* WPPIZZA_MODULE_TEMPLATES_TEMPLATES Class |
| 4 |
* |
| 5 |
* @package WPPIZZA |
| 6 |
* @subpackage WPPIZZA_MODULE_TEMPLATES_TEMPLATES |
| 7 |
* @copyright Copyright (c) 2015, Oliver Bach |
| 8 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 9 |
* @since 3.0 |
| 10 |
* |
| 11 |
*/ |
| 12 |
if ( ! defined( 'ABSPATH' ) ) exit;/*Exit if accessed directly*/ |
| 13 |
|
| 14 |
|
| 15 |
/************************************************************************************************************************ |
| 16 |
* |
| 17 |
* |
| 18 |
* |
| 19 |
* |
| 20 |
* |
| 21 |
* |
| 22 |
************************************************************************************************************************/ |
| 23 |
class WPPIZZA_MODULE_TEMPLATES_TEMPLATES{ |
| 24 |
|
| 25 |
private $settings_page = 'templates';/* which admin subpage (identified there by this->class_key) are we adding this to */ |
| 26 |
private $section_key = 'templates';/* must be unique */ |
| 27 |
private $module_priority = 10;/* display order (priority) of settings in subpage */ |
| 28 |
|
| 29 |
|
| 30 |
function __construct() { |
| 31 |
/********************************************************** |
| 32 |
[add settings to admin] |
| 33 |
***********************************************************/ |
| 34 |
if(is_admin()){ |
| 35 |
/** admin ajax **/ |
| 36 |
add_action('wppizza_ajax_admin_'.$this->settings_page.'', array( $this, 'admin_ajax')); |
| 37 |
|
| 38 |
} |
| 39 |
} |
| 40 |
/******************************************************************************************************************************************************* |
| 41 |
* |
| 42 |
* [admin ajax] |
| 43 |
* |
| 44 |
********************************************************************************************************************************************************/ |
| 45 |
function admin_ajax($wppizza_options){ |
| 46 |
|
| 47 |
//need right caps for access |
| 48 |
if( !current_user_can('wppizza_cap_'.$this->settings_page.'')){ |
| 49 |
echo '<div class="wppizza-highlight">'.__('Access Denied', 'wppizza-admin').' ['.$this->section_key.']</div>'; |
| 50 |
exit(); |
| 51 |
} |
| 52 |
|
| 53 |
/***************************************************** |
| 54 |
[preview templates output] |
| 55 |
*****************************************************/ |
| 56 |
if($_POST['vars']['field']=='preview_template'){ |
| 57 |
|
| 58 |
/**************************************** |
| 59 |
get the last completed order |
| 60 |
to use as preview |
| 61 |
****************************************/ |
| 62 |
|
| 63 |
$order = WPPIZZA() -> db -> get_last_completed_blog_order_id(false); |
| 64 |
|
| 65 |
/**************************************** |
| 66 |
no order exists that could be used |
| 67 |
as preview |
| 68 |
****************************************/ |
| 69 |
if(empty($order)){ |
| 70 |
$markup['error']="Error [ADM-101]:" . PHP_EOL . __(' Sorry, you must have at least one completed order for the preview to work.','wppizza-admin'). PHP_EOL ; |
| 71 |
|
| 72 |
print"".json_encode($markup).""; |
| 73 |
exit(); |
| 74 |
} |
| 75 |
|
| 76 |
/************************************************************ |
| 77 |
what template type (print/emails) |
| 78 |
************************************************************/ |
| 79 |
$preview_type = $_POST['vars']['data']['template_type']; |
| 80 |
/************************************************************ |
| 81 |
template id |
| 82 |
************************************************************/ |
| 83 |
$preview_id = $_POST['vars']['data']['template_id']; |
| 84 |
/************************************************************ |
| 85 |
template id |
| 86 |
************************************************************/ |
| 87 |
$preview_is_html = ($_POST['vars']['data']['mail_type'] == 'phpmailer') ? true : false ; |
| 88 |
/************************************************************ |
| 89 |
map preview parameters |
| 90 |
into the same multidimensional array (keys / values) |
| 91 |
as they will be stored in the options table when saving |
| 92 |
but ignoring |
| 93 |
[sort], [title], [omit_attachments] and [recipients_additional] |
| 94 |
as these are not required for output display/formatting |
| 95 |
of email/print templates |
| 96 |
************************************************************/ |
| 97 |
$preview = array();/* ini array */ |
| 98 |
/************************************************************ |
| 99 |
plaintext or html ? |
| 100 |
************************************************************/ |
| 101 |
$preview['mail_type'] = empty($preview_is_html) ? 'wp_mail' : 'phpmailer' ; |
| 102 |
$preview['content-type'] = empty($preview_is_html) ? 'text/plain' : 'text/html' ; |
| 103 |
|
| 104 |
/************************************************************ |
| 105 |
parse all available |
| 106 |
************************************************************/ |
| 107 |
$data = array(); |
| 108 |
parse_str($_POST['vars']['data']['template_all_elements'], $data); |
| 109 |
$data_sort = $data[WPPIZZA_SLUG]['templates'][$preview_type][$preview_id]; |
| 110 |
|
| 111 |
/************************************************************ |
| 112 |
parse enabled and map |
| 113 |
************************************************************/ |
| 114 |
$data = array(); |
| 115 |
parse_str($_POST['vars']['data']['template_elements_enabled'], $data); |
| 116 |
$data_enabled = $data[WPPIZZA_SLUG]['templates'][$preview_type][$preview_id]; |
| 117 |
|
| 118 |
/************************************************************ |
| 119 |
parse styles and map |
| 120 |
************************************************************/ |
| 121 |
$data = array(); |
| 122 |
parse_str($_POST['vars']['data']['template_styles'], $data); |
| 123 |
$data_styles = $data[WPPIZZA_SLUG]['templates'][$preview_type][$preview_id]; |
| 124 |
|
| 125 |
/************************************************************ |
| 126 |
merge mapped vars (styles, enabled lables/parameters etc), |
| 127 |
recursively, adding to preview array |
| 128 |
|
| 129 |
i.e just like it will/would be saved in the options table |
| 130 |
************************************************************/ |
| 131 |
$template_parameters = array_replace_recursive($data_enabled, $data_styles, $data_sort); |
| 132 |
|
| 133 |
/*********************************************************** |
| 134 |
check if any sections are actually enabled |
| 135 |
otherwise we'll just display a message to that effect |
| 136 |
************************************************************/ |
| 137 |
$sections_enabled = 0; |
| 138 |
foreach($template_parameters['sections'] as $section){ |
| 139 |
if(!empty($section['section_enabled'])){ |
| 140 |
$sections_enabled++; |
| 141 |
break; |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
/****************************************** |
| 146 |
let's check if we have anything selected |
| 147 |
to start off with. if not display that fact |
| 148 |
*****************************************/ |
| 149 |
if(empty($sections_enabled)){ |
| 150 |
|
| 151 |
$obj['error'] = "Error [ADM-102]: " . PHP_EOL . __('you did not select anything to display ?!','wppizza-admin'). PHP_EOL ; |
| 152 |
|
| 153 |
}else{ |
| 154 |
|
| 155 |
/** get plaintext */ |
| 156 |
if(!$preview_is_html){ |
| 157 |
$plaintext_data = WPPIZZA()->templates_email_print->get_template_email_plaintext_sections_markup($order, $template_parameters, $preview_type, $preview_id); |
| 158 |
$preview['markup']['plaintext'] = $plaintext_data['markup']; |
| 159 |
} |
| 160 |
|
| 161 |
/** get html output */ |
| 162 |
if($preview_is_html){ |
| 163 |
$html_data = WPPIZZA()->templates_email_print->get_template_email_html_sections_markup($order, $template_parameters, $preview_type, $preview_id); |
| 164 |
$preview['markup']['html'] = $html_data; |
| 165 |
} |
| 166 |
|
| 167 |
$obj=$preview; |
| 168 |
} |
| 169 |
|
| 170 |
print"".json_encode($obj).""; |
| 171 |
exit(); |
| 172 |
} |
| 173 |
/***************************************************** |
| 174 |
[adding a new message/template] |
| 175 |
*****************************************************/ |
| 176 |
if($_POST['vars']['field']=='add_template'){ |
| 177 |
/**********set header********************/ |
| 178 |
header('Content-type: application/json'); |
| 179 |
/*emails or print ?*/ |
| 180 |
$template_type = wppizza_validate_string($_POST['vars']['arrayKey']); |
| 181 |
|
| 182 |
/**get currently saved templates**/ |
| 183 |
$templates_saved = get_option(WPPIZZA_SLUG.'_templates_'.$template_type, 0); |
| 184 |
|
| 185 |
/* |
| 186 |
get all current templates that already exist |
| 187 |
and determine highest key |
| 188 |
*/ |
| 189 |
$nextKey=0; |
| 190 |
if(!empty($templates_saved)){ |
| 191 |
$highestSetKey = max(array_keys($templates_saved)); |
| 192 |
$nextKey=$highestSetKey + 1 + (int)$_POST['vars']['countNewKeys'];/*if we are adding more than one new one at the time, count them and add those to key id*/ |
| 193 |
} |
| 194 |
|
| 195 |
$obj['markup'] = WPPIZZA()->templates_email_print->admin_template($nextKey, $template_type, false); |
| 196 |
|
| 197 |
print"".json_encode($obj).""; |
| 198 |
exit(); |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
} |
| 203 |
/*************************************************************** |
| 204 |
* |
| 205 |
* [ini] |
| 206 |
* |
| 207 |
***************************************************************/ |
| 208 |
$WPPIZZA_MODULE_TEMPLATES_TEMPLATES = new WPPIZZA_MODULE_TEMPLATES_TEMPLATES(); |
| 209 |
?> |