=' ) && scanpoof_service_enabled() && function_exists( 'wp_register_ability' ) && class_exists( 'WP_Abilities_Registry' ) && (bool) apply_filters( 'scanpoof_register_abilities', true ); } function scanpoof_register_ability_category() { if ( ! scanpoof_abilities_enabled() || ! function_exists( 'wp_register_ability_category' ) ) { return; } wp_register_ability_category( 'scanpoof', array( 'label' => __( 'Scanpoof accessibility', 'scanpoof' ), 'description' => __( 'Read this site’s accessibility results from Scanpoof and ask for a new scan.', 'scanpoof' ), ) ); } /** Who may use these: an administrator, on a site that is connected to Scanpoof. */ function scanpoof_ability_permission() { if ( ! current_user_can( 'manage_options' ) ) { return false; } // Not connected to Scanpoof: refuse plainly. (WordPress hides the reason of a permission error on purpose, and // logs a notice if a callback returns one, so this is a simple "no".) return scanpoof_is_activated(); } function scanpoof_ability_meta( $readonly, $idempotent ) { return array( 'annotations' => array( 'readonly' => $readonly, 'destructive' => false, 'idempotent' => $idempotent, ), 'public' => true, 'show_in_rest' => true, 'mcp' => array( 'public' => true ), // for the WordPress MCP adapter ); } function scanpoof_register_abilities() { if ( ! scanpoof_abilities_enabled() ) { return; } $base = array( 'category' => 'scanpoof', 'permission_callback' => 'scanpoof_ability_permission', ); wp_register_ability( 'scanpoof/accessibility-summary', array_merge( $base, array( 'label' => __( 'Scanpoof: accessibility summary', 'scanpoof' ), 'description' => __( 'Returns this site’s latest accessibility score, how many issues there are of each severity, and the most common issues. Read-only.', 'scanpoof' ), 'execute_callback' => 'scanpoof_ability_summary', 'output_schema' => array( 'type' => 'object', 'properties' => array( 'site' => array( 'type' => 'string' ), 'score' => array( 'type' => array( 'integer', 'null' ) ), 'scannedAt' => array( 'type' => array( 'string', 'null' ) ), 'issues' => array( 'type' => 'object' ), 'topIssues' => array( 'type' => 'array' ), ), ), 'meta' => scanpoof_ability_meta( true, true ), ) ) ); wp_register_ability( 'scanpoof/request-rescan', array_merge( $base, array( 'label' => __( 'Scanpoof: scan this site again', 'scanpoof' ), 'description' => __( 'Starts a fresh accessibility scan of this site. Results appear in the summary when it finishes, usually within a few minutes.', 'scanpoof' ), 'execute_callback' => 'scanpoof_ability_rescan', 'output_schema' => array( 'type' => 'object', 'properties' => array( 'queued' => array( 'type' => 'boolean' ) ), ), 'meta' => scanpoof_ability_meta( false, false ), ) ) ); } /* ---- what each ability does ---- */ /** Plain text from the service, on one line and clipped. */ function scanpoof_ability_text( $value, $max ) { $value = trim( preg_replace( '/\s+/', ' ', sanitize_text_field( (string) $value ) ) ); return function_exists( 'mb_substr' ) ? mb_substr( $value, 0, $max ) : substr( $value, 0, $max ); } function scanpoof_ability_summary( $input = null ) { $ov = scanpoof_fetch_overview(); if ( is_wp_error( $ov ) ) { return $ov; } $counts = ( isset( $ov['counts'] ) && is_array( $ov['counts'] ) ) ? $ov['counts'] : array(); $top = array(); foreach ( array_slice( ( isset( $ov['topIssues'] ) && is_array( $ov['topIssues'] ) ) ? $ov['topIssues'] : array(), 0, 10 ) as $i ) { $top[] = array( 'name' => scanpoof_ability_text( isset( $i['name'] ) ? $i['name'] : '', 120 ), 'impact' => sanitize_key( isset( $i['impact'] ) ? (string) $i['impact'] : '' ), 'occurrences' => (int) ( isset( $i['occurrences'] ) ? $i['occurrences'] : 0 ), 'wcag' => scanpoof_ability_text( isset( $i['wcag'] ) ? $i['wcag'] : '', 20 ), 'fixedBy' => sanitize_key( isset( $i['fixBy'] ) ? (string) $i['fixBy'] : '' ), ); } return array( 'site' => scanpoof_site_domain(), 'score' => isset( $ov['score'] ) ? (int) $ov['score'] : null, 'scannedAt' => isset( $ov['scannedAt'] ) ? sanitize_text_field( (string) $ov['scannedAt'] ) : null, 'issues' => array( 'critical' => (int) ( isset( $counts['critical'] ) ? $counts['critical'] : 0 ), 'serious' => (int) ( isset( $counts['serious'] ) ? $counts['serious'] : 0 ), 'moderate' => (int) ( isset( $counts['moderate'] ) ? $counts['moderate'] : 0 ), 'minor' => (int) ( isset( $counts['minor'] ) ? $counts['minor'] : 0 ), ), 'topIssues' => $top, ); } function scanpoof_ability_rescan( $input = null ) { $r = scanpoof_api_post( '/api/v1/rescan' ); if ( is_wp_error( $r ) ) { return $r; } scanpoof_log( 'rescan', array( 'via' => 'ability' ) ); return array( 'queued' => true ); }