M_Subscription

The Membership 2 M Subscription class.

Defined (1)

The class is defined in the following location(s).

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