Background
1 year ago
Blocks
1 year ago
GlobalElements
1 year ago
Layout
1 year ago
License
1 year ago
Separators
11 months ago
StyleManager
1 month ago
Styles
1 year ago
Activation.php
1 year ago
Backup.php
1 year ago
CustomizerImporter.php
11 months ago
Deactivation.php
1 year ago
EditInKubioCustomizerPanel.php
1 year ago
Element.php
1 year ago
ElementBase.php
4 years ago
Importer.php
1 month ago
InnerBlocks.php
1 year ago
KubioFrontPageRevertNotice.php
9 months ago
LodashBasic.php
1 year ago
Registry.php
1 year ago
ThirdPartyPluginAssetLoaderInEditor.php
3 months ago
Utils.php
1 month ago
Backup.php
191 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Kubio\Core; |
| 4 | |
| 5 | use IlluminateAgnostic\Arr\Support\Arr; |
| 6 | |
| 7 | class Backup { |
| 8 | |
| 9 | const UNIQUE_KEY_IDENTIFIER = 'KUBIO__UNIQUE_KEY_IDENTIFIER'; |
| 10 | private $option_key_base = '_kubio_bkp'; |
| 11 | private $unique_key; |
| 12 | private $loaded_backup = null; |
| 13 | |
| 14 | |
| 15 | |
| 16 | public function __construct() { |
| 17 | $this->unique_key = get_option( $this->getBackupKey( Backup::UNIQUE_KEY_IDENTIFIER ) ); |
| 18 | if ( ! $this->unique_key ) { |
| 19 | $this->unique_key = uniqid(); |
| 20 | update_option( $this->getBackupKey( Backup::UNIQUE_KEY_IDENTIFIER ), $this->unique_key, false ); |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | private function getBackupKey( $key = null ) { |
| 25 | |
| 26 | if ( Backup::UNIQUE_KEY_IDENTIFIER === $key ) { |
| 27 | return '_kubio_backup_unique_key_id'; |
| 28 | } |
| 29 | |
| 30 | $template = get_stylesheet(); |
| 31 | $key = $key ? "_{$key}" : ''; |
| 32 | return "{$this->option_key_base}_{$this->unique_key}_$template"; |
| 33 | } |
| 34 | |
| 35 | |
| 36 | public function hasBackup( $identifier ) { |
| 37 | |
| 38 | return ! ! $this->getBackupData( $identifier ); |
| 39 | } |
| 40 | |
| 41 | public function backupSiteStructureAndStyle( $identifier ) { |
| 42 | |
| 43 | $backup = array( |
| 44 | 'templates' => $this->getBlockTemplatesData( 'wp_template' ), |
| 45 | 'parts' => $this->getBlockTemplatesData( 'wp_template_part' ), |
| 46 | 'global' => kubio_get_global_data_content(), |
| 47 | ); |
| 48 | |
| 49 | try { |
| 50 | $written_to_file = $this->maybeBackupFile( $identifier, $backup ); |
| 51 | $updated = update_option( $this->getBackupKey( $identifier ), $backup, false ); |
| 52 | |
| 53 | if ( ! ( $written_to_file || $updated ) ) { |
| 54 | return new \WP_Error( 'kubio_backup_error' ); |
| 55 | } |
| 56 | } catch ( \Exception $e ) { |
| 57 | return new \WP_Error( 'kubio_backup_error', $e->getMessage() ); |
| 58 | } |
| 59 | |
| 60 | return true; |
| 61 | } |
| 62 | |
| 63 | private function getBlockTemplatesData( $post_type = 'wp_template' ) { |
| 64 | $entities = get_block_templates( array(), $post_type ); |
| 65 | $data = array(); |
| 66 | |
| 67 | foreach ( $entities as $entity ) { |
| 68 | $data[ $entity->slug ] = array( |
| 69 | 'content' => $entity->content, |
| 70 | 'source' => get_post_meta( $entity->wp_id, '_kubio_template_source', true ), |
| 71 | 'type' => $post_type, |
| 72 | ); |
| 73 | } |
| 74 | |
| 75 | return $data; |
| 76 | } |
| 77 | |
| 78 | |
| 79 | private function putBlockTemplatesData( $entities ) { |
| 80 | foreach ( $entities as $slug => $entity ) { |
| 81 | |
| 82 | $status = true; |
| 83 | |
| 84 | if ( $entity['type'] === 'wp_template' ) { |
| 85 | $status = Importer::createTemplate( $slug, $entity['content'], true, $entity['source'] ); |
| 86 | |
| 87 | } |
| 88 | if ( $entity['type'] === 'wp_template_part' ) { |
| 89 | $status = Importer::createTemplatePart( $slug, $entity['content'], true, $entity['source'] ); |
| 90 | |
| 91 | } |
| 92 | |
| 93 | if ( is_wp_error( $status ) ) { |
| 94 | return new \WP_Error( 'kubio_backup_error' ); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | return true; |
| 99 | } |
| 100 | |
| 101 | |
| 102 | private function getBackupFolderRoot() { |
| 103 | $upload_dir = wp_upload_dir(); |
| 104 | $upload_path = untrailingslashit( $upload_dir['basedir'] ); |
| 105 | |
| 106 | return "{$upload_path}/kubio-site-backups"; |
| 107 | } |
| 108 | |
| 109 | private function getBackupFilePath( $identifier ) { |
| 110 | $theme = get_stylesheet(); |
| 111 | $upload_path = $this->getBackupFolderRoot(); |
| 112 | $file_path = "{$upload_path}/{$theme}/{$identifier }.ksb"; |
| 113 | return $file_path; |
| 114 | } |
| 115 | |
| 116 | private function maybeBackupFile( $identifier, $content ) { |
| 117 | |
| 118 | $file_path = $this->getBackupFilePath( $identifier ); |
| 119 | if ( ! file_exists( dirname( $file_path ) ) ) { |
| 120 | if ( ! wp_mkdir_p( dirname( $file_path ) ) ) { |
| 121 | return false; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | $htaccess_file = array( $this->getBackupFolderRoot() . '/.htaccess', dirname( $file_path ) . '/.htaccess' ); |
| 126 | |
| 127 | foreach ( $htaccess_file as $htaccess_file ) { |
| 128 | $htaccess_file = wp_normalize_path( $htaccess_file ); |
| 129 | if ( ! file_exists( $htaccess_file ) ) { |
| 130 | file_put_contents( $htaccess_file, "Deny from all\n" ); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | return file_put_contents( $file_path, serialize( $content ) ) !== false; |
| 135 | } |
| 136 | |
| 137 | private function maybeGetContentFromFile( $identifier ) { |
| 138 | |
| 139 | $file_path = $this->getBackupFilePath( $identifier ); |
| 140 | |
| 141 | if ( ! file_exists( $file_path ) || ! is_readable( $file_path ) ) { |
| 142 | return new \WP_Error( 'kubio_backup_error' ); |
| 143 | } |
| 144 | |
| 145 | return unserialize( file_get_contents( $file_path ) ); |
| 146 | } |
| 147 | |
| 148 | private function getBackupData( $identifier ) { |
| 149 | |
| 150 | if ( $this->loaded_backup !== null ) { |
| 151 | return $this->loaded_backup; |
| 152 | } |
| 153 | |
| 154 | $data = $this->maybeGetContentFromFile( $identifier ); |
| 155 | if ( ! $data || is_wp_error( $data ) ) { |
| 156 | $data = get_option( $this->getBackupKey( $identifier ), array() ); |
| 157 | } |
| 158 | |
| 159 | $this->loaded_backup = $data; |
| 160 | |
| 161 | return $this->loaded_backup; |
| 162 | } |
| 163 | |
| 164 | public function deleteBackup( $identifier ) { |
| 165 | delete_option( $this->getBackupKey( $identifier ) ); |
| 166 | wp_delete_file( $this->getBackupFilePath( $identifier ) ); |
| 167 | } |
| 168 | |
| 169 | |
| 170 | public function restoreBackup( $identifier ) { |
| 171 | |
| 172 | $data = $this->getBackupData( $identifier ); |
| 173 | |
| 174 | $global_data = Arr::get( $data, 'global', null ); |
| 175 | |
| 176 | $status_templates = $this->putBlockTemplatesData( Arr::get( $data, 'templates', array() ) ); |
| 177 | $status_parts = $this->putBlockTemplatesData( Arr::get( $data, 'parts', array() ) ); |
| 178 | $status_global = true; |
| 179 | |
| 180 | if ( $global_data ) { |
| 181 | $status_global = kubio_replace_global_data_content( $global_data ); |
| 182 | } |
| 183 | |
| 184 | if ( is_wp_error( $status_templates ) || is_wp_error( $status_parts ) || is_wp_error( $status_global ) ) { |
| 185 | return new \WP_Error( 'wp_template_part' ); |
| 186 | } |
| 187 | |
| 188 | return true; |
| 189 | } |
| 190 | } |
| 191 |