Automattic / Co-Authors-Plus

Multiple bylines and Guest Authors for WordPress
https://wordpress.org/plugins/co-authors-plus/
GNU General Public License v2.0
287 stars 205 forks source link

On posts list: "Uncaught ReferenceError: coAuthorsPlus_ajax_suggest_link is not defined" #1037

Closed kwisatz closed 2 months ago

kwisatz commented 2 months ago

I'll add more information here as I find any. After the 3.6.0 update, we're seeing this error on the posts list, preventing any other scripts from running:

Uncaught ReferenceError: coAuthorsPlus_ajax_suggest_link is not defined
    at HTMLDocument.<anonymous> (co-authors-plus.js?ver=3.6.0:356:30)
    at HTMLDocument.dispatch (jquery.min.js?ver=3.7.1:2:40035)
    at v.handle (jquery.min.js?ver=3.7.1:2:38006)
    at Object.trigger (jquery.min.js?ver=3.7.1:2:70124)
    at Function.ajax (jquery.min.js?ver=3.7.1:2:76037)
    at Function.<anonymous> (jquery-migrate.min.js?ver=3.4.1:2:4771)
    at e.<computed> [as ajax] (jquery-migrate.min.js?ver=3.4.1:2:1582)
    at HTMLTableSectionElement.update (cpt.js?ver=6.5.2:48:68)
    at x.<computed>.<computed>._trigger (core.min.js?ver=1.13.2:116:7687)
    at x.<computed>.<computed>._trigger (sortable.min.js?ver=1.13.2:9:25015)

Code in question is this:

// Show loading cursor for autocomplete ajax requests
    jQuery( document ).ajaxSend(function( e, xhr, settings ) {
        if ( settings.url.indexOf( coAuthorsPlus_ajax_suggest_link ) != -1 ) {
            // Including existing authors on the AJAX suggest link
            // allows us to filter them out of the search request
            var existing_authors = jQuery( 'input[name="coauthors[]"]' ).map(function(){return jQuery( this ).val();}).get();
            settings.url = settings.url.split( '&existing_authors' )[0];
            settings.url += '&existing_authors=' + existing_authors.join( ',' );
            show_loading();
        }
    });

I can see the variable gets defined here https://github.com/Automattic/Co-Authors-Plus/blob/586e803589a5d3f86de6f04a8391385529b3fbbe/php/class-coauthors-plus.php#L1460 but this code doesn't seem to run for the posts list, at least not in our case.

kwisatz commented 2 months ago

$this->is_post_type_enabled() is returning false in our case, despite the settings now having been touched and showing that the functionality is enabled for Posts.

I've added debug statements and can see that at some point, while the array of enabled post types has been 'post' and 'page' for a while, 'post' suddenly gets dropped. (Just before the closing </head>, which is where the variable would be output).

So I'm guessing the coauthors_supported_post_types filter runs at some point and removes the post post type from the list of supported types.

I will stop here for lack of time. IMHO, even if there is a reason for the filter to remove 'post' from the supported types, the javascript code should make sure that the variable is defined before using it and/or the file should only be enqueued if the post type is supported. The former is probably easier to achieve than the latter.