Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Prefix font-family -face post slugs with wp-font... to avoid reserving nice names. #59137

Open
peterwilsoncc opened this issue Feb 17, 2024 · 0 comments
Labels
[Feature] Font Library [Type] Bug An existing feature does not function as intended

Comments

@peterwilsoncc
Copy link
Contributor

Description

As plugins can modify the rewrites for custom post types & customize the rules for what constitutes a duplicate slug via the wp_unique_post_slug filter, it would be good to prefix font family and font face post slugs with wp-font-fam- and wp-font-face- respectively.

This is usually used in conjunction with custom rewrite rules for custom post types.

To be certain that WordPress will not reserve "nice names" in these circumstances, it would be good to prefix the font face and family post slugs with wp-font-... prefixes.

Step-by-step reproduction instructions

  1. Start a blog about web fonts (ie, set up a local environment).
  2. Set the permalink structure away from plain permalinks (if not already done)
  3. In the site editor add Open Sans as a font family and font face.
  4. Apply it to an element, publish the site editor changes.
  5. Add the plugin below to mu-plugins.
  6. Write a post titled "Open Sans".
  7. Publish the post, observe the slug created is open-sans-2.
  8. Remove the plugin from mu-plugins to avoid problems later.

Screenshots, screen recording, code snippet

Test plugin.

<?php
/**
 * Plugin Name: Testing unique post slugs.
 */

namespace PWCC\TestingSlugs;

add_filter( 'wp_unique_post_slug', __NAMESPACE__ . '\unique_post_slug', 10, 2 );

/**
 * Ensure the post slug is unique accross all post types.
 *
 * This is a very blunt method do demonstrate the issue with unprefixed post
 * slugs. A plugin customizing rewrites should use something more sophisticated
 * but WordPress should prefix font slugs similar to global styles
 *
 * @param string $slug    The post slug.
 * @param int    $post_id The post ID being edited
 * @return string The unique post slug.
 */
function unique_post_slug( $slug, $post_id ) {
	global $wpdb;
	$check_sql       = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND ID != %d LIMIT 1";
	$post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_id ) );

	$suffix = 2;
	do {
		$alt_post_name   = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix";
		$post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_id ) );
		++$suffix;
	} while ( $post_name_check );

	return $alt_post_name;
}

Environment info

  • WordPress 6.4
  • Gutenberg trunk @ 96cb434

Please confirm that you have searched existing issues in the repo.

Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

Yes

@peterwilsoncc peterwilsoncc added [Type] Bug An existing feature does not function as intended [Feature] Font Library labels Feb 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Font Library [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

No branches or pull requests

1 participant