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

generalized manual loaded modules mechanism #1135

Merged
merged 1 commit into from
Mar 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions admin/qtx_admin_options_update.php
Original file line number Diff line number Diff line change
Expand Up @@ -528,12 +528,12 @@ function qtranxf_updateSetting( $var, $type = QTX_STRING, $def = null ) {
qtranxf_update_setting( $var, $type, $def );
}

function qtranxf_update_setting( $var, $type = QTX_STRING, $def = null ) {
function qtranxf_update_setting( $var, $type = QTX_STRING, $def = null, $bool_allowed = false ) {
global $q_config, $qtranslate_options;
if ( ! isset( $_POST['submit'] ) ) {
return false;
}
if ( ! isset( $_POST[ $var ] ) && $type != QTX_BOOLEAN ) {
if ( ! isset( $_POST[ $var ] ) && $type != QTX_BOOLEAN && ! $bool_allowed ) {
return false;
}

Expand Down Expand Up @@ -596,10 +596,14 @@ function qtranxf_update_setting( $var, $type = QTX_STRING, $def = null ) {

return true;
case QTX_ARRAY:
$val = $_POST[ $var ];
if ( ! is_array( $_POST[ $var ] ) ) {
$val = sanitize_text_field( $val );
$val = preg_split( '/[\s,]+/', $val, -1, PREG_SPLIT_NO_EMPTY );
if ( isset($_POST[ $var ] ) ){
$val=$_POST[ $var ];
if ( ! is_array( $_POST[ $var ] ) ) {
$val = sanitize_text_field( $val );
$val = preg_split( '/[\s,]+/', $val, -1, PREG_SPLIT_NO_EMPTY );
}
}else{
$val='';
}
if ( empty( $val ) && ! is_null( $def ) ) {
if ( is_string( $def ) ) {
Expand Down Expand Up @@ -807,6 +811,7 @@ function qtranxf_update_settings() {
qtranxf_update_setting( $name, QTX_ARRAY, $default );
}
qtranxf_update_setting( 'filter_options', QTX_ARRAY );
qtranxf_update_setting( 'ma_module_enabled', QTX_ARRAY, null, true );

switch ( $q_config['url_mode'] ) {
case QTX_URL_DOMAIN:
Expand Down
31 changes: 20 additions & 11 deletions admin/qtx_admin_settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,13 @@ private function add_sections( $nonce_action ) {
$admin_sections['import'] = __( 'Import', 'qtranslate' ) . '/' . __( 'Export', 'qtranslate' );
$admin_sections['languages'] = __( 'Languages', 'qtranslate' );

if ( $q_config['slugs_enabled'] ) {
$admin_sections['slugs'] = __( 'Slugs', 'qtranslate' );
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'];
}
}

$admin_sections['troubleshooting'] = __( 'Troubleshooting', 'qtranslate' );

?>
Expand Down Expand Up @@ -750,19 +753,25 @@ class="qtranxs_explanation"><?php _e( 'The built-in integration modules are auto
</table>
</td>
</tr>
<tr id="option_slugs_enabled">
<th scope="row"><?php _e( 'Slugs translation', 'qtranslate' ) ?></th>
<?php
foreach ( $q_config['ma_module_enabled'] as $module_id=>$module_enabled ):
$module=QTX_Modules_Handler::get_module_def_by_id($module_id);
?>
<tr>
<th scope="row"><?php echo $module['name']; ?></th>
<td>
<label for="slugs_enabled">
<input type="checkbox" name="slugs_enabled"
id="slugs_enabled"
value="1"<?php checked( $q_config['slugs_enabled'] ) ?>/>&nbsp;<?php _e( 'Enable slugs translation.', 'qtranslate' ); ?>
<label for="ma_module_enabled_<?php echo $module_id; ?>">
<input type="checkbox" name="ma_module_enabled[<?php echo $module_id; ?>]"
id="ma_module_enabled_<?php echo $module_id; ?>"
value="1"<?php checked( $module_enabled ) ?>/>&nbsp;<?php echo $module['ma_checkbox_text']; ?>
</label>
<p class="qtranxs-notes"> <?php echo __( 'This activates the slug translation module. ', 'qtranslate' );
echo '&nbsp;' . __( 'Attention! This module is still experimental. It is subject to bugs and limitations.', 'qtranslate' ) ?>
<p class="qtranxs-notes"> <?php echo $module['ma_checkbox_notes']; ?>
</p>
</td>
</tr>
<?php
endforeach;
?>
<tr>
<th scope="row"><?php _e( 'Configuration Files', 'qtranslate' ) ?></th>
<td><label for="qtranxs_config_files"
Expand Down
70 changes: 57 additions & 13 deletions modules/qtx_modules_handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,21 @@ public static function load_modules_enabled() {
public static function update_manual_enabled_modules() {
global $q_config;
$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;
}
}

// TODO: workaround to enable slugs from option checkbox, could be generalized to any module for user control
if ( $q_config['slugs_enabled'] && $options_modules['slugs'] != QTX_MODULE_STATUS_ACTIVE ) {
$options_modules['slugs'] = QTX_MODULE_STATUS_ACTIVE;
if($changed){
update_option( 'qtranslate_modules', $options_modules );
self::load_modules_enabled();
do_action('qtx_ma_modules_updated');
}
}

Expand All @@ -57,7 +67,8 @@ public static function get_modules_defs() {
'id' => 'acf',
'name' => 'ACF',
'plugin' => array( 'advanced-custom-fields/acf.php', 'advanced-custom-fields-pro/acf.php' ),
'incompatible' => 'acf-qtranslate/acf-qtranslate.php'
'incompatible' => 'acf-qtranslate/acf-qtranslate.php',
'manual_activation' => false
),
array(
'id' => 'all-in-one-seo-pack',
Expand All @@ -66,48 +77,81 @@ public static function get_modules_defs() {
'all-in-one-seo-pack/all_in_one_seo_pack.php',
'all-in-one-seo-pack-pro/all_in_one_seo_pack.php'
),
'incompatible' => 'all-in-one-seo-pack-qtranslate-x/qaioseop.php'
'incompatible' => 'all-in-one-seo-pack-qtranslate-x/qaioseop.php',
'manual_activation' => false
),
array(
'id' => 'events-made-easy',
'name' => 'Events Made Easy',
'plugin' => 'events-made-easy/events-manager.php',
'incompatible' => 'events-made-easy-qtranslate-x/events-made-easy-qtranslate-x.php'
'incompatible' => 'events-made-easy-qtranslate-x/events-made-easy-qtranslate-x.php',
'manual_activation' => false
),
array(
'id' => 'jetpack',
'name' => 'Jetpack',
'plugin' => 'jetpack/jetpack.php'
'plugin' => 'jetpack/jetpack.php',
'manual_activation' => false
),
array(
'id' => 'google-site-kit',
'name' => 'Google Site Kit',
'plugin' => 'google-site-kit/google-site-kit.php'
'plugin' => 'google-site-kit/google-site-kit.php',
'manual_activation' => false
),
array(
'id' => 'gravity-forms',
'name' => 'Gravity Forms',
'plugin' => 'gravityforms/gravityforms.php',
'incompatible' => 'qtranslate-support-for-gravityforms/qtranslate-support-for-gravityforms.php'
'incompatible' => 'qtranslate-support-for-gravityforms/qtranslate-support-for-gravityforms.php',
'manual_activation' => false
),
array(
'id' => 'woo-commerce',
'name' => 'WooCommerce',
'plugin' => 'woocommerce/woocommerce.php',
'incompatible' => 'woocommerce-qtranslate-x/woocommerce-qtranslate-x.php'
'incompatible' => 'woocommerce-qtranslate-x/woocommerce-qtranslate-x.php',
'manual_activation' => false
),
array(
'id' => 'wp-seo',
'name' => 'Yoast SEO',
'plugin' => 'wordpress-seo/wp-seo.php',
'incompatible' => 'wp-seo-qtranslate-x/wordpress-seo-qtranslate-x.php'
'incompatible' => 'wp-seo-qtranslate-x/wordpress-seo-qtranslate-x.php',
'manual_activation' => false
),
array(
'id' => 'slugs',
'name' => 'Slugs (experimental)',
'name' => __( 'Slugs translation', 'qtranslate' ).sprintf(' (%s)',__( 'experimental' )),
'plugin' => true,
'incompatible' => 'qtranslate-slug/qtranslate-slug.php'
'incompatible' => 'qtranslate-slug/qtranslate-slug.php',
'manual_activation' => true,
'ma_checkbox_text' => __( 'Enable slugs translation.', 'qtranslate' ),
'ma_checkbox_notes' => __( 'This activates the slug translation module. ', 'qtranslate' ).__('Attention! This module is still experimental. It is subject to bugs and limitations.', 'qtranslate' )
)
);
}

public static function ma_modules_default_options(){
$module_defs = self::get_modules_defs();
$response=array();
foreach ( $module_defs as $module ){
if ($module['manual_activation']==true){
$response[$module['id']]=false;
}
}
return $response;
}

public static function get_module_def_by_id($module_id){
$module_defs = self::get_modules_defs();
$response=array();
foreach ( $module_defs as $module ){
if ($module['id']===$module_id){
$response=$module;
break;
}
}
return $response;
}
}
7 changes: 2 additions & 5 deletions modules/slugs/includes/class-qtranslate-slug.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,11 @@ class QtranslateSlug {
*/
function init() {
global $q_config;
if ( ! $q_config['slugs_enabled'] ) {
if ( ! $q_config['ma_module_enabled']['slugs'] ) {
return;
}

$this->options_buffer = get_option( QTS_OPTIONS_NAME );
if ( ! $this->options_buffer ) {
add_option( QTS_OPTIONS_NAME, array() );
}
$this->options_buffer = get_option( QTS_OPTIONS_NAME, array() );
$this->permalink_structure = get_option( 'permalink_structure' );

if ( ! is_admin() ) {
Expand Down
24 changes: 9 additions & 15 deletions modules/slugs/includes/qtranslate-slug-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
add_action( 'admin_head', 'qts_hide_term_slug_box', 900 );
add_action( 'init', 'qts_taxonomies_hooks', 805 );
add_action( 'admin_head', 'qts_hide_quick_edit', 600 );
add_action( 'qtranslate_edit_config', 'qts_updated_settings' );
// plugin deactivation/uninstall
register_deactivation_hook( QTRANSLATE_FILE, 'qts_deactivate' );
register_uninstall_hook( QTRANSLATE_FILE, 'qts_uninstall' );
Expand Down Expand Up @@ -103,20 +102,6 @@ function qts_deactivate() {
$wp_rewrite->flush_rules();
}

function qts_updated_settings() {
global $q_config;

$options_modules = get_option( 'qtranslate_modules', array() );
if ( $q_config['slugs_enabled'] ) {
qts_install();
$options_modules['slugs'] = QTX_MODULE_STATUS_ACTIVE;
} else {
qts_deactivate();
$options_modules['slugs'] = QTX_MODULE_STATUS_INACTIVE;
}
update_option( 'qtranslate_modules', $options_modules );
}

/**
* Creates a metabox for every post type available.
*/
Expand Down Expand Up @@ -558,4 +543,13 @@ function qts_get_terms( $terms, $taxonomy ) {
}

return $terms;
}

function qts_ma_module_updated(){
global $q_config;
if ($q_config['ma_module_enabled']['slugs']){
qts_multi_activate();
}else{
qts_deactivate();
}
}
2 changes: 1 addition & 1 deletion modules/slugs/includes/qtranslate-slug-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function qts_show_form_field( $args = array() ) {
$choices = $args['choices'];
$class = $args['class'];

$options = $qtranslate_slug->options_buffer;
$options = $qtranslate_slug->options_buffer?$qtranslate_slug->options_buffer:get_option( QTS_OPTIONS_NAME, array() );

// pass the standard value if the option is not yet set in the database
if ( ! isset( $options[ $id ] ) && $type != 'checkbox' ) {
Expand Down
1 change: 1 addition & 0 deletions modules/slugs/slugs.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@

// plugin init
add_action( 'plugins_loaded', array( $qtranslate_slug, 'init' ) );
add_action('qtx_ma_modules_updated','qts_ma_module_updated');
add_filter( 'qtranslate_convert_url', 'qts_convert_url', 10, 2 );
6 changes: 3 additions & 3 deletions qtranslate_core.php
Original file line number Diff line number Diff line change
Expand Up @@ -673,11 +673,11 @@ function qtranxf_load_option_array( $name, $default_value = null ) {

// clean up array due to previous configuration imperfections
foreach ( $vals as $key => $val ) {
if ( ! empty( $val ) ) {
if ( isset( $val ) ) {
continue;
}
unset( $vals[ $key ] );
if ( ! empty( $vals ) ) {
if ( isset( $vals ) ) {
continue;
}
delete_option( 'qtranslate_' . $name );
Expand Down 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( 'term_name', array() );

if ( $q_config['filter_options_mode'] == QTX_FILTER_OPTIONS_LIST ) {
Expand Down
2 changes: 1 addition & 1 deletion qtranslate_options.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ function qtranxf_set_default_options( &$ops ) {
'hide_default_language' => true, // hide language tag for default language in urls
'use_secure_cookie' => false,
'header_css_on' => true,
'slugs_enabled' => false,
);

// single line options
Expand Down Expand Up @@ -115,6 +114,7 @@ function qtranxf_set_default_options( &$ops ) {
'filter_options' => QTX_FILTER_OPTIONS_DEFAULT, // array
'ignore_file_types' => QTX_IGNORE_FILE_TYPES, // array
'domains' => null, // array
'ma_module_enabled' => QTX_Modules_Handler::ma_modules_default_options(),
);

// must have function 'qtranxf_default_option_name()' which returns a default value for option 'option_name'.
Expand Down