Esempio n. 1
0
function wpr_create_campaign() {
	global $wpdb, $wpr_table_campaigns, $wpr_table_templates,$wpr_loadedmodules;
	
	$options = unserialize(get_option("wpr_options"));	
	
	if($options['wpr_simple']=='Yes') {
		$totalchance = 0;
		foreach($wpr_loadedmodules as $lmodule) {
			if($_POST[$lmodule."chance"] > 0) {
				$totalchance = $totalchance + $_POST[$lmodule."chance"];
			}
		}	
			if($_POST["mixchance"] > 0) {
				$totalchance = $totalchance + $_POST["mixchance"];
			}		
	} else {
		for ($i = 1; $i <= $_POST['tnum']; $i++) {
			if($_POST["chance$i"] > 0) {
				$totalchance = $totalchance + $_POST["chance$i"];
			}
		}
	}
	
	if($_POST['keywords'] == "" && $_POST['type'] == "keyword") {
		echo '<div class="updated"><p>'.__("Please enter at least one keyword!", "wprobot").'</p></div>';	
	} elseif($_POST['feeds'] == "" && $_POST['type'] == "rss") {
		echo '<div class="updated"><p>'.__("Please enter at least one RSS feed!", "wprobot").'</p></div>';	
	} elseif($_POST['nodes'] == "" && $_POST['type'] == "nodes") {
		echo '<div class="updated"><p>'.__("Please enter at least one Amazon BrowseNode!", "wprobot").'</p></div>';			
	} elseif($_POST['categories'] == "") {
		echo '<div class="updated"><p>'.__("Please enter at least one category!", "wprobot").'</p></div>';	
	} elseif($_POST['interval'] == "") {
		echo '<div class="updated"><p>'.__("Please enter a post interval!", "wprobot").'</p></div>';		
	} elseif($_POST['name'] == "") {
		echo '<div class="updated"><p>'.__("Please enter a name for your campaign!", "wprobot").'</p></div>';	
	} elseif($_POST['title1'] == "" && $options['wpr_simple']!='Yes' || $_POST['content1'] == "" && $options['wpr_simple']!='Yes' || $_POST['chance1'] == "" && $options['wpr_simple']!='Yes') {
		echo '<div class="updated"><p>'.__("Please enter at least one template for your campaign!", "wprobot").'</p></div>';
	} elseif($_POST['amazon_department'] == "All" && $_POST['type'] == "nodes") {
		echo '<div class="updated"><p>'.__("Amazon Department can not be 'All' in a BrowseNodes campaign! You have to select the correct Department your Nodes belong to (Amazon API requirement).", "wprobot").'</p></div>';	
	} elseif($totalchance != 100 && $options['wpr_simple']=='Yes' && $_POST['type'] == "keyword") {
		echo '<div class="updated"><p>'.__("Error: The sum of percentages for all Modules together has to be 100!", "wprobot").'</p></div>';		
	} elseif($totalchance != 100 && $options['wpr_simple']!='Yes') {
		echo '<div class="updated"><p>'.__("Error: The sum of the 'Chance of being used' fields for all post templates has to be 100! (Currently: ", "wprobot").$totalchance.')</p></div>';				
	} elseif($_POST["mixchance"] > 0 && $options['wpr_simple']=='Yes' && empty($_POST["mixcontent"]) && $_POST['type'] == "keyword") {	
		echo '<div class="updated"><p>'.__("Error: Template for Mixed Posts can not be empty if percentage for it is positive!", "wprobot").'</p></div>';		
	} else {	
	
		$type = $_POST['type'];
   
		// Keywords
		$_POST['keywords'] = stripslashes($_POST['keywords']);
		$keywordsinput = str_replace("\r", "", $_POST['keywords']);
		$keywordsinput = explode("\n", $keywordsinput);    
		
		$i=0;
		$keywords = array();
		
		if($_POST["edit"]) {// GET OLD KEYWORDS FOR NUMs $oldcamp->postspan
			$edit = 1;
			$oldcamp = $wpdb->get_row( "SELECT keywords,postspan FROM $wpr_table_campaigns WHERE id = '" . $_POST["edit"] . "'" );
			$oldkeywords = unserialize($oldcamp->keywords);	
		}

		if($type == "keyword") {
			foreach( $keywordsinput as $keyword) {
				if($keyword != "") {
				
					$keyword = explode("|", $keyword);    
				
					$keywords[$i] = array($keyword[0]);
					if($edit == 1) {
						$kwnums = false;
						foreach($oldkeywords as $key => $oldkeyword)  {
							if($oldkeyword[0] == $keyword[0]) {$kwnums = $oldkeyword[1];}
						}
					}
					if($kwnums != false) {
						$keywords[$i][] = $kwnums;
					} else {
						$keywords[$i][] = array("total" => 0);
					}
					$keywords[$i]["skipped"] = 0;
					
					// Add alternative keywords
					for ($y = 1; $y <= 5; $y++) {
						if(!empty($keyword[$y])) {
							$keywords[$i]["alternative"][$y] = $keyword[$y];
						}
					}
				}
				$i++;
			}
		} elseif($type == "rss") {
			$rssinput = str_replace("\r", "", $_POST['feeds']);
			$rssinput = explode("\n", $rssinput);
			foreach( $rssinput as $rss) {
				if($rss != "") {
					$keywords[$i] = array($keywordsinput[$i]);
					if($edit == 1) {
						$kwnums = false;
						foreach($oldkeywords as $key => $oldkeyword)  {
							if($oldkeyword[0] == $keywordsinput[$i]) {$kwnums = $oldkeyword[1];}
						}
					}					
					if($kwnums != false) {
						$keywords[$i][] = $kwnums;
					} else {
						$keywords[$i][] = array("total" => 0);
					}
					$keywords[$i]["skipped"] = 0;
					$keywords[$i]["feed"] = $rss;
				}
				$i++;	
			}
		} elseif($type == "nodes") {
			$nodesinput = str_replace("\r", "", $_POST['nodes']);
			$nodesinput = explode("\n", $nodesinput);	
			$failcount = 0;
			foreach( $nodesinput as $node) {
				$nodename = wpr_aws_getnodename($node);
				if($node != "" && $nodename != false && !is_array($nodename)) {
					$keywords[$i] = array($keywordsinput[$i]);
					if($edit == 1) {
						$kwnums = false;
						foreach($oldkeywords as $key => $oldkeyword)  {
							if($oldkeyword[0] == $keywordsinput[$i]) {$kwnums = $oldkeyword[1];}
						}
					}					
					if($kwnums != false) {
						$keywords[$i][] = $kwnums;
					} else {
						$keywords[$i][] = array("total" => 0);
					}
					$keywords[$i]["skipped"] = 0;
					$keywords[$i]["bnn"] = "$nodename";					
					$keywords[$i]["node"] = $node;
				} else {
					$failcount++;
				}
				$i++;	
			}
			if($failcount > 0) {
				echo '<div class="updated"><p>'.__("<b>Error</b>: ","wprobot").$failcount.__(" Browsenodes could not be added to your campaign! Make sure to select the correct Amazon Department and the Amazon Site matching your Node ID. This is a requirement of the Amazon API.", "wprobot");						
				//if(is_array($nodename)) {
				//	echo __("<br/><br/>The last error Amazon returned was:<br/><i>","wprobot").$nodename["message"]."</i>";
				//}
				echo '</p></div>';
			}			
		}	
		$keywords = $wpdb->escape(serialize($keywords));		

		// Categories
		if($_POST['multisingle'] == "single") {
			$categorysave = array();
			$categorysave[0][0]["id"] = $_POST['categories'];
			$categorysave[0][0]["name"] = get_cat_name( $_POST['categories'] );
		} else {
			$categorysave = array();		
			$_POST['categories'] = stripslashes($_POST['categories']);			
			$categoriesinput = str_replace("\r", "", $_POST['categories']);
			$categoriesinput = explode("\n", $categoriesinput);
			$i = 0;	
			foreach($categoriesinput as $categories) {
				$categories = explode(",",$categories);
				$k = 0;	
				foreach($categories as $category) {
					if($category != "") {

						// if category starts with "-" get previous parent ID and set variable
						if($category[0] == '-') {
							$category = substr($category, 1);
							$parent = $parentid;
							$saveparent = 0;
						} else {
							$parent = "";
							$saveparent = 1;					
						}
						
						$category = str_replace("&", "&amp;", $category);

						$catname = ucwords($category);
						$category_ID = get_cat_ID( $category );		
						
						if(!$category_ID && $_POST['createcats'] == "yes") {
							if(is_numeric($parent)) {$parent = (int)$parent;} else {$parent = 0;}
	
								if(function_exists("wp_create_category")) {
									$category_ID = wp_create_category( $catname, $parent );
								} elseif(function_exists("wp_insert_category")) {
									$category_ID = wp_insert_category( array(
									  'cat_ID' => 0,
									  'cat_name' => $catname,
									  'category_description' => "",
									  'category_nicename' => "",
									  'category_parent' => $parent,
									  'taxonomy' => 'category' ) );
								}
							
						} elseif(!$category_ID && $_POST['createcats'] != "yes") {
							$category_ID = 1;
							$catname = get_cat_name( $category_ID );
						} elseif(isset($category_ID) && $_POST['createcats'] != "yes") {
							$catname = get_cat_name( $category_ID );
						}					

						$categorysave[$i][$k]["name"] = $catname;
						$categorysave[$i][$k]["id"] = $category_ID;
						
						if($saveparent == 1) {$parentid = $category_ID;}
					}
					$k++;
				}	
				$i++;
			}	
			/*if(count($categories) == 1) {
				$categories["type"] = "single";
			} else {
				$categories["type"] = "multi";			
			}	*/	
		}//print_r($categorysave);
		$categorysave = $wpdb->escape(serialize($categorysave));
		
		// Templates
		$templates = array();
		if($options['wpr_simple']=='Yes' && $type == "keyword") {
			$i = 1;
			foreach($wpr_loadedmodules as $lmodule) {
				if($_POST[$lmodule."chance"] > 0) {
					$templates[$i]["title"] = "{".$lmodule."title}";
					$templates[$i]["content"] = "{".$lmodule."}";
					if($lmodule == "ebay" || $lmodule == "yahoonews") {$templates[$i]["content"] .= "\n{".$lmodule."}\n{".$lmodule."}";}
					$templates[$i]["chance"] = $_POST[$lmodule."chance"];
					if($lmodule == "amazon") {$templates[$i]["comments"]["amazon"] = 1;} else {$templates[$i]["comments"]["amazon"] = 0;}
					if($lmodule == "flickr") {$templates[$i]["comments"]["flickr"] = 1;} else {$templates[$i]["comments"]["flickr"] = 0;}
					if($lmodule == "yahooanswers") {$templates[$i]["comments"]["yahooanswers"] = 1;} else {$templates[$i]["comments"]["yahooanswers"] = 0;}
					if($lmodule == "youtube") {$templates[$i]["comments"]["youtube"] = 1;} else {$templates[$i]["comments"]["youtube"] = 0;}
					$i++;
				}
			}
			if($_POST["mixchance"] > 0) {
				$templates[$i]["title"] = "{title}";
				$templates[$i]["content"] = stripslashes($_POST["mixcontent"]);
				$templates[$i]["chance"] = $_POST["mixchance"];		
				$templates[$i]["comments"]["amazon"] = 1;
				$templates[$i]["comments"]["flickr"] = 1;
				$templates[$i]["comments"]["yahooanswers"] = 1;
				$templates[$i]["comments"]["youtube"] = 1;				
			}
		} else {
			for ($i = 1; $i <= $_POST['tnum']; $i++) {
				if($_POST["chance$i"] > 0) {
					$templates[$i]["title"] = stripslashes($_POST["title$i"]);
					$templates[$i]["content"] = stripslashes($_POST["content$i"]);
					$templates[$i]["chance"] = $_POST["chance$i"];
					$templates[$i]["comments"]["amazon"] = $_POST["comments_amazon$i"];
					$templates[$i]["comments"]["flickr"] = $_POST["comments_flickr$i"];
					$templates[$i]["comments"]["yahooanswers"] = $_POST["comments_yahoo$i"];
					$templates[$i]["comments"]["youtube"] = $_POST["comments_youtube$i"];
				}
			}
		}
		//$templates = array_values($templates); -- MAKES FIRST TEMPLATE "0"
		$templates = $wpdb->escape(serialize($templates));
		
		// Optional settings
		$amadept = $_POST['amazon_department'];

		$yahoocat = array();
		$yahoocat["ps"] = $_POST['wpr_poststatus'];
		$yahoocat["rw"] = $_POST['wpr_rewriter'];
		$yahoocat["a"] = $_POST['wpr_author'];
		$yahoocat["t"] = $_POST['wpr_postthumbs'];
		$yahoocat["pt"] = $_POST['wpr_posttype'];
		$yahoocat = $wpdb->escape(serialize($yahoocat));
		$ebaycat = $_POST['ebay_category'];
		
		$_POST['replace'] = stripslashes($_POST['replace']);
		$replaceinput = str_replace("\r", "", $_POST['replace']);
		$replaceinput = explode("\n", $replaceinput);    
		
		$i=0;
		$replaces = array();
		foreach( $replaceinput as $replace) {
			if($replace != "") {
				$replace = explode("|", $replace);  
				$replaces[$i]["from"] = $replace[0];
				$replaces[$i]["to"] = str_replace('\"', "", $replace[1]);
				$replaces[$i]["chance"] = $replace[2];
				$replaces[$i]["code"] = $replace[3];
			}
			$i++;
		}
		$replaces = $wpdb->escape(serialize($replaces));	
		
		$_POST['exclude'] = stripslashes($_POST['exclude']);
		$exclude = str_replace("\r", "", $_POST['exclude']);
		$exclude = explode("\n", $exclude);
		foreach($exclude as $key => $value) {
			if($value == "") {
				unset($array[$key]);
			}
		}
		$exclude = array_values($exclude); 
		$exclude = $wpdb->escape(serialize($exclude));
		
		$name = $_POST['name'];
		$postevery = $_POST['interval'];
		$cr_period = $_POST['period'];
		$postspan = "WPR_" . $postevery . "_" . $cr_period;	
		
		$customfield = array();
		for ($i = 1; $i <= $_POST['cfnum']; $i++) {
			if(!empty($_POST["cf_value$i"]) && !empty($_POST["cf_name$i"])) {
				$customfield[$i]["name"] = $_POST["cf_name$i"];
				$customfield[$i]["value"] = $_POST["cf_value$i"];
			}
		}
		$customfield = $wpdb->escape(serialize($customfield));
		
		$translation = array();
		$translation["chance"] = $_POST['transchance'];
		$translation["from"] = $_POST['trans1'];
		$translation["to1"] = $_POST['trans2'];
		$translation["to2"] = $_POST['trans3'];
		$translation["to3"] = $_POST['trans4'];
		$translation["comments"] = $_POST['trans_comments'];
		$translation = $wpdb->escape(serialize($translation));
		
		if($_POST['autopost'] == "yes") {
			$pause = 0;
		} else {
			$pause = 1;
		}
		
		if($_POST["edit"]) {
			$uid = $_POST["edit"];
			$update = "UPDATE " . $wpr_table_campaigns . " SET name = '$name', ctype = '$type', keywords = '$keywords'
					, categories = '$categorysave', templates = '$templates', cinterval = '$postevery', period = '$cr_period', postspan = '$postspan'
					, replacekws = '$replaces', excludekws = '$exclude', amazon_department = '$amadept', ebay_cat = '$ebaycat', yahoo_cat = '$yahoocat'
					, translation = '$translation', customfield = '$customfield', pause = '$pause' WHERE id = $uid";
			//echo $update . "<br>";
			$results = $wpdb->query($update);
			if ($results) {
				if($postspan != $oldcamp->postspan) {
					
					$timestamp = wp_next_scheduled( 'wprobothook', array($uid) );
					wp_unschedule_event($timestamp, 'wprobothook', array($uid) );
					
					$lag = $_POST['delaystart'] * 3600;
					wpr_set_schedule($postevery, $cr_period);
					wp_schedule_event( time()+rand(1,500)+$lag, $postspan, "wprobothook" , array($uid) );	
					//wp_reschedule_event( $oldcamp->postspan, $postspan, "wprobothook", array($uid) ); // wp_reschedule_event( time(), $postspan, "wprobothook", array($uid) );
					wpr_delete_schedule($oldcamp->cinterval, $oldcamp->period);
				}
				echo '<div class="updated"><p>'.__('Campaign has been updated! Go to the <a href="?page=wpr-single&id='.$uid.'">control panel</a> to view details.', "wprobot").'</p></div>';		
			} else {
				echo '<div class="updated"><p>'.__("Campaign could not be updated!", "wprobot").'</p></div>';			
			}		
		} else {
			$insert = "INSERT INTO " . $wpr_table_campaigns . " SET name = '$name', ctype = '$type', keywords = '$keywords'
			, categories = '$categorysave', templates = '$templates', cinterval = '$postevery', period = '$cr_period', postspan = '$postspan'
			, replacekws = '$replaces', excludekws = '$exclude', amazon_department = '$amadept', ebay_cat = '$ebaycat', yahoo_cat = '$yahoocat'
			, translation = '$translation', customfield = '$customfield', pause = '$pause'";
			$result = $wpdb->query($insert);
			$insid = mysql_insert_id();
			
			if ($result) {	
			
				$sql = "SELECT LAST_INSERT_ID() FROM " . $wpr_table_campaigns;
				$id = $wpdb->get_var($sql);	$linkid = $id;
				if($linkid != $insid) {$linkid = $insid;}
				
				wpr_set_schedule($postevery, $cr_period);	
				$lag = $_POST['delaystart'] * 3600;
				if($lag == "" || !is_numeric($lag) || $lag < 0) {$lag = 200;}	
				
				wp_schedule_event( time()+$lag, $postspan, "wprobothook" , array($id) );

				$next = wp_next_scheduled( "wprobothook", array($id) );
				if($next == 0 || $next == "0" || $next == null || $next == "") {
					wp_schedule_event( time()+$lag, $postspan, "wprobothook" , array($id) );
				}
				
				echo '<div class="updated"><p>';
				printf(__('Campaign "%1$s" has been added! Go to the <a href="?page=wpr-single&id=%2$s">control panel</a> to view details.', 'wprobot'), $name, $linkid);
				echo '</p></div>';		
			}
		}	
	}
}
Esempio n. 2
0
function wpr_create_campaign()
{
    global $wpdb, $wpr_table_campaigns, $wpr_table_templates;
    $options = unserialize(get_option("wpr_options"));
    if ($_POST['keywords'] == "" && $_POST['type'] == "keyword") {
        echo '<div class="updated"><p>' . __("Please enter at least one keyword!", "wprobot") . '</p></div>';
    } elseif ($_POST['feeds'] == "" && $_POST['type'] == "rss") {
        echo '<div class="updated"><p>' . __("Please enter at least one RSS feed!", "wprobot") . '</p></div>';
    } elseif ($_POST['nodes'] == "" && $_POST['type'] == "nodes") {
        echo '<div class="updated"><p>' . __("Please enter at least one Amazon BrowseNode!", "wprobot") . '</p></div>';
    } elseif ($_POST['categories'] == "") {
        echo '<div class="updated"><p>' . __("Please enter at least one category!", "wprobot") . '</p></div>';
    } elseif ($_POST['interval'] == "") {
        echo '<div class="updated"><p>' . __("Please enter a post interval!", "wprobot") . '</p></div>';
    } elseif ($_POST['name'] == "") {
        echo '<div class="updated"><p>' . __("Please enter a name for your campaign!", "wprobot") . '</p></div>';
    } elseif ($_POST['title1'] == "" || $_POST['content1'] == "" || $_POST['chance1'] == "") {
        echo '<div class="updated"><p>' . __("Please enter at least one template for your campaign!", "wprobot") . '</p></div>';
    } elseif ($_POST['amazon_department'] == "All" && $_POST['type'] == "nodes") {
        echo '<div class="updated"><p>' . __("Amazon Department can not be 'All' in a BrowseNodes campaign! You can to select the correct Department your Nodes belong to (Amazon API requirement).", "wprobot") . '</p></div>';
    } else {
        $type = $_POST['type'];
        // Keywords
        $_POST['keywords'] = stripslashes($_POST['keywords']);
        $keywordsinput = str_replace("\r", "", $_POST['keywords']);
        $keywordsinput = explode("\n", $keywordsinput);
        $i = 0;
        $keywords = array();
        if ($_POST["edit"]) {
            // GET OLD KEYWORDS FOR NUMs $oldcamp->postspan
            $edit = 1;
            $oldcamp = $wpdb->get_row("SELECT keywords,postspan FROM {$wpr_table_campaigns} WHERE id = '" . $_POST["edit"] . "'");
            $oldkeywords = unserialize($oldcamp->keywords);
        }
        if ($type == "keyword") {
            foreach ($keywordsinput as $keyword) {
                if ($keyword != "") {
                    $keywords[$i] = array($keyword);
                    if ($edit == 1) {
                        $kwnums = false;
                        foreach ($oldkeywords as $key => $oldkeyword) {
                            if ($oldkeyword[0] == $keyword) {
                                $kwnums = $oldkeyword[1];
                            }
                        }
                    }
                    if ($kwnums != false) {
                        $keywords[$i][] = $kwnums;
                    } else {
                        $keywords[$i][] = array("total" => 0);
                    }
                    $keywords[$i]["skipped"] = 0;
                }
                $i++;
            }
        } elseif ($type == "rss") {
            $rssinput = str_replace("\r", "", $_POST['feeds']);
            $rssinput = explode("\n", $rssinput);
            foreach ($rssinput as $rss) {
                if ($rss != "") {
                    $keywords[$i] = array($keywordsinput[$i]);
                    if ($edit == 1) {
                        $kwnums = false;
                        foreach ($oldkeywords as $key => $oldkeyword) {
                            if ($oldkeyword[0] == $keywordsinput[$i]) {
                                $kwnums = $oldkeyword[1];
                            }
                        }
                    }
                    if ($kwnums != false) {
                        $keywords[$i][] = $kwnums;
                    } else {
                        $keywords[$i][] = array("total" => 0);
                    }
                    $keywords[$i]["skipped"] = 0;
                    $keywords[$i]["feed"] = $rss;
                }
                $i++;
            }
        } elseif ($type == "nodes") {
            $nodesinput = str_replace("\r", "", $_POST['nodes']);
            $nodesinput = explode("\n", $nodesinput);
            foreach ($nodesinput as $node) {
                $nodename = wpr_aws_getnodename($node);
                echo $nodename;
                if ($node != "" && $nodename != false) {
                    $keywords[$i] = array($keywordsinput[$i]);
                    if ($edit == 1) {
                        $kwnums = false;
                        foreach ($oldkeywords as $key => $oldkeyword) {
                            if ($oldkeyword[0] == $keywordsinput[$i]) {
                                $kwnums = $oldkeyword[1];
                            }
                        }
                    }
                    if ($kwnums != false) {
                        $keywords[$i][] = $kwnums;
                    } else {
                        $keywords[$i][] = array("total" => 0);
                    }
                    $keywords[$i]["skipped"] = 0;
                    $keywords[$i]["bnn"] = "{$nodename}";
                    $keywords[$i]["node"] = $node;
                }
                $i++;
            }
        }
        $keywords = $wpdb->escape(serialize($keywords));
        // Categories
        if ($_POST['multisingle'] == "single") {
            $categories = array();
            //$categories["type"] = "single";
            $categories[0]["id"] = $_POST['categories'];
        } else {
            $categories = array();
            $_POST['categories'] = stripslashes($_POST['categories']);
            $categoriesinput = str_replace("\r", "", $_POST['categories']);
            $categoriesinput = explode("\n", $categoriesinput);
            $i = 0;
            foreach ($categoriesinput as $category) {
                if ($category != "") {
                    $category_ID = $wpdb->get_var("SELECT term_id FROM {$wpdb->terms} WHERE name = '" . $category . "'");
                    if (!$category_ID && $_POST['createcats'] == "yes") {
                        $category_ID = wp_create_category(ucwords($category));
                    }
                    $categories[$i]["name"] = $category;
                    $categories[$i]["id"] = $category_ID;
                }
                $i++;
            }
            /*if(count($categories) == 1) {
            			$categories["type"] = "single";
            		} else {
            			$categories["type"] = "multi";			
            		}	*/
        }
        $categories = $wpdb->escape(serialize($categories));
        // Templates
        $templates = array();
        for ($i = 1; $i <= $_POST['tnum']; $i++) {
            if ($_POST["chance{$i}"] > 0) {
                $templates[$i]["title"] = stripslashes($_POST["title{$i}"]);
                $templates[$i]["content"] = stripslashes($_POST["content{$i}"]);
                $templates[$i]["chance"] = $_POST["chance{$i}"];
                $templates[$i]["comments"]["amazon"] = $_POST["comments_amazon{$i}"];
                $templates[$i]["comments"]["flickr"] = $_POST["comments_flickr{$i}"];
                $templates[$i]["comments"]["yahooanswers"] = $_POST["comments_yahoo{$i}"];
                $templates[$i]["comments"]["youtube"] = $_POST["comments_youtube{$i}"];
            }
        }
        //$templates = array_values($templates); -- MAKES FIRST TEMPLATE "0"
        $templates = serialize($templates);
        // Optional settings
        $amadept = $_POST['amazon_department'];
        $yahoocat = $_POST['yahoo_category'];
        $ebaycat = $_POST['ebay_category'];
        $_POST['replace'] = stripslashes($_POST['replace']);
        $replaceinput = str_replace("\r", "", $_POST['replace']);
        $replaceinput = explode("\n", $replaceinput);
        $i = 0;
        $replaces = array();
        foreach ($replaceinput as $replace) {
            if ($replace != "") {
                $replace = explode("|", $replace);
                $replaces[$i]["from"] = $replace[0];
                $replaces[$i]["to"] = str_replace('\\"', "", $replace[1]);
                $replaces[$i]["chance"] = $replace[2];
            }
            $i++;
        }
        $replaces = $wpdb->escape(serialize($replaces));
        $_POST['exclude'] = stripslashes($_POST['exclude']);
        $exclude = str_replace("\r", "", $_POST['exclude']);
        $exclude = explode("\n", $exclude);
        foreach ($exclude as $key => $value) {
            if ($value == "") {
                unset($array[$key]);
            }
        }
        $exclude = array_values($exclude);
        $exclude = $wpdb->escape(serialize($exclude));
        $name = $_POST['name'];
        $postevery = $_POST['interval'];
        $cr_period = $_POST['period'];
        $postspan = "WPR_" . $postevery . "_" . $cr_period;
        $customfield = array();
        $customfield["name"] = $_POST['cf_name'];
        $customfield["value"] = $_POST['cf_value'];
        $customfield = serialize($customfield);
        $translation = array();
        $translation["chance"] = $_POST['transchance'];
        $translation["from"] = $_POST['trans1'];
        $translation["to1"] = $_POST['trans2'];
        $translation["to2"] = $_POST['trans3'];
        $translation["to3"] = $_POST['trans4'];
        $translation["comments"] = $_POST['trans_comments'];
        $translation = serialize($translation);
        if ($_POST['autopost'] == "yes") {
            $pause = 0;
        } else {
            $pause = 1;
        }
        if ($_POST["edit"]) {
            $uid = $_POST["edit"];
            $update = "UPDATE " . $wpr_table_campaigns . " SET name = '{$name}', ctype = '{$type}', keywords = '{$keywords}'\n\t\t\t\t\t, categories = '{$categories}', templates = '{$templates}', cinterval = '{$postevery}', period = '{$cr_period}', postspan = '{$postspan}'\n\t\t\t\t\t, replacekws = '{$replaces}', excludekws = '{$exclude}', amazon_department = '{$amadept}', ebay_cat = '{$ebaycat}', yahoo_cat = '{$yahoocat}'\n\t\t\t\t\t, translation = '{$translation}', customfield = '{$customfield}', pause = '{$pause}' WHERE id = {$uid}";
            $results = $wpdb->query($update);
            if ($results) {
                if ($postspan != $oldcamp->postspan) {
                    wpr_set_schedule($postevery, $cr_period);
                    wp_reschedule_event(time(), $postspan, "wprobothook", array($uid));
                    wpr_delete_schedule($oldcamp->cinterval, $oldcamp->period);
                }
                echo '<div class="updated"><p>' . __('Campaign has been updated! Go to the <a href="?page=wpr-single&id=' . $uid . '">control panel</a> to view details.', "wprobot") . '</p></div>';
            } else {
                echo '<div class="updated"><p>' . __("Campaign could not be updated!", "wprobot") . '</p></div>';
            }
        } else {
            $insert = "INSERT INTO " . $wpr_table_campaigns . " SET name = '{$name}', ctype = '{$type}', keywords = '{$keywords}'\n\t\t\t, categories = '{$categories}', templates = '{$templates}', cinterval = '{$postevery}', period = '{$cr_period}', postspan = '{$postspan}'\n\t\t\t, replacekws = '{$replaces}', excludekws = '{$exclude}', amazon_department = '{$amadept}', ebay_cat = '{$ebaycat}', yahoo_cat = '{$yahoocat}'\n\t\t\t, translation = '{$translation}', customfield = '{$customfield}', pause = '{$pause}'";
            $result = $wpdb->query($insert);
            $insid = mysql_insert_id();
            if ($result) {
                $sql = "SELECT LAST_INSERT_ID() FROM " . $wpr_table_campaigns;
                $id = $wpdb->get_var($sql);
                $linkid = $id;
                if ($linkid != $insid) {
                    $linkid = $insid;
                }
                wpr_set_schedule($postevery, $cr_period);
                $lag = $_POST['delaystart'] * 3600;
                if ($lag == "" || !is_numeric($lag) || $lag < 0) {
                    $lag = 200;
                }
                wp_schedule_event(time() + $lag, $postspan, "wprobothook", array($id));
                $next = wp_next_scheduled("wprobothook", array($id));
                if ($next == 0 || $next == "0" || $next == null || $next == "") {
                    wp_schedule_event(time() + $lag, $postspan, "wprobothook", array($id));
                }
                echo '<div class="updated"><p>' . __('Campaign "' . $name . '" has been added! Go to the <a href="?page=wpr-single&id=' . $linkid . '">control panel</a> to view details.', 'wprobot') . '</p></div>';
            }
        }
    }
}