'', 'gen_hash' => false, 'use_fuzzy' => true, 'fuzziness' => 20, 'num_backups' => 5, 'pot_alias' => array( 'default.po', 'en_US.po', 'en.po' ), 'php_alias' => array( 'php', 'twig' ), 'jsx_alias' => array(), 'fs_persist' => false, 'fs_protect' => 1, 'pot_protect' => 1, 'pot_expected' => 1, 'max_php_size' => '100K', 'po_utf8_bom' => false, 'po_width' => '79', 'jed_pretty' => false, 'jed_clean' => false, 'ajax_files' => true, 'deepl_api_key' => '', 'deepl_api_url' => '', 'google_api_key' => '', 'yandex_api_key' => '', 'microsoft_api_key' => '', 'microsoft_api_region' => 'global', 'lecto_api_key' => '', ); /** * Create default settings instance * @return Loco_data_Settings */ public static function create(){ $args = self::$defaults; $args['version'] = loco_plugin_version(); return new Loco_data_Settings( $args ); } /** * Get currently configured global settings * @return Loco_data_Settings */ public static function get(){ $opts = self::$current; if( ! $opts ){ $opts = self::create(); $opts->fetch(); self::$current = $opts; // allow hooks to modify settings do_action('loco_settings', $opts ); } return $opts; } /** * Destroy current settings * @return void */ public static function clear(){ delete_option('loco_settings'); self::$current = null; } /** * Destroy current settings and return a fresh one * @return Loco_data_Settings */ public static function reset(){ self::clear(); return self::$current = self::create(); } /** * {@inheritdoc} */ public function offsetSet( $prop, $value ){ $value = parent::cast($prop,$value,self::$defaults); parent::offsetSet( $prop, $value ); } /** * Commit current settings to WordPress DB * @return bool */ public function persist(){ $this->version = loco_plugin_version(); $this->clean(); return update_option('loco_settings', $this->getSerializable() ); } /** * Pull current settings from WordPress DB and merge into this object * @return bool whether settings where previously saved */ public function fetch(){ $data = get_option('loco_settings'); if( is_array($data) ){ $copy = new Loco_data_Settings; $copy->setUnserialized($data); // preserve any defaults not in previously saved data // this will occur if we've added options since setting were saved $data = $copy->getArrayCopy() + $this->getArrayCopy(); // could ensure redundant keys are removed, but no need currently // $data = array_intersect_key( $data, self::$defaults ); $this->exchangeArray( $data ); $this->clean(); return true; } return false; } /** * Run migration in case plugin has been upgraded since settings last saved * @return bool whether upgrade has occurred */ public function migrate(){ $updated = false; // Always update version number in settings after an upgrade $old = $this->version; $new = loco_plugin_version(); if( version_compare($old,$new,'<') ){ $this->persist(); $updated = true; // feature alerts: if( '2.5.' === substr($new,0,4) && '2.5.' !== substr($old,0,4) ){ Loco_error_AdminNotices::info( __('Loco Translate 2.5 adds supports for JSON language pack generation.','loco-translate') ) ->addLink( apply_filters('loco_external','https://localise.biz/wordpress/plugin/manual/json'), __('Documentation','loco-translate') ); } } return $updated; } /** * Populate ALL settings from raw postdata. * @param array posted setting values * @param array optional filter to restrict modifiable values * @return Loco_data_Settings */ public function populate( array $data, $filter = null ){ // set all keys present in posted data foreach( $data as $prop => $value ){ try { if( is_null($filter) || in_array($prop,$filter,true) ) { $this->offsetSet( $prop, $value ); } } catch( InvalidArgumentException $e ){ // skipping invalid key } } // set missing boolean keys as false, because unchecked checkboxes won't post anything $defaults = self::$defaults; if( is_array($filter) ){ $defaults = array_intersect_key( array_flip($filter) ,$defaults); } foreach( array_diff_key($defaults,$data) as $prop => $default ){ if( is_bool($default) ){ parent::offsetSet( $prop, false ); } } // enforce missing values that must have a default, but were passed empty foreach( array('php_alias','max_php_size','po_width') as $prop ){ if( isset($data[$prop]) && '' === $data[$prop] ){ parent::offsetSet( $prop, self::$defaults[$prop] ); } } return $this; } /** * Map a file extension to registered types, defaults to "php" * @param string * @return string php, js or twig */ public function ext2type($x){ $x = strtolower($x); $types = array_fill_keys( $this->jsx_alias, 'js' ); $types['twig'] = 'twig'; // <- temporary hack in lieu of dedicated twig extractor return isset($types[$x]) ? $types[$x] : 'php'; } }