/**
  * Writes $data to the csv file open in $filehandle. uses the array indices of $data for column headers
  *
  * @param string 	$filepath
  * @param array 	$data 2D array, 		first numerically-indexed,
  *                    						and next-level-down preferably indexed by string
  * @param boolean 	$write_column_headers 	whether or not we should add the keys in the bottom-most array
  * 											as a row for headers in the CSV.
  *                                            Eg, if $data looked like:
  *                                            array(
  *                                              	0=>array('EVT_ID'=>1,'EVT_name'=>'monkey'...),
  * 													1=>array(...,...)
  *                                            )
  *
  * @return boolean 		if we successfully wrote to the CSV or not. If there's no $data,
  * 						we consider that a success (because we wrote everything there was...nothing)
  * @throws EE_Error
  */
 public static function write_data_array_to_csv($filepath, $data, $write_column_headers = true)
 {
     $new_file_contents = '';
     //determine if $data is actually a 2d array
     if ($data && is_array($data) && is_array(EEH_Array::get_one_item_from_array($data))) {
         //make sure top level is numerically indexed,
         if (EEH_Array::is_associative_array($data)) {
             throw new EE_Error(sprintf(__("top-level array must be numerically indexed. Does these look like numbers to you? %s", "event_espresso"), implode(",", array_keys($data))));
         }
         $item_in_top_level_array = EEH_Array::get_one_item_from_array($data);
         //now, is the last item in the top-level array of $data an associative or numeric array?
         if ($write_column_headers && EEH_Array::is_associative_array($item_in_top_level_array)) {
             //its associative, so we want to output its keys as column headers
             $keys = array_keys($item_in_top_level_array);
             $new_file_contents .= EEH_Export::get_csv_row($keys);
         }
         //start writing data
         foreach ($data as $data_row) {
             $new_file_contents .= EEH_Export::get_csv_row($data_row);
         }
         return EEH_File::write_to_file($filepath, EEH_File::get_file_contents($filepath) . $new_file_contents);
     } else {
         //no data TO write... so we can assume that's a success
         return true;
     }
 }
 public function test_write_to_file()
 {
     global $wp_filesystem;
     $wp_filesystem->chmod('/', '755');
     $file_path = '/test.txt';
     $content1 = '<html><body>hello</body></html>';
     $content2 = '<xml><monkeys><monkey/></monkeys></xml>';
     $this->assertFalse($wp_filesystem->exists($file_path));
     $wp_filesystem->put_contents($file_path, $content1, '644');
     $this->assertTrue($wp_filesystem->exists($file_path));
     $this->assertEquals($content1, EEH_File::get_file_contents($file_path));
     //now add to it
     EEH_File::write_to_file($file_path, $content2);
     $this->assertEquals($content2, EEH_File::get_file_contents($file_path));
 }
 /**
  *	write exception details to log file
  *
  *	@access public
  *	@ param timestamp $time
  *	@ param object $ex
  *	@ return void
  */
 public function write_to_error_log($time = FALSE, $ex = FALSE, $clear = FALSE)
 {
     if (!$ex) {
         return;
     }
     if (!$time) {
         $time = time();
     }
     $exception_log = '----------------------------------------------------------------------------------------' . PHP_EOL;
     $exception_log .= '[' . date('Y-m-d H:i:s', $time) . ']  Exception Details' . PHP_EOL;
     $exception_log .= 'Message: ' . $ex['msg'] . PHP_EOL;
     $exception_log .= 'Code: ' . $ex['code'] . PHP_EOL;
     $exception_log .= 'File: ' . $ex['file'] . PHP_EOL;
     $exception_log .= 'Line No: ' . $ex['line'] . PHP_EOL;
     $exception_log .= 'Stack trace: ' . PHP_EOL;
     $exception_log .= $ex['string'] . PHP_EOL;
     $exception_log .= '----------------------------------------------------------------------------------------' . PHP_EOL;
     EE_Registry::instance()->load_helper('File');
     try {
         EEH_File::ensure_file_exists_and_is_writable(EVENT_ESPRESSO_UPLOAD_DIR . 'logs' . DS . self::$_exception_log_file);
         EEH_File::add_htaccess_deny_from_all(EVENT_ESPRESSO_UPLOAD_DIR . 'logs');
         if (!$clear) {
             //get existing log file and append new log info
             $exception_log = EEH_File::get_file_contents(EVENT_ESPRESSO_UPLOAD_DIR . 'logs' . DS . self::$_exception_log_file) . $exception_log;
         }
         EEH_File::write_to_file(EVENT_ESPRESSO_UPLOAD_DIR . 'logs' . DS . self::$_exception_log_file, $exception_log);
     } catch (EE_Error $e) {
         EE_Error::add_error(sprintf(__('Event Espresso error logging could not be setup because: %s', 'event_espresso'), $e->getMessage()));
         return;
     }
 }
 /**
  * write_log
  * appends the results of the 'AHEE_log' filter to the espresso log file
  */
 public function write_log()
 {
     try {
         //get existing log file and append new log info
         $this->_log = EEH_File::get_file_contents($this->_logs_folder . $this->_log_file) . $this->_log;
         EEH_File::write_to_file($this->_logs_folder . $this->_log_file, $this->_log, 'Event Espresso Log');
     } catch (EE_Error $e) {
         EE_Error::add_error(sprintf(__('Could not write to the Event Espresso log file because: %s', 'event_espresso'), ' &nbsp; &nbsp; ' . $e->getMessage()), __FILE__, __FUNCTION__, __LINE__);
         return;
     }
 }