2007-05-27 20:04:33 -04:00
< ? php
2008-10-01 21:03:26 -04:00
/**
2015-10-16 12:56:26 -04:00
* Filesystem API : Top - level functionality
*
2012-07-26 11:08:30 -04:00
* Functions for reading , writing , modifying , and deleting files on the file system .
2012-10-03 14:49:37 -04:00
* Includes functionality for theme - specific files as well as operations for uploading ,
2012-07-26 11:08:30 -04:00
* archiving , and rendering output when necessary .
2008-10-01 21:03:26 -04:00
*
* @ package WordPress
2015-10-16 12:56:26 -04:00
* @ subpackage Filesystem
* @ since 2.3 . 0
2008-10-01 21:03:26 -04:00
*/
2007-05-27 20:04:33 -04:00
2008-10-01 21:03:26 -04:00
/** The descriptions for theme files. */
2010-12-03 23:22:48 -05:00
$wp_file_descriptions = array (
2015-12-25 16:12:26 -05:00
'functions.php' => __ ( 'Theme Functions' ),
'header.php' => __ ( 'Theme Header' ),
'footer.php' => __ ( 'Theme Footer' ),
'sidebar.php' => __ ( 'Sidebar' ),
'comments.php' => __ ( 'Comments' ),
'searchform.php' => __ ( 'Search Form' ),
'404.php' => __ ( '404 Template' ),
'link.php' => __ ( 'Links Template' ),
2020-01-17 19:54:04 -05:00
// Archives.
2015-12-25 16:12:26 -05:00
'index.php' => __ ( 'Main Index Template' ),
'archive.php' => __ ( 'Archives' ),
'author.php' => __ ( 'Author Template' ),
'taxonomy.php' => __ ( 'Taxonomy Template' ),
'category.php' => __ ( 'Category Template' ),
'tag.php' => __ ( 'Tag Template' ),
'home.php' => __ ( 'Posts Page' ),
'search.php' => __ ( 'Search Results' ),
'date.php' => __ ( 'Date Template' ),
2020-01-17 19:54:04 -05:00
// Content.
2015-12-25 16:12:26 -05:00
'singular.php' => __ ( 'Singular Template' ),
'single.php' => __ ( 'Single Post' ),
'page.php' => __ ( 'Single Page' ),
2017-09-10 13:08:43 -04:00
'front-page.php' => __ ( 'Homepage' ),
2019-03-21 15:48:50 -04:00
'privacy-policy.php' => __ ( 'Privacy Policy Page' ),
2020-01-17 19:54:04 -05:00
// Attachments.
2015-12-25 16:12:26 -05:00
'attachment.php' => __ ( 'Attachment Template' ),
'image.php' => __ ( 'Image Attachment Template' ),
'video.php' => __ ( 'Video Attachment Template' ),
'audio.php' => __ ( 'Audio Attachment Template' ),
'application.php' => __ ( 'Application Attachment Template' ),
2020-01-17 19:54:04 -05:00
// Embeds.
2016-04-17 21:05:27 -04:00
'embed.php' => __ ( 'Embed Template' ),
'embed-404.php' => __ ( 'Embed 404 Template' ),
'embed-content.php' => __ ( 'Embed Content Template' ),
'header-embed.php' => __ ( 'Embed Header Template' ),
'footer-embed.php' => __ ( 'Embed Footer Template' ),
2020-01-17 19:54:04 -05:00
// Stylesheets.
2015-12-25 16:12:26 -05:00
'style.css' => __ ( 'Stylesheet' ),
'editor-style.css' => __ ( 'Visual Editor Stylesheet' ),
'editor-style-rtl.css' => __ ( 'Visual Editor RTL Stylesheet' ),
'rtl.css' => __ ( 'RTL Stylesheet' ),
2020-01-17 19:54:04 -05:00
// Other.
2015-12-25 16:12:26 -05:00
'my-hacks.php' => __ ( 'my-hacks.php (legacy hacks support)' ),
'.htaccess' => __ ( '.htaccess (for rewrite rules )' ),
2020-01-17 19:54:04 -05:00
// Deprecated files.
2015-12-25 16:12:26 -05:00
'wp-layout.css' => __ ( 'Stylesheet' ),
'wp-comments.php' => __ ( 'Comments Template' ),
2010-12-03 23:22:48 -05:00
'wp-comments-popup.php' => __ ( 'Popup Comments Template' ),
2015-12-25 16:12:26 -05:00
'comments-popup.php' => __ ( 'Popup Comments' ),
2010-12-03 23:22:48 -05:00
);
2008-10-01 21:03:26 -04:00
/**
2020-06-25 05:39:13 -04:00
* Gets the description for standard WordPress theme files .
2008-10-01 21:03:26 -04:00
*
2010-10-27 18:24:06 -04:00
* @ since 1.5 . 0
2008-10-01 21:03:26 -04:00
*
2017-07-12 17:40:40 -04:00
* @ global array $wp_file_descriptions Theme file descriptions .
File Editors: Introduce sandboxed live editing of PHP files with rollbacks for both themes and plugins.
* Edits to active plugins which cause PHP fatal errors will no longer auto-deactivate the plugin. Supersedes #39766.
* Introduce sandboxed PHP file edits for active themes, preventing accidental whitescreening of a user's site when introducing a fatal error.
* After writing a change to a PHP file for an active theme or plugin, perform loopback requests on the file editor admin screens and the homepage to check for fatal errors. If a fatal error is encountered, roll back the edited file and display the error to the user to fix and try again.
* Introduce a secure way to scrape PHP fatal errors from a site via `wp_start_scraping_edited_file_errors()` and `wp_finalize_scraping_edited_file_errors()`.
* Moves file modifications from `theme-editor.php` and `plugin-editor.php` to common `wp_edit_theme_plugin_file()` function.
* Refactor themes and plugin editors to submit file changes via Ajax instead of doing full page refreshes when JS is available.
* Use `get` method for theme/plugin dropdowns.
* Improve styling of plugin editors, including width of plugin/theme dropdowns.
* Improve notices API for theme/plugin editor JS component.
* Strip common base directory from plugin file list. See #24048.
* Factor out functions to list editable file types in `wp_get_theme_file_editable_extensions()` and `wp_get_plugin_file_editable_extensions()`.
* Scroll to line in editor that has linting error when attempting to save. See #41886.
* Add checkbox to dismiss lint errors to proceed with saving. See #41887.
* Only style the Update File button as disabled instead of actually disabling it for accessibility reasons.
* Ensure that value from CodeMirror is used instead of `textarea` when CodeMirror is present.
* Add "Are you sure?" check when leaving editor when there are unsaved changes.
Supersedes [41560].
See #39766, #24048, #41886.
Props westonruter, Clorith, melchoyce, johnbillion, jjj, jdgrimes, azaozz.
Fixes #21622, #41887.
Built from https://develop.svn.wordpress.org/trunk@41721
git-svn-id: http://core.svn.wordpress.org/trunk@41555 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2017-10-03 20:20:45 -04:00
* @ global array $allowed_files List of allowed files .
2020-06-25 05:39:13 -04:00
*
* @ param string $file Filesystem path or filename .
2015-09-27 23:11:24 -04:00
* @ return string Description of file from $wp_file_descriptions or basename of $file if description doesn ' t exist .
2020-06-25 05:39:13 -04:00
* Appends 'Page Template' to basename of $file if the file is a page template .
2008-10-01 21:03:26 -04:00
*/
2007-05-27 20:04:33 -04:00
function get_file_description ( $file ) {
2015-09-27 23:11:24 -04:00
global $wp_file_descriptions , $allowed_files ;
2007-05-27 20:04:33 -04:00
2020-06-25 05:39:13 -04:00
$dirname = pathinfo ( $file , PATHINFO_DIRNAME );
2015-09-27 23:11:24 -04:00
$file_path = $allowed_files [ $file ];
2020-06-25 05:39:13 -04:00
2016-08-20 19:36:28 -04:00
if ( isset ( $wp_file_descriptions [ basename ( $file ) ] ) && '.' === $dirname ) {
2015-09-27 23:11:24 -04:00
return $wp_file_descriptions [ basename ( $file ) ];
} elseif ( file_exists ( $file_path ) && is_file ( $file_path ) ) {
$template_data = implode ( '' , file ( $file_path ) );
2020-06-25 05:39:13 -04:00
2015-09-27 23:11:24 -04:00
if ( preg_match ( '|Template Name:(.*)$|mi' , $template_data , $name ) ) {
2019-09-02 20:41:05 -04:00
/* translators: %s: Template name. */
2015-09-27 23:11:24 -04:00
return sprintf ( __ ( '%s Page Template' ), _cleanup_header_comment ( $name [ 1 ] ) );
}
2007-05-27 20:04:33 -04:00
}
2012-03-28 23:00:38 -04:00
return trim ( basename ( $file ) );
2007-05-27 20:04:33 -04:00
}
2008-10-01 21:03:26 -04:00
/**
2020-06-25 05:39:13 -04:00
* Gets the absolute filesystem path to the root of the WordPress installation .
2008-10-01 21:03:26 -04:00
*
2010-10-27 18:24:06 -04:00
* @ since 1.5 . 0
2008-10-01 21:03:26 -04:00
*
2020-06-25 05:39:13 -04:00
* @ return string Full filesystem path to the root of the WordPress installation .
2008-10-01 21:03:26 -04:00
*/
2007-05-27 20:04:33 -04:00
function get_home_path () {
2014-06-29 06:29:14 -04:00
$home = set_url_scheme ( get_option ( 'home' ), 'http' );
$siteurl = set_url_scheme ( get_option ( 'siteurl' ), 'http' );
2020-06-25 05:39:13 -04:00
2012-07-06 09:54:15 -04:00
if ( ! empty ( $home ) && 0 !== strcasecmp ( $home , $siteurl ) ) {
$wp_path_rel_to_home = str_ireplace ( $home , '' , $siteurl ); /* $siteurl - $home */
2017-11-30 18:11:00 -05:00
$pos = strripos ( str_replace ( '\\' , '/' , $_SERVER [ 'SCRIPT_FILENAME' ] ), trailingslashit ( $wp_path_rel_to_home ) );
$home_path = substr ( $_SERVER [ 'SCRIPT_FILENAME' ], 0 , $pos );
$home_path = trailingslashit ( $home_path );
2007-05-27 20:04:33 -04:00
} else {
$home_path = ABSPATH ;
}
2013-03-12 07:04:14 -04:00
return str_replace ( '\\' , '/' , $home_path );
2007-05-27 20:04:33 -04:00
}
2008-10-01 21:03:26 -04:00
/**
2009-11-05 18:03:48 -05:00
* Returns a listing of all files in the specified folder and all subdirectories up to 100 levels deep .
2020-06-25 05:39:13 -04:00
*
2009-11-05 18:03:48 -05:00
* The depth of the recursiveness can be controlled by the $levels param .
2008-10-01 21:03:26 -04:00
*
2009-11-05 18:03:48 -05:00
* @ since 2.6 . 0
2017-10-10 01:34:49 -04:00
* @ since 4.9 . 0 Added the `$exclusions` parameter .
2008-10-01 21:03:26 -04:00
*
2018-02-09 12:26:31 -05:00
* @ param string $folder Optional . Full path to folder . Default empty .
* @ param int $levels Optional . Levels of folders to follow , Default 100 ( PHP Loop limit ) .
* @ param string [] $exclusions Optional . List of folders and files to skip .
2021-01-04 12:18:04 -05:00
* @ return string [] | false Array of files on success , false on failure .
2008-10-01 21:03:26 -04:00
*/
2017-10-10 01:34:49 -04:00
function list_files ( $folder = '' , $levels = 100 , $exclusions = array () ) {
if ( empty ( $folder ) ) {
2008-06-16 14:35:48 -04:00
return false ;
2017-10-10 01:34:49 -04:00
}
$folder = trailingslashit ( $folder );
2008-06-16 14:35:48 -04:00
2017-10-10 01:34:49 -04:00
if ( ! $levels ) {
2008-06-16 14:35:48 -04:00
return false ;
2017-10-10 01:34:49 -04:00
}
2008-06-16 14:35:48 -04:00
$files = array ();
2017-10-10 01:34:49 -04:00
$dir = @ opendir ( $folder );
2021-04-12 14:19:09 -04:00
2017-10-10 01:34:49 -04:00
if ( $dir ) {
while ( ( $file = readdir ( $dir ) ) !== false ) {
// Skip current and parent folder links.
if ( in_array ( $file , array ( '.' , '..' ), true ) ) {
continue ;
}
// Skip hidden and excluded files.
if ( '.' === $file [ 0 ] || in_array ( $file , $exclusions , true ) ) {
2008-06-16 14:35:48 -04:00
continue ;
2017-10-10 01:34:49 -04:00
}
if ( is_dir ( $folder . $file ) ) {
$files2 = list_files ( $folder . $file , $levels - 1 );
if ( $files2 ) {
2017-11-30 18:11:00 -05:00
$files = array_merge ( $files , $files2 );
2017-10-10 01:34:49 -04:00
} else {
$files [] = $folder . $file . '/' ;
}
2008-06-16 14:35:48 -04:00
} else {
2017-10-10 01:34:49 -04:00
$files [] = $folder . $file ;
2008-06-16 14:35:48 -04:00
}
}
2019-07-09 01:45:58 -04:00
closedir ( $dir );
2008-06-16 14:35:48 -04:00
}
2017-10-10 01:34:49 -04:00
2008-06-16 14:35:48 -04:00
return $files ;
}
2007-05-27 20:04:33 -04:00
File Editors: Introduce sandboxed live editing of PHP files with rollbacks for both themes and plugins.
* Edits to active plugins which cause PHP fatal errors will no longer auto-deactivate the plugin. Supersedes #39766.
* Introduce sandboxed PHP file edits for active themes, preventing accidental whitescreening of a user's site when introducing a fatal error.
* After writing a change to a PHP file for an active theme or plugin, perform loopback requests on the file editor admin screens and the homepage to check for fatal errors. If a fatal error is encountered, roll back the edited file and display the error to the user to fix and try again.
* Introduce a secure way to scrape PHP fatal errors from a site via `wp_start_scraping_edited_file_errors()` and `wp_finalize_scraping_edited_file_errors()`.
* Moves file modifications from `theme-editor.php` and `plugin-editor.php` to common `wp_edit_theme_plugin_file()` function.
* Refactor themes and plugin editors to submit file changes via Ajax instead of doing full page refreshes when JS is available.
* Use `get` method for theme/plugin dropdowns.
* Improve styling of plugin editors, including width of plugin/theme dropdowns.
* Improve notices API for theme/plugin editor JS component.
* Strip common base directory from plugin file list. See #24048.
* Factor out functions to list editable file types in `wp_get_theme_file_editable_extensions()` and `wp_get_plugin_file_editable_extensions()`.
* Scroll to line in editor that has linting error when attempting to save. See #41886.
* Add checkbox to dismiss lint errors to proceed with saving. See #41887.
* Only style the Update File button as disabled instead of actually disabling it for accessibility reasons.
* Ensure that value from CodeMirror is used instead of `textarea` when CodeMirror is present.
* Add "Are you sure?" check when leaving editor when there are unsaved changes.
Supersedes [41560].
See #39766, #24048, #41886.
Props westonruter, Clorith, melchoyce, johnbillion, jjj, jdgrimes, azaozz.
Fixes #21622, #41887.
Built from https://develop.svn.wordpress.org/trunk@41721
git-svn-id: http://core.svn.wordpress.org/trunk@41555 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2017-10-03 20:20:45 -04:00
/**
2020-06-25 05:31:15 -04:00
* Gets the list of file extensions that are editable in plugins .
File Editors: Introduce sandboxed live editing of PHP files with rollbacks for both themes and plugins.
* Edits to active plugins which cause PHP fatal errors will no longer auto-deactivate the plugin. Supersedes #39766.
* Introduce sandboxed PHP file edits for active themes, preventing accidental whitescreening of a user's site when introducing a fatal error.
* After writing a change to a PHP file for an active theme or plugin, perform loopback requests on the file editor admin screens and the homepage to check for fatal errors. If a fatal error is encountered, roll back the edited file and display the error to the user to fix and try again.
* Introduce a secure way to scrape PHP fatal errors from a site via `wp_start_scraping_edited_file_errors()` and `wp_finalize_scraping_edited_file_errors()`.
* Moves file modifications from `theme-editor.php` and `plugin-editor.php` to common `wp_edit_theme_plugin_file()` function.
* Refactor themes and plugin editors to submit file changes via Ajax instead of doing full page refreshes when JS is available.
* Use `get` method for theme/plugin dropdowns.
* Improve styling of plugin editors, including width of plugin/theme dropdowns.
* Improve notices API for theme/plugin editor JS component.
* Strip common base directory from plugin file list. See #24048.
* Factor out functions to list editable file types in `wp_get_theme_file_editable_extensions()` and `wp_get_plugin_file_editable_extensions()`.
* Scroll to line in editor that has linting error when attempting to save. See #41886.
* Add checkbox to dismiss lint errors to proceed with saving. See #41887.
* Only style the Update File button as disabled instead of actually disabling it for accessibility reasons.
* Ensure that value from CodeMirror is used instead of `textarea` when CodeMirror is present.
* Add "Are you sure?" check when leaving editor when there are unsaved changes.
Supersedes [41560].
See #39766, #24048, #41886.
Props westonruter, Clorith, melchoyce, johnbillion, jjj, jdgrimes, azaozz.
Fixes #21622, #41887.
Built from https://develop.svn.wordpress.org/trunk@41721
git-svn-id: http://core.svn.wordpress.org/trunk@41555 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2017-10-03 20:20:45 -04:00
*
* @ since 4.9 . 0
*
2018-03-05 16:50:31 -05:00
* @ param string $plugin Path to the plugin file relative to the plugins directory .
2018-02-09 12:26:31 -05:00
* @ return string [] Array of editable file extensions .
File Editors: Introduce sandboxed live editing of PHP files with rollbacks for both themes and plugins.
* Edits to active plugins which cause PHP fatal errors will no longer auto-deactivate the plugin. Supersedes #39766.
* Introduce sandboxed PHP file edits for active themes, preventing accidental whitescreening of a user's site when introducing a fatal error.
* After writing a change to a PHP file for an active theme or plugin, perform loopback requests on the file editor admin screens and the homepage to check for fatal errors. If a fatal error is encountered, roll back the edited file and display the error to the user to fix and try again.
* Introduce a secure way to scrape PHP fatal errors from a site via `wp_start_scraping_edited_file_errors()` and `wp_finalize_scraping_edited_file_errors()`.
* Moves file modifications from `theme-editor.php` and `plugin-editor.php` to common `wp_edit_theme_plugin_file()` function.
* Refactor themes and plugin editors to submit file changes via Ajax instead of doing full page refreshes when JS is available.
* Use `get` method for theme/plugin dropdowns.
* Improve styling of plugin editors, including width of plugin/theme dropdowns.
* Improve notices API for theme/plugin editor JS component.
* Strip common base directory from plugin file list. See #24048.
* Factor out functions to list editable file types in `wp_get_theme_file_editable_extensions()` and `wp_get_plugin_file_editable_extensions()`.
* Scroll to line in editor that has linting error when attempting to save. See #41886.
* Add checkbox to dismiss lint errors to proceed with saving. See #41887.
* Only style the Update File button as disabled instead of actually disabling it for accessibility reasons.
* Ensure that value from CodeMirror is used instead of `textarea` when CodeMirror is present.
* Add "Are you sure?" check when leaving editor when there are unsaved changes.
Supersedes [41560].
See #39766, #24048, #41886.
Props westonruter, Clorith, melchoyce, johnbillion, jjj, jdgrimes, azaozz.
Fixes #21622, #41887.
Built from https://develop.svn.wordpress.org/trunk@41721
git-svn-id: http://core.svn.wordpress.org/trunk@41555 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2017-10-03 20:20:45 -04:00
*/
function wp_get_plugin_file_editable_extensions ( $plugin ) {
2020-06-25 05:31:15 -04:00
$default_types = array (
File Editors: Introduce sandboxed live editing of PHP files with rollbacks for both themes and plugins.
* Edits to active plugins which cause PHP fatal errors will no longer auto-deactivate the plugin. Supersedes #39766.
* Introduce sandboxed PHP file edits for active themes, preventing accidental whitescreening of a user's site when introducing a fatal error.
* After writing a change to a PHP file for an active theme or plugin, perform loopback requests on the file editor admin screens and the homepage to check for fatal errors. If a fatal error is encountered, roll back the edited file and display the error to the user to fix and try again.
* Introduce a secure way to scrape PHP fatal errors from a site via `wp_start_scraping_edited_file_errors()` and `wp_finalize_scraping_edited_file_errors()`.
* Moves file modifications from `theme-editor.php` and `plugin-editor.php` to common `wp_edit_theme_plugin_file()` function.
* Refactor themes and plugin editors to submit file changes via Ajax instead of doing full page refreshes when JS is available.
* Use `get` method for theme/plugin dropdowns.
* Improve styling of plugin editors, including width of plugin/theme dropdowns.
* Improve notices API for theme/plugin editor JS component.
* Strip common base directory from plugin file list. See #24048.
* Factor out functions to list editable file types in `wp_get_theme_file_editable_extensions()` and `wp_get_plugin_file_editable_extensions()`.
* Scroll to line in editor that has linting error when attempting to save. See #41886.
* Add checkbox to dismiss lint errors to proceed with saving. See #41887.
* Only style the Update File button as disabled instead of actually disabling it for accessibility reasons.
* Ensure that value from CodeMirror is used instead of `textarea` when CodeMirror is present.
* Add "Are you sure?" check when leaving editor when there are unsaved changes.
Supersedes [41560].
See #39766, #24048, #41886.
Props westonruter, Clorith, melchoyce, johnbillion, jjj, jdgrimes, azaozz.
Fixes #21622, #41887.
Built from https://develop.svn.wordpress.org/trunk@41721
git-svn-id: http://core.svn.wordpress.org/trunk@41555 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2017-10-03 20:20:45 -04:00
'bash' ,
'conf' ,
'css' ,
'diff' ,
'htm' ,
'html' ,
'http' ,
'inc' ,
'include' ,
'js' ,
'json' ,
'jsx' ,
'less' ,
'md' ,
'patch' ,
'php' ,
'php3' ,
'php4' ,
'php5' ,
'php7' ,
'phps' ,
'phtml' ,
'sass' ,
'scss' ,
'sh' ,
'sql' ,
'svg' ,
'text' ,
'txt' ,
'xml' ,
'yaml' ,
'yml' ,
);
/**
Administration: Add "File" to Theme/Plugin Editor menu names and relocate "Plugin File Editor" to Tools for block themes.
[52232] relocated the Theme Editor menu item from Appearance to Tools for block themes. This commit relocates the Plugin Editor menu item from Plugins to Tools for block themes for a consistent workflow.
Both the Theme Editor and Plugin Editor menu items are renamed to "Theme File Editor" and "Plugin File Editor" respectively. Why? To better identify their purpose, i.e. to directly edit the code in the theme or plugin files. The rename is not limited to only block themes.
Follow-up to [52232].
Props poena, annezazu, audrasjb, clorith, courane01, costdev, dryanpress, desrosj, hellofromTonya, ipstenu, jameskoster, joen, johnbillion, joyously, manfcarlo, marybaum, pbiron, SergeyBiryukov, walbo, youknowriad.
Fixes #54382.
Built from https://develop.svn.wordpress.org/trunk@52406
git-svn-id: http://core.svn.wordpress.org/trunk@51998 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2021-12-21 15:06:02 -05:00
* Filters the list of file types allowed for editing in the plugin file editor .
File Editors: Introduce sandboxed live editing of PHP files with rollbacks for both themes and plugins.
* Edits to active plugins which cause PHP fatal errors will no longer auto-deactivate the plugin. Supersedes #39766.
* Introduce sandboxed PHP file edits for active themes, preventing accidental whitescreening of a user's site when introducing a fatal error.
* After writing a change to a PHP file for an active theme or plugin, perform loopback requests on the file editor admin screens and the homepage to check for fatal errors. If a fatal error is encountered, roll back the edited file and display the error to the user to fix and try again.
* Introduce a secure way to scrape PHP fatal errors from a site via `wp_start_scraping_edited_file_errors()` and `wp_finalize_scraping_edited_file_errors()`.
* Moves file modifications from `theme-editor.php` and `plugin-editor.php` to common `wp_edit_theme_plugin_file()` function.
* Refactor themes and plugin editors to submit file changes via Ajax instead of doing full page refreshes when JS is available.
* Use `get` method for theme/plugin dropdowns.
* Improve styling of plugin editors, including width of plugin/theme dropdowns.
* Improve notices API for theme/plugin editor JS component.
* Strip common base directory from plugin file list. See #24048.
* Factor out functions to list editable file types in `wp_get_theme_file_editable_extensions()` and `wp_get_plugin_file_editable_extensions()`.
* Scroll to line in editor that has linting error when attempting to save. See #41886.
* Add checkbox to dismiss lint errors to proceed with saving. See #41887.
* Only style the Update File button as disabled instead of actually disabling it for accessibility reasons.
* Ensure that value from CodeMirror is used instead of `textarea` when CodeMirror is present.
* Add "Are you sure?" check when leaving editor when there are unsaved changes.
Supersedes [41560].
See #39766, #24048, #41886.
Props westonruter, Clorith, melchoyce, johnbillion, jjj, jdgrimes, azaozz.
Fixes #21622, #41887.
Built from https://develop.svn.wordpress.org/trunk@41721
git-svn-id: http://core.svn.wordpress.org/trunk@41555 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2017-10-03 20:20:45 -04:00
*
* @ since 2.8 . 0
2018-02-09 11:55:31 -05:00
* @ since 4.9 . 0 Added the `$plugin` parameter .
File Editors: Introduce sandboxed live editing of PHP files with rollbacks for both themes and plugins.
* Edits to active plugins which cause PHP fatal errors will no longer auto-deactivate the plugin. Supersedes #39766.
* Introduce sandboxed PHP file edits for active themes, preventing accidental whitescreening of a user's site when introducing a fatal error.
* After writing a change to a PHP file for an active theme or plugin, perform loopback requests on the file editor admin screens and the homepage to check for fatal errors. If a fatal error is encountered, roll back the edited file and display the error to the user to fix and try again.
* Introduce a secure way to scrape PHP fatal errors from a site via `wp_start_scraping_edited_file_errors()` and `wp_finalize_scraping_edited_file_errors()`.
* Moves file modifications from `theme-editor.php` and `plugin-editor.php` to common `wp_edit_theme_plugin_file()` function.
* Refactor themes and plugin editors to submit file changes via Ajax instead of doing full page refreshes when JS is available.
* Use `get` method for theme/plugin dropdowns.
* Improve styling of plugin editors, including width of plugin/theme dropdowns.
* Improve notices API for theme/plugin editor JS component.
* Strip common base directory from plugin file list. See #24048.
* Factor out functions to list editable file types in `wp_get_theme_file_editable_extensions()` and `wp_get_plugin_file_editable_extensions()`.
* Scroll to line in editor that has linting error when attempting to save. See #41886.
* Add checkbox to dismiss lint errors to proceed with saving. See #41887.
* Only style the Update File button as disabled instead of actually disabling it for accessibility reasons.
* Ensure that value from CodeMirror is used instead of `textarea` when CodeMirror is present.
* Add "Are you sure?" check when leaving editor when there are unsaved changes.
Supersedes [41560].
See #39766, #24048, #41886.
Props westonruter, Clorith, melchoyce, johnbillion, jjj, jdgrimes, azaozz.
Fixes #21622, #41887.
Built from https://develop.svn.wordpress.org/trunk@41721
git-svn-id: http://core.svn.wordpress.org/trunk@41555 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2017-10-03 20:20:45 -04:00
*
2020-06-25 05:31:15 -04:00
* @ param string [] $default_types An array of editable plugin file extensions .
* @ param string $plugin Path to the plugin file relative to the plugins directory .
File Editors: Introduce sandboxed live editing of PHP files with rollbacks for both themes and plugins.
* Edits to active plugins which cause PHP fatal errors will no longer auto-deactivate the plugin. Supersedes #39766.
* Introduce sandboxed PHP file edits for active themes, preventing accidental whitescreening of a user's site when introducing a fatal error.
* After writing a change to a PHP file for an active theme or plugin, perform loopback requests on the file editor admin screens and the homepage to check for fatal errors. If a fatal error is encountered, roll back the edited file and display the error to the user to fix and try again.
* Introduce a secure way to scrape PHP fatal errors from a site via `wp_start_scraping_edited_file_errors()` and `wp_finalize_scraping_edited_file_errors()`.
* Moves file modifications from `theme-editor.php` and `plugin-editor.php` to common `wp_edit_theme_plugin_file()` function.
* Refactor themes and plugin editors to submit file changes via Ajax instead of doing full page refreshes when JS is available.
* Use `get` method for theme/plugin dropdowns.
* Improve styling of plugin editors, including width of plugin/theme dropdowns.
* Improve notices API for theme/plugin editor JS component.
* Strip common base directory from plugin file list. See #24048.
* Factor out functions to list editable file types in `wp_get_theme_file_editable_extensions()` and `wp_get_plugin_file_editable_extensions()`.
* Scroll to line in editor that has linting error when attempting to save. See #41886.
* Add checkbox to dismiss lint errors to proceed with saving. See #41887.
* Only style the Update File button as disabled instead of actually disabling it for accessibility reasons.
* Ensure that value from CodeMirror is used instead of `textarea` when CodeMirror is present.
* Add "Are you sure?" check when leaving editor when there are unsaved changes.
Supersedes [41560].
See #39766, #24048, #41886.
Props westonruter, Clorith, melchoyce, johnbillion, jjj, jdgrimes, azaozz.
Fixes #21622, #41887.
Built from https://develop.svn.wordpress.org/trunk@41721
git-svn-id: http://core.svn.wordpress.org/trunk@41555 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2017-10-03 20:20:45 -04:00
*/
2020-06-25 05:31:15 -04:00
$file_types = ( array ) apply_filters ( 'editable_extensions' , $default_types , $plugin );
File Editors: Introduce sandboxed live editing of PHP files with rollbacks for both themes and plugins.
* Edits to active plugins which cause PHP fatal errors will no longer auto-deactivate the plugin. Supersedes #39766.
* Introduce sandboxed PHP file edits for active themes, preventing accidental whitescreening of a user's site when introducing a fatal error.
* After writing a change to a PHP file for an active theme or plugin, perform loopback requests on the file editor admin screens and the homepage to check for fatal errors. If a fatal error is encountered, roll back the edited file and display the error to the user to fix and try again.
* Introduce a secure way to scrape PHP fatal errors from a site via `wp_start_scraping_edited_file_errors()` and `wp_finalize_scraping_edited_file_errors()`.
* Moves file modifications from `theme-editor.php` and `plugin-editor.php` to common `wp_edit_theme_plugin_file()` function.
* Refactor themes and plugin editors to submit file changes via Ajax instead of doing full page refreshes when JS is available.
* Use `get` method for theme/plugin dropdowns.
* Improve styling of plugin editors, including width of plugin/theme dropdowns.
* Improve notices API for theme/plugin editor JS component.
* Strip common base directory from plugin file list. See #24048.
* Factor out functions to list editable file types in `wp_get_theme_file_editable_extensions()` and `wp_get_plugin_file_editable_extensions()`.
* Scroll to line in editor that has linting error when attempting to save. See #41886.
* Add checkbox to dismiss lint errors to proceed with saving. See #41887.
* Only style the Update File button as disabled instead of actually disabling it for accessibility reasons.
* Ensure that value from CodeMirror is used instead of `textarea` when CodeMirror is present.
* Add "Are you sure?" check when leaving editor when there are unsaved changes.
Supersedes [41560].
See #39766, #24048, #41886.
Props westonruter, Clorith, melchoyce, johnbillion, jjj, jdgrimes, azaozz.
Fixes #21622, #41887.
Built from https://develop.svn.wordpress.org/trunk@41721
git-svn-id: http://core.svn.wordpress.org/trunk@41555 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2017-10-03 20:20:45 -04:00
2020-06-25 05:31:15 -04:00
return $file_types ;
File Editors: Introduce sandboxed live editing of PHP files with rollbacks for both themes and plugins.
* Edits to active plugins which cause PHP fatal errors will no longer auto-deactivate the plugin. Supersedes #39766.
* Introduce sandboxed PHP file edits for active themes, preventing accidental whitescreening of a user's site when introducing a fatal error.
* After writing a change to a PHP file for an active theme or plugin, perform loopback requests on the file editor admin screens and the homepage to check for fatal errors. If a fatal error is encountered, roll back the edited file and display the error to the user to fix and try again.
* Introduce a secure way to scrape PHP fatal errors from a site via `wp_start_scraping_edited_file_errors()` and `wp_finalize_scraping_edited_file_errors()`.
* Moves file modifications from `theme-editor.php` and `plugin-editor.php` to common `wp_edit_theme_plugin_file()` function.
* Refactor themes and plugin editors to submit file changes via Ajax instead of doing full page refreshes when JS is available.
* Use `get` method for theme/plugin dropdowns.
* Improve styling of plugin editors, including width of plugin/theme dropdowns.
* Improve notices API for theme/plugin editor JS component.
* Strip common base directory from plugin file list. See #24048.
* Factor out functions to list editable file types in `wp_get_theme_file_editable_extensions()` and `wp_get_plugin_file_editable_extensions()`.
* Scroll to line in editor that has linting error when attempting to save. See #41886.
* Add checkbox to dismiss lint errors to proceed with saving. See #41887.
* Only style the Update File button as disabled instead of actually disabling it for accessibility reasons.
* Ensure that value from CodeMirror is used instead of `textarea` when CodeMirror is present.
* Add "Are you sure?" check when leaving editor when there are unsaved changes.
Supersedes [41560].
See #39766, #24048, #41886.
Props westonruter, Clorith, melchoyce, johnbillion, jjj, jdgrimes, azaozz.
Fixes #21622, #41887.
Built from https://develop.svn.wordpress.org/trunk@41721
git-svn-id: http://core.svn.wordpress.org/trunk@41555 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2017-10-03 20:20:45 -04:00
}
/**
2020-06-25 05:31:15 -04:00
* Gets the list of file extensions that are editable for a given theme .
*
* @ since 4.9 . 0
File Editors: Introduce sandboxed live editing of PHP files with rollbacks for both themes and plugins.
* Edits to active plugins which cause PHP fatal errors will no longer auto-deactivate the plugin. Supersedes #39766.
* Introduce sandboxed PHP file edits for active themes, preventing accidental whitescreening of a user's site when introducing a fatal error.
* After writing a change to a PHP file for an active theme or plugin, perform loopback requests on the file editor admin screens and the homepage to check for fatal errors. If a fatal error is encountered, roll back the edited file and display the error to the user to fix and try again.
* Introduce a secure way to scrape PHP fatal errors from a site via `wp_start_scraping_edited_file_errors()` and `wp_finalize_scraping_edited_file_errors()`.
* Moves file modifications from `theme-editor.php` and `plugin-editor.php` to common `wp_edit_theme_plugin_file()` function.
* Refactor themes and plugin editors to submit file changes via Ajax instead of doing full page refreshes when JS is available.
* Use `get` method for theme/plugin dropdowns.
* Improve styling of plugin editors, including width of plugin/theme dropdowns.
* Improve notices API for theme/plugin editor JS component.
* Strip common base directory from plugin file list. See #24048.
* Factor out functions to list editable file types in `wp_get_theme_file_editable_extensions()` and `wp_get_plugin_file_editable_extensions()`.
* Scroll to line in editor that has linting error when attempting to save. See #41886.
* Add checkbox to dismiss lint errors to proceed with saving. See #41887.
* Only style the Update File button as disabled instead of actually disabling it for accessibility reasons.
* Ensure that value from CodeMirror is used instead of `textarea` when CodeMirror is present.
* Add "Are you sure?" check when leaving editor when there are unsaved changes.
Supersedes [41560].
See #39766, #24048, #41886.
Props westonruter, Clorith, melchoyce, johnbillion, jjj, jdgrimes, azaozz.
Fixes #21622, #41887.
Built from https://develop.svn.wordpress.org/trunk@41721
git-svn-id: http://core.svn.wordpress.org/trunk@41555 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2017-10-03 20:20:45 -04:00
*
2018-02-09 12:26:31 -05:00
* @ param WP_Theme $theme Theme object .
* @ return string [] Array of editable file extensions .
File Editors: Introduce sandboxed live editing of PHP files with rollbacks for both themes and plugins.
* Edits to active plugins which cause PHP fatal errors will no longer auto-deactivate the plugin. Supersedes #39766.
* Introduce sandboxed PHP file edits for active themes, preventing accidental whitescreening of a user's site when introducing a fatal error.
* After writing a change to a PHP file for an active theme or plugin, perform loopback requests on the file editor admin screens and the homepage to check for fatal errors. If a fatal error is encountered, roll back the edited file and display the error to the user to fix and try again.
* Introduce a secure way to scrape PHP fatal errors from a site via `wp_start_scraping_edited_file_errors()` and `wp_finalize_scraping_edited_file_errors()`.
* Moves file modifications from `theme-editor.php` and `plugin-editor.php` to common `wp_edit_theme_plugin_file()` function.
* Refactor themes and plugin editors to submit file changes via Ajax instead of doing full page refreshes when JS is available.
* Use `get` method for theme/plugin dropdowns.
* Improve styling of plugin editors, including width of plugin/theme dropdowns.
* Improve notices API for theme/plugin editor JS component.
* Strip common base directory from plugin file list. See #24048.
* Factor out functions to list editable file types in `wp_get_theme_file_editable_extensions()` and `wp_get_plugin_file_editable_extensions()`.
* Scroll to line in editor that has linting error when attempting to save. See #41886.
* Add checkbox to dismiss lint errors to proceed with saving. See #41887.
* Only style the Update File button as disabled instead of actually disabling it for accessibility reasons.
* Ensure that value from CodeMirror is used instead of `textarea` when CodeMirror is present.
* Add "Are you sure?" check when leaving editor when there are unsaved changes.
Supersedes [41560].
See #39766, #24048, #41886.
Props westonruter, Clorith, melchoyce, johnbillion, jjj, jdgrimes, azaozz.
Fixes #21622, #41887.
Built from https://develop.svn.wordpress.org/trunk@41721
git-svn-id: http://core.svn.wordpress.org/trunk@41555 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2017-10-03 20:20:45 -04:00
*/
function wp_get_theme_file_editable_extensions ( $theme ) {
$default_types = array (
'bash' ,
'conf' ,
'css' ,
'diff' ,
'htm' ,
'html' ,
'http' ,
'inc' ,
'include' ,
'js' ,
'json' ,
'jsx' ,
'less' ,
'md' ,
'patch' ,
'php' ,
'php3' ,
'php4' ,
'php5' ,
'php7' ,
'phps' ,
'phtml' ,
'sass' ,
'scss' ,
'sh' ,
'sql' ,
'svg' ,
'text' ,
'txt' ,
'xml' ,
'yaml' ,
'yml' ,
);
/**
Administration: Add "File" to Theme/Plugin Editor menu names and relocate "Plugin File Editor" to Tools for block themes.
[52232] relocated the Theme Editor menu item from Appearance to Tools for block themes. This commit relocates the Plugin Editor menu item from Plugins to Tools for block themes for a consistent workflow.
Both the Theme Editor and Plugin Editor menu items are renamed to "Theme File Editor" and "Plugin File Editor" respectively. Why? To better identify their purpose, i.e. to directly edit the code in the theme or plugin files. The rename is not limited to only block themes.
Follow-up to [52232].
Props poena, annezazu, audrasjb, clorith, courane01, costdev, dryanpress, desrosj, hellofromTonya, ipstenu, jameskoster, joen, johnbillion, joyously, manfcarlo, marybaum, pbiron, SergeyBiryukov, walbo, youknowriad.
Fixes #54382.
Built from https://develop.svn.wordpress.org/trunk@52406
git-svn-id: http://core.svn.wordpress.org/trunk@51998 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2021-12-21 15:06:02 -05:00
* Filters the list of file types allowed for editing in the theme file editor .
File Editors: Introduce sandboxed live editing of PHP files with rollbacks for both themes and plugins.
* Edits to active plugins which cause PHP fatal errors will no longer auto-deactivate the plugin. Supersedes #39766.
* Introduce sandboxed PHP file edits for active themes, preventing accidental whitescreening of a user's site when introducing a fatal error.
* After writing a change to a PHP file for an active theme or plugin, perform loopback requests on the file editor admin screens and the homepage to check for fatal errors. If a fatal error is encountered, roll back the edited file and display the error to the user to fix and try again.
* Introduce a secure way to scrape PHP fatal errors from a site via `wp_start_scraping_edited_file_errors()` and `wp_finalize_scraping_edited_file_errors()`.
* Moves file modifications from `theme-editor.php` and `plugin-editor.php` to common `wp_edit_theme_plugin_file()` function.
* Refactor themes and plugin editors to submit file changes via Ajax instead of doing full page refreshes when JS is available.
* Use `get` method for theme/plugin dropdowns.
* Improve styling of plugin editors, including width of plugin/theme dropdowns.
* Improve notices API for theme/plugin editor JS component.
* Strip common base directory from plugin file list. See #24048.
* Factor out functions to list editable file types in `wp_get_theme_file_editable_extensions()` and `wp_get_plugin_file_editable_extensions()`.
* Scroll to line in editor that has linting error when attempting to save. See #41886.
* Add checkbox to dismiss lint errors to proceed with saving. See #41887.
* Only style the Update File button as disabled instead of actually disabling it for accessibility reasons.
* Ensure that value from CodeMirror is used instead of `textarea` when CodeMirror is present.
* Add "Are you sure?" check when leaving editor when there are unsaved changes.
Supersedes [41560].
See #39766, #24048, #41886.
Props westonruter, Clorith, melchoyce, johnbillion, jjj, jdgrimes, azaozz.
Fixes #21622, #41887.
Built from https://develop.svn.wordpress.org/trunk@41721
git-svn-id: http://core.svn.wordpress.org/trunk@41555 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2017-10-03 20:20:45 -04:00
*
* @ since 4.4 . 0
*
2020-06-25 05:31:15 -04:00
* @ param string [] $default_types An array of editable theme file extensions .
2022-01-20 18:53:05 -05:00
* @ param WP_Theme $theme The active theme object .
File Editors: Introduce sandboxed live editing of PHP files with rollbacks for both themes and plugins.
* Edits to active plugins which cause PHP fatal errors will no longer auto-deactivate the plugin. Supersedes #39766.
* Introduce sandboxed PHP file edits for active themes, preventing accidental whitescreening of a user's site when introducing a fatal error.
* After writing a change to a PHP file for an active theme or plugin, perform loopback requests on the file editor admin screens and the homepage to check for fatal errors. If a fatal error is encountered, roll back the edited file and display the error to the user to fix and try again.
* Introduce a secure way to scrape PHP fatal errors from a site via `wp_start_scraping_edited_file_errors()` and `wp_finalize_scraping_edited_file_errors()`.
* Moves file modifications from `theme-editor.php` and `plugin-editor.php` to common `wp_edit_theme_plugin_file()` function.
* Refactor themes and plugin editors to submit file changes via Ajax instead of doing full page refreshes when JS is available.
* Use `get` method for theme/plugin dropdowns.
* Improve styling of plugin editors, including width of plugin/theme dropdowns.
* Improve notices API for theme/plugin editor JS component.
* Strip common base directory from plugin file list. See #24048.
* Factor out functions to list editable file types in `wp_get_theme_file_editable_extensions()` and `wp_get_plugin_file_editable_extensions()`.
* Scroll to line in editor that has linting error when attempting to save. See #41886.
* Add checkbox to dismiss lint errors to proceed with saving. See #41887.
* Only style the Update File button as disabled instead of actually disabling it for accessibility reasons.
* Ensure that value from CodeMirror is used instead of `textarea` when CodeMirror is present.
* Add "Are you sure?" check when leaving editor when there are unsaved changes.
Supersedes [41560].
See #39766, #24048, #41886.
Props westonruter, Clorith, melchoyce, johnbillion, jjj, jdgrimes, azaozz.
Fixes #21622, #41887.
Built from https://develop.svn.wordpress.org/trunk@41721
git-svn-id: http://core.svn.wordpress.org/trunk@41555 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2017-10-03 20:20:45 -04:00
*/
$file_types = apply_filters ( 'wp_theme_editor_filetypes' , $default_types , $theme );
// Ensure that default types are still there.
return array_unique ( array_merge ( $file_types , $default_types ) );
}
/**
2020-06-25 05:39:13 -04:00
* Prints file editor templates ( for plugins and themes ) .
File Editors: Introduce sandboxed live editing of PHP files with rollbacks for both themes and plugins.
* Edits to active plugins which cause PHP fatal errors will no longer auto-deactivate the plugin. Supersedes #39766.
* Introduce sandboxed PHP file edits for active themes, preventing accidental whitescreening of a user's site when introducing a fatal error.
* After writing a change to a PHP file for an active theme or plugin, perform loopback requests on the file editor admin screens and the homepage to check for fatal errors. If a fatal error is encountered, roll back the edited file and display the error to the user to fix and try again.
* Introduce a secure way to scrape PHP fatal errors from a site via `wp_start_scraping_edited_file_errors()` and `wp_finalize_scraping_edited_file_errors()`.
* Moves file modifications from `theme-editor.php` and `plugin-editor.php` to common `wp_edit_theme_plugin_file()` function.
* Refactor themes and plugin editors to submit file changes via Ajax instead of doing full page refreshes when JS is available.
* Use `get` method for theme/plugin dropdowns.
* Improve styling of plugin editors, including width of plugin/theme dropdowns.
* Improve notices API for theme/plugin editor JS component.
* Strip common base directory from plugin file list. See #24048.
* Factor out functions to list editable file types in `wp_get_theme_file_editable_extensions()` and `wp_get_plugin_file_editable_extensions()`.
* Scroll to line in editor that has linting error when attempting to save. See #41886.
* Add checkbox to dismiss lint errors to proceed with saving. See #41887.
* Only style the Update File button as disabled instead of actually disabling it for accessibility reasons.
* Ensure that value from CodeMirror is used instead of `textarea` when CodeMirror is present.
* Add "Are you sure?" check when leaving editor when there are unsaved changes.
Supersedes [41560].
See #39766, #24048, #41886.
Props westonruter, Clorith, melchoyce, johnbillion, jjj, jdgrimes, azaozz.
Fixes #21622, #41887.
Built from https://develop.svn.wordpress.org/trunk@41721
git-svn-id: http://core.svn.wordpress.org/trunk@41555 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2017-10-03 20:20:45 -04:00
*
* @ since 4.9 . 0
*/
function wp_print_file_editor_templates () {
?>
< script type = " text/html " id = " tmpl-wp-file-editor-notice " >
< div class = " notice inline notice- { { data.type || 'info' }} { { data.alt ? 'notice-alt' : '' }} { { data.dismissible ? 'is-dismissible' : '' }} { { data.classes || '' }} " >
< # if ( 'php_error' === data.code ) { #>
< p >
< ? php
printf (
2019-09-02 20:41:05 -04:00
/* translators: 1: Line number, 2: File path. */
File Editors: Introduce sandboxed live editing of PHP files with rollbacks for both themes and plugins.
* Edits to active plugins which cause PHP fatal errors will no longer auto-deactivate the plugin. Supersedes #39766.
* Introduce sandboxed PHP file edits for active themes, preventing accidental whitescreening of a user's site when introducing a fatal error.
* After writing a change to a PHP file for an active theme or plugin, perform loopback requests on the file editor admin screens and the homepage to check for fatal errors. If a fatal error is encountered, roll back the edited file and display the error to the user to fix and try again.
* Introduce a secure way to scrape PHP fatal errors from a site via `wp_start_scraping_edited_file_errors()` and `wp_finalize_scraping_edited_file_errors()`.
* Moves file modifications from `theme-editor.php` and `plugin-editor.php` to common `wp_edit_theme_plugin_file()` function.
* Refactor themes and plugin editors to submit file changes via Ajax instead of doing full page refreshes when JS is available.
* Use `get` method for theme/plugin dropdowns.
* Improve styling of plugin editors, including width of plugin/theme dropdowns.
* Improve notices API for theme/plugin editor JS component.
* Strip common base directory from plugin file list. See #24048.
* Factor out functions to list editable file types in `wp_get_theme_file_editable_extensions()` and `wp_get_plugin_file_editable_extensions()`.
* Scroll to line in editor that has linting error when attempting to save. See #41886.
* Add checkbox to dismiss lint errors to proceed with saving. See #41887.
* Only style the Update File button as disabled instead of actually disabling it for accessibility reasons.
* Ensure that value from CodeMirror is used instead of `textarea` when CodeMirror is present.
* Add "Are you sure?" check when leaving editor when there are unsaved changes.
Supersedes [41560].
See #39766, #24048, #41886.
Props westonruter, Clorith, melchoyce, johnbillion, jjj, jdgrimes, azaozz.
Fixes #21622, #41887.
Built from https://develop.svn.wordpress.org/trunk@41721
git-svn-id: http://core.svn.wordpress.org/trunk@41555 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2017-10-03 20:20:45 -04:00
__ ( 'Your PHP code changes were rolled back due to an error on line %1$s of file %2$s. Please fix and try saving again.' ),
'{{ data.line }}' ,
'{{ data.file }}'
);
?>
</ p >
< pre > {{ data . message }} </ pre >
< # } else if ( 'file_not_writable' === data.code ) { #>
2019-07-24 21:18:55 -04:00
< p >
< ? php
printf (
2019-09-02 20:41:05 -04:00
/* translators: %s: Documentation URL. */
2019-07-24 21:18:55 -04:00
__ ( 'You need to make this file writable before you can save your changes. See <a href="%s">Changing File Permissions</a> for more information.' ),
__ ( 'https://wordpress.org/support/article/changing-file-permissions/' )
);
?>
</ p >
File Editors: Introduce sandboxed live editing of PHP files with rollbacks for both themes and plugins.
* Edits to active plugins which cause PHP fatal errors will no longer auto-deactivate the plugin. Supersedes #39766.
* Introduce sandboxed PHP file edits for active themes, preventing accidental whitescreening of a user's site when introducing a fatal error.
* After writing a change to a PHP file for an active theme or plugin, perform loopback requests on the file editor admin screens and the homepage to check for fatal errors. If a fatal error is encountered, roll back the edited file and display the error to the user to fix and try again.
* Introduce a secure way to scrape PHP fatal errors from a site via `wp_start_scraping_edited_file_errors()` and `wp_finalize_scraping_edited_file_errors()`.
* Moves file modifications from `theme-editor.php` and `plugin-editor.php` to common `wp_edit_theme_plugin_file()` function.
* Refactor themes and plugin editors to submit file changes via Ajax instead of doing full page refreshes when JS is available.
* Use `get` method for theme/plugin dropdowns.
* Improve styling of plugin editors, including width of plugin/theme dropdowns.
* Improve notices API for theme/plugin editor JS component.
* Strip common base directory from plugin file list. See #24048.
* Factor out functions to list editable file types in `wp_get_theme_file_editable_extensions()` and `wp_get_plugin_file_editable_extensions()`.
* Scroll to line in editor that has linting error when attempting to save. See #41886.
* Add checkbox to dismiss lint errors to proceed with saving. See #41887.
* Only style the Update File button as disabled instead of actually disabling it for accessibility reasons.
* Ensure that value from CodeMirror is used instead of `textarea` when CodeMirror is present.
* Add "Are you sure?" check when leaving editor when there are unsaved changes.
Supersedes [41560].
See #39766, #24048, #41886.
Props westonruter, Clorith, melchoyce, johnbillion, jjj, jdgrimes, azaozz.
Fixes #21622, #41887.
Built from https://develop.svn.wordpress.org/trunk@41721
git-svn-id: http://core.svn.wordpress.org/trunk@41555 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2017-10-03 20:20:45 -04:00
< # } else { #>
< p > {{ data . message || data . code }} </ p >
< # if ( 'lint_errors' === data.code ) { #>
< p >
< # var elementId = 'el-' + String( Math.random() ); #>
< input id = " { { elementId }} " type = " checkbox " >
< label for = " { { elementId }} " >< ? php _e ( 'Update anyway, even though it might break your site?' ); ?> </label>
</ p >
< # } #>
< # } #>
< # if ( data.dismissible ) { #>
< button type = " button " class = " notice-dismiss " >< span class = " screen-reader-text " >< ? php _e ( 'Dismiss' ); ?> </span></button>
< # } #>
</ div >
</ script >
< ? php
}
/**
2020-06-25 05:39:13 -04:00
* Attempts to edit a file for a theme or plugin .
File Editors: Introduce sandboxed live editing of PHP files with rollbacks for both themes and plugins.
* Edits to active plugins which cause PHP fatal errors will no longer auto-deactivate the plugin. Supersedes #39766.
* Introduce sandboxed PHP file edits for active themes, preventing accidental whitescreening of a user's site when introducing a fatal error.
* After writing a change to a PHP file for an active theme or plugin, perform loopback requests on the file editor admin screens and the homepage to check for fatal errors. If a fatal error is encountered, roll back the edited file and display the error to the user to fix and try again.
* Introduce a secure way to scrape PHP fatal errors from a site via `wp_start_scraping_edited_file_errors()` and `wp_finalize_scraping_edited_file_errors()`.
* Moves file modifications from `theme-editor.php` and `plugin-editor.php` to common `wp_edit_theme_plugin_file()` function.
* Refactor themes and plugin editors to submit file changes via Ajax instead of doing full page refreshes when JS is available.
* Use `get` method for theme/plugin dropdowns.
* Improve styling of plugin editors, including width of plugin/theme dropdowns.
* Improve notices API for theme/plugin editor JS component.
* Strip common base directory from plugin file list. See #24048.
* Factor out functions to list editable file types in `wp_get_theme_file_editable_extensions()` and `wp_get_plugin_file_editable_extensions()`.
* Scroll to line in editor that has linting error when attempting to save. See #41886.
* Add checkbox to dismiss lint errors to proceed with saving. See #41887.
* Only style the Update File button as disabled instead of actually disabling it for accessibility reasons.
* Ensure that value from CodeMirror is used instead of `textarea` when CodeMirror is present.
* Add "Are you sure?" check when leaving editor when there are unsaved changes.
Supersedes [41560].
See #39766, #24048, #41886.
Props westonruter, Clorith, melchoyce, johnbillion, jjj, jdgrimes, azaozz.
Fixes #21622, #41887.
Built from https://develop.svn.wordpress.org/trunk@41721
git-svn-id: http://core.svn.wordpress.org/trunk@41555 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2017-10-03 20:20:45 -04:00
*
* When editing a PHP file , loopback requests will be made to the admin and the homepage
* to attempt to see if there is a fatal error introduced . If so , the PHP change will be
* reverted .
*
* @ since 4.9 . 0
*
2018-02-09 12:26:31 -05:00
* @ param string [] $args {
File Editors: Introduce sandboxed live editing of PHP files with rollbacks for both themes and plugins.
* Edits to active plugins which cause PHP fatal errors will no longer auto-deactivate the plugin. Supersedes #39766.
* Introduce sandboxed PHP file edits for active themes, preventing accidental whitescreening of a user's site when introducing a fatal error.
* After writing a change to a PHP file for an active theme or plugin, perform loopback requests on the file editor admin screens and the homepage to check for fatal errors. If a fatal error is encountered, roll back the edited file and display the error to the user to fix and try again.
* Introduce a secure way to scrape PHP fatal errors from a site via `wp_start_scraping_edited_file_errors()` and `wp_finalize_scraping_edited_file_errors()`.
* Moves file modifications from `theme-editor.php` and `plugin-editor.php` to common `wp_edit_theme_plugin_file()` function.
* Refactor themes and plugin editors to submit file changes via Ajax instead of doing full page refreshes when JS is available.
* Use `get` method for theme/plugin dropdowns.
* Improve styling of plugin editors, including width of plugin/theme dropdowns.
* Improve notices API for theme/plugin editor JS component.
* Strip common base directory from plugin file list. See #24048.
* Factor out functions to list editable file types in `wp_get_theme_file_editable_extensions()` and `wp_get_plugin_file_editable_extensions()`.
* Scroll to line in editor that has linting error when attempting to save. See #41886.
* Add checkbox to dismiss lint errors to proceed with saving. See #41887.
* Only style the Update File button as disabled instead of actually disabling it for accessibility reasons.
* Ensure that value from CodeMirror is used instead of `textarea` when CodeMirror is present.
* Add "Are you sure?" check when leaving editor when there are unsaved changes.
Supersedes [41560].
See #39766, #24048, #41886.
Props westonruter, Clorith, melchoyce, johnbillion, jjj, jdgrimes, azaozz.
Fixes #21622, #41887.
Built from https://develop.svn.wordpress.org/trunk@41721
git-svn-id: http://core.svn.wordpress.org/trunk@41555 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2017-10-03 20:20:45 -04:00
* Args . Note that all of the arg values are already unslashed . They are , however ,
2018-02-09 11:55:31 -05:00
* coming straight from `$_POST` and are not validated or sanitized in any way .
File Editors: Introduce sandboxed live editing of PHP files with rollbacks for both themes and plugins.
* Edits to active plugins which cause PHP fatal errors will no longer auto-deactivate the plugin. Supersedes #39766.
* Introduce sandboxed PHP file edits for active themes, preventing accidental whitescreening of a user's site when introducing a fatal error.
* After writing a change to a PHP file for an active theme or plugin, perform loopback requests on the file editor admin screens and the homepage to check for fatal errors. If a fatal error is encountered, roll back the edited file and display the error to the user to fix and try again.
* Introduce a secure way to scrape PHP fatal errors from a site via `wp_start_scraping_edited_file_errors()` and `wp_finalize_scraping_edited_file_errors()`.
* Moves file modifications from `theme-editor.php` and `plugin-editor.php` to common `wp_edit_theme_plugin_file()` function.
* Refactor themes and plugin editors to submit file changes via Ajax instead of doing full page refreshes when JS is available.
* Use `get` method for theme/plugin dropdowns.
* Improve styling of plugin editors, including width of plugin/theme dropdowns.
* Improve notices API for theme/plugin editor JS component.
* Strip common base directory from plugin file list. See #24048.
* Factor out functions to list editable file types in `wp_get_theme_file_editable_extensions()` and `wp_get_plugin_file_editable_extensions()`.
* Scroll to line in editor that has linting error when attempting to save. See #41886.
* Add checkbox to dismiss lint errors to proceed with saving. See #41887.
* Only style the Update File button as disabled instead of actually disabling it for accessibility reasons.
* Ensure that value from CodeMirror is used instead of `textarea` when CodeMirror is present.
* Add "Are you sure?" check when leaving editor when there are unsaved changes.
Supersedes [41560].
See #39766, #24048, #41886.
Props westonruter, Clorith, melchoyce, johnbillion, jjj, jdgrimes, azaozz.
Fixes #21622, #41887.
Built from https://develop.svn.wordpress.org/trunk@41721
git-svn-id: http://core.svn.wordpress.org/trunk@41555 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2017-10-03 20:20:45 -04:00
*
* @ type string $file Relative path to file .
2018-03-05 16:50:31 -05:00
* @ type string $plugin Path to the plugin file relative to the plugins directory .
File Editors: Introduce sandboxed live editing of PHP files with rollbacks for both themes and plugins.
* Edits to active plugins which cause PHP fatal errors will no longer auto-deactivate the plugin. Supersedes #39766.
* Introduce sandboxed PHP file edits for active themes, preventing accidental whitescreening of a user's site when introducing a fatal error.
* After writing a change to a PHP file for an active theme or plugin, perform loopback requests on the file editor admin screens and the homepage to check for fatal errors. If a fatal error is encountered, roll back the edited file and display the error to the user to fix and try again.
* Introduce a secure way to scrape PHP fatal errors from a site via `wp_start_scraping_edited_file_errors()` and `wp_finalize_scraping_edited_file_errors()`.
* Moves file modifications from `theme-editor.php` and `plugin-editor.php` to common `wp_edit_theme_plugin_file()` function.
* Refactor themes and plugin editors to submit file changes via Ajax instead of doing full page refreshes when JS is available.
* Use `get` method for theme/plugin dropdowns.
* Improve styling of plugin editors, including width of plugin/theme dropdowns.
* Improve notices API for theme/plugin editor JS component.
* Strip common base directory from plugin file list. See #24048.
* Factor out functions to list editable file types in `wp_get_theme_file_editable_extensions()` and `wp_get_plugin_file_editable_extensions()`.
* Scroll to line in editor that has linting error when attempting to save. See #41886.
* Add checkbox to dismiss lint errors to proceed with saving. See #41887.
* Only style the Update File button as disabled instead of actually disabling it for accessibility reasons.
* Ensure that value from CodeMirror is used instead of `textarea` when CodeMirror is present.
* Add "Are you sure?" check when leaving editor when there are unsaved changes.
Supersedes [41560].
See #39766, #24048, #41886.
Props westonruter, Clorith, melchoyce, johnbillion, jjj, jdgrimes, azaozz.
Fixes #21622, #41887.
Built from https://develop.svn.wordpress.org/trunk@41721
git-svn-id: http://core.svn.wordpress.org/trunk@41555 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2017-10-03 20:20:45 -04:00
* @ type string $theme Theme being edited .
* @ type string $newcontent New content for the file .
* @ type string $nonce Nonce .
* }
* @ return true | WP_Error True on success or `WP_Error` on failure .
*/
function wp_edit_theme_plugin_file ( $args ) {
if ( empty ( $args [ 'file' ] ) ) {
return new WP_Error ( 'missing_file' );
}
2021-04-12 14:19:09 -04:00
if ( 0 !== validate_file ( $args [ 'file' ] ) ) {
File Editors: Introduce sandboxed live editing of PHP files with rollbacks for both themes and plugins.
* Edits to active plugins which cause PHP fatal errors will no longer auto-deactivate the plugin. Supersedes #39766.
* Introduce sandboxed PHP file edits for active themes, preventing accidental whitescreening of a user's site when introducing a fatal error.
* After writing a change to a PHP file for an active theme or plugin, perform loopback requests on the file editor admin screens and the homepage to check for fatal errors. If a fatal error is encountered, roll back the edited file and display the error to the user to fix and try again.
* Introduce a secure way to scrape PHP fatal errors from a site via `wp_start_scraping_edited_file_errors()` and `wp_finalize_scraping_edited_file_errors()`.
* Moves file modifications from `theme-editor.php` and `plugin-editor.php` to common `wp_edit_theme_plugin_file()` function.
* Refactor themes and plugin editors to submit file changes via Ajax instead of doing full page refreshes when JS is available.
* Use `get` method for theme/plugin dropdowns.
* Improve styling of plugin editors, including width of plugin/theme dropdowns.
* Improve notices API for theme/plugin editor JS component.
* Strip common base directory from plugin file list. See #24048.
* Factor out functions to list editable file types in `wp_get_theme_file_editable_extensions()` and `wp_get_plugin_file_editable_extensions()`.
* Scroll to line in editor that has linting error when attempting to save. See #41886.
* Add checkbox to dismiss lint errors to proceed with saving. See #41887.
* Only style the Update File button as disabled instead of actually disabling it for accessibility reasons.
* Ensure that value from CodeMirror is used instead of `textarea` when CodeMirror is present.
* Add "Are you sure?" check when leaving editor when there are unsaved changes.
Supersedes [41560].
See #39766, #24048, #41886.
Props westonruter, Clorith, melchoyce, johnbillion, jjj, jdgrimes, azaozz.
Fixes #21622, #41887.
Built from https://develop.svn.wordpress.org/trunk@41721
git-svn-id: http://core.svn.wordpress.org/trunk@41555 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2017-10-03 20:20:45 -04:00
return new WP_Error ( 'bad_file' );
}
if ( ! isset ( $args [ 'newcontent' ] ) ) {
return new WP_Error ( 'missing_content' );
}
if ( ! isset ( $args [ 'nonce' ] ) ) {
return new WP_Error ( 'missing_nonce' );
}
2021-04-12 14:19:09 -04:00
$file = $args [ 'file' ];
$content = $args [ 'newcontent' ];
2017-11-30 18:11:00 -05:00
$plugin = null ;
$theme = null ;
File Editors: Introduce sandboxed live editing of PHP files with rollbacks for both themes and plugins.
* Edits to active plugins which cause PHP fatal errors will no longer auto-deactivate the plugin. Supersedes #39766.
* Introduce sandboxed PHP file edits for active themes, preventing accidental whitescreening of a user's site when introducing a fatal error.
* After writing a change to a PHP file for an active theme or plugin, perform loopback requests on the file editor admin screens and the homepage to check for fatal errors. If a fatal error is encountered, roll back the edited file and display the error to the user to fix and try again.
* Introduce a secure way to scrape PHP fatal errors from a site via `wp_start_scraping_edited_file_errors()` and `wp_finalize_scraping_edited_file_errors()`.
* Moves file modifications from `theme-editor.php` and `plugin-editor.php` to common `wp_edit_theme_plugin_file()` function.
* Refactor themes and plugin editors to submit file changes via Ajax instead of doing full page refreshes when JS is available.
* Use `get` method for theme/plugin dropdowns.
* Improve styling of plugin editors, including width of plugin/theme dropdowns.
* Improve notices API for theme/plugin editor JS component.
* Strip common base directory from plugin file list. See #24048.
* Factor out functions to list editable file types in `wp_get_theme_file_editable_extensions()` and `wp_get_plugin_file_editable_extensions()`.
* Scroll to line in editor that has linting error when attempting to save. See #41886.
* Add checkbox to dismiss lint errors to proceed with saving. See #41887.
* Only style the Update File button as disabled instead of actually disabling it for accessibility reasons.
* Ensure that value from CodeMirror is used instead of `textarea` when CodeMirror is present.
* Add "Are you sure?" check when leaving editor when there are unsaved changes.
Supersedes [41560].
See #39766, #24048, #41886.
Props westonruter, Clorith, melchoyce, johnbillion, jjj, jdgrimes, azaozz.
Fixes #21622, #41887.
Built from https://develop.svn.wordpress.org/trunk@41721
git-svn-id: http://core.svn.wordpress.org/trunk@41555 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2017-10-03 20:20:45 -04:00
$real_file = null ;
2021-04-12 14:19:09 -04:00
File Editors: Introduce sandboxed live editing of PHP files with rollbacks for both themes and plugins.
* Edits to active plugins which cause PHP fatal errors will no longer auto-deactivate the plugin. Supersedes #39766.
* Introduce sandboxed PHP file edits for active themes, preventing accidental whitescreening of a user's site when introducing a fatal error.
* After writing a change to a PHP file for an active theme or plugin, perform loopback requests on the file editor admin screens and the homepage to check for fatal errors. If a fatal error is encountered, roll back the edited file and display the error to the user to fix and try again.
* Introduce a secure way to scrape PHP fatal errors from a site via `wp_start_scraping_edited_file_errors()` and `wp_finalize_scraping_edited_file_errors()`.
* Moves file modifications from `theme-editor.php` and `plugin-editor.php` to common `wp_edit_theme_plugin_file()` function.
* Refactor themes and plugin editors to submit file changes via Ajax instead of doing full page refreshes when JS is available.
* Use `get` method for theme/plugin dropdowns.
* Improve styling of plugin editors, including width of plugin/theme dropdowns.
* Improve notices API for theme/plugin editor JS component.
* Strip common base directory from plugin file list. See #24048.
* Factor out functions to list editable file types in `wp_get_theme_file_editable_extensions()` and `wp_get_plugin_file_editable_extensions()`.
* Scroll to line in editor that has linting error when attempting to save. See #41886.
* Add checkbox to dismiss lint errors to proceed with saving. See #41887.
* Only style the Update File button as disabled instead of actually disabling it for accessibility reasons.
* Ensure that value from CodeMirror is used instead of `textarea` when CodeMirror is present.
* Add "Are you sure?" check when leaving editor when there are unsaved changes.
Supersedes [41560].
See #39766, #24048, #41886.
Props westonruter, Clorith, melchoyce, johnbillion, jjj, jdgrimes, azaozz.
Fixes #21622, #41887.
Built from https://develop.svn.wordpress.org/trunk@41721
git-svn-id: http://core.svn.wordpress.org/trunk@41555 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2017-10-03 20:20:45 -04:00
if ( ! empty ( $args [ 'plugin' ] ) ) {
$plugin = $args [ 'plugin' ];
if ( ! current_user_can ( 'edit_plugins' ) ) {
return new WP_Error ( 'unauthorized' , __ ( 'Sorry, you are not allowed to edit plugins for this site.' ) );
}
if ( ! wp_verify_nonce ( $args [ 'nonce' ], 'edit-plugin_' . $file ) ) {
return new WP_Error ( 'nonce_failure' );
}
if ( ! array_key_exists ( $plugin , get_plugins () ) ) {
return new WP_Error ( 'invalid_plugin' );
}
if ( 0 !== validate_file ( $file , get_plugin_files ( $plugin ) ) ) {
return new WP_Error ( 'bad_plugin_file_path' , __ ( 'Sorry, that file cannot be edited.' ) );
}
$editable_extensions = wp_get_plugin_file_editable_extensions ( $plugin );
$real_file = WP_PLUGIN_DIR . '/' . $file ;
$is_active = in_array (
$plugin ,
( array ) get_option ( 'active_plugins' , array () ),
true
);
} elseif ( ! empty ( $args [ 'theme' ] ) ) {
$stylesheet = $args [ 'theme' ];
2021-04-12 14:19:09 -04:00
File Editors: Introduce sandboxed live editing of PHP files with rollbacks for both themes and plugins.
* Edits to active plugins which cause PHP fatal errors will no longer auto-deactivate the plugin. Supersedes #39766.
* Introduce sandboxed PHP file edits for active themes, preventing accidental whitescreening of a user's site when introducing a fatal error.
* After writing a change to a PHP file for an active theme or plugin, perform loopback requests on the file editor admin screens and the homepage to check for fatal errors. If a fatal error is encountered, roll back the edited file and display the error to the user to fix and try again.
* Introduce a secure way to scrape PHP fatal errors from a site via `wp_start_scraping_edited_file_errors()` and `wp_finalize_scraping_edited_file_errors()`.
* Moves file modifications from `theme-editor.php` and `plugin-editor.php` to common `wp_edit_theme_plugin_file()` function.
* Refactor themes and plugin editors to submit file changes via Ajax instead of doing full page refreshes when JS is available.
* Use `get` method for theme/plugin dropdowns.
* Improve styling of plugin editors, including width of plugin/theme dropdowns.
* Improve notices API for theme/plugin editor JS component.
* Strip common base directory from plugin file list. See #24048.
* Factor out functions to list editable file types in `wp_get_theme_file_editable_extensions()` and `wp_get_plugin_file_editable_extensions()`.
* Scroll to line in editor that has linting error when attempting to save. See #41886.
* Add checkbox to dismiss lint errors to proceed with saving. See #41887.
* Only style the Update File button as disabled instead of actually disabling it for accessibility reasons.
* Ensure that value from CodeMirror is used instead of `textarea` when CodeMirror is present.
* Add "Are you sure?" check when leaving editor when there are unsaved changes.
Supersedes [41560].
See #39766, #24048, #41886.
Props westonruter, Clorith, melchoyce, johnbillion, jjj, jdgrimes, azaozz.
Fixes #21622, #41887.
Built from https://develop.svn.wordpress.org/trunk@41721
git-svn-id: http://core.svn.wordpress.org/trunk@41555 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2017-10-03 20:20:45 -04:00
if ( 0 !== validate_file ( $stylesheet ) ) {
return new WP_Error ( 'bad_theme_path' );
}
if ( ! current_user_can ( 'edit_themes' ) ) {
return new WP_Error ( 'unauthorized' , __ ( 'Sorry, you are not allowed to edit templates for this site.' ) );
}
$theme = wp_get_theme ( $stylesheet );
if ( ! $theme -> exists () ) {
return new WP_Error ( 'non_existent_theme' , __ ( 'The requested theme does not exist.' ) );
}
2017-11-26 22:44:47 -05:00
if ( ! wp_verify_nonce ( $args [ 'nonce' ], 'edit-theme_' . $stylesheet . '_' . $file ) ) {
File Editors: Introduce sandboxed live editing of PHP files with rollbacks for both themes and plugins.
* Edits to active plugins which cause PHP fatal errors will no longer auto-deactivate the plugin. Supersedes #39766.
* Introduce sandboxed PHP file edits for active themes, preventing accidental whitescreening of a user's site when introducing a fatal error.
* After writing a change to a PHP file for an active theme or plugin, perform loopback requests on the file editor admin screens and the homepage to check for fatal errors. If a fatal error is encountered, roll back the edited file and display the error to the user to fix and try again.
* Introduce a secure way to scrape PHP fatal errors from a site via `wp_start_scraping_edited_file_errors()` and `wp_finalize_scraping_edited_file_errors()`.
* Moves file modifications from `theme-editor.php` and `plugin-editor.php` to common `wp_edit_theme_plugin_file()` function.
* Refactor themes and plugin editors to submit file changes via Ajax instead of doing full page refreshes when JS is available.
* Use `get` method for theme/plugin dropdowns.
* Improve styling of plugin editors, including width of plugin/theme dropdowns.
* Improve notices API for theme/plugin editor JS component.
* Strip common base directory from plugin file list. See #24048.
* Factor out functions to list editable file types in `wp_get_theme_file_editable_extensions()` and `wp_get_plugin_file_editable_extensions()`.
* Scroll to line in editor that has linting error when attempting to save. See #41886.
* Add checkbox to dismiss lint errors to proceed with saving. See #41887.
* Only style the Update File button as disabled instead of actually disabling it for accessibility reasons.
* Ensure that value from CodeMirror is used instead of `textarea` when CodeMirror is present.
* Add "Are you sure?" check when leaving editor when there are unsaved changes.
Supersedes [41560].
See #39766, #24048, #41886.
Props westonruter, Clorith, melchoyce, johnbillion, jjj, jdgrimes, azaozz.
Fixes #21622, #41887.
Built from https://develop.svn.wordpress.org/trunk@41721
git-svn-id: http://core.svn.wordpress.org/trunk@41555 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2017-10-03 20:20:45 -04:00
return new WP_Error ( 'nonce_failure' );
}
if ( $theme -> errors () && 'theme_no_stylesheet' === $theme -> errors () -> get_error_code () ) {
return new WP_Error (
'theme_no_stylesheet' ,
__ ( 'The requested theme does not exist.' ) . ' ' . $theme -> errors () -> get_error_message ()
);
}
$editable_extensions = wp_get_theme_file_editable_extensions ( $theme );
$allowed_files = array ();
foreach ( $editable_extensions as $type ) {
switch ( $type ) {
case 'php' :
2017-11-02 19:05:48 -04:00
$allowed_files = array_merge ( $allowed_files , $theme -> get_files ( 'php' , - 1 ) );
File Editors: Introduce sandboxed live editing of PHP files with rollbacks for both themes and plugins.
* Edits to active plugins which cause PHP fatal errors will no longer auto-deactivate the plugin. Supersedes #39766.
* Introduce sandboxed PHP file edits for active themes, preventing accidental whitescreening of a user's site when introducing a fatal error.
* After writing a change to a PHP file for an active theme or plugin, perform loopback requests on the file editor admin screens and the homepage to check for fatal errors. If a fatal error is encountered, roll back the edited file and display the error to the user to fix and try again.
* Introduce a secure way to scrape PHP fatal errors from a site via `wp_start_scraping_edited_file_errors()` and `wp_finalize_scraping_edited_file_errors()`.
* Moves file modifications from `theme-editor.php` and `plugin-editor.php` to common `wp_edit_theme_plugin_file()` function.
* Refactor themes and plugin editors to submit file changes via Ajax instead of doing full page refreshes when JS is available.
* Use `get` method for theme/plugin dropdowns.
* Improve styling of plugin editors, including width of plugin/theme dropdowns.
* Improve notices API for theme/plugin editor JS component.
* Strip common base directory from plugin file list. See #24048.
* Factor out functions to list editable file types in `wp_get_theme_file_editable_extensions()` and `wp_get_plugin_file_editable_extensions()`.
* Scroll to line in editor that has linting error when attempting to save. See #41886.
* Add checkbox to dismiss lint errors to proceed with saving. See #41887.
* Only style the Update File button as disabled instead of actually disabling it for accessibility reasons.
* Ensure that value from CodeMirror is used instead of `textarea` when CodeMirror is present.
* Add "Are you sure?" check when leaving editor when there are unsaved changes.
Supersedes [41560].
See #39766, #24048, #41886.
Props westonruter, Clorith, melchoyce, johnbillion, jjj, jdgrimes, azaozz.
Fixes #21622, #41887.
Built from https://develop.svn.wordpress.org/trunk@41721
git-svn-id: http://core.svn.wordpress.org/trunk@41555 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2017-10-03 20:20:45 -04:00
break ;
case 'css' :
2017-11-30 18:11:00 -05:00
$style_files = $theme -> get_files ( 'css' , - 1 );
File Editors: Introduce sandboxed live editing of PHP files with rollbacks for both themes and plugins.
* Edits to active plugins which cause PHP fatal errors will no longer auto-deactivate the plugin. Supersedes #39766.
* Introduce sandboxed PHP file edits for active themes, preventing accidental whitescreening of a user's site when introducing a fatal error.
* After writing a change to a PHP file for an active theme or plugin, perform loopback requests on the file editor admin screens and the homepage to check for fatal errors. If a fatal error is encountered, roll back the edited file and display the error to the user to fix and try again.
* Introduce a secure way to scrape PHP fatal errors from a site via `wp_start_scraping_edited_file_errors()` and `wp_finalize_scraping_edited_file_errors()`.
* Moves file modifications from `theme-editor.php` and `plugin-editor.php` to common `wp_edit_theme_plugin_file()` function.
* Refactor themes and plugin editors to submit file changes via Ajax instead of doing full page refreshes when JS is available.
* Use `get` method for theme/plugin dropdowns.
* Improve styling of plugin editors, including width of plugin/theme dropdowns.
* Improve notices API for theme/plugin editor JS component.
* Strip common base directory from plugin file list. See #24048.
* Factor out functions to list editable file types in `wp_get_theme_file_editable_extensions()` and `wp_get_plugin_file_editable_extensions()`.
* Scroll to line in editor that has linting error when attempting to save. See #41886.
* Add checkbox to dismiss lint errors to proceed with saving. See #41887.
* Only style the Update File button as disabled instead of actually disabling it for accessibility reasons.
* Ensure that value from CodeMirror is used instead of `textarea` when CodeMirror is present.
* Add "Are you sure?" check when leaving editor when there are unsaved changes.
Supersedes [41560].
See #39766, #24048, #41886.
Props westonruter, Clorith, melchoyce, johnbillion, jjj, jdgrimes, azaozz.
Fixes #21622, #41887.
Built from https://develop.svn.wordpress.org/trunk@41721
git-svn-id: http://core.svn.wordpress.org/trunk@41555 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2017-10-03 20:20:45 -04:00
$allowed_files [ 'style.css' ] = $style_files [ 'style.css' ];
2017-11-30 18:11:00 -05:00
$allowed_files = array_merge ( $allowed_files , $style_files );
File Editors: Introduce sandboxed live editing of PHP files with rollbacks for both themes and plugins.
* Edits to active plugins which cause PHP fatal errors will no longer auto-deactivate the plugin. Supersedes #39766.
* Introduce sandboxed PHP file edits for active themes, preventing accidental whitescreening of a user's site when introducing a fatal error.
* After writing a change to a PHP file for an active theme or plugin, perform loopback requests on the file editor admin screens and the homepage to check for fatal errors. If a fatal error is encountered, roll back the edited file and display the error to the user to fix and try again.
* Introduce a secure way to scrape PHP fatal errors from a site via `wp_start_scraping_edited_file_errors()` and `wp_finalize_scraping_edited_file_errors()`.
* Moves file modifications from `theme-editor.php` and `plugin-editor.php` to common `wp_edit_theme_plugin_file()` function.
* Refactor themes and plugin editors to submit file changes via Ajax instead of doing full page refreshes when JS is available.
* Use `get` method for theme/plugin dropdowns.
* Improve styling of plugin editors, including width of plugin/theme dropdowns.
* Improve notices API for theme/plugin editor JS component.
* Strip common base directory from plugin file list. See #24048.
* Factor out functions to list editable file types in `wp_get_theme_file_editable_extensions()` and `wp_get_plugin_file_editable_extensions()`.
* Scroll to line in editor that has linting error when attempting to save. See #41886.
* Add checkbox to dismiss lint errors to proceed with saving. See #41887.
* Only style the Update File button as disabled instead of actually disabling it for accessibility reasons.
* Ensure that value from CodeMirror is used instead of `textarea` when CodeMirror is present.
* Add "Are you sure?" check when leaving editor when there are unsaved changes.
Supersedes [41560].
See #39766, #24048, #41886.
Props westonruter, Clorith, melchoyce, johnbillion, jjj, jdgrimes, azaozz.
Fixes #21622, #41887.
Built from https://develop.svn.wordpress.org/trunk@41721
git-svn-id: http://core.svn.wordpress.org/trunk@41555 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2017-10-03 20:20:45 -04:00
break ;
default :
2017-11-02 19:05:48 -04:00
$allowed_files = array_merge ( $allowed_files , $theme -> get_files ( $type , - 1 ) );
File Editors: Introduce sandboxed live editing of PHP files with rollbacks for both themes and plugins.
* Edits to active plugins which cause PHP fatal errors will no longer auto-deactivate the plugin. Supersedes #39766.
* Introduce sandboxed PHP file edits for active themes, preventing accidental whitescreening of a user's site when introducing a fatal error.
* After writing a change to a PHP file for an active theme or plugin, perform loopback requests on the file editor admin screens and the homepage to check for fatal errors. If a fatal error is encountered, roll back the edited file and display the error to the user to fix and try again.
* Introduce a secure way to scrape PHP fatal errors from a site via `wp_start_scraping_edited_file_errors()` and `wp_finalize_scraping_edited_file_errors()`.
* Moves file modifications from `theme-editor.php` and `plugin-editor.php` to common `wp_edit_theme_plugin_file()` function.
* Refactor themes and plugin editors to submit file changes via Ajax instead of doing full page refreshes when JS is available.
* Use `get` method for theme/plugin dropdowns.
* Improve styling of plugin editors, including width of plugin/theme dropdowns.
* Improve notices API for theme/plugin editor JS component.
* Strip common base directory from plugin file list. See #24048.
* Factor out functions to list editable file types in `wp_get_theme_file_editable_extensions()` and `wp_get_plugin_file_editable_extensions()`.
* Scroll to line in editor that has linting error when attempting to save. See #41886.
* Add checkbox to dismiss lint errors to proceed with saving. See #41887.
* Only style the Update File button as disabled instead of actually disabling it for accessibility reasons.
* Ensure that value from CodeMirror is used instead of `textarea` when CodeMirror is present.
* Add "Are you sure?" check when leaving editor when there are unsaved changes.
Supersedes [41560].
See #39766, #24048, #41886.
Props westonruter, Clorith, melchoyce, johnbillion, jjj, jdgrimes, azaozz.
Fixes #21622, #41887.
Built from https://develop.svn.wordpress.org/trunk@41721
git-svn-id: http://core.svn.wordpress.org/trunk@41555 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2017-10-03 20:20:45 -04:00
break ;
}
}
2020-01-17 19:54:04 -05:00
// Compare based on relative paths.
2017-11-26 22:28:48 -05:00
if ( 0 !== validate_file ( $file , array_keys ( $allowed_files ) ) ) {
File Editors: Introduce sandboxed live editing of PHP files with rollbacks for both themes and plugins.
* Edits to active plugins which cause PHP fatal errors will no longer auto-deactivate the plugin. Supersedes #39766.
* Introduce sandboxed PHP file edits for active themes, preventing accidental whitescreening of a user's site when introducing a fatal error.
* After writing a change to a PHP file for an active theme or plugin, perform loopback requests on the file editor admin screens and the homepage to check for fatal errors. If a fatal error is encountered, roll back the edited file and display the error to the user to fix and try again.
* Introduce a secure way to scrape PHP fatal errors from a site via `wp_start_scraping_edited_file_errors()` and `wp_finalize_scraping_edited_file_errors()`.
* Moves file modifications from `theme-editor.php` and `plugin-editor.php` to common `wp_edit_theme_plugin_file()` function.
* Refactor themes and plugin editors to submit file changes via Ajax instead of doing full page refreshes when JS is available.
* Use `get` method for theme/plugin dropdowns.
* Improve styling of plugin editors, including width of plugin/theme dropdowns.
* Improve notices API for theme/plugin editor JS component.
* Strip common base directory from plugin file list. See #24048.
* Factor out functions to list editable file types in `wp_get_theme_file_editable_extensions()` and `wp_get_plugin_file_editable_extensions()`.
* Scroll to line in editor that has linting error when attempting to save. See #41886.
* Add checkbox to dismiss lint errors to proceed with saving. See #41887.
* Only style the Update File button as disabled instead of actually disabling it for accessibility reasons.
* Ensure that value from CodeMirror is used instead of `textarea` when CodeMirror is present.
* Add "Are you sure?" check when leaving editor when there are unsaved changes.
Supersedes [41560].
See #39766, #24048, #41886.
Props westonruter, Clorith, melchoyce, johnbillion, jjj, jdgrimes, azaozz.
Fixes #21622, #41887.
Built from https://develop.svn.wordpress.org/trunk@41721
git-svn-id: http://core.svn.wordpress.org/trunk@41555 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2017-10-03 20:20:45 -04:00
return new WP_Error ( 'disallowed_theme_file' , __ ( 'Sorry, that file cannot be edited.' ) );
}
2017-11-26 22:44:47 -05:00
$real_file = $theme -> get_stylesheet_directory () . '/' . $file ;
File Editors: Introduce sandboxed live editing of PHP files with rollbacks for both themes and plugins.
* Edits to active plugins which cause PHP fatal errors will no longer auto-deactivate the plugin. Supersedes #39766.
* Introduce sandboxed PHP file edits for active themes, preventing accidental whitescreening of a user's site when introducing a fatal error.
* After writing a change to a PHP file for an active theme or plugin, perform loopback requests on the file editor admin screens and the homepage to check for fatal errors. If a fatal error is encountered, roll back the edited file and display the error to the user to fix and try again.
* Introduce a secure way to scrape PHP fatal errors from a site via `wp_start_scraping_edited_file_errors()` and `wp_finalize_scraping_edited_file_errors()`.
* Moves file modifications from `theme-editor.php` and `plugin-editor.php` to common `wp_edit_theme_plugin_file()` function.
* Refactor themes and plugin editors to submit file changes via Ajax instead of doing full page refreshes when JS is available.
* Use `get` method for theme/plugin dropdowns.
* Improve styling of plugin editors, including width of plugin/theme dropdowns.
* Improve notices API for theme/plugin editor JS component.
* Strip common base directory from plugin file list. See #24048.
* Factor out functions to list editable file types in `wp_get_theme_file_editable_extensions()` and `wp_get_plugin_file_editable_extensions()`.
* Scroll to line in editor that has linting error when attempting to save. See #41886.
* Add checkbox to dismiss lint errors to proceed with saving. See #41887.
* Only style the Update File button as disabled instead of actually disabling it for accessibility reasons.
* Ensure that value from CodeMirror is used instead of `textarea` when CodeMirror is present.
* Add "Are you sure?" check when leaving editor when there are unsaved changes.
Supersedes [41560].
See #39766, #24048, #41886.
Props westonruter, Clorith, melchoyce, johnbillion, jjj, jdgrimes, azaozz.
Fixes #21622, #41887.
Built from https://develop.svn.wordpress.org/trunk@41721
git-svn-id: http://core.svn.wordpress.org/trunk@41555 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2017-10-03 20:20:45 -04:00
$is_active = ( get_stylesheet () === $stylesheet || get_template () === $stylesheet );
2017-11-26 22:44:47 -05:00
File Editors: Introduce sandboxed live editing of PHP files with rollbacks for both themes and plugins.
* Edits to active plugins which cause PHP fatal errors will no longer auto-deactivate the plugin. Supersedes #39766.
* Introduce sandboxed PHP file edits for active themes, preventing accidental whitescreening of a user's site when introducing a fatal error.
* After writing a change to a PHP file for an active theme or plugin, perform loopback requests on the file editor admin screens and the homepage to check for fatal errors. If a fatal error is encountered, roll back the edited file and display the error to the user to fix and try again.
* Introduce a secure way to scrape PHP fatal errors from a site via `wp_start_scraping_edited_file_errors()` and `wp_finalize_scraping_edited_file_errors()`.
* Moves file modifications from `theme-editor.php` and `plugin-editor.php` to common `wp_edit_theme_plugin_file()` function.
* Refactor themes and plugin editors to submit file changes via Ajax instead of doing full page refreshes when JS is available.
* Use `get` method for theme/plugin dropdowns.
* Improve styling of plugin editors, including width of plugin/theme dropdowns.
* Improve notices API for theme/plugin editor JS component.
* Strip common base directory from plugin file list. See #24048.
* Factor out functions to list editable file types in `wp_get_theme_file_editable_extensions()` and `wp_get_plugin_file_editable_extensions()`.
* Scroll to line in editor that has linting error when attempting to save. See #41886.
* Add checkbox to dismiss lint errors to proceed with saving. See #41887.
* Only style the Update File button as disabled instead of actually disabling it for accessibility reasons.
* Ensure that value from CodeMirror is used instead of `textarea` when CodeMirror is present.
* Add "Are you sure?" check when leaving editor when there are unsaved changes.
Supersedes [41560].
See #39766, #24048, #41886.
Props westonruter, Clorith, melchoyce, johnbillion, jjj, jdgrimes, azaozz.
Fixes #21622, #41887.
Built from https://develop.svn.wordpress.org/trunk@41721
git-svn-id: http://core.svn.wordpress.org/trunk@41555 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2017-10-03 20:20:45 -04:00
} else {
return new WP_Error ( 'missing_theme_or_plugin' );
}
// Ensure file is real.
if ( ! is_file ( $real_file ) ) {
2019-05-26 08:35:51 -04:00
return new WP_Error ( 'file_does_not_exist' , __ ( 'File does not exist! Please double check the name and try again.' ) );
File Editors: Introduce sandboxed live editing of PHP files with rollbacks for both themes and plugins.
* Edits to active plugins which cause PHP fatal errors will no longer auto-deactivate the plugin. Supersedes #39766.
* Introduce sandboxed PHP file edits for active themes, preventing accidental whitescreening of a user's site when introducing a fatal error.
* After writing a change to a PHP file for an active theme or plugin, perform loopback requests on the file editor admin screens and the homepage to check for fatal errors. If a fatal error is encountered, roll back the edited file and display the error to the user to fix and try again.
* Introduce a secure way to scrape PHP fatal errors from a site via `wp_start_scraping_edited_file_errors()` and `wp_finalize_scraping_edited_file_errors()`.
* Moves file modifications from `theme-editor.php` and `plugin-editor.php` to common `wp_edit_theme_plugin_file()` function.
* Refactor themes and plugin editors to submit file changes via Ajax instead of doing full page refreshes when JS is available.
* Use `get` method for theme/plugin dropdowns.
* Improve styling of plugin editors, including width of plugin/theme dropdowns.
* Improve notices API for theme/plugin editor JS component.
* Strip common base directory from plugin file list. See #24048.
* Factor out functions to list editable file types in `wp_get_theme_file_editable_extensions()` and `wp_get_plugin_file_editable_extensions()`.
* Scroll to line in editor that has linting error when attempting to save. See #41886.
* Add checkbox to dismiss lint errors to proceed with saving. See #41887.
* Only style the Update File button as disabled instead of actually disabling it for accessibility reasons.
* Ensure that value from CodeMirror is used instead of `textarea` when CodeMirror is present.
* Add "Are you sure?" check when leaving editor when there are unsaved changes.
Supersedes [41560].
See #39766, #24048, #41886.
Props westonruter, Clorith, melchoyce, johnbillion, jjj, jdgrimes, azaozz.
Fixes #21622, #41887.
Built from https://develop.svn.wordpress.org/trunk@41721
git-svn-id: http://core.svn.wordpress.org/trunk@41555 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2017-10-03 20:20:45 -04:00
}
// Ensure file extension is allowed.
$extension = null ;
if ( preg_match ( '/\.([^.]+)$/' , $real_file , $matches ) ) {
$extension = strtolower ( $matches [ 1 ] );
if ( ! in_array ( $extension , $editable_extensions , true ) ) {
return new WP_Error ( 'illegal_file_type' , __ ( 'Files of this type are not editable.' ) );
}
}
$previous_content = file_get_contents ( $real_file );
2020-10-18 13:27:06 -04:00
if ( ! is_writable ( $real_file ) ) {
File Editors: Introduce sandboxed live editing of PHP files with rollbacks for both themes and plugins.
* Edits to active plugins which cause PHP fatal errors will no longer auto-deactivate the plugin. Supersedes #39766.
* Introduce sandboxed PHP file edits for active themes, preventing accidental whitescreening of a user's site when introducing a fatal error.
* After writing a change to a PHP file for an active theme or plugin, perform loopback requests on the file editor admin screens and the homepage to check for fatal errors. If a fatal error is encountered, roll back the edited file and display the error to the user to fix and try again.
* Introduce a secure way to scrape PHP fatal errors from a site via `wp_start_scraping_edited_file_errors()` and `wp_finalize_scraping_edited_file_errors()`.
* Moves file modifications from `theme-editor.php` and `plugin-editor.php` to common `wp_edit_theme_plugin_file()` function.
* Refactor themes and plugin editors to submit file changes via Ajax instead of doing full page refreshes when JS is available.
* Use `get` method for theme/plugin dropdowns.
* Improve styling of plugin editors, including width of plugin/theme dropdowns.
* Improve notices API for theme/plugin editor JS component.
* Strip common base directory from plugin file list. See #24048.
* Factor out functions to list editable file types in `wp_get_theme_file_editable_extensions()` and `wp_get_plugin_file_editable_extensions()`.
* Scroll to line in editor that has linting error when attempting to save. See #41886.
* Add checkbox to dismiss lint errors to proceed with saving. See #41887.
* Only style the Update File button as disabled instead of actually disabling it for accessibility reasons.
* Ensure that value from CodeMirror is used instead of `textarea` when CodeMirror is present.
* Add "Are you sure?" check when leaving editor when there are unsaved changes.
Supersedes [41560].
See #39766, #24048, #41886.
Props westonruter, Clorith, melchoyce, johnbillion, jjj, jdgrimes, azaozz.
Fixes #21622, #41887.
Built from https://develop.svn.wordpress.org/trunk@41721
git-svn-id: http://core.svn.wordpress.org/trunk@41555 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2017-10-03 20:20:45 -04:00
return new WP_Error ( 'file_not_writable' );
}
$f = fopen ( $real_file , 'w+' );
2021-04-12 14:19:09 -04:00
File Editors: Introduce sandboxed live editing of PHP files with rollbacks for both themes and plugins.
* Edits to active plugins which cause PHP fatal errors will no longer auto-deactivate the plugin. Supersedes #39766.
* Introduce sandboxed PHP file edits for active themes, preventing accidental whitescreening of a user's site when introducing a fatal error.
* After writing a change to a PHP file for an active theme or plugin, perform loopback requests on the file editor admin screens and the homepage to check for fatal errors. If a fatal error is encountered, roll back the edited file and display the error to the user to fix and try again.
* Introduce a secure way to scrape PHP fatal errors from a site via `wp_start_scraping_edited_file_errors()` and `wp_finalize_scraping_edited_file_errors()`.
* Moves file modifications from `theme-editor.php` and `plugin-editor.php` to common `wp_edit_theme_plugin_file()` function.
* Refactor themes and plugin editors to submit file changes via Ajax instead of doing full page refreshes when JS is available.
* Use `get` method for theme/plugin dropdowns.
* Improve styling of plugin editors, including width of plugin/theme dropdowns.
* Improve notices API for theme/plugin editor JS component.
* Strip common base directory from plugin file list. See #24048.
* Factor out functions to list editable file types in `wp_get_theme_file_editable_extensions()` and `wp_get_plugin_file_editable_extensions()`.
* Scroll to line in editor that has linting error when attempting to save. See #41886.
* Add checkbox to dismiss lint errors to proceed with saving. See #41887.
* Only style the Update File button as disabled instead of actually disabling it for accessibility reasons.
* Ensure that value from CodeMirror is used instead of `textarea` when CodeMirror is present.
* Add "Are you sure?" check when leaving editor when there are unsaved changes.
Supersedes [41560].
See #39766, #24048, #41886.
Props westonruter, Clorith, melchoyce, johnbillion, jjj, jdgrimes, azaozz.
Fixes #21622, #41887.
Built from https://develop.svn.wordpress.org/trunk@41721
git-svn-id: http://core.svn.wordpress.org/trunk@41555 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2017-10-03 20:20:45 -04:00
if ( false === $f ) {
return new WP_Error ( 'file_not_writable' );
}
$written = fwrite ( $f , $content );
fclose ( $f );
2021-04-12 14:19:09 -04:00
File Editors: Introduce sandboxed live editing of PHP files with rollbacks for both themes and plugins.
* Edits to active plugins which cause PHP fatal errors will no longer auto-deactivate the plugin. Supersedes #39766.
* Introduce sandboxed PHP file edits for active themes, preventing accidental whitescreening of a user's site when introducing a fatal error.
* After writing a change to a PHP file for an active theme or plugin, perform loopback requests on the file editor admin screens and the homepage to check for fatal errors. If a fatal error is encountered, roll back the edited file and display the error to the user to fix and try again.
* Introduce a secure way to scrape PHP fatal errors from a site via `wp_start_scraping_edited_file_errors()` and `wp_finalize_scraping_edited_file_errors()`.
* Moves file modifications from `theme-editor.php` and `plugin-editor.php` to common `wp_edit_theme_plugin_file()` function.
* Refactor themes and plugin editors to submit file changes via Ajax instead of doing full page refreshes when JS is available.
* Use `get` method for theme/plugin dropdowns.
* Improve styling of plugin editors, including width of plugin/theme dropdowns.
* Improve notices API for theme/plugin editor JS component.
* Strip common base directory from plugin file list. See #24048.
* Factor out functions to list editable file types in `wp_get_theme_file_editable_extensions()` and `wp_get_plugin_file_editable_extensions()`.
* Scroll to line in editor that has linting error when attempting to save. See #41886.
* Add checkbox to dismiss lint errors to proceed with saving. See #41887.
* Only style the Update File button as disabled instead of actually disabling it for accessibility reasons.
* Ensure that value from CodeMirror is used instead of `textarea` when CodeMirror is present.
* Add "Are you sure?" check when leaving editor when there are unsaved changes.
Supersedes [41560].
See #39766, #24048, #41886.
Props westonruter, Clorith, melchoyce, johnbillion, jjj, jdgrimes, azaozz.
Fixes #21622, #41887.
Built from https://develop.svn.wordpress.org/trunk@41721
git-svn-id: http://core.svn.wordpress.org/trunk@41555 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2017-10-03 20:20:45 -04:00
if ( false === $written ) {
return new WP_Error ( 'unable_to_write' , __ ( 'Unable to write to file.' ) );
}
2020-06-25 05:39:13 -04:00
Upgrade/Install: Invalidate OPcache for PHP files during updates.
When files are copied into place, check whether opcode invalidation is available and attempt to invalidate to avoid unintended behavior or fatal errors from themes, plugins, or core.
Introduces `wp_opcache_invalidate()` to allow safe invalidation of PHP files from opcode cache, and a filter, `wp_opcache_invalidate_file` to override the behavior.
Replaces the existing calls to `opcache_invalidate()` in the plugin and theme editors to use the new function.
Thanks to jnylen0 for porting over a patch from ClassicPress that provided much of the approach for what is being committed.
Props nigro.simone, dd32, JasWSInc, szepe.viktor, swissspidy, JanR, asalce, Garavani, pavelevap, pputzer, GregLone, benoitchantre, jadonn, doc987, kraftbj, Krstarica, jnylen0, nextendweb, williampatton, ayeshrajans, joostdevalk, stevenkussmaul, boogah, jorbin, mikeschroder.
Fixes #36455, #50354.
Built from https://develop.svn.wordpress.org/trunk@48160
git-svn-id: http://core.svn.wordpress.org/trunk@47929 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2020-06-25 03:15:12 -04:00
wp_opcache_invalidate ( $real_file , true );
File Editors: Introduce sandboxed live editing of PHP files with rollbacks for both themes and plugins.
* Edits to active plugins which cause PHP fatal errors will no longer auto-deactivate the plugin. Supersedes #39766.
* Introduce sandboxed PHP file edits for active themes, preventing accidental whitescreening of a user's site when introducing a fatal error.
* After writing a change to a PHP file for an active theme or plugin, perform loopback requests on the file editor admin screens and the homepage to check for fatal errors. If a fatal error is encountered, roll back the edited file and display the error to the user to fix and try again.
* Introduce a secure way to scrape PHP fatal errors from a site via `wp_start_scraping_edited_file_errors()` and `wp_finalize_scraping_edited_file_errors()`.
* Moves file modifications from `theme-editor.php` and `plugin-editor.php` to common `wp_edit_theme_plugin_file()` function.
* Refactor themes and plugin editors to submit file changes via Ajax instead of doing full page refreshes when JS is available.
* Use `get` method for theme/plugin dropdowns.
* Improve styling of plugin editors, including width of plugin/theme dropdowns.
* Improve notices API for theme/plugin editor JS component.
* Strip common base directory from plugin file list. See #24048.
* Factor out functions to list editable file types in `wp_get_theme_file_editable_extensions()` and `wp_get_plugin_file_editable_extensions()`.
* Scroll to line in editor that has linting error when attempting to save. See #41886.
* Add checkbox to dismiss lint errors to proceed with saving. See #41887.
* Only style the Update File button as disabled instead of actually disabling it for accessibility reasons.
* Ensure that value from CodeMirror is used instead of `textarea` when CodeMirror is present.
* Add "Are you sure?" check when leaving editor when there are unsaved changes.
Supersedes [41560].
See #39766, #24048, #41886.
Props westonruter, Clorith, melchoyce, johnbillion, jjj, jdgrimes, azaozz.
Fixes #21622, #41887.
Built from https://develop.svn.wordpress.org/trunk@41721
git-svn-id: http://core.svn.wordpress.org/trunk@41555 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2017-10-03 20:20:45 -04:00
if ( $is_active && 'php' === $extension ) {
2017-11-30 18:11:00 -05:00
$scrape_key = md5 ( rand () );
$transient = 'scrape_key_' . $scrape_key ;
2020-10-08 17:15:13 -04:00
$scrape_nonce = ( string ) rand ();
2020-01-17 19:54:04 -05:00
// It shouldn't take more than 60 seconds to make the two loopback requests.
set_transient ( $transient , $scrape_nonce , 60 );
File Editors: Introduce sandboxed live editing of PHP files with rollbacks for both themes and plugins.
* Edits to active plugins which cause PHP fatal errors will no longer auto-deactivate the plugin. Supersedes #39766.
* Introduce sandboxed PHP file edits for active themes, preventing accidental whitescreening of a user's site when introducing a fatal error.
* After writing a change to a PHP file for an active theme or plugin, perform loopback requests on the file editor admin screens and the homepage to check for fatal errors. If a fatal error is encountered, roll back the edited file and display the error to the user to fix and try again.
* Introduce a secure way to scrape PHP fatal errors from a site via `wp_start_scraping_edited_file_errors()` and `wp_finalize_scraping_edited_file_errors()`.
* Moves file modifications from `theme-editor.php` and `plugin-editor.php` to common `wp_edit_theme_plugin_file()` function.
* Refactor themes and plugin editors to submit file changes via Ajax instead of doing full page refreshes when JS is available.
* Use `get` method for theme/plugin dropdowns.
* Improve styling of plugin editors, including width of plugin/theme dropdowns.
* Improve notices API for theme/plugin editor JS component.
* Strip common base directory from plugin file list. See #24048.
* Factor out functions to list editable file types in `wp_get_theme_file_editable_extensions()` and `wp_get_plugin_file_editable_extensions()`.
* Scroll to line in editor that has linting error when attempting to save. See #41886.
* Add checkbox to dismiss lint errors to proceed with saving. See #41887.
* Only style the Update File button as disabled instead of actually disabling it for accessibility reasons.
* Ensure that value from CodeMirror is used instead of `textarea` when CodeMirror is present.
* Add "Are you sure?" check when leaving editor when there are unsaved changes.
Supersedes [41560].
See #39766, #24048, #41886.
Props westonruter, Clorith, melchoyce, johnbillion, jjj, jdgrimes, azaozz.
Fixes #21622, #41887.
Built from https://develop.svn.wordpress.org/trunk@41721
git-svn-id: http://core.svn.wordpress.org/trunk@41555 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2017-10-03 20:20:45 -04:00
2017-11-30 18:11:00 -05:00
$cookies = wp_unslash ( $_COOKIE );
File Editors: Introduce sandboxed live editing of PHP files with rollbacks for both themes and plugins.
* Edits to active plugins which cause PHP fatal errors will no longer auto-deactivate the plugin. Supersedes #39766.
* Introduce sandboxed PHP file edits for active themes, preventing accidental whitescreening of a user's site when introducing a fatal error.
* After writing a change to a PHP file for an active theme or plugin, perform loopback requests on the file editor admin screens and the homepage to check for fatal errors. If a fatal error is encountered, roll back the edited file and display the error to the user to fix and try again.
* Introduce a secure way to scrape PHP fatal errors from a site via `wp_start_scraping_edited_file_errors()` and `wp_finalize_scraping_edited_file_errors()`.
* Moves file modifications from `theme-editor.php` and `plugin-editor.php` to common `wp_edit_theme_plugin_file()` function.
* Refactor themes and plugin editors to submit file changes via Ajax instead of doing full page refreshes when JS is available.
* Use `get` method for theme/plugin dropdowns.
* Improve styling of plugin editors, including width of plugin/theme dropdowns.
* Improve notices API for theme/plugin editor JS component.
* Strip common base directory from plugin file list. See #24048.
* Factor out functions to list editable file types in `wp_get_theme_file_editable_extensions()` and `wp_get_plugin_file_editable_extensions()`.
* Scroll to line in editor that has linting error when attempting to save. See #41886.
* Add checkbox to dismiss lint errors to proceed with saving. See #41887.
* Only style the Update File button as disabled instead of actually disabling it for accessibility reasons.
* Ensure that value from CodeMirror is used instead of `textarea` when CodeMirror is present.
* Add "Are you sure?" check when leaving editor when there are unsaved changes.
Supersedes [41560].
See #39766, #24048, #41886.
Props westonruter, Clorith, melchoyce, johnbillion, jjj, jdgrimes, azaozz.
Fixes #21622, #41887.
Built from https://develop.svn.wordpress.org/trunk@41721
git-svn-id: http://core.svn.wordpress.org/trunk@41555 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2017-10-03 20:20:45 -04:00
$scrape_params = array (
2017-11-30 18:11:00 -05:00
'wp_scrape_key' => $scrape_key ,
File Editors: Introduce sandboxed live editing of PHP files with rollbacks for both themes and plugins.
* Edits to active plugins which cause PHP fatal errors will no longer auto-deactivate the plugin. Supersedes #39766.
* Introduce sandboxed PHP file edits for active themes, preventing accidental whitescreening of a user's site when introducing a fatal error.
* After writing a change to a PHP file for an active theme or plugin, perform loopback requests on the file editor admin screens and the homepage to check for fatal errors. If a fatal error is encountered, roll back the edited file and display the error to the user to fix and try again.
* Introduce a secure way to scrape PHP fatal errors from a site via `wp_start_scraping_edited_file_errors()` and `wp_finalize_scraping_edited_file_errors()`.
* Moves file modifications from `theme-editor.php` and `plugin-editor.php` to common `wp_edit_theme_plugin_file()` function.
* Refactor themes and plugin editors to submit file changes via Ajax instead of doing full page refreshes when JS is available.
* Use `get` method for theme/plugin dropdowns.
* Improve styling of plugin editors, including width of plugin/theme dropdowns.
* Improve notices API for theme/plugin editor JS component.
* Strip common base directory from plugin file list. See #24048.
* Factor out functions to list editable file types in `wp_get_theme_file_editable_extensions()` and `wp_get_plugin_file_editable_extensions()`.
* Scroll to line in editor that has linting error when attempting to save. See #41886.
* Add checkbox to dismiss lint errors to proceed with saving. See #41887.
* Only style the Update File button as disabled instead of actually disabling it for accessibility reasons.
* Ensure that value from CodeMirror is used instead of `textarea` when CodeMirror is present.
* Add "Are you sure?" check when leaving editor when there are unsaved changes.
Supersedes [41560].
See #39766, #24048, #41886.
Props westonruter, Clorith, melchoyce, johnbillion, jjj, jdgrimes, azaozz.
Fixes #21622, #41887.
Built from https://develop.svn.wordpress.org/trunk@41721
git-svn-id: http://core.svn.wordpress.org/trunk@41555 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2017-10-03 20:20:45 -04:00
'wp_scrape_nonce' => $scrape_nonce ,
);
2017-11-30 18:11:00 -05:00
$headers = array (
File Editors: Introduce sandboxed live editing of PHP files with rollbacks for both themes and plugins.
* Edits to active plugins which cause PHP fatal errors will no longer auto-deactivate the plugin. Supersedes #39766.
* Introduce sandboxed PHP file edits for active themes, preventing accidental whitescreening of a user's site when introducing a fatal error.
* After writing a change to a PHP file for an active theme or plugin, perform loopback requests on the file editor admin screens and the homepage to check for fatal errors. If a fatal error is encountered, roll back the edited file and display the error to the user to fix and try again.
* Introduce a secure way to scrape PHP fatal errors from a site via `wp_start_scraping_edited_file_errors()` and `wp_finalize_scraping_edited_file_errors()`.
* Moves file modifications from `theme-editor.php` and `plugin-editor.php` to common `wp_edit_theme_plugin_file()` function.
* Refactor themes and plugin editors to submit file changes via Ajax instead of doing full page refreshes when JS is available.
* Use `get` method for theme/plugin dropdowns.
* Improve styling of plugin editors, including width of plugin/theme dropdowns.
* Improve notices API for theme/plugin editor JS component.
* Strip common base directory from plugin file list. See #24048.
* Factor out functions to list editable file types in `wp_get_theme_file_editable_extensions()` and `wp_get_plugin_file_editable_extensions()`.
* Scroll to line in editor that has linting error when attempting to save. See #41886.
* Add checkbox to dismiss lint errors to proceed with saving. See #41887.
* Only style the Update File button as disabled instead of actually disabling it for accessibility reasons.
* Ensure that value from CodeMirror is used instead of `textarea` when CodeMirror is present.
* Add "Are you sure?" check when leaving editor when there are unsaved changes.
Supersedes [41560].
See #39766, #24048, #41886.
Props westonruter, Clorith, melchoyce, johnbillion, jjj, jdgrimes, azaozz.
Fixes #21622, #41887.
Built from https://develop.svn.wordpress.org/trunk@41721
git-svn-id: http://core.svn.wordpress.org/trunk@41555 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2017-10-03 20:20:45 -04:00
'Cache-Control' => 'no-cache' ,
);
2019-09-21 11:59:56 -04:00
/** This filter is documented in wp-includes/class-wp-http-streams.php */
$sslverify = apply_filters ( 'https_local_ssl_verify' , false );
2017-10-10 01:27:49 -04:00
// Include Basic auth in loopback requests.
if ( isset ( $_SERVER [ 'PHP_AUTH_USER' ] ) && isset ( $_SERVER [ 'PHP_AUTH_PW' ] ) ) {
$headers [ 'Authorization' ] = 'Basic ' . base64_encode ( wp_unslash ( $_SERVER [ 'PHP_AUTH_USER' ] ) . ':' . wp_unslash ( $_SERVER [ 'PHP_AUTH_PW' ] ) );
}
// Make sure PHP process doesn't die before loopback requests complete.
2019-07-09 01:45:58 -04:00
set_time_limit ( 300 );
2017-10-10 01:27:49 -04:00
// Time to wait for loopback requests to finish.
$timeout = 100 ;
$needle_start = " ###### wp_scraping_result_start: $scrape_key ###### " ;
2017-11-30 18:11:00 -05:00
$needle_end = " ###### wp_scraping_result_end: $scrape_key ###### " ;
File Editors: Introduce sandboxed live editing of PHP files with rollbacks for both themes and plugins.
* Edits to active plugins which cause PHP fatal errors will no longer auto-deactivate the plugin. Supersedes #39766.
* Introduce sandboxed PHP file edits for active themes, preventing accidental whitescreening of a user's site when introducing a fatal error.
* After writing a change to a PHP file for an active theme or plugin, perform loopback requests on the file editor admin screens and the homepage to check for fatal errors. If a fatal error is encountered, roll back the edited file and display the error to the user to fix and try again.
* Introduce a secure way to scrape PHP fatal errors from a site via `wp_start_scraping_edited_file_errors()` and `wp_finalize_scraping_edited_file_errors()`.
* Moves file modifications from `theme-editor.php` and `plugin-editor.php` to common `wp_edit_theme_plugin_file()` function.
* Refactor themes and plugin editors to submit file changes via Ajax instead of doing full page refreshes when JS is available.
* Use `get` method for theme/plugin dropdowns.
* Improve styling of plugin editors, including width of plugin/theme dropdowns.
* Improve notices API for theme/plugin editor JS component.
* Strip common base directory from plugin file list. See #24048.
* Factor out functions to list editable file types in `wp_get_theme_file_editable_extensions()` and `wp_get_plugin_file_editable_extensions()`.
* Scroll to line in editor that has linting error when attempting to save. See #41886.
* Add checkbox to dismiss lint errors to proceed with saving. See #41887.
* Only style the Update File button as disabled instead of actually disabling it for accessibility reasons.
* Ensure that value from CodeMirror is used instead of `textarea` when CodeMirror is present.
* Add "Are you sure?" check when leaving editor when there are unsaved changes.
Supersedes [41560].
See #39766, #24048, #41886.
Props westonruter, Clorith, melchoyce, johnbillion, jjj, jdgrimes, azaozz.
Fixes #21622, #41887.
Built from https://develop.svn.wordpress.org/trunk@41721
git-svn-id: http://core.svn.wordpress.org/trunk@41555 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2017-10-03 20:20:45 -04:00
// Attempt loopback request to editor to see if user just whitescreened themselves.
if ( $plugin ) {
$url = add_query_arg ( compact ( 'plugin' , 'file' ), admin_url ( 'plugin-editor.php' ) );
} elseif ( isset ( $stylesheet ) ) {
$url = add_query_arg (
array (
'theme' => $stylesheet ,
2017-11-30 18:11:00 -05:00
'file' => $file ,
File Editors: Introduce sandboxed live editing of PHP files with rollbacks for both themes and plugins.
* Edits to active plugins which cause PHP fatal errors will no longer auto-deactivate the plugin. Supersedes #39766.
* Introduce sandboxed PHP file edits for active themes, preventing accidental whitescreening of a user's site when introducing a fatal error.
* After writing a change to a PHP file for an active theme or plugin, perform loopback requests on the file editor admin screens and the homepage to check for fatal errors. If a fatal error is encountered, roll back the edited file and display the error to the user to fix and try again.
* Introduce a secure way to scrape PHP fatal errors from a site via `wp_start_scraping_edited_file_errors()` and `wp_finalize_scraping_edited_file_errors()`.
* Moves file modifications from `theme-editor.php` and `plugin-editor.php` to common `wp_edit_theme_plugin_file()` function.
* Refactor themes and plugin editors to submit file changes via Ajax instead of doing full page refreshes when JS is available.
* Use `get` method for theme/plugin dropdowns.
* Improve styling of plugin editors, including width of plugin/theme dropdowns.
* Improve notices API for theme/plugin editor JS component.
* Strip common base directory from plugin file list. See #24048.
* Factor out functions to list editable file types in `wp_get_theme_file_editable_extensions()` and `wp_get_plugin_file_editable_extensions()`.
* Scroll to line in editor that has linting error when attempting to save. See #41886.
* Add checkbox to dismiss lint errors to proceed with saving. See #41887.
* Only style the Update File button as disabled instead of actually disabling it for accessibility reasons.
* Ensure that value from CodeMirror is used instead of `textarea` when CodeMirror is present.
* Add "Are you sure?" check when leaving editor when there are unsaved changes.
Supersedes [41560].
See #39766, #24048, #41886.
Props westonruter, Clorith, melchoyce, johnbillion, jjj, jdgrimes, azaozz.
Fixes #21622, #41887.
Built from https://develop.svn.wordpress.org/trunk@41721
git-svn-id: http://core.svn.wordpress.org/trunk@41555 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2017-10-03 20:20:45 -04:00
),
admin_url ( 'theme-editor.php' )
);
} else {
$url = admin_url ();
}
2020-03-15 15:45:07 -04:00
2020-08-05 20:25:07 -04:00
if ( function_exists ( 'session_status' ) && PHP_SESSION_ACTIVE === session_status () ) {
2020-03-15 15:45:07 -04:00
// Close any active session to prevent HTTP requests from timing out
// when attempting to connect back to the site.
session_write_close ();
}
2017-11-30 18:11:00 -05:00
$url = add_query_arg ( $scrape_params , $url );
2019-09-21 11:59:56 -04:00
$r = wp_remote_get ( $url , compact ( 'cookies' , 'headers' , 'timeout' , 'sslverify' ) );
2017-11-30 18:11:00 -05:00
$body = wp_remote_retrieve_body ( $r );
2017-10-10 01:27:49 -04:00
$scrape_result_position = strpos ( $body , $needle_start );
$loopback_request_failure = array (
2017-11-30 18:11:00 -05:00
'code' => 'loopback_request_failed' ,
2017-10-10 01:27:49 -04:00
'message' => __ ( 'Unable to communicate back with site to check for fatal errors, so the PHP change was reverted. You will need to upload your PHP file change by some other means, such as by using SFTP.' ),
);
2017-11-30 18:11:00 -05:00
$json_parse_failure = array (
2017-10-10 01:27:49 -04:00
'code' => 'json_parse_error' ,
);
$result = null ;
2021-04-12 14:19:09 -04:00
2017-10-10 01:27:49 -04:00
if ( false === $scrape_result_position ) {
$result = $loopback_request_failure ;
} else {
$error_output = substr ( $body , $scrape_result_position + strlen ( $needle_start ) );
$error_output = substr ( $error_output , 0 , strpos ( $error_output , $needle_end ) );
2017-11-30 18:11:00 -05:00
$result = json_decode ( trim ( $error_output ), true );
2017-10-10 01:27:49 -04:00
if ( empty ( $result ) ) {
$result = $json_parse_failure ;
}
}
File Editors: Introduce sandboxed live editing of PHP files with rollbacks for both themes and plugins.
* Edits to active plugins which cause PHP fatal errors will no longer auto-deactivate the plugin. Supersedes #39766.
* Introduce sandboxed PHP file edits for active themes, preventing accidental whitescreening of a user's site when introducing a fatal error.
* After writing a change to a PHP file for an active theme or plugin, perform loopback requests on the file editor admin screens and the homepage to check for fatal errors. If a fatal error is encountered, roll back the edited file and display the error to the user to fix and try again.
* Introduce a secure way to scrape PHP fatal errors from a site via `wp_start_scraping_edited_file_errors()` and `wp_finalize_scraping_edited_file_errors()`.
* Moves file modifications from `theme-editor.php` and `plugin-editor.php` to common `wp_edit_theme_plugin_file()` function.
* Refactor themes and plugin editors to submit file changes via Ajax instead of doing full page refreshes when JS is available.
* Use `get` method for theme/plugin dropdowns.
* Improve styling of plugin editors, including width of plugin/theme dropdowns.
* Improve notices API for theme/plugin editor JS component.
* Strip common base directory from plugin file list. See #24048.
* Factor out functions to list editable file types in `wp_get_theme_file_editable_extensions()` and `wp_get_plugin_file_editable_extensions()`.
* Scroll to line in editor that has linting error when attempting to save. See #41886.
* Add checkbox to dismiss lint errors to proceed with saving. See #41887.
* Only style the Update File button as disabled instead of actually disabling it for accessibility reasons.
* Ensure that value from CodeMirror is used instead of `textarea` when CodeMirror is present.
* Add "Are you sure?" check when leaving editor when there are unsaved changes.
Supersedes [41560].
See #39766, #24048, #41886.
Props westonruter, Clorith, melchoyce, johnbillion, jjj, jdgrimes, azaozz.
Fixes #21622, #41887.
Built from https://develop.svn.wordpress.org/trunk@41721
git-svn-id: http://core.svn.wordpress.org/trunk@41555 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2017-10-03 20:20:45 -04:00
// Try making request to homepage as well to see if visitors have been whitescreened.
2017-10-10 01:27:49 -04:00
if ( true === $result ) {
2017-11-30 18:11:00 -05:00
$url = home_url ( '/' );
$url = add_query_arg ( $scrape_params , $url );
2020-06-03 07:43:15 -04:00
$r = wp_remote_get ( $url , compact ( 'cookies' , 'headers' , 'timeout' , 'sslverify' ) );
2017-11-30 18:11:00 -05:00
$body = wp_remote_retrieve_body ( $r );
2017-10-10 01:27:49 -04:00
$scrape_result_position = strpos ( $body , $needle_start );
if ( false === $scrape_result_position ) {
$result = $loopback_request_failure ;
} else {
$error_output = substr ( $body , $scrape_result_position + strlen ( $needle_start ) );
$error_output = substr ( $error_output , 0 , strpos ( $error_output , $needle_end ) );
2017-11-30 18:11:00 -05:00
$result = json_decode ( trim ( $error_output ), true );
2017-10-10 01:27:49 -04:00
if ( empty ( $result ) ) {
$result = $json_parse_failure ;
}
}
File Editors: Introduce sandboxed live editing of PHP files with rollbacks for both themes and plugins.
* Edits to active plugins which cause PHP fatal errors will no longer auto-deactivate the plugin. Supersedes #39766.
* Introduce sandboxed PHP file edits for active themes, preventing accidental whitescreening of a user's site when introducing a fatal error.
* After writing a change to a PHP file for an active theme or plugin, perform loopback requests on the file editor admin screens and the homepage to check for fatal errors. If a fatal error is encountered, roll back the edited file and display the error to the user to fix and try again.
* Introduce a secure way to scrape PHP fatal errors from a site via `wp_start_scraping_edited_file_errors()` and `wp_finalize_scraping_edited_file_errors()`.
* Moves file modifications from `theme-editor.php` and `plugin-editor.php` to common `wp_edit_theme_plugin_file()` function.
* Refactor themes and plugin editors to submit file changes via Ajax instead of doing full page refreshes when JS is available.
* Use `get` method for theme/plugin dropdowns.
* Improve styling of plugin editors, including width of plugin/theme dropdowns.
* Improve notices API for theme/plugin editor JS component.
* Strip common base directory from plugin file list. See #24048.
* Factor out functions to list editable file types in `wp_get_theme_file_editable_extensions()` and `wp_get_plugin_file_editable_extensions()`.
* Scroll to line in editor that has linting error when attempting to save. See #41886.
* Add checkbox to dismiss lint errors to proceed with saving. See #41887.
* Only style the Update File button as disabled instead of actually disabling it for accessibility reasons.
* Ensure that value from CodeMirror is used instead of `textarea` when CodeMirror is present.
* Add "Are you sure?" check when leaving editor when there are unsaved changes.
Supersedes [41560].
See #39766, #24048, #41886.
Props westonruter, Clorith, melchoyce, johnbillion, jjj, jdgrimes, azaozz.
Fixes #21622, #41887.
Built from https://develop.svn.wordpress.org/trunk@41721
git-svn-id: http://core.svn.wordpress.org/trunk@41555 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2017-10-03 20:20:45 -04:00
}
delete_transient ( $transient );
2017-10-10 01:27:49 -04:00
if ( true !== $result ) {
// Roll-back file change.
File Editors: Introduce sandboxed live editing of PHP files with rollbacks for both themes and plugins.
* Edits to active plugins which cause PHP fatal errors will no longer auto-deactivate the plugin. Supersedes #39766.
* Introduce sandboxed PHP file edits for active themes, preventing accidental whitescreening of a user's site when introducing a fatal error.
* After writing a change to a PHP file for an active theme or plugin, perform loopback requests on the file editor admin screens and the homepage to check for fatal errors. If a fatal error is encountered, roll back the edited file and display the error to the user to fix and try again.
* Introduce a secure way to scrape PHP fatal errors from a site via `wp_start_scraping_edited_file_errors()` and `wp_finalize_scraping_edited_file_errors()`.
* Moves file modifications from `theme-editor.php` and `plugin-editor.php` to common `wp_edit_theme_plugin_file()` function.
* Refactor themes and plugin editors to submit file changes via Ajax instead of doing full page refreshes when JS is available.
* Use `get` method for theme/plugin dropdowns.
* Improve styling of plugin editors, including width of plugin/theme dropdowns.
* Improve notices API for theme/plugin editor JS component.
* Strip common base directory from plugin file list. See #24048.
* Factor out functions to list editable file types in `wp_get_theme_file_editable_extensions()` and `wp_get_plugin_file_editable_extensions()`.
* Scroll to line in editor that has linting error when attempting to save. See #41886.
* Add checkbox to dismiss lint errors to proceed with saving. See #41887.
* Only style the Update File button as disabled instead of actually disabling it for accessibility reasons.
* Ensure that value from CodeMirror is used instead of `textarea` when CodeMirror is present.
* Add "Are you sure?" check when leaving editor when there are unsaved changes.
Supersedes [41560].
See #39766, #24048, #41886.
Props westonruter, Clorith, melchoyce, johnbillion, jjj, jdgrimes, azaozz.
Fixes #21622, #41887.
Built from https://develop.svn.wordpress.org/trunk@41721
git-svn-id: http://core.svn.wordpress.org/trunk@41555 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2017-10-03 20:20:45 -04:00
file_put_contents ( $real_file , $previous_content );
Upgrade/Install: Invalidate OPcache for PHP files during updates.
When files are copied into place, check whether opcode invalidation is available and attempt to invalidate to avoid unintended behavior or fatal errors from themes, plugins, or core.
Introduces `wp_opcache_invalidate()` to allow safe invalidation of PHP files from opcode cache, and a filter, `wp_opcache_invalidate_file` to override the behavior.
Replaces the existing calls to `opcache_invalidate()` in the plugin and theme editors to use the new function.
Thanks to jnylen0 for porting over a patch from ClassicPress that provided much of the approach for what is being committed.
Props nigro.simone, dd32, JasWSInc, szepe.viktor, swissspidy, JanR, asalce, Garavani, pavelevap, pputzer, GregLone, benoitchantre, jadonn, doc987, kraftbj, Krstarica, jnylen0, nextendweb, williampatton, ayeshrajans, joostdevalk, stevenkussmaul, boogah, jorbin, mikeschroder.
Fixes #36455, #50354.
Built from https://develop.svn.wordpress.org/trunk@48160
git-svn-id: http://core.svn.wordpress.org/trunk@47929 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2020-06-25 03:15:12 -04:00
wp_opcache_invalidate ( $real_file , true );
File Editors: Introduce sandboxed live editing of PHP files with rollbacks for both themes and plugins.
* Edits to active plugins which cause PHP fatal errors will no longer auto-deactivate the plugin. Supersedes #39766.
* Introduce sandboxed PHP file edits for active themes, preventing accidental whitescreening of a user's site when introducing a fatal error.
* After writing a change to a PHP file for an active theme or plugin, perform loopback requests on the file editor admin screens and the homepage to check for fatal errors. If a fatal error is encountered, roll back the edited file and display the error to the user to fix and try again.
* Introduce a secure way to scrape PHP fatal errors from a site via `wp_start_scraping_edited_file_errors()` and `wp_finalize_scraping_edited_file_errors()`.
* Moves file modifications from `theme-editor.php` and `plugin-editor.php` to common `wp_edit_theme_plugin_file()` function.
* Refactor themes and plugin editors to submit file changes via Ajax instead of doing full page refreshes when JS is available.
* Use `get` method for theme/plugin dropdowns.
* Improve styling of plugin editors, including width of plugin/theme dropdowns.
* Improve notices API for theme/plugin editor JS component.
* Strip common base directory from plugin file list. See #24048.
* Factor out functions to list editable file types in `wp_get_theme_file_editable_extensions()` and `wp_get_plugin_file_editable_extensions()`.
* Scroll to line in editor that has linting error when attempting to save. See #41886.
* Add checkbox to dismiss lint errors to proceed with saving. See #41887.
* Only style the Update File button as disabled instead of actually disabling it for accessibility reasons.
* Ensure that value from CodeMirror is used instead of `textarea` when CodeMirror is present.
* Add "Are you sure?" check when leaving editor when there are unsaved changes.
Supersedes [41560].
See #39766, #24048, #41886.
Props westonruter, Clorith, melchoyce, johnbillion, jjj, jdgrimes, azaozz.
Fixes #21622, #41887.
Built from https://develop.svn.wordpress.org/trunk@41721
git-svn-id: http://core.svn.wordpress.org/trunk@41555 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2017-10-03 20:20:45 -04:00
2017-10-10 01:27:49 -04:00
if ( ! isset ( $result [ 'message' ] ) ) {
2018-02-18 21:13:32 -05:00
$message = __ ( 'Something went wrong.' );
File Editors: Introduce sandboxed live editing of PHP files with rollbacks for both themes and plugins.
* Edits to active plugins which cause PHP fatal errors will no longer auto-deactivate the plugin. Supersedes #39766.
* Introduce sandboxed PHP file edits for active themes, preventing accidental whitescreening of a user's site when introducing a fatal error.
* After writing a change to a PHP file for an active theme or plugin, perform loopback requests on the file editor admin screens and the homepage to check for fatal errors. If a fatal error is encountered, roll back the edited file and display the error to the user to fix and try again.
* Introduce a secure way to scrape PHP fatal errors from a site via `wp_start_scraping_edited_file_errors()` and `wp_finalize_scraping_edited_file_errors()`.
* Moves file modifications from `theme-editor.php` and `plugin-editor.php` to common `wp_edit_theme_plugin_file()` function.
* Refactor themes and plugin editors to submit file changes via Ajax instead of doing full page refreshes when JS is available.
* Use `get` method for theme/plugin dropdowns.
* Improve styling of plugin editors, including width of plugin/theme dropdowns.
* Improve notices API for theme/plugin editor JS component.
* Strip common base directory from plugin file list. See #24048.
* Factor out functions to list editable file types in `wp_get_theme_file_editable_extensions()` and `wp_get_plugin_file_editable_extensions()`.
* Scroll to line in editor that has linting error when attempting to save. See #41886.
* Add checkbox to dismiss lint errors to proceed with saving. See #41887.
* Only style the Update File button as disabled instead of actually disabling it for accessibility reasons.
* Ensure that value from CodeMirror is used instead of `textarea` when CodeMirror is present.
* Add "Are you sure?" check when leaving editor when there are unsaved changes.
Supersedes [41560].
See #39766, #24048, #41886.
Props westonruter, Clorith, melchoyce, johnbillion, jjj, jdgrimes, azaozz.
Fixes #21622, #41887.
Built from https://develop.svn.wordpress.org/trunk@41721
git-svn-id: http://core.svn.wordpress.org/trunk@41555 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2017-10-03 20:20:45 -04:00
} else {
2017-10-10 01:27:49 -04:00
$message = $result [ 'message' ];
unset ( $result [ 'message' ] );
File Editors: Introduce sandboxed live editing of PHP files with rollbacks for both themes and plugins.
* Edits to active plugins which cause PHP fatal errors will no longer auto-deactivate the plugin. Supersedes #39766.
* Introduce sandboxed PHP file edits for active themes, preventing accidental whitescreening of a user's site when introducing a fatal error.
* After writing a change to a PHP file for an active theme or plugin, perform loopback requests on the file editor admin screens and the homepage to check for fatal errors. If a fatal error is encountered, roll back the edited file and display the error to the user to fix and try again.
* Introduce a secure way to scrape PHP fatal errors from a site via `wp_start_scraping_edited_file_errors()` and `wp_finalize_scraping_edited_file_errors()`.
* Moves file modifications from `theme-editor.php` and `plugin-editor.php` to common `wp_edit_theme_plugin_file()` function.
* Refactor themes and plugin editors to submit file changes via Ajax instead of doing full page refreshes when JS is available.
* Use `get` method for theme/plugin dropdowns.
* Improve styling of plugin editors, including width of plugin/theme dropdowns.
* Improve notices API for theme/plugin editor JS component.
* Strip common base directory from plugin file list. See #24048.
* Factor out functions to list editable file types in `wp_get_theme_file_editable_extensions()` and `wp_get_plugin_file_editable_extensions()`.
* Scroll to line in editor that has linting error when attempting to save. See #41886.
* Add checkbox to dismiss lint errors to proceed with saving. See #41887.
* Only style the Update File button as disabled instead of actually disabling it for accessibility reasons.
* Ensure that value from CodeMirror is used instead of `textarea` when CodeMirror is present.
* Add "Are you sure?" check when leaving editor when there are unsaved changes.
Supersedes [41560].
See #39766, #24048, #41886.
Props westonruter, Clorith, melchoyce, johnbillion, jjj, jdgrimes, azaozz.
Fixes #21622, #41887.
Built from https://develop.svn.wordpress.org/trunk@41721
git-svn-id: http://core.svn.wordpress.org/trunk@41555 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2017-10-03 20:20:45 -04:00
}
2021-04-12 14:19:09 -04:00
2017-10-10 01:27:49 -04:00
return new WP_Error ( 'php_error' , $message , $result );
File Editors: Introduce sandboxed live editing of PHP files with rollbacks for both themes and plugins.
* Edits to active plugins which cause PHP fatal errors will no longer auto-deactivate the plugin. Supersedes #39766.
* Introduce sandboxed PHP file edits for active themes, preventing accidental whitescreening of a user's site when introducing a fatal error.
* After writing a change to a PHP file for an active theme or plugin, perform loopback requests on the file editor admin screens and the homepage to check for fatal errors. If a fatal error is encountered, roll back the edited file and display the error to the user to fix and try again.
* Introduce a secure way to scrape PHP fatal errors from a site via `wp_start_scraping_edited_file_errors()` and `wp_finalize_scraping_edited_file_errors()`.
* Moves file modifications from `theme-editor.php` and `plugin-editor.php` to common `wp_edit_theme_plugin_file()` function.
* Refactor themes and plugin editors to submit file changes via Ajax instead of doing full page refreshes when JS is available.
* Use `get` method for theme/plugin dropdowns.
* Improve styling of plugin editors, including width of plugin/theme dropdowns.
* Improve notices API for theme/plugin editor JS component.
* Strip common base directory from plugin file list. See #24048.
* Factor out functions to list editable file types in `wp_get_theme_file_editable_extensions()` and `wp_get_plugin_file_editable_extensions()`.
* Scroll to line in editor that has linting error when attempting to save. See #41886.
* Add checkbox to dismiss lint errors to proceed with saving. See #41887.
* Only style the Update File button as disabled instead of actually disabling it for accessibility reasons.
* Ensure that value from CodeMirror is used instead of `textarea` when CodeMirror is present.
* Add "Are you sure?" check when leaving editor when there are unsaved changes.
Supersedes [41560].
See #39766, #24048, #41886.
Props westonruter, Clorith, melchoyce, johnbillion, jjj, jdgrimes, azaozz.
Fixes #21622, #41887.
Built from https://develop.svn.wordpress.org/trunk@41721
git-svn-id: http://core.svn.wordpress.org/trunk@41555 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2017-10-03 20:20:45 -04:00
}
}
if ( $theme instanceof WP_Theme ) {
$theme -> cache_delete ();
}
return true ;
}
2008-10-01 21:03:26 -04:00
/**
2020-06-25 05:39:13 -04:00
* Returns a filename of a temporary unique file .
*
2009-11-05 18:03:48 -05:00
* Please note that the calling function must unlink () this itself .
2008-10-01 21:03:26 -04:00
*
2009-11-05 18:03:48 -05:00
* The filename is based off the passed parameter or defaults to the current unix timestamp ,
2020-06-25 05:39:13 -04:00
* while the directory can either be passed as well , or by leaving it blank , default to a writable
* temporary directory .
2008-10-01 21:03:26 -04:00
*
2009-11-05 18:03:48 -05:00
* @ since 2.6 . 0
*
2014-12-06 16:24:45 -05:00
* @ param string $filename Optional . Filename to base the Unique file off . Default empty .
* @ param string $dir Optional . Directory to store the file in . Default empty .
2020-06-25 05:39:13 -04:00
* @ return string A writable filename .
2008-10-01 21:03:26 -04:00
*/
2015-03-30 22:11:26 -04:00
function wp_tempnam ( $filename = '' , $dir = '' ) {
if ( empty ( $dir ) ) {
2008-04-25 17:34:54 -04:00
$dir = get_temp_dir ();
2015-03-30 22:11:26 -04:00
}
2020-05-16 14:42:12 -04:00
if ( empty ( $filename ) || in_array ( $filename , array ( '.' , '/' , '\\' ), true ) ) {
2017-11-23 22:05:50 -05:00
$filename = uniqid ();
2015-03-30 22:11:26 -04:00
}
2020-01-17 19:54:04 -05:00
// Use the basename of the given file without the extension as the name for the temporary directory.
2015-03-30 22:11:26 -04:00
$temp_filename = basename ( $filename );
$temp_filename = preg_replace ( '|\.[^.]*$|' , '' , $temp_filename );
2008-04-25 17:34:54 -04:00
2015-09-16 08:46:28 -04:00
// If the folder is falsey, use its parent directory name instead.
2015-03-30 22:11:26 -04:00
if ( ! $temp_filename ) {
return wp_tempnam ( dirname ( $filename ), $dir );
}
2020-01-17 19:54:04 -05:00
// Suffix some random data to avoid filename conflicts.
2015-11-16 00:33:25 -05:00
$temp_filename .= '-' . wp_generate_password ( 6 , false );
2015-03-30 22:11:26 -04:00
$temp_filename .= '.tmp' ;
2017-11-30 18:11:00 -05:00
$temp_filename = $dir . wp_unique_filename ( $dir , $temp_filename );
2015-11-16 00:33:25 -05:00
$fp = @ fopen ( $temp_filename , 'x' );
2021-04-12 14:19:09 -04:00
2015-11-16 00:33:25 -05:00
if ( ! $fp && is_writable ( $dir ) && file_exists ( $temp_filename ) ) {
return wp_tempnam ( $filename , $dir );
}
2021-04-12 14:19:09 -04:00
2015-11-16 00:33:25 -05:00
if ( $fp ) {
fclose ( $fp );
}
2015-03-30 22:11:26 -04:00
return $temp_filename ;
2008-04-25 17:34:54 -04:00
}
2008-10-01 21:03:26 -04:00
/**
2017-10-24 18:50:49 -04:00
* Makes sure that the file that was requested to be edited is allowed to be edited .
2010-10-27 18:24:06 -04:00
*
2017-10-24 18:50:49 -04:00
* Function will die if you are not allowed to edit the file .
2008-10-01 21:03:26 -04:00
*
2010-10-27 18:24:06 -04:00
* @ since 1.5 . 0
2008-10-01 21:03:26 -04:00
*
2018-02-09 12:26:31 -05:00
* @ param string $file File the user is attempting to edit .
2020-06-25 05:39:13 -04:00
* @ param string [] $allowed_files Optional . Array of allowed files to edit .
* `$file` must match an entry exactly .
2018-02-09 12:26:31 -05:00
* @ return string | void Returns the file name on success , dies on failure .
2008-10-01 21:03:26 -04:00
*/
2017-10-24 19:15:49 -04:00
function validate_file_to_edit ( $file , $allowed_files = array () ) {
2007-05-27 20:04:33 -04:00
$code = validate_file ( $file , $allowed_files );
2017-11-30 18:11:00 -05:00
if ( ! $code ) {
2007-05-27 20:04:33 -04:00
return $file ;
2017-11-30 18:11:00 -05:00
}
2007-05-27 20:04:33 -04:00
switch ( $code ) {
2017-11-30 18:11:00 -05:00
case 1 :
2014-02-21 09:46:14 -05:00
wp_die ( __ ( 'Sorry, that file cannot be edited.' ) );
2007-05-27 20:04:33 -04:00
2017-11-30 18:11:00 -05:00
// case 2 :
Administration: Replace contracted verb forms for better consistency.
This changeset replaces contracted verb forms like `doesn't`, `can't`, or `isn't` with non-contracted forms like `does not`, `cannot`, or `is not`, for better consistency across the WordPress administration. It also updates some corresponding unit tests strings.
Props Presskopp, socalchristina, aandrewdixon, francina, SergeyBiryukov, JeffPaul, audrasjb, hellofromTonya.
Fixes #38913.
See #39176.
Built from https://develop.svn.wordpress.org/trunk@52978
git-svn-id: http://core.svn.wordpress.org/trunk@52567 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-03-22 12:25:03 -04:00
// wp_die( __('Sorry, cannot call files with their real path.' ));
2007-05-27 20:04:33 -04:00
2017-11-30 18:11:00 -05:00
case 3 :
2014-02-21 09:46:14 -05:00
wp_die ( __ ( 'Sorry, that file cannot be edited.' ) );
2007-05-27 20:04:33 -04:00
}
}
2008-10-01 21:03:26 -04:00
/**
2020-06-25 05:39:13 -04:00
* Handles PHP uploads in WordPress .
*
* Sanitizes file names , checks extensions for mime type , and moves the file
* to the appropriate directory within the uploads directory .
2008-10-01 21:03:26 -04:00
*
2016-02-03 15:19:25 -05:00
* @ access private
2014-07-17 14:58:44 -04:00
* @ since 4.0 . 0
2008-10-01 21:03:26 -04:00
*
2014-07-17 14:58:44 -04:00
* @ see wp_handle_upload_error
*
2021-07-01 17:12:58 -04:00
* @ param array $file {
* Reference to a single element from `$_FILES` . Call the function once for each uploaded file .
*
* @ type string $name The original name of the file on the client machine .
* @ type string $type The mime type of the file , if the browser provided this information .
* @ type string $tmp_name The temporary filename of the file in which the uploaded file was stored on the server .
* @ type int $size The size , in bytes , of the uploaded file .
* @ type int $error The error code associated with this file upload .
* }
* @ param array | false $overrides {
2020-12-20 11:15:03 -05:00
* An array of override parameters for this file , or boolean false if none are provided .
*
* @ type callable $upload_error_handler Function to call when there is an error during the upload process .
* @ see wp_handle_upload_error () .
* @ type callable $unique_filename_callback Function to call when determining a unique file name for the file .
* @ see wp_unique_filename () .
* @ type string [] $upload_error_strings The strings that describe the error indicated in
* `$_FILES[{form field}]['error']` .
2021-01-07 09:17:11 -05:00
* @ type bool $test_form Whether to test that the `$_POST['action']` parameter is as expected .
2020-12-20 11:15:03 -05:00
* @ type bool $test_size Whether to test that the file size is greater than zero bytes .
* @ type bool $test_type Whether to test that the mime type of the file is as expected .
* @ type string [] $mimes Array of allowed mime types keyed by their file extension regex .
* }
2021-07-01 17:12:58 -04:00
* @ param string $time Time formatted in 'yyyy/mm' .
* @ param string $action Expected value for `$_POST['action']` .
* @ return array {
* On success , returns an associative array of file attributes .
* On failure , returns `$overrides['upload_error_handler']( &$file, $message )`
* or `array( 'error' => $message )` .
*
* @ type string $file Filename of the newly - uploaded file .
* @ type string $url URL of the newly - uploaded file .
* @ type string $type Mime type of the newly - uploaded file .
* }
2015-12-12 10:37:28 -05:00
*/
2014-07-17 14:58:44 -04:00
function _wp_handle_upload ( & $file , $overrides , $time , $action ) {
2007-05-27 20:04:33 -04:00
// The default error handler.
2010-02-22 11:54:51 -05:00
if ( ! function_exists ( 'wp_handle_upload_error' ) ) {
2007-05-27 20:04:33 -04:00
function wp_handle_upload_error ( & $file , $message ) {
2014-07-17 14:58:44 -04:00
return array ( 'error' => $message );
2007-05-27 20:04:33 -04:00
}
}
2014-03-23 22:45:15 -04:00
/**
2016-05-22 14:01:30 -04:00
* Filters the data for a file before it is uploaded to WordPress .
2014-03-23 22:45:15 -04:00
*
2014-11-30 06:28:24 -05:00
* The dynamic portion of the hook name , `$action` , refers to the post action .
2021-03-07 07:32:09 -05:00
*
* Possible hook names include :
2020-12-20 11:15:03 -05:00
*
* - `wp_handle_sideload_prefilter`
* - `wp_handle_upload_prefilter`
2014-11-30 06:28:24 -05:00
*
* @ since 2.9 . 0 as 'wp_handle_upload_prefilter' .
* @ since 4.0 . 0 Converted to a dynamic hook with `$action` .
2014-03-23 22:45:15 -04:00
*
2021-07-01 17:12:58 -04:00
* @ param array $file {
* Reference to a single element from `$_FILES` .
*
* @ type string $name The original name of the file on the client machine .
* @ type string $type The mime type of the file , if the browser provided this information .
* @ type string $tmp_name The temporary filename of the file in which the uploaded file was stored on the server .
* @ type int $size The size , in bytes , of the uploaded file .
* @ type int $error The error code associated with this file upload .
* }
2014-03-23 22:45:15 -04:00
*/
2014-07-17 14:58:44 -04:00
$file = apply_filters ( " { $action } _prefilter " , $file );
2009-11-05 09:24:20 -05:00
2020-12-20 11:15:03 -05:00
/**
* Filters the override parameters for a file before it is uploaded to WordPress .
*
* The dynamic portion of the hook name , `$action` , refers to the post action .
2021-03-07 07:32:09 -05:00
*
* Possible hook names include :
2020-12-20 11:15:03 -05:00
*
* - `wp_handle_sideload_overrides`
* - `wp_handle_upload_overrides`
*
* @ since 5.7 . 0
*
* @ param array | false $overrides An array of override parameters for this file . Boolean false if none are
* provided . @ see _wp_handle_upload () .
2021-07-01 17:12:58 -04:00
* @ param array $file {
* Reference to a single element from `$_FILES` .
*
* @ type string $name The original name of the file on the client machine .
* @ type string $type The mime type of the file , if the browser provided this information .
* @ type string $tmp_name The temporary filename of the file in which the uploaded file was stored on the server .
* @ type int $size The size , in bytes , of the uploaded file .
* @ type int $error The error code associated with this file upload .
* }
2020-12-20 11:15:03 -05:00
*/
$overrides = apply_filters ( " { $action } _overrides " , $overrides , $file );
2020-01-17 19:54:04 -05:00
// You may define your own function and pass the name in $overrides['upload_error_handler'].
2007-05-27 20:04:33 -04:00
$upload_error_handler = 'wp_handle_upload_error' ;
2014-05-16 12:10:38 -04:00
if ( isset ( $overrides [ 'upload_error_handler' ] ) ) {
$upload_error_handler = $overrides [ 'upload_error_handler' ];
}
2007-05-27 20:04:33 -04:00
2011-12-13 18:45:31 -05:00
// You may have had one or more 'wp_handle_upload_prefilter' functions error out the file. Handle that gracefully.
2014-07-17 14:58:44 -04:00
if ( isset ( $file [ 'error' ] ) && ! is_numeric ( $file [ 'error' ] ) && $file [ 'error' ] ) {
2016-08-09 17:54:30 -04:00
return call_user_func_array ( $upload_error_handler , array ( & $file , $file [ 'error' ] ) );
2014-07-17 14:58:44 -04:00
}
2009-11-05 09:24:20 -05:00
2014-05-16 12:10:38 -04:00
// Install user overrides. Did we mention that this voids your warranty?
2020-01-17 19:54:04 -05:00
// You may define your own function and pass the name in $overrides['unique_filename_callback'].
2008-11-14 18:01:16 -05:00
$unique_filename_callback = null ;
2014-05-16 12:10:38 -04:00
if ( isset ( $overrides [ 'unique_filename_callback' ] ) ) {
$unique_filename_callback = $overrides [ 'unique_filename_callback' ];
}
2008-11-14 18:01:16 -05:00
2014-07-17 05:14:16 -04:00
/*
2019-05-23 21:08:51 -04:00
* This may not have originally been intended to be overridable ,
2014-07-17 05:14:16 -04:00
* but historically has been .
*/
2014-05-16 12:10:38 -04:00
if ( isset ( $overrides [ 'upload_error_strings' ] ) ) {
$upload_error_strings = $overrides [ 'upload_error_strings' ];
2014-07-17 14:58:44 -04:00
} else {
// Courtesy of php.net, the strings that describe the error indicated in $_FILES[{form field}]['error'].
$upload_error_strings = array (
false ,
2020-02-20 12:18:06 -05:00
sprintf (
/* translators: 1: upload_max_filesize, 2: php.ini */
__ ( 'The uploaded file exceeds the %1$s directive in %2$s.' ),
'upload_max_filesize' ,
'php.ini'
),
sprintf (
/* translators: %s: MAX_FILE_SIZE */
__ ( 'The uploaded file exceeds the %s directive that was specified in the HTML form.' ),
'MAX_FILE_SIZE'
),
2014-07-17 14:58:44 -04:00
__ ( 'The uploaded file was only partially uploaded.' ),
__ ( 'No file was uploaded.' ),
'' ,
__ ( 'Missing a temporary folder.' ),
__ ( 'Failed to write file to disk.' ),
2017-11-30 18:11:00 -05:00
__ ( 'File upload stopped by extension.' ),
2014-07-17 14:58:44 -04:00
);
2014-05-16 12:10:38 -04:00
}
2011-07-21 13:46:01 -04:00
// All tests are on by default. Most can be turned off by $overrides[{test_name}] = false;
2014-05-16 12:10:38 -04:00
$test_form = isset ( $overrides [ 'test_form' ] ) ? $overrides [ 'test_form' ] : true ;
$test_size = isset ( $overrides [ 'test_size' ] ) ? $overrides [ 'test_size' ] : true ;
2007-05-27 20:04:33 -04:00
2014-07-17 05:14:16 -04:00
// If you override this, you must provide $ext and $type!!
2014-05-16 12:10:38 -04:00
$test_type = isset ( $overrides [ 'test_type' ] ) ? $overrides [ 'test_type' ] : true ;
2017-11-30 18:11:00 -05:00
$mimes = isset ( $overrides [ 'mimes' ] ) ? $overrides [ 'mimes' ] : false ;
2007-05-27 20:04:33 -04:00
// A correct form post will pass this test.
2021-04-12 14:19:09 -04:00
if ( $test_form && ( ! isset ( $_POST [ 'action' ] ) || $_POST [ 'action' ] !== $action ) ) {
2016-08-09 17:54:30 -04:00
return call_user_func_array ( $upload_error_handler , array ( & $file , __ ( 'Invalid form submission.' ) ) );
2014-05-16 12:10:38 -04:00
}
2021-04-12 14:19:09 -04:00
2007-05-27 20:04:33 -04:00
// A successful upload will pass this test. It makes no sense to override this one.
2014-02-27 16:16:15 -05:00
if ( isset ( $file [ 'error' ] ) && $file [ 'error' ] > 0 ) {
2016-08-09 17:54:30 -04:00
return call_user_func_array ( $upload_error_handler , array ( & $file , $upload_error_strings [ $file [ 'error' ] ] ) );
2014-02-27 16:16:15 -05:00
}
2007-05-27 20:04:33 -04:00
2018-01-17 02:16:30 -05:00
// A properly uploaded file will pass this test. There should be no reason to override this one.
2019-07-09 01:45:58 -04:00
$test_uploaded_file = 'wp_handle_upload' === $action ? is_uploaded_file ( $file [ 'tmp_name' ] ) : @ is_readable ( $file [ 'tmp_name' ] );
2018-01-17 02:16:30 -05:00
if ( ! $test_uploaded_file ) {
return call_user_func_array ( $upload_error_handler , array ( & $file , __ ( 'Specified file failed upload test.' ) ) );
}
2014-07-17 14:58:44 -04:00
$test_file_size = 'wp_handle_upload' === $action ? $file [ 'size' ] : filesize ( $file [ 'tmp_name' ] );
2007-05-27 20:04:33 -04:00
// A non-empty file will pass this test.
2014-07-17 14:58:44 -04:00
if ( $test_size && ! ( $test_file_size > 0 ) ) {
if ( is_multisite () ) {
2010-02-22 11:54:51 -05:00
$error_msg = __ ( 'File is empty. Please upload something more substantial.' );
2014-07-17 14:58:44 -04:00
} else {
2020-01-06 11:16:03 -05:00
$error_msg = sprintf (
/* translators: 1: php.ini, 2: post_max_size, 3: upload_max_filesize */
__ ( 'File is empty. Please upload something more substantial. This error could also be caused by uploads being disabled in your %1$s file or by %2$s being defined as smaller than %3$s in %1$s.' ),
'php.ini' ,
'post_max_size' ,
'upload_max_filesize'
);
2014-07-17 14:58:44 -04:00
}
2021-04-12 14:19:09 -04:00
2016-08-09 17:54:30 -04:00
return call_user_func_array ( $upload_error_handler , array ( & $file , $error_msg ) );
2010-02-22 11:54:51 -05:00
}
2007-05-27 20:04:33 -04:00
// A correct MIME type will pass this test. Override $mimes or use the upload_mimes filter.
if ( $test_type ) {
2017-11-30 18:11:00 -05:00
$wp_filetype = wp_check_filetype_and_ext ( $file [ 'tmp_name' ], $file [ 'name' ], $mimes );
$ext = empty ( $wp_filetype [ 'ext' ] ) ? '' : $wp_filetype [ 'ext' ];
$type = empty ( $wp_filetype [ 'type' ] ) ? '' : $wp_filetype [ 'type' ];
2014-05-15 00:31:14 -04:00
$proper_filename = empty ( $wp_filetype [ 'proper_filename' ] ) ? '' : $wp_filetype [ 'proper_filename' ];
2007-05-27 20:04:33 -04:00
2020-01-17 19:54:04 -05:00
// Check to see if wp_check_filetype_and_ext() determined the filename was incorrect.
2014-05-15 00:31:14 -04:00
if ( $proper_filename ) {
2010-05-15 00:47:03 -04:00
$file [ 'name' ] = $proper_filename ;
2014-05-15 00:31:14 -04:00
}
2021-04-12 14:19:09 -04:00
2017-11-30 18:11:00 -05:00
if ( ( ! $type || ! $ext ) && ! current_user_can ( 'unfiltered_upload' ) ) {
2021-11-07 18:29:56 -05:00
return call_user_func_array ( $upload_error_handler , array ( & $file , __ ( 'Sorry, you are not allowed to upload this file type.' ) ) );
2014-05-15 00:31:14 -04:00
}
2021-04-12 14:19:09 -04:00
2014-05-15 00:31:14 -04:00
if ( ! $type ) {
2008-03-06 14:48:54 -05:00
$type = $file [ 'type' ];
2014-05-15 00:31:14 -04:00
}
2009-05-07 02:50:54 -04:00
} else {
$type = '' ;
2007-05-27 20:04:33 -04:00
}
2014-07-17 05:14:16 -04:00
/*
* A writable uploads dir will pass this test . Again , there ' s no point
* overriding this one .
*/
2019-07-01 08:52:01 -04:00
$uploads = wp_upload_dir ( $time );
if ( ! ( $uploads && false === $uploads [ 'error' ] ) ) {
2016-08-09 17:54:30 -04:00
return call_user_func_array ( $upload_error_handler , array ( & $file , $uploads [ 'error' ] ) );
2014-07-17 14:58:44 -04:00
}
2007-05-27 20:04:33 -04:00
2007-12-17 22:32:43 -05:00
$filename = wp_unique_filename ( $uploads [ 'path' ], $file [ 'name' ], $unique_filename_callback );
2007-05-27 20:04:33 -04:00
2014-07-17 05:14:16 -04:00
// Move the file to the uploads dir.
2011-07-29 04:59:35 -04:00
$new_file = $uploads [ 'path' ] . " / $filename " ;
2014-07-17 14:58:44 -04:00
2017-11-30 18:11:00 -05:00
/**
2017-08-16 17:59:43 -04:00
* Filters whether to short - circuit moving the uploaded file after passing all checks .
*
2020-06-26 14:49:09 -04:00
* If a non - null value is returned from the filter , moving the file and any related
* error reporting will be completely skipped .
2017-08-16 17:59:43 -04:00
*
* @ since 4.9 . 0
*
2019-09-21 13:41:57 -04:00
* @ param mixed $move_new_file If null ( default ) move the file after the upload .
2021-07-01 17:12:58 -04:00
* @ param array $file {
* Reference to a single element from `$_FILES` .
*
* @ type string $name The original name of the file on the client machine .
* @ type string $type The mime type of the file , if the browser provided this information .
* @ type string $tmp_name The temporary filename of the file in which the uploaded file was stored on the server .
* @ type int $size The size , in bytes , of the uploaded file .
* @ type int $error The error code associated with this file upload .
* }
2019-09-21 13:41:57 -04:00
* @ param string $new_file Filename of the newly - uploaded file .
2020-06-26 14:40:04 -04:00
* @ param string $type Mime type of the newly - uploaded file .
2017-08-16 17:59:43 -04:00
*/
$move_new_file = apply_filters ( 'pre_move_uploaded_file' , null , $file , $new_file , $type );
if ( null === $move_new_file ) {
if ( 'wp_handle_upload' === $action ) {
2019-07-09 01:45:58 -04:00
$move_new_file = @ move_uploaded_file ( $file [ 'tmp_name' ], $new_file );
2014-07-17 14:58:44 -04:00
} else {
2020-01-17 19:54:04 -05:00
// Use copy and unlink because rename breaks streams.
2019-07-09 01:45:58 -04:00
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
$move_new_file = @ copy ( $file [ 'tmp_name' ], $new_file );
2017-08-16 17:59:43 -04:00
unlink ( $file [ 'tmp_name' ] );
}
if ( false === $move_new_file ) {
if ( 0 === strpos ( $uploads [ 'basedir' ], ABSPATH ) ) {
$error_path = str_replace ( ABSPATH , '' , $uploads [ 'basedir' ] ) . $uploads [ 'subdir' ];
} else {
$error_path = basename ( $uploads [ 'basedir' ] ) . $uploads [ 'subdir' ];
}
2021-04-12 14:19:09 -04:00
I18N: Improve translator comments.
* Add missing translator comments.
* Fix placement of some translator comments. Translator comments should be on the line directly above the line containing the translation function call for optimal compatibility with various `.pot` file generation tools. The CS auto-fixing, which changed some inconsistent function calls to multi-line function calls, is part of the reason why this was no longer the case for a select group of translator comments.
Includes minor code layout fixes.
Polyglots, rejoice! All WordPress core files now have translator comments for all strings with placeholders!
Props jrf, subrataemfluence, GaryJ, webdados, Dency, swissspidy, alvarogois, marcomartins, mihaiiceyro, vladwtz, niq1982, flipkeijzer, michielatyoast, chandrapatel, thrijith, joshuanoyce, FesoVik, tessak22, bhaktirajdev, cleancoded, dhavalkasvala, garrett-eclipse, bibliofille, socalchristina, priyankkpatel, 5hel2l2y, adamsilverstein, JeffPaul, pierlo, SergeyBiryukov.
Fixes #44360.
Built from https://develop.svn.wordpress.org/trunk@45926
git-svn-id: http://core.svn.wordpress.org/trunk@45737 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2019-09-01 13:13:59 -04:00
return $upload_error_handler (
$file ,
sprintf (
2019-09-02 20:41:05 -04:00
/* translators: %s: Destination file path. */
I18N: Improve translator comments.
* Add missing translator comments.
* Fix placement of some translator comments. Translator comments should be on the line directly above the line containing the translation function call for optimal compatibility with various `.pot` file generation tools. The CS auto-fixing, which changed some inconsistent function calls to multi-line function calls, is part of the reason why this was no longer the case for a select group of translator comments.
Includes minor code layout fixes.
Polyglots, rejoice! All WordPress core files now have translator comments for all strings with placeholders!
Props jrf, subrataemfluence, GaryJ, webdados, Dency, swissspidy, alvarogois, marcomartins, mihaiiceyro, vladwtz, niq1982, flipkeijzer, michielatyoast, chandrapatel, thrijith, joshuanoyce, FesoVik, tessak22, bhaktirajdev, cleancoded, dhavalkasvala, garrett-eclipse, bibliofille, socalchristina, priyankkpatel, 5hel2l2y, adamsilverstein, JeffPaul, pierlo, SergeyBiryukov.
Fixes #44360.
Built from https://develop.svn.wordpress.org/trunk@45926
git-svn-id: http://core.svn.wordpress.org/trunk@45737 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2019-09-01 13:13:59 -04:00
__ ( 'The uploaded file could not be moved to %s.' ),
$error_path
)
);
2014-07-17 14:58:44 -04:00
}
2013-06-20 22:29:26 -04:00
}
2011-10-24 15:13:23 -04:00
2014-07-17 05:14:16 -04:00
// Set correct file permissions.
2017-11-30 18:11:00 -05:00
$stat = stat ( dirname ( $new_file ) );
2007-05-27 20:04:33 -04:00
$perms = $stat [ 'mode' ] & 0000666 ;
2019-07-09 01:45:58 -04:00
chmod ( $new_file , $perms );
2007-05-27 20:04:33 -04:00
2014-07-17 05:14:16 -04:00
// Compute the URL.
2007-05-27 20:04:33 -04:00
$url = $uploads [ 'url' ] . " / $filename " ;
2014-07-17 14:58:44 -04:00
if ( is_multisite () ) {
2020-11-16 17:31:06 -05:00
clean_dirsize_cache ( $new_file );
2014-07-17 14:58:44 -04:00
}
2010-02-04 13:57:32 -05:00
2014-03-23 22:45:15 -04:00
/**
2016-05-22 14:01:30 -04:00
* Filters the data array for the uploaded file .
2014-03-23 22:45:15 -04:00
*
* @ since 2.1 . 0
*
* @ param array $upload {
* Array of upload data .
*
* @ type string $file Filename of the newly - uploaded file .
2020-06-26 14:40:04 -04:00
* @ type string $url URL of the newly - uploaded file .
* @ type string $type Mime type of the newly - uploaded file .
2014-03-23 22:45:15 -04:00
* }
2014-07-17 14:58:44 -04:00
* @ param string $context The type of upload action . Values include 'upload' or 'sideload' .
2014-03-23 22:45:15 -04:00
*/
2017-11-30 18:11:00 -05:00
return apply_filters (
2018-08-16 21:51:36 -04:00
'wp_handle_upload' ,
array (
2017-11-30 18:11:00 -05:00
'file' => $new_file ,
'url' => $url ,
'type' => $type ,
2018-08-16 21:51:36 -04:00
),
'wp_handle_sideload' === $action ? 'sideload' : 'upload'
2017-11-30 18:11:00 -05:00
);
2016-01-21 15:27:26 -05:00
}
2008-10-01 21:03:26 -04:00
/**
2016-05-23 13:28:27 -04:00
* Wrapper for _wp_handle_upload () .
*
* Passes the { @ see 'wp_handle_upload' } action .
2008-10-01 21:03:26 -04:00
*
2014-07-17 14:58:44 -04:00
* @ since 2.0 . 0
*
* @ see _wp_handle_upload ()
2008-10-01 21:03:26 -04:00
*
2021-01-03 17:04:04 -05:00
* @ param array $file Reference to a single element of `$_FILES` .
* Call the function once for each uploaded file .
2021-07-01 17:12:58 -04:00
* See _wp_handle_upload () for accepted values .
2021-01-03 17:04:04 -05:00
* @ param array | false $overrides Optional . An associative array of names => values
* to override default variables . Default false .
2021-07-01 17:12:58 -04:00
* See _wp_handle_upload () for accepted values .
2021-01-03 17:04:04 -05:00
* @ param string $time Optional . Time formatted in 'yyyy/mm' . Default null .
2021-07-01 17:12:58 -04:00
* @ return array See _wp_handle_upload () for return value .
2008-10-01 21:03:26 -04:00
*/
2014-07-17 14:58:44 -04:00
function wp_handle_upload ( & $file , $overrides = false , $time = null ) {
/*
* $_POST [ 'action' ] must be set and its value must equal $overrides [ 'action' ]
* or this :
*/
$action = 'wp_handle_upload' ;
if ( isset ( $overrides [ 'action' ] ) ) {
$action = $overrides [ 'action' ];
2014-05-16 12:15:14 -04:00
}
2008-06-04 12:13:55 -04:00
2014-07-17 14:58:44 -04:00
return _wp_handle_upload ( $file , $overrides , $time , $action );
}
2008-11-15 13:10:35 -05:00
2014-07-17 14:58:44 -04:00
/**
2016-05-23 13:28:27 -04:00
* Wrapper for _wp_handle_upload () .
*
* Passes the { @ see 'wp_handle_sideload' } action .
2014-07-17 14:58:44 -04:00
*
* @ since 2.6 . 0
*
* @ see _wp_handle_upload ()
*
2021-01-03 17:04:04 -05:00
* @ param array $file Reference to a single element of `$_FILES` .
* Call the function once for each uploaded file .
2021-07-01 17:12:58 -04:00
* See _wp_handle_upload () for accepted values .
2021-01-03 17:04:04 -05:00
* @ param array | false $overrides Optional . An associative array of names => values
* to override default variables . Default false .
2021-07-01 17:12:58 -04:00
* See _wp_handle_upload () for accepted values .
2021-01-03 17:04:04 -05:00
* @ param string $time Optional . Time formatted in 'yyyy/mm' . Default null .
2021-07-01 17:12:58 -04:00
* @ return array See _wp_handle_upload () for return value .
2014-07-17 14:58:44 -04:00
*/
function wp_handle_sideload ( & $file , $overrides = false , $time = null ) {
/*
* $_POST [ 'action' ] must be set and its value must equal $overrides [ 'action' ]
* or this :
*/
2008-06-04 12:13:55 -04:00
$action = 'wp_handle_sideload' ;
2014-05-16 12:15:14 -04:00
if ( isset ( $overrides [ 'action' ] ) ) {
$action = $overrides [ 'action' ];
}
2020-06-25 05:39:13 -04:00
2014-07-17 14:58:44 -04:00
return _wp_handle_upload ( $file , $overrides , $time , $action );
2008-06-04 12:13:55 -04:00
}
2007-05-27 20:04:33 -04:00
2008-02-11 00:45:54 -05:00
/**
2018-03-04 14:50:31 -05:00
* Downloads a URL to a local temporary file using the WordPress HTTP API .
*
* Please note that the calling function must unlink () the file .
2008-10-01 21:03:26 -04:00
*
2009-11-05 18:03:48 -05:00
* @ since 2.5 . 0
2019-03-21 01:49:50 -04:00
* @ since 5.2 . 0 Signature Verification with SoftFail was added .
Admin/HTTP API: add suggested filename support to `download_url()`.
This change allows for external clients to supply a suggested filename via a `Content-Disposition` response header. This filename is processed through `sanitize_file_name()` to ensure it is allowable (on the server, MIME's, etc...) and `validate_file()` to prevent directory traversal.
If the suggested filename fails the above processing/checks, that suggestion is discarded and the standard temporary filename (generated by WordPress) is used.
If no `Content-Disposition` header is found in the response headers, the standard temporary filename continues to be used as per normal.
Included in this change are 6 additional PHPUnit tests with 9 assertions. These tests confirm that valid filename values are correctly saved, and invalid filename values are correctly rejected.
Props cklosows, costdev, dd32, johnjamesjacoby, ocean90, psrpinto.
Fixes #38231.
Built from https://develop.svn.wordpress.org/trunk@51939
git-svn-id: http://core.svn.wordpress.org/trunk@51528 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2021-10-27 11:00:01 -04:00
* @ since 5.9 . 0 Support for Content - Disposition filename was added .
2008-10-01 21:03:26 -04:00
*
2019-04-24 03:44:51 -04:00
* @ param string $url The URL of the file to download .
2020-06-25 05:39:13 -04:00
* @ param int $timeout The timeout for the request to download the file .
* Default 300 seconds .
* @ param bool $signature_verification Whether to perform Signature Verification .
* Default false .
2018-03-04 14:50:31 -05:00
* @ return string | WP_Error Filename on success , WP_Error on failure .
2008-10-01 21:03:26 -04:00
*/
2019-04-24 03:44:51 -04:00
function download_url ( $url , $timeout = 300 , $signature_verification = false ) {
2020-01-17 19:54:04 -05:00
// WARNING: The file is not automatically deleted, the script must unlink() the file.
2017-11-30 18:11:00 -05:00
if ( ! $url ) {
return new WP_Error ( 'http_no_url' , __ ( 'Invalid URL Provided.' ) );
}
2008-02-11 00:45:54 -05:00
2021-08-16 20:16:01 -04:00
$url_path = parse_url ( $url , PHP_URL_PATH );
$url_filename = '' ;
if ( is_string ( $url_path ) && '' !== $url_path ) {
$url_filename = basename ( $url_path );
}
2016-05-30 22:21:29 -04:00
$tmpfname = wp_tempnam ( $url_filename );
2017-11-30 18:11:00 -05:00
if ( ! $tmpfname ) {
2021-04-12 14:19:09 -04:00
return new WP_Error ( 'http_no_file' , __ ( 'Could not create temporary file.' ) );
2017-11-30 18:11:00 -05:00
}
2008-02-11 00:45:54 -05:00
2017-11-30 18:11:00 -05:00
$response = wp_safe_remote_get (
2018-08-16 21:51:36 -04:00
$url ,
array (
2017-11-30 18:11:00 -05:00
'timeout' => $timeout ,
'stream' => true ,
'filename' => $tmpfname ,
)
);
2008-03-02 15:17:30 -05:00
2011-03-24 22:42:20 -04:00
if ( is_wp_error ( $response ) ) {
unlink ( $tmpfname );
2008-10-15 12:35:35 -04:00
return $response ;
}
2018-03-04 12:14:31 -05:00
$response_code = wp_remote_retrieve_response_code ( $response );
2021-04-12 14:19:09 -04:00
if ( 200 !== $response_code ) {
2018-03-04 12:14:31 -05:00
$data = array (
'code' => $response_code ,
);
// Retrieve a sample of the response body for debugging purposes.
$tmpf = fopen ( $tmpfname , 'rb' );
2021-04-12 14:19:09 -04:00
2018-03-04 12:14:31 -05:00
if ( $tmpf ) {
/**
* Filters the maximum error response body size in `download_url()` .
*
2019-01-17 01:46:50 -05:00
* @ since 5.1 . 0
2018-03-04 12:14:31 -05:00
*
* @ see download_url ()
*
* @ param int $size The maximum error response body size . Default 1 KB .
*/
$response_size = apply_filters ( 'download_url_error_max_body_size' , KB_IN_BYTES );
2021-04-12 14:19:09 -04:00
$data [ 'body' ] = fread ( $tmpf , $response_size );
2018-03-04 12:14:31 -05:00
fclose ( $tmpf );
}
2011-03-24 22:42:20 -04:00
unlink ( $tmpfname );
2021-04-12 14:19:09 -04:00
2018-03-04 12:14:31 -05:00
return new WP_Error ( 'http_404' , trim ( wp_remote_retrieve_response_message ( $response ) ), $data );
2008-03-27 03:36:30 -04:00
}
2008-10-14 12:37:08 -04:00
Admin/HTTP API: add suggested filename support to `download_url()`.
This change allows for external clients to supply a suggested filename via a `Content-Disposition` response header. This filename is processed through `sanitize_file_name()` to ensure it is allowable (on the server, MIME's, etc...) and `validate_file()` to prevent directory traversal.
If the suggested filename fails the above processing/checks, that suggestion is discarded and the standard temporary filename (generated by WordPress) is used.
If no `Content-Disposition` header is found in the response headers, the standard temporary filename continues to be used as per normal.
Included in this change are 6 additional PHPUnit tests with 9 assertions. These tests confirm that valid filename values are correctly saved, and invalid filename values are correctly rejected.
Props cklosows, costdev, dd32, johnjamesjacoby, ocean90, psrpinto.
Fixes #38231.
Built from https://develop.svn.wordpress.org/trunk@51939
git-svn-id: http://core.svn.wordpress.org/trunk@51528 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2021-10-27 11:00:01 -04:00
$content_disposition = wp_remote_retrieve_header ( $response , 'content-disposition' );
if ( $content_disposition ) {
$content_disposition = strtolower ( $content_disposition );
if ( 0 === strpos ( $content_disposition , 'attachment; filename=' ) ) {
$tmpfname_disposition = sanitize_file_name ( substr ( $content_disposition , 21 ) );
} else {
$tmpfname_disposition = '' ;
}
2021-11-24 18:59:59 -05:00
// Potential file name must be valid string.
if ( $tmpfname_disposition && is_string ( $tmpfname_disposition )
&& ( 0 === validate_file ( $tmpfname_disposition ) )
) {
2022-02-15 12:49:00 -05:00
$tmpfname_disposition = dirname ( $tmpfname ) . '/' . $tmpfname_disposition ;
Admin/HTTP API: add suggested filename support to `download_url()`.
This change allows for external clients to supply a suggested filename via a `Content-Disposition` response header. This filename is processed through `sanitize_file_name()` to ensure it is allowable (on the server, MIME's, etc...) and `validate_file()` to prevent directory traversal.
If the suggested filename fails the above processing/checks, that suggestion is discarded and the standard temporary filename (generated by WordPress) is used.
If no `Content-Disposition` header is found in the response headers, the standard temporary filename continues to be used as per normal.
Included in this change are 6 additional PHPUnit tests with 9 assertions. These tests confirm that valid filename values are correctly saved, and invalid filename values are correctly rejected.
Props cklosows, costdev, dd32, johnjamesjacoby, ocean90, psrpinto.
Fixes #38231.
Built from https://develop.svn.wordpress.org/trunk@51939
git-svn-id: http://core.svn.wordpress.org/trunk@51528 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2021-10-27 11:00:01 -04:00
if ( rename ( $tmpfname , $tmpfname_disposition ) ) {
$tmpfname = $tmpfname_disposition ;
}
if ( ( $tmpfname !== $tmpfname_disposition ) && file_exists ( $tmpfname_disposition ) ) {
unlink ( $tmpfname_disposition );
}
}
}
2013-09-21 02:54:09 -04:00
$content_md5 = wp_remote_retrieve_header ( $response , 'content-md5' );
2021-04-12 14:19:09 -04:00
2013-09-21 02:54:09 -04:00
if ( $content_md5 ) {
$md5_check = verify_file_md5 ( $tmpfname , $content_md5 );
2021-04-12 14:19:09 -04:00
2013-09-21 02:54:09 -04:00
if ( is_wp_error ( $md5_check ) ) {
unlink ( $tmpfname );
return $md5_check ;
}
}
2019-04-24 03:44:51 -04:00
// If the caller expects signature verification to occur, check to see if this URL supports it.
if ( $signature_verification ) {
/**
2019-10-26 17:09:04 -04:00
* Filters the list of hosts which should have Signature Verification attempted on .
2019-04-24 03:44:51 -04:00
*
* @ since 5.2 . 0
*
2019-10-26 17:09:04 -04:00
* @ param string [] $hostnames List of hostnames .
2019-04-24 03:44:51 -04:00
*/
2021-04-12 14:19:09 -04:00
$signed_hostnames = apply_filters ( 'wp_signature_hosts' , array ( 'wordpress.org' , 'downloads.wordpress.org' , 's.w.org' ) );
2019-04-24 03:44:51 -04:00
$signature_verification = in_array ( parse_url ( $url , PHP_URL_HOST ), $signed_hostnames , true );
}
2019-03-21 01:49:50 -04:00
2019-04-24 03:44:51 -04:00
// Perform signature valiation if supported.
2019-03-21 01:49:50 -04:00
if ( $signature_verification ) {
$signature = wp_remote_retrieve_header ( $response , 'x-content-signature' );
2021-04-12 14:19:09 -04:00
2019-03-21 01:49:50 -04:00
if ( ! $signature ) {
// Retrieve signatures from a file if the header wasn't included.
2020-01-17 19:54:04 -05:00
// WordPress.org stores signatures at $package_url.sig.
2019-04-24 03:44:51 -04:00
$signature_url = false ;
2020-05-16 14:42:12 -04:00
2021-08-16 20:16:01 -04:00
if ( is_string ( $url_path ) && ( '.zip' === substr ( $url_path , - 4 ) || '.tar.gz' === substr ( $url_path , - 7 ) ) ) {
2019-04-24 03:44:51 -04:00
$signature_url = str_replace ( $url_path , $url_path . '.sig' , $url );
}
/**
2020-08-10 20:34:08 -04:00
* Filters the URL where the signature for a file is located .
2019-04-24 03:44:51 -04:00
*
2019-04-24 19:36:51 -04:00
* @ since 5.2 . 0
2019-04-24 03:44:51 -04:00
*
* @ param false | string $signature_url The URL where signatures can be found for a file , or false if none are known .
* @ param string $url The URL being verified .
*/
$signature_url = apply_filters ( 'wp_signature_url' , $signature_url , $url );
if ( $signature_url ) {
$signature_request = wp_safe_remote_get (
$signature_url ,
array (
2020-01-17 19:54:04 -05:00
'limit_response_size' => 10 * KB_IN_BYTES , // 10KB should be large enough for quite a few signatures.
2019-04-24 03:44:51 -04:00
)
);
if ( ! is_wp_error ( $signature_request ) && 200 === wp_remote_retrieve_response_code ( $signature_request ) ) {
$signature = explode ( " \n " , wp_remote_retrieve_body ( $signature_request ) );
}
2019-03-21 01:49:50 -04:00
}
}
// Perform the checks.
2021-08-16 20:16:01 -04:00
$signature_verification = verify_file_signature ( $tmpfname , $signature , $url_filename );
2019-03-21 01:49:50 -04:00
}
if ( is_wp_error ( $signature_verification ) ) {
if (
/**
* Filters whether Signature Verification failures should be allowed to soft fail .
*
* WARNING : This may be removed from a future release .
*
* @ since 5.2 . 0
*
* @ param bool $signature_softfail If a softfail is allowed .
* @ param string $url The url being accessed .
*/
2019-04-24 03:44:51 -04:00
apply_filters ( 'wp_signature_softfail' , true , $url )
2019-03-21 01:49:50 -04:00
) {
$signature_verification -> add_data ( $tmpfname , 'softfail-filename' );
} else {
// Hard-fail.
unlink ( $tmpfname );
}
return $signature_verification ;
}
2008-02-11 00:45:54 -05:00
return $tmpfname ;
}
2013-09-21 02:54:09 -04:00
/**
2014-04-03 20:03:14 -04:00
* Calculates and compares the MD5 of a file to its expected value .
2013-09-21 02:54:09 -04:00
*
* @ since 3.7 . 0
*
2018-03-04 16:43:30 -05:00
* @ param string $filename The filename to check the MD5 of .
* @ param string $expected_md5 The expected MD5 of the file , either a base64 - encoded raw md5 ,
* or a hex - encoded md5 .
* @ return bool | WP_Error True on success , false when the MD5 format is unknown / unexpected ,
* WP_Error on failure .
2013-09-21 02:54:09 -04:00
*/
function verify_file_md5 ( $filename , $expected_md5 ) {
2021-04-12 14:19:09 -04:00
if ( 32 === strlen ( $expected_md5 ) ) {
2013-09-21 02:54:09 -04:00
$expected_raw_md5 = pack ( 'H*' , $expected_md5 );
2021-04-12 14:19:09 -04:00
} elseif ( 24 === strlen ( $expected_md5 ) ) {
2013-09-21 02:54:09 -04:00
$expected_raw_md5 = base64_decode ( $expected_md5 );
2017-11-30 18:11:00 -05:00
} else {
2020-01-17 19:54:04 -05:00
return false ; // Unknown format.
2017-11-30 18:11:00 -05:00
}
2013-09-21 02:54:09 -04:00
$file_md5 = md5_file ( $filename , true );
2017-11-30 18:11:00 -05:00
if ( $file_md5 === $expected_raw_md5 ) {
2013-09-21 02:54:09 -04:00
return true ;
2017-11-30 18:11:00 -05:00
}
2013-09-21 02:54:09 -04:00
I18N: Improve translator comments.
* Add missing translator comments.
* Fix placement of some translator comments. Translator comments should be on the line directly above the line containing the translation function call for optimal compatibility with various `.pot` file generation tools. The CS auto-fixing, which changed some inconsistent function calls to multi-line function calls, is part of the reason why this was no longer the case for a select group of translator comments.
Includes minor code layout fixes.
Polyglots, rejoice! All WordPress core files now have translator comments for all strings with placeholders!
Props jrf, subrataemfluence, GaryJ, webdados, Dency, swissspidy, alvarogois, marcomartins, mihaiiceyro, vladwtz, niq1982, flipkeijzer, michielatyoast, chandrapatel, thrijith, joshuanoyce, FesoVik, tessak22, bhaktirajdev, cleancoded, dhavalkasvala, garrett-eclipse, bibliofille, socalchristina, priyankkpatel, 5hel2l2y, adamsilverstein, JeffPaul, pierlo, SergeyBiryukov.
Fixes #44360.
Built from https://develop.svn.wordpress.org/trunk@45926
git-svn-id: http://core.svn.wordpress.org/trunk@45737 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2019-09-01 13:13:59 -04:00
return new WP_Error (
'md5_mismatch' ,
sprintf (
2019-09-02 20:41:05 -04:00
/* translators: 1: File checksum, 2: Expected checksum value. */
I18N: Improve translator comments.
* Add missing translator comments.
* Fix placement of some translator comments. Translator comments should be on the line directly above the line containing the translation function call for optimal compatibility with various `.pot` file generation tools. The CS auto-fixing, which changed some inconsistent function calls to multi-line function calls, is part of the reason why this was no longer the case for a select group of translator comments.
Includes minor code layout fixes.
Polyglots, rejoice! All WordPress core files now have translator comments for all strings with placeholders!
Props jrf, subrataemfluence, GaryJ, webdados, Dency, swissspidy, alvarogois, marcomartins, mihaiiceyro, vladwtz, niq1982, flipkeijzer, michielatyoast, chandrapatel, thrijith, joshuanoyce, FesoVik, tessak22, bhaktirajdev, cleancoded, dhavalkasvala, garrett-eclipse, bibliofille, socalchristina, priyankkpatel, 5hel2l2y, adamsilverstein, JeffPaul, pierlo, SergeyBiryukov.
Fixes #44360.
Built from https://develop.svn.wordpress.org/trunk@45926
git-svn-id: http://core.svn.wordpress.org/trunk@45737 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2019-09-01 13:13:59 -04:00
__ ( 'The checksum of the file (%1$s) does not match the expected checksum value (%2$s).' ),
bin2hex ( $file_md5 ),
bin2hex ( $expected_raw_md5 )
)
);
2013-09-21 02:54:09 -04:00
}
2019-03-21 01:49:50 -04:00
/**
* Verifies the contents of a file against its ED25519 signature .
*
* @ since 5.2 . 0
*
* @ param string $filename The file to validate .
* @ param string | array $signatures A Signature provided for the file .
2021-01-05 12:16:11 -05:00
* @ param string | false $filename_for_errors Optional . A friendly filename for errors .
2020-01-11 13:32:05 -05:00
* @ return bool | WP_Error True on success , false if verification not attempted ,
* or WP_Error describing an error condition .
2019-03-21 01:49:50 -04:00
*/
function verify_file_signature ( $filename , $signatures , $filename_for_errors = false ) {
if ( ! $filename_for_errors ) {
$filename_for_errors = wp_basename ( $filename );
}
// Check we can process signatures.
2020-04-04 23:02:11 -04:00
if ( ! function_exists ( 'sodium_crypto_sign_verify_detached' ) || ! in_array ( 'sha384' , array_map ( 'strtolower' , hash_algos () ), true ) ) {
2019-03-21 01:49:50 -04:00
return new WP_Error (
'signature_verification_unsupported' ,
sprintf (
2019-04-12 12:32:50 -04:00
/* translators: %s: The filename of the package. */
__ ( 'The authenticity of %s could not be verified as signature verification is unavailable on this system.' ),
2019-03-21 01:49:50 -04:00
'<span class="code">' . esc_html ( $filename_for_errors ) . '</span>'
),
( ! function_exists ( 'sodium_crypto_sign_verify_detached' ) ? 'sodium_crypto_sign_verify_detached' : 'sha384' )
);
}
2020-01-17 19:54:04 -05:00
// Check for a edge-case affecting PHP Maths abilities.
2019-04-24 03:44:51 -04:00
if (
! extension_loaded ( 'sodium' ) &&
2020-01-28 19:45:18 -05:00
in_array ( PHP_VERSION_ID , array ( 70200 , 70201 , 70202 ), true ) &&
2019-04-24 03:44:51 -04:00
extension_loaded ( 'opcache' )
) {
// Sodium_Compat isn't compatible with PHP 7.2.0~7.2.2 due to a bug in the PHP Opcache extension, bail early as it'll fail.
// https://bugs.php.net/bug.php?id=75938
return new WP_Error (
'signature_verification_unsupported' ,
sprintf (
2019-05-09 15:48:53 -04:00
/* translators: %s: The filename of the package. */
__ ( 'The authenticity of %s could not be verified as signature verification is unavailable on this system.' ),
2019-04-24 03:44:51 -04:00
'<span class="code">' . esc_html ( $filename_for_errors ) . '</span>'
),
array (
2022-05-20 13:38:14 -04:00
'php' => PHP_VERSION ,
2019-04-24 03:44:51 -04:00
'sodium' => defined ( 'SODIUM_LIBRARY_VERSION' ) ? SODIUM_LIBRARY_VERSION : ( defined ( 'ParagonIE_Sodium_Compat::VERSION_STRING' ) ? ParagonIE_Sodium_Compat :: VERSION_STRING : false ),
)
);
}
2019-05-17 07:06:53 -04:00
// Verify runtime speed of Sodium_Compat is acceptable.
if ( ! extension_loaded ( 'sodium' ) && ! ParagonIE_Sodium_Compat :: polyfill_is_fast () ) {
$sodium_compat_is_fast = false ;
// Allow for an old version of Sodium_Compat being loaded before the bundled WordPress one.
if ( method_exists ( 'ParagonIE_Sodium_Compat' , 'runtime_speed_test' ) ) {
2022-01-25 08:55:05 -05:00
/*
* Run `ParagonIE_Sodium_Compat::runtime_speed_test()` in optimized integer mode ,
* as that ' s what WordPress utilizes during signing verifications .
*/
2019-07-09 02:29:57 -04:00
// phpcs:disable WordPress.NamingConventions.ValidVariableName
2019-05-17 07:06:53 -04:00
$old_fastMult = ParagonIE_Sodium_Compat :: $fastMult ;
ParagonIE_Sodium_Compat :: $fastMult = true ;
$sodium_compat_is_fast = ParagonIE_Sodium_Compat :: runtime_speed_test ( 100 , 10 );
ParagonIE_Sodium_Compat :: $fastMult = $old_fastMult ;
2019-07-09 02:29:57 -04:00
// phpcs:enable
2019-05-17 07:06:53 -04:00
}
2020-01-17 19:54:04 -05:00
// This cannot be performed in a reasonable amount of time.
2019-05-17 07:06:53 -04:00
// https://github.com/paragonie/sodium_compat#help-sodium_compat-is-slow-how-can-i-make-it-fast
if ( ! $sodium_compat_is_fast ) {
return new WP_Error (
'signature_verification_unsupported' ,
sprintf (
2019-06-10 10:14:51 -04:00
/* translators: %s: The filename of the package. */
__ ( 'The authenticity of %s could not be verified as signature verification is unavailable on this system.' ),
2019-05-17 07:06:53 -04:00
'<span class="code">' . esc_html ( $filename_for_errors ) . '</span>'
),
array (
2022-05-20 13:38:14 -04:00
'php' => PHP_VERSION ,
2019-05-17 07:06:53 -04:00
'sodium' => defined ( 'SODIUM_LIBRARY_VERSION' ) ? SODIUM_LIBRARY_VERSION : ( defined ( 'ParagonIE_Sodium_Compat::VERSION_STRING' ) ? ParagonIE_Sodium_Compat :: VERSION_STRING : false ),
'polyfill_is_fast' => false ,
'max_execution_time' => ini_get ( 'max_execution_time' ),
)
);
}
}
2019-03-21 01:49:50 -04:00
if ( ! $signatures ) {
return new WP_Error (
'signature_verification_no_signature' ,
sprintf (
2019-04-12 12:32:50 -04:00
/* translators: %s: The filename of the package. */
__ ( 'The authenticity of %s could not be verified as no signature was found.' ),
2019-03-21 01:49:50 -04:00
'<span class="code">' . esc_html ( $filename_for_errors ) . '</span>'
2019-04-05 01:20:51 -04:00
),
array (
'filename' => $filename_for_errors ,
2019-03-21 01:49:50 -04:00
)
);
}
$trusted_keys = wp_trusted_keys ();
$file_hash = hash_file ( 'sha384' , $filename , true );
mbstring_binary_safe_encoding ();
2019-07-01 08:52:01 -04:00
$skipped_key = 0 ;
$skipped_signature = 0 ;
2019-04-05 01:20:51 -04:00
2019-03-21 01:49:50 -04:00
foreach ( ( array ) $signatures as $signature ) {
$signature_raw = base64_decode ( $signature );
// Ensure only valid-length signatures are considered.
if ( SODIUM_CRYPTO_SIGN_BYTES !== strlen ( $signature_raw ) ) {
2019-04-05 01:20:51 -04:00
$skipped_signature ++ ;
2019-03-21 01:49:50 -04:00
continue ;
}
foreach ( ( array ) $trusted_keys as $key ) {
$key_raw = base64_decode ( $key );
// Only pass valid public keys through.
if ( SODIUM_CRYPTO_SIGN_PUBLICKEYBYTES !== strlen ( $key_raw ) ) {
2019-04-05 01:20:51 -04:00
$skipped_key ++ ;
2019-03-21 01:49:50 -04:00
continue ;
}
if ( sodium_crypto_sign_verify_detached ( $signature_raw , $file_hash , $key_raw ) ) {
reset_mbstring_encoding ();
return true ;
}
}
}
reset_mbstring_encoding ();
return new WP_Error (
'signature_verification_failed' ,
sprintf (
2019-04-12 12:32:50 -04:00
/* translators: %s: The filename of the package. */
__ ( 'The authenticity of %s could not be verified.' ),
2019-03-21 01:49:50 -04:00
'<span class="code">' . esc_html ( $filename_for_errors ) . '</span>'
),
// Error data helpful for debugging:
array (
2019-04-05 01:20:51 -04:00
'filename' => $filename_for_errors ,
'keys' => $trusted_keys ,
'signatures' => $signatures ,
'hash' => bin2hex ( $file_hash ),
'skipped_key' => $skipped_key ,
'skipped_sig' => $skipped_signature ,
2022-05-20 13:38:14 -04:00
'php' => PHP_VERSION ,
2019-04-05 01:20:51 -04:00
'sodium' => defined ( 'SODIUM_LIBRARY_VERSION' ) ? SODIUM_LIBRARY_VERSION : ( defined ( 'ParagonIE_Sodium_Compat::VERSION_STRING' ) ? ParagonIE_Sodium_Compat :: VERSION_STRING : false ),
2019-03-21 01:49:50 -04:00
)
);
}
/**
2020-06-25 05:39:13 -04:00
* Retrieves the list of signing keys trusted by WordPress .
2019-03-21 01:49:50 -04:00
*
* @ since 5.2 . 0
*
2019-10-26 17:09:04 -04:00
* @ return string [] Array of base64 - encoded signing keys .
2019-03-21 01:49:50 -04:00
*/
function wp_trusted_keys () {
$trusted_keys = array ();
if ( time () < 1617235200 ) {
// WordPress.org Key #1 - This key is only valid before April 1st, 2021.
$trusted_keys [] = 'fRPyrxb/MvVLbdsYi+OOEv4xc+Eqpsj+kkAS6gNOkI0=' ;
}
// TODO: Add key #2 with longer expiration.
/**
2020-08-10 20:34:08 -04:00
* Filters the valid signing keys used to verify the contents of files .
2019-03-21 01:49:50 -04:00
*
* @ since 5.2 . 0
*
2019-10-26 17:09:04 -04:00
* @ param string [] $trusted_keys The trusted keys that may sign packages .
2019-03-21 01:49:50 -04:00
*/
return apply_filters ( 'wp_trusted_keys' , $trusted_keys );
}
2008-10-01 21:03:26 -04:00
/**
2018-03-04 16:43:30 -05:00
* Unzips a specified ZIP file to a location on the filesystem via the WordPress
* Filesystem Abstraction .
2008-10-01 21:03:26 -04:00
*
2018-03-04 16:43:30 -05:00
* Assumes that WP_Filesystem () has already been called and set up . Does not extract
* a root - level __MACOSX directory , if present .
*
* Attempts to increase the PHP memory limit to 256 M before uncompressing . However ,
* the most memory required shouldn ' t be much larger than the archive itself .
2008-10-01 21:03:26 -04:00
*
2009-11-05 18:03:48 -05:00
* @ since 2.5 . 0
*
2018-03-04 16:58:30 -05:00
* @ global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass .
2015-05-28 17:41:30 -04:00
*
2018-03-04 16:43:30 -05:00
* @ param string $file Full path and filename of ZIP archive .
* @ param string $to Full path on the filesystem to extract archive to .
* @ return true | WP_Error True on success , WP_Error on failure .
2008-10-01 21:03:26 -04:00
*/
2017-11-30 18:11:00 -05:00
function unzip_file ( $file , $to ) {
2008-02-11 00:45:54 -05:00
global $wp_filesystem ;
2017-11-30 18:11:00 -05:00
if ( ! $wp_filesystem || ! is_object ( $wp_filesystem ) ) {
return new WP_Error ( 'fs_unavailable' , __ ( 'Could not access filesystem.' ) );
}
2008-03-02 15:17:30 -05:00
2016-07-08 10:37:30 -04:00
// Unzip can use a lot of memory, but not this much hopefully.
wp_raise_memory_limit ( 'admin' );
2008-11-11 14:11:48 -05:00
2010-01-22 06:02:09 -05:00
$needed_dirs = array ();
2017-11-30 18:11:00 -05:00
$to = trailingslashit ( $to );
2010-01-22 06:02:09 -05:00
2019-04-02 09:34:53 -04:00
// Determine any parent directories needed (of the upgrade directory).
if ( ! $wp_filesystem -> is_dir ( $to ) ) { // Only do parents if no children exist.
2017-11-30 18:11:00 -05:00
$path = preg_split ( '![/\\\]!' , untrailingslashit ( $to ) );
for ( $i = count ( $path ); $i >= 0 ; $i -- ) {
if ( empty ( $path [ $i ] ) ) {
2008-09-13 14:06:20 -04:00
continue ;
2017-11-30 18:11:00 -05:00
}
2008-02-11 00:45:54 -05:00
2017-11-30 18:11:00 -05:00
$dir = implode ( '/' , array_slice ( $path , 0 , $i + 1 ) );
if ( preg_match ( '!^[a-z]:$!i' , $dir ) ) { // Skip it if it looks like a Windows Drive letter.
2010-01-22 06:02:09 -05:00
continue ;
2017-11-30 18:11:00 -05:00
}
2010-01-22 06:02:09 -05:00
2017-11-30 18:11:00 -05:00
if ( ! $wp_filesystem -> is_dir ( $dir ) ) {
2010-01-22 06:02:09 -05:00
$needed_dirs [] = $dir ;
2017-11-30 18:11:00 -05:00
} else {
2019-04-02 09:34:53 -04:00
break ; // A folder exists, therefore we don't need to check the levels below this.
2017-11-30 18:11:00 -05:00
}
2008-08-07 15:15:04 -04:00
}
2008-02-11 00:45:54 -05:00
}
2010-01-22 06:02:09 -05:00
2014-03-23 22:45:15 -04:00
/**
2016-05-22 14:01:30 -04:00
* Filters whether to use ZipArchive to unzip archives .
2014-03-23 22:45:15 -04:00
*
* @ since 3.0 . 0
*
* @ param bool $ziparchive Whether to use ZipArchive . Default true .
*/
2015-09-19 23:52:25 -04:00
if ( class_exists ( 'ZipArchive' , false ) && apply_filters ( 'unzip_file_use_ziparchive' , true ) ) {
2017-11-30 18:11:00 -05:00
$result = _unzip_file_ziparchive ( $file , $to , $needed_dirs );
2010-02-19 05:59:32 -05:00
if ( true === $result ) {
return $result ;
2017-11-30 18:11:00 -05:00
} elseif ( is_wp_error ( $result ) ) {
2020-05-16 14:42:12 -04:00
if ( 'incompatible_archive' !== $result -> get_error_code () ) {
2010-02-19 05:59:32 -05:00
return $result ;
2017-11-30 18:11:00 -05:00
}
2010-02-19 05:59:32 -05:00
}
}
// Fall through to PclZip if ZipArchive is not available, or encountered an error opening the file.
2017-11-30 18:11:00 -05:00
return _unzip_file_pclzip ( $file , $to , $needed_dirs );
2010-02-06 23:52:35 -05:00
}
2010-02-07 00:07:36 -05:00
/**
2018-03-04 16:43:30 -05:00
* Attempts to unzip an archive using the ZipArchive class .
*
* This function should not be called directly , use `unzip_file()` instead .
*
2010-02-07 00:07:36 -05:00
* Assumes that WP_Filesystem () has already been called and set up .
*
2010-03-26 15:13:36 -04:00
* @ since 3.0 . 0
2010-02-07 00:07:36 -05:00
* @ access private
*
2020-06-16 17:07:14 -04:00
* @ see unzip_file ()
*
2018-03-04 16:58:30 -05:00
* @ global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass .
2015-05-28 17:41:30 -04:00
*
2019-10-26 17:09:04 -04:00
* @ param string $file Full path and filename of ZIP archive .
* @ param string $to Full path on the filesystem to extract archive to .
* @ param string [] $needed_dirs A partial list of required folders needed to be created .
2018-03-04 16:43:30 -05:00
* @ return true | WP_Error True on success , WP_Error on failure .
2010-02-07 00:07:36 -05:00
*/
2017-11-30 18:11:00 -05:00
function _unzip_file_ziparchive ( $file , $to , $needed_dirs = array () ) {
2010-02-06 23:52:35 -05:00
global $wp_filesystem ;
$z = new ZipArchive ();
2010-02-07 16:28:05 -05:00
2013-10-14 17:55:09 -04:00
$zopen = $z -> open ( $file , ZIPARCHIVE :: CHECKCONS );
2021-04-12 14:19:09 -04:00
2017-11-30 18:11:00 -05:00
if ( true !== $zopen ) {
2013-10-14 17:55:09 -04:00
return new WP_Error ( 'incompatible_archive' , __ ( 'Incompatible Archive.' ), array ( 'ziparchive_error' => $zopen ) );
2017-11-30 18:11:00 -05:00
}
2010-02-06 23:52:35 -05:00
2013-10-14 16:15:09 -04:00
$uncompressed_size = 0 ;
2010-02-06 23:52:35 -05:00
for ( $i = 0 ; $i < $z -> numFiles ; $i ++ ) {
2019-07-01 08:52:01 -04:00
$info = $z -> statIndex ( $i );
2021-04-12 14:19:09 -04:00
2019-07-01 08:52:01 -04:00
if ( ! $info ) {
2013-10-11 12:06:11 -04:00
return new WP_Error ( 'stat_failed_ziparchive' , __ ( 'Could not retrieve file from archive.' ) );
2017-11-30 18:11:00 -05:00
}
2010-02-06 23:52:35 -05:00
2020-01-17 19:54:04 -05:00
if ( '__MACOSX/' === substr ( $info [ 'name' ], 0 , 9 ) ) { // Skip the OS X-created __MACOSX directory.
2010-05-02 19:24:36 -04:00
continue ;
2017-11-30 18:11:00 -05:00
}
2010-05-02 19:24:36 -04:00
2017-10-24 19:11:50 -04:00
// Don't extract invalid files:
2017-09-19 10:36:47 -04:00
if ( 0 !== validate_file ( $info [ 'name' ] ) ) {
2017-10-24 19:11:50 -04:00
continue ;
2017-09-19 10:36:47 -04:00
}
2013-10-14 16:15:09 -04:00
$uncompressed_size += $info [ 'size' ];
2019-07-01 08:52:01 -04:00
$dirname = dirname ( $info [ 'name' ] );
2016-05-12 08:40:28 -04:00
if ( '/' === substr ( $info [ 'name' ], - 1 ) ) {
// Directory.
$needed_dirs [] = $to . untrailingslashit ( $info [ 'name' ] );
2019-07-01 08:52:01 -04:00
} elseif ( '.' !== $dirname ) {
2016-05-12 08:40:28 -04:00
// Path to a file.
$needed_dirs [] = $to . untrailingslashit ( $dirname );
}
2010-02-06 23:52:35 -05:00
}
2010-02-08 13:02:23 -05:00
2013-10-14 16:58:09 -04:00
/*
* disk_free_space () could return false . Assume that any falsey value is an error .
* A disk that has zero free bytes has bigger problems .
* Require we have enough space to unzip the file and copy its contents , with a 10 % buffer .
*/
2017-05-06 10:30:40 -04:00
if ( wp_doing_cron () ) {
Upgrade/Install: Check if the `disk_free_space()` function exists before calling it.
In PHP 8+, `@` no longer suppresses fatal errors:
> The `@` operator will no longer silence fatal errors (`E_ERROR`, `E_CORE_ERROR`, `E_COMPILE_ERROR`, `E_USER_ERROR`, `E_RECOVERABLE_ERROR`, `E_PARSE`).
Reference: [https://www.php.net/manual/en/migration80.incompatible.php PHP 8: Backward Incompatible Changes].
`disk_free_space()` may be disabled by hosts, which will throw a fatal error on a call to undefined function.
This change prevents the fatal error, and falls back to `false` when `disk_free_space()` is unavailable.
Follow-up to [25540], [25774], [25776], [25831], [25869].
Props costdev, jrf, swb1192, SergeyBiryukov.
Fixes #54826. See #54730.
Built from https://develop.svn.wordpress.org/trunk@52585
git-svn-id: http://core.svn.wordpress.org/trunk@52175 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-01-17 06:41:03 -05:00
$available_space = function_exists ( 'disk_free_space' ) ? @ disk_free_space ( WP_CONTENT_DIR ) : false ;
2021-04-12 14:19:09 -04:00
2017-11-30 18:11:00 -05:00
if ( $available_space && ( $uncompressed_size * 2.1 ) > $available_space ) {
2021-04-12 14:19:09 -04:00
return new WP_Error (
'disk_full_unzip_file' ,
__ ( 'Could not copy files. You may have run out of disk space.' ),
compact ( 'uncompressed_size' , 'available_space' )
);
2017-11-30 18:11:00 -05:00
}
2013-10-24 18:59:29 -04:00
}
2013-10-14 16:15:09 -04:00
2017-11-30 18:11:00 -05:00
$needed_dirs = array_unique ( $needed_dirs );
2021-04-12 14:19:09 -04:00
2010-05-23 01:56:31 -04:00
foreach ( $needed_dirs as $dir ) {
2010-05-25 22:42:15 -04:00
// Check the parent folders of the folders all exist within the creation array.
2021-04-12 14:19:09 -04:00
if ( untrailingslashit ( $to ) === $dir ) { // Skip over the working directory, we know this exists (or will exist).
2010-05-23 01:56:31 -04:00
continue ;
2017-11-30 18:11:00 -05:00
}
2021-04-12 14:19:09 -04:00
2020-01-17 19:54:04 -05:00
if ( strpos ( $dir , $to ) === false ) { // If the directory is not within the working directory, skip it.
2010-06-07 07:19:51 -04:00
continue ;
2017-11-30 18:11:00 -05:00
}
2010-05-23 01:56:31 -04:00
2017-11-30 18:11:00 -05:00
$parent_folder = dirname ( $dir );
2021-04-12 14:19:09 -04:00
while ( ! empty ( $parent_folder )
&& untrailingslashit ( $to ) !== $parent_folder
&& ! in_array ( $parent_folder , $needed_dirs , true )
) {
2010-05-23 01:56:31 -04:00
$needed_dirs [] = $parent_folder ;
2017-11-30 18:11:00 -05:00
$parent_folder = dirname ( $parent_folder );
2010-05-23 01:56:31 -04:00
}
}
2021-04-12 14:19:09 -04:00
2017-11-30 18:11:00 -05:00
asort ( $needed_dirs );
2010-02-08 13:02:23 -05:00
2010-02-06 23:52:35 -05:00
// Create those directories if need be:
foreach ( $needed_dirs as $_dir ) {
2015-03-30 22:11:26 -04:00
// Only check to see if the Dir exists upon creation failure. Less I/O this way.
if ( ! $wp_filesystem -> mkdir ( $_dir , FS_CHMOD_DIR ) && ! $wp_filesystem -> is_dir ( $_dir ) ) {
2013-10-14 18:25:08 -04:00
return new WP_Error ( 'mkdir_failed_ziparchive' , __ ( 'Could not create directory.' ), substr ( $_dir , strlen ( $to ) ) );
2015-03-30 22:11:26 -04:00
}
2010-02-06 23:52:35 -05:00
}
2017-11-30 18:11:00 -05:00
unset ( $needed_dirs );
2010-02-08 13:02:23 -05:00
2010-02-06 23:52:35 -05:00
for ( $i = 0 ; $i < $z -> numFiles ; $i ++ ) {
2019-07-01 08:52:01 -04:00
$info = $z -> statIndex ( $i );
2021-04-12 14:19:09 -04:00
2019-07-01 08:52:01 -04:00
if ( ! $info ) {
2013-10-11 12:06:11 -04:00
return new WP_Error ( 'stat_failed_ziparchive' , __ ( 'Could not retrieve file from archive.' ) );
2017-11-30 18:11:00 -05:00
}
2010-02-06 23:52:35 -05:00
2020-05-16 14:42:12 -04:00
if ( '/' === substr ( $info [ 'name' ], - 1 ) ) { // Directory.
2010-02-06 23:52:35 -05:00
continue ;
2017-11-30 18:11:00 -05:00
}
2010-02-06 23:52:35 -05:00
2020-01-17 19:54:04 -05:00
if ( '__MACOSX/' === substr ( $info [ 'name' ], 0 , 9 ) ) { // Don't extract the OS X-created __MACOSX directory files.
2010-05-03 16:26:11 -04:00
continue ;
2017-11-30 18:11:00 -05:00
}
2010-05-02 19:24:36 -04:00
2017-10-24 19:11:50 -04:00
// Don't extract invalid files:
if ( 0 !== validate_file ( $info [ 'name' ] ) ) {
continue ;
}
2017-11-30 18:11:00 -05:00
$contents = $z -> getFromIndex ( $i );
2021-04-12 14:19:09 -04:00
2017-11-30 18:11:00 -05:00
if ( false === $contents ) {
2013-10-11 12:06:11 -04:00
return new WP_Error ( 'extract_failed_ziparchive' , __ ( 'Could not extract file from archive.' ), $info [ 'name' ] );
2017-11-30 18:11:00 -05:00
}
2010-02-07 00:02:24 -05:00
2017-11-30 18:11:00 -05:00
if ( ! $wp_filesystem -> put_contents ( $to . $info [ 'name' ], $contents , FS_CHMOD_FILE ) ) {
2013-10-14 18:25:08 -04:00
return new WP_Error ( 'copy_failed_ziparchive' , __ ( 'Could not copy file.' ), $info [ 'name' ] );
2017-11-30 18:11:00 -05:00
}
2010-02-06 23:52:35 -05:00
}
2010-02-19 05:59:32 -05:00
$z -> close ();
2010-02-06 23:52:35 -05:00
return true ;
}
2010-02-07 00:07:36 -05:00
/**
2018-03-04 16:43:30 -05:00
* Attempts to unzip an archive using the PclZip library .
*
* This function should not be called directly , use `unzip_file()` instead .
*
2010-02-07 00:07:36 -05:00
* Assumes that WP_Filesystem () has already been called and set up .
*
2010-03-26 15:13:36 -04:00
* @ since 3.0 . 0
2010-02-07 00:07:36 -05:00
* @ access private
*
2020-06-16 17:07:14 -04:00
* @ see unzip_file ()
*
2018-03-04 16:58:30 -05:00
* @ global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass .
2015-05-28 17:41:30 -04:00
*
2019-10-26 17:09:04 -04:00
* @ param string $file Full path and filename of ZIP archive .
* @ param string $to Full path on the filesystem to extract archive to .
* @ param string [] $needed_dirs A partial list of required folders needed to be created .
2018-03-04 16:43:30 -05:00
* @ return true | WP_Error True on success , WP_Error on failure .
2010-02-07 00:07:36 -05:00
*/
2017-11-30 18:11:00 -05:00
function _unzip_file_pclzip ( $file , $to , $needed_dirs = array () ) {
2010-02-06 23:52:35 -05:00
global $wp_filesystem ;
2013-09-11 04:09:08 -04:00
mbstring_binary_safe_encoding ();
2011-04-03 08:32:06 -04:00
2020-02-06 01:33:11 -05:00
require_once ABSPATH . 'wp-admin/includes/class-pclzip.php' ;
2016-08-31 12:31:29 -04:00
2017-11-30 18:11:00 -05:00
$archive = new PclZip ( $file );
2010-02-06 23:52:35 -05:00
2017-11-30 18:11:00 -05:00
$archive_files = $archive -> extract ( PCLZIP_OPT_EXTRACT_AS_STRING );
2011-04-03 08:32:06 -04:00
2013-09-11 04:09:08 -04:00
reset_mbstring_encoding ();
2011-04-03 08:32:06 -04:00
2010-02-06 23:52:35 -05:00
// Is the archive valid?
2017-11-30 18:11:00 -05:00
if ( ! is_array ( $archive_files ) ) {
return new WP_Error ( 'incompatible_archive' , __ ( 'Incompatible Archive.' ), $archive -> errorInfo ( true ) );
}
2010-02-06 23:52:35 -05:00
2020-05-23 07:38:08 -04:00
if ( 0 === count ( $archive_files ) ) {
2013-10-11 12:06:11 -04:00
return new WP_Error ( 'empty_archive_pclzip' , __ ( 'Empty archive.' ) );
2017-11-30 18:11:00 -05:00
}
2010-02-06 23:52:35 -05:00
2013-10-14 16:15:09 -04:00
$uncompressed_size = 0 ;
2020-01-17 19:54:04 -05:00
// Determine any children directories needed (From within the archive).
2010-05-02 19:24:36 -04:00
foreach ( $archive_files as $file ) {
2020-01-17 19:54:04 -05:00
if ( '__MACOSX/' === substr ( $file [ 'filename' ], 0 , 9 ) ) { // Skip the OS X-created __MACOSX directory.
2010-05-02 19:24:36 -04:00
continue ;
2017-11-30 18:11:00 -05:00
}
2010-05-03 16:26:11 -04:00
2013-10-14 16:15:09 -04:00
$uncompressed_size += $file [ 'size' ];
2017-11-30 18:11:00 -05:00
$needed_dirs [] = $to . untrailingslashit ( $file [ 'folder' ] ? $file [ 'filename' ] : dirname ( $file [ 'filename' ] ) );
2010-05-02 19:24:36 -04:00
}
2010-01-22 06:02:09 -05:00
2013-10-14 16:58:09 -04:00
/*
* disk_free_space () could return false . Assume that any falsey value is an error .
* A disk that has zero free bytes has bigger problems .
* Require we have enough space to unzip the file and copy its contents , with a 10 % buffer .
*/
2017-05-06 10:30:40 -04:00
if ( wp_doing_cron () ) {
Upgrade/Install: Check if the `disk_free_space()` function exists before calling it.
In PHP 8+, `@` no longer suppresses fatal errors:
> The `@` operator will no longer silence fatal errors (`E_ERROR`, `E_CORE_ERROR`, `E_COMPILE_ERROR`, `E_USER_ERROR`, `E_RECOVERABLE_ERROR`, `E_PARSE`).
Reference: [https://www.php.net/manual/en/migration80.incompatible.php PHP 8: Backward Incompatible Changes].
`disk_free_space()` may be disabled by hosts, which will throw a fatal error on a call to undefined function.
This change prevents the fatal error, and falls back to `false` when `disk_free_space()` is unavailable.
Follow-up to [25540], [25774], [25776], [25831], [25869].
Props costdev, jrf, swb1192, SergeyBiryukov.
Fixes #54826. See #54730.
Built from https://develop.svn.wordpress.org/trunk@52585
git-svn-id: http://core.svn.wordpress.org/trunk@52175 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-01-17 06:41:03 -05:00
$available_space = function_exists ( 'disk_free_space' ) ? @ disk_free_space ( WP_CONTENT_DIR ) : false ;
2021-04-12 14:19:09 -04:00
2017-11-30 18:11:00 -05:00
if ( $available_space && ( $uncompressed_size * 2.1 ) > $available_space ) {
2021-04-12 14:19:09 -04:00
return new WP_Error (
'disk_full_unzip_file' ,
__ ( 'Could not copy files. You may have run out of disk space.' ),
compact ( 'uncompressed_size' , 'available_space' )
);
2017-11-30 18:11:00 -05:00
}
2013-10-24 18:59:29 -04:00
}
2013-10-14 16:15:09 -04:00
2017-11-30 18:11:00 -05:00
$needed_dirs = array_unique ( $needed_dirs );
2021-04-12 14:19:09 -04:00
2010-05-23 01:56:31 -04:00
foreach ( $needed_dirs as $dir ) {
2010-05-25 22:42:15 -04:00
// Check the parent folders of the folders all exist within the creation array.
2021-04-12 14:19:09 -04:00
if ( untrailingslashit ( $to ) === $dir ) { // Skip over the working directory, we know this exists (or will exist).
2010-05-23 01:56:31 -04:00
continue ;
2017-11-30 18:11:00 -05:00
}
2021-04-12 14:19:09 -04:00
2020-01-17 19:54:04 -05:00
if ( strpos ( $dir , $to ) === false ) { // If the directory is not within the working directory, skip it.
2010-06-07 07:19:51 -04:00
continue ;
2017-11-30 18:11:00 -05:00
}
2010-05-23 01:56:31 -04:00
2017-11-30 18:11:00 -05:00
$parent_folder = dirname ( $dir );
2021-04-12 14:19:09 -04:00
while ( ! empty ( $parent_folder )
&& untrailingslashit ( $to ) !== $parent_folder
&& ! in_array ( $parent_folder , $needed_dirs , true )
) {
2010-05-23 01:56:31 -04:00
$needed_dirs [] = $parent_folder ;
2017-11-30 18:11:00 -05:00
$parent_folder = dirname ( $parent_folder );
2010-05-23 01:56:31 -04:00
}
}
2021-04-12 14:19:09 -04:00
2017-11-30 18:11:00 -05:00
asort ( $needed_dirs );
2010-01-22 06:02:09 -05:00
// Create those directories if need be:
foreach ( $needed_dirs as $_dir ) {
2013-10-15 17:24:09 -04:00
// Only check to see if the dir exists upon creation failure. Less I/O this way.
2017-11-30 18:11:00 -05:00
if ( ! $wp_filesystem -> mkdir ( $_dir , FS_CHMOD_DIR ) && ! $wp_filesystem -> is_dir ( $_dir ) ) {
2013-10-14 18:25:08 -04:00
return new WP_Error ( 'mkdir_failed_pclzip' , __ ( 'Could not create directory.' ), substr ( $_dir , strlen ( $to ) ) );
2017-11-30 18:11:00 -05:00
}
2010-01-22 06:02:09 -05:00
}
2017-11-30 18:11:00 -05:00
unset ( $needed_dirs );
2010-01-22 06:02:09 -05:00
2020-01-17 19:54:04 -05:00
// Extract the files from the zip.
2010-01-22 06:02:09 -05:00
foreach ( $archive_files as $file ) {
2017-11-30 18:11:00 -05:00
if ( $file [ 'folder' ] ) {
2010-01-22 06:02:09 -05:00
continue ;
2017-11-30 18:11:00 -05:00
}
2010-01-22 06:02:09 -05:00
2020-01-17 19:54:04 -05:00
if ( '__MACOSX/' === substr ( $file [ 'filename' ], 0 , 9 ) ) { // Don't extract the OS X-created __MACOSX directory files.
2010-05-02 19:24:36 -04:00
continue ;
2017-11-30 18:11:00 -05:00
}
2010-05-02 19:24:36 -04:00
2017-10-24 19:11:50 -04:00
// Don't extract invalid files:
2017-09-19 10:36:47 -04:00
if ( 0 !== validate_file ( $file [ 'filename' ] ) ) {
2017-10-24 19:11:50 -04:00
continue ;
2017-09-19 10:36:47 -04:00
}
2017-11-30 18:11:00 -05:00
if ( ! $wp_filesystem -> put_contents ( $to . $file [ 'filename' ], $file [ 'content' ], FS_CHMOD_FILE ) ) {
2013-10-14 18:25:08 -04:00
return new WP_Error ( 'copy_failed_pclzip' , __ ( 'Could not copy file.' ), $file [ 'filename' ] );
2017-11-30 18:11:00 -05:00
}
2010-01-22 06:02:09 -05:00
}
2021-04-12 14:19:09 -04:00
2008-03-01 16:20:23 -05:00
return true ;
2008-02-11 00:45:54 -05:00
}
2008-10-01 21:03:26 -04:00
/**
2018-03-04 16:43:30 -05:00
* Copies a directory from one location to another via the WordPress Filesystem
* Abstraction .
*
2009-11-05 18:03:48 -05:00
* Assumes that WP_Filesystem () has already been called and setup .
2008-10-01 21:03:26 -04:00
*
2009-11-05 18:03:48 -05:00
* @ since 2.5 . 0
2008-10-01 21:03:26 -04:00
*
2018-03-04 16:58:30 -05:00
* @ global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass .
2015-05-28 17:41:30 -04:00
*
2019-10-26 17:09:04 -04:00
* @ param string $from Source directory .
* @ param string $to Destination directory .
* @ param string [] $skip_list An array of files / folders to skip copying .
2018-03-04 16:43:30 -05:00
* @ return true | WP_Error True on success , WP_Error on failure .
2008-10-01 21:03:26 -04:00
*/
2017-11-30 18:11:00 -05:00
function copy_dir ( $from , $to , $skip_list = array () ) {
2008-02-11 00:45:54 -05:00
global $wp_filesystem ;
2017-11-30 18:11:00 -05:00
$dirlist = $wp_filesystem -> dirlist ( $from );
2008-02-11 00:45:54 -05:00
2021-02-02 12:23:06 -05:00
if ( false === $dirlist ) {
return new WP_Error ( 'dirlist_failed_copy_dir' , __ ( 'Directory listing failed.' ), basename ( $to ) );
}
2017-11-30 18:11:00 -05:00
$from = trailingslashit ( $from );
$to = trailingslashit ( $to );
2008-02-11 00:45:54 -05:00
foreach ( ( array ) $dirlist as $filename => $fileinfo ) {
2019-10-04 22:16:56 -04:00
if ( in_array ( $filename , $skip_list , true ) ) {
2013-09-21 02:49:11 -04:00
continue ;
2017-11-30 18:11:00 -05:00
}
2011-03-31 09:28:36 -04:00
2020-05-16 14:42:12 -04:00
if ( 'f' === $fileinfo [ 'type' ] ) {
2017-11-30 18:11:00 -05:00
if ( ! $wp_filesystem -> copy ( $from . $filename , $to . $filename , true , FS_CHMOD_FILE ) ) {
2008-11-03 12:16:41 -05:00
// If copy failed, chmod file to 0644 and try again.
2013-10-15 15:14:10 -04:00
$wp_filesystem -> chmod ( $to . $filename , FS_CHMOD_FILE );
2021-04-12 14:19:09 -04:00
2017-11-30 18:11:00 -05:00
if ( ! $wp_filesystem -> copy ( $from . $filename , $to . $filename , true , FS_CHMOD_FILE ) ) {
2013-10-11 12:06:11 -04:00
return new WP_Error ( 'copy_failed_copy_dir' , __ ( 'Could not copy file.' ), $to . $filename );
2017-11-30 18:11:00 -05:00
}
2008-11-03 12:16:41 -05:00
}
2020-06-25 05:39:13 -04:00
Upgrade/Install: Invalidate OPcache for PHP files during updates.
When files are copied into place, check whether opcode invalidation is available and attempt to invalidate to avoid unintended behavior or fatal errors from themes, plugins, or core.
Introduces `wp_opcache_invalidate()` to allow safe invalidation of PHP files from opcode cache, and a filter, `wp_opcache_invalidate_file` to override the behavior.
Replaces the existing calls to `opcache_invalidate()` in the plugin and theme editors to use the new function.
Thanks to jnylen0 for porting over a patch from ClassicPress that provided much of the approach for what is being committed.
Props nigro.simone, dd32, JasWSInc, szepe.viktor, swissspidy, JanR, asalce, Garavani, pavelevap, pputzer, GregLone, benoitchantre, jadonn, doc987, kraftbj, Krstarica, jnylen0, nextendweb, williampatton, ayeshrajans, joostdevalk, stevenkussmaul, boogah, jorbin, mikeschroder.
Fixes #36455, #50354.
Built from https://develop.svn.wordpress.org/trunk@48160
git-svn-id: http://core.svn.wordpress.org/trunk@47929 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2020-06-25 03:15:12 -04:00
wp_opcache_invalidate ( $to . $filename );
2020-05-16 14:42:12 -04:00
} elseif ( 'd' === $fileinfo [ 'type' ] ) {
2017-11-30 18:11:00 -05:00
if ( ! $wp_filesystem -> is_dir ( $to . $filename ) ) {
if ( ! $wp_filesystem -> mkdir ( $to . $filename , FS_CHMOD_DIR ) ) {
2013-10-11 12:06:11 -04:00
return new WP_Error ( 'mkdir_failed_copy_dir' , __ ( 'Could not create directory.' ), $to . $filename );
2017-11-30 18:11:00 -05:00
}
2008-08-08 18:49:35 -04:00
}
2013-09-21 02:49:11 -04:00
2020-01-17 19:54:04 -05:00
// Generate the $sub_skip_list for the subdirectory as a sub-set of the existing $skip_list.
2013-09-21 02:49:11 -04:00
$sub_skip_list = array ();
2021-04-12 14:19:09 -04:00
2013-09-21 02:49:11 -04:00
foreach ( $skip_list as $skip_item ) {
2017-11-30 18:11:00 -05:00
if ( 0 === strpos ( $skip_item , $filename . '/' ) ) {
2013-09-21 02:49:11 -04:00
$sub_skip_list [] = preg_replace ( '!^' . preg_quote ( $filename , '!' ) . '/!i' , '' , $skip_item );
2017-11-30 18:11:00 -05:00
}
2013-09-21 02:49:11 -04:00
}
2017-11-30 18:11:00 -05:00
$result = copy_dir ( $from . $filename , $to . $filename , $sub_skip_list );
2021-04-12 14:19:09 -04:00
2017-11-30 18:11:00 -05:00
if ( is_wp_error ( $result ) ) {
2008-05-29 13:29:32 -04:00
return $result ;
2017-11-30 18:11:00 -05:00
}
2008-02-11 00:45:54 -05:00
}
}
2020-05-16 14:42:12 -04:00
2009-11-05 18:03:48 -05:00
return true ;
2008-02-11 00:45:54 -05:00
}
2008-10-01 21:03:26 -04:00
/**
2020-01-17 19:54:04 -05:00
* Initializes and connects the WordPress Filesystem Abstraction classes .
2018-03-04 16:43:30 -05:00
*
2009-11-05 18:03:48 -05:00
* This function will include the chosen transport and attempt connecting .
2008-10-01 21:03:26 -04:00
*
2014-12-06 16:24:45 -05:00
* Plugins may add extra transports , And force WordPress to use them by returning
* the filename via the { @ see 'filesystem_method_file' } filter .
2008-10-01 21:03:26 -04:00
*
2009-11-05 18:03:48 -05:00
* @ since 2.5 . 0
*
2018-03-04 16:58:30 -05:00
* @ global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass .
2015-05-28 17:41:30 -04:00
*
2020-06-25 05:39:13 -04:00
* @ param array | false $args Optional . Connection args , These are passed
* directly to the `WP_Filesystem_*()` classes .
* Default false .
* @ param string | false $context Optional . Context for get_filesystem_method () .
* Default false .
* @ param bool $allow_relaxed_file_ownership Optional . Whether to allow Group / World writable .
* Default false .
* @ return bool | null True on success , false on failure ,
* null if the filesystem method class file does not exist .
2008-10-01 21:03:26 -04:00
*/
2019-07-01 04:01:57 -04:00
function WP_Filesystem ( $args = false , $context = false , $allow_relaxed_file_ownership = false ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid
2008-02-11 00:45:54 -05:00
global $wp_filesystem ;
2020-02-06 01:33:11 -05:00
require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php' ;
2016-08-31 12:31:29 -04:00
2014-11-19 00:40:23 -05:00
$method = get_filesystem_method ( $args , $context , $allow_relaxed_file_ownership );
2008-05-05 13:06:29 -04:00
2017-11-30 18:11:00 -05:00
if ( ! $method ) {
2008-02-11 00:45:54 -05:00
return false ;
2017-11-30 18:11:00 -05:00
}
2008-02-11 00:45:54 -05:00
2016-08-31 12:31:29 -04:00
if ( ! class_exists ( " WP_Filesystem_ $method " ) ) {
2014-03-23 22:45:15 -04:00
/**
2016-05-22 14:01:30 -04:00
* Filters the path for a specific filesystem method class file .
2014-03-23 22:45:15 -04:00
*
* @ since 2.6 . 0
*
* @ see get_filesystem_method ()
*
* @ param string $path Path to the specific filesystem method class file .
* @ param string $method The filesystem method to use .
*/
$abstraction_file = apply_filters ( 'filesystem_method_file' , ABSPATH . 'wp-admin/includes/class-wp-filesystem-' . $method . '.php' , $method );
2017-11-30 18:11:00 -05:00
if ( ! file_exists ( $abstraction_file ) ) {
2009-05-30 13:14:07 -04:00
return ;
2017-11-30 18:11:00 -05:00
}
2009-09-14 10:03:32 -04:00
2020-02-06 01:33:11 -05:00
require_once $abstraction_file ;
2009-05-30 13:14:07 -04:00
}
2016-08-31 12:31:29 -04:00
$method = " WP_Filesystem_ $method " ;
2008-02-11 00:45:54 -05:00
2017-11-30 18:11:00 -05:00
$wp_filesystem = new $method ( $args );
2008-02-11 00:45:54 -05:00
2020-01-17 19:54:04 -05:00
/*
* Define the timeouts for the connections . Only available after the constructor is called
* to allow for per - transport overriding of the default .
*/
2017-11-30 18:11:00 -05:00
if ( ! defined ( 'FS_CONNECT_TIMEOUT' ) ) {
2022-07-16 22:18:09 -04:00
define ( 'FS_CONNECT_TIMEOUT' , MINUTE_IN_SECONDS / 2 );
2017-11-30 18:11:00 -05:00
}
if ( ! defined ( 'FS_TIMEOUT' ) ) {
2022-07-16 22:18:09 -04:00
define ( 'FS_TIMEOUT' , MINUTE_IN_SECONDS / 2 );
2017-11-30 18:11:00 -05:00
}
2009-08-15 08:01:04 -04:00
2018-02-26 21:31:31 -05:00
if ( is_wp_error ( $wp_filesystem -> errors ) && $wp_filesystem -> errors -> has_errors () ) {
2008-02-11 00:45:54 -05:00
return false ;
2017-11-30 18:11:00 -05:00
}
2008-02-11 00:45:54 -05:00
2017-11-30 18:11:00 -05:00
if ( ! $wp_filesystem -> connect () ) {
2020-01-17 19:54:04 -05:00
return false ; // There was an error connecting to the server.
2017-11-30 18:11:00 -05:00
}
2008-02-11 00:45:54 -05:00
2008-12-04 16:58:46 -05:00
// Set the permission constants if not already set.
2017-11-30 18:11:00 -05:00
if ( ! defined ( 'FS_CHMOD_DIR' ) ) {
define ( 'FS_CHMOD_DIR' , ( fileperms ( ABSPATH ) & 0777 | 0755 ) );
}
if ( ! defined ( 'FS_CHMOD_FILE' ) ) {
define ( 'FS_CHMOD_FILE' , ( fileperms ( ABSPATH . 'index.php' ) & 0777 | 0644 ) );
}
2008-12-04 16:58:46 -05:00
2008-02-11 00:45:54 -05:00
return true ;
}
2008-10-01 21:03:26 -04:00
/**
2015-03-25 12:32:28 -04:00
* Determines which method to use for reading , writing , modifying , or deleting
* files on the filesystem .
2014-11-24 00:19:23 -05:00
*
2015-03-25 12:32:28 -04:00
* The priority of the transports are : Direct , SSH2 , FTP PHP Extension , FTP Sockets
* ( Via Sockets class , or `fsockopen()` ) . Valid values for these are : 'direct' , 'ssh2' ,
* 'ftpext' or 'ftpsockets' .
2008-10-01 21:03:26 -04:00
*
2015-03-25 12:32:28 -04:00
* The return value can be overridden by defining the `FS_METHOD` constant in `wp-config.php` ,
* or filtering via { @ see 'filesystem_method' } .
2014-11-24 00:19:23 -05:00
*
2019-07-25 18:45:57 -04:00
* @ link https :// wordpress . org / support / article / editing - wp - config - php / #wordpress-upgrade-constants
2014-11-24 00:19:23 -05:00
*
2015-03-25 12:32:28 -04:00
* Plugins may define a custom transport handler , See WP_Filesystem () .
2009-11-05 18:03:48 -05:00
*
* @ since 2.5 . 0
2008-10-01 21:03:26 -04:00
*
2015-09-25 19:58:25 -04:00
* @ global callable $_wp_filesystem_direct_method
2015-05-28 17:41:30 -04:00
*
2015-03-25 12:32:28 -04:00
* @ param array $args Optional . Connection details . Default empty array .
* @ param string $context Optional . Full path to the directory that is tested
2016-07-22 08:10:27 -04:00
* for being writable . Default empty .
2015-03-25 12:32:28 -04:00
* @ param bool $allow_relaxed_file_ownership Optional . Whether to allow Group / World writable .
* Default false .
2009-11-05 18:03:48 -05:00
* @ return string The transport to use , see description for valid return values .
2008-10-01 21:03:26 -04:00
*/
2016-07-22 08:10:27 -04:00
function get_filesystem_method ( $args = array (), $context = '' , $allow_relaxed_file_ownership = false ) {
2020-01-17 19:54:04 -05:00
// Please ensure that this is either 'direct', 'ssh2', 'ftpext', or 'ftpsockets'.
$method = defined ( 'FS_METHOD' ) ? FS_METHOD : false ;
2009-05-25 06:39:21 -04:00
2014-11-19 00:40:23 -05:00
if ( ! $context ) {
$context = WP_CONTENT_DIR ;
}
// If the directory doesn't exist (wp-content/languages) then use the parent directory as we'll create it.
2021-04-12 14:19:09 -04:00
if ( WP_LANG_DIR === $context && ! is_dir ( $context ) ) {
2014-11-19 00:40:23 -05:00
$context = dirname ( $context );
}
2013-10-16 00:15:09 -04:00
2014-11-19 00:40:23 -05:00
$context = trailingslashit ( $context );
if ( ! $method ) {
2013-10-16 00:15:09 -04:00
2017-11-23 22:05:50 -05:00
$temp_file_name = $context . 'temp-write-test-' . str_replace ( '.' , '-' , uniqid ( '' , true ) );
2017-11-30 18:11:00 -05:00
$temp_handle = @ fopen ( $temp_file_name , 'w' );
2009-05-26 20:56:32 -04:00
if ( $temp_handle ) {
2014-11-19 00:40:23 -05:00
2020-01-17 19:54:04 -05:00
// Attempt to determine the file owner of the WordPress files, and that of newly created files.
2019-07-01 08:52:01 -04:00
$wp_file_owner = false ;
$temp_file_owner = false ;
2017-11-30 18:11:00 -05:00
if ( function_exists ( 'fileowner' ) ) {
$wp_file_owner = @ fileowner ( __FILE__ );
2014-11-19 00:40:23 -05:00
$temp_file_owner = @ fileowner ( $temp_file_name );
}
2020-02-09 11:55:09 -05:00
if ( false !== $wp_file_owner && $wp_file_owner === $temp_file_owner ) {
2020-01-17 19:54:04 -05:00
/*
* WordPress is creating files as the same owner as the WordPress files ,
* this means it ' s safe to modify & create new files via PHP .
*/
2017-11-30 18:11:00 -05:00
$method = 'direct' ;
2014-12-15 09:08:23 -05:00
$GLOBALS [ '_wp_filesystem_direct_method' ] = 'file_owner' ;
2015-01-08 02:05:25 -05:00
} elseif ( $allow_relaxed_file_ownership ) {
2020-01-17 19:54:04 -05:00
/*
* The $context directory is writable , and $allow_relaxed_file_ownership is set ,
* this means we can modify files safely in this directory .
* This mode doesn ' t create new files , only alter existing ones .
*/
2017-11-30 18:11:00 -05:00
$method = 'direct' ;
2014-12-15 09:08:23 -05:00
$GLOBALS [ '_wp_filesystem_direct_method' ] = 'relaxed_ownership' ;
2014-11-19 00:40:23 -05:00
}
2019-07-09 01:45:58 -04:00
fclose ( $temp_handle );
2017-11-30 18:11:00 -05:00
@ unlink ( $temp_file_name );
2009-05-26 20:56:32 -04:00
}
2017-11-30 18:11:00 -05:00
}
2008-02-11 00:45:54 -05:00
2020-05-16 14:42:12 -04:00
if ( ! $method && isset ( $args [ 'connection_type' ] ) && 'ssh' === $args [ 'connection_type' ] && extension_loaded ( 'ssh2' ) ) {
2017-11-30 18:11:00 -05:00
$method = 'ssh2' ;
}
if ( ! $method && extension_loaded ( 'ftp' ) ) {
$method = 'ftpext' ;
}
if ( ! $method && ( extension_loaded ( 'sockets' ) || function_exists ( 'fsockopen' ) ) ) {
2020-01-17 19:54:04 -05:00
$method = 'ftpsockets' ; // Sockets: Socket extension; PHP Mode: FSockopen / fwrite / fread.
2017-11-30 18:11:00 -05:00
}
2014-03-23 22:45:15 -04:00
/**
2016-05-22 14:01:30 -04:00
* Filters the filesystem method to use .
2014-03-23 22:45:15 -04:00
*
* @ since 2.6 . 0
*
2020-06-25 05:39:13 -04:00
* @ param string $method Filesystem method to return .
* @ param array $args An array of connection details for the method .
* @ param string $context Full path to the directory that is tested for being writable .
2014-11-19 00:40:23 -05:00
* @ param bool $allow_relaxed_file_ownership Whether to allow Group / World writable .
2014-03-23 22:45:15 -04:00
*/
2014-11-19 00:40:23 -05:00
return apply_filters ( 'filesystem_method' , $method , $args , $context , $allow_relaxed_file_ownership );
2008-02-11 00:45:54 -05:00
}
2008-10-01 21:03:26 -04:00
/**
2014-11-24 00:19:23 -05:00
* Displays a form to the user to request for their FTP / SSH details in order
* to connect to the filesystem .
*
2016-07-22 08:10:27 -04:00
* All chosen / entered details are saved , excluding the password .
2008-10-01 21:03:26 -04:00
*
2014-11-24 00:19:23 -05:00
* Hostnames may be in the form of hostname : portnumber ( eg : wordpress . org : 2467 )
* to specify an alternate FTP / SSH port .
2008-10-01 21:03:26 -04:00
*
2016-07-22 08:10:27 -04:00
* Plugins may override this form by returning true | false via the { @ see 'request_filesystem_credentials' } filter .
2009-11-05 18:03:48 -05:00
*
2016-07-22 08:10:27 -04:00
* @ since 2.5 . 0
* @ since 4.6 . 0 The `$context` parameter default changed from `false` to an empty string .
2014-11-24 00:19:23 -05:00
*
2022-04-04 14:26:06 -04:00
* @ global string $pagenow The filename of the current screen .
2015-10-20 23:51:24 -04:00
*
2019-10-26 19:16:04 -04:00
* @ param string $form_post The URL to post the form to .
* @ param string $type Optional . Chosen type of filesystem . Default empty .
2020-06-25 05:39:13 -04:00
* @ param bool | WP_Error $error Optional . Whether the current request has failed
* to connect , or an error object . Default false .
* @ param string $context Optional . Full path to the directory that is tested
* for being writable . Default empty .
* @ param array $extra_fields Optional . Extra `POST` fields to be checked
* for inclusion in the post . Default null .
* @ param bool $allow_relaxed_file_ownership Optional . Whether to allow Group / World writable .
* Default false .
* @ return bool | array True if no filesystem credentials are required ,
* false if they are required but have not been provided ,
* array of credentials if they are required and have been provided .
2008-10-01 21:03:26 -04:00
*/
2016-07-22 08:10:27 -04:00
function request_filesystem_credentials ( $form_post , $type = '' , $error = false , $context = '' , $extra_fields = null , $allow_relaxed_file_ownership = false ) {
2015-10-20 23:51:24 -04:00
global $pagenow ;
2014-03-23 22:45:15 -04:00
/**
2020-03-01 05:40:07 -05:00
* Filters the filesystem credentials .
2014-03-23 22:45:15 -04:00
*
* Returning anything other than an empty string will effectively short - circuit
* output of the filesystem credentials form , returning that value instead .
*
2020-03-01 05:40:07 -05:00
* A filter should return true if no filesystem credentials are required , false if they are required but have not been
* provided , or an array of credentials if they are required and have been provided .
*
2014-03-23 22:45:15 -04:00
* @ since 2.5 . 0
2016-07-22 08:10:27 -04:00
* @ since 4.6 . 0 The `$context` parameter default changed from `false` to an empty string .
2014-03-23 22:45:15 -04:00
*
2020-03-01 05:40:07 -05:00
* @ param mixed $credentials Credentials to return instead . Default empty string .
2019-10-26 19:16:04 -04:00
* @ param string $form_post The URL to post the form to .
* @ param string $type Chosen type of filesystem .
2020-01-11 13:32:05 -05:00
* @ param bool | WP_Error $error Whether the current request has failed to connect ,
* or an error object .
2019-10-26 19:16:04 -04:00
* @ param string $context Full path to the directory that is tested for
* being writable .
* @ param array $extra_fields Extra POST fields .
2020-03-01 05:33:04 -05:00
* @ param bool $allow_relaxed_file_ownership Whether to allow Group / World writable .
2014-03-23 22:45:15 -04:00
*/
2014-11-19 00:40:23 -05:00
$req_cred = apply_filters ( 'request_filesystem_credentials' , '' , $form_post , $type , $error , $context , $extra_fields , $allow_relaxed_file_ownership );
2021-04-12 14:19:09 -04:00
2017-11-30 18:11:00 -05:00
if ( '' !== $req_cred ) {
2008-08-04 17:01:09 -04:00
return $req_cred ;
2017-11-30 18:11:00 -05:00
}
2008-08-04 17:01:09 -04:00
2017-11-30 18:11:00 -05:00
if ( empty ( $type ) ) {
2014-11-19 00:40:23 -05:00
$type = get_filesystem_method ( array (), $context , $allow_relaxed_file_ownership );
}
2008-08-04 17:01:09 -04:00
2020-05-16 14:42:12 -04:00
if ( 'direct' === $type ) {
2008-08-04 17:01:09 -04:00
return true ;
2017-11-30 18:11:00 -05:00
}
2008-08-09 01:36:14 -04:00
2017-11-30 18:11:00 -05:00
if ( is_null ( $extra_fields ) ) {
2010-03-03 14:53:39 -05:00
$extra_fields = array ( 'version' , 'locale' );
2017-11-30 18:11:00 -05:00
}
2010-03-03 14:53:39 -05:00
2017-11-30 18:11:00 -05:00
$credentials = get_option (
2018-08-16 21:51:36 -04:00
'ftp_credentials' ,
array (
2017-11-30 18:11:00 -05:00
'hostname' => '' ,
'username' => '' ,
)
);
2009-04-22 19:38:01 -04:00
2017-05-16 10:48:44 -04:00
$submitted_form = wp_unslash ( $_POST );
2020-01-17 19:54:04 -05:00
// Verify nonce, or unset submitted form field values on failure.
2017-05-16 10:48:44 -04:00
if ( ! isset ( $_POST [ '_fs_nonce' ] ) || ! wp_verify_nonce ( $_POST [ '_fs_nonce' ], 'filesystem-credentials' ) ) {
unset (
$submitted_form [ 'hostname' ],
$submitted_form [ 'username' ],
$submitted_form [ 'password' ],
$submitted_form [ 'public_key' ],
$submitted_form [ 'private_key' ],
$submitted_form [ 'connection_type' ]
);
}
2021-04-12 14:34:05 -04:00
$ftp_constants = array (
'hostname' => 'FTP_HOST' ,
'username' => 'FTP_USER' ,
'password' => 'FTP_PASS' ,
'public_key' => 'FTP_PUBKEY' ,
'private_key' => 'FTP_PRIKEY' ,
);
// If defined, set it to that. Else, if POST'd, set it to that. If not, set it to an empty string.
// Otherwise, keep it as it previously was (saved details in option).
foreach ( $ftp_constants as $key => $constant ) {
if ( defined ( $constant ) ) {
$credentials [ $key ] = constant ( $constant );
} elseif ( ! empty ( $submitted_form [ $key ] ) ) {
$credentials [ $key ] = $submitted_form [ $key ];
} elseif ( ! isset ( $credentials [ $key ] ) ) {
$credentials [ $key ] = '' ;
}
}
2008-12-09 13:03:31 -05:00
2020-01-17 19:54:04 -05:00
// Sanitize the hostname, some people might pass in odd data.
$credentials [ 'hostname' ] = preg_replace ( '|\w+://|' , '' , $credentials [ 'hostname' ] ); // Strip any schemes off.
2008-12-12 15:12:16 -05:00
2017-11-30 18:11:00 -05:00
if ( strpos ( $credentials [ 'hostname' ], ':' ) ) {
list ( $credentials [ 'hostname' ], $credentials [ 'port' ] ) = explode ( ':' , $credentials [ 'hostname' ], 2 );
if ( ! is_numeric ( $credentials [ 'port' ] ) ) {
unset ( $credentials [ 'port' ] );
}
2009-11-05 18:03:48 -05:00
} else {
2017-11-30 18:11:00 -05:00
unset ( $credentials [ 'port' ] );
2009-11-05 18:03:48 -05:00
}
2008-09-08 23:24:05 -04:00
2020-05-16 14:42:12 -04:00
if ( ( defined ( 'FTP_SSH' ) && FTP_SSH ) || ( defined ( 'FS_METHOD' ) && 'ssh2' === FS_METHOD ) ) {
2008-09-05 01:35:58 -04:00
$credentials [ 'connection_type' ] = 'ssh' ;
2020-05-16 14:42:12 -04:00
} elseif ( ( defined ( 'FTP_SSL' ) && FTP_SSL ) && 'ftpext' === $type ) { // Only the FTP Extension understands SSL.
2008-09-05 01:35:58 -04:00
$credentials [ 'connection_type' ] = 'ftps' ;
2017-05-16 10:48:44 -04:00
} elseif ( ! empty ( $submitted_form [ 'connection_type' ] ) ) {
$credentials [ 'connection_type' ] = $submitted_form [ 'connection_type' ];
2020-01-17 19:54:04 -05:00
} elseif ( ! isset ( $credentials [ 'connection_type' ] ) ) { // All else fails (and it's not defaulted to something else saved), default to FTP.
2008-09-05 01:35:58 -04:00
$credentials [ 'connection_type' ] = 'ftp' ;
2015-01-08 02:05:25 -05:00
}
2021-04-12 14:19:09 -04:00
2020-05-16 14:42:12 -04:00
if ( ! $error
2021-04-12 14:19:09 -04:00
&& ( ! empty ( $credentials [ 'hostname' ] ) && ! empty ( $credentials [ 'username' ] ) && ! empty ( $credentials [ 'password' ] )
|| 'ssh' === $credentials [ 'connection_type' ] && ! empty ( $credentials [ 'public_key' ] ) && ! empty ( $credentials [ 'private_key' ] )
2020-05-16 14:42:12 -04:00
)
) {
2008-08-04 17:01:09 -04:00
$stored_credentials = $credentials ;
2020-05-16 14:42:12 -04:00
2020-01-17 19:54:04 -05:00
if ( ! empty ( $stored_credentials [ 'port' ] ) ) { // Save port as part of hostname to simplify above code.
2008-12-12 15:12:16 -05:00
$stored_credentials [ 'hostname' ] .= ':' . $stored_credentials [ 'port' ];
2017-11-30 18:11:00 -05:00
}
2008-12-12 15:12:16 -05:00
2021-04-12 14:19:09 -04:00
unset (
$stored_credentials [ 'password' ],
$stored_credentials [ 'port' ],
$stored_credentials [ 'private_key' ],
$stored_credentials [ 'public_key' ]
);
2020-05-16 14:42:12 -04:00
Use `wp_installing()` instead of `WP_INSTALLING` constant.
The `WP_INSTALLING` constant is a flag that WordPress sets in a number of
places, telling the system that options should be fetched directly from the
database instead of from the cache, that WP should not ping wordpress.org for
updates, that the normal "not installed" checks should be bypassed, and so on.
A constant is generally necessary for this purpose, because the flag is
typically set before the WP bootstrap, meaning that WP functions are not yet
available. However, it is possible - notably, during `wpmu_create_blog()` -
for the "installing" flag to be set after WP has already loaded. In these
cases, `WP_INSTALLING` would be set for the remainder of the process, since
there's no way to change a constant once it's defined. This, in turn, polluted
later function calls that ought to have been outside the scope of site
creation, particularly the non-caching of option data. The problem was
particularly evident in the case of the automated tests, where `WP_INSTALLING`
was set the first time a site was created, and remained set for the rest of the
suite.
The new `wp_installing()` function allows developers to fetch the current
installation status (when called without any arguments) or to set the
installation status (when called with a boolean `true` or `false`). Use of
the `WP_INSTALLING` constant is still supported; `wp_installing()` will default
to `true` if the constant is defined during the bootstrap.
Props boonebgorges, jeremyfelt.
See #31130.
Built from https://develop.svn.wordpress.org/trunk@34828
git-svn-id: http://core.svn.wordpress.org/trunk@34793 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-05 11:06:28 -04:00
if ( ! wp_installing () ) {
2014-09-17 13:58:15 -04:00
update_option ( 'ftp_credentials' , $stored_credentials );
}
2020-05-16 14:42:12 -04:00
2008-08-04 17:01:09 -04:00
return $credentials ;
}
2021-04-12 14:19:09 -04:00
2017-11-30 18:11:00 -05:00
$hostname = isset ( $credentials [ 'hostname' ] ) ? $credentials [ 'hostname' ] : '' ;
$username = isset ( $credentials [ 'username' ] ) ? $credentials [ 'username' ] : '' ;
$public_key = isset ( $credentials [ 'public_key' ] ) ? $credentials [ 'public_key' ] : '' ;
$private_key = isset ( $credentials [ 'private_key' ] ) ? $credentials [ 'private_key' ] : '' ;
$port = isset ( $credentials [ 'port' ] ) ? $credentials [ 'port' ] : '' ;
2014-08-24 12:04:18 -04:00
$connection_type = isset ( $credentials [ 'connection_type' ] ) ? $credentials [ 'connection_type' ] : '' ;
2014-05-16 14:26:15 -04:00
2008-09-05 01:35:58 -04:00
if ( $error ) {
2022-06-02 11:05:13 -04:00
$error_string = __ ( '<strong>Error:</strong> Could not connect to the server. Please verify the settings are correct.' );
2017-11-30 18:11:00 -05:00
if ( is_wp_error ( $error ) ) {
2011-01-01 17:14:42 -05:00
$error_string = esc_html ( $error -> get_error_message () );
2017-11-30 18:11:00 -05:00
}
2011-01-01 17:14:42 -05:00
echo '<div id="message" class="error"><p>' . $error_string . '</p></div>' ;
2008-09-05 01:35:58 -04:00
}
2009-11-05 18:03:48 -05:00
$types = array ();
2017-11-30 18:11:00 -05:00
if ( extension_loaded ( 'ftp' ) || extension_loaded ( 'sockets' ) || function_exists ( 'fsockopen' ) ) {
$types [ 'ftp' ] = __ ( 'FTP' );
}
2020-01-17 19:54:04 -05:00
if ( extension_loaded ( 'ftp' ) ) { // Only this supports FTPS.
2017-11-30 18:11:00 -05:00
$types [ 'ftps' ] = __ ( 'FTPS (SSL)' );
}
2020-04-15 07:54:08 -04:00
if ( extension_loaded ( 'ssh2' ) ) {
2017-11-30 18:11:00 -05:00
$types [ 'ssh' ] = __ ( 'SSH2' );
}
2009-11-05 18:03:48 -05:00
2014-03-23 22:45:15 -04:00
/**
2016-05-22 14:01:30 -04:00
* Filters the connection types to output to the filesystem credentials form .
2014-03-23 22:45:15 -04:00
*
* @ since 2.9 . 0
2016-07-22 08:10:27 -04:00
* @ since 4.6 . 0 The `$context` parameter default changed from `false` to an empty string .
2014-03-23 22:45:15 -04:00
*
2019-10-26 19:16:04 -04:00
* @ param string [] $types Types of connections .
* @ param array $credentials Credentials to connect with .
* @ param string $type Chosen filesystem method .
2020-01-11 13:32:05 -05:00
* @ param bool | WP_Error $error Whether the current request has failed to connect ,
* or an error object .
2019-10-26 19:16:04 -04:00
* @ param string $context Full path to the directory that is tested for being writable .
2014-03-23 22:45:15 -04:00
*/
$types = apply_filters ( 'fs_ftp_connection_types' , $types , $credentials , $type , $error , $context );
2018-08-16 21:51:36 -04:00
?>
2017-11-30 18:11:00 -05:00
< form action = " <?php echo esc_url( $form_post ); ?> " method = " post " >
2015-03-31 22:48:28 -04:00
< div id = " request-filesystem-credentials-form " class = " request-filesystem-credentials-form " >
2018-08-16 21:51:36 -04:00
< ? php
// Print a H1 heading in the FTP credentials modal dialog, default is a H2.
$heading_tag = 'h2' ;
if ( 'plugins.php' === $pagenow || 'plugin-install.php' === $pagenow ) {
$heading_tag = 'h1' ;
}
echo " < $heading_tag id='request-filesystem-credentials-title'> " . __ ( 'Connection Information' ) . " </ $heading_tag > " ;
?>
2017-11-30 18:11:00 -05:00
< p id = " request-filesystem-credentials-desc " >
2018-08-16 21:51:36 -04:00
< ? php
2017-11-30 18:11:00 -05:00
$label_user = __ ( 'Username' );
$label_pass = __ ( 'Password' );
_e ( 'To perform the requested action, WordPress needs to access your web server.' );
2010-06-01 17:57:26 -04:00
echo ' ' ;
2018-08-16 21:51:36 -04:00
if ( ( isset ( $types [ 'ftp' ] ) || isset ( $types [ 'ftps' ] ) ) ) {
if ( isset ( $types [ 'ssh' ] ) ) {
_e ( 'Please enter your FTP or SSH credentials to proceed.' );
$label_user = __ ( 'FTP/SSH Username' );
$label_pass = __ ( 'FTP/SSH Password' );
} else {
_e ( 'Please enter your FTP credentials to proceed.' );
$label_user = __ ( 'FTP Username' );
$label_pass = __ ( 'FTP Password' );
}
echo ' ' ;
2010-06-01 16:35:59 -04:00
}
2017-11-30 18:11:00 -05:00
_e ( 'If you do not remember your credentials, you should contact your web host.' );
2017-11-22 23:09:49 -05:00
2018-08-15 02:22:26 -04:00
$hostname_value = esc_attr ( $hostname );
if ( ! empty ( $port ) ) {
$hostname_value .= " : $port " ;
}
2017-11-22 23:09:49 -05:00
$password_value = '' ;
2018-08-15 02:22:26 -04:00
if ( defined ( 'FTP_PASS' ) ) {
$password_value = '*****' ;
}
2018-08-16 21:51:36 -04:00
?>
2017-11-30 18:11:00 -05:00
</ p >
2015-03-31 22:48:28 -04:00
< label for = " hostname " >
2017-11-30 18:11:00 -05:00
< span class = " field-title " >< ? php _e ( 'Hostname' ); ?> </span>
2018-08-15 02:22:26 -04:00
< input name = " hostname " type = " text " id = " hostname " aria - describedby = " request-filesystem-credentials-desc " class = " code " placeholder = " <?php esc_attr_e( 'example: www.wordpress.org' ); ?> " value = " <?php echo $hostname_value ; ?> " < ? php disabled ( defined ( 'FTP_HOST' ) ); ?> />
2015-03-31 22:48:28 -04:00
</ label >
< div class = " ftp-username " >
< label for = " username " >
< span class = " field-title " >< ? php echo $label_user ; ?> </span>
2017-11-30 18:11:00 -05:00
< input name = " username " type = " text " id = " username " value = " <?php echo esc_attr( $username ); ?> " < ? php disabled ( defined ( 'FTP_USER' ) ); ?> />
2015-03-31 22:48:28 -04:00
</ label >
</ div >
< div class = " ftp-password " >
< label for = " password " >
< span class = " field-title " >< ? php echo $label_pass ; ?> </span>
2017-11-30 18:11:00 -05:00
< input name = " password " type = " password " id = " password " value = " <?php echo $password_value ; ?> " < ? php disabled ( defined ( 'FTP_PASS' ) ); ?> />
< ? php
if ( ! defined ( 'FTP_PASS' ) ) {
_e ( 'This password will not be stored on the server.' );}
2018-08-16 21:51:36 -04:00
?>
2015-03-31 22:48:28 -04:00
</ label >
</ div >
2015-10-20 23:51:24 -04:00
< fieldset >
2016-05-19 18:59:28 -04:00
< legend >< ? php _e ( 'Connection Type' ); ?> </legend>
2018-08-16 21:51:36 -04:00
< ? php
2016-05-19 18:59:28 -04:00
$disabled = disabled ( ( defined ( 'FTP_SSL' ) && FTP_SSL ) || ( defined ( 'FTP_SSH' ) && FTP_SSH ), true , false );
2018-08-16 21:51:36 -04:00
foreach ( $types as $name => $text ) :
?>
2017-11-30 18:11:00 -05:00
< label for = " <?php echo esc_attr( $name ); ?> " >
2018-08-15 02:22:26 -04:00
< input type = " radio " name = " connection_type " id = " <?php echo esc_attr( $name ); ?> " value = " <?php echo esc_attr( $name ); ?> " < ? php checked ( $name , $connection_type ); ?> <?php echo $disabled; ?> />
2016-05-19 18:59:28 -04:00
< ? php echo $text ; ?>
</ label >
2018-08-16 21:51:36 -04:00
< ? php
2016-05-19 18:59:28 -04:00
endforeach ;
2018-08-16 21:51:36 -04:00
?>
2016-05-19 18:59:28 -04:00
</ fieldset >
2018-08-16 21:51:36 -04:00
< ? php
if ( isset ( $types [ 'ssh' ] ) ) {
$hidden_class = '' ;
2020-05-16 14:42:12 -04:00
if ( 'ssh' !== $connection_type || empty ( $connection_type ) ) {
2018-08-16 21:51:36 -04:00
$hidden_class = ' class="hidden"' ;
}
?>
2016-12-30 02:42:42 -05:00
< fieldset id = " ssh-keys " < ? php echo $hidden_class ; ?> >
2015-10-20 23:51:24 -04:00
< legend >< ? php _e ( 'Authentication Keys' ); ?> </legend>
2015-03-31 22:48:28 -04:00
< label for = " public_key " >
2017-11-30 18:11:00 -05:00
< span class = " field-title " >< ? php _e ( 'Public Key:' ); ?> </span>
< input name = " public_key " type = " text " id = " public_key " aria - describedby = " auth-keys-desc " value = " <?php echo esc_attr( $public_key ); ?> " < ? php disabled ( defined ( 'FTP_PUBKEY' ) ); ?> />
2015-03-31 22:48:28 -04:00
</ label >
< label for = " private_key " >
2017-11-30 18:11:00 -05:00
< span class = " field-title " >< ? php _e ( 'Private Key:' ); ?> </span>
< input name = " private_key " type = " text " id = " private_key " value = " <?php echo esc_attr( $private_key ); ?> " < ? php disabled ( defined ( 'FTP_PRIKEY' ) ); ?> />
2015-03-31 22:48:28 -04:00
</ label >
2017-11-30 18:11:00 -05:00
< p id = " auth-keys-desc " >< ? php _e ( 'Enter the location on the server where the public and private keys are located. If a passphrase is needed, enter that in the password field above.' ); ?> </p>
2008-09-05 01:35:58 -04:00
</ fieldset >
2018-08-16 21:51:36 -04:00
< ? php
}
2016-05-19 18:59:28 -04:00
2018-08-16 21:51:36 -04:00
foreach ( ( array ) $extra_fields as $field ) {
if ( isset ( $submitted_form [ $field ] ) ) {
echo '<input type="hidden" name="' . esc_attr ( $field ) . '" value="' . esc_attr ( $submitted_form [ $field ] ) . '" />' ;
}
2017-11-30 18:11:00 -05:00
}
2021-05-24 15:24:57 -04:00
// Make sure the `submit_button()` function is available during the REST API call
// from WP_Site_Health_Auto_Updates::test_check_wp_filesystem_method().
if ( ! function_exists ( 'submit_button' ) ) {
require_once ABSPATH . '/wp-admin/includes/template.php' ;
}
2018-08-16 21:51:36 -04:00
?>
2015-03-31 22:48:28 -04:00
< p class = " request-filesystem-credentials-action-buttons " >
2017-05-16 10:48:44 -04:00
< ? php wp_nonce_field ( 'filesystem-credentials' , '_fs_nonce' , false , true ); ?>
2015-03-31 22:48:28 -04:00
< button class = " button cancel-button " data - js - action = " close " type = " button " >< ? php _e ( 'Cancel' ); ?> </button>
2016-09-28 15:54:28 -04:00
< ? php submit_button ( __ ( 'Proceed' ), '' , 'upgrade' , false ); ?>
2015-03-31 22:48:28 -04:00
</ p >
2008-08-04 17:01:09 -04:00
</ div >
</ form >
2018-08-16 21:51:36 -04:00
< ? php
2008-08-04 17:01:09 -04:00
return false ;
}
2015-03-17 23:18:27 -04:00
/**
2020-06-25 05:39:13 -04:00
* Prints the filesystem credentials modal when needed .
2015-03-17 23:18:27 -04:00
*
* @ since 4.2 . 0
*/
function wp_print_request_filesystem_credentials_modal () {
$filesystem_method = get_filesystem_method ();
2020-05-16 14:42:12 -04:00
2015-03-17 23:18:27 -04:00
ob_start ();
$filesystem_credentials_are_stored = request_filesystem_credentials ( self_admin_url () );
ob_end_clean ();
2020-05-16 14:42:12 -04:00
2020-02-09 11:55:09 -05:00
$request_filesystem_credentials = ( 'direct' !== $filesystem_method && ! $filesystem_credentials_are_stored );
2015-03-17 23:18:27 -04:00
if ( ! $request_filesystem_credentials ) {
return ;
}
?>
< div id = " request-filesystem-credentials-dialog " class = " notification-dialog-wrap request-filesystem-credentials-dialog " >
< div class = " notification-dialog-background " ></ div >
2015-03-31 22:48:28 -04:00
< div class = " notification-dialog " role = " dialog " aria - labelledby = " request-filesystem-credentials-title " tabindex = " 0 " >
2015-03-17 23:18:27 -04:00
< div class = " request-filesystem-credentials-dialog-content " >
< ? php request_filesystem_credentials ( site_url () ); ?>
2015-09-05 11:17:24 -04:00
</ div >
2015-03-17 23:18:27 -04:00
</ div >
</ div >
< ? php
}
Upgrade/Install: Invalidate OPcache for PHP files during updates.
When files are copied into place, check whether opcode invalidation is available and attempt to invalidate to avoid unintended behavior or fatal errors from themes, plugins, or core.
Introduces `wp_opcache_invalidate()` to allow safe invalidation of PHP files from opcode cache, and a filter, `wp_opcache_invalidate_file` to override the behavior.
Replaces the existing calls to `opcache_invalidate()` in the plugin and theme editors to use the new function.
Thanks to jnylen0 for porting over a patch from ClassicPress that provided much of the approach for what is being committed.
Props nigro.simone, dd32, JasWSInc, szepe.viktor, swissspidy, JanR, asalce, Garavani, pavelevap, pputzer, GregLone, benoitchantre, jadonn, doc987, kraftbj, Krstarica, jnylen0, nextendweb, williampatton, ayeshrajans, joostdevalk, stevenkussmaul, boogah, jorbin, mikeschroder.
Fixes #36455, #50354.
Built from https://develop.svn.wordpress.org/trunk@48160
git-svn-id: http://core.svn.wordpress.org/trunk@47929 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2020-06-25 03:15:12 -04:00
/**
2020-06-25 05:39:13 -04:00
* Attempts to clear the opcode cache for an individual PHP file .
Upgrade/Install: Invalidate OPcache for PHP files during updates.
When files are copied into place, check whether opcode invalidation is available and attempt to invalidate to avoid unintended behavior or fatal errors from themes, plugins, or core.
Introduces `wp_opcache_invalidate()` to allow safe invalidation of PHP files from opcode cache, and a filter, `wp_opcache_invalidate_file` to override the behavior.
Replaces the existing calls to `opcache_invalidate()` in the plugin and theme editors to use the new function.
Thanks to jnylen0 for porting over a patch from ClassicPress that provided much of the approach for what is being committed.
Props nigro.simone, dd32, JasWSInc, szepe.viktor, swissspidy, JanR, asalce, Garavani, pavelevap, pputzer, GregLone, benoitchantre, jadonn, doc987, kraftbj, Krstarica, jnylen0, nextendweb, williampatton, ayeshrajans, joostdevalk, stevenkussmaul, boogah, jorbin, mikeschroder.
Fixes #36455, #50354.
Built from https://develop.svn.wordpress.org/trunk@48160
git-svn-id: http://core.svn.wordpress.org/trunk@47929 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2020-06-25 03:15:12 -04:00
*
* This function can be called safely without having to check the file extension
* or availability of the OPcache extension .
*
* Whether or not invalidation is possible is cached to improve performance .
*
2020-06-25 05:39:13 -04:00
* @ since 5.5 . 0
Upgrade/Install: Invalidate OPcache for PHP files during updates.
When files are copied into place, check whether opcode invalidation is available and attempt to invalidate to avoid unintended behavior or fatal errors from themes, plugins, or core.
Introduces `wp_opcache_invalidate()` to allow safe invalidation of PHP files from opcode cache, and a filter, `wp_opcache_invalidate_file` to override the behavior.
Replaces the existing calls to `opcache_invalidate()` in the plugin and theme editors to use the new function.
Thanks to jnylen0 for porting over a patch from ClassicPress that provided much of the approach for what is being committed.
Props nigro.simone, dd32, JasWSInc, szepe.viktor, swissspidy, JanR, asalce, Garavani, pavelevap, pputzer, GregLone, benoitchantre, jadonn, doc987, kraftbj, Krstarica, jnylen0, nextendweb, williampatton, ayeshrajans, joostdevalk, stevenkussmaul, boogah, jorbin, mikeschroder.
Fixes #36455, #50354.
Built from https://develop.svn.wordpress.org/trunk@48160
git-svn-id: http://core.svn.wordpress.org/trunk@47929 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2020-06-25 03:15:12 -04:00
*
* @ link https :// www . php . net / manual / en / function . opcache - invalidate . php
*
* @ param string $filepath Path to the file , including extension , for which the opcode cache is to be cleared .
2020-06-25 05:39:13 -04:00
* @ param bool $force Invalidate even if the modification time is not newer than the file in cache .
* Default false .
* @ return bool True if opcache was invalidated for `$filepath` , or there was nothing to invalidate .
* False if opcache invalidation is not available , or is disabled via filter .
Upgrade/Install: Invalidate OPcache for PHP files during updates.
When files are copied into place, check whether opcode invalidation is available and attempt to invalidate to avoid unintended behavior or fatal errors from themes, plugins, or core.
Introduces `wp_opcache_invalidate()` to allow safe invalidation of PHP files from opcode cache, and a filter, `wp_opcache_invalidate_file` to override the behavior.
Replaces the existing calls to `opcache_invalidate()` in the plugin and theme editors to use the new function.
Thanks to jnylen0 for porting over a patch from ClassicPress that provided much of the approach for what is being committed.
Props nigro.simone, dd32, JasWSInc, szepe.viktor, swissspidy, JanR, asalce, Garavani, pavelevap, pputzer, GregLone, benoitchantre, jadonn, doc987, kraftbj, Krstarica, jnylen0, nextendweb, williampatton, ayeshrajans, joostdevalk, stevenkussmaul, boogah, jorbin, mikeschroder.
Fixes #36455, #50354.
Built from https://develop.svn.wordpress.org/trunk@48160
git-svn-id: http://core.svn.wordpress.org/trunk@47929 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2020-06-25 03:15:12 -04:00
*/
function wp_opcache_invalidate ( $filepath , $force = false ) {
static $can_invalidate = null ;
/*
* Check to see if WordPress is able to run `opcache_invalidate()` or not , and cache the value .
*
* First , check to see if the function is available to call , then if the host has restricted
* the ability to run the function to avoid a PHP warning .
*
* `opcache.restrict_api` can specify the path for files allowed to call `opcache_invalidate()` .
*
* If the host has this set , check whether the path in `opcache.restrict_api` matches
* the beginning of the path of the origin file .
*
2020-06-25 05:39:13 -04:00
* `$_SERVER['SCRIPT_FILENAME']` approximates the origin file ' s path , but `realpath()`
* is necessary because `SCRIPT_FILENAME` can be a relative path when run from CLI .
Upgrade/Install: Invalidate OPcache for PHP files during updates.
When files are copied into place, check whether opcode invalidation is available and attempt to invalidate to avoid unintended behavior or fatal errors from themes, plugins, or core.
Introduces `wp_opcache_invalidate()` to allow safe invalidation of PHP files from opcode cache, and a filter, `wp_opcache_invalidate_file` to override the behavior.
Replaces the existing calls to `opcache_invalidate()` in the plugin and theme editors to use the new function.
Thanks to jnylen0 for porting over a patch from ClassicPress that provided much of the approach for what is being committed.
Props nigro.simone, dd32, JasWSInc, szepe.viktor, swissspidy, JanR, asalce, Garavani, pavelevap, pputzer, GregLone, benoitchantre, jadonn, doc987, kraftbj, Krstarica, jnylen0, nextendweb, williampatton, ayeshrajans, joostdevalk, stevenkussmaul, boogah, jorbin, mikeschroder.
Fixes #36455, #50354.
Built from https://develop.svn.wordpress.org/trunk@48160
git-svn-id: http://core.svn.wordpress.org/trunk@47929 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2020-06-25 03:15:12 -04:00
*
* For more details , see :
* - https :// www . php . net / manual / en / opcache . configuration . php
* - https :// www . php . net / manual / en / reserved . variables . server . php
* - https :// core . trac . wordpress . org / ticket / 36455
*/
2020-06-25 05:39:13 -04:00
if ( null === $can_invalidate
&& function_exists ( 'opcache_invalidate' )
&& ( ! ini_get ( 'opcache.restrict_api' )
|| stripos ( realpath ( $_SERVER [ 'SCRIPT_FILENAME' ] ), ini_get ( 'opcache.restrict_api' ) ) === 0 )
) {
$can_invalidate = true ;
Upgrade/Install: Invalidate OPcache for PHP files during updates.
When files are copied into place, check whether opcode invalidation is available and attempt to invalidate to avoid unintended behavior or fatal errors from themes, plugins, or core.
Introduces `wp_opcache_invalidate()` to allow safe invalidation of PHP files from opcode cache, and a filter, `wp_opcache_invalidate_file` to override the behavior.
Replaces the existing calls to `opcache_invalidate()` in the plugin and theme editors to use the new function.
Thanks to jnylen0 for porting over a patch from ClassicPress that provided much of the approach for what is being committed.
Props nigro.simone, dd32, JasWSInc, szepe.viktor, swissspidy, JanR, asalce, Garavani, pavelevap, pputzer, GregLone, benoitchantre, jadonn, doc987, kraftbj, Krstarica, jnylen0, nextendweb, williampatton, ayeshrajans, joostdevalk, stevenkussmaul, boogah, jorbin, mikeschroder.
Fixes #36455, #50354.
Built from https://develop.svn.wordpress.org/trunk@48160
git-svn-id: http://core.svn.wordpress.org/trunk@47929 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2020-06-25 03:15:12 -04:00
}
// If invalidation is not available, return early.
if ( ! $can_invalidate ) {
return false ;
}
// Verify that file to be invalidated has a PHP extension.
2020-07-28 04:09:05 -04:00
if ( '.php' !== strtolower ( substr ( $filepath , - 4 ) ) ) {
Upgrade/Install: Invalidate OPcache for PHP files during updates.
When files are copied into place, check whether opcode invalidation is available and attempt to invalidate to avoid unintended behavior or fatal errors from themes, plugins, or core.
Introduces `wp_opcache_invalidate()` to allow safe invalidation of PHP files from opcode cache, and a filter, `wp_opcache_invalidate_file` to override the behavior.
Replaces the existing calls to `opcache_invalidate()` in the plugin and theme editors to use the new function.
Thanks to jnylen0 for porting over a patch from ClassicPress that provided much of the approach for what is being committed.
Props nigro.simone, dd32, JasWSInc, szepe.viktor, swissspidy, JanR, asalce, Garavani, pavelevap, pputzer, GregLone, benoitchantre, jadonn, doc987, kraftbj, Krstarica, jnylen0, nextendweb, williampatton, ayeshrajans, joostdevalk, stevenkussmaul, boogah, jorbin, mikeschroder.
Fixes #36455, #50354.
Built from https://develop.svn.wordpress.org/trunk@48160
git-svn-id: http://core.svn.wordpress.org/trunk@47929 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2020-06-25 03:15:12 -04:00
return false ;
}
/**
* Filters whether to invalidate a file from the opcode cache .
*
2020-06-25 05:39:13 -04:00
* @ since 5.5 . 0
Upgrade/Install: Invalidate OPcache for PHP files during updates.
When files are copied into place, check whether opcode invalidation is available and attempt to invalidate to avoid unintended behavior or fatal errors from themes, plugins, or core.
Introduces `wp_opcache_invalidate()` to allow safe invalidation of PHP files from opcode cache, and a filter, `wp_opcache_invalidate_file` to override the behavior.
Replaces the existing calls to `opcache_invalidate()` in the plugin and theme editors to use the new function.
Thanks to jnylen0 for porting over a patch from ClassicPress that provided much of the approach for what is being committed.
Props nigro.simone, dd32, JasWSInc, szepe.viktor, swissspidy, JanR, asalce, Garavani, pavelevap, pputzer, GregLone, benoitchantre, jadonn, doc987, kraftbj, Krstarica, jnylen0, nextendweb, williampatton, ayeshrajans, joostdevalk, stevenkussmaul, boogah, jorbin, mikeschroder.
Fixes #36455, #50354.
Built from https://develop.svn.wordpress.org/trunk@48160
git-svn-id: http://core.svn.wordpress.org/trunk@47929 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2020-06-25 03:15:12 -04:00
*
2020-07-27 05:08:05 -04:00
* @ param bool $will_invalidate Whether WordPress will invalidate `$filepath` . Default true .
* @ param string $filepath The path to the PHP file to invalidate .
Upgrade/Install: Invalidate OPcache for PHP files during updates.
When files are copied into place, check whether opcode invalidation is available and attempt to invalidate to avoid unintended behavior or fatal errors from themes, plugins, or core.
Introduces `wp_opcache_invalidate()` to allow safe invalidation of PHP files from opcode cache, and a filter, `wp_opcache_invalidate_file` to override the behavior.
Replaces the existing calls to `opcache_invalidate()` in the plugin and theme editors to use the new function.
Thanks to jnylen0 for porting over a patch from ClassicPress that provided much of the approach for what is being committed.
Props nigro.simone, dd32, JasWSInc, szepe.viktor, swissspidy, JanR, asalce, Garavani, pavelevap, pputzer, GregLone, benoitchantre, jadonn, doc987, kraftbj, Krstarica, jnylen0, nextendweb, williampatton, ayeshrajans, joostdevalk, stevenkussmaul, boogah, jorbin, mikeschroder.
Fixes #36455, #50354.
Built from https://develop.svn.wordpress.org/trunk@48160
git-svn-id: http://core.svn.wordpress.org/trunk@47929 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2020-06-25 03:15:12 -04:00
*/
if ( apply_filters ( 'wp_opcache_invalidate_file' , true , $filepath ) ) {
return opcache_invalidate ( $filepath , $force );
}
return false ;
}