| 1 |
<?php |
| 2 |
/** |
| 3 |
* Frontend feed setup. |
| 4 |
* |
| 5 |
* @copyright (c) 2021, Code Atlantic LLC. |
| 6 |
* @package ContentControl |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace ContentControl\Services; |
| 10 |
|
| 11 |
use ContentControl\Models\Restriction; |
| 12 |
|
| 13 |
use function ContentControl\plugin; |
| 14 |
use function ContentControl\get_query; |
| 15 |
use function ContentControl\current_query_context; |
| 16 |
use function ContentControl\deep_clean_array; |
| 17 |
|
| 18 |
defined( 'ABSPATH' ) || exit; |
| 19 |
|
| 20 |
/** |
| 21 |
* Restrictions service. |
| 22 |
*/ |
| 23 |
class Restrictions { |
| 24 |
|
| 25 |
/** |
| 26 |
* Get a list of all restrictions. |
| 27 |
* |
| 28 |
* @return Restriction[] |
| 29 |
*/ |
| 30 |
public function get_restrictions() { |
| 31 |
static $all_restrictions; |
| 32 |
|
| 33 |
if ( ! isset( $all_restrictions ) ) { |
| 34 |
$all_restrictions = []; |
| 35 |
|
| 36 |
if ( \ContentControl\get_data_version( 'restrictions' ) === 1 ) { |
| 37 |
$old_restrictions = \ContentControl\get_v1_restrictions(); |
| 38 |
|
| 39 |
if ( false !== $old_restrictions ) { |
| 40 |
foreach ( $old_restrictions as $key => $restriction ) { |
| 41 |
$restriction['id'] = (int) $key; |
| 42 |
$all_restrictions[ $key ] = new Restriction( $restriction ); |
| 43 |
} |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
// This should run safely if no v1 rules are found, or if they don't exist. |
| 48 |
if ( empty( $all_restrictions ) ) { |
| 49 |
// Query restriction post type. |
| 50 |
$restrictions = get_posts( |
| 51 |
[ |
| 52 |
'post_type' => 'cc_restriction', |
| 53 |
'posts_per_page' => -1, |
| 54 |
'post_status' => 'publish', |
| 55 |
// Order by menu order. |
| 56 |
'orderby' => [ |
| 57 |
'menu_order' => 'ASC', |
| 58 |
'post_modified' => 'DESC', |
| 59 |
], |
| 60 |
] |
| 61 |
); |
| 62 |
|
| 63 |
foreach ( $restrictions as $restriction ) { |
| 64 |
$all_restrictions[ $restriction->ID ] = new Restriction( $restriction ); |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
return $all_restrictions; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Get restriction, by ID, slug or object. |
| 74 |
* |
| 75 |
* @param int|string|Restriction $restriction Restriction ID, slug or object. |
| 76 |
* |
| 77 |
* @return Restriction|null |
| 78 |
*/ |
| 79 |
public function get_restriction( $restriction ) { |
| 80 |
if ( $restriction instanceof Restriction ) { |
| 81 |
return $restriction; |
| 82 |
} |
| 83 |
|
| 84 |
$restrictions = $this->get_restrictions(); |
| 85 |
|
| 86 |
// If restriction is an ID, get the object. |
| 87 |
if ( is_numeric( $restriction ) && isset( $restrictions[ $restriction ] ) ) { |
| 88 |
return $restrictions[ $restriction ]; |
| 89 |
} |
| 90 |
|
| 91 |
// If restriction is a slug, get the object. |
| 92 |
if ( is_string( $restriction ) ) { |
| 93 |
// Check if string is a slug or title. |
| 94 |
$slug = sanitize_title( $restriction ); |
| 95 |
|
| 96 |
if ( $slug === $restriction ) { |
| 97 |
foreach ( $restrictions as $restriction_obj ) { |
| 98 |
if ( $restriction_obj->slug === $restriction ) { |
| 99 |
return $restriction_obj; |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
// Check if string is a title. |
| 105 |
foreach ( $restrictions as $restriction_obj ) { |
| 106 |
if ( $restriction_obj->title === $restriction ) { |
| 107 |
return $restriction_obj; |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
return null; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Get cache key for restrictions. |
| 117 |
* |
| 118 |
* @param int|null $post_id Post ID. |
| 119 |
* |
| 120 |
* @return string |
| 121 |
*/ |
| 122 |
public function get_cache_key( $post_id = null ) { |
| 123 |
$query = get_query(); |
| 124 |
$context = current_query_context(); |
| 125 |
|
| 126 |
try { |
| 127 |
$hash_vars = deep_clean_array( $query->query_vars ); |
| 128 |
|
| 129 |
$query_hash = md5( maybe_serialize( $hash_vars ) ); |
| 130 |
} catch ( \Exception $e ) { |
| 131 |
$query_hash = md5( (string) wp_rand( 0, 100000 ) ); |
| 132 |
plugin( 'logging' )->log_unique( 'ERROR: ' . $e->getMessage() ); |
| 133 |
} |
| 134 |
|
| 135 |
if ( is_null( $post_id ) ) { |
| 136 |
$post_id = \get_the_ID(); |
| 137 |
} |
| 138 |
|
| 139 |
switch ( $context ) { |
| 140 |
case 'main': |
| 141 |
$cache_key = 'main'; |
| 142 |
break; |
| 143 |
|
| 144 |
case 'main/posts': |
| 145 |
case 'posts': |
| 146 |
$cache_key = 'post-' . $post_id; |
| 147 |
break; |
| 148 |
|
| 149 |
default: |
| 150 |
$cache_key = $context . '_' . $query_hash . ( $post_id ? ( '_post-' . $post_id ) : '' ); |
| 151 |
break; |
| 152 |
} |
| 153 |
|
| 154 |
return $cache_key; |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Get all applicable restrictions for the current post. |
| 159 |
* |
| 160 |
* Careful, this could be very unperformant if you have a lot of restrictions. |
| 161 |
* |
| 162 |
* @param int|null $post_id Post ID. |
| 163 |
* |
| 164 |
* @return Restriction[] |
| 165 |
*/ |
| 166 |
public function get_all_applicable_restrictions( $post_id = null ) { |
| 167 |
static $cache = []; |
| 168 |
$cache_key = $this->get_cache_key( $post_id ); |
| 169 |
|
| 170 |
if ( isset( $cache[ $cache_key ] ) ) { |
| 171 |
return $cache[ $cache_key ]; |
| 172 |
} |
| 173 |
|
| 174 |
$restrictions = $this->get_restrictions(); |
| 175 |
|
| 176 |
if ( ! empty( $restrictions ) ) { |
| 177 |
foreach ( $restrictions as $key => $restriction ) { |
| 178 |
if ( ! $restriction->check_rules() ) { |
| 179 |
unset( $restrictions[ $key ] ); |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
$cache[ $cache_key ] = $restrictions; |
| 185 |
|
| 186 |
return $restrictions; |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* Get the first applicable restriction for the current post. |
| 191 |
* |
| 192 |
* Performant version that breaks on first applicable restriction. Sorted by priority. |
| 193 |
* cached internally. |
| 194 |
* |
| 195 |
* @param int|null $post_id Post ID. |
| 196 |
* |
| 197 |
* @return Restriction|false |
| 198 |
*/ |
| 199 |
public function get_applicable_restriction( $post_id = null ) { |
| 200 |
static $cache = []; |
| 201 |
$cache_key = $this->get_cache_key( $post_id ); |
| 202 |
|
| 203 |
if ( isset( $cache[ $cache_key ] ) ) { |
| 204 |
return $cache[ $cache_key ]; |
| 205 |
} |
| 206 |
|
| 207 |
$cache[ $cache_key ] = false; |
| 208 |
|
| 209 |
$restrictions = $this->get_restrictions(); |
| 210 |
|
| 211 |
$restrictions = $this->sort_restrictions_by_priority( $restrictions ); |
| 212 |
|
| 213 |
if ( ! empty( $restrictions ) ) { |
| 214 |
foreach ( $restrictions as $restriction ) { |
| 215 |
if ( $restriction->check_rules() ) { |
| 216 |
$cache[ $cache_key ] = $restriction; |
| 217 |
break; |
| 218 |
} |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
return $cache[ $cache_key ]; |
| 223 |
} |
| 224 |
|
| 225 |
/** |
| 226 |
* Check if post has applicable restrictions. |
| 227 |
* |
| 228 |
* Cached via get_applicable_restriction(). |
| 229 |
* |
| 230 |
* @param int|null $post_id Post ID. |
| 231 |
* |
| 232 |
* @return boolean |
| 233 |
*/ |
| 234 |
public function has_applicable_restrictions( $post_id = null ) { |
| 235 |
return false !== $this->get_applicable_restriction( $post_id ); |
| 236 |
} |
| 237 |
|
| 238 |
/** |
| 239 |
* Check if user meets requirements for given restriction. |
| 240 |
* |
| 241 |
* @param int|string|Restriction $restriction Restriction ID, slug or object. |
| 242 |
* |
| 243 |
* @return boolean |
| 244 |
*/ |
| 245 |
public function user_meets_requirements( $restriction ) { |
| 246 |
$restriction = $this->get_restriction( $restriction ); |
| 247 |
|
| 248 |
if ( null === $restriction ) { |
| 249 |
return false; |
| 250 |
} |
| 251 |
|
| 252 |
static $cache = []; |
| 253 |
$cache_key = $restriction->id; |
| 254 |
|
| 255 |
// Check cache. |
| 256 |
if ( isset( $cache[ $cache_key ] ) ) { |
| 257 |
return $cache[ $cache_key ]; |
| 258 |
} |
| 259 |
|
| 260 |
$user_meets_requirements = $restriction->user_meets_requirements(); |
| 261 |
|
| 262 |
// Cache result. |
| 263 |
$cache[ $cache_key ] = $user_meets_requirements; |
| 264 |
|
| 265 |
return $user_meets_requirements; |
| 266 |
} |
| 267 |
|
| 268 |
/** |
| 269 |
* Sort restrictions based on post sort order. |
| 270 |
* |
| 271 |
* MOVE to restrictions file. |
| 272 |
* |
| 273 |
* @param \ContentControl\Models\Restriction[] $restrictions Restrictions. |
| 274 |
* |
| 275 |
* @return \ContentControl\Models\Restriction[] |
| 276 |
*/ |
| 277 |
public function sort_restrictions_by_priority( $restrictions ) { |
| 278 |
usort( $restrictions, function ( $a, $b ) { |
| 279 |
return $a->priority - $b->priority; |
| 280 |
}); |
| 281 |
|
| 282 |
return $restrictions; |
| 283 |
} |
| 284 |
} |
| 285 |
|