0

我为 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;
}

ps:我不是插件的作者

4

4 回答 4

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;
}
于 2013-03-09T13:46:37.413 回答
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;
}
于 2013-03-09T14:03:22.143 回答
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;
}
于 2013-03-09T13:59:31.277 回答
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;
}
于 2013-03-09T14:16:13.590 回答