PluginProbe
Taboola / 1.0.4
Taboola v1.0.4
1.0.4 1.0.5 1.0.6 1.0.8 2.0.1 2.0.2 2.1.0 2.1.1 2.2.2 2.2.3 3.0.0 3.0.1 3.0.2 3.1.0 trunk 1.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.2 1.0.3
← All changes | taboola_widget.php +107 -12 1.0.21.0.4 View file →
@@ -2,12 +2,20 @@
2 2 /**
3 3 * Plugin Name: Taboola
4 4 * Plugin URI: http://taboola.com
5 5 * Description: Taboola
6 - * Version: 1.0.2
6 + * Version: 1.0.4
7 7 * Author: Taboola
8 8 */
9 9
10 +define ("XPATH_MARKER","/");
11 +define ("JS_INDICATOR","{JS}");
12 +define ("JS_MARKER","{");
13 +define ("TABOOLA_CONTENT_FORMAT_STRING",'string');
14 +define ("TABOOLA_CONTENT_FORMAT_SCRIPT",'script');
15 +define ("TABOOLA_CONTENT_FORMAT_HTML",'html');
16 +
17 +
10 18 include_once('widget.php');
11 19
12 20 if (!class_exists('TaboolaWP')) {
13 21 class TaboolaWP
@@ -136,9 +144,9 @@
136 144 }
137 145
138 146 function load_taboola_content($content)
139 147 {
140 -
148 + $taboola_content = array();
141 149 if ($this->should_show_content_widget()){
142 150
143 151 $script2_content = str_replace('{{WIDGET_ID}}', $this->settings->first_bc_widget_id, file_get_contents($this->plugin_directory.'js/script2.js'));
144 152 $script2_content = str_replace('{{CONTAINER}}', 'taboola-below-article', $script2_content);
@@ -143,11 +151,10 @@
143 151 $script2_content = str_replace('{{WIDGET_ID}}', $this->settings->first_bc_widget_id, file_get_contents($this->plugin_directory.'js/script2.js'));
144 152 $script2_content = str_replace('{{CONTAINER}}', 'taboola-below-article', $script2_content);
145 153 $script2_content = str_replace('{{PLACEMENT}}', 'below-article', $script2_content);
146 154
147 - $content = $content .
148 - '<div id="taboola-below-article"></div>' .
149 - '<script type="text/javascript">'.$script2_content.'</script>';
155 + $taboola_content[TABOOLA_CONTENT_FORMAT_HTML][] = '<div id="taboola-below-article"></div>';
156 + $taboola_content[TABOOLA_CONTENT_FORMAT_SCRIPT][] = $script2_content;
150 157
151 158 // Adding the 2nd widget if needed
152 159 if($this->settings->second_bc_enabled && trim($this->settings->second_bc_widget_id) != ''){
153 160 $script2_content = str_replace('{{WIDGET_ID}}', $this->settings->second_bc_widget_id, file_get_contents($this->plugin_directory.'js/script2.js'));
@@ -153,17 +160,94 @@
153 160 $script2_content = str_replace('{{WIDGET_ID}}', $this->settings->second_bc_widget_id, file_get_contents($this->plugin_directory.'js/script2.js'));
154 161 $script2_content = str_replace('{{CONTAINER}}', 'taboola-below-article-second', $script2_content);
155 162 $script2_content = str_replace('{{PLACEMENT}}', 'below-article-2nd', $script2_content);
156 163
157 - $content = $content .
158 - '<div id="taboola-below-article-second"></div>' .
159 - '<script type="text/javascript">'.$script2_content.'</script>';
164 + $taboola_content[TABOOLA_CONTENT_FORMAT_HTML][] = '<div id="taboola-below-article-second"></div>';
165 + $taboola_content[TABOOLA_CONTENT_FORMAT_SCRIPT][] = $script2_content;
160 166 }
167 +
168 + $content = $this->embed_taboola_content_location($content,$taboola_content,trim($this->settings->location_string));
161 169 }
162 170
163 171 return $content;
164 172 }
165 173
174 +
175 + // Extract the taboola content in the required format:
176 + // String - for injecting on the servr side
177 + // Script or HTML - for injecting on the client side
178 + function format_taboola_content($taboola_content,$format){
179 + $ret_val = null;
180 +
181 + switch($format){
182 + case TABOOLA_CONTENT_FORMAT_STRING:
183 + $result_string = join("",$taboola_content[TABOOLA_CONTENT_FORMAT_HTML]).
184 + "<script type='text/javascript'>".join("\n",$taboola_content[TABOOLA_CONTENT_FORMAT_SCRIPT])."</script>";
185 + $ret_val = $result_string;
186 + break;
187 +
188 + // script or html
189 + default:
190 + $ret_val = str_replace("\n","",join("",$taboola_content[$format]));
191 + break;
192 + }
193 +
194 + return $ret_val;
195 + }
196 +
197 +
198 + // Do the actual logic of choosing where to place the taboola content based on the "location" attribute
199 + function embed_taboola_content_location($content, $taboola_content, $location){
200 + $do_default = true;
201 +
202 + if (isset($location) && $location != ''){
203 + $first_char = substr($location,0,1);
204 +
205 + // DIV/XPATH provided for JS handling
206 + if ($first_char == JS_MARKER){
207 + $full_indicator = substr($location,0,strlen(JS_INDICATOR));
208 +
209 + if ($full_indicator == JS_INDICATOR){
210 +
211 + $xpath = substr($location,strlen(JS_INDICATOR));
212 + $js_inject_script = file_get_contents($this->plugin_directory.'js/js_inject.min.js');
213 + $js_inject_script = str_replace("{{XPATH}}",$xpath,$js_inject_script);
214 + $js_inject_script = str_replace("{{HTML}}",$this->format_taboola_content($taboola_content,TABOOLA_CONTENT_FORMAT_HTML),$js_inject_script);
215 + $js_inject_script = str_replace("{{SCRIPT}}",$this->format_taboola_content($taboola_content,TABOOLA_CONTENT_FORMAT_SCRIPT),$js_inject_script);
216 +
217 + $content = $content."<span id='tbdefault'></span><script type='text/javascript'>".$js_inject_script."</script>";
218 + $do_default = false;
219 + }
220 +
221 + // server side selector provided (see simple_html_dom selectors http://simplehtmldom.sourceforge.net/manual.htm)
222 + // basically it's CSS selectors like in jQuery
223 + } else{
224 +
225 + require_once('simple_html_dom.php');
226 +
227 + $html_doc = str_get_html($content);
228 + $target_location = $html_doc->find($location,0);
229 +
230 + // if the location was found within the html content
231 + if (isset($target_location) && is_object($target_location)){
232 +
233 + // adding taboola content AFTER the target location
234 + $target_location->outertext = $target_location->outertext.$this->format_taboola_content($taboola_content,TABOOLA_CONTENT_FORMAT_STRING);
235 + $content = $html_doc;
236 + $do_default = false;
237 + }
238 + }
239 + }
240 +
241 + // Default - add to the end of the content
242 + if ($do_default){
243 + $content = $content.$this->format_taboola_content($taboola_content,TABOOLA_CONTENT_FORMAT_STRING);
244 + }
245 +
246 + return $content;
247 + }
248 +
249 +
166 250 function admin_generate_menu(){
167 251 global $current_user;
168 252 add_menu_page('Taboola', 'Taboola', 'manage_options', 'taboola_widget', array(&$this, 'admin_taboola_settings'), $this->plugin_url.'img/taboola_icon.png', 110);
169 253 }
@@ -181,8 +265,11 @@
181 265 ($_POST['second_bc_enabled'] == 'on' && trim($_POST['second_bc_widget_id']) == '')
182 266 ){
183 267 $taboola_errors[] = "Please add a 'Widget ID' in order to apply changes to your widgets";
184 268 }
269 + if (trim($_POST['location_string']) != '' && !$this->is_location_string_valid($_POST['location_string'])){
270 + $taboola_errors[] = "Location string not valid, please contact your Taboola account manager to get a valid location string";
271 + }
185 272 if(count($taboola_errors) == 0){
186 273
187 274 $data = array(
188 275 "publisher_id" => trim($_POST['publisher_id']),
@@ -189,9 +276,11 @@
189 276 "first_bc_enabled" => $_POST['first_bc_enabled'] == 'on' ? true : false,
190 277 "first_bc_widget_id" => trim($_POST['first_bc_widget_id']),
191 278 "second_bc_enabled" => $_POST['second_bc_enabled'] == 'on' ? true : false,
192 279 "second_bc_widget_id" => trim($_POST['second_bc_widget_id']),
280 + "location_string" => trim($_POST['location_string'])
193 281 );
282 +
194 283 //var_dump($settings);
195 284 if($settings == NULL){
196 285 $wpdb->insert($this->tbl_taboola_settings, $data, null, null);
197 286 } else {
@@ -203,8 +292,13 @@
203 292
204 293
205 294 include_once('settings.php');
206 295 }
296 +
297 + function is_location_string_valid($location_string){
298 + // TODO:: validate the location string
299 + return true;
300 + }
207 301 function activate(){
208 302 global $wpdb;
209 303
210 304 require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
@@ -229,17 +323,18 @@
229 323 `first_bc_custom_css` TEXT DEFAULT NULL,
230 324 `second_bc_enabled` TINYINT(1) NOT NULL DEFAULT FALSE,
231 325 `second_bc_widget_id` VARCHAR(255) DEFAULT NULL,
232 326 `second_bc_custom_css` TEXT DEFAULT NULL,
327 + `location_string` TEXT DEFAULT NULL,
233 328 PRIMARY KEY (`id`)
234 329 )" . $charset_collate . ";";
235 330
236 - //check if table exists
237 - if ($wpdb->get_var("show tables like '" . $settings_table . "'") != $settings_table) {
331 + // creat/update the table
238 332 dbDelta($sql_table_settings);
239 - }
240 333 }
241 334 }
242 335 }
243 336
244 337 global $taboolaWP;
245 -$taboolaWP = new TaboolaWP();
338 +$taboolaWP = new TaboolaWP();
339 +
340 +//