Edit File by line
/home/barbar84/public_h.../wp-admin/includes
File: file.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* Filesystem API: Top-level functionality
[2] Fix | Delete
*
[3] Fix | Delete
* Functions for reading, writing, modifying, and deleting files on the file system.
[4] Fix | Delete
* Includes functionality for theme-specific files as well as operations for uploading,
[5] Fix | Delete
* archiving, and rendering output when necessary.
[6] Fix | Delete
*
[7] Fix | Delete
* @package WordPress
[8] Fix | Delete
* @subpackage Filesystem
[9] Fix | Delete
* @since 2.3.0
[10] Fix | Delete
*/
[11] Fix | Delete
[12] Fix | Delete
/** The descriptions for theme files. */
[13] Fix | Delete
$wp_file_descriptions = array(
[14] Fix | Delete
'functions.php' => __( 'Theme Functions' ),
[15] Fix | Delete
'header.php' => __( 'Theme Header' ),
[16] Fix | Delete
'footer.php' => __( 'Theme Footer' ),
[17] Fix | Delete
'sidebar.php' => __( 'Sidebar' ),
[18] Fix | Delete
'comments.php' => __( 'Comments' ),
[19] Fix | Delete
'searchform.php' => __( 'Search Form' ),
[20] Fix | Delete
'404.php' => __( '404 Template' ),
[21] Fix | Delete
'link.php' => __( 'Links Template' ),
[22] Fix | Delete
// Archives.
[23] Fix | Delete
'index.php' => __( 'Main Index Template' ),
[24] Fix | Delete
'archive.php' => __( 'Archives' ),
[25] Fix | Delete
'author.php' => __( 'Author Template' ),
[26] Fix | Delete
'taxonomy.php' => __( 'Taxonomy Template' ),
[27] Fix | Delete
'category.php' => __( 'Category Template' ),
[28] Fix | Delete
'tag.php' => __( 'Tag Template' ),
[29] Fix | Delete
'home.php' => __( 'Posts Page' ),
[30] Fix | Delete
'search.php' => __( 'Search Results' ),
[31] Fix | Delete
'date.php' => __( 'Date Template' ),
[32] Fix | Delete
// Content.
[33] Fix | Delete
'singular.php' => __( 'Singular Template' ),
[34] Fix | Delete
'single.php' => __( 'Single Post' ),
[35] Fix | Delete
'page.php' => __( 'Single Page' ),
[36] Fix | Delete
'front-page.php' => __( 'Homepage' ),
[37] Fix | Delete
'privacy-policy.php' => __( 'Privacy Policy Page' ),
[38] Fix | Delete
// Attachments.
[39] Fix | Delete
'attachment.php' => __( 'Attachment Template' ),
[40] Fix | Delete
'image.php' => __( 'Image Attachment Template' ),
[41] Fix | Delete
'video.php' => __( 'Video Attachment Template' ),
[42] Fix | Delete
'audio.php' => __( 'Audio Attachment Template' ),
[43] Fix | Delete
'application.php' => __( 'Application Attachment Template' ),
[44] Fix | Delete
// Embeds.
[45] Fix | Delete
'embed.php' => __( 'Embed Template' ),
[46] Fix | Delete
'embed-404.php' => __( 'Embed 404 Template' ),
[47] Fix | Delete
'embed-content.php' => __( 'Embed Content Template' ),
[48] Fix | Delete
'header-embed.php' => __( 'Embed Header Template' ),
[49] Fix | Delete
'footer-embed.php' => __( 'Embed Footer Template' ),
[50] Fix | Delete
// Stylesheets.
[51] Fix | Delete
'style.css' => __( 'Stylesheet' ),
[52] Fix | Delete
'editor-style.css' => __( 'Visual Editor Stylesheet' ),
[53] Fix | Delete
'editor-style-rtl.css' => __( 'Visual Editor RTL Stylesheet' ),
[54] Fix | Delete
'rtl.css' => __( 'RTL Stylesheet' ),
[55] Fix | Delete
// Other.
[56] Fix | Delete
'my-hacks.php' => __( 'my-hacks.php (legacy hacks support)' ),
[57] Fix | Delete
'.htaccess' => __( '.htaccess (for rewrite rules )' ),
[58] Fix | Delete
// Deprecated files.
[59] Fix | Delete
'wp-layout.css' => __( 'Stylesheet' ),
[60] Fix | Delete
'wp-comments.php' => __( 'Comments Template' ),
[61] Fix | Delete
'wp-comments-popup.php' => __( 'Popup Comments Template' ),
[62] Fix | Delete
'comments-popup.php' => __( 'Popup Comments' ),
[63] Fix | Delete
);
[64] Fix | Delete
[65] Fix | Delete
/**
[66] Fix | Delete
* Gets the description for standard WordPress theme files.
[67] Fix | Delete
*
[68] Fix | Delete
* @since 1.5.0
[69] Fix | Delete
*
[70] Fix | Delete
* @global array $wp_file_descriptions Theme file descriptions.
[71] Fix | Delete
* @global array $allowed_files List of allowed files.
[72] Fix | Delete
*
[73] Fix | Delete
* @param string $file Filesystem path or filename.
[74] Fix | Delete
* @return string Description of file from $wp_file_descriptions or basename of $file if description doesn't exist.
[75] Fix | Delete
* Appends 'Page Template' to basename of $file if the file is a page template.
[76] Fix | Delete
*/
[77] Fix | Delete
function get_file_description( $file ) {
[78] Fix | Delete
global $wp_file_descriptions, $allowed_files;
[79] Fix | Delete
[80] Fix | Delete
$dirname = pathinfo( $file, PATHINFO_DIRNAME );
[81] Fix | Delete
$file_path = $allowed_files[ $file ];
[82] Fix | Delete
[83] Fix | Delete
if ( isset( $wp_file_descriptions[ basename( $file ) ] ) && '.' === $dirname ) {
[84] Fix | Delete
return $wp_file_descriptions[ basename( $file ) ];
[85] Fix | Delete
} elseif ( file_exists( $file_path ) && is_file( $file_path ) ) {
[86] Fix | Delete
$template_data = implode( '', file( $file_path ) );
[87] Fix | Delete
[88] Fix | Delete
if ( preg_match( '|Template Name:(.*)$|mi', $template_data, $name ) ) {
[89] Fix | Delete
/* translators: %s: Template name. */
[90] Fix | Delete
return sprintf( __( '%s Page Template' ), _cleanup_header_comment( $name[1] ) );
[91] Fix | Delete
}
[92] Fix | Delete
}
[93] Fix | Delete
[94] Fix | Delete
return trim( basename( $file ) );
[95] Fix | Delete
}
[96] Fix | Delete
[97] Fix | Delete
/**
[98] Fix | Delete
* Gets the absolute filesystem path to the root of the WordPress installation.
[99] Fix | Delete
*
[100] Fix | Delete
* @since 1.5.0
[101] Fix | Delete
*
[102] Fix | Delete
* @return string Full filesystem path to the root of the WordPress installation.
[103] Fix | Delete
*/
[104] Fix | Delete
function get_home_path() {
[105] Fix | Delete
$home = set_url_scheme( get_option( 'home' ), 'http' );
[106] Fix | Delete
$siteurl = set_url_scheme( get_option( 'siteurl' ), 'http' );
[107] Fix | Delete
[108] Fix | Delete
if ( ! empty( $home ) && 0 !== strcasecmp( $home, $siteurl ) ) {
[109] Fix | Delete
$wp_path_rel_to_home = str_ireplace( $home, '', $siteurl ); /* $siteurl - $home */
[110] Fix | Delete
$pos = strripos( str_replace( '\\', '/', $_SERVER['SCRIPT_FILENAME'] ), trailingslashit( $wp_path_rel_to_home ) );
[111] Fix | Delete
$home_path = substr( $_SERVER['SCRIPT_FILENAME'], 0, $pos );
[112] Fix | Delete
$home_path = trailingslashit( $home_path );
[113] Fix | Delete
} else {
[114] Fix | Delete
$home_path = ABSPATH;
[115] Fix | Delete
}
[116] Fix | Delete
[117] Fix | Delete
return str_replace( '\\', '/', $home_path );
[118] Fix | Delete
}
[119] Fix | Delete
[120] Fix | Delete
/**
[121] Fix | Delete
* Returns a listing of all files in the specified folder and all subdirectories up to 100 levels deep.
[122] Fix | Delete
*
[123] Fix | Delete
* The depth of the recursiveness can be controlled by the $levels param.
[124] Fix | Delete
*
[125] Fix | Delete
* @since 2.6.0
[126] Fix | Delete
* @since 4.9.0 Added the `$exclusions` parameter.
[127] Fix | Delete
*
[128] Fix | Delete
* @param string $folder Optional. Full path to folder. Default empty.
[129] Fix | Delete
* @param int $levels Optional. Levels of folders to follow, Default 100 (PHP Loop limit).
[130] Fix | Delete
* @param string[] $exclusions Optional. List of folders and files to skip.
[131] Fix | Delete
* @return string[]|false Array of files on success, false on failure.
[132] Fix | Delete
*/
[133] Fix | Delete
function list_files( $folder = '', $levels = 100, $exclusions = array() ) {
[134] Fix | Delete
if ( empty( $folder ) ) {
[135] Fix | Delete
return false;
[136] Fix | Delete
}
[137] Fix | Delete
[138] Fix | Delete
$folder = trailingslashit( $folder );
[139] Fix | Delete
[140] Fix | Delete
if ( ! $levels ) {
[141] Fix | Delete
return false;
[142] Fix | Delete
}
[143] Fix | Delete
[144] Fix | Delete
$files = array();
[145] Fix | Delete
[146] Fix | Delete
$dir = @opendir( $folder );
[147] Fix | Delete
if ( $dir ) {
[148] Fix | Delete
while ( ( $file = readdir( $dir ) ) !== false ) {
[149] Fix | Delete
// Skip current and parent folder links.
[150] Fix | Delete
if ( in_array( $file, array( '.', '..' ), true ) ) {
[151] Fix | Delete
continue;
[152] Fix | Delete
}
[153] Fix | Delete
[154] Fix | Delete
// Skip hidden and excluded files.
[155] Fix | Delete
if ( '.' === $file[0] || in_array( $file, $exclusions, true ) ) {
[156] Fix | Delete
continue;
[157] Fix | Delete
}
[158] Fix | Delete
[159] Fix | Delete
if ( is_dir( $folder . $file ) ) {
[160] Fix | Delete
$files2 = list_files( $folder . $file, $levels - 1 );
[161] Fix | Delete
if ( $files2 ) {
[162] Fix | Delete
$files = array_merge( $files, $files2 );
[163] Fix | Delete
} else {
[164] Fix | Delete
$files[] = $folder . $file . '/';
[165] Fix | Delete
}
[166] Fix | Delete
} else {
[167] Fix | Delete
$files[] = $folder . $file;
[168] Fix | Delete
}
[169] Fix | Delete
}
[170] Fix | Delete
[171] Fix | Delete
closedir( $dir );
[172] Fix | Delete
}
[173] Fix | Delete
[174] Fix | Delete
return $files;
[175] Fix | Delete
}
[176] Fix | Delete
[177] Fix | Delete
/**
[178] Fix | Delete
* Gets the list of file extensions that are editable in plugins.
[179] Fix | Delete
*
[180] Fix | Delete
* @since 4.9.0
[181] Fix | Delete
*
[182] Fix | Delete
* @param string $plugin Path to the plugin file relative to the plugins directory.
[183] Fix | Delete
* @return string[] Array of editable file extensions.
[184] Fix | Delete
*/
[185] Fix | Delete
function wp_get_plugin_file_editable_extensions( $plugin ) {
[186] Fix | Delete
[187] Fix | Delete
$default_types = array(
[188] Fix | Delete
'bash',
[189] Fix | Delete
'conf',
[190] Fix | Delete
'css',
[191] Fix | Delete
'diff',
[192] Fix | Delete
'htm',
[193] Fix | Delete
'html',
[194] Fix | Delete
'http',
[195] Fix | Delete
'inc',
[196] Fix | Delete
'include',
[197] Fix | Delete
'js',
[198] Fix | Delete
'json',
[199] Fix | Delete
'jsx',
[200] Fix | Delete
'less',
[201] Fix | Delete
'md',
[202] Fix | Delete
'patch',
[203] Fix | Delete
'php',
[204] Fix | Delete
'php3',
[205] Fix | Delete
'php4',
[206] Fix | Delete
'php5',
[207] Fix | Delete
'php7',
[208] Fix | Delete
'phps',
[209] Fix | Delete
'phtml',
[210] Fix | Delete
'sass',
[211] Fix | Delete
'scss',
[212] Fix | Delete
'sh',
[213] Fix | Delete
'sql',
[214] Fix | Delete
'svg',
[215] Fix | Delete
'text',
[216] Fix | Delete
'txt',
[217] Fix | Delete
'xml',
[218] Fix | Delete
'yaml',
[219] Fix | Delete
'yml',
[220] Fix | Delete
);
[221] Fix | Delete
[222] Fix | Delete
/**
[223] Fix | Delete
* Filters the list of file types allowed for editing in the plugin editor.
[224] Fix | Delete
*
[225] Fix | Delete
* @since 2.8.0
[226] Fix | Delete
* @since 4.9.0 Added the `$plugin` parameter.
[227] Fix | Delete
*
[228] Fix | Delete
* @param string[] $default_types An array of editable plugin file extensions.
[229] Fix | Delete
* @param string $plugin Path to the plugin file relative to the plugins directory.
[230] Fix | Delete
*/
[231] Fix | Delete
$file_types = (array) apply_filters( 'editable_extensions', $default_types, $plugin );
[232] Fix | Delete
[233] Fix | Delete
return $file_types;
[234] Fix | Delete
}
[235] Fix | Delete
[236] Fix | Delete
/**
[237] Fix | Delete
* Gets the list of file extensions that are editable for a given theme.
[238] Fix | Delete
*
[239] Fix | Delete
* @since 4.9.0
[240] Fix | Delete
*
[241] Fix | Delete
* @param WP_Theme $theme Theme object.
[242] Fix | Delete
* @return string[] Array of editable file extensions.
[243] Fix | Delete
*/
[244] Fix | Delete
function wp_get_theme_file_editable_extensions( $theme ) {
[245] Fix | Delete
[246] Fix | Delete
$default_types = array(
[247] Fix | Delete
'bash',
[248] Fix | Delete
'conf',
[249] Fix | Delete
'css',
[250] Fix | Delete
'diff',
[251] Fix | Delete
'htm',
[252] Fix | Delete
'html',
[253] Fix | Delete
'http',
[254] Fix | Delete
'inc',
[255] Fix | Delete
'include',
[256] Fix | Delete
'js',
[257] Fix | Delete
'json',
[258] Fix | Delete
'jsx',
[259] Fix | Delete
'less',
[260] Fix | Delete
'md',
[261] Fix | Delete
'patch',
[262] Fix | Delete
'php',
[263] Fix | Delete
'php3',
[264] Fix | Delete
'php4',
[265] Fix | Delete
'php5',
[266] Fix | Delete
'php7',
[267] Fix | Delete
'phps',
[268] Fix | Delete
'phtml',
[269] Fix | Delete
'sass',
[270] Fix | Delete
'scss',
[271] Fix | Delete
'sh',
[272] Fix | Delete
'sql',
[273] Fix | Delete
'svg',
[274] Fix | Delete
'text',
[275] Fix | Delete
'txt',
[276] Fix | Delete
'xml',
[277] Fix | Delete
'yaml',
[278] Fix | Delete
'yml',
[279] Fix | Delete
);
[280] Fix | Delete
[281] Fix | Delete
/**
[282] Fix | Delete
* Filters the list of file types allowed for editing in the theme editor.
[283] Fix | Delete
*
[284] Fix | Delete
* @since 4.4.0
[285] Fix | Delete
*
[286] Fix | Delete
* @param string[] $default_types An array of editable theme file extensions.
[287] Fix | Delete
* @param WP_Theme $theme The current theme object.
[288] Fix | Delete
*/
[289] Fix | Delete
$file_types = apply_filters( 'wp_theme_editor_filetypes', $default_types, $theme );
[290] Fix | Delete
[291] Fix | Delete
// Ensure that default types are still there.
[292] Fix | Delete
return array_unique( array_merge( $file_types, $default_types ) );
[293] Fix | Delete
}
[294] Fix | Delete
[295] Fix | Delete
/**
[296] Fix | Delete
* Prints file editor templates (for plugins and themes).
[297] Fix | Delete
*
[298] Fix | Delete
* @since 4.9.0
[299] Fix | Delete
*/
[300] Fix | Delete
function wp_print_file_editor_templates() {
[301] Fix | Delete
?>
[302] Fix | Delete
<script type="text/html" id="tmpl-wp-file-editor-notice">
[303] Fix | Delete
<div class="notice inline notice-{{ data.type || 'info' }} {{ data.alt ? 'notice-alt' : '' }} {{ data.dismissible ? 'is-dismissible' : '' }} {{ data.classes || '' }}">
[304] Fix | Delete
<# if ( 'php_error' === data.code ) { #>
[305] Fix | Delete
<p>
[306] Fix | Delete
<?php
[307] Fix | Delete
printf(
[308] Fix | Delete
/* translators: 1: Line number, 2: File path. */
[309] Fix | Delete
__( '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.' ),
[310] Fix | Delete
'{{ data.line }}',
[311] Fix | Delete
'{{ data.file }}'
[312] Fix | Delete
);
[313] Fix | Delete
?>
[314] Fix | Delete
</p>
[315] Fix | Delete
<pre>{{ data.message }}</pre>
[316] Fix | Delete
<# } else if ( 'file_not_writable' === data.code ) { #>
[317] Fix | Delete
<p>
[318] Fix | Delete
<?php
[319] Fix | Delete
printf(
[320] Fix | Delete
/* translators: %s: Documentation URL. */
[321] Fix | Delete
__( 'You need to make this file writable before you can save your changes. See <a href="%s">Changing File Permissions</a> for more information.' ),
[322] Fix | Delete
__( 'https://wordpress.org/support/article/changing-file-permissions/' )
[323] Fix | Delete
);
[324] Fix | Delete
?>
[325] Fix | Delete
</p>
[326] Fix | Delete
<# } else { #>
[327] Fix | Delete
<p>{{ data.message || data.code }}</p>
[328] Fix | Delete
[329] Fix | Delete
<# if ( 'lint_errors' === data.code ) { #>
[330] Fix | Delete
<p>
[331] Fix | Delete
<# var elementId = 'el-' + String( Math.random() ); #>
[332] Fix | Delete
<input id="{{ elementId }}" type="checkbox">
[333] Fix | Delete
<label for="{{ elementId }}"><?php _e( 'Update anyway, even though it might break your site?' ); ?></label>
[334] Fix | Delete
</p>
[335] Fix | Delete
<# } #>
[336] Fix | Delete
<# } #>
[337] Fix | Delete
<# if ( data.dismissible ) { #>
[338] Fix | Delete
<button type="button" class="notice-dismiss"><span class="screen-reader-text"><?php _e( 'Dismiss' ); ?></span></button>
[339] Fix | Delete
<# } #>
[340] Fix | Delete
</div>
[341] Fix | Delete
</script>
[342] Fix | Delete
<?php
[343] Fix | Delete
}
[344] Fix | Delete
[345] Fix | Delete
/**
[346] Fix | Delete
* Attempts to edit a file for a theme or plugin.
[347] Fix | Delete
*
[348] Fix | Delete
* When editing a PHP file, loopback requests will be made to the admin and the homepage
[349] Fix | Delete
* to attempt to see if there is a fatal error introduced. If so, the PHP change will be
[350] Fix | Delete
* reverted.
[351] Fix | Delete
*
[352] Fix | Delete
* @since 4.9.0
[353] Fix | Delete
*
[354] Fix | Delete
* @param string[] $args {
[355] Fix | Delete
* Args. Note that all of the arg values are already unslashed. They are, however,
[356] Fix | Delete
* coming straight from `$_POST` and are not validated or sanitized in any way.
[357] Fix | Delete
*
[358] Fix | Delete
* @type string $file Relative path to file.
[359] Fix | Delete
* @type string $plugin Path to the plugin file relative to the plugins directory.
[360] Fix | Delete
* @type string $theme Theme being edited.
[361] Fix | Delete
* @type string $newcontent New content for the file.
[362] Fix | Delete
* @type string $nonce Nonce.
[363] Fix | Delete
* }
[364] Fix | Delete
* @return true|WP_Error True on success or `WP_Error` on failure.
[365] Fix | Delete
*/
[366] Fix | Delete
function wp_edit_theme_plugin_file( $args ) {
[367] Fix | Delete
if ( empty( $args['file'] ) ) {
[368] Fix | Delete
return new WP_Error( 'missing_file' );
[369] Fix | Delete
}
[370] Fix | Delete
$file = $args['file'];
[371] Fix | Delete
if ( 0 !== validate_file( $file ) ) {
[372] Fix | Delete
return new WP_Error( 'bad_file' );
[373] Fix | Delete
}
[374] Fix | Delete
[375] Fix | Delete
if ( ! isset( $args['newcontent'] ) ) {
[376] Fix | Delete
return new WP_Error( 'missing_content' );
[377] Fix | Delete
}
[378] Fix | Delete
$content = $args['newcontent'];
[379] Fix | Delete
[380] Fix | Delete
if ( ! isset( $args['nonce'] ) ) {
[381] Fix | Delete
return new WP_Error( 'missing_nonce' );
[382] Fix | Delete
}
[383] Fix | Delete
[384] Fix | Delete
$plugin = null;
[385] Fix | Delete
$theme = null;
[386] Fix | Delete
$real_file = null;
[387] Fix | Delete
if ( ! empty( $args['plugin'] ) ) {
[388] Fix | Delete
$plugin = $args['plugin'];
[389] Fix | Delete
[390] Fix | Delete
if ( ! current_user_can( 'edit_plugins' ) ) {
[391] Fix | Delete
return new WP_Error( 'unauthorized', __( 'Sorry, you are not allowed to edit plugins for this site.' ) );
[392] Fix | Delete
}
[393] Fix | Delete
[394] Fix | Delete
if ( ! wp_verify_nonce( $args['nonce'], 'edit-plugin_' . $file ) ) {
[395] Fix | Delete
return new WP_Error( 'nonce_failure' );
[396] Fix | Delete
}
[397] Fix | Delete
[398] Fix | Delete
if ( ! array_key_exists( $plugin, get_plugins() ) ) {
[399] Fix | Delete
return new WP_Error( 'invalid_plugin' );
[400] Fix | Delete
}
[401] Fix | Delete
[402] Fix | Delete
if ( 0 !== validate_file( $file, get_plugin_files( $plugin ) ) ) {
[403] Fix | Delete
return new WP_Error( 'bad_plugin_file_path', __( 'Sorry, that file cannot be edited.' ) );
[404] Fix | Delete
}
[405] Fix | Delete
[406] Fix | Delete
$editable_extensions = wp_get_plugin_file_editable_extensions( $plugin );
[407] Fix | Delete
[408] Fix | Delete
$real_file = WP_PLUGIN_DIR . '/' . $file;
[409] Fix | Delete
[410] Fix | Delete
$is_active = in_array(
[411] Fix | Delete
$plugin,
[412] Fix | Delete
(array) get_option( 'active_plugins', array() ),
[413] Fix | Delete
true
[414] Fix | Delete
);
[415] Fix | Delete
[416] Fix | Delete
} elseif ( ! empty( $args['theme'] ) ) {
[417] Fix | Delete
$stylesheet = $args['theme'];
[418] Fix | Delete
if ( 0 !== validate_file( $stylesheet ) ) {
[419] Fix | Delete
return new WP_Error( 'bad_theme_path' );
[420] Fix | Delete
}
[421] Fix | Delete
[422] Fix | Delete
if ( ! current_user_can( 'edit_themes' ) ) {
[423] Fix | Delete
return new WP_Error( 'unauthorized', __( 'Sorry, you are not allowed to edit templates for this site.' ) );
[424] Fix | Delete
}
[425] Fix | Delete
[426] Fix | Delete
$theme = wp_get_theme( $stylesheet );
[427] Fix | Delete
if ( ! $theme->exists() ) {
[428] Fix | Delete
return new WP_Error( 'non_existent_theme', __( 'The requested theme does not exist.' ) );
[429] Fix | Delete
}
[430] Fix | Delete
[431] Fix | Delete
if ( ! wp_verify_nonce( $args['nonce'], 'edit-theme_' . $stylesheet . '_' . $file ) ) {
[432] Fix | Delete
return new WP_Error( 'nonce_failure' );
[433] Fix | Delete
}
[434] Fix | Delete
[435] Fix | Delete
if ( $theme->errors() && 'theme_no_stylesheet' === $theme->errors()->get_error_code() ) {
[436] Fix | Delete
return new WP_Error(
[437] Fix | Delete
'theme_no_stylesheet',
[438] Fix | Delete
__( 'The requested theme does not exist.' ) . ' ' . $theme->errors()->get_error_message()
[439] Fix | Delete
);
[440] Fix | Delete
}
[441] Fix | Delete
[442] Fix | Delete
$editable_extensions = wp_get_theme_file_editable_extensions( $theme );
[443] Fix | Delete
[444] Fix | Delete
$allowed_files = array();
[445] Fix | Delete
foreach ( $editable_extensions as $type ) {
[446] Fix | Delete
switch ( $type ) {
[447] Fix | Delete
case 'php':
[448] Fix | Delete
$allowed_files = array_merge( $allowed_files, $theme->get_files( 'php', -1 ) );
[449] Fix | Delete
break;
[450] Fix | Delete
case 'css':
[451] Fix | Delete
$style_files = $theme->get_files( 'css', -1 );
[452] Fix | Delete
$allowed_files['style.css'] = $style_files['style.css'];
[453] Fix | Delete
$allowed_files = array_merge( $allowed_files, $style_files );
[454] Fix | Delete
break;
[455] Fix | Delete
default:
[456] Fix | Delete
$allowed_files = array_merge( $allowed_files, $theme->get_files( $type, -1 ) );
[457] Fix | Delete
break;
[458] Fix | Delete
}
[459] Fix | Delete
}
[460] Fix | Delete
[461] Fix | Delete
// Compare based on relative paths.
[462] Fix | Delete
if ( 0 !== validate_file( $file, array_keys( $allowed_files ) ) ) {
[463] Fix | Delete
return new WP_Error( 'disallowed_theme_file', __( 'Sorry, that file cannot be edited.' ) );
[464] Fix | Delete
}
[465] Fix | Delete
[466] Fix | Delete
$real_file = $theme->get_stylesheet_directory() . '/' . $file;
[467] Fix | Delete
[468] Fix | Delete
$is_active = ( get_stylesheet() === $stylesheet || get_template() === $stylesheet );
[469] Fix | Delete
[470] Fix | Delete
} else {
[471] Fix | Delete
return new WP_Error( 'missing_theme_or_plugin' );
[472] Fix | Delete
}
[473] Fix | Delete
[474] Fix | Delete
// Ensure file is real.
[475] Fix | Delete
if ( ! is_file( $real_file ) ) {
[476] Fix | Delete
return new WP_Error( 'file_does_not_exist', __( 'File does not exist! Please double check the name and try again.' ) );
[477] Fix | Delete
}
[478] Fix | Delete
[479] Fix | Delete
// Ensure file extension is allowed.
[480] Fix | Delete
$extension = null;
[481] Fix | Delete
if ( preg_match( '/\.([^.]+)$/', $real_file, $matches ) ) {
[482] Fix | Delete
$extension = strtolower( $matches[1] );
[483] Fix | Delete
if ( ! in_array( $extension, $editable_extensions, true ) ) {
[484] Fix | Delete
return new WP_Error( 'illegal_file_type', __( 'Files of this type are not editable.' ) );
[485] Fix | Delete
}
[486] Fix | Delete
}
[487] Fix | Delete
[488] Fix | Delete
$previous_content = file_get_contents( $real_file );
[489] Fix | Delete
[490] Fix | Delete
if ( ! is_writable( $real_file ) ) {
[491] Fix | Delete
return new WP_Error( 'file_not_writable' );
[492] Fix | Delete
}
[493] Fix | Delete
[494] Fix | Delete
$f = fopen( $real_file, 'w+' );
[495] Fix | Delete
if ( false === $f ) {
[496] Fix | Delete
return new WP_Error( 'file_not_writable' );
[497] Fix | Delete
}
[498] Fix | Delete
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function