| 1 |
<?php |
| 2 |
/** |
| 3 |
* Backward compatibility functions. |
| 4 |
* |
| 5 |
* @package ContentControl |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace ContentControl; |
| 9 |
|
| 10 |
defined( 'ABSPATH' ) || exit; |
| 11 |
|
| 12 |
/** |
| 13 |
* Get v1 restrictions from wp_options. |
| 14 |
* |
| 15 |
* @return array<string,string|bool|int|array<mixed>>[]|false |
| 16 |
*/ |
| 17 |
function get_v1_restrictions() { |
| 18 |
$settings = \get_option( 'jp_cc_settings', [] ); |
| 19 |
|
| 20 |
if ( ! isset( $settings['restrictions'] ) || empty( $settings['restrictions'] ) ) { |
| 21 |
return false; |
| 22 |
} |
| 23 |
|
| 24 |
return $settings['restrictions']; |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Remap old conditions to new rules. |
| 29 |
* |
| 30 |
* @param array<array<string,mixed>> $old_conditions Array of old conditions. |
| 31 |
* |
| 32 |
* @return array{logicalOperator:string,items:array<array<string,mixed>>} |
| 33 |
*/ |
| 34 |
function remap_conditions_to_query( $old_conditions ) { |
| 35 |
$query = [ |
| 36 |
'logicalOperator' => 'and', |
| 37 |
'items' => [], |
| 38 |
]; |
| 39 |
|
| 40 |
// Old conditions were in a group[rules[]] format. |
| 41 |
foreach ( $old_conditions as $conditions ) { |
| 42 |
$group = [ |
| 43 |
'id' => uniqid(), |
| 44 |
'type' => 'group', |
| 45 |
'query' => [ |
| 46 |
'logicalOperator' => 'or', |
| 47 |
'items' => [], |
| 48 |
], |
| 49 |
]; |
| 50 |
|
| 51 |
// Loop over all old condtions in this group and map them to new rules. |
| 52 |
foreach ( $conditions as $condition ) { |
| 53 |
$group['query']['items'][] = remap_condition_to_rule( $condition ); |
| 54 |
} |
| 55 |
|
| 56 |
$query['items'][] = $group; |
| 57 |
} |
| 58 |
|
| 59 |
return $query; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Remap old condition to new rule. |
| 64 |
* |
| 65 |
* @param array<string,mixed> $condition Old condition. |
| 66 |
* |
| 67 |
* @return array<string,mixed> |
| 68 |
*/ |
| 69 |
function remap_condition_to_rule( $condition ) { |
| 70 |
$target = $condition['target']; |
| 71 |
|
| 72 |
// Handles 95% of the work except the name remap. |
| 73 |
$rule = [ |
| 74 |
'id' => uniqid(), |
| 75 |
'type' => 'rule', |
| 76 |
// custom rules will pass through. |
| 77 |
'name' => $target, |
| 78 |
'notOperand' => isset( $condition['not_operand'] ) && $condition['not_operand'] ? true : false, |
| 79 |
'options' => isset( $condition['settings'] ) && is_array( $condition['settings'] ) ? $condition['settings'] : [], |
| 80 |
]; |
| 81 |
|
| 82 |
// Start from simplest to match to most complex. |
| 83 |
if ( 'is_front_page' === $target ) { |
| 84 |
$rule['name'] = 'content_is_front_page'; |
| 85 |
} elseif ( 'is_home' === $target ) { |
| 86 |
$rule['name'] = 'content_is_blog_index'; |
| 87 |
} elseif ( 'is_search' === $target ) { |
| 88 |
$rule['name'] = 'content_is_search_results'; |
| 89 |
} elseif ( 'is_404' === $target ) { |
| 90 |
$rule['name'] = 'content_is_404_page'; |
| 91 |
} elseif ( strpos( $target, 'tax_' ) === 0 ) { |
| 92 |
// Split the target into post type and modifier. |
| 93 |
$tax_target = explode( '_', $target ); |
| 94 |
|
| 95 |
// Remove the tax_ prefix. |
| 96 |
array_shift( $tax_target ); |
| 97 |
|
| 98 |
// Modifier should be the last key. |
| 99 |
$modifier = array_pop( $tax_target ); |
| 100 |
|
| 101 |
// Post type is the remaining keys combined. |
| 102 |
$taxnomy = implode( '_', $tax_target ); |
| 103 |
|
| 104 |
// Using a switch for readability. |
| 105 |
switch ( $modifier ) { |
| 106 |
case 'all': |
| 107 |
$rule['name'] = "content_is_{$taxnomy}_archive"; |
| 108 |
break; |
| 109 |
|
| 110 |
case 'selected': |
| 111 |
$rule['name'] = "content_is_selected_tax_{$taxnomy}"; |
| 112 |
break; |
| 113 |
|
| 114 |
case 'ID': |
| 115 |
$rule['name'] = "content_is_tax_{$taxnomy}_with_id"; |
| 116 |
break; |
| 117 |
} |
| 118 |
} elseif ( strpos( $target, '_w_' ) > 0 ) { |
| 119 |
$pt_target = explode( '_w_', $target ); |
| 120 |
// First key is the post type. |
| 121 |
$post_type = array_shift( $pt_target ); |
| 122 |
// Last Key is the taxonomy. |
| 123 |
$taxonomy = array_pop( $pt_target ); |
| 124 |
|
| 125 |
$rule['name'] = "content_is_{$post_type}_with_{$taxonomy}"; |
| 126 |
} else { |
| 127 |
// Split the target into post type and modifier. |
| 128 |
$pt_target = explode( '_', $target ); |
| 129 |
|
| 130 |
// Modifier should be the last key. |
| 131 |
$modifier = array_pop( $pt_target ); |
| 132 |
|
| 133 |
// Post type is the remaining keys combined. |
| 134 |
$post_type = implode( '_', $pt_target ); |
| 135 |
|
| 136 |
if ( post_type_exists( $post_type ) && ! empty( $modifier ) ) { |
| 137 |
switch ( $modifier ) { |
| 138 |
case 'index': |
| 139 |
$rule['name'] = "content_is_{$post_type}_archive"; |
| 140 |
break; |
| 141 |
|
| 142 |
case 'all': |
| 143 |
$rule['name'] = "content_is_{$post_type}"; |
| 144 |
break; |
| 145 |
|
| 146 |
case 'selected': |
| 147 |
$rule['name'] = "content_is_selected_{$post_type}"; |
| 148 |
break; |
| 149 |
|
| 150 |
case 'ID': |
| 151 |
$rule['name'] = "content_is_{$post_type}_with_id"; |
| 152 |
break; |
| 153 |
|
| 154 |
case 'children': |
| 155 |
$rule['name'] = "content_is_child_of_{$post_type}"; |
| 156 |
break; |
| 157 |
|
| 158 |
case 'ancestors': |
| 159 |
$rule['name'] = "content_is_ancestor_of_{$post_type}"; |
| 160 |
break; |
| 161 |
|
| 162 |
case 'template': |
| 163 |
$rule['name'] = "content_is_{$post_type}_with_template"; |
| 164 |
break; |
| 165 |
} |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
return $rule; |
| 170 |
} |
| 171 |
|