bug', 'file' ); return; } if ( ! Builder::isIndexValid( '', 'tmp' ) ) { Builder::log( '[Indexer] Skipping copying of "tmp" tables - "tmp" index is invalid', 'debug', 'file' ); return; } $tables = Utils::getAllPluginTables(); foreach ( $tables as $table ) { if ( strpos( $table, 'dgwt_wcas_stats' ) ) { continue; } // Remove all not "main" index tables (even those that don't have "tmp" version). if ( strpos( $table, '_tmp' ) === false ) { $wpdb->query( "DROP TABLE IF EXISTS $table" ); } if ( strpos( $table, '_tmp' ) !== false ) { $mainTable = str_replace( '_tmp', '', $table ); // Make sure the table at index "main" does not exist. $wpdb->query( "DROP TABLE IF EXISTS $mainTable" ); $wpdb->query( "RENAME TABLE $table TO $mainTable" ); } } foreach ( self::getIndexInfoStruct() as $key => $value ) { $value = get_option( self::LAST_BUILD_OPTION_KEY . '_' . $key . '_tmp', $value ); delete_option( self::LAST_BUILD_OPTION_KEY . '_' . $key ); /* * Make sure the options are copied. * Sometimes it happened that some options were not copied, and we have to be sure that everything went OK. */ $count = 0; do { $count ++; if ( $count > 1 ) { usleep( 500000 ); } if ( $count === 1 ) { $addResult = add_option( self::LAST_BUILD_OPTION_KEY . '_' . $key, $value, '', 'no' ); $savedValue = get_option( self::LAST_BUILD_OPTION_KEY . '_' . $key ); $continue = ! $addResult || $value !== $savedValue; } else { delete_option( self::LAST_BUILD_OPTION_KEY . '_' . $key ); $wpdb->query( $wpdb->prepare( "INSERT INTO $wpdb->options (`option_name`, `option_value`, `autoload`) VALUES (%s, %s, 'no')", self::LAST_BUILD_OPTION_KEY . '_' . $key, maybe_serialize( $value ) ) ); $continue = false; } if ( $key === 'logs' ) { break; } } while ( $continue && $count < 3 ); } } /** * Remove all options created by this plugin * * @param bool $networkScope delete tables in whole network * * @return void */ public static function deleteIndexOptions( $networkScope = false ) { global $wpdb; $prefix = $wpdb->prefix; if ( is_multisite() && $networkScope ) { $prefix = $wpdb->base_prefix; } $lastBuildOptionKey = $wpdb->esc_like( self::LAST_BUILD_OPTION_KEY ) . '%'; $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->options WHERE option_name LIKE %s", $lastBuildOptionKey ) ); delete_transient( self::DETAILS_DISPLAY_KEY ); if ( is_multisite() && $networkScope ) { foreach ( get_sites() as $site ) { if ( is_numeric( $site->blog_id ) ) { $blogID = $site->blog_id == 1 ? '' : $site->blog_id . '_'; $table = $prefix . $blogID . 'options'; $wpdb->query( $wpdb->prepare( "DELETE FROM $table WHERE option_name LIKE %s", $lastBuildOptionKey ) ); $wpdb->delete( $table, array( 'option_name' => '_transient_timeout_' . self::DETAILS_DISPLAY_KEY ) ); $wpdb->delete( $table, array( 'option_name' => '_transient_' . self::DETAILS_DISPLAY_KEY ) ); } } } } /** * Remove all database tables created by this plugin * * @param bool $networkScope delete tables in whole network * * @return void */ public static function deleteDatabaseTables( $networkScope = false ) { global $wpdb; // DB tables $tables = Utils::getAllPluginTables( $networkScope ); if ( ! empty( $tables ) ) { foreach ( $tables as $table ) { $wpdb->query( "DROP TABLE IF EXISTS $table" ); } } } /** * Removal of planned actions that will update products in the index */ public static function wipeActionScheduler() { $queue = Utils::getQueue(); if ( empty( $queue ) ) { return; } try { $queue->cancel_all( 'dgwt/wcas/tnt/background_product_update' ); } catch ( Exception $e ) { } } /** * Dispatch building variation index */ public static function maybeDispatchVariationAsyncProcess() { if ( ! self::canBuildVariationsIndex() ) { return; } $status = self::getInfo( 'status', Config::getIndexRole() ); $sEndTs = absint( self::getInfo( 'end_searchable_ts', Config::getIndexRole() ) ); $rEndTs = absint( self::getInfo( 'end_readable_ts', Config::getIndexRole() ) ); if ( ( Config::isIndexerMode( 'async' ) && $status === 'building' && ! empty( $rEndTs ) ) || ( Config::isIndexerMode( 'sync' ) && $status === 'building' && ! empty( $sEndTs ) && ! empty( $rEndTs ) ) || ( Config::isIndexerMode( 'direct' ) && $status === 'building' ) ) { self::addInfo( 'start_variation_ts', time() ); // Reset end time because this process may end several times self::addInfo( 'end_variation_ts', 0 ); self::log( '[Variation index] Building...' ); DGWT_WCAS()->tntsearchMySql->asynchBuildIndexV->maybe_dispatch(); } } /** * Dispatch building taxonomies index */ public static function maybeDispatchTaxonomyAsyncProcess() { if ( ! self::canBuildTaxonomyIndex() ) { self::maybeDispatchVariationAsyncProcess(); return; } $status = self::getInfo( 'status', Config::getIndexRole() ); $rEndTs = absint( self::getInfo( 'end_readable_ts', Config::getIndexRole() ) ); if ( ( Config::isIndexerMode( 'async' ) && $status === 'building' && ! empty( $rEndTs ) ) || ( Config::isIndexerMode( 'sync' ) && $status === 'building' && ! empty( $rEndTs ) ) || ( Config::isIndexerMode( 'direct' ) && $status === 'building' ) ) { RequestT::handle(); } } /** * Check if the indexer working too long without any action * * @return bool */ public static function isIndexerWorkingTooLong( $forceMaxNoActionTime = 0 ) { $status = Builder::getInfo( 'status', Config::getIndexRole() ); // Return early if indexer is not working if ( ! in_array( $status, array( 'building', 'preparing' ) ) ) { return false; } $lastActionTs = absint( Builder::getInfo( 'last_action_ts', Config::getIndexRole() ) ); // Return early if the indexer info hasn't been created yet if ( empty( $lastActionTs ) ) { return false; } $diff = time() - $lastActionTs; $maxNoActionTime = defined( 'DISABLE_WP_CRON' ) && DISABLE_WP_CRON ? 61 * MINUTE_IN_SECONDS : 16 * MINUTE_IN_SECONDS; /** * Filters maximum no action time of indexer. * * @param int $maxNoActionTime Max time in seconds. 16 min if WP-Cron is enabled or 61 min if not */ $maxNoActionTime = apply_filters( 'dgwt/wcas/indexer/max_no_action_time', $maxNoActionTime ); if ( $forceMaxNoActionTime > 0 ) { $maxNoActionTime = $forceMaxNoActionTime; } return in_array( $status, array( 'building', 'preparing' ) ) && $diff >= $maxNoActionTime; } }