PluginProbe
Taboola / 2.1.0
Taboola v2.1.0
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 +211 -367 3.0.02.1.0 View file →
@@ -1,193 +1,86 @@
1 1 <?php
2 2 /**
3 - * Plugin Name: Taboola
4 - * Plugin URI: https://developers.taboola.com/web-integrations/docs/wordpress-plugin
5 - * Description: Taboola
6 - * Version: 3.0.0
7 - * Author: Taboola
3 + * Plugin Name: Taboola
4 + * Plugin URI: https://developers.taboola.com/web-integrations/docs/wordpress-plugin
5 + * Description: Taboola
6 + * Version: 2.1.0
7 + * Author: Taboola
8 8 */
9 9
10 -define( 'TABOOLA_PLUGIN_VERSION', '3.0' ); // track every release
11 -define( 'TABOOLA_MIN_VER', '3.0' ); // bump only when DB changes
12 -define( 'TABOOLA_DEBUG_MODE', false );
10 +define ("TABOOLA_PLUGIN_VERSION","2.1.0"); // => UPDATE THIS FOR *EVERY* RELEASE (USED FOR TRACKING)
11 +define ("TABOOLA_MIN_VER","2.0.1"); // => 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 13
14 -define( 'TABOOLA_OPTION_NAME', 'taboola_plugin_version' );
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 15
16 -define( 'TABOOLA_XPATH_MARKER', '/' );
17 -define( 'TABOOLA_JS_INDICATOR', '{JS}' );
18 -define( 'TABOOLA_JS_MARKER', '{' );
19 -define( 'TABOOLA_CONTENT_FORMAT_STRING', 'string' );
20 -define( 'TABOOLA_CONTENT_FORMAT_SCRIPT', 'script' );
21 -define( 'TABOOLA_CONTENT_FORMAT_HTML', 'html' );
16 +define ("TABOOLA_XPATH_MARKER","/");
17 +define ("TABOOLA_JS_INDICATOR","{JS}");
18 +define ("TABOOLA_JS_MARKER","{");
19 +define ("TABOOLA_CONTENT_FORMAT_STRING",'string');
20 +define ("TABOOLA_CONTENT_FORMAT_SCRIPT",'script');
21 +define ("TABOOLA_CONTENT_FORMAT_HTML",'html');
22 22
23 -include_once 'widget.php';
24 -require_once 'JavaScriptWrapper.php';
25 -require_once plugin_dir_path( __FILE__ ) . 'simple_html_dom.php'; // ← NEW
26 23
27 -if ( ! class_exists( 'TaboolaWP' ) ) {
28 -class TaboolaWP {
24 +include_once('widget.php');
25 +require_once('JavaScriptWrapper.php');
29 26
30 - /* ─────────────────────────── properties ─────────────────────────── */
31 - public $data = [];
32 - public $_is_widget_on_page;
33 - public $_is_head_script_loaded = false;
34 27
35 - private $tpl_sw = 'importScripts("https://cdn.taboola.com/webpush/tsw.js");';
36 - private $msg_sw_error = <<<SWE
37 - <p>The file <code>sw.js</code> in the WP root is not writable.
38 - Please change its permissions or paste the code below manually:</p>
39 - <pre><code>{{SW}}</code></pre>
40 - <p>Also make sure it's reachable at <code>{{DOMAIN}}/sw.js</code></p>
41 -SWE;
28 +if (!class_exists('TaboolaWP')) {
29 + class TaboolaWP
30 + {
31 + //save internal data
32 + public $data = array();
33 + public $_is_widget_on_page;
34 + public $_is_head_script_loaded = false;
42 35
43 - public $plugin_name;
44 - public $plugin_directory;
45 - public $plugin_url;
46 - public $settings;
47 - public $tbl_taboola_settings;
36 + function __construct()
37 + {
38 + global $wpdb;
48 39
49 - /* ─────────────────────────── constructor ─────────────────────────── */
50 - public function __construct() {
51 - global $wpdb;
40 + //initialize plugin constant
41 + DEFINE('TaboolaWP', true);
52 42
53 - define( 'TaboolaWP', true );
43 + $this->_is_widget_on_page = false;
44 + $this->_is_head_script_loaded = false;
54 45
55 - $this->_is_widget_on_page = false;
56 - $this->_is_head_script_loaded = false;
46 + $this->plugin_name = plugin_basename(__FILE__);
47 + $this->plugin_directory = plugin_dir_path(__FILE__);
48 + $this->plugin_url = trailingslashit(WP_PLUGIN_URL . '/' . dirname(plugin_basename(__FILE__)));
49 + $this->settings = $wpdb->get_row("select * from ".$wpdb->prefix."_taboola_settings limit 1");
57 50
58 - $this->plugin_name = plugin_basename( __FILE__ );
59 - $this->plugin_directory = plugin_dir_path( __FILE__ );
60 - $this->plugin_url = trailingslashit(
61 - WP_PLUGIN_URL . '/' . dirname( plugin_basename( __FILE__ ) )
62 - );
63 - $this->tbl_taboola_settings = $wpdb->prefix . '_taboola_settings';
64 - $this->settings = $wpdb->get_row(
65 - "SELECT * FROM {$this->tbl_taboola_settings} LIMIT 1"
66 - );
51 + $this->tbl_taboola_settings = $wpdb->prefix . '_taboola_settings';
67 52
68 - /* activation / upgrade */
69 - register_activation_hook( $this->plugin_name, [ $this, 'update_db' ] );
70 - add_action( 'admin_init', [ $this, 'update_db' ] );
53 + //activation function
54 + register_activation_hook($this->plugin_name, array(&$this, 'update_db'));
55 + add_action('admin_init', array(&$this, 'update_db'));
71 56
72 - /* widgets */
73 - if ( $this->settings && ! empty( $this->settings->publisher_id ) ) {
74 - add_action(
75 - 'widgets_init',
76 - static fn() => register_widget( 'WP_Widget_Taboola' )
77 - );
78 - }
57 + // Enable sidebar widgets
58 + if ($this->settings != NULL && !empty($this->settings->publisher_id)){
59 + //register Taboola widget
60 + add_action('widgets_init',
61 + function(){
62 + return register_widget("WP_Widget_Taboola");
63 + }
64 + );
65 + }
79 66
80 - /* admin vs front-end hooks */
81 - if ( is_admin() ) {
82 - add_action( 'admin_menu', [ $this, 'admin_generate_menu' ] );
83 - add_filter( 'plugin_action_links',
84 - [ $this, 'plugin_action_links' ], 10, 2 );
85 - } elseif ( $this->settings ) {
86 - /* loader & flush */
87 - add_action( 'wp_head', [ $this, 'taboola_header_loader_inject' ] );
88 - add_action( 'wp_footer', [ $this, 'taboola_footer_loader_js' ] );
67 + //$this->should_place_tag_outside_of_content = $this->settings->out_of_content_enabled;
89 68
90 - /* content widgets */
91 - add_filter( 'the_content', [ $this, 'load_taboola_content' ] );
92 - add_filter( 'the_content', [ $this, 'load_taboola_content_mid' ] );
93 -
94 - /* full-page buffer – home & category */
95 - add_action( 'template_redirect', [ $this, 'tb_home_buffer_start' ], 0 );
96 - add_action( 'shutdown', [ $this, 'tb_home_buffer_flush' ], PHP_INT_MAX );
97 -
98 - add_action( 'template_redirect', [ $this, 'tb_cat_buffer_start' ], 0 );
99 - add_action( 'shutdown', [ $this, 'tb_cat_buffer_flush' ], PHP_INT_MAX );
100 - }
101 - } // __construct
102 -
103 -
104 -/* ======================================================================
105 - CATEGORY ▸ should we show the widget?
106 - ====================================================================== */
107 -private function should_show_content_widget_category(): bool {
108 - return (
109 - trim( $this->settings->publisher_id ) !== '' &&
110 - ( is_category() || is_archive() || is_search() ) &&
111 - $this->settings->category_enabled &&
112 - trim( $this->settings->category_widget_id ) !== ''
113 - );
114 -}
115 -
116 -/* ----------------------------------------------------------------------
117 - HOMEPAGE ▸ full-page buffer
118 - -------------------------------------------------------------------- */
119 -public function tb_home_buffer_start() {
120 - if ( $this->should_show_content_widget_home() ) {
121 - ob_start( [ $this, 'tb_home_buffer_inject' ] );
69 + if (is_admin()) {
70 + //add menu for plugin
71 + add_action( 'admin_menu', array(&$this, 'admin_generate_menu') );
72 + add_filter('plugin_action_links', array(&$this, 'plugin_action_links'), 10, 2 );
73 + }else if ($this->settings != NULL){
74 + add_action('wp_head', array(&$this, 'taboola_header_loader_inject'));
75 + add_action('wp_footer', array(&$this, 'taboola_footer_loader_js'));
76 + add_filter('the_content', array(&$this, 'load_taboola_content'));
77 + add_filter('the_content', array(&$this, 'load_taboola_content_mid'));
78 + add_filter('the_content', array(&$this, 'load_taboola_content_home'));
79 + }
122 80 }
123 -}
124 -public function tb_home_buffer_inject( $html ) {
125 - return $this->load_taboola_content_home( $html );
126 -}
127 81
128 -/* ----------------------------------------------------------------------
129 - CATEGORY ▸ buffer hooks
130 - -------------------------------------------------------------------- */
131 -public function tb_cat_buffer_start() {
132 - if ( $this->should_show_content_widget_category() ) {
133 - ob_start( [ $this, 'tb_cat_buffer_inject' ] );
134 - }
135 -}
136 -public function tb_cat_buffer_inject( $html ) {
137 - return $this->load_taboola_content_category( $html );
138 -}
139 -public function tb_cat_buffer_flush() {
140 - if ( ob_get_length() ) {
141 - echo ob_get_clean();
142 - }
143 -}
144 -
145 -/* ----------------------------------------------------------------------
146 - CATEGORY ▸ choose where to inject
147 - -------------------------------------------------------------------- */
148 -private function embed_taboola_content_location_category(
149 - string $html,
150 - array $taboola_content,
151 - string $selector ) : string {
152 -
153 - $formatted = $this->format_taboola_content_category( $taboola_content );
154 -
155 - /* A. no selector → bottom of page */
156 - if ( $selector === '' ) {
157 - return str_ireplace( '</body>', $formatted . '</body>', $html );
158 - }
159 -
160 - /* B. selector given → try server-side injection */
161 - $doc = str_get_html( $html );
162 - if ( ! $doc ) {
163 - // parser failed → graceful fallback
164 - return str_ireplace( '</body>', $formatted . '</body>', $html );
165 - }
166 -
167 - $occurrence = max( 0, $this->settings->category_location_string_occurrence - 1 );
168 - $target = $doc->find( $selector, $occurrence );
169 -
170 - if ( $target ) { // selector found
171 - $target->outertext .= $formatted;
172 - return (string) $doc; // done – no duplicate
173 - }
174 -
175 - /* C. selector not found → bottom of page */
176 - return str_ireplace( '</body>', $formatted . '</body>', $html );
177 -}
178 -
179 -/* ------------------------------------------------------------------ */
180 -public function tb_home_buffer_flush() {
181 - if ( ob_get_length() ) {
182 - echo ob_get_clean();
183 - }
184 -}
185 -
186 -
187 -
188 -
189 -function plugin_action_links($links, $file) {
82 + function plugin_action_links($links, $file) {
190 83 static $this_plugin;
191 84
192 85 if (!$this_plugin) {
193 86 $this_plugin = plugin_basename(__FILE__);
@@ -209,20 +102,17 @@
209 102 private function should_show_content_widget_mid(){
210 103 // PC - only if v2 was installed:
211 104 if (!$this->is_db_updated_for_min_ver("2.0.0"))
212 105 return false;
213 - $retVal1 = ((trim($this->settings->publisher_id) != '') && is_single() && !empty($this->settings->mid_enabled));
106 + $retVal1 = ((trim($this->settings->publisher_id) != '') && is_single() && $this->settings->mid_enabled && trim($this->settings->mid_widget_id) != '');
214 107 return $retVal1;
215 108 }
216 109
217 110 private function should_show_content_widget_home(){
218 111 // PC - only if v2 was installed:
219 -
220 - //error_log( print_r( $this->settings, true ) ); // TEMP line
221 -
222 112 if (!$this->is_db_updated_for_min_ver("2.0.0"))
223 113 return false;
224 - $retVal2 = ((trim($this->settings->publisher_id) != '') && (is_front_page() || is_home()) && $this->settings->home_enabled && trim($this->settings->home_widget_id) != '');
114 + $retVal2 = ((trim($this->settings->publisher_id) != '') && is_front_page() && $this->settings->home_enabled && trim($this->settings->home_widget_id) != '');
225 115 return $retVal2;
226 116 }
227 117
228 118 private function should_show_sidebar_widget(){
@@ -231,9 +121,9 @@
231 121 }
232 122
233 123 // Determine if a taboola widget should be added somewhere on the current page (content or sidebar)
234 124 function is_widget_on_page(){
235 - return $this->should_show_content_widget() || $this->should_show_content_widget_mid() || $this->should_show_content_widget_home() || $this->should_show_sidebar_widget() || $this->should_show_content_widget_category() ;
125 + return $this->should_show_content_widget() || $this->should_show_content_widget_mid() || $this->should_show_content_widget_home() || $this->should_show_sidebar_widget();
236 126 }
237 127
238 128 function get_page_type(){
239 129 $page_type='article';
@@ -265,8 +155,9 @@
265 155
266 156 $scriptWrapper = new JavaScriptWrapper("loaderInjectionScript.js",$stringParams);
267 157 $head_string = $scriptWrapper->getScriptMarkupString();
268 158 }
159 +
269 160 return $head_string;
270 161 }
271 162
272 163
@@ -274,23 +165,13 @@
274 165 function taboola_header_loader_inject(){
275 166 echo $this->taboola_header_loader_js();
276 167 }
277 168
278 - // adding webpush loader
279 - function taboola_webpush_loader_js() {
280 - if(is_single() || is_home() || is_category() || is_front_page()){
281 - $stringParamsWeb = array(
282 - '{{PUBLISHER_ID}}' => $this->settings->publisher_id_push
283 - );
284 - $webpushInjectionScript = new JavaScriptWrapper("webPush.js",$stringParamsWeb);
285 - echo $webpushInjectionScript;
286 - }
287 - }
288 169
289 170 function taboola_footer_loader_js() {
290 171
291 172 // Only adding flush script if a widget is going to be placed on the page.
292 - if ($this->is_widget_on_page()){
173 + if ( $this->is_widget_on_page() ){
293 174 $flushInjectionScript = new JavaScriptWrapper('flushInjectionScript.js',array());
294 175 echo $flushInjectionScript->getScriptMarkupString();
295 176 }
296 177 }
@@ -318,37 +199,24 @@
318 199 return $content;
319 200 }
320 201
321 202 // Mid-article-widget
322 - function load_taboola_content_mid($content) {
203 + function load_taboola_content_mid($content)
204 + {
205 + $taboola_content_mid = array();
323 206 if ($this->should_show_content_widget_mid()){
324 - $mid_widgets = json_decode($this->settings->mid_widgets ?? '[]');
325 207
326 - if (is_array($mid_widgets) && !empty($mid_widgets)) {
327 - foreach ($mid_widgets as $index => $widget) {
328 - $container_id = 'taboola-mid-article-thumbnails-' . $index;
329 -
330 - $widgetParams = array(
331 - '{{WIDGET_ID}}' => $widget->widget_id,
332 - '{{CONTAINER}}' => $container_id,
333 - '{{PLACEMENT}}' => $widget->placement
334 - );
208 + $secondWidgetParams = array('{{WIDGET_ID}}' => $this->settings->mid_widget_id,
209 + '{{CONTAINER}}' => 'taboola-mid-article-thumbnails',
210 + '{{PLACEMENT}}' => $this->settings->mid_placement);
211 +
212 + $secondWidgetScript = new JavaScriptWrapper("widgetInjectionScript.js",$secondWidgetParams);
213 + $taboola_content_mid[TABOOLA_CONTENT_FORMAT_HTML][] = "<div id='taboola-mid-article-thumbnails'></div>";
214 + $taboola_content_mid[TABOOLA_CONTENT_FORMAT_SCRIPT][] = $secondWidgetScript;
335 215
336 - $widgetScript = new JavaScriptWrapper("widgetInjectionScript.js", $widgetParams);
337 - $taboola_content_mid = array(
338 - TABOOLA_CONTENT_FORMAT_HTML => array("<div id='{$container_id}'></div>"),
339 - TABOOLA_CONTENT_FORMAT_SCRIPT => array($widgetScript),
340 - );
216 + $content = $this->embed_taboola_content_location_mid($content,$taboola_content_mid,trim($this->settings->mid_location_string));
217 + }
341 218
342 - $content = $this->embed_taboola_content_location_mid(
343 - $content,
344 - $taboola_content_mid,
345 - $widget->location_string,
346 - $widget->occurrence
347 - );
348 - }
349 - }
350 - }
351 219 return $content;
352 220 }
353 221
354 222 // Homepage widget
@@ -353,9 +221,9 @@
353 221
354 222 // Homepage widget
355 223 function load_taboola_content_home($content)
356 224 {
357 - //error_log( 'TB-DEBUG ② entered load_taboola_content_home' );
225 +
358 226 $taboola_content_home = array();
359 227 if ($this->should_show_content_widget_home()){
360 228
361 229 $homeWidgetParams = array('{{WIDGET_ID}}' => $this->settings->home_widget_id,
@@ -370,33 +238,9 @@
370 238 }
371 239 return $content;
372 240
373 241 }
374 - /* ------------------------------------------------------------------
375 - * CATEGORY ▸ build widget & inject
376 - * ------------------------------------------------------------------*/
377 -public function load_taboola_content_category( $content ) {
378 242
379 - // 1) Build the DIV that Taboola will fill
380 - $taboola[TABOOLA_CONTENT_FORMAT_HTML][] =
381 - "<div id='taboola-category-thumbnails'></div>";
382 -
383 - // 2) Build the JS that loads the feed (reuse the wrapper)
384 - $taboola[TABOOLA_CONTENT_FORMAT_SCRIPT][] =
385 - new JavaScriptWrapper( 'widgetInjectionScript.js', [
386 - '{{WIDGET_ID}}' => $this->settings->category_widget_id,
387 - '{{CONTAINER}}' => 'taboola-category-thumbnails',
388 - '{{PLACEMENT}}' => $this->settings->category_placement,
389 - ]);
390 -
391 - // 3) Decide where to insert (selector or end of <body>)
392 - return $this->embed_taboola_content_location_category(
393 - $content,
394 - $taboola,
395 - trim( $this->settings->category_location_string )
396 - );
397 -}
398 -
399 243 // Below-article widget
400 244
401 245 // Extract the taboola content in the required format:
402 246 // String - for injecting on the servr side
@@ -463,18 +307,8 @@
463 307 return $ret_val;
464 308
465 309 }
466 310
467 -
468 - // ► CATEGORY formatter ◄
469 -private function format_taboola_content_category( $arr ) {
470 - return implode( "\n", [
471 - implode( "\n", $arr[TABOOLA_CONTENT_FORMAT_HTML] ?? [] ),
472 - '<script type="text/javascript">' .
473 - implode( "\n", $arr[TABOOLA_CONTENT_FORMAT_SCRIPT] ?? [] ) .
474 - '</script>',
475 - ]);
476 -}
477 311 // Below-article widget
478 312 // Do the actual logic of choosing where to place the taboola content.
479 313 function embed_taboola_content_location($content, $taboola_content){
480 314 $do_default = true;
@@ -500,9 +334,9 @@
500 334 }
501 335
502 336 // Mid-article widget
503 337 // Do the actual logic of choosing where to place the taboola content based on the "location" attribute
504 - function embed_taboola_content_location_mid($content, $taboola_content_mid, $location, $occurrence = 1){
338 + function embed_taboola_content_location_mid($content, $taboola_content_mid, $location){
505 339 $do_default = true;
506 340
507 341 if (isset($location) && $location != ''){
508 342 $first_char = substr($location,0,1);
@@ -529,9 +363,9 @@
529 363 } else{
530 364 require_once('simple_html_dom.php');
531 365
532 366 $html_doc = str_get_html($content);
533 - $target_location = $html_doc->find($location, ($occurrence) - 1);
367 + $target_location = $html_doc->find($location,($this->settings->mid_location_string_occurrence)-1);
534 368
535 369 // if the location was found within the html content
536 370 if (isset($target_location) && is_object($target_location)){
537 371
@@ -541,8 +375,12 @@
541 375 $do_default = false;
542 376 }
543 377 }
544 378 }
379 + // Default for below-article widget - add to the end of the content
380 + // if ($do_default){
381 + // $content = $content.$this->format_taboola_content_mid($taboola_content_mid,TABOOLA_CONTENT_FORMAT_STRING);
382 + // }
545 383
546 384 return $content;
547 385 }
548 386
@@ -549,22 +387,8 @@
549 387 // Homepage widget
550 388 // Do the actual logic of choosing where to place the taboola content based on the "location" attribute
551 389 function embed_taboola_content_location_home($content, $taboola_content_home, $location){
552 390
553 -
554 - // error_log( 'TB-DEBUG ③ raw selector string: [' . $location . ']' );
555 -
556 - // load parser
557 - //require_once plugin_dir_path( __FILE__ ) . 'simple_html_dom.php';
558 - // $html_doc = str_get_html( $content );
559 -
560 - // occurrence: settings is 1-based, find() wants 0-based
561 - // $occurrence = max( 1, intval( $this->settings->home_location_string_occurrence ) );
562 - // $node = $html_doc->find( $location, $occurrence - 1 );
563 - // $found = is_object( $node ) ? 1 : 0;
564 -
565 - // error_log( "TB-DEBUG ③ found {$found} matching node(s) for selector [{$location}]" );
566 - // ────────────────────────────
567 391 $do_default = true;
568 392
569 393 if (isset($location) && $location != ''){
570 394 $first_char = substr($location,0,1);
@@ -604,22 +428,22 @@
604 428 }
605 429 }
606 430 }
607 431 // Default for below-article widget - add to the end of the content
608 - if ($do_default){
609 - $content = $content.$this->format_taboola_content_home($taboola_content_home,TABOOLA_CONTENT_FORMAT_STRING);
610 - }
432 + // if ($do_default){
433 + // $content = $content.$this->format_taboola_content_home($taboola_content_home,TABOOLA_CONTENT_FORMAT_STRING);
434 + // }
611 435 return $content;
612 -
613 436
614 437 }
615 438
616 439 function admin_generate_menu(){
617 440 global $current_user;
618 - 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);
441 + add_menu_page('Taboola', 'Taboola', 'manage_options', 'taboola_widget', array(&$this, 'admin_taboola_settings'), $this->plugin_url.'img/taboola_icon.png', 110);
619 442 }
620 443
621 444 function admin_taboola_settings(){
445 +
622 446 global $wpdb;
623 447 $settings = $wpdb->get_row("select * from ".$wpdb->prefix."_taboola_settings limit 1");
624 448 $taboola_errors = array();
625 449 if($_SERVER['REQUEST_METHOD'] == 'POST'){
@@ -627,14 +451,8 @@
627 451 if(trim(strip_tags($_POST['publisher_id'])) == ''){
628 452 $taboola_errors[] = "Publisher ID";
629 453 }
630 454
631 - if(isset($_POST['web_push_enabled'])) {
632 - if (trim(strip_tags($_POST['publisher_id_push'])) == '') {
633 - $taboola_errors[] = "Publisher Push ID";
634 - }
635 - }
636 -
637 455 if(isset($_POST['first_bc_enabled'])) {
638 456 if (trim(strip_tags($_POST['first_bc_widget_id'])) == '') {
639 457 $taboola_errors[] = "Below-article > Widget ID";
640 458 }
@@ -643,18 +461,33 @@
643 461 }
644 462 }
645 463
646 464 if(isset($_POST['mid_enabled'])) {
647 - if(isset($_POST['mid_widget_id']) && is_array($_POST['mid_widget_id'])) {
648 - foreach ($_POST['mid_widget_id'] as $index => $widget_id) {
649 - if(trim($widget_id) == '') {
650 - $taboola_errors[] = "Mid-article Widget #".($index + 1)." > Widget ID";
651 - }
652 - if(trim($_POST['mid_placement'][$index]) == '') {
653 - $taboola_errors[] = "Mid-article Widget #".($index + 1)." > Placement Name";
654 - }
465 + if (trim(strip_tags($_POST['mid_widget_id'])) == '') {
466 + $taboola_errors[] = "Mid-article > Widget ID";
467 + }
468 + if (trim(strip_tags($_POST['mid_placement'])) == '') {
469 + $taboola_errors[] = "Mid-article > Placement Name";
470 + }
471 + if (trim(strip_tags($_POST['mid_location_string'])) == '') {
472 + $taboola_errors[] = "Mid-article > CSS selector";
473 + }
474 + else {
475 + // If the 'location' WAS filled in, then...
476 +
477 + // 1) Check for a valid 'location':
478 +
479 + // Validation method has not been implemented
480 + // mid_if (!$this->is_locaton_string_valid1($_POST['mid_location_string'])) {
481 + // $taboola_errors[] = "Mid-article > CSS Selector (invalid value)";
482 + // }
483 +
484 + // 2) Validate the 'occurrence':
485 + if (!$this->is_mid_location_string_occurrence_valid($_POST['mid_location_string_occurrence'])) {
486 + $taboola_errors[] = "Mid-article > Occurance (must be >= 1)";
655 487 }
656 488 }
489 +
657 490 }
658 491
659 492 if(isset($_POST['home_enabled'])) {
660 493 if (trim(strip_tags($_POST['home_widget_id'])) == '') {
@@ -667,51 +500,29 @@
667 500 if (trim(strip_tags($_POST['home_location_string'])) == '') {
668 501 $taboola_errors[] = "Homepage > CSS selector";
669 502 }
670 503 else {
504 + // If the 'location' WAS filled in, then...
505 +
506 + // 1) Check for a valid 'location':
507 +
508 + // Validation method has not been implemented
509 + // if (!$this->is_home_location_string_valid($_POST['home_location_string'])) {
510 + // $taboola_errors[] = "Homepage > CSS Selector (invalid value)";
511 + // }
512 +
513 + // 2) Validate the 'occurrence':
671 514 if (!empty($_POST['home_location_string']) && !$this->is_home_location_string_occurrence_valid($_POST['home_location_string_occurrence'])) {
672 515 $taboola_errors[] = "Homepage > Occurance (must be >= 1)";
673 516 }
674 517 }
675 - }
676 - if ( isset($_POST['category_enabled']) ) {
677 - if ( trim(strip_tags($_POST['category_widget_id'])) == '' )
678 - $taboola_errors[] = "Category > Widget ID";
679 - if ( trim(strip_tags($_POST['category_placement'])) == '' )
680 - $taboola_errors[] = "Category > Placement Name";
681 - }
518 + }
682 519
683 520 if(count($taboola_errors) == 0){
684 -
685 - $mid_widgets_data = array();
686 - if (isset($_POST['mid_widget_id']) && is_array($_POST['mid_widget_id'])) {
687 - foreach ($_POST['mid_widget_id'] as $index => $widget_id) {
688 - if (!empty(trim($widget_id))) {
689 -
690 - $location_string = 'p';
691 - if (isset($_POST['mid_paragraph_ui_mode'][$index]) && $_POST['mid_paragraph_ui_mode'][$index] === 'Other') {
692 - if (isset($_POST['mid_location_string'][$index])) {
693 - $location_string = sanitize_text_field($_POST['mid_location_string'][$index]);
694 - }
695 - }
696 521
697 - $mid_widgets_data[] = array(
698 - 'widget_id' => sanitize_text_field($widget_id),
699 - 'placement' => sanitize_text_field($_POST['mid_placement'][$index]),
700 - 'location_string' => $location_string,
701 - 'occurrence' => intval($_POST['mid_location_string_occurrence'][$index]),
702 - );
703 - }
704 - }
705 - }
706 - $mid_widgets_json = json_encode($mid_widgets_data);
707 -
708 522 $data = array(
709 523 "publisher_id" => trim($_POST['publisher_id']),
710 524
711 - "web_push_enabled" => isset($_POST['web_push_enabled']) ? true : false,
712 - "publisher_id_push" => !empty($_POST['publisher_id_push']) ? trim($_POST['publisher_id_push']) : '',
713 -
714 525 "first_bc_enabled" => isset($_POST['first_bc_enabled']) ? true : false,
715 526 "first_bc_widget_id" => !empty($_POST['first_bc_widget_id']) ? trim($_POST['first_bc_widget_id']) : '',
716 527 "first_bc_placement" => !empty($_POST['first_bc_placement']) ? trim($_POST['first_bc_placement']) : '',
717 528
@@ -717,23 +528,24 @@
717 528
718 529 "out_of_content_enabled" => isset($_POST['out_of_content_enabled']) ? true : false,
719 530
720 531 "mid_enabled" => isset($_POST['mid_enabled']) ? true : false,
721 - "mid_widgets" => $mid_widgets_json,
722 -
532 + "mid_widget_id" => !empty($_POST['mid_widget_id']) ? trim($_POST['mid_widget_id']) : '',
533 + "mid_placement" => !empty($_POST['mid_placement']) ? trim($_POST['mid_placement']) : '',
534 + "mid_paragraph_ui_mode" => !empty($_POST['mid_paragraph_ui_mode']) ? trim($_POST['mid_paragraph_ui_mode']) : '',
535 +
536 + "mid_location_string_occurrence" => !empty($_POST['mid_location_string_occurrence']) ? $_POST['mid_location_string_occurrence'] : '',
537 + "mid_location_string" => !empty($_POST['mid_location_string']) ? trim($_POST['mid_location_string']) : '',
538 +
723 539 "home_enabled" => isset($_POST['home_enabled']) ? true : false,
724 540 "home_widget_id" => !empty($_POST['home_widget_id']) ? trim($_POST['home_widget_id']) : '',
725 541 "home_placement" => !empty($_POST['home_placement']) ? trim($_POST['home_placement']) : '',
726 542
727 543 "home_location_string_occurrence" => !empty($_POST['home_location_string_occurrence']) ? $_POST['home_location_string_occurrence'] : '',
728 - "home_location_string" => !empty($_POST['home_location_string']) ? trim($_POST['home_location_string']) : '',
729 - "category_enabled" => isset($_POST['category_enabled']) ? true : false,
730 - "category_widget_id" => !empty($_POST['category_widget_id']) ? trim($_POST['category_widget_id']) : '',
731 - "category_placement" => !empty($_POST['category_placement']) ? trim($_POST['category_placement']) : '',
732 - "category_location_string_occurrence" => !empty($_POST['category_location_string_occurrence']) ? $_POST['category_location_string_occurrence'] : '',
733 - "category_location_string" => !empty($_POST['category_location_string']) ? trim($_POST['category_location_string']) : '',
544 + "home_location_string" => !empty($_POST['home_location_string']) ? trim($_POST['home_location_string']) : ''
545 + );
734 546
735 - );
547 + //var_dump($settings);
736 548
737 549 $is_valid_nonce = false;
738 550
739 551 if (isset( $_POST['my_plugin_nonce']) && wp_verify_nonce( $_POST['my_plugin_nonce'], 'my_plugin_update_field_action' ) ) {
@@ -741,19 +553,21 @@
741 553 }
742 554
743 555 if ($is_valid_nonce) {
744 556 if($settings == NULL){
745 - $wpdb->insert($this->tbl_taboola_settings, $data);
557 + $wpdb->insert($this->tbl_taboola_settings, $data, null, null);
746 558 } else {
747 559 $wpdb->update($this->tbl_taboola_settings, $data, array('id' => $settings->id));
748 560 }
749 561 }
562 +
750 563 }
751 564 $settings = $wpdb->get_row("select * from ".$wpdb->prefix."_taboola_settings limit 1");
752 565 }
566 +
753 567 include_once('settings.php');
754 568 }
755 -
569 +
756 570 function is_mid_location_string_valid($mid_location_string){
757 571 // TODO:: validate the location string
758 572 return true;
759 573 }
@@ -777,9 +591,8 @@
777 591 return true;
778 592 }
779 593 return false;
780 594 }
781 -
782 595 function columnExists($column_name) {
783 596 global $wpdb;
784 597 $table_name = $wpdb->prefix . "_taboola_settings";
785 598 tb_write_log("table name : " . $table_name);
@@ -805,44 +618,60 @@
805 618 else {
806 619 tb_write_log("This is NOT a v1 upgrade!"); //PC
807 620 return false;
808 621 }
622 +
809 623 }
810 624
811 625 function is_db_updated_for_min_ver($min_ver) {
626 + /**
627 + * Checks if the DB was *previously* updated for the minimum version specified.
628 + * $min_ver should be passed in a format like this: "2.1.0"
629 + */
812 630 global $wpdb;
813 - $table_name = $wpdb->prefix . "_taboola_settings";
814 631
815 - if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
816 - tb_write_log("Table NOT FOUND");
817 - return false;
818 - }
819 - else {
820 - tb_write_log("Table EXISTS");
821 - }
632 + // Check the saved version in wp_options. Default to '1.0.0' if the option is not found.
822 633 $saved_version = get_option(TABOOLA_OPTION_NAME, '1.0.0');
823 634
824 635 $db_is_up_to_date = version_compare($saved_version, $min_ver, '>=');
636 + //tb_write_log($db_is_up_to_date);
825 637 return $db_is_up_to_date;
826 638
827 639 }
828 640
829 641 function is_db_updated_for_current_ver() {
642 + /**
643 + * Checks if the DB was *previously* updated for the version currently loaded.
644 + */
830 645 global $wpdb;
646 +
647 + // Check the saved version in wp_options. Default to '1.0.0' if the option is not found.
831 648 $saved_version = get_option(TABOOLA_OPTION_NAME, '1.0.0');
649 +
650 + // Get the currently loaded plugin version from wp_options.
832 651 $plugin_version = $this->get_loaded_plugin_version();
652 +
833 653 $db_is_up_to_date = version_compare($saved_version, $plugin_version, '>=');
654 + //tb_write_log($db_is_up_to_date);
834 655 return $db_is_up_to_date;
656 +
835 657 }
836 658
837 659 function get_loaded_plugin_version() {
838 - $plugin_data = get_plugin_data( __FILE__ );
660 +
661 + $plugin_data = get_plugin_data( __FILE__ ); // Can be invoked from Admin page only
839 662 $loaded_plugin_version = $plugin_data['Version'];
840 663 return $loaded_plugin_version;
664 +
841 665 }
842 666
843 667 function save_taboola_version($min_ver) {
668 + // Get the currently loaded plugin version
669 + // $plugin_version = $this->get_loaded_plugin_version();
670 +
844 671 tb_write_log("SAVING NEW MIN VERSION: " . $min_ver); // PC
672 +
673 + // Check the saved version in wp_options. Default to '' if the option is not found.
845 674 $saved_version = get_option(TABOOLA_OPTION_NAME, '');
846 675 if ($saved_version == '') {
847 676 add_option(TABOOLA_OPTION_NAME, $min_ver);
848 677 }
@@ -851,9 +680,12 @@
851 680 }
852 681 }
853 682
854 683 function update_db(){
684 +
685 + // If we are up to date, then skip this method...
855 686 if ($this->is_db_updated_for_min_ver(TABOOLA_MIN_VER)) {
687 + //tb_write_log("All up to date!");
856 688 return;
857 689 }
858 690
859 691 $this->save_taboola_version(TABOOLA_MIN_VER);
@@ -860,41 +692,54 @@
860 692
861 693 global $wpdb;
862 694 require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
863 695
864 - $charset_collate = '';
865 - if ( ! empty( $wpdb->charset ) ) {
866 - $charset_collate = "DEFAULT CHARACTER SET {$wpdb->charset}";
696 +
697 + //check mysql version
698 + if (function_exists('mysqli_get_server_info') && version_compare(mysqli_get_server_info(), '4.1.0', '>=')) {
699 + if (!empty($wpdb->charset))
700 + $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
701 + if (!empty($wpdb->collate))
702 + $charset_collate .= " COLLATE $wpdb->collate";
867 703 }
868 - if ( ! empty( $wpdb->collate ) ) {
869 - $charset_collate .= " COLLATE {$wpdb->collate}";
870 - }
871 704
705 + // Check if this is an upgrade from v1
872 706 $is_upgrade_from_v1 = $this->is_upgrade_from_v1();
707 +
708 + // tb_write_log("Is upgrade from v1: " . ($is_upgrade_from_v1 ? 'true' : 'false'));
709 +
710 +
711 + //settings table structure
873 712 $sql_table_settings = "
874 713 CREATE TABLE `" . $wpdb->prefix . "_taboola_settings` (
875 714 `id` INT NOT NULL AUTO_INCREMENT ,
876 715 `publisher_id` VARCHAR(255) DEFAULT NULL,
877 - `web_push_enabled` TINYINT(1) NOT NULL DEFAULT FALSE,
878 - `publisher_id_push` INT(15) DEFAULT NULL,
879 716 `first_bc_enabled` TINYINT(1) NOT NULL DEFAULT FALSE,
880 717 `first_bc_widget_id` VARCHAR(255) DEFAULT NULL,
881 718 `first_bc_placement` VARCHAR(255) DEFAULT " . ($is_upgrade_from_v1 ? "'below-article'" : "NULL") .",
719 + `first_bc_custom_css` TEXT DEFAULT NULL,
720 + `second_bc_enabled` TINYINT(1) NOT NULL DEFAULT FALSE,
721 + `second_bc_widget_id` VARCHAR(255) DEFAULT NULL,
722 + `second_bc_custom_css` TEXT DEFAULT NULL,
723 + `location_string` TEXT DEFAULT NULL,
724 + `mid_enabled` TINYINT(1) NOT NULL DEFAULT FALSE,
725 + `mid_widget_id` VARCHAR(255) DEFAULT NULL,
726 + `mid_placement` VARCHAR(255) DEFAULT NULL,
882 727 `out_of_content_enabled` TINYINT(1) NOT NULL DEFAULT TRUE,
883 - `mid_enabled` TINYINT(1) NOT NULL DEFAULT FALSE,
884 - `mid_widgets` TEXT DEFAULT NULL,
728 + `mid_location_string` TEXT DEFAULT NULL,
729 + `mid_location_string_occurrence` SMALLINT DEFAULT NULL,
730 + `mid_paragraph_ui_mode` VARCHAR(255) DEFAULT NULL,
885 731 `home_enabled` TINYINT(1) NOT NULL DEFAULT FALSE,
886 732 `home_widget_id` VARCHAR(255) DEFAULT NULL,
887 733 `home_placement` VARCHAR(255) DEFAULT NULL,
888 734 `home_location_string` TEXT DEFAULT NULL,
889 735 `home_location_string_occurrence` SMALLINT DEFAULT NULL,
890 - `category_enabled` TINYINT(1) NOT NULL DEFAULT FALSE,
891 - `category_widget_id` VARCHAR(255) DEFAULT NULL,
892 - `category_placement` VARCHAR(255) DEFAULT NULL,
893 - `category_location_string` TEXT DEFAULT NULL,
894 - `category_location_string_occurrence` SMALLINT DEFAULT NULL,
895 736 PRIMARY KEY (`id`)
896 - ) $charset_collate;";
737 + )" . $charset_collate . ";";
738 +
739 + // tb_write_log($sql_table_settings);
740 +
741 + // create/update the table
897 742 dbDelta($sql_table_settings);
898 743 }
899 744 }
900 745 }
@@ -901,13 +746,18 @@
901 746
902 747 global $taboolaWP;
903 748 $taboolaWP = new TaboolaWP();
904 749
750 +/*
751 +A few utility methods for logging
752 +*/
753 +
754 +// Writes debugging messages to 'logs/debug.log'.
755 +// The exact file location depends on *where* you trigger the function.
756 +// In this script, we trigger the function from the admin dashboard.
757 +// So the log file is located under 'wp-admin/logs'.
905 758 function tb_write_log($log_msg)
906 759 {
907 - if (!TABOOLA_DEBUG_MODE)
908 - return;
909 -
910 760 $log_filename = "logs";
911 761 if (!file_exists($log_filename))
912 762 {
913 763 mkdir($log_filename, 0777, true);
@@ -913,14 +763,18 @@
913 763 mkdir($log_filename, 0777, true);
914 764 }
915 765 $log_file_data = $log_filename.'/debug.log';
916 766
767 + // Add date/time
917 768 $date = date('Y-m-d H:i:s');
918 769 $log_msg_with_date = $date." : ".$log_msg;
919 -
920 - file_put_contents($log_file_data, $log_msg_with_date . "\n", FILE_APPEND);
770 +
771 + if (TABOOLA_DEBUG_MODE)
772 + file_put_contents($log_file_data, $log_msg_with_date . "\n", FILE_APPEND);
773 +
921 774 }
922 775
776 +// Write to console
923 777 function tb_console_log( $data ){
924 778
925 779 If(wp_get_environment_type() === 'development') {
926 780
@@ -930,8 +784,9 @@
930 784
931 785 }
932 786 }
933 787
788 +// Write to webpage and stop
934 789 function tb_print_to_page($data) {
935 790 If(wp_get_environment_type() === 'development') {
936 791 print_r($data);
937 792 die;
@@ -937,16 +792,5 @@
937 792 die;
938 793 }
939 794 }
940 795
941 -function enqueue_taboola_scripts() {
942 - wp_register_script(
943 - 'taboola-injector',
944 - plugins_url('js/js_inject.min.js', __FILE__),
945 - array(),
946 - null,
947 - true
948 - );
949 -
950 - wp_enqueue_script('taboola-injector');
951 -}
952 -add_action('wp_enqueue_scripts', 'enqueue_taboola_scripts');
796 +//