Skip to content

Commit

Permalink
Refactor module classes and file structure (#1153)
Browse files Browse the repository at this point in the history
The module objects are very difficult to manipulate with simple arrays.

Reorganize the code for a much better structure:
- `QTX_Admin_Module`: provides the basic static data (id, name, plugins, ...) and the built-in setup.
- `QTX_Admin_Module_Manager`: manages the module state updates and notices
- `QTX_Admin_Module_Settings`: holds the data relevant for the settings, mostly for display
- `QTX_Module_Loader`: reads the state and loads modules (used by admin and front core)

Move files to the `modules` folder to keep the related code together.
  • Loading branch information
herrvigg authored Apr 24, 2022
1 parent 169d6d8 commit 3da12c5
Show file tree
Hide file tree
Showing 15 changed files with 548 additions and 395 deletions.
6 changes: 3 additions & 3 deletions admin/qtx_activation_hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
exit;
}

require_once( QTRANSLATE_DIR . '/admin/qtx_admin_modules.php' );
require_once( QTRANSLATE_DIR . '/modules/qtx_admin_module_manager.php' );

/**
* Save language properties from configuration to WP options
Expand Down Expand Up @@ -833,7 +833,7 @@ function qtranxf_activation_hook() {
global $qtranslate_options;
qtranxf_admin_set_default_options( $qtranslate_options );
qtranxf_load_option_array( 'admin_enabled_modules', $qtranslate_options['admin']['admin_enabled_modules'] );
QTX_Admin_Modules::update_modules_state();
QTX_Admin_Module_Manager::update_modules_state();

/**
* A chance to execute activation actions specifically for this plugin.
Expand Down Expand Up @@ -1056,5 +1056,5 @@ function qtranxf_register_activation_hooks() {
$qtx_plugin_basename = plugin_basename( QTRANSLATE_FILE );
register_activation_hook( $qtx_plugin_basename, 'qtranxf_activation_hook' );
register_deactivation_hook( $qtx_plugin_basename, 'qtranxf_deactivation_hook' );
QTX_Admin_Modules::register_hooks();
QTX_Admin_Module_Manager::register_hooks();
}
220 changes: 0 additions & 220 deletions admin/qtx_admin_modules.php

This file was deleted.

6 changes: 3 additions & 3 deletions admin/qtx_admin_options.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
}

require_once( QTRANSLATE_DIR . '/admin/qtx_admin_utils.php' );
require_once( QTRANSLATE_DIR . '/modules/qtx_admin_module.php' );

function qtranxf_admin_set_default_options( &$options ) {
// options processed in a standardized way
Expand Down Expand Up @@ -40,9 +41,8 @@ function qtranxf_admin_set_default_options( &$options ) {

// Boolean set defining the default enabled options for each module, hard values not depending on any state.
$options['admin']['admin_enabled_modules'] = array();
foreach ( QTX_Modules_Handler::get_modules_defs() as $module_def ) {
// TODO: expand default values in module def
$options['admin']['admin_enabled_modules'][ $module_def['id'] ] = ( $module_def['plugin'] !== true );
foreach ( QTX_Admin_Module::get_modules() as $module ) {
$options['admin']['admin_enabled_modules'][ $module->id ] = $module->is_default_enabled();
}

// options processed in a special way
Expand Down
5 changes: 3 additions & 2 deletions admin/qtx_admin_options_update.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

require_once( QTRANSLATE_DIR . '/admin/qtx_admin_options.php' );
require_once( QTRANSLATE_DIR . '/admin/qtx_import_export.php' );
require_once( QTRANSLATE_DIR . '/modules/qtx_admin_module_manager.php' );

function qtranxf_editConfig() {
_deprecated_function( __FUNCTION__, '3.10.0', 'qtranxf_edit_config' );
Expand Down Expand Up @@ -378,7 +379,7 @@ function qtranxf_reset_config() {
qtranxf_reload_config();
add_filter( 'locale', 'qtranxf_localeForCurrentLanguage', 99 );

QTX_Admin_Modules::update_modules_state();
QTX_Admin_Module_Manager::update_modules_state();
}

add_action( 'qtranslate_save_config', 'qtranxf_reset_config', 20 );
Expand Down Expand Up @@ -943,7 +944,7 @@ function qtranxf_update_settings() {

qtranxf_update_setting( 'admin_enabled_modules', QTX_BOOLEAN_SET, $qtranslate_options['admin']['admin_enabled_modules'] );

QTX_Admin_Modules::update_modules_state();
QTX_Admin_Module_Manager::update_modules_state();

// opportunity to update special custom settings on sub-plugins
do_action( 'qtranslate_update_settings' );
Expand Down
49 changes: 27 additions & 22 deletions admin/qtx_admin_settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require_once( QTRANSLATE_DIR . '/admin/qtx_admin_options_update.php' );
require_once( QTRANSLATE_DIR . '/admin/qtx_admin_settings_language_list.php' );
require_once( QTRANSLATE_DIR . '/admin/qtx_import_export.php' );
require_once( QTRANSLATE_DIR . '/modules/qtx_admin_module_settings.php' );

/**
* Class QTX_Admin_Settings
Expand Down Expand Up @@ -252,9 +253,11 @@ private function add_sections( $nonce_action ) {
$admin_sections[ $key ] = $value;
}
$admin_sections['integration'] = __( 'Integration', 'qtranslate' );
foreach ( QTX_Modules_Handler::get_active_modules() as $module ) {
if ( isset( $module ['has_settings'] ) && $module ['has_settings'] ) {
$admin_sections[ $module['id'] ] = $module['name'];

$settings_modules = QTX_Admin_Module_Settings::get_settings_modules();
foreach ( $settings_modules as $module ) {
if ( $module->is_active() && $module->has_settings() ) {
$admin_sections[ $module->id ] = $module->name;
}
}
$admin_sections['import'] = __( 'Import', 'qtranslate' ) . '/' . __( 'Export', 'qtranslate' );
Expand All @@ -273,7 +276,7 @@ private function add_sections( $nonce_action ) {
<?php
$this->add_general_section();
$this->add_advanced_section();
$this->add_integration_section();
$this->add_integration_section( $settings_modules );
$this->add_troubleshooting_section();
// Allow to load additional services
do_action( 'qtranslate_configuration', $this->options_uri );
Expand Down Expand Up @@ -700,7 +703,12 @@ private function add_advanced_section() {
$this->close_section( 'advanced' );
}

private function add_integration_section() {
/**
* @param QTX_Admin_Module_Settings[] $settings_modules
*
* @return void
*/
private function add_integration_section( $settings_modules ) {
global $q_config;

$this->open_section( 'integration' ); ?>
Expand All @@ -721,33 +729,30 @@ class="qtranxs_explanation"><?php _e( 'Each built-in integration module can only
<table id="qtranxs_modules" class="widefat">
<thead>
<tr>
<th class="row-title"><?php _ex( 'Name', 'Module admin', 'qtranslate' ); ?></th>
<th><?php _ex( 'Required plugin', 'Module admin', 'qtranslate' ); ?></th>
<th><?php _ex( 'Module', 'Module admin', 'qtranslate' ); ?></th>
<th class="row-title"><?php _ex( 'Name', 'Module settings', 'qtranslate' ); ?></th>
<th><?php _ex( 'Required plugin', 'Module settings', 'qtranslate' ); ?></th>
<th><?php _ex( 'Module', 'Module settings', 'qtranslate' ); ?></th>
</tr>
</thead>
<tbody>
<?php
foreach ( QTX_Admin_Modules::get_modules_infos() as $module ) :
$module_id = $module['def']['id'];
$module_is_checked = ( isset( $q_config['admin_enabled_modules'][ $module_id ] ) && $q_config['admin_enabled_modules'][ $module_id ] ) || ( $module['state'] == QTX_MODULE_STATE_ACTIVE );
$module_is_disabled = ( QTX_Admin_Modules::can_module_be_activated( $module['def'] ) != QTX_MODULE_STATE_ACTIVE );
foreach ( $settings_modules as $module ) :
?>
<tr>
<td>
<input type="checkbox"
name="admin_enabled_modules[<?php echo $module_id; ?>]"
id="admin_enabled_modules_<?php echo $module_id; ?>"
value="1"<?php checked( $module_is_checked );
disabled( $module_is_disabled ) ?>/>
<label for="admin_enabled_modules_<?php echo $module_id; ?>">
<?php echo $module['def']['name']; ?>
name="admin_enabled_modules[<?php echo $module->id; ?>]"
id="admin_enabled_modules_<?php echo $module->id; ?>"
value="1"<?php checked( $module->is_checked() );
disabled( $module->is_disabled() ) ?>/>
<label for="admin_enabled_modules_<?php echo $module->id; ?>">
<?php echo $module->name; ?>
</label>
</td>
<td><?php echo $module['plugin'] ?></td>
<td style="color: <?php echo $module['color'] ?>">
<span class="dashicons <?php echo $module['icon'] ?>"></span>
<?php echo $module['module'] ?>
<td><?php echo $module->plugin_state_label ?></td>
<td style="color: <?php echo $module->color ?>">
<span class="dashicons <?php echo $module->icon ?>"></span>
<?php echo $module->module_state_label ?>
</td>
</tr>
<?php endforeach; ?>
Expand Down
Loading

0 comments on commit 3da12c5

Please sign in to comment.