PluginProbe
Taboola / 2.2.2
Taboola v2.2.2
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 +620 -91 1.0.32.2.2 View file →
@@ -1,16 +1,22 @@
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/docs/wordpress-plugin
5 5 * Description: Taboola
6 - * Version: 1.0.3
6 + * Version: 2.2.2
7 7 * Author: Taboola
8 8 */
9 9
10 -define ("XPATH_MARKER","/");
11 -define ("JS_INDICATOR","{JS}");
12 -define ("JS_MARKER","{");
10 +define ("TABOOLA_PLUGIN_VERSION","2.2.2"); // => UPDATE THIS FOR *EVERY* RELEASE (USED FOR TRACKING)
11 +define ("TABOOLA_MIN_VER","2.2.2"); // => UPDATE THIS *ONLY* IF THIS RELEASE HAS *DB CHANGES*
12 +define ("TABOOLA_DEBUG_MODE", false); // => SET THIS TO 'FALSE' FOR *EVERY* RELEASE (USED TO SUPRESS DEBUGGING LOGS)
13 +
14 +define ("TABOOLA_OPTION_NAME","taboola_plugin_version"); // Note: if this release has DB changes, then the min version will be saved under 'taboola_plugin_version' in 'wp_options'.
15 +
16 +define ("TABOOLA_XPATH_MARKER","/");
17 +define ("TABOOLA_JS_INDICATOR","{JS}");
18 +define ("TABOOLA_JS_MARKER","{");
13 19 define ("TABOOLA_CONTENT_FORMAT_STRING",'string');
14 20 define ("TABOOLA_CONTENT_FORMAT_SCRIPT",'script');
15 21 define ("TABOOLA_CONTENT_FORMAT_HTML",'html');
16 22
@@ -15,9 +21,11 @@
15 21 define ("TABOOLA_CONTENT_FORMAT_HTML",'html');
16 22
17 23
18 24 include_once('widget.php');
25 +require_once('JavaScriptWrapper.php');
19 26
27 +
20 28 if (!class_exists('TaboolaWP')) {
21 29 class TaboolaWP
22 30 {
23 31 //save internal data
@@ -23,10 +31,28 @@
23 31 //save internal data
24 32 public $data = array();
25 33 public $_is_widget_on_page;
26 34 public $_is_head_script_loaded = false;
35 + private $tpl_sw = 'importScripts("https://cdn.taboola.com/webpush/tsw.js");';
27 36
28 - function TaboolaWP()
37 + private $msg_sw_error = <<<SWE
38 + <p>
39 + The file sw.js in the root directory of Wordpress is not writable.
40 + Please change its permissions and try again. Otherwise replace its contents manually:
41 + </p>
42 + <pre><code>{{SW}}</code></pre>
43 + <p>
44 + Also make sure that the file is accessible at {{DOMAIN}}/sw.js
45 + </p>
46 + SWE;
47 +
48 + public $plugin_name;
49 + public $plugin_directory;
50 + public $plugin_url;
51 + public $settings;
52 + public $tbl_taboola_settings;
53 +
54 + public function __construct()
29 55 {
30 56 global $wpdb;
31 57
32 58 //initialize plugin constant
@@ -42,34 +68,54 @@
42 68
43 69 $this->tbl_taboola_settings = $wpdb->prefix . '_taboola_settings';
44 70
45 71 //activation function
46 - register_activation_hook($this->plugin_name, array(&$this, 'activate'));
72 + register_activation_hook($this->plugin_name, array(&$this, 'update_db'));
73 + add_action('admin_init', array(&$this, 'update_db'));
47 74
48 75 // Enable sidebar widgets
49 - if($this->settings != NULL && !empty($this->settings->publisher_id)){
76 + if ($this->settings != NULL && !empty($this->settings->publisher_id)){
50 77 //register Taboola widget
51 78 add_action('widgets_init',
52 - create_function('', 'return register_widget("WP_Widget_Taboola");')
79 + function(){
80 + return register_widget("WP_Widget_Taboola");
81 + }
53 82 );
54 83 }
55 84
85 + //$this->should_place_tag_outside_of_content = $this->settings->out_of_content_enabled;
86 +
56 87 if (is_admin()) {
57 88 //add menu for plugin
58 89 add_action( 'admin_menu', array(&$this, 'admin_generate_menu') );
59 90 add_filter('plugin_action_links', array(&$this, 'plugin_action_links'), 10, 2 );
60 - } else {
61 - if($this->settings != NULL){
91 + }elseif ($this->settings != NULL){
62 92 add_action('wp_head', array(&$this, 'taboola_header_loader_inject'));
93 + if (!empty($this->settings->publisher_id_push)) {
94 + add_action('wp_head', array(&$this, 'taboola_webpush_loader_js'));
95 +
96 + $sw = 'sw.js';
97 + $sw_path = ABSPATH . $sw;
98 +
99 + $content = file_exists($sw_path) ? file_get_contents($sw_path) : '';
100 +
101 + if (strpos($content, $this->tpl_sw) === false) {
102 + if (!is_writable(ABSPATH) || (file_exists($sw_path) && !is_writable($sw_path))) {
103 + return $this->notice($this->msg_sw_error);
104 + }
105 + $content = $this->tpl_sw . PHP_EOL . $content;
106 + if (file_put_contents($sw_path, $content) === false) {
107 + return $this->notice($this->msg_sw_error);
108 + }
109 + }
110 + }
63 111 add_action('wp_footer', array(&$this, 'taboola_footer_loader_js'));
64 112 add_filter('the_content', array(&$this, 'load_taboola_content'));
65 - }
66 - }
67 - }
113 + add_filter('the_content', array(&$this, 'load_taboola_content_mid'));
114 + add_filter('the_content', array(&$this, 'load_taboola_content_home'));
115 + }
116 + }
68 117
69 -
70 -
71 -
72 118 function plugin_action_links($links, $file) {
73 119 static $this_plugin;
74 120
75 121 if (!$this_plugin) {
@@ -88,8 +134,24 @@
88 134 $retVal = ((trim($this->settings->publisher_id) != '') && is_single() && $this->settings->first_bc_enabled && trim($this->settings->first_bc_widget_id) != '');
89 135 return $retVal;
90 136 }
91 137
138 + private function should_show_content_widget_mid(){
139 + // PC - only if v2 was installed:
140 + if (!$this->is_db_updated_for_min_ver("2.0.0"))
141 + return false;
142 + $retVal1 = ((trim($this->settings->publisher_id) != '') && is_single() && $this->settings->mid_enabled && trim($this->settings->mid_widget_id) != '');
143 + return $retVal1;
144 + }
145 +
146 + private function should_show_content_widget_home(){
147 + // PC - only if v2 was installed:
148 + if (!$this->is_db_updated_for_min_ver("2.0.0"))
149 + return false;
150 + $retVal2 = ((trim($this->settings->publisher_id) != '') && (is_front_page() || is_home()) && $this->settings->home_enabled && trim($this->settings->home_widget_id) != '');
151 + return $retVal2;
152 + }
153 +
92 154 private function should_show_sidebar_widget(){
93 155 $retVal = ((trim($this->settings->publisher_id) != '') && is_active_widget( false, false, TABOOLA_WIDGET_BASE_ID, true ));
94 156 return $retVal;
95 157 }
@@ -95,16 +157,15 @@
95 157 }
96 158
97 159 // Determine if a taboola widget should be added somewhere on the current page (content or sidebar)
98 160 function is_widget_on_page(){
99 - return $this->should_show_content_widget() || $this->should_show_sidebar_widget();
161 + return $this->should_show_content_widget() || $this->should_show_content_widget_mid() || $this->should_show_content_widget_home() || $this->should_show_sidebar_widget();
100 162 }
101 163
102 164 function get_page_type(){
103 -
104 165 $page_type='article';
105 166 if (is_front_page()){
106 - $page_type='home';
167 + $page_type='home';
107 168 }else if (is_category() || is_archive() || is_search()){
108 169 $page_type='category';
109 170 }
110 171 return $page_type;
@@ -113,17 +174,25 @@
113 174 // return the head loader script
114 175 function taboola_header_loader_js() {
115 176 $head_string = "";
116 177
117 - // Only adding Script1 if a widget is going to be placed on the page.
178 + // Only adding the loader if a widget is going to be placed on the page.
118 179 if ($this->is_widget_on_page()){
180 + // PC - since these params will be inserted in 'loaderInjectionScript.js' via search and replace,
181 + // double brackets are used to ensure that each key is a unique string.
182 + $stringParams = array(
183 + '{{PUBLISHER_ID}}' => $this->settings->publisher_id,
184 + '{{PAGE_TYPE}}' => $this->get_page_type(),
185 + '{{WORDPRESS_VERSION}}' => get_bloginfo('version'),
186 + '{{PHP_VERSION}}' => phpversion(),
187 + '{{PLUGIN_VERSION}}' => TABOOLA_PLUGIN_VERSION,
188 + '{{LOC_MID}}' => $this->settings->mid_location_string,
189 + '{{LOC_HOME}}' => $this->settings->home_location_string
190 + );
119 191
120 - $script1_content = str_replace('{{PUBLISHER_ID}}', $this->settings->publisher_id, file_get_contents($this->plugin_directory.'js/script1.js'));
121 - $script1_content = str_replace('{{PAGE_TYPE}}',$this->get_page_type(),$script1_content);
122 -
123 - $head_string = '<script type="text/javascript">'.$script1_content.'</script>';
192 + $scriptWrapper = new JavaScriptWrapper("loaderInjectionScript.js",$stringParams);
193 + $head_string = $scriptWrapper->getScriptMarkupString();
124 194 }
125 -
126 195 return $head_string;
127 196 }
128 197
129 198
@@ -128,51 +197,97 @@
128 197
129 198
130 199 // This function is used for the hook action, injects the header content to the <head> tag.
131 200 function taboola_header_loader_inject(){
132 -
133 201 echo $this->taboola_header_loader_js();
134 202 }
135 203
204 + // adding webpush loader
205 + function taboola_webpush_loader_js() {
206 + if(is_single() || is_home() || is_category() || is_front_page()){
207 + $stringParamsWeb = array(
208 + '{{PUBLISHER_ID}}' => $this->settings->publisher_id_push
209 + );
210 + $webpushInjectionScript = new JavaScriptWrapper("webPush.js",$stringParamsWeb);
211 + echo $webpushInjectionScript;
212 + }
213 + }
136 214
137 215 function taboola_footer_loader_js() {
138 216
139 - // Only adding Script1 if a widget is going to be placed on the page.
140 - if ( $this->is_widget_on_page() ){
141 - $script3_content = file_get_contents($this->plugin_directory.'js/script3.js');
142 - echo '<script type="text/javascript">'.$script3_content.'</script>';
217 + // Only adding flush script if a widget is going to be placed on the page.
218 + if ($this->is_widget_on_page()){
219 + $flushInjectionScript = new JavaScriptWrapper('flushInjectionScript.js',array());
220 + echo $flushInjectionScript->getScriptMarkupString();
143 221 }
144 222 }
145 223
224 + // Below-article widget
146 225 function load_taboola_content($content)
147 226 {
148 227 $taboola_content = array();
149 228 if ($this->should_show_content_widget()){
150 229
151 - $script2_content = str_replace('{{WIDGET_ID}}', $this->settings->first_bc_widget_id, file_get_contents($this->plugin_directory.'js/script2.js'));
152 - $script2_content = str_replace('{{CONTAINER}}', 'taboola-below-article', $script2_content);
153 - $script2_content = str_replace('{{PLACEMENT}}', 'below-article', $script2_content);
230 + $firstWidgetParams = array(
231 + '{{WIDGET_ID}}' => $this->settings->first_bc_widget_id,
232 + '{{CONTAINER}}' => 'taboola-below-article-thumbnails',
233 + // PC - If v2 was not installed, then use v1 logic
234 + '{{PLACEMENT}}' => (!empty($this->settings->first_bc_placement)) ? $this->settings->first_bc_placement : 'below-article' // In case v1 upgrade has not run yet
235 + );
154 236
155 - $taboola_content[TABOOLA_CONTENT_FORMAT_HTML][] = '<div id="taboola-below-article"></div>';
156 - $taboola_content[TABOOLA_CONTENT_FORMAT_SCRIPT][] = $script2_content;
237 + $firstWidgetScript = new JavaScriptWrapper("widgetInjectionScript.js",$firstWidgetParams);
238 + $taboola_content[TABOOLA_CONTENT_FORMAT_HTML][] = "<div id='taboola-below-article-thumbnails'></div>";
239 + $taboola_content[TABOOLA_CONTENT_FORMAT_SCRIPT][] = $firstWidgetScript;
157 240
158 - // Adding the 2nd widget if needed
159 - if($this->settings->second_bc_enabled && trim($this->settings->second_bc_widget_id) != ''){
160 - $script2_content = str_replace('{{WIDGET_ID}}', $this->settings->second_bc_widget_id, file_get_contents($this->plugin_directory.'js/script2.js'));
161 - $script2_content = str_replace('{{CONTAINER}}', 'taboola-below-article-second', $script2_content);
162 - $script2_content = str_replace('{{PLACEMENT}}', 'below-article-2nd', $script2_content);
241 + $content = $this->embed_taboola_content_location($content,$taboola_content);
242 + }
163 243
164 - $taboola_content[TABOOLA_CONTENT_FORMAT_HTML][] = '<div id="taboola-below-article-second"></div>';
165 - $taboola_content[TABOOLA_CONTENT_FORMAT_SCRIPT][] = $script2_content;
166 - }
244 + return $content;
245 + }
167 246
168 - $content = $this->embed_taboola_content_location($content,$taboola_content,trim($this->settings->location_string));
247 + // Mid-article-widget
248 + function load_taboola_content_mid($content)
249 + {
250 + $taboola_content_mid = array();
251 + if ($this->should_show_content_widget_mid()){
252 +
253 + $secondWidgetParams = array('{{WIDGET_ID}}' => $this->settings->mid_widget_id,
254 + '{{CONTAINER}}' => 'taboola-mid-article-thumbnails',
255 + '{{PLACEMENT}}' => $this->settings->mid_placement);
256 +
257 + $secondWidgetScript = new JavaScriptWrapper("widgetInjectionScript.js",$secondWidgetParams);
258 + $taboola_content_mid[TABOOLA_CONTENT_FORMAT_HTML][] = "<div id='taboola-mid-article-thumbnails'></div>";
259 + $taboola_content_mid[TABOOLA_CONTENT_FORMAT_SCRIPT][] = $secondWidgetScript;
260 +
261 + $content = $this->embed_taboola_content_location_mid($content,$taboola_content_mid,trim($this->settings->mid_location_string));
169 262 }
170 263
171 264 return $content;
172 265 }
173 266
267 + // Homepage widget
268 + function load_taboola_content_home($content)
269 + {
270 +
271 + $taboola_content_home = array();
272 + if ($this->should_show_content_widget_home()){
174 273
274 + $homeWidgetParams = array('{{WIDGET_ID}}' => $this->settings->home_widget_id,
275 + '{{CONTAINER}}' => 'taboola-homepage-thumbnails',
276 + '{{PLACEMENT}}' => $this->settings->home_placement);
277 +
278 + $homeWidgetScript = new JavaScriptWrapper("widgetInjectionScript.js",$homeWidgetParams);
279 + $taboola_content_home[TABOOLA_CONTENT_FORMAT_HTML][] = "<div id='taboola-homepage-thumbnails'></div>";
280 + $taboola_content_home[TABOOLA_CONTENT_FORMAT_SCRIPT][] = $homeWidgetScript;
281 +
282 + $content = $this->embed_taboola_content_location_home($content,$taboola_content_home,trim($this->settings->home_location_string));
283 + }
284 + return $content;
285 +
286 + }
287 +
288 + // Below-article widget
289 +
175 290 // Extract the taboola content in the required format:
176 291 // String - for injecting on the servr side
177 292 // Script or HTML - for injecting on the client side
178 293 function format_taboola_content($taboola_content,$format){
@@ -194,28 +309,98 @@
194 309 return $ret_val;
195 310 }
196 311
197 312
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){
313 + // Mid-article widget
314 + function format_taboola_content_mid($taboola_content_mid,$format){
315 + $ret_val = null;
316 +
317 + switch($format){
318 + case TABOOLA_CONTENT_FORMAT_STRING:
319 + $result_string = join("",$taboola_content_mid[TABOOLA_CONTENT_FORMAT_HTML]).
320 + "<script type='text/javascript'>".join("\n",$taboola_content_mid[TABOOLA_CONTENT_FORMAT_SCRIPT])."</script>";
321 + $ret_val = $result_string;
322 + break;
323 +
324 + // script or html
325 + default:
326 + $ret_val = str_replace("\n","",join("",$taboola_content_mid[$format]));
327 + break;
328 + }
329 +
330 + return $ret_val;
331 + }
332 +
333 +
334 + // Homepage widget
335 + function format_taboola_content_home($taboola_content_home,$format){
336 +
337 + $ret_val = null;
338 +
339 + switch($format){
340 + case TABOOLA_CONTENT_FORMAT_STRING:
341 + $result_string = join("",$taboola_content_home[TABOOLA_CONTENT_FORMAT_HTML]).
342 + "<script type='text/javascript'>".join("\n",$taboola_content_home[TABOOLA_CONTENT_FORMAT_SCRIPT])."</script>";
343 + $ret_val = $result_string;
344 + break;
345 +
346 + // script or html
347 + default:
348 + $ret_val = str_replace("\n","",join("",$taboola_content_home[$format]));
349 + break;
350 + }
351 +
352 + return $ret_val;
353 +
354 + }
355 +
356 + // Below-article widget
357 + // Do the actual logic of choosing where to place the taboola content.
358 + function embed_taboola_content_location($content, $taboola_content){
200 359 $do_default = true;
201 360
361 + // tag is placed outside of content in order to allow "read more" functionality.
362 + if ($this->settings->out_of_content_enabled){
363 +
364 + $scriptWrapper = new JavaScriptWrapper("js_inject.min.js",array(
365 + "{{HTML}}" => $this->format_taboola_content($taboola_content,TABOOLA_CONTENT_FORMAT_HTML),
366 + "{{SCRIPT}}" => $this->format_taboola_content($taboola_content,TABOOLA_CONTENT_FORMAT_SCRIPT))
367 + );
368 + $scriptWrapper->appendScript("injectWidgetByMarker('tbmarker');");
369 + $content = $content."<span id='tbmarker'></span><script type='text/javascript'>".$scriptWrapper."</script>";
370 + $do_default = false;
371 + }
372 +
373 + // Default for below-article widget - add to the end of the content
374 + if ($do_default){
375 + $content = $content.$this->format_taboola_content($taboola_content,TABOOLA_CONTENT_FORMAT_STRING);
376 + }
377 +
378 + return $content;
379 + }
380 +
381 + // Mid-article widget
382 + // Do the actual logic of choosing where to place the taboola content based on the "location" attribute
383 + function embed_taboola_content_location_mid($content, $taboola_content_mid, $location){
384 + $do_default = true;
385 +
202 386 if (isset($location) && $location != ''){
203 387 $first_char = substr($location,0,1);
204 388
205 389 // DIV/XPATH provided for JS handling
206 - if ($first_char == JS_MARKER){
207 - $full_indicator = substr($location,0,strlen(JS_INDICATOR));
390 + if ($first_char == TABOOLA_JS_MARKER){
391 + $full_indicator = substr($location,0,strlen(TABOOLA_JS_INDICATOR));
208 392
209 - if ($full_indicator == JS_INDICATOR){
393 + if ($full_indicator == TABOOLA_JS_INDICATOR){
210 394
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);
395 + $xpath = substr($location,strlen(TABOOLA_JS_INDICATOR));
396 + $scriptWrapper = new JavaScriptWrapper("js_inject.min.js",array(
397 + "{{HTML}}" => $this->format_taboola_content_mid($taboola_content_mid,TABOOLA_CONTENT_FORMAT_HTML),
398 + "{{SCRIPT}}" => $this->format_taboola_content_mid($taboola_content_mid,TABOOLA_CONTENT_FORMAT_SCRIPT))
399 + );
400 + $scriptWrapper->appendScript("injectWidgetByXpath('".$xpath."');");
401 + $content = $content."<span id='tbdefault'></span><script type='text/javascript'>".$scriptWrapper."</script>";
216 402
217 - $content = $content."<span id='tbdefault'></span><script type='text/javascript'>".$js_inject_script."</script>";
218 403 $do_default = false;
219 404 }
220 405
221 406 // server side selector provided (see simple_html_dom selectors http://simplehtmldom.sourceforge.net/manual.htm)
@@ -220,37 +405,86 @@
220 405
221 406 // server side selector provided (see simple_html_dom selectors http://simplehtmldom.sourceforge.net/manual.htm)
222 407 // basically it's CSS selectors like in jQuery
223 408 } else{
224 -
225 409 require_once('simple_html_dom.php');
226 410
227 411 $html_doc = str_get_html($content);
228 - $target_location = $html_doc->find($location,0);
412 + $target_location = $html_doc->find($location,($this->settings->mid_location_string_occurrence)-1);
229 413
230 414 // if the location was found within the html content
231 415 if (isset($target_location) && is_object($target_location)){
232 416
233 417 // adding taboola content AFTER the target location
234 - $target_location->outertext = $target_location->outertext.$this->format_taboola_content($taboola_content,TABOOLA_CONTENT_FORMAT_STRING);
418 + $target_location->outertext = $target_location->outertext.$this->format_taboola_content_mid($taboola_content_mid,TABOOLA_CONTENT_FORMAT_STRING);
235 419 $content = $html_doc;
236 420 $do_default = false;
237 421 }
238 422 }
239 423 }
424 + // Default for below-article widget - add to the end of the content
425 + // if ($do_default){
426 + // $content = $content.$this->format_taboola_content_mid($taboola_content_mid,TABOOLA_CONTENT_FORMAT_STRING);
427 + // }
240 428
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 429 return $content;
247 430 }
248 431
432 + // Homepage widget
433 + // Do the actual logic of choosing where to place the taboola content based on the "location" attribute
434 + function embed_taboola_content_location_home($content, $taboola_content_home, $location){
249 435
436 + $do_default = true;
437 +
438 + if (isset($location) && $location != ''){
439 + $first_char = substr($location,0,1);
440 +
441 + // DIV/XPATH provided for JS handling
442 + if ($first_char == TABOOLA_JS_MARKER){
443 + $full_indicator = substr($location,0,strlen(TABOOLA_JS_INDICATOR));
444 +
445 + if ($full_indicator == TABOOLA_JS_INDICATOR){
446 +
447 + $xpath = substr($location,strlen(TABOOLA_JS_INDICATOR));
448 + $scriptWrapper = new JavaScriptWrapper("js_inject.min.js",array(
449 + "{{HTML}}" => $this->format_taboola_content_home($taboola_content_home,TABOOLA_CONTENT_FORMAT_HTML),
450 + "{{SCRIPT}}" => $this->format_taboola_content_home($taboola_content_home,TABOOLA_CONTENT_FORMAT_SCRIPT))
451 + );
452 + $scriptWrapper->appendScript("injectWidgetByXpath('".$xpath."');");
453 + $content = $content."<span id='tbdefault'></span><script type='text/javascript'>".$scriptWrapper."</script>";
454 +
455 + $do_default = false;
456 + }
457 +
458 + // server side selector provided (see simple_html_dom selectors http://simplehtmldom.sourceforge.net/manual.htm)
459 + // basically it's CSS selectors like in jQuery
460 + } else{
461 + require_once('simple_html_dom.php');
462 +
463 + $html_doc = str_get_html($content);
464 + $target_location = $html_doc->find($location,($this->settings->home_location_string_occurrence)-1);
465 +
466 + // if the location was found within the html content
467 + if (isset($target_location) && is_object($target_location)){
468 +
469 + // adding taboola content AFTER the target location
470 + $target_location->outertext = $target_location->outertext.$this->format_taboola_content_home($taboola_content_home,TABOOLA_CONTENT_FORMAT_STRING);
471 + $content = $html_doc;
472 + $do_default = false;
473 + }
474 + }
475 + }
476 + // Default for below-article widget - add to the end of the content
477 + // if ($do_default){
478 + // $content = $content.$this->format_taboola_content_home($taboola_content_home,TABOOLA_CONTENT_FORMAT_STRING);
479 + // }
480 + return $content;
481 +
482 + }
483 +
250 484 function admin_generate_menu(){
251 485 global $current_user;
252 - add_menu_page('Taboola', 'Taboola', 'manage_options', 'taboola_widget', array(&$this, 'admin_taboola_settings'), $this->plugin_url.'img/taboola_icon.png', 110);
486 + add_menu_page(__('Taboola','taboola_widget'), __('Taboola','taboola_widget'), 'manage_options', 'taboola_widget', array(&$this, 'admin_taboola_settings'), $this->plugin_url.'img/taboola_icon.png', 110);
253 487 }
254 488
255 489 function admin_taboola_settings(){
256 490 global $wpdb;
@@ -257,57 +491,283 @@
257 491 $settings = $wpdb->get_row("select * from ".$wpdb->prefix."_taboola_settings limit 1");
258 492 $taboola_errors = array();
259 493 if($_SERVER['REQUEST_METHOD'] == 'POST'){
260 494
261 - if(trim($_POST['publisher_id']) == ''){
262 - $taboola_errors[] = "Please add a 'Publisher ID' in order to apply changes to your widgets";
495 + if(trim(strip_tags($_POST['publisher_id'])) == ''){
496 + $taboola_errors[] = "Publisher ID";
263 497 }
264 - if(($_POST['first_bc_enabled'] == 'on' && trim($_POST['first_bc_widget_id']) == '') ||
265 - ($_POST['second_bc_enabled'] == 'on' && trim($_POST['second_bc_widget_id']) == '')
266 - ){
267 - $taboola_errors[] = "Please add a 'Widget ID' in order to apply changes to your widgets";
498 +
499 + if(isset($_POST['web_push_enabled'])) {
500 + if (trim(strip_tags($_POST['publisher_id_push'])) == '') {
501 + $taboola_errors[] = "Publisher Push ID";
502 + }
268 503 }
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";
504 +
505 + if(isset($_POST['first_bc_enabled'])) {
506 + if (trim(strip_tags($_POST['first_bc_widget_id'])) == '') {
507 + $taboola_errors[] = "Below-article > Widget ID";
508 + }
509 + if (trim(strip_tags($_POST['first_bc_placement'])) == '') {
510 + $taboola_errors[] = "Below-article > Placement Name";
511 + }
271 512 }
513 +
514 + if(isset($_POST['mid_enabled'])) {
515 + if (trim(strip_tags($_POST['mid_widget_id'])) == '') {
516 + $taboola_errors[] = "Mid-article > Widget ID";
517 + }
518 + if (trim(strip_tags($_POST['mid_placement'])) == '') {
519 + $taboola_errors[] = "Mid-article > Placement Name";
520 + }
521 + if (trim(strip_tags($_POST['mid_location_string'])) == '') {
522 + $taboola_errors[] = "Mid-article > CSS selector";
523 + }
524 + else {
525 + // If the 'location' WAS filled in, then...
526 +
527 + // 1) Check for a valid 'location':
528 +
529 + // Validation method has not been implemented
530 + // mid_if (!$this->is_locaton_string_valid1($_POST['mid_location_string'])) {
531 + // $taboola_errors[] = "Mid-article > CSS Selector (invalid value)";
532 + // }
533 +
534 + // 2) Validate the 'occurrence':
535 + if (!$this->is_mid_location_string_occurrence_valid($_POST['mid_location_string_occurrence'])) {
536 + $taboola_errors[] = "Mid-article > Occurance (must be >= 1)";
537 + }
538 + }
539 +
540 + }
541 +
542 + if(isset($_POST['home_enabled'])) {
543 + if (trim(strip_tags($_POST['home_widget_id'])) == '') {
544 + $taboola_errors[] = "Homepage > Widget ID";
545 + }
546 + if (trim(strip_tags($_POST['home_placement'])) == '') {
547 + $taboola_errors[] = "Homepage > Placement Name";
548 + }
549 +
550 + if (trim(strip_tags($_POST['home_location_string'])) == '') {
551 + $taboola_errors[] = "Homepage > CSS selector";
552 + }
553 + else {
554 + // If the 'location' WAS filled in, then...
555 +
556 + // 1) Check for a valid 'location':
557 +
558 + // Validation method has not been implemented
559 + // if (!$this->is_home_location_string_valid($_POST['home_location_string'])) {
560 + // $taboola_errors[] = "Homepage > CSS Selector (invalid value)";
561 + // }
562 +
563 + // 2) Validate the 'occurrence':
564 + if (!empty($_POST['home_location_string']) && !$this->is_home_location_string_occurrence_valid($_POST['home_location_string_occurrence'])) {
565 + $taboola_errors[] = "Homepage > Occurance (must be >= 1)";
566 + }
567 + }
568 + }
569 +
272 570 if(count($taboola_errors) == 0){
273 -
274 571 $data = array(
275 572 "publisher_id" => trim($_POST['publisher_id']),
276 - "first_bc_enabled" => $_POST['first_bc_enabled'] == 'on' ? true : false,
277 - "first_bc_widget_id" => trim($_POST['first_bc_widget_id']),
278 - "second_bc_enabled" => $_POST['second_bc_enabled'] == 'on' ? true : false,
279 - "second_bc_widget_id" => trim($_POST['second_bc_widget_id']),
280 - "location_string" => trim($_POST['location_string'])
573 +
574 + "web_push_enabled" => isset($_POST['web_push_enabled']) ? true : false,
575 + "publisher_id_push" => !empty($_POST['publisher_id_push']) ? trim($_POST['publisher_id_push']) : '',
576 +
577 + "first_bc_enabled" => isset($_POST['first_bc_enabled']) ? true : false,
578 + "first_bc_widget_id" => !empty($_POST['first_bc_widget_id']) ? trim($_POST['first_bc_widget_id']) : '',
579 + "first_bc_placement" => !empty($_POST['first_bc_placement']) ? trim($_POST['first_bc_placement']) : '',
580 +
581 + "out_of_content_enabled" => isset($_POST['out_of_content_enabled']) ? true : false,
582 +
583 + "mid_enabled" => isset($_POST['mid_enabled']) ? true : false,
584 + "mid_widget_id" => !empty($_POST['mid_widget_id']) ? trim($_POST['mid_widget_id']) : '',
585 + "mid_placement" => !empty($_POST['mid_placement']) ? trim($_POST['mid_placement']) : '',
586 + "mid_paragraph_ui_mode" => !empty($_POST['mid_paragraph_ui_mode']) ? trim($_POST['mid_paragraph_ui_mode']) : '',
587 +
588 + "mid_location_string_occurrence" => !empty($_POST['mid_location_string_occurrence']) ? $_POST['mid_location_string_occurrence'] : '',
589 + "mid_location_string" => !empty($_POST['mid_location_string']) ? trim($_POST['mid_location_string']) : '',
590 +
591 + "home_enabled" => isset($_POST['home_enabled']) ? true : false,
592 + "home_widget_id" => !empty($_POST['home_widget_id']) ? trim($_POST['home_widget_id']) : '',
593 + "home_placement" => !empty($_POST['home_placement']) ? trim($_POST['home_placement']) : '',
594 +
595 + "home_location_string_occurrence" => !empty($_POST['home_location_string_occurrence']) ? $_POST['home_location_string_occurrence'] : '',
596 + "home_location_string" => !empty($_POST['home_location_string']) ? trim($_POST['home_location_string']) : ''
281 597 );
282 598
283 599 //var_dump($settings);
284 - if($settings == NULL){
285 - $wpdb->insert($this->tbl_taboola_settings, $data, null, null);
286 - } else {
287 - $wpdb->update($this->tbl_taboola_settings, $data, array('id' => $settings->id));
600 +
601 + $is_valid_nonce = false;
602 +
603 + if (isset( $_POST['my_plugin_nonce']) && wp_verify_nonce( $_POST['my_plugin_nonce'], 'my_plugin_update_field_action' ) ) {
604 + $is_valid_nonce = true;
288 605 }
606 +
607 + if ($is_valid_nonce) {
608 + if($settings == NULL){
609 + $wpdb->insert($this->tbl_taboola_settings, $data, null, null);
610 + } else {
611 + $wpdb->update($this->tbl_taboola_settings, $data, array('id' => $settings->id));
612 + }
613 + }
289 614 }
290 615 $settings = $wpdb->get_row("select * from ".$wpdb->prefix."_taboola_settings limit 1");
291 616 }
617 + include_once('settings.php');
618 + }
619 +
620 + function is_mid_location_string_valid($mid_location_string){
621 + // TODO:: validate the location string
622 + return true;
623 + }
292 624
625 + function is_mid_location_string_occurrence_valid($mid_location_string_occurrence){
293 626
294 - include_once('settings.php');
627 + if ((int)$mid_location_string_occurrence >= 1) {
628 + return true;
629 + }
630 + return false;
295 631 }
296 632
297 - function is_location_string_valid($location_string){
633 + function is_home_location_string_valid($home_location_string){
298 634 // TODO:: validate the location string
299 635 return true;
300 636 }
301 - function activate(){
637 +
638 + function is_home_location_string_occurrence_valid($home_location_string_occurrence){
639 +
640 + if ((int)$home_location_string_occurrence >= 1) {
641 + return true;
642 + }
643 + return false;
644 + }
645 +
646 + function columnExists($column_name) {
302 647 global $wpdb;
648 + $table_name = $wpdb->prefix . "_taboola_settings";
649 + tb_write_log("table name : " . $table_name);
650 + tb_write_log("column name : " . $column_name);
303 651
652 + $column_exists = $wpdb->get_var("SHOW COLUMNS FROM $table_name LIKE '$column_name'") !== null;
653 +
654 + if ($column_exists) {
655 + tb_write_log("Yes - column " . $column_name . " DOES exist!"); //PC
656 + return true;
657 + } else {
658 + tb_write_log("No - column " . $column_name . " does NOT exist!"); //PC
659 + return false;
660 + }
661 + }
662 +
663 + function is_upgrade_from_v1() {
664 +
665 + if($this->columnExists("first_bc_widget_id") && !$this->columnExists("first_bc_placement")){
666 + tb_write_log("This IS a v1 upgrade!"); //PC
667 + return true;
668 + }
669 + else {
670 + tb_write_log("This is NOT a v1 upgrade!"); //PC
671 + return false;
672 + }
673 + }
674 +
675 + function is_db_updated_for_min_ver($min_ver) {
676 + /**
677 + * Checks if the DB was *previously* updated for the minimum version specified.
678 + * $min_ver should be passed in a format like this: "2.1.0"
679 + */
680 +
681 + global $wpdb;
682 +
683 + // PC - Temporary patch for v2.1.0
684 +
685 + // START patch ===============================================
686 + // Check if the `_taboola` table exists.
687 + // If not, then return false.
688 +
689 + $table_name = $wpdb->prefix . "_taboola_settings";
690 +
691 + if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
692 + tb_write_log("Table NOT FOUND");
693 + return false;
694 + }
695 + else {
696 + tb_write_log("Table EXISTS");
697 + }
698 + // END patch =================================================
699 +
700 + // Check the saved version in wp_options. Default to '1.0.0' if the option is not found.
701 + $saved_version = get_option(TABOOLA_OPTION_NAME, '1.0.0');
702 +
703 + $db_is_up_to_date = version_compare($saved_version, $min_ver, '>=');
704 + //tb_write_log($db_is_up_to_date);
705 + return $db_is_up_to_date;
706 +
707 + }
708 +
709 + function is_db_updated_for_current_ver() {
710 + /**
711 + * DEPRECATED
712 + * Checks if the DB was *previously* updated for the version currently loaded.
713 + */
714 + global $wpdb;
715 +
716 + // Check the saved version in wp_options. Default to '1.0.0' if the option is not found.
717 + $saved_version = get_option(TABOOLA_OPTION_NAME, '1.0.0');
718 +
719 + // Get the currently loaded plugin version.
720 + $plugin_version = $this->get_loaded_plugin_version();
721 +
722 + $db_is_up_to_date = version_compare($saved_version, $plugin_version, '>=');
723 + //tb_write_log($db_is_up_to_date);
724 + return $db_is_up_to_date;
725 +
726 + }
727 +
728 + function get_loaded_plugin_version() {
729 +
730 + $plugin_data = get_plugin_data( __FILE__ ); // Can be invoked from Admin page only
731 + $loaded_plugin_version = $plugin_data['Version'];
732 + return $loaded_plugin_version;
733 +
734 + }
735 +
736 + function save_taboola_version($min_ver) {
737 + /**
738 + * Checks for the saved version in wp_options.
739 + * If not found - adds it. Else - updates it.
740 + */
741 +
742 +
743 + tb_write_log("SAVING NEW MIN VERSION: " . $min_ver); // PC
744 +
745 + $saved_version = get_option(TABOOLA_OPTION_NAME, '');
746 + if ($saved_version == '') {
747 + add_option(TABOOLA_OPTION_NAME, $min_ver);
748 + }
749 + else {
750 + update_option(TABOOLA_OPTION_NAME, $min_ver);
751 + }
752 + }
753 +
754 + function update_db(){
755 +
756 + // If we are up to date, then skip this method...
757 + if ($this->is_db_updated_for_min_ver(TABOOLA_MIN_VER)) {
758 + //tb_write_log("All up to date!");
759 + return;
760 + }
761 +
762 + $this->save_taboola_version(TABOOLA_MIN_VER);
763 +
764 + global $wpdb;
304 765 require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
766 +
305 767
306 - $settings_table = $this->tbl_taboola_settings;
307 -
308 - //check mysql version
309 - if (version_compare(mysql_get_server_info(), '4.1.0', '>=')) {
768 + // Handling for older MySQL versions
769 + if (function_exists('mysql_get_server_info') && version_compare(mysql_get_server_info(), '4.1.0', '>=')) {
310 770 if (!empty($wpdb->charset))
311 771 $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
312 772 if (!empty($wpdb->collate))
313 773 $charset_collate .= " COLLATE $wpdb->collate";
@@ -312,24 +772,47 @@
312 772 if (!empty($wpdb->collate))
313 773 $charset_collate .= " COLLATE $wpdb->collate";
314 774 }
315 775
776 + // Check if this is an upgrade from v1
777 + $is_upgrade_from_v1 = $this->is_upgrade_from_v1();
778 +
779 + // tb_write_log("Is upgrade from v1: " . ($is_upgrade_from_v1 ? 'true' : 'false'));
780 +
781 +
316 782 //settings table structure
317 783 $sql_table_settings = "
318 784 CREATE TABLE `" . $wpdb->prefix . "_taboola_settings` (
319 785 `id` INT NOT NULL AUTO_INCREMENT ,
320 786 `publisher_id` VARCHAR(255) DEFAULT NULL,
787 + `web_push_enabled` TINYINT(1) NOT NULL DEFAULT FALSE,
788 + `publisher_id_push` INT(15) DEFAULT NULL,
321 789 `first_bc_enabled` TINYINT(1) NOT NULL DEFAULT FALSE,
322 790 `first_bc_widget_id` VARCHAR(255) DEFAULT NULL,
791 + `first_bc_placement` VARCHAR(255) DEFAULT " . ($is_upgrade_from_v1 ? "'below-article'" : "NULL") .",
323 792 `first_bc_custom_css` TEXT DEFAULT NULL,
324 793 `second_bc_enabled` TINYINT(1) NOT NULL DEFAULT FALSE,
325 794 `second_bc_widget_id` VARCHAR(255) DEFAULT NULL,
326 795 `second_bc_custom_css` TEXT DEFAULT NULL,
327 796 `location_string` TEXT DEFAULT NULL,
797 + `mid_enabled` TINYINT(1) NOT NULL DEFAULT FALSE,
798 + `mid_widget_id` VARCHAR(255) DEFAULT NULL,
799 + `mid_placement` VARCHAR(255) DEFAULT NULL,
800 + `out_of_content_enabled` TINYINT(1) NOT NULL DEFAULT TRUE,
801 + `mid_location_string` TEXT DEFAULT NULL,
802 + `mid_location_string_occurrence` SMALLINT DEFAULT NULL,
803 + `mid_paragraph_ui_mode` VARCHAR(255) DEFAULT NULL,
804 + `home_enabled` TINYINT(1) NOT NULL DEFAULT FALSE,
805 + `home_widget_id` VARCHAR(255) DEFAULT NULL,
806 + `home_placement` VARCHAR(255) DEFAULT NULL,
807 + `home_location_string` TEXT DEFAULT NULL,
808 + `home_location_string_occurrence` SMALLINT DEFAULT NULL,
328 809 PRIMARY KEY (`id`)
329 810 )" . $charset_collate . ";";
811 +
812 + // tb_write_log($sql_table_settings);
330 813
331 - // creat/update the table
814 + // create/update the table
332 815 dbDelta($sql_table_settings);
333 816 }
334 817 }
335 818 }
@@ -335,6 +818,52 @@
335 818 }
336 819
337 820 global $taboolaWP;
338 821 $taboolaWP = new TaboolaWP();
822 +
823 +/*
824 +A few utility methods for logging
825 +*/
826 +
827 +// Writes debugging messages to 'logs/debug.log'.
828 +// The exact file location depends on *where* you trigger the function.
829 +// In this script, we trigger the function from the admin dashboard.
830 +// So the log file is located under 'wp-admin/logs'.
831 +function tb_write_log($log_msg)
832 +{
833 + $log_filename = "logs";
834 + if (!file_exists($log_filename))
835 + {
836 + mkdir($log_filename, 0777, true);
837 + }
838 + $log_file_data = $log_filename.'/debug.log';
839 +
840 + // Add date/time
841 + $date = date('Y-m-d H:i:s');
842 + $log_msg_with_date = $date." : ".$log_msg;
843 +
844 + if (TABOOLA_DEBUG_MODE)
845 + file_put_contents($log_file_data, $log_msg_with_date . "\n", FILE_APPEND);
846 +
847 +}
848 +
849 +// Write to console
850 +function tb_console_log( $data ){
851 +
852 + If(wp_get_environment_type() === 'development') {
853 +
854 + echo '<script>';
855 + echo 'console.log('. json_encode( $data ) .')';
856 + echo '</script>';
857 +
858 + }
859 +}
860 +
861 +// Write to webpage and stop
862 +function tb_print_to_page($data) {
863 + If(wp_get_environment_type() === 'development') {
864 + print_r($data);
865 + die;
866 + }
867 +}
339 868
340 869 //