Making WordPress.org

Changeset 12933


Ignore:
Timestamp:
10/06/2023 02:09:21 AM (13 months ago)
Author:
tellyworth
Message:

Plugins: add opt-out toggle for Live Preview button

This allows plugin committers to disable the Live Preview button for specific plugins. It adds a button to the Danger Zone section on the Advanced view of the plugin page.

See #7251

Location:
sites/trunk/wordpress.org/public_html/wp-content
Files:
1 added
4 edited

Legend:

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

    r12617 r12933  
    3434        new Routes\Plugin_Self_Close();
    3535        new Routes\Plugin_Self_Transfer();
     36        new Routes\Plugin_Self_Toggle_Preview();
    3637        new Routes\Plugin_Release_Confirmation();
    3738        new Routes\Plugin_E2E_Callback();
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/class-template.php

    r12931 r12933  
    794794            array( '_wpnonce' => wp_create_nonce( 'wp_rest' ) ),
    795795            home_url( 'wp-json/plugins/v1/plugin/' . $post->post_name . '/self-transfer' )
     796        );
     797    }
     798
     799    /**
     800     * Generates a link to toggle the Live Preview button.
     801     *
     802     * @param int|\WP_Post|null $post Optional. Post ID or post object. Defaults to global $post.
     803     * @return string URL to toggle status.
     804     */
     805    public static function get_self_toggle_preview_link( $post = null ) {
     806        $post = get_post( $post );
     807
     808        return add_query_arg(
     809            array( '_wpnonce' => wp_create_nonce( 'wp_rest' ) ),
     810            home_url( 'wp-json/plugins/v1/plugin/' . $post->post_name . '/self-toggle-preview' )
    796811        );
    797812    }
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-plugins/inc/template-tags.php

    r12816 r12933  
    520520        // Output the self close button.
    521521        the_plugin_self_close_button();
     522
     523        // Output the toggle preview button.
     524        the_plugin_self_toggle_preview_button();
    522525    }
    523526
     
    554557        // Translators: %s is the plugin name, as defined by the plugin itself.
    555558        echo '<p><input class="button" type="submit" value="' . esc_attr( sprintf( __( 'I understand, please close %s.', 'wporg-plugins' ), get_the_title() ) ) . '" /></p>';
     559        echo '</form>';
     560    }
     561}
     562
     563/**
     564 * Displays a form for plugin committers to toggle the Live Preview button.
     565 */
     566function the_plugin_self_toggle_preview_button() {
     567    $post            = get_post();
     568    $toggle_link     = Template::get_self_toggle_preview_link( $post );
     569
     570    if ( ! current_user_can( 'plugin_self_close', $post ) ) {
     571        return;
     572    }
     573
     574    echo '<h4>' . esc_html__( 'Toggle Live Preview', 'wporg-plugins' ) . '</h4>';
     575    $preview_status = get_post_meta( $post->ID, '_no_preview', true ) ? 'disabled' : 'enabled';
     576    if ( 'enabled' === $preview_status ) {
     577        echo '<p>' . esc_html__( 'The Live Preview link to Playground is currently enabled. Use the toggle button to disable it.', 'wporg-plugins' ) . '</p>';
     578    } else {
     579        echo '<p>' . esc_html__( 'The Live Preview link to Playground is currently disabled. Use the toggle button to enable it.', 'wporg-plugins' ) . '</p>';
     580    }
     581
     582    echo '<div class="plugin-notice notice notice-warning notice-alt"><p>';
     583    _e( '<strong>Note:</strong> This only affects the availability of the Live Preview button on the plugin page. It does not prevent a plugin from running in the Playground.', 'wporg-plugins' );
     584    echo '</p></div>';
     585
     586    if ( $toggle_link ) {
     587        echo '<form method="POST" action="' . esc_url( $toggle_link ) . '" onsubmit="return confirm( jQuery(this).prev(\'.notice\').text() );">';
     588        // Translators: %s is the plugin name, as defined by the plugin itself.
     589        echo '<p><input class="button" type="submit" value="' . esc_attr( sprintf( __( 'Please toggle the Live Preview link for %s', 'wporg-plugins' ), get_the_title() ) ) . '" /></p>';
    556590        echo '</form>';
    557591    }
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-plugins/template-parts/plugin-single.php

    r12931 r12933  
    4141            <?php if ( 'publish' === get_post_status() || current_user_can( 'plugin_admin_view', $post ) ) : ?>
    4242                <a class="plugin-download button download-button button-large" href="<?php echo esc_url( Template::download_link() ); ?>"><?php esc_html_e( 'Download', 'wporg-plugins' ); ?></a>
    43                 <?php if ( !Template::is_plugin_outdated( $post ) ) : ?>
     43                <?php if ( !Template::is_plugin_outdated( $post ) && !get_post_meta( $post->ID, '_no_preview', true ) ) : ?>
    4444                    <a class="plugin-download button download-button button-large" href="<?php echo esc_url( Template::preview_link() ); ?>"><?php esc_html_e( 'Live Preview', 'wporg-plugins' ); ?></a>
    4545                <?php endif; ?>
Note: See TracChangeset for help on using the changeset viewer.