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

Add support for anchor links #1044

Merged
merged 40 commits into from
Jan 23, 2024
Merged

Add support for anchor links #1044

merged 40 commits into from
Jan 23, 2024

Conversation

attackant
Copy link
Member

@attackant attackant commented Jan 16, 2024

Fixes #1039

This PR does a few things:

  • Adds ability to add anchor links to HTML elements in body and text components
  • Handles validation and deduplication of IDs
  • Fixes issue where non static method was called statically ( new BC_Setup() )->action_init();
  • Fixes array to string conversion error
  • Extracts HTML cleanup into multiple methods
  • Updates unit tests
  • Fixes an edge case where Apple_News:is_initialized() never returns true
  • Incremental code cleanup and modernization
  • Adding some type checking to private methods

Introduced more strict type declarations in the parser class. This includes making the type of 'format' string explicitly and also ensuring all methods have explicit input and output types. Also, improved some code efficiency by replacing some strpos checks with str_starts_with and str_contains, and handling non-breaking spaces in a more efficient way by using an array in str_ireplace.
…unctionality

The clean_html function of the class-parser.php file has been refactored to improve readability and functionality. New private methods have been introduced to handle specific tasks such as removing empty tags, handling anchor links, root-relative URLs validation, and the conversion of non-breaking spaces to regular spaces. The upgrade has improved the code's efficiency, readability, and maintainability.
Several enhancements are introduced to the Class_Body of the Apple Exporter. The changes include addition of namespaces for Theme and DOMElement, explicit definition of return types, and improvement in code readability.
…initialized returns false after saving settings.

The codebase for 'class-apple-news.php' has been significantly refactored to utilize explicit typing for better code readability and maintainability. More specific type hinting is introduced for function parameters and return types, contributing to stricter code standards. A bug, causing the function 'is_initialized' to return false even after saving settings, has also been fixed in this commit.
Two formatting changes have been made in class-apple-news.php file. Indentation of a comment block was corrected for the $with_padding parameter. Additionally, the statement initializing the $settings variable was reformatted for improved readability.
…x fatal

The $is_initialized static variable in class-apple-news.php is now explicitly initialized to false. The logic for checking if the necessary plugin settings are initialized has also been refactored to improve readability and simplify the boolean expression.
Revised error handling in the Apple News 'push' action and corresponding unit test. The code now uses a regular expression for error detection in tests and 'str_contains' instead of 'preg_match' in the actual application.
Anchor link handling in the HTML parser has been removed to simplify the code and make it cleaner. Anchor links, which were previously replaced with the global post permalink, will now fall under the regular expression used for managing different link types in the HTML.
Anchor tag attributes have been updated from 'name' to 'id' within the test-class-parser test. Corresponding anchor link references have been updated to directly link to the 'id' attribute.
Improved the class-component.php script by correcting namespaces, refining variable initialization, and adjusting type hinting in methods. Added and updated comments to enhance understanding and readability of the code.
Anchor links are now permitted without auto-conversion. Additionally, the URL detection has been optimized to use the PHP built-in "str_starts_with" function for better performance.
This commit refactors several parts of the codebase for improved readability, performance and adherence to coding standards. Changes include the use of PHP 8's `str_contains()` and `str_starts_with()`, addressing typos in comments, plus removing unnecessary imports. A few adjustments were made to comments and annotations for clearer understanding.
@attackant attackant self-assigned this Jan 16, 2024
@attackant attackant added this to the v2.4.4 milestone Jan 16, 2024
@attackant attackant linked an issue Jan 16, 2024 that may be closed by this pull request
This commit carries out a number of improvements to enhance both the readability and functionality. Firstly, it fixes a comment punctuation issue in 'class-apple-news.php'. Secondly, it substitutes 'json_encode()' with 'wp_json_encode()' in 'class-request.php' to ensure the proper encoding of data in WordPress. Finally, it cleans up the formatting in 'class-export.php', removing unnecessary backslashes and adding parentheses for instantiation of 'BC_Setup' class.
This commit adds punctuation to the existing comment in the 'class-body.php' file for clearer readability. It also improves the overall code clarity and standards conformance according to PHP CodeSniffer's rules.
This commit introduces an update to the allowed_html array in class-html.php, where an 'id' attribute is now allowed for a variety of HTML tags.
Removed the type hinting from the constructor and parse methods in class-parser.php to allow more flexibility in input types. The HTML formatting is now applied directly within the 'apple_news_parse_html' filter instead of being processed separately, and excess whitespace is removed from the return value of the 'convert_spaces' method.
Updated the comments of a check on the admin email to reflect that the issue is inconsistency in return value rather than missing return value. This change will provide more accurate information within the comment for future modifications.
Adjusted the attributes of the HTML tags allowed in 'class-component.php'. Along with changing 'href' to boolean for 'a' tag, 'id' attributes have now been included for most tags in the 'allowed_html' array. This modification will enable more precise element targeting and manipulation.
Introduced a method to convert HTML 'id' attributes into unique identifiers in the text of a component to support internal anchor links. This includes properly handling potential duplicates and identifiers starting with digits, which are not allowed in Apple News.
Removed the strict typing for input parameters in `filter_update_post_metadata`, `all_keys_exist`, and `clone_settings` methods. This change includes addition of internal checks for data types within methods to handle possible variations. The new implementation offers more flexibility for input types and includes better error handling for cases when non-array inputs are provided.
Altered how duplicate identifiers are managed in 'class-components.php'. Instead of concatenating a count to duplicate identifiers, the updated code now skips any that already exist. Also, added a TODO comment in 'class-heading.php' to indicate a planned enhancement: adding ID identifiers to anchor links within headings, improving navigation within Apple News structures.
Updated the function in 'class-components.php' to remove any duplicate identifiers, improving text flow at all orientations. This change replaces the old method which added an increasing count to duplicate identifiers. In addition, a TODO comment has been added to 'class-heading.php' outlining a future enhancement for adding ID identifiers to anchor links within headings, further enhancing navigation in Apple News structures.
Copy link

@mogmarsh mogmarsh left a comment

Choose a reason for hiding this comment

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

This looks really good. I made a bunch of comments, but for the most part, they're all suggestions. Feel free to disagree with me and ignore them. 👍

includes/apple-exporter/class-parser.php Outdated Show resolved Hide resolved
includes/apple-exporter/class-parser.php Outdated Show resolved Hide resolved
includes/apple-exporter/components/class-body.php Outdated Show resolved Hide resolved
includes/apple-exporter/components/class-component.php Outdated Show resolved Hide resolved
includes/apple-exporter/components/class-component.php Outdated Show resolved Hide resolved
includes/apple-exporter/components/class-component.php Outdated Show resolved Hide resolved
includes/class-apple-news.php Outdated Show resolved Hide resolved
Removed counter for duplicate identifiers in 'class-components.php'. The function was adjusted to directly add identifiers into our dictionary without counting, which enhances the text flow. In the future, 'class-heading.php' may include ID identifiers to further improve navigation.
…root_relative_urls to handle both single and double quoted urls
The commit replaces the string comparison against an empty string checks with the PHP empty() function in class-component.php. This change is expected to make the check more reliable, as it will now also return true for uninitialized variables, null values, and a few other edge cases that weren't handled by the string comparison.
@attackant
Copy link
Member Author

Thanks @mogmarsh I went ahead with all of your suggestions. Will merge this after I make another round of update for the Heading component. 🥳

@attackant attackant mentioned this pull request Jan 17, 2024
The changes encompass various refactoring actions for apple exporter header class. These include implementing proper namespace usage, correctly declaring array data types, specifying return types as well as enhancing PHPDoc comments for methods for better code clarity and readability. Additionally, the future plan to remove duplicate methods across child classes is marked with a TODO comment.
In the `test-class-heading.php` and `test-class-parser.php` tests, an ID has been added to the heading in the HTML content. Several test functions have been updated to explicitly specify 'void' as the return type. Apart from these, a small tweak was made to streamline the HTML parsing verification logic.
Updated the class-heading component to include 'identifier' key for heading IDs. Adjusted regular expression in the build function to capture heading ID value, if set, from HTML input. This addition enhances navigation and provides anchor links to headings.
@attackant attackant merged commit cc80a85 into release/v2.4.4 Jan 23, 2024
8 checks passed
@attackant attackant deleted the issue-1039 branch January 23, 2024 00:02
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.

Add Support for Anchor Links
2 participants