title: Simplify RecursiveIteratorIterator PHP function
body: <p>I have the following functions show below which deletes a WordPress site. It works. However, I feel like there is a more efficient and simpler way to do it, but I am not sure how.</p>
  <p>How can I simplify this to delete the /wp-admin, wp-content and wp-includes folders and their contents as well as the PHP files which reside in the root directory of the WordPress site without deleting any other folders?</p>
  <p>For example, a user may install another WordPress site in a sub-directory and when deleting a single WP site I don't want it to delete the files and folders of the site residing in the sub-directory.</p>
  <p>I'm a bit of a noob so if someone can point me in the right direction.</p>
  <pre><code>// remove wp-admin folder and it's contents
  $wpadmin = new RecursiveIteratorIterator(
      new RecursiveDirectoryIterator($directory . '/wp-admin', RecursiveDirectoryIterator::SKIP_DOTS),
      RecursiveIteratorIterator::CHILD_FIRST
  );
  foreach ($wpadmin as $fileinfo) {
      $todo = ($fileinfo-&gt;isDir() ? 'rmdir' : 'unlink');
      $todo($fileinfo-&gt;getRealPath());
  };
  rmdir($directory . '/wp-admin');
  
  // remove wp-content folder and it's contents
  $wpcontent = new RecursiveIteratorIterator(
      new RecursiveDirectoryIterator($directory . '/wp-content', RecursiveDirectoryIterator::SKIP_DOTS),
      RecursiveIteratorIterator::CHILD_FIRST
  );
  foreach ($wpcontent as $fileinfo) {
      $todo = ($fileinfo-&gt;isDir() ? 'rmdir' : 'unlink');
      $todo($fileinfo-&gt;getRealPath());
  };
  rmdir($directory . '/wp-content');
  
  // remove wp-includes folder and it's contents
  $wpincludes = new RecursiveIteratorIterator(
      new RecursiveDirectoryIterator($directory . '/wp-includes', RecursiveDirectoryIterator::SKIP_DOTS),
      RecursiveIteratorIterator::CHILD_FIRST
  );
  foreach ($wpincludes as $fileinfo) {
      $todo = ($fileinfo-&gt;isDir() ? 'rmdir' : 'unlink');
      $todo($fileinfo-&gt;getRealPath());
  };
  rmdir($directory . '/wp-includes');
  
  // remove PHP files in WP root folder
  $wpfiles = new RecursiveIteratorIterator(
      new RecursiveDirectoryIterator($directory, RecursiveDirectoryIterator::SKIP_DOTS),
      RecursiveIteratorIterator::LEAVES_ONLY
  );
  foreach ($wpfiles as $fileinfo) {
      unlink($fileinfo-&gt;getRealPath());
  }
  </code></pre>