PluginProbe
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More / trunk
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More vtrunk
trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.10 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 2.0.0 2.0.1 2.0.10 2.0.11 2.0.12 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 All 47 releases
content-control / classes / Upgrades / Backup.php

Backup.php in Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More trunk, at classes/Upgrades/Backup.php

177 lines 4.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Content Control Migrations
4 *
5 * @package ContentControl\Plugin
6 */
7
8 namespace ContentControl\Upgrades;
9
10 defined( 'ABSPATH' ) || exit;
11
12 use function __;
13
14 /**
15 * Backup before v2 migration.
16 */
17 abstract class Backup extends \ContentControl\Base\Upgrade {
18
19 const TYPE = 'backup';
20 const VERSION = 1;
21
22 /**
23 * The label for the file.
24 *
25 * @var string
26 */
27 protected $file_label = 'Content Control v1 Data Backup';
28
29 /**
30 * The file name.
31 *
32 * @var string
33 */
34 protected $file_name = 'content-control_v1_data-backup.json';
35
36 /**
37 * Get the label for the upgrade.
38 *
39 * @return string
40 */
41 public function label() {
42 return __( 'Backup plugin settings', 'content-control' );
43 }
44
45 /**
46 * Get v1 data.
47 *
48 * @return array<string,mixed>
49 */
50 abstract public function get_data();
51
52
53 /**
54 * Run the migration.
55 *
56 * @return void|\WP_Error|false
57 */
58 public function run() {
59 // Gets a stream or mock stream for sending events.
60 $stream = $this->stream();
61
62 $stream->start_task( __( 'Backing up plugin settings', 'content-control' ) );
63
64 $data = $this->get_data();
65
66 // Save the backup to the media library as a JSON file.
67 // Get WordPress upload directory.
68 $upload_dir = wp_upload_dir();
69
70 // Check if there was an error getting the upload directory.
71 if ( false !== $upload_dir['error'] ) {
72 // Handle the error, e.g., logging or returning.
73 $stream->send_event(
74 'task:error',
75 [
76 'message' => 'Error backing up data: retrieving the upload directory.',
77 ]
78 );
79
80 return false;
81 }
82
83 global $wp_filesystem;
84
85 // Initialize the WP filesystem.
86 if ( ! \WP_Filesystem() ) {
87 $stream->send_event(
88 'task:error',
89 [
90 'message' => 'Error backing up data: Unable to initialize the WP filesystem.',
91 ]
92 );
93
94 return false;
95 }
96
97 // Check if WP Filesystem is correctly initialized (in some cases, WP_Filesystem() might not throw an error, but still doesn't properly initialize).
98 if ( ! $wp_filesystem || ! is_object( $wp_filesystem ) ) {
99 $stream->send_event(
100 'task:error',
101 [
102 'message' => 'Error backing up data: WP Filesystem object is missing.',
103 ]
104 );
105
106 return false;
107 }
108
109 // Check if the put_contents method is available.
110 if ( ! method_exists( $wp_filesystem, 'put_contents' ) || ! method_exists( $wp_filesystem, 'exists' ) ) {
111 $stream->send_event(
112 'task:error',
113 [
114 'message' => 'Error backing up data: The put_contents method is missing from WP Filesystem.',
115 ]
116 );
117
118 return false;
119 }
120
121 $base_file_name = $this->file_name;
122 $file_extension = '.json';
123 $counter = 0;
124
125 // Determine the actual file name.
126 do {
127 $file_name = $base_file_name;
128 if ( $counter ) {
129 $file_name .= '-' . $counter;
130 }
131 $file_name .= $file_extension;
132 $file_path = $upload_dir['basedir'] . '/' . $file_name;
133 ++$counter;
134 } while ( $wp_filesystem->exists( $file_path ) );
135
136 // Write to the file.
137 $result = $wp_filesystem->put_contents( $file_path, wp_json_encode( $data ) );
138
139 // Check if there was an error writing to the file.
140 if ( ! $result ) {
141 // Handle the error, e.g., logging or returning.
142 $stream->send_event(
143 'task:error',
144 [
145 'message' => 'Error backing up data: Error writing to the JSON file.',
146 ]
147 );
148
149 return false;
150 }
151
152 // Add the file to the media library.
153 $attachment = [
154 'guid' => $upload_dir['baseurl'] . $file_name,
155 'post_mime_type' => 'application/json',
156 'post_title' => $this->file_label,
157 'post_content' => '',
158 'post_status' => 'inherit',
159 ];
160
161 $attach_id = wp_insert_attachment( $attachment, $file_path, 0, true );
162
163 if ( ! is_wp_error( $attach_id ) ) {
164 require_once ABSPATH . 'wp-admin/includes/image.php';
165 $attach_data = wp_generate_attachment_metadata( $attach_id, $file_path );
166 wp_update_attachment_metadata( $attach_id, $attach_data );
167 } else {
168 /* translators: %s: error message */
169 $stream->send_event( 'task:error', [ 'message' => sprintf( __( 'Failed to add JSON file to media library: %s', 'content-control' ), $attach_id->get_error_message() ) ] );
170 return false;
171 }
172
173 /* translators: %s: file path */
174 $stream->complete_task( sprintf( __( 'Plugin data backed up successfully to media library. Download the backup %s.', 'content-control' ), $upload_dir['baseurl'] . '/' . $file_name ) );
175 }
176 }
177