Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update code
  • Loading branch information
swissspidy committed Sep 22, 2023
commit 323b2dcd34de92745d5be71120555bb9f4e0bf5e
67 changes: 50 additions & 17 deletions gp-includes/formats/format-php.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ function ( $translation ) {
);
}

return '<?php' . PHP_EOL . 'return ' . $this->var_export( $result, true ) . ';';
return '<?php' . PHP_EOL . 'return ' . $this->var_export( $result ) . ';';
}

/**
Expand All @@ -75,7 +75,7 @@ function ( $translation ) {
* @since 4.0.0
*
* @param string $file_name The name of the uploaded PHP file.
* @return Translations|bool The extracted originals on success, false on failure.
* @return false Always returns false, as this is not currently implemented.
*/
public function read_originals_from_file( $file_name ) {
// TODO: Either implement in a secure way or mark as unsupported.
Expand All @@ -89,10 +89,47 @@ public function read_originals_from_file( $file_name ) {
*
* @param string $file_name The name of the uploaded properties file.
* @param GP_Project $project Unused. The project object to read the translations into.
* @return Translations|bool The extracted translations on success, false on failure.
* @return false Always returns false, as this is not currently implemented.
*/
public function read_translations_from_file( $file_name, $project = null ) {
return $this->read_originals_from_file( $file_name );
// TODO: Either implement in a secure way or mark as unsupported.
return false;
}

/**
* Determines if the given array is a list.
*
* An array is considered a list if its keys consist of consecutive numbers from 0 to count($array)-1.
*
* Polyfill for array_is_list() in PHP 8.1.
*
* @see https://github.com/symfony/polyfill-php81/tree/main
*
* @since 4.0.0
*
* @codeCoverageIgnore
*
* @param array<mixed> $arr The array being evaluated.
* @return bool True if array is a list, false otherwise.
*/
private function array_is_list( array $arr ): bool {
if ( function_exists( 'array_is_list' ) ) {
return array_is_list( $arr );
}

if ( ( array() === $arr ) || ( array_values( $arr ) === $arr ) ) {
return true;
}

$next_key = -1;

foreach ( $arr as $k => $v ) {
if ( ++$next_key !== $k ) {
return false;
}
}

return true;
}

/**
Expand All @@ -103,27 +140,23 @@ public function read_translations_from_file( $file_name, $project = null ) {
*
* @since 4.0.0
*
* @param mixed $value The variable you want to export.
* @param bool $return_only Optional. Whether to return the variable representation instead of outputing it. Default false.
* @return string|void The variable representation or void.
* @param mixed $value The variable you want to export.
* @return string The variable representation.
*/
private function var_export( $value, $return_only = false ) {
private function var_export( $value ): string {
if ( ! is_array( $value ) ) {
return var_export( $value, $return_only );
return var_export( $value, true );
}

$entries = array();
foreach ( $value as $key => $val ) {
$entries[] = var_export( $key, true ) . '=>' . $this->var_export( $val, true );
}

$code = '[' . implode( ',', $entries ) . ']';
if ( $return_only ) {
return $code;
$is_list = $this->array_is_list( $value );

foreach ( $value as $key => $val ) {
$entries[] = $is_list ? $this->var_export( $val ) : var_export( $key, true ) . '=>' . $this->var_export( $val );
}

// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo $code;
return '[' . implode( ',', $entries ) . ']';
}
}

Expand Down