| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace HM\BackUpWordPress; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Symfony\Component\Filesystem\LockHandler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * Manages status and progress of a backup | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | class Backup_Status { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | 	 * The filename for the backup file we are the tracking status of | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | 	 * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | 	private $filename = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 	 * [$lock_handler description] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 	 * @var LockHandler | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 	private $lock_handler = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 	private $callback; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 	 * @param string $id The unique id for the backup job | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 	public function __construct( $id ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 		$this->id = (string) $id; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 		$this->cleanup_after_crash(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 	 * Start the tracking a backup process. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 	 * This creates a backup running file and issues a file lock. This prevents duplicate | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 	 * instances of this backup process running concurrently and allows us to detect if | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 	 * the PHP thread running the process is killed as that will clear the lock. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 	 * @param  string $backup_filename The filename for the backup file that we're tracking | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 	 * @param  string $status_message  The initial status for the backup process | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 	 * @return boolean                  Whether the backup process was success marked as started | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 	public function start( $backup_filename, $status_message ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 		$this->filename = $backup_filename; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 		// Clear any errors from previous backup runs | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 		Notices::get_instance()->clear_notice_context( 'backup_errors' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 		add_action( 'shutdown', array( $this, 'catch_fatals' ), 10 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 		$this->lock_handler = new LockHandler( basename( $this->get_status_filepath() ), Path::get_path() ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 		if ( ! $this->lock_handler->lock() || $this->get_status() ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 		    return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 		return $this->set_status( $status_message ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 	 * Mark a backup process as finished. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 	 * This removes the file lock and deletes the running file. | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 69 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  | 	public function finish() { | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  | 		if ( isset( $this->lock_handler ) && is_a( $this->lock_handler, 'LockHandler' ) ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  | 			$this->lock_handler->release(); | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  | 		// Delete the backup running file | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  | 		if ( file_exists( $this->get_status_filepath() ) ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  | 			return unlink( $this->get_status_filepath() ); | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  | 		return false; | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 	 * Check if the backup has been started by checking if the running file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 	 * exists. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 	 * @return boolean Whether the backup process has been started | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 	public function is_started() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 		return (bool) file_exists( $this->get_status_filepath() ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 	public function is_running() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 		if ( ! $this->is_started() ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 			return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 		// If we're in the same thread then we know we must be running if the running file exists | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 		if ( is_a( $this->lock_handler, 'LockHandler' ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 			return $this->is_started(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 		$lock_handler = new LockHandler( basename( $this->get_status_filepath() ), Path::get_path() ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 		return ! $lock_handler->lock(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 	 * If the running file exists but isn't locked then the thread that | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 	 * the backup process is running in must have been killed. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 	 * You should only be running this command from a separate thread | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 	 * @return boolean Whether the backup process has crashed or not | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 	public function has_crashed() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 		return ( $this->is_started() && ! $this->is_running() ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | 	 * Handle a process that's previouly crashed. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | 	 * Delete the partially created backup if it exists and then run the standard | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  | 	 * cleanup tasks and set an error message for the user. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  | 	 * @return bool Whether the crash was handled or not | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  | 	public function cleanup_after_crash() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  | 		if ( ! $this->has_crashed() ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  | 			return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 136 |  | View Code Duplication | 		if ( file_exists( trailingslashit( Path::get_path() ) . $this->get_backup_filename() ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  | 			unlink( trailingslashit( Path::get_path() ) . $this->get_backup_filename() ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  | 		$this->finish(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  | 		$message = __( 'Your previous backup failed, the backup process was killed before it could complete. Please contact your host for assistance.', 'backupwordpress' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  | 		Notices::get_instance()->set_notices( 'backup_errors', array( $message ), true ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  | 		return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  | 	 * Catch fatal errors and react accordingly. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  | 	 * Hooked into the shutdown action. If we've shutdown because of a Fatal error | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  | 	 * then we cleanup and set an error message for the user. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  | 	public function catch_fatals() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  | 		$error = error_get_last(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  | 		if ( empty( $error ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  | 			return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  | 		if ( ! isset( $error['type'] ) || ! defined( 'E_ERROR' ) || E_ERROR !== $error['type'] ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  | 			return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 167 |  | View Code Duplication | 		if ( file_exists( trailingslashit( Path::get_path() ) . $this->get_backup_filename() ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  | 			unlink( trailingslashit( Path::get_path() ) . $this->get_backup_filename() ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  | 		$this->finish(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  | 		$message = sprintf( __( 'Your previous backup failed, the backup process encountered a %s before it could complete. The error was %s.', 'backupwordpress' ), '<code>PHP Fatal Error</code>', '<code>' . $error['message'] . '</code>' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  | 		Notices::get_instance()->set_notices( 'backup_errors', array( $message ), true ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  | 	 * Get the filepath for the backup file we're tracking | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  | 	 * @return string The path to the backup file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  | 	public function get_backup_filename() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  | 		if ( $this->is_started() ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  | 			$status = json_decode( file_get_contents( $this->get_status_filepath() ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  | 			if ( ! empty( $status->filename ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  | 				$this->filename = $status->filename; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  | 		return $this->filename; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  | 	 * Get the status of the running backup. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  | 	 * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  | 	public function get_status() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  | 		if ( ! file_exists( $this->get_status_filepath() ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  | 			return false; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  | 		$status = json_decode( file_get_contents( $this->get_status_filepath() ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 |  |  | 		if ( ! empty( $status->status ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 210 |  |  | 			return $status->status; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 212 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 213 |  |  | 		return false; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 214 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 215 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 216 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 217 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 218 |  |  | 	 * Set the status of the running backup | 
            
                                                                                                            
                            
            
                                    
            
            
                | 219 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 220 |  |  | 	 * @param string $message | 
            
                                                                                                            
                            
            
                                    
            
            
                | 221 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 222 |  |  | 	 * @return null | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 223 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 224 |  |  | 	public function set_status( $message ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 225 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 226 |  |  | 		if ( is_callable( $this->callback ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 227 |  |  | 			call_user_func( $this->callback, $message ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 228 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 229 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 230 |  |  | 		// If start hasn't been called yet then we wont' have a backup filename | 
            
                                                                                                            
                            
            
                                    
            
            
                | 231 |  |  | 		if ( ! $this->filename ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 232 |  |  | 			return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 233 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 234 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 235 |  |  | 		$status = json_encode( (object) array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 236 |  |  | 			'filename' => $this->filename, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 237 |  |  | 			'started'  => $this->get_start_time(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 238 |  |  | 			'status'   => $message, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 239 |  |  | 		) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 240 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 241 |  |  | 		return (bool) file_put_contents( $this->get_status_filepath(), $status ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 242 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 243 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 244 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 245 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 246 |  |  | 	 * Get the time that the current running backup was started | 
            
                                                                                                            
                            
            
                                    
            
            
                | 247 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 248 |  |  | 	 * @return int $timestamp | 
            
                                                                                                            
                            
            
                                    
            
            
                | 249 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 250 |  |  | 	public function get_start_time() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 251 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 252 |  |  | 		if ( ! file_exists( $this->get_status_filepath() ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 253 |  |  | 			return 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 254 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 255 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 256 |  |  | 		$status = json_decode( file_get_contents( $this->get_status_filepath() ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 257 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 258 |  |  | 		if ( ! empty( $status->started ) && (int) (string) $status->started === $status->started ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 259 |  |  | 			return $status->started; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 260 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 261 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 262 |  |  | 		return time(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 263 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 264 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 265 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 266 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 267 |  |  | 	 * Get the path to the backup running file that stores the running backup status | 
            
                                                                                                            
                            
            
                                    
            
            
                | 268 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 269 |  |  | 	 * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 270 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 271 |  |  | 	public function get_status_filepath() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 272 |  |  | 		return Path::get_path() . '/.backup-' . $this->id . '-running'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 273 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 274 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 275 |  |  | 	public function set_status_callback( $callback ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 276 |  |  | 		$this->callback = $callback; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 277 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 278 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 279 |  |  |  | 
            
                        
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: