MySLP_REST_API Ajax Not Initialized

Generate Embed is not populating the embed map as the REST API call is generating an internal error.

The MySLP_REST_API is expecting the SLP AJAX property to be set and it is null.

Related Call Stack

class.myslp.rest.api.php:438, MySLP_REST_API->get_options()
class.myslp.rest.api.php:279, MySLP_REST_API->get_map_options()
MySLP.php:257, MySLP->rest_dispatch_request_filter()
class-wp-hook.php:324, WP_Hook->apply_filters()
plugin.php:205, apply_filters()
class-wp-rest-server.php:1187, WP_REST_Server->respond_to_request()
class-wp-rest-server.php:1041, WP_REST_Server->dispatch()
class-wp-rest-server.php:431, WP_REST_Server->serve_request()
rest-api.php:424, rest_api_loaded()
class-wp-hook.php:324, WP_Hook->apply_filters()
class-wp-hook.php:348, WP_Hook->do_action()
plugin.php:565, do_action_ref_array()
class-wp.php:418, WP->parse_request()
class-wp.php:813, WP->main()
functions.php:1336, wp()
wp-blog-header.php:16, require()
index.php:17, {main}()

Dev Notes

Stack tracing…

  1. On the initial call the store-locator-plus.php MUP is called first an is loading with the initMySLP _jsonp call from the embed script.
  2. the SLPlus::initialize_after_plugins_loaded() is being called.
    • DOING_AJAX is not set, so createobject_AJAX() is not called.
  3. MySLP_REST_API->get_options() is called, which expects SLP->AJAX to be set, but it is not.

The front-end/locations.js is using jQuery.ajax() but it is calling a REST API url, not an AJAX listener endpoint… from the locations.js call…

Update the MySLP_REST_API->get_options() method to use SLP_Ajax::get_instance() to ensure that object is instantiated before use when not doing an AJAX call.

Use the main MySLP::getUserBlogId() to fetch blog IDs.
Use SLP_Ajax::get_instance() versus the uninitialized slplus->AjaxHandler to get the SLP AJAX instance.

slplus->AJAX (slplus->AJAX_Handler) was not initialized because the call is a REST call not an AJAX call.

SaaS Development / ECS Error establishing a database connection

Check The Server URL

For local development using composer the Docker/Composers/Secrets/docker-compose-rds-secrets.yml file will have the DB Host URL.

For ECS deployments the URL is in the Task Definition that is being run by the service. After updating the task you will want to deploy it to the ECS Service with Force Deployment and update the running service.

Check The Port

Newer database connections are not connecting on the default MySQL Port 3306 and instead use a different port. Newer systems use port 3309 for the development database server per the RDS database setup example.

Edit the Docker/Composers/Secrets/docker-compose-rds-secrets.yml file and add the port to the end:

0 comments on “Debugging: Excess Option Lookup”

Debugging: Excess Option Lookup

An excess number of option value lookups was discovered while testing Store Locator Plus® 5.13.8 where the option/label_directions was searched multiple times for a single map render. To reduce load on servers this should only happen once, at the start of the initial map rendering.

This is not a blatant “it did something wrong” or “didn’t do something it should” type of bug. This is a performance and resource usage bug and may require some new architcture.

The manifestation:

Debugging path… investigation

Looking for label_directions in the code…

The initial search through the plugins reveals places the settings are created, set, stored, and managed but not the “JavaScript hook” being fired via /wp-json (the REST API for WordPress). So we need to look deeper.

Looking for options/ in the code…

Let’s try the start of the REST API path after the generic /wp-json/store-locator-plus/v2/ part…

We can ignore the assets/ directory… but what else is in here…

Ahhh… the slp_core.js call to slplus.rest_url + ‘options/’ + attribute looks sus…a

Solution (planned for SLP 5.14)

There was a problem in slp_core.js with value testing.

Turns out an empty value causes multiple REST calls. The following is an invalid test to see if a property exists, which is what we want here. Previously it was checking the value was SET and had a “non-falsy” value. In JavaScript lots of things are “kind of true” or “kind of false” (aka Falsy), for example the empty string “” is FALSE. That is not what we want here.

In slp_core.js we ant to replace the if (!<var>) with if (! var.hasOwnProperty()) …

Cause

The call to /v2/options/label_directions was firing once for every location because the default value for the directions label is empty (“”). This was evaluating to false, which forced the SLP JavaScript to query the server to get the value, which set it to “”.

For each location that was rendered the setting was checked… “is the label for directions set?” or more accurately “Is the label for directions set and NOT empty?”. Every time it came back saying “no, it is NOT set” or rather “It IS empty” … so the code would then go ask the REST API server for the label value.

Rinse and repeat.

0 comments on “Debugging: Location Category Not Filtering”

Debugging: Location Category Not Filtering

The User Action : Filtering Locations With Category on the Testing : Location Categories article is not limiting output to only the specified category.

Tech Overview

The front end sends a request back to WordPress via AJAX to request a list of locations. Part of that request encodes all of the form field entries, including the category drop down selection, into a query-encoded (key/value pairs with an & delimiter) string that is send in the formData property to the backend.

