| | 1 | <?php |
| | 2 | |
| | 3 | /** |
| | 4 | * Class providing a mechanism for users to submit feedback on posts. |
| | 5 | * |
| | 6 | * @package handbook |
| | 7 | */ |
| | 8 | class WPorg_Handbook_Feedback { |
| | 9 | /** |
| | 10 | * Initialize the Feedback module. |
| | 11 | */ |
| | 12 | static function init() { |
| | 13 | add_action( 'rest_api_init', array( __CLASS__, 'register_rest_endpoints' ) ); |
| | 14 | add_action( 'wp_print_styles', array( __CLASS__, 'print_styles' ) ); |
| | 15 | add_filter( 'the_content', array( __CLASS__, 'render_form' ) ); |
| | 16 | } |
| | 17 | |
| | 18 | /** |
| | 19 | * Register REST API endpoints. |
| | 20 | */ |
| | 21 | static public function register_rest_endpoints() { |
| | 22 | register_rest_route( |
| | 23 | 'wporg-handbook/v1', |
| | 24 | 'submit-feedback' , |
| | 25 | array( |
| | 26 | 'methods' => array( 'PUT' ), |
| | 27 | 'callback' => array( __CLASS__, 'handle_form_submission' ), |
| | 28 | ) ); |
| | 29 | } |
| | 30 | |
| | 31 | /** |
| | 32 | * Email feedback to the site admin. |
| | 33 | * |
| | 34 | * @param WP_REST_Request $request |
| | 35 | * |
| | 36 | * @return string|WP_Error |
| | 37 | */ |
| | 38 | static public function handle_form_submission( $request ) { |
| | 39 | $admin_email = get_option( 'admin_email' ); |
| | 40 | |
| | 41 | // Removing tags for formatting purposes, but still need to practice late-escaping for security purposes. |
| | 42 | $page_id = absint( $request->get_param( 'pageID' ) ); |
| | 43 | $page_title = strip_tags( get_the_title( $page_id ) ); |
| | 44 | $feedback = strip_tags( $request->get_param( 'message' ) ); |
| | 45 | $valid_submission = $page_id && ! empty( $feedback ); |
| | 46 | |
| | 47 | $response = new WP_Error( |
| | 48 | 'email_failed', |
| | 49 | sprintf( |
| | 50 | __( "I'm sorry, but your feedback could not be submitted, please send an email to %s instead.", 'wporg' ), |
| | 51 | $admin_email |
| | 52 | ) |
| | 53 | ); |
| | 54 | |
| | 55 | if ( ! $valid_submission ) { |
| | 56 | return $response; |
| | 57 | } |
| | 58 | |
| | 59 | $message = sprintf( |
| | 60 | // translators: %s is a post title. |
| | 61 | __( 'Somebody reported that "%s" did not help them, and suggested the following changes:', 'wporg' ) . "\n\n" . |
| | 62 | '----' . "\n\n" . |
| | 63 | '%s' . "\n\n" . |
| | 64 | '----' . "\n\n" . |
| | 65 | __( 'If you think those would be helpful changes, please edit the page to reflect them.', 'wporg' ) . "\n\n" . |
| | 66 | '%s', |
| | 67 | esc_html( $page_title ), |
| | 68 | esc_html( $feedback ), |
| | 69 | esc_url_raw( get_edit_post_link( $page_id, 'raw' ) ) |
| | 70 | ); |
| | 71 | |
| | 72 | $mail_sent = wp_mail( |
| | 73 | $admin_email, |
| | 74 | // translators: %s is a post title. |
| | 75 | sprintf( __( 'Feedback on "%s"', 'wporg' ), esc_html( $page_title ) ), |
| | 76 | $message |
| | 77 | ); |
| | 78 | |
| | 79 | if ( $mail_sent ) { |
| | 80 | $response = __( 'Thanks for your feedback!', 'wporg' ); |
| | 81 | } |
| | 82 | |
| | 83 | return $response; |
| | 84 | } |
| | 85 | |
| | 86 | /** |
| | 87 | * Print the styles needed for the feedback markup. |
| | 88 | */ |
| | 89 | static public function print_styles() { |
| | 90 | wp_enqueue_style( 'dashicons' ); |
| | 91 | |
| | 92 | ?> |
| | 93 | |
| | 94 | <!-- handbook/feedback --> |
| | 95 | <style> |
| | 96 | .handbook-feedback { |
| | 97 | } |
| | 98 | |
| | 99 | .feedback-vote { |
| | 100 | background-color: #B4B9BE; |
| | 101 | } |
| | 102 | |
| | 103 | .feedback-vote-helpful.has-voted { |
| | 104 | background-color: #46B450; |
| | 105 | } |
| | 106 | |
| | 107 | .feedback-vote-unhelpful.has-voted { |
| | 108 | background-color: #DC3232; |
| | 109 | } |
| | 110 | |
| | 111 | #feedback-message-container { |
| | 112 | margin-top: 10px; |
| | 113 | } |
| | 114 | |
| | 115 | #feedback-message-container input[type="submit"] { |
| | 116 | margin-top: 10px; |
| | 117 | } |
| | 118 | |
| | 119 | .handbook-spinner { |
| | 120 | background: url( <?php echo esc_url( admin_url( 'images/spinner.gif' ) ); ?> ) no-repeat; |
| | 121 | background-size: 20px 20px; |
| | 122 | display: inline-block; |
| | 123 | vertical-align: middle; |
| | 124 | opacity: 0.7; |
| | 125 | filter: alpha( opacity=70 ); |
| | 126 | width: 20px; |
| | 127 | height: 20px; |
| | 128 | margin: 0 0 0 5px; |
| | 129 | } |
| | 130 | |
| | 131 | .dashicons.feedback-success { |
| | 132 | color: #46B450; |
| | 133 | } |
| | 134 | |
| | 135 | .dashicons.feedback-error { |
| | 136 | color: #DC3232; |
| | 137 | } |
| | 138 | </style> |
| | 139 | |
| | 140 | <?php |
| | 141 | } |
| | 142 | |
| | 143 | /** |
| | 144 | * Append the feedback form to the post's content. |
| | 145 | * |
| | 146 | * @param string $content |
| | 147 | * |
| | 148 | * @return string |
| | 149 | */ |
| | 150 | static public function render_form( $content ) { |
| | 151 | if ( 'handbook' !== get_post_type() ) { |
| | 152 | return $content; |
| | 153 | } |
| | 154 | |
| | 155 | /* |
| | 156 | * Prevent this function from being called multiple times, because shortcodes in the page content apply |
| | 157 | * `the_content`, etc. |
| | 158 | */ |
| | 159 | remove_filter( 'the_content', array( __CLASS__, 'render_form' ) ); |
| | 160 | |
| | 161 | ob_start(); |
| | 162 | |
| | 163 | ?> |
| | 164 | |
| | 165 | <div class="handbook-feedback callout callout-info"> |
| | 166 | <?php esc_html_e( 'Did this page help you?', 'wporg' ); ?> |
| | 167 | |
| | 168 | <button class="feedback-vote feedback-vote-helpful"> |
| | 169 | <span class="dashicons dashicons-thumbs-up"> |
| | 170 | <span class="screen-reader-text"> |
| | 171 | <?php esc_html_e( 'Yes', 'wporg' ); ?> |
| | 172 | </span> |
| | 173 | </span> |
| | 174 | </button> |
| | 175 | |
| | 176 | <button class="feedback-vote feedback-vote-unhelpful"> |
| | 177 | <span class="dashicons dashicons-thumbs-down"> |
| | 178 | <span class="screen-reader-text"> |
| | 179 | <?php esc_html_e( 'No', 'wporg' ); ?> |
| | 180 | </span> |
| | 181 | </span> |
| | 182 | </button> |
| | 183 | |
| | 184 | <div id="feedback-message-container" style="display: none;"> |
| | 185 | <input id="handbook-page-id" type="hidden" value="<?php echo absint( get_the_ID() ); ?>" /> |
| | 186 | |
| | 187 | <label for="feedback-message"> |
| | 188 | <?php esc_html_e( 'How could it have been more helpful?', 'wporg' ); ?> |
| | 189 | </label> |
| | 190 | <textarea id="feedback-message" name="feedback-message"></textarea> |
| | 191 | |
| | 192 | <input id="submit-feedback" type="submit" value="Send Feedback"> |
| | 193 | </div> |
| | 194 | </div> |
| | 195 | |
| | 196 | <script id="tmpl-handbook-feedback-spinner" type="text/template"> |
| | 197 | <?php esc_html_e( 'Sending feedback...', 'wporg' ); ?> |
| | 198 | <span class="handbook-spinner"></span> |
| | 199 | </script> |
| | 200 | |
| | 201 | <script id="tmpl-handbook-feedback-submission-result" type="text/template"> |
| | 202 | <span class="dashicons dashicons-{{data.icon}} feedback-{{data.status}}"></span> |
| | 203 | |
| | 204 | {{data.message}} |
| | 205 | </script> |
| | 206 | |
| | 207 | <?php |
| | 208 | |
| | 209 | return $content . ob_get_clean(); |
| | 210 | } |
| | 211 | } |
| | 212 | |
| | 213 | WPorg_Handbook_Feedback::init(); |