qtranslate / qtranslate-xt

qTranslate-XT (eXTended) - reviving qTranslate-X multilingual plugin for WordPress. A new community-driven plugin soon. Built-in modules for WooCommerce, ACF, slugs and others.
GNU General Public License v2.0
557 stars 107 forks source link

[ACF] Language Switcher Buttons not showing on ACF options pages #1343

Open hirasso opened 1 year ago

hirasso commented 1 year ago

Describe the bug

On ACF options pages, the language switching buttons do not appear

To Reproduce Steps to reproduce the behavior:

  1. Follow the instructions from ACF (above) to create an options page
  2. Go to said options page
  3. No language switching buttons are being rendered

Expected behavior The language switching buttons should show up as they do for normal post edit screens

Possibly related

798

Debug info Go to <YOUR_SITE>/wp-admin/options-general.php?page=qtranslate-xt#troubleshooting. Press Collect information button and copy/paste the output below.

Versions:
{
  "PHP_VERSION": "8.0.8",
  "WP_VERSION": "6.2.2",
  "QTX_VERSION": "3.15.1",
  "Plugins": [
    "ACF Autosize 2.0.15",
    "Advanced Custom Fields: Unique ID Field 1.0.0",
    "Admin Collapse Subpages 2.4",
    "Admin Post Navigation 2.1",
    "Advanced Custom Fields PRO 6.1.6",
    "Category Checklist Tree 1.3.2",
    "Check &amp; Log Email 1.0.7",
    "Classic Editor 1.6.3",
    "Debug Bar 1.1.4",
    "Yoast Duplicate Post 4.5",
    "Email Address Encoder 1.0.22",
    "Enable Media Replace 4.1.2",
    "Filenames to latin 2.7",
    "Git Updater 12.2.1",
    "iOS Images Fixer 1.2.4",
    "Limit Login Attempts 1.7.2",
    "Loop Post Navigation Links 3.0.2",
    "Post Type Switcher 3.2.1",
    "qTranslate-XT 3.15.1",
    "Regenerate Thumbnails 3.1.5",
    "Relevanssi 4.20.0",
    "Rewrite Rules Inspector 1.3.1",
    "RH Admin Utilities 1.9.0",
    "RH Privacy Consent 1.1.2",
    "RH Shortcodes 1.1.3",
    "RH Updater 1.2.0",
    "Safe SVG 2.1.1",
    "External Links 6.8.1",
    "Simple Page Ordering 2.5.0",
    "YIKES Simple Taxonomy Ordering 2.3.4",
    "Term Management Tools 2.0.1",
    "User Switching 1.7.0",
    "WordPress Importer 0.8.1",
    "Yoast SEO 20.7",
    "Media Library Categories Premium 2.1.1",
    "WP Super Cache 1.9.4",
    "WP-Sweep 1.1.8",
    "WP Sync DB 1.6"
  ]
}
Configuration:
{
  "default_language": "en",
  "enabled_languages": [
    "de",
    "en",
    "es"
  ],
  "locale": {
    "de": "de_DE",
    "en": "en_US",
    "es": "es_ES"
  },
  "date_format": {
    "de": "%A, \\d\\e\\r %e. %B %Y",
    "en": "%A %B %e%q, %Y",
    "es": "%d \\d\\e %B \\d\\e %Y"
  },
  "time_format": {
    "de": "%H:%M",
    "en": "%I:%M %p",
    "es": "%H:%M hrs."
  },
  "url_mode": 2,
  "use_strftime": 3,
  "filter_options_mode": 0,
  "language_name_case": 0,
  "detect_browser_language": false,
  "hide_untranslated": false,
  "show_menu_alternative_language": false,
  "show_displayed_language_prefix": false,
  "show_alternative_content": false,
  "hide_default_language": false,
  "use_secure_cookie": false,
  "header_css_on": false,
  "text_field_filters": [],
  "disable_client_cookies": false,
  "url_info": {
    "cookie_lang_front": "en",
    "cookie_lang_admin": "en",
    "cookie_front_or_admin_found": true,
    "pagenow": "admin-ajax.php",
    "REQUEST_METHOD": "GET",
    "WP_ADMIN": true,
    "WP_DOING_AJAX_POST": [],
    "scheme": "https",
    "host": "example.test",
    "path": "/core/wp-admin/admin-ajax.php",
    "query": "action=admin_debug_info",
    "path-base": "/core",
    "wp-path": "/wp-admin/admin-ajax.php",
    "language_neutral_path": true,
    "http_referer": "https://example.test/core/wp-admin/options-general.php?page=qtranslate-xt",
    "referer_admin": true,
    "doing_front_end": false,
    "lang_cookie_admin": "en",
    "lang_admin": "en",
    "language": "en",
    "set_cookie": false
  },
  "language": "en",
  "editor_mode": 0,
  "highlight_mode": 0,
  "auto_update_mo": true,
  "hide_lsb_copy_content": true,
  "lsb_style": "custom",
  "config_files": [
    "./i18n-config.json"
  ],
  "custom_fields": [],
  "custom_field_classes": [],
  "post_type_excluded": [
    "attachment",
    "oembed_cache",
    "user_request",
    "acf-field-group",
    "acf-field"
  ],
  "admin_enabled_modules": {
    "acf": true,
    "wp-seo": true,
    "all-in-one-seo-pack": false,
    "events-made-easy": false,
    "jetpack": false,
    "google-site-kit": false,
    "gravity-forms": false,
    "woo-commerce": false,
    "slugs": false
  },
  "qtrans_compatibility": false
}
moskalukigor commented 1 year ago

