/**
  * Construct the instruction object.
  *
  * @since 1.3.0
  *
  * @param int|object instruction The instruction item or item ID.
  */
 public function __construct($instruction)
 {
     if (is_numeric($instruction)) {
         $items_api = new Simmer_Recipe_Items();
         $instruction = $items_api->get_item($instruction);
         if (!$instruction) {
             return false;
         }
     }
     $this->id = $instruction->recipe_item_id;
     $this->order = $instruction->recipe_item_order;
     $this->description = $this->get_description(true);
     $this->is_heading = $this->is_heading(true);
 }
 /**
  * Get the items that belong to the recipe.
  *
  * @since 1.3.0
  *
  * @param  string $type  Optional. The type of items to get. If blank, all items
  *                       will be returned. Default: all items.
  * @return array  $items The attached items.
  */
 public function get_items($type = '')
 {
     $args = array();
     if ($type) {
         $args['type'] = esc_attr($type);
     }
     $items_api = new Simmer_Recipe_Items();
     $items = (array) $items_api->get_items($this->id, $args);
     /**
      * Filter a recipe's retrieved items.
      *
      * @since 1.3.0
      *
      * @param array $items     The retrieved items.
      * @param int   $recipe_id The recipe ID.
      */
     $items = apply_filters('simmer_get_recipe_items', $items, $this->id);
     return $items;
 }
 /**
  * Upgrade Simmer from 1.2.X to 1.3.X.
  *
  * @since 1.3.0
  */
 public function from_1_2()
 {
     global $wpdb;
     // Get all recipe IDs.
     $recipe_ids = get_posts(array('post_type' => 'recipe', 'post_status' => 'any', 'numberposts' => -1, 'fields' => 'ids'));
     if ($recipe_ids) {
         $items_api = new Simmer_Recipe_Items();
         foreach ($recipe_ids as $recipe_id) {
             $ingredients = get_post_meta($recipe_id, '_recipe_ingredients', true);
             if ($ingredients) {
                 $order = 0;
                 foreach ($ingredients as $ingredient) {
                     $item_id = $items_api->add_item($recipe_id, 'ingredient', $order);
                     if (isset($ingredient['amt'])) {
                         $wpdb->insert($wpdb->prefix . 'simmer_recipe_itemmeta', array('recipe_item_id' => $item_id, 'meta_key' => 'amount', 'meta_value' => $ingredient['amt']), array('%d', '%s', '%s'));
                     }
                     if (isset($ingredient['unit'])) {
                         $wpdb->insert($wpdb->prefix . 'simmer_recipe_itemmeta', array('recipe_item_id' => $item_id, 'meta_key' => 'unit', 'meta_value' => $ingredient['unit']), array('%d', '%s', '%s'));
                     }
                     if (isset($ingredient['desc'])) {
                         $wpdb->insert($wpdb->prefix . 'simmer_recipe_itemmeta', array('recipe_item_id' => $item_id, 'meta_key' => 'description', 'meta_value' => $ingredient['desc']), array('%d', '%s', '%s'));
                     }
                     $order++;
                 }
             }
             $instructions = get_post_meta($recipe_id, '_recipe_instructions', true);
             if ($instructions) {
                 $order = 0;
                 foreach ($instructions as $instruction) {
                     $item_id = $items_api->add_item($recipe_id, 'instruction', $order);
                     if (isset($instruction['desc'])) {
                         $wpdb->insert($wpdb->prefix . 'simmer_recipe_itemmeta', array('recipe_item_id' => $item_id, 'meta_key' => 'description', 'meta_value' => $instruction['desc']), array('%d', '%s', '%s'));
                     }
                     if (isset($instruction['heading']) && 1 == $instruction['heading']) {
                         $wpdb->insert($wpdb->prefix . 'simmer_recipe_itemmeta', array('recipe_item_id' => $item_id, 'meta_key' => 'is_heading', 'meta_value' => $instruction['heading']), array('%d', '%s', '%d'));
                     }
                     $order++;
                 }
             }
         }
     }
 }
Exemple #4
0
/**
 * Delete an existing recipe item from the database.
 *
 * @since 1.3.0
 *
 * @param  int  $item_id The ID of the item to delete.
 * @return bool $result  Whether the item was deleted.
 */
function simmer_delete_recipe_item($item_id)
{
    $items_api = new Simmer_Recipe_Items();
    $result = $items_api->delete_item($item_id);
    return $result;
}