Making WordPress.org

Changeset 10100


Ignore:
Timestamp:
07/27/2020 06:14:28 AM (5 years ago)
Author:
tellyworth
Message:

Plugin dir: include translation data in API response for block plugins.

This is needed for one potential solution to #50732-core. Might be reverted in future if a different solution is used.

Props tellyworth, ryelle, dd32.

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

Legend:

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

    r10098 r10100  
    245245                ];
    246246            }, $result['language_packs'] );
     247        }
     248
     249        // Include json translation data directly for block plugins, so it's immediately available on insert
     250        if ( !empty( $result['language_pack'] ) && !empty( $result['block_assets'] ) ) {
     251            foreach ( $result['block_assets'] as $file ) {
     252                if ( 'js' === pathinfo( $file, PATHINFO_EXTENSION ) ) {
     253                    // Look inside language pack zip files for a correctly names .json
     254                    foreach ( $result['language_pack'] as $lp ) {
     255                        $version_path = 'trunk' === $result['stable_tag'] ? 'trunk' : 'tags/' . $result['stable_tag'];
     256                        $_file = str_replace( "/{$version_path}/", '', $file );
     257                        $zip_path = '/nfs/rosetta/builds/plugins/' . $result['slug'] . '/' . $result['version'] . '/' . $lp['language'] . '.zip';
     258                        $json_file = $result['slug'] . '-' . $lp['language'] . '-' . md5( $_file ) . '.json';
     259                        if ( $fp = fopen( "zip://$zip_path#$json_file", 'r' ) ) {
     260                            $json = fread( $fp, 100 * KB_IN_BYTES ); // max 100kb
     261                            if ( $json && feof($fp) ) {
     262                                // Note that we're intentionally not decoding the json here, as we don't want to alter it by converting to and from PHP data types.
     263                                $result['block_translations'][ $lp['language'] ][ $file ] = $json;
     264                            }
     265                            fclose( $fp );
     266                        }
     267                    }
     268                }
     269            }
    247270        }
    248271
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/standalone/class-plugins-info-api-request.php

    r10098 r10100  
    106106        'blocks'               => true,
    107107        'block_assets'         => true,
     108        'block_translations'   => true,
    108109        'author_block_count'   => true,
    109110        'author_block_rating'  => true,
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/standalone/class-plugins-info-api.php

    r10098 r10100  
    178178                }
    179179            }
     180        }
     181
     182        // Only include block translation data for the selected locale.
     183        if ( ! empty( $response['block_translations'] ) ) {
     184            $response['block_translations'] = !empty( $response['block_translations'][ $request->locale ] ) ? $response['block_translations'][ $request->locale ] : [];
    180185        }
    181186
Note: See TracChangeset for help on using the changeset viewer.