Making WordPress.org

Changeset 14075


Ignore:
Timestamp:
09/24/2024 07:12:05 AM (23 months ago)
Author:
tellyworth
Message:

Plugin Dir: use locale for Live Preview button.

This carries the user's current locale through to Playground when using the Live Preview button in the plugin directory. A setSiteLanguage step is automatically added to the plugin's blueprint.json file if not already present.

See #7769.

Location:
sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory
Files:
2 edited

Legend:

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

    r13335 r14075  
    4444                }
    4545
    46         $blueprints = get_post_meta( $plugin->ID, 'assets_blueprints', true );
    47         // Note: for now, only use a file called `blueprint.json`.
     46                $blueprints = get_post_meta( $plugin->ID, 'assets_blueprints', true );
     47                // Note: for now, only use a file called `blueprint.json`.
    4848                if ( !isset( $blueprints['blueprint.json'] ) ) {
    4949                        return new \WP_Error( 'no_blueprint', 'File not found', array( 'status' => 404 ) );
    50         }
    51         $blueprint = $blueprints['blueprint.json'];
    52         if ( !$blueprint || !isset( $blueprint['contents'] ) || !is_string( $blueprint['contents'] ) ) {
     50                }
     51                $blueprint = $blueprints['blueprint.json'];
     52                if ( !$blueprint || !isset( $blueprint['contents'] ) || !is_string( $blueprint['contents'] ) ) {
    5353                        return new \WP_Error( 'invalid_blueprint', 'Invalid file', array( 'status' => 500 ) );
    54         }
     54                }
     55
     56                if ( $request->get_param('lang') && 'en' !== strtolower( substr( $request->get_param('lang'), 0, 2 ) ) ) {
     57                        // Check if there's already a setSiteLanguage step
     58                        $blueprint_data = json_decode( $blueprint['contents'] );
     59                        if ( isset( $blueprint_data->steps ) ) {
     60                                if ( !in_array( 'setSiteLanguage', wp_list_pluck( $blueprint_data->steps, 'step' ) ) ) {
     61                                        // Add setSiteLanguage as the final step
     62                                        array_push( $blueprint_data->steps,
     63                                                (object)[
     64                                                        'step' => 'setSiteLanguage',
     65                                                        'language' => sanitize_text_field( $request->get_param('lang') )
     66                                                ]
     67                                        );
     68                                }
     69                        } else {
     70                                // No steps in blueprint, so create one
     71                                $blueprint_data->steps = [
     72                                        (object)[
     73                                                'step' => 'setSiteLanguage',
     74                                                'language' => sanitize_text_field( $request->get_param('lang') )
     75                                        ]
     76                                ];
     77                        }
     78                        header( 'Access-Control-Allow-Origin: *' );
     79                        return $blueprint_data;
     80                }
    5581
    5682                // Configure this elsewhere?
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/class-template.php

    r13673 r14075  
    752752         * @return false|string The preview url. False if no preview is configured.
    753753         */
    754         public static function preview_link( $post = null ) {
     754        public static function preview_link( $post = null, $locale = null ) {
    755755                $post = get_post( $post );
    756756
     
    762762                $blueprint = $blueprints['blueprint.json'];
    763763
    764                 return sprintf( 'https://playground.wordpress.net/?plugin=%s&blueprint-url=%s', esc_attr($post->post_name), esc_attr($blueprint['url'] ) );
     764                $blueprint_url = $blueprint['url'];
     765                $locale = $locale ?? get_locale();
     766                $blueprint_url = add_query_arg( 'lang', $locale, $blueprint_url );
     767
     768                return sprintf( 'https://playground.wordpress.net/?plugin=%s&blueprint-url=%s', esc_attr($post->post_name), rawurlencode($blueprint_url) );
    765769        }
    766770
Note: See TracChangeset for help on using the changeset viewer.