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 +248 -331 3.0.02.2.2 View file →
@@ -1,193 +1,122 @@
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.2.2
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.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 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>
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;
35 + private $tpl_sw = 'importScripts("https://cdn.taboola.com/webpush/tsw.js");';
36 +
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>
39 42 <pre><code>{{SW}}</code></pre>
40 - <p>Also make sure it's reachable at <code>{{DOMAIN}}/sw.js</code></p>
41 -SWE;
43 + <p>
44 + Also make sure that the file is accessible at {{DOMAIN}}/sw.js
45 + </p>
46 + SWE;
42 47
43 - public $plugin_name;
44 - public $plugin_directory;
45 - public $plugin_url;
46 - public $settings;
47 - public $tbl_taboola_settings;
48 + public $plugin_name;
49 + public $plugin_directory;
50 + public $plugin_url;
51 + public $settings;
52 + public $tbl_taboola_settings;
48 53
49 - /* ─────────────────────────── constructor ─────────────────────────── */
50 - public function __construct() {
51 - global $wpdb;
54 + public function __construct()
55 + {
56 + global $wpdb;
52 57
53 - define( 'TaboolaWP', true );
58 + //initialize plugin constant
59 + DEFINE('TaboolaWP', true);
54 60
55 - $this->_is_widget_on_page = false;
56 - $this->_is_head_script_loaded = false;
61 + $this->_is_widget_on_page = false;
62 + $this->_is_head_script_loaded = false;
57 63
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 - );
64 + $this->plugin_name = plugin_basename(__FILE__);
65 + $this->plugin_directory = plugin_dir_path(__FILE__);
66 + $this->plugin_url = trailingslashit(WP_PLUGIN_URL . '/' . dirname(plugin_basename(__FILE__)));
67 + $this->settings = $wpdb->get_row("select * from ".$wpdb->prefix."_taboola_settings limit 1");
67 68
68 - /* activation / upgrade */
69 - register_activation_hook( $this->plugin_name, [ $this, 'update_db' ] );
70 - add_action( 'admin_init', [ $this, 'update_db' ] );
69 + $this->tbl_taboola_settings = $wpdb->prefix . '_taboola_settings';
71 70
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 - }
71 + //activation function
72 + register_activation_hook($this->plugin_name, array(&$this, 'update_db'));
73 + add_action('admin_init', array(&$this, 'update_db'));
79 74
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' ] );
75 + // Enable sidebar widgets
76 + if ($this->settings != NULL && !empty($this->settings->publisher_id)){
77 + //register Taboola widget
78 + add_action('widgets_init',
79 + function(){
80 + return register_widget("WP_Widget_Taboola");
81 + }
82 + );
83 + }
89 84
90 - /* content widgets */
91 - add_filter( 'the_content', [ $this, 'load_taboola_content' ] );
92 - add_filter( 'the_content', [ $this, 'load_taboola_content_mid' ] );
85 + //$this->should_place_tag_outside_of_content = $this->settings->out_of_content_enabled;
93 86
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 );
87 + if (is_admin()) {
88 + //add menu for plugin
89 + add_action( 'admin_menu', array(&$this, 'admin_generate_menu') );
90 + add_filter('plugin_action_links', array(&$this, 'plugin_action_links'), 10, 2 );
91 + }elseif ($this->settings != NULL){
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'));
97 95
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' ] );
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 + }
111 + add_action('wp_footer', array(&$this, 'taboola_footer_loader_js'));
112 + add_filter('the_content', array(&$this, 'load_taboola_content'));
113 + add_filter('the_content', array(&$this, 'load_taboola_content_mid'));
114 + add_filter('the_content', array(&$this, 'load_taboola_content_home'));
115 + }
122 116 }
123 -}
124 -public function tb_home_buffer_inject( $html ) {
125 - return $this->load_taboola_content_home( $html );
126 -}
127 117
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) {
118 + function plugin_action_links($links, $file) {
190 119 static $this_plugin;
191 120
192 121 if (!$this_plugin) {
193 122 $this_plugin = plugin_basename(__FILE__);
@@ -209,17 +138,14 @@
209 138 private function should_show_content_widget_mid(){
210 139 // PC - only if v2 was installed:
211 140 if (!$this->is_db_updated_for_min_ver("2.0.0"))
212 141 return false;
213 - $retVal1 = ((trim($this->settings->publisher_id) != '') && is_single() && !empty($this->settings->mid_enabled));
142 + $retVal1 = ((trim($this->settings->publisher_id) != '') && is_single() && $this->settings->mid_enabled && trim($this->settings->mid_widget_id) != '');
214 143 return $retVal1;
215 144 }
216 145
217 146 private function should_show_content_widget_home(){
218 147 // PC - only if v2 was installed:
219 -
220 - //error_log( print_r( $this->settings, true ) ); // TEMP line
221 -
222 148 if (!$this->is_db_updated_for_min_ver("2.0.0"))
223 149 return false;
224 150 $retVal2 = ((trim($this->settings->publisher_id) != '') && (is_front_page() || is_home()) && $this->settings->home_enabled && trim($this->settings->home_widget_id) != '');
225 151 return $retVal2;
@@ -231,9 +157,9 @@
231 157 }
232 158
233 159 // Determine if a taboola widget should be added somewhere on the current page (content or sidebar)
234 160 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() ;
161 + return $this->should_show_content_widget() || $this->should_show_content_widget_mid() || $this->should_show_content_widget_home() || $this->should_show_sidebar_widget();
236 162 }
237 163
238 164 function get_page_type(){
239 165 $page_type='article';
@@ -318,37 +244,24 @@
318 244 return $content;
319 245 }
320 246
321 247 // Mid-article-widget
322 - function load_taboola_content_mid($content) {
248 + function load_taboola_content_mid($content)
249 + {
250 + $taboola_content_mid = array();
323 251 if ($this->should_show_content_widget_mid()){
324 - $mid_widgets = json_decode($this->settings->mid_widgets ?? '[]');
325 252
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 - );
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;
335 260
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 - );
261 + $content = $this->embed_taboola_content_location_mid($content,$taboola_content_mid,trim($this->settings->mid_location_string));
262 + }
341 263
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 264 return $content;
352 265 }
353 266
354 267 // Homepage widget
@@ -353,9 +266,9 @@
353 266
354 267 // Homepage widget
355 268 function load_taboola_content_home($content)
356 269 {
357 - //error_log( 'TB-DEBUG ② entered load_taboola_content_home' );
270 +
358 271 $taboola_content_home = array();
359 272 if ($this->should_show_content_widget_home()){
360 273
361 274 $homeWidgetParams = array('{{WIDGET_ID}}' => $this->settings->home_widget_id,
@@ -370,33 +283,9 @@
370 283 }
371 284 return $content;
372 285
373 286 }
374 - /* ------------------------------------------------------------------
375 - * CATEGORY ▸ build widget & inject
376 - * ------------------------------------------------------------------*/
377 -public function load_taboola_content_category( $content ) {
378 287
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 288 // Below-article widget
400 289
401 290 // Extract the taboola content in the required format:
402 291 // String - for injecting on the servr side
@@ -463,18 +352,8 @@
463 352 return $ret_val;
464 353
465 354 }
466 355
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 356 // Below-article widget
478 357 // Do the actual logic of choosing where to place the taboola content.
479 358 function embed_taboola_content_location($content, $taboola_content){
480 359 $do_default = true;
@@ -500,9 +379,9 @@
500 379 }
501 380
502 381 // Mid-article widget
503 382 // 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){
383 + function embed_taboola_content_location_mid($content, $taboola_content_mid, $location){
505 384 $do_default = true;
506 385
507 386 if (isset($location) && $location != ''){
508 387 $first_char = substr($location,0,1);
@@ -529,9 +408,9 @@
529 408 } else{
530 409 require_once('simple_html_dom.php');
531 410
532 411 $html_doc = str_get_html($content);
533 - $target_location = $html_doc->find($location, ($occurrence) - 1);
412 + $target_location = $html_doc->find($location,($this->settings->mid_location_string_occurrence)-1);
534 413
535 414 // if the location was found within the html content
536 415 if (isset($target_location) && is_object($target_location)){
537 416
@@ -541,8 +420,12 @@
541 420 $do_default = false;
542 421 }
543 422 }
544 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 + // }
545 428
546 429 return $content;
547 430 }
548 431
@@ -549,22 +432,8 @@
549 432 // Homepage widget
550 433 // Do the actual logic of choosing where to place the taboola content based on the "location" attribute
551 434 function embed_taboola_content_location_home($content, $taboola_content_home, $location){
552 435
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 436 $do_default = true;
568 437
569 438 if (isset($location) && $location != ''){
570 439 $first_char = substr($location,0,1);
@@ -604,13 +473,12 @@
604 473 }
605 474 }
606 475 }
607 476 // 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 - }
477 + // if ($do_default){
478 + // $content = $content.$this->format_taboola_content_home($taboola_content_home,TABOOLA_CONTENT_FORMAT_STRING);
479 + // }
611 480 return $content;
612 -
613 481
614 482 }
615 483
616 484 function admin_generate_menu(){
@@ -643,18 +511,33 @@
643 511 }
644 512 }
645 513
646 514 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 - }
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)";
655 537 }
656 538 }
539 +
657 540 }
658 541
659 542 if(isset($_POST['home_enabled'])) {
660 543 if (trim(strip_tags($_POST['home_widget_id'])) == '') {
@@ -667,45 +550,25 @@
667 550 if (trim(strip_tags($_POST['home_location_string'])) == '') {
668 551 $taboola_errors[] = "Homepage > CSS selector";
669 552 }
670 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':
671 564 if (!empty($_POST['home_location_string']) && !$this->is_home_location_string_occurrence_valid($_POST['home_location_string_occurrence'])) {
672 565 $taboola_errors[] = "Homepage > Occurance (must be >= 1)";
673 566 }
674 567 }
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 - }
568 + }
682 569
683 570 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 -
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 571 $data = array(
709 572 "publisher_id" => trim($_POST['publisher_id']),
710 573
711 574 "web_push_enabled" => isset($_POST['web_push_enabled']) ? true : false,
@@ -717,23 +580,24 @@
717 580
718 581 "out_of_content_enabled" => isset($_POST['out_of_content_enabled']) ? true : false,
719 582
720 583 "mid_enabled" => isset($_POST['mid_enabled']) ? true : false,
721 - "mid_widgets" => $mid_widgets_json,
722 -
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 +
723 591 "home_enabled" => isset($_POST['home_enabled']) ? true : false,
724 592 "home_widget_id" => !empty($_POST['home_widget_id']) ? trim($_POST['home_widget_id']) : '',
725 593 "home_placement" => !empty($_POST['home_placement']) ? trim($_POST['home_placement']) : '',
726 594
727 595 "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']) : '',
596 + "home_location_string" => !empty($_POST['home_location_string']) ? trim($_POST['home_location_string']) : ''
597 + );
734 598
735 - );
599 + //var_dump($settings);
736 600
737 601 $is_valid_nonce = false;
738 602
739 603 if (isset( $_POST['my_plugin_nonce']) && wp_verify_nonce( $_POST['my_plugin_nonce'], 'my_plugin_update_field_action' ) ) {
@@ -741,9 +605,9 @@
741 605 }
742 606
743 607 if ($is_valid_nonce) {
744 608 if($settings == NULL){
745 - $wpdb->insert($this->tbl_taboola_settings, $data);
609 + $wpdb->insert($this->tbl_taboola_settings, $data, null, null);
746 610 } else {
747 611 $wpdb->update($this->tbl_taboola_settings, $data, array('id' => $settings->id));
748 612 }
749 613 }
@@ -808,9 +672,21 @@
808 672 }
809 673 }
810 674
811 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 +
812 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 +
813 689 $table_name = $wpdb->prefix . "_taboola_settings";
814 690
815 691 if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
816 692 tb_write_log("Table NOT FOUND");
@@ -818,31 +694,55 @@
818 694 }
819 695 else {
820 696 tb_write_log("Table EXISTS");
821 697 }
698 + // END patch =================================================
699 +
700 + // Check the saved version in wp_options. Default to '1.0.0' if the option is not found.
822 701 $saved_version = get_option(TABOOLA_OPTION_NAME, '1.0.0');
823 702
824 703 $db_is_up_to_date = version_compare($saved_version, $min_ver, '>=');
704 + //tb_write_log($db_is_up_to_date);
825 705 return $db_is_up_to_date;
826 706
827 707 }
828 708
829 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 + */
830 714 global $wpdb;
715 +
716 + // Check the saved version in wp_options. Default to '1.0.0' if the option is not found.
831 717 $saved_version = get_option(TABOOLA_OPTION_NAME, '1.0.0');
718 +
719 + // Get the currently loaded plugin version.
832 720 $plugin_version = $this->get_loaded_plugin_version();
721 +
833 722 $db_is_up_to_date = version_compare($saved_version, $plugin_version, '>=');
723 + //tb_write_log($db_is_up_to_date);
834 724 return $db_is_up_to_date;
725 +
835 726 }
836 727
837 728 function get_loaded_plugin_version() {
838 - $plugin_data = get_plugin_data( __FILE__ );
729 +
730 + $plugin_data = get_plugin_data( __FILE__ ); // Can be invoked from Admin page only
839 731 $loaded_plugin_version = $plugin_data['Version'];
840 732 return $loaded_plugin_version;
733 +
841 734 }
842 735
843 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 +
844 743 tb_write_log("SAVING NEW MIN VERSION: " . $min_ver); // PC
744 +
845 745 $saved_version = get_option(TABOOLA_OPTION_NAME, '');
846 746 if ($saved_version == '') {
847 747 add_option(TABOOLA_OPTION_NAME, $min_ver);
848 748 }
@@ -851,9 +751,12 @@
851 751 }
852 752 }
853 753
854 754 function update_db(){
755 +
756 + // If we are up to date, then skip this method...
855 757 if ($this->is_db_updated_for_min_ver(TABOOLA_MIN_VER)) {
758 + //tb_write_log("All up to date!");
856 759 return;
857 760 }
858 761
859 762 $this->save_taboola_version(TABOOLA_MIN_VER);
@@ -860,17 +763,24 @@
860 763
861 764 global $wpdb;
862 765 require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
863 766
864 - $charset_collate = '';
865 - if ( ! empty( $wpdb->charset ) ) {
866 - $charset_collate = "DEFAULT CHARACTER SET {$wpdb->charset}";
767 +
768 + // Handling for older MySQL versions
769 + if (function_exists('mysql_get_server_info') && version_compare(mysql_get_server_info(), '4.1.0', '>=')) {
770 + if (!empty($wpdb->charset))
771 + $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
772 + if (!empty($wpdb->collate))
773 + $charset_collate .= " COLLATE $wpdb->collate";
867 774 }
868 - if ( ! empty( $wpdb->collate ) ) {
869 - $charset_collate .= " COLLATE {$wpdb->collate}";
870 - }
871 775
776 + // Check if this is an upgrade from v1
872 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 +
782 + //settings table structure
873 783 $sql_table_settings = "
874 784 CREATE TABLE `" . $wpdb->prefix . "_taboola_settings` (
875 785 `id` INT NOT NULL AUTO_INCREMENT ,
876 786 `publisher_id` VARCHAR(255) DEFAULT NULL,
@@ -878,23 +788,31 @@
878 788 `publisher_id_push` INT(15) DEFAULT NULL,
879 789 `first_bc_enabled` TINYINT(1) NOT NULL DEFAULT FALSE,
880 790 `first_bc_widget_id` VARCHAR(255) DEFAULT NULL,
881 791 `first_bc_placement` VARCHAR(255) DEFAULT " . ($is_upgrade_from_v1 ? "'below-article'" : "NULL") .",
792 + `first_bc_custom_css` TEXT DEFAULT NULL,
793 + `second_bc_enabled` TINYINT(1) NOT NULL DEFAULT FALSE,
794 + `second_bc_widget_id` VARCHAR(255) DEFAULT NULL,
795 + `second_bc_custom_css` TEXT DEFAULT NULL,
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,
882 800 `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,
801 + `mid_location_string` TEXT DEFAULT NULL,
802 + `mid_location_string_occurrence` SMALLINT DEFAULT NULL,
803 + `mid_paragraph_ui_mode` VARCHAR(255) DEFAULT NULL,
885 804 `home_enabled` TINYINT(1) NOT NULL DEFAULT FALSE,
886 805 `home_widget_id` VARCHAR(255) DEFAULT NULL,
887 806 `home_placement` VARCHAR(255) DEFAULT NULL,
888 807 `home_location_string` TEXT DEFAULT NULL,
889 808 `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 809 PRIMARY KEY (`id`)
896 - ) $charset_collate;";
810 + )" . $charset_collate . ";";
811 +
812 + // tb_write_log($sql_table_settings);
813 +
814 + // create/update the table
897 815 dbDelta($sql_table_settings);
898 816 }
899 817 }
900 818 }
@@ -901,13 +819,18 @@
901 819
902 820 global $taboolaWP;
903 821 $taboolaWP = new TaboolaWP();
904 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'.
905 831 function tb_write_log($log_msg)
906 832 {
907 - if (!TABOOLA_DEBUG_MODE)
908 - return;
909 -
910 833 $log_filename = "logs";
911 834 if (!file_exists($log_filename))
912 835 {
913 836 mkdir($log_filename, 0777, true);
@@ -913,14 +836,18 @@
913 836 mkdir($log_filename, 0777, true);
914 837 }
915 838 $log_file_data = $log_filename.'/debug.log';
916 839
840 + // Add date/time
917 841 $date = date('Y-m-d H:i:s');
918 842 $log_msg_with_date = $date." : ".$log_msg;
919 -
920 - file_put_contents($log_file_data, $log_msg_with_date . "\n", FILE_APPEND);
843 +
844 + if (TABOOLA_DEBUG_MODE)
845 + file_put_contents($log_file_data, $log_msg_with_date . "\n", FILE_APPEND);
846 +
921 847 }
922 848
849 +// Write to console
923 850 function tb_console_log( $data ){
924 851
925 852 If(wp_get_environment_type() === 'development') {
926 853
@@ -930,8 +857,9 @@
930 857
931 858 }
932 859 }
933 860
861 +// Write to webpage and stop
934 862 function tb_print_to_page($data) {
935 863 If(wp_get_environment_type() === 'development') {
936 864 print_r($data);
937 865 die;
@@ -937,16 +865,5 @@
937 865 die;
938 866 }
939 867 }
940 868
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');
869 +//