/wp-content/plugins/loco-translate/src/data/Preferences.php

https://gitlab.com/campus-academy/krowkaramel · PHP · 181 lines · 91 code · 24 blank · 66 comment · 10 complexity · b61177e2db7fc2b8d40d3a2a810f0b06 MD5 · raw file

  1. <?php
  2. /**
  3. * Data object persisted as a WordPress user meta entry under the loco_prefs key
  4. *
  5. * @property string $credit Last-Translator credit, defaults to current display name
  6. * @property string[] $locales List of locales user wants to be restricted to seeing.
  7. */
  8. class Loco_data_Preferences extends Loco_data_Serializable {
  9. /**
  10. * User preference singletons
  11. * @var Loco_data_Preferences[]
  12. */
  13. private static $current = [];
  14. /**
  15. * ID of the currently operational user
  16. * @var int
  17. */
  18. private $user_id = 0;
  19. /**
  20. * Available options and their defaults
  21. * @var array
  22. */
  23. private static $defaults = [
  24. 'credit' => '',
  25. 'locales' => [],
  26. ];
  27. /**
  28. * Get current user's preferences
  29. * @return Loco_data_Preferences
  30. */
  31. public static function get(){
  32. $id = get_current_user_id();
  33. if( ! $id ){
  34. // allow null return only on command line. All web users must be logged in
  35. if( 'cli' === PHP_SAPI || defined('LOCO_TEST') ){
  36. return null;
  37. }
  38. throw new Exception( 'No current user' ); // @codeCoverageIgnore
  39. }
  40. if( isset(self::$current[$id]) ){
  41. return self::$current[$id];
  42. }
  43. $prefs = self::create($id);
  44. self::$current[$id] = $prefs;
  45. $prefs->fetch();
  46. return $prefs;
  47. }
  48. /**
  49. * Create default settings instance
  50. * @param int User ID
  51. * @return Loco_data_Preferences
  52. */
  53. public static function create( $id ){
  54. $prefs = new Loco_data_Preferences( self::$defaults );
  55. $prefs->user_id = $id;
  56. return $prefs;
  57. }
  58. /**
  59. * Destroy current user's preferences
  60. * @return void
  61. */
  62. public static function clear(){
  63. get_current_user_id() && self::get()->remove();
  64. }
  65. /**
  66. * Persist object in WordPress usermeta table
  67. * @return bool
  68. */
  69. public function persist(){
  70. return update_user_meta( $this->user_id, 'loco_prefs', $this->getSerializable() ) ? true : false;
  71. }
  72. /**
  73. * Retrieve and unserialize this object from WordPress usermeta table
  74. * @return bool whether object existed in cache
  75. */
  76. public function fetch(){
  77. $data = get_user_meta( $this->user_id, 'loco_prefs', true );
  78. // See comments in Loco_data_Settings
  79. if( is_array($data) ){
  80. $copy = new Loco_data_Preferences;
  81. $copy->setUnserialized($data);
  82. $data = $copy->getArrayCopy() + $this->getArrayCopy();
  83. $this->exchangeArray($data);
  84. $this->clean();
  85. return true;
  86. }
  87. return false;
  88. }
  89. /**
  90. * Delete usermeta entry from WordPress
  91. * return bool
  92. */
  93. public function remove(){
  94. $id = $this->user_id;
  95. self::$current[$id] = null;
  96. return delete_user_meta( $id, 'loco_prefs' );
  97. }
  98. /**
  99. * Populate all settings from raw postdata.
  100. * @param array
  101. * @return Loco_data_Preferences
  102. */
  103. public function populate( array $data ){
  104. // set all keys present in array
  105. foreach( $data as $prop => $value ){
  106. try {
  107. $this->offsetSet( $prop, $value );
  108. }
  109. catch( InvalidArgumentException $e ){
  110. // skipping invalid key
  111. }
  112. }
  113. return $this;
  114. }
  115. /**
  116. * {@inheritdoc}
  117. */
  118. public function offsetSet( $prop, $value ){
  119. $value = parent::cast($prop,$value,self::$defaults);
  120. parent::offsetSet( $prop, $value );
  121. }
  122. /**
  123. * Get default Last-Translator credit
  124. * @return string
  125. */
  126. public function default_credit(){
  127. $user = wp_get_current_user();
  128. $name = (string) $user->get('display_name');
  129. if( $user->get('user_login') === $name ){
  130. $name = '';
  131. }
  132. return $name;
  133. }
  134. /**
  135. * Check if user wants to know about this locale
  136. * @param Loco_Locale locale to match in whitelist
  137. * @return bool
  138. */
  139. public function has_locale( Loco_Locale $locale ){
  140. $haystack = $this->locales;
  141. if( $haystack ){
  142. foreach( $haystack as $tag ){
  143. $tag = strtolower($tag);
  144. // allow language wildcard. en_GB allowed by "en"
  145. if( $locale->lang === $tag ){
  146. return true;
  147. }
  148. // else normalize whitelist before comparison
  149. if( Loco_Locale::parse($tag)->__toString() === $locale->__toString() ){
  150. return true;
  151. }
  152. }
  153. return false;
  154. }
  155. return true;
  156. }
  157. }