[wp-trac] [WordPress Trac] #37678: Add an action hook for plugins to do database upgrades on

WordPress Trac noreply at wordpress.org
Fri Aug 19 13:36:44 UTC 2016


#37678: Add an action hook for plugins to do database upgrades on
-----------------------------+------------------------------
 Reporter:  johnjamesjacoby  |       Owner:
     Type:  defect (bug)     |      Status:  new
 Priority:  normal           |   Milestone:  Awaiting Review
Component:  Database         |     Version:
 Severity:  normal           |  Resolution:
 Keywords:  2nd-opinion      |     Focuses:
-----------------------------+------------------------------

Comment (by flixos90):

 Alternatively we could handle storing the database version of the plugins,
 and the plugins could register their update function.

 {{{
 function update_database() {
     global $db_plugins;

     $versions = get_option( 'plugin_db_versions', array() );

     foreach ( $db_plugins as $plugin => $data ) {
         if ( empty( $versions[ $plugin ] ) || version_compare(
 $data['version'], $versions[ $plugin ], '<' ) ) {
             call_user_func( $data['callback'], $versions[ $plugin ] );

             $versions[ $plugin ] = $data['version'];
         }
     }

     update_option( 'plugin_db_versions', $versions );
 }
 add_action( 'admin_init', 'update_database' );

 function register_db_update_hook( $plugin, $version, $callback ) {
     global $db_plugins;

     $db_plugins[ $plugin ] = array( 'version' => $version, 'callback' =>
 $callback );
 }
 }}}

 Something like this code could be used (I don't want to introduce a
 global, but for the sake of simplicity I do it above) - of course it would
 need to be refined. Also, not sure if that function should be hooked into
 `admin_init` for the reasons @johnjamesjacoby  stated. It could just be
 called afterwards.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/37678#comment:1>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list