Skip to content

Conversation

matt-bernhardt
Copy link
Member

@matt-bernhardt matt-bernhardt commented Apr 19, 2023

Ticket: https://mitlibraries.atlassian.net/browse/LM-306

This makes the map template into something usable. Specific changes here are:

  • Fixes how a $val variable is being assigned within the map template, only picking a random value from an array if that array is not empty - this is used to pick a thumbnail to represent a location when multiple images have been uploaded.
  • Replaces a global $gStudy24Url variable which used to be defined in functions.php, instead defining the variable on the templates which need it. More details and a reference URL are in that commit message.
  • Improves how a v parameter is defined and handled by the map template (was throwing a warning)
  • Updates how the $pageLink variable is calculated, preventing a scenario where we were trying to extract object properties from a boolean value. The new logic sets $pageLink based on the location record as a default, and then overrides that if the location record has been assigned to display at a given page location. (This construct appears three times on the map page template).

Please note CodeClimate is flagging the use of variable names that don't follow snake_case naming conventions. Starting to make those changes now will, I fear, start touching more and more lines in the template, which will end in a general rebuild of the template. I don't think that I have time for that, for as much as it would carry some advantages.

Developer

Secrets

  • No secrets are impacted by this work.

Documentation

  • No documentation changes are made.

Accessibility

  • ANDI or Wave has been run in accordance to
    our guide and
    all issues introduced by these changes have been resolved or opened as new
    issues (link to those issues in the Pull Request details above)

Stakeholder approval

  • Stakeholder approval has been confirmed

Dependencies

NO dependencies are updated

Code Reviewer

  • The commit message is clear and follows our guidelines
    (not just this pull request message)
  • The changes have been verified
  • The documentation has been updated or is unnecessary
  • New dependencies are appropriate or there were no changes

** Why are these changes being introduced:

* Attempting to load the map generates a few warning and error messages
  in the console. One of them reads:

PHP Warning:  Undefined array key "v" in /app/web/app/themes/mitlib-parent/templates/page-map-locations.php on line 16

** Relevant ticket(s):

* https://mitlibraries.atlassian.net/browse/lm-306

** How does this address that need:

This improves the way that the map template requests the "v" query
parameter.

* First, it declares that this is an expected parameter via a call to
  $wp->add_query_var as part of an init hook.
* With that done, we can now get the parameter from the get_query_var
  function, rather than directly from the $_GET superglobal.
* Along the way we also ditch a confusing line of code with an unneeded
  ternary.

** Document any side effects to this change:

* Frankly, I'm not sure that this whole construct is used anywhere. The
  map looks no different if you append ?v=map to the URL or don't, which
  is the only value that causes the showMap javascript variable to be a
  1. But, I'm leery right now of removing things that end up being
  needed somehow.
** Why are these changes being introduced:

* Attempting to load the map template right now generates the following
  error message in the logs:

PHP Fatal error:  Uncaught ValueError: array_rand(): Argument #1 ($array) cannot be empty in /app/web/app/themes/mitlib-parent/templates/page-map-locations.php:63

** Relevant ticket(s):

* https://mitlibraries.atlassian.net/browse/lm-306

** How does this address that need:

* The problem appears to be that the page template doesn't anticipate a
  condition in which no images are associated with a location record?
  Which is odd, as there _are_ records in the legacy system which do not
  have images. Regardless, the Courtyard Cafe record is definitely
  breaking the map template because the $arMain array in this template
  ends up being empty.
* This changes how this array is handled, only grabbing a random item
  from $arMain if there is at least one element in the array. Otherwise
  the default value of '' (now assigned at the start of these lines) is
  left unchanged.

** Document any side effects to this change:

* This template is a pit of vipers, lying in wait for an unsuspecting
  developer. I barely escaped with my life.
This was a global variable set in functions.php, which is no longer
considered a good practice - so I'm changing to define the value on
any page template that needs it.

See this page for details: https://codex.wordpress.org/Global_Variables

There are likely even better ways to handle this, but nothing is a quick, easily achievable approach that puts control over this in the hands of UX.
@matt-bernhardt matt-bernhardt changed the title Clean up handling of v parameter on map template Fix map template on parent theme Apr 20, 2023
** Why are these changes being introduced:

* An attempt to calculate the URL for location records is currently a
  bit messy, spawning messages like this in the logs:

PHP Warning:  Attempt to read property "ID" on bool in /app/web/app/themes/mitlib-parent/templates/page-map-locations.php on line 85

** Relevant ticket(s):

* https://mitlibraries.atlassian.net/browse/lm-306

** How does this address that need:

* This changes how the $pageLink value is calculated within the page
  template. That value could come from two different places, and the old
  code relied on WordPress' forgiving nature a bit too much. The new way
  first assigns $pageLink to the URL for the Location record itself
  (which is either native to the Location, or is defined as a separate
  path via the Page Links To plugin - but either was is returned by
  get_permalink( $post ) ).
* Then, for location records which have been set - via the display_page
  custom field - to render at a different location, we look up that URL
  via get_permalink( displayPage->ID ).

** Document any side effects to this change:

* This change gets made in three locations on the page template, because
  the template is a bit repetitive and should ideally be supported by
  some custom functions.
@matt-bernhardt matt-bernhardt marked this pull request as ready for review April 24, 2023 14:25
@matt-bernhardt matt-bernhardt requested a review from JPrevost April 24, 2023 14:25
// phpcs:disable WordPress.Security.ValidatedSanitizedInput.InputNotValidated -- need to properly receive 'v' parameter.
$showMap = ( $_GET['v'] != '' ) && ( $_GET['v'] == 'map' ) ? 1 : 0;
// phpcs:enable
$v = get_query_var( 'v' );
Copy link
Member

Choose a reason for hiding this comment

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

This is a fantastic example of how clear multiline conditionals are so much clearer than the single line equivalent.

@matt-bernhardt matt-bernhardt merged commit 60e9971 into master Apr 24, 2023
@matt-bernhardt matt-bernhardt deleted the lm306 branch April 24, 2023 17:22
matt-bernhardt pushed a commit that referenced this pull request May 10, 2023
remove the lh-hsts plugin requirement
this causes a conflict with hsts defined in pantheon.yml. users should just use pantheon.yml.

see https://docs.pantheon.io/pantheon-yml#enforce-https--hsts
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.

2 participants