<commit_before><?php

load_plugin_textdomain( 'buddypress-sitewide-activity-widget', FALSE, basename( dirname( __FILE__ ) ) . '/languages/' );

load_plugin_textdomain( 'invite-anyone', false, dirname( basename( __FILE__ ) ). '/languages/' );

// prevent users from seeing one another's followers (can only see their own)
// unfortunately there's no filter to prevent running the query, but we can at least empty the result before rendering
function hcommons_filter_get_followers( $followers ) {
	if ( bp_displayed_user_id() !== get_current_user_id() ) {
		$followers = [];
	}
	return $followers;
}
add_filter( 'bp_follow_get_followers', 'hcommons_filter_get_followers' );

function hcommons_filter_groups_activity_new_update_action( $activity_action ) {
	$activity_action = preg_replace( '/(in the group <a href="[^"]*)(">)/', '\1activity\2', $activity_action );
	return $activity_action;
}
add_filter( 'groups_activity_new_update_action', 'hcommons_filter_groups_activity_new_update_action' );


/**
 * remove urls in comments from comment notification emails so that we don't trigger spam filters
 */
function hcommons_filter_comment_notification_text( $text ) {
	$delimiter = 'You can see all comments on this post here:';
	$exploded_text = explode( $delimiter, $text );

	// http://stackoverflow.com/a/6165666/700113
	$pattern = "/(?i)\b((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’]))/";
	$replace = '<url removed>';
	$exploded_text[0] = preg_replace( $pattern, $replace, $exploded_text[0] );

	$text = $exploded_text[0] . $delimiter . $exploded_text[1];

	return $text;
}
add_filter( 'comment_notification_text', 'hcommons_filter_comment_notification_text' );

/**
 * append some text to the bottom of any/all themes to tell users about HC and its networks
 */
function hcommons_wp_footer() {
	function is_society_blog() {
		$society_blog_ids = [
			constant( 'HC_ROOT_BLOG_ID' ),
			constant( 'UP_ROOT_BLOG_ID' ),
			constant( 'AJS_ROOT_BLOG_ID' ),
			constant( 'ASEEES_ROOT_BLOG_ID' ),
			constant( 'CAA_ROOT_BLOG_ID' ),
			constant( 'MLA_ROOT_BLOG_ID' ),
		];

		return in_array( (string) get_current_blog_id(), $society_blog_ids );
	}

	if (
		class_exists( 'Humanities_Commons' ) &&
		! empty( Humanities_Commons::$society_id ) &&
		! is_society_blog()
	) {
		$main_site_domain = Humanities_Commons::$main_site->domain;
		$society_id = Humanities_Commons::$society_id;

		$society_url = sprintf(
			'https://%s%s',
			( 'hc' === $society_id ) ? '' : $society_id . '.',
			$main_site_domain
		);

		$style = implode( ';', [
			'background-color: white',
			'color: black',
			'line-height: 3em',
			'position: relative',
			'text-align: center',
			'width: 100%',
			'z-index: 100',
		] );

		$text = sprintf(
			'<div id="hcommons-network-footer" style="%s">This site is part of %s<em><a href="%s">Humanities Commons</a></em>. <a href="%s">Explore other sites on this network</a> or <a href="%s">register to build your own</a>.</div>',
			$style,
			( 'hc' === $society_id ) ? '' : sprintf( 'the %s network on ', strtoupper( $society_id ) ),
			'https://' . $main_site_domain,
			trailingslashit( $society_url ) . 'sites',
			$society_url
		);

		// fix commentpress
		$script = '<script>jQuery(".cp_sidebar_toc #hcommons-network-footer").appendTo("#footer").css({"line-height": "2em"});</script>';

		echo $text . $script;
	}
}
add_action( 'wp_footer', 'hcommons_wp_footer' );

/**
 * Comments are not nested correctly if caching is on, so disable it for commentpress.
 */
function hcommons_selectively_disable_object_cache() {
	$theme = wp_get_theme();

	if ( false !== strpos( strtolower( $theme->get( 'Name' ) ), 'commentpress' ) ) {
		wp_cache_add_non_persistent_groups( array( 'comment' ) );
	}
}
add_action( 'plugins_loaded', 'hcommons_selectively_disable_object_cache' );


