Home > Enterprise >  Suppress getAttribute() error when using cfDecodeEmail() to decode email using PHP
Suppress getAttribute() error when using cfDecodeEmail() to decode email using PHP

Time:09-21

The purpose: To decode cloudflare email

The problem: Uncaught Error: Call to a member function getAttribute() on null

Sounds as easy as using is_null() but it doesn't work. Tried is_object() but i still can't get out of the error.

Objective:

$field['value'] = cfDecodeEmail($a->getAttribute("data-cfemail")); code returns the email address which I have assigned into an ACF field.

So now any field that doesn't have the value returns the error mentioned above on getAttribute().

So if this would point you towards what I'm trying to achieve:

if (is_null()) {
 echo '';
} else {
  $field['value'] = cfDecodeEmail($a->getAttribute("data-cfemail"));
}

The whole code:

add_filter('acf/load_field/name=application_email', function($field) {
    $howtoapply = get_field('howtoapply');
    if( get_post_meta($post->ID, 'application_email', true) == '' ) {

$apply_link = get_field('howtoapply');

$link = $apply_link;
libxml_use_internal_errors(true);
$dom = new DOMDocument();
@$dom->loadHTML($link);
$dom->loadHTML($link);
foreach ($dom->getElementsByTagName("a") as $a) {
 
}

function cfDecodeEmail($encodedString){
  $k = hexdec(substr($encodedString,0,2));
  for($i=2,$email='';$i<strlen($encodedString)-1;$i =2){
    $email.=chr(hexdec(substr($encodedString,$i,2))^$k);
  }
  return $email;
}

    $field['value'] = cfDecodeEmail($a->getAttribute("data-cfemail"));
        
    }
    
    return $field; 
    
});

I don't know what is missing from this code.

Thanks.

CodePudding user response:

$field['value'] = cfDecodeEmail( $a->getAttribute( 'data-cfemail' ) );

You have to check null or is_object on object $a, it will be done like this.

$field['value'] = ( ! is_null( $a ) && is_object( $a ) ) ? cfDecodeEmail( $a->getAttribute( 'data-cfemail' ) ) : '';
  • Related