buffer = array() ;
$this->obj = $obj ;
}
/** ====================================================================================================================================================
* Add title in the form
*
* @param string $title the title to add
* @return void
*/
function add_title($title) {
$this->buffer[] = array('title', $title) ;
}
/** ====================================================================================================================================================
* Add title macroblock (i.e. that can be duplicate a plurality of time if the user needs it) in the form
*
* @param string $title the title to add
* @return void
*/
function add_title_macroblock($title, $lien="") {
if ($lien=="") {
$lien = __("Add new", "SL_framework") ;
}
$this->buffer[] = array('title_macro', $title, $lien) ;
}
/** ====================================================================================================================================================
* Add a comment in the form
*
* @param string $comment the comment to add
* @return void
*/
function add_comment($comment) {
$this->buffer[] = array('comment', $comment) ;
}
/** ====================================================================================================================================================
* Add a comment in the form which display the default value of the param
*
* @param string $comment the comment to add
* @return void
*/
function add_comment_default_value($param) {
$comment = $this->obj->get_default_option($param) ;
$comment = str_replace("\r", "", $comment) ;
$comment = str_replace("<", "<", $comment) ;
$comment = str_replace(">", ">", $comment) ;
if (strpos($comment, "*")===0)
$comment = substr($comment, 1) ;
$comment = str_replace(" ", " ", $comment) ;
$comment = str_replace("\n", " ", $comment) ;
$this->buffer[] = array('comment', "$comment") ;
}
/** ====================================================================================================================================================
* Add a textarea, input, checkbox, etc. in the form to enable the modification of parameter of the plugin
*
* Please note that the default value of the parameter (defined in the get_default_option function) will define the type of input form. If the default value is a: - string, the input form will be an input text - integer, the input form will be an input text accepting only integer - string beggining with a '*', the input form will be a textarea - string equals to '[file]$path', the input form will be a file input and the file will be stored at $path (relative to the upload folder) - string equals to '[password]$password', the input form will be a password input ; - string equals to '[page]$page', the input form will be a dropdown list with a list of the pages ; - string equals to '[media]$media', the input form will be a element in the media library ; - array of string, the input form will be a dropdown list - boolean, the input form will be a checkbox
*
* @param string $param the name of the parameter/option as defined in your plugin and especially in the get_default_option of your plugin
* @param string $name the displayed name of the parameter in the form
* @param string $forbid regexp which will delete some characters in the submitted string (only a warning is raised) : For instance $forbid = "/[^a-zA-Z0-9]/" will remove all the non alphanumeric value
* @param string $allow regexp which will verify that the submitted string will respect this rexexp, if not, the submitted value is not saved and an erreor is raised : For instance, $allow = "/^[a-zA-Z]/" require that the submitted string begin with a nalpha character
* @param array $related a list of the other params that will be actived/deactivated when this parameter is set to true/false (thus, this param should be a boolean)
* @return void
*/
function add_param($param, $name, $forbid="", $allow="", $related=array()) {
$this->buffer[] = array('param', $param, $name, $forbid, $allow, $related) ;
}
/** ====================================================================================================================================================
* Get the new value of the parameter after its update (with happen upon calling flush)
*
* @param string $param the name of the parameter/option as defined in your plugin and especially in the get_default_option of your plugin
* @return mixed the value of the param (array if there is an error or null if there is no new value)
*/
function get_new_value($param) {
global $_POST ;
global $_FILES ;
// We reset the value to default
//---------------------------------------
if (isset($_POST['resetOptions'])) {
$value = $this->obj->get_default_option($param) ;
if ((is_string($value))&&(strpos($value, "*")===0))
$value = substr($value, 1) ;
return $value ;
}
// We find the correspondance in the array to find the allow and forbid tag
//---------------------------------------
$name = "" ;
$forbid = "" ;
$allow = "" ;
$related = "" ;
for($iii=0; $iiibuffer); $iii++) {
$ligne = $this->buffer[$iii] ;
if ($ligne[0]=="param") {
if ($param == $ligne[1]) {
$name = $ligne[2] ;
$forbid = $ligne[3] ;
$allow = $ligne[4] ;
$related = $ligne[5] ;
break;
}
}
}
// What is the type of the parameter ?
//---------------------------------------
$type = "string" ;
if (is_bool($this->obj->get_default_option($param))) $type = "boolean" ;
if (is_int($this->obj->get_default_option($param))) $type = "int" ;
if (is_array($this->obj->get_default_option($param))) $type = "list" ;
// C'est un text si dans le texte par defaut, il y a une etoile
if (is_string($this->obj->get_default_option($param))) {
if (strpos($this->obj->get_default_option($param), "*") === 0) $type = "text" ;
}
// C'est un file si dans le texte par defaut est egal a [file]
if (is_string($this->obj->get_default_option($param))) {
if (str_replace("[file]","",$this->obj->get_default_option($param)) != $this->obj->get_default_option($param)) $type = "file" ;
}
// C'est un password si dans le texte par defaut est egal a [password]
if (is_string($this->obj->get_default_option($param))) {
if (str_replace("[password]","",$this->obj->get_default_option($param)) != $this->obj->get_default_option($param)) $type = "password" ;
}
// C'est un media si dans le texte par defaut est egal a [media]
if (is_string($this->obj->get_default_option($param))) {
if (str_replace("[media]","",$this->obj->get_default_option($param)) != $this->obj->get_default_option($param)) $type = "media" ;
}
// C'est un media si dans le texte par defaut est egal a [media]
if (is_string($this->obj->get_default_option($param))) {
if (str_replace("[page]","",$this->obj->get_default_option($param)) != $this->obj->get_default_option($param)) $type = "page" ;
}
// We format the param
//---------------------------------------
$problem_e = "" ;
$problem_w = "" ;
if (isset($_POST['submitOptions'])) {
// Is it a boolean ?
if ($type=="boolean") {
if (isset($_POST[$param])) {
if ($_POST[$param]) {
return true ;
} else {
return false ;
}
} else {
if (isset($_POST[$param."_workaround"])) {
return false ;
} else {
return $this->obj->get_default_option($param) ;
}
}
}
// Is it an integer ?
if ($type=="int") {
if (isset($_POST[$param])) {
if (SLFramework_Utils::is_really_int($_POST[$param])) {
return (int)$_POST[$param] ;
} else {
if ($_POST[$param]=="") {
return 0 ;
} else {
return array("error", "
".__('Error: the submitted value is not an integer and thus, the parameter has not been updated!', 'SL_framework')."
\n") ;
}
}
} else {
return $this->obj->get_default_option($param) ;
}
}
// Is it a string ?
if (($type=="string")||($type=="text")||($type=="password")) {
if (isset($_POST[$param])) {
$tmp = $_POST[$param] ;
if ($forbid!="") {
$tmp = preg_replace($forbid, '', $_POST[$param]) ;
}
if (($allow!="")&&(!preg_match($allow, $_POST[$param]))) {
return array("error","
".__('Error: the submitted string does not match the constrains', 'SL_framework')." (".$allow.")!
\n") ;
} else {
return stripslashes($tmp) ;
}
} else {
if ($type=="text") {
if (substr($this->obj->get_default_option($param), 0,1)) {
return substr($this->obj->get_default_option($param), 1) ;
}
}
return $this->obj->get_default_option($param) ;
}
}
// Is it a list ?
if ($type=="list") {
if (isset($_POST[$param])) {
$selected = $_POST[$param] ;
$array = $this->obj->get_param($param) ;
$mod = false ;
for ($i=0 ; $iobj->get_default_option($param) ;
}
}
// is it a media
if ($type=="media") {
if (isset($_POST[$param])) {
return $_POST[$param] ;
} else {
return str_replace("[media]","", $this->obj->get_default_option($param)) ;
}
}
// is it a page
if ($type=="page") {
if (isset($_POST[$param])) {
return $_POST[$param] ;
} else {
return str_replace("[page]","", $this->obj->get_default_option($param)) ;
}
}
// Is it a file ?
if ($type=="file") {
// deleted ?
$upload_dir = wp_upload_dir();
if (isset($_POST["delete_".$param])) {
$deleted = $_POST["delete_".$param] ;
if ($deleted=="1") {
if (file_exists($upload_dir["basedir"].$this->obj->get_param($param))){
@unlink($upload_dir["basedir"].$this->obj->get_param($param)) ;
}
return $this->obj->get_default_option($param) ;
}
}
if (isset($_FILES[$param])) {
$tmp = $_FILES[$param]['tmp_name'] ;
if ($tmp != "") {
if ($_FILES[$param]["error"] > 0) {
return array("error", "
".__('Error: the submitted file can not be uploaded!', 'SL_framework')."
\n") ;
}
}
} else {
return $this->obj->get_param($param) ;
}
} else {
return $this->obj->get_param($param) ;
}
}
}
return null ;
}
/** ====================================================================================================================================================
* Remove a parameter of the plugin which has been previously added through add_param (then it can be useful if you want to update a parameter without printing the form)
*
* It will also remove any comment for the same
*
* @param string $param the name of the parameter/option as defined in your plugin and especially in the get_default_option of your plugin
* @return void
*/
function remove_param($param) {
$search_comment = false ;
foreach($this->buffer as $j=>$i){
if (!$search_comment) {
if($i[0] == $param){
unset($this->buffer[$j]) ;
$search_comment = true ;
}
} else {
if ($i[0] == "comment"){
unset($this->buffer[$j]) ;
} else {
return ;
}
}
}
$this->buffer = array_values($this->buffer) ;
}
/** ====================================================================================================================================================
* Print the form with parameters
*
* @return void
*/
function flush() {
global $_POST ;
global $_FILES ;
global $wpdb ;
$this->buffer[] = array('end', "") ;
// We create the beginning of the form
$this->output = "
".__("Parameters",'SL_framework')."
" ;
if ($this->obj->getPluginID()!="") {
$this->output .= "
".__("Here are the parameters of the plugin. Modify them at will to fit your needs.","SL_framework")."
" ;
} else {
$this->output .= "
".__("Here are the parameters of the framework. Modify them at will to fit your needs.","SL_framework")."