/includes/class-gfp-stripe-data.php

  1. <?php 
  2.  
  3. /** 
  4. * Class GFP_Stripe_Data 
  5. */ 
  6. class GFP_Stripe_Data { 
  7.  
  8. private static $_current_stripe_form_meta = array(); 
  9. private static $_current_stripe_feeds = array(); 
  10. private static $_current_stripe_transactions = array(); 
  11.  
  12. public static function flush_current_stripe_form_meta() { 
  13. self::$_current_stripe_form_meta = null; 
  14. self::$_current_stripe_feeds = null; 
  15.  
  16. /** 
  17. * @return string 
  18. */ 
  19. public static function get_stripe_table_name() { 
  20. global $wpdb; 
  21.  
  22. return $wpdb->prefix . 'rg_stripe'; 
  23.  
  24. /** 
  25. * @return string 
  26. */ 
  27. public static function get_transaction_table_name() { 
  28. global $wpdb; 
  29.  
  30. return $wpdb->prefix . 'rg_stripe_transaction'; 
  31.  
  32. /** 
  33. * 
  34. * @since 0.1 
  35. */ 
  36. public static function update_table( $current_version ) { 
  37. global $wpdb; 
  38. $stripe_table = self::get_stripe_table_name(); 
  39. $stripe_transaction_table = self::get_transaction_table_name(); 
  40.  
  41. if ( ! empty( $wpdb->charset ) ) { 
  42. $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset"; 
  43. if ( ! empty( $wpdb->collate ) ) { 
  44. $charset_collate .= " COLLATE $wpdb->collate"; 
  45.  
  46. require_once( ABSPATH . '/wp-admin/includes/upgrade.php' ); 
  47.  
  48. if ( ( ! empty( $current_version ) ) && ( version_compare( $current_version, '1.8.2', '<' ) ) ) { 
  49. self::combine_legacy_feeds(); 
  50. self::rename_legacy_meta_column( $stripe_table, 'meta', 'rules' ); 
  51. self::rename_legacy_meta_column( $stripe_table, 'configurations', 'rules' ); 
  52. self::drop_index( $stripe_table, 'form_id' ); 
  53. self::drop_legacy_columns( $stripe_table, array( 'id', 'is_active' ) ); 
  54. self::drop_legacy_columns( $stripe_transaction_table, array( 'subscription_id', 'is_renewal' ) ); 
  55.  
  56. $sql = "CREATE TABLE $stripe_table ( 
  57. form_id mediumint(8) unsigned not null,  
  58. form_settings longtext,  
  59. rules longtext,  
  60. PRIMARY KEY (form_id) 
  61. )$charset_collate;"; 
  62.  
  63. dbDelta( $sql ); 
  64.  
  65. $sql = "CREATE TABLE $stripe_transaction_table ( 
  66. id int(10) unsigned not null auto_increment,  
  67. entry_id int(10) unsigned,  
  68. user_id int(10) unsigned,  
  69. transaction_type varchar(30) not null,  
  70. transaction_id varchar(50),  
  71. amount decimal(19, 2),  
  72. currency varchar(5),  
  73. date_created datetime,  
  74. mode char(4) not null,  
  75. meta longtext,  
  76. PRIMARY KEY (id),  
  77. KEY entry_id (entry_id),  
  78. KEY user_id ( user_id ),  
  79. KEY transaction_type (transaction_type) 
  80. )$charset_collate;"; 
  81.  
  82. dbDelta( $sql ); 
  83.  
  84. do_action( 'gfp_stripe_data_after_update_table' ); 
  85.  
  86.  
  87. /** 
  88. * @param $entry_id 
  89. * @param $transaction_type 
  90. * @param $subscription_id 
  91. * @param $transaction_id 
  92. * @param $amount 
  93. * 
  94. * @return mixed 
  95. */ 
  96. public static function insert_transaction( $entry_id, $user_id = null, $transaction_type, $transaction_id, $amount, $currency, $mode, $meta = '' ) { 
  97.  
  98. GFP_Stripe::log_debug( 'Inserting transaction into transaction table' ); 
  99.  
  100. global $wpdb; 
  101. $table_name = self::get_transaction_table_name(); 
  102.  
  103. $sql = $wpdb->prepare( " INSERT INTO $table_name (entry_id, user_id, transaction_type, transaction_id, amount, currency, date_created, mode, meta) 
  104. values(%d, %d, %s, %s, %f, %s, utc_timestamp(), %s, %s)", $entry_id, $user_id, $transaction_type, $transaction_id, $amount, $currency, $mode, json_encode( $meta ) ); 
  105. $wpdb->query( $sql ); 
  106. $id = $wpdb->insert_id; 
  107.  
  108. do_action( 'gform_post_payment_transaction', $id, $entry_id, $transaction_type, $transaction_id, $amount, false ); 
  109.  
  110. return $id; 
  111.  
  112. public static function update_transaction( $entry_id, $property_name, $property_value ) { 
  113. global $wpdb; 
  114. $table_name = self::get_transaction_table_name(); 
  115.  
  116. $result = $wpdb->update( $table_name, array( $property_name => $property_value ), array( 'entry_id' => $entry_id ) ); 
  117.  
  118. /** 
  119. * @return mixed 
  120. */ 
  121. public static function get_all_feeds() { 
  122. global $wpdb; 
  123.  
  124. $table_name = self::get_stripe_table_name(); 
  125. $sql = "SELECT * FROM $table_name"; 
  126. $results = $wpdb->get_results( $sql, ARRAY_A ); 
  127.  
  128. $feeds = array(); 
  129.  
  130. foreach ( $results as $result ) { 
  131. $stripe_form_meta = self::process_stripe_form_meta( $result ); 
  132. if ( ( ! empty( $stripe_form_meta ) ) && ( ! empty( $stripe_form_meta[ 'rules' ] ) ) ) { 
  133. $feeds[ ] = $stripe_form_meta; 
  134.  
  135. return $feeds; 
  136.  
  137. public static function get_stripe_form_meta( $form_id ) { 
  138. global $wpdb; 
  139.  
  140. if ( isset( self::$_current_stripe_form_meta[ $form_id ] ) ) { 
  141. return self::$_current_stripe_form_meta[ $form_id ]; 
  142.  
  143. $table_name = self::get_stripe_table_name(); 
  144. $stripe_form_meta = $wpdb->get_row( $wpdb->prepare( "SELECT form_settings, rules FROM {$table_name} WHERE form_id=%d", $form_id ), ARRAY_A ); 
  145.  
  146. $stripe_form_meta = self::process_stripe_form_meta( $stripe_form_meta ); 
  147.  
  148. self::$_current_stripe_form_meta[ $form_id ] = $stripe_form_meta; 
  149.  
  150. return $stripe_form_meta; 
  151.  
  152. public static function get_all_form_ids() { 
  153. global $wpdb; 
  154. $table = GFFormsModel::get_form_table_name(); 
  155. $sql = "SELECT id from $table"; 
  156. $results = $wpdb->get_col( $sql ); 
  157.  
  158. return $results; 
  159.  
  160. /** 
  161. * @param $form_id 
  162. * @param bool $only_active 
  163. * 
  164. * @return array 
  165. */ 
  166. public static function get_feed_by_form( $form_id, $only_active = false ) { 
  167. global $wpdb; 
  168.  
  169. if ( isset( self::$_current_stripe_feeds[ $form_id ] ) ) { 
  170. $feeds = self::$_current_stripe_feeds[ $form_id ]; 
  171. } else { 
  172. $table_name = self::get_stripe_table_name(); 
  173. $form_table_name = RGFormsModel::get_form_table_name(); 
  174.  
  175. $sql = $wpdb->prepare( "SELECT s.form_id, s.rules, f.title as form_title 
  176. FROM $table_name s 
  177. INNER JOIN $form_table_name f ON s.form_id = f.id 
  178. WHERE form_id=%d", $form_id ); 
  179.  
  180. $results = $wpdb->get_results( $sql, ARRAY_A ); 
  181.  
  182. if ( empty( $results ) ) { 
  183. return array(); 
  184.  
  185. $rules = GFFormsModel::unserialize( $results[ 0 ][ 'rules' ] ); 
  186. $feeds = array(); 
  187.  
  188. foreach ( $rules as $rule ) { 
  189. $feed_items = array( 
  190. 'id' => $rule[ 'id' ],  
  191. 'form_id' => $results[ 0 ][ 'form_id' ],  
  192. 'form_title' => $results[ 0 ][ 'form_title' ],  
  193. 'meta' => $rule 
  194. ); 
  195. if ( isset( $rule[ 'is_active' ] ) ) { 
  196. $feed_items[ 'is_active' ] = $rule[ 'is_active' ]; 
  197. $feeds[ ] = $feed_items; 
  198. self::$_current_stripe_feeds[ $form_id ] = $feeds; 
  199.  
  200.  
  201. if ( $only_active ) { 
  202. $active_feeds = array(); 
  203. foreach ( $feeds as $feed ) { 
  204. if ( ( isset( $feed[ 'is_active' ] ) ) && ( true == $feed[ 'is_active' ] ) ) { 
  205. $active_feeds[ ] = $feed; 
  206. $feeds = $active_feeds; 
  207.  
  208.  
  209. return $feeds; 
  210.  
  211. /** 
  212. * @param $feed_id 
  213. * 
  214. * @return array 
  215. */ 
  216. public static function get_feed( $form_id, $feed_id ) { 
  217. $form_feeds = self::get_feed_by_form( $form_id ); 
  218.  
  219. foreach ( $form_feeds as $form_feed ) { 
  220. if ( ( $form_feed[ 'id' ] == $feed_id ) || ( ! empty( $form_feed[ 'meta' ][ 'old_id' ] ) && $form_feed[ 'meta' ][ 'old_id' ] == $feed_id ) ) { 
  221. $feed = $form_feed; 
  222. break; 
  223.  
  224. return $feed; 
  225.  
  226. public static function get_transaction_by( $type, $value ) { 
  227. global $wpdb; 
  228. $table_name = self::get_transaction_table_name(); 
  229. $transaction = null; 
  230.  
  231. if ( 'entry' == $type || 'user_id' == $type || 'transaction_id' == $type ) { 
  232. if ( isset( self::$_current_stripe_transactions[ $value ] ) ) { 
  233. $transaction = self::$_current_stripe_transactions[ $value ]; 
  234. } else { 
  235. switch ( $type ) { 
  236. case 'entry': 
  237. $sql = $wpdb->prepare( "SELECT * FROM {$table_name} 
  238. WHERE entry_id=%d", $value ); 
  239. $transaction = $wpdb->get_row( $sql, ARRAY_A ); 
  240. if ( ! empty( $transaction ) ) { 
  241. $transaction[ 'meta' ] = GFFormsModel::unserialize( $transaction[ 'meta' ] ); 
  242. self::$_current_stripe_transactions[ $value ] = $transaction; 
  243. break; 
  244. case 'user_id': 
  245. $sql = $wpdb->prepare( "SELECT * FROM {$table_name} 
  246. WHERE user_id=%d", $value ); 
  247. $transaction = $wpdb->get_results( $sql, ARRAY_A ); 
  248. foreach ( $transaction as $key => $t ) { 
  249. $transaction[ $key ][ 'meta' ] = GFFormsModel::unserialize( $t[ 'meta' ] ); 
  250. self::$_current_stripe_transactions[ $value ] = $transaction; 
  251. break; 
  252. case 'transaction_id': 
  253. $sql = $wpdb->prepare( "SELECT * FROM {$table_name} 
  254. WHERE transaction_id=%s", $value ); 
  255. $transaction = $wpdb->get_row( $sql, ARRAY_A ); 
  256. if ( ! empty( $transaction ) ) { 
  257. $transaction[ 'meta' ] = GFFormsModel::unserialize( $transaction[ 'meta' ] ); 
  258. self::$_current_stripe_transactions[ $value ] = $transaction; 
  259. break; 
  260.  
  261. return $transaction; 
  262.  
  263. private static function process_stripe_form_meta( $meta ) { 
  264. $meta[ 'rules' ] = GFFormsModel::unserialize( $meta[ 'rules' ] ); 
  265.  
  266. if ( ! $meta[ 'rules' ] ) { 
  267. return null; 
  268.  
  269. $meta[ 'form_settings' ] = GFFormsModel::unserialize( $meta[ 'form_settings' ] ); 
  270.  
  271. return $meta; 
  272.  
  273. public static function save_feeds( $form_id, $feeds ) { 
  274. return self::update_stripe_form_meta( $form_id, $feeds, 'rules' ); 
  275.  
  276.  
  277. public static function update_stripe_form_meta( $form_id, $meta, $meta_name ) { 
  278. global $wpdb; 
  279. $stripe_table_name = self::get_stripe_table_name(); 
  280. $meta = json_encode( $meta ); 
  281.  
  282. if ( intval( $wpdb->get_var( $wpdb->prepare( "SELECT count(0) FROM $stripe_table_name WHERE form_id=%d", $form_id ) ) ) > 0 ) { 
  283. $result = $wpdb->query( $wpdb->prepare( "UPDATE $stripe_table_name SET $meta_name=%s WHERE form_id=%d", $meta, $form_id ) ); 
  284. } else { 
  285. $result = $wpdb->query( $wpdb->prepare( "INSERT INTO $stripe_table_name(form_id, $meta_name) VALUES(%d, %s)", $form_id, $meta ) ); 
  286.  
  287. self::$_current_stripe_form_meta[ $form_id ] = null; 
  288. self::$_current_stripe_feeds[ $form_id ] = null; 
  289.  
  290. return $result; 
  291.  
  292. public static function delete_stripe_form_meta( $form_id ) { 
  293. global $wpdb; 
  294. $stripe_table_name = self::get_stripe_table_name(); 
  295.  
  296. $wpdb->query( $wpdb->prepare( "DELETE FROM $stripe_table_name WHERE form_id=%s", $form_id ) ); 
  297.  
  298. self::flush_current_stripe_form_meta(); 
  299.  
  300. /** 
  301. * @param $id 
  302. */ 
  303. public static function delete_feed( $feed_id, $form_id ) { 
  304.  
  305. if ( ! $form_id ) { 
  306. return false; 
  307.  
  308. $form_meta = self::get_stripe_form_meta( $form_id ); 
  309. unset( $form_meta[ 'rules' ][ $feed_id ] ); 
  310.  
  311. self::flush_current_stripe_form_meta(); 
  312.  
  313. return self::save_feeds( $form_id, $form_meta[ 'rules' ] ); 
  314.  
  315. /** 
  316. * 
  317. */ 
  318. public static function drop_tables() { 
  319. global $wpdb; 
  320. $wpdb->query( "DROP TABLE IF EXISTS " . self::get_stripe_table_name() ); 
  321. $wpdb->query( "DROP TABLE IF EXISTS " . self::get_transaction_table_name() ); 
  322.  
  323. /**--------- LEGACY ---------*/ 
  324.  
  325. public static function update_legacy_feed( $id, $form_id, $is_active, $setting ) { 
  326. global $wpdb; 
  327. $table_name = self::get_stripe_table_name(); 
  328. $setting = json_encode( $setting ); 
  329.  
  330. $wpdb->update( $table_name, array( 
  331. "form_id" => $form_id,  
  332. "is_active" => $is_active,  
  333. "meta" => $setting 
  334. ), array( "id" => $id ), array( "%d", "%d", "%s" ), array( "%d" ) ); 
  335.  
  336. return $id; 
  337.  
  338. public static function delete_legacy_feed( $id ) { 
  339. global $wpdb; 
  340. $table_name = self::get_stripe_table_name(); 
  341. $wpdb->query( $wpdb->prepare( "DELETE FROM $table_name WHERE id=%s", $id ) ); 
  342.  
  343. private static function combine_legacy_feeds() { 
  344.  
  345. $form_ids = self::get_all_form_ids(); 
  346.  
  347. foreach ( $form_ids as $form_id ) { 
  348. $stripe_form_meta = self::get_legacy_stripe_form_meta( $form_id ); 
  349. if ( empty( $stripe_form_meta ) ) { 
  350. continue; 
  351. } else { 
  352. $rules = array(); 
  353. $id_index = 1; 
  354. foreach ( $stripe_form_meta as $meta_row ) { 
  355. $id = "{$form_id}.{$id_index}"; 
  356. $meta_row[ 'meta' ][ 'is_active' ] = $meta_row[ 'is_active' ]; 
  357. $meta_row[ 'meta' ][ 'old_id' ] = $meta_row[ 'id' ]; 
  358. $meta_row[ 'meta' ][ 'id' ] = $id; 
  359. $rules[ $id ] = $meta_row[ 'meta' ]; 
  360. $id_index ++; 
  361. self::update_legacy_feed( $stripe_form_meta[ 0 ][ 'id' ], $stripe_form_meta[ 0 ][ 'form_id' ], $stripe_form_meta[ 0 ][ 'is_active' ], $rules ); 
  362. if ( 1 < count( $stripe_form_meta ) ) { 
  363. foreach ( $stripe_form_meta as $key => $meta_row ) { 
  364. if ( 0 !== $key ) { 
  365. self::delete_legacy_feed( $meta_row[ 'id' ] ); 
  366.  
  367.  
  368. private static function get_legacy_stripe_form_meta( $form_id ) { 
  369. global $wpdb; 
  370.  
  371. $table = self::get_stripe_table_name(); 
  372. $sql = $wpdb->prepare( "SELECT * FROM $table WHERE form_id=%d", $form_id ); 
  373. $results = $wpdb->get_results( $sql, ARRAY_A ); 
  374. if ( empty( $results ) ) { 
  375. $results = array(); 
  376. } else { 
  377. if ( array_key_exists( 'meta', $results[ 0 ] ) ) { 
  378.  
  379. //Deserializing meta 
  380. $count = sizeof( $results ); 
  381. for ( $i = 0; $i < $count; $i ++ ) { 
  382. $results[ $i ][ 'meta' ] = maybe_unserialize( $results[ $i ][ 'meta' ] ); 
  383.  
  384. return $results; 
  385. } else { 
  386. $results = array(); 
  387.  
  388. return $results; 
  389.  
  390. private static function rename_legacy_meta_column( $table, $old_name, $new_name ) { 
  391.  
  392. global $wpdb; 
  393.  
  394. if ( self::has_column( $table, $old_name ) ) { 
  395. $sql = "ALTER TABLE $table CHANGE $old_name $new_name longtext"; 
  396. $result = $wpdb->query( $sql ); 
  397.  
  398.  
  399. private static function drop_index( $table, $index ) { 
  400. global $wpdb; 
  401. $has_index = $wpdb->get_var( "SHOW INDEX FROM {$table} WHERE Key_name='{$index}'" ); 
  402. if ( $has_index ) { 
  403. $wpdb->query( "DROP INDEX {$index} ON {$table}" ); 
  404.  
  405. private static function drop_legacy_columns( $table, $columns ) { 
  406. global $wpdb; 
  407.  
  408. $has_columns = false; 
  409.  
  410. $drop = ''; 
  411. foreach ( $columns as $key => $column ) { 
  412. if ( self::has_column( $table, $column ) ) { 
  413. $has_columns = true; 
  414. if ( 0 == $key ) { 
  415. $drop = "DROP {$column}"; 
  416. } else { 
  417. $drop .= ", DROP {$column}"; 
  418. if ( $has_columns ) { 
  419. $sql = "ALTER TABLE $table $drop"; 
  420. $result = $wpdb->query( $sql ); 
  421.  
  422. private static function has_column( $table, $column ) { 
  423. global $wpdb; 
  424.  
  425. $sql = "SHOW COLUMNS FROM $table LIKE '$column'"; 
  426. $has_column = $wpdb->get_var( $sql ); 
  427.  
  428. return $has_column; 
  429.  
.