mardi 26 avril 2016

Why is WordPress 4.5's New JSON Data Handling Breaking My Plugin?

In version 4.5, WordPress changed how it handles POST data in the edit menu items page. Ok, cool. However, for some reason their change broke my plugin and I don't understand why or how to unbreak it. An array of items entered on the menu items page no longer saves to the database.

Here's the culprit change in WP 4.5:

/*
 * If a JSON blob of navigation menu data is found, expand it and inject it
 * into `$_POST` to avoid PHP `max_input_vars` limitations. See #14134. 
 */
if ( isset( $_POST['nav-menu-data'] ) ) {
    $data = json_decode( stripslashes( $_POST['nav-menu-data'] ) );
    if ( ! is_null( $data ) && $data ) {
        foreach ( $data as $post_input_data ) {
            // For input names that are arrays (e.g. `menu-item-db-id[3]`), derive the array path keys via regex.
            if ( preg_match( '#(.*)(?:\[(\d+)\])#', $post_input_data->name, $matches ) ) {
                if ( empty( $_POST[$matches[1]] ) ) {
                    $_POST[$matches[1]] = array();
                }
                $_POST[$matches[1]][(int)$matches[2]] = $post_input_data->value;
            } else {
                $_POST[$post_input_data->name] = $post_input_data->value;
            }
        }
    }
}

See: http://ift.tt/20Ydjj4 When I delete the new code from WordPress Core it works fine.

My plugin asking for user data (an array of locations):

<select name="menu-item-visibility[<?php echo $item_id; ?>][]" id="edit-menu-item-visibility-<?php echo $item_id; ?>" class="chzn-select" multiple="true">
<?php
$vals = get_post_meta( $item_id, 'locations', true );
foreach( $countries as $key => $value ) { 
?>
    <option value="<?php echo $key;?>"<?php echo is_array( $vals ) && in_array( $key, $vals ) ? "selected='selected'" : ''; ?>> <?php echo $value;?> </option>
<?php
}
?>
</select>

And trying to put it in the database:

/* Put locations in the database. */
function csmi_update_locations( $menu_id, $menu_item_db_id, $args ) {
    $meta_value = get_post_meta( $menu_item_db_id, 'locations', true );
    if ( isset( $_POST[ 'menu-item-visibility' ][ $menu_item_db_id ] ) ) { 
        $new_meta_value = $_POST[ 'menu-item-visibility' ][ $menu_item_db_id ];
    }
    if ( !isset($new_meta_value ) ) {
    delete_post_meta( $menu_item_db_id, 'locations', $meta_value );
    }
    elseif ( $meta_value !== $new_meta_value ) {
        update_post_meta( $menu_item_db_id, 'locations', $new_meta_value );
    }
}

See: http://ift.tt/1MVk56C

Any idea why it's not saving?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire