whyisjake

Jake Spurlock

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