Skip to content

Commit 34f842c

Browse files
committed
Merge branch 'develop'
2 parents 744c953 + f4f62d1 commit 34f842c

File tree

5 files changed

+104
-107
lines changed

5 files changed

+104
-107
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/)
55
and this project adheres to [Semantic Versioning](http://semver.org/).
66

7+
## [4.21.1] - 2022-07-20
8+
### Fixed
9+
* An out-of-memory PHP error when importing items.
10+
711
## [4.21] - 2022-07-13
812
### Change
913
* Updated Twig to v1.42.2, to support PHP 8 or later.

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"require": {
1515
"php": "^5.4 | ^7.0",
1616
"ext-curl": "*",
17+
"ext-json": "*",
1718
"ext-simplexml": "*",
1819
"psr/log": "^1.1",
1920
"twig/twig": "^1.0 | ^2.10",

includes/feed-importing.php

Lines changed: 68 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
* Called on hook 'wprss_fetch_single_feed_hook'.
2424
*
2525
* @since 3.2
26+
*
27+
* @throws Exception
2628
*/
2729
function wprss_fetch_insert_single_feed_items( $feed_ID ) {
2830
set_transient('wpra/feeds/importing/' . $feed_ID, true, 0);
@@ -104,56 +106,72 @@ function wprss_fetch_insert_single_feed_items( $feed_ID ) {
104106
? wprss_get_general_setting('unique_titles')
105107
: $unique_titles_only;
106108
$unique_titles_only = filter_var($unique_titles_only, FILTER_VALIDATE_BOOLEAN);
107-
// Gather the titles of the items that are imported
108-
// The import process will check not only the titles in the DB but the titles currently in the feed
109-
$existing_titles = [];
110109

111110
// Gather the existing feed item IDs for this feed source
112111
$useGuids = get_post_meta($feed_ID, 'wprss_use_guids', true);
113112
$useGuids = filter_var($useGuids, FILTER_VALIDATE_BOOLEAN);
114-
$existingIds = $useGuids
115-
? wprss_get_existing_guids()
116-
: wprss_get_existing_permalinks();
113+
114+
// Gather the IDs and titles of the items that are imported
115+
// The import process will not only check the IDs and titles against the DB, but also against the feed
116+
// itself. This prevents duplicate items in the feed from importing duplicates.
117+
$existingIds = [];
118+
$existingTitles = [];
117119

118120
// Generate a list of items fetched, that are not already in the DB
119121
$new_items = array();
120122
foreach ( $items_to_insert as $item ) {
121-
$item_title = $item->get_title();
123+
$itemTitle = $item->get_title();
122124
$guid = $item->get_id();
123125
$permalink = $item->get_permalink();
124126
$permalink = wprss_normalize_permalink( $permalink, $item, $feed_ID );
125127

126128
// Check if blacklisted
127129
if (wprss_is_blacklisted($permalink)) {
128-
$logger->debug('Item "{0}" is blacklisted', [$item_title]);
129-
130+
$logger->debug('Item "{0}" is blacklisted', [$itemTitle]);
130131
continue;
131132
}
132133

133-
// Check if already imported
134-
$idToCheck = $useGuids ? $guid : $permalink;
135-
if (array_key_exists($idToCheck, $existingIds)) {
136-
$logger->debug('Item "{0}" already exists in the database', [$item_title]);
134+
$itemId = $useGuids ? $guid : $permalink;
135+
136+
// Check if already imported in database
137+
//-----------------------------------------
138+
$itemIdExists = $useGuids ? wprss_guid_exists($guid) : wprss_permalink_exists($permalink);
139+
$itemsTitleExists = $unique_titles_only && wprss_item_title_exists($item->get_title());
140+
141+
if ($itemIdExists || $itemsTitleExists) {
142+
$reason = $itemIdExists
143+
? ($useGuids ? 'GUID' : 'permalink')
144+
: 'Non-unique title';
145+
146+
$logger->debug('Item "{title}" already exists in the database. Reason: {reason}', [
147+
'title' => $itemTitle,
148+
'reason' => $reason
149+
]);
137150

138151
continue;
139152
}
140153

141-
// Check if title exists (if the option is enabled)
142-
if ($unique_titles_only) {
143-
$title_exists_db = wprss_item_title_exists($item->get_title());
144-
$title_exists_feed = array_key_exists($item_title, $existing_titles);
145-
$title_exists = $title_exists_db || $title_exists_feed;
146-
// Add this item's title to the list to check against
147-
$existing_titles[$item_title] = 1;
154+
// Check if item is duplicated in the feed
155+
//-----------------------------------------
156+
$itemIdIsDuped = array_key_exists($itemId, $existingIds);
157+
$itemTitleIsDuped = $unique_titles_only && array_key_exists($itemTitle, $existingTitles);
148158

149-
if ($title_exists) {
150-
$logger->debug('An item with the title "{0}" already exists', [$item_title]);
159+
if ($itemIdIsDuped || $itemTitleIsDuped) {
160+
$reason = $itemIdIsDuped
161+
? ($useGuids ? 'GUID' : 'permalink')
162+
: 'Non-unique title';
151163

152-
continue;
153-
}
164+
$logger->debug('Item "{title}" is duplicated in the feed. Reason: {reason}', [
165+
'title' => $itemTitle,
166+
'reason' => $reason,
167+
]);
168+
169+
continue;
170+
} else {
171+
$existingIds[$itemId] = 1;
172+
$existingTitles[$itemTitle] = 1;
154173
}
155174

156-
$existingIds[$idToCheck] = 1;
157175
$new_items[] = $item;
158176
}
159177

@@ -184,14 +202,14 @@ function wprss_fetch_insert_single_feed_items( $feed_ID ) {
184202
? 0
185203
: $num_new_items - $num_can_insert;
186204

187-
// Get an array with the DB feed items in reverse order (oldest first)
205+
// Get an array with the DB feed items in reverse order (the oldest first)
188206
$db_feed_items_reversed = array_reverse( $db_feed_items->posts );
189207
// Cut the array to get only the first few that are to be deleted ( equal to $num_feed_items_to_delete )
190208
$feed_items_to_delete = array_slice( $db_feed_items_reversed, 0, $num_feed_items_to_delete );
191209

192210
// Iterate the feed items and delete them
193211
$num_items_deleted = 0;
194-
foreach ( $feed_items_to_delete as $key => $post ) {
212+
foreach ( $feed_items_to_delete as $post ) {
195213
wp_delete_post( $post->ID, TRUE );
196214
$num_items_deleted++;
197215
}
@@ -201,7 +219,7 @@ function wprss_fetch_insert_single_feed_items( $feed_ID ) {
201219
}
202220
}
203221

204-
update_post_meta( $feed_ID, 'wprss_last_update', $last_update_time = time() );
222+
update_post_meta( $feed_ID, 'wprss_last_update', time() );
205223
update_post_meta( $feed_ID, 'wprss_last_update_items', 0 );
206224

207225
// Insert the items into the db
@@ -270,34 +288,31 @@ function wprss_get_feed_items( $feed_url, $source, $force_feed = FALSE ) {
270288
}
271289

272290
if (defined('WP_DEBUG') && WP_DEBUG) {
273-
add_action('cron_request', 'wprss_cron_add_xdebug_cookie', 10);
274-
}
275-
276-
/**
277-
* Allow debugging of wp_cron jobs using xDebug.
278-
*
279-
* This is done by taking the XDEBUG cookie received from the browser (which enables an xDebug session) and passing it
280-
* to WP Cron. That way, code initiated from a cron job will be debuggable.
281-
*
282-
* @param array $cronRequest
283-
*
284-
* @return array $cron_request_array with the current XDEBUG_SESSION cookie added if set
285-
*/
286-
function wprss_cron_add_xdebug_cookie($cronRequest)
287-
{
288-
if (empty($_COOKIE['XDEBUG_SESSION'])) {
289-
return ($cronRequest);
290-
}
291+
/**
292+
* Allow debugging of wp_cron jobs using xDebug.
293+
*
294+
* This is done by taking the XDEBUG cookie received from the browser (which enables an xDebug session) and passing it
295+
* to WP Cron. That way, code initiated from a cron job will be debuggable.
296+
*
297+
* @param array $cronRequest
298+
*
299+
* @return array $cron_request_array with the current XDEBUG_SESSION cookie added if set
300+
*/
301+
add_action('cron_request', function($cronRequest) {
302+
if (empty($_COOKIE['XDEBUG_SESSION'])) {
303+
return ($cronRequest);
304+
}
291305

292-
$cookie = filter_var($_COOKIE['XDEBUG_SESSION'], FILTER_SANITIZE_STRING);
306+
$cookie = filter_var($_COOKIE['XDEBUG_SESSION'], FILTER_SANITIZE_STRING);
293307

294-
if (empty($cronRequest['args']['cookies'])) {
295-
$cronRequest['args']['cookies'] = [];
296-
}
308+
if (empty($cronRequest['args']['cookies'])) {
309+
$cronRequest['args']['cookies'] = [];
310+
}
297311

298-
$cronRequest['args']['cookies']['XDEBUG_SESSION'] = $cookie;
312+
$cronRequest['args']['cookies']['XDEBUG_SESSION'] = $cookie;
299313

300-
return $cronRequest;
314+
return $cronRequest;
315+
});
301316
}
302317

303318
/**
@@ -364,7 +379,7 @@ function wprss_fetch_feed($url, $source = null, $param_force_feed = false)
364379

365380
// If a feed source was passed
366381
if ($source !== null || $param_force_feed) {
367-
// Get the force feed option for the feed source
382+
// Get the force-feed option for the feed source
368383
$force_feed = get_post_meta($source, 'wprss_force_feed', true);
369384
// If turned on, force the feed
370385
if ($force_feed == 'true' || $param_force_feed) {

includes/feed-processing.php

Lines changed: 29 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -120,39 +120,36 @@ function wprss_get_feed_items_for_source($source_id)
120120
return wprss_get_imported_items($source_id);
121121
}
122122

123-
/**
124-
* Queries the DB to get the GUIDs for existing feed items.
125-
*/
126-
function wprss_get_existing_guids() {
123+
/** Checks if a permalink exists in the database. */
124+
function wprss_permalink_exists($permalink) {
127125
global $wpdb;
128126

129-
$cols = $wpdb->get_col(
130-
"SELECT q.`meta_value`
131-
FROM {$wpdb->postmeta} AS p
132-
JOIN {$wpdb->postmeta} AS q
133-
ON (q.`meta_key` = 'wprss_item_guid' AND p.`post_id` = q.`post_id`)"
127+
$numRows = $wpdb->query(
128+
$wpdb->prepare(
129+
"SELECT `meta_value`
130+
FROM {$wpdb->postmeta}
131+
WHERE (`meta_key` = 'wprss_item_permalink' AND `meta_value` = %s)",
132+
$permalink
133+
)
134134
);
135135

136-
return @array_flip($cols);
136+
return !!$numRows;
137137
}
138138

139-
/**
140-
* Database query to get existing permalinks
141-
*
142-
* @since 3.0
143-
*/
144-
function wprss_get_existing_permalinks()
145-
{
139+
/** Checks if a GUID exists in the database. */
140+
function wprss_guid_exists($guid) {
146141
global $wpdb;
147142

148-
$cols = $wpdb->get_col(
149-
"SELECT q.`meta_value`
150-
FROM {$wpdb->postmeta} AS p
151-
JOIN {$wpdb->postmeta} AS q
152-
ON (q.`meta_key` = 'wprss_item_permalink' AND p.`post_id` = q.`post_id`)"
143+
$numRows = $wpdb->query(
144+
$wpdb->prepare(
145+
"SELECT `meta_value`
146+
FROM {$wpdb->postmeta}
147+
WHERE (`meta_key` = 'wprss_item_guid' AND `meta_value` = %s)",
148+
$guid
149+
)
153150
);
154151

155-
return @array_flip($cols);
152+
return !!$numRows;
156153
}
157154

158155
/**
@@ -168,37 +165,17 @@ function wprss_item_title_exists($title)
168165
{
169166
global $wpdb;
170167

171-
$query = $wpdb->prepare(
172-
"SELECT *
173-
FROM `{$wpdb->posts}` AS p
174-
JOIN `{$wpdb->postmeta}` AS q
175-
ON p.`ID` = q.`post_id`
176-
WHERE q.`meta_key` = 'wprss_feed_id' AND p.`post_title` = %s",
177-
[html_entity_decode($title)]
178-
);
179-
180-
$cols = $wpdb->get_col($query);
181-
182-
return count($cols) > 0;
183-
}
184-
185-
/**
186-
* Database query to get existing titles
187-
*
188-
* @since 4.7
189-
*/
190-
function wprss_get_existing_titles()
191-
{
192-
global $wpdb;
193-
194-
$cols = $wpdb->get_col(
195-
"SELECT p.`post_title`
196-
FROM `{$wpdb->posts}` AS p
197-
JOIN `{$wpdb->postmeta}` AS q
198-
ON p.`ID` = q.`post_id`"
168+
$numRows = $wpdb->query(
169+
$wpdb->prepare(
170+
"SELECT p.`post_title`
171+
FROM {$wpdb->posts} AS p
172+
JOIN {$wpdb->postmeta} AS q ON p.`ID` = q.`post_id`
173+
WHERE q.`meta_key` = 'wprss_feed_id' AND p.`post_title` = %s",
174+
[html_entity_decode($title)]
175+
)
199176
);
200177

201-
return @array_flip($cols);
178+
return !!$numRows;
202179
}
203180

204181
add_action('publish_wprss_feed', 'wprss_fetch_insert_feed_items', 10);

wp-rss-aggregator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Plugin Name: WP RSS Aggregator
55
* Plugin URI: https://www.wprssaggregator.com/#utm_source=wpadmin&utm_medium=plugin&utm_campaign=wpraplugin
66
* Description: Imports and aggregates multiple RSS Feeds.
7-
* Version: 4.21
7+
* Version: 4.21.1
88
* Author: RebelCode
99
* Author URI: https://www.wprssaggregator.com
1010
* Text Domain: wprss
@@ -76,7 +76,7 @@
7676

7777
// Set the version number of the plugin.
7878
if( !defined( 'WPRSS_VERSION' ) )
79-
define( 'WPRSS_VERSION', '4.21' );
79+
define( 'WPRSS_VERSION', '4.21.1' );
8080

8181
if( !defined( 'WPRSS_WP_MIN_VERSION' ) )
8282
define( 'WPRSS_WP_MIN_VERSION', '4.8' );

0 commit comments

Comments
 (0)