· 7 years ago · Feb 07, 2019, 05:49 AM
1<?php
2
3/**
4 * Class FUE_Admin_Controller
5 *
6 * Controller for the Admin Panel
7 */
8class FUE_Admin_Controller {
9 /**
10 * Register the menu items
11 */
12 public static function add_menu() {
13 add_menu_page( __( 'Follow-Up', 'follow_up_emails' ), __( 'Follow-Up', 'follow_up_emails' ), 'manage_follow_up_emails', 'followup-emails', 'FUE_Admin_Controller::admin_controller', 'dashicons-email-alt', '54.51' );
14 add_submenu_page( 'followup-emails', __( 'Follow-Up Emails', 'follow_up_emails' ), __( 'Emails', 'follow_up_emails' ), 'manage_follow_up_emails', 'followup-emails', 'FUE_Admin_Controller::admin_controller' );
15 add_submenu_page( 'followup-emails', __( 'Campaigns', 'follow_up_emails' ), __( 'Campaigns', 'follow_up_emails' ), 'manage_follow_up_emails', 'fue_campaigns', 'FUE_Admin_Controller::admin_controller' );
16
17 add_submenu_page( 'followup-emails', __( 'New Follow-Up', 'follow_up_emails' ), __( 'New Follow-Up', 'follow_up_emails' ), 'manage_follow_up_emails', 'fue_post_email', 'FUE_Admin_Controller::admin_controller' );
18 add_submenu_page( 'followup-emails', __( 'New Tweet', 'follow_up_emails' ), __( 'New Tweet', 'follow_up_emails' ), 'manage_follow_up_emails', 'fue_post_tweet', 'FUE_Admin_Controller::admin_controller' );
19
20 do_action( 'fue_menu' );
21
22 add_submenu_page( 'followup-emails', __( 'Scheduled Emails', 'follow_up_emails' ), __( 'Scheduled Emails', 'follow_up_emails' ), 'manage_follow_up_emails', 'followup-emails-queue', 'FUE_Admin_Controller::queue_table' );
23 add_submenu_page( 'followup-emails', __( 'Subscribers', 'follow_up_emails' ), __( 'Mailing Lists', 'follow_up_emails' ), 'manage_follow_up_emails', 'followup-emails-subscribers', 'FUE_Admin_Controller::subscribers_table' );
24 add_submenu_page( 'followup-emails', __( 'Follow-Up Emails Templates', 'follow_up_emails' ), __( 'Templates', 'follow_up_emails' ), 'manage_follow_up_emails', 'followup-emails-templates', 'FUE_Admin_Controller::templates' );
25 add_submenu_page( 'followup-emails', __( 'Follow-Up Emails Settings', 'follow_up_emails' ), __( 'Settings', 'follow_up_emails' ), 'manage_follow_up_emails', 'followup-emails-settings', 'FUE_Admin_Controller::settings' );
26 }
27
28 public static function get_screen_ids() {
29 return apply_filters( 'fue_screen_ids', array(
30 'toplevel_page_followup-emails',
31 'follow_up_email',
32 'edit-follow_up_email_campaign',
33 'follow-up_page_followup-emails-reports',
34 'follow-up_page_followup-emails-reports-customers',
35 'follow-up_page_followup-emails-coupons',
36 'follow-up_page_followup-emails-queue',
37 'follow-up_page_followup-emails-subscribers',
38 'follow-up_page_followup-emails-optouts',
39 'follow-up_page_followup-emails-settings',
40 'follow-up_page_followup-emails-addons',
41 'follow-up_page_followup-emails-templates',
42 ) );
43 }
44
45 /**
46 * Enable all elements when working with HTML templates.
47 *
48 * @unused
49 * @param array $options
50 * @return array
51 */
52 public static function enable_mce_elements( $options ) {
53 if ( empty( $_GET['page'] ) || 'followup-emails-templates' != $_GET['page'] ) {
54 return $options;
55 }
56
57 if ( ! isset( $options['extended_valid_elements'] ) ) {
58 $options['extended_valid_elements'] = '';
59 } else {
60 $options['extended_valid_elements'] .= ',';
61 }
62
63 if ( ! isset( $options['custom_elements'] ) ) {
64 $options['custom_elements'] = '';
65 } else {
66 $options['custom_elements'] .= ',';
67 }
68
69 $options['extended_valid_elements'] .= '*[*]';
70 $options['custom_elements'] .= '*[*]';
71 $options['plugins'] .= ',fullpage';
72
73 return $options;
74 }
75
76 /**
77 * Register the full_page plugin for TinyMCE to edit full HTML templates
78 * @param array $plugins_array
79 * @return array
80 */
81 public static function register_mce_plugins( $plugins_array ) {
82 if ( empty( $_GET['page'] ) || 'followup-emails-templates' !== $_GET['page'] ) {
83 return $plugins_array;
84 }
85
86 $plugins = array( 'fullpage' ); // Add any more plugins you want to load here.
87
88 // Build the response - the key is the plugin name, value is the URL to the plugin JS.
89 foreach ( $plugins as $plugin ) {
90 $plugins_array[ $plugin ] = FUE_TEMPLATES_URL . '/js/tinymce/' . $plugin . '/plugin.min.js';
91 }
92 return $plugins_array;
93 }
94
95 /**
96 * Replace the placeholder URL we're using for the Email Form page with the actual URL.
97 *
98 * @param $url
99 * @param $original_url
100 * @param $_context
101 *
102 * @return string|void
103 */
104 public static function replace_email_form_url( $url, $original_url, $_context ) {
105 if ( 'admin.php?page=fue_post_email' === $url ) {
106 return admin_url( 'post-new.php?post_type=follow_up_email' );
107 } elseif ( 'admin.php?page=fue_post_tweet' === $url ) {
108 return admin_url( 'post-new.php?post_type=follow_up_email&type=twitter' );
109 } elseif ( 'admin.php?page=fue_campaigns' === $url ) {
110 return admin_url( 'edit-tags.php?taxonomy=follow_up_email_campaign' );
111 } elseif ( strpos( $url, 'edit.php?follow_up_email_campaign=' ) !== false ) {
112 $parts = array();
113 parse_str( $url, $parts );
114 $terms = array_values( $parts );
115
116 return esc_url( 'admin.php?page=followup-emails&campaign=' . $terms[0] );
117 }
118
119 return $url;
120 }
121
122 /**
123 * Set the current submenu item in the admin nav menu.
124 *
125 * @param string $parent_file
126 * @return string
127 */
128 public static function set_active_submenu( $parent_file ) {
129 global $submenu_file, $plugin_page;
130
131 if ( 'edit.php?post_type=follow_up_email' === $parent_file ) {
132 $parent_file = 'followup-emails';
133 $submenu_file = null;
134 } elseif ( 'edit-tags.php?taxonomy=follow_up_email_campaign' === $submenu_file ) {
135 $parent_file = 'followup-emails';
136 $submenu_file = 'fue_campaigns';
137 }
138
139 return $parent_file;
140 }
141
142 /**
143 * Routes the request to the correct page/file.
144 */
145 public static function admin_controller() {
146 $tab = isset( $_GET['tab'] ) ? $_GET['tab'] : 'list';
147
148 switch ( $tab ) {
149 case 'list':
150 self::list_emails_page();
151 break;
152 case 'edit':
153 self::email_form( 1, $_GET['id'] );
154 break;
155 case 'send':
156 $id = $_GET['id'];
157 $email = new FUE_Email( $id );
158 if ( ! $email->exists() ) {
159 wp_die( "The requested data could not be found!" );
160 }
161
162 self::send_manual_form( $email );
163 break;
164 case 'send_manual_emails':
165 self::send_manual_emails();
166 break;
167 case 'send_manual_email_batches':
168 self::send_manual_email_batches();
169 break;
170 case 'updater':
171 self::updater_page();
172 break;
173 case 'data_updater':
174 self::data_updater();
175 break;
176 case 'history':
177 self::show_followup_history();
178 break;
179 default:
180 // Allow add-ons to add tabs.
181 do_action( 'fue_admin_controller', $tab );
182 break;
183 }
184 }
185
186 /**
187 * FUE Dashboard Widget.
188 */
189 public static function dashboard_widget() {
190 wp_add_dashboard_widget( 'fue-dashboard', __( 'Follow-Up Emails', 'follow_up_emails' ), array( 'FUE_Report_Dashboard_Widget', 'display' ) );
191 }
192
193 /**
194 * Page that lists all FUE_Emails.
195 */
196 public static function list_emails_page() {
197 $types = Follow_Up_Emails::get_email_types();
198 $campaigns = get_terms( 'follow_up_email_campaign', array( 'hide_empty' => false ) );
199 $bccs = get_option( 'fue_bcc_types', false );
200 $from_addresses = get_option( 'fue_from_email_types', false );
201 $from_names = get_option( 'fue_from_name_types', false );
202
203 include FUE_TEMPLATES_DIR . '/email-list/email-list.php';
204 }
205
206 /**
207 * Send Single Email form.
208 *
209 * @param $email
210 */
211 public static function send_manual_form( $email ) {
212 $wpdb = Follow_Up_Emails::instance()->wpdb;
213
214 include FUE_TEMPLATES_DIR . '/send_manual_form.php';
215 }
216
217 /**
218 * Send manual emails in batches.
219 */
220 public static function send_manual_emails() {
221 include FUE_TEMPLATES_DIR . '/send_manual_emails.php';
222 }
223
224 /**
225 * If batched sending is enabled and the number of recipients exceeds the
226 * manual sending limit send the manual emails in batches.
227 */
228 public static function send_manual_email_batches() {
229 $args = array(
230 'page_title' => __( 'Processing Emails', 'follow_up_emails' ),
231 'return_url' => admin_url( 'admin.php?page=followup-emails' ),
232 'ajax_endpoint' => 'fue_send_manual_email_batches',
233 'entity_label_singular' => __( 'batch', 'follow_up_emails' ),
234 'entity_label_plural' => __( 'batches', 'follow_up_emails' ),
235 'action_label' => _x( 'processed', 'Data updater action label', 'follow_up_emails' ),
236 );
237
238 include FUE_TEMPLATES_DIR . '/data-updater.php';
239 }
240
241 /**
242 * Admin interface for managing subscribers.
243 */
244 public static function subscribers_table() {
245 $wpdb = Follow_Up_Emails::instance()->wpdb;
246 $view = ( empty( $_REQUEST['view'] ) ) ? 'subscribers' : $_REQUEST['view'];
247 include FUE_TEMPLATES_DIR . '/subscribers-page.php';
248 }
249
250 /**
251 * Admin Updater interface.
252 */
253 public static function updater_page() {
254 global $wpdb;
255
256 include FUE_TEMPLATES_DIR . '/updater.php';
257 }
258
259 /**
260 * Admin Updater interface.
261 */
262 public static function data_updater() {
263 $act = isset( $_GET['act'] ) ? $_GET['act'] : '';
264
265 switch ( $act ) {
266 case 'clear_scheduler':
267 self::clear_scheduler_updater();
268 break;
269 case 'migrate_logs':
270 self::migrate_logs();
271 break;
272 }
273 }
274
275 /**
276 * Handler for db version 20160203 that removes unnecessary scheduled-action
277 * rows.
278 */
279 public static function clear_scheduler_updater() {
280 $return = admin_url( 'admin.php?page=followup-emails' );
281
282 $args = array(
283 'page_title' => __( 'Data Update', 'follow_up_emails' ),
284 'return_url' => $return,
285 'ajax_endpoint' => 'fue_clear_scheduled_actions',
286 'entity_label_singular' => 'item',
287 'entity_label_plural' => 'items',
288 'action_label' => 'updated',
289 );
290
291 include FUE_TEMPLATES_DIR . '/data-updater.php';
292 }
293
294 /**
295 * Handler for db version 20160308 that moves follow-up history logs to
296 * the new followup_followup_history table.
297 */
298 public static function migrate_logs() {
299 $return = admin_url( 'admin.php?page=followup-emails' );
300
301 $args = array(
302 'page_title' => __( 'Data Update', 'follow_up_emails' ),
303 'return_url' => $return,
304 'ajax_endpoint' => 'fue_migrate_logs',
305 'entity_label_singular' => 'item',
306 'entity_label_plural' => 'items',
307 'action_label' => 'updated',
308 );
309
310 include FUE_TEMPLATES_DIR . '/data-updater.php';
311 }
312
313 /**
314 * Settings Interface
315 */
316 public static function settings() {
317 global $wpdb;
318
319 $pages = get_pages();
320 $emails = get_option( 'fue_daily_emails' );
321 $bcc = get_option( 'fue_bcc', '' );
322 $from = get_option( 'fue_from_email', '' );
323 $from_name = get_option( 'fue_from_name', get_bloginfo( 'name' ) );
324 $bounce = get_option( 'fue_bounce_settings', '' );
325 $email_batches = get_option( 'fue_email_batches', 0 );
326 $disable_logging = get_option( 'fue_disable_action_scheduler_logging', 1 );
327 $api_enabled = get_option( 'fue_api_enabled', 'yes' );
328 $emails_per_batch = get_option( 'fue_emails_per_batch', 100 );
329 $email_batch_interval = get_option( 'fue_batch_interval', 10 );
330 $spf = get_option( 'fue_spf', array() );
331 $dkim = get_option( 'fue_dkim', array() );
332 $tab = ( isset( $_GET['tab'] ) ) ? $_GET['tab'] : 'system';
333
334 include FUE_TEMPLATES_DIR . '/settings/settings.php';
335 }
336
337 /**
338 * Render the templates page.
339 */
340 public static function templates() {
341 add_thickbox();
342
343 include FUE_TEMPLATES_DIR . '/add-ons/templates.php';
344 }
345
346 /**
347 * System Info page.
348 */
349 public static function system_info() {
350 include FUE_TEMPLATES_DIR . '/settings/system-info.php';
351 }
352
353 /**
354 * Display the queue items.
355 */
356 public static function queue_table() {
357 $table = new FUE_Sending_Queue_List_Table();
358 $table->prepare_items();
359 $table->messages();
360 ?>
361 <style>
362 span.trash a {
363 color: #a00 !important;
364 }
365
366 </style>
367 <script>
368 jQuery(document).ready(function($) {
369 $("#delete-all-submit").click(function(e) {
370 if ( confirm( "<?php _e( 'This will delete ALL scheduled emails! Continue?', 'follow_up_emails' ); ?>" ) ) {
371 return true;
372 }
373 return false;
374 });
375 });
376 </script>
377 <div class="wrap">
378 <h2><?php _e( 'Scheduled Emails', 'follow_up_emails' ); ?></h2>
379
380 <form id="queue-filter" action="" method="get">
381 <?php $table->display(); ?>
382 </form>
383 </div>
384 <?php
385 }
386
387 public static function show_followup_history() {
388 $wpdb = Follow_Up_Emails::instance()->wpdb;
389 $id = ( ! empty( $_GET['id'] ) ) ? absint( $_GET['id'] ) : 0;
390 $emails = fue_get_emails( 'any', null, array( 'orderby' => 'post_title' ) );
391
392 if ( $id > 0 ) {
393 $args['followup_id'] = $id;
394 } else {
395 $args['number'] = 100;
396 }
397
398 $logs = FUE_Followup_Logger::get_logs( $args );
399
400 include FUE_TEMPLATES_DIR . '/email-log.php';
401 }
402
403 /**
404 * Register the scripts early so other addons can use them.
405 */
406 public static function register_scripts() {
407 $screen = get_current_screen();
408
409 if ( ! in_array( $screen->id, self::get_screen_ids() ) ) {
410 return;
411 }
412
413 if ( ! wp_script_is( 'jquery-tiptip', 'registered' ) ) {
414 wp_register_script( 'jquery-tiptip', FUE_URL . '/templates/js/jquery.tipTip.min.js', array( 'jquery' ), FUE_VERSION, true );
415 }
416
417 // blockUI.
418 if ( ! wp_script_is( 'jquery-blockui', 'registered' ) ) {
419 wp_register_script( 'jquery-blockui', FUE_URL . '/templates/js/jquery-blockui/jquery.blockUI.min.js', array( 'jquery' ), FUE_VERSION, true );
420 }
421
422 // select2.
423 if ( ! wp_script_is( 'select2', 'registered' ) ) {
424 wp_register_script( 'select2', '//cdnjs.cloudflare.com/ajax/libs/select2/3.5.2/select2.min.js', array( 'jquery' ), '3.5.2' );
425 wp_register_style( 'select2', '//cdnjs.cloudflare.com/ajax/libs/select2/3.5.2/select2.css' );
426 wp_register_style( 'select2-fue', plugins_url( 'templates/select2.css', FUE_FILE ), array(), '3.5.2' );
427 }
428 }
429
430 /**
431 * Load the necessary scripts.
432 */
433 public static function settings_scripts() {
434 $screen = get_current_screen();
435
436 $page = isset( $_GET['page'] ) ? $_GET['page'] : '';
437 $fue_pages = array(
438 'followup-emails',
439 'followup-emails-form',
440 'followup-emails-reports',
441 'followup-emails-reports-customers',
442 'followup-emails-queue',
443 'followup-emails-subscribers',
444 );
445
446 if ( in_array( $page, $fue_pages ) || 'follow_up_email' === $screen->post_type ) {
447 wp_enqueue_script( 'jquery-blockui' );
448 wp_enqueue_script( 'media-upload' );
449 wp_enqueue_script( 'thickbox' );
450 wp_enqueue_script( 'editor' );
451
452 wp_enqueue_style( 'thickbox' );
453
454 wp_enqueue_script( 'jquery-tiptip' );
455 wp_enqueue_script( 'jquery-ui-core', null, array( 'jquery' ) );
456 wp_enqueue_script( 'jquery-ui-datepicker', null, array( 'jquery-ui-core' ) );
457 wp_enqueue_script( 'jquery-ui-sortable', null, array( 'jquery-ui-core' ) );
458 wp_enqueue_script( 'fue-list', plugins_url( 'templates/js/email-list.js', FUE_FILE ), array( 'jquery', 'jquery-ui-datepicker', 'jquery-ui-sortable' ), FUE_VERSION );
459 wp_enqueue_script( 'raphael', plugins_url( 'templates/js/justgage/raphael.min.js', FUE_FILE ), null, '2.1.0', true );
460 wp_enqueue_script( 'justgage', plugins_url( 'templates/js/justgage/justgage.1.1.min.js', FUE_FILE ), null, '1.1', true );
461
462 wp_enqueue_style( 'jquery-ui-css', '//ajax.googleapis.com/ajax/libs/jqueryui/1.8.21/themes/base/jquery-ui.css' );
463 wp_enqueue_style( 'fue_email_form', plugins_url( 'templates/email-form.css', FUE_FILE ) );
464
465 $translate = apply_filters( 'fue_script_locale', array(
466 'email_name' => __( 'Email Name', 'follow_up_emails' ),
467 'processing_request' => __( 'Processing request...', 'follow_up_emails' ),
468 'dupe' => __( 'A follow-up email with the same settings already exists. Do you want to create it anyway?', 'follow_up_emails' ),
469 'similar' => __( 'A similar follow-up email already exists. Do you wish to continue?', 'follow_up_emails' ),
470 'save' => isset( $_GET['mode'] ) ? __( 'Save', 'follow_up_emails' ) : __( 'Build your email', 'follow_up_emails' ),
471 'ajax_loader' => plugins_url() . '/woocommerce-follow-up-emails/templates/images/ajax-loader.gif',
472 ) );
473 wp_localize_script( 'fue-list', 'FUE', $translate );
474 }
475
476 if ( in_array( $screen->id, array( 'dashboard' ) ) ) {
477 wp_enqueue_style( 'fue_admin_dashboard_styles', plugins_url( 'templates/dashboard.css', FUE_FILE ), array(), FUE_VERSION );
478
479 wp_enqueue_script( 'jsapi', '//www.google.com/jsapi' );
480 wp_enqueue_script( 'js-cookie', FUE_TEMPLATES_URL . '/js/js-cookie.js', array(), FUE_VERSION );
481 wp_enqueue_script( 'fue-dashboard', FUE_TEMPLATES_URL . '/js/dashboard.js', array( 'justgage', 'jquery-tiptip', 'js-cookie' ), FUE_VERSION );
482
483 wp_enqueue_script( 'raphael', plugins_url( 'templates/js/justgage/raphael.min.js', FUE_FILE ), null, '2.1.0', true );
484 wp_enqueue_script( 'justgage', plugins_url( 'templates/js/justgage/justgage.1.1.min.js', FUE_FILE ), null, '1.1', true );
485
486 wp_enqueue_script( 'circliful', plugins_url( 'templates/js/circliful/js/jquery.circliful.min.js', FUE_FILE ), array( 'jquery' ) );
487 wp_enqueue_style( 'circliful', plugins_url( 'templates/js/circliful/css/jquery.circliful.css', FUE_FILE ), array(), FUE_VERSION );
488
489 wp_enqueue_script( 'jquery-tiptip' );
490 }
491
492 if ( 'followup-emails-reports' === $page ) {
493 wp_enqueue_style( 'fue_admin_report_flags', plugins_url( 'templates/flags.css', FUE_FILE ), array(), FUE_VERSION );
494 wp_enqueue_style( 'fue_admin_report_styles', plugins_url( 'templates/reports.css', FUE_FILE ), array(), FUE_VERSION );
495
496 wp_enqueue_script( 'circliful', plugins_url( 'templates/js/circliful/js/jquery.circliful.min.js', FUE_FILE ), array( 'jquery' ) );
497 wp_enqueue_style( 'circliful', plugins_url( 'templates/js/circliful/css/jquery.circliful.css', FUE_FILE ), array(), FUE_VERSION );
498
499 wp_enqueue_script( 'fue-user-view', plugins_url( 'templates/js/user-view.js', FUE_FILE ), array( 'jquery' ), FUE_VERSION );
500 wp_enqueue_script( 'jquery-ui-datepicker', null, array( 'jquery' ) );
501 }
502
503 if ( in_array( $page, array( 'followup-emails-queue', 'followup-emails-settings', 'followup-emails', 'followup-emails-subscribers', 'followup-emails-reports-customers' ) ) ) {
504 wp_enqueue_script( 'select2' );
505 wp_enqueue_style( 'select2-fue', plugins_url( 'templates/select2.css', FUE_FILE ), array(), '3.5.2' );
506 wp_enqueue_script( 'jquery-tiptip' );
507
508 if ( ! empty( $_GET['tab'] ) ) {
509 switch ( $_GET['tab'] ) {
510 case 'send_manual_emails':
511 wp_enqueue_script( 'fue_manual_send', FUE_TEMPLATES_URL . '/js/manual_send.js', array(
512 'jquery',
513 'jquery-ui-progressbar',
514 ), FUE_VERSION );
515 break;
516
517 case 'send_manual_email_batches':
518 case 'data_updater':
519 wp_enqueue_script( 'jquery-ui-progressbar', false, array( 'jquery', 'jquery-ui' ) );
520 wp_enqueue_script(
521 'fue_data_updater',
522 FUE_TEMPLATES_URL . '/js/data-updater.js',
523 array( 'jquery', 'jquery-ui-progressbar' ),
524 FUE_VERSION
525 );
526 break;
527 }
528 }
529 }
530
531 if ( 'followup-emails-settings' === $page ) {
532 wp_enqueue_script( 'fue_settings', FUE_TEMPLATES_URL . '/js/settings.js', array( 'jquery' ), FUE_VERSION, true );
533 wp_enqueue_style( 'fue-settings', FUE_TEMPLATES_URL . '/add-ons/fue-settings.css' );
534 }
535
536 if ( 'followup-emails-templates' === $page ) {
537 wp_enqueue_script( 'jquery-blockui' );
538 wp_enqueue_script( 'fue_templates', FUE_TEMPLATES_URL . '/js/templates.js', array( 'jquery' ), FUE_VERSION );
539
540 $translate = apply_filters( 'fue_script_locale', array(
541 'ajax_loader' => plugins_url() . '/woocommerce-follow-up-emails/templates/images/ajax-loader.gif',
542 'get_template_nonce' => wp_create_nonce( 'get_template_html' ),
543 'save_template_nonce' => wp_create_nonce( 'save_template_html' ),
544 ) );
545 wp_localize_script( 'fue_templates', 'FUE_Templates', $translate );
546
547 wp_enqueue_style( 'fue-addons', FUE_TEMPLATES_URL . '/add-ons/templates.css' );
548 }
549 }
550}