'It\'s a temporary deactivation', 'no_longer_need' => 'I no longer need the plugin', 'found_better' => 'I found a better plugin', 'not_working' => 'It\'s not working / broke my site', 'couldnt_configure' => 'I couldn\'t get the plugin to work', 'missing_feature' => 'Missing a feature I need', 'other' => 'Other', ]; /** * Deactivating is the act being surveyed, so it is also the capability. * * Deliberately NOT the base class's `delete_posts`: an author who can delete a * post has no business filing a deactivation reason, and deliberately not a * connected-account check either — someone deactivating over a broken or * expired connection is exactly the feedback worth having. * * @param WP_REST_Request $request * @return bool */ public function permission_check( WP_REST_Request $request ) { $this->request = $request; return current_user_can( 'deactivate_plugins' ); } public function register_routes() { $this->post( $this->endpoint, [ $this, 'submit' ] ); } /** * The reason list, shaped for the JS dropdown. * * @return array */ public static function get_reasons_for_js() { $reasons = []; foreach ( self::REASONS as $value => $label ) { $reasons[] = [ 'value' => $value, // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteral -- constant-fed, one entry per REASONS key. 'label' => __( $label, 'templately' ), ]; } return $reasons; } /** * Forward the reason to the cloud. */ public function submit() { $reason = $this->get_param( 'reason', '', 'sanitize_key' ); if ( ! array_key_exists( $reason, self::REASONS ) ) { return $this->error( 'invalid_reason', __( 'Unknown deactivation reason.', 'templately' ), $this->endpoint, 400 ); } // sanitize_textarea_field, not the base sanitize_text_field: this is the one // free-text field in the payload and newlines are the whole point of it. $message = $this->get_param( 'message', '', 'sanitize_textarea_field' ); $response = Helper::make_api_post_request( 'v2/deactivation-feedback', [ 'reason' => $reason, 'message' => $message, ] ); if ( is_wp_error( $response ) ) { return $this->error( 'api_error', $response->get_error_message(), $this->endpoint, 500 ); } return $this->success( [] ); } }