wp_cache_replace_line()WPSCache 1.0

No Hooks.

Return

null. Nothing.

Usage

wp_cache_replace_line( $old, $new, $my_file );
$old (required)
-
$new (required)
-
$my_file (required)
-

Code of wp_cache_replace_line() WPSCache 1.9

function wp_cache_replace_line( $old, $new, $my_file ) {
	if ( @is_file( $my_file ) == false ) {
		if ( function_exists( 'set_transient' ) ) {
			set_transient( 'wpsc_config_error', 'config_file_missing', 10 );
		}
		return false;
	}
	if ( ! is_writeable_ACLSafe( $my_file ) ) {
		if ( function_exists( 'set_transient' ) ) {
			set_transient( 'wpsc_config_error', 'config_file_ro', 10 );
		}
		trigger_error( "Error: file $my_file is not writable." );
		return false;
	}

	$found  = false;
	$loaded = false;
	$c      = 0;
	$lines  = array();
	while ( ! $loaded ) {
		$lines = file( $my_file );
		if ( ! empty( $lines ) && is_array( $lines ) ) {
			$loaded = true;
		} else {
			$c++;
			if ( $c > 100 ) {
				if ( function_exists( 'set_transient' ) ) {
					set_transient( 'wpsc_config_error', 'config_file_not_loaded', 10 );
				}
				trigger_error( "wp_cache_replace_line: Error  - file $my_file could not be loaded." );
				return false;
			}
		}
	}
	foreach ( (array) $lines as $line ) {
		if (
			trim( $new ) != '' &&
			trim( $new ) == trim( $line )
		) {
			wp_cache_debug( "wp_cache_replace_line: setting not changed - $new" );
			return true;
		} elseif ( preg_match( "/$old/", $line ) ) {
			wp_cache_debug( 'wp_cache_replace_line: changing line ' . trim( $line ) . " to *$new*" );
			$found = true;
		}
	}

	$tmp_config_filename = tempnam( $GLOBALS['cache_path'], md5( mt_rand( 0, 9999 ) ) );
	if ( file_exists( $tmp_config_filename . '.php' ) ) {
		unlink( $tmp_config_filename . '.php' );
		if ( file_exists( $tmp_config_filename . '.php' ) ) {
			die( __( 'WARNING: attempt to intercept updating of config file.', 'wp-super-cache' ) );
		}
	}
	rename( $tmp_config_filename, $tmp_config_filename . '.php' );
	$tmp_config_filename .= '.php';
	wp_cache_debug( 'wp_cache_replace_line: writing to ' . $tmp_config_filename );
	$fd = fopen( $tmp_config_filename, 'w' );
	if ( ! $fd ) {
		if ( function_exists( 'set_transient' ) ) {
			set_transient( 'wpsc_config_error', 'config_file_ro', 10 );
		}
		trigger_error( "wp_cache_replace_line: Error  - could not write to $my_file" );
		return false;
	}
	if ( $found ) {
		foreach ( (array) $lines as $line ) {
			if ( ! preg_match( "/$old/", $line ) ) {
				fputs( $fd, $line );
			} elseif ( $new != '' ) {
				fputs( $fd, "$new\n" );
			}
		}
	} else {
		$done = false;
		foreach ( (array) $lines as $line ) {
			if ( $done || ! preg_match( '/^(if\ \(\ \!\ )?define|\$|\?>/', $line ) ) {
				fputs( $fd, $line );
			} else {
				fputs( $fd, "$new\n" );
				fputs( $fd, $line );
				$done = true;
			}
		}
	}
	fclose( $fd );
	rename( $tmp_config_filename, $my_file );
	wp_cache_debug( 'wp_cache_replace_line: moved ' . $tmp_config_filename . ' to ' . $my_file );

	if ( function_exists( 'opcache_invalidate' ) ) {
		@opcache_invalidate( $my_file );
	}

	return true;
}