Each template in the theme can have optional meta HTML comment, that is used to configure a category, that is created to match that template in the "Structure & Data" section. The typical comment looks like this:
<!--## <NAME>Name of the page</NAME> <DESC>Description of the page</DESC> <SECTION>Top Category||Sub Category||Sub Sub Category</SECTION> ##-->
The following problems are known due way how processing of this comment is implemented:
- no validation is made, that it's a valid XML (e.g. placing "&" instead of "&" won't break anything
- the XML is treated like a regular string and therefore typical XML errors (e.g. pair tag name mismatch) are simply ignored
- when unknown XML settings are added developer isn't informed about that, which can lead to code working in unexpected ways
Solution
The plan below would allow separate all template comment related logic into one class and allow addition of new settings later if needed.
- create the "
TemplateCommentParser
" class, that:- would accept path to template as constructor argument
- the "
parse
" method would return associative array of results
- add support for data types in the class:
- "string" - any string
- "boolean" - true (when "yes", "true" or "1"), false (when "no", "false" or "0"), exception otherwise
- "category_path" - would split value by "||" and trim each each element
- define supported settings and their data types as class properties
- when template doesn't exist throw an exception
- when no meta comment found, then return an empty array
- when meta comment found:
- if it's invalid XML, then throw an exception
- when unsupported setting found, then throw an exception
- when supported setting found, but it has value unknown format, then throw an exception
- return parsing result