zencart / zencart

Zen Cart® is a full-function e-commerce application for your website.
https://github.com/zencart/zencart/releases
Other
375 stars 233 forks source link

Custom page with custom META_TAG_TITLE causes 'Constant META_TAG_TITLE already defined' Warning #5449

Closed neekfenwick closed 1 year ago

neekfenwick commented 1 year ago

Describe the bug includes/modules/meta_tags.php allows you to define a constant named after your custom page, which is used to define META_TAG_TITLE, but later again defines META_TAG_TITLE, causing a duplicate definition Warning to be generated.

Version Zen Cart version: 1.5.8 PHP version: 8.1 MySQL version: n/a Plugins that may be related: n/a What older/newer versions were also tested? n/a

To Reproduce Steps to reproduce/demonstrate the behavior:

  1. Create a includes/modules/pages/custom_page/header_php.php which contains: define('META_TAG_TITLE_CUSTOM_PAGE', 'This is a custom meta title tag');
  2. Create a includes/templates/template_default/tempaltes/tpl_custom_page_default.php which contains Hello World text.
  3. Navigate to the page: https://my-site.com/index.php?main_page=custom_page
  4. See Warning generated: PHP Warning: Constant META_TAG_TITLE already defined in /home/neek/workspace/zen-cart/zencart/includes/modules/meta_tags.php on line 344.

Expected behavior Constants should not be defined twice.

Screenshots Screenshot from 2022-12-07 10-37-51

Additional context I'm not sure how many people create custom pages with custom meta tags like this.

meta_tags.php line 40 sets META_TAG_TITLE: if (defined('META_TAG_TITLE_' . strtoupper($metatag_page_name))) define('META_TAG_TITLE', constant('META_TAG_TITLE_' . strtoupper($metatag_page_name))); then the default case in the switch, hit because this is a custom page not matched by the other case blocks, sets it again on line 344: define('META_TAG_TITLE', (defined('NAVBAR_TITLE') ? NAVBAR_TITLE . PRIMARY_SECTION : '') . TITLE . TAGLINE);

Maybe just guard those lines around 344 with defined() checks?

scottcwilson commented 1 year ago

Can you use a 1.5.8 style language file in this case? Move your define out of

includes/modules/pages/custom_page/header_php.php

to

includes/languages/english/lang.custom_page.php

and make it an array entry, in the style of new language file settings, per

https://docs.zen-cart.com/dev/code/158_language_files/

neekfenwick commented 1 year ago

Sorry for the late reply, been very busy. Tried your suggestion but for some reason lang.custom_page.php wasn't loaded at all. I'll try to spend more time on it but doing live site rollout today so likely next week.

scottcwilson commented 1 year ago

I have never done what you're doing or seen it done - this could be a bug or a weakness in the documentation. Please let me know what you find or (better) submit a PR if required.