' . esc_html__( 'Journal', 'admintrace-activity-logger' ) . '' );
}
return $links;
}
/**
* First-run notice.
*/
public static function welcome() {
if ( ! current_user_can( Capabilities::VIEW ) ) {
return;
}
$user_id = get_current_user_id();
if ( get_user_meta( $user_id, 'srah_dismiss_welcome', true ) ) {
return;
}
if ( ! get_transient( 'srah_welcome_' . $user_id ) ) {
return;
}
$url = wp_nonce_url( admin_url( 'admin-post.php?action=srah_dismiss_welcome' ), 'srah_dismiss_welcome' );
echo '
';
}
/**
* Dismiss the welcome notice.
*/
public static function dismiss_welcome() {
self::authorize( Capabilities::VIEW, 'srah_dismiss_welcome' );
update_user_meta( get_current_user_id(), 'srah_dismiss_welcome', 1 );
delete_transient( 'srah_welcome_' . get_current_user_id() );
wp_safe_redirect( admin_url() );
exit;
}
/**
* Save settings.
*/
public static function save_settings() {
self::authorize( Capabilities::MANAGE, 'srah_save_settings' );
$before = Settings::all();
$clean = Settings::sanitize( Settings_Screen::input_from_request(), $before );
Settings::update( $clean );
// Saving another section must not push the next digest back.
if ( $before['email_digest'] !== $clean['email_digest'] ) {
Notifications::reschedule();
} else {
Notifications::ensure_scheduled();
}
$changed = array();
foreach ( $clean as $key => $value ) {
if ( in_array( $key, array( 'feed_token', 'webhook_url' ), true ) ) {
if ( (string) $before[ $key ] !== (string) $value ) {
$changed[] = $key;
}
continue;
}
if ( $before[ $key ] !== $value ) {
$changed[] = $key;
}
}
Recorder::log(
array(
'summary' => __( 'Updated AdminTrace Activity Logger settings.', 'admintrace-activity-logger' ),
'category' => 'settings',
'action' => 'settings.srah',
'severity' => 'notice',
'logger' => 'settings',
'context' => array( 'changed' => $changed ),
)
);
Notices::redirect( 'success', __( 'Settings saved.', 'admintrace-activity-logger' ), 'admintrace-activity-logger-settings' );
}
/**
* Download the current filtered journal.
*/
public static function export() {
self::authorize( Capabilities::EXPORT, 'srah_export' );
$filters = Timeline_Screen::filters();
$args = Access::constrain( Timeline_Screen::query_args( $filters ) );
$format = isset( $_REQUEST['format'] ) ? sanitize_key( wp_unslash( $_REQUEST['format'] ) ) : 'csv'; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- authorize() verified the nonce.
Export::send( $format, $args );
}
/**
* Delete selected events.
*/
public static function delete_selected() {
self::authorize( Capabilities::DELETE, 'srah_delete' );
// phpcs:ignore WordPress.Security.NonceVerification.Missing -- authorize() verified the nonce.
$raw = isset( $_POST['event_ids'] ) ? array_map( 'sanitize_text_field', (array) wp_unslash( $_POST['event_ids'] ) ) : array();
// A combined card posts its row IDs as one comma-separated value.
$deleted = Retention::delete_ids( wp_parse_id_list( implode( ',', $raw ) ) );
if ( ! $deleted ) {
Notices::redirect( 'warning', __( 'Select at least one event to delete.', 'admintrace-activity-logger' ) );
}
Notices::redirect(
'success',
sprintf(
/* translators: %d: number of deleted events. */
_n( 'Deleted %d event.', 'Deleted %d events.', $deleted, 'admintrace-activity-logger' ),
$deleted
)
);
}
/**
* Delete every event on this site.
*/
public static function delete_all() {
self::authorize( Capabilities::DELETE, 'srah_delete_all' );
if ( empty( $_POST['confirm_delete_all'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing -- authorize() verified the nonce.
Notices::redirect( 'error', __( 'Confirm deletion before clearing the journal.', 'admintrace-activity-logger' ), 'admintrace-activity-logger-settings' );
}
$deleted = Retention::delete_all();
Notices::redirect(
'success',
sprintf(
/* translators: %d: number of deleted events. */
__( 'Deleted %d events.', 'admintrace-activity-logger' ),
$deleted
),
'admintrace-activity-logger-settings'
);
}
/**
* Run retention immediately.
*/
public static function prune() {
self::authorize( Capabilities::DELETE, 'srah_prune' );
$deleted = Retention::prune();
Notices::redirect(
'success',
sprintf(
/* translators: %d: number of deleted events. */
__( 'Removed %d events older than the retention period.', 'admintrace-activity-logger' ),
$deleted
),
'admintrace-activity-logger-settings'
);
}
/**
* Save a manual note.
*/
public static function note() {
self::authorize( Capabilities::MANAGE, 'srah_note' );
// phpcs:disable WordPress.Security.NonceVerification.Missing -- authorize() verified the nonce.
$summary = isset( $_POST['summary'] ) ? sanitize_textarea_field( wp_unslash( $_POST['summary'] ) ) : '';
$severity = isset( $_POST['severity'] ) ? sanitize_key( wp_unslash( $_POST['severity'] ) ) : 'info';
// phpcs:enable WordPress.Security.NonceVerification.Missing
if ( ! isset( Vocabulary::severities()[ $severity ] ) ) {
$severity = 'info';
}
$id = Recorder::log(
array(
'summary' => $summary,
'category' => 'journal',
'action' => 'journal.note',
'severity' => $severity,
'logger' => 'journal',
)
);
if ( ! $id ) {
Notices::redirect( 'error', __( 'The note could not be saved.', 'admintrace-activity-logger' ) );
}
Notices::redirect( 'success', __( 'Note added to the journal.', 'admintrace-activity-logger' ) );
}
/**
* Send the digest to its recipient now.
*/
public static function test_email() {
self::authorize( Capabilities::MANAGE, 'srah_test_email' );
$settings = Settings::all();
$previous = $settings['email_digest'];
if ( 'off' === $previous ) {
$settings['email_digest'] = 'weekly';
Settings::update( $settings );
}
try {
$sent = Notifications::send_digest();
} finally {
if ( 'off' === $previous ) {
$settings['email_digest'] = 'off';
Settings::update( $settings );
}
}
$to = Notifications::recipient( Settings::all()['digest_email'] );
if ( ! $sent ) {
Notices::redirect( 'error', __( 'The test email could not be sent.', 'admintrace-activity-logger' ), 'admintrace-activity-logger-settings' );
}
Notices::redirect(
'success',
sprintf(
/* translators: %s: email address. */
__( 'Test digest sent to %s.', 'admintrace-activity-logger' ),
$to
),
'admintrace-activity-logger-settings'
);
}
/**
* Check capability and nonce.
*
* @param string $capability Capability.
* @param string $action Nonce action.
*/
private static function authorize( $capability, $action ) {
if ( ! current_user_can( $capability ) ) {
wp_die( esc_html__( 'Sorry, you are not allowed to do that.', 'admintrace-activity-logger' ), 403 );
}
check_admin_referer( $action );
}
}