PluginProbe
Taboola / trunk
Taboola vtrunk
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 +941 -102 1.0trunk View file →
@@ -1,60 +1,218 @@
1 1 <?php
2 2 /**
3 - * Plugin Name: Taboola
4 - * Plugin URI: http://taboola.com
5 - * Description: Taboola
6 - * Version: 1.0
7 - * Author: Taboola
3 + * Plugin Name: Taboola
4 + * Plugin URI: https://developers.taboola.com/web-integrations/docs/wordpress-plugin
5 + * Description: Taboola
6 + * Version: 3.1.0
7 + * Author: Taboola
8 8 */
9 9
10 -include_once('widget.php');
10 +define( 'TABOOLA_PLUGIN_VERSION', '3.1.0' ); // track every release
11 +define( 'TABOOLA_MIN_VER', '3.0' ); // bump only when DB changes
12 +define( 'TABOOLA_DEBUG_MODE', false );
11 13
12 -if (!class_exists('TaboolaWP')) {
13 - class TaboolaWP
14 - {
14 +define( 'TABOOLA_OPTION_NAME', 'taboola_plugin_version' );
15 15
16 - //save internal data
17 - public $data = array();
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' );
18 22
19 - function TaboolaWP()
20 - {
21 - global $wpdb;
22 - //initialize plugin constant
23 - DEFINE('TaboolaWP', true);
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 +}
24 28
25 - $this->plugin_name = plugin_basename(__FILE__);
26 - $this->plugin_directory = plugin_dir_path(__FILE__);
27 - $this->plugin_url = trailingslashit(WP_PLUGIN_URL . '/' . dirname(plugin_basename(__FILE__)));
28 - $this->settings = $wpdb->get_row("select * from ".$wpdb->prefix."_taboola_settings limit 1");
29 +if ( ! class_exists( 'TaboolaWP' ) ) {
30 +class TaboolaWP {
29 31
30 - $this->tbl_taboola_settings = $wpdb->prefix . '_taboola_settings';
32 + /* ─────────────────────────── properties ─────────────────────────── */
33 + public $data = [];
34 + public $_is_widget_on_page;
35 + public $_is_head_script_loaded = false;
31 36
32 - //activation function
33 - register_activation_hook($this->plugin_name, array(&$this, 'activate'));
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>
41 + <pre><code>{{SW}}</code></pre>
42 + <p>Also make sure it's reachable at <code>{{DOMAIN}}/sw.js</code></p>
43 +SWE;
34 44
45 + public $plugin_name;
46 + public $plugin_directory;
47 + public $plugin_url;
48 + public $settings;
49 + public $tbl_taboola_settings;
35 50
36 - // disable sidebar widget
37 - /*if($this->settings != NULL && !empty($this->settings->publisher_id)){
38 - //register Taboola widget
39 - add_action('widgets_init',
40 - create_function('', 'return register_widget("WP_Widget_Taboola");')
41 - );
42 - }*/
51 + /* ─────────────────────────── constructor ─────────────────────────── */
52 + public function __construct() {
53 + global $wpdb;
54 + //initialize plugin constant
55 + define( 'TaboolaWP', true );
43 56
44 - if (is_admin()) {
45 - //add menu for plugin
46 - add_action( 'admin_menu', array(&$this, 'admin_generate_menu') );
47 - add_filter('plugin_action_links', array(&$this, 'plugin_action_links'), 10, 2 );
48 - } else {
49 - if($this->settings != NULL){
50 - add_action('wp_head', array(&$this, 'taboola_header_loader_js'));
51 - add_action('wp_footer', array(&$this, 'taboola_footer_loader_js'));
52 - add_filter('the_content', array(&$this, 'load_taboola_content'));
53 - }
54 - }
57 + $this->_is_widget_on_page = false;
58 + $this->_is_head_script_loaded = false;
59 +
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 + );
69 +
70 + /* activation / upgrade */
71 + register_activation_hook( $this->plugin_name, [ $this, 'update_db' ] );
72 + add_action( 'admin_init', [ $this, 'update_db' ] );
73 +
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 + );
55 80 }
56 - function plugin_action_links($links, $file) {
81 +
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' ] );
97 +
98 + $sw = 'sw.js';
99 + $sw_path = ABSPATH . $sw;
100 +
101 + $content = file_exists( $sw_path ) ? file_get_contents( $sw_path ) : '';
102 +
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 );
106 + }
107 + $content = $this->tpl_sw . PHP_EOL . $content;
108 + if ( file_put_contents( $sw_path, $content ) === false ) {
109 + return $this->notice( $this->msg_sw_error );
110 + }
111 + }
112 + }
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' ] );
147 + }
148 +}
149 +public function tb_home_buffer_inject( $html ) {
150 + return $this->load_taboola_content_home( $html );
151 +}
152 +
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) {
57 215 static $this_plugin;
58 216
59 217 if (!$this_plugin) {
60 218 $this_plugin = plugin_basename(__FILE__);
@@ -66,125 +224,806 @@
66 224 }
67 225
68 226 return $links;
69 227 }
70 - function taboola_header_loader_js() {
71 - $script1_content = str_replace('{{PUBLISHER_ID}}', $this->settings->publisher_id, file_get_contents($this->plugin_directory.'js/script1.js'));
72 - echo '<script type="text/javascript">'.$script1_content.'</script>';
228 +
229 + private function should_show_content_widget(){
230 + $retVal = ((trim($this->settings->publisher_id) != '') && is_single() && $this->settings->first_bc_enabled && trim($this->settings->first_bc_widget_id) != '');
231 + return $retVal;
73 232 }
233 +
234 + private function should_show_content_widget_mid(){
235 + // PC - only if v2 was installed:
236 + if (!$this->is_db_updated_for_min_ver("2.0.0"))
237 + return false;
238 + $retVal1 = ((trim($this->settings->publisher_id) != '') && is_single() && !empty($this->settings->mid_enabled));
239 + return $retVal1;
240 + }
241 +
242 + private function should_show_content_widget_home(){
243 + // PC - only if v2 was installed:
244 +
245 + //error_log( print_r( $this->settings, true ) ); // TEMP line
246 +
247 + if (!$this->is_db_updated_for_min_ver("2.0.0"))
248 + return false;
249 + $retVal2 = ((trim($this->settings->publisher_id) != '') && (is_front_page() || is_home()) && $this->settings->home_enabled && trim($this->settings->home_widget_id) != '');
250 + return $retVal2;
251 + }
252 +
253 + private function should_show_sidebar_widget(){
254 + $retVal = ((trim($this->settings->publisher_id) != '') && is_active_widget( false, false, TABOOLA_WIDGET_BASE_ID, true ));
255 + return $retVal;
256 + }
257 +
258 + // Determine if a taboola widget should be added somewhere on the current page (content or sidebar)
259 + function is_widget_on_page(){
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() ;
261 + }
262 +
263 + function get_page_type(){
264 + $page_type='article';
265 + if (is_front_page()){
266 + $page_type='home';
267 + }else if (is_category() || is_archive() || is_search()){
268 + $page_type='category';
269 + }
270 + return $page_type;
271 + }
272 +
273 + // return the head loader script
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 + }
286 +
287 + $stringParams = array(
288 + '{{PUBLISHER_ID}}' => $this->settings->publisher_id,
289 + '{{PAGE_TYPE}}' => $this->get_page_type(),
290 + '{{WORDPRESS_VERSION}}' => get_bloginfo('version'),
291 + '{{PHP_VERSION}}' => phpversion(),
292 + '{{PLUGIN_VERSION}}' => TABOOLA_PLUGIN_VERSION,
293 + '{{LOC_MID}}' => $mid_locations_string,
294 + '{{LOC_HOME}}' => $this->settings->home_location_string ?? ''
295 + );
296 +
297 + $scriptWrapper = new JavaScriptWrapper("loaderInjectionScript.js",$stringParams);
298 + return $scriptWrapper->getScriptMarkupString();
299 + }
300 + return "";
301 + }
302 +
303 + // This function is used for the hook action, injects the header content to the <head> tag.
304 + function taboola_header_loader_inject(){
305 + echo $this->taboola_header_loader_js();
306 + }
307 +
308 + // adding webpush loader
309 + function taboola_webpush_loader_js() {
310 + if(is_single() || is_home() || is_category() || is_front_page()){
311 + $stringParamsWeb = array(
312 + '{{PUBLISHER_ID}}' => $this->settings->publisher_id_push
313 + );
314 + $webpushInjectionScript = new JavaScriptWrapper("webPush.js",$stringParamsWeb);
315 + echo $webpushInjectionScript;
316 + }
317 + }
318 +
74 319 function taboola_footer_loader_js() {
75 - $script3_content = file_get_contents($this->plugin_directory.'js/script3.js');
76 - echo '<script type="text/javascript">'.$script3_content.'</script>';
320 +
321 + // Only adding flush script if a widget is going to be placed on the page.
322 + if ($this->is_widget_on_page()){
323 + $flushInjectionScript = new JavaScriptWrapper('flushInjectionScript.js',array());
324 + echo $flushInjectionScript->getScriptMarkupString();
325 + }
77 326 }
78 327
328 + // Below-article widget
79 329 function load_taboola_content($content)
80 330 {
81 - if (trim($this->settings->publisher_id) == ''){
82 - return $content;
331 + $taboola_content = array();
332 + if ($this->should_show_content_widget()){
333 +
334 + $firstWidgetParams = array(
335 + '{{WIDGET_ID}}' => $this->settings->first_bc_widget_id,
336 + '{{CONTAINER}}' => 'taboola-below-article-thumbnails',
337 + // PC - If v2 was not installed, then use v1 logic
338 + '{{PLACEMENT}}' => (!empty($this->settings->first_bc_placement)) ? $this->settings->first_bc_placement : 'below-article' // In case v1 upgrade has not run yet
339 + );
340 +
341 + $firstWidgetScript = new JavaScriptWrapper("widgetInjectionScript.js",$firstWidgetParams);
342 + $taboola_content[TABOOLA_CONTENT_FORMAT_HTML][] = "<div id='taboola-below-article-thumbnails'></div>";
343 + $taboola_content[TABOOLA_CONTENT_FORMAT_SCRIPT][] = $firstWidgetScript;
344 +
345 + $content = $this->embed_taboola_content_location($content,$taboola_content);
83 346 }
84 - if (is_single()) {
85 347
86 - if($this->settings->first_bc_enabled && trim($this->settings->first_bc_widget_id) != ''){
87 - $script2_content = str_replace('{{WIDGET_ID}}', $this->settings->first_bc_widget_id, file_get_contents($this->plugin_directory.'js/script2.js'));
88 - $script2_content = str_replace('{{CONTAINER}}', 'taboola-below-article', $script2_content);
89 - $script2_content = str_replace('{{PLACEMENT}}', 'below-article', $script2_content);
348 + return $content;
349 + }
90 350
91 - $content = $content .
92 - '<div id="taboola-below-article" style="'.$this->settings->first_bc_custom_css.'"></div>' .
93 - '<script type="text/javascript">'.$script2_content.'</script>';
94 - if($this->settings->second_bc_enabled && trim($this->settings->second_bc_widget_id) != ''){
95 - $script2_content = str_replace('{{WIDGET_ID}}', $this->settings->second_bc_widget_id, file_get_contents($this->plugin_directory.'js/script2.js'));
96 - $script2_content = str_replace('{{CONTAINER}}', 'taboola-below-article-second', $script2_content);
97 - $script2_content = str_replace('{{PLACEMENT}}', 'below-article-2nd', $script2_content);
351 + // Mid-article-widget
352 + function load_taboola_content_mid($content) {
353 + if ($this->should_show_content_widget_mid()){
354 + $mid_widgets = json_decode($this->settings->mid_widgets ?? '[]');
98 355
99 - $content = $content .
100 - '<div id="taboola-below-article-second" style="'.$this->settings->second_bc_custom_css.'"></div>' .
101 - '<script type="text/javascript">'.$script2_content.'</script>';
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 + );
365 +
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 + );
102 378 }
103 379 }
104 380 }
381 + return $content;
382 + }
105 383
384 + // Homepage widget
385 + function load_taboola_content_home($content)
386 + {
387 + //error_log( 'TB-DEBUG ② entered load_taboola_content_home' );
388 + $taboola_content_home = array();
389 + if ($this->should_show_content_widget_home()){
390 +
391 + $homeWidgetParams = array('{{WIDGET_ID}}' => $this->settings->home_widget_id,
392 + '{{CONTAINER}}' => 'taboola-homepage-thumbnails',
393 + '{{PLACEMENT}}' => $this->settings->home_placement);
394 +
395 + $homeWidgetScript = new JavaScriptWrapper("widgetInjectionScript.js",$homeWidgetParams);
396 + $taboola_content_home[TABOOLA_CONTENT_FORMAT_HTML][] = "<div id='taboola-homepage-thumbnails'></div>";
397 + $taboola_content_home[TABOOLA_CONTENT_FORMAT_SCRIPT][] = $homeWidgetScript;
398 +
399 + $content = $this->embed_taboola_content_location_home($content,$taboola_content_home,trim($this->settings->home_location_string));
400 + }
106 401 return $content;
402 +
107 403 }
404 + /* ------------------------------------------------------------------
405 + * CATEGORY ▸ build widget & inject
406 + * ------------------------------------------------------------------*/
407 +public function load_taboola_content_category( $content ) {
108 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 +
429 + // Below-article widget
430 +
431 + // Extract the taboola content in the required format:
432 + // String - for injecting on the servr side
433 + // Script or HTML - for injecting on the client side
434 + function format_taboola_content($taboola_content,$format){
435 + $ret_val = null;
436 +
437 + switch($format){
438 + case TABOOLA_CONTENT_FORMAT_STRING:
439 + $result_string = join("",$taboola_content[TABOOLA_CONTENT_FORMAT_HTML]).
440 + "<script type='text/javascript'>".join("\n",$taboola_content[TABOOLA_CONTENT_FORMAT_SCRIPT])."</script>";
441 + $ret_val = $result_string;
442 + break;
443 +
444 + // script or html
445 + default:
446 + $ret_val = str_replace("\n","",join("",$taboola_content[$format]));
447 + break;
448 + }
449 +
450 + return $ret_val;
451 + }
452 +
453 +
454 + // Mid-article widget
455 + function format_taboola_content_mid($taboola_content_mid,$format){
456 + $ret_val = null;
457 +
458 + switch($format){
459 + case TABOOLA_CONTENT_FORMAT_STRING:
460 + $result_string = join("",$taboola_content_mid[TABOOLA_CONTENT_FORMAT_HTML]).
461 + "<script type='text/javascript'>".join("\n",$taboola_content_mid[TABOOLA_CONTENT_FORMAT_SCRIPT])."</script>";
462 + $ret_val = $result_string;
463 + break;
464 +
465 + // script or html
466 + default:
467 + $ret_val = str_replace("\n","",join("",$taboola_content_mid[$format]));
468 + break;
469 + }
470 +
471 + return $ret_val;
472 + }
473 +
474 +
475 + // Homepage widget
476 + function format_taboola_content_home($taboola_content_home,$format){
477 +
478 + $ret_val = null;
479 +
480 + switch($format){
481 + case TABOOLA_CONTENT_FORMAT_STRING:
482 + $result_string = join("",$taboola_content_home[TABOOLA_CONTENT_FORMAT_HTML]).
483 + "<script type='text/javascript'>".join("\n",$taboola_content_home[TABOOLA_CONTENT_FORMAT_SCRIPT])."</script>";
484 + $ret_val = $result_string;
485 + break;
486 +
487 + // script or html
488 + default:
489 + $ret_val = str_replace("\n","",join("",$taboola_content_home[$format]));
490 + break;
491 + }
492 +
493 + return $ret_val;
494 +
495 + }
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 +}
507 + /* ------------------------------------------------------------------
508 + * wpautop() hardening for anything injected through the_content.
509 + *
510 + * wpautop() pads block-level tags with blank lines, then splits the
511 + * content on blank lines and wraps each chunk in <p>. Its <script>
512 + * protection only runs after that split, so two things tear an injected
513 + * <script> apart and spill its tail onto the page as reader-visible text:
514 + * 1. a blank line anywhere in the script body, and
515 + * 2. a block-level tag (e.g. <div>) sitting inside a JS string literal.
516 + * Both have to be neutralised; fixing only one still breaks.
517 + * ------------------------------------------------------------------*/
518 +
519 + // Hide markup from wpautop's block-tag scan. \x3C decodes back to "<"
520 + // when the surrounding JS string literal is evaluated.
521 + private function js_escape_markup($markup){
522 + return str_replace('<', '\x3C', (string) $markup);
523 + }
524 +
525 + // Collapse blank lines so wpautop has no paragraph boundary to split on.
526 + private function wpautop_safe_script($script){
527 + return preg_replace('/(\R[ \t]*){2,}/', "\n", (string) $script);
528 + }
529 +
530 + // Below-article widget
531 + // Do the actual logic of choosing where to place the taboola content.
532 + function embed_taboola_content_location($content, $taboola_content){
533 + $do_default = true;
534 +
535 + // tag is placed outside of content in order to allow "read more" functionality.
536 + if ($this->settings->out_of_content_enabled){
537 +
538 + $scriptWrapper = new JavaScriptWrapper("js_inject.min.js",array(
539 + "{{HTML}}" => $this->js_escape_markup($this->format_taboola_content($taboola_content,TABOOLA_CONTENT_FORMAT_HTML)),
540 + "{{SCRIPT}}" => $this->js_escape_markup($this->format_taboola_content($taboola_content,TABOOLA_CONTENT_FORMAT_SCRIPT)))
541 + );
542 + $scriptWrapper->appendScript("injectWidgetByMarker('tbmarker');");
543 + $content = $content."<span id='tbmarker'></span><script type='text/javascript'>".$this->wpautop_safe_script($scriptWrapper)."</script>";
544 + $do_default = false;
545 + }
546 +
547 + // Default for below-article widget - add to the end of the content
548 + if ($do_default){
549 + $content = $content.$this->format_taboola_content($taboola_content,TABOOLA_CONTENT_FORMAT_STRING);
550 + }
551 +
552 + return $content;
553 + }
554 +
555 + // Mid-article widget
556 + // Do the actual logic of choosing where to place the taboola content based on the "location" attribute
557 + function embed_taboola_content_location_mid($content, $taboola_content_mid, $location, $occurrence = 1){
558 + $do_default = true;
559 +
560 + if (isset($location) && $location != ''){
561 + $first_char = substr($location,0,1);
562 +
563 + // DIV/XPATH provided for JS handling
564 + if ($first_char == TABOOLA_JS_MARKER){
565 + $full_indicator = substr($location,0,strlen(TABOOLA_JS_INDICATOR));
566 +
567 + if ($full_indicator == TABOOLA_JS_INDICATOR){
568 +
569 + $xpath = substr($location,strlen(TABOOLA_JS_INDICATOR));
570 + $scriptWrapper = new JavaScriptWrapper("js_inject.min.js",array(
571 + "{{HTML}}" => $this->js_escape_markup($this->format_taboola_content_mid($taboola_content_mid,TABOOLA_CONTENT_FORMAT_HTML)),
572 + "{{SCRIPT}}" => $this->js_escape_markup($this->format_taboola_content_mid($taboola_content_mid,TABOOLA_CONTENT_FORMAT_SCRIPT)))
573 + );
574 + $scriptWrapper->appendScript("injectWidgetByXpath('".$xpath."');");
575 + $content = $content."<span id='tbdefault'></span><script type='text/javascript'>".$this->wpautop_safe_script($scriptWrapper)."</script>";
576 +
577 + $do_default = false;
578 + }
579 +
580 + // server side selector provided (see simple_html_dom selectors http://simplehtmldom.sourceforge.net/manual.htm)
581 + // basically it's CSS selectors like in jQuery
582 + } else{
583 + if ( ! class_exists( 'simple_html_dom' ) ) {
584 + require_once('simple_html_dom.php');
585 + }
586 +
587 + $html_doc = str_get_html($content);
588 + $target_location = $html_doc->find($location, ($occurrence) - 1);
589 +
590 + // if the location was found within the html content
591 + if (isset($target_location) && is_object($target_location)){
592 +
593 + // adding taboola content AFTER the target location
594 + $target_location->outertext = $target_location->outertext.$this->format_taboola_content_mid($taboola_content_mid,TABOOLA_CONTENT_FORMAT_STRING);
595 + $content = $html_doc;
596 + $do_default = false;
597 + }
598 + }
599 + }
600 +
601 + return $content;
602 + }
603 +
604 + // Homepage widget
605 + // Do the actual logic of choosing where to place the taboola content based on the "location" attribute
606 + function embed_taboola_content_location_home($content, $taboola_content_home, $location){
607 +
608 +
609 + // error_log( 'TB-DEBUG ③ raw selector string: [' . $location . ']' );
610 +
611 + // load parser
612 + //require_once plugin_dir_path( __FILE__ ) . 'simple_html_dom.php';
613 + // $html_doc = str_get_html( $content );
614 +
615 + // occurrence: settings is 1-based, find() wants 0-based
616 + // $occurrence = max( 1, intval( $this->settings->home_location_string_occurrence ) );
617 + // $node = $html_doc->find( $location, $occurrence - 1 );
618 + // $found = is_object( $node ) ? 1 : 0;
619 +
620 + // error_log( "TB-DEBUG ③ found {$found} matching node(s) for selector [{$location}]" );
621 + // ────────────────────────────
622 + $do_default = true;
623 +
624 + if (isset($location) && $location != ''){
625 + $first_char = substr($location,0,1);
626 +
627 + // DIV/XPATH provided for JS handling
628 + if ($first_char == TABOOLA_JS_MARKER){
629 + $full_indicator = substr($location,0,strlen(TABOOLA_JS_INDICATOR));
630 +
631 + if ($full_indicator == TABOOLA_JS_INDICATOR){
632 +
633 + $xpath = substr($location,strlen(TABOOLA_JS_INDICATOR));
634 + $scriptWrapper = new JavaScriptWrapper("js_inject.min.js",array(
635 + "{{HTML}}" => $this->js_escape_markup($this->format_taboola_content_home($taboola_content_home,TABOOLA_CONTENT_FORMAT_HTML)),
636 + "{{SCRIPT}}" => $this->js_escape_markup($this->format_taboola_content_home($taboola_content_home,TABOOLA_CONTENT_FORMAT_SCRIPT)))
637 + );
638 + $scriptWrapper->appendScript("injectWidgetByXpath('".$xpath."');");
639 + $content = $content."<span id='tbdefault'></span><script type='text/javascript'>".$this->wpautop_safe_script($scriptWrapper)."</script>";
640 +
641 + $do_default = false;
642 + }
643 +
644 + // server side selector provided (see simple_html_dom selectors http://simplehtmldom.sourceforge.net/manual.htm)
645 + // basically it's CSS selectors like in jQuery
646 + } else{
647 + if ( ! class_exists( 'simple_html_dom' ) ) {
648 + require_once('simple_html_dom.php');
649 + }
650 +
651 + $html_doc = str_get_html($content);
652 + $target_location = $html_doc->find($location,($this->settings->home_location_string_occurrence)-1);
653 +
654 + // if the location was found within the html content
655 + if (isset($target_location) && is_object($target_location)){
656 +
657 + // adding taboola content AFTER the target location
658 + $target_location->outertext = $target_location->outertext.$this->format_taboola_content_home($taboola_content_home,TABOOLA_CONTENT_FORMAT_STRING);
659 + $content = $html_doc;
660 + $do_default = false;
661 + }
662 + }
663 + }
664 + // Default for below-article widget - add to the end of the content
665 + if ($do_default){
666 + $content = $content.$this->format_taboola_content_home($taboola_content_home,TABOOLA_CONTENT_FORMAT_STRING);
667 + }
668 + return $content;
669 +
670 +
671 + }
672 +
109 673 function admin_generate_menu(){
110 674 global $current_user;
111 - add_menu_page('Taboola', 'Taboola', 'manage_options', 'taboola_widget', array(&$this, 'admin_taboola_settings'), $this->plugin_url.'img/taboola_icon.png', 110);
675 + add_menu_page(__('Taboola','taboola_widget'), __('Taboola','taboola_widget'), 'manage_options', 'taboola_widget', array(&$this, 'admin_taboola_settings'), $this->plugin_url.'img/taboola_icon.png', 110);
112 676 }
113 677
678 + // Empty numeric inputs must reach MySQL as NULL, not '', or strict mode
679 + // rejects the whole row.
680 + private function nullable_int($value){
681 + return (isset($value) && trim((string) $value) !== '') ? (int) $value : null;
682 + }
683 +
114 684 function admin_taboola_settings(){
115 685 global $wpdb;
116 686 $settings = $wpdb->get_row("select * from ".$wpdb->prefix."_taboola_settings limit 1");
117 687 $taboola_errors = array();
688 + $taboola_save_error = '';
118 689 if($_SERVER['REQUEST_METHOD'] == 'POST'){
119 690
120 - if(trim($_POST['publisher_id']) == ''){
121 - $taboola_errors[] = "Please add a 'Publisher ID' in order to apply changes to your widgets";
691 + if(trim(strip_tags($_POST['publisher_id'])) == ''){
692 + $taboola_errors[] = "Publisher ID";
122 693 }
123 - if(($_POST['first_bc_enabled'] == 'on' && trim($_POST['first_bc_widget_id']) == '') ||
124 - ($_POST['second_bc_enabled'] == 'on' && trim($_POST['second_bc_widget_id']) == '')
125 - ){
126 - $taboola_errors[] = "Please add a 'Widget ID' in order to apply changes to your widgets";
694 +
695 + if(isset($_POST['web_push_enabled'])) {
696 + if (trim(strip_tags($_POST['publisher_id_push'])) == '') {
697 + $taboola_errors[] = "Publisher Push ID";
698 + }
127 699 }
700 +
701 + if(isset($_POST['first_bc_enabled'])) {
702 + if (trim(strip_tags($_POST['first_bc_widget_id'])) == '') {
703 + $taboola_errors[] = "Below-article > Widget ID";
704 + }
705 + if (trim(strip_tags($_POST['first_bc_placement'])) == '') {
706 + $taboola_errors[] = "Below-article > Placement Name";
707 + }
708 + }
709 +
710 + if(isset($_POST['mid_enabled'])) {
711 + if(isset($_POST['mid_widget_id']) && is_array($_POST['mid_widget_id'])) {
712 + foreach ($_POST['mid_widget_id'] as $index => $widget_id) {
713 + if(trim($widget_id) == '') {
714 + $taboola_errors[] = "Mid-article Widget #".($index + 1)." > Widget ID";
715 + }
716 + if(trim($_POST['mid_placement'][$index]) == '') {
717 + $taboola_errors[] = "Mid-article Widget #".($index + 1)." > Placement Name";
718 + }
719 + }
720 + }
721 + }
722 +
723 + if(isset($_POST['home_enabled'])) {
724 + if (trim(strip_tags($_POST['home_widget_id'])) == '') {
725 + $taboola_errors[] = "Homepage > Widget ID";
726 + }
727 + if (trim(strip_tags($_POST['home_placement'])) == '') {
728 + $taboola_errors[] = "Homepage > Placement Name";
729 + }
730 +
731 + if (trim(strip_tags($_POST['home_location_string'])) == '') {
732 + $taboola_errors[] = "Homepage > CSS selector";
733 + }
734 + else {
735 + if (!empty($_POST['home_location_string']) && !$this->is_home_location_string_occurrence_valid($_POST['home_location_string_occurrence'])) {
736 + $taboola_errors[] = "Homepage > Occurance (must be >= 1)";
737 + }
738 + }
739 + }
740 + if ( isset($_POST['category_enabled']) ) {
741 + if ( trim(strip_tags($_POST['category_widget_id'])) == '' )
742 + $taboola_errors[] = "Category > Widget ID";
743 + if ( trim(strip_tags($_POST['category_placement'])) == '' )
744 + $taboola_errors[] = "Category > Placement Name";
745 + }
746 +
128 747 if(count($taboola_errors) == 0){
748 +
749 + $mid_widgets_data = array();
750 + if (isset($_POST['mid_widget_id']) && is_array($_POST['mid_widget_id'])) {
751 + foreach ($_POST['mid_widget_id'] as $index => $widget_id) {
752 + if (!empty(trim($widget_id))) {
753 +
754 + $location_string = 'p';
755 + if (isset($_POST['mid_paragraph_ui_mode'][$index]) && $_POST['mid_paragraph_ui_mode'][$index] === 'Other') {
756 + if (isset($_POST['mid_location_string'][$index])) {
757 + $location_string = sanitize_text_field($_POST['mid_location_string'][$index]);
758 + }
759 + }
129 760
761 + $mid_widgets_data[] = array(
762 + 'widget_id' => sanitize_text_field($widget_id),
763 + 'placement' => sanitize_text_field($_POST['mid_placement'][$index]),
764 + 'location_string' => $location_string,
765 + 'occurrence' => intval($_POST['mid_location_string_occurrence'][$index]),
766 + );
767 + }
768 + }
769 + }
770 + $mid_widgets_json = json_encode($mid_widgets_data);
771 +
772 + /* $wpdb formats every value as %s unless told otherwise, so a PHP
773 + false or an empty string reaches MySQL as ''. Under
774 + STRICT_TRANS_TABLES (the MySQL 8 default) '' is rejected for the
775 + TINYINT/INT columns and the whole write is refused. Send real
776 + integers for the flags and NULL for empty numeric fields. */
130 777 $data = array(
131 778 "publisher_id" => trim($_POST['publisher_id']),
132 - "first_bc_enabled" => $_POST['first_bc_enabled'] == 'on' ? true : false,
133 - "first_bc_widget_id" => trim($_POST['first_bc_widget_id']),
134 - "first_bc_custom_css" => trim($_POST['first_bc_custom_css']),
135 - "second_bc_enabled" => $_POST['second_bc_enabled'] == 'on' ? true : false,
136 - "second_bc_widget_id" => trim($_POST['second_bc_widget_id']),
137 - "second_bc_custom_css" => trim($_POST['second_bc_custom_css'])
779 +
780 + "web_push_enabled" => isset($_POST['web_push_enabled']) ? 1 : 0,
781 + "publisher_id_push" => $this->nullable_int($_POST['publisher_id_push'] ?? null),
782 +
783 + "first_bc_enabled" => isset($_POST['first_bc_enabled']) ? 1 : 0,
784 + "first_bc_widget_id" => !empty($_POST['first_bc_widget_id']) ? trim($_POST['first_bc_widget_id']) : '',
785 + "first_bc_placement" => !empty($_POST['first_bc_placement']) ? trim($_POST['first_bc_placement']) : '',
786 +
787 + "out_of_content_enabled" => isset($_POST['out_of_content_enabled']) ? 1 : 0,
788 +
789 + "mid_enabled" => isset($_POST['mid_enabled']) ? 1 : 0,
790 + "mid_widgets" => $mid_widgets_json,
791 +
792 + "home_enabled" => isset($_POST['home_enabled']) ? 1 : 0,
793 + "home_widget_id" => !empty($_POST['home_widget_id']) ? trim($_POST['home_widget_id']) : '',
794 + "home_placement" => !empty($_POST['home_placement']) ? trim($_POST['home_placement']) : '',
795 +
796 + "home_location_string_occurrence" => $this->nullable_int($_POST['home_location_string_occurrence'] ?? null),
797 + "home_location_string" => !empty($_POST['home_location_string']) ? trim($_POST['home_location_string']) : '',
798 + "category_enabled" => isset($_POST['category_enabled']) ? 1 : 0,
799 + "category_widget_id" => !empty($_POST['category_widget_id']) ? trim($_POST['category_widget_id']) : '',
800 + "category_placement" => !empty($_POST['category_placement']) ? trim($_POST['category_placement']) : '',
801 + "category_location_string_occurrence" => $this->nullable_int($_POST['category_location_string_occurrence'] ?? null),
802 + "category_location_string" => !empty($_POST['category_location_string']) ? trim($_POST['category_location_string']) : '',
803 +
138 804 );
139 - //var_dump($settings);
140 - if($settings == NULL){
141 - $wpdb->insert($this->tbl_taboola_settings, $data, null, null);
805 +
806 + $is_valid_nonce = false;
807 +
808 + if (isset( $_POST['my_plugin_nonce']) && wp_verify_nonce( $_POST['my_plugin_nonce'], 'my_plugin_update_field_action' ) ) {
809 + $is_valid_nonce = true;
810 + }
811 +
812 + if ($is_valid_nonce) {
813 + if($settings == NULL){
814 + $saved = $wpdb->insert($this->tbl_taboola_settings, $data);
815 + } else {
816 + $saved = $wpdb->update($this->tbl_taboola_settings, $data, array('id' => $settings->id));
817 + }
818 +
819 + // update() returns 0 when nothing changed; only false is a failure.
820 + if ($saved === false) {
821 + $taboola_save_error = "The database rejected the write, so your changes were not saved: "
822 + . ($wpdb->last_error !== '' ? $wpdb->last_error : 'unknown database error');
823 + }
142 824 } else {
143 - $wpdb->update($this->tbl_taboola_settings, $data, array('id' => $settings->id));
825 + $taboola_save_error = "Security check failed - the settings page had been open too long. Reload it and apply your changes again.";
144 826 }
145 827 }
146 828 $settings = $wpdb->get_row("select * from ".$wpdb->prefix."_taboola_settings limit 1");
147 829 }
830 + include_once('settings.php');
831 + }
832 +
833 + function is_mid_location_string_valid($mid_location_string){
834 + // TODO:: validate the location string
835 + return true;
836 + }
148 837
838 + function is_mid_location_string_occurrence_valid($mid_location_string_occurrence){
149 839
150 - include_once('settings.php');
840 + if ((int)$mid_location_string_occurrence >= 1) {
841 + return true;
842 + }
843 + return false;
151 844 }
152 - function activate(){
845 +
846 + function is_home_location_string_valid($home_location_string){
847 + // TODO:: validate the location string
848 + return true;
849 + }
850 +
851 + function is_home_location_string_occurrence_valid($home_location_string_occurrence){
852 +
853 + if ((int)$home_location_string_occurrence >= 1) {
854 + return true;
855 + }
856 + return false;
857 + }
858 +
859 + function columnExists($column_name) {
153 860 global $wpdb;
861 + $table_name = $wpdb->prefix . "_taboola_settings";
862 + tb_write_log("table name : " . $table_name);
863 + tb_write_log("column name : " . $column_name);
154 864
155 - require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
865 + $column_exists = $wpdb->get_var("SHOW COLUMNS FROM $table_name LIKE '$column_name'") !== null;
156 866
157 - $settings_table = $this->tbl_taboola_settings;
867 + if ($column_exists) {
868 + tb_write_log("Yes - column " . $column_name . " DOES exist!"); //PC
869 + return true;
870 + } else {
871 + tb_write_log("No - column " . $column_name . " does NOT exist!"); //PC
872 + return false;
873 + }
874 + }
158 875
159 - //check mysql version
160 - if (version_compare(mysql_get_server_info(), '4.1.0', '>=')) {
161 - if (!empty($wpdb->charset))
162 - $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
163 - if (!empty($wpdb->collate))
164 - $charset_collate .= " COLLATE $wpdb->collate";
876 + function is_upgrade_from_v1() {
877 +
878 + if($this->columnExists("first_bc_widget_id") && !$this->columnExists("first_bc_placement")){
879 + tb_write_log("This IS a v1 upgrade!"); //PC
880 + return true;
881 + }
882 + else {
883 + tb_write_log("This is NOT a v1 upgrade!"); //PC
884 + return false;
165 885 }
886 + }
166 887
167 - //settings table structure
888 + function is_db_updated_for_min_ver($min_ver) {
889 + global $wpdb;
890 + $table_name = $wpdb->prefix . "_taboola_settings";
891 +
892 + if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
893 + tb_write_log("Table NOT FOUND");
894 + return false;
895 + }
896 + else {
897 + tb_write_log("Table EXISTS");
898 + }
899 + $saved_version = get_option(TABOOLA_OPTION_NAME, '1.0.0');
900 +
901 + $db_is_up_to_date = version_compare($saved_version, $min_ver, '>=');
902 + return $db_is_up_to_date;
903 +
904 + }
905 +
906 + function is_db_updated_for_current_ver() {
907 + global $wpdb;
908 + $saved_version = get_option(TABOOLA_OPTION_NAME, '1.0.0');
909 + $plugin_version = $this->get_loaded_plugin_version();
910 + $db_is_up_to_date = version_compare($saved_version, $plugin_version, '>=');
911 + return $db_is_up_to_date;
912 + }
913 +
914 + function get_loaded_plugin_version() {
915 + $plugin_data = get_plugin_data( __FILE__ );
916 + $loaded_plugin_version = $plugin_data['Version'];
917 + return $loaded_plugin_version;
918 + }
919 +
920 + function save_taboola_version($min_ver) {
921 + tb_write_log("SAVING NEW MIN VERSION: " . $min_ver); // PC
922 + $saved_version = get_option(TABOOLA_OPTION_NAME, '');
923 + if ($saved_version == '') {
924 + add_option(TABOOLA_OPTION_NAME, $min_ver);
925 + }
926 + else {
927 + update_option(TABOOLA_OPTION_NAME, $min_ver);
928 + }
929 + }
930 +
931 + function update_db(){
932 + if ($this->is_db_updated_for_min_ver(TABOOLA_MIN_VER)) {
933 + return;
934 + }
935 +
936 + $this->save_taboola_version(TABOOLA_MIN_VER);
937 +
938 + global $wpdb;
939 + require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
940 +
941 + $charset_collate = '';
942 + if ( ! empty( $wpdb->charset ) ) {
943 + $charset_collate = "DEFAULT CHARACTER SET {$wpdb->charset}";
944 + }
945 + if ( ! empty( $wpdb->collate ) ) {
946 + $charset_collate .= " COLLATE {$wpdb->collate}";
947 + }
948 +
949 + $is_upgrade_from_v1 = $this->is_upgrade_from_v1();
168 950 $sql_table_settings = "
169 951 CREATE TABLE `" . $wpdb->prefix . "_taboola_settings` (
170 952 `id` INT NOT NULL AUTO_INCREMENT ,
171 953 `publisher_id` VARCHAR(255) DEFAULT NULL,
954 + `web_push_enabled` TINYINT(1) NOT NULL DEFAULT FALSE,
955 + `publisher_id_push` INT(15) DEFAULT NULL,
172 956 `first_bc_enabled` TINYINT(1) NOT NULL DEFAULT FALSE,
173 957 `first_bc_widget_id` VARCHAR(255) DEFAULT NULL,
174 - `first_bc_custom_css` TEXT DEFAULT NULL,
175 - `second_bc_enabled` TINYINT(1) NOT NULL DEFAULT FALSE,
176 - `second_bc_widget_id` VARCHAR(255) DEFAULT NULL,
177 - `second_bc_custom_css` TEXT DEFAULT NULL,
958 + `first_bc_placement` VARCHAR(255) DEFAULT " . ($is_upgrade_from_v1 ? "'below-article'" : "NULL") .",
959 + `out_of_content_enabled` TINYINT(1) NOT NULL DEFAULT TRUE,
960 + `mid_enabled` TINYINT(1) NOT NULL DEFAULT FALSE,
961 + `mid_widgets` TEXT DEFAULT NULL,
962 + `home_enabled` TINYINT(1) NOT NULL DEFAULT FALSE,
963 + `home_widget_id` VARCHAR(255) DEFAULT NULL,
964 + `home_placement` VARCHAR(255) DEFAULT NULL,
965 + `home_location_string` TEXT DEFAULT NULL,
966 + `home_location_string_occurrence` SMALLINT DEFAULT NULL,
967 + `category_enabled` TINYINT(1) NOT NULL DEFAULT FALSE,
968 + `category_widget_id` VARCHAR(255) DEFAULT NULL,
969 + `category_placement` VARCHAR(255) DEFAULT NULL,
970 + `category_location_string` TEXT DEFAULT NULL,
971 + `category_location_string_occurrence` SMALLINT DEFAULT NULL,
178 972 PRIMARY KEY (`id`)
179 - )" . $charset_collate . ";";
180 -
181 - //check if table exists
182 - if ($wpdb->get_var("show tables like '" . $settings_table . "'") != $settings_table) {
973 + ) $charset_collate;";
183 974 dbDelta($sql_table_settings);
184 - }
185 975 }
186 976 }
187 977 }
188 978
189 979 global $taboolaWP;
190 -$taboolaWP = new TaboolaWP();
980 +$taboolaWP = new TaboolaWP();
981 +
982 +function tb_write_log($log_msg)
983 +{
984 + if (!TABOOLA_DEBUG_MODE)
985 + return;
986 +
987 + $log_filename = "logs";
988 + if (!file_exists($log_filename))
989 + {
990 + mkdir($log_filename, 0777, true);
991 + }
992 + $log_file_data = $log_filename.'/debug.log';
993 +
994 + $date = date('Y-m-d H:i:s');
995 + $log_msg_with_date = $date." : ".$log_msg;
996 +
997 + file_put_contents($log_file_data, $log_msg_with_date . "\n", FILE_APPEND);
998 +}
999 +
1000 +function tb_console_log( $data ){
1001 +
1002 + If(wp_get_environment_type() === 'development') {
1003 +
1004 + echo '<script>';
1005 + echo 'console.log('. json_encode( $data ) .')';
1006 + echo '</script>';
1007 +
1008 + }
1009 +}
1010 +
1011 +function tb_print_to_page($data) {
1012 + If(wp_get_environment_type() === 'development') {
1013 + print_r($data);
1014 + die;
1015 + }
1016 +}
1017 +
1018 +function enqueue_taboola_scripts() {
1019 + wp_register_script(
1020 + 'taboola-injector',
1021 + plugins_url('js/js_inject.min.js', __FILE__),
1022 + array(),
1023 + null,
1024 + true
1025 + );
1026 +
1027 + wp_enqueue_script('taboola-injector');
1028 +}
1029 +add_action('wp_enqueue_scripts', 'enqueue_taboola_scripts');