';
$this->header( __( 'Make your statement', 'easyable' ), __( 'One page that shows visitors you take accessibility seriously.', 'easyable' ) );
$this->flag_notice();
?>
';
}
/**
* Settings screen.
*/
public function screen_settings() {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
$settings = get_option( 'accscan_settings', array() );
$types = isset( $settings['post_types'] ) ? (array) $settings['post_types'] : array( 'page', 'post' );
$level = isset( $settings['wcag_level'] ) ? $settings['wcag_level'] : 'wcag21aa';
$schedule = isset( $settings['schedule'] ) ? $settings['schedule'] : 'off';
$public = get_post_types( array( 'public' => true ), 'objects' );
echo '
';
$this->header( __( 'Settings', 'easyable' ), __( 'Choose what to check. Everything stays on your own server.', 'easyable' ) );
$this->flag_notice();
?>
';
}
/* ------------------------------------------------------------ AJAX */
/**
* Capability + nonce gate for AJAX handlers.
*/
private function ajax_gate() {
if ( ! current_user_can( 'manage_options' ) ) {
wp_send_json_error( array( 'msg' => 'forbidden' ), 403 );
}
check_ajax_referer( 'accscan_nonce', 'nonce' );
}
/**
* Start a scan: create the scan record.
*/
public function ajax_start_scan() {
$this->ajax_gate();
global $wpdb;
// phpcs:ignore WordPress.DB.DirectDatabaseQuery
$wpdb->insert(
$wpdb->prefix . 'accscan_scans',
array( 'started_at' => current_time( 'mysql' ), 'status' => 'running' ),
array( '%s', '%s' )
);
wp_send_json_success( array( 'scan_id' => (int) $wpdb->insert_id ) );
}
/**
* Return the URL list for the runner.
*/
public function ajax_get_urls() {
$this->ajax_gate();
wp_send_json_success( array( 'urls' => AccScan_Core::collect_urls() ) );
}
/**
* Save one batch of page results.
*/
public function ajax_save_results() {
$this->ajax_gate();
global $wpdb;
$scan_id = isset( $_POST['scan_id'] ) ? (int) $_POST['scan_id'] : 0; // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Nonce verified in ajax_gate() above via check_ajax_referer().
$pages = isset( $_POST['pages'] ) ? json_decode( wp_unslash( $_POST['pages'] ), true ) : null; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput, WordPress.Security.NonceVerification.Missing -- Nonce verified in ajax_gate() above; decoded as data, each field sanitized below.
if ( ! $scan_id || ! is_array( $pages ) ) {
wp_send_json_error( array( 'msg' => 'bad request' ), 400 );
}
$table = $wpdb->prefix . 'accscan_issues';
$saved = 0;
foreach ( $pages as $page ) {
$url = isset( $page['url'] ) ? esc_url_raw( $page['url'] ) : '';
$title = isset( $page['title'] ) ? sanitize_text_field( $page['title'] ) : '';
$type = isset( $page['type'] ) ? sanitize_key( $page['type'] ) : '';
foreach ( isset( $page['violations'] ) && is_array( $page['violations'] ) ? $page['violations'] : array() as $v ) {
$rule = isset( $v['rule'] ) ? sanitize_key( $v['rule'] ) : '';
$target = isset( $v['target'] ) ? sanitize_text_field( substr( (string) $v['target'], 0, 480 ) ) : '';
if ( ! $rule ) {
continue;
}
// phpcs:ignore WordPress.DB.DirectDatabaseQuery
$wpdb->insert(
$table,
array(
'scan_id' => $scan_id,
'rule_id' => $rule,
'impact' => isset( $v['impact'] ) ? sanitize_key( $v['impact'] ) : 'moderate',
'page_url' => $url,
'page_title' => $title,
'post_type' => $type,
'target' => $target,
'html_snippet' => isset( $v['html'] ) ? sanitize_text_field( substr( (string) $v['html'], 0, 300 ) ) : '',
'failure_summary' => isset( $v['summary'] ) ? sanitize_textarea_field( substr( (string) $v['summary'], 0, 500 ) ) : '',
'signature' => AccScan_Core::signature( $rule, $url, $target ),
),
array( '%d', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s' )
);
$saved++;
}
}
// phpcs:ignore WordPress.DB.DirectDatabaseQuery
$wpdb->query( $wpdb->prepare( 'UPDATE ' . $wpdb->prefix . 'accscan_scans SET urls_done = urls_done + %d WHERE id = %d', count( $pages ), $scan_id ) );
wp_send_json_success( array( 'saved' => $saved ) );
}
/**
* Finish a scan: compute score + counts and close the record.
*/
public function ajax_finish_scan() {
$this->ajax_gate();
global $wpdb;
$scan_id = isset( $_POST['scan_id'] ) ? (int) $_POST['scan_id'] : 0; // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Nonce verified in ajax_gate() above via check_ajax_referer().
$urls_count = isset( $_POST['urls_count'] ) ? (int) $_POST['urls_count'] : 0; // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Nonce verified in ajax_gate() above via check_ajax_referer().
$issues_table = $wpdb->prefix . 'accscan_issues';
// phpcs:ignore WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter -- Table name is built from $wpdb->prefix only; the scan id value is prepared.
$rows = $wpdb->get_results( $wpdb->prepare( "SELECT impact, signature FROM $issues_table WHERE scan_id = %d", $scan_id ), ARRAY_A );
$ignored = AccScan_Core::ignored_signatures();
$active = array();
foreach ( (array) $rows as $row ) {
if ( ! in_array( $row['signature'], $ignored, true ) ) {
$active[] = $row;
}
}
$score = AccScan_Core::compute_score( $active );
$counts = AccScan_Core::bucket_counts( $active );
// phpcs:ignore WordPress.DB.DirectDatabaseQuery
$wpdb->update(
$wpdb->prefix . 'accscan_scans',
array(
'finished_at' => current_time( 'mysql' ),
'status' => 'complete',
'urls_count' => $urls_count,
'score' => $score,
'counts' => wp_json_encode( $counts ),
),
array( 'id' => $scan_id ),
array( '%s', '%s', '%d', '%d', '%s' ),
array( '%d' )
);
wp_send_json_success( array( 'score' => $score, 'counts' => $counts ) );
}
/* ---------------------------------------------------- form actions */
/**
* CSV export of the latest scan.
*/
public function handle_export() {
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( esc_html__( 'You are not allowed to do this.', 'easyable' ) );
}
check_admin_referer( 'accscan_export' );
$results = AccScan_Core::latest_results();
nocache_headers();
header( 'Content-Type: text/csv; charset=utf-8' );
header( 'Content-Disposition: attachment; filename=accessibility-problems.csv' );
// The CSV is built as a string and echoed: no direct filesystem
// calls (fopen/fputcsv/fclose) are needed for a download.
echo $this->csv_line( array( 'Problem', 'How urgent', 'Page', 'Page URL', 'Element', 'Why it matters', 'How to fix' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- CSV download, fields escaped by csv_line().
foreach ( $results['issues'] as $issue ) {
if ( ! empty( $issue['ignored'] ) ) {
continue;
}
$guide = AccScan_Guidance::for_rule( $issue['rule_id'] );
$bucket = AccScan_Core::bucket_for_impact( $issue['impact'] );
$row = $this->csv_line(
array(
$guide['title'],
$bucket['label'],
$issue['page_title'],
$issue['page_url'],
$issue['target'],
$guide['why'],
implode( ' ', $guide['steps'] ),
)
);
echo $row; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- CSV download; the row is built and escaped by csv_line().
}
exit;
}
/**
* Escape and join one CSV row (RFC 4180 quoting).
*
* @param string[] $fields Row fields.
* @return string
*/
private function csv_line( $fields ) {
$cells = array();
foreach ( (array) $fields as $field ) {
$field = (string) $field;
if ( preg_match( '/[",\r\n ]/', $field ) ) {
$field = '"' . str_replace( '"', '""', $field ) . '"';
}
$cells[] = $field;
}
return implode( ',', $cells ) . "\r\n";
}
/**
* Ignore / unignore all instances of one rule from the latest scan.
*/
public function handle_ignore() {
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( esc_html__( 'You are not allowed to do this.', 'easyable' ) );
}
check_admin_referer( 'accscan_ignore' );
$mode = isset( $_POST['mode'] ) ? sanitize_key( wp_unslash( $_POST['mode'] ) ) : '';
$rule = isset( $_POST['rule'] ) ? sanitize_key( wp_unslash( $_POST['rule'] ) ) : '';
if ( $rule ) {
$results = AccScan_Core::latest_results();
$ignored = AccScan_Core::ignored_signatures();
$sigs = array();
foreach ( $results['issues'] as $issue ) {
if ( $issue['rule_id'] === $rule ) {
$sigs[] = $issue['signature'];
}
}
if ( 'ignore-rule' === $mode ) {
$ignored = array_values( array_unique( array_merge( $ignored, $sigs ) ) );
} else {
$ignored = array_values( array_diff( $ignored, $sigs ) );
}
update_option( 'accscan_ignored', $ignored );
}
wp_safe_redirect( admin_url( 'admin.php?page=accscan-problems' ) );
exit;
}
/**
* Create the statement draft page.
*/
public function handle_statement() {
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( esc_html__( 'You are not allowed to do this.', 'easyable' ) );
}
check_admin_referer( 'accscan_statement' );
$org = isset( $_POST['org'] ) ? sanitize_text_field( wp_unslash( $_POST['org'] ) ) : '';
$email = isset( $_POST['email'] ) ? sanitize_email( wp_unslash( $_POST['email'] ) ) : '';
$level = isset( $_POST['level'] ) ? sanitize_text_field( wp_unslash( $_POST['level'] ) ) : 'WCAG 2.1 AA';
if ( ! in_array( $level, array( 'WCAG 2.1 AA', 'WCAG 2.2 AA' ), true ) ) {
$level = 'WCAG 2.1 AA';
}
$results = AccScan_Core::latest_results();
$known = array();
foreach ( $results['issues'] as $issue ) {
if ( empty( $issue['ignored'] ) ) {
$guide = AccScan_Guidance::for_rule( $issue['rule_id'] );
$known[] = $guide['title'];
}
}
$data = array(
'org' => $org,
'email' => $email,
'level' => $level,
'date' => date_i18n( get_option( 'date_format' ) ),
'known_issues' => $known,
);
$result = AccScan_Statement::create_page( $data );
if ( ! is_wp_error( $result ) ) {
$saved = get_option( 'accscan_statement', array() );
$saved['org'] = $org;
$saved['email'] = $email;
$saved['level'] = $level;
update_option( 'accscan_statement', $saved );
}
wp_safe_redirect( admin_url( 'admin.php?page=accscan-statement&accscan_msg=statement' ) );
exit;
}
/**
* Save settings.
*/
public function handle_settings() {
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( esc_html__( 'You are not allowed to do this.', 'easyable' ) );
}
check_admin_referer( 'accscan_settings' );
$types = isset( $_POST['post_types'] ) ? array_map( 'sanitize_key', (array) wp_unslash( $_POST['post_types'] ) ) : array(); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput
$types = array_filter( $types, 'post_type_exists' );
$level = isset( $_POST['wcag_level'] ) ? sanitize_key( wp_unslash( $_POST['wcag_level'] ) ) : 'wcag21aa';
if ( ! in_array( $level, array( 'wcag21aa', 'wcag22aa' ), true ) ) {
$level = 'wcag21aa';
}
$schedule = isset( $_POST['schedule'] ) ? sanitize_key( wp_unslash( $_POST['schedule'] ) ) : 'off';
if ( ! in_array( $schedule, array( 'off', 'weekly' ), true ) ) {
$schedule = 'off';
}
update_option(
'accscan_settings',
array(
'post_types' => array_values( $types ),
'wcag_level' => $level,
'schedule' => $schedule,
)
);
accscan_sync_schedule();
wp_safe_redirect( admin_url( 'admin.php?page=accscan-settings&accscan_msg=settings' ) );
exit;
}
}