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

Add new ACF settings to select extended qTranslate types #1300

Merged
merged 1 commit into from
Mar 25, 2023
Merged
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
84 changes: 81 additions & 3 deletions src/modules/acf/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class QTX_Module_Acf_Admin {
*/
public function __construct() {
add_action( 'acf/input/admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
add_filter( 'acf/get_field_types', array( $this, 'get_field_types' ) );
add_action( 'admin_head', array( $this, 'admin_head' ) );
add_action( 'admin_init', array( $this, 'admin_init' ) );

Expand Down Expand Up @@ -41,6 +42,51 @@ public static function group_sub_fields() {
];
}

/**
* Return the qtranslate fields e.g. post or page.
* @return array
*/
public static function qtranslate_fields() {
return [
'qtranslate_file',
'qtranslate_image',
'qtranslate_post_object',
'qtranslate_text',
'qtranslate_textarea',
'qtranslate_url',
'qtranslate_wysiwyg',
];
}

/**
* Filter the field types to exclude the extended fields that have been deactivated in settings.
*
* @param array $groups
*
* @return array
*/
public function get_field_types( $groups ) {
if ( ! isset ( $groups[ QTX_Module_Acf_Register::ACF_CATEGORY_QTX ] ) ) {
return $groups;
}
$fields = array_keys( self::qtranslate_fields() );
$default = array_fill_keys( array_keys( $fields ), false );
$settings = self::get_module_setting( 'qtranslate_fields', $default );
$is_active = function ( $field ) use ( $settings ) {
return isset( $settings[ $field ] ) && $settings[ $field ];
};
$group_qtx = &$groups[ QTX_Module_Acf_Register::ACF_CATEGORY_QTX ];
foreach ( $group_qtx as $field_id => $field_object ) {
if ( ! $is_active( $field_id ) ) {
unset( $group_qtx[ $field_id ] );
}
}
if ( empty( $group_qtx ) ) {
unset( $groups[ QTX_Module_Acf_Register::ACF_CATEGORY_QTX ] );
}
return $groups;
}

/**
* Load javascript and stylesheets on admin pages
*/
Expand Down Expand Up @@ -190,6 +236,7 @@ public function filter_qtranslate_admin_config( $config ) {
* Retrieve the value of a setting for the ACF module.
*
* @param string $name
* @param mixed $default
*
* @return mixed
*/
Expand Down Expand Up @@ -230,13 +277,22 @@ public function admin_init() {
'section-acf-standard'
);

// Extended fields (ACF-QTX overridden fields)
// Extended fields (overridden "qTranslate-XT" fields)
add_settings_section(
'section-acf-extended',
__( 'Extended fields', 'qtranslate' ),
'',
__( 'Extended qTranslate fields', 'qtranslate' ),
function () {
_e( 'Additional types with a specific handling and rendering for qTranslate. Prefer the standard ACF fields as much as possible.', 'qtranslate' );
},
'settings-qtranslate-acf'
);
add_settings_field(
'select_qtranslate_fields',
__( 'Active types', 'qtranslate' ),
array( $this, 'render_setting_qtranslate_fields' ),
'settings-qtranslate-acf',
'section-acf-extended'
);
add_settings_field(
'show_language_tabs',
'Display language tabs',
Expand Down Expand Up @@ -300,6 +356,28 @@ public function render_setting_group_sub_fields() {
<?php endforeach;
}

/**
* Render setting
*/
public function render_setting_qtranslate_fields() {
$fields = self::qtranslate_fields();
$default = array_fill_keys( array_keys( $fields ), false );
$settings = self::get_module_setting( 'qtranslate_fields', $default );
foreach ( $fields as $id ):
$acf_type = acf()->fields->get_field_type( $id );
if ( ! isset( $acf_type ) ) {
continue;
}
?>
<label>
<input type="checkbox"
name="<?php echo QTX_OPTIONS_MODULE_ACF ?>[qtranslate_fields][<?php echo $id ?>]" <?php checked( isset( $settings[ $id ] ) && $settings[ $id ] ); ?>
value="1"/><?php echo $acf_type->label ?>
</label>
<br/>
<?php endforeach;
}

/**
* Render setting
*/
Expand Down