PluginProbe
Taboola / 2.2.3
Taboola v2.2.3
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 +278 -317 3.0.12.2.3 View file →
@@ -1,206 +1,155 @@
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.1
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.3
7 + * Author: Taboola
8 8 */
9 9
10 -define( 'TABOOLA_PLUGIN_VERSION', '3.0.1' ); // 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.3"); // => 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;
52 - //initialize plugin constant
53 - define( 'TaboolaWP', true );
54 + public function __construct()
55 + {
56 + global $wpdb;
54 57
55 - $this->_is_widget_on_page = false;
56 - $this->_is_head_script_loaded = false;
58 + //initialize plugin constant
59 + DEFINE('TaboolaWP', true);
57 60
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 - );
61 + $this->_is_widget_on_page = false;
62 + $this->_is_head_script_loaded = false;
67 63
68 - /* activation / upgrade */
69 - register_activation_hook( $this->plugin_name, [ $this, 'update_db' ] );
70 - add_action( 'admin_init', [ $this, 'update_db' ] );
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");
71 68
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 - }
69 + $this->tbl_taboola_settings = $wpdb->prefix . '_taboola_settings';
79 70
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 - }
86 -
87 -
88 -
89 -
90 - elseif ( $this->settings ) {
91 - /* loader & flush */
92 - add_action( 'wp_head', [ $this, 'taboola_header_loader_inject' ] );
93 - if ( ! empty( $this->settings->publisher_id_push ) ) {
94 - add_action( 'wp_head', [ $this, 'taboola_webpush_loader_js' ] );
71 + //activation function
72 + register_activation_hook($this->plugin_name, array(&$this, 'update_db'));
73 + add_action('admin_init', array(&$this, 'update_db'));
95 74
96 - $sw = 'sw.js';
97 - $sw_path = ABSPATH . $sw;
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 + }
98 84
99 - $content = file_exists( $sw_path ) ? file_get_contents( $sw_path ) : '';
85 + //$this->should_place_tag_outside_of_content = $this->settings->out_of_content_enabled;
100 86
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 );
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'));
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 104 }
105 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 );
106 + if (file_put_contents($sw_path, $content) === false) {
107 + return $this->notice($this->msg_sw_error);
108 108 }
109 109 }
110 110 }
111 - add_action( 'wp_footer', [ $this, 'taboola_footer_loader_js' ] );
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 + // add_filter('the_excerpt', array(&$this, 'load_taboola_content_home'));
116 + // Homepage widget – capture the full page, inject once.
117 + // Homepage widget – start buffering *before* anything else renders
118 + add_action( 'template_redirect', [ $this, 'tb_home_buffer_start' ], 0 );
112 119
113 - /* content widgets */
114 - add_filter( 'the_content', [ $this, 'load_taboola_content' ] );
115 - add_filter( 'the_content', [ $this, 'load_taboola_content_mid' ] );
120 + // Homepage widget – flush the buffer *after* literally everything else
121 + add_action( 'shutdown', [ $this, 'tb_home_buffer_flush' ], PHP_INT_MAX );
116 122
117 - /* full-page buffer – home & category */
118 - add_action( 'template_redirect', [ $this, 'tb_home_buffer_start' ], 0 );
119 - add_action( 'shutdown', [ $this, 'tb_home_buffer_flush' ], PHP_INT_MAX );
120 -
121 - add_action( 'template_redirect', [ $this, 'tb_cat_buffer_start' ], 0 );
122 - add_action( 'shutdown', [ $this, 'tb_cat_buffer_flush' ], PHP_INT_MAX );
123 - }
124 - } // __construct
125 -
126 -
127 -/* ======================================================================
128 - CATEGORY ▸ should we show the widget?
129 - ====================================================================== */
130 -private function should_show_content_widget_category(): bool {
131 - return (
132 - trim( $this->settings->publisher_id ) !== '' &&
133 - ( is_category() || is_archive() || is_search() ) &&
134 - $this->settings->category_enabled &&
135 - trim( $this->settings->category_widget_id ) !== ''
136 - );
137 -}
138 -
139 -/* ----------------------------------------------------------------------
140 - HOMEPAGE ▸ full-page buffer
141 - -------------------------------------------------------------------- */
123 +
124 + }
125 +
126 + }
127 +
128 + /* ------------------------------------------------------------------
129 + * HOMEPAGE WIDGET – full-page buffer
130 + * ----------------------------------------------------------------*/
142 131 public function tb_home_buffer_start() {
132 +
133 + // Use the plug-in’s own logic to decide if the home widget is enabled
143 134 if ( $this->should_show_content_widget_home() ) {
144 135 ob_start( [ $this, 'tb_home_buffer_inject' ] );
145 136 }
146 137 }
138 +
147 139 public function tb_home_buffer_inject( $html ) {
140 + //error_log( '🟢 Taboola buffer inject ran. HTML length = ' . strlen( $html ) ); // LOG #1
141 + // error_log( 'TB-DEBUG ① buffer inject, html len='.strlen($html) );
142 +
143 + /*
144 + * load_taboola_content_home() already:
145 + * – reads your saved widget-ID, placement, selector & occurrence
146 + * – builds the HTML + JS
147 + * – calls embed_taboola_content_location_home() to place it
148 + */
148 149 return $this->load_taboola_content_home( $html );
149 150 }
150 151
151 -/* ----------------------------------------------------------------------
152 - CATEGORY ▸ buffer hooks
153 - -------------------------------------------------------------------- */
154 -public function tb_cat_buffer_start() {
155 - if ( $this->should_show_content_widget_category() ) {
156 - ob_start( [ $this, 'tb_cat_buffer_inject' ] );
157 - }
158 -}
159 -public function tb_cat_buffer_inject( $html ) {
160 - return $this->load_taboola_content_category( $html );
161 -}
162 -public function tb_cat_buffer_flush() {
163 - if ( ob_get_length() ) {
164 - echo ob_get_clean();
165 - }
166 -}
167 -
168 -/* ----------------------------------------------------------------------
169 - CATEGORY ▸ choose where to inject
170 - -------------------------------------------------------------------- */
171 -private function embed_taboola_content_location_category(
172 - string $html,
173 - array $taboola_content,
174 - string $selector ) : string {
175 -
176 - $formatted = $this->format_taboola_content_category( $taboola_content );
177 -
178 - /* A. no selector → bottom of page */
179 - if ( $selector === '' ) {
180 - return str_ireplace( '</body>', $formatted . '</body>', $html );
181 - }
182 -
183 - /* B. selector given → try server-side injection */
184 - $doc = str_get_html( $html );
185 - if ( ! $doc ) {
186 - // parser failed → graceful fallback
187 - return str_ireplace( '</body>', $formatted . '</body>', $html );
188 - }
189 -
190 - $occurrence = max( 0, $this->settings->category_location_string_occurrence - 1 );
191 - $target = $doc->find( $selector, $occurrence );
192 -
193 - if ( $target ) { // selector found
194 - $target->outertext .= $formatted;
195 - return (string) $doc; // done – no duplicate
196 - }
197 -
198 - /* C. selector not found → bottom of page */
199 - return str_ireplace( '</body>', $formatted . '</body>', $html );
200 -}
201 -
202 -/* ------------------------------------------------------------------ */
203 152 public function tb_home_buffer_flush() {
204 153 if ( ob_get_length() ) {
205 154 echo ob_get_clean();
206 155 }
@@ -205,12 +154,9 @@
205 154 echo ob_get_clean();
206 155 }
207 156 }
208 157
209 -
210 -
211 -
212 -function plugin_action_links($links, $file) {
158 + function plugin_action_links($links, $file) {
213 159 static $this_plugin;
214 160
215 161 if (!$this_plugin) {
216 162 $this_plugin = plugin_basename(__FILE__);
@@ -232,9 +178,9 @@
232 178 private function should_show_content_widget_mid(){
233 179 // PC - only if v2 was installed:
234 180 if (!$this->is_db_updated_for_min_ver("2.0.0"))
235 181 return false;
236 - $retVal1 = ((trim($this->settings->publisher_id) != '') && is_single() && !empty($this->settings->mid_enabled));
182 + $retVal1 = ((trim($this->settings->publisher_id) != '') && is_single() && $this->settings->mid_enabled && trim($this->settings->mid_widget_id) != '');
237 183 return $retVal1;
238 184 }
239 185
240 186 private function should_show_content_widget_home(){
@@ -254,9 +200,9 @@
254 200 }
255 201
256 202 // Determine if a taboola widget should be added somewhere on the current page (content or sidebar)
257 203 function is_widget_on_page(){
258 - 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() ;
204 + return $this->should_show_content_widget() || $this->should_show_content_widget_mid() || $this->should_show_content_widget_home() || $this->should_show_sidebar_widget();
259 205 }
260 206
261 207 function get_page_type(){
262 208 $page_type='article';
@@ -268,21 +214,15 @@
268 214 return $page_type;
269 215 }
270 216
271 217 // return the head loader script
272 - function taboola_header_loader_js() {
218 + function taboola_header_loader_js() {
219 + $head_string = "";
220 +
221 + // Only adding the loader if a widget is going to be placed on the page.
273 222 if ($this->is_widget_on_page()){
274 -
275 - // New logic to get all mid-article locations
276 - $mid_locations_string = '';
277 - if (!empty($this->settings->mid_widgets)) {
278 - $mid_widgets_array = json_decode($this->settings->mid_widgets, true);
279 - if (is_array($mid_widgets_array)) {
280 - $locations = array_column($mid_widgets_array, 'location_string');
281 - $mid_locations_string = implode(', ', $locations);
282 - }
283 - }
284 -
223 + // PC - since these params will be inserted in 'loaderInjectionScript.js' via search and replace,
224 + // double brackets are used to ensure that each key is a unique string.
285 225 $stringParams = array(
286 226 '{{PUBLISHER_ID}}' => $this->settings->publisher_id,
287 227 '{{PAGE_TYPE}}' => $this->get_page_type(),
288 228 '{{WORDPRESS_VERSION}}' => get_bloginfo('version'),
@@ -287,18 +227,19 @@
287 227 '{{PAGE_TYPE}}' => $this->get_page_type(),
288 228 '{{WORDPRESS_VERSION}}' => get_bloginfo('version'),
289 229 '{{PHP_VERSION}}' => phpversion(),
290 230 '{{PLUGIN_VERSION}}' => TABOOLA_PLUGIN_VERSION,
291 - '{{LOC_MID}}' => $mid_locations_string,
292 - '{{LOC_HOME}}' => $this->settings->home_location_string ?? ''
231 + '{{LOC_MID}}' => $this->settings->mid_location_string,
232 + '{{LOC_HOME}}' => $this->settings->home_location_string
293 233 );
294 234
295 235 $scriptWrapper = new JavaScriptWrapper("loaderInjectionScript.js",$stringParams);
296 - return $scriptWrapper->getScriptMarkupString();
236 + $head_string = $scriptWrapper->getScriptMarkupString();
297 237 }
298 - return "";
238 + return $head_string;
299 239 }
300 240
241 +
301 242 // This function is used for the hook action, injects the header content to the <head> tag.
302 243 function taboola_header_loader_inject(){
303 244 echo $this->taboola_header_loader_js();
304 245 }
@@ -346,37 +287,24 @@
346 287 return $content;
347 288 }
348 289
349 290 // Mid-article-widget
350 - function load_taboola_content_mid($content) {
291 + function load_taboola_content_mid($content)
292 + {
293 + $taboola_content_mid = array();
351 294 if ($this->should_show_content_widget_mid()){
352 - $mid_widgets = json_decode($this->settings->mid_widgets ?? '[]');
353 295
354 - if (is_array($mid_widgets) && !empty($mid_widgets)) {
355 - foreach ($mid_widgets as $index => $widget) {
356 - $container_id = 'taboola-mid-article-thumbnails-' . $index;
357 -
358 - $widgetParams = array(
359 - '{{WIDGET_ID}}' => $widget->widget_id,
360 - '{{CONTAINER}}' => $container_id,
361 - '{{PLACEMENT}}' => $widget->placement
362 - );
296 + $secondWidgetParams = array('{{WIDGET_ID}}' => $this->settings->mid_widget_id,
297 + '{{CONTAINER}}' => 'taboola-mid-article-thumbnails',
298 + '{{PLACEMENT}}' => $this->settings->mid_placement);
299 +
300 + $secondWidgetScript = new JavaScriptWrapper("widgetInjectionScript.js",$secondWidgetParams);
301 + $taboola_content_mid[TABOOLA_CONTENT_FORMAT_HTML][] = "<div id='taboola-mid-article-thumbnails'></div>";
302 + $taboola_content_mid[TABOOLA_CONTENT_FORMAT_SCRIPT][] = $secondWidgetScript;
363 303
364 - $widgetScript = new JavaScriptWrapper("widgetInjectionScript.js", $widgetParams);
365 - $taboola_content_mid = array(
366 - TABOOLA_CONTENT_FORMAT_HTML => array("<div id='{$container_id}'></div>"),
367 - TABOOLA_CONTENT_FORMAT_SCRIPT => array($widgetScript),
368 - );
304 + $content = $this->embed_taboola_content_location_mid($content,$taboola_content_mid,trim($this->settings->mid_location_string));
305 + }
369 306
370 - $content = $this->embed_taboola_content_location_mid(
371 - $content,
372 - $taboola_content_mid,
373 - $widget->location_string,
374 - $widget->occurrence
375 - );
376 - }
377 - }
378 - }
379 307 return $content;
380 308 }
381 309
382 310 // Homepage widget
@@ -398,33 +326,9 @@
398 326 }
399 327 return $content;
400 328
401 329 }
402 - /* ------------------------------------------------------------------
403 - * CATEGORY ▸ build widget & inject
404 - * ------------------------------------------------------------------*/
405 -public function load_taboola_content_category( $content ) {
406 330
407 - // 1) Build the DIV that Taboola will fill
408 - $taboola[TABOOLA_CONTENT_FORMAT_HTML][] =
409 - "<div id='taboola-category-thumbnails'></div>";
410 -
411 - // 2) Build the JS that loads the feed (reuse the wrapper)
412 - $taboola[TABOOLA_CONTENT_FORMAT_SCRIPT][] =
413 - new JavaScriptWrapper( 'widgetInjectionScript.js', [
414 - '{{WIDGET_ID}}' => $this->settings->category_widget_id,
415 - '{{CONTAINER}}' => 'taboola-category-thumbnails',
416 - '{{PLACEMENT}}' => $this->settings->category_placement,
417 - ]);
418 -
419 - // 3) Decide where to insert (selector or end of <body>)
420 - return $this->embed_taboola_content_location_category(
421 - $content,
422 - $taboola,
423 - trim( $this->settings->category_location_string )
424 - );
425 -}
426 -
427 331 // Below-article widget
428 332
429 333 // Extract the taboola content in the required format:
430 334 // String - for injecting on the servr side
@@ -491,18 +395,8 @@
491 395 return $ret_val;
492 396
493 397 }
494 398
495 -
496 - // ► CATEGORY formatter ◄
497 -private function format_taboola_content_category( $arr ) {
498 - return implode( "\n", [
499 - implode( "\n", $arr[TABOOLA_CONTENT_FORMAT_HTML] ?? [] ),
500 - '<script type="text/javascript">' .
501 - implode( "\n", $arr[TABOOLA_CONTENT_FORMAT_SCRIPT] ?? [] ) .
502 - '</script>',
503 - ]);
504 -}
505 399 // Below-article widget
506 400 // Do the actual logic of choosing where to place the taboola content.
507 401 function embed_taboola_content_location($content, $taboola_content){
508 402 $do_default = true;
@@ -528,9 +422,9 @@
528 422 }
529 423
530 424 // Mid-article widget
531 425 // Do the actual logic of choosing where to place the taboola content based on the "location" attribute
532 - function embed_taboola_content_location_mid($content, $taboola_content_mid, $location, $occurrence = 1){
426 + function embed_taboola_content_location_mid($content, $taboola_content_mid, $location){
533 427 $do_default = true;
534 428
535 429 if (isset($location) && $location != ''){
536 430 $first_char = substr($location,0,1);
@@ -557,9 +451,9 @@
557 451 } else{
558 452 require_once('simple_html_dom.php');
559 453
560 454 $html_doc = str_get_html($content);
561 - $target_location = $html_doc->find($location, ($occurrence) - 1);
455 + $target_location = $html_doc->find($location,($this->settings->mid_location_string_occurrence)-1);
562 456
563 457 // if the location was found within the html content
564 458 if (isset($target_location) && is_object($target_location)){
565 459
@@ -569,8 +463,12 @@
569 463 $do_default = false;
570 464 }
571 465 }
572 466 }
467 + // Default for below-article widget - add to the end of the content
468 + // if ($do_default){
469 + // $content = $content.$this->format_taboola_content_mid($taboola_content_mid,TABOOLA_CONTENT_FORMAT_STRING);
470 + // }
573 471
574 472 return $content;
575 473 }
576 474
@@ -671,18 +569,33 @@
671 569 }
672 570 }
673 571
674 572 if(isset($_POST['mid_enabled'])) {
675 - if(isset($_POST['mid_widget_id']) && is_array($_POST['mid_widget_id'])) {
676 - foreach ($_POST['mid_widget_id'] as $index => $widget_id) {
677 - if(trim($widget_id) == '') {
678 - $taboola_errors[] = "Mid-article Widget #".($index + 1)." > Widget ID";
679 - }
680 - if(trim($_POST['mid_placement'][$index]) == '') {
681 - $taboola_errors[] = "Mid-article Widget #".($index + 1)." > Placement Name";
682 - }
573 + if (trim(strip_tags($_POST['mid_widget_id'])) == '') {
574 + $taboola_errors[] = "Mid-article > Widget ID";
575 + }
576 + if (trim(strip_tags($_POST['mid_placement'])) == '') {
577 + $taboola_errors[] = "Mid-article > Placement Name";
578 + }
579 + if (trim(strip_tags($_POST['mid_location_string'])) == '') {
580 + $taboola_errors[] = "Mid-article > CSS selector";
581 + }
582 + else {
583 + // If the 'location' WAS filled in, then...
584 +
585 + // 1) Check for a valid 'location':
586 +
587 + // Validation method has not been implemented
588 + // mid_if (!$this->is_locaton_string_valid1($_POST['mid_location_string'])) {
589 + // $taboola_errors[] = "Mid-article > CSS Selector (invalid value)";
590 + // }
591 +
592 + // 2) Validate the 'occurrence':
593 + if (!$this->is_mid_location_string_occurrence_valid($_POST['mid_location_string_occurrence'])) {
594 + $taboola_errors[] = "Mid-article > Occurance (must be >= 1)";
683 595 }
684 596 }
597 +
685 598 }
686 599
687 600 if(isset($_POST['home_enabled'])) {
688 601 if (trim(strip_tags($_POST['home_widget_id'])) == '') {
@@ -695,45 +608,25 @@
695 608 if (trim(strip_tags($_POST['home_location_string'])) == '') {
696 609 $taboola_errors[] = "Homepage > CSS selector";
697 610 }
698 611 else {
612 + // If the 'location' WAS filled in, then...
613 +
614 + // 1) Check for a valid 'location':
615 +
616 + // Validation method has not been implemented
617 + // if (!$this->is_home_location_string_valid($_POST['home_location_string'])) {
618 + // $taboola_errors[] = "Homepage > CSS Selector (invalid value)";
619 + // }
620 +
621 + // 2) Validate the 'occurrence':
699 622 if (!empty($_POST['home_location_string']) && !$this->is_home_location_string_occurrence_valid($_POST['home_location_string_occurrence'])) {
700 623 $taboola_errors[] = "Homepage > Occurance (must be >= 1)";
701 624 }
702 625 }
703 - }
704 - if ( isset($_POST['category_enabled']) ) {
705 - if ( trim(strip_tags($_POST['category_widget_id'])) == '' )
706 - $taboola_errors[] = "Category > Widget ID";
707 - if ( trim(strip_tags($_POST['category_placement'])) == '' )
708 - $taboola_errors[] = "Category > Placement Name";
709 - }
626 + }
710 627
711 628 if(count($taboola_errors) == 0){
712 -
713 - $mid_widgets_data = array();
714 - if (isset($_POST['mid_widget_id']) && is_array($_POST['mid_widget_id'])) {
715 - foreach ($_POST['mid_widget_id'] as $index => $widget_id) {
716 - if (!empty(trim($widget_id))) {
717 -
718 - $location_string = 'p';
719 - if (isset($_POST['mid_paragraph_ui_mode'][$index]) && $_POST['mid_paragraph_ui_mode'][$index] === 'Other') {
720 - if (isset($_POST['mid_location_string'][$index])) {
721 - $location_string = sanitize_text_field($_POST['mid_location_string'][$index]);
722 - }
723 - }
724 -
725 - $mid_widgets_data[] = array(
726 - 'widget_id' => sanitize_text_field($widget_id),
727 - 'placement' => sanitize_text_field($_POST['mid_placement'][$index]),
728 - 'location_string' => $location_string,
729 - 'occurrence' => intval($_POST['mid_location_string_occurrence'][$index]),
730 - );
731 - }
732 - }
733 - }
734 - $mid_widgets_json = json_encode($mid_widgets_data);
735 -
736 629 $data = array(
737 630 "publisher_id" => trim($_POST['publisher_id']),
738 631
739 632 "web_push_enabled" => isset($_POST['web_push_enabled']) ? true : false,
@@ -745,23 +638,24 @@
745 638
746 639 "out_of_content_enabled" => isset($_POST['out_of_content_enabled']) ? true : false,
747 640
748 641 "mid_enabled" => isset($_POST['mid_enabled']) ? true : false,
749 - "mid_widgets" => $mid_widgets_json,
750 -
642 + "mid_widget_id" => !empty($_POST['mid_widget_id']) ? trim($_POST['mid_widget_id']) : '',
643 + "mid_placement" => !empty($_POST['mid_placement']) ? trim($_POST['mid_placement']) : '',
644 + "mid_paragraph_ui_mode" => !empty($_POST['mid_paragraph_ui_mode']) ? trim($_POST['mid_paragraph_ui_mode']) : '',
645 +
646 + "mid_location_string_occurrence" => !empty($_POST['mid_location_string_occurrence']) ? $_POST['mid_location_string_occurrence'] : '',
647 + "mid_location_string" => !empty($_POST['mid_location_string']) ? trim($_POST['mid_location_string']) : '',
648 +
751 649 "home_enabled" => isset($_POST['home_enabled']) ? true : false,
752 650 "home_widget_id" => !empty($_POST['home_widget_id']) ? trim($_POST['home_widget_id']) : '',
753 651 "home_placement" => !empty($_POST['home_placement']) ? trim($_POST['home_placement']) : '',
754 652
755 653 "home_location_string_occurrence" => !empty($_POST['home_location_string_occurrence']) ? $_POST['home_location_string_occurrence'] : '',
756 - "home_location_string" => !empty($_POST['home_location_string']) ? trim($_POST['home_location_string']) : '',
757 - "category_enabled" => isset($_POST['category_enabled']) ? true : false,
758 - "category_widget_id" => !empty($_POST['category_widget_id']) ? trim($_POST['category_widget_id']) : '',
759 - "category_placement" => !empty($_POST['category_placement']) ? trim($_POST['category_placement']) : '',
760 - "category_location_string_occurrence" => !empty($_POST['category_location_string_occurrence']) ? $_POST['category_location_string_occurrence'] : '',
761 - "category_location_string" => !empty($_POST['category_location_string']) ? trim($_POST['category_location_string']) : '',
654 + "home_location_string" => !empty($_POST['home_location_string']) ? trim($_POST['home_location_string']) : ''
655 + );
762 656
763 - );
657 + //var_dump($settings);
764 658
765 659 $is_valid_nonce = false;
766 660
767 661 if (isset( $_POST['my_plugin_nonce']) && wp_verify_nonce( $_POST['my_plugin_nonce'], 'my_plugin_update_field_action' ) ) {
@@ -769,9 +663,9 @@
769 663 }
770 664
771 665 if ($is_valid_nonce) {
772 666 if($settings == NULL){
773 - $wpdb->insert($this->tbl_taboola_settings, $data);
667 + $wpdb->insert($this->tbl_taboola_settings, $data, null, null);
774 668 } else {
775 669 $wpdb->update($this->tbl_taboola_settings, $data, array('id' => $settings->id));
776 670 }
777 671 }
@@ -836,9 +730,21 @@
836 730 }
837 731 }
838 732
839 733 function is_db_updated_for_min_ver($min_ver) {
734 + /**
735 + * Checks if the DB was *previously* updated for the minimum version specified.
736 + * $min_ver should be passed in a format like this: "2.1.0"
737 + */
738 +
840 739 global $wpdb;
740 +
741 + // PC - Temporary patch for v2.1.0
742 +
743 + // START patch ===============================================
744 + // Check if the `_taboola` table exists.
745 + // If not, then return false.
746 +
841 747 $table_name = $wpdb->prefix . "_taboola_settings";
842 748
843 749 if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
844 750 tb_write_log("Table NOT FOUND");
@@ -846,31 +752,55 @@
846 752 }
847 753 else {
848 754 tb_write_log("Table EXISTS");
849 755 }
756 + // END patch =================================================
757 +
758 + // Check the saved version in wp_options. Default to '1.0.0' if the option is not found.
850 759 $saved_version = get_option(TABOOLA_OPTION_NAME, '1.0.0');
851 760
852 761 $db_is_up_to_date = version_compare($saved_version, $min_ver, '>=');
762 + //tb_write_log($db_is_up_to_date);
853 763 return $db_is_up_to_date;
854 764
855 765 }
856 766
857 767 function is_db_updated_for_current_ver() {
768 + /**
769 + * DEPRECATED
770 + * Checks if the DB was *previously* updated for the version currently loaded.
771 + */
858 772 global $wpdb;
773 +
774 + // Check the saved version in wp_options. Default to '1.0.0' if the option is not found.
859 775 $saved_version = get_option(TABOOLA_OPTION_NAME, '1.0.0');
776 +
777 + // Get the currently loaded plugin version.
860 778 $plugin_version = $this->get_loaded_plugin_version();
779 +
861 780 $db_is_up_to_date = version_compare($saved_version, $plugin_version, '>=');
781 + //tb_write_log($db_is_up_to_date);
862 782 return $db_is_up_to_date;
783 +
863 784 }
864 785
865 786 function get_loaded_plugin_version() {
866 - $plugin_data = get_plugin_data( __FILE__ );
787 +
788 + $plugin_data = get_plugin_data( __FILE__ ); // Can be invoked from Admin page only
867 789 $loaded_plugin_version = $plugin_data['Version'];
868 790 return $loaded_plugin_version;
791 +
869 792 }
870 793
871 794 function save_taboola_version($min_ver) {
795 + /**
796 + * Checks for the saved version in wp_options.
797 + * If not found - adds it. Else - updates it.
798 + */
799 +
800 +
872 801 tb_write_log("SAVING NEW MIN VERSION: " . $min_ver); // PC
802 +
873 803 $saved_version = get_option(TABOOLA_OPTION_NAME, '');
874 804 if ($saved_version == '') {
875 805 add_option(TABOOLA_OPTION_NAME, $min_ver);
876 806 }
@@ -879,9 +809,12 @@
879 809 }
880 810 }
881 811
882 812 function update_db(){
813 +
814 + // If we are up to date, then skip this method...
883 815 if ($this->is_db_updated_for_min_ver(TABOOLA_MIN_VER)) {
816 + //tb_write_log("All up to date!");
884 817 return;
885 818 }
886 819
887 820 $this->save_taboola_version(TABOOLA_MIN_VER);
@@ -888,17 +821,24 @@
888 821
889 822 global $wpdb;
890 823 require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
891 824
892 - $charset_collate = '';
893 - if ( ! empty( $wpdb->charset ) ) {
894 - $charset_collate = "DEFAULT CHARACTER SET {$wpdb->charset}";
825 +
826 + // Handling for older MySQL versions
827 + if (function_exists('mysql_get_server_info') && version_compare(mysql_get_server_info(), '4.1.0', '>=')) {
828 + if (!empty($wpdb->charset))
829 + $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
830 + if (!empty($wpdb->collate))
831 + $charset_collate .= " COLLATE $wpdb->collate";
895 832 }
896 - if ( ! empty( $wpdb->collate ) ) {
897 - $charset_collate .= " COLLATE {$wpdb->collate}";
898 - }
899 833
834 + // Check if this is an upgrade from v1
900 835 $is_upgrade_from_v1 = $this->is_upgrade_from_v1();
836 +
837 + // tb_write_log("Is upgrade from v1: " . ($is_upgrade_from_v1 ? 'true' : 'false'));
838 +
839 +
840 + //settings table structure
901 841 $sql_table_settings = "
902 842 CREATE TABLE `" . $wpdb->prefix . "_taboola_settings` (
903 843 `id` INT NOT NULL AUTO_INCREMENT ,
904 844 `publisher_id` VARCHAR(255) DEFAULT NULL,
@@ -906,23 +846,31 @@
906 846 `publisher_id_push` INT(15) DEFAULT NULL,
907 847 `first_bc_enabled` TINYINT(1) NOT NULL DEFAULT FALSE,
908 848 `first_bc_widget_id` VARCHAR(255) DEFAULT NULL,
909 849 `first_bc_placement` VARCHAR(255) DEFAULT " . ($is_upgrade_from_v1 ? "'below-article'" : "NULL") .",
850 + `first_bc_custom_css` TEXT DEFAULT NULL,
851 + `second_bc_enabled` TINYINT(1) NOT NULL DEFAULT FALSE,
852 + `second_bc_widget_id` VARCHAR(255) DEFAULT NULL,
853 + `second_bc_custom_css` TEXT DEFAULT NULL,
854 + `location_string` TEXT DEFAULT NULL,
855 + `mid_enabled` TINYINT(1) NOT NULL DEFAULT FALSE,
856 + `mid_widget_id` VARCHAR(255) DEFAULT NULL,
857 + `mid_placement` VARCHAR(255) DEFAULT NULL,
910 858 `out_of_content_enabled` TINYINT(1) NOT NULL DEFAULT TRUE,
911 - `mid_enabled` TINYINT(1) NOT NULL DEFAULT FALSE,
912 - `mid_widgets` TEXT DEFAULT NULL,
859 + `mid_location_string` TEXT DEFAULT NULL,
860 + `mid_location_string_occurrence` SMALLINT DEFAULT NULL,
861 + `mid_paragraph_ui_mode` VARCHAR(255) DEFAULT NULL,
913 862 `home_enabled` TINYINT(1) NOT NULL DEFAULT FALSE,
914 863 `home_widget_id` VARCHAR(255) DEFAULT NULL,
915 864 `home_placement` VARCHAR(255) DEFAULT NULL,
916 865 `home_location_string` TEXT DEFAULT NULL,
917 866 `home_location_string_occurrence` SMALLINT DEFAULT NULL,
918 - `category_enabled` TINYINT(1) NOT NULL DEFAULT FALSE,
919 - `category_widget_id` VARCHAR(255) DEFAULT NULL,
920 - `category_placement` VARCHAR(255) DEFAULT NULL,
921 - `category_location_string` TEXT DEFAULT NULL,
922 - `category_location_string_occurrence` SMALLINT DEFAULT NULL,
923 867 PRIMARY KEY (`id`)
924 - ) $charset_collate;";
868 + )" . $charset_collate . ";";
869 +
870 + // tb_write_log($sql_table_settings);
871 +
872 + // create/update the table
925 873 dbDelta($sql_table_settings);
926 874 }
927 875 }
928 876 }
@@ -929,11 +877,19 @@
929 877
930 878 global $taboolaWP;
931 879 $taboolaWP = new TaboolaWP();
932 880
881 +/*
882 +A few utility methods for logging
883 +*/
884 +
885 +// Writes debugging messages to 'logs/debug.log'.
886 +// The exact file location depends on *where* you trigger the function.
887 +// In this script, we trigger the function from the admin dashboard.
888 +// So the log file is located under 'wp-admin/logs'.
933 889 function tb_write_log($log_msg)
934 890 {
935 - if (!TABOOLA_DEBUG_MODE)
891 + if (!TABOOLA_DEBUG_MODE) // If not debug mode, do nothing
936 892 return;
937 893
938 894 $log_filename = "logs";
939 895 if (!file_exists($log_filename))
@@ -941,8 +897,9 @@
941 897 mkdir($log_filename, 0777, true);
942 898 }
943 899 $log_file_data = $log_filename.'/debug.log';
944 900
901 + // Add date/time
945 902 $date = date('Y-m-d H:i:s');
946 903 $log_msg_with_date = $date." : ".$log_msg;
947 904
948 905 file_put_contents($log_file_data, $log_msg_with_date . "\n", FILE_APPEND);
@@ -947,8 +904,9 @@
947 904
948 905 file_put_contents($log_file_data, $log_msg_with_date . "\n", FILE_APPEND);
949 906 }
950 907
908 +// Write to console
951 909 function tb_console_log( $data ){
952 910
953 911 If(wp_get_environment_type() === 'development') {
954 912
@@ -958,8 +916,9 @@
958 916
959 917 }
960 918 }
961 919
920 +// Write to webpage and stop
962 921 function tb_print_to_page($data) {
963 922 If(wp_get_environment_type() === 'development') {
964 923 print_r($data);
965 924 die;
@@ -964,17 +923,19 @@
964 923 print_r($data);
965 924 die;
966 925 }
967 926 }
927 +//
968 928
929 +// Enqueue js_inject.min.js script for XPath injection
969 930 function enqueue_taboola_scripts() {
970 931 wp_register_script(
971 932 'taboola-injector',
972 933 plugins_url('js/js_inject.min.js', __FILE__),
973 - array(),
974 - null,
975 - true
934 + array(), // No dependencies
935 + null, // No version specified
936 + true // Load in footer
976 937 );
977 938
978 939 wp_enqueue_script('taboola-injector');
979 940 }
980 -add_action('wp_enqueue_scripts', 'enqueue_taboola_scripts');
941 +add_action('wp_enqueue_scripts', 'enqueue_taboola_scripts');