• Resolved Robert

    (@e2robert)


    Hello,

    I’m using your plugin and it is great. However, there are some minor errors (in terms of clean programming violations) that always popup in our error log (the errors are not functional, but it is hard to grasp the real errors, if such PHP notices pollute the error log):

    1. Notice: Undefined index: PHP_AUTH_USER in wp-content/plugins/more-privacy-options/ds_wp3_private_blog.php on line 348
    2. Notice: Undefined index: PHP_AUTH_PW in wp-content/plugins/more-privacy-options/ds_wp3_private_blog.php on line 349
    3. Notice: Undefined property: WP_Error::$user_login in wp-content/plugins/more-privacy-options/ds_wp3_private_blog.php on line 352
    4. Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; ds_more_privacy_options has a deprecated constructor in wp-content/plugins/more-privacy-options/ds_wp3_private_blog.php on line 93

    The solution to the first two points is easy; just replace:
    $credentials['user_login'] = $_SERVER['PHP_AUTH_USER'];
    with:
    $credentials['user_login'] = isset($_SERVER['PHP_AUTH_USER']) ? $_SERVER['PHP_AUTH_USER'] : '';

    The third problem can be solved by replacing:
    $user_id = get_user_id_from_string( $user->user_login );
    with
    $user_id = !is_wp_error( $user ) ? get_user_id_from_string( $user->user_login ) : null;

    And finally we need to use __construct to stay future proof. PHP4-styled constructors will be not called by future PHP versions. so we need to rename the method ds_more_privacy_options to __construct.
    Additionally we can add another method to retain backward compatibility:

    function ds_more_privacy_options() {
    	$this->__construct();
    }

    I hope you can understand why clean programming is important. If you got questions, feel free to contact me. I’ve also appended now a patch if that is easier for you:

    Index: wp-content/plugins/more-privacy-options/ds_wp3_private_blog.php
    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
    ===================================================================
    --- wp-content/plugins/more-privacy-options/ds_wp3_private_blog.php	(revision 4)
    +++ wp-content/plugins/more-privacy-options/ds_wp3_private_blog.php	(revision )
    @@ -94,6 +94,10 @@
     		var $l10n_prefix;
    
     	function ds_more_privacy_options() {
    +		$this->__construct();
    +	}
    +
    +	function __construct() {
     		global  $current_blog;
    
     		if ( ! is_multisite() ) {
    @@ -181,7 +185,7 @@
     				$blogname = get_blog_option( $blog_id, 'blogname');
     			$email =  stripslashes( get_site_option('admin_email') );
     			$subject = __('Site ', $this->l10n_prefix).'"'.$blogname.'" (ID: '.$blog_id.'), '.get_site_url( $blog_id ).', '. __('changed reading visibility setting to ', $this->l10n_prefix) .'"'. $to_new.'"';
    -  			$message .= __('Site ', $this->l10n_prefix).'"'.$blogname.'" (ID: '.$blog_id.'), '.get_site_url( $blog_id ).', '.__('changed reading visibility setting to ', $this->l10n_prefix) .'"'. $to_new.'."';
    +  			$message = __('Site ', $this->l10n_prefix).'"'.$blogname.'" (ID: '.$blog_id.'), '.get_site_url( $blog_id ).', '.__('changed reading visibility setting to ', $this->l10n_prefix) .'"'. $to_new.'."';
       			$message .= __(" \r\n\r\nSent by More Privacy Options plugin.", $this->l10n_prefix);
    
     			$headers = 'Auto-Submitted: auto-generated';
    @@ -345,11 +349,11 @@
     		//December 2012 tested with "Free RSS" for iPhone. Google Reader does not authenticate locked feeds. Tough to find a free reader that does authenticate.
     		global $current_blog, $blog_id;
     			$credentials = array();
    -        	$credentials['user_login'] = $_SERVER['PHP_AUTH_USER'];
    -        	$credentials['user_password'] = $_SERVER['PHP_AUTH_PW'];
    +        	$credentials['user_login'] = isset($_SERVER['PHP_AUTH_USER']) ? $_SERVER['PHP_AUTH_USER'] : '';
    +        	$credentials['user_password'] = isset($_SERVER['PHP_AUTH_PW']) ? $_SERVER['PHP_AUTH_PW'] : '';
     			$credentials['remember'] = true;
     			$user = wp_signon( $credentials, false ); //if this creates WP_User, the next 3 lines are redundant
    -			$user_id = get_user_id_from_string( $user->user_login );
    +			$user_id = !is_wp_error( $user ) ? get_user_id_from_string( $user->user_login ) : null;
    
     			if ( is_wp_error( $user ) ||
     				// "Members Only"
    \ No newline at end of file

    https://www.ads-software.com/plugins/more-privacy-options/

Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Clean PHP Programming – avoid PHP Notices’ is closed to new replies.