PluginProbe ʕ •ᴥ•ʔ
Kubio AI Page Builder / 1.6.4
Kubio AI Page Builder v1.6.4
2.8.3 2.8.2 2.8.1 trunk 1.0.0 1.0.1 1.1.0 1.2.0 1.2.1 1.2.2 1.2.3 1.3.0 1.3.1 1.3.2 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.7.0 1.7.1 1.7.2 1.7.3 1.8.0 1.8.1 1.8.2 1.9.0 2.0.0 2.1.1 2.1.2 2.1.3 2.2.0 2.2.3 2.2.4 2.2.5 2.3.0 2.3.1 2.3.3 2.3.4 2.4.0 2.4.1 2.4.2 2.4.3 2.4.5 2.5.0 2.5.1 2.5.2 2.5.3 2.6.0 2.6.1 2.6.2 2.6.3 2.6.5 2.6.6 2.6.7 2.7.0 2.7.1 2.7.2 2.7.3 2.8.0
kubio / lib / src / Core / Backup.php
kubio / lib / src / Core Last commit date
Background 3 years ago Blocks 3 years ago GlobalElements 4 years ago Layout 4 years ago License 4 years ago Separators 4 years ago StyleManager 3 years ago Styles 4 years ago Activation.php 3 years ago Backup.php 4 years ago CustomizerImporter.php 3 years ago Deactivation.php 4 years ago EditInKubioCustomizerPanel.php 4 years ago Element.php 4 years ago ElementBase.php 4 years ago Importer.php 3 years ago InnerBlocks.php 4 years ago LodashBasic.php 4 years ago Registry.php 4 years ago Utils.php 3 years ago
Backup.php
194 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 ( ! mkdir( dirname( $file_path ), 0777, true ) ) {
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
149 private function getBackupData( $identifier ) {
150
151 if ( $this->loaded_backup !== null ) {
152 return $this->loaded_backup;
153 }
154
155 $data = $this->maybeGetContentFromFile( $identifier );
156 if ( ! $data || is_wp_error( $data ) ) {
157 $data = get_option( $this->getBackupKey( $identifier ), array() );
158 }
159
160 $this->loaded_backup = $data;
161
162 return $this->loaded_backup;
163 }
164
165 public function deleteBackup( $identifier ) {
166 delete_option( $this->getBackupKey( $identifier ) );
167 unlink( $this->getBackupFilePath( $identifier ) );
168 }
169
170
171 public function restoreBackup( $identifier ) {
172
173 $data = $this->getBackupData( $identifier );
174
175 $global_data = Arr::get( $data, 'global', null );
176
177 $status_templates = $this->putBlockTemplatesData( Arr::get( $data, 'templates', array() ) );
178 $status_parts = $this->putBlockTemplatesData( Arr::get( $data, 'parts', array() ) );
179 $status_global = true;
180
181 if ( $global_data ) {
182 $status_global = kubio_replace_global_data_content( $global_data );
183 }
184
185 if ( is_wp_error( $status_templates ) || is_wp_error( $status_parts ) || is_wp_error( $status_global ) ) {
186 return new \WP_Error( 'wp_template_part' );
187 }
188
189 return true;
190 }
191
192
193 }
194