Skip to content

Commit

Permalink
Mods: display settings tabs from state (qtranslate#1146)
Browse files Browse the repository at this point in the history
The tab display is independent from its manual activation.
Any module could have extra settings (e.g. ACF).

Make the display dependent on the module state instead.
Add a new `has_settings` field in the module defintions.
Rename `load_modules_enabled` to `load_active_modules`.
  • Loading branch information
herrvigg authored and spleen1981 committed Apr 3, 2022
1 parent 324f3f6 commit 7feb723
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 24 deletions.
23 changes: 6 additions & 17 deletions admin/qtx_admin_settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,33 +244,22 @@ private function add_configuration_inspector() {
}

private function add_sections( $nonce_action ) {
global $q_config;

$admin_sections = array();
$admin_sections['general'] = __( 'General', 'qtranslate' );
$admin_sections['advanced'] = __( 'Advanced', 'qtranslate' );

$custom_sections = apply_filters( 'qtranslate_admin_sections', array() );
$custom_sections = apply_filters( 'qtranslate_admin_sections', array() );
foreach ( $custom_sections as $key => $value ) {
$admin_sections[ $key ] = $value;
}

$admin_sections['integration'] = __( 'Integration', 'qtranslate' );
$admin_sections['import'] = __( 'Import', 'qtranslate' ) . '/' . __( 'Export', 'qtranslate' );
$admin_sections['languages'] = __( 'Languages', 'qtranslate' );

//TODO: this actually assumes every manual activation module has settings, dedicated key to be added if that is not the case...
if ( isset( $q_config['ma_module_enabled'] ) ) {
foreach ( $q_config['ma_module_enabled'] as $module_id => $module_enabled ) {
if ( ! $module_enabled ) {
continue;
}
$ma_module = QTX_Modules_Handler::get_module_def_by_id( $module_id );
$admin_sections[ $module_id ] = $ma_module['name'];
foreach ( QTX_Modules_Handler::get_active_modules() as $module ) {
if ( isset( $module ['has_settings'] ) && $module ['has_settings'] ) {
$admin_sections[ $module['id'] ] = $module['name'];
}
}
$admin_sections['import'] = __( 'Import', 'qtranslate' ) . '/' . __( 'Export', 'qtranslate' );
$admin_sections['languages'] = __( 'Languages', 'qtranslate' );
$admin_sections['troubleshooting'] = __( 'Troubleshooting', 'qtranslate' );

?>
<h2 class="nav-tab-wrapper">
<?php foreach ( $admin_sections as $slug => $name ) : ?>
Expand Down
30 changes: 24 additions & 6 deletions modules/qtx_modules_handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,44 @@

class QTX_Modules_Handler {
/**
* Loads modules previously enabled in the options after validation for plugin integration on admin-side.
* Get the modules previously activated in the options after validation for plugin integration on admin-side.
* Note these should be loaded before "qtranslate_init_language" is triggered.
*
* @see QTX_Admin_Modules::update_modules_status()
* @array Module defs.
*/
public static function load_modules_enabled() {
$def_modules = self::get_modules_defs();
public static function get_active_modules() {
$options_modules = get_option( 'qtranslate_modules', array() );
if ( ! is_array( $options_modules ) ) {
return null;
return array();
}

$active_modules = array();
$def_modules = self::get_modules_defs();
foreach ( $def_modules as $def_module ) {
if ( ! array_key_exists( $def_module['id'], $options_modules ) ) {
continue;
}
$module_status = $options_modules[ $def_module['id'] ];
if ( $module_status === QTX_MODULE_STATUS_ACTIVE ) {
include_once( QTRANSLATE_DIR . '/modules/' . $def_module['id'] . '/' . $def_module['id'] . '.php' );
array_push( $active_modules, $def_module );
}
}

return $active_modules;
}

/**
* Loads modules previously activated in the options after validation for plugin integration on admin-side.
* Note these should be loaded before "qtranslate_init_language" is triggered.
*
* @see QTX_Admin_Modules::update_modules_status()
*/
public static function load_active_modules() {
$def_modules = self::get_active_modules();
foreach ( $def_modules as $def_module ) {
include_once( QTRANSLATE_DIR . '/modules/' . $def_module['id'] . '/' . $def_module['id'] . '.php' );
}
}

public static function update_manual_enabled_modules() {
Expand All @@ -46,7 +63,7 @@ public static function update_manual_enabled_modules() {

if ( $changed ) {
update_option( 'qtranslate_modules', $options_modules );
self::load_modules_enabled();
self::load_active_modules();
do_action( 'qtx_ma_modules_updated' );
}
}
Expand Down Expand Up @@ -118,6 +135,7 @@ public static function get_modules_defs() {
'plugin' => true,
'incompatible' => 'qtranslate-slug/qtranslate-slug.php',
'manual_activation' => true,
'has_settings' => true,
)
);
}
Expand Down
2 changes: 1 addition & 1 deletion qtranslate_core.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ function qtranxf_init_language() {

qtranxf_load_option_qtrans_compatibility();

QTX_Modules_Handler::load_modules_enabled();
QTX_Modules_Handler::load_active_modules();

/**
* allow other plugins and modules to initialize whatever they need for language
Expand Down

0 comments on commit 7feb723

Please sign in to comment.