<?php
/**
 * BuddyPress Check In plugin functions file.
 *
 * @link       https://wbcomdesigns.com/
 * @since      1.0.0
 *
 * @package    BuddyPress Check In
 * @subpackage BuddyPress Check In/includes
 */


/**
 * Determine whether check-in assets (JS/CSS) should be enqueued.
 *
 * This helper checks if the current context matches any conditions where
 * check-in functionality is expected to be used — such as BuddyPress components,
 * specific shortcodes, or post types. It is used to conditionally load
 * plugin styles and scripts only when needed.
 *
 * Override the logic via the 'bpchk_enqueue_checkins_assets' filter.
 *
 * @since 1.0.0
 *
 * @param WP_Post $post              The current global post object.
 * @param string  $checkin_tab_slug The sanitized slug of the check-in tab.
 * @param string  $current_component The current BuddyPress component (e.g., 'activity').
 *
 * @return bool Whether to enqueue the check-in plugin assets.
 */
function bpchk_enqueue_checkins_assets( $post, $checkin_tab_slug, $current_component ) {
	$request_uri = filter_input( INPUT_SERVER, 'REQUEST_URI', FILTER_SANITIZE_URL );

	$should_enqueue = (
		bp_is_groups_component()
		|| bp_is_activity_component()
		|| bp_is_profile_component()
		|| ( $request_uri && strpos( $request_uri, $checkin_tab_slug ) !== false )
		|| ( isset( $post->post_content ) && has_shortcode( $post->post_content, 'activity-listing' ) )
		|| ( isset( $post->post_content ) && has_shortcode( $post->post_content, 'bppfa_postform' ) )
		|| ( is_single() && get_post_type() === 'business' )
		|| $current_component === 'activity'
	);

	/**
	 * Filter whether to enqueue checkin styles.
	 *
	 * @param bool   $should_enqueue       Final result of checkin style logic.
	 * @param object $post                 The current post object.
	 * @param string $checkin_tab_slug     Slug for checkin tab.
	 * @param string $current_component    Current BuddyPress component.
	 */
	return apply_filters( 'bpchk_enqueue_checkins_assets', $should_enqueue, $post, $checkin_tab_slug, $current_component );
}

