<?php
/**
 * The public-facing functionality of the plugin.
 *
 * @link       https://wbcomdesigns.com/
 * @since      1.0.0
 *
 * @package    Bp_Checkins
 * @subpackage Bp_Checkins/public
 **/

if ( ! class_exists( 'Bpchk_Checkins_Public' ) ) :

	/**
	 * The public-facing functionality of the plugin.
	 *
	 * Defines the plugin name, version, and two examples hooks for how to
	 * enqueue the admin-specific stylesheet and JavaScript.
	 *
	 * @package    Bp_Checkins
	 * @subpackage Bp_Checkins/public
	 * @author     Wbcom Designs <admin@wbcomdesigns.com>
	 */
	class Bpchk_Checkins_Public {

		/**
		 * The ID of this plugin.
		 *
		 * @since    1.0.0
		 * @access   private
		 * @var      string    $plugin_name    The ID of this plugin.
		 */
		private $plugin_name;

		/**
		 * The version of this plugin.
		 *
		 * @since    1.0.0
		 * @access   private
		 * @var      string    $version    The current version of this plugin.
		 */
		private $version;

		/**
		 * The version of this plugin.
		 *
		 * @since    1.0.0
		 * @access   public
		 * @var      object    $bpchk_globals    The plugin settings.
		 */
		public $bpchk_globals;

		/**
		 * Initialize the class and set its properties.
		 *
		 * @since    1.0.0
		 * @param      string $plugin_name       The name of the plugin.
		 * @param      string $version    The version of this plugin.
		 */
		public function __construct( $plugin_name, $version ) {
			global $bpchk_globals;
			$this->plugin_name    = $plugin_name;
			$this->version        = $version;
			$this->bpchk_globals  = &$bpchk_globals;
		}

		/**
		 * Render location pickup html to buddypress what's new section.
		 *
		 * @since    1.0.7
		 */
		public function render_location_pickup_html() {
			global $bpchk_globals;
			global $allowedposttags;
			$checkin_html = '';
			if ( is_user_logged_in() ) {

				if ( ! self::bp_checkins_is_youzer_activity() ) {
					return;
				}

				// Create the checkin html.
				if ( $bpchk_globals->apikey ) {
					$checkin_html .= '<div class="post-elements-buttons-item bpchk-marker-container"><div class="checkins-icon bp-tooltip" data-bp-tooltip-pos="up" data-bp-tooltip="' . esc_attr__( 'Add a place', 'bp-check-in' ) . '"><i class="wb-icons wb-icon-map-pin bpchk-allow-checkin"></i></div></div>';
				}
			}
			echo wp_kses_post( $checkin_html );
		}

		/**
		 * Render location pickup map panle html.
		 *
		 * @since    1.9.2
		 */
		public function render_location_map_panle_html( $activity = [] ) {
			global $bpchk_globals, $allowedposttags;			
			$checkin_html = '';
			if ( is_user_logged_in() ) {

				if ( ! self::bp_checkins_is_youzer_activity() ) {
					return;
				}
				$place = $latitude = $longitude = '';
				$add_as_my_place = 'yes';
				 
				// Create the checkin html.
				if ( $bpchk_globals->apikey ) {
					$add_cat_nonce = wp_create_nonce( 'bpchk-checkins-activity-post' );
					$checkin_html .= '<div class="bp-checkins bp-checkin-panel">';
					
					$checkin_html     .= '<div class="checkin-by-autocomplete">';
						$checkin_html .= '<input type="text" id="bpchk-autocomplete-place" name="bp-checkin-address" placeholder="' . esc_attr__( 'Start typing your location...', 'bp-check-in' ) . '" value="' . esc_attr($place). '" />';
						$checkin_html .= '<input type="hidden" name="bp-checkin-lat" id="bpchk-checkin-place-lat" value="' . esc_attr($latitude). '"/>';
						$checkin_html .= '<input type="hidden" name="bp-checkin-lng" id="bpchk-checkin-place-lng" value="' . esc_attr($longitude). '"/>';
						$checkin_html .= '<input type="hidden" name="bpchk-checkins-activity-nonce" value="' . esc_attr( $add_cat_nonce ) . '">';
						$checkin_html .= '<input type="checkbox" name="bp-checkin-add-as-place" value="yes" id="bpchk-add-as-place" '. checked($add_as_my_place, 'yes', false).' />';
						$checkin_html .= '<label for="bpchk-add-as-place">' . esc_html__( 'Add this location to Check-ins tab', 'bp-check-in' ) . '</label>';						
					$checkin_html     .= '</div>';
					$checkin_html     .= '<div class="checkin-by-autocomplete-map" id="checkin-by-autocomplete-map"></div>';										
					$checkin_html     .= '<div class="clear"></div>';

					$checkin_html .= '</div>';

					wp_nonce_field('bpchk-checkins-activity-post-action', 'bpchk-checkins-activity-post-value');
				}
			}
			 $allowed_tags = array(
				'div'    => array(
					'class' => array(),
					'id'    => array(),
				),
				'input'  => array(
					'type'        => array(),
					'id'          => array(),
					'name'        => array(),
					'placeholder' => array(),
					'value'       => array(),
					'checked'     => array(),
				),
			);
			echo wp_kses( $checkin_html, $allowed_tags );
		}


		/**
		 * Register the stylesheets for the public-facing side of the site.
		 *
		 * @since    1.0.0
		 */
		public function enqueue_styles() {
			global $bpchk_globals, $post;

			$rtl_css = is_rtl() ? '-rtl' : '';

			$extension = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '.css' : '.min.css';

			$current_component = '';
			if ( isset( $post->ID ) && $post->ID != '' && $post->ID != '0' ) {
				$_elementor_data = get_post_meta( $post->ID, '_elementor_data', true );
				if ( $_elementor_data != '' && str_contains($_elementor_data, 'bp_newsfeed_element_widget') || str_contains($_elementor_data, 'buddypress_shortcode_activity_widget') || str_contains($_elementor_data, 'bbp-activity')) {
					$current_component = 'activity';
				}
			}

			$checkin_tab_slug = isset( $bpchk_globals->tab_name ) ? $bpchk_globals->tab_name : 'checkin';
			$checkin_tab_slug = apply_filters( 'bpchk_member_profile_checkin_tab_slug', sanitize_title( $checkin_tab_slug ) );
			wp_register_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css' . $rtl_css . '/bp-checkins-public' . $extension, array(), $this->version, 'all' );
			
			if ( bpchk_enqueue_checkins_assets( $post, $checkin_tab_slug, $current_component ) ) {				
				wp_enqueue_style( $this->plugin_name . '-ui-css', plugin_dir_url( __FILE__ ) . 'css/vendor/jquery-ui.css', array(), $this->version, 'all' );
				wp_enqueue_style( $this->plugin_name . '-font-awesome', plugin_dir_url( __FILE__ ) . 'css/vendor/font-awesome.min.css', array(), $this->version, 'all' );
				wp_enqueue_style( $this->plugin_name );
			}

			wp_register_style(
				$handle = 'wb-icons',
				$src    = plugin_dir_url( __FILE__ ) . 'css' .$rtl_css. '/wb-icons' . $extension,
				$deps   = array(),
				$ver    = $this->version,
				$media  = 'all'
			);

			if ( ! wp_style_is( 'wb-icons', 'enqueued' ) ) {
				wp_enqueue_style( 'wb-icons' );
			}
		}

		/**
		 * Register the JavaScript for the public-facing side of the site.
		 *
		 * @since    1.0.0
		 */
		public function enqueue_scripts() {
			
			global $bpchk_globals, $post;

			$extension = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '.js' : '.min.js';
			$path      = ( $extension === '.js' ) ? '' : '/min';
			
			$current_component = '';
			if ( isset( $post->ID ) && $post->ID != '' && $post->ID != '0' ) {
				$_elementor_data = get_post_meta( $post->ID, '_elementor_data', true );
				if ( $_elementor_data != '' && str_contains($_elementor_data, 'bp_newsfeed_element_widget') || str_contains($_elementor_data, 'buddypress_shortcode_activity_widget') || str_contains($_elementor_data, 'bbp-activity')) {
					$current_component = 'activity';
				}
			}

			$checkin_tab_slug = isset( $bpchk_globals->tab_name ) ? $bpchk_globals->tab_name : 'checkin';
			$checkin_tab_slug = apply_filters( 'bpchk_member_profile_checkin_tab_slug', sanitize_title( $checkin_tab_slug ) );
			wp_register_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js' .$path. '/bp-checkins-public' . $extension, array( 'jquery', 'jquery-ui-datepicker', 'wp-i18n' ), $this->version, false );			

			if ( bpchk_enqueue_checkins_assets( $post, $checkin_tab_slug, $current_component ) ) {	
				wp_enqueue_script( 'jquery-ui-accordion' );
				$locale  = get_locale();
				$region  = strlen( $locale ) >= 5 ? substr( $locale, 3, 2 ) : 'US'; // e.g., 'IN' from 'en_IN'
				$language = substr( $locale, 0, 2 ); // e.g., 'en' from 'en_IN'

				$google_url = add_query_arg(
					array(
						'v'        => 'weekly',
						'libraries'=> 'places',
						'key'      => $bpchk_globals->apikey,
						'region'   => $region,
						'language' => $language,
					),
					'https://maps.googleapis.com/maps/api/js'
				);

				wp_enqueue_script(
					$this->plugin_name . '-google-places-api',
					$google_url,
					array( 'jquery' ),
					$this->version,
					false
				);

				wp_enqueue_script( $this->plugin_name );
				wp_set_script_translations( $this->plugin_name, 'bp-check-in' );
				
				if ( is_user_logged_in() && bp_is_active( 'xprofile' ) ) {
					if ( xprofile_get_field_id_from_name( 'Location' ) ) {
						$bpchk_location_id = xprofile_get_field_id_from_name( 'Location' );
						$bpchk_loc_xprof   = 'field_' . $bpchk_location_id;
					}
				}
				if ( empty( $bpchk_loc_xprof ) ) {
					$bpchk_loc_xprof = '';
				}
				wp_localize_script(
					$this->plugin_name,
					'bpchk_public_js_obj',
					array(
						'ajaxurl'          => admin_url( 'admin-ajax.php' ),
						'checkin_by'       => $bpchk_globals->checkin_by,
						'bpchk_loc_xprof'  => $bpchk_loc_xprof,
						'buddyboss'        => buddypress()->buddyboss,
						'add_checkin_text' => __( 'Add a place', 'bp-check-in' ),
						'bpchk_ajax_nonce' => wp_create_nonce( 'ajax-nonce' ),
						'youzify_enabled'  => class_exists( 'Youzify' ),
					)
				);
			}
		}

		/**
		 * Register a new tab in member's profile - Checkin
		 *
		 * @since    1.0.1
		 */
		public function bpchk_member_profile_checkin_tab() {
			if ( is_user_logged_in() ) {

				global $bpchk_globals;
				$bpchk_settings = bp_get_option( 'bpchk_general_settings' );
				if ( isset( $bpchk_globals->tab_visibility ) && '1' !== $bpchk_globals->tab_visibility ) {
					return;
				}

				$displayed_uid    = bp_displayed_user_id();
				$parent_slug      = 'checkin';
				$my_places_link   = bp_core_get_userlink( $displayed_uid, false, true ) . $parent_slug . '/check-ins';
				$checkin_tab_name = isset( $bpchk_settings['tab_name'] ) ? $bpchk_settings['tab_name'] : '';
				$get_users_fav_places = get_user_meta( $displayed_uid, 'bpchk_fav_places', true );
				if ( !empty( $get_users_fav_places) ) {
					$count = count( $get_users_fav_places );
							$checkin_tab_name = sprintf( '%s <span class="count">%d</span>', 
								esc_html( $checkin_tab_name ), 
								$count 
							);
				}
				$checkin_tab_slug = isset( $bpchk_globals->tab_name ) ? $bpchk_globals->tab_name : 'checkin';

				bp_core_new_nav_item(
					array(
						'name'                    => apply_filters( 'bpchk_member_profile_checkin_tab_name', ( $checkin_tab_name ) ),
						'slug'                    => apply_filters( 'bpchk_member_profile_checkin_tab_slug', sanitize_title( $checkin_tab_slug ) ),
						'screen_function'         => array( $this, 'bpchk_checkins_activity_show_screen' ),
						'position'                => 75,
						'default_subnav_slug'     => 'check-ins',
						'show_for_displayed_user' => true,
					)
				);
			}
		}

		/**
		 * Screen function for listing all my places in menu item
		 */
		public function bpchk_checkins_activity_show_screen() {
			add_action( 'bp_template_content', array( $this, 'bpchk_checkins_tab_function_to_show_content' ) );
			bp_core_load_template( apply_filters( 'bpchk_core_template_plugin', 'members/single/plugins' ) );
		}

		/**
		 * My Places - Content
		 */
		public function bpchk_checkins_tab_function_to_show_content() {
			$file = BPCHK_PLUGIN_PATH . 'public/templates/checkin/bp-checkins-activity.php';
			if ( file_exists( $file ) ) {
				include_once $file;
			}
		}

		/**
		 * Return country from json data
		 *
		 * @param    array $jsondata    Google places api data.
		 */
		public static function google_get_country( $jsondata ) {
			return self::bpchk_find_long_name_given_type( 'country', $jsondata['results'][0]['address_components'] );
		}

		/**
		 * Return province from json data
		 *
		 * @param    array $jsondata    Google places api data.
		 */
		public static function google_get_province( $jsondata ) {
			return self::bpchk_find_long_name_given_type( 'administrative_area_level_1', $jsondata['results'][0]['address_components'], true );
		}

		/**
		 * Return city from json data
		 *
		 * @param    array $jsondata    Google places api data.
		 */
		public static function google_get_city( $jsondata ) {
			return self::bpchk_find_long_name_given_type( 'locality', $jsondata['results'][0]['address_components'] );
		}

		/**
		 * Return street from json data
		 *
		 * @param    array $jsondata    Google places api data.
		 */
		public static function google_get_street( $jsondata ) {
			return self::bpchk_find_long_name_given_type( 'street_number', $jsondata['results'][0]['address_components'] ) . ' ' . self::bpchk_find_long_name_given_type( 'route', $jsondata['results'][0]['address_components'] );
		}

		/**
		 * Return postal code from json data
		 *
		 * @param    array $jsondata    Google places api data.
		 */
		public static function google_get_postalcode( $jsondata ) {
			return self::bpchk_find_long_name_given_type( 'postal_code', $jsondata['results'][0]['address_components'] );
		}

		/**
		 * Return country code from json data
		 *
		 * @param    array $jsondata    Google places api data.
		 */
		public static function google_get_country_code( $jsondata ) {
			return self::bpchk_find_long_name_given_type( 'country', $jsondata['results'][0]['address_components'], true );
		}

		/**
		 * Return formatted address from json data
		 *
		 * @param    array $jsondata    Google places api data.
		 */
		public static function google_get_address( $jsondata ) {
			return $jsondata['results'][0]['formatted_address'];
		}

		/**
		 * Searching in Google Geo json, return the long name given the type.
		 * (If short_name is true, return short name)
		 *
		 * @param    string  $type  The type of the place.
		 * @param    array   $array    The place type array.
		 * @param    boolean $short_name    Short name exist.
		 */
		public static function bpchk_find_long_name_given_type( $type, $array, $short_name = false ) {
			foreach ( $array as $value ) {
				if ( in_array( $type, $value['types'], true ) ) {
					if ( $short_name ) {
						return $value['short_name'];
					}
					return $value['long_name'];
				}
			}
		}


		/**
		 * Add location xprofile field.
		 *
		 * @since 1.0.1
		 */
		public function bpchk_add_location_xprofile_field() {
			global $bpchk_globals;

			// Check if the BuddyPress XProfile component is active, exit the function if it's not.
			if( ! bp_is_active( 'xprofile' ) ){
				return;
			}

			if ( ! isset( $bpchk_globals->enable_location_field ) && '1' !== $bpchk_globals->enable_location_field ) {
				return;
			}

			if ( xprofile_get_field_id_from_name( 'Location' ) ) {
				return;
			}			

			$location_list_args = array(
				'field_group_id' => 1,
				'type'           => 'textbox',
				'name'           => 'Location',
				'description'    => 'Please select your location',
				'is_required'    => false,
				'can_delete'     => true,
				'order_by'       => 'default',
			);
			xprofile_insert_field( $location_list_args );

			if ( function_exists( 'bp_core_xprofile_update_profile_completion_user_progress' ) ) {
				bp_core_xprofile_update_profile_completion_user_progress();
			}
		}

		/**
		 * Ajax request to save location xprofile field.
		 *
		 * @since 1.0.1
		 */
		public function bpchk_save_xprofile_location() {
			if ( isset( $_POST['nonce'] ) && ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'ajax-nonce' ) ) {
				return;
			}
			$action = isset( $_POST['action'] ) ? sanitize_text_field( wp_unslash( $_POST['action'] ) ) : '';
			if ( $action && 'bpchk_save_xprofile_location' === $action ) {
				$args = array(
					'place'     => isset( $_POST['place'] ) ? sanitize_text_field( wp_unslash( $_POST['place'] ) ) : '',					
					'latitude'  => isset( $_POST['latitude'] ) ? sanitize_text_field( wp_unslash( $_POST['latitude'] ) ) : '',					
					'longitude' => isset( $_POST['longitude'] ) ? sanitize_text_field( wp_unslash( $_POST['longitude'] ) ) : '',
				);
				if ( xprofile_get_field_id_from_name( 'Location' ) ) {
					$bpchk_location_id = xprofile_get_field_id_from_name( 'Location' );
					bp_xprofile_update_meta( $bpchk_location_id, 'data', 'bpchk_loc_xprofile', $args );
				}
			}
			exit;
		}

		/**
		 * Function to filter location xprofile field value at profile page.
		 *
		 * @since 1.0.1
		 * @param string $field_value Value for the profile field.
		 * @param string $field_type  Type for the profile field.
		 * @param int    $field_id    ID for the profile field.
		 */
		public function bpchk_show_xprofile_location( $field_value, $field_type, $field_id ) {
			global $bpchk_globals;
			if ( xprofile_get_field_id_from_name( 'Location' ) ) {
				$bpchk_location_id = xprofile_get_field_id_from_name( 'Location' );
				if ( $field_id === $bpchk_location_id ) {
					$loc_xprof_meta = bp_xprofile_get_meta( $bpchk_location_id, 'data', 'bpchk_loc_xprofile' );
					if ( ! empty( $loc_xprof_meta ) && is_array( $loc_xprof_meta ) ) {

						$field_value = '<a class=checkin-loc href="https://maps.google.com/maps/place/' . $loc_xprof_meta['place'] . '/@' . $loc_xprof_meta['latitude'] . ',' . $loc_xprof_meta['longitude'] . '" target="_blank" title="' . $loc_xprof_meta['place'] . '">' . $loc_xprof_meta['place'] . '</a>';
						return $field_value;
					}
				}
				return $field_value;
			} else {
				return $field_value;
			}
		}

		/**
		 * Function to add checkin activity types.
		 *
		 * @since 1.0.1
		 * @param array $types Value for the profile field.
		 */
		public function bpchk_add_checkin_activity_type( $types ) {
			$types[] = 'activity_bpchk_chkins';
			return $types;
		}

		/**
		 * Function to register activity action.
		 *
		 * @since 1.0.1
		 */
		public function custom_plugin_register_activity_actions() {

			$component_id = buddypress()->activity->id;

			bp_activity_set_action(
				$component_id,
				'activity_bpchk_chkins',
				__( 'Check-ins Update', 'bp-check-in' ),
				array( $this, 'bp_activity_format_activity_action_activity_bpchk_chkins' ),
				__( 'Check-ins', 'bp-check-in' ),
				array( 'activity', 'member', 'group', 'member_groups' )
			);
		}

		/**
		 * Format 'activity_update' activity actions.
		 *
		 * @since 1.0.1
		 *
		 * @param string $action   Static activity action.
		 * @param object $activity Activity data object.
		 * @return string $action
		 */
		public function bp_activity_format_activity_action_activity_bpchk_chkins( $action, $activity ) {

			$action = sprintf(
				/* translators: s: User name */
				__( '%s checked-in', 'bp-check-in' ),
				bp_core_get_userlink( $activity->user_id )
			);

			/**
			 * Filters the formatted activity action update string.
			 *
			 * @since 1.2.0
			 *
			 * @param string               $action   Activity action string value.
			 * @param BP_Activity_Activity $activity Activity item object.
			 */
			return apply_filters( 'bpchk_activity_new_checkin_action', $action, $activity );
		}

		/**
		 * Function to set activity type activity_bpchk_chkins.
		 *
		 * @since 1.0.1
		 * @param array $activity_object Activity object.
		 */
		public function bpchk_update_activity_type_checkins( $activity_object ) {
			 // Verify the nonce
			if ( ! isset( $_POST['bpchk-checkins-activity-nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['bpchk-checkins-activity-nonce'] ) ), 'bpchk-checkins-activity-post' ) ) {
				return; // Nonce failed, so stop execution
			}			
			$place_details = array();
			$latitude  = ( isset( $_POST['bp-checkin-lat'] ) && is_numeric( $_POST['bp-checkin-lat'] ) ) ? floatval( $_POST['bp-checkin-lat'] ) : 0;			
			$longitude = ( isset( $_POST['bp-checkin-lng'] ) && is_numeric( $_POST['bp-checkin-lng'] ) ) ? floatval( $_POST['bp-checkin-lng'] ) : 0;

			if ( isset( $_POST['bp-checkin-address'] ) && $_POST['bp-checkin-address'] != '' ) { //phpcs:ignore
				$place_details['place']           = isset( $_POST['bp-checkin-address'] ) ? sanitize_text_field( wp_unslash( $_POST['bp-checkin-address'] ) ) : ''; //phpcs:ignore
				$place_details['latitude']        = $latitude ; //phpcs:ignore
				$place_details['longitude']       = $longitude;
				$place_details['add_as_my_place'] = isset( $_POST['bp-checkin-add-as-place'] ) ? sanitize_text_field( wp_unslash( $_POST['bp-checkin-add-as-place'] ) ) : ''; //phpcs:ignore 
			}
			if ( ! empty( $place_details ) ) {			
				$activity_object->type = 'activity_bpchk_chkins';

				if( $activity_object->component === 'groups' ) {
					$user_link  = bp_core_get_userlink( $activity_object->user_id );
					$group      = groups_get_group( $activity_object->item_id );
					
					if( function_exists( 'buddypress' ) && isset( buddypress()->buddyboss ) ) {
						$group_link = bp_get_group_permalink( $group );
						$group_html = '<a href="' . esc_url( $group_link ) . '">' . esc_html( $group->name ) . '</a>';
					} else {
						$group_link = bp_get_group_url( $group );
						$group_html = '<a href="' . esc_url( $group_link ) . '">' . esc_html( $group->name ) . '</a>';
					}
					
					// Create the action string with proper HTML structure
					$action = sprintf(
						/* translators: 1: User link HTML, 2: Group link HTML */
						__( '%1$s checked-in in the group %2$s', 'bp-check-in' ),
						$user_link,  // This already contains <a> tag from bp_core_get_userlink()
						$group_html  // This contains <a> tag we built
					);
					
					$activity_object->action = $action;
				}
			}
		}

		/**
		 * Action performed to save the activity update to show the checkin.
		 *
		 * @param string $content The actvity content.
		 * @param int    $user_id User id.
		 * @param int    $activity_id Activity id.
		 * @since 1.0.1
		 */
		public function bpchk_update_meta_on_post_update( $content, $user_id, $activity_id ) {			
			global $wpdb, $bpchk_globals;

			$activity_tbl = $wpdb->base_prefix . 'bp_activity';
			$apikey       = $bpchk_globals->apikey;

			$place_details = array();

			 // Verify the nonce
			if ( ! isset( $_POST['bpchk-checkins-activity-post-value'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['bpchk-checkins-activity-post-value'] ) ), 'bpchk-checkins-activity-post-action' ) ) {
				return; // Nonce failed, so stop execution
			}

			$latitude  = ( isset( $_POST['bp-checkin-lat'] ) && is_numeric( $_POST['bp-checkin-lat'] ) ) ? floatval( $_POST['bp-checkin-lat'] ) : 0;
			$longitude = ( isset( $_POST['bp-checkin-lng'] ) && is_numeric( $_POST['bp-checkin-lng'] ) ) ? floatval( $_POST['bp-checkin-lng'] ) : 0;

			if ( isset( $_POST['bp-checkin-address'] ) && $_POST['bp-checkin-address'] != '' ) {
				$place_details['place']           = isset( $_POST['bp-checkin-address'] ) ? sanitize_text_field( wp_unslash( $_POST['bp-checkin-address'] ) ) : '';
				$place_details['latitude']        = $latitude;
				$place_details['longitude']       = $longitude ;
				$place_details['add_as_my_place'] = isset( $_POST['bp-checkin-add-as-place'] ) ? sanitize_text_field( wp_unslash( $_POST['bp-checkin-add-as-place'] ) ) : '';
			}
			if ( ! empty( $place_details ) ) {
				$place           = $place_details['place'];
				$longitude       = $place_details['longitude'];
				$latitude        = $place_details['latitude'];
				$location_html = esc_html__( '-at', 'bp-check-in' ) . ' <a class="checkin-loc" href="%s" target="_blank" title="%s">%s</a>';
				$location_html = sprintf(
					$location_html,
					esc_url( 'https://maps.google.com/maps/place/' . urlencode( $place ) . '/@' . $latitude . ',' . $longitude ),
					esc_attr( $place ),
					esc_html( $place )
				);
				// Always update the place meta
				bp_activity_update_meta( $activity_id, 'bpchk_place_details', $place_details );

				// Remove any previous check-in location in content
				$content = preg_replace( '/-at <a class="checkin-loc".*?<\/a>/', '', $content );
				$content = trim( $content ) . ' ' . $location_html;

				// Update the activity content
				// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- BuddyPress activity content update requires direct DB access
				$wpdb->update(
					$activity_tbl,
					array( 'content' => $content ),
					array( 'id' => $activity_id ),
					array( '%s' ),
					array( '%d' )
				);
				// Clear BuddyPress activity cache for this activity.
				wp_cache_delete( 'bp_activity_' . $activity_id, 'bp' );
				if ( 'yes' === $place_details['add_as_my_place'] ) {
					$bpchk_fav_places = get_user_meta( $user_id, 'bpchk_fav_places', true );
					// If the meta does not exist or is not an array, initialize it
					if ( ! is_array( $bpchk_fav_places ) ) {
						$bpchk_fav_places = array();
					}
					

					$place_get_url    = 'https://maps.googleapis.com/maps/api/geocode/json?key=' . $apikey . "&latlng=$latitude,$longitude&sensor=false";
					$response         = wp_remote_get( $place_get_url );			

					$response_code = wp_remote_retrieve_response_code( $response );
					if ( 200 === $response_code ) {
						$jsondata         = json_decode( wp_remote_retrieve_body( $response ), true );
						$place_visit_date = gmdate( 'Y-m-d', time() );				

						if ( $jsondata['results'][0]['formatted_address'] ) {
							$address                      = array();
							$address['latitude']          = $latitude;
							$address['longitude']         = $longitude;
							$address['activity_id']       = $activity_id;
							$address['place']             = $place;
							$address['country']           = self::google_get_country( $jsondata );
							$address['province']          = self::google_get_province( $jsondata );
							$address['city']              = self::google_get_city( $jsondata );
							$address['street']            = self::google_get_street( $jsondata );
							$address['postal_code']       = self::google_get_postalcode( $jsondata );
							$address['country_code']      = self::google_get_country_code( $jsondata );
							$address['formatted_address'] = self::google_get_address( $jsondata );
							$address['visit_date']        = $place_visit_date;

							$location_exists = false;

							foreach ( $bpchk_fav_places as $key => $place ) {
								if (
									isset( $place['latitude'], $place['longitude'] ) &&
									floatval( $place['latitude'] ) === floatval( $address['latitude'] ) &&
									floatval( $place['longitude'] ) === floatval( $address['longitude'] )
								) {
									// Update existing favorite location
									$bpchk_fav_places[ $key ] = $address;
									$location_exists = true;
									break;
								}
							}

							// If not found, append as new favorite
							if ( ! $location_exists ) {
								$bpchk_fav_places[] = $address;
							}

							// Update the user meta with the new array					
							update_user_meta( $user_id, 'bpchk_fav_places', $bpchk_fav_places );
						}
					}
				}
			}
		}

		/**
		 * Action performed to save the group activity update to show the checkin.
		 *
		 * @param string $content The group activity content.
		 * @param int    $user_id The user id.
		 * @param int    $group_id The group id.
		 * @param int    $activity_id The group id.
		 * @since 1.0.1
		 */
		public function bpchk_update_group_meta_on_post_update( $content, $user_id, $group_id, $activity_id ) {			
			global $wpdb, $bpchk_globals;

		

			// Verify the nonce
			if ( ! isset( $_POST['bpchk-checkins-activity-nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['bpchk-checkins-activity-nonce'] ) ), 'bpchk-checkins-activity-post' ) ) {
				return; // Nonce failed, so stop execution
			}

			$activity_tbl = $wpdb->base_prefix . 'bp_activity';
			$apikey       = $bpchk_globals->apikey;
			$place_details = array();

			$latitude  = ( isset( $_POST['bp-checkin-lat'] ) && is_numeric( $_POST['bp-checkin-lat'] ) ) ? floatval( $_POST['bp-checkin-lat'] ) : 0;
			$longitude = ( isset( $_POST['bp-checkin-lng'] ) && is_numeric( $_POST['bp-checkin-lng'] ) ) ? floatval( $_POST['bp-checkin-lng'] ) : 0;

			if ( isset( $_POST['bp-checkin-address'] ) && $_POST['bp-checkin-address'] != '' ) {
				$place_details['place']           = isset( $_POST['bp-checkin-address'] ) ? sanitize_text_field( wp_unslash( $_POST['bp-checkin-address'] ) ) : '';
				$place_details['latitude']        = $latitude;
				$place_details['longitude']       = $longitude;
				$place_details['add_as_my_place'] = isset( $_POST['bp-checkin-add-as-place'] ) ? sanitize_text_field( wp_unslash( $_POST['bp-checkin-add-as-place'] ) ) : '';
			}

			if ( ! empty( $place_details ) ) {
				$place           = $place_details['place'];
				$longitude       = $place_details['longitude'];
				$latitude        = $place_details['latitude'];
				$add_as_my_place = $place_details['add_as_my_place'];

				$location_html = esc_html__( '-at', 'bp-check-in' ) . ' <a class="checkin-loc" href="%s" target="_blank" title="%s">%s</a>';
				$location_html = sprintf(
					$location_html,
					esc_url( 'https://maps.google.com/maps/place/' . urlencode( $place ) . '/@' . $latitude . ',' . $longitude ),
					esc_attr( $place ),
					esc_html( $place )
				);
				$content = preg_replace( '/-at <a class="checkin-loc".*?<\/a>/', '', $content );
				$content = trim( $content ) . ' ' . $location_html;

					// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- BuddyPress activity content update requires direct DB access
					$wpdb->update(
						$activity_tbl,
						array( 'content' => $content ),
						array( 'id' => $activity_id ),
						array( '%s' ),
						array( '%d' )
					);
					// Clear BuddyPress activity cache for this activity.
					wp_cache_delete( 'bp_activity_' . $activity_id, 'bp' );

					// Update the location details in activity meta.
					bp_activity_update_meta( $activity_id, 'bpchk_place_details', $place_details );

				if ( 'yes' === $add_as_my_place ) {
					$bpchk_fav_places = get_user_meta( $user_id, 'bpchk_fav_places', true );
					// If the meta does not exist or is not an array, initialize it
					if ( ! is_array( $bpchk_fav_places ) ) {
						$bpchk_fav_places = array();
					}
					
					$place_get_url    = 'https://maps.googleapis.com/maps/api/geocode/json?key=' . $apikey . "&latlng=$latitude,$longitude&sensor=false";
					$response         = wp_remote_get( $place_get_url );					

					$response_code = wp_remote_retrieve_response_code( $response );

					if ( 200 === $response_code ) {
						$jsondata         = json_decode( wp_remote_retrieve_body( $response ), true );
						$place_visit_date = gmdate( 'Y-m-d', time() );

						if ( isset( $jsondata['results'][0]['formatted_address'] ) ) {
							$address                      = array();
							$address['latitude']          = $latitude;
							$address['longitude']         = $longitude;
							$address['activity_id']       = $activity_id;
							$address['place']             = $place;
							$address['country']           = self::google_get_country( $jsondata );
							$address['province']          = self::google_get_province( $jsondata );
							$address['city']              = self::google_get_city( $jsondata );
							$address['street']            = self::google_get_street( $jsondata );
							$address['postal_code']       = self::google_get_postalcode( $jsondata );
							$address['country_code']      = self::google_get_country_code( $jsondata );
							$address['formatted_address'] = self::google_get_address( $jsondata );
							$address['visit_date']        = $place_visit_date;

							$location_exists = false;

							foreach ( $bpchk_fav_places as $key => $place ) {
								if (
									isset( $place['latitude'], $place['longitude'] ) &&
									floatval( $place['latitude'] ) === floatval( $address['latitude'] ) &&
									floatval( $place['longitude'] ) === floatval( $address['longitude'] )
								) {
									// Update existing favorite location
									$bpchk_fav_places[ $key ] = $address;
									$location_exists = true;
									break;
								}
							}

							// If not found, append as new favorite
							if ( ! $location_exists ) {
								$bpchk_fav_places[] = $address;
							}

							// Update the user meta with the new array					
							update_user_meta( $user_id, 'bpchk_fav_places', $bpchk_fav_places );
						}
					}
				}
			}
		}

		/**
		 * To set activity action for check-in type activity in group.
		 *
		 * @param string $activity_action The group activity action.
		 * @since 1.0.1
		 */
		public function bpchk_groups_activity_new_update_action( $activity_action ) {
			global $bp;
			$user_id = bp_loggedin_user_id();
			if ( ! isset( $_POST['bp-checkin-address'] ) ) {
				return $activity_action;
			}
			$place_details = array();			
			// Verify the nonce
			if ( ! isset( $_POST['bpchk-checkins-activity-nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['bpchk-checkins-activity-nonce'] ) ), 'bpchk-checkins-activity-post' ) ) {
				return; // Nonce failed, so stop execution
			}
			$latitude  = ( isset( $_POST['bp-checkin-lat'] ) && is_numeric( $_POST['bp-checkin-lat'] ) ) ? floatval( $_POST['bp-checkin-lat'] ) : 0;			
			$longitude = ( isset( $_POST['bp-checkin-lng'] ) && is_numeric( $_POST['bp-checkin-lng'] ) ) ? floatval( $_POST['bp-checkin-lng'] ) : 0;

			if ( isset( $_POST['bp-checkin-address'] ) && $_POST['bp-checkin-address'] != '' ) {
				$place_details['place']           = isset( $_POST['bp-checkin-address'] ) ? sanitize_text_field( wp_unslash( $_POST['bp-checkin-address'] ) ) : '';
				$place_details['latitude']        = $latitude;
				$place_details['longitude']       = $longitude;
				$place_details['add_as_my_place'] = isset( $_POST['bp-checkin-add-as-place'] ) ? sanitize_text_field( wp_unslash( $_POST['bp-checkin-add-as-place'] ) ) : '';
			}
			if ( ! empty( $place_details ) ) {
			if ( function_exists('bp_get_version') && version_compare(bp_get_version(), '12.0.0', '>=') ) {
					/* translators: %s: */
					$activity_action = sprintf( __( '%1$s checked-in in the group %2$s', 'bp-check-in' ), bp_core_get_userlink( $user_id ), '<a href="' . bp_get_group_url( $bp->groups->current_group ) . '">' . esc_attr( $bp->groups->current_group->name ) . '</a>' );	
				}else{
				/* translators: %s: */
					$activity_action = sprintf( __( '%1$s checked-in in the group %2$s', 'bp-check-in' ), bp_core_get_userlink( $user_id ), '<a href="' . bp_get_group_permalink( $bp->groups->current_group ) . '">' . esc_attr( $bp->groups->current_group->name ) . '</a>' );
				}
			}
			return $activity_action;
		}

		/**
		 * Show mep on checkin activities
		 *
		 * @since 1.0.1
		 */
		public function bpchk_show_google_map_in_checkin_activity() {
			$activity_id = bp_get_activity_id();
			global $wpdb, $bpchk_globals;
			$activity_meta_tbl = $wpdb->base_prefix . 'bp_activity_meta';
			$place             = bp_activity_get_meta( $activity_id, 'bpchk_place_details', true );
			if ( ! empty( $place )  ) {
				$apikey        = $bpchk_globals->apikey;	
				$latitude  = ( isset( $place['latitude'] ) && is_numeric( $place['latitude'] ) )  ? floatval( $place['latitude'] ) : 0;				
				$longitude = ( isset( $place['longitude'] ) && is_numeric( $place['longitude'] ) ) ? floatval( $place['longitude'] ) : 0;
				
				$place_get_url = "https://maps.googleapis.com/maps/api/geocode/json?latlng=$latitude,$longitude&sensor=false";
				$response      = wp_remote_get( $place_get_url );			

				$response_code = wp_remote_retrieve_response_code( $response );

				$formatted_address = $place['place'];
				if ( 200 === $response_code ) {
					$jsondata = json_decode( wp_remote_retrieve_body( $response ), true );
					if ( isset( $jsondata['results'][0]['formatted_address'] ) ) {
						$formatted_address = self::google_get_address( $jsondata );

					}
				}
				$map_url = 'https://www.google.com/maps/embed/v1/place?key=' . $apikey . '&q=' . $formatted_address;
				echo '<div id="bpchk-place-map-' . esc_attr($activity_id). '" class="bpchk-place-map" activity-id='.esc_attr($activity_id).'><iframe frameborder="0" style="border:0" src="' . esc_url( $map_url ) . '" allowfullscreen></iframe></div>';
			}
		}
		public function bpchk_show_google_map_in_checkin_emded_activity( $content, $activity_content ) {
			ob_start();
			$activity_id = bp_get_activity_id();
			global $wpdb, $bpchk_globals;
			$activity_meta_tbl = $wpdb->base_prefix . 'bp_activity_meta';
			$place             = bp_activity_get_meta( $activity_id, 'bpchk_place_details', true );

			if ( ! empty( $place ) ) {
				$apikey        = $bpchk_globals->apikey;
				$latitude      = ( isset( $place['latitude'] ) ) ? $place['latitude'] : '';
				$longitude     = ( isset( $place['longitude'] ) ) ? $place['longitude'] : '';
				$place_get_url = "https://maps.googleapis.com/maps/api/geocode/json?latlng=$latitude,$longitude&sensor=false";
				$response      = wp_remote_get( $place_get_url );
				$response_code     = wp_remote_retrieve_response_code( $response );
				$formatted_address = $place['place'];
				if ( 200 === $response_code ) {
					$jsondata = json_decode( wp_remote_retrieve_body( $response ), true );
					if ( isset( $jsondata['results'][0]['formatted_address'] ) ) {
						$formatted_address = self::google_get_address( $jsondata );
					}
				}
				$map_url = 'https://www.google.com/maps/embed/v1/place?key=' . $apikey . '&q=' . $formatted_address;
				echo '<div id="bpchk-place-map-' . esc_attr($activity_id). '" class="bpchk-place-map" activity-id='.esc_attr($activity_id).'><iframe frameborder="0" style="border:0" src="' . esc_url( $map_url ) . '" allowfullscreen></iframe></div>';
			}
			return $content . ob_get_clean();
		}

		/**
		 * Ajax served to delete checkin locations from check-ins tab.
		 */
		public function bpchk_delete_user_checkin_location() {
			
			if ( isset( $_POST['ajaxNonce'] ) && ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['ajaxNonce'] ) ), 'ajax-nonce' ) ) {
				wp_send_json_error( array( 'message' => 'Invalid nonce' ) );
			}
			global $bp;
			$checkin_id       = isset( $_POST['checkin_id'] ) ? wp_unslash( $_POST[ 'checkin_id' ] ) : array(); //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
			$activity_id      = !empty( $checkin_id ) ? filter_var_array( $checkin_id, FILTER_SANITIZE_SPECIAL_CHARS ) : array();	
			$bpchk_fav_places = get_user_meta( bp_loggedin_user_id(), 'bpchk_fav_places', true );
			
			if ( ! empty( $bpchk_fav_places ) && !empty( $activity_id ) ) {
				foreach( $activity_id as $act_key => $activity_id ){
					 $activity = bp_activity_get_specific( array( 'activity_ids' => $activity_id ) );
					if ( ! empty( $activity['activities'] ) && $activity['activities'][0]->user_id != bp_loggedin_user_id() ) {
							wp_send_json_error( array( 'message' => 'Unauthorized access' ) );
					}
					foreach ( $bpchk_fav_places as $key => $fav_places ) {				
						if ( $fav_places['activity_id'] == $activity_id ) {					
							unset( $bpchk_fav_places[ $key ] ); // Unset the array key
						}
					}
				}				
				
				// Reindex the array after unsetting
				$bpchk_fav_places = array_values( $bpchk_fav_places );
				
				// Update the user meta
				update_user_meta( bp_loggedin_user_id(), 'bpchk_fav_places', $bpchk_fav_places );
			}
			wp_send_json_success( array( 'message' => 'Check-in(s) deleted successfully.' ) );
		}

		/**
		 * Function to be called when user click on read more button in activity content and appends the google map in activity content.
		 * @since 2.3.1
		 * 
		 */
		public function bpchk_checkin_activity_read_more() {
			if ( isset( $_POST['ajaxNonce'] ) && ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['ajaxNonce'] ) ), 'ajax-nonce' ) ) {
				exit();
			}
			if( class_exists( 'Youzify' ) ) {
				exit();
			}

			$activity_id = !empty( $_POST['activity_id'] ) ? filter_var( wp_unslash( $_POST['activity_id'] ), FILTER_SANITIZE_SPECIAL_CHARS ) : '';	
			global $wpdb, $bpchk_globals;
			$activity_meta_tbl = $wpdb->base_prefix . 'bp_activity_meta';
			$place             = bp_activity_get_meta( $activity_id, 'bpchk_place_details', true );
			if ( ! empty( $place )  ) {
				$apikey        = $bpchk_globals->apikey;	
				$latitude  = ( isset( $place['latitude'] ) && is_numeric( $place['latitude'] ) )  ? floatval( $place['latitude'] ) : 0;				
				$longitude = ( isset( $place['longitude'] ) && is_numeric( $place['longitude'] ) ) ? floatval( $place['longitude'] ) : 0;
				
				$place_get_url = "https://maps.googleapis.com/maps/api/geocode/json?latlng=$latitude,$longitude&sensor=false";
				$response      = wp_remote_get( $place_get_url );		

				$response_code = wp_remote_retrieve_response_code( $response );

				$formatted_address = $place['place'];
				if ( 200 === $response_code ) {
					$jsondata = json_decode( wp_remote_retrieve_body( $response ), true );
					if ( isset( $jsondata['results'][0]['formatted_address'] ) ) {
						$formatted_address = self::google_get_address( $jsondata );

					}
				}
				$map_url = 'https://www.google.com/maps/embed/v1/place?key=' . $apikey . '&q=' . $formatted_address;
				echo '<div id="bpchk-place-map-' . esc_attr($activity_id). '" class="bpchk-place-map" activity-id='.esc_attr($activity_id).'><iframe frameborder="0" style="border:0" src="' . esc_url( $map_url ) . '" allowfullscreen></iframe></div>';
			}
			wp_die();

		}

		/**
		 * Add buddypress chckin activity on yozer activity
		 *
		 * @param array $post_types
		 * @return array
		 */
		public function bp_checkin_allow_youzer_activity( $post_types ) {
			if ( is_array( $post_types ) ) {
				array_push( $post_types, 'activity_bpchk_chkins' );
			}
			return $post_types;
		}

			/**
			 * Static function to check if youzer wall option enable or not.
			 *
			 * @return boolean
			 */
		public static function bp_checkins_is_youzer_activity() {
			$is_enable = true;
			if ( class_exists( 'Youzer' ) ) {
				$unallowed_activities = yz_option( 'yz_unallowed_activities' );
				if ( ! empty( $unallowed_activities ) ) {
					$unallowed_activities = (array) array_flip( $unallowed_activities );

					if ( ! array_key_exists( 'activity_bpchk_chkins', $unallowed_activities ) ) {
						$is_enable = true;
					} else {
						$is_enable = false;
					}
				}
			}
			return $is_enable;
		}

		public function bp_checkin_hide_profile_field( $retval ) {
			global $bpchk_globals;
			
			if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
				$field_id = xprofile_get_field_id_from_name( 'Location' );
				
				$excludes = array_filter( array_map( 'trim', explode( ',', $retval['exclude_fields'] ?? '' ) ) );
				$excludes[] = (string) $field_id;
				$excludes = array_unique( $excludes );

				$retval['exclude_fields'] = implode( ',', $excludes );

				return $retval;
			}
			
			if ( '1' === $bpchk_globals->enable_location_field ) {
			
				$field_id = xprofile_get_field_id_from_name( 'Location' );

				// hide the field on profile view tab
				if ( ! bp_is_user_profile_edit() ) {
					$retval['exclude_fields'] = $field_id; // ID's separated by comma
				}

				// hide the field on profile edit tab
				if ( bp_is_user_profile_edit() ) {
					$retval['exclude_fields'] = $field_id; // ID's separated by comma
				}

				// allow field on register page
				if ( bp_is_register_page() ) {
					$retval['include_fields'] = $field_id; // ID's separated by comma
				}
			}
			return $retval;
		}

		/**
		 * Embedded activity content.
		 *
		 * @since BuddyPress 9.1.1
		 *
		 * @param string $activity_content  Embedded activity
		 */
		public function bp_checkin_activity_embed_add_inline_styles() {
			?>
			<style type="text/css">
				.bpchk-place-map {
					width: 100%;
					margin-top: 15px;
				}
				.bpchk-place-map iframe {
					width: 100%;
				}
			</style>
			<?php
		}
		
		/*
		 * Remove checking place from the content
		 */
		public function bp_checkin_get_edit_activity_content( $content, $activity) {
			if( $activity->type != 'activity_bpchk_chkins') {
				return $content;
			}
			$activity_id 			= $activity->id;
			$bpchk_place_details 	= bp_activity_get_meta( $activity_id, 'bpchk_place_details', true );
			$place					= isset($bpchk_place_details['place'])? $bpchk_place_details['place'] : '';			
			return str_replace('-at '.$place,'', $content);
		}
		
		public function bp_checkin_get_chkins_activity_content( $activity ) {
			if( $activity->type != 'activity_bpchk_chkins') {
				return;
			}
			
			$this->render_edit_activity_location_map_panle_html( $activity );
			?>
			<script>
				(function($) {
				'use strict';
					$(document).ready(function($){	
						initialize_edit_map();
					});
				})( jQuery );
			</script>
			<?php
		}
		
		/**
		 * Dispaly html for the edit activity.
		 *
		 * @param [type] $activity
		 * @return void
		 */
		public function render_edit_activity_location_map_panle_html( $activity ) {
			global $bpchk_globals, $allowedposttags;			
			$checkin_html = '';
			if ( is_user_logged_in() ) {

				if ( ! self::bp_checkins_is_youzer_activity() ) {
					return;
				}
				$place = $latitude = $longitude = '';
				$add_as_my_place = 'yes';
				if( !empty( $activity ) ) {
					$activity_id 	= $activity->id;			
					$bpchk_place_details 	= bp_activity_get_meta( $activity_id, 'bpchk_place_details', true );			
					$place					= (isset($bpchk_place_details['place'])) ? $bpchk_place_details['place'] : '';
					$latitude  = ( isset( $bpchk_place_details['latitude'] ) && is_numeric( $bpchk_place_details['latitude'] ) ) ? floatval( $bpchk_place_details['latitude'] ) : 0;					
					$longitude = ( isset( $bpchk_place_details['longitude'] ) && is_numeric( $bpchk_place_details['longitude'] ) ) ? floatval( $bpchk_place_details['longitude'] ) : 0;

					$add_as_my_place		= (isset($bpchk_place_details['add_as_my_place'])) ? $bpchk_place_details['add_as_my_place'] : '';
				}
				
				// Create the checkin html.
				if ( $bpchk_globals->apikey ) {
					$add_cat_nonce = wp_create_nonce( 'bpchk-checkins-activity-post' );
					$checkin_html .= '<div class="bp-checkins bp-checkin-panel">';

					$checkin_html     .= '<div class="checkin-by-autocomplete">';
						$checkin_html .= '<input type="text" id="bpchk-autocomplete-edit-place" name="bp-checkin-address" placeholder="' . __( 'Start typing your location...', 'bp-check-in' ) . '" value="' . esc_attr($place). '" />';
						$checkin_html .= '<input type="hidden" name="bp-checkin-lat" id="bpchk-checkin-edit-place-lat" value="' . esc_attr($latitude). '"/>';
						$checkin_html .= '<input type="hidden" name="bp-checkin-lng" id="bpchk-checkin-edit-place-lng" value="' . esc_attr($longitude). '"/>';
						$checkin_html .= '<input type="hidden" name="bpchk-checkins-activity-nonce" value="' . esc_attr( $add_cat_nonce ) . '">';
						$checkin_html .= '<input type="checkbox" name="bp-checkin-add-as-place" value="yes" id="bpchk-add-as-edit-place" '. checked($add_as_my_place, 'yes', false).' />';
						$checkin_html .= '<label for="bpchk-add-as-place">' . __( 'Add this location to Check-ins tab', 'bp-check-in' ) . '</label>';						
					$checkin_html     .= '</div>';
					$checkin_html     .= '<div class="checkin-by-autocomplete-map" id="checkin-by-autocomplete-edit-map"></div>';										
					$checkin_html     .= '<div class="clear"></div>';

					$checkin_html .= '</div>';
					$checkin_html .= wp_nonce_field('bpchk-checkins-activity-post-action', 'bpchk-checkins-activity-post-value',true , false);
				}
			}
			 $allowed_tags = array(
				'div'    => array(
					'class' => array(),
					'id'    => array(),
				),
				'input'  => array(
					'type'        => array(),
					'id'          => array(),
					'name'        => array(),
					'placeholder' => array(),
					'value'       => array(),
					'checked'     => array(),
				),
			);
			echo wp_kses( $checkin_html, $allowed_tags );
		}

	}
endif;