// shibboleth attempts to put users back where they came from after authenticating with the redirect_to param.
// that param is not always preserved through the login flow, so handle it here with a cookie to be sure.
function hcommons_maybe_redirect_after_login() {
	$param_name = 'redirect_to';
	$cookie_name = $param_name;

	// Some pages need to be excluded from being redirect targets.
	$blacklist = [
		'/',
		'/clear-session/',
		'/logged-out/',
		'/not-a-member/',
		'/wp-admin/admin-ajax.php',
		'/wp-login.php?action=shibboleth',
	];

	// Once user has authenticated, maybe redirect to original destination.
	if ( is_user_logged_in() ) {

		// unset cookie on each network domains
		foreach ( get_networks() as $network ) {
			setcookie( $cookie_name, '', time() - YEAR_IN_SECONDS, COOKIEPATH, $network->cookie_domain );
		}

		if ( isset( $_COOKIE[ $cookie_name ] ) ) {
			// only redirect if we're not already there
			if ( false === strpos( $_COOKIE[ $cookie_name ], $_SERVER['REQUEST_URI'] ) ) {
				// Can't use wp_safe_redirect due to filters, just send directly.
				header( 'Location: ' . $_COOKIE[ $cookie_name ] );
				exit;
			}
		}

	// Otherwise, as long as this isn't a blacklisted page, set cookie.
	} else if ( ! in_array( $_SERVER['REQUEST_URI'], $blacklist ) && false === strpos( $_SERVER['REQUEST_URI'], '/wp-json/' ) ) {
		// Direct access to protected group docs is handled with another redirect, leave as-is.
		if (
			! isset( $_COOKIE['bp-message'] ) ||
			! preg_match( '/You must be a logged-in member/', $_COOKIE['bp-message'] )
		) {
			$cookie_value = isset( $_REQUEST[ $param_name ] ) ? $_REQUEST[ $param_name ] : get_site_url() . $_SERVER['REQUEST_URI'];

			setcookie( $cookie_name, $cookie_value, time() + 5 * MINUTE_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN );
		}

		// No need to set duplicate cookies, once we set one we're done with this request.
		remove_action( 'bp_do_404', __METHOD__ );
		remove_action( 'init', __METHOD__, 15 );<commit_msg>shorten redirect_to cookie lifetime to 1 minute<commit_after><?php

load_plugin_textdomain( 'buddypress-sitewide-activity-widget', FALSE, basename( dirname( __FILE__ ) ) . '/languages/' );

load_plugin_textdomain( 'invite-anyone', false, dirname( basename( __FILE__ ) ). '/languages/' );

// prevent users from seeing one another's followers (can only see their own)
// unfortunately there's no filter to prevent running the query, but we can at least empty the result before rendering
function hcommons_filter_get_followers( $followers ) {
	if ( bp_displayed_user_id() !== get_current_user_id() ) {
		$followers = [];
	}
	return $followers;
}
add_filter( 'bp_follow_get_followers', 'hcommons_filter_get_followers' );

function hcommons_filter_groups_activity_new_update_action( $activity_action ) {
	$activity_action = preg_replace( '/(in the group <a href="[^"]*)(">)/', '\1activity\2', $activity_action );
	return $activity_action;
}
add_filter( 'groups_activity_new_update_action', 'hcommons_filter_groups_activity_new_update_action' );


/**
 * remove urls in comments from comment notification emails so that we don't trigger spam filters
 */
function hcommons_filter_comment_notification_text( $text ) {
	$delimiter = 'You can see all comments on this post here:';
	$exploded_text = explode( $delimiter, $text );

	// http://stackoverflow.com/a/6165666/700113
	$pattern = "/(?i)\b((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’]))/";
	$replace = '<url removed>';
	$exploded_text[0] = preg_replace( $pattern, $replace, $exploded_text[0] );

	$text = $exploded_text[0] . $delimiter . $exploded_text[1];

	return $text;
}
add_filter( 'comment_notification_text', 'hcommons_filter_comment_notification_text' );

/**
 * append some text to the bottom of any/all themes to tell users about HC and its networks
 */
function hcommons_wp_footer() {
	function is_society_blog() {
		$society_blog_ids = [
			constant( 'HC_ROOT_BLOG_ID' ),
			constant( 'UP_ROOT_BLOG_ID' ),
			constant( 'AJS_ROOT_BLOG_ID' ),
			constant( 'ASEEES_ROOT_BLOG_ID' ),
			constant( 'CAA_ROOT_BLOG_ID' ),
			constant( 'MLA_ROOT_BLOG_ID' ),
		];

		return in_array( (string) get_current_blog_id(), $society_blog_ids );
	}

	if (
		class_exists( 'Humanities_Commons' ) &&
		! empty( Humanities_Commons::$society_id ) &&
		! is_society_blog()
	) {
		$main_site_domain = Humanities_Commons::$main_site->domain;
		$society_id = Humanities_Commons::$society_id;

		$society_url = sprintf(
			'https://%s%s',
			( 'hc' === $society_id ) ? '' : $society_id . '.',
			$main_site_domain
		);

		$style = implode( ';', [
			'background-color: white',
			'color: black',
			'line-height: 3em',
			'position: relative',
			'text-align: center',
			'width: 100%',
			'z-index: 100',
		] );

		$text = sprintf(
			'<div id="hcommons-network-footer" style="%s">This site is part of %s<em><a href="%s">Humanities Commons</a></em>. <a href="%s">Explore other sites on this network</a> or <a href="%s">register to build your own</a>.</div>',
			$style,
			( 'hc' === $society_id ) ? '' : sprintf( 'the %s network on ', strtoupper( $society_id ) ),
			'https://' . $main_site_domain,
			trailingslashit( $society_url ) . 'sites',
			$society_url
		);

		// fix commentpress
		$script = '<script>jQuery(".cp_sidebar_toc #hcommons-network-footer").appendTo("#footer").css({"line-height": "2em"});</script>';

		echo $text . $script;
	}
}
add_action( 'wp_footer', 'hcommons_wp_footer' );

/**
 * Comments are not nested correctly if caching is on, so disable it for commentpress.
 */
function hcommons_selectively_disable_object_cache() {
	$theme = wp_get_theme();

	if ( false !== strpos( strtolower( $theme->get( 'Name' ) ), 'commentpress' ) ) {
		wp_cache_add_non_persistent_groups( array( 'comment' ) );
	}
}
add_action( 'plugins_loaded', 'hcommons_selectively_disable_object_cache' );


// shibboleth attempts to put users back where they came from after authenticating with the redirect_to param.
// that param is not always preserved through the login flow, so handle it here with a cookie to be sure.
function hcommons_maybe_redirect_after_login() {
	$param_name = 'redirect_to';
	$cookie_name = $param_name;

	// Some pages need to be excluded from being redirect targets.
	$blacklist = [
		'/',
		'/clear-session/',
		'/logged-out/',
		'/not-a-member/',
		'/wp-admin/admin-ajax.php',
		'/wp-login.php?action=shibboleth',
	];

	// Once user has authenticated, maybe redirect to original destination.
	if ( is_user_logged_in() ) {

		// unset cookie on each network domains
		foreach ( get_networks() as $network ) {
			setcookie( $cookie_name, '', time() - YEAR_IN_SECONDS, COOKIEPATH, $network->cookie_domain );
		}

		if ( isset( $_COOKIE[ $cookie_name ] ) ) {
			// only redirect if we're not already there
			if ( false === strpos( $_COOKIE[ $cookie_name ], $_SERVER['REQUEST_URI'] ) ) {
				// Can't use wp_safe_redirect due to filters, just send directly.
				header( 'Location: ' . $_COOKIE[ $cookie_name ] );
				exit;
			}
		}

	// Otherwise, as long as this isn't a blacklisted page, set cookie.
	} else if ( ! in_array( $_SERVER['REQUEST_URI'], $blacklist ) && false === strpos( $_SERVER['REQUEST_URI'], '/wp-json/' ) ) {
		// Direct access to protected group docs is handled with another redirect, leave as-is.
		if (
			! isset( $_COOKIE['bp-message'] ) ||
			! preg_match( '/You must be a logged-in member/', $_COOKIE['bp-message'] )
		) {
			$cookie_value = isset( $_REQUEST[ $param_name ] ) ? $_REQUEST[ $param_name ] : get_site_url() . $_SERVER['REQUEST_URI'];

			setcookie( $cookie_name, $cookie_value, time() + MINUTE_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN );
		}

		// No need to set duplicate cookies, once we set one we're done with this request.
		remove_action( 'bp_do_404', __METHOD__ );
		remove_action( 'init', __METHOD__, 15 );
{'fraction_non_alphanumeric': 0.17116585118947192, 'fraction_numerical': 0.006580057364602666, 'mean_word_length': 4.460617227084293, 'pattern_counts': {'":': 2, '<': 45, '<?xml version=': 0, '>': 49, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '751414', 'n_tokens_mistral': 4488, 'n_tokens_neox': 4165, 'n_words': 973}