sage-directives icon indicating copy to clipboard operation
sage-directives copied to clipboard

Fix issues with field, hasfield, isfield post ID parsing

Open EHLOVader opened this issue 4 years ago • 4 comments

Switching to is_numeric to identify ID values from strings, since the blade template is always parsed as a string and is_string wasn't catching IDs.

This may however need more to really fix it. This will handle any cases when someone passes in a number directly, but does not handle variables or function calls, which might happen.

Fixes #44 Fixes #63

EHLOVader avatar Nov 03 '21 22:11 EHLOVader

I have added an attempt at identifying wp post ID retrieval methods which might be used with get_field* directives.

EHLOVader avatar Nov 16 '21 17:11 EHLOVader

This is really needed. Also fixes an issue I opened some days ago before I saw this PR: https://github.com/Log1x/sage-directives/issues/70

@Log1x Sorry, is this package still maintained?

erip2 avatar Mar 22 '22 16:03 erip2

@erip2 It was. I may have slowed this down, not wanting to introduce bugs and hoping that it could be tested by others who had issues I've mentioned above.

Log1x was open to merge

It sounds like you may have confirmed it worked with composer-patches perhaps?

EHLOVader avatar Mar 23 '22 21:03 EHLOVader

@EHLOVader I didn't use composer-patches.

I've only created my own directives with the changes you've made here and I've used those but I would like better to use the package ones instead of creating custom ones.

erip2 avatar Mar 24 '22 15:03 erip2

Wondering if this looks good to merge? If it does can you merge it and add #hacktoberfest-accepted label to it? Thanks!

EHLOVader avatar Oct 09 '22 18:10 EHLOVader

This PR fails when you pass a variable to the @field directive. Take the following use-case:

@foreach($postids as $id)
{{ $id }}'s Title: @field('title', $id)
@endforeach

It should be noted that if you are passing variables as your $id you are limited to the following only:

  • get_the_ID()
  • $post->ID
  • get_queried_object_id()

i.e.. works:

@foreach($posts as $post)
{{ $post->ID }}'s Title: @field('title', $post->ID)
@endforeach

2gen avatar May 25 '23 17:05 2gen

That is correct.

It also accepts an explicit number as noted in the documentation.

@field('text', 1)

I think that $id makes sense to add to the list of options. I tried to find any ways that someone might be able to get an ID and pass it through in the view. Before this I don't think it worked for any value (variables, functions, or numbers) because it was using is_string and the first go at this fix was just checking is_numeric which worked with all the examples in the docs, but didn't work when I wanted to use a variable.

It would be good to add these notes to the docs, or maybe it needs rewritten to work differently. Not sure what can reliably be done since it isn't running the php just parsing the string and generating php from the blade input so you have no clue what output of a function will be, or the type of the variable.

I think this presented issues with string manipulations and arrays before too.

EHLOVader avatar May 25 '23 19:05 EHLOVader

Superseded by #82

Log1x avatar Jul 29 '23 14:07 Log1x