-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache-function.php
More file actions
120 lines (105 loc) · 3.02 KB
/
cache-function.php
File metadata and controls
120 lines (105 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
// Create GeoJSON file
function jsonrequest_template_redirect() {
//Setup the and verify post type exist
if ( get_post_type( get_the_ID() ) == "location" && is_archive() )
{
global $query_string, $post;
//Set maximum number of post to 2000. This can be changed
query_posts( $query_string . '&order=DEC&posts_per_page=2000' );
}
$post_id = $post->ID;
//Receive the geo request call from the "Create Cache File" option
if(isset($_REQUEST['geo']))
{
if( have_posts() )
{
while( have_posts() )
{
the_post();
$output[]= array(
'title' => get_the_title(),
'meta' => get_post_custom(),
'id'=>get_the_id()
);
}
}
//define the file an place in the uploads directory
$upload_dir = wp_upload_dir();
//Current Cache file
$cacheFile = $upload_dir['basedir'].'/geojson.php';
/**
* This is where you specify how frequently you would like the change file to update
* +1 month = every month, +1 week = weekly, +1 day = every day, +1 hour = every hour etc..
* more info @ http://www.php.net/manual/en/function.strtotime.php
*/
$expiration_date = strtotime("+1 day");
$now = strtotime("now");
//Add time trigger to file. Once file is visited we check for expiration.
$callFile = get_site_url().'/location/?geo';
if(!empty($output))
{
$fileContents .= '
<?php
$expiration_date = '.$expiration_date.';
$now = strtotime("now");
if ( $expiration_date < $now ) {
$refresh_file = file_get_contents("'.$callFile.'");
if ( $refresh_file ) {
echo file_get_contents("'.$cacheFile.'");
}
} else {
?>
';
//Creating the geo JSON feed within the PHP file
$fileContents .= '
var locationMap = {
"type": "FeatureCollection",
"features": [
';
$count = 1;
foreach ($output as $key => $value)
{
$terms = get_the_terms( $value['id'], 'location_symbol' );
if(!empty($terms))
{
$term = array_pop($terms);
}
else
{
$term = NULL;
}
$fileContents .='
{
"geometry": {
"type": "Point",
"coordinates": ['.$value['meta']['coordinates'][0].']
},
"type": "Feature",
"properties": {
"name": "' . $value['title'] . '",
"link": "' . get_permalink( $value['id'] ) . '",
"symbol": "' . $term->name . '"
},
"id": '.$count.'
},
';
$count++;
}
$fileContents .='
]
};
<?php } ?>';
//writefile
file_put_contents("$cacheFile", $fileContents);
echo "A new cache file has been created at <b>".site_url()."/wp-content/uploads/geojson.php</b>. ";
die();
}
else
{
//Only write a file if we have created location post
echo "No locations added. Please add a location before creating a cache file";
die();
}
}
} // end template_redirect