PluginProbe
Taboola / 1.0.13
Taboola v1.0.13
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 +157 -33 1.0.21.0.13 View file →
@@ -1,14 +1,24 @@
1 1 <?php
2 2 /**
3 3 * Plugin Name: Taboola
4 - * Plugin URI: http://taboola.com
4 + * Plugin URI: https://developers.taboola.com/web-integrations/discuss
5 5 * Description: Taboola
6 - * Version: 1.0.2
6 + * Version: 1.0.13
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 +define ("TABOOLA_PLUGIN_VERSION","1.0.13");
17 +
18 +
10 19 include_once('widget.php');
20 +require_once('JavaScriptWrapper.php');
11 21
12 22 if (!class_exists('TaboolaWP')) {
13 23 class TaboolaWP
14 24 {
@@ -16,9 +26,10 @@
16 26 public $data = array();
17 27 public $_is_widget_on_page;
18 28 public $_is_head_script_loaded = false;
19 29
20 - function TaboolaWP()
30 +
31 + function __construct()
21 32 {
22 33 global $wpdb;
23 34
24 35 //initialize plugin constant
@@ -40,12 +51,16 @@
40 51 // Enable sidebar widgets
41 52 if($this->settings != NULL && !empty($this->settings->publisher_id)){
42 53 //register Taboola widget
43 54 add_action('widgets_init',
44 - create_function('', 'return register_widget("WP_Widget_Taboola");')
55 + function(){
56 + return register_widget("WP_Widget_Taboola");
57 + }
45 58 );
46 59 }
47 60
61 + //$this->should_place_tag_outside_of_content = $this->settings->out_of_content_enabled;
62 +
48 63 if (is_admin()) {
49 64 //add menu for plugin
50 65 add_action( 'admin_menu', array(&$this, 'admin_generate_menu') );
51 66 add_filter('plugin_action_links', array(&$this, 'plugin_action_links'), 10, 2 );
@@ -105,15 +120,20 @@
105 120 // return the head loader script
106 121 function taboola_header_loader_js() {
107 122 $head_string = "";
108 123
109 - // Only adding Script1 if a widget is going to be placed on the page.
124 + // Only adding the loader if a widget is going to be placed on the page.
110 125 if ($this->is_widget_on_page()){
111 126
112 - $script1_content = str_replace('{{PUBLISHER_ID}}', $this->settings->publisher_id, file_get_contents($this->plugin_directory.'js/script1.js'));
113 - $script1_content = str_replace('{{PAGE_TYPE}}',$this->get_page_type(),$script1_content);
127 + $stringParams = array(
128 + '{{PUBLISHER_ID}}' => $this->settings->publisher_id,
129 + '{{PAGE_TYPE}}' => $this->get_page_type(),
130 + '{{WORDPRESS_VERSION}}' => get_bloginfo('version'),
131 + '{{PLUGIN_VERSION}}' => TABOOLA_PLUGIN_VERSION
132 + );
114 133
115 - $head_string = '<script type="text/javascript">'.$script1_content.'</script>';
134 + $scriptWrapper = new JavaScriptWrapper("loaderInjectionScript.js",$stringParams);
135 + $head_string = $scriptWrapper->getScripMarkupString();
116 136 }
117 137
118 138 return $head_string;
119 139 }
@@ -127,43 +147,134 @@
127 147
128 148
129 149 function taboola_footer_loader_js() {
130 150
131 - // Only adding Script1 if a widget is going to be placed on the page.
151 + // Only adding flush script if a widget is going to be placed on the page.
132 152 if ( $this->is_widget_on_page() ){
133 - $script3_content = file_get_contents($this->plugin_directory.'js/script3.js');
134 - echo '<script type="text/javascript">'.$script3_content.'</script>';
153 + $flushInjectionScript = new JavaScriptWrapper('flushInjectionScript.js',array());
154 + echo $flushInjectionScript->getScripMarkupString();
135 155 }
136 156 }
137 157
138 158 function load_taboola_content($content)
139 159 {
140 -
160 + $taboola_content = array();
141 161 if ($this->should_show_content_widget()){
142 162
143 - $script2_content = str_replace('{{WIDGET_ID}}', $this->settings->first_bc_widget_id, file_get_contents($this->plugin_directory.'js/script2.js'));
144 - $script2_content = str_replace('{{CONTAINER}}', 'taboola-below-article', $script2_content);
145 - $script2_content = str_replace('{{PLACEMENT}}', 'below-article', $script2_content);
163 + $firstWidgetParams = array('{{WIDGET_ID}}' => $this->settings->first_bc_widget_id,
164 + '{{CONTAINER}}' => 'taboola-below-article',
165 + '{{PLACEMENT}}' => 'below-article');
146 166
147 - $content = $content .
148 - '<div id="taboola-below-article"></div>' .
149 - '<script type="text/javascript">'.$script2_content.'</script>';
167 + $firstWidgetScript = new JavaScriptWrapper("widgetInjectionScript.js",$firstWidgetParams);
168 + $taboola_content[TABOOLA_CONTENT_FORMAT_HTML][] = "<div id='taboola-below-article'></div>";
169 + $taboola_content[TABOOLA_CONTENT_FORMAT_SCRIPT][] = $firstWidgetScript;
150 170
151 171 // Adding the 2nd widget if needed
152 172 if($this->settings->second_bc_enabled && trim($this->settings->second_bc_widget_id) != ''){
153 - $script2_content = str_replace('{{WIDGET_ID}}', $this->settings->second_bc_widget_id, file_get_contents($this->plugin_directory.'js/script2.js'));
154 - $script2_content = str_replace('{{CONTAINER}}', 'taboola-below-article-second', $script2_content);
155 - $script2_content = str_replace('{{PLACEMENT}}', 'below-article-2nd', $script2_content);
156 173
157 - $content = $content .
158 - '<div id="taboola-below-article-second"></div>' .
159 - '<script type="text/javascript">'.$script2_content.'</script>';
174 + $secondWidgetParams = array('{{WIDGET_ID}}' => $this->settings->second_bc_widget_id,
175 + '{{CONTAINER}}' => 'taboola-below-article-second',
176 + '{{PLACEMENT}}' => 'below-article-2nd');
177 + $secondWidgetScript = new JavaScriptWrapper("widgetInjectionScript.js",$secondWidgetParams);
178 +
179 + $taboola_content[TABOOLA_CONTENT_FORMAT_HTML][] = "<div id='taboola-below-article-second'></div>";
180 + $taboola_content[TABOOLA_CONTENT_FORMAT_SCRIPT][] = $secondWidgetScript;
160 181 }
182 +
183 + $content = $this->embed_taboola_content_location($content,$taboola_content,trim($this->settings->location_string));
161 184 }
162 185
163 186 return $content;
164 187 }
165 188
189 +
190 + // Extract the taboola content in the required format:
191 + // String - for injecting on the servr side
192 + // Script or HTML - for injecting on the client side
193 + function format_taboola_content($taboola_content,$format){
194 + $ret_val = null;
195 +
196 + switch($format){
197 + case TABOOLA_CONTENT_FORMAT_STRING:
198 + $result_string = join("",$taboola_content[TABOOLA_CONTENT_FORMAT_HTML]).
199 + "<script type='text/javascript'>".join("\n",$taboola_content[TABOOLA_CONTENT_FORMAT_SCRIPT])."</script>";
200 + $ret_val = $result_string;
201 + break;
202 +
203 + // script or html
204 + default:
205 + $ret_val = str_replace("\n","",join("",$taboola_content[$format]));
206 + break;
207 + }
208 +
209 + return $ret_val;
210 + }
211 +
212 +
213 + // Do the actual logic of choosing where to place the taboola content based on the "location" attribute
214 + function embed_taboola_content_location($content, $taboola_content, $location){
215 + $do_default = true;
216 +
217 + if (isset($location) && $location != ''){
218 + $first_char = substr($location,0,1);
219 +
220 + // DIV/XPATH provided for JS handling
221 + if ($first_char == JS_MARKER){
222 + $full_indicator = substr($location,0,strlen(JS_INDICATOR));
223 +
224 + if ($full_indicator == JS_INDICATOR){
225 +
226 + $xpath = substr($location,strlen(JS_INDICATOR));
227 + $scriptWrapper = new JavaScriptWrapper("js_inject.min.js",array(
228 + "{{HTML}}" => $this->format_taboola_content($taboola_content,TABOOLA_CONTENT_FORMAT_HTML),
229 + "{{SCRIPT}}" => $this->format_taboola_content($taboola_content,TABOOLA_CONTENT_FORMAT_SCRIPT))
230 + );
231 + $scriptWrapper->appendScript("injectWidgetByXpath('".$xpath."');");
232 + $content = $content."<span id='tbdefault'></span><script type='text/javascript'>".$scriptWrapper."</script>";
233 +
234 + $do_default = false;
235 + }
236 +
237 + // server side selector provided (see simple_html_dom selectors https://simplehtmldom.sourceforge.net/manual.htm)
238 + // basically it's CSS selectors like in jQuery
239 + } else{
240 +
241 + require_once('simple_html_dom.php');
242 +
243 + $html_doc = str_get_html($content);
244 + $target_location = $html_doc->find($location,0);
245 +
246 + // if the location was found within the html content
247 + if (isset($target_location) && is_object($target_location)){
248 +
249 + // adding taboola content AFTER the target location
250 + $target_location->outertext = $target_location->outertext.$this->format_taboola_content($taboola_content,TABOOLA_CONTENT_FORMAT_STRING);
251 + $content = $html_doc;
252 + $do_default = false;
253 + }
254 + }
255 +
256 + // tag is placed outside of content in order to allow "read more" functionality.
257 + }elseif ($this->settings->out_of_content_enabled){
258 +
259 + $scriptWrapper = new JavaScriptWrapper("js_inject.min.js",array(
260 + "{{HTML}}" => $this->format_taboola_content($taboola_content,TABOOLA_CONTENT_FORMAT_HTML),
261 + "{{SCRIPT}}" => $this->format_taboola_content($taboola_content,TABOOLA_CONTENT_FORMAT_SCRIPT))
262 + );
263 + $scriptWrapper->appendScript("injectWidgetByMarker('tbmarker');");
264 + $content = $content."<span id='tbmarker'></span><script type='text/javascript'>".$scriptWrapper."</script>";
265 + $do_default = false;
266 + }
267 +
268 + // Default - add to the end of the content
269 + if ($do_default){
270 + $content = $content.$this->format_taboola_content($taboola_content,TABOOLA_CONTENT_FORMAT_STRING);
271 + }
272 +
273 + return $content;
274 + }
275 +
276 +
166 277 function admin_generate_menu(){
167 278 global $current_user;
168 279 add_menu_page('Taboola', 'Taboola', 'manage_options', 'taboola_widget', array(&$this, 'admin_taboola_settings'), $this->plugin_url.'img/taboola_icon.png', 110);
169 280 }
@@ -176,22 +287,28 @@
176 287
177 288 if(trim($_POST['publisher_id']) == ''){
178 289 $taboola_errors[] = "Please add a 'Publisher ID' in order to apply changes to your widgets";
179 290 }
180 - if(($_POST['first_bc_enabled'] == 'on' && trim($_POST['first_bc_widget_id']) == '') ||
181 - ($_POST['second_bc_enabled'] == 'on' && trim($_POST['second_bc_widget_id']) == '')
291 + if((isset($_POST['first_bc_enabled']) && trim($_POST['first_bc_widget_id']) == '') ||
292 + (isset($_POST['second_bc_enabled']) && trim($_POST['second_bc_widget_id']) == '')
182 293 ){
183 294 $taboola_errors[] = "Please add a 'Widget ID' in order to apply changes to your widgets";
184 295 }
296 + if (trim($_POST['location_string']) != '' && !$this->is_location_string_valid($_POST['location_string'])){
297 + $taboola_errors[] = "Location string not valid, please contact your Taboola account manager to get a valid location string";
298 + }
185 299 if(count($taboola_errors) == 0){
186 300
187 301 $data = array(
188 302 "publisher_id" => trim($_POST['publisher_id']),
189 - "first_bc_enabled" => $_POST['first_bc_enabled'] == 'on' ? true : false,
303 + "first_bc_enabled" => isset($_POST['first_bc_enabled']) ? true : false,
190 304 "first_bc_widget_id" => trim($_POST['first_bc_widget_id']),
191 - "second_bc_enabled" => $_POST['second_bc_enabled'] == 'on' ? true : false,
305 + "second_bc_enabled" => isset($_POST['second_bc_enabled']) ? true : false,
192 306 "second_bc_widget_id" => trim($_POST['second_bc_widget_id']),
307 + "location_string" => trim($_POST['location_string']),
308 + "out_of_content_enabled" => isset($_POST['out_of_content_enabled']) ? true : false
193 309 );
310 +
194 311 //var_dump($settings);
195 312 if($settings == NULL){
196 313 $wpdb->insert($this->tbl_taboola_settings, $data, null, null);
197 314 } else {
@@ -203,8 +320,13 @@
203 320
204 321
205 322 include_once('settings.php');
206 323 }
324 +
325 + function is_location_string_valid($location_string){
326 + // TODO:: validate the location string
327 + return true;
328 + }
207 329 function activate(){
208 330 global $wpdb;
209 331
210 332 require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
@@ -211,9 +333,9 @@
211 333
212 334 $settings_table = $this->tbl_taboola_settings;
213 335
214 336 //check mysql version
215 - if (version_compare(mysql_get_server_info(), '4.1.0', '>=')) {
337 + if (function_exists('mysql_get_server_info') && version_compare(mysql_get_server_info(), '4.1.0', '>=')) {
216 338 if (!empty($wpdb->charset))
217 339 $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
218 340 if (!empty($wpdb->collate))
219 341 $charset_collate .= " COLLATE $wpdb->collate";
@@ -229,17 +351,19 @@
229 351 `first_bc_custom_css` TEXT DEFAULT NULL,
230 352 `second_bc_enabled` TINYINT(1) NOT NULL DEFAULT FALSE,
231 353 `second_bc_widget_id` VARCHAR(255) DEFAULT NULL,
232 354 `second_bc_custom_css` TEXT DEFAULT NULL,
355 + `location_string` TEXT DEFAULT NULL,
356 + `out_of_content_enabled` TINYINT(1) NOT NULL DEFAULT TRUE,
233 357 PRIMARY KEY (`id`)
234 358 )" . $charset_collate . ";";
235 359
236 - //check if table exists
237 - if ($wpdb->get_var("show tables like '" . $settings_table . "'") != $settings_table) {
360 + // create/update the table
238 361 dbDelta($sql_table_settings);
239 - }
240 362 }
241 363 }
242 364 }
243 365
244 366 global $taboolaWP;
245 -$taboolaWP = new TaboolaWP();
367 +$taboolaWP = new TaboolaWP();
368 +
369 +//