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

Numeric attribute fix #180

Closed
wants to merge 4 commits into from

Conversation

bfintal
Copy link
Contributor

@bfintal bfintal commented Feb 21, 2015

Fixes #128

Doing this

'attrs' => array(
    array(
       'label' => 'url',
       'attr'  => '0',
       'type'  => 'textarea',
    ),
),

creates a shortcode with an attribute 0="value", WordPress cannot parse this as an unnamed attribute. The regex here removes numeric attributes and removes them. This doesn't affect attribute-like strings outside the shortcode definition and doesn't affect normal string attributes.

@danielbachhuber
Copy link
Contributor

The regex here removes numeric attributes and removes them.

The problem is that it's also special-casing numeric attributes, so I can no longer use 0 as an attribute (as unlikely as that is).

@bfintal
Copy link
Contributor Author

bfintal commented Feb 22, 2015

You can, unnamed attributes in WP are placed as numeric keys in the $attr variable during shortcode parsing. So I from what I can tell unnamed & numeric are treated the same way in WP. So there shouldn't be any problems.

Tested this using Jetpack's googlemaps shortcode:

/**
 * [googlemaps] shortcode
 *
 * Example usage:
 *   [googlemaps http://maps.google.com/maps?f=q&hl=en&geocode=&q=San+Francisco,+CA&sll=43.469466,-83.998504&sspn=0.01115,0.025942&g=San+Francisco,+CA&ie=UTF8&z=12&iwloc=addr&ll=37.808156,-122.402458&output=embed&s=AARTsJp56EajYksz3JXgNCwT3LJnGsqqAQ&w=425&h=350]
 *   [googlemaps https://mapsengine.google.com/map/embed?mid=zbBhkou4wwtE.kUmp8K6QJ7SA&w=640&h=480]
 */
function jetpack_googlemaps_shortcode( $atts ) {
    if ( !isset($atts[0]) )
        return '';

    $params = ltrim( $atts[0], '=' );

    var_dump( $atts[0] );

    // didn't paste the rest of the code

The var_dump( $atts[0] ) there returned the correct attribute after the preg_replace ran.

@mattheu
Copy link
Contributor

mattheu commented Feb 24, 2015

Also related - #128

I'm not sure how we would tackle this.

EDIT - Oh - I see you already commented on this.

@bfintal
Copy link
Contributor Author

bfintal commented Feb 24, 2015

Did some tests and it seems that this PR just solves part of the problem. Will get back with an explanation.

@danielbachhuber
Copy link
Contributor

I think we should have some unit test coverage here.

Now unnamed attributes are rendered, but editing them removes the values
@bfintal
Copy link
Contributor Author

bfintal commented Feb 24, 2015

After extensive testing and debugging, I'm revamping my fix for this. I think this is the right way to approach this.

The real problem: unnamed attributes are not recognized by the editor views & the UI

I removed my initial commit with the regex, and now the views recognize the unnamed attributes.

The fix is to handle unnamed attributes as numeric ones. That means:

array(
   'label' => '',
   'attr'  => '0',
   'type'  => 'text',
   'value' => 'value1'
),
array(
   'label' => '',
   'attr'  => '1',
   'type'  => 'text',
   'value' => 'value2'
),

Will now generate:

[shortcode "value1" "value2"]

WordPress/PHP-wise, these attributes are accessible using the normal $attr[0] and $attr[1]

The fix is not yet complete. The only thing missing now is that editing an existing shortcode turns the attribute input fields blank, like the values are not being found.

@bfintal
Copy link
Contributor Author

bfintal commented Feb 24, 2015

The last part of the solution may be is to add another regex at the function sui.utils.shortcodeViewConstructor.edit to check for unnamed attribute values, then do an attr.set on those. TBH, I'm having trouble with figuring out a clean regex for this. Will continue trying in the morning if nobody solves this :)

@bfintal
Copy link
Contributor Author

bfintal commented Feb 25, 2015

Alright, finally finished the fix. I'm very well pleased with the results.

bfintal added a commit to bfintal/Shortcake that referenced this pull request Feb 25, 2015
Since the code has changed a lot since wp-shortcake#180, I’m redoing the changes
with the new file structure
@bfintal
Copy link
Contributor Author

bfintal commented Feb 25, 2015

Closing and doing a new cleaner PR #198, since this now has tons of conflicts.

@bfintal bfintal closed this Feb 25, 2015
@mattheu
Copy link
Contributor

mattheu commented Feb 25, 2015

now has tons of conflicts.

We just landed some big changes to the structure of the JS. Apologies for the conflicts!

@bfintal
Copy link
Contributor Author

bfintal commented Feb 25, 2015

Apologies for the conflicts!

No problem! It's about time I tried to learn how to use grunt.

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.

Support positional (unnamed) parameters
3 participants