[ Index ]

PHP Cross Reference of GlotPress

title

Body

[close]

/gp-includes/routes/ -> translation-set.php (source)

   1  <?php
   2  /**
   3   * Routes: GP_Route_Translation_Set class
   4   *
   5   * @package GlotPress
   6   * @subpackage Routes
   7   * @since 1.0.0
   8   */
   9  
  10  /**
  11   * Core class used to implement the translation set route.
  12   *
  13   * @since 1.0.0
  14   */
  15  class GP_Route_Translation_Set extends GP_Route_Main {
  16  	public function new_get() {
  17          $set             = new GP_Translation_Set();
  18          $set->project_id = gp_get( 'project_id' );
  19          $project         = $set->project_id ? GP::$project->get( $set->project_id ) : null;
  20          if ( $this->cannot_edit_set_and_redirect( $set ) ) {
  21              return;
  22          }
  23          $this->tmpl( 'translation-set-new', get_defined_vars() );
  24      }
  25  
  26  	public function new_post() {
  27          if ( $this->invalid_nonce_and_redirect( 'add-translation-set' ) ) {
  28              return;
  29          }
  30  
  31          $new_set = new GP_Translation_Set( gp_post( 'set', array() ) );
  32          if ( $this->cannot_edit_set_and_redirect( $new_set ) ) {
  33              return;
  34          }
  35          if ( $this->invalid_and_redirect( $new_set ) ) {
  36              return;
  37          }
  38          $created_set = GP::$translation_set->create_and_select( $new_set );
  39          if ( ! $created_set ) {
  40              $this->errors[] = __( 'Error in creating translation set!', 'glotpress' );
  41              $this->redirect( gp_url( '/sets/-new', array( 'project_id' => $new_set->project_id ) ) );
  42          } else {
  43              GP::$project->get( $created_set->project_id );
  44              $this->notices[] = __( 'The translation set was created!', 'glotpress' );
  45              $this->redirect( gp_url_project_locale( GP::$project->get( $created_set->project_id ), $created_set->locale, $created_set->slug ) );
  46          }
  47      }
  48  
  49  	public function single( $set_id ) {
  50          $items = $this->get_set_project_and_locale_from_set_id_or_404( $set_id );
  51          if ( ! $items ) {
  52              return;
  53          }
  54          list( $set, $project, ) = $items;
  55          $this->redirect( gp_url_project( $project, array( $set->locale, $set->slug ) ) );
  56      }
  57  
  58  	public function edit_get( $set_id ) {
  59          $items = $this->get_set_project_and_locale_from_set_id_or_404( $set_id );
  60          if ( ! $items ) {
  61              return;
  62          }
  63          list( $set, $project, $locale ) = $items;
  64          if ( $this->cannot_and_redirect( 'write', 'project', $set->project_id, gp_url_project( $project ) ) ) {
  65              return;
  66          }
  67          $url = gp_url_project( $project, gp_url_join( $set->locale, $set->slug ) );
  68          $this->tmpl( 'translation-set-edit', get_defined_vars() );
  69      }
  70  
  71      /**
  72       * Saves settings for a translation set and redirects back to the project locales page.
  73       *
  74       * @since 1.0.0
  75       *
  76       * @param int $set_id A translation set id to edit the settings of.
  77       */
  78  	public function edit_post( $set_id ) {
  79          if ( $this->invalid_nonce_and_redirect( 'edit-translation-set_' . $set_id ) ) {
  80              return;
  81          }
  82  
  83          $items = $this->get_set_project_and_locale_from_set_id_or_404( $set_id );
  84  
  85          if ( ! $items ) {
  86              return;
  87          }
  88  
  89          list( $set, ,  ) = $items;
  90  
  91          $new_set = new GP_Translation_Set( gp_post( 'set', array() ) );
  92  
  93          if ( $this->cannot_edit_set_and_redirect( $new_set ) ) {
  94              return;
  95          }
  96  
  97          if ( $this->invalid_and_redirect( $new_set, gp_url( '/sets/' . $set_id . '/-edit' ) ) ) {
  98              return;
  99          }
 100  
 101          if ( ! $set->update( $new_set ) ) {
 102              $this->errors[] = __( 'Error in updating translation set!', 'glotpress' );
 103              $this->redirect();
 104              return;
 105          }
 106  
 107          $project = GP::$project->get( $new_set->project_id );
 108  
 109          $this->notices[] = __( 'The translation set was updated!', 'glotpress' );
 110  
 111          $this->redirect( gp_url_project_locale( $project, $new_set->locale, $new_set->slug ) );
 112      }
 113  
 114      /**
 115       * Deletes a translation set.
 116       *
 117       * @since 2.0.0
 118       *
 119       * @param int $set_id The id of the translation set to delete.
 120       */
 121  	public function delete_post( $set_id ) {
 122          if ( $this->invalid_nonce_and_redirect( 'delete-translation-set_' . $set_id ) ) {
 123              return;
 124          }
 125  
 126          $items = $this->get_set_project_and_locale_from_set_id_or_404( $set_id );
 127          if ( ! $items ) {
 128              return;
 129          }
 130  
 131          list( $set, $project, $locale ) = $items;
 132          if ( $this->cannot_and_redirect( 'delete', 'project', $set->project_id, gp_url_project( $project ) ) ) {
 133              return;
 134          }
 135  
 136          if ( ! $set->delete() ) {
 137              $this->errors[] = __( 'Error deleting translation set!', 'glotpress' );
 138              $this->redirect();
 139              return;
 140          }
 141  
 142          $this->notices[] = __( 'The translation set was deleted!', 'glotpress' );
 143          $this->redirect( gp_url_project( $project ) );
 144      }
 145  
 146      /**
 147       * Displays the delete page for translations sets.
 148       *
 149       * @since 2.0.0
 150       *
 151       * @param int $set_id The id of the translation set to delete.
 152       */
 153  	public function delete_get( $set_id ) {
 154          $items = $this->get_set_project_and_locale_from_set_id_or_404( $set_id );
 155          if ( ! $items ) {
 156              return;
 157          }
 158  
 159          list( $set, $project, $locale ) = $items;
 160          if ( $this->cannot_and_redirect( 'delete', 'project', $set->project_id, gp_url_project( $project ) ) ) {
 161              return;
 162          }
 163  
 164          $url = gp_url_project( $project, gp_url_join( $set->locale, $set->slug ) );
 165          $this->tmpl( 'translation-set-delete', get_defined_vars() );
 166      }
 167  
 168      /**
 169       * Determines whether the current user can edit a translation set.
 170       *
 171       * @param GP_Translation_Set $set The translation set to edit.
 172       */
 173  	private function cannot_edit_set_and_redirect( $set ) {
 174          return $this->cannot_and_redirect( 'write', 'project', $set->project_id );
 175      }
 176  
 177  	private function get_set_project_and_locale_from_set_id_or_404( $set_id ) {
 178          $set = GP::$translation_set->get( $set_id );
 179          if ( ! $set ) {
 180              $this->die_with_404( array( 'title' => "Translation set wasn't found" ) );
 181              return;
 182          }
 183          $project = GP::$project->get( $set->project_id );
 184          if ( ! $project ) {
 185              $this->die_with_404( array( 'title' => "The project associated with this translation set wasn't found" ) );
 186              return;
 187          }
 188          $locale = $locale = GP_Locales::by_slug( $set->locale );
 189          if ( ! $locale ) {
 190              $this->die_with_404( array( 'title' => "The locale associated with this translation set wasn't found" ) );
 191              return;
 192          }
 193          return array( $set, $project, $locale );
 194      }
 195  }


Generated: Thu Mar 28 01:01:06 2024 Cross-referenced by PHPXref 0.7.1