Wordpress Favourites Plugin - Read array of posts from cookie

333 Views Asked by At

I need to read the cookie generated by "Wordpress Favourites" plugin (great code foundation), and get the posts ID's out of the cookie array for use in the interface. This is to avoid front-end caching at our hosting provider WP Engine.

The cookie is called "simplefavorites" the cookie data looks like this when viewed in Firebug console:

Under "value":

[{"site_id":1,"posts":[17411,22578],"groups":[{"group_id":1,"site_id":1,"group_name":"Default List","posts":[17411,22578]}]}]

and under "Raw Data":

%5B%7B%22site_id%22%3A1%2C%22posts%22%3A%5B17411%2C22578%5D%2C%22groups%22%3A%5B%7B%22group_id%22%3A1%2C%22site_id%22%3A1%2C%22group_name%22%3A%22Default+List%22%2C%22posts%22%3A%5B17411%2C22578%5D%7D%5D%7D%5D

I need to get the post values as an array to work with like array(17411,22578). We can return the entire cookie contents, and detect whether it exists, but yanking out just the ID's is proving to be tricky. Any suggestions on how to parse this string would be greatly appreciated. Here's what we're working with right now....

    // Read Cookie For Favorites Functionality - simplefavourites
    function readCookie(name) {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for (var i = 0; i < ca.length; i++) {
            var c = ca[i];
            while (c.charAt(0) == ' ') c = c.substring(1, c.length);
            if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
        }
        return null;
    }
    // Extract post ID's from cookie string
    function getvaluefromcookiestring(cookiecontents){
        cookiecontents = decodeURI(cookiecontents); // this works fine and
        var val = JSON.parse(cookiecontents),
        v = val[0]; // the contents of the cookie is a one-node array
        return v.posts;
    }
    var cookiecontents = readCookie('simplefavorites');
    cookiecontents = getvaluefromcookiestring(cookiecontents);
    alert(cookiecontents);

We receive a "SyntaxError: JSON.parse: expected ':' after property name in object at line 1 column 12 of the JSON data"

------PROBLEM SOLVED------ with the help of @Spartacus and @Louys Patrice Bessette. See related question here on retrieving values from a cookie correctly: Javascript - retrieving data from a cookie as "value" not "raw data"

    // Read cookie for favorites functionality - simplefavourites
    function readCookie(name) {
        var c = document.cookie.match(new RegExp('(^| )' + name + '=([^;]+)'));
        return c ? c[2] : null;
    }
    // Extract post ID's from cookie string
    function getvaluefromcookiestring(cookiecontents){
        cookiecontents = decodeURI(cookiecontents.replace(/%3A/g, ":").replace(/%2C/g, ",").replace(/\+/g, "%20"));
        var val = JSON.parse(cookiecontents),
        v = val[0];
        return v.posts;
    }
    var cookiecontents = readCookie('simplefavorites');
    cookiecontents = getvaluefromcookiestring(cookiecontents);
    alert(cookiecontents);

Thanks for all the help stackers!

2

There are 2 best solutions below

12
On BEST ANSWER

Change your cookie-getter function to this:

function readCookie(name) {
    var c = document.cookie.match(new RegExp('(^| )' + name + '=([^;]+)'));
    return c ? c[2] : null;
}

Then use JSON.parse to turn the value into an object for manipulation. You can grab it like so:

function getpostsfromfavouritecookie(cookiecontents){
        var val = JSON.parse(cookiecontents),
            v = val[0]; // the contents of the cookie is a one-node array

        return v.posts;
}

var cookiecontents = readCookie('simplefavorites');
var posts = getpostsfromfavouritecookie(cookiecontents);

alert(posts);
1
On

It seems to be valid JSON. Use JSON.parse().

var cookieString = '[{"site_id":1,"posts":[17411,22578],"groups":[{"group_id":1,"site_id":1,"group_name":"Default List","posts":[17411,22578]}]}]';
var obj = JSON.parse(cookieString);
// now access object properties

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse