Exemplo n.º 1
0
 public function save_recipe($post_id)
 {
     $ingredients = $_POST["ingredients"];
     $directions = $_POST["directions"];
     $directions_array = array();
     libxml_use_internal_errors(true);
     $dom = new DOMDocument();
     $dom->loadHTML('<?xml encoding="UTF-8">' . $ingredients);
     $items = $dom->documentElement;
     $result = array();
     foreach ($items->childNodes as $item) {
         if ($item->hasChildNodes()) {
             $childs = $item->childNodes;
             foreach ($childs as $i) {
                 $result = array_merge($result, $this->input_parser($i));
             }
         }
     }
     Ingredient::delete_recipe_ingredients($post_id);
     $lines = $result;
     //$resulpreg_split( "/[\n\r]+/", $ingredients );
     $count = 1;
     foreach ($lines as $line) {
         $ing = Ingredient::process($line, $count);
         if ($ing) {
             $ing->add_to_database($post_id);
             $count++;
         }
     }
     $dom->loadHTML('<?xml encoding="UTF-8">' . $directions);
     $items = $dom->documentElement;
     $result = array();
     foreach ($items->childNodes as $item) {
         if ($item->hasChildNodes()) {
             $childs = $item->childNodes;
             foreach ($childs as $i) {
                 $result = array_merge($result, $this->input_parser($i));
             }
         }
     }
     $lines = $result;
     //preg_split( "/[\n\r]+/", $directions );
     $count = 1;
     foreach ($lines as $line) {
         $step = Direction::process($count, $line);
         if ($step) {
             $directions_array[] = $step;
         }
         $count++;
     }
     update_post_meta($post_id, 'directions', $directions_array);
     if (isset($_POST["recipe_asin"])) {
         $array = explode(",", wp_strip_all_tags($_POST["recipe_asin"]));
         $array = array_filter($array);
         update_post_meta($post_id, 'equipment', implode(",", $array));
     }
     if (isset($_POST["notes"])) {
         update_post_meta($post_id, 'notes', wp_strip_all_tags($_POST["notes"]));
     }
     if (isset($_POST["source"])) {
         update_post_meta($post_id, 'source', wp_strip_all_tags($_POST["source"]));
     }
     if (isset($_POST["source-url"])) {
         update_post_meta($post_id, 'source_url', wp_strip_all_tags($_POST["source-url"]));
     }
     if (strlen($_POST["prep-time-hour"]) || strlen($_POST["prep-time-min"])) {
         $min = 0 + (int) $_POST["prep-time-hour"] * 60;
         $min = $min + (int) $_POST["prep-time-min"];
         update_post_meta($post_id, 'prep_time', $min);
     } else {
         update_post_meta($post_id, 'prep_time', 0);
     }
     if (strlen($_POST["cooking-time-hour"]) || strlen($_POST["cooking-time-min"])) {
         $min = 0 + (int) $_POST["cooking-time-hour"] * 60;
         $min = $min + (int) $_POST["cooking-time-min"];
         update_post_meta($post_id, 'cooking_time', $min);
     } else {
         update_post_meta($post_id, 'cooking_time', 0);
     }
     if (strlen($_POST["total-time-hour"]) || strlen($_POST["total-time-min"])) {
         $min = 0 + (int) $_POST["total-time-hour"] * 60;
         $min = $min + (int) $_POST["total-time-min"];
         update_post_meta($post_id, 'total_time', $min);
     } else {
         update_post_meta($post_id, 'total_time', 0);
     }
     if (isset($_POST["yield"])) {
         update_post_meta($post_id, 'yield', wp_strip_all_tags($_POST["yield"]));
     }
     if (isset($_POST["category"])) {
         update_post_meta($post_id, 'category', wp_strip_all_tags($_POST["category"]));
     }
     if (isset($_POST["cuisine"])) {
         update_post_meta($post_id, 'cuisine', wp_strip_all_tags($_POST["cuisine"]));
     }
 }