Esempio n. 1
0
 /**
  * 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);
         $searchable_recipe = $recipe->title();
         $searchable_recipe .= ' - ';
         $searchable_recipe .= $recipe->description();
         $searchable_recipe .= ' - ';
         if ($recipe->has_ingredients()) {
             $previous_group = null;
             foreach ($recipe->ingredients() as $ingredient) {
                 $group = isset($ingredient['group']) ? $ingredient['group'] : '';
                 if ($group !== $previous_group && $group) {
                     $searchable_recipe .= $group . ': ';
                     $previous_group = $group;
                 }
                 $searchable_recipe .= $ingredient['ingredient'];
                 if (trim($ingredient['notes']) !== '') {
                     $searchable_recipe .= ' (' . $ingredient['notes'] . ')';
                 }
                 $searchable_recipe .= ', ';
             }
         }
         if ($recipe->has_instructions()) {
             $previous_group = null;
             foreach ($recipe->instructions() as $instruction) {
                 $group = isset($instruction['group']) ? $instruction['group'] : '';
                 if ($group !== $previous_group && $group) {
                     $searchable_recipe .= $group . ': ';
                     $previous_group = $group;
                 }
                 $searchable_recipe .= $instruction['description'] . '; ';
             }
         }
         $searchable_recipe .= ' - ';
         $searchable_recipe .= $recipe->notes();
         // Prevent shortcodes
         $searchable_recipe = str_replace('[', '(', $searchable_recipe);
         $searchable_recipe = str_replace(']', ')', $searchable_recipe);
         $post_content = preg_replace("/<div class=\"wpurp-searchable-recipe\"[^<]*<\\/div>/", "", $post->post_content);
         // Backwards compatibility
         $post_content = preg_replace("/\\[wpurp-searchable-recipe\\][^\\[]*\\[\\/wpurp-searchable-recipe\\]/", "", $post_content);
         $post_content .= '[wpurp-searchable-recipe]';
         $post_content .= htmlentities($searchable_recipe);
         $post_content .= '[/wpurp-searchable-recipe]';
         remove_action('save_post', array($this, 'save'), 15, 2);
         wp_update_post(array('ID' => $recipe->ID(), 'post_content' => $post_content));
         update_post_meta($recipe->ID(), 'wpurp_text_search_3', time());
         add_action('save_post', array($this, 'save'), 15, 2);
     }
 }
            <td class="recipe-general-form-label"><label for="recipe_alternate_image"><?php 
        _e('Image', 'wp-ultimate-recipe');
        ?>
</label></td>
            <td class="recipe-general-form-field">
                <input type="hidden" name="recipe_alternate_image" id="recipe_alternate_image"  value="<?php 
        echo $recipe->alternate_image();
        ?>
" />
                <input class="recipe_alternate_image_add button <?php 
        if ($recipe->alternate_image()) {
            echo ' wpurp-hide';
        }
        ?>
" rel="<?php 
        echo $recipe->ID();
        ?>
" type="button" value="<?php 
        _e('Add Alternate Image', 'wp-ultimate-recipe');
        ?>
" />
                <input class="recipe_alternate_image_remove button<?php 
        if (!$recipe->alternate_image()) {
            echo ' wpurp-hide';
        }
        ?>
" type="button" value="<?php 
        _e('Remove Alternate Image', 'wp-ultimate-recipe');
        ?>
" />
                }
                $searchable_recipe .= ', ';
            }
        }
        if ($recipe->has_instructions()) {
            $previous_group = null;
            foreach ($recipe->instructions() as $instruction) {
                $group = isset($instruction['group']) ? $instruction['group'] : '';
                if ($group !== $previous_group && $group) {
                    $searchable_recipe .= $group . ': ';
                    $previous_group = $group;
                }
                $searchable_recipe .= $instruction['description'] . '; ';
            }
        }
        $searchable_recipe .= ' - ';
        $searchable_recipe .= $recipe->notes();
        // Prevent shortcodes
        $searchable_recipe = str_replace('[', '(', $searchable_recipe);
        $searchable_recipe = str_replace(']', ')', $searchable_recipe);
        $post_content = preg_replace("/<div class=\"wpurp-searchable-recipe\"[^<]*<\\/div>/", "", $post->post_content);
        $post_content .= '<div class="wpurp-searchable-recipe" style="display:none">';
        $post_content .= htmlentities($searchable_recipe);
        $post_content .= '</div>';
        wp_update_post(array('ID' => $recipe->ID(), 'post_content' => $post_content));
        update_post_meta($recipe->ID(), 'wpurp_text_search', time());
    }
} else {
    // Finished migrating, all recipes have a full text search
    update_option('wpurp_cron_migrate_version', '2.3.0');
}
 /**
  * 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());
     }
 }