0

In functions.php, I want to access the DB once to get an ACF object, and then "save" it locally to prevent further requests to the DB.

I thought of first calling the following function in the "init' hook.

Then, supposedly, when I call it on later hooks the $current_store var is already set because of the use of the "static" keyword, and the function will stop on the first "if" - returning the already saved static var.

It doesn't work - when accessing the function on later hooks 'isset($current_store)' returns false.

What am I doing wrong?

function get_current_store()
{
    if (isset($current_store)) {
        return $current_store;
    }

    $store_url_parts = explode('.', $_SERVER['HTTP_HOST']);
    $store_subdomain = $store_url_parts[0];

    $store_url_parts_reversed = array_reverse($store_url_parts);

    if (in_array("il", $store_url_parts_reversed)) {
        $domain = $store_url_parts_reversed[2];
    } else {
        $domain = $store_url_parts_reversed[2];
    }

    if ($store_subdomain == $domain) {
        $current_store = 'main';

        return $current_store;
    }

    if ($stores_page = get_page_by_title('Stores', OBJECT, 'option')) {
        $stores_page_id = $stores_page->ID;

        if (function_exists('have_rows')) {
            if (have_rows('stores', $stores_page_id)) {
                $store_url_parts = get_store_url_parts();
                $store_subdomain = $store_url_parts[0];
                $stores          = array();

                while (have_rows('stores', $stores_page_id)) {
                    the_row();

                $store = get_row(true);

                    $stores[] = $store;
                }

                $subdomains = array_column($stores, 'store_subdomain');

                $current_store_id     = array_search($store_subdomain, $subdomains);

                static $current_store = array();

                if ($current_store_id !== false) {
                    $current_store = $stores[$current_store_id];
                } else {
                    $current_store = false;
                }

                return $current_store;
            }
        }
   }

}

2
  • When you say "later on", do you mean later on the same page load, or on other requests or other pages? Commented Nov 21, 2018 at 11:14
  • Same page load. Sorry for not being clear.
    – Asaf Moshe
    Commented Nov 21, 2018 at 11:17

3 Answers 3

0

This is a good use case for the Object Cache:

WP_Object_Cache is WordPress' class for caching data which may be computationally expensive to regenerate, such as the result of complex database queries.

You can store things in the cache with wp_cache_set() and retrieve them with wp_cache_get(). You just need to give it a key and value, as well as a group name unique to your theme/plugin or set of functionality to avoid conflicts:

function get_current_store()
{
    // Check if the current store is cached.
    $cache = wp_cache_get( 'current_store', 'my_plugin' );

    // If it is, return the cached store.
    if ( $cache ) {
        return $cache;
    }

    // Otherwise do the work to figure out $current_store;

    // Then cache it.
    wp_cache_set( 'current_store', $current_store, 'my_plugin' );

    return $current_store;
}

Now no matter how many times you run get_current_store() on a page it will only do the work of generating the current store variable once.

2
  • Thanks! I did some reading and it seems to be the case, but I want to make sure as it is very important for us - there is no chance a certain user gets a cached object from another user's session, is this correct? (Because if there is a chance it is very bad for us). Will the 'wp_cache_add_non_persistent_groups' is a good addition to make sure it isn't happening? Is there something else recommended to do for securing this? Thanks again.
    – Asaf Moshe
    Commented Nov 21, 2018 at 13:46
  • The object cache isn't persistent at all by default, but there are plugins that can make it persistent. If you want to future-proof against one of those plugins in case you want to use one later, then yes, using wp_cache_add_non_persistent_groups( 'my_plugin' ) is probably a good idea. Commented Nov 21, 2018 at 14:00
0

Jacob Peattie answer is a great alternative.

But if someone iterested, the problem with my original code is that static $current_store needs to be declared at the beggining of the function.

This code works:

function get_current_store()
{
    static $current_store;

    if (isset($current_store)) {
        return $current_store;
    } else {
        $store_url_parts = explode('.', $_SERVER['HTTP_HOST']);
        $store_subdomain = $store_url_parts[0];

        $store_url_parts_reversed = array_reverse($store_url_parts);

        if (in_array("il", $store_url_parts_reversed)) {
            $domain = $store_url_parts_reversed[2];
        } else {
            $domain = $store_url_parts_reversed[2];
        }

        if ($store_subdomain == $domain) {
            $current_store = 'main';

            return $current_store;
        }

        if ($stores_page = get_page_by_title('Stores', OBJECT, 'option')) {
            $stores_page_id = $stores_page->ID;

            if (function_exists('have_rows')) {
                if (have_rows('stores', $stores_page_id)) {
                    $store_url_parts = get_store_url_parts();
                    $store_subdomain = $store_url_parts[0];
                    $stores          = array();

                    while (have_rows('stores', $stores_page_id)) {
                        the_row();

                        $store = get_row(true);

                        $stores[] = $store;
                    }

                    $subdomains = array_column($stores, 'store_subdomain');

                    $current_store_id = array_search($store_subdomain, $subdomains);

                    $current_store = array();

                    if ($current_store_id !== false) {
                        $current_store = $stores[$current_store_id];
                    } else {
                        $current_store = false;
                    }
                }
            }
            return $current_store;
        }
    }
}
-1

if the $current_store is some kind of global variable then your function should start with

function get_current_store() {
    global $current_store
    // the rest of your source code here ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.