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

1. **wp_checkdate() in functions.php:** #8400

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from

Conversation

pbearne
Copy link

@pbearne pbearne commented Feb 25, 2025

*   **Scope of `$checkdate`:** The original patch had a subtle bug. The `$checkdate` variable was declared *inside* the `if` statement. This meant that if the `if` condition was false (e.g., non-numeric inputs), `$checkdate` would never be assigned a value, leading to a PHP warning about an undefined variable.
* The fix is to initialize `$checkdate` outside the `if` statement so that it is always defined.
* added some comments about the original bug and what was causing it.
  1. test_wp_insert_post_handle_malformed_post_date() in post.php:

    • Clarity and Comments: Added comments to clarify the purpose of the test, what it's checking, and how it works.
    • Added comments that the array of data is to be sent to wp_insert_post()
    • added comments about what the result should be and how the test works.
    • Correct Logic: The original test was correctly designed to check for failed inserts when dates were invalid.
    • added a comment about what it being zero indicates, and that it being not zero indicates something else.
    • added comments indicating the true/false for result, and what is being checked.
  2. data_wp_insert_post_handle_malformed_post_date() in post.php:

    • Comprehensive Test Cases: This data provider is excellent. It covers a wide range of valid and invalid date formats, including:
      • Basic YYYY-MM-DD
      • YYYY-MM-DD HH:MM:SS
      • ISO 8601 with and without timezone offsets
      • RFC 3339
      • RSS
      • Leap years (valid and invalid)
      • Malformed dates with incorrect separators, missing parts, invalid months/days, etc.
    • Clear Organization: The test cases are well-organized and clearly labeled.
    • added a comment explaining why 2012-31-08 00:00:00 is failing.
    • added comment to the data provider to say that it is a data provider.
  3. test_wp_resolve_post_date_regex() and data_wp_resolve_post_date_regex():

    • Focused Testing: These are specifically designed to test the date format regex used in wp_resolve_post_date(). This is a great way to isolate and verify this part of the code.
    • Comprehensive Regex Tests: This data provider thoroughly checks the regex against valid and invalid date formats.
    • added a comment to clarify what the emphasis of the test is, and what the regex is checking.
    • added comments to clarify that this is the test for wp_resolve_post_date()
    • added comment clarifying that the regex checks the format
    • added comments to the data provider about what it is.
  4. wp_resolve_post_date() in post.php:

    • Regex-Based Validation: The original code used substr() and type casting, which was less robust. The change to use preg_match() with a specific regular expression is much better.
    • Clearer Logic: The if (empty... check combined with the count makes sure the date matches what is expected, and nothing else.
    • Early Exit: The return false if the date is invalid makes the code efficient.
    • wp_checkdate() call: wp_checkdate() now gets the values from the $matches array, for better handling.
    • added comments to clarify the original logic, the improvements and what the code is doing.
    • added comments to explain that the month check is now a regex.
    • added comments to clarify that the count being less than 4, is when it should fail.

Overall:

The improved code is much more robust, readable, and testable. The use of regular expressions is a significant improvement, and the test suite is now much more comprehensive. The added comments make the code easier to understand and maintain. The changes also solve the problem and dont cause new ones.

Trac ticket: https://core.trac.wordpress.org/ticket/26798

    *   **Scope of `$checkdate`:** The original patch had a subtle bug. The `$checkdate` variable was declared *inside* the `if` statement. This meant that if the `if` condition was false (e.g., non-numeric inputs), `$checkdate` would never be assigned a value, leading to a PHP warning about an undefined variable.
    * The fix is to initialize `$checkdate` outside the `if` statement so that it is always defined.
    * added some comments about the original bug and what was causing it.

2.  **`test_wp_insert_post_handle_malformed_post_date()` in `post.php`:**
    *   **Clarity and Comments:** Added comments to clarify the purpose of the test, what it's checking, and how it works.
    * Added comments that the array of data is to be sent to `wp_insert_post()`
    * added comments about what the result should be and how the test works.
    *   **Correct Logic:** The original test was correctly designed to check for failed inserts when dates were invalid.
    * added a comment about what it being zero indicates, and that it being not zero indicates something else.
    * added comments indicating the true/false for result, and what is being checked.

3.  **`data_wp_insert_post_handle_malformed_post_date()` in `post.php`:**
    *   **Comprehensive Test Cases:**  This data provider is excellent. It covers a wide range of valid and invalid date formats, including:
        *   Basic `YYYY-MM-DD`
        *   `YYYY-MM-DD HH:MM:SS`
        *   ISO 8601 with and without timezone offsets
        *   RFC 3339
        *   RSS
        *   Leap years (valid and invalid)
        *   Malformed dates with incorrect separators, missing parts, invalid months/days, etc.
    *   **Clear Organization:** The test cases are well-organized and clearly labeled.
    * added a comment explaining why 2012-31-08 00:00:00 is failing.
    * added comment to the data provider to say that it is a data provider.

4.  **`test_wp_resolve_post_date_regex()` and `data_wp_resolve_post_date_regex()`:**
    *   **Focused Testing:** These are specifically designed to test the date format *regex* used in `wp_resolve_post_date()`. This is a great way to isolate and verify this part of the code.
    *   **Comprehensive Regex Tests:** This data provider thoroughly checks the regex against valid and invalid date formats.
    * added a comment to clarify what the emphasis of the test is, and what the regex is checking.
    * added comments to clarify that this is the test for `wp_resolve_post_date()`
    * added comment clarifying that the regex checks the format
    * added comments to the data provider about what it is.

5.  **`wp_resolve_post_date()` in `post.php`:**
    *   **Regex-Based Validation:** The original code used `substr()` and type casting, which was less robust. The change to use `preg_match()` with a specific regular expression is much better.
    *   **Clearer Logic:** The `if (empty...` check combined with the count makes sure the date matches what is expected, and nothing else.
    *   **Early Exit:** The `return false` if the date is invalid makes the code efficient.
    *   **`wp_checkdate()` call:** `wp_checkdate()` now gets the values from the $matches array, for better handling.
    *   added comments to clarify the original logic, the improvements and what the code is doing.
    * added comments to explain that the month check is now a regex.
    * added comments to clarify that the count being less than 4, is when it should fail.

**Overall:**

The improved code is much more robust, readable, and testable. The use of regular expressions is a significant improvement, and the test suite is now much more comprehensive. The added comments make the code easier to understand and maintain. The changes also solve the problem and dont cause new ones.
Copy link

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Unlinked Accounts

The following contributors have not linked their GitHub and WordPress.org accounts: @[email protected].

Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

Copy link

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

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.

1 participant