1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Is the first line of code necessary?

Discussion in 'General PHP and MySQL Discussions' started by LPH, Jun 9, 2013.

  1. LPH

    LPH Well-Known Member

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

    defined'ABSPATH' ) OR exit;

    // If uninstall is not called from WordPress then exit
    if( !defined'ABSPATH') && !defined'WP_UNINSTALL_PLUGIN' ) )
    What do you think?
  2. Arantor

    Arantor Active Member

    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:

    if( !defined'ABSPATH') || !defined'WP_UNINSTALL_PLUGIN' ) )
  3. Jon W

    Jon W Well-Known Member

    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.
    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.
    LPH likes this.
  4. LPH

    LPH Well-Known Member

    Thank you both. The first line was added a few days ago and I didn't document why I put it there. :eek:
    Jon W likes this.
  5. Arantor

    Arantor Active Member

    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)
  6. LPH

    LPH Well-Known Member

    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:
  7. jmurrayhead

    jmurrayhead Well-Known Member

    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.
  8. Jon W

    Jon W Well-Known Member

    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.

Share This Page