Skip to content

Commit

Permalink
Merge pull request #327 from equalizedigital/hotfix/304-empty_linkphp…
Browse files Browse the repository at this point in the history
…-rule-logic-false-positive

Hotfix/304 empty linkphp rule logic false positive
  • Loading branch information
boonedev authored Sep 26, 2023
2 parents 4e43b0f + dd76d55 commit 9815077
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 61 deletions.
74 changes: 37 additions & 37 deletions includes/helper-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ function edac_compare_strings( $string1, $string2 ) {
} else {
return 0;
}

}

/**
Expand Down Expand Up @@ -192,7 +191,6 @@ function edac_filter_by_value( $array, $index, $value ) {
} else {
return null;
}

}

/**
Expand Down Expand Up @@ -290,7 +288,6 @@ function edac_post_types() {
}

return $post_types;

}

/**
Expand All @@ -312,7 +309,6 @@ function edac_process_actions() {
do_action( 'edac_' . $_GET['edac-action'], $_GET );
}
}

}

/**
Expand All @@ -334,7 +330,8 @@ function edac_str_get_html(
$target_charset = DEFAULT_TARGET_CHARSET,
$stripRN = true,
$defaultBRText = DEFAULT_BR_TEXT,
$defaultSpanText = DEFAULT_SPAN_TEXT ) {
$defaultSpanText = DEFAULT_SPAN_TEXT
) {
$dom = new EDAC_Dom(
null,
$lowercase,
Expand Down Expand Up @@ -473,16 +470,16 @@ function edac_replace_css_variables( $value, $css_array ) {
function edac_generate_nonce( $secret, $timeout_seconds = 120 ) {

$length = 10;
$chars = '1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM';
$ll = strlen( $chars ) - 1;
$salt = '';
$chars = '1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM';
$ll = strlen( $chars ) - 1;
$salt = '';
while ( strlen( $salt ) < $length ) {
$salt .= $chars[ rand( 0, $ll ) ];
}

$time = time();
$time = time();
$max_time = $time + $timeout_seconds;
$nonce = $salt . ',' . $max_time . ',' . sha1( $salt . $secret . $max_time );
$nonce = $salt . ',' . $max_time . ',' . sha1( $salt . $secret . $max_time );
return $nonce;
}

Expand All @@ -501,10 +498,10 @@ function edac_is_valid_nonce( $secret, $nonce ) {
if ( count( $a ) != 3 ) {
return false;
}
$salt = $a[0];
$salt = $a[0];
$max_time = intval( $a[1] );
$hash = $a[2];
$back = sha1( $salt . $secret . $max_time );
$hash = $a[2];
$back = sha1( $salt . $secret . $max_time );
if ( $back != $hash ) {
return false;
}
Expand All @@ -523,19 +520,19 @@ function edac_is_valid_nonce( $secret, $nonce ) {
*/
function edac_get_upcoming_meetups_json( $meetup, $count = 5 ) {

$key = 'upcoming_meetups__' . sanitize_title( $meetup ) . '__' . intval( $count );
$key = 'upcoming_meetups__' . sanitize_title( $meetup ) . '__' . intval( $count );
$output = get_transient( $key );

if ( false === $output ) {

$query_args = array(
'sign' => 'true',
'sign' => 'true',
'photo-host' => 'public',
'page' => intval( $count ),
'page' => intval( $count ),
);

$request_uri = 'https://api.meetup.com/' . sanitize_title( $meetup ) . '/events';
$request = wp_remote_get( add_query_arg( $query_args, $request_uri ) );
$request = wp_remote_get( add_query_arg( $query_args, $request_uri ) );

if ( is_wp_error( $request ) || '200' != wp_remote_retrieve_response_code( $request ) ) {
return;
Expand Down Expand Up @@ -588,7 +585,6 @@ function edac_get_upcoming_meetups_html( $meetup, $count = 5, $truncate = true,
$html .= '</ul>';

return $html;

}

/**
Expand All @@ -601,15 +597,15 @@ function edac_get_upcoming_meetups_html( $meetup, $count = 5, $truncate = true,
function edac_truncate_html_content( $html, $paragraph_count = 1 ) {

$allowed_tags = array(
'div' => array(),
'p' => array(),
'span' => array(),
'br' => array(),
'hr' => array(),
'div' => array(),
'p' => array(),
'span' => array(),
'br' => array(),
'hr' => array(),
'strong' => array(),
'b' => array(),
'em' => array(),
'i' => array(),
'b' => array(),
'em' => array(),
'i' => array(),
);


Expand Down Expand Up @@ -643,7 +639,6 @@ function edac_truncate_html_content( $html, $paragraph_count = 1 ) {
}

return false;

}

/**
Expand All @@ -664,32 +659,35 @@ function edac_get_issue_density( $issue_count, $element_count, $content_length )
$content_weight = .2;

$error_elements_percentage = $issue_count / $element_count;
$error_content_percentage = $issue_count / $content_length;
$error_content_percentage = $issue_count / $content_length;

$score = ( ( $error_elements_percentage * $element_weight ) + ( $error_content_percentage * $content_weight ) );

return round( $score * 100, 2 );

}


/**
* Get info from a dom that we need for calculating density
* Get info from html that we need for calculating density
*
* @param [type] $dom
* @param string $html
* @return void
*/
function edac_get_body_density_data( $dom ) {
function edac_get_body_density_data( $html ) {


if ( $dom ) {
if ( $html && trim( $html ) !== '' ) {

$body_element = $dom->find( 'body', 0 );
$density_dom = new simple_html_dom();
$density_dom->load( $html );

$body_element = $density_dom->find( 'body', 0 );

if ( null == $body_element ) {
return false;
}

// Remove the elements we shouldn't count
// Remove the elements we shouldn't count.
foreach ( $body_element->find( '.edac-highlight-panel,#wpadminbar,style,script' ) as $element ) {
$element->remove();
}
Expand All @@ -699,18 +697,20 @@ function edac_get_body_density_data( $dom ) {
$body_elements_count = edac_count_dom_descendants( $body_element );

$body_content = preg_replace( '/[^A-Za-z0-9]/', '', $body_element->plaintext );

return array(
$body_elements_count,
strlen( $body_content ),
);

}
}


return false;
}


/**
* Recursively count elements in a dom
*
Expand All @@ -721,7 +721,7 @@ function edac_count_dom_descendants( $dom_elements ) {
$count = 0;

foreach ( $dom_elements->children() as $child ) {
$count++;
++$count;
$count += edac_count_dom_descendants( $child ); // Recursively count descendants.
}

Expand Down
55 changes: 46 additions & 9 deletions includes/rules/empty_link.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,70 @@
* @return array
*/
function edac_rule_empty_link( $content, $post ) {
$dom = $content['html'];

$dom = $content['html'];
$errors = array();

$links = $dom->find( 'a' );

foreach ( $links as $link ) {
$error = false;

if (
str_ireplace( array( ' ', '&nbsp;', '-', '_' ), '', trim( $link->plaintext ) ) == ''
&& $link->hasAttribute( 'href' )
&& $link->getAttribute( 'aria-label' ) == ''
&& $link->getAttribute( 'title' ) == ''
) {
$a_tag_code = $link->outertext;
$image = $link->find( 'img' );
$input = $link->find( 'input' );
$i = $link->find( 'i' );

// This link does not have plaintext within the tag &
// does have an href &
// does not have an aria-label &
// does not have a title.

$a_tag_code = $link->outertext;

if (
'' !== $a_tag_code
&& ! $link->hasAttribute( 'id' )
&& ! $link->hasAttribute( 'name' )
&& ( ! isset( $image[0] ) || trim( $image[0]->getAttribute( 'alt' ) ) == '' )
&& ( ! isset( $input[0] ) || trim( $input[0]->getAttribute( 'value' ) ) == '' )
&& ( ! isset( $i[0] ) || ( trim( $i[0]->getAttribute( 'title' ) ) == '' ) && trim( $i[0]->getAttribute( 'aria-label' ) ) == '' )
) {
$errors[] = $a_tag_code;

// This link does not have an id &
// does not have a name.

$image = $link->find( 'img' );
if ( ! $error && isset( $input[0] ) && trim( $image[0]->getAttribute( 'alt' ) ) == '' ) {

// The first image inside the link does not have an alt.
// Throw error.
$error = $a_tag_code;
}

$input = $link->find( 'input' );
if ( ! $error && isset( $input[0] ) && trim( $image[0]->getAttribute( 'value' ) ) == '' ) {

// The first input inside the link does not have a value.
// Throw error.
$error = $a_tag_code;
}

$i = $link->find( 'i' );
if ( ! $error && isset( $input[0] ) &&
trim( $i[0]->getAttribute( 'title' ) ) == '' &&
trim( $i[0]->getAttribute( 'aria-label' ) ) == ''
) {

// The first i inside the link does not have a title &
// does not have an aria-lable.
// Throw error.
$error = $a_tag_code;
}
}

if ( $error ) {
$errors[] = $error;
}
}
}
return $errors;
Expand Down
32 changes: 17 additions & 15 deletions includes/validate.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,8 @@ function edac_remove_corrected_posts( $post_ID, $type, $pre = 1, $ruleset = 'php
global $wpdb;

// TODO: setup a rules class for loading/filtering rules.
$rules = edac_register_rules();
$js_rule_ids = array();
$rules = edac_register_rules();
$js_rule_ids = array();
$php_rule_ids = array();
foreach ( $rules as $rule ) {
if ( array_key_exists( 'ruleset', $rule ) && 'js' === $rule['ruleset'] ) {
Expand All @@ -196,7 +196,7 @@ function edac_remove_corrected_posts( $post_ID, $type, $pre = 1, $ruleset = 'php
// Build a sql sanitized list from an array
// See: https://stackoverflow.com/questions/10634058/wordpress-prepared-statement-with-in-condition .
$js_rule_ids = array_map(
function( $v ) {
function ( $v ) {
return "'" . esc_sql( $v ) . "'";
},
$js_rule_ids
Expand All @@ -206,7 +206,7 @@ function( $v ) {
// Build a sql sanitized list from an array
// See: https://stackoverflow.com/questions/10634058/wordpress-prepared-statement-with-in-condition .
$php_rule_ids = array_map(
function( $v ) {
function ( $v ) {
return "'" . esc_sql( $v ) . "'";
},
$php_rule_ids
Expand Down Expand Up @@ -312,13 +312,14 @@ function edac_get_content( $post ) {


try {

// setup the context for the request.
// note - if follow_location => false, permalinks that redirect (both offsite and on).
// will not be followed, so $content['html] will be false.
$merged_context_opts = array_merge( $default_context_opts, $context_opts );
$context = stream_context_create( $merged_context_opts );

$dom = file_get_html( $url, false, $context );
$dom = file_get_html( $url, false, $context );
$content['html'] = edac_remove_elements(
$dom,
array(
Expand All @@ -328,17 +329,18 @@ function edac_get_content( $post ) {
'#qm-icon-container', // query-monitor.
)
);

// Write density data to post meta.
$body_density_data = edac_get_body_density_data( $content['html'] );

if ( false != $body_density_data ) {

update_post_meta( $post->ID, '_edac_density_data', $body_density_data );
// Write density data to post meta.
if ( $content['html'] ) {

} else {
delete_post_meta( $post->ID, '_edac_density_data' );
$page_html = $content['html']->save();
$body_density_data = edac_get_body_density_data( $page_html );

if ( false != $body_density_data ) {
update_post_meta( $post->ID, '_edac_density_data', $body_density_data );
} else {
delete_post_meta( $post->ID, '_edac_density_data' );
}
}
} catch ( Exception $e ) {
update_post_meta( $post->ID, '_edac_density_data', '0,0' );
Expand Down Expand Up @@ -412,7 +414,7 @@ function edac_get_content( $post ) {
*/
function edac_show_draft_posts( $query ) {

//$headers = getallheaders();
// $headers = getallheaders();

// Do not run if it's not the main query.
if ( ! $query->is_main_query() ) {
Expand All @@ -436,7 +438,7 @@ function edac_show_draft_posts( $query ) {
$url_token = isset( $_GET['edac_token'] ) ? sanitize_text_field( $_GET['edac_token'] ) : false;

// If the token is not set we do nothing and return early.
if( false === $url_token ){
if ( false === $url_token ) {
return;
}

Expand Down

0 comments on commit 9815077

Please sign in to comment.