Is the first line of code necessary?

LPH

Well-known member
I'm looking at this code and trying to figure out if the first line is even necessary.

PHP:
defined( 'ABSPATH' ) OR exit;

// If uninstall is not called from WordPress then exit
if( !defined( 'ABSPATH') && !defined( 'WP_UNINSTALL_PLUGIN' ) )
   exit();

What do you think?
 
Well, I suppose there is an edge condition where WP_UNINSTALL_PLUGIN being defined will pass the first one but not the second, but I'd argue that if WP_UNINSTALL_PLUGIN is defined, WP's absolute path should be around too anyway.

Perhaps the safest solution is simply:

PHP:
if( !defined( 'ABSPATH') || !defined( 'WP_UNINSTALL_PLUGIN' ) )
  exit();
 
The two bits of code above are not logically the same. See the following truth table where A is the code posted by lph and B is the code posted by Arantor.
Code:
ABSPATH  WP_UNINSTALL_PLUGIN  A      B
TRUE     TRUE                 TRUE   TRUE
TRUE     FALSE                TRUE   FALSE
FALSE    TRUE                 FALSE  FALSE
FALSE    FALSE                FALSE  FALSE

It is actually the second line that is unnecessary because for that line to be true, it would require that ABSPATH is not defined, but we have already determined from the first line that it is defined.
 
  • Like
Reactions: LPH
Thank you both. The first line was added a few days ago and I didn't document why I put it there. :eek:
 
The two bits of code above are not logically the same. See the following truth table where A is the code posted by lph and B is the code posted by Arantor.
Code:
ABSPATH  WP_UNINSTALL_PLUGIN  A      B
TRUE    TRUE                TRUE  TRUE
TRUE    FALSE                TRUE  FALSE
FALSE    TRUE                FALSE  FALSE
FALSE    FALSE                FALSE  FALSE

It is actually the second line that is unnecessary because for that line to be true, it would require that ABSPATH is not defined, but we have already determined from the first line that it is defined.

Of course they're not logically the same. I specifically made mention of that fact in my opening paragraph that you could have WP_UNINSTALL_PLUGIN defined and not ABSPATH and it would behave oddly...

The second line is not unnecessary as a result... which is why I suggested an alternative that wasn't quite 'perfect' but covered the meaning more effectively (IMO)
 
Maybe if this is in "English" then I might be able to understand a little bit better. Please correct the flow of ideas if something is wrong.

Original code:
If ABSPATH is defined then continue or exit

If ABSPATH not defined AND WP_UNINSTALL_PLUGIN not defined then exit

Arantor code:
If ABSPATH not defined OR WP_UNINSTALL_PLUGIN not defined then exit

What I am after
Avoid someone to trigger the uninstall without meeting the conditions of ABSPATH and/or WP_UNINSTALL_PLUGIN being defined ...

In other words,
if the ABSPATH is undefined then exit.
if WP_UNINSTALL_PLUGIN is undefined then exit

Is this an OR or AND condition? :unsure:
 
Since either condition will trigger the exit, but both conditions do not have to be met, this would be OR logic. If either ABSPATH is undefined OR WP_UNINSTALL_PLUGIN is undefined, THEN exit.
 
My understanding is that this code would NOT exit in the event that the uninstaller wasn't triggered, i.e., it would run some other code that wasn't the uninstaller. Hence the original code (without the first line) would do this.

At some point, you have then decided that you want ABSPATH to be defined for all other pages as well. This then makes the second line completely useless.
 
Back
Top Bottom