The category ID is not being parsed and filtered properly.

Looking At The Data Request

Via browser developer tools and the network I/O inspector, XHR filter…

HTTP Method: POST
URL: /wp-admin/admin-ajax.php
Request Payload…

You can see the formdata property in the POST body along with the encoded form data including the cat=5 entry. This is where the requests tells the back end to limit results to those that have WordPress category ID #5 attached to the location.

Diving Into The Code

On the back end the AJAX is routed through the Store Locator Plus® base plugin via the standard WordPress hooks and filters. These are setup in the SLP_AJAX class via this add_ajax_hooks method:

    /**
     * Add our AJAX hooks.
     *
     * @uses \SLP_AJAX::slp_delete_location for the AJAX 'slp_delete_location' action
     */
    public function add_ajax_hooks() {

    	// -- priv (WP logged in) and no priv (guest) AJAX calls
        add_action( 'wp_ajax_csl_ajax_search', array( $this, 'csl_ajax_search' ) );
        add_action( 'wp_ajax_nopriv_csl_ajax_search', array( $this, 'csl_ajax_search' ) );

        add_action( 'wp_ajax_csl_ajax_onload', array( $this, 'csl_ajax_onload' ) );
        add_action( 'wp_ajax_nopriv_csl_ajax_onload', array( $this, 'csl_ajax_onload' ) );

	    // -- priv only AJAX calls (WP logged in)
	    add_action( 'wp_ajax_slp_delete_location', array( $this, 'slp_delete_location' ) );
        add_action( 'wp_ajax_slp_change_option', array( $this, 'slp_change_option' ) );
    }

Where the csl_ajax_search eventually finds its way to the find_locations method.

Debugging the find_locations method it looks like the formdata variable coming in via the superglobal $_REQUEST variable is not being set properly.

Digging Into The Malformed Variables

Looking at the call stack the form data is mishandled causing the & in the query string to be encoded as &amp, this causes wp_parse_args() to set the property name incorrect as you can see in the debugging screenshot below.

Turns out the wp_kses_post() is too aggressive with the data munging and converts & to &amp; which then throws wp_parse_args() for a loop.

Instead we need to use esc_url_raw() to leave most URL entities intact.

0 comments on “Debugging : Location Category Dropdown Not Rendering”

Debugging : Location Category Dropdown Not Rendering

Read the Testing : Location Categories article for background on this feature.

The setting to display the category selector on the front end stopped working. The Power add on is active, but with SLP 5.13.X prerelease the drop down no longer appears.

This is very likely due to escaping of output that is overly aggressive on what it filters out on the front end.

Tech Overview

The base plugin renders the map , search form and results on the front end using shortcodes. The [slplus] shortcode renders the entire UX based on a combination of HTML and secondary shortcodes. These secondary shortcodes are parsed by the SLP base plugin to do things like “put the search form here” or “put the results here”. Within each of those custom shortcodes is another set of shortcodes, for example for the search form “put the address input box here” or “put the radius selector here”.

One of those additional shortcodes is provided by the Power add on to “put the category selector drop down here”.

That is not being processed.

Code Path

SLP_Power_UI.php

This attaches the method for rendering additional SLP search form features. This method is getting called.

public function add_hooks_and_filters() 
...
add_filter( 'shortcode_slp_searchelement'   , array( $this , 'process_slp_search_element_shortcode' )   );

This method is not getting called when the [slplus] shortcode is rendered…

