private function doConvert($postID, $postType)
 {
     /** @global $wpdb wpdb */
     global $wpdb;
     $result = new stdClass();
     switch ($postType) {
         case 'ultimate-recipe':
             $result->recipe = new stdClass();
             if ($postID == 'random') {
                 $posts = get_posts(array('post_type' => 'recipe', 'nopaging' => true));
                 $post = $posts[array_rand($posts)];
             } else {
                 $post = get_post($postID);
             }
             $recipe = get_post_custom($post->ID);
             $user = get_userdata($post->post_author);
             $image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'full');
             $cuisine = wp_get_object_terms($post->ID, 'cuisine');
             if ($cuisine instanceof WP_Error) {
                 register_taxonomy('cuisine', 'recipe');
                 $cuisine = wp_get_object_terms($post->ID, 'cuisine');
             }
             $course = wp_get_object_terms($post->ID, 'course');
             if ($course instanceof WP_Error) {
                 register_taxonomy('course', 'recipe');
                 $course = wp_get_object_terms($post->ID, 'course');
             }
             /**
              * Very dodgy way of processing times - not sure what else we can do!
              * Times other than "x minutes" are going to be unparsable reliably
              * Will have to adjust this based on real user values as we come across them
              *
              * Try for xlated "minutes" first
              * If that doesn't work, at least try some likely English duration specifiers
              */
             $xMinutes = __('minutes', 'wp-ultimate-recipe');
             $timeText = $recipe['recipe_prep_time_text'][0];
             if ($timeText == $xMinutes || $timeText == 'minute') {
                 $result->recipe->prep_time = 'PT' . $recipe['recipe_prep_time'][0] . 'M';
             } elseif ($timeText == 'hours' || $timeText == 'hour') {
                 $result->recipe->prep_time = 'PT' . $recipe['recipe_prep_time'][0] . 'H0M';
             }
             $timeText = $recipe['recipe_cook_time_text'][0];
             if ($timeText == $xMinutes || $timeText == 'minute') {
                 $result->recipe->cook_time = 'PT' . $recipe['recipe_cook_time'][0] . 'M';
             } elseif ($timeText == 'hours' || $timeText == 'hour') {
                 $result->recipe->cook_time = 'PT' . $recipe['recipe_cook_time'][0] . 'H0M';
             }
             $result->recipe->recipe_image = !empty($image) ? $image[0] : '';
             if (is_array($cuisine) && !empty($cuisine[0])) {
                 $result->recipe->cuisine = htmlspecialchars($cuisine[0]->name);
             }
             if (is_array($course) && !empty($course[0])) {
                 $result->recipe->mealType = htmlspecialchars($course[0]->name);
             }
             $result->recipe->recipe_title = htmlspecialchars($recipe['recipe_title'][0]);
             /** @noinspection PhpUndefinedFieldInspection */
             $result->recipe->author = htmlspecialchars($user->data->display_name);
             $result->recipe->summary = htmlspecialchars($recipe['recipe_description'][0]);
             $notes = preg_replace_callback('%<(strong|em)>(.*?)</\\1>%', array($this, 'notesConversion'), $recipe['recipe_notes'][0]);
             $result->recipe->notes = preg_replace('%<a ([^>]+)>(.*?)</a>%i', '[url $1]$2[/url]', $notes);
             $section = '';
             $ingredients = array();
             $urIngredients = @unserialize($recipe['recipe_ingredients'][0]);
             if (!$urIngredients) {
                 $urIngredients = array();
             }
             foreach ($urIngredients as $urIngredient) {
                 if ($urIngredient['group'] != $section) {
                     $section = $urIngredient['group'];
                     $ingredients[] = '!' . htmlspecialchars($urIngredient['group']);
                 }
                 $ingredient = htmlspecialchars($urIngredient['amount']) . ' ' . htmlspecialchars($urIngredient['unit']) . ' ' . htmlspecialchars($urIngredient['ingredient']);
                 if (!empty($urIngredient['notes'])) {
                     $ingredient .= ' ' . htmlspecialchars($urIngredient['notes']);
                 }
                 $ingredients[] = $ingredient;
             }
             $result->ingredients = $ingredients;
             $section = '';
             $instructions = array();
             $urInstructions = @unserialize($recipe['recipe_instructions'][0]);
             if (!$urInstructions) {
                 $urInstructions = array();
             }
             foreach ($urInstructions as $urInstruction) {
                 if ($urInstruction['group'] != $section) {
                     $section = $urInstruction['group'];
                     $instructions[] = '!' . htmlspecialchars($urInstruction['group']);
                 }
                 $instruction = htmlspecialchars($urInstruction['description']);
                 if (!empty($urInstruction['image'])) {
                     $instructionImage = wp_get_attachment_image_src($urInstruction['image'], 'large');
                     if (!empty($instructionImage)) {
                         $instruction .= '[br][img src="' . $instructionImage[0] . '" width="' . $instructionImage[1] . '" height="' . $instructionImage[2] . '" /]';
                     }
                 }
                 $instructions[] = $instruction;
             }
             $result->recipe->instructions = implode("\n", $instructions);
             break;
         case 'recipeseo':
             $result->recipe = $wpdb->get_row("SELECT * FROM " . $wpdb->prefix . "amd_recipeseo_recipes WHERE recipe_id=" . $postID);
             $ingredients = $wpdb->get_results("SELECT * FROM " . $wpdb->prefix . "amd_recipeseo_ingredients WHERE recipe_id=" . $postID . " ORDER BY ingredient_id");
             $result->ingredients = array();
             foreach ($ingredients as $ingredient) {
                 $result->ingredients[] = $ingredient->amount . " " . $ingredient->name;
             }
             break;
         case 'zlrecipe':
             $result->recipe = $wpdb->get_row("SELECT * FROM " . $wpdb->prefix . "amd_zlrecipe_recipes WHERE recipe_id=" . $postID);
             $result->recipe->nReviews = 1;
             $result->recipe->summary = $this->decodeMarkdown($result->recipe->summary);
             $result->recipe->notes = $this->decodeMarkdown($result->recipe->notes);
             /**
              * If only total time is specified, use it as the cook time
              * TODO - Do this for all plugins?
              */
             if ($result->recipe->cook_time == '' && $result->recipe->prep_time == '') {
                 $result->recipe->cook_time = $result->recipe->total_time;
             }
             $ingredients = explode("\n", str_replace("\r", "", $result->recipe->ingredients));
             $result->ingredients = array();
             foreach ($ingredients as $ingredient) {
                 $ingredient = trim($ingredient);
                 if ($ingredient != '') {
                     if (preg_match('/^%([^\\s]+)/', $ingredient, $regs)) {
                         if (($n = count($result->ingredients)) > 0 && $result->ingredients[$n - 1][0] != '!') {
                             $result->ingredients[$n - 1] .= '[br][img src="' . $regs[1] . '"]';
                             continue;
                         } else {
                             $ingredient = '[img src="' . $regs[1] . '"]';
                         }
                     } else {
                         $ingredient = $this->decodeMarkdown($ingredient);
                     }
                     $result->ingredients[] = $ingredient;
                 }
             }
             unset($result->recipe->ingredients);
             $instructions = explode("\n", str_replace("\r", "", $result->recipe->instructions));
             $convertedInstructions = array();
             foreach ($instructions as $instruction) {
                 $instruction = trim($instruction);
                 if ($instruction != '') {
                     if (preg_match('/^%([^\\s]+)/', $instruction, $regs)) {
                         if (($n = count($convertedInstructions)) > 0 && $convertedInstructions[$n - 1][0] != '!') {
                             $convertedInstructions[$n - 1] .= '[br][img src="' . $regs[1] . '"]';
                             continue;
                         } else {
                             $instruction = '[img src="' . $regs[1] . '"]';
                         }
                     } else {
                         $instruction = $this->decodeMarkdown($instruction);
                     }
                     $convertedInstructions[] = $instruction;
                 }
             }
             /**
              * For silly historical reasons, instructions are returned as a string
              */
             $result->recipe->instructions = implode("\n", $convertedInstructions);
             break;
         case 'recipress':
             $meta = get_post_custom($postID);
             $result->recipe = new stdClass();
             $result->ingredients = array();
             $result->recipe->instructions = '';
             /** @noinspection PhpUndefinedFunctionInspection */
             $size = recipress_options('instruction_image_size');
             $result->recipe->recipe_title = $meta['title'][0] ? $meta['title'][0] : '';
             $photo = wp_get_attachment_image_src($meta['photo'][0], 'thumbnail', false);
             $result->recipe->recipe_image = $photo ? $photo[0] : '';
             $result->recipe->summary = $meta['summary'][0] ? $meta['summary'][0] : '';
             $terms = get_the_terms($postID, 'cuisine');
             $result->recipe->cuisine = $terms[0]->name;
             $terms = get_the_terms($postID, 'course');
             $result->recipe->mealType = $terms[0]->name;
             /** @noinspection PhpUndefinedFunctionInspection */
             $result->recipe->cook_time = $meta['cook_time'][0] ? recipress_time($meta['cook_time'][0], 'iso') : '';
             /** @noinspection PhpUndefinedFunctionInspection */
             $result->recipe->prep_time = $meta['prep_time'][0] ? recipress_time($meta['prep_time'][0], 'iso') : '';
             $result->recipe->yield = $meta['yield'][0] ? $meta['yield'][0] : '';
             $result->recipe->serves = $meta['servings'][0] ? $meta['servings'][0] : '';
             $ingredients = $meta['ingredient'];
             $ingredients = unserialize($ingredients[0]);
             foreach ($ingredients as $ingredient) {
                 $newIngredient = $ingredient['amount'] . ' ' . $ingredient['measurement'] . ' ' . $ingredient['ingredient'];
                 if (!empty($ingredient['notes'])) {
                     $newIngredient .= ', ' . $ingredient['notes'];
                 }
                 $result->ingredients[] = trim(str_replace('  ', ' ', $newIngredient));
             }
             $instructions = $meta['instruction'];
             $instructions = unserialize($instructions[0]);
             foreach ($instructions as $instruction) {
                 $result->recipe->instructions .= $instruction['description'];
                 if (!empty($instruction['image'])) {
                     $result->recipe->instructions .= "[br]" . wp_get_attachment_image($instruction['image'], $size, false);
                     $result->recipe->instructions = str_replace('<', '[', $result->recipe->instructions);
                     $result->recipe->instructions = str_replace('>', ']', $result->recipe->instructions);
                 }
                 $result->recipe->instructions .= "\n";
             }
             break;
             /**
              * Make the Recipe Card data look like RecipeSEO/Ziplist - only because we already have the JS for those
              */
         /**
          * Make the Recipe Card data look like RecipeSEO/Ziplist - only because we already have the JS for those
          */
         case 'yumprint':
             $post = $wpdb->get_row("SELECT * FROM " . $wpdb->prefix . "yumprint_recipe_recipe WHERE id=" . $postID);
             $nutrition = json_decode($post->nutrition);
             $result->recipe = json_decode($post->recipe);
             $result->recipe->recipe_title = $result->recipe->title;
             $result->recipe->recipe_image = $result->recipe->image;
             $result->ingredients = $result->recipe->ingredients[0]->lines;
             $result->recipe->instructions = implode("\n", $result->recipe->directions[0]->lines);
             $result->recipe->notes = implode("\n", $result->recipe->notes[0]->lines);
             $result->recipe->prep_time = $this->yumprintTime($result->recipe->prepTime);
             $result->recipe->cook_time = $this->yumprintTime($result->recipe->cookTime);
             $result->recipe->yield = $result->recipe->yields;
             $result->recipe->serving_size = $result->recipe->servings;
             $result->recipe->nutrition = $nutrition;
             $serves = (int) $result->recipe->servings;
             $div = !empty($serves) ? $serves : 1;
             /** @noinspection PhpUnusedLocalVariableInspection */
             foreach ($nutrition as $key => &$value) {
                 $value /= $div;
             }
             $nutrition->calories = round($nutrition->calories);
             $nutrition->totalFat = round($nutrition->totalFat) . 'g';
             $nutrition->saturatedFat = round($nutrition->saturatedFat) . 'g';
             $nutrition->transFat = round($nutrition->transFat) . 'g';
             $nutrition->polyunsaturatedFat = round($nutrition->polyunsaturatedFat);
             $nutrition->monounsaturatedFat = round($nutrition->monounsaturatedFat);
             $nutrition->unsaturatedFat = $nutrition->polyunsaturatedFat + $nutrition->monounsaturatedFat . 'g';
             $nutrition->cholesterol = round($nutrition->cholesterol) . 'mg';
             $nutrition->sodium = round($nutrition->sodium) . 'mg';
             $nutrition->totalCarbohydrates = round($nutrition->totalCarbohydrates) . 'g';
             $nutrition->dietaryFiber = round($nutrition->dietaryFiber) . 'g';
             $nutrition->sugars = round($nutrition->sugars) . 'g';
             $nutrition->protein = round($nutrition->protein) . 'g';
             $result->recipe->nutrition = $nutrition;
             unset($result->recipe->prepTime);
             unset($result->recipe->cookTime);
             unset($result->recipe->totalTime);
             unset($result->recipe->yields);
             unset($result->recipe->servings);
             unset($result->recipe->title);
             unset($result->recipe->image);
             unset($result->recipe->ingredients);
             unset($result->recipe->directions);
             break;
             /**
              * Get me cooking
              */
         /**
          * Get me cooking
          */
         case 'gmc':
         case 'gmc_recipe':
             /**
              * If GMC is installed, use it to get ingredients, but turn off error reporting to stop it crashing
              */
             if (function_exists('print_ingredient_description')) {
                 error_reporting(0);
                 $gmcInstalled = true;
             } else {
                 $gmcInstalled = false;
             }
             $result->recipe = new stdClass();
             $post = get_post($postID);
             $result->recipe->recipe_title = html_entity_decode(get_the_title($postID), ENT_COMPAT, 'UTF-8');
             $result->recipe->author = get_post_meta($postID, 'gmc-source-name', true);
             $result->recipe->summary = get_post_meta($post->ID, "gmc-description", true);
             $thumbID = get_post_thumbnail_id($postID);
             $postThumb = $thumbID != 0 ? wp_get_attachment_image_src($thumbID, 'medium') : '';
             if (!empty($postThumb)) {
                 $result->recipe->recipe_image = $postThumb[0];
             }
             $prepHour = (int) get_post_meta($postID, "gmc-prep-time-hours", true);
             $prepMinute = (int) get_post_meta($postID, "gmc-prep-time-mins", true);
             $cookHour = (int) get_post_meta($postID, "gmc-cooking-time-hours", true);
             $cookMinute = (int) get_post_meta($postID, "gmc-cooking-time-mins", true);
             $result->recipe->prep_time = "PT{$prepHour}H{$prepMinute}M";
             $result->recipe->cook_time = "PT{$cookHour}H{$cookMinute}M";
             $mealTypes = wp_get_object_terms($postID, 'gmc_course');
             if ($mealTypes instanceof WP_Error) {
                 register_taxonomy('gmc_course', 'gmc_recipe');
                 $mealTypes = wp_get_object_terms($postID, 'gmc_course');
             }
             if (is_array($mealTypes) && count($mealTypes) > 0) {
                 $result->recipe->mealType = $mealTypes[0]->name;
             }
             $regions = wp_get_object_terms($postID, 'gmc_region');
             if ($regions instanceof WP_Error) {
                 register_taxonomy('gmc_region', 'gmc_recipe');
                 $regions = wp_get_object_terms($postID, 'gmc_region');
             }
             if (is_array($regions) && count($regions) > 0) {
                 $result->recipe->cuisine = $regions[0]->name;
             }
             $result->ingredients = array();
             $result->recipe->instructions = '';
             $currentSection = '';
             $steps = get_posts('post_status=publish&post_type=gmc_recipestep&nopaging=1&orderby=menu_order&order=ASC&post_parent=' . $postID);
             /** @var $step WP_Post */
             foreach ($steps as $step) {
                 if (!empty($step->post_content)) {
                     $section = get_post_meta($step->ID, 'gmc_stepgroup', true);
                     if (!empty($section) && $section != $currentSection) {
                         $result->recipe->instructions .= "!{$section}\n";
                         $currentSection = $section;
                     }
                     $content = preg_replace('/\\r?\\n/i', '[br]', $step->post_content);
                     $thumbID = get_post_thumbnail_id($step->ID);
                     if ($thumbID) {
                         $postThumb = wp_get_attachment_image_src($thumbID, 'medium');
                         if ($postThumb) {
                             $content .= '[br][img src="' . $postThumb[0] . '" width="' . $postThumb[1] . '" height="' . $postThumb[2] . '"]';
                         }
                     }
                     $result->recipe->instructions .= $content . "\n";
                 }
             }
             $result->recipe->instructions = rtrim($result->recipe->instructions, "\n");
             $ingredients = get_posts('post_status=publish&post_type=gmc_recipeingredient&nopaging=1&orderby=menu_order&order=ASC&post_parent=' . $postID);
             $currentSection = '';
             /** @var $ingredient WP_Post */
             foreach ($ingredients as $ingredient) {
                 $section = get_post_meta($ingredient->ID, "gmc-ingredientgroup", true);
                 if (!empty($section) && $section != $currentSection) {
                     $result->ingredients[] = "!{$section}";
                     $currentSection = $section;
                 }
                 if ($gmcInstalled) {
                     /** @noinspection PhpUndefinedFunctionInspection */
                     $iLine = trim(print_ingredient_description($ingredient));
                 } else {
                     $quantity = get_post_meta($ingredient->ID, "gmc-ingredientquantity", true);
                     $measurement = get_post_meta($ingredient->ID, 'gmc-ingredientmeasurement', true);
                     $title = $ingredient->post_title;
                     $iLine = trim("{$quantity} {$measurement} {$title}");
                 }
                 if (!empty($iLine)) {
                     $result->ingredients[] = $iLine;
                 }
             }
             /**
              * Try to convert HTML in notes into something EasyRecipe will understand
              */
             $notes = preg_replace_callback('%<(strong|em)>(.*?)</\\1>%', array($this, 'notesConversion'), $post->post_content);
             $notes = preg_replace('%<a ([^>]+)>(.*?)</a>%i', '[url $1]$2[/url]', $notes);
             $result->recipe->notes = strip_tags($notes);
             $result->recipe->yield = $servings = get_post_meta($post->ID, "gmc-nr-servings", true);
             $nutrition = new stdClass();
             if (get_post_meta($postID, "gmc_has_nutrition", true)) {
                 $result->recipe->serving_size = get_post_meta($post->ID, "gmc_gda_servings", true);
                 $nutrition->calories = get_post_meta($post->ID, "gmc_nutrition_kcal_serving", true);
                 if ($nutrition->calories != '') {
                     $nutrition->calories *= 1000;
                 }
                 $nutrition->totalFat = round(get_post_meta($post->ID, "gmc_nutrition_fat_total_serving", true)) . 'g';
                 $nutrition->saturatedFat = round(get_post_meta($post->ID, "gmc_nutrition_fat_sat_serving", true)) . 'g';
                 $nutrition->sodium = round(get_post_meta($post->ID, "gmc_nutrition_salt_sod_serving", true)) . 'mg';
                 $nutrition->totalCarbohydrates = round(get_post_meta($post->ID, "gmc_nutrition_carb_total_serving", true)) . 'g';
                 $nutrition->dietaryFiber = round(get_post_meta($post->ID, "gmc_nutrition_fibre_serving", true)) . 'g';
                 $nutrition->sugars = round(get_post_meta($post->ID, "gmc_nutrition_carb_sugar_serving", true)) . 'g';
                 $nutrition->protein = round(get_post_meta($post->ID, "gmc_nutrition_protein_serving", true)) . 'g';
             }
             $result->recipe->nutrition = $nutrition;
             break;
         case 'kitchebug':
             break;
     }
     return $result;
 }
