/app_old/membershipincludes/classes/class.subscription.php

  1. <?php 
  2. if(!class_exists('M_Subscription')) { 
  3.  
  4. class M_Subscription { 
  5.  
  6. var $id = false; 
  7.  
  8. var $db; 
  9. var $tables = array('membership_levels', 'membership_rules', 'subscriptions', 'subscriptions_levels', 'membership_relationships', 'subscriptionmeta'); 
  10.  
  11. var $membership_levels; 
  12. var $membership_rules; 
  13. var $subscriptions; 
  14. var $subscriptions_levels; 
  15. var $membership_relationships; 
  16. var $subscriptionmeta; 
  17.  
  18. // if the data needs reloaded, or hasn't been loaded yet 
  19. var $dirty = true; 
  20.  
  21. var $subscription; 
  22. var $levels = array(); 
  23.  
  24. var $levelorder = array(); 
  25.  
  26. function __construct( $id = false ) { 
  27.  
  28. global $wpdb; 
  29.  
  30. $this->db =& $wpdb; 
  31.  
  32. foreach($this->tables as $table) { 
  33. $this->$table = membership_db_prefix($this->db, $table); 
  34.  
  35. $this->id = $id; 
  36.  
  37.  
  38. function M_Subscription( $id = false ) { 
  39. $this->__construct( $id ); 
  40.  
  41. // Fields 
  42.  
  43. function sub_id() { 
  44.  
  45. if(empty($this->subscription)) { 
  46. $sub = $this->get(); 
  47.  
  48. if($sub) { 
  49. return $sub->id; 
  50. } else { 
  51. return false; 
  52. } else { 
  53. return $this->subscription->id; 
  54.  
  55.  
  56. function sub_name() { 
  57.  
  58. if(empty($this->subscription)) { 
  59. $sub = $this->get(); 
  60.  
  61. if($sub) { 
  62. return stripslashes($sub->sub_name); 
  63. } else { 
  64. return false; 
  65. } else { 
  66. return stripslashes($this->subscription->sub_name); 
  67.  
  68.  
  69. function sub_description() { 
  70.  
  71. if(empty($this->subscription)) { 
  72. $sub = $this->get(); 
  73.  
  74. if($sub) { 
  75. return stripslashes($sub->sub_description); 
  76. } else { 
  77. return false; 
  78. } else { 
  79. return stripslashes($this->subscription->sub_description); 
  80.  
  81.  
  82. function sub_pricetext() { 
  83.  
  84. if(empty($this->subscription)) { 
  85. $sub = $this->get(); 
  86.  
  87. if($sub) { 
  88. return stripslashes($sub->sub_pricetext); 
  89. } else { 
  90. return false; 
  91. } else { 
  92. return stripslashes($this->subscription->sub_pricetext); 
  93.  
  94.  
  95. function get_pricingarray() { 
  96.  
  97. $levels = $this->get_levels(); 
  98.  
  99. $prices = array(); 
  100.  
  101. foreach( (array) $levels as $key => $level ) { 
  102.  
  103. if($level->sub_type == 'indefinite') { 
  104. // This will be the last item in any list 
  105. $prices[] = array( 'period' => $level->level_period, 'amount' => $level->level_price, 'type' => $level->sub_type, 'unit' => $level->level_period_unit); 
  106. break; 
  107. } elseif($level->sub_type == 'serial') { 
  108. // This will be the last item in any list 
  109. $prices[] = array( 'period' => $level->level_period, 'amount' => $level->level_price, 'type' => $level->sub_type, 'unit' => $level->level_period_unit); 
  110. break; 
  111. } else { 
  112. $prices[] = array( 'period' => $level->level_period, 'amount' => $level->level_price, 'type' => $level->sub_type, 'unit' => $level->level_period_unit); 
  113.  
  114. if(!empty($prices)) { 
  115. return $prices; 
  116. } else { 
  117. return false; 
  118.  
  119.  
  120. // Gets 
  121.  
  122. function get_next_level($level_id, $order_id) { 
  123. // returns the next level - if there is one 
  124. $onkey = false; 
  125.  
  126. if(empty($this->levels)) { 
  127. $this->levels = $this->get_levels(); 
  128.  
  129. if(!empty($this->levels)) { 
  130. $onkey = false; 
  131. foreach($this->levels as $key => $level) { 
  132. if($level->level_order == $order_id) { 
  133. // This is the order we are at - check the level_id 
  134. if($level->level_id == $level_id) { 
  135. // sweet - nobody has been messing around with the subscription 
  136. $onkey = $key; 
  137. } else { 
  138. // We're not on the right level, but this is location we should be at 
  139. // return the key for this level and hope for the best I guess 
  140. $onkey = $key; 
  141. break; 
  142.  
  143. if($onkey !== false) { 
  144. // we have a key for our current position, check it's mode / period and pos next level 
  145. switch($this->levels[$onkey]->sub_type) { 
  146. case 'finite': // we attempt to move to the next level 
  147. if(isset($this->levels[(int) $onkey + 1])) { 
  148. return $this->levels[(int) $onkey + 1]; 
  149. } else { 
  150. return false; 
  151. case 'indefinite': // we stay at our current level 
  152. return $this->levels[$onkey]; 
  153. case 'serial': // we renew at our current level 
  154. return $this->levels[$onkey]; 
  155.  
  156.  
  157. } else { 
  158. return false; 
  159.  
  160. } else { 
  161. return false; 
  162.  
  163.  
  164. function get() { 
  165.  
  166. if($this->dirty) { 
  167. $sql = $this->db->prepare( "SELECT * FROM {$this->subscriptions} WHERE id = %d", $this->id); 
  168.  
  169. $this->subscription = $this->db->get_row($sql); 
  170.  
  171. $this->dirty = false; 
  172.  
  173. return $this->subscription; 
  174.  
  175.  
  176. function get_levels() { 
  177.  
  178. $sql = $this->db->prepare( "SELECT * FROM {$this->subscriptions_levels} sl INNER JOIN {$this->membership_levels} l on sl.level_id = l.id WHERE sub_id = %d ORDER BY level_order ASC", $this->id ); 
  179.  
  180. $this->levels = $this->db->get_results( $sql ); 
  181.  
  182. return $this->levels; 
  183.  
  184.  
  185. function get_level_at($level_id, $level_order) { 
  186. $sql = $this->db->prepare( "SELECT * FROM {$this->subscriptions_levels} sl INNER JOIN {$this->membership_levels} l on sl.level_id = l.id WHERE sub_id = %d AND level_id = %d AND level_order = %d ORDER BY level_order ASC", $this->id, $level_id, $level_order ); 
  187.  
  188. $this->levels = $this->db->get_row( $sql ); 
  189.  
  190. return $this->levels; 
  191.  
  192. function get_level_at_position($level_order) { 
  193. $sql = $this->db->prepare( "SELECT * FROM {$this->subscriptions_levels} sl INNER JOIN {$this->membership_levels} l on sl.level_id = l.id WHERE sub_id = %d AND level_order = %d ORDER BY level_order ASC", $this->id, $level_order ); 
  194.  
  195. $this->levels = $this->db->get_row( $sql ); 
  196.  
  197. return $this->levels; 
  198.  
  199. function toggleactivation( $force = false ) { 
  200.  
  201. if(!apply_filters( 'pre_membership_toggleactivate_subscription', true, $this->id )) { 
  202. return false; 
  203.  
  204. $sql = $this->db->prepare( "UPDATE {$this->subscriptions} SET sub_active = NOT sub_active WHERE id = %d", $this->id); 
  205.  
  206. $this->dirty = true; 
  207.  
  208. $result = $this->db->query($sql); 
  209.  
  210. do_action( 'membership_toggleactivate_subscription', $this->id, $result ); 
  211.  
  212. return $result; 
  213.  
  214.  
  215. function togglepublic( $force = false ) { 
  216.  
  217. if(!apply_filters( 'pre_membership_togglepublic_subscription', true, $this->id )) { 
  218. return false; 
  219.  
  220. $sql = $this->db->prepare( "UPDATE {$this->subscriptions} SET sub_public = NOT sub_public WHERE id = %d", $this->id); 
  221.  
  222. $this->dirty = true; 
  223.  
  224. $result = $this->db->query($sql); 
  225.  
  226. do_action( 'membership_togglepublic_subscription', $this->id, $result ); 
  227.  
  228. return $result; 
  229.  
  230.  
  231.  
  232. function delete( $force = false ) { 
  233.  
  234. if(!apply_filters( 'pre_membership_delete_subscription', true, $this->id )) { 
  235. return false; 
  236.  
  237.  
  238. $sql = $this->db->prepare( "DELETE FROM {$this->subscriptions} WHERE id = %d", $this->id); 
  239.  
  240. $sql2 = $this->db->prepare( "DELETE FROM {$this->subscriptions_levels} WHERE sub_id = %d", $this->id); 
  241.  
  242. if($this->db->query($sql)) { 
  243.  
  244. $this->db->query($sql2); 
  245.  
  246. $this->dirty = true; 
  247.  
  248. do_action( 'membership_delete_subscription', $this->id ); 
  249.  
  250. return true; 
  251.  
  252.  
  253. function add() { 
  254.  
  255. $this->dirty = true; 
  256.  
  257. if($this->id > 0) { 
  258. $this->update(); 
  259. } else { 
  260.  
  261. $return = $this->db->insert($this->subscriptions, array('sub_name' => $_POST['sub_name'], 'sub_description' => $_POST['sub_description'], 'sub_pricetext' => $_POST['sub_pricetext'])); 
  262. $this->id = $this->db->insert_id; 
  263.  
  264. if(!empty($_POST['level-order'])) { 
  265.  
  266. $levels = explode(', ', $_POST['level-order']); 
  267. $count = 1; 
  268. foreach( (array) $levels as $level ) { 
  269. if(!empty($level)) { 
  270. // Check if the rule has any information for it. 
  271. if(isset($_POST['levelmode'][$level])) { 
  272. $levelmode = esc_attr($_POST['levelmode'][$level]); 
  273. } else { 
  274. continue; 
  275.  
  276. if(isset($_POST['levelperiod'][$level])) { 
  277. $levelperiod = esc_attr($_POST['levelperiod'][$level]); 
  278. } else { 
  279. $levelperiod = ''; 
  280.  
  281. if(isset($_POST['levelperiodunit'][$level])) { 
  282. $levelperiodunit = esc_attr($_POST['levelperiodunit'][$level]); 
  283. } else { 
  284. $levelperiodunit = ''; 
  285.  
  286. if(isset($_POST['levelprice'][$level])) { 
  287. $levelprice = floatval(esc_attr($_POST['levelprice'][$level])); 
  288. } else { 
  289. $levelprice = ''; 
  290.  
  291. if(isset($_POST['levelcurrency'][$level])) { 
  292. $levelcurrency = esc_attr($_POST['levelcurrency'][$level]); 
  293. } else { 
  294. $levelcurrency = ''; 
  295.  
  296.  
  297.  
  298. // Calculate the level id 
  299. $lev = explode('-', $level); 
  300. if($lev[0] == 'level') { 
  301. $level_id = (int) $lev[1]; 
  302. // write it to the database 
  303. $this->db->insert($this->subscriptions_levels, array( "sub_id" => $this->id,  
  304. "level_period" => $levelperiod,  
  305. "sub_type" => $levelmode,  
  306. "level_price" => $levelprice,  
  307. "level_currency" => $levelcurrency,  
  308. "level_order" => $count++,  
  309. "level_id" => $level_id,  
  310. "level_period_unit" => $levelperiodunit 
  311. )); 
  312.  
  313.  
  314.  
  315.  
  316.  
  317. do_action('membership_subscription_add', $this->id); 
  318.  
  319. return $this->id; // for now 
  320.  
  321.  
  322.  
  323. function update() { 
  324.  
  325. $this->dirty = true; 
  326.  
  327. if($this->id < 0) { 
  328. $this->add(); 
  329. } else { 
  330.  
  331. $return = $this->db->update($this->subscriptions, array('sub_name' => $_POST['sub_name'], 'sub_description' => $_POST['sub_description'], 'sub_pricetext' => $_POST['sub_pricetext']), array('id' => $this->id)); 
  332.  
  333. // Remove the existing rules for this subscription level 
  334. $this->db->query( $this->db->prepare( "DELETE FROM {$this->subscriptions_levels} WHERE sub_id = %d", $this->id ) ); 
  335.  
  336. if(!empty($_POST['level-order'])) { 
  337.  
  338. $levels = explode(', ', $_POST['level-order']); 
  339. $count = 1; 
  340. foreach( (array) $levels as $level ) { 
  341. if(!empty($level)) { 
  342. // Check if the rule has any information for it. 
  343. if(isset($_POST['levelmode'][$level])) { 
  344. $levelmode = esc_attr($_POST['levelmode'][$level]); 
  345. } else { 
  346. continue; 
  347.  
  348. if(isset($_POST['levelperiod'][$level])) { 
  349. $levelperiod = esc_attr($_POST['levelperiod'][$level]); 
  350. } else { 
  351. $levelperiod = ''; 
  352.  
  353. if(isset($_POST['levelperiodunit'][$level])) { 
  354. $levelperiodunit = esc_attr($_POST['levelperiodunit'][$level]); 
  355. } else { 
  356. $levelperiodunit = ''; 
  357.  
  358. if(isset($_POST['levelprice'][$level])) { 
  359. $levelprice = floatval(esc_attr($_POST['levelprice'][$level])); 
  360. } else { 
  361. $levelprice = ''; 
  362.  
  363. if(isset($_POST['levelcurrency'][$level])) { 
  364. $levelcurrency = esc_attr($_POST['levelcurrency'][$level]); 
  365. } else { 
  366. $levelcurrency = ''; 
  367.  
  368. // Calculate the level id 
  369. $lev = explode('-', $level); 
  370. if($lev[0] == 'level') { 
  371. $level_id = (int) $lev[1]; 
  372. // write it to the database 
  373. $this->db->insert($this->subscriptions_levels, array( "sub_id" => $this->id,  
  374. "level_period" => $levelperiod,  
  375. "sub_type" => $levelmode,  
  376. "level_price" => $levelprice,  
  377. "level_currency" => $levelcurrency,  
  378. "level_order" => $count++,  
  379. "level_id" => $level_id,  
  380. "level_period_unit" => $levelperiodunit 
  381. )); 
  382.  
  383.  
  384.  
  385.  
  386.  
  387. do_action('membership_subscription_update', $this->id); 
  388.  
  389. return true; // for now 
  390.  
  391.  
  392.  
  393. // For display 
  394.  
  395. function sub_template() { 
  396.  
  397. global $M_options; 
  398.  
  399. ?> 
  400. <li class='sortable-levels' id="%templateid%" > 
  401. <div class='joiningline'> </div> 
  402. <div class="sub-operation" style="display: block;"> 
  403. <h2 class="sidebar-name">%startingpoint%<span><a href='#remove' class='removelink' title='<?php _e("Remove this level from the subscription.", 'membership'); ?>'><?php _e('Remove', 'membership'); ?></a></span></h2> 
  404. <div class="inner-operation"> 
  405. <div class='levelfields' style='float: left;'> 
  406. <label for='levelmode[%level%]'><?php _e('Mode : ', 'membership'); ?></label> 
  407. <select name='levelmode[%level%]'> 
  408. <!-- <option value='trial'>Trial</option> --> 
  409. <option value='finite'>Finite</option> 
  410. <option value='indefinite'>Indefinite</option> 
  411. <option value='serial'>Serial</option> 
  412. <!-- <option value='sequential'>Sequential</option> --> 
  413. </select> 
  414. <label for='levelperiod[%level%]'><?php _e('Period : ', 'membership'); ?></label> 
  415. <select name='levelperiod[%level%]'> 
  416. <?php 
  417. for($n = 1; $n <= 365; $n++) { 
  418. ?> 
  419. <option value='<?php echo $n; ?>'><?php echo $n; ?></option> 
  420. <?php 
  421. ?> 
  422. </select>   
  423. <select name="levelperiodunit[%level%]"> 
  424. <option value='d'><?php _e('day(s)', 'membership'); ?></option> 
  425. <option value='w'><?php _e('week(s)', 'membership'); ?></option> 
  426. <option value='m'><?php _e('month(s)', 'membership'); ?></option> 
  427. <option value='y'><?php _e('year(s)', 'membership'); ?></option> 
  428. </select> 
  429.  
  430. <label for='levelprice[%level%]'><?php _e('Price : ', 'membership'); ?></label> 
  431. <input type='text' name='levelprice[%level%]' value='' class='narrow' /> 
  432. <?php /** ?> 
  433. <select name='levelprice[%level%]'> 
  434. <option value=''></option> 
  435. <?php 
  436. for($n = 1; $n <= (int) MEMBERSHIP_MAX_CHARGE; $n++) { 
  437. ?> 
  438. <option value='<?php echo $n; ?>'><?php echo $n; ?></option> 
  439. <?php 
  440. } 
  441. ?> 
  442. </select><?php */ ?>  
  443. <?php 
  444. if(!empty($M_options['paymentcurrency'])) { 
  445. echo esc_html($M_options['paymentcurrency']); 
  446. } else { 
  447. $M_options['paymentcurrency'] = 'USD'; 
  448. echo esc_html($M_options['paymentcurrency']); 
  449. ?> 
  450. </div> 
  451. <div class='levelinformation' style='float: right;'> 
  452. <p class='description'> 
  453. <strong><?php _e('Mode details', 'membership'); ?></strong><br/> 
  454. <?php _e('<strong>Finite</strong> - user remains at this level for a set period of time before ending', 'membership');?><br/><br/> 
  455. <?php _e('<strong>Indefinite</strong> - user remains at this level for ever.', 'membership');?><br/><br/> 
  456. <?php _e('<strong>Serial</strong> - user remains at this level for a set period of time and is then renewed at the same level', 'membership');?><br/><br/> 
  457. <?php _e('<strong>Note:</strong> - depending on the payment gateway used, changing the price will not alter subscriptions charged to existing members.', 'membership');?> 
  458. </p> 
  459. </div> 
  460. </div> 
  461. </div> 
  462. </li> 
  463. <?php 
  464.  
  465. function sub_details() { 
  466.  
  467. global $M_options; 
  468.  
  469. $count = 1; 
  470.  
  471. $this->levels = $this->get_levels(); 
  472.  
  473. $afterserial = false; 
  474.  
  475. if(!empty($this->levels)) { 
  476. $count = 1; 
  477. foreach($this->levels as $key => $level) { 
  478.  
  479. $levelid = 'level-' . $level->level_id . '-' . $count++; 
  480. $this->levelorder[] = $levelid; 
  481.  
  482. ?> 
  483. <li class='sortable-levels <?php echo ($afterserial) ? 'afterserial' : ''; ?>' id="<?php echo $levelid; ?>" > 
  484. <div class='joiningline'> </div> 
  485. <div class="sub-operation" style="display: block;"> 
  486. <h2 class="sidebar-name"><?php echo esc_html($level->level_title); ?><span><a href='#remove' class='removelink' title='<?php _e("Remove this level from the subscription.", 'membership'); ?>'><?php _e('Remove', 'membership'); ?></a></span></h2> 
  487. <div class="inner-operation"> 
  488. <div class='levelfields' style='float: left;'> 
  489. <label for='levelmode[<?php echo $levelid; ?>]'><?php _e('Mode : ', 'membership'); ?></label> 
  490. <select name='levelmode[<?php echo $levelid; ?>]' class='sublevelmode'> 
  491. <!-- <option value='trial' <?php if($level->sub_type == 'trial') echo "selected='selected'"; ?>><?php _e('Trial', 'membership'); ?></option> --> 
  492. <option value='finite' <?php if($level->sub_type == 'finite') echo "selected='selected'"; ?>><?php _e('Finite', 'membership'); ?></option> 
  493. <option value='indefinite' <?php if($level->sub_type == 'indefinite') echo "selected='selected'"; ?>><?php _e('Indefinite', 'membership'); ?></option> 
  494. <option value='serial' <?php if($level->sub_type == 'serial') echo "selected='selected'"; ?>><?php _e('Serial', 'membership'); ?></option> 
  495. <!-- <option value='sequential' <?php if($level->sub_type == 'sequential') echo "selected='selected'"; ?>><?php _e('Sequential', 'membership'); ?></option> --> 
  496. </select> 
  497. <label for='levelperiod[<?php echo $levelid; ?>]'><?php _e('Period : ', 'membership'); ?></label> 
  498. <select name='levelperiod[<?php echo $levelid; ?>]' class='sublevelperiod'> 
  499. <?php 
  500. for($n = 1; $n <= 365; $n++) { 
  501. ?> 
  502. <option value='<?php echo $n; ?>' <?php if($level->level_period == $n) echo "selected='selected'"; ?>><?php echo $n; ?></option> 
  503. <?php 
  504. ?> 
  505. </select>  
  506. <select name="levelperiodunit[<?php echo $levelid; ?>]" class='sublevelperiodunit'> 
  507. <option value='d' <?php if($level->level_period_unit == 'd') echo "selected='selected'"; ?>><?php _e('day(s)', 'membership'); ?></option> 
  508. <option value='w' <?php if($level->level_period_unit == 'w') echo "selected='selected'"; ?>><?php _e('week(s)', 'membership'); ?></option> 
  509. <option value='m' <?php if($level->level_period_unit == 'm') echo "selected='selected'"; ?>><?php _e('month(s)', 'membership'); ?></option> 
  510. <option value='y' <?php if($level->level_period_unit == 'y') echo "selected='selected'"; ?>><?php _e('year(s)', 'membership'); ?></option> 
  511. </select> 
  512.  
  513. <label for='levelprice[<?php echo $levelid; ?>]'><?php _e('Price : ', 'membership'); ?></label> 
  514. <input type='text' name='levelprice[<?php echo $levelid; ?>]' value='<?php echo number_format($level->level_price, 2, '.', ''); ?>' class='narrow sublevelprice' /> 
  515. <?php /** ?> 
  516. <select name='levelprice[<?php echo $levelid; ?>]'> 
  517. <option value=''></option> 
  518. <?php 
  519. for($n = 1; $n <= (int) MEMBERSHIP_MAX_CHARGE; $n++) { 
  520. ?> 
  521. <option value='<?php echo $n; ?>' <?php if($level->level_price == $n) echo "selected='selected'"; ?>><?php echo $n; ?></option> 
  522. <?php 
  523. } 
  524. ?> 
  525. </select><?php */ ?>  
  526. <?php 
  527. if(!empty($M_options['paymentcurrency'])) { 
  528. echo esc_html($M_options['paymentcurrency']); 
  529. } else { 
  530. $M_options['paymentcurrency'] = 'USD'; 
  531. echo esc_html($M_options['paymentcurrency']); 
  532. ?> 
  533. </div> 
  534. <div class='levelinformation' style='float: right;'> 
  535. <p class='description'> 
  536. <strong><?php _e('Mode details', 'membership'); ?></strong><br/> 
  537. <?php _e('<strong>Finite</strong> - user remains at this level for a set period of time before ending', 'membership');?><br/><br/> 
  538. <?php _e('<strong>Indefinite</strong> - user remains at this level for ever.', 'membership');?><br/><br/> 
  539. <?php _e('<strong>Serial</strong> - user remains at this level for a set period of time and is then renewed at the same level', 'membership');?><br/><br/> 
  540. <?php _e('<strong>Note:</strong> - depending on the payment gateway used, changing the price will not alter subscriptions charged to existing members.', 'membership');?> 
  541. </p> 
  542. </div> 
  543. </div> 
  544. </div> 
  545. </li> 
  546. <?php 
  547.  
  548. // Set the afterserial to true 
  549. if($level->sub_type == 'serial' || $level->sub_type == 'indefinite') { 
  550. $afterserial = true; 
  551.  
  552.  
  553.  
  554. // Counting 
  555. function count() { 
  556.  
  557. $sql = $this->db->prepare( "SELECT count(*) as subcount FROM {$this->membership_relationships} WHERE sub_id = %d", $this->id ); 
  558.  
  559. return $this->db->get_var( $sql ); 
  560.  
  561.  
  562. // Meta information 
  563. function get_meta($key, $default = false) { 
  564.  
  565. $sql = $this->db->prepare( "SELECT meta_value FROM {$this->subscriptionmeta} WHERE meta_key = %s AND sub_id = %d", $key, $this->id); 
  566.  
  567. $row = $this->db->get_var( $sql ); 
  568.  
  569. if(empty($row)) { 
  570. return $default; 
  571. } else { 
  572. return $row; 
  573.  
  574.  
  575. function add_meta($key, $value) { 
  576.  
  577. return $this->insertorupdate( $this->subscriptionmeta, array( 'sub_id' => $this->id, 'meta_key' => $key, 'meta_value' => $value) ); 
  578.  
  579.  
  580. function update_meta($key, $value) { 
  581.  
  582. return $this->insertorupdate( $this->subscriptionmeta, array( 'sub_id' => $this->id, 'meta_key' => $key, 'meta_value' => $value) ); 
  583.  
  584.  
  585. function delete_meta($key) { 
  586.  
  587. $sql = $this->db->prepare( "DELETE FROM {$this->subscriptionmeta} WHERE meta_key = %s AND sub_id = %d", $key, $this->id); 
  588.  
  589. return $this->db->query( $sql ); 
  590.  
  591.  
  592. function insertorupdate( $table, $query ) { 
  593.  
  594. $fields = array_keys($query); 
  595. $formatted_fields = array(); 
  596. foreach ( $fields as $field ) { 
  597. $form = '%s'; 
  598. $formatted_fields[] = $form; 
  599. $sql = "INSERT INTO `$table` (`" . implode( '`, `', $fields ) . "`) VALUES ('" . implode( "', '", $formatted_fields ) . "')"; 
  600. $sql .= " ON DUPLICATE KEY UPDATE "; 
  601.  
  602. $dup = array(); 
  603. foreach($fields as $field) { 
  604. $dup[] = "`" . $field . "` = VALUES(`" . $field . "`)"; 
  605.  
  606. $sql .= implode(', ', $dup); 
  607.  
  608. return $this->db->query( $this->db->prepare( $sql, $query ) ); 
  609.  
  610.  
  611.  
  612. ?> 
.