developers.php 10.7 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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
<?php
/**
 * Restriction utility & helper functions.
 *
 * @package ContentControl
 * @subpackage Functions
 * @copyright (c) 2023 Code Atlantic LLC
 */

namespace ContentControl;

use ContentControl\Models\Restriction;

use function ContentControl\plugin;
use function ContentControl\set_rules_query;
use function ContentControl\is_rest;

defined( 'ABSPATH' ) || exit;

/**
 * Check if content has restrictions.
 *
 * @param int|null $content_id Content ID.
 *
 * @return bool
 *
 * @since 2.0.0
 */
function content_has_restrictions( $content_id = null ) {
	$overload_content = setup_content_globals( $content_id );

	$has_restrictions = plugin( 'restrictions' )->has_applicable_restrictions( $content_id );

	// Clear content if we overloaded it.
	if ( $overload_content ) {
		reset_content_globals();
	}

	/**
	 * Filter whether content has restrictions.
	 *
	 * @param bool $has_restrictions Whether content has restrictions.
	 * @param int|null $content_id Content ID.
	 *
	 * @return bool
	 *
	 * @since 2.0.0
	 */
	return (bool) apply_filters( 'content_control/content_has_restrictions', $has_restrictions, $content_id );
}

/**
 * Check if user can access content.
 *
 * @param int|null $content_id Content ID.
 *
 * @return bool True if user meets requirements, false if not.
 *
 * @since 2.0.0
 */
function user_can_view_content( $content_id = null ) {
	if ( ! content_has_restrictions( $content_id ) ) {
		return true;
	}

	$can_view = true;

	$restrictions = [];

	if ( false === (bool) apply_filters( 'content_control/check_all_restrictions', false, $content_id ) ) {
		/**
		 * Fetch first applicable restriction.
		 *
		 * @var Restriction|false $restriction
		 */
		$restriction = get_applicable_restrictions( $content_id, true );

		if ( $restriction ) {
			$can_view       = $restriction->user_meets_requirements();
			$restrictions[] = $restriction;
		}
	} else {
		/**
		 * Fetch all applicable restrictions.
		 *
		 * @var Restriction[]|false $restrictions
		 */
		$restrictions = get_applicable_restrictions( $content_id, false );

		if ( count( $restrictions ) ) {
			$checks = [];

			foreach ( $restrictions as $restriction ) {
				$checks[] = $restriction->user_meets_requirements();
			}

			// When checking all, we are looking for any true value.
			$can_view = in_array( true, $checks, true );
		}
	}

	/**
	 * Filter whether user can view content.
	 *
	 * @param bool $can_view Whether user can view content.
	 * @param int|null $content_id Content ID.
	 * @param Restriction[] $restrictions Restrictions.
	 *
	 * @return bool
	 *
	 * @since 2.0.0
	 */
	return (bool) apply_filters( 'content_control/user_can_view_content', $can_view, $content_id, $restrictions );
}

/**
 * Helper that checks if given or current content is restricted or not.
 *
 * @see \ContentControl\user_can_view_content() to check if user can view content.
 *
 * @param int|null $content_id Content ID.
 *
 * @return bool
 *
 * @since 2.0.0
 */
function content_is_restricted( $content_id = null ) {
	$is_restricted = ! user_can_view_content( $content_id );

	/**
	 * Filter whether content is restricted.
	 *
	 * @param bool $is_restricted Whether content is restricted.
	 * @param int|null $content_id Content ID.
	 *
	 * @return bool
	 *
	 * @since 2.0.0
	 */
	return (bool) apply_filters( 'content_control/content_is_restricted', $is_restricted, $content_id );
}

/**
 * Get applicable restrictions for the given content.
 *
 * If $single is true, return the first applicable restriction. If false, return all applicable restrictions.
 * Sorted by priority and cached internally.
 *
 * @param int|null $content_id Content ID.
 * @param bool     $single     Whether to return a single match or an array of matches.
 *
 * @return Restriction|Restriction[]|false
 *
 * @since 2.4.0
 */
