Support » Plugin: Query Monitor » Coherence problem between plugin lwa (login with ajax) and query-monitor

Viewing 5 replies - 1 through 5 (of 5 total)
  • A little complementary comment :

    After loading first the login page while you are not logged in as admin the Query Monitor results will not naturally be displayed.

    To display the Query Monitor report you need to fill your credentials as admin and submit.

    But after the display of the Query Monitor report you can check the user : even it has been temporarily connected (display of Query Monitor report) it has been disconnected after the load…

    What happens ?

    Best regards

    Trebly

    Trebly

    (@trebly)

    Hi,

    Did you read my message ?

    note : I uses Chrome.

    Is-it a problem to reply to my question ?

    There is a true problem and a partial solution found.

    I have a quick summary :

    • When after a crash an admin url is submitted (automatic by browser context restauration) the “login page is displayed. In this case, because QM is defined to operate only with admin user, QM is not activated and doesn’t display any report
    • When you login from the previous “login page”, normally, if successful, you are redirected the the admin page which url was given to the browser at beginning. But because QM is then activated (login successful) QM report is loaded and the redirection is not performed. Then you get a “login page” from which nothing can be done : new try with the login submit will fail and in fact will disconnect you while you were logged in ; and there is no way to redirect while (verified) you are connected. It is a dead lock (for the current tab and all others tab which display at same time this same login page unusable).

    In this are implicated :

    • WP login process
    • QM : the alone case which breaks the login page redirection and for which WP login page cannot function. Practically if the login doesn’t use “Login with Ajax” QM report is never displayed (the redirection is performed before QM report can be displayed)
    • Plugin login with Ajax which can workaround the problem

    I have developed an add-on to LWA which allows to redirect to original url. This solves the problem for me but it remains alive, I ma sure of this, for others WP users who uses simultaneously LWA and QM.

    Best regards

    Trebly
    _________________________________________________________________________________
    note : The previous message was very long because I had not found the exact problem. It is not simple to solve such a problem fully alone.

    Marcus

    (@netweblogic)

    NetWebLogic Support

    If you can paste your add-on code that fixes the issue here, that might help us understand the issue better than your explanation, and if the fix makes sense, I could add it to the plugin.

    I myself cannot spend time on debugging an issue that I could not reproduce before (if I remember correctly), and also will affect such a small handful of developers that require this functionality to work specifically in a dev environment with both these plugins active. Not that I don’t want to, but I have a limited set of time and therefore must choose the best things to spend that time on that’ll serve the greatest number of people.

    Hi,
    I think that to reproduce is easy.
    Nevertheless I perfectly understand your position regardless this problem, I have the same time use problem.

    Best regards

    Trebly
    _____________________________________________________________________________
    This is the add-on, the js does everything :

    If QM is not present this will have no effect.
    The code js and the css are set into a subdir named “Trebly_login_ext”.

    To add into public static function init() the enque js and css

    // BEGINadd-on by Trebly script and css
                wp_enqueue_script( "trebly-add-on-login", plugin_dir_url( __FILE__ ) . 'Trebly_login_ext/function_login_ext.js', array( 'jquery' ), 'F2-1c5-b80402-1');
                wp_enqueue_style( "trebly-add-on-login", plugin_dir_url( __FILE__ ) . 'Trebly_login_ext/function_login_ext.css', array(), '2.12.1g-b80402');
                // ENDadd-on Trebly
    /**
     * Created by bty-a on 21/03/2018.
     */
    
    // Management of user login
    /*
     The purpose of this section is to manage the case when after displaying the logging page an admin user has been connected by another mean.
     if you fill again the form with admin IDs and query monitor is activated you will get a loop. This is because query monitor will display his results for the new submission, again.
     At a moment you must break the loop.
     This can be done by checking the user connection without submitting the form.
     To check the user connection where going to use an AJAX request.
     If the result is that an admin is connected, then the link "redirect_to (url)" can be used.
     If an admin user is not activated(logged in) the link would lead to a new logging page.
     If query monitor is not activated, after this submission of the form you will reach the redirected_to url.
     But if there are several logging pages you will have to repeat the filling of logging data for each one.
     This is the reason why we have defined to elements added to the logging page:
     - a button to check the user
     - a link to redirect the logging page to the redirect_to url
     With this the user can directly activate the link if he is sure that it is logged in
     Accordingly with the results of the user check the title of the link will be adapted for an accurate warning.
     For now this is added to the plugging query monitor.
     But this could be added to simple login.
     */
    
    (function( $ ) {
    
    $(document).ready(function () {
        if (window.location.href.indexOf("wp-login.php") != -1 && window.location.href.indexOf("%2Fwp-admin") != -1) {
            var data_admin_user_log = {
                action: 'is_admin_user_logged_in' // name of action declared into php ajax function for checking the admin logged in
            };
    
    // some xxT text vars and flag to manage context
            var loggedinT ="";
            var checkuserT = '';
            var isloggedin_admin=false;
            var check_flipflop = false;
            var l_ajaxurl = "";
    
            // var ajaxurl=window.ajaxurl;
           // var ajaxurl=qm_l10n.ajaxurl; /* todo ###b80320 - set a value from wp not necessarily qm_110n to use the function out of qm context in the future */
            if( typeof LWA !== 'undefined' && LWA.ajaxurl !="" ) { l_ajaxurl = LWA.ajaxurl;
            } else {
                alert('Invalid context to use Ajax\n some functions can not to be operational');
            }
    
             /**
             * The function is not used split into the ajax command
             */
            var get_user_status = function() {
                var response = "";
                if (l_ajaxurl !="") {
                    $.post(l_ajaxurl, data_admin_user_log, function(response) {
                        if(response.indexOf('yes')!= -1 ) {
                            // user is logged in
                            loggedinT = "You are logged in as admin : the link \"redirect to\" is activable";
                            alert("Content returned by Ajax request : \n["+ response +']');
                            check_flipflop = !isloggedin_admin; // set to true if isloggedin_admin was false
                            isloggedin_admin=true;
                            checkuserT = " the user has been already checked as admin from here, repeat the check?";
    
                        } else {
                            // user is not logged in
                            loggedinT = "You are not logged in this link cannot be activated it would lead to a new login page (ajax answer is : ["+response+"]";
                            alert("Content returned by Ajax request : ["+ loggedinT+']');
                            check_flipflop = isloggedin_admin; // set to true if isloggedin_admin was true
                            isloggedin_admin=false;
                            checkuserT = " the user has been already checked not as admin from here, repeat the check ?"
                        }
                    });
                } else {
                    alert('Invalid context to use Ajax\nundefined local ajaxurl (l_ajaxurl)\n : unable to get user status');
                }
            };
            /*
             Insert new elements : two buttons and the "redirect_to" link
             */
            // document useful elements
            var loginpanel = document.getElementById("login");
            var backtoblog = document.getElementById("backtoblog");
            var linkback = document.getElementsByName("redirect_to");
            var response ="";
    
            // if (qmdiv !=null && qmwrapper != null && qmtitle0 !=null && linkback != null) {
            if (backtoblog != null && loginpanel != null && linkback != null) {
     
                var loginform0 = document.getElementById('loginform');
                loginform0.style.backgroundColor = "lightgrey";
    
                var warning0 = document.createElement('span');
                warning0.innerHTML = "WARNING : this form cannot be used to login - you must use the \"clean login\" button";
                warning0.className = 'warning0';
                loginpanel.insertBefore(warning0,loginpanel.childNodes[0]);
    
                var loginAddDiv= document.createElement('div');
                loginAddDiv.className = "login-ext-div";
                loginpanel.appendChild(loginAddDiv);
    
                var margin0 = document.createElement('span');
                margin0.innerHTML = " - ";
                loginAddDiv.appendChild(margin0);
                // Added b80320 - a button to check the user
     
                var buttonUser0 = document.createElement("button");
                buttonUser0.id = "checkUser0";
                buttonUser0.type = "button";
                buttonUser0.value = "User ?";
                buttonUser0.className = "login-ext-elmts";
                buttonUser0.title = "Check if an admin user is logged in";
                buttonUser0.innerHTML = 'Check User'; //+checkuserT;
                loginAddDiv.appendChild(buttonUser0);
     
                var margin3 = document.createElement('span');
                margin3.innerHTML = " - ";
                loginAddDiv.appendChild(margin3);
    // Added b80320-21 - a link to launch the clean login but this doesn't function without explanation (in WP ?) see new version
     
                var linkLogin0 = document.createElement("a");
                linkLogin0.id = "login0";
                linkLogin0.type = "link";
                linkLogin0.href = window.location.href.substr(0,window.location.href.indexOf("?"));
                linkLogin0.target = "_blank" ; // a new tab to not forget the redirect_to link
                linkLogin0.value = "Login";
                linkLogin0.className = "login-ext-elmts";
                linkLogin0.title = "Launches a clean login in a new tab \n note that the current form cannot be used to login";
                linkLogin0.innerHTML = 'Clean login';
                loginAddDiv.appendChild(linkLogin0);
    
                var margin1 = document.createElement('span');
                 margin1.innerHTML = " - ";
                loginAddDiv.appendChild(margin1);
    
                var linkback0 = document.createElement("a");
                linkback0.id = "redirect_to";
                linkback0.href = linkback[0].value ;
                linkback0.className = "login-ext-elmts";
                linkback0.innerHTML = 'Back to initial request'; // : testing version with returned string by Ajax ='+"["+response+"]";
                linkback0.remark = "This link can redirect to the original url if an admin user or a user which can see query monitor results\n"+
                    "is logged in and if the link is directed to an accessible target for the a user which has been connected by another mean\n"+
                    "than this login page. If you are not sure about the connection check the current logged in by using the dedicated button";
                // loginpanel.childNodes[1].insertAdjacentElement('beforebegin', linkback0); // added by default at the end of div login
                linkback0.title=linkback0.remark;
                loginAddDiv.appendChild(linkback0);
    
            }
    
            /*
             Action on event on new elements
             */
    
            $("#checkUser0")
                .click(function(the_event){
                    the_event.stopImmediatePropagation();
                    $.post(l_ajaxurl, data_admin_user_log, function(response) {
                        // todo ####b80321 here sanitize and filter ajax response
    
                        // Filter the response
                        const regex = /\[ulogbegin\](.*)?\[ulogend\]/g;
                        m = regex.exec(response);
                        if(m !=null && m[1] != null && m[1].indexOf('[yes]')!= -1 ) {
                            // user is logged in, 
                            alert ('An admin is logged in');
                            loggedinT = "You are logged in as admin : the link \"redirect to\" is activable";
                           // alert("Content returned by Ajax request : \n["+ response +']');
                            check_flipflop = !isloggedin_admin; // set to true if isloggedin_admin was false
                            isloggedin_admin=true;
                            checkuserT = " the user has been already checked as admin from here, repeat the check?";
                            // todo ###b80321 create a button to display the remark
                            linkback0.title = 'The user has been checked here as admin the link can normally be used.\n'+linkback0.remark;
    
                        } else {
                            // user is not logged in,
                            alert("There is no admin user logged in");
                            loggedinT = "You are not logged in this link cannot be activated it would lead to a new login page"; // (ajax answer is : ["+response+"]";
                           // alert("Content returned by Ajax request : ["+ response+']');
                            check_flipflop = isloggedin_admin; // set to true if isloggedin_admin was true
                            isloggedin_admin=false;
                            checkuserT = " the user has been already checked not as admin from here, repeat the check ?" ;
                            linkback0.title = 'The user has NOT been checked here as admin, the link can, may be, not to reach his target.\n'+linkback0.remark;
                        }
                    });
    
                  });
    
            $("#redirect_to")
                .click(function(the_event){
                    if (isloggedin_admin == true){
                        return true;
                    } else {
    
                        if (confirm("BE CAREFUL : You must be logged in as admin to activate this link successfully\n" +
                            "The user as not been checked from this page\nConfirm") == true) {
                            return true;
                        } else {
                            the_event.stopImmediatePropagation();
                            return false;
                        }
                    }
                })
                .hover( function(){
                    if (check_flipflop) { // do something if value have changed
                        if (isloggedin_admin ){
                            $(this)[0].title = loggedinT; // todo ###introduce changing the message for title depending of user status
    
                        } else {
                            $(this)[0].title = loggedinT;
                        }
                    }
                    return true;
                });
            
        } // if url login.php
    });
    
    }) ( jQuery ); /* jQuery */

    To add css file

    /**
    Author : Trebly b80322
     */
    /**
    *      - added style for display of function_login_ext.js : extension of LWA login for login.php page
    */
    
    .login-ext-div { /* the whole div for button and link extend */
        /* No special attributes defined b80322 */
    }
    .warning0 {
        display: block;
        margin-bottom: 2em !important; /* if not should be overlapped by ".login" */
        color: red;
        font-weight: 800;
        text-align: center;
    }
    
    .login-ext-elmts {
        /* uses temporarily the standard to display the button and links into the div .login-ext-div */
    }

    Hi,

    First I apologize.
    When I redact the message I was as feeling to answer to caimin_nwl (@caimin_nwl) because the problem is quite symmetric.

    The enhanced code has been added into login with ajax plugin (LWA). So for QM it must be slightly modified to fit the context (the check for example of the ajaxurl).

    I have forgotten a little but essential piece of code : the function which answers and the ajax actions, here is :

        function ajax_check_admin_user_logged_in() {
            $isit = (is_user_logged_in() && is_admin())?'yes':'no';
            // todo ###b80321 the response of ajax contains often other elements, this problem is to solve. Temporarily a filter acts on the string between the delimiters : [ulogbegin]<the answer>[ulogend]
            echo ("[ulogbegin][Check admin user ended with : [".$isit)."]][ulogend]";
            wp_die();
        }
        add_action('wp_ajax_is_admin_user_logged_in', 'ajax_check_admin_user_logged_in');
        add_action('wp_ajax_nopriv_is_user_logged_in', 'ajax_check_admin_user_logged_in'); // For case of a not logged in user (visitor)

    About the subject : is it a very particular case, I can tell this.

    LWA provides a perfect widget for login and there are many users which uses it.

    Then when you are developing QM is a major tools, so I do not think that the situation to have both running is exceptional.

    There is no other condition else than to have to log with the main WP login page (particularly when you reconnect after browser restart on several previously opened admin pages). In such condition there no other solution than mine to reload successfully the right “pages” (several admin urls), this restores the work context (for example remember that you where modifying widgets or menu or…and… when you had to restart accidentally or decide to stop for the night).

Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.