Making WordPress.org


Ignore:
Timestamp:
03/14/2024 05:40:44 AM (13 months ago)
Author:
tellyworth
Message:

Plugin directory: prompt plugin developers to create a blueprint for previews

This adds a notice, visible only to a plugin's developers, prompting them to test and create a blueprint.json file so users can run a Live Preview of their plugin in the WordPress Playground. The notice can be dismissed.

See #7487.

File:
1 edited

Legend:

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

    r13310 r13335  
    774774    public static function preview_link_zip( $slug, $attachment_id, $type = null ) {
    775775
    776         $zip_hash = self::preview_link_hash( $attachment_id );
     776        $file = get_attached_file( $attachment_id );
     777        $zip_hash = self::preview_link_hash( $file );
    777778        if ( !$zip_hash ) {
    778779            return false;
     
    788789
    789790    /**
     791     * Generate a live preview (playground) link for a published plugin that does not yet have a custom blueprint. Needed for developer testing.
     792     *
     793     * @param string $slug            The slug of the plugin post.
     794     * @param int $download_link      The URL of the zip download for the plugin.
     795     * @param bool $blueprint_only    False will return a full preview URL. True will return only a blueprint URL.
     796     * @return false|string           The preview or blueprint URL.
     797     */
     798    public static function preview_link_developer( $slug, $download_link, $blueprint_only = false ) {
     799
     800        $url_hash = self::preview_link_hash( $download_link );
     801        if ( !$url_hash ) {
     802            return false;
     803        }
     804        $dev_blueprint = sprintf( 'https://wordpress.org/plugins/wp-json/plugins/v1/plugin/%s/blueprint.json?url_hash=%s', esc_attr( $slug ), esc_attr( $url_hash ) );
     805        if ( $blueprint_only ) {
     806            return $dev_blueprint;
     807        }
     808        $url_preview = add_query_arg( 'blueprint-url', urlencode($dev_blueprint), 'https://playground.wordpress.net/' );
     809
     810        return $url_preview;
     811    }
     812
     813    /**
    790814     * Return a time-dependent variable for zip preview links.
    791815     *
     
    800824     * Return a nonce-style hash for zip preview links.
    801825     *
    802      * @param int $attachment_id      The ID of the attachment post corresponding to a plugin zip file.
     826     * @param string $zip_file        The filesystem path or URL of the zip file.
    803827     * @param int $tick_offest        Number to subtract from the nonce tick. Use both 0 and -1 to verify older nonces.
    804828     * @return false|string           The hash as a hex string; or false if the attachment ID is invalid.
    805829     */
    806     public static function preview_link_hash( $attachment_id, $tick_offset = 0 ) {
    807         $file = get_attached_file( $attachment_id );
    808         if ( !$file ) {
     830    public static function preview_link_hash( $zip_file, $tick_offset = 0 ) {
     831        if ( !$zip_file ) {
    809832            return false;
    810833        }
    811834        $tick = self::preview_link_tick() - $tick_offset;
    812         return wp_hash( $tick . '|' . $file, 'nonce' );
     835        return wp_hash( $tick . '|' . $zip_file, 'nonce' );
    813836    }
    814837
     
    912935        return add_query_arg(
    913936            array( '_wpnonce' => wp_create_nonce( 'wp_rest' ) ),
     937            home_url( 'wp-json/plugins/v1/plugin/' . $post->post_name . '/self-toggle-preview' )
     938        );
     939    }
     940
     941    /**
     942     * Generates a link to dismiss a missing blueprint notice.
     943     *
     944     * @param int|\WP_Post|null $post Optional. Post ID or post object. Defaults to global $post.
     945     * @return string URL to toggle status.
     946     */
     947    public static function get_self_dismiss_blueprint_notice_link( $post = null ) {
     948        $post = get_post( $post );
     949
     950        return add_query_arg(
     951            array( '_wpnonce' => wp_create_nonce( 'wp_rest' ), 'dismiss' => 1 ),
    914952            home_url( 'wp-json/plugins/v1/plugin/' . $post->post_name . '/self-toggle-preview' )
    915953        );
Note: See TracChangeset for help on using the changeset viewer.