예제 #2
0
function recipress_recipe($field, $attr = null)
{
    global $post;
    $meta = get_post_custom($post->ID);
    switch ($field) {
        // title
        case 'title':
            $title = get_the_title() . ' ' . __('Recipe', 'recipress');
            $recipe_title = $meta['title'][0];
            if ($recipe_title) {
                $title = $recipe_title;
            }
            return $title;
            break;
            // photo
        // photo
        case 'photo':
            if (current_theme_supports('post-thumbnails') && recipress_options('use_photo') != 'no') {
                $photo = get_the_post_thumbnail($post->ID, 'thumbnail', $attr);
            } else {
                $photo_id = $meta['photo'][0];
                $photo = wp_get_attachment_image($photo_id, 'thumbnail', false, $attr);
            }
            return $photo;
            break;
            // summary
        // summary
        case 'summary':
            return $meta['summary'][0];
            break;
            // cuisine
        // cuisine
        case 'cuisine':
            $cuisine = get_the_term_list($post->ID, 'cuisine', $attr);
            return $cuisine;
            break;
            // course
        // course
        case 'course':
            $course = get_the_term_list($post->ID, 'course', $attr);
            return $course;
            break;
            // skill_level
        // skill_level
        case 'skill_level':
            $skill_level = get_the_term_list($post->ID, 'skill_level', $attr);
            return $skill_level;
            break;
            // prep_time
        // prep_time
        case 'prep_time':
            $prep_time = $meta['prep_time'][0];
            $prep_time = recipress_time($prep_time, $attr);
            return $prep_time;
            break;
            // cook_time
        // cook_time
        case 'cook_time':
            $cook_time = $meta['cook_time'][0];
            $cook_time = recipress_time($cook_time, $attr);
            return $cook_time;
            break;
            // ready_time
        // ready_time
        case 'ready_time':
            $prep_time = $meta['prep_time'][0];
            $cook_time = $meta['cook_time'][0];
            $other_time = $meta['other_time'][0];
            $ready_time = $prep_time + $cook_time + $other_time;
            $ready_time = recipress_time($ready_time, $attr);
            return $ready_time;
            break;
            // yield
        // yield
        case 'yield':
            $yield = $meta['yield'][0];
            $servings = $meta['servings'][0];
            if ($yield && $servings) {
                $yield = $yield . ' (' . $servings . ' ' . __('Servings', 'recipress') . ')';
            }
            if (!$yield && $servings) {
                $yield = $servings . ' ' . __('Servings', 'recipress');
            }
            return $yield;
            break;
            // cost
        // cost
        case 'cost':
            $cost = $meta['cost'][0];
            return $cost;
            break;
            // ingredients
        // ingredients
        case 'ingredients':
            $ingredients = $meta['ingredient'];
            foreach ($ingredients as $ingredient) {
                $ingredients = unserialize($ingredient);
            }
            $output = $ingredients;
            return $output;
            break;
            // instructions
        // instructions
        case 'instructions':
            $instructions = $meta['instruction'];
            foreach ($instructions as $instruction) {
                $instructions = unserialize($instruction);
            }
            $output = $instructions;
            return $output;
            break;
        default:
            return $meta[$field][0];
    }
    // end switch
}