whyisjake

ninnypants

May 19th, 2010
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 12.95 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: eShop Mamma Store
  4. Plugin URI: http://eshopmamma.com
  5. Description: Totally rad shopping card system for eShopMamma
  6. Author: Jake Spurlock
  7. Version: 0.5
  8. Author URI: http://jakespurlock.com
  9. */  
  10.    
  11. /*  Copyright 2010
  12.  
  13. This program is free software; you can redistribute it and/or modify
  14. it under the terms of the GNU General Public License as published by
  15. the Free Software Foundation; either version 2 of the License, or
  16. (at your option) any later version.
  17.  
  18. This program is distributed in the hope that it will be useful,
  19. but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21. GNU General Public License for more details.
  22.  
  23. You should have received a copy of the GNU General Public License
  24. along with this program; if not, write to the Free Software
  25. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  26. */
  27.  
  28. $themename = "eShop Momma Store";
  29. $shortname = "mama";
  30.  
  31. add_action( 'init', 'create_my_post_types' );
  32.  
  33. function create_my_post_types() {
  34.     register_post_type( 'the_store',
  35.         array(
  36.             'label' => __( 'Products' ),
  37.             'singular_label' => __( 'Product' ),
  38.             'public' => true,
  39.             'show_ui' => true,
  40.             'publicly_queryable' => true,
  41.             'exclude_from_search' => false,
  42.             'menu_position' => 20,
  43.             'hierarchical' => false,
  44.             'query_var' => true,
  45.             'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail', 'comments' ),
  46.             'rewrite' => array( 'slug' => 'store', 'with_front' => false ),
  47.             'permalink_epmask' => EP_PERMALINK,
  48.         )
  49.     );
  50.     register_taxonomy( 'Product Line', 'the_store', array( 'hierarchical' => true, 'label' => __('Product Lines') ) );
  51.     register_taxonomy( 'Product Tags', 'the_store', array( 'hierarchical' => false, 'label' => __('Product Tags'), 'query_var' => 'prod_tags', 'rewrite' => array('slug' => 'product-tags' ) ) );
  52.  
  53. }
  54.  
  55. add_filter( 'pre_get_posts', 'my_get_posts' );
  56.  
  57. function my_get_posts( $query ) {
  58.  
  59.     if ( is_home() )
  60.         $query->set( 'post_type', array( 'post', 'product', 'the_store', 'attachment' ) );
  61.  
  62.     return $query;
  63. }
  64.  
  65. //Here is an action to create a custom theme page for displaying content from the store. This will be useful to theme developers.
  66.  
  67. // Template selection
  68.  
  69. add_action("template_redirect", 'my_template_redirect');
  70.  
  71. function my_template_redirect()
  72. {
  73.     global $wp;
  74.     global $wp_query;
  75.     if ($wp->query_vars["post_type"] == "the_store")
  76.     {
  77.         // Let's look for the property.php template file in the current theme
  78.         if (have_posts())
  79.         {
  80.             include(TEMPLATEPATH . '/store.php');
  81.             die();
  82.         }
  83.         else
  84.         {
  85.             $wp_query->is_404 = true;
  86.         }
  87.     }
  88. }
  89.  
  90. /*Options Page*/
  91.  
  92. add_action('admin_menu', 'my_plugin_menu');
  93.  
  94. function my_plugin_menu() {
  95.  
  96.   add_submenu_page('edit.php?post_type=the_store', 'eShop Momma Options', 'Store Options', 'manage_options', 'js_store_options', 'my_plugin_options');
  97.  
  98. }
  99.  
  100.  
  101. function my_plugin_options() {
  102.  
  103.   if (!current_user_can('manage_options'))  {
  104.     wp_die( __('You do not have sufficient permissions to access this page.') );
  105.   }
  106.  
  107. global $themename, $shortname;
  108. $options = array (
  109.  
  110. array( "name" => "$themename",
  111.     "type" => "title"),
  112.  
  113. array( "type" => "open"),
  114.  
  115. array( "name" => "PayPal Email",
  116.     "desc" => "Enter your PayPal email here.",
  117.     "id" => $shortname."_paypal",
  118.     "type" => "text",
  119.     "std" => ""),
  120.  
  121. array( "name" => "Default Handling",
  122.     "desc" => "Any fee that you want automatically added to every purchase.",
  123.     "id" => $shortname."_handling",
  124.     "type" => "text",
  125.     "std" => "0.00"),
  126.  
  127. /*array( "name" => "Footer Stuff",
  128.     "desc" => "Here is where you add stuff to the end of the body tag.",
  129.     "id" => $shortname."_footer_stuff",
  130.     "type" => "radio",
  131.     "std" => ""),   */
  132.  
  133. array( "type" => "close")
  134.  
  135. );
  136.  
  137.     echo '<div class="wrap">';
  138.     echo '<h2>'; echo $themename; echo ' Settings</h2>'; ?>
  139.     <form method="post">
  140.  
  141. <?php foreach ($options as $value) {
  142. switch ( $value['type'] ) {
  143.  
  144. case "open":
  145. ?>
  146. <table width="100%" border="0" style="background-color:#eef5fb; padding:10px;">
  147.  
  148. <?php break;
  149.  
  150. case "close":
  151. ?>
  152.  
  153. </table><br />
  154.  
  155. <?php break;
  156.  
  157. case "title":
  158. ?>
  159. <table width="100%" border="0" style="background-color:#dceefc; padding:5px 10px;"><tr>
  160. <td colspan="2"><h3 style="font-family:Georgia,'Times New Roman',Times,serif;"><?php echo $value['name']; ?></h3></td>
  161. </tr>
  162.  
  163. <?php break;
  164.  
  165. case 'text':
  166. ?>
  167.  
  168. <tr>
  169. <td width="20%" rowspan="2" valign="middle"><strong><?php echo $value['name']; ?></strong></td>
  170. <td width="80%"><input style="width:400px;" name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>" type="<?php echo $value['type']; ?>" value="<?php if ( get_settings( $value['id'] ) != "") { echo get_settings( $value['id'] ); } else { echo $value['std']; } ?>" /></td>
  171. </tr>
  172.  
  173. <tr>
  174. <td><small><?php echo $value['desc']; ?></small></td>
  175. </tr><tr><td colspan="2" style="margin-bottom:5px;border-bottom:1px dotted #000000;">&nbsp;</td></tr><tr><td colspan="2">&nbsp;</td></tr>
  176.  
  177. <?php
  178. break;
  179.  
  180. case 'textarea':
  181. ?>
  182.  
  183. <tr>
  184. <td width="20%" rowspan="2" valign="middle"><strong><?php echo $value['name']; ?></strong></td>
  185. <td width="80%"><textarea name="<?php echo $value['id']; ?>" style="width:400px; height:200px;" type="<?php echo $value['type']; ?>" cols="" rows=""><?php if ( get_settings( $value['id'] ) != "") { echo get_settings( $value['id'] ); } else { echo $value['std']; } ?></textarea></td>
  186.  
  187. </tr>
  188.  
  189. <tr>
  190. <td><small><?php echo $value['desc']; ?></small></td>
  191. </tr><tr><td colspan="2" style="margin-bottom:5px;border-bottom:1px dotted #000000;">&nbsp;</td></tr><tr><td colspan="2">&nbsp;</td></tr>
  192.  
  193. <?php
  194. break;
  195.  
  196. case 'select':
  197. ?>
  198. <tr>
  199. <td width="20%" rowspan="2" valign="middle"><strong><?php echo $value['name']; ?></strong></td>
  200. <td width="80%"><select style="width:240px;" name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>"><?php foreach ($value['options'] as $option) { ?><option<?php if ( get_settings( $value['id'] ) == $option) { echo ' selected="selected"'; } elseif ($option == $value['std']) { echo ' selected="selected"'; } ?>><?php echo $option; ?></option><?php } ?></select></td>
  201. </tr>
  202.  
  203. <tr>
  204. <td><small><?php echo $value['desc']; ?></small></td>
  205. </tr><tr><td colspan="2" style="margin-bottom:5px;border-bottom:1px dotted #000000;">&nbsp;</td></tr><tr><td colspan="2">&nbsp;</td></tr>
  206.  
  207. <?php
  208. break;
  209.  
  210. case "checkbox":
  211. ?>
  212. <tr>
  213. <td width="20%" rowspan="2" valign="middle"><strong><?php echo $value['name']; ?></strong></td>
  214. <td width="80%"><?php if(get_option($value['id'])){ $checked = "checked=\"checked\""; }else{ $checked = "";} ?>
  215. <input type="checkbox" name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>" value="true" <?php echo $checked; ?> />
  216. </td>
  217. </tr>
  218.  
  219. <tr>
  220. <td><small><?php echo $value['desc']; ?></small></td>
  221. </tr><tr><td colspan="2" style="margin-bottom:5px;border-bottom:1px dotted #000000;">&nbsp;</td></tr><tr><td colspan="2">&nbsp;</td></tr>
  222.  
  223. <?php break;
  224.  
  225. }
  226. }
  227. ?>
  228.  
  229. <p class="submit">
  230. <input name="save" type="submit" value="Save changes" />
  231. <input type="hidden" name="action" value="save" />
  232. </p>
  233. </form>
  234. <form method="post">
  235. <p class="submit">
  236. <input name="reset" type="submit" value="Reset" />
  237. <input type="hidden" name="action" value="reset" />
  238. </p>
  239. </form>
  240. <?php   echo '</div>'; ?>
  241.  
  242. <?php }
  243.  
  244. ?>
  245. <?php
  246.  
  247. $prefix = 'thestore_';
  248.  
  249. $meta_box = array(
  250.     'id' => 'my-meta-box',
  251.     'title' => 'Store Item Details',
  252.     'page' => 'the_store',
  253.     'context' => 'normal',
  254.     'priority' => 'high',
  255.     'fields' => array(
  256.         array(
  257.             'name' => 'Price',
  258.             'desc' => 'Enter product price here.',
  259.             'id' => $prefix . 'price',
  260.             'type' => 'text',
  261.             'std' => '10.00'
  262.         ),
  263.         array(
  264.             'name' => 'Shipping',
  265.             'desc' => 'Enter product shipping here.',
  266.             'id' => $prefix . 'shipping',
  267.             'type' => 'text',
  268.             'std' => '1.00'
  269.         ),
  270.         array(
  271.             'name' => 'Secondary Shipping',
  272.             'desc' => 'Enter price to ship a second item. This will be added to every item as the quantity increases',
  273.             'id' => $prefix . 'shipping2',
  274.             'type' => 'text',
  275.             'std' => '0.00'
  276.         ),
  277.         /*array(
  278.             'name' => 'Display Purchase Link?',
  279.             'id' => $prefix . 'radio',
  280.             'type' => 'radio',
  281.             'options' => array(
  282.                 array('name' => 'Yes', 'value' => 'Yes'),
  283.                 array('name' => 'No', 'value' => 'No'),
  284.             )
  285.         ),*/
  286.         array(
  287.             'name' => 'Display PayPal link?',
  288.             'id' => $prefix . 'display',
  289.             'type' => 'checkbox'
  290.         )
  291.     )
  292. );
  293.  
  294. add_action('admin_menu', 'mytheme_add_box');
  295.  
  296. // Add meta box
  297. function mytheme_add_box() {
  298.     global $meta_box;
  299.    
  300.     add_meta_box($meta_box['id'], $meta_box['title'], 'mytheme_show_box', $meta_box['page'], $meta_box['context'], $meta_box['priority']);
  301. }
  302.  
  303. // Callback function to show fields in meta box
  304. function mytheme_show_box() {
  305.     global $meta_box, $post;
  306.    
  307.     // Use nonce for verification
  308.     echo '<input type="hidden" name="mytheme_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
  309.    
  310.     echo '<table class="form-table">';
  311.  
  312.     foreach ($meta_box['fields'] as $field) {
  313.         // get current post meta data
  314.         $meta = get_post_meta($post->ID, $field['id'], true);
  315.        
  316.         echo '<tr>',
  317.                 '<th style="width:20%"><label for="', $field['id'], '">', $field['name'], '</label></th>',
  318.                 '<td>';
  319.         switch ($field['type']) {
  320.             case 'text':
  321.                 echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" size="30" style="width:97%" />',
  322.                     '<br />', $field['desc'];
  323.                 break;
  324.             case 'textarea':
  325.                 echo '<textarea name="', $field['id'], '" id="', $field['id'], '" cols="60" rows="4" style="width:97%">', $meta ? $meta : $field['std'], '</textarea>',
  326.                     '<br />', $field['desc'];
  327.                 break;
  328.             case 'select':
  329.                 echo '<select name="', $field['id'], '" id="', $field['id'], '">';
  330.                 foreach ($field['options'] as $option) {
  331.                     echo '<option', $meta == $option ? ' selected="selected"' : '', '>', $option, '</option>';
  332.                 }
  333.                 echo '</select>';
  334.                 break;
  335.             case 'radio':
  336.                 foreach ($field['options'] as $option) {
  337.                     echo '<input type="radio" name="', $field['id'], '" value="', $option['value'], '"', $meta == $option['value'] ? ' checked="checked"' : '', ' />', $option['name'];
  338.                 }
  339.                 break;
  340.             case 'checkbox':
  341.                 echo '<input type="checkbox" name="', $field['id'], '" id="', $field['id'], '"', $meta ? ' checked="checked"' : '', ' />';
  342.                 break;
  343.         }
  344.         echo    '<td>',
  345.             '</tr>';
  346.     }
  347.    
  348.     echo '</table>';
  349. }
  350.  
  351. add_action('save_post', 'mytheme_save_data');
  352.  
  353. // Save data from meta box
  354. function mytheme_save_data($post_id) {
  355.     global $meta_box;
  356.    
  357.     // verify nonce
  358.     if (!wp_verify_nonce($_POST['mytheme_meta_box_nonce'], basename(__FILE__))) {
  359.         return $post_id;
  360.     }
  361.  
  362.     // check autosave
  363.     if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
  364.         return $post_id;
  365.     }
  366.  
  367.     // check permissions
  368.     if ('page' == $_POST['post_type']) {
  369.         if (!current_user_can('edit_page', $post_id)) {
  370.             return $post_id;
  371.         }
  372.     } elseif (!current_user_can('edit_post', $post_id)) {
  373.         return $post_id;
  374.     }
  375.    
  376.     foreach ($meta_box['fields'] as $field) {
  377.         $old = get_post_meta($post_id, $field['id'], true);
  378.         $new = $_POST[$field['id']];
  379.        
  380.         if ($new && $new != $old) {
  381.             update_post_meta($post_id, $field['id'], $new);
  382.         } elseif ('' == $new && $old) {
  383.             delete_post_meta($post_id, $field['id'], $old);
  384.         }
  385.     }
  386. }
  387.  
  388.  
  389.  
  390. function add_some_stuff($content) {
  391.     global $shortname;
  392.     if (is_post_type('the_store')) {
  393.         $display = get_post_custom_values('thestore_display');
  394.         if($display[0]){
  395.             $item_name = the_title('', '', false);
  396.             $shopping_url = get_permalink();
  397.             $item_number = get_the_ID();
  398.             $amount = get_post_custom_values('thestore_price');
  399.             $amount = $amount[0];
  400.             $shipping = get_post_custom_values('thestore_shipping');
  401.             $shipping = $shipping[0];
  402.             $shipping2 = get_post_custom_values('thestore_shipping2');
  403.             $shipping2 = $shipping2[0];
  404.             $business = get_option($shortname.'_paypal');
  405.             $handling = get_post_custom_values($shortname.'_handling');
  406.             $handling = $handling[0];
  407.            
  408.             $paypal = <<<PAYPAL
  409.                     <form method="post" action="https://www.paypal.com/cgi-bin/webscr" target="paypal">
  410.                     <input type="hidden" name="cmd" value="_cart">
  411.                     <input type="hidden" name="business" value="$business">
  412.                     <input type="hidden" name="item_name" value="$item_name">
  413.                     <input type="hidden" name="shopping_url" value="$shopping_url">
  414.                     <input type="hidden" name="item_number" value="$item_number">
  415.                     <input type="hidden" name="amount" value="$amount">
  416.                     <input type="hidden" name="currency_code" value="USD">
  417.                     <input type="hidden" name="shipping" value="$shipping">
  418.                     <input type="hidden" name="shipping2" value="$shipping2">
  419.                     <input type="hidden" name="handling_cart" value="$handling">
  420.                     <input type="hidden" name="bn"  value="ButtonFactory.PayPal.001">
  421.                     <input type="image" name="add" src="http://www.powersellersunite.com/buttonfactory/sc-but-01.gif">
  422.                     </form>
  423. PAYPAL;
  424.            
  425.            
  426.            
  427.             return $content.$paypal;
  428.         }
  429.     }
  430. }
  431.  
  432. add_action('the_content', 'add_some_stuff');
  433.  
  434. ?>
Add Comment
Please, Sign In to add comment