Permalinks: Validate custom permalink structures.

Custom permalink structures require at least one valid structure tag, e.g. `%postname%`. If none is included, it would leave users with broken permalinks.
Let's make sure this won't happen by validating the permalink structure.

Adds unit tests.

Props rockwell15 for initial patch.
Fixes #35936.
Built from https://develop.svn.wordpress.org/trunk@37747


git-svn-id: http://core.svn.wordpress.org/trunk@37712 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Pascal Birchler 2016-06-19 12:02:29 +00:00
parent 62fdd6bd1a
commit a97f0a2c19
3 changed files with 31 additions and 32 deletions

View File

@ -82,6 +82,9 @@ if ( isset($_POST['permalink_structure']) || isset($_POST['category_base']) ) {
else
$permalink_structure = $blog_prefix . $permalink_structure;
}
$permalink_structure = sanitize_option( 'permalink_structure', $permalink_structure );
$wp_rewrite->set_permalink_structure( $permalink_structure );
}
@ -99,6 +102,24 @@ if ( isset($_POST['permalink_structure']) || isset($_POST['category_base']) ) {
$wp_rewrite->set_tag_base( $tag_base );
}
$message = __( 'Permalink structure updated.' );
if ( $iis7_permalinks ) {
if ( $permalink_structure && ! $usingpi && ! $writable ) {
$message = __( 'You should update your web.config now.' );
} elseif ( $permalink_structure && ! $usingpi && $writable ) {
$message = __( 'Permalink structure updated. Remove write access on web.config file now!' );
}
} elseif ( ! $is_nginx && $permalink_structure && ! $usingpi && ! $writable && $update_required ) {
$message = __( 'You should update your .htaccess now.' );
}
if ( ! get_settings_errors() ) {
add_settings_error( 'general', 'settings_updated', $message, 'updated' );
}
set_transient( 'settings_errors', get_settings_errors(), 30 );
wp_redirect( admin_url( 'options-permalink.php?settings-updated=true' ) );
exit;
}
@ -125,42 +146,12 @@ if ( $iis7_permalinks ) {
}
}
if ( $wp_rewrite->using_index_permalinks() )
$usingpi = true;
else
$usingpi = false;
$usingpi = $wp_rewrite->using_index_permalinks();
flush_rewrite_rules();
require( ABSPATH . 'wp-admin/admin-header.php' );
if ( ! empty( $_GET['settings-updated'] ) ) : ?>
<div id="message" class="updated notice is-dismissible"><p><?php
if ( ! is_multisite() ) {
if ( $iis7_permalinks ) {
if ( $permalink_structure && ! $usingpi && ! $writable ) {
_e('You should update your web.config now.');
} elseif ( $permalink_structure && ! $usingpi && $writable ) {
_e('Permalink structure updated. Remove write access on web.config file now!');
} else {
_e('Permalink structure updated.');
}
} elseif ( $is_nginx ) {
_e('Permalink structure updated.');
} else {
if ( $permalink_structure && ! $usingpi && ! $writable && $update_required ) {
_e('You should update your .htaccess now.');
} else {
_e('Permalink structure updated.');
}
}
} else {
_e('Permalink structure updated.');
}
?>
</p></div>
<?php endif; ?>
<div class="wrap">
<h1><?php echo esc_html( $title ); ?></h1>

View File

@ -4204,6 +4204,14 @@ function sanitize_option( $option, $value ) {
$value = esc_url_raw( $value );
$value = str_replace( 'http://', '', $value );
}
if ( 'permalink_structure' === $option && '' !== $value && ! preg_match( '/%[^\/%]+%/', $value ) ) {
$error = sprintf(
/* translators: %s: Codex URL */
__( 'A structure tag is required when using custom permalinks. <a href="%s">Learn more</a>' ),
__( 'https://codex.wordpress.org/Using_Permalinks#Choosing_your_permalink_structure' )
);
}
break;
case 'default_role' :

View File

@ -4,7 +4,7 @@
*
* @global string $wp_version
*/
$wp_version = '4.6-alpha-37746';
$wp_version = '4.6-alpha-37747';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.