';
return $html;
}
/**
* Calculate the issue density
*
* @param int $issue_count number of issues.
* @param int $element_count number of elements.
* @param int $content_length length of content.
* @return int
*/
function edac_get_issue_density( $issue_count, $element_count, $content_length ) {
if ( $element_count < 1 || $content_length < 1 ) {
return 0;
}
$element_weight = .8;
$content_weight = .2;
$error_elements_percentage = $issue_count / $element_count;
$error_content_percentage = $issue_count / $content_length;
$score = (
( $error_elements_percentage * $element_weight ) +
( $error_content_percentage * $content_weight )
);
return round( $score * 100, 2 );
}
/**
* Get simplified summary
*
* @param integer $post Post ID.
* @return void
*/
function edac_get_simplified_summary( $post = null ) {
if ( null === $post ) {
$post = get_the_ID();
}
if ( null === $post ) {
return;
}
echo wp_kses_post(
( new \EDAC\Inc\Simplified_Summary() )->simplified_summary_markup( $post )
);
}
/**
* Get Post Count by available custom post types
*
* @return mixed
*/
function edac_get_posts_count() {
$output = [];
$post_types = get_option( 'edac_post_types' );
if ( $post_types ) {
foreach ( $post_types as $post_type ) {
$counts = wp_count_posts( $post_type );
if ( $counts ) {
foreach ( $counts as $key => $value ) {
// phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual
if ( 0 == $value ) {
unset( $counts->{$key} );
}
}
}
if ( $counts ) {
$array = [];
foreach ( $counts as $key => $value ) {
$array[] = $key . ' = ' . $value;
}
if ( $array ) {
$output[] = $post_type . ': ' . implode( ', ', $array );
}
}
}
}
if ( $output ) {
return implode( ', ', $output );
}
return false;
}
/**
* Get Raw Global Error Count
*
* @return array
*/
function edac_get_error_count() {
global $wpdb;
// Define a unique cache key for our data.
$cache_key = 'edac_errors_' . get_current_blog_id();
$stored_errors = wp_cache_get( $cache_key );
// Check if the result exists in the cache.
if ( false === $stored_errors ) {
// If not, perform the database query.
$table_name = $wpdb->prefix . 'accessibility_checker';
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
$stored_errors = (int) $wpdb->get_var( $wpdb->prepare( 'SELECT count(*) FROM %i WHERE siteid = %d AND ruletype = %s', $table_name, get_current_blog_id(), 'error' ) );
// Save the result in the cache for future use.
wp_cache_set( $cache_key, $stored_errors );
}
return $stored_errors;
}
/**
* Get Raw Global Warning Count
*
* @return array Array of.
*/
function edac_get_warning_count() {
global $wpdb;
// Define a unique cache key for our data.
$cache_key = 'edac_warnings_' . get_current_blog_id();
$stored_warnings = wp_cache_get( $cache_key );
// Check if the result exists in the cache.
if ( false === $stored_warnings ) {
// If not, perform the database query.
$table_name = $wpdb->prefix . 'accessibility_checker';
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
$stored_warnings = (int) $wpdb->get_var( $wpdb->prepare( 'SELECT count(*) FROM %i WHERE siteid = %d AND ruletype = %s', $table_name, get_current_blog_id(), 'warning' ) );
// Save the result in the cache for future use.
wp_cache_set( $cache_key, $stored_warnings );
}
return $stored_warnings;
}
/**
* Get Database Table Count
*
* @param string $table Database table.
* @return int
*/
function edac_database_table_count( $table ) {
global $wpdb;
// Create a unique cache key based on the table's name.
$cache_key = 'edac_table_count_' . $table;
// Try to get the count from the cache first.
$count = wp_cache_get( $cache_key );
if ( false === $count ) {
// If the count is not in the cache, perform the database query.
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
$count = $wpdb->get_var( $wpdb->prepare( 'SELECT count(*) FROM %i', $wpdb->prefix . $table ) );
// Save the count to the cache for future use.
wp_cache_set( $cache_key, $count );
}
return $count;
}
/**
* Generate a summary statistic list item.
*
* @since 1.14.0
*
* @param string $item_class The base CSS class for the list item.
* @param int $count The count of items to display.
* @param string $label The translated label with count included.
*
* @return string The generated HTML list item.
*/
function edac_generate_summary_stat( string $item_class, int $count, string $label ): string {
$has_error_class = ( $count > 0 ) ? ' has-errors' : '';
return '
' . $count . '
' . $label . '
';
}
/**
* Generate links to pro page with some params.
*
* @param array $query_args A list of key value pairs to add as query vars to the link.
* @param string $type The type of link to generate. Default is 'pro'.
* @param array $args Additional arguments to pass on the link.
* @return string
*/
function edac_generate_link_type( $query_args = [], $type = 'pro', $args = [] ): string {
$date_now = new DateTime( gmdate( 'Y-m-d H:i:s' ) );
$activation_date = new DateTime( get_option( 'edac_activation_date', gmdate( 'Y-m-d H:i:s' ) ) );
$interval = $date_now->diff( $activation_date );
$days_active = $interval->days;
$query_defaults = [
'utm_source' => 'accessibility-checker',
'utm_medium' => 'software',
'utm_campaign' => 'wordpress-general',
'php_version' => PHP_VERSION,
'platform' => 'wordpress',
'platform_version' => $GLOBALS['wp_version'],
'software' => defined( 'EDACP_KEY_VALID' ) && EDACP_KEY_VALID ? 'pro' : 'free',
'software_version' => defined( 'EDACP_VERSION' ) ? EDACP_VERSION : EDAC_VERSION,
'days_active' => $days_active,
];
$query_args = array_merge( $query_defaults, $query_args );
switch ( $type ) {
case 'help':
$base_link = trailingslashit( 'https://a11ychecker.com/help' . $args['help_id'] ?? '' );
break;
case 'custom': // phpcs:ignore -- intentially only breaking inside the condition because if it's not set we want to hit default.
if ( $args['base_link'] ) {
$base_link = $args['base_link'];
break;
}
case 'pro':
default:
$base_link = 'https://equalizedigital.com/accessibility-checker/pricing/';
break;
}
return add_query_arg( $query_args, $base_link );
}
/**
* Check if WooCommerce is enabled.
*
* This just checks for existence of the main WooCommerce function and class.
*
* @return bool
*/
function edac_is_woocommerce_enabled() {
return function_exists( 'WC' ) && class_exists( 'WooCommerce' );
}
/**
* Check if a given post id is the WooCommerce checkout page.
*
* @param int $post_id The post ID to check.
* @return bool
*/
function edac_check_if_post_id_is_woocommerce_checkout_page( $post_id ) {
if ( ! edac_is_woocommerce_enabled() ) {
return false;
}
return wc_get_page_id( 'checkout' ) === $post_id;
}
/**
* Parse HTML content to extract image or SVG elements
*
* @param string $html The HTML content to parse.
* @return array Array containing 'img' (string) and 'svg' (string) keys.
*/
function edac_parse_html_for_media( $html ) {
if ( empty( $html ) ) {
return [
'img' => null,
'svg' => null,
];
}
// Decode HTML entities before processing.
$decoded_html = html_entity_decode( $html, ENT_QUOTES | ENT_HTML5 );
// Early return if no media tags found.
if ( stripos( $decoded_html, ' null,
'svg' => null,
];
}
// More specific img tag regex pattern.
if ( preg_match( '/]+src=([\'"])(.*?)\1[^>]*>/i', $decoded_html, $matches ) ) {
return [
'img' => $matches[2] ?? null,
'svg' => null,
];
}
// SVG pattern remains the same.
if ( preg_match( '/