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_2.php

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

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