Making WordPress.org


Ignore:
Timestamp:
07/27/2020 06:14:28 AM (4 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.

File:
1 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
Note: See TracChangeset for help on using the changeset viewer.