/** * Parse out a unit value from the start of a given string. * * This method checks the first word in a string for possible * unit label matches as defined by the core measurement units. * It will account for capitalization, abbreviation, & other oddities. * * @since 1.2.0 * @access private * * @param string $string The string from user input. * @return array|false $result The resulting unit information or no match was found. */ private function parse_unit($string) { $_unit = ''; $end = 0; // Isolate the first word. $first_word = strtok($string, ' '); // Get the available measurement units. $units = Simmer_Recipe_Ingredients::get_units(); $_units = array(); // Remove unit types (volume, weight, etc...) from array. foreach ($units as $unit) { $_units = array_merge($unit, $_units); } // Loop through each unit & its labels for a match. foreach ($_units as $unit => $labels) { foreach ($labels as $label) { // Check that no match has been found yet. if ('' != $_unit && 0 == $end) { continue; } // Force lowercase and remove . from input unit. $_first_word = strtolower($first_word); $_first_word = str_replace('.', '', $_first_word); // Force lowercase and remove . from looped label. $_label = strtolower($label); $_label = str_replace('.', '', $_label); // If we have a match, set the values. if ($_first_word === $_label) { $_unit = $unit; $end = strlen($first_word); } } } if ($_unit && $end) { $result = array('result' => $_unit, 'start' => 0, 'end' => $end); } else { $result = false; } return $result; }
/** * 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; } }
/** * Get the approprate label for a given unit based on count. * * @since 1.0.0 * * @param array $unit The given unit & its labels. * @param int $count Optional. The ingredient count. * @return string $label The appropriate label. */ public static function get_unit_label($unit, $count = 1) { if (!is_array($unit)) { $units = Simmer_Recipe_Ingredients::get_units(); foreach ($units as $type => $units) { if (isset($units[$unit])) { $unit = $units[$unit]; break; } } } // If an abbreviation is set, use that. if ('abbr' == get_option('simmer_units_format') && isset($unit['abbr']) && !empty($unit['abbr'])) { $label = $unit['abbr']; // Otherwise, choose either plural or single based on the count. } else { if (isset($unit['plural']) && 1 < (double) $count) { $label = $unit['plural']; } else { if (isset($unit['single'])) { $label = $unit['single']; // Finally, if none of the appropriate labels are set then bail. } else { return false; } } } /** * Filter a single unit of measure's label. * * @since 1.0.0 * * @param string $label The generated label. * @param string $unit The raw unit slug. * @param int $count The ingredient count. Used to determine singular vs. plural. */ $label = apply_filters('simmer_get_unit_label', $label, $unit, $count); return $label; }
/** * Delete an existing ingredient. * * @since 1.3.0 * * @param int $ingredient_id The ID for the ingredient you want to delete. * @return bool $result Whether the ingredient was deleted. */ function simmer_delete_recipe_ingredient($ingredient_id) { $ingredients_api = new Simmer_Recipe_Ingredients(); $result = $ingredients_api->delete_ingredient($ingredient_id); return $result; }
/** * Get the ingredients list type. * * @since 1.0.0 * * @return string $type The ingredients list type. */ function simmer_get_ingredients_list_type() { $ingredients = new Simmer_Recipe_Ingredients(); $type = $ingredients->get_list_type(); return $type; }