| 1 |
<?php |
| 2 |
/** |
| 3 |
* Booking Calendar Admin Deactivation Feedback Class |
| 4 |
* |
| 5 |
* @package Booking Calendar Admin |
| 6 |
* @version 1.0.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
defined( 'ABSPATH' ) || exit; |
| 10 |
|
| 11 |
if ( ! class_exists( 'WPBC_Admin_Deactivation_Feedback', false ) ) : |
| 12 |
|
| 13 |
/** |
| 14 |
* WPBC_Admin_Deactivation_Feedback Class |
| 15 |
*/ |
| 16 |
class WPBC_Admin_Deactivation_Feedback { |
| 17 |
|
| 18 |
const FEEDBACK_URL = 'https://wpbookingcalendar.com/wp-json/tgreporting/v1/deactivation/'; |
| 19 |
|
| 20 |
/** |
| 21 |
* Class constructor. |
| 22 |
* Attaches the necessary actions and filters for the deactivate feedback feature. |
| 23 |
* Adds an action to enqueue scripts on the plugins screen. |
| 24 |
* Adds an action to handle the AJAX request for sending deactivate feedback. |
| 25 |
*/ |
| 26 |
public function __construct() { |
| 27 |
add_action( |
| 28 |
'current_screen', |
| 29 |
function () { |
| 30 |
if ( ! $this->is_plugins_screen() ) { |
| 31 |
return; |
| 32 |
} |
| 33 |
|
| 34 |
add_action( 'admin_enqueue_scripts', array( $this, 'scripts' ) ); |
| 35 |
} |
| 36 |
); |
| 37 |
|
| 38 |
// Ajax. |
| 39 |
add_action( 'wp_ajax_wpbc_deactivate_feedback', array( $this, 'send' ) ); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Enqueue scripts. |
| 44 |
*/ |
| 45 |
public function scripts() { |
| 46 |
add_action( 'admin_footer', array( $this, 'feedback_html' ) ); |
| 47 |
|
| 48 |
// $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
| 49 |
$suffix = ''; |
| 50 |
|
| 51 |
wp_enqueue_script( 'wpbc-admin-deactivation-feedback', |
| 52 |
wpbc_plugin_url( '/includes/_feedback_deactivation/_out/feedback.js' ), |
| 53 |
array( 'jquery' ), |
| 54 |
WP_BK_VERSION_NUM, |
| 55 |
array( 'in_footer' => WPBC_JS_IN_FOOTER ) |
| 56 |
); |
| 57 |
wp_localize_script( |
| 58 |
'wpbc-admin-deactivation-feedback', |
| 59 |
'wpbc_plugins_params', |
| 60 |
array( |
| 61 |
'ajax_url' => admin_url( 'admin-ajax.php' ), |
| 62 |
'more_details_required_text' => esc_html__( 'Please share more details before submitting.', 'booking' ), |
| 63 |
) |
| 64 |
); |
| 65 |
|
| 66 |
wp_enqueue_style( 'wpbc-admin-deactivation-feedback', trailingslashit( plugins_url( '', __FILE__ ) ) . '_out/feedback.css', array(), WP_BK_VERSION_NUM ); |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
/** |
| 71 |
* Deactivation Feedback HTML. |
| 72 |
* |
| 73 |
* @return void |
| 74 |
*/ |
| 75 |
public function feedback_html() { |
| 76 |
$wpbc_deactivate_reasons = array( |
| 77 |
'feature_unavailable' => array( |
| 78 |
'title' => esc_html__( 'Missing important features', 'booking' ), |
| 79 |
), |
| 80 |
'complex_to_use' => array( |
| 81 |
'title' => esc_html__( 'I found the plugin complex to use.', 'booking' ), |
| 82 |
), |
| 83 |
'temporary_deactivation' => array( |
| 84 |
'title' => esc_html__( 'Temporary deactivation', 'booking' ), |
| 85 |
), |
| 86 |
'found_a_better_plugin' => array( |
| 87 |
'title' => esc_html__( 'I found a better alternative', 'booking' ), |
| 88 |
), |
| 89 |
'no_longer_needed' => array( |
| 90 |
'title' => esc_html__( 'I no longer need the plugin', 'booking' ), |
| 91 |
), |
| 92 |
'setup_confusing' => array( |
| 93 |
'title' => esc_html__( 'Plugin setup process is confusing', 'booking' ), |
| 94 |
), |
| 95 |
'no_documentation' => array( |
| 96 |
'title' => esc_html__( 'Lack of documentation or tutorials', 'booking' ), |
| 97 |
), |
| 98 |
'other' => array( |
| 99 |
'title' => esc_html__( 'Technical issues / Bugs', 'booking' ), |
| 100 |
), |
| 101 |
); |
| 102 |
|
| 103 |
require_once WPBC_PLUGIN_DIR . '/includes/_feedback_deactivation/feedback_view.php'; // API |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Send API Request. |
| 108 |
* |
| 109 |
* @return void |
| 110 |
*/ |
| 111 |
public function send() { |
| 112 |
if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( $_POST['_wpnonce'] ), '_wpbc_deactivate_feedback_nonce' ) ) { |
| 113 |
wp_send_json_error(); |
| 114 |
} |
| 115 |
|
| 116 |
$reason_text = '---'; |
| 117 |
$reason_slug = ''; |
| 118 |
|
| 119 |
if ( ! empty( $_POST['reason_slug'] ) ) { |
| 120 |
$reason_slug = sanitize_text_field( wp_unslash( $_POST['reason_slug'] ) ); |
| 121 |
} |
| 122 |
|
| 123 |
if ( ! empty( $_POST[ "wpbc_deactivate-feedback-more-details" ] ) ) { |
| 124 |
$reason_text = sanitize_textarea_field( wp_unslash( $_POST[ "wpbc_deactivate-feedback-more-details" ] ) ); |
| 125 |
} |
| 126 |
|
| 127 |
if ( ( '' !== $reason_slug ) && ( '' === trim( $reason_text ) || '---' === $reason_text ) ) { |
| 128 |
wp_send_json_error( |
| 129 |
array( |
| 130 |
'message' => esc_html__( 'Please share more details before submitting.', 'booking' ), |
| 131 |
) |
| 132 |
); |
| 133 |
} |
| 134 |
|
| 135 |
$deactivation_data = array( |
| 136 |
'reason_text' => $reason_text, |
| 137 |
'reason_slug' => $reason_slug, |
| 138 |
'admin_email' => get_bloginfo( 'admin_email' ), |
| 139 |
'website_url' => esc_url_raw( get_bloginfo( 'url' ) ), |
| 140 |
'base_product' => is_plugin_active( 'booking-calendar-com/booking-calendar-com.php' ) ? 'booking-calendar-com/booking-calendar-com.php' : 'booking/wpdev-booking.php', |
| 141 |
); |
| 142 |
|
| 143 |
// TODO: Make in future API request. |
| 144 |
// $this->send_api_request( $deactivation_data ); |
| 145 |
|
| 146 |
wpbc_feedback_deactivation__send_email( $reason_slug, implode( " \n\n", $deactivation_data ) ); |
| 147 |
|
| 148 |
wp_send_json_success(); |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Sends an API request with deactivation data. |
| 153 |
* |
| 154 |
* @param array $deactivation_data Deactivation Data. |
| 155 |
* @return string The response body from the API request. |
| 156 |
*/ |
| 157 |
private function send_api_request( $deactivation_data ) { |
| 158 |
$headers = array( |
| 159 |
'user-agent' => 'BookingCalendar/' . WP_BK_VERSION_NUM . '; ' . get_bloginfo( 'url' ), |
| 160 |
); |
| 161 |
|
| 162 |
$response = wp_remote_post( |
| 163 |
self::FEEDBACK_URL, |
| 164 |
array( |
| 165 |
'method' => 'POST', |
| 166 |
'timeout' => 45, |
| 167 |
'redirection' => 5, |
| 168 |
'httpversion' => '1.0', |
| 169 |
'blocking' => true, |
| 170 |
'headers' => $headers, |
| 171 |
'body' => array( 'deactivation_data' => $deactivation_data ), |
| 172 |
) |
| 173 |
); |
| 174 |
return wp_remote_retrieve_body( $response ); |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Check if the current screen is the plugins screen and returns a boolean. |
| 179 |
* |
| 180 |
* @return boolean |
| 181 |
*/ |
| 182 |
private function is_plugins_screen() { |
| 183 |
return in_array( get_current_screen()->id, array( 'plugins', 'plugins-network' ), true ); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
new WPBC_Admin_Deactivation_Feedback(); |
| 188 |
endif; |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
/** |
| 194 |
* Send email with feedback |
| 195 |
* |
| 196 |
* @param $uninstall_slug |
| 197 |
* @param $feedback_description |
| 198 |
* |
| 199 |
* @return void |
| 200 |
*/ |
| 201 |
function wpbc_feedback_deactivation__send_email( $uninstall_slug, $feedback_description ) { |
| 202 |
|
| 203 |
$us_data = wp_get_current_user(); |
| 204 |
|
| 205 |
$fields_values = array(); |
| 206 |
$fields_values['from_email'] = get_option( 'admin_email' ); |
| 207 |
$fields_values['from_name'] = $us_data->display_name; |
| 208 |
$fields_values['from_name'] = wp_specialchars_decode( esc_html( stripslashes( $fields_values['from_name'] ) ), ENT_QUOTES ); |
| 209 |
$fields_values['from_email'] = sanitize_email( $fields_values['from_email'] ); |
| 210 |
|
| 211 |
$subject = 'WPBC Uninstall: ' . $uninstall_slug . ''; |
| 212 |
|
| 213 |
|
| 214 |
$message = ''; |
| 215 |
$message .= $feedback_description . "\n"; |
| 216 |
$message .= '=====' . "\n"; |
| 217 |
|
| 218 |
$cleaned_data_booking_feedback_arr = get_bk_option( 'booking_feedback__after_send' ); |
| 219 |
if ( ( ! empty( $cleaned_data_booking_feedback_arr ) ) && ( is_array( $cleaned_data_booking_feedback_arr ) ) ) { |
| 220 |
$feedback_description_setup = implode( "\n", $cleaned_data_booking_feedback_arr ); |
| 221 |
$message .= $feedback_description_setup . "\n"; |
| 222 |
$message .= '=====' . "\n"; |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
$message .="\n"; |
| 227 |
|
| 228 |
$message .= $fields_values['from_name'] . "\n"; |
| 229 |
$message .="\n"; |
| 230 |
$message .= '---------------------------------------------' . "\n"; |
| 231 |
|
| 232 |
$message .= 'Booking Calendar ' . wpbc_feedback_01_get_version() . "\n"; |
| 233 |
|
| 234 |
$how_old_info_arr = wpbc_get_info__about_how_old(); |
| 235 |
if ( ! empty( $how_old_info_arr ) ) { |
| 236 |
$message .= "\n"; |
| 237 |
$message .= 'From: ' . $how_old_info_arr['date_echo']; |
| 238 |
$message .= ' - ' . $how_old_info_arr['days'] . ' days ago.'; |
| 239 |
} |
| 240 |
|
| 241 |
$message .="\n"; |
| 242 |
$message .= '---------------------------------------------' . "\n"; |
| 243 |
$message .= '[' . date_i18n( get_bk_option( 'booking_date_format' ) ) . ' ' . date_i18n( get_bk_option( 'booking_time_format' ) ) . ']'. "\n"; |
| 244 |
$message .= home_url() ; |
| 245 |
|
| 246 |
|
| 247 |
$headers = ''; |
| 248 |
|
| 249 |
if ( ! empty( $fields_values['from_email'] ) ) { |
| 250 |
|
| 251 |
$headers .= 'From: ' . $fields_values['from_name'] . ' <' . $fields_values['from_email'] . '> ' . "\r\n"; |
| 252 |
} else { |
| 253 |
/* If we don't have an email from the input headers default to wordpress@$sitename |
| 254 |
* Some hosts will block outgoing mail from this address if it doesn't exist but |
| 255 |
* there's no easy alternative. Defaulting to admin_email might appear to be another |
| 256 |
* option but some hosts may refuse to relay mail from an unknown domain. See |
| 257 |
* https://core.trac.wordpress.org/ticket/5007. |
| 258 |
*/ |
| 259 |
} |
| 260 |
$headers .= 'Content-Type: ' . 'text/plain' . "\r\n" ; // 'text/html' |
| 261 |
|
| 262 |
$attachments = ''; |
| 263 |
|
| 264 |
|
| 265 |
$to = 'feedback_uninstall@wpbookingcalendar.com'; |
| 266 |
|
| 267 |
// debuge('In email', htmlentities($to), $subject, htmlentities($message), $headers, $attachments) ; |
| 268 |
// debuge( '$to, $subject, $message, $headers, $attachments',htmlspecialchars($to), htmlspecialchars($subject), htmlspecialchars($message), htmlspecialchars($headers), htmlspecialchars($attachments)); |
| 269 |
if ( wpbc_email_api_is_allow_send( true, '', '' ) ) { |
| 270 |
$return = wp_mail( $to, $subject, $message, $headers, $attachments ); |
| 271 |
} |
| 272 |
} |
| 273 |
|