Restrictions_2.php
4.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
/**
* Content Control Migrations
*
* @package ContentControl\Plugin
*/
namespace ContentControl\Upgrades;
defined( 'ABSPATH' ) || exit;
use function __;
use function add_post_meta;
use function wp_parse_args;
use function wp_insert_post;
use function ContentControl\remap_conditions_to_query;
use function ContentControl\get_default_restriction_settings;
/**
* Restrictions v2 migration.
*/
class Restrictions_2 extends \ContentControl\Base\Upgrade {
const TYPE = 'restrictions';
const VERSION = 2;
/**
* Get the label for the upgrade.
*
* @return string
*/
public function label() {
return __( 'Update global restrictions', 'content-control' );
}
/**
* Get the dependencies for this upgrade.
*
* @return string[]
*/
public function get_dependencies() {
return [
'backup-2',
];
}
/**
* Run the migration.
*
* @return void|\WP_Error|bool
*/
public function run() {
// Gets a stream or mock stream for sending events.
$stream = $this->stream();
// Get settings (where restrictions were store before).
$settings = \get_option( 'jp_cc_settings', [] );
if ( ! is_array( $settings ) ||
empty( $settings ) ||
! isset( $settings['restrictions'] ) ||
empty( $settings['restrictions'] )
) {
$stream->complete_task( __( 'No restrictions to migrate.', 'content-control' ) );
return true;
}
$restrictions = $settings['restrictions'];
$count = count( $restrictions );
if ( $count ) {
$stream->start_task(
// translators: %d: number of restrictions to migrate.
sprintf( __( 'Migrating %d restrictions', 'content-control' ), $count ),
$count
);
$progress = 0;
foreach ( $restrictions as $key => $restriction ) {
if ( $this->migrate_restriction( $restriction ) ) {
++$progress;
$stream->update_task_progress( $progress );
// Unset any restrictions that were migrated.
unset( $restrictions[ $key ] );
} else {
$stream->send_event(
'task:error',
[
// translators: %s: restriction title.
'message' => sprintf( __( 'Failed to migrate restriction "%s".', 'content-control' ), $restriction['title'] ),
'restriction' => $restriction,
]
);
}
}
}
if ( empty( $restrictions ) ) {
unset( $settings['restrictions'] );
} else {
$settings['restrictions'] = $restrictions;
}
// Update the settings with any migrated restrictions removed so they can later be migrated.
\update_option( 'jp_cc_settings', $settings );
if ( ! $count || $progress === $count ) {
// translators: %d: number of restrictions migrated.
$stream->complete_task( sprintf( __( '%d restrictions migrated', 'content-control' ), $count ) );
} else {
$stream->send_event(
'task:error',
[
// translators: %d: number of restrictions that failed to migrate.
'message' => sprintf( __( 'Failed to migrate %d restrictions.', 'content-control' ), count( $settings['restrictions'] ) ),
]
);
// Prevent marking this upgrade as complete.
return false;
}
}
/**
* Migrate a given restriction to the new post type.
*
* @param array<string,mixed> $restriction Restriction data.
*
* @return bool True if successful, false otherwise.
*/
public function migrate_restriction( $restriction ) {
static $priority = 0;
$restriction = wp_parse_args( $restriction, [
'title' => '',
'who' => '',
'roles' => [],
'protection_method' => 'redirect',
'show_excerpts' => false,
'override_default_message' => false,
'custom_message' => '',
'redirect_type' => 'login',
'redirect_url' => '',
'conditions' => '',
]);
$new_restriction_id = wp_insert_post(
[
'post_type' => 'cc_restriction',
'post_title' => $restriction['title'],
'post_content' => $restriction['custom_message'],
'post_status' => 'publish',
'menu_order' => $priority++, // Maintain order.
]
);
// Convert from associative to indexed array.
$user_roles = is_array( $restriction['roles'] ) ? array_values( $restriction['roles'] ) : [];
$settings = wp_parse_args( [
'userStatus' => $restriction['who'],
'roleMatch' => count( $user_roles ) > 0 ? 'match' : 'any',
'userRoles' => $user_roles,
'protectionMethod' => 'custom_message' === $restriction['protection_method'] ? 'replace' : 'redirect',
'showExcerpts' => $restriction['show_excerpts'],
'overrideMessage' => $restriction['override_default_message'],
'customMessage' => $restriction['custom_message'],
'redirectType' => $restriction['redirect_type'],
'redirectUrl' => esc_url_raw( $restriction['redirect_url'] ),
'conditions' => remap_conditions_to_query( $restriction['conditions'] ),
], get_default_restriction_settings() );
$added_meta = add_post_meta( $new_restriction_id, 'restriction_settings', $settings, true );
return $new_restriction_id > 0 && $added_meta;
}
}