<?php
/**
 *
 */
class BackWPup_JobType_WPPlugin extends BackWPup_JobTypes {

	/**
	 *
	 */
	public function __construct() {

		$this->info[ 'ID' ]          = 'WPPLUGIN';
		$this->info[ 'name' ]        = __( 'Plugins', 'backwpup' );
		$this->info[ 'description' ] = __( 'Installed plugins list', 'backwpup' );
		$this->info[ 'URI' ]         = translate( BackWPup::get_plugin_data( 'PluginURI' ), 'backwpup' );
		$this->info[ 'author' ]      = BackWPup::get_plugin_data( 'Author' );
		$this->info[ 'authorURI' ]   = translate( BackWPup::get_plugin_data( 'AuthorURI' ), 'backwpup' );
		$this->info[ 'version' ]     = BackWPup::get_plugin_data( 'Version' );

	}

	/**
	 * @return bool
	 */
	public function creates_file() {

		return TRUE;
	}

	/**
	 * @return array
	 */
	public function option_defaults() {
		return array( 'pluginlistfilecompression' => '', 'pluginlistfile' => sanitize_title_with_dashes( get_bloginfo( 'name' ) ) . '.pluginlist.%Y-%m-%d' );
	}

	/**
	 * @param $jobid
	 * @return void
	 */
	public function edit_tab( $jobid ) {
		?>
		<table class="form-table">
			<tr valign="top">
				<th scope="row"><label for="idpluginlistfile"><?php _e( 'Plugin list file name', 'backwpup' ) ?></label></th>
				<td>
					<input name="pluginlistfile" type="text" id="idpluginlistfile"
						   value="<?php echo BackWPup_Option::get( $jobid, 'pluginlistfile' );?>"
						   class="medium-text code"/>.txt
				</td>
			</tr>
			<tr valign="top">
				<th scope="row"><?php _e( 'File compression', 'backwpup' ) ?></th>
				<td>
					<?php
					echo '<label for="idpluginlistfilecompression"><input class="radio" type="radio"' . checked( '', BackWPup_Option::get( $jobid, 'pluginlistfilecompression' ), FALSE ) . ' name="pluginlistfilecompression"  id="idpluginlistfilecompression" value="" /> ' . __( 'none', 'backwpup' ). '</label><br />';
					if ( function_exists( 'gzopen' ) )
						echo '<label for="idpluginlistfilecompression-gz"><input class="radio" type="radio"' . checked( '.gz', BackWPup_Option::get( $jobid, 'pluginlistfilecompression' ), FALSE ) . ' name="pluginlistfilecompression" id="idpluginlistfilecompression-gz" value=".gz" /> ' . __( 'GZip', 'backwpup' ). '</label><br />';
					else
						echo '<label for="idpluginlistfilecompression-gz"><input class="radio" type="radio"' . checked( '.gz', BackWPup_Option::get( $jobid, 'pluginlistfilecompression' ), FALSE ) . ' name="pluginlistfilecompression" id="idpluginlistfilecompression-gz" value=".gz" disabled="disabled" /> ' . __( 'GZip', 'backwpup' ). '</label><br />';
					if ( function_exists( 'bzopen' ) )
						echo '<label for="idpluginlistfilecompression-bz2"><input class="radio" type="radio"' . checked( '.bz2', BackWPup_Option::get( $jobid, 'pluginlistfilecompression' ), FALSE ) . ' name="pluginlistfilecompression" id="idpluginlistfilecompression-bz2" value=".bz2" /> ' . __( 'BZip2', 'backwpup' ). '</label><br />';
					else
						echo '<label for="idpluginlistfilecompression-bz2"><input class="radio" type="radio"' . checked( '.bz2', BackWPup_Option::get( $jobid, 'pluginlistfilecompression' ), FALSE ) . ' name="pluginlistfilecompression" id="idpluginlistfilecompression-bz2" value=".bz2" disabled="disabled" /> ' . __( 'BZip2', 'backwpup' ). '</label><br />';
					?>
				</td>
			</tr>
		</table>
	<?php
	}


	/**
	 * @param $id
	 */
	public function edit_form_post_save( $id ) {

		BackWPup_Option::update( $id, 'pluginlistfile', $_POST[ 'pluginlistfile' ] );
		if ( $_POST[ 'pluginlistfilecompression' ] == '' || $_POST[ 'pluginlistfilecompression' ] == '.gz' || $_POST[ 'pluginlistfilecompression' ] == '.bz2' )
			BackWPup_Option::update( $id, 'pluginlistfilecompression', $_POST[ 'pluginlistfilecompression' ] );
	}

