/** * Handles saving of recipes */ public function save($id, $post) { if ($post->post_type == 'recipe') { if (!isset($_POST['recipe_meta_box_nonce']) || !wp_verify_nonce($_POST['recipe_meta_box_nonce'], 'recipe')) { return $id; } $recipe = new WPURP_Recipe($post); $fields = $recipe->fields(); // Make sure the recipe_title meta is present if (!isset($_POST['recipe_title'])) { $_POST['recipe_title'] = $recipe->title(); } else { if ($_POST['recipe_title'] == '') { $_POST['recipe_title'] = $post->post_title; } } // TODO Refactor saving of fields foreach ($fields as $field) { $old = get_post_meta($recipe->ID(), $field, true); $new = isset($_POST[$field]) ? $_POST[$field] : null; // Field specific adjustments if (isset($new) && $field == 'recipe_ingredients') { $ingredients = array(); $non_empty_ingredients = array(); foreach ($new as $ingredient) { if (trim($ingredient['ingredient']) != '') { $term = term_exists($ingredient['ingredient'], 'ingredient'); if ($term === 0 || $term === null) { $term = wp_insert_term($ingredient['ingredient'], 'ingredient'); } if (is_wp_error($term)) { if (isset($term->error_data['term_exists'])) { $term_id = intval($term->error_data['term_exists']); } else { var_dump($term); } } else { $term_id = intval($term['term_id']); } $ingredient['ingredient_id'] = $term_id; $ingredients[] = $term_id; $ingredient['amount_normalized'] = $this->normalize_amount($ingredient['amount']); $non_empty_ingredients[] = $ingredient; } } wp_set_post_terms($recipe->ID(), $ingredients, 'ingredient'); $new = $non_empty_ingredients; } elseif (isset($new) && $field == 'recipe_instructions') { $non_empty_instructions = array(); foreach ($new as $instruction) { if ($instruction['description'] != '' || isset($instruction['image']) && $instruction['image'] != '') { $non_empty_instructions[] = $instruction; } } $new = $non_empty_instructions; } elseif (isset($new) && $field == 'recipe_servings') { update_post_meta($recipe->ID(), 'recipe_servings_normalized', $this->normalize_servings($new)); } elseif (isset($new) && $field == 'recipe_rating') { $term_name = intval($new) == 1 ? $new . ' ' . __('star', 'wp-ultimate-recipe') : $new . ' ' . __('stars', 'wp-ultimate-recipe'); wp_set_post_terms($recipe->ID(), $term_name, 'rating'); } // Update or delete meta data if changed if (isset($new) && $new != $old) { update_post_meta($recipe->ID(), $field, $new); if ($field == 'recipe_ingredients' && WPUltimateRecipe::is_addon_active('nutritional-information') && WPUltimateRecipe::option('nutritional_information_notice', '1') == '1' && current_user_can(WPUltimateRecipe::option('nutritional_information_capability', 'manage_options'))) { $notice = '<strong>' . $_POST['recipe_title'] . ':</strong> <a href="' . admin_url('edit.php?post_type=recipe&page=wpurp_nutritional_information&limit_by_recipe=' . $recipe->ID()) . '">' . __('Update the Nutritional Information', 'wp-ultimate-recipe') . '</a>'; WPUltimateRecipe::get()->helper('notices')->add_admin_notice($notice); } } elseif ($new == '' && $old) { delete_post_meta($recipe->ID(), $field, $old); } } $this->update_recipe_terms($recipe->ID()); } }