AyeCode / userswp

Lightweight WordPress User Profile Plugin, provides a front end login form, registration form, edit account form, forgot password, searchable users directory, and user profiles. It can be extended and we provide add-ons to integrate it with WooCommerce, Easy Digital Downloads, Wp Jobs Manager, GeoDirectory, MailChimp, and many other plugins.
https://userswp.io/
GNU General Public License v2.0
63 stars 26 forks source link

Cache the results of uwp_get_custom_field_info #692

Closed picocodes closed 1 year ago

picocodes commented 1 year ago

Checked and this function executes the same db query 196 times in a single page load ( on a page with lots of users ). We can cache the results using a static or global variable to ensure that it only reads the db once.

Account-–-Noptin

sachyya commented 1 year ago

Can you specify the page too where this function is being executed or maybe list out steps to recreate this issue?

picocodes commented 1 year ago

Hi @sachyya, install the Query Monitor plugin then open the frontend users page.

A quick fix would be:-

$cache_key = $htmlvar_name . '_' . $form_type;
if ( isset( $GLOBALS['uwp_custom_field_info'][ $cache_key ] ) ) {
    return $GLOBALS['uwp_custom_field_info'][ $cache_key ];
}
sachyya commented 1 year ago

The multiple calls being made is due to the filter uwp_get_usermeta applied in class-meta.php L105 in which modify_datepicker_value_on_get is being hooked. That hooked function executes uwp_get_custom_field_info.

In default setting, modify_datepicker_value_on_get does nothing to modify to the filter. I suppose it can be omitted. Can you specify the use case of this function? Otherwise, I suppose a simple one would be to omit this filter that in return reduces the function calls.

picocodes commented 1 year ago

The function retrieves a field's settings as defined in the UWP form builder. I'm not sure that removing the filter is the correct fix since the function is called in several other places.

picocodes commented 1 year ago

Please check how the same issue was fixed in GeoDirectory.

https://github.com/AyeCode/geodirectory/issues/2104 https://github.com/AyeCode/geodirectory/blob/master/includes/custom-fields/functions.php#L364