Skip to content

Instantly share code, notes, and snippets.

@mbeall
Created August 23, 2013 07:11
Show Gist options
  • Save mbeall/6316376 to your computer and use it in GitHub Desktop.
Save mbeall/6316376 to your computer and use it in GitHub Desktop.
<?php
/*
Plugin Name: Sparks Food
Plugin URI: http://starverte.com/plugins/sparks-food
Description: Part of the Sparks Framework. A plugin that allows for easy restaurant management.
Version: 0.1.0
Author: Star Verte LLC
Author URI: http://www.starverte.com
License: GPLv2 or later
Copyright 2013 Star Verte LLC (email : info@starverte.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
You should have received a copy of the GNU General Public License
along with Sparks Food. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* Register custom post type sparks_food
*/
add_action( 'init', 'register_sparks_food', 0 );
function register_sparks_food() {
$labels = array(
'name' => _x( 'Food', 'Post Type General Name', 'sparks' ),
'singular_name' => _x( 'Food Item', 'Post Type Singular Name', 'sparks' ),
'menu_name' => __( 'Food', 'sparks' ),
'parent_item_colon' => __( 'Parent Food Item:', 'sparks' ),
'all_items' => __( 'All Food Items', 'sparks' ),
'view_item' => __( 'View Food', 'sparks' ),
'add_new_item' => __( 'Add New Food Item', 'sparks' ),
'add_new' => __( 'New Food Item', 'sparks' ),
'edit_item' => __( 'Edit Food Item', 'sparks' ),
'update_item' => __( 'Update Food Item', 'sparks' ),
'search_items' => __( 'Search food items', 'sparks' ),
'not_found' => __( 'No food items found', 'sparks' ),
'not_found_in_trash' => __( 'No food items found in Trash. Did you check the compost?', 'sparks' ),
);
$rewrite = array(
'slug' => 'food',
'with_front' => false,
'pages' => false,
'feeds' => true,
);
$args = array(
'label' => __( 'sparks_food', 'sparks' ),
'description' => __( 'Items on a restaurant menu', 'sparks' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'thumbnail', ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'menu_icon' => '',
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'rewrite' => $rewrite,
'capability_type' => 'page',
);
register_post_type( 'sparks_food', $args );
}
/*
* Register custom taxonomy food_marker
*/
add_action( 'init', 'register_food_marker', 0 );
function register_food_marker() {
$labels = array(
'name' => _x( 'Markers', 'Taxonomy General Name', 'sparks' ),
'singular_name' => _x( 'Marker', 'Taxonomy Singular Name', 'sparks' ),
'menu_name' => __( 'Food Markers', 'sparks' ),
'all_items' => __( 'All Markers', 'sparks' ),
'parent_item' => __( 'Parent Marker', 'sparks' ),
'parent_item_colon' => __( 'Parent Marker:', 'sparks' ),
'new_item_name' => __( 'New Marker Name', 'sparks' ),
'add_new_item' => __( 'Add New Marker', 'sparks' ),
'edit_item' => __( 'Edit Marker', 'sparks' ),
'update_item' => __( 'Update Marker', 'sparks' ),
'separate_items_with_commas' => __( 'Separate markers with commas', 'sparks' ),
'search_items' => __( 'Search markers', 'sparks' ),
'add_or_remove_items' => __( 'Add or remove markers', 'sparks' ),
'choose_from_most_used' => __( 'Choose from the most used markers', 'sparks' ),
);
$rewrite = array(
'slug' => 'markers',
'with_front' => true,
'hierarchical' => false,
);
$args = array(
'labels' => $labels,
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => false,
'show_tagcloud' => false,
'rewrite' => $rewrite,
);
register_taxonomy( 'food_marker', 'sparks_food', $args );
}
/*
* Create custom meta box
*/
add_action( 'add_meta_boxes', 'sparks_food_meta_boxes' );
function sparks_food_meta_boxes() { add_meta_box('sparks_food_meta', 'Details', 'sparks_food_meta', 'sparks_food', 'side', 'high'); }
function sparks_food_meta() {
global $post;
$custom = get_post_custom($post->ID); ?>
<p><input type="text" size="15" name="food_ref" value="<?php if (isset($custom['food_ref'])) { echo $custom["food_ref"] [0]; } ?>" placeholder="Ref #">
<input type="text" size="10" name="food_price" value="<?php if (isset($custom['food_price'])) { echo $custom["food_price"] [0]; } ?>" placeholder="Base Price"></p>
<p><input type="text" size="20" name="food_option_a" value="<?php if (isset($custom['food_option_a'])) { echo $custom["food_option_a"] [0]; } ?>" placeholder="Option A">
<input type="text" size="5" name="food_option_a_cost" value="<?php if (isset($custom['food_option_a_cost'])) { echo $custom["food_option_a_cost"] [0]; } ?>" placeholder="Cost"></p>
<p><input type="text" size="20" name="food_option_b" value="<?php if (isset($custom['food_option_b'])) { echo $custom["food_option_b"] [0]; } ?>" placeholder="Option B">
<input type="text" size="5" name="food_option_b_cost" value="<?php if (isset($custom['food_option_b_cost'])) { echo $custom["food_option_b_cost"] [0]; } ?>" placeholder="Cost"></p>
<p><input type="text" size="20" name="food_option_c" value="<?php if (isset($custom['food_option_c'])) { echo $custom["food_option_c"] [0]; } ?>" placeholder="Option C">
<input type="text" size="5" name="food_option_c_cost" value="<?php if (isset($custom['food_option_c_cost'])) { echo $custom["food_option_c_cost"] [0]; } ?>" placeholder="Cost"></p>
<p><input type="text" size="20" name="food_option_d" value="<?php if (isset($custom['food_option_d'])) { echo $custom["food_option_d"] [0]; } ?>" placeholder="Option D">
<input type="text" size="5" name="food_option_d_cost" value="<?php if (isset($custom['food_option_d_cost'])) { echo $custom["food_option_d_cost"] [0]; } ?>" placeholder="Cost"></p>
<p><input type="text" size="20" name="food_option_e" value="<?php if (isset($custom['food_option_e'])) { echo $custom["food_option_e"] [0]; } ?>" placeholder="Option E">
<input type="text" size="5" name="food_option_e_cost" value="<?php if (isset($custom['food_option_e_cost'])) { echo $custom["food_option_e_cost"] [0]; } ?>" placeholder="Cost"></p><?php
}
/*
* Save data from meta boxes
*/
add_action('save_post', 'save_sparks_food');
function save_sparks_food() {
global $post;
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE && (isset($post_id))) { return $post_id; }
if(defined('DOING_AJAX') && DOING_AJAX && (isset($post_id))) { return $post_id; } //Prevents the metaboxes from being overwritten while quick editing.
if(ereg('/\edit\.php', $_SERVER['REQUEST_URI']) && (isset($post_id))) { return $post_id; } //Detects if the save action is coming from a quick edit/batch edit.
if (isset($_POST['food_ref'])) { update_post_meta($post->ID, "food_ref", $_POST["food_ref"]); }
if (!empty($_POST['food_price'])) { $number = number_format(($_POST["food_price"]), 2, '.', ''); update_post_meta($post->ID, "food_price", $number); }
elseif (isset($_POST['food_price'])) {update_post_meta($post->ID, "food_price", ""); }
if (!empty($_POST['food_option_a'])) {
update_post_meta($post->ID, "food_option_a", $_POST["food_option_a"]);
if (!empty($_POST['food_option_a_cost'])) { $number = number_format(($_POST["food_option_a_cost"]), 2, '.', ''); update_post_meta($post->ID, "food_option_a_cost", $number); }
else { update_post_meta($post->ID, "food_option_a_cost", "0.00"); }
}
elseif (isset($_POST['food_option_a']) & isset($_POST['food_option_a_cost'])) { update_post_meta($post->ID, "food_option_a", ""); update_post_meta($post->ID, "food_option_a_cost", ""); }
if (!empty($_POST['food_option_b'])) {
update_post_meta($post->ID, "food_option_b", $_POST["food_option_b"]);
if (!empty($_POST['food_option_b_cost'])) { $number = number_format(($_POST["food_option_b_cost"]), 2, '.', ''); update_post_meta($post->ID, "food_option_b_cost", $number); }
else { update_post_meta($post->ID, "food_option_b_cost", "0.00"); }
}
elseif (isset($_POST['food_option_b']) & isset($_POST['food_option_b_cost'])) { update_post_meta($post->ID, "food_option_b", ""); update_post_meta($post->ID, "food_option_b_cost", ""); }
if (!empty($_POST['food_option_c'])) {
update_post_meta($post->ID, "food_option_c", $_POST["food_option_c"]);
if (!empty($_POST['food_option_c_cost'])) { $number = number_format(($_POST["food_option_c_cost"]), 2, '.', ''); update_post_meta($post->ID, "food_option_c_cost", $number); }
else { update_post_meta($post->ID, "food_option_c_cost", "0.00"); }
}
elseif (isset($_POST['food_option_c']) & isset($_POST['food_option_c_cost'])) { update_post_meta($post->ID, "food_option_c", ""); update_post_meta($post->ID, "food_option_c_cost", ""); }
if (!empty($_POST['food_option_d'])) {
update_post_meta($post->ID, "food_option_d", $_POST["food_option_d"]);
if (!empty($_POST['food_option_d_cost'])) { $number = number_format(($_POST["food_option_d_cost"]), 2, '.', ''); update_post_meta($post->ID, "food_option_d_cost", $number); }
else { update_post_meta($post->ID, "food_option_d_cost", "0.00"); }
}
elseif (isset($_POST['food_option_d']) & isset($_POST['food_option_d_cost'])) { update_post_meta($post->ID, "food_option_d", ""); update_post_meta($post->ID, "food_option_d_cost", ""); }
if (!empty($_POST['food_option_e'])) {
update_post_meta($post->ID, "food_option_e", $_POST["food_option_e"]);
if (!empty($_POST['food_option_e_cost'])) { $number = number_format(($_POST["food_option_e_cost"]), 2, '.', ''); update_post_meta($post->ID, "food_option_e_cost", $number); }
else { update_post_meta($post->ID, "food_option_e_cost", "0.00"); }
}
elseif (isset($_POST['food_option_e']) & isset($_POST['food_option_e_cost'])) { update_post_meta($post->ID, "food_option_e", ""); update_post_meta($post->ID, "food_option_e_cost", ""); }
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment