| 1 |
<?php |
| 2 |
|
| 3 |
namespace YayMail\Controllers; |
| 4 |
|
| 5 |
use YayMail\Abstracts\BaseController; |
| 6 |
use YayMail\Models\SettingModel; |
| 7 |
use YayMail\Models\TemplateModel; |
| 8 |
use YayMail\Utils\SingletonTrait; |
| 9 |
use YayMail\YayMailTemplate; |
| 10 |
|
| 11 |
/** |
| 12 |
* Template Controller |
| 13 |
* |
| 14 |
* @method static TemplateController get_instance() |
| 15 |
*/ |
| 16 |
class TemplateController extends BaseController { |
| 17 |
use SingletonTrait; |
| 18 |
|
| 19 |
private $model = null; |
| 20 |
|
| 21 |
protected function __construct() { |
| 22 |
$this->model = TemplateModel::get_instance(); |
| 23 |
$this->init_hooks(); |
| 24 |
} |
| 25 |
|
| 26 |
protected function init_hooks() { |
| 27 |
$template_id_args = [ |
| 28 |
'template_id' => [ |
| 29 |
'type' => 'string', |
| 30 |
'required' => true, |
| 31 |
], |
| 32 |
]; |
| 33 |
register_rest_route( |
| 34 |
YAYMAIL_REST_NAMESPACE, |
| 35 |
'/templates', |
| 36 |
[ |
| 37 |
[ |
| 38 |
'methods' => \WP_REST_Server::READABLE, |
| 39 |
'callback' => [ $this, 'exec_get_all_templates' ], |
| 40 |
'permission_callback' => [ $this, 'permission_callback' ], |
| 41 |
], |
| 42 |
] |
| 43 |
); |
| 44 |
|
| 45 |
register_rest_route( |
| 46 |
YAYMAIL_REST_NAMESPACE, |
| 47 |
'/templates/(?P<template_id>\d+)', |
| 48 |
[ |
| 49 |
[ |
| 50 |
'methods' => \WP_REST_Server::READABLE, |
| 51 |
'callback' => [ $this, 'exec_get_template_by_id' ], |
| 52 |
'permission_callback' => [ $this, 'permission_callback' ], |
| 53 |
'args' => $template_id_args, |
| 54 |
], |
| 55 |
[ |
| 56 |
'methods' => \WP_REST_Server::EDITABLE, |
| 57 |
'callback' => [ $this, 'exec_update_template' ], |
| 58 |
'permission_callback' => [ $this, 'permission_callback' ], |
| 59 |
'args' => $template_id_args, |
| 60 |
], |
| 61 |
[ |
| 62 |
'methods' => \WP_REST_Server::DELETABLE, |
| 63 |
'callback' => [ $this, 'exec_delete_template' ], |
| 64 |
'permission_callback' => [ $this, 'permission_callback' ], |
| 65 |
'args' => $template_id_args, |
| 66 |
], |
| 67 |
] |
| 68 |
); |
| 69 |
|
| 70 |
register_rest_route( |
| 71 |
YAYMAIL_REST_NAMESPACE, |
| 72 |
'/templates/get-template-by-name', |
| 73 |
[ |
| 74 |
[ |
| 75 |
'methods' => \WP_REST_Server::READABLE, |
| 76 |
'callback' => [ $this, 'exec_get_template_by_name' ], |
| 77 |
'permission_callback' => [ $this, 'permission_callback' ], |
| 78 |
], |
| 79 |
] |
| 80 |
); |
| 81 |
|
| 82 |
register_rest_route( |
| 83 |
YAYMAIL_REST_NAMESPACE, |
| 84 |
'/templates/change-status', |
| 85 |
[ |
| 86 |
[ |
| 87 |
'methods' => \WP_REST_Server::EDITABLE, |
| 88 |
'callback' => [ $this, 'exec_change_status' ], |
| 89 |
'permission_callback' => [ $this, 'permission_callback' ], |
| 90 |
], |
| 91 |
] |
| 92 |
); |
| 93 |
|
| 94 |
register_rest_route( |
| 95 |
YAYMAIL_REST_NAMESPACE, |
| 96 |
'/templates/reset', |
| 97 |
[ |
| 98 |
[ |
| 99 |
'methods' => \WP_REST_Server::EDITABLE, |
| 100 |
'callback' => [ $this, 'exec_reset_templates' ], |
| 101 |
'permission_callback' => [ $this, 'permission_callback' ], |
| 102 |
], |
| 103 |
] |
| 104 |
); |
| 105 |
|
| 106 |
register_rest_route( |
| 107 |
YAYMAIL_REST_NAMESPACE, |
| 108 |
'/templates/copy-template', |
| 109 |
[ |
| 110 |
[ |
| 111 |
'methods' => \WP_REST_Server::EDITABLE, |
| 112 |
'callback' => [ $this, 'exec_copy_template' ], |
| 113 |
'permission_callback' => [ $this, 'permission_callback' ], |
| 114 |
], |
| 115 |
] |
| 116 |
); |
| 117 |
|
| 118 |
register_rest_route( |
| 119 |
YAYMAIL_REST_NAMESPACE, |
| 120 |
'/templates/(?P<template_name>[a-zA-Z0-9_-]+)/all-elements', |
| 121 |
[ |
| 122 |
[ |
| 123 |
'methods' => \WP_REST_Server::READABLE, |
| 124 |
'callback' => [ $this, 'exec_get_all_elements_by_template' ], |
| 125 |
'permission_callback' => [ $this, 'permission_callback' ], |
| 126 |
'args' => [ |
| 127 |
'template_name' => [ |
| 128 |
'type' => 'string', |
| 129 |
'required' => true, |
| 130 |
], |
| 131 |
], |
| 132 |
], |
| 133 |
] |
| 134 |
); |
| 135 |
|
| 136 |
register_rest_route( |
| 137 |
YAYMAIL_REST_NAMESPACE, |
| 138 |
'/templates/(?P<template_name>[a-zA-Z0-9_-]+)/all-shortcodes/(?P<order>[a-zA-Z0-9_-]+)', |
| 139 |
[ |
| 140 |
[ |
| 141 |
'methods' => \WP_REST_Server::READABLE, |
| 142 |
'callback' => [ $this, 'exec_get_all_shortcodes_by_template' ], |
| 143 |
'permission_callback' => [ $this, 'permission_callback' ], |
| 144 |
'args' => [ |
| 145 |
'template_name' => [ |
| 146 |
'type' => 'string', |
| 147 |
'required' => true, |
| 148 |
], |
| 149 |
'order' => [ |
| 150 |
'type' => 'string', |
| 151 |
'required' => true, |
| 152 |
], |
| 153 |
], |
| 154 |
], |
| 155 |
] |
| 156 |
); |
| 157 |
|
| 158 |
register_rest_route( |
| 159 |
YAYMAIL_REST_NAMESPACE, |
| 160 |
'/templates/global-header-footer/change-status', |
| 161 |
[ |
| 162 |
[ |
| 163 |
'methods' => \WP_REST_Server::EDITABLE, |
| 164 |
'callback' => [ $this, 'exec_change_global_header_footer_status' ], |
| 165 |
'permission_callback' => [ $this, 'permission_callback' ], |
| 166 |
], |
| 167 |
] |
| 168 |
); |
| 169 |
} |
| 170 |
|
| 171 |
public function exec_get_all_templates( \WP_REST_Request $request ) { |
| 172 |
return $this->exec( [ $this, 'get_all_templates' ], $request ); |
| 173 |
} |
| 174 |
|
| 175 |
public function get_all_templates( \WP_REST_Request $request ) { |
| 176 |
$templates = $this->model->find_all(); |
| 177 |
return apply_filters( 'yaymail_get_all_templates', $templates ); |
| 178 |
} |
| 179 |
|
| 180 |
public function exec_get_template_by_id( \WP_REST_Request $request ) { |
| 181 |
return $this->exec( [ $this, 'get_template_by_id' ], $request ); |
| 182 |
} |
| 183 |
|
| 184 |
public function get_template_by_id( \WP_REST_Request $request ) { |
| 185 |
$id = sanitize_text_field( $request->get_param( 'template_id' ) ); |
| 186 |
$template_data = $this->model::find_by_id( $id ); |
| 187 |
return $template_data; |
| 188 |
} |
| 189 |
|
| 190 |
public function exec_update_template( \WP_REST_Request $request ) { |
| 191 |
return $this->exec( [ $this, 'update_template' ], $request ); |
| 192 |
} |
| 193 |
|
| 194 |
public function update_template( \WP_REST_Request $request ) { |
| 195 |
$data = json_decode( $request->get_param( 'data' ), true ); |
| 196 |
$id = sanitize_text_field( $data['template_id'] ); |
| 197 |
$elements = $data['template_elements']; |
| 198 |
// TODO: later |
| 199 |
// $elements = Helpers::elements_remove_settings_empty( $request->get_param( 'template_elements' ) ); |
| 200 |
$background_color = sanitize_text_field( $data['background_color'] ); |
| 201 |
$text_link_color = sanitize_text_field( $data['text_link_color'] ); |
| 202 |
$content_background_color = sanitize_text_field( $data['content_background_color'] ); |
| 203 |
$content_text_color = sanitize_text_field( $data['content_text_color'] ); |
| 204 |
$title_color = sanitize_text_field( $data['title_color'] ); |
| 205 |
$preheader = sanitize_text_field( $data['preheader'] ); |
| 206 |
$global_header_settings = $data['global_header_settings'] ?? YayMailTemplate::DEFAULT_DATA['global_header_settings']; |
| 207 |
$global_footer_settings = $data['global_footer_settings'] ?? YayMailTemplate::DEFAULT_DATA['global_footer_settings']; |
| 208 |
$update_data = [ |
| 209 |
'elements' => $elements, |
| 210 |
'background_color' => $background_color, |
| 211 |
'text_link_color' => $text_link_color, |
| 212 |
'content_background_color' => $content_background_color, |
| 213 |
'content_text_color' => $content_text_color, |
| 214 |
'title_color' => $title_color, |
| 215 |
'global_header_settings' => $global_header_settings, |
| 216 |
'global_footer_settings' => $global_footer_settings, |
| 217 |
'preheader' => $preheader, |
| 218 |
]; |
| 219 |
|
| 220 |
if ( isset( $data['email_subject'] ) ) { |
| 221 |
$template_name = get_post_meta( (int) $id, YayMailTemplate::META_KEYS['name'], true ); |
| 222 |
$this->model::save_email_subject( $template_name, $data['email_subject'] ); |
| 223 |
} |
| 224 |
|
| 225 |
return $this->model::update( $id, $update_data, true ); |
| 226 |
} |
| 227 |
|
| 228 |
public function exec_delete_template( \WP_REST_Request $request ) { |
| 229 |
return $this->exec( [ $this, 'delete_template' ], $request ); |
| 230 |
} |
| 231 |
|
| 232 |
public function delete_template( \WP_REST_Request $request ) { |
| 233 |
$id = sanitize_text_field( $request->get_param( 'template_id' ) ); |
| 234 |
$this->model::delete( $id ); |
| 235 |
return [ 'success' => true ]; |
| 236 |
} |
| 237 |
|
| 238 |
public function exec_get_template_by_name( \WP_REST_Request $request ) { |
| 239 |
return $this->exec( [ $this, 'get_template_by_name' ], $request ); |
| 240 |
} |
| 241 |
|
| 242 |
public function get_template_by_name( \WP_REST_Request $request ) { |
| 243 |
$template_name = sanitize_text_field( $request->get_param( 'template_name' ) ); |
| 244 |
$template_data = $this->model::find_by_name( $template_name ); |
| 245 |
|
| 246 |
if ( null === $template_data ) { |
| 247 |
$all_emails = yaymail_get_emails(); |
| 248 |
if ( in_array( |
| 249 |
$template_name, |
| 250 |
array_map( |
| 251 |
function ( $email ) { |
| 252 |
return $email->get_id(); |
| 253 |
}, |
| 254 |
$all_emails |
| 255 |
) |
| 256 |
) ) { |
| 257 |
|
| 258 |
$template_data = $this->model::insert( |
| 259 |
[ |
| 260 |
'name' => $template_name, |
| 261 |
'elements' => yaymail_get_default_elements( $template_name ), |
| 262 |
] |
| 263 |
); |
| 264 |
} |
| 265 |
}//end if |
| 266 |
|
| 267 |
return $template_data; |
| 268 |
} |
| 269 |
|
| 270 |
public function exec_change_status( \WP_REST_Request $request ) { |
| 271 |
return $this->exec( [ $this, 'change_status' ], $request ); |
| 272 |
} |
| 273 |
|
| 274 |
public function change_status( \WP_REST_Request $request ) { |
| 275 |
$list_id = is_array( $request->get_param( 'list_id' ) ) ? array_map( 'sanitize_text_field', wp_unslash( $request->get_param( 'list_id' ) ) ) : []; |
| 276 |
$status = sanitize_text_field( $request->get_param( 'status' ) ); |
| 277 |
foreach ( $list_id as $id ) { |
| 278 |
$this->model::update( |
| 279 |
$id, |
| 280 |
[ |
| 281 |
'status' => $status, |
| 282 |
] |
| 283 |
); |
| 284 |
} |
| 285 |
return [ 'success' => true ]; |
| 286 |
} |
| 287 |
|
| 288 |
public function exec_reset_templates( \WP_REST_Request $request ) { |
| 289 |
return $this->exec( [ $this, 'reset_templates' ], $request ); |
| 290 |
} |
| 291 |
|
| 292 |
public function reset_templates( \WP_REST_Request $request ) { |
| 293 |
$list_id = is_array( $request->get_param( 'list_id' ) ) ? array_map( 'sanitize_text_field', wp_unslash( $request->get_param( 'list_id' ) ) ) : []; |
| 294 |
$list_template_data = []; |
| 295 |
|
| 296 |
foreach ( $list_id as $id ) { |
| 297 |
$template_data = $this->model::find_by_id( $id ); |
| 298 |
$default_elements = yaymail_get_default_elements( $template_data['name'] ); |
| 299 |
$update_data = [ |
| 300 |
'elements' => $default_elements, |
| 301 |
'background_color' => YayMailTemplate::DEFAULT_DATA['background_color'], |
| 302 |
'text_link_color' => YayMailTemplate::DEFAULT_DATA['text_link_color'], |
| 303 |
'content_background_color' => YayMailTemplate::DEFAULT_DATA['content_background_color'], |
| 304 |
'content_text_color' => YayMailTemplate::DEFAULT_DATA['content_text_color'], |
| 305 |
'title_color' => YayMailTemplate::DEFAULT_DATA['title_color'], |
| 306 |
'global_header_settings' => wp_parse_args( |
| 307 |
[ |
| 308 |
'hidden' => true, |
| 309 |
], |
| 310 |
$template_data['global_header_settings'] ?? YayMailTemplate::DEFAULT_DATA['global_header_settings'], |
| 311 |
), |
| 312 |
'global_footer_settings' => wp_parse_args( |
| 313 |
[ |
| 314 |
'hidden' => true, |
| 315 |
], |
| 316 |
$template_data['global_footer_settings'] ?? YayMailTemplate::DEFAULT_DATA['global_footer_settings'], |
| 317 |
), |
| 318 |
]; |
| 319 |
$template_data['elements'] = $update_data['elements']; |
| 320 |
$template_data['background_color'] = $update_data['background_color']; |
| 321 |
$template_data['text_link_color'] = $update_data['text_link_color']; |
| 322 |
$template_data['content_background_color'] = $update_data['content_background_color']; |
| 323 |
$template_data['content_text_color'] = $update_data['content_text_color']; |
| 324 |
$template_data['title_color'] = $update_data['title_color'] ?? '#000000'; |
| 325 |
$template_data['global_header_settings'] = $update_data['global_header_settings']; |
| 326 |
$template_data['global_footer_settings'] = $update_data['global_footer_settings']; |
| 327 |
|
| 328 |
$list_template_data[] = $template_data; |
| 329 |
$this->model::update( $id, $update_data, true ); |
| 330 |
}//end foreach |
| 331 |
|
| 332 |
return [ |
| 333 |
'success' => true, |
| 334 |
'list_template_data' => $list_template_data, |
| 335 |
]; |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
public function exec_copy_template( \WP_REST_Request $request ) { |
| 340 |
return $this->exec( [ $this, 'copy_template' ], $request ); |
| 341 |
} |
| 342 |
|
| 343 |
public function copy_template( \WP_REST_Request $request ) { |
| 344 |
$template_id = sanitize_text_field( $request->get_param( 'template_id' ) ); |
| 345 |
$from_template = sanitize_text_field( $request->get_param( 'from_template' ) ); |
| 346 |
$copy_template_data = $this->model::find_by_name( $from_template ); |
| 347 |
|
| 348 |
if ( empty( $copy_template_data ) || empty( $copy_template_data['id'] ) ) { |
| 349 |
return [ |
| 350 |
'success' => false, |
| 351 |
'message' => 'Template not found', |
| 352 |
]; |
| 353 |
} |
| 354 |
|
| 355 |
$update_data = [ |
| 356 |
'elements' => ! empty( $copy_template_data['elements'] ) ? $copy_template_data['elements'] : yaymail_get_default_elements( $from_template ), |
| 357 |
'background_color' => $copy_template_data['background_color'] ?? YAYMAIL_COLOR_BACKGROUND_DEFAULT, |
| 358 |
'content_background_color' => $copy_template_data['content_background_color'] ?? '#ffffff', |
| 359 |
'content_text_color' => $copy_template_data['content_text_color'] ?? '#000000', |
| 360 |
'text_link_color' => $copy_template_data['text_link_color'] ?? YAYMAIL_COLOR_WC_DEFAULT, |
| 361 |
'global_header_settings' => ! empty( $copy_template_data['global_header_settings'] ) ? $copy_template_data['global_header_settings'] : YayMailTemplate::DEFAULT_DATA['global_header_settings'], |
| 362 |
'global_footer_settings' => ! empty( $copy_template_data['global_footer_settings'] ) ? $copy_template_data['global_footer_settings'] : YayMailTemplate::DEFAULT_DATA['global_footer_settings'], |
| 363 |
'title_color' => $copy_template_data['title_color'] ?? '#000000', |
| 364 |
]; |
| 365 |
|
| 366 |
$this->model::update( $template_id, $update_data, true ); |
| 367 |
return [ |
| 368 |
'success' => true, |
| 369 |
]; |
| 370 |
} |
| 371 |
|
| 372 |
public function exec_get_all_elements_by_template( \WP_REST_Request $request ) { |
| 373 |
return $this->exec( [ $this, 'get_all_elements' ], $request ); |
| 374 |
} |
| 375 |
|
| 376 |
public function get_all_elements( \WP_REST_Request $request ) { |
| 377 |
$template_name = sanitize_text_field( $request->get_param( 'template_name' ) ); |
| 378 |
$elements = $this->model::get_elements_for_template( $template_name ); |
| 379 |
return $elements; |
| 380 |
} |
| 381 |
|
| 382 |
public function exec_get_all_shortcodes_by_template( \WP_REST_Request $request ) { |
| 383 |
return $this->exec( [ $this, 'get_all_shortcodes' ], $request ); |
| 384 |
} |
| 385 |
|
| 386 |
public function get_all_shortcodes( \WP_REST_Request $request ) { |
| 387 |
$template_name = sanitize_text_field( $request->get_param( 'template_name' ) ); |
| 388 |
$order_id = sanitize_text_field( $request->get_param( 'order' ) ); |
| 389 |
return $this->model->get_shortcodes_by_template_name_and_order_id( $template_name, $order_id ); |
| 390 |
} |
| 391 |
|
| 392 |
public function exec_change_global_header_footer_status( \WP_REST_Request $request ) { |
| 393 |
return $this->exec( [ $this, 'change_global_header_footer_status' ], $request ); |
| 394 |
} |
| 395 |
|
| 396 |
public function change_global_header_footer_status( \WP_REST_Request $request ) { |
| 397 |
$status = filter_var( $request->get_param( 'status' ), FILTER_VALIDATE_BOOLEAN ); |
| 398 |
$platform = sanitize_text_field( $request->get_param( 'platform' ) ?? 'yaymail' ); |
| 399 |
$platform_obj = \YayMail\Platform\PlatformRegistry::get( $platform ) ?? \YayMail\Platform\PlatformRegistry::get( 'yaymail' ); |
| 400 |
$key = $platform_obj ? $platform_obj->ghf_option_key( 'enabled' ) : 'global_header_footer_enabled'; |
| 401 |
return SettingModel::update( [ $key => $status ] ); |
| 402 |
} |
| 403 |
} |