Skip to content

Commit

Permalink
Optimize performance by reusing instances of PayFeed from memory.
Browse files Browse the repository at this point in the history
  • Loading branch information
rvdsteege committed Nov 24, 2023
1 parent 89f88b7 commit fa89eab
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
43 changes: 39 additions & 4 deletions src/FeedsDB.php
Expand Up @@ -23,6 +23,41 @@
* @since 1.0.0
*/
class FeedsDB {
/**
* Feeds.
*
* @var array<int, PayFeed>
*/
private static $feeds = [];

/**
* Get feed.
*
* @param int $post_id Post ID.
* @return PayFeed
*/
public static function get_feed( $post_id ) {
if ( ! array_key_exists( $post_id, self::$feeds ) ) {
self::$feeds[ $post_id ] = new PayFeed( $post_id );
}

return self::$feeds[ $post_id ];
}

/**
* Delete feed.
*
* @param int $post_id Post ID.
* @return void
*/
public static function delete_feed( $post_id ) {
\wp_delete_post( $post_id );

if ( \array_key_exists( $post_id, self::$feeds ) ) {
unset( self::$feeds[ $post_id ] );
}
}

/**
* Get feeds by form ID.
*
Expand All @@ -33,8 +68,6 @@ class FeedsDB {
* @return PayFeed[]
*/
public static function get_feeds_by_form_id( $form_id, $meta = [] ) {
$feeds = [];

$meta_query = [
[
'key' => '_pronamic_pay_gf_form_id',
Expand All @@ -53,8 +86,10 @@ public static function get_feeds_by_form_id( $form_id, $meta = [] ) {
]
);

$feeds = [];

foreach ( $query->posts as $post_id ) {
$feeds[] = new PayFeed( $post_id );
$feeds[] = self::get_feed( $post_id );
}

return $feeds;
Expand Down Expand Up @@ -100,7 +135,7 @@ public static function get_feed_by_entry_id( $entry_id ) {
$feed_id = gform_get_meta( $entry_id, 'ideal_feed_id' );

if ( ! empty( $feed_id ) ) {
return new PayFeed( $feed_id );
return self::get_feed( $feed_id );
}

return null;
Expand Down
4 changes: 2 additions & 2 deletions src/PaymentAddOn.php
Expand Up @@ -401,7 +401,7 @@ public function get_feed( $id ) {
return false;
}

$feed = new PayFeed( $id );
$feed = FeedsDB::get_feed( $id );

$post = array_merge(
$post,
Expand Down Expand Up @@ -648,7 +648,7 @@ public function insert_feed( $form_id, $is_active, $meta ) {
* @param int $feed_id Feed ID.
*/
public function delete_feed( $feed_id ) {
wp_delete_post( $feed_id );
FeedsDB::delete_feed( $feed_id );
}

/**
Expand Down

0 comments on commit fa89eab

Please sign in to comment.