insert_with_markers

Inserts an array of strings into a file (.htaccess ), placing it between BEGIN and END markers.

Description

insert_with_markers( (string) $filename, (string) $marker, (array|string) $insertion ); 

Replaces existing marked info. Retains surrounding data. Creates file if none exists.

Parameters (3)

0. $filename (string)
Filename to alter.
1. $marker (string)
The marker to alter.
2. $insertion (array|string)
The new content to insert.

Usage

  1. if ( !function_exists( 'insert_with_markers' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/misc.php'; 
  3.  
  4. // Filename to alter. 
  5. $filename = ''; 
  6.  
  7. // The marker to alter. 
  8. $marker = ''; 
  9.  
  10. // The new content to insert. 
  11. $insertion = null; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = insert_with_markers($filename, $marker, $insertion); 
  15.  

Defined (1)

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

/wp-admin/includes/misc.php  
  1. function insert_with_markers( $filename, $marker, $insertion ) { 
  2. if ( ! file_exists( $filename ) ) { 
  3. if ( ! is_writable( dirname( $filename ) ) ) { 
  4. return false; 
  5. if ( ! touch( $filename ) ) { 
  6. return false; 
  7. } elseif ( ! is_writeable( $filename ) ) { 
  8. return false; 
  9.  
  10. if ( ! is_array( $insertion ) ) { 
  11. $insertion = explode( "\n", $insertion ); 
  12.  
  13. $start_marker = "# BEGIN {$marker}"; 
  14. $end_marker = "# END {$marker}"; 
  15.  
  16. $fp = fopen( $filename, 'r+' ); 
  17. if ( ! $fp ) { 
  18. return false; 
  19.  
  20. // Attempt to get a lock. If the filesystem supports locking, this will block until the lock is acquired. 
  21. flock( $fp, LOCK_EX ); 
  22.  
  23. $lines = array(); 
  24. while ( ! feof( $fp ) ) { 
  25. $lines[] = rtrim( fgets( $fp ), "\r\n" ); 
  26.  
  27. // Split out the existing file into the preceding lines, and those that appear after the marker 
  28. $pre_lines = $post_lines = $existing_lines = array(); 
  29. $found_marker = $found_end_marker = false; 
  30. foreach ( $lines as $line ) { 
  31. if ( ! $found_marker && false !== strpos( $line, $start_marker ) ) { 
  32. $found_marker = true; 
  33. continue; 
  34. } elseif ( ! $found_end_marker && false !== strpos( $line, $end_marker ) ) { 
  35. $found_end_marker = true; 
  36. continue; 
  37. if ( ! $found_marker ) { 
  38. $pre_lines[] = $line; 
  39. } elseif ( $found_marker && $found_end_marker ) { 
  40. $post_lines[] = $line; 
  41. } else { 
  42. $existing_lines[] = $line; 
  43.  
  44. // Check to see if there was a change 
  45. if ( $existing_lines === $insertion ) { 
  46. flock( $fp, LOCK_UN ); 
  47. fclose( $fp ); 
  48.  
  49. return true; 
  50.  
  51. // Generate the new file data 
  52. $new_file_data = implode( "\n", array_merge( 
  53. $pre_lines,  
  54. array( $start_marker ),  
  55. $insertion,  
  56. array( $end_marker ),  
  57. $post_lines 
  58. ) ); 
  59.  
  60. // Write to the start of the file, and truncate it to that length 
  61. fseek( $fp, 0 ); 
  62. $bytes = fwrite( $fp, $new_file_data ); 
  63. if ( $bytes ) { 
  64. ftruncate( $fp, ftell( $fp ) ); 
  65. fflush( $fp ); 
  66. flock( $fp, LOCK_UN ); 
  67. fclose( $fp ); 
  68.  
  69. return (bool) $bytes;