/**
  * Parse out amount values from the start of a given string.
  *
  * This method checks the first two words in a string for possible
  * numerical values. It will automatically recognize integers, floats,
  * and fractions or combination of any two. If any exist in the first
  * two words of a string, a total value will be determined by adding
  * them together.
  *
  * @since  1.2.0
  * @access private
  *
  * @param  string      $string The string from user input.
  * @return array|false $result The resulting amount information or false if none exists.
  */
 private function parse_amount($string)
 {
     // Isolate the first word.
     $first_word = strtok($string, ' ');
     // Get amount string if it meets our criteria.
     $amount_length = strspn($first_word, '0123456789/.');
     $amount_string = substr($string, 0, $amount_length);
     if ($amount_string) {
         // Format the amount.
         $amount = trim($amount_string);
         $amount = Simmer_Recipe_Ingredient::convert_amount_to_float($amount);
         // Isolate the second word to check for fractions or floats.
         $string = substr($string, $amount_length);
         $string = trim($string);
         $second_word = strtok($string, ' ');
         // Filter the second word against allowed characters.
         $fraction_length = strspn($second_word, '0123456789/.');
         $fraction_string = substr($string, 0, $fraction_length);
         // If a fraction or float does indeed exist, parse it.
         if ($fraction_string) {
             // Update the final amount string to include the fraction or float.
             $amount_string = $amount_string . ' ' . $fraction_string;
             $amount_length = strlen($amount_string);
             // Format the fraction to a float.
             $fraction_amount = trim($fraction_string);
             $fraction_amount = Simmer_Recipe_Ingredient::convert_amount_to_float($fraction_amount);
             // Add the two together for one final value.
             $amount = $amount + $fraction_amount;
         }
         $result = array('start' => 0, 'end' => $amount_length, 'result' => $amount);
     } else {
         $result = false;
     }
     return $result;
 }
Esempio n. 2
0
 /**
  * Sort two ingredients by their amounts.
  *
  * @since  1.3.3
  * @access private
  *
  * @see usort()
  *
  * @param  object $a The first ingredient object.
  * @param  object $b The second ingredient object.
  * @return int       Whether first amount is greater than, less than, or equal to the second.
  */
 private function sort_ingredients_by_amount($a, $b)
 {
     // Get the filtered amounts.
     $a = $a->get_amount();
     $b = $b->get_amount();
     // Convert the amounts to floats.
     $a = Simmer_Recipe_Ingredient::convert_amount_to_float($a);
     $b = Simmer_Recipe_Ingredient::convert_amount_to_float($b);
     if ($a == $b) {
         return 0;
     }
     return $a < $b ? -1 : 1;
 }
Esempio n. 3
0
/**
 * Print or return a <select> field of all avialable units of measure.
 *
 * @since 1.0.0
 *
 * @param array $args {
 *     Optional. The custom arguments.
 *
 *     @type string $name      The field element's name attribute. Default 'simmer-unit'.
 *     @type string $select    The unit slug to be selected. Default none.
 *     @type string $id        The field element's id attribute. Default none.
 *     @type string $class     The field element's class attribute. Default 'simmer-units-dropdown'.
 *     @type int    $tab_index The field tab index. Default '0'.
 *     @type bool   $echo      Whether to echo or return the field element. Default 'true'.
 * }
 * @return string $output The generated <select> field.
 */
