Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend File System Class #215

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open

Extend File System Class #215

wants to merge 10 commits into from

Conversation

namithj
Copy link
Contributor

@namithj namithj commented Nov 29, 2024

Extend File System Class

Pull Request

What changed?

(Please list the changes you made...)

Why did it change?

(Please list the reasons you made the changes...)

Did you fix any specific issues?

(Please tag any specific issues here...)

CERTIFICATION

By opening this pull request, I do agree to abide by the Code of Conduct and be bound by the terms of the Contribution Guidelines in effect on the date and time of my contribution as proven by the revision information in GitHub.

@namithj namithj requested a review from costdev November 29, 2024 15:02
@namithj
Copy link
Contributor Author

namithj commented Nov 29, 2024

@costdev Can you take a look here before I go too far and see if there are any issues with this approach.

Copy link
Contributor

@costdev costdev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the ping @namithj! I don't see any immediate issues.

I understand the desire to extend WP_Filesystem_Direct as this is indeed extending the underlying functionality, and there's no need to, for example, repeat the contents of WP_Filesystem_Direct::chmod() elsewhere. I think it's fine to keep going.

What other methods do you intend to introduce to this class?

@namithj
Copy link
Contributor Author

namithj commented Nov 30, 2024

I will be overriding the get_contents_array function to introduce a two new parameters (no of lines to read and read direction) which will let us read from bottom up and also modify it to read via file pointer and iterate over the required no of lines instead of pulling the whole content into memory as it does now.

The Read Routine
@namithj namithj marked this pull request as ready for review December 4, 2024 14:54
PHP Doc Alignment
@namithj
Copy link
Contributor Author

namithj commented Dec 20, 2024

I will try and push the changes today so that we can merge this in

require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php';
require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php';
WP_Filesystem();
return new Filesystem_Direct( false );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This means that after calling $wp_filesystem = self::init_filesystem(), $wp_filesystem will always be truthy (objects are always truthy). The if ( ! $wp_filesystem ) condition in Debug::verify_filesystem() will never pass in this circumstance and the method will always return true.

Therefore, we can likely remove Debug::verify_filesystem() altogether since it's a private method, and self::init_filesystem() is called in all methods that call self::verify_filesystem().

}
return $content;

return $file_content;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can now change the @return annotation in the docblock to:

@return array|WP_Error An array of lines in the file, limited to $limit, or a WP_Error object on failure.

require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php';
require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php';
WP_Filesystem();
return new Filesystem_Direct( false );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, we can store the filesystem object to a static property to avoid re-running WP_Filesystem() and creating a new object each time.

/**
 * The filesystem.
 *
 * @var Filesystem_Direct
 */
private static $filesystem;

// init_filesystem()
if ( ! self::$filesystem instanceof Filesystem_Direct ) {
	require_once ABSPATH . 'wp-admin/includes/file.php';
	require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php';
	require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php';
	WP_Filesystem();
	self::$filesystem = new Filesystem_Direct( false );
}
return self::$filesystem;

@@ -31,23 +31,20 @@ private static function get_file_path() {
/**
* Initializes the WordPress Filesystem.
*
* @return WP_Filesystem_Base|false The filesystem object or false on failure.
* @return WP_Filesystem_Direct|false The filesystem object or false on failure.
Copy link
Contributor

@costdev costdev Dec 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @return WP_Filesystem_Direct|false The filesystem object or false on failure.
* @return Filesystem_Direct The filesystem object.

We don't use the WP_ prefix for the class name, and the method can't return false.

Comment on lines +17 to +18
*
* @since 2.5.0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
*
* @since 2.5.0

Not applicable to AspireUpdate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants