Plugin upgrading: Widget settings
I have tried doing some research on this but haven't found anything solid yet. I have a plugin I am working on and between the last version and the new version we made some updates to the widget that changes some of the settings names (on the backend) and I am having trouble creating an upgrade routine to do this.
What I have done so far that seems to (mostly) work is this:
$widget = get_option( 'widget_name' );
if( is_array( $widget ) ! empty( $widget ) ) {
foreach( $widget as $a = $b ) {
if( ! is_array( $b ) ) {
continue;
}
foreach( $b as $k = $v ) {
$widget[$a]['setting1'] = $widget[$a]['oldsetting1'];
$widget[$a]['setting2'] = $widget[$a]['oldsetting2'];
}
}
update_option( 'widget_name', $widget );
}
In most of my tests this works out ok, but the problem becomes that the old widget no longer displays it's output. Only the title of the widget will show. I can fix this by going and saving each individual widget and then it will work fine, but I don't want to make my users do that.
I thought something like this might work:
$settings = $widgets-get_settings();
foreach( $settings as $s ) {
$s['setting1'] = $s['oldsetting1'];
$s['setting2'] = $s['oldsetting2'];
$widgets-save_settings( $s );
}
But it seems that the save_settings()
call must be wrong because this removes the widget entirely.
I am having trouble finding any sort of standard for something like this and would just like to hear any thoughs, ideas, or links you might have to doing something like this.
Thanks in advance for any help.
EDIT:
This is not actually a question about tracking license keys or upgrading plugins that aren't hosted on the WP repo. What this is more about is updating settings between 2 version of a plugin when a user upgrades.
Example:
version 1.0.0 has a setting field name
Well in version 1.1.0 we decide we need both first and last name so we change the old setting to be first_name
and then add a new setting last_name
.
Transferring these options if saved as post meta for a custom post type is no problem:
$old_name = get_post_meta( $post-ID, 'name', true );
$first_name = update_post_meta ( $post-ID, 'first_name', true );
delete_post_meta( $post-ID, 'name' );
So that part is easy. What I am having trouble with that seems to not be easy is doing this same thing but for WIDGET settings.
Hopefully this will clear up any confusion and help to make this be easier to answer.
EDIT 2:
Result of echo 'pre' . print_r( $widget, true ) . '/pre';
from first code chunk above:
Array
(
[2] = Array
(
[title] = Class Schedule
[id] = 23
[display_type] = grid
[order] = asc
[display_title_text] = Events on
[paging] = 1
[list_max_num] = 7
[list_max_length] = days
[list_start_offset_num] = 0
[list_start_offset_direction] = back
[gce_per_page_num] = 7
[gce_events_per_page] = days
)
[3] = Array
(
[title] = Examples
[id] = 24
[display_type] = grid
[order] = asc
[display_title_text] = Events on
[paging] = 1
[list_max_num] = 7
[list_max_length] = days
[list_start_offset_num] = 0
[list_start_offset_direction] = back
[gce_per_page_num] = 7
[gce_events_per_page] = days
)
[_multiwidget] = 1
)