add_action( 'admin_enqueue_scripts', $this, 'enqueue_assets' ); $loader->add_action( 'wp_ajax_' . self::ACTION, $this, 'send' ); } /** * The reasons offered. "field" names the extra input a reason asks for. * * @return array */ private function reasons() { return array( array( 'value' => 'temporary', 'label' => __( 'It is only a temporary deactivation', 'quicklyfly' ), ), array( 'value' => 'broke', 'label' => __( 'It broke something on my site', 'quicklyfly' ), 'field' => __( 'What broke? A page, a plugin or a theme name helps.', 'quicklyfly' ), ), array( 'value' => 'not-working', 'label' => __( 'I could not get it to work', 'quicklyfly' ), 'field' => __( 'What were you trying to do?', 'quicklyfly' ), ), array( 'value' => 'not-fast', 'label' => __( 'It did not make my site feel faster', 'quicklyfly' ), ), array( 'value' => 'better', 'label' => __( 'I found a better plugin', 'quicklyfly' ), 'field' => __( 'Which plugin?', 'quicklyfly' ), ), array( 'value' => 'missing', 'label' => __( 'A feature I need is missing', 'quicklyfly' ), 'field' => __( 'Which feature?', 'quicklyfly' ), ), array( 'value' => 'other', 'label' => __( 'Other', 'quicklyfly' ), 'field' => __( 'Tell us more', 'quicklyfly' ), ), ); } /** * Loads the form on the Plugins screen. * * @param string $hook_suffix Current admin screen. * @return void */ public function enqueue_assets( $hook_suffix ) { if ( 'plugins.php' !== $hook_suffix || ! current_user_can( self::CAPABILITY ) ) { return; } /** * Filters whether the deactivation feedback form is shown. * * @param bool $enabled Whether to show it. */ if ( ! apply_filters( 'quicklyfly_feedback_enabled', true ) ) { return; } wp_enqueue_style( 'quicklyfly-feedback', QUICKLYFLY_URL . 'assets/css/quicklyfly-feedback.css', array(), QUICKLYFLY_ASSET_VER ); wp_enqueue_script( 'quicklyfly-feedback', QUICKLYFLY_URL . 'assets/js/quicklyfly-feedback.js', array(), QUICKLYFLY_ASSET_VER, true ); wp_add_inline_script( 'quicklyfly-feedback', 'window.quicklyflyFeedback = ' . wp_json_encode( array( 'ajaxUrl' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( self::NONCE_ACTION ), 'plugin' => plugin_basename( QUICKLYFLY_FILE ), 'deactivateUrl' => wp_nonce_url( self_admin_url( 'plugins.php?action=deactivate&plugin=' . rawurlencode( plugin_basename( QUICKLYFLY_FILE ) ) ), 'deactivate-plugin_' . plugin_basename( QUICKLYFLY_FILE ) ), 'action' => self::ACTION, 'email' => QUICKLYFLY_FEEDBACK_EMAIL, 'reasons' => $this->reasons(), 'text' => array( 'title' => __( 'Sorry to see QuicklyFly go', 'quicklyfly' ), 'intro' => __( 'Would you tell us why you are deactivating it? It takes a few seconds and helps us fix what is wrong.', 'quicklyfly' ), 'email' => __( 'Your email (optional, only if you would like a reply)', 'quicklyfly' ), 'tech' => __( 'Include technical details (site address, WordPress, PHP and plugin versions)', 'quicklyfly' ), 'privacy' => __( 'Pressing "Submit & Deactivate" emails your answer to the QuicklyFly team. "Skip & Deactivate" sends nothing.', 'quicklyfly' ), 'submit' => __( 'Submit & Deactivate', 'quicklyfly' ), 'skip' => __( 'Skip & Deactivate', 'quicklyfly' ), 'cancel' => __( 'Cancel', 'quicklyfly' ), 'sending' => __( 'Sending…', 'quicklyfly' ), 'working' => __( 'Deactivating…', 'quicklyfly' ), 'pick' => __( 'Please choose a reason, or skip.', 'quicklyfly' ), 'closeLabel' => __( 'Close', 'quicklyfly' ), ), ) ) . ';', 'before' ); } /** * Emails the answer to the plugin author. * * @return void */ public function send() { check_ajax_referer( self::NONCE_ACTION, 'nonce' ); if ( ! current_user_can( self::CAPABILITY ) ) { wp_send_json_error( array( 'message' => __( 'You are not allowed to do this.', 'quicklyfly' ) ), 403 ); } if ( get_transient( 'quicklyfly_feedback_sent' ) ) { wp_send_json_success( array( 'sent' => false ) ); } $reason = isset( $_POST['reason'] ) ? sanitize_key( wp_unslash( $_POST['reason'] ) ) : ''; $details = isset( $_POST['details'] ) ? sanitize_textarea_field( wp_unslash( $_POST['details'] ) ) : ''; $email = isset( $_POST['email'] ) ? sanitize_email( wp_unslash( $_POST['email'] ) ) : ''; $tech = ! empty( $_POST['tech'] ); $labels = wp_list_pluck( $this->reasons(), 'label', 'value' ); if ( ! isset( $labels[ $reason ] ) ) { wp_send_json_error( array( 'message' => __( 'Please choose a reason.', 'quicklyfly' ) ), 400 ); } $lines = array( 'Reason: ' . $labels[ $reason ], 'Details: ' . ( '' !== $details ? mb_substr( $details, 0, 1000 ) : '-' ), 'Reply to: ' . ( '' !== $email ? $email : '-' ), ); if ( $tech ) { $lines[] = 'Site: ' . home_url(); $lines[] = 'QuicklyFly: ' . QUICKLYFLY_VERSION . ( QuicklyFly_License::is_pro() ? ' (Pro)' : ' (Free)' ); $lines[] = 'WordPress: ' . get_bloginfo( 'version' ); $lines[] = 'PHP: ' . PHP_VERSION; } $headers = array( 'Content-Type: text/plain; charset=UTF-8' ); if ( '' !== $email ) { $headers[] = 'Reply-To: ' . $email; } $sent = wp_mail( QUICKLYFLY_FEEDBACK_EMAIL, '[QuicklyFly] Deactivation feedback: ' . $reason, implode( "\n", $lines ), $headers ); set_transient( 'quicklyfly_feedback_sent', 1, MINUTE_IN_SECONDS ); wp_send_json_success( array( 'sent' => (bool) $sent ) ); } }