prefix . 'postfee_orders'; } public static function create( $data ) { global $wpdb; $data = wp_parse_args( $data, array( 'status' => 'pending', 'created_at' => postfee_now(), ) ); $wpdb->insert( self::table(), $data ); return (int) $wpdb->insert_id; } public static function get( $id ) { global $wpdb; return $wpdb->get_row( $wpdb->prepare( 'SELECT * FROM %i WHERE id = %d', self::table(), $id ) ); } public static function update( $id, $data ) { global $wpdb; return $wpdb->update( self::table(), $data, array( 'id' => $id ) ); } public static function find_by_ref( $gateway, $ref, $status = '' ) { global $wpdb; if ( $status ) { return $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM %i WHERE gateway = %s AND gateway_ref = %s AND status = %s', self::table(), $gateway, $ref, $status ) ); } return $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM %i WHERE gateway = %s AND gateway_ref = %s', self::table(), $gateway, $ref ) ); } /** * Moves a pending order to paid. Returns true only for the call that made * the change, so return URLs and webhooks racing each other fulfil once. */ public static function mark_paid( $id, $gateway_ref = '' ) { global $wpdb; if ( $gateway_ref ) { return 1 === $wpdb->query( $wpdb->prepare( "UPDATE %i SET status = 'paid', paid_at = %s, gateway_ref = %s WHERE id = %d AND status = 'pending'", self::table(), postfee_now(), $gateway_ref, $id ) ); } return 1 === $wpdb->query( $wpdb->prepare( "UPDATE %i SET status = 'paid', paid_at = %s WHERE id = %d AND status = 'pending'", self::table(), postfee_now(), $id ) ); } public static function query( $per_page = 30, $page = 1 ) { global $wpdb; return $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM %i ORDER BY id DESC LIMIT %d OFFSET %d', self::table(), $per_page, ( max( 1, $page ) - 1 ) * $per_page ) ); } public static function count() { global $wpdb; return (int) $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM %i', self::table() ) ); } }