Skip to content

Commit

Permalink
modules: generalize manual activation
Browse files Browse the repository at this point in the history
  • Loading branch information
spleen1981 committed Apr 3, 2022
1 parent 324f3f6 commit 8759262
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 19 deletions.
8 changes: 4 additions & 4 deletions admin/qtx_admin_modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,26 +162,26 @@ public static function get_modules_infos() {
$status = array_key_exists( $module_def['id'], $options_modules ) ? $options_modules[ $module_def['id'] ] : QTX_MODULE_STATUS_UNDEFINED;
switch ( $status ) {
case QTX_MODULE_STATUS_ACTIVE:
$info['plugin'] = __( 'Active', 'qtranslate' );
$info['plugin'] = $module_def['plugin']===true?'-':__( 'Active', 'qtranslate' );
$info['module'] = __( 'Active', 'qtranslate' );
$info['icon'] = 'dashicons-yes';
$info['color'] = 'green';
break;
case QTX_MODULE_STATUS_INACTIVE:
$info['plugin'] = __( 'Inactive', 'qtranslate' );
$info['plugin'] = $module_def['plugin']===true?'-':__( 'Inactive', 'qtranslate' );
$info['module'] = __( 'Inactive', 'qtranslate' );
$info['icon'] = 'dashicons-no-alt';
$info['color'] = '';
break;
case QTX_MODULE_STATUS_BLOCKED:
$info['plugin'] = __( 'Active', 'qtranslate' );
$info['plugin'] = $module_def['plugin']===true?'-':__( 'Active', 'qtranslate' );
$info['module'] = __( 'Blocked', 'qtranslate' );
$info['icon'] = 'dashicons-warning';
$info['color'] = 'orange';
break;
case QTX_MODULE_STATUS_UNDEFINED:
default:
$info['plugin'] = __( 'Undefined', 'qtranslate' );
$info['plugin'] = $module_def['plugin']===true?'-':__( 'Undefined', 'qtranslate' );
$info['module'] = __( 'Inactive', 'qtranslate' );
$info['icon'] = 'dashicons-editor-help';
$info['color'] = '';
Expand Down
14 changes: 11 additions & 3 deletions admin/qtx_admin_options_update.php
Original file line number Diff line number Diff line change
Expand Up @@ -598,20 +598,28 @@ function qtranxf_update_setting( $var, $type = QTX_STRING, $def = null, $bool_el
case QTX_ARRAY:
if ( isset( $_POST[ $var ] ) ) {
$val = $_POST[ $var ];
if ( ! is_array( $_POST[ $var ] ) ) {
if ( ! is_array( $val ) ) {
$val = sanitize_text_field( $val );
$val = preg_split( '/[\s,]+/', $val, -1, PREG_SPLIT_NO_EMPTY );
}
} else {
$val = '';
$val = array();
}
if ( empty( $val ) && ! is_null( $def ) ) {
if ( ! $bool_elements_array && empty( $val ) && ! is_null( $def ) ) {
if ( is_string( $def ) ) {
$val = preg_split( '/[\s,]+/', $def, -1, PREG_SPLIT_NO_EMPTY );
} else if ( is_array( $def ) ) {
$val = $def;
}
}
// in case of bools array compare updating settings arrays with defaults to find keys set as false
if ( $bool_elements_array && ! is_null( $def ) && is_array( $def )) {
foreach ( $def as $key => $value ) {
if ( ! array_key_exists( $key, $val ) ) {
$val[ $key ] = false; //e.g. unchecked checkboxes input are not included in _$_POST
}
}
}
if ( isset( $q_config[ $var ] ) && qtranxf_array_compare( $q_config[ $var ], $val ) ) {
return false;
}
Expand Down
3 changes: 2 additions & 1 deletion admin/qtx_admin_settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,8 @@ class="qtranxs_explanation"><?php _e( 'The built-in integration modules are auto
<input type="checkbox"
name="ma_module_enabled[<?php echo $module['id']; ?>]"
id="ma_module_enabled_<?php echo $module['id']; ?>"
value="1"<?php checked( $q_config['ma_module_enabled'][ $module['id'] ] ) ?>/>
value="1"<?php checked( $q_config['ma_module_enabled'][ $module['id'] ] );
disabled(QTX_Admin_Modules::check_module( QTX_Modules_Handler::get_module_def_by_id( $module['id'] ) ) != QTX_MODULE_STATUS_ACTIVE ) ?>/>
<?php echo $module['name']; ?>
</label>
<?php else: echo $module['name']; endif; ?>
Expand Down
26 changes: 16 additions & 10 deletions modules/qtx_modules_handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,21 @@ public static function update_manual_enabled_modules() {
$options_modules = get_option( 'qtranslate_modules', array() );
$changed = false;
foreach ( $q_config['ma_module_enabled'] as $module_id => $module_enabled ) {
if ( $module_enabled && $options_modules[ $module_id ] != QTX_MODULE_STATUS_ACTIVE ) {
$options_modules[ $module_id ] = QTX_MODULE_STATUS_ACTIVE;
$changed = true;
} else if ( ! $module_enabled && $options_modules[ $module_id ] == QTX_MODULE_STATUS_ACTIVE ) {
$options_modules[ $module_id ] = QTX_MODULE_STATUS_INACTIVE;
$changed = true;
$status = QTX_Admin_Modules::check_module( self::get_module_def_by_id( $module_id ) );
if ($status == QTX_MODULE_STATUS_ACTIVE) { //module can be activated
if ( $module_enabled && $options_modules[ $module_id ] != QTX_MODULE_STATUS_ACTIVE ) {
$options_modules[ $module_id ] = QTX_MODULE_STATUS_ACTIVE;
$changed = true;
} else if ( ! $module_enabled && $options_modules[ $module_id ] == QTX_MODULE_STATUS_ACTIVE ) {
$options_modules[ $module_id ] = QTX_MODULE_STATUS_INACTIVE;
$changed = true;
}
} else { //TODO check if actual cases exists from user interface, otherwise this condition is redundant
$q_config['ma_module_enabled'][$module_id] = false;
if ( $options_modules[ $module_id ] != $status ) {
$options_modules[ $module_id ] = $status;
$changed = true;
}
}
}

Expand Down Expand Up @@ -117,7 +126,6 @@ public static function get_modules_defs() {
'name' => __( 'Slugs translation', 'qtranslate' ) . sprintf( ' (%s)', __( 'experimental' ) ),
'plugin' => true,
'incompatible' => 'qtranslate-slug/qtranslate-slug.php',
'manual_activation' => true,
)
);
}
Expand All @@ -126,9 +134,7 @@ public static function ma_modules_default_options() {
$module_defs = self::get_modules_defs();
$response = array();
foreach ( $module_defs as $module ) {
if ( isset( $module['manual_activation'] ) && $module['manual_activation'] == true ) {
$response[ $module['id'] ] = false;
}
$response[ $module['id'] ] = $module['plugin']===true ? false : QTX_Admin_Modules::check_module( $module )===QTX_MODULE_STATUS_ACTIVE;
}

return $response;
Expand Down
2 changes: 1 addition & 1 deletion qtranslate_core.php
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,7 @@ function qtranxf_load_config() {
foreach ( $qtranslate_options['front']['array'] as $name => $def ) {
qtranxf_load_option_array( $name, $def );
}
qtranxf_load_option( 'ma_module_enabled' );
qtranxf_load_option_array( 'ma_module_enabled', $qtranslate_options['default_value']['ma_module_enabled'] );
qtranxf_load_option_array( 'term_name', array() );

if ( $q_config['filter_options_mode'] == QTX_FILTER_OPTIONS_LIST ) {
Expand Down

0 comments on commit 8759262

Please sign in to comment.