function get_applicable_restrictions( $content_id = null, $single = true ) {
	$overload_content = setup_content_globals( $content_id );

	$restriction = plugin( 'restrictions' )->get_applicable_restrictions( $content_id, $single );

	// Clear content if we overloaded it.
	if ( $overload_content ) {
		reset_content_globals();
	}

	return $restriction;
}

/**
 * Get applicable restriction.
 *
 * @param int|null $content_id Content ID.
 *
 * @return Restriction|false
 *
 * @since 2.0.0
 */
function get_applicable_restriction( $content_id = null ) {
	return get_applicable_restrictions( $content_id, true );
}

/**
 * Get all applicable restrictions.
 *
 * @param int|null $content_id Content ID.
 *
 * @return Restriction[]
 *
 * @since 2.0.11
 */
function get_all_applicable_restrictions( $content_id = null ) {
	return get_applicable_restrictions( $content_id, false );
}

/**
 * Check if query has restrictions.
 *
 * @param \WP_Query $query Query object.
 *
 * @return array<array{restriction:Restriction,post_ids:int[]}>|false
 *
 * @since 2.0.0
 */
function get_restriction_matches_for_queried_posts( $query ) {
	// If its the main query, and not an archive-like page, bail.
	if ( $query->is_main_query() && ( ! $query->is_home() && ! $query->is_archive() && ! $query->is_search() ) ) {
		return false;
	}

	if ( empty( $query->posts ) ) {
		return false;
	}

	static $restrictions;

	// Generate cache key from hasing $wp_query.
	$cache_key = md5( (string) wp_json_encode( $query ) );

	if ( isset( $restrictions[ $cache_key ] ) ) {
		return $restrictions[ $cache_key ];
	}

	set_rules_query( $query );

	foreach ( $query->posts as $post ) {
		/**
		 * Post ID.
		 *
		 * @var \WP_Post $post
		 */
		if ( content_is_restricted( $post->ID ) ) {
			// TODO This needs to likely respect the filter for checking all applicable restrictions.
			$restriction = get_applicable_restriction( $post->ID );

			if ( ! $restriction ) {
				continue;
			}

			if ( ! isset( $restrictions[ $cache_key ][ $restriction->priority ] ) ) {
				// Handles deduplication & sorting.
				$restrictions[ $cache_key ][ $restriction->priority ] = [
					'restriction' => $restriction,
					'post_ids'    => [],
				];
			}

			// Add post to restriction.
			$restrictions[ $cache_key ][ $restriction->priority ]['post_ids'][] = $post->ID;
		}
	}

	set_rules_query( null );

	if ( empty( $restrictions[ $cache_key ] ) ) {
		$restrictions[ $cache_key ] = false;
	} else {
		// Sort by priority.
		ksort( $restrictions[ $cache_key ] );
		// Remove priority keys.
		$restrictions[ $cache_key ] = array_values( $restrictions[ $cache_key ] );
	}

	return $restrictions[ $cache_key ];
}

/**
 * Check if query has restrictions.
 *
 * @param \WP_Term_Query $query Query object.
 *
 * @return array<array{restriction:Restriction,term_ids:int[]}>|false
 *
 * @since 2.2.0
 */
