Skip to content

New customisable rewrite bases for post types & taxonomies#812

Merged
mauteri merged 21 commits intoGatherPress:mainfrom
carstingaxion:fix/722
Sep 3, 2024
Merged

New customisable rewrite bases for post types & taxonomies#812
mauteri merged 21 commits intoGatherPress:mainfrom
carstingaxion:fix/722

Conversation

@carstingaxion
Copy link
Copy Markdown
Collaborator

@carstingaxion carstingaxion commented Aug 25, 2024

Description of the Change

This PR introduces a new settings section under GatherPress’ general settings to customise the rewrite slug for public post types and taxonomies.

If nothing is set, a localised string will be used, that is sanitised for use as part of an URL.

Screenshot with localised German defaults.
Screenshot with localised German defaults.

Closes #722

How to test the Change

  1. Go to Events > Settings > General
  2. Change the rewrite slug for events
  3. Save settings
  4. Go to Events > All Events
  5. Click on the View quick edit link
  6. Confirm you opened your event under its new rewrite base, without any 404 errors.

Changelog Entry

Added - New customisable rewrite bases for post types & taxonomies

Credits

Props @carstingaxion

Checklist:

  • I agree to follow this project's Code of Conduct.
  • I have updated the documentation accordingly.
  • I have added tests to cover my change.
  • All new and existing tests pass.

@carstingaxion

This comment was marked as outdated.

@carstingaxion

This comment was marked as outdated.

@carstingaxion
Copy link
Copy Markdown
Collaborator Author

carstingaxion commented Aug 26, 2024

@mauteri Can I define a custom callback via the GatherPress settings API, that gets called whenever a particular setting gets updated?

I didn't found anything like this in the existing codebase, so I just used https://developer.wordpress.org/reference/hooks/update_option_option/.

@carstingaxion carstingaxion changed the title Fix 722 New customisable rewrite bases for post types & taxonomies Aug 26, 2024
@carstingaxion carstingaxion marked this pull request as ready for review August 26, 2024 13:03
@carstingaxion carstingaxion requested a review from mauteri August 26, 2024 13:03
@carstingaxion
Copy link
Copy Markdown
Collaborator Author

I would like to introduce a new and breaking change here:

Please let us consider plurals as the post type bases, not the singulars like it is!
It would make so much more sense and the URLs more human readable.

@mauteri
Copy link
Copy Markdown
Contributor

mauteri commented Aug 27, 2024

I would like to introduce a new and breaking change here:

Please let us consider plurals as the post type bases, not the singulars like it is!

It would make so much more sense and the URLs more human readable.

Isn't singular the norm? For example, category pages by default are domain.com/category/term.

@carstingaxion
Copy link
Copy Markdown
Collaborator Author

I ❤️ the new spell checker!

@carstingaxion carstingaxion requested a review from mauteri August 29, 2024 02:46
@mauteri
Copy link
Copy Markdown
Contributor

mauteri commented Sep 2, 2024

@carstingaxion I'm getting undefined for the website URL in preview on both my local when I checkout your branch and the Playground environment.

Screenshot 2024-09-02 at 7 35 25 PM

@carstingaxion
Copy link
Copy Markdown
Collaborator Author

Ah. Du you remember one of your last reviews?

I should change site_url() to home_url(). But I only did that in php.

🙄

I'll fix that.

@carstingaxion
Copy link
Copy Markdown
Collaborator Author

image


return (
<>
{window.GatherPress.urls.homeUrl + '/'}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, missed usage of this. There's a getFromGlobal helper you can use here. Has build in error handling and such.
getFromGlobal('urls.homeUrl')

Copy link
Copy Markdown
Collaborator Author

@carstingaxion carstingaxion Sep 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will be fixed.

btw. I asked chatgpt how to avoid that in the future

How to write an eslint rule that checks if there is any window.gatherpress inside the code and suggests to use foo instead?

To create a custom ESLint rule that checks for the presence of window.gatherpress in your code and suggests using foo instead, you can follow these steps:

  1. Set Up Your ESLint Environment: Make sure you have ESLint installed in your project. If you haven't done this yet, you can install it using npm:

    npm install eslint --save-dev
  2. Create a Custom Rule: You can create a custom ESLint rule by defining it in a JavaScript file. For example, create a file named no-gatherpress.js in a directory called eslint-rules.

  3. Implement the Rule: In the no-gatherpress.js file, you can implement the rule as follows:

    module.exports = {
        meta: {
            type: 'suggestion', // This rule is a suggestion
            docs: {
                description: 'disallow window.gatherpress and suggest using foo instead',
                category: 'Best Practices',
                recommended: false,
            },
            schema: [], // No options
        },
        create(context) {
            return {
                MemberExpression(node) {
                    // Check if the expression is window.gatherpress
                    if (
                        node.object.name === 'window' &&
                        node.property.name === 'gatherpress'
                    ) {
                        context.report({
                            node,
                            message: 'Use foo instead of window.gatherpress.',
                        });
                    }
                },
            };
        },
    };
  4. Configure ESLint to Use Your Custom Rule: In your ESLint configuration file (e.g., .eslintrc.js), you need to include your custom rule. Here’s an example of how to do that:

    module.exports = {
        // Your existing ESLint configuration
        rules: {
            'no-gatherpress': 'warn', // or 'error' if you want it to be an error
        },
        overrides: [
            {
                files: ['*.js'], // Specify the files where the rule should apply
                rules: {
                    'no-gatherpress': require('./eslint-rules/no-gatherpress'),
                },
            },
        ],
    };
  5. Run ESLint: Now you can run ESLint on your codebase, and it will warn you whenever it finds window.gatherpress, suggesting to use foo instead.

    npx eslint your-file.js

Summary

This custom ESLint rule checks for the usage of window.gatherpress and suggests using foo instead. You can adjust the severity of the rule (e.g., warn or error) based on your preference. Make sure to test the rule in your codebase to ensure it works as expected.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Sep 3, 2024

Preview changes with Playground

You can preview the least recent changes for PR#812 by following one of the links below:

⚠️ Note: The preview sites are created using WordPress Playground. You can add content, edit settings, and test the themes as you would on a real site, but please note that changes are not saved between sessions.

@mauteri
Copy link
Copy Markdown
Contributor

mauteri commented Sep 3, 2024

Looks good, thanks! Merging!

@mauteri mauteri merged commit a98a618 into GatherPress:main Sep 3, 2024
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

Successfully merging this pull request may close these issues.

Issue with the translation of the "event" part of the slug

2 participants