Skip to content

Commit ce31692

Browse files
authored
reduce the number of db calls on the wp dashboard and move some check… (#295)
* reduce the number of db calls on the wp dashboard and move some checks to link fixer dashboard, added harder caching on stats * remove unsused code.
1 parent 6524e16 commit ce31692

4 files changed

Lines changed: 29 additions & 17 deletions

File tree

src/Dashboard/Dashboard_Notifications.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,18 +90,34 @@ public function render_widget(): void {
9090
'iawmlf_is_online' => iawmlf_is_archive_api_online(),
9191
'iawmlf_link_to_settings' => Settings_Page::get_page_url(),
9292
'iawmlf_link_table' => Report_Page::get_page_url(),
93-
'iawmlf_total_links' => ( new Link_Repository() )->query_links( PHP_INT_MAX ),
93+
'iawmlf_total_link_count' => self::get_link_count(),
9494
'iawmlf_auto_archiver_enabled' => Settings::add_own_links(),
9595
'iawmlf_scan_existing_enabled' => Settings::should_scan_existing_posts(),
9696
'iawmlf_link_processing_enabled' => Settings::is_link_processing_enabled(),
9797
'iawmlf_link_check_duration' => Settings::get_link_check_duration(),
9898
'iawmlf_failed_check_count' => Settings::get_failed_count(),
9999
'iawmlf_onboarding_details' => Dashboard_Statistics::get_onboarding_statistics(),
100-
'iawmlf_link_stats' => Dashboard_Statistics::get_link_statistics(),
101100
)
102101
);
103102
}
104103

