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 ); // Check DB and display an admin notice if there are tables missing add_action( 'init', array( $this, 'verify_db' ) ); // Install the plugin add_action( 'wp_stream_before_db_notices', array( $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 = isset( $_REQUEST['wp_stream_update'] ) ? $_REQUEST['wp_stream_update'] : null; 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(); } /** * Verify that the required DB tables exists * * @return void */ public function verify_db() { /** * Filter will halt install() if set to true * * @param bool * * @return bool */ if ( apply_filters( 'wp_stream_no_tables', false ) ) { return; } if ( ! function_exists( 'is_plugin_active_for_network' ) ) { require_once( ABSPATH . '/wp-admin/includes/plugin.php' ); } /** * Fires before admin notices are triggered for missing database tables. */ do_action( 'wp_stream_before_db_notices' ); global $wpdb; $database_message = ''; $uninstall_message = ''; // Check if all needed DB is present $missing_tables = array(); foreach ( $this->plugin->db->get_table_names() as $table_name ) { $table_search = $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $table_name ) ); if ( $table_search !== $table_name ) { $missing_tables[] = $table_name; } } if ( $missing_tables ) { $database_message .= sprintf( '%s %s', _n( 'The following table is not present in the WordPress database:', 'The following tables are not present in the WordPress database:', count( $missing_tables ), 'stream' ), esc_html( implode( ', ', $missing_tables ) ) ); } if ( is_plugin_active_for_network( $this->plugin->locations['plugin'] ) && current_user_can( 'manage_network_plugins' ) ) { $uninstall_message = sprintf( __( 'Please uninstall the Stream plugin and activate it again.', 'stream' ), network_admin_url( 'plugins.php#stream' ) ); } elseif ( current_user_can( 'activate_plugins' ) ) { $uninstall_message = sprintf( __( 'Please uninstall the Stream plugin and activate it again.', 'stream' ), admin_url( 'plugins.php#stream' ) ); } if ( ! empty( $database_message ) ) { $this->plugin->admin->notice( $database_message ); if ( ! empty( $uninstall_message ) ) { $this->plugin->admin->notice( $uninstall_message ); } } } /** * 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 ( is_plugin_active_for_network( $plugin ) ) { $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 ); } return; } /** * @return string */ public function get_db_version() { return get_site_option( $this->option_key ); } /** * @return void */ 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 = isset( $_REQUEST['wp_stream_update'] ) ? $_REQUEST['wp_stream_update'] : null; 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() { ?>