merge get_transient and a call to a rest api

Question

I display long range (time based) data, which i fetch from a rest api and then store as transient with a long expiration time. Then I fetch short range data, which changes permanently and therefore has a short expiration time. I tried to combine the long term transient with a call to a rest api, but could not succeed. Is this concept possible with wordpress transients?
Did anyone ever try something similar?

        //this section works well
 if (!empty($L_transdata) && !empty($L_transdata)) {
        //both transients are populated
        $L_short_json = json_decode($L_transdata, true); // convert it to an array.
        $S_short_json = json_decode($S_transdata, true); // convert it to an array.

        $D_All = array_merge($L_short_json, $S_short_json);//merge both arrays

        $L_clean_json = json_encode($D_All);
        wp_send_json($L_clean_json);//send data to jquery function

        } 
        //the else if part causes problems: parsererror 
 else if (!empty($L_transdata) && empty($S_transdata)){
        //fetch transient with long expiration time
        $L_short_json = json_decode($L_transdata, true); // convert it to an array.
        //fetch more data from rest api
        $S_Date = getDatesFromRange($S_startD, $S_EndD);
        //encode the data
        $ST_clean_json = json_encode($S_Date); //json
        //set the transient
        set_transient($S_transient, $ST_clean_json, 1 * HOUR_IN_SECONDS); //HOUR_IN_SECONDS

        $S_short_json = json_decode($Date, true); // convert it to an array.
        $D_All = array_merge($L_short_json, $S_short_json);//merge

        $L_clean_json = json_encode($D_All);
        wp_send_json($L_clean_json);

 } else {
        //this section works fine
        $L_Date = getDatesFromRange($L_startD, $L_EndD);
        $S_Date = getDatesFromRange($S_startD, $S_EndD);

        $ST_clean_json = json_encode($S_Date);
        set_transient($S_transient, $ST_clean_json, 1 * HOUR_IN_SECONDS);
        $LT_clean_json = json_encode($L_Date); //json
        set_transient($L_transient, $LT_clean_json, 14 * DAY_IN_SECONDS); //HOUR_IN_SECONDS

        $L_short_json = json_decode($L_Date, true); // convert it to an array.
        $S_short_json = json_decode($S_Date, true);

        $D_All = array_merge($L_short_json, $S_short_json);//merge 
        $L_clean_json = json_encode($D_All);
        wp_send_json($L_clean_json);

    }
0
timholz 8 months 0 Answers 102 views 0

Leave an answer