I was able to fix it using js code. Consider that your option page urls should start with acf-

functions.php

function qtx_option_fix( $hook ) {
    if ( str_contains($hook ,'acf-') ) {
        wp_enqueue_script( 'qtx-option-fix', get_stylesheet_directory_uri() . '/js/qtx-option-fix.js', array(), '1.0' );
    }
}
add_action( 'admin_enqueue_scripts', 'qtx_option_fix' );

qtx-option-fix.js

$(document).ready(function () {

    //set timeout to wait for qTranslate to load
    setTimeout(function () {
        //get qTranslateX config
        var qtx = qTranslateConfig.js.get_qtx();
        qtx.addDisplayHookAttrs(document.getElementById('post'), qtx);
        qtx.setupLanguageSwitch();
    }, 1000);

});

I understand that this is only a temporary solution, but it works for me now.

Because if I add the LSB via qtranslate-config.json, then when saving the repeater type field - I get a fatal error that refers to qtranxf_isMultilingual (Argument #1 ($str) must be of type ?string, array given)

herrvigg commented 1 year ago

The initialization sequence became very tricky since jQuery3, see https://github.com/qtranslate/qtranslate-xt/blob/ba0b8797f4a32aed93c6d9047138417edd1cc50b/js/core/index.js#L18C1-L20

// With jQuery3 ready handlers fire asynchronously and may be fired after load.
// See: https://github.com/jquery/jquery/issues/3194

QTX will not create the LSB if there are no display or content hooks defined when the initialize() function is called. With the ACF options pages I see two situations:

  1. if you don't have any translatable item in an ACF group linked to the option page, I think it's normal that the LSB are not shown. Otherwise why would you need the LSB?
  2. If you have any ACF group linked to the option with translatable items then yes it's a bug.

Are you in case 1 or 2?

moskalukigor commented 1 year ago

Are you in case 1 or 2?

Personally, I have the second case.

hirasso commented 1 year ago

I'm also in case 2. I have several translatable fields.

herrvigg commented 1 year ago

Then it's a bug, though I don't manage to reproduce it. But this can depend on the event sequence as mentioned above or coming from recent updates in ACF 6 Pro. What type of fields is it about? The repeater field is broken, see #882.

herrvigg commented 1 year ago

Because if I add the LSB via qtranslate-config.json, then when saving the repeater type field - I get a fatal error that refers to qtranxf_isMultilingual (Argument #1 ($str) must be of type ?string, array given)

The QTX built-in configuration for ACF is supposed to handle the option page without requiring any change from your side. What did you mean by adding the LSB to the json file?

To help understand the bug - for both of you:

  1. what is the option page URL, is it something like this? https://<your-site>/wp-admin/admin.php?page=acf-options
  2. in a console please type this and give the result: JSON.stringify(qTranslateConfig.page_config) (nothing confidential)
  3. which type of fields do you have linked to the option page?
herrvigg commented 1 year ago

Could try again on master? I pushed another fix for #1342 commit https://github.com/qtranslate/qtranslate-xt/commit/7af9a31050de528112b4439754047aae8285e607. That is setting the LSB at the end from the script for ACF. If it solves the problem I still would like to understand better what is going on so the questions above are valid in any case.

hirasso commented 1 year ago

I'm AFK this week, can check it out next week. Thank you so much for looking into it, @herrvigg 🙏

hirasso commented 1 year ago

It works with the current master! 🎉