104+
/**
105+
* Gets the link count from cache.
106+
*
107+
* @since 1.3.5
108+
*
109+
* @return integer
110+
*/
111+
public static function get_link_count(): int {
112+
$cached = get_transient( 'iawmlf_link_count' );
113+
if ( false !== $cached ) {
114+
return (int) $cached;
115+
}
116+
$count = ( new Link_Repository() )->count_links( PHP_INT_MAX, 1, array(), array(), array(), 'any' );
117+
set_transient( 'iawmlf_link_count', $count, 15 * MINUTE_IN_SECONDS );
118+
return $count;
119+
}
120+
105121
/**
106122
* Get the sites account details from Archive.org.
107123
*

src/Dashboard/Dashboard_Statistics.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,13 @@ private static function compile_link_statistics(): array {
128128

129129
// Get all the link stats.
130130
$all_links = $links->count_links( \PHP_INT_MAX, 1 );
131-
$all_broken = $links->count_links( \PHP_INT_MAX, 1, array( Link_Repository::LINK_STATUS_BROKEN ), array(), array(), Link_Repository::ORDER_DATE_DESC, null, null, false );
131+
$all_broken = $links->count_links( \PHP_INT_MAX, 1, array( Link_Repository::LINK_STATUS_BROKEN ), array(), array(), 'fallback', null, null, false );
132132
$has_archive_link = $links->count_links( \PHP_INT_MAX, 1, array(), array(), array( Link_Repository::LINK_HAS_ARCHIVE ) );
133-
$redirected_broken_ = $links->count_links( \PHP_INT_MAX, 1, array( Link_Repository::LINK_STATUS_BROKEN ), array(), array( Link_Repository::LINK_HAS_ARCHIVE ), Link_Repository::ORDER_DATE_DESC, null, null, false );
134-
$not_checked = $links->count_links( \PHP_INT_MAX, 1, array(), array(), array(), Link_Repository::ORDER_DATE_DESC, null, null, null, null, false );
135-
$process_new = $links->count_links( \PHP_INT_MAX, 1, array(), array(), array(), Link_Repository::ORDER_DATE_DESC, null, null, null, array( Link::PROCESS_NEW ) );
136-
$process_done_ = $links->count_links( \PHP_INT_MAX, 1, array(), array(), array(), Link_Repository::ORDER_DATE_DESC, null, null, null, array( Link::PROCESS_DONE ) );
137-
$process_pending = $links->count_links( \PHP_INT_MAX, 1, array(), array(), array(), Link_Repository::ORDER_DATE_DESC, null, null, null, array( Link::PROCESS_PENDING ) );
133+
$redirected_broken_ = $links->count_links( \PHP_INT_MAX, 1, array( Link_Repository::LINK_STATUS_BROKEN ), array(), array( Link_Repository::LINK_HAS_ARCHIVE ), 'fallback', null, null, false );
134+
$not_checked = $links->count_links( \PHP_INT_MAX, 1, array(), array(), array(), 'fallback', null, null, null, null, false );
135+
$process_new = $links->count_links( \PHP_INT_MAX, 1, array(), array(), array(), 'fallback', null, null, null, array( Link::PROCESS_NEW ) );
136+
$process_done_ = $links->count_links( \PHP_INT_MAX, 1, array(), array(), array(), 'fallback', null, null, null, array( Link::PROCESS_DONE ) );
137+
$process_pending = $links->count_links( \PHP_INT_MAX, 1, array(), array(), array(), 'fallback', null, null, null, array( Link::PROCESS_PENDING ) );
138138
$last_checks = $links->query_links( 10, 1, array(), array(), array(), Link_Repository::ORDER_DATE_DESC, null, null, null, null, null, true );
139139

140140
// Extract the details from last checks.

templates/admin/dashboard/page.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131

3232
// Extract link statistics
33-
$iawmlf_total_links_count = $iawmlf_link_stats['total_links'] ?? 0;
33+
$iawmlf_total_link_count = $iawmlf_link_stats['total_links'] ?? 0;
3434
$iawmlf_all_broken_links = $iawmlf_link_stats['all_broken_links'] ?? 0;
3535
$iawmlf_links_with_archive = $iawmlf_link_stats['links_with_archive'] ?? 0;
3636
$iawmlf_links_without_archive = $iawmlf_link_stats['links_without_archive'] ?? 0;
@@ -134,7 +134,7 @@
134134
<!-- Row 1: Total Links | Being Redirected -->
135135
<div class="iawmlf_dashboard-stats-box">
136136
<a href="<?php echo esc_url( $iawmlf_link_table ); ?>" class="iawmlf_dashboard-stats-number iawmlf_dashboard-stats-link">
137-
<?php echo esc_html( $iawmlf_total_links_count ); ?>
137+
<?php echo esc_html( $iawmlf_total_link_count ); ?>
138138
</a>
139139
<div class="iawmlf_dashboard-stats-label"><?php esc_html_e( 'Total Links', 'internet-archive-wayback-machine-link-fixer' ); ?></div>
140140
</div>
@@ -179,9 +179,6 @@
179179
// Set up variables for the widget (it expects $iawmlf_details, not $iawmlf_account_details)
180180
$iawmlf_details = $iawmlf_account_details;
181181

182-
// Create a total links array for the widget (pass the actual count)
183-
$iawmlf_total_links = array_fill( 0, $iawmlf_total_links_count, null ); // Mock array with correct count
184-
185182
// Include the existing widget template
186183
require __DIR__ . '/widget.php';
187184
?>

templates/admin/dashboard/widget.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,13 @@
99
* @param bool $iawmlf_is_online Whether the Archive.org API is online.
1010
* @param string $iawmlf_link_to_settings URL to the settings page.
1111
* @param string $iawmlf_link_table URL to the links report page.
12-
* @param array $iawmlf_total_links Array of all links in the system.
12+
* @param int $iawmlf_total_link_count Total number of links in the system.
1313
* @param bool $iawmlf_auto_archiver_enabled Whether auto archiver is enabled.
1414
* @param bool $iawmlf_scan_existing_enabled Whether scanning existing posts is enabled.
1515
* @param bool $iawmlf_link_processing_enabled Whether link processing is enabled.
1616
* @param int $iawmlf_link_check_duration Number of days between link checks.
1717
* @param int $iawmlf_failed_check_count Number of failed checks before marking as broken.
1818
* @param array $iawmlf_onboarding_details Array containing onboarding status and details.
19-
* @param array $iawmlf_link_stats Array containing link statistics including total_links count.
2019
*/
2120

2221
use Internet_Archive\Wayback_Machine_Link_Fixer\Dashboard\Dashboard_Page;
@@ -32,7 +31,7 @@
3231
'admin/dashboard/onboarding.php',
3332
array(
3433
'iawmlf_onboarding_details' => $iawmlf_onboarding_details,
35-
'iawmlf_total_links_count' => $iawmlf_link_stats['total_links'],
34+
'iawmlf_total_links_count' => $iawmlf_total_link_count,
3635
'iawmlf_link_table' => \Internet_Archive\Wayback_Machine_Link_Fixer\Dashboard\Report_Page::get_page_url(),
3736
)
3837
);
@@ -164,7 +163,7 @@
164163
printf(
165164
/* translators: %d: number of links */
166165
esc_html__( 'View Links (%d)', 'internet-archive-wayback-machine-link-fixer' ),
167-
count( $iawmlf_total_links )
166+
absint( $iawmlf_total_link_count )
168167
);
169168
?>
170169
</a>

0 commit comments

Comments
 (0)