bp_forums_bbpress_write

The BuddyPress bp forums bbpress write function.

Description

bp_forums_bbpress_write( $file_source, $file_target, $alterations ); 

Parameters (3)

0. $file_source
The file source.
1. $file_target
The file target.
2. $alterations
The alterations.

Usage

  1. if ( !function_exists( 'bp_forums_bbpress_write' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-forums/deprecated/1.7.php'; 
  3.  
  4. // The file source. 
  5. $file_source = null; 
  6.  
  7. // The file target. 
  8. $file_target = null; 
  9.  
  10. // The alterations. 
  11. $alterations = null; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = bp_forums_bbpress_write($file_source, $file_target, $alterations); 
  15.  

Defined (1)

The function is defined in the following location(s).

/bp-forums/deprecated/1.7.php  
  1. function bp_forums_bbpress_write( $file_source, $file_target, $alterations ) { 
  2.  
  3. if ( empty( $file_source ) || !file_exists( $file_source ) || !is_file( $file_source ) ) { 
  4. return -1; 
  5.  
  6. if ( empty( $file_target ) ) { 
  7. $file_target = $file_source; 
  8.  
  9. if ( empty( $alterations ) || !is_array( $alterations ) ) { 
  10. return -2; 
  11.  
  12. // Get the existing lines in the file. 
  13. $lines = file( $file_source ); 
  14.  
  15. // Initialise an array to store the modified lines. 
  16. $modified_lines = array(); 
  17.  
  18. // Loop through the lines and modify them. 
  19. foreach ( (array) $lines as $line ) { 
  20. if ( isset( $alterations[substr( $line, 0, 20 )] ) ) { 
  21. $alteration = $alterations[substr( $line, 0, 20 )]; 
  22. $modified_lines[] = str_replace( $alteration[0], $alteration[1], $line ); 
  23. } else { 
  24. $modified_lines[] = $line; 
  25.  
  26. $writable = true; 
  27. if ( file_exists( $file_target ) ) { 
  28. if ( !is_writable( $file_target ) ) { 
  29. $writable = false; 
  30. } else { 
  31. $dir_target = dirname( $file_target ); 
  32.  
  33. if ( file_exists( $dir_target ) ) { 
  34. if ( !is_writable( $dir_target ) || !is_dir( $dir_target ) ) { 
  35. $writable = false; 
  36. } else { 
  37. $writable = false; 
  38.  
  39. if ( empty( $writable ) ) { 
  40. return trim( join( null, $modified_lines ) ); 
  41.  
  42. // Open the file for writing - rewrites the whole file. 
  43. $file_handle = fopen( $file_target, 'w' ); 
  44.  
  45. // Write lines one by one to avoid OS specific newline hassles. 
  46. foreach ( (array) $modified_lines as $modified_line ) { 
  47. if ( strlen( $modified_line ) - 2 === strrpos( $modified_line, '?>' ) ) { 
  48. $modified_line = '?>'; 
  49.  
  50. fwrite( $file_handle, $modified_line ); 
  51. if ( $modified_line == '?>' ) { 
  52. break; 
  53.  
  54. // Close the config file. 
  55. fclose( $file_handle ); 
  56.  
  57. @chmod( $file_target, 0666 ); 
  58.  
  59. return 1;