PluginProbe
Taboola / 3.0.2
Taboola v3.0.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 +374 -257 2.2.23.0.2 View file →
@@ -1,122 +1,218 @@
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.2
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.2' ); // 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 +if ( ! class_exists( 'simple_html_dom' ) ) {
26 + require_once plugin_dir_path( __FILE__ ) . 'simple_html_dom.php'; // ← NEW
27 +}
23 28
24 -include_once('widget.php');
25 -require_once('JavaScriptWrapper.php');
29 +if ( ! class_exists( 'TaboolaWP' ) ) {
30 +class TaboolaWP {
26 31
32 + /* ─────────────────────────── properties ─────────────────────────── */
33 + public $data = [];
34 + public $_is_widget_on_page;
35 + public $_is_head_script_loaded = false;
27 36
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>
37 + private $tpl_sw = 'importScripts("https://cdn.taboola.com/webpush/tsw.js");';
38 + private $msg_sw_error = <<<SWE
39 + <p>The file <code>sw.js</code> in the WP root is not writable.
40 + Please change its permissions or paste the code below manually:</p>
42 41 <pre><code>{{SW}}</code></pre>
43 - <p>
44 - Also make sure that the file is accessible at {{DOMAIN}}/sw.js
45 - </p>
46 - SWE;
42 + <p>Also make sure it's reachable at <code>{{DOMAIN}}/sw.js</code></p>
43 +SWE;
47 44
48 - public $plugin_name;
49 - public $plugin_directory;
50 - public $plugin_url;
51 - public $settings;
52 - public $tbl_taboola_settings;
45 + public $plugin_name;
46 + public $plugin_directory;
47 + public $plugin_url;
48 + public $settings;
49 + public $tbl_taboola_settings;
53 50
54 - public function __construct()
55 - {
56 - global $wpdb;
51 + /* ─────────────────────────── constructor ─────────────────────────── */
52 + public function __construct() {
53 + global $wpdb;
54 + //initialize plugin constant
55 + define( 'TaboolaWP', true );
57 56
58 - //initialize plugin constant
59 - DEFINE('TaboolaWP', true);
57 + $this->_is_widget_on_page = false;
58 + $this->_is_head_script_loaded = false;
60 59
61 - $this->_is_widget_on_page = false;
62 - $this->_is_head_script_loaded = false;
60 + $this->plugin_name = plugin_basename( __FILE__ );
61 + $this->plugin_directory = plugin_dir_path( __FILE__ );
62 + $this->plugin_url = trailingslashit(
63 + WP_PLUGIN_URL . '/' . dirname( plugin_basename( __FILE__ ) )
64 + );
65 + $this->tbl_taboola_settings = $wpdb->prefix . '_taboola_settings';
66 + $this->settings = $wpdb->get_row(
67 + "SELECT * FROM {$this->tbl_taboola_settings} LIMIT 1"
68 + );
63 69
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");
70 + /* activation / upgrade */
71 + register_activation_hook( $this->plugin_name, [ $this, 'update_db' ] );
72 + add_action( 'admin_init', [ $this, 'update_db' ] );
68 73
69 - $this->tbl_taboola_settings = $wpdb->prefix . '_taboola_settings';
74 + /* widgets */
75 + if ( $this->settings && ! empty( $this->settings->publisher_id ) ) {
76 + add_action(
77 + 'widgets_init',
78 + static fn() => register_widget( 'WP_Widget_Taboola' )
79 + );
80 + }
70 81
71 - //activation function
72 - register_activation_hook($this->plugin_name, array(&$this, 'update_db'));
73 - add_action('admin_init', array(&$this, 'update_db'));
82 + /* admin vs front-end hooks */
83 + if ( is_admin() ) {
84 + add_action( 'admin_menu', [ $this, 'admin_generate_menu' ] );
85 + add_filter( 'plugin_action_links',
86 + [ $this, 'plugin_action_links' ], 10, 2 );
87 + }
88 +
89 +
90 +
91 +
92 + elseif ( $this->settings ) {
93 + /* loader & flush */
94 + add_action( 'wp_head', [ $this, 'taboola_header_loader_inject' ] );
95 + if ( ! empty( $this->settings->publisher_id_push ) ) {
96 + add_action( 'wp_head', [ $this, 'taboola_webpush_loader_js' ] );
74 97
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 + $sw = 'sw.js';
99 + $sw_path = ABSPATH . $sw;
84 100
85 - //$this->should_place_tag_outside_of_content = $this->settings->out_of_content_enabled;
101 + $content = file_exists( $sw_path ) ? file_get_contents( $sw_path ) : '';
86 102
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);
103 + if ( strpos( $content, $this->tpl_sw ) === false ) {
104 + if ( ! is_writable( ABSPATH ) || ( file_exists( $sw_path ) && ! is_writable( $sw_path ) ) ) {
105 + return $this->notice( $this->msg_sw_error );
104 106 }
105 107 $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 + if ( file_put_contents( $sw_path, $content ) === false ) {
109 + return $this->notice( $this->msg_sw_error );
108 110 }
109 111 }
110 112 }
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 - }
113 + add_action( 'wp_footer', [ $this, 'taboola_footer_loader_js' ] );
114 +
115 + /* content widgets */
116 + add_filter( 'the_content', [ $this, 'load_taboola_content' ] );
117 + add_filter( 'the_content', [ $this, 'load_taboola_content_mid' ] );
118 +
119 + /* full-page buffer – home & category */
120 + add_action( 'template_redirect', [ $this, 'tb_home_buffer_start' ], 0 );
121 + add_action( 'shutdown', [ $this, 'tb_home_buffer_flush' ], PHP_INT_MAX );
122 +
123 + add_action( 'template_redirect', [ $this, 'tb_cat_buffer_start' ], 0 );
124 + add_action( 'shutdown', [ $this, 'tb_cat_buffer_flush' ], PHP_INT_MAX );
125 + }
126 + } // __construct
127 +
128 +
129 +/* ======================================================================
130 + CATEGORY ▸ should we show the widget?
131 + ====================================================================== */
132 +private function should_show_content_widget_category(): bool {
133 + return (
134 + trim( $this->settings->publisher_id ) !== '' &&
135 + ( is_category() || is_archive() || is_search() ) &&
136 + $this->settings->category_enabled &&
137 + trim( $this->settings->category_widget_id ) !== ''
138 + );
139 +}
140 +
141 +/* ----------------------------------------------------------------------
142 + HOMEPAGE ▸ full-page buffer
143 + -------------------------------------------------------------------- */
144 +public function tb_home_buffer_start() {
145 + if ( $this->should_show_content_widget_home() ) {
146 + ob_start( [ $this, 'tb_home_buffer_inject' ] );
116 147 }
148 +}
149 +public function tb_home_buffer_inject( $html ) {
150 + return $this->load_taboola_content_home( $html );
151 +}
117 152
118 - function plugin_action_links($links, $file) {
153 +/* ----------------------------------------------------------------------
154 + CATEGORY ▸ buffer hooks
155 + -------------------------------------------------------------------- */
156 +public function tb_cat_buffer_start() {
157 + if ( $this->should_show_content_widget_category() ) {
158 + ob_start( [ $this, 'tb_cat_buffer_inject' ] );
159 + }
160 +}
161 +public function tb_cat_buffer_inject( $html ) {
162 + return $this->load_taboola_content_category( $html );
163 +}
164 +public function tb_cat_buffer_flush() {
165 + if ( ob_get_length() ) {
166 + echo ob_get_clean();
167 + }
168 +}
169 +
170 +/* ----------------------------------------------------------------------
171 + CATEGORY ▸ choose where to inject
172 + -------------------------------------------------------------------- */
173 +private function embed_taboola_content_location_category(
174 + string $html,
175 + array $taboola_content,
176 + string $selector ) : string {
177 +
178 + $formatted = $this->format_taboola_content_category( $taboola_content );
179 +
180 + /* A. no selector → bottom of page */
181 + if ( $selector === '' ) {
182 + return str_ireplace( '</body>', $formatted . '</body>', $html );
183 + }
184 +
185 + /* B. selector given → try server-side injection */
186 + $doc = str_get_html( $html );
187 + if ( ! $doc ) {
188 + // parser failed → graceful fallback
189 + return str_ireplace( '</body>', $formatted . '</body>', $html );
190 + }
191 +
192 + $occurrence = max( 0, $this->settings->category_location_string_occurrence - 1 );
193 + $target = $doc->find( $selector, $occurrence );
194 +
195 + if ( $target ) { // selector found
196 + $target->outertext .= $formatted;
197 + return (string) $doc; // done – no duplicate
198 + }
199 +
200 + /* C. selector not found → bottom of page */
201 + return str_ireplace( '</body>', $formatted . '</body>', $html );
202 +}
203 +
204 +/* ------------------------------------------------------------------ */
205 +public function tb_home_buffer_flush() {
206 + if ( ob_get_length() ) {
207 + echo ob_get_clean();
208 + }
209 +}
210 +
211 +
212 +
213 +
214 +function plugin_action_links($links, $file) {
119 215 static $this_plugin;
120 216
121 217 if (!$this_plugin) {
122 218 $this_plugin = plugin_basename(__FILE__);
@@ -138,14 +234,17 @@
138 234 private function should_show_content_widget_mid(){
139 235 // PC - only if v2 was installed:
140 236 if (!$this->is_db_updated_for_min_ver("2.0.0"))
141 237 return false;
142 - $retVal1 = ((trim($this->settings->publisher_id) != '') && is_single() && $this->settings->mid_enabled && trim($this->settings->mid_widget_id) != '');
238 + $retVal1 = ((trim($this->settings->publisher_id) != '') && is_single() && !empty($this->settings->mid_enabled));
143 239 return $retVal1;
144 240 }
145 241
146 242 private function should_show_content_widget_home(){
147 243 // PC - only if v2 was installed:
244 +
245 + //error_log( print_r( $this->settings, true ) ); // TEMP line
246 +
148 247 if (!$this->is_db_updated_for_min_ver("2.0.0"))
149 248 return false;
150 249 $retVal2 = ((trim($this->settings->publisher_id) != '') && (is_front_page() || is_home()) && $this->settings->home_enabled && trim($this->settings->home_widget_id) != '');
151 250 return $retVal2;
@@ -157,9 +256,9 @@
157 256 }
158 257
159 258 // Determine if a taboola widget should be added somewhere on the current page (content or sidebar)
160 259 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();
260 + 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 261 }
163 262
164 263 function get_page_type(){
165 264 $page_type='article';
@@ -171,15 +270,21 @@
171 270 return $page_type;
172 271 }
173 272
174 273 // return the head loader script
175 - function taboola_header_loader_js() {
176 - $head_string = "";
274 + function taboola_header_loader_js() {
275 + if ($this->is_widget_on_page()){
276 +
277 + // New logic to get all mid-article locations
278 + $mid_locations_string = '';
279 + if (!empty($this->settings->mid_widgets)) {
280 + $mid_widgets_array = json_decode($this->settings->mid_widgets, true);
281 + if (is_array($mid_widgets_array)) {
282 + $locations = array_column($mid_widgets_array, 'location_string');
283 + $mid_locations_string = implode(', ', $locations);
284 + }
285 + }
177 286
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 287 $stringParams = array(
183 288 '{{PUBLISHER_ID}}' => $this->settings->publisher_id,
184 289 '{{PAGE_TYPE}}' => $this->get_page_type(),
185 290 '{{WORDPRESS_VERSION}}' => get_bloginfo('version'),
@@ -184,19 +289,18 @@
184 289 '{{PAGE_TYPE}}' => $this->get_page_type(),
185 290 '{{WORDPRESS_VERSION}}' => get_bloginfo('version'),
186 291 '{{PHP_VERSION}}' => phpversion(),
187 292 '{{PLUGIN_VERSION}}' => TABOOLA_PLUGIN_VERSION,
188 - '{{LOC_MID}}' => $this->settings->mid_location_string,
189 - '{{LOC_HOME}}' => $this->settings->home_location_string
293 + '{{LOC_MID}}' => $mid_locations_string,
294 + '{{LOC_HOME}}' => $this->settings->home_location_string ?? ''
190 295 );
191 296
192 297 $scriptWrapper = new JavaScriptWrapper("loaderInjectionScript.js",$stringParams);
193 - $head_string = $scriptWrapper->getScriptMarkupString();
298 + return $scriptWrapper->getScriptMarkupString();
194 299 }
195 - return $head_string;
300 + return "";
196 301 }
197 302
198 -
199 303 // This function is used for the hook action, injects the header content to the <head> tag.
200 304 function taboola_header_loader_inject(){
201 305 echo $this->taboola_header_loader_js();
202 306 }
@@ -244,24 +348,37 @@
244 348 return $content;
245 349 }
246 350
247 351 // Mid-article-widget
248 - function load_taboola_content_mid($content)
249 - {
250 - $taboola_content_mid = array();
352 + function load_taboola_content_mid($content) {
251 353 if ($this->should_show_content_widget_mid()){
354 + $mid_widgets = json_decode($this->settings->mid_widgets ?? '[]');
252 355
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;
356 + if (is_array($mid_widgets) && !empty($mid_widgets)) {
357 + foreach ($mid_widgets as $index => $widget) {
358 + $container_id = 'taboola-mid-article-thumbnails-' . $index;
359 +
360 + $widgetParams = array(
361 + '{{WIDGET_ID}}' => $widget->widget_id,
362 + '{{CONTAINER}}' => $container_id,
363 + '{{PLACEMENT}}' => $widget->placement
364 + );
260 365
261 - $content = $this->embed_taboola_content_location_mid($content,$taboola_content_mid,trim($this->settings->mid_location_string));
366 + $widgetScript = new JavaScriptWrapper("widgetInjectionScript.js", $widgetParams);
367 + $taboola_content_mid = array(
368 + TABOOLA_CONTENT_FORMAT_HTML => array("<div id='{$container_id}'></div>"),
369 + TABOOLA_CONTENT_FORMAT_SCRIPT => array($widgetScript),
370 + );
371 +
372 + $content = $this->embed_taboola_content_location_mid(
373 + $content,
374 + $taboola_content_mid,
375 + $widget->location_string,
376 + $widget->occurrence
377 + );
378 + }
379 + }
262 380 }
263 -
264 381 return $content;
265 382 }
266 383
267 384 // Homepage widget
@@ -266,9 +383,9 @@
266 383
267 384 // Homepage widget
268 385 function load_taboola_content_home($content)
269 386 {
270 -
387 + //error_log( 'TB-DEBUG ② entered load_taboola_content_home' );
271 388 $taboola_content_home = array();
272 389 if ($this->should_show_content_widget_home()){
273 390
274 391 $homeWidgetParams = array('{{WIDGET_ID}}' => $this->settings->home_widget_id,
@@ -283,9 +400,33 @@
283 400 }
284 401 return $content;
285 402
286 403 }
404 + /* ------------------------------------------------------------------
405 + * CATEGORY ▸ build widget & inject
406 + * ------------------------------------------------------------------*/
407 +public function load_taboola_content_category( $content ) {
287 408
409 + // 1) Build the DIV that Taboola will fill
410 + $taboola[TABOOLA_CONTENT_FORMAT_HTML][] =
411 + "<div id='taboola-category-thumbnails'></div>";
412 +
413 + // 2) Build the JS that loads the feed (reuse the wrapper)
414 + $taboola[TABOOLA_CONTENT_FORMAT_SCRIPT][] =
415 + new JavaScriptWrapper( 'widgetInjectionScript.js', [
416 + '{{WIDGET_ID}}' => $this->settings->category_widget_id,
417 + '{{CONTAINER}}' => 'taboola-category-thumbnails',
418 + '{{PLACEMENT}}' => $this->settings->category_placement,
419 + ]);
420 +
421 + // 3) Decide where to insert (selector or end of <body>)
422 + return $this->embed_taboola_content_location_category(
423 + $content,
424 + $taboola,
425 + trim( $this->settings->category_location_string )
426 + );
427 +}
428 +
288 429 // Below-article widget
289 430
290 431 // Extract the taboola content in the required format:
291 432 // String - for injecting on the servr side
@@ -352,8 +493,18 @@
352 493 return $ret_val;
353 494
354 495 }
355 496
497 +
498 + // ► CATEGORY formatter ◄
499 +private function format_taboola_content_category( $arr ) {
500 + return implode( "\n", [
501 + implode( "\n", $arr[TABOOLA_CONTENT_FORMAT_HTML] ?? [] ),
502 + '<script type="text/javascript">' .
503 + implode( "\n", $arr[TABOOLA_CONTENT_FORMAT_SCRIPT] ?? [] ) .
504 + '</script>',
505 + ]);
506 +}
356 507 // Below-article widget
357 508 // Do the actual logic of choosing where to place the taboola content.
358 509 function embed_taboola_content_location($content, $taboola_content){
359 510 $do_default = true;
@@ -379,9 +530,9 @@
379 530 }
380 531
381 532 // Mid-article widget
382 533 // 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){
534 + function embed_taboola_content_location_mid($content, $taboola_content_mid, $location, $occurrence = 1){
384 535 $do_default = true;
385 536
386 537 if (isset($location) && $location != ''){
387 538 $first_char = substr($location,0,1);
@@ -405,12 +556,14 @@
405 556
406 557 // server side selector provided (see simple_html_dom selectors http://simplehtmldom.sourceforge.net/manual.htm)
407 558 // basically it's CSS selectors like in jQuery
408 559 } else{
409 - require_once('simple_html_dom.php');
560 + if ( ! class_exists( 'simple_html_dom' ) ) {
561 + require_once('simple_html_dom.php');
562 + }
410 563
411 564 $html_doc = str_get_html($content);
412 - $target_location = $html_doc->find($location,($this->settings->mid_location_string_occurrence)-1);
565 + $target_location = $html_doc->find($location, ($occurrence) - 1);
413 566
414 567 // if the location was found within the html content
415 568 if (isset($target_location) && is_object($target_location)){
416 569
@@ -420,12 +573,8 @@
420 573 $do_default = false;
421 574 }
422 575 }
423 576 }
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 577
429 578 return $content;
430 579 }
431 580
@@ -432,8 +581,22 @@
432 581 // Homepage widget
433 582 // Do the actual logic of choosing where to place the taboola content based on the "location" attribute
434 583 function embed_taboola_content_location_home($content, $taboola_content_home, $location){
435 584
585 +
586 + // error_log( 'TB-DEBUG ③ raw selector string: [' . $location . ']' );
587 +
588 + // load parser
589 + //require_once plugin_dir_path( __FILE__ ) . 'simple_html_dom.php';
590 + // $html_doc = str_get_html( $content );
591 +
592 + // occurrence: settings is 1-based, find() wants 0-based
593 + // $occurrence = max( 1, intval( $this->settings->home_location_string_occurrence ) );
594 + // $node = $html_doc->find( $location, $occurrence - 1 );
595 + // $found = is_object( $node ) ? 1 : 0;
596 +
597 + // error_log( "TB-DEBUG ③ found {$found} matching node(s) for selector [{$location}]" );
598 + // ────────────────────────────
436 599 $do_default = true;
437 600
438 601 if (isset($location) && $location != ''){
439 602 $first_char = substr($location,0,1);
@@ -456,10 +619,12 @@
456 619 }
457 620
458 621 // server side selector provided (see simple_html_dom selectors http://simplehtmldom.sourceforge.net/manual.htm)
459 622 // basically it's CSS selectors like in jQuery
460 - } else{
623 + } else{
624 + if ( ! class_exists( 'simple_html_dom' ) ) {
461 625 require_once('simple_html_dom.php');
626 + }
462 627
463 628 $html_doc = str_get_html($content);
464 629 $target_location = $html_doc->find($location,($this->settings->home_location_string_occurrence)-1);
465 630
@@ -473,12 +638,13 @@
473 638 }
474 639 }
475 640 }
476 641 // 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 - // }
642 + if ($do_default){
643 + $content = $content.$this->format_taboola_content_home($taboola_content_home,TABOOLA_CONTENT_FORMAT_STRING);
644 + }
480 645 return $content;
646 +
481 647
482 648 }
483 649
484 650 function admin_generate_menu(){
@@ -511,33 +677,18 @@
511 677 }
512 678 }
513 679
514 680 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)";
681 + if(isset($_POST['mid_widget_id']) && is_array($_POST['mid_widget_id'])) {
682 + foreach ($_POST['mid_widget_id'] as $index => $widget_id) {
683 + if(trim($widget_id) == '') {
684 + $taboola_errors[] = "Mid-article Widget #".($index + 1)." > Widget ID";
685 + }
686 + if(trim($_POST['mid_placement'][$index]) == '') {
687 + $taboola_errors[] = "Mid-article Widget #".($index + 1)." > Placement Name";
688 + }
537 689 }
538 690 }
539 -
540 691 }
541 692
542 693 if(isset($_POST['home_enabled'])) {
543 694 if (trim(strip_tags($_POST['home_widget_id'])) == '') {
@@ -550,25 +701,45 @@
550 701 if (trim(strip_tags($_POST['home_location_string'])) == '') {
551 702 $taboola_errors[] = "Homepage > CSS selector";
552 703 }
553 704 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 705 if (!empty($_POST['home_location_string']) && !$this->is_home_location_string_occurrence_valid($_POST['home_location_string_occurrence'])) {
565 706 $taboola_errors[] = "Homepage > Occurance (must be >= 1)";
566 707 }
567 708 }
568 - }
709 + }
710 + if ( isset($_POST['category_enabled']) ) {
711 + if ( trim(strip_tags($_POST['category_widget_id'])) == '' )
712 + $taboola_errors[] = "Category > Widget ID";
713 + if ( trim(strip_tags($_POST['category_placement'])) == '' )
714 + $taboola_errors[] = "Category > Placement Name";
715 + }
569 716
570 717 if(count($taboola_errors) == 0){
718 +
719 + $mid_widgets_data = array();
720 + if (isset($_POST['mid_widget_id']) && is_array($_POST['mid_widget_id'])) {
721 + foreach ($_POST['mid_widget_id'] as $index => $widget_id) {
722 + if (!empty(trim($widget_id))) {
723 +
724 + $location_string = 'p';
725 + if (isset($_POST['mid_paragraph_ui_mode'][$index]) && $_POST['mid_paragraph_ui_mode'][$index] === 'Other') {
726 + if (isset($_POST['mid_location_string'][$index])) {
727 + $location_string = sanitize_text_field($_POST['mid_location_string'][$index]);
728 + }
729 + }
730 +
731 + $mid_widgets_data[] = array(
732 + 'widget_id' => sanitize_text_field($widget_id),
733 + 'placement' => sanitize_text_field($_POST['mid_placement'][$index]),
734 + 'location_string' => $location_string,
735 + 'occurrence' => intval($_POST['mid_location_string_occurrence'][$index]),
736 + );
737 + }
738 + }
739 + }
740 + $mid_widgets_json = json_encode($mid_widgets_data);
741 +
571 742 $data = array(
572 743 "publisher_id" => trim($_POST['publisher_id']),
573 744
574 745 "web_push_enabled" => isset($_POST['web_push_enabled']) ? true : false,
@@ -580,25 +751,24 @@
580 751
581 752 "out_of_content_enabled" => isset($_POST['out_of_content_enabled']) ? true : false,
582 753
583 754 "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 -
755 + "mid_widgets" => $mid_widgets_json,
756 +
591 757 "home_enabled" => isset($_POST['home_enabled']) ? true : false,
592 758 "home_widget_id" => !empty($_POST['home_widget_id']) ? trim($_POST['home_widget_id']) : '',
593 759 "home_placement" => !empty($_POST['home_placement']) ? trim($_POST['home_placement']) : '',
594 760
595 761 "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']) : ''
762 + "home_location_string" => !empty($_POST['home_location_string']) ? trim($_POST['home_location_string']) : '',
763 + "category_enabled" => isset($_POST['category_enabled']) ? true : false,
764 + "category_widget_id" => !empty($_POST['category_widget_id']) ? trim($_POST['category_widget_id']) : '',
765 + "category_placement" => !empty($_POST['category_placement']) ? trim($_POST['category_placement']) : '',
766 + "category_location_string_occurrence" => !empty($_POST['category_location_string_occurrence']) ? $_POST['category_location_string_occurrence'] : '',
767 + "category_location_string" => !empty($_POST['category_location_string']) ? trim($_POST['category_location_string']) : '',
768 +
597 769 );
598 770
599 - //var_dump($settings);
600 -
601 771 $is_valid_nonce = false;
602 772
603 773 if (isset( $_POST['my_plugin_nonce']) && wp_verify_nonce( $_POST['my_plugin_nonce'], 'my_plugin_update_field_action' ) ) {
604 774 $is_valid_nonce = true;
@@ -605,9 +775,9 @@
605 775 }
606 776
607 777 if ($is_valid_nonce) {
608 778 if($settings == NULL){
609 - $wpdb->insert($this->tbl_taboola_settings, $data, null, null);
779 + $wpdb->insert($this->tbl_taboola_settings, $data);
610 780 } else {
611 781 $wpdb->update($this->tbl_taboola_settings, $data, array('id' => $settings->id));
612 782 }
613 783 }
@@ -672,21 +842,9 @@
672 842 }
673 843 }
674 844
675 845 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 846 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 847 $table_name = $wpdb->prefix . "_taboola_settings";
690 848
691 849 if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
692 850 tb_write_log("Table NOT FOUND");
@@ -694,55 +852,31 @@
694 852 }
695 853 else {
696 854 tb_write_log("Table EXISTS");
697 855 }
698 - // END patch =================================================
699 -
700 - // Check the saved version in wp_options. Default to '1.0.0' if the option is not found.
701 856 $saved_version = get_option(TABOOLA_OPTION_NAME, '1.0.0');
702 857
703 858 $db_is_up_to_date = version_compare($saved_version, $min_ver, '>=');
704 - //tb_write_log($db_is_up_to_date);
705 859 return $db_is_up_to_date;
706 860
707 861 }
708 862
709 863 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 864 global $wpdb;
715 -
716 - // Check the saved version in wp_options. Default to '1.0.0' if the option is not found.
717 865 $saved_version = get_option(TABOOLA_OPTION_NAME, '1.0.0');
718 -
719 - // Get the currently loaded plugin version.
720 866 $plugin_version = $this->get_loaded_plugin_version();
721 -
722 867 $db_is_up_to_date = version_compare($saved_version, $plugin_version, '>=');
723 - //tb_write_log($db_is_up_to_date);
724 868 return $db_is_up_to_date;
725 -
726 869 }
727 870
728 871 function get_loaded_plugin_version() {
729 -
730 - $plugin_data = get_plugin_data( __FILE__ ); // Can be invoked from Admin page only
872 + $plugin_data = get_plugin_data( __FILE__ );
731 873 $loaded_plugin_version = $plugin_data['Version'];
732 874 return $loaded_plugin_version;
733 -
734 875 }
735 876
736 877 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 878 tb_write_log("SAVING NEW MIN VERSION: " . $min_ver); // PC
744 -
745 879 $saved_version = get_option(TABOOLA_OPTION_NAME, '');
746 880 if ($saved_version == '') {
747 881 add_option(TABOOLA_OPTION_NAME, $min_ver);
748 882 }
@@ -751,12 +885,9 @@
751 885 }
752 886 }
753 887
754 888 function update_db(){
755 -
756 - // If we are up to date, then skip this method...
757 889 if ($this->is_db_updated_for_min_ver(TABOOLA_MIN_VER)) {
758 - //tb_write_log("All up to date!");
759 890 return;
760 891 }
761 892
762 893 $this->save_taboola_version(TABOOLA_MIN_VER);
@@ -763,24 +894,17 @@
763 894
764 895 global $wpdb;
765 896 require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
766 897
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";
898 + $charset_collate = '';
899 + if ( ! empty( $wpdb->charset ) ) {
900 + $charset_collate = "DEFAULT CHARACTER SET {$wpdb->charset}";
774 901 }
902 + if ( ! empty( $wpdb->collate ) ) {
903 + $charset_collate .= " COLLATE {$wpdb->collate}";
904 + }
775 905
776 - // Check if this is an upgrade from v1
777 906 $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 907 $sql_table_settings = "
784 908 CREATE TABLE `" . $wpdb->prefix . "_taboola_settings` (
785 909 `id` INT NOT NULL AUTO_INCREMENT ,
786 910 `publisher_id` VARCHAR(255) DEFAULT NULL,
@@ -788,31 +912,23 @@
788 912 `publisher_id_push` INT(15) DEFAULT NULL,
789 913 `first_bc_enabled` TINYINT(1) NOT NULL DEFAULT FALSE,
790 914 `first_bc_widget_id` VARCHAR(255) DEFAULT NULL,
791 915 `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,
916 + `out_of_content_enabled` TINYINT(1) NOT NULL DEFAULT TRUE,
797 917 `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,
918 + `mid_widgets` TEXT DEFAULT NULL,
804 919 `home_enabled` TINYINT(1) NOT NULL DEFAULT FALSE,
805 920 `home_widget_id` VARCHAR(255) DEFAULT NULL,
806 921 `home_placement` VARCHAR(255) DEFAULT NULL,
807 922 `home_location_string` TEXT DEFAULT NULL,
808 923 `home_location_string_occurrence` SMALLINT DEFAULT NULL,
924 + `category_enabled` TINYINT(1) NOT NULL DEFAULT FALSE,
925 + `category_widget_id` VARCHAR(255) DEFAULT NULL,
926 + `category_placement` VARCHAR(255) DEFAULT NULL,
927 + `category_location_string` TEXT DEFAULT NULL,
928 + `category_location_string_occurrence` SMALLINT DEFAULT NULL,
809 929 PRIMARY KEY (`id`)
810 - )" . $charset_collate . ";";
811 -
812 - // tb_write_log($sql_table_settings);
813 -
814 - // create/update the table
930 + ) $charset_collate;";
815 931 dbDelta($sql_table_settings);
816 932 }
817 933 }
818 934 }
@@ -819,18 +935,13 @@
819 935
820 936 global $taboolaWP;
821 937 $taboolaWP = new TaboolaWP();
822 938
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 939 function tb_write_log($log_msg)
832 940 {
941 + if (!TABOOLA_DEBUG_MODE)
942 + return;
943 +
833 944 $log_filename = "logs";
834 945 if (!file_exists($log_filename))
835 946 {
836 947 mkdir($log_filename, 0777, true);
@@ -836,18 +947,14 @@
836 947 mkdir($log_filename, 0777, true);
837 948 }
838 949 $log_file_data = $log_filename.'/debug.log';
839 950
840 - // Add date/time
841 951 $date = date('Y-m-d H:i:s');
842 952 $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 -
953 +
954 + file_put_contents($log_file_data, $log_msg_with_date . "\n", FILE_APPEND);
847 955 }
848 956
849 -// Write to console
850 957 function tb_console_log( $data ){
851 958
852 959 If(wp_get_environment_type() === 'development') {
853 960
@@ -857,9 +964,8 @@
857 964
858 965 }
859 966 }
860 967
861 -// Write to webpage and stop
862 968 function tb_print_to_page($data) {
863 969 If(wp_get_environment_type() === 'development') {
864 970 print_r($data);
865 971 die;
@@ -865,5 +971,16 @@
865 971 die;
866 972 }
867 973 }
868 974
869 -//
975 +function enqueue_taboola_scripts() {
976 + wp_register_script(
977 + 'taboola-injector',
978 + plugins_url('js/js_inject.min.js', __FILE__),
979 + array(),
980 + null,
981 + true
982 + );
983 +
984 + wp_enqueue_script('taboola-injector');
985 +}
986 +add_action('wp_enqueue_scripts', 'enqueue_taboola_scripts');