| 1 |
<?php |
| 2 |
class Poll_Maker_Settings_Actions { |
| 3 |
private $plugin_name; |
| 4 |
|
| 5 |
public function __construct($plugin_name) { |
| 6 |
$this->plugin_name = $plugin_name; |
| 7 |
// $this->check_setting_mailchimp(); |
| 8 |
} |
| 9 |
|
| 10 |
public function store_data($data){ |
| 11 |
global $wpdb; |
| 12 |
$settings_table = $wpdb->prefix . "ayspoll_settings"; |
| 13 |
if( isset($data["settings_action"]) && wp_verify_nonce( $data["settings_action"], 'settings_action' ) ){ |
| 14 |
$success = 0; |
| 15 |
$roles = (isset($data['ays_user_roles']) && !empty($data['ays_user_roles'])) ? array_map( 'sanitize_text_field', $data['ays_user_roles'] ) : array('administrator'); |
| 16 |
$mailchimp_username = isset($data['ays_mailchimp_username']) ? stripslashes(sanitize_text_field($data['ays_mailchimp_username'])) : ''; |
| 17 |
$mailchimp_api_key = isset($data['ays_mailchimp_api_key']) ? (sanitize_text_field($data['ays_mailchimp_api_key'])) : ''; |
| 18 |
|
| 19 |
if (preg_match('/^[a-zA-Z0-9]+-[a-z]+\d+$/', $mailchimp_api_key)) { |
| 20 |
$mailchimp_api_key = stripslashes($mailchimp_api_key); |
| 21 |
} else { |
| 22 |
$mailchimp_api_key = ''; |
| 23 |
} |
| 24 |
|
| 25 |
$mailchimp = array( |
| 26 |
'username' => $mailchimp_username, |
| 27 |
'apiKey' => $mailchimp_api_key |
| 28 |
); |
| 29 |
|
| 30 |
$mailchimp = array( |
| 31 |
'username' => $mailchimp_username, |
| 32 |
'apiKey' => $mailchimp_api_key |
| 33 |
); |
| 34 |
|
| 35 |
$disable_ip_storing = (isset($data['ays_disable_ip_storing']) && $data['ays_disable_ip_storing'] == 'on') ? "on" : 'off'; |
| 36 |
|
| 37 |
$answer_default_count = isset($data['ays_answer_default_count']) && !empty($data['ays_answer_default_count']) ? absint( sanitize_text_field($data['ays_answer_default_count']) ) : 2; |
| 38 |
|
| 39 |
$answers_sound = isset($data['ays_poll_answers_sound']) ? sanitize_text_field($data['ays_poll_answers_sound']) : ''; |
| 40 |
|
| 41 |
// Poll title length |
| 42 |
$poll_title_length = (isset($data['ays_poll_title_length']) && intval($data['ays_poll_title_length']) != 0) ? absint(intval($data['ays_poll_title_length'])) : 5; |
| 43 |
|
| 44 |
// Poll Category Title length |
| 45 |
$poll_category_title_length = (isset($data['ays_poll_category_title_length']) && intval($data['ays_poll_category_title_length']) != 0) ? absint(intval($data['ays_poll_category_title_length'])) : 5; |
| 46 |
|
| 47 |
// Poll Results Title length |
| 48 |
$poll_results_title_length = (isset($data['ays_poll_results_title_length']) && intval($data['ays_poll_results_title_length']) != 0) ? absint(intval($data['ays_poll_results_title_length'])) : 5; |
| 49 |
|
| 50 |
// Default Category for poll |
| 51 |
$ays_default_cat = (isset($data['ays_poll_default_cat']) && !empty($data['ays_poll_default_cat'])) ? sanitize_text_field(implode("," , $data['ays_poll_default_cat'])) : "1"; |
| 52 |
|
| 53 |
// Default Type for poll |
| 54 |
$ays_default_type = (isset($data['ays_poll_default_type']) && $data['ays_poll_default_type'] != "") ? sanitize_text_field($data['ays_poll_default_type']) : "choosing"; |
| 55 |
|
| 56 |
// Poll expired and unpublished message |
| 57 |
$all_shortcode_message = isset($data['ays_poll_all_polls_shortcodes']) && $data['ays_poll_all_polls_shortcodes'] != '' ? sanitize_text_field($data['ays_poll_all_polls_shortcodes']) : ''; |
| 58 |
|
| 59 |
// Show result view |
| 60 |
$poll_show_result_view = isset($data['ays_poll_show_result_view']) ? sanitize_text_field($data['ays_poll_show_result_view']) : 'standart'; |
| 61 |
|
| 62 |
// Animation Top |
| 63 |
$poll_animation_top = (isset($data['ays_poll_animation_top']) && $data['ays_poll_animation_top'] != '') ? absint(intval($data['ays_poll_animation_top'])) : 100; |
| 64 |
$poll_enable_animation_top = (isset( $data['ays_poll_enable_animation_top'] ) && $data['ays_poll_enable_animation_top'] == 'on') ? 'on' : 'off'; |
| 65 |
|
| 66 |
// General CSS File |
| 67 |
$poll_exclude_general_css = (isset( $data['ays_poll_exclude_general_css'] ) && sanitize_text_field( $data['ays_poll_exclude_general_css'] ) == 'on') ? 'on' : 'off'; |
| 68 |
|
| 69 |
// WP Editor height |
| 70 |
$poll_wp_editor_height = (isset($data['ays_poll_wp_editor_height']) && $data['ays_poll_wp_editor_height'] != '' && $data['ays_poll_wp_editor_height'] != 0) ? absint( sanitize_text_field($data['ays_poll_wp_editor_height']) ) : 50; |
| 71 |
|
| 72 |
$options = array( |
| 73 |
"disable_ip_storing" => $disable_ip_storing, |
| 74 |
"answer_default_count" => $answer_default_count, |
| 75 |
"answers_sound" => $answers_sound, |
| 76 |
"poll_title_length" => $poll_title_length, |
| 77 |
"poll_category_title_length"=> $poll_category_title_length, |
| 78 |
"poll_results_title_length" => $poll_results_title_length, |
| 79 |
"default_category" => $ays_default_cat, |
| 80 |
"default_type" => $ays_default_type, |
| 81 |
"all_shortcode_message" => $all_shortcode_message, |
| 82 |
"show_result_view" => $poll_show_result_view, |
| 83 |
"poll_animation_top" => $poll_animation_top, |
| 84 |
"poll_enable_animation_top" => $poll_enable_animation_top, |
| 85 |
"poll_exclude_general_css" => $poll_exclude_general_css, |
| 86 |
"poll_wp_editor_height" => $poll_wp_editor_height, |
| 87 |
); |
| 88 |
|
| 89 |
$poll_fields_placeholder_name = (isset($data['ays_poll_fields_placeholder_name']) && $data['ays_poll_fields_placeholder_name'] != '') ? stripslashes(sanitize_text_field( $data['ays_poll_fields_placeholder_name'] )) : 'Name' ; |
| 90 |
$poll_fields_placeholder_email = (isset($data['ays_poll_fields_placeholder_email']) && $data['ays_poll_fields_placeholder_email'] != '') ? stripslashes(sanitize_text_field( $data['ays_poll_fields_placeholder_email'] )) : 'E-mail' ; |
| 91 |
$poll_fields_placeholder_phone = (isset($data['ays_poll_fields_placeholder_phone']) && $data['ays_poll_fields_placeholder_phone'] != '') ? stripslashes(sanitize_text_field( $data['ays_poll_fields_placeholder_phone'] )) : 'Phone' ; |
| 92 |
|
| 93 |
$poll_fields_label_name = (isset($data['ays_poll_fields_label_name']) && $data['ays_poll_fields_label_name'] != '') ? stripslashes(sanitize_text_field( $data['ays_poll_fields_label_name'] )) : 'Name' ; |
| 94 |
$poll_fields_label_email = (isset($data['ays_poll_fields_label_email']) && $data['ays_poll_fields_label_email'] != '') ? stripslashes(sanitize_text_field( $data['ays_poll_fields_label_email'] )) : 'E-mail' ; |
| 95 |
$poll_fields_label_phone = (isset($data['ays_poll_fields_label_phone']) && $data['ays_poll_fields_label_phone'] != '') ? stripslashes(sanitize_text_field( $data['ays_poll_fields_label_phone'] )) : 'Phone' ; |
| 96 |
|
| 97 |
$fields_placeholders = array( |
| 98 |
'poll_fields_placeholder_name' => $poll_fields_placeholder_name, |
| 99 |
'poll_fields_placeholder_email' => $poll_fields_placeholder_email, |
| 100 |
'poll_fields_placeholder_phone' => $poll_fields_placeholder_phone, |
| 101 |
|
| 102 |
'poll_fields_label_name' => $poll_fields_label_name, |
| 103 |
'poll_fields_label_email' => $poll_fields_label_email, |
| 104 |
'poll_fields_label_phone' => $poll_fields_label_phone, |
| 105 |
|
| 106 |
|
| 107 |
); |
| 108 |
|
| 109 |
$del_stat = ""; |
| 110 |
$month_count = isset($data['ays_delete_results_by']) ? intval(sanitize_text_field($data['ays_delete_results_by'])) : null; |
| 111 |
if($month_count !== null && $month_count > 0){ |
| 112 |
$year = intval( date( 'Y', current_time('timestamp') ) ); |
| 113 |
$dt = intval( date( 'n', current_time('timestamp') ) ); |
| 114 |
$month = $dt - $month_count; |
| 115 |
if($month < 0){ |
| 116 |
$month = 12 - $month; |
| 117 |
if($month > 12){ |
| 118 |
$mn = $month % 12; |
| 119 |
$mnac = ($month - $mn) / 12; |
| 120 |
$month = 12 - ($mn); |
| 121 |
$year -= $mnac; |
| 122 |
} |
| 123 |
}elseif($month == 0){ |
| 124 |
$month = 12; |
| 125 |
$year--; |
| 126 |
} |
| 127 |
$sql = "DELETE FROM " . esc_sql($wpdb->prefix) . "ayspoll_reports |
| 128 |
WHERE YEAR(vote_date) = '".esc_sql($year)."' |
| 129 |
AND MONTH(vote_date) <= '".esc_sql($month)."'"; |
| 130 |
$res = $wpdb->query($sql); |
| 131 |
if($res >= 0){ |
| 132 |
$del_stat = "&del_stat=ok&mcount=".$month_count; |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
$result = $this->ays_update_setting('mailchimp', json_encode($mailchimp)); |
| 137 |
if ($result) { |
| 138 |
$success++; |
| 139 |
} |
| 140 |
|
| 141 |
$result = $this->ays_update_setting('options', json_encode($options)); |
| 142 |
if($result){ |
| 143 |
$success++; |
| 144 |
} |
| 145 |
|
| 146 |
$result = $this->ays_update_setting('fields_placeholders', json_encode($fields_placeholders, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES)); |
| 147 |
if ($result) { |
| 148 |
$success++; |
| 149 |
} |
| 150 |
|
| 151 |
$message = "saved"; |
| 152 |
if($success > 0){ |
| 153 |
$tab = ""; |
| 154 |
if(isset($data['ays_poll_tab'])){ |
| 155 |
$tab = "&ays_poll_tab=".sanitize_text_field($data['ays_poll_tab']); |
| 156 |
} |
| 157 |
$url = admin_url('admin.php') . "?page=poll-maker-ays-settings" . $tab . '&status=' . $message.$del_stat; |
| 158 |
wp_redirect( esc_url_raw($url) ); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
} |
| 163 |
|
| 164 |
public function get_db_data(){ |
| 165 |
global $wpdb; |
| 166 |
$settings_table = esc_sql($wpdb->prefix . "ayspoll_settings"); |
| 167 |
$sql = "SELECT * FROM ".$settings_table; |
| 168 |
|
| 169 |
$results = $wpdb->get_results($sql, 'ARRAY_A'); |
| 170 |
if(count($results) > 0){ |
| 171 |
return $results; |
| 172 |
}else{ |
| 173 |
return array(); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
public function check_setting_mailchimp(){ |
| 178 |
global $wpdb; |
| 179 |
$settings_table = esc_sql($wpdb->prefix . "ayspoll_settings"); |
| 180 |
$mailchimp = esc_sql('mailchimp'); |
| 181 |
$sql = "SELECT COUNT(*) FROM ".$settings_table." WHERE meta_key = %s"; |
| 182 |
|
| 183 |
$result = $wpdb->get_var( |
| 184 |
$wpdb->prepare( $sql, $mailchimp) |
| 185 |
); |
| 186 |
|
| 187 |
if(intval($result) == 0){ |
| 188 |
$this->ays_add_setting("mailchimp", "", "", ""); |
| 189 |
} |
| 190 |
return false; |
| 191 |
} |
| 192 |
|
| 193 |
public function ays_get_setting($meta_key){ |
| 194 |
global $wpdb; |
| 195 |
$settings_table = esc_sql($wpdb->prefix . "ayspoll_settings"); |
| 196 |
$key_meta = esc_sql($meta_key); |
| 197 |
|
| 198 |
$sql = "SELECT meta_value FROM ".$settings_table." WHERE meta_key = %s"; |
| 199 |
|
| 200 |
$result = $wpdb->get_var( |
| 201 |
$wpdb->prepare( $sql, $key_meta) |
| 202 |
); |
| 203 |
|
| 204 |
if($result != ""){ |
| 205 |
return $result; |
| 206 |
} |
| 207 |
return false; |
| 208 |
} |
| 209 |
|
| 210 |
public function ays_add_setting($meta_key, $meta_value, $note = "", $options = ""){ |
| 211 |
global $wpdb; |
| 212 |
$settings_table = $wpdb->prefix . "ayspoll_settings"; |
| 213 |
$result = $wpdb->insert( |
| 214 |
$settings_table, |
| 215 |
array( |
| 216 |
'meta_key' => $meta_key, |
| 217 |
'meta_value' => $meta_value, |
| 218 |
'note' => $note, |
| 219 |
'options' => $options |
| 220 |
), |
| 221 |
array( '%s', '%s', '%s', '%s' ) |
| 222 |
); |
| 223 |
if($result >= 0){ |
| 224 |
return true; |
| 225 |
} |
| 226 |
return false; |
| 227 |
} |
| 228 |
|
| 229 |
public function ays_update_setting($meta_key, $meta_value, $note = null, $options = null){ |
| 230 |
global $wpdb; |
| 231 |
$settings_table = $wpdb->prefix . "ayspoll_settings"; |
| 232 |
$value = array( |
| 233 |
'meta_value' => $meta_value, |
| 234 |
); |
| 235 |
$value_s = array( '%s' ); |
| 236 |
if($note != null){ |
| 237 |
$value['note'] = $note; |
| 238 |
$value_s[] = '%s'; |
| 239 |
} |
| 240 |
if($options != null){ |
| 241 |
$value['options'] = $options; |
| 242 |
$value_s[] = '%s'; |
| 243 |
} |
| 244 |
$result = $wpdb->update( |
| 245 |
$settings_table, |
| 246 |
$value, |
| 247 |
array( 'meta_key' => $meta_key, ), |
| 248 |
$value_s, |
| 249 |
array( '%s' ) |
| 250 |
); |
| 251 |
if($result >= 0){ |
| 252 |
return true; |
| 253 |
} |
| 254 |
return false; |
| 255 |
} |
| 256 |
|
| 257 |
public function ays_delete_setting($meta_key){ |
| 258 |
global $wpdb; |
| 259 |
$settings_table = $wpdb->prefix . "ayspoll_settings"; |
| 260 |
$wpdb->delete( |
| 261 |
$settings_table, |
| 262 |
array( 'meta_key' => $meta_key ), |
| 263 |
array( '%s' ) |
| 264 |
); |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
public function poll_settings_notices($status){ |
| 269 |
|
| 270 |
if ( empty( $status ) ) |
| 271 |
return; |
| 272 |
|
| 273 |
if ( 'saved' == $status ) |
| 274 |
$updated_message = esc_html(esc_html__( 'Changes saved.', "poll-maker" ) ); |
| 275 |
elseif ( 'updated' == $status ) |
| 276 |
$updated_message = esc_html(esc_html__( 'Poll attribute .', "poll-maker" ) ); |
| 277 |
elseif ( 'deleted' == $status ) |
| 278 |
$updated_message = esc_html(esc_html__( 'Poll attribute deleted.', "poll-maker" ) ); |
| 279 |
|
| 280 |
if ( empty( $updated_message ) ) |
| 281 |
return; |
| 282 |
|
| 283 |
?> |
| 284 |
<div class="ays-poll-admin-notice notice notice-success is-dismissible"> |
| 285 |
<p> <?php echo $updated_message; ?> </p> |
| 286 |
</div> |
| 287 |
<?php |
| 288 |
} |
| 289 |
|
| 290 |
} |