<?php

/**
 * Users XML sitemap provider.
 *
 * @since 5.5.0
 */
class LH_Buddpress_sitemap_extender_class extends WP_Sitemaps_Provider {
    
    var $the_args = false;
    var $the_total = false;
    
    
	/**
	 * LH_Buddpress_sitemap_extender_class constructor.
	 *
	 * @since 5.5.0
	 */
	public function __construct() {
		$this->name        = 'groups';
		$this->object_type = 'group';
		
		//filter to capture args from bp_has_groups   
        add_filter( 'bp_after_has_groups_parse_args', array($this,'extract_args'), PHP_INT_MAX,1);
        bp_has_groups();
        global $groups_template;
        $this->the_total = $groups_template->total_group_count;
        remove_filter( 'bp_after_has_groups_parse_args', array($this,'extract_args'));
		
	}
	
	
	public function extract_args($args){
	    
	    $this->the_args = $args;
	    
	    return $args;
	}

	/**
	 * Gets a URL list for a user sitemap.
	 *
	 * @since 5.5.0
	 *
	 * @param int    $page_num       Page of results.
	 * @param string $object_subtype Optional. Not applicable for Users but
	 *                               required for compatibility with the parent
	 *                               provider class. Default empty.
	 * @return array Array of URLs for a sitemap.
	 */
	public function get_url_list( $page_num, $object_subtype = '' ) {
		/**
		 * Filters the users URL list before it is generated.
		 *
		 * Passing a non-null value will effectively short-circuit the generation,
		 * returning that value instead.
		 *
		 * @since 5.5.0
		 *
		 * @param array  $url_list The URL list. Default null.
		 * @param int    $page_num Page of results.
		 */
		$url_list = apply_filters(
			'wp_sitemaps_users_pre_url_list',
			null,
			$page_num
		);

		if ( null !== $url_list ) {
			return $url_list;
		}

if (!empty($page_num)){
    
	$args['paged'] = $page_num;
		
}

if ( bp_has_groups($args) ){







 while ( bp_groups() ) : bp_the_group();

			$sitemap_entry = array(
				'loc' => bp_get_group_permalink( $group ),
			);

			/**
			 * Filters the sitemap entry for an individual user.
			 *
			 * @since 5.5.0
			 *
			 * @param array   $sitemap_entry Sitemap entry for the user.
			 * @param WP_User $user          User object.
			 */
			$sitemap_entry = apply_filters( 'bp_groups_sitemap_entry', $sitemap_entry, $group );
			$url_list[]    = $sitemap_entry;
		endwhile;
		
}

		return $url_list;
	}

	/**
	 * Gets the max number of pages available for the object type.
	 *
	 * @since 5.5.0
	 *
	 * @see WP_Sitemaps_Provider::max_num_pages
	 *
	 * @param string $object_subtype Optional. Not applicable for Users but
	 *                               required for compatibility with the parent
	 *                               provider class. Default empty.
	 * @return int Total page count.
	 */
	public function get_max_num_pages( $object_subtype = '' ) {
	    


		return (int) ceil( $this->the_total /  $this->the_args['per_page']);
	}

	
}


?>