graylaurenm
1/8/2017 - 4:11 PM

MPP to WPRM "Servings" Fix

MPP to WPRM "Servings" Fix

<?php

$recipes = WPRM_Recipe_Manager::get_recipes();

foreach ( $recipes as $recipe_id => $options ) {
    $recipe = WPRM_Recipe_Manager::get_recipe( $recipe_id );

    if ( ! $recipe->servings() ) {
        $import_source = get_post_meta( $recipe_id, 'wprm_import_source', true );

        if ( 'mealplannerpro' === $import_source || 'mealplannerpro-checked' === $import_source ) {
            $import_backup = get_post_meta( $recipe_id, 'wprm_import_backup', true );
            $mpp_recipe_id = $import_backup['mpp_recipe_id'];

            global $wpdb;
            $mpp_recipe = $wpdb->get_row( 'SELECT * FROM ' . $wpdb->prefix . 'mpprecipe_recipes WHERE recipe_id=' . $mpp_recipe_id );

            // Get servings from mpp servings.
            $match = preg_match( '/^\s*\d+/', $mpp_recipe->serving_size, $servings_array );
            if ( 1 === $match ) {
                    $servings = str_replace( ' ','', $servings_array[0] );
            } else {
                    $servings = '';
            }

            $servings_unit = preg_replace( '/^\s*\d+\s*/', '', $mpp_recipe->serving_size );

            // Update WPRM recipe.
            update_post_meta( $recipe_id, 'wprm_servings', $servings );
            update_post_meta( $recipe_id, 'wprm_servings_unit', $servings_unit );
        }
    }
}