$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>static array on functions.php|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

static array on functions.php

matteradmin8PV0评论

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;
            }
        }
   }

}

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;
            }
        }
   }

}

Share Improve this question asked Nov 21, 2018 at 10:34 Asaf MosheAsaf Moshe 1 2
  • When you say "later on", do you mean later on the same page load, or on other requests or other pages? – Jacob Peattie Commented Nov 21, 2018 at 11:14
  • Same page load. Sorry for not being clear. – Asaf Moshe Commented Nov 21, 2018 at 11:17
Add a comment  | 

3 Answers 3

Reset to default 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.

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;
        }
    }
}

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 ...

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far