function get_restriction_matches_for_queried_terms( $query ) {
	if ( empty( $query->terms ) ) {
		return false;
	}

	static $restrictions;

	// Generate cache key from hasing $wp_query.
	$cache_key = md5( (string) wp_json_encode( $query ) );

	if ( isset( $restrictions[ $cache_key ] ) ) {
		return $restrictions[ $cache_key ];
	}

	set_rules_query( $query );

	foreach ( $query->terms as $term ) {
		$term_id = false;

		if ( is_object( $term ) && isset( $term->term_id ) ) {
			$term_id = (int) $term->term_id;
		} elseif ( is_numeric( $term ) ) {
			$term_id = absint( $term );
		}

		if ( $term_id > 0 && content_is_restricted( $term_id ) ) {
			// TODO This needs to likely respect the filter for checking all applicable restrictions.
			$restriction = get_applicable_restrictions( $term_id, true );

			if ( ! $restriction ) {
				continue;
			}

			if ( ! isset( $restrictions[ $cache_key ][ $restriction->priority ] ) ) {
				// Handles deduplication & sorting.
				$restrictions[ $cache_key ][ $restriction->priority ] = [
					'restriction' => $restriction,
					'term_ids'    => [],
				];
			}

			// Add term to restriction.
			$restrictions[ $cache_key ][ $restriction->priority ]['term_ids'][] = $term_id;
		}
	}

	set_rules_query( null );

	if ( empty( $restrictions[ $cache_key ] ) ) {
		$restrictions[ $cache_key ] = false;
	} else {
		// Sort by priority.
		ksort( $restrictions[ $cache_key ] );
		// Remove priority keys.
		$restrictions[ $cache_key ] = array_values( $restrictions[ $cache_key ] );
	}

	return $restrictions[ $cache_key ];
}

/**
 * Check if the referrer is the sites admin area.
 *
 * @return bool
 *
 * @since 2.2.0
 */
function check_referrer_is_admin() {
	$referrer = wp_get_raw_referer();
	if ( empty( $referrer ) ) {
		return false;
	}

	$admin_url = admin_url();
	// Normalize URLs for comparison.
	$normalized_referrer  = strtolower( $referrer );
	$normalized_admin_url = strtolower( $admin_url );

	// Compare the beginning of the referrer with the admin URL.
	return str_starts_with( $normalized_referrer, $normalized_admin_url );
}

/**
 * Check if request is excluded.
 *
 * @return bool
 *
 * @since 2.3.1
 */
function request_is_excluded_rest_endpoint() {
	/**
	 * Filter whether to exclude a request from being restricted.
	 *
	 * @param bool $is_excluded Whether to exclude the request.
	 *
	 * @return bool
	 */
	return apply_filters( 'content_control/request_is_excluded_rest_endpoint', ! is_wp_core_rest_namespace() );
}

/**
 * Check if request is excluded.
 *
 * @return bool
 *
 * @since 2.3.0
 */
function request_is_excluded() {
	static $is_excluded;

	if ( isset( $is_excluded ) ) {
		return $is_excluded;
	}

	$is_excluded = false;

	if (
		// Check if doing cron.
		is_cron() ||
		// phpcs:ignore WordPress.Security.NonceVerification.Recommended
		( is_ajax() && isset( $_REQUEST['action'] ) && 'heartbeat' === $_REQUEST['action'] ) ||
		// If this is rest request and not core wp namespace.
		( is_rest() && request_is_excluded_rest_endpoint() )
	) {
		$is_excluded = true;
	}

	return $is_excluded;
}

/**
 * Check if the request is for a priveleged user in the admin area.
 *
 * @return bool
 *
 * @since 2.3.0
 */
function request_for_user_is_excluded() {
	// Check if user has permission to manage settings and is on the admin area.
	if ( user_is_excludable() ) {
		if (
			// Is in the admin area.
			is_admin() ||
			( is_preview() && current_user_can( 'edit_post', get_the_ID() ) ) ||
			// Is an ajax request from the admin area.
			(
				( is_ajax() || is_rest() ) &&
				check_referrer_is_admin()
			)
		) {
			return true;
		}
	}

	return false;
}

/**
 * Check if protection methods should be disabled.
 *
 * Generally used to bypass protections when using page editors.
 *
 * @return bool
 *
 * @since 2.0.0
 */
function protection_is_disabled() {
	static $protection_disabled;

	if ( isset( $protection_disabled ) ) {
		return $protection_disabled;
	}

	$protection_disabled = user_is_excluded() || request_is_excluded() || request_for_user_is_excluded();

	/**
	 * Filter whether protection is disabled.
	 *
	 * @param bool $is_disabled Whether protection is disabled.
	 *
	 * @return bool
	 *
	 * @since 2.0.0
	 */
	$protection_disabled = apply_filters( 'content_control/protection_is_disabled', $protection_disabled );

	return $protection_disabled;
}