Jump to content

JSON Sitemap


MuchDev
 Share

Recommended Posts

Hey guys, I know this isn't really a how to, but I am currently working on a system to inline above the fold css and was needing a json pagelist, and figured I would write something to automate it. I included the ability to flatten the tree or keep it expanded. There is also a filter section where you can exclude individual pages by path, parent, id, or template. This is all pretty bare bones and will pretty much just gives you a json array of your page paths, but that is all I needed. This is working code but you will need to clear out the filters for your own site. 

<?php 

/*  
 *  JSON SITEMAP GENERATOR
 *	By Sam Fleming -- MUCHDEV
 *  This will create a json sitemap for grunt or other purposes
 * 		
 *  option includes flat / expanded. Use however you need 
 *   
 *  apply whatever filters you need to exclude in the filter array
 *  curently supports excluding by:
 *   	Path
 *   	Parrent
 *   	ID
 *		Template
 */
 
 
###########################################################
## SET HERE IF YOU WANT AN EXPANDED OR FLATTENED SITEMAP ##
###########################################################
$flatten = true;

###########################################################
##            YOUR PAGE FILTERS GO HERE                  ##
########################################################### 
$filters = array(
    "paths" => array(
        "/processwire/"
    ),
    "parents" => array(
        "3211",
        "5",    
        "55",
        "98745"    
    ),
    "ids" => array(
        "1123",
        "5985",
        "321454",
        "15885",    
        "321115",
        "39"            
    ),    
    "templates" => array(
        "template1",
     "template2"
    )    
);
 
function buildSitemap($flatten) { 
	
	$home = wire('pages')->get('/');
	
	$urls[] = $home->httpUrl;	
	$urls[] = getChildren($home->children);
	
	if($flatten){
		$flatArray = arrayFlatten($urls);
		return json_encode($flatArray);		
	}else{
		return json_encode($urls);
	} 
	
}

function getChildren($pages){
	
	foreach($pages as $page){
		
		//iterate through all the filters in array, and if the type matches then set flag to true
		$flag = false;		
		
		foreach($filters as $filter){
			switch($filter){
				case "paths":
					foreach($filter as $path){
						if($page->path == $path) $flag = true;					
					}
					break;
				case "parents":	
					foreach($filter as $parrent){
						if($page->parrent == $parrent)$flag = true;													
					}				
					break;					
				case "ids":
					foreach($filter as $id){
						if($page->id == $id)$flag = true;													
					}			
					break;					
				case "templates":
					foreach($filter as $template){
						if($page->template == $template)$flag = true;													
					}				
					break;					
			}
		}
			
		if(!$flag) $urls[] = $page->httpUrl;
		
		//if page has children then recurse
		if($page->numChildren){
			$urls[] = getChildren($page->children);	
		}
	}
	
	return $urls;
}	

function arrayFlatten($array) {
    $return = array();
    foreach ($array as $key => $value) {
        if (is_array($value)){
            $return = array_merge($return, arrayFlatten($value));
        } else {
            $return[$key] = $value;
        }
    }

    return $return;
}

	echo buildSitemap($flatten);
?>

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...