function simmer_units_select_field($args = '', $count = 1)
{
    // Get the available measurement units.
    $units = Simmer_Recipe_Ingredients::get_units();
    // If there are none, then stop generating the <select> field.
    if (empty($units)) {
        return false;
    }
    $defaults = array('name' => 'simmer-unit', 'selected' => '', 'id' => '', 'class' => 'simmer-units-dropdown', 'tab_index' => 0, 'echo' => true);
    $args = wp_parse_args($args, $defaults);
    /**
     * Allow others to filter the args.
     *
     * @since 1.0.0
     */
    $args = apply_filters('simmer_units_select_field_args', $args);
    // Start building an array of <select> field attributes.
    $attributes = array();
    // If an id is set, add it to the attributes array.
    if (!empty($args['id'])) {
        $attributes['id'] = $args['id'];
    }
    // If a class is set, add it to the attributes array.
    if (!empty($args['class'])) {
        $attributes['class'] = $args['class'];
    }
    // If a name is set, add it to the attributes array.
    if (!empty($args['name'])) {
        $attributes['name'] = $args['name'];
    }
    // If a tab index is set, create the attribute.
    if ((int) $args['tab_index'] > 0) {
        $attributes['tabindex'] = (int) $args['tab_index'];
    }
    /**
     * Allow others to modify the array of attributes.
     *
     * @since 1.0.0
     */
    $attributes = apply_filters('simmer_units_select_field_attributes', $attributes);
    // If no attributes are defined above, then we have nothing more to discuss.
    if (empty($attributes) || !is_array($attributes)) {
        return false;
    }
    // Start building the <select> field.
    $output = '<select';
    foreach ($attributes as $attribute => $value) {
        $output .= ' ' . esc_attr($attribute) . '="' . esc_attr($value) . '"';
    }
    $output .= '>';
    $output .= '<option value="" ' . selected('', $args['selected'], false) . '></option>';
    foreach ($units as $unit_type => $units) {
        $output .= '<optgroup label="' . esc_html(ucfirst($unit_type)) . '">';
        foreach ($units as $unit => $labels) {
            $output .= '<option value="' . esc_attr($unit) . '" ' . selected($unit, $args['selected'], false) . '>';
            $output .= esc_html(Simmer_Recipe_Ingredient::get_unit_label($labels, $count));
            $output .= '</option>';
        }
        $output .= '</optgroup>';
    }
    $output .= '</select>';
    // Echo or return the resulting select field.
    if ((bool) $args['echo']) {
        echo $output;
    } else {
        return $output;
    }
}
 /**
  * Save the recipe meta.
  *
  * @since 1.0.0
  *
  * @param  int $recipe_id The ID of the current recipe.
  * @return void
  */
 public function save_recipe_meta($id)
 {
     // Check if this is an autosave.
     if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
         return;
     }
     // Check that this was a POST request.
     if ('POST' != strtoupper($_SERVER['REQUEST_METHOD'])) {
         return;
     }
     // Check the save recipe nonce.
     if (!wp_verify_nonce($_POST['simmer_nonce'], 'simmer_save_recipe_meta')) {
         return;
     }
     /** Ingredients **/
     // Add or update ingredient items.
     if (isset($_POST['simmer_ingredients']) && !empty($_POST['simmer_ingredients'])) {
         foreach ($_POST['simmer_ingredients'] as $key => $ingredient) {
             if (empty($ingredient['description'])) {
                 continue;
             }
             $amount = isset($ingredient['amount']) ? Simmer_Recipe_Ingredient::convert_amount_to_float($ingredient['amount']) : '';
             $unit = isset($ingredient['unit']) ? $ingredient['unit'] : '';
             if (isset($ingredient['id']) && $ingredient['id']) {
                 $ingredient_id = simmer_update_recipe_ingredient($ingredient['id'], array('amount' => $amount, 'unit' => $unit, 'description' => $ingredient['description'], 'order' => $ingredient['order'], 'is_heading' => $ingredient['heading']));
             } else {
                 $ingredient_id = simmer_add_recipe_ingredient(get_the_ID(), $ingredient['description'], $amount, $unit, $ingredient['heading'], $ingredient['order']);
             }
             /**
              * Fire after saving a recipe ingredient.
              *
              * @since 1.3.0
              *
              * @param int $ingredient_id The ingredient ID.
              * @param int $id            The recipe ID.
              */
             do_action('simmer_admin_save_recipe_ingredient', $ingredient_id, $id);
         }
     }
     // Delete 'removed' ingredient items.
     if (isset($_POST['simmer_ingredients_remove']) && !empty($_POST['simmer_ingredients_remove'])) {
         foreach ($_POST['simmer_ingredients_remove'] as $ingredient_id) {
             simmer_delete_recipe_ingredient($ingredient_id);
             /**
              * Fire after deleting a recipe ingredient.
              *
              * @since 1.3.0
              */
             do_action('simmer_admin_delete_recipe_ingredient');
         }
     }
     /** Instructions **/
     // Add or update instruction items.
     if (isset($_POST['simmer_instructions']) && !empty($_POST['simmer_instructions'])) {
         foreach ($_POST['simmer_instructions'] as $key => $instruction) {
             if (empty($instruction['description'])) {
                 continue;
             }
             if (isset($instruction['id']) && $instruction['id']) {
                 $instruction_id = simmer_update_recipe_instruction($instruction['id'], array('description' => $instruction['description'], 'is_heading' => $instruction['heading'], 'order' => $instruction['order']));
             } else {
                 $instruction_id = simmer_add_recipe_instruction(get_the_ID(), $instruction['description'], $instruction['heading'], $instruction['order']);
             }
             /**
              * Fire after saving a recipe instruction.
              *
              * @since 1.3.0
              *
              * @param int $instruction_id The instruction ID.
              * @param int $id            The recipe ID.
              */
             do_action('simmer_admin_save_recipe_instruction', $instruction_id, $id);
         }
     }
     // Delete 'removed' instruction items.
     if (isset($_POST['simmer_instructions_remove']) && !empty($_POST['simmer_instructions_remove'])) {
         foreach ($_POST['simmer_instructions_remove'] as $instruction_id) {
             simmer_delete_recipe_instruction($instruction_id);
             /**
              * Fire after deleting a recipe instruction.
              *
              * @since 1.3.0
              */
             do_action('simmer_admin_delete_recipe_instruction');
         }
     }
     /** Information **/
     // Save the prep, cooking, and total times.
     $times = (array) $_POST['simmer_times'];
     if (!empty($times)) {
         foreach ($times as $time => $values) {
             // Convert the hours input to minutes.
             $hours = (int) $values['h'] * 60;
             $minutes = (int) $values['m'];
             $duration = $hours + $minutes;
             if (0 != $duration) {
                 update_post_meta($id, '_recipe_' . $time . '_time', $duration);
             } else {
                 delete_post_meta($id, '_recipe_' . $time . '_time');
             }
         }
     }
     // Maybe save the servings.
     $servings = absint($_POST['simmer_servings']);
     if (!empty($servings)) {
         update_post_meta($id, '_recipe_servings', absint($_POST['simmer_servings']));
     } else {
         delete_post_meta($id, '_recipe_servings');
     }
     // Maybe save the servings label.
     if (!empty($_POST['simmer_servings_label'])) {
         update_post_meta($id, '_recipe_servings_label', $_POST['simmer_servings_label']);
     } else {
         delete_post_meta($id, '_recipe_servings_label');
     }
     // Maybe save the yield.
     if (!empty($_POST['simmer_yield'])) {
         update_post_meta($id, '_recipe_yield', $_POST['simmer_yield']);
     } else {
         delete_post_meta($id, '_recipe_yield');
     }
     // Maybe save the source text.
     if (!empty($_POST['simmer_source_text'])) {
         update_post_meta($id, '_recipe_source_text', $_POST['simmer_source_text']);
     } else {
         delete_post_meta($id, '_recipe_source_text');
     }
     // Maybe save the source URL.
     if (!empty($_POST['simmer_source_url'])) {
         update_post_meta($id, '_recipe_source_url', $_POST['simmer_source_url']);
     } else {
         delete_post_meta($id, '_recipe_source_url');
     }
     /**
      * Allow others to do things when the recipe meta is saved.
      *
      * @since 1.0.0
      *
      * @param int $id The current recipe ID.
      * @return void
      */
     do_action('simmer_save_recipe_meta', $id);
 }