	/**
	 * @param $job_object
	 * @return bool
	 */
	public function job_run( $job_object ) {

		$job_object->substeps_todo = 1;

		trigger_error( sprintf( __( '%d. Trying to generate a file with installed plugin names&#160;&hellip;', 'backwpup' ), $job_object->steps_data[ $job_object->step_working ][ 'STEP_TRY' ] ), E_USER_NOTICE );
		//build filename
		if ( empty( $job_object->temp[ 'pluginlistfile' ] ) )
			$job_object->temp[ 'pluginlistfile' ] = $job_object->generate_filename( $job_object->job[ 'pluginlistfile' ], 'txt' ) . $job_object->job[ 'pluginlistfilecompression' ];

		if ( $job_object->job[ 'pluginlistfilecompression' ] == '.gz' )
			$handle = fopen( 'compress.zlib://' . BackWPup::get_plugin_data( 'TEMP' ) . $job_object->temp[ 'pluginlistfile' ], 'w' );
		elseif ( $job_object->job[ 'pluginlistfilecompression' ] == '.bz2' )
			$handle = fopen( 'compress.bzip2://' . BackWPup::get_plugin_data( 'TEMP' ) . $job_object->temp[ 'pluginlistfile' ], 'w' );
		else
			$handle = fopen( BackWPup::get_plugin_data( 'TEMP' ) . $job_object->temp[ 'pluginlistfile' ], 'w' );

		//open file
		$header = "------------------------------------------------------------" . PHP_EOL;
		$header .= "  Plugin list generated with BackWPup version: " . BackWPup::get_plugin_data( 'Version' ) . PHP_EOL;
		$header .= "  " . translate( BackWPup::get_plugin_data( 'pluginuri' ), 'backwpup' ) . PHP_EOL;
		$header .= "  Blog Name: " . get_bloginfo( 'name' ) . PHP_EOL;
		$header .= "  Blog URL: " . get_bloginfo( 'url' ) . PHP_EOL;
		$header .= "  Generated on: " . date_i18n( 'Y-m-d H:i.s' ) . PHP_EOL;
		$header .= "------------------------------------------------------------" . PHP_EOL . PHP_EOL;
		fwrite( $handle, $header );
		//get Plugins
		if ( ! function_exists( 'get_plugins' ) )
			require_once ABSPATH . 'wp-admin/includes/plugin.php';
		$plugins        = get_plugins();
		$plugins_active = get_option( 'active_plugins' );
		//write it to file
		fwrite( $handle, PHP_EOL . __( 'All plugin information:', 'backwpup' ) . PHP_EOL . '------------------------------' . PHP_EOL );
		foreach ( $plugins as $plugin ) {
			fwrite( $handle, $plugin[ 'Name' ] . ' (v.' . $plugin[ 'Version' ] . ') ' . html_entity_decode( sprintf( __( 'from %s', 'backwpup' ), $plugin[ 'Author' ] ), ENT_QUOTES ) . PHP_EOL . "\t" . $plugin[ 'PluginURI' ] . PHP_EOL );
		}
		fwrite( $handle, PHP_EOL . __( 'Active plugins:', 'backwpup' ) . PHP_EOL . '------------------------------' . PHP_EOL );

		foreach ( $plugins as $key => $plugin ) {
			if ( in_array( $key, $plugins_active ) )
				fwrite( $handle, $plugin[ 'Name' ] . PHP_EOL );
		}
		fwrite( $handle, PHP_EOL . __( 'Inactive plugins:', 'backwpup' ) . PHP_EOL . '------------------------------' . PHP_EOL );
		foreach ( $plugins as $key => $plugin ) {
			if ( ! in_array( $key, $plugins_active ) )
				fwrite( $handle, $plugin[ 'Name' ] . PHP_EOL );
		}
		fclose( $handle );

		//add file to backup files
		if ( is_readable( BackWPup::get_plugin_data( 'TEMP' ) . $job_object->temp[ 'pluginlistfile' ] ) ) {
			$job_object->additional_files_to_backup[ ] = BackWPup::get_plugin_data( 'TEMP' ) . $job_object->temp[ 'pluginlistfile' ];
			$job_object->count_files ++;
			$job_object->count_filesize = $job_object->count_filesize + @filesize( BackWPup::get_plugin_data( 'TEMP' ) . $job_object->temp[ 'pluginlistfile' ] );
			trigger_error( sprintf( __( 'Added plugin list file "%1$s" with %2$s to backup file list.', 'backwpup' ), $job_object->temp[ 'pluginlistfile' ], size_format( filesize( BackWPup::get_plugin_data( 'TEMP' ) . $job_object->temp[ 'pluginlistfile' ] ), 2 ) ), E_USER_NOTICE );
		}
		$job_object->substeps_done = 1;

		return TRUE;
	}
}
