[ Index ]

PHP Cross Reference of GlotPress

title

Body

[close]

/gp-includes/formats/ -> format-json.php (source)

   1  <?php
   2  /**
   3   * GlotPress Format JSON class
   4   *
   5   * @since 2.3.0
   6   *
   7   * @package GlotPress
   8   */
   9  
  10  /**
  11   * Format class used to support JSON file format.
  12   *
  13   * @since 2.3.0
  14   */
  15  class GP_Format_JSON extends GP_Format {
  16      /**
  17       * Name of file format, used in file format dropdowns.
  18       *
  19       * @since 2.3.0
  20       *
  21       * @var string
  22       */
  23      public $name = 'JSON (.json)';
  24  
  25      /**
  26       * File extension of the file format, used to autodetect formats and when creating the output file names.
  27       *
  28       * @since 2.3.0
  29       *
  30       * @var string
  31       */
  32      public $extension = 'json';
  33  
  34      /**
  35       * Generates a string the contains the $entries to export in the JSON file format.
  36       *
  37       * @since 2.3.0
  38       *
  39       * @param GP_Project         $project         The project the strings are being exported for, not used
  40       *                                            in this format but part of the scaffold of the parent object.
  41       * @param GP_Locale          $locale          The locale object the strings are being exported for. not used
  42       *                                            in this format but part of the scaffold of the parent object.
  43       * @param GP_Translation_Set $translation_set The locale object the strings are being
  44       *                                            exported for. not used in this format but part
  45       *                                            of the scaffold of the parent object.
  46       * @param GP_Translation     $entries         The entries to export.
  47       * @return string The exported JSON string.
  48       */
  49  	public function print_exported_file( $project, $locale, $translation_set, $entries ) {
  50          $result = array();
  51  
  52          /* @var Translation_Entry $entry */
  53          foreach ( $entries as $entry ) {
  54              $key = $entry->context ? $entry->context . chr( 4 ) . $entry->singular : $entry->singular;
  55  
  56              $result[ $key ] = array_filter( $entry->translations, function ( $translation ) {
  57                  return null !== $translation;
  58              } );
  59          }
  60  
  61          /**
  62           * Filter whether the exported JSON should be pretty printed.
  63           *
  64           * @since 2.3.0
  65           *
  66           * @param bool $pretty_print Whether pretty print should be enabled or not. Default false.
  67           */
  68          $pretty_print = apply_filters( 'gp_json_export_pretty_print', false );
  69  
  70          return wp_json_encode( $result, ( true === $pretty_print ) ? JSON_PRETTY_PRINT : 0 );
  71      }
  72  
  73      /**
  74       * Reads a set of original strings from a JSON file.
  75       *
  76       * @since 2.3.0
  77       *
  78       * @param string $file_name The name of the uploaded JSON file.
  79       * @return Translations|bool The extracted originals on success, false on failure.
  80       */
  81  	public function read_originals_from_file( $file_name ) {
  82          $json = $this->decode_json_file( $file_name );
  83  
  84          if ( ! $json ) {
  85              return false;
  86          }
  87  
  88          $entries = new Translations();
  89  
  90          foreach ( $json as $key => $value ) {
  91              if ( '' === $key ) {
  92                  continue;
  93              }
  94  
  95              $args = array(
  96                  'singular' => $key,
  97              );
  98  
  99              if ( false !== strpos( $key, chr( 4 ) ) ) {
 100                  $key              = explode( chr( 4 ), $key );
 101                  $args['context']  = $key[0];
 102                  $args['singular'] = $key[1];
 103              }
 104  
 105              $value = (array) $value;
 106  
 107              if ( isset( $value[0] ) ) {
 108                  $args['translations'] = $value[0];
 109              }
 110  
 111              if ( isset( $value[1] ) ) {
 112                  $args['plural'] = $value[1];
 113              }
 114  
 115              $entries->add_entry( new Translation_Entry( $args ) );
 116          }
 117  
 118          return $entries;
 119      }
 120  
 121      /**
 122       * Reads a set of translations from a JSON file.
 123       *
 124       * @since 2.3.0
 125       *
 126       * @param string     $file_name The name of the uploaded properties file.
 127       * @param GP_Project $project   Unused. The project object to read the translations into.
 128       * @return Translations|bool The extracted translations on success, false on failure.
 129       */
 130  	public function read_translations_from_file( $file_name, $project = null ) {
 131          return $this->read_originals_from_file( $file_name );
 132      }
 133  
 134      /**
 135       * Loads a given JSON file and decodes its content.
 136       *
 137       * @since 2.3.0
 138       *
 139       * @param string $file_name The name of the JSON file to parse.
 140       * @return array|false The encoded value or false on failure.
 141       */
 142  	protected function decode_json_file( $file_name ) {
 143          if ( ! file_exists( $file_name ) ) {
 144              return false;
 145          }
 146  
 147          $file = file_get_contents( $file_name );
 148  
 149          if ( ! $file ) {
 150              return false;
 151          }
 152  
 153          $json = json_decode( $file, true );
 154  
 155          if ( null === $json ) {
 156              return false;
 157          }
 158  
 159          return $json;
 160      }
 161  }
 162  
 163  GP::$formats['json'] = new GP_Format_JSON();


Generated: Mon Nov 18 01:01:56 2019 Cross-referenced by PHPXref 0.7.1