替代PHP“goto output;”

时间:2013-03-09 13:43:59

标签: php wordpress goto

我使用WordPress的插件使用“goto output;”这对我的一些使用PHP 5.3的朋友造成了问题。他们还想使用我找到的名为MiniPress的插件。

如何重写这个插件,以便在PHP 5.3中工作?

private static function get_filesystem() {
    if ( false === self::$fs ) {
        global $wp_filesystem;

        require_once( ABSPATH . 'wp-admin/includes/file.php' );

        $write_method = get_filesystem_method( array(), false );
        if ( $write_method != 'direct' && $write_method != 'ftpsockets' ) {
            self::$fs = false;
            goto output;
        }

        if ( false === ( $creds = @request_filesystem_credentials( '' ) ) ) {
            self::$fs = false;
            goto output;
        }

        if ( ! WP_Filesystem( $creds ) ) {
            self::$fs = false;
            goto output;
        }

        self::$fs = $wp_filesystem;
    }

    output:
    return self::$fs;
}

p.s:我不是插件的作者

4 个答案:

答案 0 :(得分:2)

只需替换:

goto output;

使用:

return self::$fs;

并在结尾删除output:

然后可以将您的方法重写为:

private static function get_filesystem() {
    if ( false === self::$fs ) {
        global $wp_filesystem;
        require_once( ABSPATH . 'wp-admin/includes/file.php' );
        $write_method = get_filesystem_method( array(), false );
        if (
            ($write_method != 'direct' && $write_method != 'ftpsockets') || 
            (false === ($creds = @request_filesystem_credentials(''))) ||
            (!WP_Filesystem($creds))
        ) self::$fs = false;
        else self::$fs = $wp_filesystem;
    }
    return self::$fs;
}

答案 1 :(得分:0)

这应该有效

private static function get_filesystem() {
    if ( false === self::$fs ) {
        global $wp_filesystem;

        require_once( ABSPATH . 'wp-admin/includes/file.php' );

        $write_method = get_filesystem_method( array(), false );
        if ( $write_method != 'direct' && $write_method != 'ftpsockets' ) {
            self::$fs = false;
        }

        else if ( false === ( $creds = @request_filesystem_credentials( '' ) ) ) {
            self::$fs = false;
        }

        else if ( ! WP_Filesystem( $creds ) ) {
            self::$fs = false;
        } else self::$fs = $wp_filesystem;
    }

    return self::$fs;
}

答案 2 :(得分:0)

另一种方式:

private static function get_filesystem() {
    if ( false === self::$fs ) {
        global $wp_filesystem;

        require_once( ABSPATH . 'wp-admin/includes/file.php' );

        $write_method = get_filesystem_method( array(), false );

        if ( 
            $write_method != 'direct' && $write_method != 'ftpsockets' 
            ||
            false === ( $creds = @request_filesystem_credentials( '' ) )
            ||
            ! WP_Filesystem( $creds )
        ) {
            self::$fs = false;
        } else {
            self::$fs = $wp_filesystem;
        }
    } 
    return self::$fs;
}

答案 3 :(得分:0)

如果删除所有冗余代码,最终会得到以下结果:

private static function get_filesystem() {
    if ( false === self::$fs ) {
        global $wp_filesystem;
        require_once( ABSPATH . 'wp-admin/includes/file.php' );
        $write_method = get_filesystem_method( array(), false );
        if (($write_method == 'direct' || $write_method == 'ftpsockets') && 
            (false !== ($creds = @request_filesystem_credentials(''))) &&
            (WP_Filesystem($creds)) {
          self::$fs = $wp_filesystem;
        }
    }
    return self::$fs;
}