This repository has been archived by the owner on Jun 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
RuntimeInfos.class.php
198 lines (175 loc) · 4.57 KB
/
RuntimeInfos.class.php
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?php
/**
* stores the RuntimeInfos class definition
*/
/**
* Manages a set of info to be printed as a json generally at the endo of the page
* @author etessore
* @version 1.0.1
* @package classes
*/
/*
* Changelog
* 1.0.1
* solved notice of undefined post on 404 pages
*/
class RuntimeInfos{
/**
* @var array the list of infos
*/
public $infos = array();
/**
* @var int the way we want the infos
*/
public $type;
/**
* @var int id of the post these infos refer to
*/
protected $id;
/**
*
* @var int use this as type to have a <script> tag in the footer
*/
const TYPE_FOOTER = 1;
/**
* @var int uset this as type to have an external js on an admin-ajax.php url
*/
const TYPE_AJAX = 2;
/**
* @var string the action parameter for the admin-ajax
*/
const ajax_action = 'runtime-infos';
/**
* Initializes the current set of infos with default values
*/
public function __construct(){
$this->set_type(self::TYPE_AJAX);
}
/**
* Sets the default values:
* postID, theme_url, upload_dir, home_url and ajaxurl
*/
public function set_standard_infos(){
$uploadDir = wp_upload_dir();
$uploadDir = $uploadDir['baseurl'];
$this
->set_type(self::TYPE_AJAX)
->add_info('postID', $this->get_the_ID())
->add_info('theme_url', get_template_directory_uri())
->add_info('upload_dir', $uploadDir)
->add_info('home_url', get_bloginfo('url'))
->add_info('ajaxurl', admin_url('admin-ajax.php'));
if(defined('ICL_LANGUAGE_CODE')){
$this->add_info('currentLanguage', substr(ICL_LANGUAGE_CODE, 0, 2));
}
$this->generate_unique_id();
set_transient($this->id, $this->infos);
if($this->type == self::TYPE_AJAX){
$this->load_assets();
}
}
/**
* Get the ID for the current post
*/
private function get_the_ID(){
if(is_404()) return '';
return get_the_ID();
}
/**
* Adds an info to the current set
* @param string $key the name of the info
* @param string $value the value of the info
* @return RuntimeInfos $this for chainability
*/
public function add_info($key, $value){
$this->infos[$key] = $value;
return $this;
}
/**
* Sets the type
* @param int $type
* @see RuntimeInfos::TYPE_AJAX
* @see RuntimeInfos::TYPE_FOOTER
*/
public function set_type($type){
$this->type = $type;
return $this;
}
/**
* Hook the print of this infos to Wordpress footer
* @return RuntimeInfos $this for chainability
*/
public function hook(){
switch($this->type){
case self::TYPE_AJAX:
add_action('wp_ajax_'.self::ajax_action, array(__CLASS__, 'ajax_callback'));
add_action('wp_ajax_nopriv_'.self::ajax_action, array(__CLASS__, 'ajax_callback'));
add_action('wp', array(&$this, 'set_standard_infos'));
break;
case self::TYPE_FOOTER:
add_action('wp_footer', array(&$this, 'the_markup'));
break;
}
return $this;
}
/**
* Registers and enqueue the js from admin-ajax.php
* This has to be called after RuntimeInfos::generate_unique_id()
*/
public function load_assets(){
if(!isset($this->id)){
wp_die('You have to call RuntimeInfos::generate_unique_id() before RuntimeInfos::load_assets()');
}
$nonce = wp_create_nonce("runtime_infos_nonce");
$src =
admin_url('admin-ajax.php')
.'?'
.http_build_query(
array(
'action' => self::ajax_action,
'id' => $this->id,
'nonce' => $nonce
)
);
wp_register_script('ajax-runtime-infos', $src, false, false, false);
wp_enqueue_script('ajax-runtime-infos');
}
/**
* Format the data set in JSON.
* Overload this if you want a different
* data structure, i.e. XML
*/
protected function render(){
return json_encode($this->infos);
}
/**
* Retrieves the html markup
* @return string html markup
*/
function get_markup() {
return ThemeHelpers::script($this->render(), 'id="runtime-infos"');
}
/**
* Generates an unique ID for the current data set
* @return string the ID
*/
private function generate_unique_id(){
$this->id = abs(crc32(serialize($this->infos)));
return $this->id;
}
/**
* Prints the markup
*/
public function the_markup(){
echo $this->get_markup();
}
/**
* Callback executed when the admin ajax url
* is called with self::ajax_action parameter
*/
public static function ajax_callback(){
$data = get_transient(intval($_GET['id']));
header("Content-type: text/javascript");
die('var runtimeInfos='.json_encode($data).';');
}
}