wp/wp-includes/l10n/class-wp-translation-file-php.php
changeset 21 48c4eec2b7e6
equal deleted inserted replaced
20:7b1b88e27a20 21:48c4eec2b7e6
       
     1 <?php
       
     2 /**
       
     3  * I18N: WP_Translation_File_PHP class.
       
     4  *
       
     5  * @package WordPress
       
     6  * @subpackage I18N
       
     7  * @since 6.5.0
       
     8  */
       
     9 
       
    10 /**
       
    11  * Class WP_Translation_File_PHP.
       
    12  *
       
    13  * @since 6.5.0
       
    14  */
       
    15 class WP_Translation_File_PHP extends WP_Translation_File {
       
    16 	/**
       
    17 	 * Parses the file.
       
    18 	 *
       
    19 	 * @since 6.5.0
       
    20 	 */
       
    21 	protected function parse_file() {
       
    22 		$this->parsed = true;
       
    23 
       
    24 		$result = include $this->file;
       
    25 		if ( ! $result || ! is_array( $result ) ) {
       
    26 			$this->error = 'Invalid data';
       
    27 			return;
       
    28 		}
       
    29 
       
    30 		if ( isset( $result['messages'] ) && is_array( $result['messages'] ) ) {
       
    31 			foreach ( $result['messages'] as $original => $translation ) {
       
    32 				$this->entries[ (string) $original ] = $translation;
       
    33 			}
       
    34 			unset( $result['messages'] );
       
    35 		}
       
    36 
       
    37 		$this->headers = array_change_key_case( $result );
       
    38 	}
       
    39 
       
    40 	/**
       
    41 	 * Exports translation contents as a string.
       
    42 	 *
       
    43 	 * @since 6.5.0
       
    44 	 *
       
    45 	 * @return string Translation file contents.
       
    46 	 */
       
    47 	public function export(): string {
       
    48 		$data = array_merge( $this->headers, array( 'messages' => $this->entries ) );
       
    49 
       
    50 		return '<?php' . PHP_EOL . 'return ' . $this->var_export( $data ) . ';' . PHP_EOL;
       
    51 	}
       
    52 
       
    53 	/**
       
    54 	 * Outputs or returns a parsable string representation of a variable.
       
    55 	 *
       
    56 	 * Like {@see var_export()} but "minified", using short array syntax
       
    57 	 * and no newlines.
       
    58 	 *
       
    59 	 * @since 6.5.0
       
    60 	 *
       
    61 	 * @param mixed $value The variable you want to export.
       
    62 	 * @return string The variable representation.
       
    63 	 */
       
    64 	private function var_export( $value ): string {
       
    65 		if ( ! is_array( $value ) ) {
       
    66 			return var_export( $value, true );
       
    67 		}
       
    68 
       
    69 		$entries = array();
       
    70 
       
    71 		$is_list = array_is_list( $value );
       
    72 
       
    73 		foreach ( $value as $key => $val ) {
       
    74 			$entries[] = $is_list ? $this->var_export( $val ) : var_export( $key, true ) . '=>' . $this->var_export( $val );
       
    75 		}
       
    76 
       
    77 		return '[' . implode( ',', $entries ) . ']';
       
    78 	}
       
    79 }