Skip to content

Commit

Permalink
error handling on options save
Browse files Browse the repository at this point in the history
  • Loading branch information
johnclause committed Jun 7, 2015
1 parent 1dd5c81 commit 4eae0d9
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 14 deletions.
2 changes: 1 addition & 1 deletion admin/qtx_activation_hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ function qtranxf_load_config_files($json_files){
//$cfg_json=php_strip_whitespace($fnm);
if($cfg_json){
$cfg=json_decode($cfg_json,true);
if($cfg){
if(!empty($cfg) && is_array($cfg)){
$cfg_all = qtranxf_merge_config($cfg_all,$cfg);
}else{
qtranxf_error_log(sprintf(__('Could not parse %s file "%s" listed in option "%s".', 'qtranslate'), 'JSON', $fnm, '<a href="'.admin_url('options-general.php?page=qtranslate-x#integration').'">'.__('Configuration Files', 'qtranslate').'</a>'));
Expand Down
22 changes: 21 additions & 1 deletion admin/qtx_admin_options.php
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,27 @@ function qtranxf_updateSettings()

//special cases handling

if(isset($_POST['json_config_files'])){
//verify that files are loadable
$json_config_files_post = sanitize_text_field(stripslashes($_POST['json_config_files']));
$json_files = preg_split('/[\s,]+/',$json_config_files_post,null,PREG_SPLIT_NO_EMPTY);
if(empty($json_files)){
$_POST['config_files'] = array();
unset($_POST['json_config_files']);
}else{
$nerr = isset($q_config['errors']) ? count($q_config['errors']) : 0;
$cfg = qtranxf_load_config_files($json_files);
if(!empty($q_config['errors']) && $nerr != count($q_config['errors'])){//new errors occurred
$_POST['json_config_files'] = implode(PHP_EOL,$json_files);
}else{
$_POST['config_files'] = implode(PHP_EOL,$json_files);
unset($_POST['json_config_files']);
}
}
}

if(isset($_POST['json_custom_i18n_config'])){
//verify that JSON string can be parsed
$cfg_json = sanitize_text_field(stripslashes($_POST['json_custom_i18n_config']));
if(empty($cfg_json)){
$_POST['custom_i18n_config'] = array();
Expand Down Expand Up @@ -568,7 +588,7 @@ function qtranxf_updateSettings()
foreach($q_config['errors'] as $msg){
$errors[] = $msg;
}
unset($q_config['errors']);
unset($q_config['errors']);//todo error handling needs to be cleaned up
}
return $errors;
}
18 changes: 7 additions & 11 deletions admin/qtx_admin_utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,15 @@ function qtranxf_detect_admin_language($url_info) {

function qtranxf_array_compare($a,$b) {
if( !is_array($a) || !is_array($b) ) return false;
if(count($a)!=count($b)) return false;
foreach($a as $k => $v){
if(is_array($v)) return qtranxf_array_compare($v,$b[$k]);
if($b[$k] !== $v) return false;
if(count($a) != count($b)) return false;
foreach($a as $k => $v){
if(is_array($v)){
if(!qtranxf_array_compare($v,$b[$k])) return false;
}else{
if($b[$k] !== $v) return false;
}
}
return true;
//$sa = serialize($a);
//$sb = serialize($b);
//return $sa === $sb;
//can be optimized
//$diff_a=array_diff($a,$b);
//$diff_b=array_diff($b,$a);
//return empty($diff_a) && empty($diff_b);
}

function qtranxf_join_texts($texts,$sep) {
Expand Down
2 changes: 1 addition & 1 deletion admin/qtx_configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,7 @@ function qtranxf_conf() {
<tr valign="top">
<th scope="row"><?php _e('Configuration Files', 'qtranslate') ?></th>
<td><label for="qtranxs_config_files" class="qtranxs_explanation"><?php printf(__('List of configuration files. Unless prefixed with "%s", paths are relative to %s variable: %s. Absolute paths are also acceptable.', 'qtranslate'), './', 'WP_CONTENT_DIR', trailingslashit(WP_CONTENT_DIR)) ?></label>
<br/><textarea name="config_files" id="qtranxs_config_files" rows="4" style="width:100%"><?php echo implode(PHP_EOL,$q_config['config_files']) ?></textarea>
<br/><textarea name="json_config_files" id="qtranxs_config_files" rows="4" style="width:100%"><?php if(isset($_POST['json_config_files'])) echo $_POST['json_config_files']; else echo implode(PHP_EOL,$q_config['config_files']) ?></textarea>
<p class="qtranxs_notes"><?php printf(__('The list gets auto-updated on a 3rd-party integrated plugin activation/deactivation. You may also add your own custom files for your theme or plugins. File "%s" is the default configuration loaded from this plugin folder. It is not recommended to modify any configuration file from other authors, but you may alter any configuration item through your own custom file appended to the end of this list.', 'qtranslate'), './i18n-config.json');
echo ' '; printf(__('Please, read %sIntegration Guide%s for more information.', 'qtranslate'), '<a href="https://qtranslatexteam.wordpress.com/integration/" target="_blank">', '</a>');
echo ' '.__('To reset to default, clear the text.', 'qtranslate') ?></p>
Expand Down
1 change: 1 addition & 0 deletions dev/i18n-config/i18n-config-profile.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"forms":{
"main":{
"fields":{
"nickname":{},
"description":{}
}
}
Expand Down

0 comments on commit 4eae0d9

Please sign in to comment.