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

strtolower on slug #32

Open
chrisspiegl opened this issue Aug 24, 2012 · 1 comment
Open

strtolower on slug #32

chrisspiegl opened this issue Aug 24, 2012 · 1 comment

Comments

@chrisspiegl
Copy link

I just found out that sometimes the slog does not get set in lower case. How I found it:

  1. Create a post with not some letters not in lower case
  2. Published that post
  3. I have a hook that shortenes the url based on the $post['post-absolute-permalink'] variable
  4. The shortened URL pointed to the 'not all lowercase' post name / the site itself (the frontpage) shows the all lower case link (which works).

Hope this is accurate enough. I don't know where to search for the error. Maybe someone knows. Looking forward to a fix.

@pocketWashburn
Copy link
Contributor

I know this one is 9 months old, so if you've already found a fix my apologies. I've been seeing the same problem, and I believe I'm narrowing in on the issue.

The quick solution is to have your slug (file name) be exclusively lower case.

The longer answer has absorbed a number of hours this evening/morning now, but I believe is traced to the fact that the slug only gets passed through strtolower in Post::expected_source_filename(). I believe that this function doesn't get run on new posts before the hook is called, thus the $slug referenced by the hook isn't in lower case, however as part of the Updater process the function does get called leading to the links on the home page being correct but those available to hooks being incorrect.

There are several different possible solutions:

  1. just save your files in all lower case, causing your slug to be in all lower case and this issue to never occur.

  2. modify the Post::array_for_template() function to make $slug lowercase:

public function array_for_template()
    {
        $this->slug = strtolower($this->slug);
        ...
    }

This seems 'hacky' and like its not solving the real issue...

  1. modify Post::__construct so that $this->slug is set to lowercase at that time, making it no longer necessary to set it to lower case in Post::expected_source_filename():
public function __construct($source_filename, $is_draft = -1 /* auto */)
    {
        ...
        if (is_numeric($filename_datestr)) {
            ...
            $this->slug = strtolower(ltrim(substr($filename, 11, -(strlen(Updater::$post_extension))), '-'));
        } else {
            ...
            $this->slug = strtolower(substring_before($filename, '.'));
        }
    }

This seems less 'hacky' to me, but I'm not clear if there was a reason for waiting to make the slug lowercase until Post::expected_source_filename()

  1. Figure out why its only an issue for the array_for_template() call thats part of the hook process, and not for the normal post updating/creating that gets done for the rest of the blog.

I'm leaning towards option number 3, and after I get some sleep I'll test it further to see if it breaks anything, and possibly submit a pull request with the change to see what Marco thinks (assuming he has time/interest).

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

No branches or pull requests

2 participants