Ejemplo n.º 1
0
 /**
  * write_file
  * @param string $full_file_path
  * @param string $file_contents - the content to be written to the file
  * @param string $file_type
  * @throws EE_Error
  * @return bool
  */
 public static function write_to_file($full_file_path = '', $file_contents = '', $file_type = '')
 {
     $full_file_path = EEH_File::standardise_directory_separators($full_file_path);
     $file_type = !empty($file_type) ? rtrim($file_type, ' ') . ' ' : '';
     $folder = EEH_File::remove_filename_from_filepath($full_file_path);
     if (!EEH_File::verify_is_writable($folder, 'folder')) {
         if (defined('WP_DEBUG') && WP_DEBUG) {
             $msg = sprintf(__('The %1$sfile located at "%2$s" is not writable.', 'event_espresso'), $file_type, $full_file_path);
             $msg .= EEH_File::_permissions_error_for_unreadable_filepath($full_file_path);
             throw new EE_Error($msg);
         }
         return FALSE;
     }
     // load WP_Filesystem and set file permissions
     $wp_filesystem = EEH_File::_get_wp_filesystem();
     // write the file
     if (!$wp_filesystem->put_contents($full_file_path, $file_contents)) {
         if (defined('WP_DEBUG') && WP_DEBUG) {
             $msg = sprintf(__('The %1$sfile located at "%2$s" could not be written to.', 'event_espresso'), $file_type, $full_file_path);
             $msg .= EEH_File::_permissions_error_for_unreadable_filepath($full_file_path, 'f');
             throw new EE_Error($msg);
         }
         return FALSE;
     }
     return TRUE;
 }
 public function test_ensure_file_exists_and_is_writable()
 {
     global $wp_filesystem;
     $file_path = '/test.txt';
     // Test creation/exists checks
     $this->assertFalse($wp_filesystem->exists($file_path));
     $this->assertTrue(EEH_File::ensure_file_exists_and_is_writable($file_path));
     $this->assertTrue(EEH_File::verify_is_writable($file_path));
     $wp_filesystem->chmod($file_path, '000');
     try {
         $this->assertFalse(EEH_File::ensure_file_exists_and_is_writable($file_path));
         $this->fail(sprintf(__('An exception SHOULD have been thrown but wasn\'t', 'event_espresso')));
     } catch (EE_Error $e) {
         $this->assertTrue(TRUE);
     }
     try {
         $this->assertFalse(EEH_File::verify_is_writable($file_path));
         $this->fail(sprintf(__('An exception SHOULD have been thrown but wasn\'t', 'event_espresso')));
     } catch (EE_Error $e) {
         $this->assertTrue(TRUE);
     }
 }
Ejemplo n.º 3
0
 /**
  * Copies a file. Mostly a wrapper of WP_Filesystem::copy
  * @param string $source_file
  * @param string $destination_file
  * @param boolean $overwrite
  * @throws EE_Error if filesystem credentials are required
  * @return boolean success
  */
 public static function copy($source_file, $destination_file, $overwrite = FALSE)
 {
     $full_source_path = EEH_File::standardise_directory_separators($source_file);
     if (!EEH_File::exists($full_source_path)) {
         if (defined('WP_DEBUG') && WP_DEBUG) {
             $msg = sprintf(__('The file located at "%2$s" is not readable or doesn\'t exist.', 'event_espresso'), $full_source_path);
             $msg .= EEH_File::_permissions_error_for_unreadable_filepath($full_source_path);
             throw new EE_Error($msg);
         }
         return FALSE;
     }
     $full_dest_path = EEH_File::standardise_directory_separators($destination_file);
     $folder = EEH_File::remove_filename_from_filepath($full_dest_path);
     if (!EEH_File::verify_is_writable($folder, 'folder')) {
         if (defined('WP_DEBUG') && WP_DEBUG) {
             $msg = sprintf(__('The file located at "%2$s" is not writable.', 'event_espresso'), $full_dest_path);
             $msg .= EEH_File::_permissions_error_for_unreadable_filepath($full_dest_path);
             throw new EE_Error($msg);
         }
         return FALSE;
     }
     // load WP_Filesystem and set file permissions
     $wp_filesystem = EEH_File::_get_wp_filesystem($destination_file);
     // write the file
     if (!$wp_filesystem->copy(EEH_File::convert_local_filepath_to_remote_filepath($full_source_path), EEH_File::convert_local_filepath_to_remote_filepath($full_dest_path), $overwrite)) {
         if (defined('WP_DEBUG') && WP_DEBUG) {
             $msg = sprintf(__('Attempted writing to file %1$s, but could not, probably because of permissions issues', 'event_espresso'), $full_source_path);
             $msg .= EEH_File::_permissions_error_for_unreadable_filepath($full_source_path, 'f');
             throw new EE_Error($msg);
         }
         return FALSE;
     }
     return TRUE;
 }