PluginProbe
Taboola / 3.0.1
Taboola v3.0.1
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 +366 -255 2.2.23.0.1 View file →
@@ -1,122 +1,216 @@
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: 2.2.2
7 - * Author: Taboola
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
8 8 */
9 9
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)
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 );
13 13
14 -define ("TABOOLA_OPTION_NAME","taboola_plugin_version"); // Note: if this release has DB changes, then the min version will be saved under 'taboola_plugin_version' in 'wp_options'.
14 +define( 'TABOOLA_OPTION_NAME', 'taboola_plugin_version' );
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
23 26
24 -include_once('widget.php');
25 -require_once('JavaScriptWrapper.php');
27 +if ( ! class_exists( 'TaboolaWP' ) ) {
28 +class TaboolaWP {
26 29
30 + /* ─────────────────────────── properties ─────────────────────────── */
31 + public $data = [];
32 + public $_is_widget_on_page;
33 + public $_is_head_script_loaded = false;
27 34
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>
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>
42 39 <pre><code>{{SW}}</code></pre>
43 - <p>
44 - Also make sure that the file is accessible at {{DOMAIN}}/sw.js
45 - </p>
46 - SWE;
40 + <p>Also make sure it's reachable at <code>{{DOMAIN}}/sw.js</code></p>
41 +SWE;
47 42
48 - public $plugin_name;
49 - public $plugin_directory;
50 - public $plugin_url;
51 - public $settings;
52 - public $tbl_taboola_settings;
43 + public $plugin_name;
44 + public $plugin_directory;
45 + public $plugin_url;
46 + public $settings;
47 + public $tbl_taboola_settings;
53 48
54 - public function __construct()
55 - {
56 - global $wpdb;
49 + /* ─────────────────────────── constructor ─────────────────────────── */
50 + public function __construct() {
51 + global $wpdb;
52 + //initialize plugin constant
53 + define( 'TaboolaWP', true );
57 54
58 - //initialize plugin constant
59 - DEFINE('TaboolaWP', true);
55 + $this->_is_widget_on_page = false;
56 + $this->_is_head_script_loaded = false;
60 57
61 - $this->_is_widget_on_page = false;
62 - $this->_is_head_script_loaded = false;
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 + );
63 67
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");
68 + /* activation / upgrade */
69 + register_activation_hook( $this->plugin_name, [ $this, 'update_db' ] );
70 + add_action( 'admin_init', [ $this, 'update_db' ] );
68 71
69 - $this->tbl_taboola_settings = $wpdb->prefix . '_taboola_settings';
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 + }
70 79
71 - //activation function
72 - register_activation_hook($this->plugin_name, array(&$this, 'update_db'));
73 - add_action('admin_init', array(&$this, 'update_db'));
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' ] );
74 95
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 - }
96 + $sw = 'sw.js';
97 + $sw_path = ABSPATH . $sw;
84 98
85 - //$this->should_place_tag_outside_of_content = $this->settings->out_of_content_enabled;
99 + $content = file_exists( $sw_path ) ? file_get_contents( $sw_path ) : '';
86 100
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);
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', 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 - }
111 + add_action( 'wp_footer', [ $this, 'taboola_footer_loader_js' ] );
112 +
113 + /* content widgets */
114 + add_filter( 'the_content', [ $this, 'load_taboola_content' ] );
115 + add_filter( 'the_content', [ $this, 'load_taboola_content_mid' ] );
116 +
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 + -------------------------------------------------------------------- */
142 +public function tb_home_buffer_start() {
143 + if ( $this->should_show_content_widget_home() ) {
144 + ob_start( [ $this, 'tb_home_buffer_inject' ] );
116 145 }
146 +}
147 +public function tb_home_buffer_inject( $html ) {
148 + return $this->load_taboola_content_home( $html );
149 +}
117 150
118 - function plugin_action_links($links, $file) {
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 +public function tb_home_buffer_flush() {
204 + if ( ob_get_length() ) {
205 + echo ob_get_clean();
206 + }
207 +}
208 +
209 +
210 +
211 +
212 +function plugin_action_links($links, $file) {
119 213 static $this_plugin;
120 214
121 215 if (!$this_plugin) {
122 216 $this_plugin = plugin_basename(__FILE__);
@@ -138,14 +232,17 @@
138 232 private function should_show_content_widget_mid(){
139 233 // PC - only if v2 was installed:
140 234 if (!$this->is_db_updated_for_min_ver("2.0.0"))
141 235 return false;
142 - $retVal1 = ((trim($this->settings->publisher_id) != '') && is_single() && $this->settings->mid_enabled && trim($this->settings->mid_widget_id) != '');
236 + $retVal1 = ((trim($this->settings->publisher_id) != '') && is_single() && !empty($this->settings->mid_enabled));
143 237 return $retVal1;
144 238 }
145 239
146 240 private function should_show_content_widget_home(){
147 241 // PC - only if v2 was installed:
242 +
243 + //error_log( print_r( $this->settings, true ) ); // TEMP line
244 +
148 245 if (!$this->is_db_updated_for_min_ver("2.0.0"))
149 246 return false;
150 247 $retVal2 = ((trim($this->settings->publisher_id) != '') && (is_front_page() || is_home()) && $this->settings->home_enabled && trim($this->settings->home_widget_id) != '');
151 248 return $retVal2;
@@ -157,9 +254,9 @@
157 254 }
158 255
159 256 // Determine if a taboola widget should be added somewhere on the current page (content or sidebar)
160 257 function is_widget_on_page(){
161 - return $this->should_show_content_widget() || $this->should_show_content_widget_mid() || $this->should_show_content_widget_home() || $this->should_show_sidebar_widget();
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() ;
162 259 }
163 260
164 261 function get_page_type(){
165 262 $page_type='article';
@@ -171,15 +268,21 @@
171 268 return $page_type;
172 269 }
173 270
174 271 // return the head loader script
175 - function taboola_header_loader_js() {
176 - $head_string = "";
272 + function taboola_header_loader_js() {
273 + 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 + }
177 284
178 - // Only adding the loader if a widget is going to be placed on the page.
179 - if ($this->is_widget_on_page()){
180 - // PC - since these params will be inserted in 'loaderInjectionScript.js' via search and replace,
181 - // double brackets are used to ensure that each key is a unique string.
182 285 $stringParams = array(
183 286 '{{PUBLISHER_ID}}' => $this->settings->publisher_id,
184 287 '{{PAGE_TYPE}}' => $this->get_page_type(),
185 288 '{{WORDPRESS_VERSION}}' => get_bloginfo('version'),
@@ -184,19 +287,18 @@
184 287 '{{PAGE_TYPE}}' => $this->get_page_type(),
185 288 '{{WORDPRESS_VERSION}}' => get_bloginfo('version'),
186 289 '{{PHP_VERSION}}' => phpversion(),
187 290 '{{PLUGIN_VERSION}}' => TABOOLA_PLUGIN_VERSION,
188 - '{{LOC_MID}}' => $this->settings->mid_location_string,
189 - '{{LOC_HOME}}' => $this->settings->home_location_string
291 + '{{LOC_MID}}' => $mid_locations_string,
292 + '{{LOC_HOME}}' => $this->settings->home_location_string ?? ''
190 293 );
191 294
192 295 $scriptWrapper = new JavaScriptWrapper("loaderInjectionScript.js",$stringParams);
193 - $head_string = $scriptWrapper->getScriptMarkupString();
296 + return $scriptWrapper->getScriptMarkupString();
194 297 }
195 - return $head_string;
298 + return "";
196 299 }
197 300
198 -
199 301 // This function is used for the hook action, injects the header content to the <head> tag.
200 302 function taboola_header_loader_inject(){
201 303 echo $this->taboola_header_loader_js();
202 304 }
@@ -244,24 +346,37 @@
244 346 return $content;
245 347 }
246 348
247 349 // Mid-article-widget
248 - function load_taboola_content_mid($content)
249 - {
250 - $taboola_content_mid = array();
350 + function load_taboola_content_mid($content) {
251 351 if ($this->should_show_content_widget_mid()){
352 + $mid_widgets = json_decode($this->settings->mid_widgets ?? '[]');
252 353
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;
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 + );
260 363
261 - $content = $this->embed_taboola_content_location_mid($content,$taboola_content_mid,trim($this->settings->mid_location_string));
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 + );
369 +
370 + $content = $this->embed_taboola_content_location_mid(
371 + $content,
372 + $taboola_content_mid,
373 + $widget->location_string,
374 + $widget->occurrence
375 + );
376 + }
377 + }
262 378 }
263 -
264 379 return $content;
265 380 }
266 381
267 382 // Homepage widget
@@ -266,9 +381,9 @@
266 381
267 382 // Homepage widget
268 383 function load_taboola_content_home($content)
269 384 {
270 -
385 + //error_log( 'TB-DEBUG ② entered load_taboola_content_home' );
271 386 $taboola_content_home = array();
272 387 if ($this->should_show_content_widget_home()){
273 388
274 389 $homeWidgetParams = array('{{WIDGET_ID}}' => $this->settings->home_widget_id,
@@ -283,9 +398,33 @@
283 398 }
284 399 return $content;
285 400
286 401 }
402 + /* ------------------------------------------------------------------
403 + * CATEGORY ▸ build widget & inject
404 + * ------------------------------------------------------------------*/
405 +public function load_taboola_content_category( $content ) {
287 406
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 +
288 427 // Below-article widget
289 428
290 429 // Extract the taboola content in the required format:
291 430 // String - for injecting on the servr side
@@ -352,8 +491,18 @@
352 491 return $ret_val;
353 492
354 493 }
355 494
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 +}
356 505 // Below-article widget
357 506 // Do the actual logic of choosing where to place the taboola content.
358 507 function embed_taboola_content_location($content, $taboola_content){
359 508 $do_default = true;
@@ -379,9 +528,9 @@
379 528 }
380 529
381 530 // Mid-article widget
382 531 // Do the actual logic of choosing where to place the taboola content based on the "location" attribute
383 - function embed_taboola_content_location_mid($content, $taboola_content_mid, $location){
532 + function embed_taboola_content_location_mid($content, $taboola_content_mid, $location, $occurrence = 1){
384 533 $do_default = true;
385 534
386 535 if (isset($location) && $location != ''){
387 536 $first_char = substr($location,0,1);
@@ -408,9 +557,9 @@
408 557 } else{
409 558 require_once('simple_html_dom.php');
410 559
411 560 $html_doc = str_get_html($content);
412 - $target_location = $html_doc->find($location,($this->settings->mid_location_string_occurrence)-1);
561 + $target_location = $html_doc->find($location, ($occurrence) - 1);
413 562
414 563 // if the location was found within the html content
415 564 if (isset($target_location) && is_object($target_location)){
416 565
@@ -420,12 +569,8 @@
420 569 $do_default = false;
421 570 }
422 571 }
423 572 }
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 - // }
428 573
429 574 return $content;
430 575 }
431 576
@@ -432,8 +577,22 @@
432 577 // Homepage widget
433 578 // Do the actual logic of choosing where to place the taboola content based on the "location" attribute
434 579 function embed_taboola_content_location_home($content, $taboola_content_home, $location){
435 580
581 +
582 + // error_log( 'TB-DEBUG ③ raw selector string: [' . $location . ']' );
583 +
584 + // load parser
585 + //require_once plugin_dir_path( __FILE__ ) . 'simple_html_dom.php';
586 + // $html_doc = str_get_html( $content );
587 +
588 + // occurrence: settings is 1-based, find() wants 0-based
589 + // $occurrence = max( 1, intval( $this->settings->home_location_string_occurrence ) );
590 + // $node = $html_doc->find( $location, $occurrence - 1 );
591 + // $found = is_object( $node ) ? 1 : 0;
592 +
593 + // error_log( "TB-DEBUG ③ found {$found} matching node(s) for selector [{$location}]" );
594 + // ────────────────────────────
436 595 $do_default = true;
437 596
438 597 if (isset($location) && $location != ''){
439 598 $first_char = substr($location,0,1);
@@ -473,12 +632,13 @@
473 632 }
474 633 }
475 634 }
476 635 // Default for below-article widget - add to the end of the content
477 - // if ($do_default){
478 - // $content = $content.$this->format_taboola_content_home($taboola_content_home,TABOOLA_CONTENT_FORMAT_STRING);
479 - // }
636 + if ($do_default){
637 + $content = $content.$this->format_taboola_content_home($taboola_content_home,TABOOLA_CONTENT_FORMAT_STRING);
638 + }
480 639 return $content;
640 +
481 641
482 642 }
483 643
484 644 function admin_generate_menu(){
@@ -511,33 +671,18 @@
511 671 }
512 672 }
513 673
514 674 if(isset($_POST['mid_enabled'])) {
515 - if (trim(strip_tags($_POST['mid_widget_id'])) == '') {
516 - $taboola_errors[] = "Mid-article > Widget ID";
517 - }
518 - if (trim(strip_tags($_POST['mid_placement'])) == '') {
519 - $taboola_errors[] = "Mid-article > Placement Name";
520 - }
521 - if (trim(strip_tags($_POST['mid_location_string'])) == '') {
522 - $taboola_errors[] = "Mid-article > CSS selector";
523 - }
524 - else {
525 - // If the 'location' WAS filled in, then...
526 -
527 - // 1) Check for a valid 'location':
528 -
529 - // Validation method has not been implemented
530 - // mid_if (!$this->is_locaton_string_valid1($_POST['mid_location_string'])) {
531 - // $taboola_errors[] = "Mid-article > CSS Selector (invalid value)";
532 - // }
533 -
534 - // 2) Validate the 'occurrence':
535 - if (!$this->is_mid_location_string_occurrence_valid($_POST['mid_location_string_occurrence'])) {
536 - $taboola_errors[] = "Mid-article > Occurance (must be >= 1)";
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 + }
537 683 }
538 684 }
539 -
540 685 }
541 686
542 687 if(isset($_POST['home_enabled'])) {
543 688 if (trim(strip_tags($_POST['home_widget_id'])) == '') {
@@ -550,25 +695,45 @@
550 695 if (trim(strip_tags($_POST['home_location_string'])) == '') {
551 696 $taboola_errors[] = "Homepage > CSS selector";
552 697 }
553 698 else {
554 - // If the 'location' WAS filled in, then...
555 -
556 - // 1) Check for a valid 'location':
557 -
558 - // Validation method has not been implemented
559 - // if (!$this->is_home_location_string_valid($_POST['home_location_string'])) {
560 - // $taboola_errors[] = "Homepage > CSS Selector (invalid value)";
561 - // }
562 -
563 - // 2) Validate the 'occurrence':
564 699 if (!empty($_POST['home_location_string']) && !$this->is_home_location_string_occurrence_valid($_POST['home_location_string_occurrence'])) {
565 700 $taboola_errors[] = "Homepage > Occurance (must be >= 1)";
566 701 }
567 702 }
568 - }
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 + }
569 710
570 711 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 +
571 736 $data = array(
572 737 "publisher_id" => trim($_POST['publisher_id']),
573 738
574 739 "web_push_enabled" => isset($_POST['web_push_enabled']) ? true : false,
@@ -580,25 +745,24 @@
580 745
581 746 "out_of_content_enabled" => isset($_POST['out_of_content_enabled']) ? true : false,
582 747
583 748 "mid_enabled" => isset($_POST['mid_enabled']) ? true : false,
584 - "mid_widget_id" => !empty($_POST['mid_widget_id']) ? trim($_POST['mid_widget_id']) : '',
585 - "mid_placement" => !empty($_POST['mid_placement']) ? trim($_POST['mid_placement']) : '',
586 - "mid_paragraph_ui_mode" => !empty($_POST['mid_paragraph_ui_mode']) ? trim($_POST['mid_paragraph_ui_mode']) : '',
587 -
588 - "mid_location_string_occurrence" => !empty($_POST['mid_location_string_occurrence']) ? $_POST['mid_location_string_occurrence'] : '',
589 - "mid_location_string" => !empty($_POST['mid_location_string']) ? trim($_POST['mid_location_string']) : '',
590 -
749 + "mid_widgets" => $mid_widgets_json,
750 +
591 751 "home_enabled" => isset($_POST['home_enabled']) ? true : false,
592 752 "home_widget_id" => !empty($_POST['home_widget_id']) ? trim($_POST['home_widget_id']) : '',
593 753 "home_placement" => !empty($_POST['home_placement']) ? trim($_POST['home_placement']) : '',
594 754
595 755 "home_location_string_occurrence" => !empty($_POST['home_location_string_occurrence']) ? $_POST['home_location_string_occurrence'] : '',
596 - "home_location_string" => !empty($_POST['home_location_string']) ? trim($_POST['home_location_string']) : ''
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']) : '',
762 +
597 763 );
598 764
599 - //var_dump($settings);
600 -
601 765 $is_valid_nonce = false;
602 766
603 767 if (isset( $_POST['my_plugin_nonce']) && wp_verify_nonce( $_POST['my_plugin_nonce'], 'my_plugin_update_field_action' ) ) {
604 768 $is_valid_nonce = true;
@@ -605,9 +769,9 @@
605 769 }
606 770
607 771 if ($is_valid_nonce) {
608 772 if($settings == NULL){
609 - $wpdb->insert($this->tbl_taboola_settings, $data, null, null);
773 + $wpdb->insert($this->tbl_taboola_settings, $data);
610 774 } else {
611 775 $wpdb->update($this->tbl_taboola_settings, $data, array('id' => $settings->id));
612 776 }
613 777 }
@@ -672,21 +836,9 @@
672 836 }
673 837 }
674 838
675 839 function is_db_updated_for_min_ver($min_ver) {
676 - /**
677 - * Checks if the DB was *previously* updated for the minimum version specified.
678 - * $min_ver should be passed in a format like this: "2.1.0"
679 - */
680 -
681 840 global $wpdb;
682 -
683 - // PC - Temporary patch for v2.1.0
684 -
685 - // START patch ===============================================
686 - // Check if the `_taboola` table exists.
687 - // If not, then return false.
688 -
689 841 $table_name = $wpdb->prefix . "_taboola_settings";
690 842
691 843 if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
692 844 tb_write_log("Table NOT FOUND");
@@ -694,55 +846,31 @@
694 846 }
695 847 else {
696 848 tb_write_log("Table EXISTS");
697 849 }
698 - // END patch =================================================
699 -
700 - // Check the saved version in wp_options. Default to '1.0.0' if the option is not found.
701 850 $saved_version = get_option(TABOOLA_OPTION_NAME, '1.0.0');
702 851
703 852 $db_is_up_to_date = version_compare($saved_version, $min_ver, '>=');
704 - //tb_write_log($db_is_up_to_date);
705 853 return $db_is_up_to_date;
706 854
707 855 }
708 856
709 857 function is_db_updated_for_current_ver() {
710 - /**
711 - * DEPRECATED
712 - * Checks if the DB was *previously* updated for the version currently loaded.
713 - */
714 858 global $wpdb;
715 -
716 - // Check the saved version in wp_options. Default to '1.0.0' if the option is not found.
717 859 $saved_version = get_option(TABOOLA_OPTION_NAME, '1.0.0');
718 -
719 - // Get the currently loaded plugin version.
720 860 $plugin_version = $this->get_loaded_plugin_version();
721 -
722 861 $db_is_up_to_date = version_compare($saved_version, $plugin_version, '>=');
723 - //tb_write_log($db_is_up_to_date);
724 862 return $db_is_up_to_date;
725 -
726 863 }
727 864
728 865 function get_loaded_plugin_version() {
729 -
730 - $plugin_data = get_plugin_data( __FILE__ ); // Can be invoked from Admin page only
866 + $plugin_data = get_plugin_data( __FILE__ );
731 867 $loaded_plugin_version = $plugin_data['Version'];
732 868 return $loaded_plugin_version;
733 -
734 869 }
735 870
736 871 function save_taboola_version($min_ver) {
737 - /**
738 - * Checks for the saved version in wp_options.
739 - * If not found - adds it. Else - updates it.
740 - */
741 -
742 -
743 872 tb_write_log("SAVING NEW MIN VERSION: " . $min_ver); // PC
744 -
745 873 $saved_version = get_option(TABOOLA_OPTION_NAME, '');
746 874 if ($saved_version == '') {
747 875 add_option(TABOOLA_OPTION_NAME, $min_ver);
748 876 }
@@ -751,12 +879,9 @@
751 879 }
752 880 }
753 881
754 882 function update_db(){
755 -
756 - // If we are up to date, then skip this method...
757 883 if ($this->is_db_updated_for_min_ver(TABOOLA_MIN_VER)) {
758 - //tb_write_log("All up to date!");
759 884 return;
760 885 }
761 886
762 887 $this->save_taboola_version(TABOOLA_MIN_VER);
@@ -763,24 +888,17 @@
763 888
764 889 global $wpdb;
765 890 require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
766 891
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";
892 + $charset_collate = '';
893 + if ( ! empty( $wpdb->charset ) ) {
894 + $charset_collate = "DEFAULT CHARACTER SET {$wpdb->charset}";
774 895 }
896 + if ( ! empty( $wpdb->collate ) ) {
897 + $charset_collate .= " COLLATE {$wpdb->collate}";
898 + }
775 899
776 - // Check if this is an upgrade from v1
777 900 $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
783 901 $sql_table_settings = "
784 902 CREATE TABLE `" . $wpdb->prefix . "_taboola_settings` (
785 903 `id` INT NOT NULL AUTO_INCREMENT ,
786 904 `publisher_id` VARCHAR(255) DEFAULT NULL,
@@ -788,31 +906,23 @@
788 906 `publisher_id_push` INT(15) DEFAULT NULL,
789 907 `first_bc_enabled` TINYINT(1) NOT NULL DEFAULT FALSE,
790 908 `first_bc_widget_id` VARCHAR(255) DEFAULT NULL,
791 909 `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,
910 + `out_of_content_enabled` TINYINT(1) NOT NULL DEFAULT TRUE,
797 911 `mid_enabled` TINYINT(1) NOT NULL DEFAULT FALSE,
798 - `mid_widget_id` VARCHAR(255) DEFAULT NULL,
799 - `mid_placement` VARCHAR(255) DEFAULT NULL,
800 - `out_of_content_enabled` TINYINT(1) NOT NULL DEFAULT TRUE,
801 - `mid_location_string` TEXT DEFAULT NULL,
802 - `mid_location_string_occurrence` SMALLINT DEFAULT NULL,
803 - `mid_paragraph_ui_mode` VARCHAR(255) DEFAULT NULL,
912 + `mid_widgets` TEXT DEFAULT NULL,
804 913 `home_enabled` TINYINT(1) NOT NULL DEFAULT FALSE,
805 914 `home_widget_id` VARCHAR(255) DEFAULT NULL,
806 915 `home_placement` VARCHAR(255) DEFAULT NULL,
807 916 `home_location_string` TEXT DEFAULT NULL,
808 917 `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,
809 923 PRIMARY KEY (`id`)
810 - )" . $charset_collate . ";";
811 -
812 - // tb_write_log($sql_table_settings);
813 -
814 - // create/update the table
924 + ) $charset_collate;";
815 925 dbDelta($sql_table_settings);
816 926 }
817 927 }
818 928 }
@@ -819,18 +929,13 @@
819 929
820 930 global $taboolaWP;
821 931 $taboolaWP = new TaboolaWP();
822 932
823 -/*
824 -A few utility methods for logging
825 -*/
826 -
827 -// Writes debugging messages to 'logs/debug.log'.
828 -// The exact file location depends on *where* you trigger the function.
829 -// In this script, we trigger the function from the admin dashboard.
830 -// So the log file is located under 'wp-admin/logs'.
831 933 function tb_write_log($log_msg)
832 934 {
935 + if (!TABOOLA_DEBUG_MODE)
936 + return;
937 +
833 938 $log_filename = "logs";
834 939 if (!file_exists($log_filename))
835 940 {
836 941 mkdir($log_filename, 0777, true);
@@ -836,18 +941,14 @@
836 941 mkdir($log_filename, 0777, true);
837 942 }
838 943 $log_file_data = $log_filename.'/debug.log';
839 944
840 - // Add date/time
841 945 $date = date('Y-m-d H:i:s');
842 946 $log_msg_with_date = $date." : ".$log_msg;
843 -
844 - if (TABOOLA_DEBUG_MODE)
845 - file_put_contents($log_file_data, $log_msg_with_date . "\n", FILE_APPEND);
846 -
947 +
948 + file_put_contents($log_file_data, $log_msg_with_date . "\n", FILE_APPEND);
847 949 }
848 950
849 -// Write to console
850 951 function tb_console_log( $data ){
851 952
852 953 If(wp_get_environment_type() === 'development') {
853 954
@@ -857,9 +958,8 @@
857 958
858 959 }
859 960 }
860 961
861 -// Write to webpage and stop
862 962 function tb_print_to_page($data) {
863 963 If(wp_get_environment_type() === 'development') {
864 964 print_r($data);
865 965 die;
@@ -865,5 +965,16 @@
865 965 die;
866 966 }
867 967 }
868 968
869 -//
969 +function enqueue_taboola_scripts() {
970 + wp_register_script(
971 + 'taboola-injector',
972 + plugins_url('js/js_inject.min.js', __FILE__),
973 + array(),
974 + null,
975 + true
976 + );
977 +
978 + wp_enqueue_script('taboola-injector');
979 +}
980 +add_action('wp_enqueue_scripts', 'enqueue_taboola_scripts');