Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .distignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ coverage-report
.git
.github
.gitignore
index.php
node_modules
models
package.json
Expand Down
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,26 @@ add_filter( 'iawmlf_dashboard_link_count', function( int $link_count ): int {
});
```

#### `iawmlf_dashboard_link_stats_cache_expiry`

This is used to define how long dashboard link statistics are cached for. The default is 120 seconds (2 minutes).

```php
add_filter( 'iawmlf_dashboard_link_stats_cache_expiry', function( int $cache_expiry ): int {
return 5 * \MINUTE_IN_SECONDS; // Cache for 5 minutes
});
```

#### `iawmlf_dashboard_onboarding_stats_cache_expiry`

This is used to define how long dashboard onboarding statistics are cached for. The default is 120 seconds (2 minutes).

```php
add_filter( 'iawmlf_dashboard_onboarding_stats_cache_expiry', function( int $cache_expiry ): int {
return 5 * \MINUTE_IN_SECONDS; // Cache for 5 minutes
});
```

#### `iawmlf_menu_icon_base64`

This is used to override the base64 encoded PNG for the admin menu item icon. This allows you to customize the plugin's menu icon.
Expand Down
15 changes: 15 additions & 0 deletions assets/css/src/admin/_wizard.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
// If onboarding is active.
body.iawmlf-onboarding-active #wpbody-content {
position: fixed;
left: 0;
z-index: 9999;
background: #f1f1f1;
height: 100%;
width: 100%;
padding: 0 32px;
.wrap>h1 {
text-align: center;
margin-bottom: 25px;
}
}

#iawmlf_wizard {
padding: 20px 30px;
background: white;
Expand Down
11 changes: 11 additions & 0 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,18 @@ function iawmlf_get_plugin_instance(): Plugin {
* @return void
*/
function iawmlf_activate(): void {

// Run migrations.
Migrations::up();

// If already marked as completed, do nothing.
if ( Settings::ONBOARDING_COMPLETED_OPTION === Settings::get_onboarding_status( Settings::ONBOARDING_PENDING_OPTION )
|| Settings::is_wizard_completed() ) {
return;
}

// Set the onboarding status to pending.
Settings::set_onboarding_status( Settings::ONBOARDING_PENDING_OPTION );
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/Dashboard/Dashboard_Notifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ public function render_widget(): void {
'iawmlf_link_processing_enabled' => Settings::is_link_processing_enabled(),
'iawmlf_link_check_duration' => Settings::get_link_check_duration(),
'iawmlf_failed_check_count' => Settings::get_failed_count(),
'iawmlf_onboarding_details' => Dashboard_Statistics::get_onboarding_statistics(),
'iawmlf_link_stats' => Dashboard_Statistics::get_link_statistics(),
)
);
}
Expand Down
143 changes: 22 additions & 121 deletions src/Dashboard/Dashboard_Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,25 @@ public function initialize(): void {
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) );
}

/**
* Checks if this page is the current page.
*
* @return boolean
*/
public static function is_current_page(): bool {
$screen = get_current_screen();
return $screen && 'toplevel_page_' . self::DASHBOARD_SLUG === $screen->id;
}

/**
* Gets the page URL.
*
* @return string
*/
public static function get_page_url(): string {
return admin_url( 'admin.php?page=' . self::DASHBOARD_SLUG );
}


/**
* Enqueue dashboard assets (styles and scripts).
Expand Down Expand Up @@ -148,132 +167,13 @@ public function rename_first_submenu_item(): void {
}
}

/**
* Get the statistics for display on the dashboard.
*
* @return array{
* total_links: int<0, max>,
* broken_links: int<0, max>,
* links_with_archive: int<0, max>,
* links_without_archive: int<0, max>,
* not_checked: int<0, max>,
* process_done: int<0, max>,
* process_new: int<0, max>,
* process_pending: int<0, max>,
* last_checks: list<LastCheck>
* }
*/
private function get_statistics(): array {
// Attempt to get from the transient.
$stats = get_transient( self::STATS_TRANSIENT_KEY );

if ( false === $stats || ! is_array( $stats ) ) {
$stats = $this->compile_statistics();

// Store for 2 minutes.
set_transient( self::STATS_TRANSIENT_KEY, $stats, 2 * MINUTE_IN_SECONDS );
}

return $stats;
}

/**
* Compile the statistics.
*
* @return array{
* total_links: int<0, max>,
* broken_links: int<0, max>,
* links_with_archive: int<0, max>,
* links_without_archive: int<0, max>,
* not_checked: int<0, max>,
* process_done: int<0, max>,
* process_new: int<0, max>,
* process_pending: int<0, max>,
* }
*/
private function compile_statistics(): array {
$all_links = $this->link_repository->query_links( \PHP_INT_MAX, 1, array(), array(), array(), Link_Repository::ORDER_DATE_DESC, null, null, null );

// Get all the links stats.
$all_broken = array();
$redirected_broken = array();
$has_archive_link = array();
$not_checked = array();
$process_done = array();
$process_new = array();
$process_pending = array();
$last_checks = array();

// Loop through all links to gather stats.
foreach ( $all_links as $link ) {
if ( $link->is_broken() && ! $link->is_excluded() ) {
$all_broken[] = $link->get_id();
}

if ( $link->is_broken() && $link->has_archived_href() && ! $link->is_excluded() ) {
$redirected_broken[] = $link->get_id();
}

if ( $link->has_archived_href() ) {
$has_archive_link[] = $link->get_id();
}

if ( null === $link->get_last_check() ) {
$not_checked[] = $link->get_id();
} else {
$last = $link->get_last_check();
$last_checks[] = array(
'id' => $link->get_id(),
'last_check' => $last,
);
}

switch ( $link->get_archive_process() ) {
case Link::PROCESS_NEW:
$process_new[] = $link->get_id();
break;
case Link::PROCESS_PENDING:
$process_pending[] = $link->get_id();
break;
default:
$process_done[] = $link->get_id();
break;
}
}

// Sort the last checks by date desc.
usort(
$last_checks,
function ( $a, $b ) {
return strtotime( $b['last_check']['date'] ) <=> strtotime( $a['last_check']['date'] );
}
);

$stats = array(
'total_links' => count( $all_links ),
'all_broken_links' => count( $all_broken ),
'broken_and_redirected_links' => count( $redirected_broken ),
'broken_not_redirected_links' => count( $all_broken ) - count( $redirected_broken ),
'links_with_archive' => count( $has_archive_link ),
'links_without_archive' => count( $all_links ) - count( $has_archive_link ),
'not_checked' => count( $not_checked ),
'process_done' => count( $process_done ),
'process_new' => count( $process_new ),
'process_pending' => count( $process_pending ),
'last_checks' => array_slice( $last_checks, 0, $this->links_per_section ),
);

return $stats;
}


/**
* Render the dashboard page.
*
* @return void
*/
public function render_page(): void {
$link_stats = $this->get_statistics();
$link_stats = Dashboard_Statistics::get_link_statistics();

$last_checks = array_map(
function ( $check ) {
Expand Down Expand Up @@ -346,7 +246,7 @@ function ( $link ) {
iawmlf_render_template(
'admin/dashboard/page.php',
array(
'iawmlf_link_stats' => $link_stats,
'iawmlf_link_stats' => Dashboard_Statistics::get_link_statistics(),
'iawmlf_last_checks' => $last_checks,
'iawmlf_latest_links' => $latest_links,
'iawmlf_account_details' => Dashboard_Notifications::get_account_details(),
Expand All @@ -365,6 +265,7 @@ function ( $link ) {
'iawmlf_filtered_valid' => esc_url( $valid_link ),
'iawmlf_filtered_has_archive' => esc_url( $has_archive_link ),
'iawmlf_filtered_no_archive' => esc_url( $has_no_archive_link ),
'iawmlf_onboarding_details' => Dashboard_Statistics::get_onboarding_statistics(),
)
);
}
Expand Down
Loading