plugin = $plugin; $this->db_version = $this->get_db_version(); $this->stream_url = self_admin_url( $this->plugin->admin->admin_parent_page . '&page=' . $this->plugin->admin->settings_page_slug ); // Ensure the tables are created even when the plugin activation hook does not run, // and run the check in WP Admin or on WP CLI to avoid extra frontend load. if ( is_admin() || ( defined( 'WP_CLI' ) && WP_CLI ) ) { $this->check(); } register_activation_hook( $this->plugin->locations['plugin'], array( $this, 'check' ) ); } /** * Check db version, create/update table schema accordingly * If database update required admin notice will be given * on the plugin update screen * * @return void */ public function check() { if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { return; } if ( empty( $this->db_version ) ) { $this->install( $this->plugin->get_version() ); return; } if ( $this->plugin->get_version() === $this->db_version ) { return; } $update = null; if ( isset( $_REQUEST['wp_stream_update'] ) && wp_verify_nonce( 'wp_stream_update_db' ) ) { $update = esc_attr( $_REQUEST['wp_stream_update'] ); } if ( ! $update ) { $this->update_required = true; $this->success_db = $this->update( $this->db_version, $this->plugin->get_version(), array( 'type' => 'auto', ) ); } if ( 'update_and_continue' === $update ) { $this->success_db = $this->update( $this->db_version, $this->plugin->get_version(), array( 'type' => 'user', ) ); } $versions = $this->db_update_versions(); if ( ! $this->success_db && version_compare( end( $versions ), $this->db_version, '>' ) ) { add_action( 'all_admin_notices', array( $this, 'update_notice_hook' ) ); return; } $this->update_db_option(); } /** * Register a routine to be called when stream or a stream connector has been updated * It works by comparing the current version with the version previously stored in the database. * * @param string $file A reference to the main plugin file. * @param string $callback The function to run when the hook is called. * @param string $version The version to which the plugin is updating. * * @return void */ public function register_update_hook( $file, $callback, $version ) { if ( ! is_admin() ) { return; } $plugin = plugin_basename( $file ); if ( $this->plugin->is_network_activated() ) { $current_versions = get_site_option( $this->option_key . '_connectors', array() ); $network = true; } elseif ( is_plugin_active( $plugin ) ) { $current_versions = get_option( $this->option_key . '_connectors', array() ); $network = false; } else { return; } if ( version_compare( $version, $current_versions[ $plugin ], '>' ) ) { call_user_func( $callback, $current_versions[ $plugin ], $network ); $current_versions[ $plugin ] = $version; } if ( $network ) { update_site_option( $this->option_key . '_registered_connectors', $current_versions ); } else { update_option( $this->option_key . '_registered_connectors', $current_versions ); } } /** * Returns the database version. * * @return string */ public function get_db_version() { return get_site_option( $this->option_key ); } /** * Checks if migration was successful. */ public function update_db_option() { if ( $this->success_db ) { $success_op = update_site_option( $this->option_key, $this->plugin->get_version() ); } if ( ! empty( $this->success_db ) ) { return; } wp_die( esc_html__( 'There was an error updating the Stream database. Please try again.', 'stream' ), esc_html__( 'Database Update Error', 'stream' ), array( 'response' => 200, 'back_link' => 1, ) ); } /** * Added to the admin_notices hook when file plugin version is higher than database plugin version * * @action admin_notices * * @return void */ public function update_notice_hook() { if ( ! current_user_can( $this->plugin->admin->view_cap ) ) { return; } $update = null; if ( isset( $_REQUEST['wp_stream_update'] ) && wp_verify_nonce( 'wp_stream_update_db' ) ) { $update = esc_attr( $_REQUEST['wp_stream_update'] ); } if ( ! $update ) { $this->prompt_update(); return; } if ( 'update_and_continue' === $update ) { $this->prompt_update_status(); } } /** * Action hook callback function * * Adds the user controlled database upgrade routine to the plugins updated page. * When database update is complete page will refresh with dismissible message to user. * * @return void */ public function prompt_update() { ?>