public function process_slp_search_element_shortcode( $attributes ) {

The shortcode_slp_searchelement filter is only fired from this piece of code:

SLP_UI.php

Provided by the base plugin, this handles most of the front end initialization and execution.

This line of code is being called to apply filters…

public function create_SearchElement( $attributes, $content = null ) {
$attributes = apply_filters( 'shortcode_slp_searchelement', $attributes );

Checking Call Order

⚠️ Evaluating the call execution order, the above apply_filters is being called BEFORE the power add on has run the SLP_POWER_UI add_hooks_and_filters() , noted above, to hook onto that filter call.

Power Add Hooks & Filters

Here is the call stack when Power add_hooks_and_filters is run…

The call to SLP_BaseClass_Addon->createobject_UserInterface() is fired during the execution of the ‘wp_enqueue_scripts’ action hook as defined in base_class.addon.php in the slp_init() method…

// User Interface?
//
if ( ! empty( $this->userinterface_class_name ) ) {
add_action( 'wp_enqueue_scripts' , array( $this , 'createobject_UserInterface' ) );
}

Base Plugin Create Search Element

Here is the call stack , CALLED FIRST, for creating the search element:

slp_render_shortcode() is called to start this process per the WordPress add_shortcode() method in SLP_UI :: initialize().

That means do_shortcode() is being called very early —

This is due to a side effect of all the fuckery WordPress Core has been doing to deal with the block mode editor. In order to support new block themes (like Twenty Twenty Two) WP_Block->render() calls render_block_core_post_content() super early. Before any script enqueue.

WP Core: render_block_core_post_content() calls the “the_content” filter way earlier than non-block themes:

$content = apply_filters( 'the_content', str_replace( ']]>', ']]&gt;', $content ) );

Remedies

Lance.bio : WP Hooks & Filters Reference

Attach creatobject_UserInterface Calls To New Hook

With the block theme, firing that method via the ‘wp_enqueue_scripts’ method is too late for WP Block Themes.

There are multiple methods that could be used to hook this, but which one will be BEFORE the WP Block rendering calls the ‘the_content’ filter AND still work for older themes?

Option 1 : Hook to ‘the_content’ filter with an earlier precedence on the weight

Replace

// User Interface? 
// 
if ( ! empty( $this->userinterface_class_name ) ) {  add_action( 'wp_enqueue_scripts' , array( $this , 'createobject_UserInterface' ) ); }

With the call order weight parameter added (the default WP action weight is 10 if not specified)

// User Interface? 
// 
if ( ! empty( $this->userinterface_class_name ) ) {  add_action( 'the_content' , array( $this , 'createobject_UserInterface' ), 5 ); }
Concerns

The main concern here is that a ‘the_content’ script does not get called everywhere a ‘wp_enqueue_scripts’ hook gets called. The question is whether AJAX and REST calls run through here.

Results

Did not work, broke a lot of features and setup in the plugin.

Option 2 : Attach to an earlier hook…

Candidates…

  • block_parser_class — in function parse_blocks() probably not called on non-block themes.
  • template_include — before template_canvas.php which appears to be before template_canvas where the first line is get_the_block_template(); the assumption is template_canvas is only called for block based themes.
Concerns

‘template_include’ hook may get called too often, but it is a classic filter that has been around for a while and should work for both block and legacy templates.

Results

Seems to be working, needs more testing.

0 comments on “Debugging: Find Locations / Initial Load not working with Power add on enabled”

Debugging: Find Locations / Initial Load not working with Power add on enabled

Initial post to AJAX
Initial response

This looks like a problem with the AJAX processor on the backend. Need to look into ajax_onload.

csl_ajax_onload refs

The problem was with the Power add on trying to validate SLP location IDs when updating map markers. The intent is to NOT manipulate a map marker with an invalid ID as it can cause more problems downstream.

Resolution

DO NOT check_admin_referer() for the AJAX calls. The easiest way to check that , since we do not have an easily-accessible reference variable for “hey, I’m running an AJAX query now” is to simply ensure the nonce we want to validate even exists in the first place.

0 comments on “Debugging: Add locations w/ Power “Uncaught SyntaxError: redeclaration of const wp_data””

Debugging: Add locations w/ Power “Uncaught SyntaxError: redeclaration of const wp_data”

SLP 5.13.5 prerelease + Power 5.11 has a problem found during testing… the admin add locations breaks (probably a lot of other things as well).

The browser console:

This is related to moving to wp_add_inline_script() vs. the wp_localize_script() based on findings that indicate wp_localize_script() may a not work with block themes and it is really intended for i18n/l10n.

Further investigation finds…

store-locator-le/include/module/admin_tabs/SLP_BaseClass_Admin.php
enqueue_admin_javascript()

This is called for each add-on that needs it…


Power needs it.
handle: slppower_manage_locations
data stack:
{"ajax_nonce":"50164ddb77"}

SLP (base plugin) needs it.
handle: slp_manage_locations
data stack:
{"ajax_nonce":"50164ddb77"}

However, a code search shows that ONLY the base plugin admin JavaScript is using/referencing wp_data…

Why is Power setting up an enqueue of this file?

Code Analysis of enqueue_admin_javascript()…

This is always true:

if ( ! empty( $tab_js_settings ) ) {

Because this is always set further up in the code…

// All our JS will now have an AJAX_NONCE setting.
$tab_js_settings = array( 'ajax_nonce' => wp_create_nonce('slp_ajax'), );

They key player here “ajax_nonce” set above is ONLY ever used here… SLP base plugin admin.js… which is only called when doing a change_option. That is fired from both the base plugin and Power add on, HOWEVER… they both reference the SLP_ADMIN.change_option() method in JavaScript.

ajax_nonce usage in JavaScript is only in SLP base plugin admin.js (SLP_ADMIN) change_option() method attached to the options property (SLP_ADMIN.options.change_option).
All calls to the SLP_ADMIN.options.change_option() call

Resolution

Do not set the baseline tab_js_settings in SLP_BaseClass_Admin.php

This will stop the default (only) ajax_nonce PHP array entry from being set, effectively short-circuiting the enqueue of the script from the Power add-on.

While this fixes the issue short-term and stops overloading the tab_js_settings, if Power (or any other add on) finds it necessary to add some custom entries to the tab_js_settings array, it will break again. This is a fragile patch.

The change…

Add the handle to the JavaScript variable definition

The current setup is to hard-code the JS environment variable to “wp_data” for ALL add-ons. This will make them “fight” per the message above.

Instead of blindly referencing a generic hard-coded “wp_data” variable, let’s change that to be based on the add-on name.

For our use cases above that would do this.

For SLP base plugin wp_data => slp_data

For Power add on wp_data => slppower_data

The change…