Making WordPress.org


Ignore:
Timestamp:
08/08/2023 05:14:08 AM (16 months ago)
Author:
dd32
Message:

Plugin Directory: Allow plugins releases to be discarded instead of confirmed.

Sometimes a release should not be released, this allows discarding those releases to cease the warnings.

See #5352.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/api/routes/class-plugin-release-confirmation.php

    r12178 r12813  
    4545                ]
    4646            ],
    47             'permission_callback' => function( $request ) {
    48                 $plugin = Plugin_Directory::get_plugin_post( $request['plugin_slug'] );
    49 
    50                 return (
    51                     Release_Confirmation_Shortcode::can_access() &&
    52                     current_user_can( 'plugin_manage_releases', $plugin )
    53                 );
    54             },
     47            'permission_callback' => [ $this, 'permission_can_access_plugin' ],
     48        ] );
     49
     50        register_rest_route( 'plugins/v1', '/plugin/(?P<plugin_slug>[^/]+)/release-confirmation/(?P<plugin_tag>[^/]+)/discard', [
     51            'methods'             => \WP_REST_Server::READABLE, // TODO: This really should be a POST
     52            'callback'            => [ $this, 'discard_release' ],
     53            'args'                => [
     54                'plugin_slug' => [
     55                    'validate_callback' => [ $this, 'validate_plugin_slug_callback' ],
     56                ],
     57                'plugin_tag' => [
     58                    'validate_callback' => [ $this, 'validate_plugin_tag_callback' ],
     59                ]
     60            ],
     61            'permission_callback' => [ $this, 'permission_can_access_plugin' ],
    5562        ] );
    5663
     
    8794
    8895    /**
     96     * Validate that the user can manage releases for the given plugin.
     97     */
     98    public function permission_can_access_plugin( $request ) {
     99        $plugin = Plugin_Directory::get_plugin_post( $request['plugin_slug'] );
     100
     101        return (
     102            Release_Confirmation_Shortcode::can_access() &&
     103            current_user_can( 'plugin_manage_releases', $plugin )
     104        );
     105    }
     106
     107    /**
    89108     * Endpoint to self-close a plugin.
    90109     *
     
    154173        header( 'Location: ' . $result['location'] );
    155174
    156         if ( ! $release || ! empty( $release['confirmed'][ $user_login ] ) ) {
    157             // Already confirmed.
     175        if ( ! $release || ! empty( $release['confirmed'][ $user_login ] ) || ! empty( $release['discarded'] ) ) {
     176            // Already confirmed, or unable to be confirmed.
    158177            $result['confirmed'] = false;
    159178            return $result;
     
    166185        // Mark the release as confirmed if enough confirmations.
    167186        if ( count( $release['confirmations'] ) >= $release['confirmations_required'] ) {
    168             $release['confirmed'] = true;
    169             $result['fully_confirmed']     = true;
     187            $release['confirmed']      = true;
     188            $result['fully_confirmed'] = true;
    170189        }
    171190
     
    193212
    194213    /**
     214     * A simple endpoint to decline/discard a release.
     215     */
     216    public function discard_release( $request ) {
     217        $user_login = wp_get_current_user()->user_login;
     218        $plugin     = Plugin_Directory::get_plugin_post( $request['plugin_slug'] );
     219        $tag        = $request['plugin_tag'];
     220        $release    = Plugin_Directory::get_release( $plugin, $tag );
     221        $result     = [
     222            'location' => wp_get_referer() ?: home_url( '/developers/releases/' ),
     223        ];
     224        header( 'Location: ' . $result['location'] );
     225
     226        if ( ! $release || $release['confirmed'] || ! empty( $release['discarded'] ) ) {
     227            // Already confirmed, or other error encountered.
     228            $result['confirmed'] = false;
     229            return $result;
     230        }
     231
     232        // Record this user as discarding the release.
     233        $release['confirmed'] = false; // Already false, just noting it here explicitely.
     234        $release['discarded'] = [
     235            'user' => $user_login,
     236            'time' => time(),
     237        ];
     238
     239        Plugin_Directory::add_release( $plugin, $release );
     240
     241        return $result;
     242    }
     243
     244    /**
    195245     * Send a Access email
    196246     */
Note: See TracChangeset for help on using the changeset viewer.