back-compat.php
4.2 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
<?php
/**
* Backward compatibility functions.
*
* @package ContentControl
*/
namespace ContentControl;
defined( 'ABSPATH' ) || exit;
/**
* Get v1 restrictions from wp_options.
*
* @return array<string,string|bool|int|array<mixed>>[]|false
*/
function get_v1_restrictions() {
$settings = \get_option( 'jp_cc_settings', [] );
if ( ! isset( $settings['restrictions'] ) || empty( $settings['restrictions'] ) ) {
return false;
}
return $settings['restrictions'];
}
/**
* Remap old conditions to new rules.
*
* @param array<array<string,mixed>> $old_conditions Array of old conditions.
*
* @return array{logicalOperator:string,items:array<array<string,mixed>>}
*/
function remap_conditions_to_query( $old_conditions ) {
$query = [
'logicalOperator' => 'and',
'items' => [],
];
// Old conditions were in a group[rules[]] format.
foreach ( $old_conditions as $conditions ) {
$group = [
'id' => uniqid(),
'type' => 'group',
'query' => [
'logicalOperator' => 'or',
'items' => [],
],
];
// Loop over all old condtions in this group and map them to new rules.
foreach ( $conditions as $condition ) {
$group['query']['items'][] = remap_condition_to_rule( $condition );
}
$query['items'][] = $group;
}
return $query;
}
/**
* Remap old condition to new rule.
*
* @param array<string,mixed> $condition Old condition.
*
* @return array<string,mixed>
*/
function remap_condition_to_rule( $condition ) {
$target = $condition['target'];
// Handles 95% of the work except the name remap.
$rule = [
'id' => uniqid(),
'type' => 'rule',
// custom rules will pass through.
'name' => $target,
'notOperand' => isset( $condition['not_operand'] ) && $condition['not_operand'] ? true : false,
'options' => isset( $condition['settings'] ) && is_array( $condition['settings'] ) ? $condition['settings'] : [],
];
// Start from simplest to match to most complex.
if ( 'is_front_page' === $target ) {
$rule['name'] = 'content_is_front_page';
} elseif ( 'is_home' === $target ) {
$rule['name'] = 'content_is_blog_index';
} elseif ( 'is_search' === $target ) {
$rule['name'] = 'content_is_search_results';
} elseif ( 'is_404' === $target ) {
$rule['name'] = 'content_is_404_page';
} elseif ( strpos( $target, 'tax_' ) === 0 ) {
// Split the target into post type and modifier.
$tax_target = explode( '_', $target );
// Remove the tax_ prefix.
array_shift( $tax_target );
// Modifier should be the last key.
$modifier = array_pop( $tax_target );
// Post type is the remaining keys combined.
$taxnomy = implode( '_', $tax_target );
// Using a switch for readability.
switch ( $modifier ) {
case 'all':
$rule['name'] = "content_is_{$taxnomy}_archive";
break;
case 'selected':
$rule['name'] = "content_is_selected_tax_{$taxnomy}";
break;
case 'ID':
$rule['name'] = "content_is_tax_{$taxnomy}_with_id";
break;
}
} elseif ( strpos( $target, '_w_' ) > 0 ) {
$pt_target = explode( '_w_', $target );
// First key is the post type.
$post_type = array_shift( $pt_target );
// Last Key is the taxonomy.
$taxonomy = array_pop( $pt_target );
$rule['name'] = "content_is_{$post_type}_with_{$taxonomy}";
} else {
// Split the target into post type and modifier.
$pt_target = explode( '_', $target );
// Modifier should be the last key.
$modifier = array_pop( $pt_target );
// Post type is the remaining keys combined.
$post_type = implode( '_', $pt_target );
if ( post_type_exists( $post_type ) && ! empty( $modifier ) ) {
switch ( $modifier ) {
case 'index':
$rule['name'] = "content_is_{$post_type}_archive";
break;
case 'all':
$rule['name'] = "content_is_{$post_type}";
break;
case 'selected':
$rule['name'] = "content_is_selected_{$post_type}";
break;
case 'ID':
$rule['name'] = "content_is_{$post_type}_with_id";
break;
case 'children':
$rule['name'] = "content_is_child_of_{$post_type}";
break;
case 'ancestors':
$rule['name'] = "content_is_ancestor_of_{$post_type}";
break;
case 'template':
$rule['name'] = "content_is_{$post_type}_with_template";
break;
}
}
}
return $rule;
}