-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsend-statistics-modal.php
103 lines (83 loc) · 3.82 KB
/
send-statistics-modal.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
if ( !class_exists('FruitfulStatisticModal')) {
class FruitfulStatisticModal {
public $data;
/**
* Constructor
**/
public function __construct( $data ) {
$this->data = $data;
// Add action to enqueue modal notification scripts
add_action( 'admin_enqueue_scripts', array( $this, 'add_admin_scripts' ) );
// Add action on submit modal notification
add_action( 'wp_ajax_fruitful_statistic_submit_modal', array( $this, 'submit_modal' ) );
}
/**
* Function enqueue scripts for all admin pages
*/
public function add_admin_scripts() {
if ( ! wp_script_is( 'fruitful-stats-modal', 'enqueued' ) ) {
wp_enqueue_script( 'fruitful-stats-modal', $this->data['stats_uri'] . 'fruitful-stats/assets/js/admin_scripts.js', array( 'jquery' ) );
}
if ( ! wp_style_is( 'fruitful-stats-modal-styles', 'enqueued' ) ) {
wp_enqueue_style( 'fruitful-stats-modal-styles', $this->data['stats_uri'] . 'fruitful-stats/assets/styles/admin_styles.css' );
}
}
/**
* Function show modal notification
* And update fruitful theme settings options on first theme init
*/
public function admin_show_modal() {
require $this->data['stats_path'] . 'fruitful-stats/view/send-statistics-modal-view.php';
}
/**
* Action on submit statistics modal notification
*/
public function submit_modal() {
$request_data = $_POST['data'];
$response = array(
'status' => 'failed',
'title' => esc_html__( 'Uh oh!', 'fruitful-stats' ),
'error_message' => esc_html__( 'Sorry, something went wrong, and we failed to receive the shared data from you.', 'fruitful-stats' ),
'error_description' => esc_html__( 'No worries; go to the theme option to enter the required data manually and save changes.', 'fruitful-stats' ),
'stat_msg' => '',
'subscr_msg' => ''
);
$ffc_statistics_option = get_option( 'ffc_statistics_option' );
if ( ! empty( $request_data ) ) {
foreach ( $request_data as $option => $value ) {
if ( $option === 'ffc_statistic' || $option === 'ffc_subscribe' ) {
$ffc_statistics_option[ $option ] = (int) $value;
} elseif ( $option === 'ffc_subscribe_name' ) {
$ffc_statistics_option[ $option ] = sanitize_text_field( $value );
} elseif ( $option === 'ffc_subscribe_email' ) {
$ffc_statistics_option[ $option ] = sanitize_email( $value );
} else {
$ffc_statistics_option[ $option ] = $value;
}
}
update_option( 'ffc_statistics_option', $ffc_statistics_option );
if ( $ffc_statistics_option['ffc_statistic'] === 1 || $ffc_statistics_option['ffc_subscribe'] === 1 ) {
$response = array(
'status' => 'success',
'title' => esc_html__( 'Thank you!', 'fruitful-stats' ),
'error_message' => '',
'error_description' => '',
'stat_msg' => $ffc_statistics_option['ffc_statistic'] === 1 ? esc_html__( 'Thank you for being supportive, we appreciate your understanding and assistance!', 'fruitful-stats' ) : '',
'subscr_msg' => $ffc_statistics_option['ffc_subscribe'] === 1 ? esc_html__( 'Don\'t forget to check your inbox for our latest letter - you\'ll like that!', 'fruitful-stats' ) : '',
);
} else {
$response = array(
'status' => 'success',
'title' => esc_html__( 'What a pity!', 'fruitful-stats' ),
'error_message' => '',
'error_description' => '',
'stat_msg' => esc_html__( 'We wish you could have shared your site anonymous technical data and joined our community.', 'fruitful-stats' ),
'subscr_msg' => esc_html__( 'But if you ever change your mind, you can always do that in the theme options.', 'fruitful-stats' )
);
}
}
wp_send_json( $response );
}
}
}