Ask your WordPress questions! Pay money and get answers fast! (more info)

WP Alchemy - replacing eval in MediaAccess.php WordPress

  • SOLVED

Hello,

is there a way to replace eval function in WP Alchemy class -> MediaAccess.php (line 375) with another one:

var data = $(this).attr('class').match(/({.*})/i);
data = (data && data[1]) ? data[1] : '' ;
data = eval("(" + (data.indexOf('{') < 0 ? '{' + data + '}' : data) + ")");


since it fails in Theme-Check plugin?

Whole script can be downloaded here: http://www.farinspace.com/wpalchemy-metabox/#download


Thanks!

Answers (1)

2012-12-05

Dbranes answers:

Hi, you could try to replace:

var data = $(this).attr('class').match(/({.*})/i);
data = (data && data[1]) ? data[1] : '' ;
data = eval("(" + (data.indexOf('{') < 0 ? '{' + data + '}' : data) + ")");


with


var data = $(this).attr('class').match(/({.*})/i);
data = (data && data[1]) ? data[1] : '' ;
if( data.indexOf('{') < 0 ){
data ='{' + data + '}';
}
data = jQuery.parseJSON(data);


to get rid of the eval function.

(I have not tested this, but it looks like in your code that the "data" is a json string that is converted into a json object)


denoizzed comments:

Thanks, but unfortunately it seems to be breaking WordPress Upload functionality:
http://d.pr/i/c9Zs


Dbranes comments:

ok, I have found out that the "data" string/object is constructed from a class name like this one:

{label:'Insert'}


and then translated into an object like this (via eval):

Object {label: "Insert"}


If we are going to skip <strong>eval</strong> and use instead <strong>jQuery.parseJSON()</strong>, the input string has to be like

{"label":"Insert"}

with double quotation mark on the key:value pair, then this would work

data = jQuery.parseJSON('{"label":"Insert"}');


or in general

data = jQuery.parseJSON(data);


So in short these examples will not work:


data = jQuery.parseJSON('{label:"Insert"}');
data = jQuery.parseJSON('{label:Insert}');
data = jQuery.parseJSON("{'label':'Insert'}");
data = jQuery.parseJSON("{'label':Insert}");
data = jQuery.parseJSON("{label:'Insert'}");


but only

data = jQuery.parseJSON('{"label":"Insert"}');


at least to my experiments.


Dbranes comments:

you can then try to replace this line:

data = eval("(" + (data.indexOf('{') < 0 ? '{' + data + '}' : data) + ")");


with this:

if( data.indexOf('{') < 0 ){
data ='{' + data + '}';
}
data=data.replace('"',"'");
data=data.replace('{','{\"');
data=data.replace('}','\"}');
data=data.replace(':','\":\"');
data=data.replace("'","");
data = jQuery.parseJSON(data);