/adminpages/discountcodes.php

  1. <?php 
  2. //only admins can get this 
  3. if(!function_exists("current_user_can") || (!current_user_can("manage_options") && !current_user_can("pmpro_discountcodes"))) 
  4. die(__("You do not have permissions to perform this action.", 'paid-memberships-pro' )); 
  5.  
  6. //vars 
  7. global $wpdb, $pmpro_currency_symbol; 
  8.  
  9. if(isset($_REQUEST['edit'])) 
  10. $edit = intval($_REQUEST['edit']); 
  11. else 
  12. $edit = false; 
  13.  
  14. if(isset($_REQUEST['delete'])) 
  15. $delete = intval($_REQUEST['delete']); 
  16. else 
  17. $delete = false; 
  18.  
  19. if(isset($_REQUEST['saveid'])) 
  20. $saveid = intval($_POST['saveid']); 
  21. else 
  22. $saveid = false;  
  23.  
  24. if(isset($_REQUEST['s'])) 
  25. $s = sanitize_text_field($_REQUEST['s']); 
  26. else 
  27. $s = ""; 
  28.  
  29. if($saveid) 
  30. //get vars 
  31. //disallow/strip all non-alphanumeric characters except - 
  32. $code = preg_replace("/[^A-Za-z0-9\-]/", "", sanitize_text_field($_POST['code'])); 
  33. $starts_month = intval($_POST['starts_month']); 
  34. $starts_day = intval($_POST['starts_day']); 
  35. $starts_year = intval($_POST['starts_year']); 
  36. $expires_month = intval($_POST['expires_month']); 
  37. $expires_day = intval($_POST['expires_day']); 
  38. $expires_year = intval($_POST['expires_year']); 
  39. $uses = intval($_POST['uses']); 
  40.  
  41. //fix up dates  
  42. $starts = date_i18n("Y-m-d", strtotime($starts_month . "/" . $starts_day . "/" . $starts_year, current_time("timestamp"))); 
  43. $expires = date_i18n("Y-m-d", strtotime($expires_month . "/" . $expires_day . "/" . $expires_year, current_time("timestamp"))); 
  44.  
  45. //insert/update/replace discount code 
  46. $wpdb->replace( 
  47. $wpdb->pmpro_discount_codes,  
  48. array( 
  49. 'id'=>max($saveid, 0),  
  50. 'code' => $code,  
  51. 'starts' => $starts,  
  52. 'expires' => $expires,  
  53. 'uses' => $uses  
  54. ),  
  55. array( 
  56. '%d',  
  57. '%s',  
  58. '%s',  
  59. '%s',  
  60. '%d' 
  61. ); 
  62.  
  63. //check for errors and show appropriate message if inserted or updated 
  64. if(empty($wpdb->last_error)) { 
  65. if($saveid < 1) { 
  66. //insert 
  67. $pmpro_msg = __("Discount code added successfully.", 'paid-memberships-pro' ); 
  68. $pmpro_msgt = "success"; 
  69. $saved = true; 
  70. $edit = $wpdb->insert_id; 
  71. } else { 
  72. //updated 
  73. $pmpro_msg = __("Discount code updated successfully.", 'paid-memberships-pro' ); 
  74. $pmpro_msgt = "success"; 
  75. $saved = true; 
  76. $edit = $saveid; 
  77. } else { 
  78. if($saveid < 1) { 
  79. //error inserting 
  80. $pmpro_msg = __("Error adding discount code. That code may already be in use.", 'paid-memberships-pro' ) . $wpdb->last_error; 
  81. $pmpro_msgt = "error"; 
  82. } else { 
  83. //error updating 
  84. $pmpro_msg = __("Error updating discount code. That code may already be in use.", 'paid-memberships-pro' ); 
  85. $pmpro_msgt = "error"; 
  86. }  
  87.  
  88. //now add the membership level rows 
  89. if($saved && $edit > 0) 
  90. //get the submitted values 
  91. $all_levels_a = $_REQUEST['all_levels']; 
  92. if(!empty($_REQUEST['levels'])) 
  93. $levels_a = $_REQUEST['levels']; 
  94. else 
  95. $levels_a = array(); 
  96. $initial_payment_a = $_REQUEST['initial_payment']; 
  97.  
  98. if(!empty($_REQUEST['recurring'])) 
  99. $recurring_a = $_REQUEST['recurring'];  
  100. $billing_amount_a = $_REQUEST['billing_amount']; 
  101. $cycle_number_a = $_REQUEST['cycle_number']; 
  102. $cycle_period_a = $_REQUEST['cycle_period']; 
  103. $billing_limit_a = $_REQUEST['billing_limit']; 
  104.  
  105. if(!empty($_REQUEST['custom_trial'])) 
  106. $custom_trial_a = $_REQUEST['custom_trial']; 
  107. $trial_amount_a = $_REQUEST['trial_amount']; 
  108. $trial_limit_a = $_REQUEST['trial_limit']; 
  109.  
  110. if(!empty($_REQUEST['expiration'])) 
  111. $expiration_a = $_REQUEST['expiration']; 
  112. $expiration_number_a = $_REQUEST['expiration_number']; 
  113. $expiration_period_a = $_REQUEST['expiration_period']; 
  114.  
  115. //clear the old rows 
  116. $wpdb->delete($wpdb->pmpro_discount_codes_levels, array('code_id' => $edit), array('%d')); 
  117.  
  118. //add a row for each checked level 
  119. if(!empty($levels_a)) 
  120. foreach($levels_a as $level_id) 
  121. //get the values ready 
  122. $n = array_search($level_id, $all_levels_a); //this is the key location of this level's values 
  123. $initial_payment = sanitize_text_field($initial_payment_a[$n]); 
  124.  
  125. //is this recurring? 
  126. if(!empty($recurring_a)) 
  127. if(in_array($level_id, $recurring_a)) 
  128. $recurring = 1; 
  129. else 
  130. $recurring = 0; 
  131. else 
  132. $recurring = 0; 
  133.  
  134. if(!empty($recurring)) 
  135. $billing_amount = sanitize_text_field($billing_amount_a[$n]); 
  136. $cycle_number = intval($cycle_number_a[$n]); 
  137. $cycle_period = sanitize_text_field($cycle_period_a[$n]); 
  138. $billing_limit = intval($billing_limit_a[$n]); 
  139.  
  140. //custom trial 
  141. if(!empty($custom_trial_a)) 
  142. if(in_array($level_id, $custom_trial_a)) 
  143. $custom_trial = 1; 
  144. else 
  145. $custom_trial = 0; 
  146. else 
  147. $custom_trial = 0; 
  148.  
  149. if(!empty($custom_trial)) 
  150. $trial_amount = sanitize_text_field($trial_amount_a[$n]); 
  151. $trial_limit = intval($trial_limit_a[$n]); 
  152. else 
  153. $trial_amount = ''; 
  154. $trial_limit = ''; 
  155. else 
  156. $billing_amount = ''; 
  157. $cycle_number = ''; 
  158. $cycle_period = 'Month'; 
  159. $billing_limit = ''; 
  160. $custom_trial = 0; 
  161. $trial_amount = ''; 
  162. $trial_limit = ''; 
  163.  
  164. if(!empty($expiration_a)) 
  165. if(in_array($level_id, $expiration_a)) 
  166. $expiration = 1; 
  167. else 
  168. $expiration = 0; 
  169. else 
  170. $expiration = 0; 
  171.  
  172. if(!empty($expiration)) 
  173. $expiration_number = intval($expiration_number_a[$n]); 
  174. $expiration_period = sanitize_text_field($expiration_period_a[$n]); 
  175. else 
  176. $expiration_number = ''; 
  177. $expiration_period = 'Month'; 
  178.  
  179.  
  180.  
  181. //okay, do the insert 
  182. $wpdb->insert( 
  183. $wpdb->pmpro_discount_codes_levels,  
  184. array( 
  185. 'code_id' => $edit,  
  186. 'level_id' => $level_id,  
  187. 'initial_payment' => $initial_payment,  
  188. 'billing_amount' => $billing_amount,  
  189. 'cycle_number' => $cycle_number,  
  190. 'cycle_period' => $cycle_period,  
  191. 'billing_limit' => $billing_limit,  
  192. 'trial_amount' => $trial_amount,  
  193. 'trial_limit' => $trial_limit,  
  194. 'expiration_number' => $expiration_number,  
  195. 'expiration_period' => $expiration_period 
  196. ),  
  197. array( 
  198. '%d',  
  199. '%d',  
  200. '%f',  
  201. '%f',  
  202. '%d',  
  203. '%s',  
  204. '%d',  
  205. '%f',  
  206. '%d',  
  207. '%d',  
  208. '%s' 
  209. ); 
  210.  
  211. if(empty($wpdb->last_error)) 
  212. //okay 
  213. do_action("pmpro_save_discount_code_level", $edit, $level_id); 
  214. else 
  215. $level = pmpro_getLevel($level_id); 
  216. $level_errors[] = sprintf(__("Error saving values for the %s level.", 'paid-memberships-pro' ), $level->name); 
  217.  
  218. //errors? 
  219. if(!empty($level_errors)) 
  220. $pmpro_msg = __("There were errors updating the level values: ", 'paid-memberships-pro' ) . implode(" ", $level_errors); 
  221. $pmpro_msgt = "error"; 
  222. else 
  223. //all good. set edit = NULL so we go back to the overview page 
  224. $edit = NULL; 
  225.  
  226. do_action("pmpro_save_discount_code", $saveid); 
  227.  
  228. //are we deleting? 
  229. if(!empty($delete)) 
  230. //is this a code? 
  231. $code = $wpdb->get_var( $wpdb->prepare( "SELECT code FROM $wpdb->pmpro_discount_codes WHERE id = %d LIMIT 1", $delete ) ); 
  232. if(!empty($code)) 
  233. //action 
  234. do_action("pmpro_delete_discount_code", $delete); 
  235.  
  236. //delete the code levels 
  237. $r1 = $wpdb->delete($wpdb->pmpro_discount_codes_levels, array('code_id'=>$delete), array('%d')); 
  238.  
  239. if($r1 !== false) 
  240. //delete the code 
  241. $r2 = $wpdb->delete($wpdb->pmpro_discount_codes, array('id'=>$delete), array('%d')); 
  242.  
  243. if($r2 !== false) 
  244. $pmpro_msg = sprintf(__("Code %s deleted successfully.", 'paid-memberships-pro' ), $code); 
  245. $pmpro_msgt = "success"; 
  246. else 
  247. $pmpro_msg = __("Error deleting discount code. The code was only partially deleted. Please try again.", 'paid-memberships-pro' ); 
  248. $pmpro_msgt = "error"; 
  249. else 
  250. $pmpro_msg = __("Error deleting code. Please try again.", 'paid-memberships-pro' ); 
  251. $pmpro_msgt = "error"; 
  252. else 
  253. $pmpro_msg = __("Code not found.", 'paid-memberships-pro' ); 
  254. $pmpro_msgt = "error"; 
  255.  
  256. require_once(dirname(__FILE__) . "/admin_header.php"); 
  257. ?> 
  258.  
  259. <?php if($edit) { ?> 
  260.  
  261. <h2> 
  262. <?php 
  263. if($edit > 0) 
  264. echo __("Edit Discount Code", 'paid-memberships-pro' ); 
  265. else 
  266. echo __("Add New Discount Code", 'paid-memberships-pro' ); 
  267. ?> 
  268. </h2> 
  269.  
  270. <?php if(!empty($pmpro_msg)) { ?> 
  271. <div id="message" class="<?php if($pmpro_msgt == "success") echo "updated fade"; else echo "error"; ?>"><p><?php echo $pmpro_msg?></p></div> 
  272. <?php } ?> 
  273.  
  274. <div> 
  275. <?php 
  276. // get the code... 
  277. if($edit > 0) 
  278. $code = $wpdb->get_row( 
  279. $wpdb->prepare(" 
  280. SELECT *, UNIX_TIMESTAMP(starts) as starts, UNIX_TIMESTAMP(expires) as expires 
  281. FROM $wpdb->pmpro_discount_codes 
  282. WHERE id = %d LIMIT 1",  
  283. $edit ),  
  284. OBJECT 
  285. ); 
  286.  
  287. $uses = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->pmpro_discount_codes_uses WHERE code_id = %d", $code->id ) ); 
  288. $levels = $wpdb->get_results( $wpdb->prepare(" 
  289. SELECT l.id, l.name, cl.initial_payment, cl.billing_amount, cl.cycle_number, cl.cycle_period, cl.billing_limit, cl.trial_amount, cl.trial_limit 
  290. FROM $wpdb->pmpro_membership_levels l 
  291. LEFT JOIN $wpdb->pmpro_discount_codes_levels cl 
  292. ON l.id = cl.level_id 
  293. WHERE cl.code_id = %s",  
  294. $code->code 
  295. ) ); 
  296. $temp_id = $code->id; 
  297. elseif(!empty($copy) && $copy > 0) 
  298. $code = $wpdb->get_row( $wpdb->prepare(" 
  299. SELECT *, UNIX_TIMESTAMP(starts) as starts, UNIX_TIMESTAMP(expires) as expires 
  300. FROM $wpdb->pmpro_discount_codes 
  301. WHERE id = %d LIMIT 1",  
  302. $copy ),  
  303. OBJECT 
  304. ); 
  305. $temp_id = $level->id; 
  306. $level->id = NULL; 
  307.  
  308. // didn't find a discount code, let's add a new one... 
  309. if(empty($code->id)) $edit = -1; 
  310.  
  311. //defaults for new codes 
  312. if($edit == -1) 
  313. $code = new stdClass(); 
  314. $code->code = pmpro_getDiscountCode(); 
  315. ?> 
  316. <form action="" method="post"> 
  317. <input name="saveid" type="hidden" value="<?php echo $edit?>" /> 
  318. <table class="form-table"> 
  319. <tbody> 
  320. <tr> 
  321. <th scope="row" valign="top"><label><?php _e('ID', 'paid-memberships-pro' );?>:</label></th> 
  322. <td class="pmpro_lite"><?php if(!empty($code->id)) echo $code->id; else echo __("This will be generated when you save.", 'paid-memberships-pro' );?></td> 
  323. </tr> 
  324.  
  325. <tr> 
  326. <th scope="row" valign="top"><label for="code"><?php _e('Code', 'paid-memberships-pro' );?>:</label></th> 
  327. <td><input name="code" type="text" size="20" value="<?php echo str_replace("\"", """, stripslashes($code->code))?>" /></td> 
  328. </tr> 
  329.  
  330. <?php 
  331. //some vars for the dates 
  332. $current_day = date_i18n("j"); 
  333. if(!empty($code->starts)) 
  334. $selected_starts_day = date_i18n("j", $code->starts); 
  335. else 
  336. $selected_starts_day = $current_day; 
  337. if(!empty($code->expires)) 
  338. $selected_expires_day = date_i18n("j", $code->expires); 
  339. else 
  340. $selected_expires_day = $current_day; 
  341.  
  342. $current_month = date_i18n("M"); 
  343. if(!empty($code->starts)) 
  344. $selected_starts_month = date_i18n("m", $code->starts); 
  345. else 
  346. $selected_starts_month = date_i18n("m"); 
  347. if(!empty($code->expires)) 
  348. $selected_expires_month = date_i18n("m", $code->expires); 
  349. else 
  350. $selected_expires_month = date_i18n("m");  
  351.  
  352. $current_year = date_i18n("Y"); 
  353. if(!empty($code->starts)) 
  354. $selected_starts_year = date_i18n("Y", $code->starts); 
  355. else 
  356. $selected_starts_year = $current_year; 
  357. if(!empty($code->expires)) 
  358. $selected_expires_year = date_i18n("Y", $code->expires); 
  359. else 
  360. $selected_expires_year = (int)$current_year + 1; 
  361. ?> 
  362.  
  363. <tr> 
  364. <th scope="row" valign="top"><label for="starts"><?php _e('Start Date', 'paid-memberships-pro' );?>:</label></th> 
  365. <td> 
  366. <select name="starts_month"> 
  367. <?php 
  368. for($i = 1; $i < 13; $i++) 
  369. ?> 
  370. <option value="<?php echo $i?>" <?php if($i == $selected_starts_month) { ?>selected="selected"<?php } ?>><?php echo date_i18n("M", strtotime($i . "/1/" . $current_year, current_time("timestamp")))?></option> 
  371. <?php 
  372. ?> 
  373. </select> 
  374. <input name="starts_day" type="text" size="2" value="<?php echo $selected_starts_day?>" /> 
  375. <input name="starts_year" type="text" size="4" value="<?php echo $selected_starts_year?>" /> 
  376. </td> 
  377. </tr> 
  378.  
  379. <tr> 
  380. <th scope="row" valign="top"><label for="expires"><?php _e('Expiration Date', 'paid-memberships-pro' );?>:</label></th> 
  381. <td> 
  382. <select name="expires_month"> 
  383. <?php 
  384. for($i = 1; $i < 13; $i++) 
  385. ?> 
  386. <option value="<?php echo $i?>" <?php if($i == $selected_expires_month) { ?>selected="selected"<?php } ?>><?php echo date_i18n("M", strtotime($i . "/1/" . $current_year, current_time("timestamp")))?></option> 
  387. <?php 
  388. ?> 
  389. </select> 
  390. <input name="expires_day" type="text" size="2" value="<?php echo $selected_expires_day?>" /> 
  391. <input name="expires_year" type="text" size="4" value="<?php echo $selected_expires_year?>" /> 
  392. </td> 
  393. </tr> 
  394.  
  395. <tr> 
  396. <th scope="row" valign="top"><label for="uses"><?php _e('Uses', 'paid-memberships-pro' );?>:</label></th> 
  397. <td> 
  398. <input name="uses" type="text" size="10" value="<?php if(!empty($code->uses)) echo str_replace("\"", """, stripslashes($code->uses));?>" /> 
  399. <small class="pmpro_lite"><?php _e('Leave blank for unlimited uses.', 'paid-memberships-pro' );?></small> 
  400. </td> 
  401. </tr> 
  402.  
  403. </tbody> 
  404. </table> 
  405.  
  406. <?php do_action("pmpro_discount_code_after_settings", $edit); ?> 
  407.  
  408. <h3><?php _e('Which Levels Will This Code Apply To?', 'paid-memberships-pro' ); ?></h3> 
  409.  
  410. <div class="pmpro_discount_levels"> 
  411. <?php 
  412. $levels = $wpdb->get_results("SELECT * FROM $wpdb->pmpro_membership_levels"); 
  413. foreach($levels as $level) 
  414. //if this level is already managed for this discount code, use the code values 
  415. if($edit > 0) 
  416. $code_level = $wpdb->get_row( $wpdb->prepare(" 
  417. SELECT l.id, cl.*, l.name, l.description, l.allow_signups 
  418. FROM $wpdb->pmpro_discount_codes_levels cl 
  419. LEFT JOIN $wpdb->pmpro_membership_levels l 
  420. ON cl.level_id = l.id 
  421. WHERE cl.code_id = %d AND cl.level_id = %d LIMIT 1",  
  422. $edit,  
  423. $level->id ) 
  424. ); 
  425. if($code_level) 
  426. $level = $code_level; 
  427. $level->checked = true; 
  428. else 
  429. $level_checked = false; 
  430. else 
  431. $level_checked = false; 
  432. ?> 
  433. <div> 
  434. <input type="hidden" name="all_levels[]" value="<?php echo $level->id?>" /> 
  435. <input type="checkbox" id="levels_<?php echo $level->id;?>" name="levels[]" value="<?php echo $level->id?>" <?php if(!empty($level->checked)) { ?>checked="checked"<?php } ?> onclick="if(jQuery(this).is(':checked')) jQuery(this).next().next().show(); else jQuery(this).next().next().hide();" /> 
  436. <label for="levels_<?php echo $level->id;?>"><?php echo $level->name?></label> 
  437. <div class="pmpro_discount_levels_pricing level_<?php echo $level->id?>" <?php if(empty($level->checked)) { ?>style="display: none;"<?php } ?>> 
  438. <table class="form-table"> 
  439. <tbody> 
  440. <tr> 
  441. <th scope="row" valign="top"><label for="initial_payment"><?php _e('Initial Payment', 'paid-memberships-pro' );?>:</label></th> 
  442. <td> 
  443. <?php 
  444. if(pmpro_getCurrencyPosition() == "left") 
  445. echo $pmpro_currency_symbol; 
  446. ?> 
  447. <input name="initial_payment[]" type="text" size="20" value="<?php echo str_replace("\"", """, stripslashes($level->initial_payment))?>" /> 
  448. <?php 
  449. if(pmpro_getCurrencyPosition() == "right") 
  450. echo $pmpro_currency_symbol; 
  451. ?> 
  452. <small><?php _e('The initial amount collected at registration.', 'paid-memberships-pro' );?></small> 
  453. </td> 
  454. </tr> 
  455.  
  456. <tr> 
  457. <th scope="row" valign="top"><label><?php _e('Recurring Subscription', 'paid-memberships-pro' );?>:</label></th> 
  458. <td><input class="recurring_checkbox" id="recurring_<?php echo $level->id;?>" name="recurring[]" type="checkbox" value="<?php echo $level->id?>" <?php if(pmpro_isLevelRecurring($level)) { echo "checked='checked'"; } ?> onclick="if(jQuery(this).attr('checked')) { jQuery(this).parent().parent().siblings('.recurring_info').show(); if(!jQuery('#custom_trial_<?php echo $level->id?>').is(':checked')) jQuery(this).parent().parent().siblings('.trial_info').hide();} else jQuery(this).parent().parent().siblings('.recurring_info').hide();" /> <label for="recurring_<?php echo $level->id;?>"><?php _e('Check if this level has a recurring subscription payment.', 'paid-memberships-pro' );?></label></td> 
  459. </tr> 
  460.  
  461. <tr class="recurring_info" <?php if(!pmpro_isLevelRecurring($level)) {?>style="display: none;"<?php } ?>> 
  462. <th scope="row" valign="top"><label for="billing_amount"><?php _e('Billing Amount', 'paid-memberships-pro' );?>:</label></th> 
  463. <td> 
  464. <?php 
  465. if(pmpro_getCurrencyPosition() == "left") 
  466. echo $pmpro_currency_symbol; 
  467. ?> 
  468. <input name="billing_amount[]" type="text" size="20" value="<?php echo str_replace("\"", """, stripslashes($level->billing_amount))?>" /> 
  469. <?php 
  470. if(pmpro_getCurrencyPosition() == "right") 
  471. echo $pmpro_currency_symbol; 
  472. ?> 
  473. <small>per</small> 
  474. <input name="cycle_number[]" type="text" size="10" value="<?php echo str_replace("\"", """, stripslashes($level->cycle_number))?>" /> 
  475. <select name="cycle_period[]" onchange="updateCyclePeriod();"> 
  476. <?php 
  477. $cycles = array( __('Day(s)', 'paid-memberships-pro' ) => 'Day', __('Week(s)', 'paid-memberships-pro' ) => 'Week', __('Month(s)', 'paid-memberships-pro' ) => 'Month', __('Year(s)', 'paid-memberships-pro' ) => 'Year' ); 
  478. foreach ( $cycles as $name => $value ) { 
  479. echo "<option value='$value'"; 
  480. if ( $level->cycle_period == $value ) echo " selected='selected'"; 
  481. echo ">$name</option>"; 
  482. ?> 
  483. </select> 
  484. <br /><small><?php _e('The amount to be billed one cycle after the initial payment.', 'paid-memberships-pro' );?></small> 
  485. </td> 
  486. </tr> 
  487.  
  488. <tr class="recurring_info" <?php if(!pmpro_isLevelRecurring($level)) {?>style="display: none;"<?php } ?>> 
  489. <th scope="row" valign="top"><label for="billing_limit"><?php _e('Billing Cycle Limit', 'paid-memberships-pro' );?>:</label></th> 
  490. <td> 
  491. <input name="billing_limit[]" type="text" size="20" value="<?php echo $level->billing_limit?>" /> 
  492. <br /><small><?php _e('The <strong>total</strong> number of recurring billing cycles for this level, including the trial period (if applicable) but not including the initial payment. Set to zero if membership is indefinite.', 'paid-memberships-pro' );?></small> 
  493. </td> 
  494. </tr> 
  495.  
  496. <tr class="recurring_info" <?php if (!pmpro_isLevelRecurring($level)) echo "style='display:none;'";?>> 
  497. <th scope="row" valign="top"><label><?php _e('Custom Trial', 'paid-memberships-pro' );?>:</label></th> 
  498. <td><input id="custom_trial_<?php echo $level->id?>" id="custom_trial_<?php echo $level->id;?>" name="custom_trial[]" type="checkbox" value="<?php echo $level->id?>" <?php if ( pmpro_isLevelTrial($level) ) { echo "checked='checked'"; } ?> onclick="if(jQuery(this).attr('checked')) jQuery(this).parent().parent().siblings('.trial_info').show(); else jQuery(this).parent().parent().siblings('.trial_info').hide();" /> <label for="custom_trial_<?php echo $level->id;?>"><?php _e('Check to add a custom trial period.', 'paid-memberships-pro' );?></label></td> 
  499. </tr> 
  500.  
  501. <tr class="trial_info recurring_info" <?php if (!pmpro_isLevelTrial($level)) echo "style='display:none;'";?>> 
  502. <th scope="row" valign="top"><label for="trial_amount"><?php _e('Trial Billing Amount', 'paid-memberships-pro' );?>:</label></th> 
  503. <td> 
  504. <?php 
  505. if(pmpro_getCurrencyPosition() == "left") 
  506. echo $pmpro_currency_symbol; 
  507. ?> 
  508. <input name="trial_amount[]" type="text" size="20" value="<?php echo str_replace("\"", """, stripslashes($level->trial_amount))?>" /> 
  509. <?php 
  510. if(pmpro_getCurrencyPosition() == "right") 
  511. echo $pmpro_currency_symbol; 
  512. ?> 
  513. <small><?php _e('for the first', 'paid-memberships-pro' );?></small> 
  514. <input name="trial_limit[]" type="text" size="10" value="<?php echo str_replace("\"", """, stripslashes($level->trial_limit))?>" /> 
  515. <small><?php _e('subscription payments', 'paid-memberships-pro' );?>.</small> 
  516. </td> 
  517. </tr> 
  518.  
  519. <tr> 
  520. <th scope="row" valign="top"><label><?php _e('Membership Expiration', 'paid-memberships-pro' );?>:</label></th> 
  521. <td><input id="expiration_<?php echo $level->id;?>" name="expiration[]" type="checkbox" value="<?php echo $level->id?>" <?php if(pmpro_isLevelExpiring($level)) { echo "checked='checked'"; } ?> onclick="if(jQuery(this).is(':checked')) { jQuery(this).parent().parent().siblings('.expiration_info').show(); } else { jQuery(this).parent().parent().siblings('.expiration_info').hide();}" /> <label for="expiration_<?php echo $level->id;?>"><?php _e('Check this to set when membership access expires.', 'paid-memberships-pro' );?></label></td> 
  522. </tr> 
  523.  
  524. <tr class="expiration_info" <?php if(!pmpro_isLevelExpiring($level)) {?>style="display: none;"<?php } ?>> 
  525. <th scope="row" valign="top"><label for="billing_amount"><?php _e('Expires In', 'paid-memberships-pro' );?>:</label></th> 
  526. <td> 
  527. <input id="expiration_number" name="expiration_number[]" type="text" size="10" value="<?php echo str_replace("\"", """, stripslashes($level->expiration_number))?>" /> 
  528. <select id="expiration_period" name="expiration_period[]"> 
  529. <?php 
  530. $cycles = array( __('Day(s)', 'paid-memberships-pro' ) => 'Day', __('Week(s)', 'paid-memberships-pro' ) => 'Week', __('Month(s)', 'paid-memberships-pro' ) => 'Month', __('Year(s)', 'paid-memberships-pro' ) => 'Year' ); 
  531. foreach ( $cycles as $name => $value ) { 
  532. echo "<option value='$value'"; 
  533. if ( $level->expiration_period == $value ) echo " selected='selected'"; 
  534. echo ">$name</option>"; 
  535. ?> 
  536. </select> 
  537. <br /><small><?php _e('Set the duration of membership access. Note that the any future payments (recurring subscription, if any) will be cancelled when the membership expires.', 'paid-memberships-pro' );?></small> 
  538. </td> 
  539. </tr> 
  540. </tbody> 
  541. </table> 
  542.  
  543. <?php do_action("pmpro_discount_code_after_level_settings", $edit, $level); ?> 
  544.  
  545. </div> 
  546. </div> 
  547. <script> 
  548.  
  549. </script> 
  550. <?php 
  551. ?> 
  552. </div> 
  553.  
  554. <p class="submit topborder"> 
  555. <input name="save" type="submit" class="button button-primary" value="Save Code" /> 
  556. <input name="cancel" type="button" class="button button-secondary" value="Cancel" onclick="location.href='<?php echo get_admin_url(NULL, '/admin.php?page=pmpro-discountcodes')?>';" /> 
  557. </p> 
  558. </form> 
  559. </div> 
  560.  
  561. <?php } else { ?> 
  562.  
  563. <h2> 
  564. <?php _e('Memberships Discount Codes', 'paid-memberships-pro' );?> 
  565. <a href="admin.php?page=pmpro-discountcodes&edit=-1" class="add-new-h2"><?php _e('Add New Discount Code', 'paid-memberships-pro' );?></a> 
  566. </h2> 
  567.  
  568. <?php if(!empty($pmpro_msg)) { ?> 
  569. <div id="message" class="<?php if($pmpro_msgt == "success") echo "updated fade"; else echo "error"; ?>"><p><?php echo $pmpro_msg?></p></div> 
  570. <?php } ?> 
  571.  
  572. <form id="posts-filter" method="get" action=""> 
  573. <p class="search-box"> 
  574. <label class="screen-reader-text" for="post-search-input"><?php _e('Search Discount Codes', 'paid-memberships-pro' );?>:</label> 
  575. <input type="hidden" name="page" value="pmpro-discountcodes" /> 
  576. <input id="post-search-input" type="text" value="<?php if(!empty($s)) echo $s;?>" name="s" size="30" /> 
  577. <input class="button" type="submit" value="<?php _e('Search', 'paid-memberships-pro' );?>" id="search-submit "/> 
  578. </p> 
  579. </form> 
  580.  
  581. <br class="clear" /> 
  582. <?php 
  583. $sqlQuery = "SELECT *, UNIX_TIMESTAMP(starts) as starts, UNIX_TIMESTAMP(expires) as expires FROM $wpdb->pmpro_discount_codes "; 
  584. if(!empty($s)) 
  585. $sqlQuery .= "WHERE code LIKE '%$s%' "; 
  586. $sqlQuery .= "ORDER BY id ASC"; 
  587.  
  588. $codes = $wpdb->get_results($sqlQuery, OBJECT); 
  589. ?> 
  590. <table class="widefat"> 
  591. <thead> 
  592. <tr> 
  593. <th><?php _e('ID', 'paid-memberships-pro' );?></th> 
  594. <th><?php _e('Code', 'paid-memberships-pro' );?></th> 
  595. <th><?php _e('Starts', 'paid-memberships-pro' );?></th> 
  596. <th><?php _e('Expires', 'paid-memberships-pro' );?></th> 
  597. <th><?php _e('Uses', 'paid-memberships-pro' );?></th> 
  598. <th><?php _e('Levels', 'paid-memberships-pro' );?></th> 
  599. <?php do_action("pmpro_discountcodes_extra_cols_header", $codes);?> 
  600. <th></th> 
  601. <th></th> 
  602. </tr> 
  603. </thead> 
  604. <tbody> 
  605. <?php 
  606. if(!$codes) 
  607. ?> 
  608. <tr><td colspan="7" class="pmpro_pad20"> 
  609. <p><?php _e('Discount codes allow you to offer your memberships at discounted prices to select customers.', 'paid-memberships-pro' );?> <a href="admin.php?page=pmpro-discountcodes&edit=-1"><?php _e('Create your first discount code now', 'paid-memberships-pro' );?></a>.</p> 
  610. </td></tr> 
  611. <?php 
  612. else 
  613. $count = 0; 
  614. foreach($codes as $code) 
  615. ?> 
  616. <tr<?php if($count++ % 2 == 1) { ?> class="alternate"<?php } ?>> 
  617. <td><?php echo $code->id?></td> 
  618. <td> 
  619. <a href="?page=pmpro-discountcodes&edit=<?php echo $code->id?>"><?php echo $code->code?></a> 
  620. </td> 
  621. <td> 
  622. <?php echo date_i18n(get_option('date_format'), $code->starts)?> 
  623. </td> 
  624. <td> 
  625. <?php echo date_i18n(get_option('date_format'), $code->expires)?> 
  626. </td>  
  627. <td> 
  628. <?php 
  629. $uses = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->pmpro_discount_codes_uses WHERE code_id = %d", $code->id ) ); 
  630. if($code->uses > 0) 
  631. echo "<strong>" . (int)$uses . "</strong>/" . $code->uses; 
  632. else 
  633. echo "<strong>" . (int)$uses . "</strong>/unlimited"; 
  634. ?> 
  635. </td> 
  636. <td> 
  637. <?php 
  638. $sqlQuery = $wpdb->prepare(" 
  639. SELECT l.id, l.name 
  640. FROM $wpdb->pmpro_membership_levels l 
  641. LEFT JOIN $wpdb->pmpro_discount_codes_levels cl 
  642. ON l.id = cl.level_id 
  643. WHERE cl.code_id = %d",  
  644. $code->id 
  645. ); 
  646. $levels = $wpdb->get_results($sqlQuery); 
  647.  
  648. $level_names = array(); 
  649. foreach($levels as $level) 
  650. $level_names[] = "<a target=\"_blank\" href=\"" . pmpro_url("checkout", "?level=" . $level->id . "&discount_code=" . $code->code) . "\">" . $level->name . "</a>"; 
  651. if($level_names) 
  652. echo implode(", ", $level_names); 
  653. else 
  654. echo "None"; 
  655. ?> 
  656. </td> 
  657. <?php do_action("pmpro_discountcodes_extra_cols_body", $code);?> 
  658. <td> 
  659. <a href="?page=pmpro-discountcodes&edit=<?php echo $code->id?>"><?php _e('edit', 'paid-memberships-pro' );?></a> 
  660. </td> 
  661. <td> 
  662. <a href="javascript:askfirst('<?php echo str_replace("'", "\'", sprintf(__('Are you sure you want to delete the %s discount code? The subscriptions for existing users will not change, but new users will not be able to use this code anymore.', 'paid-memberships-pro' ), $code->code));?>', '?page=pmpro-discountcodes&delete=<?php echo $code->id?>'); void(0);"><?php _e('delete', 'paid-memberships-pro' );?></a> 
  663. </td> 
  664. </tr> 
  665. <?php 
  666. ?> 
  667. </tbody> 
  668. </table> 
  669.  
  670. <?php } ?> 
  671.  
  672. <?php 
  673. require_once(dirname(__FILE__) . "/admin_footer.php"); 
  674. ?> 
.