Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor module classes #1153

Merged
merged 10 commits into from
Apr 24, 2022
109 changes: 30 additions & 79 deletions admin/qtx_admin_modules.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php

/**
* Module admin management, taking care of the state updates and notices.
*/
class QTX_Admin_Modules {

/**
* Register hooks for modules and related plugins
*/
Expand Down Expand Up @@ -29,56 +31,49 @@ public static function update_modules_state( $func_is_active = 'is_plugin_active

$option_modules = array();
require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
foreach ( QTX_Modules_Handler::get_modules_defs() as $module_def ) {
$state = self::can_module_be_activated( $module_def, $func_is_active );
foreach ( QTX_Module_Setup::get_modules() as $module ) {
$state = self::can_module_be_activated( $module, $func_is_active );
if ( $state == QTX_MODULE_STATE_ACTIVE ) {
// The admin options matter only if the module can be activated, otherwise the hard conditions prevail.
if ( isset ( $q_config['admin_enabled_modules'][ $module_def['id'] ] ) && ! $q_config['admin_enabled_modules'][ $module_def['id'] ] ) {
if ( isset ( $q_config['admin_enabled_modules'][ $module->id ] ) && ! $q_config['admin_enabled_modules'][ $module->id ] ) {
$state = QTX_MODULE_STATE_INACTIVE;
}
}
$option_modules[ $module_def['id'] ] = $state;
$option_modules[ $module->id ] = $state;
}

$old_option_modules = get_option( 'qtranslate_modules_state' );
update_option( 'qtranslate_modules_state', $option_modules );

// trigger info notices only if changed
// Trigger info notices and potential loading only if changed.
if ( $old_option_modules != $option_modules ) {
set_transient( 'qtranslate_notice_modules', true, 5 );
QTX_Modules_Handler::load_active_modules();
QTX_Module_Loader::load_active_modules();
}
}

/**
* Check if the module has a related plugin active, if any.
*
* @param array $module_def
* @param QTX_Module $module
* @param callable $func_is_active
*
* @return bool|mixed true if the integration plugin is active or if the module does not have any..
* @return bool true if the integration plugin is active OR if the module does not have any..
*/
public static function is_module_plugin_active( $module_def, $func_is_active = 'is_plugin_active' ) {
$active = false;
public static function is_module_plugin_active( $module, $func_is_active = 'is_plugin_active' ) {
if ( empty( $module->plugins ) ) {
return true; // Attention :should not be interpreted as "having a plugin".
}

require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
// TODO the call_user_func should be replaced by direct calls from PHP7
$integration_plugin = $module_def['plugin'];
if ( is_array( $integration_plugin ) ) {
$active = false;
foreach ( $integration_plugin as $item_plugin ) {
if ( call_user_func( $func_is_active, $item_plugin ) ) {
$active = true;
break;
}
foreach ( $module->plugins as $plugin ) {
if ( call_user_func( $func_is_active, $plugin ) ) {
return true;
}
} else if ( is_bool( $integration_plugin ) ) {
$active = $integration_plugin;
} else if ( is_string( $integration_plugin ) ) {
$active = call_user_func( $func_is_active, $integration_plugin );
}

return $active;
return false;
}

/**
Expand All @@ -88,17 +83,16 @@ public static function is_module_plugin_active( $module_def, $func_is_active = '
*
* ATTENTION: the admin checkboxes are ignored in this check! This evaluates the "potential" state.
*
* @param array $module_def
* @param QTX_Module $module
* @param callable $func_is_active callback to evaluate if a plugin is active
*
* @return integer module state
*/
public static function can_module_be_activated( $module_def, $func_is_active = 'is_plugin_active' ) {
public static function can_module_be_activated( $module, $func_is_active = 'is_plugin_active' ) {
$state = QTX_MODULE_STATE_INACTIVE;

$active = self::is_module_plugin_active( $module_def, $func_is_active );
if ( $active ) {
if ( isset( $module_def['incompatible'] ) && call_user_func( $func_is_active, $module_def['incompatible'] ) ) {
if ( self::is_module_plugin_active( $module, $func_is_active ) ) {
if ( isset( $module->incompatible ) && call_user_func( $func_is_active, $module->incompatible ) ) {
$state = QTX_MODULE_STATE_BLOCKED;
} else {
$state = QTX_MODULE_STATE_ACTIVE;
Expand Down Expand Up @@ -145,25 +139,25 @@ public static function admin_notices() {
return;
}

$module_defs = QTX_Modules_Handler::get_modules_defs();
$active_modules = array();
foreach ( $module_defs as $module_def ) {
if ( ! array_key_exists( $module_def['id'], $options_modules ) ) {
$modules = QTX_Module_Setup::get_modules();
foreach ( $modules as $module ) {
if ( ! array_key_exists( $module->id, $options_modules ) ) {
continue;
}

switch ( $options_modules[ $module_def['id'] ] ) {
switch ( $options_modules[ $module->id ] ) {
case QTX_MODULE_STATE_BLOCKED:
$incompatible_plugin = $module_def['incompatible'];
$incompatible_plugin = $module->incompatible;
$plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . $incompatible_plugin, false, true );
$plugin_name = $plugin_data['Name'];
$url_deactivate = esc_url( wp_nonce_url( admin_url( 'plugins.php?action=deactivate&plugin=' . urlencode( $incompatible_plugin ) ), 'deactivate-plugin_' . $incompatible_plugin ) );
$msg = '<p>' . sprintf( __( 'The plugin "%s" is incompatible with the module "%s" of %s. Please disable it.', 'qtranslate' ), $plugin_name, $module_def['name'], 'qTranslate&#8209;XT' ) . '</p>';
$msg = '<p>' . sprintf( __( 'The plugin "%s" is incompatible with the module "%s" of %s. Please disable it.', 'qtranslate' ), $plugin_name, $module->name, 'qTranslate&#8209;XT' ) . '</p>';
$msg .= '<p><a class="button" href="' . $url_deactivate . '"><strong>' . sprintf( __( 'Deactivate plugin %s', 'qtranslate' ), $plugin_name ) . '</strong></a>';
echo '<div class="notice notice-warning is-dismissible">' . $msg . '</div>';
break;
case QTX_MODULE_STATE_ACTIVE:
$active_modules[] = $module_def['name'];
$active_modules[] = $module->name;
break;
}
}
Expand All @@ -174,47 +168,4 @@ public static function admin_notices() {
delete_transient( 'qtranslate_notice_modules' );
}
}

/**
* Retrieve infos for all modules (for display).
* The status is retrieved from the modules option.
*/
public static function get_modules_infos() {
$module_defs = QTX_Modules_Handler::get_modules_defs();
$options_modules = get_option( 'qtranslate_modules_state', array() );
$infos = array();
foreach ( $module_defs as $module_def ) {
$info = array();
$info['def'] = $module_def;
$info['state'] = isset( $options_modules[ $module_def['id'] ] ) ? $options_modules[ $module_def['id'] ] : QTX_MODULE_STATE_UNDEFINED;
$info['plugin'] = $module_def['plugin'] === true ? _x( 'None', 'Module admin', 'qtranslate' ) : ( self::is_module_plugin_active( $module_def ) ? _x( 'Active', 'Module admin', 'qtranslate' ) : _x( 'Inactive', 'Module admin', 'qtranslate' ) );
switch ( $info['state'] ) {
case QTX_MODULE_STATE_ACTIVE:
$info['module'] = _x( 'Active', 'Module admin', 'qtranslate' );
$info['icon'] = 'dashicons-yes';
$info['color'] = 'green';
break;
case QTX_MODULE_STATE_INACTIVE:
$info['module'] = _x( 'Inactive', 'Module admin', 'qtranslate' );
$info['icon'] = 'dashicons-no-alt';
$info['color'] = '';
break;
case QTX_MODULE_STATE_BLOCKED:
$info['module'] = _x( 'Blocked', 'Module admin', 'qtranslate' );
$info['icon'] = 'dashicons-warning';
$info['color'] = 'orange';
break;
case QTX_MODULE_STATE_UNDEFINED:
default:
$info['module'] = __( 'Inactive', 'qtranslate' );
$info['icon'] = 'dashicons-editor-help';
$info['color'] = '';
break;
}

array_push( $infos, $info );
}

return $infos;
}
}
5 changes: 2 additions & 3 deletions admin/qtx_admin_options.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,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_Module_Setup::get_modules() as $module ) {
$options['admin']['admin_enabled_modules'][ $module->id ] = $module->is_default_enabled();
}

// options processed in a special way
Expand Down
38 changes: 18 additions & 20 deletions admin/qtx_admin_settings.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_update.php' );
require_once( QTRANSLATE_DIR . '/admin/qtx_admin_settings_language_list.php' );
require_once( QTRANSLATE_DIR . '/admin/qtx_admin_settings_module.php' );
require_once( QTRANSLATE_DIR . '/admin/qtx_import_export.php' );

/**
Expand Down Expand Up @@ -252,9 +253,9 @@ 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'];
foreach ( QTX_Module_Loader::get_active_modules() as $module ) {
if ( $module->has_settings() ) {
$admin_sections[ $module->id ] = $module->name;
}
}
$admin_sections['import'] = __( 'Import', 'qtranslate' ) . '/' . __( 'Export', 'qtranslate' );
Expand Down Expand Up @@ -721,33 +722,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 ( QTX_Admin_Settings_Module::get_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
89 changes: 89 additions & 0 deletions admin/qtx_admin_settings_module.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

/**
* Module admin settings, for display in the settings panels.
*/
class QTX_Admin_Settings_Module {
public $id;
public $name;
protected $state;
public $plugin_state_label;
public $module_state_label;
public $icon;
public $color;
protected $module;

/**
* Constructor.
*
* @param QTX_Module $module
*/
public function __construct( $module, $state ) {
$this->module = $module;
$this->id = $module->id;
$this->name = $module->name;
$this->state = $state;
switch ( $this->state ) {
case QTX_MODULE_STATE_ACTIVE:
$this->module_state_label = _x( 'Active', 'Module settings', 'qtranslate' );
$this->icon = 'dashicons-yes';
$this->color = 'green';
break;
case QTX_MODULE_STATE_INACTIVE:
$this->module_state_label = _x( 'Inactive', 'Module settings', 'qtranslate' );
$this->icon = 'dashicons-no-alt';
$this->color = '';
break;
case QTX_MODULE_STATE_BLOCKED:
$this->module_state_label = _x( 'Blocked', 'Module settings', 'qtranslate' );
$this->icon = 'dashicons-warning';
$this->color = 'orange';
break;
case QTX_MODULE_STATE_UNDEFINED:
default:
$this->module_state_label = _x( 'Inactive', 'Module settings', 'qtranslate' );
$this->icon = 'dashicons-editor-help';
$this->color = '';
break;
}
$this->plugin_state_label = empty( $module->plugins ) ? _x( 'None', 'Module settings', 'qtranslate' ) : ( QTX_Admin_Modules::is_module_plugin_active( $module ) ? _x( 'Active', 'Module settings', 'qtranslate' ) : _x( 'Inactive', 'Module settings', 'qtranslate' ) );
}

/**
* Retrieve admin enabled checked settings.
*
* @return bool
*/
public function is_checked() {
global $q_config;

return ( isset( $q_config['admin_enabled_modules'][ $this->module->id ] ) && $q_config['admin_enabled_modules'][ $this->module->id ] ) || ( $this->state == QTX_MODULE_STATE_ACTIVE );
}

/**
* Retrieve disabled settings.
*
* @return bool
*/
public function is_disabled() {
return ( QTX_Admin_Modules::can_module_be_activated( $this->module ) != QTX_MODULE_STATE_ACTIVE );
}

/**
* Retrieve settings for all modules (for display).
* The status is retrieved from the modules option.
*
* @return QTX_Admin_Settings_Module[]
*/
public static function get_settings_modules() {
$modules = QTX_Module_Setup::get_modules();
$options_modules = get_option( 'qtranslate_modules_state', array() );
$settings = array();
foreach ( $modules as $module ) {
$state = isset( $options_modules[ $module->id ] ) ? $options_modules[ $module->id ] : QTX_MODULE_STATE_UNDEFINED;
$settings[] = new QTX_Admin_Settings_Module( $module, $state );
}

return $settings;
}
}
Loading