theme = wp_get_theme()->get( 'TextDomain' ); $this->theme_dir = $wp_filesystem->wp_themes_dir() . get_template() . '/'; $this->mega_dir = $this->theme_dir . 'mega/'; $this->temp_dir = $wp_filesystem->wp_content_dir() . 'mega_temp/';//trailingslashit() } public function setCreds()//fs_connect { $this->wp_filesystem = false; } public function download( $url, &$logs ) { $temp = download_url( $url ); if ( is_wp_error( $temp )) { $logs->addLog( sprintf( esc_html__( 'Download failed: %s', 'mega' ), $temp->get_error_message( $temp->get_error_code() )), $temp->get_error_code() ); return false; } $logs->addLog( esc_html__( 'Downloading file ... Done!', 'mega' ), false, 'notice' ); return $temp; } public function unzip( $file, $to, &$logs ) { $unzip = unzip_file( realpath( $file ), $to ); if ( is_wp_error( $unzip )) { $logs->addLog( sprintf( esc_html__( 'Extract failed: %s', 'mega' ), $unzip->get_error_message( $unzip->get_error_code() )), $unzip->get_error_code() ); return false; } $logs->addLog( esc_html__( 'Extracting archive ... Done!', 'mega' ), false, 'notice' ); return true; } public function delete( $path, $include = array(), $exclude = array(), $empty = true )//if exclude not empty, run recurssion, include = delete only those { if ( is_file( $path )) { unlink( $path ); } else if ( is_dir( $path )) { if ( !array_filter( $exclude ) && !array_filter( $include )) { global $wp_filesystem; $wp_filesystem->delete( $path, true ); } else { $this->deleteFilter( $path, '', $include, $exclude, $empty ); } } } public function delTree( $dir ) { $files = array_diff( scandir( $dir ), array( '.', '..' )); foreach ( $files as $file ) { ( is_dir( "$dir/$file" )) ? delTree( "$dir/$file" ) : unlink( "$dir/$file" ); } return rmdir( $dir ); } public function deleteFilter( $path1, $rel = '', $include = array(), $exclude = array(), $empty = true ) { global $wp_filesystem; $dirlist = $wp_filesystem->dirlist( $path1 ); $path = trailingslashit( $path1 ); foreach ( (array) $dirlist as $filename => $fileinfo ) { $relpath = $rel . $filename; if ( $fileinfo['type'] === 'f' ) { if ( in_array( $relpath, $include )) $this->delete( $path . $filename ); } elseif ( $fileinfo['type'] === 'd' ) { $this->deleteFilter( $path . $filename, trailingslashit( $relpath ), $include, $exclude, $empty ); } } //print_r(array_diff(scandir($path), array('.', '..'))); if ( !empty( $rel ) && $empty && !array_filter( array_diff(scandir($path1), array('.', '..'))))//$wp_filesystem->dirlist( $path ))) { $this->delete( $path1 ); } } public function _copy( $from, $to, &$logs ) { $copy = copy_dir( $from, $to ); if ( is_wp_error( $copy )) { $logs->addLog( sprintf( esc_html__( 'Copy failed: %s', 'mega' ), $copy->get_error_message( $copy->get_error_code() )), $copy->get_error_code() ); return false; } $logs->addLog( esc_html__( 'Copying ... Done!', 'mega' ), false, 'notice' ); return true; } public function filter( $path, $exclude = array() ) { //if dir supplied, filter dir with files in array, if file supplied, filter it with array, false on failure } public function compare( $source, $target, &$logs, $list = false, $rel = '' )//collect { global $wp_filesystem; if ( !is_array( $source )) { $logs->addLog( esc_html__( 'Source should ba a valid array containing the compare data.', 'mega' )); return null; } //$this->list = array(); $r = true; foreach ( $source as $filename => $fileinfo ) { if ( $fileinfo['type'] === 'f' ) { if ( $list ) $this->list[] = $rel . $filename;//str_replace( $source, '', $path );//$path; if ( $r && !$this->isEqualFiles( array( 'size' => $fileinfo['size'], 'sha1' => $fileinfo['sha1'] ), $target . $rel . $filename )) //$fileinfo->getPathname() { $logs->addLog( esc_html__( 'Error: The files were modified.', 'mega' ), 'files_modified' ); $r = false; if ( !$r )//if ( !$list ) break; } } elseif ( $fileinfo['type'] === 'd' ) { $r = $this->compare( $source[$filename], $target, $logs, $list, $rel . $filename . '/' ); if ( !$r ) break; } } if ( $r && $rel === '' ) $logs->addLog( esc_html__( 'Checking for any changes within folder ... Ok!', 'mega' ), false, 'notice' ); return $r; } public function isEqualFiles( $a, $b ) { if ( !isset( $a ) || !is_array( $a ) || !isset( $a['size'] ) || !is_file( $b )) return false; if ( $a['size'] !== filesize( $b )) return false; if ( $a['sha1'] == sha1_file( $b )) return true; else return false; //http://fi2.php.net/manual/en/function.sha1.php filetime (last modified) } } //$mega_updater = new Mega_Updater(); class Mega_Updater { public function __construct() { global $mega_package; $this->cron_framework = wp_get_theme() . '_framework_update'; $this->cron_theme = wp_get_theme() . '_theme_update'; $this->cron = wp_get_theme() . '_update'; if ( $mega_package > 0 ) { if ( !wp_next_scheduled( $this->cron ) && !wp_next_scheduled( $this->cron_framework ) && !wp_next_scheduled( $this->cron_theme )) $this->cron( $this->cron ); add_action( $this->cron, array( $this, 'update' )); add_action( $this->cron_framework, array( $this, 'updateFramework' )); add_action( $this->cron_theme, array( $this, 'updateTheme' )); register_deactivation_hook( __FILE__, array( $this, 'disable' )); $this->disableWPUpdate(); } } public function disable() { $this->cronUnset( $this->cron ); $this->cronUnset( $this->cron_framework ); $this->cronUnset( $this->cron_theme ); } public function hook() { } public function disableWPUpdate() { } public function cron( $key, $freq = 'twicedaily', $offset = 0 ) { wp_clear_scheduled_hook( $key ); wp_schedule_event(( time() + $offset ), $freq, $key ); } public function cronUnset( $key ) { wp_clear_scheduled_hook( $key ); } public function updateFramework() { $this->update( 'framework' ); } public function updateTheme() { $this->update( 'theme' ); } public function update( $type = 'all' ) { //check if already required : require ABSPATH . 'wp-admin/includes/file.php';//move to file class @set_time_limit( 60 );//300 $logs = new Mega_Logss(); $file = new Mega_File(); if ( !$file ) { if ( !wp_next_scheduled( $this->cron_framework ) && !wp_next_scheduled( $this->cron_theme )) $this->cron( $this->cron, 'weekly' ); return false; } $api = new Mega_API_Connect( array( 'action' => 'update-check' )); $api->data = $api->connect(); if ( isset( $api->data['status'] ) && $api->data['status'] === 'error' ) { $logs->addLog( $api->data['code'] ); $this->postUpdate( $logs ); } else if ( $this->doCompare( $file, $api, $logs )) $this->postUpdate(); else $this->postUpdate( $logs );//failed supply last $logs->logs message, supply error codes to logs as well, do $log->logs get last > code } public function doCompare( $file, $api, &$logs, $type = 'all' ) { global $mega_package; $mega = $theme = false; if (( $type === 'all' || $type === 'framework' ) && version_compare( $api->data['download']['mega']['version'], MEGA_VER, $mega_package_files > 0 ? '>' : '>=' )) { $logs->addLog( esc_html__( 'Preparing to check any changes in mega framework ...', 'mega' ), false, 'notice' ); $args = array( 'ver' => array( 'local' => MEGA_VER, 'original' => $api->data['compare']['mega']['version'], ), 'local' => $file->mega_dir, 'original' => $mega_package_files > 0 ? $api->data['compare']['mega']['data']['master'] : $api->data['compare']['mega']['data']['zero'], 'copy_from' => $file->temp_dir . 'update/' . $file->theme . '/mega/', 'temp' => $file->temp_dir . 'update/', 'download' => $api->data['download']['mega']['file'] ); $mega = $this->processCompare( $args, $file, $api, $logs ); if ( !$mega ) $this->cron( $this->cron_framework, 'weekly' ); } if ( $type === 'all' || $type === 'theme' ) { $logs->addLog( esc_html__( 'Preparing to check any changes in theme folder ...', 'mega' ), false, 'notice' ); $args = array( 'ver' => array( 'local' => wp_get_theme()->get( 'Version' ), 'original' => $api->data['compare']['theme']['version'], ), 'local' => $file->theme_dir, 'original' => $mega_package_files > 0 ? $api->data['compare']['theme']['data']['master'] : $api->data['compare']['theme']['data']['zero'], 'copy_from' => $file->temp_dir . 'update/' . $file->theme . '/', 'temp' => $file->temp_dir . 'update/' ); if ( $mega && version_compare( $api->data['download']['mega']['version'], $api->data['download']['theme']['master']['min_framework'], '>=' )) { $args['download'] = $api->data['download']['theme']['master']['file']; } else if ( version_compare( MEGA_VER, $api->data['download']['theme']['compat']['min_framework'], '>=' )) { $args['download'] = $api->data['download']['theme']['compat']['file']; } $theme = $this->processCompare( $args, $file, $api, $logs ); if ( !$theme ) $this->cron( $this->cron_theme, 'weekly' ); } if ( !$mega || !$theme ) $this->cronUnset( $this->cron ); if ( $mega || $theme ) return true; return false; } public function processCompare( $args, $file, $api, &$logs )//doCompareFramework - doCompareTheme { $r = false; if ( $args['ver']['original'] >= $args['ver']['local'] ) { $logs->addLog( esc_html__( 'The compare version is lower or equal then what you have locally', 'mega' ), 'bad_compare' ); } else if ( $file->compare( $args['original'], $args['local'], $logs, true )) { if ( $this->doUpdate( $args, $file, $api, $logs )) { $logs->addLog( esc_html__( 'Updated successfully, you can exit now.', 'mega' ), false, 'notice' ); $r = true; } } return $r; } public function doUpdate( $args, $file, $api, &$logs ) { $r = false; $logs->addLog( esc_html__( 'Preparing to update ...', 'mega' ), false, 'notice' ); if ( $temp = $file->download( $args['download'], $logs )) { if ( $file->unzip( $temp, $args['temp'], $logs )) { $file->delete( $args['local'], $file->list ); if ( $file->_copy( $args['copy_from'], $args['local'], $logs )) { $logs->addLog( esc_html__( 'Updating ... Done!', 'mega' ), false, 'notice' ); $r = true; } } $file->delete( $temp ); } return $r; } public function postUpdate( $logs = '' ) { $api = new Mega_API_Connect( array( 'action' => 'post-update' ), $logs ); $api->connect(); } } class Mega_Logss { public $logs = false; public function addLog( $message, $code = false, $type = 'error' ) { $this->logs[] = array( 'code' => $code, 'time' => date( 'H:i', current_time( 'timestamp' )), 'message' => $message, 'type' => $type ); return key( $this->logs ); } }