id || ! current_user_can( self::CAP ) || ! get_option( self::WELCOME ) ) {
return;
}
$dismiss = wp_nonce_url( admin_url( 'admin-post.php?action=alexibility_fixes_dismiss' ), 'alexibility_fixes_dismiss' );
echo '
';
}
public static function dismiss() {
if ( ! current_user_can( self::CAP ) ) {
wp_die( esc_html__( 'You are not allowed to do this.', 'alexibility-fixes' ), 403 );
}
check_admin_referer( 'alexibility_fixes_dismiss' );
delete_option( self::WELCOME );
wp_safe_redirect( admin_url( 'plugins.php' ) );
exit;
}
/** On the plugin's own page only: the badge's CSS, for the preview in Settings. */
public static function enqueue_styles( $hook_suffix ) {
if ( 'tools_page_' . self::SLUG !== $hook_suffix ) {
return;
}
Alexibility_Render::inline_style(
'alexibility-fixes-admin',
Alexibility_Render::badge_css() . '.alexibility-fixes-badge-preview{max-width:420px;margin-top:8px;background:#f0f0f1;border-radius:4px}'
);
}
public static function menu() {
add_management_page(
__( 'Accessibility Fixes', 'alexibility-fixes' ),
__( 'Accessibility Fixes', 'alexibility-fixes' ),
self::CAP,
self::SLUG,
array( __CLASS__, 'render' )
);
}
private static function page_url() {
return admin_url( 'tools.php?page=' . self::SLUG );
}
private static function notice( $type, $message ) {
set_transient( 'alexibility_fixes_notice_' . get_current_user_id(), array( 'type' => $type, 'message' => $message ), 60 );
}
/** One handler for every form on the page; each action has its own nonce. */
public static function handle() {
$do = isset( $_POST['do'] ) ? sanitize_key( wp_unslash( $_POST['do'] ) ) : '';
if ( ! current_user_can( self::CAP ) ) {
wp_die( esc_html__( 'You are not allowed to do this.', 'alexibility-fixes' ), 403 );
}
check_admin_referer( 'alexibility_fixes_' . $do );
switch ( $do ) {
case 'upload':
$file = $_FILES['plan'] ?? null; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput -- validated below.
if ( ! $file || UPLOAD_ERR_OK !== $file['error'] || ! is_uploaded_file( $file['tmp_name'] ) || $file['size'] > 5 * MB_IN_BYTES ) {
self::notice( 'error', __( 'Choose the fixes.json file to upload (5 MB at most).', 'alexibility-fixes' ) );
break;
}
// Read in place; the file is never stored in uploads where it could be fetched.
$plan = Alexibility_Plan::from_json( (string) file_get_contents( $file['tmp_name'] ) ); // phpcs:ignore WordPress.WP.AlternativeFunctions
if ( is_wp_error( $plan ) ) {
self::notice( 'error', $plan->get_error_message() );
break;
}
Alexibility_Plan::save( $plan );
/* translators: %d: number of fixes */
self::notice( 'success', sprintf( _n( 'Loaded %d approved fix. Review it below, then apply.', 'Loaded %d approved fixes. Review them below, then apply.', count( $plan['fixes'] ), 'alexibility-fixes' ), count( $plan['fixes'] ) ) );
break;
case 'apply':
$run = Alexibility_Content::apply( Alexibility_Plan::approved() );
update_option( Alexibility_Render::ENABLED, true );
$message = sprintf(
/* translators: %d: number of changes */
_n( 'Applied: %d content change saved, and theme-level fixes are on.', 'Applied: %d content changes saved, and theme-level fixes are on.', count( $run['changes'] ), 'alexibility-fixes' ),
count( $run['changes'] )
);
if ( $run['failed'] ) {
/* translators: %s: list of problems */
$message .= ' ' . sprintf( __( 'Not saved: %s.', 'alexibility-fixes' ), implode( '; ', array_map( function ( $id, $why ) {
return get_the_title( $id ) . ' (' . $why . ')';
}, array_keys( $run['failed'] ), $run['failed'] ) ) );
}
self::notice( $run['failed'] ? 'warning' : 'success', $message );
break;
case 'rollback':
$result = Alexibility_Content::rollback( isset( $_POST['run'] ) ? sanitize_text_field( wp_unslash( $_POST['run'] ) ) : '' );
if ( is_wp_error( $result ) ) {
self::notice( 'error', $result->get_error_message() );
break;
}
update_option( Alexibility_Render::ENABLED, false );
/* translators: %d: number of changes restored */
$message = sprintf( __( 'Rolled back %d change(s) and switched theme-level fixes off.', 'alexibility-fixes' ), $result['restored'] );
if ( $result['skipped'] ) {
/* translators: %s: list of items */
$message .= ' ' . sprintf( __( 'Left alone because they changed since: %s.', 'alexibility-fixes' ), implode( ', ', $result['skipped'] ) );
}
self::notice( $result['skipped'] ? 'warning' : 'success', $message );
break;
case 'pdf-rollback':
$result = Alexibility_Pdfs::rollback( isset( $_POST['run'] ) ? sanitize_text_field( wp_unslash( $_POST['run'] ) ) : '' );
if ( is_wp_error( $result ) ) {
self::notice( 'error', $result->get_error_message() );
break;
}
/* translators: %d: number of PDF files restored */
$message = sprintf( _n( 'Put back %d original PDF.', 'Put back %d original PDFs.', $result['restored'], 'alexibility-fixes' ), $result['restored'] );
if ( $result['skipped'] ) {
/* translators: %s: list of files */
$message .= ' ' . sprintf( __( 'Left alone: %s.', 'alexibility-fixes' ), implode( ', ', $result['skipped'] ) );
}
self::notice( $result['skipped'] ? 'warning' : 'success', $message );
break;
case 'settings':
update_option( Alexibility_Render::ENABLED, ! empty( $_POST['render'] ) );
// '0'/'1' rather than false/true: update_option() skips saving false when the option doesn't exist
// yet, so a string always records the site's own choice.
update_option( Alexibility_Render::CREDIT, empty( $_POST['credit'] ) ? '0' : '1' );
update_option( Alexibility_Render::CREDIT_URL, isset( $_POST['credit_url'] ) ? esc_url_raw( wp_unslash( $_POST['credit_url'] ) ) : '' );
self::notice( 'success', __( 'Settings saved.', 'alexibility-fixes' ) );
break;
}
wp_safe_redirect( self::page_url() );
exit;
}
private static function form_open( $do ) {
echo '