ActivationService.php
1 month ago
ActivationServiceProvider.php
1 year ago
DeactivationSurveyForm.php
6 months ago
DeactivationSurveyForm.php
299 lines
| 1 | <?php |
| 2 | namespace SureCart\Activation; |
| 3 | |
| 4 | /** |
| 5 | * Service for plugin deactivation survey form. |
| 6 | */ |
| 7 | class DeactivationSurveyForm { |
| 8 | /** |
| 9 | * Feedback URL. |
| 10 | * |
| 11 | * @var string |
| 12 | */ |
| 13 | private $feedback_api_endpoint = 'api/plugin-deactivate'; |
| 14 | |
| 15 | /** |
| 16 | * Bootstrap. |
| 17 | * |
| 18 | * @return void |
| 19 | */ |
| 20 | public function bootstrap() { |
| 21 | // handle ajax request. |
| 22 | add_action( 'wp_ajax_sc_plugin_deactivate_feedback', array( $this, 'sendPluginDeactivateFeedback' ) ); |
| 23 | |
| 24 | // show feedback form on plugins screen. |
| 25 | if ( $this->isPluginsScreen() ) { |
| 26 | add_action( 'admin_enqueue_scripts', array( $this, 'loadFormStyles' ) ); |
| 27 | add_action( 'admin_footer', array( $this, 'showFeedbackForm' ) ); |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Render feedback HTML on plugins.php admin page only. |
| 33 | * |
| 34 | * This function renders the feedback form HTML on the plugins.php admin page. |
| 35 | * It takes an optional string parameter $id for the form wrapper ID and an optional array parameter $args for customizing the form. |
| 36 | * |
| 37 | * @since 1.1.6 |
| 38 | * @return void |
| 39 | */ |
| 40 | public function showFeedbackForm() { |
| 41 | // Set default arguments for the feedback form. |
| 42 | $args = array( |
| 43 | 'source' => 'User Deactivation Survey', |
| 44 | 'popup_reasons' => $this->getDefaultReasons(), |
| 45 | 'popup_description' => __( 'If you have a moment, please share why you are deactivating the plugin.', 'surecart' ), |
| 46 | 'id' => 'deactivation-survey-surecart', |
| 47 | 'popup_logo' => esc_url( trailingslashit( plugin_dir_url( SURECART_PLUGIN_FILE ) ) . 'images/icon.svg' ), |
| 48 | 'plugin_slug' => 'surecart', |
| 49 | 'plugin_version' => \SureCart::plugin()->version(), |
| 50 | 'popup_title' => __( 'Quick Feedback', 'surecart' ), |
| 51 | 'support_url' => 'https://surecart.com/support/', |
| 52 | ); |
| 53 | |
| 54 | ?> |
| 55 | <div id="<?php echo esc_attr( $args['id'] ); ?>" class="uds-feedback-form--wrapper" style="display: none"> |
| 56 | <div class="uds-feedback-form--container"> |
| 57 | <div class="uds-form-header--wrapper"> |
| 58 | <div class="uds-form-title--icon-wrapper"> |
| 59 | <?php if ( ! empty( $args['popup_logo'] ) ) { ?> |
| 60 | <img class="uds-icon" src="<?php echo esc_url( $args['popup_logo'] ); ?>" title="<?php echo esc_attr( $args['plugin_slug'] ); ?> <?php echo esc_attr( __( 'Icon', 'surecart' ) ); ?>" /> |
| 61 | <?php } ?> |
| 62 | <h2 class="uds-title"><?php echo esc_html( $args['popup_title'] ); ?></h2> |
| 63 | </div> |
| 64 | <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="uds-close"> |
| 65 | <path stroke-linecap="round" stroke-linejoin="round" d="M6 18 18 6M6 6l12 12" /> |
| 66 | </svg> |
| 67 | </div> |
| 68 | <div class="uds-form-body--content"> |
| 69 | |
| 70 | <?php if ( ! empty( $args['popup_description'] ) ) { ?> |
| 71 | <p class="uds-form-description"><?php echo esc_html( $args['popup_description'] ); ?></p> |
| 72 | <?php } ?> |
| 73 | |
| 74 | <form class="sc-feedback-form" id="sc-feedback-form" method="post"> |
| 75 | <?php foreach ( $args['popup_reasons'] as $key => $value ) { ?> |
| 76 | <fieldset> |
| 77 | <div class="reason"> |
| 78 | <input type="radio" class="uds-reason-input" name="uds_reason_input" id="uds_reason_input_<?php echo esc_attr( $key ); ?>" value="<?php echo esc_attr( $key ); ?>" data-placeholder="<?php echo esc_attr( $value['placeholder'] ); ?>" data-show_cta="<?php echo esc_attr( $value['show_cta'] ); ?>" data-accept_feedback="<?php echo esc_attr( $value['accept_feedback'] ); ?>"> |
| 79 | <label class="uds-reason-label" for="uds_reason_input_<?php echo esc_attr( $key ); ?>"><?php echo esc_html( $value['label'] ); ?></label> |
| 80 | </div> |
| 81 | </fieldset> |
| 82 | <?php } ?> |
| 83 | |
| 84 | <fieldset> |
| 85 | <textarea class="uds-options-feedback hide" id="uds-options-feedback" rows="3" name="uds_options_feedback" placeholder="<?php echo esc_attr( __( 'Please tell us more details.', 'surecart' ) ); ?>"></textarea> |
| 86 | <?php |
| 87 | if ( ! empty( $args['support_url'] ) ) { |
| 88 | ?> |
| 89 | <p class="uds-option-feedback-cta hide"> |
| 90 | <?php |
| 91 | echo wp_kses_post( |
| 92 | sprintf( |
| 93 | /* translators: %1$s: link html start, %2$s: link html end*/ |
| 94 | __( 'Need help from our experts? %1$sClick here to contact us.%2$s', 'surecart' ), |
| 95 | '<a href="' . esc_url( $args['support_url'] ) . '" target="_blank">', |
| 96 | '</a>' |
| 97 | ) |
| 98 | ); |
| 99 | ?> |
| 100 | </p> |
| 101 | <?php } ?> |
| 102 | </fieldset> |
| 103 | |
| 104 | <div class="uds-feedback-form-sumbit--actions"> |
| 105 | <button class="button button-primary uds-feedback-submit" data-action="submit"><?php esc_html_e( 'Submit & Deactivate', 'surecart' ); ?></button> |
| 106 | <button class="button button-secondary uds-feedback-skip" data-action="skip"><?php esc_html_e( 'Skip & Deactivate', 'surecart' ); ?></button> |
| 107 | <input type="hidden" name="referer" value="<?php echo esc_url( get_site_url() ); ?>"> |
| 108 | <input type="hidden" name="version" value="<?php echo esc_attr( $args['plugin_version'] ); ?>"> |
| 109 | <input type="hidden" name="source" value="<?php echo esc_attr( $args['plugin_slug'] ); ?>"> |
| 110 | </div> |
| 111 | </form> |
| 112 | </div> |
| 113 | </div> |
| 114 | </div> |
| 115 | <?php |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Load form styles. |
| 120 | * |
| 121 | * This function loads the necessary styles for the feedback form. |
| 122 | * |
| 123 | * @since 1.1.6 |
| 124 | * @return void |
| 125 | */ |
| 126 | public function loadFormStyles() { |
| 127 | wp_enqueue_script( |
| 128 | 'sc-plugin-deactivation-feedback-script', |
| 129 | plugins_url( 'dist/scripts/plugin-deactivation-feedback.js', SURECART_PLUGIN_FILE ), |
| 130 | array( 'jquery' ), |
| 131 | \SureCart::plugin()->version(), |
| 132 | true |
| 133 | ); |
| 134 | |
| 135 | // Add localize JS. |
| 136 | wp_localize_script( |
| 137 | 'sc-plugin-deactivation-feedback-script', |
| 138 | 'scUdsVars', |
| 139 | array( |
| 140 | 'ajaxurl' => esc_url( admin_url( 'admin-ajax.php' ) ), |
| 141 | '_ajax_nonce' => wp_create_nonce( 'sc_plugin_deactivate_feedback' ), |
| 142 | '_current_theme' => function_exists( 'wp_get_theme' ) ? wp_get_theme()->get_template() : '', |
| 143 | '_plugin_slug' => array( 'surecart' ), |
| 144 | ) |
| 145 | ); |
| 146 | |
| 147 | wp_enqueue_style( 'sc-plugin-deactivation-feedback-style', plugins_url( 'dist/styles/plugin-deactivation-feedback.css', SURECART_PLUGIN_FILE ), array(), \SureCart::plugin()->version() ); |
| 148 | wp_style_add_data( 'sc-plugin-deactivation-feedback-style', 'rtl', 'replace' ); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Sends plugin deactivation feedback to the server. |
| 153 | * |
| 154 | * This function checks the user's permission and verifies the nonce for the request. |
| 155 | * If the checks pass, it sends the feedback data to the server for processing. |
| 156 | * |
| 157 | * @return void |
| 158 | */ |
| 159 | public function sendPluginDeactivateFeedback() { |
| 160 | /** |
| 161 | * Nonce verification |
| 162 | */ |
| 163 | if ( ! check_ajax_referer( 'sc_plugin_deactivate_feedback', 'security', false ) ) { |
| 164 | wp_send_json_error( array( 'message' => __( 'Nonce validation failed', 'surecart' ) ) ); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Check permission |
| 169 | */ |
| 170 | if ( ! current_user_can( 'delete_plugins' ) ) { |
| 171 | wp_send_json_error( array( 'message' => __( 'Sorry, you are not allowed to do this operation.', 'surecart' ) ) ); |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Get the feedback data. |
| 176 | */ |
| 177 | $feedback_data = array( |
| 178 | 'reason' => isset( $_POST['reason'] ) ? sanitize_text_field( wp_unslash( $_POST['reason'] ) ) : '', |
| 179 | 'feedback' => isset( $_POST['feedback'] ) ? sanitize_text_field( wp_unslash( $_POST['feedback'] ) ) : '', |
| 180 | 'domain_name' => isset( $_POST['referer'] ) ? sanitize_text_field( wp_unslash( $_POST['referer'] ) ) : '', |
| 181 | 'version' => isset( $_POST['version'] ) ? sanitize_text_field( wp_unslash( $_POST['version'] ) ) : '', |
| 182 | 'plugin' => isset( $_POST['source'] ) ? sanitize_text_field( wp_unslash( $_POST['source'] ) ) : '', |
| 183 | ); |
| 184 | |
| 185 | /** |
| 186 | * Send the feedback data to the server. |
| 187 | */ |
| 188 | wp_safe_remote_post( |
| 189 | $this->getApiUrl() . $this->feedback_api_endpoint, |
| 190 | array( |
| 191 | 'body' => wp_json_encode( $feedback_data ), |
| 192 | 'headers' => array( |
| 193 | 'Content-Type' => 'application/json', |
| 194 | 'Accept' => 'application/json', |
| 195 | ), |
| 196 | 'blocking' => false, // don't wait for the response. |
| 197 | ) |
| 198 | ); |
| 199 | |
| 200 | /** |
| 201 | * Send success response. |
| 202 | */ |
| 203 | wp_send_json_success(); |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Get the array of default reasons. |
| 208 | * |
| 209 | * @return array Default reasons. |
| 210 | */ |
| 211 | public function getDefaultReasons() { |
| 212 | return apply_filters( |
| 213 | 'uds_default_deactivation_reasons', |
| 214 | array( |
| 215 | 'temporary_deactivation' => array( |
| 216 | 'label' => esc_html__( 'This is a temporary deactivation for testing.', 'surecart' ), |
| 217 | 'placeholder' => esc_html__( 'How can we assist you?', 'surecart' ), |
| 218 | 'show_cta' => 'false', |
| 219 | 'accept_feedback' => 'false', |
| 220 | ), |
| 221 | 'plugin_not_working' => array( |
| 222 | 'label' => esc_html__( 'Something isn\'t working properly.', 'surecart' ), |
| 223 | 'placeholder' => esc_html__( 'Please tell us more about what went wrong?', 'surecart' ), |
| 224 | 'show_cta' => 'true', |
| 225 | 'accept_feedback' => 'true', |
| 226 | ), |
| 227 | 'found_better_plugin' => array( |
| 228 | 'label' => esc_html__( 'I found a better alternative.', 'surecart' ), |
| 229 | 'placeholder' => esc_html__( 'Could you please specify which solution?', 'surecart' ), |
| 230 | 'show_cta' => 'false', |
| 231 | 'accept_feedback' => 'true', |
| 232 | ), |
| 233 | 'missing_a_feature' => array( |
| 234 | 'label' => esc_html__( 'It\'s missing a specific feature.', 'surecart' ), |
| 235 | 'placeholder' => esc_html__( 'Please tell us more about the feature.', 'surecart' ), |
| 236 | 'show_cta' => 'false', |
| 237 | 'accept_feedback' => 'true', |
| 238 | ), |
| 239 | 'other' => array( |
| 240 | 'label' => esc_html__( 'Other', 'surecart' ), |
| 241 | 'placeholder' => esc_html__( 'Please tell us more details.', 'surecart' ), |
| 242 | 'show_cta' => 'false', |
| 243 | 'accept_feedback' => 'true', |
| 244 | ), |
| 245 | ) |
| 246 | ); |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Check if the current screen is allowed for the survey. |
| 251 | * |
| 252 | * This function checks if the current screen is one of the allowed screens for displaying the survey. |
| 253 | * It uses the `get_current_screen` function to get the current screen information and compares it with the list of allowed screens. |
| 254 | * |
| 255 | * The function is accurate for identifying the main plugins screen, but doesn't account for: |
| 256 | * - Network admin plugins screen (plugins.php in multisite) |
| 257 | * - AJAX requests that might be triggered from the plugins screen |
| 258 | * - Early admin page loads where get_current_screen() might not be available yet |
| 259 | * |
| 260 | * @return bool True if the current screen is allowed, false otherwise. |
| 261 | */ |
| 262 | public function isPluginsScreen() { |
| 263 | // Check if we are in the admin area. |
| 264 | if ( ! is_admin() ) { |
| 265 | return false; |
| 266 | } |
| 267 | |
| 268 | // Handle case where function is called before get_current_screen() is available. |
| 269 | if ( ! function_exists( 'get_current_screen' ) ) { |
| 270 | // Alternative check: look at the current admin page URL. |
| 271 | global $pagenow; |
| 272 | return 'plugins.php' === $pagenow; |
| 273 | } |
| 274 | |
| 275 | // Get the current screen. |
| 276 | $current_screen = get_current_screen(); |
| 277 | |
| 278 | // Check if $current_screen is a valid object before accessing its properties. |
| 279 | if ( ! is_object( $current_screen ) ) { |
| 280 | return false; // Return false if current screen is not valid. |
| 281 | } |
| 282 | |
| 283 | // Get the current screen ID. |
| 284 | $screen_id = $current_screen->id; |
| 285 | |
| 286 | // Check for both regular and network admin plugins screens. |
| 287 | return ! empty( $screen_id ) && ( 'plugins' === $screen_id || 'plugins-network' === $screen_id ); |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * Get API URL for sending analytics. |
| 292 | * |
| 293 | * @return string API URL. |
| 294 | */ |
| 295 | public function getApiUrl() { |
| 296 | return defined( 'BSF_ANALYTICS_API_BASE_URL' ) ? BSF_ANALYTICS_API_BASE_URL : 'https://analytics.brainstormforce.com/'; |
| 297 | } |
| 298 | } |
| 299 |