GFP_Stripe_Data

Class GFP_Stripe_Data.

Defined (1)

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

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