• Resolved Ward

    (@yward)


    The plugin works great in checkout page, however it causes errors in every other page:

    
    InvalidValueError: not an instance of HTMLInputElement js:63:128
    TypeError: $(...).select2 is not a function
    checkout-address-autofill-for-woocommerce//assets/js/autofill.js:8

    This jQuery error breaks other jQuery functionality.

    Is there is a way to prevent plugin resources from loading outside of checkout page?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author ZetaMatic

    (@zetamatic)

    Hi @yward,

    We are checking the issue, once done will update to you.

    Plugin Author ZetaMatic

    (@zetamatic)

    Hello @yward,

    We have fixed the issue and released an updated for the plugin. Kindly check the updated version plugin.

    Please share your review if you liked our plugin or support so far.

    Thread Starter Ward

    (@yward)

    Sadly your update didn’t solve the problem, however I have solved with the following method:
    1- I deactivated the plugin.
    2- I wrote this code to activate it only when visiting /checkout/ page.

    The code is used as a MU plugin:

    
    function lwp_plugin_control($plugins)
    {
        $lwp_controlled_plugins = wp_cache_get('lwp_controlled_plugins', 'plugins');
        if ($lwp_controlled_plugins !== false) {
            return $lwp_controlled_plugins;
        }
        wp_cache_set('lwp_original_plugins', $plugins, 'plugins');
    
        // Enable plugins on certain URLs
        $enabling_rules = array(
            'checkout-address-autofill-for-woocommerce/checkout-address-autofill-for-woocommerce.php' => array(
                '/checkout/'
            )
        );
        // Run enables
        $plugins = array_unique(
            array_merge($plugins, lwp_plugins_affected_by($enabling_rules))
        );
        wp_cache_set('lwp_controlled_plugins', $plugins, 'plugins');
        return $plugins;
    }
    add_filter('option_active_plugins', 'lwp_plugin_control');
    
    /**
     * Scan for affected plugins to manipulate
     *
     * @param array $rules Plugin files paied with URL rules
     * @return array Affected plugins by their dir + file name
     */
    function lwp_plugins_affected_by($rules)
    {
        $affected = array();
        $current_path = add_query_arg(array());
        foreach ($rules as $plugin => $paths) {
            // If any of the paths match the current path
            $matches = array_filter(
                $paths,
                function ($path) use ($current_path) {
                    return (
                        $path === $current_path ||
                        preg_match('%'.$path.'%', $current_path)
                    );
                }
            );
            if (empty($matches)) {
                continue;
            }
            $affected[] = $plugin;
            add_filter('plugin_action_links_'.$plugin, 'lwp_add_action_links');
        }
        return $affected;
    }
    
    /**
     * Show a red message if a plugin is affected by the MU plugin
     * It explains why it's not possible to change it by hand
     *
     * @param array $links Links in the plugin row, like "Activate"
     * @return array Changed links to show in the plugin row
     */
    function lwp_add_action_links($links)
    {
        unset($links['activate'], $links['deactivate']);
        array_unshift($links, '<span style="color:red;">Controlled by MU!</span>');
        return $links;
    }
    
    /**
     * Prevent saving controlled plugin states by 3rd party plugins
     *
     * @param array $new_value New list of active plugins
     * @param array $old_value Old list of active plugins
     * @return array New list of active plugins
     */
    function lwp_prevent_saving_plugins($new_value, $old_value)
    {
        $lwp_controlled_plugins = wp_cache_get('lwp_controlled_plugins', 'plugins');
        $lwp_original_plugins = wp_cache_get('lwp_original_plugins', 'plugins');
    
        sort($old_value);
        sort($new_value);
        sort($lwp_controlled_plugins);
    
        if (($new_value === $old_value) &&
            ($old_value === $lwp_controlled_plugins)) {
            return $lwp_original_plugins;
        }
        return $new_value;
    }
    
    add_filter(
        'pre_update_option_active_plugins',
        'lwp_prevent_saving_plugins', 10, 2
    );
    Plugin Author ZetaMatic

    (@zetamatic)

    Hello @yward,

    I know you did a workaround to fix the issue. But the updated version plugin is working fine in the Demo site. You can check it here: https://demo.zetamatic.com. The Select2 issue is not there.

    Can you please share your site URL? So that I can check the console error.

    Thanks

    Thread Starter Ward

    (@yward)

    I’ll private message you a staging site link once I’m home to reproduce the issue.
    No worries I have rated your plugin positively regardless.

    Thank you for the follow up!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Console Errors’ is closed to new replies.